distributed.rs 28.4 KB
Newer Older
1
2
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
Ryan Olson's avatar
Ryan Olson committed
3

4
use crate::component::{Component, Instance};
5
use crate::pipeline::PipelineError;
6
7
use crate::pipeline::network::manager::NetworkManager;
use crate::service::{ComponentNatsServerPrometheusMetrics, ServiceClient, ServiceSet};
8
use crate::storage::key_value_store::{
9
10
    EtcdStore, KeyValueStore, KeyValueStoreEnum, KeyValueStoreManager, KeyValueStoreSelect,
    MemoryStore,
11
};
12
use crate::transports::nats::DRTNatsClientPrometheusMetrics;
Ryan Olson's avatar
Ryan Olson committed
13
use crate::{
14
    component::{self, ComponentBuilder, Endpoint, Namespace},
15
    discovery::Discovery,
16
17
    metrics::PrometheusUpdateCallback,
    metrics::{MetricsHierarchy, MetricsRegistry},
Ryan Olson's avatar
Ryan Olson committed
18
19
    transports::{etcd, nats, tcp},
};
20
use crate::{discovery, system_status_server, transports};
Ryan Olson's avatar
Ryan Olson committed
21

22
use super::utils::GracefulShutdownTracker;
23
24
use crate::SystemHealth;
use crate::runtime::Runtime;
Ryan Olson's avatar
Ryan Olson committed
25

26
// Used instead of std::cell::OnceCell because get_or_try_init there is nightly
27
use async_once_cell::OnceCell;
28

29
use std::fmt;
30
use std::sync::{Arc, OnceLock, Weak};
31
use std::time::Duration;
32
use tokio::sync::watch::Receiver;
33
34

use anyhow::Result;
Ryan Olson's avatar
Ryan Olson committed
35
36
use derive_getters::Dissolve;
use figment::error;
37
38
use std::collections::HashMap;
use tokio::sync::Mutex;
39
use tokio_util::sync::CancellationToken;
Ryan Olson's avatar
Ryan Olson committed
40

41
42
type InstanceMap = HashMap<Endpoint, Weak<Receiver<Vec<Instance>>>>;

43
44
45
46
47
48
49
50
51
/// Distributed [Runtime] which provides access to shared resources across the cluster, this includes
/// communication protocols and transports.
#[derive(Clone)]
pub struct DistributedRuntime {
    // local runtime
    runtime: Runtime,

    nats_client: Option<transports::nats::Client>,
    store: KeyValueStoreManager,
52
    network_manager: Arc<NetworkManager>,
53
54
    tcp_server: Arc<OnceCell<Arc<transports::tcp::server::TcpStreamServer>>>,
    system_status_server: Arc<OnceLock<Arc<system_status_server::SystemStatusServerInfo>>>,
55
    request_plane: RequestPlaneMode,
56
57
58
59

    // Service discovery client
    discovery_client: Arc<dyn discovery::Discovery>,

60
61
62
63
    // Discovery metadata (only used for Kubernetes backend)
    // Shared with system status server to expose via /metadata endpoint
    discovery_metadata: Option<Arc<tokio::sync::RwLock<discovery::DiscoveryMetadata>>>,

64
65
66
67
68
69
70
    // local registry for components
    // the registry allows us to use share runtime resources across instances of the same component object.
    // take for example two instances of a client to the same remote component. The registry allows us to use
    // a single endpoint watcher for both clients, this keeps the number background tasking watching specific
    // paths in etcd to a minimum.
    component_registry: component::Registry,

71
    instance_sources: Arc<tokio::sync::Mutex<InstanceMap>>,
72
73
74
75
76
77
78
79

    // Health Status
    system_health: Arc<parking_lot::Mutex<SystemHealth>>,

    // This hierarchy's own metrics registry
    metrics_registry: MetricsRegistry,
}

80
impl MetricsHierarchy for DistributedRuntime {
81
82
83
84
    fn basename(&self) -> String {
        "".to_string() // drt has no basename. Basename only begins with the Namespace.
    }

85
86
87
88
89
90
    fn parent_hierarchies(&self) -> Vec<&dyn MetricsHierarchy> {
        vec![] // drt is the root, so no parent hierarchies
    }

