store-io-engine 0.6.0

The store-io engine: volumes, regions, barriers with flush sharing, receipts, provisioning and A/B slot objects over any store-io platform.
Documentation
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! Who a finished flush wakes (D-125, D-126): every barrier it covered, all
//! at once, and the designate, which may lead the next flush on the slot
//! freed; a barrier whose bucket held another need, or that leads only on
//! slot 0, is woken from the shared word; poison wakes every bucket. Tests build with two buckets, so needs one apart use
//! different buckets and needs two apart share one. No test joins a thread:
//! outcomes come over channels with a timeout, so a stranded barrier fails
//! its test instead of hanging the run.

use super::*;
use std::sync::Arc;
use std::sync::mpsc;
use std::time::Duration;

const LONG: Duration = Duration::from_secs(10);

fn until_waiting(d: &Domain, n: u32) {
    let start = std::time::Instant::now();
    while d.waiting() < n {
        assert!(start.elapsed() < LONG, "never registered");
        std::thread::yield_now();
    }
}

fn until_issued(d: &Domain, n: u64) {
    let start = std::time::Instant::now();
    while d.issued.load(Ordering::SeqCst) < n {
        assert!(start.elapsed() < LONG, "never issued");
        std::thread::yield_now();
    }
}

/// A leader for `need` whose flush runs until `go` is sent; its outcome
/// arrives on the returned channel (never joined: a stranded thread must
/// fail the test, not hang it).
fn held_leader(d: &Arc<Domain>, need: u64) -> (mpsc::Sender<()>, mpsc::Receiver<bool>) {
    let (go, wait_go) = mpsc::channel::<()>();
    let (tx, rx) = mpsc::channel();
    let d = Arc::clone(d);
    let _h = std::thread::spawn(move || {
        let r = d.barrier(need, |_, _| {
            // Longer than any wait of the test: a flush released by its
            // own timeout must never rescue a stranded barrier in time
            // (it made a mutant survive at random).
            let _released = wait_go.recv_timeout(3 * LONG);
            Ok(())
        });
        let _sent = tx.send(r.is_ok());
    });
    (go, rx)
}

/// A barrier for `need` on another thread; its outcome arrives on the
/// returned channel.
fn barrier(d: &Arc<Domain>, need: u64) -> mpsc::Receiver<bool> {
    let (tx, rx) = mpsc::channel();
    let d = Arc::clone(d);
    let _h = std::thread::spawn(move || {
        let _sent = tx.send(d.barrier(need, |_, _| Ok(())).is_ok());
    });
    rx
}

/// Three barriers covered by one flush in flight sleep in one bucket; the
/// leader wakes them all with one wake, so all three return.
#[test]
fn test_every_barrier_a_flush_covered_is_woken() {
    let d = Arc::new(Domain::new());
    let need = d.ticket();
    let (go, leader) = held_leader(&d, need);
    until_issued(&d, 1);
    let followers: Vec<_> = (0..3).map(|_| barrier(&d, need)).collect();
    until_waiting(&d, 3);
    let _sent = go.send(());
    for f in &followers {
        assert_eq!(f.recv_timeout(LONG), Ok(true), "a covered barrier slept on");
    }
    assert_eq!(leader.recv_timeout(LONG), Ok(true));
    assert_eq!(d.waiting(), 0);
}

