rogue_machines 0.1.2 → 0.5.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +64 -0
- data/Cargo.lock +22 -2
- data/Cargo.toml +2 -0
- data/README.md +147 -36
- data/ext/rogue_machines_native/core/Cargo.toml +4 -6
- data/ext/rogue_machines_native/core/src/experiment.rs +50 -16
- data/ext/rogue_machines_native/core/src/lib.rs +21 -8
- data/ext/rogue_machines_native/core/src/rogue.rs +152 -63
- data/ext/rogue_machines_native/core/src/slot.rs +34 -0
- data/ext/rogue_machines_native/core/src/test_support.rs +27 -0
- data/ext/rogue_machines_native/ffi/Cargo.toml +5 -9
- data/ext/rogue_machines_native/ffi/src/init.rs +15 -0
- data/ext/rogue_machines_native/ffi/src/lib.rs +12 -166
- data/ext/rogue_machines_native/ffi/src/rogue.rs +194 -0
- data/ext/rogue_machines_native/ffi/src/values.rs +288 -0
- data/lib/rogue_machines/runtime.rb +312 -0
- data/lib/rogue_machines/version.rb +1 -1
- data/lib/rogue_machines.rb +94 -264
- data/sig/rogue_machines.rbs +210 -104
- metadata +9 -6
- data/ext/rogue_machines_native/core/src/armed.rs +0 -78
- data/ext/rogue_machines_native/ffi/src/injection.rs +0 -44
- data/ext/rogue_machines_native/ffi/src/registry.rs +0 -288
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b062ebd1a2de12c036e33397cd155f13ba36eeef4d44f93921bc0d9516320379
|
|
4
|
+
data.tar.gz: 772b43076ae84dc160c411747e0b5e440e87a8901e0d1dbbb7e51063183426a6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d23272a39f481186fcb83286953873aa54cc2c60c8d3ca96674e9ed07fa1a94972953d33b7dd5680566b77f97d71178c0a925d14561ae1e134074b0dfdf2865e
|
|
7
|
+
data.tar.gz: d82c00961cc312c0a8b1a10dd589ed7825b92f2d6b5062ee6e1ebb7b2e4a21a617e244d281b347d2598fb85f32d1c288a056dcb8eb827ef428c97acea9b10470
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,69 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.5.0](https://github.com/seuros/rogue_machines/compare/rogue_machines/v0.4.0...rogue_machines/v0.5.0) (2026-11-16)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### ⚠ BREAKING CHANGES
|
|
7
|
+
|
|
8
|
+
* carry the runtime's garbled substitute in Replaced and Disarmed
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* carry the runtime's garbled substitute in Replaced and Disarmed ([acece66](https://github.com/seuros/rogue_machines/commit/acece668aa14809616fc90c3eb7f2c9665325dad))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Bug Fixes
|
|
16
|
+
|
|
17
|
+
* fix wrapped classes at load so building a value runs no Ruby ([39982ca](https://github.com/seuros/rogue_machines/commit/39982cace4de937ec30e83b9da15be38b0ed46b3))
|
|
18
|
+
|
|
19
|
+
## [0.4.0](https://github.com/seuros/rogue_machines/compare/rogue_machines/v0.3.1...rogue_machines/v0.4.0) (2026-11-14)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### ⚠ BREAKING CHANGES
|
|
23
|
+
|
|
24
|
+
* adopt matryona 0.4 symbol_or_fields for faults, naming what was received
|
|
25
|
+
|
|
26
|
+
### Features
|
|
27
|
+
|
|
28
|
+
* adopt matryona 0.4 symbol_or_fields for faults, naming what was received ([29d07fe](https://github.com/seuros/rogue_machines/commit/29d07fe671cf3f00c8ff284f3a03dcaab0f0176e))
|
|
29
|
+
|
|
30
|
+
## [0.3.1](https://github.com/seuros/rogue_machines/compare/rogue_machines/v0.3.0...rogue_machines/v0.3.1) (2026-11-14)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
### Bug Fixes
|
|
34
|
+
|
|
35
|
+
* **deps:** require matryona-machines >= 0.3 ([b92985b](https://github.com/seuros/rogue_machines/commit/b92985b2ee76787772e6d6c1e5deeffa1a039d89))
|
|
36
|
+
|
|
37
|
+
## [0.3.0](https://github.com/seuros/rogue_machines/compare/rogue_machines/v0.2.0...rogue_machines/v0.3.0) (2026-11-14)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
### ⚠ BREAKING CHANGES
|
|
41
|
+
|
|
42
|
+
* adopt matryona-machines 0.2: struct-only field reads, caret rb-sys, missing_docs
|
|
43
|
+
|
|
44
|
+
### Features
|
|
45
|
+
|
|
46
|
+
* adopt matryona-machines 0.2: struct-only field reads, caret rb-sys, missing_docs ([af562d6](https://github.com/seuros/rogue_machines/commit/af562d60cde84679fc6b8bce9eedb4af3559a0b4))
|
|
47
|
+
|
|
48
|
+
## [0.2.0](https://github.com/seuros/rogue_machines/compare/rogue_machines/v0.1.3...rogue_machines/v0.2.0) (2026-11-14)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
### ⚠ BREAKING CHANGES
|
|
52
|
+
|
|
53
|
+
* mirror the core Rogue with sealed validated values, garbled replacement answers Replaced, runtime hands out a frozen copy
|
|
54
|
+
|
|
55
|
+
### Features
|
|
56
|
+
|
|
57
|
+
* mirror the core Rogue with sealed validated values, garbled replacement answers Replaced, runtime hands out a frozen copy ([e805ae4](https://github.com/seuros/rogue_machines/commit/e805ae4792f25f5aeaa24b165192c14b80362067))
|
|
58
|
+
|
|
59
|
+
## [0.1.3](https://github.com/seuros/rogue_machines/compare/rogue_machines/v0.1.2...rogue_machines/v0.1.3) (2026-11-06)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
### Bug Fixes
|
|
63
|
+
|
|
64
|
+
* extract arg_error and range error helpers in ffi ([ee6ccf4](https://github.com/seuros/rogue_machines/commit/ee6ccf4ef0caef9f5ef7b4496ee0ccdb633ddc1e))
|
|
65
|
+
* keep test modules out of the packaged crate ([425cf81](https://github.com/seuros/rogue_machines/commit/425cf81b4e94ea2abd8de21ec74426c71338c11c))
|
|
66
|
+
|
|
3
67
|
## [0.1.2](https://github.com/seuros/rogue_machines/compare/rogue_machines/v0.1.1...rogue_machines/v0.1.2) (2026-11-01)
|
|
4
68
|
|
|
5
69
|
|
data/Cargo.lock
CHANGED
|
@@ -35,6 +35,12 @@ version = "2.13.2"
|
|
|
35
35
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
36
36
|
checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06"
|
|
37
37
|
|
|
38
|
+
[[package]]
|
|
39
|
+
name = "bytes"
|
|
40
|
+
version = "1.12.1"
|
|
41
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
42
|
+
checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04"
|
|
43
|
+
|
|
38
44
|
[[package]]
|
|
39
45
|
name = "cexpr"
|
|
40
46
|
version = "0.6.0"
|
|
@@ -110,6 +116,7 @@ version = "0.9.3"
|
|
|
110
116
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
111
117
|
checksum = "40ef424404d166cbb71c8b8d19c9c589c748c4547c1afa1928c5504299f6c713"
|
|
112
118
|
dependencies = [
|
|
119
|
+
"bytes",
|
|
113
120
|
"magnus-macros",
|
|
114
121
|
"rb-sys",
|
|
115
122
|
"rb-sys-env",
|
|
@@ -127,6 +134,15 @@ dependencies = [
|
|
|
127
134
|
"syn",
|
|
128
135
|
]
|
|
129
136
|
|
|
137
|
+
[[package]]
|
|
138
|
+
name = "matryona-machines"
|
|
139
|
+
version = "0.6.0"
|
|
140
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
141
|
+
checksum = "cf8a06ab02efd767270815a1f515a40bbf3bfc77038e9360676ebe8d47833b07"
|
|
142
|
+
dependencies = [
|
|
143
|
+
"magnus",
|
|
144
|
+
]
|
|
145
|
+
|
|
130
146
|
[[package]]
|
|
131
147
|
name = "memchr"
|
|
132
148
|
version = "2.8.3"
|
|
@@ -228,13 +244,17 @@ checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4"
|
|
|
228
244
|
|
|
229
245
|
[[package]]
|
|
230
246
|
name = "rogue-machines"
|
|
231
|
-
version = "0.
|
|
247
|
+
version = "0.5.0"
|
|
248
|
+
dependencies = [
|
|
249
|
+
"matryona-machines",
|
|
250
|
+
]
|
|
232
251
|
|
|
233
252
|
[[package]]
|
|
234
253
|
name = "rogue_machines_native"
|
|
235
|
-
version = "0.1.
|
|
254
|
+
version = "0.1.5"
|
|
236
255
|
dependencies = [
|
|
237
256
|
"magnus",
|
|
257
|
+
"matryona-machines",
|
|
238
258
|
"rb-sys",
|
|
239
259
|
"rogue-machines",
|
|
240
260
|
]
|
data/Cargo.toml
CHANGED
|
@@ -8,10 +8,12 @@ rust-version = "1.99"
|
|
|
8
8
|
|
|
9
9
|
[workspace.dependencies]
|
|
10
10
|
rogue-machines = { path = "ext/rogue_machines_native/core" }
|
|
11
|
+
matryona-machines = { version = ">=0.6", default-features = false }
|
|
11
12
|
|
|
12
13
|
[workspace.lints.rust]
|
|
13
14
|
unsafe_code = "forbid"
|
|
14
15
|
unused_must_use = "warn"
|
|
16
|
+
missing_docs = "warn"
|
|
15
17
|
|
|
16
18
|
[workspace.lints.clippy]
|
|
17
19
|
all = { level = "warn", priority = -1 }
|
data/README.md
CHANGED
|
@@ -26,7 +26,9 @@ Requires **Ruby 4.0+** and a Rust toolchain, or a platform gem.
|
|
|
26
26
|
```ruby
|
|
27
27
|
require 'rogue_machines'
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
MINUTE = 60 * 1_000_000_000 # Integer nanoseconds, like every time in this gem
|
|
30
|
+
|
|
31
|
+
RogueMachines.arm(:payments, fault: :raise, share: 0.01, expires_in: 5 * MINUTE)
|
|
30
32
|
|
|
31
33
|
RogueMachines.run(:payments) do
|
|
32
34
|
Payments.charge(order)
|
|
@@ -50,8 +52,8 @@ RogueMachines.run(:payments) { Payments.charge(order) } # the real call
|
|
|
50
52
|
```
|
|
51
53
|
|
|
52
54
|
It allocates nothing: zero objects per call for a Symbol, a String or a frozen
|
|
53
|
-
String name. The overhead is
|
|
54
|
-
than the native read. That is why this ships as a runtime dependency rather
|
|
55
|
+
String name. The overhead is a couple of hundred nanoseconds, most of it the
|
|
56
|
+
name check rather than the native read. That is why this ships as a runtime dependency rather
|
|
55
57
|
than a test-only one. The interlock sits on every call to every protected
|
|
56
58
|
dependency, so it has to be cheap enough to leave in.
|
|
57
59
|
|
|
@@ -62,12 +64,12 @@ file. Arming is an explicit call naming a target and a share of traffic.
|
|
|
62
64
|
|
|
63
65
|
## ⏱️ Every Experiment Self-Destructs
|
|
64
66
|
|
|
65
|
-
`expires_in:` is **required
|
|
66
|
-
outlives the officer who armed it, and no constructor in the
|
|
67
|
-
takes a fault without
|
|
67
|
+
`expires_in:` is **required**, in Integer nanoseconds. There is no way to arm
|
|
68
|
+
an experiment that outlives the officer who armed it, and no constructor in the
|
|
69
|
+
Rust core that takes a fault without an instant it expires at.
|
|
68
70
|
|
|
69
71
|
```ruby
|
|
70
|
-
RogueMachines.arm(:payments, fault: :raise, share: 1.0, expires_in:
|
|
72
|
+
RogueMachines.arm(:payments, fault: :raise, share: 1.0, expires_in: MINUTE)
|
|
71
73
|
RogueMachines.armed # => [:payments]
|
|
72
74
|
|
|
73
75
|
# sixty seconds later, with nobody touching anything
|
|
@@ -75,10 +77,11 @@ RogueMachines.armed # => []
|
|
|
75
77
|
RogueMachines.armed? # => false
|
|
76
78
|
```
|
|
77
79
|
|
|
78
|
-
An expiry is **
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
An expiry is **judged, not scheduled**. An expired experiment injects nothing
|
|
81
|
+
from its expiry on, but the core only empties its slot when told to sweep. The
|
|
82
|
+
runtime sweeps under its lock whenever `run`, `armed`, `armed?` or `arm` takes
|
|
83
|
+
a reading, and once swept a clock that steps backwards cannot bring the
|
|
84
|
+
experiment back.
|
|
82
85
|
|
|
83
86
|
An experiment lasts at most twenty-four hours. Past that it is configuration,
|
|
84
87
|
not an experiment, and it must be armed again deliberately.
|
|
@@ -89,13 +92,14 @@ not an experiment, and it must be armed again deliberately.
|
|
|
89
92
|
|
|
90
93
|
```ruby
|
|
91
94
|
# Fail.
|
|
92
|
-
RogueMachines.arm(:payments, fault: :raise, share: 0.01, expires_in:
|
|
95
|
+
RogueMachines.arm(:payments, fault: :raise, share: 0.01, expires_in: 5 * MINUTE)
|
|
93
96
|
|
|
94
|
-
# Be slow, then make the real call.
|
|
95
|
-
RogueMachines.arm(:search, fault:
|
|
97
|
+
# Be slow (two seconds, in nanoseconds), then make the real call.
|
|
98
|
+
RogueMachines.arm(:search, fault: RogueMachines::Delay.new(2_000_000_000), share: 0.5,
|
|
99
|
+
expires_in: 5 * MINUTE)
|
|
96
100
|
|
|
97
101
|
# Succeed, with a substitute.
|
|
98
|
-
RogueMachines.arm(:catalogue, fault: :garble, garbled: [], share: 0.1, expires_in:
|
|
102
|
+
RogueMachines.arm(:catalogue, fault: :garble, garbled: [], share: 0.1, expires_in: 5 * MINUTE)
|
|
99
103
|
```
|
|
100
104
|
|
|
101
105
|
A `:raise` raises `RogueMachines::Injected`. A `:garble` returns `garbled:`
|
|
@@ -103,7 +107,7 @@ without running the block, which defaults to `nil` so a forgotten substitute
|
|
|
103
107
|
cannot quietly return the real value.
|
|
104
108
|
|
|
105
109
|
**There is no weapon that holds a caller forever.** A dependency that times out
|
|
106
|
-
is a
|
|
110
|
+
is a `Delay` longer than the caller's own budget, which leaves the caller's
|
|
107
111
|
timeout in charge of when it gives up. The delay is spent against the ambient
|
|
108
112
|
`deadline_machines` budget, so a stall inside a deadline trips that deadline
|
|
109
113
|
instead of sleeping past it. Because a deadline is opt-in per call site, a
|
|
@@ -114,8 +118,8 @@ it bounds what a mistyped argument can do to a caller that has none.
|
|
|
114
118
|
|
|
115
119
|
## 🎲 Targeting
|
|
116
120
|
|
|
117
|
-
`share:` is a fraction of calls between 0 and 1
|
|
118
|
-
call comes from `RogueMachines.random`. Replace it to replay an exact run:
|
|
121
|
+
`share:` is a fraction of calls between 0 and 1 (or a `Fraction` of
|
|
122
|
+
millionths), and the draw deciding each call comes from `RogueMachines.random`. Replace it to replay an exact run:
|
|
119
123
|
|
|
120
124
|
```ruby
|
|
121
125
|
RogueMachines.random = Random.new(1234)
|
|
@@ -136,8 +140,8 @@ expire an experiment without sleeping:
|
|
|
136
140
|
|
|
137
141
|
```ruby
|
|
138
142
|
RogueMachines.clock = DeadlineMachines::TestClock.new
|
|
139
|
-
RogueMachines.arm(:payments, fault: :raise, share: 1.0, expires_in:
|
|
140
|
-
RogueMachines.clock.
|
|
143
|
+
RogueMachines.arm(:payments, fault: :raise, share: 1.0, expires_in: MINUTE)
|
|
144
|
+
RogueMachines.clock.now_ns += MINUTE
|
|
141
145
|
RogueMachines.armed? # => false
|
|
142
146
|
```
|
|
143
147
|
|
|
@@ -148,15 +152,15 @@ rather than tripping it.
|
|
|
148
152
|
everything first, because every armed expiry was computed against the outgoing
|
|
149
153
|
clock and an expiry is only meaningful against the clock it came from.
|
|
150
154
|
|
|
151
|
-
It is safe to do under load. A reading is taken before
|
|
152
|
-
caller's clock never runs inside
|
|
153
|
-
through when the clock is replaced.
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
155
|
+
It is safe to do under load. A reading is taken before the runtime's lock, so
|
|
156
|
+
that a caller's clock never runs inside it, which means a call can be part way
|
|
157
|
+
through when the clock is replaced. Once the lock is held, the call checks that
|
|
158
|
+
the clock it read is still the one installed; if not, the reading is not acted
|
|
159
|
+
on and is taken again, up to `READINGS` times. After that, arming raises
|
|
160
|
+
`RogueMachines::Error` and nothing is armed, a decision makes the real call,
|
|
161
|
+
and a query reports nothing. A clock returning something that is not an Integer
|
|
162
|
+
raises `TypeError` naming the clock, and a negative reading `RangeError`.
|
|
163
|
+
`disarm` and `disarm_all` take no reading at all.
|
|
160
164
|
|
|
161
165
|
Replacing `DeadlineMachines.clock` underneath is reflected, since the
|
|
162
166
|
delegation is not memoised, but it cannot disarm anything. A clock whose epoch
|
|
@@ -169,7 +173,9 @@ experiment is measured from slightly before it is armed. With a real monotonic
|
|
|
169
173
|
clock that is microseconds. Nothing infers that a reading came from a
|
|
170
174
|
different clock: with several threads taking their own readings, a reading
|
|
171
175
|
older than the arming is ordinary interleaving, and treating it as evidence of
|
|
172
|
-
a clock change retires experiments that were just armed.
|
|
176
|
+
a clock change retires experiments that were just armed. For the same reason a
|
|
177
|
+
reading held across a backwards step of the *same* clock sweeps against
|
|
178
|
+
expiries it predates; replace clocks through `RogueMachines.clock=`.
|
|
173
179
|
|
|
174
180
|
---
|
|
175
181
|
|
|
@@ -178,9 +184,114 @@ a clock change retires experiments that were just armed.
|
|
|
178
184
|
There is no pure-Ruby fallback and no switch to turn one on. `require
|
|
179
185
|
'rogue_machines'` loads the extension and fails if it is not there. The gem is
|
|
180
186
|
a binding to the `rogue-machines` crate in `ext/rogue_machines_native/core`,
|
|
181
|
-
which has zero dependencies and
|
|
182
|
-
|
|
183
|
-
|
|
187
|
+
which has zero dependencies, no features, and is plain `no_std`: the crate the
|
|
188
|
+
gem consumes is the one that builds for wasm and for a microcontroller.
|
|
189
|
+
|
|
190
|
+
---
|
|
191
|
+
|
|
192
|
+
## 🪞 The Core, Mirrored
|
|
193
|
+
|
|
194
|
+
Under the runtime sits a plain mirror of the core: `RogueMachines::Rogue` and
|
|
195
|
+
the Data values `Slot`, `Fraction`, `Draw`, `Delay`, `Experiment`, `Inject`,
|
|
196
|
+
`Replaced` and `Disarmed`, positional like the Rust constructors, with Integer
|
|
197
|
+
nanoseconds and symbols for unit variants (`:raise`, `:garble`, `:pass`,
|
|
198
|
+
`:armed`, `:already_armed`, `:not_armed`).
|
|
199
|
+
|
|
200
|
+
```ruby
|
|
201
|
+
include RogueMachines
|
|
202
|
+
|
|
203
|
+
rogue = Rogue.new
|
|
204
|
+
slot = Slot.new(0)
|
|
205
|
+
experiment = Experiment.new(:raise, Fraction::ALL, 100)
|
|
206
|
+
|
|
207
|
+
rogue.arm(slot, experiment) # => :armed
|
|
208
|
+
rogue.arm(slot, experiment) # => :already_armed
|
|
209
|
+
rogue.decide(slot, 99, Draw.new(0)) # => #<data Inject fault=:raise>
|
|
210
|
+
rogue.decide(slot, 100, Draw.new(0)) # => :pass
|
|
211
|
+
rogue.any_armed? # => true: expired, still held
|
|
212
|
+
rogue.sweep(100) # => 1
|
|
213
|
+
rogue.disarm(slot) # => :not_armed
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
Every Data value validates through the core when it is built, by the gem or by
|
|
217
|
+
hand: `TypeError` for the wrong type, `RangeError` for a value the core's
|
|
218
|
+
constructor rejects (`Slot.new(32)`, `Fraction.new(1_000_001)`,
|
|
219
|
+
`Draw.new(1_000_000)`, `Delay.new(-1)`, `Experiment.new(:nope, ...)`).
|
|
220
|
+
The value classes, and `Rogue`, are sealed: subclassing one raises
|
|
221
|
+
`TypeError` and `allocate` is undefined. Sealing stops accidents, not
|
|
222
|
+
reflection (a named subclass's constant is bound before `inherited` raises,
|
|
223
|
+
and `Marshal.load` skips `initialize`), so the binding is the authority: every
|
|
224
|
+
value it reads must be an instance of exactly the gem's class (`TypeError`
|
|
225
|
+
otherwise, a subclass included) and is rebuilt through the core's constructors
|
|
226
|
+
(`RangeError` for a forged one), before the core sees it.
|
|
227
|
+
|
|
228
|
+
## 📜 Contract
|
|
229
|
+
|
|
230
|
+
- **Refusals.** The core has none: a `Slot` is the caller's own number, not an
|
|
231
|
+
issued id, and every slot can always be armed. The runtime has one,
|
|
232
|
+
`RogueMachines.arm` answering `:full` when every slot holds a live
|
|
233
|
+
experiment under another name; it changes nothing and never raises.
|
|
234
|
+
- **Answers say what held before.** Arming answers `:armed`, a `Replaced`
|
|
235
|
+
holding the experiment it displaced (expired or not) and, from the runtime,
|
|
236
|
+
the `garbled:` substitute that went with it (`nil` for none; the core mirror
|
|
237
|
+
`Rogue` keeps no substitutes, so its `Replaced` always holds `nil`), or
|
|
238
|
+
`:already_armed`;
|
|
239
|
+
disarming answers a `Disarmed` holding what was there and, from the
|
|
240
|
+
runtime, the substitute it removed (`nil` for none, always `nil` from
|
|
241
|
+
`Rogue`), or `:not_armed`.
|
|
242
|
+
- **Honoured repeats.** Re-arming a slot with the identical experiment and
|
|
243
|
+
disarming an empty slot are provable from the slot itself, so they are
|
|
244
|
+
honoured and change nothing. In the runtime a `:garble` substitute is part
|
|
245
|
+
of what is armed: `RogueMachines.arm` answers `:already_armed` only when the
|
|
246
|
+
stored substitute is equal (`==`) too; a differing one replaces it and
|
|
247
|
+
answers a `Replaced` holding the experiment and the old substitute, as a
|
|
248
|
+
differing experiment does.
|
|
249
|
+
- **Queries are pure.** In the core and in `Rogue`, every query judges expiry
|
|
250
|
+
against the instant it is given and changes nothing; only `sweep` empties
|
|
251
|
+
expired slots. The runtime's `armed`, `armed?`, `run` and `arm` sweep on
|
|
252
|
+
purpose, under the lock, with a reading against the installed clock.
|
|
253
|
+
- **Time** is Integer nanoseconds in and out: `expires_at`, `Delay#duration`,
|
|
254
|
+
every `now`, `expires_in:`. Non-Integer is `TypeError`; negative or past
|
|
255
|
+
`2**64 - 1` is `RangeError`.
|
|
256
|
+
- **Scopes and restarts.** Nothing is issued, so there is no scope. A fresh
|
|
257
|
+
process starts disarmed.
|
|
258
|
+
- **Exceptions.** Malformed input raises `TypeError` or `RangeError` before
|
|
259
|
+
anything changes. `Injected` is the fault itself; `Error` is a clock replaced
|
|
260
|
+
faster than it can be read.
|
|
261
|
+
- **Never loses work.** `Rogue#arm` and `#disarm` answer with values built by
|
|
262
|
+
Ruby, so each decides on a copy and keeps it only once the answer exists: a
|
|
263
|
+
raise while building it (a redefined `new`) applies nothing, freezing the
|
|
264
|
+
Rogue meanwhile raises `FrozenError` and changing it `RuntimeError`, with the
|
|
265
|
+
outer call not applied. No call holds the Rogue while Ruby code runs, so a
|
|
266
|
+
re-entrant call is an ordinary one, never a `fatal`.
|
|
267
|
+
- **The runtime's Rogue is not yours.** `RogueMachines.rogue` answers a frozen
|
|
268
|
+
copy taken under the lock: changing the runtime's own Rogue behind the names
|
|
269
|
+
would leave a name on an empty slot that the next arm hands to another name.
|
|
270
|
+
`Rogue#dup` is an unfrozen copy to experiment on.
|
|
271
|
+
|
|
272
|
+
## Every answer
|
|
273
|
+
|
|
274
|
+
No answer records the clock: the core keeps none.
|
|
275
|
+
|
|
276
|
+
| Call | Honoured | Refused |
|
|
277
|
+
|:--|:--|:--|
|
|
278
|
+
| `Rogue#arm(slot, experiment)` | `:armed`, `Replaced`, `:already_armed` (unchanged) | never |
|
|
279
|
+
| `Rogue#disarm(slot)` | `Disarmed`, `:not_armed` (unchanged) | never |
|
|
280
|
+
| `Rogue#disarm_all` | slots emptied; `0` is unchanged | never |
|
|
281
|
+
| `Rogue#sweep(now)` | slots emptied because expired; `0` is unchanged | never |
|
|
282
|
+
| `Rogue#any_armed?` / `armed_count` | held slots, expired or not | query |
|
|
283
|
+
| `Rogue#live_count(now)` | slots live at `now` | query |
|
|
284
|
+
| `Rogue#vacant` | the lowest empty `Slot`, or `nil` | query |
|
|
285
|
+
| `Rogue#experiment(slot)` | what it holds, or `nil` | query |
|
|
286
|
+
| `Rogue#active(slot, now)` | what it holds if live at `now`, or `nil` | query |
|
|
287
|
+
| `Rogue#decide(slot, now, draw)` | `Inject`, `:pass` | query |
|
|
288
|
+
| `Rogue#dup` / `clone` | a copy holding the same experiments | query |
|
|
289
|
+
| `RogueMachines.rogue` | a frozen copy of the runtime's Rogue | query |
|
|
290
|
+
| `RogueMachines.arm(name, ...)` | `:armed`, `Replaced` (also for a new `garbled:`), `:already_armed` (unchanged) | `:full` (unchanged) |
|
|
291
|
+
| `RogueMachines.disarm(name)` | `Disarmed`, `:not_armed` (unchanged) | never |
|
|
292
|
+
| `RogueMachines.disarm_all` | experiments emptied; `0` is unchanged | never |
|
|
293
|
+
| `RogueMachines.armed` / `armed?` | names live now | never |
|
|
294
|
+
| `RogueMachines.run(name) { }` | the block's value, the substitute, or raises `Injected` | never |
|
|
184
295
|
|
|
185
296
|
---
|
|
186
297
|
|
|
@@ -188,11 +299,11 @@ gem consumes it is not `no_std`.
|
|
|
188
299
|
|
|
189
300
|
- **No breaker, limiter, retry or budget.** It trips those. `breaker_machines`,
|
|
190
301
|
`throttle_machines`, `chrono_machines` and `shed_machines` own them.
|
|
191
|
-
- **No clock of its own.** Every
|
|
192
|
-
which is why the core builds for bare metal.
|
|
302
|
+
- **No clock of its own.** Every instant is a `Duration` the caller passes in
|
|
303
|
+
(Integer nanoseconds in Ruby), which is why the core builds for bare metal.
|
|
193
304
|
- **No randomness of its own.** The draw is the caller's.
|
|
194
305
|
- **No capacity beyond 32 experiments**, because the armed set is one 32-bit
|
|
195
|
-
word and the interlock on the hot path is a single
|
|
306
|
+
word and the interlock on the hot path is a single read of it.
|
|
196
307
|
|
|
197
308
|
---
|
|
198
309
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "rogue-machines"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.5.0"
|
|
4
4
|
edition.workspace = true
|
|
5
5
|
rust-version.workspace = true
|
|
6
6
|
authors = ["Abdelkader Boudih <oss@seuros.com>"]
|
|
@@ -10,15 +10,13 @@ repository = "https://github.com/seuros/rogue_machines"
|
|
|
10
10
|
keywords = ["fault-injection", "chaos-engineering", "resilience", "testing", "no-std"]
|
|
11
11
|
categories = ["no-std", "development-tools::testing"]
|
|
12
12
|
readme = "README.md"
|
|
13
|
-
|
|
13
|
+
exclude = ["src/tests/", "src/**/tests.rs", "tests/", "src/test_support.rs"]
|
|
14
14
|
[lib]
|
|
15
15
|
name = "rogue_machines"
|
|
16
16
|
crate-type = ["lib"]
|
|
17
17
|
|
|
18
|
-
[
|
|
19
|
-
|
|
20
|
-
std = ["alloc"]
|
|
21
|
-
alloc = []
|
|
18
|
+
[dev-dependencies]
|
|
19
|
+
matryona-machines = { workspace = true, features = ["testing"] }
|
|
22
20
|
|
|
23
21
|
[lints]
|
|
24
22
|
workspace = true
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
//! What a fault is, who gets it, and when it stops.
|
|
2
2
|
|
|
3
|
+
use core::time::Duration;
|
|
4
|
+
|
|
3
5
|
use crate::PER_MILLION;
|
|
4
6
|
|
|
5
7
|
/// What the caller does instead of the real call.
|
|
@@ -7,8 +9,8 @@ use crate::PER_MILLION;
|
|
|
7
9
|
pub enum Fault {
|
|
8
10
|
/// Fail.
|
|
9
11
|
Raise,
|
|
10
|
-
/// Take this
|
|
11
|
-
Delay
|
|
12
|
+
/// Take this much longer, then make the real call.
|
|
13
|
+
Delay(Duration),
|
|
12
14
|
/// Succeed, with a substitute value.
|
|
13
15
|
Garble,
|
|
14
16
|
}
|
|
@@ -34,49 +36,81 @@ impl Fraction {
|
|
|
34
36
|
}
|
|
35
37
|
}
|
|
36
38
|
|
|
39
|
+
/// The share, in millionths.
|
|
37
40
|
#[must_use]
|
|
38
41
|
pub const fn millionths(self) -> u32 {
|
|
39
42
|
self.0
|
|
40
43
|
}
|
|
41
44
|
|
|
42
|
-
/// Whether `draw
|
|
45
|
+
/// Whether `draw` falls in this share.
|
|
46
|
+
#[must_use]
|
|
47
|
+
pub const fn covers(self, draw: Draw) -> bool {
|
|
48
|
+
draw.0 < self.0
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/// One call's random draw, in millionths: always below [`PER_MILLION`].
|
|
53
|
+
///
|
|
54
|
+
/// A type rather than a bare integer so a draw at or above the whole, which
|
|
55
|
+
/// no share covers and which would bias every decision to pass, cannot reach
|
|
56
|
+
/// a decision at all.
|
|
57
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
|
58
|
+
pub struct Draw(u32);
|
|
59
|
+
|
|
60
|
+
impl Draw {
|
|
61
|
+
/// Rejects a draw at or above [`PER_MILLION`].
|
|
62
|
+
#[must_use]
|
|
63
|
+
pub const fn new(millionths: u32) -> Option<Self> {
|
|
64
|
+
if millionths < PER_MILLION {
|
|
65
|
+
Some(Self(millionths))
|
|
66
|
+
} else {
|
|
67
|
+
None
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/// The draw, in millionths.
|
|
43
72
|
#[must_use]
|
|
44
|
-
pub const fn
|
|
45
|
-
|
|
73
|
+
pub const fn millionths(self) -> u32 {
|
|
74
|
+
self.0
|
|
46
75
|
}
|
|
47
76
|
}
|
|
48
77
|
|
|
49
78
|
/// An armed fault.
|
|
50
79
|
///
|
|
51
80
|
/// The expiry is what keeps an experiment from outliving the person who armed
|
|
52
|
-
/// it, so there is no way to build one without
|
|
81
|
+
/// it, so there is no way to build one without an instant it expires at.
|
|
82
|
+
/// Every combination of fields is a valid experiment.
|
|
53
83
|
///
|
|
54
84
|
/// An expiry is only meaningful against the clock it was computed on. This
|
|
55
|
-
/// does not try to infer that
|
|
56
|
-
/// several threads taking their own readings,
|
|
57
|
-
/// is ordinary interleaving rather than evidence of anything
|
|
58
|
-
/// as evidence retires experiments that were just armed.
|
|
85
|
+
/// does not try to infer that an instant came from a different clock: with
|
|
86
|
+
/// several threads taking their own readings, an instant older than the
|
|
87
|
+
/// arming is ordinary interleaving rather than evidence of anything.
|
|
59
88
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
60
89
|
pub struct Experiment {
|
|
90
|
+
/// What an injected call does.
|
|
61
91
|
pub fault: Fault,
|
|
92
|
+
/// The share of calls it reaches.
|
|
62
93
|
pub fraction: Fraction,
|
|
63
|
-
/// The first
|
|
64
|
-
pub
|
|
94
|
+
/// The first instant at which this is no longer live.
|
|
95
|
+
pub expires_at: Duration,
|
|
65
96
|
}
|
|
66
97
|
|
|
67
98
|
impl Experiment {
|
|
99
|
+
/// An experiment that injects `fault` into `fraction` of calls until
|
|
100
|
+
/// `expires_at`.
|
|
68
101
|
#[must_use]
|
|
69
|
-
pub const fn new(fault: Fault, fraction: Fraction,
|
|
102
|
+
pub const fn new(fault: Fault, fraction: Fraction, expires_at: Duration) -> Self {
|
|
70
103
|
Self {
|
|
71
104
|
fault,
|
|
72
105
|
fraction,
|
|
73
|
-
|
|
106
|
+
expires_at,
|
|
74
107
|
}
|
|
75
108
|
}
|
|
76
109
|
|
|
110
|
+
/// Whether this has expired by `now`: `now >= expires_at`.
|
|
77
111
|
#[must_use]
|
|
78
|
-
pub
|
|
79
|
-
|
|
112
|
+
pub fn spent(&self, now: Duration) -> bool {
|
|
113
|
+
now >= self.expires_at
|
|
80
114
|
}
|
|
81
115
|
}
|
|
82
116
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
#![
|
|
1
|
+
#![no_std]
|
|
2
|
+
#![cfg_attr(not(test), deny(clippy::indexing_slicing))]
|
|
2
3
|
|
|
3
4
|
//! Fault injection: making a dependency fail, stall or lie on purpose.
|
|
4
5
|
//!
|
|
@@ -7,24 +8,36 @@
|
|
|
7
8
|
//!
|
|
8
9
|
//! Nothing here performs a fault. The caller raises, sleeps and substitutes,
|
|
9
10
|
//! and supplies both the clock and the draw. Disarmed is the default and the
|
|
10
|
-
//! fast path: with nothing armed
|
|
11
|
+
//! fast path: with nothing armed, [`Rogue::any_armed`] is one word compared
|
|
12
|
+
//! with zero.
|
|
13
|
+
//!
|
|
14
|
+
//! Time is [`core::time::Duration`] since an epoch the caller picks. The core
|
|
15
|
+
//! keeps no clock of its own: an expiry is judged against the instant each
|
|
16
|
+
//! call supplies, and no call records one.
|
|
17
|
+
//!
|
|
18
|
+
//! Nothing here refuses, so the crate has no `Refusal`: a [`Slot`] is the
|
|
19
|
+
//! caller's to pick, every slot can always be armed, and the answers to
|
|
20
|
+
//! [`Rogue::arm`], [`Rogue::disarm`] and [`Rogue::disarm_all`] say what the
|
|
21
|
+
//! slots held before.
|
|
11
22
|
|
|
12
23
|
/// Compiles and runs the README's examples as doctests.
|
|
13
24
|
#[cfg(doctest)]
|
|
14
25
|
#[doc = include_str!("../README.md")]
|
|
15
26
|
struct ReadmeDoctests;
|
|
16
27
|
|
|
17
|
-
mod armed;
|
|
18
28
|
pub mod experiment;
|
|
19
29
|
pub mod rogue;
|
|
30
|
+
pub mod slot;
|
|
31
|
+
|
|
32
|
+
#[cfg(test)]
|
|
33
|
+
mod test_support;
|
|
20
34
|
|
|
21
|
-
pub
|
|
22
|
-
pub use
|
|
23
|
-
pub use
|
|
24
|
-
pub use rogue::{Decision, Rogue};
|
|
35
|
+
pub use experiment::{Draw, Experiment, Fault, Fraction};
|
|
36
|
+
pub use rogue::{Arming, Decision, Disarming, Rogue};
|
|
37
|
+
pub use slot::Slot;
|
|
25
38
|
|
|
26
39
|
/// Slots a [`Rogue`] holds, which is the width of the mask tracking them.
|
|
27
40
|
pub const EXPERIMENTS: usize = u32::BITS as usize;
|
|
28
41
|
|
|
29
|
-
/// Millionths in a whole, the unit a [`Fraction`]
|
|
42
|
+
/// Millionths in a whole, the unit a [`Fraction`] and a [`Draw`] are given in.
|
|
30
43
|
pub const PER_MILLION: u32 = 1_000_000;
|