1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//! What reaches a turn while it is running.
//!
//! One agent per print at a time. An event that arrives for a print whose turn
//! is still running does not start a second turn: it is written down like any
//! other and then handed to the turn already running, which takes it the next
//! time it looks at the print. Whatever that turn never took is handed, when it
//! returns, to one more turn in the same session — so nothing that arrived is
//! lost and no two agents ever disagree about one machine.
//!
//! This is a blocking structure on purpose. A look waits on it for up to a
//! minute and a half, and the caller is expected to wait on a thread of its
//! own rather than on an asynchronous worker, exactly as the harness itself is
//! driven.
use std::collections::BTreeMap;
use std::sync::{Condvar, Mutex, MutexGuard};
use std::time::{Duration, Instant};
use printobserver_types::{EventRecord, ImageRef, PrintId};
use printobserver_vision_api::Detection;
/// One event waiting for a running turn, with what the detector did about it.
#[derive(Debug, Clone, PartialEq)]
pub struct Arrival {
/// When the frame an alert brought was captured; the event's own payload says
/// it for a check or a look, whose frame it was written down with.
pub frame_captured_at: Option<printobserver_types::Timestamp>,
/// The event, as it was written down, carrying its image.
pub event: EventRecord,
/// What the detector did, when the event is one of its detections.
pub detection: Option<Detection>,
/// What came of asking the server's own camera for a frame as a detector's
/// alert arrived, which its check is composed from where it can be.
pub camera_frame: AlertCameraFrame,
}
/// What came of asking the server's own camera for a frame as a detector's
/// alert arrived.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum AlertCameraFrame {
/// Nothing was asked: the event is no detector's alert, or its check could
/// not be composed whatever frame it had.
#[default]
NotAsked,
/// The camera's frame, stored against the alert, and when it was captured.
Had {
/// The stored frame.
image: ImageRef,
/// When it was captured.
captured_at: printobserver_types::Timestamp,
},
/// The camera gave no frame, or it could not be stored, and why.
Missing(String),
}
/// One print's inbox.
#[derive(Debug, Default)]
struct Inbox {
/// Whether a turn is running for this print.
busy: bool,
/// What arrived while it was running and it has not taken, oldest first.
pending: Vec<Arrival>,
}
/// Every print's inbox, and the signal an arrival raises.
#[derive(Debug, Default)]
pub struct Inboxes {
/// One inbox per print that has a turn running.
state: Mutex<BTreeMap<PrintId, Inbox>>,
/// Raised whenever something is handed to a running turn.
arrived: Condvar,
}
impl Inboxes {
/// Every inbox, held.
fn held(&self) -> MutexGuard<'_, BTreeMap<PrintId, Inbox>> {
self.state
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
/// Claim the print's turn for this arrival, or hand it to the turn already
/// running.
///
/// Answers `true` when the caller now holds the print's turn and must run
/// it, and `false` when the arrival was handed to a turn already running.
pub fn claim_or_hand_over(&self, print_id: PrintId, arrival: Arrival) -> bool {
let mut state = self.held();
let inbox = state.entry(print_id).or_default();
if inbox.busy {
inbox.pending.push(arrival);
drop(state);
self.arrived.notify_all();
return false;
}
inbox.busy = true;
true
}
/// Claim the print's turn only when nothing holds it.
///
/// Answers `true` when the caller now holds the print's turn and must run
/// it, and `false` — handing nothing over — when a turn is already running
/// for it. What the periodic observation claims with: an observation is
/// owed to a print nobody is watching, and one queued behind a running turn
/// would only pile up beside what that turn is already looking at.
pub fn claim_if_idle(&self, print_id: PrintId) -> bool {
let mut state = self.held();
let inbox = state.entry(print_id).or_default();
if inbox.busy {
return false;
}
inbox.busy = true;
true
}
/// What the turn that just returned never took, oldest first, or — when it
/// took everything — nothing, with the print's turn released.
pub fn next_or_release(&self, print_id: PrintId) -> Option<Vec<Arrival>> {
let mut state = self.held();
let taken = state
.get_mut(&print_id)
.map(|inbox| core::mem::take(&mut inbox.pending))
.filter(|left| !left.is_empty());
if taken.is_none() {
state.remove(&print_id);
}
taken
}
/// Hand what a look took back to the print's running turn, ahead of
/// anything that arrived since, because the look that took it delivered
/// nothing.
///
/// A print with no turn running any more has nothing to hand it to: its
/// turn released it, and what was waiting went with it.
pub fn put_back(&self, print_id: PrintId, taken: Vec<Arrival>) {
if taken.is_empty() {
return;
}
let mut state = self.held();
if let Some(inbox) = state.get_mut(&print_id) {
let since = core::mem::replace(&mut inbox.pending, taken);
inbox.pending.extend(since);
drop(state);
self.arrived.notify_all();
}
}
/// Release the print's turn, dropping anything still waiting for it.
///
/// For a print that has ended: nothing waiting for it can be acted on.
pub fn release(&self, print_id: PrintId) {
self.held().remove(&print_id);
}
/// Wait up to `wait` for anything to be handed to the print's running
/// turn, and take it.
///
/// Answers at once when something is already waiting, and answers nothing
/// after the whole wait when nothing arrives. A print with no turn running
/// has nothing handed to it, because an event for it claims a turn of its
/// own instead; a look at it simply waits.
pub fn wait_and_take(&self, print_id: PrintId, wait: Duration) -> Vec<Arrival> {
let deadline = Instant::now() + wait;
let mut state = self.held();
loop {
if let Some(inbox) = state.get_mut(&print_id)
&& !inbox.pending.is_empty()
{
return core::mem::take(&mut inbox.pending);
}
let now = Instant::now();
if now >= deadline {
return Vec::new();
}
state = self
.arrived
.wait_timeout(state, deadline - now)
.unwrap_or_else(std::sync::PoisonError::into_inner)
.0;
}
}
}
#[cfg(test)]
mod tests {
use super::{Arrival, Inboxes};
use printobserver_types::{
EventBody, EventId, EventKind, EventRecord, EventSource, PrintId, Timestamp,
};
use std::time::{Duration, Instant};
/// One arrival, of no kind in particular.
fn arrival() -> Arrival {
Arrival {
frame_captured_at: None,
event: EventRecord {
id: EventId::new(),
print_id: None,
source: EventSource::new("obico"),
received_at: Timestamp::now(),
image: None,
body: EventBody {
kind: EventKind::new("obico_failure_alert").expect("a kind"),
payload: printobserver_types::serde_json::Value::Null,
},
raw: None,
},
detection: None,
camera_frame: super::AlertCameraFrame::NotAsked,
}
}
/// A second arrival while a turn runs is handed over rather than claimed,
/// is what the turn's next look takes, and the turn is released once
/// nothing is left.
#[test]
fn an_arrival_during_a_turn_is_handed_to_it() {
let inboxes = Inboxes::default();
let print = PrintId::new();
assert!(inboxes.claim_or_hand_over(print, arrival()));
assert!(!inboxes.claim_or_hand_over(print, arrival()));
assert_eq!(inboxes.wait_and_take(print, Duration::ZERO).len(), 1);
assert_eq!(inboxes.next_or_release(print), None);
assert!(inboxes.claim_or_hand_over(print, arrival()));
}
/// What a turn never took is handed to the next turn, not dropped, and the
/// print stays claimed while that turn runs.
#[test]
fn what_a_turn_never_took_is_handed_to_the_next() {
let inboxes = Inboxes::default();
let print = PrintId::new();
assert!(inboxes.claim_or_hand_over(print, arrival()));
assert!(!inboxes.claim_or_hand_over(print, arrival()));
assert_eq!(
inboxes.next_or_release(print).map(|left| left.len()),
Some(1)
);
assert!(!inboxes.claim_or_hand_over(print, arrival()));
}
/// Releasing a print drops what was waiting, and its next event claims a
/// turn of its own.
#[test]
fn a_released_print_drops_what_was_waiting() {
let inboxes = Inboxes::default();
let print = PrintId::new();
assert!(inboxes.claim_or_hand_over(print, arrival()));
assert!(!inboxes.claim_or_hand_over(print, arrival()));
inboxes.release(print);
assert!(inboxes.wait_and_take(print, Duration::ZERO).is_empty());
assert!(inboxes.claim_or_hand_over(print, arrival()));
}
/// An idle claim takes a print nobody holds and is refused one a turn
/// holds, handing nothing to that turn; an event that arrives after it is
/// handed to the turn it claimed.
#[test]
fn an_idle_claim_hands_nothing_to_a_running_turn() {
let inboxes = Inboxes::default();
let print = PrintId::new();
assert!(inboxes.claim_or_hand_over(print, arrival()));
assert!(!inboxes.claim_if_idle(print));
assert!(inboxes.wait_and_take(print, Duration::ZERO).is_empty());
assert_eq!(inboxes.next_or_release(print), None);
assert!(inboxes.claim_if_idle(print));
assert!(!inboxes.claim_if_idle(print));
assert!(!inboxes.claim_or_hand_over(print, arrival()));
assert_eq!(inboxes.wait_and_take(print, Duration::ZERO).len(), 1);
}
/// What a look took and could not deliver goes back ahead of what arrived
/// since, and a print with no turn running takes nothing back.
#[test]
fn what_a_look_could_not_deliver_goes_back_first() {
let inboxes = Inboxes::default();
let print = PrintId::new();
assert!(inboxes.claim_or_hand_over(print, arrival()));
let first = arrival();
assert!(!inboxes.claim_or_hand_over(print, first.clone()));
let taken = inboxes.wait_and_take(print, Duration::ZERO);
let since = arrival();
assert!(!inboxes.claim_or_hand_over(print, since.clone()));
inboxes.put_back(print, taken);
assert_eq!(
inboxes.wait_and_take(print, Duration::ZERO),
vec![first, since]
);
inboxes.release(print);
inboxes.put_back(print, vec![arrival()]);
assert!(inboxes.claim_or_hand_over(print, arrival()));
assert!(inboxes.wait_and_take(print, Duration::ZERO).is_empty());
}
/// A wait returns the moment something is handed over, rather than at the
/// end of the wait.
#[test]
fn a_wait_returns_early_on_an_arrival() {
let inboxes = Inboxes::default();
let print = PrintId::new();
assert!(inboxes.claim_or_hand_over(print, arrival()));
let started = Instant::now();
std::thread::scope(|scope| {
let waiting = scope.spawn(|| inboxes.wait_and_take(print, Duration::from_secs(30)));
std::thread::sleep(Duration::from_millis(50));
assert!(!inboxes.claim_or_hand_over(print, arrival()));
assert_eq!(waiting.join().expect("the wait ends").len(), 1);
});
assert!(started.elapsed() < Duration::from_secs(10));
}
}