-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat: BackFillJobStream #9578
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
feat: BackFillJobStream #9578
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a61f4c7
feat: BackFillJobStream first iteration
loocapro 15c2a9d
feat: promoting to mod and introducing stream capacity
loocapro ab1fa61
Update crates/exex/exex/src/backfill/stream.rs
loocapro db006d7
rename capacity to parallelism, added a with_parallelism and initiali…
loocapro 136caa8
const fn
loocapro 701f948
chore: fix comment
loocapro 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| use reth_evm::execute::{ | ||
| BlockExecutionError, BlockExecutionOutput, BlockExecutorProvider, Executor, | ||
| }; | ||
| use reth_primitives::{BlockNumber, BlockWithSenders, Receipt}; | ||
| use reth_provider::{ | ||
| BlockReader, HeaderProvider, ProviderError, StateProviderFactory, TransactionVariant, | ||
| }; | ||
| use reth_revm::database::StateProviderDatabase; | ||
| use reth_tracing::tracing::trace; | ||
| use std::ops::RangeInclusive; | ||
|
|
||
| use crate::BackfillJob; | ||
|
|
||
| /// Single block Backfill job started for a specific range. | ||
| /// | ||
| /// It implements [`Iterator`] which executes a block each time the | ||
| /// iterator is advanced and yields ([`BlockWithSenders`], [`BlockExecutionOutput`]) | ||
| #[derive(Debug, Clone)] | ||
| pub struct SingleBlockBackfillJob<E, P> { | ||
| executor: E, | ||
| provider: P, | ||
| pub(crate) range: RangeInclusive<BlockNumber>, | ||
| } | ||
|
|
||
| impl<E, P> Iterator for SingleBlockBackfillJob<E, P> | ||
| where | ||
| E: BlockExecutorProvider, | ||
| P: HeaderProvider + BlockReader + StateProviderFactory, | ||
| { | ||
| type Item = Result<(BlockWithSenders, BlockExecutionOutput<Receipt>), BlockExecutionError>; | ||
|
|
||
| fn next(&mut self) -> Option<Self::Item> { | ||
| self.range.next().map(|block_number| self.execute_block(block_number)) | ||
| } | ||
| } | ||
|
|
||
| impl<E, P> SingleBlockBackfillJob<E, P> | ||
| where | ||
| E: BlockExecutorProvider, | ||
| P: HeaderProvider + BlockReader + StateProviderFactory, | ||
| { | ||
| pub(crate) fn execute_block( | ||
| &self, | ||
| block_number: u64, | ||
| ) -> Result<(BlockWithSenders, BlockExecutionOutput<Receipt>), BlockExecutionError> { | ||
| let td = self | ||
| .provider | ||
| .header_td_by_number(block_number)? | ||
| .ok_or_else(|| ProviderError::HeaderNotFound(block_number.into()))?; | ||
|
|
||
| // Fetch the block with senders for execution. | ||
| let block_with_senders = self | ||
| .provider | ||
| .block_with_senders(block_number.into(), TransactionVariant::WithHash)? | ||
| .ok_or_else(|| ProviderError::HeaderNotFound(block_number.into()))?; | ||
|
|
||
| // Configure the executor to use the previous block's state. | ||
| let executor = self.executor.executor(StateProviderDatabase::new( | ||
| self.provider.history_by_block_number(block_number.saturating_sub(1))?, | ||
| )); | ||
|
|
||
| trace!(target: "exex::backfill", number = block_number, txs = block_with_senders.block.body.len(), "Executing block"); | ||
|
|
||
| let block_execution_output = executor.execute((&block_with_senders, td).into())?; | ||
|
|
||
| Ok((block_with_senders, block_execution_output)) | ||
| } | ||
| } | ||
|
|
||
| impl<E, P> From<BackfillJob<E, P>> for SingleBlockBackfillJob<E, P> { | ||
| fn from(value: BackfillJob<E, P>) -> Self { | ||
| Self { executor: value.executor, provider: value.provider, range: value.range } | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| use super::job::SingleBlockBackfillJob; | ||
| use futures::{ | ||
| stream::{FuturesOrdered, Stream}, | ||
| StreamExt, | ||
| }; | ||
| use reth_evm::execute::{BlockExecutionError, BlockExecutionOutput, BlockExecutorProvider}; | ||
| use reth_primitives::{BlockNumber, BlockWithSenders, Receipt}; | ||
| use reth_provider::{BlockReader, HeaderProvider, StateProviderFactory}; | ||
| use std::{ | ||
| ops::RangeInclusive, | ||
| pin::Pin, | ||
| task::{ready, Context, Poll}, | ||
| }; | ||
| use tokio::task::JoinHandle; | ||
|
|
||
| type BackfillTasks = FuturesOrdered< | ||
| JoinHandle<Result<(BlockWithSenders, BlockExecutionOutput<Receipt>), BlockExecutionError>>, | ||
| >; | ||
|
|
||
| /// The default parallelism for active tasks in [`BackFillJobStream`]. | ||
| const DEFAULT_PARALLELISM: usize = 4; | ||
|
|
||
| /// Stream for processing backfill jobs asynchronously. | ||
| /// | ||
| /// This struct manages the execution of [`SingleBlockBackfillJob`] tasks, allowing blocks to be | ||
| /// processed asynchronously but in order within a specified range. | ||
| #[derive(Debug)] | ||
| pub struct BackFillJobStream<E, P> { | ||
| job: SingleBlockBackfillJob<E, P>, | ||
| tasks: BackfillTasks, | ||
| range: RangeInclusive<BlockNumber>, | ||
| parallelism: usize, | ||
| } | ||
|
|
||
| impl<E, P> BackFillJobStream<E, P> | ||
| where | ||
| E: BlockExecutorProvider + Clone + Send + 'static, | ||
| P: HeaderProvider + BlockReader + StateProviderFactory + Clone + Send + 'static, | ||
| { | ||
| /// Creates a new [`BackFillJobStream`] with the default parallelism. | ||
| /// | ||
| /// # Parameters | ||
| /// - `job`: The [`SingleBlockBackfillJob`] to be executed asynchronously. | ||
| /// | ||
| /// # Returns | ||
| /// A new instance of [`BackFillJobStream`] with the default parallelism. | ||
| pub fn new(job: SingleBlockBackfillJob<E, P>) -> Self { | ||
| let range = job.range.clone(); | ||
| Self { job, tasks: FuturesOrdered::new(), range, parallelism: DEFAULT_PARALLELISM } | ||
| } | ||
|
|
||
| /// Configures the parallelism of the [`BackFillJobStream`] to handle active tasks. | ||
| /// | ||
| /// # Parameters | ||
| /// - `parallelism`: The parallelism to handle active tasks. | ||
| /// | ||
| /// # Returns | ||
| /// The modified instance of [`BackFillJobStream`] with the specified parallelism. | ||
| pub const fn with_parallelism(mut self, parallelism: usize) -> Self { | ||
| self.parallelism = parallelism; | ||
| self | ||
| } | ||
|
|
||
| fn spawn_task( | ||
| &self, | ||
| block_number: BlockNumber, | ||
| ) -> JoinHandle<Result<(BlockWithSenders, BlockExecutionOutput<Receipt>), BlockExecutionError>> | ||
| { | ||
| let job = self.job.clone(); | ||
| tokio::task::spawn_blocking(move || job.execute_block(block_number)) | ||
| } | ||
| } | ||
|
|
||
| impl<E, P> Stream for BackFillJobStream<E, P> | ||
| where | ||
| E: BlockExecutorProvider + Clone + Send + 'static, | ||
| P: HeaderProvider + BlockReader + StateProviderFactory + Clone + Send + 'static + Unpin, | ||
| { | ||
| type Item = Result<(BlockWithSenders, BlockExecutionOutput<Receipt>), BlockExecutionError>; | ||
|
|
||
| fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
| let this = self.get_mut(); | ||
|
|
||
| // Spawn new tasks only if we are below the parallelism configured. | ||
| while this.tasks.len() < this.parallelism { | ||
| if let Some(block_number) = this.range.next() { | ||
| let task = this.spawn_task(block_number); | ||
| this.tasks.push_back(task); | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| match ready!(this.tasks.poll_next_unpin(cx)) { | ||
| Some(res) => Poll::Ready(Some(res.map_err(|e| BlockExecutionError::Other(e.into()))?)), | ||
| None => Poll::Ready(None), | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.