Skip to content

Commit

Permalink
NRG: Invalidate pending append entries cache (#6513)
Browse files Browse the repository at this point in the history
The `n.pae` is an in-memory cache of pending but not yet applied
entries. When applying commits we can pull from this cache so we don't
need to pull them from disk for example. However, the cache has a
bounded size. So if the cache would be fully filled and we'd store a
different entry at an index that was cached, we'd apply the wrong
(cached) entry.

If we get an entry that we can't cache because it's full, we can simply
drop the entry from the cache if it exists. If an entry at this index
doesn't exist it's a noop, but if it did exist then it clears up room in
the cache for the next entries to be stored.

Signed-off-by: Maurice van Veen <[email protected]>
  • Loading branch information
derekcollison authored Feb 15, 2025
2 parents 9e3464c + fec8a68 commit 3c575bf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
9 changes: 7 additions & 2 deletions server/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -3567,8 +3567,13 @@ CONTINUE:
if l > paeWarnThreshold && l%paeWarnModulo == 0 {
n.warn("%d append entries pending", len(n.pae))
}
} else if l%paeWarnModulo == 0 {
n.debug("Not saving to append entries pending")
} else {
// Invalidate cache entry at this index, we might have
// stored it previously with a different value.
delete(n.pae, n.pindex)
if l%paeWarnModulo == 0 {
n.debug("Not saving to append entries pending")
}
}
} else {
// This is a replay on startup so just take the appendEntry version.
Expand Down
2 changes: 1 addition & 1 deletion server/raft_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func (a *stateAdder) snapshot(t *testing.T) {
// Helper to wait for a certain state.
func (rg smGroup) waitOnTotal(t *testing.T, expected int64) {
t.Helper()
checkFor(t, 20*time.Second, 200*time.Millisecond, func() error {
checkFor(t, 5*time.Second, 200*time.Millisecond, func() error {
for _, sm := range rg {
asm := sm.(*stateAdder)
if total := asm.total(); total != expected {
Expand Down
37 changes: 37 additions & 0 deletions server/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,43 @@ func TestNRGTermNoDecreaseAfterWALReset(t *testing.T) {
}
}

func TestNRGPendingAppendEntryCacheInvalidation(t *testing.T) {
for _, test := range []struct {
title string
entries int
}{
{title: "empty", entries: 1},
{title: "at limit", entries: paeDropThreshold},
{title: "full", entries: paeDropThreshold + 1},
} {
t.Run(test.title, func(t *testing.T) {
c := createJetStreamClusterExplicit(t, "R3S", 3)
defer c.shutdown()

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

l.(*stateAdder).proposeDelta(1)
rg.waitOnTotal(t, 1)

// Fill up the cache with N entries.
// The contents don't matter as they should never be applied.
rg.lockAll()
for _, s := range rg {
n := s.node().(*raft)
for i := 0; i < test.entries; i++ {
n.pae[n.pindex+uint64(1+i)] = newAppendEntry("", 0, 0, 0, 0, nil)
}
}
rg.unlockAll()

l.(*stateAdder).proposeDelta(1)
rg.waitOnTotal(t, 2)
})
}
}

func TestNRGCatchupDoesNotTruncateUncommittedEntriesWithQuorum(t *testing.T) {
n, cleanup := initSingleMemRaftNode(t)
defer cleanup()
Expand Down

0 comments on commit 3c575bf

Please sign in to comment.