Search in sources :

Example 1 with GetLeaderRequest

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();
}
Also used : Status(org.apache.ignite.raft.jraft.Status) Message(org.apache.ignite.raft.jraft.rpc.Message) GetLeaderRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderRequest) GetLeaderResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderResponse) JRaftException(org.apache.ignite.raft.jraft.error.JRaftException) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) ErrorResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse)

Example 2 with GetLeaderRequest

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;
    });
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) GetLeaderRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderRequest) GetLeaderResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderResponse)

Example 3 with GetLeaderRequest

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);
}
Also used : GetLeaderRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderRequest) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) GetLeaderResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderResponse) Test(org.junit.jupiter.api.Test)

Aggregations

GetLeaderRequest (org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderRequest)3 GetLeaderResponse (org.apache.ignite.raft.jraft.rpc.CliRequests.GetLeaderResponse)3 CompletableFuture (java.util.concurrent.CompletableFuture)1 RaftGroupService (org.apache.ignite.raft.client.service.RaftGroupService)1 Status (org.apache.ignite.raft.jraft.Status)1 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)1 JRaftException (org.apache.ignite.raft.jraft.error.JRaftException)1 Message (org.apache.ignite.raft.jraft.rpc.Message)1 ErrorResponse (org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse)1 Test (org.junit.jupiter.api.Test)1