dynamo-runtime 1.7.0

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
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

#[path = "common/selector.rs"]
mod selector;

use std::net::SocketAddr;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

use anyhow::{Error, Result};
use async_trait::async_trait;
use bytes::Bytes;
use futures::StreamExt;
use serde::{Deserialize, Serialize};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::{Notify, watch};

use dynamo_runtime::config::environment_names::llm::DYN_HTTP_BACKEND_STREAM_TIMEOUT_SECS;
use dynamo_runtime::config::environment_names::response_plane::DYN_RESPONSE_STREAM_ESTABLISH_TIMEOUT_SECS;
use dynamo_runtime::pipeline::network::egress::push_router::{
    PushRouter, PushRouterOptions, RouterMode,
};
use dynamo_runtime::{
    DistributedRuntime, Runtime,
    component::{Endpoint, Instance},
    discovery::EndpointInstanceId,
    distributed::DistributedConfig,
    engine::{AsyncEngine, AsyncEngineContextProvider},
    error::{DynamoError, ErrorType, match_error_chain},
    pipeline::{
        AddressedPushRouter, AddressedRequest, ManyIn, ManyOut, PipelineError, ResponseStream,
        SingleIn, StreamingDispatch,
        network::{Ingress, PushWorkHandler, ResponsePlaneMode},
    },
    protocols::maybe_error::MaybeError,
};

const ESTABLISH_TIMEOUT: Duration = Duration::from_secs(1);

#[derive(Clone, Debug, Deserialize, Serialize)]
struct TestResponse {
    #[serde(default)]
    error: Option<DynamoError>,
}

impl MaybeError for TestResponse {
    fn from_err(err: impl std::error::Error + 'static) -> Self {
        Self {
            error: Some(DynamoError::from(
                Box::new(err) as Box<dyn std::error::Error + 'static>
            )),
        }
    }

    fn err(&self) -> Option<DynamoError> {
        self.error.clone()
    }
}

struct NeverConnectsHandler {
    request_received: Arc<Notify>,
}

#[async_trait]
impl PushWorkHandler for NeverConnectsHandler {
    async fn handle_payload(
        &self,
        _payload: Bytes,
        _request_id: Option<String>,
    ) -> Result<(), PipelineError> {
        self.request_received.notify_one();
        Ok(())
    }

    fn add_metrics(
        &self,
        _endpoint: &dynamo_runtime::component::Endpoint,
        _metrics_labels: Option<&[(&str, &str)]>,
    ) -> Result<()> {
        Ok(())
    }
}

/// `Ingress` connects back to the frontend before calling `generate`, so
/// stalling here leaves the response stream connected but without a prologue.
struct StalledEngine {
    request_received: Arc<Notify>,
    release_request: Arc<Notify>,
}

#[async_trait]
impl AsyncEngine<SingleIn<u64>, ManyOut<TestResponse>, Error> for StalledEngine {
    async fn generate(&self, input: SingleIn<u64>) -> Result<ManyOut<TestResponse>, Error> {
        self.request_received.notify_one();
        self.release_request.notified().await;
        let (_request, context) = input.into_parts();
        Ok(ResponseStream::new(
            Box::pin(futures::stream::empty()),
            context.context(),
        ))
    }
}

/// Sends its prologue (by returning a stream) but produces no response until
/// released.
struct SilentEngine {
    release: Arc<Notify>,
}

#[async_trait]
impl AsyncEngine<SingleIn<u64>, ManyOut<TestResponse>, Error> for SilentEngine {
    async fn generate(&self, input: SingleIn<u64>) -> Result<ManyOut<TestResponse>, Error> {
        let (_request, context) = input.into_parts();
        let release = self.release.clone();
        Ok(ResponseStream::new(
            Box::pin(futures::stream::once(async move {
                release.notified().await;
                TestResponse { error: None }
            })),
            context.context(),
        ))
    }
}

/// Answers every request with a single successful response.
struct CountingEngine {
    requests: Arc<AtomicUsize>,
}

#[async_trait]
impl AsyncEngine<SingleIn<u64>, ManyOut<TestResponse>, Error> for CountingEngine {
    async fn generate(&self, input: SingleIn<u64>) -> Result<ManyOut<TestResponse>, Error> {
        self.requests.fetch_add(1, Ordering::SeqCst);
        let (_request, context) = input.into_parts();
        Ok(ResponseStream::new(
            Box::pin(futures::stream::iter([TestResponse { error: None }])),
            context.context(),
        ))
    }
}

