coglet 0.21.0

High-performance prediction server for Cog ML models
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
//! Permit pool implementation with typestate for compile-time state transition safety.
//!
//! Slot poisoning is a pool-level property: a poisoned slot is permanently removed
//! from the pool regardless of whether a prediction was active on it.

use std::sync::Arc;
use std::sync::Mutex as StdMutex;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

use futures::SinkExt;
use tokio::net::unix::OwnedWriteHalf;
use tokio::sync::{Mutex, mpsc};
use tokio_util::codec::FramedWrite;

use crate::bridge::codec::JsonCodec;
use crate::bridge::protocol::{SlotId, SlotRequest};

pub(crate) struct PermitInner {
    pub slot_id: SlotId,
    pub writer: FramedWrite<OwnedWriteHalf, JsonCodec<SlotRequest>>,
    pub idle_flag: Arc<AtomicBool>,
    pub poisoned: Arc<AtomicBool>,
}

struct PoolConnection {
    pool_tx: mpsc::Sender<PermitInner>,
    pool_available: Arc<AtomicUsize>,
}

impl Clone for PoolConnection {
    fn clone(&self) -> Self {
        Self {
            pool_tx: self.pool_tx.clone(),
            pool_available: Arc::clone(&self.pool_available),
        }
    }
}

/// A permit actively running a prediction.
pub struct PermitInUse {
    slot_id: SlotId,
    writer: Option<FramedWrite<OwnedWriteHalf, JsonCodec<SlotRequest>>>,
    idle_flag: Arc<AtomicBool>,
    poisoned: Arc<AtomicBool>,
    pool: PoolConnection,
}

impl PermitInUse {
    pub(crate) fn new(
        inner: PermitInner,
        pool_tx: mpsc::Sender<PermitInner>,
        pool_available: Arc<AtomicUsize>,
    ) -> Self {
        inner.idle_flag.store(false, Ordering::Release);

        Self {
            slot_id: inner.slot_id,
            writer: Some(inner.writer),
            idle_flag: inner.idle_flag,
            poisoned: inner.poisoned,
            pool: PoolConnection {
                pool_tx,
                pool_available,
            },
        }
    }

    pub fn slot_id(&self) -> SlotId {
        self.slot_id
    }

    /// Transition to idle state - permit will return to pool on drop
    /// (unless the slot has been poisoned at the pool level).
    pub fn into_idle(mut self) -> PermitIdle {
        self.idle_flag.store(true, Ordering::Release);
        PermitIdle {
            slot_id: self.slot_id,
            writer: self.writer.take(),
            idle_flag: Arc::clone(&self.idle_flag),
            poisoned: Arc::clone(&self.poisoned),
            pool: self.pool.clone(),
        }
    }

    /// Transition to poisoned state - permit will NOT return to pool.
    ///
    /// Also sets the pool-level poison flag so the slot is never reused.
    pub fn into_poisoned(mut self) -> PermitPoisoned {
        self.poisoned.store(true, Ordering::Release);
        PermitPoisoned {
            slot_id: self.slot_id,
            _writer: self.writer.take(),
        }
    }

    pub async fn send(&mut self, request: SlotRequest) -> Result<(), PermitError> {
        let writer = self.writer.as_mut().ok_or(PermitError::Consumed)?;
        writer
            .send(request)
            .await
            .map_err(|e| PermitError::Send(e.to_string()))
    }
}

impl Drop for PermitInUse {
    fn drop(&mut self) {
        if self.writer.is_some() && !self.poisoned.load(Ordering::Acquire) {
            tracing::error!(slot = %self.slot_id, "PermitInUse dropped without state transition");
        }
    }
}

/// A permit that completed successfully - returns to pool on drop
/// (unless the slot has been poisoned at the pool level).
pub struct PermitIdle {
    slot_id: SlotId,
    writer: Option<FramedWrite<OwnedWriteHalf, JsonCodec<SlotRequest>>>,
    idle_flag: Arc<AtomicBool>,
    poisoned: Arc<AtomicBool>,
    pool: PoolConnection,
}

impl PermitIdle {
    pub fn slot_id(&self) -> SlotId {
        self.slot_id
    }
}

