snodra-messaging-sqlx 0.7.0

Snodra messaging: the SQL message store on PostgreSQL and MySQL (outbox, processed events, dead letters, processor control, external calls, sagas), sharing its tables with snodra-messaging-jdbc
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! `snodra_outbox` and `snodra_processed_events`.
//!
//! An append joins the caller's transaction (and, on PostgreSQL with notify on, runs
//! `pg_notify('snodra_outbox', '')` in it: delivered at the commit, dropped by a rollback). A claim
//! runs in a transaction of its own: `SELECT ... FOR UPDATE SKIP LOCKED` in `(created_at, seq)`
//! order picks the entries free, already the owner's or whose lease passed, then `owner` and
//! `lease_until` mark them the owner's; a concurrent claim skips the rows this one locked and,
//! once it committed, no longer matches them.

use std::sync::Arc;
use std::time::Duration;

use futures::future::BoxFuture;
use snodra_messaging::event_store::ReplayRange;
use snodra_messaging::store::{
    AppendListener, OutboxEntry, OutboxStore, ProcessedEventStore, ProcessedMarker, StoreError, Tx,
};

use crate::db::{Conn, Db, Dialect, Row, Stmt, placeholders};
#[cfg(feature = "mysql")]
use crate::db::{ErrorKind, error_kind, store_error};
use crate::limits;

/// The relays' claim: the entries free, already the owner's or whose lease passed, in order,
/// skipping those another claim locked. At READ COMMITTED a locking read on MySQL still locks each
/// row it scans until it has evaluated the row's filter (then releases the ones that do not
/// match): for that instant a concurrent claim can skip a row it could have taken, which it then
/// takes at its next claim.
const CLAIM: &str = "SELECT id, type, event_key, CAST(headers AS {text}) AS headers, \
     CAST(payload AS {text}) AS payload, created_at, seq, attempts FROM snodra_outbox \
     WHERE sent_at IS NULL AND failed_at IS NULL AND (owner IS NULL OR owner = ? OR lease_until <= ?) \
     ORDER BY created_at, seq, id LIMIT ? FOR UPDATE SKIP LOCKED";

/// The notification of an append, in its transaction (PostgreSQL).
#[cfg(feature = "postgres")]
const NOTIFY: &str = "SELECT pg_notify('snodra_outbox', '')";

pub(crate) struct SqlOutboxStore {
    db: Arc<Db>,
    #[cfg(feature = "postgres")]
    notifications: Option<Arc<crate::notify::Notifications>>,
}

impl SqlOutboxStore {
    /// The outbox of `db`; on PostgreSQL with `notify`, appends notify and relays listen.
    pub(crate) fn new(db: Arc<Db>, notify: bool) -> SqlOutboxStore {
        #[cfg(feature = "postgres")]
        let notifications = match &db.pool {
            crate::db::Pool::Pg(pool) if notify => Some(crate::notify::Notifications::new(pool)),
            _ => None,
        };
        #[cfg(not(feature = "postgres"))]
        let _ = notify;
        SqlOutboxStore {
            db,
            #[cfg(feature = "postgres")]
            notifications,
        }
    }

    pub(crate) fn listens(&self) -> u64 {
        #[cfg(feature = "postgres")]
        if let Some(n) = &self.notifications {
            return n.listens();
        }
        0
    }

    async fn insert(&self, conn: &mut Conn<'_>, entry: &OutboxEntry) -> Result<(), StoreError> {
        limits::check("an outbox entry's id", &entry.id, limits::ID)?;
        limits::check("an outbox entry's type", &entry.type_name, limits::NAME)?;
        limits::require_object(&entry.type_name, &entry.payload)?;
        let headers = limits::headers_json("an outbox entry's headers", &entry.headers)?;
        let payload = String::from_utf8(entry.payload.clone()).map_err(|e| {
            invalid(format!(
                "the payload of event {} is not UTF-8: {e}",
                entry.type_name
            ))
        })?;
        conn.execute(
            Stmt::new(
                "INSERT INTO snodra_outbox (id, type, event_key, headers, payload, created_at, seq, attempts) \
                 VALUES (?, ?, ?, CAST(? AS {json}), CAST(? AS {json}), ?, ?, ?)",
            )
            .arg("id", &entry.id)
            .arg("type", &entry.type_name)
            .arg("key", &entry.key)
            .arg("headers", headers)
            .arg("payload", payload)
            .time("createdAt", entry.created_at_ms)
            .arg("seq", entry.seq)
            .arg("attempts", entry.attempts),
        )
        .await?;
        #[cfg(feature = "postgres")]
        if self.notifications.is_some() && conn.dialect() == Dialect::Postgres {
            conn.fetch_all(Stmt::new(NOTIFY)).await?;
        }
        Ok(())
    }

