faucet_core/traits.rs
1//! Shared traits for faucet sources and sinks.
2
3use crate::error::FaucetError;
4use crate::pipeline::StreamPage;
5use async_trait::async_trait;
6use futures_core::Stream;
7use serde_json::Value;
8use std::pin::Pin;
9
10/// A source fetches records from an external system.
11#[async_trait]
12pub trait Source: Send + Sync {
13 /// Primary fetch method. Receives context from a parent source's records.
14 ///
15 /// An empty context map means this is a root source (no parent).
16 /// Connectors that support being a child should use
17 /// [`substitute_context()`](crate::util::substitute_context) to resolve
18 /// `{placeholder}` tokens in their URL path, query parameters, headers,
19 /// or body. Connectors that don't need parent context ignore the map.
20 async fn fetch_with_context(
21 &self,
22 context: &std::collections::HashMap<String, Value>,
23 ) -> Result<Vec<Value>, FaucetError>;
24
25 /// Convenience: fetch with no parent context.
26 async fn fetch_all(&self) -> Result<Vec<Value>, FaucetError> {
27 self.fetch_with_context(&std::collections::HashMap::new())
28 .await
29 }
30
31 /// Incremental fetch with parent context support.
32 ///
33 /// Returns the records and an optional bookmark value for incremental
34 /// replication. The default delegates to `fetch_with_context` and
35 /// returns `None` for the bookmark.
36 async fn fetch_with_context_incremental(
37 &self,
38 context: &std::collections::HashMap<String, Value>,
39 ) -> Result<(Vec<Value>, Option<Value>), FaucetError> {
40 let records = self.fetch_with_context(context).await?;
41 Ok((records, None))
42 }
43
44 /// Convenience: incremental fetch with no parent context.
45 async fn fetch_all_incremental(&self) -> Result<(Vec<Value>, Option<Value>), FaucetError> {
46 self.fetch_with_context_incremental(&std::collections::HashMap::new())
47 .await
48 }
49
50 /// Stream records page-by-page so the pipeline can write to the sink as
51 /// pages arrive instead of buffering the full result set.
52 ///
53 /// `batch_size` is the *hint* the pipeline passes down; sources are free
54 /// to use a larger or smaller native chunk (e.g. one page per HTTP
55 /// response, one row-group per Parquet file) but should approximate it
56 /// where feasible. The special value `batch_size = 0` means "do not
57 /// batch — emit the entire result set in a single page." Sources that
58 /// stream natively should treat `0` as "skip the chunking layer and
59 /// yield one page after the underlying read completes" (useful for
60 /// small lookup tables or for sinks like SQL `COPY` / BigQuery load
61 /// jobs that prefer one large request).
62 ///
63 /// The default implementation fetches the full result set via
64 /// [`fetch_with_context_incremental`](Self::fetch_with_context_incremental)
65 /// and chunks it in memory by `batch_size`. The bookmark (when present)
66 /// is attached to the *final* page so the pipeline only persists after
67 /// the entire fetch has been written. Sources that can stream natively
68 /// override this method and may emit per-page bookmarks (e.g. CDC).
69 ///
70 /// An empty result with a `Some(bookmark)` still yields one empty page
71 /// carrying the bookmark, so incremental runs that produce no records
72 /// still advance their checkpoint.
73 fn stream_pages<'a>(
74 &'a self,
75 context: &'a std::collections::HashMap<String, Value>,
76 batch_size: usize,
77 ) -> Pin<Box<dyn Stream<Item = Result<StreamPage, FaucetError>> + Send + 'a>> {
78 Box::pin(async_stream::try_stream! {
79 let (records, bookmark) = self
80 .fetch_with_context_incremental(context)
81 .await?;
82 let total = records.len();
83 // batch_size == 0 means "no batching" — emit all records as one
84 // page. Otherwise chunk into pages of size `batch_size`.
85 let chunk = if batch_size == 0 { usize::MAX } else { batch_size };
86
87 if total == 0 {
88 if bookmark.is_some() {
89 yield StreamPage {
90 records: Vec::new(),
91 bookmark,
92 };
93 }
94 return;
95 }
96
97 let mut iter = records.into_iter();
98 let mut consumed = 0usize;
99 loop {
100 let batch: Vec<Value> = iter.by_ref().take(chunk).collect();
101 if batch.is_empty() {
102 break;
103 }
104 consumed += batch.len();
105 let page_bookmark = if consumed >= total {
106 bookmark.clone()
107 } else {
108 None
109 };
110 yield StreamPage {
111 records: batch,
112 bookmark: page_bookmark,
113 };
114 }
115 })
116 }
117
118 /// Whether this source can emit **columnar** ([`ColumnarPage`](crate::columnar::ColumnarPage))
119 /// pages via [`stream_batches`](Self::stream_batches). Default: `false`.
120 ///
121 /// The pipeline uses the columnar fast path only when *both* the source and
122 /// sink return `true` here (and no `Value`-shaped stage needs to observe the
123 /// records), so an Arrow-native `parquet → parquet` chain never materializes
124 /// `Value`. Opt-in and additive — see [`crate::columnar`] (RFC 0002 / #375).
125 #[cfg(feature = "arrow")]
126 fn supports_columnar(&self) -> bool {
127 false
128 }
129
130 /// Stream the source natively as Arrow
131 /// [`ColumnarPage`](crate::columnar::ColumnarPage)s.
132 ///
133 /// Only invoked when [`supports_columnar`](Self::supports_columnar) returns
134 /// `true`; the default yields a single typed "unsupported" error so a source
135 /// that advertises support but forgets to override this fails loudly rather
136 /// than silently. Each page's `bookmark` carries the same checkpoint
137 /// semantics as [`StreamPage`].
138 #[cfg(feature = "arrow")]
139 fn stream_batches<'a>(
140 &'a self,
141 context: &'a std::collections::HashMap<String, Value>,
142 batch_size: usize,
143 ) -> Pin<Box<dyn Stream<Item = Result<crate::columnar::ColumnarPage, FaucetError>> + Send + 'a>>
144 {
145 let _ = (context, batch_size);
146 let name = self.connector_name();
147 let err: Result<crate::columnar::ColumnarPage, FaucetError> = Err(FaucetError::Source(
148 format!("source '{name}' does not support columnar streaming (stream_batches)"),
149 ));
150 Box::pin(futures::stream::once(async move { err }))
151 }
152
153 /// Return a JSON Schema describing the configuration this source accepts.
154 fn config_schema(&self) -> Value {
155 serde_json::json!({"type": "object", "properties": {}})
156 }
157
158 /// Stable key under which this source's incremental-replication bookmark
159 /// should be persisted in a [`StateStore`](crate::state::StateStore).
160 ///
161 /// Returning `Some(key)` opts this source into resumable runs: when the
162 /// pipeline is configured with a state store via
163 /// [`Pipeline::with_state_store`](crate::Pipeline::with_state_store), it
164 /// reads the bookmark at `key` before fetching and writes the new
165 /// bookmark back only after the sink confirms the batch was written.
166 ///
167 /// The default returns `None`, meaning the source is not persisted.
168 /// Keys must satisfy [`validate_state_key`](crate::state::validate_state_key).
169 fn state_key(&self) -> Option<String> {
170 None
171 }
172
173 /// Apply a bookmark loaded from a [`StateStore`](crate::state::StateStore)
174 /// as this run's starting point.
175 ///
176 /// The default implementation ignores the value, which keeps existing
177 /// sources backwards-compatible. Sources that support incremental
178 /// replication override this — typically by storing the value behind
179 /// interior mutability and consulting it inside
180 /// `fetch_with_context_incremental`.
181 async fn apply_start_bookmark(&self, _bookmark: Value) -> Result<(), FaucetError> {
182 Ok(())
183 }
184
185 /// Capture the source's current replication position **without consuming
186 /// any changes**, ensuring any server-side resource (e.g. a logical
187 /// replication slot) needed to later resume from that position exists.
188 ///
189 /// Returns the position as a bookmark [`Value`] — the same shape
190 /// [`apply_start_bookmark`](Self::apply_start_bookmark) accepts — or `None`
191 /// if this source does not support position capture.
192 ///
193 /// Used by the snapshot→CDC replication orchestrator (`faucet replicate`)
194 /// to anchor the CDC stream at-or-before the bulk snapshot's read point so
195 /// the handoff has no gap. The default returns `None`.
196 async fn capture_resume_position(&self) -> Result<Option<Value>, FaucetError> {
197 Ok(None)
198 }
199
200 /// Whether this source **deterministically replays** the same page sequence
201 /// from a given bookmark — the requirement for the atomic-watermark
202 /// effectively-once path (a non-deterministic replay could cause the pipeline
203 /// to skip a page whose contents differ from the one already committed).
204 /// Default: `false`.
205 ///
206 /// Sources with a durable monotonic position and per-page bookmarks (CDC)
207 /// override this to return `true`. The pipeline rejects
208 /// `DeliveryMode::ExactlyOnce` against a source that returns `false`.
209 fn supports_exactly_once(&self) -> bool {
210 false
211 }
212
213 /// The typed replay capability this source advertises — see
214 /// [`ReplayGuarantee`](crate::ReplayGuarantee).
215 ///
216 /// The default derives from [`supports_exactly_once`](Self::supports_exactly_once)
217 /// (the boolean stays the back-compat primitive: existing connectors that
218 /// override only the boolean automatically advertise `Deterministic`
219 /// here). Override this directly only to *diverge* from the boolean —
220 /// there is currently no reason to.
221 fn replay_guarantee(&self) -> crate::idempotency::ReplayGuarantee {
222 if self.supports_exactly_once() {
223 crate::idempotency::ReplayGuarantee::Deterministic
224 } else {
225 crate::idempotency::ReplayGuarantee::NonDeterministic
226 }
227 }
228
229 /// Whether this source can split its work into independent shards for
230 /// clustered (Mode B) execution. Default: `false` (single whole-dataset
231 /// shard). Sources with a natural partition (object-store prefixes, table
232 /// primary-key ranges) override this to `true` and implement
233 /// [`enumerate_shards`](Self::enumerate_shards) +
234 /// [`apply_shard`](Self::apply_shard).
235 fn is_shardable(&self) -> bool {
236 false
237 }
238
239 /// Enumerate the shards this source splits into, aiming for roughly `target`
240 /// of them (a hint — the source may return fewer, e.g. when the dataset is
241 /// small, or one per natural partition regardless of `target`).
242 ///
243 /// Called **once per run** by the cluster coordinator; enumeration must be
244 /// deterministic enough that re-enumeration yields a compatible set (stable
245 /// shard ids), since it may run on more than one instance and is reconciled
246 /// by idempotent insert. May perform read-only I/O (a `LIST`, a `MIN/MAX`
247 /// query). The default returns a single whole-dataset shard
248 /// ([`ShardSpec::whole`](crate::ShardSpec::whole)), preserving today's
249 /// single-worker behavior.
250 async fn enumerate_shards(
251 &self,
252 _target: usize,
253 ) -> Result<Vec<crate::shard::ShardSpec>, FaucetError> {
254 Ok(vec![crate::shard::ShardSpec::whole()])
255 }
256
257 /// Narrow this source instance to a single shard before streaming.
258 ///
259 /// Called on the worker that claims `shard`, after construction and before
260 /// any `stream_pages` call. Like [`apply_start_bookmark`](Self::apply_start_bookmark)
261 /// this takes `&self` and is expected to record the shard behind interior
262 /// mutability (the source consults it when building its query / listing).
263 /// The default ignores the shard — a non-shardable source only ever receives
264 /// [`ShardSpec::whole`](crate::ShardSpec::whole), so ignoring it streams the
265 /// whole dataset. Implementations should accept the whole shard as a no-op.
266 async fn apply_shard(&self, _shard: &crate::shard::ShardSpec) -> Result<(), FaucetError> {
267 Ok(())
268 }
269
270 /// Whether this source can enumerate the datasets behind its connection
271 /// via [`discover`](Self::discover). Default: `false`. Sources backed by
272 /// an introspectable catalog (database `information_schema`, MongoDB
273 /// collections, Elasticsearch indices, object-store prefixes) override
274 /// this to `true`.
275 fn supports_discover(&self) -> bool {
276 false
277 }
278
279 /// Enumerate the datasets living behind this source's connection — one
280 /// [`DatasetDescriptor`](crate::discover::DatasetDescriptor) per table /
281 /// collection / index / prefix, each carrying a partial config override
282 /// that selects it (used by `faucet discover` to scaffold one matrix row
283 /// per dataset).
284 ///
285 /// Must be **read-only and cheap**: catalog metadata queries and listings
286 /// only, never a data scan. Descriptors must never embed credentials.
287 /// The default returns a typed "unsupported" error; override it (and
288 /// return `true` from [`supports_discover`](Self::supports_discover))
289 /// only for sources with a real catalog to introspect.
290 async fn discover(&self) -> Result<Vec<crate::discover::DatasetDescriptor>, FaucetError> {
291 Err(FaucetError::Source(format!(
292 "source '{}' does not support dataset discovery",
293 self.connector_name()
294 )))
295 }
296
297 /// Stable identifier used as the `connector` label on metrics and the
298 /// `connector` attribute on spans. Defaults to the final segment of
299 /// `std::any::type_name::<Self>()`, e.g. `"RestSource"`. Built-in
300 /// connectors override with a short, friendly snake_case name (e.g.
301 /// `"rest"`). Must return a non-empty string; observability decorators
302 /// fall back to `"unknown"` in release builds if it is empty (and
303 /// `debug_assert!` in debug builds).
304 fn connector_name(&self) -> &'static str {
305 crate::observability::strip_type_name(std::any::type_name::<Self>())
306 }
307
308 /// Logical dataset identity for lineage emission, following OpenLineage
309 /// naming conventions (<https://openlineage.io/docs/spec/naming>).
310 ///
311 /// The default returns `"<connector_name>://unknown"`. Built-in connectors
312 /// override with a credential-free URI derived from their config. Strip any
313 /// credentials with [`redact_uri_credentials`](crate::redact_uri_credentials).
314 /// Informational metadata only — never used for I/O.
315 fn dataset_uri(&self) -> String {
316 format!("{}://unknown", self.connector_name())
317 }
318
319 /// Run a fast, non-mutating preflight probe (used by `faucet doctor`).
320 ///
321 /// The default pulls a **single page** via
322 /// [`stream_pages`](Self::stream_pages) and reports success/failure — it
323 /// exercises the real read path (DNS, TLS, auth, the first request, the
324 /// first-record decode) but never paginates the full dataset and never
325 /// repeats. The page stream is dropped immediately after the first page.
326 ///
327 /// Sources whose first page *blocks* waiting for inbound data (webhook,
328 /// websocket) or has *side effects* (CDC consuming WAL) override this with a
329 /// cheaper, side-effect-free probe. Probe-level failures are returned as a
330 /// [`ProbeStatus::Fail`](crate::check::ProbeStatus) inside `Ok(report)`.
331 async fn check(
332 &self,
333 ctx: &crate::check::CheckContext,
334 ) -> Result<crate::check::CheckReport, FaucetError> {
335 use crate::check::{CheckReport, Probe};
336 use futures::StreamExt;
337
338 let empty = std::collections::HashMap::new();
339 let start = std::time::Instant::now();
340 let mut pages = self.stream_pages(&empty, 1);
341 let probe = match tokio::time::timeout(ctx.timeout, pages.next()).await {
342 Err(_) => Probe::fail("read", start.elapsed(), "timed out fetching first page"),
343 Ok(None) | Ok(Some(Ok(_))) => Probe::pass("read", start.elapsed()),
344 Ok(Some(Err(e))) => Probe::fail("read", start.elapsed(), e.to_string()),
345 };
346 Ok(CheckReport::single(probe))
347 }
348}
349
350/// Per-row outcome from [`Sink::write_batch_partial`].
351///
352/// `Ok(())` — the row was durably written to the sink.
353/// `Err(_)` — the row failed; the pipeline will route it to the DLQ when
354/// one is configured.
355pub type RowOutcome = Result<(), FaucetError>;
356
357/// A sink writes records to an external system.
358#[async_trait]
359pub trait Sink: Send + Sync {
360 /// Write a batch of records to the destination.
361 ///
362 /// Returns the number of records successfully written.
363 async fn write_batch(&self, records: &[Value]) -> Result<usize, FaucetError>;
364
365 /// Flush any buffered data to the destination.
366 ///
367 /// The default implementation is a no-op (suitable for sinks that
368 /// write immediately in `write_batch`).
369 async fn flush(&self) -> Result<(), FaucetError> {
370 Ok(())
371 }
372
373 /// Write a batch and report per-row outcomes.
374 ///
375 /// Sinks whose underlying API exposes per-row results (BigQuery
376 /// `insertAll`, Elasticsearch `_bulk`) override this. The default
377 /// implementation delegates to [`Self::write_batch`] and maps a single success
378 /// onto a uniform all-`Ok(())` vector. An outer failure is bubbled up
379 /// unchanged so the pipeline's DLQ router can apply its `on_batch_error`
380 /// policy at a single decision point.
381 async fn write_batch_partial(&self, records: &[Value]) -> Result<Vec<RowOutcome>, FaucetError> {
382 self.write_batch(records).await?;
383 Ok(records.iter().map(|_| Ok(())).collect())
384 }
385
386 /// Whether this sink can consume **columnar** (`arrow::RecordBatch`) writes
387 /// via [`write_batch_columnar`](Self::write_batch_columnar) without first
388 /// converting to `Value`. Default: `false`.
389 ///
390 /// The pipeline takes the columnar fast path only when *both* the source and
391 /// sink return `true` (RFC 0002 / #375).
392 #[cfg(feature = "arrow")]
393 fn supports_columnar(&self) -> bool {
394 false
395 }
396
397 /// Write a columnar `RecordBatch` to the destination, returning the number of
398 /// rows written.
399 ///
400 /// The default converts the batch to `Value` rows via the
401 /// [`columnar`](crate::columnar) shim and delegates to
402 /// [`write_batch`](Self::write_batch), so every sink participates correctly
403 /// even without a native columnar path. Sinks that write Arrow/Parquet
404 /// directly override this — and return `true` from
405 /// [`supports_columnar`](Self::supports_columnar) — to skip the conversion.
406 #[cfg(feature = "arrow")]
407 async fn write_batch_columnar(
408 &self,
409 batch: &arrow::array::RecordBatch,
410 ) -> Result<usize, FaucetError> {
411 let rows = crate::columnar::record_batch_to_values(batch)?;
412 self.write_batch(&rows).await
413 }
414
415 /// Whether this sink can durably commit a page's rows **and** a commit token
416 /// in a single atomic transaction. Default: `false` (at-least-once only).
417 ///
418 /// Only return `true` from a sink that genuinely commits both atomically —
419 /// see [`write_batch_idempotent`](Self::write_batch_idempotent). The pipeline
420 /// rejects `DeliveryMode::ExactlyOnce` against a sink that returns `false`.
421 fn supports_idempotent_writes(&self) -> bool {
422 false
423 }
424
425 /// The strongest delivery guarantee this sink can uphold — see
426 /// [`SinkGuarantee`](crate::SinkGuarantee).
427 ///
428 /// The default derives from the two back-compat primitives:
429 /// [`supports_idempotent_writes`](Self::supports_idempotent_writes) →
430 /// `AtomicWatermark`, else an upsert-capable
431 /// [`supported_write_modes`](Self::supported_write_modes) → `KeyedUpsert`,
432 /// else `AtLeastOnce`. Existing connectors that override only the
433 /// primitives automatically advertise the right capability here.
434 fn sink_guarantee(&self) -> crate::idempotency::SinkGuarantee {
435 if self.supports_idempotent_writes() {
436 crate::idempotency::SinkGuarantee::AtomicWatermark
437 } else if self
438 .supported_write_modes()
439 .contains(&crate::write_mode::WriteMode::Upsert)
440 {
441 crate::idempotency::SinkGuarantee::KeyedUpsert
442 } else {
443 crate::idempotency::SinkGuarantee::AtLeastOnce
444 }
445 }
446
447 /// Whether this sink instance is **configured** to dedup by key — i.e.
448 /// `write_mode: upsert` (or `delete`) with a non-empty `key`, so
449 /// re-applying a record with the same key converges instead of
450 /// duplicating. Default: `false`.
451 ///
452 /// Distinct from [`sink_guarantee`](Self::sink_guarantee) (capability):
453 /// this reflects the *live config*. Sinks that flatten a
454 /// [`WriteSpec`](crate::write_mode::WriteSpec) into their config override
455 /// it as `self.config.write.dedups_by_key()`. The pipeline consults it to
456 /// derive the keyed-upsert effectively-once mechanism at run time.
457 fn dedups_by_key(&self) -> bool {
458 false
459 }
460
461 /// Write modes this sink can apply. Default: append-only. Sinks that
462 /// implement key-based merge override this to include
463 /// [`WriteMode::Upsert`](crate::write_mode::WriteMode) /
464 /// [`WriteMode::Delete`](crate::write_mode::WriteMode). The CLI rejects a
465 /// configured mode that is not in this set at config-load time.
466 fn supported_write_modes(&self) -> &'static [crate::write_mode::WriteMode] {
467 &[crate::write_mode::WriteMode::Append]
468 }
469
470 /// The sink's live destination schema as an `infer_schema`-shaped object
471 /// (`{"type":"object","properties":{ <col>: <type-fragment>, … }}`), or
472 /// `None` for a schemaless sink or a target that does not exist yet.
473 ///
474 /// Used by the schema-drift policy to diff each page's shape against the
475 /// real destination. Default: `Ok(None)` (drift handling is inert).
476 async fn current_schema(&self) -> Result<Option<Value>, FaucetError> {
477 Ok(None)
478 }
479
480 /// Whether this sink can apply additive/widening DDL via
481 /// [`evolve_schema`](Self::evolve_schema). Default: `false`. The CLI rejects
482 /// `on_drift: evolve` against a sink that returns `false` at config-load.
483 fn supports_schema_evolution(&self) -> bool {
484 false
485 }
486
487 /// Apply an additive schema evolution (new columns, lossless widenings,
488 /// nullability relaxations) to the destination. MUST be idempotent
489 /// (`ADD COLUMN IF NOT EXISTS` semantics) so concurrent runs converge.
490 ///
491 /// Default: a typed "unsupported" error. Override only when the backend
492 /// supports in-place additive DDL (and return `true` from
493 /// `supports_schema_evolution`).
494 async fn evolve_schema(
495 &self,
496 evolution: &crate::drift::SchemaEvolution,
497 ) -> Result<(), FaucetError> {
498 let _ = evolution;
499 Err(FaucetError::Sink(format!(
500 "sink '{}' does not support schema evolution",
501 self.connector_name()
502 )))
503 }
504
505 /// Write `records` AND durably record `token` for `scope`, atomically.
506 ///
507 /// `scope` namespaces the watermark (the pipeline passes the per-row state
508 /// key, e.g. `"{name}::{row_id}"`). `token` is a monotonic, fixed-width
509 /// string (see [`format_token`](crate::format_token)).
510 ///
511 /// The default is **not** idempotent: it ignores the token and delegates to
512 /// [`write_batch`](Self::write_batch). Override only when the commit is
513 /// genuinely atomic (and return `true` from `supports_idempotent_writes`).
514 async fn write_batch_idempotent(
515 &self,
516 records: &[Value],
517 scope: &str,
518 token: &str,
519 ) -> Result<usize, FaucetError> {
520 let _ = (scope, token);
521 self.write_batch(records).await
522 }
523
524 /// The last token durably committed for `scope`, or `None` if this sink has
525 /// never committed under that scope. Default: `None`.
526 async fn last_committed_token(&self, scope: &str) -> Result<Option<String>, FaucetError> {
527 let _ = scope;
528 Ok(None)
529 }
530
531 /// Return a JSON Schema describing the configuration this sink accepts.
532 ///
533 /// The schema is auto-generated from the config struct using `schemars`.
534 /// Callers can inspect it to discover required fields, types, defaults,
535 /// and descriptions before constructing the sink.
536 ///
537 /// The default returns an empty object schema.
538 fn config_schema(&self) -> Value {
539 serde_json::json!({"type": "object", "properties": {}})
540 }
541
542 /// Stable identifier used as the `connector` label on metrics and the
543 /// `connector` attribute on spans. See `Source::connector_name`.
544 fn connector_name(&self) -> &'static str {
545 crate::observability::strip_type_name(std::any::type_name::<Self>())
546 }
547
548 /// Logical dataset identity for lineage emission, following OpenLineage
549 /// naming conventions (<https://openlineage.io/docs/spec/naming>).
550 ///
551 /// The default returns `"<connector_name>://unknown"`. Built-in connectors
552 /// override with a credential-free URI derived from their config. Strip any
553 /// credentials with [`redact_uri_credentials`](crate::redact_uri_credentials).
554 /// Informational metadata only — never used for I/O.
555 fn dataset_uri(&self) -> String {
556 format!("{}://unknown", self.connector_name())
557 }
558
559 /// Run a fast, non-mutating preflight probe (used by `faucet doctor`).
560 ///
561 /// Unlike sources, a sink has no non-mutating "first page" equivalent
562 /// (`write_batch` mutates the destination), so the default returns
563 /// [`CheckReport::not_implemented`](crate::check::CheckReport::not_implemented).
564 /// Built-in sinks override this with a connect / auth / metadata probe.
565 ///
566 /// The probe **MUST be idempotent and side-effect-free** — no inserts, no
567 /// residual rows or objects — and must never put credentials or connection
568 /// strings in a probe `reason`/`hint`.
569 async fn check(
570 &self,
571 _ctx: &crate::check::CheckContext,
572 ) -> Result<crate::check::CheckReport, FaucetError> {
573 Ok(crate::check::CheckReport::not_implemented())
574 }
575}
576
577#[cfg(test)]
578mod tests {
579 use super::*;
580 use serde_json::json;
581
582 // ── Mock Source ──────────────────────────────────────────────────────────
583
584 struct MockSource {
585 records: Vec<Value>,
586 }
587
588 #[async_trait]
589 impl Source for MockSource {
590 async fn fetch_with_context(
591 &self,
592 _context: &std::collections::HashMap<String, Value>,
593 ) -> Result<Vec<Value>, FaucetError> {
594 Ok(self.records.clone())
595 }
596 }
597
598 struct IncrementalSource {
599 records: Vec<Value>,
600 bookmark: Value,
601 }
602
603 #[async_trait]
604 impl Source for IncrementalSource {
605 async fn fetch_with_context(
606 &self,
607 _context: &std::collections::HashMap<String, Value>,
608 ) -> Result<Vec<Value>, FaucetError> {
609 Ok(self.records.clone())
610 }
611
612 async fn fetch_with_context_incremental(
613 &self,
614 _context: &std::collections::HashMap<String, Value>,
615 ) -> Result<(Vec<Value>, Option<Value>), FaucetError> {
616 Ok((self.records.clone(), Some(self.bookmark.clone())))
617 }
618 }
619
620 struct FailingSource;
621
622 #[async_trait]
623 impl Source for FailingSource {
624 async fn fetch_with_context(
625 &self,
626 _context: &std::collections::HashMap<String, Value>,
627 ) -> Result<Vec<Value>, FaucetError> {
628 Err(FaucetError::Auth("no credentials".into()))
629 }
630 }
631
632 // ── Mock Sink ───────────────────────────────────────────────────────────
633
634 struct MockSink {
635 written: std::sync::Mutex<Vec<Value>>,
636 }
637
638 impl MockSink {
639 fn new() -> Self {
640 Self {
641 written: std::sync::Mutex::new(Vec::new()),
642 }
643 }
644 }
645
646 #[async_trait]
647 impl Sink for MockSink {
648 async fn write_batch(&self, records: &[Value]) -> Result<usize, FaucetError> {
649 let mut w = self.written.lock().unwrap();
650 w.extend(records.iter().cloned());
651 Ok(records.len())
652 }
653 }
654
655 struct FailingSink;
656
657 #[async_trait]
658 impl Sink for FailingSink {
659 async fn write_batch(&self, _records: &[Value]) -> Result<usize, FaucetError> {
660 Err(FaucetError::Sink("write failed".into()))
661 }
662 }
663
664 // ── Source tests ────────────────────────────────────────────────────────
665
666 #[tokio::test]
667 async fn source_fetch_all_returns_records() {
668 let source = MockSource {
669 records: vec![json!({"id": 1}), json!({"id": 2})],
670 };
671 let records = source.fetch_all().await.unwrap();
672 assert_eq!(records.len(), 2);
673 assert_eq!(records[0]["id"], 1);
674 }
675
676 #[tokio::test]
677 async fn source_fetch_all_empty() {
678 let source = MockSource { records: vec![] };
679 let records = source.fetch_all().await.unwrap();
680 assert!(records.is_empty());
681 }
682
683 #[tokio::test]
684 async fn source_default_incremental_returns_none_bookmark() {
685 let source = MockSource {
686 records: vec![json!({"id": 1})],
687 };
688 let (records, bookmark) = source.fetch_all_incremental().await.unwrap();
689 assert_eq!(records.len(), 1);
690 assert!(bookmark.is_none());
691 }
692
693 #[tokio::test]
694 async fn source_custom_incremental_returns_bookmark() {
695 let source = IncrementalSource {
696 records: vec![json!({"id": 1})],
697 bookmark: json!("2024-12-01"),
698 };
699 let (records, bookmark) = source.fetch_all_incremental().await.unwrap();
700 assert_eq!(records.len(), 1);
701 assert_eq!(bookmark, Some(json!("2024-12-01")));
702 }
703
704 #[tokio::test]
705 async fn source_error_propagates() {
706 let source = FailingSource;
707 let result = source.fetch_all().await;
708 assert!(result.is_err());
709 assert!(matches!(result, Err(FaucetError::Auth(_))));
710 }
711
712 #[tokio::test]
713 async fn source_as_trait_object() {
714 let source: Box<dyn Source> = Box::new(MockSource {
715 records: vec![json!({"id": 42})],
716 });
717 let records = source.fetch_all().await.unwrap();
718 assert_eq!(records[0]["id"], 42);
719 }
720
721 // ── Sink tests ──────────────────────────────────────────────────────────
722
723 #[tokio::test]
724 async fn sink_write_batch_returns_count() {
725 let sink = MockSink::new();
726 let records = vec![json!({"id": 1}), json!({"id": 2}), json!({"id": 3})];
727 let count = sink.write_batch(&records).await.unwrap();
728 assert_eq!(count, 3);
729 }
730
731 #[tokio::test]
732 async fn sink_write_batch_empty() {
733 let sink = MockSink::new();
734 let count = sink.write_batch(&[]).await.unwrap();
735 assert_eq!(count, 0);
736 }
737
738 #[tokio::test]
739 async fn sink_accumulates_records() {
740 let sink = MockSink::new();
741 sink.write_batch(&[json!({"a": 1})]).await.unwrap();
742 sink.write_batch(&[json!({"b": 2})]).await.unwrap();
743 let written = sink.written.lock().unwrap();
744 assert_eq!(written.len(), 2);
745 }
746
747 #[tokio::test]
748 async fn sink_default_flush_is_noop() {
749 let sink = MockSink::new();
750 assert!(sink.flush().await.is_ok());
751 }
752
753 #[tokio::test]
754 async fn sink_error_propagates() {
755 let sink = FailingSink;
756 let result = sink.write_batch(&[json!({"id": 1})]).await;
757 assert!(result.is_err());
758 assert!(matches!(result, Err(FaucetError::Sink(_))));
759 }
760
761 #[tokio::test]
762 async fn sink_as_trait_object() {
763 let sink: Box<dyn Sink> = Box::new(MockSink::new());
764 let count = sink.write_batch(&[json!({"id": 1})]).await.unwrap();
765 assert_eq!(count, 1);
766 }
767
768 // ── stream_pages tests ──────────────────────────────────────────────────
769
770 use crate::pipeline::DEFAULT_BATCH_SIZE;
771 use futures::StreamExt;
772
773 #[tokio::test]
774 async fn default_stream_pages_chunks_records() {
775 let source = MockSource {
776 records: (0..5).map(|i| json!({"i": i})).collect(),
777 };
778 let ctx = std::collections::HashMap::new();
779 let mut pages = source.stream_pages(&ctx, 2);
780 let mut all = Vec::new();
781 while let Some(page) = pages.next().await {
782 all.push(page.unwrap());
783 }
784 // 5 records, batch_size=2 → pages of [2, 2, 1]
785 assert_eq!(all.len(), 3);
786 assert_eq!(all[0].records.len(), 2);
787 assert_eq!(all[1].records.len(), 2);
788 assert_eq!(all[2].records.len(), 1);
789 }
790
791 #[tokio::test]
792 async fn default_stream_pages_attaches_bookmark_to_final_page_only() {
793 let source = IncrementalSource {
794 records: (0..5).map(|i| json!({"i": i})).collect(),
795 bookmark: json!("v1"),
796 };
797 let ctx = std::collections::HashMap::new();
798 let mut pages = source.stream_pages(&ctx, 2);
799 let mut collected = Vec::new();
800 while let Some(page) = pages.next().await {
801 collected.push(page.unwrap());
802 }
803 assert_eq!(collected.len(), 3);
804 assert!(collected[0].bookmark.is_none());
805 assert!(collected[1].bookmark.is_none());
806 assert_eq!(collected[2].bookmark, Some(json!("v1")));
807 }
808
809 #[tokio::test]
810 async fn default_stream_pages_single_page_when_batch_size_exceeds_total() {
811 let source = MockSource {
812 records: vec![json!({"id": 1}), json!({"id": 2})],
813 };
814 let ctx = std::collections::HashMap::new();
815 let mut pages = source.stream_pages(&ctx, 100);
816 let mut collected = Vec::new();
817 while let Some(page) = pages.next().await {
818 collected.push(page.unwrap());
819 }
820 assert_eq!(collected.len(), 1);
821 assert_eq!(collected[0].records.len(), 2);
822 }
823
824 #[tokio::test]
825 async fn default_stream_pages_batch_size_zero_emits_single_page() {
826 // batch_size = 0 is the "no batching" sentinel — yields every record
827 // in one page regardless of total count.
828 let source = MockSource {
829 records: (0..50_000).map(|i| json!({"i": i})).collect(),
830 };
831 let ctx = std::collections::HashMap::new();
832 let mut pages = source.stream_pages(&ctx, 0);
833 let mut collected = Vec::new();
834 while let Some(page) = pages.next().await {
835 collected.push(page.unwrap());
836 }
837 assert_eq!(
838 collected.len(),
839 1,
840 "batch_size=0 must emit exactly one page"
841 );
842 assert_eq!(collected[0].records.len(), 50_000);
843 }
844
845 #[tokio::test]
846 async fn default_stream_pages_batch_size_zero_attaches_bookmark_to_sole_page() {
847 let source = IncrementalSource {
848 records: (0..3).map(|i| json!({"i": i})).collect(),
849 bookmark: json!("v1"),
850 };
851 let ctx = std::collections::HashMap::new();
852 let mut pages = source.stream_pages(&ctx, 0);
853 let page = pages.next().await.unwrap().unwrap();
854 assert_eq!(page.records.len(), 3);
855 assert_eq!(page.bookmark, Some(json!("v1")));
856 assert!(pages.next().await.is_none());
857 }
858
859 #[tokio::test]
860 async fn default_stream_pages_empty_source_yields_no_pages() {
861 let source = MockSource { records: vec![] };
862 let ctx = std::collections::HashMap::new();
863 let mut pages = source.stream_pages(&ctx, DEFAULT_BATCH_SIZE);
864 assert!(pages.next().await.is_none());
865 }
866
867 #[tokio::test]
868 async fn default_stream_pages_empty_source_with_bookmark_yields_single_empty_page() {
869 let source = IncrementalSource {
870 records: vec![],
871 bookmark: json!("v0"),
872 };
873 let ctx = std::collections::HashMap::new();
874 let mut pages = source.stream_pages(&ctx, DEFAULT_BATCH_SIZE);
875 let mut collected = Vec::new();
876 while let Some(page) = pages.next().await {
877 collected.push(page.unwrap());
878 }
879 // One empty-records page that carries the bookmark, so the pipeline
880 // still persists progress on otherwise-empty incremental runs.
881 assert_eq!(collected.len(), 1);
882 assert!(collected[0].records.is_empty());
883 assert_eq!(collected[0].bookmark, Some(json!("v0")));
884 }
885
886 #[tokio::test]
887 async fn default_stream_pages_propagates_fetch_errors() {
888 let source = FailingSource;
889 let ctx = std::collections::HashMap::new();
890 let mut pages = source.stream_pages(&ctx, DEFAULT_BATCH_SIZE);
891 let first = pages.next().await.unwrap();
892 assert!(matches!(first, Err(FaucetError::Auth(_))));
893 }
894
895 #[test]
896 fn source_default_connector_name_is_stripped_type_name() {
897 // MockSource lives at `faucet_core::traits::tests::MockSource`; the
898 // stripped type_name yields the trailing segment.
899 let source = MockSource { records: vec![] };
900 assert_eq!(source.connector_name(), "MockSource");
901 }
902
903 #[test]
904 fn sink_default_connector_name_is_stripped_type_name() {
905 let sink = MockSink::new();
906 assert_eq!(sink.connector_name(), "MockSink");
907 }
908
909 #[test]
910 fn source_default_dataset_uri_uses_connector_name() {
911 let source = MockSource { records: vec![] };
912 assert_eq!(source.dataset_uri(), "MockSource://unknown");
913 }
914
915 #[test]
916 fn sink_default_dataset_uri_uses_connector_name() {
917 let sink = MockSink::new();
918 assert_eq!(sink.dataset_uri(), "MockSink://unknown");
919 }
920
921 // ── write_batch_partial tests ───────────────────────────────────────────
922
923 #[tokio::test]
924 async fn default_write_batch_partial_success_returns_all_ok() {
925 let sink = MockSink::new();
926 let records = vec![json!({"id": 1}), json!({"id": 2}), json!({"id": 3})];
927 let outcomes = sink.write_batch_partial(&records).await.unwrap();
928 assert_eq!(outcomes.len(), 3);
929 assert!(outcomes.iter().all(|o| o.is_ok()));
930 assert_eq!(sink.written.lock().unwrap().len(), 3);
931 }
932
933 #[tokio::test]
934 async fn default_write_batch_partial_bubbles_outer_err() {
935 let sink = FailingSink;
936 let records = vec![json!({"id": 1}), json!({"id": 2})];
937 let result = sink.write_batch_partial(&records).await;
938 assert!(matches!(result, Err(FaucetError::Sink(_))));
939 }
940
941 #[tokio::test]
942 async fn default_write_batch_partial_empty_returns_empty_vec() {
943 let sink = MockSink::new();
944 let outcomes = sink.write_batch_partial(&[]).await.unwrap();
945 assert!(outcomes.is_empty());
946 }
947
948 #[tokio::test]
949 async fn default_write_batch_partial_callable_through_trait_object() {
950 let sink: Box<dyn Sink> = Box::new(MockSink::new());
951 let records = vec![json!({"id": 1}), json!({"id": 2})];
952 let outcomes = sink.write_batch_partial(&records).await.unwrap();
953 assert_eq!(outcomes.len(), 2);
954 assert!(outcomes.iter().all(|o| o.is_ok()));
955 }
956
957 // ── check() tests ─────────────────────────────────────────────────────────
958
959 #[tokio::test]
960 async fn source_default_check_pulls_first_page_and_passes() {
961 let source = MockSource {
962 records: vec![json!({"id": 1}), json!({"id": 2})],
963 };
964 let report = source
965 .check(&crate::check::CheckContext::default())
966 .await
967 .unwrap();
968 assert_eq!(report.failed_count(), 0);
969 assert!(
970 report
971 .probes
972 .iter()
973 .any(|p| p.name == "read" && matches!(p.status, crate::check::ProbeStatus::Pass))
974 );
975 }
976
977 #[tokio::test]
978 async fn source_default_check_passes_on_empty_source() {
979 let source = MockSource { records: vec![] };
980 let report = source
981 .check(&crate::check::CheckContext::default())
982 .await
983 .unwrap();
984 // Reachable but empty is still a healthy source.
985 assert_eq!(report.failed_count(), 0);
986 }
987
988 #[tokio::test]
989 async fn source_default_check_fails_when_fetch_errors() {
990 let source = FailingSource;
991 let report = source
992 .check(&crate::check::CheckContext::default())
993 .await
994 .unwrap();
995 assert_eq!(report.failed_count(), 1);
996 assert!(report.probes.iter().any(
997 |p| p.name == "read" && matches!(p.status, crate::check::ProbeStatus::Fail { .. })
998 ));
999 }
1000
1001 #[tokio::test]
1002 async fn sink_default_check_is_not_implemented_skip() {
1003 let sink = MockSink::new();
1004 let report = sink
1005 .check(&crate::check::CheckContext::default())
1006 .await
1007 .unwrap();
1008 assert_eq!(report.probes.len(), 1);
1009 assert!(matches!(
1010 report.probes[0].status,
1011 crate::check::ProbeStatus::Skip { .. }
1012 ));
1013 }
1014
1015 #[tokio::test]
1016 async fn source_check_callable_through_trait_object() {
1017 let source: Box<dyn Source> = Box::new(MockSource {
1018 records: vec![json!({"id": 1})],
1019 });
1020 let report = source
1021 .check(&crate::check::CheckContext::default())
1022 .await
1023 .unwrap();
1024 assert_eq!(report.failed_count(), 0);
1025 }
1026
1027 // ── idempotent-write / exactly-once capability tests ──────────────────────
1028
1029 #[tokio::test]
1030 async fn sink_default_is_not_idempotent() {
1031 let sink = MockSink::new();
1032 assert!(!sink.supports_idempotent_writes());
1033 // Default write_batch_idempotent ignores the token and delegates.
1034 let n = sink
1035 .write_batch_idempotent(&[json!({"id": 1})], "scope::a", "00000000000000000001")
1036 .await
1037 .unwrap();
1038 assert_eq!(n, 1);
1039 assert_eq!(sink.last_committed_token("scope::a").await.unwrap(), None);
1040 assert_eq!(sink.written.lock().unwrap().len(), 1);
1041 }
1042
1043 #[test]
1044 fn source_default_does_not_support_exactly_once() {
1045 let source = MockSource { records: vec![] };
1046 assert!(!source.supports_exactly_once());
1047 }
1048
1049 #[test]
1050 fn sink_default_supported_write_modes_is_append_only() {
1051 use crate::write_mode::WriteMode;
1052 let sink = MockSink::new();
1053 assert_eq!(sink.supported_write_modes(), &[WriteMode::Append]);
1054 }
1055
1056 #[test]
1057 fn supported_write_modes_callable_through_trait_object() {
1058 use crate::write_mode::WriteMode;
1059 let sink: Box<dyn Sink> = Box::new(MockSink::new());
1060 assert!(sink.supported_write_modes().contains(&WriteMode::Append));
1061 }
1062
1063 #[tokio::test]
1064 async fn sink_default_current_schema_is_none() {
1065 let sink = MockSink::new();
1066 assert_eq!(sink.current_schema().await.unwrap(), None);
1067 }
1068
1069 #[test]
1070 fn sink_default_does_not_support_schema_evolution() {
1071 let sink = MockSink::new();
1072 assert!(!sink.supports_schema_evolution());
1073 }
1074
1075 #[tokio::test]
1076 async fn sink_default_evolve_schema_is_unsupported_error() {
1077 let sink = MockSink::new();
1078 let evo = crate::drift::SchemaEvolution::default();
1079 let err = sink.evolve_schema(&evo).await.unwrap_err();
1080 assert!(matches!(err, FaucetError::Sink(_)));
1081 assert!(err.to_string().contains("schema evolution"));
1082 }
1083
1084 #[tokio::test]
1085 async fn source_default_capture_resume_position_is_none() {
1086 let source = MockSource { records: vec![] };
1087 assert_eq!(source.capture_resume_position().await.unwrap(), None);
1088 }
1089
1090 #[tokio::test]
1091 async fn capture_resume_position_callable_through_trait_object() {
1092 let source: Box<dyn Source> = Box::new(MockSource { records: vec![] });
1093 assert!(source.capture_resume_position().await.unwrap().is_none());
1094 }
1095
1096 #[tokio::test]
1097 async fn source_default_does_not_support_discover() {
1098 let source: Box<dyn Source> = Box::new(MockSource { records: vec![] });
1099 assert!(!source.supports_discover());
1100 let err = source.discover().await.unwrap_err();
1101 assert!(matches!(err, FaucetError::Source(_)));
1102 assert!(
1103 err.to_string().contains("dataset discovery"),
1104 "typed unsupported error: {err}"
1105 );
1106 }
1107
1108 #[tokio::test]
1109 async fn source_default_is_not_shardable() {
1110 let source: Box<dyn Source> = Box::new(MockSource { records: vec![] });
1111 assert!(!source.is_shardable());
1112 }
1113
1114 #[tokio::test]
1115 async fn source_default_enumerates_single_whole_shard() {
1116 // A non-shardable source enumerates to exactly one whole-dataset shard,
1117 // regardless of the requested target — preserving single-worker behavior.
1118 let source: Box<dyn Source> = Box::new(MockSource { records: vec![] });
1119 let shards = source.enumerate_shards(8).await.unwrap();
1120 assert_eq!(shards.len(), 1);
1121 assert!(shards[0].is_whole());
1122 }
1123
1124 #[tokio::test]
1125 async fn source_default_apply_shard_is_noop() {
1126 let source: Box<dyn Source> = Box::new(MockSource { records: vec![] });
1127 // Applying the whole shard is a no-op and must not error.
1128 source
1129 .apply_shard(&crate::shard::ShardSpec::whole())
1130 .await
1131 .unwrap();
1132 }
1133}