impl Drop for PermitIdle {
    fn drop(&mut self) {
        // If the slot was poisoned at the pool level, don't return it.
        if self.poisoned.load(Ordering::Acquire) {
            tracing::warn!(slot = %self.slot_id, "Slot poisoned - not returning to pool");
            return;
        }

        if let Some(writer) = self.writer.take() {
            let inner = PermitInner {
                slot_id: self.slot_id,
                writer,
                idle_flag: Arc::clone(&self.idle_flag),
                poisoned: Arc::clone(&self.poisoned),
            };

            if self.pool.pool_tx.try_send(inner).is_ok() {
                self.pool.pool_available.fetch_add(1, Ordering::Release);
            }
        }
    }
}

/// A poisoned permit - slot permanently failed, will NOT return to pool.
pub struct PermitPoisoned {
    slot_id: SlotId,
    _writer: Option<FramedWrite<OwnedWriteHalf, JsonCodec<SlotRequest>>>,
}

impl PermitPoisoned {
    pub fn slot_id(&self) -> SlotId {
        self.slot_id
    }
}

impl Drop for PermitPoisoned {
    fn drop(&mut self) {
        tracing::warn!(slot = %self.slot_id, "Slot poisoned - capacity reduced");
    }
}

/// A permit in any state (for containers needing dynamic state).
pub enum AnyPermit {
    InUse(PermitInUse),
    Idle(PermitIdle),
    Poisoned(PermitPoisoned),
}

impl AnyPermit {
    pub fn slot_id(&self) -> SlotId {
        match self {
            AnyPermit::InUse(p) => p.slot_id(),
            AnyPermit::Idle(p) => p.slot_id(),
            AnyPermit::Poisoned(p) => p.slot_id(),
        }
    }

    pub fn is_idle(&self) -> bool {
        matches!(self, AnyPermit::Idle(_))
    }

    pub fn is_poisoned(&self) -> bool {
        matches!(self, AnyPermit::Poisoned(_))
    }

    pub fn is_in_use(&self) -> bool {
        matches!(self, AnyPermit::InUse(_))
    }
}

#[must_use = "must be activated to enable slot idle transition"]
#[derive(Debug)]
pub struct InactiveSlotIdleToken {
    slot_id: SlotId,
}

impl InactiveSlotIdleToken {
    pub fn new(slot_id: SlotId) -> Self {
        Self { slot_id }
    }

    pub fn slot_id(&self) -> SlotId {
        self.slot_id
    }

    pub fn activate(self) -> SlotIdleToken {
        SlotIdleToken {
            slot_id: self.slot_id,
            create_time: std::time::Instant::now(),
            alarm_handle: tokio::spawn(async move {
                // This task exists solely to alert if the token isn't consumed within a reasonable time.
                // If we see this alert, it means the slot won't return to the pool until the process exits.
                tokio::time::sleep(SlotIdleToken::ALERT_THRESHOLD).await;
                tracing::error!(slot = %self.slot_id, "IdleToken not consumed after 5s - slot will not return to pool");
            }),
        }
    }
}

/// Token confirming the worker has marked the slot as idle, allowing the permit to return to the pool on drop.
#[must_use = "IdleToken confirms the worker has marked the slot as idle"]
#[derive(Debug)]
pub struct SlotIdleToken {
    pub(crate) slot_id: SlotId,
    pub(crate) create_time: std::time::Instant,
    pub(crate) alarm_handle: tokio::task::JoinHandle<()>,
}

impl SlotIdleToken {
    const ALERT_THRESHOLD: std::time::Duration = std::time::Duration::from_secs(5);

    pub fn slot_id(&self) -> SlotId {
        self.slot_id
    }

    pub fn consume(self) {
        let elapsed = self.create_time.elapsed();
        if elapsed > Self::ALERT_THRESHOLD {
            tracing::warn!(slot = %self.slot_id, latency = ?elapsed, "Delayed IdleToken Consumption");
        }
        tracing::debug!(slot = %self.slot_id, "IdleToken consumed");
    }
}

impl Drop for SlotIdleToken {
    fn drop(&mut self) {
        self.alarm_handle.abort();
    }
}

#[derive(Debug, Clone, thiserror::Error)]
pub enum PermitError {
    #[error("Permit already consumed")]
    Consumed,
    #[error("Failed to send on slot socket: {0}")]
    Send(String),
}

/// Pool of prediction slot permits.
///
/// Slot poisoning is tracked here. A poisoned slot is permanently removed
/// from the pool — its permit will not be returned or acquired again.
pub struct PermitPool {
    available_rx: Mutex<mpsc::Receiver<PermitInner>>,
    available_tx: mpsc::Sender<PermitInner>,
    num_slots: usize,
    available_count: Arc<AtomicUsize>,
    /// Per-slot poison flags, shared with permits for fast checking.
    poison_flags: StdMutex<Vec<(SlotId, Arc<AtomicBool>)>>,
}

impl PermitPool {
    pub fn new(num_slots: usize) -> Self {
        let (tx, rx) = mpsc::channel(num_slots);

        Self {
            available_rx: Mutex::new(rx),
            available_tx: tx,
            num_slots,
            available_count: Arc::new(AtomicUsize::new(0)),
            poison_flags: StdMutex::new(Vec::with_capacity(num_slots)),
        }
    }

    pub fn add_permit(
        &self,
        slot_id: SlotId,
        writer: FramedWrite<OwnedWriteHalf, JsonCodec<SlotRequest>>,
    ) {
        let poisoned = Arc::new(AtomicBool::new(false));

        // Store the flag for external poisoning.
        if let Ok(mut flags) = self.poison_flags.lock() {
            flags.push((slot_id, Arc::clone(&poisoned)));
        }

        let inner = PermitInner {
            slot_id,
            writer,
            idle_flag: Arc::new(AtomicBool::new(true)),
            poisoned,
        };

        if let Err(e) = self.available_tx.try_send(inner) {
            tracing::error!(slot = %slot_id, error = %e, "Failed to add permit to pool");
        } else {
            self.available_count.fetch_add(1, Ordering::Release);
        }
    }

    /// Poison a slot. The permit will not be returned to the pool.
    ///
    /// This works whether the slot is idle (in the pool) or in use (held by a prediction).
    /// - Idle: the permit will be discarded on next `acquire`/`try_acquire`.
    /// - In use: `PermitIdle::drop` will see the flag and not return it.
    pub fn poison(&self, slot_id: SlotId) {
        if let Ok(flags) = self.poison_flags.lock() {
            for (id, flag) in flags.iter() {
                if *id == slot_id {
                    if !flag.swap(true, Ordering::AcqRel) {
                        tracing::warn!(slot = %slot_id, "Slot poisoned - capacity permanently reduced");
                    }
                    return;
                }
            }
        }
        tracing::warn!(slot = %slot_id, "Attempted to poison unknown slot");
    }

    /// Check if a slot is poisoned.
    pub fn is_poisoned(&self, slot_id: SlotId) -> bool {
        if let Ok(flags) = self.poison_flags.lock() {
            for (id, flag) in flags.iter() {
                if *id == slot_id {
                    return flag.load(Ordering::Acquire);
                }
            }
        }
        false
    }

    pub fn try_acquire(&self) -> Option<PermitInUse> {
        let mut rx = self.available_rx.try_lock().ok()?;
        loop {
            let inner = rx.try_recv().ok()?;
            self.available_count.fetch_sub(1, Ordering::Release);

            // Skip poisoned permits — they're permanently dead.
            if inner.poisoned.load(Ordering::Acquire) {
                tracing::debug!(slot = %inner.slot_id, "Discarding poisoned permit from pool");
                continue;
            }

            return Some(PermitInUse::new(
                inner,
                self.available_tx.clone(),
                Arc::clone(&self.available_count),
            ));
        }
    }

    pub async fn acquire(&self) -> Option<PermitInUse> {
        let mut rx = self.available_rx.lock().await;
        loop {
            let inner = rx.recv().await?;
            self.available_count.fetch_sub(1, Ordering::Release);

            // Skip poisoned permits — they're permanently dead.
            if inner.poisoned.load(Ordering::Acquire) {
                tracing::debug!(slot = %inner.slot_id, "Discarding poisoned permit from pool");
                continue;
            }

            return Some(PermitInUse::new(
                inner,
                self.available_tx.clone(),
                Arc::clone(&self.available_count),
            ));
        }
    }