    fn get_metrics_registry(&self) -> &MetricsRegistry {
        &self.metrics_registry
91
92
93
    }
}

Ryan Olson's avatar
Ryan Olson committed
94
95
96
97
98
99
impl std::fmt::Debug for DistributedRuntime {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "DistributedRuntime")
    }
}

Ryan Olson's avatar
Ryan Olson committed
100
101
impl DistributedRuntime {
    pub async fn new(runtime: Runtime, config: DistributedConfig) -> Result<Self> {
102
        let (selected_kv_store, nats_config, request_plane) = config.dissolve();
Ryan Olson's avatar
Ryan Olson committed
103
104
105

        let runtime_clone = runtime.clone();

106
        let store = match selected_kv_store {
107
            KeyValueStoreSelect::Etcd(etcd_config) => {
108
109
110
111
                let etcd_client = etcd::Client::new(*etcd_config, runtime_clone).await.inspect_err(|err|
                    // The returned error doesn't show because of a dropped runtime error, so
                    // log it first.
                    tracing::error!(%err, "Could not connect to etcd. Pass `--store-kv ..` to use a different backend or start etcd."))?;
112
                KeyValueStoreManager::etcd(etcd_client)
113
            }
114
115
            KeyValueStoreSelect::File(root) => KeyValueStoreManager::file(root),
            KeyValueStoreSelect::Memory => KeyValueStoreManager::memory(),
116
        };
Ryan Olson's avatar
Ryan Olson committed
117

118
119
120
121
        let nats_client = match nats_config {
            Some(nc) => Some(nc.connect().await?),
            None => None,
        };
Ryan Olson's avatar
Ryan Olson committed
122

123
        // Start system status server for health and metrics if enabled in configuration
124
125
126
127
128
129
130
131
        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
        };
132
133
        let starting_health_status = config.starting_health_status.clone();
        let use_endpoint_health_status = config.use_endpoint_health_status.clone();
134
135
        let health_endpoint_path = config.system_health_path.clone();
        let live_endpoint_path = config.system_live_path.clone();
136
        let system_health = Arc::new(parking_lot::Mutex::new(SystemHealth::new(
137
138
            starting_health_status,
            use_endpoint_health_status,
139
140
            health_endpoint_path,
            live_endpoint_path,
141
        )));
142

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
        // Initialize discovery client based on backend configuration
        let discovery_backend =
            std::env::var("DYN_DISCOVERY_BACKEND").unwrap_or_else(|_| "kv_store".to_string());

