Search in sources :

Example 81 with Status

use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.

the class Replicator method onHeartbeatReturned.

static void onHeartbeatReturned(final ThreadId id, final Status status, final AppendEntriesRequest request, final AppendEntriesResponse response, final long rpcSendTime) {
    if (id == null) {
        // replicator already was destroyed.
        return;
    }
    final long startTimeMs = Utils.nowMs();
    Replicator r;
    if ((r = (Replicator) id.lock()) == null) {
        return;
    }
    boolean doUnlock = true;
    try {
        final boolean isLogDebugEnabled = LOG.isDebugEnabled();
        StringBuilder sb = null;
        if (isLogDebugEnabled) {
            sb = // 
            new StringBuilder("Node ").append(// 
            r.options.getGroupId()).append(// 
            ':').append(// 
            r.options.getServerId()).append(// 
            " received HeartbeatResponse from ").append(// 
            r.options.getPeerId()).append(// 
            " prevLogIndex=").append(// 
            request.getPrevLogIndex()).append(// 
            " prevLogTerm=").append(request.getPrevLogTerm());
        }
        if (!status.isOk()) {
            if (isLogDebugEnabled) {
                // 
                sb.append(" fail, sleep, status=").append(status);
                LOG.debug(sb.toString());
            }
            r.setState(State.Probe);
            notifyReplicatorStatusListener(r, ReplicatorEvent.ERROR, status);
            if (++r.consecutiveErrorTimes % 10 == 0) {
                LOG.warn("Fail to issue RPC to {}, consecutiveErrorTimes={}, error={}", r.options.getPeerId(), r.consecutiveErrorTimes, status);
            }
            r.startHeartbeatTimer(startTimeMs);
            return;
        }
        r.consecutiveErrorTimes = 0;
        if (response.getTerm() > r.options.getTerm()) {
            if (isLogDebugEnabled) {
                // 
                sb.append(" fail, greater term ").append(// 
                response.getTerm()).append(// 
                " expect term ").append(r.options.getTerm());
                LOG.debug(sb.toString());
            }
            final NodeImpl node = r.options.getNode();
            r.notifyOnCaughtUp(RaftError.EPERM.getNumber(), true);
            r.destroy();
            node.increaseTermTo(response.getTerm(), new Status(RaftError.EHIGHERTERMRESPONSE, "Leader receives higher term heartbeat_response from peer:%s", r.options.getPeerId()));
            return;
        }
        if (!response.getSuccess() && response.hasLastLogIndex()) {
            if (isLogDebugEnabled) {
                // 
                sb.append(" fail, response term ").append(// 
                response.getTerm()).append(// 
                " lastLogIndex ").append(response.getLastLogIndex());
                LOG.debug(sb.toString());
            }
            LOG.warn("Heartbeat to peer {} failure, try to send a probe request.", r.options.getPeerId());
            doUnlock = false;
            r.sendProbeRequest();
            r.startHeartbeatTimer(startTimeMs);
            return;
        }
        if (isLogDebugEnabled) {
            LOG.debug(sb.toString());
        }
        if (rpcSendTime > r.lastRpcSendTimestamp) {
            r.lastRpcSendTimestamp = rpcSendTime;
        }
        r.startHeartbeatTimer(startTimeMs);
    } finally {
        if (doUnlock) {
            id.unlock();
        }
    }
}
Also used : Status(com.alipay.sofa.jraft.Status)

Example 82 with Status

use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.

the class CliServiceImpl method transferLeader.

@Override
public Status transferLeader(final String groupId, final Configuration conf, final PeerId peer) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
    Requires.requireNonNull(conf, "Null configuration");
    Requires.requireNonNull(peer, "Null peer");
    final PeerId leaderId = new PeerId();
    final Status st = checkLeaderAndConnect(groupId, conf, leaderId);
    if (!st.isOk()) {
        return st;
    }
    final TransferLeaderRequest.Builder rb = // 
    TransferLeaderRequest.newBuilder().setGroupId(// 
    groupId).setLeaderId(leaderId.toString());
    if (!peer.isEmpty()) {
        rb.setPeerId(peer.toString());
    }
    try {
        final Message result = this.cliClientService.transferLeader(leaderId.getEndpoint(), rb.build(), null).get();
        return statusFromResponse(result);
    } catch (final Exception e) {
        return new Status(-1, e.getMessage());
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Message(com.google.protobuf.Message) TransferLeaderRequest(com.alipay.sofa.jraft.rpc.CliRequests.TransferLeaderRequest) JRaftException(com.alipay.sofa.jraft.error.JRaftException) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 83 with Status

use of com.alipay.sofa.jraft.Status 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)

Example 84 with Status

use of com.alipay.sofa.jraft.Status 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)

Example 85 with Status

use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.

the class CliServiceImpl method removeLearners.

@Override
public Status removeLearners(final String groupId, final Configuration conf, final List<PeerId> learners) {
    checkLearnersOpParams(groupId, conf, learners);
    final PeerId leaderId = new PeerId();
    final Status st = getLeader(groupId, conf, leaderId);
    if (!st.isOk()) {
        return st;
    }
    if (!this.cliClientService.connect(leaderId.getEndpoint())) {
        return new Status(-1, "Fail to init channel to leader %s", leaderId);
    }
    final RemoveLearnersRequest.Builder rb = // 
    RemoveLearnersRequest.newBuilder().setGroupId(// 
    groupId).setLeaderId(leaderId.toString());
    for (final PeerId peer : learners) {
        rb.addLearners(peer.toString());
    }
    try {
        final Message result = this.cliClientService.removeLearners(leaderId.getEndpoint(), rb.build(), null).get();
        return processLearnersOpResponse(groupId, result, "removing learners: %s", learners);
    } catch (final Exception e) {
        return new Status(-1, e.getMessage());
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) RemoveLearnersRequest(com.alipay.sofa.jraft.rpc.CliRequests.RemoveLearnersRequest) Message(com.google.protobuf.Message) JRaftException(com.alipay.sofa.jraft.error.JRaftException) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

Status (com.alipay.sofa.jraft.Status)213 Test (org.junit.Test)63 PeerId (com.alipay.sofa.jraft.entity.PeerId)55 CountDownLatch (java.util.concurrent.CountDownLatch)36 BaseKVStoreClosure (com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure)33 Configuration (com.alipay.sofa.jraft.conf.Configuration)25 Message (com.google.protobuf.Message)24 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)22 ArrayList (java.util.ArrayList)22 Node (com.alipay.sofa.jraft.Node)21 Closure (com.alipay.sofa.jraft.Closure)17 Task (com.alipay.sofa.jraft.entity.Task)17 ByteBuffer (java.nio.ByteBuffer)16 Endpoint (com.alipay.sofa.jraft.util.Endpoint)15 List (java.util.List)15 RaftException (com.alipay.sofa.jraft.error.RaftException)14 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)12 LogId (com.alipay.sofa.jraft.entity.LogId)12 KVStoreClosure (com.alipay.sofa.jraft.rhea.storage.KVStoreClosure)12 JRaftException (com.alipay.sofa.jraft.error.JRaftException)11