/// Two slots: flush 2 finishes before flush 1. When flush 1 finishes,
/// `done` moves past both, and the barriers of both needs are woken.
#[test]
fn test_a_jump_past_several_flushes_wakes_each_bucket() {
    let d = Arc::new(Domain::with_slots(2));
    let first = d.ticket();
    let (go_first, l1) = held_leader(&d, first);
    until_issued(&d, 1);
    let second = d.ticket();
    let (go_second, l2) = held_leader(&d, second);
    until_issued(&d, 2);
    let f1 = barrier(&d, first);
    let f2 = barrier(&d, second);
    until_waiting(&d, 2);
    let _sent = go_second.send(());
    // Flush 2 succeeded, but flush 1 is unfinished: nothing is covered, and
    // the second leader waits too.
    until_waiting(&d, 3);
    assert_eq!(d.done.load(Ordering::SeqCst), 0);
    let _sent = go_first.send(());
    assert_eq!(f1.recv_timeout(LONG), Ok(true), "need 1 slept on");
    assert_eq!(f2.recv_timeout(LONG), Ok(true), "need 2 slept on");
    assert_eq!(l1.recv_timeout(LONG), Ok(true));
    assert_eq!(l2.recv_timeout(LONG), Ok(true));
    assert_eq!(d.done.load(Ordering::SeqCst), 2);
}

/// One slot: a barrier needing the next flush sleeps while the slot is
/// busy, and the leader, finishing, wakes it to lead that flush.
#[test]
fn test_a_barrier_needing_the_next_flush_is_woken_to_lead_it() {
    let d = Arc::new(Domain::new());
    let first = d.ticket();
    let (go, leader) = held_leader(&d, first);
    until_issued(&d, 1);
    let next = d.ticket();
    assert_eq!(next, 2);
    let f = barrier(&d, next);
    until_waiting(&d, 1);
    let _sent = go.send(());
    assert_eq!(f.recv_timeout(LONG), Ok(true), "the next flush never led");
    assert_eq!(leader.recv_timeout(LONG), Ok(true));
    assert_eq!(d.done.load(Ordering::SeqCst), 2);
}

/// A barrier whose bucket holds another need sleeps on the shared word
/// and is woken when its flush finishes. Three slots: flushes 1 to 3 run; a
/// barrier needing flush 3 sleeps first (bucket 1); one needing flush 1,
/// two apart (bucket 1 too), sleeps after it and must return as soon as
/// flush 1 finishes, with flushes 2 and 3 still running.
#[test]
fn test_a_barrier_whose_bucket_holds_another_need_is_still_woken() {
    let d = Arc::new(Domain::with_slots(3));
    let first = d.ticket();
    let (go_first, l1) = held_leader(&d, first);
    until_issued(&d, 1);
    let (go_second, l2) = held_leader(&d, d.ticket());
    until_issued(&d, 2);
    let third_need = d.ticket();
    let (go_third, l3) = held_leader(&d, third_need);
    until_issued(&d, 3);
    let third = barrier(&d, third_need);
    until_waiting(&d, 1);
    let late = barrier(&d, first);
    until_waiting(&d, 2);
    assert_eq!(d.lock().shared, 1, "the late barrier shares no bucket");
    let _sent = go_first.send(());
    assert_eq!(
        late.recv_timeout(LONG),
        Ok(true),
        "need 1 slept past its flush"
    );
    let _sent = go_second.send(());
    let _sent = go_third.send(());
    assert_eq!(third.recv_timeout(LONG), Ok(true));
    assert_eq!(l1.recv_timeout(LONG), Ok(true));
    assert_eq!(l2.recv_timeout(LONG), Ok(true));
    assert_eq!(l3.recv_timeout(LONG), Ok(true));
}

/// One slot: of several barriers needing the next flush, the first sleeps
/// as the designate and the rest in that flush's bucket; when the slot is
/// freed only the designate is woken, leads, and its flush wakes the rest.
#[test]
fn test_the_designate_leads_the_next_flush_for_its_bucket() {
    let d = Arc::new(Domain::new());
    let (go, leader) = held_leader(&d, d.ticket());
    until_issued(&d, 1);
    let next = d.ticket();
    let first = barrier(&d, next);
    until_waiting(&d, 1);
    assert_eq!(d.lock().designate, Some(next));
    let rest: Vec<_> = (0..3).map(|_| barrier(&d, next)).collect();
    until_waiting(&d, 4);
    let b = (next % BUCKETS as u64) as usize;
    assert_eq!(
        d.lock().buckets[b].waiting,
        3,
        "the rest sleep in the bucket"
    );
    let _sent = go.send(());
    assert_eq!(first.recv_timeout(LONG), Ok(true));
    for r in &rest {
        assert_eq!(r.recv_timeout(LONG), Ok(true), "a covered barrier slept on");
    }
    assert_eq!(leader.recv_timeout(LONG), Ok(true));
    assert_eq!(d.stats().flushes, 2, "the next flush was led once");
    assert_eq!(d.waiting(), 0);
}

/// Two slots: a barrier that can lead only on slot 0 and a data barrier,
/// both needing the next flush, sleep while both slots are busy. When the
/// flush on slot 1 finishes, the data barrier leads on it at once, though
/// slot 0 is still busy and the slot-0 barrier slept first.
#[test]
fn test_the_next_flush_is_led_by_a_barrier_that_can_use_the_freed_slot() {
    let d = Arc::new(Domain::with_slots(2));
    let (go_first, l1) = held_leader(&d, d.ticket());
    until_issued(&d, 1);
    let (go_second, l2) = held_leader(&d, d.ticket());
    until_issued(&d, 2);
    let need = d.ticket();
    let (tx, main) = mpsc::channel();
    {
        let d = Arc::clone(&d);
        let _h = std::thread::spawn(move || {
            let _sent = tx.send(d.barrier_main(need, || Ok(())).is_ok());
        });
    }
    until_waiting(&d, 1);
    let data = barrier(&d, need);
    until_waiting(&d, 2);
    let _sent = go_first.send(());
    // Flush 3 is issued on slot 1 while flush 2 still holds slot 0.
    until_issued(&d, 3);
    let _sent = go_second.send(());
    assert_eq!(data.recv_timeout(LONG), Ok(true));
    assert_eq!(main.recv_timeout(LONG), Ok(true));
    assert_eq!(l1.recv_timeout(LONG), Ok(true));
    assert_eq!(l2.recv_timeout(LONG), Ok(true));
}

/// A wake of a bucket's word that deregistered nobody (as a spurious
/// return of the OS's wait would be) leaves its barrier asleep: it does
/// not leave its bed, so it never registers twice.
#[test]
fn test_a_wake_that_deregisters_nobody_leaves_the_barrier_asleep() {
    let d = Arc::new(Domain::new());
    let need = d.ticket();
    let (go, leader) = held_leader(&d, need);
    until_issued(&d, 1);
    let f = barrier(&d, need);
    until_waiting(&d, 1);
    // Long enough to be inside the OS's wait.
    std::thread::sleep(Duration::from_millis(20));
    d.bucket_words[(need % BUCKETS as u64) as usize].wake_all();
    std::thread::sleep(Duration::from_millis(50));
    assert_eq!(d.waiting(), 1, "a barrier left its bed unregistered");
    assert_eq!(f.try_recv(), Err(mpsc::TryRecvError::Empty));
    let _sent = go.send(());
    assert_eq!(f.recv_timeout(LONG), Ok(true));
    assert_eq!(leader.recv_timeout(LONG), Ok(true));
}

/// Three slots, flushes finishing 3, 2, then 1: `done` jumps from 0 to 3,
/// more needs than there are buckets (2 in tests), and every covered need
/// is woken, the one whose bucket held another need included.
#[test]
fn test_a_jump_past_more_needs_than_buckets_wakes_every_need() {
    let d = Arc::new(Domain::with_slots(3));
    let needs: Vec<u64> = (0..3).map(|i| i + d.ticket()).collect();
    let mut leaders = Vec::new();
    for (i, &need) in needs.iter().enumerate() {
        leaders.push(held_leader(&d, need));
        until_issued(&d, i as u64 + 1);
    }
    let followers: Vec<_> = needs.iter().map(|&n| barrier(&d, n)).collect();
    until_waiting(&d, 3);
    // Flush 3, then 2: each leader, its flush done but `done` still 0,
    // parks too; then flush 1 moves `done` from 0 to 3 at once.
    for (parked, (go, _)) in (4..).zip(leaders.iter().rev().take(2)) {
        let _sent = go.send(());
        until_waiting(&d, parked);
    }
    assert_eq!(d.done.load(Ordering::SeqCst), 0);
    let _sent = leaders[0].0.send(());
    for f in &followers {
        assert_eq!(f.recv_timeout(LONG), Ok(true), "a covered need slept on");
    }
    for (_, l) in &leaders {
        assert_eq!(l.recv_timeout(LONG), Ok(true));
    }
    assert_eq!(d.done.load(Ordering::SeqCst), 3);
    assert_eq!(d.waiting(), 0);
}

/// Poison wakes the barriers asleep in a bucket, and they fail.
#[test]
fn test_poison_wakes_barriers_asleep_in_a_bucket() {
    let d = Arc::new(Domain::new());
    let need = d.ticket();
    let (go, leader) = held_leader(&d, need);
    until_issued(&d, 1);
    let followers: Vec<_> = (0..2).map(|_| barrier(&d, need)).collect();
    until_waiting(&d, 2);
    d.poison(FirstCause {
        op: None,
        raw: None,
    });
    for f in &followers {
        assert_eq!(
            f.recv_timeout(LONG),
            Ok(false),
            "a poisoned barrier slept on"
        );
    }
    let _sent = go.send(());
    assert!(leader.recv_timeout(LONG).is_ok());
}

/// Yielding is learned: `MISSES` waits in a row that ran out stop it, the
/// next `SKIPS` waits do not yield, one then tries, and a miss on that try
/// stops it again at once.
#[test]
fn test_waits_stop_yielding_after_misses_and_try_again_once() {
    let p = Patience::default();
    for _ in 0..MISSES {
        assert!(p.try_yield(), "stopped before its misses");
        p.ran_out();
    }
    for _ in 0..SKIPS {
        assert!(!p.try_yield(), "yielded during the skips");
    }
    assert!(p.try_yield(), "no try after the skips");
    p.ran_out();
    assert!(!p.try_yield(), "a miss on the try did not stop it again");
}

/// Yields that were enough forget the misses: only misses in a row stop
/// yielding.
#[test]
fn test_a_wait_that_reached_its_goal_forgets_the_misses() {
    let p = Patience::default();
    for _ in 0..MISSES - 1 {
        p.ran_out();
    }
    p.reached();
    for _ in 0..MISSES - 1 {
        p.ran_out();
    }
    assert!(p.try_yield(), "misses that were not in a row stopped it");
}

/// A wait with a budget yields past its count until its budget has passed
/// (D-155): its goal, reached on the clock's tenth read, is reached.
#[test]
fn test_a_wait_with_a_budget_yields_past_its_count() {
    let p = Patience::default();
    let (t, checks) = (core::cell::Cell::new(0u64), core::cell::Cell::new(0u32));
    let now = || {
        t.set(t.get() + 1000);
        t.get()
    };
    let reached = || {
        checks.set(checks.get() + 1);
        t.get() >= 10_000
    };
    assert!(
        p.yield_within(0, 50_000, now, reached),
        "stopped at its count"
    );
    assert!(checks.get() > YIELDS, "never yielded past its count");
    assert_eq!(p.missed.load(core::sync::atomic::Ordering::Relaxed), 0);
}

/// A budget is capped at `BUDGET_MAX_NS`: a goal never reached stops the
/// yields there, a miss.
#[test]
fn test_a_wait_with_a_budget_stops_at_its_cap() {
    let p = Patience::default();
    let (t, checks) = (core::cell::Cell::new(0u64), core::cell::Cell::new(0u32));
    let now = || {
        t.set(t.get() + 1000);
        t.get()
    };
    let reached = || {
        checks.set(checks.get() + 1);
        false
    };
    assert!(!p.yield_within(0, u64::MAX, now, reached));
    let most = YIELDS + (BUDGET_MAX_NS / 1000) as u32 + 2;
    assert!(
        checks.get() <= most,
        "{} checks: past the cap",
        checks.get()
    );
    assert_eq!(p.missed.load(core::sync::atomic::Ordering::Relaxed), 1);
}

/// A clock that does not move still ends the yields, at `YIELDS_MAX`.
#[test]
fn test_a_wait_with_a_budget_on_a_still_clock_ends() {
    let p = Patience::default();
    let checks = core::cell::Cell::new(0u32);
    let reached = || {
        checks.set(checks.get() + 1);
        assert!(checks.get() <= YIELDS_MAX + 8, "yielded past YIELDS_MAX");
        false
    };
    assert!(!p.yield_within(0, 50_000, || 0, reached));
    assert!(checks.get() > YIELDS_MAX, "stopped before YIELDS_MAX");
}

/// A budget of 0 waits as `yield_until`, its `YIELDS`, and reads no clock.
#[test]
fn test_a_wait_with_no_budget_reads_no_clock() {
    let p = Patience::default();
    let checks = core::cell::Cell::new(0u32);
    let reached = || {
        checks.set(checks.get() + 1);
        false
    };
    let reads = core::cell::Cell::new(0u32);
    let now = || {
        reads.set(reads.get() + 1);
        0
    };
    assert!(!p.yield_within(0, 0, now, reached));
    assert_eq!(checks.get(), YIELDS + 2);
    assert_eq!(reads.get(), 0, "read the clock with no budget");
}

fn missed(d: &Domain) -> u32 {
    d.patience
        .missed
        .load(core::sync::atomic::Ordering::Relaxed)
}

/// Yields for a flush that does not finish count as a miss; a goal already
/// reached forgets it, and a poisoned domain is no miss (nothing to wait
/// for).
#[test]
fn test_yields_that_run_out_are_counted() {
    let d = Domain::new();
    d.yield_to(1);
    assert_eq!(missed(&d), 1, "ran out, and not counted");
    d.yield_to(0);
    assert_eq!(missed(&d), 0, "reached, and not forgotten");
    d.poison(FirstCause {
        op: None,
        raw: None,
    });
    d.yield_to(1);
    assert_eq!(missed(&d), 0, "poison counted as a miss");
}

/// A barrier needing the next flush yields for the flush before it (done
/// here), not for its own, which only it may lead: no miss, and it then
/// sleeps as the designate until a slot is free.
#[test]
fn test_a_barrier_needing_the_next_flush_yields_for_the_one_before_it() {
    let d = Arc::new(Domain::new());
    assert!(d.barrier(d.ticket(), |_, _| Ok(())).is_ok());
    let need = d.ticket();
    assert!(d.hold_leadership());
    let r = barrier(&d, need);
    until_waiting(&d, 1);
    assert_eq!(missed(&d), 0, "yielded for a flush only it could lead");
    d.drop_leadership();
    assert_eq!(r.recv_timeout(LONG), Ok(true));
}

/// While waits skip yielding, a waiting barrier goes straight to sleep, and
/// that wait is counted off.
#[test]
fn test_a_wait_in_the_skips_sleeps_without_yielding() {
    let d = Arc::new(Domain::new());
    for _ in 0..MISSES {
        d.patience.ran_out();
    }
    let need = d.ticket();
    let (go, leader) = held_leader(&d, need);
    until_issued(&d, 1);
    let r = barrier(&d, need);
    until_waiting(&d, 1);
    let left = d.patience.skip.load(core::sync::atomic::Ordering::Relaxed);
    assert_eq!(left, SKIPS - 1, "yielded in the skips");
    let _sent = go.send(());
    assert_eq!(r.recv_timeout(LONG), Ok(true));
    assert_eq!(leader.recv_timeout(LONG), Ok(true));
}