        let (discovery_client, discovery_metadata) = match discovery_backend.as_str() {
            "kubernetes" => {
                tracing::info!("Initializing Kubernetes discovery backend");
                let metadata = Arc::new(tokio::sync::RwLock::new(
                    crate::discovery::DiscoveryMetadata::new(),
                ));
                let client = crate::discovery::KubeDiscoveryClient::new(
                    metadata.clone(),
                    runtime.primary_token(),
                )
                .await
                .inspect_err(
                    |err| tracing::error!(%err, "Failed to initialize Kubernetes discovery client"),
                )?;
                (Arc::new(client) as Arc<dyn Discovery>, Some(metadata))
            }
            _ => {
                tracing::info!("Initializing KV store discovery backend");
                use crate::discovery::KVStoreDiscovery;
                (
                    Arc::new(KVStoreDiscovery::new(
                        store.clone(),
                        runtime.primary_token(),
                    )) as Arc<dyn Discovery>,
                    None,
                )
            }
174
175
        };

176
        let component_registry = component::Registry::new();
177
178
        let nats_client_for_metrics = nats_client.clone();

179
180
181
182
183
184
185
186
        // NetworkManager for request plane
        let network_manager = NetworkManager::new(
            runtime.child_token(),
            nats_client.clone().map(|c| c.client().clone()),
            component_registry.clone(),
            request_plane,
        );

187
        let distributed_runtime = Self {
Ryan Olson's avatar
Ryan Olson committed
188
            runtime,
189
            store,
190
            network_manager: Arc::new(network_manager),
Ryan Olson's avatar
Ryan Olson committed
191
192
            nats_client,
            tcp_server: Arc::new(OnceCell::new()),
193
            system_status_server: Arc::new(OnceLock::new()),
194
            discovery_client,
195
            discovery_metadata,
196
            component_registry,
197
            instance_sources: Arc::new(Mutex::new(HashMap::new())),
198
            metrics_registry: crate::MetricsRegistry::new(),
199
            system_health,
200
            request_plane,
201
202
        };

203
204
205
206
207
        if let Some(nats_client_for_metrics) = nats_client_for_metrics {
            let nats_client_metrics = DRTNatsClientPrometheusMetrics::new(
                &distributed_runtime,
                nats_client_for_metrics.client().clone(),
            )?;
208
            // Register a callback to update NATS client metrics on the DRT's metrics registry
209
210
211
212
213
214
215
216
            let nats_client_callback = Arc::new({
                let nats_client_clone = nats_client_metrics.clone();
                move || {
                    nats_client_clone.set_from_client_stats();
                    Ok(())
                }
            });
            distributed_runtime
217
218
                .metrics_registry
                .add_update_callback(nats_client_callback);
219
        }
220

221
222
223
224
225
226
        // Initialize the uptime gauge in SystemHealth
        distributed_runtime
            .system_health
            .lock()
            .initialize_uptime_gauge(&distributed_runtime)?;

227
        // Handle system status server initialization
228
        if let Some(cancel_token) = cancel_token {
229
            // System server is enabled - start both the state and HTTP server
230
            let host = config.system_host.clone();
231
            let port = config.system_port as u16;
232

233
            // Start system status server (it creates SystemStatusState internally)
234
            match crate::system_status_server::spawn_system_status_server(
235
236
237
238
                &host,
                port,
                cancel_token,
                Arc::new(distributed_runtime.clone()),
239
                distributed_runtime.discovery_metadata.clone(),
240
241
242
            )
            .await
            {
243
                Ok((addr, handle)) => {
244
                    tracing::info!("System status server started successfully on {}", addr);
245

246
247
248
249
250
251
                    // Store system status server information
                    let system_status_server_info =
                        crate::system_status_server::SystemStatusServerInfo::new(
                            addr,
                            Some(handle),
                        );
252

253
                    // Initialize the system_status_server field
254
                    distributed_runtime
255
256
257
                        .system_status_server
                        .set(Arc::new(system_status_server_info))
                        .expect("System status server info should only be set once");
258
259
                }
                Err(e) => {
260
                    tracing::error!("System status server startup failed: {}", e);
261
                }
262
            }
263
        } else {
264
            // System server HTTP is disabled, but uptime metrics are still being tracked via SystemHealth
265
266
267
            tracing::debug!(
                "System status server HTTP endpoints disabled, but uptime metrics are being tracked"
            );
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
        // Start health check manager if enabled
        if config.health_check_enabled {
            let health_check_config = crate::health_check::HealthCheckConfig {
                canary_wait_time: std::time::Duration::from_secs(config.canary_wait_time_secs),
                request_timeout: std::time::Duration::from_secs(
                    config.health_check_request_timeout_secs,
                ),
            };

            // Start the health check manager (spawns per-endpoint monitoring tasks)
            match crate::health_check::start_health_check_manager(
                distributed_runtime.clone(),
                Some(health_check_config),
            )
            .await
            {
                Ok(()) => tracing::info!(
                    "Health check manager started (canary_wait_time: {}s, request_timeout: {}s)",
                    config.canary_wait_time_secs,
                    config.health_check_request_timeout_secs
                ),
                Err(e) => tracing::error!("Health check manager failed to start: {}", e),
            }
        }

295
        Ok(distributed_runtime)
Ryan Olson's avatar
Ryan Olson committed
296
297
298
    }

    pub async fn from_settings(runtime: Runtime) -> Result<Self> {
299
        let config = DistributedConfig::from_settings();
Ryan Olson's avatar
Ryan Olson committed
300
301
302
303
304
305
306
        Self::new(runtime, config).await
    }

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

307
308
309
310
    pub fn primary_token(&self) -> CancellationToken {
        self.runtime.primary_token()
    }

311
312
313
314
315
316
317
318
319
320
321
    // TODO: Don't hand out pointers, instead have methods to use the registry in friendly ways
    // (without being aware of async locks and so on)
    pub fn component_registry(&self) -> &component::Registry {
        &self.component_registry
    }

    // TODO: Don't hand out pointers, instead provide system health related services.
    pub fn system_health(&self) -> Arc<parking_lot::Mutex<SystemHealth>> {
        self.system_health.clone()
    }

322
    pub fn connection_id(&self) -> u64 {
323
        self.discovery_client.instance_id()
Ryan Olson's avatar
Ryan Olson committed
324
325
326
327
    }

    pub fn shutdown(&self) {
        self.runtime.shutdown();
328
        self.store.shutdown();
Ryan Olson's avatar
Ryan Olson committed
329
330
331
332
    }

    /// Create a [`Namespace`]
    pub fn namespace(&self, name: impl Into<String>) -> Result<Namespace> {
333
        Namespace::new(self.clone(), name.into())
Ryan Olson's avatar
Ryan Olson committed
334
335
    }

336
337
338
    /// Returns the discovery interface for service registration and discovery
    pub fn discovery(&self) -> Arc<dyn Discovery> {
        self.discovery_client.clone()
339
340
    }

341
    pub async fn tcp_server(&self) -> Result<Arc<tcp::server::TcpStreamServer>> {
Ryan Olson's avatar
Ryan Olson committed
342
343
344
345
346
        Ok(self
            .tcp_server
            .get_or_try_init(async move {
                let options = tcp::server::ServerOptions::default();
                let server = tcp::server::TcpStreamServer::new(options).await?;
347
                Ok::<_, PipelineError>(server)
Ryan Olson's avatar
Ryan Olson committed
348
349
350
351
352
            })
            .await?
            .clone())
    }

353
    /// Get the network manager
354
355
356
    ///
    /// The network manager consolidates all network configuration and provides
    /// unified access to request plane servers and clients.
357
358
    pub fn network_manager(&self) -> Arc<NetworkManager> {
        self.network_manager.clone()
359
360
361
362
363
364
365
366
367
    }

    /// Get the request plane server (convenience method)
    ///
    /// This is a shortcut for `network_manager().await?.server().await`.
    pub async fn request_plane_server(
        &self,
    ) -> Result<Arc<dyn crate::pipeline::network::ingress::unified_server::RequestPlaneServer>>
    {
368
        self.network_manager().server().await
Ryan Olson's avatar
Ryan Olson committed
369
370
    }

371
372
373
374
375
    /// 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()
376
377
    }

378
    /// An interface to store things outside of the process. Usually backed by something like etcd.
379
    /// Currently does key-value, but will grow to include whatever we need to store.
380
381
    pub fn store(&self) -> &KeyValueStoreManager {
        &self.store
382
383
    }

384
385
386
387
388
    /// How the frontend should talk to the backend.
    pub fn request_plane(&self) -> RequestPlaneMode {
        self.request_plane
    }

389
390
391
    pub fn child_token(&self) -> CancellationToken {
        self.runtime.child_token()
    }
392

393
394
395
396
    pub(crate) fn graceful_shutdown_tracker(&self) -> Arc<GracefulShutdownTracker> {
        self.runtime.graceful_shutdown_tracker()
    }

