config.rs 26.6 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 anyhow::Result;
Ryan Olson's avatar
Ryan Olson committed
5
6
7
use derive_builder::Builder;
use figment::{
    Figment,
8
    providers::{Env, Format, Serialized, Toml},
Ryan Olson's avatar
Ryan Olson committed
9
10
};
use serde::{Deserialize, Serialize};
11
use std::fmt;
12
use std::sync::OnceLock;
Ryan Olson's avatar
Ryan Olson committed
13
14
use validator::Validate;

15
16
pub mod environment_names;

17
18
/// Default system host for health and metrics endpoints
const DEFAULT_SYSTEM_HOST: &str = "0.0.0.0";
19

20
21
/// Default system port for health and metrics endpoints (-1 = disabled)
const DEFAULT_SYSTEM_PORT: i16 = -1;
22

23
24
25
26
/// Default health endpoint paths
const DEFAULT_SYSTEM_HEALTH_PATH: &str = "/health";
const DEFAULT_SYSTEM_LIVE_PATH: &str = "/live";

27
28
29
30
31
32
/// Default health check configuration
/// This is the wait time before sending canary health checks when no activity is detected
pub const DEFAULT_CANARY_WAIT_TIME_SECS: u64 = 10;
/// Default timeout for individual health check requests
pub const DEFAULT_HEALTH_CHECK_REQUEST_TIMEOUT_SECS: u64 = 3;

Ryan Olson's avatar
Ryan Olson committed
33
34
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkerConfig {
35
    /// Grace shutdown period for the system server.
Ryan Olson's avatar
Ryan Olson committed
36
37
38
39
    pub graceful_shutdown_timeout: u64,
}

impl WorkerConfig {
40
41
    /// Instantiates and reads server configurations from appropriate sources.
    /// Panics on invalid configuration.
Ryan Olson's avatar
Ryan Olson committed
42
43
44
45
    pub fn from_settings() -> Self {
        // All calls should be global and thread safe.
        Figment::new()
            .merge(Serialized::defaults(Self::default()))
46
            .merge(Env::prefixed("DYN_WORKER_"))
Ryan Olson's avatar
Ryan Olson committed
47
            .extract()
48
            .unwrap() // safety: Called on startup, so panic is reasonable
Ryan Olson's avatar
Ryan Olson committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    }
}

impl Default for WorkerConfig {
    fn default() -> Self {
        WorkerConfig {
            graceful_shutdown_timeout: if cfg!(debug_assertions) {
                1 // Debug build: 1 second
            } else {
                30 // Release build: 30 seconds
            },
        }
    }
}

64
65
66
67
68
69
70
#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
#[serde(rename_all = "lowercase")]
pub enum HealthStatus {
    Ready,
    NotReady,
}

Ryan Olson's avatar
Ryan Olson committed
71
72
73
74
75
/// Runtime configuration
/// Defines the configuration for Tokio runtimes
#[derive(Serialize, Deserialize, Validate, Debug, Builder, Clone)]
#[builder(build_fn(private, name = "build_internal"), derive(Debug, Serialize))]
pub struct RuntimeConfig {
76
    /// Number of async worker threads
Ryan Olson's avatar
Ryan Olson committed
77
    /// If set to 1, the runtime will run in single-threaded mode
78
79
    /// Set this at runtime with environment variable DYN_RUNTIME_NUM_WORKER_THREADS. Defaults to
    /// number of cores.
Ryan Olson's avatar
Ryan Olson committed
80
81
    #[validate(range(min = 1))]
    #[builder_field_attr(serde(skip_serializing_if = "Option::is_none"))]
82
    pub num_worker_threads: Option<usize>,
Ryan Olson's avatar
Ryan Olson committed
83
84
85

