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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
//! `StoreSet` (R5.3, D-144): stores on several devices made durable
//! together, one flush per store and the flushes in parallel, with no format
//! change and no lock across stores (MDEV-02): each store keeps its own
//! domain, flush thread, poison and receipts.
//!
//! # A set barrier
//!
//! 1. Each ticket is matched to its store by its volume and open, and its
//!    region found among the store's live regions (released: `NotReady`;
//!    recycled or retiring: `StaleGeneration`).
//! 2. A store is **ready** once every append ticket's region has completed
//!    through the ticket's end. Each append ticket's completed prefix is
//!    snapshotted then, and the store's `need` is a domain ticket taken
//!    **after** the last snapshot, so its flush covers every snapshotted
//!    byte; a store with only page tickets is ready at once, `need` the
//!    largest of theirs. Ready stores are handed off as each is found ready:
//!    the call waits on one remaining store at a time and sweeps them all at
//!    every wake.
//! 3. A store needs **no flush** when it is power-safe with no ticket on a
//!    region forcing data sync (D-102), when a finished flush already covers
//!    `need`, or when every ticket is already durable; such a store is
//!    answered only once ready (a prefix receipt short of a ticket's end
//!    would still read as covering it).
//! 4. Every store needing a flush but the last is handed to its own flush
//!    thread, the request answered into the call's [`Answers`]; the calling
//!    thread leads the last store's flush itself, then yields for its
//!    answers about as long as its own flush took (at most 200 us, D-155;
//!    under the set's `Patience`, D-139), then sleeps. A store whose
//!    flush thread cannot start is flushed on the calling thread, and a hung
//!    flush the caller leads holds the call (no poison releases a system
//!    call).
//! 5. The call's `Answers` is registered as a poison waker on every store it
//!    handed off, and the poison of each read after the registration, so a
//!    store poisoned while its flush thread hangs ends the wait.
//! 6. Each ticket's receipt is built by the code that builds it for a single
//!    store (`receipt_for`, `region::sync_tail`), never from a flush number
//!    below the store's `need` (checked in release builds: a violation
//!    poisons that store and fails its tickets). A failed store's tickets
//!    keep a receipt only where what is already published proves them.
//!
//! Concurrent callers of one set are the normal case (a commit layer's
//! committers). Every call has its own answers, from a per-thread pool, and
//! tags its requests with a sequence number from a process-wide counter that
//! is never reset: an outcome of another call (a late one, on a reused
//! `Answers`) is dropped, never taken as this call's flush.

use std::cell::RefCell;
use std::sync::{Arc, PoisonError};

#[cfg(loom)]
use loom::sync::{Condvar, Mutex, MutexGuard};
#[cfg(not(loom))]
use std::sync::{Condvar, Mutex, MutexGuard};

use crate::sync::{AtomicU64, Ordering};

use store_io_core::error::{Error, ErrorContext, FirstCause, NotWrittenCause};
use store_io_platform::{IoBuf, Platform};

use crate::completion::{Rejected, WriteMode};
use crate::domain::{Patience, PoisonWaker};
use crate::flusher::{Answer, Flushed};
use crate::io::not_written;
use crate::receipt::{DurableReceipt, RegionPos, WriteTicket};
use crate::region::{AppendRegion, PageRegion, sync_tail};
use crate::settle::in_range;
use crate::store::{LiveRegion, Store};
use crate::submitter::Submitter;

/// The most stores in a set: a request's tag keeps the store's index in 16
/// bits.
pub const MAX_SET_STORES: usize = 1 << 16;

/// Bits of a tag that hold the store's index; the call's sequence is above.
const INDEX_BITS: u32 = 16;

/// The bits of a call's sequence a tag keeps (the answers compare those
/// alone, so the sequence may wrap without a call missing its outcomes).
const SEQ_MASK: u64 = (1 << (64 - INDEX_BITS)) - 1;

/// A spare grown past this many stores (or tickets) is dropped, not kept:
/// one call on a huge set does not pin its memory to the thread.
const SPARE_KEEP: usize = 1024;

/// The sequence of every set barrier call in the process, never reset: a
/// late outcome of an earlier call never matches a later one.
static CALLS: core::sync::atomic::AtomicU64 = core::sync::atomic::AtomicU64::new(1);

/// Stores on several devices, made durable together (R5.3).
///
/// ```ignore
/// let set = StoreSet::new([a, b])?;
/// let t1 = set[0].append_region("data")?.append(bytes)?;
/// let t2 = set[1].append_region("data")?.append(bytes)?;
/// let receipts = set.barrier(&[&t1, &t2])?; // one flush per store, in parallel
/// ```
pub struct StoreSet<P: Platform> {
    stores: Vec<Store<P>>,
    /// Whether a call yields for its answers before it sleeps (D-139).
    patience: Patience,
    barriers: core::sync::atomic::AtomicU64,
    stores_flushed: core::sync::atomic::AtomicU64,
    sleeps: core::sync::atomic::AtomicU64,
    fallbacks: core::sync::atomic::AtomicU64,
}

impl<P: Platform> core::fmt::Debug for StoreSet<P> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("StoreSet")
            .field("stores", &self.stores.len())
            .finish_non_exhaustive()
    }
}

