-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix(provider): cap storage_v2 unwind history by MDBX tip #23335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ae6cc31
fix(provider): add unwind reader barrier for storage_v2
joshieDo 92fa036
docs(db): clarify reader txnid semantics
joshieDo 1259505
refactor(provider): simplify unwind reader tracker
joshieDo dcaf736
refactor(db): reuse pre-commit reader wait
joshieDo f93fd06
refactor(db-api): require reader txnid methods
joshieDo 9baccce
refactor(provider): narrow reader tracker input
joshieDo 2743027
refactor(provider): keep unwind storage_v2 local
joshieDo 99f08ac
Merge branch 'main' into joshie/unwind-reader-barrier
joshieDo 4d62686
fix(stages): drop ro provider before unwind commit
joshieDo f2f2bda
docs(provider): note unwind reader drain
joshieDo b01485a
refactor(provider): cap historical rocksdb lookups by mdbx tip
joshieDo b5b3657
refactor(provider): simplify rocksdb history cap logic
joshieDo 7d8fbd6
docs(provider): restore historical lookup wording
joshieDo 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
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
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
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 |
|---|---|---|
|
|
@@ -37,7 +37,7 @@ use reth_chain_state::{ComputedTrieData, ExecutedBlock}; | |
| use reth_chainspec::{ChainInfo, ChainSpecProvider, EthChainSpec}; | ||
| use reth_db_api::{ | ||
| cursor::{DbCursorRO, DbCursorRW, DbDupCursorRO, DbDupCursorRW}, | ||
| database::Database, | ||
| database::{Database, ReaderTxnTracker}, | ||
| models::{ | ||
| sharded_key, storage_sharded_key::StorageShardedKey, AccountBeforeTx, BlockNumberAddress, | ||
| BlockNumberAddressRange, ShardedKey, StorageBeforeTx, StorageSettings, | ||
|
|
@@ -212,6 +212,8 @@ pub struct DatabaseProvider<TX, N: NodeTypes> { | |
| minimum_pruning_distance: u64, | ||
| /// Database provider metrics | ||
| metrics: metrics::DatabaseProviderMetrics, | ||
| /// Database handle used to inspect active MDBX readers during unwind commits. | ||
| reader_txn_tracker: Option<Arc<dyn ReaderTxnTracker>>, | ||
|
Comment on lines
+215
to
+216
Collaborator
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. this is essentially
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. yeah having this generic over |
||
| } | ||
|
|
||
| impl<TX: Debug, N: NodeTypes> Debug for DatabaseProvider<TX, N> { | ||
|
|
@@ -229,6 +231,7 @@ impl<TX: Debug, N: NodeTypes> Debug for DatabaseProvider<TX, N> { | |
| .field("pending_rocksdb_batches", &"<pending batches>") | ||
| .field("commit_order", &self.commit_order) | ||
| .field("minimum_pruning_distance", &self.minimum_pruning_distance) | ||
| .field("reader_txn_tracker", &"<reader txn tracker>") | ||
| .finish() | ||
| } | ||
| } | ||
|
|
@@ -244,9 +247,57 @@ impl<TX, N: NodeTypes> DatabaseProvider<TX, N> { | |
| self.minimum_pruning_distance = distance; | ||
| self | ||
| } | ||
|
|
||
| /// Attaches MDBX reader tracking so unwind commits can wait on active readers. | ||
| pub(crate) fn with_reader_txn_tracker<DB>(mut self, db: DB) -> Self | ||
| where | ||
| DB: Database + 'static, | ||
| { | ||
| self.reader_txn_tracker = Some(Arc::new(db)); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl<TX: DbTx + 'static, N: NodeTypes> DatabaseProvider<TX, N> { | ||
| /// Commits unwind writes in MDBX -> `RocksDB` -> static-file order. | ||
| /// | ||
| /// This keeps MDBX as the first durable step so an interrupted unwind can be recovered by | ||
| /// truncating static files from checkpoints on the next startup. | ||
| /// | ||
| /// For `storage_v2`, this waits after the MDBX commit and again after the `RocksDB` commit so | ||
| /// readers holding older MDBX-visible views, including readers opened before the `RocksDB` | ||
| /// commit, cannot overlap the later cross-store steps. | ||
| /// | ||
| /// Example: a reader that still sees pre-unwind `RocksDB` history must not survive long enough | ||
| /// to route a lookup into changesets (SF) that the unwind is about to make unreachable. | ||
| fn commit_unwind(self) -> ProviderResult<()> { | ||
| let storage_v2 = self.cached_storage_settings().storage_v2; | ||
| let reader_txn_tracker = self.reader_txn_tracker.clone(); | ||
| self.tx.commit()?; | ||
|
|
||
| if storage_v2 && | ||
| let Some(reader_txn_tracker) = reader_txn_tracker.as_ref() && | ||
| let Some(committed_txn_id) = reader_txn_tracker.last_txnid() | ||
| { | ||
| reader_txn_tracker.wait_for_readers_before_txnid(committed_txn_id); | ||
| } | ||
|
|
||
| let batches = std::mem::take(&mut *self.pending_rocksdb_batches.lock()); | ||
| for batch in batches { | ||
| self.rocksdb_provider.commit_batch(batch)?; | ||
| } | ||
|
|
||
| if storage_v2 && | ||
| let Some(reader_txn_tracker) = reader_txn_tracker.as_ref() && | ||
| let Some(fence_txnid) = reader_txn_tracker.commit_fence()? | ||
| { | ||
| reader_txn_tracker.wait_for_readers_before_txnid(fence_txnid); | ||
| } | ||
|
|
||
| self.static_file_provider.commit()?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// State provider for latest state | ||
| pub fn latest<'a>(&'a self) -> Box<dyn StateProvider + 'a> { | ||
| trace!(target: "providers::db", "Returning latest state provider"); | ||
|
|
@@ -382,6 +433,7 @@ impl<TX: DbTxMut, N: NodeTypes> DatabaseProvider<TX, N> { | |
| commit_order, | ||
| minimum_pruning_distance: MINIMUM_UNWIND_SAFE_DISTANCE, | ||
| metrics: metrics::DatabaseProviderMetrics::default(), | ||
| reader_txn_tracker: None, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1007,6 +1059,7 @@ impl<TX: DbTx + 'static, N: NodeTypesForProvider> DatabaseProvider<TX, N> { | |
| commit_order: CommitOrder::Normal, | ||
| minimum_pruning_distance: MINIMUM_UNWIND_SAFE_DISTANCE, | ||
| metrics: metrics::DatabaseProviderMetrics::default(), | ||
| reader_txn_tracker: None, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -3800,19 +3853,8 @@ impl<TX: DbTx + 'static, N: NodeTypes + 'static> DBProvider for DatabaseProvider | |
| skip_all | ||
| )] | ||
| fn commit(self) -> ProviderResult<()> { | ||
| // For unwinding it makes more sense to commit the database first, since if | ||
| // it is interrupted before the static files commit, we can just | ||
| // truncate the static files according to the | ||
| // checkpoints on the next start-up. | ||
| if self.static_file_provider.has_unwind_queued() || self.commit_order.is_unwind() { | ||
| self.tx.commit()?; | ||
|
|
||
| let batches = std::mem::take(&mut *self.pending_rocksdb_batches.lock()); | ||
| for batch in batches { | ||
| self.rocksdb_provider.commit_batch(batch)?; | ||
| } | ||
|
|
||
| self.static_file_provider.commit()?; | ||
| self.commit_unwind()?; | ||
| } else { | ||
| // Normal path: finalize() will call sync_all() if not already synced | ||
| let mut timings = metrics::CommitTimings::default(); | ||
|
|
@@ -3880,15 +3922,18 @@ mod tests { | |
| U256, | ||
| }; | ||
| use reth_chain_state::ExecutedBlock; | ||
| use reth_db_api::models::StorageSettings; | ||
| use reth_ethereum_primitives::Receipt; | ||
| use reth_execution_types::{AccountRevertInit, BlockExecutionOutput, BlockExecutionResult}; | ||
| use reth_primitives_traits::SealedBlock; | ||
| use reth_storage_api::MetadataWriter; | ||
| use reth_testing_utils::generators::{self, random_block, BlockParams}; | ||
| use reth_trie::{ | ||
| HashedPostState, KeccakKeyHasher, Nibbles, StoredNibbles, StoredNibblesSubKey, | ||
| }; | ||
| use revm_database::BundleState; | ||
| use revm_state::AccountInfo; | ||
| use std::{sync::mpsc, time::Duration}; | ||
|
|
||
| #[test] | ||
| fn test_receipts_by_block_range_empty_range() { | ||
|
|
@@ -3902,6 +3947,47 @@ mod tests { | |
| assert_eq!(result, Vec::<Vec<reth_ethereum_primitives::Receipt>>::new()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn unwind_commit_waits_for_pre_commit_readers() { | ||
| let factory = create_test_provider_factory(); | ||
| factory.set_storage_settings_cache(StorageSettings::v2()); | ||
|
|
||
| let reader = factory.provider().unwrap(); | ||
| let provider_rw = factory.unwind_provider_rw().unwrap(); | ||
| provider_rw.write_metadata("unwind-wait-test", vec![1]).unwrap(); | ||
| let (done_tx, done_rx) = mpsc::channel(); | ||
|
|
||
| let handle = std::thread::spawn(move || { | ||
| let result = provider_rw.commit(); | ||
| done_tx.send(result).unwrap(); | ||
| }); | ||
|
|
||
| assert!( | ||
| done_rx.recv_timeout(Duration::from_millis(50)).is_err(), | ||
| "unwind commit should wait while an older read transaction is still open" | ||
| ); | ||
|
|
||
| drop(reader); | ||
|
|
||
| done_rx.recv_timeout(Duration::from_secs(1)).unwrap().unwrap(); | ||
| handle.join().unwrap(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn unwind_fence_commit_advances_txnid() { | ||
| let factory = create_test_provider_factory(); | ||
| factory.set_storage_settings_cache(StorageSettings::v2()); | ||
|
|
||
| let provider_rw = factory.unwind_provider_rw().unwrap(); | ||
| provider_rw.write_metadata("unwind-fence-txnid-test", vec![1]).unwrap(); | ||
| provider_rw.commit().unwrap(); | ||
|
|
||
| let before_fence = Database::last_txnid(factory.db_ref()).unwrap(); | ||
| let fence_txnid = ReaderTxnTracker::commit_fence(factory.db_ref()).unwrap().unwrap(); | ||
|
|
||
| assert!(fence_txnid > before_fence, "sentinel fence should advance the MDBX txnid"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_receipts_by_block_range_nonexistent_blocks() { | ||
| let factory = create_test_provider_factory(); | ||
|
|
||
Oops, something went wrong.
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.
this is especially important to understand