Search in sources :

Example 21 with BaseKVStoreClosure

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

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

the class DefaultRegionKVService method handleDeleteRangeRequest.

@Override
public void handleDeleteRangeRequest(final DeleteRangeRequest request, final RequestProcessClosure<BaseRequest, BaseResponse<?>> closure) {
    final DeleteRangeResponse response = new DeleteRangeResponse();
    response.setRegionId(getRegionId());
    response.setRegionEpoch(getRegionEpoch());
    try {
        KVParameterRequires.requireSameEpoch(request, getRegionEpoch());
        final byte[] startKey = KVParameterRequires.requireNonNull(request.getStartKey(), "deleteRange.startKey");
        final byte[] endKey = KVParameterRequires.requireNonNull(request.getEndKey(), "deleteRange.endKey");
        this.rawKVStore.deleteRange(startKey, endKey, new BaseKVStoreClosure() {

            @Override
            public void run(final Status status) {
                if (status.isOk()) {
                    response.setValue((Boolean) getData());
                } else {
                    setFailure(request, response, status, getError());
                }
                closure.sendResponse(response);
            }
        });
    } catch (final Throwable t) {
        LOG.error("Failed to handle: {}, {}.", request, StackTraceUtil.stackTrace(t));
        response.setError(Errors.forException(t));
        closure.sendResponse(response);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) DeleteRangeResponse(com.alipay.sofa.jraft.rhea.cmd.store.DeleteRangeResponse)

Example 23 with BaseKVStoreClosure

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

the class DefaultRegionKVService method handleNodeExecuteRequest.

@Override
public void handleNodeExecuteRequest(final NodeExecuteRequest request, final RequestProcessClosure<BaseRequest, BaseResponse<?>> closure) {
    final NodeExecuteResponse response = new NodeExecuteResponse();
    response.setRegionId(getRegionId());
    response.setRegionEpoch(getRegionEpoch());
    try {
        KVParameterRequires.requireSameEpoch(request, getRegionEpoch());
        final NodeExecutor executor = KVParameterRequires.requireNonNull(request.getNodeExecutor(), "node.executor");
        this.rawKVStore.execute(executor, true, new BaseKVStoreClosure() {

            @Override
            public void run(final Status status) {
                if (status.isOk()) {
                    response.setValue((Boolean) getData());
                } else {
                    setFailure(request, response, status, getError());
                }
                closure.sendResponse(response);
            }
        });
    } catch (final Throwable t) {
        LOG.error("Failed to handle: {}, {}.", request, StackTraceUtil.stackTrace(t));
        response.setError(Errors.forException(t));
        closure.sendResponse(response);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) NodeExecuteResponse(com.alipay.sofa.jraft.rhea.cmd.store.NodeExecuteResponse) NodeExecutor(com.alipay.sofa.jraft.rhea.storage.NodeExecutor)

Example 24 with BaseKVStoreClosure

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

the class DefaultRegionKVService method handleCompareAndPutAll.

@Override
public void handleCompareAndPutAll(final CASAllRequest request, final RequestProcessClosure<BaseRequest, BaseResponse<?>> closure) {
    final CASAllResponse response = new CASAllResponse();
    response.setRegionId(getRegionId());
    response.setRegionEpoch(getRegionEpoch());
    try {
        KVParameterRequires.requireSameEpoch(request, getRegionEpoch());
        final List<CASEntry> casEntries = KVParameterRequires.requireNonEmpty(request.getCasEntries(), "casAll.casEntries");
        this.rawKVStore.compareAndPutAll(casEntries, new BaseKVStoreClosure() {

            @Override
            public void run(final Status status) {
                if (status.isOk()) {
                    response.setValue((Boolean) getData());
                } else {
                    setFailure(request, response, status, getError());
                }
                closure.sendResponse(response);
            }
        });
    } catch (final Throwable t) {
        LOG.error("Failed to handle: {}, {}.", request, StackTraceUtil.stackTrace(t));
        response.setError(Errors.forException(t));
        closure.sendResponse(response);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) CASAllResponse(com.alipay.sofa.jraft.rhea.cmd.store.CASAllResponse) CASEntry(com.alipay.sofa.jraft.rhea.storage.CASEntry)

Example 25 with BaseKVStoreClosure

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

the class DefaultRegionKVService method handleGetSequence.

@Override
public void handleGetSequence(final GetSequenceRequest request, final RequestProcessClosure<BaseRequest, BaseResponse<?>> closure) {
    final GetSequenceResponse response = new GetSequenceResponse();
    response.setRegionId(getRegionId());
    response.setRegionEpoch(getRegionEpoch());
    try {
        KVParameterRequires.requireSameEpoch(request, getRegionEpoch());
        final byte[] seqKey = KVParameterRequires.requireNonNull(request.getSeqKey(), "sequence.seqKey");
        final int step = KVParameterRequires.requireNonNegative(request.getStep(), "sequence.step");
        this.rawKVStore.getSequence(seqKey, step, new BaseKVStoreClosure() {

            @Override
            public void run(final Status status) {
                if (status.isOk()) {
                    response.setValue((Sequence) getData());
                } else {
                    setFailure(request, response, status, getError());
                }
                closure.sendResponse(response);
            }
        });
    } catch (final Throwable t) {
        LOG.error("Failed to handle: {}, {}.", request, StackTraceUtil.stackTrace(t));
        response.setError(Errors.forException(t));
        closure.sendResponse(response);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) GetSequenceResponse(com.alipay.sofa.jraft.rhea.cmd.store.GetSequenceResponse) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) Sequence(com.alipay.sofa.jraft.rhea.storage.Sequence)

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