distributed.rs 7.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Ryan Olson's avatar
Ryan Olson committed
15
16
17

pub use crate::component::Component;
use crate::{
18
    component::{self, ComponentBuilder, Endpoint, InstanceSource, Namespace},
Ryan Olson's avatar
Ryan Olson committed
19
20
21
22
23
24
    discovery::DiscoveryClient,
    service::ServiceClient,
    transports::{etcd, nats, tcp},
    ErrorContext,
};

25
use super::{error, Arc, DistributedRuntime, OnceCell, Result, Runtime, Weak, OK};
Ryan Olson's avatar
Ryan Olson committed
26
27
28

use derive_getters::Dissolve;
use figment::error;
29
30
use std::collections::HashMap;
use tokio::sync::Mutex;
31
use tokio_util::sync::CancellationToken;
Ryan Olson's avatar
Ryan Olson committed
32
33
34
35

impl DistributedRuntime {
    pub async fn new(runtime: Runtime, config: DistributedConfig) -> Result<Self> {
        let secondary = runtime.secondary();
36
        let (etcd_config, nats_config, is_static) = config.dissolve();
Ryan Olson's avatar
Ryan Olson committed
37
38
39

        let runtime_clone = runtime.clone();

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
        let etcd_client = if is_static {
            None
        } else {
            Some(
                secondary
                    .spawn(async move {
                        let client = etcd::Client::new(etcd_config.clone(), runtime_clone)
                            .await
                            .context(format!(
                                "Failed to connect to etcd server with config {:?}",
                                etcd_config
                            ))?;
                        OK(client)
                    })
                    .await??,
            )
        };
Ryan Olson's avatar
Ryan Olson committed
57
58
59
60
61
62
63
64
65
66
67

        let nats_client = secondary
            .spawn(async move {
                let client = nats_config.clone().connect().await.context(format!(
                    "Failed to connect to NATS server with config {:?}",
                    nats_config
                ))?;
                anyhow::Ok(client)
            })
            .await??;

68
        let distributed_runtime = Self {
Ryan Olson's avatar
Ryan Olson committed
69
70
71
72
73
            runtime,
            etcd_client,
            nats_client,
            tcp_server: Arc::new(OnceCell::new()),
            component_registry: component::Registry::new(),
74
            is_static,
75
            instance_sources: Arc::new(Mutex::new(HashMap::new())),
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
            start_time: std::time::Instant::now(),
        };

        // Start HTTP server for health and metrics (if enabled)
        let config = crate::config::RuntimeConfig::from_settings().unwrap_or_default();
        if config.http_server_enabled() {
            let drt_arc = Arc::new(distributed_runtime.clone());
            let runtime_clone = distributed_runtime.runtime.clone();
            secondary.spawn(async move {
                if let Err(e) = crate::http_server::start_http_server(
                    &config.http_server_host,
                    config.http_server_port,
                    runtime_clone.child_token(),
                    drt_arc,
                )
                .await
                {
                    tracing::error!("HTTP server startup failed: {}", e);
                } else {
                    tracing::debug!("HTTP server started successfully");
                }
            });
        } else {
            tracing::debug!(
                "Health and metrics HTTP server is disabled via DYN_RUNTIME_HTTP_ENABLED"
            );
        }

        Ok(distributed_runtime)
Ryan Olson's avatar
Ryan Olson committed
105
106
107
    }

    pub async fn from_settings(runtime: Runtime) -> Result<Self> {
108
109
110
111
112
113
114
        let config = DistributedConfig::from_settings(false);
        Self::new(runtime, config).await
    }

    // Call this if you are using static workers that do not need etcd-based discovery.
    pub async fn from_settings_without_discovery(runtime: Runtime) -> Result<Self> {
        let config = DistributedConfig::from_settings(true);
Ryan Olson's avatar
Ryan Olson committed
115
116
117
118
119
120
121
        Self::new(runtime, config).await
    }

