Skip to content

Optimize consensus loop - #5728

Merged
ffuugoo merged 4 commits into
devfrom
optimize-consensus-loop
Jan 28, 2025
Merged

ffuugoo merged 4 commits into
devfrom
optimize-consensus-loop

Conversation

@ffuugoo

@ffuugoo ffuugoo commented Jan 3, 2025 •

Copy link
Copy Markdown
Contributor

This PR fixes two non-critical performance bugs in consensus loop:

Node that receives constant stream of consensus updates will trigger excessive Raft elections

Our consensus loop is structured in the following way:

There's a corner case with this loop structure:

  1. Imagine we have a node that receives a constant stream of updates. E.g., it's a new node, or it was offline and needs to catch up with the cluster.
  2. Such node receives a constant stream of updates from the leader, so on each iteration of consensus loop we would propagate a full batch of messages to Raft node, and then process on_ready for all these messages.
  3. Processing on_ready will often take more time, than election timeout.
  4. So, on next iteration of the loop, we will tick the node too many times and trigger leader election.
  5. And this may happen multiple times while the node is catching up with consensus. It's not critical for correctness or consistency, but it slows consensus down a lot.

To fix this issue, we check if any new Raft messages were received since last iteration of the loop, and limit the number of times we tick Raft node.

From my tests, Raft crate is not super reliant on ticks: it works even if you never tick any node, as long as you established a leader and all nodes are online, so this should be safe.

`try_sync_local_state` is not always triggered reliably

While working on the first bug in the PR, I discovered that sometimes the try_sync_local_state might not be triggered for multiple seconds on a node.

  1. We trigger try_sync_local_state, if during an iteration of consensus loop, no messages were received and no Ready state produced by Raft node.
  2. This is a valid approach, but it's overly strict.
  3. Raft nodes exchange heartbeats every 2 ticks. So, every 2 iterations of consensus loop, a heartbeat message is received and Ready state is produced to send a heartbeat-response message.
  4. And I've seen cases, when consensus loop would get into a "lockstep" with heartbeats: every iteration of the loop would take 2 ticks (not sure why exactly, maybe because on_ready would take a bit too long to process, or due to timeouts not being perfectly precise), and so for multipe iterations of the loop, each iteration would receive a message and/or produce Ready state, and try_sync_local_state might not be triggered for seconds.

To fix this, I reworked how we trigger `try_sync_local_state:

  1. on_ready now returns is_idle flag.
  2. Raft node is "idle", if during on_ready Raft state was not updated. E.g., no new entries appended to WAL, no entries committed, no Raft snapshot applied. The only idle operation during on_ready is sending outgoing Raft messages.
  3. We trigger try_sync_local_state, if the node was idle for a few consecutive iterations of consensus loop.

This way, we can reliably detect when node caught up with consensus (and heartbeats don't affect this check).

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 changed the title Optimize consensus loop Prevent excessive re-elections during consensus sync Jan 6, 2025
@ffuugoo
ffuugoo force-pushed the optimize-consensus-loop branch from 3464252 to a7799ac Compare January 6, 2025 16:39
@ffuugoo ffuugoo changed the title Prevent excessive re-elections during consensus sync Optimize consensus loop Jan 8, 2025
@ffuugoo
ffuugoo requested a review from generall January 8, 2025 16:39
@ffuugoo
ffuugoo marked this pull request as ready for review January 8, 2025 16:39
@ffuugoo
ffuugoo removed the request for review from generall January 8, 2025 16:48
@ffuugoo
ffuugoo marked this pull request as draft January 8, 2025 16:48
Comment thread Cargo.toml
license = "Apache-2.0"
edition = "2021"
rust-version = "1.77"
rust-version = "1.80"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Required for div_duration_f32 that I now use in consensus loop.

@ffuugoo
ffuugoo marked this pull request as ready for review January 8, 2025 20:31
@ffuugoo
ffuugoo requested a review from generall January 8, 2025 20:31

@timvisee timvisee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I did go through this, but I find it quite hard to keep track on what all the changes do exactly.

I agree that testing this on choas tesing for a while is a good idea, just to be sure there's no obvious problem. We can start testing after releasing 1.13.

Comment thread src/consensus.rs Resolved
Comment thread src/consensus.rs Resolved
Comment thread src/consensus.rs Resolved
@ffuugoo

ffuugoo commented Jan 17, 2025

Copy link
Copy Markdown
Contributor Author

Qdrant 1.13 is released, so we can review and merge this now.

ack @generall @timvisee

@ffuugoo
ffuugoo force-pushed the optimize-consensus-loop branch from b4f5ba1 to 19c80dd Compare January 17, 2025 16:33
@ffuugoo
ffuugoo requested review from generall and timvisee January 17, 2025 16:34

@timvisee timvisee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There's a lot going on here. Though I don't see any obvious problems I'm not confident I got all possible edge cases here in these changes.

As said before, let's test this on chaos testing for a while. We can merge this once we've released 1.13.1 which will be in one or two days.

Comment thread src/consensus.rs Resolved
Comment thread src/consensus.rs Resolved
@ffuugoo
ffuugoo force-pushed the optimize-consensus-loop branch from 19c80dd to fda28ee Compare January 28, 2025 12:14
@ffuugoo

ffuugoo commented Jan 28, 2025

Copy link
Copy Markdown
Contributor Author

Rebased on dev (just in case), will merge once green ✅

@ffuugoo
ffuugoo merged commit f72dcfb into dev Jan 28, 2025
@ffuugoo
ffuugoo deleted the optimize-consensus-loop branch January 28, 2025 12:55
timvisee pushed a commit that referenced this pull request Feb 11, 2025
@timvisee timvisee mentioned this pull request Feb 11, 2025
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