1818package org .apache .hadoop .hbase .master ;
1919
2020import static org .junit .Assert .assertEquals ;
21+ import static org .junit .Assert .assertNotEquals ;
22+ import static org .junit .Assert .assertNotNull ;
2123import static org .junit .Assert .assertTrue ;
2224
2325import java .io .IOException ;
3537import org .apache .hadoop .hbase .ClusterMetrics .Option ;
3638import org .apache .hadoop .hbase .HBaseClassTestRule ;
3739import org .apache .hadoop .hbase .HBaseTestingUtility ;
38- import org .apache .hadoop .hbase .HColumnDescriptor ;
3940import org .apache .hadoop .hbase .HConstants ;
4041import org .apache .hadoop .hbase .HRegionLocation ;
41- import org .apache .hadoop .hbase .HTableDescriptor ;
4242import org .apache .hadoop .hbase .MetaTableAccessor ;
4343import org .apache .hadoop .hbase .MetaTableAccessor .Visitor ;
4444import org .apache .hadoop .hbase .RegionLocations ;
4545import org .apache .hadoop .hbase .ServerName ;
4646import org .apache .hadoop .hbase .StartMiniClusterOption ;
4747import org .apache .hadoop .hbase .TableName ;
4848import org .apache .hadoop .hbase .client .Admin ;
49+ import org .apache .hadoop .hbase .client .ColumnFamilyDescriptorBuilder ;
4950import org .apache .hadoop .hbase .client .Connection ;
5051import org .apache .hadoop .hbase .client .ConnectionFactory ;
5152import org .apache .hadoop .hbase .client .Delete ;
5253import org .apache .hadoop .hbase .client .RegionInfo ;
5354import org .apache .hadoop .hbase .client .RegionReplicaUtil ;
5455import org .apache .hadoop .hbase .client .Result ;
5556import org .apache .hadoop .hbase .client .Table ;
57+ import org .apache .hadoop .hbase .client .TableDescriptor ;
58+ import org .apache .hadoop .hbase .client .TableDescriptorBuilder ;
5659import org .apache .hadoop .hbase .testclassification .MasterTests ;
5760import org .apache .hadoop .hbase .testclassification .MediumTests ;
5861import org .apache .hadoop .hbase .util .Bytes ;
6871import org .slf4j .Logger ;
6972import org .slf4j .LoggerFactory ;
7073
71- @ Category ({MasterTests .class , MediumTests .class })
74+ import org .apache .hbase .thirdparty .com .google .common .io .Closeables ;
75+
76+ @ Category ({ MasterTests .class , MediumTests .class })
7277public class TestMasterOperationsForRegionReplicas {
7378
7479 @ ClassRule
7580 public static final HBaseClassTestRule CLASS_RULE =
76- HBaseClassTestRule .forClass (TestMasterOperationsForRegionReplicas .class );
81+ HBaseClassTestRule .forClass (TestMasterOperationsForRegionReplicas .class );
7782
7883 private static final Logger LOG = LoggerFactory .getLogger (TestRegionPlacement .class );
7984 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility ();
@@ -90,18 +95,19 @@ public static void setupBeforeClass() throws Exception {
9095 conf = TEST_UTIL .getConfiguration ();
9196 conf .setBoolean ("hbase.tests.use.shortcircuit.reads" , false );
9297 TEST_UTIL .startMiniCluster (numSlaves );
98+ TEST_UTIL .getAdmin ().balancerSwitch (false , true );
9399 CONNECTION = ConnectionFactory .createConnection (TEST_UTIL .getConfiguration ());
94100 ADMIN = CONNECTION .getAdmin ();
95- while (ADMIN .getClusterMetrics (EnumSet .of (Option .LIVE_SERVERS ))
96- . getLiveServerMetrics () .size () < numSlaves ) {
101+ while (ADMIN .getClusterMetrics (EnumSet .of (Option .LIVE_SERVERS )). getLiveServerMetrics ( )
102+ .size () < numSlaves ) {
97103 Thread .sleep (100 );
98104 }
99105 }
100106
101107 @ AfterClass
102108 public static void tearDownAfterClass () throws Exception {
103- if ( ADMIN != null ) ADMIN .close ();
104- if ( CONNECTION != null && ! CONNECTION . isClosed ()) CONNECTION . close ();
109+ Closeables .close (ADMIN , true );
110+ Closeables . close (CONNECTION , true );
105111 TEST_UTIL .shutdownMiniCluster ();
106112 }
107113
@@ -111,15 +117,16 @@ public void testCreateTableWithSingleReplica() throws Exception {
111117 final int numReplica = 1 ;
112118 final TableName tableName = TableName .valueOf (name .getMethodName ());
113119 try {
114- HTableDescriptor desc = new HTableDescriptor ( tableName );
115- desc . setRegionReplication (numReplica );
116- desc . addFamily ( new HColumnDescriptor ("family" ));
120+ TableDescriptor desc =
121+ TableDescriptorBuilder . newBuilder ( tableName ). setRegionReplication (numReplica )
122+ . setColumnFamily ( ColumnFamilyDescriptorBuilder . of ("family" )). build ( );
117123 ADMIN .createTable (desc , Bytes .toBytes ("A" ), Bytes .toBytes ("Z" ), numRegions );
124+ TEST_UTIL .waitUntilAllRegionsAssigned (tableName );
125+ TEST_UTIL .waitUntilNoRegionsInTransition ();
118126
119127 validateNumberOfRowsInMeta (tableName , numRegions , ADMIN .getConnection ());
120- List <RegionInfo > hris = MetaTableAccessor .getTableRegions (
121- ADMIN .getConnection (), tableName );
122- assert (hris .size () == numRegions * numReplica );
128+ List <RegionInfo > hris = MetaTableAccessor .getTableRegions (ADMIN .getConnection (), tableName );
129+ assertEquals (numRegions * numReplica , hris .size ());
123130 } finally {
124131 ADMIN .disableTable (tableName );
125132 ADMIN .deleteTable (tableName );
@@ -132,38 +139,39 @@ public void testCreateTableWithMultipleReplicas() throws Exception {
132139 final int numRegions = 3 ;
133140 final int numReplica = 2 ;
134141 try {
135- HTableDescriptor desc = new HTableDescriptor ( tableName );
136- desc . setRegionReplication (numReplica );
137- desc . addFamily ( new HColumnDescriptor ("family" ));
142+ TableDescriptor desc =
143+ TableDescriptorBuilder . newBuilder ( tableName ). setRegionReplication (numReplica )
144+ . setColumnFamily ( ColumnFamilyDescriptorBuilder . of ("family" )). build ( );
138145 ADMIN .createTable (desc , Bytes .toBytes ("A" ), Bytes .toBytes ("Z" ), numRegions );
139- TEST_UTIL .waitTableEnabled (tableName );
146+ TEST_UTIL .waitUntilAllRegionsAssigned (tableName );
147+ TEST_UTIL .waitUntilNoRegionsInTransition ();
140148 validateNumberOfRowsInMeta (tableName , numRegions , ADMIN .getConnection ());
141149
142150 List <RegionInfo > hris = MetaTableAccessor .getTableRegions (ADMIN .getConnection (), tableName );
143- assert ( hris . size () == numRegions * numReplica );
151+ assertEquals ( numRegions * numReplica , hris . size () );
144152 // check that the master created expected number of RegionState objects
145153 for (int i = 0 ; i < numRegions ; i ++) {
146154 for (int j = 0 ; j < numReplica ; j ++) {
147155 RegionInfo replica = RegionReplicaUtil .getRegionInfoForReplica (hris .get (i ), j );
148156 RegionState state = TEST_UTIL .getHBaseCluster ().getMaster ().getAssignmentManager ()
149- .getRegionStates ().getRegionState (replica );
150- assert (state != null );
157+ .getRegionStates ().getRegionState (replica );
158+ assertNotNull (state );
151159 }
152160 }
153161
154162 List <Result > metaRows = MetaTableAccessor .fullScanRegions (ADMIN .getConnection ());
155163 int numRows = 0 ;
156164 for (Result result : metaRows ) {
157165 RegionLocations locations = MetaTableAccessor .getRegionLocations (result );
158- RegionInfo hri = locations .getRegionLocation ().getRegionInfo ();
166+ RegionInfo hri = locations .getRegionLocation ().getRegion ();
159167 if (!hri .getTable ().equals (tableName )) continue ;
160168 numRows += 1 ;
161169 HRegionLocation [] servers = locations .getRegionLocations ();
162170 // have two locations for the replicas of a region, and the locations should be different
163- assert ( servers .length == 2 );
164- assert (! servers [0 ]. equals ( servers [1 ]) );
171+ assertEquals ( 2 , servers .length );
172+ assertNotEquals ( servers [1 ], servers [0 ] );
165173 }
166- assert ( numRows == numRegions );
174+ assertEquals ( numRegions , numRows );
167175
168176 // The same verification of the meta as above but with the SnapshotOfRegionAssignmentFromMeta
169177 // class
@@ -176,12 +184,14 @@ public void testCreateTableWithMultipleReplicas() throws Exception {
176184 TEST_UTIL .getHBaseClusterInterface ().waitForMasterToStop (master , 30000 );
177185 TEST_UTIL .getHBaseClusterInterface ().startMaster (master .getHostname (), master .getPort ());
178186 TEST_UTIL .getHBaseClusterInterface ().waitForActiveAndReadyMaster ();
187+ TEST_UTIL .waitUntilAllRegionsAssigned (tableName );
188+ TEST_UTIL .waitUntilNoRegionsInTransition ();
179189 for (int i = 0 ; i < numRegions ; i ++) {
180190 for (int j = 0 ; j < numReplica ; j ++) {
181191 RegionInfo replica = RegionReplicaUtil .getRegionInfoForReplica (hris .get (i ), j );
182192 RegionState state = TEST_UTIL .getHBaseCluster ().getMaster ().getAssignmentManager ()
183- .getRegionStates ().getRegionState (replica );
184- assert (state != null );
193+ .getRegionStates ().getRegionState (replica );
194+ assertNotNull (state );
185195 }
186196 }
187197 validateFromSnapshotFromMeta (TEST_UTIL , tableName , numRegions , numReplica ,
@@ -192,86 +202,94 @@ public void testCreateTableWithMultipleReplicas() throws Exception {
192202 // figure current cluster ports and pass them in on next cluster start so new cluster comes
193203 // up at same coordinates -- and the assignment retention logic has a chance to cut in.
194204 List <Integer > rsports = new ArrayList <>();
195- for (JVMClusterUtil .RegionServerThread rst :
196- TEST_UTIL . getHBaseCluster () .getLiveRegionServerThreads ()) {
205+ for (JVMClusterUtil .RegionServerThread rst : TEST_UTIL . getHBaseCluster ()
206+ .getLiveRegionServerThreads ()) {
197207 rsports .add (rst .getRegionServer ().getRpcServer ().getListenerAddress ().getPort ());
198208 }
199209 TEST_UTIL .shutdownMiniHBaseCluster ();
200- StartMiniClusterOption option = StartMiniClusterOption . builder ()
201- .numRegionServers (numSlaves ).rsPorts (rsports ).build ();
210+ StartMiniClusterOption option =
211+ StartMiniClusterOption . builder () .numRegionServers (numSlaves ).rsPorts (rsports ).build ();
202212 TEST_UTIL .startMiniHBaseCluster (option );
203- TEST_UTIL .waitTableAvailable (tableName );
213+ TEST_UTIL .waitUntilAllRegionsAssigned (tableName );
214+ TEST_UTIL .waitUntilNoRegionsInTransition ();
204215 validateFromSnapshotFromMeta (TEST_UTIL , tableName , numRegions , numReplica ,
205216 ADMIN .getConnection ());
206217
207218 // Now shut the whole cluster down, and verify regions are assigned even if there is only
208219 // one server running
209220 TEST_UTIL .shutdownMiniHBaseCluster ();
210221 TEST_UTIL .startMiniHBaseCluster ();
211- TEST_UTIL .waitTableAvailable (tableName );
222+ TEST_UTIL .waitUntilAllRegionsAssigned (tableName );
223+ TEST_UTIL .waitUntilNoRegionsInTransition ();
212224 validateSingleRegionServerAssignment (ADMIN .getConnection (), numRegions , numReplica );
213- for (int i = 1 ; i < numSlaves ; i ++) { //restore the cluster
225+ for (int i = 1 ; i < numSlaves ; i ++) { // restore the cluster
214226 TEST_UTIL .getMiniHBaseCluster ().startRegionServer ();
215227 }
216228
217229 // Check on alter table
218230 ADMIN .disableTable (tableName );
219- assert (ADMIN .isTableDisabled (tableName ));
220- //increase the replica
221- desc . setRegionReplication ( numReplica + 1 );
222- ADMIN . modifyTable (desc );
231+ assertTrue (ADMIN .isTableDisabled (tableName ));
232+ // increase the replica
233+ ADMIN . modifyTable (
234+ TableDescriptorBuilder . newBuilder (desc ). setRegionReplication ( numReplica + 1 ). build () );
223235 ADMIN .enableTable (tableName );
224236 LOG .info (ADMIN .getDescriptor (tableName ).toString ());
225- assert (ADMIN .isTableEnabled (tableName ));
226- List <RegionInfo > regions = TEST_UTIL .getMiniHBaseCluster ().getMaster ().
227- getAssignmentManager ().getRegionStates ().getRegionsOfTable (tableName );
228- assertTrue ("regions.size=" + regions .size () + ", numRegions=" + numRegions +
229- ", numReplica=" + numReplica , regions .size () == numRegions * (numReplica + 1 ));
237+ assertTrue (ADMIN .isTableEnabled (tableName ));
238+ TEST_UTIL .waitUntilAllRegionsAssigned (tableName );
239+ TEST_UTIL .waitUntilNoRegionsInTransition ();
240+ List <RegionInfo > regions = TEST_UTIL .getMiniHBaseCluster ().getMaster ().getAssignmentManager ()
241+ .getRegionStates ().getRegionsOfTable (tableName );
242+ assertTrue ("regions.size=" + regions .size () + ", numRegions=" + numRegions + ", numReplica=" +
243+ numReplica , regions .size () == numRegions * (numReplica + 1 ));
230244
231- //decrease the replica(earlier, table was modified to have a replica count of numReplica + 1)
245+ // decrease the replica(earlier, table was modified to have a replica count of numReplica + 1)
232246 ADMIN .disableTable (tableName );
233- desc . setRegionReplication ( numReplica );
234- ADMIN . modifyTable (desc );
247+ ADMIN . modifyTable (
248+ TableDescriptorBuilder . newBuilder (desc ). setRegionReplication ( numReplica ). build () );
235249 ADMIN .enableTable (tableName );
236- assert (ADMIN .isTableEnabled (tableName ));
237- regions = TEST_UTIL .getMiniHBaseCluster ().getMaster ()
238- .getAssignmentManager ().getRegionStates ().getRegionsOfTable (tableName );
239- assert (regions .size () == numRegions * numReplica );
240- //also make sure the meta table has the replica locations removed
250+ assertTrue (ADMIN .isTableEnabled (tableName ));
251+ TEST_UTIL .waitUntilAllRegionsAssigned (tableName );
252+ TEST_UTIL .waitUntilNoRegionsInTransition ();
253+ regions = TEST_UTIL .getMiniHBaseCluster ().getMaster ().getAssignmentManager ().getRegionStates ()
254+ .getRegionsOfTable (tableName );
255+ assertEquals (numRegions * numReplica , regions .size ());
256+ // also make sure the meta table has the replica locations removed
241257 hris = MetaTableAccessor .getTableRegions (ADMIN .getConnection (), tableName );
242- assert ( hris . size () == numRegions * numReplica );
243- //just check that the number of default replica regions in the meta table are the same
244- //as the number of regions the table was created with, and the count of the
245- //replicas is numReplica for each region
258+ assertEquals ( numRegions * numReplica , hris . size () );
259+ // just check that the number of default replica regions in the meta table are the same
260+ // as the number of regions the table was created with, and the count of the
261+ // replicas is numReplica for each region
246262 Map <RegionInfo , Integer > defaultReplicas = new HashMap <>();
247263 for (RegionInfo hri : hris ) {
248- Integer i ;
249264 RegionInfo regionReplica0 = RegionReplicaUtil .getRegionInfoForDefaultReplica (hri );
250- defaultReplicas .put (regionReplica0 ,
251- ( i = defaultReplicas .get (regionReplica0 )) == null ? 1 : i + 1 );
265+ Integer i = defaultReplicas .get (regionReplica0 );
266+ defaultReplicas .put (regionReplica0 , i == null ? 1 : i + 1 );
252267 }
253- assert ( defaultReplicas .size () == numRegions );
268+ assertEquals ( numRegions , defaultReplicas .size ());
254269 Collection <Integer > counts = new HashSet <>(defaultReplicas .values ());
255- assert (counts .size () == 1 && counts .contains (numReplica ));
270+ assertEquals (1 , counts .size ());
271+ assertTrue (counts .contains (numReplica ));
256272 } finally {
257273 ADMIN .disableTable (tableName );
258274 ADMIN .deleteTable (tableName );
259275 }
260276 }
261277
262- @ Test @ Ignore ("Enable when we have support for alter_table- HBASE-10361" )
278+ @ Test
279+ @ Ignore ("Enable when we have support for alter_table- HBASE-10361" )
263280 public void testIncompleteMetaTableReplicaInformation () throws Exception {
264281 final TableName tableName = TableName .valueOf (name .getMethodName ());
265282 final int numRegions = 3 ;
266283 final int numReplica = 2 ;
267284 try {
268285 // Create a table and let the meta table be updated with the location of the
269286 // region locations.
270- HTableDescriptor desc = new HTableDescriptor ( tableName );
271- desc . setRegionReplication (numReplica );
272- desc . addFamily ( new HColumnDescriptor ("family" ));
287+ TableDescriptor desc =
288+ TableDescriptorBuilder . newBuilder ( tableName ). setRegionReplication (numReplica )
289+ . setColumnFamily ( ColumnFamilyDescriptorBuilder . of ("family" )). build ( );
273290 ADMIN .createTable (desc , Bytes .toBytes ("A" ), Bytes .toBytes ("Z" ), numRegions );
274- TEST_UTIL .waitTableEnabled (tableName );
291+ TEST_UTIL .waitUntilAllRegionsAssigned (tableName );
292+ TEST_UTIL .waitUntilNoRegionsInTransition ();
275293 Set <byte []> tableRows = new HashSet <>();
276294 List <RegionInfo > hris = MetaTableAccessor .getTableRegions (ADMIN .getConnection (), tableName );
277295 for (RegionInfo hri : hris ) {
@@ -295,27 +313,21 @@ public void testIncompleteMetaTableReplicaInformation() throws Exception {
295313 // even if the meta table is partly updated, when we re-enable the table, we should
296314 // get back the desired number of replicas for the regions
297315 ADMIN .enableTable (tableName );
298- assert (ADMIN .isTableEnabled (tableName ));
299- List <RegionInfo > regions = TEST_UTIL .getMiniHBaseCluster ().getMaster ()
300- .getAssignmentManager ().getRegionStates ().getRegionsOfTable (tableName );
301- assert (regions .size () == numRegions * numReplica );
316+ assertTrue (ADMIN .isTableEnabled (tableName ));
317+ TEST_UTIL .waitUntilAllRegionsAssigned (tableName );
318+ TEST_UTIL .waitUntilNoRegionsInTransition ();
319+ List <RegionInfo > regions = TEST_UTIL .getMiniHBaseCluster ().getMaster ().getAssignmentManager ()
320+ .getRegionStates ().getRegionsOfTable (tableName );
321+ assertEquals (numRegions * numReplica , regions .size ());
302322 } finally {
303323 ADMIN .disableTable (tableName );
304324 ADMIN .deleteTable (tableName );
305325 }
306326 }
307327
308- private String printRegions (List <RegionInfo > regions ) {
309- StringBuilder strBuf = new StringBuilder ();
310- for (RegionInfo r : regions ) {
311- strBuf .append (" ____ " + r .toString ());
312- }
313- return strBuf .toString ();
314- }
315-
316328 private void validateNumberOfRowsInMeta (final TableName table , int numRegions ,
317329 Connection connection ) throws IOException {
318- assert (ADMIN .tableExists (table ));
330+ assert (ADMIN .tableExists (table ));
319331 final AtomicInteger count = new AtomicInteger ();
320332 Visitor visitor = new Visitor () {
321333 @ Override
@@ -325,16 +337,16 @@ public boolean visit(Result r) throws IOException {
325337 }
326338 };
327339 MetaTableAccessor .fullScanRegions (connection , visitor );
328- assert ( count .get () == numRegions );
340+ assertEquals ( numRegions , count .get ());
329341 }
330342
331343 private void validateFromSnapshotFromMeta (HBaseTestingUtility util , TableName table ,
332344 int numRegions , int numReplica , Connection connection ) throws IOException {
333- SnapshotOfRegionAssignmentFromMeta snapshot = new SnapshotOfRegionAssignmentFromMeta (
334- connection );
345+ SnapshotOfRegionAssignmentFromMeta snapshot =
346+ new SnapshotOfRegionAssignmentFromMeta ( connection );
335347 snapshot .initialize ();
336348 Map <RegionInfo , ServerName > regionToServerMap = snapshot .getRegionToRegionServerMap ();
337- assert (regionToServerMap .size () == numRegions * numReplica );
349+ assert (regionToServerMap .size () == numRegions * numReplica );
338350 Map <ServerName , List <RegionInfo >> serverToRegionMap = snapshot .getRegionServerToRegionMap ();
339351 for (Map .Entry <ServerName , List <RegionInfo >> entry : serverToRegionMap .entrySet ()) {
340352 if (entry .getKey ().equals (util .getHBaseCluster ().getMaster ().getServerName ())) {
@@ -345,7 +357,7 @@ private void validateFromSnapshotFromMeta(HBaseTestingUtility util, TableName ta
345357 for (RegionInfo region : regions ) {
346358 byte [] startKey = region .getStartKey ();
347359 if (region .getTable ().equals (table )) {
348- setOfStartKeys .add (startKey ); //ignore other tables
360+ setOfStartKeys .add (startKey ); // ignore other tables
349361 LOG .info ("--STARTKEY {}--" , new String (startKey , StandardCharsets .UTF_8 ));
350362 }
351363 }
@@ -357,10 +369,10 @@ private void validateFromSnapshotFromMeta(HBaseTestingUtility util, TableName ta
357369
358370 private void validateSingleRegionServerAssignment (Connection connection , int numRegions ,
359371 int numReplica ) throws IOException {
360- SnapshotOfRegionAssignmentFromMeta snapshot = new SnapshotOfRegionAssignmentFromMeta (
361- connection );
372+ SnapshotOfRegionAssignmentFromMeta snapshot =
373+ new SnapshotOfRegionAssignmentFromMeta ( connection );
362374 snapshot .initialize ();
363- Map <RegionInfo , ServerName > regionToServerMap = snapshot .getRegionToRegionServerMap ();
375+ Map <RegionInfo , ServerName > regionToServerMap = snapshot .getRegionToRegionServerMap ();
364376 assertEquals (regionToServerMap .size (), numRegions * numReplica );
365377 Map <ServerName , List <RegionInfo >> serverToRegionMap = snapshot .getRegionServerToRegionMap ();
366378 assertEquals ("One Region Only" , 1 , serverToRegionMap .keySet ().size ());
0 commit comments