dynamo-llm 1.5.1

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

//! Decouples publication drivers from Relay internals through state watches and generation-safe
//! streams.

use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use dynamo_kv_router::indexer::cuckoo::ProducerIdentity;
use tokio::sync::{OwnedSemaphorePermit, Semaphore, watch};
use tokio_util::sync::CancellationToken;

use super::super::identity::{DcPoolCatalog, DcRelayIdentity};
use super::super::load::PoolLoadSnapshot;
use super::super::pool_registry::PoolRegistry;
use super::super::topology::{TopologyPublisher, TopologySnapshot};
use super::hub::PublicationHubError;
use super::stream::{self, PoolPublicationStream, TerminalPublicationFailure};

pub(in crate::kv_dc_relay) const DEFAULT_SNAPSHOT_PROGRESS_TIMEOUT: Duration =
    Duration::from_secs(60);
pub(in crate::kv_dc_relay) const DEFAULT_SNAPSHOT_ENCODING_CONCURRENCY: usize = 2;
pub(in crate::kv_dc_relay) const DEFAULT_ACTIVE_POOL_STREAMS: usize = 64;

mod private {
    pub trait Sealed {}
}

/// Stable error categories that publication drivers can map to their transport.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum PublicationErrorKind {
    /// The requested pool is not active.
    NotFound,
    /// The pool is active, but its generation differs from the request.
    ProducerMismatch,
    /// Publication state is temporarily unavailable and requires a fresh stream.
    Unavailable,
    /// A bounded Relay publication resource was exhausted.
    ResourceExhausted,
    /// The source produced an invalid identity, format, or sequence transition.
    InvalidPublication,
    /// Internal snapshot encoding failed.
    Internal,
}

/// Failure to open or advance a pool publication stream.
#[derive(Debug, thiserror::Error)]
#[error("{message}")]
pub struct PublicationError {
    kind: PublicationErrorKind,
    message: String,
}

impl PublicationError {
    /// Transport-neutral classification for driver-specific error mapping.
    pub const fn kind(&self) -> PublicationErrorKind {
        self.kind
    }

    pub(crate) fn unavailable(message: impl Into<String>) -> Self {
        Self::new(PublicationErrorKind::Unavailable, message)
    }

    pub(crate) fn resource_exhausted(message: impl Into<String>) -> Self {
        Self::new(PublicationErrorKind::ResourceExhausted, message)
    }

    pub(crate) fn invalid_publication(message: impl Into<String>) -> Self {
        Self::new(PublicationErrorKind::InvalidPublication, message)
    }

    pub(crate) fn internal(message: impl Into<String>) -> Self {
        Self::new(PublicationErrorKind::Internal, message)
    }

    fn new(kind: PublicationErrorKind, message: impl Into<String>) -> Self {
        Self {
            kind,
            message: message.into(),
        }
    }
}

impl From<PublicationHubError> for PublicationError {
    fn from(error: PublicationHubError) -> Self {
        let kind = match error {
            PublicationHubError::UnknownPool(_) => PublicationErrorKind::NotFound,
            PublicationHubError::ProducerMismatch(_) => PublicationErrorKind::ProducerMismatch,
            PublicationHubError::Unavailable(_) => PublicationErrorKind::Unavailable,
            PublicationHubError::SubscriberLimit { .. }
            | PublicationHubError::InitializedHubLimit { .. }
            | PublicationHubError::SubscriberLagged(_) => PublicationErrorKind::ResourceExhausted,
            PublicationHubError::IdentityChanged { .. }
            | PublicationHubError::LeaseChanged { .. }
            | PublicationHubError::SequenceGap { .. }
            | PublicationHubError::BucketOutOfRange { .. } => {
                PublicationErrorKind::InvalidPublication
            }
        };
        Self::new(kind, error.to_string())
    }
}

/// A driver-facing, read-only view of the Relay's publication state.
///
/// Implementations own snapshot bootstrapping and stream continuity. Drivers
/// receive canonical frames and must not interact with actors or publication
/// hubs directly.
#[async_trait]
pub trait RelayPublicationSource: private::Sealed + Send + Sync {
    /// Identity of the Relay runtime that owns all advertised generations.
    fn relay_identity(&self) -> DcRelayIdentity;

