use of com.alipay.sofa.jraft.rhea.errors.Errors in project sofa-jraft by sofastack.
the class MapFailoverFutureTest method multiGet.
private FutureGroup<Map<Integer, Integer>> multiGet(final int start, final int end, final int retriesLeft, final Throwable lastCause) {
final Errors lastError = lastCause == null ? null : Errors.forException(lastCause);
final List<CompletableFuture<Map<Integer, Integer>>> futureList = Lists.newArrayList();
final int mid = ((end - start) / 2) + start;
for (int i = 0; i < 2; i++) {
final int subStart = i == 0 ? start : mid;
final int subEnd = i == 0 ? mid : end;
if (subEnd - subStart > 0) {
final RetryCallable<Map<Integer, Integer>> retryCallable = retryCause -> multiGet(subStart, subEnd, retriesLeft - 1, retryCause);
final MapFailoverFuture<Integer, Integer> future = new MapFailoverFuture<>(retriesLeft, retryCallable);
regionMultiGet(subStart, subEnd, future, retriesLeft, lastError);
futureList.add(future);
}
}
return new FutureGroup<>(futureList);
}
use of com.alipay.sofa.jraft.rhea.errors.Errors in project sofa-jraft by sofastack.
the class MapFailoverFutureTest method regionMultiGet.
@SuppressWarnings("unused")
private void regionMultiGet(final int start, final int end, final CompletableFuture<Map<Integer, Integer>> future, final int retriesLeft, final Errors lastCause) {
System.out.println("start=" + start + ", end=" + end);
final RetryRunner retryRunner = retryCause -> regionMultiGet(start, end, future, retriesLeft - 1, retryCause);
final FailoverClosure<Map<Integer, Integer>> closure = new FailoverClosureImpl<>(future, false, retriesLeft, retryRunner);
if (getCounter(start, end).incrementAndGet() < 2) {
System.err.println("fail: " + start + " - " + end);
closure.failure(Errors.INVALID_REGION_MEMBERSHIP);
} else {
final Map<Integer, Integer> result = Maps.newHashMap();
for (int i = start; i < end; i++) {
result.put(i, i);
}
closure.success(result);
}
}
use of com.alipay.sofa.jraft.rhea.errors.Errors in project sofa-jraft by sofastack.
the class DefaultRheaKVStore method internalDelete.
private FutureGroup<Boolean> internalDelete(final List<byte[]> keys, final int retriesLeft, final Throwable lastCause) {
final Map<Region, List<byte[]>> regionMap = this.pdClient.findRegionsByKeys(keys, ApiExceptionHelper.isInvalidEpoch(lastCause));
final List<CompletableFuture<Boolean>> futures = Lists.newArrayListWithCapacity(regionMap.size());
final Errors lastError = lastCause == null ? null : Errors.forException(lastCause);
for (final Map.Entry<Region, List<byte[]>> entry : regionMap.entrySet()) {
final Region region = entry.getKey();
final List<byte[]> subKeys = entry.getValue();
final RetryCallable<Boolean> retryCallable = retryCause -> internalDelete(subKeys, retriesLeft - 1, retryCause);
final BoolFailoverFuture future = new BoolFailoverFuture(retriesLeft, retryCallable);
internalRegionDelete(region, subKeys, future, retriesLeft, lastError);
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 internalScan.
private FutureGroup<List<KVEntry>> internalScan(final byte[] startKey, final byte[] endKey, final boolean readOnlySafe, final boolean returnValue, final int retriesLeft, final Throwable lastCause) {
Requires.requireNonNull(startKey, "startKey");
final List<Region> regionList = this.pdClient.findRegionsByKeyRange(startKey, endKey, ApiExceptionHelper.isInvalidEpoch(lastCause));
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[] regionStartKey = region.getStartKey();
final byte[] regionEndKey = region.getEndKey();
final byte[] subStartKey = regionStartKey == null ? startKey : BytesUtil.max(regionStartKey, startKey);
final byte[] subEndKey = regionEndKey == null ? endKey : (endKey == null ? regionEndKey : BytesUtil.min(regionEndKey, endKey));
final ListRetryCallable<KVEntry> retryCallable = retryCause -> internalScan(subStartKey, subEndKey, readOnlySafe, returnValue, retriesLeft - 1, retryCause);
final ListFailoverFuture<KVEntry> future = new ListFailoverFuture<>(retriesLeft, retryCallable);
internalRegionScan(region, subStartKey, subEndKey, false, 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 internalRegionDeleteRange.
private void internalRegionDeleteRange(final Region region, final byte[] subStartKey, final byte[] subEndKey, final CompletableFuture<Boolean> future, final int retriesLeft, final Errors lastCause) {
final RegionEngine regionEngine = getRegionEngine(region.getId(), true);
final RetryRunner retryRunner = retryCause -> internalRegionDeleteRange(region, subStartKey, subEndKey, future, retriesLeft - 1, retryCause);
final FailoverClosure<Boolean> closure = new FailoverClosureImpl<>(future, false, retriesLeft, retryRunner);
if (regionEngine != null) {
if (ensureOnValidEpoch(region, regionEngine, closure)) {
getRawKVStore(regionEngine).deleteRange(subStartKey, subEndKey, closure);
}
} else {
final DeleteRangeRequest request = new DeleteRangeRequest();
request.setStartKey(subStartKey);
request.setEndKey(subEndKey);
request.setRegionId(region.getId());
request.setRegionEpoch(region.getRegionEpoch());
this.rheaKVRpcService.callAsyncWithRpc(request, closure, lastCause);
}
}
Aggregations