    /// Maximum number of blocking threads
    /// Blocking threads are used for blocking operations, this value must be greater than 0.
86
87
    /// Set this at runtime with environment variable DYN_RUNTIME_MAX_BLOCKING_THREADS. Defaults to
    /// 512.
Ryan Olson's avatar
Ryan Olson committed
88
    #[validate(range(min = 1))]
89
    #[builder(default = "512")]
Ryan Olson's avatar
Ryan Olson committed
90
91
    #[builder_field_attr(serde(skip_serializing_if = "Option::is_none"))]
    pub max_blocking_threads: usize,
92

93
    /// System status server host for health and metrics endpoints
94
95
    /// Set this at runtime with environment variable DYN_SYSTEM_HOST
    #[builder(default = "DEFAULT_SYSTEM_HOST.to_string()")]
96
    #[builder_field_attr(serde(skip_serializing_if = "Option::is_none"))]
97
    pub system_host: String,
98

99
    /// System status server port for health and metrics endpoints
100
101
    /// Set to -1 to disable the system status server (default)
    /// Set to a positive port number (e.g. 8081) to enable it
102
103
    /// Set this at runtime with environment variable DYN_SYSTEM_PORT
    #[builder(default = "DEFAULT_SYSTEM_PORT")]
104
    #[builder_field_attr(serde(skip_serializing_if = "Option::is_none"))]
105
106
107
108
109
110
111
112
    pub system_port: i16,

    /// Health and metrics System status server enabled (DEPRECATED)
    /// This field is deprecated. Use system_port instead (set to positive value to enable)
    /// Environment variable DYN_SYSTEM_ENABLED is deprecated
    #[deprecated(
        note = "Use system_port instead. Set DYN_SYSTEM_PORT to enable the system metrics server."
    )]
113
114
    #[builder(default = "false")]
    #[builder_field_attr(serde(skip_serializing_if = "Option::is_none"))]
115
    pub system_enabled: bool,
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

    /// Starting Health Status
    /// Set this at runtime with environment variable DYN_SYSTEM_STARTING_HEALTH_STATUS
    #[builder(default = "HealthStatus::NotReady")]
    #[builder_field_attr(serde(skip_serializing_if = "Option::is_none"))]
    pub starting_health_status: HealthStatus,

    /// Use Endpoint Health Status
    /// When using endpoint health status, health status
    /// is the AND of individual endpoint health
    /// Set this at runtime with environment variable DYN_SYSTEM_USE_ENDPOINT_HEALTH_STATUS
    /// with the list of endpoints to consider for system health
    #[builder(default = "vec![]")]
    #[builder_field_attr(serde(skip_serializing_if = "Option::is_none"))]
    pub use_endpoint_health_status: Vec<String>,
131
132
133
134
135
136
137
138
139
140

    /// Health endpoint paths
    /// Set this at runtime with environment variable DYN_SYSTEM_HEALTH_PATH
    #[builder(default = "DEFAULT_SYSTEM_HEALTH_PATH.to_string()")]
    #[builder_field_attr(serde(skip_serializing_if = "Option::is_none"))]
    pub system_health_path: String,
    /// Set this at runtime with environment variable DYN_SYSTEM_LIVE_PATH
    #[builder(default = "DEFAULT_SYSTEM_LIVE_PATH.to_string()")]
    #[builder_field_attr(serde(skip_serializing_if = "Option::is_none"))]
    pub system_live_path: String,
141

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    /// Number of threads for the Rayon compute pool
    /// If not set, defaults to num_cpus / 2
    /// Set this at runtime with environment variable DYN_COMPUTE_THREADS
    #[builder(default = "None")]
    #[builder_field_attr(serde(skip_serializing_if = "Option::is_none"))]
    pub compute_threads: Option<usize>,

    /// Stack size for compute threads in bytes
    /// Defaults to 2MB (2097152 bytes)
    /// Set this at runtime with environment variable DYN_COMPUTE_STACK_SIZE
    #[builder(default = "Some(2 * 1024 * 1024)")]
    #[builder_field_attr(serde(skip_serializing_if = "Option::is_none"))]
    pub compute_stack_size: Option<usize>,

    /// Thread name prefix for compute pool threads
    /// Set this at runtime with environment variable DYN_COMPUTE_THREAD_PREFIX
    #[builder(default = "\"compute\".to_string()")]
    #[builder_field_attr(serde(skip_serializing_if = "Option::is_none"))]
    pub compute_thread_prefix: String,