/// TCP proxy between the frontend and the worker's request plane. Frontend
/// bytes pass through and are counted. Worker bytes, which carry the request
/// ACKs, are held until `release` is set.
struct HoldingProxy {
    addr: SocketAddr,
    forwarded: Arc<AtomicUsize>,
    release: watch::Sender<bool>,
}

async fn start_holding_proxy(upstream: SocketAddr) -> HoldingProxy {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let forwarded = Arc::new(AtomicUsize::new(0));
    let (release, release_rx) = watch::channel(false);
    let counter = forwarded.clone();
    tokio::spawn(async move {
        while let Ok((inbound, _)) = listener.accept().await {
            let outbound = TcpStream::connect(upstream).await.unwrap();
            let (mut inbound_read, mut inbound_write) = inbound.into_split();
            let (mut outbound_read, mut outbound_write) = outbound.into_split();
            let counter = counter.clone();
            tokio::spawn(async move {
                let mut buf = vec![0u8; 64 * 1024];
                loop {
                    let n = match inbound_read.read(&mut buf).await {
                        Ok(0) | Err(_) => break,
                        Ok(n) => n,
                    };
                    counter.fetch_add(n, Ordering::SeqCst);
                    if outbound_write.write_all(&buf[..n]).await.is_err() {
                        break;
                    }
                }
            });
            let mut release_rx = release_rx.clone();
            tokio::spawn(async move {
                let _ = release_rx.wait_for(|released| *released).await;
                let _ = tokio::io::copy(&mut outbound_read, &mut inbound_write).await;
            });
        }
    });
    HoldingProxy {
        addr,
        forwarded,
        release,
    }
}

/// Request-plane dispatch that sends every request through a proxy.
struct ProxiedDispatch {
    inner: Arc<AddressedPushRouter>,
    proxy: SocketAddr,
}

#[async_trait]
impl StreamingDispatch<u64, TestResponse> for ProxiedDispatch {
    async fn generate(
        &self,
        request: SingleIn<AddressedRequest<u64>>,
    ) -> Result<ManyOut<TestResponse>, Error> {
        let (addressed, context) = request.transfer(());
        let establish_timeout = addressed.establish_timeout();
        let (payload, address, instance) = addressed.into_parts();
        let (_, endpoint_path) = address.split_once('/').unwrap();
        let proxied = AddressedRequest::with_instance(
            payload,
            format!("{}/{endpoint_path}", self.proxy),
            instance.unwrap(),
        )
        .with_establish_timeout(establish_timeout);
        let (_, request) = context.transfer(proxied);
        StreamingDispatch::<u64, TestResponse>::generate(self.inner.as_ref(), request).await
    }

    async fn generate_bidirectional(
        &self,
        instance: Instance,
        address: String,
        input: ManyIn<u64>,
    ) -> Result<ManyOut<TestResponse>, Error> {
        StreamingDispatch::<u64, TestResponse>::generate_bidirectional(
            self.inner.as_ref(),
            instance,
            address,
            input,
        )
        .await
    }

    async fn on_instance_removed(&self, id: &EndpointInstanceId) {
        StreamingDispatch::<u64, TestResponse>::on_instance_removed(self.inner.as_ref(), id).await;
    }

    async fn on_instance_added(&self, id: &EndpointInstanceId) {
        StreamingDispatch::<u64, TestResponse>::on_instance_added(self.inner.as_ref(), id).await;
    }
}

fn test_endpoint(distributed: &DistributedRuntime, endpoint_name: &str) -> Endpoint {
    distributed
        .namespace("response_stream_establish_timeout".to_string())
        .unwrap()
        .component("backend".to_string())
        .unwrap()
        .endpoint(endpoint_name.to_string())
}

async fn poll_until(mut pred: impl FnMut() -> bool) -> bool {
    for _ in 0..250 {
        if pred() {
            return true;
        }
        tokio::time::sleep(Duration::from_millis(20)).await;
    }
    pred()
}

