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,162 @@
|
|
|
1
|
+
//! The private hooks every `Data` value calls on construction: each checks
|
|
2
|
+
//! its fields through the core and answers what the value stores. Wrong type
|
|
3
|
+
//! is `TypeError`; a value the core would never hold is `RangeError`.
|
|
4
|
+
use std::time::Duration;
|
|
5
|
+
|
|
6
|
+
use magnus::{Error, RArray, Ruby, Value, prelude::*};
|
|
7
|
+
use matryona_machines::ruby::{exact_array, exact_fields, finite, integer, optional};
|
|
8
|
+
use suspect_machines::{Beat, Scan, Statistics, Suspicion};
|
|
9
|
+
|
|
10
|
+
use crate::classes::classes;
|
|
11
|
+
use crate::{CAPACITY, WINDOW, convert, phi, phi_from, range_error, threshold, threshold_from};
|
|
12
|
+
|
|
13
|
+
/// `Phi.new`: the level as a Float the core's `Phi::new` accepts.
|
|
14
|
+
pub fn phi_level(ruby: &Ruby, value: Value) -> Result<f64, Error> {
|
|
15
|
+
phi(ruby, finite(value, "phi")?).map(suspect_machines::Phi::value)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/// `Threshold.new`: the level as a Float the core's `Threshold::new` accepts.
|
|
19
|
+
pub fn threshold_level(ruby: &Ruby, value: Value) -> Result<f64, Error> {
|
|
20
|
+
let level = finite(value, "threshold")?;
|
|
21
|
+
threshold(ruby, level).map(suspect_machines::Threshold::value)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/// `Phi#millis`: the core's `Phi::millis`.
|
|
25
|
+
pub fn millis(ruby: &Ruby, this: Value) -> Result<u64, Error> {
|
|
26
|
+
phi_from(ruby, this).map(suspect_machines::Phi::millis)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/// `Phi#reaches?`: the core's `Phi::reaches`, for a `Threshold` only.
|
|
30
|
+
pub fn reaches(ruby: &Ruby, this: Value, threshold: Value) -> Result<bool, Error> {
|
|
31
|
+
let threshold = threshold_from(ruby, threshold)?;
|
|
32
|
+
Ok(phi_from(ruby, this)?.reaches(threshold))
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/// `Normal.new`: the core's `Normal::new`, `RangeError` for a zero floor.
|
|
36
|
+
pub fn normal(ruby: &Ruby, min_std_dev: Value) -> Result<u64, Error> {
|
|
37
|
+
let floor: u64 = integer(min_std_dev, "min_std_dev")?;
|
|
38
|
+
convert::normal(ruby, Duration::from_nanos(floor)).map(|_| floor)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/// `Config.new` and `with_acceptable_pause`: the core's `Config::new` and
|
|
42
|
+
/// `Config::with_acceptable_pause`.
|
|
43
|
+
pub fn config(
|
|
44
|
+
ruby: &Ruby,
|
|
45
|
+
distribution: Value,
|
|
46
|
+
min_samples: Value,
|
|
47
|
+
pause: Value,
|
|
48
|
+
) -> Result<(Value, usize, u64), Error> {
|
|
49
|
+
let min_samples = integer(min_samples, "min_samples")?;
|
|
50
|
+
let pause: u64 = integer(pause, "acceptable_pause")?;
|
|
51
|
+
convert::config_from(ruby, distribution, min_samples, Duration::from_nanos(pause))?;
|
|
52
|
+
Ok((distribution, min_samples, pause))
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// `Config#with_acceptable_pause`: the core's `Config::with_acceptable_pause`
|
|
56
|
+
/// on this config, read by its exact class from the struct (no reader runs)
|
|
57
|
+
/// and revalidated through the core, since `Marshal.load` skips
|
|
58
|
+
/// `initialize`. The copy is built from what the core answered.
|
|
59
|
+
pub fn with_acceptable_pause(ruby: &Ruby, this: Value, pause: Value) -> Result<Value, Error> {
|
|
60
|
+
let config = convert::config(ruby, this)?;
|
|
61
|
+
let pause: u64 = integer(pause, "acceptable_pause")?;
|
|
62
|
+
let paused = config
|
|
63
|
+
.with_acceptable_pause(Duration::from_nanos(pause))
|
|
64
|
+
.map_err(|error| convert::config_error(ruby, error))?;
|
|
65
|
+
convert::config_value(ruby, paused)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/// `Recorded.new`: the core's `Beat::recorded` for the compiled window.
|
|
69
|
+
pub fn recorded(ruby: &Ruby, interval: Value, samples: Value) -> Result<(u64, usize), Error> {
|
|
70
|
+
let interval: u64 = integer(interval, "interval")?;
|
|
71
|
+
let samples = integer(samples, "samples")?;
|
|
72
|
+
Beat::recorded::<WINDOW>(Duration::from_nanos(interval), samples)
|
|
73
|
+
.map(|_| (interval, samples))
|
|
74
|
+
.ok_or_else(|| {
|
|
75
|
+
range_error(
|
|
76
|
+
ruby,
|
|
77
|
+
"a Recorded needs interval >= 1 and samples in 1..WINDOW",
|
|
78
|
+
)
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/// `WarmingUp.new`: the core's `Suspicion::warming_up` for the compiled
|
|
83
|
+
/// window.
|
|
84
|
+
pub fn warming_up(
|
|
85
|
+
ruby: &Ruby,
|
|
86
|
+
samples: Value,
|
|
87
|
+
required: Value,
|
|
88
|
+
silence: Value,
|
|
89
|
+
) -> Result<(usize, usize, u64), Error> {
|
|
90
|
+
let (samples, required) = (integer(samples, "samples")?, integer(required, "required")?);
|
|
91
|
+
let silence: u64 = integer(silence, "silence")?;
|
|
92
|
+
Suspicion::warming_up::<WINDOW>(samples, required, Duration::from_nanos(silence))
|
|
93
|
+
.map(|_| (samples, required, silence))
|
|
94
|
+
.ok_or_else(|| range_error(ruby, "a WarmingUp needs samples < required <= WINDOW"))
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/// `Measured.new`: the level must be a `Phi`.
|
|
98
|
+
pub fn measured(ruby: &Ruby, phi: Value) -> Result<Value, Error> {
|
|
99
|
+
phi_from(ruby, phi).map(|_| phi)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/// `Statistics.new`: the core's `Statistics::new` for the compiled window.
|
|
103
|
+
pub fn statistics(
|
|
104
|
+
ruby: &Ruby,
|
|
105
|
+
samples: Value,
|
|
106
|
+
mean_ns: Value,
|
|
107
|
+
std_dev_ns: Value,
|
|
108
|
+
min: Value,
|
|
109
|
+
max: Value,
|
|
110
|
+
) -> Result<(usize, f64, f64, u64, u64), Error> {
|
|
111
|
+
let samples = integer(samples, "samples")?;
|
|
112
|
+
let mean = finite(mean_ns, "mean_ns")?;
|
|
113
|
+
let spread = finite(std_dev_ns, "std_dev_ns")?;
|
|
114
|
+
let (min, max): (u64, u64) = (integer(min, "min")?, integer(max, "max")?);
|
|
115
|
+
let (low, high) = (Duration::from_nanos(min), Duration::from_nanos(max));
|
|
116
|
+
Statistics::new::<WINDOW>(samples, mean, spread, low, high)
|
|
117
|
+
.map(|stats| (samples, stats.mean_ns(), stats.std_dev_ns(), min, max))
|
|
118
|
+
.ok_or_else(|| {
|
|
119
|
+
range_error(
|
|
120
|
+
ruby,
|
|
121
|
+
"Statistics need samples in 1..WINDOW, 1 <= min <= max (equal for one \
|
|
122
|
+
sample), a mean within min..max and a positively signed deviation of \
|
|
123
|
+
at most (max - min) / 2",
|
|
124
|
+
)
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/// `Suspect.new`: any `u64` peer and incarnation, and a `Phi`.
|
|
129
|
+
pub fn suspect(
|
|
130
|
+
ruby: &Ruby,
|
|
131
|
+
peer: Value,
|
|
132
|
+
incarnation: Value,
|
|
133
|
+
phi: Value,
|
|
134
|
+
) -> Result<(u64, u64, Value), Error> {
|
|
135
|
+
let (peer, incarnation) = (integer(peer, "peer")?, integer(incarnation, "incarnation")?);
|
|
136
|
+
phi_from(ruby, phi)?;
|
|
137
|
+
Ok((peer, incarnation, phi))
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/// `Scan.new`: an Array of `Suspect` and a resume cursor, as the core's
|
|
141
|
+
/// `Scan::new` for the compiled capacity accepts. Stores a frozen copy.
|
|
142
|
+
pub fn scan(ruby: &Ruby, suspects: Value, next: Value) -> Result<(RArray, Option<usize>), Error> {
|
|
143
|
+
let message = "suspects must be an Array of SuspectMachines::Suspect";
|
|
144
|
+
let list = exact_array(ruby, suspects, message)?;
|
|
145
|
+
let copy = ruby.ary_new_capa(list.len());
|
|
146
|
+
let class = classes(ruby)?.suspect.get(ruby);
|
|
147
|
+
for entry in list {
|
|
148
|
+
let message = "suspects must be SuspectMachines::Suspect";
|
|
149
|
+
let [peer, incarnation, phi] = exact_fields(ruby, entry, class, message)?;
|
|
150
|
+
suspect(ruby, peer, incarnation, phi)?;
|
|
151
|
+
copy.push(entry)?;
|
|
152
|
+
}
|
|
153
|
+
let cursor: Option<usize> = optional(next, "next")?;
|
|
154
|
+
Scan::new::<CAPACITY>(copy.len(), cursor).ok_or_else(|| {
|
|
155
|
+
range_error(
|
|
156
|
+
ruby,
|
|
157
|
+
"a Scan holds at most CAPACITY suspects and resumes below CAPACITY",
|
|
158
|
+
)
|
|
159
|
+
})?;
|
|
160
|
+
copy.freeze();
|
|
161
|
+
Ok((copy, cursor))
|
|
162
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'suspect_machines/version'
|
|
4
|
+
|
|
5
|
+
# Every time, instant or span, is Integer nanoseconds. Every value mirrors the
|
|
6
|
+
# Rust core's type of the same name, field for field, and is checked by the
|
|
7
|
+
# core when it is built, whether by the gem or by hand: a wrong type raises
|
|
8
|
+
# TypeError, a value the core would never hold raises RangeError.
|
|
9
|
+
module SuspectMachines
|
|
10
|
+
# The core's `Phi`: a suspicion level, finite and not negative (a Float).
|
|
11
|
+
# `millis` and `reaches?` are the core's `Phi::millis` and `Phi::reaches`.
|
|
12
|
+
Phi = Data.define(:value) do
|
|
13
|
+
def initialize(value:)
|
|
14
|
+
super(value: SuspectMachines.__send__(:__phi, value))
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# The core's `Threshold`: a phi level worth acting on, finite and above
|
|
19
|
+
# zero (a Float).
|
|
20
|
+
Threshold = Data.define(:value) do
|
|
21
|
+
def initialize(value:)
|
|
22
|
+
super(value: SuspectMachines.__send__(:__threshold, value))
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# The core's `Normal`, carried by `Distribution::Normal`: intervals are
|
|
27
|
+
# assumed normally distributed, with the standard deviation floored at
|
|
28
|
+
# `min_std_dev`, at least 1 (`Normal::new` refuses zero: RangeError). The
|
|
29
|
+
# other distribution, `Distribution::Exponential`, is the symbol :exponential.
|
|
30
|
+
Normal = Data.define(:min_std_dev) do
|
|
31
|
+
def initialize(min_std_dev:)
|
|
32
|
+
super(min_std_dev: SuspectMachines.__send__(:__normal, min_std_dev))
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Detector policy. `Config.new(distribution, min_samples)` is the core's
|
|
37
|
+
# constructor and tolerates no pause; `with_acceptable_pause` answers a copy
|
|
38
|
+
# that does. `distribution` is :exponential or a Normal.
|
|
39
|
+
Config = Data.define(:distribution, :min_samples, :acceptable_pause) do
|
|
40
|
+
class << self
|
|
41
|
+
alias_method :__build, :new
|
|
42
|
+
private :__build
|
|
43
|
+
|
|
44
|
+
def new(distribution, min_samples) = __build(distribution, min_samples, 0)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def initialize(distribution:, min_samples:, acceptable_pause:)
|
|
48
|
+
distribution, min_samples, acceptable_pause =
|
|
49
|
+
SuspectMachines.__send__(:__config, distribution, min_samples, acceptable_pause)
|
|
50
|
+
super
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# `with_acceptable_pause(pause)` is native: the core's
|
|
54
|
+
# `Config::with_acceptable_pause` on this config, read from the struct by
|
|
55
|
+
# its exact class (never through the readers) and checked again.
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# The core's `Beat::Recorded`: an accepted heartbeat that put an interval in
|
|
59
|
+
# the window, the time since the previous heartbeat and the intervals now held.
|
|
60
|
+
Recorded = Data.define(:interval, :samples) do
|
|
61
|
+
def initialize(interval:, samples:)
|
|
62
|
+
interval, samples = SuspectMachines.__send__(:__recorded, interval, samples)
|
|
63
|
+
super
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# The core's `Suspicion::WarmingUp`: fewer intervals than the warm-up
|
|
68
|
+
# requires, so no phi exists yet. `silence` is the time since the last
|
|
69
|
+
# heartbeat, for a bootstrap timeout.
|
|
70
|
+
WarmingUp = Data.define(:samples, :required, :silence) do
|
|
71
|
+
def initialize(samples:, required:, silence:)
|
|
72
|
+
samples, required, silence = SuspectMachines.__send__(:__warming_up, samples, required, silence)
|
|
73
|
+
super
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# The core's `Suspicion::Measured`: the suspicion level, a Phi.
|
|
78
|
+
Measured = Data.define(:phi) do
|
|
79
|
+
def initialize(phi:)
|
|
80
|
+
super(phi: SuspectMachines.__send__(:__measured, phi))
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Statistics of one peer's interval window. The mean and the population
|
|
85
|
+
# standard deviation (before any floor) are Float nanoseconds; `min` and
|
|
86
|
+
# `max` are the shortest and longest interval.
|
|
87
|
+
Statistics = Data.define(:samples, :mean_ns, :std_dev_ns, :min, :max) do
|
|
88
|
+
def initialize(samples:, mean_ns:, std_dev_ns:, min:, max:)
|
|
89
|
+
samples, mean_ns, std_dev_ns, min, max =
|
|
90
|
+
SuspectMachines.__send__(:__statistics, samples, mean_ns, std_dev_ns, min, max)
|
|
91
|
+
super
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# A peer whose phi reached the scan threshold.
|
|
96
|
+
Suspect = Data.define(:peer, :incarnation, :phi) do
|
|
97
|
+
def initialize(peer:, incarnation:, phi:)
|
|
98
|
+
peer, incarnation, phi = SuspectMachines.__send__(:__suspect, peer, incarnation, phi)
|
|
99
|
+
super
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# One bounded page of suspects (a frozen Array). The core's `Scan::suspects`
|
|
104
|
+
# counts the suspects it wrote to the caller's buffer; here `suspects` holds
|
|
105
|
+
# them. `next` is the cursor to resume from, nil once the roster has been
|
|
106
|
+
# fully examined.
|
|
107
|
+
Scan = Data.define(:suspects, :next) do
|
|
108
|
+
def initialize(suspects:, next:)
|
|
109
|
+
suspects, cursor = SuspectMachines.__send__(:__scan, suspects, binding.local_variable_get(:next))
|
|
110
|
+
super(suspects:, next: cursor)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Native is required: no algorithm fallback. It checks and builds the Data
|
|
116
|
+
# values above, so they must exist first.
|
|
117
|
+
require 'suspect_machines_native/suspect_machines_native'
|
|
118
|
+
|
|
119
|
+
module SuspectMachines
|
|
120
|
+
private_class_method :__phi, :__threshold, :__normal, :__config, :__recorded, :__warming_up,
|
|
121
|
+
:__measured, :__statistics, :__suspect, :__scan
|
|
122
|
+
|
|
123
|
+
# The values are final. `initialize` is where each validates, and a subclass
|
|
124
|
+
# could override it, or `allocate` build an instance that never ran it:
|
|
125
|
+
# subclassing raises TypeError (as subclassing Class does) and `allocate`
|
|
126
|
+
# is gone. `new`, keywords, `with` and `dup` all still run the check.
|
|
127
|
+
[Phi, Threshold, Normal, Config, Recorded, WarmingUp, Measured, Statistics, Suspect, Scan].each do |value|
|
|
128
|
+
value.singleton_class.undef_method(:allocate)
|
|
129
|
+
value.define_singleton_method(:inherited) { |_| raise TypeError, "#{value} cannot be subclassed" }
|
|
130
|
+
end
|
|
131
|
+
end
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
module SuspectMachines
|
|
2
|
+
VERSION: String
|
|
3
|
+
|
|
4
|
+
# Every time, instant or span, is Integer nanoseconds of the caller's
|
|
5
|
+
# monotonic clock. A non-Integer raises TypeError; negative or past 2**64-1
|
|
6
|
+
# raises RangeError.
|
|
7
|
+
#
|
|
8
|
+
# Every Data value below is checked by the core when it is built, by the
|
|
9
|
+
# gem or by hand (including `with`): a wrong type raises TypeError, a value
|
|
10
|
+
# the core would never hold raises RangeError. The value classes are final:
|
|
11
|
+
# subclassing one raises TypeError and `allocate` is undefined. WINDOW and
|
|
12
|
+
# CAPACITY are the compiled 128 and 64.
|
|
13
|
+
|
|
14
|
+
# Every refusal symbol: one per variant of the core's `Refusal`. A refused
|
|
15
|
+
# call changed nothing. Each method below lists exactly the ones it can
|
|
16
|
+
# answer. `:overflow` (a time past u64 nanoseconds) cannot be reached from
|
|
17
|
+
# Ruby: such an Integer raises RangeError before the core sees it.
|
|
18
|
+
type refusal = :time_regression | :same_instant | :overflow | :stale | :full | :untracked
|
|
19
|
+
|
|
20
|
+
# The core's `Distribution`: the unit variant is a symbol.
|
|
21
|
+
type distribution = :exponential | Normal
|
|
22
|
+
|
|
23
|
+
# The core's `Beat`.
|
|
24
|
+
type beat = :first | Recorded | :restarted
|
|
25
|
+
|
|
26
|
+
# The core's `Suspicion`.
|
|
27
|
+
type suspicion = :no_heartbeat | WarmingUp | Measured
|
|
28
|
+
|
|
29
|
+
# Private native checks behind the Data values: each answers the fields as
|
|
30
|
+
# the core holds them (a Float for a phi, a threshold, a mean or a
|
|
31
|
+
# deviation; a frozen copy of the suspects), TypeError for a wrong type,
|
|
32
|
+
# RangeError for a value the core would never hold.
|
|
33
|
+
private def self.__phi: (Float | Integer value) -> Float
|
|
34
|
+
private def self.__threshold: (Float | Integer value) -> Float
|
|
35
|
+
private def self.__normal: (Integer min_std_dev) -> Integer
|
|
36
|
+
private def self.__config: (distribution distribution, Integer min_samples, Integer acceptable_pause)
|
|
37
|
+
-> [distribution, Integer, Integer]
|
|
38
|
+
private def self.__recorded: (Integer interval, Integer samples) -> [Integer, Integer]
|
|
39
|
+
private def self.__warming_up: (Integer samples, Integer required, Integer silence) -> [Integer, Integer, Integer]
|
|
40
|
+
private def self.__measured: (Phi phi) -> Phi
|
|
41
|
+
private def self.__statistics: (Integer samples, Float | Integer mean_ns, Float | Integer std_dev_ns, Integer min,
|
|
42
|
+
Integer max) -> [Integer, Float, Float, Integer, Integer]
|
|
43
|
+
private def self.__suspect: (Integer peer, Integer incarnation, Phi phi) -> [Integer, Integer, Phi]
|
|
44
|
+
private def self.__scan: (Array[Suspect] suspects, Integer? next) -> [Array[Suspect], Integer?]
|
|
45
|
+
|
|
46
|
+
# Mirrors the core's `Phi`: `value` is a finite, non-negative Float
|
|
47
|
+
# (-0.0 is stored as 0.0). Integer input is stored as a Float.
|
|
48
|
+
class Phi < Data
|
|
49
|
+
attr_reader value: Float
|
|
50
|
+
def self.new: (Float | Integer value) -> Phi
|
|
51
|
+
| (value: Float | Integer) -> Phi
|
|
52
|
+
# The core's `Phi::millis`: thousandths, rounded down, saturating at 2**64-1.
|
|
53
|
+
def millis: () -> Integer
|
|
54
|
+
# The core's `Phi::reaches`. TypeError for anything but a Threshold.
|
|
55
|
+
def reaches?: (Threshold threshold) -> bool
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Mirrors the core's `Threshold`: `value` is a finite Float above zero.
|
|
59
|
+
class Threshold < Data
|
|
60
|
+
attr_reader value: Float
|
|
61
|
+
def self.new: (Float | Integer value) -> Threshold
|
|
62
|
+
| (value: Float | Integer) -> Threshold
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Mirrors the core's `Normal` (`Normal::new`), carried by
|
|
66
|
+
# `Distribution::Normal`. `min_std_dev` is an Integer in 1..2**64-1; zero
|
|
67
|
+
# raises RangeError.
|
|
68
|
+
class Normal < Data
|
|
69
|
+
attr_reader min_std_dev: Integer
|
|
70
|
+
def self.new: (Integer min_std_dev) -> Normal
|
|
71
|
+
| (min_std_dev: Integer) -> Normal
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Mirrors the core's `Config`. `new` takes the two arguments of the core's
|
|
75
|
+
# `Config::new`, positionally, and starts with an acceptable pause of zero.
|
|
76
|
+
# Raises RangeError for a zero `min_samples` or an unknown distribution
|
|
77
|
+
# symbol.
|
|
78
|
+
class Config < Data
|
|
79
|
+
attr_reader distribution: distribution
|
|
80
|
+
attr_reader min_samples: Integer
|
|
81
|
+
attr_reader acceptable_pause: Integer
|
|
82
|
+
def self.new: (distribution distribution, Integer min_samples) -> Config
|
|
83
|
+
# Data's own `new`, kept under a private name for the three-field build.
|
|
84
|
+
private def self.__build: (distribution distribution, Integer min_samples, Integer acceptable_pause) -> Config
|
|
85
|
+
| (distribution: distribution, min_samples: Integer, acceptable_pause: Integer) -> Config
|
|
86
|
+
# A copy that tolerates `pause` of extra silence.
|
|
87
|
+
def with_acceptable_pause: (Integer pause) -> Config
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Mirrors the core's `Beat::Recorded` (`Beat::recorded`): interval >= 1,
|
|
91
|
+
# samples in 1..WINDOW.
|
|
92
|
+
class Recorded < Data
|
|
93
|
+
attr_reader interval: Integer
|
|
94
|
+
attr_reader samples: Integer
|
|
95
|
+
def self.new: (Integer interval, Integer samples) -> Recorded
|
|
96
|
+
| (interval: Integer, samples: Integer) -> Recorded
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Mirrors the core's `Suspicion::WarmingUp` (`Suspicion::warming_up`):
|
|
100
|
+
# samples < required <= WINDOW.
|
|
101
|
+
class WarmingUp < Data
|
|
102
|
+
attr_reader samples: Integer
|
|
103
|
+
attr_reader required: Integer
|
|
104
|
+
attr_reader silence: Integer
|
|
105
|
+
def self.new: (Integer samples, Integer required, Integer silence) -> WarmingUp
|
|
106
|
+
| (samples: Integer, required: Integer, silence: Integer) -> WarmingUp
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Mirrors the core's `Suspicion::Measured`. `phi` must be a Phi.
|
|
110
|
+
class Measured < Data
|
|
111
|
+
attr_reader phi: Phi
|
|
112
|
+
def self.new: (Phi phi) -> Measured
|
|
113
|
+
| (phi: Phi) -> Measured
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# Mirrors the core's `Statistics` (`Statistics::new`): samples in
|
|
117
|
+
# 1..WINDOW, 1 <= min <= max (equal for one sample), a mean within min..max
|
|
118
|
+
# and a positively signed deviation of at most (max - min) / 2 (both stored
|
|
119
|
+
# as Floats).
|
|
120
|
+
class Statistics < Data
|
|
121
|
+
attr_reader samples: Integer
|
|
122
|
+
attr_reader mean_ns: Float
|
|
123
|
+
attr_reader std_dev_ns: Float
|
|
124
|
+
attr_reader min: Integer
|
|
125
|
+
attr_reader max: Integer
|
|
126
|
+
def self.new: (Integer samples, Float | Integer mean_ns, Float | Integer std_dev_ns, Integer min, Integer max)
|
|
127
|
+
-> Statistics
|
|
128
|
+
| (samples: Integer, mean_ns: Float | Integer, std_dev_ns: Float | Integer, min: Integer,
|
|
129
|
+
max: Integer) -> Statistics
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Mirrors the core's `Suspect`: u64 peer and incarnation, and a Phi.
|
|
133
|
+
class Suspect < Data
|
|
134
|
+
attr_reader peer: Integer
|
|
135
|
+
attr_reader incarnation: Integer
|
|
136
|
+
attr_reader phi: Phi
|
|
137
|
+
def self.new: (Integer peer, Integer incarnation, Phi phi) -> Suspect
|
|
138
|
+
| (peer: Integer, incarnation: Integer, phi: Phi) -> Suspect
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# Mirrors the core's `Scan` (`Scan::new`). `suspects` holds the suspects
|
|
142
|
+
# themselves (a frozen copy, at most CAPACITY), where the core's
|
|
143
|
+
# `Scan::suspects` counts those it wrote to the caller's buffer; `next` is
|
|
144
|
+
# nil or below CAPACITY.
|
|
145
|
+
class Scan < Data
|
|
146
|
+
attr_reader suspects: Array[Suspect]
|
|
147
|
+
attr_reader next: Integer?
|
|
148
|
+
def self.new: (Array[Suspect] suspects, Integer? next) -> Scan
|
|
149
|
+
| (suspects: Array[Suspect], next: Integer?) -> Scan
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# One peer on its own.
|
|
153
|
+
class Detector
|
|
154
|
+
WINDOW: Integer
|
|
155
|
+
|
|
156
|
+
# Raises RangeError when `min_samples` exceeds WINDOW.
|
|
157
|
+
def self.new: (Config config) -> Detector
|
|
158
|
+
def window: () -> Integer
|
|
159
|
+
|
|
160
|
+
# Honoured: :first or a Recorded; each records `now` as the last
|
|
161
|
+
# heartbeat (the observation itself). Refused: :time_regression,
|
|
162
|
+
# :same_instant, :overflow (unreachable); a refusal records nothing.
|
|
163
|
+
def heartbeat: (Integer now) -> (:first | Recorded | :time_regression | :same_instant | :overflow)
|
|
164
|
+
|
|
165
|
+
# A query. Honoured: :no_heartbeat, a WarmingUp or a Measured. Refused:
|
|
166
|
+
# :time_regression, :overflow (unreachable).
|
|
167
|
+
def suspicion: (Integer now) -> (suspicion | :time_regression | :overflow)
|
|
168
|
+
def statistics: () -> Statistics?
|
|
169
|
+
def samples: () -> Integer
|
|
170
|
+
def last_heartbeat: () -> Integer?
|
|
171
|
+
def config: () -> Config
|
|
172
|
+
|
|
173
|
+
# Forgets every interval and the last heartbeat. Always honoured.
|
|
174
|
+
def reset: () -> nil
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
class Roster
|
|
178
|
+
CAPACITY: Integer
|
|
179
|
+
WINDOW: Integer
|
|
180
|
+
|
|
181
|
+
# Raises RangeError when `min_samples` exceeds WINDOW.
|
|
182
|
+
def self.new: (Config config) -> Roster
|
|
183
|
+
def capacity: () -> Integer
|
|
184
|
+
def window: () -> Integer
|
|
185
|
+
def size: () -> Integer
|
|
186
|
+
def empty?: () -> bool
|
|
187
|
+
def config: () -> Config
|
|
188
|
+
|
|
189
|
+
# Honoured: :first, a Recorded, or :restarted for a newer incarnation;
|
|
190
|
+
# each records `now` as the peer's last heartbeat. Refused:
|
|
191
|
+
# :time_regression, :same_instant, :stale, :full, :overflow (unreachable).
|
|
192
|
+
def heartbeat: (Integer peer, Integer incarnation, Integer now)
|
|
193
|
+
-> (beat | :time_regression | :same_instant | :stale | :full | :overflow)
|
|
194
|
+
|
|
195
|
+
# A query. Honoured: a WarmingUp or a Measured; :no_heartbeat is the core's
|
|
196
|
+
# remaining variant, which a roster never answers because a tracked peer
|
|
197
|
+
# always has a heartbeat. Refused: :untracked, :time_regression, :overflow
|
|
198
|
+
# (unreachable).
|
|
199
|
+
def suspicion: (Integer peer, Integer now)
|
|
200
|
+
-> (suspicion | :untracked | :time_regression | :overflow)
|
|
201
|
+
|
|
202
|
+
# A query. `threshold` must be a Threshold. `limit` is the length of the
|
|
203
|
+
# core's output buffer, 0..CAPACITY. Honoured: a Scan; a peer whose last
|
|
204
|
+
# heartbeat is later than `now` is skipped, not refused. Refused:
|
|
205
|
+
# :overflow (unreachable).
|
|
206
|
+
def scan: (Integer now, Threshold threshold, Integer cursor, Integer limit) -> (Scan | :overflow)
|
|
207
|
+
def statistics: (Integer peer) -> Statistics?
|
|
208
|
+
def incarnation: (Integer peer) -> Integer?
|
|
209
|
+
def last_heartbeat: (Integer peer) -> Integer?
|
|
210
|
+
|
|
211
|
+
# Never refuses. :not_tracked is the idempotent repeat: the peer was
|
|
212
|
+
# already absent and nothing changed.
|
|
213
|
+
def forget: (Integer peer) -> (:forgotten | :not_tracked)
|
|
214
|
+
end
|
|
215
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: suspect_machines
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.5.3
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Abdelkader Boudih
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rb_sys
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0.9'
|
|
19
|
+
- - ">="
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: 0.9.130
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - "~>"
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '0.9'
|
|
29
|
+
- - ">="
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: 0.9.130
|
|
32
|
+
description: 'Records peer heartbeats and reports how suspicious each silence is as
|
|
33
|
+
a phi value scaled to that peer''s own rhythm. Advisory only: it never declares
|
|
34
|
+
a peer dead.'
|
|
35
|
+
email:
|
|
36
|
+
- oss@seuros.com
|
|
37
|
+
executables: []
|
|
38
|
+
extensions:
|
|
39
|
+
- ext/suspect_machines_native/extconf.rb
|
|
40
|
+
extra_rdoc_files: []
|
|
41
|
+
files:
|
|
42
|
+
- CHANGELOG.md
|
|
43
|
+
- Cargo.lock
|
|
44
|
+
- Cargo.toml
|
|
45
|
+
- LICENSE.txt
|
|
46
|
+
- README.md
|
|
47
|
+
- ext/suspect_machines_native/core/CHANGELOG.md
|
|
48
|
+
- ext/suspect_machines_native/core/Cargo.toml
|
|
49
|
+
- ext/suspect_machines_native/core/README.md
|
|
50
|
+
- ext/suspect_machines_native/core/src/detector.rs
|
|
51
|
+
- ext/suspect_machines_native/core/src/lib.rs
|
|
52
|
+
- ext/suspect_machines_native/core/src/math.rs
|
|
53
|
+
- ext/suspect_machines_native/core/src/roster.rs
|
|
54
|
+
- ext/suspect_machines_native/core/src/test_support.rs
|
|
55
|
+
- ext/suspect_machines_native/core/src/window.rs
|
|
56
|
+
- ext/suspect_machines_native/extconf.rb
|
|
57
|
+
- ext/suspect_machines_native/ffi/Cargo.toml
|
|
58
|
+
- ext/suspect_machines_native/ffi/extconf.rb
|
|
59
|
+
- ext/suspect_machines_native/ffi/src/classes.rs
|
|
60
|
+
- ext/suspect_machines_native/ffi/src/convert.rs
|
|
61
|
+
- ext/suspect_machines_native/ffi/src/detector.rs
|
|
62
|
+
- ext/suspect_machines_native/ffi/src/init.rs
|
|
63
|
+
- ext/suspect_machines_native/ffi/src/lib.rs
|
|
64
|
+
- ext/suspect_machines_native/ffi/src/roster.rs
|
|
65
|
+
- ext/suspect_machines_native/ffi/src/values.rs
|
|
66
|
+
- lib/suspect_machines.rb
|
|
67
|
+
- lib/suspect_machines/version.rb
|
|
68
|
+
- sig/suspect_machines.rbs
|
|
69
|
+
homepage: https://github.com/seuros/suspect_machines
|
|
70
|
+
licenses:
|
|
71
|
+
- MIT
|
|
72
|
+
metadata:
|
|
73
|
+
source_code_uri: https://github.com/seuros/suspect_machines
|
|
74
|
+
rubygems_mfa_required: 'true'
|
|
75
|
+
cargo_crate_name: suspect_machines_native
|
|
76
|
+
cargo_manifest_path: ext/suspect_machines_native/ffi/Cargo.toml
|
|
77
|
+
rdoc_options: []
|
|
78
|
+
require_paths:
|
|
79
|
+
- lib
|
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
81
|
+
requirements:
|
|
82
|
+
- - ">="
|
|
83
|
+
- !ruby/object:Gem::Version
|
|
84
|
+
version: 4.0.0
|
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '0'
|
|
90
|
+
requirements: []
|
|
91
|
+
rubygems_version: 4.0.20
|
|
92
|
+
specification_version: 4
|
|
93
|
+
summary: Phi-accrual failure detection over a native Rust core
|
|
94
|
+
test_files: []
|