162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
    /// Enable active health checking with payloads
    /// Set this at runtime with environment variable DYN_HEALTH_CHECK_ENABLED
    #[builder(default = "false")]
    #[builder_field_attr(serde(skip_serializing_if = "Option::is_none"))]
    pub health_check_enabled: bool,

    /// Canary wait time in seconds (time to wait before sending health check when no activity)
    /// Set this at runtime with environment variable DYN_CANARY_WAIT_TIME
    #[builder(default = "DEFAULT_CANARY_WAIT_TIME_SECS")]
    #[builder_field_attr(serde(skip_serializing_if = "Option::is_none"))]
    pub canary_wait_time_secs: u64,

    /// Health check request timeout in seconds
    /// Set this at runtime with environment variable DYN_HEALTH_CHECK_REQUEST_TIMEOUT
    #[builder(default = "DEFAULT_HEALTH_CHECK_REQUEST_TIMEOUT_SECS")]
    #[builder_field_attr(serde(skip_serializing_if = "Option::is_none"))]
    pub health_check_request_timeout_secs: u64,
Ryan Olson's avatar
Ryan Olson committed
179
180
}

181
182
183
184
185
186
187
188
189
impl fmt::Display for RuntimeConfig {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // If None, it defaults to "number of cores", so we indicate that.
        match self.num_worker_threads {
            Some(val) => write!(f, "num_worker_threads={val}, ")?,
            None => write!(f, "num_worker_threads=default (num_cores), ")?,
        }

        write!(f, "max_blocking_threads={}, ", self.max_blocking_threads)?;
190
191
        write!(f, "system_host={}, ", self.system_host)?;
        write!(f, "system_port={}, ", self.system_port)?;
192
193
194
195
196
197
198
199
200
201
        write!(
            f,
            "use_endpoint_health_status={:?}",
            self.use_endpoint_health_status
        )?;
        write!(
            f,
            "starting_health_status={:?}",
            self.starting_health_status
        )?;
202
203
        write!(f, ", system_health_path={}", self.system_health_path)?;
        write!(f, ", system_live_path={}", self.system_live_path)?;
204
205
206
207
208
209
210
        write!(f, ", health_check_enabled={}", self.health_check_enabled)?;
        write!(f, ", canary_wait_time_secs={}", self.canary_wait_time_secs)?;
        write!(
            f,
            ", health_check_request_timeout_secs={}",
            self.health_check_request_timeout_secs
        )?;
211
212
213
214
215

        Ok(())
    }
}

Ryan Olson's avatar
Ryan Olson committed
216
217
218
219
220
221
222
223
impl RuntimeConfig {
    pub fn builder() -> RuntimeConfigBuilder {
        RuntimeConfigBuilder::default()
    }

