rogue_machines 0.1.2-aarch64-linux → 0.5.0-aarch64-linux

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.
@@ -4,293 +4,123 @@ require 'deadline_machines'
4
4
 
5
5
  require_relative 'rogue_machines/version'
6
6
 
7
- # Rogue, Injection, Error and Injected are defined by the native extension;
8
- # there is no pure-Ruby implementation to fall back to.
9
- require 'rogue_machines_native/rogue_machines_native'
10
-
11
7
  # Fault injection: making a dependency fail, stall or lie on purpose.
12
8
  #
13
- # Disarmed is the default. `run` costs one native call that reads one word
14
- # until something is armed.
9
+ # Two layers. `Rogue` and the Data values below mirror the core one for one:
10
+ # positional, Integer nanoseconds, symbols for unit variants. The module
11
+ # functions (`arm`, `run`, ...) are a process-wide runtime on top of one Rogue
12
+ # that names experiments, owns the clock and the random draw, and performs
13
+ # the fault.
14
+ #
15
+ # Every value validates at construction through the core: TypeError for the
16
+ # wrong type, RangeError for a value the core's constructor rejects.
15
17
  module RogueMachines
16
- # Attempts at one consistent reading before giving up on a clock that is
17
- # being replaced faster than it can be read.
18
- READINGS = 100
19
-
20
- @registry = Rogue.new
21
- @substitutes = {}.freeze
22
- @lock = Mutex.new
23
-
24
- class << self
25
- attr_writer :random
26
-
27
- # Monotonic nanoseconds. Delegates to deadline_machines rather than
28
- # memoising it, so replacing that gem's clock moves every expiry with it.
29
- def clock
30
- @clock || DeadlineMachines.clock
31
- end
32
-
33
- # Installing a clock disarms everything first. Every armed expiry was
34
- # computed against the outgoing clock, and an expiry measured against a
35
- # clock it did not come from is not an expiry.
36
- #
37
- # It also starts a generation, so a reading already taken against the
38
- # outgoing clock is refused rather than compared against expiries it has
39
- # no relation to. `sweep` is destructive, so such a reading would retire
40
- # experiments armed after the swap.
41
- def clock=(clock)
42
- @lock.synchronize do
43
- # The native call goes last here, against the rule the other two-store
44
- # writes follow, because this order is observable: `reading` is
45
- # lock-free, and bumping the generation first would pair an outgoing
46
- # reading with the incoming generation, which every consumer accepts.
47
- Thread.handle_interrupt(Object => :never) do
48
- @substitutes = {}.freeze
49
- @clock = clock
50
- registry.invalidate
51
- end
52
- end
53
- end
54
-
55
- def random
56
- @random ||= Random.new
57
- end
58
-
59
- def registry
60
- @registry
61
- end
62
-
63
- # Arms an experiment under `name`, replacing any already there.
64
- #
65
- # RogueMachines.arm(:payments, fault: :raise, share: 0.01, expires_in: 300)
66
- # RogueMachines.arm(:slow, fault: :delay, delay: 2, share: 1.0, expires_in: 60)
67
- # RogueMachines.arm(:lies, fault: :garble, garbled: [], share: 0.5, expires_in: 60)
68
- #
69
- # `expires_in` is required and counts from now, so no experiment outlives
70
- # the person who armed it.
71
- def arm(name, fault:, share:, expires_in:, delay: nil, garbled: nil)
72
- key = named(name)
73
-
74
- # The reading is taken before the lock so that a caller's clock never
75
- # runs inside it, which means the expiry is measured from slightly
76
- # before the arming. See "The clock is deadline_machines'" in the README.
77
- pair = reading
78
- unless pair
79
- raise Error, "the clock was replaced #{READINGS} times while taking one reading; " \
80
- 'nothing is armed'
81
- end
82
-
83
- now, age = pair
84
- options = { fault: fault.to_s, share: Float(share), expires_in: Float(expires_in),
85
- now_ns: now, generation: age }
86
- options[:delay] = Float(delay) if delay
87
-
88
- @lock.synchronize do
89
- # Two stores. An interrupt landing between them would leave an armed
90
- # garble with nothing to return for the rest of its life, and nothing
91
- # repairs that, because publishing only ever removes keys.
92
- Thread.handle_interrupt(Object => :never) do
93
- registry.arm(key, **options)
94
- payload = options[:fault] == 'garble' ? garbled : nil
95
- publish(substitutes.merge(key => payload), registry.armed(now, age))
96
- end
97
- end
98
- name
99
- end
100
-
101
- # Reports whether `name` was still running, which an experiment that
102
- # expired on its own is not.
103
- #
104
- # The substitute goes first: a call landing between the two sees a garble
105
- # with nothing to return rather than the real value it was armed against.
106
- def disarm(name)
107
- key = named(name)
108
- pair = reading
109
- return false unless pair
110
-
111
- now, age = pair
112
- @lock.synchronize do
113
- # A reading from a replaced clock must not drop a substitute belonging
114
- # to the generation that replaced it.
115
- break false unless registry.generation == age
116
-
117
- # The native call validates its reading and can raise, so it goes
118
- # first: nothing is mutated unless it succeeds. Ordering no longer
119
- # matters to a reader, since a decision and its payload are read
120
- # under this same lock.
121
- Thread.handle_interrupt(Object => :never) do
122
- was_running = registry.disarm(key, now, age)
123
- @substitutes = substitutes.except(key).freeze
124
- was_running
125
- end
126
- end
127
- end
128
-
129
- def disarm_all
130
- @lock.synchronize do
131
- Thread.handle_interrupt(Object => :never) do
132
- registry.disarm_all
133
- @substitutes = {}.freeze
134
- end
135
- end
136
- end
137
-
138
- # With no name, whether anything at all is armed. Asked through the name
139
- # list rather than the gate `run` reads, because the gate only drops an
140
- # expired experiment when something next decides.
141
- def armed?(name = nil)
142
- names = running
143
- return names.any? if name.nil?
144
-
145
- names.include?(named(name))
18
+ # The core's `Slot`: an index below EXPERIMENTS, picked by the caller.
19
+ # Not an issued id: any in-range index names a slot.
20
+ Slot = Data.define(:index) do
21
+ def initialize(index:)
22
+ super(index: RogueMachines.__send__(:__slot, index))
146
23
  end
