diff --git a/hbase0.98/hbase-snapshot-0.98/src/main/java/com/kakao/hbase/snapshot/Snapshot.java b/hbase0.98/hbase-snapshot-0.98/src/main/java/com/kakao/hbase/snapshot/Snapshot.java index 2272b97..732cadb 100644 --- a/hbase0.98/hbase-snapshot-0.98/src/main/java/com/kakao/hbase/snapshot/Snapshot.java +++ b/hbase0.98/hbase-snapshot-0.98/src/main/java/com/kakao/hbase/snapshot/Snapshot.java @@ -34,14 +34,14 @@ public class Snapshot implements Watcher { static final SimpleDateFormat DATE_FORMAT_SNAPSHOT = new SimpleDateFormat("yyyyMMddHHmmss"); + static final int ABORT_ZNODE_AGE_THRESHOLD_MS = 24 * 60 * 60 * 1000; + static final String TIMESTAMP_PREFIX = "_S"; private static final int SESSION_TIMEOUT = 120000; private static final SimpleDateFormat DATE_FORMAT_LOG = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static final String ABORT_WATCH_PREFIX = "/hbase/online-snapshot/abort/"; private static final String ACQUIRED_WATCH_PREFIX = "/hbase/online-snapshot/acquired/"; - private static final String TIMESTAMP_PREFIX = "_S"; private static final int MAX_RETRY = 10; private static final long RETRY_INTERVAL = 2000; - private static final int ABORT_ZNODE_AGE_THRESHOLD_MS = 24 * 60 * 60 * 1000; @VisibleForTesting static boolean skipCheckTableExistence = false; private final SnapshotArgs args; @@ -159,8 +159,7 @@ private void deleteOldAbortZnodes(ZooKeeper zooKeeper) throws KeeperException, I String parentZnode = ABORT_WATCH_PREFIX.substring(0, ABORT_WATCH_PREFIX.length() - 1); List children = zooKeeper.getChildren(parentZnode, false); for (String snapshotName : children) { - long abortZnodeAge = System.currentTimeMillis() - SnapshotUtil.getSnapshotTimestamp(snapshotName); - if (abortZnodeAge > ABORT_ZNODE_AGE_THRESHOLD_MS) { + if (SnapshotUtil.isOldZnode(snapshotName)) { String abortZnode = ABORT_WATCH_PREFIX + snapshotName; System.out.println(timestamp(TimestampFormat.log) + " - znode deleted - " + abortZnode); zooKeeper.delete(abortZnode, -1); diff --git a/hbase0.98/hbase-snapshot-0.98/src/main/java/com/kakao/hbase/snapshot/SnapshotUtil.java b/hbase0.98/hbase-snapshot-0.98/src/main/java/com/kakao/hbase/snapshot/SnapshotUtil.java index 96d5e5e..7faf575 100644 --- a/hbase0.98/hbase-snapshot-0.98/src/main/java/com/kakao/hbase/snapshot/SnapshotUtil.java +++ b/hbase0.98/hbase-snapshot-0.98/src/main/java/com/kakao/hbase/snapshot/SnapshotUtil.java @@ -3,9 +3,21 @@ public class SnapshotUtil { static long getSnapshotTimestamp(String snapshotName) { try { - return Snapshot.DATE_FORMAT_SNAPSHOT.parse(snapshotName.substring(snapshotName.length() - 14)).getTime(); + String suffix = snapshotName.substring(snapshotName.length() - 16); + if (suffix.startsWith(Snapshot.TIMESTAMP_PREFIX)) { + return Snapshot.DATE_FORMAT_SNAPSHOT.parse(suffix.substring(2)).getTime(); + } else { + // invalid snapshot name format + return 0; + } } catch (Throwable e) { + // invalid snapshot name format return 0; } } + + static boolean isOldZnode(String snapshotName) { + long abortZnodeAge = System.currentTimeMillis() - getSnapshotTimestamp(snapshotName); + return getSnapshotTimestamp(snapshotName) > 0 && abortZnodeAge > Snapshot.ABORT_ZNODE_AGE_THRESHOLD_MS; + } } diff --git a/hbase0.98/hbase-snapshot-0.98/src/test/java/com/kakao/hbase/snapshot/SnapshotUtilTest.java b/hbase0.98/hbase-snapshot-0.98/src/test/java/com/kakao/hbase/snapshot/SnapshotUtilTest.java index c9590a0..131f21b 100644 --- a/hbase0.98/hbase-snapshot-0.98/src/test/java/com/kakao/hbase/snapshot/SnapshotUtilTest.java +++ b/hbase0.98/hbase-snapshot-0.98/src/test/java/com/kakao/hbase/snapshot/SnapshotUtilTest.java @@ -16,4 +16,10 @@ public void testGetSnapshotTimestamp() throws ParseException { Assert.assertEquals(date.getTime(), snapshotTimestamp); Assert.assertEquals(timestampMS, snapshotTimestamp); } + + @Test + public void testInvalidSnapshotName() { + Assert.assertTrue(SnapshotUtil.isOldZnode("valid-snapshot-name_S20000309110025")); + Assert.assertFalse(SnapshotUtil.isOldZnode("invalid-snapshot-name")); + } }