Skip to content

Commit

Permalink
Refactor:
Browse files Browse the repository at this point in the history
Extract Method to improve code readability.
Method had Cyclomatic complexity: 12
  • Loading branch information
JAYDIPSINH27 committed Mar 31, 2024
1 parent 39c185e commit 70f514d
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions jraft-core/src/main/java/com/alipay/sofa/jraft/core/NodeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1272,34 +1272,42 @@ private void stepDown(final long term, final boolean wakeupCandidate, final Stat
if (!this.state.isActive()) {
return;
}

handleStateSpecificActions(status);
resetLeaderIdAndState(term, status);
handleReplicatorActions(wakeupCandidate);
handleTransferActions();
restartElectionTimer();
}

private void handleStateSpecificActions(final Status status) {
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
private void resetLeaderIdAndState(long term, Status status) {
resetLeaderId(PeerId.emptyPeer(), status);
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);
}
}

private void handleReplicatorActions(boolean wakeupCandidate) {
if (wakeupCandidate) {
this.wakingCandidate = this.replicatorGroup.stopAllAndFindTheNextCandidate(this.conf);
if (this.wakingCandidate != null) {
Expand All @@ -1308,15 +1316,18 @@ private void stepDown(final long term, final boolean wakeupCandidate, final Stat
} else {
this.replicatorGroup.stopAll();
}
}

private void handleTransferActions() {
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.
}

private void restartElectionTimer() {
if (!isLearner()) {
this.electionTimer.restart();
} else {
Expand Down

0 comments on commit 70f514d

Please sign in to comment.