Search in sources :

Example 6 with PingRequest

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

the class AbstractClientService method connect.

@Override
public boolean connect(final Endpoint endpoint) {
    final RpcClient rc = this.rpcClient;
    if (rc == null) {
        throw new IllegalStateException("Client service is uninitialized.");
    }
    if (isConnected(rc, endpoint)) {
        return true;
    }
    try {
        final PingRequest req = // 
        PingRequest.newBuilder().setSendTimestamp(// 
        System.currentTimeMillis()).build();
        final ErrorResponse resp = (ErrorResponse) rc.invokeSync(endpoint, req, this.rpcOptions.getRpcConnectTimeoutMs());
        return resp.getErrorCode() == 0;
    } catch (final InterruptedException e) {
        Thread.currentThread().interrupt();
        return false;
    } catch (final RemotingException e) {
        LOG.error("Fail to connect {}, remoting exception: {}.", endpoint, e.getMessage());
        return false;
    }
}
Also used : PingRequest(com.alipay.sofa.jraft.rpc.RpcRequests.PingRequest) RemotingException(com.alipay.sofa.jraft.error.RemotingException) RpcClient(com.alipay.sofa.jraft.rpc.RpcClient) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse)

Example 7 with PingRequest

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

the class AbstractClientServiceTest method testInvokeWithDoneOnException.

@Test
public void testInvokeWithDoneOnException() throws Exception {
    InvokeContext invokeCtx = new InvokeContext();
    invokeCtx.put(InvokeContext.CRC_SWITCH, false);
    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, invokeCtx, done, -1);
    Mockito.verify(this.rpcClient).invokeAsync(eq(this.endpoint), eq(request), eq(invokeCtx), callbackArg.capture(), eq((long) this.rpcOptions.getRpcDefaultTimeout()));
    InvokeCallback cb = callbackArg.getValue();
    assertNotNull(cb);
    assertNotNull(future);
    assertNull(done.getResponse());
    assertNull(done.status);
    assertFalse(future.isDone());
    cb.complete(null, new InvokeTimeoutException());
    try {
        future.get();
        fail();
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof InvokeTimeoutException);
    }
    done.latch.await();
    assertNotNull(done.status);
    assertEquals(RaftError.ETIMEDOUT.getNumber(), done.status.getCode());
}
Also used : PingRequest(com.alipay.sofa.jraft.rpc.RpcRequests.PingRequest) InvokeTimeoutException(com.alipay.sofa.jraft.error.InvokeTimeoutException) Message(com.google.protobuf.Message) ExecutionException(java.util.concurrent.ExecutionException) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.Test)

Example 8 with PingRequest

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

the class AppendEntriesRequestProcessorTest method testTooManyPendingResponses.

@Test
public void testTooManyPendingResponses() {
    final PeerId peer = mockNode();
    NodeManager.getInstance().get(this.groupId, peer).getRaftOptions().setMaxReplicatorInflightMsgs(2);
    final RpcContext asyncContext = Mockito.mock(RpcContext.class);
    final AppendEntriesRequestProcessor processor = (AppendEntriesRequestProcessor) newProcessor();
    final PeerPair pair = processor.pairOf(this.peerIdStr, this.serverId);
    final PingRequest msg = TestUtils.createPingRequest();
    final Connection conn = Mockito.mock(Connection.class);
    Mockito.when(asyncContext.getConnection()).thenReturn(conn);
    final PeerRequestContext ctx = processor.getOrCreatePeerRequestContext(this.groupId, pair, conn);
    assertNotNull(ctx);
    processor.sendSequenceResponse(this.groupId, pair, 1, asyncContext, msg);
    processor.sendSequenceResponse(this.groupId, pair, 2, asyncContext, msg);
    processor.sendSequenceResponse(this.groupId, pair, 3, asyncContext, msg);
    Mockito.verify(asyncContext, Mockito.never()).sendResponse(msg);
    Mockito.verify(conn).close();
    final PeerRequestContext newCtx = processor.getOrCreatePeerRequestContext(this.groupId, pair, conn);
    assertNotNull(newCtx);
    assertNotSame(ctx, newCtx);
}
Also used : RpcContext(com.alipay.sofa.jraft.rpc.RpcContext) PingRequest(com.alipay.sofa.jraft.rpc.RpcRequests.PingRequest) PeerPair(com.alipay.sofa.jraft.rpc.impl.core.AppendEntriesRequestProcessor.PeerPair) Connection(com.alipay.sofa.jraft.rpc.Connection) PeerRequestContext(com.alipay.sofa.jraft.rpc.impl.core.AppendEntriesRequestProcessor.PeerRequestContext) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 9 with PingRequest

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

the class AbstractClientServiceTest method testInvokeWithDoneException.

@Test
public void testInvokeWithDoneException() throws Exception {
    InvokeContext invokeCtx = new InvokeContext();
    invokeCtx.put(InvokeContext.CRC_SWITCH, false);
    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());
    try {
        future.get();
        fail();
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof RemotingException);
    }
    done.latch.await();
    assertNotNull(done.status);
    assertEquals(RaftError.EINTERNAL.getNumber(), done.status.getCode());
}
Also used : PingRequest(com.alipay.sofa.jraft.rpc.RpcRequests.PingRequest) Message(com.google.protobuf.Message) RemotingException(com.alipay.sofa.jraft.error.RemotingException) ExecutionException(java.util.concurrent.ExecutionException) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.Test)

Aggregations

PingRequest (com.alipay.sofa.jraft.rpc.RpcRequests.PingRequest)9 Test (org.junit.Test)8 ErrorResponse (com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse)6 Message (com.google.protobuf.Message)4 RpcRequestCommand (com.alipay.remoting.rpc.protocol.RpcRequestCommand)2 RemotingException (com.alipay.sofa.jraft.error.RemotingException)2 RpcContext (com.alipay.sofa.jraft.rpc.RpcContext)2 PeerPair (com.alipay.sofa.jraft.rpc.impl.core.AppendEntriesRequestProcessor.PeerPair)2 ExecutionException (java.util.concurrent.ExecutionException)2 RpcResponseCommand (com.alipay.remoting.rpc.protocol.RpcResponseCommand)1 Status (com.alipay.sofa.jraft.Status)1 PeerId (com.alipay.sofa.jraft.entity.PeerId)1 InvokeTimeoutException (com.alipay.sofa.jraft.error.InvokeTimeoutException)1 Connection (com.alipay.sofa.jraft.rpc.Connection)1 RpcClient (com.alipay.sofa.jraft.rpc.RpcClient)1 PeerRequestContext (com.alipay.sofa.jraft.rpc.impl.core.AppendEntriesRequestProcessor.PeerRequestContext)1