Skip to main content

arcly_http/data/
db.rs

1//! Unified database facade — one handle, three ecosystems.
2//!
3//! `ArclyDbPool` is the single type user code injects regardless of which
4//! driver is compiled in (`db-sqlx`, `db-seaorm`, `db-diesel`). It implements
5//! [`DataSource`], so it slots straight into the existing
6//! `DataSourceRegistry` — tenant routing, read/write splitting, and
7//! `ReadAfterWritePin` all apply unchanged, because primary/replica selection
8//! happens here at the facade, before any driver is touched.
9//!
10//! ## Zero-lock guarantees
11//!
12//! - Replica selection: one `AtomicUsize` round-robin.
13//! - SQLx / SeaORM pools are internally lock-free async acquires.
14//! - Diesel (sync core) never runs on a worker thread: both `pool.get()` and
15//!   the query closure execute inside `spawn_blocking`
16//!   (see `data::drivers::diesel`).
17//!
18//! ## Feature gating
19//!
20//! This module always compiles. Driver variants exist only when their Cargo
21//! feature is enabled; with no `db-*` feature, the enums are uninhabited and
22//! every operation reports a configuration error at the call site.
23
24use std::sync::atomic::{AtomicUsize, Ordering};
25
26use futures::future::BoxFuture;
27
28use crate::data::{AccessIntent, DataError, DataSource};
29
30// ─── Driver wrapper ───────────────────────────────────────────────────────────
31
32/// One concrete connection pool. Variants are feature-gated; constructors
33/// live in `data::drivers::*`.
34pub enum DbDriver {
35    #[cfg(feature = "db-sqlx")]
36    Sqlx(sqlx::AnyPool),
37    #[cfg(feature = "db-seaorm")]
38    SeaOrm(sea_orm::DatabaseConnection),
39    #[cfg(feature = "db-diesel")]
40    Diesel(crate::data::drivers::diesel::DieselBlockingPool),
41}
42
43/// An acquired handle, decoupled from the pool's lifetime so it can be held
44/// across `.await` points and moved into transactions.
45pub enum OwnedDbConn {
46    #[cfg(feature = "db-sqlx")]
47    Sqlx(sqlx::pool::PoolConnection<sqlx::Any>),
48    #[cfg(feature = "db-seaorm")]
49    SeaOrm(sea_orm::DatabaseConnection),
50    #[cfg(feature = "db-diesel")]
51    Diesel(crate::data::drivers::diesel::DieselBlockingPool),
52}
53
54// ─── Facade pool ──────────────────────────────────────────────────────────────
55
56/// Primary + replicas for one logical database. Build at boot (plugin
57/// `on_init`), register into `DataSourceRegistry<ArclyDbPool>`, freeze.
58pub struct ArclyDbPool {
59    name: &'static str,
60    primary: DbDriver,
61    replicas: Vec<DbDriver>,
62    rr: AtomicUsize,
63}
64
65impl ArclyDbPool {
66    pub fn new(name: &'static str, primary: DbDriver) -> Self {
67        Self {
68            name,
69            primary,
70            replicas: Vec::new(),
71            rr: AtomicUsize::new(0),
72        }
73    }
74
75    /// Add a read replica (boot-time only — consumes `self`).
76    pub fn with_replica(mut self, replica: DbDriver) -> Self {
77        self.replicas.push(replica);
78        self
79    }
80
81    /// Writes → primary; reads → replica round-robin (primary when none).
82    /// `true` in the second tuple slot when a replica was chosen — the
83    /// acquire path uses it to fall back to the primary on replica failure.
84    pub(crate) fn pick(&self, intent: AccessIntent) -> (&DbDriver, bool) {
85        match intent {
86            AccessIntent::Write => (&self.primary, false),
87            AccessIntent::Read if self.replicas.is_empty() => (&self.primary, false),
88            AccessIntent::Read => {
89                let i = self.rr.fetch_add(1, Ordering::Relaxed);
90                (&self.replicas[i % self.replicas.len()], true)
91            }
92        }
93    }
94
95    /// The primary driver — transactions always run here.
96    pub(crate) fn primary(&self) -> &DbDriver {
97        &self.primary
98    }
99
100    /// Acquire from one concrete driver.
101    #[allow(unused_variables, unreachable_code)]
102    async fn acquire_driver(&self, driver: &DbDriver) -> Result<OwnedDbConn, DataError> {
103        match driver {
104            #[cfg(feature = "db-sqlx")]
105            DbDriver::Sqlx(pool) => Ok(OwnedDbConn::Sqlx(
106                pool.acquire().await.map_err(|e| DataError(e.to_string()))?,
107            )),
108            #[cfg(feature = "db-seaorm")]
109            DbDriver::SeaOrm(conn) => Ok(OwnedDbConn::SeaOrm(conn.clone())),
110            #[cfg(feature = "db-diesel")]
111            DbDriver::Diesel(pool) => Ok(OwnedDbConn::Diesel(pool.clone())),
112            #[allow(unreachable_patterns)]
113            _ => Err(DataError(
114                "no database driver feature enabled (db-sqlx / db-seaorm / db-diesel)".into(),
115            )),
116        }
117    }
118}
119
120impl DataSource for ArclyDbPool {
121    type Conn = OwnedDbConn;
122
123    /// Reads that land on a replica **fail over to the primary** when the
124    /// replica acquire fails — one dead replica must degrade read capacity,
125    /// not turn 1/N of reads into errors until a restart. Acquire outcomes
126    /// are labelled per pool in Prometheus.
127    fn acquire(&self, intent: AccessIntent) -> BoxFuture<'_, Result<Self::Conn, DataError>> {
128        Box::pin(async move {
129            let started = std::time::Instant::now();
130            let (driver, is_replica) = self.pick(intent);
131
132            let result = match self.acquire_driver(driver).await {
133                Ok(conn) => Ok(conn),
134                Err(replica_err) if is_replica => {
135                    metrics::counter!("db_replica_fallback_total", "pool" => self.name)
136                        .increment(1);
137                    tracing::warn!(
138                        pool = self.name,
139                        error = %replica_err,
140                        "replica acquire failed — falling back to primary"
141                    );
142                    self.acquire_driver(&self.primary).await
143                }
144                Err(e) => Err(e),
145            };
146
147            metrics::histogram!("db_acquire_seconds", "pool" => self.name)
148                .record(started.elapsed().as_secs_f64());
149            if result.is_err() {
150                metrics::counter!("db_acquire_errors_total", "pool" => self.name).increment(1);
151            }
152            result
153        })
154    }
155
156    fn name(&self) -> &'static str {
157        self.name
158    }
159}
160
161// ─── Health integration ───────────────────────────────────────────────────────
162
163impl ArclyDbPool {
164    /// Liveness ping against the **primary** driver — `SELECT 1` for SQLx,
165    /// the native `ping()` for SeaORM, a pooled checkout for Diesel.
166    /// Cheap enough for probe handlers (which additionally bound every
167    /// check with their own per-check timeout).
168    #[allow(unreachable_code)]
169    pub async fn ping(&self) -> Result<(), DataError> {
170        match self.primary() {
171            #[cfg(feature = "db-sqlx")]
172            DbDriver::Sqlx(pool) => {
173                sqlx::query_scalar::<_, i64>("SELECT 1")
174                    .fetch_one(pool)
175                    .await
176                    .map_err(|e| DataError(e.to_string()))?;
177                Ok(())
178            }
179            #[cfg(feature = "db-seaorm")]
180            DbDriver::SeaOrm(conn) => conn.ping().await.map_err(|e| DataError(e.to_string())),
181            #[cfg(feature = "db-diesel")]
182            DbDriver::Diesel(pool) => pool.ping().await,
183            #[allow(unreachable_patterns)]
184            _ => Err(DataError("no database driver feature enabled".into())),
185        }
186    }
187}
188
189/// Readiness probe over every pool in a [`DataSourceRegistry`](crate::data::DataSourceRegistry): pings each
190/// primary and reports the first failure as `Unhealthy` naming the pool.
191///
192/// Register from `on_start`, where the frozen container hands out `&'static`
193/// references:
194///
195/// ```ignore
196/// let registry = container.get::<DataSourceRegistry<ArclyDbPool>>();
197/// health::global().register("database", DbHealthCheck::new(registry));
198/// ```
199pub struct DbHealthCheck {
200    registry: &'static crate::data::DataSourceRegistry<ArclyDbPool>,
201}
202
203impl DbHealthCheck {
204    pub fn new(registry: &'static crate::data::DataSourceRegistry<ArclyDbPool>) -> Self {
205        Self { registry }
206    }
207}
208
209impl crate::observability::health::HealthCheck for DbHealthCheck {
210    fn check(&self) -> BoxFuture<'_, crate::observability::health::HealthStatus> {
211        use crate::observability::health::HealthStatus;
212        Box::pin(async move {
213            for (name, pool) in self.registry.iter() {
214                if let Err(e) = pool.ping().await {
215                    let label = if name.is_empty() { "default" } else { name };
216                    return HealthStatus::Unhealthy(format!("pool `{label}`: {e}"));
217                }
218            }
219            HealthStatus::Healthy
220        })
221    }
222}
223
224#[cfg(all(test, feature = "db-sqlx"))]
225mod tests {
226    use super::*;
227
228    async fn memory_pool() -> DbDriver {
229        sqlx::any::install_default_drivers();
230        DbDriver::Sqlx(
231            sqlx::any::AnyPoolOptions::new()
232                .max_connections(2)
233                .connect("sqlite::memory:")
234                .await
235                .expect("in-memory sqlite"),
236        )
237    }
238
239    /// A pool whose acquires always fail: lazily connected to a path that
240    /// cannot exist (read-only mode, missing file).
241    fn dead_pool() -> DbDriver {
242        sqlx::any::install_default_drivers();
243        DbDriver::Sqlx(
244            sqlx::any::AnyPoolOptions::new()
245                .max_connections(1)
246                .acquire_timeout(std::time::Duration::from_millis(200))
247                .connect_lazy("sqlite:///nonexistent-dir/arcly-itest.db?mode=ro")
248                .expect("lazy pool builds without connecting"),
249        )
250    }
251
252    #[tokio::test]
253    async fn dead_replica_falls_back_to_primary_for_reads() {
254        let pool = ArclyDbPool::new("failover-test", memory_pool().await).with_replica(dead_pool());
255
256        // Round-robin sends this read to the (dead) replica; the facade must
257        // recover via the primary instead of surfacing an error.
258        for _ in 0..3 {
259            let conn = pool.acquire(AccessIntent::Read).await;
260            assert!(
261                conn.is_ok(),
262                "read must fall back to primary: {:?}",
263                conn.err().map(|e| e.to_string())
264            );
265        }
266        // Writes are untouched by the failover path.
267        assert!(pool.acquire(AccessIntent::Write).await.is_ok());
268    }
269
270    #[tokio::test]
271    async fn ping_succeeds_on_live_primary_and_fails_on_dead() {
272        let live = ArclyDbPool::new("live", memory_pool().await);
273        assert!(live.ping().await.is_ok());
274
275        let dead = ArclyDbPool::new("dead", dead_pool());
276        assert!(dead.ping().await.is_err());
277    }
278}