Skip to main content

rhei_core/traits/
sync.rs

1//! [`SyncEngine`] trait — CDC-to-OLAP replication.
2//!
3//! The production implementation (`CdcSyncEngine` in `rhei-sync`) polls a
4//! [`crate::CdcConsumer`], converts events to DML via the sync converter, and
5//! applies them to an [`crate::OlapEngine`].
6
7use crate::types::{SyncResult, SyncStatus};
8
9/// Applies CDC events from the OLTP engine to the OLAP engine.
10///
11/// The sync loop (in `rhei-sync`) calls [`SyncEngine::sync_once`] repeatedly
12/// on a configurable interval. A [`crate::SyncMode`] controls whether the OLAP
13/// table is kept as a mirror (`Destructive`) or an append-only temporal store
14/// (`Temporal` / SCD Type 2).
15///
16/// # Contract for implementors
17///
18/// - [`sync_once`](SyncEngine::sync_once) must be idempotent with respect to
19///   network/storage failures: a partially-applied cycle must be recoverable by
20///   re-running with the same watermark.
21/// - [`status`](SyncEngine::status) must be cheap and non-blocking.
22pub trait SyncEngine: Send + Sync {
23    /// Engine-specific error type returned by all fallible methods.
24    type Error: std::error::Error + Send + Sync + 'static;
25
26    /// Poll the CDC log and apply any new events to the OLAP engine.
27    ///
28    /// Returns a [`SyncResult`] summarising the work done. If there are no new
29    /// events, the result will have all counters set to zero and `last_seq =
30    /// None`.
31    ///
32    /// # Errors
33    ///
34    /// Returns `Err(Self::Error)` if either the CDC consumer or the OLAP engine
35    /// encounters an unrecoverable failure.
36    fn sync_once(
37        &self,
38    ) -> impl std::future::Future<Output = Result<SyncResult, Self::Error>> + Send;
39
40    /// Return a snapshot of the current replication state.
41    ///
42    /// # Errors
43    ///
44    /// Returns `Err(Self::Error)` if the underlying status query fails.
45    fn status(&self) -> impl std::future::Future<Output = Result<SyncStatus, Self::Error>> + Send;
46}