suspect_machines 0.5.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 +73 -0
- data/Cargo.lock +307 -0
- data/Cargo.toml +27 -0
- data/LICENSE.txt +21 -0
- data/README.md +335 -0
- data/ext/suspect_machines_native/core/CHANGELOG.md +48 -0
- data/ext/suspect_machines_native/core/Cargo.toml +17 -0
- data/ext/suspect_machines_native/core/README.md +115 -0
- data/ext/suspect_machines_native/core/src/detector.rs +90 -0
- data/ext/suspect_machines_native/core/src/lib.rs +454 -0
- data/ext/suspect_machines_native/core/src/math.rs +229 -0
- data/ext/suspect_machines_native/core/src/roster.rs +314 -0
- data/ext/suspect_machines_native/core/src/test_support.rs +189 -0
- data/ext/suspect_machines_native/core/src/window.rs +159 -0
- data/ext/suspect_machines_native/extconf.rb +2 -0
- data/ext/suspect_machines_native/ffi/Cargo.toml +21 -0
- data/ext/suspect_machines_native/ffi/extconf.rb +14 -0
- data/ext/suspect_machines_native/ffi/src/classes.rs +49 -0
- data/ext/suspect_machines_native/ffi/src/convert.rs +193 -0
- data/ext/suspect_machines_native/ffi/src/detector.rs +70 -0
- data/ext/suspect_machines_native/ffi/src/init.rs +63 -0
- data/ext/suspect_machines_native/ffi/src/lib.rs +69 -0
- data/ext/suspect_machines_native/ffi/src/roster.rs +129 -0
- data/ext/suspect_machines_native/ffi/src/values.rs +162 -0
- data/lib/suspect_machines/version.rb +5 -0
- data/lib/suspect_machines.rb +131 -0
- data/sig/suspect_machines.rbs +215 -0
- metadata +94 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
//! The `Data` classes, resolved once at load and pinned. The exact-class
|
|
2
|
+
//! reads and clone-then-keep are `matryona_machines::ruby`'s.
|
|
3
|
+
//!
|
|
4
|
+
//! A value read back must be an instance of exactly the gem's class: a
|
|
5
|
+
//! subclass constant leaks past the seal and `Data#initialize` can be bound
|
|
6
|
+
//! directly, so `is_a?` is not enough. Its fields are read straight from the
|
|
7
|
+
//! struct (no reader runs) and rebuilt through the core constructor, which
|
|
8
|
+
//! `Marshal.load` skipped.
|
|
9
|
+
use magnus::{Error, RModule, Ruby};
|
|
10
|
+
use matryona_machines::ruby::Class;
|
|
11
|
+
|
|
12
|
+
/// Every `Data` class the binding checks or builds.
|
|
13
|
+
pub struct Classes {
|
|
14
|
+
pub phi: Class,
|
|
15
|
+
pub threshold: Class,
|
|
16
|
+
pub normal: Class,
|
|
17
|
+
pub config: Class,
|
|
18
|
+
pub recorded: Class,
|
|
19
|
+
pub warming_up: Class,
|
|
20
|
+
pub measured: Class,
|
|
21
|
+
pub statistics: Class,
|
|
22
|
+
pub suspect: Class,
|
|
23
|
+
pub scan: Class,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
matryona_machines::registry! {
|
|
27
|
+
static CLASSES: Classes = "suspect_machines";
|
|
28
|
+
/// The classes resolved at load.
|
|
29
|
+
pub fn classes;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/// Resolve the classes `suspect_machines.rb` defined before requiring the
|
|
33
|
+
/// extension.
|
|
34
|
+
pub fn resolve(ruby: &Ruby, module: RModule) -> Result<&'static Classes, Error> {
|
|
35
|
+
let class = |name| Class::resolve(ruby, module, name);
|
|
36
|
+
let resolved = Classes {
|
|
37
|
+
phi: class("Phi")?,
|
|
38
|
+
threshold: class("Threshold")?,
|
|
39
|
+
normal: class("Normal")?,
|
|
40
|
+
config: class("Config")?,
|
|
41
|
+
recorded: class("Recorded")?,
|
|
42
|
+
warming_up: class("WarmingUp")?,
|
|
43
|
+
measured: class("Measured")?,
|
|
44
|
+
statistics: class("Statistics")?,
|
|
45
|
+
suspect: class("Suspect")?,
|
|
46
|
+
scan: class("Scan")?,
|
|
47
|
+
};
|
|
48
|
+
CLASSES.set(ruby, resolved)
|
|
49
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
//! Ruby `Data` values to core types and back.
|
|
2
|
+
use std::time::Duration;
|
|
3
|
+
|
|
4
|
+
use magnus::{Error, Ruby, Value, prelude::*};
|
|
5
|
+
use matryona_machines::ruby::{
|
|
6
|
+
Choice, Refused, SymbolNames, duration, exact_fields, integer, nanoseconds,
|
|
7
|
+
symbol_or_fields_why,
|
|
8
|
+
};
|
|
9
|
+
use suspect_machines::{
|
|
10
|
+
Beat, Config, ConfigError, Distribution, Normal, Refusal, Statistics, Suspicion,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
use crate::classes::classes;
|
|
14
|
+
use crate::{phi_value, refusal};
|
|
15
|
+
|
|
16
|
+
/// A constructor error of the core, raised as `RangeError`. `DurationOverflow`
|
|
17
|
+
/// and `ZeroWindow` cannot be reached from Ruby: times are `u64` nanoseconds
|
|
18
|
+
/// and the window is compiled in.
|
|
19
|
+
pub fn config_error(ruby: &Ruby, error: ConfigError) -> Error {
|
|
20
|
+
let message = match error {
|
|
21
|
+
ConfigError::ZeroWarmUp => "min_samples must be at least 1",
|
|
22
|
+
ConfigError::ZeroDeviationFloor => "min_std_dev must be at least 1",
|
|
23
|
+
ConfigError::DurationOverflow => "a duration exceeds 2**64-1 nanoseconds",
|
|
24
|
+
ConfigError::ZeroWindow => "the compiled window has no slots",
|
|
25
|
+
ConfigError::WarmUpExceedsWindow => "min_samples exceeds the compiled window",
|
|
26
|
+
};
|
|
27
|
+
Error::new(ruby.exception_range_error(), message)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
matryona_machines::symbols! {
|
|
31
|
+
/// A distribution as Ruby holds it: `:exponential`, or a `Normal`.
|
|
32
|
+
Distributions for Distribution as "a distribution" => names_only {
|
|
33
|
+
Normal(..) => "normal",
|
|
34
|
+
Exponential => "exponential",
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
matryona_machines::symbols! {
|
|
39
|
+
/// A beat as Ruby holds it: `:first`, a `Recorded`, or `:restarted`.
|
|
40
|
+
Beats for Beat as "a beat" => names_only {
|
|
41
|
+
First => "first",
|
|
42
|
+
Recorded { .. } => "recorded",
|
|
43
|
+
Restarted => "restarted",
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
matryona_machines::symbols! {
|
|
48
|
+
/// A suspicion as Ruby holds it: `:no_heartbeat`, a `WarmingUp`, or a
|
|
49
|
+
/// `Measured`.
|
|
50
|
+
Suspicions for Suspicion as "a suspicion" => names_only {
|
|
51
|
+
NoHeartbeat => "no_heartbeat",
|
|
52
|
+
WarmingUp { .. } => "warming_up",
|
|
53
|
+
Measured(..) => "measured",
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/// `:exponential`, or a `Normal` carrying its deviation floor: `RangeError`
|
|
58
|
+
/// naming any other Symbol (`unknown distribution :nope`), `TypeError` for
|
|
59
|
+
/// anything else.
|
|
60
|
+
pub fn distribution(ruby: &Ruby, value: Value) -> Result<Distribution, Error> {
|
|
61
|
+
let normal_class = classes(ruby)?.normal.get(ruby);
|
|
62
|
+
let read = symbol_or_fields_why(
|
|
63
|
+
ruby,
|
|
64
|
+
value,
|
|
65
|
+
Distributions,
|
|
66
|
+
&[normal_class],
|
|
67
|
+
|why| match why {
|
|
68
|
+
Refused::UnknownSymbol(symbol) => format!("unknown distribution {}", symbol.inspect()),
|
|
69
|
+
_ => "distribution must be :exponential or a SuspectMachines::Normal".into(),
|
|
70
|
+
},
|
|
71
|
+
)?;
|
|
72
|
+
match read {
|
|
73
|
+
Choice::Symbol(distribution) => Ok(distribution),
|
|
74
|
+
Choice::Struct(_, [floor]) => {
|
|
75
|
+
let floor = duration(floor, "min_std_dev")?;
|
|
76
|
+
normal(ruby, floor).map(Distribution::Normal)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/// The core's `Normal::new`: `RangeError` for a zero floor.
|
|
82
|
+
pub fn normal(ruby: &Ruby, min_std_dev: Duration) -> Result<Normal, Error> {
|
|
83
|
+
Normal::new(min_std_dev).map_err(|error| config_error(ruby, error))
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/// A config read from Ruby, handed to the core's constructor `build` for a
|
|
87
|
+
/// detector or a roster; either error is `RangeError`.
|
|
88
|
+
pub fn configured<T>(
|
|
89
|
+
ruby: &Ruby,
|
|
90
|
+
value: Value,
|
|
91
|
+
build: impl FnOnce(Config) -> Result<T, ConfigError>,
|
|
92
|
+
) -> Result<T, Error> {
|
|
93
|
+
build(config(ruby, value)?).map_err(|error| config_error(ruby, error))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
fn distribution_value(ruby: &Ruby, distribution: Distribution) -> Result<Value, Error> {
|
|
97
|
+
match distribution {
|
|
98
|
+
Distribution::Normal(normal) => {
|
|
99
|
+
let floor = nanoseconds(ruby, normal.min_std_dev());
|
|
100
|
+
classes(ruby)?.normal.get(ruby).funcall("new", (floor,))
|
|
101
|
+
}
|
|
102
|
+
Distribution::Exponential => {
|
|
103
|
+
Ok(Distributions::symbol(ruby, &Distribution::Exponential).as_value())
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
pub fn config(ruby: &Ruby, value: Value) -> Result<Config, Error> {
|
|
109
|
+
let message = "expected a SuspectMachines::Config";
|
|
110
|
+
let [distribution, min_samples, pause] =
|
|
111
|
+
exact_fields(ruby, value, classes(ruby)?.config.get(ruby), message)?;
|
|
112
|
+
let min_samples = integer(min_samples, "min_samples")?;
|
|
113
|
+
let pause = duration(pause, "acceptable_pause")?;
|
|
114
|
+
config_from(ruby, distribution, min_samples, pause)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/// The core's `Config::new` then `Config::with_acceptable_pause`.
|
|
118
|
+
pub fn config_from(
|
|
119
|
+
ruby: &Ruby,
|
|
120
|
+
distribution: Value,
|
|
121
|
+
min_samples: usize,
|
|
122
|
+
pause: Duration,
|
|
123
|
+
) -> Result<Config, Error> {
|
|
124
|
+
let distribution = self::distribution(ruby, distribution)?;
|
|
125
|
+
Config::new(distribution, min_samples)
|
|
126
|
+
.and_then(|config| config.with_acceptable_pause(pause))
|
|
127
|
+
.map_err(|error| config_error(ruby, error))
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
pub fn config_value(ruby: &Ruby, config: Config) -> Result<Value, Error> {
|
|
131
|
+
classes(ruby)?.config.get(ruby).funcall(
|
|
132
|
+
"__build",
|
|
133
|
+
(
|
|
134
|
+
distribution_value(ruby, config.distribution())?,
|
|
135
|
+
config.min_samples(),
|
|
136
|
+
nanoseconds(ruby, config.acceptable_pause()),
|
|
137
|
+
),
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/// An answer of `heartbeat`; a `Recorded` is built through `new`. The
|
|
142
|
+
/// interval is a difference of two `u64` instants, so it always fits.
|
|
143
|
+
pub fn beat(ruby: &Ruby, outcome: Result<Beat, Refusal>) -> Result<Value, Error> {
|
|
144
|
+
match outcome {
|
|
145
|
+
Ok(Beat::Recorded { interval, samples }) => {
|
|
146
|
+
let recorded = classes(ruby)?.recorded.get(ruby);
|
|
147
|
+
recorded.funcall("new", (nanoseconds(ruby, interval), samples))
|
|
148
|
+
}
|
|
149
|
+
Ok(beat @ (Beat::First | Beat::Restarted)) => Ok(Beats::symbol_of(ruby, beat).as_value()),
|
|
150
|
+
Err(refused) => Ok(refusal(ruby, refused)),
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
pub fn suspicion(ruby: &Ruby, outcome: Result<Suspicion, Refusal>) -> Result<Value, Error> {
|
|
155
|
+
match outcome {
|
|
156
|
+
Ok(Suspicion::NoHeartbeat) => {
|
|
157
|
+
Ok(Suspicions::symbol_of(ruby, Suspicion::NoHeartbeat).as_value())
|
|
158
|
+
}
|
|
159
|
+
Ok(Suspicion::WarmingUp {
|
|
160
|
+
samples,
|
|
161
|
+
required,
|
|
162
|
+
silence,
|
|
163
|
+
}) => classes(ruby)?
|
|
164
|
+
.warming_up
|
|
165
|
+
.get(ruby)
|
|
166
|
+
.funcall("new", (samples, required, nanoseconds(ruby, silence))),
|
|
167
|
+
Ok(Suspicion::Measured(phi)) => classes(ruby)?
|
|
168
|
+
.measured
|
|
169
|
+
.get(ruby)
|
|
170
|
+
.funcall("new", (phi_value(ruby, phi)?,)),
|
|
171
|
+
Err(refused) => Ok(refusal(ruby, refused)),
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
pub fn statistics(ruby: &Ruby, statistics: Option<Statistics>) -> Result<Option<Value>, Error> {
|
|
176
|
+
let Some(statistics) = statistics else {
|
|
177
|
+
return Ok(None);
|
|
178
|
+
};
|
|
179
|
+
classes(ruby)?
|
|
180
|
+
.statistics
|
|
181
|
+
.get(ruby)
|
|
182
|
+
.funcall(
|
|
183
|
+
"new",
|
|
184
|
+
(
|
|
185
|
+
statistics.samples(),
|
|
186
|
+
statistics.mean_ns(),
|
|
187
|
+
statistics.std_dev_ns(),
|
|
188
|
+
nanoseconds(ruby, statistics.min()),
|
|
189
|
+
nanoseconds(ruby, statistics.max()),
|
|
190
|
+
),
|
|
191
|
+
)
|
|
192
|
+
.map(Some)
|
|
193
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
//! `SuspectMachines::Detector`: one peer on its own.
|
|
2
|
+
use std::cell::RefCell;
|
|
3
|
+
|
|
4
|
+
use magnus::{Error, Integer, Ruby, Value, prelude::*, typed_data::Obj};
|
|
5
|
+
use matryona_machines::ruby::{duration, keep, nanoseconds};
|
|
6
|
+
use suspect_machines::Detector as Core;
|
|
7
|
+
|
|
8
|
+
use crate::{WINDOW, convert};
|
|
9
|
+
|
|
10
|
+
#[magnus::wrap(class = "SuspectMachines::Detector", free_immediately, size)]
|
|
11
|
+
pub struct Detector {
|
|
12
|
+
core: RefCell<Core<WINDOW>>,
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
impl Detector {
|
|
16
|
+
pub fn new(ruby: &Ruby, config: Value) -> Result<Self, Error> {
|
|
17
|
+
let core = convert::configured(ruby, config, Core::new)?;
|
|
18
|
+
Ok(Self {
|
|
19
|
+
core: RefCell::new(core),
|
|
20
|
+
})
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/// Clone-then-keep: a `Recorded` answer is built through `new`.
|
|
24
|
+
pub fn heartbeat(ruby: &Ruby, this: Obj<Self>, now: Value) -> Result<Value, Error> {
|
|
25
|
+
this.check_frozen()?;
|
|
26
|
+
let now = duration(now, "now")?;
|
|
27
|
+
keep(
|
|
28
|
+
ruby,
|
|
29
|
+
this,
|
|
30
|
+
&this.core,
|
|
31
|
+
|core| core.heartbeat(now),
|
|
32
|
+
|outcome| convert::beat(ruby, outcome),
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
pub fn suspicion(ruby: &Ruby, this: &Self, now: Value) -> Result<Value, Error> {
|
|
37
|
+
let now = duration(now, "now")?;
|
|
38
|
+
let outcome = this.core.borrow().suspicion(now);
|
|
39
|
+
convert::suspicion(ruby, outcome)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
pub fn statistics(ruby: &Ruby, this: &Self) -> Result<Option<Value>, Error> {
|
|
43
|
+
let statistics = this.core.borrow().statistics();
|
|
44
|
+
convert::statistics(ruby, statistics)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
pub fn samples(&self) -> usize {
|
|
48
|
+
self.core.borrow().samples()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
pub fn last_heartbeat(ruby: &Ruby, this: &Self) -> Option<Integer> {
|
|
52
|
+
let last = this.core.borrow().last_heartbeat();
|
|
53
|
+
last.map(|at| nanoseconds(ruby, at))
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
pub fn config(ruby: &Ruby, this: &Self) -> Result<Value, Error> {
|
|
57
|
+
let config = this.core.borrow().config();
|
|
58
|
+
convert::config_value(ruby, config)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
pub fn reset(this: Obj<Self>) -> Result<(), Error> {
|
|
62
|
+
this.check_frozen()?;
|
|
63
|
+
this.core.borrow_mut().reset();
|
|
64
|
+
Ok(())
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
pub const fn window() -> usize {
|
|
68
|
+
WINDOW
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
//! Private native entry point.
|
|
2
|
+
use magnus::{Error, Ruby, function, method, prelude::*};
|
|
3
|
+
use matryona_machines::ruby::Class;
|
|
4
|
+
|
|
5
|
+
use crate::{CAPACITY, WINDOW, classes, detector::Detector, roster::Roster, values};
|
|
6
|
+
|
|
7
|
+
#[magnus::init]
|
|
8
|
+
fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
9
|
+
let module = ruby.define_module("SuspectMachines")?;
|
|
10
|
+
module.define_singleton_method("__phi", function!(values::phi_level, 1))?;
|
|
11
|
+
module.define_singleton_method("__threshold", function!(values::threshold_level, 1))?;
|
|
12
|
+
module.define_singleton_method("__normal", function!(values::normal, 1))?;
|
|
13
|
+
module.define_singleton_method("__config", function!(values::config, 3))?;
|
|
14
|
+
module.define_singleton_method("__recorded", function!(values::recorded, 2))?;
|
|
15
|
+
module.define_singleton_method("__warming_up", function!(values::warming_up, 3))?;
|
|
16
|
+
module.define_singleton_method("__measured", function!(values::measured, 1))?;
|
|
17
|
+
module.define_singleton_method("__statistics", function!(values::statistics, 5))?;
|
|
18
|
+
module.define_singleton_method("__suspect", function!(values::suspect, 3))?;
|
|
19
|
+
module.define_singleton_method("__scan", function!(values::scan, 2))?;
|
|
20
|
+
let resolved = classes::resolve(ruby, module)?;
|
|
21
|
+
let config = resolved.config.get(ruby);
|
|
22
|
+
config.define_method(
|
|
23
|
+
"with_acceptable_pause",
|
|
24
|
+
method!(values::with_acceptable_pause, 1),
|
|
25
|
+
)?;
|
|
26
|
+
let phi = resolved.phi.get(ruby);
|
|
27
|
+
phi.define_method("millis", method!(values::millis, 0))?;
|
|
28
|
+
phi.define_method("reaches?", method!(values::reaches, 1))?;
|
|
29
|
+
|
|
30
|
+
let class = module.define_class("Detector", ruby.class_object())?;
|
|
31
|
+
class.undef_default_alloc_func();
|
|
32
|
+
class.const_set("WINDOW", WINDOW)?;
|
|
33
|
+
class.define_singleton_method("new", function!(Detector::new, 1))?;
|
|
34
|
+
class.define_method("heartbeat", method!(Detector::heartbeat, 1))?;
|
|
35
|
+
class.define_method("suspicion", method!(Detector::suspicion, 1))?;
|
|
36
|
+
class.define_method("statistics", method!(Detector::statistics, 0))?;
|
|
37
|
+
class.define_method("samples", method!(Detector::samples, 0))?;
|
|
38
|
+
class.define_method("last_heartbeat", method!(Detector::last_heartbeat, 0))?;
|
|
39
|
+
class.define_method("config", method!(Detector::config, 0))?;
|
|
40
|
+
class.define_method("reset", method!(Detector::reset, 0))?;
|
|
41
|
+
class.define_method("window", function!(Detector::window, 0))?;
|
|
42
|
+
// `new` answers a Detector: its class is fixed now, before it runs.
|
|
43
|
+
Class::pin_wrapped::<Detector>(ruby, class)?;
|
|
44
|
+
|
|
45
|
+
let class = module.define_class("Roster", ruby.class_object())?;
|
|
46
|
+
class.undef_default_alloc_func();
|
|
47
|
+
class.const_set("CAPACITY", CAPACITY)?;
|
|
48
|
+
class.const_set("WINDOW", WINDOW)?;
|
|
49
|
+
class.define_singleton_method("new", function!(Roster::new, 1))?;
|
|
50
|
+
class.define_method("heartbeat", method!(Roster::heartbeat, 3))?;
|
|
51
|
+
class.define_method("suspicion", method!(Roster::suspicion, 2))?;
|
|
52
|
+
class.define_method("scan", method!(Roster::scan, 4))?;
|
|
53
|
+
class.define_method("statistics", method!(Roster::statistics, 1))?;
|
|
54
|
+
class.define_method("incarnation", method!(Roster::incarnation, 1))?;
|
|
55
|
+
class.define_method("last_heartbeat", method!(Roster::last_heartbeat, 1))?;
|
|
56
|
+
class.define_method("forget", method!(Roster::forget, 1))?;
|
|
57
|
+
class.define_method("size", method!(Roster::size, 0))?;
|
|
58
|
+
class.define_method("empty?", method!(Roster::is_empty, 0))?;
|
|
59
|
+
class.define_method("config", method!(Roster::config, 0))?;
|
|
60
|
+
class.define_method("capacity", function!(Roster::capacity, 0))?;
|
|
61
|
+
class.define_method("window", function!(Roster::window, 0))?;
|
|
62
|
+
Class::pin_wrapped::<Roster>(ruby, class).map(drop)
|
|
63
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
//! Thin binding to the phi-accrual detector and the bounded roster.
|
|
2
|
+
mod classes;
|
|
3
|
+
mod convert;
|
|
4
|
+
mod detector;
|
|
5
|
+
mod init;
|
|
6
|
+
mod roster;
|
|
7
|
+
mod values;
|
|
8
|
+
|
|
9
|
+
use classes::classes;
|
|
10
|
+
use magnus::{Error, Ruby, Value, prelude::*};
|
|
11
|
+
use matryona_machines::ruby::{Symbols, exact_fields, finite};
|
|
12
|
+
use suspect_machines::{Phi, Refusal, Threshold};
|
|
13
|
+
|
|
14
|
+
/// Peers one roster can track.
|
|
15
|
+
const CAPACITY: usize = 64;
|
|
16
|
+
/// Intervals kept per peer, by a roster and by a detector alike.
|
|
17
|
+
const WINDOW: usize = 128;
|
|
18
|
+
|
|
19
|
+
fn range_error(ruby: &Ruby, message: &'static str) -> Error {
|
|
20
|
+
Error::new(ruby.exception_range_error(), message)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/// The core's `Phi::new`: `RangeError` unless finite and not negative.
|
|
24
|
+
fn phi(ruby: &Ruby, value: f64) -> Result<Phi, Error> {
|
|
25
|
+
Phi::new(value).ok_or_else(|| range_error(ruby, "phi must be finite and not negative"))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/// The core's `Threshold::new`: `RangeError` unless finite and above zero.
|
|
29
|
+
fn threshold(ruby: &Ruby, value: f64) -> Result<Threshold, Error> {
|
|
30
|
+
Threshold::new(value)
|
|
31
|
+
.ok_or_else(|| range_error(ruby, "threshold must be finite and greater than zero"))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/// The core's `Phi` rebuilt from a `SuspectMachines::Phi`.
|
|
35
|
+
fn phi_from(ruby: &Ruby, value: Value) -> Result<Phi, Error> {
|
|
36
|
+
let message = "expected a SuspectMachines::Phi";
|
|
37
|
+
let [level] = exact_fields(ruby, value, classes(ruby)?.phi.get(ruby), message)?;
|
|
38
|
+
phi(ruby, finite(level, "phi")?)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/// The core's `Threshold` rebuilt from a `SuspectMachines::Threshold`.
|
|
42
|
+
fn threshold_from(ruby: &Ruby, value: Value) -> Result<Threshold, Error> {
|
|
43
|
+
let message = "expected a SuspectMachines::Threshold";
|
|
44
|
+
let [level] = exact_fields(ruby, value, classes(ruby)?.threshold.get(ruby), message)?;
|
|
45
|
+
threshold(ruby, finite(level, "threshold")?)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/// A `SuspectMachines::Phi` holding `phi`, built through `new`.
|
|
49
|
+
fn phi_value(ruby: &Ruby, phi: Phi) -> Result<Value, Error> {
|
|
50
|
+
classes(ruby)?.phi.get(ruby).funcall("new", (phi.value(),))
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
matryona_machines::symbols! {
|
|
54
|
+
/// Every variant has its own symbol. `Overflow` cannot be reached from
|
|
55
|
+
/// Ruby: every Integer the binding accepts fits `u64` nanoseconds.
|
|
56
|
+
Refusals for Refusal as "a refusal" {
|
|
57
|
+
TimeRegression => "time_regression",
|
|
58
|
+
SameInstant => "same_instant",
|
|
59
|
+
Overflow => "overflow",
|
|
60
|
+
Stale => "stale",
|
|
61
|
+
Full => "full",
|
|
62
|
+
Untracked => "untracked",
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/// The refusal's symbol.
|
|
67
|
+
fn refusal(ruby: &Ruby, refusal: Refusal) -> Value {
|
|
68
|
+
Refusals::symbol(ruby, refusal).as_value()
|
|
69
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
//! `SuspectMachines::Roster`.
|
|
2
|
+
use std::cell::RefCell;
|
|
3
|
+
|
|
4
|
+
use magnus::{Error, Integer, Ruby, Symbol, Value, prelude::*, typed_data::Obj};
|
|
5
|
+
use matryona_machines::ruby::{Symbols, duration, integer, integer_in, keep, nanoseconds};
|
|
6
|
+
use suspect_machines::{Forgetting, Roster as Core, Suspect};
|
|
7
|
+
|
|
8
|
+
use crate::classes::classes;
|
|
9
|
+
use crate::{CAPACITY, WINDOW, convert, phi_value, refusal, threshold_from};
|
|
10
|
+
|
|
11
|
+
matryona_machines::symbols! {
|
|
12
|
+
/// What `forget` answers.
|
|
13
|
+
Forgettings for Forgetting as "a forgetting" {
|
|
14
|
+
Forgotten => "forgotten",
|
|
15
|
+
NotTracked => "not_tracked",
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
#[magnus::wrap(class = "SuspectMachines::Roster", free_immediately, size)]
|
|
20
|
+
pub struct Roster {
|
|
21
|
+
core: RefCell<Box<Core<CAPACITY, WINDOW>>>,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
impl Roster {
|
|
25
|
+
pub fn new(ruby: &Ruby, config: Value) -> Result<Self, Error> {
|
|
26
|
+
let core = convert::configured(ruby, config, Core::new)?;
|
|
27
|
+
Ok(Self {
|
|
28
|
+
core: RefCell::new(Box::new(core)),
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
pub fn heartbeat(
|
|
33
|
+
ruby: &Ruby,
|
|
34
|
+
this: Obj<Self>,
|
|
35
|
+
peer: Value,
|
|
36
|
+
incarnation: Value,
|
|
37
|
+
now: Value,
|
|
38
|
+
) -> Result<Value, Error> {
|
|
39
|
+
this.check_frozen()?;
|
|
40
|
+
let (peer, incarnation) = (integer(peer, "peer")?, integer(incarnation, "incarnation")?);
|
|
41
|
+
let now = duration(now, "now")?;
|
|
42
|
+
keep(
|
|
43
|
+
ruby,
|
|
44
|
+
this,
|
|
45
|
+
&this.core,
|
|
46
|
+
|core| core.heartbeat(peer, incarnation, now),
|
|
47
|
+
|outcome| convert::beat(ruby, outcome),
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
pub fn suspicion(ruby: &Ruby, this: &Self, peer: Value, now: Value) -> Result<Value, Error> {
|
|
52
|
+
let (peer, now) = (integer(peer, "peer")?, duration(now, "now")?);
|
|
53
|
+
let outcome = this.core.borrow().suspicion(peer, now);
|
|
54
|
+
convert::suspicion(ruby, outcome)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/// `limit` is the length of the core's `out` buffer.
|
|
58
|
+
pub fn scan(
|
|
59
|
+
ruby: &Ruby,
|
|
60
|
+
this: &Self,
|
|
61
|
+
now: Value,
|
|
62
|
+
threshold: Value,
|
|
63
|
+
cursor: Value,
|
|
64
|
+
limit: Value,
|
|
65
|
+
) -> Result<Value, Error> {
|
|
66
|
+
let now = duration(now, "now")?;
|
|
67
|
+
let cursor: usize = integer(cursor, "cursor")?;
|
|
68
|
+
let limit = integer_in(limit, "limit", 0..=CAPACITY)?;
|
|
69
|
+
let threshold = threshold_from(ruby, threshold)?;
|
|
70
|
+
let classes = classes(ruby)?;
|
|
71
|
+
let (suspect, scan_class) = (classes.suspect.get(ruby), classes.scan.get(ruby));
|
|
72
|
+
let mut buffer = [Suspect::default(); CAPACITY];
|
|
73
|
+
let out = buffer.get_mut(..limit).unwrap_or(&mut []);
|
|
74
|
+
let outcome = this.core.borrow().scan(now, threshold, cursor, out);
|
|
75
|
+
let scan = match outcome {
|
|
76
|
+
Ok(scan) => scan,
|
|
77
|
+
Err(refused) => return Ok(refusal(ruby, refused)),
|
|
78
|
+
};
|
|
79
|
+
let suspects = ruby.ary_new_capa(scan.suspects());
|
|
80
|
+
for found in out.iter().take(scan.suspects()) {
|
|
81
|
+
let phi = phi_value(ruby, found.phi())?;
|
|
82
|
+
let entry: Value = suspect.funcall("new", (found.peer(), found.incarnation(), phi))?;
|
|
83
|
+
suspects.push(entry)?;
|
|
84
|
+
}
|
|
85
|
+
scan_class.funcall("new", (suspects, scan.next()))
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
pub fn statistics(ruby: &Ruby, this: &Self, peer: Value) -> Result<Option<Value>, Error> {
|
|
89
|
+
let statistics = this.core.borrow().statistics(integer(peer, "peer")?);
|
|
90
|
+
convert::statistics(ruby, statistics)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
pub fn incarnation(&self, peer: Value) -> Result<Option<u64>, Error> {
|
|
94
|
+
Ok(self.core.borrow().incarnation(integer(peer, "peer")?))
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
pub fn last_heartbeat(ruby: &Ruby, this: &Self, peer: Value) -> Result<Option<Integer>, Error> {
|
|
98
|
+
let last = this.core.borrow().last_heartbeat(integer(peer, "peer")?);
|
|
99
|
+
Ok(last.map(|at| nanoseconds(ruby, at)))
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
pub fn forget(ruby: &Ruby, this: Obj<Self>, peer: Value) -> Result<Symbol, Error> {
|
|
103
|
+
this.check_frozen()?;
|
|
104
|
+
let peer = integer(peer, "peer")?;
|
|
105
|
+
let outcome = this.core.borrow_mut().forget(peer);
|
|
106
|
+
Ok(Forgettings::symbol(ruby, outcome))
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
pub fn size(&self) -> usize {
|
|
110
|
+
self.core.borrow().len()
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
pub fn is_empty(&self) -> bool {
|
|
114
|
+
self.core.borrow().is_empty()
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
pub fn config(ruby: &Ruby, this: &Self) -> Result<Value, Error> {
|
|
118
|
+
let config = this.core.borrow().config();
|
|
119
|
+
convert::config_value(ruby, config)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
pub const fn capacity() -> usize {
|
|
123
|
+
CAPACITY
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
pub const fn window() -> usize {
|
|
127
|
+
WINDOW
|
|
128
|
+
}
|
|
129
|
+
}
|