Search in sources :

Example 6 with RocksRawKVStore

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);
}
Also used : CRC64(com.alipay.sofa.jraft.util.CRC64) RocksRawKVStore(com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore) LocalFileMeta(com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta) File(java.io.File)

Example 7 with RocksRawKVStore

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);
}
Also used : Status(com.alipay.sofa.jraft.Status) RawKVStore(com.alipay.sofa.jraft.rhea.storage.RawKVStore) RocksRawKVStore(com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) KVStoreClosure(com.alipay.sofa.jraft.rhea.storage.KVStoreClosure) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) Sequence(com.alipay.sofa.jraft.rhea.storage.Sequence) Test(org.junit.Test)

Example 8 with RocksRawKVStore

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);
}
Also used : StoreEngineHelper(com.alipay.sofa.jraft.rhea.StoreEngineHelper) Region(com.alipay.sofa.jraft.rhea.metadata.Region) TestSnapshotReader(com.alipay.sofa.jraft.rhea.storage.TestSnapshotReader) SyncKVStore(com.alipay.sofa.jraft.rhea.storage.SyncKVStore) Lists(com.alipay.sofa.jraft.rhea.util.Lists) SstColumnFamily(com.alipay.sofa.jraft.rhea.storage.SstColumnFamily) Map(java.util.Map) After(org.junit.After) KVStoreSnapshotFileFactory(com.alipay.sofa.jraft.rhea.storage.KVStoreSnapshotFileFactory) Method(java.lang.reflect.Method) Path(java.nio.file.Path) DistributedLock(com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock) EnumMap(java.util.EnumMap) KVStoreClosure(com.alipay.sofa.jraft.rhea.storage.KVStoreClosure) RawKVStore(com.alipay.sofa.jraft.rhea.storage.RawKVStore) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) KVOperation(com.alipay.sofa.jraft.rhea.storage.KVOperation) Assert.assertFalse(org.junit.Assert.assertFalse) RocksDBOptions(com.alipay.sofa.jraft.rhea.options.RocksDBOptions) TestClosure(com.alipay.sofa.jraft.rhea.storage.TestClosure) KVState(com.alipay.sofa.jraft.rhea.storage.KVState) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) KVStoreSnapshotFile(com.alipay.sofa.jraft.rhea.storage.KVStoreSnapshotFile) KeyValueTool.makeKey(com.alipay.sofa.jraft.rhea.KeyValueTool.makeKey) Closure(com.alipay.sofa.jraft.Closure) KVIterator(com.alipay.sofa.jraft.rhea.storage.KVIterator) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) ExecutorServiceHelper(com.alipay.sofa.jraft.util.ExecutorServiceHelper) LocalLock(com.alipay.sofa.jraft.rhea.storage.LocalLock) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) KVStoreAccessHelper(com.alipay.sofa.jraft.rhea.storage.KVStoreAccessHelper) TestSnapshotWriter(com.alipay.sofa.jraft.rhea.storage.TestSnapshotWriter) RocksRawKVStore(com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Status(com.alipay.sofa.jraft.Status) ByteArray(com.alipay.sofa.jraft.rhea.util.ByteArray) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Assert.assertNull(org.junit.Assert.assertNull) Paths(java.nio.file.Paths) LocalFileMeta(com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta) Sequence(com.alipay.sofa.jraft.rhea.storage.Sequence) KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry) KVStateOutputList(com.alipay.sofa.jraft.rhea.storage.KVStateOutputList) KeyValueTool.makeValue(com.alipay.sofa.jraft.rhea.KeyValueTool.makeValue) Assert.assertEquals(org.junit.Assert.assertEquals) BytesUtil(com.alipay.sofa.jraft.util.BytesUtil) KVStoreClosure(com.alipay.sofa.jraft.rhea.storage.KVStoreClosure) TestClosure(com.alipay.sofa.jraft.rhea.storage.TestClosure) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) Closure(com.alipay.sofa.jraft.Closure) RocksRawKVStore(com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore) TestSnapshotReader(com.alipay.sofa.jraft.rhea.storage.TestSnapshotReader) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutorService(java.util.concurrent.ExecutorService) Region(com.alipay.sofa.jraft.rhea.metadata.Region) KVStoreSnapshotFile(com.alipay.sofa.jraft.rhea.storage.KVStoreSnapshotFile) LocalFileMeta(com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta) KVStoreSnapshotFile(com.alipay.sofa.jraft.rhea.storage.KVStoreSnapshotFile) File(java.io.File) TestSnapshotWriter(com.alipay.sofa.jraft.rhea.storage.TestSnapshotWriter)

Example 9 with RocksRawKVStore

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);
}
Also used : Status(com.alipay.sofa.jraft.Status) RawKVStore(com.alipay.sofa.jraft.rhea.storage.RawKVStore) RocksRawKVStore(com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) KVStoreClosure(com.alipay.sofa.jraft.rhea.storage.KVStoreClosure) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) Test(org.junit.Test)

Example 10 with RocksRawKVStore

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;
}
Also used : RocksRawKVStore(com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore) File(java.io.File) RocksDBOptions(com.alipay.sofa.jraft.rhea.options.RocksDBOptions)

Aggregations

RocksRawKVStore (com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore)10 BaseKVStoreClosure (com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure)6 KVStoreClosure (com.alipay.sofa.jraft.rhea.storage.KVStoreClosure)6 RawKVStore (com.alipay.sofa.jraft.rhea.storage.RawKVStore)6 File (java.io.File)6 Test (org.junit.Test)6 RocksDBOptions (com.alipay.sofa.jraft.rhea.options.RocksDBOptions)5 Status (com.alipay.sofa.jraft.Status)4 LocalFileMeta (com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta)3 KVEntry (com.alipay.sofa.jraft.rhea.storage.KVEntry)3 KVStateOutputList (com.alipay.sofa.jraft.rhea.storage.KVStateOutputList)3 Sequence (com.alipay.sofa.jraft.rhea.storage.Sequence)3 TestClosure (com.alipay.sofa.jraft.rhea.storage.TestClosure)3 Closure (com.alipay.sofa.jraft.Closure)2 KeyValueTool.makeKey (com.alipay.sofa.jraft.rhea.KeyValueTool.makeKey)2 KeyValueTool.makeValue (com.alipay.sofa.jraft.rhea.KeyValueTool.makeValue)2 StoreEngineHelper (com.alipay.sofa.jraft.rhea.StoreEngineHelper)2 Region (com.alipay.sofa.jraft.rhea.metadata.Region)2 KVIterator (com.alipay.sofa.jraft.rhea.storage.KVIterator)2 KVOperation (com.alipay.sofa.jraft.rhea.storage.KVOperation)2