Skip to content

Commit 1997a40

Browse files
committed
Refactor processAppendEntry()
Add recoverAppendEntry() to handle append entries during recovery. This is to separate the normal path from recovery path.
1 parent 33625a5 commit 1997a40

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

server/raft.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ func (s *Server) initRaftNode(accName string, cfg *RaftConfig, labels pprofLabel
512512
truncateAndErr(index - 1)
513513
break
514514
}
515-
n.processAppendEntry(ae, nil)
515+
n.recoverAppendEntry(ae)
516516
// Check how much we have queued up so far to determine if we should pause.
517517
for _, e := range ae.entries {
518518
qsz += len(e.Data)
@@ -3420,9 +3420,19 @@ func (n *raft) updateLeader(newLeader string) {
34203420
}
34213421
}
34223422

3423-
// processAppendEntry will process an appendEntry. This is called either
3424-
// during recovery or from processAppendEntries when there are new entries
3425-
// to be committed.
3423+
// recoverAppendEntry will process an appendEntry for recovery
3424+
func (n *raft) recoverAppendEntry(ae *appendEntry) {
3425+
n.Lock()
3426+
defer n.Unlock()
3427+
3428+
n.pterm = ae.term
3429+
n.pindex = ae.pindex + 1
3430+
3431+
n.processEntriesAndCommit(ae, false)
3432+
}
3433+
3434+
// processAppendEntry will process an appendEntry. This is called from
3435+
// processAppendEntries when there are new entries to be committed.
34263436
func (n *raft) processAppendEntry(ae *appendEntry, sub *subscription) {
34273437
// Make a copy of the reply subject, as ae may return
34283438
// to its pool as part of processAppendEntryLocked

server/raft_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ func TestNRGCandidateDoesntRevertTermAfterOldAE(t *testing.T) {
806806
// the term. Give it to the follower in candidate state.
807807
ae := newAppendEntry(leader.id, 6, leader.commit, leader.pterm, leader.pindex, nil)
808808
follower.switchToCandidate()
809-
follower.processAppendEntry(ae, nil)
809+
follower.processAppendEntry(ae, follower.aesub)
810810

811811
// The candidate must not have reverted back to term 6.
812812
require_NotEqual(t, follower.term, 6)

0 commit comments

Comments
 (0)