Search in sources :

Example 6 with StorageException

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();
    }
}
Also used : Timer(com.codahale.metrics.Timer) File(java.io.File) StorageException(com.alipay.sofa.jraft.rhea.errors.StorageException) RocksDBException(org.rocksdb.RocksDBException) StorageException(com.alipay.sofa.jraft.rhea.errors.StorageException) IOException(java.io.IOException) 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 7 with StorageException

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();
    }
}
Also used : ColumnFamilyOptions(org.rocksdb.ColumnFamilyOptions) ReadOptions(org.rocksdb.ReadOptions) BackupableDBOptions(org.rocksdb.BackupableDBOptions) IngestExternalFileOptions(org.rocksdb.IngestExternalFileOptions) WriteOptions(org.rocksdb.WriteOptions) RestoreOptions(org.rocksdb.RestoreOptions) Options(org.rocksdb.Options) RocksDBOptions(com.alipay.sofa.jraft.rhea.options.RocksDBOptions) DBOptions(org.rocksdb.DBOptions) EnvOptions(org.rocksdb.EnvOptions) RocksDBException(org.rocksdb.RocksDBException) StringAppendOperator(org.rocksdb.StringAppendOperator) RocksIterator(org.rocksdb.RocksIterator) EnvOptions(org.rocksdb.EnvOptions) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) 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) SstFileWriter(org.rocksdb.SstFileWriter) Timer(com.codahale.metrics.Timer) ReadOptions(org.rocksdb.ReadOptions) StorageException(com.alipay.sofa.jraft.rhea.errors.StorageException) Map(java.util.Map) EnumMap(java.util.EnumMap) File(java.io.File)

Example 8 with StorageException

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();
    }
}
Also used : RocksDBException(org.rocksdb.RocksDBException) Timer(com.codahale.metrics.Timer) 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 9 with StorageException

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();
    }
}
Also used : BackupEngine(org.rocksdb.BackupEngine) RestoreOptions(org.rocksdb.RestoreOptions) RocksDBException(org.rocksdb.RocksDBException) Timer(com.codahale.metrics.Timer) BackupableDBOptions(org.rocksdb.BackupableDBOptions) 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 10 with StorageException

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();
    }
}
Also used : RocksDBException(org.rocksdb.RocksDBException) Timer(com.codahale.metrics.Timer) Map(java.util.Map) EnumMap(java.util.EnumMap) File(java.io.File) StorageException(com.alipay.sofa.jraft.rhea.errors.StorageException) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) IngestExternalFileOptions(org.rocksdb.IngestExternalFileOptions) 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)

Aggregations

StorageException (com.alipay.sofa.jraft.rhea.errors.StorageException)11 DistributedLock (com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock)9 Timer (com.codahale.metrics.Timer)9 Lock (java.util.concurrent.locks.Lock)9 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)9 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)9 RocksDBException (org.rocksdb.RocksDBException)9 File (java.io.File)8 IOException (java.io.IOException)4 BackupableDBOptions (org.rocksdb.BackupableDBOptions)3 Region (com.alipay.sofa.jraft.rhea.metadata.Region)2 EnumMap (java.util.EnumMap)2 Map (java.util.Map)2 BackupEngine (org.rocksdb.BackupEngine)2 ColumnFamilyHandle (org.rocksdb.ColumnFamilyHandle)2 IngestExternalFileOptions (org.rocksdb.IngestExternalFileOptions)2 RestoreOptions (org.rocksdb.RestoreOptions)2 RocksDBOptions (com.alipay.sofa.jraft.rhea.options.RocksDBOptions)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 BackupInfo (org.rocksdb.BackupInfo)1