triton_distributed/
lib.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//! Triton
17
18#![allow(dead_code)]
19#![allow(unused_imports)]
20
21use std::sync::{Arc, Mutex};
22
23pub use anyhow::{anyhow as error, Context as ErrorContext, Error, Ok as OK, Result};
24
25use async_once_cell::OnceCell;
26use tracing as log;
27
28mod config;
29pub use config::RuntimeConfig;
30
31pub mod component;
32pub mod discovery;
33pub mod engine;
34pub mod pipeline;
35pub mod protocols;
36pub mod runtime;
37pub mod service;
38pub mod transports;
39pub mod worker;
40
41pub mod distributed;
42
43pub use tokio_util::sync::CancellationToken;
44pub use worker::Worker;
45
46/// Types of Tokio runtimes that can be used to construct a Triton [Runtime].
47#[derive(Clone)]
48enum RuntimeType {
49    Shared(Arc<tokio::runtime::Runtime>),
50    External(tokio::runtime::Handle),
51}
52
53/// Local [Runtime] which provides access to shared resources local to the physical node/machine.
54#[derive(Debug, Clone)]
55pub struct Runtime {
56    id: Arc<String>,
57    primary: RuntimeType,
58    secondary: Arc<tokio::runtime::Runtime>,
59    cancellation_token: CancellationToken,
60}
61
62/// Distributed [Runtime] which provides access to shared resources across the cluster, this includes
63/// communication protocols and transports.
64#[derive(Clone)]
65pub struct DistributedRuntime {
66    // local runtime
67    runtime: Runtime,
68
69    // we might consider a unifed transport manager here
70    etcd_client: transports::etcd::Client,
71    nats_client: transports::nats::Client,
72    tcp_server: Arc<OnceCell<Arc<transports::tcp::server::TcpStreamServer>>>,
73
74    // local registry for components
75    // the registry allows us to use share runtime resources across instances of the same component object.
76    // take fo example two instances of a client to the same remote component. The registry allows us to use
77    // a single endpoint watcher for both clients, this keeps the number background tasking watching specific
78    // paths in etcd to a minimum.
79    component_registry: component::Registry,
80}