use of com.alipay.sofa.jraft.rhea.cmd.store.BaseResponse in project sofa-jraft by sofastack.
the class DefaultRheaKVRpcService method internalCallAsyncWithRpc.
private <V> void internalCallAsyncWithRpc(final Endpoint endpoint, final BaseRequest request, final FailoverClosure<V> closure) {
final InvokeContext invokeCtx = new InvokeContext();
invokeCtx.put(BoltRpcClient.BOLT_CTX, ExtSerializerSupports.getInvokeContext());
final InvokeCallback invokeCallback = new InvokeCallback() {
@Override
public void complete(final Object result, final Throwable err) {
if (err == null) {
final BaseResponse<?> response = (BaseResponse<?>) result;
if (response.isSuccess()) {
closure.setData(response.getValue());
closure.run(Status.OK());
} else {
closure.setError(response.getError());
closure.run(new Status(-1, "RPC failed with address: %s, response: %s", endpoint, response));
}
} else {
closure.failure(err);
}
}
@Override
public Executor executor() {
return rpcCallbackExecutor;
}
};
try {
this.rpcClient.invokeAsync(endpoint, request, invokeCtx, invokeCallback, this.rpcTimeoutMillis);
} catch (final Throwable t) {
closure.failure(t);
}
}
use of com.alipay.sofa.jraft.rhea.cmd.store.BaseResponse in project sofa-jraft by sofastack.
the class KVCommandProcessor method handleRequest.
@Override
public void handleRequest(final RpcContext rpcCtx, final T request) {
Requires.requireNonNull(request, "request");
final RequestProcessClosure<BaseRequest, BaseResponse<?>> closure = new RequestProcessClosure<>(request, rpcCtx);
final RegionKVService regionKVService = this.storeEngine.getRegionKVService(request.getRegionId());
if (regionKVService == null) {
final NoRegionFoundResponse noRegion = new NoRegionFoundResponse();
noRegion.setRegionId(request.getRegionId());
noRegion.setError(Errors.NO_REGION_FOUND);
noRegion.setValue(false);
closure.sendResponse(noRegion);
return;
}
switch(request.magic()) {
case BaseRequest.PUT:
regionKVService.handlePutRequest((PutRequest) request, closure);
break;
case BaseRequest.BATCH_PUT:
regionKVService.handleBatchPutRequest((BatchPutRequest) request, closure);
break;
case BaseRequest.PUT_IF_ABSENT:
regionKVService.handlePutIfAbsentRequest((PutIfAbsentRequest) request, closure);
break;
case BaseRequest.GET_PUT:
regionKVService.handleGetAndPutRequest((GetAndPutRequest) request, closure);
break;
case BaseRequest.COMPARE_PUT:
regionKVService.handleCompareAndPutRequest((CompareAndPutRequest) request, closure);
break;
case BaseRequest.DELETE:
regionKVService.handleDeleteRequest((DeleteRequest) request, closure);
break;
case BaseRequest.DELETE_RANGE:
regionKVService.handleDeleteRangeRequest((DeleteRangeRequest) request, closure);
break;
case BaseRequest.BATCH_DELETE:
regionKVService.handleBatchDeleteRequest((BatchDeleteRequest) request, closure);
break;
case BaseRequest.MERGE:
regionKVService.handleMergeRequest((MergeRequest) request, closure);
break;
case BaseRequest.GET:
regionKVService.handleGetRequest((GetRequest) request, closure);
break;
case BaseRequest.MULTI_GET:
regionKVService.handleMultiGetRequest((MultiGetRequest) request, closure);
break;
case BaseRequest.CONTAINS_KEY:
regionKVService.handleContainsKeyRequest((ContainsKeyRequest) request, closure);
break;
case BaseRequest.SCAN:
regionKVService.handleScanRequest((ScanRequest) request, closure);
break;
case BaseRequest.GET_SEQUENCE:
regionKVService.handleGetSequence((GetSequenceRequest) request, closure);
break;
case BaseRequest.RESET_SEQUENCE:
regionKVService.handleResetSequence((ResetSequenceRequest) request, closure);
break;
case BaseRequest.KEY_LOCK:
regionKVService.handleKeyLockRequest((KeyLockRequest) request, closure);
break;
case BaseRequest.KEY_UNLOCK:
regionKVService.handleKeyUnlockRequest((KeyUnlockRequest) request, closure);
break;
case BaseRequest.NODE_EXECUTE:
regionKVService.handleNodeExecuteRequest((NodeExecuteRequest) request, closure);
break;
case BaseRequest.RANGE_SPLIT:
regionKVService.handleRangeSplitRequest((RangeSplitRequest) request, closure);
break;
case BaseRequest.COMPARE_PUT_ALL:
regionKVService.handleCompareAndPutAll((CASAllRequest) request, closure);
break;
default:
throw new RheaRuntimeException("Unsupported request type: " + request.getClass().getName());
}
}
use of com.alipay.sofa.jraft.rhea.cmd.store.BaseResponse in project sofa-jraft by sofastack.
the class DefaultRheaKVCliService method rangeSplit.
@Override
public Status rangeSplit(final long regionId, final long newRegionId, final String groupId, final Configuration conf) {
final PeerId leaderId = new PeerId();
final Status st = this.cliService.getLeader(groupId, conf, leaderId);
if (!st.isOk()) {
throw new IllegalStateException(st.getErrorMsg());
}
final RangeSplitRequest request = new RangeSplitRequest();
request.setRegionId(regionId);
request.setNewRegionId(newRegionId);
try {
final BaseResponse<?> response = (BaseResponse<?>) this.rpcClient.invokeSync(leaderId.getEndpoint(), request, this.opts.getTimeoutMs());
if (response.isSuccess()) {
return Status.OK();
}
return new Status(-1, "Fail to range split on region %d, error: %s", regionId, response);
} catch (final Exception e) {
LOG.error("Fail to range split on exception: {}.", StackTraceUtil.stackTrace(e));
return new Status(-1, "fail to range split on region %d", regionId);
}
}
Aggregations