Search in sources :

Example 6 with Sequence

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

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

the class DefaultRheaKVStore method internalGetSequence.

private void internalGetSequence(final byte[] seqKey, final int step, final CompletableFuture<Sequence> future, final int retriesLeft, final Errors lastCause) {
    final Region region = this.pdClient.findRegionByKey(seqKey, ErrorsHelper.isInvalidEpoch(lastCause));
    final RegionEngine regionEngine = getRegionEngine(region.getId(), true);
    final RetryRunner retryRunner = retryCause -> internalGetSequence(seqKey, step, future, retriesLeft - 1, retryCause);
    final FailoverClosure<Sequence> closure = new FailoverClosureImpl<>(future, retriesLeft, retryRunner);
    if (regionEngine != null) {
        if (ensureOnValidEpoch(region, regionEngine, closure)) {
            getRawKVStore(regionEngine).getSequence(seqKey, step, closure);
        }
    } else {
        final GetSequenceRequest request = new GetSequenceRequest();
        request.setSeqKey(seqKey);
        request.setStep(step);
        request.setRegionId(region.getId());
        request.setRegionEpoch(region.getRegionEpoch());
        this.rheaKVRpcService.callAsyncWithRpc(request, closure, lastCause);
    }
}
Also used : NamedThreadFactory(com.alipay.sofa.jraft.rhea.util.concurrent.NamedThreadFactory) PlacementDriverClient(com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient) BatchingOptions(com.alipay.sofa.jraft.rhea.options.BatchingOptions) LoggerFactory(org.slf4j.LoggerFactory) FailoverClosure(com.alipay.sofa.jraft.rhea.client.failover.FailoverClosure) ExtSerializerSupports(com.alipay.sofa.jraft.rhea.rpc.ExtSerializerSupports) Region(com.alipay.sofa.jraft.rhea.metadata.Region) Lists(com.alipay.sofa.jraft.rhea.util.Lists) ListFailoverFuture(com.alipay.sofa.jraft.rhea.client.failover.impl.ListFailoverFuture) DescriberManager(com.alipay.sofa.jraft.rhea.DescriberManager) Map(java.util.Map) Constants(com.alipay.sofa.jraft.rhea.util.Constants) ApiExceptionHelper(com.alipay.sofa.jraft.rhea.errors.ApiExceptionHelper) ScanRequest(com.alipay.sofa.jraft.rhea.cmd.store.ScanRequest) Endpoint(com.alipay.sofa.jraft.util.Endpoint) MergeRequest(com.alipay.sofa.jraft.rhea.cmd.store.MergeRequest) ThreadFactory(java.util.concurrent.ThreadFactory) DeleteRequest(com.alipay.sofa.jraft.rhea.cmd.store.DeleteRequest) WaitStrategyType(com.alipay.sofa.jraft.rhea.util.concurrent.disruptor.WaitStrategyType) CASEntry(com.alipay.sofa.jraft.rhea.storage.CASEntry) DistributedLock(com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock) LogExceptionHandler(com.alipay.sofa.jraft.util.LogExceptionHandler) CASAllRequest(com.alipay.sofa.jraft.rhea.cmd.store.CASAllRequest) TaskDispatcher(com.alipay.sofa.jraft.rhea.util.concurrent.disruptor.TaskDispatcher) PeerId(com.alipay.sofa.jraft.entity.PeerId) RouteTable(com.alipay.sofa.jraft.RouteTable) PlacementDriverOptions(com.alipay.sofa.jraft.rhea.options.PlacementDriverOptions) KVStoreClosure(com.alipay.sofa.jraft.rhea.storage.KVStoreClosure) KVMetrics(com.alipay.sofa.jraft.rhea.metrics.KVMetrics) CompareAndPutRequest(com.alipay.sofa.jraft.rhea.cmd.store.CompareAndPutRequest) PutRequest(com.alipay.sofa.jraft.rhea.cmd.store.PutRequest) FakePlacementDriverClient(com.alipay.sofa.jraft.rhea.client.pd.FakePlacementDriverClient) ContainsKeyRequest(com.alipay.sofa.jraft.rhea.cmd.store.ContainsKeyRequest) RawKVStore(com.alipay.sofa.jraft.rhea.storage.RawKVStore) DeleteRangeRequest(com.alipay.sofa.jraft.rhea.cmd.store.DeleteRangeRequest) Strings(com.alipay.sofa.jraft.rhea.util.Strings) List(java.util.List) ResetSequenceRequest(com.alipay.sofa.jraft.rhea.cmd.store.ResetSequenceRequest) RegionEngine(com.alipay.sofa.jraft.rhea.RegionEngine) BoolFailoverFuture(com.alipay.sofa.jraft.rhea.client.failover.impl.BoolFailoverFuture) JRaftHelper(com.alipay.sofa.jraft.rhea.JRaftHelper) ZipStrategyManager(com.alipay.sofa.jraft.rhea.storage.zip.ZipStrategyManager) RemotePlacementDriverClient(com.alipay.sofa.jraft.rhea.client.pd.RemotePlacementDriverClient) FailoverClosureImpl(com.alipay.sofa.jraft.rhea.client.failover.impl.FailoverClosureImpl) RheaKVStoreOptions(com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions) Requires(com.alipay.sofa.jraft.util.Requires) Histogram(com.codahale.metrics.Histogram) RheaRuntimeException(com.alipay.sofa.jraft.rhea.errors.RheaRuntimeException) BatchPutRequest(com.alipay.sofa.jraft.rhea.cmd.store.BatchPutRequest) StackTraceUtil(com.alipay.sofa.jraft.rhea.util.StackTraceUtil) CompletableFuture(java.util.concurrent.CompletableFuture) FollowerStateListener(com.alipay.sofa.jraft.rhea.FollowerStateListener) Utils(com.alipay.sofa.jraft.util.Utils) ErrorsHelper(com.alipay.sofa.jraft.rhea.errors.ErrorsHelper) BatchDeleteRequest(com.alipay.sofa.jraft.rhea.cmd.store.BatchDeleteRequest) StateListenerContainer(com.alipay.sofa.jraft.rhea.StateListenerContainer) RetryCallable(com.alipay.sofa.jraft.rhea.client.failover.RetryCallable) GetRequest(com.alipay.sofa.jraft.rhea.cmd.store.GetRequest) Errors(com.alipay.sofa.jraft.rhea.errors.Errors) StateListener(com.alipay.sofa.jraft.rhea.StateListener) GetAndPutRequest(com.alipay.sofa.jraft.rhea.cmd.store.GetAndPutRequest) KeyLockRequest(com.alipay.sofa.jraft.rhea.cmd.store.KeyLockRequest) KeyUnlockRequest(com.alipay.sofa.jraft.rhea.cmd.store.KeyUnlockRequest) MapFailoverFuture(com.alipay.sofa.jraft.rhea.client.failover.impl.MapFailoverFuture) KVMetricNames(com.alipay.sofa.jraft.rhea.metrics.KVMetricNames) KVIterator(com.alipay.sofa.jraft.rhea.storage.KVIterator) NodeExecutor(com.alipay.sofa.jraft.rhea.storage.NodeExecutor) Dispatcher(com.alipay.sofa.jraft.rhea.util.concurrent.disruptor.Dispatcher) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) StoreEngine(com.alipay.sofa.jraft.rhea.StoreEngine) MultiGetRequest(com.alipay.sofa.jraft.rhea.cmd.store.MultiGetRequest) EventHandler(com.lmax.disruptor.EventHandler) PutIfAbsentRequest(com.alipay.sofa.jraft.rhea.cmd.store.PutIfAbsentRequest) Logger(org.slf4j.Logger) RetryRunner(com.alipay.sofa.jraft.rhea.client.failover.RetryRunner) GetSequenceRequest(com.alipay.sofa.jraft.rhea.cmd.store.GetSequenceRequest) RingBuffer(com.lmax.disruptor.RingBuffer) AffinityNamedThreadFactory(com.alipay.sofa.jraft.rhea.util.concurrent.AffinityNamedThreadFactory) ListRetryCallable(com.alipay.sofa.jraft.rhea.client.failover.ListRetryCallable) Status(com.alipay.sofa.jraft.Status) ByteArray(com.alipay.sofa.jraft.rhea.util.ByteArray) NodeExecuteRequest(com.alipay.sofa.jraft.rhea.cmd.store.NodeExecuteRequest) TimeUnit(java.util.concurrent.TimeUnit) StoreEngineOptions(com.alipay.sofa.jraft.rhea.options.StoreEngineOptions) Sequence(com.alipay.sofa.jraft.rhea.storage.Sequence) RpcOptions(com.alipay.sofa.jraft.rhea.options.RpcOptions) KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry) Collections(java.util.Collections) LeaderStateListener(com.alipay.sofa.jraft.rhea.LeaderStateListener) EventFactory(com.lmax.disruptor.EventFactory) Disruptor(com.lmax.disruptor.dsl.Disruptor) BytesUtil(com.alipay.sofa.jraft.util.BytesUtil) FailoverClosureImpl(com.alipay.sofa.jraft.rhea.client.failover.impl.FailoverClosureImpl) GetSequenceRequest(com.alipay.sofa.jraft.rhea.cmd.store.GetSequenceRequest) Region(com.alipay.sofa.jraft.rhea.metadata.Region) RegionEngine(com.alipay.sofa.jraft.rhea.RegionEngine) Sequence(com.alipay.sofa.jraft.rhea.storage.Sequence) RetryRunner(com.alipay.sofa.jraft.rhea.client.failover.RetryRunner)

