tuwunel_service/integrity/
columns.rs1use 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
26pub struct Unreadable {
31 pub column: String,
33
34 pub error: String,
36}
37
38pub struct Unused {
43 pub column: String,
45
46 pub bytes: Option<u64>,
48}
49
50#[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#[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}