24
+ end
147
25
 
148
- # The experiments still running, which excludes any that has expired.
149
- def armed
150
- running.map(&:to_sym)
151
- end
152
-
153
- # Runs the block, or the fault armed under `name` instead.
154
- def run(name)
155
- check(name)
156
- unless registry.any_armed?
157
- release unless substitutes.empty?
158
- return yield
159
- end
160
-
161
- key = named(name)
162
- pair = reading
163
- return yield unless pair
164
-
165
- now, age = pair
166
- drawn = random.rand(PER_MILLION)
167
-
168
- # The decision and the payload live in different stores, so reading them
169
- # one after the other is wrong in both orders: a name published between
170
- # the two is in the decision and not the table, and one retired between
171
- # them is in the table and not the decision. They are read together.
172
- # Nothing caller-supplied runs inside the lock, since the clock and the
173
- # draw are already taken.
174
- injection, payload = @lock.synchronize do
175
- found = registry.decide(key, now, drawn, age)
176
- [found, found && substitutes[key]]
177
- end
178
- reconcile(now, age)
179
- return yield unless injection
180
-
181
- case injection.fault
182
- when :raise then raise Injected, "#{name} is armed to raise"
183
- when :garble then payload
184
- when :delay
185
- stall(injection.delay_ns)
186
- yield
187
- end
26
+ # The core's `Fraction`: the share of calls a fault reaches, in millionths,
27
+ # 0..PER_MILLION.
28
+ Fraction = Data.define(:millionths) do
29
+ def initialize(millionths:)
30
+ super(millionths: RogueMachines.__send__(:__fraction, millionths))
188
31
  end
189
32
 
190
- private
33
+ # The core's `Fraction::covers`: whether `draw` (a Draw) falls in this
34
+ # share, which is half open at its own boundary.
35
+ def covers?(draw) = RogueMachines.__send__(:__covers, self, draw)
36
+ end
191
37
 