    pub(crate) fn figment() -> Figment {
        Figment::new()
            .merge(Serialized::defaults(RuntimeConfig::default()))
Neelay Shah's avatar
Neelay Shah committed
224
225
            .merge(Toml::file("/opt/dynamo/defaults/runtime.toml"))
            .merge(Toml::file("/opt/dynamo/etc/runtime.toml"))
226
227
            .merge(Env::prefixed("DYN_RUNTIME_").filter_map(|k| {
                let full_key = format!("DYN_RUNTIME_{}", k.as_str());
228
229
230
231
232
233
                // filters out empty environment variables
                match std::env::var(&full_key) {
                    Ok(v) if !v.is_empty() => Some(k.into()),
                    _ => None,
                }
            }))
234
235
236
237
238
239
240
241
242
243
            .merge(Env::prefixed("DYN_SYSTEM_").filter_map(|k| {
                let full_key = format!("DYN_SYSTEM_{}", k.as_str());
                // filters out empty environment variables
                match std::env::var(&full_key) {
                    Ok(v) if !v.is_empty() => {
                        // Map DYN_SYSTEM_* to the correct field names
                        let mapped_key = match k.as_str() {
                            "HOST" => "system_host",
                            "PORT" => "system_port",
                            "ENABLED" => "system_enabled",
244
245
                            "USE_ENDPOINT_HEALTH_STATUS" => "use_endpoint_health_status",
                            "STARTING_HEALTH_STATUS" => "starting_health_status",
246
247
                            "HEALTH_PATH" => "system_health_path",
                            "LIVE_PATH" => "system_live_path",
248
249
250
251
252
253
254
                            _ => k.as_str(),
                        };
                        Some(mapped_key.into())
                    }
                    _ => None,
                }
            }))
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
            .merge(Env::prefixed("DYN_COMPUTE_").filter_map(|k| {
                let full_key = format!("DYN_COMPUTE_{}", k.as_str());
                // filters out empty environment variables
                match std::env::var(&full_key) {
                    Ok(v) if !v.is_empty() => {
                        // Map DYN_COMPUTE_* to the correct field names
                        let mapped_key = match k.as_str() {
                            "THREADS" => "compute_threads",
                            "STACK_SIZE" => "compute_stack_size",
                            "THREAD_PREFIX" => "compute_thread_prefix",
                            _ => k.as_str(),
                        };
                        Some(mapped_key.into())
                    }
                    _ => None,
                }
            }))
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
            .merge(Env::prefixed("DYN_HEALTH_CHECK_").filter_map(|k| {
                let full_key = format!("DYN_HEALTH_CHECK_{}", k.as_str());
                // filters out empty environment variables
                match std::env::var(&full_key) {
                    Ok(v) if !v.is_empty() => {
                        // Map DYN_HEALTH_CHECK_* to the correct field names
                        let mapped_key = match k.as_str() {
                            "ENABLED" => "health_check_enabled",
                            "REQUEST_TIMEOUT" => "health_check_request_timeout_secs",
                            _ => k.as_str(),
                        };
                        Some(mapped_key.into())
                    }
                    _ => None,
                }
            }))
            .merge(Env::prefixed("DYN_CANARY_").filter_map(|k| {
                let full_key = format!("DYN_CANARY_{}", k.as_str());
                // filters out empty environment variables
                match std::env::var(&full_key) {
                    Ok(v) if !v.is_empty() => {
                        // Map DYN_CANARY_* to the correct field names
                        let mapped_key = match k.as_str() {
                            "WAIT_TIME" => "canary_wait_time_secs",
                            _ => k.as_str(),
                        };
                        Some(mapped_key.into())
                    }
                    _ => None,
                }
            }))
Ryan Olson's avatar
Ryan Olson committed
303
304
305
306
307
    }

    /// Load the runtime configuration from the environment and configuration files
    /// Configuration is priorities in the following order, where the last has the lowest priority:
    /// 1. Environment variables (top priority)
308
    ///    TO DO: Add documentation for configuration files. Paths should be configurable.
Neelay Shah's avatar
Neelay Shah committed
309
310
    /// 2. /opt/dynamo/etc/runtime.toml
    /// 3. /opt/dynamo/defaults/runtime.toml (lowest priority)
Ryan Olson's avatar
Ryan Olson committed
311
    ///
312
    /// Environment variables are prefixed with `DYN_RUNTIME_` and `DYN_SYSTEM`
Ryan Olson's avatar
Ryan Olson committed
313
    pub fn from_settings() -> Result<RuntimeConfig> {
314
        use environment_names::runtime::system as env_system;
315
        // Check for deprecated environment variables
316
        if std::env::var(env_system::DYN_SYSTEM_USE_ENDPOINT_HEALTH_STATUS).is_ok() {
317
318
319
320
321
322
323
            tracing::warn!(
                "DYN_SYSTEM_USE_ENDPOINT_HEALTH_STATUS is deprecated and no longer used. \
                System health is now determined by endpoints that register with health check payloads. \
                Please update your configuration to register health check payloads directly on endpoints."
            );
        }

324
        if std::env::var(env_system::DYN_SYSTEM_ENABLED).is_ok() {
325
326
327
328
329
330
331
            tracing::warn!(
                "DYN_SYSTEM_ENABLED is deprecated. \
                System metrics server is now controlled solely by DYN_SYSTEM_PORT. \
                Set DYN_SYSTEM_PORT to a positive value to enable the server, or set to -1 to disable (default)."
            );
        }

Ryan Olson's avatar
Ryan Olson committed
332
333
334
335
336
        let config: RuntimeConfig = Self::figment().extract()?;
        config.validate()?;
        Ok(config)
    }

337
    /// Check if System server should be enabled
338
339
340
    /// System server is enabled when DYN_SYSTEM_PORT is set to a positive value
    /// Negative values disable the server
    /// TODO: Support port = 0 to bind to a random available port
341
    pub fn system_server_enabled(&self) -> bool {
342
        self.system_port > 0
343
344
    }

Ryan Olson's avatar
Ryan Olson committed
345
346
    pub fn single_threaded() -> Self {
        RuntimeConfig {
347
            num_worker_threads: Some(1),
Ryan Olson's avatar
Ryan Olson committed
348
            max_blocking_threads: 1,
349
350
            system_host: DEFAULT_SYSTEM_HOST.to_string(),
            system_port: DEFAULT_SYSTEM_PORT,
351
            #[allow(deprecated)]
352
            system_enabled: false,
353
354
            starting_health_status: HealthStatus::NotReady,
            use_endpoint_health_status: vec![],
355
356
            system_health_path: DEFAULT_SYSTEM_HEALTH_PATH.to_string(),
            system_live_path: DEFAULT_SYSTEM_LIVE_PATH.to_string(),
357
358
359
            compute_threads: Some(1),
            compute_stack_size: Some(2 * 1024 * 1024),
            compute_thread_prefix: "compute".to_string(),
360
361
362
            health_check_enabled: false,
            canary_wait_time_secs: DEFAULT_CANARY_WAIT_TIME_SECS,
            health_check_request_timeout_secs: DEFAULT_HEALTH_CHECK_REQUEST_TIMEOUT_SECS,
Ryan Olson's avatar
Ryan Olson committed
363
364
365
366
        }
    }

    /// Create a new default runtime configuration
367
368
369
370
371
372
    pub(crate) fn create_runtime(&self) -> std::io::Result<tokio::runtime::Runtime> {
        tokio::runtime::Builder::new_multi_thread()
            .worker_threads(
                self.num_worker_threads
                    .unwrap_or_else(|| std::thread::available_parallelism().unwrap().get()),
            )
Ryan Olson's avatar
Ryan Olson committed
373
374
            .max_blocking_threads(self.max_blocking_threads)
            .enable_all()
375
            .build()
Ryan Olson's avatar
Ryan Olson committed
376
377
378
379
380
    }
}

impl Default for RuntimeConfig {
    fn default() -> Self {
381
        let num_cores = std::thread::available_parallelism().unwrap().get();
382
        Self {
383
384
            num_worker_threads: Some(num_cores),
            max_blocking_threads: num_cores,
385
386
            system_host: DEFAULT_SYSTEM_HOST.to_string(),
            system_port: DEFAULT_SYSTEM_PORT,
387
            #[allow(deprecated)]
388
            system_enabled: false,
389
390
            starting_health_status: HealthStatus::NotReady,
            use_endpoint_health_status: vec![],
391
392
            system_health_path: DEFAULT_SYSTEM_HEALTH_PATH.to_string(),
            system_live_path: DEFAULT_SYSTEM_LIVE_PATH.to_string(),
393
394
395
            compute_threads: None,
            compute_stack_size: Some(2 * 1024 * 1024),
            compute_thread_prefix: "compute".to_string(),
396
397
398
            health_check_enabled: false,
            canary_wait_time_secs: DEFAULT_CANARY_WAIT_TIME_SECS,
            health_check_request_timeout_secs: DEFAULT_HEALTH_CHECK_REQUEST_TIMEOUT_SECS,
399
        }
Ryan Olson's avatar
Ryan Olson committed
400
401
402
403
404
405
406
407
408
409
410
    }
}

