Skip to main content

DurabilityLayer

Struct DurabilityLayer 

Source
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

  1. DurabilityLayer::open() — acquire locks, restore if needed, start background tasks
  2. layer.with_conn() — run SQL statements
  3. layer.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

Source

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.

Source

pub async fn open_with_metrics( config: Config, bucket: Arc<dyn BucketStore>, metrics: Arc<dyn Metrics>, ) -> Result<Self>

Source

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.

Source

pub async fn last_sync(&self) -> Instant

Source

pub async fn is_healthy(&self) -> bool

Source

pub async fn health_report(&self) -> HealthReport

Source

pub async fn conn(&self) -> MutexGuard<'_, Connection>

Source

pub async fn with_conn<F, T>(&self, f: F) -> Result<T>
where F: FnOnce(&Connection) -> 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"])
})?;
Source

pub fn sync_notify(&self) -> Arc<Notify> ⓘ

Source

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.

Source

pub async fn shutdown(&self) -> Result<()>

Graceful shutdown — checkpoint WAL, final sync, release locks.

This should always be called before dropping the DurabilityLayer. If the layer is dropped without calling shutdown(), a warning is logged and the WAL may not be checkpointed.

Trait Implementations§

Source§

impl Drop for DurabilityLayer

Source§

fn drop(&mut self)

Logs a warning if shutdown() wasn’t called. Calling shutdown() explicitly is the recommended cleanup path — it safely checkpoints the WAL on the Tokio runtime thread.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more