obix 0.12.3

Implementation of outbox backed by PG / 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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
use futures::{FutureExt, StreamExt};
use serde::{Serialize, de::DeserializeOwned};
use tokio::sync::{broadcast, mpsc};

use std::collections::HashMap;
use std::sync::{
    Arc,
    atomic::{AtomicU64, Ordering},
};

use super::cache::PersistentOutboxEventCache;
use super::listener::PersistentOutboxListener;
use crate::{
    handle::{OwnedTaskHandle, spawn_supervised},
    out::event::*,
    out::lane::{CommitOrder, InsertOrder, LaneHandle},
    sequence::{CommitGroupId, CommitSequence, EventSequence},
    tables::{CommitCheckpoint, MailboxTables, PersistentEventRows},
};

/// How long a failed group fetch waits before retrying the same group.
const FETCH_RETRY_INTERVAL: std::time::Duration = std::time::Duration::from_millis(250);

/// How far this process's fold has got. Both values are in-process: the lane is
/// computed, so every `Enabled` process derives the same numbering on its own.
#[derive(Clone)]
pub(crate) struct SequencerPositions {
    fold_position: Arc<AtomicU64>,
    commit_head: Arc<AtomicU64>,
}

impl SequencerPositions {
    /// The highest insert sequence this fold has passed: emitted, or skipped.
    pub(crate) fn fold_position(&self) -> EventSequence {
        EventSequence::from(self.fold_position.load(Ordering::Acquire))
    }

    /// The highest position this process's fold has emitted.
    pub(crate) fn commit_head(&self) -> CommitSequence {
        CommitSequence::from(self.commit_head.load(Ordering::Acquire))
    }
}

/// What a commit-ordered listener needs from the sequencer: the lane's
/// fan-out and its head.
pub(crate) struct SequencerHandle<P>
where
    P: Serialize + DeserializeOwned + Send + Sync + 'static,
{
    commit_head: Arc<AtomicU64>,
    commit_sender: broadcast::Sender<CommitTransport<P>>,
    backfill_request: mpsc::UnboundedSender<(CommitSequence, mpsc::Sender<CommitTransport<P>>)>,
    backfill_buffer_size: usize,
    positions: SequencerPositions,
    _task: OwnedTaskHandle,
}

type CommitTransport<P> = Transport<CommitOrder, P>;

impl<P> std::fmt::Debug for SequencerHandle<P>
where
    P: Serialize + DeserializeOwned + Send + Sync + 'static,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SequencerHandle")
            .field("commit_head", &self.commit_head.load(Ordering::Relaxed))
            .finish_non_exhaustive()
    }
}

impl<P> SequencerHandle<P>
where
    P: Serialize + DeserializeOwned + Send + Sync + 'static,
{
    pub(crate) fn positions(&self) -> SequencerPositions {
        self.positions.clone()
    }

    pub(crate) fn handle(&self) -> LaneHandle<CommitOrder, P> {
        LaneHandle::new(
            self.commit_head.clone(),
            self.commit_sender.subscribe(),
            self.backfill_request.clone(),
            self.backfill_buffer_size,
        )
    }
}

/// Where a fold's output goes.
enum Sink<P>
where
    P: Serialize + DeserializeOwned + Send + Sync + 'static,
{
    Live(broadcast::Sender<CommitTransport<P>>),
    /// One backfill request: positions in `(after, until]`, then the
    /// consumer's listener takes over from the broadcast.
    Backfill {
        sender: mpsc::Sender<CommitTransport<P>>,
        after: CommitSequence,
        until: CommitSequence,
    },
}

impl<P> Sink<P>
where
    P: Serialize + DeserializeOwned + Send + Sync + 'static,
{
    /// Emit one positioned delivery. `false` means the fold should stop.
    async fn emit(&mut self, delivery: CommitTransport<P>) -> bool {
        match self {
            Self::Live(sender) => {
                let _ = sender.send(delivery);
                true
            }
            Self::Backfill { sender, after, .. } => {
                if delivery.position() <= *after {
                    return true;
                }
                sender.send(delivery).await.is_ok()
            }
        }
    }

    fn finished(&self, head: CommitSequence) -> bool {
        match self {
            Self::Live(_) => false,
            Self::Backfill { until, .. } => head >= *until,
        }
    }
}

