mire 0.2.0

A small, generic PostgreSQL event-sourcing library: append-only event streams, aggregates with optimistic concurrency, and subscription-based projections (requires tokio + sqlx)
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//! Phase 4 (review CORE-2/SUB-1): per-stream ordering contract for
//! subscribers. See `tasks/subscriber-ordering.md`.
//!
//! Delivery order today is `(transaction_id, global_position)`; the
//! transaction id is assigned at a transaction's FIRST write. A
//! multi-statement transaction (`TransactionScope`, the saga runner) that
//! writes something unrelated before appending acquires its xid early, so a
//! stream's v2 can be ordered — and delivered — before v1. These tests pin
//! the required contract: a subscription delivers each stream's events in
//! strictly increasing `stream_version` order, holding back out-of-order
//! events (and lagging the checkpoint) until the predecessor arrives.
//!
//! Gated on `DATABASE_URL`. Note: tests share the database with the rest of
//! the suite, so deliverability can be delayed by other tests' open
//! transactions (the xmin guard); assertions therefore collect across
//! repeated polls with a timeout rather than expecting single-poll batches.

use std::time::{Duration, Instant};

use mire::{Event, EventData, EventStore, ExpectedVersion, RecordedEvent, Subscription};
use serde::{Deserialize, Serialize};
use sqlx::{PgPool, Row};
use uuid::Uuid;

mod common;

#[derive(Debug, Clone, Serialize, Deserialize)]
struct Marked {
    mark: String,
}
impl EventData for Marked {
    fn event_type(&self) -> &'static str {
        "ordering.marked"
    }
}

async fn store() -> Option<EventStore> {
    common::store().await
}

fn ev(mark: &str) -> Event<Marked> {
    Event::new(Marked {
        mark: mark.to_string(),
    })
}

/// Force xid assignment NOW on the connection, before any append — this is
/// what makes a multi-statement transaction's events sort earlier than
/// freshly-started transactions that commit before it.
async fn force_xid<'e, E>(executor: E)
where
    E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
    sqlx::query("SELECT pg_current_xact_id()")
        .execute(executor)
        .await
        .expect("force xid");
}

/// Poll until `pred` returns true over the accumulated deliveries, or panic
/// at the deadline. Returns everything delivered.
async fn collect_until(
    sub: &mut Subscription,
    category: &str,
    deadline: Duration,
    pred: impl Fn(&[RecordedEvent]) -> bool,
) -> Vec<RecordedEvent> {
    let start = Instant::now();
    let mut got: Vec<RecordedEvent> = Vec::new();
    loop {
        got.extend(sub.poll_category(category).await.expect("poll"));
        if pred(&got) {
            return got;
        }
        assert!(
            start.elapsed() < deadline,
            "timed out waiting for deliveries; got {:?}",
            got.iter()
                .map(|e| (e.stream_id.clone(), e.stream_version))
                .collect::<Vec<_>>()
        );
        tokio::time::sleep(Duration::from_millis(25)).await;
    }
}

/// Assert each stream's versions appear in strictly increasing order — the
/// contract under test.
fn assert_per_stream_order(events: &[RecordedEvent]) {
    let mut last: std::collections::HashMap<&str, i64> = std::collections::HashMap::new();
    for e in events {
        if let Some(prev) = last.insert(e.stream_id.as_str(), e.stream_version) {
            assert!(
                e.stream_version > prev,
                "per-stream ordering violated on {}: delivered v{} after v{}",
                e.stream_id,
                e.stream_version,
                prev
            );
        }
    }
}

/// The cursor `(transaction_id, global_position)` of one event, read back
/// from the log, for checkpoint-lag assertions.
async fn cursor_of(pool: &PgPool, stream_id: &str, version: i64) -> (u64, i64) {
    let row = sqlx::query(
        "SELECT transaction_id::text AS txid, global_position
         FROM es_events WHERE stream_id = $1 AND stream_version = $2",
    )
    .bind(stream_id)
    .bind(version)
    .fetch_one(pool)
    .await
    .expect("event exists");
    let txid: String = row.get("txid");
    (txid.parse().unwrap(), row.get("global_position"))
}

