Skip to content

Commit

Permalink
Delete old /hbase/online-snapshot/abort znodes
Browse files Browse the repository at this point in the history
  • Loading branch information
terence-yoo committed Mar 9, 2018
1 parent 848504e commit 4dc0c38
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@
import java.util.*;

public class Snapshot implements Watcher {
static final SimpleDateFormat DATE_FORMAT_SNAPSHOT = new SimpleDateFormat("yyyyMMddHHmmss");
private static final int SESSION_TIMEOUT = 120000;
private static final SimpleDateFormat DATE_FORMAT_SNAPSHOT = new SimpleDateFormat("yyyyMMddHHmmss");
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 @@ -137,6 +138,7 @@ public void run() throws IOException, KeeperException, InterruptedException {
deleteOldSnapshots(admin, tableName);
}
deleteSnapshotsForNotExistingTables();
deleteOldAbortZnodes(zooKeeper);

retrySnapshot(zooKeeper, failedSnapshotMap);
Util.sendAlertAfterSuccess(args, this.getClass());
Expand All @@ -153,6 +155,19 @@ public void run() throws IOException, KeeperException, InterruptedException {
}
}

private void deleteOldAbortZnodes(ZooKeeper zooKeeper) throws KeeperException, InterruptedException {
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) {
String abortZnode = ABORT_WATCH_PREFIX + snapshotName;
System.out.println(timestamp(TimestampFormat.log) + " - znode deleted - " + abortZnode);
zooKeeper.delete(abortZnode, -1);
}
}
}

/**
* Retry failed snapshots
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.kakao.hbase.snapshot;

public class SnapshotUtil {
static long getSnapshotTimestamp(String snapshotName) {
try {
return Snapshot.DATE_FORMAT_SNAPSHOT.parse(snapshotName.substring(snapshotName.length() - 14)).getTime();
} catch (Throwable e) {
return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.kakao.hbase.snapshot;

import org.junit.Assert;
import org.junit.Test;

import java.text.ParseException;
import java.util.Date;

public class SnapshotUtilTest {
@Test
public void testGetSnapshotTimestamp() throws ParseException {
String timestampStr = "20180309110025";
long timestampMS = 1520560825000L;
long snapshotTimestamp = SnapshotUtil.getSnapshotTimestamp("table1_S" + timestampStr);
Date date = Snapshot.DATE_FORMAT_SNAPSHOT.parse(timestampStr);
Assert.assertEquals(date.getTime(), snapshotTimestamp);
Assert.assertEquals(timestampMS, snapshotTimestamp);
}
}

0 comments on commit 4dc0c38

Please sign in to comment.