Skip to main content

tuwunel_service/integrity/
roomid_tscount_pducount.rs

1//! The timestamp index `roomid_tscount_pducount`.
2//!
3//! Each timeline event has one row keyed by its room, its timestamp and its
4//! biased count, holding the count, so a timestamp seeks to the events nearest
5//! it. The index derives from `pduid_pdu`, and its rebuild writes over it without
6//! clearing it first.
7
8use std::{fmt, sync::Arc};
9
10use futures::Stream;
11use ruma::{MilliSecondsSinceUnixEpoch, OwnedRoomId, RoomId};
12use serde::Deserialize;
13use tuwunel_core::{
14	Result, implement,
15	result::NotFound,
16	utils::{TryReadyExt, stream::TryBroadbandExt},
17};
18use tuwunel_database::{KeyBuf, KeyVal, Map};
19use tuwunel_matrix::pdu::RawPduId;
20
21use super::{Check, Report, ScanExt, tally};
22use crate::{Services, rooms::timeline::bias_count};
23
24type Key<'a> = (&'a RoomId, u64, u64);
25
26pub(super) static CHECK: Check = Check {
27	name: "timestamp-index",
28	about: "Every timeline event has its roomid_tscount_pducount row, holding its count",
29	quiescent: true,
30	walk: |services, limit| tally(scan(services), limit),
31};
32
33/// A timeline event whose timestamp index row is absent or wrong.
34///
35/// Such an event is out of reach of a seek by timestamp. Only an event row whose
36/// key is not a PDU id gives no row to look for.
37pub enum Finding {
38	/// The event has no row in the index.
39	Missing(Row),
40
41	/// The event's row holds a count other than the event's.
42	Mismatched(Row),
43
44	/// The event row's key is not a PDU id.
45	Unparsable {
46		/// The stored key.
47		key: KeyBuf,
48	},
49}
50
51/// The row one timeline event gives the index.
52///
53/// Its key is the room, the timestamp and the biased count, and its value is the
54/// count.
55pub struct Row {
56	/// The event the row indexes.
57	pub pdu_id: RawPduId,
58
59	/// The event's room.
60	pub room_id: OwnedRoomId,
61
62	/// The event's origin timestamp, in milliseconds.
63	pub ts: u64,
64}
65
66/// What one event row gives the index.
67///
68/// An event row whose key is not a PDU id gives no row to write, but the check
69/// reports it.
70pub(crate) enum Expected {
71	/// The event's row.
72	Row(Row),
73
74	/// The event row's key is not a PDU id.
75	Unparsable(KeyBuf),
76}
77
78#[derive(Deserialize)]
79struct PduRoomTs {
80	room_id: OwnedRoomId,
81	origin_server_ts: MilliSecondsSinceUnixEpoch,
82}
83
84/// Streams each timeline event whose index row is absent or holds another count.
85///
86/// The two columns sort differently, so each event's row is read by key. Only
87/// coverage is checked: a row naming no stored event is not reported, because
88/// the rebuild removes none either. An event whose value does not decode gives no
89/// row here, and is left to the relation index check, which decodes every event.
90#[tracing::instrument(level = "trace", skip_all)]
91pub fn scan(services: &Services) -> impl Stream<Item = Result<Finding>> + Send + '_ {
92	let index = &services.db["roomid_tscount_pducount"];
93
94	expected(services)
95		.broad_and_then(async |expected| match expected {
96			| Expected::Row(row) => verify(index, row).await,
97			| Expected::Unparsable(key) => Ok(Some(Finding::Unparsable { key })),
98		})
99		.ready_try_filter_map(Ok)
100}
101
102/// Streams the row each timeline event gives the index.
103///
104/// The check compares these with the index and the rebuild writes every one, so
105/// the two agree on what the index should hold.
106#[tracing::instrument(level = "trace", skip_all)]
107pub(crate) fn expected(services: &Services) -> impl Stream<Item = Result<Expected>> + Send + '_ {
108	services.db["pduid_pdu"]
109		.raw_scan()
110		.scanned(&services.server)
111		.ready_try_filter_map(|row| Ok(derive(row)))
112}
113
114impl Report for Finding {
115	fn kind(&self) -> &'static str {
116		match self {
117			| Self::Missing(_) => "missing-row",
118			| Self::Mismatched(_) => "mismatched-count",
119			| Self::Unparsable { .. } => "unparsable-key",
120		}
121	}
122}
123
124impl fmt::Display for Finding {
125	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126		match self {
127			| Self::Missing(Row { pdu_id, room_id, ts }) => {
128				write!(f, "event {pdu_id:?} in {room_id} at {ts} has no index row")
129			},
130			| Self::Mismatched(Row { pdu_id, room_id, ts }) => {
131				write!(
132					f,
133					"index row of event {pdu_id:?} in {room_id} at {ts} holds another count"
134				)
135			},
136			| Self::Unparsable { key } => {
137				write!(f, "event key is not a PDU id: {}", key.escape_ascii())
138			},
139		}
140	}
141}
142
143fn derive((key, value): KeyVal<'_>) -> Option<Expected> {
144	let Some(pdu_id) = RawPduId::try_from_bytes(key) else {
145		return Some(Expected::Unparsable(KeyBuf::from_slice(key)));
146	};
147
148	let PduRoomTs { room_id, origin_server_ts } = serde_json::from_slice(value).ok()?;
149	let ts = origin_server_ts.get().into();
150
151	Some(Expected::Row(Row { pdu_id, room_id, ts }))
152}
153
154#[tracing::instrument(level = "debug", skip_all)]
155async fn verify(index: &Arc<Map>, row: Row) -> Result<Option<Finding>> {
156	let stored = index
157		.qry(&row.key())
158		.await
159		.optional()?
160		.map(|count| *count == row.pdu_id.count());
161
162	let finding = match stored {
163		| None => Some(Finding::Missing(row)),
164		| Some(false) => Some(Finding::Mismatched(row)),
165		| Some(true) => None,
166	};
167
168	Ok(finding)
169}
170
171/// The key of the index row.
172///
173/// Its count is biased so that backfilled events sort before the rest.
174#[implement(Row)]
175#[inline]
176pub(crate) fn key(&self) -> Key<'_> { (&self.room_id, self.ts, bias_count(self.pdu_id.count())) }