impl RuntimeConfigBuilder {
    /// Build and validate the runtime configuration
    pub fn build(&self) -> Result<RuntimeConfig> {
        let config = self.build_internal()?;
        config.validate()?;
        Ok(config)
    }
}
411

412
413
414
415
416
417
418
419
/// Check if a string is truthy
/// This will be used to evaluate environment variables or any other subjective
/// configuration parameters that can be set by the user that should be evaluated
/// as a boolean value.
pub fn is_truthy(val: &str) -> bool {
    matches!(val.to_lowercase().as_str(), "1" | "true" | "on" | "yes")
}

420
421
422
423
424
425
426
427
428
429
430
431
432
pub fn parse_bool(val: &str) -> anyhow::Result<bool> {
    if is_truthy(val) {
        Ok(true)
    } else if is_falsey(val) {
        Ok(false)
    } else {
        anyhow::bail!(
            "Invalid boolean value: '{}'. Expected one of: true/false, 1/0, on/off, yes/no",
            val
        )
    }
}

433
434
435
436
437
438
439
440
/// Check if a string is falsey
/// This will be used to evaluate environment variables or any other subjective
/// configuration parameters that can be set by the user that should be evaluated
/// as a boolean value (opposite of is_truthy).
pub fn is_falsey(val: &str) -> bool {
    matches!(val.to_lowercase().as_str(), "0" | "false" | "off" | "no")
}

441
442
443
444
445
446
447
448
/// Check if an environment variable is truthy
pub fn env_is_truthy(env: &str) -> bool {
    match std::env::var(env) {
        Ok(val) => is_truthy(val.as_str()),
        Err(_) => false,
    }
}

449
450
451
452
453
454
/// Check if an environment variable is falsey
pub fn env_is_falsey(env: &str) -> bool {
    match std::env::var(env) {
        Ok(val) => is_falsey(val.as_str()),
        Err(_) => false,
    }
455
456
457
}

/// Check whether JSONL logging enabled
458
/// Set the `DYN_LOGGING_JSONL` environment variable a [`is_truthy`] value
459
pub fn jsonl_logging_enabled() -> bool {
460
    env_is_truthy(environment_names::logging::DYN_LOGGING_JSONL)
461
462
463
}

/// Check whether logging with ANSI terminal escape codes and colors is disabled.
464
/// Set the `DYN_SDK_DISABLE_ANSI_LOGGING` environment variable a [`is_truthy`] value
465
pub fn disable_ansi_logging() -> bool {
466
    env_is_truthy(environment_names::logging::DYN_SDK_DISABLE_ANSI_LOGGING)
467
}
468

Ryan Olson's avatar
Ryan Olson committed
469
470
471
/// Check whether to use local timezone for logging timestamps (default is UTC)
/// Set the `DYN_LOG_USE_LOCAL_TZ` environment variable to a [`is_truthy`] value
pub fn use_local_timezone() -> bool {
472
    env_is_truthy(environment_names::logging::DYN_LOG_USE_LOCAL_TZ)
Ryan Olson's avatar
Ryan Olson committed
473
474
}