397
    pub fn instance_sources(&self) -> Arc<Mutex<InstanceMap>> {
398
399
        self.instance_sources.clone()
    }
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544

    /// TODO: This is a temporary KV router measure for component/component.rs EventPublisher impl for
    /// Component, to allow it to publish to NATS. KV Router is the only user.
    pub(crate) async fn kv_router_nats_publish(
        &self,
        subject: String,
        payload: bytes::Bytes,
    ) -> anyhow::Result<()> {
        let Some(nats_client) = self.nats_client.as_ref() else {
            anyhow::bail!("KV router's EventPublisher requires NATS");
        };
        Ok(nats_client.client().publish(subject, payload).await?)
    }

    /// TODO: This is a temporary KV router measure for component/component.rs EventSubscriber impl for
    /// Component, to allow it to subscribe to NATS. KV Router is the only user.
    pub(crate) async fn kv_router_nats_subscribe(
        &self,
        subject: String,
    ) -> Result<async_nats::Subscriber> {
        let Some(nats_client) = self.nats_client.as_ref() else {
            anyhow::bail!("KV router's EventSubscriber requires NATS");
        };
        Ok(nats_client.client().subscribe(subject).await?)
    }

    /// Start NATS metrics service in the background to isolate the async,
    /// and because we don't need it yet.
    /// TODO: This and the things it calls should be in a nats module somewhere.
    pub fn start_stats_service(&self, component: Component) {
        let drt = self.clone();
        self.runtime().secondary().spawn(async move {
            let service_name = component.service_name();
            if let Err(err) = drt.add_stats_service(component).await {
                tracing::error!(error = %err, component = service_name, "Failed starting stats service");
            }
        });
    }

    /// Gather NATS metrics
    async fn add_stats_service(&self, component: Component) -> anyhow::Result<()> {
        let service_name = component.service_name();

        // Pre-check to save cost of creating the service, but don't hold the lock
        if self
            .component_registry()
            .inner
            .lock()
            .await
            .services
            .contains_key(&service_name)
        {
            // The NATS service is per component, but it is called from `serve_endpoint`, and there
            // are often multiple endpoints for a component (e.g. `clear_kv_blocks` and `generate`).
            tracing::trace!("Service {service_name} already exists");
            return Ok(());
        }

        let Some(nats_client) = self.nats_client.as_ref() else {
            anyhow::bail!("Cannot create NATS service without NATS.");
        };
        let description = None;
        let (nats_service, stats_reg) =
            crate::component::service::build_nats_service(nats_client, &component, description)
                .await?;

        let mut guard = self.component_registry().inner.lock().await;
        if !guard.services.contains_key(&service_name) {
            // Normal case
            guard.services.insert(service_name.clone(), nats_service);
            guard.stats_handlers.insert(service_name.clone(), stats_reg);

            tracing::info!("Added NATS / stats service {service_name}");

            drop(guard);
        } else {
            drop(guard);
            let _ = nats_service.stop().await;
            // The NATS service is per component, but it is called from `serve_endpoint`, and there
            // are often multiple endpoints for a component (e.g. `clear_kv_blocks` and `generate`).
            // TODO: Is this still true?
            return Ok(());
        }

        let cancel_token = self.primary_token();
        let service_client = self
            .nats_client
            .as_ref()
            .map(|nc| ServiceClient::new(nc.clone()))
            .ok_or_else(|| {
                anyhow::anyhow!("Stats service requires NATS client to collect service metrics.")
            })?;
        // If there is another component with the same service name, this will fail.
        let component_metrics = ComponentNatsServerPrometheusMetrics::new(&component)?;

        self.runtime().secondary().spawn(nats_metrics_worker(
            cancel_token,
            service_client,
            component_metrics,
            component,
        ));
        Ok(())
    }
}

/// Add Prometheus metrics for this component's NATS service stats.
///
/// Starts a background task that periodically requests service statistics from NATS
/// and updates the corresponding Prometheus metrics. The first scrape happens immediately,
/// then subsequent scrapes occur at a fixed interval of 9.8 seconds (MAX_WAIT_MS),
/// which should be near or smaller than typical Prometheus scraping intervals to ensure
/// metrics are fresh when Prometheus collects them.
async fn nats_metrics_worker(
    cancel_token: CancellationToken,
    service_client: ServiceClient,
    component_metrics: ComponentNatsServerPrometheusMetrics,
    component: Component,
) {
    const MAX_WAIT_MS: Duration = Duration::from_millis(9800); // Should be <= Prometheus scrape interval
    let timeout = Duration::from_millis(500);
    let mut interval = tokio::time::interval(MAX_WAIT_MS);
    interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);

    let service_name = component.service_name();
    loop {
        tokio::select! {
            result = service_client.collect_services(&service_name, timeout) => {
                match result {
                    Ok(service_set) => {
                        component_metrics.update_from_service_set(&service_set);
                    }
                    Err(err) => {
                        tracing::error!("Background scrape failed for {service_name}: {err}",);
                        component_metrics.reset_to_zeros();
                    }
                }
            }
            _ = cancel_token.cancelled() => {
                tracing::trace!("nats_metrics_worker stopped");
                break;
            }
        }

        interval.tick().await;
    }
Ryan Olson's avatar
Ryan Olson committed
545
546
547
548
}

#[derive(Dissolve)]
pub struct DistributedConfig {
549
    pub store_backend: KeyValueStoreSelect,
550
    pub nats_config: Option<nats::ClientOptions>,
551
    pub request_plane: RequestPlaneMode,
Ryan Olson's avatar
Ryan Olson committed
552
553
554
}

impl DistributedConfig {
555
    pub fn from_settings() -> DistributedConfig {
556
        let request_plane = RequestPlaneMode::from_env();
Ryan Olson's avatar
Ryan Olson committed
557
        DistributedConfig {
558
            store_backend: KeyValueStoreSelect::Etcd(Box::default()),
559
560
561
562
563
564
            nats_config: if request_plane.is_nats() {
                Some(nats::ClientOptions::default())
            } else {
                None
            },
            request_plane,
Ryan Olson's avatar
Ryan Olson committed
565
566
        }
    }
Ryan Olson's avatar
Ryan Olson committed
567
568

    pub fn for_cli() -> DistributedConfig {
569
570
571
572
        let etcd_config = etcd::ClientOptions {
            attach_lease: false,
            ..Default::default()
        };
573
        let request_plane = RequestPlaneMode::from_env();
574
575
        DistributedConfig {
            store_backend: KeyValueStoreSelect::Etcd(Box::new(etcd_config)),
576
577
578
579
580
581
            nats_config: if request_plane.is_nats() {
                Some(nats::ClientOptions::default())
            } else {
                None
            },
            request_plane,
582
583
        }
    }
584
585
586
587
588
589
590
591
592
593
594
595

    /// A DistributedConfig that isn't distributed, for when the frontend and backend are in the
    /// same process.
    pub fn process_local() -> DistributedConfig {
        DistributedConfig {
            store_backend: KeyValueStoreSelect::Memory,
            nats_config: None,
            // This won't be used in process local, so we likely need a "none" option to
            // communicate that and avoid opening the ports.
            request_plane: RequestPlaneMode::Tcp,
        }
    }
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
}

/// Request plane transport mode configuration
///
/// This determines how requests are distributed from routers to workers:
/// - `Nats`: Use NATS for request distribution (default, legacy)
/// - `Http`: Use HTTP/2 for request distribution
/// - `Tcp`: Use raw TCP for request distribution with msgpack support
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RequestPlaneMode {
    /// Use NATS for request plane (default for backward compatibility)
    Nats,
    /// Use HTTP/2 for request plane
    Http,
    /// Use raw TCP for request plane with msgpack support
    Tcp,
}

impl Default for RequestPlaneMode {
    fn default() -> Self {
        Self::Nats
    }
}

impl fmt::Display for RequestPlaneMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Nats => write!(f, "nats"),
            Self::Http => write!(f, "http"),
            Self::Tcp => write!(f, "tcp"),
        }
    }
}

impl std::str::FromStr for RequestPlaneMode {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "nats" => Ok(Self::Nats),
            "http" => Ok(Self::Http),
            "tcp" => Ok(Self::Tcp),
            _ => Err(anyhow::anyhow!(
                "Invalid request plane mode: '{}'. Valid options are: 'nats', 'http', 'tcp'",
                s
            )),
642
        }
Ryan Olson's avatar
Ryan Olson committed
643
    }
Ryan Olson's avatar
Ryan Olson committed
644
}
645

646
647
648
649
650
651
652
653
654
impl RequestPlaneMode {
    /// Get the request plane mode from environment variable (uncached)
    /// Reads from `DYN_REQUEST_PLANE` environment variable.
    fn from_env() -> Self {
        std::env::var("DYN_REQUEST_PLANE")
            .ok()
            .and_then(|s| s.parse().ok())
            .unwrap_or_default()
    }
655
656
657
658

    pub fn is_nats(&self) -> bool {
        matches!(self, RequestPlaneMode::Nats)
    }
659
660
}

