From 70f514df743fa6d6cf481777114ede444d61d0fb Mon Sep 17 00:00:00 2001 From: jpadhiyar Date: Sat, 30 Mar 2024 22:52:40 -0300 Subject: [PATCH] Refactor: Extract Method to improve code readability. Method had Cyclomatic complexity: 12 --- .../com/alipay/sofa/jraft/core/NodeImpl.java | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/jraft-core/src/main/java/com/alipay/sofa/jraft/core/NodeImpl.java b/jraft-core/src/main/java/com/alipay/sofa/jraft/core/NodeImpl.java index ae1c0df4a..fe48943ee 100644 --- a/jraft-core/src/main/java/com/alipay/sofa/jraft/core/NodeImpl.java +++ b/jraft-core/src/main/java/com/alipay/sofa/jraft/core/NodeImpl.java @@ -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) { @@ -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 {