View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertNull;
24  import static org.junit.Assert.assertTrue;
25  
26  import org.apache.hadoop.hbase.testclassification.SmallTests;
27  import org.junit.Test;
28  import org.junit.experimental.categories.Category;
29  
30  @Category(SmallTests.class)
31  public class TestRegionLocations {
32  
33    ServerName sn0 = ServerName.valueOf("host0", 10, 10);
34    ServerName sn1 = ServerName.valueOf("host1", 10, 10);
35    ServerName sn2 = ServerName.valueOf("host2", 10, 10);
36    ServerName sn3 = ServerName.valueOf("host3", 10, 10);
37  
38    HRegionInfo info0 = hri(0);
39    HRegionInfo info1 = hri(1);
40    HRegionInfo info2 = hri(2);
41    HRegionInfo info9 = hri(9);
42  
43    @Test
44    public void testSizeMethods() {
45      RegionLocations list = new RegionLocations();
46      assertTrue(list.isEmpty());
47      assertEquals(0, list.size());
48      assertEquals(0, list.numNonNullElements());
49  
50      list = hrll((HRegionLocation)null);
51      assertTrue(list.isEmpty());
52      assertEquals(1, list.size());
53      assertEquals(0, list.numNonNullElements());
54  
55      HRegionInfo info0 = hri(0);
56      list = hrll(hrl(info0, null));
57      assertTrue(list.isEmpty());
58      assertEquals(1, list.size());
59      assertEquals(0, list.numNonNullElements());
60  
61      HRegionInfo info9 = hri(9);
62      list = hrll(hrl(info9, null));
63      assertTrue(list.isEmpty());
64      assertEquals(10, list.size());
65      assertEquals(0, list.numNonNullElements());
66  
67      list = hrll(hrl(info0, null), hrl(info9, null));
68      assertTrue(list.isEmpty());
69      assertEquals(10, list.size());
70      assertEquals(0, list.numNonNullElements());
71    }
72  
73    private HRegionInfo hri(int replicaId) {
74      TableName table = TableName.valueOf("table");
75      byte[] startKey = HConstants.EMPTY_START_ROW;
76      byte[] endKey = HConstants.EMPTY_END_ROW;
77      long regionId = System.currentTimeMillis();
78      HRegionInfo info = new HRegionInfo(table, startKey, endKey, false, regionId, replicaId);
79      return info;
80    }
81  
82    private HRegionLocation hrl(HRegionInfo hri, ServerName sn) {
83      return new HRegionLocation(hri, sn);
84    }
85  
86    private HRegionLocation hrl(HRegionInfo hri, ServerName sn, long seqNum) {
87      return new HRegionLocation(hri, sn, seqNum);
88    }
89  
90    private RegionLocations hrll(HRegionLocation ... locations) {
91      return new RegionLocations(locations);
92    }
93  
94    @Test
95    public void testRemoveByServer() {
96      RegionLocations list;
97  
98      // test remove from empty list
99      list = new RegionLocations();
100     assertTrue(list == list.removeByServer(sn0));
101 
102     // test remove from single element list
103     list = hrll(hrl(info0, sn0));
104     assertTrue(list == list.removeByServer(sn1));
105     list = list.removeByServer(sn0);
106     assertEquals(0, list.numNonNullElements());
107 
108     // test remove from multi element list
109     list = hrll(hrl(info0, sn0), hrl(info1, sn1), hrl(info2, sn2), hrl(info9, sn2));
110     assertTrue(list == list.removeByServer(sn3)); // no region is mapped to sn3
111     list = list.removeByServer(sn0);
112     assertNull(list.getRegionLocation(0));
113     assertEquals(sn1, list.getRegionLocation(1).getServerName());
114     assertEquals(sn2, list.getRegionLocation(2).getServerName());
115     assertNull(list.getRegionLocation(5));
116     assertEquals(sn2, list.getRegionLocation(9).getServerName());
117 
118     // test multi-element remove from multi element list
119     list = hrll(hrl(info0, sn1), hrl(info1, sn1), hrl(info2, sn0), hrl(info9, sn0));
120     list = list.removeByServer(sn0);
121     assertEquals(sn1, list.getRegionLocation(0).getServerName());
122     assertEquals(sn1, list.getRegionLocation(1).getServerName());
123     assertNull(list.getRegionLocation(2));
124     assertNull(list.getRegionLocation(5));
125     assertNull(list.getRegionLocation(9));
126   }
127 
128   @Test
129   public void testRemove() {
130     RegionLocations list;
131 
132     // test remove from empty list
133     list = new RegionLocations();
134     assertTrue(list == list.remove(hrl(info0, sn0)));
135 
136     // test remove from single element list
137     list = hrll(hrl(info0, sn0));
138     assertTrue(list == list.remove(hrl(info0, sn1)));
139     list = list.remove(hrl(info0, sn0));
140     assertTrue(list.isEmpty());
141 
142     // test remove from multi element list
143     list = hrll(hrl(info0, sn0), hrl(info1, sn1), hrl(info2, sn2), hrl(info9, sn2));
144     assertTrue(list == list.remove(hrl(info1, sn3))); // no region is mapped to sn3
145     list = list.remove(hrl(info0, sn0));
146     assertNull(list.getRegionLocation(0));
147     assertEquals(sn1, list.getRegionLocation(1).getServerName());
148     assertEquals(sn2, list.getRegionLocation(2).getServerName());
149     assertNull(list.getRegionLocation(5));
150     assertEquals(sn2, list.getRegionLocation(9).getServerName());
151 
152     list = list.remove(hrl(info9, sn2));
153     assertNull(list.getRegionLocation(0));
154     assertEquals(sn1, list.getRegionLocation(1).getServerName());
155     assertEquals(sn2, list.getRegionLocation(2).getServerName());
156     assertNull(list.getRegionLocation(5));
157     assertNull(list.getRegionLocation(9));
158 
159 
160     // test multi-element remove from multi element list
161     list = hrll(hrl(info0, sn1), hrl(info1, sn1), hrl(info2, sn0), hrl(info9, sn0));
162     list = list.remove(hrl(info9, sn0));
163     assertEquals(sn1, list.getRegionLocation(0).getServerName());
164     assertEquals(sn1, list.getRegionLocation(1).getServerName());
165     assertEquals(sn0, list.getRegionLocation(2).getServerName());
166     assertNull(list.getRegionLocation(5));
167     assertNull(list.getRegionLocation(9));
168   }
169 
170   @Test
171   public void testUpdateLocation() {
172     RegionLocations list;
173 
174     // test add to empty list
175     list = new RegionLocations();
176     list = list.updateLocation(hrl(info0, sn1), false, false);
177     assertEquals(sn1, list.getRegionLocation(0).getServerName());
178 
179     // test add to non-empty list
180     list = list.updateLocation(hrl(info9, sn3, 10), false, false);
181     assertEquals(sn3, list.getRegionLocation(9).getServerName());
182     assertEquals(10, list.size());
183     list = list.updateLocation(hrl(info2, sn2, 10), false, false);
184     assertEquals(sn2, list.getRegionLocation(2).getServerName());
185     assertEquals(10, list.size());
186 
187     // test update greater SeqNum
188     list = list.updateLocation(hrl(info2, sn3, 11), false, false);
189     assertEquals(sn3, list.getRegionLocation(2).getServerName());
190     assertEquals(sn3, list.getRegionLocation(9).getServerName());
191 
192     // test update equal SeqNum
193     list = list.updateLocation(hrl(info2, sn1, 11), false, false); // should not update
194     assertEquals(sn3, list.getRegionLocation(2).getServerName());
195     assertEquals(sn3, list.getRegionLocation(9).getServerName());
196     list = list.updateLocation(hrl(info2, sn1, 11), true, false); // should update
197     assertEquals(sn1, list.getRegionLocation(2).getServerName());
198     assertEquals(sn3, list.getRegionLocation(9).getServerName());
199 
200     // test force update
201     list = list.updateLocation(hrl(info2, sn2, 9), false, true); // should update
202     assertEquals(sn2, list.getRegionLocation(2).getServerName());
203     assertEquals(sn3, list.getRegionLocation(9).getServerName());
204   }
205 
206   @Test
207   public void testMergeLocations() {
208     RegionLocations list1, list2;
209 
210     // test merge empty lists
211     list1 = new RegionLocations();
212     list2 = new RegionLocations();
213 
214     assertTrue(list1 == list1.mergeLocations(list2));
215 
216     // test merge non-empty and empty
217     list2 = hrll(hrl(info0, sn0));
218     list1 = list1.mergeLocations(list2);
219     assertEquals(sn0, list1.getRegionLocation(0).getServerName());
220 
221     // test merge empty and non empty
222     list1 = hrll();
223     list1 = list2.mergeLocations(list1);
224     assertEquals(sn0, list1.getRegionLocation(0).getServerName());
225 
226     // test merge non intersecting
227     list1 = hrll(hrl(info0, sn0), hrl(info1, sn1));
228     list2 = hrll(hrl(info2, sn2));
229     list1 = list2.mergeLocations(list1);
230     assertEquals(sn0, list1.getRegionLocation(0).getServerName());
231     assertEquals(sn1, list1.getRegionLocation(1).getServerName());
232     assertEquals(2, list1.size()); // the size is taken from the argument list to merge
233 
234     // do the other way merge as well
235     list1 = hrll(hrl(info0, sn0), hrl(info1, sn1));
236     list2 = hrll(hrl(info2, sn2));
237     list1 = list1.mergeLocations(list2);
238     assertEquals(sn0, list1.getRegionLocation(0).getServerName());
239     assertEquals(sn1, list1.getRegionLocation(1).getServerName());
240     assertEquals(sn2, list1.getRegionLocation(2).getServerName());
241 
242     // test intersecting lists same seqNum
243     list1 = hrll(hrl(info0, sn0), hrl(info1, sn1));
244     list2 = hrll(hrl(info0, sn2), hrl(info1, sn2), hrl(info9, sn3));
245     list1 = list2.mergeLocations(list1); // list1 should override
246     assertEquals(2, list1.size());
247     assertEquals(sn0, list1.getRegionLocation(0).getServerName());
248     assertEquals(sn1, list1.getRegionLocation(1).getServerName());
249 
250     // do the other way
251     list1 = hrll(hrl(info0, sn0), hrl(info1, sn1));
252     list2 = hrll(hrl(info0, sn2), hrl(info1, sn2), hrl(info9, sn3));
253     list1 = list1.mergeLocations(list2); // list2 should override
254     assertEquals(10, list1.size());
255     assertEquals(sn2, list1.getRegionLocation(0).getServerName());
256     assertEquals(sn2, list1.getRegionLocation(1).getServerName());
257     assertEquals(sn3, list1.getRegionLocation(9).getServerName());
258 
259     // test intersecting lists different seqNum
260     list1 = hrll(hrl(info0, sn0, 10), hrl(info1, sn1, 10));
261     list2 = hrll(hrl(info0, sn2, 11), hrl(info1, sn2, 11), hrl(info9, sn3, 11));
262     list1 = list1.mergeLocations(list2); // list2 should override because of seqNum
263     assertEquals(10, list1.size());
264     assertEquals(sn2, list1.getRegionLocation(0).getServerName());
265     assertEquals(sn2, list1.getRegionLocation(1).getServerName());
266     assertEquals(sn3, list1.getRegionLocation(9).getServerName());
267 
268     // do the other way
269     list1 = hrll(hrl(info0, sn0, 10), hrl(info1, sn1, 10));
270     list2 = hrll(hrl(info0, sn2, 11), hrl(info1, sn2, 11), hrl(info9, sn3, 11));
271     list1 = list1.mergeLocations(list2); // list2 should override
272     assertEquals(10, list1.size());
273     assertEquals(sn2, list1.getRegionLocation(0).getServerName());
274     assertEquals(sn2, list1.getRegionLocation(1).getServerName());
275     assertEquals(sn3, list1.getRegionLocation(9).getServerName());
276   }
277 
278   @Test
279   public void testConstructWithNullElements() {
280     // RegionLocations can contain null elements as well. These null elements can
281 
282     RegionLocations list = new RegionLocations((HRegionLocation)null);
283     assertTrue(list.isEmpty());
284     assertEquals(1, list.size());
285     assertEquals(0, list.numNonNullElements());
286 
287     list = new RegionLocations(null, hrl(info1, sn0));
288     assertFalse(list.isEmpty());
289     assertEquals(2, list.size());
290     assertEquals(1, list.numNonNullElements());
291 
292     list = new RegionLocations(hrl(info0, sn0), null);
293     assertEquals(2, list.size());
294     assertEquals(1, list.numNonNullElements());
295 
296     list = new RegionLocations(null, hrl(info2, sn0), null, hrl(info9, sn0));
297     assertEquals(10, list.size());
298     assertEquals(2, list.numNonNullElements());
299 
300     list = new RegionLocations(null, hrl(info2, sn0), null, hrl(info9, sn0), null);
301     assertEquals(11, list.size());
302     assertEquals(2, list.numNonNullElements());
303 
304     list = new RegionLocations(null, hrl(info2, sn0), null, hrl(info9, sn0), null, null);
305     assertEquals(12, list.size());
306     assertEquals(2, list.numNonNullElements());
307   }
308 }