Search in sources :

Example 1 with BackupEngine

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();
    }
}
Also used : BackupInfo(org.rocksdb.BackupInfo) BackupEngine(org.rocksdb.BackupEngine) RocksDBException(org.rocksdb.RocksDBException) Timer(com.codahale.metrics.Timer) BackupableDBOptions(org.rocksdb.BackupableDBOptions) File(java.io.File) StorageException(com.alipay.sofa.jraft.rhea.errors.StorageException) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) DistributedLock(com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Example 2 with BackupEngine

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();
    }
}
Also used : Path(java.nio.file.Path) BackupEngine(org.rocksdb.BackupEngine) RocksDBException(org.rocksdb.RocksDBException) BackupableDBOptions(org.rocksdb.BackupableDBOptions)

Example 3 with BackupEngine

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;
}
Also used : BackupEngine(org.rocksdb.BackupEngine) RestoreOptions(org.rocksdb.RestoreOptions) CompletableFuture(java.util.concurrent.CompletableFuture) BackupableDBOptions(org.rocksdb.BackupableDBOptions) IOException(java.io.IOException) RocksDBException(org.rocksdb.RocksDBException)

Example 4 with BackupEngine

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;
}
Also used : BackupEngine(org.rocksdb.BackupEngine) CompletableFuture(java.util.concurrent.CompletableFuture) BackupableDBOptions(org.rocksdb.BackupableDBOptions) IOException(java.io.IOException) IOException(java.io.IOException) RocksDBException(org.rocksdb.RocksDBException)

Example 5 with BackupEngine

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;
}
Also used : BackupEngine(org.rocksdb.BackupEngine) RestoreOptions(org.rocksdb.RestoreOptions) CompletableFuture(java.util.concurrent.CompletableFuture) BackupableDBOptions(org.rocksdb.BackupableDBOptions) IOException(java.io.IOException) RocksDBException(org.rocksdb.RocksDBException)

Aggregations

BackupEngine (org.rocksdb.BackupEngine)9 BackupableDBOptions (org.rocksdb.BackupableDBOptions)9 RocksDBException (org.rocksdb.RocksDBException)9 RestoreOptions (org.rocksdb.RestoreOptions)5 Timer (com.codahale.metrics.Timer)4 IOException (java.io.IOException)4 Lock (java.util.concurrent.locks.Lock)4 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)4 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)4 CompletableFuture (java.util.concurrent.CompletableFuture)3 StorageException (com.alipay.sofa.jraft.rhea.errors.StorageException)2 DistributedLock (com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock)2 StorageException (io.dingodb.store.row.errors.StorageException)2 DistributedLock (io.dingodb.store.row.util.concurrent.DistributedLock)2 File (java.io.File)2 Path (java.nio.file.Path)2 BackupInfo (org.rocksdb.BackupInfo)2 BlockBasedTableConfig (org.rocksdb.BlockBasedTableConfig)1 BloomFilter (org.rocksdb.BloomFilter)1 LRUCache (org.rocksdb.LRUCache)1