Skip to main content

tuwunel_service/migrations/injectivity/
mod.rs

1//! Repair reported short-id residue while preserving unproven data.
2//!
3//! A database the superseded repair settled is repaired only when its short-id
4//! families disagree. A versioned record distinguishes a verified clean
5//! completion, one that left residue, and a settlement whose families agree.
6//! Legacy markers are kept present so older binaries skip their superseded
7//! repair.
8
9mod seen;
10
11use tuwunel_core::result::NotFound;
12use tuwunel_database::Database;
13
14use self::seen::{MARKER, repair, run, settle, stamp};
15use crate::Services;
16
17const SUPERSEDED: &str = "fix_short_injectivity";
18const CACHE_CLEARED: &str = "clear_auth_chain_cache";
19const LEGACY: [&str; 2] = [SUPERSEDED, CACHE_CLEARED];
20
21/// Records a fresh database without scanning its populations.
22///
23/// The versioned record is written and synced fallibly, so a failed write or
24/// sync never certifies it. Legacy markers for older binaries follow only a
25/// certified record, so a database whose stamp fails stays eligible on the next
26/// start.
27pub(super) fn mark_clean(services: &Services) {
28	if stamp(&services.db).is_none() {
29		return;
30	}
31
32	let global = &services.db["global"];
33
34	for marker in LEGACY {
35		global.insert(marker, []);
36	}
37}
38
39/// Repairs an eligible database without making repair failures fatal to startup.
40///
41/// A database the superseded repair settled has its short-id families checked
42/// once instead, and is repaired only when they disagree. Read-only opens are
43/// skipped, and an outcome is recorded only once it is durable. Legacy markers
44/// wait for that record, so a database whose attempt ends without one stays
45/// eligible on the next start.
46pub(super) async fn fix(services: &Services) {
47	let db = &services.db;
48
49	if settled_before(db).await && settle(services).await {
50		return;
51	}
52
53	run(db, async || {
54		services.server.progress.begin(MARKER);
55		repair(services).await
56	})
57	.await;
58
59	if db["global"].get(MARKER).await.is_ok() {
60		stamp_legacy(services).await;
61	}
62}
63
64/// Whether the superseded repair settled this database before this one ran.
65///
66/// The settled form is the superseded marker's empty value beside a recorded
67/// auth chain cache clear; without the clear, the full repair runs and clears
68/// the cache. A decline record, an absent marker or a failed read leaves the
69/// database eligible, and a recorded outcome is left for the runner to honor. A
70/// build that stamped the legacy markers before attempting this repair leaves
71/// the same form when its attempt ended early, and nothing tells the two apart.
72async fn settled_before(db: &Database) -> bool {
73	let global = &db["global"];
74
75	global.get(MARKER).await.is_missing()
76		&& global.get(CACHE_CLEARED).await.is_ok()
77		&& global
78			.get(SUPERSEDED)
79			.await
80			.as_deref()
81			.is_ok_and(<[u8]>::is_empty)
82}
83
84/// Writes each absent legacy marker so a rollback never reruns the old repair.
85///
86/// Releases before this repair gate their own scan on the marker's presence
87/// alone. Existing values, decline records included, are kept, and a failed
88/// read never counts as absence.
89async fn stamp_legacy(services: &Services) {
90	if services.db.engine.is_read_only() {
91		return;
92	}
93
94	let global = &services.db["global"];
95
96	for marker in LEGACY {
97		if global.get(marker).await.is_missing() {
98			global.insert(marker, []);
99		}
100	}
101}