handoff_machines 0.8.3
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 +7 -0
- data/CHANGELOG.md +113 -0
- data/Cargo.lock +331 -0
- data/Cargo.toml +30 -0
- data/LICENSE.txt +21 -0
- data/README.md +383 -0
- data/ext/handoff_machines_native/core/CHANGELOG.md +81 -0
- data/ext/handoff_machines_native/core/Cargo.toml +22 -0
- data/ext/handoff_machines_native/core/README.md +215 -0
- data/ext/handoff_machines_native/core/src/iter.rs +130 -0
- data/ext/handoff_machines_native/core/src/lib.rs +498 -0
- data/ext/handoff_machines_native/core/src/refusal.rs +83 -0
- data/ext/handoff_machines_native/core/src/transfer.rs +437 -0
- data/ext/handoff_machines_native/core/src/types.rs +440 -0
- data/ext/handoff_machines_native/extconf.rb +2 -0
- data/ext/handoff_machines_native/ffi/Cargo.toml +21 -0
- data/ext/handoff_machines_native/ffi/extconf.rb +14 -0
- data/ext/handoff_machines_native/ffi/src/classes.rs +50 -0
- data/ext/handoff_machines_native/ffi/src/handoff.rs +375 -0
- data/ext/handoff_machines_native/ffi/src/init.rs +62 -0
- data/ext/handoff_machines_native/ffi/src/lib.rs +36 -0
- data/ext/handoff_machines_native/ffi/src/names.rs +86 -0
- data/ext/handoff_machines_native/ffi/src/values.rs +211 -0
- data/lib/handoff_machines/version.rb +4 -0
- data/lib/handoff_machines.rb +263 -0
- data/sig/handoff_machines.rbs +291 -0
- metadata +93 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
//! Private hooks the Ruby `Data` values validate themselves with, each
|
|
2
|
+
//! through the core constructor of the type it mirrors, and the readers for
|
|
3
|
+
//! every value the binding takes from Ruby. `TypeError` for a value of the
|
|
4
|
+
//! wrong type (a subclass of one of this gem's classes included), `RangeError`
|
|
5
|
+
//! for one the core would not build.
|
|
6
|
+
use handoff_machines::{
|
|
7
|
+
Acked, Action, FencingToken, Limits, Order, Phase, Recipient, Status, Ticket,
|
|
8
|
+
};
|
|
9
|
+
use magnus::{Error, Ruby, Value, prelude::*};
|
|
10
|
+
use matryona_machines::ruby::{
|
|
11
|
+
Choice, Refused, Symbols, duration, exact_fields, fencing, instant, integer, optional,
|
|
12
|
+
symbol_or_fields_why,
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
use crate::classes::classes;
|
|
16
|
+
use crate::names::{Actions, Phases, Recipients};
|
|
17
|
+
|
|
18
|
+
fn range(ruby: &Ruby, message: &'static str) -> Error {
|
|
19
|
+
Error::new(ruby.exception_range_error(), message)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/// A phase from its symbol: `TypeError` for a non-Symbol, `RangeError` for a
|
|
23
|
+
/// Symbol that names no phase.
|
|
24
|
+
pub fn phase(ruby: &Ruby, value: Value) -> Result<Phase, Error> {
|
|
25
|
+
Phases::parse(ruby, value)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/// The core's `Limits::new`, its two fields converted in order.
|
|
29
|
+
fn limits_of(ruby: &Ruby, phase_budget: Value, per_replica: Value) -> Result<Limits, Error> {
|
|
30
|
+
let phase_budget = duration(phase_budget, "phase_budget")?;
|
|
31
|
+
Limits::new(phase_budget, integer(per_replica, "per_replica")?)
|
|
32
|
+
.ok_or_else(|| range(ruby, "per_replica must be at least 1"))
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
matryona_machines::fencing_family! {
|
|
36
|
+
/// The handoff core's fencing token (lease-machines' type), for
|
|
37
|
+
/// `matryona_machines::ruby::fencing`.
|
|
38
|
+
pub Family in classes {
|
|
39
|
+
token: FencingToken,
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/// A ticket: only this gem's `Ticket`, through `Ticket::new`.
|
|
44
|
+
pub fn ticket(ruby: &Ruby, value: Value) -> Result<Ticket, Error> {
|
|
45
|
+
let class = classes(ruby)?.ticket.get(ruby);
|
|
46
|
+
let message = "expected a HandoffMachines::Ticket";
|
|
47
|
+
let [partition, generation] = exact_fields::<2>(ruby, value, class, message)?;
|
|
48
|
+
crate::ticket(ruby, partition, generation)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/// Limits: only this gem's `Limits`, through `Limits::new`.
|
|
52
|
+
pub fn limits(ruby: &Ruby, value: Value) -> Result<Limits, Error> {
|
|
53
|
+
let class = classes(ruby)?.limits.get(ruby);
|
|
54
|
+
let message = "expected a HandoffMachines::Limits";
|
|
55
|
+
let [phase_budget, per_replica] = exact_fields::<2>(ruby, value, class, message)?;
|
|
56
|
+
limits_of(ruby, phase_budget, per_replica)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/// `Limits.new`.
|
|
60
|
+
pub fn check_limits(ruby: &Ruby, phase_budget: Value, per_replica: Value) -> Result<(), Error> {
|
|
61
|
+
limits_of(ruby, phase_budget, per_replica).map(drop)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/// `Ticket.new`: the core's `Ticket::new`.
|
|
65
|
+
pub fn check_ticket(ruby: &Ruby, partition: Value, generation: Value) -> Result<(), Error> {
|
|
66
|
+
crate::ticket(ruby, partition, generation).map(drop)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/// The acknowledgement `build` makes of `value`'s phase, or the `RangeError`
|
|
70
|
+
/// its `None` stands for.
|
|
71
|
+
fn acked(ruby: &Ruby, value: Value, build: fn(Phase) -> Option<Acked>) -> Result<(), Error> {
|
|
72
|
+
build(phase(ruby, value)?)
|
|
73
|
+
.map(drop)
|
|
74
|
+
.ok_or_else(|| range(ruby, "no acknowledgement answers this phase"))
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/// `Recorded.new`: the core's `Acked::recorded`.
|
|
78
|
+
pub fn check_recorded(ruby: &Ruby, value: Value) -> Result<(), Error> {
|
|
79
|
+
acked(ruby, value, Acked::recorded)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/// `Duplicate.new`: the core's `Acked::duplicate`.
|
|
83
|
+
pub fn check_duplicate(ruby: &Ruby, value: Value) -> Result<(), Error> {
|
|
84
|
+
acked(ruby, value, Acked::duplicate)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/// `Replica.new`: any unsigned 64-bit Integer, as the core's field is.
|
|
88
|
+
pub fn check_replica(id: Value) -> Result<(), Error> {
|
|
89
|
+
integer::<u64>(id, "id").map(drop)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/// `Drain.new`: any unsigned 64-bit Integer, as the core's field is.
|
|
93
|
+
pub fn check_drain(until: Value) -> Result<(), Error> {
|
|
94
|
+
integer::<u64>(until, "until").map(drop)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/// `Capture.new`: any unsigned 64-bit Integer, as the core's field is.
|
|
98
|
+
pub fn check_capture(at: Value) -> Result<(), Error> {
|
|
99
|
+
integer::<u64>(at, "at").map(drop)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/// `Install.new`: any unsigned 64-bit Integer, as the core's field is.
|
|
103
|
+
pub fn check_install(checkpoint: Value) -> Result<(), Error> {
|
|
104
|
+
integer::<u64>(checkpoint, "checkpoint").map(drop)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/// The core's `Recipient`: `:authority`, or exactly this gem's `Replica`.
|
|
108
|
+
fn recipient(ruby: &Ruby, value: Value) -> Result<Recipient, Error> {
|
|
109
|
+
let replica = [classes(ruby)?.replica.get(ruby)];
|
|
110
|
+
let message = |why: Refused<'_>| match why {
|
|
111
|
+
Refused::UnknownSymbol(symbol) => format!("unknown recipient {}", symbol.inspect()),
|
|
112
|
+
_ => "a recipient is :authority or a HandoffMachines::Replica".into(),
|
|
113
|
+
};
|
|
114
|
+
Ok(
|
|
115
|
+
match symbol_or_fields_why(ruby, value, Recipients, &replica, message)? {
|
|
116
|
+
Choice::Symbol(recipient) => recipient,
|
|
117
|
+
Choice::Struct(_, [id]) => Recipient::Replica(integer(id, "id")?),
|
|
118
|
+
},
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/// The core's `Action` from a Symbol (`:quiesce` or `:discard`, the two that
|
|
123
|
+
/// carry no field) or exactly one of this gem's action `Data` values.
|
|
124
|
+
fn action(ruby: &Ruby, value: Value) -> Result<Action, Error> {
|
|
125
|
+
let actions = classes(ruby)?.actions.map(|class| class.get(ruby));
|
|
126
|
+
let message = |why: Refused<'_>| match why {
|
|
127
|
+
Refused::UnknownSymbol(symbol) => format!("unknown action {}", symbol.inspect()),
|
|
128
|
+
_ => "an action is :quiesce, :discard or an action Data (exactly its class)".into(),
|
|
129
|
+
};
|
|
130
|
+
let (index, field) = match symbol_or_fields_why(ruby, value, Actions, &actions, message)? {
|
|
131
|
+
Choice::Symbol(action) => return Ok(action),
|
|
132
|
+
Choice::Struct(index, [field]) => (index, field),
|
|
133
|
+
};
|
|
134
|
+
Ok(match index {
|
|
135
|
+
0 => Action::Drain {
|
|
136
|
+
until: integer(field, "until")?,
|
|
137
|
+
},
|
|
138
|
+
1 => Action::Capture {
|
|
139
|
+
at: integer(field, "at")?,
|
|
140
|
+
},
|
|
141
|
+
2 => Action::Install {
|
|
142
|
+
checkpoint: integer(field, "checkpoint")?,
|
|
143
|
+
},
|
|
144
|
+
3 => Action::Fence {
|
|
145
|
+
above: fencing::token::<Family>(ruby, field)?,
|
|
146
|
+
},
|
|
147
|
+
4 => Action::Activate {
|
|
148
|
+
token: fencing::token::<Family>(ruby, field)?,
|
|
149
|
+
},
|
|
150
|
+
_resume => Action::Resume {
|
|
151
|
+
token: if field.is_nil() {
|
|
152
|
+
None
|
|
153
|
+
} else {
|
|
154
|
+
Some(fencing::token::<Family>(ruby, field)?)
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/// `Order.new`: the core's `Order::new`.
|
|
161
|
+
pub fn check_order(ruby: &Ruby, held: Value, to: Value, action: Value) -> Result<(), Error> {
|
|
162
|
+
let held = ticket(ruby, held)?;
|
|
163
|
+
let (to, action) = (recipient(ruby, to)?, self::action(ruby, action)?);
|
|
164
|
+
Order::new(held, to, action)
|
|
165
|
+
.map(drop)
|
|
166
|
+
.ok_or_else(|| range(ruby, "only the authority fences, and it does nothing else"))
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/// `Status.new`: the core's `Status::new`. Answers the token as this gem's
|
|
170
|
+
/// own (or nil), to keep in place of the one given.
|
|
171
|
+
#[allow(clippy::too_many_arguments)] // One per field of the core's `Status`.
|
|
172
|
+
pub fn check_status(
|
|
173
|
+
ruby: &Ruby,
|
|
174
|
+
held: Value,
|
|
175
|
+
source: Value,
|
|
176
|
+
target: Value,
|
|
177
|
+
phase: Value,
|
|
178
|
+
base: Value,
|
|
179
|
+
until: Value,
|
|
180
|
+
checkpoint: Value,
|
|
181
|
+
fence: Option<Value>,
|
|
182
|
+
deadline: Value,
|
|
183
|
+
) -> Result<Option<Value>, Error> {
|
|
184
|
+
let held = ticket(ruby, held)?;
|
|
185
|
+
let (source, target) = (integer(source, "source")?, integer(target, "target")?);
|
|
186
|
+
let base = integer(base, "base")?;
|
|
187
|
+
let (until, checkpoint) = (
|
|
188
|
+
optional(until, "until")?,
|
|
189
|
+
optional(checkpoint, "checkpoint")?,
|
|
190
|
+
);
|
|
191
|
+
let fence = fence
|
|
192
|
+
.map(|fence| fencing::token::<Family>(ruby, fence))
|
|
193
|
+
.transpose()?;
|
|
194
|
+
let status = Status::new(
|
|
195
|
+
held,
|
|
196
|
+
source,
|
|
197
|
+
target,
|
|
198
|
+
self::phase(ruby, phase)?,
|
|
199
|
+
base,
|
|
200
|
+
until,
|
|
201
|
+
checkpoint,
|
|
202
|
+
fence,
|
|
203
|
+
// Up to `Duration::MAX`: the core saturates `now + phase_budget`
|
|
204
|
+
// past what a `now` can be.
|
|
205
|
+
instant(deadline, "deadline")?,
|
|
206
|
+
);
|
|
207
|
+
status.ok_or_else(|| range(ruby, "no live transfer reports this status"))?;
|
|
208
|
+
fence
|
|
209
|
+
.map(|fence| fencing::token_object::<Family>(ruby, fence))
|
|
210
|
+
.transpose()
|
|
211
|
+
}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'handoff_machines/version'
|
|
4
|
+
|
|
5
|
+
# Every value mirrors the core type of the same name, field for field. Every
|
|
6
|
+
# instant and span (`now`, `phase_budget`, `deadline`) is Integer nanoseconds.
|
|
7
|
+
#
|
|
8
|
+
# The native side is the authority on every value it reads: one of this gem's
|
|
9
|
+
# classes must be exactly that class (a subclass is a TypeError, whatever it
|
|
10
|
+
# holds), and every field is validated again through the core constructor, so
|
|
11
|
+
# a value forged past `initialize` (`Marshal.load`, a bound
|
|
12
|
+
# `Data#initialize`) raises TypeError or RangeError and never reaches the core.
|
|
13
|
+
module HandoffMachines
|
|
14
|
+
|
|
15
|
+
# A fencing token, the lease core's `FencingToken` which the handoff core
|
|
16
|
+
# re-exports: the issuing authority's scope in the top 16 bits of `raw`
|
|
17
|
+
# above a sequence number counted from 1. The coordinator issues none: bring
|
|
18
|
+
# them from your authority. Every method that takes a token reads its `raw`,
|
|
19
|
+
# so the `lease_machines` gem's FencingToken fits as it is, and this gem does
|
|
20
|
+
# not depend on that one; a value that keeps a token (Fence, Activate,
|
|
21
|
+
# Resume, Status) converts it to this class. A bare Integer is not a token:
|
|
22
|
+
# TypeError.
|
|
23
|
+
#
|
|
24
|
+
# Tokens of one scope are ordered; tokens of two scopes are not, so there is
|
|
25
|
+
# no `<` or `<=>`. Compare with `order` and `newer_than?`.
|
|
26
|
+
FencingToken = Data.define(:raw) do
|
|
27
|
+
# The core's `FencingToken::from_raw`. RangeError when the sequence number
|
|
28
|
+
# is zero, which no authority issues.
|
|
29
|
+
def self.from_raw(raw) = new(raw)
|
|
30
|
+
|
|
31
|
+
def initialize(raw:)
|
|
32
|
+
HandoffMachines.__send__(:native_raw, raw)
|
|
33
|
+
super
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# The scope of the authority that issued this token.
|
|
37
|
+
def scope = HandoffMachines.__send__(:native_token_scope, self)
|
|
38
|
+
|
|
39
|
+
# -1, 0 or 1 against a token of the same scope; nil across scopes.
|
|
40
|
+
def order(other) = HandoffMachines.__send__(:native_token_order, self, other)
|
|
41
|
+
|
|
42
|
+
# Whether this token is of `other`'s scope and strictly newer. False across
|
|
43
|
+
# scopes, so its negation does not mean "older or equal".
|
|
44
|
+
def newer_than?(other) = HandoffMachines.__send__(:native_token_newer, self, other)
|
|
45
|
+
end
|
|
46
|
+
# Every value below is valid or does not exist: it is checked at
|
|
47
|
+
# construction through the core constructor of the type it mirrors.
|
|
48
|
+
# TypeError for a value of the wrong type, RangeError for one the core would
|
|
49
|
+
# not build. A value built by hand is held to the same rule as one returned.
|
|
50
|
+
|
|
51
|
+
# Admission and timing limits: how long one phase may last before `overdue`
|
|
52
|
+
# reports the transfer, and how many transfers one replica may take part in.
|
|
53
|
+
# The core's `Limits::new`: `per_replica` is at least 1.
|
|
54
|
+
Limits = Data.define(:phase_budget, :per_replica) do
|
|
55
|
+
def initialize(phase_budget:, per_replica:)
|
|
56
|
+
HandoffMachines.__send__(:__limits, phase_budget, per_replica)
|
|
57
|
+
super
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# One transfer: the partition and the generation it was issued under.
|
|
62
|
+
# Plain data, so it can travel to a replica and come back in its reply. The
|
|
63
|
+
# generation carries the scope of the coordinator that issued it; a
|
|
64
|
+
# coordinator of another scope answers :unknown to the ticket. The core's
|
|
65
|
+
# `Ticket::new`: a generation with a zero sequence, which no coordinator
|
|
66
|
+
# issues, raises RangeError.
|
|
67
|
+
Ticket = Data.define(:partition, :generation) do
|
|
68
|
+
def initialize(partition:, generation:)
|
|
69
|
+
HandoffMachines.__send__(:__ticket, partition, generation)
|
|
70
|
+
super
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# The scope of the coordinator that issued this ticket.
|
|
74
|
+
def scope = Handoff.__send__(:native_scope_of, self)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Honoured acknowledgement, recorded for the first time; the transfer now
|
|
78
|
+
# stands at `phase`. The core's `Acked::recorded`: never :quiesce.
|
|
79
|
+
Recorded = Data.define(:phase) do
|
|
80
|
+
def initialize(phase:)
|
|
81
|
+
HandoffMachines.__send__(:__recorded, phase)
|
|
82
|
+
super
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def duplicate? = false
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Honoured idempotent repeat: nothing changed, and the transfer still stands
|
|
89
|
+
# at `phase`, which may be later than the one the acknowledgement first led
|
|
90
|
+
# to. The core's `Acked::duplicate`: never :quiesce, :complete or :aborted.
|
|
91
|
+
Duplicate = Data.define(:phase) do
|
|
92
|
+
def initialize(phase:)
|
|
93
|
+
HandoffMachines.__send__(:__duplicate, phase)
|
|
94
|
+
super
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def duplicate? = true
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Order#to for a replica, by its caller-owned id. The lease authority is
|
|
101
|
+
# the symbol :authority.
|
|
102
|
+
Replica = Data.define(:id) do
|
|
103
|
+
def initialize(id:)
|
|
104
|
+
HandoffMachines.__send__(:__replica, id)
|
|
105
|
+
super
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Order#action, one Data per core `Action` variant that carries a field.
|
|
110
|
+
# The two that carry none are the symbols :quiesce and :discard, and only
|
|
111
|
+
# those two are Symbols: a bare :resume is not `Resume.new(nil)`. Offsets
|
|
112
|
+
# and checkpoint ids are Integers in 0...2**64; tokens are converted to
|
|
113
|
+
# this gem's FencingToken.
|
|
114
|
+
Drain = Data.define(:until) do
|
|
115
|
+
def initialize(**field)
|
|
116
|
+
HandoffMachines.__send__(:__drain, field[:until]) if field.key?(:until)
|
|
117
|
+
super
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
Capture = Data.define(:at) do
|
|
121
|
+
def initialize(at:)
|
|
122
|
+
HandoffMachines.__send__(:__capture, at)
|
|
123
|
+
super
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
Install = Data.define(:checkpoint) do
|
|
127
|
+
def initialize(checkpoint:)
|
|
128
|
+
HandoffMachines.__send__(:__install, checkpoint)
|
|
129
|
+
super
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
Fence = Data.define(:above) do
|
|
133
|
+
def initialize(above:) = super(above: HandoffMachines.__send__(:__token, above))
|
|
134
|
+
end
|
|
135
|
+
Activate = Data.define(:token) do
|
|
136
|
+
def initialize(token:) = super(token: HandoffMachines.__send__(:__token, token))
|
|
137
|
+
end
|
|
138
|
+
# `token` is nil when the rollback was not fenced; anything else is checked
|
|
139
|
+
# as a token, so `false` is a TypeError.
|
|
140
|
+
Resume = Data.define(:token) do
|
|
141
|
+
def initialize(token:) = super(token: token.nil? ? nil : HandoffMachines.__send__(:__token, token))
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# One outstanding instruction: who must act (`to`, a Replica or
|
|
145
|
+
# :authority) and what they must do. The core's `Order::new`: only the
|
|
146
|
+
# authority is asked to fence, and it is asked nothing else.
|
|
147
|
+
Order = Data.define(:ticket, :to, :action) do
|
|
148
|
+
def initialize(ticket:, to:, action:)
|
|
149
|
+
HandoffMachines.__send__(:__order, ticket, to, action)
|
|
150
|
+
super
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# A read-only view of one active transfer. The core's `Status::checked`:
|
|
155
|
+
# a ticket some coordinator issues, two different replicas, a phase that is
|
|
156
|
+
# not final, and only what that phase has recorded.
|
|
157
|
+
Status = Data.define(
|
|
158
|
+
:ticket, :source, :target, :phase, :base, :until, :checkpoint, :token, :deadline
|
|
159
|
+
) do
|
|
160
|
+
# `until` is a keyword, so the fields arrive as a Hash. A missing or
|
|
161
|
+
# unknown one is Data's own ArgumentError.
|
|
162
|
+
def initialize(**fields)
|
|
163
|
+
return super unless fields.keys.sort == members.sort
|
|
164
|
+
|
|
165
|
+
super(**fields.merge(token: HandoffMachines.__send__(:__status, *fields.values_at(*members))))
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# Sealed: no subclass can skip the checks above by overriding `initialize`,
|
|
170
|
+
# and no bare `allocate` builds a value that never ran them. Ruby still
|
|
171
|
+
# binds a subclass constant before `inherited` raises; the native side
|
|
172
|
+
# refuses its instances.
|
|
173
|
+
[FencingToken, Limits, Ticket, Recorded, Duplicate, Replica, Drain, Capture, Install, Fence, Activate, Resume,
|
|
174
|
+
Order, Status].each do |value|
|
|
175
|
+
value.singleton_class.undef_method(:allocate)
|
|
176
|
+
value.define_singleton_method(:inherited) { |_| raise TypeError, "#{value} cannot be subclassed" }
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
# The action Data a native row names, by the action's name.
|
|
180
|
+
ACTIONS = { drain: Drain, capture: Capture, install: Install, fence: Fence, activate: Activate,
|
|
181
|
+
resume: Resume }.freeze
|
|
182
|
+
# The actions whose field is a fencing token.
|
|
183
|
+
TOKENED = %i[fence activate resume].freeze
|
|
184
|
+
private_constant :ACTIONS, :TOKENED
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# Native is required: no algorithm fallback. It resolves the classes above
|
|
188
|
+
# once, here.
|
|
189
|
+
require 'handoff_machines_native/handoff_machines_native'
|
|
190
|
+
|
|
191
|
+
module HandoffMachines
|
|
192
|
+
class << self
|
|
193
|
+
private :native_raw, :native_token_scope, :native_token_order, :native_token_newer,
|
|
194
|
+
:__limits, :__ticket, :__recorded, :__duplicate, :__replica, :__drain, :__capture, :__install,
|
|
195
|
+
:__token, :__order, :__status
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# The core's `Handoff`. Every mutating call (start and each
|
|
199
|
+
# acknowledgement) reads its arguments before the core is asked, decides on
|
|
200
|
+
# a copy, builds the answer (a Ticket, a Recorded or Duplicate: their `new`
|
|
201
|
+
# runs), then keeps the copy only if this coordinator is still unfrozen
|
|
202
|
+
# (FrozenError) and unchanged (RuntimeError: a re-entrant call changed it).
|
|
203
|
+
# Either way, a raise means nothing was applied. A refusal is a Symbol.
|
|
204
|
+
class Handoff
|
|
205
|
+
class << self
|
|
206
|
+
# A fresh generation namespace in scope zero.
|
|
207
|
+
def new(limits) = with_scope(limits, 0)
|
|
208
|
+
|
|
209
|
+
# A fresh generation namespace whose generations carry `scope`, an
|
|
210
|
+
# Integer in 0..65_535. Give each coordinator its own.
|
|
211
|
+
def with_scope(limits, scope) = native_with_scope(limits, scope)
|
|
212
|
+
|
|
213
|
+
# Generations of `scope` above `last_generation`: what #generation
|
|
214
|
+
# answered on the previous coordinator of this namespace, nil when it had
|
|
215
|
+
# issued nothing. RangeError when `last_generation` is not a generation
|
|
216
|
+
# of `scope`: another scope's, one that lost its top bits, or a bare 0
|
|
217
|
+
# (no generation in any scope).
|
|
218
|
+
def restored(limits, scope, last_generation) = native_restored(limits, scope, last_generation)
|
|
219
|
+
|
|
220
|
+
private :native_with_scope, :native_restored, :native_scope_of
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def limits
|
|
224
|
+
Limits.new(*native_limits)
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def status(partition)
|
|
228
|
+
head, tail = native_status(partition)
|
|
229
|
+
return unless head
|
|
230
|
+
|
|
231
|
+
granted = tail[3] && FencingToken.new(tail[3])
|
|
232
|
+
Status.new(Ticket.new(head[0], head[1]), *head[2..], *tail[0..2], granted, tail[4])
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
# At most `limit` orders from position `from` (zero to begin), and the
|
|
236
|
+
# position to resume at.
|
|
237
|
+
def orders(from, limit)
|
|
238
|
+
rows, position = native_orders(from, limit)
|
|
239
|
+
[rows.map { |row| order(*row) }, position]
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# At most `limit` overdue tickets from slot `from` (zero to begin), and the
|
|
243
|
+
# slot to resume at.
|
|
244
|
+
def overdue(now, from, limit)
|
|
245
|
+
rows, position = native_overdue(now, from, limit)
|
|
246
|
+
[rows.map { |row| Ticket.new(*row) }, position]
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
private
|
|
250
|
+
|
|
251
|
+
def order(partition, generation, recipient, name, field)
|
|
252
|
+
to = recipient.is_a?(Symbol) ? recipient : Replica.new(recipient)
|
|
253
|
+
Order.new(Ticket.new(partition, generation), to, action(name, field))
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# The action Data or Symbol a native row names.
|
|
257
|
+
def action(name, field)
|
|
258
|
+
return name unless ACTIONS.key?(name)
|
|
259
|
+
|
|
260
|
+
ACTIONS.fetch(name).new(field && TOKENED.include?(name) ? FencingToken.new(field) : field)
|
|
261
|
+
end
|
|
262
|
+
end
|
|
263
|
+
end
|