Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
terence-yoo committed Mar 9, 2018
1 parent 74dbd97 commit 843291c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

0 comments on commit 843291c

Please sign in to comment.