|
| 1 | +//! App-owned follow-up work for a newly inserted `events` row. |
| 2 | +//! |
| 3 | +//! Two pieces of work follow every durable event insert: |
| 4 | +//! |
| 5 | +//! - **Push match enqueue.** Push-eligible kinds get a `push_match_queue` row |
| 6 | +//! when the community has an eligible lease. This runs right after the |
| 7 | +//! INSERT, in the same transaction, and any failure rejects the event. |
| 8 | +//! - **Channel TTL refresh.** A channel-scoped event pushes its ephemeral |
| 9 | +//! channel's `ttl_deadline` forward. [`AdmittedTx`] records the channel and |
| 10 | +//! runs the refresh as the last statement before COMMIT (see |
| 11 | +//! [`refresh_channel_ttls`]). A refresh failure is logged and counted, and |
| 12 | +//! the event still commits. |
| 13 | +//! |
| 14 | +//! This is the application-side replacement for the `events_enqueue_push_match` |
| 15 | +//! and `events_refresh_channel_ttl` triggers (migrations 0023, 0024, 0040). |
| 16 | +//! Both run during the migration window; the push enqueue is idempotent |
| 17 | +//! (`ON CONFLICT DO NOTHING`) and a second TTL refresh is harmless. Every |
| 18 | +//! production `INSERT INTO events` must call this module, which |
| 19 | +//! `tests/observability_source.rs` enforces. |
| 20 | +
|
| 21 | +use std::collections::BTreeSet; |
| 22 | + |
| 23 | +use buzz_core::CommunityId; |
| 24 | +use sqlx::PgConnection; |
| 25 | +use uuid::Uuid; |
| 26 | + |
| 27 | +use crate::{AdmittedTx, DbError, Result}; |
| 28 | + |
| 29 | +/// Event kinds that wake push subscribers. Keep identical to the relay's |
| 30 | +/// validated NIP-PL descriptor. |
| 31 | +pub(crate) const PUSH_MATCH_KINDS: [i32; 4] = [9, 40002, 45001, 45003]; |
| 32 | + |
| 33 | +/// Kind 9007 creates the channel and initializes its deadline itself. |
| 34 | +const KIND_CHANNEL_CREATE: i32 = 9007; |
| 35 | + |
| 36 | +/// SQLSTATE `lock_not_available`, raised when `lock_timeout` expires. |
| 37 | +const SQLSTATE_LOCK_NOT_AVAILABLE: &str = "55P03"; |
| 38 | + |
| 39 | +/// Follow-up for an event row inserted in an admitted transaction. |
| 40 | +/// |
| 41 | +/// Call only when the INSERT affected a row. Enqueues the push match job now |
| 42 | +/// and records the channel for the pre-commit TTL refresh. |
| 43 | +/// |
| 44 | +/// If the insert ran inside a savepoint that may still roll back, call |
| 45 | +/// [`enqueue_push_match`] inside the savepoint and |
| 46 | +/// [`AdmittedTx::record_channel_event`] after it is released instead. |
| 47 | +pub(crate) async fn after_admitted_insert( |
| 48 | + tx: &mut AdmittedTx, |
| 49 | + event_id: &[u8], |
| 50 | + kind: i32, |
| 51 | + channel_id: Option<Uuid>, |
| 52 | +) -> Result<()> { |
| 53 | + let community = tx.community(); |
| 54 | + enqueue_push_match(tx.conn(), community, event_id, kind).await?; |
| 55 | + tx.record_channel_event(channel_id, kind); |
| 56 | + Ok(()) |
| 57 | +} |
| 58 | + |
| 59 | +/// Enqueue a push match job for a just-inserted event, if its kind is |
| 60 | +/// push-eligible and the community has an eligible lease. |
| 61 | +/// |
| 62 | +/// Takes the push gate SHARED and holds it to transaction end. Lease |
| 63 | +/// activations take it EXCLUSIVE (`acquire_push_gate_lock` in `push.rs`), so |
| 64 | +/// an event either sees the committed lease or strictly precedes the |
| 65 | +/// activation, in which case no wake was owed. The lock must be its own |
| 66 | +/// statement: under READ COMMITTED the eligibility check needs a snapshot |
| 67 | +/// taken after the lock is granted. |
| 68 | +/// |
| 69 | +/// Errors propagate and reject the event, as the trigger did. |
| 70 | +pub(crate) async fn enqueue_push_match( |
| 71 | + conn: &mut PgConnection, |
| 72 | + community: CommunityId, |
| 73 | + event_id: &[u8], |
| 74 | + kind: i32, |
| 75 | +) -> Result<()> { |
| 76 | + if !PUSH_MATCH_KINDS.contains(&kind) { |
| 77 | + return Ok(()); |
| 78 | + } |
| 79 | + crate::observability::observe_advisory_lock( |
| 80 | + crate::observability::LockType::PushGate, |
| 81 | + sqlx::query("SELECT pg_advisory_xact_lock_shared(hashtextextended($1, 0))") |
| 82 | + .bind(crate::push::push_gate_lock_key(community)) |
| 83 | + .execute(&mut *conn), |
| 84 | + ) |
| 85 | + .await?; |
| 86 | + sqlx::query( |
| 87 | + "INSERT INTO push_match_queue (community_id, event_id) \ |
| 88 | + SELECT $1, $2 \ |
| 89 | + WHERE EXISTS ( \ |
| 90 | + SELECT 1 FROM push_leases \ |
| 91 | + WHERE community_id = $1 \ |
| 92 | + AND active \ |
| 93 | + AND endpoint_enabled \ |
| 94 | + AND expires_at > EXTRACT(EPOCH FROM now())::bigint) \ |
| 95 | + ON CONFLICT DO NOTHING", |
| 96 | + ) |
| 97 | + .bind(community.as_uuid()) |
| 98 | + .bind(event_id) |
| 99 | + .execute(&mut *conn) |
| 100 | + .await?; |
| 101 | + Ok(()) |
| 102 | +} |
| 103 | + |
| 104 | +/// The channel whose TTL an event of `kind` in `channel_id` refreshes, if any. |
| 105 | +pub(crate) fn ttl_refresh_channel(channel_id: Option<Uuid>, kind: i32) -> Option<Uuid> { |
| 106 | + channel_id.filter(|_| kind != KIND_CHANNEL_CREATE) |
| 107 | +} |
| 108 | + |
| 109 | +/// Refresh the TTL deadline of every channel that received an event in this |
| 110 | +/// transaction. [`AdmittedTx::commit`] calls this as its last work before |
| 111 | +/// COMMIT, which keeps the deferred-trigger timing from migration 0024: the |
| 112 | +/// deadline is computed at commit. An ephemeral channel's row lock, taken by |
| 113 | +/// the UPDATE, is held through the remaining refreshes and the COMMIT round |
| 114 | +/// trip; the trigger held it only inside COMMIT. A permanent channel's row is |
| 115 | +/// never locked. |
| 116 | +/// |
| 117 | +/// Channels are refreshed in sorted order, so two transactions that touch the |
| 118 | +/// same channels cannot deadlock on the shared locks against an exclusive |
| 119 | +/// waiter. Each refresh runs in its own savepoint. A failure is rolled back, |
| 120 | +/// logged, and counted, and the event still commits, as with the trigger's |
| 121 | +/// `EXCEPTION WHEN OTHERS`. A cancelled statement (`57014`) still rejects the |
| 122 | +/// transaction, because plpgsql never caught it either. |
| 123 | +pub(crate) async fn refresh_channel_ttls( |
| 124 | + conn: &mut PgConnection, |
| 125 | + community: CommunityId, |
| 126 | + channels: &BTreeSet<Uuid>, |
| 127 | +) -> Result<()> { |
| 128 | + for &channel in channels { |
| 129 | + sqlx::query("SAVEPOINT channel_ttl_refresh") |
| 130 | + .execute(&mut *conn) |
| 131 | + .await?; |
| 132 | + match refresh_channel_ttl(conn, community, channel).await { |
| 133 | + Ok(()) => { |
| 134 | + sqlx::query("RELEASE SAVEPOINT channel_ttl_refresh") |
| 135 | + .execute(&mut *conn) |
| 136 | + .await?; |
| 137 | + } |
| 138 | + Err(error) => { |
| 139 | + // plpgsql `WHEN OTHERS` never caught `query_canceled`, so a |
| 140 | + // cancelled refresh has always rejected the event. |
| 141 | + if error.is_statement_cancelled() { |
| 142 | + return Err(error); |
| 143 | + } |
| 144 | + sqlx::query("ROLLBACK TO SAVEPOINT channel_ttl_refresh") |
| 145 | + .execute(&mut *conn) |
| 146 | + .await?; |
| 147 | + sqlx::query("RELEASE SAVEPOINT channel_ttl_refresh") |
| 148 | + .execute(&mut *conn) |
| 149 | + .await?; |
| 150 | + let reason = if sqlstate(&error).as_deref() == Some(SQLSTATE_LOCK_NOT_AVAILABLE) { |
| 151 | + "lock_timeout" |
| 152 | + } else { |
| 153 | + "error" |
| 154 | + }; |
| 155 | + metrics::counter!( |
| 156 | + "buzz_db_channel_ttl_refresh_failures_total", |
| 157 | + "reason" => reason, |
| 158 | + ) |
| 159 | + .increment(1); |
| 160 | + tracing::warn!( |
| 161 | + %error, |
| 162 | + channel_id = %channel, |
| 163 | + reason, |
| 164 | + "channel TTL refresh failed; event committed without refreshing the deadline" |
| 165 | + ); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + Ok(()) |
| 170 | +} |
| 171 | + |
| 172 | +async fn refresh_channel_ttl( |
| 173 | + conn: &mut PgConnection, |
| 174 | + community: CommunityId, |
| 175 | + channel: Uuid, |
| 176 | +) -> Result<()> { |
| 177 | + // SHARED here, EXCLUSIVE in `update_channel` for TTL transitions: the same |
| 178 | + // total order the 0022 row lock gave, without serializing hot channels. |
| 179 | + sqlx::query("SELECT pg_advisory_xact_lock_shared(hashtextextended($1, 0))") |
| 180 | + .bind(crate::channel::channel_ttl_lock_key(community, channel)) |
| 181 | + .execute(&mut *conn) |
| 182 | + .await?; |
| 183 | + // A separate statement, so its snapshot is taken after the lock is |
| 184 | + // granted. `ttl_seconds IS NOT NULL` filters a permanent channel before |
| 185 | + // any tuple lock is taken, so its row is never locked (migration 0024). |
| 186 | + sqlx::query( |
| 187 | + "UPDATE channels \ |
| 188 | + SET ttl_deadline = clock_timestamp() + make_interval(secs => ttl_seconds) \ |
| 189 | + WHERE community_id = $1 AND id = $2 \ |
| 190 | + AND ttl_seconds IS NOT NULL \ |
| 191 | + AND archived_at IS NULL \ |
| 192 | + AND deleted_at IS NULL", |
| 193 | + ) |
| 194 | + .bind(community.as_uuid()) |
| 195 | + .bind(channel) |
| 196 | + .execute(&mut *conn) |
| 197 | + .await?; |
| 198 | + Ok(()) |
| 199 | +} |
| 200 | + |
| 201 | +fn sqlstate(error: &DbError) -> Option<String> { |
| 202 | + match error { |
| 203 | + DbError::Sqlx(sqlx::Error::Database(database_error)) => { |
| 204 | + database_error.code().map(|code| code.into_owned()) |
| 205 | + } |
| 206 | + _ => None, |
| 207 | + } |
| 208 | +} |
0 commit comments