Search in sources :

Example 1 with InvokeCallback

use of io.dingodb.raft.rpc.InvokeCallback in project dingo by dingodb.

the class DefaultDingoRowStoreRpcService 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(io.dingodb.raft.Status) BaseResponse(io.dingodb.store.row.cmd.store.BaseResponse) InvokeContext(io.dingodb.raft.rpc.InvokeContext) InvokeCallback(io.dingodb.raft.rpc.InvokeCallback)

Example 2 with InvokeCallback

use of io.dingodb.raft.rpc.InvokeCallback in project dingo by dingodb.

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(io.dingodb.raft.Status) BaseResponse(io.dingodb.store.row.cmd.pd.BaseResponse) InvokeContext(io.dingodb.raft.rpc.InvokeContext) InvokeCallback(io.dingodb.raft.rpc.InvokeCallback)

Example 3 with InvokeCallback

use of io.dingodb.raft.rpc.InvokeCallback in project dingo by dingodb.

the class AbstractClientService method invokeWithDone.

public <T extends Message> Future<Message> invokeWithDone(final Endpoint endpoint, final Message request, final InvokeContext ctx, final RpcResponseClosure<T> done, final int timeoutMs, final Executor rpcExecutor) {
    final RpcClient rc = this.rpcClient;
    final FutureImpl<Message> future = new FutureImpl<>();
    final Executor currExecutor = rpcExecutor != null ? rpcExecutor : this.rpcExecutor;
    try {
        if (rc == null) {
            future.failure(new IllegalStateException("Client service is uninitialized."));
            // should be in another thread to avoid dead locking.
            RpcUtils.runClosureInExecutor(currExecutor, done, new Status(RaftError.EINTERNAL, "Client service is uninitialized."));
            return future;
        }
        rc.invokeAsync(endpoint, request, ctx, new InvokeCallback() {

            @SuppressWarnings({ "unchecked", "ConstantConditions" })
            @Override
            public void complete(final Object result, final Throwable err) {
                if (future.isCancelled()) {
                    onCanceled(request, done);
                    return;
                }
                if (err == null) {
                    Status status = Status.OK();
                    Message msg;
                    if (result instanceof RpcRequests.ErrorResponse) {
                        status = handleErrorResponse((RpcRequests.ErrorResponse) result);
                        msg = (Message) result;
                    } else if (result instanceof Message) {
                        final Descriptors.FieldDescriptor fd = // 
                        ((Message) result).getDescriptorForType().findFieldByNumber(RpcResponseFactory.ERROR_RESPONSE_NUM);
                        if (fd != null && ((Message) result).hasField(fd)) {
                            final RpcRequests.ErrorResponse eResp = (RpcRequests.ErrorResponse) ((Message) result).getField(fd);
                            status = handleErrorResponse(eResp);
                            msg = eResp;
                        } else {
                            msg = (T) result;
                        }
                    } else {
                        msg = (T) result;
                    }
                    if (done != null) {
                        try {
                            if (status.isOk()) {
                                done.setResponse((T) msg);
                            }
                            done.run(status);
                        } catch (final Throwable t) {
                            LOG.error("Fail to run RpcResponseClosure, the request is {}.", request, t);
                        }
                    }
                    if (!future.isDone()) {
                        future.setResult(msg);
                    }
                } else {
                    if (done != null) {
                        try {
                            done.run(new Status(err instanceof InvokeTimeoutException ? RaftError.ETIMEDOUT : RaftError.EINTERNAL, "RPC exception:" + err.getMessage()));
                        } catch (final Throwable t) {
                            LOG.error("Fail to run RpcResponseClosure, the request is {}.", request, t);
                        }
                    }
                    if (!future.isDone()) {
                        future.failure(err);
                    }
                }
            }

            @Override
            public Executor executor() {
                return currExecutor;
            }
        }, timeoutMs <= 0 ? this.rpcOptions.getRpcDefaultTimeout() : timeoutMs);
    } catch (final InterruptedException e) {
        Thread.currentThread().interrupt();
        future.failure(e);
        // should be in another thread to avoid dead locking.
        RpcUtils.runClosureInExecutor(currExecutor, done, new Status(RaftError.EINTR, "Sending rpc was interrupted"));
    } catch (final RemotingException e) {
        future.failure(e);
        // should be in another thread to avoid dead locking.
        RpcUtils.runClosureInExecutor(currExecutor, done, new Status(RaftError.EINTERNAL, "Fail to send a RPC request:" + e.getMessage()));
    }
    return future;
}
Also used : Status(io.dingodb.raft.Status) InvokeCallback(io.dingodb.raft.rpc.InvokeCallback) InvokeTimeoutException(io.dingodb.raft.error.InvokeTimeoutException) Message(com.google.protobuf.Message) RpcRequests(io.dingodb.raft.rpc.RpcRequests) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Executor(java.util.concurrent.Executor) RemotingException(io.dingodb.raft.error.RemotingException) Descriptors(com.google.protobuf.Descriptors) RpcClient(io.dingodb.raft.rpc.RpcClient)

