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

use std::{
    collections::HashMap,
    sync::{
        Arc, Weak,
        atomic::{AtomicU8, Ordering},
    },
    time::Duration,
};

use dynamo_kv_router::{
    multi_worker_sequence::{ReplicaRequestLeaseObserver, active_request_expiry_duration},
    scheduling::AttemptId,
};
use parking_lot::Mutex;
use tokio_util::sync::CancellationToken;

use super::{
    indexer::ApproximateRequestLease,
    scheduler::{SchedulerBookingCleanup, SchedulerBookingDescriptor},
};

const LEASE_QUIET: u8 = 0;
const LEASE_TOUCHED: u8 = 1;
const LEASE_CLAIMED: u8 = 2;

struct LeaseClock(AtomicU8);

impl LeaseClock {
    fn new() -> Self {
        Self(AtomicU8::new(LEASE_TOUCHED))
    }

    fn touch(&self) {
        let _ = self.0.compare_exchange(
            LEASE_QUIET,
            LEASE_TOUCHED,
            Ordering::AcqRel,
            Ordering::Acquire,
        );
    }

    fn is_active(&self) -> bool {
        self.0.load(Ordering::Acquire) != LEASE_CLAIMED
    }

    fn claim_now(&self) -> bool {
        self.0.swap(LEASE_CLAIMED, Ordering::AcqRel) != LEASE_CLAIMED
    }

    fn reap(&self) -> bool {
        match self.0.load(Ordering::Acquire) {
            LEASE_TOUCHED => {
                let _ = self.0.compare_exchange(
                    LEASE_TOUCHED,
                    LEASE_QUIET,
                    Ordering::AcqRel,
                    Ordering::Acquire,
                );
                false
            }
            LEASE_QUIET => self
                .0
                .compare_exchange(
                    LEASE_QUIET,
                    LEASE_CLAIMED,
                    Ordering::AcqRel,
                    Ordering::Acquire,
                )
                .is_ok(),
            LEASE_CLAIMED => false,
            state => unreachable!("invalid request lease CLOCK state {state}"),
        }
    }
}

struct RequestLeaseRecord {
    clock: LeaseClock,
    booking: SchedulerBookingDescriptor,
    approximate_lru: Option<ApproximateRequestLease>,
}

impl RequestLeaseRecord {
    fn new(
        booking: SchedulerBookingDescriptor,
        approximate_lru: Option<ApproximateRequestLease>,
    ) -> Self {
        Self {
            clock: LeaseClock::new(),
            booking,
            approximate_lru,
        }
    }
}

struct RequestLeaseManagerInner {
    active: Mutex<ActiveRequestLeases>,
    scheduler: SchedulerBookingCleanup,
}

#[derive(Default)]
struct ActiveRequestLeases {
    by_attempt: HashMap<AttemptId, Arc<RequestLeaseRecord>>,
    current_by_request: HashMap<String, AttemptId>,
}

impl RequestLeaseManagerInner {
    fn insert(&self, record: Arc<RequestLeaseRecord>, track_request_id: bool) {
        let attempt_id = record.booking.attempt_id;
        let mut active = self.active.lock();
        if let Some(existing) = active.by_attempt.get(&attempt_id) {
            if existing.booking == record.booking {
                existing.clock.touch();
                return;
            }
            tracing::error!(
                attempt_id = %attempt_id,
                existing_request_id = %existing.booking.request_id,
                replacement_request_id = %record.booking.request_id,
                "Duplicate request lease attempt ID; preserving the existing lease"
            );
            return;
        }
        if track_request_id {
            active
                .current_by_request
                .insert(record.booking.request_id.clone(), attempt_id);
        }
        active.by_attempt.insert(attempt_id, record);
    }

    fn matching_record(
        &self,
        booking: &SchedulerBookingDescriptor,
    ) -> Option<Arc<RequestLeaseRecord>> {
        self.active
            .lock()
            .by_attempt
            .get(&booking.attempt_id)
            .filter(|record| record.booking == *booking)
            .cloned()
    }

    fn current_record(&self, request_id: &str) -> Option<Arc<RequestLeaseRecord>> {
        let active = self.active.lock();
        let attempt_id = active.current_by_request.get(request_id)?;
        active.by_attempt.get(attempt_id).cloned()
    }

    fn remove(&self, record: &Arc<RequestLeaseRecord>) {
        let attempt_id = record.booking.attempt_id;
        let mut active = self.active.lock();
        if active
            .by_attempt
            .get(&attempt_id)
            .is_some_and(|current| Arc::ptr_eq(current, record))
        {
            active.by_attempt.remove(&attempt_id);
            if active
                .current_by_request
                .get(&record.booking.request_id)
                .is_some_and(|current| *current == attempt_id)
            {
                active.current_by_request.remove(&record.booking.request_id);
            }
        }
    }

    fn enqueue_completion(&self, record: &RequestLeaseRecord) {
        self.scheduler.enqueue(record.booking.clone());
        if let Some(approximate_lru) = &record.approximate_lru {
            approximate_lru.release_now();
        }
    }

    fn enqueue_expiry(&self, record: &RequestLeaseRecord) {
        // NOTE: Request-liveness expiry is deliberately isolated to this router.
        // Local and mirrored scheduler copies expire independently, and only an
        // explicit lifecycle completion publishes `Free` to peer routers.
        self.scheduler.enqueue_expired(record.booking.clone());
        if let Some(approximate_lru) = &record.approximate_lru {
            approximate_lru.release_now();
        }
    }

    fn reap(&self) {
        let records = self
            .active
            .lock()
            .by_attempt
            .values()
            .cloned()
            .collect::<Vec<_>>();
        for record in records {
            if !record.clock.reap() {
                continue;
            }
            self.remove(&record);
            self.enqueue_expiry(&record);
        }
    }
}

/// One request-liveness coordinator and periodic reaper per `KvRouter`.
#[derive(Clone)]
pub(crate) struct RequestLeaseManager {
    inner: Arc<RequestLeaseManagerInner>,
}

impl RequestLeaseManager {
    pub(crate) fn new(scheduler: SchedulerBookingCleanup, cancellation: CancellationToken) -> Self {
        let inner = Arc::new(RequestLeaseManagerInner {
            active: Mutex::new(ActiveRequestLeases::default()),
            scheduler,
        });
        start_reaper(
            Arc::downgrade(&inner),
            active_request_expiry_duration(),
            cancellation,
        );
        Self { inner }
    }

    pub(crate) fn register_local(
        &self,
        booking: SchedulerBookingDescriptor,
        approximate_lru: Option<ApproximateRequestLease>,
    ) -> RequestAttemptLease {
        let record = Arc::new(RequestLeaseRecord::new(booking, approximate_lru));
        self.inner.insert(Arc::clone(&record), false);
        RequestAttemptLease {
            manager: self.clone(),
            record,
        }
    }

    pub(crate) fn register_detached(
        &self,
        booking: SchedulerBookingDescriptor,
        approximate_lru: Option<ApproximateRequestLease>,
    ) -> DetachedRequestLeaseEnrollment {
        let record = Arc::new(RequestLeaseRecord::new(booking, approximate_lru));
        self.inner.insert(Arc::clone(&record), true);
        DetachedRequestLeaseEnrollment {
            manager: self.clone(),
            record,
            armed: true,
        }
    }

    pub(crate) fn touch_request(&self, request_id: &str) {
        if let Some(record) = self.inner.current_record(request_id) {
            record.clock.touch();
        }
    }

    pub(crate) async fn finish_request(&self, request_id: &str) -> bool {
        let Some(record) = self.inner.current_record(request_id) else {
            return false;
        };
        self.finish(&record).await;
        true
    }

    fn register_remote(&self, booking: SchedulerBookingDescriptor) {
        self.inner
            .insert(Arc::new(RequestLeaseRecord::new(booking, None)), false);
    }

    fn touch_booking(&self, booking: &SchedulerBookingDescriptor) {
        if let Some(record) = self.inner.matching_record(booking) {
            record.clock.touch();
        }
    }

    fn complete_remote(&self, booking: &SchedulerBookingDescriptor) {
        let Some(record) = self.inner.matching_record(booking) else {
            return;
        };
        if record.clock.claim_now() {
            self.inner.remove(&record);
        }
    }

    fn enqueue_completion(&self, record: &Arc<RequestLeaseRecord>) {
        if !record.clock.claim_now() {
            return;
        }
        self.inner.remove(record);
        self.inner.enqueue_completion(record);
    }

