1717 */ 
1818package  org .apache .hadoop .hbase .util ;
1919
20+ import  java .io .IOException ;
2021import  java .util .ArrayList ;
2122import  java .util .List ;
2223import  java .util .concurrent .TimeUnit ;
@@ -65,12 +66,12 @@ public class TestRegionMover2 {
6566  @ ClassRule 
6667  public  static  final  HBaseClassTestRule  CLASS_RULE  =
6768    HBaseClassTestRule .forClass (TestRegionMover2 .class );
69+   private  static  final  String  CF  = "fam1" ;
6870
6971  @ Rule 
7072  public  TestName  name  = new  TestName ();
7173
7274  private  static  final  Logger  LOG  = LoggerFactory .getLogger (TestRegionMover2 .class );
73- 
7475  private  static  final  HBaseTestingUtil  TEST_UTIL  = new  HBaseTestingUtil ();
7576
7677  @ BeforeClass 
@@ -86,12 +87,7 @@ public static void tearDownAfterClass() throws Exception {
8687
8788  @ Before 
8889  public  void  setUp () throws  Exception  {
89-     final  TableName  tableName  = TableName .valueOf (name .getMethodName ());
90-     TableDescriptor  tableDesc  = TableDescriptorBuilder .newBuilder (tableName )
91-       .setColumnFamily (ColumnFamilyDescriptorBuilder .of ("fam1" )).build ();
92-     int  startKey  = 0 ;
93-     int  endKey  = 80000 ;
94-     TEST_UTIL .getAdmin ().createTable (tableDesc , Bytes .toBytes (startKey ), Bytes .toBytes (endKey ), 9 );
90+     createTable (name .getMethodName ());
9591  }
9692
9793  @ After 
@@ -101,17 +97,23 @@ public void tearDown() throws Exception {
10197    TEST_UTIL .getAdmin ().deleteTable (tableName );
10298  }
10399
100+   private  TableName  createTable (String  name ) throws  IOException  {
101+     final  TableName  tableName  = TableName .valueOf (name );
102+     TableDescriptor  tableDesc  = TableDescriptorBuilder .newBuilder (tableName )
103+       .setColumnFamily (ColumnFamilyDescriptorBuilder .of (CF )).build ();
104+     int  startKey  = 0 ;
105+     int  endKey  = 80000 ;
106+     TEST_UTIL .getAdmin ().createTable (tableDesc , Bytes .toBytes (startKey ), Bytes .toBytes (endKey ), 9 );
107+     return  tableName ;
108+   }
109+ 
104110  @ Test 
105111  public  void  testWithMergedRegions () throws  Exception  {
106112    final  TableName  tableName  = TableName .valueOf (name .getMethodName ());
107113    SingleProcessHBaseCluster  cluster  = TEST_UTIL .getHBaseCluster ();
108114    Admin  admin  = TEST_UTIL .getAdmin ();
109115    Table  table  = TEST_UTIL .getConnection ().getTable (tableName );
110-     List <Put > puts  = new  ArrayList <>();
111-     for  (int  i  = 0 ; i  < 10000 ; i ++) {
112-       puts .add (new  Put (Bytes .toBytes ("rowkey_"  + i )).addColumn (Bytes .toBytes ("fam1" ),
113-         Bytes .toBytes ("q1" ), Bytes .toBytes ("val_"  + i )));
114-     }
116+     List <Put > puts  = createPuts (10000 );
115117    table .put (puts );
116118    admin .flush (tableName );
117119    HRegionServer  regionServer  = cluster .getRegionServer (0 );
@@ -141,11 +143,7 @@ public void testWithSplitRegions() throws Exception {
141143    SingleProcessHBaseCluster  cluster  = TEST_UTIL .getHBaseCluster ();
142144    Admin  admin  = TEST_UTIL .getAdmin ();
143145    Table  table  = TEST_UTIL .getConnection ().getTable (tableName );
144-     List <Put > puts  = new  ArrayList <>();
145-     for  (int  i  = 10 ; i  < 50000 ; i ++) {
146-       puts .add (new  Put (Bytes .toBytes (i )).addColumn (Bytes .toBytes ("fam1" ), Bytes .toBytes ("q1" ),
147-         Bytes .toBytes ("val_"  + i )));
148-     }
146+     List <Put > puts  = createPuts (50000 );
149147    table .put (puts );
150148    admin .flush (tableName );
151149    admin .compact (tableName );
@@ -190,16 +188,11 @@ public void testFailedRegionMove() throws Exception {
190188    SingleProcessHBaseCluster  cluster  = TEST_UTIL .getHBaseCluster ();
191189    Admin  admin  = TEST_UTIL .getAdmin ();
192190    Table  table  = TEST_UTIL .getConnection ().getTable (tableName );
193-     List <Put > puts  = new  ArrayList <>();
194-     for  (int  i  = 0 ; i  < 1000 ; i ++) {
195-       puts .add (new  Put (Bytes .toBytes ("rowkey_"  + i )).addColumn (Bytes .toBytes ("fam1" ),
196-         Bytes .toBytes ("q1" ), Bytes .toBytes ("val_"  + i )));
197-     }
191+     List <Put > puts  = createPuts (1000 );
198192    table .put (puts );
199193    admin .flush (tableName );
200194    HRegionServer  regionServer  = cluster .getRegionServer (0 );
201195    String  rsName  = regionServer .getServerName ().getAddress ().toString ();
202-     int  numRegions  = regionServer .getNumberOfOnlineRegions ();
203196    List <HRegion > hRegions  = regionServer .getRegions ().stream ()
204197      .filter (hRegion  -> hRegion .getRegionInfo ().getTable ().equals (tableName ))
205198      .collect (Collectors .toList ());
@@ -217,14 +210,30 @@ public void testFailedRegionMove() throws Exception {
217210    }
218211  }
219212
213+   @ Test 
214+   public  void  testDeletedTable () throws  Exception  {
215+     TableName  tableNameToDelete  = createTable (name .getMethodName () + "ToDelete" );
216+     SingleProcessHBaseCluster  cluster  = TEST_UTIL .getHBaseCluster ();
217+     HRegionServer  regionServer  = cluster .getRegionServer (0 );
218+     String  rsName  = regionServer .getServerName ().getAddress ().toString ();
219+     RegionMover .RegionMoverBuilder  rmBuilder  =
220+       new  RegionMover .RegionMoverBuilder (rsName , TEST_UTIL .getConfiguration ()).ack (true )
221+         .maxthreads (8 );
222+     try  (Admin  admin  = TEST_UTIL .getAdmin (); RegionMover  rm  = rmBuilder .build ()) {
223+       LOG .debug ("Unloading {}" , regionServer .getServerName ());
224+       rm .unload ();
225+       Assert .assertEquals (0 , regionServer .getNumberOfOnlineRegions ());
226+       LOG .debug ("Successfully Unloaded, now delete table" );
227+       admin .disableTable (tableNameToDelete );
228+       admin .deleteTable (tableNameToDelete );
229+       Assert .assertTrue (rm .load ());
230+     }
231+   }
232+ 
220233  public  void  loadDummyDataInTable (TableName  tableName ) throws  Exception  {
221234    Admin  admin  = TEST_UTIL .getAdmin ();
222235    Table  table  = TEST_UTIL .getConnection ().getTable (tableName );
223-     List <Put > puts  = new  ArrayList <>();
224-     for  (int  i  = 0 ; i  < 1000 ; i ++) {
225-       puts .add (new  Put (Bytes .toBytes ("rowkey_"  + i )).addColumn (Bytes .toBytes ("fam1" ),
226-         Bytes .toBytes ("q1" ), Bytes .toBytes ("val_"  + i )));
227-     }
236+     List <Put > puts  = createPuts (1000 );
228237    table .put (puts );
229238    admin .flush (tableName );
230239  }
@@ -297,6 +306,15 @@ public void testIsolateMetaAndRandomRegionOnTheRandomServer() throws Exception {
297306    regionIsolationOperation (randomSeverRegion , randomSeverRegion , 2 , true );
298307  }
299308
309+   private  List <Put > createPuts (int  count ) {
310+     List <Put > puts  = new  ArrayList <>();
311+     for  (int  i  = 0 ; i  < count ; i ++) {
312+       puts .add (new  Put (Bytes .toBytes ("rowkey_"  + i )).addColumn (Bytes .toBytes (CF ),
313+         Bytes .toBytes ("q1" ), Bytes .toBytes ("val_"  + i )));
314+     }
315+     return  puts ;
316+   }
317+ 
300318  public  ServerName  findMetaRSLocation () throws  Exception  {
301319    ZKWatcher  zkWatcher  = new  ZKWatcher (TEST_UTIL .getConfiguration (), null , null );
302320    List <HRegionLocation > result  = new  ArrayList <>();
@@ -325,7 +343,7 @@ public ServerName findSourceServerName(TableName tableName) throws Exception {
325343    }
326344    if  (sourceServer  == null ) {
327345      throw  new  Exception (
328-         "This shouln 't happen, No RS found with more than 2 regions of table : "  + tableName );
346+         "This shouldn 't happen, No RS found with more than 2 regions of table : "  + tableName );
329347    }
330348    return  sourceServer ;
331349  }
@@ -372,7 +390,7 @@ public void regionIsolationOperation(ServerName sourceServerName,
372390      new  RegionMover .RegionMoverBuilder (destinationRSName , TEST_UTIL .getConfiguration ()).ack (true )
373391        .maxthreads (8 ).isolateRegionIdArray (listOfRegionIDsToIsolate );
374392    try  (RegionMover  rm  = rmBuilder .build ()) {
375-       LOG .debug ("Unloading {} except regions  : {}" , destinationRS .getServerName (),
393+       LOG .debug ("Unloading {} except regions: {}" , destinationRS .getServerName (),
376394        listOfRegionIDsToIsolate );
377395      rm .isolateRegions ();
378396      Assert .assertEquals (numRegionsToIsolate , destinationRS .getNumberOfOnlineRegions ());
@@ -381,8 +399,8 @@ public void regionIsolationOperation(ServerName sourceServerName,
381399        Assert .assertTrue (
382400          listOfRegionIDsToIsolate .contains (onlineRegions .get (i ).getRegionInfo ().getEncodedName ()));
383401      }
384-       LOG .debug ("Successfully Isolated "   +  listOfRegionIDsToIsolate .size () +  " regions : " 
385-         +  listOfRegionIDsToIsolate  +  " on "  +  destinationRS .getServerName ());
402+       LOG .debug ("Successfully Isolated {} regions: {} on {}"  ,  listOfRegionIDsToIsolate .size (), 
403+         listOfRegionIDsToIsolate ,  destinationRS .getServerName ());
386404    } finally  {
387405      admin .recommissionRegionServer (destinationRS .getServerName (), null );
388406    }
0 commit comments