Example 4 with InvokeCallback

use of io.dingodb.raft.rpc.InvokeCallback in project dingo by dingodb.

the class StoreHeartbeatSender method callAsyncWithRpc.

private <V> void callAsyncWithRpc(final Endpoint endpoint, final BaseRequest request, final HeartbeatClosure<V> closure) {
    final io.dingodb.raft.rpc.InvokeContext invokeCtx = new io.dingodb.raft.rpc.InvokeContext();
    invokeCtx.put(BoltRpcClient.BOLT_CTX, ExtSerializerSupports.getInvokeContext());
    final InvokeCallback invokeCallback = new InvokeCallback() {

        @SuppressWarnings("unchecked")
        @Override
        public void complete(final Object result, final Throwable err) {
            if (err == null) {
                final BaseResponse<?> response = (BaseResponse<?>) result;
                if (response.isSuccess()) {
                    closure.setResult((V) 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.run(new Status(-1, err.getMessage()));
            }
        }

        @Override
        public Executor executor() {
            return heartbeatRpcCallbackExecutor;
        }
    };
    try {
        this.rpcClient.invokeAsync(endpoint, request, invokeCtx, invokeCallback, this.heartbeatRpcTimeoutMillis);
    } catch (final Throwable t) {
        closure.run(new Status(-1, t.getMessage()));
    }
}
Also used : Status(io.dingodb.raft.Status) BaseResponse(io.dingodb.store.row.cmd.pd.BaseResponse) InvokeCallback(io.dingodb.raft.rpc.InvokeCallback)

Example 5 with InvokeCallback

use of io.dingodb.raft.rpc.InvokeCallback in project dingo by dingodb.

the class StoreHeartbeatSender method callAsyncWithRpc.

private <V> void callAsyncWithRpc(final Endpoint endpoint, final BaseRequest request, final HeartbeatClosure<V> closure) {
    final io.dingodb.raft.rpc.InvokeContext invokeCtx = new io.dingodb.raft.rpc.InvokeContext();
    invokeCtx.put(BoltRpcClient.BOLT_CTX, ExtSerializerSupports.getInvokeContext());
    final InvokeCallback invokeCallback = new InvokeCallback() {

        @SuppressWarnings("unchecked")
        @Override
        public void complete(final Object result, final Throwable err) {
            if (err == null) {
                final BaseResponse<?> response = (BaseResponse<?>) result;
                if (response.isSuccess()) {
                    closure.setResult((V) 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.run(new Status(-1, err.getMessage()));
            }
        }

        @Override
        public Executor executor() {
            return heartbeatRpcCallbackExecutor;
        }
    };
    try {
        this.rpcClient.invokeAsync(endpoint, request, invokeCtx, invokeCallback, this.heartbeatRpcTimeoutMillis);
    } catch (final Throwable t) {
        closure.run(new Status(-1, t.getMessage()));
    }
}
Also used : Status(io.dingodb.raft.Status) BaseResponse(io.dingodb.store.row.cmd.pd.BaseResponse) InvokeCallback(io.dingodb.raft.rpc.InvokeCallback)

Aggregations

Status (io.dingodb.raft.Status)5 InvokeCallback (io.dingodb.raft.rpc.InvokeCallback)5 BaseResponse (io.dingodb.store.row.cmd.pd.BaseResponse)3 InvokeContext (io.dingodb.raft.rpc.InvokeContext)2 Descriptors (com.google.protobuf.Descriptors)1 Message (com.google.protobuf.Message)1 InvokeTimeoutException (io.dingodb.raft.error.InvokeTimeoutException)1 RemotingException (io.dingodb.raft.error.RemotingException)1 RpcClient (io.dingodb.raft.rpc.RpcClient)1 RpcRequests (io.dingodb.raft.rpc.RpcRequests)1 BaseResponse (io.dingodb.store.row.cmd.store.BaseResponse)1 Executor (java.util.concurrent.Executor)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1