diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/backup/BackupInfo.java hbase-client/src/main/java/org/apache/hadoop/hbase/backup/BackupInfo.java index df8860a..d4936ec 100644 --- hbase-client/src/main/java/org/apache/hadoop/hbase/backup/BackupInfo.java +++ hbase-client/src/main/java/org/apache/hadoop/hbase/backup/BackupInfo.java @@ -51,6 +51,15 @@ import org.apache.hadoop.hbase.util.Bytes; public class BackupInfo implements Comparable { private static final Log LOG = LogFactory.getLog(BackupInfo.class); + public static interface Filter { + + /** + * Filter + * @param info: backup info + * @return true if info passes filter, false otherwise + */ + public boolean apply(BackupInfo image); + } // backup status flag public static enum BackupState { WAITING, RUNNING, COMPLETE, FAILED, ANY; diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/backup/impl/BackupCommands.java hbase-client/src/main/java/org/apache/hadoop/hbase/backup/impl/BackupCommands.java index 6d93a7c..c217a5b 100644 --- hbase-client/src/main/java/org/apache/hadoop/hbase/backup/impl/BackupCommands.java +++ hbase-client/src/main/java/org/apache/hadoop/hbase/backup/impl/BackupCommands.java @@ -461,25 +461,44 @@ public final class BackupCommands { public void execute() throws IOException { super.execute(); - + int n = parseHistoryLength(); - TableName tableName = getTableName(); + final TableName tableName = getTableName(); + final String setName = getTableSetName(); + BackupInfo.Filter tableNameFilter = new BackupInfo.Filter() { + @Override + public boolean apply(BackupInfo info) { + if (tableName == null) return true; + List names = info.getTableNames(); + return names.contains(tableName); + } + }; + BackupInfo.Filter tableSetFilter = new BackupInfo.Filter() { + @Override + public boolean apply(BackupInfo info) { + if (setName == null) return true; + String backupId = info.getBackupId(); + return backupId.startsWith(setName); + } + }; Path backupRootPath = getBackupRootPath(); List history = null; - Configuration conf = getConf() != null? getConf(): HBaseConfiguration.create(); - if(backupRootPath == null) { + Configuration conf = getConf() != null ? getConf() : HBaseConfiguration.create(); + if (backupRootPath == null) { // Load from hbase:backup - try(final Connection conn = ConnectionFactory.createConnection(conf); - final BackupAdmin admin = conn.getAdmin().getBackupAdmin();){ - history = admin.getHistory(n, tableName); - } + try (final Connection conn = ConnectionFactory.createConnection(conf); + final BackupAdmin admin = conn.getAdmin().getBackupAdmin();) { + + history = admin.getHistory(n, tableNameFilter, tableSetFilter); + } } else { // load from backup FS - history = BackupClientUtil.getHistory(conf, n, tableName, backupRootPath); - } - for(BackupInfo info: history){ + history = BackupClientUtil.getHistory(conf, n, backupRootPath, + tableNameFilter, tableSetFilter); + } + for (BackupInfo info : history) { System.out.println(info.getShortDescription()); - } + } } private Path getBackupRootPath() throws IOException { @@ -507,6 +526,12 @@ public final class BackupCommands { } } + private String getTableSetName() throws IOException { + String value = cmdline.getOptionValue("set"); + if (value == null) return null; + return value; + } + private int parseHistoryLength() throws IOException { String value = cmdline.getOptionValue("n"); try{ diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/backup/util/BackupClientUtil.java hbase-client/src/main/java/org/apache/hadoop/hbase/backup/util/BackupClientUtil.java index 5255434..1ab7071 100644 --- hbase-client/src/main/java/org/apache/hadoop/hbase/backup/util/BackupClientUtil.java +++ hbase-client/src/main/java/org/apache/hadoop/hbase/backup/util/BackupClientUtil.java @@ -359,7 +359,7 @@ public final class BackupClientUtil { + HConstants.HREGION_LOGDIR_NAME; } - public static List getHistory(Configuration conf, Path backupRootPath) + private static List getHistory(Configuration conf, Path backupRootPath) throws IOException { // Get all (n) history from backup root destination FileSystem fs = FileSystem.get(conf); @@ -392,28 +392,27 @@ public final class BackupClientUtil { return infos; } - public static List getHistory(Configuration conf, int n, TableName name, - Path backupRootPath) throws IOException { + public static List getHistory(Configuration conf, int n, Path backupRootPath, + BackupInfo.Filter... filters) throws IOException { List infos = getHistory(conf, backupRootPath); - if (name == null) { - if (infos.size() <= n) return infos; - return infos.subList(0, n); - } else { - List ret = new ArrayList(); - int count = 0; - for (BackupInfo info : infos) { - List names = info.getTableNames(); - if (names.contains(name)) { - ret.add(info); - if (++count == n) { - break; - } + List ret = new ArrayList(); + for (BackupInfo info : infos) { + if (ret.size() == n) { + break; + } + boolean passed = true; + for(int i=0; i < filters.length; i++) { + if(!filters[i].apply(info)) { + passed = false; break; } } - return ret; + if (passed) { + ret.add(info); + } } + return ret; } - + private static boolean isBackupDirectory(LocatedFileStatus lfs) { return lfs.getPath().getName().startsWith(BackupRestoreConstants.BACKUPID_PREFIX); } diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/client/BackupAdmin.java hbase-client/src/main/java/org/apache/hadoop/hbase/client/BackupAdmin.java index b19dea1..2e5ca2a 100644 --- hbase-client/src/main/java/org/apache/hadoop/hbase/client/BackupAdmin.java +++ hbase-client/src/main/java/org/apache/hadoop/hbase/client/BackupAdmin.java @@ -122,13 +122,13 @@ public interface BackupAdmin extends Closeable{ /** - * Show backup history command for a table + * Show backup history command with filters * @param n - last n backup sessions - * @param name - table's name + * @param f - list of filters * @return list of backup infos * @throws IOException exception */ - public List getHistory(int n, TableName name) throws IOException; + public List getHistory(int n, BackupInfo.Filter ... f) throws IOException; /** diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseBackupAdmin.java hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseBackupAdmin.java index 8e08fd0..ecd2c80 100644 --- hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseBackupAdmin.java +++ hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseBackupAdmin.java @@ -333,12 +333,24 @@ public class HBaseBackupAdmin implements BackupAdmin { } @Override - public List getHistory(int n, TableName name) throws IOException { - if (name == null) return getHistory(n); + public List getHistory(int n, BackupInfo.Filter ... filters) throws IOException { + if (filters.length == 0) return getHistory(n); try (final BackupSystemTable table = new BackupSystemTable(conn)) { - List history = table.getBackupHistoryForTable(name); - n = Math.min(n, history.size()); - return history.subList(0, n); + List history = table.getBackupHistory(); + List result = new ArrayList(); + for(BackupInfo bi: history) { + if(result.size() == n) break; + boolean passed = true; + for(int i=0; i < filters.length; i++) { + if(!filters[i].apply(bi)) { + passed = false; break; + } + } + if(passed) { + result.add(bi); + } + } + return result; } } diff --git hbase-server/src/test/java/org/apache/hadoop/hbase/backup/TestBackupShowHistory.java hbase-server/src/test/java/org/apache/hadoop/hbase/backup/TestBackupShowHistory.java index 4a94cb9..4594338 100644 --- hbase-server/src/test/java/org/apache/hadoop/hbase/backup/TestBackupShowHistory.java +++ hbase-server/src/test/java/org/apache/hadoop/hbase/backup/TestBackupShowHistory.java @@ -70,7 +70,13 @@ public class TestBackupShowHistory extends TestBackupBase { List history = getBackupAdmin().getHistory(10); assertTrue(findBackup(history, backupId)); - history = BackupClientUtil.getHistory(conf1, 10, null, new Path(BACKUP_ROOT_DIR)); + BackupInfo.Filter nullFilter = new BackupInfo.Filter() { + @Override + public boolean apply(BackupInfo info) { + return true; + } + }; + history = BackupClientUtil.getHistory(conf1, 10, new Path(BACKUP_ROOT_DIR), nullFilter); assertTrue(findBackup(history, backupId)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -90,8 +96,23 @@ public class TestBackupShowHistory extends TestBackupBase { String backupId2 = fullTableBackup(tableList); assertTrue(checkSucceeded(backupId2)); LOG.info("backup complete: "+ table2); + BackupInfo.Filter tableNameFilter = new BackupInfo.Filter() { + @Override + public boolean apply(BackupInfo image) { + if (table1 == null) return true; + List names = image.getTableNames(); + return names.contains(table1); + } + }; + BackupInfo.Filter tableSetFilter = new BackupInfo.Filter() { + @Override + public boolean apply(BackupInfo info) { + String backupId = info.getBackupId(); + return backupId.startsWith("backup"); + } + }; - history = getBackupAdmin().getHistory(10, table1); + history = getBackupAdmin().getHistory(10, tableNameFilter, tableSetFilter); assertTrue(history.size() > 0); boolean success = true; for (BackupInfo info: history){ @@ -101,7 +122,9 @@ public class TestBackupShowHistory extends TestBackupBase { } } assertTrue(success); - history = BackupClientUtil.getHistory(conf1, 10, table1, new Path(BACKUP_ROOT_DIR)); + + history = BackupClientUtil.getHistory(conf1, 10, new Path(BACKUP_ROOT_DIR), + tableNameFilter, tableSetFilter); assertTrue(history.size() > 0); success = true; for (BackupInfo info: history){ @@ -111,7 +134,12 @@ public class TestBackupShowHistory extends TestBackupBase { } } assertTrue(success); - + + args = new String[]{"history", "-n", "10", "-path", BACKUP_ROOT_DIR, + "-t", "table1", "-set", "backup"}; + // Run backup + ret = ToolRunner.run(conf1, new BackupDriver(), args); + assertTrue(ret == 0); LOG.info("show_history"); }