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

logstore: load last entry term #142016

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft

Conversation

pav-kv
Copy link
Collaborator

@pav-kv pav-kv commented Feb 26, 2025

We already read the last entry term when initializing RawNode, so there is no additional overhead, except the fact that this entry is now not stashed into the raft entry cache. There appears to be low benefit in putting it into the cache though. Typically, we wouldn't need exactly the LastIndex entry post startup, it would more likely be an earlier entry. So putting LastIndex into the cache actually makes it ineffective because the cache drops entries below the cached slice.

Eventually, on startup we will load the term cache instead of the last entry.

As a nice side-effect, this PR removes the invalidLastTerm oddity. It also introduces an invariant that Replica always knows the last raft log entry index/term. We can use this to improve the raft.Storage API (e.g. add LastEntryID() method and remove this panic).

Resolves #142013

See the function comment. RawNode never accesses log indices above the
storage.LastIndex() because it has them covered by the unstable log.

Epic: none
Release note: none
Epic: none
Release note: none
@pav-kv pav-kv requested review from tbg and hakuuww February 26, 2025 01:14
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@pav-kv
Copy link
Collaborator Author

pav-kv commented Feb 26, 2025

Stacked on top of #142014.

We already read the last entry term when initializing RawNode, so there
is no additional overhead, except the fact that this entry is now not
stashed into the raft entry cache. There appears to be low benefit in
putting it into the cache though. Typically, we wouldn't need exactly
the LastIndex entry post startup.

Eventually, on startup we will load the term cache instead of the last
entry.

Epic: none
Release note: none
We always know the term of the last entry. It is now loaded on replica
startup. The applySnapshot code resets the log to empty, and knows the
term of the last entry.

Epic: none
Release note: none
Comment on lines 738 to +739
LastIndex: state.RaftAppliedIndex,
LastTerm: invalidLastTerm,
LastTerm: state.RaftAppliedIndexTerm,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Logically, these should be set based on truncState (scoped to log storage) rather than ReplicaState (scoped to state machine), even though these index/term are the same in applySnapshot (need to double-check).

Copy link
Contributor

@hakuuww hakuuww Feb 26, 2025

Choose a reason for hiding this comment

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

Seems to involve a lot of refactor, lets save this task for the future.

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.

kv/kvserver: Implement LoadLastTerm for creating or restarting replica
3 participants