-
Notifications
You must be signed in to change notification settings - Fork 4.1k
raft: add fast path to raftLog.match() #160102
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
base: master
Are you sure you want to change the base?
Conversation
Epic: None Release note: None
|
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:
I was unable to automatically find a reviewer. You can try CCing one of the following members:
🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf. |
fd270d1 to
8682aa4
Compare
|
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:
🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf. |
0aea3a3 to
68dbdee
Compare
hakuuww
left a comment
There was a problem hiding this 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: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.
hakuuww
left a comment
There was a problem hiding this 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: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 LeadSliceare already appended to the caller'sraftLog.
However, this is not true at this point in time, as theLeadSliceis 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
68dbdee to
0864c4c
Compare
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 appendingLeadSlicein the common-case.lis "our" log,sis the log appendLeadSlicefromMsgApp.By raft invariants, we have a guarantee that our (follower) log
lis a prefix of thel.accTermleader's log.So if the
accTermleader sends us log appends (indicated by theLeadSlice s.termfield, and checked byl.accTerm==s.term),we may skip all the
matchTermchecks for the entries in the appendingLeadSlice sas they will all succeed meaning there are no log inconsistencies between the log appendLeadSlice sand our logl.It is also possible that we have not yet appended the first log entry from the s.term leader so
l.accTerm==s.termwould evaluate tofalse. 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 ofs. 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
termCachein place the potential penalty cost of doing a linear scan (l.matchTerm(id)) for allidwithinLeadSlice 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