use of com.alipay.sofa.jraft.error.InvokeTimeoutException in project sofa-jraft by sofastack.
the class GrpcClient method invokeSync.
@Override
public Object invokeSync(final Endpoint endpoint, final Object request, final InvokeContext ctx, final long timeoutMs) throws RemotingException {
final CompletableFuture<Object> future = new CompletableFuture<>();
invokeAsync(endpoint, request, ctx, (result, err) -> {
if (err == null) {
future.complete(result);
} else {
future.completeExceptionally(err);
}
}, timeoutMs);
try {
return future.get(timeoutMs, TimeUnit.MILLISECONDS);
} catch (final TimeoutException e) {
future.cancel(true);
throw new InvokeTimeoutException(e);
} catch (final Throwable t) {
future.cancel(true);
throw new RemotingException(t);
}
}
use of com.alipay.sofa.jraft.error.InvokeTimeoutException 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;
}
use of com.alipay.sofa.jraft.error.InvokeTimeoutException 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());
}
Aggregations