475
476
477
478
479
480
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_runtime_config_with_env_vars() -> Result<()> {
481
        use environment_names::runtime;
482
483
        temp_env::with_vars(
            vec![
484
485
                (runtime::DYN_RUNTIME_NUM_WORKER_THREADS, Some("24")),
                (runtime::DYN_RUNTIME_MAX_BLOCKING_THREADS, Some("32")),
486
487
488
            ],
            || {
                let config = RuntimeConfig::from_settings()?;
489
                assert_eq!(config.num_worker_threads, Some(24));
490
491
492
493
494
495
496
497
                assert_eq!(config.max_blocking_threads, 32);
                Ok(())
            },
        )
    }

    #[test]
    fn test_runtime_config_defaults() -> Result<()> {
498
        use environment_names::runtime;
499
500
        temp_env::with_vars(
            vec![
501
502
                (runtime::DYN_RUNTIME_NUM_WORKER_THREADS, None::<&str>),
                (runtime::DYN_RUNTIME_MAX_BLOCKING_THREADS, Some("")),
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
            ],
            || {
                let config = RuntimeConfig::from_settings()?;

                let default_config = RuntimeConfig::default();
                assert_eq!(config.num_worker_threads, default_config.num_worker_threads);
                assert_eq!(
                    config.max_blocking_threads,
                    default_config.max_blocking_threads
                );
                Ok(())
            },
        )
    }

    #[test]
    fn test_runtime_config_rejects_invalid_thread_count() -> Result<()> {
520
        use environment_names::runtime;
521
522
        temp_env::with_vars(
            vec![
523
524
                (runtime::DYN_RUNTIME_NUM_WORKER_THREADS, Some("0")),
                (runtime::DYN_RUNTIME_MAX_BLOCKING_THREADS, Some("0")),
525
526
527
528
529
            ],
            || {
                let result = RuntimeConfig::from_settings();
                assert!(result.is_err());
                if let Err(e) = result {
530
531
532
533
534
535
536
537
                    assert!(
                        e.to_string()
                            .contains("num_worker_threads: Validation error")
                    );
                    assert!(
                        e.to_string()
                            .contains("max_blocking_threads: Validation error")
                    );
538
539
540
541
542
                }
                Ok(())
            },
        )
    }
543
544

    #[test]
545
    fn test_runtime_config_system_server_env_vars() -> Result<()> {
546
        use environment_names::runtime::system;
547
548
        temp_env::with_vars(
            vec![
549
550
                (system::DYN_SYSTEM_HOST, Some("127.0.0.1")),
                (system::DYN_SYSTEM_PORT, Some("9090")),
551
552
553
            ],
            || {
                let config = RuntimeConfig::from_settings()?;
554
555
                assert_eq!(config.system_host, "127.0.0.1");
                assert_eq!(config.system_port, 9090);
556
557
558
559
560
561
                Ok(())
            },
        )
    }

    #[test]
562
    fn test_system_server_disabled_by_default() {
563
564
        use environment_names::runtime::system;
        temp_env::with_vars(vec![(system::DYN_SYSTEM_PORT, None::<&str>)], || {
565
            let config = RuntimeConfig::from_settings().unwrap();
566
            assert!(!config.system_server_enabled());
567
            assert_eq!(config.system_port, -1);
568
569
570
571
        });
    }

    #[test]
572
    fn test_system_server_disabled_with_negative_port() {
573
574
        use environment_names::runtime::system;
        temp_env::with_vars(vec![(system::DYN_SYSTEM_PORT, Some("-1"))], || {
575
            let config = RuntimeConfig::from_settings().unwrap();
576
            assert!(!config.system_server_enabled());
577
            assert_eq!(config.system_port, -1);
578
579
580
581
        });
    }

    #[test]
582
    fn test_system_server_enabled_with_port() {
583
584
        use environment_names::runtime::system;
        temp_env::with_vars(vec![(system::DYN_SYSTEM_PORT, Some("9527"))], || {
585
            let config = RuntimeConfig::from_settings().unwrap();
586
            assert!(config.system_server_enabled());
587
            assert_eq!(config.system_port, 9527);
588
589
590
        });
    }

