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;
}
}
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());
}
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);
}
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());
}
Aggregations