dynamo-runtime 1.6.1

Dynamo Runtime Library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use super::*;
use crate::{DistributedRuntime, Runtime, config::HealthStatus, distributed::DistributedConfig};

fn http_env() -> [(&'static str, Option<&'static str>); 7] {
    [
        ("DYN_SYSTEM_HOST", Some("127.0.0.1")),
        ("DYN_SYSTEM_PORT", Some("0")),
        ("DYN_SYSTEM_LIVE_PATH", None),
        ("DYN_SYSTEM_HEALTH_PATH", None),
        ("DYN_SYSTEM_STARTING_HEALTH_STATUS", Some("notready")),
        ("DYN_HEALTH_CHECK_ENABLED", Some("false")),
        ("DYN_SYSTEM_USE_ENDPOINT_HEALTH_STATUS", None),
    ]
}

fn client() -> reqwest::Client {
    reqwest::Client::builder()
        .no_proxy()
        .timeout(Duration::from_secs(3))
        .build()
        .unwrap()
}

fn nats_config(address: std::net::SocketAddr) -> crate::transports::nats::ClientOptions {
    use crate::transports::nats::{ClientOptions, NatsAuth};
    ClientOptions::builder()
        .server(format!("nats://{address}"))
        .auth(NatsAuth::UserPass("user".into(), "user".into()))
        .tls_ca_cert_path(None)
        .tls_client_cert_path(None)
        .tls_client_key_path(None)
        .tls_insecure(false)
        .build()
        .unwrap()
}

async fn status(client: &reqwest::Client, base: &str, path: &str) -> u16 {
    client
        .get(format!("{base}{path}"))
        .send()
        .await
        .unwrap()
        .status()
        .as_u16()
}

async fn wait_closed(address: std::net::SocketAddr) {
    tokio::time::timeout(Duration::from_secs(5), async {
        loop {
            if tokio::net::TcpListener::bind(address).await.is_ok() {
                break;
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
    })
    .await
    .expect("listener released");
}

#[tokio::test]
async fn runtime_only_probes_and_routes_share_the_advertised_listener() {
    if crate::test_utils::run_isolated(
        concat!(
            module_path!(),
            "::runtime_only_probes_and_routes_share_the_advertised_listener"
        ),
        &[],
    ) {
        return;
    }

    temp_env::async_with_vars(http_env(), async {
        // Port zero used to bind twice and fail the second server-info registration.
        let runtime = Runtime::from_current().unwrap();
        let drt = DistributedRuntime::new_with_probe_policy(
            runtime.clone(),
            DistributedConfig::process_local(),
            SystemProbePolicy::RuntimeOnly,
        )
        .await
        .unwrap();
        let info = drt.system_status_server_info().unwrap();
        assert_ne!(info.port(), 0);
        let base = format!("http://{}", info.socket_addr);
        let client = client();
        drt.system_health()
            .lock()
            .set_health_status(HealthStatus::NotReady);
        assert_eq!(status(&client, &base, "/live").await, 200);
        assert_eq!(status(&client, &base, "/health").await, 200);
        assert_eq!(status(&client, &base, "/metrics").await, 200);
        assert_eq!(status(&client, &base, "/missing").await, 404);
        // Keep DRT and its server-info handle alive: explicit shutdown must suffice.
        runtime.shutdown();
        wait_closed(info.socket_addr).await;
    })
    .await;
}

#[tokio::test]
async fn default_worker_probes_keep_configured_health_and_response() {
    if crate::test_utils::run_isolated(
        concat!(
            module_path!(),
            "::default_worker_probes_keep_configured_health_and_response"
        ),
        &[],
    ) {
        return;
    }

    temp_env::async_with_vars(http_env(), async {
        temp_env::async_with_vars(
            [
                ("DYN_SYSTEM_LIVE_PATH", Some("/custom-live")),
                ("DYN_SYSTEM_HEALTH_PATH", Some("/custom-health")),
            ],
            async {
                let runtime = Runtime::from_current().unwrap();
                let drt =
                    DistributedRuntime::new(runtime.clone(), DistributedConfig::process_local())
                        .await
                        .unwrap();
                let info = drt.system_status_server_info().unwrap();
                let base = format!("http://{}", info.socket_addr);
                let client = client();
                for (health, code) in [(HealthStatus::NotReady, 503), (HealthStatus::Ready, 200)] {
                    drt.system_health().lock().set_health_status(health);
                    for path in ["/custom-live", "/custom-health"] {
                        let response = client.get(format!("{base}{path}")).send().await.unwrap();
                        assert_eq!(response.status().as_u16(), code);
                        let body: serde_json::Value = response.json().await.unwrap();
                        assert!(body.get("uptime").is_some());
                        assert!(body.get("endpoints").is_some());
                    }
                }
                runtime.shutdown();
                wait_closed(info.socket_addr).await;
            },
        )
        .await;
    })
    .await;
}

#[tokio::test]
async fn runtime_shutdown_withdraws_readiness_without_stopping_liveness() {
    if crate::test_utils::run_isolated(
        concat!(
            module_path!(),
            "::runtime_shutdown_withdraws_readiness_without_stopping_liveness"
        ),
        &[],
    ) {
        return;
    }

    temp_env::async_with_vars(http_env(), async {
        let runtime = Runtime::from_current().unwrap();
        let drt = DistributedRuntime::new_with_probe_policy(
            runtime.clone(),
            DistributedConfig::process_local(),
            SystemProbePolicy::RuntimeOnly,
        )
        .await
        .unwrap();
        let info = drt.system_status_server_info().unwrap();
        let base = format!("http://{}", info.socket_addr);
        let client = client();
        assert_eq!(status(&client, &base, "/health").await, 200);
        let guard = runtime.graceful_shutdown_tracker().register_task();
        runtime.shutdown();
        assert_eq!(status(&client, &base, "/health").await, 503);
        assert_eq!(status(&client, &base, "/live").await, 200);
        assert_eq!(status(&client, &base, "/metrics").await, 200);
        assert!(!runtime.primary_token().is_cancelled());
        drop(guard);
        wait_closed(info.socket_addr).await;
    })
    .await;
}

// DYN_SYSTEM_PORT is an i16. Reserve a dynamically selected port in its range.
fn reserve_system_port() -> std::net::TcpListener {
    for _ in 0..1000 {
        if let Ok(listener) =
            std::net::TcpListener::bind(("127.0.0.1", fastrand::u16(10000..32768)))
        {
            return listener;
        }
    }
    panic!("reserve system port");
}

#[tokio::test]
async fn pending_runtime_initialization_does_not_bind_http_and_cancels_on_shutdown() {
    if crate::test_utils::run_isolated(
        concat!(
            module_path!(),
            "::pending_runtime_initialization_does_not_bind_http_and_cancels_on_shutdown"
        ),
        &[],
    ) {
        return;
    }

    temp_env::async_with_vars(http_env(), async {
        let reserved = reserve_system_port();
        let address = reserved.local_addr().unwrap();
        let port = address.port().to_string();
        temp_env::async_with_vars([("DYN_SYSTEM_PORT", Some(port.as_str()))], async {
            let runtime = Runtime::from_current().unwrap();
            // Accept TCP but withhold NATS INFO to hold runtime initialization open.
            let peer = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
            let distributed = DistributedConfig {
                nats_config: Some(nats_config(peer.local_addr().unwrap())),
                ..DistributedConfig::process_local()
            };
            // Keep the port occupied: an early RuntimeOnly bind would fail
            // before the constructor could reach the pending NATS connection.
            let mut construction = Box::pin(DistributedRuntime::new_with_probe_policy(
                runtime.clone(),
                distributed,
                SystemProbePolicy::RuntimeOnly,
            ));
            let (_connection, _) = tokio::select! {
                result = &mut construction => panic!("constructed before NATS INFO: {result:?}"),
                peer = tokio::time::timeout(Duration::from_secs(5), peer.accept()) => {
                    peer.unwrap().unwrap()
                },
            };
            runtime.mark_shutting_down();
            let error = tokio::time::timeout(Duration::from_secs(5), construction)
                .await
                .expect("runtime shutdown cancels pending DRT construction")
                .unwrap_err();
            assert!(
                error
                    .to_string()
                    .contains("runtime shut down during initialization")
            );
            runtime.shutdown();
        })
        .await;
    })
    .await;
}

#[tokio::test]
async fn disabled_http_and_bind_failure_preserve_policy_contracts() {
    if crate::test_utils::run_isolated(
        concat!(
            module_path!(),
            "::disabled_http_and_bind_failure_preserve_policy_contracts"
        ),
        &[],
    ) {
        return;
    }

    temp_env::async_with_vars(http_env(), async {
        temp_env::async_with_vars([("DYN_SYSTEM_PORT", Some("-1"))], async {
            for policy in [SystemProbePolicy::Worker, SystemProbePolicy::RuntimeOnly] {
                let runtime = Runtime::from_current().unwrap();
                let drt = DistributedRuntime::new_with_probe_policy(
                    runtime.clone(),
                    DistributedConfig::process_local(),
                    policy,
                )
                .await
                .unwrap();
                assert!(drt.system_status_server_info().is_none());
                runtime.shutdown();
            }
        })
        .await;
        let occupied = reserve_system_port();
        let port = occupied.local_addr().unwrap().port().to_string();
        temp_env::async_with_vars([("DYN_SYSTEM_PORT", Some(port.as_str()))], async {
            let runtime = Runtime::from_current().unwrap();
            assert!(
                DistributedRuntime::new_with_probe_policy(
                    runtime.clone(),
                    DistributedConfig::process_local(),
                    SystemProbePolicy::RuntimeOnly
                )
                .await
                .is_err()
            );
            let drt = DistributedRuntime::new(runtime.clone(), DistributedConfig::process_local())
                .await
                .unwrap();
            assert!(drt.system_status_server_info().is_none());
            runtime.shutdown();
        })
        .await;
    })
    .await;
}
#[tokio::test]
async fn dependency_outage_only_fails_readiness_and_can_recover() {
    if crate::test_utils::run_isolated(
        concat!(
            module_path!(),
            "::dependency_outage_only_fails_readiness_and_can_recover"
        ),
        &[],
    ) {
        return;
    }

    temp_env::async_with_vars(http_env(), async {
        use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
        use tokio::sync::Notify;

        // A minimal NATS peer lets us close and restore the real client connection
        // without an external daemon or inference requests.
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let disconnect = Arc::new(Notify::new());
        let reconnect = Arc::new(Notify::new());
        let peer_disconnect = disconnect.clone();
        let peer_reconnect = reconnect.clone();
        let peer = tokio_util::task::AbortOnDropHandle::new(tokio::spawn(async move {
            for attempt in 0..2 {
                if attempt > 0 {
                    peer_reconnect.notified().await;
                }
                let (socket, _) = listener.accept().await.unwrap();
                let (reader, mut writer) = socket.into_split();
                writer
                    .write_all(
                        concat!(
                            "INFO {\"server_id\":\"probe-test\",\"version\":\"2.10.0\",",
                            "\"proto\":1,\"max_payload\":1048576}\r\n"
                        )
                        .as_bytes(),
                    )
                    .await
                    .unwrap();
                let connection = async {
                    let mut lines = BufReader::new(reader).lines();
                    while let Some(line) = lines.next_line().await.unwrap() {
                        if line == "PING" {
                            writer.write_all(b"PONG\r\n").await.unwrap();
                        }
                    }
                };
                tokio::select! {
                    _ = peer_disconnect.notified() => {},
                    _ = connection => {},
                }
            }
        }));
        let runtime = Runtime::from_current().unwrap();
        let distributed = DistributedConfig {
            nats_config: Some(nats_config(address)),
            ..DistributedConfig::process_local()
        };
        let drt = DistributedRuntime::new_with_probe_policy(
            runtime.clone(),
            distributed,
            SystemProbePolicy::RuntimeOnly,
        )
        .await
        .unwrap();
        let base = format!(
            "http://{}",
            drt.system_status_server_info().unwrap().socket_addr
        );
        let client = client();
        assert_eq!(status(&client, &base, "/health").await, 200);
        disconnect.notify_one();
        tokio::time::timeout(Duration::from_secs(5), async {
            while status(&client, &base, "/health").await != 503 {
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
        })
        .await
        .unwrap();
        assert_eq!(status(&client, &base, "/live").await, 200);
        reconnect.notify_one();
        tokio::time::timeout(Duration::from_secs(10), async {
            while status(&client, &base, "/health").await != 200 {
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
        })
        .await
        .unwrap();
        runtime.shutdown();
        drop(peer);
    })
    .await;
}

#[tokio::test]
async fn stalled_discovery_check_times_out_without_blocking_liveness() {
    if crate::test_utils::run_isolated(
        concat!(
            module_path!(),
            "::stalled_discovery_check_times_out_without_blocking_liveness"
        ),
        &[],
    ) {
        return;
    }

    temp_env::async_with_vars(http_env(), async {
        let peer = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        // Without a lease or authentication RPC, etcd uses a lazy channel. This
        // lets DRT initialize before the deliberately stalled maintenance RPC.
        let etcd = crate::transports::etcd::Client::builder()
            .etcd_url(vec![format!("http://{}", peer.local_addr().unwrap())])
            .attach_lease(false)
            .build()
            .unwrap();
        let runtime = Runtime::from_current().unwrap();
        let drt = tokio::time::timeout(
            Duration::from_secs(5),
            DistributedRuntime::new_with_probe_policy(
                runtime.clone(),
                DistributedConfig {
                    discovery_backend: crate::distributed::DiscoveryBackend::KvStore(
                        crate::storage::kv::Selector::Etcd(Box::new(etcd)),
                    ),
                    ..DistributedConfig::process_local()
                },
                SystemProbePolicy::RuntimeOnly,
            ),
        )
        .await
        .unwrap()
        .unwrap();
        let info = drt.system_status_server_info().unwrap();
        let base = format!("http://{}", info.socket_addr);
        let client = client();
        let started = Instant::now();
        let mut health = tokio_util::task::AbortOnDropHandle::new(tokio::spawn({
            let client = client.clone();
            let url = format!("{base}/health");
            async move { client.get(url).send().await }
        }));
        let (_connection, _) = tokio::select! {
            biased;
            result = &mut health => {
                panic!("health completed before discovery connected: {result:?}")
            },
            peer = tokio::time::timeout(Duration::from_secs(5), peer.accept()) => {
                    peer.unwrap().unwrap()
                },
        };
        assert_eq!(status(&client, &base, "/live").await, 200);
        // The HTTP client has a longer deadline than the one-second handler.
        // A missing handler timeout produces a client error, not this HTTP 503.
        assert_eq!(health.await.unwrap().unwrap().status().as_u16(), 503);
        // Allow timer granularity while rejecting an immediate dependency error.
        assert!(started.elapsed() >= Duration::from_millis(900));
        runtime.shutdown();
        wait_closed(info.socket_addr).await;
    })
    .await;
}

#[tokio::test]
async fn etcd_readiness_requires_an_elected_leader() {
    if crate::test_utils::run_isolated(
        concat!(
            module_path!(),
            "::etcd_readiness_requires_an_elected_leader"
        ),
        &[],
    ) {
        return;
    }

    use std::sync::atomic::{AtomicU8, Ordering};

    temp_env::async_with_vars(http_env(), async {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let leader = Arc::new(AtomicU8::new(1));
        let response_leader = leader.clone();
        let _server = tokio_util::task::AbortOnDropHandle::new(tokio::spawn(async move {
            let (socket, _) = listener.accept().await.unwrap();
            let mut connection = h2::server::handshake(socket).await.unwrap();
            let mut requests = tokio::task::JoinSet::new();
            loop {
                tokio::select! {
                    Some(result) = requests.join_next(), if !requests.is_empty() => {
                        result.unwrap();
                    }
                    request = connection.accept() => {
                        let Some(request) = request else { break };
                        let (request, mut response) = request.unwrap();
                        let response_leader = response_leader.clone();
                        requests.spawn(async move {
                            assert_eq!(request.method(), axum::http::Method::POST);
                            assert_eq!(request.uri().path(), "/etcdserverpb.Maintenance/Status");
                            let mut input = request.into_body();
                            while let Some(data) = input.data().await {
                                let data = data.unwrap();
                                input.flow_control().release_capacity(data.len()).unwrap();
                            }
                            let headers = axum::http::Response::builder()
                                .header("content-type", "application/grpc")
                                .body(())
                                .unwrap();
                            let mut body = response.send_response(headers, false).unwrap();
                            // Unary gRPC frame: uncompressed, two-byte protobuf payload.
                            // etcd StatusResponse.leader is uint64 field 4 (tag 0x20).
                            // IDs 0 and 1 each fit in one protobuf varint byte.
                            let leader_id = response_leader.load(Ordering::SeqCst);
                            let frame = vec![0, 0, 0, 0, 2, 0x20, leader_id];
                            body.send_data(bytes::Bytes::from(frame), false).unwrap();
                            let mut trailers = axum::http::HeaderMap::new();
                            trailers.insert("grpc-status", "0".parse().unwrap());
                            body.send_trailers(trailers).unwrap();
                        });
                    }
                }
            }
        }));
        let etcd = crate::transports::etcd::Client::builder()
            .etcd_url(vec![format!("http://{address}")])
            .attach_lease(false)
            .build()
            .unwrap();
        let runtime = Runtime::from_current().unwrap();
        let drt = tokio::time::timeout(
            Duration::from_secs(5),
            DistributedRuntime::new_with_probe_policy(
                runtime.clone(),
                DistributedConfig {
                    discovery_backend: crate::distributed::DiscoveryBackend::KvStore(
                        crate::storage::kv::Selector::Etcd(Box::new(etcd)),
                    ),
                    ..DistributedConfig::process_local()
                },
                SystemProbePolicy::RuntimeOnly,
            ),
        )
        .await
        .unwrap()
        .unwrap();
        let info = drt.system_status_server_info().unwrap();
        let base = format!("http://{}", info.socket_addr);
        let client = client();
        for (leader_id, expected) in [(1, 200), (0, 503), (1, 200)] {
            leader.store(leader_id, Ordering::SeqCst);
            assert_eq!(status(&client, &base, "/health").await, expected);
            assert_eq!(status(&client, &base, "/live").await, 200);
        }
        runtime.shutdown();
        wait_closed(info.socket_addr).await;
    })
    .await;
}