async fn subscription_cursor(pool: &PgPool, id: &str) -> (u64, i64) {
    let row = sqlx::query(
        "SELECT last_transaction_id::text AS txid, last_position
         FROM es_subscriptions WHERE subscription_id = $1",
    )
    .bind(id)
    .fetch_one(pool)
    .await
    .expect("subscription row");
    let txid: String = row.get("txid");
    (txid.parse().unwrap(), row.get("last_position"))
}

/// §6 test 1+3 schedule: tx A assigns its xid early; tx B then creates the
/// stream at v1 and commits; tx A appends v2 and commits. In `(txid, pos)`
/// order v2 (A's old xid) precedes v1 (B's newer xid).
async fn inverted_pair(store: &EventStore, category: &str, stream: &str) {
    let mut tx_a = store.begin_transaction().await.expect("begin A");
    force_xid(&mut **tx_a.tx()).await;

    // tx B: independent quick append, creates v1, commits first.
    store
        .append(stream, category, ExpectedVersion::NoStream, &[ev("v1")])
        .await
        .expect("append v1");

    // tx A: appends v2 with the early xid, commits second.
    tx_a.append(stream, category, ExpectedVersion::Exact(1), &[ev("v2")])
        .await
        .expect("append v2");
    tx_a.commit().await.expect("commit A");
}

#[tokio::test]
async fn same_batch_inversion_is_reordered() {
    let Some(store) = store().await else {
        eprintln!("skipping: DATABASE_URL not set");
        return;
    };
    let suffix = Uuid::new_v4().simple().to_string();
    let category = format!("ordsb{suffix}");
    let stream = format!("{category}-s1");

    let mut sub = Subscription::create(
        store.clone(),
        store.pool().clone(),
        format!("ord-sb-{suffix}"),
        100,
    )
    .await
    .expect("create sub");

    inverted_pair(&store, &category, &stream).await;

    let got = collect_until(&mut sub, &category, Duration::from_secs(10), |g| {
        g.len() >= 2
    })
    .await;
    assert_eq!(got.len(), 2);
    assert_per_stream_order(&got);
    assert_eq!(
        (got[0].stream_version, got[1].stream_version),
        (1, 2),
        "v1 must be delivered before v2 even though v2 sorts first by xid"
    );
}

#[tokio::test]
async fn limit_cut_does_not_split_inversion() {
    let Some(store) = store().await else {
        eprintln!("skipping: DATABASE_URL not set");
        return;
    };
    let suffix = Uuid::new_v4().simple().to_string();
    let category = format!("ordlc{suffix}");
    let stream = format!("{category}-s1");

    // batch_size = 1 forces the inversion pair across separate polls.
    let mut sub = Subscription::create(
        store.clone(),
        store.pool().clone(),
        format!("ord-lc-{suffix}"),
        1,
    )
    .await
    .expect("create sub");

    inverted_pair(&store, &category, &stream).await;

    let got = collect_until(&mut sub, &category, Duration::from_secs(10), |g| {
        g.len() >= 2
    })
    .await;
    assert_per_stream_order(&got);
    assert_eq!((got[0].stream_version, got[1].stream_version), (1, 2));
}

