Skip to content

Conversation

@hakuuww
Copy link
Contributor

@hakuuww hakuuww commented Dec 24, 2025

This PR addresses the TODO optimizations to add a fast path to raftLog.match() to avoid doing a linear scan to find a fork point on "our" raftLog against the appending LeadSlice in the common-case.

l is "our" log, s is the log append LeadSlice from MsgApp.

By raft invariants, we have a guarantee that our (follower) log l is a prefix of the l.accTerm leader's log.
So if the accTerm leader sends us log appends (indicated by the LeadSlice s.term field, and checked by l.accTerm == s.term),

we may skip all the matchTerm checks for the entries in the appending LeadSlice sas they will all succeed meaning there are no log inconsistencies between the log append LeadSlice s and our log l.

It is also possible that we have not yet appended the first log entry from the s.term leader so l.accTerm == s.term would evaluate to false. In this case, we can still use the Raft log matching property to do a fast-path check:
Check the term match at min(s.lastIndex(), l.lastIndex()) entry of s. If the term matches, there is no mismatch between the two logs.

In either case, if there is no mismatch, return min(s.lastIndex(), l.lastIndex()). All entryIDs up to and including this index are guaranteed to match.

Note that there is still one remaining TODO in raftLog.match() untouched.
However with the termCache in place the potential penalty cost of doing a linear scan (l.matchTerm(id)) for all id within LeadSlice s.entries.id) is no longer substantial, as it is very unlikely that the term check falls back to a storage call.
Therefore the TODO may be updated to reflect the current state.

Epic: None
Release note: None

Epic: None
Release note: None
@hakuuww hakuuww requested a review from a team as a code owner December 24, 2025 03:54
@blathers-crl
Copy link

blathers-crl bot commented Dec 24, 2025

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR.

Before a member of our team reviews your PR, I have some potential action items for you:

  • We notice you have more than one commit in your PR. We try break logical changes into separate commits, but commits such as "fix typo" or "address review commits" should be squashed into one commit and pushed with --force
  • When CI has completed, please ensure no errors have appeared.

I was unable to automatically find a reviewer. You can try CCing one of the following members:

  • A person you worked with closely on this PR.
  • The person who created the ticket, or a CRDB organization member involved with the ticket (author, commenter, etc.).
  • Join our community slack channel and ask on #contributors.
  • Try find someone else from here.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl blathers-crl bot added O-community Originated from the community X-blathers-untriaged blathers was unable to find an owner labels Dec 24, 2025
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@hakuuww hakuuww force-pushed the raft-improve-raftlog.match branch from fd270d1 to 8682aa4 Compare December 24, 2025 04:37
@blathers-crl
Copy link

blathers-crl bot commented Dec 24, 2025

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

Thank you for updating your pull request.

Before a member of our team reviews your PR, I have some potential action items for you:

  • We notice you have more than one commit in your PR. We try break logical changes into separate commits, but commits such as "fix typo" or "address review commits" should be squashed into one commit and pushed with --force
  • When CI has completed, please ensure no errors have appeared.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@hakuuww hakuuww force-pushed the raft-improve-raftlog.match branch 6 times, most recently from 0aea3a3 to 68dbdee Compare December 24, 2025 08:44
Copy link
Contributor Author

@hakuuww hakuuww left a comment

Choose a reason for hiding this comment

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

@hakuuww made 1 comment.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained.


pkg/raft/log.go line 194 at r2 (raw file):

//
// All the entries up to the returned index are already present in the log, and
// do not need to be rewritten. The caller can safely fast-forward the appending

"appended" indicates entries in s LeadSlice are already appended to the caller's raftLog.
However, this is not true at this point in time, as the LeadSlice is still not appended yet.

Also it won't be "appended" if match returns 0, false.

Copy link
Contributor Author

@hakuuww hakuuww left a comment

Choose a reason for hiding this comment

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

@hakuuww made 1 comment and resolved 1 discussion.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained.


pkg/raft/log.go line 194 at r2 (raw file):

Previously, hakuuww (Anthony Xu) wrote…

"appended" indicates entries in s LeadSlice are already appended to the caller's raftLog.
However, this is not true at this point in time, as the LeadSlice is still not appended yet.

Also it won't be "appended" if match returns 0, false.

Appending makes a bit more sense as we are in the process of attempting to append the LeadSlice to raftLog.

Use the invariants of accTerm and LeadSlice.term, along with
the log matching property to add a fast-path optimization.

Most log append requests can skip the term checks that scan
the entire appending leadSlice.

Epic: none
Release note: none
@hakuuww hakuuww force-pushed the raft-improve-raftlog.match branch from 68dbdee to 0864c4c Compare December 24, 2025 19:50
@yuzefovich yuzefovich removed the X-blathers-untriaged blathers was unable to find an owner label Dec 26, 2025
@yuzefovich yuzefovich requested a review from pav-kv December 26, 2025 17:42
@tuansydau tuansydau added the T-kv KV Team label Dec 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-community Originated from the community T-kv KV Team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants