-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(katana): stage sync pipeline (#2502)
pipeline abstraction
- Loading branch information
Showing
15 changed files
with
275 additions
and
52 deletions.
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 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 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 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 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 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 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,18 @@ | ||
[package] | ||
edition.workspace = true | ||
license.workspace = true | ||
name = "katana-pipeline" | ||
repository.workspace = true | ||
version.workspace = true | ||
|
||
[dependencies] | ||
katana-core.workspace = true | ||
katana-executor.workspace = true | ||
katana-pool.workspace = true | ||
katana-tasks.workspace = true | ||
|
||
anyhow.workspace = true | ||
async-trait.workspace = true | ||
futures.workspace = true | ||
thiserror.workspace = true | ||
tracing.workspace = true |
This file contains 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,77 @@ | ||
#![cfg_attr(not(test), warn(unused_crate_dependencies))] | ||
|
||
pub mod stage; | ||
|
||
use core::future::IntoFuture; | ||
|
||
use futures::future::BoxFuture; | ||
use stage::Stage; | ||
use tracing::info; | ||
|
||
/// The result of a pipeline execution. | ||
pub type PipelineResult = Result<(), Error>; | ||
|
||
/// The future type for [Pipeline]'s implementation of [IntoFuture]. | ||
pub type PipelineFut = BoxFuture<'static, PipelineResult>; | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error { | ||
#[error(transparent)] | ||
Stage(#[from] stage::Error), | ||
} | ||
|
||
/// Manages the execution of stages. | ||
/// | ||
/// The pipeline drives the execution of stages, running each stage to completion in the order they | ||
/// were added. | ||
/// | ||
/// Inspired by [`reth`]'s staged sync pipeline. | ||
/// | ||
/// [`reth`]: /~https://github.com/paradigmxyz/reth/blob/c7aebff0b6bc19cd0b73e295497d3c5150d40ed8/crates/stages/api/src/pipeline/mod.rs#L66 | ||
pub struct Pipeline { | ||
stages: Vec<Box<dyn Stage>>, | ||
} | ||
|
||
impl Pipeline { | ||
/// Create a new empty pipeline. | ||
pub fn new() -> Self { | ||
Self { stages: Vec::new() } | ||
} | ||
|
||
/// Insert a new stage into the pipeline. | ||
pub fn add_stage(&mut self, stage: Box<dyn Stage>) { | ||
self.stages.push(stage); | ||
} | ||
|
||
/// Start the pipeline. | ||
pub async fn run(&mut self) -> PipelineResult { | ||
for stage in &mut self.stages { | ||
info!(id = %stage.id(), "Executing stage"); | ||
stage.execute().await?; | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl IntoFuture for Pipeline { | ||
type Output = PipelineResult; | ||
type IntoFuture = PipelineFut; | ||
|
||
fn into_future(mut self) -> Self::IntoFuture { | ||
Box::pin(async move { self.run().await }) | ||
} | ||
} | ||
|
||
impl core::default::Default for Pipeline { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl core::fmt::Debug for Pipeline { | ||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
f.debug_struct("Pipeline") | ||
.field("stages", &self.stages.iter().map(|s| s.id()).collect::<Vec<_>>()) | ||
.finish() | ||
} | ||
} |
This file contains 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,34 @@ | ||
mod sequencing; | ||
|
||
pub use sequencing::Sequencing; | ||
|
||
/// The result type of a stage execution. See [Stage::execute]. | ||
pub type StageResult = Result<(), Error>; | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub enum StageId { | ||
Sequencing, | ||
} | ||
|
||
impl core::fmt::Display for StageId { | ||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
match self { | ||
StageId::Sequencing => write!(f, "Sequencing"), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error { | ||
#[error(transparent)] | ||
Other(#[from] anyhow::Error), | ||
} | ||
|
||
#[async_trait::async_trait] | ||
pub trait Stage: Send + Sync { | ||
/// Returns the id which uniquely identifies the stage. | ||
fn id(&self) -> StageId; | ||
|
||
/// Executes the stage. | ||
async fn execute(&mut self) -> StageResult; | ||
} |
Oops, something went wrong.