use of com.alipay.sofa.jraft.rhea.client.failover.impl.FailoverClosureImpl 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.client.failover.impl.FailoverClosureImpl 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);
}
}
use of com.alipay.sofa.jraft.rhea.client.failover.impl.FailoverClosureImpl 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.client.failover.impl.FailoverClosureImpl 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.client.failover.impl.FailoverClosureImpl 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);
}
}
Aggregations