Search in sources :

Example 1 with GetLeaderResponse

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

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;
        }
        final GetLeaderRequest.Builder rb = // 
        GetLeaderRequest.newBuilder().setGroupId(// 
        groupId).setPeerId(peer.toString());
        final Future<Message> result = this.cliClientService.getLeader(peer.getEndpoint(), rb.build(), 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).getErrorMsg());
                } else {
                    final String savedMsg = st.getErrorMsg();
                    st.setError(-1, "%s, %s", savedMsg, ((ErrorResponse) msg).getErrorMsg());
                }
            } else {
                final GetLeaderResponse response = (GetLeaderResponse) msg;
                if (leaderId.parse(response.getLeaderId())) {
                    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(com.alipay.sofa.jraft.Status) Message(com.google.protobuf.Message) GetLeaderRequest(com.alipay.sofa.jraft.rpc.CliRequests.GetLeaderRequest) GetLeaderResponse(com.alipay.sofa.jraft.rpc.CliRequests.GetLeaderResponse) JRaftException(com.alipay.sofa.jraft.error.JRaftException) PeerId(com.alipay.sofa.jraft.entity.PeerId) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse)

Aggregations

Status (com.alipay.sofa.jraft.Status)1 PeerId (com.alipay.sofa.jraft.entity.PeerId)1 JRaftException (com.alipay.sofa.jraft.error.JRaftException)1 GetLeaderRequest (com.alipay.sofa.jraft.rpc.CliRequests.GetLeaderRequest)1 GetLeaderResponse (com.alipay.sofa.jraft.rpc.CliRequests.GetLeaderResponse)1 ErrorResponse (com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse)1 Message (com.google.protobuf.Message)1