Skip to content

Commit

Permalink
expose bottomless backup savepoint through PrimaryDatabase API
Browse files Browse the repository at this point in the history
  • Loading branch information
Horusiath committed Feb 27, 2024
1 parent dc1e8c7 commit 8005d6d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
9 changes: 9 additions & 0 deletions bottomless/src/bottomless_wal.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::ffi::c_int;
use std::sync::{Arc, Mutex};

use crate::completion_progress::SavepointTracker;
use libsql_sys::ffi::{SQLITE_BUSY, SQLITE_IOERR_WRITE};
use libsql_sys::wal::wrapper::{WalWrapper, WrapWal};
use libsql_sys::wal::{
Expand Down Expand Up @@ -29,6 +30,14 @@ impl BottomlessWalWrapper {
}
}

pub fn backup_savepoint(&self) -> Option<SavepointTracker> {
let lock = self.replicator.lock().unwrap();
match &*lock {
None => None,
Some(replicator) => Some(replicator.savepoint()),
}
}

pub fn shutdown(&self) -> Option<Replicator> {
self.replicator.lock().unwrap().take()
}
Expand Down
12 changes: 4 additions & 8 deletions bottomless/src/completion_progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use uuid::Uuid;
pub struct SavepointTracker {
next_frame_no: Arc<AtomicU32>,
receiver: Receiver<u32>,
pub generation: Arc<ArcSwapOption<Uuid>>,
pub generation_snapshot: Receiver<Result<Option<Uuid>>>,
pub db_path: String,
generation: Arc<ArcSwapOption<Uuid>>,
generation_snapshot: Receiver<Result<Option<Uuid>>>,
db_path: String,
}

impl SavepointTracker {
Expand Down Expand Up @@ -41,7 +41,7 @@ impl SavepointTracker {
.wait_for(|gen| match gen {
Ok(Some(gen)) => gen == &*generation,
Ok(None) => false,
Err(e) => true,
Err(_) => true,
})
.await?;
return match &*res {
Expand Down Expand Up @@ -98,8 +98,4 @@ impl CompletionProgress {
self.detached_ranges.insert(start_frame, end_frame);
}
}

pub(crate) fn subscribe(&mut self) -> Receiver<u32> {
self.tx.subscribe()
}
}
18 changes: 18 additions & 0 deletions libsql-server/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ pub trait Database: Sync + Send + 'static {
fn destroy(self);

async fn shutdown(self) -> Result<()>;

/// Snapshots a DB state at the moment of a call and awaits for a backup service
/// to complete saving that state. Any changes happening concurrently are not
/// guaranteed to be part of confirmation.
async fn backup_savepoint(&self) -> Result<()>;
}

pub struct ReplicaDatabase {
Expand All @@ -41,6 +46,10 @@ impl Database for ReplicaDatabase {
async fn shutdown(self) -> Result<()> {
Ok(())
}

async fn backup_savepoint(&self) -> Result<()> {
Ok(()) // backup doesn't happen on replicas
}
}

pub struct PrimaryDatabase {
Expand Down Expand Up @@ -81,4 +90,13 @@ impl Database for PrimaryDatabase {

Ok(())
}

async fn backup_savepoint(&self) -> Result<()> {
if let Some(wal) = self.wal_manager.wrapper() {
if let Some(mut tracker) = wal.backup_savepoint() {
tracker.confirmed().await?;
}
}
Ok(())
}
}

0 comments on commit 8005d6d

Please sign in to comment.