async fn assert_establish_timeout(
    distributed: &DistributedRuntime,
    endpoint_name: &str,
    handler: Arc<dyn PushWorkHandler>,
    request_received: Arc<Notify>,
    before_shutdown: impl FnOnce(),
) {
    let endpoint = test_endpoint(distributed, endpoint_name);
    let started = endpoint
        .clone()
        .endpoint_builder()
        .handler(handler)
        .graceful_shutdown(false)
        .start_with_registration()
        .await
        .unwrap();

    let client = endpoint.client().await.unwrap();
    let instance_id = client.wait_for_instances().await.unwrap()[0].id();
    let router = PushRouter::<u64, TestResponse>::from_client_with_options(
        client.clone(),
        RouterMode::RoundRobin,
        selector::options(),
    )
    .await
    .unwrap();

    let request = tokio::spawn(async move { router.generate(SingleIn::new(42)).await });
    tokio::time::timeout(Duration::from_secs(5), request_received.notified())
        .await
        .expect("worker did not receive the request");

    let error = tokio::time::timeout(Duration::from_secs(10), request)
        .await
        .expect("request remained blocked past the response timeout")
        .expect("request task panicked")
        .expect_err("request unexpectedly succeeded without a response stream");
    assert!(
        match_error_chain(error.as_ref(), &[ErrorType::ResponseTimeout], &[]),
        "{endpoint_name}: expected a response timeout, got: {error:#}"
    );
    assert!(
        !client.instance_ids_avail().contains(&instance_id),
        "{endpoint_name}: worker that never established a response stream should be quarantined"
    );

    before_shutdown();
    tokio::time::timeout(Duration::from_secs(5), started.shutdown())
        .await
        .expect("worker endpoint did not shut down")
        .unwrap();
}

/// The establish timeout stops at the prologue. A worker that sends its
/// prologue but no response is bounded by the inactivity timeout instead.
async fn assert_first_response_uses_inactivity_timeout(distributed: &DistributedRuntime) {
    let endpoint = test_endpoint(distributed, "prologue_without_first_response");
    let release = Arc::new(Notify::new());
    let handler: Arc<dyn PushWorkHandler> = Ingress::for_engine(Arc::new(SilentEngine {
        release: release.clone(),
    }))
    .unwrap();
    let started = endpoint
        .clone()
        .endpoint_builder()
        .handler(handler)
        .graceful_shutdown(false)
        .start_with_registration()
        .await
        .unwrap();
    let client = endpoint.client().await.unwrap();
    client.wait_for_instances().await.unwrap();
    let router = PushRouter::<u64, TestResponse>::from_client_with_options(
        client.clone(),
        RouterMode::RoundRobin,
        selector::options(),
    )
    .await
    .unwrap();

    let mut stream =
        tokio::time::timeout(Duration::from_secs(10), router.generate(SingleIn::new(7)))
            .await
            .expect("dispatch did not complete after the prologue")
            .expect("a worker that sent its prologue must not hit the establish timeout");
    let item = tokio::time::timeout(Duration::from_secs(15), stream.next())
        .await
        .expect("inactivity timeout did not fire")
        .expect("stream ended without the inactivity timeout error");
    let error = item.err().expect("expected the inactivity timeout error");
    assert!(
        match_error_chain(&error, &[ErrorType::ResponseTimeout], &[]),
        "expected a response timeout, got: {error}"
    );
    assert!(
        error.to_string().contains("inactivity"),
        "the first response must be bounded by the inactivity timeout, got: {error}"
    );

    drop(stream);
    release.notify_one();
    tokio::time::timeout(Duration::from_secs(5), started.shutdown())
        .await
        .expect("worker endpoint did not shut down")
        .unwrap();
}

