distributed.rs 12.8 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

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

27
use super::{error, Arc, DistributedRuntime, OnceCell, Result, Runtime, SystemHealth, Weak, OK};
28
use std::sync::OnceLock;
Ryan Olson's avatar
Ryan Olson committed
29
30
31

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

36
37
38
39
40
41
42
43
44
45
impl MetricsRegistry for DistributedRuntime {
    fn basename(&self) -> String {
        "".to_string() // drt has no basename. Basename only begins with the Namespace.
    }

    fn parent_hierarchy(&self) -> Vec<String> {
        vec![] // drt is the root, so no parent hierarchy
    }
}

Ryan Olson's avatar
Ryan Olson committed
46
47
impl DistributedRuntime {
    pub async fn new(runtime: Runtime, config: DistributedConfig) -> Result<Self> {
48
        let (etcd_config, nats_config, is_static) = config.dissolve();
Ryan Olson's avatar
Ryan Olson committed
49
50
51

        let runtime_clone = runtime.clone();

52
53
54
        let etcd_client = if is_static {
            None
        } else {
55
            Some(etcd::Client::new(etcd_config.clone(), runtime_clone).await?)
56
        };
Ryan Olson's avatar
Ryan Olson committed
57

58
        let nats_client = nats_config.clone().connect().await?;
Ryan Olson's avatar
Ryan Olson committed
59

60
        // Start system status server for health and metrics if enabled in configuration
61
62
63
64
65
66
67
68
        let config = crate::config::RuntimeConfig::from_settings().unwrap_or_default();
        // IMPORTANT: We must extract cancel_token from runtime BEFORE moving runtime into the struct below.
        // This is because after moving, runtime is no longer accessible in this scope (ownership rules).
        let cancel_token = if config.system_server_enabled() {
            Some(runtime.clone().child_token())
        } else {
            None
        };
69
70
        let starting_health_status = config.starting_health_status.clone();
        let use_endpoint_health_status = config.use_endpoint_health_status.clone();
71
72
73
        let health_endpoint_path = config.system_health_path.clone();
        let live_endpoint_path = config.system_live_path.clone();
        let system_health = Arc::new(std::sync::Mutex::new(SystemHealth::new(
74
75
            starting_health_status,
            use_endpoint_health_status,
76
77
            health_endpoint_path,
            live_endpoint_path,
78
        )));
79

80
81
        let nats_client_for_metrics = nats_client.clone();

82
        let distributed_runtime = Self {
Ryan Olson's avatar
Ryan Olson committed
83
84
85
86
            runtime,
            etcd_client,
            nats_client,
            tcp_server: Arc::new(OnceCell::new()),
87
            system_status_server: Arc::new(OnceLock::new()),
Ryan Olson's avatar
Ryan Olson committed
88
            component_registry: component::Registry::new(),
89
            is_static,
90
            instance_sources: Arc::new(Mutex::new(HashMap::new())),
91
            hierarchy_to_metricsregistry: Arc::new(std::sync::RwLock::new(HashMap::<
92
                String,
93
                crate::MetricsRegistryEntry,
94
            >::new())),
95
            system_health,
96
97
        };

98
        let nats_client_metrics = DRTNatsClientPrometheusMetrics::new(
99
100
101
102
103
104
            &distributed_runtime,
            nats_client_for_metrics.client().clone(),
        )?;
        let mut drt_hierarchies = distributed_runtime.parent_hierarchy();
        drt_hierarchies.push(distributed_runtime.hierarchy());
        // Register a callback to update NATS client metrics
105
106
        let nats_client_callback = Arc::new({
            let nats_client_clone = nats_client_metrics.clone();
107
            move || {
108
                nats_client_clone.set_from_client_stats();
109
110
111
                Ok(())
            }
        });
112
        distributed_runtime.register_metrics_callback(drt_hierarchies, nats_client_callback);
113

114
        // Handle system status server initialization
115
        if let Some(cancel_token) = cancel_token {
116
            // System server is enabled - start both the state and HTTP server
117
118
119
            let host = config.system_host.clone();
            let port = config.system_port;

120
            // Start system status server (it creates SystemStatusState internally)
121
            match crate::system_status_server::spawn_system_status_server(
122
123
124
125
                &host,
                port,
                cancel_token,
                Arc::new(distributed_runtime.clone()),
126
127
128
            )
            .await
            {
129
                Ok((addr, handle)) => {
130
                    tracing::info!("System status server started successfully on {}", addr);
131

132
133
134
135
136
137
                    // Store system status server information
                    let system_status_server_info =
                        crate::system_status_server::SystemStatusServerInfo::new(
                            addr,
                            Some(handle),
                        );
138

139
                    // Initialize the system_status_server field
140
                    distributed_runtime
141
142
143
                        .system_status_server
                        .set(Arc::new(system_status_server_info))
                        .expect("System status server info should only be set once");
144
145
                }
                Err(e) => {
146
                    tracing::error!("System status server startup failed: {}", e);
147
                }
148
            }
149
        } else {
150
151
152
153
154
155
156
157
158
159
160
161
            // System server HTTP is disabled, but still create the state for metrics
            // This ensures uptime_seconds metric is always registered
            let system_status_state = crate::system_status_server::SystemStatusState::new(
                Arc::new(distributed_runtime.clone()),
            )?;

            // Initialize the start time for uptime tracking
            if let Err(e) = system_status_state.initialize_start_time() {
                tracing::warn!("Failed to initialize system status start time: {}", e);
            }

            tracing::debug!("System status server HTTP endpoints disabled, but uptime metrics are being tracked");
162
163
164
        }

        Ok(distributed_runtime)
Ryan Olson's avatar
Ryan Olson committed
165
166
167
    }

