Skip to main content

tuwunel_service/migrations/injectivity/seen/
identity.rs

1use std::collections::BTreeSet;
2
3use futures::{TryStreamExt, future::try_join};
4use tuwunel_core::{
5	Result,
6	utils::{IterStream, TryReadyExt, stream::TryWidebandExt},
7};
8use tuwunel_database::{Database, KeyBuf, Map, Slice, Txn, serialize_key, serialize_val};
9
10use crate::{
11	Services,
12	integrity::{
13		ScanExt,
14		short_ids::{
15			identity::{Candidate, Family, Identities, Kind, admitted, census},
16			references::References,
17		},
18	},
19};
20
21const CACHE_BATCH: usize = 64;
22
23#[tracing::instrument(level = "debug", skip_all)]
24pub(super) async fn repair(services: &Services) -> Result<Identities> {
25	clear_cache(services, "authchainkey_authchain").await?;
26	let identities = census(services).await?;
27	let identities = match heal(services, &identities).await? {
28		| false => identities,
29		| true => census(services).await?,
30	};
31
32	let references = References::census(services, &identities).await?;
33
34	let events =
35		cleanup(services, &identities.events, &references.events, references.event_complete)
36			.await?;
37
38	let statekeys = cleanup(
39		services,
40		&identities.statekeys,
41		&references.statekeys,
42		references.statekey_complete,
43	)
44	.await?;
45
46	match events || statekeys {
47		| false => Ok(identities),
48		| true => census(services).await,
49	}
50}
51
52#[tracing::instrument(level = "debug", skip_all)]
53pub(super) async fn clear_cache(services: &Services, column: &str) -> Result {
54	let db = &services.db;
55	let map = &db[column];
56
57	map.raw_keys()
58		.scanned(&services.server)
59		.map_ok(KeyBuf::from_slice)
60		.try_chunks(CACHE_BATCH)
61		.map_err(|error| error.1)
62		.ready_try_for_each(|keys| deletion(db, map, keys).try_write())
63		.await?;
64
65	// One barrier covers every batch; a cache left partly cleared is cleared again on retry.
66	db.engine.sync()
67}
68
69fn deletion(db: &Database, map: &Map, keys: impl IntoIterator<Item = impl AsRef<Slice>>) -> Txn {
70	keys.into_iter().fold(db.txn(), |mut txn, key| {
71		txn.del_raw(map, key);
72		txn
73	})
74}
75
76#[tracing::instrument(level = "debug", skip_all)]
77async fn heal(services: &Services, identities: &Identities) -> Result<bool> {
78	let db = &services.db;
79	let Identities { events, statekeys } = identities;
80	let (event_heals, statekey_heals) =
81		try_join(admissible(db, events, healable), admissible(db, statekeys, healable)).await?;
82
83	if event_heals.len() == 0 && statekey_heals.len() == 0 {
84		return Ok(false);
85	}
86
87	// A persisted summary omits entries a heal makes resolvable, so it clears before any write.
88	clear_cache(services, "roomid_spacehierarchy").await?;
89
90	event_heals
91		.map(|candidate| (events, candidate))
92		.chain(statekey_heals.map(|candidate| (statekeys, candidate)))
93		.try_for_each(|(family, candidate)| {
94			services.server.check_running()?;
95			write(db, family, candidate)
96		})
97		.map(|()| true)
98}
99
100fn healable(candidate: &Candidate) -> bool {
101	matches!(candidate.kind, Kind::Reverse | Kind::Forward)
102}
103
104fn write(db: &Database, family: &Family, candidate: &Candidate) -> Result {
105	let short = serialize_key(candidate.short)?;
106	let identity = serialize_val(candidate.identity.as_slice())?;
107	let txn = match candidate.kind {
108		| Kind::Reverse => Txn::insert(&db[family.reverse], [(short, identity)]),
109		| _ => Txn::insert(&db[family.forward], [(identity, short)]),
110	};
111
112	txn.try_execute().map_err(Into::into)
113}
114
115#[tracing::instrument(level = "debug", skip_all)]
116pub(super) async fn cleanup(
117	services: &Services,
118	family: &Family,
119	references: &BTreeSet<u64>,
120	complete: bool,
121) -> Result<bool> {
122	if !complete {
123		return Ok(false);
124	}
125
126	let db = &services.db;
127	let eligible = |candidate: &Candidate| {
128		matches!(candidate.kind, Kind::Alias(_)) && !references.contains(&candidate.short)
129	};
130
131	let keys = admissible(db, family, eligible)
132		.await?
133		.map(|candidate| candidate.short.to_be_bytes());
134
135	let changed = keys.len() != 0;
136
137	services.server.check_running()?;
138	deletion(db, &db[family.reverse], keys)
139		.try_execute()
140		.map(|()| changed)
141		.map_err(Into::into)
142}
143
144async fn admissible<'a, F>(
145	db: &Database,
146	family: &'a Family,
147	eligible: F,
148) -> Result<impl ExactSizeIterator<Item = &'a Candidate>>
149where
150	F: Fn(&Candidate) -> bool + Send + Sync,
151{
152	// Collected whole, so every buffered admission error precedes the caller's writes.
153	let candidates: Vec<_> = family
154		.candidates
155		.iter()
156		.filter(|candidate| eligible(candidate))
157		.try_stream()
158		.wide_and_then(async |candidate| {
159			let admit = admitted(db, family, candidate).await?;
160
161			Ok(admit.then_some(candidate))
162		})
163		.ready_try_filter_map(Ok)
164		.try_collect()
165		.await?;
166
167	Ok(candidates.into_iter())
168}