Skip to main content

tuwunel_service/integrity/
columns.rs

1use std::fmt;
2
3use futures::{Stream, StreamExt};
4use tuwunel_core::{
5	Result,
6	utils::{IterStream, TryReadyExt},
7};
8
9use super::{Check, Report, ScanExt, tally};
10use crate::Services;
11
12pub(super) static CHECK: Check = Check {
13	name: "columns",
14	about: "Every row of every column reads back and passes its checksum",
15	quiescent: false,
16	walk: |services, limit| tally(scan(services), limit),
17};
18
19pub(super) static UNUSED: Check = Check {
20	name: "unused-columns",
21	about: "No column on disk is outside this build's schema",
22	quiescent: false,
23	walk: |services, limit| tally(unused(services), limit),
24};
25
26/// A column whose rows could not all be read.
27///
28/// The walk of a column ends at its first bad block, so this says the column is
29/// damaged, not how much of it is.
30pub struct Unreadable {
31	/// The column.
32	pub column: String,
33
34	/// What stopped the walk.
35	pub error: String,
36}
37
38/// A column on disk that this build's schema does not describe.
39///
40/// Nothing in this build reads such a column except a migration importing from
41/// it, so its bytes are often reclaimable.
42pub struct Unused {
43	/// The column.
44	pub column: String,
45
46	/// Bytes its table files occupy; absent when they could not be measured.
47	pub bytes: Option<u64>,
48}
49
50/// Streams each column that fails a full read.
51///
52/// Every row of every column family on the engine is read with its checksum
53/// verified, including families this build's schema does not describe. A failed
54/// walk is a finding about that column and the next one still runs; only a stop
55/// request ends the stream.
56#[tracing::instrument(level = "trace", skip_all)]
57pub fn scan(services: &Services) -> impl Stream<Item = Result<Unreadable>> + Send + '_ {
58	services
59		.db
60		.engine
61		.column_names()
62		.stream()
63		.filter_map(async |column| walk(services, column).await.transpose())
64}
65
66/// Streams each column on disk that this build's schema does not describe.
67///
68/// Another server's columns and an older schema's leftovers land here, which is
69/// how data nothing reads any more shows up. A foreign column a later migration
70/// still wants is listed all the same.
71#[tracing::instrument(level = "trace", skip_all)]
72pub fn unused(services: &Services) -> impl Stream<Item = Result<Unused>> + Send + '_ {
73	services.db.undescribed().stream().map(|column| {
74		let bytes = services.db.engine.column_size(&column).ok();
75
76		Ok(Unused { column, bytes })
77	})
78}
79
80impl Report for Unreadable {
81	fn kind(&self) -> &'static str { "unreadable-column" }
82}
83
84impl fmt::Display for Unreadable {
85	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86		write!(f, "column {} failed a full read: {}", self.column, self.error)
87	}
88}
89
90impl Report for Unused {
91	fn kind(&self) -> &'static str { "unused-column" }
92}
93
94impl fmt::Display for Unused {
95	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96		match self.bytes {
97			| Some(bytes) =>
98				write!(f, "column {} is unused and holds {bytes} bytes", self.column),
99			| None => write!(f, "column {} is unused", self.column),
100		}
101	}
102}
103
104#[tracing::instrument(level = "debug", skip(services))]
105async fn walk(services: &Services, column: String) -> Result<Option<Unreadable>> {
106	let Some(map) = services.db.open_cf(&column)? else {
107		return Ok(None);
108	};
109
110	let walked = map
111		.raw_scan()
112		.scanned(&services.server)
113		.ready_try_for_each(|_| Ok(()))
114		.await;
115
116	match walked {
117		| Err(error) if error.is_interrupted() => Err(error),
118		| Err(error) => Ok(Some(Unreadable { column, error: error.to_string() })),
119		| Ok(()) => Ok(None),
120	}
121}