Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

implements copy-on-write for vote-accounts#19362

Merged
behzadnouri merged 1 commit intosolana-labs:masterfrom
behzadnouri:arc-vote-accounts-lite
Aug 30, 2021
Merged

implements copy-on-write for vote-accounts#19362
behzadnouri merged 1 commit intosolana-labs:masterfrom
behzadnouri:arc-vote-accounts-lite

Conversation

@behzadnouri
Copy link
Copy Markdown
Contributor

Problem

Bank::vote_accounts redundantly clones vote-accounts HashMap even though
an immutable reference will suffice:
https://github.com/solana-labs/solana/blob/95c998a19/runtime/src/bank.rs#L5174-L5186

Summary of Changes

This commit implements copy-on-write semantics for vote-accounts by
wrapping the underlying HashMap in Arc<...>.

@behzadnouri behzadnouri force-pushed the arc-vote-accounts-lite branch 3 times, most recently from 66af32d to 24cbc2c Compare August 23, 2021 02:16
@codecov
Copy link
Copy Markdown

codecov Bot commented Aug 23, 2021

Codecov Report

Merging #19362 (84db04c) into master (af33012) will increase coverage by 0.0%.
The diff coverage is 86.2%.

❗ Current head 84db04c differs from pull request most recent head 818fd84. Consider uploading reports for the commit 818fd84 to get more accurate results

@@           Coverage Diff            @@
##           master   #19362    +/-   ##
========================================
  Coverage    82.7%    82.7%            
========================================
  Files         459      461     +2     
  Lines      130921   131077   +156     
========================================
+ Hits       108321   108457   +136     
- Misses      22600    22620    +20     

Bank::vote_accounts redundantly clones vote-accounts HashMap even though
an immutable reference will suffice:
https://github.com/solana-labs/solana/blob/95c998a19/runtime/src/bank.rs#L5174-L5186

