obix 0.10.0

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
use serde::{Serialize, de::DeserializeOwned};

use es_entity::hooks::HookOperation;

use crate::{
    inbox::{InboxError, InboxEvent, InboxEventId, InboxEventStatus, InboxIdempotencyKey},
    out::{
        DecodeFailure, EphemeralEventType, EphemeralOutboxEvent, OutboxEventId,
        PersistentOutboxEvent, UndecodableEventError,
    },
    sequence::*,
};

#[derive(Clone)]
#[cfg_attr(feature = "default-tables", derive(obix_macros::MailboxTables))]
#[cfg_attr(feature = "default-tables", obix(crate = "crate"))]
pub struct DefaultMailboxTables;

/// Decode one stored persistent row into a delivery item, invoked from
/// `MailboxTables` derive output. A `NULL` payload is a plain placeholder
/// (`Ok` with `payload: None`); a payload that does not decode into the
/// caller's event type (e.g. a variant the consumer does not know yet, or a
/// row written by a different event enum sharing the table) must neither
/// panic — a single poison row previously wedged the whole pipeline in a hot
/// panic/retry loop — nor be silently dropped: it becomes the `Err` arm
/// ([`UndecodableEventError`]), still occupying its sequence position, and
/// its fate is decided by consumer policy (see
/// [`SingletonSubscriber::handle_undecodable`](crate::SingletonSubscriber::handle_undecodable)).
#[doc(hidden)]
pub fn decode_persistent_event<P>(
    id: OutboxEventId,
    sequence: u64,
    recorded_at: chrono::DateTime<chrono::Utc>,
    tracing_context: Option<es_entity::context::TracingContext>,
    payload: Option<serde_json::Value>,
) -> Result<PersistentOutboxEvent<P>, UndecodableEventError>
where
    P: Serialize + DeserializeOwned + Send,
{
    let sequence = EventSequence::from(sequence);
    let payload = match payload {
        None => None,
        Some(raw) => match P::deserialize(&raw) {
            Ok(payload) => Some(payload),
            Err(error) => {
                record_persistent_payload_undecodable(&error, u64::from(sequence));
                return Err(UndecodableEventError {
                    id,
                    sequence,
                    recorded_at,
                    failure: DecodeFailure {
                        error: error.to_string(),
                        raw,
                    },
                });
            }
        },
    };
    Ok(PersistentOutboxEvent {
        id,
        sequence,
        payload,
        tracing_context,
        recorded_at,
    })
}

#[tracing::instrument(
    name = "obix.tables.persistent_payload_undecodable",
    level = "error",
    skip_all,
    fields(otel.status_code = "ERROR", error = %error, sequence = sequence)
)]
fn record_persistent_payload_undecodable(error: &serde_json::Error, sequence: u64) {}

/// Invoked from `MailboxTables` derive output when a stored ephemeral event
/// payload cannot be deserialized; the event is dropped from the result.
/// Unlike the persistent stream (ordered, guaranteed delivery — see
/// [`decode_persistent_payload`]) the ephemeral stream is best-effort
/// last-value by design, so dropping is the honest degradation.
#[doc(hidden)]
#[tracing::instrument(
    name = "obix.tables.ephemeral_payload_undecodable",
    level = "error",
    skip_all,
    fields(otel.status_code = "ERROR", error = %error, event_type = %event_type)
)]
pub fn record_ephemeral_payload_undecodable(error: &serde_json::Error, event_type: &str) {}

/// Invoked from `MailboxTables` derive output when a stored ephemeral row's
/// `event_type` cannot be deserialized (schema drift or foreign rows); the
/// event is dropped from the result, like an undecodable payload.
#[doc(hidden)]
#[tracing::instrument(
    name = "obix.tables.ephemeral_event_type_undecodable",
    level = "error",
    skip_all,
    fields(otel.status_code = "ERROR", error = %error, event_type = %event_type)
)]
pub fn record_ephemeral_event_type_undecodable(error: &serde_json::Error, event_type: &str) {}

/// Invoked from `MailboxTables` derive output when the tracing-context
/// envelope cannot be deserialized; the event is kept with no context
/// attached (the context is delivery metadata, not payload data).
#[doc(hidden)]
#[tracing::instrument(
    name = "obix.tables.tracing_context_undecodable",
    level = "error",
    skip_all,
    fields(otel.status_code = "ERROR", error = %error)
)]
pub fn record_tracing_context_undecodable(error: &serde_json::Error) {}

/// One page/batch of decoded persistent rows: each item is one committed
/// sequence position — `Ok` for a decoded event or a placeholder, `Err`
/// for a stored payload that does not decode into `P`.
pub type PersistentEventRows<P> = Vec<Result<PersistentOutboxEvent<P>, UndecodableEventError>>;

pub trait MailboxTables: Send + Sync + 'static {
    fn highest_known_persistent_sequence<'a>(
        op: impl es_entity::IntoOneTimeExecutor<'a>,
    ) -> impl Future<Output = Result<EventSequence, sqlx::Error>> + Send;

    fn persist_events<'a, P>(
        op: &mut HookOperation<'a>,
        events: impl Iterator<Item = P>,
    ) -> impl Future<Output = Result<Vec<PersistentOutboxEvent<P>>, sqlx::Error>> + Send
    where
        P: Serialize + DeserializeOwned + Send;

    /// [`persist_events`](Self::persist_events) variant whose insert carries
    /// the legacy in-transaction `pg_notify` hint. Only for operations
    /// without commit-hook support (bare `sqlx::Transaction`), where
    /// `post_commit` never runs and the debounced notifier cannot observe
    /// the commit.
    fn persist_events_notifying<'a, P>(
        op: &mut HookOperation<'a>,
        events: impl Iterator<Item = P>,
    ) -> impl Future<Output = Result<Vec<PersistentOutboxEvent<P>>, sqlx::Error>> + Send
    where
        P: Serialize + DeserializeOwned + Send;

    /// Load the committed rows in `(from_sequence, from_sequence +
    /// buffer_size]` with a plain bounded SELECT. Each item is one committed
    /// sequence position: `Ok` for a decoded event or a stored placeholder
    /// (`payload: None`), `Err` for a stored payload that does not decode
    /// into `P` — delivered, not dropped, so it still occupies its sequence
    /// position. The page may contain sequence gaps (in-flight or lost
    /// writers): this method never writes placeholder rows — that is
    /// exclusively [`fill_gaps`](Self::fill_gaps) /
    /// [`fill_gaps_deduped`](Self::fill_gaps_deduped), invoked proof-gated
    /// and batch-capped from the per-process `out::gap_fill::GapFiller`.
    fn load_next_page<P>(
        pool: &sqlx::PgPool,
        from_sequence: EventSequence,
        buffer_size: usize,
    ) -> impl Future<Output = Result<PersistentEventRows<P>, sqlx::Error>> + Send
    where
        P: Serialize + DeserializeOwned + Send;

    /// Load the **contiguous** committed run in `(from_sequence,
    /// from_sequence + buffer_size]` — the same window as
    /// [`load_next_page`](Self::load_next_page), cut at the first gap. The cut
    /// is found index-only, so a window blocked at its first sequence returns
    /// an empty page without touching the heap.
    ///
    /// For callers delivering in sequence order, where everything past the
    /// first hole is unusable anyway. This deliberately cannot distinguish
    /// "the window has a hole" from "the window ended" — the caller resolves
    /// that against its own frontier, or asks
    /// [`missing_sequences`](Self::missing_sequences).
    fn load_next_contiguous_page<P>(
        pool: &sqlx::PgPool,
        from_sequence: EventSequence,
        buffer_size: usize,
    ) -> impl Future<Output = Result<PersistentEventRows<P>, sqlx::Error>> + Send
    where
        P: Serialize + DeserializeOwned + Send;

    /// Whether `sequence` has a committed row — a real event or a
    /// placeholder. One index probe, no payload: the per-interval re-check
    /// for a reader parked on a gap.
    fn sequence_present(
        pool: &sqlx::PgPool,
        sequence: EventSequence,
    ) -> impl Future<Output = Result<bool, sqlx::Error>> + Send;

    /// The sequences in `(after_sequence, up_to_sequence]` with no committed
    /// row, as an index-only anti-join — no payloads fetched. For callers
    /// that must report holes rather than consume events.
    fn missing_sequences(
        pool: &sqlx::PgPool,
        after_sequence: EventSequence,
        up_to_sequence: EventSequence,
    ) -> impl Future<Output = Result<Vec<EventSequence>, sqlx::Error>> + Send;

    /// Insert placeholder rows (`payload: NULL`) for the given sequences
    /// with `ON CONFLICT (sequence) DO NOTHING`, returning only the rows
    /// actually inserted. Sequences that already have a committed row —
    /// a real event or an earlier placeholder — are left untouched: no
    /// rewrite, no dead tuple. A sequence whose writer is still in flight
    /// blocks the insert on that transaction's speculative-insertion lock
    /// until it resolves, so callers must only pass sequences that are
    /// provably abandoned: either their own rolled-back allocations (the
    /// rollback-compensation path) or sequences proven lost via
    /// [`abandonment_marker`](Self::abandonment_marker) /
    /// [`abandonment_proof_passed`](Self::abandonment_proof_passed).
    fn fill_gaps<P>(
        pool: &sqlx::PgPool,
        sequences: Vec<EventSequence>,
    ) -> impl Future<Output = Result<PersistentEventRows<P>, sqlx::Error>> + Send
    where
        P: Serialize + DeserializeOwned + Send;

    /// [`fill_gaps`](Self::fill_gaps) behind a per-table
    /// `pg_try_advisory_xact_lock`: the cluster-wide dedup for backstop
    /// fills, where multiple nodes may attempt the same range. Returns
    /// `None` without inserting anything when another connection holds the
    /// fill lock — the winner's rows are committed by the time its lock
    /// releases, so the caller's next page read delivers them.
    fn fill_gaps_deduped<P>(
        pool: &sqlx::PgPool,
        sequences: Vec<EventSequence>,
    ) -> impl Future<Output = Result<Option<PersistentEventRows<P>>, sqlx::Error>> + Send
    where
        P: Serialize + DeserializeOwned + Send;

    /// Assign an abandonment marker: a real xid on this connection (one
    /// auto-commit statement), returned together with the sequence
    /// allocation head read in the same statement. Every write transaction
    /// that had begun before the marker holds a smaller xid, and every
    /// sequence `<=` the returned head was allocated before it — the two
    /// facts [`abandonment_proof_passed`](Self::abandonment_proof_passed)
    /// combines into a proof.
    fn abandonment_marker(
        pool: &sqlx::PgPool,
    ) -> impl Future<Output = Result<(String, EventSequence), sqlx::Error>> + Send;

    /// Whether the xmin horizon has passed `marker`: every transaction
    /// with an older xid has ended. A sequence allocated before the marker
    /// (per [`abandonment_marker`](Self::abandonment_marker)'s head) that
    /// is still absent from the table once this returns `true` is provably
    /// abandoned — its writer ended without committing it — so a
    /// placeholder insert cannot collide with a live writer. Latency is
    /// the actual remaining lifetime of the concurrent write transactions,
    /// not a fixed guess.
    fn abandonment_proof_passed(
        pool: &sqlx::PgPool,
        marker: &str,
    ) -> impl Future<Output = Result<bool, sqlx::Error>> + Send;

    /// Load the committed events in `(after_sequence, up_to_sequence]` with
    /// a plain SELECT. Unlike [`load_next_page`](Self::load_next_page) this
    /// never writes placeholder rows for sequence gaps — sequences absent
    /// from the result belong to in-flight transactions and are left to the
    /// grace-period gap fill. Undecodable payloads are the `Err` items, as
    /// in [`load_next_page`](Self::load_next_page).
    fn load_events_in_range<P>(
        pool: &sqlx::PgPool,
        after_sequence: EventSequence,
        up_to_sequence: EventSequence,
    ) -> impl Future<Output = Result<PersistentEventRows<P>, sqlx::Error>> + Send
    where
        P: Serialize + DeserializeOwned + Send;

    fn persist_ephemeral_event<P>(
        pool: &sqlx::PgPool,
        now: Option<chrono::DateTime<chrono::Utc>>,
        event_type: EphemeralEventType,
        payload: P,
    ) -> impl Future<Output = Result<EphemeralOutboxEvent<P>, sqlx::Error>> + Send
    where
        P: Serialize + DeserializeOwned + Send;

    fn persist_ephemeral_event_in_op<'a, P>(
        op: &mut HookOperation<'a>,
        event_type: EphemeralEventType,
        payload: P,
    ) -> impl Future<Output = Result<EphemeralOutboxEvent<P>, sqlx::Error>> + Send
    where
        P: Serialize + DeserializeOwned + Send;

    fn load_ephemeral_events<P>(
        pool: &sqlx::PgPool,
        event_type_filter: Option<EphemeralEventType>,
    ) -> impl Future<Output = Result<Vec<EphemeralOutboxEvent<P>>, sqlx::Error>> + Send
    where
        P: Serialize + DeserializeOwned + Send;

    fn persistent_outbox_events_channel() -> &'static str;
    fn ephemeral_outbox_events_channel() -> &'static str;

    /// Base name of the persistent outbox events table (honouring any table
    /// prefix). The partition maintainer derives child partition names
    /// (`{table}_p{k}`) and the sequence-object name (`{table}_sequence_seq`)
    /// from it.
    fn persistent_outbox_events_table() -> &'static str;

    /// Job type of this outbox's keyed waker: `{persistent table}.keyed-waker`.
    /// Composed at macro-expansion time so the waker's `job::JobType` — which
    /// only accepts `&'static str` — needs no runtime formatting and no leak.
    const KEYED_WAKER_JOB_TYPE: &'static str;

    // === Inbox methods ===

    fn insert_inbox_event<P>(
        op: &mut impl es_entity::AtomicOperation,
        idempotency_key: &InboxIdempotencyKey,
        payload: &P,
    ) -> impl Future<Output = Result<Option<InboxEventId>, sqlx::Error>> + Send
    where
        P: Serialize + Send + Sync;

    fn find_inbox_event_by_id(
        pool: &sqlx::PgPool,
        id: InboxEventId,
    ) -> impl Future<Output = Result<InboxEvent, InboxError>> + Send;

    fn update_inbox_event_status(
        pool: &sqlx::PgPool,
        now: Option<chrono::DateTime<chrono::Utc>>,
        id: InboxEventId,
        status: InboxEventStatus,
        error: Option<&str>,
    ) -> impl Future<Output = Result<(), sqlx::Error>> + Send;

    fn update_inbox_event_status_in_op(
        op: &mut impl es_entity::AtomicOperation,
        id: InboxEventId,
        status: InboxEventStatus,
        error: Option<&str>,
    ) -> impl Future<Output = Result<(), sqlx::Error>> + Send;

    fn list_inbox_events_by_status(
        pool: &sqlx::PgPool,
        status: InboxEventStatus,
        limit: usize,
    ) -> impl Future<Output = Result<Vec<InboxEvent>, InboxError>> + Send;

    // === Keyed-subscriber subscription methods ===

    /// Insert a new subscription row, idempotently: a conflict on
    /// `(subscriber_type, key)` — an already-live subscription — resolves to
    /// success without overwriting the existing row's `start_after` or
    /// `wake_keys`. Re-subscribing an already-subscribed key must never
    /// silently rewind or fast-forward its birth frontier.
    fn insert_subscription_in_op(
        op: &mut impl es_entity::AtomicOperation,
        subscriber_type: &str,
        key: &str,
        wake_keys: &[String],
        instance_config: serde_json::Value,
        start_after: EventSequence,
    ) -> impl Future<Output = Result<(), sqlx::Error>> + Send;

    /// Delete a subscription row. Row absence is the tombstone: no job-kill
    /// API exists or is needed — the runner's next run-start row check (or a
    /// stray wake) observes the missing row and completes.
    fn delete_subscription_in_op(
        op: &mut impl es_entity::AtomicOperation,
        subscriber_type: &str,
        key: &str,
    ) -> impl Future<Output = Result<(), sqlx::Error>> + Send;

    /// Point-read one subscription's identity and terms by primary key.
    /// `None` means cancelled (or never subscribed) — the caller's row-is-truth
    /// check.
    fn find_subscription(
        pool: &sqlx::PgPool,
        subscriber_type: &str,
        key: &str,
    ) -> impl Future<Output = Result<Option<SubscriptionRow>, sqlx::Error>> + Send;

    /// Advance one subscription's mirrored cursor, in the caller's op so it
    /// shares the fate of the job checkpoint it copies. Monotonic: a lower
    /// value than the stored one is ignored rather than applied, so a write
    /// from a superseded generation cannot rewind it.
    fn update_subscription_checkpoint_in_op(
        op: &mut impl es_entity::AtomicOperation,
        subscriber_type: &str,
        key: &str,
        checkpoint: EventSequence,
    ) -> impl Future<Output = Result<(), sqlx::Error>> + Send;

    /// `(subscriber_type, key)` of the subscriptions whose mirrored cursor is
    /// below `below`, furthest behind first, capped at `limit` — the waker's
    /// catch-up scan.
    ///
    /// Spans every *registered* subscriber type because the waker does: one
    /// scan per pass for the whole outbox, not one per type. Ordering is what
    /// makes `limit` safe to apply — it sheds the members with the most
    /// slack, so a cap can bound the wake rate without ever starving the
    /// member closest to falling out of the cache.
    ///
    /// `subscriber_types` must filter in SQL, not afterwards: a row whose
    /// type is not registered here has no runner to advance its checkpoint,
    /// so it is permanently the furthest behind and would otherwise win every
    /// scan and consume the entire limit.
    fn subscriptions_behind(
        op: &mut impl es_entity::AtomicOperation,
        subscriber_types: &[String],
        below: EventSequence,
        limit: i64,
    ) -> impl Future<Output = Result<Vec<(String, String)>, sqlx::Error>> + Send;

    /// The `(subscriber_type, key)` of every subscription whose declared
    /// `wake_keys` contain a key the batch classified an event to for that
    /// same type — the waker's flush-time lookup, run **on the flush op** so
    /// the wakes it drives and the waker's own checkpoint commit atomically.
    /// Liveness-only: an over-approximating false positive here is a harmless
    /// empty wake, never a correctness gap.
    ///
    /// `subscriber_types` and `wake_keys` are **parallel arrays**: element `i`
    /// of each names one (type, wake key) pair to match. One query covers
    /// every registered type without losing per-type precision — a type is
    /// never matched against another type's keys.
    fn subscriptions_for_wake_keys(
        op: &mut impl es_entity::AtomicOperation,
        subscriber_types: &[String],
        wake_keys: &[String],
    ) -> impl Future<Output = Result<Vec<(String, String)>, sqlx::Error>> + Send;
}

/// One subscription's identity and terms, as stored — everything but the
/// primary key `(subscriber_type, key)` itself, which the caller already
/// knows from its own lookup.
#[derive(Debug, Clone)]
pub struct SubscriptionRow {
    pub wake_keys: Vec<String>,
    pub instance_config: serde_json::Value,
    pub start_after: EventSequence,
    pub created_at: chrono::DateTime<chrono::Utc>,
}