Example 8 with Sequence

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

Sequence (com.alipay.sofa.jraft.rhea.storage.Sequence)8 Status (com.alipay.sofa.jraft.Status)6 BaseKVStoreClosure (com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure)5 KVStoreClosure (com.alipay.sofa.jraft.rhea.storage.KVStoreClosure)5 RawKVStore (com.alipay.sofa.jraft.rhea.storage.RawKVStore)5 Closure (com.alipay.sofa.jraft.Closure)2 LocalFileMeta (com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta)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 KVEntry (com.alipay.sofa.jraft.rhea.storage.KVEntry)2 KVIterator (com.alipay.sofa.jraft.rhea.storage.KVIterator)2 KVStoreSnapshotFile (com.alipay.sofa.jraft.rhea.storage.KVStoreSnapshotFile)2 KVStoreSnapshotFileFactory (com.alipay.sofa.jraft.rhea.storage.KVStoreSnapshotFileFactory)2 LocalLock (com.alipay.sofa.jraft.rhea.storage.LocalLock)2 SyncKVStore (com.alipay.sofa.jraft.rhea.storage.SyncKVStore)2 TestClosure (com.alipay.sofa.jraft.rhea.storage.TestClosure)2 TestSnapshotReader (com.alipay.sofa.jraft.rhea.storage.TestSnapshotReader)2 TestSnapshotWriter (com.alipay.sofa.jraft.rhea.storage.TestSnapshotWriter)2