This commit implements copy-on-write semantics for vote-accounts by
wrapping the underlying HashMap in Arc<...>.
@behzadnouri behzadnouri force-pushed the arc-vote-accounts-lite branch from 24cbc2c to 818fd84 Compare August 26, 2021 17:13
Comment on lines +94 to +122
pub(crate) fn insert(&mut self, pubkey: Pubkey, (stake, vote_account): (u64, VoteAccount)) {
self.add_node_stake(stake, &vote_account);
if let Some((stake, vote_account)) =
self.vote_accounts.insert(pubkey, (stake, vote_account))
{
let vote_accounts = Arc::make_mut(&mut self.vote_accounts);
if let Some((stake, vote_account)) = vote_accounts.insert(pubkey, (stake, vote_account)) {
self.sub_node_stake(stake, &vote_account);
}
}

pub fn remove(&mut self, pubkey: &Pubkey) -> Option<(u64, VoteAccount)> {
let value = self.vote_accounts.remove(pubkey);
if let Some((stake, ref vote_account)) = value {
pub(crate) fn remove(&mut self, pubkey: &Pubkey) -> Option<(u64, VoteAccount)> {
let vote_accounts = Arc::make_mut(&mut self.vote_accounts);
let entry = vote_accounts.remove(pubkey);
if let Some((stake, ref vote_account)) = entry {
self.sub_node_stake(stake, vote_account);
}
value
entry
}

pub fn add_stake(&mut self, pubkey: &Pubkey, delta: u64) {
if let Some((stake, vote_account)) = self.vote_accounts.get_mut(pubkey) {
pub(crate) fn add_stake(&mut self, pubkey: &Pubkey, delta: u64) {
let vote_accounts = Arc::make_mut(&mut self.vote_accounts);
if let Some((stake, vote_account)) = vote_accounts.get_mut(pubkey) {
*stake += delta;
let vote_account = vote_account.clone();
self.add_node_stake(delta, &vote_account);
}
}

pub fn sub_stake(&mut self, pubkey: &Pubkey, delta: u64) {
if let Some((stake, vote_account)) = self.vote_accounts.get_mut(pubkey) {
pub(crate) fn sub_stake(&mut self, pubkey: &Pubkey, delta: u64) {
let vote_accounts = Arc::make_mut(&mut self.vote_accounts);
if let Some((stake, vote_account)) = vote_accounts.get_mut(pubkey) {
Copy link
Copy Markdown
Contributor

@carllin carllin Aug 29, 2021

Choose a reason for hiding this comment

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

Nice! I think this works

Quick sanity check, this should be safe even if we have interleaving updates between Replay and BankingStage.

For example, imagine we have:

     /--------5 (Banking Stage)
0
   \-----------4 (ReplayStage)

So the sequence of events:

  1. We start a leader block for our own slot 5 , inheriting the shared stakes object from the slot 0:

    stakes: RwLock::new(parent.stakes.read().unwrap().clone()),
    .

  2. At some later point in time Replay concurrently creates and starts replaying block 4, also inheriting the shared stakes object from the slot 0.

The situation we want to avoid is either of the slot-specific updates to stakes via update_cached_accounts():

fn update_cached_accounts(
from being visible to the other child.

Now we know this is safe because if we do an update in slot 5 before or after 4 is created, we clone the vote accounts for 5 via make_mut, so these updates are hidden from 4.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, it will be safe because as soon as any mutations are about to be made (and there is still another Arc reference to the same hash-map), a clone is made and so the mutations are not leaked to other Arc references.
The added test_staked_nodes_cow test is testing one such scenario.

@behzadnouri behzadnouri requested a review from carllin August 29, 2021 14:44
@behzadnouri behzadnouri merged commit 8ad52fa into solana-labs:master Aug 30, 2021
@behzadnouri behzadnouri deleted the arc-vote-accounts-lite branch August 30, 2021 15:54
jeffwashington pushed a commit to jeffwashington/solana that referenced this pull request Aug 31, 2021
Bank::vote_accounts redundantly clones vote-accounts HashMap even though
an immutable reference will suffice:
https://github.com/solana-labs/solana/blob/95c998a19/runtime/src/bank.rs#L5174-L5186

This commit implements copy-on-write semantics for vote-accounts by
wrapping the underlying HashMap in Arc<...>.
frits-metalogix added a commit to identity-com/solana that referenced this pull request Nov 24, 2021
@carllin carllin added the v1.8 label Dec 28, 2021
mergify Bot pushed a commit that referenced this pull request Dec 28, 2021
Bank::vote_accounts redundantly clones vote-accounts HashMap even though
an immutable reference will suffice:
https://github.com/solana-labs/solana/blob/95c998a19/runtime/src/bank.rs#L5174-L5186

This commit implements copy-on-write semantics for vote-accounts by
wrapping the underlying HashMap in Arc<...>.

(cherry picked from commit 8ad52fa)

# Conflicts:
#	core/src/cluster_info_vote_listener.rs
#	core/src/consensus.rs
#	ledger/src/blockstore_processor.rs
#	runtime/src/bank.rs
#	runtime/src/stakes.rs
#	runtime/src/vote_account.rs
carllin pushed a commit that referenced this pull request Dec 28, 2021
Bank::vote_accounts redundantly clones vote-accounts HashMap even though
an immutable reference will suffice:
https://github.com/solana-labs/solana/blob/95c998a19/runtime/src/bank.rs#L5174-L5186

This commit implements copy-on-write semantics for vote-accounts by
wrapping the underlying HashMap in Arc<...>.
behzadnouri added a commit to behzadnouri/solana that referenced this pull request Dec 28, 2021
Bank::vote_accounts redundantly clones vote-accounts HashMap even though
an immutable reference will suffice:
https://github.com/solana-labs/solana/blob/95c998a19/runtime/src/bank.rs#L5174-L5186

This commit implements copy-on-write semantics for vote-accounts by
wrapping the underlying HashMap in Arc<...>.
behzadnouri added a commit that referenced this pull request Dec 28, 2021
Bank::vote_accounts redundantly clones vote-accounts HashMap even though
an immutable reference will suffice:
https://github.com/solana-labs/solana/blob/95c998a19/runtime/src/bank.rs#L5174-L5186

This commit implements copy-on-write semantics for vote-accounts by
wrapping the underlying HashMap in Arc<...>.
mergify Bot added a commit that referenced this pull request Dec 28, 2021
…22139)

Bank::vote_accounts redundantly clones vote-accounts HashMap even though
an immutable reference will suffice:
https://github.com/solana-labs/solana/blob/95c998a19/runtime/src/bank.rs#L5174-L5186

This commit implements copy-on-write semantics for vote-accounts by
wrapping the underlying HashMap in Arc<...>.

Co-authored-by: behzad nouri <behzadnouri@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants