Search in sources :

Example 1 with GetPeersResponse

use of com.alipay.sofa.jraft.rpc.CliRequests.GetPeersResponse in project sofa-jraft by sofastack.

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);
    }
    final GetPeersRequest.Builder rb = // 
    GetPeersRequest.newBuilder().setGroupId(// 
    groupId).setLeaderId(// 
    leaderId.toString()).setOnlyAlive(onlyGetAlive);
    try {
        final Message result = this.cliClientService.getPeers(leaderId.getEndpoint(), rb.build(), 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 ProtocolStringList responsePeers = returnLearners ? resp.getLearnersList() : resp.getPeersList();
            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.getErrorMsg());
        }
    } catch (final JRaftException e) {
        throw e;
    } catch (final Exception e) {
        throw new JRaftException(e);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Message(com.google.protobuf.Message) JRaftException(com.alipay.sofa.jraft.error.JRaftException) ArrayList(java.util.ArrayList) GetPeersRequest(com.alipay.sofa.jraft.rpc.CliRequests.GetPeersRequest) ProtocolStringList(com.google.protobuf.ProtocolStringList) JRaftException(com.alipay.sofa.jraft.error.JRaftException) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) GetPeersResponse(com.alipay.sofa.jraft.rpc.CliRequests.GetPeersResponse) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

Status (com.alipay.sofa.jraft.Status)1 PeerId (com.alipay.sofa.jraft.entity.PeerId)1 JRaftException (com.alipay.sofa.jraft.error.JRaftException)1 GetPeersRequest (com.alipay.sofa.jraft.rpc.CliRequests.GetPeersRequest)1 GetPeersResponse (com.alipay.sofa.jraft.rpc.CliRequests.GetPeersResponse)1 ErrorResponse (com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse)1 Message (com.google.protobuf.Message)1 ProtocolStringList (com.google.protobuf.ProtocolStringList)1 ArrayList (java.util.ArrayList)1