fair_machines 0.1.5 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,49 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.7.0](https://github.com/seuros/fair_machines/compare/fair-machines-v0.6.0...fair-machines-v0.7.0) (2026-11-16)
4
+
5
+
6
+ ### ⚠ BREAKING CHANGES
7
+
8
+ * seal TenantFair and KeyedFair against subclassing
9
+
10
+ ### Features
11
+
12
+ * seal TenantFair and KeyedFair against subclassing ([74f1b9c](https://github.com/seuros/fair_machines/commit/74f1b9c6f3d283689441bfa7a8e469da16695f4f))
13
+
14
+ ## [0.6.0](https://github.com/seuros/fair_machines/compare/fair-machines-v0.5.0...fair-machines-v0.6.0) (2026-11-14)
15
+
16
+
17
+ ### ⚠ BREAKING CHANGES
18
+
19
+ * build the binding on matryona-machines 0.2; compiler-checked refusal symbols; read Lane fields from the struct; ship no test files
20
+
21
+ ### Features
22
+
23
+ * build the binding on matryona-machines 0.2; compiler-checked refusal symbols; read Lane fields from the struct; ship no test files ([9e05fa0](https://github.com/seuros/fair_machines/commit/9e05fa06ec652a0693d0d83f4fd5c5e63fc96f31))
24
+
25
+ ## [0.5.0](https://github.com/seuros/fair_machines/compare/fair-machines-v0.4.0...fair-machines-v0.5.0) (2026-11-14)
26
+
27
+
28
+ ### ⚠ BREAKING CHANGES
29
+
30
+ * validate Lane through the core, seal it, exact-class reads, clone-then-keep admit
31
+
32
+ ### Features
33
+
34
+ * validate Lane through the core, seal it, exact-class reads, clone-then-keep admit ([2f819ae](https://github.com/seuros/fair_machines/commit/2f819aeed1bfc76251be703b14e8dafd6d17ee45))
35
+
36
+ ## [0.4.0](https://github.com/seuros/fair_machines/compare/fair-machines-v0.3.0...fair-machines-v0.4.0) (2026-11-13)
37
+
38
+
39
+ ### ⚠ BREAKING CHANGES
40
+
41
+ * converted to the fleet conventions. One exhaustive Refusal per crate, Result from every operation that can refuse, a refusal changes nothing, provable idempotent repeats are honoured, Ruby mirrors the core. No compatibility aliases. Requires flow-machines >= 0.3 and priority-machines >= 0.4.
42
+
43
+ ### Features
44
+
45
+ * answer Result<_, Refusal>, refuse unadmitted heads in select, mirror the core positionally in Ruby ([5e73ef6](https://github.com/seuros/fair_machines/commit/5e73ef67f86a0326709e27eca46d8f97bccd8aba))
46
+
3
47
  ## [0.3.0](https://github.com/seuros/fair_machines/compare/fair-machines-v0.2.2...fair-machines-v0.3.0) (2026-11-07)
4
48
 
5
49
 
@@ -1,16 +1,19 @@
1
1
  [package]
2
2
  name = "fair-machines"
3
- version = "0.3.0"
3
+ version = "0.7.0"
4
4
  edition.workspace = true
5
5
  rust-version.workspace = true
6
6
  license.workspace = true
7
7
  description = "Tenant-bounded admission composed with deficit round robin"
8
8
  readme = "README.md"
9
- exclude = ["src/**/tests.rs", "tests/"]
9
+ exclude = ["src/**/tests.rs", "src/tests/", "src/test_support.rs", "tests/"]
10
10
  keywords = ["fair-queuing", "multi-tenant", "drr", "scheduling", "no-std"]
11
11
  categories = ["no-std", "algorithms"]
12
12
  [dependencies]
13
13
  priority-machines.workspace = true
14
14
  flow-machines.workspace = true
15
+
16
+ [dev-dependencies]
17
+ matryona-machines = { workspace = true, features = ["testing"] }
15
18
  [lints]
16
19
  workspace = true
@@ -19,3 +19,64 @@ its budget.
19
19
 
20
20
  Tenant hierarchies, per-flow WFQ, runtime preemption, and strict latency/starvation
21
21
  bounds are not implemented.
22
+
23
+ ## Contract
24
+
25
+ - **Answers.** Every operation that can refuse returns `Result<T, Refusal>`,
26
+ and `T` describes only honoured outcomes.
27
+
28
+ | Call | Honoured | Refused |
29
+ | --- | --- | --- |
30
+ | `TenantFair::admit(tenant, cost)` | `()` | `UnknownTenant`, `Insufficient` |
31
+ | `TenantFair::cancel(tenant, cost)` | `()` | `UnknownTenant`, `ExceedsInFlight` |
32
+ | `TenantFair::select(heads)` | `Some(tenant)`, or `None` exactly when every head is `None` | `ExceedsInFlight` |
33
+ | `KeyedFair::admit(hash, cost)` | the `Lane` charged | `Insufficient` |
34
+ | `KeyedFair::release(lane, cost)` | `()` | `UnknownLane`, `ExceedsInFlight` |
35
+ | `in_flight`, `available` | `Some(cost)`, `None` for a tenant or lane the instance does not have | queries, never refuse |
36
+ | `KeyedFair::lane(hash)`, `lane_at(index)`, `Lane::new(index)` | a `Lane`; `lane_at` is `None` past the last lane | pure functions |
37
+
38
+ - **Refusals change nothing.** Every `Err` leaves the value equal to a clone
39
+ taken before the call: no budget, no DRR credit, no cursor.
40
+ - **`Refusal`.** `UnknownTenant`: the index is `N` or more. `UnknownLane`: the
41
+ lane was built for a `KeyedFair` with more lanes. `Insufficient`: the
42
+ admission asks for more cost than the budget has available; there is no
43
+ partial admission. `ExceedsInFlight`: more cost is being returned than the
44
+ budget has admitted; there is no partial return.
45
+ - **`select` and an unadmitted head.** `select` is a drain: `Ok(None)` means
46
+ every head was `None`, and nothing else. A head that costs more than its
47
+ tenant has admitted is not "nothing to dispatch": selecting it would return
48
+ cost that was never admitted, the same fault as an over-sized `cancel`. It
49
+ is refused as `ExceedsInFlight` and the whole call changes nothing, rather
50
+ than treating that tenant as empty, which would silently drop its saved
51
+ credit and hide the caller's accounting error. An honoured `select` records
52
+ what the round observed, as DRR does: a tenant presented as `None` loses the
53
+ credit it had saved.
54
+ - **Repeats cannot be honoured as repeats.** Budgets count cost, not jobs, so
55
+ the core cannot tell a repeated `cancel` or `release` from a second job of
56
+ the same cost. Either is honoured while that much cost is in flight and
57
+ refused as `ExceedsInFlight` once it is not. Return every admitted cost
58
+ exactly once.
59
+ - **`Lane` is not an issued id.** It is the bucket a hash maps to, a pure
60
+ function of the hash and `LANES`. Nothing is counted or issued: `admit`
61
+ answers it only to save the caller a second hash, `lane`, `lane_at` and
62
+ `Lane::new` build one without admitting anything, and the same hash names the same lane
63
+ in every instance with as many lanes. That aliasing is the design, so a lane
64
+ carries no scope, proves no admission, and there is no sequence to resume. A
65
+ lane built for more lanes than an instance has is `UnknownLane`.
66
+ - **No issued ids, no restarts to manage.** Neither type hands out a handle.
67
+ A rebuilt instance starts with empty budgets; cost admitted before the
68
+ restart is the caller's to re-admit or forget.
69
+ - **No clock.** Nothing here takes time, so no answer records one.
70
+ - **No panics.** The one `assert!` is evaluated at compile time:
71
+ `KeyedFair<0>` does not build, so no runtime path can meet a lane-less
72
+ instance. There is no `debug_assert!` arm: `select` settles every head
73
+ against its tenant's budget before the scheduler runs (an unadmitted head
74
+ refuses the whole call there), and presents each head to the DRR with
75
+ whether it drains its tenant, so the DRR deactivates a drained tenant
76
+ inside the selection and nothing after its answer can be refused. `cancel`
77
+ checks the tenant against the budgets; the scheduler's unknown-lane refusal
78
+ for the same index is carried as `UnknownTenant`, and both commit last.
79
+ - **Sibling seams.** The `flow-machines` credit windows and the
80
+ `priority-machines` DRR are private: no sibling type appears in this API,
81
+ so none is re-exported. Their refusals are mapped onto `Refusal` variant by
82
+ variant.
@@ -2,11 +2,30 @@ use core::num::NonZeroU32;
2
2
 
3
3
  use flow_machines::CreditWindow;
4
4
 
5
- /// Lane a hashed key was admitted to; return its cost with [`KeyedFair::release`].
5
+ use crate::Refusal;
6
+
7
+ /// The lane a hash maps to: a bucket index, not an issued id.
8
+ ///
9
+ /// [`KeyedFair::admit`] answers the lane it charged so the caller can return
10
+ /// the cost with [`KeyedFair::release`] without hashing again. A lane is a
11
+ /// pure function of the hash and `LANES`: nothing is counted or issued, the
12
+ /// same hash names the same lane in every instance with as many lanes, and
13
+ /// [`KeyedFair::lane`], [`KeyedFair::lane_at`] or [`Lane::new`] build one
14
+ /// without admitting anything. It therefore carries no scope and proves
15
+ /// nothing: holding a lane does not show that cost was admitted to it.
6
16
  #[derive(Debug, Clone, Copy, PartialEq, Eq)]
7
17
  pub struct Lane(usize);
8
18
 
9
19
  impl Lane {
20
+ /// The lane at position `index`, for no instance in particular. Every
21
+ /// position is a lane of some `KeyedFair`; one with `index` lanes or
22
+ /// fewer answers [`Refusal::UnknownLane`] or `None` for it.
23
+ #[must_use]
24
+ pub const fn new(index: usize) -> Self {
25
+ Self(index)
26
+ }
27
+
28
+ /// Position of the lane, in `0..LANES` of the instance it was built for.
10
29
  #[must_use]
11
30
  pub const fn index(self) -> usize {
12
31
  self.0
@@ -16,7 +35,7 @@ impl Lane {
16
35
  /// Independent cost budgets for an open set of tenants, hashed onto `LANES`
17
36
  /// lanes. Tenants sharing a lane share its budget; none can borrow another
18
37
  /// lane's.
19
- #[derive(Debug)]
38
+ #[derive(Debug, Clone, PartialEq, Eq)]
20
39
  pub struct KeyedFair<const LANES: usize> {
21
40
  lanes: [CreditWindow; LANES],
22
41
  }
@@ -24,6 +43,8 @@ pub struct KeyedFair<const LANES: usize> {
24
43
  impl<const LANES: usize> KeyedFair<LANES> {
25
44
  const NONEMPTY: () = assert!(LANES > 0, "KeyedFair needs at least one lane");
26
45
 
46
+ /// Start with every lane empty and the same positive `capacity` each.
47
+ /// `LANES` must be positive: zero fails to compile.
27
48
  #[must_use]
28
49
  pub fn new(capacity: NonZeroU32) -> Self {
29
50
  let () = Self::NONEMPTY;
@@ -32,12 +53,17 @@ impl<const LANES: usize> KeyedFair<LANES> {
32
53
  }
33
54
  }
34
55
 
56
+ /// The lane `hash` maps to: the hash space cut into `LANES` equal,
57
+ /// ordered ranges. A pure function; no instance is involved.
35
58
  #[must_use]
36
- #[allow(clippy::cast_possible_truncation)]
59
+ #[allow(clippy::cast_possible_truncation, clippy::arithmetic_side_effects)]
37
60
  pub const fn lane(hash: u64) -> Lane {
61
+ // Both factors are below 2^64, so the product is below 2^128 and
62
+ // cannot overflow; shifted down it is below `LANES`, so it fits.
38
63
  Lane(((hash as u128 * LANES as u128) >> 64) as usize)
39
64
  }
40
65
 
66
+ /// The lane at `index`; `None` for an index of `LANES` or more.
41
67
  #[must_use]
42
68
  pub const fn lane_at(index: usize) -> Option<Lane> {
43
69
  if index < LANES {
@@ -47,23 +73,62 @@ impl<const LANES: usize> KeyedFair<LANES> {
47
73
  }
48
74
  }
49
75
 
50
- #[must_use]
51
- pub fn admit(&mut self, hash: u64, cost: NonZeroU32) -> Option<Lane> {
76
+ /// Admit `cost` to the lane `hash` maps to and answer that lane.
77
+ ///
78
+ /// # Errors
79
+ ///
80
+ /// [`Refusal::Insufficient`] when `cost` exceeds the lane's available
81
+ /// budget: there is no partial admission and nothing changed.
82
+ pub const fn admit(&mut self, hash: u64, cost: NonZeroU32) -> Result<Lane, Refusal> {
52
83
  let lane = Self::lane(hash);
53
- self.lanes[lane.0].try_acquire(cost).then_some(lane)
84
+ // In bounds by construction: `lane` answers below `LANES` for every
85
+ // hash. Kept as an index because `admit` is `const` and slice `get`
86
+ // is not.
87
+ #[allow(clippy::indexing_slicing)]
88
+ let window = &mut self.lanes[lane.0];
89
+ match window.try_acquire(cost) {
90
+ Ok(()) => Ok(lane),
91
+ Err(refusal) => Err(Refusal::from_flow(refusal)),
92
+ }
54
93
  }
55
94
 
56
- /// Over-release leaves state unchanged and returns `false`.
57
- #[must_use]
58
- pub fn release(&mut self, lane: Lane, cost: NonZeroU32) -> bool {
95
+ /// Return `cost` to `lane`.
96
+ ///
97
+ /// Lanes count cost, not work, so a repeated `release` cannot be
98
+ /// recognised as a repeat. It is honoured while the lane has that much
99
+ /// cost in flight and refused once it does not.
100
+ ///
101
+ /// # Errors
102
+ ///
103
+ /// [`Refusal::UnknownLane`] for a lane this instance does not have,
104
+ /// [`Refusal::ExceedsInFlight`] when `cost` exceeds what the lane has in
105
+ /// flight: there is no partial return. Nothing changed.
106
+ pub fn release(&mut self, lane: Lane, cost: NonZeroU32) -> Result<(), Refusal> {
59
107
  self.lanes
60
108
  .get_mut(lane.0)
61
- .is_some_and(|budget| budget.release(cost))
109
+ .ok_or(Refusal::UnknownLane)?
110
+ .release(cost)
111
+ .map_err(Refusal::from_flow)
112
+ }
113
+
114
+ /// Number of lanes, `LANES`.
115
+ #[must_use]
116
+ pub const fn lane_count(&self) -> usize {
117
+ LANES
118
+ }
119
+
120
+ /// Cost admitted to `lane` and not yet released; `None` for a lane this
121
+ /// instance does not have.
122
+ #[must_use]
123
+ pub fn in_flight(&self, lane: Lane) -> Option<u32> {
124
+ self.lanes.get(lane.0).map(CreditWindow::in_flight)
62
125
  }
63
126
 
127
+ /// Cost `lane` may still admit; `None` for a lane this instance does not
128
+ /// have.
64
129
  #[must_use]
65
- pub fn in_flight(&self, lane: Lane) -> u32 {
66
- self.lanes.get(lane.0).map_or(0, CreditWindow::in_flight)
130
+ pub fn available(&self, lane: Lane) -> Option<u32> {
131
+ self.lanes.get(lane.0).map(CreditWindow::available)
67
132
  }
68
133
  }
69
134
 
@@ -1,15 +1,20 @@
1
1
  #![no_std]
2
2
  #![doc = include_str!("../README.md")]
3
+ #![cfg_attr(not(test), deny(clippy::indexing_slicing))]
3
4
  use core::num::NonZeroU32;
4
5
  use flow_machines::CreditWindow;
5
- use priority_machines::DeficitRoundRobin;
6
+ use priority_machines::{Deactivation, DeficitRoundRobin, Head};
6
7
 
7
8
  mod keyed;
9
+ mod refusal;
10
+ #[cfg(test)]
11
+ mod test_support;
8
12
 
9
13
  pub use keyed::{KeyedFair, Lane};
14
+ pub use refusal::Refusal;
10
15
 
11
16
  /// Fixed tenants with independent cost budgets and DRR scheduling.
12
- #[derive(Debug)]
17
+ #[derive(Debug, Clone, PartialEq, Eq)]
13
18
  pub struct TenantFair<const N: usize> {
14
19
  budgets: [CreditWindow; N],
15
20
  scheduler: DeficitRoundRobin<N>,
@@ -20,55 +25,132 @@ impl<const N: usize> TenantFair<N> {
20
25
  #[must_use]
21
26
  pub fn new(capacities: [NonZeroU32; N], quanta: [NonZeroU32; N]) -> Self {
22
27
  Self {
23
- budgets: core::array::from_fn(|i| CreditWindow::new(capacities[i])),
28
+ budgets: capacities.map(CreditWindow::new),
24
29
  scheduler: DeficitRoundRobin::new(quanta),
25
30
  }
26
31
  }
27
32
 
28
33
  /// Admit cost without borrowing another tenant's capacity.
29
- #[must_use]
30
- pub fn admit(&mut self, tenant: usize, cost: NonZeroU32) -> bool {
34
+ ///
35
+ /// # Errors
36
+ ///
37
+ /// [`Refusal::UnknownTenant`] for an index of `N` or more,
38
+ /// [`Refusal::Insufficient`] when `cost` exceeds the tenant's available
39
+ /// budget: there is no partial admission. Nothing changed.
40
+ pub fn admit(&mut self, tenant: usize, cost: NonZeroU32) -> Result<(), Refusal> {
31
41
  self.budgets
32
42
  .get_mut(tenant)
33
- .is_some_and(|budget| budget.try_acquire(cost))
43
+ .ok_or(Refusal::UnknownTenant)?
44
+ .try_acquire(cost)
45
+ .map_err(Refusal::from_flow)
34
46
  }
35
47
 
36
- /// Dispatch one admitted head and return its queued-cost credit.
48
+ /// Dispatch one admitted head and return its cost to the tenant's budget.
37
49
  ///
38
- /// # Panics
39
- /// Panics if an internal scheduling/accounting invariant is violated.
40
- pub fn select(&mut self, heads: &[Option<NonZeroU32>; N]) -> Option<usize> {
41
- let eligible = core::array::from_fn(|i| {
42
- heads[i].filter(|cost| cost.get() <= self.budgets[i].in_flight())
43
- });
44
- let tenant = self.scheduler.select(&eligible)?;
45
- if let Some(cost) = eligible[tenant] {
46
- assert!(self.release(tenant, cost));
50
+ /// `heads[i]` is the cost at the head of tenant `i`'s queue, `None` when
51
+ /// it has nothing waiting. A drain: `Ok(None)` is the honoured answer
52
+ /// "every head is `None`", returned exactly then. Like every selection it
53
+ /// records what the round observed: a tenant presented as `None` loses
54
+ /// the credit it had saved.
55
+ ///
56
+ /// # Errors
57
+ ///
58
+ /// [`Refusal::ExceedsInFlight`] when any head costs more than its tenant
59
+ /// has admitted. Such a head was never admitted, so it cannot be
60
+ /// dispatched, and the call is refused whole rather than quietly treating
61
+ /// that tenant as empty. Nothing changed: no credit was paid, no head
62
+ /// charged, no budget touched.
63
+ pub fn select(&mut self, heads: &[Option<NonZeroU32>; N]) -> Result<Option<usize>, Refusal> {
64
+ // Every head is settled up front: each tenant's budget as it would
65
+ // stand once its head is dispatched. A head that was never admitted
66
+ // refuses the whole call here, before the scheduler runs.
67
+ let mut settled = self.budgets.clone();
68
+ let mut presented = [None; N];
69
+ for ((budget, head), presented) in settled.iter_mut().zip(heads).zip(&mut presented) {
70
+ if let Some(cost) = *head {
71
+ budget.release(cost).map_err(Refusal::from_flow)?;
72
+ *presented = Some(Tail {
73
+ cost,
74
+ last: budget.in_flight() == 0,
75
+ });
76
+ }
77
+ }
78
+ // A head that drains its tenant deactivates it inside the selection,
79
+ // so nothing after the scheduler's answer can be refused.
80
+ let Some(tenant) = self.scheduler.select(&presented) else {
81
+ return Ok(None);
82
+ };
83
+ for (index, (budget, settled)) in self.budgets.iter_mut().zip(settled).enumerate() {
84
+ if index == tenant {
85
+ *budget = settled;
86
+ }
47
87
  }
48
- Some(tenant)
88
+ Ok(Some(tenant))
49
89
  }
50
90
 
51
- /// Cancel queued cost; invalid/over-release requests leave state unchanged.
91
+ /// Cancel queued cost: work that left the queue without being selected.
92
+ ///
93
+ /// Budgets count cost, not jobs, so a repeated `cancel` cannot be
94
+ /// recognised as a repeat. It is honoured while the tenant has that much
95
+ /// cost admitted and refused once it does not.
52
96
  ///
53
- /// # Panics
54
- /// Panics if an internal lane-index invariant is violated.
97
+ /// # Errors
98
+ ///
99
+ /// [`Refusal::UnknownTenant`] for an index of `N` or more,
100
+ /// [`Refusal::ExceedsInFlight`] when `cost` exceeds what the tenant has
101
+ /// admitted: there is no partial return. Nothing changed.
102
+ pub fn cancel(&mut self, tenant: usize, cost: NonZeroU32) -> Result<(), Refusal> {
103
+ let budget = self.budgets.get_mut(tenant).ok_or(Refusal::UnknownTenant)?;
104
+ let mut returned = budget.clone();
105
+ returned.release(cost).map_err(Refusal::from_flow)?;
106
+ if returned.in_flight() == 0 {
107
+ // A drained tenant leaves the rotation. The scheduler has the
108
+ // same N lanes as the budgets, and its refusal for an unknown
109
+ // lane is this crate's `UnknownTenant`: carried, not asserted.
110
+ match self.scheduler.deactivate(tenant) {
111
+ Ok(Deactivation::Deactivated | Deactivation::Idle) => {}
112
+ Err(refusal) => return Err(Refusal::from_priority(refusal)),
113
+ }
114
+ }
115
+ *budget = returned;
116
+ Ok(())
117
+ }
118
+
119
+ /// Number of tenants, `N`.
55
120
  #[must_use]
56
- pub fn cancel(&mut self, tenant: usize, cost: NonZeroU32) -> bool {
57
- self.release(tenant, cost)
121
+ pub const fn tenant_count(&self) -> usize {
122
+ N
58
123
  }
59
124
 
60
- /// Return queued cost; a drained tenant leaves the DRR rotation.
61
- fn release(&mut self, tenant: usize, cost: NonZeroU32) -> bool {
62
- let Some(budget) = self.budgets.get_mut(tenant) else {
63
- return false;
64
- };
65
- if !budget.release(cost) {
66
- return false;
67
- }
68
- if budget.in_flight() == 0 {
69
- assert!(self.scheduler.deactivate(tenant));
70
- }
71
- true
125
+ /// Cost the tenant has admitted and not yet dispatched or cancelled;
126
+ /// `None` for an index of `N` or more.
127
+ #[must_use]
128
+ pub fn in_flight(&self, tenant: usize) -> Option<u32> {
129
+ self.budgets.get(tenant).map(CreditWindow::in_flight)
130
+ }
131
+
132
+ /// Cost the tenant may still admit; `None` for an index of `N` or more.
133
+ #[must_use]
134
+ pub fn available(&self, tenant: usize) -> Option<u32> {
135
+ self.budgets.get(tenant).map(CreditWindow::available)
136
+ }
137
+ }
138
+
139
+ /// A tenant's head as the scheduler reads it: its cost, and whether
140
+ /// dispatching it drains the tenant's budget.
141
+ #[derive(Debug, Clone, Copy)]
142
+ struct Tail {
143
+ cost: NonZeroU32,
144
+ last: bool,
145
+ }
146
+
147
+ impl Head for Tail {
148
+ fn cost(self) -> NonZeroU32 {
149
+ self.cost
150
+ }
151
+
152
+ fn last(self) -> bool {
153
+ self.last
72
154
  }
73
155
  }
74
156
 
@@ -0,0 +1,47 @@
1
+ use core::fmt;
2
+
3
+ /// Why a call was not honoured. A refused call changes nothing.
4
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
5
+ pub enum Refusal {
6
+ /// The tenant index is not one of the scheduler's tenants.
7
+ UnknownTenant,
8
+ /// The lane is not one of this instance's lanes: it was built for a
9
+ /// `KeyedFair` with more lanes.
10
+ UnknownLane,
11
+ /// The admission asks for more cost than the budget has available.
12
+ Insufficient,
13
+ /// More cost is being returned than the budget accounts as admitted: an
14
+ /// over-sized `cancel` or `release`, or a head presented to `select` that
15
+ /// costs more than its tenant has admitted.
16
+ ExceedsInFlight,
17
+ }
18
+
19
+ impl Refusal {
20
+ /// A budget's refusal, under the name it has here.
21
+ pub(crate) const fn from_flow(refusal: flow_machines::Refusal) -> Self {
22
+ match refusal {
23
+ flow_machines::Refusal::Insufficient { .. } => Self::Insufficient,
24
+ flow_machines::Refusal::ExceedsInFlight { .. } => Self::ExceedsInFlight,
25
+ }
26
+ }
27
+
28
+ /// The scheduler's refusal: its lanes are the tenants.
29
+ pub(crate) const fn from_priority(refusal: priority_machines::Refusal) -> Self {
30
+ match refusal {
31
+ priority_machines::Refusal::UnknownLane => Self::UnknownTenant,
32
+ }
33
+ }
34
+ }
35
+
36
+ impl fmt::Display for Refusal {
37
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38
+ f.write_str(match self {
39
+ Self::UnknownTenant => "tenant index is not one of the scheduler's tenants",
40
+ Self::UnknownLane => "lane is not one of this instance's lanes",
41
+ Self::Insufficient => "more cost asked for than the budget has available",
42
+ Self::ExceedsInFlight => "more cost returned than the budget has admitted",
43
+ })
44
+ }
45
+ }
46
+
47
+ impl core::error::Error for Refusal {}
@@ -0,0 +1,61 @@
1
+ //! Helpers shared by the crate's test modules.
2
+
3
+ use core::{fmt::Debug, num::NonZeroU32};
4
+
5
+ use matryona_machines::testing::XorShift64;
6
+
7
+ use crate::Refusal;
8
+
9
+ /// A positive test amount; zero is a broken test assumption.
10
+ pub fn nz(value: u32) -> NonZeroU32 {
11
+ let Some(value) = NonZeroU32::new(value) else {
12
+ panic!("test amounts are positive");
13
+ };
14
+ value
15
+ }
16
+
17
+ /// The next draw of `rng`, in `0..bound`, as the `u32` the cores count in.
18
+ pub fn below(rng: &mut XorShift64, bound: u32) -> u32 {
19
+ let Ok(draw) = u32::try_from(rng.below(u64::from(bound))) else {
20
+ panic!("a draw below a u32 bound fits a u32");
21
+ };
22
+ draw
23
+ }
24
+
25
+ /// The next draw of `rng` as an index, in `0..bound`.
26
+ pub fn index(rng: &mut XorShift64, bound: u32) -> usize {
27
+ let Ok(index) = usize::try_from(below(rng, bound)) else {
28
+ panic!("a u32 index fits a usize");
29
+ };
30
+ index
31
+ }
32
+
33
+ /// How often a randomized run saw each refusal. Every refusal must leave the
34
+ /// value exactly as it was.
35
+ #[derive(Debug, Default)]
36
+ pub struct Refusals {
37
+ pub unknown_tenant: u32,
38
+ pub unknown_lane: u32,
39
+ pub insufficient: u32,
40
+ pub exceeds_in_flight: u32,
41
+ }
42
+
43
+ impl Refusals {
44
+ pub fn record<T: PartialEq + Debug>(
45
+ &mut self,
46
+ refused: Option<Refusal>,
47
+ after: &T,
48
+ before: &T,
49
+ ) {
50
+ let Some(refusal) = refused else {
51
+ return;
52
+ };
53
+ assert_eq!(after, before, "{refusal:?} changed the value");
54
+ *match refusal {
55
+ Refusal::UnknownTenant => &mut self.unknown_tenant,
56
+ Refusal::UnknownLane => &mut self.unknown_lane,
57
+ Refusal::Insufficient => &mut self.insufficient,
58
+ Refusal::ExceedsInFlight => &mut self.exceeds_in_flight,
59
+ } += 1;
60
+ }
61
+ }
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "fair_machines_native"
3
- version = "0.1.4"
3
+ version = "0.1.8"
4
4
  edition.workspace = true
5
5
  rust-version.workspace = true
6
6
  license.workspace = true
@@ -16,6 +16,7 @@ doctest = false
16
16
  fair-machines.workspace = true
17
17
  magnus.workspace = true
18
18
  rb-sys.workspace = true
19
+ matryona-machines = { workspace = true, features = ["ruby"] }
19
20
 
20
21
  [lints]
21
22
  workspace = true