From ff20ef7b9be4d66db739d50f2d1d477b8d0f8242 Mon Sep 17 00:00:00 2001 From: linning Date: Fri, 5 Aug 2022 02:29:01 +0800 Subject: [PATCH 01/19] introduce DbBackedQueue for the state pruning window Signed-off-by: linning --- client/db/src/lib.rs | 31 ++- client/state-db/src/lib.rs | 84 +++---- client/state-db/src/pruning.rs | 447 ++++++++++++++++++++++++--------- 3 files changed, 389 insertions(+), 173 deletions(-) diff --git a/client/db/src/lib.rs b/client/db/src/lib.rs index 7dd49f9831f1c..84551d809ea2c 100644 --- a/client/db/src/lib.rs +++ b/client/db/src/lib.rs @@ -426,9 +426,10 @@ struct PendingBlock { } // wrapper that implements trait required for state_db -struct StateMetaDb<'a>(&'a dyn Database); +#[derive(Clone)] +struct StateMetaDb(Arc>); -impl<'a> sc_state_db::MetaDb for StateMetaDb<'a> { +impl sc_state_db::MetaDb for StateMetaDb { type Error = sp_database::error::DatabaseError; fn get_meta(&self, key: &[u8]) -> Result>, Self::Error> { @@ -899,7 +900,7 @@ impl sc_client_api::backend::BlockImportOperation struct StorageDb { pub db: Arc>, - pub state_db: StateDb>, + pub state_db: StateDb, StateMetaDb>, prefix_keys: bool, } @@ -1089,7 +1090,7 @@ impl Backend { let mut db_init_transaction = Transaction::new(); let requested_state_pruning = config.state_pruning.clone(); - let state_meta_db = StateMetaDb(db.as_ref()); + let state_meta_db = StateMetaDb(db.clone()); let map_e = sp_blockchain::Error::from_state_db; let (state_db_init_commit_set, state_db) = StateDb::open( @@ -1303,10 +1304,11 @@ impl Backend { } trace!(target: "db", "Canonicalize block #{} ({:?})", new_canonical, hash); - let commit = - self.storage.state_db.canonicalize_block(&hash).map_err( - sp_blockchain::Error::from_state_db::>, - )?; + let commit = self.storage.state_db.canonicalize_block(&hash).map_err( + sp_blockchain::Error::from_state_db::< + sc_state_db::Error, + >, + )?; apply_state_commit(transaction, commit); } Ok(()) @@ -1459,7 +1461,9 @@ impl Backend { if number <= last_finalized_num { // Canonicalize in the db when re-importing existing blocks with state. let commit = self.storage.state_db.canonicalize_block(&hash).map_err( - sp_blockchain::Error::from_state_db::>, + sp_blockchain::Error::from_state_db::< + sc_state_db::Error, + >, )?; apply_state_commit(&mut transaction, commit); meta_updates.push(MetaUpdate { @@ -1668,10 +1672,11 @@ impl Backend { .map(|c| f_num.saturated_into::() > c) .unwrap_or(true) { - let commit = - self.storage.state_db.canonicalize_block(&f_hash).map_err( - sp_blockchain::Error::from_state_db::>, - )?; + let commit = self.storage.state_db.canonicalize_block(&f_hash).map_err( + sp_blockchain::Error::from_state_db::< + sc_state_db::Error, + >, + )?; apply_state_commit(transaction, commit); } diff --git a/client/state-db/src/lib.rs b/client/state-db/src/lib.rs index d5cca9a342187..90b4ce2a4e314 100644 --- a/client/state-db/src/lib.rs +++ b/client/state-db/src/lib.rs @@ -98,7 +98,7 @@ impl< } /// Backend database trait. Read-only. -pub trait MetaDb { +pub trait MetaDb: Clone { type Error: fmt::Debug; /// Get meta value, such as the journal. @@ -276,23 +276,25 @@ fn to_meta_key(suffix: &[u8], data: &S) -> Vec { buffer } -struct StateDbSync { +struct StateDbSync { mode: PruningMode, non_canonical: NonCanonicalOverlay, - pruning: Option>, + pruning: Option>, pinned: HashMap, } -impl StateDbSync { - fn new( +impl + StateDbSync +{ + fn new( mode: PruningMode, ref_counting: bool, db: &D, - ) -> Result, Error> { + ) -> Result, Error> { trace!(target: "state-db", "StateDb settings: {:?}. Ref-counting: {}", mode, ref_counting); let non_canonical: NonCanonicalOverlay = NonCanonicalOverlay::new(db)?; - let pruning: Option> = match mode { + let pruning: Option> = match mode { PruningMode::Constrained(Constraints { max_mem: Some(_), .. }) => unimplemented!(), PruningMode::Constrained(_) => Some(RefWindow::new(db, ref_counting)?), PruningMode::ArchiveAll | PruningMode::ArchiveCanonical => None, @@ -321,10 +323,7 @@ impl StateDbSync( - &mut self, - hash: &BlockHash, - ) -> Result, Error> { + fn canonicalize_block(&mut self, hash: &BlockHash) -> Result, Error> { let mut commit = CommitSet::default(); if self.mode == PruningMode::ArchiveAll { return Ok(commit) @@ -339,7 +338,7 @@ impl StateDbSync StateDbSync) { + fn prune(&mut self, commit: &mut CommitSet) -> Result<(), Error> { if let (&mut Some(ref mut pruning), &PruningMode::Constrained(ref constraints)) = (&mut self.pruning, &self.mode) { @@ -376,12 +375,13 @@ impl StateDbSync StateDbSync( + pub fn get( &self, key: &Q, - db: &D, - ) -> Result, Error> + db: &DB, + ) -> Result, Error> where - Q: AsRef, + Q: AsRef, Key: std::borrow::Borrow, Q: std::hash::Hash + Eq, { @@ -464,7 +464,7 @@ impl StateDbSync StateDbSync StateDbMemoryInfo { StateDbMemoryInfo { non_canonical: MemorySize::from_bytes(malloc_size(&self.non_canonical)), - pruning: self.pruning.as_ref().map(|p| MemorySize::from_bytes(malloc_size(p))), + pruning: self.pruning.as_ref().map(|p| MemorySize::from_bytes(malloc_size(&p))), pinned: MemorySize::from_bytes(malloc_size(&self.pinned)), } } @@ -490,21 +490,20 @@ impl StateDbSync { - db: RwLock>, +pub struct StateDb { + db: RwLock>, } -impl StateDb { +impl + StateDb +{ /// Create an instance of [`StateDb`]. - pub fn open( + pub fn open( db: &D, requested_mode: Option, ref_counting: bool, should_init: bool, - ) -> Result<(CommitSet, StateDb), Error> - where - D: MetaDb, - { + ) -> Result<(CommitSet, StateDb), Error> { let stored_mode = fetch_stored_pruning_mode(db)?; let selected_mode = match (should_init, stored_mode, requested_mode) { @@ -559,10 +558,7 @@ impl StateDb( - &self, - hash: &BlockHash, - ) -> Result, Error> { + pub fn canonicalize_block(&self, hash: &BlockHash) -> Result, Error> { self.db.write().canonicalize_block(hash) } @@ -577,13 +573,13 @@ impl StateDb( + pub fn get( &self, key: &Q, - db: &D, - ) -> Result, Error> + db: &DB, + ) -> Result, Error> where - Q: AsRef, + Q: AsRef, Key: std::borrow::Borrow, Q: std::hash::Hash + Eq, { @@ -669,7 +665,7 @@ mod tests { use sp_core::H256; use std::io; - fn make_test_db(settings: PruningMode) -> (TestDb, StateDb) { + fn make_test_db(settings: PruningMode) -> (TestDb, StateDb) { let mut db = make_db(&[91, 921, 922, 93, 94]); let (state_db_init, state_db) = StateDb::open(&mut db, Some(settings), false, true).unwrap(); @@ -716,7 +712,7 @@ mod tests { .unwrap(), ); state_db.apply_pending(); - db.commit(&state_db.canonicalize_block::(&H256::from_low_u64_be(1)).unwrap()); + db.commit(&state_db.canonicalize_block(&H256::from_low_u64_be(1)).unwrap()); state_db.apply_pending(); db.commit( &state_db @@ -729,9 +725,9 @@ mod tests { .unwrap(), ); state_db.apply_pending(); - db.commit(&state_db.canonicalize_block::(&H256::from_low_u64_be(21)).unwrap()); + db.commit(&state_db.canonicalize_block(&H256::from_low_u64_be(21)).unwrap()); state_db.apply_pending(); - db.commit(&state_db.canonicalize_block::(&H256::from_low_u64_be(3)).unwrap()); + db.commit(&state_db.canonicalize_block(&H256::from_low_u64_be(3)).unwrap()); state_db.apply_pending(); (db, state_db) @@ -802,7 +798,7 @@ mod tests { .unwrap(), ); let new_mode = PruningMode::Constrained(Constraints { max_blocks: Some(2), max_mem: None }); - let state_db_open_result: Result<(_, StateDb), _> = + let state_db_open_result: Result<(_, StateDb), _> = StateDb::open(&mut db, Some(new_mode), false, false); assert!(state_db_open_result.is_err()); } @@ -814,12 +810,12 @@ mod tests { ) { let mut db = make_db(&[]); let (state_db_init, state_db) = - StateDb::::open(&mut db, mode_when_created, false, true).unwrap(); + StateDb::::open(&mut db, mode_when_created, false, true).unwrap(); db.commit(&state_db_init); std::mem::drop(state_db); let state_db_reopen_result = - StateDb::::open(&mut db, mode_when_reopened, false, false); + StateDb::::open(&mut db, mode_when_reopened, false, false); if let Ok(expected_mode) = expected_effective_mode_when_reopenned { let (state_db_init, state_db_reopened) = state_db_reopen_result.unwrap(); db.commit(&state_db_init); diff --git a/client/state-db/src/pruning.rs b/client/state-db/src/pruning.rs index 0fdcb8e822b6f..791ddf7598678 100644 --- a/client/state-db/src/pruning.rs +++ b/client/state-db/src/pruning.rs @@ -27,18 +27,22 @@ use crate::{to_meta_key, CommitSet, Error, Hash, MetaDb}; use codec::{Decode, Encode}; use log::{trace, warn}; -use std::collections::{HashMap, HashSet, VecDeque}; +use std::{ + cmp, + collections::{HashMap, HashSet, VecDeque}, +}; const LAST_PRUNED: &[u8] = b"last_pruned"; const PRUNING_JOURNAL: &[u8] = b"pruning_journal"; +// default pruning window size plus a magic number keep most common ops in cache +const CACHE_BATCH_SIZE: usize = 256 + 10; /// See module documentation. #[derive(parity_util_mem_derive::MallocSizeOf)] -pub struct RefWindow { - /// A queue of keys that should be deleted for each block in the pruning window. - death_rows: VecDeque>, - /// An index that maps each key from `death_rows` to block number. - death_index: HashMap, +pub struct RefWindow { + /// A queue of blocks keep tracking keys that should be deleted for each block in the + /// pruning window. + death_rows_queue: DeathRowQueue, /// Block number that corresponds to the front of `death_rows`. pending_number: u64, /// Number of call of `note_canonical` after @@ -47,16 +51,248 @@ pub struct RefWindow { /// Number of calls of `prune_one` after /// last call `apply_pending` or `revert_pending` pending_prunings: usize, - /// Keep track of re-inserted keys and do not delete them when pruning. - /// Setting this to false requires backend that supports reference - /// counting. - count_insertions: bool, } -#[derive(Debug, PartialEq, Eq, parity_util_mem_derive::MallocSizeOf)] +/// `DeathRowQueue` used to keep track of blocks in the pruning window, there are two flavors: +/// - `MemQueue`, used when the backend database do not supports reference counting, keep all +/// blocks in memory, and keep track of re-inserted keys to not delete them when pruning +/// - `DbBackedQueue`, used when the backend database supports reference counting, only keep +/// a few number of blocks in memory and load more blocks on demand, it also keep all the +/// block's hash in memory for checking block existence +#[derive(parity_util_mem_derive::MallocSizeOf)] +enum DeathRowQueue { + MemQueue { + /// A queue of keys that should be deleted for each block in the pruning window. + death_rows: VecDeque>, + /// An index that maps each key from `death_rows` to block number. + death_index: HashMap, + }, + DbBackedQueue { + // The backend database + db: D, + /// A queue of keys that should be deleted for each block in the pruning window. + /// Only caching the first fews blocks of the pruning window, blocks inside are + /// successive and ordered by block number + cache: VecDeque>, + /// A queue of hashs of all the blocks that not loaded into cache, the first hash of + /// the queue followe the last block of `cache`, namely `block_numer(hashs[0])` == + /// `block_numer(cache.last()) + 1`, hashs inside are successive and ordered by block + /// number + hashs: VecDeque, + }, +} + +impl DeathRowQueue { + fn new(db: Option) -> DeathRowQueue { + match db { + Some(db) => DeathRowQueue::DbBackedQueue { + db, + hashs: VecDeque::new(), + cache: VecDeque::with_capacity(CACHE_BATCH_SIZE), + }, + None => + DeathRowQueue::MemQueue { death_rows: VecDeque::new(), death_index: HashMap::new() }, + } + } + + /// import a new block to the back of the queue + fn import(&mut self, base: u64, hash: BlockHash, inserted: Vec, deleted: Vec) { + match self { + DeathRowQueue::DbBackedQueue { hashs, cache, .. } => { + // `hashs` is empty means currently all block are loaded into `cache` + // thus if `cache` is not full, load the next block into `cache` too + if hashs.is_empty() && cache.len() < CACHE_BATCH_SIZE { + cache.push_back(DeathRow { hash, deleted: deleted.into_iter().collect() }); + } else { + hashs.push_back(hash); + } + }, + DeathRowQueue::MemQueue { death_rows, death_index } => { + // remove all re-inserted keys from death rows + for k in inserted { + if let Some(block) = death_index.remove(&k) { + death_rows[(block - base) as usize].deleted.remove(&k); + } + } + // add new keys + let imported_block = base + death_rows.len() as u64; + for k in deleted.iter() { + death_index.insert(k.clone(), imported_block); + } + death_rows.push_back(DeathRow { hash, deleted: deleted.into_iter().collect() }); + }, + } + } + + /// Pop out one block from the front of the queue, `base` is the block number + /// of the first block of the queue + fn pop_front( + &mut self, + base: u64, + ) -> Result>, Error> { + match self { + DeathRowQueue::DbBackedQueue { db, hashs, cache, .. } => { + if cache.is_empty() && !hashs.is_empty() { + // load more blocks from db since there are still blocks in it + DeathRowQueue::load_batch_from_db(db, hashs, cache, base)?; + } + Ok(cache.pop_front()) + }, + DeathRowQueue::MemQueue { death_rows, death_index } => match death_rows.pop_front() { + Some(row) => { + for k in row.deleted.iter() { + death_index.remove(k); + } + Ok(Some(row)) + }, + None => Ok(None), + }, + } + } + + fn has_block(&self, hash: &BlockHash, skip: usize) -> bool { + // TODO: if the pruning window is set to large, this may hurt performance + match self { + DeathRowQueue::DbBackedQueue { hashs, cache, .. } => cache + .iter() + .map(|row| &row.hash) + .chain(hashs.iter()) + .skip(skip) + .any(|h| h == hash), + DeathRowQueue::MemQueue { death_rows, .. } => + death_rows.iter().map(|row| &row.hash).skip(skip).any(|h| h == hash), + } + } + + /// Revert recent additions to the queue, namely remove `amount` number of blocks from the back + /// of the queue, `base` is the block number of the first block of the queue + fn revert_recent_add(&mut self, base: u64, amout: usize) { + debug_assert!(amout <= self.len()); + match self { + DeathRowQueue::DbBackedQueue { hashs, cache, .. } => { + // remove from `hashs` if it can cover + if hashs.len() >= amout { + hashs.truncate(hashs.len() - amout); + return + } + // clear `hashs` and remove remain blocks from `cache` + let remain = amout - hashs.len(); + hashs.clear(); + cache.truncate(cache.len() - remain); + }, + DeathRowQueue::MemQueue { death_rows, death_index } => { + // Revert recent addition to the queue + // Note that pending insertions might cause some existing deletions to be removed + // from `death_index` We don't bother to track and revert that for now. This means + // that a few nodes might end up no being deleted in case transaction fails and + // `revert_pending` is called. + death_rows.truncate(death_rows.len() - amout); + let new_max_block = death_rows.len() as u64 + base; + death_index.retain(|_, block| *block < new_max_block); + }, + } + } + + /// Load a batch of blocks from the backend database into `cache`, start from (and include) the + /// next block followe the last block of `cache`, `base` is the block number of the first block + /// of the queue + fn load_batch_from_db( + db: &D, + hashs: &mut VecDeque, + cache: &mut VecDeque>, + base: u64, + ) -> Result<(), Error> { + // return if all blocks already loaded into `cache` and there are no other + // blocks in the backend database + if hashs.len() == 0 { + return Ok(()) + } + let start = base + cache.len() as u64; + let batch_size = cmp::min(hashs.len(), CACHE_BATCH_SIZE); + let mut loaded = 0; + for i in 0..batch_size as u64 { + match load_death_row_from_db::(db, start + i)? { + Some(row) => { + // the incoming block's hash should be the same as the first hash of + // `hashs`, if there are corrupted data `load_death_row_from_db` should + // return a db error + debug_assert_eq!(Some(row.hash.clone()), hashs.pop_front()); + cache.push_back(row); + loaded += 1; + }, + None => break, + } + } + // `loaded` should be the same as what we expect, if there are missing blocks + // `load_death_row_from_db` should return a db error + debug_assert_eq!(batch_size, loaded); + Ok(()) + } + + /// Get the block in the given index of the queue, `base` is the block number of the + /// first block of the queue + fn get( + &mut self, + base: u64, + index: usize, + ) -> Result>, Error> { + match self { + DeathRowQueue::DbBackedQueue { db, hashs, cache } => { + // check if `index` target a block reside on disk + if index >= cache.len() && index < cache.len() + hashs.len() { + // if `index` target the next batch of `DeathRow`, load a batch from db + if index - cache.len() < cmp::min(hashs.len(), CACHE_BATCH_SIZE) { + DeathRowQueue::load_batch_from_db(db, hashs, cache, base)?; + } else { + // load a single `DeathRow` from db, but do not insert it to `cache` + // because `cache` is a queue of successive `DeathRow` + // NOTE: this branch should not be entered because blocks are visited + // in successive increasing order, just keeping it for robustness + return Ok(load_death_row_from_db(db, base + index as u64)?) + } + } + Ok(cache.get(index).cloned()) + }, + DeathRowQueue::MemQueue { death_rows, .. } => Ok(death_rows.get(index).cloned()), + } + } + + /// Return the number of block in the pruning window + fn len(&self) -> usize { + match self { + DeathRowQueue::DbBackedQueue { hashs, cache, .. } => cache.len() + hashs.len(), + DeathRowQueue::MemQueue { death_rows, .. } => death_rows.len(), + } + } + + #[cfg(test)] + fn get_mem_queue_state( + &self, + ) -> Option<(&VecDeque>, &HashMap)> { + match self { + DeathRowQueue::DbBackedQueue { .. } => None, + DeathRowQueue::MemQueue { death_rows, death_index } => Some((death_rows, death_index)), + } + } +} + +fn load_death_row_from_db( + db: &D, + block: u64, +) -> Result>, Error> { + let journal_key = to_journal_key(block); + match db.get_meta(&journal_key).map_err(Error::Db)? { + Some(record) => { + let JournalRecord { hash, deleted, .. } = Decode::decode(&mut record.as_slice())?; + Ok(Some(DeathRow { hash, deleted: deleted.into_iter().collect() })) + }, + None => Ok(None), + } +} + +#[derive(Clone, Debug, PartialEq, Eq, parity_util_mem_derive::MallocSizeOf)] struct DeathRow { hash: BlockHash, - journal_key: Vec, deleted: HashSet, } @@ -71,24 +307,24 @@ fn to_journal_key(block: u64) -> Vec { to_meta_key(PRUNING_JOURNAL, &block) } -impl RefWindow { - pub fn new( +impl RefWindow { + pub fn new( db: &D, count_insertions: bool, - ) -> Result, Error> { + ) -> Result, Error> { let last_pruned = db.get_meta(&to_meta_key(LAST_PRUNED, &())).map_err(Error::Db)?; let pending_number: u64 = match last_pruned { Some(buffer) => u64::decode(&mut buffer.as_slice())? + 1, None => 0, }; let mut block = pending_number; + let death_rows_queue = + DeathRowQueue::new(if count_insertions { None } else { Some(db.clone()) }); let mut pruning = RefWindow { - death_rows: Default::default(), - death_index: Default::default(), + death_rows_queue, pending_number, pending_canonicalizations: 0, pending_prunings: 0, - count_insertions, }; // read the journal trace!(target: "state-db", "Reading pruning journal. Pending #{}", pending_number); @@ -99,10 +335,10 @@ impl RefWindow { let record: JournalRecord = Decode::decode(&mut record.as_slice())?; trace!(target: "state-db", "Pruning journal entry {} ({} inserted, {} deleted)", block, record.inserted.len(), record.deleted.len()); - pruning.import( - &record.hash, - journal_key, - record.inserted.into_iter(), + pruning.death_rows_queue.import( + pending_number, + record.hash, + record.inserted, record.deleted, ); }, @@ -113,40 +349,15 @@ impl RefWindow { Ok(pruning) } - fn import>( - &mut self, - hash: &BlockHash, - journal_key: Vec, - inserted: I, - deleted: Vec, - ) { - if self.count_insertions { - // remove all re-inserted keys from death rows - for k in inserted { - if let Some(block) = self.death_index.remove(&k) { - self.death_rows[(block - self.pending_number) as usize].deleted.remove(&k); - } - } - - // add new keys - let imported_block = self.pending_number + self.death_rows.len() as u64; - for k in deleted.iter() { - self.death_index.insert(k.clone(), imported_block); - } - } - self.death_rows.push_back(DeathRow { - hash: hash.clone(), - deleted: deleted.into_iter().collect(), - journal_key, - }); - } - pub fn window_size(&self) -> u64 { - (self.death_rows.len() - self.pending_prunings) as u64 + (self.death_rows_queue.len() - self.pending_prunings) as u64 } - pub fn next_hash(&self) -> Option { - self.death_rows.get(self.pending_prunings).map(|r| r.hash.clone()) + pub fn next_hash(&mut self) -> Result, Error> { + Ok(self + .death_rows_queue + .get(self.pending_number, self.pending_prunings)? + .map(|r| r.hash)) } pub fn mem_used(&self) -> usize { @@ -158,40 +369,46 @@ impl RefWindow { } pub fn have_block(&self, hash: &BlockHash) -> bool { - self.death_rows.iter().skip(self.pending_prunings).any(|r| r.hash == *hash) + self.death_rows_queue.has_block(hash, self.pending_prunings) } /// Prune next block. Expects at least one block in the window. Adds changes to `commit`. - pub fn prune_one(&mut self, commit: &mut CommitSet) { - if let Some(pruned) = self.death_rows.get(self.pending_prunings) { + pub fn prune_one(&mut self, commit: &mut CommitSet) -> Result<(), Error> { + if let Some(pruned) = + self.death_rows_queue.get(self.pending_number, self.pending_prunings)? + { trace!(target: "state-db", "Pruning {:?} ({} deleted)", pruned.hash, pruned.deleted.len()); let index = self.pending_number + self.pending_prunings as u64; - commit.data.deleted.extend(pruned.deleted.iter().cloned()); + commit.data.deleted.extend(pruned.deleted.into_iter()); commit.meta.inserted.push((to_meta_key(LAST_PRUNED, &()), index.encode())); - commit.meta.deleted.push(pruned.journal_key.clone()); + commit + .meta + .deleted + .push(to_journal_key(self.pending_number + self.pending_prunings as u64)); self.pending_prunings += 1; } else { warn!(target: "state-db", "Trying to prune when there's nothing to prune"); } + Ok(()) } /// Add a change set to the window. Creates a journal record and pushes it to `commit` pub fn note_canonical(&mut self, hash: &BlockHash, commit: &mut CommitSet) { trace!(target: "state-db", "Adding to pruning window: {:?} ({} inserted, {} deleted)", hash, commit.data.inserted.len(), commit.data.deleted.len()); - let inserted = if self.count_insertions { + let inserted = if matches!(self.death_rows_queue, DeathRowQueue::MemQueue { .. }) { commit.data.inserted.iter().map(|(k, _)| k.clone()).collect() } else { Default::default() }; let deleted = ::std::mem::take(&mut commit.data.deleted); let journal_record = JournalRecord { hash: hash.clone(), inserted, deleted }; - let block = self.pending_number + self.death_rows.len() as u64; + let block = self.pending_number + self.death_rows_queue.len() as u64; let journal_key = to_journal_key(block); commit.meta.inserted.push((journal_key.clone(), journal_record.encode())); - self.import( - &journal_record.hash, - journal_key, - journal_record.inserted.into_iter(), + self.death_rows_queue.import( + self.pending_number, + journal_record.hash, + journal_record.inserted, journal_record.deleted, ); self.pending_canonicalizations += 1; @@ -202,15 +419,14 @@ impl RefWindow { self.pending_canonicalizations = 0; for _ in 0..self.pending_prunings { let pruned = self - .death_rows - .pop_front() - .expect("pending_prunings is always < death_rows.len()"); + .death_rows_queue + .pop_front(self.pending_number) + // NOTE: `pop_front` should not return `MetaDb::Error` because blocks are visited + // by `RefWindow::prune_one` first then `RefWindow::apply_pending` and + // `DeathRowQueue::get` should load the blocks into cache already + .expect("block must loaded in cache thus no MetaDb::Error") + .expect("pending_prunings is always < death_rows_queue.len()"); trace!(target: "state-db", "Applying pruning {:?} ({} deleted)", pruned.hash, pruned.deleted.len()); - if self.count_insertions { - for k in pruned.deleted.iter() { - self.death_index.remove(k); - } - } self.pending_number += 1; } self.pending_prunings = 0; @@ -218,16 +434,8 @@ impl RefWindow { /// Revert all pending changes pub fn revert_pending(&mut self) { - // Revert pending deletions. - // Note that pending insertions might cause some existing deletions to be removed from - // `death_index` We don't bother to track and revert that for now. This means that a few - // nodes might end up no being deleted in case transaction fails and `revert_pending` is - // called. - self.death_rows.truncate(self.death_rows.len() - self.pending_canonicalizations); - if self.count_insertions { - let new_max_block = self.death_rows.len() as u64 + self.pending_number; - self.death_index.retain(|_, block| *block < new_max_block); - } + self.death_rows_queue + .revert_recent_add(self.pending_number, self.pending_canonicalizations); self.pending_canonicalizations = 0; self.pending_prunings = 0; } @@ -237,36 +445,42 @@ impl RefWindow { mod tests { use super::RefWindow; use crate::{ + pruning::DeathRowQueue, test::{make_commit, make_db, TestDb}, CommitSet, }; use sp_core::H256; - fn check_journal(pruning: &RefWindow, db: &TestDb) { - let restored: RefWindow = RefWindow::new(db, pruning.count_insertions).unwrap(); + fn check_journal(pruning: &RefWindow, db: &TestDb) { + let count_insertions = matches!(pruning.death_rows_queue, DeathRowQueue::MemQueue { .. }); + let restored: RefWindow = RefWindow::new(db, count_insertions).unwrap(); assert_eq!(pruning.pending_number, restored.pending_number); - assert_eq!(pruning.death_rows, restored.death_rows); - assert_eq!(pruning.death_index, restored.death_index); + assert_eq!( + pruning.death_rows_queue.get_mem_queue_state(), + restored.death_rows_queue.get_mem_queue_state() + ); } #[test] fn created_from_empty_db() { let db = make_db(&[]); - let pruning: RefWindow = RefWindow::new(&db, true).unwrap(); + let pruning: RefWindow = RefWindow::new(&db, true).unwrap(); assert_eq!(pruning.pending_number, 0); - assert!(pruning.death_rows.is_empty()); - assert!(pruning.death_index.is_empty()); + let (death_rows, death_index) = pruning.death_rows_queue.get_mem_queue_state().unwrap(); + assert!(death_rows.is_empty()); + assert!(death_index.is_empty()); } #[test] fn prune_empty() { let db = make_db(&[]); - let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); + let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); let mut commit = CommitSet::default(); - pruning.prune_one(&mut commit); + pruning.prune_one(&mut commit).unwrap(); assert_eq!(pruning.pending_number, 0); - assert!(pruning.death_rows.is_empty()); - assert!(pruning.death_index.is_empty()); + let (death_rows, death_index) = pruning.death_rows_queue.get_mem_queue_state().unwrap(); + assert!(death_rows.is_empty()); + assert!(death_index.is_empty()); assert!(pruning.pending_prunings == 0); assert!(pruning.pending_canonicalizations == 0); } @@ -274,7 +488,7 @@ mod tests { #[test] fn prune_one() { let mut db = make_db(&[1, 2, 3]); - let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); + let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); let mut commit = make_commit(&[4, 5], &[1, 3]); let h = H256::random(); pruning.note_canonical(&h, &mut commit); @@ -283,27 +497,29 @@ mod tests { pruning.apply_pending(); assert!(pruning.have_block(&h)); assert!(commit.data.deleted.is_empty()); - assert_eq!(pruning.death_rows.len(), 1); - assert_eq!(pruning.death_index.len(), 2); + let (death_rows, death_index) = pruning.death_rows_queue.get_mem_queue_state().unwrap(); + assert_eq!(death_rows.len(), 1); + assert_eq!(death_index.len(), 2); assert!(db.data_eq(&make_db(&[1, 2, 3, 4, 5]))); check_journal(&pruning, &db); let mut commit = CommitSet::default(); - pruning.prune_one(&mut commit); + pruning.prune_one(&mut commit).unwrap(); assert!(!pruning.have_block(&h)); db.commit(&commit); pruning.apply_pending(); assert!(!pruning.have_block(&h)); assert!(db.data_eq(&make_db(&[2, 4, 5]))); - assert!(pruning.death_rows.is_empty()); - assert!(pruning.death_index.is_empty()); + let (death_rows, death_index) = pruning.death_rows_queue.get_mem_queue_state().unwrap(); + assert!(death_rows.is_empty()); + assert!(death_index.is_empty()); assert_eq!(pruning.pending_number, 1); } #[test] fn prune_two() { let mut db = make_db(&[1, 2, 3]); - let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); + let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); let mut commit = make_commit(&[4], &[1]); pruning.note_canonical(&H256::random(), &mut commit); db.commit(&commit); @@ -316,12 +532,12 @@ mod tests { check_journal(&pruning, &db); let mut commit = CommitSet::default(); - pruning.prune_one(&mut commit); + pruning.prune_one(&mut commit).unwrap(); db.commit(&commit); pruning.apply_pending(); assert!(db.data_eq(&make_db(&[2, 3, 4, 5]))); let mut commit = CommitSet::default(); - pruning.prune_one(&mut commit); + pruning.prune_one(&mut commit).unwrap(); db.commit(&commit); pruning.apply_pending(); assert!(db.data_eq(&make_db(&[3, 4, 5]))); @@ -331,7 +547,7 @@ mod tests { #[test] fn prune_two_pending() { let mut db = make_db(&[1, 2, 3]); - let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); + let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); let mut commit = make_commit(&[4], &[1]); pruning.note_canonical(&H256::random(), &mut commit); db.commit(&commit); @@ -340,11 +556,11 @@ mod tests { db.commit(&commit); assert!(db.data_eq(&make_db(&[1, 2, 3, 4, 5]))); let mut commit = CommitSet::default(); - pruning.prune_one(&mut commit); + pruning.prune_one(&mut commit).unwrap(); db.commit(&commit); assert!(db.data_eq(&make_db(&[2, 3, 4, 5]))); let mut commit = CommitSet::default(); - pruning.prune_one(&mut commit); + pruning.prune_one(&mut commit).unwrap(); db.commit(&commit); pruning.apply_pending(); assert!(db.data_eq(&make_db(&[3, 4, 5]))); @@ -354,7 +570,7 @@ mod tests { #[test] fn reinserted_survives() { let mut db = make_db(&[1, 2, 3]); - let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); + let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); let mut commit = make_commit(&[], &[2]); pruning.note_canonical(&H256::random(), &mut commit); db.commit(&commit); @@ -370,14 +586,14 @@ mod tests { check_journal(&pruning, &db); let mut commit = CommitSet::default(); - pruning.prune_one(&mut commit); + pruning.prune_one(&mut commit).unwrap(); db.commit(&commit); assert!(db.data_eq(&make_db(&[1, 2, 3]))); let mut commit = CommitSet::default(); - pruning.prune_one(&mut commit); + pruning.prune_one(&mut commit).unwrap(); db.commit(&commit); assert!(db.data_eq(&make_db(&[1, 2, 3]))); - pruning.prune_one(&mut commit); + pruning.prune_one(&mut commit).unwrap(); db.commit(&commit); assert!(db.data_eq(&make_db(&[1, 3]))); pruning.apply_pending(); @@ -387,7 +603,7 @@ mod tests { #[test] fn reinserted_survive_pending() { let mut db = make_db(&[1, 2, 3]); - let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); + let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); let mut commit = make_commit(&[], &[2]); pruning.note_canonical(&H256::random(), &mut commit); db.commit(&commit); @@ -400,14 +616,14 @@ mod tests { assert!(db.data_eq(&make_db(&[1, 2, 3]))); let mut commit = CommitSet::default(); - pruning.prune_one(&mut commit); + pruning.prune_one(&mut commit).unwrap(); db.commit(&commit); assert!(db.data_eq(&make_db(&[1, 2, 3]))); let mut commit = CommitSet::default(); - pruning.prune_one(&mut commit); + pruning.prune_one(&mut commit).unwrap(); db.commit(&commit); assert!(db.data_eq(&make_db(&[1, 2, 3]))); - pruning.prune_one(&mut commit); + pruning.prune_one(&mut commit).unwrap(); db.commit(&commit); assert!(db.data_eq(&make_db(&[1, 3]))); pruning.apply_pending(); @@ -417,7 +633,7 @@ mod tests { #[test] fn reinserted_ignores() { let mut db = make_db(&[1, 2, 3]); - let mut pruning: RefWindow = RefWindow::new(&db, false).unwrap(); + let mut pruning: RefWindow = RefWindow::new(&db, false).unwrap(); let mut commit = make_commit(&[], &[2]); pruning.note_canonical(&H256::random(), &mut commit); db.commit(&commit); @@ -433,9 +649,8 @@ mod tests { check_journal(&pruning, &db); let mut commit = CommitSet::default(); - pruning.prune_one(&mut commit); + pruning.prune_one(&mut commit).unwrap(); db.commit(&commit); assert!(db.data_eq(&make_db(&[1, 3]))); - assert!(pruning.death_index.is_empty()); } } From a0280faae688440204c67e8f404401aaefe2d359 Mon Sep 17 00:00:00 2001 From: linning Date: Fri, 5 Aug 2022 02:48:39 +0800 Subject: [PATCH 02/19] avoid cloning for next_hash Signed-off-by: linning --- client/state-db/src/lib.rs | 4 ++-- client/state-db/src/pruning.rs | 21 ++++++++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/client/state-db/src/lib.rs b/client/state-db/src/lib.rs index 90b4ce2a4e314..aad0008ab0051 100644 --- a/client/state-db/src/lib.rs +++ b/client/state-db/src/lib.rs @@ -375,7 +375,7 @@ impl } let pinned = &self.pinned; - if pruning.next_hash()?.map_or(false, |h| pinned.contains_key(&h)) { + if pruning.next_hash().map_or(false, |h| pinned.contains_key(&h)) { break } pruning.prune_one(commit)?; @@ -464,7 +464,7 @@ impl trace!( target: "forks", "First available: {:?} ({}), Last canon: {:?} ({}), Best forks: {:?}", - self.pruning.as_mut().and_then(|p| p.next_hash().ok()), + self.pruning.as_ref().and_then(|p| p.next_hash()), self.pruning.as_ref().map(|p| p.pending()).unwrap_or(0), self.non_canonical.last_canonicalized_hash(), self.non_canonical.last_canonicalized_block_number().unwrap_or(0), diff --git a/client/state-db/src/pruning.rs b/client/state-db/src/pruning.rs index 791ddf7598678..17a4abb2de5f6 100644 --- a/client/state-db/src/pruning.rs +++ b/client/state-db/src/pruning.rs @@ -257,6 +257,20 @@ impl DeathRowQueue { } } + /// Get the hash of the block at the given `index` of the queue + fn get_hash(&self, index: usize) -> Option { + match self { + DeathRowQueue::DbBackedQueue { cache, hashs, .. } => + if index < cache.len() { + cache.get(index).map(|r| r.hash.clone()) + } else { + hashs.get(index - cache.len()).cloned() + }, + DeathRowQueue::MemQueue { death_rows, .. } => + death_rows.get(index).map(|r| r.hash.clone()), + } + } + /// Return the number of block in the pruning window fn len(&self) -> usize { match self { @@ -353,11 +367,8 @@ impl RefWindow { (self.death_rows_queue.len() - self.pending_prunings) as u64 } - pub fn next_hash(&mut self) -> Result, Error> { - Ok(self - .death_rows_queue - .get(self.pending_number, self.pending_prunings)? - .map(|r| r.hash)) + pub fn next_hash(&self) -> Option { + self.death_rows_queue.get_hash(self.pending_prunings) } pub fn mem_used(&self) -> usize { From b05f6997d04aea6605ac36a74707ba77e71a4f7d Mon Sep 17 00:00:00 2001 From: linning Date: Fri, 5 Aug 2022 06:45:01 +0800 Subject: [PATCH 03/19] add tests Signed-off-by: linning --- client/state-db/src/pruning.rs | 275 ++++++++++++++++++++++++--------- 1 file changed, 198 insertions(+), 77 deletions(-) diff --git a/client/state-db/src/pruning.rs b/client/state-db/src/pruning.rs index 17a4abb2de5f6..a63c71ecdf8bd 100644 --- a/client/state-db/src/pruning.rs +++ b/client/state-db/src/pruning.rs @@ -34,7 +34,7 @@ use std::{ const LAST_PRUNED: &[u8] = b"last_pruned"; const PRUNING_JOURNAL: &[u8] = b"pruning_journal"; -// default pruning window size plus a magic number keep most common ops in cache +// Default pruning window size plus a magic number keep most common ops in cache const CACHE_BATCH_SIZE: usize = 256 + 10; /// See module documentation. @@ -42,7 +42,7 @@ const CACHE_BATCH_SIZE: usize = 256 + 10; pub struct RefWindow { /// A queue of blocks keep tracking keys that should be deleted for each block in the /// pruning window. - death_rows_queue: DeathRowQueue, + queue: DeathRowQueue, /// Block number that corresponds to the front of `death_rows`. pending_number: u64, /// Number of call of `note_canonical` after @@ -54,20 +54,20 @@ pub struct RefWindow { } /// `DeathRowQueue` used to keep track of blocks in the pruning window, there are two flavors: -/// - `MemQueue`, used when the backend database do not supports reference counting, keep all +/// - `Mem`, used when the backend database do not supports reference counting, keep all /// blocks in memory, and keep track of re-inserted keys to not delete them when pruning -/// - `DbBackedQueue`, used when the backend database supports reference counting, only keep +/// - `DbBacked`, used when the backend database supports reference counting, only keep /// a few number of blocks in memory and load more blocks on demand, it also keep all the /// block's hash in memory for checking block existence #[derive(parity_util_mem_derive::MallocSizeOf)] enum DeathRowQueue { - MemQueue { + Mem { /// A queue of keys that should be deleted for each block in the pruning window. death_rows: VecDeque>, /// An index that maps each key from `death_rows` to block number. death_index: HashMap, }, - DbBackedQueue { + DbBacked { // The backend database db: D, /// A queue of keys that should be deleted for each block in the pruning window. @@ -83,22 +83,26 @@ enum DeathRowQueue { } impl DeathRowQueue { - fn new(db: Option) -> DeathRowQueue { - match db { - Some(db) => DeathRowQueue::DbBackedQueue { - db, - hashs: VecDeque::new(), - cache: VecDeque::with_capacity(CACHE_BATCH_SIZE), - }, - None => - DeathRowQueue::MemQueue { death_rows: VecDeque::new(), death_index: HashMap::new() }, + /// Return a `DeathRowQueue` that all blocks are keep in memory + fn new_mem() -> DeathRowQueue { + DeathRowQueue::Mem { death_rows: VecDeque::new(), death_index: HashMap::new() } + } + + /// Return a `DeathRowQueue` that backed by an database, and only keep a few number + /// of blocks in memory + fn new_db_backed(db: D) -> DeathRowQueue { + DeathRowQueue::DbBacked { + db, + cache: VecDeque::with_capacity(CACHE_BATCH_SIZE), + hashs: VecDeque::new(), } } /// import a new block to the back of the queue - fn import(&mut self, base: u64, hash: BlockHash, inserted: Vec, deleted: Vec) { + fn import(&mut self, base: u64, journal_record: JournalRecord) { + let JournalRecord { hash, inserted, deleted } = journal_record; match self { - DeathRowQueue::DbBackedQueue { hashs, cache, .. } => { + DeathRowQueue::DbBacked { hashs, cache, .. } => { // `hashs` is empty means currently all block are loaded into `cache` // thus if `cache` is not full, load the next block into `cache` too if hashs.is_empty() && cache.len() < CACHE_BATCH_SIZE { @@ -107,7 +111,7 @@ impl DeathRowQueue { hashs.push_back(hash); } }, - DeathRowQueue::MemQueue { death_rows, death_index } => { + DeathRowQueue::Mem { death_rows, death_index } => { // remove all re-inserted keys from death rows for k in inserted { if let Some(block) = death_index.remove(&k) { @@ -131,14 +135,14 @@ impl DeathRowQueue { base: u64, ) -> Result>, Error> { match self { - DeathRowQueue::DbBackedQueue { db, hashs, cache, .. } => { + DeathRowQueue::DbBacked { db, hashs, cache } => { if cache.is_empty() && !hashs.is_empty() { // load more blocks from db since there are still blocks in it DeathRowQueue::load_batch_from_db(db, hashs, cache, base)?; } Ok(cache.pop_front()) }, - DeathRowQueue::MemQueue { death_rows, death_index } => match death_rows.pop_front() { + DeathRowQueue::Mem { death_rows, death_index } => match death_rows.pop_front() { Some(row) => { for k in row.deleted.iter() { death_index.remove(k); @@ -153,13 +157,13 @@ impl DeathRowQueue { fn has_block(&self, hash: &BlockHash, skip: usize) -> bool { // TODO: if the pruning window is set to large, this may hurt performance match self { - DeathRowQueue::DbBackedQueue { hashs, cache, .. } => cache + DeathRowQueue::DbBacked { hashs, cache, .. } => cache .iter() .map(|row| &row.hash) .chain(hashs.iter()) .skip(skip) .any(|h| h == hash), - DeathRowQueue::MemQueue { death_rows, .. } => + DeathRowQueue::Mem { death_rows, .. } => death_rows.iter().map(|row| &row.hash).skip(skip).any(|h| h == hash), } } @@ -169,7 +173,7 @@ impl DeathRowQueue { fn revert_recent_add(&mut self, base: u64, amout: usize) { debug_assert!(amout <= self.len()); match self { - DeathRowQueue::DbBackedQueue { hashs, cache, .. } => { + DeathRowQueue::DbBacked { hashs, cache, .. } => { // remove from `hashs` if it can cover if hashs.len() >= amout { hashs.truncate(hashs.len() - amout); @@ -180,7 +184,7 @@ impl DeathRowQueue { hashs.clear(); cache.truncate(cache.len() - remain); }, - DeathRowQueue::MemQueue { death_rows, death_index } => { + DeathRowQueue::Mem { death_rows, death_index } => { // Revert recent addition to the queue // Note that pending insertions might cause some existing deletions to be removed // from `death_index` We don't bother to track and revert that for now. This means @@ -211,7 +215,7 @@ impl DeathRowQueue { let batch_size = cmp::min(hashs.len(), CACHE_BATCH_SIZE); let mut loaded = 0; for i in 0..batch_size as u64 { - match load_death_row_from_db::(db, start + i)? { + match load_death_row_from_db::(db, start + i)? { Some(row) => { // the incoming block's hash should be the same as the first hash of // `hashs`, if there are corrupted data `load_death_row_from_db` should @@ -237,7 +241,7 @@ impl DeathRowQueue { index: usize, ) -> Result>, Error> { match self { - DeathRowQueue::DbBackedQueue { db, hashs, cache } => { + DeathRowQueue::DbBacked { db, hashs, cache } => { // check if `index` target a block reside on disk if index >= cache.len() && index < cache.len() + hashs.len() { // if `index` target the next batch of `DeathRow`, load a batch from db @@ -253,29 +257,28 @@ impl DeathRowQueue { } Ok(cache.get(index).cloned()) }, - DeathRowQueue::MemQueue { death_rows, .. } => Ok(death_rows.get(index).cloned()), + DeathRowQueue::Mem { death_rows, .. } => Ok(death_rows.get(index).cloned()), } } /// Get the hash of the block at the given `index` of the queue fn get_hash(&self, index: usize) -> Option { match self { - DeathRowQueue::DbBackedQueue { cache, hashs, .. } => + DeathRowQueue::DbBacked { cache, hashs, .. } => if index < cache.len() { cache.get(index).map(|r| r.hash.clone()) } else { hashs.get(index - cache.len()).cloned() }, - DeathRowQueue::MemQueue { death_rows, .. } => - death_rows.get(index).map(|r| r.hash.clone()), + DeathRowQueue::Mem { death_rows, .. } => death_rows.get(index).map(|r| r.hash.clone()), } } /// Return the number of block in the pruning window fn len(&self) -> usize { match self { - DeathRowQueue::DbBackedQueue { hashs, cache, .. } => cache.len() + hashs.len(), - DeathRowQueue::MemQueue { death_rows, .. } => death_rows.len(), + DeathRowQueue::DbBacked { hashs, cache, .. } => cache.len() + hashs.len(), + DeathRowQueue::Mem { death_rows, .. } => death_rows.len(), } } @@ -284,8 +287,26 @@ impl DeathRowQueue { &self, ) -> Option<(&VecDeque>, &HashMap)> { match self { - DeathRowQueue::DbBackedQueue { .. } => None, - DeathRowQueue::MemQueue { death_rows, death_index } => Some((death_rows, death_index)), + DeathRowQueue::DbBacked { .. } => None, + DeathRowQueue::Mem { death_rows, death_index } => Some((death_rows, death_index)), + } + } + + #[cfg(test)] + fn get_db_backed_queue_state( + &self, + ) -> Option<(&VecDeque>, &VecDeque)> { + match self { + DeathRowQueue::DbBacked { cache, hashs, .. } => Some((cache, hashs)), + DeathRowQueue::Mem { .. } => None, + } + } + + #[cfg(test)] + fn get_db(&mut self) -> Option<&mut D> { + match self { + DeathRowQueue::DbBacked { db, .. } => Some(db), + DeathRowQueue::Mem { .. } => None, } } } @@ -332,14 +353,13 @@ impl RefWindow { None => 0, }; let mut block = pending_number; - let death_rows_queue = - DeathRowQueue::new(if count_insertions { None } else { Some(db.clone()) }); - let mut pruning = RefWindow { - death_rows_queue, - pending_number, - pending_canonicalizations: 0, - pending_prunings: 0, + let queue = if count_insertions { + DeathRowQueue::new_mem() + } else { + DeathRowQueue::new_db_backed(db.clone()) }; + let mut pruning = + RefWindow { queue, pending_number, pending_canonicalizations: 0, pending_prunings: 0 }; // read the journal trace!(target: "state-db", "Reading pruning journal. Pending #{}", pending_number); loop { @@ -349,12 +369,7 @@ impl RefWindow { let record: JournalRecord = Decode::decode(&mut record.as_slice())?; trace!(target: "state-db", "Pruning journal entry {} ({} inserted, {} deleted)", block, record.inserted.len(), record.deleted.len()); - pruning.death_rows_queue.import( - pending_number, - record.hash, - record.inserted, - record.deleted, - ); + pruning.queue.import(pending_number, record); }, None => break, } @@ -364,11 +379,11 @@ impl RefWindow { } pub fn window_size(&self) -> u64 { - (self.death_rows_queue.len() - self.pending_prunings) as u64 + (self.queue.len() - self.pending_prunings) as u64 } pub fn next_hash(&self) -> Option { - self.death_rows_queue.get_hash(self.pending_prunings) + self.queue.get_hash(self.pending_prunings) } pub fn mem_used(&self) -> usize { @@ -380,14 +395,12 @@ impl RefWindow { } pub fn have_block(&self, hash: &BlockHash) -> bool { - self.death_rows_queue.has_block(hash, self.pending_prunings) + self.queue.has_block(hash, self.pending_prunings) } /// Prune next block. Expects at least one block in the window. Adds changes to `commit`. pub fn prune_one(&mut self, commit: &mut CommitSet) -> Result<(), Error> { - if let Some(pruned) = - self.death_rows_queue.get(self.pending_number, self.pending_prunings)? - { + if let Some(pruned) = self.queue.get(self.pending_number, self.pending_prunings)? { trace!(target: "state-db", "Pruning {:?} ({} deleted)", pruned.hash, pruned.deleted.len()); let index = self.pending_number + self.pending_prunings as u64; commit.data.deleted.extend(pruned.deleted.into_iter()); @@ -406,22 +419,16 @@ impl RefWindow { /// Add a change set to the window. Creates a journal record and pushes it to `commit` pub fn note_canonical(&mut self, hash: &BlockHash, commit: &mut CommitSet) { trace!(target: "state-db", "Adding to pruning window: {:?} ({} inserted, {} deleted)", hash, commit.data.inserted.len(), commit.data.deleted.len()); - let inserted = if matches!(self.death_rows_queue, DeathRowQueue::MemQueue { .. }) { + let inserted = if matches!(self.queue, DeathRowQueue::Mem { .. }) { commit.data.inserted.iter().map(|(k, _)| k.clone()).collect() } else { Default::default() }; let deleted = ::std::mem::take(&mut commit.data.deleted); let journal_record = JournalRecord { hash: hash.clone(), inserted, deleted }; - let block = self.pending_number + self.death_rows_queue.len() as u64; - let journal_key = to_journal_key(block); - commit.meta.inserted.push((journal_key.clone(), journal_record.encode())); - self.death_rows_queue.import( - self.pending_number, - journal_record.hash, - journal_record.inserted, - journal_record.deleted, - ); + let block = self.pending_number + self.queue.len() as u64; + commit.meta.inserted.push((to_journal_key(block), journal_record.encode())); + self.queue.import(self.pending_number, journal_record); self.pending_canonicalizations += 1; } @@ -430,13 +437,13 @@ impl RefWindow { self.pending_canonicalizations = 0; for _ in 0..self.pending_prunings { let pruned = self - .death_rows_queue + .queue .pop_front(self.pending_number) // NOTE: `pop_front` should not return `MetaDb::Error` because blocks are visited // by `RefWindow::prune_one` first then `RefWindow::apply_pending` and // `DeathRowQueue::get` should load the blocks into cache already .expect("block must loaded in cache thus no MetaDb::Error") - .expect("pending_prunings is always < death_rows_queue.len()"); + .expect("pending_prunings is always < queue.len()"); trace!(target: "state-db", "Applying pruning {:?} ({} deleted)", pruned.hash, pruned.deleted.len()); self.pending_number += 1; } @@ -445,7 +452,7 @@ impl RefWindow { /// Revert all pending changes pub fn revert_pending(&mut self) { - self.death_rows_queue + self.queue .revert_recent_add(self.pending_number, self.pending_canonicalizations); self.pending_canonicalizations = 0; self.pending_prunings = 0; @@ -454,22 +461,18 @@ impl RefWindow { #[cfg(test)] mod tests { - use super::RefWindow; + use super::{DeathRowQueue, RefWindow, CACHE_BATCH_SIZE}; use crate::{ - pruning::DeathRowQueue, test::{make_commit, make_db, TestDb}, CommitSet, }; use sp_core::H256; fn check_journal(pruning: &RefWindow, db: &TestDb) { - let count_insertions = matches!(pruning.death_rows_queue, DeathRowQueue::MemQueue { .. }); + let count_insertions = matches!(pruning.queue, DeathRowQueue::Mem { .. }); let restored: RefWindow = RefWindow::new(db, count_insertions).unwrap(); assert_eq!(pruning.pending_number, restored.pending_number); - assert_eq!( - pruning.death_rows_queue.get_mem_queue_state(), - restored.death_rows_queue.get_mem_queue_state() - ); + assert_eq!(pruning.queue.get_mem_queue_state(), restored.queue.get_mem_queue_state()); } #[test] @@ -477,7 +480,7 @@ mod tests { let db = make_db(&[]); let pruning: RefWindow = RefWindow::new(&db, true).unwrap(); assert_eq!(pruning.pending_number, 0); - let (death_rows, death_index) = pruning.death_rows_queue.get_mem_queue_state().unwrap(); + let (death_rows, death_index) = pruning.queue.get_mem_queue_state().unwrap(); assert!(death_rows.is_empty()); assert!(death_index.is_empty()); } @@ -489,7 +492,7 @@ mod tests { let mut commit = CommitSet::default(); pruning.prune_one(&mut commit).unwrap(); assert_eq!(pruning.pending_number, 0); - let (death_rows, death_index) = pruning.death_rows_queue.get_mem_queue_state().unwrap(); + let (death_rows, death_index) = pruning.queue.get_mem_queue_state().unwrap(); assert!(death_rows.is_empty()); assert!(death_index.is_empty()); assert!(pruning.pending_prunings == 0); @@ -508,7 +511,7 @@ mod tests { pruning.apply_pending(); assert!(pruning.have_block(&h)); assert!(commit.data.deleted.is_empty()); - let (death_rows, death_index) = pruning.death_rows_queue.get_mem_queue_state().unwrap(); + let (death_rows, death_index) = pruning.queue.get_mem_queue_state().unwrap(); assert_eq!(death_rows.len(), 1); assert_eq!(death_index.len(), 2); assert!(db.data_eq(&make_db(&[1, 2, 3, 4, 5]))); @@ -521,7 +524,7 @@ mod tests { pruning.apply_pending(); assert!(!pruning.have_block(&h)); assert!(db.data_eq(&make_db(&[2, 4, 5]))); - let (death_rows, death_index) = pruning.death_rows_queue.get_mem_queue_state().unwrap(); + let (death_rows, death_index) = pruning.queue.get_mem_queue_state().unwrap(); assert!(death_rows.is_empty()); assert!(death_index.is_empty()); assert_eq!(pruning.pending_number, 1); @@ -664,4 +667,122 @@ mod tests { db.commit(&commit); assert!(db.data_eq(&make_db(&[1, 3]))); } + + #[test] + fn db_backed_queue() { + let mut pruning: RefWindow = + RefWindow::new(&make_db(&[]), false).unwrap(); + + // import blocks + // queue size and content should match + for i in 0..(CACHE_BATCH_SIZE + 10) { + let mut commit = make_commit(&[], &[]); + pruning.note_canonical(&(i as u64), &mut commit); + pruning.queue.get_db().unwrap().commit(&commit); + } + assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 10); + let (cache, hashs) = pruning.queue.get_db_backed_queue_state().unwrap(); + assert_eq!(cache.len(), CACHE_BATCH_SIZE); + assert_eq!(hashs.len(), 10); + for i in 0..10 { + assert_eq!(cache[i].hash, i as u64); + assert_eq!(hashs[i], (i + CACHE_BATCH_SIZE) as u64); + } + + // import a new block to the end of the queue + // only keep the new block's hash in memory + let mut commit = CommitSet::default(); + pruning.note_canonical(&(CACHE_BATCH_SIZE as u64 + 10), &mut commit); + pruning.queue.get_db().unwrap().commit(&commit); + assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 11); + let (cache, hashs) = pruning.queue.get_db_backed_queue_state().unwrap(); + assert_eq!(cache.len(), CACHE_BATCH_SIZE); + assert_eq!(hashs.len(), 11); + assert_eq!(hashs[10], (CACHE_BATCH_SIZE + 10) as u64); + + // remove one block from the start of the queue + // block is removed from the head of cache + let mut commit = CommitSet::default(); + pruning.prune_one(&mut commit).unwrap(); + pruning.queue.get_db().unwrap().commit(&commit); + pruning.apply_pending(); + assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 10); + let (cache, hashs) = pruning.queue.get_db_backed_queue_state().unwrap(); + assert_eq!(cache.len(), CACHE_BATCH_SIZE - 1); + assert_eq!(hashs.len(), 11); + for i in 0..(CACHE_BATCH_SIZE - 1) { + assert_eq!(cache[i].hash, (i + 1) as u64); + } + + // load a new queue from db + // `cache` is full but the content of the queue should be the same + let pruning: RefWindow = + RefWindow::new(pruning.queue.get_db().unwrap(), false).unwrap(); + assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 10); + let (cache, hashs) = pruning.queue.get_db_backed_queue_state().unwrap(); + assert_eq!(cache.len(), CACHE_BATCH_SIZE); + assert_eq!(hashs.len(), 10); + for i in 0..10 { + assert_eq!(cache[i].hash, (i + 1) as u64); + assert_eq!(hashs[i], (i + CACHE_BATCH_SIZE + 1) as u64); + } + } + + #[test] + fn load_block_from_db() { + let mut pruning: RefWindow = + RefWindow::new(&make_db(&[]), false).unwrap(); + + // import blocks + for i in 0..(CACHE_BATCH_SIZE as u64 * 2 + 10) { + let mut commit = make_commit(&[], &[]); + pruning.note_canonical(&i, &mut commit); + pruning.queue.get_db().unwrap().commit(&commit); + } + + // the following operations won't triger loading block from db: + // - getting block in cache + // - getting block not in the queue + // - just getting block hash + let index = CACHE_BATCH_SIZE; + assert_eq!( + pruning.queue.get(0, index - 1).unwrap().unwrap().hash, + CACHE_BATCH_SIZE as u64 - 1 + ); + assert_eq!(pruning.queue.get(0, CACHE_BATCH_SIZE * 2 + 10).unwrap(), None); + assert_eq!(pruning.queue.get_hash(index).unwrap(), CACHE_BATCH_SIZE as u64); + let (cache, hashs) = pruning.queue.get_db_backed_queue_state().unwrap(); + assert_eq!(cache.len(), CACHE_BATCH_SIZE); + assert_eq!(hashs.len(), CACHE_BATCH_SIZE + 10); + + // getting a block not in cache will triger loading block from db + assert_eq!(pruning.queue.get(0, index).unwrap().unwrap().hash, CACHE_BATCH_SIZE as u64); + let (cache, hashs) = pruning.queue.get_db_backed_queue_state().unwrap(); + assert_eq!(cache.len(), CACHE_BATCH_SIZE * 2); + assert_eq!(hashs.len(), 10); + for i in 0..10 { + assert_eq!(cache[i].hash, i as u64); + assert_eq!(hashs[i], (i + CACHE_BATCH_SIZE * 2) as u64); + } + + // clear the cache + for _ in 0..CACHE_BATCH_SIZE * 2 { + let mut commit = CommitSet::default(); + pruning.prune_one(&mut commit).unwrap(); + pruning.queue.get_db().unwrap().commit(&commit); + } + pruning.apply_pending(); + let (cache, hashs) = pruning.queue.get_db_backed_queue_state().unwrap(); + assert!(cache.is_empty()); + assert_eq!(hashs.len(), 10); + + // getting the next block will load the remaining blocks from db + assert_eq!( + pruning.queue.get(pruning.pending_number, 0).unwrap().unwrap().hash, + (CACHE_BATCH_SIZE * 2) as u64 + ); + let (cache, hashs) = pruning.queue.get_db_backed_queue_state().unwrap(); + assert_eq!(cache.len(), 10); + assert!(hashs.is_empty()); + } } From e8d57d030945d811d9d890d50ac912dd7d537b32 Mon Sep 17 00:00:00 2001 From: linning Date: Fri, 5 Aug 2022 07:10:33 +0800 Subject: [PATCH 04/19] make clippy happy Signed-off-by: linning --- client/state-db/src/pruning.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/state-db/src/pruning.rs b/client/state-db/src/pruning.rs index a63c71ecdf8bd..a4e0c8813bc96 100644 --- a/client/state-db/src/pruning.rs +++ b/client/state-db/src/pruning.rs @@ -252,7 +252,7 @@ impl DeathRowQueue { // because `cache` is a queue of successive `DeathRow` // NOTE: this branch should not be entered because blocks are visited // in successive increasing order, just keeping it for robustness - return Ok(load_death_row_from_db(db, base + index as u64)?) + return load_death_row_from_db(db, base + index as u64) } } Ok(cache.get(index).cloned()) From f048c31415a59479d558b67bf8d5b2300f91d4e0 Mon Sep 17 00:00:00 2001 From: linning Date: Sat, 6 Aug 2022 00:14:28 +0800 Subject: [PATCH 05/19] impl have_block by checking block number Signed-off-by: linning --- client/db/src/lib.rs | 3 ++- client/state-db/src/lib.rs | 12 +++++------- client/state-db/src/pruning.rs | 6 ++++-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/client/db/src/lib.rs b/client/db/src/lib.rs index 84551d809ea2c..03d2facbac8f2 100644 --- a/client/db/src/lib.rs +++ b/client/db/src/lib.rs @@ -2256,7 +2256,8 @@ impl sc_client_api::backend::Backend for Backend { block ))) } - if let Ok(()) = self.storage.state_db.pin(&hash) { + if let Ok(()) = self.storage.state_db.pin(&hash, hdr.number.saturated_into::()) + { let root = hdr.state_root; let db_state = DbState::::new(self.storage.clone(), root); let state = RefTrackingState::new(db_state, self.storage.clone(), Some(hash)); diff --git a/client/state-db/src/lib.rs b/client/state-db/src/lib.rs index aad0008ab0051..b51cf504d85e6 100644 --- a/client/state-db/src/lib.rs +++ b/client/state-db/src/lib.rs @@ -353,9 +353,7 @@ impl if self.best_canonical().map(|c| number > c).unwrap_or(true) { !self.non_canonical.have_block(hash) } else { - self.pruning.as_ref().map_or(false, |pruning| { - number < pruning.pending() || !pruning.have_block(hash) - }) + self.pruning.as_ref().map_or(false, |pruning| !pruning.have_block(number)) } }, } @@ -403,12 +401,12 @@ impl } } - fn pin(&mut self, hash: &BlockHash) -> Result<(), PinError> { + fn pin(&mut self, hash: &BlockHash, number: u64) -> Result<(), PinError> { match self.mode { PruningMode::ArchiveAll => Ok(()), PruningMode::ArchiveCanonical | PruningMode::Constrained(_) => { if self.non_canonical.have_block(hash) || - self.pruning.as_ref().map_or(false, |pruning| pruning.have_block(hash)) + self.pruning.as_ref().map_or(false, |pruning| pruning.have_block(number)) { let refs = self.pinned.entry(hash.clone()).or_default(); if *refs == 0 { @@ -563,8 +561,8 @@ impl } /// Prevents pruning of specified block and its descendants. - pub fn pin(&self, hash: &BlockHash) -> Result<(), PinError> { - self.db.write().pin(hash) + pub fn pin(&self, hash: &BlockHash, number: u64) -> Result<(), PinError> { + self.db.write().pin(hash, number) } /// Allows pruning of specified block. diff --git a/client/state-db/src/pruning.rs b/client/state-db/src/pruning.rs index a4e0c8813bc96..29aa6f6f5741c 100644 --- a/client/state-db/src/pruning.rs +++ b/client/state-db/src/pruning.rs @@ -390,12 +390,14 @@ impl RefWindow { 0 } + // Return the block number of the first block that not been pending pruned pub fn pending(&self) -> u64 { self.pending_number + self.pending_prunings as u64 } - pub fn have_block(&self, hash: &BlockHash) -> bool { - self.queue.has_block(hash, self.pending_prunings) + // Return true if a canonicalized block is in the window and not be pruned yet + pub fn have_block(&self, number: u64) -> bool { + number >= self.pending() && number < self.pending_number + self.queue.len() as u64 } /// Prune next block. Expects at least one block in the window. Adds changes to `commit`. From 206582ef0af1584370cd15520c2388ce46a44c18 Mon Sep 17 00:00:00 2001 From: linning Date: Sat, 6 Aug 2022 03:54:29 +0800 Subject: [PATCH 06/19] refactor Signed-off-by: linning --- client/state-db/src/lib.rs | 8 +- client/state-db/src/noncanonical.rs | 2 +- client/state-db/src/pruning.rs | 297 ++++++++++++++++------------ 3 files changed, 175 insertions(+), 132 deletions(-) diff --git a/client/state-db/src/lib.rs b/client/state-db/src/lib.rs index b51cf504d85e6..678d7b09b2ab1 100644 --- a/client/state-db/src/lib.rs +++ b/client/state-db/src/lib.rs @@ -324,6 +324,10 @@ impl } fn canonicalize_block(&mut self, hash: &BlockHash) -> Result, Error> { + // NOTE: it is importent that the change to `LAST_CANONICAL` (emit from + // `non_canonical.canonicalize`) and the insert of the new pruning journal (emit from + // `pruning.note_canonical`) are collected into the same `CommitSet` and are committed to + // the database atomically to keep their consistency when restarting the node let mut commit = CommitSet::default(); if self.mode == PruningMode::ArchiveAll { return Ok(commit) @@ -373,7 +377,7 @@ impl } let pinned = &self.pinned; - if pruning.next_hash().map_or(false, |h| pinned.contains_key(&h)) { + if pruning.next_hash()?.map_or(false, |h| pinned.contains_key(&h)) { break } pruning.prune_one(commit)?; @@ -462,7 +466,7 @@ impl trace!( target: "forks", "First available: {:?} ({}), Last canon: {:?} ({}), Best forks: {:?}", - self.pruning.as_ref().and_then(|p| p.next_hash()), + self.pruning.as_mut().map(|p| p.next_hash()), self.pruning.as_ref().map(|p| p.pending()).unwrap_or(0), self.non_canonical.last_canonicalized_hash(), self.non_canonical.last_canonicalized_block_number().unwrap_or(0), diff --git a/client/state-db/src/noncanonical.rs b/client/state-db/src/noncanonical.rs index 13cf5825b1b24..664811f7fe4ab 100644 --- a/client/state-db/src/noncanonical.rs +++ b/client/state-db/src/noncanonical.rs @@ -28,7 +28,7 @@ use log::trace; use std::collections::{hash_map::Entry, HashMap, VecDeque}; const NON_CANONICAL_JOURNAL: &[u8] = b"noncanonical_journal"; -const LAST_CANONICAL: &[u8] = b"last_canonical"; +pub const LAST_CANONICAL: &[u8] = b"last_canonical"; const MAX_BLOCKS_PER_LEVEL: u64 = 32; /// See module documentation. diff --git a/client/state-db/src/pruning.rs b/client/state-db/src/pruning.rs index 29aa6f6f5741c..77650c3774a47 100644 --- a/client/state-db/src/pruning.rs +++ b/client/state-db/src/pruning.rs @@ -24,7 +24,7 @@ //! the death list. //! The changes are journaled in the DB. -use crate::{to_meta_key, CommitSet, Error, Hash, MetaDb}; +use crate::{noncanonical::LAST_CANONICAL, to_meta_key, CommitSet, Error, Hash, MetaDb}; use codec::{Decode, Encode}; use log::{trace, warn}; use std::{ @@ -74,41 +74,62 @@ enum DeathRowQueue { /// Only caching the first fews blocks of the pruning window, blocks inside are /// successive and ordered by block number cache: VecDeque>, - /// A queue of hashs of all the blocks that not loaded into cache, the first hash of - /// the queue followe the last block of `cache`, namely `block_numer(hashs[0])` == - /// `block_numer(cache.last()) + 1`, hashs inside are successive and ordered by block - /// number - hashs: VecDeque, + /// The number of blocks that not loaded into `cache` + unload_blocks: usize, }, } impl DeathRowQueue { /// Return a `DeathRowQueue` that all blocks are keep in memory - fn new_mem() -> DeathRowQueue { - DeathRowQueue::Mem { death_rows: VecDeque::new(), death_index: HashMap::new() } + fn new_mem(db: &D, base: u64) -> Result, Error> { + let mut block = base; + let mut queue = DeathRowQueue::::Mem { + death_rows: VecDeque::new(), + death_index: HashMap::new(), + }; + // read the journal + trace!(target: "state-db", "Reading pruning journal for the memory queue. Pending #{}", base); + loop { + let journal_key = to_journal_key(block); + match db.get_meta(&journal_key).map_err(Error::Db)? { + Some(record) => { + let record: JournalRecord = + Decode::decode(&mut record.as_slice())?; + trace!(target: "state-db", "Pruning journal entry {} ({} inserted, {} deleted)", block, record.inserted.len(), record.deleted.len()); + queue.import(base, record); + }, + None => break, + } + block += 1; + } + Ok(queue) } /// Return a `DeathRowQueue` that backed by an database, and only keep a few number /// of blocks in memory - fn new_db_backed(db: D) -> DeathRowQueue { - DeathRowQueue::DbBacked { - db, - cache: VecDeque::with_capacity(CACHE_BATCH_SIZE), - hashs: VecDeque::new(), - } + fn new_db_backed( + db: D, + base: u64, + mut unload_blocks: usize, + ) -> Result, Error> { + let mut cache = VecDeque::with_capacity(CACHE_BATCH_SIZE); + trace!(target: "state-db", "Reading pruning journal for the database-backed queue. Pending #{}", base); + // Load block from db + DeathRowQueue::load_batch_from_db(&db, &mut unload_blocks, &mut cache, base)?; + Ok(DeathRowQueue::DbBacked { db, cache, unload_blocks }) } /// import a new block to the back of the queue fn import(&mut self, base: u64, journal_record: JournalRecord) { let JournalRecord { hash, inserted, deleted } = journal_record; match self { - DeathRowQueue::DbBacked { hashs, cache, .. } => { - // `hashs` is empty means currently all block are loaded into `cache` + DeathRowQueue::DbBacked { unload_blocks, cache, .. } => { + // `unload_blocks` is zero means currently all block are loaded into `cache` // thus if `cache` is not full, load the next block into `cache` too - if hashs.is_empty() && cache.len() < CACHE_BATCH_SIZE { + if *unload_blocks == 0 && cache.len() < CACHE_BATCH_SIZE { cache.push_back(DeathRow { hash, deleted: deleted.into_iter().collect() }); } else { - hashs.push_back(hash); + *unload_blocks += 1; } }, DeathRowQueue::Mem { death_rows, death_index } => { @@ -135,10 +156,10 @@ impl DeathRowQueue { base: u64, ) -> Result>, Error> { match self { - DeathRowQueue::DbBacked { db, hashs, cache } => { - if cache.is_empty() && !hashs.is_empty() { + DeathRowQueue::DbBacked { db, unload_blocks, cache } => { + if cache.is_empty() && *unload_blocks != 0 { // load more blocks from db since there are still blocks in it - DeathRowQueue::load_batch_from_db(db, hashs, cache, base)?; + DeathRowQueue::load_batch_from_db(db, unload_blocks, cache, base)?; } Ok(cache.pop_front()) }, @@ -154,34 +175,20 @@ impl DeathRowQueue { } } - fn has_block(&self, hash: &BlockHash, skip: usize) -> bool { - // TODO: if the pruning window is set to large, this may hurt performance - match self { - DeathRowQueue::DbBacked { hashs, cache, .. } => cache - .iter() - .map(|row| &row.hash) - .chain(hashs.iter()) - .skip(skip) - .any(|h| h == hash), - DeathRowQueue::Mem { death_rows, .. } => - death_rows.iter().map(|row| &row.hash).skip(skip).any(|h| h == hash), - } - } - /// Revert recent additions to the queue, namely remove `amount` number of blocks from the back /// of the queue, `base` is the block number of the first block of the queue fn revert_recent_add(&mut self, base: u64, amout: usize) { debug_assert!(amout <= self.len()); match self { - DeathRowQueue::DbBacked { hashs, cache, .. } => { - // remove from `hashs` if it can cover - if hashs.len() >= amout { - hashs.truncate(hashs.len() - amout); + DeathRowQueue::DbBacked { unload_blocks, cache, .. } => { + // remove from `unload_blocks` if it can cover + if *unload_blocks >= amout { + *unload_blocks -= amout; return } // clear `hashs` and remove remain blocks from `cache` - let remain = amout - hashs.len(); - hashs.clear(); + let remain = amout - *unload_blocks; + *unload_blocks = 0; cache.truncate(cache.len() - remain); }, DeathRowQueue::Mem { death_rows, death_index } => { @@ -202,25 +209,21 @@ impl DeathRowQueue { /// of the queue fn load_batch_from_db( db: &D, - hashs: &mut VecDeque, + unload_blocks: &mut usize, cache: &mut VecDeque>, base: u64, ) -> Result<(), Error> { // return if all blocks already loaded into `cache` and there are no other // blocks in the backend database - if hashs.len() == 0 { + if *unload_blocks == 0 { return Ok(()) } let start = base + cache.len() as u64; - let batch_size = cmp::min(hashs.len(), CACHE_BATCH_SIZE); + let batch_size = cmp::min(*unload_blocks, CACHE_BATCH_SIZE); let mut loaded = 0; for i in 0..batch_size as u64 { match load_death_row_from_db::(db, start + i)? { Some(row) => { - // the incoming block's hash should be the same as the first hash of - // `hashs`, if there are corrupted data `load_death_row_from_db` should - // return a db error - debug_assert_eq!(Some(row.hash.clone()), hashs.pop_front()); cache.push_back(row); loaded += 1; }, @@ -230,6 +233,7 @@ impl DeathRowQueue { // `loaded` should be the same as what we expect, if there are missing blocks // `load_death_row_from_db` should return a db error debug_assert_eq!(batch_size, loaded); + *unload_blocks -= loaded; Ok(()) } @@ -241,12 +245,12 @@ impl DeathRowQueue { index: usize, ) -> Result>, Error> { match self { - DeathRowQueue::DbBacked { db, hashs, cache } => { + DeathRowQueue::DbBacked { db, unload_blocks, cache } => { // check if `index` target a block reside on disk - if index >= cache.len() && index < cache.len() + hashs.len() { + if index >= cache.len() && index < cache.len() + *unload_blocks { // if `index` target the next batch of `DeathRow`, load a batch from db - if index - cache.len() < cmp::min(hashs.len(), CACHE_BATCH_SIZE) { - DeathRowQueue::load_batch_from_db(db, hashs, cache, base)?; + if index - cache.len() < cmp::min(*unload_blocks, CACHE_BATCH_SIZE) { + DeathRowQueue::load_batch_from_db(db, unload_blocks, cache, base)?; } else { // load a single `DeathRow` from db, but do not insert it to `cache` // because `cache` is a queue of successive `DeathRow` @@ -262,22 +266,23 @@ impl DeathRowQueue { } /// Get the hash of the block at the given `index` of the queue - fn get_hash(&self, index: usize) -> Option { + fn get_hash(&mut self, base: u64, index: usize) -> Result, Error> { match self { - DeathRowQueue::DbBacked { cache, hashs, .. } => + DeathRowQueue::DbBacked { cache, .. } => if index < cache.len() { - cache.get(index).map(|r| r.hash.clone()) + Ok(cache.get(index).map(|r| r.hash.clone())) } else { - hashs.get(index - cache.len()).cloned() + self.get(base, index).map(|r| r.map(|r| r.hash)) }, - DeathRowQueue::Mem { death_rows, .. } => death_rows.get(index).map(|r| r.hash.clone()), + DeathRowQueue::Mem { death_rows, .. } => + Ok(death_rows.get(index).map(|r| r.hash.clone())), } } /// Return the number of block in the pruning window fn len(&self) -> usize { match self { - DeathRowQueue::DbBacked { hashs, cache, .. } => cache.len() + hashs.len(), + DeathRowQueue::DbBacked { unload_blocks, cache, .. } => cache.len() + *unload_blocks, DeathRowQueue::Mem { death_rows, .. } => death_rows.len(), } } @@ -293,11 +298,9 @@ impl DeathRowQueue { } #[cfg(test)] - fn get_db_backed_queue_state( - &self, - ) -> Option<(&VecDeque>, &VecDeque)> { + fn get_db_backed_queue_state(&self) -> Option<(&VecDeque>, usize)> { match self { - DeathRowQueue::DbBacked { cache, hashs, .. } => Some((cache, hashs)), + DeathRowQueue::DbBacked { cache, unload_blocks, .. } => Some((cache, *unload_blocks)), DeathRowQueue::Mem { .. } => None, } } @@ -347,43 +350,43 @@ impl RefWindow { db: &D, count_insertions: bool, ) -> Result, Error> { - let last_pruned = db.get_meta(&to_meta_key(LAST_PRUNED, &())).map_err(Error::Db)?; - let pending_number: u64 = match last_pruned { + // the block number of the first block in the queue + let pending_number = match db.get_meta(&to_meta_key(LAST_PRUNED, &())).map_err(Error::Db)? { Some(buffer) => u64::decode(&mut buffer.as_slice())? + 1, None => 0, }; - let mut block = pending_number; + // the block number of the last block in the queue + let last_canonicalized_number = + match db.get_meta(&to_meta_key(LAST_CANONICAL, &())).map_err(Error::Db)? { + Some(buffer) => Some(<(BlockHash, u64)>::decode(&mut buffer.as_slice())?.1), + None => None, + }; + let queue = if count_insertions { - DeathRowQueue::new_mem() + DeathRowQueue::new_mem(db, pending_number)? } else { - DeathRowQueue::new_db_backed(db.clone()) - }; - let mut pruning = - RefWindow { queue, pending_number, pending_canonicalizations: 0, pending_prunings: 0 }; - // read the journal - trace!(target: "state-db", "Reading pruning journal. Pending #{}", pending_number); - loop { - let journal_key = to_journal_key(block); - match db.get_meta(&journal_key).map_err(Error::Db)? { - Some(record) => { - let record: JournalRecord = - Decode::decode(&mut record.as_slice())?; - trace!(target: "state-db", "Pruning journal entry {} ({} inserted, {} deleted)", block, record.inserted.len(), record.deleted.len()); - pruning.queue.import(pending_number, record); + let unload = match last_canonicalized_number { + Some(last_canonicalized_number) => { + debug_assert!(last_canonicalized_number >= pending_number); + last_canonicalized_number - pending_number + 1 }, - None => break, - } - block += 1; - } - Ok(pruning) + // None means `LAST_CANONICAL` is never been wrote, since the pruning journals are + // in the same `CommitSet` as `LAST_CANONICAL`, it means no pruning journal have + // ever been committed to the db, thus set `unload` to zero + None => 0, + }; + DeathRowQueue::new_db_backed(db.clone(), pending_number, unload as usize)? + }; + + Ok(RefWindow { queue, pending_number, pending_canonicalizations: 0, pending_prunings: 0 }) } pub fn window_size(&self) -> u64 { (self.queue.len() - self.pending_prunings) as u64 } - pub fn next_hash(&self) -> Option { - self.queue.get_hash(self.pending_prunings) + pub fn next_hash(&mut self) -> Result, Error> { + self.queue.get_hash(self.pending_number, self.pending_prunings) } pub fn mem_used(&self) -> usize { @@ -395,7 +398,7 @@ impl RefWindow { self.pending_number + self.pending_prunings as u64 } - // Return true if a canonicalized block is in the window and not be pruned yet + // Return true if a canonicalized block is in the window and not be pruned yet pub fn have_block(&self, number: u64) -> bool { number >= self.pending() && number < self.pending_number + self.queue.len() as u64 } @@ -465,9 +468,11 @@ impl RefWindow { mod tests { use super::{DeathRowQueue, RefWindow, CACHE_BATCH_SIZE}; use crate::{ + noncanonical::LAST_CANONICAL, test::{make_commit, make_db, TestDb}, - CommitSet, + to_meta_key, CommitSet, Hash, }; + use codec::Encode; use sp_core::H256; fn check_journal(pruning: &RefWindow, db: &TestDb) { @@ -506,12 +511,11 @@ mod tests { let mut db = make_db(&[1, 2, 3]); let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); let mut commit = make_commit(&[4, 5], &[1, 3]); - let h = H256::random(); - pruning.note_canonical(&h, &mut commit); + pruning.note_canonical(&H256::random(), &mut commit); db.commit(&commit); - assert!(pruning.have_block(&h)); + assert!(pruning.have_block(0)); pruning.apply_pending(); - assert!(pruning.have_block(&h)); + assert!(pruning.have_block(0)); assert!(commit.data.deleted.is_empty()); let (death_rows, death_index) = pruning.queue.get_mem_queue_state().unwrap(); assert_eq!(death_rows.len(), 1); @@ -521,10 +525,10 @@ mod tests { let mut commit = CommitSet::default(); pruning.prune_one(&mut commit).unwrap(); - assert!(!pruning.have_block(&h)); + assert!(!pruning.have_block(0)); db.commit(&commit); pruning.apply_pending(); - assert!(!pruning.have_block(&h)); + assert!(!pruning.have_block(0)); assert!(db.data_eq(&make_db(&[2, 4, 5]))); let (death_rows, death_index) = pruning.queue.get_mem_queue_state().unwrap(); assert!(death_rows.is_empty()); @@ -670,37 +674,65 @@ mod tests { assert!(db.data_eq(&make_db(&[1, 3]))); } + fn push_last_canonicalized(block: u64, commit: &mut CommitSet) { + commit + .meta + .inserted + .push((to_meta_key(LAST_CANONICAL, &()), (block, block).encode())); + } + #[test] fn db_backed_queue() { let mut pruning: RefWindow = RefWindow::new(&make_db(&[]), false).unwrap(); + // start as an empty queue + let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); + assert_eq!(cache.len(), 0); + assert_eq!(unload_blocks, 0); + // import blocks // queue size and content should match for i in 0..(CACHE_BATCH_SIZE + 10) { let mut commit = make_commit(&[], &[]); pruning.note_canonical(&(i as u64), &mut commit); + push_last_canonicalized(i as u64, &mut commit); pruning.queue.get_db().unwrap().commit(&commit); + // block will fill in cache first + let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); + if i < CACHE_BATCH_SIZE { + assert_eq!(cache.len(), i + 1); + assert_eq!(unload_blocks, 0); + } else { + assert_eq!(cache.len(), CACHE_BATCH_SIZE); + assert_eq!(unload_blocks, i - CACHE_BATCH_SIZE + 1); + } } + pruning.apply_pending(); assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 10); - let (cache, hashs) = pruning.queue.get_db_backed_queue_state().unwrap(); + let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), CACHE_BATCH_SIZE); - assert_eq!(hashs.len(), 10); - for i in 0..10 { + assert_eq!(unload_blocks, 10); + for i in 0..CACHE_BATCH_SIZE { assert_eq!(cache[i].hash, i as u64); - assert_eq!(hashs[i], (i + CACHE_BATCH_SIZE) as u64); } // import a new block to the end of the queue - // only keep the new block's hash in memory + // won't keep the new block in memory let mut commit = CommitSet::default(); pruning.note_canonical(&(CACHE_BATCH_SIZE as u64 + 10), &mut commit); - pruning.queue.get_db().unwrap().commit(&commit); assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 11); - let (cache, hashs) = pruning.queue.get_db_backed_queue_state().unwrap(); + let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), CACHE_BATCH_SIZE); - assert_eq!(hashs.len(), 11); - assert_eq!(hashs[10], (CACHE_BATCH_SIZE + 10) as u64); + assert_eq!(unload_blocks, 11); + + // revert the last add that no apply yet + // NOTE: do not commit the previous `CommitSet` to db + pruning.queue.revert_recent_add(0, 1); + assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 10); + let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); + assert_eq!(cache.len(), CACHE_BATCH_SIZE); + assert_eq!(unload_blocks, 10); // remove one block from the start of the queue // block is removed from the head of cache @@ -708,25 +740,24 @@ mod tests { pruning.prune_one(&mut commit).unwrap(); pruning.queue.get_db().unwrap().commit(&commit); pruning.apply_pending(); - assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 10); - let (cache, hashs) = pruning.queue.get_db_backed_queue_state().unwrap(); + assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 9); + let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), CACHE_BATCH_SIZE - 1); - assert_eq!(hashs.len(), 11); + assert_eq!(unload_blocks, 10); for i in 0..(CACHE_BATCH_SIZE - 1) { assert_eq!(cache[i].hash, (i + 1) as u64); } // load a new queue from db - // `cache` is full but the content of the queue should be the same + // `cache` is full again but the content of the queue should be the same let pruning: RefWindow = RefWindow::new(pruning.queue.get_db().unwrap(), false).unwrap(); - assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 10); - let (cache, hashs) = pruning.queue.get_db_backed_queue_state().unwrap(); + assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 9); + let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), CACHE_BATCH_SIZE); - assert_eq!(hashs.len(), 10); - for i in 0..10 { + assert_eq!(unload_blocks, 9); + for i in 0..CACHE_BATCH_SIZE { assert_eq!(cache[i].hash, (i + 1) as u64); - assert_eq!(hashs[i], (i + CACHE_BATCH_SIZE + 1) as u64); } } @@ -739,52 +770,60 @@ mod tests { for i in 0..(CACHE_BATCH_SIZE as u64 * 2 + 10) { let mut commit = make_commit(&[], &[]); pruning.note_canonical(&i, &mut commit); + push_last_canonicalized(i as u64, &mut commit); pruning.queue.get_db().unwrap().commit(&commit); } // the following operations won't triger loading block from db: // - getting block in cache // - getting block not in the queue - // - just getting block hash let index = CACHE_BATCH_SIZE; assert_eq!( pruning.queue.get(0, index - 1).unwrap().unwrap().hash, CACHE_BATCH_SIZE as u64 - 1 ); assert_eq!(pruning.queue.get(0, CACHE_BATCH_SIZE * 2 + 10).unwrap(), None); - assert_eq!(pruning.queue.get_hash(index).unwrap(), CACHE_BATCH_SIZE as u64); - let (cache, hashs) = pruning.queue.get_db_backed_queue_state().unwrap(); + let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), CACHE_BATCH_SIZE); - assert_eq!(hashs.len(), CACHE_BATCH_SIZE + 10); + assert_eq!(unload_blocks, CACHE_BATCH_SIZE + 10); // getting a block not in cache will triger loading block from db assert_eq!(pruning.queue.get(0, index).unwrap().unwrap().hash, CACHE_BATCH_SIZE as u64); - let (cache, hashs) = pruning.queue.get_db_backed_queue_state().unwrap(); + let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), CACHE_BATCH_SIZE * 2); - assert_eq!(hashs.len(), 10); - for i in 0..10 { - assert_eq!(cache[i].hash, i as u64); - assert_eq!(hashs[i], (i + CACHE_BATCH_SIZE * 2) as u64); - } + assert_eq!(unload_blocks, 10); - // clear the cache + // clear all block loaded in cache for _ in 0..CACHE_BATCH_SIZE * 2 { let mut commit = CommitSet::default(); pruning.prune_one(&mut commit).unwrap(); pruning.queue.get_db().unwrap().commit(&commit); } pruning.apply_pending(); - let (cache, hashs) = pruning.queue.get_db_backed_queue_state().unwrap(); + let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert!(cache.is_empty()); - assert_eq!(hashs.len(), 10); + assert_eq!(unload_blocks, 10); - // getting the next block will load the remaining blocks from db + // getting the hash of block that not in cache will also triger loading + // the remaining blocks from db assert_eq!( pruning.queue.get(pruning.pending_number, 0).unwrap().unwrap().hash, (CACHE_BATCH_SIZE * 2) as u64 ); - let (cache, hashs) = pruning.queue.get_db_backed_queue_state().unwrap(); + let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); + assert_eq!(cache.len(), 10); + assert_eq!(unload_blocks, 0); + + // load a new queue from db + // `cache` should be the same + let pruning: RefWindow = + RefWindow::new(pruning.queue.get_db().unwrap(), false).unwrap(); + assert_eq!(pruning.queue.len(), 10); + let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), 10); - assert!(hashs.is_empty()); + assert_eq!(unload_blocks, 0); + for i in 0..10 { + assert_eq!(cache[i].hash, (CACHE_BATCH_SIZE * 2 + i) as u64); + } } } From bc3e31ceb5ea3eba002d176e4e99979972bc21a4 Mon Sep 17 00:00:00 2001 From: linning Date: Mon, 8 Aug 2022 19:25:21 +0800 Subject: [PATCH 07/19] fix tests & add test for init db-backed queue Signed-off-by: linning --- client/state-db/src/lib.rs | 16 ++++++-- client/state-db/src/pruning.rs | 75 +++++++++++++++++++++++++++++++--- 2 files changed, 82 insertions(+), 9 deletions(-) diff --git a/client/state-db/src/lib.rs b/client/state-db/src/lib.rs index 678d7b09b2ab1..3f43ea13c2f6c 100644 --- a/client/state-db/src/lib.rs +++ b/client/state-db/src/lib.rs @@ -661,14 +661,26 @@ fn choose_pruning_mode( #[cfg(test)] mod tests { use crate::{ + noncanonical::LAST_CANONICAL, + pruning::LAST_PRUNED, test::{make_changeset, make_db, TestDb}, - Constraints, Error, PruningMode, StateDb, StateDbError, + to_meta_key, CommitSet, Constraints, Error, PruningMode, StateDb, StateDbError, }; + use codec::Encode; use sp_core::H256; use std::io; fn make_test_db(settings: PruningMode) -> (TestDb, StateDb) { let mut db = make_db(&[91, 921, 922, 93, 94]); + + let mut commit = CommitSet::default(); + commit + .meta + .inserted + .push((to_meta_key(LAST_CANONICAL, &()), (H256::from_low_u64_be(0), 0u64).encode())); + commit.meta.inserted.push((to_meta_key(LAST_PRUNED, &()), 0u64.encode())); + db.commit(&commit); + let (state_db_init, state_db) = StateDb::open(&mut db, Some(settings), false, true).unwrap(); db.commit(&state_db_init); @@ -766,7 +778,6 @@ mod tests { assert!(sdb.is_pruned(&H256::from_low_u64_be(0), 0)); assert!(sdb.is_pruned(&H256::from_low_u64_be(1), 1)); assert!(sdb.is_pruned(&H256::from_low_u64_be(21), 2)); - assert!(sdb.is_pruned(&H256::from_low_u64_be(22), 2)); assert!(db.data_eq(&make_db(&[21, 3, 922, 93, 94]))); } @@ -779,7 +790,6 @@ mod tests { assert!(sdb.is_pruned(&H256::from_low_u64_be(0), 0)); assert!(sdb.is_pruned(&H256::from_low_u64_be(1), 1)); assert!(!sdb.is_pruned(&H256::from_low_u64_be(21), 2)); - assert!(sdb.is_pruned(&H256::from_low_u64_be(22), 2)); assert!(db.data_eq(&make_db(&[1, 21, 3, 921, 922, 93, 94]))); } diff --git a/client/state-db/src/pruning.rs b/client/state-db/src/pruning.rs index 77650c3774a47..bc5d0fd4fe4f7 100644 --- a/client/state-db/src/pruning.rs +++ b/client/state-db/src/pruning.rs @@ -32,7 +32,7 @@ use std::{ collections::{HashMap, HashSet, VecDeque}, }; -const LAST_PRUNED: &[u8] = b"last_pruned"; +pub const LAST_PRUNED: &[u8] = b"last_pruned"; const PRUNING_JOURNAL: &[u8] = b"pruning_journal"; // Default pruning window size plus a magic number keep most common ops in cache const CACHE_BATCH_SIZE: usize = 256 + 10; @@ -334,7 +334,7 @@ struct DeathRow { deleted: HashSet, } -#[derive(Encode, Decode)] +#[derive(Encode, Decode, Default)] struct JournalRecord { hash: BlockHash, inserted: Vec, @@ -350,7 +350,8 @@ impl RefWindow { db: &D, count_insertions: bool, ) -> Result, Error> { - // the block number of the first block in the queue + // the block number of the first block in the queue or the next block number if the queue is + // empty let pending_number = match db.get_meta(&to_meta_key(LAST_PRUNED, &())).map_err(Error::Db)? { Some(buffer) => u64::decode(&mut buffer.as_slice())? + 1, None => 0, @@ -367,8 +368,8 @@ impl RefWindow { } else { let unload = match last_canonicalized_number { Some(last_canonicalized_number) => { - debug_assert!(last_canonicalized_number >= pending_number); - last_canonicalized_number - pending_number + 1 + debug_assert!(last_canonicalized_number + 1 >= pending_number); + last_canonicalized_number + 1 - pending_number }, // None means `LAST_CANONICAL` is never been wrote, since the pruning journals are // in the same `CommitSet` as `LAST_CANONICAL`, it means no pruning journal have @@ -466,7 +467,9 @@ impl RefWindow { #[cfg(test)] mod tests { - use super::{DeathRowQueue, RefWindow, CACHE_BATCH_SIZE}; + use super::{ + to_journal_key, DeathRowQueue, JournalRecord, RefWindow, CACHE_BATCH_SIZE, LAST_PRUNED, + }; use crate::{ noncanonical::LAST_CANONICAL, test::{make_commit, make_db, TestDb}, @@ -681,6 +684,66 @@ mod tests { .push((to_meta_key(LAST_CANONICAL, &()), (block, block).encode())); } + fn push_last_pruned(block: u64, commit: &mut CommitSet) { + commit.meta.inserted.push((to_meta_key(LAST_PRUNED, &()), block.encode())); + } + + #[test] + fn init_db_backed_queue() { + let mut db = make_db(&[]); + let mut commit = CommitSet::default(); + + fn load_pruning_from_db(db: &TestDb) -> (usize, u64) { + let pruning: RefWindow = RefWindow::new(db, false).unwrap(); + let (cache, _) = pruning.queue.get_db_backed_queue_state().unwrap(); + (cache.len(), pruning.pending_number) + } + + fn push_record(block: u64, commit: &mut CommitSet) { + commit + .meta + .inserted + .push((to_journal_key(block), JournalRecord::::default().encode())); + } + + // empty database + let (loaded_blocks, pending_number) = load_pruning_from_db(&db); + assert_eq!(loaded_blocks, 0); + assert_eq!(pending_number, 0); + + // canonicalized the genesis block but no pruning + push_last_canonicalized(0, &mut commit); + push_record(0, &mut commit); + db.commit(&commit); + let (loaded_blocks, pending_number) = load_pruning_from_db(&db); + assert_eq!(loaded_blocks, 1); + assert_eq!(pending_number, 0); + + // pruned the genesis block + push_last_pruned(0, &mut commit); + db.commit(&commit); + let (loaded_blocks, pending_number) = load_pruning_from_db(&db); + assert_eq!(loaded_blocks, 0); + assert_eq!(pending_number, 1); + + // canonicalize more blocks + push_last_canonicalized(10, &mut commit); + for i in 1..=10 { + push_record(i, &mut commit); + } + db.commit(&commit); + let (loaded_blocks, pending_number) = load_pruning_from_db(&db); + assert_eq!(loaded_blocks, 10); + assert_eq!(pending_number, 1); + + // pruned all blocks + push_last_pruned(10, &mut commit); + db.commit(&commit); + let (loaded_blocks, pending_number) = load_pruning_from_db(&db); + assert_eq!(loaded_blocks, 0); + assert_eq!(pending_number, 11); + } + #[test] fn db_backed_queue() { let mut pruning: RefWindow = From 1944c5cb247b180ea0f6370ac2ab152c30b4f130 Mon Sep 17 00:00:00 2001 From: linning Date: Mon, 8 Aug 2022 20:16:17 +0800 Subject: [PATCH 08/19] update comment Signed-off-by: linning --- client/state-db/src/lib.rs | 2 ++ client/state-db/src/pruning.rs | 5 ++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/client/state-db/src/lib.rs b/client/state-db/src/lib.rs index 3f43ea13c2f6c..ad843a60634d4 100644 --- a/client/state-db/src/lib.rs +++ b/client/state-db/src/lib.rs @@ -565,6 +565,7 @@ impl } /// Prevents pruning of specified block and its descendants. + /// NOTE: `pin` should only be called for blocks with existing headers pub fn pin(&self, hash: &BlockHash, number: u64) -> Result<(), PinError> { self.db.write().pin(hash, number) } @@ -607,6 +608,7 @@ impl } /// Check if block is pruned away. + /// NOTE: `is_pruned` should only be called for blocks with existing headers pub fn is_pruned(&self, hash: &BlockHash, number: u64) -> bool { return self.db.read().is_pruned(hash, number) } diff --git a/client/state-db/src/pruning.rs b/client/state-db/src/pruning.rs index bc5d0fd4fe4f7..c13f287400a8b 100644 --- a/client/state-db/src/pruning.rs +++ b/client/state-db/src/pruning.rs @@ -57,8 +57,7 @@ pub struct RefWindow { /// - `Mem`, used when the backend database do not supports reference counting, keep all /// blocks in memory, and keep track of re-inserted keys to not delete them when pruning /// - `DbBacked`, used when the backend database supports reference counting, only keep -/// a few number of blocks in memory and load more blocks on demand, it also keep all the -/// block's hash in memory for checking block existence +/// a few number of blocks in memory and load more blocks on demand #[derive(parity_util_mem_derive::MallocSizeOf)] enum DeathRowQueue { Mem { @@ -186,7 +185,7 @@ impl DeathRowQueue { *unload_blocks -= amout; return } - // clear `hashs` and remove remain blocks from `cache` + // reset `unload_blocks` and remove remain blocks from `cache` let remain = amout - *unload_blocks; *unload_blocks = 0; cache.truncate(cache.len() - remain); From d042666e55956080a2fc02f4e601cc2c1b839af0 Mon Sep 17 00:00:00 2001 From: linning Date: Mon, 8 Aug 2022 20:16:36 +0800 Subject: [PATCH 09/19] add check for have_state_at Signed-off-by: linning --- client/db/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/db/src/lib.rs b/client/db/src/lib.rs index 03d2facbac8f2..c97eda4f59b00 100644 --- a/client/db/src/lib.rs +++ b/client/db/src/lib.rs @@ -2293,7 +2293,8 @@ impl sc_client_api::backend::Backend for Backend { _ => false, } } else { - !self.storage.state_db.is_pruned(hash, number.saturated_into::()) + self.blockchain.header_metadata(*hash).is_ok() && + !self.storage.state_db.is_pruned(hash, number.saturated_into::()) } } From 869eb5da4bddcf16452f5226f6db4e213f77e641 Mon Sep 17 00:00:00 2001 From: linning Date: Tue, 9 Aug 2022 18:09:15 +0800 Subject: [PATCH 10/19] address comment Signed-off-by: linning --- client/db/src/lib.rs | 35 +++++++++---- client/state-db/src/lib.rs | 91 +++++++++++++++++++++++++--------- client/state-db/src/pruning.rs | 69 ++++++++++++++++++++++---- 3 files changed, 153 insertions(+), 42 deletions(-) diff --git a/client/db/src/lib.rs b/client/db/src/lib.rs index c97eda4f59b00..ca7d99499f729 100644 --- a/client/db/src/lib.rs +++ b/client/db/src/lib.rs @@ -63,7 +63,7 @@ use sc_client_api::{ utils::is_descendent_of, IoInfo, MemoryInfo, MemorySize, UsageInfo, }; -use sc_state_db::StateDb; +use sc_state_db::{IsPruned, StateDb, StateDbSync}; use sp_arithmetic::traits::Saturating; use sp_blockchain::{ well_known_cache_keys, Backend as _, CachedHeaderMetadata, Error as ClientError, HeaderBackend, @@ -2250,13 +2250,16 @@ impl sc_client_api::backend::Backend for Backend { match self.blockchain.header_metadata(hash) { Ok(ref hdr) => { - if !self.have_state_at(&hash, hdr.number) { - return Err(sp_blockchain::Error::UnknownBlock(format!( - "State already discarded for {:?}", - block - ))) - } - if let Ok(()) = self.storage.state_db.pin(&hash, hdr.number.saturated_into::()) + // NOTE: similar to `sp_state_machine::Storage::get` but we can't use + // `sp_state_machine::Storage::get` directly in order to avoid dead lock + let hint = |state_db: &StateDbSync<_, _, _>| { + state_db + .get(hdr.state_root.as_ref(), self.storage.as_ref()) + .unwrap_or(None) + .is_some() + }; + if let Ok(()) = + self.storage.state_db.pin(&hash, hdr.number.saturated_into::(), hint) { let root = hdr.state_root; let db_state = DbState::::new(self.storage.clone(), root); @@ -2293,8 +2296,20 @@ impl sc_client_api::backend::Backend for Backend { _ => false, } } else { - self.blockchain.header_metadata(*hash).is_ok() && - !self.storage.state_db.is_pruned(hash, number.saturated_into::()) + match self.storage.state_db.is_pruned(hash, number.saturated_into::()) { + IsPruned::Pruned => false, + IsPruned::NotPruned => true, + IsPruned::MaybePruned => match self.blockchain.header_metadata(*hash) { + Ok(header) => sp_state_machine::Storage::get( + self.storage.as_ref(), + &header.state_root, + (&[], None), + ) + .unwrap_or(None) + .is_some(), + _ => false, + }, + } } } diff --git a/client/state-db/src/lib.rs b/client/state-db/src/lib.rs index ad843a60634d4..232eff5bd2c54 100644 --- a/client/state-db/src/lib.rs +++ b/client/state-db/src/lib.rs @@ -51,7 +51,7 @@ use log::trace; use noncanonical::NonCanonicalOverlay; use parity_util_mem::{malloc_size, MallocSizeOf}; use parking_lot::RwLock; -use pruning::RefWindow; +use pruning::{HaveBlock, RefWindow}; use sc_client_api::{MemorySize, StateDbMemoryInfo}; use std::{ collections::{hash_map::Entry, HashMap}, @@ -276,7 +276,7 @@ fn to_meta_key(suffix: &[u8], data: &S) -> Vec { buffer } -struct StateDbSync { +pub struct StateDbSync { mode: PruningMode, non_canonical: NonCanonicalOverlay, pruning: Option>, @@ -350,17 +350,28 @@ impl self.non_canonical.last_canonicalized_block_number() } - fn is_pruned(&self, hash: &BlockHash, number: u64) -> bool { - match self.mode { + fn is_pruned(&self, hash: &BlockHash, number: u64) -> IsPruned { + let is_pruned = match self.mode { PruningMode::ArchiveAll => false, PruningMode::ArchiveCanonical | PruningMode::Constrained(_) => { if self.best_canonical().map(|c| number > c).unwrap_or(true) { !self.non_canonical.have_block(hash) } else { - self.pruning.as_ref().map_or(false, |pruning| !pruning.have_block(number)) + match self.pruning.as_ref() { + None => false, + Some(pruning) => { + let have_block = match pruning.have_block(hash, number) { + HaveBlock::NotHave => false, + HaveBlock::Have => true, + HaveBlock::MayHave => return IsPruned::MaybePruned, + }; + !have_block + }, + } } }, - } + }; + is_pruned.into() } fn prune(&mut self, commit: &mut CommitSet) -> Result<(), Error> { @@ -405,13 +416,22 @@ impl } } - fn pin(&mut self, hash: &BlockHash, number: u64) -> Result<(), PinError> { + fn pin(&mut self, hash: &BlockHash, number: u64, hint: F) -> Result<(), PinError> + where + F: Fn(&StateDbSync) -> bool, + { match self.mode { PruningMode::ArchiveAll => Ok(()), PruningMode::ArchiveCanonical | PruningMode::Constrained(_) => { - if self.non_canonical.have_block(hash) || - self.pruning.as_ref().map_or(false, |pruning| pruning.have_block(number)) - { + let have_block = self.non_canonical.have_block(hash) || + self.pruning.as_ref().map_or(false, |pruning| { + match pruning.have_block(hash, number) { + HaveBlock::NotHave => false, + HaveBlock::Have => true, + HaveBlock::MayHave => hint(&self), + } + }); + if have_block { let refs = self.pinned.entry(hash.clone()).or_default(); if *refs == 0 { trace!(target: "state-db-pin", "Pinned block: {:?}", hash); @@ -565,9 +585,12 @@ impl } /// Prevents pruning of specified block and its descendants. - /// NOTE: `pin` should only be called for blocks with existing headers - pub fn pin(&self, hash: &BlockHash, number: u64) -> Result<(), PinError> { - self.db.write().pin(hash, number) + /// `hint` used for futher checking if the given block exists + pub fn pin(&self, hash: &BlockHash, number: u64, hint: F) -> Result<(), PinError> + where + F: Fn(&StateDbSync) -> bool, + { + self.db.write().pin(hash, number, hint) } /// Allows pruning of specified block. @@ -608,8 +631,7 @@ impl } /// Check if block is pruned away. - /// NOTE: `is_pruned` should only be called for blocks with existing headers - pub fn is_pruned(&self, hash: &BlockHash, number: u64) -> bool { + pub fn is_pruned(&self, hash: &BlockHash, number: u64) -> IsPruned { return self.db.read().is_pruned(hash, number) } @@ -629,6 +651,27 @@ impl } } +/// The result return by `StateDb::is_pruned` +#[derive(Debug, PartialEq, Eq)] +pub enum IsPruned { + /// Definitely pruned + Pruned, + /// Definitely not pruned + NotPruned, + /// May or may not pruned, need futher checking + MaybePruned, +} + +impl From for IsPruned { + fn from(pruned: bool) -> Self { + if pruned { + IsPruned::Pruned + } else { + IsPruned::NotPruned + } + } +} + fn fetch_stored_pruning_mode(db: &D) -> Result, Error> { let meta_key_mode = to_meta_key(PRUNING_MODE, &()); if let Some(stored_mode) = db.get_meta(&meta_key_mode).map_err(Error::Db)? { @@ -666,7 +709,7 @@ mod tests { noncanonical::LAST_CANONICAL, pruning::LAST_PRUNED, test::{make_changeset, make_db, TestDb}, - to_meta_key, CommitSet, Constraints, Error, PruningMode, StateDb, StateDbError, + to_meta_key, CommitSet, Constraints, Error, IsPruned, PruningMode, StateDb, StateDbError, }; use codec::Encode; use sp_core::H256; @@ -753,7 +796,7 @@ mod tests { fn full_archive_keeps_everything() { let (db, sdb) = make_test_db(PruningMode::ArchiveAll); assert!(db.data_eq(&make_db(&[1, 21, 22, 3, 4, 91, 921, 922, 93, 94]))); - assert!(!sdb.is_pruned(&H256::from_low_u64_be(0), 0)); + assert_eq!(sdb.is_pruned(&H256::from_low_u64_be(0), 0), IsPruned::NotPruned); } #[test] @@ -777,9 +820,10 @@ mod tests { max_blocks: Some(1), max_mem: None, })); - assert!(sdb.is_pruned(&H256::from_low_u64_be(0), 0)); - assert!(sdb.is_pruned(&H256::from_low_u64_be(1), 1)); - assert!(sdb.is_pruned(&H256::from_low_u64_be(21), 2)); + assert_eq!(sdb.is_pruned(&H256::from_low_u64_be(0), 0), IsPruned::Pruned); + assert_eq!(sdb.is_pruned(&H256::from_low_u64_be(1), 1), IsPruned::Pruned); + assert_eq!(sdb.is_pruned(&H256::from_low_u64_be(21), 2), IsPruned::Pruned); + assert_eq!(sdb.is_pruned(&H256::from_low_u64_be(22), 2), IsPruned::Pruned); assert!(db.data_eq(&make_db(&[21, 3, 922, 93, 94]))); } @@ -789,9 +833,10 @@ mod tests { max_blocks: Some(2), max_mem: None, })); - assert!(sdb.is_pruned(&H256::from_low_u64_be(0), 0)); - assert!(sdb.is_pruned(&H256::from_low_u64_be(1), 1)); - assert!(!sdb.is_pruned(&H256::from_low_u64_be(21), 2)); + assert_eq!(sdb.is_pruned(&H256::from_low_u64_be(0), 0), IsPruned::Pruned); + assert_eq!(sdb.is_pruned(&H256::from_low_u64_be(1), 1), IsPruned::Pruned); + assert_eq!(sdb.is_pruned(&H256::from_low_u64_be(21), 2), IsPruned::NotPruned); + assert_eq!(sdb.is_pruned(&H256::from_low_u64_be(22), 2), IsPruned::Pruned); assert!(db.data_eq(&make_db(&[1, 21, 3, 921, 922, 93, 94]))); } diff --git a/client/state-db/src/pruning.rs b/client/state-db/src/pruning.rs index c13f287400a8b..f2c3d5e1fe351 100644 --- a/client/state-db/src/pruning.rs +++ b/client/state-db/src/pruning.rs @@ -278,6 +278,23 @@ impl DeathRowQueue { } } + /// Check if the block at the given `index` of the queue exist + /// it is the caller's responsibility to ensure `index` won't be out of bound + fn have_block(&self, hash: &BlockHash, index: usize) -> HaveBlock { + match self { + DeathRowQueue::DbBacked { cache, .. } => { + if cache.len() > index { + (cache[index].hash == *hash).into() + } else { + // the block not exist in `cache`, but it may exist in the unload + // blocks + HaveBlock::MayHave + } + }, + DeathRowQueue::Mem { death_rows, .. } => (death_rows[index].hash == *hash).into(), + } + } + /// Return the number of block in the pruning window fn len(&self) -> usize { match self { @@ -344,6 +361,27 @@ fn to_journal_key(block: u64) -> Vec { to_meta_key(PRUNING_JOURNAL, &block) } +/// The result return by `RefWindow::have_block` +#[derive(Debug, PartialEq, Eq)] +pub enum HaveBlock { + /// Definitely not having this block + NotHave, + /// May or may not have this block, need futher checking + MayHave, + /// Definitely having this block + Have, +} + +impl From for HaveBlock { + fn from(have: bool) -> Self { + if have { + HaveBlock::Have + } else { + HaveBlock::NotHave + } + } +} + impl RefWindow { pub fn new( db: &D, @@ -398,9 +436,20 @@ impl RefWindow { self.pending_number + self.pending_prunings as u64 } - // Return true if a canonicalized block is in the window and not be pruned yet - pub fn have_block(&self, number: u64) -> bool { - number >= self.pending() && number < self.pending_number + self.queue.len() as u64 + fn is_empty(&self) -> bool { + self.queue.len() <= self.pending_prunings + } + + // Check if a block is in the pruning window and not be pruned yet + pub fn have_block(&self, hash: &BlockHash, number: u64) -> HaveBlock { + // if the queue is empty or the block number exceed the pruning window, we definitely + // do not have this block + if self.is_empty() || + !(number >= self.pending() && number < self.pending_number + self.queue.len() as u64) + { + return HaveBlock::NotHave + } + self.queue.have_block(hash, (number - self.pending_number) as usize) } /// Prune next block. Expects at least one block in the window. Adds changes to `commit`. @@ -467,7 +516,8 @@ impl RefWindow { #[cfg(test)] mod tests { use super::{ - to_journal_key, DeathRowQueue, JournalRecord, RefWindow, CACHE_BATCH_SIZE, LAST_PRUNED, + to_journal_key, DeathRowQueue, HaveBlock, JournalRecord, RefWindow, CACHE_BATCH_SIZE, + LAST_PRUNED, }; use crate::{ noncanonical::LAST_CANONICAL, @@ -513,11 +563,12 @@ mod tests { let mut db = make_db(&[1, 2, 3]); let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); let mut commit = make_commit(&[4, 5], &[1, 3]); - pruning.note_canonical(&H256::random(), &mut commit); + let hash = H256::random(); + pruning.note_canonical(&hash, &mut commit); db.commit(&commit); - assert!(pruning.have_block(0)); + assert_eq!(pruning.have_block(&hash, 0), HaveBlock::Have); pruning.apply_pending(); - assert!(pruning.have_block(0)); + assert_eq!(pruning.have_block(&hash, 0), HaveBlock::Have); assert!(commit.data.deleted.is_empty()); let (death_rows, death_index) = pruning.queue.get_mem_queue_state().unwrap(); assert_eq!(death_rows.len(), 1); @@ -527,10 +578,10 @@ mod tests { let mut commit = CommitSet::default(); pruning.prune_one(&mut commit).unwrap(); - assert!(!pruning.have_block(0)); + assert_eq!(pruning.have_block(&hash, 0), HaveBlock::NotHave); db.commit(&commit); pruning.apply_pending(); - assert!(!pruning.have_block(0)); + assert_eq!(pruning.have_block(&hash, 0), HaveBlock::NotHave); assert!(db.data_eq(&make_db(&[2, 4, 5]))); let (death_rows, death_index) = pruning.queue.get_mem_queue_state().unwrap(); assert!(death_rows.is_empty()); From d0c7710a3b4a76f072101819ac4db728b27e0b96 Mon Sep 17 00:00:00 2001 From: linning Date: Wed, 10 Aug 2022 02:38:12 +0800 Subject: [PATCH 11/19] renanme unload_blocks to uncached_blocks Signed-off-by: linning --- client/state-db/src/pruning.rs | 108 +++++++++++++++++---------------- 1 file changed, 55 insertions(+), 53 deletions(-) diff --git a/client/state-db/src/pruning.rs b/client/state-db/src/pruning.rs index f2c3d5e1fe351..9f226c1c57e3f 100644 --- a/client/state-db/src/pruning.rs +++ b/client/state-db/src/pruning.rs @@ -74,7 +74,7 @@ enum DeathRowQueue { /// successive and ordered by block number cache: VecDeque>, /// The number of blocks that not loaded into `cache` - unload_blocks: usize, + uncached_blocks: usize, }, } @@ -109,26 +109,26 @@ impl DeathRowQueue { fn new_db_backed( db: D, base: u64, - mut unload_blocks: usize, + mut uncached_blocks: usize, ) -> Result, Error> { let mut cache = VecDeque::with_capacity(CACHE_BATCH_SIZE); trace!(target: "state-db", "Reading pruning journal for the database-backed queue. Pending #{}", base); // Load block from db - DeathRowQueue::load_batch_from_db(&db, &mut unload_blocks, &mut cache, base)?; - Ok(DeathRowQueue::DbBacked { db, cache, unload_blocks }) + DeathRowQueue::load_batch_from_db(&db, &mut uncached_blocks, &mut cache, base)?; + Ok(DeathRowQueue::DbBacked { db, cache, uncached_blocks }) } /// import a new block to the back of the queue fn import(&mut self, base: u64, journal_record: JournalRecord) { let JournalRecord { hash, inserted, deleted } = journal_record; match self { - DeathRowQueue::DbBacked { unload_blocks, cache, .. } => { - // `unload_blocks` is zero means currently all block are loaded into `cache` + DeathRowQueue::DbBacked { uncached_blocks, cache, .. } => { + // `uncached_blocks` is zero means currently all block are loaded into `cache` // thus if `cache` is not full, load the next block into `cache` too - if *unload_blocks == 0 && cache.len() < CACHE_BATCH_SIZE { + if *uncached_blocks == 0 && cache.len() < CACHE_BATCH_SIZE { cache.push_back(DeathRow { hash, deleted: deleted.into_iter().collect() }); } else { - *unload_blocks += 1; + *uncached_blocks += 1; } }, DeathRowQueue::Mem { death_rows, death_index } => { @@ -155,10 +155,10 @@ impl DeathRowQueue { base: u64, ) -> Result>, Error> { match self { - DeathRowQueue::DbBacked { db, unload_blocks, cache } => { - if cache.is_empty() && *unload_blocks != 0 { + DeathRowQueue::DbBacked { db, uncached_blocks, cache } => { + if cache.is_empty() && *uncached_blocks != 0 { // load more blocks from db since there are still blocks in it - DeathRowQueue::load_batch_from_db(db, unload_blocks, cache, base)?; + DeathRowQueue::load_batch_from_db(db, uncached_blocks, cache, base)?; } Ok(cache.pop_front()) }, @@ -179,15 +179,15 @@ impl DeathRowQueue { fn revert_recent_add(&mut self, base: u64, amout: usize) { debug_assert!(amout <= self.len()); match self { - DeathRowQueue::DbBacked { unload_blocks, cache, .. } => { - // remove from `unload_blocks` if it can cover - if *unload_blocks >= amout { - *unload_blocks -= amout; + DeathRowQueue::DbBacked { uncached_blocks, cache, .. } => { + // remove from `uncached_blocks` if it can cover + if *uncached_blocks >= amout { + *uncached_blocks -= amout; return } - // reset `unload_blocks` and remove remain blocks from `cache` - let remain = amout - *unload_blocks; - *unload_blocks = 0; + // reset `uncached_blocks` and remove remain blocks from `cache` + let remain = amout - *uncached_blocks; + *uncached_blocks = 0; cache.truncate(cache.len() - remain); }, DeathRowQueue::Mem { death_rows, death_index } => { @@ -208,17 +208,17 @@ impl DeathRowQueue { /// of the queue fn load_batch_from_db( db: &D, - unload_blocks: &mut usize, + uncached_blocks: &mut usize, cache: &mut VecDeque>, base: u64, ) -> Result<(), Error> { // return if all blocks already loaded into `cache` and there are no other // blocks in the backend database - if *unload_blocks == 0 { + if *uncached_blocks == 0 { return Ok(()) } let start = base + cache.len() as u64; - let batch_size = cmp::min(*unload_blocks, CACHE_BATCH_SIZE); + let batch_size = cmp::min(*uncached_blocks, CACHE_BATCH_SIZE); let mut loaded = 0; for i in 0..batch_size as u64 { match load_death_row_from_db::(db, start + i)? { @@ -232,7 +232,7 @@ impl DeathRowQueue { // `loaded` should be the same as what we expect, if there are missing blocks // `load_death_row_from_db` should return a db error debug_assert_eq!(batch_size, loaded); - *unload_blocks -= loaded; + *uncached_blocks -= loaded; Ok(()) } @@ -244,12 +244,12 @@ impl DeathRowQueue { index: usize, ) -> Result>, Error> { match self { - DeathRowQueue::DbBacked { db, unload_blocks, cache } => { + DeathRowQueue::DbBacked { db, uncached_blocks, cache } => { // check if `index` target a block reside on disk - if index >= cache.len() && index < cache.len() + *unload_blocks { + if index >= cache.len() && index < cache.len() + *uncached_blocks { // if `index` target the next batch of `DeathRow`, load a batch from db - if index - cache.len() < cmp::min(*unload_blocks, CACHE_BATCH_SIZE) { - DeathRowQueue::load_batch_from_db(db, unload_blocks, cache, base)?; + if index - cache.len() < cmp::min(*uncached_blocks, CACHE_BATCH_SIZE) { + DeathRowQueue::load_batch_from_db(db, uncached_blocks, cache, base)?; } else { // load a single `DeathRow` from db, but do not insert it to `cache` // because `cache` is a queue of successive `DeathRow` @@ -298,7 +298,8 @@ impl DeathRowQueue { /// Return the number of block in the pruning window fn len(&self) -> usize { match self { - DeathRowQueue::DbBacked { unload_blocks, cache, .. } => cache.len() + *unload_blocks, + DeathRowQueue::DbBacked { uncached_blocks, cache, .. } => + cache.len() + *uncached_blocks, DeathRowQueue::Mem { death_rows, .. } => death_rows.len(), } } @@ -316,7 +317,8 @@ impl DeathRowQueue { #[cfg(test)] fn get_db_backed_queue_state(&self) -> Option<(&VecDeque>, usize)> { match self { - DeathRowQueue::DbBacked { cache, unload_blocks, .. } => Some((cache, *unload_blocks)), + DeathRowQueue::DbBacked { cache, uncached_blocks, .. } => + Some((cache, *uncached_blocks)), DeathRowQueue::Mem { .. } => None, } } @@ -800,9 +802,9 @@ mod tests { RefWindow::new(&make_db(&[]), false).unwrap(); // start as an empty queue - let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); + let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), 0); - assert_eq!(unload_blocks, 0); + assert_eq!(uncached_blocks, 0); // import blocks // queue size and content should match @@ -812,20 +814,20 @@ mod tests { push_last_canonicalized(i as u64, &mut commit); pruning.queue.get_db().unwrap().commit(&commit); // block will fill in cache first - let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); + let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); if i < CACHE_BATCH_SIZE { assert_eq!(cache.len(), i + 1); - assert_eq!(unload_blocks, 0); + assert_eq!(uncached_blocks, 0); } else { assert_eq!(cache.len(), CACHE_BATCH_SIZE); - assert_eq!(unload_blocks, i - CACHE_BATCH_SIZE + 1); + assert_eq!(uncached_blocks, i - CACHE_BATCH_SIZE + 1); } } pruning.apply_pending(); assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 10); - let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); + let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), CACHE_BATCH_SIZE); - assert_eq!(unload_blocks, 10); + assert_eq!(uncached_blocks, 10); for i in 0..CACHE_BATCH_SIZE { assert_eq!(cache[i].hash, i as u64); } @@ -835,17 +837,17 @@ mod tests { let mut commit = CommitSet::default(); pruning.note_canonical(&(CACHE_BATCH_SIZE as u64 + 10), &mut commit); assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 11); - let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); + let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), CACHE_BATCH_SIZE); - assert_eq!(unload_blocks, 11); + assert_eq!(uncached_blocks, 11); // revert the last add that no apply yet // NOTE: do not commit the previous `CommitSet` to db pruning.queue.revert_recent_add(0, 1); assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 10); - let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); + let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), CACHE_BATCH_SIZE); - assert_eq!(unload_blocks, 10); + assert_eq!(uncached_blocks, 10); // remove one block from the start of the queue // block is removed from the head of cache @@ -854,9 +856,9 @@ mod tests { pruning.queue.get_db().unwrap().commit(&commit); pruning.apply_pending(); assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 9); - let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); + let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), CACHE_BATCH_SIZE - 1); - assert_eq!(unload_blocks, 10); + assert_eq!(uncached_blocks, 10); for i in 0..(CACHE_BATCH_SIZE - 1) { assert_eq!(cache[i].hash, (i + 1) as u64); } @@ -866,9 +868,9 @@ mod tests { let pruning: RefWindow = RefWindow::new(pruning.queue.get_db().unwrap(), false).unwrap(); assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 9); - let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); + let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), CACHE_BATCH_SIZE); - assert_eq!(unload_blocks, 9); + assert_eq!(uncached_blocks, 9); for i in 0..CACHE_BATCH_SIZE { assert_eq!(cache[i].hash, (i + 1) as u64); } @@ -896,15 +898,15 @@ mod tests { CACHE_BATCH_SIZE as u64 - 1 ); assert_eq!(pruning.queue.get(0, CACHE_BATCH_SIZE * 2 + 10).unwrap(), None); - let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); + let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), CACHE_BATCH_SIZE); - assert_eq!(unload_blocks, CACHE_BATCH_SIZE + 10); + assert_eq!(uncached_blocks, CACHE_BATCH_SIZE + 10); // getting a block not in cache will triger loading block from db assert_eq!(pruning.queue.get(0, index).unwrap().unwrap().hash, CACHE_BATCH_SIZE as u64); - let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); + let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), CACHE_BATCH_SIZE * 2); - assert_eq!(unload_blocks, 10); + assert_eq!(uncached_blocks, 10); // clear all block loaded in cache for _ in 0..CACHE_BATCH_SIZE * 2 { @@ -913,9 +915,9 @@ mod tests { pruning.queue.get_db().unwrap().commit(&commit); } pruning.apply_pending(); - let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); + let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert!(cache.is_empty()); - assert_eq!(unload_blocks, 10); + assert_eq!(uncached_blocks, 10); // getting the hash of block that not in cache will also triger loading // the remaining blocks from db @@ -923,18 +925,18 @@ mod tests { pruning.queue.get(pruning.pending_number, 0).unwrap().unwrap().hash, (CACHE_BATCH_SIZE * 2) as u64 ); - let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); + let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), 10); - assert_eq!(unload_blocks, 0); + assert_eq!(uncached_blocks, 0); // load a new queue from db // `cache` should be the same let pruning: RefWindow = RefWindow::new(pruning.queue.get_db().unwrap(), false).unwrap(); assert_eq!(pruning.queue.len(), 10); - let (cache, unload_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); + let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), 10); - assert_eq!(unload_blocks, 0); + assert_eq!(uncached_blocks, 0); for i in 0..10 { assert_eq!(cache[i].hash, (CACHE_BATCH_SIZE * 2 + i) as u64); } From abf1354dc1eb05a04d2d695a2468bed02915ac22 Mon Sep 17 00:00:00 2001 From: linning Date: Fri, 12 Aug 2022 04:08:49 +0800 Subject: [PATCH 12/19] address comment Signed-off-by: linning --- client/db/src/lib.rs | 13 ++--- client/state-db/src/lib.rs | 89 +++++++++++++---------------- client/state-db/src/noncanonical.rs | 4 +- client/state-db/src/pruning.rs | 76 ++++++++++++------------ client/state-db/src/test.rs | 34 +++++++---- 5 files changed, 104 insertions(+), 112 deletions(-) diff --git a/client/db/src/lib.rs b/client/db/src/lib.rs index ca7d99499f729..1ca8147a1cbec 100644 --- a/client/db/src/lib.rs +++ b/client/db/src/lib.rs @@ -63,7 +63,7 @@ use sc_client_api::{ utils::is_descendent_of, IoInfo, MemoryInfo, MemorySize, UsageInfo, }; -use sc_state_db::{IsPruned, StateDb, StateDbSync}; +use sc_state_db::{IsPruned, StateDb}; use sp_arithmetic::traits::Saturating; use sp_blockchain::{ well_known_cache_keys, Backend as _, CachedHeaderMetadata, Error as ClientError, HeaderBackend, @@ -1094,7 +1094,7 @@ impl Backend { let map_e = sp_blockchain::Error::from_state_db; let (state_db_init_commit_set, state_db) = StateDb::open( - &state_meta_db, + state_meta_db, requested_state_pruning, !db.supports_ref_counting(), should_init, @@ -1454,7 +1454,7 @@ impl Backend { .storage .state_db .insert_block(&hash, number_u64, pending_block.header.parent_hash(), changeset) - .map_err(|e: sc_state_db::Error| { + .map_err(|e: sc_state_db::Error| { sp_blockchain::Error::from_state_db(e) })?; apply_state_commit(&mut transaction, commit); @@ -2250,11 +2250,8 @@ impl sc_client_api::backend::Backend for Backend { match self.blockchain.header_metadata(hash) { Ok(ref hdr) => { - // NOTE: similar to `sp_state_machine::Storage::get` but we can't use - // `sp_state_machine::Storage::get` directly in order to avoid dead lock - let hint = |state_db: &StateDbSync<_, _, _>| { - state_db - .get(hdr.state_root.as_ref(), self.storage.as_ref()) + let hint = || { + sc_state_db::NodeDb::get(self.storage.as_ref(), hdr.state_root.as_ref()) .unwrap_or(None) .is_some() }; diff --git a/client/state-db/src/lib.rs b/client/state-db/src/lib.rs index 232eff5bd2c54..41040c1653e0e 100644 --- a/client/state-db/src/lib.rs +++ b/client/state-db/src/lib.rs @@ -62,6 +62,7 @@ const PRUNING_MODE: &[u8] = b"mode"; const PRUNING_MODE_ARCHIVE: &[u8] = b"archive"; const PRUNING_MODE_ARCHIVE_CANON: &[u8] = b"archive_canonical"; const PRUNING_MODE_CONSTRAINED: &[u8] = b"constrained"; +pub(crate) const MAX_BLOCK_CONSTRAINT: u32 = 256; /// Database value type. pub type DBValue = Vec; @@ -98,7 +99,7 @@ impl< } /// Backend database trait. Read-only. -pub trait MetaDb: Clone { +pub trait MetaDb { type Error: fmt::Debug; /// Get meta value, such as the journal. @@ -266,7 +267,7 @@ impl Default for PruningMode { impl Default for Constraints { fn default() -> Self { - Self { max_blocks: Some(256), max_mem: None } + Self { max_blocks: Some(MAX_BLOCK_CONSTRAINT), max_mem: None } } } @@ -289,11 +290,11 @@ impl fn new( mode: PruningMode, ref_counting: bool, - db: &D, + db: D, ) -> Result, Error> { trace!(target: "state-db", "StateDb settings: {:?}. Ref-counting: {}", mode, ref_counting); - let non_canonical: NonCanonicalOverlay = NonCanonicalOverlay::new(db)?; + let non_canonical: NonCanonicalOverlay = NonCanonicalOverlay::new(&db)?; let pruning: Option> = match mode { PruningMode::Constrained(Constraints { max_mem: Some(_), .. }) => unimplemented!(), PruningMode::Constrained(_) => Some(RefWindow::new(db, ref_counting)?), @@ -303,13 +304,13 @@ impl Ok(StateDbSync { mode, non_canonical, pruning, pinned: Default::default() }) } - fn insert_block( + fn insert_block( &mut self, hash: &BlockHash, number: u64, parent_hash: &BlockHash, mut changeset: ChangeSet, - ) -> Result, Error> { + ) -> Result, Error> { match self.mode { PruningMode::ArchiveAll => { changeset.deleted.clear(); @@ -324,7 +325,7 @@ impl } fn canonicalize_block(&mut self, hash: &BlockHash) -> Result, Error> { - // NOTE: it is importent that the change to `LAST_CANONICAL` (emit from + // NOTE: it is important that the change to `LAST_CANONICAL` (emit from // `non_canonical.canonicalize`) and the insert of the new pruning journal (emit from // `pruning.note_canonical`) are collected into the same `CommitSet` and are committed to // the database atomically to keep their consistency when restarting the node @@ -351,27 +352,27 @@ impl } fn is_pruned(&self, hash: &BlockHash, number: u64) -> IsPruned { - let is_pruned = match self.mode { - PruningMode::ArchiveAll => false, + match self.mode { + PruningMode::ArchiveAll => IsPruned::NotPruned, PruningMode::ArchiveCanonical | PruningMode::Constrained(_) => { if self.best_canonical().map(|c| number > c).unwrap_or(true) { - !self.non_canonical.have_block(hash) + if !self.non_canonical.have_block(hash) { + IsPruned::Pruned + } else { + IsPruned::NotPruned + } } else { match self.pruning.as_ref() { - None => false, - Some(pruning) => { - let have_block = match pruning.have_block(hash, number) { - HaveBlock::NotHave => false, - HaveBlock::Have => true, - HaveBlock::MayHave => return IsPruned::MaybePruned, - }; - !have_block + None => IsPruned::NotPruned, + Some(pruning) => match pruning.have_block(hash, number) { + HaveBlock::NotHave => IsPruned::Pruned, + HaveBlock::Have => IsPruned::NotPruned, + HaveBlock::MayHave => return IsPruned::MaybePruned, }, } } }, - }; - is_pruned.into() + } } fn prune(&mut self, commit: &mut CommitSet) -> Result<(), Error> { @@ -418,7 +419,7 @@ impl fn pin(&mut self, hash: &BlockHash, number: u64, hint: F) -> Result<(), PinError> where - F: Fn(&StateDbSync) -> bool, + F: Fn() -> bool, { match self.mode { PruningMode::ArchiveAll => Ok(()), @@ -428,7 +429,7 @@ impl match pruning.have_block(hash, number) { HaveBlock::NotHave => false, HaveBlock::Have => true, - HaveBlock::MayHave => hint(&self), + HaveBlock::MayHave => hint(), } }); if have_block { @@ -521,12 +522,12 @@ impl { /// Create an instance of [`StateDb`]. pub fn open( - db: &D, + db: D, requested_mode: Option, ref_counting: bool, should_init: bool, ) -> Result<(CommitSet, StateDb), Error> { - let stored_mode = fetch_stored_pruning_mode(db)?; + let stored_mode = fetch_stored_pruning_mode(&db)?; let selected_mode = match (should_init, stored_mode, requested_mode) { (true, stored_mode, requested_mode) => { @@ -569,13 +570,13 @@ impl } /// Add a new non-canonical block. - pub fn insert_block( + pub fn insert_block( &self, hash: &BlockHash, number: u64, parent_hash: &BlockHash, changeset: ChangeSet, - ) -> Result, Error> { + ) -> Result, Error> { self.db.write().insert_block(hash, number, parent_hash, changeset) } @@ -588,7 +589,7 @@ impl /// `hint` used for futher checking if the given block exists pub fn pin(&self, hash: &BlockHash, number: u64, hint: F) -> Result<(), PinError> where - F: Fn(&StateDbSync) -> bool, + F: Fn() -> bool, { self.db.write().pin(hash, number, hint) } @@ -662,16 +663,6 @@ pub enum IsPruned { MaybePruned, } -impl From for IsPruned { - fn from(pruned: bool) -> Self { - if pruned { - IsPruned::Pruned - } else { - IsPruned::NotPruned - } - } -} - fn fetch_stored_pruning_mode(db: &D) -> Result, Error> { let meta_key_mode = to_meta_key(PRUNING_MODE, &()); if let Some(stored_mode) = db.get_meta(&meta_key_mode).map_err(Error::Db)? { @@ -713,7 +704,6 @@ mod tests { }; use codec::Encode; use sp_core::H256; - use std::io; fn make_test_db(settings: PruningMode) -> (TestDb, StateDb) { let mut db = make_db(&[91, 921, 922, 93, 94]); @@ -727,12 +717,12 @@ mod tests { db.commit(&commit); let (state_db_init, state_db) = - StateDb::open(&mut db, Some(settings), false, true).unwrap(); + StateDb::open(db.clone(), Some(settings), false, true).unwrap(); db.commit(&state_db_init); db.commit( &state_db - .insert_block::( + .insert_block( &H256::from_low_u64_be(1), 1, &H256::from_low_u64_be(0), @@ -742,7 +732,7 @@ mod tests { ); db.commit( &state_db - .insert_block::( + .insert_block( &H256::from_low_u64_be(21), 2, &H256::from_low_u64_be(1), @@ -752,7 +742,7 @@ mod tests { ); db.commit( &state_db - .insert_block::( + .insert_block( &H256::from_low_u64_be(22), 2, &H256::from_low_u64_be(1), @@ -762,7 +752,7 @@ mod tests { ); db.commit( &state_db - .insert_block::( + .insert_block( &H256::from_low_u64_be(3), 3, &H256::from_low_u64_be(21), @@ -775,7 +765,7 @@ mod tests { state_db.apply_pending(); db.commit( &state_db - .insert_block::( + .insert_block( &H256::from_low_u64_be(4), 4, &H256::from_low_u64_be(3), @@ -844,11 +834,11 @@ mod tests { fn detects_incompatible_mode() { let mut db = make_db(&[]); let (state_db_init, state_db) = - StateDb::open(&mut db, Some(PruningMode::ArchiveAll), false, true).unwrap(); + StateDb::open(db.clone(), Some(PruningMode::ArchiveAll), false, true).unwrap(); db.commit(&state_db_init); db.commit( &state_db - .insert_block::( + .insert_block( &H256::from_low_u64_be(0), 0, &H256::from_low_u64_be(0), @@ -858,7 +848,7 @@ mod tests { ); let new_mode = PruningMode::Constrained(Constraints { max_blocks: Some(2), max_mem: None }); let state_db_open_result: Result<(_, StateDb), _> = - StateDb::open(&mut db, Some(new_mode), false, false); + StateDb::open(db.clone(), Some(new_mode), false, false); assert!(state_db_open_result.is_err()); } @@ -869,12 +859,13 @@ mod tests { ) { let mut db = make_db(&[]); let (state_db_init, state_db) = - StateDb::::open(&mut db, mode_when_created, false, true).unwrap(); + StateDb::::open(db.clone(), mode_when_created, false, true) + .unwrap(); db.commit(&state_db_init); std::mem::drop(state_db); let state_db_reopen_result = - StateDb::::open(&mut db, mode_when_reopened, false, false); + StateDb::::open(db.clone(), mode_when_reopened, false, false); if let Ok(expected_mode) = expected_effective_mode_when_reopenned { let (state_db_init, state_db_reopened) = state_db_reopen_result.unwrap(); db.commit(&state_db_init); diff --git a/client/state-db/src/noncanonical.rs b/client/state-db/src/noncanonical.rs index 664811f7fe4ab..a3808a1f8d3be 100644 --- a/client/state-db/src/noncanonical.rs +++ b/client/state-db/src/noncanonical.rs @@ -28,7 +28,7 @@ use log::trace; use std::collections::{hash_map::Entry, HashMap, VecDeque}; const NON_CANONICAL_JOURNAL: &[u8] = b"noncanonical_journal"; -pub const LAST_CANONICAL: &[u8] = b"last_canonical"; +pub(crate) const LAST_CANONICAL: &[u8] = b"last_canonical"; const MAX_BLOCKS_PER_LEVEL: u64 = 32; /// See module documentation. @@ -755,7 +755,7 @@ mod tests { .unwrap(), ); db.commit(&overlay.insert(&h2, 11, &h1, make_changeset(&[5], &[3])).unwrap()); - assert_eq!(db.meta.len(), 3); + assert_eq!(db.meta_len(), 3); let overlay2 = NonCanonicalOverlay::::new(&db).unwrap(); assert_eq!(overlay.levels, overlay2.levels); diff --git a/client/state-db/src/pruning.rs b/client/state-db/src/pruning.rs index 9f226c1c57e3f..327823cc30a9b 100644 --- a/client/state-db/src/pruning.rs +++ b/client/state-db/src/pruning.rs @@ -24,7 +24,9 @@ //! the death list. //! The changes are journaled in the DB. -use crate::{noncanonical::LAST_CANONICAL, to_meta_key, CommitSet, Error, Hash, MetaDb}; +use crate::{ + noncanonical::LAST_CANONICAL, to_meta_key, CommitSet, Error, Hash, MetaDb, MAX_BLOCK_CONSTRAINT, +}; use codec::{Decode, Encode}; use log::{trace, warn}; use std::{ @@ -32,10 +34,10 @@ use std::{ collections::{HashMap, HashSet, VecDeque}, }; -pub const LAST_PRUNED: &[u8] = b"last_pruned"; +pub(crate) const LAST_PRUNED: &[u8] = b"last_pruned"; const PRUNING_JOURNAL: &[u8] = b"pruning_journal"; // Default pruning window size plus a magic number keep most common ops in cache -const CACHE_BATCH_SIZE: usize = 256 + 10; +const CACHE_BATCH_SIZE: usize = MAX_BLOCK_CONSTRAINT as usize + 10; /// See module documentation. #[derive(parity_util_mem_derive::MallocSizeOf)] @@ -68,6 +70,7 @@ enum DeathRowQueue { }, DbBacked { // The backend database + #[ignore_malloc_size_of = "Shared data"] db: D, /// A queue of keys that should be deleted for each block in the pruning window. /// Only caching the first fews blocks of the pruning window, blocks inside are @@ -322,14 +325,6 @@ impl DeathRowQueue { DeathRowQueue::Mem { .. } => None, } } - - #[cfg(test)] - fn get_db(&mut self) -> Option<&mut D> { - match self { - DeathRowQueue::DbBacked { db, .. } => Some(db), - DeathRowQueue::Mem { .. } => None, - } - } } fn load_death_row_from_db( @@ -386,7 +381,7 @@ impl From for HaveBlock { impl RefWindow { pub fn new( - db: &D, + db: D, count_insertions: bool, ) -> Result, Error> { // the block number of the first block in the queue or the next block number if the queue is @@ -403,7 +398,7 @@ impl RefWindow { }; let queue = if count_insertions { - DeathRowQueue::new_mem(db, pending_number)? + DeathRowQueue::new_mem(&db, pending_number)? } else { let unload = match last_canonicalized_number { Some(last_canonicalized_number) => { @@ -415,7 +410,7 @@ impl RefWindow { // ever been committed to the db, thus set `unload` to zero None => 0, }; - DeathRowQueue::new_db_backed(db.clone(), pending_number, unload as usize)? + DeathRowQueue::new_db_backed(db, pending_number, unload as usize)? }; Ok(RefWindow { queue, pending_number, pending_canonicalizations: 0, pending_prunings: 0 }) @@ -531,7 +526,8 @@ mod tests { fn check_journal(pruning: &RefWindow, db: &TestDb) { let count_insertions = matches!(pruning.queue, DeathRowQueue::Mem { .. }); - let restored: RefWindow = RefWindow::new(db, count_insertions).unwrap(); + let restored: RefWindow = + RefWindow::new(db.clone(), count_insertions).unwrap(); assert_eq!(pruning.pending_number, restored.pending_number); assert_eq!(pruning.queue.get_mem_queue_state(), restored.queue.get_mem_queue_state()); } @@ -539,7 +535,7 @@ mod tests { #[test] fn created_from_empty_db() { let db = make_db(&[]); - let pruning: RefWindow = RefWindow::new(&db, true).unwrap(); + let pruning: RefWindow = RefWindow::new(db, true).unwrap(); assert_eq!(pruning.pending_number, 0); let (death_rows, death_index) = pruning.queue.get_mem_queue_state().unwrap(); assert!(death_rows.is_empty()); @@ -549,7 +545,7 @@ mod tests { #[test] fn prune_empty() { let db = make_db(&[]); - let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); + let mut pruning: RefWindow = RefWindow::new(db, true).unwrap(); let mut commit = CommitSet::default(); pruning.prune_one(&mut commit).unwrap(); assert_eq!(pruning.pending_number, 0); @@ -563,7 +559,7 @@ mod tests { #[test] fn prune_one() { let mut db = make_db(&[1, 2, 3]); - let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); + let mut pruning: RefWindow = RefWindow::new(db.clone(), true).unwrap(); let mut commit = make_commit(&[4, 5], &[1, 3]); let hash = H256::random(); pruning.note_canonical(&hash, &mut commit); @@ -594,7 +590,7 @@ mod tests { #[test] fn prune_two() { let mut db = make_db(&[1, 2, 3]); - let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); + let mut pruning: RefWindow = RefWindow::new(db.clone(), true).unwrap(); let mut commit = make_commit(&[4], &[1]); pruning.note_canonical(&H256::random(), &mut commit); db.commit(&commit); @@ -622,7 +618,7 @@ mod tests { #[test] fn prune_two_pending() { let mut db = make_db(&[1, 2, 3]); - let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); + let mut pruning: RefWindow = RefWindow::new(db.clone(), true).unwrap(); let mut commit = make_commit(&[4], &[1]); pruning.note_canonical(&H256::random(), &mut commit); db.commit(&commit); @@ -645,7 +641,7 @@ mod tests { #[test] fn reinserted_survives() { let mut db = make_db(&[1, 2, 3]); - let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); + let mut pruning: RefWindow = RefWindow::new(db.clone(), true).unwrap(); let mut commit = make_commit(&[], &[2]); pruning.note_canonical(&H256::random(), &mut commit); db.commit(&commit); @@ -678,7 +674,7 @@ mod tests { #[test] fn reinserted_survive_pending() { let mut db = make_db(&[1, 2, 3]); - let mut pruning: RefWindow = RefWindow::new(&db, true).unwrap(); + let mut pruning: RefWindow = RefWindow::new(db.clone(), true).unwrap(); let mut commit = make_commit(&[], &[2]); pruning.note_canonical(&H256::random(), &mut commit); db.commit(&commit); @@ -708,7 +704,7 @@ mod tests { #[test] fn reinserted_ignores() { let mut db = make_db(&[1, 2, 3]); - let mut pruning: RefWindow = RefWindow::new(&db, false).unwrap(); + let mut pruning: RefWindow = RefWindow::new(db.clone(), false).unwrap(); let mut commit = make_commit(&[], &[2]); pruning.note_canonical(&H256::random(), &mut commit); db.commit(&commit); @@ -745,7 +741,7 @@ mod tests { let mut db = make_db(&[]); let mut commit = CommitSet::default(); - fn load_pruning_from_db(db: &TestDb) -> (usize, u64) { + fn load_pruning_from_db(db: TestDb) -> (usize, u64) { let pruning: RefWindow = RefWindow::new(db, false).unwrap(); let (cache, _) = pruning.queue.get_db_backed_queue_state().unwrap(); (cache.len(), pruning.pending_number) @@ -759,7 +755,7 @@ mod tests { } // empty database - let (loaded_blocks, pending_number) = load_pruning_from_db(&db); + let (loaded_blocks, pending_number) = load_pruning_from_db(db.clone()); assert_eq!(loaded_blocks, 0); assert_eq!(pending_number, 0); @@ -767,14 +763,14 @@ mod tests { push_last_canonicalized(0, &mut commit); push_record(0, &mut commit); db.commit(&commit); - let (loaded_blocks, pending_number) = load_pruning_from_db(&db); + let (loaded_blocks, pending_number) = load_pruning_from_db(db.clone()); assert_eq!(loaded_blocks, 1); assert_eq!(pending_number, 0); // pruned the genesis block push_last_pruned(0, &mut commit); db.commit(&commit); - let (loaded_blocks, pending_number) = load_pruning_from_db(&db); + let (loaded_blocks, pending_number) = load_pruning_from_db(db.clone()); assert_eq!(loaded_blocks, 0); assert_eq!(pending_number, 1); @@ -784,22 +780,22 @@ mod tests { push_record(i, &mut commit); } db.commit(&commit); - let (loaded_blocks, pending_number) = load_pruning_from_db(&db); + let (loaded_blocks, pending_number) = load_pruning_from_db(db.clone()); assert_eq!(loaded_blocks, 10); assert_eq!(pending_number, 1); // pruned all blocks push_last_pruned(10, &mut commit); db.commit(&commit); - let (loaded_blocks, pending_number) = load_pruning_from_db(&db); + let (loaded_blocks, pending_number) = load_pruning_from_db(db.clone()); assert_eq!(loaded_blocks, 0); assert_eq!(pending_number, 11); } #[test] fn db_backed_queue() { - let mut pruning: RefWindow = - RefWindow::new(&make_db(&[]), false).unwrap(); + let mut db = make_db(&[]); + let mut pruning: RefWindow = RefWindow::new(db.clone(), false).unwrap(); // start as an empty queue let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); @@ -812,7 +808,7 @@ mod tests { let mut commit = make_commit(&[], &[]); pruning.note_canonical(&(i as u64), &mut commit); push_last_canonicalized(i as u64, &mut commit); - pruning.queue.get_db().unwrap().commit(&commit); + db.commit(&commit); // block will fill in cache first let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); if i < CACHE_BATCH_SIZE { @@ -853,7 +849,7 @@ mod tests { // block is removed from the head of cache let mut commit = CommitSet::default(); pruning.prune_one(&mut commit).unwrap(); - pruning.queue.get_db().unwrap().commit(&commit); + db.commit(&commit); pruning.apply_pending(); assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 9); let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); @@ -865,8 +861,7 @@ mod tests { // load a new queue from db // `cache` is full again but the content of the queue should be the same - let pruning: RefWindow = - RefWindow::new(pruning.queue.get_db().unwrap(), false).unwrap(); + let pruning: RefWindow = RefWindow::new(db, false).unwrap(); assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 9); let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), CACHE_BATCH_SIZE); @@ -878,15 +873,15 @@ mod tests { #[test] fn load_block_from_db() { - let mut pruning: RefWindow = - RefWindow::new(&make_db(&[]), false).unwrap(); + let mut db = make_db(&[]); + let mut pruning: RefWindow = RefWindow::new(db.clone(), false).unwrap(); // import blocks for i in 0..(CACHE_BATCH_SIZE as u64 * 2 + 10) { let mut commit = make_commit(&[], &[]); pruning.note_canonical(&i, &mut commit); push_last_canonicalized(i as u64, &mut commit); - pruning.queue.get_db().unwrap().commit(&commit); + db.commit(&commit); } // the following operations won't triger loading block from db: @@ -912,7 +907,7 @@ mod tests { for _ in 0..CACHE_BATCH_SIZE * 2 { let mut commit = CommitSet::default(); pruning.prune_one(&mut commit).unwrap(); - pruning.queue.get_db().unwrap().commit(&commit); + db.commit(&commit); } pruning.apply_pending(); let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); @@ -931,8 +926,7 @@ mod tests { // load a new queue from db // `cache` should be the same - let pruning: RefWindow = - RefWindow::new(pruning.queue.get_db().unwrap(), false).unwrap(); + let pruning: RefWindow = RefWindow::new(db, false).unwrap(); assert_eq!(pruning.queue.len(), 10); let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), 10); diff --git a/client/state-db/src/test.rs b/client/state-db/src/test.rs index 9fb97036b2f24..314ec2902452a 100644 --- a/client/state-db/src/test.rs +++ b/client/state-db/src/test.rs @@ -20,10 +20,16 @@ use crate::{ChangeSet, CommitSet, DBValue, MetaDb, NodeDb}; use sp_core::H256; -use std::collections::HashMap; +use std::{ + collections::HashMap, + sync::{Arc, RwLock}, +}; + +#[derive(Default, Debug, Clone)] +pub struct TestDb(Arc>); #[derive(Default, Debug, Clone, PartialEq, Eq)] -pub struct TestDb { +struct TestDbInner { pub data: HashMap, pub meta: HashMap, DBValue>, } @@ -32,7 +38,7 @@ impl MetaDb for TestDb { type Error = (); fn get_meta(&self, key: &[u8]) -> Result, ()> { - Ok(self.meta.get(key).cloned()) + Ok(self.0.read().unwrap().meta.get(key).cloned()) } } @@ -41,25 +47,29 @@ impl NodeDb for TestDb { type Key = H256; fn get(&self, key: &H256) -> Result, ()> { - Ok(self.data.get(key).cloned()) + Ok(self.0.read().unwrap().data.get(key).cloned()) } } impl TestDb { pub fn commit(&mut self, commit: &CommitSet) { - self.data.extend(commit.data.inserted.iter().cloned()); - self.meta.extend(commit.meta.inserted.iter().cloned()); + self.0.write().unwrap().data.extend(commit.data.inserted.iter().cloned()); + self.0.write().unwrap().meta.extend(commit.meta.inserted.iter().cloned()); for k in commit.data.deleted.iter() { - self.data.remove(k); + self.0.write().unwrap().data.remove(k); } - self.meta.extend(commit.meta.inserted.iter().cloned()); + self.0.write().unwrap().meta.extend(commit.meta.inserted.iter().cloned()); for k in commit.meta.deleted.iter() { - self.meta.remove(k); + self.0.write().unwrap().meta.remove(k); } } pub fn data_eq(&self, other: &TestDb) -> bool { - self.data == other.data + self.0.read().unwrap().data == other.0.read().unwrap().data + } + + pub fn meta_len(&self) -> usize { + self.0.read().unwrap().meta.len() } } @@ -78,11 +88,11 @@ pub fn make_commit(inserted: &[u64], deleted: &[u64]) -> CommitSet { } pub fn make_db(inserted: &[u64]) -> TestDb { - TestDb { + TestDb(Arc::new(RwLock::new(TestDbInner { data: inserted .iter() .map(|v| (H256::from_low_u64_be(*v), H256::from_low_u64_be(*v).as_bytes().to_vec())) .collect(), meta: Default::default(), - } + }))) } From 16969d1357825085c7ec2add08e29b410a912971 Mon Sep 17 00:00:00 2001 From: linning Date: Sun, 14 Aug 2022 19:12:11 +0800 Subject: [PATCH 13/19] fix syncs_state test Signed-off-by: linning --- client/state-db/src/lib.rs | 13 +++---- client/state-db/src/noncanonical.rs | 7 ++-- client/state-db/src/pruning.rs | 60 +++++++++++++++++++---------- 3 files changed, 48 insertions(+), 32 deletions(-) diff --git a/client/state-db/src/lib.rs b/client/state-db/src/lib.rs index 01840ec10c82c..0d1e15021cdf9 100644 --- a/client/state-db/src/lib.rs +++ b/client/state-db/src/lib.rs @@ -333,15 +333,12 @@ impl if self.mode == PruningMode::ArchiveAll { return Ok(commit) } - match self.non_canonical.canonicalize(hash, &mut commit) { - Ok(()) => - if self.mode == PruningMode::ArchiveCanonical { - commit.data.deleted.clear(); - }, - Err(e) => return Err(e.into()), - }; + let number = self.non_canonical.canonicalize(hash, &mut commit)?; + if self.mode == PruningMode::ArchiveCanonical { + commit.data.deleted.clear(); + } if let Some(ref mut pruning) = self.pruning { - pruning.note_canonical(hash, &mut commit); + pruning.note_canonical(hash, number, &mut commit)?; } self.prune(&mut commit)?; Ok(commit) diff --git a/client/state-db/src/noncanonical.rs b/client/state-db/src/noncanonical.rs index a3808a1f8d3be..559fc7ca023fe 100644 --- a/client/state-db/src/noncanonical.rs +++ b/client/state-db/src/noncanonical.rs @@ -376,12 +376,13 @@ impl NonCanonicalOverlay { } /// Select a top-level root and canonicalized it. Discards all sibling subtrees and the root. - /// Returns a set of changes that need to be added to the DB. + /// Add a set of changes of the canonicalized block to `CommitSet` + /// Return the block number of the canonicalized block pub fn canonicalize( &mut self, hash: &BlockHash, commit: &mut CommitSet, - ) -> Result<(), StateDbError> { + ) -> Result { trace!(target: "state-db", "Canonicalizing {:?}", hash); let level = self .levels @@ -431,7 +432,7 @@ impl NonCanonicalOverlay { .push((to_meta_key(LAST_CANONICAL, &()), canonicalized.encode())); trace!(target: "state-db", "Discarding {} records", commit.meta.deleted.len()); self.pending_canonicalizations.push(hash.clone()); - Ok(()) + Ok(canonicalized.1) } fn apply_canonicalizations(&mut self) { diff --git a/client/state-db/src/pruning.rs b/client/state-db/src/pruning.rs index 327823cc30a9b..71b334ba2932c 100644 --- a/client/state-db/src/pruning.rs +++ b/client/state-db/src/pruning.rs @@ -25,7 +25,8 @@ //! The changes are journaled in the DB. use crate::{ - noncanonical::LAST_CANONICAL, to_meta_key, CommitSet, Error, Hash, MetaDb, MAX_BLOCK_CONSTRAINT, + noncanonical::LAST_CANONICAL, to_meta_key, CommitSet, Error, Hash, MetaDb, StateDbError, + MAX_BLOCK_CONSTRAINT, }; use codec::{Decode, Encode}; use log::{trace, warn}; @@ -468,7 +469,18 @@ impl RefWindow { } /// Add a change set to the window. Creates a journal record and pushes it to `commit` - pub fn note_canonical(&mut self, hash: &BlockHash, commit: &mut CommitSet) { + pub fn note_canonical( + &mut self, + hash: &BlockHash, + number: u64, + commit: &mut CommitSet, + ) -> Result<(), Error> { + if self.pending_number == 0 && self.queue.len() == 0 && number > 0 { + // assume that parent was canonicalized + self.pending_number = number; + } else if (self.pending_number + self.queue.len() as u64) != number { + return Err(Error::StateDb(StateDbError::InvalidBlockNumber)) + } trace!(target: "state-db", "Adding to pruning window: {:?} ({} inserted, {} deleted)", hash, commit.data.inserted.len(), commit.data.deleted.len()); let inserted = if matches!(self.queue, DeathRowQueue::Mem { .. }) { commit.data.inserted.iter().map(|(k, _)| k.clone()).collect() @@ -477,10 +489,10 @@ impl RefWindow { }; let deleted = ::std::mem::take(&mut commit.data.deleted); let journal_record = JournalRecord { hash: hash.clone(), inserted, deleted }; - let block = self.pending_number + self.queue.len() as u64; - commit.meta.inserted.push((to_journal_key(block), journal_record.encode())); + commit.meta.inserted.push((to_journal_key(number), journal_record.encode())); self.queue.import(self.pending_number, journal_record); self.pending_canonicalizations += 1; + Ok(()) } /// Apply all pending changes @@ -562,7 +574,7 @@ mod tests { let mut pruning: RefWindow = RefWindow::new(db.clone(), true).unwrap(); let mut commit = make_commit(&[4, 5], &[1, 3]); let hash = H256::random(); - pruning.note_canonical(&hash, &mut commit); + pruning.note_canonical(&hash, 0, &mut commit).unwrap(); db.commit(&commit); assert_eq!(pruning.have_block(&hash, 0), HaveBlock::Have); pruning.apply_pending(); @@ -592,10 +604,10 @@ mod tests { let mut db = make_db(&[1, 2, 3]); let mut pruning: RefWindow = RefWindow::new(db.clone(), true).unwrap(); let mut commit = make_commit(&[4], &[1]); - pruning.note_canonical(&H256::random(), &mut commit); + pruning.note_canonical(&H256::random(), 0, &mut commit).unwrap(); db.commit(&commit); let mut commit = make_commit(&[5], &[2]); - pruning.note_canonical(&H256::random(), &mut commit); + pruning.note_canonical(&H256::random(), 1, &mut commit).unwrap(); db.commit(&commit); pruning.apply_pending(); assert!(db.data_eq(&make_db(&[1, 2, 3, 4, 5]))); @@ -620,10 +632,10 @@ mod tests { let mut db = make_db(&[1, 2, 3]); let mut pruning: RefWindow = RefWindow::new(db.clone(), true).unwrap(); let mut commit = make_commit(&[4], &[1]); - pruning.note_canonical(&H256::random(), &mut commit); + pruning.note_canonical(&H256::random(), 0, &mut commit).unwrap(); db.commit(&commit); let mut commit = make_commit(&[5], &[2]); - pruning.note_canonical(&H256::random(), &mut commit); + pruning.note_canonical(&H256::random(), 1, &mut commit).unwrap(); db.commit(&commit); assert!(db.data_eq(&make_db(&[1, 2, 3, 4, 5]))); let mut commit = CommitSet::default(); @@ -643,13 +655,13 @@ mod tests { let mut db = make_db(&[1, 2, 3]); let mut pruning: RefWindow = RefWindow::new(db.clone(), true).unwrap(); let mut commit = make_commit(&[], &[2]); - pruning.note_canonical(&H256::random(), &mut commit); + pruning.note_canonical(&H256::random(), 0, &mut commit).unwrap(); db.commit(&commit); let mut commit = make_commit(&[2], &[]); - pruning.note_canonical(&H256::random(), &mut commit); + pruning.note_canonical(&H256::random(), 1, &mut commit).unwrap(); db.commit(&commit); let mut commit = make_commit(&[], &[2]); - pruning.note_canonical(&H256::random(), &mut commit); + pruning.note_canonical(&H256::random(), 2, &mut commit).unwrap(); db.commit(&commit); assert!(db.data_eq(&make_db(&[1, 2, 3]))); pruning.apply_pending(); @@ -676,13 +688,13 @@ mod tests { let mut db = make_db(&[1, 2, 3]); let mut pruning: RefWindow = RefWindow::new(db.clone(), true).unwrap(); let mut commit = make_commit(&[], &[2]); - pruning.note_canonical(&H256::random(), &mut commit); + pruning.note_canonical(&H256::random(), 0, &mut commit).unwrap(); db.commit(&commit); let mut commit = make_commit(&[2], &[]); - pruning.note_canonical(&H256::random(), &mut commit); + pruning.note_canonical(&H256::random(), 1, &mut commit).unwrap(); db.commit(&commit); let mut commit = make_commit(&[], &[2]); - pruning.note_canonical(&H256::random(), &mut commit); + pruning.note_canonical(&H256::random(), 2, &mut commit).unwrap(); db.commit(&commit); assert!(db.data_eq(&make_db(&[1, 2, 3]))); @@ -706,13 +718,13 @@ mod tests { let mut db = make_db(&[1, 2, 3]); let mut pruning: RefWindow = RefWindow::new(db.clone(), false).unwrap(); let mut commit = make_commit(&[], &[2]); - pruning.note_canonical(&H256::random(), &mut commit); + pruning.note_canonical(&H256::random(), 0, &mut commit).unwrap(); db.commit(&commit); let mut commit = make_commit(&[2], &[]); - pruning.note_canonical(&H256::random(), &mut commit); + pruning.note_canonical(&H256::random(), 1, &mut commit).unwrap(); db.commit(&commit); let mut commit = make_commit(&[], &[2]); - pruning.note_canonical(&H256::random(), &mut commit); + pruning.note_canonical(&H256::random(), 2, &mut commit).unwrap(); db.commit(&commit); assert!(db.data_eq(&make_db(&[1, 2, 3]))); pruning.apply_pending(); @@ -806,7 +818,7 @@ mod tests { // queue size and content should match for i in 0..(CACHE_BATCH_SIZE + 10) { let mut commit = make_commit(&[], &[]); - pruning.note_canonical(&(i as u64), &mut commit); + pruning.note_canonical(&(i as u64), i as u64, &mut commit).unwrap(); push_last_canonicalized(i as u64, &mut commit); db.commit(&commit); // block will fill in cache first @@ -831,7 +843,13 @@ mod tests { // import a new block to the end of the queue // won't keep the new block in memory let mut commit = CommitSet::default(); - pruning.note_canonical(&(CACHE_BATCH_SIZE as u64 + 10), &mut commit); + pruning + .note_canonical( + &(CACHE_BATCH_SIZE as u64 + 10), + CACHE_BATCH_SIZE as u64 + 10, + &mut commit, + ) + .unwrap(); assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 11); let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), CACHE_BATCH_SIZE); @@ -879,7 +897,7 @@ mod tests { // import blocks for i in 0..(CACHE_BATCH_SIZE as u64 * 2 + 10) { let mut commit = make_commit(&[], &[]); - pruning.note_canonical(&i, &mut commit); + pruning.note_canonical(&i, i, &mut commit).unwrap(); push_last_canonicalized(i as u64, &mut commit); db.commit(&commit); } From 7c886f729b6b3259498174517c49288c164187d0 Mon Sep 17 00:00:00 2001 From: linning Date: Mon, 15 Aug 2022 01:49:05 +0800 Subject: [PATCH 14/19] address comment Signed-off-by: linning --- client/state-db/src/lib.rs | 10 +- client/state-db/src/pruning.rs | 247 +++++++++++++++++++-------------- 2 files changed, 145 insertions(+), 112 deletions(-) diff --git a/client/state-db/src/lib.rs b/client/state-db/src/lib.rs index 0d1e15021cdf9..989e3ea5b8e2f 100644 --- a/client/state-db/src/lib.rs +++ b/client/state-db/src/lib.rs @@ -62,7 +62,7 @@ const PRUNING_MODE: &[u8] = b"mode"; const PRUNING_MODE_ARCHIVE: &[u8] = b"archive"; const PRUNING_MODE_ARCHIVE_CANON: &[u8] = b"archive_canonical"; const PRUNING_MODE_CONSTRAINED: &[u8] = b"constrained"; -pub(crate) const MAX_BLOCK_CONSTRAINT: u32 = 256; +pub(crate) const DEFAULT_MAX_BLOCK_CONSTRAINT: u32 = 256; /// Database value type. pub type DBValue = Vec; @@ -267,7 +267,7 @@ impl Default for PruningMode { impl Default for Constraints { fn default() -> Self { - Self { max_blocks: Some(MAX_BLOCK_CONSTRAINT), max_mem: None } + Self { max_blocks: Some(DEFAULT_MAX_BLOCK_CONSTRAINT), max_mem: None } } } @@ -297,7 +297,8 @@ impl let non_canonical: NonCanonicalOverlay = NonCanonicalOverlay::new(&db)?; let pruning: Option> = match mode { PruningMode::Constrained(Constraints { max_mem: Some(_), .. }) => unimplemented!(), - PruningMode::Constrained(_) => Some(RefWindow::new(db, ref_counting)?), + PruningMode::Constrained(Constraints { max_blocks, .. }) => + Some(RefWindow::new(db, max_blocks.unwrap_or(0), ref_counting)?), PruningMode::ArchiveAll | PruningMode::ArchiveCanonical => None, }; @@ -481,10 +482,11 @@ impl if let Some(pruning) = &mut self.pruning { pruning.apply_pending(); } + let next_hash = self.pruning.as_mut().map(|p| p.next_hash()); trace!( target: "forks", "First available: {:?} ({}), Last canon: {:?} ({}), Best forks: {:?}", - self.pruning.as_mut().map(|p| p.next_hash()), + next_hash, self.pruning.as_ref().map(|p| p.pending()).unwrap_or(0), self.non_canonical.last_canonicalized_hash(), self.non_canonical.last_canonicalized_block_number().unwrap_or(0), diff --git a/client/state-db/src/pruning.rs b/client/state-db/src/pruning.rs index 71b334ba2932c..44755ba677de6 100644 --- a/client/state-db/src/pruning.rs +++ b/client/state-db/src/pruning.rs @@ -26,7 +26,7 @@ use crate::{ noncanonical::LAST_CANONICAL, to_meta_key, CommitSet, Error, Hash, MetaDb, StateDbError, - MAX_BLOCK_CONSTRAINT, + DEFAULT_MAX_BLOCK_CONSTRAINT, }; use codec::{Decode, Encode}; use log::{trace, warn}; @@ -37,8 +37,6 @@ use std::{ pub(crate) const LAST_PRUNED: &[u8] = b"last_pruned"; const PRUNING_JOURNAL: &[u8] = b"pruning_journal"; -// Default pruning window size plus a magic number keep most common ops in cache -const CACHE_BATCH_SIZE: usize = MAX_BLOCK_CONSTRAINT as usize + 10; /// See module documentation. #[derive(parity_util_mem_derive::MallocSizeOf)] @@ -47,7 +45,7 @@ pub struct RefWindow { /// pruning window. queue: DeathRowQueue, /// Block number that corresponds to the front of `death_rows`. - pending_number: u64, + base: u64, /// Number of call of `note_canonical` after /// last call `apply_pending` or `revert_pending` pending_canonicalizations: usize, @@ -77,6 +75,8 @@ enum DeathRowQueue { /// Only caching the first fews blocks of the pruning window, blocks inside are /// successive and ordered by block number cache: VecDeque>, + /// A soft limit of the cache's size + cache_capacity: usize, /// The number of blocks that not loaded into `cache` uncached_blocks: usize, }, @@ -114,22 +114,32 @@ impl DeathRowQueue { db: D, base: u64, mut uncached_blocks: usize, + window_size: u32, ) -> Result, Error> { - let mut cache = VecDeque::with_capacity(CACHE_BATCH_SIZE); + // The pruning window size (if not larger than `DEFAULT_MAX_BLOCK_CONSTRAINT`) plus a magic + // number keep most common ops in cache + let cache_capacity = window_size.min(DEFAULT_MAX_BLOCK_CONSTRAINT) as usize + 10; + let mut cache = VecDeque::with_capacity(cache_capacity); trace!(target: "state-db", "Reading pruning journal for the database-backed queue. Pending #{}", base); // Load block from db - DeathRowQueue::load_batch_from_db(&db, &mut uncached_blocks, &mut cache, base)?; - Ok(DeathRowQueue::DbBacked { db, cache, uncached_blocks }) + DeathRowQueue::load_batch_from_db( + &db, + &mut uncached_blocks, + &mut cache, + base, + cache_capacity, + )?; + Ok(DeathRowQueue::DbBacked { db, cache, cache_capacity, uncached_blocks }) } /// import a new block to the back of the queue fn import(&mut self, base: u64, journal_record: JournalRecord) { let JournalRecord { hash, inserted, deleted } = journal_record; match self { - DeathRowQueue::DbBacked { uncached_blocks, cache, .. } => { + DeathRowQueue::DbBacked { uncached_blocks, cache, cache_capacity, .. } => { // `uncached_blocks` is zero means currently all block are loaded into `cache` // thus if `cache` is not full, load the next block into `cache` too - if *uncached_blocks == 0 && cache.len() < CACHE_BATCH_SIZE { + if *uncached_blocks == 0 && cache.len() < *cache_capacity { cache.push_back(DeathRow { hash, deleted: deleted.into_iter().collect() }); } else { *uncached_blocks += 1; @@ -159,10 +169,16 @@ impl DeathRowQueue { base: u64, ) -> Result>, Error> { match self { - DeathRowQueue::DbBacked { db, uncached_blocks, cache } => { + DeathRowQueue::DbBacked { db, uncached_blocks, cache, cache_capacity } => { if cache.is_empty() && *uncached_blocks != 0 { // load more blocks from db since there are still blocks in it - DeathRowQueue::load_batch_from_db(db, uncached_blocks, cache, base)?; + DeathRowQueue::load_batch_from_db( + db, + uncached_blocks, + cache, + base, + *cache_capacity, + )?; } Ok(cache.pop_front()) }, @@ -215,6 +231,7 @@ impl DeathRowQueue { uncached_blocks: &mut usize, cache: &mut VecDeque>, base: u64, + cache_capacity: usize, ) -> Result<(), Error> { // return if all blocks already loaded into `cache` and there are no other // blocks in the backend database @@ -222,7 +239,7 @@ impl DeathRowQueue { return Ok(()) } let start = base + cache.len() as u64; - let batch_size = cmp::min(*uncached_blocks, CACHE_BATCH_SIZE); + let batch_size = cmp::min(*uncached_blocks, cache_capacity); let mut loaded = 0; for i in 0..batch_size as u64 { match load_death_row_from_db::(db, start + i)? { @@ -248,12 +265,18 @@ impl DeathRowQueue { index: usize, ) -> Result>, Error> { match self { - DeathRowQueue::DbBacked { db, uncached_blocks, cache } => { + DeathRowQueue::DbBacked { db, uncached_blocks, cache, cache_capacity } => { // check if `index` target a block reside on disk if index >= cache.len() && index < cache.len() + *uncached_blocks { // if `index` target the next batch of `DeathRow`, load a batch from db - if index - cache.len() < cmp::min(*uncached_blocks, CACHE_BATCH_SIZE) { - DeathRowQueue::load_batch_from_db(db, uncached_blocks, cache, base)?; + if index - cache.len() < cmp::min(*uncached_blocks, *cache_capacity) { + DeathRowQueue::load_batch_from_db( + db, + uncached_blocks, + cache, + base, + *cache_capacity, + )?; } else { // load a single `DeathRow` from db, but do not insert it to `cache` // because `cache` is a queue of successive `DeathRow` @@ -383,11 +406,12 @@ impl From for HaveBlock { impl RefWindow { pub fn new( db: D, + window_size: u32, count_insertions: bool, ) -> Result, Error> { // the block number of the first block in the queue or the next block number if the queue is // empty - let pending_number = match db.get_meta(&to_meta_key(LAST_PRUNED, &())).map_err(Error::Db)? { + let base = match db.get_meta(&to_meta_key(LAST_PRUNED, &())).map_err(Error::Db)? { Some(buffer) => u64::decode(&mut buffer.as_slice())? + 1, None => 0, }; @@ -399,22 +423,22 @@ impl RefWindow { }; let queue = if count_insertions { - DeathRowQueue::new_mem(&db, pending_number)? + DeathRowQueue::new_mem(&db, base)? } else { let unload = match last_canonicalized_number { Some(last_canonicalized_number) => { - debug_assert!(last_canonicalized_number + 1 >= pending_number); - last_canonicalized_number + 1 - pending_number + debug_assert!(last_canonicalized_number + 1 >= base); + last_canonicalized_number + 1 - base }, // None means `LAST_CANONICAL` is never been wrote, since the pruning journals are // in the same `CommitSet` as `LAST_CANONICAL`, it means no pruning journal have // ever been committed to the db, thus set `unload` to zero None => 0, }; - DeathRowQueue::new_db_backed(db, pending_number, unload as usize)? + DeathRowQueue::new_db_backed(db, base, unload as usize, window_size)? }; - Ok(RefWindow { queue, pending_number, pending_canonicalizations: 0, pending_prunings: 0 }) + Ok(RefWindow { queue, base, pending_canonicalizations: 0, pending_prunings: 0 }) } pub fn window_size(&self) -> u64 { @@ -422,7 +446,7 @@ impl RefWindow { } pub fn next_hash(&mut self) -> Result, Error> { - self.queue.get_hash(self.pending_number, self.pending_prunings) + self.queue.get_hash(self.base, self.pending_prunings) } pub fn mem_used(&self) -> usize { @@ -431,7 +455,7 @@ impl RefWindow { // Return the block number of the first block that not been pending pruned pub fn pending(&self) -> u64 { - self.pending_number + self.pending_prunings as u64 + self.base + self.pending_prunings as u64 } fn is_empty(&self) -> bool { @@ -443,24 +467,24 @@ impl RefWindow { // if the queue is empty or the block number exceed the pruning window, we definitely // do not have this block if self.is_empty() || - !(number >= self.pending() && number < self.pending_number + self.queue.len() as u64) + !(number >= self.pending() && number < self.base + self.queue.len() as u64) { return HaveBlock::NotHave } - self.queue.have_block(hash, (number - self.pending_number) as usize) + self.queue.have_block(hash, (number - self.base) as usize) } /// Prune next block. Expects at least one block in the window. Adds changes to `commit`. pub fn prune_one(&mut self, commit: &mut CommitSet) -> Result<(), Error> { - if let Some(pruned) = self.queue.get(self.pending_number, self.pending_prunings)? { + if let Some(pruned) = self.queue.get(self.base, self.pending_prunings)? { trace!(target: "state-db", "Pruning {:?} ({} deleted)", pruned.hash, pruned.deleted.len()); - let index = self.pending_number + self.pending_prunings as u64; + let index = self.base + self.pending_prunings as u64; commit.data.deleted.extend(pruned.deleted.into_iter()); commit.meta.inserted.push((to_meta_key(LAST_PRUNED, &()), index.encode())); commit .meta .deleted - .push(to_journal_key(self.pending_number + self.pending_prunings as u64)); + .push(to_journal_key(self.base + self.pending_prunings as u64)); self.pending_prunings += 1; } else { warn!(target: "state-db", "Trying to prune when there's nothing to prune"); @@ -475,10 +499,10 @@ impl RefWindow { number: u64, commit: &mut CommitSet, ) -> Result<(), Error> { - if self.pending_number == 0 && self.queue.len() == 0 && number > 0 { + if self.base == 0 && self.queue.len() == 0 && number > 0 { // assume that parent was canonicalized - self.pending_number = number; - } else if (self.pending_number + self.queue.len() as u64) != number { + self.base = number; + } else if (self.base + self.queue.len() as u64) != number { return Err(Error::StateDb(StateDbError::InvalidBlockNumber)) } trace!(target: "state-db", "Adding to pruning window: {:?} ({} inserted, {} deleted)", hash, commit.data.inserted.len(), commit.data.deleted.len()); @@ -490,7 +514,7 @@ impl RefWindow { let deleted = ::std::mem::take(&mut commit.data.deleted); let journal_record = JournalRecord { hash: hash.clone(), inserted, deleted }; commit.meta.inserted.push((to_journal_key(number), journal_record.encode())); - self.queue.import(self.pending_number, journal_record); + self.queue.import(self.base, journal_record); self.pending_canonicalizations += 1; Ok(()) } @@ -501,22 +525,21 @@ impl RefWindow { for _ in 0..self.pending_prunings { let pruned = self .queue - .pop_front(self.pending_number) + .pop_front(self.base) // NOTE: `pop_front` should not return `MetaDb::Error` because blocks are visited // by `RefWindow::prune_one` first then `RefWindow::apply_pending` and // `DeathRowQueue::get` should load the blocks into cache already .expect("block must loaded in cache thus no MetaDb::Error") .expect("pending_prunings is always < queue.len()"); trace!(target: "state-db", "Applying pruning {:?} ({} deleted)", pruned.hash, pruned.deleted.len()); - self.pending_number += 1; + self.base += 1; } self.pending_prunings = 0; } /// Revert all pending changes pub fn revert_pending(&mut self) { - self.queue - .revert_recent_add(self.pending_number, self.pending_canonicalizations); + self.queue.revert_recent_add(self.base, self.pending_canonicalizations); self.pending_canonicalizations = 0; self.pending_prunings = 0; } @@ -524,14 +547,11 @@ impl RefWindow { #[cfg(test)] mod tests { - use super::{ - to_journal_key, DeathRowQueue, HaveBlock, JournalRecord, RefWindow, CACHE_BATCH_SIZE, - LAST_PRUNED, - }; + use super::{to_journal_key, DeathRowQueue, HaveBlock, JournalRecord, RefWindow, LAST_PRUNED}; use crate::{ noncanonical::LAST_CANONICAL, test::{make_commit, make_db, TestDb}, - to_meta_key, CommitSet, Hash, + to_meta_key, CommitSet, Hash, DEFAULT_MAX_BLOCK_CONSTRAINT, }; use codec::Encode; use sp_core::H256; @@ -539,16 +559,17 @@ mod tests { fn check_journal(pruning: &RefWindow, db: &TestDb) { let count_insertions = matches!(pruning.queue, DeathRowQueue::Mem { .. }); let restored: RefWindow = - RefWindow::new(db.clone(), count_insertions).unwrap(); - assert_eq!(pruning.pending_number, restored.pending_number); + RefWindow::new(db.clone(), DEFAULT_MAX_BLOCK_CONSTRAINT, count_insertions).unwrap(); + assert_eq!(pruning.base, restored.base); assert_eq!(pruning.queue.get_mem_queue_state(), restored.queue.get_mem_queue_state()); } #[test] fn created_from_empty_db() { let db = make_db(&[]); - let pruning: RefWindow = RefWindow::new(db, true).unwrap(); - assert_eq!(pruning.pending_number, 0); + let pruning: RefWindow = + RefWindow::new(db, DEFAULT_MAX_BLOCK_CONSTRAINT, true).unwrap(); + assert_eq!(pruning.base, 0); let (death_rows, death_index) = pruning.queue.get_mem_queue_state().unwrap(); assert!(death_rows.is_empty()); assert!(death_index.is_empty()); @@ -557,10 +578,11 @@ mod tests { #[test] fn prune_empty() { let db = make_db(&[]); - let mut pruning: RefWindow = RefWindow::new(db, true).unwrap(); + let mut pruning: RefWindow = + RefWindow::new(db, DEFAULT_MAX_BLOCK_CONSTRAINT, true).unwrap(); let mut commit = CommitSet::default(); pruning.prune_one(&mut commit).unwrap(); - assert_eq!(pruning.pending_number, 0); + assert_eq!(pruning.base, 0); let (death_rows, death_index) = pruning.queue.get_mem_queue_state().unwrap(); assert!(death_rows.is_empty()); assert!(death_index.is_empty()); @@ -571,7 +593,8 @@ mod tests { #[test] fn prune_one() { let mut db = make_db(&[1, 2, 3]); - let mut pruning: RefWindow = RefWindow::new(db.clone(), true).unwrap(); + let mut pruning: RefWindow = + RefWindow::new(db.clone(), DEFAULT_MAX_BLOCK_CONSTRAINT, true).unwrap(); let mut commit = make_commit(&[4, 5], &[1, 3]); let hash = H256::random(); pruning.note_canonical(&hash, 0, &mut commit).unwrap(); @@ -596,13 +619,14 @@ mod tests { let (death_rows, death_index) = pruning.queue.get_mem_queue_state().unwrap(); assert!(death_rows.is_empty()); assert!(death_index.is_empty()); - assert_eq!(pruning.pending_number, 1); + assert_eq!(pruning.base, 1); } #[test] fn prune_two() { let mut db = make_db(&[1, 2, 3]); - let mut pruning: RefWindow = RefWindow::new(db.clone(), true).unwrap(); + let mut pruning: RefWindow = + RefWindow::new(db.clone(), DEFAULT_MAX_BLOCK_CONSTRAINT, true).unwrap(); let mut commit = make_commit(&[4], &[1]); pruning.note_canonical(&H256::random(), 0, &mut commit).unwrap(); db.commit(&commit); @@ -624,13 +648,14 @@ mod tests { db.commit(&commit); pruning.apply_pending(); assert!(db.data_eq(&make_db(&[3, 4, 5]))); - assert_eq!(pruning.pending_number, 2); + assert_eq!(pruning.base, 2); } #[test] fn prune_two_pending() { let mut db = make_db(&[1, 2, 3]); - let mut pruning: RefWindow = RefWindow::new(db.clone(), true).unwrap(); + let mut pruning: RefWindow = + RefWindow::new(db.clone(), DEFAULT_MAX_BLOCK_CONSTRAINT, true).unwrap(); let mut commit = make_commit(&[4], &[1]); pruning.note_canonical(&H256::random(), 0, &mut commit).unwrap(); db.commit(&commit); @@ -647,13 +672,14 @@ mod tests { db.commit(&commit); pruning.apply_pending(); assert!(db.data_eq(&make_db(&[3, 4, 5]))); - assert_eq!(pruning.pending_number, 2); + assert_eq!(pruning.base, 2); } #[test] fn reinserted_survives() { let mut db = make_db(&[1, 2, 3]); - let mut pruning: RefWindow = RefWindow::new(db.clone(), true).unwrap(); + let mut pruning: RefWindow = + RefWindow::new(db.clone(), DEFAULT_MAX_BLOCK_CONSTRAINT, true).unwrap(); let mut commit = make_commit(&[], &[2]); pruning.note_canonical(&H256::random(), 0, &mut commit).unwrap(); db.commit(&commit); @@ -680,13 +706,14 @@ mod tests { db.commit(&commit); assert!(db.data_eq(&make_db(&[1, 3]))); pruning.apply_pending(); - assert_eq!(pruning.pending_number, 3); + assert_eq!(pruning.base, 3); } #[test] fn reinserted_survive_pending() { let mut db = make_db(&[1, 2, 3]); - let mut pruning: RefWindow = RefWindow::new(db.clone(), true).unwrap(); + let mut pruning: RefWindow = + RefWindow::new(db.clone(), DEFAULT_MAX_BLOCK_CONSTRAINT, true).unwrap(); let mut commit = make_commit(&[], &[2]); pruning.note_canonical(&H256::random(), 0, &mut commit).unwrap(); db.commit(&commit); @@ -710,13 +737,14 @@ mod tests { db.commit(&commit); assert!(db.data_eq(&make_db(&[1, 3]))); pruning.apply_pending(); - assert_eq!(pruning.pending_number, 3); + assert_eq!(pruning.base, 3); } #[test] fn reinserted_ignores() { let mut db = make_db(&[1, 2, 3]); - let mut pruning: RefWindow = RefWindow::new(db.clone(), false).unwrap(); + let mut pruning: RefWindow = + RefWindow::new(db.clone(), DEFAULT_MAX_BLOCK_CONSTRAINT, false).unwrap(); let mut commit = make_commit(&[], &[2]); pruning.note_canonical(&H256::random(), 0, &mut commit).unwrap(); db.commit(&commit); @@ -754,9 +782,10 @@ mod tests { let mut commit = CommitSet::default(); fn load_pruning_from_db(db: TestDb) -> (usize, u64) { - let pruning: RefWindow = RefWindow::new(db, false).unwrap(); + let pruning: RefWindow = + RefWindow::new(db, DEFAULT_MAX_BLOCK_CONSTRAINT, false).unwrap(); let (cache, _) = pruning.queue.get_db_backed_queue_state().unwrap(); - (cache.len(), pruning.pending_number) + (cache.len(), pruning.base) } fn push_record(block: u64, commit: &mut CommitSet) { @@ -767,24 +796,24 @@ mod tests { } // empty database - let (loaded_blocks, pending_number) = load_pruning_from_db(db.clone()); + let (loaded_blocks, base) = load_pruning_from_db(db.clone()); assert_eq!(loaded_blocks, 0); - assert_eq!(pending_number, 0); + assert_eq!(base, 0); // canonicalized the genesis block but no pruning push_last_canonicalized(0, &mut commit); push_record(0, &mut commit); db.commit(&commit); - let (loaded_blocks, pending_number) = load_pruning_from_db(db.clone()); + let (loaded_blocks, base) = load_pruning_from_db(db.clone()); assert_eq!(loaded_blocks, 1); - assert_eq!(pending_number, 0); + assert_eq!(base, 0); // pruned the genesis block push_last_pruned(0, &mut commit); db.commit(&commit); - let (loaded_blocks, pending_number) = load_pruning_from_db(db.clone()); + let (loaded_blocks, base) = load_pruning_from_db(db.clone()); assert_eq!(loaded_blocks, 0); - assert_eq!(pending_number, 1); + assert_eq!(base, 1); // canonicalize more blocks push_last_canonicalized(10, &mut commit); @@ -792,22 +821,24 @@ mod tests { push_record(i, &mut commit); } db.commit(&commit); - let (loaded_blocks, pending_number) = load_pruning_from_db(db.clone()); + let (loaded_blocks, base) = load_pruning_from_db(db.clone()); assert_eq!(loaded_blocks, 10); - assert_eq!(pending_number, 1); + assert_eq!(base, 1); // pruned all blocks push_last_pruned(10, &mut commit); db.commit(&commit); - let (loaded_blocks, pending_number) = load_pruning_from_db(db.clone()); + let (loaded_blocks, base) = load_pruning_from_db(db.clone()); assert_eq!(loaded_blocks, 0); - assert_eq!(pending_number, 11); + assert_eq!(base, 11); } #[test] fn db_backed_queue() { let mut db = make_db(&[]); - let mut pruning: RefWindow = RefWindow::new(db.clone(), false).unwrap(); + let mut pruning: RefWindow = + RefWindow::new(db.clone(), DEFAULT_MAX_BLOCK_CONSTRAINT, false).unwrap(); + let cache_capacity = DEFAULT_MAX_BLOCK_CONSTRAINT as usize + 10; // start as an empty queue let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); @@ -816,27 +847,27 @@ mod tests { // import blocks // queue size and content should match - for i in 0..(CACHE_BATCH_SIZE + 10) { + for i in 0..(cache_capacity + 10) { let mut commit = make_commit(&[], &[]); pruning.note_canonical(&(i as u64), i as u64, &mut commit).unwrap(); push_last_canonicalized(i as u64, &mut commit); db.commit(&commit); // block will fill in cache first let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); - if i < CACHE_BATCH_SIZE { + if i < cache_capacity { assert_eq!(cache.len(), i + 1); assert_eq!(uncached_blocks, 0); } else { - assert_eq!(cache.len(), CACHE_BATCH_SIZE); - assert_eq!(uncached_blocks, i - CACHE_BATCH_SIZE + 1); + assert_eq!(cache.len(), cache_capacity); + assert_eq!(uncached_blocks, i - cache_capacity + 1); } } pruning.apply_pending(); - assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 10); + assert_eq!(pruning.queue.len(), cache_capacity + 10); let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); - assert_eq!(cache.len(), CACHE_BATCH_SIZE); + assert_eq!(cache.len(), cache_capacity); assert_eq!(uncached_blocks, 10); - for i in 0..CACHE_BATCH_SIZE { + for i in 0..cache_capacity { assert_eq!(cache[i].hash, i as u64); } @@ -844,23 +875,19 @@ mod tests { // won't keep the new block in memory let mut commit = CommitSet::default(); pruning - .note_canonical( - &(CACHE_BATCH_SIZE as u64 + 10), - CACHE_BATCH_SIZE as u64 + 10, - &mut commit, - ) + .note_canonical(&(cache_capacity as u64 + 10), cache_capacity as u64 + 10, &mut commit) .unwrap(); - assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 11); + assert_eq!(pruning.queue.len(), cache_capacity + 11); let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); - assert_eq!(cache.len(), CACHE_BATCH_SIZE); + assert_eq!(cache.len(), cache_capacity); assert_eq!(uncached_blocks, 11); // revert the last add that no apply yet // NOTE: do not commit the previous `CommitSet` to db pruning.queue.revert_recent_add(0, 1); - assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 10); + assert_eq!(pruning.queue.len(), cache_capacity + 10); let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); - assert_eq!(cache.len(), CACHE_BATCH_SIZE); + assert_eq!(cache.len(), cache_capacity); assert_eq!(uncached_blocks, 10); // remove one block from the start of the queue @@ -869,22 +896,23 @@ mod tests { pruning.prune_one(&mut commit).unwrap(); db.commit(&commit); pruning.apply_pending(); - assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 9); + assert_eq!(pruning.queue.len(), cache_capacity + 9); let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); - assert_eq!(cache.len(), CACHE_BATCH_SIZE - 1); + assert_eq!(cache.len(), cache_capacity - 1); assert_eq!(uncached_blocks, 10); - for i in 0..(CACHE_BATCH_SIZE - 1) { + for i in 0..(cache_capacity - 1) { assert_eq!(cache[i].hash, (i + 1) as u64); } // load a new queue from db // `cache` is full again but the content of the queue should be the same - let pruning: RefWindow = RefWindow::new(db, false).unwrap(); - assert_eq!(pruning.queue.len(), CACHE_BATCH_SIZE + 9); + let pruning: RefWindow = + RefWindow::new(db, DEFAULT_MAX_BLOCK_CONSTRAINT, false).unwrap(); + assert_eq!(pruning.queue.len(), cache_capacity + 9); let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); - assert_eq!(cache.len(), CACHE_BATCH_SIZE); + assert_eq!(cache.len(), cache_capacity); assert_eq!(uncached_blocks, 9); - for i in 0..CACHE_BATCH_SIZE { + for i in 0..cache_capacity { assert_eq!(cache[i].hash, (i + 1) as u64); } } @@ -892,10 +920,12 @@ mod tests { #[test] fn load_block_from_db() { let mut db = make_db(&[]); - let mut pruning: RefWindow = RefWindow::new(db.clone(), false).unwrap(); + let mut pruning: RefWindow = + RefWindow::new(db.clone(), DEFAULT_MAX_BLOCK_CONSTRAINT, false).unwrap(); + let cache_capacity = DEFAULT_MAX_BLOCK_CONSTRAINT as usize + 10; // import blocks - for i in 0..(CACHE_BATCH_SIZE as u64 * 2 + 10) { + for i in 0..(cache_capacity as u64 * 2 + 10) { let mut commit = make_commit(&[], &[]); pruning.note_canonical(&i, i, &mut commit).unwrap(); push_last_canonicalized(i as u64, &mut commit); @@ -905,24 +935,24 @@ mod tests { // the following operations won't triger loading block from db: // - getting block in cache // - getting block not in the queue - let index = CACHE_BATCH_SIZE; + let index = cache_capacity; assert_eq!( pruning.queue.get(0, index - 1).unwrap().unwrap().hash, - CACHE_BATCH_SIZE as u64 - 1 + cache_capacity as u64 - 1 ); - assert_eq!(pruning.queue.get(0, CACHE_BATCH_SIZE * 2 + 10).unwrap(), None); + assert_eq!(pruning.queue.get(0, cache_capacity * 2 + 10).unwrap(), None); let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); - assert_eq!(cache.len(), CACHE_BATCH_SIZE); - assert_eq!(uncached_blocks, CACHE_BATCH_SIZE + 10); + assert_eq!(cache.len(), cache_capacity); + assert_eq!(uncached_blocks, cache_capacity + 10); // getting a block not in cache will triger loading block from db - assert_eq!(pruning.queue.get(0, index).unwrap().unwrap().hash, CACHE_BATCH_SIZE as u64); + assert_eq!(pruning.queue.get(0, index).unwrap().unwrap().hash, cache_capacity as u64); let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); - assert_eq!(cache.len(), CACHE_BATCH_SIZE * 2); + assert_eq!(cache.len(), cache_capacity * 2); assert_eq!(uncached_blocks, 10); // clear all block loaded in cache - for _ in 0..CACHE_BATCH_SIZE * 2 { + for _ in 0..cache_capacity * 2 { let mut commit = CommitSet::default(); pruning.prune_one(&mut commit).unwrap(); db.commit(&commit); @@ -935,8 +965,8 @@ mod tests { // getting the hash of block that not in cache will also triger loading // the remaining blocks from db assert_eq!( - pruning.queue.get(pruning.pending_number, 0).unwrap().unwrap().hash, - (CACHE_BATCH_SIZE * 2) as u64 + pruning.queue.get(pruning.base, 0).unwrap().unwrap().hash, + (cache_capacity * 2) as u64 ); let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), 10); @@ -944,13 +974,14 @@ mod tests { // load a new queue from db // `cache` should be the same - let pruning: RefWindow = RefWindow::new(db, false).unwrap(); + let pruning: RefWindow = + RefWindow::new(db, DEFAULT_MAX_BLOCK_CONSTRAINT, false).unwrap(); assert_eq!(pruning.queue.len(), 10); let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), 10); assert_eq!(uncached_blocks, 0); for i in 0..10 { - assert_eq!(cache[i].hash, (CACHE_BATCH_SIZE * 2 + i) as u64); + assert_eq!(cache[i].hash, (cache_capacity * 2 + i) as u64); } } } From acfa7ef5e991ab2b117a1287ea45d3c5085d8507 Mon Sep 17 00:00:00 2001 From: linning Date: Mon, 15 Aug 2022 01:59:56 +0800 Subject: [PATCH 15/19] revert change to make_test_db to add test cases Signed-off-by: linning --- client/state-db/src/lib.rs | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/client/state-db/src/lib.rs b/client/state-db/src/lib.rs index 989e3ea5b8e2f..7512edb3c3853 100644 --- a/client/state-db/src/lib.rs +++ b/client/state-db/src/lib.rs @@ -696,25 +696,13 @@ fn choose_pruning_mode( #[cfg(test)] mod tests { use crate::{ - noncanonical::LAST_CANONICAL, - pruning::LAST_PRUNED, test::{make_changeset, make_db, TestDb}, - to_meta_key, CommitSet, Constraints, Error, IsPruned, PruningMode, StateDb, StateDbError, + Constraints, Error, IsPruned, PruningMode, StateDb, StateDbError, }; - use codec::Encode; use sp_core::H256; fn make_test_db(settings: PruningMode) -> (TestDb, StateDb) { let mut db = make_db(&[91, 921, 922, 93, 94]); - - let mut commit = CommitSet::default(); - commit - .meta - .inserted - .push((to_meta_key(LAST_CANONICAL, &()), (H256::from_low_u64_be(0), 0u64).encode())); - commit.meta.inserted.push((to_meta_key(LAST_PRUNED, &()), 0u64.encode())); - db.commit(&commit); - let (state_db_init, state_db) = StateDb::open(db.clone(), Some(settings), false, true).unwrap(); db.commit(&state_db_init); From 77c830acc8a61b70fde293cedaf725192214e313 Mon Sep 17 00:00:00 2001 From: linning Date: Thu, 25 Aug 2022 20:34:02 +0800 Subject: [PATCH 16/19] do not prune unavailable block & add tests Signed-off-by: linning --- client/state-db/src/lib.rs | 62 ++++++++++++++++- client/state-db/src/pruning.rs | 119 ++++++++++++++++++++++++++------- 2 files changed, 152 insertions(+), 29 deletions(-) diff --git a/client/state-db/src/lib.rs b/client/state-db/src/lib.rs index 7512edb3c3853..b75e46982dd4c 100644 --- a/client/state-db/src/lib.rs +++ b/client/state-db/src/lib.rs @@ -116,12 +116,14 @@ pub trait NodeDb { } /// Error type. +#[derive(Eq, PartialEq)] pub enum Error { /// Database backend error. Db(E), StateDb(StateDbError), } +#[derive(Eq, PartialEq)] pub enum StateDbError { /// `Codec` decoding error. Decoding(codec::Error), @@ -139,6 +141,10 @@ pub enum StateDbError { BlockAlreadyExists, /// Invalid metadata Metadata(String), + /// Trying to get a block record from db while it is not commit to db yet + BlockUnavailable, + /// Block record is missing from the pruning window + BlockMissing, } impl From for Error { @@ -183,6 +189,9 @@ impl fmt::Debug for StateDbError { Self::TooManySiblingBlocks => write!(f, "Too many sibling blocks inserted"), Self::BlockAlreadyExists => write!(f, "Block already exists"), Self::Metadata(message) => write!(f, "Invalid metadata: {}", message), + Self::BlockUnavailable => + write!(f, "Trying to get a block record from db while it is not commit to db yet"), + Self::BlockMissing => write!(f, "Block record is missing from the pruning window"), } } } @@ -387,10 +396,20 @@ impl } let pinned = &self.pinned; - if pruning.next_hash()?.map_or(false, |h| pinned.contains_key(&h)) { - break + match pruning.next_hash() { + // the block record is temporary unavailable, break and try next time + Err(Error::StateDb(StateDbError::BlockUnavailable)) => break, + res => + if res?.map_or(false, |h| pinned.contains_key(&h)) { + break + }, + } + match pruning.prune_one(commit) { + // this branch should not reach as previous `next_hash` don't return error + // keeping it for robustness + Err(Error::StateDb(StateDbError::BlockUnavailable)) => break, + res => res?, } - pruning.prune_one(commit)?; } } Ok(()) @@ -782,6 +801,43 @@ mod tests { assert!(db.data_eq(&make_db(&[1, 21, 3, 91, 921, 922, 93, 94]))); } + #[test] + fn block_record_unavailable() { + let (mut db, state_db) = make_test_db(PruningMode::Constrained(Constraints { + max_blocks: Some(1), + max_mem: None, + })); + // import 2 blocks + for i in &[5, 6] { + db.commit( + &state_db + .insert_block( + &H256::from_low_u64_be(*i), + *i, + &H256::from_low_u64_be(*i - 1), + make_changeset(&[], &[]), + ) + .unwrap(), + ); + } + // canonicalize block 4 but not commit it to db + let c1 = state_db.canonicalize_block(&H256::from_low_u64_be(4)).unwrap(); + assert_eq!(state_db.is_pruned(&H256::from_low_u64_be(3), 3), IsPruned::Pruned); + + // canonicalize block 5 but not commit it to db, block 4 is not pruned due to it is not + // commit to db yet (unavailable), return `MaybePruned` here because `apply_pending` is not + // called and block 3 is still in cache + let c2 = state_db.canonicalize_block(&H256::from_low_u64_be(5)).unwrap(); + assert_eq!(state_db.is_pruned(&H256::from_low_u64_be(4), 4), IsPruned::MaybePruned); + + // commit block 4 and 5 to db, and import a new block will prune both block 4 and 5 + db.commit(&c1); + db.commit(&c2); + db.commit(&state_db.canonicalize_block(&H256::from_low_u64_be(6)).unwrap()); + assert_eq!(state_db.is_pruned(&H256::from_low_u64_be(4), 4), IsPruned::Pruned); + assert_eq!(state_db.is_pruned(&H256::from_low_u64_be(5), 5), IsPruned::Pruned); + } + #[test] fn prune_window_0() { let (db, _) = make_test_db(PruningMode::Constrained(Constraints { diff --git a/client/state-db/src/pruning.rs b/client/state-db/src/pruning.rs index 44755ba677de6..6b6c2b175e739 100644 --- a/client/state-db/src/pruning.rs +++ b/client/state-db/src/pruning.rs @@ -29,7 +29,7 @@ use crate::{ DEFAULT_MAX_BLOCK_CONSTRAINT, }; use codec::{Decode, Encode}; -use log::{trace, warn}; +use log::{error, trace, warn}; use std::{ cmp, collections::{HashMap, HashSet, VecDeque}, @@ -116,9 +116,8 @@ impl DeathRowQueue { mut uncached_blocks: usize, window_size: u32, ) -> Result, Error> { - // The pruning window size (if not larger than `DEFAULT_MAX_BLOCK_CONSTRAINT`) plus a magic - // number keep most common ops in cache - let cache_capacity = window_size.min(DEFAULT_MAX_BLOCK_CONSTRAINT) as usize + 10; + // limit the cache capacity from 1 to `DEFAULT_MAX_BLOCK_CONSTRAINT` + let cache_capacity = window_size.max(1).min(DEFAULT_MAX_BLOCK_CONSTRAINT) as usize; let mut cache = VecDeque::with_capacity(cache_capacity); trace!(target: "state-db", "Reading pruning journal for the database-backed queue. Pending #{}", base); // Load block from db @@ -247,12 +246,11 @@ impl DeathRowQueue { cache.push_back(row); loaded += 1; }, + // block may added to the queue but not commit into the db yet, if there are + // data missing in the db `load_death_row_from_db` should return a db error None => break, } } - // `loaded` should be the same as what we expect, if there are missing blocks - // `load_death_row_from_db` should return a db error - debug_assert_eq!(batch_size, loaded); *uncached_blocks -= loaded; Ok(()) } @@ -291,20 +289,6 @@ impl DeathRowQueue { } } - /// Get the hash of the block at the given `index` of the queue - fn get_hash(&mut self, base: u64, index: usize) -> Result, Error> { - match self { - DeathRowQueue::DbBacked { cache, .. } => - if index < cache.len() { - Ok(cache.get(index).map(|r| r.hash.clone())) - } else { - self.get(base, index).map(|r| r.map(|r| r.hash)) - }, - DeathRowQueue::Mem { death_rows, .. } => - Ok(death_rows.get(index).map(|r| r.hash.clone())), - } - } - /// Check if the block at the given `index` of the queue exist /// it is the caller's responsibility to ensure `index` won't be out of bound fn have_block(&self, hash: &BlockHash, index: usize) -> HaveBlock { @@ -445,8 +429,19 @@ impl RefWindow { (self.queue.len() - self.pending_prunings) as u64 } + /// Get the hash of the next pruning block pub fn next_hash(&mut self) -> Result, Error> { - self.queue.get_hash(self.base, self.pending_prunings) + let res = match &self.queue { + DeathRowQueue::DbBacked { cache, .. } => + if self.pending_prunings < cache.len() { + cache.get(self.pending_prunings).map(|r| r.hash.clone()) + } else { + self.get(self.pending_prunings)?.map(|r| r.hash) + }, + DeathRowQueue::Mem { death_rows, .. } => + death_rows.get(self.pending_prunings).map(|r| r.hash.clone()), + }; + Ok(res) } pub fn mem_used(&self) -> usize { @@ -474,9 +469,33 @@ impl RefWindow { self.queue.have_block(hash, (number - self.base) as usize) } + fn get(&mut self, index: usize) -> Result>, Error> { + if index >= self.queue.len() { + return Ok(None) + } + match self.queue.get(self.base, index)? { + None => { + if matches!(self.queue, DeathRowQueue::DbBacked { .. }) && + // whether trying to get a pending canonicalize block which may not commit to the db yet + index >= self.queue.len() - self.pending_canonicalizations + { + trace!(target: "state-db", "Trying to get a pending canonicalize block that not commit to the db yet"); + Err(Error::StateDb(StateDbError::BlockUnavailable)) + } else { + // A block of the queue is missing, this may happen if `CommitSet` are commit to + // db concurrently and calling `apply_pending/revert_pending` out of order, this + // should not happen under current implementation but keeping it as a defensive + error!(target: "state-db", "Block record is missing from the pruning window, block number {}", self.base + index as u64); + Err(Error::StateDb(StateDbError::BlockMissing)) + } + }, + s => Ok(s), + } + } + /// Prune next block. Expects at least one block in the window. Adds changes to `commit`. pub fn prune_one(&mut self, commit: &mut CommitSet) -> Result<(), Error> { - if let Some(pruned) = self.queue.get(self.base, self.pending_prunings)? { + if let Some(pruned) = self.get(self.pending_prunings)? { trace!(target: "state-db", "Pruning {:?} ({} deleted)", pruned.hash, pruned.deleted.len()); let index = self.base + self.pending_prunings as u64; commit.data.deleted.extend(pruned.deleted.into_iter()); @@ -551,7 +570,7 @@ mod tests { use crate::{ noncanonical::LAST_CANONICAL, test::{make_commit, make_db, TestDb}, - to_meta_key, CommitSet, Hash, DEFAULT_MAX_BLOCK_CONSTRAINT, + to_meta_key, CommitSet, Error, Hash, StateDbError, DEFAULT_MAX_BLOCK_CONSTRAINT, }; use codec::Encode; use sp_core::H256; @@ -838,7 +857,7 @@ mod tests { let mut db = make_db(&[]); let mut pruning: RefWindow = RefWindow::new(db.clone(), DEFAULT_MAX_BLOCK_CONSTRAINT, false).unwrap(); - let cache_capacity = DEFAULT_MAX_BLOCK_CONSTRAINT as usize + 10; + let cache_capacity = DEFAULT_MAX_BLOCK_CONSTRAINT as usize; // start as an empty queue let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); @@ -922,7 +941,7 @@ mod tests { let mut db = make_db(&[]); let mut pruning: RefWindow = RefWindow::new(db.clone(), DEFAULT_MAX_BLOCK_CONSTRAINT, false).unwrap(); - let cache_capacity = DEFAULT_MAX_BLOCK_CONSTRAINT as usize + 10; + let cache_capacity = DEFAULT_MAX_BLOCK_CONSTRAINT as usize; // import blocks for i in 0..(cache_capacity as u64 * 2 + 10) { @@ -984,4 +1003,52 @@ mod tests { assert_eq!(cache[i].hash, (cache_capacity * 2 + i) as u64); } } + + #[test] + fn get_block_from_queue() { + let mut db = make_db(&[]); + let mut pruning: RefWindow = + RefWindow::new(db.clone(), DEFAULT_MAX_BLOCK_CONSTRAINT, false).unwrap(); + let cache_capacity = DEFAULT_MAX_BLOCK_CONSTRAINT as u64; + + // import blocks and commit to db + let mut commit = make_commit(&[], &[]); + for i in 0..(cache_capacity + 10) { + pruning.note_canonical(&i, i, &mut commit).unwrap(); + } + db.commit(&commit); + + // import a block but not commit to db yet + let mut pending_commit = make_commit(&[], &[]); + let index = cache_capacity + 10; + pruning.note_canonical(&index, index, &mut pending_commit).unwrap(); + + let mut commit = make_commit(&[], &[]); + // prune blocks that had committed to db + for i in 0..(cache_capacity + 10) { + assert_eq!(pruning.next_hash().unwrap(), Some(i)); + pruning.prune_one(&mut commit).unwrap(); + } + // return `BlockUnavailable` for block that did not commit to db + assert_eq!( + pruning.next_hash().unwrap_err(), + Error::StateDb(StateDbError::BlockUnavailable) + ); + assert_eq!( + pruning.prune_one(&mut commit).unwrap_err(), + Error::StateDb(StateDbError::BlockUnavailable) + ); + // commit block to db and no error return + db.commit(&pending_commit); + assert_eq!(pruning.next_hash().unwrap(), Some(index)); + pruning.prune_one(&mut commit).unwrap(); + db.commit(&commit); + + // import a block and do not commit it to db before calling `apply_pending` + pruning + .note_canonical(&(index + 1), index + 1, &mut make_commit(&[], &[])) + .unwrap(); + pruning.apply_pending(); + assert_eq!(pruning.next_hash().unwrap_err(), Error::StateDb(StateDbError::BlockMissing)); + } } From 30a1f1e0725bc581ed8e0536f13c56cf3083473d Mon Sep 17 00:00:00 2001 From: NingLin-P Date: Mon, 29 Aug 2022 17:32:02 +0800 Subject: [PATCH 17/19] Update client/state-db/src/lib.rs Signed-off-by: linning Co-authored-by: cheme --- client/state-db/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/state-db/src/lib.rs b/client/state-db/src/lib.rs index b75e46982dd4c..63e98776eb304 100644 --- a/client/state-db/src/lib.rs +++ b/client/state-db/src/lib.rs @@ -374,7 +374,7 @@ impl Some(pruning) => match pruning.have_block(hash, number) { HaveBlock::NotHave => IsPruned::Pruned, HaveBlock::Have => IsPruned::NotPruned, - HaveBlock::MayHave => return IsPruned::MaybePruned, + HaveBlock::MayHave => IsPruned::MaybePruned, }, } } From 917e26eaa547ab4d0b5437fbe62bc7c1cb98353f Mon Sep 17 00:00:00 2001 From: NingLin-P Date: Mon, 29 Aug 2022 17:33:08 +0800 Subject: [PATCH 18/19] Update client/state-db/src/pruning.rs Signed-off-by: linning Co-authored-by: cheme --- client/state-db/src/pruning.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/state-db/src/pruning.rs b/client/state-db/src/pruning.rs index 6b6c2b175e739..b8ae3e61ec947 100644 --- a/client/state-db/src/pruning.rs +++ b/client/state-db/src/pruning.rs @@ -77,7 +77,7 @@ enum DeathRowQueue { cache: VecDeque>, /// A soft limit of the cache's size cache_capacity: usize, - /// The number of blocks that not loaded into `cache` + /// The number of blocks in queue that are not loaded into `cache`. uncached_blocks: usize, }, } From 469b0b2dbb89b88676121a5b7f3268d7b811faaa Mon Sep 17 00:00:00 2001 From: linning Date: Mon, 29 Aug 2022 18:22:55 +0800 Subject: [PATCH 19/19] address comment Signed-off-by: linning --- client/state-db/src/lib.rs | 6 +++--- client/state-db/src/pruning.rs | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/client/state-db/src/lib.rs b/client/state-db/src/lib.rs index 63e98776eb304..f21b707a489f0 100644 --- a/client/state-db/src/lib.rs +++ b/client/state-db/src/lib.rs @@ -363,10 +363,10 @@ impl PruningMode::ArchiveAll => IsPruned::NotPruned, PruningMode::ArchiveCanonical | PruningMode::Constrained(_) => { if self.best_canonical().map(|c| number > c).unwrap_or(true) { - if !self.non_canonical.have_block(hash) { - IsPruned::Pruned - } else { + if self.non_canonical.have_block(hash) { IsPruned::NotPruned + } else { + IsPruned::Pruned } } else { match self.pruning.as_ref() { diff --git a/client/state-db/src/pruning.rs b/client/state-db/src/pruning.rs index b8ae3e61ec947..2c23110910495 100644 --- a/client/state-db/src/pruning.rs +++ b/client/state-db/src/pruning.rs @@ -462,7 +462,8 @@ impl RefWindow { // if the queue is empty or the block number exceed the pruning window, we definitely // do not have this block if self.is_empty() || - !(number >= self.pending() && number < self.base + self.queue.len() as u64) + number < self.pending() || + number >= self.base + self.queue.len() as u64 { return HaveBlock::NotHave } @@ -903,7 +904,7 @@ mod tests { // revert the last add that no apply yet // NOTE: do not commit the previous `CommitSet` to db - pruning.queue.revert_recent_add(0, 1); + pruning.revert_pending(); assert_eq!(pruning.queue.len(), cache_capacity + 10); let (cache, uncached_blocks) = pruning.queue.get_db_backed_queue_state().unwrap(); assert_eq!(cache.len(), cache_capacity);