Skip to content

Commit

Permalink
tiny improve
Browse files Browse the repository at this point in the history
Change-Id: I29adb7df352bbbba3fbd416831b11b0968818cb0
  • Loading branch information
Linary committed Mar 26, 2021
1 parent 084bca2 commit 060f190
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public Path buildSnapshotPath(Path originDataPath, String snapshotPrefix,
RocksDB rocksdb = this.createSnapshotRocksDB(snapshotPath.toString());
Path snapshotLinkPath = Paths.get(originDataPath + "_link");
try {
createCheckpoint(rocksdb, snapshotLinkPath.toString());
this.createCheckpoint(rocksdb, snapshotLinkPath.toString());
} finally {
rocksdb.close();
}
Expand All @@ -360,7 +360,7 @@ public Path buildSnapshotPath(Path originDataPath, String snapshotPrefix,

@Override
public void createSnapshot(String snapshotPath) {
createCheckpoint(this.rocksdb, snapshotPath);
this.createCheckpoint(this.rocksdb, snapshotPath);
}

@Override
Expand Down Expand Up @@ -488,6 +488,29 @@ private RocksDB createSnapshotRocksDB(String snapshotPath)
return RocksDB.open(options, snapshotPath, cfds, cfhs);
}

private void createCheckpoint(RocksDB rocksdb, String targetPath) {
// https://github.com/facebook/rocksdb/wiki/Checkpoints
try (Checkpoint checkpoint = Checkpoint.create(rocksdb)) {
String tempPath = targetPath + "_temp";
File tempFile = new File(tempPath);
FileUtils.deleteDirectory(tempFile);
LOG.debug("Deleted temp directory {}", tempFile);

FileUtils.forceMkdir(tempFile.getParentFile());
checkpoint.createCheckpoint(tempPath);
File snapshotFile = new File(targetPath);
FileUtils.deleteDirectory(snapshotFile);
LOG.debug("Deleted stale directory {}", snapshotFile);
if (!tempFile.renameTo(snapshotFile)) {
throw new IOException(String.format("Failed to rename %s to %s",
tempFile, snapshotFile));
}
} catch (Exception e) {
throw new BackendException("Failed to create checkpoint at path %s",
e, targetPath);
}
}

public static Set<String> listCFs(String path) throws RocksDBException {
Set<String> cfs = new HashSet<>();

Expand Down Expand Up @@ -693,29 +716,6 @@ public static final String decode(byte[] bytes) {
return StringEncoding.decode(bytes);
}

public static void createCheckpoint(RocksDB rocksdb, String targetPath) {
// https://github.com/facebook/rocksdb/wiki/Checkpoints
try (Checkpoint checkpoint = Checkpoint.create(rocksdb)) {
String tempPath = targetPath + "_temp";
File tempFile = new File(tempPath);
FileUtils.deleteDirectory(tempFile);
LOG.debug("Deleted temp directory {}", tempFile);

FileUtils.forceMkdir(tempFile.getParentFile());
checkpoint.createCheckpoint(tempPath);
File snapshotFile = new File(targetPath);
FileUtils.deleteDirectory(snapshotFile);
LOG.debug("Deleted stale directory {}", snapshotFile);
if (!tempFile.renameTo(snapshotFile)) {
throw new IOException(String.format("Failed to rename %s to %s",
tempFile, snapshotFile));
}
} catch (Exception e) {
throw new BackendException("Failed to create checkpoint at path %s",
e, targetPath);
}
}

private class CFHandle implements Closeable {

private final ColumnFamilyHandle handle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,9 +663,12 @@ public void resumeSnapshot(String snapshotPrefix, boolean deleteSnapshot) {
RocksDBSessions sessions = entry.getValue();
sessions.resumeSnapshot(snapshotPath.toString());

Path parentPath = snapshotPath.getParent();
if (Files.list(parentPath).count() == 0) {
FileUtils.deleteDirectory(parentPath.toFile());
if (deleteSnapshot) {
// Delete empty snapshot parent directory
Path parentPath = snapshotPath.getParent();
if (Files.list(parentPath).count() == 0) {
FileUtils.deleteDirectory(parentPath.toFile());
}
}
}
LOG.info("The store '{}' resume snapshot successfully", this.store);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5492,7 +5492,7 @@ public void testRemoveEdgesOfSuperVertex() {
try {
graph.truncateBackend();
} catch (UnsupportedOperationException e) {
LOG.warn("Failed to create snapshot", e);
LOG.warn("Failed to truncate backend", e);
}
}

Expand Down

0 comments on commit 060f190

Please sign in to comment.