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.
data/lib/fair_machines.rb CHANGED
@@ -1,4 +1,37 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'fair_machines/version'
4
+
5
+ module FairMachines
6
+ # The core's `Lane`: the bucket a hash maps to, at position `index`.
7
+ # A bucket position, not an issued id: `KeyedFair.lane`, `KeyedFair.lane_at`
8
+ # and `Lane.new` build one without admitting anything, and holding one
9
+ # proves nothing. Like `Lane::new`, it holds only a position: an Integer a
10
+ # `usize` holds, TypeError for anything else, RangeError outside the range.
11
+ Lane = Data.define(:index) do
12
+ def initialize(index:)
13
+ super(index: FairMachines.__send__(:__lane, index))
14
+ end
15
+ end
16
+ end
17
+
18
+ # Native is required: there is no pure-Ruby fallback. It validates and builds
19
+ # Lane values, so the Data class above must exist first.
4
20
  require 'fair_machines_native/fair_machines_native'
21
+
22
+ module FairMachines
23
+ private_class_method :__lane
24
+
25
+ # Lane is final. `initialize` is where it validates, and a subclass could
26
+ # override it, or `allocate` build an instance that never ran it:
27
+ # subclassing raises TypeError and `allocate` is gone. This stops accidents,
28
+ # not reflection, so the binding also checks every Lane it reads.
29
+ Lane.singleton_class.undef_method(:allocate)
30
+ Lane.define_singleton_method(:inherited) { |_| raise TypeError, "#{Lane} cannot be subclassed" }
31
+
32
+ # The schedulers wrap native state and have no allocator: a subclass's
33
+ # `new` would build the base class anyway, so subclassing raises too.
34
+ [TenantFair, KeyedFair].each do |scheduler|
35
+ scheduler.define_singleton_method(:inherited) { |_| raise TypeError, "#{scheduler} cannot be subclassed" }
36
+ end
37
+ end
@@ -1,18 +1,119 @@
1
+ # Capacities, quanta and costs are Integers in 1..2**32-1: TypeError for
2
+ # anything else, RangeError outside the range, zero included. Hashes are
3
+ # Integers in 0..2**64-1; tenant and lane positions and lane counts are
4
+ # Integers a `usize` holds (0..2**64-1 on a 64-bit platform, 0..2**32-1 on a
5
+ # 32-bit one): TypeError for anything else, RangeError outside the range.
6
+ # Every message names the offending argument. Arguments are positional, in
7
+ # the core's order. Nothing here takes time, so no answer records a clock. A
8
+ # frozen object raises FrozenError on every mutating call. Refusals are
9
+ # symbols, one per core `Refusal` variant; they never raise, and a refused
10
+ # call changes nothing.
1
11
  module FairMachines
2
12
  VERSION: String
13
+
14
+ # The core's `Refusal`.
15
+ # :unknown_tenant, the tenant index is not one of the scheduler's tenants.
16
+ # :unknown_lane, the lane is not one of this instance's lanes.
17
+ # :insufficient, more cost asked for than the budget has available.
18
+ # :exceeds_in_flight, more cost returned than the budget has admitted.
19
+ type refusal = :unknown_tenant | :unknown_lane | :insufficient | :exceeds_in_flight
20
+
21
+ # The core's `Lane`: the bucket a hash maps to. A position, not an issued
22
+ # id: it can be built freely and proves nothing. Like the core's
23
+ # `Lane::new(index)`, it holds only a position: `index` is an Integer a
24
+ # `usize` holds, TypeError for anything else, RangeError outside the range.
25
+ # Sealed: subclassing raises TypeError and `allocate` is undefined. Every
26
+ # Lane the binding reads must be an instance of exactly this class
27
+ # (TypeError otherwise) and its index is re-checked (RangeError), so a
28
+ # value forged by reflection or Marshal never reaches the core.
29
+ class Lane < Data
30
+ attr_reader index: Integer
31
+ def self.new: (Integer index) -> Lane
32
+ | (index: Integer) -> Lane
33
+ end
34
+
35
+ # The core's `TenantFair<N>`; `N` is the length of the arrays.
36
+ # Sealed: subclassing raises TypeError, and there is no allocator.
3
37
  class TenantFair
4
- def self.new: (capacities: Array[Integer], quanta: Array[Integer]) -> instance
5
- def admit: (tenant: Integer, cost: Integer) -> bool
6
- def cancel: (tenant: Integer, cost: Integer) -> bool
7
- def select: (Array[Integer?] heads) -> Integer?
38
+ # The largest `N` compiled into this binding: `TenantFair<1>` to
39
+ # `TenantFair<16>`. The core takes any `N`; it has no ceiling of its own.
40
+ MAX_TENANTS: Integer
41
+
42
+ # The core's `TenantFair::new(capacities, quanta)`. `N` is the arrays'
43
+ # length: raises RangeError unless both have the same length, in
44
+ # 1..MAX_TENANTS, and TypeError unless both are Arrays.
45
+ def self.new: (Array[Integer] capacities, Array[Integer] quanta) -> TenantFair
46
+
47
+ # Honoured: :admitted. Refused: :unknown_tenant, :insufficient.
48
+ def admit: (Integer tenant, Integer cost) -> (:admitted | :unknown_tenant | :insufficient)
49
+
50
+ # Honoured: :cancelled. Refused: :unknown_tenant, :exceeds_in_flight.
51
+ # A repeat cannot be recognised: it is honoured while that much cost is
52
+ # still admitted and refused once it is not.
53
+ def cancel: (Integer tenant, Integer cost) -> (:cancelled | :unknown_tenant | :exceeds_in_flight)
54
+
55
+ # `heads` has one entry per tenant: the cost at the head of its queue,
56
+ # nil when it has nothing waiting. Honoured: the tenant to dispatch, or
57
+ # nil exactly when every head is nil. Refused: :exceeds_in_flight, some
58
+ # head costs more than its tenant has admitted. Raises RangeError
59
+ # unless `heads` has tenant_count entries.
60
+ def select: (Array[Integer?] heads) -> (Integer | nil | :exceeds_in_flight)
61
+
8
62
  def tenant_count: () -> Integer