/// Folds the insert-ordered stream into commit order: a group is emitted whole
/// at first sight of its lowest member, its members in `sequence` order, and a
/// position is a running count of rows emitted. Pure function of the events
/// table; only sparse [`CommitCheckpoint`]s of the fold are persisted.
struct Sequencer<P, Tables>
where
    P: Serialize + DeserializeOwned + Send + Sync + 'static,
{
    pool: sqlx::PgPool,
    /// Rows emitted so far — the next group is numbered from `head + 1`.
    head: CommitSequence,
    /// Published copy of `head`, for the fence and for listeners.
    commit_head: Option<Arc<AtomicU64>>,
    fold_position: Option<Arc<AtomicU64>>,
    sink: Sink<P>,
    /// Emitted groups with members the fold has not reached yet, by highest
    /// member. Seeded from a checkpoint so a resumed fold cannot re-emit them.
    seen: HashMap<CommitGroupId, EventSequence>,
    checkpoint_every: usize,
    checkpoint_interval: std::time::Duration,
    groups_since_checkpoint: usize,
    last_checkpoint: tokio::time::Instant,
    _phantom: std::marker::PhantomData<Tables>,
}

impl<P, Tables> Sequencer<P, Tables>
where
    P: Serialize + DeserializeOwned + Send + Sync + 'static,
    Tables: MailboxTables,
{
    /// Fold a drained batch of insert-lane deliveries, fetching every
    /// first-sighted group's members in one statement. `false` means stop.
    async fn fold_batch(&mut self, batch: Vec<Transport<InsertOrder, P>>) -> bool {
        let mut members = self.fetch_members(&batch).await;
        for delivery in batch {
            let sequence = delivery.sequence();
            if !self.place(delivery, &mut members).await {
                return false;
            }
            // INVARIANT: publish only after this delivery is accounted for —
            // its group emitted and the head advanced, or provably never. The
            // fence reads this then the head, so publishing earlier lets it
            // return with the frontier event still undelivered. It must also
            // advance on every early return in `place`, or the fence stalls.
            if let Some(position) = &self.fold_position {
                position.store(u64::from(sequence), Ordering::Release);
            }
            if self.sink.finished(self.head) {
                return false;
            }
        }
        true
    }

    /// One statement for every group first sighted in `batch`. The floor is the
    /// lowest first sight: no group's members sit below their own MIN.
    async fn fetch_members(
        &self,
        batch: &[Transport<InsertOrder, P>],
    ) -> HashMap<CommitGroupId, PersistentEventRows<P>> {
        let mut wanted: Vec<CommitGroupId> = Vec::new();
        let mut floor: Option<EventSequence> = None;
        for delivery in batch {
            if !delivery.has_payload() {
                continue;
            }
            let group = delivery.commit_group();
            if self.seen.contains_key(&group) || wanted.contains(&group) {
                continue;
            }
            wanted.push(group);
            floor.get_or_insert(delivery.sequence());
        }
        let Some(floor) = floor else {
            return HashMap::new();
        };

        // Retried, never skipped: giving up would drop these groups from the
        // lane, and a retry re-reads the same rows for the same numbering.
        loop {
            match Tables::load_group_members::<P>(&self.pool, &wanted, floor).await {
                Ok(groups) => return groups.into_iter().collect(),
                Err(error) => {
                    record_fetch_failed(&error, u64::from(floor));
                    tokio::time::sleep(FETCH_RETRY_INTERVAL).await;
                }
            }
        }
    }

    /// Emit one delivery's group on first sight, or establish it needs none.
    async fn place(
        &mut self,
        delivery: Transport<InsertOrder, P>,
        members: &mut HashMap<CommitGroupId, PersistentEventRows<P>>,
    ) -> bool {
        if !delivery.has_payload() {
            return true;
        }
        let sequence = delivery.sequence();
        let group = delivery.commit_group();
        if let Some(group_max) = self.seen.get(&group).copied() {
            if sequence >= group_max {
                self.seen.remove(&group);
            }
            return true;
        }

        // INVARIANT: a group's members all commit in one transaction, so once
        // any one is delivered the rest are committed too. An empty result is
        // therefore a transient visibility miss, never the true membership —
        // retried like a fetch error, since accepting it would drop the group.
        let rows = match members.remove(&group).filter(|rows| !rows.is_empty()) {
            Some(rows) => rows,
            None => self.fetch_group(group, sequence).await,
        };

        let group_max = rows
            .iter()
            .map(|row| match row {
                Ok(event) => event.sequence,
                Err(error) => error.sequence,
            })
            .max()
            .unwrap_or(sequence);
        if group_max > sequence {
            self.seen.insert(group, group_max);
        }

        let last = rows.len() - 1;
        for (index, row) in rows.into_iter().enumerate() {
            self.head = self.head.next();
            let delivery = Delivery::new(self.head, index == last, PersistentDelivery::from(row));
            if !self.sink.emit(delivery).await {
                return false;
            }
        }
        if let Some(head) = &self.commit_head {
            head.store(u64::from(self.head), Ordering::Release);
        }

        self.groups_since_checkpoint += 1;
        if self.groups_since_checkpoint >= self.checkpoint_every
            || self.last_checkpoint.elapsed() >= self.checkpoint_interval
        {
            self.checkpoint(sequence);
        }
        true
    }

    /// Fetch one group's membership, retrying until it is non-empty.
    async fn fetch_group(
        &self,
        group: CommitGroupId,
        floor: EventSequence,
    ) -> PersistentEventRows<P> {
        loop {
            match Tables::load_group_members::<P>(&self.pool, &[group], floor).await {
                Ok(groups) => {
                    if let Some(rows) = groups
                        .into_iter()
                        .find(|(id, _)| *id == group)
                        .map(|(_, rows)| rows)
                        .filter(|rows| !rows.is_empty())
                    {
                        return rows;
                    }
                    record_empty_group_fetch(u64::from(floor), i64::from(group));
                }
                Err(error) => record_fetch_failed(&error, u64::from(floor)),
            }
            tokio::time::sleep(FETCH_RETRY_INTERVAL).await;
        }
    }

    /// Record where the fold is. Fire-and-forget: a lost write only costs a
    /// longer resume next time.
    fn checkpoint(&mut self, sequence: EventSequence) {
        self.groups_since_checkpoint = 0;
        self.last_checkpoint = tokio::time::Instant::now();
        if !matches!(self.sink, Sink::Live(_)) {
            return;
        }
        let checkpoint = CommitCheckpoint {
            sequence,
            commit_seq: self.head,
            open_groups: self.seen.iter().map(|(g, max)| (*g, *max)).collect(),
        };
        let pool = self.pool.clone();
        tokio::spawn(async move {
            if let Err(error) = Tables::write_commit_checkpoint(&pool, &checkpoint).await {
                record_checkpoint_failed(&error, u64::from(checkpoint.sequence));
            }
        });
    }
}

