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

AccountsDb: make write_accounts_to_cache() never block sending to the bg hasher #3814

Merged
merged 1 commit into from
Dec 2, 2024

Conversation

alessandrod
Copy link

@alessandrod alessandrod commented Nov 27, 2024

Avoid commit_transactions() => store() => store_accounts_to() callers having to notify the bg hasher thread by doing an explicit sleep instead of sleeping on channel.recv() when the channel is empty. This avoids the case in which multiple replay/banking threads call commit_transactions() at the same time and end up... sleeping themselves acquiring the mutex to wake up the bg hasher.

Right side of the marker is with this PR

Screenshot 2024-11-28 at 9 45 32 am

… bg hasher

Avoid commit_transactions() => store() => store_accounts_to() callers
having to notify the bg hasher thread by doing an explicit sleep instead
of sleeping on channel.recv() when the channel is empty. This avoids the
case in which multiple replay/banking threads call commit_transactions()
at the same time and end up... sleeping themselves acquiring the mutex
to wake up the bg hasher.
@alessandrod
Copy link
Author

Here's a run I did a few weeks ago at higher throughput

Screenshot 2024-12-02 at 10 21 36 pm

@brooksprumo
Copy link

Can you help me understand, please?

On the store side, here's where we send the accounts to the background thread for hashing:

// hash this accounts in bg
if let Some(ref sender) = &self.sender_bg_hasher {
let _ = sender.send(cached_accounts);
};

Are you saying that send() blocks these threads because we are calling recv() instead of try_recv() in the background hasher thread here?

let result = receiver.recv();

@brooksprumo
Copy link

Or is the issue because send() will notify receivers, and using recv() is what registers a SyncReceiver?

notify receivers in send():
https://docs.rs/crossbeam-channel/latest/src/crossbeam_channel/flavors/list.rs.html#295-296

grab a lock to wake up a receiver:
https://docs.rs/crossbeam-channel/latest/src/crossbeam_channel/waker.rs.html#223

Copy link

@brooksprumo brooksprumo left a comment

Choose a reason for hiding this comment

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

:shipit:

(assuming my understanding in #3814 (comment) is correct)

@@ -2355,7 +2355,10 @@ impl AccountsDb {
};
}
}
Err(err) => {
Err(TryRecvError::Empty) => {
sleep(Duration::from_millis(5));

Choose a reason for hiding this comment

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

I'm not sure what the "right" sleep duration should be here, but 5 ms seems fine to start with.

@alessandrod
Copy link
Author

Or is the issue because send() will notify receivers, and using recv() is what registers a SyncReceiver?

notify receivers in send(): https://docs.rs/crossbeam-channel/latest/src/crossbeam_channel/flavors/list.rs.html#295-296

grab a lock to wake up a receiver: https://docs.rs/crossbeam-channel/latest/src/crossbeam_channel/waker.rs.html#223

correct, the issue is racing to notify, sleeping on the lock

@alessandrod alessandrod merged commit f2e3e39 into anza-xyz:master Dec 2, 2024
41 checks passed
@alessandrod alessandrod added the v2.1 Backport to v2.1 branch label Dec 2, 2024
Copy link

mergify bot commented Dec 2, 2024

Backports to the beta branch are to be avoided unless absolutely necessary for fixing bugs, security issues, and perf regressions. Changes intended for backport should be structured such that a minimum effective diff can be committed separately from any refactoring, plumbing, cleanup, etc that are not strictly necessary to achieve the goal. Any of the latter should go only into master and ride the normal stabilization schedule. Exceptions include CI/metrics changes, CLI improvements and documentation updates on a case by case basis.

mergify bot pushed a commit that referenced this pull request Dec 2, 2024
… bg hasher (#3814)

Avoid commit_transactions() => store() => store_accounts_to() callers
having to notify the bg hasher thread by doing an explicit sleep instead
of sleeping on channel.recv() when the channel is empty. This avoids the
case in which multiple replay/banking threads call commit_transactions()
at the same time and end up... sleeping themselves acquiring the mutex
to wake up the bg hasher.

(cherry picked from commit f2e3e39)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v2.1 Backport to v2.1 branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants