use of org.apache.bookkeeper.statelib.api.exceptions.StateStoreException in project bookkeeper by apache.
the class RocksdbKVStore method openRocksdb.
protected static Pair<RocksDB, List<ColumnFamilyHandle>> openRocksdb(File dir, DBOptions options, ColumnFamilyOptions cfOpts) throws StateStoreException {
// make sure the db directory's parent dir is created
ColumnFamilyDescriptor metaDesc = new ColumnFamilyDescriptor(METADATA_CF, cfOpts);
ColumnFamilyDescriptor dataDesc = new ColumnFamilyDescriptor(DATA_CF, cfOpts);
try {
Files.createDirectories(dir.toPath());
File dbDir = new File(dir, "current");
if (!dbDir.exists()) {
// empty state
String uuid = UUID.randomUUID().toString();
Path checkpointPath = Paths.get(dir.getAbsolutePath(), "checkpoints", uuid);
Files.createDirectories(checkpointPath);
Files.createSymbolicLink(Paths.get(dbDir.getAbsolutePath()), checkpointPath);
}
List<ColumnFamilyHandle> cfHandles = Lists.newArrayListWithExpectedSize(2);
RocksDB db = RocksDB.open(options, dbDir.getAbsolutePath(), Lists.newArrayList(metaDesc, dataDesc), cfHandles);
return Pair.of(db, cfHandles);
} catch (IOException ioe) {
log.error("Failed to create parent directory {} for opening rocksdb", dir.getParentFile().toPath(), ioe);
throw new StateStoreException(ioe);
} catch (RocksDBException dbe) {
log.error("Failed to open rocksdb at dir {}", dir.getAbsolutePath(), dbe);
throw new StateStoreException(dbe);
}
}
use of org.apache.bookkeeper.statelib.api.exceptions.StateStoreException in project bookkeeper by apache.
the class RocksCheckpointer method restore.
public static CheckpointMetadata restore(String dbName, File dbPath, CheckpointStore checkpointStore) throws StateStoreException {
try {
String dbPrefix = String.format("%s", dbName);
Pair<String, CheckpointMetadata> latestCheckpoint = getLatestCheckpoint(dbPrefix, checkpointStore);
File checkpointsDir = new File(dbPath, "checkpoints");
String checkpointId = latestCheckpoint.getLeft();
CheckpointMetadata checkpointMetadata = latestCheckpoint.getRight();
if (checkpointId != null) {
RocksdbRestoreTask task = new RocksdbRestoreTask(dbName, checkpointsDir, checkpointStore);
task.restore(checkpointId, checkpointMetadata);
} else {
// no checkpoints available, create an empty directory
checkpointId = UUID.randomUUID().toString();
Files.createDirectories(Paths.get(checkpointsDir.getAbsolutePath(), checkpointId));
}
Path restoredCheckpointPath = Paths.get(checkpointsDir.getAbsolutePath(), checkpointId);
log.info("Successfully restore checkpoint {} to {}", checkpointId, restoredCheckpointPath);
File currentDir = new File(dbPath, "current");
Files.deleteIfExists(Paths.get(currentDir.getAbsolutePath()));
Files.createSymbolicLink(Paths.get(currentDir.getAbsolutePath()), restoredCheckpointPath);
// after successfully restore from remote checkpoints, cleanup other unused checkpoints
cleanupLocalCheckpoints(checkpointsDir, checkpointId);
return checkpointMetadata;
} catch (IOException ioe) {
log.error("Failed to restore rocksdb {}", dbName, ioe);
throw new StateStoreException("Failed to restore rocksdb " + dbName, ioe);
}
}
use of org.apache.bookkeeper.statelib.api.exceptions.StateStoreException in project bookkeeper by apache.
the class RocksdbRestoreTask method restore.
public void restore(String checkpointId, CheckpointMetadata metadata) throws StateStoreException {
File checkpointedDir = new File(checkpointDir, checkpointId);
try {
List<String> filesToCopy = getFilesToCopy(checkpointId, checkpointedDir, metadata);
copyFilesFromRemote(checkpointId, checkpointedDir, filesToCopy);
} catch (IOException ioe) {
log.error("Failed to restore checkpoint {} to local directory {}", new Object[] { checkpointId, checkpointedDir, ioe });
throw new StateStoreException("Failed to restore checkpoint " + checkpointId + " to local directory " + checkpointedDir, ioe);
}
}
use of org.apache.bookkeeper.statelib.api.exceptions.StateStoreException in project bookkeeper by apache.
the class RocksdbKVStore method readLastRevision.
private void readLastRevision() throws StateStoreException {
byte[] revisionBytes;
try {
revisionBytes = db.get(metaCfHandle, LAST_REVISION);
} catch (RocksDBException e) {
throw new StateStoreException("Failed to read last revision from state store " + name(), e);
}
if (null == revisionBytes) {
return;
}
long revision = Bytes.toLong(revisionBytes, 0);
lastRevisionUpdater.set(this, revision);
}
use of org.apache.bookkeeper.statelib.api.exceptions.StateStoreException in project bookkeeper by apache.
the class RocksdbCheckpointTask method checkpoint.
public String checkpoint(byte[] txid) throws StateStoreException {
String checkpointId = UUID.randomUUID().toString();
File tempDir = new File(checkpointDir, checkpointId);
log.info("Create a local checkpoint of state store {} at {}", dbName, tempDir);
try {
try {
checkpoint.createCheckpoint(tempDir.getAbsolutePath());
} catch (RocksDBException e) {
throw new StateStoreException("Failed to create a checkpoint at " + tempDir, e);
}
String remoteCheckpointPath = RocksUtils.getDestCheckpointPath(dbPrefix, checkpointId);
if (!checkpointStore.fileExists(remoteCheckpointPath)) {
checkpointStore.createDirectories(remoteCheckpointPath);
}
String sstsPath = RocksUtils.getDestSstsPath(dbPrefix);
if (!checkpointStore.fileExists(sstsPath)) {
checkpointStore.createDirectories(sstsPath);
}
// get the files to copy
List<File> filesToCopy = getFilesToCopy(tempDir);
// copy the files
copyFilesToDest(checkpointId, filesToCopy);
// finalize copy files
finalizeCopyFiles(checkpointId, filesToCopy);
// dump the file list to checkpoint file
finalizeCheckpoint(checkpointId, tempDir, txid);
// clean up the remote checkpoints
if (removeRemoteCheckpointsAfterSuccessfulCheckpoint) {
cleanupRemoteCheckpoints(tempDir, checkpointId);
}
return checkpointId;
} catch (IOException ioe) {
log.error("Failed to checkpoint db {} to dir {}", new Object[] { dbName, tempDir, ioe });
throw new StateStoreException("Failed to checkpoint db " + dbName + " to dir " + tempDir, ioe);
} finally {
if (removeLocalCheckpointAfterSuccessfulCheckpoint && tempDir.exists()) {
try {
MoreFiles.deleteRecursively(Paths.get(tempDir.getAbsolutePath()), RecursiveDeleteOption.ALLOW_INSECURE);
} catch (IOException ioe) {
log.warn("Failed to remove temporary checkpoint dir {}", tempDir, ioe);
}
}
}
}
Aggregations