    async fn update(&self, stmt: Stmt) -> Result<u64, StoreError> {
        let mut t = self.db.begin().await?;
        let result = t.conn().execute(stmt).await;
        t.finish(result).await
    }
}

fn invalid(message: String) -> StoreError {
    StoreError::Failed(snodra_messaging::error::MessagingError::InvalidMessage(
        message,
    ))
}

fn read_entry(row: &Row) -> Result<OutboxEntry, StoreError> {
    Ok(OutboxEntry {
        id: row.text("id")?,
        type_name: row.text("type")?,
        key: row.opt_text("event_key")?,
        headers: limits::strings(row.opt_text("headers")?.as_deref())?,
        payload: row.text("payload")?.into_bytes(),
        created_at_ms: row.ms("created_at")?,
        seq: row.int("seq")?,
        attempts: row.int32("attempts")?,
    })
}

impl OutboxStore for SqlOutboxStore {
    fn append<'a>(
        &'a self,
        tx: &'a mut Tx,
        entry: OutboxEntry,
    ) -> BoxFuture<'a, Result<(), StoreError>> {
        Box::pin(async move {
            let mut scope = self.db.scope(tx).await?;
            let result = self.insert(&mut scope.conn(), &entry).await;
            scope.finish(result).await
        })
    }

    fn append_now(&self, entry: OutboxEntry) -> BoxFuture<'_, Result<(), StoreError>> {
        Box::pin(async move {
            let mut t = self.db.begin().await?;
            let result = self.insert(&mut t.conn(), &entry).await;
            t.finish(result).await
        })
    }

    fn claim<'a>(
        &'a self,
        owner: &'a str,
        lease: Duration,
        limit: usize,
    ) -> BoxFuture<'a, Result<Vec<OutboxEntry>, StoreError>> {
        Box::pin(async move {
            let mut t = self.db.own().await?;
            let result = async {
                let mut conn = t.conn();
                let now = self.db.now();
                let rows = conn
                    .fetch_all(
                        Stmt::new(CLAIM)
                            .arg("owner", owner)
                            .time("now", now)
                            .arg("limit", i64::try_from(limit).unwrap_or(i64::MAX)),
                    )
                    .await?;
                let entries = rows.iter().map(read_entry).collect::<Result<Vec<_>, _>>()?;
                if !entries.is_empty() {
                    let lease_ms = i64::try_from(lease.as_millis()).unwrap_or(i64::MAX);
                    conn.execute(
                        Stmt::built(format!(
                            "UPDATE snodra_outbox SET owner = ?, lease_until = ? WHERE id IN ({})",
                            placeholders(entries.len())
                        ))
                        .arg("owner", owner)
                        .time("until", now.saturating_add(lease_ms))
                        .args("ids", entries.iter().map(|e| e.id.as_str())),
                    )
                    .await?;
                }
                Ok(entries)
            }
            .await;
            t.finish(result).await
        })
    }

    fn mark_sent<'a>(&'a self, ids: &'a [String]) -> BoxFuture<'a, Result<(), StoreError>> {
        Box::pin(async move {
            if ids.is_empty() {
                return Ok(());
            }
            self.update(
                Stmt::built(format!(
                    "UPDATE snodra_outbox SET sent_at = ? WHERE id IN ({})",
                    placeholders(ids.len())
                ))
                .time("now", self.db.now())
                .args("ids", ids),
            )
            .await?;
            Ok(())
        })
    }

    fn mark_attempt_failed<'a>(
        &'a self,
        id: &'a str,
        error: &'a str,
    ) -> BoxFuture<'a, Result<(), StoreError>> {
        Box::pin(async move {
            self.update(
                Stmt::new(
                    "UPDATE snodra_outbox SET attempts = attempts + 1, last_error = ? WHERE id = ?",
                )
                .arg("error", limits::cut(error))
                .arg("id", id),
            )
            .await?;
            Ok(())
        })
    }

    fn mark_failed<'a>(
        &'a self,
        id: &'a str,
        error: &'a str,
    ) -> BoxFuture<'a, Result<(), StoreError>> {
        Box::pin(async move {
            self.update(
                Stmt::new(
                    "UPDATE snodra_outbox SET attempts = attempts + 1, last_error = ?, failed_at = ? WHERE id = ?",
                )
                .arg("error", limits::cut(error))
                .time("now", self.db.now())
                .arg("id", id),
            )
            .await?;
            Ok(())
        })
    }

    fn purge(&self, sent_before_ms: i64) -> BoxFuture<'_, Result<u64, StoreError>> {
        Box::pin(crate::purge::batched(
            &self.db,
            "snodra_outbox",
            "id",
            "sent_at < ?",
            sent_before_ms,
        ))
    }

    fn pending(&self) -> BoxFuture<'_, Result<u64, StoreError>> {
        Box::pin(async move {
            let mut t = self.db.begin().await?;
            let result = async {
                let row = t
                    .conn()
                    .fetch_one(Stmt::new(
                        "SELECT COUNT(*) AS n FROM snodra_outbox WHERE sent_at IS NULL AND failed_at IS NULL",
                    ))
                    .await?;
                Ok(u64::try_from(row.int("n")?).unwrap_or(0))
            }
            .await;
            t.finish(result).await
        })
    }

    /// Registers `wake` with the PostgreSQL notifications; nothing on MySQL or with notify off
    /// (the relay polls).
    fn on_append(&self, wake: Arc<dyn Fn() + Send + Sync>) -> AppendListener {
        #[cfg(feature = "postgres")]
        if let Some(n) = &self.notifications {
            return n.register(wake);
        }
        let _ = wake;
        AppendListener::none()
    }

    fn oldest_pending(&self) -> BoxFuture<'_, Result<Option<i64>, StoreError>> {
        Box::pin(async move {
            let mut t = self.db.begin().await?;
            let result = async {
                t.conn()
                    .fetch_optional(Stmt::new(
                        "SELECT created_at FROM snodra_outbox WHERE sent_at IS NULL AND failed_at IS NULL \
                         ORDER BY created_at, seq LIMIT 1",
                    ))
                    .await?
                    .map(|row| row.ms("created_at"))
                    .transpose()
            }
            .await;
            t.finish(result).await
        })
    }
}

