|
| 1 | +//! Finding channels whose relay-signed discovery events are missing. |
| 2 | +//! |
| 3 | +//! The relay publishes a channel's kind:39000 (metadata), kind:39001 |
| 4 | +//! (admins) and kind:39002 (members) after the channel's own write commits, |
| 5 | +//! so a failure in between leaves a healthy channel that clients cannot |
| 6 | +//! confirm. These reads find such channels so the relay can publish them |
| 7 | +//! again. |
| 8 | +
|
| 9 | +use buzz_core::CommunityId; |
| 10 | +use buzz_datastore_tracing::datastore_span; |
| 11 | +use chrono::{DateTime, Utc}; |
| 12 | +use sqlx::{PgPool, Row}; |
| 13 | +use uuid::Uuid; |
| 14 | + |
| 15 | +use crate::{observability, Db, Result}; |
| 16 | + |
| 17 | +/// The relay-signed kinds every live channel has one live event of. |
| 18 | +pub const GROUP_DISCOVERY_KINDS: [i32; 3] = [39000, 39001, 39002]; |
| 19 | + |
| 20 | +/// A live channel with at least one of [`GROUP_DISCOVERY_KINDS`] missing. |
| 21 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 22 | +pub struct ChannelMissingDiscovery { |
| 23 | + /// Community that owns the channel. |
| 24 | + pub community_id: CommunityId, |
| 25 | + /// Canonical host for the owning community. |
| 26 | + pub host: String, |
| 27 | + /// The channel. |
| 28 | + pub channel_id: Uuid, |
| 29 | + /// When the channel was created; with `channel_id`, the scan's cursor. |
| 30 | + pub created_at: DateTime<Utc>, |
| 31 | +} |
| 32 | + |
| 33 | +// One probe per kind on `idx_events_addressable`. A channel's discovery |
| 34 | +// events are written after its row, so `created_at` bounds them from below |
| 35 | +// and, at run time, prunes the monthly `events` partitions to the ones since |
| 36 | +// the channel was created. The hour of slack covers a relay clock behind the |
| 37 | +// database's: events carry the relay's time, the channel row the database's. |
| 38 | +// `$also` adds further conditions on `e`, such as a constant floor the |
| 39 | +// planner can prune with whatever join it picks. |
| 40 | +// |
| 41 | +// Expands to the probe as a string literal, with the kinds array and relay |
| 42 | +// pubkey as the given bind parameters, so each query stays static SQL. |
| 43 | +macro_rules! missing_kind_probe { |
| 44 | + ($kinds:literal, $relay:literal, $also:literal) => { |
| 45 | + concat!( |
| 46 | + "SELECT 1 FROM unnest(", |
| 47 | + $kinds, |
| 48 | + "::int[]) AS wanted(kind) |
| 49 | + WHERE NOT EXISTS ( |
| 50 | + SELECT 1 FROM events e |
| 51 | + WHERE e.community_id = ch.community_id |
| 52 | + AND e.kind = wanted.kind |
| 53 | + AND e.pubkey = ", |
| 54 | + $relay, |
| 55 | + " |
| 56 | + AND e.channel_id = ch.id |
| 57 | + AND e.deleted_at IS NULL |
| 58 | + AND e.created_at >= ch.created_at - INTERVAL '1 hour' |
| 59 | + ", |
| 60 | + $also, |
| 61 | + " |
| 62 | + )" |
| 63 | + ) |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +const CHANNEL_MISSING_DISCOVERY_SQL: &str = concat!( |
| 68 | + "SELECT EXISTS ( |
| 69 | + SELECT 1 FROM channels ch |
| 70 | + WHERE ch.community_id = $1 AND ch.id = $2 AND ch.deleted_at IS NULL |
| 71 | + AND EXISTS (", |
| 72 | + missing_kind_probe!("$4", "$3", ""), |
| 73 | + "))" |
| 74 | +); |
| 75 | + |
| 76 | +const LIST_CHANNELS_MISSING_DISCOVERY_SQL: &str = concat!( |
| 77 | + "SELECT ch.community_id, community.host, ch.id, ch.created_at |
| 78 | + FROM channels ch |
| 79 | + JOIN communities community |
| 80 | + ON community.id = ch.community_id |
| 81 | + AND community.archived_at IS NULL |
| 82 | + AND community.deleted_at IS NULL |
| 83 | + AND community.deletion_state = 'active' |
| 84 | + WHERE ch.deleted_at IS NULL |
| 85 | + AND ch.created_at >= $1 |
| 86 | + AND ch.created_at < $2 |
| 87 | + AND (ch.created_at, ch.id) > ($3, $4) |
| 88 | + AND EXISTS (", |
| 89 | + missing_kind_probe!( |
| 90 | + "$6", |
| 91 | + "$5", |
| 92 | + "AND e.created_at >= $1::timestamptz - INTERVAL '1 hour'" |
| 93 | + ), |
| 94 | + ") |
| 95 | + ORDER BY ch.created_at, ch.id |
| 96 | + LIMIT $7" |
| 97 | +); |
| 98 | + |
| 99 | +/// Whether the live channel `channel_id` lacks a live relay-signed event of |
| 100 | +/// any of [`GROUP_DISCOVERY_KINDS`]. A deleted or unknown channel lacks |
| 101 | +/// nothing. |
| 102 | +pub async fn channel_missing_discovery( |
| 103 | + pool: &PgPool, |
| 104 | + community_id: CommunityId, |
| 105 | + channel_id: Uuid, |
| 106 | + relay_pubkey: &[u8], |
| 107 | +) -> Result<bool> { |
| 108 | + let mut connection = |
| 109 | + observability::acquire_writer(pool, observability::WriterOperation::EventWrite).await?; |
| 110 | + let missing: bool = sqlx::query_scalar(CHANNEL_MISSING_DISCOVERY_SQL) |
| 111 | + .bind(community_id.as_uuid()) |
| 112 | + .bind(channel_id) |
| 113 | + .bind(relay_pubkey) |
| 114 | + .bind(GROUP_DISCOVERY_KINDS.as_slice()) |
| 115 | + .fetch_one(&mut *connection) |
| 116 | + .await?; |
| 117 | + Ok(missing) |
| 118 | +} |
| 119 | + |
| 120 | +/// One page, in `(created_at, id)` order, of live channels in active |
| 121 | +/// communities created from `created_since` until `created_before` that |
| 122 | +/// lack a live relay-signed event of any of [`GROUP_DISCOVERY_KINDS`]. |
| 123 | +/// |
| 124 | +/// `after` is the previous page's last `(created_at, channel_id)`. A page |
| 125 | +/// shorter than `limit` is the last one. This is an internal |
| 126 | +/// cross-community maintenance read: callers must keep each channel's |
| 127 | +/// community when publishing for it. |
| 128 | +pub async fn list_channels_missing_discovery( |
| 129 | + pool: &PgPool, |
| 130 | + relay_pubkey: &[u8], |
| 131 | + created_since: DateTime<Utc>, |
| 132 | + created_before: DateTime<Utc>, |
| 133 | + after: Option<(DateTime<Utc>, Uuid)>, |
| 134 | + limit: i64, |
| 135 | +) -> Result<Vec<ChannelMissingDiscovery>> { |
| 136 | + let mut connection = |
| 137 | + observability::acquire_writer(pool, observability::WriterOperation::Maintenance).await?; |
| 138 | + let (after_created_at, after_id) = after.unwrap_or((created_since, Uuid::nil())); |
| 139 | + let rows = sqlx::query(LIST_CHANNELS_MISSING_DISCOVERY_SQL) |
| 140 | + .bind(created_since) |
| 141 | + .bind(created_before) |
| 142 | + .bind(after_created_at) |
| 143 | + .bind(after_id) |
| 144 | + .bind(relay_pubkey) |
| 145 | + .bind(GROUP_DISCOVERY_KINDS.as_slice()) |
| 146 | + .bind(limit) |
| 147 | + .fetch_all(&mut *connection) |
| 148 | + .await?; |
| 149 | + rows.into_iter() |
| 150 | + .map(|row| { |
| 151 | + Ok(ChannelMissingDiscovery { |
| 152 | + community_id: CommunityId::from_uuid(row.try_get("community_id")?), |
| 153 | + host: row.try_get("host")?, |
| 154 | + channel_id: row.try_get("id")?, |
| 155 | + created_at: row.try_get("created_at")?, |
| 156 | + }) |
| 157 | + }) |
| 158 | + .collect() |
| 159 | +} |
| 160 | + |
| 161 | +impl Db { |
| 162 | + /// Whether a live channel lacks any of its relay-signed discovery events. |
| 163 | + /// See [`channel_missing_discovery`]. |
| 164 | + #[datastore_span(name = "channel_missing_discovery", system = "postgresql")] |
| 165 | + pub async fn channel_missing_discovery( |
| 166 | + &self, |
| 167 | + community_id: CommunityId, |
| 168 | + channel_id: Uuid, |
| 169 | + relay_pubkey: &[u8], |
| 170 | + ) -> Result<bool> { |
| 171 | + channel_missing_discovery(&self.pool, community_id, channel_id, relay_pubkey).await |
| 172 | + } |
| 173 | + |
| 174 | + /// One page of channels lacking relay-signed discovery events. See |
| 175 | + /// [`list_channels_missing_discovery`]. |
| 176 | + #[datastore_span(name = "list_channels_missing_discovery", system = "postgresql")] |
| 177 | + pub async fn list_channels_missing_discovery( |
| 178 | + &self, |
| 179 | + relay_pubkey: &[u8], |
| 180 | + created_since: DateTime<Utc>, |
| 181 | + created_before: DateTime<Utc>, |
| 182 | + after: Option<(DateTime<Utc>, Uuid)>, |
| 183 | + limit: i64, |
| 184 | + ) -> Result<Vec<ChannelMissingDiscovery>> { |
| 185 | + list_channels_missing_discovery( |
| 186 | + &self.pool, |
| 187 | + relay_pubkey, |
| 188 | + created_since, |
| 189 | + created_before, |
| 190 | + after, |
| 191 | + limit, |
| 192 | + ) |
| 193 | + .await |
| 194 | + } |
| 195 | +} |
0 commit comments