use of org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderRequest in project ignite-3 by apache.
the class CliServiceImpl method getLeader.
@Override
public Status getLeader(final String groupId, final Configuration conf, final PeerId leaderId) {
Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
Requires.requireNonNull(leaderId, "Null leader id");
if (conf == null || conf.isEmpty()) {
return new Status(RaftError.EINVAL, "Empty group configuration");
}
final Status st = new Status(-1, "Fail to get leader of group %s", groupId);
for (final PeerId peer : conf) {
if (!this.cliClientService.connect(peer.getEndpoint())) {
LOG.error("Fail to connect peer {} to get leader for group {}.", peer, groupId);
continue;
}
GetLeaderRequest rb = cliOptions.getRaftMessagesFactory().getLeaderRequest().groupId(groupId).peerId(peer.toString()).build();
final Future<Message> result = this.cliClientService.getLeader(peer.getEndpoint(), rb, null);
try {
final Message msg = result.get(this.cliOptions.getTimeoutMs() <= 0 ? this.cliOptions.getRpcDefaultTimeout() : this.cliOptions.getTimeoutMs(), TimeUnit.MILLISECONDS);
if (msg instanceof ErrorResponse) {
if (st.isOk()) {
st.setError(-1, ((ErrorResponse) msg).errorMsg());
} else {
final String savedMsg = st.getErrorMsg();
st.setError(-1, "%s, %s", savedMsg, ((ErrorResponse) msg).errorMsg());
}
} else {
final GetLeaderResponse response = (GetLeaderResponse) msg;
if (leaderId.parse(response.leaderId())) {
break;
}
}
} catch (final Exception e) {
if (st.isOk()) {
st.setError(-1, e.getMessage());
} else {
final String savedMsg = st.getErrorMsg();
st.setError(-1, "%s, %s", savedMsg, e.getMessage());
}
}
}
if (leaderId.isEmpty()) {
return st;
}
return Status.OK();
}
use of org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderRequest in project ignite-3 by apache.
the class RaftGroupServiceImpl method refreshLeader.
/**
* {@inheritDoc}
*/
@Override
public CompletableFuture<Void> refreshLeader() {
GetLeaderRequest req = factory.getLeaderRequest().groupId(groupId).build();
CompletableFuture<GetLeaderResponse> fut = new CompletableFuture<>();
sendWithRetry(randomNode(), req, currentTimeMillis() + timeout, fut);
return fut.thenApply(resp -> {
leader = parsePeer(resp.leaderId());
return null;
});
}
use of org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderRequest in project ignite-3 by apache.
the class RaftGroupServiceTest method testGetLeaderRequest.
/**
*/
@Test
public void testGetLeaderRequest() throws Exception {
String groupId = "test";
mockLeaderRequest(false);
RaftGroupService service = RaftGroupServiceImpl.start(groupId, cluster, FACTORY, TIMEOUT, NODES, false, DELAY, executor).get(3, TimeUnit.SECONDS);
assertNull(service.leader());
service.refreshLeader().get();
GetLeaderRequest req = FACTORY.getLeaderRequest().groupId(groupId).build();
GetLeaderResponse fut = (GetLeaderResponse) messagingService.invoke(leader.address(), req, TIMEOUT).get();
assertEquals(fut.leaderId(), PeerId.fromPeer(leader).toString());
assertEquals(fut.currentTerm(), CURRENT_TERM);
}
Aggregations