Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libsql: add per-sync-url metadata #1908

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions libsql/src/sync.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::{util::ConnectorService, Result};

use std::path::Path;
use std::{path::Path, str::FromStr};

use bytes::Bytes;
use chrono::Utc;
use http::{HeaderValue, StatusCode};
use http::{uri::InvalidUri, HeaderValue, StatusCode, Uri};
use hyper::Body;
use tokio::io::AsyncWriteExt as _;
use uuid::Uuid;

#[cfg(test)]
mod test;

const METADATA_VERSION: u32 = 0;
const METADATA_VERSION: u32 = 1;

const DEFAULT_MAX_RETRIES: usize = 5;

Expand Down Expand Up @@ -49,6 +49,10 @@ pub enum SyncError {
InvalidPushFrameNoHigh(u32, u32),
#[error("failed to pull frame: status={0}, error={1}")]
PullFrame(StatusCode, String),
#[error("invalid sync uri: {0}")]
InvalidSyncUri(InvalidUri),
#[error("Unable to construct metadata filename: {0}")]
UnableToConstructMetadataFilename(String),
}

impl SyncError {
Expand All @@ -60,7 +64,7 @@ impl SyncError {
pub struct SyncContext {
db_path: String,
client: hyper::Client<ConnectorService, Body>,
sync_url: String,
sync_url: Uri,
auth_token: Option<HeaderValue>,
max_retries: usize,
/// Represents the max_frame_no from the server.
Expand All @@ -86,6 +90,8 @@ impl SyncContext {
None => None,
};

let sync_url = Uri::from_str(&sync_url).map_err(SyncError::InvalidSyncUri)?;

let mut me = Self {
db_path,
sync_url,
Expand All @@ -107,7 +113,11 @@ impl SyncContext {
}

#[tracing::instrument(skip(self))]
pub(crate) async fn pull_one_frame(&mut self, generation: u32, frame_no: u32) -> Result<Option<Bytes>> {
pub(crate) async fn pull_one_frame(
&mut self,
generation: u32,
frame_no: u32,
) -> Result<Option<Bytes>> {
let uri = format!(
"{}/sync/{}/{}/{}",
self.sync_url,
Expand Down Expand Up @@ -294,7 +304,7 @@ impl SyncContext {
}

pub(crate) async fn write_metadata(&mut self) -> Result<()> {
let path = format!("{}-info", self.db_path);
let path = self.sync_metadata_filename()?;

let mut metadata = MetadataJson {
hash: 0,
Expand All @@ -313,9 +323,12 @@ impl SyncContext {
}

async fn read_metadata(&mut self) -> Result<()> {
let path = format!("{}-info", self.db_path);
let path = self.sync_metadata_filename()?;

if !Path::new(&path).try_exists().map_err(SyncError::io("metadata file exists"))? {
if !Path::new(&path)
.try_exists()
.map_err(SyncError::io("metadata file exists"))?
{
tracing::debug!("no metadata info file found");
return Ok(());
}
Expand Down Expand Up @@ -344,6 +357,16 @@ impl SyncContext {

Ok(())
}

fn sync_metadata_filename(&self) -> Result<String> {
let authority = self.sync_url.authority().ok_or_else(|| {
SyncError::UnableToConstructMetadataFilename("no authority set".into())
})?;

let host = authority.host();

Ok(format!("{}-{}-info", self.db_path, host))
}
}

#[derive(serde::Serialize, serde::Deserialize, Debug)]
Expand Down
4 changes: 2 additions & 2 deletions libsql/src/sync/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ async fn test_sync_context_corrupted_metadata() {
assert_eq!(durable_frame, 0);
assert_eq!(server.frame_count(), 1);

// Update metadata path to use -info instead of .meta
let metadata_path = format!("{}-info", db_path.to_str().unwrap());
// Inject invalid data into the metadata file to force a recovery
let metadata_path = sync_ctx.sync_metadata_filename().unwrap();
std::fs::write(&metadata_path, b"invalid json data").unwrap();

// Create new sync context with corrupted metadata
Expand Down
Loading