tuwunel_service/integrity/
roomid_tscount_pducount.rs1use 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
33pub enum Finding {
38 Missing(Row),
40
41 Mismatched(Row),
43
44 Unparsable {
46 key: KeyBuf,
48 },
49}
50
51pub struct Row {
56 pub pdu_id: RawPduId,
58
59 pub room_id: OwnedRoomId,
61
62 pub ts: u64,
64}
65
66pub(crate) enum Expected {
71 Row(Row),
73
74 Unparsable(KeyBuf),
76}
77
78#[derive(Deserialize)]
79struct PduRoomTs {
80 room_id: OwnedRoomId,
81 origin_server_ts: MilliSecondsSinceUnixEpoch,
82}
83
84#[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#[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#[implement(Row)]
175#[inline]
176pub(crate) fn key(&self) -> Key<'_> { (&self.room_id, self.ts, bias_count(self.pdu_id.count())) }