Skip to content

Commit

Permalink
NRG: Do not revert term on truncate WAL
Browse files Browse the repository at this point in the history
Signed-off-by: Neil Twigg <neil@nats.io>
Co-authored-by: Maurice van Veen <github@mauricevanveen.com>
  • Loading branch information
neilalexander and MauriceVanVeen committed Oct 8, 2024
1 parent c9d0a12 commit b3cd4f5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -3215,7 +3215,7 @@ func (n *raft) truncateWAL(term, index uint64) {
}
}
// Set after we know we have truncated properly.
n.term, n.pterm, n.pindex = term, term, index
n.pterm, n.pindex = term, index
}

// Reset our WAL. This is equivalent to truncating all data from the log.
Expand Down
39 changes: 39 additions & 0 deletions server/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,3 +1007,42 @@ func TestNRGWALEntryWithoutQuorumMustTruncate(t *testing.T) {
return nil
})
}

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

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

l := rg.leader().node().(*raft)
l.term = 20

esm := encodeStreamMsgAllowCompress("foo", "_INBOX.foo", nil, nil, 0, 0, true)
entries := []*Entry{newEntry(EntryNormal, esm)}
l.Lock()
ae := l.buildAppendEntry(entries)
l.Unlock()

for _, f := range rg {
if f.node().ID() != l.ID() {
fn := f.node().(*raft)
fn.processAppendEntry(ae, fn.aesub)
require_Equal(t, fn.term, 20) // Follower's term gets upped as expected.
}
}

// Lower the term, simulating the followers receiving a message from an old term/leader.
ae.term = 3
for _, f := range rg {
if f.node().ID() != l.ID() {
fn := f.node().(*raft)
fn.processAppendEntry(ae, fn.aesub)
require_Equal(t, fn.term, 20) // Follower should reject and the term stays the same.

fn.resetWAL()
fn.processAppendEntry(ae, fn.aesub)
require_Equal(t, fn.term, 20) // Follower should reject again, even after reset, term stays the same.
}
}
}

0 comments on commit b3cd4f5

Please sign in to comment.