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// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16/// In a Pipeline, the [`AsyncEngine`] is constrained to take a [`Context`] as input and return
17/// a [`super::engine::ResponseStream`] as output.
18use serde::{Deserialize, Serialize};
19
20mod nodes;
21pub use nodes::{
22    Operator, PipelineNode, PipelineOperator, SegmentSink, SegmentSource, Service, ServiceBackend,
23    ServiceFrontend, Sink, Source,
24};
25
26pub mod context;
27pub mod error;
28pub mod network;
29pub mod registry;
30
31pub use crate::engine::{
32    self as engine, async_trait, AsyncEngine, AsyncEngineContext, AsyncEngineContextProvider, Data,
33    DataStream, Engine, EngineStream, EngineUnary, ResponseStream,
34};
35pub use anyhow::Error;
36pub use context::Context;
37pub use error::{PipelineError, PipelineErrorExt, TwoPartCodecError};
38
39/// Pipeline inputs carry a [`Context`] which can be used to carry metadata or additional information
40/// about the request. This information propagates through the stages, both local and distributed.
41pub type SingleIn<T> = Context<T>;
42
43/// Pipeline inputs carry a [`Context`] which can be used to carry metadata or additional information
44/// about the request. This information propagates through the stages, both local and distributed.
45pub type ManyIn<T> = Context<DataStream<T>>;
46
47/// Type alias for the output of pipeline that returns a single value
48pub type SingleOut<T> = EngineUnary<T>;
49
50/// Type alias for the output of pipeline that returns multiple values
51pub type ManyOut<T> = EngineStream<T>;
52
53pub type ServiceEngine<T, U> = Engine<T, U, Error>;
54
55/// Unary Engine is a pipeline that takes a single input and returns a single output
56pub type UnaryEngine<T, U> = ServiceEngine<SingleIn<T>, SingleOut<U>>;
57
58/// `ClientStreaming` Engine is a pipeline that takes multiple inputs and returns a single output
59/// Typically the engine will consume the entire input stream; however, it can also decided to exit
60/// early and emit a response without consuming the entire input stream.
61pub type ClientStreamingEngine<T, U> = ServiceEngine<ManyIn<T>, SingleOut<U>>;
62
63/// `ServerStreaming` takes a single input and returns multiple outputs.
64pub type ServerStreamingEngine<T, U> = ServiceEngine<SingleIn<T>, ManyOut<U>>;
65
66/// `BidirectionalStreaming` takes multiple inputs and returns multiple outputs. Input and output values
67/// are considered independent of each other; however, they could be constrained to be related.
68pub type BidirectionalStreamingEngine<T, U> = ServiceEngine<ManyIn<T>, ManyOut<U>>;
69
70pub trait AsyncTransportEngine<T: PipelineIO, U: PipelineIO>:
71    AsyncEngine<T, U, Error> + Send + Sync + 'static
72{
73}
74
75// pub type TransportEngine<T, U> = Arc<dyn AsyncTransportEngine<T, U>>;
76
77mod sealed {
78    use super::*;
79
80    #[allow(dead_code)]
81    pub struct Token;
82
83    pub trait Connectable {
84        type DataType: Data;
85    }
86
87    impl<T: Data> Connectable for Context<T> {
88        type DataType = T;
89    }
90    impl<T: Data> Connectable for EngineUnary<T> {
91        type DataType = T;
92    }
93    impl<T: Data> Connectable for EngineStream<T> {
94        type DataType = T;
95    }
96}
97
98pub trait PipelineIO: Data + sealed::Connectable + AsyncEngineContextProvider {
99    fn id(&self) -> String;
100}
101
102impl<T: Data> PipelineIO for Context<T> {
103    fn id(&self) -> String {
104        self.id().to_string()
105    }
106}
107impl<T: Data> PipelineIO for EngineUnary<T> {
108    fn id(&self) -> String {
109        self.context().id().to_string()
110    }
111}
112impl<T: Data> PipelineIO for EngineStream<T> {
113    fn id(&self) -> String {
114        self.context().id().to_string()
115    }
116}
117
118#[derive(Serialize, Deserialize, Debug, Clone)]
119pub struct Event {
120    pub id: String,
121}