661
pub mod distributed_test_utils {
662
663
    //! Common test helper functions for DistributedRuntime tests

664
    /// Helper function to create a DRT instance for integration-only tests.
665
    /// Uses from_current to leverage existing tokio runtime
666
    /// Note: Settings are read from environment variables inside DistributedRuntime::from_settings
667
    #[cfg(feature = "integration")]
668
669
670
    pub async fn create_test_drt_async() -> super::DistributedRuntime {
        use crate::{storage::key_value_store::KeyValueStoreSelect, transports::nats};

671
        let rt = crate::Runtime::from_current().unwrap();
672
673
        let config = super::DistributedConfig {
            store_backend: KeyValueStoreSelect::Memory,
674
            nats_config: Some(nats::ClientOptions::default()),
675
            request_plane: crate::distributed::RequestPlaneMode::default(),
676
677
        };
        super::DistributedRuntime::new(rt, config).await.unwrap()
678
679
    }
}
680

681
#[cfg(all(test, feature = "integration"))]
682
mod tests {
683
    use super::RequestPlaneMode;
684
685
686
687
    use super::distributed_test_utils::create_test_drt_async;

    #[tokio::test]
    async fn test_drt_uptime_after_delay_system_disabled() {
688
        use crate::config::environment_names::runtime::system as env_system;
689
        // Test uptime with system status server disabled
690
        temp_env::async_with_vars([(env_system::DYN_SYSTEM_PORT, None::<&str>)], async {
691
692
693
694
695
696
697
            // Start a DRT
            let drt = create_test_drt_async().await;

            // Wait 50ms
            tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

            // Check that uptime is 50+ ms
698
            let uptime = drt.system_health.lock().uptime();
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
            assert!(
                uptime >= std::time::Duration::from_millis(50),
                "Expected uptime to be at least 50ms, but got {:?}",
                uptime
            );

            println!(
                "✓ DRT uptime test passed (system disabled): uptime = {:?}",
                uptime
            );
        })
        .await;
    }

    #[tokio::test]
    async fn test_drt_uptime_after_delay_system_enabled() {
715
        use crate::config::environment_names::runtime::system as env_system;
716
        // Test uptime with system status server enabled
717
        temp_env::async_with_vars([(env_system::DYN_SYSTEM_PORT, Some("8081"))], async {
718
719
720
721
722
723
724
            // Start a DRT
            let drt = create_test_drt_async().await;

            // Wait 50ms
            tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

            // Check that uptime is 50+ ms
725
            let uptime = drt.system_health.lock().uptime();
726
727
728
729
730
731
732
733
734
735
736
737
738
            assert!(
                uptime >= std::time::Duration::from_millis(50),
                "Expected uptime to be at least 50ms, but got {:?}",
                uptime
            );

            println!(
                "✓ DRT uptime test passed (system enabled): uptime = {:?}",
                uptime
            );
        })
        .await;
    }
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774

    #[test]
    fn test_request_plane_mode_from_str() {
        assert_eq!(
            "nats".parse::<RequestPlaneMode>().unwrap(),
            RequestPlaneMode::Nats
        );
        assert_eq!(
            "http".parse::<RequestPlaneMode>().unwrap(),
            RequestPlaneMode::Http
        );
        assert_eq!(
            "tcp".parse::<RequestPlaneMode>().unwrap(),
            RequestPlaneMode::Tcp
        );
        assert_eq!(
            "NATS".parse::<RequestPlaneMode>().unwrap(),
            RequestPlaneMode::Nats
        );
        assert_eq!(
            "HTTP".parse::<RequestPlaneMode>().unwrap(),
            RequestPlaneMode::Http
        );
        assert_eq!(
            "TCP".parse::<RequestPlaneMode>().unwrap(),
            RequestPlaneMode::Tcp
        );
        assert!("invalid".parse::<RequestPlaneMode>().is_err());
    }

    #[test]
    fn test_request_plane_mode_display() {
        assert_eq!(RequestPlaneMode::Nats.to_string(), "nats");
        assert_eq!(RequestPlaneMode::Http.to_string(), "http");
        assert_eq!(RequestPlaneMode::Tcp.to_string(), "tcp");
    }
775
}