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

Optimize max_flushed_root update in flushing roots #4320

Merged
merged 3 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions accounts-db/src/accounts_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,7 @@ impl AccountsCache {
}

pub fn add_root(&self, root: Slot) {
let max_flushed_root = self.fetch_max_flush_root();
if root > max_flushed_root || (root == max_flushed_root && root == 0) {
self.maybe_unflushed_roots.write().unwrap().insert(root);
}
self.maybe_unflushed_roots.write().unwrap().insert(root);
Copy link
Author

Choose a reason for hiding this comment

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

Since we can prove that slot > max_flush_root, we don't need to check root > max_flush_root.
And since we remove the slot > max_flush_root check, there is no need to special case root == 0 for genesis block too.

Copy link

Choose a reason for hiding this comment

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

we can prove that slot > max_flush_root

Is this because roots are monotonically increasing?

Copy link
Author

Choose a reason for hiding this comment

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

yes, exactly!

}

pub fn clear_roots(&self, max_root: Option<Slot>) -> BTreeSet<Slot> {
Expand Down
23 changes: 13 additions & 10 deletions accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6295,30 +6295,33 @@ impl AccountsDb {
});

// Always flush up to `requested_flush_root`, which is necessary for things like snapshotting.
let cached_roots: BTreeSet<Slot> = self.accounts_cache.clear_roots(requested_flush_root);
let flushed_roots: BTreeSet<Slot> = self.accounts_cache.clear_roots(requested_flush_root);
Copy link
Author

Choose a reason for hiding this comment

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

rename: cached_roots -> flushed_roots for clarity.


// Iterate from highest to lowest so that we don't need to flush earlier
// outdated updates in earlier roots
let mut num_roots_flushed = 0;
let mut flush_stats = FlushStats::default();
for &root in cached_roots.iter().rev() {
for &root in flushed_roots.iter().rev() {
if let Some(stats) =
self.flush_slot_cache_with_clean(root, should_flush_f.as_mut(), max_clean_root)
{
num_roots_flushed += 1;
flush_stats.accumulate(&stats);
}
}

// Regardless of whether this slot was *just* flushed from the cache by the above
// `flush_slot_cache()`, we should update the `max_flush_root`.
// This is because some rooted slots may be flushed to storage *before* they are marked as root.
// This can occur for instance when
// the cache is overwhelmed, we flushed some yet to be rooted frozen slots
// These slots may then *later* be marked as root, so we still need to handle updating the
// `max_flush_root` in the accounts cache.
// Note that self.flush_slot_cache_with_clean() can return None if the
// slot is already been flushed. This can happen if the cache is
// overwhelmed and we flushed some yet to be rooted frozen slots.
// However, Independent of whether the last slot was actually flushed
Copy link

Choose a reason for hiding this comment

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

nit Independent -> independent

Copy link
Author

Choose a reason for hiding this comment

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

yeah. I will change it in another pr so that people don't need to re-approve this one again.

// from the cache by the above loop, we should always update the
// `max_flush_root` to the max of the flushed roots, because that's
// max_flushed_root tracks the logical last root that was flushed to
// storage by snapshotting.
if let Some(&root) = flushed_roots.last() {
self.accounts_cache.set_max_flush_root(root);
}
let num_new_roots = cached_roots.len();
let num_new_roots = flushed_roots.len();
(num_new_roots, num_roots_flushed, flush_stats)
}

Expand Down
Loading