boatramp_node/backends.rs
1//! Backend-selection enums for a node's blob + KV stores (node-library N2b).
2//!
3//! These are the domain types the store-construction assembly dispatches on. They
4//! live here (not the binary) so the assembly can move into the library; the
5//! `clap::ValueEnum` derive is behind the optional `clap` feature so the CLI
6//! binary uses them directly in its args, while a non-CLI embedder never pulls
7//! clap. (`build_kv`/`build_blobs` join this module as they migrate off the
8//! binary's `ServeArgs`.)
9
10use std::path::Path;
11use std::sync::Arc;
12
13use boatramp_core::kv::{KvOpenPolicy, KvStore, MemoryKv};
14
15use crate::error::Result;
16
17/// Blob (file-content) backend.
18///
19/// `Deserialize` (lowercase: `fs`/`s3`/`gcs`/`azure`) so `[serve].blobs` in `boatramp.cfg` can
20/// select the backend — the config-level analog of the `--blobs` flag, which `boatramp blob
21/// migrate` reads to build a source/destination backend from a config file alone.
22#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize)]
23#[serde(rename_all = "lowercase")]
24#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
25pub enum BlobBackend {
26 /// Local filesystem (`<data-dir>/blobs`).
27 Fs,
28 /// S3-compatible object store (requires `--features s3`).
29 S3,
30 /// Google Cloud Storage (requires `--features gcs`).
31 Gcs,
32 /// Azure Blob Storage (requires `--features azure`).
33 Azure,
34}
35
36/// Metadata (manifest + pointer) backend.
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
39pub enum KvBackend {
40 /// Transactional LSM over object storage; durable local default
41 /// (`<data-dir>/kv-slate`). Requires `--features slatedb` (on by default).
42 Slatedb,
43 /// In-memory (ephemeral; lost on restart).
44 Memory,
45 /// Cloudflare KV over REST (requires `--features cloudflare-kv`).
46 Cloudflare,
47}
48
49/// Flush interval for the control-plane SlateDB store: tiny, so a control-plane
50/// write is durable almost immediately (correctness over throughput).
51pub const CONTROL_PLANE_FLUSH: std::time::Duration = std::time::Duration::from_millis(5);
52
53/// Where the SlateDB control-plane store lives when it runs on an S3-compatible
54/// object store (Cloudflare R2) instead of local disk — the durable, remote-state
55/// deployment. Credentials come from the ambient AWS environment
56/// (`AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY`), matching the S3 blob backend.
57#[derive(Debug, Clone)]
58pub struct SlateKvS3 {
59 /// The bucket the store lives in (shared with S3 blobs, under `prefix`).
60 pub bucket: String,
61 /// Custom endpoint (R2: `https://<account>.r2.cloudflarestorage.com`).
62 pub endpoint: Option<String>,
63 /// Region (R2 uses `auto`).
64 pub region: Option<String>,
65 /// Use path-style addressing (R2 accepts it).
66 pub path_style: bool,
67 /// Key prefix within the bucket (keeps the LSM files apart from the blobs).
68 pub prefix: String,
69}
70
71/// Build the metadata KV store for the selected [`KvBackend`]. When `slate_s3` is
72/// set (and the backend is SlateDB), the store runs on R2/S3 (durable across a
73/// scale-to-zero container stop) rather than the local `data_dir`.
74///
75/// `policy` (v0.9.0 KV-recovery, C3/C7/C8/C11) is the cold-open recovery policy for the SlateDB
76/// backend (a no-op for Memory/Cloudflare). This is the SINGLE-NODE control-plane open site, so its
77/// default is [`KvOpenPolicy::SelfHeal`]; the caller passes [`KvOpenPolicy::Strict`] for
78/// `--strict-kv`. (The cluster node-local Raft store opens elsewhere and stays strict — C8.)
79pub async fn build_kv(
80 kv: KvBackend,
81 data_dir: &Path,
82 slate_s3: Option<&SlateKvS3>,
83 policy: KvOpenPolicy,
84) -> Result<Arc<dyn KvStore>> {
85 match kv {
86 KvBackend::Slatedb => build_slatedb_kv(data_dir, slate_s3, policy).await,
87 KvBackend::Memory => Ok(Arc::new(MemoryKv::new())),
88 KvBackend::Cloudflare => build_cloudflare_kv(),
89 }
90}
91
92#[cfg(feature = "slatedb")]
93async fn build_slatedb_kv(
94 data_dir: &Path,
95 slate_s3: Option<&SlateKvS3>,
96 policy: KvOpenPolicy,
97) -> Result<Arc<dyn KvStore>> {
98 match slate_s3 {
99 Some(s3) => Ok(Arc::new(
100 boatramp_storage::SlateKv::open_s3_with_flush_policy(
101 &boatramp_storage::S3StoreConfig {
102 bucket: s3.bucket.clone(),
103 endpoint: s3.endpoint.clone(),
104 region: s3.region.clone(),
105 path_style: s3.path_style,
106 },
107 &s3.prefix,
108 CONTROL_PLANE_FLUSH,
109 policy,
110 )
111 .await?,
112 )),
113 None => Ok(Arc::new(
114 boatramp_storage::SlateKv::open_local_with_flush_policy(
115 data_dir.join("kv-slate"),
116 CONTROL_PLANE_FLUSH,
117 policy,
118 )
119 .await?,
120 )),
121 }
122}
123
124#[cfg(not(feature = "slatedb"))]
125async fn build_slatedb_kv(
126 _data_dir: &Path,
127 _slate_s3: Option<&SlateKvS3>,
128 _policy: KvOpenPolicy,
129) -> Result<Arc<dyn KvStore>> {
130 Err(crate::error::Error::NoSlatedbSupport)
131}
132
133#[cfg(feature = "cloudflare-kv")]
134fn build_cloudflare_kv() -> Result<Arc<dyn KvStore>> {
135 Ok(Arc::new(boatramp_storage::CloudflareKv::from_env()?))
136}
137
138#[cfg(not(feature = "cloudflare-kv"))]
139fn build_cloudflare_kv() -> Result<Arc<dyn KvStore>> {
140 Err(crate::error::Error::NoCloudflareKvSupport)
141}