From 30d542843b842d2e068a305fd01d2c346c8f84a9 Mon Sep 17 00:00:00 2001 From: reubenninan Date: Thu, 8 Aug 2024 19:31:02 +0000 Subject: [PATCH] Do not decrement follower nextIndex below 1 If the current leader receives a unsuccessful append entry, it will decrement the nextIndex for a follower. However, non-leaders can reject append entry requests due to an outdated term and if the nextIndex is already 1 because the replica log is still empty, the leader will try to decrement nextIndex below 1. Reported by Antithesis Run: cfd1ec4bf99273b57cd43d73d8b5216a-17-4 Session: bbe2acf6d61b18536998f059e394f2d9-17-4 --- toy-raft/raft/raft.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/toy-raft/raft/raft.go b/toy-raft/raft/raft.go index 645698b..dc342ec 100644 --- a/toy-raft/raft/raft.go +++ b/toy-raft/raft/raft.go @@ -344,11 +344,16 @@ func (rn *RaftNodeImpl) processOneTransistionInternal(inactivityTimeout time.Dur } else { // NOTE: this only executes if log doesn't match + // minimum next index is 1 + if followerState.nextIndex > 1 { + followerState.nextIndex -= 1 + } + // guard: - if followerState.nextIndex == 1 { - panic(fmt.Sprintf("cannot decrement nextIndex for follower %s below 1", appendEntriesResponse.ResponderId)) + if followerState.nextIndex < 1 { + panic(fmt.Sprintf("nextIndex for follower %s is below 1", appendEntriesResponse.ResponderId)) } - followerState.nextIndex -= 1 + // guard: // BUG: this is being violated sometimes if followerState.nextIndex <= followerState.matchIndex {