1717 */
1818package org .apache .hadoop .hbase .backup ;
1919
20+ import static org .junit .Assert .assertTrue ;
21+
2022import java .io .IOException ;
2123import java .util .ArrayList ;
2224import java .util .Arrays ;
2325import java .util .HashMap ;
26+ import java .util .HashSet ;
2427import java .util .Iterator ;
2528import java .util .List ;
2629import java .util .Map ;
2730import java .util .Map .Entry ;
2831import java .util .Objects ;
32+ import java .util .Set ;
2933import org .apache .hadoop .conf .Configuration ;
3034import org .apache .hadoop .fs .FileStatus ;
3135import org .apache .hadoop .fs .FileSystem ;
5862import org .apache .hadoop .hbase .client .TableDescriptorBuilder ;
5963import org .apache .hadoop .hbase .master .cleaner .LogCleaner ;
6064import org .apache .hadoop .hbase .master .cleaner .TimeToLiveLogCleaner ;
65+ import org .apache .hadoop .hbase .net .Address ;
6166import org .apache .hadoop .hbase .regionserver .LogRoller ;
67+ import org .apache .hadoop .hbase .rsgroup .RSGroupInfo ;
68+ import org .apache .hadoop .hbase .rsgroup .RSGroupUtil ;
6269import org .apache .hadoop .hbase .security .HadoopSecurityEnabledUserProviderForTesting ;
6370import org .apache .hadoop .hbase .security .UserProvider ;
6471import org .apache .hadoop .hbase .security .access .SecureTestUtil ;
@@ -87,6 +94,15 @@ public class TestBackupBase {
8794 protected static Configuration conf1 ;
8895 protected static Configuration conf2 ;
8996
97+ protected static final int RSGROUP_RS_NUM = 5 ;
98+ protected static final int NUM_REGIONSERVERS = 3 ;
99+ protected static final String RSGROUP_NAME = "rsgroup1" ;
100+ protected static final String RSGROUP_NAMESPACE = "rsgroup_ns" ;
101+ protected static final TableName RSGROUP_TABLE_1 =
102+ TableName .valueOf (RSGROUP_NAMESPACE + ":rsgroup_table1" );
103+ protected static final TableName RSGROUP_TABLE_2 =
104+ TableName .valueOf (RSGROUP_NAMESPACE + ":rsgroup_table2" );
105+
90106 protected static TableName table1 = TableName .valueOf ("table1" );
91107 protected static TableDescriptor table1Desc ;
92108 protected static TableName table2 = TableName .valueOf ("table2" );
@@ -108,6 +124,7 @@ public class TestBackupBase {
108124
109125 protected static boolean autoRestoreOnFailure ;
110126 protected static boolean useSecondCluster ;
127+ protected static boolean enableRSgroup ;
111128
112129 static class IncrementalTableBackupClientForTest extends IncrementalTableBackupClient {
113130 public IncrementalTableBackupClientForTest () {
@@ -292,6 +309,22 @@ public void execute() throws IOException {
292309 }
293310 }
294311
312+ private static RSGroupInfo addGroup (String groupName , int serverCount ) throws IOException {
313+ Admin admin = TEST_UTIL .getAdmin ();
314+ RSGroupInfo defaultInfo = admin .getRSGroup (RSGroupInfo .DEFAULT_GROUP );
315+ admin .addRSGroup (groupName );
316+ Set <Address > set = new HashSet <>();
317+ for (Address server : defaultInfo .getServers ()) {
318+ if (set .size () == serverCount ) {
319+ break ;
320+ }
321+ set .add (server );
322+ }
323+ admin .moveServersToRSGroup (set , groupName );
324+ RSGroupInfo result = admin .getRSGroup (groupName );
325+ return result ;
326+ }
327+
295328 public static void setUpHelper () throws Exception {
296329 BACKUP_ROOT_DIR = Path .SEPARATOR + "backupUT" ;
297330 BACKUP_REMOTE_ROOT_DIR = Path .SEPARATOR + "backupUT" ;
@@ -314,7 +347,13 @@ public static void setUpHelper() throws Exception {
314347
315348 // Set MultiWAL (with 2 default WAL files per RS)
316349 conf1 .set (WALFactory .WAL_PROVIDER , provider );
317- TEST_UTIL .startMiniCluster ();
350+ if (enableRSgroup ) {
351+ conf1 .setBoolean (RSGroupUtil .RS_GROUP_ENABLED , true );
352+ TEST_UTIL .startMiniCluster (RSGROUP_RS_NUM + NUM_REGIONSERVERS );
353+ addGroup (RSGROUP_NAME , RSGROUP_RS_NUM );
354+ } else {
355+ TEST_UTIL .startMiniCluster ();
356+ }
318357
319358 if (useSecondCluster ) {
320359 conf2 = HBaseConfiguration .create (conf1 );
@@ -352,6 +391,7 @@ public static void setUpHelper() throws Exception {
352391 public static void setUp () throws Exception {
353392 TEST_UTIL = new HBaseTestingUtil ();
354393 conf1 = TEST_UTIL .getConfiguration ();
394+ enableRSgroup = false ;
355395 autoRestoreOnFailure = true ;
356396 useSecondCluster = false ;
357397 setUpHelper ();
@@ -377,6 +417,7 @@ public static void tearDown() throws Exception {
377417 }
378418 TEST_UTIL .shutdownMiniCluster ();
379419 TEST_UTIL .shutdownMiniMapReduceCluster ();
420+ enableRSgroup = false ;
380421 autoRestoreOnFailure = true ;
381422 useSecondCluster = false ;
382423 }
@@ -406,16 +447,16 @@ protected BackupRequest createBackupRequest(BackupType type, List<TableName> tab
406447 return request ;
407448 }
408449
409- protected String backupTables (BackupType type , List <TableName > tables , String path )
450+ protected BackupInfo backupTables (BackupType type , List <TableName > tables , String path )
410451 throws IOException {
411452 Connection conn = null ;
412453 BackupAdmin badmin = null ;
413- String backupId ;
454+ BackupInfo backupInfo ;
414455 try {
415456 conn = ConnectionFactory .createConnection (conf1 );
416457 badmin = new BackupAdminImpl (conn );
417458 BackupRequest request = createBackupRequest (type , new ArrayList <>(tables ), path );
418- backupId = badmin .backupTables (request );
459+ backupInfo = badmin .backupTables (request );
419460 } finally {
420461 if (badmin != null ) {
421462 badmin .close ();
@@ -424,14 +465,14 @@ protected String backupTables(BackupType type, List<TableName> tables, String pa
424465 conn .close ();
425466 }
426467 }
427- return backupId ;
468+ return backupInfo ;
428469 }
429470
430- protected String fullTableBackup (List <TableName > tables ) throws IOException {
471+ protected BackupInfo fullTableBackup (List <TableName > tables ) throws IOException {
431472 return backupTables (BackupType .FULL , tables , BACKUP_ROOT_DIR );
432473 }
433474
434- protected String incrementalTableBackup (List <TableName > tables ) throws IOException {
475+ protected BackupInfo incrementalTableBackup (List <TableName > tables ) throws IOException {
435476 return backupTables (BackupType .INCREMENTAL , tables , BACKUP_ROOT_DIR );
436477 }
437478
@@ -479,6 +520,23 @@ protected static void createTables() throws Exception {
479520 table .close ();
480521 ha .close ();
481522 conn .close ();
523+
524+ if (enableRSgroup ) {
525+ ha .createNamespace (NamespaceDescriptor .create (RSGROUP_NAMESPACE )
526+ .addConfiguration (RSGroupInfo .NAMESPACE_DESC_PROP_GROUP , RSGROUP_NAME ).build ());
527+
528+ ha .createTable (TableDescriptorBuilder .newBuilder (RSGROUP_TABLE_1 )
529+ .setColumnFamily (ColumnFamilyDescriptorBuilder .of (famName )).build ());
530+ table = ConnectionFactory .createConnection (conf1 ).getTable (RSGROUP_TABLE_1 );
531+ loadTable (table );
532+ table .close ();
533+
534+ ha .createTable (TableDescriptorBuilder .newBuilder (RSGROUP_TABLE_2 )
535+ .setColumnFamily (ColumnFamilyDescriptorBuilder .of (famName )).build ());
536+ table = ConnectionFactory .createConnection (conf1 ).getTable (RSGROUP_TABLE_2 );
537+ loadTable (table );
538+ table .close ();
539+ }
482540 }
483541
484542 protected boolean checkSucceeded (String backupId ) throws IOException {
@@ -501,7 +559,7 @@ protected boolean checkFailed(String backupId) throws IOException {
501559 return status .getState () == BackupState .FAILED ;
502560 }
503561
504- private BackupInfo getBackupInfo (String backupId ) throws IOException {
562+ protected BackupInfo getBackupInfo (String backupId ) throws IOException {
505563 try (BackupSystemTable table = new BackupSystemTable (TEST_UTIL .getConnection ())) {
506564 BackupInfo status = table .readBackupInfo (backupId );
507565 return status ;
@@ -538,6 +596,26 @@ protected List<FileStatus> getListOfWALFiles(Configuration c) throws IOException
538596 return logFiles ;
539597 }
540598
599+ protected Set <Address > getRsgroupServers (String rsgroupName ) throws IOException {
600+ RSGroupInfo rsGroupInfo = TEST_UTIL .getAdmin ().getRSGroup (rsgroupName );
601+ if (
602+ rsGroupInfo != null && rsGroupInfo .getServers () != null && !rsGroupInfo .getServers ().isEmpty ()
603+ ) {
604+ return new HashSet <>(rsGroupInfo .getServers ());
605+ }
606+ return new HashSet <>();
607+ }
608+
609+ protected void checkIfWALFilesBelongToRsgroup (List <String > walFiles , String rsgroupName )
610+ throws IOException {
611+ for (String file : walFiles ) {
612+ Address walServerAddress =
613+ Address .fromString (BackupUtils .parseHostNameFromLogFile (new Path (file )));
614+ assertTrue ("Backed WAL files should be from RSGroup " + rsgroupName ,
615+ getRsgroupServers (rsgroupName ).contains (walServerAddress ));
616+ }
617+ }
618+
541619 protected void dumpBackupDir () throws IOException {
542620 // Dump Backup Dir
543621 FileSystem fs = FileSystem .get (conf1 );
0 commit comments