Search in sources :

Example 16 with ErrorResponse

use of org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse 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 17 with ErrorResponse

use of org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse 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 18 with ErrorResponse

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

the class CliServiceImpl method getPeers.

private List<PeerId> getPeers(final String groupId, final Configuration conf, final boolean returnLearners, final boolean onlyGetAlive) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireNonNull(conf, "Null conf");
    final PeerId leaderId = new PeerId();
    final Status st = getLeader(groupId, conf, leaderId);
    if (!st.isOk()) {
        throw new IllegalStateException(st.getErrorMsg());
    }
    if (!this.cliClientService.connect(leaderId.getEndpoint())) {
        throw new IllegalStateException("Fail to init channel to leader " + leaderId);
    }
    GetPeersRequest req = cliOptions.getRaftMessagesFactory().getPeersRequest().groupId(groupId).leaderId(leaderId.toString()).onlyAlive(onlyGetAlive).build();
    try {
        final Message result = this.cliClientService.getPeers(leaderId.getEndpoint(), req, null).get(this.cliOptions.getTimeoutMs() <= 0 ? this.cliOptions.getRpcDefaultTimeout() : this.cliOptions.getTimeoutMs(), TimeUnit.MILLISECONDS);
        if (result instanceof GetPeersResponse) {
            final GetPeersResponse resp = (GetPeersResponse) result;
            final List<PeerId> peerIdList = new ArrayList<>();
            final Collection<String> responsePeers = returnLearners ? resp.learnersList() : resp.peersList();
            for (final String peerIdStr : responsePeers) {
                final PeerId newPeer = new PeerId();
                newPeer.parse(peerIdStr);
                peerIdList.add(newPeer);
            }
            return peerIdList;
        } else {
            final ErrorResponse resp = (ErrorResponse) result;
            throw new JRaftException(resp.errorMsg());
        }
    } catch (final JRaftException e) {
        throw e;
    } catch (final Exception e) {
        throw new JRaftException(e);
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) Message(org.apache.ignite.raft.jraft.rpc.Message) JRaftException(org.apache.ignite.raft.jraft.error.JRaftException) ArrayList(java.util.ArrayList) GetPeersRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.GetPeersRequest) JRaftException(org.apache.ignite.raft.jraft.error.JRaftException) ErrorResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse) GetPeersResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.GetPeersResponse) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 19 with ErrorResponse

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

the class NodeRequestProcessorTest method testPeerIdNotFound.

@Test
public void testPeerIdNotFound() {
    this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
    ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
    assertNotNull(resp);
    assertEquals(RaftError.ENOENT.getNumber(), resp.errorCode());
    assertEquals("Peer id not found: localhost:8081, group: test", resp.errorMsg());
}
Also used : ErrorResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.jupiter.api.Test)

Example 20 with ErrorResponse

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

the class NodeRequestProcessorTest method testOK.

@Test
public void testOK() {
    Node node = Mockito.mock(Node.class, withSettings().extraInterfaces(RaftServerService.class));
    Mockito.when(node.getGroupId()).thenReturn("test");
    PeerId peerId = new PeerId("localhost", 8081);
    Mockito.when(node.getNodeId()).thenReturn(new NodeId("test", peerId));
    asyncContext.getNodeManager().add(node);
    this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
    ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
    assertNotNull(resp);
    assertEquals(0, resp.errorCode());
}
Also used : Node(org.apache.ignite.raft.jraft.Node) NodeId(org.apache.ignite.raft.jraft.entity.NodeId) RaftServerService(org.apache.ignite.raft.jraft.rpc.RaftServerService) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) ErrorResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.jupiter.api.Test)

Aggregations

ErrorResponse (org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse)24 Test (org.junit.jupiter.api.Test)20 Status (org.apache.ignite.raft.jraft.Status)5 Node (org.apache.ignite.raft.jraft.Node)4 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)4 Message (org.apache.ignite.raft.jraft.rpc.Message)4 PingRequest (org.apache.ignite.raft.jraft.rpc.RpcRequests.PingRequest)3 NodeId (org.apache.ignite.raft.jraft.entity.NodeId)2 InvokeTimeoutException (org.apache.ignite.raft.jraft.error.InvokeTimeoutException)2 JRaftException (org.apache.ignite.raft.jraft.error.JRaftException)2 RemotingException (org.apache.ignite.raft.jraft.error.RemotingException)2 RpcClient (org.apache.ignite.raft.jraft.rpc.RpcClient)2 ConnectException (java.net.ConnectException)1 ArrayList (java.util.ArrayList)1 Executor (java.util.concurrent.Executor)1 RaftMessagesFactory (org.apache.ignite.raft.jraft.RaftMessagesFactory)1 GetLeaderRequest (org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderRequest)1 GetLeaderResponse (org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderResponse)1 GetPeersRequest (org.apache.ignite.raft.jraft.rpc.CliRequests.GetPeersRequest)1 GetPeersResponse (org.apache.ignite.raft.jraft.rpc.CliRequests.GetPeersResponse)1