63
+
64
+ # Cost admitted and not yet dispatched or cancelled; nil for an index
65
+ # that is not a tenant. A query.
66
+ def in_flight: (Integer tenant) -> Integer?
67
+
68
+ # Cost the tenant may still admit; nil for an index that is not a tenant.
69
+ def available: (Integer tenant) -> Integer?
9
70
  end
71
+
72
+ # The core's `KeyedFair<LANES>`. `LANES` is a const generic in the core;
73
+ # here it is the first argument wherever the core names the type.
74
+ # Sealed like TenantFair.
10
75
  class KeyedFair
11
- def self.new: (lanes: Integer, capacity: Integer) -> instance
12
- def admit: (hash: Integer, cost: Integer) -> Integer?
13
- def release: (lane: Integer, cost: Integer) -> bool
14
- def in_flight: (Integer lane) -> Integer
15
- def lane: (Integer hash) -> Integer
76
+ # The largest `LANES` compiled into this binding: `KeyedFair<1>` to
77
+ # `KeyedFair<256>`. The core takes any `LANES > 0`; it has no ceiling of
78
+ # its own.
79
+ MAX_LANES: Integer
80
+
81
+ # The core's `KeyedFair::<LANES>::new(capacity)`. Raises RangeError
82
+ # unless `lanes` is in 1..MAX_LANES.
83
+ def self.new: (Integer lanes, Integer capacity) -> KeyedFair
84
+
85
+ # The core's associated `KeyedFair::<LANES>::lane(hash)`: the lane a hash
86
+ # maps to. A pure function. Raises RangeError unless `lanes` is in
87
+ # 1..MAX_LANES.
88
+ def self.lane: (Integer lanes, Integer hash) -> Lane
89
+
90
+ # The core's associated `KeyedFair::<LANES>::lane_at(index)`: nil for an
91
+ # index of `lanes` or more. Raises RangeError unless `lanes` is in
92
+ # 1..MAX_LANES.
93
+ def self.lane_at: (Integer lanes, Integer index) -> Lane?
94
+
95
+ # Honoured: the Lane that was charged. Refused: :insufficient. The Lane
96
+ # is built through `Lane.new` before the admission is kept: if that
97
+ # raises, freezes this KeyedFair (FrozenError) or changes it
98
+ # (RuntimeError), nothing is admitted.
99
+ def admit: (Integer hash, Integer cost) -> (Lane | :insufficient)
100
+
101
+ # Honoured: :released. Refused: :unknown_lane, :exceeds_in_flight. A
102
+ # repeat cannot be recognised: it is honoured while that much cost is
103
+ # still in flight and refused once it is not. Raises TypeError unless
104
+ # given a Lane.
105
+ def release: (Lane lane, Integer cost) -> (:released | :unknown_lane | :exceeds_in_flight)
106
+
16
107
  def lane_count: () -> Integer
108
+
109
+ # Cost admitted to the lane and not yet released; nil for a lane this
110
+ # instance does not have. Raises TypeError unless given a Lane.
111
+ def in_flight: (Lane lane) -> Integer?
112
+
113
+ # Cost the lane may still admit; nil for a lane this instance does not have.
114
+ def available: (Lane lane) -> Integer?
17
115
  end
116
+
117
+ # Private native hook `Lane#initialize` validates its index through.
118
+ private def self.__lane: (Integer index) -> Integer
18
119
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fair_machines
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih
@@ -29,8 +29,8 @@ dependencies:
29
29
  - - ">="
30
30
  - !ruby/object:Gem::Version
31
31
  version: 0.9.130
32
- description: Initial unstable Ruby adapter to the no_std fair-machines Rust core.
33
- Local source builds require unpublished path-only flow and priority siblings.
32
+ description: 'Ruby adapter to the no_std fair-machines Rust core: per-tenant cost
33
+ budgets with deficit round robin dispatch, and hashed lanes for an open tenant set.'
34
34
  email:
35
35
  - oss@seuros.com
36
36
  executables: []
@@ -39,6 +39,7 @@ extensions:
39
39
  extra_rdoc_files: []
40
40
  files:
41
41
  - CHANGELOG.md
42
+ - Cargo.lock
42
43
  - Cargo.toml
43
44
  - LICENSE.txt
44
45
  - README.md
@@ -47,6 +48,8 @@ files:
47
48
  - ext/fair_machines_native/core/README.md
48
49
  - ext/fair_machines_native/core/src/keyed.rs
49
50
  - ext/fair_machines_native/core/src/lib.rs
51
+ - ext/fair_machines_native/core/src/refusal.rs
52
+ - ext/fair_machines_native/core/src/test_support.rs
50
53
  - ext/fair_machines_native/extconf.rb
51
54
  - ext/fair_machines_native/ffi/Cargo.toml
52
55
  - ext/fair_machines_native/ffi/extconf.rb