crabka-remote-storage-topic 0.3.6

Topic-backed RemoteLogMetadataManager for Crabka tiered storage
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
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
//! [`TopicBasedRemoteLogMetadataManager`] — production
//! [`RemoteLogMetadataManager`] implementation backed by a publish /
//! subscribe [`MetadataEventLog`].
//!
//! The manager keeps the canonical in-memory view in an
//! [`InmemoryRemoteLogMetadataManager`] (so the lifecycle state
//! machine is the single source of truth for cache mutation) and uses
//! the [`MetadataEventLog`] as the durable event log.
//!
//! Lifecycle:
//!
//! - [`TopicBasedRemoteLogMetadataManager::start`]: load any on-disk
//!   snapshot into the cache and spawn the consumer pump subscribed to
//!   NOTHING. The broker then drives the consumed set via
//!   [`TopicBasedRemoteLogMetadataManager::reconcile_assignment`], adding
//!   only the `__remote_log_metadata` partitions covering the
//!   user-partitions this broker leads or follows. A newly-added partition
//!   is gated by `NotReady` until the pump reaches the HWM observed at
//!   assignment time; a partition this broker does not consume is a genuine
//!   `Ok(None)` (never served from any stale cache).
//! - Mutation calls (`add`/`update`/`put_partition_delete`):
//!   serialize, publish, and wait until the consumer pump has applied
//!   the published offset to the inner cache. The sync return implies
//!   "the event has been recorded and is visible to local reads".
//! - Read calls: pure local lookups against the inner cache.
//! - Drop / [`TopicBasedRemoteLogMetadataManager::shutdown`]: cancel the consumer pump.

use std::sync::Arc;

use bytes::Bytes;
use futures_util::StreamExt;
use tokio::runtime::Handle;
use tokio::sync::watch;
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;
use tracing::warn;

use crabka_remote_storage::{
    InmemoryRemoteLogMetadataManager, RemoteLogMetadataManager, RemoteLogSegmentMetadata,
    RemoteLogSegmentMetadataUpdate, RemoteLogSegmentState, RemotePartitionDeleteMetadata,
    RemoteStorageError, TopicIdPartition,
};

use crate::error::MetadataLogError;
use crate::log::{AssignmentHandle, MetadataEventLog, MetadataEventStream, PartitionStart};
use crate::partitioning::metadata_partition_for;
use crate::serde::MetadataEvent;

/// Sentinel target HWM meaning "this partition is assigned but its real
/// high-water mark is not yet known" (the `high_water_marks` RPC failed,
/// or the partition had no entry in the returned index). The gate treats
/// it as `NotReady` (a real applied offset can never reach `i64::MAX`),
/// and the next `reconcile_assignment` re-attempts the HWM fetch to
/// replace it with the real target.
const HWM_UNKNOWN: i64 = i64::MAX;

/// Outcome of the per-metadata-partition readiness check that gates the
/// gated [`RemoteLogMetadataManager`] read methods
/// ([`RemoteLogMetadataManager::remote_log_segment_metadata`],
/// [`RemoteLogMetadataManager::list_remote_log_segments`],
/// [`RemoteLogMetadataManager::highest_offset_for_epoch`]).
enum ReadGate {
    /// This broker does not consume the metadata partition (neither leads
    /// nor follows any covered user-partition) → answer `Ok(None)`.
    Unassigned,
    /// Assigned but the consumer pump has not reached the assignment-time
    /// HWM → answer `Err(NotReady)` (retryable).
    NotReady,
    /// Assigned and caught up → delegate to the inner cache.
    Ready,
}

/// Production [`RemoteLogMetadataManager`] backed by the
/// `__remote_log_metadata` topic (via a [`MetadataEventLog`]
/// adapter).
///
/// Construct with [`Self::start`]; it loads any on-disk snapshot but
/// consumes no metadata partitions until [`Self::reconcile_assignment`]
/// adds the broker's leader/follower-derived set.
pub struct TopicBasedRemoteLogMetadataManager {
    log: Arc<dyn MetadataEventLog>,
    inner: Arc<InmemoryRemoteLogMetadataManager>,
    applied: Arc<std::sync::Mutex<Vec<i64>>>,
    applied_tx: watch::Sender<u64>,
    runtime: Handle,
    shutdown: CancellationToken,
    pump: std::sync::Mutex<Option<JoinHandle<()>>>,
    /// Directory the on-disk RLMM cache snapshot is written to (one
    /// [`SNAPSHOT_FILE_NAME`](crate::snapshot::SNAPSHOT_FILE_NAME) file).
    snapshot_dir: std::path::PathBuf,
    /// Handle of the background snapshotter task; aborted on `Drop`,
    /// joined on [`Self::shutdown_and_flush`].
    snapshotter: std::sync::Mutex<Option<JoinHandle<()>>>,
    /// Live assignment handle for the metadata-log subscription. Held so
    /// resume-from-snapshot and per-broker partition-assignment logic
    /// assignment) can mutate the consumed set at runtime. Driven by
    /// [`Self::reconcile_assignment`].
    assignment: Arc<dyn AssignmentHandle>,
    /// Per-metadata-partition committed offsets loaded from the snapshot
    /// at `start()`, indexed by metadata partition (`-1` == no committed
    /// event for that partition / full replay). Retained as the single
    /// canonical source for resume-offset lookups; assignment
    /// reconciler reads it via [`Self::committed_offset`] when it
    /// dynamically adds a partition (to start at `committed + 1`).
    committed_offsets: Vec<i64>,
    /// Metadata partition → target HWM observed at assignment time.
    /// Presence == this manager is currently assigned that partition;
    /// reads for a user-partition hashing into it return
    /// [`RemoteStorageError::NotReady`] until `applied[mp] >= target - 1`.
    /// Empty for managers that never call [`Self::reconcile_assignment`]
    /// (every read then delegates straight to `inner`).
    ready_targets: Arc<std::sync::Mutex<std::collections::HashMap<i32, i64>>>,
}

impl TopicBasedRemoteLogMetadataManager {
    /// Load any on-disk snapshot into the cache and spawn the consumer
    /// pump with an empty assignment. The manager consumes nothing until
    /// [`Self::reconcile_assignment`] is driven (by the broker).
    ///
    /// `runtime` must be a Tokio runtime handle that lives at least
    /// as long as the returned manager. The synchronous
    /// [`RemoteLogMetadataManager`] methods bridge to this handle via
    /// `block_on`, so they must NOT be called from a task running on
    /// this same runtime — the broker only invokes them through
    /// `spawn_blocking`, which is the only supported call pattern.
    ///
    /// # Errors
    ///
    /// Currently infallible (the consumed set starts empty), but returns a
    /// `Result` so the bootstrap contract stays stable if `start` regains a
    /// fallible step.
    // Kept `async` for the established 4-arg bootstrap contract the broker
    // awaits; bootstrap no longer blocks on catch-up (it consumes nothing
    // until `reconcile_assignment`), so there is no internal `.await`.
    #[allow(clippy::unused_async)]
    pub async fn start(
        log: Arc<dyn MetadataEventLog>,
        runtime: Handle,
        snapshot_dir: std::path::PathBuf,
        snapshot_interval: std::time::Duration,
    ) -> Result<Arc<Self>, RemoteStorageError> {
        let n = usize::try_from(log.partition_count()).expect("partition_count fits in usize");
        let (applied_tx, _) = watch::channel(0u64);
        let inner = Arc::new(InmemoryRemoteLogMetadataManager::new());
        let shutdown = CancellationToken::new();

        // Load the snapshot (if any) ONCE and seed the cache from its
        // dump. `resume_from_snapshot` is the single canonical place that
        // turns a loaded snapshot into the per-partition committed offsets.
        // On absence/corruption, committed[] is all -1 (full replay) and the
        // cache stays empty — never fatal.
        let snapshot = match crate::snapshot::Snapshot::load(
            &snapshot_dir.join(crate::snapshot::SNAPSHOT_FILE_NAME),
        ) {
            Ok(snap) => snap,
            Err(e) => {
                warn!(error = ?e, "topic-based RLMM: snapshot corrupt; starting from empty cache");
                None
            }
        };
        if let Some(snap) = &snapshot {
            inner.import(snap.dump.clone());
        }
        // A freshly-started manager consumes NOTHING. The broker drives
        // the consumed set via [`Self::reconcile_assignment`], adding only the
        // metadata partitions covering user-partitions this broker leads or
        // follows (each resumed at its snapshot `committed + 1`). This is what
        // makes an unassigned partition a genuine `Ok(None)` rather than a
        // false hit from globally-replayed state.
        let (committed, _assignment) = Self::resume_from_snapshot(snapshot.as_ref(), n);

        // Pre-seed `applied` to the committed offsets so readiness checks for
        // a later-added partition only block on the delta from committed+1 to
        // the assignment-time HWM.
        let applied = Arc::new(std::sync::Mutex::new(committed.clone()));

        let (stream, assignment_handle) = log.subscribe(Vec::new());
        let pump = runtime.spawn(pump_loop(
            stream,
            inner.clone(),
            applied.clone(),
            applied_tx.clone(),
            shutdown.clone(),
        ));

        let manager = Arc::new(Self {
            log,
            inner,
            applied,
            applied_tx,
            runtime,
            shutdown,
            pump: std::sync::Mutex::new(Some(pump)),
            snapshot_dir,
            snapshotter: std::sync::Mutex::new(None),
            assignment: assignment_handle,
            committed_offsets: committed,
            ready_targets: Arc::new(std::sync::Mutex::new(std::collections::HashMap::new())),
        });

        // Spawn the periodic snapshotter: flush whenever the cache
        // advanced since the last write, plus a final flush on shutdown.
        let snapshotter = {
            let weak = Arc::downgrade(&manager);
            let shutdown = manager.shutdown.clone();
            manager.runtime.spawn(async move {
                let mut last_written: i64 = -1;
                loop {
                    tokio::select! {
                        biased;
                        () = shutdown.cancelled() => return,
                        () = tokio::time::sleep(snapshot_interval) => {}
                    }
                    let Some(m) = weak.upgrade() else { return };
                    // Only write when the cache advanced since the last snapshot.
                    let highest = {
                        let applied = m.applied.lock().expect("applied mutex poisoned");
                        applied.iter().copied().max().unwrap_or(-1)
                    };
                    if highest > last_written {
                        match m.write_snapshot() {
                            Ok(written) => last_written = written,
                            Err(e) => {
                                warn!(error = ?e, "topic-based RLMM: periodic snapshot failed");
                            }
                        }
                    }
                }
            })
        };
        *manager
            .snapshotter
            .lock()
            .expect("snapshotter mutex poisoned") = Some(snapshotter);

        // Nothing is consumed at bootstrap (empty assignment), so the manager
        // is immediately ready. Per-partition catch-up after a later
        // `reconcile_assignment` is governed by `metadata_partition_ready`,
        // which gates reads with `NotReady` until the pump reaches the
        // assignment-time HWM.
        Ok(manager)
    }

    /// Cancel the consumer pump. Read methods continue to work
    /// against whatever was applied before shutdown; mutation methods
    /// will time out / fail to make progress.
    pub fn shutdown(&self) {
        self.shutdown.cancel();
    }

    /// Cancel the pump + snapshotter, then write a final snapshot
    /// capturing everything applied so far. Safe to call once on
    /// graceful shutdown.
    pub async fn shutdown_and_flush(&self) {
        self.shutdown.cancel();
        // Take the handle out of the lock BEFORE awaiting it, so the
        // (sync) mutex is not held across the await point.
        let handle = self
            .snapshotter
            .lock()
            .expect("snapshotter mutex poisoned")
            .take();
        // Let the snapshotter observe cancellation and stop touching
        // `applied` before we take the final consistent capture.
        if let Some(h) = handle {
            let _ = h.await;
        }
        if let Err(e) = self.write_snapshot() {
            warn!(error = ?e, "topic-based RLMM: final snapshot flush failed");
        }
    }

    /// Capture the pump's committed offsets together with a cache
    /// export under a consistent lock, and write a snapshot. The
    /// `applied` lock is held only long enough to clone the offsets and
    /// run `export()` (which takes the inner partitions lock); no Kafka
    /// round-trips happen inside, so the hold is bounded. Returns the
    /// highest committed offset written (for the "advanced since last"
    /// check).
    fn write_snapshot(&self) -> Result<i64, crate::error::SnapshotError> {
        // Benign-replay invariant: the pump updates `inner` BEFORE bumping
        // `applied`, so the captured cache may lead the captured committed
        // offset by at most one event (the in-flight one). On resume that
        // single event is replayed from committed+1 and harmlessly
        // re-rejected: a re-applied AddSegment hits already-exists, and a
        // re-applied finished→finished update is a no-op. The dangerous
        // direction — cache BEHIND committed, which would skip an event on
        // resume — cannot occur because inner is always updated first.
        let (committed_offsets, dump) = {
            let applied = self.applied.lock().expect("applied mutex poisoned");
            let dump = self.inner.export();
            (applied.clone(), dump)
        };
        let max = committed_offsets.iter().copied().max().unwrap_or(-1);
        let snap = crate::snapshot::Snapshot {
            committed_offsets,
            dump,
        };
        let path = self.snapshot_dir.join(crate::snapshot::SNAPSHOT_FILE_NAME);
        snap.write_atomic(&path)?;
        Ok(max)
    }

    /// Canonical resume-from-snapshot computation, shared by `start()` and
    /// the resume tests. Given an already-loaded snapshot (or `None` for a
    /// missing/corrupt one) and the metadata-partition count `n`, produce:
    ///
    /// - the per-partition committed offsets, indexed by metadata
    ///   partition and padded/truncated to `n` (`-1` == no committed
    ///   event → full replay for that partition), and
    /// - the metadata-consumer assignment that resumes each partition at
    ///   `committed + 1`.
    ///
    /// This is the ONLY place the `committed + 1` resume policy lives; do
    /// not recompute it elsewhere.
    fn resume_from_snapshot(
        snapshot: Option<&crate::snapshot::Snapshot>,
        n: usize,
    ) -> (Vec<i64>, Vec<PartitionStart>) {
        let mut committed = vec![-1i64; n];
        if let Some(snap) = snapshot {
            for (i, &off) in snap.committed_offsets.iter().take(n).enumerate() {
                committed[i] = off;
            }
        }
        let assignment = (0..n)
            .map(|i| PartitionStart {
                partition: i32::try_from(i).expect("partition fits in i32"),
                start_offset: committed[i] + 1,
            })
            .collect();
        (committed, assignment)
    }

    /// Committed offset loaded from the snapshot for a single metadata
    /// partition, or `-1` when the partition is out of range or had no
    /// committed event (full replay). The assignment reconciler uses
    /// this to start a dynamically-added partition at `committed + 1`.
    #[must_use]
    pub fn committed_offset(&self, partition: i32) -> i64 {
        usize::try_from(partition)
            .ok()
            .and_then(|i| self.committed_offsets.get(i).copied())
            .unwrap_or(-1)
    }

    /// The read decision for metadata partition `mp`, used to gate
    /// [`RemoteLogMetadataManager::remote_log_segment_metadata`].
    fn metadata_partition_gate(&self, mp: i32) -> ReadGate {
        let target = {
            let guard = self.ready_targets.lock().expect("ready_targets poisoned");
            match guard.get(&mp) {
                Some(&t) => t,
                // Not assigned: this broker neither leads nor follows any
                // user-partition in `mp`, so it must not answer from any
                // stale cache it happened to consume earlier. A genuine
                // miss — `Ok(None)`, NOT `NotReady`.
                None => return ReadGate::Unassigned,
            }
        };
        if target == 0 {
            return ReadGate::Ready; // empty partition: nothing to catch up to
        }
        // A sentinel target means the real HWM is not yet known (the
        // assignment-time fetch failed); the partition is assigned but the
        // answer is unknown → retryable, never a false `Ok(None)`.
        if target == HWM_UNKNOWN {
            return ReadGate::NotReady;
        }
        let Ok(idx) = usize::try_from(mp) else {
            // Defensive: a metadata partition index that doesn't fit in
            // usize is nonsensical, but if it ever happens we must NOT
            // fail open into `Ready` (which would serve a possibly-stale
            // or false-miss answer). Treat it as still catching up.
            return ReadGate::NotReady;
        };
        let applied = self.applied.lock().expect("applied mutex poisoned");
        if idx < applied.len() && applied[idx] >= target - 1 {
            ReadGate::Ready
        } else {
            ReadGate::NotReady
        }
    }

    /// `true` when metadata partition `mp` is assigned and caught up to its
    /// assignment-time HWM. Used by tests to poll for catch-up.
    #[cfg(test)]
    fn metadata_partition_ready(&self, mp: i32) -> bool {
        matches!(self.metadata_partition_gate(mp), ReadGate::Ready)
    }

    /// The metadata partitions this manager is currently assigned (tracked
    /// for readiness). Sorted ascending.
    #[must_use]
    pub fn assigned_metadata_partitions(&self) -> Vec<i32> {
        let mut v: Vec<i32> = self
            .ready_targets
            .lock()
            .expect("ready_targets poisoned")
            .keys()
            .copied()
            .collect();
        v.sort_unstable();
        v
    }

    /// Diff `desired` against the current assignment and drive the
    /// [`AssignmentHandle`]: add newly-needed partitions (seeded from the
    /// snapshot committed offset + 1, falling back to 0 when there is
    /// no committed event) and remove ones no longer needed. Records each
    /// added partition's assignment-time HWM so reads gate on `NotReady`
    /// until the pump catches up.
    ///
    /// HWM-fetch failure fails CLOSED: a partition whose real high-water
    /// mark could not be obtained is recorded with the `HWM_UNKNOWN`
    /// sentinel target so the gate returns `NotReady` (retryable), never a
    /// false `Ok(None)`. Such partitions are re-attempted on every
    /// subsequent reconcile (which the broker drives on each image change /
    /// reconciler tick), so a transient `high_water_marks` failure
    /// self-heals: the sentinel is replaced with the real target as soon as
    /// the fetch succeeds.
    ///
    /// MUST be driven by a SINGLE task. This method is not internally
    /// serialized — it interleaves `.await` points with reads/writes of the
    /// `ready_targets` map under short, non-overlapping locks — so two
    /// concurrent callers could race the add/remove/refresh logic.
    /// Correctness relies on the broker invoking it from exactly one
    /// reconciler task.
    ///
    /// Async because it reads the log's high-water marks; the broker calls
    /// it from its reconciler task (on the runtime), never from a
    /// `spawn_blocking` thread.
    pub async fn reconcile_assignment(&self, desired: &[i32]) {
        use std::collections::HashSet;
        let want: HashSet<i32> = desired.iter().copied().collect();
        // Snapshot the current per-partition targets so we can both diff the
        // assigned set and find partitions still carrying the HWM-unknown
        // sentinel (which need a refresh). Lock released before the `.await`.
        let current: std::collections::HashMap<i32, i64> = self
            .ready_targets
            .lock()
            .expect("ready_targets poisoned")
            .clone();
        let have: HashSet<i32> = current.keys().copied().collect();

        let needs_add = want.difference(&have).copied().collect::<Vec<_>>();
        // Partitions still assigned (in want) whose recorded target is the
        // sentinel: their HWM is still unknown, so re-attempt the fetch.
        let needs_refresh = want
            .iter()
            .copied()
            .filter(|mp| current.get(mp) == Some(&HWM_UNKNOWN))
            .collect::<Vec<_>>();

        // One HWM snapshot covers both additions and sentinel refreshes.
        let needs_hwm = !needs_add.is_empty() || !needs_refresh.is_empty();
        let hwms = if needs_hwm {
            match self.log.high_water_marks().await {
                Ok(h) => Some(h),
                Err(e) => {
                    warn!(error = ?e, "topic-based RLMM: high_water_marks fetch failed; \
                          assigned partitions gate NotReady until a later reconcile refreshes");
                    None
                }
            }
        } else {
            None
        };

        // Resolve a partition's target HWM from the (maybe-missing) snapshot.
        // A failed fetch (`None`) or a missing per-partition entry both yield
        // the sentinel so the gate stays NotReady — never fail open to 0.
        let target_for = |mp: i32| -> i64 {
            match &hwms {
                Some(h) => usize::try_from(mp)
                    .ok()
                    .and_then(|i| h.get(i).copied())
                    .unwrap_or(HWM_UNKNOWN),
                None => HWM_UNKNOWN,
            }
        };

        for mp in needs_add {
            // `committed_offset` is `-1` when there is no committed event
            // (full replay), so `+ 1` lands on the resume start offset (0).
            let start_offset = self.committed_offset(mp) + 1;
            self.assignment.add(PartitionStart {
                partition: mp,
                start_offset,
            });
            // Assign-but-NotReady when the HWM is unknown: the broker DOES
            // own this partition, so leaving it Unassigned would wrongly
            // return Ok(None). The sentinel makes the gate return NotReady.
            self.ready_targets
                .lock()
                .expect("ready_targets poisoned")
                .insert(mp, target_for(mp));
        }
        // Replace the sentinel for already-assigned partitions whose HWM is
        // now known (the partition stays assigned; only its target changes).
        for mp in needs_refresh {
            let target = target_for(mp);
            if target != HWM_UNKNOWN {
                let mut guard = self.ready_targets.lock().expect("ready_targets poisoned");
                // Only refresh if still assigned with the sentinel (a
                // concurrent remove would have dropped it — see the
                // single-task contract above).
                if guard.get(&mp) == Some(&HWM_UNKNOWN) {
                    guard.insert(mp, target);
                }
            }
        }
        for mp in have.difference(&want).copied() {
            self.assignment.remove(mp);
            self.ready_targets
                .lock()
                .expect("ready_targets poisoned")
                .remove(&mp);
        }
    }

    async fn wait_for_offset(&self, partition: i32, offset: i64) {
        let idx = usize::try_from(partition).expect("partition non-negative");
        let mut rx = self.applied_tx.subscribe();
        loop {
            {
                let applied = self.applied.lock().expect("applied mutex poisoned");
                if applied[idx] >= offset {
                    return;
                }
            }
            if rx.changed().await.is_err() {
                return;
            }
        }
    }

    fn publish_and_wait(
        &self,
        tp: &TopicIdPartition,
        event: Bytes,
    ) -> Result<(), RemoteStorageError> {
        let partition = metadata_partition_for(tp, self.log.partition_count());
        let log = self.log.clone();
        // Caller is on a non-runtime (spawn_blocking) thread; block_on
        // is safe and gives us the assigned offset to wait on.
        self.runtime.block_on(async {
            let offset = log
                .publish(partition, event)
                .await
                .map_err(MetadataLogError::into_storage)?;
            self.wait_for_offset(partition, offset).await;
            Ok::<_, RemoteStorageError>(())
        })
    }
}

impl Drop for TopicBasedRemoteLogMetadataManager {
    fn drop(&mut self) {
        self.shutdown.cancel();
        if let Some(handle) = self.pump.lock().expect("pump mutex poisoned").take() {
            handle.abort();
        }
        if let Some(handle) = self
            .snapshotter
            .lock()
            .expect("snapshotter mutex poisoned")
            .take()
        {
            handle.abort();
        }
    }
}

impl RemoteLogMetadataManager for TopicBasedRemoteLogMetadataManager {
    fn add_remote_log_segment_metadata(
        &self,
        metadata: RemoteLogSegmentMetadata,
    ) -> Result<(), RemoteStorageError> {
        // Mirror the in-memory manager's eager precondition: fail
        // fast before paying a round trip through Kafka.
        if metadata.state() != RemoteLogSegmentState::CopySegmentStarted {
            return Err(RemoteStorageError::InvalidAdd {
                id: metadata.remote_log_segment_id().clone(),
                reason: format!(
                    "starting state must be CopySegmentStarted, got {:?}",
                    metadata.state()
                ),
            });
        }
        let tp = metadata.remote_log_segment_id().topic_id_partition.clone();
        let event = MetadataEvent::AddSegment(metadata).encode();
        self.publish_and_wait(&tp, event)
    }

    fn update_remote_log_segment_metadata(
        &self,
        update: RemoteLogSegmentMetadataUpdate,
    ) -> Result<(), RemoteStorageError> {
        let tp = update.remote_log_segment_id.topic_id_partition.clone();
        let event = MetadataEvent::UpdateSegment(update).encode();
        self.publish_and_wait(&tp, event)
    }

    fn remote_log_segment_metadata(
        &self,
        topic_id_partition: &TopicIdPartition,
        leader_epoch: i32,
        offset: i64,
    ) -> Result<Option<RemoteLogSegmentMetadata>, RemoteStorageError> {
        let mp = metadata_partition_for(topic_id_partition, self.log.partition_count());
        match self.metadata_partition_gate(mp) {
            // Not this broker's partition → genuine miss, do NOT serve any
            // stale cache.
            ReadGate::Unassigned => Ok(None),
            // Assigned but not caught up → retryable, distinct from a miss.
            ReadGate::NotReady => Err(RemoteStorageError::NotReady { partition: mp }),
            ReadGate::Ready => {
                self.inner
                    .remote_log_segment_metadata(topic_id_partition, leader_epoch, offset)
            }
        }
    }

    fn highest_offset_for_epoch(
        &self,
        topic_id_partition: &TopicIdPartition,
        leader_epoch: i32,
    ) -> Result<Option<i64>, RemoteStorageError> {
        let mp = metadata_partition_for(topic_id_partition, self.log.partition_count());
        match self.metadata_partition_gate(mp) {
            ReadGate::Unassigned => Ok(None),
            ReadGate::NotReady => Err(RemoteStorageError::NotReady { partition: mp }),
            ReadGate::Ready => self
                .inner
                .highest_offset_for_epoch(topic_id_partition, leader_epoch),
        }
    }

    fn list_remote_log_segments(
        &self,
        topic_id_partition: &TopicIdPartition,
    ) -> Result<Vec<RemoteLogSegmentMetadata>, RemoteStorageError> {
        let mp = metadata_partition_for(topic_id_partition, self.log.partition_count());
        match self.metadata_partition_gate(mp) {
            // Not this broker's partition → it does not own it, so it must
            // not serve any stale segments it happened to consume earlier.
            ReadGate::Unassigned => Ok(Vec::new()),
            ReadGate::NotReady => Err(RemoteStorageError::NotReady { partition: mp }),
            ReadGate::Ready => self.inner.list_remote_log_segments(topic_id_partition),
        }
    }

    fn list_remote_log_segments_by_epoch(
        &self,
        topic_id_partition: &TopicIdPartition,
        leader_epoch: i32,
    ) -> Result<Vec<RemoteLogSegmentMetadata>, RemoteStorageError> {
        self.inner
            .list_remote_log_segments_by_epoch(topic_id_partition, leader_epoch)
    }

    fn put_remote_partition_delete_metadata(
        &self,
        metadata: RemotePartitionDeleteMetadata,
    ) -> Result<(), RemoteStorageError> {
        let tp = metadata.topic_id_partition.clone();
        let event = MetadataEvent::PartitionDelete(metadata).encode();
        self.publish_and_wait(&tp, event)
    }
}

async fn pump_loop(
    mut stream: MetadataEventStream,
    inner: Arc<InmemoryRemoteLogMetadataManager>,
    applied: Arc<std::sync::Mutex<Vec<i64>>>,
    applied_tx: watch::Sender<u64>,
    shutdown: CancellationToken,
) {
    let mut version: u64 = 0;
    loop {
        let next = tokio::select! {
            biased;
            () = shutdown.cancelled() => return,
            n = stream.next() => n,
        };
        let Some(record) = next else { return };
        match MetadataEvent::decode(&record.payload) {
            Ok(MetadataEvent::AddSegment(md)) => {
                if let Err(e) = inner.add_remote_log_segment_metadata(md) {
                    warn!(error = ?e, partition = record.partition, offset = record.offset,
                          "topic-based RLMM: add replay rejected");
                }
            }
            Ok(MetadataEvent::UpdateSegment(u)) => {
                if let Err(e) = inner.update_remote_log_segment_metadata(u) {
                    warn!(error = ?e, partition = record.partition, offset = record.offset,
                          "topic-based RLMM: update replay rejected");
                }
            }
            Ok(MetadataEvent::PartitionDelete(d)) => {
                if let Err(e) = inner.put_remote_partition_delete_metadata(d) {
                    warn!(error = ?e, partition = record.partition, offset = record.offset,
                          "topic-based RLMM: partition-delete replay rejected");
                }
            }
            Err(e) => {
                warn!(error = ?e, partition = record.partition, offset = record.offset,
                      "topic-based RLMM: failed to decode event");
            }
        }
        if let Ok(idx) = usize::try_from(record.partition) {
            let mut a = applied.lock().expect("applied mutex poisoned");
            if idx < a.len() && record.offset > a[idx] {
                a[idx] = record.offset;
            }
        }
        version = version.wrapping_add(1);
        let _ = applied_tx.send(version);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use assert2::assert;
    use std::collections::BTreeMap;
    use uuid::Uuid;

    use crabka_remote_storage::{CustomMetadata, RemoteLogSegmentId, RemotePartitionDeleteState};

    use crate::error::MetadataLogError;
    use crate::log::{AssignmentHandle, InProcessMetadataEventLog, MetadataEventStream};

    /// Test double that delegates to an inner [`InProcessMetadataEventLog`]
    /// but can be told to fail `high_water_marks()` on demand. The
    /// in-process fixture's HWM RPC always succeeds, which is why the rest
    /// of the suite cannot exercise the C1 fail-closed path.
    struct HwmFlakyLog {
        inner: Arc<InProcessMetadataEventLog>,
        fail_hwm: std::sync::atomic::AtomicBool,
    }

    impl HwmFlakyLog {
        fn new(partition_count: i32) -> Arc<Self> {
            Arc::new(Self {
                inner: InProcessMetadataEventLog::new(partition_count),
                fail_hwm: std::sync::atomic::AtomicBool::new(false),
            })
        }
        fn set_fail_hwm(&self, fail: bool) {
            self.fail_hwm
                .store(fail, std::sync::atomic::Ordering::SeqCst);
        }
    }

    #[async_trait::async_trait]
    impl MetadataEventLog for HwmFlakyLog {
        fn partition_count(&self) -> i32 {
            self.inner.partition_count()
        }
        async fn publish(&self, partition: i32, event: Bytes) -> Result<i64, MetadataLogError> {
            self.inner.publish(partition, event).await
        }
        fn subscribe(
            &self,
            assignment: Vec<PartitionStart>,
        ) -> (MetadataEventStream, Arc<dyn AssignmentHandle>) {
            self.inner.subscribe(assignment)
        }
        async fn high_water_marks(&self) -> Result<Vec<i64>, MetadataLogError> {
            if self.fail_hwm.load(std::sync::atomic::Ordering::SeqCst) {
                return Err(MetadataLogError::Other("injected HWM failure".into()));
            }
            self.inner.high_water_marks().await
        }
    }

    static SNAP_COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

    fn snapshot_test_dir(label: &str) -> std::path::PathBuf {
        std::env::temp_dir().join(format!(
            "crabka-rlmm-{label}-{}-{}",
            std::process::id(),
            SNAP_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
        ))
    }

    fn tp() -> TopicIdPartition {
        TopicIdPartition::new(Uuid::from_u128(1), "orders", 0)
    }

    fn started(id: u128, start: i64, end: i64) -> RemoteLogSegmentMetadata {
        RemoteLogSegmentMetadata::new(
            RemoteLogSegmentId::new(tp(), Uuid::from_u128(id)),
            start,
            end,
            end + 1,
            1,
            100,
            2048,
            RemoteLogSegmentState::CopySegmentStarted,
            BTreeMap::from([(0, start)]),
        )
        .unwrap()
    }

    fn finish(id: u128) -> RemoteLogSegmentMetadataUpdate {
        RemoteLogSegmentMetadataUpdate {
            remote_log_segment_id: RemoteLogSegmentId::new(tp(), Uuid::from_u128(id)),
            event_timestamp_ms: 200,
            custom_metadata: Some(CustomMetadata(vec![7])),
            state: RemoteLogSegmentState::CopySegmentFinished,
            broker_id: 1,
        }
    }

    /// Run the sync RLMM trait method on the blocking pool, exactly
    /// like the broker does.
    async fn on_blocking<T, F>(f: F) -> T
    where
        F: FnOnce() -> T + Send + 'static,
        T: Send + 'static,
    {
        tokio::task::spawn_blocking(f).await.unwrap()
    }

    /// Poll until `tp` reads `Ok(Some)` (assigned + caught up), or panic.
    async fn wait_ready(m: &Arc<TopicBasedRemoteLogMetadataManager>, tp: &TopicIdPartition) {
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);
        loop {
            if matches!(m.remote_log_segment_metadata(tp, 0, 42), Ok(Some(_))) {
                return;
            }
            assert!(
                std::time::Instant::now() < deadline,
                "partition never became ready"
            );
            tokio::time::sleep(std::time::Duration::from_millis(5)).await;
        }
    }

    /// Start a manager that consumes NOTHING until the caller drives
    /// `reconcile_assignment`. Used by the assignment/readiness tests, which
    /// assert pre-assignment reads are a genuine miss.
    async fn start_manager(
        log: Arc<dyn MetadataEventLog>,
    ) -> Arc<TopicBasedRemoteLogMetadataManager> {
        TopicBasedRemoteLogMetadataManager::start(
            log,
            Handle::current(),
            snapshot_test_dir("test"),
            std::time::Duration::from_hours(1),
        )
        .await
        .unwrap()
    }

    /// Start a manager and assign EVERY metadata partition (the eager
    /// "consume all" behavior). Used by tests that publish through the
    /// manager and read the result back, and by the multi-broker pre-seed
    /// writers. Blocks until each non-empty partition has caught up to its
    /// assignment-time HWM so a subsequent read does not race the pump.
    async fn start_manager_all(
        log: Arc<dyn MetadataEventLog>,
    ) -> Arc<TopicBasedRemoteLogMetadataManager> {
        let n = log.partition_count();
        let m = start_manager(log).await;
        let all: Vec<i32> = (0..n).collect();
        m.reconcile_assignment(&all).await;
        // Wait for the pump to catch up to every assigned partition's HWM so
        // the manager is "ready" for all partitions, mirroring the old
        // bootstrap contract.
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
        while !all.iter().all(|&mp| m.metadata_partition_ready(mp)) {
            assert!(
                std::time::Instant::now() < deadline,
                "manager did not catch up on all partitions within 5s"
            );
            tokio::time::sleep(std::time::Duration::from_millis(2)).await;
        }
        m
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn add_finish_query_round_trip() {
        let log: Arc<dyn MetadataEventLog> = InProcessMetadataEventLog::new(4);
        let m = start_manager_all(log).await;
        let m2 = m.clone();
        on_blocking(move || {
            m2.add_remote_log_segment_metadata(started(10, 0, 99))
                .unwrap();
        })
        .await;
        let m2 = m.clone();
        on_blocking(move || m2.update_remote_log_segment_metadata(finish(10)).unwrap()).await;

        let got = m
            .remote_log_segment_metadata(&tp(), 0, 42)
            .unwrap()
            .expect("segment found");
        assert!(got.remote_log_segment_id().id == Uuid::from_u128(10));
        assert!(got.custom_metadata() == Some(&CustomMetadata(vec![7])));
        assert!(m.highest_offset_for_epoch(&tp(), 0).unwrap() == Some(99));
        m.shutdown();
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn add_with_wrong_state_is_rejected_eagerly() {
        let log: Arc<dyn MetadataEventLog> = InProcessMetadataEventLog::new(2);
        let m = start_manager(log.clone()).await;
        // Force a non-Started state via the lifecycle helper.
        let bad = started(10, 0, 9).with_update(&finish(10)).unwrap();
        let m2 = m.clone();
        let err = on_blocking(move || m2.add_remote_log_segment_metadata(bad).unwrap_err()).await;
        assert!(matches!(err, RemoteStorageError::InvalidAdd { .. }));
        // Eager rejection means nothing was published.
        assert!(log.high_water_marks().await.unwrap() == vec![0; 2]);
        m.shutdown();
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn two_managers_sharing_a_log_converge() {
        let log: Arc<dyn MetadataEventLog> = InProcessMetadataEventLog::new(4);
        let a = start_manager_all(log.clone()).await;
        let b = start_manager_all(log.clone()).await;

        let a2 = a.clone();
        on_blocking(move || {
            a2.add_remote_log_segment_metadata(started(10, 0, 99))
                .unwrap();
        })
        .await;
        let a2 = a.clone();
        on_blocking(move || a2.update_remote_log_segment_metadata(finish(10)).unwrap()).await;

        // `b` must observe `a`'s writes once its pump has applied
        // them. Poll up to 2s for the in-process broadcast to fan out.
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);
        while b.highest_offset_for_epoch(&tp(), 0).unwrap() != Some(99) {
            assert!(
                std::time::Instant::now() < deadline,
                "manager B did not converge within 2s"
            );
            tokio::time::sleep(std::time::Duration::from_millis(5)).await;
        }
        assert!(b.highest_offset_for_epoch(&tp(), 0).unwrap() == Some(99));
        let got = b
            .remote_log_segment_metadata(&tp(), 0, 50)
            .unwrap()
            .unwrap();
        assert!(got.remote_log_segment_id().id == Uuid::from_u128(10));

        a.shutdown();
        b.shutdown();
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn restart_rehydrates_from_log() {
        let log: Arc<dyn MetadataEventLog> = InProcessMetadataEventLog::new(4);
        {
            let m = start_manager_all(log.clone()).await;
            for (id, start, end) in [(10u128, 0, 99), (11, 100, 199), (12, 200, 299)] {
                let m2 = m.clone();
                on_blocking(move || {
                    m2.add_remote_log_segment_metadata(started(id, start, end))
                        .unwrap();
                })
                .await;
                let m2 = m.clone();
                on_blocking(move || m2.update_remote_log_segment_metadata(finish(id)).unwrap())
                    .await;
            }
            m.shutdown();
        }

        // Fresh manager against the same log: assigning all partitions
        // replays the full history before the read below.
        let fresh = start_manager_all(log).await;
        let listed = fresh.list_remote_log_segments(&tp()).unwrap();
        assert!(listed.len() == 3);
        assert!(listed[0].start_offset() == 0);
        assert!(listed[2].end_offset() == 299);
        assert!(fresh.highest_offset_for_epoch(&tp(), 0).unwrap() == Some(299));
        fresh.shutdown();
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn partition_delete_lifecycle_round_trip() {
        let log: Arc<dyn MetadataEventLog> = InProcessMetadataEventLog::new(2);
        let m = start_manager_all(log).await;
        for state in [
            RemotePartitionDeleteState::DeletePartitionMarked,
            RemotePartitionDeleteState::DeletePartitionStarted,
            RemotePartitionDeleteState::DeletePartitionFinished,
        ] {
            let m2 = m.clone();
            on_blocking(move || {
                m2.put_remote_partition_delete_metadata(RemotePartitionDeleteMetadata {
                    topic_id_partition: tp(),
                    state,
                    event_timestamp_ms: 500,
                    broker_id: 1,
                })
                .unwrap();
            })
            .await;
        }
        m.shutdown();
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn shutdown_flushes_a_snapshot_covering_applied_events() {
        let dir = snapshot_test_dir("mgr-snap");
        let log: Arc<dyn MetadataEventLog> = InProcessMetadataEventLog::new(4);
        let m = TopicBasedRemoteLogMetadataManager::start(
            log.clone(),
            Handle::current(),
            dir.clone(),
            std::time::Duration::from_hours(1), // long interval: only shutdown flushes
        )
        .await
        .unwrap();
        m.reconcile_assignment(&(0..log.partition_count()).collect::<Vec<_>>())
            .await;
        let m2 = m.clone();
        on_blocking(move || {
            m2.add_remote_log_segment_metadata(started(10, 0, 99))
                .unwrap();
        })
        .await;
        let m2 = m.clone();
        on_blocking(move || m2.update_remote_log_segment_metadata(finish(10)).unwrap()).await;

        m.shutdown_and_flush().await;

        let path = dir.join(crate::snapshot::SNAPSHOT_FILE_NAME);
        let snap = crate::snapshot::Snapshot::load(&path)
            .unwrap()
            .expect("snapshot written");
        // The orders partition's committed offset covers both events.
        let p = crate::partitioning::metadata_partition_for(&tp(), 4);
        let idx = usize::try_from(p).unwrap();
        assert!(
            snap.committed_offsets[idx] >= 1,
            "committed >= last applied offset"
        );
        // The dump contains the finished segment.
        assert!(snap.dump.partitions.len() == 1);
        assert!(snap.dump.partitions[0].segments.len() == 1);
        std::fs::remove_dir_all(&dir).ok();
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn restart_resumes_from_snapshot_without_replaying_from_zero() {
        let dir = snapshot_test_dir("resume");
        let log: Arc<dyn MetadataEventLog> = InProcessMetadataEventLog::new(4);
        let interval = std::time::Duration::from_hours(1);

        // First lifetime: seed three finished segments, then shutdown-flush.
        let pre_cache;
        {
            let m = TopicBasedRemoteLogMetadataManager::start(
                log.clone(),
                Handle::current(),
                dir.clone(),
                interval,
            )
            .await
            .unwrap();
            m.reconcile_assignment(&(0..log.partition_count()).collect::<Vec<_>>())
                .await;
            for (id, start, end) in [(10u128, 0, 99), (11, 100, 199), (12, 200, 299)] {
                let m2 = m.clone();
                on_blocking(move || {
                    m2.add_remote_log_segment_metadata(started(id, start, end))
                        .unwrap();
                })
                .await;
                let m2 = m.clone();
                on_blocking(move || m2.update_remote_log_segment_metadata(finish(id)).unwrap())
                    .await;
            }
            pre_cache = m.list_remote_log_segments(&tp()).unwrap();
            m.shutdown_and_flush().await;
        }

        // Snapshot now records committed offset N for the orders partition.
        let p = crate::partitioning::metadata_partition_for(&tp(), 4);
        let idx = usize::try_from(p).unwrap();
        let snap = crate::snapshot::Snapshot::load(&dir.join(crate::snapshot::SNAPSHOT_FILE_NAME))
            .unwrap()
            .expect("snapshot present");
        let committed = snap.committed_offsets[idx];
        assert!(
            committed >= 5,
            "6 events (3 add + 3 finish) → committed >= 5"
        );

        // The canonical resume computation resumes the orders partition at
        // committed + 1 (same path start() uses).
        let (resumed_committed, assignment) =
            TopicBasedRemoteLogMetadataManager::resume_from_snapshot(Some(&snap), 4);
        let orders_start = assignment
            .iter()
            .find(|s| s.partition == p)
            .map(|s| s.start_offset)
            .unwrap();
        assert!(orders_start == committed + 1, "resume from N+1, not 0");
        assert!(resumed_committed[idx] == committed);

        // Second lifetime against the SAME log + dir: must resume, not replay.
        let fresh = TopicBasedRemoteLogMetadataManager::start(
            log.clone(),
            Handle::current(),
            dir.clone(),
            interval,
        )
        .await
        .unwrap();
        // The manager exposes the same committed offset via its canonical
        // accessor used by the assignment reconciler.
        assert!(fresh.committed_offset(p) == committed);
        // Assign every partition and wait for catch-up so the gated read
        // methods delegate to the (snapshot-seeded) inner cache. The orders
        // partition has no backlog past `committed`, so it is ready as soon
        // as the assignment-time HWM is recorded.
        fresh
            .reconcile_assignment(&(0..log.partition_count()).collect::<Vec<_>>())
            .await;
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
        while !fresh.metadata_partition_ready(p) {
            assert!(
                std::time::Instant::now() < deadline,
                "fresh manager did not catch up on the orders partition"
            );
            tokio::time::sleep(std::time::Duration::from_millis(2)).await;
        }
        let post_cache = fresh.list_remote_log_segments(&tp()).unwrap();
        assert!(
            post_cache == pre_cache,
            "post-load cache equals pre-restart cache"
        );
        assert!(fresh.highest_offset_for_epoch(&tp(), 0).unwrap() == Some(299));
        fresh.shutdown();
        std::fs::remove_dir_all(&dir).ok();
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn add_then_remove_drives_assignment_and_readiness() {
        use crate::partitioning::metadata_partition_for;

        let log: Arc<dyn MetadataEventLog> = InProcessMetadataEventLog::new(4);
        // Pre-seed a finished segment for `tp()` so a ready read returns Some.
        {
            let writer = start_manager_all(log.clone()).await;
            let w2 = writer.clone();
            on_blocking(move || {
                w2.add_remote_log_segment_metadata(started(10, 0, 99))
                    .unwrap();
            })
            .await;
            let w2 = writer.clone();
            on_blocking(move || w2.update_remote_log_segment_metadata(finish(10)).unwrap()).await;
            writer.shutdown();
        }

        let mp = metadata_partition_for(&tp(), log.partition_count());
        let m = start_manager(log).await;

        // Before assignment: the partition is not consumed → genuine miss.
        assert!(matches!(
            m.remote_log_segment_metadata(&tp(), 0, 42),
            Ok(None)
        ));

        // Assign it. add() must enqueue a PartitionStart for `mp`, and the
        // pump catches up; once applied >= HWM-1 the read returns Some.
        m.reconcile_assignment(&[mp]).await;
        assert!(m.assigned_metadata_partitions() == vec![mp]);

        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);
        loop {
            match m.remote_log_segment_metadata(&tp(), 0, 42) {
                Ok(Some(md)) => {
                    assert!(md.remote_log_segment_id().id == Uuid::from_u128(10));
                    break;
                }
                Err(RemoteStorageError::NotReady { partition }) => {
                    assert!(partition == mp, "NotReady names the catching-up partition");
                    assert!(
                        std::time::Instant::now() < deadline,
                        "metadata partition never became ready"
                    );
                    tokio::time::sleep(std::time::Duration::from_millis(5)).await;
                }
                other => panic!("unexpected read outcome: {other:?}"),
            }
        }

        // Remove it: assignment drops, and subsequent reads are a genuine
        // miss (Ok(None)) — the partition is no longer consumed.
        m.reconcile_assignment(&[]).await;
        assert!(m.assigned_metadata_partitions().is_empty());
        assert!(matches!(
            m.remote_log_segment_metadata(&tp(), 0, 42),
            Ok(None)
        ));
        m.shutdown();
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn unknown_partition_query_is_none() {
        let log: Arc<dyn MetadataEventLog> = InProcessMetadataEventLog::new(2);
        let m = start_manager(log).await;
        let other = TopicIdPartition::new(Uuid::from_u128(999), "nope", 0);
        assert!(m.remote_log_segment_metadata(&other, 0, 0).unwrap() == None);
        assert!(m.highest_offset_for_epoch(&other, 0).unwrap() == None);
        assert!(m.list_remote_log_segments(&other).unwrap().is_empty());
        m.shutdown();
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn two_brokers_split_metadata_partitions() {
        use crate::partitioning::metadata_partition_for;

        // Use a wide metadata topic so two user-partitions land in distinct
        // buckets.
        let n = 16;
        let topic_id = Uuid::from_u128(0xFEED);
        let tp_a = TopicIdPartition::new(topic_id, "orders", 0);
        let tp_b = TopicIdPartition::new(topic_id, "orders", 1);
        let mp_a = metadata_partition_for(&tp_a, n);
        let mp_b = metadata_partition_for(&tp_b, n);
        assert!(
            mp_a != mp_b,
            "test needs the two partitions in distinct buckets"
        );

        let log: Arc<dyn MetadataEventLog> = InProcessMetadataEventLog::new(n);

        // Seed one finished segment for each user-partition via a transient
        // writer (consumes all partitions, no assignment gating).
        for (tp, id) in [(tp_a.clone(), 100u128), (tp_b.clone(), 200)] {
            let w = start_manager_all(log.clone()).await;
            let started = RemoteLogSegmentMetadata::new(
                RemoteLogSegmentId::new(tp.clone(), Uuid::from_u128(id)),
                0,
                99,
                100,
                1,
                100,
                2048,
                RemoteLogSegmentState::CopySegmentStarted,
                BTreeMap::from([(0, 0)]),
            )
            .unwrap();
            let w2 = w.clone();
            on_blocking(move || w2.add_remote_log_segment_metadata(started).unwrap()).await;
            let upd = RemoteLogSegmentMetadataUpdate {
                remote_log_segment_id: RemoteLogSegmentId::new(tp, Uuid::from_u128(id)),
                event_timestamp_ms: 200,
                custom_metadata: None,
                state: RemoteLogSegmentState::CopySegmentFinished,
                broker_id: 1,
            };
            let w2 = w.clone();
            on_blocking(move || w2.update_remote_log_segment_metadata(upd).unwrap()).await;
            w.shutdown();
        }

        // Broker A consumes mp_a only; Broker B consumes mp_b only.
        let a = start_manager(log.clone()).await;
        let b = start_manager(log).await;
        a.reconcile_assignment(&[mp_a]).await;
        b.reconcile_assignment(&[mp_b]).await;

        assert!(a.assigned_metadata_partitions() == vec![mp_a]);
        assert!(b.assigned_metadata_partitions() == vec![mp_b]);
        // Disjoint shares.
        assert!(
            a.assigned_metadata_partitions()
                .iter()
                .all(|p| !b.assigned_metadata_partitions().contains(p)),
            "shares must be disjoint"
        );

        // Poll until each is caught up and serves its own partition.
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);
        loop {
            let a_own = a.remote_log_segment_metadata(&tp_a, 0, 42);
            let b_own = b.remote_log_segment_metadata(&tp_b, 0, 42);
            if matches!(a_own, Ok(Some(_))) && matches!(b_own, Ok(Some(_))) {
                break;
            }
            assert!(
                std::time::Instant::now() < deadline,
                "managers did not catch up: a={a_own:?} b={b_own:?}"
            );
            tokio::time::sleep(std::time::Duration::from_millis(5)).await;
        }

        // Cross reads (partition the broker does NOT consume) are a genuine
        // miss, not NotReady.
        assert!(
            matches!(a.remote_log_segment_metadata(&tp_b, 0, 42), Ok(None)),
            "A does not consume mp_b → genuine miss"
        );
        assert!(
            matches!(b.remote_log_segment_metadata(&tp_a, 0, 42), Ok(None)),
            "B does not consume mp_a → genuine miss"
        );

        a.shutdown();
        b.shutdown();
    }

    /// Runtime `remove` then `add` reassignment must not
    /// double-deliver a metadata partition's events into the cache. A
    /// re-applied `AddSegment` is harmlessly rejected by the lifecycle state
    /// machine, so the segment list stays at exactly one entry — proving no
    /// duplicate corruption after remove + re-add.
    #[tokio::test(flavor = "multi_thread")]
    async fn reassignment_remove_then_readd_applies_no_duplicates() {
        use crate::partitioning::metadata_partition_for;

        let log: Arc<dyn MetadataEventLog> = InProcessMetadataEventLog::new(4);
        // Pre-seed a single finished segment for `tp()`.
        {
            let writer = start_manager_all(log.clone()).await;
            let w2 = writer.clone();
            on_blocking(move || {
                w2.add_remote_log_segment_metadata(started(10, 0, 99))
                    .unwrap();
            })
            .await;
            let w2 = writer.clone();
            on_blocking(move || w2.update_remote_log_segment_metadata(finish(10)).unwrap()).await;
            writer.shutdown();
        }

        let mp = metadata_partition_for(&tp(), log.partition_count());
        let m = start_manager(log).await;

        // Add → catch up → exactly one segment.
        m.reconcile_assignment(&[mp]).await;
        wait_ready(&m, &tp()).await;
        assert!(
            m.list_remote_log_segments(&tp()).unwrap().len() == 1,
            "one segment after first assignment"
        );

        // Remove (drops the live fetch task mid-flight if one is running) …
        m.reconcile_assignment(&[]).await;
        assert!(m.assigned_metadata_partitions().is_empty());

        // … then re-add. The pump re-injects the backlog from the resume
        // offset; the re-applied AddSegment is rejected by the lifecycle
        // machine, so NO duplicate lands in the cache.
        m.reconcile_assignment(&[mp]).await;
        wait_ready(&m, &tp()).await;

        let listed = m.list_remote_log_segments(&tp()).unwrap();
        assert!(
            listed.len() == 1,
            "remove + re-add must not duplicate the segment, got {listed:?}"
        );
        assert!(listed[0].remote_log_segment_id().id == Uuid::from_u128(10));
        // The finished state survived (no half-applied duplicate update).
        assert!(m.highest_offset_for_epoch(&tp(), 0).unwrap() == Some(99));

        m.shutdown();
    }

    /// C1: a HWM-fetch failure must fail CLOSED. When `high_water_marks`
    /// errors at assignment time, the newly-added partition must gate
    /// `NotReady` (retryable) — NEVER `Ok(None)` (a false end-of-tier) —
    /// and the sentinel must self-heal on a later reconcile once the HWM
    /// fetch succeeds.
    #[tokio::test(flavor = "multi_thread")]
    async fn hwm_fetch_failure_gates_not_ready_then_self_heals() {
        use crate::partitioning::metadata_partition_for;

        let flaky = HwmFlakyLog::new(4);
        let log: Arc<dyn MetadataEventLog> = flaky.clone();

        // Pre-seed a finished segment for `tp()` via a healthy writer (HWM
        // not failing yet), so a ready read would return Some.
        {
            let writer = start_manager_all(log.clone()).await;
            let w2 = writer.clone();
            on_blocking(move || {
                w2.add_remote_log_segment_metadata(started(10, 0, 99))
                    .unwrap();
            })
            .await;
            let w2 = writer.clone();
            on_blocking(move || w2.update_remote_log_segment_metadata(finish(10)).unwrap()).await;
            writer.shutdown();
        }

        let mp = metadata_partition_for(&tp(), log.partition_count());
        let m = start_manager(log).await;

        // Assign the partition WHILE the HWM RPC is failing. The partition
        // must be added (the broker owns it) but recorded with the sentinel
        // target so the gate returns NotReady, not Ok(None).
        flaky.set_fail_hwm(true);
        m.reconcile_assignment(&[mp]).await;
        assert!(
            m.assigned_metadata_partitions() == vec![mp],
            "partition is assigned even though HWM is unknown (broker owns it)"
        );

        // Give the pump ample time to drain the backlog. Even fully caught
        // up, the read must stay NotReady because the real HWM is unknown —
        // it must NEVER collapse to Ok(None).
        let deadline = std::time::Instant::now() + std::time::Duration::from_millis(300);
        while std::time::Instant::now() < deadline {
            match m.remote_log_segment_metadata(&tp(), 0, 42) {
                Err(RemoteStorageError::NotReady { partition }) => assert!(partition == mp),
                other => panic!("HWM-unknown partition must read NotReady, got {other:?}"),
            }
            // The list path is gated the same way.
            match m.list_remote_log_segments(&tp()) {
                Err(RemoteStorageError::NotReady { partition }) => assert!(partition == mp),
                other => panic!("HWM-unknown partition list must be NotReady, got {other:?}"),
            }
            tokio::time::sleep(std::time::Duration::from_millis(10)).await;
        }

        // Recover: HWM fetch now succeeds. A subsequent reconcile (which the
        // broker drives on each image change / tick) must replace the
        // sentinel with the real target. Once the pump has caught up the read
        // returns Some.
        flaky.set_fail_hwm(false);
        m.reconcile_assignment(&[mp]).await;
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);
        loop {
            match m.remote_log_segment_metadata(&tp(), 0, 42) {
                Ok(Some(md)) => {
                    assert!(md.remote_log_segment_id().id == Uuid::from_u128(10));
                    break;
                }
                Err(RemoteStorageError::NotReady { partition }) => {
                    assert!(partition == mp);
                    assert!(
                        std::time::Instant::now() < deadline,
                        "partition never became ready after HWM recovered"
                    );
                    tokio::time::sleep(std::time::Duration::from_millis(5)).await;
                }
                other => panic!("unexpected read outcome after recovery: {other:?}"),
            }
        }
        // The list path is now Ready too.
        assert!(m.list_remote_log_segments(&tp()).unwrap().len() == 1);
        assert!(m.highest_offset_for_epoch(&tp(), 0).unwrap() == Some(99));

        m.shutdown();
    }
}