use of com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore in project sofa-jraft by sofastack.
the class SnapshotBenchmark method snapshot.
public void snapshot(final boolean isSstSnapshot, final boolean isFastSnapshot) throws IOException {
final File backupDir = new File("backup");
if (backupDir.exists()) {
FileUtils.deleteDirectory(backupDir);
}
FileUtils.forceMkdir(backupDir);
final LocalFileMeta meta = doSnapshotSave(backupDir.getAbsolutePath(), isSstSnapshot, isFastSnapshot);
this.kvStore.shutdown();
FileUtils.deleteDirectory(new File(this.tempPath));
FileUtils.forceMkdir(new File(this.tempPath));
this.kvStore = new RocksRawKVStore();
this.kvStore.init(this.dbOptions);
final String name;
if (isSstSnapshot) {
name = "sst";
} else {
if (isFastSnapshot) {
name = "fast";
} else {
name = "slow";
}
}
final long decompressStart = System.nanoTime();
final String sourceFile = Paths.get(backupDir.getAbsolutePath(), SNAPSHOT_ARCHIVE).toString();
ZipUtil.decompress(sourceFile, backupDir.getAbsolutePath(), new CRC64());
System.out.println(name + " decompress time cost: " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - decompressStart));
final long loadStart = System.nanoTime();
doSnapshotLoad(backupDir.getAbsolutePath(), meta, isFastSnapshot);
System.out.println(name + " load snapshot time cost: " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - loadStart));
FileUtils.deleteDirectory(backupDir);
}
use of com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore in project sofa-jraft by sofastack.
the class RocksKVStoreTest method getSequenceTest.
/**
* Test method: {@link RocksRawKVStore#getSequence(byte[], int, KVStoreClosure)}
*/
@Test
public void getSequenceTest() throws InterruptedException {
final byte[] seqKey = makeKey("seq_test");
Sequence sequence = new SyncKVStore<Sequence>() {
@Override
public void execute(RawKVStore kvStore, KVStoreClosure closure) {
kvStore.getSequence(seqKey, 199, closure);
}
}.apply(this.kvStore);
assertEquals(sequence.getStartValue(), 0);
assertEquals(sequence.getEndValue(), 199);
Sequence sequence2 = new SyncKVStore<Sequence>() {
@Override
public void execute(RawKVStore kvStore, KVStoreClosure closure) {
kvStore.getSequence(seqKey, 10, closure);
}
}.apply(this.kvStore);
assertEquals(sequence2.getStartValue(), 199);
assertEquals(sequence2.getEndValue(), 209);
this.kvStore.resetSequence(seqKey, null);
Sequence sequence3 = new SyncKVStore<Sequence>() {
@Override
public void execute(RawKVStore kvStore, KVStoreClosure closure) {
kvStore.getSequence(seqKey, 11, closure);
}
}.apply(this.kvStore);
assertEquals(sequence3.getStartValue(), 0);
assertEquals(sequence3.getEndValue(), 11);
// read-only
Sequence sequence4 = new SyncKVStore<Sequence>() {
@Override
public void execute(RawKVStore kvStore, KVStoreClosure closure) {
kvStore.getSequence(seqKey, 0, closure);
}
}.apply(this.kvStore);
assertEquals(sequence4.getStartValue(), 11);
assertEquals(sequence4.getEndValue(), 11);
KVStoreClosure assertFailed = new BaseKVStoreClosure() {
@Override
public void run(Status status) {
assertEquals("Fail to [GET_SEQUENCE], step must >= 0", status.getErrorMsg());
}
};
this.kvStore.getSequence(seqKey, -1, assertFailed);
}
use of com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore in project sofa-jraft by sofastack.
the class RocksKVStoreTest method snapshotTest.
public void snapshotTest() throws Exception {
final File backupDir = new File("backup");
if (backupDir.exists()) {
FileUtils.deleteDirectory(backupDir);
}
FileUtils.forceMkdir(backupDir);
for (int i = 0; i < 100000; i++) {
final String v = String.valueOf(i);
this.kvStore.put(makeKey(v), makeValue(v), null);
}
final Region region = new Region();
KVStoreSnapshotFile kvStoreSnapshotFile = KVStoreSnapshotFileFactory.getKVStoreSnapshotFile(this.kvStore);
final ExecutorService snapshotPool = StoreEngineHelper.createSnapshotExecutor(1, 2);
final TestSnapshotWriter snapshotWriter = new TestSnapshotWriter(backupDir.getAbsolutePath());
final CountDownLatch latch = new CountDownLatch(1);
final Closure done = status -> {
assertTrue(status.isOk());
latch.countDown();
};
kvStoreSnapshotFile.save(snapshotWriter, region, done, snapshotPool);
latch.await();
final LocalFileMeta meta = (LocalFileMeta) snapshotWriter.getFileMeta(SNAPSHOT_ARCHIVE);
assertNotNull(meta);
assertNotNull(get(makeKey("1")));
this.kvStore.put(makeKey("100001"), makeValue("100001"), null);
assertNotNull(get(makeKey("100001")));
this.kvStore.shutdown();
FileUtils.deleteDirectory(new File(this.tempPath));
FileUtils.forceMkdir(new File(this.tempPath));
this.kvStore = new RocksRawKVStore();
this.kvStore.init(this.dbOptions);
assertNull(get(makeKey("1")));
final TestSnapshotReader snapshotReader = new TestSnapshotReader(snapshotWriter.metaTable, backupDir.getAbsolutePath());
kvStoreSnapshotFile = KVStoreSnapshotFileFactory.getKVStoreSnapshotFile(this.kvStore);
final boolean ret = kvStoreSnapshotFile.load(snapshotReader, region);
assertTrue(ret);
for (int i = 0; i < 100000; i++) {
final String v = String.valueOf(i);
assertArrayEquals(makeValue(v), get(makeKey(v)));
}
// key[100001] is put after the snapshot, so key[100001] should not exist.
assertNull(get(makeKey("100001")));
FileUtils.deleteDirectory(backupDir);
ExecutorServiceHelper.shutdownAndAwaitTermination(snapshotPool);
}
use of com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore in project sofa-jraft by sofastack.
the class RocksKVStoreTest method compareAndPutTest.
/**
* Test method: {@link RocksRawKVStore#compareAndPut(byte[], byte[], byte[], KVStoreClosure)}
*/
@Test
public void compareAndPutTest() {
final byte[] key = makeKey("put_test");
final byte[] value = makeValue("put_test_value");
this.kvStore.put(key, value, null);
final byte[] update = makeValue("put_test_update");
KVStoreClosure kvStoreClosure = new BaseKVStoreClosure() {
@Override
public void run(Status status) {
assertEquals(status, Status.OK());
}
};
this.kvStore.compareAndPut(key, value, update, kvStoreClosure);
assertEquals(kvStoreClosure.getData(), Boolean.TRUE);
byte[] newValue = new SyncKVStore<byte[]>() {
@Override
public void execute(RawKVStore kvStore, KVStoreClosure closure) {
kvStore.get(key, closure);
}
}.apply(this.kvStore);
assertArrayEquals(update, newValue);
this.kvStore.compareAndPut(key, value, update, kvStoreClosure);
assertEquals(kvStoreClosure.getData(), Boolean.FALSE);
}
use of com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore in project sofa-jraft by sofastack.
the class StoreEngine method initRocksDB.
private boolean initRocksDB(final StoreEngineOptions opts) {
RocksDBOptions rocksOpts = opts.getRocksDBOptions();
if (rocksOpts == null) {
rocksOpts = new RocksDBOptions();
opts.setRocksDBOptions(rocksOpts);
}
String dbPath = rocksOpts.getDbPath();
if (Strings.isNotBlank(dbPath)) {
try {
FileUtils.forceMkdir(new File(dbPath));
} catch (final Throwable t) {
LOG.error("Fail to make dir for dbPath {}.", dbPath);
return false;
}
} else {
dbPath = "";
}
final String childPath = "db_" + this.storeId + "_" + opts.getServerAddress().getPort();
rocksOpts.setDbPath(Paths.get(dbPath, childPath).toString());
this.dbPath = new File(rocksOpts.getDbPath());
final RocksRawKVStore rocksRawKVStore = new RocksRawKVStore();
if (!rocksRawKVStore.init(rocksOpts)) {
LOG.error("Fail to init [RocksRawKVStore].");
return false;
}
this.rawKVStore = rocksRawKVStore;
return true;
}
Aggregations