192
- def substitutes
193
- @substitutes
38
+ # The core's `Draw`: one call's random draw, in millionths, below
39
+ # PER_MILLION.
40
+ Draw = Data.define(:millionths) do
41
+ def initialize(millionths:)
42
+ super(millionths: RogueMachines.__send__(:__draw, millionths))
194
43
  end
44
+ end
195
45
 
196
- # A reading and the generation it belongs to.
197
- #
198
- # The generation is read on both sides of the reading. Reading it only
199
- # afterwards would adopt the new one and hand a stale reading to a
200
- # destructive sweep; reading it only before would pair an outgoing
201
- # reading with the incoming generation, which every consumer accepts. The
202
- # pair cannot be sampled in one step, so a change means retry.
203
- def reading
204
- READINGS.times do
205
- age = registry.generation
206
- now = clock.now_ns
207
- unless now.is_a?(Integer) && !now.negative?
208
- raise Error, "#{clock.class} returned #{now.inspect}; a reading must be a " \
209
- 'non-negative Integer of nanoseconds'
210
- end
211
- return [now, age] if registry.generation == age
212
- end
213
- nil
46
+ # The core's `Fault::Delay`: wait `duration` Integer nanoseconds, then make
47
+ # the real call. The other faults are the symbols :raise and :garble.
48
+ Delay = Data.define(:duration) do
49
+ def initialize(duration:)
50
+ super(duration: RogueMachines.__send__(:__delay, duration))
214
51
  end
52
+ end
215
53
 
216
- # Rejects a name the native table could not key on, without converting it,
217
- # so a call site answers the same way whether or not anything is armed.
218
- def check(name)
219
- case name
220
- when Symbol then legible(name.name)
221
- when String then legible(name)
222
- else raise ArgumentError, "an experiment name must be a Symbol or a String, got #{name.class}"
223
- end
54
+ # The core's `Experiment`: a fault (:raise, :garble or a Delay), the
55
+ # Fraction of calls it reaches, and the Integer nanosecond instant it
56
+ # expires at.
57
+ Experiment = Data.define(:fault, :fraction, :expires_at) do
58
+ def initialize(fault:, fraction:, expires_at:)
59
+ fault, fraction, expires_at = RogueMachines.__send__(:__experiment, fault, fraction, expires_at)
60
+ super
224
61
  end
225
62
 
226
- def legible(name)
227
- raise ArgumentError, 'an experiment needs a name' if name.empty?
228
- return if name.ascii_only?
229
- return if name.encoding == Encoding::UTF_8 && name.valid_encoding?
230
- raise ArgumentError, "an experiment name must be valid UTF-8, got #{name.encoding}" \
231
- unless name.encoding == Encoding::UTF_8
232
-
233
- raise ArgumentError, 'an experiment name holds bytes that are not valid UTF-8'
234
- end
63
+ # The core's `Experiment::spent`: whether this has expired by `now`
64
+ # Integer nanoseconds, `now >= expires_at`.
65
+ def spent?(now) = RogueMachines.__send__(:__spent, self, now)
66
+ end
235
67
 
236
- # The checked name, as something the native table can key on. Not `to_s`:
237
- # a String subclass can override it, which armed one name and reported
238
- # another.
239
- def named(name)
240
- check(name)
241
- name.is_a?(Symbol) ? name.name : name
68
+ # The core's `Decision::Inject`: do `fault` instead of the real call.
69
+ Inject = Data.define(:fault) do
70
+ def initialize(fault:)
71
+ super(fault: RogueMachines.__send__(:__fault, fault))
242
72
  end
73
+ end
243
74
 
244
- # The names still running, releasing the substitutes of everything that is
245
- # not.
246
- def running
247
- pair = reading
248
- return [] unless pair
249
-
250
- now, age = pair
251
- @lock.synchronize do
252
- # A reading from a replaced clock lists nothing, and must not be used
253
- # to prune: publishing against an empty list would wipe the table.
254
- break [] unless registry.generation == age
255
-
256
- names = registry.armed(now, age)
257
- publish(substitutes, names)
258
- names
259
- end
75
+ # The core's `Arming::Replaced`: the experiment the slot held before, and
76
+ # the `garbled:` substitute the runtime held with it (any object, or nil;
77
+ # always nil from the core mirror `Rogue`, which keeps no substitutes).
78
+ Replaced = Data.define(:experiment, :garbled) do
79
+ def initialize(experiment:, garbled:)
80
+ super(experiment: RogueMachines.__send__(:__held, experiment), garbled:)
260
81
  end
