tollgate-client 0.32.0

Instance-side quota runtime: background lease refill into the admission layer's LeaseSlot, and a bounded, batched, shed-on-overflow usage writer.
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
561
562
563
564
//! Off-path account membership and stable lease slots.
use std::collections::{BTreeSet, HashMap, HashSet};
use std::sync::{Arc, Mutex};

use jiff::Timestamp;
use tokio::sync::watch;
use tollgate_admission::{LeaseSlot, MapEntry};
use tollgate_core::{
    AccountId, AccountSnapshot, AccountStatus, EnforcementMode, LocalSharding, Principal,
};

// Every diagnostic sum has at most usize::MAX u64 contributions. The
// u128 bound is proved in AccountLifecycle.catalogue_total_fits; make the
// cardinality assumption a compile-time restriction on supported targets.
const _: () = assert!(usize::BITS <= 64);

/// Instance-wide diagnostic estimates. None means no contributing policy or
/// lease. Checked u128 sums cover every supported in-memory catalogue.
/// No admission reads this view.
#[derive(Debug, Clone, Default)]
pub struct RuntimeFundingReport {
    /// Units remaining on every retained account's current lease, summed.
    /// `None` when no slot holds a lease.
    pub total_lease_remaining: Option<u128>,
    /// Overage units spent under elastic enforcement across every retained
    /// slot, for the process's life. Irreversible: it never falls, and a
    /// rising value is work admitted past funding that will be invoiced.
    pub total_overage_spent: u128,
    /// Per-instance overage caps summed over eligible accounts, taking each
    /// account's largest cap among its active, unexpired snapshots. `None`
    /// when no eligible account has a cap. Fleet exposure is this times the
    /// number of instances.
    pub total_overage_cap: Option<u128>,
    /// The earliest `usable_until` among eligible accounts' current leases:
    /// when the next lease stops funding work unless refill replaces it.
    /// `None` when no eligible account holds a lease.
    pub earliest_lease_usable_until: Option<Timestamp>,
}

/// Shared account slots. Slots retain irreversible overage spend for the
/// process lifetime; membership, timers, and managers have separate lifetimes.
pub struct SlotRegistry {
    inner: Mutex<Registry>,
    sharding: LocalSharding,
    changed: Option<watch::Sender<()>>,
}

#[derive(Default)]
struct Registry {
    slots: HashMap<AccountId, Arc<LeaseSlot>>,
    principals: HashMap<Principal, Arc<AccountSnapshot>>,
    members: HashMap<AccountId, HashMap<Principal, Arc<AccountSnapshot>>>,
    deadlines: BTreeSet<(Timestamp, Principal)>,
    dirty: BTreeSet<AccountId>,
    tracked: HashSet<Principal>,
    resolutions: HashMap<Principal, Timestamp>,
}

#[derive(Clone)]
pub(crate) struct AccountBinding {
    pub account: AccountId,
    pub slot: Arc<LeaseSlot>,
    pub snapshots: Vec<Arc<AccountSnapshot>>,
}

impl AccountBinding {
    pub fn eligible(&self, now: Timestamp) -> bool {
        self.snapshots
            .iter()
            .any(|snapshot| snapshot.status == AccountStatus::Active && now < snapshot.valid_until)
    }

    pub fn fundable(&self, now: Timestamp) -> bool {
        self.snapshots.iter().any(|snapshot| {
            snapshot.status == AccountStatus::Active
                && now < snapshot.valid_until
                && quota_usable(&self.slot, snapshot.enforcement_mode, now)
        })
    }
}

impl Default for SlotRegistry {
    fn default() -> Self {
        Self {
            inner: Mutex::new(Registry::default()),
            sharding: LocalSharding::SINGLE,
            changed: None,
        }
    }
}

impl SlotRegistry {
    pub(crate) fn track(&self, principal: Principal) {
        if self.observes() {
            self.inner
                .lock()
                .expect("slot registry poisoned")
                .tracked
                .insert(principal);
        }
    }