    pub fn runtime(&self) -> &Runtime {
        &self.runtime
    }

122
123
124
125
    pub fn primary_token(&self) -> CancellationToken {
        self.runtime.primary_token()
    }

126
127
128
129
    /// The etcd lease all our components will be attached to.
    /// Not available for static workers.
    pub fn primary_lease(&self) -> Option<etcd::Lease> {
        self.etcd_client.as_ref().map(|c| c.primary_lease())
Ryan Olson's avatar
Ryan Olson committed
130
131
132
133
134
135
136
137
    }

    pub fn shutdown(&self) {
        self.runtime.shutdown();
    }

    /// Create a [`Namespace`]
    pub fn namespace(&self, name: impl Into<String>) -> Result<Namespace> {
138
        Namespace::new(self.clone(), name.into(), self.is_static)
Ryan Olson's avatar
Ryan Olson committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
    }

    // /// Create a [`Component`]
    // pub fn component(
    //     &self,
    //     name: impl Into<String>,
    //     namespace: impl Into<String>,
    // ) -> Result<Component> {
    //     Ok(ComponentBuilder::from_runtime(self.clone())
    //         .name(name.into())
    //         .namespace(namespace.into())
    //         .build()?)
    // }

    pub(crate) fn discovery_client(&self, namespace: impl Into<String>) -> DiscoveryClient {
154
155
156
157
158
159
        DiscoveryClient::new(
            namespace.into(),
            self.etcd_client
                .clone()
                .expect("Attempt to get discovery_client on static DistributedRuntime"),
        )
Ryan Olson's avatar
Ryan Olson committed
160
161
162
163
164
165
    }

    pub(crate) fn service_client(&self) -> ServiceClient {
        ServiceClient::new(self.nats_client.clone())
    }

166
    pub async fn tcp_server(&self) -> Result<Arc<tcp::server::TcpStreamServer>> {
Ryan Olson's avatar
Ryan Olson committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
        Ok(self
            .tcp_server
            .get_or_try_init(async move {
                let options = tcp::server::ServerOptions::default();
                let server = tcp::server::TcpStreamServer::new(options).await?;
                OK(server)
            })
            .await?
            .clone())
    }

    pub fn nats_client(&self) -> nats::Client {
        self.nats_client.clone()
    }

182
    // todo(ryan): deprecate this as we move to Discovery traits and Component Identifiers
183
    pub fn etcd_client(&self) -> Option<etcd::Client> {
Ryan Olson's avatar
Ryan Olson committed
184
185
        self.etcd_client.clone()
    }
186
187
188
189

    pub fn child_token(&self) -> CancellationToken {
        self.runtime.child_token()
    }
190
191
192
193

    pub fn instance_sources(&self) -> Arc<Mutex<HashMap<Endpoint, Weak<InstanceSource>>>> {
        self.instance_sources.clone()
    }
194
195
196
197
198

    /// Get the uptime of this DistributedRuntime in seconds
    pub fn uptime(&self) -> std::time::Duration {
        self.start_time.elapsed()
    }
Ryan Olson's avatar
Ryan Olson committed
199
200
201
202
203
204
}

#[derive(Dissolve)]
pub struct DistributedConfig {
    pub etcd_config: etcd::ClientOptions,
    pub nats_config: nats::ClientOptions,
205
    pub is_static: bool,
Ryan Olson's avatar
Ryan Olson committed
206
207
208
}

impl DistributedConfig {
209
    pub fn from_settings(is_static: bool) -> DistributedConfig {
Ryan Olson's avatar
Ryan Olson committed
210
211
212
        DistributedConfig {
            etcd_config: etcd::ClientOptions::default(),
            nats_config: nats::ClientOptions::default(),
213
            is_static,
Ryan Olson's avatar
Ryan Olson committed
214
215
        }
    }
Ryan Olson's avatar
Ryan Olson committed
216
217
218
219
220

    pub fn for_cli() -> DistributedConfig {
        let mut config = DistributedConfig {
            etcd_config: etcd::ClientOptions::default(),
            nats_config: nats::ClientOptions::default(),
221
            is_static: false,
Ryan Olson's avatar
Ryan Olson committed
222
223
224
225
226
227
        };

        config.etcd_config.attach_lease = false;

        config
    }
Ryan Olson's avatar
Ryan Olson committed
228
}