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::{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`.
74pub async fn build_kv(
75 kv: KvBackend,
76 data_dir: &Path,
77 slate_s3: Option<&SlateKvS3>,
78) -> Result<Arc<dyn KvStore>> {
79 match kv {
80 KvBackend::Slatedb => build_slatedb_kv(data_dir, slate_s3).await,
81 KvBackend::Memory => Ok(Arc::new(MemoryKv::new())),
82 KvBackend::Cloudflare => build_cloudflare_kv(),
83 }
84}
85
86#[cfg(feature = "slatedb")]
87async fn build_slatedb_kv(
88 data_dir: &Path,
89 slate_s3: Option<&SlateKvS3>,
90) -> Result<Arc<dyn KvStore>> {
91 match slate_s3 {
92 Some(s3) => Ok(Arc::new(
93 boatramp_storage::SlateKv::open_s3_with_flush(
94 &boatramp_storage::S3StoreConfig {
95 bucket: s3.bucket.clone(),
96 endpoint: s3.endpoint.clone(),
97 region: s3.region.clone(),
98 path_style: s3.path_style,
99 },
100 &s3.prefix,
101 CONTROL_PLANE_FLUSH,
102 )
103 .await?,
104 )),
105 None => Ok(Arc::new(
106 boatramp_storage::SlateKv::open_local_with_flush(
107 data_dir.join("kv-slate"),
108 CONTROL_PLANE_FLUSH,
109 )
110 .await?,
111 )),
112 }
113}
114
115#[cfg(not(feature = "slatedb"))]
116async fn build_slatedb_kv(
117 _data_dir: &Path,
118 _slate_s3: Option<&SlateKvS3>,
119) -> Result<Arc<dyn KvStore>> {
120 Err(crate::error::Error::NoSlatedbSupport)
121}
122
123#[cfg(feature = "cloudflare-kv")]
124fn build_cloudflare_kv() -> Result<Arc<dyn KvStore>> {
125 Ok(Arc::new(boatramp_storage::CloudflareKv::from_env()?))
126}
127
128#[cfg(not(feature = "cloudflare-kv"))]
129fn build_cloudflare_kv() -> Result<Arc<dyn KvStore>> {
130 Err(crate::error::Error::NoCloudflareKvSupport)
131}