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);
}
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);
}
}
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);
}
}
Aggregations