struct BackfillSource<P, Tables>
where
    P: Serialize + DeserializeOwned + Send + Sync + 'static,
{
    pool: sqlx::PgPool,
    cache: Arc<PersistentOutboxEventCache<P, Tables>>,
    page: usize,
    checkpoint_every: usize,
    checkpoint_interval: std::time::Duration,
}

impl<P, Tables> Clone for BackfillSource<P, Tables>
where
    P: Serialize + DeserializeOwned + Send + Sync + 'static,
{
    fn clone(&self) -> Self {
        Self {
            pool: self.pool.clone(),
            cache: self.cache.clone(),
            page: self.page,
            checkpoint_every: self.checkpoint_every,
            checkpoint_interval: self.checkpoint_interval,
        }
    }
}

/// Serve one commit-lane backfill by re-folding from the nearest checkpoint at
/// or below `after`, which reproduces the same numbering.
async fn serve_commit_backfill<P, Tables>(
    source: BackfillSource<P, Tables>,
    after: CommitSequence,
    until: CommitSequence,
    sender: mpsc::Sender<CommitTransport<P>>,
) where
    P: Serialize + DeserializeOwned + Send + Sync + 'static + Unpin,
    Tables: MailboxTables,
{
    let BackfillSource {
        pool,
        cache,
        page,
        checkpoint_every,
        checkpoint_interval,
    } = source;
    let seed = match Tables::load_commit_checkpoint_for(&pool, after).await {
        Ok(seed) => seed,
        Err(error) => {
            record_checkpoint_read_failed(&error, u64::from(after));
            return;
        }
    };
    let (from, head, seen) = match seed {
        Some(checkpoint) => (
            checkpoint.sequence,
            checkpoint.commit_seq,
            checkpoint.open_groups.into_iter().collect(),
        ),
        None => (EventSequence::BEGIN, CommitSequence::BEGIN, HashMap::new()),
    };
    record_backfill_started(u64::from(after), u64::from(from), u64::from(head));

    let mut listener = PersistentOutboxListener::transport_stream(cache.handle(), Some(from), page);
    let mut sequencer = Sequencer::<P, Tables> {
        pool,
        head,
        commit_head: None,
        fold_position: None,
        sink: Sink::Backfill {
            sender,
            after,
            until,
        },
        seen,
        checkpoint_every,
        checkpoint_interval,
        groups_since_checkpoint: 0,
        last_checkpoint: tokio::time::Instant::now(),
        _phantom: std::marker::PhantomData,
    };

    loop {
        let Some(batch) = next_batch(&mut listener, page).await else {
            return;
        };
        if !sequencer.fold_batch(batch).await {
            return;
        }
    }
}