/// What a set's barriers did ([`StoreSet::stats`]).
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct SetStats {
    /// Set barrier calls.
    pub barriers: u64,
    /// Stores the calls waited on: each call counts once each store with a
    /// ticket that became ready, whether it was flushed, needed no flush,
    /// or failed.
    pub stores: u64,
    /// Times a call slept waiting for a flush thread's answer.
    pub sleeps: u64,
    /// Stores flushed on the calling thread because no flush thread took
    /// them (one could not be started): besides the one store each call
    /// flushes itself.
    pub fallbacks: u64,
}

impl<P: Platform> StoreSet<P>
where
    P::Queue: Send,
{
    /// A set of writable stores (each a handle the caller keeps too). Stores
    /// on one device are allowed, and flushed separately.
    ///
    /// # Errors
    ///
    /// `NotWritten`: `InvalidOptions` for more than [`MAX_SET_STORES`]
    /// stores; `ReadOnly` for a read-only store; `Overlap` for the same open
    /// store twice, or two stores with one volume id (a container copied by
    /// another tool: a receipt could not name which).
    pub fn new<S: Into<Store<P>>>(stores: impl IntoIterator<Item = S>) -> Result<Self, Error> {
        let stores: Vec<Store<P>> = stores.into_iter().map(Into::into).collect();
        let refuse = |cause| not_written(cause, ErrorContext::default());
        if stores.len() > MAX_SET_STORES {
            return Err(refuse(NotWrittenCause::InvalidOptions));
        }
        for (i, s) in stores.iter().enumerate() {
            if s.inner.read_only {
                return Err(refuse(NotWrittenCause::ReadOnly));
            }
            let twice = stores[..i]
                .iter()
                .any(|t| Arc::ptr_eq(&t.inner, &s.inner) || t.inner.volume == s.inner.volume);
            if twice {
                return Err(refuse(NotWrittenCause::Overlap));
            }
        }
        Ok(Self {
            stores,
            patience: Patience::default(),
            barriers: core::sync::atomic::AtomicU64::new(0),
            stores_flushed: core::sync::atomic::AtomicU64::new(0),
            sleeps: core::sync::atomic::AtomicU64::new(0),
            fallbacks: core::sync::atomic::AtomicU64::new(0),
        })
    }

    /// The number of stores.
    #[must_use]
    pub fn len(&self) -> usize {
        self.stores.len()
    }

    /// Whether the set has no store.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.stores.is_empty()
    }

    /// The stores, in set order.
    pub fn iter(&self) -> core::slice::Iter<'_, Store<P>> {
        self.stores.iter()
    }

    /// Whether stores `i` and `j` share no physical device, read afresh
    /// (`Platform::device_identity`: a volume moved online changes it): false
    /// when either identity is incomplete.
    ///
    /// # Panics
    ///
    /// If `i` or `j` is not a store of the set.
    #[must_use]
    pub fn distinct(&self, i: usize, j: usize) -> bool {
        let identity = |s: &Store<P>| {
            let inner = &s.inner;
            inner.platform.device_identity(&inner._dir, &inner.file)
        };
        identity(&self.stores[i]).distinct(&identity(&self.stores[j]))
    }

    /// Writes `bytes` once into each of `regions`, a copy on each device
    /// (API-10): one result per copy, in `regions`' order. The copies are
    /// written one after another, in set order, each by its store's own
    /// blocking append (one lane held at a time, so dittos naming stores in
    /// different orders cannot deadlock); a copy's failure is its store's
    /// (a write failing after submission poisons that store). Make them
    /// durable with [`Self::barrier`], which flushes the stores in
    /// parallel. The quorum is the caller's.
    ///
    /// Every pair of the regions' stores must be on distinct devices, read
    /// afresh ([`Self::distinct`]): two regions of one store, or stores
    /// whose devices' identities are incomplete, are refused. (One region
    /// has no pair: it is written as an `append` would be; none writes
    /// nothing.) A copy its own region refuses (too large, say) is that
    /// copy's result.
    ///
    /// # Errors
    ///
    /// Before any write: `NotWritten(ForeignPosition)` for a region of no
    /// store in the set, `NotWritten(SameDevice)` unless every pair is
    /// distinct.
    pub fn ditto(
        &self,
        regions: &[&AppendRegion<P>],
        bytes: &[u8],
    ) -> Result<Vec<Result<WriteTicket, Error>>, Error> {
        let at = self.ditto_checked(regions.iter().map(|r| r.store()))?;
        Ok(in_set_order(&at, |k| regions[k].append(bytes)))
    }

    /// [`Self::ditto`] for pages: `bytes` written at each position, each
    /// position's region on a distinct device.
    ///
    /// # Errors
    ///
    /// As [`Self::ditto`]; also before any write, a position its region
    /// refuses (`ForeignPosition`, `StaleGeneration`, `OutOfBounds`,
    /// `Misaligned`, `Empty`).
    pub fn ditto_pages(
        &self,
        writes: &[(&PageRegion<P>, RegionPos)],
        bytes: &[u8],
    ) -> Result<Vec<Result<WriteTicket, Error>>, Error> {
        let at = self.ditto_checked(writes.iter().map(|(r, _)| r.store()))?;
        for (r, pos) in writes {
            let _end = r.check(pos, bytes.len())?;
        }
        Ok(in_set_order(&at, |k| writes[k].0.write(writes[k].1, bytes)))
    }

    /// Each copy's store index, once every store is in the set and every
    /// pair distinct (each identity read once for the call).
    fn ditto_checked<'s>(
        &self,
        stores: impl Iterator<Item = &'s Store<P>>,
    ) -> Result<Vec<usize>, Error>
    where
        P: 's,
    {
        let refuse = |cause| not_written(cause, ErrorContext::default());
        let mut at = Vec::new();
        for s in stores {
            let i = self
                .stores
                .iter()
                .position(|t| Arc::ptr_eq(&t.inner, &s.inner))
                .ok_or_else(|| refuse(NotWrittenCause::ForeignPosition))?;
            at.push(i);
        }
        let identities: Vec<_> = at
            .iter()
            .map(|&i| {
                let inner = &self.stores[i].inner;
                inner.platform.device_identity(&inner._dir, &inner.file)
            })
            .collect();
        for (a, x) in identities.iter().enumerate() {
            for y in &identities[a + 1..] {
                if !x.distinct(y) {
                    return Err(refuse(NotWrittenCause::SameDevice));
                }
            }
        }
        Ok(at)
    }

    /// What the set's barriers did so far.
    #[must_use]
    pub fn stats(&self) -> SetStats {
        use core::sync::atomic::Ordering::Relaxed;
        SetStats {
            barriers: self.barriers.load(Relaxed),
            stores: self.stores_flushed.load(Relaxed),
            sleeps: self.sleeps.load(Relaxed),
            fallbacks: self.fallbacks.load(Relaxed),
        }
    }

    /// Makes every ticket durable, each store flushed at most once and the
    /// flushes in parallel; every store is flushed even if one fails.
    /// Returns the receipts in ticket order.
    ///
    /// Like [`AppendRegion::sync_through`], a ticket behind the calling
    /// thread's own unreaped [`Submitter`] appends waits for completions only
    /// that thread can reap.
    ///
    /// # Errors
    ///
    /// The first failing ticket's error (see [`Self::barrier_each`]).
    pub fn barrier(&self, tickets: &[&WriteTicket]) -> Result<Vec<DurableReceipt>, Error> {
        self.barrier_each(tickets)?.into_iter().collect()
    }

    /// [`Self::barrier`] with one result per ticket, for a caller deciding a
    /// quorum: a store's failure is its own (its flush failing poisons that
    /// store only), and the others' receipts stand.
    ///
    /// # Errors
    ///
    /// The outer error only for a ticket of no store in the set
    /// (`NotWritten(ForeignPosition)`), before anything is flushed. Per
    /// ticket: `NotWritten(NotReady)` for a region since released,
    /// `NotWritten(StaleGeneration)` for one since recycled, `Poisoned`,
    /// `DurabilityUnknown` if its store's flush failed (a flush handed to
    /// its store's flush thread may instead answer `Poisoned`, the failed
    /// flush as its first cause, when the call sees the poison first).
    pub fn barrier_each(
        &self,
        tickets: &[&WriteTicket],
    ) -> Result<Vec<Result<DurableReceipt, Error>>, Error> {
        use core::sync::atomic::Ordering::Relaxed;
        let mut call = Call::new(self, tickets)?;
        let _ = self.barriers.fetch_add(1, Relaxed);
        call.run();
        let _ = self.stores_flushed.fetch_add(call.stores_waited(), Relaxed);
        let _ = self.sleeps.fetch_add(call.sleeps, Relaxed);
        let _ = self.fallbacks.fetch_add(call.fallbacks, Relaxed);
        Ok(call.finish())
    }

    /// Whether `store` is one of the set's: `ForeignPosition` if not.
    fn member(&self, store: &Store<P>) -> Result<(), Error> {
        if self
            .stores
            .iter()
            .any(|s| Arc::ptr_eq(&s.inner, &store.inner))
        {
            Ok(())
        } else {
            Err(not_written(
                NotWrittenCause::ForeignPosition,
                ErrorContext::default(),
            ))
        }
    }

    /// [`AppendRegion::append_after`] with dependencies on any store of the
    /// set checked as that region's own store's are (each matched to its
    /// store by volume, its region held from the append's lane through its
    /// reservation, the holds taken in set order).
    ///
    /// # Errors
    ///
    /// `NotWritten(ForeignPosition)` before any work if `region` is of no
    /// store in the set; else as [`AppendRegion::append_after`].
    pub fn append_after(
        &self,
        region: &AppendRegion<P>,
        deps: &[DurableReceipt],
        bytes: &[u8],
    ) -> Result<WriteTicket, Error> {
        self.member(region.store())?;
        region.append_after_in(deps, bytes, &self.stores)
    }

    /// [`PageRegion::write_after`] with dependencies on any store of the set
    /// (as [`Self::append_after`]).
    ///
    /// # Errors
    ///
    /// `NotWritten(ForeignPosition)` before any work if `region` is of no
    /// store in the set; else as [`PageRegion::write_after`].
    pub fn write_after(
        &self,
        region: &PageRegion<P>,
        deps: &[DurableReceipt],
        pos: RegionPos,
        bytes: &[u8],
    ) -> Result<WriteTicket, Error> {
        self.member(region.store())?;
        region.write_after_in(deps, pos, bytes, &self.stores)
    }
}

