Apply snapshots automatically on empty taskdbs

This commit is contained in:
Dustin J. Mitchell
2021-10-19 22:01:37 -04:00
parent 636862f8c5
commit c72cae648d
7 changed files with 86 additions and 18 deletions

View File

@@ -160,6 +160,10 @@ impl Server for LocalServer {
// the local server never requests a snapshot, so it should never get one
unreachable!()
}
fn get_snapshot(&mut self) -> anyhow::Result<Option<(VersionId, Snapshot)>> {
Ok(None)
}
}
#[cfg(test)]

View File

@@ -152,4 +152,25 @@ impl Server for RemoteServer {
.send_bytes(ciphertext.as_ref())
.map(|_| ())?)
}
fn get_snapshot(&mut self) -> anyhow::Result<Option<(VersionId, Snapshot)>> {
let url = format!("{}/v1/client/snapshot", self.origin);
match self
.agent
.get(&url)
.set("X-Client-Key", &self.client_key.to_string())
.call()
{
Ok(resp) => {
let version_id = get_uuid_header(&resp, "X-Version-Id")?;
let ciphertext = Ciphertext::from_resp(resp, SNAPSHOT_CONTENT_TYPE)?;
let snapshot = ciphertext
.open(&self.encryption_secret, version_id)?
.payload;
Ok(Some((version_id, snapshot)))
}
Err(ureq::Error::Status(status, _)) if status == 404 => Ok(None),
Err(err) => Err(err.into()),
}
}
}

View File

@@ -12,9 +12,8 @@ struct Version {
history_segment: HistorySegment,
}
#[derive(Clone)]
/// TestServer implements the Server trait with a test implementation.
#[derive(Clone)]
pub(crate) struct TestServer(Arc<Mutex<Inner>>);
pub(crate) struct Inner {
@@ -35,6 +34,7 @@ impl TestServer {
snapshot: None,
})))
}
// feel free to add any test utility functions here
/// Get a boxed Server implementation referring to this TestServer
pub(crate) fn server(&self) -> Box<dyn Server> {
@@ -51,6 +51,12 @@ impl TestServer {
let inner = self.0.lock().unwrap();
inner.snapshot.as_ref().cloned()
}
/// Delete a version from storage
pub(crate) fn delete_version(&mut self, parent_version_id: VersionId) {
let mut inner = self.0.lock().unwrap();
inner.versions.remove(&parent_version_id);
}
}
impl Server for TestServer {
@@ -119,4 +125,9 @@ impl Server for TestServer {
inner.snapshot = Some((version_id, snapshot));
Ok(())
}
fn get_snapshot(&mut self) -> anyhow::Result<Option<(VersionId, Snapshot)>> {
let inner = self.0.lock().unwrap();
Ok(inner.snapshot.clone())
}
}

View File

@@ -65,4 +65,6 @@ pub trait Server {
/// Add a snapshot on the server
fn add_snapshot(&mut self, version_id: VersionId, snapshot: Snapshot) -> anyhow::Result<()>;
fn get_snapshot(&mut self) -> anyhow::Result<Option<(VersionId, Snapshot)>>;
}