Search in sources :

Example 1 with JRaftException

use of org.apache.ignite.raft.jraft.error.JRaftException 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);
    }
}
Also used : Status(org.apache.ignite.raft.jraft.Status) Message(org.apache.ignite.raft.jraft.rpc.Message) JRaftException(org.apache.ignite.raft.jraft.error.JRaftException) ArrayList(java.util.ArrayList) GetPeersRequest(org.apache.ignite.raft.jraft.rpc.CliRequests.GetPeersRequest) JRaftException(org.apache.ignite.raft.jraft.error.JRaftException) ErrorResponse(org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse) GetPeersResponse(org.apache.ignite.raft.jraft.rpc.CliRequests.GetPeersResponse) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Aggregations

ArrayList (java.util.ArrayList)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 GetPeersRequest (org.apache.ignite.raft.jraft.rpc.CliRequests.GetPeersRequest)1 GetPeersResponse (org.apache.ignite.raft.jraft.rpc.CliRequests.GetPeersResponse)1 Message (org.apache.ignite.raft.jraft.rpc.Message)1 ErrorResponse (org.apache.ignite.raft.jraft.rpc.RpcRequests.ErrorResponse)1