Skip to content
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
17 changes: 11 additions & 6 deletions server/jetstream_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3185,7 +3185,7 @@ func (js *jetStream) monitorStream(mset *stream, sa *streamAssignment, sendSnaps
// If the error signals we timed out of a snapshot, we should try to replay the snapshot
// instead of fully resetting the state. Resetting the clustered state may result in
// race conditions and should only be used as a last effort attempt.
if errors.Is(err, errCatchupAbortedNoLeader) || err == errCatchupTooManyRetries {
if errors.Is(err, errCatchupAbortedNoLeader) || err == errCatchupTooManyRetries || err == errAlreadyLeader {
if node := mset.raftNode(); node != nil && node.DrainAndReplaySnapshot() {
break
}
Expand Down Expand Up @@ -3997,10 +3997,8 @@ func (js *jetStream) applyStreamEntries(mset *stream, ce *CommittedEntry, isReco
}
}

if isRecovering || !mset.IsLeader() {
if err := mset.processSnapshot(ss, ce.Index); err != nil && err != errAlreadyLeader {
return 0, err
}
if err := mset.processSnapshot(ss, ce.Index); err != nil {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Skip stale snapshot state when leader is already ahead

Calling processSnapshot() unconditionally on leaders re-applies snapshot metadata even when the leader does not need catchup. In processSnapshot, mset.setCLFS(snap.Failed) runs before the up-to-date check, so a SendSnapshot captured slightly behind current applied state can rewind CLFS on the leader. If intervening applied entries had already incremented CLFS, the next clustered message apply can hit the lseq != mset.lseq + clfs check in processJetStreamMsgWithBatch and return errLastSeqMismatch, which drives the cluster-reset path. This only appears under scale-up/leader traffic races, but it is a real regression from previously skipping leader-side snapshot processing.

Useful? React with 👍 / 👎.

return 0, err
}
} else if e.Type == EntryRemovePeer {
js.mu.RLock()
Expand Down Expand Up @@ -9925,7 +9923,14 @@ func (mset *stream) processSnapshot(snap *StreamReplicatedState, index uint64) (

// Pause the apply channel for our raft group while we catch up.
if err := n.PauseApply(); err != nil {
return err
// The only reason PauseApply can fail is due to errAlreadyLeader.
// We step down to get someone else to become the leader that can catch us up.
// Ignore the error since we could have already stepped down before us doing so here.
_ = n.StepDown()
// Now try pausing again and continue to catchup.
if err = n.PauseApply(); err != nil {
return err
}
}

// Set our catchup state.
Expand Down
40 changes: 40 additions & 0 deletions server/jetstream_cluster_3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8949,3 +8949,43 @@ func TestJetStreamClusterInterestStreamMsgWithNoInterestStillAppliesRollup(t *te
t.Run(fmt.Sprintf("R%d", replicas), func(t *testing.T) { test(t, replicas) })
}
}

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

nc, js := jsClientConnect(t, c.randomServer())
defer nc.Close()

_, err := js.AddStream(&nats.StreamConfig{
Name: "TEST",
Subjects: []string{"foo"},
Replicas: 3,
})
require_NoError(t, err)

// Publish a message and ensure everyone is synced up.
_, err = js.Publish("foo", nil)
require_NoError(t, err)
checkFor(t, 2*time.Second, 200*time.Millisecond, func() error {
return checkState(t, c, globalAccountName, "TEST")
})

// Get the current stream leader.
sl := c.streamLeader(globalAccountName, "TEST")
require_NotNil(t, sl)
mset, err := sl.globalAccount().lookupStream("TEST")
require_NoError(t, err)
rn := mset.raftNode()

// Grab the current state of the leader which contains the message we've published.
snap := mset.stateSnapshot()
// Truncate this leader's store to be empty, while remaining Raft leader.
require_NoError(t, mset.store.Truncate(0))
// Send the snapshot containing the message. Even though we're Raft leader, we must
// still check we're up-to-date and if not: step down and catch up.
require_NoError(t, rn.SendSnapshot(snap))
checkFor(t, 10*time.Second, 200*time.Millisecond, func() error {
return checkState(t, c, globalAccountName, "TEST")
})
}
4 changes: 2 additions & 2 deletions server/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1133,8 +1133,8 @@ func (n *raft) PauseApply() error {
}

func (n *raft) pauseApplyLocked() {
// If we are currently a candidate make sure we step down.
if n.State() == Candidate {
// If we are currently not a follower, make sure we step down.
if n.State() != Follower {
n.stepdownLocked(noLeader)
}

Expand Down
2 changes: 1 addition & 1 deletion server/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ func isOutOfSpaceErr(err error) bool {
var errFirstSequenceMismatch = errors.New("first sequence mismatch")

func isClusterResetErr(err error) bool {
return err == errLastSeqMismatch || err == ErrStoreEOF || err == errFirstSequenceMismatch || errors.Is(err, errCatchupAbortedNoLeader) || err == errCatchupTooManyRetries
return err == errLastSeqMismatch || err == ErrStoreEOF || err == errFirstSequenceMismatch || errors.Is(err, errCatchupAbortedNoLeader) || err == errCatchupTooManyRetries || err == errAlreadyLeader
}

// Copy all fields.
Expand Down
Loading