/// Requests waiting on local admission or on the worker's ACK have not
/// reached the post-ACK phase. They must not time out or quarantine the worker,
/// however long the wait.
async fn assert_local_admission_wait_is_not_bounded(distributed: &DistributedRuntime) {
    let endpoint = test_endpoint(distributed, "saturated_local_admission");
    let requests = Arc::new(AtomicUsize::new(0));
    let handler: Arc<dyn PushWorkHandler> = Ingress::for_engine(Arc::new(CountingEngine {
        requests: requests.clone(),
    }))
    .unwrap();
    let started = endpoint
        .clone()
        .endpoint_builder()
        .handler(handler)
        .graceful_shutdown(false)
        .start_with_registration()
        .await
        .unwrap();
    let client = endpoint.client().await.unwrap();
    let instance = client.wait_for_instances().await.unwrap()[0].clone();
    let instance_id = instance.id();
    let worker_address = instance.transport.address();
    let worker_address = worker_address
        .strip_prefix("tcp://")
        .unwrap_or(worker_address);
    let (worker_socket, _) = worker_address.split_once('/').unwrap();
    let proxy = start_holding_proxy(worker_socket.parse().unwrap()).await;

    let dispatch = Arc::new(ProxiedDispatch {
        inner: AddressedPushRouter::from_runtime_provider(&endpoint)
            .await
            .unwrap(),
        proxy: proxy.addr,
    });
    let router = Arc::new(
        PushRouter::<u64, TestResponse>::from_client_with_options(
            client.clone(),
            RouterMode::RoundRobin,
            PushRouterOptions {
                dispatch: Some(dispatch),
                selectors: Some(selector::selectors()),
                ..PushRouterOptions::default()
            },
        )
        .await
        .unwrap(),
    );
    assert!(
        poll_until(|| client.instance_ids_avail().contains(&instance_id)).await,
        "precondition: worker should be available"
    );

    // The proxy holds the first request's ACK, so it keeps the client's only
    // admission permit and the second request waits on local admission.
    let first = tokio::spawn({
        let router = router.clone();
        async move { router.generate(SingleIn::new(1)).await }
    });
    assert!(
        poll_until(|| requests.load(Ordering::SeqCst) == 1).await,
        "worker did not receive the first request"
    );
    let forwarded_before = proxy.forwarded.load(Ordering::SeqCst);
    let second = tokio::spawn({
        let router = router.clone();
        async move { router.generate(SingleIn::new(2)).await }
    });

    tokio::time::sleep(ESTABLISH_TIMEOUT * 3).await;
    assert!(!first.is_finished(), "unACKed request must keep waiting");
    assert!(
        !second.is_finished(),
        "request waiting on local admission must keep waiting"
    );
    assert_eq!(
        proxy.forwarded.load(Ordering::SeqCst),
        forwarded_before,
        "request waiting on local admission must not send bytes"
    );
    assert_eq!(requests.load(Ordering::SeqCst), 1);
    assert!(
        client.instance_ids_avail().contains(&instance_id),
        "local admission wait must not quarantine the worker"
    );

    proxy.release.send(true).unwrap();
    for request in [first, second] {
        let mut stream = tokio::time::timeout(Duration::from_secs(10), request)
            .await
            .expect("request did not complete after the ACKs were released")
            .expect("request task panicked")
            .expect("request failed after the ACKs were released");
        let item = stream.next().await.expect("missing response");
        assert!(item.err().is_none(), "unexpected error: {:?}", item.err());
    }
    assert_eq!(requests.load(Ordering::SeqCst), 2);
    assert!(client.instance_ids_avail().contains(&instance_id));

    tokio::time::timeout(Duration::from_secs(5), started.shutdown())
        .await
        .expect("worker endpoint did not shut down")
        .unwrap();
}

// All cases share one runtime: the TCP request-plane server is process-wide
// and does not outlive the tokio runtime that started it. One request-plane
// connection with one admission permit lets a held ACK saturate local admission.
#[tokio::test]
async fn response_stream_establish_timeout_starts_at_ack() {
    let establish_secs = ESTABLISH_TIMEOUT.as_secs().to_string();
    temp_env::async_with_vars(
        [
            (
                DYN_RESPONSE_STREAM_ESTABLISH_TIMEOUT_SECS,
                Some(establish_secs.as_str()),
            ),
            (DYN_HTTP_BACKEND_STREAM_TIMEOUT_SECS, Some("4")),
            ("DYN_TCP_POOL_SIZE", Some("1")),
            ("DYN_TCP_CHANNEL_BUFFER", Some("1")),
            ("DYN_TCP_REQUEST_TIMEOUT", Some("30")),
        ],
        async {
            let runtime = Runtime::from_current().unwrap();
            let config = DistributedConfig {
                response_plane: Some(ResponsePlaneMode::Tcp),
                ..DistributedConfig::process_local()
            };
            let distributed = DistributedRuntime::new(runtime.clone(), config)
                .await
                .unwrap();

            let request_received = Arc::new(Notify::new());
            assert_establish_timeout(
                &distributed,
                "never_connects_back",
                Arc::new(NeverConnectsHandler {
                    request_received: request_received.clone(),
                }),
                request_received,
                || {},
            )
            .await;

            let request_received = Arc::new(Notify::new());
            let release_request = Arc::new(Notify::new());
            let stalled: Arc<dyn PushWorkHandler> = Ingress::for_engine(Arc::new(StalledEngine {
                request_received: request_received.clone(),
                release_request: release_request.clone(),
            }))
            .unwrap();
            assert_establish_timeout(
                &distributed,
                "stalls_before_prologue",
                stalled,
                request_received,
                || release_request.notify_one(),
            )
            .await;

            assert_first_response_uses_inactivity_timeout(&distributed).await;
            assert_local_admission_wait_is_not_bounded(&distributed).await;

            runtime.shutdown();
        },
    )
    .await;
}