pub struct DurabilityLayer { /* private fields */ }Expand description
The core type — manages a SQLite database with automatic S3 backup.
DurabilityLayer wraps a rusqlite::Connection and continuously ships
WAL frames to an S3-compatible bucket in the background. On startup it
restores the local DB from the bucket if needed.
§Lifecycle
DurabilityLayer::open()— acquire locks, restore if needed, start background taskslayer.with_conn()— run SQL statementslayer.shutdown()— checkpoint WAL, final sync, release locks
§Multi-writer protection
open() acquires three guards:
- flock on
.db.lock— prevents two processes on the same machine - lock.json in bucket — prevents two processes anywhere (with heartbeat)
- writer.json in bucket — permanent ownership record
If any guard fails, open() returns an error.
Implementations§
Source§impl DurabilityLayer
impl DurabilityLayer
Sourcepub async fn open(config: Config, bucket: Arc<dyn BucketStore>) -> Result<Self>
pub async fn open(config: Config, bucket: Arc<dyn BucketStore>) -> Result<Self>
Open a DurabilityLayer — the main entry point.
This acquires the local file lock, verifies writer identity, acquires the bucket replica lock, restores from S3 if needed, validates bucket integrity, and starts background replication tasks.
§Errors
Returns Error::DatabaseLocked if another process holds the local DB lock.
Returns Error::ReplicaLocked if another process holds the bucket lock.
Returns Error::WrongWriter if another server owns this bucket prefix.
pub async fn open_with_metrics( config: Config, bucket: Arc<dyn BucketStore>, metrics: Arc<dyn Metrics>, ) -> Result<Self>
Sourcepub async fn sync_now(&self) -> Result<()>
pub async fn sync_now(&self) -> Result<()>
Wait for all pending WAL frames to be shipped to the bucket.
Returns once the shipper has caught up, or times out after sync_timeout
(default 5s). On timeout, returns an error — the WAL frames remain locally
and will be retried on the next sync.
pub async fn last_sync(&self) -> Instant
pub async fn is_healthy(&self) -> bool
pub async fn health_report(&self) -> HealthReport
pub async fn conn(&self) -> MutexGuard<'_, Connection>
Sourcepub async fn with_conn<F, T>(&self, f: F) -> Result<T>
pub async fn with_conn<F, T>(&self, f: F) -> Result<T>
Run a closure with exclusive access to the rusqlite::Connection.
This is the preferred way to execute SQL. The connection is held under a Tokio mutex, so the closure should be short-lived.
§Example
layer.with_conn(|conn| {
conn.execute("INSERT INTO users (name) VALUES (?)", ["Alice"])
})?;pub fn sync_notify(&self) -> Arc<Notify> ⓘ
Sourcepub async fn force_snapshot(&self) -> Result<()>
pub async fn force_snapshot(&self) -> Result<()>
Force an immediate snapshot upload to the bucket.
Triggers VACUUM INTO and uploads the resulting DB file.
Normally snapshots happen automatically based on WAL size and time
intervals, but this is useful for ensuring data is durably in S3
before shutdown or during testing.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for DurabilityLayer
impl !UnwindSafe for DurabilityLayer
impl Forget for DurabilityLayer
impl Freeze for DurabilityLayer
impl Move for DurabilityLayer
impl Send for DurabilityLayer
impl Sync for DurabilityLayer
impl Unpin for DurabilityLayer
impl UnsafeUnpin for DurabilityLayer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more