#[tokio::test]
async fn cross_poll_inversion_is_held_back() {
    let Some(store) = store().await else {
        eprintln!("skipping: DATABASE_URL not set");
        return;
    };
    let suffix = Uuid::new_v4().simple().to_string();
    let category = format!("ordcp{suffix}");
    let stream = format!("{category}-s1");

    let mut sub = Subscription::create(
        store.clone(),
        store.pool().clone(),
        format!("ord-cp-{suffix}"),
        100,
    )
    .await
    .expect("create sub");

    // xid order must be: A < C(pin) < B. While C is open, the xmin guard
    // admits v2 (A) but not v1 (B) — the window where today's code delivers
    // v2 first and corrupts last-write-wins read models.
    let mut tx_a = store.begin_transaction().await.expect("begin A");
    force_xid(&mut **tx_a.tx()).await;

    let mut pin = store.pool().begin().await.expect("begin pin");
    force_xid(&mut *pin).await;

    store
        .append(&stream, &category, ExpectedVersion::NoStream, &[ev("v1")])
        .await
        .expect("append v1");
    tx_a.append(&stream, &category, ExpectedVersion::Exact(1), &[ev("v2")])
        .await
        .expect("append v2");
    tx_a.commit().await.expect("commit A");

    // While the pin holds the xmin window open, v2 must NOT be delivered
    // (its predecessor v1 is not deliverable yet).
    let probe_deadline = Instant::now() + Duration::from_millis(800);
    while Instant::now() < probe_deadline {
        let got = sub.poll_category(&category).await.expect("poll");
        assert!(
            got.is_empty(),
            "nothing from this stream may be delivered while v1 is blocked; got {:?}",
            got.iter().map(|e| e.stream_version).collect::<Vec<_>>()
        );
        tokio::time::sleep(Duration::from_millis(25)).await;
    }

    pin.rollback().await.expect("release pin");

    let got = collect_until(&mut sub, &category, Duration::from_secs(10), |g| {
        g.len() >= 2
    })
    .await;
    assert_per_stream_order(&got);
    assert_eq!((got[0].stream_version, got[1].stream_version), (1, 2));
}

#[tokio::test]
async fn unrelated_streams_flow_during_holdback_and_checkpoint_lags() {
    let Some(store) = store().await else {
        eprintln!("skipping: DATABASE_URL not set");
        return;
    };
    let suffix = Uuid::new_v4().simple().to_string();
    let category = format!("ordfl{suffix}");
    let stream_s = format!("{category}-s");
    let stream_t = format!("{category}-t");
    let sub_id = format!("ord-fl-{suffix}");

    let mut sub = Subscription::create(store.clone(), store.pool().clone(), sub_id.clone(), 100)
        .await
        .expect("create sub");

    // xid order: A < D < C(pin) < B. While C is open: S's v2 (A) is gated on
    // the undeliverable v1 (B) and must be held; T's t1 (D) has no
    // predecessor and must keep flowing.
    let mut tx_a = store.begin_transaction().await.expect("begin A");
    force_xid(&mut **tx_a.tx()).await;
    let mut tx_d = store.begin_transaction().await.expect("begin D");
    force_xid(&mut **tx_d.tx()).await;
    let mut pin = store.pool().begin().await.expect("begin pin");
    force_xid(&mut *pin).await;

    store
        .append(&stream_s, &category, ExpectedVersion::NoStream, &[ev("v1")])
        .await
        .expect("append v1");
    tx_a.append(&stream_s, &category, ExpectedVersion::Exact(1), &[ev("v2")])
        .await
        .expect("append v2");
    tx_a.commit().await.expect("commit A");
    tx_d.append(&stream_t, &category, ExpectedVersion::NoStream, &[ev("t1")])
        .await
        .expect("append t1");
    tx_d.commit().await.expect("commit D");

    // T flows while S is held.
    let got = collect_until(&mut sub, &category, Duration::from_secs(10), |g| {
        g.iter().any(|e| e.stream_id == stream_t)
    })
    .await;
    assert!(
        got.iter().all(|e| e.stream_id != stream_s),
        "S's v2 must be held while its v1 is undeliverable; delivered {:?}",
        got.iter()
            .map(|e| (e.stream_id.clone(), e.stream_version))
            .collect::<Vec<_>>()
    );

    // The persisted checkpoint must not pass the held event: a restart from
    // it has to re-present v2.
    sub.checkpoint().await.expect("checkpoint");
    let v2_cursor = cursor_of(store.pool(), &stream_s, 2).await;
    let cp = subscription_cursor(store.pool(), &sub_id).await;
    assert!(
        cp < v2_cursor,
        "checkpoint {cp:?} must lag behind the held event at {v2_cursor:?}"
    );

    pin.rollback().await.expect("release pin");

    // A fresh subscription (restart from the persisted checkpoint) must see
    // S's events in order; t1 may legitimately be redelivered (at-least-once).
    let mut sub2 = Subscription::create(store.clone(), store.pool().clone(), sub_id.clone(), 100)
        .await
        .expect("recreate sub");
    let got = collect_until(&mut sub2, &category, Duration::from_secs(10), |g| {
        g.iter()
            .filter(|e| e.stream_id == stream_s)
            .map(|e| e.stream_version)
            .collect::<Vec<_>>()
            == vec![1, 2]
    })
    .await;
    assert_per_stream_order(&got);
}

