use of com.alipay.sofa.jraft.rhea.storage.Sequence in project sofa-jraft by sofastack.
the class RocksKVStoreTest method multiGroupSnapshotTest.
public void multiGroupSnapshotTest() throws Exception {
final File backupDir = new File("multi-backup");
if (backupDir.exists()) {
FileUtils.deleteDirectory(backupDir);
}
final List<Region> regions = Lists.newArrayList();
regions.add(new Region(1, makeKey("0"), makeKey("1"), null, null));
regions.add(new Region(2, makeKey("1"), makeKey("2"), null, null));
regions.add(new Region(3, makeKey("2"), makeKey("3"), null, null));
regions.add(new Region(4, makeKey("3"), makeKey("4"), null, null));
regions.add(new Region(5, makeKey("4"), makeKey("5"), null, null));
for (int i = 0; i < 5; i++) {
final String v = String.valueOf(i);
this.kvStore.put(makeKey(v), makeValue(v), null);
}
for (int i = 0; i < 5; i++) {
this.kvStore.getSequence(makeKey(i + "_seq_test"), 10, null);
}
KVStoreSnapshotFile kvStoreSnapshotFile = KVStoreSnapshotFileFactory.getKVStoreSnapshotFile(this.kvStore);
final ExecutorService snapshotPool = StoreEngineHelper.createSnapshotExecutor(1, 5);
final List<TestSnapshotWriter> writers = Lists.newArrayList();
for (int i = 0; i < 4; i++) {
final Path p = Paths.get(backupDir.getAbsolutePath(), String.valueOf(i));
final TestSnapshotWriter snapshotWriter = new TestSnapshotWriter(p.toString());
writers.add(snapshotWriter);
final CountDownLatch latch = new CountDownLatch(1);
final Closure done = status -> {
assertTrue(status.isOk());
latch.countDown();
};
kvStoreSnapshotFile.save(snapshotWriter, regions.get(i), done, snapshotPool);
latch.await();
final LocalFileMeta meta = (LocalFileMeta) snapshotWriter.getFileMeta(SNAPSHOT_ARCHIVE);
assertNotNull(meta);
}
this.kvStore.shutdown();
FileUtils.deleteDirectory(new File(this.tempPath));
FileUtils.forceMkdir(new File(this.tempPath));
this.kvStore = new RocksRawKVStore();
this.kvStore.init(this.dbOptions);
kvStoreSnapshotFile = KVStoreSnapshotFileFactory.getKVStoreSnapshotFile(this.kvStore);
for (int i = 0; i < 4; i++) {
final Path p = Paths.get(backupDir.getAbsolutePath(), String.valueOf(i));
final TestSnapshotReader snapshotReader = new TestSnapshotReader(writers.get(i).metaTable, p.toString());
final boolean ret = kvStoreSnapshotFile.load(snapshotReader, regions.get(i));
assertTrue(ret);
}
for (int i = 0; i < 4; i++) {
final String v = String.valueOf(i);
final byte[] seqKey = makeKey(i + "_seq_test");
assertArrayEquals(makeValue(v), get(makeKey(v)));
final Sequence sequence = new SyncKVStore<Sequence>() {
@Override
public void execute(RawKVStore kvStore, KVStoreClosure closure) {
kvStore.getSequence(seqKey, 10, closure);
}
}.apply(this.kvStore);
assertEquals(10L, sequence.getStartValue());
assertEquals(20L, sequence.getEndValue());
}
assertNull(get(makeKey("5")));
final Sequence sequence = new SyncKVStore<Sequence>() {
@Override
public void execute(RawKVStore kvStore, KVStoreClosure closure) {
kvStore.getSequence(makeKey("4_seq_test"), 10, closure);
}
}.apply(this.kvStore);
assertEquals(0L, sequence.getStartValue());
FileUtils.deleteDirectory(backupDir);
ExecutorServiceHelper.shutdownAndAwaitTermination(snapshotPool);
}
use of com.alipay.sofa.jraft.rhea.storage.Sequence in project sofa-jraft by sofastack.
the class MemoryKVStoreTest method multiGroupSnapshotTest.
@Test
public void multiGroupSnapshotTest() throws Exception {
final File backupDir = new File("multi-backup");
if (backupDir.exists()) {
FileUtils.deleteDirectory(backupDir);
}
final List<Region> regions = Lists.newArrayList();
regions.add(new Region(1, makeKey("0"), makeKey("1"), null, null));
regions.add(new Region(2, makeKey("1"), makeKey("2"), null, null));
regions.add(new Region(3, makeKey("2"), makeKey("3"), null, null));
regions.add(new Region(4, makeKey("3"), makeKey("4"), null, null));
regions.add(new Region(5, makeKey("4"), makeKey("5"), null, null));
for (int i = 0; i < 5; i++) {
final String v = String.valueOf(i);
this.kvStore.put(makeKey(v), makeValue(v), null);
}
for (int i = 0; i < 5; i++) {
this.kvStore.getSequence(makeKey(i + "_seq_test"), 10, null);
}
KVStoreSnapshotFile kvStoreSnapshotFile = KVStoreSnapshotFileFactory.getKVStoreSnapshotFile(this.kvStore);
final ExecutorService snapshotPool = StoreEngineHelper.createSnapshotExecutor(1, 2);
final List<TestSnapshotWriter> writers = Lists.newArrayList();
for (int i = 0; i < 4; i++) {
final Path p = Paths.get(backupDir.getAbsolutePath(), String.valueOf(i));
final TestSnapshotWriter snapshotWriter = new TestSnapshotWriter(p.toString());
writers.add(snapshotWriter);
final CountDownLatch latch = new CountDownLatch(1);
final Closure done = status -> {
assertTrue(status.isOk());
latch.countDown();
};
kvStoreSnapshotFile.save(snapshotWriter, regions.get(i), done, snapshotPool);
latch.await();
final LocalFileMeta meta = (LocalFileMeta) snapshotWriter.getFileMeta(SNAPSHOT_ARCHIVE);
assertNotNull(meta);
}
this.kvStore.shutdown();
this.kvStore = new MemoryRawKVStore();
final MemoryDBOptions dbOpts = new MemoryDBOptions();
this.kvStore.init(dbOpts);
kvStoreSnapshotFile = KVStoreSnapshotFileFactory.getKVStoreSnapshotFile(this.kvStore);
for (int i = 0; i < 4; i++) {
final Path p = Paths.get(backupDir.getAbsolutePath(), String.valueOf(i));
final TestSnapshotReader snapshotReader = new TestSnapshotReader(writers.get(i).metaTable, p.toString());
final boolean ret = kvStoreSnapshotFile.load(snapshotReader, regions.get(i));
assertTrue(ret);
}
for (int i = 0; i < 4; i++) {
final String v = String.valueOf(i);
final byte[] seqKey = makeKey(i + "_seq_test");
assertArrayEquals(makeValue(v), get(makeKey(v)));
final Sequence sequence = new SyncKVStore<Sequence>() {
@Override
public void execute(RawKVStore kvStore, KVStoreClosure closure) {
kvStore.getSequence(seqKey, 10, closure);
}
}.apply(this.kvStore);
assertEquals(10L, sequence.getStartValue());
assertEquals(20L, sequence.getEndValue());
}
assertNull(get(makeKey("5")));
final Sequence sequence = new SyncKVStore<Sequence>() {
@Override
public void execute(RawKVStore kvStore, KVStoreClosure closure) {
kvStore.getSequence(makeKey("4_seq_test"), 10, closure);
}
}.apply(this.kvStore);
assertEquals(0L, sequence.getStartValue());
FileUtils.deleteDirectory(backupDir);
ExecutorServiceHelper.shutdownAndAwaitTermination(snapshotPool);
}
use of com.alipay.sofa.jraft.rhea.storage.Sequence in project sofa-jraft by sofastack.
the class MemoryKVStoreTest method getSequenceTest.
/**
* Test method: {@link MemoryRawKVStore#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.Sequence in project sofa-jraft by sofastack.
the class AbstractRheaKVStoreTest method getSequenceTest.
/**
* Test method: {@link RheaKVStore#getSequence(byte[], int)}
*/
private void getSequenceTest(RheaKVStore store) {
// regions: 1 -> [null, g), 2 -> [g, null)
byte[] seqKey = makeKey("seq_test");
checkRegion(store, seqKey, 2);
Sequence sequence = store.bGetSequence(seqKey, 199);
assertEquals(sequence.getStartValue(), 0);
assertEquals(sequence.getEndValue(), 199);
// get-only, do not update
final long latestVal = store.bGetLatestSequence(seqKey);
assertEquals(latestVal, 199);
sequence = store.bGetSequence(seqKey, 10);
assertEquals(sequence.getStartValue(), 199);
assertEquals(sequence.getEndValue(), 209);
sequence = store.bGetSequence(seqKey, 10);
assertEquals(sequence.getStartValue(), 209);
assertEquals(sequence.getEndValue(), 219);
assertNull(store.bGet(seqKey));
store.bResetSequence(seqKey);
sequence = store.bGetSequence(seqKey, 11);
assertEquals(sequence.getStartValue(), 0);
assertEquals(sequence.getEndValue(), 11);
}
use of com.alipay.sofa.jraft.rhea.storage.Sequence in project sofa-jraft by sofastack.
the class GetSequenceExample method getSequence.
public static void getSequence(final RheaKVStore rheaKVStore) {
final byte[] key = writeUtf8("sequence");
rheaKVStore.getSequence(key, 10);
// async
final CompletableFuture<Sequence> f1 = rheaKVStore.getSequence(key, 20);
final CompletableFuture<Sequence> f2 = rheaKVStore.getSequence("sequence", 30);
CompletableFuture.allOf(f1, f2).join();
LOG.info("Async getSequence result={}", f1.join());
LOG.info("Async getSequence result={}", f2.join());
final CompletableFuture<Boolean> f3 = rheaKVStore.resetSequence(key);
f3.join();
// sync
final Sequence b1 = rheaKVStore.bGetSequence(key, 40);
final Sequence b2 = rheaKVStore.bGetSequence("sequence", 50);
LOG.info("Sync getSequence result={}", b1);
LOG.info("Sync getSequence result={}", b2);
rheaKVStore.bResetSequence(key);
}
Aggregations