use of org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse in project ignite-3 by apache.
the class AbstractClientService method connectAsync.
@Override
public CompletableFuture<Boolean> connectAsync(Endpoint endpoint) {
final RpcClient rc = this.rpcClient;
if (rc == null) {
throw new IllegalStateException("Client service is uninitialized.");
}
// Remote node is alive and pinged, safe to continue.
if (readyAddresses.contains(endpoint.toString())) {
return CompletableFuture.completedFuture(true);
}
final RpcRequests.PingRequest req = rpcOptions.getRaftMessagesFactory().pingRequest().sendTimestamp(System.currentTimeMillis()).build();
CompletableFuture<Message> fut = invokeWithDone(endpoint, req, null, null, rpcOptions.getRpcConnectTimeoutMs(), rpcExecutor);
return fut.thenApply(msg -> {
ErrorResponse resp = (ErrorResponse) msg;
if (resp != null && resp.errorCode() == 0) {
readyAddresses.add(endpoint.toString());
return true;
} else {
return false;
}
});
}
use of org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse in project ignite-3 by apache.
the class AbstractClientService method invokeWithDone.
public <T extends Message> CompletableFuture<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) {
// TODO asch replace with ignite exception, check all places IGNITE-14832
future.completeExceptionally(new IllegalStateException("Client service is uninitialized."));
// should be in another thread to avoid dead locking.
Utils.runClosureInExecutor(currExecutor, done, new Status(RaftError.EINTERNAL, "Client service is uninitialized."));
return future;
}
return rc.invokeAsync(endpoint, request, ctx, new InvokeCallback() {
@Override
public void complete(final Object result, final Throwable err) {
if (err == null) {
Status status = Status.OK();
Message msg;
if (result instanceof ErrorResponse) {
status = handleErrorResponse((ErrorResponse) result);
msg = (Message) result;
} else {
msg = (Message) 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 {}.", t, request);
}
}
if (!future.isDone()) {
future.complete(msg);
}
} else {
if (ThrowUtil.hasCause(err, null, ConnectException.class))
// Force logical reconnect.
readyAddresses.remove(endpoint.toString());
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 {}.", t, request);
}
}
if (!future.isDone()) {
future.completeExceptionally(err);
}
}
}
@Override
public Executor executor() {
return currExecutor;
}
}, timeoutMs <= 0 ? this.rpcOptions.getRpcDefaultTimeout() : timeoutMs);
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
future.completeExceptionally(e);
// should be in another thread to avoid dead locking.
Utils.runClosureInExecutor(currExecutor, done, new Status(RaftError.EINTR, "Sending rpc was interrupted"));
} catch (final RemotingException e) {
future.completeExceptionally(e);
// should be in another thread to avoid dead locking.
Utils.runClosureInExecutor(currExecutor, done, new Status(RaftError.EINTERNAL, "Fail to send a RPC request:" + e.getMessage()));
}
return future;
}
use of org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse in project ignite-3 by apache.
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);
}
GetPeersRequest req = cliOptions.getRaftMessagesFactory().getPeersRequest().groupId(groupId).leaderId(leaderId.toString()).onlyAlive(onlyGetAlive).build();
try {
final Message result = this.cliClientService.getPeers(leaderId.getEndpoint(), req, 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 Collection<String> responsePeers = returnLearners ? resp.learnersList() : resp.peersList();
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.errorMsg());
}
} catch (final JRaftException e) {
throw e;
} catch (final Exception e) {
throw new JRaftException(e);
}
}
use of org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse in project ignite-3 by apache.
the class NodeRequestProcessorTest method testPeerIdNotFound.
@Test
public void testPeerIdNotFound() {
this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
assertNotNull(resp);
assertEquals(RaftError.ENOENT.getNumber(), resp.errorCode());
assertEquals("Peer id not found: localhost:8081, group: test", resp.errorMsg());
}
use of org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse in project ignite-3 by apache.
the class NodeRequestProcessorTest method testOK.
@Test
public void testOK() {
Node node = Mockito.mock(Node.class, withSettings().extraInterfaces(RaftServerService.class));
Mockito.when(node.getGroupId()).thenReturn("test");
PeerId peerId = new PeerId("localhost", 8081);
Mockito.when(node.getNodeId()).thenReturn(new NodeId("test", peerId));
asyncContext.getNodeManager().add(node);
this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
assertNotNull(resp);
assertEquals(0, resp.errorCode());
}
Aggregations