Search in sources :

Example 16 with BaseKVStoreClosure

use of com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure in project sofa-jraft by sofastack.

the class InstructionProcessor method processSplit.

private boolean processSplit(final Instruction instruction) {
    try {
        final Instruction.RangeSplit rangeSplit = instruction.getRangeSplit();
        if (rangeSplit == null) {
            return false;
        }
        final Long newRegionId = rangeSplit.getNewRegionId();
        if (newRegionId == null) {
            LOG.error("RangeSplit#newRegionId must not be null, {}.", instruction);
            return false;
        }
        final Region region = instruction.getRegion();
        final long regionId = region.getId();
        final RegionEngine engine = this.storeEngine.getRegionEngine(regionId);
        if (engine == null) {
            LOG.error("Could not found regionEngine, {}.", instruction);
            return false;
        }
        if (!region.equals(engine.getRegion())) {
            LOG.warn("Instruction [{}] is out of date.", instruction);
            return false;
        }
        final CompletableFuture<Status> future = new CompletableFuture<>();
        this.storeEngine.applySplit(regionId, newRegionId, new BaseKVStoreClosure() {

            @Override
            public void run(Status status) {
                future.complete(status);
            }
        });
        final Status status = future.get(20, TimeUnit.SECONDS);
        final boolean ret = status.isOk();
        if (ret) {
            LOG.info("Range-split succeeded, instruction: {}.", instruction);
        } else {
            LOG.warn("Range-split failed: {}, instruction: {}.", status, instruction);
        }
        return ret;
    } catch (final Throwable t) {
        LOG.error("Caught an exception on #processSplit: {}.", StackTraceUtil.stackTrace(t));
        return false;
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) Instruction(com.alipay.sofa.jraft.rhea.metadata.Instruction) CompletableFuture(java.util.concurrent.CompletableFuture) Region(com.alipay.sofa.jraft.rhea.metadata.Region) RegionEngine(com.alipay.sofa.jraft.rhea.RegionEngine)

Example 17 with BaseKVStoreClosure

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

Example 18 with BaseKVStoreClosure

use of com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure in project sofa-jraft by sofastack.

the class MemoryKVStoreTest method compareAndPutTest.

/**
 * Test method: {@link MemoryRawKVStore#compareAndPut(byte[], byte[], byte[], KVStoreClosure)}
 */
@Test
public void compareAndPutTest() {
    final byte[] key = makeKey("put_test");
    byte[] value = makeValue("put_test_value");
    this.kvStore.put(key, value, null);
    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) MemoryRawKVStore(com.alipay.sofa.jraft.rhea.storage.MemoryRawKVStore) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) KVStoreClosure(com.alipay.sofa.jraft.rhea.storage.KVStoreClosure) Test(org.junit.Test)

Example 19 with BaseKVStoreClosure

use of com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure in project sofa-jraft by sofastack.

the class RocksKVStoreTest method batchCompareAndPutTest.

/**
 * Test method: {@link RocksRawKVStore#batchCompareAndPut(KVStateOutputList)}
 */
@Test
public void batchCompareAndPutTest() {
    final KVStateOutputList kvStates = KVStateOutputList.newInstance();
    final int batchWriteSize = RocksRawKVStore.MAX_BATCH_WRITE_SIZE + 1;
    for (int i = 1; i <= batchWriteSize; i++) {
        final byte[] key = makeKey("put_test" + i);
        final byte[] value = makeValue("put_test_value" + i);
        kvStates.add(KVState.of(KVOperation.createPut(key, value), null));
    }
    this.kvStore.batchPut(kvStates);
    kvStates.clear();
    for (int i = 1; i <= batchWriteSize; i++) {
        final byte[] key = makeKey("put_test" + i);
        final byte[] value = makeValue("put_test_value" + i);
        final byte[] update = makeValue("put_test_update" + i);
        KVStoreClosure kvStoreClosure = new BaseKVStoreClosure() {

            @Override
            public void run(Status status) {
                assertEquals(status, Status.OK());
            }
        };
        kvStates.add(KVState.of(KVOperation.createCompareAndPut(key, update, value), kvStoreClosure));
    }
    this.kvStore.batchCompareAndPut(kvStates);
    kvStates.forEach(kvState -> assertEquals(kvState.getDone().getData(), Boolean.FALSE));
    kvStates.clear();
    for (int i = 1; i <= batchWriteSize; i++) {
        final byte[] key = makeKey("put_test" + i);
        final byte[] value = makeValue("put_test_value" + i);
        final byte[] update = makeValue("put_test_update" + i);
        KVStoreClosure kvStoreClosure = new BaseKVStoreClosure() {

            @Override
            public void run(Status status) {
                assertEquals(status, Status.OK());
            }
        };
        kvStates.add(KVState.of(KVOperation.createCompareAndPut(key, value, update), kvStoreClosure));
    }
    this.kvStore.batchCompareAndPut(kvStates);
    kvStates.forEach(kvState -> assertEquals(kvState.getDone().getData(), Boolean.TRUE));
}
Also used : Status(com.alipay.sofa.jraft.Status) KVStateOutputList(com.alipay.sofa.jraft.rhea.storage.KVStateOutputList) 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 20 with BaseKVStoreClosure

use of com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure 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)

Aggregations

Status (com.alipay.sofa.jraft.Status)29 BaseKVStoreClosure (com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure)29 KVStoreClosure (com.alipay.sofa.jraft.rhea.storage.KVStoreClosure)8 Test (org.junit.Test)8 RawKVStore (com.alipay.sofa.jraft.rhea.storage.RawKVStore)4 Sequence (com.alipay.sofa.jraft.rhea.storage.Sequence)3 BatchDeleteResponse (com.alipay.sofa.jraft.rhea.cmd.store.BatchDeleteResponse)2 BatchPutResponse (com.alipay.sofa.jraft.rhea.cmd.store.BatchPutResponse)2 CompareAndPutResponse (com.alipay.sofa.jraft.rhea.cmd.store.CompareAndPutResponse)2 GetAndPutResponse (com.alipay.sofa.jraft.rhea.cmd.store.GetAndPutResponse)2 MultiGetResponse (com.alipay.sofa.jraft.rhea.cmd.store.MultiGetResponse)2 KVEntry (com.alipay.sofa.jraft.rhea.storage.KVEntry)2 KVStateOutputList (com.alipay.sofa.jraft.rhea.storage.KVStateOutputList)2 MemoryRawKVStore (com.alipay.sofa.jraft.rhea.storage.MemoryRawKVStore)2 RocksRawKVStore (com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore)2 TestClosure (com.alipay.sofa.jraft.rhea.storage.TestClosure)2 DistributedLock (com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock)2 RegionEngine (com.alipay.sofa.jraft.rhea.RegionEngine)1 CASAllResponse (com.alipay.sofa.jraft.rhea.cmd.store.CASAllResponse)1 ContainsKeyResponse (com.alipay.sofa.jraft.rhea.cmd.store.ContainsKeyResponse)1