    async fn finish(&self, record: &Arc<RequestLeaseRecord>) {
        if !record.clock.claim_now() {
            return;
        }
        self.inner.remove(record);

        // Enqueue both subsystem commands before the first await. Cancellation of
        // the finishing future therefore cannot strand either cleanup.
        let scheduler_ack = self
            .inner
            .scheduler
            .enqueue_acknowledged(record.booking.clone());
        let lru_ack = record
            .approximate_lru
            .as_ref()
            .map(ApproximateRequestLease::begin_finish)
            .transpose();

        if let Err(error) = scheduler_ack.wait().await {
            tracing::warn!(
                request_id = %record.booking.request_id,
                worker = ?record.booking.worker,
                attempt_id = %record.booking.attempt_id,
                %error,
                "Failed to release scheduler booking"
            );
        }
        match lru_ack {
            Ok(Some(Some(ack))) => {
                if let Err(error) = ack.wait().await {
                    tracing::warn!(
                        request_id = %record.booking.request_id,
                        worker = ?record.booking.worker,
                        attempt_id = %record.booking.attempt_id,
                        %error,
                        "Failed to release approximate LRU request lease"
                    );
                }
            }
            Ok(Some(None)) | Ok(None) => {}
            Err(error) => tracing::warn!(
                request_id = %record.booking.request_id,
                worker = ?record.booking.worker,
                attempt_id = %record.booking.attempt_id,
                %error,
                "Failed to enqueue approximate LRU request release"
            ),
        }
    }
}

impl ReplicaRequestLeaseObserver for RequestLeaseManager {
    fn admitted(&self, booking: SchedulerBookingDescriptor) {
        self.register_remote(booking);
    }

    fn progressed(&self, booking: &SchedulerBookingDescriptor) {
        self.touch_booking(booking);
    }

    fn completed(&self, booking: &SchedulerBookingDescriptor) {
        self.complete_remote(booking);
    }
}

pub(crate) struct RequestAttemptLease {
    manager: RequestLeaseManager,
    record: Arc<RequestLeaseRecord>,
}

impl RequestAttemptLease {
    pub(crate) fn booking(&self) -> &SchedulerBookingDescriptor {
        &self.record.booking
    }

    pub(crate) fn touch(&self) {
        self.record.clock.touch();
    }

    pub(crate) fn is_active(&self) -> bool {
        self.record.clock.is_active()
    }

    pub(crate) async fn finish(&self) {
        self.manager.finish(&self.record).await;
    }
}

impl Drop for RequestAttemptLease {
    fn drop(&mut self) {
        self.manager.enqueue_completion(&self.record);
    }
}

/// Cancellation owner while a public admission installs its detached lifecycle.
/// Once committed, the manager retains the record until explicit completion or expiry.
#[must_use = "detached request enrollment must be committed or cleaned up"]
pub(crate) struct DetachedRequestLeaseEnrollment {
    manager: RequestLeaseManager,
    record: Arc<RequestLeaseRecord>,
    armed: bool,
}

impl DetachedRequestLeaseEnrollment {
    pub(crate) fn commit(mut self) {
        self.armed = false;
    }

    pub(crate) async fn finish(mut self) {
        self.manager.finish(&self.record).await;
        self.armed = false;
    }
}

impl Drop for DetachedRequestLeaseEnrollment {
    fn drop(&mut self) {
        if self.armed {
            self.manager.enqueue_completion(&self.record);
        }
    }
}

fn start_reaper(
    manager: Weak<RequestLeaseManagerInner>,
    scan_interval: Duration,
    cancellation: CancellationToken,
) {
    tokio::spawn(async move {
        let mut interval = tokio::time::interval(scan_interval);
        interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
        loop {
            tokio::select! {
                _ = cancellation.cancelled() => break,
                _ = interval.tick() => {
                    let Some(manager) = manager.upgrade() else {
                        break;
                    };
                    // NOTE: This is deliberately a two-scan, second-chance (2S)
                    // approximation. A touched lease becomes quiet on one scan and
                    // is eligible for cleanup on the next. Cache-retention TTL is
                    // a separate policy and never enters this manager.
                    manager.reap();
                }
            }
        }
    });
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn clock_coalesces_progress_and_cannot_resurrect_a_claimed_lease() {
        let clock = LeaseClock::new();

        assert!(!clock.reap());
        clock.touch();
        clock.touch();
        assert!(!clock.reap());
        assert!(clock.reap());
        assert!(!clock.is_active());

        clock.touch();
        assert!(!clock.is_active());
        assert!(!clock.reap());
    }
}