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