armdb 0.15.1

sharded bitcask key-value storage optimized for NVMe
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
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
//! Segmented transaction ownership. Commit pins and locks one active view;
//! recovery routes every logged version to its historical generation.
use crate::codec::Codec;
use crate::error::{DbError, DbResult};
use crate::hook::TypedWriteHook;
use crate::key::Key;
use crate::seg::core::{SegCore, View};
use crate::seg::freeze::Admission;
use crate::seg::generation::{GenLookup, GenProbe, Generation, GenerationLocks};
use crate::seg::hash::{key_hash, overlapping_shards, shard_for_hash};
use crate::seg::{SegMap, SegTree};
use crate::txlog::record::{OpKind, VersionTag};
use crate::txn::TxTarget;
use crate::txn::participant::{DeferredReplay, PlannedOp, TxBackend, TxLocked, TxParticipant};
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::time::Instant;

mod commit;
mod replay;
use commit::SegCommitLocked;
use replay::SegReplayLocked;

// Both facades share admission, hash lock routing, replay and event ownership.
/// Marker restricting write hints to segmented facades.
pub(crate) trait SegTxTarget {}

macro_rules! impl_segmented_participant {
    ($facade:ident) => {
        impl<K, T, C, H> TxTarget for $facade<K, T, C, H>
        where
            K: Key + Hash + Eq + Send + Sync + 'static,
            T: Send + Sync + 'static,
            C: Codec<T> + 'static,
            H: TypedWriteHook<K, T> + 'static,
        {
            type Key = K;
            type Val = T;
            fn tx_encode(&self, value: &T, out: &mut Vec<u8>) -> DbResult<()> {
                self.codec.encode_to(value, out)
            }
            fn tx_decode(&self, bytes: &[u8]) -> DbResult<T> {
                self.codec.decode_from(bytes)
            }
        }

        impl<K, T, C, H> SegTxTarget for $facade<K, T, C, H>
        where
            K: Key + Hash + Eq + Send + Sync + 'static,
            T: Send + Sync + 'static,
            C: Codec<T> + 'static,
            H: TypedWriteHook<K, T> + 'static,
        {
        }

        impl<K, T, C, H> TxParticipant for $facade<K, T, C, H>
        where
            K: Key + Hash + Eq + Send + Sync + 'static,
            T: Send + Sync + 'static,
            C: Codec<T> + 'static,
            H: TypedWriteHook<K, T> + 'static,
        {
            fn name(&self) -> &'static str {
                self.core().meta.name
            }
            fn backend(&self) -> TxBackend {
                TxBackend::Bitcask
            }
            fn schema_fingerprint(&self) -> u64 {
                self.core().meta.typ_hash
            }
            fn encrypted(&self) -> bool {
                false
            }
            fn max_entry_bytes(&self) -> Option<usize> {
                Some(
                    self.config().write_buffer_size.min(
                        (u32::MAX as usize).saturating_sub(crate::seg::block::BLOCK_HEADER_LEN),
                    ),
                )
            }
            fn shard_count(&self) -> usize {
                self.config().shard_count
            }
            fn shard_of(&self, key: &[u8]) -> DbResult<usize> {
                checked_key::<K>(key)?;
                Ok(shard_for_hash(
                    key_hash(key, self.core().seed),
                    self.shard_count(),
                ))
            }
            fn tx_check_user_op(&self) -> DbResult<()> {
                if self.is_index_target_participant() {
                    return Err(DbError::Config(crate::seg::index_target::DIRECT_WRITE));
                }
                Ok(())
            }
            fn tx_needs_old_value(&self) -> bool {
                self.hook_active()
            }
            fn tx_project(
                &self,
                key: &[u8],
                old: Option<&[u8]>,
                new: Option<&[u8]>,
            ) -> DbResult<Vec<crate::txn::participant::TxDerivedWrite>> {
                self.project_bytes(&checked_key::<K>(key)?, old, new)
            }
            fn tx_counter_deltas(
                &self,
                key: &[u8],
                old: Option<&[u8]>,
                new: Option<&[u8]>,
            ) -> DbResult<Vec<crate::txn::participant::TxCounterDelta>> {
                self.counter_bytes(&checked_key::<K>(key)?, old, new)
            }
            fn tx_preplan_projection_targets(&self) -> DbResult<Vec<Arc<dyn TxParticipant>>> {
                let mut out = Vec::new();
                if let Some(targets) = self.hook.projection_targets() {
                    for target in targets.erased_targets() {
                        out.push(targets.resolve_tx_participant(target.name).ok_or(
                            DbError::Internal(
                                "declared segmented index target missing participant",
                            ),
                        )?);
                    }
                }
                if let Some(targets) = self.hook.counter_targets() {
                    out.extend(targets.all().map(|m| m.as_tx_participant()));
                }
                if H::EMITS
                    && let Some(targets) = self.hook.stream_targets()
                {
                    out.extend(targets.tx_participants());
                }
                Ok(out)
            }
            fn tx_preplan_stream_targets(&self) -> DbResult<Vec<Arc<dyn TxParticipant>>> {
                Ok(if H::EMITS {
                    self.hook
                        .stream_targets()
                        .map(|t| t.tx_participants())
                        .unwrap_or_default()
                } else {
                    Vec::new()
                })
            }
            fn tx_emits(&self) -> DbResult<bool> {
                Ok(H::EMITS)
            }
            fn tx_prepare_streams(&self) -> DbResult<Vec<crate::queue::retention::ProducerPermit>> {
                if H::EMITS {
                    self.hook
                        .stream_targets()
                        .map_or(Ok(Vec::new()), crate::QueueTargets::prepare_streams)
                } else {
                    Ok(Vec::new())
                }
            }
            fn tx_stream_writes(
                &self,
                key: &[u8],
                old: Option<&[u8]>,
                new: Option<&[u8]>,
            ) -> DbResult<crate::txn::participant::TxStreamWrites> {
                if !H::EMITS {
                    return Ok(crate::txn::participant::TxStreamWrites::empty());
                }
                let Some(targets) = self.hook.stream_targets() else {
                    return Ok(crate::txn::participant::TxStreamWrites::empty());
                };
                let key = checked_key::<K>(key)?;
                let old = old.map(|v| self.codec.decode_from(v)).transpose()?;
                let new = new.map(|v| self.codec.decode_from(v)).transpose()?;
                let (rows, bytes) = self.effective_projection_limits();
                let mut sink = crate::EmitSink::new(targets, rows, bytes);
                self.hook
                    .stream_project(&key, old.as_ref(), new.as_ref(), &mut sink)?;
                targets.lower(&sink)
            }
            fn tx_admit(&self) -> DbResult<()> {
                self.core().admit_tx()
            }
            fn lock_shards<'a>(&'a self, shards: &[usize]) -> DbResult<Box<dyn TxLocked + 'a>> {
                self.lock_shards_since(shards, None)
            }
            fn lock_shards_since<'a>(
                &'a self,
                shards: &[usize],
                since_ms: Option<u64>,
            ) -> DbResult<Box<dyn TxLocked + 'a>> {
                let callback = self.event_callback()?;
                let core = self.core();
                loop {
                    let view = core.view();
                    let real =
                        overlapping_shards(shards, self.shard_count(), view.active.shard_count())?;
                    let locks = view.active.lock_owned(&real)?;
                    core.check_writable()?;
                    if view.active.is_frozen() {
                        drop(locks);
                        continue;
                    }
                    if view.active.is_full() {
                        drop(locks);
                        // `tx_admit` already waited for room with no locks held.
                        // Earlier participants are locked now, so only an immediate
                        // freeze is allowed here; a saturated seal queue refuses at
                        // once instead of stalling their writers.
                        core.make_room(&view.active, Admission::Deadline(Instant::now()))?;
                        continue;
                    }
                    return Ok(Box::new(SegCommitLocked {
                        locks,
                        core: core.clone(),
                        view,
                        reservations: Vec::new(),
                        logged: false,
                        cleaned: false,
                        events: Vec::new(),
                        callback,
                        observed: HashMap::new(),
                        since_ms,
                    }));
                }
            }
            fn lock_for_replay<'a>(&'a self, _: &[usize]) -> DbResult<Box<dyn TxLocked + 'a>> {
                Ok(Box::new(SegReplayLocked {
                    core: self.core().clone(),
                    view: self.core().view(),
                    events: Vec::new(),
                    callback: self.event_callback()?,
                }))
            }
        }

        impl<K, T, C, H> $facade<K, T, C, H>
        where
            K: Key + Hash + Eq + Send + Sync + 'static,
            T: Send + Sync + 'static,
            C: Codec<T> + 'static,
            H: TypedWriteHook<K, T> + 'static,
        {
            /// The deferred `on_write` channel, present only for `H::NEEDS_WRITE`.
            fn event_callback(&self) -> DbResult<Option<commit::EventCallback<K>>> {
                if !H::NEEDS_WRITE {
                    return Ok(None);
                }
                let owner = self
                    .tx_self
                    .get()
                    .and_then(std::sync::Weak::upgrade)
                    .ok_or(DbError::Internal("segmented hook owner was not bound"))?;
                Ok(Some(Box::new(move |event| owner.fire_event(event))))
            }
        }
    };
}
impl_segmented_participant!(SegMap);
impl_segmented_participant!(SegTree);

fn checked_key<K: Key>(bytes: &[u8]) -> DbResult<K> {
    if bytes.len() != size_of::<K>() {
        return Err(DbError::Internal(
            "segmented transaction key length mismatch",
        ));
    }
    Ok(K::from_bytes(bytes))
}
fn bitcask(tag: VersionTag) -> DbResult<u64> {
    match tag {
        VersionTag::Bitcask { gsn } => Ok(gsn),
        _ => Err(DbError::Internal(
            "wrong version tag on segmented participant",
        )),
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    use crate::NoHook;
    use crate::seg::SegConfig;
    use crate::seg::core::OpenOptions;
    use crate::seg::testkit::{TestKey, TextCodec, key_of, meta};

    fn open(path: &std::path::Path, config: SegConfig) -> SegMap<TestKey, String, TextCodec> {
        SegMap::open_with(
            path,
            meta(),
            config,
            TextCodec,
            OpenOptions {
                worker: false,
                ..OpenOptions::production()
            },
        )
        .unwrap()
    }
    fn open_tree(
        path: &std::path::Path,
        config: SegConfig,
    ) -> crate::seg::SegTree<TestKey, String, TextCodec> {
        crate::seg::SegTree::open_with(
            path,
            meta(),
            &[],
            config,
            TextCodec,
            OpenOptions {
                worker: false,
                ..OpenOptions::production()
            },
        )
        .unwrap()
    }

    #[test]
    fn tree_tx_locks_keep_historical_hash_routing() {
        use crate::seg::testkit::ordered_key;
        let dir = tempfile::tempdir().unwrap();
        let mut config = SegConfig::test();
        config.shard_count = 3;
        let tree = open_tree(dir.path(), config.clone());
        tree.put(&ordered_key(1), &"old".to_owned()).unwrap();
        let old = tree.core().view().active.clone();
        let missing = old.engine().gsn().fetch_add(1, Ordering::Relaxed);
        tree.seal().unwrap();
        tree.put(&ordered_key(1), &"young".to_owned()).unwrap();
        tree.durable_flush().unwrap();
        drop(old);
        drop(tree);
        config.shard_count = 7;
        let tree = open_tree(dir.path(), config);
        assert_eq!(tree.backend(), TxBackend::Bitcask);
        let key = ordered_key(1);
        let hash = key_hash(key.as_bytes(), tree.core().seed);
        assert_eq!(
            tree.shard_of(key.as_bytes()).unwrap(),
            shard_for_hash(hash, 7)
        );
        let held = tree
            .lock_shards(&[tree.shard_of(key.as_bytes()).unwrap()])
            .unwrap();
        assert_eq!(
            held.apply_domain(key.as_bytes()).unwrap(),
            shard_for_hash(hash, 3)
        );
        held.abort();
        let older = tree.core().view().frozen[0].clone();
        let next = tree.core().view().active.next_gsn();
        let mut replay = tree.lock_for_replay(&[]).unwrap();
        assert!(
            replay
                .needs_replay(key.as_bytes(), OpKind::Put, tag(missing))
                .unwrap()
        );
        replay
            .apply_put(key.as_bytes(), b"replayed", tag(missing))
            .unwrap();
        assert!(
            matches!(older.lookup(hash, &key).unwrap(), GenLookup::Live(v) if v == b"replayed")
        );
        assert_eq!(tree.core().view().active.next_gsn(), next);
        assert_eq!(tree.try_get(&key).unwrap(), Some("young".to_owned()));
    }

    #[test]
    fn tree_tx_apply_and_replay_update_ordered_index() {
        use crate::seg::testkit::ordered_key;
        let dir = tempfile::tempdir().unwrap();
        let tree = open_tree(dir.path(), SegConfig::test());
        let keys = [ordered_key(9), ordered_key(1), ordered_key(5)];
        let mut held = tree.lock_shards(&[0, 1]).unwrap();
        let tags = held
            .reserve(&keys.iter().map(plan).collect::<Vec<_>>())
            .unwrap();
        held.mark_logged();
        for (key, version) in keys.iter().zip(tags) {
            held.apply_put(key.as_bytes(), b"committed", version)
                .unwrap();
        }
        held.finish(
            &mut Vec::new(),
            &mut crate::watch::WatchWakeBatch::default(),
        )
        .unwrap();
        tree.durable_flush().unwrap();
        drop(tree);
        let tree = open_tree(dir.path(), SegConfig::test());
        let fresh = tree.core().view().active.next_gsn();
        let mut replay = tree.lock_for_replay(&[]).unwrap();
        replay
            .apply_put(ordered_key(3).as_bytes(), b"replayed", tag(fresh))
            .unwrap();
        replay
            .apply_delete(ordered_key(5).as_bytes(), tag(fresh + 1))
            .unwrap();
        drop(replay);
        let rows = tree.iter().map(|r| r.unwrap().0).collect::<Vec<_>>();
        assert_eq!(rows, vec![ordered_key(1), ordered_key(3), ordered_key(9)]);
        // Missing logged application fences the same collection after lock cleanup.
        let mut held = tree.lock_shards(&[0, 1]).unwrap();
        held.reserve(&[plan(&ordered_key(7))]).unwrap();
        held.mark_logged();
        drop(held);
        assert!(matches!(
            tree.put(&ordered_key(7), &"late".to_owned()),
            Err(DbError::CollectionFenced { .. })
        ));
    }

    #[test]
    fn tree_active_hook_requires_tx() {
        let dir = tempfile::tempdir().unwrap();
        let seen = Seen::default();
        let tree = Arc::new(
            crate::seg::SegTree::open_deferred(
                dir.path(),
                meta(),
                &[],
                false,
                SegConfig::test(),
                TextCodec,
                Recorder(seen.clone()),
            )
            .unwrap(),
        );
        assert!(tree.tx_self.set(Arc::downgrade(&tree)).is_ok());
        assert!(tree.tx_needs_old_value());
        assert!(matches!(
            tree.put(&key_of(1), &"value".to_owned()),
            Err(DbError::SegHookRequiresTx { .. })
        ));
        assert!(tree.event_callback().unwrap().is_some());
        let key = key_of(1);
        tree.core().put(&key, b"old").unwrap();
        let mut held = tree
            .lock_shards(&[tree.shard_of(key.as_bytes()).unwrap()])
            .unwrap();
        let version = held.reserve(&[plan(&key)]).unwrap()[0];
        held.mark_logged();
        assert_eq!(
            held.apply_put(key.as_bytes(), b"value", version).unwrap(),
            1
        );
        let mut deferred = held
            .finish(
                &mut Vec::new(),
                &mut crate::watch::WatchWakeBatch::default(),
            )
            .unwrap();
        // Deferred callback owns its Arc; it remains valid after the facade is dropped.
        assert_eq!(deferred.event_count(), 1);
        assert!(
            tree.tx_project(key_of(1).as_bytes(), None, Some(b"value"))
                .unwrap()
                .is_empty()
        );
        drop(tree);
        assert!(seen.lock().unwrap().is_empty());
        deferred.replay_one(0);
        assert_eq!(
            *seen.lock().unwrap(),
            vec![(Some("old".to_owned()), Some("value".to_owned()))]
        );
    }

    fn cleanup_fixture_with_deadline(
        fixture: impl FnOnce() + Send + 'static,
        timeout: std::time::Duration,
    ) -> Result<(), std::sync::mpsc::RecvTimeoutError> {
        let (send, recv) = std::sync::mpsc::channel();
        // The full fixture, its non-Send held contexts, inner scopes and every
        // destructor live on this owner. The test parent never joins it.
        std::thread::spawn(move || {
            let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(fixture));
            let _ = send.send(result);
        });
        match recv.recv_timeout(timeout)? {
            Ok(()) => Ok(()),
            Err(panic) => std::panic::resume_unwind(panic),
        }
    }
    fn bounded_cleanup(fixture: impl FnOnce() + Send + 'static) {
        cleanup_fixture_with_deadline(fixture, std::time::Duration::from_secs(15))
            .expect("whole cleanup fixture failed to finish before its deadline");
    }

    #[test]
    fn cleanup_deadline_bounds_non_send_context_drop() {
        use std::{sync::mpsc, time::Duration};
        let (release, released) = mpsc::channel();
        let (constructed, construction) = mpsc::channel();
        let (finished, finish) = mpsc::channel::<()>();
        let releaser = std::thread::spawn(move || {
            let _ = finish.recv_timeout(Duration::from_millis(300));
            release.send(()).unwrap();
        });
        let result = cleanup_fixture_with_deadline(
            move || {
                struct Held {
                    released: mpsc::Receiver<()>,
                    _not_send: std::rc::Rc<()>,
                }
                impl Drop for Held {
                    fn drop(&mut self) {
                        self.released.recv().unwrap();
                    }
                }
                let _held = Held {
                    released,
                    _not_send: std::rc::Rc::new(()),
                };
                constructed.send(()).unwrap();
            },
            Duration::from_millis(50),
        );
        construction.recv_timeout(Duration::from_secs(1)).unwrap();
        finished.send(()).ok();
        releaser.join().unwrap();
        assert_eq!(
            result,
            Err(mpsc::RecvTimeoutError::Timeout),
            "whole fixture deadline must include non-Send context Drop"
        );
    }

    fn tag(gsn: u64) -> VersionTag {
        VersionTag::Bitcask { gsn }
    }
    fn plan(key: &TestKey) -> PlannedOp<'_> {
        PlannedOp {
            key: key.as_bytes(),
            delete: false,
        }
    }

    #[test]
    fn replay_routes_to_frozen_generation_with_historical_shards() {
        let dir = tempfile::tempdir().unwrap();
        let mut config = SegConfig::test();
        config.shard_count = 3;
        let map = open(dir.path(), config.clone());
        map.put(&key_of(1), &"old".to_owned()).unwrap();
        let old = map.core().view().active.clone();
        // Reserve a real missing logged version before the freeze's range closes.
        let missing = old.engine().gsn().fetch_add(1, Ordering::Relaxed);
        map.seal().unwrap();
        map.put(&key_of(1), &"young".to_owned()).unwrap();
        map.durable_flush().unwrap();
        drop(old);
        drop(map);
        config.shard_count = 7;
        let map = open(dir.path(), config);
        let old = map.core().view().frozen[0].clone();
        assert_eq!(old.shard_count(), 3);
        let younger_next = map.core().view().active.next_gsn();
        let mut replay = map.lock_for_replay(&[0]).unwrap();
        assert!(
            replay
                .needs_replay(key_of(1).as_bytes(), OpKind::Put, tag(missing))
                .unwrap()
        );
        replay
            .apply_put(key_of(1).as_bytes(), b"replayed", tag(missing))
            .unwrap();
        assert_eq!(map.try_get(&key_of(1)).unwrap(), Some("young".to_owned()));
        let h = key_hash(key_of(1).as_bytes(), map.core().seed);
        assert!(matches!(old.lookup(h,&key_of(1)).unwrap(),GenLookup::Live(v) if v==b"replayed"));
        assert_eq!(map.core().view().active.next_gsn(), younger_next);
        // Recovered watermark is the open-time snapshot, never live progress.
        assert!(
            replay
                .needs_replay(key_of(1).as_bytes(), OpKind::Put, tag(missing))
                .unwrap()
        );
        assert_eq!(
            old.engine().shards()[shard_for_hash(h, 3)]
                .lock()
                .armed_gsn(),
            None
        );
        drop(replay);
        map.durable_flush().unwrap();
        drop(old);
        drop(map);
        let map = open(dir.path(), SegConfig::test());
        assert!(
            !map.lock_for_replay(&[])
                .unwrap()
                .needs_replay(key_of(1).as_bytes(), OpKind::Put, tag(missing))
                .unwrap()
        );
    }

    #[test]
    fn replay_has_no_admission_with_full_active_and_saturated_queue() {
        let dir = tempfile::tempdir().unwrap();
        let config = SegConfig {
            seal_max_keys: 1,
            max_frozen_generations: 1,
            ..SegConfig::test()
        };
        let map = open(dir.path(), config.clone());
        map.put(&key_of(1), &"one".to_owned()).unwrap();
        map.seal().unwrap();
        map.put(&key_of(2), &"two".to_owned()).unwrap();
        map.durable_flush().unwrap();
        drop(map);
        let map = open(dir.path(), config);
        let core = map.core();
        let view = core.view();
        assert!(view.active.is_full());
        assert_eq!(view.frozen.len(), 1);
        let mut replay = map.lock_for_replay(&[0, 1]).unwrap();
        let known = view.active.next_gsn() - 1;
        assert!(
            !replay
                .needs_replay(key_of(2).as_bytes(), OpKind::Put, tag(known))
                .unwrap()
        );
        assert_eq!(core.view().active.id(), view.active.id());
        assert_eq!(core.view().frozen.len(), 1);
    }

    #[test]
    fn replay_rejects_wrong_tags_and_generation_gaps_and_skips_old_versions() {
        let dir = tempfile::tempdir().unwrap();
        let map = open(dir.path(), SegConfig::test());
        map.put(&key_of(1), &"one".to_owned()).unwrap();
        map.seal().unwrap();
        let core = map.core();
        let view = core.view();
        // Pin a synthetic gap in the in-memory range for the routing seam.
        let old = view.frozen[0].clone();
        old.mark_frozen(old.frozen_at_ms(), old.min_gsn());
        let mut replay = map.lock_for_replay(&[]).unwrap();
        let wrong = VersionTag::Fixed {
            slot_id: 0,
            target_meta: 0,
        };
        assert!(matches!(
            replay.needs_replay(key_of(1).as_bytes(), OpKind::Put, wrong),
            Err(DbError::Internal(_))
        ));
        assert!(
            !replay
                .needs_replay(key_of(1).as_bytes(), OpKind::Put, tag(0))
                .unwrap()
        );
        // Shift active's lower bound using its seed seam (without touching disk).
        view.active.seed_gsn(old.min_gsn() + 10);
        assert!(matches!(
            replay.needs_replay(key_of(1).as_bytes(), OpKind::Put, tag(old.min_gsn() + 1)),
            Err(DbError::Internal(_))
        ));
    }

    #[test]
    fn commit_cleanup_fences_logged_debt_and_disarms_before_unlock_on_drop() {
        bounded_cleanup(move || {
            let dir = tempfile::tempdir().unwrap();
            let map = open(dir.path(), SegConfig::test());
            let key = key_of(1);
            let shard = map.shard_of(key.as_bytes()).unwrap();
            let mut locked = map.lock_shards(&[shard]).unwrap();
            let tags = locked.reserve(&[plan(&key)]).unwrap();
            locked.mark_logged();
            locked.tx_debug_arm(key.as_bytes(), tags[0]).unwrap();
            let panic = std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || {
                let _held = locked;
                panic!("unwind");
            }));
            assert!(panic.is_err());
            assert!(matches!(
                map.put(&key, &"late".to_owned()),
                Err(DbError::CollectionFenced { .. })
            ));
            let gen_ = map.core().view().active.clone();
            assert_eq!(gen_.engine().shards()[shard].lock().armed_gsn(), None);
            // Reacquisition also proves the holder released its index mutex.
            let _held = gen_.lock_owned(&[shard]).unwrap();
        });
    }

    #[test]
    fn unlogged_abort_is_unfenced_and_reservation_tags_follow_original_order() {
        let dir = tempfile::tempdir().unwrap();
        let map = open(dir.path(), SegConfig::test());
        let keys = (0..20).map(key_of).collect::<Vec<_>>();
        let mut held = map.lock_shards(&[0, 1]).unwrap();
        let ops = keys.iter().map(plan).collect::<Vec<_>>();
        let tags = held.reserve(&ops).unwrap();
        let mut last = [None, None];
        for (key, t) in keys.iter().zip(tags) {
            let sid = held.apply_domain(key.as_bytes()).unwrap();
            let g = bitcask(t).unwrap();
            if let Some(prev) = last[sid] {
                assert_eq!(g, prev + 1);
            }
            last[sid] = Some(g);
        }
        held.abort();
        map.put(&keys[0], &"fine".to_owned()).unwrap();
        assert!(map.core().fence_reason().is_none());
    }

    #[test]
    fn published_participant_uses_same_arc_allocation() {
        let dir = tempfile::tempdir().unwrap();
        let map = Arc::new(open(dir.path(), SegConfig::test()));
        let participant: Arc<dyn TxParticipant> = map.clone();
        let collection: Arc<dyn crate::armour::Collection> = map.clone();
        assert_eq!(
            Arc::as_ptr(&participant) as *const (),
            Arc::as_ptr(&collection) as *const ()
        );
        crate::access::ManagedOpen::bind_tx_arc(&map);
        assert!(Arc::ptr_eq(
            &map,
            &map.tx_self.get().unwrap().upgrade().unwrap()
        ));
        let _ = NoHook;
    }
    #[test]
    fn saturated_commit_is_rejected_before_txlog_growth() {
        let dir = tempfile::tempdir().unwrap();
        let db = crate::armour::Db::open_test(dir.path().join("db")).unwrap();
        let config = SegConfig {
            seal_max_keys: 1,
            max_frozen_generations: 1,
            seal_wait_timeout: std::time::Duration::from_millis(1),
            ..SegConfig::test()
        };
        let map = Arc::new(open(&dir.path().join("rows"), config));
        crate::sync::lock(&db.collections).push(map.clone());
        map.put(&key_of(1), &"one".to_owned()).unwrap();
        map.seal().unwrap();
        map.put(&key_of(2), &"two".to_owned()).unwrap();
        let before = db.txlog().total_bytes();
        let failure = db
            .tx()
            .put(&map, &key_of(3), &"three".to_owned())
            .unwrap()
            .commit();
        assert!(
            matches!(failure, Err(DbError::SealBackpressure { .. })),
            "{failure:?}"
        );
        assert_eq!(db.txlog().total_bytes(), before);
        assert_eq!(map.try_get(&key_of(3)).unwrap(), None);
    }

    #[test]
    fn logged_other_shard_drains_and_finish_reports_real_debt() {
        let dir = tempfile::tempdir().unwrap();
        let config = SegConfig {
            shard_count: 3,
            ..SegConfig::test()
        };
        let map = open(dir.path(), config);
        map.durable_flush().unwrap();
        drop(map);
        let map = open(
            dir.path(),
            SegConfig {
                shard_count: 7,
                ..SegConfig::test()
            },
        );
        let first = key_of(1);
        let h = key_hash(first.as_bytes(), map.core().seed);
        let domain = shard_for_hash(h, 3);
        let other = (2..1000)
            .map(key_of)
            .find(|k| {
                let first_real =
                    overlapping_shards(&[map.shard_of(first.as_bytes()).unwrap()], 7, 3).unwrap();
                let other_real =
                    overlapping_shards(&[map.shard_of(k.as_bytes()).unwrap()], 7, 3).unwrap();
                other_real.iter().all(|s| !first_real.contains(s))
            })
            .unwrap();
        let mut debt = map
            .lock_shards(&[map.shard_of(first.as_bytes()).unwrap()])
            .unwrap();
        let mut drain = map
            .lock_shards(&[map.shard_of(other.as_bytes()).unwrap()])
            .unwrap();
        let debt_tag = debt.reserve(&[plan(&first)]).unwrap()[0];
        let drain_tag = drain.reserve(&[plan(&other)]).unwrap()[0];
        debt.mark_logged();
        drain.mark_logged();
        let mut fenced = Vec::new();
        debt.finish(&mut fenced, &mut crate::watch::WatchWakeBatch::default())
            .unwrap();
        assert_eq!(fenced, vec![(domain, bitcask(debt_tag).unwrap())]);
        drain
            .apply_put(other.as_bytes(), b"drained", drain_tag)
            .unwrap();
        let mut healthy = Vec::new();
        drain
            .finish(&mut healthy, &mut crate::watch::WatchWakeBatch::default())
            .unwrap();
        assert!(healthy.is_empty());
        assert_eq!(map.try_get(&other).unwrap(), Some("drained".to_owned()));
    }

    #[test]
    fn replay_apply_failure_clears_armed_gsn_before_unlock() {
        let dir = tempfile::tempdir().unwrap();
        let map = open(dir.path(), SegConfig::test());
        let view = map.core().view();
        let key = key_of(1);
        let gsn = view.active.next_gsn();
        let mut replay = map.lock_for_replay(&[]).unwrap();
        let too_big = vec![0; map.config().write_buffer_size + 1];
        assert!(
            replay
                .apply_put(key.as_bytes(), &too_big, tag(gsn))
                .is_err()
        );
        let s = shard_for_hash(
            key_hash(key.as_bytes(), map.core().seed),
            view.active.shard_count(),
        );
        assert_eq!(view.active.engine().shards()[s].lock().armed_gsn(), None);
        let _held = view.active.lock_owned(&[s]).unwrap();
    }
    #[test]
    fn journal_entry_cap_rejects_before_log_append() {
        let dir = tempfile::tempdir().unwrap();
        let db = crate::armour::Db::open_test(dir.path().join("db")).unwrap();
        let map = Arc::new(open(&dir.path().join("rows"), SegConfig::test()));
        crate::sync::lock(&db.collections).push(map.clone());
        let before = db.txlog().total_bytes();
        let value = "x".repeat(map.config().write_buffer_size);
        assert!(
            db.tx()
                .put(&map, &key_of(1), &value)
                .unwrap()
                .commit()
                .is_err()
        );
        assert_eq!(db.txlog().total_bytes(), before);
        assert_eq!(map.try_get(&key_of(1)).unwrap(), None);
        assert_eq!(map.max_entry_bytes(), Some(map.config().write_buffer_size));
    }

    #[test]
    fn physical_domain_missing_write_recovers_with_more_and_fewer_planner_shards() {
        for (physical, planner) in [(2, 4), (4, 2)] {
            let dir = tempfile::tempdir().unwrap();
            let path = dir.path().join("rows");
            let initial = open(
                &path,
                SegConfig {
                    shard_count: physical,
                    ..SegConfig::test()
                },
            );
            initial.durable_flush().unwrap();
            drop(initial);
            let cfg = SegConfig {
                shard_count: planner,
                ..SegConfig::test()
            };
            let map = Arc::new(open(&path, cfg.clone()));
            let gen_ = map.core().view().active.clone();
            assert_eq!(gen_.shard_count(), physical);
            let keys = (0..2000).map(key_of).collect::<Vec<_>>();
            let real = |k: &TestKey| shard_for_hash(key_hash(k, map.core().seed), physical);
            let planned = |k: &TestKey| map.shard_of(k).unwrap();
            let first = keys[0];
            let same = *keys
                .iter()
                .find(|k| {
                    **k != first
                        && real(k) == real(&first)
                        && (physical >= planner || planned(k) != planned(&first))
                })
                .unwrap();
            let other = *keys
                .iter()
                .find(|k| {
                    real(k) != real(&first)
                        && (physical <= planner || planned(k) == planned(&first))
                })
                .unwrap();
            let db = crate::armour::Db::open_test(dir.path().join("db")).unwrap();
            crate::sync::lock(&db.collections).push(map.clone());
            let failure = {
                let _fail = crate::txn::failpoints::ApplyFailGuard::at(0);
                db.tx()
                    .put(&map, &first, &"missing".to_owned())
                    .unwrap()
                    .put(&map, &same, &"same-domain".to_owned())
                    .unwrap()
                    .put(&map, &other, &"healthy".to_owned())
                    .unwrap()
                    .durable()
                    .commit()
            };
            match failure {
                Err(DbError::TxFailedAfterLog { fenced, .. }) => {
                    assert_eq!(fenced.len(), 1);
                    assert_eq!(fenced[0].shard, real(&first));
                }
                other => panic!("expected physical-domain failure, got {other:?}"),
            }
            assert_eq!(map.try_get(&first).unwrap(), None);
            assert_eq!(map.try_get(&other).unwrap(), Some("healthy".to_owned()));
            map.durable_flush().unwrap(); // makes wrong-domain watermark advancement observable
            for shard in gen_.engine().shards().iter() {
                assert_eq!(shard.lock().armed_gsn(), None);
            }
            drop(gen_);
            drop(map);
            drop(db);
            let db = crate::armour::Db::open_test(dir.path().join("db")).unwrap();
            let map = open(&path, cfg);
            crate::txn::replay::replay_pending(&db, &map, None, None).unwrap();
            assert_eq!(map.try_get(&first).unwrap(), Some("missing".to_owned()));
            assert_eq!(map.try_get(&same).unwrap(), Some("same-domain".to_owned()));
            assert_eq!(map.try_get(&other).unwrap(), Some("healthy".to_owned()));
        }
    }

    #[test]
    fn cleanup_abort_finish_error_and_panic_after_arm_or_apply_disarm_and_fence() {
        bounded_cleanup(move || {
            for mode in 0..4 {
                let dir = tempfile::tempdir().unwrap();
                let map = open(dir.path(), SegConfig::test());
                let keys = [key_of(1), key_of(2)];
                let mut held = map.lock_shards(&[0, 1]).unwrap();
                let tags = held
                    .reserve(&keys.iter().map(plan).collect::<Vec<_>>())
                    .unwrap();
                held.mark_logged();
                if mode == 3 {
                    held.apply_put(&keys[0], b"applied", tags[0]).unwrap();
                }
                held.tx_debug_arm(&keys[1], tags[1]).unwrap();
                match mode {
                    0 => held.abort(),
                    1 => {
                        let _fail = crate::seg::failpoints::FinishFailGuard::new();
                        assert!(
                            held.finish(
                                &mut Vec::new(),
                                &mut crate::watch::WatchWakeBatch::default()
                            )
                            .is_err()
                        );
                    }
                    2 | 3 => {
                        assert!(
                            std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || {
                                let _held = held;
                                panic!("logged cleanup unwind");
                            }))
                            .is_err()
                        );
                    }
                    _ => unreachable!(),
                }
                assert!(matches!(
                    map.put(&keys[1], &"late".to_owned()),
                    Err(DbError::CollectionFenced { .. })
                ));
                assert!(matches!(map.seal(), Err(DbError::CollectionFenced { .. })));
                assert_eq!(map.try_get(&keys[1]).unwrap(), None);
                assert_eq!(
                    map.try_get(&keys[0]).unwrap(),
                    (mode == 3).then_some("applied".to_owned())
                );
                let gen_ = map.core().view().active.clone();
                assert!(map.core().view().frozen.is_empty());
                for shard in gen_.engine().shards().iter() {
                    assert_eq!(shard.lock().armed_gsn(), None);
                }
                let _locks = gen_.lock_owned(&[0, 1]).unwrap();
            }
        });
    }

    #[test]
    fn cleanup_waiting_facade_and_freeze_refuse_after_logged_abort_before_unlock() {
        bounded_cleanup(move || {
            use std::time::Duration;
            let dir = tempfile::tempdir().unwrap();
            let map = open(dir.path(), SegConfig::test());
            map.put(&key_of(0), &"existing".to_owned()).unwrap();
            let gen_ = map.core().view().active.clone();
            std::thread::scope(|scope| {
                let key = key_of(1);
                let mut held = map.lock_shards(&[0, 1]).unwrap();
                let tag = held.reserve(&[plan(&key)]).unwrap()[0];
                held.mark_logged();
                held.tx_debug_arm(&key, tag).unwrap();
                let writer_waiting = gen_.observe_index_lock_attempt();
                let writer = scope.spawn(|| map.put(&key_of(1), &"refused".to_owned()));
                writer_waiting.recv_timeout(Duration::from_secs(5)).unwrap();
                let freeze_waiting = gen_.observe_index_lock_attempt();
                let freeze = scope.spawn(|| map.seal());
                freeze_waiting.recv_timeout(Duration::from_secs(5)).unwrap();
                let manifest =
                    std::fs::read(dir.path().join(crate::seg::manifest::MANIFEST_NAME)).unwrap();
                held.abort();
                assert!(matches!(
                    writer.join().unwrap(),
                    Err(DbError::CollectionFenced { .. })
                ));
                assert!(matches!(
                    freeze.join().unwrap(),
                    Err(DbError::CollectionFenced { .. })
                ));
                assert_eq!(
                    std::fs::read(dir.path().join(crate::seg::manifest::MANIFEST_NAME)).unwrap(),
                    manifest
                );
            });
            assert_eq!(
                map.try_get(&key_of(0)).unwrap(),
                Some("existing".to_owned())
            );
            assert_eq!(map.try_get(&key_of(1)).unwrap(), None);
            assert!(!gen_.is_frozen());
            assert!(map.core().view().frozen.is_empty());
            for shard in gen_.engine().shards().iter() {
                assert_eq!(shard.lock().armed_gsn(), None);
            }
        });
    }

    #[test]
    fn cleanup_concurrent_logged_real_shard_drains_after_other_domain_fences() {
        bounded_cleanup(move || {
            use std::time::Duration;
            let dir = tempfile::tempdir().unwrap();
            let initial = open(
                dir.path(),
                SegConfig {
                    shard_count: 3,
                    ..SegConfig::test()
                },
            );
            initial.durable_flush().unwrap();
            drop(initial);
            let map = Arc::new(open(
                dir.path(),
                SegConfig {
                    shard_count: 7,
                    ..SegConfig::test()
                },
            ));
            let first = key_of(1);
            let first_real = overlapping_shards(&[map.shard_of(&first).unwrap()], 7, 3).unwrap();
            let other = (2..1000)
                .map(key_of)
                .find(|k| {
                    overlapping_shards(&[map.shard_of(k).unwrap()], 7, 3)
                        .unwrap()
                        .iter()
                        .all(|s| !first_real.contains(s))
                })
                .unwrap();
            let mut debt = map.lock_shards(&[map.shard_of(&first).unwrap()]).unwrap();
            let tag = debt.reserve(&[plan(&first)]).unwrap()[0];
            debt.mark_logged();
            debt.tx_debug_arm(&first, tag).unwrap();
            let (logged_tx, logged_rx) = std::sync::mpsc::channel();
            let (drain_tx, drain_rx) = std::sync::mpsc::channel();
            std::thread::scope(|scope| {
                let debt = debt; // context and scope unwind stay inside the bounded owner
                let map = &map;
                let drain = scope.spawn(move || {
                    let mut held = map.lock_shards(&[map.shard_of(&other).unwrap()]).unwrap();
                    let tag = held.reserve(&[plan(&other)]).unwrap()[0];
                    held.mark_logged();
                    logged_tx.send(()).unwrap();
                    drain_rx.recv_timeout(Duration::from_secs(5)).unwrap();
                    held.apply_put(&other, b"drained", tag).unwrap();
                    let mut fenced = Vec::new();
                    held.finish(&mut fenced, &mut crate::watch::WatchWakeBatch::default())
                        .unwrap();
                    assert!(fenced.is_empty());
                });
                logged_rx.recv_timeout(Duration::from_secs(5)).unwrap();
                let mut fenced = Vec::new();
                debt.finish(&mut fenced, &mut crate::watch::WatchWakeBatch::default())
                    .unwrap();
                assert_eq!(
                    fenced,
                    vec![(
                        shard_for_hash(key_hash(&first, map.core().seed), 3),
                        bitcask(tag).unwrap()
                    )]
                );
                drain_tx.send(()).unwrap();
                drain.join().unwrap();
            });
            assert_eq!(map.try_get(&other).unwrap(), Some("drained".to_owned()));
            assert_eq!(map.try_get(&first).unwrap(), None);
            for shard in map.core().view().active.engine().shards().iter() {
                assert_eq!(shard.lock().armed_gsn(), None);
            }
        });
    }

    #[test]
    fn cleanup_finish_error_reports_finish_stage_after_all_applies_without_phantom_debt() {
        bounded_cleanup(move || {
            let dir = tempfile::tempdir().unwrap();
            let db = crate::armour::Db::open_test(dir.path().join("db")).unwrap();
            let map = Arc::new(open(&dir.path().join("rows"), SegConfig::test()));
            crate::sync::lock(&db.collections).push(map.clone());
            let failure = {
                let _fail = crate::seg::failpoints::FinishFailGuard::new();
                db.tx()
                    .put(&map, &key_of(1), &"applied".to_owned())
                    .unwrap()
                    .durable()
                    .commit()
            };
            match failure {
                Err(DbError::TxFailedAfterLog { stage, fenced, .. }) => {
                    assert_eq!(stage, crate::TxFailStage::Finish);
                    assert!(fenced.is_empty());
                }
                other => panic!("expected post-log finish error, got {other:?}"),
            }
            assert_eq!(map.try_get(&key_of(1)).unwrap(), Some("applied".to_owned()));
            assert!(map.core().fence_reason().is_none());
            map.put(&key_of(2), &"later".to_owned()).unwrap();
            for shard in map.core().view().active.engine().shards().iter() {
                assert_eq!(shard.lock().armed_gsn(), None);
            }
        });
    }

    #[test]
    fn cleanup_concurrent_admission_bounds_threshold_overshoot_by_admitted_batches() {
        bounded_cleanup(move || {
            use std::time::Duration;
            let dir = tempfile::tempdir().unwrap();
            let db = Arc::new(crate::armour::Db::open_test(dir.path().join("db")).unwrap());
            let map = Arc::new(open(
                &dir.path().join("rows"),
                SegConfig {
                    seal_max_keys: 2,
                    sync_batch_size: 100_000,
                    sync_interval: None,
                    ..SegConfig::test()
                },
            ));
            crate::sync::lock(&db.collections).push(map.clone());
            let gen_ = map.core().view().active.clone();
            let mut keys = [Vec::new(), Vec::new()];
            for key in (0..1000).map(key_of) {
                let shard = shard_for_hash(key_hash(&key, map.core().seed), 2);
                if keys[shard].len() < 3 {
                    keys[shard].push(key);
                }
                if keys.iter().all(|v| v.len() == 3) {
                    break;
                }
            }
            assert!(keys.iter().all(|v| v.len() == 3));
            let (admitted_tx, admitted_rx) = std::sync::mpsc::channel();
            let (release_a, released_a) = std::sync::mpsc::channel();
            let (release_b, released_b) = std::sync::mpsc::channel();
            std::thread::scope(|scope| {
                let mut handles = Vec::new();
                for (keys, released) in keys.into_iter().zip([released_a, released_b]) {
                    let db = db.clone();
                    let map = map.clone();
                    let gen_ = gen_.clone();
                    let admitted = admitted_tx.clone();
                    handles.push(scope.spawn(move || {
                        let mut tx = db
                            .tx()
                            .guard(&map, &keys[0], move |_| {
                                assert!(
                                    !gen_.is_full(),
                                    "both batches must enter admission before apply"
                                );
                                admitted.send(()).unwrap();
                                released.recv_timeout(Duration::from_secs(5)).unwrap();
                                Ok(())
                            })
                            .unwrap();
                        for key in keys {
                            tx = tx.put(&map, &key, &"batch".to_owned()).unwrap();
                        }
                        tx.durable().commit().unwrap();
                    }));
                }
                // Receives bound admission; the outer fixture deadline also bounds
                // commit, joins and all destructors, including lock-order failures.
                admitted_rx.recv_timeout(Duration::from_secs(5)).unwrap();
                admitted_rx.recv_timeout(Duration::from_secs(5)).unwrap();
                assert_eq!(gen_.keys(), 0);
                release_a.send(()).unwrap();
                release_b.send(()).unwrap();
                for handle in handles {
                    handle.join().unwrap();
                }
            });
            assert!(Arc::ptr_eq(&gen_, &map.core().view().active));
            assert_eq!(
                gen_.keys(),
                6,
                "exactly two already-admitted three-write batches"
            );
            assert_eq!(
                gen_.bytes(),
                6 * crate::entry::entry_size(size_of::<TestKey>(), 5) as u64
            );
            assert!(gen_.is_full());
            assert!(map.core().view().frozen.is_empty());
        });
    }

    type Seen = Arc<std::sync::Mutex<Vec<(Option<String>, Option<String>)>>>;
    struct Recorder(Seen);
    impl TypedWriteHook<TestKey, String> for Recorder {
        fn on_write(&self, _: &TestKey, old: Option<&String>, new: Option<&String>) {
            self.0.lock().unwrap().push((old.cloned(), new.cloned()));
        }
    }

    #[test]
    fn replay_fires_on_write_for_the_visible_transition_only() {
        let dir = tempfile::tempdir().unwrap();
        let seen: Seen = Arc::default();
        let map = Arc::new(
            SegMap::open_deferred(
                dir.path(),
                meta(),
                SegConfig::test(),
                TextCodec,
                Recorder(seen.clone()),
            )
            .unwrap(),
        );
        crate::access::ManagedOpen::bind_tx_arc(&map);
        map.core().put(&key_of(1), b"old").unwrap();
        // A frozen-layer version shadowed by a newer active one is invisible.
        let frozen = map.core().view().active.clone();
        let shadowed = frozen.engine().gsn().fetch_add(1, Ordering::Relaxed);
        map.seal().unwrap();
        map.core().put(&key_of(1), b"young").unwrap();
        let fresh = map
            .core()
            .view()
            .active
            .engine()
            .gsn()
            .fetch_add(1, Ordering::Relaxed);
        let mut replay = map.lock_for_replay(&[]).unwrap();
        assert_eq!(
            replay
                .apply_put(key_of(1).as_bytes(), b"shadowed", tag(shadowed))
                .unwrap(),
            0
        );
        assert_eq!(
            replay
                .apply_put(key_of(2).as_bytes(), b"new", tag(fresh))
                .unwrap(),
            1
        );
        let mut deferred = replay
            .finish(
                &mut Vec::new(),
                &mut crate::watch::WatchWakeBatch::default(),
            )
            .unwrap();
        assert_eq!(deferred.event_count(), 1);
        deferred.replay_one(0);
        assert_eq!(*seen.lock().unwrap(), vec![(None, Some("new".to_owned()))]);
    }

    #[test]
    fn admission_waits_before_earlier_participants_are_locked() {
        use crate::counter_tests::Group;
        use std::time::Duration;
        let dir = tempfile::tempdir().unwrap();
        let db = Arc::new(crate::armour::Db::open_test(dir.path().join("db")).unwrap());
        // Published first: locked before the segmented participant.
        let native = db
            .open_var_typed_map::<Group, crate::RapiraCodec, _>(crate::Config::test(), NoHook, &[])
            .unwrap();
        let wait = Duration::from_millis(1500);
        let map = Arc::new(open(
            &dir.path().join("rows"),
            SegConfig {
                seal_max_keys: 1,
                max_frozen_generations: 1,
                seal_wait_timeout: wait,
                ..SegConfig::test()
            },
        ));
        crate::sync::lock(&db.collections).push(map.clone());
        map.put(&key_of(1), &"one".to_owned()).unwrap();
        map.seal().unwrap();
        map.put(&key_of(2), &"two".to_owned()).unwrap();
        assert!(map.core().view().active.is_full());
        let row = 7u64.to_be_bytes();
        let before = db.txlog().total_bytes();
        std::thread::scope(|scope| {
            let committer = scope.spawn(|| {
                db.tx()
                    .put(&native, &row, &Group { member_count: 1 })
                    .unwrap()
                    .put(&map, &key_of(3), &"three".to_owned())
                    .unwrap()
                    .commit()
            });
            std::thread::sleep(Duration::from_millis(200));
            // The commit is waiting for seal room; the native row is not locked.
            let started = std::time::Instant::now();
            native.put(&row, &Group { member_count: 2 }).unwrap();
            assert!(started.elapsed() < wait / 2, "{:?}", started.elapsed());
            assert!(!committer.is_finished(), "admission must still be waiting");
            assert!(matches!(
                committer.join().unwrap(),
                Err(DbError::SealBackpressure { .. })
            ));
        });
        assert_eq!(db.txlog().total_bytes(), before);
        assert_eq!(
            native.try_get(&row).unwrap(),
            Some(Group { member_count: 2 })
        );
        assert_eq!(map.try_get(&key_of(3)).unwrap(), None);
    }
}