Skip to content

Correctly set term field in Raft snapshot metadata - #5598

Merged
generall merged 1 commit into
devfrom
fix-consensus
Dec 6, 2024
Merged

generall merged 1 commit into
devfrom
fix-consensus

Conversation

@ffuugoo

@ffuugoo ffuugoo commented Dec 6, 2024 •

Copy link
Copy Markdown
Contributor

We recently (?) discovered a bug on chaos-testing, when the node might get "stuck" (e.g., consensus on this node does not progress) after applying Raft snapshot.

In the logs on chaos-testing I can see that Raft leader and "stuck" node get into a sort of infinite loop:

  • (let's assume that "stuck" node just recovered Raft snapshot and current commit index on this node is 1337)
  • Raft leader would try to send operation 1337 to the node
    • not sure 100% why it tries to send operation 1337 (instead of 1338) in the first place
    • I think it might be a sanity check, to compare that all indices match after recovering snapshot (see below)
  • the node would reject operation 1337, if term in the received message does not match term in the storage/WAL
  • because our "stuck" node recovered Raft snapshot, there are no entries in the WAL, it is fully compacted
  • there's a "special case" in Storage::term method for this case specifically:
    • if requested index is the same as latest_snapshot_meta index, we return the term from latest_snapshot_meta instead of trying to read it from the WAL
  • but if latest_snapshot_meta (for some reason) contains wrong term for operation 1337, then sanity check would keep rejecting operation 1337 every single time, and the node would get stuck

So, I've checked how we initialize snapshot metadata and tweaked our implementation to match example MemStorage from raft crate.

I did not reproduce the issue locally, but I think I know at least one possible scenario how it could happen: in most cases current commit index matches the current term, but right after new leader is elected, current term would be higher than term of operation at current commit index.

Anyway, this seems like a plausible fix, so I propose we merge this and see if the issue repro on chaos-testing again.

All Submissions:

  • Contributions should target the dev branch. Did you create your branch from dev?
  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

New Feature Submissions:

  1. Does your submission pass tests?
  2. Have you formatted your code locally using cargo +nightly fmt --all command prior to submission?
  3. Have you checked your code using cargo clippy --all --all-features command?

Changes to Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

@ffuugoo
ffuugoo requested review from generall and timvisee December 6, 2024 17:14
@ffuugoo
ffuugoo marked this pull request as ready for review December 6, 2024 18:30
@generall
generall merged commit fffe1a2 into dev Dec 6, 2024
@generall
generall deleted the fix-consensus branch December 6, 2024 18:58
@timvisee timvisee mentioned this pull request Dec 9, 2024
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.

2 participants