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): Mark n.Leader() when complete with applied floor #6518

Merged
merged 3 commits into from
Feb 19, 2025

Conversation

MauriceVanVeen
Copy link
Member

Follow-up of #6485

After above PR the RAFT state would become Leader as normal, but a call to updateLeadChange would be delayed until the server is up-to-date with all stored but unapplied entries from its log. The intention of this is to not respond to read/write requests for meta/stream/consumer until the leader is in a complete state, which ensures consistent handling of requests after leader changes.

However, calls like isLeader, isStreamLeader, and isConsumerLeader would request n.Leader() which returns whether the RAFT node is leader. And it would not use the signal sent to updateLeadChange.

To achieve this level of consistency with minimal code changes:

  • n.Leader() only returns true once the RAFT node is both a leader, and all initial entries from its log were applied.
  • Calls to n.StepDown() would be preceded by n.Leader(), however with it being changed to not just return whether the RAFT node is leader this is not possible anymore. Instead we can always just call n.StepDown() without checking for being leader. As the RAFT code itself already checks for this as well.

Some test de-flakes are included as well.

Signed-off-by: Maurice van Veen github@mauricevanveen.com

Signed-off-by: Maurice van Veen <github@mauricevanveen.com>
Signed-off-by: Maurice van Veen <github@mauricevanveen.com>
node.StepDown(preferredLeader)

// Call actual stepdown.
err = node.StepDown(preferredLeader)
Copy link
Member Author

Choose a reason for hiding this comment

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

No need to call StepDown in a go routine after a 250ms delay when stepping down stream/consumer.
This was introduced in #3079, to not process messages while stepping down. This is not needed anymore, as the RAFT logic itself will take care of that.

return nil
}
// If we are no longer the leader stop trying.
if !mset.isLeader() {
Copy link
Member Author

Choose a reason for hiding this comment

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

De-flake for TestJetStreamSuperClusterSourceAndMirrorConsumersLeaderChange as it would still create a mirror consumer even if the stream was not a leader anymore after being stepped down.

// Make sure this is not a new consumer with the same name.
if nca != nil && nca == ca {
// Make sure this is the same consumer assignment, and not a new consumer with the same name.
if nca != nil && reflect.DeepEqual(nca, ca) {
Copy link
Member Author

Choose a reason for hiding this comment

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

De-flakes TestJetStreamClusterGhostEphemeralsAfterRestart which would flake with Still have %d missing consumers. This would happen because a server could come online and need to catchup on some consumer deletes. But the leader had made a snapshot, making this server process that snapshot, which overwrites the consumer assignment to have the exact same config and RAFT group, but using different pointers. That would leave these consumer assignments around, whereas they should have been deleted still.

@MauriceVanVeen MauriceVanVeen changed the title NRG: Mark n.Leader() when complete with applied floor NRG (2.11): Mark n.Leader() when complete with applied floor Feb 17, 2025
Signed-off-by: Maurice van Veen <github@mauricevanveen.com>
@MauriceVanVeen MauriceVanVeen marked this pull request as ready for review February 19, 2025 12:10
@MauriceVanVeen MauriceVanVeen requested a review from a team as a code owner February 19, 2025 12:10
Copy link
Member

@neilalexander neilalexander left a comment

Choose a reason for hiding this comment

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

LGTM, have discussed this one with Maurice.

@derekcollison derekcollison merged commit 9782bf7 into main Feb 19, 2025
5 checks passed
@derekcollison derekcollison deleted the maurice/nrg-consistency branch February 19, 2025 13:42
neilalexander added a commit that referenced this pull request Mar 10, 2025
Test could fail with:
```
jetstream_cluster_4_test.go:201: Should have dropped message published on "messages.3553" since got error: nats: timeout
```

This was due to a regression introduced by
#6518, since we now wait to
return `isLeader()` until we're fully up-to-date with our initial log
when becoming new leader. But we could have some entries from a previous
leader that weren't applied yet, which would result in timeouts because
we wouldn't respond under the new logic. So don't check `n.Leader()`
state, but just RAFT leader state to respond in `processJetStreamMsg`.

But it could also genuinely timeout, in which case the test shouldn't
fail either.

Signed-off-by: Maurice van Veen <github@mauricevanveen.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants