meterstore 0.12.0

Hot/cold tiered store for metering time series — PostgreSQL for the recent window, Apache Iceberg for history.
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
//! Scheduled upkeep: archival, snapshot expiry, the retention sweep, and the
//! invariant check.
//!
//! Four jobs with different cadences and very different consequences, which is
//! why they are one type with one entry point rather than four independent
//! timers:
//!
//! - **Archival** must run often enough that the hot tier stays bounded, and its
//!   lag is the thing to alert on.
//! - **Snapshot expiry** is a *compliance* decision, not cleanup: a snapshot is
//!   what makes a past settlement reproducible, so a run that expires too
//!   eagerly destroys the audit position the cold tier exists to hold. It is
//!   therefore opt-in and runs rarely.
//! - **The retention sweep** ([`Maintenance::anonymise_after`]) is the other
//!   compliance decision, and the only job here that is irreversible. Also
//!   opt-in, and it runs across every table at once because the subject registry
//!   is deployment-wide.
//! - **The invariant check** is the alert. It is cheap and it is the only one
//!   whose failure means query results may already be wrong.
//!
//! # Nothing here holds state
//!
//! Every job is idempotent and recovers from an interruption on its own,
//! so the scheduler is a loop and a clock. It keeps no checkpoint, because the
//! checkpoint is the Iceberg snapshot summary. Restarting the process loses
//! nothing.
//!
//! # The clock is injected
//!
//! `now` is a parameter, so a test can drive months of archival in milliseconds
//! and a deployment with skewed clocks fails visibly rather than subtly. Tiering
//! itself never reads a clock — it routes on `from`, a data value — so the only
//! thing wall time decides is *when* a window becomes eligible.

use time::{Duration, OffsetDateTime};
use tracing::{info, warn};

use crate::error::Result;
use crate::tiering::ArchivalOutcome;

/// What one maintenance cycle did to **one** table.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct TableMaintenance {
    /// The table this row describes.
    pub table: String,
    /// Archival runs performed, in order.
    pub archival: Vec<ArchivalOutcome>,
    /// Cold snapshots expired, when expiry was enabled for this cycle.
    pub snapshots_expired: usize,
    /// Rows found in the wrong tier. **Non-zero means results may be wrong.**
    pub invariant_violations: u64,
    /// Why this table's cycle did not complete, if it did not.
    ///
    /// Rendered rather than typed because it is a *report*: the cycle has already
    /// moved on to the next table, and what is left to do with this is log it and
    /// alert on it. A caller wanting to act on the error programmatically runs
    /// that table's [`archive`](crate::MeterStore::archive) itself.
    pub failure: Option<String>,
}

impl TableMaintenance {
    /// Rows moved from hot to cold.
    pub fn rows_archived(&self) -> u64 {
        self.archival.iter().map(|o| o.rows).sum()
    }

    /// Windows archived.
    pub fn windows_archived(&self) -> usize {
        self.archival
            .iter()
            .filter(|o| o.archived_anything())
            .count()
    }

    /// Whether another process was doing this table's work.
    pub fn lease_contended(&self) -> bool {
        self.archival.iter().any(|o| o.lease_contended)
    }

    /// Whether a run stopped because a lock was not available.
    ///
    /// Not a failure and not counted as one: the statement declined to queue for
    /// a lock rather than blocking every reader and writer behind it, and nothing
    /// was changed. It is worth surfacing because a table that defers *every*
    /// cycle is a table whose watermark is not moving, and the fix is on the
    /// database — a long-running query, or a session idle in a transaction —
    /// rather than here.
    pub fn deferred(&self) -> bool {
        self.archival.iter().any(|o| o.deferred)
    }

    /// Whether this table's cycle completed and its tiers still partition its
    /// data as they should.
    pub fn healthy(&self) -> bool {
        self.invariant_violations == 0 && self.failure.is_none()
    }
}

/// What one maintenance cycle did.
///
/// **Per table**, because every table is its own unit. A cycle that only
/// summed would report a deployment healthy while one of its tables was
/// quarantined, and would give an operator no name to act on. The aggregate
/// accessors fold the rows rather than replacing them, because "did anything
/// move" is a real question too.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct MaintenanceOutcome {
    /// One entry per table the cycle ran over, in name order.
    pub tables: Vec<TableMaintenance>,
    /// Subjects whose linkage this cycle destroyed, when a retention policy is
    /// configured.
    ///
    /// **Not per table.** The subject registry is deployment-wide, so a subject
    /// is due only once every table that names it has passed the ceiling — see
    /// [`Maintenance::anonymise_after`].
    pub anonymised: Vec<crate::erasure::ErasureRecord>,
    /// Why the retention sweep did not run, if it was configured and failed.
    ///
    /// Rendered rather than typed, for the reason
    /// [`TableMaintenance::failure`] is: the cycle has already moved on, and
    /// what is left to do with it is log it and alert on it.
    pub retention_failure: Option<String>,
}

impl MaintenanceOutcome {
    /// Rows moved from hot to cold this cycle, across every table.
    pub fn rows_archived(&self) -> u64 {
        self.tables
            .iter()
            .map(TableMaintenance::rows_archived)
            .sum()
    }

    /// Windows archived this cycle, across every table.
    pub fn windows_archived(&self) -> usize {
        self.tables
            .iter()
            .map(TableMaintenance::windows_archived)
            .sum()
    }

    /// Cold snapshots expired this cycle, across every table.
    pub fn snapshots_expired(&self) -> usize {
        self.tables.iter().map(|t| t.snapshots_expired).sum()
    }

    /// Rows found in the wrong tier, across every table.
    ///
    /// **Non-zero means results may be wrong**, and
    /// [`unhealthy`](Self::unhealthy) names which table.
    pub fn invariant_violations(&self) -> u64 {
        self.tables.iter().map(|t| t.invariant_violations).sum()
    }

    /// Whether another process was doing the work for any table.
    pub fn lease_contended(&self) -> bool {
        self.tables.iter().any(TableMaintenance::lease_contended)
    }

    /// Whether any table's run stopped because a lock was not available.
    pub fn deferred(&self) -> bool {
        self.tables.iter().any(TableMaintenance::deferred)
    }

    /// Whether every table's tiers still partition their data as they should,
    /// and the retention sweep — if configured — ran.
    pub fn healthy(&self) -> bool {
        self.tables.iter().all(TableMaintenance::healthy) && self.retention_failure.is_none()
    }

    /// The tables that are not healthy — what an alert should name.
    pub fn unhealthy(&self) -> impl Iterator<Item = &TableMaintenance> {
        self.tables.iter().filter(|t| !t.healthy())
    }

    /// The tables whose cycle failed, with the reason.
    ///
    /// A failed **retention sweep** appears here too, under the name
    /// `<retention>`: it is not a table's failure, but it is a failure an
    /// operator has to see, and a caller iterating this to build an alert must
    /// not have to know about a second place to look.
    pub fn failures(&self) -> impl Iterator<Item = (&str, &str)> {
        self.tables
            .iter()
            .filter_map(|t| Some((t.table.as_str(), t.failure.as_deref()?)))
            .chain(
                self.retention_failure
                    .as_deref()
                    .map(|why| (RETENTION_LABEL, why)),
            )
    }

    /// Subjects anonymised this cycle.
    pub fn subjects_anonymised(&self) -> usize {
        self.anonymised.len()
    }
}

/// What a failed retention sweep is named in [`MaintenanceOutcome::failures`].
///
/// Angle-bracketed so it cannot collide with a table name: `TableConfig` refuses
/// anything that is not a plain identifier.
pub const RETENTION_LABEL: &str = "<retention>";

/// Upkeep for one store or a whole catalog, run on demand or on a schedule.
///
/// **One loop over N tables**, not N loops. Each table keeps its own watermark,
/// archiver and lease; only the *scheduling* is shared, which is what
/// gives a deployment one place to ask whether upkeep is keeping up.
#[derive(Debug, Clone)]
pub struct Maintenance {
    /// The tables this loop maintains, in the order they are visited.
    stores: Vec<crate::session::MeterStore>,
    interval: Duration,
    max_windows: usize,
    expire_snapshots: bool,
    retention: Option<RetentionSweep>,
}

/// A configured § 60 Abs. 6 sweep: the policy, and who to record as having run it.
#[derive(Debug, Clone)]
struct RetentionSweep {
    policy: crate::erasure::Retention,
    reason: String,
    actor: String,
}

impl Maintenance {
    /// Default cadence: often enough that a daily window is archived promptly
    /// without the job being the thing that wakes the database up.
    pub const DEFAULT_INTERVAL: Duration = Duration::minutes(15);

    /// How many windows one cycle will catch up at most.
    ///
    /// Bounded so a store that has been down for a month catches up over several
    /// cycles instead of holding one process for hours — each window is its own
    /// commit and independently recoverable, so stopping early costs nothing.
    pub const DEFAULT_MAX_WINDOWS: usize = 32;

    /// Build maintenance for one store, with defaults.
    pub fn new(store: crate::session::MeterStore) -> Self {
        Self::over(vec![store])
    }

    /// Build maintenance over several stores — every table of a catalog.
    ///
    /// Visited in the order given, one after another. Sequential rather than
    /// concurrent for the reason `archive_all` is: each table's archival holds
    /// its own lease and its own PostgreSQL connections, and running twenty at
    /// once turns a background job into a load spike on the operational database
    /// it is meant to be relieving.
    ///
    /// A table whose cycle fails does not stop the others — the outcome names it
    /// and the next tick tries again — because the tables share nothing a partial
    /// run could corrupt.
    pub fn over(stores: Vec<crate::session::MeterStore>) -> Self {
        Self {
            stores,
            interval: Self::DEFAULT_INTERVAL,
            max_windows: Self::DEFAULT_MAX_WINDOWS,
            expire_snapshots: false,
            retention: None,
        }
    }

    /// The tables this loop maintains.
    pub fn tables(&self) -> Vec<&str> {
        self.stores.iter().map(|s| s.table()).collect()
    }

    /// How often [`spawn`](Self::spawn) runs a cycle.
    pub fn interval(mut self, interval: Duration) -> Self {
        self.interval = interval;
        self
    }

    /// Cap the windows one cycle catches up.
    pub fn max_windows(mut self, max: usize) -> Self {
        self.max_windows = max.max(1);
        self
    }

    /// Also expire snapshots past the configured retention window.
    ///
    /// **Off by default, and deliberately so.** The retention window decides how
    /// far back a settlement can be reproduced, which is a compliance decision
    /// rather than a disk-space one. A store that quietly expired snapshots
    /// would be making that decision on the operator's behalf.
    pub fn expire_snapshots(mut self, enabled: bool) -> Self {
        self.expire_snapshots = enabled;
        self
    }

    /// Also anonymise subjects whose readings have passed a retention ceiling.
    ///
    /// **Off by default.** § 60 Abs. 6 MsbG obliges the Messstellenbetreiber to
    /// erase or anonymise personenbezogene Messwerte as soon as storing them is
    /// no longer necessary, *"spätestens jedoch nach drei Jahren ab dem Schluss
    /// des Kalenderjahres"* — a duty on a clock rather than an answer to a
    /// request, which is why it belongs on a schedule. It is opt-in for the
    /// reason snapshot expiry is: destroying a linkage is irreversible, so a
    /// store that did it uninvited would take a compliance decision on the
    /// operator's behalf.
    ///
    /// # A catalog operation even with one table
    ///
    /// The sweep runs across **every** store this loop holds, and a subject is
    /// due only once all of them have passed the ceiling. The registry is
    /// deployment-wide — one map keyed by natural identifier — so a single
    /// erasure unlinks a subject everywhere, and sweeping per table would orphan
    /// readings the other tables still hold.
    ///
    /// `reason` and `actor` go into the audit trail, which is what makes an
    /// erasure provable to a regulator; neither may be empty.
    ///
    /// ```no_run
    /// # use meterstore::{Maintenance, Retention};
    /// # fn example(m: Maintenance) -> Maintenance {
    /// m.anonymise_after(Retention::CalendarYears(3), "§ 60 Abs. 6 MsbG", "retention-job")
    /// # }
    /// ```
    pub fn anonymise_after(
        mut self,
        policy: crate::erasure::Retention,
        reason: impl Into<String>,
        actor: impl Into<String>,
    ) -> Self {
        self.retention = Some(RetentionSweep {
            policy,
            reason: reason.into(),
            actor: actor.into(),
        });
        self
    }

    /// The retention policy this loop applies, if any.
    pub fn retention(&self) -> Option<crate::erasure::Retention> {
        self.retention.as_ref().map(|r| r.policy)
    }

    /// Run one cycle over every table.
    ///
    /// Per table: archival first, then the invariant check, so the check sees the
    /// state the cycle produced rather than the one it started from. A failed
    /// archival ends *that table's* half — expiring snapshots or reporting health
    /// on top of a half-finished archival would describe a state that does not
    /// exist — and the cycle moves on to the next table.
    ///
    /// # A failure is reported, not returned
    ///
    /// `Ok` with [`MaintenanceOutcome::failures`] populated, rather than `Err`.
    /// The tables share nothing a partial run could corrupt, and what fails here
    /// persists until an operator acts — a quarantined schema, an unreachable
    /// catalogue — so stopping would let one such table freeze archival for every
    /// other, whose hot tier then grows without bound for the length of the
    /// incident.
    ///
    /// [`healthy`](MaintenanceOutcome::healthy) is false while any table failed,
    /// so a caller checking only that still notices.
    pub async fn run_once(&self, now: OffsetDateTime) -> Result<MaintenanceOutcome> {
        let mut tables = Vec::with_capacity(self.stores.len());
        for store in &self.stores {
            // **A failing table does not end the cycle.** The tables share
            // nothing a partial run could corrupt, and the states that fail here
            // — a quarantined schema, an unreachable catalogue — persist until an
            // operator acts. Aborting would mean one quarantined table freezing
            // archival for every other, whose hot tier then grows without bound
            // for as long as the quarantine lasts: a second, larger incident
            // caused by the reporting of the first.
            tables.push(match self.run_table(store, now).await {
                Ok(done) => done,
                Err(e) => {
                    warn!(
                        table = store.table(),
                        error = %e,
                        "maintenance failed for this table; the cycle continues with the rest"
                    );
                    TableMaintenance {
                        table: store.table().to_string(),
                        failure: Some(e.to_string()),
                        ..Default::default()
                    }
                }
            });
        }

        // **After** archival, and over every store at once. The order is no
        // longer load-bearing — the sweep reads no relation, only the registry's
        // own `epoch` column — but it stays last because that is where a duty
        // that comes due on a clock belongs in a cycle whose other steps are
        // about moving data. Over every store because the registry is
        // deployment-wide: the stores are consulted to find it and to tell
        // "nothing was due" apart from "this deployment stores no reference".
        let (anonymised, retention_failure) = match &self.retention {
            None => (Vec::new(), None),
            Some(sweep) => {
                let cutoff = sweep.policy.cutoff(now);
                match super::catalog::anonymise_across(
                    self.stores.iter(),
                    cutoff,
                    &sweep.reason,
                    &sweep.actor,
                    now,
                )
                .await
                {
                    Ok(records) => (records, None),
                    Err(e) => {
                        warn!(
                            error = %e,
                            %cutoff,
                            "retention sweep failed; the cycle continues and the next tick retries"
                        );
                        (Vec::new(), Some(e.to_string()))
                    }
                }
            }
        };

        let outcome = MaintenanceOutcome {
            tables,
            anonymised,
            retention_failure,
        };
        info!(
            tables = outcome.tables.len(),
            windows = outcome.windows_archived(),
            rows = outcome.rows_archived(),
            snapshots_expired = outcome.snapshots_expired(),
            anonymised = outcome.subjects_anonymised(),
            failed = outcome.failures().count(),
            healthy = outcome.healthy(),
            "maintenance cycle"
        );
        Ok(outcome)
    }

    /// One table's half of a cycle.
    async fn run_table(
        &self,
        store: &crate::session::MeterStore,
        now: OffsetDateTime,
    ) -> Result<TableMaintenance> {
        let archival = store.archive(now, self.max_windows).await?;

        // Expiry is a **metadata mutation**, so it belongs to the replica that
        // won the archive lease and to no other. Every replica runs the same
        // schedule by design; a loser that went on to expire snapshots anyway
        // would have two processes rewriting one table's metadata on their own
        // clocks, racing for a compare-and-swap that only one can win and
        // reporting the loss as a failed cycle. It also re-stamps the boundary
        // onto the current snapshot, which is the one step that must not
        // interleave with a commit moving it.
        //
        // Reading `status` below is not a mutation, so every replica still does
        // it — the invariant is worth checking from wherever it is noticed.
        let contended = archival.iter().all(|o| o.lease_contended);
        let snapshots_expired = match self.expire_snapshots && !contended {
            true => store.expire_snapshots(now).await?,
            false => 0,
        };

        let status = store.status(now).await?;
        let invariant_violations = status.invariant_violations.max(0) as u64;
        if invariant_violations > 0 {
            warn!(
                table = store.table(),
                invariant_violations, "rows are in the wrong tier: query results may be wrong"
            );
        }

        Ok(TableMaintenance {
            table: store.table().to_string(),
            archival,
            snapshots_expired,
            invariant_violations,
            failure: None,
        })
    }

    /// Run cycles on the configured interval, on the caller's runtime.
    ///
    /// The returned handle **owns** the loop: dropping it stops the loop after
    /// the cycle in flight, exactly as [`MaintenanceHandle::shutdown`] does. A
    /// handle that goes out of scope therefore cannot leave a background task
    /// archiving against a store nobody is watching.
    ///
    /// A failing cycle is logged and retried on the next tick rather than ending
    /// the loop: every failure mode here is either transient or needs an
    /// operator, and neither is helped by the scheduler giving up silently.
    pub fn spawn(self) -> MaintenanceHandle {
        let (tx, mut rx) = tokio::sync::oneshot::channel::<()>();
        let period = std::time::Duration::from_secs(
            u64::try_from(self.interval.whole_seconds().max(1)).unwrap_or(900),
        );
        let tables = self.tables().join(", ");

        let task = tokio::spawn(async move {
            let mut ticker = tokio::time::interval(period);
            ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);

            loop {
                tokio::select! {
                    _ = &mut rx => {
                        info!(tables = %tables, "maintenance stopped");
                        return;
                    }
                    _ = ticker.tick() => {
                        // The clock is read here and nowhere deeper, so every
                        // decision in a cycle is made against one instant.
                        let now = OffsetDateTime::now_utc();
                        if let Err(e) = self.run_once(now).await {
                            warn!(tables = %tables, error = %e, "maintenance cycle failed; retrying next tick");
                        }
                    }
                }
            }
        });

        MaintenanceHandle {
            task,
            stop: Some(tx),
        }
    }
}

/// A running maintenance loop, which it owns.
#[derive(Debug)]
pub struct MaintenanceHandle {
    task: tokio::task::JoinHandle<()>,
    stop: Option<tokio::sync::oneshot::Sender<()>>,
}

impl MaintenanceHandle {
    /// Ask the loop to stop after the cycle in flight, and wait for it.
    ///
    /// Graceful rather than an abort, because a cycle interrupted between the
    /// cold commit and the partition drop leaves an orphan for the next run to
    /// reclaim — recoverable, but pointless work to create on a clean shutdown.
    ///
    /// Dropping the handle does the same thing without waiting: the loop's stop
    /// signal is this handle's sender, so it fires either way.
    pub async fn shutdown(mut self) {
        if let Some(stop) = self.stop.take() {
            let _ = stop.send(());
        }
        let _ = (&mut self.task).await;
    }

    /// Whether the loop has ended.
    pub fn is_finished(&self) -> bool {
        self.task.is_finished()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::watermark::{ArchivalWindow, TieringWatermark};
    use time::macros::datetime;

    fn window() -> ArchivalWindow {
        ArchivalWindow::new(
            datetime!(2026-07-20 00:00 UTC),
            datetime!(2026-07-21 00:00 UTC),
        )
        .unwrap()
    }

    fn archived(rows: u64) -> ArchivalOutcome {
        ArchivalOutcome {
            window: Some(window()),
            rows,
            watermark: window().resulting_watermark(),
            orphans_reclaimed: 0,
            partitions_created: 0,
            lease_contended: false,
            deferred: false,
        }
    }

    fn idle() -> ArchivalOutcome {
        ArchivalOutcome {
            window: None,
            rows: 0,
            watermark: TieringWatermark::empty(),
            orphans_reclaimed: 0,
            partitions_created: 3,
            lease_contended: false,
            deferred: false,
        }
    }

    fn contended() -> ArchivalOutcome {
        ArchivalOutcome {
            lease_contended: true,
            ..idle()
        }
    }

    fn table(name: &str, archival: Vec<ArchivalOutcome>) -> TableMaintenance {
        TableMaintenance {
            table: name.to_string(),
            archival,
            snapshots_expired: 0,
            invariant_violations: 0,
            failure: None,
        }
    }

    #[test]
    fn an_outcome_sums_only_the_windows_that_moved_data() {
        let outcome = MaintenanceOutcome {
            tables: vec![table(
                "readings_versions",
                vec![archived(96), archived(4), idle()],
            )],
            ..Default::default()
        };
        assert_eq!(outcome.rows_archived(), 100);
        assert_eq!(outcome.windows_archived(), 2);
    }

    #[test]
    fn an_outcome_over_several_tables_folds_them_and_keeps_them_apart() {
        // The aggregate answers "did anything move"; the rows answer "which
        // table". A cycle that only summed would report a deployment healthy
        // while one of its tables was quarantined, and would give an operator no
        // name to act on.
        let outcome = MaintenanceOutcome {
            tables: vec![
                table("readings_versions", vec![archived(96)]),
                TableMaintenance {
                    invariant_violations: 3,
                    ..table("esa_typ2_versions", vec![archived(4)])
                },
            ],
            ..Default::default()
        };

        assert_eq!(outcome.rows_archived(), 100);
        assert_eq!(outcome.windows_archived(), 2);
        assert_eq!(outcome.invariant_violations(), 3);
        assert!(!outcome.healthy());

        let named: Vec<&str> = outcome.unhealthy().map(|t| t.table.as_str()).collect();
        assert_eq!(named, vec!["esa_typ2_versions"]);
        assert!(outcome.tables[0].healthy(), "the other table is fine");
    }

    #[test]
    fn a_failed_table_is_reported_rather_than_ending_the_cycle() {
        // Returning early would let one quarantined table freeze archival for
        // every other, whose hot tier then grows without bound for the length of
        // the quarantine — a second and larger incident caused by how the first
        // was reported. So a failure is a row, and `healthy` is false.
        let outcome = MaintenanceOutcome {
            tables: vec![
                table("readings_versions", vec![archived(96)]),
                TableMaintenance {
                    failure: Some("quarantined: column \"tenant\"".to_string()),
                    ..table("esa_typ2_versions", Vec::new())
                },
            ],
            ..Default::default()
        };

        assert!(!outcome.healthy());
        assert_eq!(
            outcome.failures().map(|(t, _)| t).collect::<Vec<_>>(),
            vec!["esa_typ2_versions"],
        );
        // The table that did work still reports what it did.
        assert_eq!(outcome.rows_archived(), 96);
        assert!(outcome.tables[0].healthy());
    }

    #[test]
    fn health_is_exactly_the_absence_of_violations() {
        let bad = MaintenanceOutcome {
            tables: vec![TableMaintenance {
                invariant_violations: 1,
                ..table("readings_versions", Vec::new())
            }],
            ..Default::default()
        };
        assert!(!bad.healthy());
        // A cycle over no tables is vacuously healthy, which is right: it is what
        // a catalog reports before anything has been added to it.
        assert!(MaintenanceOutcome::default().healthy());
    }

    #[test]
    fn contention_is_reported_and_is_not_a_failure() {
        // Every replica runs the schedule and one wins. The others must not look
        // like failures, or the alert fires on a healthy deployment.
        let outcome = MaintenanceOutcome {
            tables: vec![table("readings_versions", vec![contended()])],
            ..Default::default()
        };
        assert!(outcome.lease_contended());
        assert!(outcome.healthy());
        assert_eq!(outcome.rows_archived(), 0);
    }

    #[test]
    fn a_failed_retention_sweep_is_a_failure_the_table_alert_already_sees() {
        // The sweep is not any one table's work, but an operator watching
        // `failures()` for a name must not have to know about a second place to
        // look — a retention duty silently not running is the failure mode.
        let outcome = MaintenanceOutcome {
            tables: vec![table("readings_versions", vec![archived(96)])],
            retention_failure: Some("no subject column is declared".to_string()),
            ..Default::default()
        };

        assert!(!outcome.healthy());
        assert_eq!(
            outcome.failures().map(|(t, _)| t).collect::<Vec<_>>(),
            vec![RETENTION_LABEL],
        );
        // And the table's own work still reports as done.
        assert_eq!(outcome.rows_archived(), 96);
        assert!(outcome.tables[0].healthy());
    }

    #[test]
    fn a_retention_policy_is_opt_in_and_readable_back() {
        // Destroying a linkage is irreversible, so a loop must never do it
        // uninvited — and a deployment that did opt in has to be able to see it.
        let plain = Maintenance::over(Vec::new());
        assert_eq!(plain.retention(), None);

        let sweeping = Maintenance::over(Vec::new()).anonymise_after(
            crate::erasure::Retention::CalendarYears(3),
            "§ 60 Abs. 6 MsbG",
            "retention-job",
        );
        assert_eq!(
            sweeping.retention(),
            Some(crate::erasure::Retention::CalendarYears(3))
        );
    }

    #[test]
    fn the_defaults_are_sane() {
        // A cadence longer than the archival step would let the hot tier grow a
        // window at a time, and a zero window cap would make every cycle a
        // no-op with nothing reporting a failure.
        assert!(Maintenance::DEFAULT_INTERVAL < Duration::DAY);
        const { assert!(Maintenance::DEFAULT_MAX_WINDOWS >= 1) };
    }
}