dynamo_runtime/
pipeline.rs

1// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4/// In a Pipeline, the [`AsyncEngine`] is constrained to take a [`Context`] as input and return
5/// a [`super::engine::ResponseStream`] as output.
6use serde::{Deserialize, Serialize};
7
8mod nodes;
9pub use nodes::{
10    Operator, PipelineNode, PipelineOperator, SegmentSink, SegmentSource, Service, ServiceBackend,
11    ServiceFrontend, Sink, Source,
12};
13
14pub mod context;
15pub mod error;
16pub mod network;
17pub use network::egress::addressed_router::{AddressedPushRouter, AddressedRequest};
18pub use network::egress::push_router::{PushRouter, RouterMode};
19pub mod registry;
20
21pub use crate::engine::{
22    self as engine, AsyncEngine, AsyncEngineContext, AsyncEngineContextProvider, Data, DataStream,
23    Engine, EngineStream, EngineUnary, ResponseStream, async_trait,
24};
25pub use anyhow::Error;
26pub use context::Context;
27pub use error::{PipelineError, PipelineErrorExt, TwoPartCodecError};
28
29/// Pipeline inputs carry a [`Context`] which can be used to carry metadata or additional information
30/// about the request. This information propagates through the stages, both local and distributed.
31pub type SingleIn<T> = Context<T>;
32
33/// Pipeline inputs carry a [`Context`] which can be used to carry metadata or additional information
34/// about the request. This information propagates through the stages, both local and distributed.
35pub type ManyIn<T> = Context<DataStream<T>>;
36
37/// Type alias for the output of pipeline that returns a single value
38pub type SingleOut<T> = EngineUnary<T>;
39
40/// Type alias for the output of pipeline that returns multiple values
41pub type ManyOut<T> = EngineStream<T>;
42
43pub type ServiceEngine<T, U> = Engine<T, U, Error>;
44
45/// Unary Engine is a pipeline that takes a single input and returns a single output
46pub type UnaryEngine<T, U> = ServiceEngine<SingleIn<T>, SingleOut<U>>;
47
48/// `ClientStreaming` Engine is a pipeline that takes multiple inputs and returns a single output
49/// Typically the engine will consume the entire input stream; however, it can also decided to exit
50/// early and emit a response without consuming the entire input stream.
51pub type ClientStreamingEngine<T, U> = ServiceEngine<ManyIn<T>, SingleOut<U>>;
52
53/// `ServerStreaming` takes a single input and returns multiple outputs.
54pub type ServerStreamingEngine<T, U> = ServiceEngine<SingleIn<T>, ManyOut<U>>;
55
56/// `BidirectionalStreaming` takes multiple inputs and returns multiple outputs. Input and output values
57/// are considered independent of each other; however, they could be constrained to be related.
58pub type BidirectionalStreamingEngine<T, U> = ServiceEngine<ManyIn<T>, ManyOut<U>>;
59
60pub trait AsyncTransportEngine<T: Data + PipelineIO, U: Data + PipelineIO>:
61    AsyncEngine<T, U, Error> + Send + Sync + 'static
62{
63}
64
65// pub type TransportEngine<T, U> = Arc<dyn AsyncTransportEngine<T, U>>;
66
67mod sealed {
68    use super::*;
69
70    #[allow(dead_code)]
71    pub struct Token;
72
73    pub trait Connectable {
74        type DataType: Data;
75    }
76
77    impl<T: Data> Connectable for Context<T> {
78        type DataType = T;
79    }
80    impl<T: Data> Connectable for EngineUnary<T> {
81        type DataType = T;
82    }
83    impl<T: Data> Connectable for EngineStream<T> {
84        type DataType = T;
85    }
86}
87
88pub trait PipelineIO: sealed::Connectable + AsyncEngineContextProvider + 'static {
89    fn id(&self) -> String;
90}
91
92impl<T: Data> PipelineIO for Context<T> {
93    fn id(&self) -> String {
94        self.id().to_string()
95    }
96}
97impl<T: Data> PipelineIO for EngineUnary<T> {
98    fn id(&self) -> String {
99        self.context().id().to_string()
100    }
101}
102impl<T: Data> PipelineIO for EngineStream<T> {
103    fn id(&self) -> String {
104        self.context().id().to_string()
105    }
106}
107
108#[derive(Serialize, Deserialize, Debug, Clone)]
109pub struct Event {
110    pub id: String,
111}