Search in sources :

Example 6 with InvokeCallback

use of com.alipay.sofa.jraft.rpc.InvokeCallback in project sofa-jraft by sofastack.

the class DefaultPlacementDriverRpcService method internalCallPdWithRpc.

private <V> void internalCallPdWithRpc(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.pd.BaseResponse) InvokeContext(com.alipay.sofa.jraft.rpc.InvokeContext) InvokeCallback(com.alipay.sofa.jraft.rpc.InvokeCallback)

Example 7 with InvokeCallback

use of com.alipay.sofa.jraft.rpc.InvokeCallback in project mmqtt by MrHKing.

the class JRaftServer method invokeToLeader.

private void invokeToLeader(final String group, final Message request, final int timeoutMillis, FailoverClosure closure) {
    try {
        final Endpoint leaderIp = Optional.ofNullable(getLeader(group)).orElseThrow(() -> new NoLeaderException(group)).getEndpoint();
        cliClientService.getRpcClient().invokeAsync(leaderIp, request, new InvokeCallback() {

            @Override
            public void complete(Object o, Throwable ex) {
                if (Objects.nonNull(ex)) {
                    closure.setThrowable(ex);
                    closure.run(new Status(RaftError.UNKNOWN, ex.getMessage()));
                    return;
                }
                closure.setResponse((Response) o);
                closure.run(Status.OK());
            }

            @Override
            public Executor executor() {
                return RaftExecutor.getRaftCliServiceExecutor();
            }
        }, timeoutMillis);
    } catch (Exception e) {
        closure.setThrowable(e);
        closure.run(new Status(RaftError.UNKNOWN, e.toString()));
    }
}
Also used : Response(org.monkey.mmq.core.entity.Response) InvokeCallback(com.alipay.sofa.jraft.rpc.InvokeCallback) Executor(java.util.concurrent.Executor) Endpoint(com.alipay.sofa.jraft.util.Endpoint)

Aggregations

InvokeCallback (com.alipay.sofa.jraft.rpc.InvokeCallback)7 Status (com.alipay.sofa.jraft.Status)5 Executor (java.util.concurrent.Executor)4 BaseResponse (com.alipay.sofa.jraft.rhea.cmd.pd.BaseResponse)2 InvokeContext (com.alipay.sofa.jraft.rpc.InvokeContext)2 Endpoint (com.alipay.sofa.jraft.util.Endpoint)2 Response (com.alibaba.nacos.consistency.entity.Response)1 ConsistencyException (com.alibaba.nacos.consistency.exception.ConsistencyException)1 DuplicateRaftGroupException (com.alibaba.nacos.core.distributed.raft.exception.DuplicateRaftGroupException)1 JRaftException (com.alibaba.nacos.core.distributed.raft.exception.JRaftException)1 NoLeaderException (com.alibaba.nacos.core.distributed.raft.exception.NoLeaderException)1 NoSuchRaftGroupException (com.alibaba.nacos.core.distributed.raft.exception.NoSuchRaftGroupException)1 RaftExecutor (com.alibaba.nacos.core.distributed.raft.utils.RaftExecutor)1 InvokeTimeoutException (com.alipay.sofa.jraft.error.InvokeTimeoutException)1 RemotingException (com.alipay.sofa.jraft.error.RemotingException)1 IncrementAndGetRequest (com.alipay.sofa.jraft.example.counter.rpc.CounterOutter.IncrementAndGetRequest)1 BaseResponse (com.alipay.sofa.jraft.rhea.cmd.store.BaseResponse)1 RpcClient (com.alipay.sofa.jraft.rpc.RpcClient)1 ErrorResponse (com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse)1 Descriptors (com.google.protobuf.Descriptors)1