    pub(crate) fn retain(&self, principals: &HashSet<Principal>) {
        if self.observes() {
            let mut inner = self.inner.lock().expect("slot registry poisoned");
            inner.tracked.clone_from(principals);
            // The predicate is pure and total -- every untracked resolution
            // goes, whatever order they are visited in.
            #[allow(
                clippy::disallowed_methods,
                reason = "pure, total predicate: the visit order cannot change which entries survive"
            )]
            inner
                .resolutions
                .retain(|principal, _| principals.contains(principal));
        }
    }

    pub(crate) fn resolution_counts(&self, now: Timestamp) -> (usize, usize) {
        let inner = self.inner.lock().expect("slot registry poisoned");
        #[allow(
            clippy::disallowed_methods,
            reason = "counts unresolved principals; a count does not depend on the order they are counted in"
        )]
        let unresolved = inner
            .tracked
            .iter()
            .filter(|principal| {
                inner
                    .resolutions
                    .get(principal)
                    .is_none_or(|until| now >= *until)
            })
            .count();
        (inner.tracked.len(), unresolved)
    }

    pub(crate) fn funding(&self, now: Timestamp) -> RuntimeFundingReport {
        let inner = self.inner.lock().expect("slot registry poisoned");
        let mut report = RuntimeFundingReport::default();
        #[allow(
            clippy::disallowed_methods,
            reason = "sums overage across slots with checked addition; the total does not depend on the order the addends arrive in"
        )]
        for slot in inner.slots.values() {
            report.total_overage_spent = report
                .total_overage_spent
                .checked_add(u128::from(slot.overage().spent().get()))
                .expect("at most usize::MAX u64 contributions fit u128");
            if let Some(lease) = slot.load_observed() {
                let sum = report
                    .total_lease_remaining
                    .unwrap_or(0)
                    .checked_add(u128::from(lease.remaining().get()))
                    .expect("at most usize::MAX u64 contributions fit u128");
                report.total_lease_remaining = Some(sum);
            }
        }
        #[allow(
            clippy::disallowed_methods,
            reason = "reduces each account's members to `any(..)` and `max(..)`; neither depends on the order they are visited in"
        )]
        for (account, members) in &inner.members {
            if members
                .values()
                .any(|s| s.status == AccountStatus::Active && now < s.valid_until)
                && let Some(lease) = inner.slots[account].load_observed()
            {
                let until = lease.usable_until();
                report.earliest_lease_usable_until = Some(
                    report
                        .earliest_lease_usable_until
                        .map_or(until, |old| old.min(until)),
                );
            }
            let cap = members
                .values()
                .filter(|s| s.status == AccountStatus::Active && now < s.valid_until)
                .filter_map(|s| s.enforcement_mode.overage_cap())
                .max();
            if let Some(cap) = cap {
                let sum = report
                    .total_overage_cap
                    .unwrap_or(0)
                    .checked_add(u128::from(cap.get()))
                    .expect("at most usize::MAX u64 contributions fit u128");
                report.total_overage_cap = Some(sum);
            }
        }
        report
    }
    /// An empty registry with the unsharded layout.
    #[must_use]
    pub fn new() -> Arc<Self> {
        Arc::new(Self::default())
    }

    /// An empty registry whose slots use `sharding`. It must match the
    /// snapshot map's layout, or [`SnapshotManager::spawn`](crate::SnapshotManager::spawn)
    /// refuses the pair.
    #[must_use]
    pub fn with_sharding(sharding: LocalSharding) -> Arc<Self> {
        Arc::new(Self {
            sharding,
            ..Self::default()
        })
    }

    pub(crate) fn observed(sharding: LocalSharding) -> (Arc<Self>, watch::Receiver<()>) {
        let (changed, receiver) = watch::channel(());
        (
            Arc::new(Self {
                sharding,
                changed: Some(changed),
                ..Self::default()
            }),
            receiver,
        )
    }

    /// Create or retrieve an account's stable slot. This is a control-plane
    /// primitive; it creates no membership and never starts a manager.
    #[must_use]
    pub fn slot(&self, account: AccountId) -> Arc<LeaseSlot> {
        Arc::clone(
            self.inner
                .lock()
                .expect("slot registry poisoned")
                .slots
                .entry(account)
                .or_insert_with(|| LeaseSlot::with_sharding(account, self.sharding)),
        )
    }

    /// The shard layout every slot in this registry is created with.
    #[must_use]
    pub fn sharding(&self) -> LocalSharding {
        self.sharding
    }

    pub(crate) fn observes(&self) -> bool {
        self.changed.is_some()
    }

    /// Publish the actual map result, never an offered snapshot that the map
    /// may have rejected. The snapshot manager serializes publication and this
    /// observation; the runtime does not expose its map's write surface.
    pub(crate) fn observe_many(
        &self,
        entries: impl IntoIterator<Item = (Principal, Option<MapEntry>)>,
    ) {
        let mut inner = self.inner.lock().expect("slot registry poisoned");
        for (principal, entry) in entries {
            let until = match &entry {
                Some(MapEntry::Present(state)) => Some(state.snapshot.valid_until),
                Some(MapEntry::NegativeUntil { until }) => Some(*until),
                None => None,
            };
            if let Some(until) = until {
                inner.resolutions.insert(principal, until);
            } else {
                inner.resolutions.remove(&principal);
            }
            if let Some(previous) = inner.principals.remove(&principal) {
                inner.deadlines.remove(&(previous.valid_until, principal));
                let account = previous.account_id;
                if let Some(members) = inner.members.get_mut(&account) {
                    members.remove(&principal);
                    if members.is_empty() {
                        inner.members.remove(&account);
                    }
                }
                inner.dirty.insert(account);
            }
            if let Some(MapEntry::Present(state)) = entry {
                let snapshot = Arc::clone(&state.snapshot);
                let account = snapshot.account_id;
                inner.deadlines.insert((snapshot.valid_until, principal));
                inner.principals.insert(principal, Arc::clone(&snapshot));
                inner
                    .members
                    .entry(account)
                    .or_default()
                    .insert(principal, snapshot);
                inner.dirty.insert(account);
            }
        }
        drop(inner);
        self.wake();
    }

    fn wake(&self) {
        if let Some(changed) = &self.changed {
            changed.send_replace(());
        }
    }

    pub(crate) fn drain_changes(&self, now: Timestamp) -> Vec<AccountBinding> {
        let mut inner = self.inner.lock().expect("slot registry poisoned");
        while let Some(&(at, principal)) = inner.deadlines.first() {
            if at > now {
                break;
            }
            inner.deadlines.pop_first();
            if let Some(snapshot) = inner.principals.get(&principal) {
                let account = snapshot.account_id;
                inner.dirty.insert(account);
            }
        }
        let dirty = std::mem::take(&mut inner.dirty);
        dirty
            .into_iter()
            .map(|account| inner.binding(account))
            .collect()
    }

    pub(crate) fn next_expiry(&self) -> Option<Timestamp> {
        self.inner
            .lock()
            .expect("slot registry poisoned")
            .deadlines
            .first()
            .map(|(at, _)| *at)
    }

    /// Both callers are order-independent: `readiness` counts eligible and
    /// unfundable accounts, and `account_reports` re-keys into a `BTreeMap`.
    #[allow(
        clippy::disallowed_methods,
        reason = "callers count or re-key into a BTreeMap; the key order never reaches an output"
    )]
    pub(crate) fn bindings(&self) -> Vec<AccountBinding> {
        let inner = self.inner.lock().expect("slot registry poisoned");
        inner
            .members
            .keys()
            .map(|&account| inner.binding(account))
            .collect()
    }

    /// Every retained account's slot, copied out so a caller can walk them
    /// without holding the registry.
    ///
    /// Unordered: the only consumer sorts by a total order before anything
    /// reaches an output.
    #[allow(
        clippy::disallowed_methods,
        reason = "the contention report sorts by (count, account) before any output"
    )]
    pub(crate) fn slots(&self) -> Vec<(AccountId, Arc<LeaseSlot>)> {
        self.inner
            .lock()
            .expect("slot registry poisoned")
            .slots
            .iter()
            .map(|(&account, slot)| (account, Arc::clone(slot)))
            .collect()
    }

    pub(crate) fn retained_slots(&self) -> usize {
        self.inner
            .lock()
            .expect("slot registry poisoned")
            .slots
            .len()
    }
}

