Search in sources :

Example 16 with ErrorResponse

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

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 ErrorResponse) {
                        status = handleErrorResponse((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 ErrorResponse eResp = (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(com.alipay.sofa.jraft.Status) InvokeCallback(com.alipay.sofa.jraft.rpc.InvokeCallback) InvokeTimeoutException(com.alipay.sofa.jraft.error.InvokeTimeoutException) Message(com.google.protobuf.Message) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Executor(java.util.concurrent.Executor) RemotingException(com.alipay.sofa.jraft.error.RemotingException) Descriptors(com.google.protobuf.Descriptors) RpcClient(com.alipay.sofa.jraft.rpc.RpcClient)

Example 17 with ErrorResponse

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

the class RpcResponseFactoryTest method testNewResponseWithArgs.

@Test
public void testNewResponseWithArgs() {
    ErrorResponse response = (ErrorResponse) RpcFactoryHelper.responseFactory().newResponse(null, 300, "hello world");
    assertEquals(response.getErrorCode(), 300);
    assertEquals(response.getErrorMsg(), "hello world");
}
Also used : ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.Test)

Example 18 with ErrorResponse

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

the class RpcResponseFactoryTest method testNewResponseFromStatus.

@Test
public void testNewResponseFromStatus() {
    ErrorResponse response = (ErrorResponse) RpcFactoryHelper.responseFactory().newResponse(null, Status.OK());
    assertEquals(response.getErrorCode(), 0);
    assertEquals(response.getErrorMsg(), "");
}
Also used : ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.Test)

Example 19 with ErrorResponse

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

the class BaseCliRequestProcessorTest method testInvalidPeerId.

@Test
public void testInvalidPeerId() {
    this.processor = new MockCliRequestProcessor("localhost", "test");
    this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
    ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
    assertNotNull(resp);
    assertEquals(RaftError.EINVAL.getNumber(), resp.getErrorCode());
    assertEquals("Fail to parse peer: localhost", resp.getErrorMsg());
}
Also used : ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.Test)

Example 20 with ErrorResponse

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

the class BaseCliRequestProcessorTest method testManyNodes.

@Test
public void testManyNodes() {
    Node node1 = Mockito.mock(Node.class);
    Mockito.when(node1.getGroupId()).thenReturn("test");
    Mockito.when(node1.getNodeId()).thenReturn(new NodeId("test", new PeerId("localhost", 8081)));
    NodeOptions opts = new NodeOptions();
    Mockito.when(node1.getOptions()).thenReturn(opts);
    NodeManager.getInstance().addAddress(new Endpoint("localhost", 8081));
    NodeManager.getInstance().add(node1);
    Node node2 = Mockito.mock(Node.class);
    Mockito.when(node2.getGroupId()).thenReturn("test");
    Mockito.when(node2.getNodeId()).thenReturn(new NodeId("test", new PeerId("localhost", 8082)));
    Mockito.when(node2.getOptions()).thenReturn(opts);
    NodeManager.getInstance().addAddress(new Endpoint("localhost", 8082));
    NodeManager.getInstance().add(node2);
    this.processor = new MockCliRequestProcessor(null, "test");
    this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
    ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
    assertNotNull(resp);
    assertEquals(RaftError.EINVAL.getNumber(), resp.getErrorCode());
    assertEquals("Peer must be specified since there're 2 nodes in group test", resp.getErrorMsg());
}
Also used : Endpoint(com.alipay.sofa.jraft.util.Endpoint) Node(com.alipay.sofa.jraft.Node) NodeId(com.alipay.sofa.jraft.entity.NodeId) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) PeerId(com.alipay.sofa.jraft.entity.PeerId) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.Test)

Aggregations

ErrorResponse (com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse)26 Test (org.junit.Test)22 Message (com.google.protobuf.Message)8 Status (com.alipay.sofa.jraft.Status)6 PingRequest (com.alipay.sofa.jraft.rpc.RpcRequests.PingRequest)6 Node (com.alipay.sofa.jraft.Node)4 PeerId (com.alipay.sofa.jraft.entity.PeerId)4 RemotingException (com.alipay.sofa.jraft.error.RemotingException)3 NodeId (com.alipay.sofa.jraft.entity.NodeId)2 InvokeTimeoutException (com.alipay.sofa.jraft.error.InvokeTimeoutException)2 JRaftException (com.alipay.sofa.jraft.error.JRaftException)2 RpcClient (com.alipay.sofa.jraft.rpc.RpcClient)2 ExecutionException (java.util.concurrent.ExecutionException)2 RpcRequestCommand (com.alipay.remoting.rpc.protocol.RpcRequestCommand)1 RpcResponseCommand (com.alipay.remoting.rpc.protocol.RpcResponseCommand)1 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)1 GetLeaderRequest (com.alipay.sofa.jraft.rpc.CliRequests.GetLeaderRequest)1 GetLeaderResponse (com.alipay.sofa.jraft.rpc.CliRequests.GetLeaderResponse)1 GetPeersRequest (com.alipay.sofa.jraft.rpc.CliRequests.GetPeersRequest)1 GetPeersResponse (com.alipay.sofa.jraft.rpc.CliRequests.GetPeersResponse)1