faucet_core/idempotency.rs
1//! Exactly-once / idempotent delivery primitives.
2//!
3//! The pipeline issues a monotonic **commit token** for every page that carries
4//! a bookmark. The token is persisted in the [`StateStore`](crate::state::StateStore)
5//! value next to the bookmark and committed inside the sink's own transaction,
6//! so a crash between "sink durably wrote" and "state persisted" is resolved on
7//! resume by skipping pages the sink already committed. See
8//! `docs/superpowers/specs/2026-06-09-exactly-once-delivery-design.md`.
9
10use serde::{Deserialize, Serialize};
11use serde_json::Value;
12
13/// Delivery guarantee for a pipeline run.
14#[derive(
15 Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema,
16)]
17#[serde(rename_all = "snake_case")]
18pub enum DeliveryMode {
19 /// Today's behaviour: a page may be re-delivered after a crash between the
20 /// sink write and the bookmark persist. Downstream must tolerate duplicates.
21 #[default]
22 AtLeastOnce,
23 /// The sink durably records a per-page commit token atomically with the
24 /// data; on resume the pipeline skips already-committed pages. Requires a
25 /// state store, an idempotent sink, and a deterministic-replay source.
26 ExactlyOnce,
27}
28
29/// How faithfully a [`Source`](crate::Source) **replays** its record stream
30/// when resumed from a bookmark.
31///
32/// This is the source-side capability the effectively-once *atomic-watermark*
33/// mechanism depends on: after a crash the pipeline re-anchors the source at a
34/// persisted position, and correctness requires that nothing before that
35/// position is re-emitted and nothing after it is skipped.
36#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
37#[serde(rename_all = "snake_case")]
38pub enum ReplayGuarantee {
39 /// Resuming from a bookmark may replay a *different* record stream
40 /// (query-based sources whose upstream can mutate, sources without
41 /// per-page bookmarks). The default.
42 #[default]
43 NonDeterministic,
44 /// The source emits a complete resume position (bookmark) on **every**
45 /// page, and resuming from any such bookmark continues the record stream
46 /// at exactly that position — no record before the bookmark is re-emitted
47 /// and none after it is skipped (immutable-log sources: CDC WAL/binlog/
48 /// change streams, Kafka partitions).
49 Deterministic,
50}
51
52/// The strongest delivery guarantee a [`Sink`](crate::Sink) can uphold.
53#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
54#[serde(rename_all = "snake_case")]
55pub enum SinkGuarantee {
56 /// Plain writes: a replayed page is written again. The default.
57 #[default]
58 AtLeastOnce,
59 /// The sink can dedup by key (`write_mode: upsert` with a configured
60 /// `key`): re-applying a record with the same key converges instead of
61 /// duplicating.
62 KeyedUpsert,
63 /// The sink can commit a page's rows **and** a commit token in one atomic
64 /// transaction ([`Sink::write_batch_idempotent`](crate::Sink)).
65 AtomicWatermark,
66}
67
68/// The mechanism through which a pipeline achieves effectively-once delivery.
69#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
70#[serde(rename_all = "snake_case")]
71pub enum EffectivelyOnceMechanism {
72 /// Deterministic-replay source + sink that commits data and a per-page
73 /// commit token atomically; on resume already-committed pages are skipped
74 /// (or the stream is re-anchored at the sink's recorded position).
75 AtomicWatermark,
76 /// The sink dedups by key (`write_mode: upsert`); replayed records
77 /// converge on the same keyed row. Works with any source.
78 KeyedUpsert,
79}
80
81/// The end-to-end guarantee a *pipeline* provides for a given
82/// source × sink × config combination.
83///
84/// Deliberately no `ExactlyOnce` variant — distributed-consensus exactly-once
85/// is not achievable here; effectively-once (idempotent at-least-once: each
86/// record is *observably applied* once) is the ceiling, and
87/// `delivery: exactly_once` in config is precisely documented as requesting it.
88#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
89#[serde(rename_all = "snake_case", tag = "guarantee", content = "via")]
90pub enum DeliveryGuarantee {
91 /// A crash between the sink write and the bookmark persist may re-deliver
92 /// a page. Downstream must tolerate duplicates.
93 AtLeastOnce,
94 /// Idempotent at-least-once: each record is observably applied once.
95 EffectivelyOnce(EffectivelyOnceMechanism),
96}
97
98impl std::fmt::Display for DeliveryGuarantee {
99 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100 match self {
101 Self::AtLeastOnce => write!(f, "at-least-once"),
102 Self::EffectivelyOnce(EffectivelyOnceMechanism::AtomicWatermark) => {
103 write!(f, "effectively-once (atomic watermark)")
104 }
105 Self::EffectivelyOnce(EffectivelyOnceMechanism::KeyedUpsert) => {
106 write!(f, "effectively-once (keyed upsert)")
107 }
108 }
109 }
110}
111
112/// Inputs to [`derive_delivery_guarantee`] — the facts about a concrete
113/// source × sink × config combination the derivation keys off.
114#[derive(Debug, Clone, Copy, Default)]
115pub struct GuaranteeInputs {
116 /// The source's replay capability.
117 pub replay: ReplayGuarantee,
118 /// Whether the sink commits data + token atomically
119 /// (`Sink::supports_idempotent_writes`).
120 pub sink_atomic: bool,
121 /// Whether the sink is *configured* to dedup by key — `write_mode: upsert`
122 /// (or `delete`) with a non-empty `key` (`Sink::dedups_by_key`).
123 pub keyed_upsert_configured: bool,
124 /// Whether a durable (non-memory) state store is configured. The
125 /// atomic-watermark mechanism persists its cross-restart sequence here.
126 pub durable_state: bool,
127 /// Whether a DLQ is configured (incompatible with the atomic-watermark
128 /// mechanism in this version).
129 pub dlq: bool,
130}
131
132/// Derive the end-to-end [`DeliveryGuarantee`] a pipeline actually provides.
133///
134/// Preference order: the atomic-watermark mechanism (strongest bookkeeping,
135/// no keyed-schema requirement) when the topology supports it, then keyed
136/// upsert, then at-least-once. A sink that is both atomic and keyed reports
137/// atomic-watermark when the source replays deterministically, and falls back
138/// to keyed upsert otherwise.
139pub fn derive_delivery_guarantee(i: &GuaranteeInputs) -> DeliveryGuarantee {
140 if i.sink_atomic && i.replay == ReplayGuarantee::Deterministic && i.durable_state && !i.dlq {
141 return DeliveryGuarantee::EffectivelyOnce(EffectivelyOnceMechanism::AtomicWatermark);
142 }
143 if i.keyed_upsert_configured {
144 return DeliveryGuarantee::EffectivelyOnce(EffectivelyOnceMechanism::KeyedUpsert);
145 }
146 DeliveryGuarantee::AtLeastOnce
147}
148
149/// Reserved key marking the exactly-once state wrapper object.
150const EO_MARKER: &str = "__faucet_eo";
151const EO_BOOKMARK: &str = "bookmark";
152const EO_SEQ: &str = "seq";
153
154/// Width of the zero-padded decimal token. `u64::MAX` is 20 digits, so 20 makes
155/// lexicographic order match numeric order for the full `u64` range.
156const TOKEN_WIDTH: usize = 20;
157
158/// Separator between the numeric sequence and the embedded resume bookmark in
159/// a commit token. The prefix before it is always the fixed-width sequence.
160const TOKEN_BOOKMARK_SEP: char = '#';
161
162/// Render a page sequence as a fixed-width, lexicographically-ordered token.
163pub fn format_token(seq: u64) -> String {
164 format!("{seq:0TOKEN_WIDTH$}")
165}
166
167/// Render a commit token that carries the page's **resume bookmark** alongside
168/// the sequence: `"{seq:020}#{bookmark-json}"`.
169///
170/// Sinks store the token opaquely, so the committed watermark doubles as a
171/// durable record of *where the stream stood* when the page committed. On
172/// resume the pipeline recovers that position from the sink
173/// ([`parse_token_parts`]) and re-anchors the source there — closing the
174/// crash window between "sink durably committed" and "state store persisted"
175/// without requiring the source to replay identical page boundaries.
176pub fn format_token_with_bookmark(seq: u64, bookmark: Option<&Value>) -> String {
177 match bookmark {
178 Some(bm) => format!("{seq:0TOKEN_WIDTH$}{TOKEN_BOOKMARK_SEP}{bm}"),
179 None => format_token(seq),
180 }
181}
182
183/// Parse the numeric sequence from a token produced by [`format_token`] or
184/// [`format_token_with_bookmark`]. Returns `None` on garbage.
185pub fn parse_token(s: &str) -> Option<u64> {
186 let seq = match s.split_once(TOKEN_BOOKMARK_SEP) {
187 Some((prefix, _)) => prefix,
188 None => s,
189 };
190 seq.trim().parse::<u64>().ok()
191}
192
193/// Parse a stored commit token into `(seq, embedded_bookmark)`.
194///
195/// Tokens written before bookmarks were embedded (bare `format_token` output)
196/// parse with `bookmark = None`. A bookmark suffix that is not valid JSON also
197/// yields `None` for the bookmark — the sequence alone still drives the
198/// skip-on-resume path.
199pub fn parse_token_parts(s: &str) -> Option<(u64, Option<Value>)> {
200 match s.split_once(TOKEN_BOOKMARK_SEP) {
201 Some((prefix, suffix)) => {
202 let seq = prefix.trim().parse::<u64>().ok()?;
203 Some((seq, serde_json::from_str(suffix).ok()))
204 }
205 None => Some((s.trim().parse::<u64>().ok()?, None)),
206 }
207}
208
209/// Wrap a bookmark + sequence into the exactly-once state value.
210pub fn wrap_state(bookmark: Option<&Value>, seq: u64) -> Value {
211 serde_json::json!({
212 EO_MARKER: 1,
213 EO_BOOKMARK: bookmark.cloned().unwrap_or(Value::Null),
214 EO_SEQ: seq,
215 })
216}
217
218/// Unwrap a stored state value into `(bookmark, seq)`.
219///
220/// A value that is the exactly-once wrapper object unwraps to its inner
221/// bookmark + seq. Anything else is treated as a legacy/at-least-once **bare
222/// bookmark** with `seq = 0` — so switching an existing pipeline to
223/// `exactly_once` resumes cleanly (the sink's own watermark is authoritative).
224pub fn unwrap_state(value: &Value) -> (Option<Value>, u64) {
225 if let Value::Object(map) = value
226 && map.get(EO_MARKER).and_then(Value::as_u64) == Some(1)
227 {
228 let bookmark = match map.get(EO_BOOKMARK) {
229 None | Some(Value::Null) => None,
230 Some(v) => Some(v.clone()),
231 };
232 let seq = map.get(EO_SEQ).and_then(Value::as_u64).unwrap_or(0);
233 return (bookmark, seq);
234 }
235 // Legacy bare bookmark.
236 let bookmark = if value.is_null() {
237 None
238 } else {
239 Some(value.clone())
240 };
241 (bookmark, 0)
242}
243
244/// Canonical watermark table the SQL sinks UPSERT the commit token into.
245pub const COMMIT_TOKEN_TABLE: &str = "_faucet_commit_token";
246/// Watermark column holding the pipeline state-key (`{name}::{row_id}`).
247pub const COMMIT_TOKEN_SCOPE_COL: &str = "scope";
248/// Watermark column holding the latest committed token.
249pub const COMMIT_TOKEN_TOKEN_COL: &str = "token";
250
251/// Iceberg snapshot summary property names.
252pub const ICEBERG_SCOPE_PROP: &str = "faucet.commit-scope";
253pub const ICEBERG_TOKEN_PROP: &str = "faucet.commit-token";
254
255/// Fit a watermark scope into a length-capped, indexable key column.
256///
257/// The scope is the pipeline state key — `{name}::{row}` for a root, plus
258/// `::{parent_record_key}` for a child — and a child's key comes from *record
259/// data*, so it has no length bound. The SQL sinks store it as a PRIMARY KEY, and
260/// a key column cannot be unbounded (MySQL's index limit, SQL Server's 900-byte
261/// key budget), so an over-long scope either errors or — under a non-strict MySQL
262/// `sql_mode` — is **truncated**, silently collapsing two distinct rows onto one
263/// watermark so one row's committed token suppresses the other's pages (#456 L1).
264///
265/// Scopes at or under `max` are returned verbatim, so every watermark written
266/// before this existed still resolves. A longer one is replaced by a
267/// deterministic, collision-resistant digest form (`__h:<64-hex>`), which is
268/// stable across restarts — the only property the watermark needs.
269/// Length of the `__h:` + 16 hex-digit suffix appended to a shortened scope.
270const SCOPE_DIGEST_LEN: usize = 4 + 16;
271
272pub fn scope_key(scope: &str, max: usize) -> String {
273 if scope.len() <= max {
274 return scope.to_owned();
275 }
276 // FNV-1a rather than a crypto hash: `sha2` is an optional dependency of this
277 // crate (masking / transform-hash / encryption) and this module is always
278 // compiled. The requirement is determinism, not preimage resistance — the same
279 // choice the backfill progress marker makes.
280 let mut h: u64 = 0xcbf2_9ce4_8422_2325;
281 for b in scope.as_bytes() {
282 h ^= u64::from(*b);
283 h = h.wrapping_mul(0x1000_0000_01b3);
284 }
285 // Keep as much of the readable head as fits, so an operator inspecting the
286 // watermark table can still tell which pipeline a row belongs to. Truncate on
287 // a char boundary — a scope may hold non-ASCII.
288 let room = max.saturating_sub(SCOPE_DIGEST_LEN);
289 let mut head = 0usize;
290 for (i, _) in scope.char_indices() {
291 if i > room {
292 break;
293 }
294 head = i;
295 }
296 let shortened = format!("{}__h:{h:016x}", &scope[..head]);
297 tracing::debug!(
298 scope_len = scope.len(),
299 max,
300 "exactly-once scope exceeds the sink's key-column width; shortening with a digest"
301 );
302 shortened
303}
304
305#[cfg(test)]
306mod tests {
307 use super::*;
308 use serde_json::json;
309
310 #[test]
311 fn token_round_trips_and_orders_lexicographically() {
312 assert_eq!(format_token(42).len(), TOKEN_WIDTH);
313 assert_eq!(parse_token(&format_token(42)), Some(42));
314 assert_eq!(parse_token(&format_token(0)), Some(0));
315 assert_eq!(parse_token(&format_token(u64::MAX)), Some(u64::MAX));
316 assert!(format_token(9) < format_token(10));
317 assert!(format_token(2) < format_token(1000));
318 }
319
320 #[test]
321 fn parse_token_rejects_garbage() {
322 assert_eq!(parse_token("abc"), None);
323 assert_eq!(parse_token(""), None);
324 }
325
326 #[test]
327 fn wrap_then_unwrap_preserves_bookmark_and_seq() {
328 let bm = json!({"lsn": "0/16B2D58"});
329 let wrapped = wrap_state(Some(&bm), 7);
330 let (got_bm, got_seq) = unwrap_state(&wrapped);
331 assert_eq!(got_bm, Some(bm));
332 assert_eq!(got_seq, 7);
333 }
334
335 #[test]
336 fn wrap_none_bookmark_unwraps_to_none() {
337 let wrapped = wrap_state(None, 3);
338 let (got_bm, got_seq) = unwrap_state(&wrapped);
339 assert_eq!(got_bm, None);
340 assert_eq!(got_seq, 3);
341 }
342
343 #[test]
344 fn legacy_bare_bookmark_unwraps_with_seq_zero() {
345 let (bm, seq) = unwrap_state(&json!("2024-12-01"));
346 assert_eq!(bm, Some(json!("2024-12-01")));
347 assert_eq!(seq, 0);
348 let (bm2, seq2) = unwrap_state(&json!({"updated_at": "2024-12-01"}));
349 assert_eq!(bm2, Some(json!({"updated_at": "2024-12-01"})));
350 assert_eq!(seq2, 0);
351 }
352
353 #[test]
354 fn object_with_non_sentinel_marker_is_treated_as_bare_bookmark() {
355 // A legacy/user object that merely contains the key must NOT be misread
356 // as an EO wrapper — only the typed sentinel `1` counts.
357 let v = json!({"__faucet_eo": null, "offset": 500});
358 let (bm, seq) = unwrap_state(&v);
359 assert_eq!(bm, Some(v));
360 assert_eq!(seq, 0);
361 }
362
363 #[test]
364 fn null_value_unwraps_to_none_seq_zero() {
365 let (bm, seq) = unwrap_state(&json!(null));
366 assert_eq!(bm, None);
367 assert_eq!(seq, 0);
368 }
369
370 #[test]
371 fn token_with_bookmark_round_trips() {
372 let bm = json!({"partition_offsets": [{"topic": "t", "partition": 0, "offset": 42}]});
373 let token = format_token_with_bookmark(7, Some(&bm));
374 assert!(token.starts_with(&format_token(7)));
375 assert_eq!(parse_token(&token), Some(7));
376 let (seq, parsed_bm) = parse_token_parts(&token).unwrap();
377 assert_eq!(seq, 7);
378 assert_eq!(parsed_bm, Some(bm));
379 }
380
381 #[test]
382 fn token_with_no_bookmark_is_bare_and_back_compatible() {
383 assert_eq!(format_token_with_bookmark(3, None), format_token(3));
384 let (seq, bm) = parse_token_parts(&format_token(3)).unwrap();
385 assert_eq!((seq, bm), (3, None));
386 }
387
388 #[test]
389 fn token_with_bookmark_orders_lexicographically_on_prefix() {
390 // The fixed-width numeric prefix keeps lexicographic order meaningful
391 // even with an embedded bookmark (kafka side-topic folding compares
392 // parsed sequences, but SQL MAX() naturally works too).
393 let a = format_token_with_bookmark(9, Some(&json!({"o": 1})));
394 let b = format_token_with_bookmark(10, Some(&json!({"o": 2})));
395 assert!(a < b);
396 }
397
398 #[test]
399 fn parse_token_parts_tolerates_garbage() {
400 assert_eq!(parse_token_parts("abc"), None);
401 assert_eq!(parse_token_parts(""), None);
402 // Bad JSON suffix: sequence survives, bookmark is dropped.
403 let (seq, bm) = parse_token_parts("00000000000000000005#{not json").unwrap();
404 assert_eq!((seq, bm), (5, None));
405 // parse_token ignores the suffix entirely.
406 assert_eq!(parse_token("00000000000000000005#{not json"), Some(5));
407 }
408
409 #[test]
410 fn derive_guarantee_prefers_atomic_then_keyed_then_at_least_once() {
411 use ReplayGuarantee::*;
412 let base = GuaranteeInputs {
413 replay: Deterministic,
414 sink_atomic: true,
415 keyed_upsert_configured: false,
416 durable_state: true,
417 dlq: false,
418 };
419 assert_eq!(
420 derive_delivery_guarantee(&base),
421 DeliveryGuarantee::EffectivelyOnce(EffectivelyOnceMechanism::AtomicWatermark)
422 );
423 // Atomic path degrades without deterministic replay…
424 let non_det = GuaranteeInputs {
425 replay: NonDeterministic,
426 ..base
427 };
428 assert_eq!(
429 derive_delivery_guarantee(&non_det),
430 DeliveryGuarantee::AtLeastOnce
431 );
432 // …but keyed upsert rescues it, source-independent.
433 let keyed = GuaranteeInputs {
434 keyed_upsert_configured: true,
435 ..non_det
436 };
437 assert_eq!(
438 derive_delivery_guarantee(&keyed),
439 DeliveryGuarantee::EffectivelyOnce(EffectivelyOnceMechanism::KeyedUpsert)
440 );
441 // A DLQ or missing durable state disables atomic; keyed still applies.
442 let dlq = GuaranteeInputs {
443 dlq: true,
444 keyed_upsert_configured: true,
445 ..base
446 };
447 assert_eq!(
448 derive_delivery_guarantee(&dlq),
449 DeliveryGuarantee::EffectivelyOnce(EffectivelyOnceMechanism::KeyedUpsert)
450 );
451 let mem_state = GuaranteeInputs {
452 durable_state: false,
453 ..base
454 };
455 assert_eq!(
456 derive_delivery_guarantee(&mem_state),
457 DeliveryGuarantee::AtLeastOnce
458 );
459 }
460
461 #[test]
462 fn guarantee_display_is_human_readable() {
463 assert_eq!(DeliveryGuarantee::AtLeastOnce.to_string(), "at-least-once");
464 assert_eq!(
465 DeliveryGuarantee::EffectivelyOnce(EffectivelyOnceMechanism::AtomicWatermark)
466 .to_string(),
467 "effectively-once (atomic watermark)"
468 );
469 assert_eq!(
470 DeliveryGuarantee::EffectivelyOnce(EffectivelyOnceMechanism::KeyedUpsert).to_string(),
471 "effectively-once (keyed upsert)"
472 );
473 }
474
475 #[test]
476 fn capability_enums_default_to_weakest() {
477 assert_eq!(
478 ReplayGuarantee::default(),
479 ReplayGuarantee::NonDeterministic
480 );
481 assert_eq!(SinkGuarantee::default(), SinkGuarantee::AtLeastOnce);
482 }
483
484 #[test]
485 fn delivery_mode_serde_is_snake_case_and_defaults_at_least_once() {
486 assert_eq!(DeliveryMode::default(), DeliveryMode::AtLeastOnce);
487 assert_eq!(
488 serde_json::to_string(&DeliveryMode::ExactlyOnce).unwrap(),
489 "\"exactly_once\""
490 );
491 let m: DeliveryMode = serde_json::from_str("\"at_least_once\"").unwrap();
492 assert_eq!(m, DeliveryMode::AtLeastOnce);
493 }
494}
495
496#[cfg(test)]
497mod scope_key_tests {
498 use super::*;
499
500 /// #456 L1: the SQL sinks store the scope as a length-capped PRIMARY KEY, so
501 /// a long child scope (its key comes from record data and has no bound) either
502 /// errored or — under a non-strict MySQL sql_mode — truncated, collapsing two
503 /// rows onto one watermark.
504 #[test]
505 fn scope_key_passes_short_scopes_through_and_shortens_long_ones() {
506 // Backwards compatible: anything that fit before is returned verbatim, so
507 // watermarks written before this existed still resolve.
508 assert_eq!(scope_key("pipe::row", 255), "pipe::row");
509 let exactly = "x".repeat(255);
510 assert_eq!(scope_key(&exactly, 255), exactly);
511
512 // Over the cap: shortened, and within the cap.
513 let long = format!("pipe::row::{}", "k".repeat(400));
514 let key = scope_key(&long, 255);
515 assert!(key.len() <= 255, "len {}", key.len());
516 assert_ne!(key, long);
517 // Keeps a readable head so the row is still attributable.
518 assert!(key.starts_with("pipe::row::"), "{key}");
519 assert!(key.contains("__h:"), "{key}");
520 }
521
522 #[test]
523 fn scope_key_is_deterministic_and_distinguishes_scopes() {
524 let a = format!("pipe::row::{}", "a".repeat(400));
525 let b = format!("pipe::row::{}", "b".repeat(400));
526 // Stable across calls — the watermark must resolve after a restart.
527 assert_eq!(scope_key(&a, 255), scope_key(&a, 255));
528 // Two distinct scopes must not collide onto one watermark. Under plain
529 // truncation both of these would become the same 255-char prefix.
530 assert_ne!(scope_key(&a, 255), scope_key(&b, 255));
531 assert_eq!(&a[..255], &format!("pipe::row::{}", "a".repeat(400))[..255]);
532 }
533
534 #[test]
535 fn scope_key_truncates_on_a_char_boundary() {
536 // A multi-byte head must not be split mid-character (that would panic).
537 let long = format!("pipé::{}", "é".repeat(400));
538 let key = scope_key(&long, 255);
539 assert!(key.len() <= 255);
540 assert!(key.contains("__h:"), "{key}");
541 }
542}