/// `snodra_processed_events`: one row per marker id (by its hash), written in the caller's
/// transaction. A stage-1 marker also keeps its processor, source, segment and position (and the
/// position as a number when it is a decimal one, for resets by range).
pub(crate) struct SqlProcessedEventStore {
    db: Arc<Db>,
}

impl SqlProcessedEventStore {
    pub(crate) fn new(db: Arc<Db>) -> SqlProcessedEventStore {
        SqlProcessedEventStore { db }
    }
}

/// Inserts a row unless one with its key exists; false when it existed. On PostgreSQL without an
/// error (`ON CONFLICT ... DO NOTHING`: a duplicate key would abort the caller's transaction), on
/// MySQL by the duplicate-key error (which leaves the transaction usable).
pub(crate) async fn insert_once(
    conn: &mut Conn<'_>,
    insert: &'static str,
    pg_conflict: &'static str,
    build: impl Fn(Stmt) -> Stmt,
) -> Result<bool, StoreError> {
    match conn.dialect() {
        #[cfg(feature = "postgres")]
        Dialect::Postgres => {
            let sql = format!("{insert} {pg_conflict}");
            Ok(conn.execute(build(Stmt::built(sql))).await? == 1)
        }
        #[cfg(feature = "mysql")]
        Dialect::MySql => {
            let _ = pg_conflict;
            match conn.try_execute(build(Stmt::new(insert))).await? {
                Ok(_) => Ok(true),
                Err(e) if error_kind(&e) == ErrorKind::Duplicate => Ok(false),
                Err(e) => Err(store_error(e)),
            }
        }
    }
}

/// Whether the marker `id` is recorded (by its hash).
pub(crate) async fn marker_exists(conn: &mut Conn<'_>, id: &str) -> Result<bool, StoreError> {
    let row = conn
        .fetch_one(
            Stmt::new("SELECT COUNT(*) AS n FROM snodra_processed_events WHERE id_hash = ?")
                .arg("idHash", limits::sha256(id)),
        )
        .await?;
    Ok(row.int("n")? > 0)
}

/// A bound on a position compares only positions of its kind: decimal ones by value, the others
/// by their bytes. The SQL fragment and its parameter.
pub(crate) fn position_bound(op: &str, position: &str) -> (String, crate::db::Arg) {
    match limits::position_number(position) {
        Some(n) => (format!("position_num {op} ?"), n.into()),
        None => (
            format!("position_num IS NULL AND position {op} ?"),
            position.into(),
        ),
    }
}

impl ProcessedEventStore for SqlProcessedEventStore {
    fn record<'a>(
        &'a self,
        tx: &'a mut Tx,
        marker: &'a ProcessedMarker,
    ) -> BoxFuture<'a, Result<bool, StoreError>> {
        Box::pin(async move {
            limits::check_opt(
                "a marker's processor",
                marker.processor.as_deref(),
                limits::PROCESSOR,
            )?;
            limits::check_opt(
                "a marker's source",
                marker.source.as_deref(),
                limits::SOURCE,
            )?;
            limits::check_opt(
                "a marker's segment",
                marker.segment.as_deref(),
                limits::SEGMENT,
            )?;
            limits::check_opt(
                "a marker's position",
                marker.position.as_deref(),
                limits::POSITION,
            )?;
            let mut scope = self.db.scope(tx).await?;
            let result = insert_once(
                &mut scope.conn(),
                "INSERT INTO snodra_processed_events \
                 (id_hash, id, type, processed_at, processor, source, segment, position, position_num) \
                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
                "ON CONFLICT (id_hash) DO NOTHING",
                |s| {
                    s.arg("idHash", limits::sha256(&marker.id))
                        .arg("id", &marker.id)
                        .arg("type", &marker.type_name)
                        .time("processedAt", marker.processed_at_ms)
                        .arg("processor", &marker.processor)
                        .arg("source", &marker.source)
                        .arg("segment", &marker.segment)
                        .arg("position", &marker.position)
                        .arg(
                            "positionNum",
                            marker.position.as_deref().and_then(limits::position_number),
                        )
                },
            )
            .await;
            scope.finish(result).await
        })
    }

    fn exists<'a>(&'a self, id: &'a str) -> BoxFuture<'a, Result<bool, StoreError>> {
        Box::pin(async move {
            let mut t = self.db.begin().await?;
            let result = marker_exists(&mut t.conn(), id).await;
            t.finish(result).await
        })
    }

    fn delete_markers<'a>(
        &'a self,
        processor: &'a str,
        ranges: &'a [ReplayRange],
    ) -> BoxFuture<'a, Result<u64, StoreError>> {
        Box::pin(async move {
            if ranges.is_empty() {
                return Ok(0);
            }
            let mut t = self.db.own().await?;
            let result = async {
                let mut deleted = 0;
                for range in ranges {
                    let (from, from_arg) = position_bound(">=", &range.from);
                    let (until, until_arg) = position_bound("<", &range.until);
                    deleted += t
                        .conn()
                        .execute(
                            Stmt::built(format!(
                                "DELETE FROM snodra_processed_events WHERE processor = ? AND source = ? \
                                 AND segment = ? AND {from} AND {until}"
                            ))
                            .arg("processor", processor)
                            .arg("source", &range.source)
                            .arg("segment", &range.segment)
                            .arg("from", from_arg)
                            .arg("until", until_arg),
                        )
                        .await?;
                }
                Ok(deleted)
            }
            .await;
            t.finish(result).await
        })
    }

    fn purge(&self, before_ms: i64) -> BoxFuture<'_, Result<u64, StoreError>> {
        Box::pin(crate::purge::batched(
            &self.db,
            "snodra_processed_events",
            "id_hash",
            "processed_at < ?",
            before_ms,
        ))
    }
}