    /// Watches complete pool-catalog snapshots, including the current value.
    fn watch_catalog(&self) -> watch::Receiver<DcPoolCatalog>;

    /// Watches complete serving-readiness snapshots, including the current value.
    fn watch_readiness(&self) -> watch::Receiver<Arc<TopologySnapshot>>;

    /// Watches complete authoritative pool-load snapshots, including the current value.
    fn watch_load(&self) -> watch::Receiver<Vec<PoolLoadSnapshot>>;

    /// Completes when the Relay begins shutting down.
    ///
    /// This is a read-only lifecycle signal. Dropping the returned future or the
    /// source does not affect Relay lifetime.
    async fn wait_for_shutdown(&self);

    /// Opens the exact producer generation requested by the driver.
    ///
    /// The returned stream begins with a complete snapshot and then emits
    /// contiguous deltas. Any lag, generation change, or sequence gap terminates
    /// the stream with an error; callers must open a new stream and snapshot.
    async fn subscribe_pool(
        &self,
        expected: ProducerIdentity,
    ) -> Result<PoolPublicationStream, PublicationError>;
}

#[derive(Clone)]
pub(crate) struct RegistryPublicationSource {
    pools: Arc<PoolRegistry>,
    topology: Arc<TopologyPublisher>,
    relay_identity: DcRelayIdentity,
    lifecycle: CancellationToken,
    snapshot_encoding_permits: Arc<Semaphore>,
    active_stream_permits: Arc<Semaphore>,
    max_active_streams: usize,
    snapshot_progress_timeout: Duration,
}

impl RegistryPublicationSource {
    pub(crate) fn new(
        pools: Arc<PoolRegistry>,
        topology: Arc<TopologyPublisher>,
        relay_identity: DcRelayIdentity,
        lifecycle: CancellationToken,
        snapshot_encoding_permits: Arc<Semaphore>,
        max_active_streams: usize,
        snapshot_progress_timeout: Duration,
    ) -> Self {
        debug_assert!(!snapshot_progress_timeout.is_zero());
        debug_assert_ne!(max_active_streams, 0);
        Self {
            pools,
            topology,
            relay_identity,
            lifecycle,
            snapshot_encoding_permits,
            active_stream_permits: Arc::new(Semaphore::new(max_active_streams)),
            max_active_streams,
            snapshot_progress_timeout,
        }
    }

    async fn acquire_stream_permits(
        &self,
        generation_cancel: &CancellationToken,
    ) -> Result<(OwnedSemaphorePermit, OwnedSemaphorePermit), PublicationError> {
        if self.lifecycle.is_cancelled() {
            return Err(PublicationError::unavailable(
                "publication source is shutting down",
            ));
        }

        let active_stream_permit = self
            .active_stream_permits
            .clone()
            .try_acquire_owned()
            .map_err(|_| {
                PublicationError::resource_exhausted(format!(
                    "Relay reached its active pool stream limit {}",
                    self.max_active_streams
                ))
            })?;

        let snapshot_encoding_permit = tokio::select! {
            biased;
            _ = self.lifecycle.cancelled() => {
                return Err(PublicationError::unavailable(
                    "publication source is shutting down",
                ));
            }
            _ = generation_cancel.cancelled() => {
                return Err(PublicationError::unavailable(
                    "pool generation retired while waiting for publication admission",
                ));
            }
            permit = self.snapshot_encoding_permits.clone().acquire_owned() => permit
                .map_err(|_| PublicationError::unavailable(
                    "publication snapshot encoder is shutting down",
                ))?,
        };

        Ok((active_stream_permit, snapshot_encoding_permit))
    }

    fn terminal_failure(&self, expected: ProducerIdentity) -> TerminalPublicationFailure {
        let pools = Arc::downgrade(&self.pools);
        Arc::new(move |reason| {
            let Some(pools) = pools.upgrade() else {
                return;
            };
            let reason = format!("publication stream: {reason}");
            pools.fence_generation(expected.pool_id(), expected.layout_generation(), &reason);
        })
    }
}

impl private::Sealed for RegistryPublicationSource {}

#[async_trait]
impl RelayPublicationSource for RegistryPublicationSource {
    fn relay_identity(&self) -> DcRelayIdentity {
        self.relay_identity
    }

    fn watch_catalog(&self) -> watch::Receiver<DcPoolCatalog> {
        self.pools.watch_catalog()
    }

    fn watch_readiness(&self) -> watch::Receiver<Arc<TopologySnapshot>> {
        self.topology.watch()
    }

    fn watch_load(&self) -> watch::Receiver<Vec<PoolLoadSnapshot>> {
        self.pools.watch_load()
    }

    async fn wait_for_shutdown(&self) {
        self.lifecycle.cancelled().await;
    }

    async fn subscribe_pool(
        &self,
        expected: ProducerIdentity,
    ) -> Result<PoolPublicationStream, PublicationError> {
        if self.lifecycle.is_cancelled() {
            return Err(PublicationError::unavailable(
                "publication source is shutting down",
            ));
        }
        let generation_cancel = self.pools.validate_active_producer(expected)?;
        // Acquire encoding capacity first so permit waiters cannot pin a generation snapshot and
        // force full-lane COW while publication advances.
        let (active_stream_permit, snapshot_encoding_permit) =
            self.acquire_stream_permits(&generation_cancel).await?;
        let subscription = tokio::select! {
            biased;
            _ = self.lifecycle.cancelled() => {
                return Err(PublicationError::unavailable(
                    "publication source is shutting down",
                ));
            }
            subscription = self.pools.subscribe_pool(expected) => subscription?,
        };
        if self.lifecycle.is_cancelled() {
            return Err(PublicationError::unavailable(
                "publication source is shutting down",
            ));
        }
        stream::open(
            subscription,
            expected,
            active_stream_permit,
            snapshot_encoding_permit,
            self.snapshot_progress_timeout,
            self.lifecycle.clone(),
            self.terminal_failure(expected),
        )
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use dynamo_kv_router::identity::{
        CacheSemanticsId, DcId, IdentitySource, IndexerDomainId, PoolId, RoutingScopeId,
    };
    use dynamo_kv_router::indexer::cuckoo::{CkfConfig, DcCkfState};

    use super::*;
    use crate::kv_dc_relay::discovery::DcMembershipView;
    use crate::kv_dc_relay::pool_registry::{PoolActorConfig, PoolPublicationConfig};

    fn producer() -> ProducerIdentity {
        let format = DcCkfState::new(CkfConfig::new(32))
            .expect("fixture state")
            .format();
        ProducerIdentity::new(
            PoolId::new(
                IndexerDomainId::new(
                    CacheSemanticsId::new([1; 16], IdentitySource::Explicit),
                    RoutingScopeId::new([2; 16], IdentitySource::Explicit),
                ),
                DcId::new(3),
            ),
            7,
            11,
            format,
        )
    }

    fn source(
        lifecycle: CancellationToken,
        snapshot_encoding_permits: Arc<Semaphore>,
        max_active_streams: usize,
    ) -> Arc<RegistryPublicationSource> {
        let relay_identity = DcRelayIdentity::new(11, 7);
        let pools = Arc::new(PoolRegistry::new_with_publication_config(
            relay_identity,
            PoolActorConfig {
                expected_unique_blocks: 32,
                publication_threshold: 1,
                publication_delay: Duration::from_millis(1),
            },
            PoolPublicationConfig::default(),
        ));
        let topology = Arc::new(TopologyPublisher::new(
            DcMembershipView::default(),
            &pools.catalog(),
        ));
        Arc::new(RegistryPublicationSource::new(
            pools,
            topology,
            relay_identity,
            lifecycle,
            snapshot_encoding_permits,
            max_active_streams,
            DEFAULT_SNAPSHOT_PROGRESS_TIMEOUT,
        ))
    }

    #[tokio::test]
    async fn lifecycle_cancels_snapshot_permit_wait_without_cancel_authority() {
        let lifecycle = CancellationToken::new();
        let source = source(lifecycle.clone(), Arc::new(Semaphore::new(0)), 1);
        let generation_cancel = CancellationToken::new();
        let waiting_source = source.clone();
        let subscribe = tokio::spawn(async move {
            waiting_source
                .acquire_stream_permits(&generation_cancel)
                .await
        });
        tokio::time::timeout(Duration::from_secs(1), async {
            while source.active_stream_permits.available_permits() != 0 {
                tokio::task::yield_now().await;
            }
        })
        .await
        .expect("permit waiter must hold the active stream permit");

        let observing_source = source.clone();
        let lifecycle_wait = tokio::spawn(async move {
            observing_source.wait_for_shutdown().await;
        });
        lifecycle.cancel();

        tokio::time::timeout(Duration::from_secs(1), lifecycle_wait)
            .await
            .expect("lifecycle observer must wake")
            .expect("lifecycle observer task");
        let result = tokio::time::timeout(Duration::from_secs(1), subscribe)
            .await
            .expect("subscribe permit wait must cancel")
            .expect("subscribe task");
        let error = match result {
            Ok(_) => panic!("shutdown returned a publication stream"),
            Err(error) => error,
        };
        assert_eq!(error.kind(), PublicationErrorKind::Unavailable);
        assert_eq!(source.active_stream_permits.available_permits(), 1);
    }

    #[tokio::test]
    async fn generation_retirement_cancels_snapshot_permit_wait() {
        let lifecycle = CancellationToken::new();
        let source = source(lifecycle.clone(), Arc::new(Semaphore::new(0)), 1);
        let generation_cancel = CancellationToken::new();
        let waiting_cancel = generation_cancel.clone();
        let waiting_source = source.clone();
        let subscribe =
            tokio::spawn(
                async move { waiting_source.acquire_stream_permits(&waiting_cancel).await },
            );
        tokio::time::timeout(Duration::from_secs(1), async {
            while source.active_stream_permits.available_permits() != 0 {
                tokio::task::yield_now().await;
            }
        })
        .await
        .expect("permit waiter must hold the active stream permit");

        generation_cancel.cancel();

        let result = tokio::time::timeout(Duration::from_secs(1), subscribe)
            .await
            .expect("generation retirement must cancel the permit wait")
            .expect("subscribe task");
        let error = match result {
            Ok(_) => panic!("retired generation acquired publication admission"),
            Err(error) => error,
        };
        assert_eq!(error.kind(), PublicationErrorKind::Unavailable);
        assert!(!lifecycle.is_cancelled());
        assert_eq!(source.active_stream_permits.available_permits(), 1);
    }

    #[tokio::test]
    async fn unknown_generation_is_rejected_before_permit_wait() {
        let source = source(CancellationToken::new(), Arc::new(Semaphore::new(0)), 1);

        let result =
            tokio::time::timeout(Duration::from_secs(1), source.subscribe_pool(producer()))
                .await
                .expect("identity precheck must not wait for a snapshot permit");
        let error = match result {
            Ok(_) => panic!("unknown generation opened a publication stream"),
            Err(error) => error,
        };

        assert_eq!(error.kind(), PublicationErrorKind::NotFound);
        assert_eq!(source.active_stream_permits.available_permits(), 1);
    }

    #[tokio::test]
    async fn active_pool_stream_limit_is_global_and_fail_fast() {
        let lifecycle = CancellationToken::new();
        let source = source(lifecycle.clone(), Arc::new(Semaphore::new(0)), 1);
        let generation_cancel = CancellationToken::new();
        let waiting_cancel = generation_cancel.clone();
        let waiting_source = source.clone();
        let first =
            tokio::spawn(
                async move { waiting_source.acquire_stream_permits(&waiting_cancel).await },
            );
        tokio::time::timeout(Duration::from_secs(1), async {
            while source.active_stream_permits.available_permits() != 0 {
                tokio::task::yield_now().await;
            }
        })
        .await
        .expect("first stream must hold the global permit");

        let error = match source.acquire_stream_permits(&generation_cancel).await {
            Ok(_) => panic!("second stream exceeded the global limit"),
            Err(error) => error,
        };
        assert_eq!(error.kind(), PublicationErrorKind::ResourceExhausted);

        lifecycle.cancel();
        assert!(first.await.expect("first subscribe task").is_err());
        assert_eq!(source.active_stream_permits.available_permits(), 1);
    }
}