    pub async fn from_settings(runtime: Runtime) -> Result<Self> {
168
169
170
171
172
173
174
        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
175
176
177
178
179
180
181
        Self::new(runtime, config).await
    }

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

182
183
184
185
    pub fn primary_token(&self) -> CancellationToken {
        self.runtime.primary_token()
    }

186
187
188
189
    /// 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
190
191
192
193
194
195
196
197
    }

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

    /// Create a [`Namespace`]
    pub fn namespace(&self, name: impl Into<String>) -> Result<Namespace> {
198
        Namespace::new(self.clone(), name.into(), self.is_static)
Ryan Olson's avatar
Ryan Olson committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    }

    // /// 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 {
214
215
216
217
218
219
        DiscoveryClient::new(
            namespace.into(),
            self.etcd_client
                .clone()
                .expect("Attempt to get discovery_client on static DistributedRuntime"),
        )
Ryan Olson's avatar
Ryan Olson committed
220
221
222
223
224
225
    }

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

226
    pub async fn tcp_server(&self) -> Result<Arc<tcp::server::TcpStreamServer>> {
Ryan Olson's avatar
Ryan Olson committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
        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()
    }

242
243
244
245
246
    /// Get system status server information if available
    pub fn system_status_server_info(
        &self,
    ) -> Option<Arc<crate::system_status_server::SystemStatusServerInfo>> {
        self.system_status_server.get().cloned()
247
248
    }

249
    // todo(ryan): deprecate this as we move to Discovery traits and Component Identifiers
250
    pub fn etcd_client(&self) -> Option<etcd::Client> {
Ryan Olson's avatar
Ryan Olson committed
251
252
        self.etcd_client.clone()
    }
253
254
255
256

    pub fn child_token(&self) -> CancellationToken {
        self.runtime.child_token()
    }
257
258
259
260

    pub fn instance_sources(&self) -> Arc<Mutex<HashMap<Endpoint, Weak<InstanceSource>>>> {
        self.instance_sources.clone()
    }
261

262
263
    /// Add a Prometheus metric to a specific hierarchy's registry. Note that it is possible
    /// to register the same metric name multiple times, as long as the labels are different.
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
    pub fn add_prometheus_metric(
        &self,
        hierarchy: &str,
        metric_name: &str,
        prometheus_metric: Box<dyn prometheus::core::Collector>,
    ) -> anyhow::Result<()> {
        let mut registries = self.hierarchy_to_metricsregistry.write().unwrap();
        let entry = registries.entry(hierarchy.to_string()).or_default();

        // Try to register the metric and provide better error information
        match entry.prometheus_registry.register(prometheus_metric) {
            Ok(_) => Ok(()),
            Err(e) => {
                let error_msg = e.to_string();
                tracing::error!(
                    hierarchy = ?hierarchy,
                    error = ?error_msg,
                    metric_name = ?metric_name,
                    "Metric registration failed"
                );
                Err(e.into())
            }
        }
    }

    /// Add a callback function to metrics registries for the given hierarchies
    pub fn register_metrics_callback(&self, hierarchies: Vec<String>, callback: RuntimeCallback) {
        let mut registries = self.hierarchy_to_metricsregistry.write().unwrap();
        for hierarchy in hierarchies {
            registries
                .entry(hierarchy)
                .or_default()
                .add_callback(callback.clone());
        }
    }

    /// Execute all callbacks for a given hierarchy key and return their results
    pub fn execute_metrics_callbacks(&self, hierarchy: &str) -> Vec<anyhow::Result<()>> {
        // Clone callbacks while holding read lock (fast operation)
        let callbacks = {
            let registries = self.hierarchy_to_metricsregistry.read().unwrap();
            registries
                .get(hierarchy)
                .map(|entry| entry.runtime_callbacks.clone())
        }; // Read lock released here

        // Execute callbacks without holding the lock
        match callbacks {
            Some(callbacks) => callbacks.iter().map(|callback| callback()).collect(),
            None => Vec::new(),
        }
    }

    /// Get all registered hierarchy keys. Private because it is only used for testing.
    fn get_registered_hierarchies(&self) -> Vec<String> {
        let registries = self.hierarchy_to_metricsregistry.read().unwrap();
        registries.keys().cloned().collect()
    }
Ryan Olson's avatar
Ryan Olson committed
322
323
324
325
326
327
}

#[derive(Dissolve)]
pub struct DistributedConfig {
    pub etcd_config: etcd::ClientOptions,
    pub nats_config: nats::ClientOptions,
328
    pub is_static: bool,
Ryan Olson's avatar
Ryan Olson committed
329
330
331
}

impl DistributedConfig {
332
    pub fn from_settings(is_static: bool) -> DistributedConfig {
Ryan Olson's avatar
Ryan Olson committed
333
334
335
        DistributedConfig {
            etcd_config: etcd::ClientOptions::default(),
            nats_config: nats::ClientOptions::default(),
336
            is_static,
Ryan Olson's avatar
Ryan Olson committed
337
338
        }
    }
Ryan Olson's avatar
Ryan Olson committed
339
340
341
342
343

    pub fn for_cli() -> DistributedConfig {
        let mut config = DistributedConfig {
            etcd_config: etcd::ClientOptions::default(),
            nats_config: nats::ClientOptions::default(),
344
            is_static: false,
Ryan Olson's avatar
Ryan Olson committed
345
346
347
348
349
350
        };

        config.etcd_config.attach_lease = false;

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