Java Code Examples for com.alipay.sofa.jraft.entity.PeerId#emptyPeer()

The following examples show how to use com.alipay.sofa.jraft.entity.PeerId#emptyPeer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CliServiceImpl.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
private PeerId findTargetPeer(final PeerId self, final String groupId, final Configuration conf,
                              final LeaderCounter leaderCounter) {
    for (final PeerId peerId : getAlivePeers(groupId, conf)) {
        if (peerId.equals(self)) {
            continue;
        }
        if (leaderCounter.get(peerId) >= leaderCounter.getExpectedAverage()) {
            continue;
        }
        return peerId;
    }
    return PeerId.emptyPeer();
}
 
Example 2
Source File: NodeImpl.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
private void resetLeaderId(final PeerId newLeaderId, final Status status) {
    if (newLeaderId.isEmpty()) {
        if (!this.leaderId.isEmpty() && this.state.compareTo(State.STATE_TRANSFERRING) > 0) {
            this.fsmCaller.onStopFollowing(new LeaderChangeContext(this.leaderId.copy(), this.currTerm, status));
        }
        this.leaderId = PeerId.emptyPeer();
    } else {
        if (this.leaderId == null || this.leaderId.isEmpty()) {
            this.fsmCaller.onStartFollowing(new LeaderChangeContext(newLeaderId, this.currTerm, status));
        }
        this.leaderId = newLeaderId.copy();
    }
}
 
Example 3
Source File: NodeImpl.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
private void stepDown(final long term, final boolean wakeupCandidate, final Status status) {
    LOG.debug("Node {} stepDown, term={}, newTerm={}, wakeupCandidate={}.", getNodeId(), this.currTerm, term,
        wakeupCandidate);
    if (!this.state.isActive()) {
        return;
    }
    if (this.state == State.STATE_CANDIDATE) {
        stopVoteTimer();
    } else if (this.state.compareTo(State.STATE_TRANSFERRING) <= 0) {
        stopStepDownTimer();
        this.ballotBox.clearPendingTasks();
        // signal fsm leader stop immediately
        if (this.state == State.STATE_LEADER) {
            onLeaderStop(status);
        }
    }
    // reset leader_id
    resetLeaderId(PeerId.emptyPeer(), status);

    // soft state in memory
    this.state = State.STATE_FOLLOWER;
    this.confCtx.reset();
    updateLastLeaderTimestamp(Utils.monotonicMs());
    if (this.snapshotExecutor != null) {
        this.snapshotExecutor.interruptDownloadingSnapshots(term);
    }

    // meta state
    if (term > this.currTerm) {
        this.currTerm = term;
        this.votedId = PeerId.emptyPeer();
        this.metaStorage.setTermAndVotedFor(term, this.votedId);
    }

    if (wakeupCandidate) {
        this.wakingCandidate = this.replicatorGroup.stopAllAndFindTheNextCandidate(this.conf);
        if (this.wakingCandidate != null) {
            Replicator.sendTimeoutNowAndStop(this.wakingCandidate, this.options.getElectionTimeoutMs());
        }
    } else {
        this.replicatorGroup.stopAll();
    }
    if (this.stopTransferArg != null) {
        if (this.transferTimer != null) {
            this.transferTimer.cancel(true);
        }
        // There is at most one StopTransferTimer at the same term, it's safe to
        // mark stopTransferArg to NULL
        this.stopTransferArg = null;
    }
    // Learner node will not trigger the election timer.
    if (!isLearner()) {
        this.electionTimer.restart();
    } else {
        LOG.info("Node {} is a learner, election timer is not started.", this.nodeId);
    }
}