82
+ end
261
83
 
262
- # Releases the substitutes of anything no longer running, so one armed
263
- # experiment holding the gate open cannot keep another's substitute, and
264
- # the caller's object, alive until the next `arm`.
265
- def reconcile(now_ns, age)
266
- return if substitutes.size <= registry.armed_count(now_ns, age)
267
-
268
- @lock.synchronize do
269
- publish(substitutes, registry.armed(now_ns, age)) if registry.generation == age
270
- end
84
+ # The core's `Disarming::Disarmed`: the experiment the slot held, and the
85
+ # `garbled:` substitute the runtime removed with it (any object, or nil;
86
+ # always nil from the core mirror `Rogue`, which keeps no substitutes).
87
+ Disarmed = Data.define(:experiment, :garbled) do
88
+ def initialize(experiment:, garbled:)
89
+ super(experiment: RogueMachines.__send__(:__held, experiment), garbled:)
271
90
  end
91
+ end
92
+ end
272
93
 
273
- # Only if nothing is armed once the lock is held. The gate `run` reads is
274
- # sampled before it, so an `arm` landing in between would otherwise have
275
- # its just-published substitute discarded, leaving an armed garble with
276
- # nothing to return for the rest of its life.
277
- def release
278
- @lock.synchronize { @substitutes = {}.freeze unless registry.any_armed? }
279
- end
94
+ # Native is required: there is no pure-Ruby fallback. It validates and builds
95
+ # the Data values above, so they must exist first.
96
+ require 'rogue_machines_native/rogue_machines_native'
280
97
 
281
- # Replaces the table with the entries `names` still covers, in one
282
- # assignment.
283
- def publish(table, names)
284
- @substitutes = table.slice(*names).freeze
285
- end
98
+ module RogueMachines
99
+ private_class_method :__slot, :__fraction, :__draw, :__delay, :__experiment, :__fault, :__delay_of, :__held,
100
+ :__spent, :__covers
101
+
102
+ # The core's `Fraction::NONE` and `Fraction::ALL`.
103
+ Fraction::NONE = Fraction.new(0)
104
+ Fraction::ALL = Fraction.new(PER_MILLION)
105
+
106
+ # The values are final. `initialize` is where each validates, and a subclass
107
+ # could override it, or `allocate` build an instance that never ran it:
108
+ # subclassing raises TypeError (as subclassing Class does) and `allocate`
109
+ # is gone. `new`, keywords, `with` and `dup` all still run the check.
110
+ [Slot, Fraction, Draw, Delay, Experiment, Inject, Replaced, Disarmed].each do |value|
111
+ value.singleton_class.undef_method(:allocate)
112
+ value.define_singleton_method(:inherited) { |_| raise TypeError, "#{value} cannot be subclassed" }
113
+ end
114
+ # The native Rogue has no allocator; it is final too.
115
+ Rogue.define_singleton_method(:inherited) { |_| raise TypeError, "#{Rogue} cannot be subclassed" }
286
116
 
287
- # Spends the delay against the caller's deadline rather than past it, so a
288
- # slow dependency trips the timeout that was already there.
289
- def stall(delay_ns)
290
- DeadlineMachines.check!
291
- seconds = DeadlineMachines.clamp_sleep(delay_ns.fdiv(NS_PER_SECOND))
292
- sleep(seconds) if seconds.positive?
293
- DeadlineMachines.check!
117
+ class Rogue
118
+ # A copy, frozen when this is unless `freeze:` says otherwise.
119
+ def clone(freeze: nil)
120
+ copy = dup
121
+ (freeze.nil? ? frozen? : freeze) ? copy.freeze : copy
294
122
  end
295
123
  end
296
124
  end
125
+
126
+ require_relative 'rogue_machines/runtime'