use of com.alipay.sofa.jraft.rhea.errors.StorageException in project sofa-jraft by sofastack.
the class RocksRawKVStore method readSnapshot.
void readSnapshot(final String snapshotPath) {
final Timer.Context timeCtx = getTimeContext("READ_SNAPSHOT");
final Lock writeLock = this.readWriteLock.writeLock();
writeLock.lock();
try {
final File snapshotFile = new File(snapshotPath);
if (!snapshotFile.exists()) {
LOG.error("Snapshot file [{}] not exists.", snapshotPath);
return;
}
closeRocksDB();
final String dbPath = this.opts.getDbPath();
final File dbFile = new File(dbPath);
FileUtils.deleteDirectory(dbFile);
if (!Utils.atomicMoveFile(snapshotFile, dbFile, true)) {
throw new StorageException("Fail to rename [" + snapshotPath + "] to [" + dbPath + "].");
}
// reopen the db
openRocksDB(this.opts);
} catch (final Exception e) {
throw new StorageException("Fail to read snapshot from path: " + snapshotPath, e);
} finally {
writeLock.unlock();
timeCtx.stop();
}
}
use of com.alipay.sofa.jraft.rhea.errors.StorageException in project sofa-jraft by sofastack.
the class RocksRawKVStore method doCreateSstFiles.
void doCreateSstFiles(final Snapshot snapshot, final EnumMap<SstColumnFamily, File> sstFileTable, final byte[] startKey, final byte[] endKey, final CompletableFuture<Void> future) {
final Timer.Context timeCtx = getTimeContext("CREATE_SST_FILE");
final Lock readLock = this.readWriteLock.readLock();
readLock.lock();
try {
if (!this.shutdownLock.isAvailable()) {
// KV store has shutdown, we do not release rocksdb's snapshot
future.completeExceptionally(new StorageException("KV store has shutdown."));
return;
}
try (final ReadOptions readOptions = new ReadOptions();
final EnvOptions envOptions = new EnvOptions();
final Options options = new Options().setMergeOperator(new StringAppendOperator())) {
readOptions.setSnapshot(snapshot);
for (final Map.Entry<SstColumnFamily, File> entry : sstFileTable.entrySet()) {
final SstColumnFamily sstColumnFamily = entry.getKey();
final File sstFile = entry.getValue();
final ColumnFamilyHandle columnFamilyHandle = findColumnFamilyHandle(sstColumnFamily);
try (final RocksIterator it = this.db.newIterator(columnFamilyHandle, readOptions);
final SstFileWriter sstFileWriter = new SstFileWriter(envOptions, options)) {
if (startKey == null) {
it.seekToFirst();
} else {
it.seek(startKey);
}
sstFileWriter.open(sstFile.getAbsolutePath());
long count = 0;
for (; ; ) {
if (!it.isValid()) {
break;
}
final byte[] key = it.key();
if (endKey != null && BytesUtil.compare(key, endKey) >= 0) {
break;
}
sstFileWriter.put(key, it.value());
++count;
it.next();
}
if (count == 0) {
sstFileWriter.close();
} else {
sstFileWriter.finish();
}
LOG.info("Finish sst file {} with {} keys.", sstFile, count);
} catch (final RocksDBException e) {
throw new StorageException("Fail to create sst file at path: " + sstFile, e);
}
}
future.complete(null);
} catch (final Throwable t) {
future.completeExceptionally(t);
} finally {
// Nothing to release, rocksDB never own the pointer for a snapshot.
snapshot.close();
// The pointer to the snapshot is released by the database instance.
this.db.releaseSnapshot(snapshot);
}
} finally {
readLock.unlock();
timeCtx.stop();
}
}
use of com.alipay.sofa.jraft.rhea.errors.StorageException in project sofa-jraft by sofastack.
the class RocksRawKVStore method initFencingToken.
@Override
public void initFencingToken(final byte[] parentKey, final byte[] childKey) {
final Timer.Context timeCtx = getTimeContext("INIT_FENCING_TOKEN");
final Lock readLock = this.readWriteLock.readLock();
readLock.lock();
try {
final byte[] realKey = BytesUtil.nullToEmpty(parentKey);
final byte[] parentBytesVal = this.db.get(this.fencingHandle, realKey);
if (parentBytesVal == null) {
return;
}
this.db.put(this.fencingHandle, this.writeOptions, childKey, parentBytesVal);
} catch (final RocksDBException e) {
throw new StorageException("Fail to init fencing token.", e);
} finally {
readLock.unlock();
timeCtx.stop();
}
}
use of com.alipay.sofa.jraft.rhea.errors.StorageException in project sofa-jraft by sofastack.
the class RocksRawKVStore method restoreBackup.
void restoreBackup(final String backupDBPath, final RocksDBBackupInfo rocksBackupInfo) {
final Timer.Context timeCtx = getTimeContext("RESTORE_BACKUP");
final Lock writeLock = this.readWriteLock.writeLock();
writeLock.lock();
closeRocksDB();
try (final BackupableDBOptions backupOpts = createBackupDBOptions(backupDBPath);
final BackupEngine backupEngine = BackupEngine.open(this.options.getEnv(), backupOpts);
final RestoreOptions restoreOpts = new RestoreOptions(false)) {
final String dbPath = this.opts.getDbPath();
backupEngine.restoreDbFromBackup(rocksBackupInfo.getBackupId(), dbPath, dbPath, restoreOpts);
LOG.info("Restored rocksDB from {} with {}.", backupDBPath, rocksBackupInfo);
// reopen the db
openRocksDB(this.opts);
} catch (final RocksDBException e) {
throw new StorageException("Fail to restore from path: " + backupDBPath, e);
} finally {
writeLock.unlock();
timeCtx.stop();
}
}
use of com.alipay.sofa.jraft.rhea.errors.StorageException in project sofa-jraft by sofastack.
the class RocksRawKVStore method ingestSstFiles.
void ingestSstFiles(final EnumMap<SstColumnFamily, File> sstFileTable) {
final Timer.Context timeCtx = getTimeContext("INGEST_SST_FILE");
final Lock readLock = this.readWriteLock.readLock();
readLock.lock();
try {
for (final Map.Entry<SstColumnFamily, File> entry : sstFileTable.entrySet()) {
final SstColumnFamily sstColumnFamily = entry.getKey();
final File sstFile = entry.getValue();
final ColumnFamilyHandle columnFamilyHandle = findColumnFamilyHandle(sstColumnFamily);
try (final IngestExternalFileOptions ingestOptions = new IngestExternalFileOptions()) {
if (FileUtils.sizeOf(sstFile) == 0L) {
return;
}
final String filePath = sstFile.getAbsolutePath();
LOG.info("Start ingest sst file {}.", filePath);
this.db.ingestExternalFile(columnFamilyHandle, Collections.singletonList(filePath), ingestOptions);
} catch (final RocksDBException e) {
throw new StorageException("Fail to ingest sst file at path: " + sstFile, e);
}
}
} finally {
readLock.unlock();
timeCtx.stop();
}
}
Aggregations