Search in sources :

Example 1 with BaseResponse

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);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) BaseResponse(com.alipay.sofa.jraft.rhea.cmd.store.BaseResponse) InvokeContext(com.alipay.sofa.jraft.rpc.InvokeContext) InvokeCallback(com.alipay.sofa.jraft.rpc.InvokeCallback)

Example 2 with BaseResponse

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());
    }
}
Also used : BaseResponse(com.alipay.sofa.jraft.rhea.cmd.store.BaseResponse) NoRegionFoundResponse(com.alipay.sofa.jraft.rhea.cmd.store.NoRegionFoundResponse) RheaRuntimeException(com.alipay.sofa.jraft.rhea.errors.RheaRuntimeException) BaseRequest(com.alipay.sofa.jraft.rhea.cmd.store.BaseRequest)

Example 3 with BaseResponse

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);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) BaseResponse(com.alipay.sofa.jraft.rhea.cmd.store.BaseResponse) RangeSplitRequest(com.alipay.sofa.jraft.rhea.cmd.store.RangeSplitRequest) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

BaseResponse (com.alipay.sofa.jraft.rhea.cmd.store.BaseResponse)3 Status (com.alipay.sofa.jraft.Status)2 PeerId (com.alipay.sofa.jraft.entity.PeerId)1 BaseRequest (com.alipay.sofa.jraft.rhea.cmd.store.BaseRequest)1 NoRegionFoundResponse (com.alipay.sofa.jraft.rhea.cmd.store.NoRegionFoundResponse)1 RangeSplitRequest (com.alipay.sofa.jraft.rhea.cmd.store.RangeSplitRequest)1 RheaRuntimeException (com.alipay.sofa.jraft.rhea.errors.RheaRuntimeException)1 InvokeCallback (com.alipay.sofa.jraft.rpc.InvokeCallback)1 InvokeContext (com.alipay.sofa.jraft.rpc.InvokeContext)1