591
592
    #[test]
    fn test_system_server_starting_health_status_ready() {
593
        use environment_names::runtime::system;
594
        temp_env::with_vars(
595
            vec![(system::DYN_SYSTEM_STARTING_HEALTH_STATUS, Some("ready"))],
596
597
598
599
600
601
602
603
604
            || {
                let config = RuntimeConfig::from_settings().unwrap();
                assert!(config.starting_health_status == HealthStatus::Ready);
            },
        );
    }

    #[test]
    fn test_system_use_endpoint_health_status() {
605
        use environment_names::runtime::system;
606
        temp_env::with_vars(
607
608
609
610
            vec![(
                system::DYN_SYSTEM_USE_ENDPOINT_HEALTH_STATUS,
                Some("[\"ready\"]"),
            )],
611
612
613
614
615
616
617
            || {
                let config = RuntimeConfig::from_settings().unwrap();
                assert!(config.use_endpoint_health_status == vec!["ready"]);
            },
        );
    }

618
619
    #[test]
    fn test_system_health_endpoint_path_default() {
620
621
        use environment_names::runtime::system;
        temp_env::with_vars(vec![(system::DYN_SYSTEM_HEALTH_PATH, None::<&str>)], || {
622
623
624
625
626
627
628
            let config = RuntimeConfig::from_settings().unwrap();
            assert_eq!(
                config.system_health_path,
                DEFAULT_SYSTEM_HEALTH_PATH.to_string()
            );
        });

629
        temp_env::with_vars(vec![(system::DYN_SYSTEM_LIVE_PATH, None::<&str>)], || {
630
631
632
633
634
635
636
637
638
639
            let config = RuntimeConfig::from_settings().unwrap();
            assert_eq!(
                config.system_live_path,
                DEFAULT_SYSTEM_LIVE_PATH.to_string()
            );
        });
    }

    #[test]
    fn test_system_health_endpoint_path_custom() {
640
        use environment_names::runtime::system;
641
        temp_env::with_vars(
642
            vec![(system::DYN_SYSTEM_HEALTH_PATH, Some("/custom/health"))],
643
644
645
646
647
648
            || {
                let config = RuntimeConfig::from_settings().unwrap();
                assert_eq!(config.system_health_path, "/custom/health");
            },
        );

649
650
651
652
653
654
655
        temp_env::with_vars(
            vec![(system::DYN_SYSTEM_LIVE_PATH, Some("/custom/live"))],
            || {
                let config = RuntimeConfig::from_settings().unwrap();
                assert_eq!(config.system_live_path, "/custom/live");
            },
        );
656
657
    }

658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
    #[test]
    fn test_is_truthy_and_falsey() {
        // Test truthy values
        assert!(is_truthy("1"));
        assert!(is_truthy("true"));
        assert!(is_truthy("TRUE"));
        assert!(is_truthy("on"));
        assert!(is_truthy("yes"));

        // Test falsey values
        assert!(is_falsey("0"));
        assert!(is_falsey("false"));
        assert!(is_falsey("FALSE"));
        assert!(is_falsey("off"));
        assert!(is_falsey("no"));

        // Test opposite behavior
        assert!(!is_truthy("0"));
        assert!(!is_falsey("1"));

        // Test env functions
        temp_env::with_vars(vec![("TEST_TRUTHY", Some("true"))], || {
            assert!(env_is_truthy("TEST_TRUTHY"));
            assert!(!env_is_falsey("TEST_TRUTHY"));
        });

        temp_env::with_vars(vec![("TEST_FALSEY", Some("false"))], || {
            assert!(!env_is_truthy("TEST_FALSEY"));
            assert!(env_is_falsey("TEST_FALSEY"));
        });

        // Test missing env vars
        temp_env::with_vars(vec![("TEST_MISSING", None::<&str>)], || {
            assert!(!env_is_truthy("TEST_MISSING"));
            assert!(!env_is_falsey("TEST_MISSING"));
        });
    }
695
}