Snapshot implements Watcher { public void run() throws IOException, KeeperException, InterruptedException { String timestamp = timestamp(TimestampFormat.snapshot); String connectString = admin.getConfiguration().get("hbase.zookeeper.quorum"); ZooKeeper zooKeeper = null; try { Map<String, String> failedSnapshotMap = new TreeMap<>(); zooKeeper = new ZooKeeper(connectString, SESSION_TIMEOUT, this); for (String tableName : args.tableSet(admin)) { if (args.isExcluded(tableName)) { System.out.println(timestamp(TimestampFormat.log) + " - Table \"" + tableName + "\" - EXCLUDED"); continue; } String snapshotName = getPrefix(tableName) + timestamp; try { snapshot(zooKeeper, tableName, snapshotName); } catch (Throwable e) { failedSnapshotMap.put(snapshotName, tableName); } deleteOldSnapshots(admin, tableName); } deleteSnapshotsForNotExistingTables(); deleteOldAbortZnodes(zooKeeper); retrySnapshot(zooKeeper, failedSnapshotMap); Util.sendAlertAfterSuccess(args, this.getClass()); } catch (Throwable e) { String message = "ConnectionString= " + connectString + ", CurrentHost= " + InetAddress.getLocalHost().getHostName() + ", Message= " + errorMessage(e); System.out.println("\n" + timestamp(TimestampFormat.log) + " - " + message); Util.sendAlertAfterFailed(args, this.getClass(), message); throw e; } finally { if (zooKeeper != null && zooKeeper.getState().isConnected()) { zooKeeper.close(); } } } Snapshot(HBaseAdmin admin, SnapshotArgs args); static void main(String[] argsParam); void run(); @Override void process(WatchedEvent watchedEvent);  }
@Test public void testAllTables() throws Exception { List<HBaseProtos.SnapshotDescription> snapshotDescriptions; createAdditionalTable(tableName + "2"); createAdditionalTable(tableName + "3"); String[] argsParam = {"localhost", ".*", "--test"}; SnapshotArgs args = new SnapshotArgs(argsParam); Snapshot app = new Snapshot(admin, args); app.run(); snapshotDescriptions = listSnapshots(tableName + ".*"); assertEquals(3, snapshotDescriptions.size()); Thread.sleep(1000); app.run(); snapshotDescriptions = listSnapshots(tableName + ".*"); assertEquals(6, snapshotDescriptions.size()); Thread.sleep(1000); app.run(); snapshotDescriptions = listSnapshots(tableName + ".*"); assertEquals(9, snapshotDescriptions.size()); Thread.sleep(1000); app.run(); snapshotDescriptions = listSnapshots(tableName + ".*"); assertEquals(12, snapshotDescriptions.size()); }
@Test public void testNotExistingTable() throws Exception { class SnapshotArgsTest extends SnapshotArgs { public SnapshotArgsTest(String[] args) throws IOException { super(args); } @Override public Set<String> tableSet(HBaseAdmin admin) throws IOException { Set<String> set = new TreeSet<>(); set.add("INVALID_TABLE"); return set; } } String[] argsParam; SnapshotArgs args; Snapshot app; argsParam = new String[]{"localhost", ".*", "--" + Args.OPTION_AFTER_FAILURE + "=" + AlertSenderTest.ALERT_SCRIPT}; args = new SnapshotArgsTest(argsParam); app = new Snapshot(admin, args); app.run(); }