    pub fn num_slots(&self) -> usize {
        self.num_slots
    }

    pub fn available(&self) -> usize {
        self.available_count.load(Ordering::Acquire)
    }
}

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

    async fn make_socket_pair() -> (OwnedWriteHalf, tokio::net::unix::OwnedReadHalf) {
        let (a, b) = UnixStream::pair().unwrap();
        let (read, write) = a.into_split();
        let _ = b;
        (write, read)
    }

    #[tokio::test]
    async fn pool_add_and_acquire() {
        let pool = PermitPool::new(2);

        let (write1, _read1) = make_socket_pair().await;
        let (write2, _read2) = make_socket_pair().await;

        let slot1 = SlotId::new();
        let slot2 = SlotId::new();

        pool.add_permit(slot1, FramedWrite::new(write1, JsonCodec::new()));
        pool.add_permit(slot2, FramedWrite::new(write2, JsonCodec::new()));

        let p1 = pool.try_acquire();
        assert!(p1.is_some());

        let p2 = pool.try_acquire();
        assert!(p2.is_some());

        let p3 = pool.try_acquire();
        assert!(p3.is_none());
    }

    #[tokio::test]
    async fn permit_returns_to_pool_when_idle() {
        let pool = PermitPool::new(1);

        let (write, _read) = make_socket_pair().await;
        let slot = SlotId::new();

        pool.add_permit(slot, FramedWrite::new(write, JsonCodec::new()));

        {
            let permit = pool.try_acquire().unwrap();
            let _idle_permit = permit.into_idle();
        }

        let permit = pool.try_acquire();
        assert!(permit.is_some());
    }

    #[tokio::test]
    async fn permit_orphaned_when_poisoned() {
        let pool = PermitPool::new(1);

        let (write, _read) = make_socket_pair().await;
        let slot = SlotId::new();

        pool.add_permit(slot, FramedWrite::new(write, JsonCodec::new()));

        {
            let permit = pool.try_acquire().unwrap();
            let _poisoned_permit = permit.into_poisoned();
        }

        let permit = pool.try_acquire();
        assert!(permit.is_none());
    }

    #[tokio::test]
    async fn pool_poison_idle_slot() {
        // Poison a slot while it's idle in the pool — acquire should skip it.
        let pool = PermitPool::new(2);

        let (write1, _read1) = make_socket_pair().await;
        let (write2, _read2) = make_socket_pair().await;

        let slot1 = SlotId::new();
        let slot2 = SlotId::new();

        pool.add_permit(slot1, FramedWrite::new(write1, JsonCodec::new()));
        pool.add_permit(slot2, FramedWrite::new(write2, JsonCodec::new()));

        assert!(!pool.is_poisoned(slot1));
        pool.poison(slot1);
        assert!(pool.is_poisoned(slot1));
        assert!(!pool.is_poisoned(slot2));

        // First acquire should skip poisoned slot1, return slot2.
        let permit = pool.try_acquire().unwrap();
        assert_eq!(permit.slot_id(), slot2);

        // No more permits available.
        assert!(pool.try_acquire().is_none());
    }

    #[tokio::test]
    async fn pool_poison_in_use_slot_prevents_return() {
        // Poison a slot while a prediction holds it — into_idle + drop should NOT return it.
        let pool = PermitPool::new(1);

        let (write, _read) = make_socket_pair().await;
        let slot = SlotId::new();

        pool.add_permit(slot, FramedWrite::new(write, JsonCodec::new()));

        {
            let permit = pool.try_acquire().unwrap();
            // Poison while in use.
            pool.poison(slot);
            // Transition to idle — drop should see the poison flag.
            let _idle = permit.into_idle();
        }

        // Permit should NOT have returned to the pool.
        assert!(pool.try_acquire().is_none());
    }

    #[tokio::test]
    async fn pool_poison_is_idempotent() {
        let pool = PermitPool::new(1);

        let (write, _read) = make_socket_pair().await;
        let slot = SlotId::new();

        pool.add_permit(slot, FramedWrite::new(write, JsonCodec::new()));

        pool.poison(slot);
        pool.poison(slot); // Should not panic or double-count.
        assert!(pool.is_poisoned(slot));
    }
}