Search in sources :

Example 11 with ErrorResponse

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

the class NodeRequestProcessorTest method testInvalidPeerId.

@Test
public void testInvalidPeerId() {
    this.processor = new MockRequestProcessor("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 peerId: localhost", resp.getErrorMsg());
}
Also used : ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.Test)

Example 12 with ErrorResponse

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

the class AbstractClientServiceTest method testInvokeWithDoneOK.

@Test
public void testInvokeWithDoneOK() throws Exception {
    ArgumentCaptor<InvokeCallback> callbackArg = ArgumentCaptor.forClass(InvokeCallback.class);
    PingRequest request = TestUtils.createPingRequest();
    MockRpcResponseClosure<ErrorResponse> done = new MockRpcResponseClosure<>();
    Future<Message> future = this.clientService.invokeWithDone(this.endpoint, request, done, -1);
    Mockito.verify(this.rpcClient).invokeAsync(eq(this.endpoint), eq(request), Mockito.any(), callbackArg.capture(), eq((long) this.rpcOptions.getRpcDefaultTimeout()));
    InvokeCallback cb = callbackArg.getValue();
    assertNotNull(cb);
    assertNotNull(future);
    assertNull(done.getResponse());
    assertNull(done.status);
    assertFalse(future.isDone());
    ErrorResponse response = (ErrorResponse) this.rpcResponseFactory.newResponse(null, Status.OK());
    cb.complete(response, null);
    Message msg = future.get();
    assertNotNull(msg);
    assertTrue(msg instanceof ErrorResponse);
    assertSame(msg, response);
    done.latch.await();
    assertNotNull(done.status);
    assertEquals(0, done.status.getCode());
}
Also used : PingRequest(com.alipay.sofa.jraft.rpc.RpcRequests.PingRequest) Message(com.google.protobuf.Message) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.Test)

Example 13 with ErrorResponse

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

the class AbstractClientServiceTest method testCancel.

@Test
public void testCancel() throws Exception {
    ArgumentCaptor<InvokeCallback> callbackArg = ArgumentCaptor.forClass(InvokeCallback.class);
    PingRequest request = TestUtils.createPingRequest();
    MockRpcResponseClosure<ErrorResponse> done = new MockRpcResponseClosure<>();
    Future<Message> future = this.clientService.invokeWithDone(this.endpoint, request, done, -1);
    Mockito.verify(this.rpcClient).invokeAsync(eq(this.endpoint), eq(request), Mockito.any(), callbackArg.capture(), eq((long) this.rpcOptions.getRpcDefaultTimeout()));
    InvokeCallback cb = callbackArg.getValue();
    assertNotNull(cb);
    assertNotNull(future);
    assertNull(done.getResponse());
    assertNull(done.status);
    assertFalse(future.isDone());
    future.cancel(true);
    ErrorResponse response = (ErrorResponse) this.rpcResponseFactory.newResponse(null, Status.OK());
    cb.complete(response, null);
    // The closure should be notified with ECANCELED error code.
    done.latch.await();
    assertNotNull(done.status);
    assertEquals(RaftError.ECANCELED.getNumber(), done.status.getCode());
}
Also used : PingRequest(com.alipay.sofa.jraft.rpc.RpcRequests.PingRequest) Message(com.google.protobuf.Message) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.Test)

Example 14 with ErrorResponse

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

the class CliServiceImpl method getLeader.

@Override
public Status getLeader(final String groupId, final Configuration conf, final PeerId leaderId) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireNonNull(leaderId, "Null leader id");
    if (conf == null || conf.isEmpty()) {
        return new Status(RaftError.EINVAL, "Empty group configuration");
    }
    final Status st = new Status(-1, "Fail to get leader of group %s", groupId);
    for (final PeerId peer : conf) {
        if (!this.cliClientService.connect(peer.getEndpoint())) {
            LOG.error("Fail to connect peer {} to get leader for group {}.", peer, groupId);
            continue;
        }
        final GetLeaderRequest.Builder rb = // 
        GetLeaderRequest.newBuilder().setGroupId(// 
        groupId).setPeerId(peer.toString());
        final Future<Message> result = this.cliClientService.getLeader(peer.getEndpoint(), rb.build(), null);
        try {
            final Message msg = result.get(this.cliOptions.getTimeoutMs() <= 0 ? this.cliOptions.getRpcDefaultTimeout() : this.cliOptions.getTimeoutMs(), TimeUnit.MILLISECONDS);
            if (msg instanceof ErrorResponse) {
                if (st.isOk()) {
                    st.setError(-1, ((ErrorResponse) msg).getErrorMsg());
                } else {
                    final String savedMsg = st.getErrorMsg();
                    st.setError(-1, "%s, %s", savedMsg, ((ErrorResponse) msg).getErrorMsg());
                }
            } else {
                final GetLeaderResponse response = (GetLeaderResponse) msg;
                if (leaderId.parse(response.getLeaderId())) {
                    break;
                }
            }
        } catch (final Exception e) {
            if (st.isOk()) {
                st.setError(-1, e.getMessage());
            } else {
                final String savedMsg = st.getErrorMsg();
                st.setError(-1, "%s, %s", savedMsg, e.getMessage());
            }
        }
    }
    if (leaderId.isEmpty()) {
        return st;
    }
    return Status.OK();
}
Also used : Status(com.alipay.sofa.jraft.Status) Message(com.google.protobuf.Message) GetLeaderRequest(com.alipay.sofa.jraft.rpc.CliRequests.GetLeaderRequest) GetLeaderResponse(com.alipay.sofa.jraft.rpc.CliRequests.GetLeaderResponse) JRaftException(com.alipay.sofa.jraft.error.JRaftException) PeerId(com.alipay.sofa.jraft.entity.PeerId) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse)

Example 15 with ErrorResponse

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

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);
    }
    final GetPeersRequest.Builder rb = // 
    GetPeersRequest.newBuilder().setGroupId(// 
    groupId).setLeaderId(// 
    leaderId.toString()).setOnlyAlive(onlyGetAlive);
    try {
        final Message result = this.cliClientService.getPeers(leaderId.getEndpoint(), rb.build(), 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 ProtocolStringList responsePeers = returnLearners ? resp.getLearnersList() : resp.getPeersList();
            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.getErrorMsg());
        }
    } catch (final JRaftException e) {
        throw e;
    } catch (final Exception e) {
        throw new JRaftException(e);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Message(com.google.protobuf.Message) JRaftException(com.alipay.sofa.jraft.error.JRaftException) ArrayList(java.util.ArrayList) GetPeersRequest(com.alipay.sofa.jraft.rpc.CliRequests.GetPeersRequest) ProtocolStringList(com.google.protobuf.ProtocolStringList) JRaftException(com.alipay.sofa.jraft.error.JRaftException) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) GetPeersResponse(com.alipay.sofa.jraft.rpc.CliRequests.GetPeersResponse) PeerId(com.alipay.sofa.jraft.entity.PeerId)

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