/// Wait for one delivery, then take whatever else is already buffered, up to
/// `page`. `None` means the stream ended.
async fn next_batch<P, S>(listener: &mut S, page: usize) -> Option<Vec<Transport<InsertOrder, P>>>
where
    P: Serialize + DeserializeOwned + Send + Sync + 'static,
    S: StreamExt<Item = Transport<InsertOrder, P>> + Unpin,
{
    let first = listener.next().await?;
    let mut batch = Vec::with_capacity(page);
    batch.push(first);
    while batch.len() < page {
        match listener.next().now_or_never() {
            Some(Some(delivery)) => batch.push(delivery),
            Some(None) => break,
            None => break,
        }
    }
    Some(batch)
}

/// Start this process's sequencer, resuming from the newest checkpoint or from
/// the beginning of the stream when there is none.
pub(crate) async fn spawn<P, Tables>(
    pool: &sqlx::PgPool,
    cache: Arc<PersistentOutboxEventCache<P, Tables>>,
    buffer_size: usize,
    page: usize,
    checkpoint_every: usize,
    checkpoint_interval: std::time::Duration,
) -> Result<SequencerHandle<P>, sqlx::Error>
where
    P: Serialize + DeserializeOwned + Send + Sync + 'static + Unpin,
    Tables: MailboxTables,
{
    let page = page.max(1);
    let frontier = Tables::highest_known_persistent_sequence(pool).await?;
    let seed = Tables::load_commit_checkpoint(pool, frontier).await?;
    let (from, head, seen): (_, _, HashMap<_, _>) = match seed {
        Some(checkpoint) => (
            checkpoint.sequence,
            checkpoint.commit_seq,
            checkpoint.open_groups.into_iter().collect(),
        ),
        None => (EventSequence::BEGIN, CommitSequence::BEGIN, HashMap::new()),
    };
    record_started(
        u64::from(from),
        u64::from(frontier),
        u64::from(frontier).saturating_sub(u64::from(from)),
    );

    let (commit_sender, _) = broadcast::channel(buffer_size);
    let commit_head = Arc::new(AtomicU64::new(u64::from(head)));
    let fold_position = Arc::new(AtomicU64::new(u64::from(from)));
    let (backfill_request, mut backfill_rx) = mpsc::unbounded_channel();
    let backfill_source = BackfillSource {
        pool: pool.clone(),
        cache: cache.clone(),
        page,
        checkpoint_every: checkpoint_every.max(1),
        checkpoint_interval,
    };
    let backfill_head = commit_head.clone();

    let mut listener =
        PersistentOutboxListener::transport_stream(cache.handle(), Some(from), buffer_size);
    let mut sequencer = Sequencer::<P, Tables> {
        pool: pool.clone(),
        head,
        commit_head: Some(commit_head.clone()),
        fold_position: Some(fold_position.clone()),
        sink: Sink::Live(commit_sender.clone()),
        seen,
        checkpoint_every: checkpoint_every.max(1),
        checkpoint_interval,
        groups_since_checkpoint: 0,
        last_checkpoint: tokio::time::Instant::now(),
        _phantom: std::marker::PhantomData,
    };

    let task = spawn_supervised("obix::commit_sequencer", async move {
        loop {
            tokio::select! {
                request = backfill_rx.recv() => {
                    match request {
                        Some((after, sender)) => {
                            // Sampled here, not in the task: the hand-off point
                            // is the head as of the request, so an advancing
                            // fold cannot outrun the backfill forever.
                            let until = CommitSequence::from(
                                backfill_head.load(Ordering::Acquire),
                            );
                            tokio::spawn(serve_commit_backfill::<P, Tables>(
                                backfill_source.clone(),
                                after,
                                until,
                                sender,
                            ));
                        }
                        None => break,
                    }
                }

                batch = next_batch(&mut listener, page) => {
                    match batch {
                        Some(batch) => {
                            if !sequencer.fold_batch(batch).await {
                                break;
                            }
                        }
                        None => {
                            record_stream_closed();
                            break;
                        }
                    }
                }
            }
        }
    });

    Ok(SequencerHandle {
        commit_head: commit_head.clone(),
        commit_sender,
        backfill_request,
        backfill_buffer_size: page,
        positions: SequencerPositions {
            fold_position,
            commit_head,
        },
        _task: OwnedTaskHandle::new(task),
    })
}

#[tracing::instrument(
    name = "obix.sequencer.started",
    level = "info",
    skip_all,
    fields(from_sequence = from_sequence, insert_frontier = insert_frontier, behind = behind),
)]
fn record_started(from_sequence: u64, insert_frontier: u64, behind: u64) {}

#[tracing::instrument(
    name = "obix.sequencer.backfill_started",
    level = "info",
    skip_all,
    fields(after = after, from_sequence = from_sequence, from_commit_seq = from_commit_seq),
)]
fn record_backfill_started(after: u64, from_sequence: u64, from_commit_seq: u64) {}

#[tracing::instrument(
    name = "obix.sequencer.fetch_failed",
    level = "warn",
    skip_all,
    fields(error = %error, floor = floor),
)]
fn record_fetch_failed(error: &sqlx::Error, floor: u64) {}

/// Should never fire: see the INVARIANT in `Sequencer::place`.
#[tracing::instrument(
    name = "obix.sequencer.empty_group_fetch",
    level = "warn",
    fields(floor = floor, group = group),
)]
fn record_empty_group_fetch(floor: u64, group: i64) {}

#[tracing::instrument(
    name = "obix.sequencer.checkpoint_failed",
    level = "warn",
    skip_all,
    fields(error = %error, sequence = sequence),
)]
fn record_checkpoint_failed(error: &sqlx::Error, sequence: u64) {}

#[tracing::instrument(
    name = "obix.sequencer.checkpoint_read_failed",
    level = "warn",
    skip_all,
    fields(error = %error, after = after),
)]
fn record_checkpoint_read_failed(error: &sqlx::Error, after: u64) {}

#[tracing::instrument(
    name = "obix.sequencer.stream_closed",
    level = "error",
    fields(otel.status_code = "ERROR"),
)]
fn record_stream_closed() {}