Search in sources :

Example 1 with RemotingException

use of org.apache.ignite.raft.jraft.error.RemotingException in project ignite-3 by apache.

the class AbstractClientServiceTest method testInvokeWithDoneException.

@Test
public void testInvokeWithDoneException() throws Exception {
    InvokeContext invokeCtx = new InvokeContext();
    ArgumentCaptor<InvokeCallback> callbackArg = ArgumentCaptor.forClass(InvokeCallback.class);
    PingRequest request = TestUtils.createPingRequest();
    Mockito.doThrow(new RemotingException()).when(this.rpcClient).invokeAsync(eq(this.endpoint), eq(request), eq(invokeCtx), callbackArg.capture(), eq((long) this.rpcOptions.getRpcDefaultTimeout()));
    MockRpcResponseClosure<ErrorResponse> done = new MockRpcResponseClosure<>();
    Future<Message> future = this.clientService.invokeWithDone(this.endpoint, request, invokeCtx, done, -1);
    InvokeCallback cb = callbackArg.getValue();
    assertNotNull(cb);
    assertNotNull(future);
    assertTrue(future.isDone());
    done.latch.await();
    assertNotNull(done.status);
    assertEquals(RaftError.EINTERNAL.getNumber(), done.status.getCode());
}
Also used : PingRequest(org.apache.ignite.raft.jraft.rpc.RpcRequests.PingRequest) RemotingException(org.apache.ignite.raft.jraft.error.RemotingException) ErrorResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.jupiter.api.Test)

Example 2 with RemotingException

use of org.apache.ignite.raft.jraft.error.RemotingException 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)

Example 3 with RemotingException

use of org.apache.ignite.raft.jraft.error.RemotingException in project ignite-3 by apache.

the class IgniteRpcClient method invokeAsync.

/**
 * {@inheritDoc}
 */
@Override
public CompletableFuture<Message> invokeAsync(Endpoint endpoint, Object request, InvokeContext ctx, InvokeCallback callback, long timeoutMs) {
    CompletableFuture<Message> fut = new CompletableFuture<>();
    fut.orTimeout(timeoutMs, TimeUnit.MILLISECONDS).whenComplete((res, err) -> {
        assert !(res == null && err == null) : res + " " + err;
        if (err == null && recordPred != null && recordPred.test(res, this.toString()))
            recordedMsgs.add(new Object[] { res, this.toString(), fut.hashCode(), System.currentTimeMillis(), null });
        if (err instanceof ExecutionException)
            err = new RemotingException(err);
        else if (// Translate timeout exception.
        err instanceof TimeoutException)
            err = new InvokeTimeoutException();
        Throwable finalErr = err;
        // Avoid deadlocks if a closure has completed in the same thread.
        Utils.runInThread(callback.executor(), () -> callback.complete(res, finalErr));
    });
    // Future hashcode used as corellation id.
    if (recordPred != null && recordPred.test(request, endpoint.toString()))
        recordedMsgs.add(new Object[] { request, endpoint.toString(), fut.hashCode(), System.currentTimeMillis(), null });
    synchronized (this) {
        if (blockPred != null && blockPred.test(request, endpoint.toString())) {
            Object[] msgData = { request, endpoint.toString(), fut.hashCode(), System.currentTimeMillis(), (Runnable) () -> send(endpoint, request, fut, timeoutMs) };
            blockedMsgs.add(msgData);
            LOG.info("Blocked message to={} id={} msg={}", endpoint.toString(), msgData[2], S.toString(request));
            return fut;
        }
    }
    send(endpoint, request, fut, timeoutMs);
    return fut;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) InvokeTimeoutException(org.apache.ignite.raft.jraft.error.InvokeTimeoutException) NetworkMessage(org.apache.ignite.network.NetworkMessage) Message(org.apache.ignite.raft.jraft.rpc.Message) RemotingException(org.apache.ignite.raft.jraft.error.RemotingException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) InvokeTimeoutException(org.apache.ignite.raft.jraft.error.InvokeTimeoutException)

Example 4 with RemotingException

use of org.apache.ignite.raft.jraft.error.RemotingException in project ignite-3 by apache.

the class DefaultRaftClientService method failedFuture.

/**
 * @param executor The executor to run done closure.
 * @param request The request.
 * @param done The closure.
 * @param endpoint The endpoint.
 * @return The future.
 */
private Future<Message> failedFuture(Executor executor, Message request, RpcResponseClosure<?> done, Endpoint endpoint) {
    // fail-fast when no connection
    final CompletableFuture<Message> future = new CompletableFuture<>();
    executor.execute(() -> {
        if (done != null) {
            try {
                done.run(new Status(RaftError.EINTERNAL, "Check connection[%s] fail and try to create new one", endpoint));
            } catch (final Throwable t) {
                LOG.error("Fail to run RpcResponseClosure, the request is {}.", t, request);
            }
        }
        future.completeExceptionally(new RemotingException("Check connection[" + endpoint.toString() + "] fail and try to create new one"));
    });
    return future;
}
Also used : Status(org.apache.ignite.raft.jraft.Status) CompletableFuture(java.util.concurrent.CompletableFuture) Message(org.apache.ignite.raft.jraft.rpc.Message) RemotingException(org.apache.ignite.raft.jraft.error.RemotingException)

Aggregations

RemotingException (org.apache.ignite.raft.jraft.error.RemotingException)4 Message (org.apache.ignite.raft.jraft.rpc.Message)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 Status (org.apache.ignite.raft.jraft.Status)2 InvokeTimeoutException (org.apache.ignite.raft.jraft.error.InvokeTimeoutException)2 ErrorResponse (org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse)2 ConnectException (java.net.ConnectException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Executor (java.util.concurrent.Executor)1 TimeoutException (java.util.concurrent.TimeoutException)1 NetworkMessage (org.apache.ignite.network.NetworkMessage)1 InvokeCallback (org.apache.ignite.raft.jraft.rpc.InvokeCallback)1 RpcClient (org.apache.ignite.raft.jraft.rpc.RpcClient)1 PingRequest (org.apache.ignite.raft.jraft.rpc.RpcRequests.PingRequest)1 Test (org.junit.jupiter.api.Test)1