Skip to content

Commit

Permalink
Gets accounts-db fields for snapshots sooner (#1561)
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo authored May 31, 2024
1 parent 6455a2a commit b4352f3
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 64 deletions.
41 changes: 15 additions & 26 deletions runtime/src/bank/serde_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,16 @@ mod tests {
let key3 = Pubkey::new_unique();
bank_test_utils::deposit(&bank2, &key3, 0).unwrap();

let accounts_db = &bank2.rc.accounts.accounts_db;

bank2.squash();
bank2.force_flush_accounts_cache();
let expected_accounts_hash = AccountsHash(Hash::new_unique());
bank2
.accounts()
.accounts_db
.set_accounts_hash(bank2.slot(), (expected_accounts_hash, 30));
accounts_db.set_accounts_hash(bank2_slot, (expected_accounts_hash, 30));

let expected_incremental_snapshot_persistence =
has_incremental_snapshot_persistence.then(|| BankIncrementalSnapshotPersistence {
full_slot: bank2.slot() - 1,
full_slot: bank2_slot - 1,
full_hash: SerdeAccountsHash(Hash::new_unique()),
full_capitalization: 31,
incremental_hash: SerdeIncrementalAccountsHash(Hash::new_unique()),
Expand All @@ -150,10 +149,7 @@ mod tests {

let expected_epoch_accounts_hash = has_epoch_accounts_hash.then(|| {
let epoch_accounts_hash = EpochAccountsHash::new(Hash::new_unique());
bank2
.rc
.accounts
.accounts_db
accounts_db
.epoch_accounts_hash_manager
.set_valid(epoch_accounts_hash, eah_start_slot);
epoch_accounts_hash
Expand All @@ -165,7 +161,10 @@ mod tests {
serde_snapshot::serialize_bank_snapshot_into(
&mut writer,
bank2.get_fields_to_serialize(),
&bank2.rc.accounts.accounts_db,
accounts_db.get_bank_hash_stats(bank2_slot).unwrap(),
accounts_db.get_accounts_delta_hash(bank2_slot).unwrap(),
expected_accounts_hash,
accounts_db,
&get_storages_to_serialize(&bank2.get_snapshot_storages(None)),
expected_incremental_snapshot_persistence.as_ref(),
expected_epoch_accounts_hash,
Expand All @@ -179,12 +178,8 @@ mod tests {
let (_accounts_dir, dbank_paths) = get_temp_accounts_paths(4).unwrap();
// Create a directory to simulate AppendVecs unpackaged from a snapshot tar
let copied_accounts = TempDir::new().unwrap();
let storage_and_next_append_vec_id = copy_append_vecs(
&bank2.rc.accounts.accounts_db,
copied_accounts.path(),
storage_access,
)
.unwrap();
let storage_and_next_append_vec_id =
copy_append_vecs(accounts_db, copied_accounts.path(), storage_access).unwrap();

let cursor = Cursor::new(buf.as_slice());
let mut reader = BufReader::new(cursor);
Expand Down Expand Up @@ -474,7 +469,7 @@ mod tests {

#[cfg(RUSTC_WITH_SPECIALIZATION)]
mod test_bank_serialize {
use {super::*, solana_sdk::clock::Slot};
use {super::*, solana_accounts_db::accounts_db::BankHashStats, solana_sdk::clock::Slot};

// This some what long test harness is required to freeze the ABI of
// Bank's serialization due to versioned nature
Expand All @@ -493,15 +488,6 @@ mod tests {
where
S: serde::Serializer,
{
bank.rc
.accounts
.accounts_db
.set_accounts_delta_hash(bank.slot(), AccountsDeltaHash(Hash::new_unique()));
bank.rc.accounts.accounts_db.set_accounts_hash(
bank.slot(),
(AccountsHash(Hash::new_unique()), u64::default()),
);

let snapshot_storages = bank.get_snapshot_storages(None);
// ensure there is at least one snapshot storage example for ABI digesting
assert!(!snapshot_storages.is_empty());
Expand All @@ -517,6 +503,9 @@ mod tests {
serde_snapshot::serialize_bank_snapshot_with(
serializer,
bank.get_fields_to_serialize(),
BankHashStats::default(),
AccountsDeltaHash(Hash::new_unique()),
AccountsHash(Hash::new_unique()),
&bank.rc.accounts.accounts_db,
&get_storages_to_serialize(&snapshot_storages),
Some(&incremental_snapshot_persistence),
Expand Down
60 changes: 35 additions & 25 deletions runtime/src/serde_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use {
AccountsFileId, AtomicAccountsFileId, BankHashStats, IndexGenerationInfo,
},
accounts_file::{AccountsFile, StorageAccess},
accounts_hash::{AccountsDeltaHash, AccountsHash},
accounts_index::AccountSecondaryIndexes,
accounts_update_notifier_interface::AccountsUpdateNotifier,
ancestors::AncestorsForSerialization,
Expand Down Expand Up @@ -580,6 +581,9 @@ where
pub fn serialize_bank_snapshot_into<W>(
stream: &mut BufWriter<W>,
bank_fields: BankFieldsToSerialize,
bank_hash_stats: BankHashStats,
accounts_delta_hash: AccountsDeltaHash,
accounts_hash: AccountsHash,
accounts_db: &AccountsDb,
account_storage_entries: &[Vec<Arc<AccountStorageEntry>>],
incremental_snapshot_persistence: Option<&BankIncrementalSnapshotPersistence>,
Expand All @@ -595,6 +599,9 @@ where
serialize_bank_snapshot_with(
&mut serializer,
bank_fields,
bank_hash_stats,
accounts_delta_hash,
accounts_hash,
accounts_db,
account_storage_entries,
incremental_snapshot_persistence,
Expand All @@ -606,6 +613,9 @@ where
pub fn serialize_bank_snapshot_with<S>(
serializer: S,
bank_fields: BankFieldsToSerialize,
bank_hash_stats: BankHashStats,
accounts_delta_hash: AccountsDeltaHash,
accounts_hash: AccountsHash,
accounts_db: &AccountsDb,
account_storage_entries: &[Vec<Arc<AccountStorageEntry>>],
incremental_snapshot_persistence: Option<&BankIncrementalSnapshotPersistence>,
Expand All @@ -621,6 +631,9 @@ where
accounts_db,
slot,
account_storage_entries,
bank_hash_stats,
accounts_delta_hash,
accounts_hash,
};

(
Expand All @@ -647,17 +660,21 @@ impl<'a> Serialize for SerializableBankAndStorage<'a> {
{
let slot = self.bank.slot();
let fields = self.bank.get_fields_to_serialize();
let accounts_db = &self.bank.rc.accounts.accounts_db;
let bank_hash_stats = accounts_db.get_bank_hash_stats(slot).unwrap();
let accounts_delta_hash = accounts_db.get_accounts_delta_hash(slot).unwrap();
let accounts_hash = accounts_db.get_accounts_hash(slot).unwrap().0;
let lamports_per_signature = fields.fee_rate_governor.lamports_per_signature;
let bank_fields_to_serialize = (
SerializableVersionedBank::from(fields),
SerializableAccountsDb::<'_> {
accounts_db: &self.bank.rc.accounts.accounts_db,
accounts_db,
slot,
account_storage_entries: self.snapshot_storages,
bank_hash_stats,
accounts_delta_hash,
accounts_hash,
},
// Additional fields, we manually store the lamps per signature here so that
// we can grab it on restart.
// TODO: if we do a snapshot version bump, consider moving this out.
lamports_per_signature,
None::<BankIncrementalSnapshotPersistence>,
self.bank
Expand All @@ -682,12 +699,19 @@ impl<'a> Serialize for SerializableBankAndStorageNoExtra<'a> {
{
let slot = self.bank.slot();
let fields = self.bank.get_fields_to_serialize();
let accounts_db = &self.bank.rc.accounts.accounts_db;
let bank_hash_stats = accounts_db.get_bank_hash_stats(slot).unwrap();
let accounts_delta_hash = accounts_db.get_accounts_delta_hash(slot).unwrap();
let accounts_hash = accounts_db.get_accounts_hash(slot).unwrap().0;
(
SerializableVersionedBank::from(fields),
SerializableAccountsDb::<'_> {
accounts_db: &self.bank.rc.accounts.accounts_db,
accounts_db,
slot,
account_storage_entries: self.snapshot_storages,
bank_hash_stats,
accounts_delta_hash,
accounts_hash,
},
)
.serialize(serializer)
Expand All @@ -712,6 +736,9 @@ struct SerializableAccountsDb<'a> {
accounts_db: &'a AccountsDb,
slot: Slot,
account_storage_entries: &'a [Vec<Arc<AccountStorageEntry>>],
bank_hash_stats: BankHashStats,
accounts_delta_hash: AccountsDeltaHash,
accounts_hash: AccountsHash,
}

impl<'a> Serialize for SerializableAccountsDb<'a> {
Expand All @@ -735,27 +762,10 @@ impl<'a> Serialize for SerializableAccountsDb<'a> {
)
}));
let slot = self.slot;
let accounts_delta_hash = self
.accounts_db
.get_accounts_delta_hash(slot)
.map(Into::into)
.unwrap_or_else(|| panic!("Missing accounts delta hash entry for slot {slot}"));
// NOTE: This accounts hash is only needed when serializing a full snapshot.
// When serializing an incremental snapshot, there will not be a full accounts hash
// at `slot`. In that case, use the default, because it doesn't actually get used.
let accounts_hash = self
.accounts_db
.get_accounts_hash(slot)
.map(|(accounts_hash, _)| accounts_hash.into())
.unwrap_or_default();
let stats = self
.accounts_db
.get_bank_hash_stats(slot)
.unwrap_or_else(|| panic!("Missing bank hash stats entry for slot {slot}"));
let bank_hash_info = BankHashInfo {
accounts_delta_hash,
accounts_hash,
stats,
accounts_delta_hash: self.accounts_delta_hash.into(),
accounts_hash: self.accounts_hash.into(),
stats: self.bank_hash_stats.clone(),
};

let historical_roots = Vec::<Slot>::default();
Expand Down
6 changes: 6 additions & 0 deletions runtime/src/serde_snapshot/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,18 @@ mod serde_snapshot_tests {
where
W: Write,
{
let bank_hash_stats = accounts_db.get_bank_hash_stats(slot).unwrap();
let accounts_delta_hash = accounts_db.get_accounts_delta_hash(slot).unwrap();
let accounts_hash = accounts_db.get_accounts_hash(slot).unwrap().0;
serialize_into(
stream,
&SerializableAccountsDb {
accounts_db,
slot,
account_storage_entries,
bank_hash_stats,
accounts_delta_hash,
accounts_hash,
},
)
}
Expand Down
Loading

0 comments on commit b4352f3

Please sign in to comment.