This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add small header cache #7516
Merged
Merged
Add small header cache #7516
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e949c12
Remove header query
arkpar 90d9197
Header cache
arkpar b3ced92
Fix potential race issue
arkpar 5e14df4
Simplify status query
arkpar 4bbd127
Merge branch 'master' of github.com:paritytech/substrate into a-cache…
arkpar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -49,6 +49,9 @@ use std::sync::Arc; | |||||||||||||||||||||||
| use std::path::{Path, PathBuf}; | ||||||||||||||||||||||||
| use std::io; | ||||||||||||||||||||||||
| use std::collections::{HashMap, HashSet}; | ||||||||||||||||||||||||
| use parking_lot::{Mutex, RwLock}; | ||||||||||||||||||||||||
| use linked_hash_map::LinkedHashMap; | ||||||||||||||||||||||||
| use log::{trace, debug, warn}; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| use sc_client_api::{ | ||||||||||||||||||||||||
| UsageInfo, MemoryInfo, IoInfo, MemorySize, | ||||||||||||||||||||||||
|
|
@@ -63,7 +66,6 @@ use codec::{Decode, Encode}; | |||||||||||||||||||||||
| use hash_db::Prefix; | ||||||||||||||||||||||||
| use sp_trie::{MemoryDB, PrefixedMemoryDB, prefixed_key}; | ||||||||||||||||||||||||
| use sp_database::Transaction; | ||||||||||||||||||||||||
| use parking_lot::RwLock; | ||||||||||||||||||||||||
| use sp_core::ChangesTrieConfiguration; | ||||||||||||||||||||||||
| use sp_core::offchain::storage::{OffchainOverlayedChange, OffchainOverlayedChanges}; | ||||||||||||||||||||||||
| use sp_core::storage::{well_known_keys, ChildInfo}; | ||||||||||||||||||||||||
|
|
@@ -83,7 +85,6 @@ use sc_state_db::StateDb; | |||||||||||||||||||||||
| use sp_blockchain::{CachedHeaderMetadata, HeaderMetadata, HeaderMetadataCache}; | ||||||||||||||||||||||||
| use crate::storage_cache::{CachingState, SyncingCachingState, SharedCache, new_shared_cache}; | ||||||||||||||||||||||||
| use crate::stats::StateUsageStats; | ||||||||||||||||||||||||
| use log::{trace, debug, warn}; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Re-export the Database trait so that one can pass an implementation of it. | ||||||||||||||||||||||||
| pub use sp_database::Database; | ||||||||||||||||||||||||
|
|
@@ -93,6 +94,7 @@ pub use sc_state_db::PruningMode; | |||||||||||||||||||||||
| pub use bench::BenchmarkingState; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const MIN_BLOCKS_TO_KEEP_CHANGES_TRIES_FOR: u32 = 32768; | ||||||||||||||||||||||||
| const CACHE_HEADERS: usize = 8; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /// Default value for storage cache child ratio. | ||||||||||||||||||||||||
| const DEFAULT_CHILD_RATIO: (usize, usize) = (1, 10); | ||||||||||||||||||||||||
|
|
@@ -352,12 +354,24 @@ impl<'a> sc_state_db::MetaDb for StateMetaDb<'a> { | |||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| fn cache_header<Hash: std::cmp::Eq + std::hash::Hash, Header>( | ||||||||||||||||||||||||
| cache: &mut LinkedHashMap<Hash, Option<Header>>, | ||||||||||||||||||||||||
| hash: Hash, | ||||||||||||||||||||||||
| header: Option<Header> | ||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||
| cache.insert(hash, header); | ||||||||||||||||||||||||
| while cache.len() > CACHE_HEADERS { | ||||||||||||||||||||||||
| cache.pop_front(); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /// Block database | ||||||||||||||||||||||||
| pub struct BlockchainDb<Block: BlockT> { | ||||||||||||||||||||||||
| db: Arc<dyn Database<DbHash>>, | ||||||||||||||||||||||||
| meta: Arc<RwLock<Meta<NumberFor<Block>, Block::Hash>>>, | ||||||||||||||||||||||||
| leaves: RwLock<LeafSet<Block::Hash, NumberFor<Block>>>, | ||||||||||||||||||||||||
| header_metadata_cache: Arc<HeaderMetadataCache<Block>>, | ||||||||||||||||||||||||
| header_cache: Mutex<LinkedHashMap<Block::Hash, Option<Block::Header>>>, | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| impl<Block: BlockT> BlockchainDb<Block> { | ||||||||||||||||||||||||
|
|
@@ -369,6 +383,7 @@ impl<Block: BlockT> BlockchainDb<Block> { | |||||||||||||||||||||||
| leaves: RwLock::new(leaves), | ||||||||||||||||||||||||
| meta: Arc::new(RwLock::new(meta)), | ||||||||||||||||||||||||
| header_metadata_cache: Arc::new(HeaderMetadataCache::default()), | ||||||||||||||||||||||||
| header_cache: Default::default(), | ||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
@@ -407,7 +422,24 @@ impl<Block: BlockT> BlockchainDb<Block> { | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| impl<Block: BlockT> sc_client_api::blockchain::HeaderBackend<Block> for BlockchainDb<Block> { | ||||||||||||||||||||||||
| fn header(&self, id: BlockId<Block>) -> ClientResult<Option<Block::Header>> { | ||||||||||||||||||||||||
| utils::read_header(&*self.db, columns::KEY_LOOKUP, columns::HEADER, id) | ||||||||||||||||||||||||
| match &id { | ||||||||||||||||||||||||
| BlockId::Hash(h) => { | ||||||||||||||||||||||||
| let mut cache = self.header_cache.lock(); | ||||||||||||||||||||||||
| if let Some(result) = cache.get_refresh(h) { | ||||||||||||||||||||||||
| return Ok(result.clone()); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| match utils::read_header(&*self.db, columns::KEY_LOOKUP, columns::HEADER, id) { | ||||||||||||||||||||||||
| Ok(header) => { | ||||||||||||||||||||||||
| cache_header(&mut cache, h.clone(), header.clone()); | ||||||||||||||||||||||||
| Ok(header) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| e @Err(_) => e, | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| BlockId::Number(_) => { | ||||||||||||||||||||||||
| utils::read_header(&*self.db, columns::KEY_LOOKUP, columns::HEADER, id) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| fn info(&self) -> sc_client_api::blockchain::Info<Block> { | ||||||||||||||||||||||||
|
|
@@ -424,12 +456,17 @@ impl<Block: BlockT> sc_client_api::blockchain::HeaderBackend<Block> for Blockcha | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| fn status(&self, id: BlockId<Block>) -> ClientResult<sc_client_api::blockchain::BlockStatus> { | ||||||||||||||||||||||||
| let exists = match id { | ||||||||||||||||||||||||
| BlockId::Hash(_) => read_db( | ||||||||||||||||||||||||
| &*self.db, | ||||||||||||||||||||||||
| columns::KEY_LOOKUP, | ||||||||||||||||||||||||
| columns::HEADER, | ||||||||||||||||||||||||
| id | ||||||||||||||||||||||||
| )?.is_some(), | ||||||||||||||||||||||||
| BlockId::Hash(h) => match self.header_cache.lock().get(&h) { | ||||||||||||||||||||||||
| Some(header) => header.is_some(), | ||||||||||||||||||||||||
| None => { | ||||||||||||||||||||||||
| read_db( | ||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't we cache here?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed it to simply use |
||||||||||||||||||||||||
| &*self.db, | ||||||||||||||||||||||||
| columns::KEY_LOOKUP, | ||||||||||||||||||||||||
| columns::HEADER, | ||||||||||||||||||||||||
| id | ||||||||||||||||||||||||
| )?.is_some() | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| BlockId::Number(n) => n <= self.meta.read().best_number, | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
| match exists { | ||||||||||||||||||||||||
|
|
@@ -515,7 +552,7 @@ impl<Block: BlockT> HeaderMetadata<Block> for BlockchainDb<Block> { | |||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| fn insert_header_metadata(&self, hash: Block::Hash, metadata: CachedHeaderMetadata<Block>) { | ||||||||||||||||||||||||
| self.header_metadata_cache.insert_header_metadata(hash, metadata) | ||||||||||||||||||||||||
| self.header_metadata_cache.insert_header_metadata(hash, metadata); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| fn remove_header_metadata(&self, hash: Block::Hash) { | ||||||||||||||||||||||||
|
|
@@ -1117,12 +1154,6 @@ impl<Block: BlockT> Backend<Block> { | |||||||||||||||||||||||
| hash, | ||||||||||||||||||||||||
| )?; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let header_metadata = CachedHeaderMetadata::from(&pending_block.header); | ||||||||||||||||||||||||
| self.blockchain.insert_header_metadata( | ||||||||||||||||||||||||
| header_metadata.hash, | ||||||||||||||||||||||||
| header_metadata, | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| transaction.set_from_vec(columns::HEADER, &lookup_key, pending_block.header.encode()); | ||||||||||||||||||||||||
| if let Some(body) = &pending_block.body { | ||||||||||||||||||||||||
| transaction.set_from_vec(columns::BODY, &lookup_key, body.encode()); | ||||||||||||||||||||||||
|
|
@@ -1271,7 +1302,7 @@ impl<Block: BlockT> Backend<Block> { | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| meta_updates.push((hash, number, pending_block.leaf_state.is_best(), finalized)); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Some((number, hash, enacted, retracted, displaced_leaf, is_best, cache)) | ||||||||||||||||||||||||
| Some((pending_block.header, number, hash, enacted, retracted, displaced_leaf, is_best, cache)) | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| None | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
|
|
@@ -1297,7 +1328,11 @@ impl<Block: BlockT> Backend<Block> { | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| self.storage.db.commit(transaction)?; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Apply all in-memory state shanges. | ||||||||||||||||||||||||
| // Code beyond this point can't fail. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if let Some(( | ||||||||||||||||||||||||
| header, | ||||||||||||||||||||||||
| number, | ||||||||||||||||||||||||
| hash, | ||||||||||||||||||||||||
| enacted, | ||||||||||||||||||||||||
|
|
@@ -1306,6 +1341,12 @@ impl<Block: BlockT> Backend<Block> { | |||||||||||||||||||||||
| is_best, | ||||||||||||||||||||||||
| mut cache, | ||||||||||||||||||||||||
| )) = imported { | ||||||||||||||||||||||||
| let header_metadata = CachedHeaderMetadata::from(&header); | ||||||||||||||||||||||||
| self.blockchain.insert_header_metadata( | ||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. |
||||||||||||||||||||||||
| header_metadata.hash, | ||||||||||||||||||||||||
| header_metadata, | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| cache_header(&mut self.blockchain.header_cache.lock(), hash, Some(header)); | ||||||||||||||||||||||||
| cache.sync_cache( | ||||||||||||||||||||||||
| &enacted, | ||||||||||||||||||||||||
| &retracted, | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.