impl Registry {
    /// The snapshot list is only ever reduced with `any(..)`, by
    /// `AccountBinding::eligible` and `fundable`, so its order is not an output.
    #[allow(
        clippy::disallowed_methods,
        reason = "the snapshots are only reduced with any(..); their order never reaches an output"
    )]
    fn binding(&self, account: AccountId) -> AccountBinding {
        AccountBinding {
            account,
            slot: Arc::clone(
                self.slots
                    .get(&account)
                    .expect("published membership has a slot"),
            ),
            snapshots: self
                .members
                .get(&account)
                .map(|members| members.values().cloned().collect())
                .unwrap_or_default(),
        }
    }
}

fn quota_usable(slot: &LeaseSlot, mode: EnforcementMode, now: Timestamp) -> bool {
    let lease_usable = slot
        .load_observed()
        .is_some_and(|lease| now < lease.usable_until() && !lease.remaining().is_zero());
    lease_usable
        || mode
            .overage_cap()
            .is_some_and(|cap| !slot.overage().headroom(cap).is_zero())
}

#[cfg(test)]
mod tests {
    #![allow(
        clippy::disallowed_methods,
        reason = "unit tests that build an arbitrary `now` the assertions are relative to; \
                  no assertion here depends on what the clock actually said"
    )]
    use super::*;
    use jiff::SignedDuration;
    use tollgate_core::{
        CommitFunding, CostUnits, EnforcementMode, FencingToken, LeaseGrant, LeaseId, LocalLease,
        Reservation,
    };
    fn lease_expiring_at(expires_at: Timestamp, units: u64) -> Arc<LocalLease> {
        Arc::new(LocalLease::new(
            LeaseGrant {
                lease_id: LeaseId(1),
                account_id: AccountId(1),
                fencing_token: FencingToken(1),
                units: CostUnits(units),
                expires_at,
            },
            CostUnits(0),
        ))
    }

    #[test]
    fn configured_sharding_is_preserved_by_stable_account_slots() {
        let sharding = LocalSharding::new(std::num::NonZeroUsize::new(8).unwrap());
        let registry = SlotRegistry::with_sharding(sharding);
        let slot = registry.slot(AccountId(1));
        assert!(!registry.observes());
        assert_eq!(registry.retained_slots(), 1);
        assert_eq!(registry.sharding(), sharding);
        assert_eq!(slot.sharding(), sharding);
        assert!(Arc::ptr_eq(&slot, &registry.slot(AccountId(1))));
        assert!(!Arc::ptr_eq(&slot, &registry.slot(AccountId(2))));
        assert_eq!(registry.retained_slots(), 2);
    }

    #[tokio::test]
    async fn membership_wakes_once_per_publication_and_expires_at_its_exact_deadline() {
        use tollgate_admission::{ArcSwapSnapshotMap, SnapshotMap};
        use tollgate_core::{CostTable, Generation, PermissionBits, ResolvedLimits};
        let (registry, mut changed) = SlotRegistry::observed(LocalSharding::SINGLE);
        let map = ArcSwapSnapshotMap::default();
        let now = Timestamp::from_second(100).unwrap();
        let until = Timestamp::from_second(101).unwrap();
        let principal = Principal(11);
        let snapshot = Arc::new(
            AccountSnapshot::builder(
                AccountId(1),
                Generation(1),
                AccountStatus::Active,
                until,
                PermissionBits::bit(0),
                ResolvedLimits::new(100),
                Arc::new(CostTable::builder(CostUnits(0), CostUnits(0)).build()),
            )
            .build(),
        );
        map.install(principal, snapshot, registry.slot(AccountId(1)))
            .unwrap();
        registry.observe_many([(principal, map.get(&principal))]);
        assert!(changed.has_changed().unwrap());
        changed.borrow_and_update();
        assert_eq!(registry.drain_changes(now).len(), 1);
        assert!(registry.drain_changes(now).is_empty());
        assert_eq!(registry.next_expiry(), Some(until));
        let expired = registry.drain_changes(until);
        assert_eq!(expired.len(), 1);
        assert!(!expired[0].eligible(until));
        assert_eq!(registry.next_expiry(), None);
        assert!(registry.drain_changes(until).is_empty());
        registry.observe_many([(principal, None)]);
        assert!(changed.has_changed().unwrap());
        assert!(registry.bindings().is_empty());
        assert_eq!(registry.retained_slots(), 1);
    }

    #[test]
    fn readiness_closes_the_lease_window_exactly_when_debits_do() {
        let now = Timestamp::now();
        let slot = LeaseSlot::for_account(AccountId(1));
        assert!(
            !quota_usable(&slot, EnforcementMode::Strict, now),
            "an empty slot funds nothing"
        );

        let lease = lease_expiring_at(now, 100);
        drop(slot.replace(Arc::clone(&lease)));
        assert!(
            lease.try_debit(CostUnits(1), now).is_err(),
            "the request path denies at the boundary",
        );
        assert!(
            !quota_usable(&slot, EnforcementMode::Strict, now),
            "so readiness must not still be advertising at it",
        );

        drop(slot.replace(lease_expiring_at(
            now.checked_add(SignedDuration::from_secs(60)).unwrap(),
            100,
        )));
        assert!(quota_usable(&slot, EnforcementMode::Strict, now));

        drop(slot.replace(lease_expiring_at(
            now.checked_add(SignedDuration::from_secs(60)).unwrap(),
            0,
        )));
        assert!(
            !quota_usable(&slot, EnforcementMode::Strict, now),
            "a live lease with nothing left funds nothing either",
        );
    }

    /// The mode's whole purpose, stated as a readiness property: an elastic
    /// account with headroom keeps its instance in rotation on exactly the
    /// states a strict one is withdrawn for, and leaves rotation when the
    /// headroom is gone (INVARIANTS.md GL-10).
    #[test]
    fn readiness_counts_overage_headroom_for_an_elastic_account() {
        let now = Timestamp::now();
        let elastic = EnforcementMode::Elastic {
            overage_cap: CostUnits(100),
        };
        let slot = LeaseSlot::for_account(AccountId(1));

        // No lease at all, and a live lease with nothing left: both deny under
        // `Strict`, and both are exactly what elastic mode serves through.
        assert!(!quota_usable(&slot, EnforcementMode::Strict, now));
        assert!(quota_usable(&slot, elastic, now));

        drop(slot.replace(lease_expiring_at(
            now.checked_add(SignedDuration::from_secs(60)).unwrap(),
            0,
        )));
        assert!(!quota_usable(&slot, EnforcementMode::Strict, now));
        assert!(quota_usable(&slot, elastic, now));

        // Spending the cap withdraws the instance, because at that point it
        // really cannot admit anything.
        let overage =
            Reservation::reserve_overage(slot.overage(), CostUnits(100), CostUnits(100)).unwrap();
        overage
            .commit_at_execution_start(now, CommitFunding::LeaseOnly)
            .unwrap();
        assert!(
            !quota_usable(&slot, elastic, now),
            "a spent cap is not admissible, and readiness must say so"
        );

        // A cap raised by a republish restores readiness with no other change.
        assert!(quota_usable(
            &slot,
            EnforcementMode::Elastic {
                overage_cap: CostUnits(200)
            },
            now
        ));
    }
}