Search in sources :

Example 1 with RpcClient

use of org.apache.ignite.raft.jraft.rpc.RpcClient in project ignite-3 by apache.

the class AbstractClientService method connectAsync.

@Override
public CompletableFuture<Boolean> connectAsync(Endpoint endpoint) {
    final RpcClient rc = this.rpcClient;
    if (rc == null) {
        throw new IllegalStateException("Client service is uninitialized.");
    }
    // Remote node is alive and pinged, safe to continue.
    if (readyAddresses.contains(endpoint.toString())) {
        return CompletableFuture.completedFuture(true);
    }
    final RpcRequests.PingRequest req = rpcOptions.getRaftMessagesFactory().pingRequest().sendTimestamp(System.currentTimeMillis()).build();
    CompletableFuture<Message> fut = invokeWithDone(endpoint, req, null, null, rpcOptions.getRpcConnectTimeoutMs(), rpcExecutor);
    return fut.thenApply(msg -> {
        ErrorResponse resp = (ErrorResponse) msg;
        if (resp != null && resp.errorCode() == 0) {
            readyAddresses.add(endpoint.toString());
            return true;
        } else {
            return false;
        }
    });
}
Also used : Message(org.apache.ignite.raft.jraft.rpc.Message) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) RpcClient(org.apache.ignite.raft.jraft.rpc.RpcClient) ErrorResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse)

Example 2 with RpcClient

use of org.apache.ignite.raft.jraft.rpc.RpcClient in project ignite-3 by apache.

the class AbstractClientService method invokeWithDone.

public <T extends Message> CompletableFuture<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) {
            // TODO asch replace with ignite exception, check all places IGNITE-14832
            future.completeExceptionally(new IllegalStateException("Client service is uninitialized."));
            // should be in another thread to avoid dead locking.
            Utils.runClosureInExecutor(currExecutor, done, new Status(RaftError.EINTERNAL, "Client service is uninitialized."));
            return future;
        }
        return rc.invokeAsync(endpoint, request, ctx, new InvokeCallback() {

            @Override
            public void complete(final Object result, final Throwable err) {
                if (err == null) {
                    Status status = Status.OK();
                    Message msg;
                    if (result instanceof ErrorResponse) {
                        status = handleErrorResponse((ErrorResponse) result);
                        msg = (Message) result;
                    } else {
                        msg = (Message) 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 {}.", t, request);
                        }
                    }
                    if (!future.isDone()) {
                        future.complete(msg);
                    }
                } else {
                    if (ThrowUtil.hasCause(err, null, ConnectException.class))
                        // Force logical reconnect.
                        readyAddresses.remove(endpoint.toString());
                    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 {}.", t, request);
                        }
                    }
                    if (!future.isDone()) {
                        future.completeExceptionally(err);
                    }
                }
            }

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

Aggregations

Message (org.apache.ignite.raft.jraft.rpc.Message)2 RpcClient (org.apache.ignite.raft.jraft.rpc.RpcClient)2 ErrorResponse (org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse)2 ConnectException (java.net.ConnectException)1 Executor (java.util.concurrent.Executor)1 Status (org.apache.ignite.raft.jraft.Status)1 InvokeTimeoutException (org.apache.ignite.raft.jraft.error.InvokeTimeoutException)1 RemotingException (org.apache.ignite.raft.jraft.error.RemotingException)1 InvokeCallback (org.apache.ignite.raft.jraft.rpc.InvokeCallback)1 RpcRequests (org.apache.ignite.raft.jraft.rpc.RpcRequests)1