use of com.alipay.sofa.jraft.rhea.errors.Errors in project sofa-jraft by sofastack.
the class DefaultRheaKVStore method internalPutIfAbsent.
private void internalPutIfAbsent(final byte[] key, final byte[] value, final CompletableFuture<byte[]> future, final int retriesLeft, final Errors lastCause) {
final Region region = this.pdClient.findRegionByKey(key, ErrorsHelper.isInvalidEpoch(lastCause));
final RegionEngine regionEngine = getRegionEngine(region.getId(), true);
final RetryRunner retryRunner = retryCause -> internalPutIfAbsent(key, value, future, retriesLeft - 1, retryCause);
final FailoverClosure<byte[]> closure = new FailoverClosureImpl<>(future, retriesLeft, retryRunner);
if (regionEngine != null) {
if (ensureOnValidEpoch(region, regionEngine, closure)) {
getRawKVStore(regionEngine).putIfAbsent(key, value, closure);
}
} else {
final PutIfAbsentRequest request = new PutIfAbsentRequest();
request.setKey(key);
request.setValue(value);
request.setRegionId(region.getId());
request.setRegionEpoch(region.getRegionEpoch());
this.rheaKVRpcService.callAsyncWithRpc(request, closure, lastCause);
}
}
use of com.alipay.sofa.jraft.rhea.errors.Errors in project sofa-jraft by sofastack.
the class DefaultRheaKVStore method internalReverseScan.
private FutureGroup<List<KVEntry>> internalReverseScan(final byte[] startKey, final byte[] endKey, final boolean readOnlySafe, final boolean returnValue, final int retriesLeft, final Throwable lastCause) {
Requires.requireNonNull(endKey, "endKey");
final List<Region> regionList = this.pdClient.findRegionsByKeyRange(endKey, startKey, ApiExceptionHelper.isInvalidEpoch(lastCause));
Collections.reverse(regionList);
final List<CompletableFuture<List<KVEntry>>> futures = Lists.newArrayListWithCapacity(regionList.size());
final Errors lastError = lastCause == null ? null : Errors.forException(lastCause);
for (final Region region : regionList) {
final byte[] regionEndKey = region.getEndKey();
final byte[] regionStartKey = region.getStartKey();
final byte[] subStartKey = regionEndKey == null ? startKey : (startKey == null ? regionEndKey : BytesUtil.min(regionEndKey, startKey));
final byte[] subEndKey = regionStartKey == null ? endKey : BytesUtil.max(regionStartKey, endKey);
final ListRetryCallable<KVEntry> retryCallable = retryCause -> internalReverseScan(subStartKey, subEndKey, readOnlySafe, returnValue, retriesLeft - 1, retryCause);
final ListFailoverFuture<KVEntry> future = new ListFailoverFuture<>(retriesLeft, retryCallable);
internalRegionScan(region, subStartKey, subEndKey, true, readOnlySafe, returnValue, future, retriesLeft, lastError, this.onlyLeaderRead);
futures.add(future);
}
return new FutureGroup<>(futures);
}
use of com.alipay.sofa.jraft.rhea.errors.Errors in project sofa-jraft by sofastack.
the class DefaultRheaKVStore method internalSingleRegionScan.
private void internalSingleRegionScan(final byte[] startKey, final byte[] endKey, final int limit, final boolean readOnlySafe, final boolean returnValue, final CompletableFuture<List<KVEntry>> future, final int retriesLeft, final Errors lastCause, final boolean requireLeader) {
Requires.requireNonNull(startKey, "startKey");
final Region region = this.pdClient.findRegionByKey(startKey, ErrorsHelper.isInvalidEpoch(lastCause));
final byte[] regionEndKey = region.getEndKey();
final byte[] realEndKey = regionEndKey == null ? endKey : (endKey == null ? regionEndKey : BytesUtil.min(regionEndKey, endKey));
final RegionEngine regionEngine = getRegionEngine(region.getId(), requireLeader);
// require leader on retry
final RetryRunner retryRunner = retryCause -> internalSingleRegionScan(startKey, endKey, limit, readOnlySafe, returnValue, future, retriesLeft - 1, retryCause, true);
final FailoverClosure<List<KVEntry>> closure = new FailoverClosureImpl<>(future, retriesLeft, retryRunner);
if (regionEngine != null) {
if (ensureOnValidEpoch(region, regionEngine, closure)) {
getRawKVStore(regionEngine).scan(startKey, realEndKey, limit, readOnlySafe, returnValue, closure);
}
} else {
final ScanRequest request = new ScanRequest();
request.setStartKey(startKey);
request.setEndKey(realEndKey);
request.setLimit(limit);
request.setReadOnlySafe(readOnlySafe);
request.setReturnValue(returnValue);
request.setRegionId(region.getId());
request.setRegionEpoch(region.getRegionEpoch());
this.rheaKVRpcService.callAsyncWithRpc(request, closure, lastCause, requireLeader);
}
}
use of com.alipay.sofa.jraft.rhea.errors.Errors in project sofa-jraft by sofastack.
the class DefaultRheaKVStore method internalRegionDelete.
private void internalRegionDelete(final Region region, final List<byte[]> subKeys, final CompletableFuture<Boolean> future, final int retriesLeft, final Errors lastCause) {
final RegionEngine regionEngine = getRegionEngine(region.getId(), true);
final RetryRunner retryRunner = retryCause -> internalRegionDelete(region, subKeys, future, retriesLeft - 1, retryCause);
final FailoverClosure<Boolean> closure = new FailoverClosureImpl<>(future, false, retriesLeft, retryRunner);
if (regionEngine != null) {
if (ensureOnValidEpoch(region, regionEngine, closure)) {
final RawKVStore rawKVStore = getRawKVStore(regionEngine);
if (this.kvDispatcher == null) {
rawKVStore.delete(subKeys, closure);
} else {
this.kvDispatcher.execute(() -> rawKVStore.delete(subKeys, closure));
}
}
} else {
final BatchDeleteRequest request = new BatchDeleteRequest();
request.setKeys(subKeys);
request.setRegionId(region.getId());
request.setRegionEpoch(region.getRegionEpoch());
this.rheaKVRpcService.callAsyncWithRpc(request, closure, lastCause);
}
}
use of com.alipay.sofa.jraft.rhea.errors.Errors in project sofa-jraft by sofastack.
the class DefaultRheaKVStore method internalRegionPut.
private void internalRegionPut(final Region region, final List<KVEntry> subEntries, final CompletableFuture<Boolean> future, final int retriesLeft, final Errors lastCause) {
final RegionEngine regionEngine = getRegionEngine(region.getId(), true);
final RetryRunner retryRunner = retryCause -> internalRegionPut(region, subEntries, future, retriesLeft - 1, retryCause);
final FailoverClosure<Boolean> closure = new FailoverClosureImpl<>(future, false, retriesLeft, retryRunner);
if (regionEngine != null) {
if (ensureOnValidEpoch(region, regionEngine, closure)) {
final RawKVStore rawKVStore = getRawKVStore(regionEngine);
if (this.kvDispatcher == null) {
rawKVStore.put(subEntries, closure);
} else {
this.kvDispatcher.execute(() -> rawKVStore.put(subEntries, closure));
}
}
} else {
final BatchPutRequest request = new BatchPutRequest();
request.setKvEntries(subEntries);
request.setRegionId(region.getId());
request.setRegionEpoch(region.getRegionEpoch());
this.rheaKVRpcService.callAsyncWithRpc(request, closure, lastCause);
}
}
Aggregations