#[tokio::test]
async fn legacy_inverted_checkpoint_trips_backstop_loudly() {
    // A checkpoint written by pre-fix code could have passed v2 while v1 was
    // still undeliverable (the inversion this phase fixes). Replaying from
    // such a checkpoint presents v1 AFTER v2 counts as delivered — the
    // read model may already hold v2's state, so applying v1 would regress
    // it. The always-on backstop (D-ORD-3) must refuse loudly instead of
    // handing v1 to a handler.
    let Some(store) = store().await else {
        eprintln!("skipping: DATABASE_URL not set");
        return;
    };
    let suffix = Uuid::new_v4().simple().to_string();
    let category = format!("ordbs{suffix}");
    let stream = format!("{category}-s1");
    let sub_id = format!("ord-bs-{suffix}");

    inverted_pair(&store, &category, &stream).await;

    // Forge the legacy state: a persisted checkpoint sitting exactly on v2
    // (which sorts BEFORE v1 in (txid, pos) order), claiming v2 handled
    // while v1 was never delivered.
    let (v2_txid, v2_pos) = cursor_of(store.pool(), &stream, 2).await;
    sqlx::query(
        "INSERT INTO es_subscriptions (subscription_id, last_position, last_transaction_id)
         VALUES ($1, $2, $3::text::xid8)",
    )
    .bind(&sub_id)
    .bind(v2_pos)
    .bind(v2_txid.to_string())
    .execute(store.pool())
    .await
    .expect("forge legacy checkpoint");

    let mut sub = Subscription::create(store.clone(), store.pool().clone(), sub_id.clone(), 100)
        .await
        .expect("create sub");

    // v1 (beyond the forged checkpoint) eventually becomes deliverable and
    // must trip the backstop rather than be delivered.
    let start = Instant::now();
    loop {
        match sub.poll_category(&category).await {
            Ok(got) if got.is_empty() => {
                assert!(
                    start.elapsed() < Duration::from_secs(10),
                    "timed out waiting for the backstop to trip"
                );
                tokio::time::sleep(Duration::from_millis(25)).await;
            }
            Ok(got) => panic!(
                "v1 must not be delivered after a checkpoint that passed v2; got {:?}",
                got.iter().map(|e| e.stream_version).collect::<Vec<_>>()
            ),
            Err(mire::EventStoreError::OrderingViolation {
                stream_id,
                version,
                last_delivered,
                ..
            }) => {
                assert_eq!(stream_id, stream);
                assert_eq!(version, 1);
                assert_eq!(last_delivered, 2);
                break;
            }
            Err(e) => panic!("unexpected error: {e}"),
        }
    }
}

#[tokio::test]
async fn chaos_concurrent_scopes_never_regress_per_stream_order() {
    // §6 test 5: N writer tasks (one per stream, so OCC never conflicts),
    // each alternating quick appends with TransactionScope appends that
    // force an early xid and then dawdle — the exact recipe for xid/version
    // inversions — while a pin task cycles long-enough transactions to keep
    // the xmin horizon jagged. The consumer must still see every stream's
    // versions as exactly 1..=N in order.
    let Some(store) = store().await else {
        eprintln!("skipping: DATABASE_URL not set");
        return;
    };
    let suffix = Uuid::new_v4().simple().to_string();
    let category = format!("ordch{suffix}");
    const STREAMS: usize = 4;
    const EVENTS_PER_STREAM: i64 = 20;

    let mut sub = Subscription::create(
        store.clone(),
        store.pool().clone(),
        format!("ord-ch-{suffix}"),
        7, // small batches: force LIMIT cuts through inversion cohorts
    )
    .await
    .expect("create sub");

    let pin_stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
    let pin_task = {
        let pool = store.pool().clone();
        let stop = pin_stop.clone();
        tokio::spawn(async move {
            while !stop.load(std::sync::atomic::Ordering::Relaxed) {
                let mut pin = pool.begin().await.expect("pin begin");
                force_xid(&mut *pin).await;
                tokio::time::sleep(Duration::from_millis(15)).await;
                pin.rollback().await.expect("pin rollback");
            }
        })
    };

    let writers: Vec<_> = (0..STREAMS)
        .map(|s| {
            let store = store.clone();
            let category = category.clone();
            tokio::spawn(async move {
                let stream = format!("{category}-s{s}");
                for v in 1..=EVENTS_PER_STREAM {
                    let expected = if v == 1 {
                        ExpectedVersion::NoStream
                    } else {
                        ExpectedVersion::Exact(v - 1)
                    };
                    if v % 2 == 0 {
                        // Multi-statement: early xid, then dawdle before the
                        // append so other writers' xids overtake ours.
                        let mut scope = store.begin_transaction().await.expect("begin");
                        force_xid(&mut **scope.tx()).await;
                        tokio::time::sleep(Duration::from_millis(3 + (v as u64 % 7))).await;
                        scope
                            .append(&stream, &category, expected, &[ev(&format!("v{v}"))])
                            .await
                            .expect("scoped append");
                        scope.commit().await.expect("commit");
                    } else {
                        store
                            .append(&stream, &category, expected, &[ev(&format!("v{v}"))])
                            .await
                            .expect("append");
                    }
                }
            })
        })
        .collect();
    for w in writers {
        w.await.expect("writer");
    }
    pin_stop.store(true, std::sync::atomic::Ordering::Relaxed);
    pin_task.await.expect("pin task");

    let total = STREAMS as i64 * EVENTS_PER_STREAM;
    let got = collect_until(&mut sub, &category, Duration::from_secs(30), |g| {
        g.len() as i64 >= total
    })
    .await;
    assert_eq!(got.len() as i64, total);
    assert_per_stream_order(&got);
    for s in 0..STREAMS {
        let stream = format!("{category}-s{s}");
        let versions: Vec<i64> = got
            .iter()
            .filter(|e| e.stream_id == stream)
            .map(|e| e.stream_version)
            .collect();
        assert_eq!(
            versions,
            (1..=EVENTS_PER_STREAM).collect::<Vec<i64>>(),
            "stream {stream} must arrive complete and in order"
        );
    }
}

#[tokio::test]
async fn restart_replay_does_not_trip_backstop() {
    // At-least-once replay (reset to an older cursor) legitimately
    // re-presents delivered events; the ordering backstop must reset with
    // the cursor instead of misreading the replay as a regression.
    let Some(store) = store().await else {
        eprintln!("skipping: DATABASE_URL not set");
        return;
    };
    let suffix = Uuid::new_v4().simple().to_string();
    let category = format!("ordrs{suffix}");
    let stream = format!("{category}-s1");

    let mut sub = Subscription::create(
        store.clone(),
        store.pool().clone(),
        format!("ord-rs-{suffix}"),
        100,
    )
    .await
    .expect("create sub");

    store
        .append(&stream, &category, ExpectedVersion::NoStream, &[ev("v1")])
        .await
        .expect("append v1");
    store
        .append(&stream, &category, ExpectedVersion::Exact(1), &[ev("v2")])
        .await
        .expect("append v2");

    let got = collect_until(&mut sub, &category, Duration::from_secs(10), |g| {
        g.len() >= 2
    })
    .await;
    assert_per_stream_order(&got);

    sub.reset().await.expect("reset");
    let replay = collect_until(&mut sub, &category, Duration::from_secs(10), |g| {
        g.len() >= 2
    })
    .await;
    assert_per_stream_order(&replay);
    assert_eq!(
        replay
            .iter()
            .map(|e| e.stream_version)
            .collect::<Vec<i64>>(),
        vec![1, 2]
    );
}