Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NRG (2.11): Ensure proposal and AE response queues drain after stepdown #5666

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions server/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1989,9 +1989,11 @@ func (n *raft) runAsFollower() {
n.debug("Ignoring old vote response, we have stepped down")
n.votes.popOne()
case <-n.resp.ch:
// We're receiving append entry responses from the network, probably because
// we have only just stepped down and they were already in flight. Ignore them.
n.resp.popOne()
// Ignore append entry responses received from before the state change.
n.resp.drain()
case <-n.prop.ch:
// Ignore proposals received from before the state change.
n.prop.drain()
case <-n.reqs.ch:
// We've just received a vote request from the network.
// Because of drain() it is possible that we get nil from popOne().
Expand Down Expand Up @@ -2966,8 +2968,11 @@ func (n *raft) runAsCandidate() {
case <-n.entry.ch:
n.processAppendEntries()
case <-n.resp.ch:
// Ignore
n.resp.popOne()
// Ignore append entry responses received from before the state change.
n.resp.drain()
case <-n.prop.ch:
// Ignore proposals received from before the state change.
n.prop.drain()
case <-n.s.quitCh:
n.shutdown(false)
return
Expand Down Expand Up @@ -4089,8 +4094,9 @@ func (n *raft) switchState(state RaftState) {

if pstate == Leader && state != Leader {
n.updateLeadChange(false)
// Drain the response queue.
// Drain the append entry response and proposal queues.
n.resp.drain()
n.prop.drain()
} else if state == Leader && pstate != Leader {
if len(n.pae) > 0 {
n.pae = make(map[uint64]*appendEntry)
Expand Down
22 changes: 22 additions & 0 deletions server/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,28 @@ func TestNRGSimpleElection(t *testing.T) {
}
}

func TestNRGSwitchStateClearsQueues(t *testing.T) {
c := createJetStreamClusterExplicit(t, "R3S", 3)
defer c.shutdown()

rg := c.createMemRaftGroup("TEST", 3, newStateAdder)
rg.waitOnLeader()

sa := rg.leader().(*stateAdder)
n := sa.node().(*raft)

for i := 0; i < 10_000; i++ {
sa.proposeDelta(1)
}

n.Lock()
defer n.Unlock()

n.switchState(Follower)
require_Equal(t, n.prop.len(), 0)
require_Equal(t, n.resp.len(), 0)
}

func TestNRGStepDownOnSameTermDoesntClearVote(t *testing.T) {
c := createJetStreamClusterExplicit(t, "R3S", 3)
defer c.shutdown()
Expand Down