use of org.rocksdb.BackupEngine in project sofa-jraft by sofastack.
the class RocksRawKVStore method backupDB.
RocksDBBackupInfo backupDB(final String backupDBPath) throws IOException {
final Timer.Context timeCtx = getTimeContext("BACKUP_DB");
FileUtils.forceMkdir(new File(backupDBPath));
final Lock writeLock = this.readWriteLock.writeLock();
writeLock.lock();
try (final BackupableDBOptions backupOpts = createBackupDBOptions(backupDBPath);
final BackupEngine backupEngine = BackupEngine.open(this.options.getEnv(), backupOpts)) {
backupEngine.createNewBackup(this.db, true);
final List<BackupInfo> backupInfoList = backupEngine.getBackupInfo();
if (backupInfoList.isEmpty()) {
LOG.warn("Fail to backup at {}, empty backup info.", backupDBPath);
return null;
}
// chose the backupInfo who has max backupId
final BackupInfo backupInfo = Collections.max(backupInfoList, Comparator.comparingInt(BackupInfo::backupId));
final RocksDBBackupInfo rocksBackupInfo = new RocksDBBackupInfo(backupInfo);
LOG.info("Backup rocksDB into {} with backupInfo {}.", backupDBPath, rocksBackupInfo);
return rocksBackupInfo;
} catch (final RocksDBException e) {
throw new StorageException("Fail to backup at path: " + backupDBPath, e);
} finally {
writeLock.unlock();
timeCtx.stop();
}
}
use of org.rocksdb.BackupEngine in project xdagj by XDagger.
the class RocksdbKVSource method backup.
public void backup() {
resetDbLock.readLock().lock();
if (log.isTraceEnabled()) {
log.trace("~> RocksdbKVSource.backup(): " + name);
}
Path path = backupPath();
path.toFile().mkdirs();
try (BackupableDBOptions backupOptions = new BackupableDBOptions(path.toString());
BackupEngine backups = BackupEngine.open(Env.getDefault(), backupOptions)) {
backups.createNewBackup(db, true);
if (log.isTraceEnabled()) {
log.trace("<~ RocksdbKVSource.backup(): " + name + " done");
}
} catch (RocksDBException e) {
log.error("Failed to backup database '{}'", name, e);
hintOnTooManyOpenFiles(e);
throw new RuntimeException(e);
} finally {
resetDbLock.readLock().unlock();
}
}
use of org.rocksdb.BackupEngine in project yamcs by yamcs.
the class RDBFactory method restoreBackup.
public CompletableFuture<Void> restoreBackup(int backupId, String backupDir, String relativePath) {
CompletableFuture<Void> cf = new CompletableFuture<>();
executor.execute(() -> {
try (BackupableDBOptions opt = new BackupableDBOptions(backupDir);
BackupEngine backupEngine = BackupEngine.open(Env.getDefault(), opt);
RestoreOptions restoreOpt = new RestoreOptions(false)) {
String absolutePath = getAbsolutePath(relativePath);
if (backupId == -1) {
backupEngine.restoreDbFromLatestBackup(absolutePath, absolutePath, restoreOpt);
} else {
backupEngine.restoreDbFromBackup(backupId, absolutePath, absolutePath, restoreOpt);
}
cf.complete(null);
} catch (Exception e) {
cf.completeExceptionally(e);
} finally {
}
});
return cf;
}
use of org.rocksdb.BackupEngine in project yamcs by yamcs.
the class RDBFactory method doBackup.
/**
* Performs a backup of the database to the given directory
*
* @param relativePath
* @param backupDir
* @return a future that can be used to know when the backup has finished and if there was any error
*/
public CompletableFuture<Void> doBackup(String relativePath, String backupDir) {
CompletableFuture<Void> cf = new CompletableFuture<>();
executor.execute(() -> {
YRDB db = null;
try {
BackupUtils.verifyBackupDirectory(backupDir, false);
} catch (IOException e) {
log.warn("Invalid backup directory: {} ", e.toString());
cf.completeExceptionally(e);
return;
}
try (BackupableDBOptions opt = new BackupableDBOptions(backupDir);
BackupEngine backupEngine = BackupEngine.open(Env.getDefault(), opt)) {
db = getRdb(relativePath, false);
backupEngine.createNewBackup(db.getDb());
cf.complete(null);
} catch (Exception e) {
log.warn("Got error when creating the backup: {} ", e.getMessage());
cf.completeExceptionally(e);
} finally {
if (db != null) {
dispose(db);
}
}
});
return cf;
}
use of org.rocksdb.BackupEngine in project yamcs by yamcs.
the class RDBFactory method restoreBackup.
public CompletableFuture<Void> restoreBackup(String backupDir, String relativePath) {
CompletableFuture<Void> cf = new CompletableFuture<>();
executor.execute(() -> {
try (BackupableDBOptions opt = new BackupableDBOptions(backupDir);
BackupEngine backupEngine = BackupEngine.open(Env.getDefault(), opt);
RestoreOptions restoreOpt = new RestoreOptions(false)) {
String absolutePath = getAbsolutePath(relativePath);
backupEngine.restoreDbFromLatestBackup(absolutePath, absolutePath, restoreOpt);
cf.complete(null);
} catch (Exception e) {
cf.completeExceptionally(e);
} finally {
}
});
return cf;
}
Aggregations