Skip to content

Commit

Permalink
Cache account stores, flush from AccountsBackgroundService (solana-la…
Browse files Browse the repository at this point in the history
  • Loading branch information
carllin authored Jan 12, 2021
1 parent 4a66e3e commit 6dfad06
Show file tree
Hide file tree
Showing 25 changed files with 2,725 additions and 954 deletions.
11 changes: 5 additions & 6 deletions accounts-bench/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ use clap::{crate_description, crate_name, value_t, App, Arg};
use rayon::prelude::*;
use solana_measure::measure::Measure;
use solana_runtime::{
accounts::{create_test_accounts, update_accounts, Accounts},
accounts::{create_test_accounts, update_accounts_bench, Accounts},
accounts_index::Ancestors,
};
use solana_sdk::{genesis_config::ClusterType, pubkey::Pubkey};
use std::env;
use std::fs;
use std::path::PathBuf;
use std::{collections::HashSet, env, fs, path::PathBuf};

fn main() {
solana_logger::setup();
Expand Down Expand Up @@ -56,7 +54,8 @@ fn main() {
if fs::remove_dir_all(path.clone()).is_err() {
println!("Warning: Couldn't remove {:?}", path);
}
let accounts = Accounts::new(vec![path], &ClusterType::Testnet);
let accounts =
Accounts::new_with_config(vec![path], &ClusterType::Testnet, HashSet::new(), false);
println!("Creating {} accounts", num_accounts);
let mut create_time = Measure::start("create accounts");
let pubkeys: Vec<_> = (0..num_slots)
Expand Down Expand Up @@ -92,7 +91,7 @@ fn main() {
time.stop();
println!("{}", time);
for slot in 0..num_slots {
update_accounts(&accounts, &pubkeys, ((x + 1) * num_slots + slot) as u64);
update_accounts_bench(&accounts, &pubkeys, ((x + 1) * num_slots + slot) as u64);
accounts.add_root((x * num_slots + slot) as u64);
}
} else {
Expand Down
2 changes: 2 additions & 0 deletions core/src/tvu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub struct TvuConfig {
pub trusted_validators: Option<HashSet<Pubkey>>,
pub repair_validators: Option<HashSet<Pubkey>>,
pub accounts_hash_fault_injection_slots: u64,
pub accounts_db_caching_enabled: bool,
}

impl Tvu {
Expand Down Expand Up @@ -272,6 +273,7 @@ impl Tvu {
bank_forks.clone(),
&exit,
accounts_background_request_handler,
tvu_config.accounts_db_caching_enabled,
);

Tvu {
Expand Down
4 changes: 4 additions & 0 deletions core/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ pub struct ValidatorConfig {
pub no_poh_speed_test: bool,
pub poh_pinned_cpu_core: usize,
pub account_indexes: HashSet<AccountIndex>,
pub accounts_db_caching_enabled: bool,
}

impl Default for ValidatorConfig {
Expand Down Expand Up @@ -164,6 +165,7 @@ impl Default for ValidatorConfig {
no_poh_speed_test: true,
poh_pinned_cpu_core: poh_service::DEFAULT_PINNED_CPU_CORE,
account_indexes: HashSet::new(),
accounts_db_caching_enabled: false,
}
}
}
Expand Down Expand Up @@ -629,6 +631,7 @@ impl Validator {
trusted_validators: config.trusted_validators.clone(),
repair_validators: config.repair_validators.clone(),
accounts_hash_fault_injection_slots: config.accounts_hash_fault_injection_slots,
accounts_db_caching_enabled: config.accounts_db_caching_enabled,
},
);

Expand Down Expand Up @@ -960,6 +963,7 @@ fn new_banks_from_ledger(
frozen_accounts: config.frozen_accounts.clone(),
debug_keys: config.debug_keys.clone(),
account_indexes: config.account_indexes.clone(),
accounts_db_caching_enabled: config.accounts_db_caching_enabled,
..blockstore_processor::ProcessOptions::default()
};

Expand Down
4 changes: 3 additions & 1 deletion core/tests/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ mod tests {
None,
None,
HashSet::new(),
false,
);
bank0.freeze();
let mut bank_forks = BankForks::new(bank0);
Expand Down Expand Up @@ -161,6 +162,7 @@ mod tests {
None,
None,
HashSet::new(),
false,
)
.unwrap();

Expand Down Expand Up @@ -216,7 +218,7 @@ mod tests {
if slot % set_root_interval == 0 || slot == last_slot - 1 {
// set_root should send a snapshot request
bank_forks.set_root(bank.slot(), &request_sender, None);
snapshot_request_handler.handle_snapshot_requests();
snapshot_request_handler.handle_snapshot_requests(false);
}
}

Expand Down
1 change: 1 addition & 0 deletions ledger-tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1926,6 +1926,7 @@ fn main() {
);
assert!(bank.is_complete());
bank.squash();
bank.force_flush_accounts_cache();
bank.clean_accounts(true);
bank.update_accounts_hash();
if rehash {
Expand Down
1 change: 1 addition & 0 deletions ledger/src/bank_forks_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub fn load(
process_options.debug_keys.clone(),
Some(&crate::builtins::get(process_options.bpf_jit)),
process_options.account_indexes.clone(),
process_options.accounts_db_caching_enabled,
)
.expect("Load from snapshot failed");
if let Some(shrink_paths) = shrink_paths {
Expand Down
5 changes: 5 additions & 0 deletions ledger/src/blockstore_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ pub struct ProcessOptions {
pub frozen_accounts: Vec<Pubkey>,
pub debug_keys: Option<Arc<HashSet<Pubkey>>>,
pub account_indexes: HashSet<AccountIndex>,
pub accounts_db_caching_enabled: bool,
}

pub fn process_blockstore(
Expand All @@ -371,6 +372,7 @@ pub fn process_blockstore(
opts.debug_keys.clone(),
Some(&crate::builtins::get(opts.bpf_jit)),
opts.account_indexes.clone(),
opts.accounts_db_caching_enabled,
);
let bank0 = Arc::new(bank0);
info!("processing ledger for slot 0...");
Expand Down Expand Up @@ -929,6 +931,8 @@ fn load_frozen_forks(
new_root_bank.squash();

if last_free.elapsed() > Duration::from_secs(10) {
// Must be called after `squash()`, so that AccountsDb knows what
// the roots are for the cache flushing in exhaustively_free_unused_resource().
// This could take few secs; so update last_free later
new_root_bank.exhaustively_free_unused_resource();
last_free = Instant::now();
Expand Down Expand Up @@ -2901,6 +2905,7 @@ pub mod tests {
None,
None,
HashSet::new(),
false,
);
*bank.epoch_schedule()
}
Expand Down
1 change: 1 addition & 0 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ args=(
--init-complete-file "$dataDir"/init-completed
--snapshot-compression none
--require-tower
--accounts-db-caching-enabled
)
# shellcheck disable=SC2086
solana-validator "${args[@]}" $SOLANA_RUN_SH_VALIDATOR_ARGS &
Expand Down
43 changes: 28 additions & 15 deletions runtime/benches/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ fn test_accounts_create(bencher: &mut Bencher) {
None,
None,
HashSet::new(),
false,
);
bencher.iter(|| {
let mut pubkeys: Vec<Pubkey> = vec![];
Expand All @@ -61,35 +62,39 @@ fn test_accounts_create(bencher: &mut Bencher) {
fn test_accounts_squash(bencher: &mut Bencher) {
let (mut genesis_config, _) = create_genesis_config(100_000);
genesis_config.rent.burn_percent = 100; // Avoid triggering an assert in Bank::distribute_rent_to_validators()
let bank1 = Arc::new(Bank::new_with_paths(
let mut prev_bank = Arc::new(Bank::new_with_paths(
&genesis_config,
vec![PathBuf::from("bench_a1")],
&[],
None,
None,
HashSet::new(),
false,
));
let mut pubkeys: Vec<Pubkey> = vec![];
deposit_many(&bank1, &mut pubkeys, 250_000);
bank1.freeze();
deposit_many(&prev_bank, &mut pubkeys, 250_000);
prev_bank.freeze();

// Measures the performance of the squash operation.
// This mainly consists of the freeze operation which calculates the
// merkle hash of the account state and distribution of fees and rent
let mut slot = 1u64;
bencher.iter(|| {
let bank2 = Arc::new(Bank::new_from_parent(&bank1, &Pubkey::default(), slot));
bank2.deposit(&pubkeys[0], 1);
bank2.squash();
let next_bank = Arc::new(Bank::new_from_parent(&prev_bank, &Pubkey::default(), slot));
next_bank.deposit(&pubkeys[0], 1);
next_bank.squash();
slot += 1;
prev_bank = next_bank;
});
}

#[bench]
fn test_accounts_hash_bank_hash(bencher: &mut Bencher) {
let accounts = Accounts::new(
let accounts = Accounts::new_with_config(
vec![PathBuf::from("bench_accounts_hash_internal")],
&ClusterType::Development,
HashSet::new(),
false,
);
let mut pubkeys: Vec<Pubkey> = vec![];
let num_accounts = 60_000;
Expand All @@ -107,9 +112,11 @@ fn test_accounts_hash_bank_hash(bencher: &mut Bencher) {
#[bench]
fn test_update_accounts_hash(bencher: &mut Bencher) {
solana_logger::setup();
let accounts = Accounts::new(
let accounts = Accounts::new_with_config(
vec![PathBuf::from("update_accounts_hash")],
&ClusterType::Development,
HashSet::new(),
false,
);
let mut pubkeys: Vec<Pubkey> = vec![];
create_test_accounts(&accounts, &mut pubkeys, 50_000, 0);
Expand All @@ -124,9 +131,11 @@ fn test_update_accounts_hash(bencher: &mut Bencher) {
#[bench]
fn test_accounts_delta_hash(bencher: &mut Bencher) {
solana_logger::setup();
let accounts = Accounts::new(
let accounts = Accounts::new_with_config(
vec![PathBuf::from("accounts_delta_hash")],
&ClusterType::Development,
HashSet::new(),
false,
);
let mut pubkeys: Vec<Pubkey> = vec![];
create_test_accounts(&accounts, &mut pubkeys, 100_000, 0);
Expand All @@ -138,17 +147,19 @@ fn test_accounts_delta_hash(bencher: &mut Bencher) {
#[bench]
fn bench_delete_dependencies(bencher: &mut Bencher) {
solana_logger::setup();
let accounts = Accounts::new(
let accounts = Accounts::new_with_config(
vec![PathBuf::from("accounts_delete_deps")],
&ClusterType::Development,
HashSet::new(),
false,
);
let mut old_pubkey = Pubkey::default();
let zero_account = Account::new(0, 0, &Account::default().owner);
for i in 0..1000 {
let pubkey = solana_sdk::pubkey::new_rand();
let account = Account::new((i + 1) as u64, 0, &Account::default().owner);
accounts.store_slow(i, &pubkey, &account);
accounts.store_slow(i, &old_pubkey, &zero_account);
accounts.store_slow_uncached(i, &pubkey, &account);
accounts.store_slow_uncached(i, &old_pubkey, &zero_account);
old_pubkey = pubkey;
accounts.add_root(i);
}
Expand All @@ -165,12 +176,14 @@ fn store_accounts_with_possible_contention<F: 'static>(
F: Fn(&Accounts, &[Pubkey]) + Send + Copy,
{
let num_readers = 5;
let accounts = Arc::new(Accounts::new(
let accounts = Arc::new(Accounts::new_with_config(
vec![
PathBuf::from(std::env::var("FARF_DIR").unwrap_or_else(|_| "farf".to_string()))
.join(bench_name),
],
&ClusterType::Development,
HashSet::new(),
false,
));
let num_keys = 1000;
let slot = 0;
Expand All @@ -180,7 +193,7 @@ fn store_accounts_with_possible_contention<F: 'static>(
.map(|_| {
let pubkey = solana_sdk::pubkey::new_rand();
let account = Account::new(1, 0, &Account::default().owner);
accounts.store_slow(slot, &pubkey, &account);
accounts.store_slow_uncached(slot, &pubkey, &account);
pubkey
})
.collect(),
Expand All @@ -206,7 +219,7 @@ fn store_accounts_with_possible_contention<F: 'static>(
// Write to a different slot than the one being read from. Because
// there's a new account pubkey being written to every time, will
// compete for the accounts index lock on every store
accounts.store_slow(slot + 1, &solana_sdk::pubkey::new_rand(), &account);
accounts.store_slow_uncached(slot + 1, &solana_sdk::pubkey::new_rand(), &account);
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/benches/accounts_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn bench_accounts_index(bencher: &mut Bencher) {
);
reclaims.clear();
}
index.add_root(root);
index.add_root(root, false);
root += 1;
fork += 1;
});
Expand Down
1 change: 1 addition & 0 deletions runtime/benches/append_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ fn append_vec_sequential_read(bencher: &mut Bencher) {
let mut indexes = add_test_accounts(&vec, size);
bencher.iter(|| {
let (sample, pos) = indexes.pop().unwrap();
println!("reading pos {} {}", sample, pos);
let (account, _next) = vec.get_account(pos).unwrap();
let (_meta, test) = create_test_account(sample);
assert_eq!(account.data, test.data.as_slice());
Expand Down
Loading

0 comments on commit 6dfad06

Please sign in to comment.