Skip to content

State cache tweaks #7095

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

Merged
merged 20 commits into from
Mar 18, 2025
Merged

Conversation

michaelsproul
Copy link
Member

@michaelsproul michaelsproul commented Mar 10, 2025

Issue Addressed

Backport of:

For:

Proposed Changes

  • Prevent writing to state cache when migrating the database
  • Add state-cache-headroom flag to control pruning
  • Prune old epoch boundary states ahead of mid-epoch states
  • Never prune head block's state
  • Avoid caching ancestor states unless they are on an epoch boundary
  • Log when states enter/exit the cache

Co-authored-by: Eitan Seri-Levi [email protected]

eserilev and others added 4 commits March 10, 2025 15:29
* add an update_cache flag to get_state to have more granular control over when we write to the cache

* State cache tweaks

- add state-cache-headroom flag to control pruning
- prune old epoch boundary states ahead of mid-epoch states
- never prune head block's state
- avoid caching ancestor states unless they are on an epoch boundary

---------

Co-authored-by: Michael Sproul <[email protected]>
@michaelsproul michaelsproul added ready-for-review The code is ready for review v7.0.0 New release c. Q1 2025 v7.0.0-beta.clean Clean release post Holesky rescue labels Mar 10, 2025
@michaelsproul michaelsproul mentioned this pull request Mar 10, 2025
14 tasks
@michaelsproul michaelsproul requested review from eserilev and dapplion and removed request for eserilev March 11, 2025 05:22
@dapplion
Copy link
Collaborator

Could you also add this logs to known when states enter / exit the cache?

5b84094

@@ -816,7 +816,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
.get_blinded_block(&block_root)?
.ok_or(Error::MissingBeaconBlock(block_root))?;
let state = self
.get_state(&block.state_root(), Some(block.slot()))?
.get_state(&block.state_root(), Some(block.slot()), true)?
Copy link
Collaborator

Choose a reason for hiding this comment

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

The function rev_iter_block_roots_from is only used for tests, why true here? Maybe we can move this body to store_tests to have only less codepath to think about.

Copy link
Member

Choose a reason for hiding this comment

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

I've added a TODO to move this to store_tests, I'll take care of that in a separate PR

In general we cache states in test fn's to make CI go brr

@@ -47,8 +47,10 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
.state_root_at_slot(state_slot)?
.ok_or(BeaconChainError::NoStateForSlot(state_slot))?;

// This branch is reached from the HTTP API. We assume the user wants
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add some tag to all these comments? Like

Suggested change
// This branch is reached from the HTTP API. We assume the user wants
// `update_cache = true` This branch is reached from the HTTP API. We assume the user wants

or

Suggested change
// This branch is reached from the HTTP API. We assume the user wants
// `get_state(.., true)` This branch is reached from the HTTP API. We assume the user wants

Will help future generations

Copy link
Member Author

@michaelsproul michaelsproul Mar 17, 2025

Choose a reason for hiding this comment

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

I don't think this is really helpful compared to just grepping for get_state. I feel like we're bikeshedding these comments a bit at this point

Comment on lines 1514 to 1516
// TODO(hdiff): is this optimization necessary? Computing diffs is expensive so we may want
// to avoid it.
if self.load_hot_state_summary(state_root)?.is_some() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

An TODO(hdiff) sneaked in. This check relies on the assumption that states are only ever deleted on finalization pruning. Otherwise we need a lock. I'm not sure if this check is same in this branch.

Copy link
Member Author

@michaelsproul michaelsproul Mar 17, 2025

Choose a reason for hiding this comment

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

I've deleted this check, as I want to keep this PR minimal and release-v7.0.0 is working OK wrt temporary states/etc AFAICT. The bugs we saw on tree-states hot were caused by partially removing the temporary states concept.

Comment on lines +388 to +390
--state-cache-headroom <N>
Minimum number of states to cull from the state cache when it gets
full [default: 1]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this flag be hidden?

Copy link
Member Author

@michaelsproul michaelsproul Mar 17, 2025

Choose a reason for hiding this comment

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

I don't mind it not being hidden. Or maybe we hide it until we implement:

Copy link
Member

@jimmygchen jimmygchen left a comment

Choose a reason for hiding this comment

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

Reviewed the new diffs after Lion's review, looks good 👍

@michaelsproul michaelsproul added ready-for-merge This PR is ready to merge. and removed ready-for-review The code is ready for review labels Mar 18, 2025
mergify bot added a commit that referenced this pull request Mar 18, 2025
@mergify mergify bot merged commit 4de0626 into sigp:release-v7.0.0 Mar 18, 2025
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-for-merge This PR is ready to merge. v7.0.0-beta.clean Clean release post Holesky rescue v7.0.0 New release c. Q1 2025
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants