Skip to main content

feldera_storage/
checkpoint_synchronizer.rs

1use std::sync::{Arc, LazyLock};
2
3use feldera_types::{
4    checkpoint::{CheckpointMetadata, CheckpointSyncMetrics, HostInfo, RemoteCheckpoint},
5    config::{PipelineIdentity, SyncConfig},
6};
7
8use crate::StorageBackend;
9
10pub trait CheckpointSynchronizer: Sync {
11    /// Push a checkpoint to remote object storage.
12    ///
13    /// `host_info` identifies the calling host within a multihost pipeline.
14    /// When `Some`, the checkpoint zip and catalog are written under
15    /// `host{N}/` in the remote bucket; when `None` (solo pipeline), the
16    /// existing flat layout is used for backward compatibility.
17    ///
18    /// `pipeline` identifies the pipeline performing the push and is used to
19    /// enforce bucket ownership: the push fails before writing any data if the
20    /// bucket is already owned by a different pipeline.
21    fn push(
22        &self,
23        checkpoint: uuid::Uuid,
24        storage: Arc<dyn StorageBackend>,
25        remote_config: SyncConfig,
26        host_info: Option<HostInfo>,
27        pipeline: PipelineIdentity,
28    ) -> anyhow::Result<Option<CheckpointSyncMetrics>>;
29
30    /// Pull a checkpoint from remote object storage.
31    ///
32    /// `host_info` scopes the pull to the correct `host{N}/` subdirectory.
33    /// Pass `None` for solo pipelines to use the existing flat layout.
34    ///
35    /// `standby` indicates that the pipeline is in standby mode: the
36    /// local-storage cache is bypassed (always pull from remote) and a missing
37    /// remote checkpoint is treated as an error rather than a fresh start.
38    ///
39    /// `pipeline` identifies the pipeline performing the pull. It is used only
40    /// to warn (never fail) when pulling from a bucket owned by a different
41    /// pipeline.
42    fn pull(
43        &self,
44        storage: Arc<dyn StorageBackend>,
45        remote_config: SyncConfig,
46        host_info: Option<HostInfo>,
47        standby: bool,
48        pipeline: PipelineIdentity,
49    ) -> anyhow::Result<(CheckpointMetadata, Option<CheckpointSyncMetrics>)>;
50
51    /// Takes ownership of the remote bucket for `pipeline` as it starts.
52    ///
53    /// Called once per run, before the pipeline starts processing, when
54    /// `remote_config.take_bucket_ownership` is set.  Claims an unowned
55    /// bucket, refreshes the ownership of a bucket `pipeline` already owns,
56    /// and takes over a bucket owned by a different pipeline, logging a
57    /// warning that names the previous and new owners.  [`Self::push`] never
58    /// takes ownership, so if another pipeline takes the bucket over later in
59    /// this run, the next push fails.
60    ///
61    /// Every host of a multihost pipeline calls this as it starts.  The hosts
62    /// share `pipeline`, so their concurrent writes agree, and a write by
63    /// another host of the same pipeline counts as success.
64    ///
65    /// The default implementation fails, reporting `take_bucket_ownership` as
66    /// unsupported.  It keeps this method optional, so that a synchronizer
67    /// that predates it still compiles: the enterprise synchronizer is built
68    /// from a separate repository, which may lag behind this trait, and a
69    /// runtime version override builds a newer runtime against an older
70    /// platform's synchronizer.
71    ///
72    /// # Arguments
73    /// - `storage`: local storage, used to stage the ownership file.
74    /// - `remote_config`: sync settings; `bucket` is the location to take.
75    /// - `pipeline`: identity of the pipeline taking ownership.
76    ///
77    /// # Returns
78    /// `Ok(())` once the bucket's ownership file names `pipeline`; an error if
79    /// it could not be written, a different pipeline overwrote it at the same
80    /// moment, or the synchronizer does not support taking ownership.
81    fn take_ownership(
82        &self,
83        _storage: Arc<dyn StorageBackend>,
84        _remote_config: SyncConfig,
85        _pipeline: PipelineIdentity,
86    ) -> anyhow::Result<()> {
87        anyhow::bail!(
88            "this build's checkpoint synchronizer does not support `take_bucket_ownership`"
89        )
90    }
91
92    /// List checkpoints available in remote object storage.
93    fn list_remote(&self, remote_config: SyncConfig) -> anyhow::Result<Vec<RemoteCheckpoint>>;
94}
95
96inventory::collect!(&'static dyn CheckpointSynchronizer);
97
98/// Lazily resolves the checkpoint synchronizer.
99///
100/// This panic is safe as all enterprise builds must include the checkpoint-sync
101/// crate.
102pub static SYNCHRONIZER: LazyLock<&'static dyn CheckpointSynchronizer> = LazyLock::new(|| {
103    *inventory::iter::<&dyn CheckpointSynchronizer>
104        .into_iter()
105        .next()
106        .expect("no checkpoint synchronizer found; are enterprise features enabled?")
107});