impl<P: Platform> core::ops::Index<usize> for StoreSet<P> {
    type Output = Store<P>;

    fn index(&self, i: usize) -> &Store<P> {
        &self.stores[i]
    }
}

impl<'a, P: Platform> IntoIterator for &'a StoreSet<P> {
    type Item = &'a Store<P>;
    type IntoIter = core::slice::Iter<'a, Store<P>>;

    fn into_iter(self) -> Self::IntoIter {
        self.stores.iter()
    }
}

impl<P: Platform> Submitter<P>
where
    P::Queue: Send,
{
    /// [`Self::append_after`] with dependencies on any store of `set` (data
    /// through a submitter on one device, the pointer through one on
    /// another).
    ///
    /// # Errors
    ///
    /// `NotWritten(ForeignPosition)`, with the buffer and before any work,
    /// unless this submitter's store is in the set (so its own store's
    /// dependencies are always checked); else as [`Self::append_after`].
    pub fn append_after_in(
        &mut self,
        set: &StoreSet<P>,
        region: &AppendRegion<P>,
        deps: &[DurableReceipt],
        buf: IoBuf,
        mode: WriteMode,
        tag: u64,
    ) -> Result<RegionPos, Rejected> {
        if let Err(error) = set.member(&self.store) {
            return Err(Rejected {
                error,
                buf: Some(buf),
            });
        }
        self.append_after_with(region, deps, buf, mode, tag, &set.stores)
    }

    /// [`Self::write_after`] with dependencies on any store of `set`.
    ///
    /// # Errors
    ///
    /// As [`Self::append_after_in`], else as [`Self::write_after`].
    #[allow(clippy::too_many_arguments)]
    pub fn write_after_in(
        &mut self,
        set: &StoreSet<P>,
        region: &PageRegion<P>,
        deps: &[DurableReceipt],
        pos: RegionPos,
        buf: IoBuf,
        mode: WriteMode,
        tag: u64,
    ) -> Result<(), Rejected> {
        if let Err(error) = set.member(&self.store) {
            return Err(Rejected {
                error,
                buf: Some(buf),
            });
        }
        self.write_after_with(region, deps, pos, buf, mode, tag, &set.stores)
    }
}

/// Runs `write(k)` for every copy `k`, in the set order of its store
/// (`at[k]`), and gives the results back in the copies' order.
fn in_set_order<T>(
    at: &[usize],
    mut write: impl FnMut(usize) -> Result<T, Error>,
) -> Vec<Result<T, Error>> {
    let mut order: Vec<usize> = (0..at.len()).collect();
    order.sort_by_key(|&k| at[k]);
    let mut out: Vec<Option<Result<T, Error>>> = (0..at.len()).map(|_| None).collect();
    for k in order {
        out[k] = Some(write(k));
    }
    out.into_iter()
        .map(|r| {
            r.unwrap_or_else(|| {
                Err(not_written(
                    NotWrittenCause::ForeignPosition,
                    ErrorContext::default(),
                ))
            })
        })
        .collect()
}

// ---------------------------------------------------------------- answers

/// One call's answer channel: the flush threads it handed stores to post
/// here, and a store's poison wakes it (R5.3 steps 4-5).
pub(crate) struct Answers {
    state: Mutex<AnswerState>,
    ready: Condvar,
    /// Outcomes of the current call posted: read without the lock while
    /// the call yields.
    posted: AtomicU64,
    /// Poison wakes of the current call's handed-off stores: read without
    /// the lock while the call yields, so a poison ends its yields (a late
    /// wake of an earlier call's store at most sends one call to its
    /// sleep, where the poison is read under the lock).
    poisons: AtomicU64,
}

struct AnswerState {
    /// The call these answers are for; outcomes of any other are dropped.
    seq: u64,
    /// Per store of the set, its flush thread's outcome once posted.
    slots: Vec<Option<Flushed>>,
}

/// What a call needs besides its result, kept by each thread for its next
/// call: no allocation per call once warm.
struct Spare {
    answers: Arc<Answers>,
    states: Vec<Ticket>,
    store_of: Vec<usize>,
    parts: Vec<Part>,
}

thread_local! {
    /// Each thread's spare: a call takes it and gives it back.
    static SPARE: RefCell<Option<Spare>> = const { RefCell::new(None) };
}

fn lock_answers(a: &Answers) -> MutexGuard<'_, AnswerState> {
    a.state.lock().unwrap_or_else(PoisonError::into_inner)
}

impl Spare {
    /// This thread's spare (or a new one), emptied.
    fn take() -> Self {
        let mut s = SPARE
            .try_with(|s| s.try_borrow_mut().ok().and_then(|mut s| s.take()))
            .ok()
            .flatten()
            .unwrap_or_else(|| Self {
                answers: Arc::new(Answers {
                    state: Mutex::new(AnswerState {
                        seq: 0,
                        slots: Vec::new(),
                    }),
                    ready: Condvar::new(),
                    posted: AtomicU64::new(0),
                    poisons: AtomicU64::new(0),
                }),
                states: Vec::new(),
                store_of: Vec::new(),
                parts: Vec::new(),
            });
        s.states.clear();
        s.store_of.clear();
        s.parts.clear();
        s
    }

    /// Back to this thread (a flush thread may still hold the answers, to
    /// post a late outcome the next call drops).
    fn give_back(mut self) {
        if self.parts.capacity() > SPARE_KEEP || self.states.capacity() > SPARE_KEEP {
            return;
        }
        // No region is kept alive by a spare.
        self.states.clear();
        // From a thread-local's destructor the spare is just dropped (never
        // a panic in a drop).
        let _kept = SPARE.try_with(|s| {
            if let Ok(mut slot) = s.try_borrow_mut() {
                *slot = Some(self);
            }
        });
    }
}

impl Answers {
    /// Sets these answers up for call `seq` of a set of `stores`: every
    /// slot empty, and any other call's outcome dropped from now on.
    fn reset(&self, seq: u64, stores: usize) {
        let mut st = lock_answers(self);
        st.seq = seq & SEQ_MASK;
        st.slots.clear();
        st.slots.resize_with(stores, || None);
        self.posted.store(0, Ordering::SeqCst);
        self.poisons.store(0, Ordering::SeqCst);
    }
}

/// Sleeps on `a` until `settled` holds of its slots, read under its mutex
/// (so neither a post nor a poison wake after a check is missed). Returns
/// how many times it slept.
fn wait_settled(a: &Answers, settled: impl Fn(&AnswerState) -> bool) -> u64 {
    let mut st = lock_answers(a);
    let mut sleeps = 0;
    while !settled(&st) {
        sleeps += 1;
        crate::test_hook::step(crate::test_hook::Step::AnswersWait);
        st = a.ready.wait(st).unwrap_or_else(PoisonError::into_inner);
    }
    sleeps
}

impl Answer for Answers {
    fn post(&self, tag: u64, outcome: Flushed) {
        let (seq, i) = (tag >> INDEX_BITS, (tag & ((1 << INDEX_BITS) - 1)) as usize);
        let mut st = lock_answers(self);
        if st.seq != seq {
            return;
        }
        if let Some(slot) = st.slots.get_mut(i) {
            if slot.is_none() {
                *slot = Some(outcome);
                let _ = self.posted.fetch_add(1, Ordering::SeqCst);
            }
        }
    }

    fn signal(&self) {
        crate::test_hook::step(crate::test_hook::Step::AnswerPosted);
        // The slot was set under the mutex, which the caller holds while it
        // checks the slots: notified here, it is either waiting or has not
        // checked yet.
        self.ready.notify_one();
    }
}

impl PoisonWaker for Answers {
    fn poisoned(&self) {
        let _ = self.poisons.fetch_add(1, Ordering::SeqCst);
        // Under the mutex: the caller reads the poison under it before it
        // waits, so this wake is never missed.
        let _sync = lock_answers(self);
        self.ready.notify_one();
    }
}

// ---------------------------------------------------------------- one call

/// A ticket's progress through a call.
enum Ticket {
    /// Answered.
    Done(Result<DurableReceipt, Error>),
    /// An append ticket: its region, whether a data flush backs its receipt
    /// (the store flushes, or the region forces data sync, read once), and
    /// its completed prefix once its store is ready.
    Append {
        region: Arc<LiveRegion>,
        synced: bool,
        snapshot: Option<u64>,
    },
    /// A page (or slot) ticket: whether a data flush backs its receipt.
    Page { synced: bool },
}

/// One store's part in a call.
#[derive(Default)]
struct Part {
    /// Tickets of this store not yet answered.
    open: usize,
    /// Every append ticket has completed through its end.
    ready: bool,
    /// The flush that covers its tickets.
    need: u64,
    /// The store's flush, its flush thread's or this call's.
    outcome: Option<Flushed>,
    /// Handed to its flush thread; the poison waker's key.
    handed: Option<u64>,
}

struct Call<'a, P: Platform>
where
    P::Queue: Send,
{
    set: &'a StoreSet<P>,
    tickets: &'a [&'a WriteTicket],
    states: Vec<Ticket>,
    /// Per ticket, its store's index.
    store_of: Vec<usize>,
    parts: Vec<Part>,
    answers: Arc<Answers>,
    seq: u64,
    sleeps: u64,
    fallbacks: u64,
}

impl<'a, P: Platform> Call<'a, P>
where
    P::Queue: Send,
{
    /// Matches every ticket (R5.3 step 1); answers what needs no flush.
    fn new(set: &'a StoreSet<P>, tickets: &'a [&'a WriteTicket]) -> Result<Self, Error> {
        let Spare {
            answers,
            mut states,
            mut store_of,
            mut parts,
        } = Spare::take();
        for t in tickets {
            let at = set
                .stores
                .iter()
                .position(|s| s.inner.volume == t.volume && s.inner.epoch == t.epoch);
            let Some(i) = at else {
                Spare {
                    answers,
                    states,
                    store_of,
                    parts,
                }
                .give_back();
                return Err(not_written(NotWrittenCause::ForeignPosition, ctx(t)));
            };
            store_of.push(i);
        }
        parts.resize_with(set.stores.len(), Part::default);
        for (t, &i) in tickets.iter().zip(&store_of) {
            let state = Self::matched(&set.stores[i], t);
            if !matches!(state, Ticket::Done(_)) {
                parts[i].open += 1;
            }
            states.push(state);
        }
        let seq = CALLS.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
        answers.reset(seq, set.stores.len());
        Ok(Self {
            set,
            tickets,
            states,
            store_of,
            parts,
            answers,
            seq,
            sleeps: 0,
            fallbacks: 0,
        })
    }

    /// A ticket matched to its store's live region.
    fn matched(store: &Store<P>, t: &WriteTicket) -> Ticket {
        let c = ctx(t);
        let found = store
            .inner
            .regions
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .iter()
            .find(|r| r.id == t.region)
            .map(Arc::clone);
        let Some(region) = found else {
            return Ticket::Done(Err(not_written(NotWrittenCause::NotReady, c)));
        };
        if region.generation != t.generation || region.gate.check().is_err() {
            return Ticket::Done(Err(not_written(NotWrittenCause::StaleGeneration, c)));
        }
        let flushes = store.inner.policy.flush;
        let synced = flushes || region.forced();
        let Some(f) = region.frontier.as_ref() else {
            return Ticket::Page { synced };
        };
        // Already durable: no flush (as `sync_through`). A forced region on
        // a power-safe store counts only what a data flush made durable.
        let durable = if synced && !flushes {
            f.synced()
        } else {
            f.durable()
        };
        if durable >= t.end {
            store.inner.domain.count_already_durable();
            return Ticket::Done(Ok(store.receipt(&region, 0, durable, 0, true, synced)));
        }
        Ticket::Append {
            region,
            synced,
            snapshot: None,
        }
    }

    fn run(&mut self) {
        let mut lead: Option<usize> = None;
        loop {
            // Every store found ready is decided; each one needing a flush
            // is handed off, except one kept back while nothing remains to
            // wait for, which this thread leads.
            for i in 0..self.parts.len() {
                if self.parts[i].open == 0 || self.parts[i].ready || !self.is_ready(i) {
                    continue;
                }
                if self.ready(i) {
                    if let Some(prev) = lead.replace(i) {
                        self.hand_off(prev);
                    }
                }
            }
            if self.parts.iter().all(|p| p.open == 0 || p.ready) {
                break;
            }
            // More to wait for: nothing is kept back meanwhile.
            if let Some(prev) = lead.take() {
                self.hand_off(prev);
            }
            // A store whose last ticket completed since the sweep has none
            // to wait on: the next sweep finds it ready.
            if let Some(waiting) = self.first_unready() {
                self.wait_ready(waiting);
            }
        }
        // Its own flush timed on its store's clock: the stores handed off
        // were asked first, so their answers are yielded for about as long
        // (D-155).
        // Timed only with answers to wait for.
        let mut budget = (0, 0);
        if let Some(i) = lead {
            let need = self.parts[i].need;
            let store = &self.set.stores[i];
            let timed = self.parts.iter().any(|p| p.handed.is_some());
            let clock = || {
                if timed {
                    store.inner.platform.monotonic_ns()
                } else {
                    0
                }
            };
            let start = clock();
            self.parts[i].outcome = Some(store.barrier_to(need).map(|()| need));
            let end = clock();
            budget = (end, end.saturating_sub(start));
        }
        self.collect(lead, budget);
    }

    /// Whether every append ticket of store `i` has completed through its
    /// end (or its store is poisoned: then nothing more completes).
    fn is_ready(&self, i: usize) -> bool {
        if self.set.stores[i].inner.domain.poisoned().is_some() {
            return true;
        }
        self.states
            .iter()
            .zip(self.tickets)
            .zip(&self.store_of)
            .all(|((s, t), &j)| match s {
                Ticket::Append { region, .. } if j == i => region
                    .frontier
                    .as_ref()
                    .is_none_or(|f| f.completed() >= t.end),
                _ => true,
            })
    }

    /// Store `i` is ready: snapshots its append tickets' prefixes, then
    /// takes its `need` (after the last snapshot), and decides whether it
    /// needs a flush. Returns true if it does.
    fn ready(&mut self, i: usize) -> bool {
        let store = &self.set.stores[i];
        let domain = &store.inner.domain;
        let mut need = 0;
        let mut appends = false;
        let mut flush = store.inner.policy.flush;
        for ((s, t), &j) in self.states.iter_mut().zip(self.tickets).zip(&self.store_of) {
            if j != i {
                continue;
            }
            match s {
                Ticket::Append {
                    region,
                    synced,
                    snapshot,
                } => {
                    *snapshot = region.frontier.as_ref().map(|f| f.completed());
                    appends = true;
                    flush |= *synced;
                }
                Ticket::Page { synced } => {
                    need = need.max(t.need);
                    flush |= *synced;
                }
                Ticket::Done(_) => {}
            }
        }
        if appends {
            // After the last snapshot: the flush covers every byte of them.
            need = need.max(domain.ticket());
        }
        let part = &mut self.parts[i];
        part.ready = true;
        part.need = need;
        if let Some(first) = domain.poisoned() {
            part.outcome = Some(Err(Error::Poisoned { first }));
            return false;
        }
        if !flush {
            // Power-safe, no forced region: durable at completion.
            part.outcome = Some(Ok(u64::MAX));
            return false;
        }
        if domain.join_if_done(need) {
            part.outcome = Some(Ok(need));
            return false;
        }
        true
    }

    /// The first store not yet ready, and the ticket to wait on.
    fn first_unready(&self) -> Option<(usize, usize)> {
        self.states.iter().enumerate().find_map(|(k, s)| match s {
            Ticket::Append { region, .. } => {
                let i = self.store_of[k];
                let done = region
                    .frontier
                    .as_ref()
                    .is_none_or(|f| f.completed() >= self.tickets[k].end);
                (!self.parts[i].ready && !done).then_some((i, k))
            }
            _ => None,
        })
    }

    /// Waits until ticket `k` of store `i` has completed (or the store is
    /// poisoned).
    fn wait_ready(&self, (i, k): (usize, usize)) {
        let Ticket::Append { region, .. } = &self.states[k] else {
            return;
        };
        let domain = &self.set.stores[i].inner.domain;
        if let Some(f) = region.frontier.as_ref() {
            let _reached = f.wait_completed(self.tickets[k].end, || domain.poisoned().is_some());
        }
    }

    /// Hands store `i`'s flush to its flush thread, registered for its
    /// poison first; flushes it on this thread if no flush thread takes it.
    fn hand_off(&mut self, i: usize) {
        let store = &self.set.stores[i];
        let need = self.parts[i].need;
        if let Some(f) = store.flusher() {
            let waker: Arc<dyn PoisonWaker> = Arc::clone(&self.answers) as Arc<dyn PoisonWaker>;
            let key = store.inner.domain.on_poison(waker);
            let tag = (self.seq << INDEX_BITS) | i as u64;
            if f.request(
                need,
                store_io_core::IoClass::Commit,
                store.inner.domain.arrival(),
                tag,
                Arc::clone(&self.answers) as Arc<dyn Answer>,
            ) {
                self.parts[i].handed = Some(key);
                return;
            }
            store.inner.domain.forget_poison_waker(key);
        }
        self.fallbacks += 1;
        self.parts[i].outcome = Some(store.barrier_to(need).map(|()| need));
    }

    /// Waits for every handed-off store's answer, or its poison: yielding
    /// first (for `budget`'s time from its start, on store `lead`'s clock,
    /// when this call led a flush), then asleep on the answers.
    fn collect(&mut self, lead: Option<usize>, (start, budget): (u64, u64)) {
        let want = self.parts.iter().filter(|p| p.handed.is_some()).count() as u64;
        if want == 0 {
            return;
        }
        let answers = Arc::clone(&self.answers);
        // Every answer posted, or a handed-off store poisoned (settled
        // below, under the lock).
        let all_posted = || {
            crate::test_hook::step(crate::test_hook::Step::AnswersYield);
            answers.posted.load(Ordering::SeqCst) >= want
                || answers.poisons.load(Ordering::SeqCst) > 0
        };
        let patience = &self.set.patience;
        let reached = match lead {
            Some(i) => {
                let platform = &self.set.stores[i].inner.platform;
                patience.yield_within(start, budget, || platform.monotonic_ns(), all_posted)
            }
            None => patience.yield_until(all_posted),
        };
        // Yields a poison ended still wait for the stores not poisoned.
        if !reached || answers.posted.load(Ordering::SeqCst) < want {
            // The poison is read after the registration (in `hand_off`) and
            // under the mutex the poison waker takes: a poison before it is
            // seen here, one after it wakes the wait.
            let slept = wait_settled(&answers, |st| self.settled(st));
            self.sleeps += slept;
        }
        let mut st = lock_answers(&answers);
        for (i, part) in self.parts.iter_mut().enumerate() {
            if part.handed.is_none() {
                continue;
            }
            let store = &self.set.stores[i];
            part.outcome = Some(st.slots[i].take().unwrap_or_else(|| {
                Err(Error::Poisoned {
                    first: store.inner.domain.poisoned().unwrap_or(FirstCause {
                        op: None,
                        raw: None,
                    }),
                })
            }));
        }
        drop(st);
    }

    /// Whether every handed-off store has answered or is poisoned.
    fn settled(&self, st: &AnswerState) -> bool {
        self.parts.iter().enumerate().all(|(i, p)| {
            p.handed.is_none()
                || st.slots[i].is_some()
                || self.set.stores[i].inner.domain.poisoned().is_some()
        })
    }

    /// Stores this call waited on (each once).
    fn stores_waited(&self) -> u64 {
        self.parts.iter().filter(|p| p.ready).count() as u64
    }

    /// Every ticket's receipt or error (R5.3 step 6).
    fn finish(mut self) -> Vec<Result<DurableReceipt, Error>> {
        // No receipt below a store's need: a violation poisons that store
        // (with no lock of the call's held) and fails its tickets.
        for (i, part) in self.parts.iter_mut().enumerate() {
            if let Some(Ok(n)) = part.outcome {
                if n < part.need {
                    let store = &self.set.stores[i];
                    store.inner.domain.poison(FirstCause {
                        op: None,
                        raw: None,
                    });
                    store.wake_frontiers();
                    part.outcome = Some(Err(Error::Poisoned {
                        first: store.inner.domain.poisoned().unwrap_or(FirstCause {
                            op: None,
                            raw: None,
                        }),
                    }));
                }
            }
        }
        let mut out = Vec::with_capacity(self.tickets.len());
        let mut states = std::mem::take(&mut self.states);
        for ((s, t), &i) in states.drain(..).zip(self.tickets).zip(&self.store_of) {
            let store = &self.set.stores[i];
            let part = &self.parts[i];
            out.push(match s {
                Ticket::Done(r) => r,
                Ticket::Append {
                    region,
                    synced,
                    snapshot,
                } => match &part.outcome {
                    Some(Ok(_)) => {
                        let need = if synced { part.need } else { u64::MAX };
                        let through = snapshot.unwrap_or(0);
                        Ok(sync_tail(store, &region, through, need, synced))
                    }
                    Some(Err(e)) => failed_append(store, &region, t, synced, e),
                    None => Err(Error::Poisoned {
                        first: FirstCause {
                            op: None,
                            raw: None,
                        },
                    }),
                },
                Ticket::Page { synced } => match &part.outcome {
                    Some(Ok(_)) => {
                        let need = if synced { part.need } else { u64::MAX };
                        Ok(store.receipt_for(t, need))
                    }
                    Some(Err(e)) => failed_page(store, t, e),
                    None => Err(Error::Poisoned {
                        first: FirstCause {
                            op: None,
                            raw: None,
                        },
                    }),
                },
            });
        }
        self.states = states;
        out
    }
}

impl<P: Platform> Drop for Call<'_, P>
where
    P::Queue: Send,
{
    fn drop(&mut self) {
        // Unregistered on every exit, with no lock of the answers held.
        for (i, part) in self.parts.iter_mut().enumerate() {
            if let Some(key) = part.handed.take() {
                self.set.stores[i].inner.domain.forget_poison_waker(key);
            }
        }
        Spare {
            answers: Arc::clone(&self.answers),
            states: std::mem::take(&mut self.states),
            store_of: std::mem::take(&mut self.store_of),
            parts: std::mem::take(&mut self.parts),
        }
        .give_back();
    }
}

/// A failed store's append ticket (R5.3 step 6): a prefix receipt if what
/// is already published reaches its end, else a range receipt if a finished
/// flush covers it, else the store's error.
fn failed_append<P: Platform>(
    store: &Store<P>,
    region: &LiveRegion,
    t: &WriteTicket,
    synced: bool,
    e: &Error,
) -> Result<DurableReceipt, Error>
where
    P::Queue: Send,
{
    let flushes = store.inner.policy.flush;
    if let Some(f) = region.frontier.as_ref() {
        let durable = if synced && !flushes {
            f.synced()
        } else {
            f.durable()
        };
        if durable >= t.end {
            return Ok(store.receipt(region, 0, durable, 0, true, synced));
        }
    }
    failed_page(store, t, e)
}

/// A failed store's page ticket: a range receipt if a finished flush covers
/// it, else the store's error.
fn failed_page<P: Platform>(
    store: &Store<P>,
    t: &WriteTicket,
    e: &Error,
) -> Result<DurableReceipt, Error>
where
    P::Queue: Send,
{
    if store.inner.domain.join_if_done(t.need) {
        Ok(store.receipt_for(t, t.need))
    } else {
        Err(in_range(e.clone(), t))
    }
}

fn ctx(t: &WriteTicket) -> ErrorContext {
    ErrorContext {
        region: Some(t.region),
        range: Some(store_io_core::error::ByteRange {
            start: t.start,
            end: t.end,
        }),
    }
}

#[cfg(all(test, loom))]
mod loom_tests {
    use super::*;

    use crate::domain::Domain;

    fn answers(seq: u64) -> Arc<Answers> {
        let a = Arc::new(Answers {
            state: Mutex::new(AnswerState {
                seq: 0,
                slots: Vec::new(),
            }),
            ready: Condvar::new(),
            posted: AtomicU64::new(0),
            poisons: AtomicU64::new(0),
        });
        a.reset(seq, 1);
        a
    }

    /// Waits as a call does (`wait_settled`), until store 0 answered or
    /// `d` is poisoned.
    fn wait(a: &Answers, d: Option<&Domain>) -> Option<Flushed> {
        let _slept = wait_settled(a, |st| {
            st.slots[0].is_some() || d.is_some_and(|d| d.poisoned().is_some())
        });
        lock_answers(a).slots[0].take()
    }

    /// Answers reused by the next call: a late outcome of the earlier call,
    /// posted before or after the reuse, is never taken as the next call's.
    #[test]
    fn loom_reused_answers_drop_a_late_outcome() {
        loom::model(|| {
            let a = answers(1);
            let late = {
                let a = Arc::clone(&a);
                loom::thread::spawn(move || {
                    a.post(1 << INDEX_BITS, Ok(7));
                    a.signal();
                })
            };
            a.reset(2, 1);
            let own = {
                let a = Arc::clone(&a);
                loom::thread::spawn(move || {
                    a.post(2 << INDEX_BITS, Ok(9));
                    a.signal();
                })
            };
            assert!(matches!(wait(&a, None), Some(Ok(9))));
            let _l = late.join();
            let _o = own.join();
        });
    }

    /// A poison landing anywhere around the call's registration ends its
    /// wait: before it, the call's read sees it; after it, the waker wakes
    /// the call.
    #[test]
    fn loom_a_poison_around_the_registration_ends_the_wait() {
        loom::model(|| {
            let d = Arc::new(Domain::new());
            let a = answers(1);
            let poisoner = {
                let d = Arc::clone(&d);
                loom::thread::spawn(move || {
                    d.poison(FirstCause {
                        op: None,
                        raw: None,
                    });
                })
            };
            let key = d.on_poison(Arc::clone(&a) as Arc<dyn PoisonWaker>);
            assert!(wait(&a, Some(&d)).is_none());
            d.forget_poison_waker(key);
            let _p = poisoner.join();
        });
    }
}