Make Storage methods async

This will better support concurrent requests.
This commit is contained in:
Dustin J. Mitchell 2025-07-11 17:28:05 -04:00
parent 4de5c9a345
commit 7559364017
No known key found for this signature in database
13 changed files with 597 additions and 412 deletions

View file

@ -10,6 +10,7 @@ license = "MIT"
[dependencies]
uuid.workspace = true
async-trait.workspace = true
anyhow.workspace = true
thiserror.workspace = true
log.workspace = true
@ -18,3 +19,4 @@ chrono.workspace = true
[dev-dependencies]
pretty_assertions.workspace = true
tokio.workspace = true

View file

@ -44,8 +44,9 @@ struct InnerTxn<'a> {
committed: bool,
}
#[async_trait::async_trait]
impl Storage for InMemoryStorage {
fn txn(&self, client_id: Uuid) -> anyhow::Result<Box<dyn StorageTxn + '_>> {
async fn txn(&self, client_id: Uuid) -> anyhow::Result<Box<dyn StorageTxn + '_>> {
Ok(Box::new(InnerTxn {
client_id,
guard: self.0.lock().expect("poisoned lock"),
@ -55,12 +56,13 @@ impl Storage for InMemoryStorage {
}
}
#[async_trait::async_trait(?Send)]
impl StorageTxn for InnerTxn<'_> {
fn get_client(&mut self) -> anyhow::Result<Option<Client>> {
async fn get_client(&mut self) -> anyhow::Result<Option<Client>> {
Ok(self.guard.clients.get(&self.client_id).cloned())
}
fn new_client(&mut self, latest_version_id: Uuid) -> anyhow::Result<()> {
async fn new_client(&mut self, latest_version_id: Uuid) -> anyhow::Result<()> {
if self.guard.clients.contains_key(&self.client_id) {
return Err(anyhow::anyhow!("Client {} already exists", self.client_id));
}
@ -75,7 +77,7 @@ impl StorageTxn for InnerTxn<'_> {
Ok(())
}
fn set_snapshot(&mut self, snapshot: Snapshot, data: Vec<u8>) -> anyhow::Result<()> {
async fn set_snapshot(&mut self, snapshot: Snapshot, data: Vec<u8>) -> anyhow::Result<()> {
let client = self
.guard
.clients
@ -87,7 +89,7 @@ impl StorageTxn for InnerTxn<'_> {
Ok(())
}
fn get_snapshot_data(&mut self, version_id: Uuid) -> anyhow::Result<Option<Vec<u8>>> {
async fn get_snapshot_data(&mut self, version_id: Uuid) -> anyhow::Result<Option<Vec<u8>>> {
// sanity check
let client = self.guard.clients.get(&self.client_id);
let client = client.ok_or_else(|| anyhow::anyhow!("no such client"))?;
@ -97,7 +99,7 @@ impl StorageTxn for InnerTxn<'_> {
Ok(self.guard.snapshots.get(&self.client_id).cloned())
}
fn get_version_by_parent(
async fn get_version_by_parent(
&mut self,
parent_version_id: Uuid,
) -> anyhow::Result<Option<Version>> {
@ -116,7 +118,7 @@ impl StorageTxn for InnerTxn<'_> {
}
}
fn get_version(&mut self, version_id: Uuid) -> anyhow::Result<Option<Version>> {
async fn get_version(&mut self, version_id: Uuid) -> anyhow::Result<Option<Version>> {
Ok(self
.guard
.versions
@ -124,7 +126,7 @@ impl StorageTxn for InnerTxn<'_> {
.cloned())
}
fn add_version(
async fn add_version(
&mut self,
version_id: Uuid,
parent_version_id: Uuid,
@ -174,7 +176,7 @@ impl StorageTxn for InnerTxn<'_> {
Ok(())
}
fn commit(&mut self) -> anyhow::Result<()> {
async fn commit(&mut self) -> anyhow::Result<()> {
self.committed = true;
Ok(())
}
@ -193,32 +195,33 @@ mod test {
use super::*;
use chrono::Utc;
#[test]
fn test_get_client_empty() -> anyhow::Result<()> {
#[tokio::test]
async fn test_get_client_empty() -> anyhow::Result<()> {
let storage = InMemoryStorage::new();
let mut txn = storage.txn(Uuid::new_v4())?;
let maybe_client = txn.get_client()?;
let mut txn = storage.txn(Uuid::new_v4()).await?;
let maybe_client = txn.get_client().await?;
assert!(maybe_client.is_none());
Ok(())
}
#[test]
fn test_client_storage() -> anyhow::Result<()> {
#[tokio::test]
async fn test_client_storage() -> anyhow::Result<()> {
let storage = InMemoryStorage::new();
let client_id = Uuid::new_v4();
let mut txn = storage.txn(client_id)?;
let mut txn = storage.txn(client_id).await?;
let latest_version_id = Uuid::new_v4();
txn.new_client(latest_version_id)?;
txn.new_client(latest_version_id).await?;
let client = txn.get_client()?.unwrap();
let client = txn.get_client().await?.unwrap();
assert_eq!(client.latest_version_id, latest_version_id);
assert!(client.snapshot.is_none());
let latest_version_id = Uuid::new_v4();
txn.add_version(latest_version_id, Uuid::new_v4(), vec![1, 1])?;
txn.add_version(latest_version_id, Uuid::new_v4(), vec![1, 1])
.await?;
let client = txn.get_client()?.unwrap();
let client = txn.get_client().await?.unwrap();
assert_eq!(client.latest_version_id, latest_version_id);
assert!(client.snapshot.is_none());
@ -227,38 +230,39 @@ mod test {
timestamp: Utc::now(),
versions_since: 4,
};
txn.set_snapshot(snap.clone(), vec![1, 2, 3])?;
txn.set_snapshot(snap.clone(), vec![1, 2, 3]).await?;
let client = txn.get_client()?.unwrap();
let client = txn.get_client().await?.unwrap();
assert_eq!(client.latest_version_id, latest_version_id);
assert_eq!(client.snapshot.unwrap(), snap);
txn.commit()?;
txn.commit().await?;
Ok(())
}
#[test]
fn test_gvbp_empty() -> anyhow::Result<()> {
#[tokio::test]
async fn test_gvbp_empty() -> anyhow::Result<()> {
let storage = InMemoryStorage::new();
let client_id = Uuid::new_v4();
let mut txn = storage.txn(client_id)?;
let maybe_version = txn.get_version_by_parent(Uuid::new_v4())?;
let mut txn = storage.txn(client_id).await?;
let maybe_version = txn.get_version_by_parent(Uuid::new_v4()).await?;
assert!(maybe_version.is_none());
Ok(())
}
#[test]
fn test_add_version_and_get_version() -> anyhow::Result<()> {
#[tokio::test]
async fn test_add_version_and_get_version() -> anyhow::Result<()> {
let storage = InMemoryStorage::new();
let client_id = Uuid::new_v4();
let mut txn = storage.txn(client_id)?;
let mut txn = storage.txn(client_id).await?;
let version_id = Uuid::new_v4();
let parent_version_id = Uuid::new_v4();
let history_segment = b"abc".to_vec();
txn.new_client(parent_version_id)?;
txn.add_version(version_id, parent_version_id, history_segment.clone())?;
txn.new_client(parent_version_id).await?;
txn.add_version(version_id, parent_version_id, history_segment.clone())
.await?;
let expected = Version {
version_id,
@ -266,74 +270,76 @@ mod test {
history_segment,
};
let version = txn.get_version_by_parent(parent_version_id)?.unwrap();
let version = txn.get_version_by_parent(parent_version_id).await?.unwrap();
assert_eq!(version, expected);
let version = txn.get_version(version_id)?.unwrap();
let version = txn.get_version(version_id).await?.unwrap();
assert_eq!(version, expected);
txn.commit()?;
txn.commit().await?;
Ok(())
}
#[test]
fn test_add_version_exists() -> anyhow::Result<()> {
#[tokio::test]
async fn test_add_version_exists() -> anyhow::Result<()> {
let storage = InMemoryStorage::new();
let client_id = Uuid::new_v4();
let mut txn = storage.txn(client_id)?;
let mut txn = storage.txn(client_id).await?;
let version_id = Uuid::new_v4();
let parent_version_id = Uuid::new_v4();
let history_segment = b"abc".to_vec();
txn.new_client(parent_version_id)?;
txn.add_version(version_id, parent_version_id, history_segment.clone())?;
txn.new_client(parent_version_id).await?;
txn.add_version(version_id, parent_version_id, history_segment.clone())
.await?;
assert!(txn
.add_version(version_id, parent_version_id, history_segment.clone())
.await
.is_err());
txn.commit()?;
txn.commit().await?;
Ok(())
}
#[test]
fn test_snapshots() -> anyhow::Result<()> {
#[tokio::test]
async fn test_snapshots() -> anyhow::Result<()> {
let storage = InMemoryStorage::new();
let client_id = Uuid::new_v4();
let mut txn = storage.txn(client_id)?;
let mut txn = storage.txn(client_id).await?;
txn.new_client(Uuid::new_v4())?;
assert!(txn.get_client()?.unwrap().snapshot.is_none());
txn.new_client(Uuid::new_v4()).await?;
assert!(txn.get_client().await?.unwrap().snapshot.is_none());
let snap = Snapshot {
version_id: Uuid::new_v4(),
timestamp: Utc::now(),
versions_since: 3,
};
txn.set_snapshot(snap.clone(), vec![9, 8, 9])?;
txn.set_snapshot(snap.clone(), vec![9, 8, 9]).await?;
assert_eq!(
txn.get_snapshot_data(snap.version_id)?.unwrap(),
txn.get_snapshot_data(snap.version_id).await?.unwrap(),
vec![9, 8, 9]
);
assert_eq!(txn.get_client()?.unwrap().snapshot, Some(snap));
assert_eq!(txn.get_client().await?.unwrap().snapshot, Some(snap));
let snap2 = Snapshot {
version_id: Uuid::new_v4(),
timestamp: Utc::now(),
versions_since: 10,
};
txn.set_snapshot(snap2.clone(), vec![0, 2, 4, 6])?;
txn.set_snapshot(snap2.clone(), vec![0, 2, 4, 6]).await?;
assert_eq!(
txn.get_snapshot_data(snap2.version_id)?.unwrap(),
txn.get_snapshot_data(snap2.version_id).await?.unwrap(),
vec![0, 2, 4, 6]
);
assert_eq!(txn.get_client()?.unwrap().snapshot, Some(snap2));
assert_eq!(txn.get_client().await?.unwrap().snapshot, Some(snap2));
// check that mismatched version is detected
assert!(txn.get_snapshot_data(Uuid::new_v4()).is_err());
assert!(txn.get_snapshot_data(Uuid::new_v4()).await.is_err());
txn.commit()?;
txn.commit().await?;
Ok(())
}
}

View file

@ -106,17 +106,17 @@ impl Server {
}
/// Implementation of the GetChildVersion protocol transaction.
pub fn get_child_version(
pub async fn get_child_version(
&self,
client_id: ClientId,
parent_version_id: VersionId,
) -> Result<GetVersionResult, ServerError> {
let mut txn = self.storage.txn(client_id)?;
let client = txn.get_client()?.ok_or(ServerError::NoSuchClient)?;
let mut txn = self.txn(client_id).await?;
let client = txn.get_client().await?.ok_or(ServerError::NoSuchClient)?;
// If a version with parentVersionId equal to the requested parentVersionId exists, it is
// returned.
if let Some(version) = txn.get_version_by_parent(parent_version_id)? {
if let Some(version) = txn.get_version_by_parent(parent_version_id).await? {
return Ok(GetVersionResult::Success {
version_id: version.version_id,
parent_version_id: version.parent_version_id,
@ -142,7 +142,7 @@ impl Server {
}
/// Implementation of the AddVersion protocol transaction
pub fn add_version(
pub async fn add_version(
&self,
client_id: ClientId,
parent_version_id: VersionId,
@ -150,8 +150,8 @@ impl Server {
) -> Result<(AddVersionResult, SnapshotUrgency), ServerError> {
log::debug!("add_version(client_id: {client_id}, parent_version_id: {parent_version_id})");
let mut txn = self.storage.txn(client_id)?;
let client = txn.get_client()?.ok_or(ServerError::NoSuchClient)?;
let mut txn = self.txn(client_id).await?;
let client = txn.get_client().await?.ok_or(ServerError::NoSuchClient)?;
// check if this version is acceptable, under the protection of the transaction
if client.latest_version_id != NIL_VERSION_ID
@ -169,8 +169,9 @@ impl Server {
log::debug!("add_version request accepted: new version_id: {version_id}");
// update the DB
txn.add_version(version_id, parent_version_id, history_segment)?;
txn.commit()?;
txn.add_version(version_id, parent_version_id, history_segment)
.await?;
txn.commit().await?;
// calculate the urgency
let time_urgency = match client.snapshot {
@ -194,7 +195,7 @@ impl Server {
}
/// Implementation of the AddSnapshot protocol transaction
pub fn add_snapshot(
pub async fn add_snapshot(
&self,
client_id: ClientId,
version_id: VersionId,
@ -202,8 +203,8 @@ impl Server {
) -> Result<(), ServerError> {
log::debug!("add_snapshot(client_id: {client_id}, version_id: {version_id})");
let mut txn = self.storage.txn(client_id)?;
let client = txn.get_client()?.ok_or(ServerError::NoSuchClient)?;
let mut txn = self.txn(client_id).await?;
let client = txn.get_client().await?.ok_or(ServerError::NoSuchClient)?;
// NOTE: if the snapshot is rejected, this function logs about it and returns
// Ok(()), as there's no reason to report an errot to the client / user.
@ -239,7 +240,7 @@ impl Server {
}
// get the parent version ID
if let Some(parent) = txn.get_version(vid)? {
if let Some(parent) = txn.get_version(vid).await? {
vid = parent.parent_version_id;
} else {
// this version does not exist; "this should not happen" but if it does,
@ -257,21 +258,23 @@ impl Server {
versions_since: 0,
},
data,
)?;
txn.commit()?;
)
.await?;
txn.commit().await?;
Ok(())
}
/// Implementation of the GetSnapshot protocol transaction
pub fn get_snapshot(
pub async fn get_snapshot(
&self,
client_id: ClientId,
) -> Result<Option<(Uuid, Vec<u8>)>, ServerError> {
let mut txn = self.storage.txn(client_id)?;
let client = txn.get_client()?.ok_or(ServerError::NoSuchClient)?;
let mut txn = self.txn(client_id).await?;
let client = txn.get_client().await?.ok_or(ServerError::NoSuchClient)?;
Ok(if let Some(snap) = client.snapshot {
txn.get_snapshot_data(snap.version_id)?
txn.get_snapshot_data(snap.version_id)
.await?
.map(|data| (snap.version_id, data))
} else {
None
@ -279,8 +282,8 @@ impl Server {
}
/// Convenience method to get a transaction for the embedded storage.
pub fn txn(&self, client_id: Uuid) -> Result<Box<dyn StorageTxn + '_>, ServerError> {
Ok(self.storage.txn(client_id)?)
pub async fn txn(&self, client_id: Uuid) -> Result<Box<dyn StorageTxn + '_>, ServerError> {
Ok(self.storage.txn(client_id).await?)
}
}
@ -288,68 +291,83 @@ impl Server {
mod test {
use super::*;
use crate::inmemory::InMemoryStorage;
use crate::storage::{Snapshot, Storage, StorageTxn};
use crate::storage::{Snapshot, Storage};
use chrono::{Duration, TimeZone, Utc};
use pretty_assertions::assert_eq;
fn setup<INIT, RES>(init: INIT) -> anyhow::Result<(Server, RES)>
where
INIT: FnOnce(&mut dyn StorageTxn, Uuid) -> anyhow::Result<RES>,
{
/// Set up for a test, returning storage and a client_id.
fn setup() -> (InMemoryStorage, Uuid) {
let _ = env_logger::builder().is_test(true).try_init();
let storage = InMemoryStorage::new();
let client_id = Uuid::new_v4();
let res;
{
let mut txn = storage.txn(client_id)?;
res = init(txn.as_mut(), client_id)?;
txn.commit()?;
}
Ok((Server::new(ServerConfig::default(), storage), res))
(storage, client_id)
}
/// Convert storage into a Server.
fn into_server(storage: InMemoryStorage) -> Server {
Server::new(ServerConfig::default(), storage)
}
/// Add versions to the DB for the given client.
async fn add_versions(
storage: &InMemoryStorage,
client_id: Uuid,
num_versions: u32,
snapshot_version: Option<u32>,
snapshot_days_ago: Option<i64>,
) -> anyhow::Result<Vec<Uuid>> {
let mut txn = storage.txn(client_id).await?;
let mut versions = vec![];
let mut version_id = Uuid::nil();
txn.new_client(Uuid::nil()).await?;
debug_assert!(num_versions < u8::MAX.into());
for vnum in 0..num_versions {
let parent_version_id = version_id;
version_id = Uuid::new_v4();
versions.push(version_id);
txn.add_version(
version_id,
parent_version_id,
// Generate some unique data for this version.
vec![0, 0, vnum as u8],
)
.await?;
if Some(vnum) == snapshot_version {
txn.set_snapshot(
Snapshot {
version_id,
versions_since: 0,
timestamp: Utc::now() - Duration::days(snapshot_days_ago.unwrap_or(0)),
},
// Generate some unique data for this snapshot.
vec![vnum as u8],
)
.await?;
}
}
txn.commit().await?;
Ok(versions)
}
/*
/// Utility setup function for add_version tests
fn av_setup(
async fn av_setup(
num_versions: u32,
snapshot_version: Option<u32>,
snapshot_days_ago: Option<i64>,
) -> anyhow::Result<(Server, Uuid, Vec<Uuid>)> {
let (server, (client_id, versions)) = setup(|txn, client_id| {
let mut versions = vec![];
let mut version_id = Uuid::nil();
txn.new_client(Uuid::nil())?;
debug_assert!(num_versions < u8::MAX.into());
for vnum in 0..num_versions {
let parent_version_id = version_id;
version_id = Uuid::new_v4();
versions.push(version_id);
txn.add_version(
version_id,
parent_version_id,
// Generate some unique data for this version.
vec![0, 0, vnum as u8],
)?;
if Some(vnum) == snapshot_version {
txn.set_snapshot(
Snapshot {
version_id,
versions_since: 0,
timestamp: Utc::now() - Duration::days(snapshot_days_ago.unwrap_or(0)),
},
// Generate some unique data for this snapshot.
vec![vnum as u8],
)?;
}
}
let (server, (client_id, versions)) = setup(async |txn, client_id| {
Ok((client_id, versions))
})?;
})
.await?;
Ok((server, client_id, versions))
}
*/
/// Utility function to check the results of an add_version call
fn av_success_check(
async fn av_success_check(
server: &Server,
client_id: Uuid,
existing_versions: &[Uuid],
@ -364,17 +382,17 @@ mod test {
}
// verify that the storage was updated
let mut txn = server.txn(client_id)?;
let client = txn.get_client()?.unwrap();
let mut txn = server.txn(client_id).await?;
let client = txn.get_client().await?.unwrap();
assert_eq!(client.latest_version_id, new_version_id);
let parent_version_id = existing_versions.last().cloned().unwrap_or_else(Uuid::nil);
let version = txn.get_version(new_version_id)?.unwrap();
let version = txn.get_version(new_version_id).await?.unwrap();
assert_eq!(version.version_id, new_version_id);
assert_eq!(version.parent_version_id, parent_version_id);
assert_eq!(version.history_segment, expected_history);
} else {
panic!("did not get Ok from add_version: {:?}", add_version_result);
panic!("did not get Ok from add_version: {add_version_result:?}");
}
assert_eq!(snapshot_urgency, expected_urgency);
@ -426,89 +444,108 @@ mod test {
);
}
#[test]
fn get_child_version_not_found_initial_nil() -> anyhow::Result<()> {
let (server, client_id) = setup(|txn, client_id| {
txn.new_client(NIL_VERSION_ID)?;
#[tokio::test]
async fn get_child_version_not_found_initial_nil() -> anyhow::Result<()> {
let (storage, client_id) = setup();
{
let mut txn = storage.txn(client_id).await?;
txn.new_client(NIL_VERSION_ID).await?;
txn.commit().await?;
}
let server = into_server(storage);
Ok(client_id)
})?;
// when no latest version exists, the first version is NotFound
assert_eq!(
server.get_child_version(client_id, NIL_VERSION_ID)?,
server.get_child_version(client_id, NIL_VERSION_ID).await?,
GetVersionResult::NotFound
);
Ok(())
}
#[test]
fn get_child_version_not_found_initial_continuing() -> anyhow::Result<()> {
let (server, client_id) = setup(|txn, client_id| {
txn.new_client(NIL_VERSION_ID)?;
#[tokio::test]
async fn get_child_version_not_found_initial_continuing() -> anyhow::Result<()> {
let (storage, client_id) = setup();
{
let mut txn = storage.txn(client_id).await?;
txn.new_client(NIL_VERSION_ID).await?;
txn.commit().await?;
}
Ok(client_id)
})?;
let server = into_server(storage);
// when no latest version exists, _any_ child version is NOT_FOUND. This allows syncs to
// start to a new server even if the client already has been uploading to another service.
assert_eq!(
server.get_child_version(client_id, Uuid::new_v4(),)?,
server.get_child_version(client_id, Uuid::new_v4(),).await?,
GetVersionResult::NotFound
);
Ok(())
}
#[test]
fn get_child_version_not_found_up_to_date() -> anyhow::Result<()> {
let (server, (client_id, parent_version_id)) = setup(|txn, client_id| {
#[tokio::test]
async fn get_child_version_not_found_up_to_date() -> anyhow::Result<()> {
let (storage, client_id) = setup();
let parent_version_id = Uuid::new_v4();
{
let mut txn = storage.txn(client_id).await?;
// add a parent version, but not the requested child version
let parent_version_id = Uuid::new_v4();
txn.new_client(parent_version_id)?;
txn.add_version(parent_version_id, NIL_VERSION_ID, vec![])?;
Ok((client_id, parent_version_id))
})?;
txn.new_client(parent_version_id).await?;
txn.add_version(parent_version_id, NIL_VERSION_ID, vec![])
.await?;
txn.commit().await?;
}
let server = into_server(storage);
assert_eq!(
server.get_child_version(client_id, parent_version_id)?,
server
.get_child_version(client_id, parent_version_id)
.await?,
GetVersionResult::NotFound
);
Ok(())
}
#[test]
fn get_child_version_gone_not_latest() -> anyhow::Result<()> {
let (server, client_id) = setup(|txn, client_id| {
#[tokio::test]
async fn get_child_version_gone_not_latest() -> anyhow::Result<()> {
let (storage, client_id) = setup();
let parent_version_id = Uuid::new_v4();
{
let mut txn = storage.txn(client_id).await?;
// Add a parent version, but not the requested parent version
let parent_version_id = Uuid::new_v4();
txn.new_client(parent_version_id)?;
txn.add_version(parent_version_id, NIL_VERSION_ID, vec![])?;
Ok(client_id)
})?;
txn.new_client(parent_version_id).await?;
txn.add_version(parent_version_id, NIL_VERSION_ID, vec![])
.await?;
txn.commit().await?;
}
let server = into_server(storage);
assert_eq!(
server.get_child_version(client_id, Uuid::new_v4(),)?,
server.get_child_version(client_id, Uuid::new_v4(),).await?,
GetVersionResult::Gone
);
Ok(())
}
#[test]
fn get_child_version_found() -> anyhow::Result<()> {
let (server, (client_id, version_id, parent_version_id, history_segment)) =
setup(|txn, client_id| {
let version_id = Uuid::new_v4();
let parent_version_id = Uuid::new_v4();
let history_segment = b"abcd".to_vec();
#[tokio::test]
async fn get_child_version_found() -> anyhow::Result<()> {
let (storage, client_id) = setup();
let version_id = Uuid::new_v4();
let parent_version_id = Uuid::new_v4();
let history_segment = b"abcd".to_vec();
{
let mut txn = storage.txn(client_id).await?;
txn.new_client(version_id).await?;
txn.add_version(version_id, parent_version_id, history_segment.clone())
.await?;
txn.commit().await?;
}
txn.new_client(version_id)?;
txn.add_version(version_id, parent_version_id, history_segment.clone())?;
Ok((client_id, version_id, parent_version_id, history_segment))
})?;
let server = into_server(storage);
assert_eq!(
server.get_child_version(client_id, parent_version_id)?,
server
.get_child_version(client_id, parent_version_id)
.await?,
GetVersionResult::Success {
version_id,
parent_version_id,
@ -518,29 +555,41 @@ mod test {
Ok(())
}
#[test]
fn add_version_conflict() -> anyhow::Result<()> {
let (server, client_id, versions) = av_setup(3, None, None)?;
#[tokio::test]
async fn add_version_conflict() -> anyhow::Result<()> {
let (storage, client_id) = setup();
let versions = add_versions(&storage, client_id, 3, None, None).await?;
// try to add a child of a version other than the latest
let server = into_server(storage);
assert_eq!(
server.add_version(client_id, versions[1], vec![3, 6, 9])?.0,
server
.add_version(client_id, versions[1], vec![3, 6, 9])
.await?
.0,
AddVersionResult::ExpectedParentVersion(versions[2])
);
// verify that the storage wasn't updated
let mut txn = server.txn(client_id)?;
assert_eq!(txn.get_client()?.unwrap().latest_version_id, versions[2]);
assert_eq!(txn.get_version_by_parent(versions[2])?, None);
let mut txn = server.txn(client_id).await?;
assert_eq!(
txn.get_client().await?.unwrap().latest_version_id,
versions[2]
);
assert_eq!(txn.get_version_by_parent(versions[2]).await?, None);
Ok(())
}
#[test]
fn add_version_with_existing_history() -> anyhow::Result<()> {
let (server, client_id, versions) = av_setup(1, None, None)?;
#[tokio::test]
async fn add_version_with_existing_history() -> anyhow::Result<()> {
let (storage, client_id) = setup();
let versions = add_versions(&storage, client_id, 1, None, None).await?;
let result = server.add_version(client_id, versions[0], vec![3, 6, 9])?;
let server = into_server(storage);
let result = server
.add_version(client_id, versions[0], vec![3, 6, 9])
.await?;
av_success_check(
&server,
@ -550,17 +599,22 @@ mod test {
vec![3, 6, 9],
// urgency=high because there are no snapshots yet
SnapshotUrgency::High,
)?;
)
.await?;
Ok(())
}
#[test]
fn add_version_with_no_history() -> anyhow::Result<()> {
let (server, client_id, versions) = av_setup(0, None, None)?;
#[tokio::test]
async fn add_version_with_no_history() -> anyhow::Result<()> {
let (storage, client_id) = setup();
let versions = add_versions(&storage, client_id, 0, None, None).await?;
let server = into_server(storage);
let parent_version_id = Uuid::nil();
let result = server.add_version(client_id, parent_version_id, vec![3, 6, 9])?;
let result = server
.add_version(client_id, parent_version_id, vec![3, 6, 9])
.await?;
av_success_check(
&server,
@ -570,16 +624,21 @@ mod test {
vec![3, 6, 9],
// urgency=high because there are no snapshots yet
SnapshotUrgency::High,
)?;
)
.await?;
Ok(())
}
#[test]
fn add_version_success_recent_snapshot() -> anyhow::Result<()> {
let (server, client_id, versions) = av_setup(1, Some(0), None)?;
#[tokio::test]
async fn add_version_success_recent_snapshot() -> anyhow::Result<()> {
let (storage, client_id) = setup();
let versions = add_versions(&storage, client_id, 1, Some(0), None).await?;
let result = server.add_version(client_id, versions[0], vec![1, 2, 3])?;
let server = into_server(storage);
let result = server
.add_version(client_id, versions[0], vec![1, 2, 3])
.await?;
av_success_check(
&server,
@ -589,17 +648,22 @@ mod test {
vec![1, 2, 3],
// no snapshot request since the previous version has a snapshot
SnapshotUrgency::None,
)?;
)
.await?;
Ok(())
}
#[test]
fn add_version_success_aged_snapshot() -> anyhow::Result<()> {
#[tokio::test]
async fn add_version_success_aged_snapshot() -> anyhow::Result<()> {
// one snapshot, but it was 50 days ago
let (server, client_id, versions) = av_setup(1, Some(0), Some(50))?;
let (storage, client_id) = setup();
let versions = add_versions(&storage, client_id, 1, Some(0), Some(50)).await?;
let result = server.add_version(client_id, versions[0], vec![1, 2, 3])?;
let server = into_server(storage);
let result = server
.add_version(client_id, versions[0], vec![1, 2, 3])
.await?;
av_success_check(
&server,
@ -609,18 +673,24 @@ mod test {
vec![1, 2, 3],
// urgency=high due to days since the snapshot
SnapshotUrgency::High,
)?;
)
.await?;
Ok(())
}
#[test]
fn add_version_success_snapshot_many_versions_ago() -> anyhow::Result<()> {
#[tokio::test]
async fn add_version_success_snapshot_many_versions_ago() -> anyhow::Result<()> {
// one snapshot, but it was 50 versions ago
let (mut server, client_id, versions) = av_setup(50, Some(0), None)?;
let (storage, client_id) = setup();
let versions = add_versions(&storage, client_id, 50, Some(0), None).await?;
let mut server = into_server(storage);
server.config.snapshot_versions = 30;
let result = server.add_version(client_id, versions[49], vec![1, 2, 3])?;
let result = server
.add_version(client_id, versions[49], vec![1, 2, 3])
.await?;
av_success_check(
&server,
@ -630,136 +700,165 @@ mod test {
vec![1, 2, 3],
// urgency=high due to number of versions since the snapshot
SnapshotUrgency::High,
)?;
)
.await?;
Ok(())
}
#[test]
fn add_snapshot_success_latest() -> anyhow::Result<()> {
let (server, (client_id, version_id)) = setup(|txn, client_id| {
let version_id = Uuid::new_v4();
#[tokio::test]
async fn add_snapshot_success_latest() -> anyhow::Result<()> {
let (storage, client_id) = setup();
let version_id = Uuid::new_v4();
{
let mut txn = storage.txn(client_id).await?;
// set up a task DB with one version in it
txn.new_client(version_id)?;
txn.add_version(version_id, NIL_VERSION_ID, vec![])?;
txn.new_client(version_id).await?;
txn.add_version(version_id, NIL_VERSION_ID, vec![]).await?;
// add a snapshot for that version
Ok((client_id, version_id))
})?;
server.add_snapshot(client_id, version_id, vec![1, 2, 3])?;
txn.commit().await?;
}
let server = into_server(storage);
server
.add_snapshot(client_id, version_id, vec![1, 2, 3])
.await?;
// verify the snapshot
let mut txn = server.txn(client_id)?;
let client = txn.get_client()?.unwrap();
let mut txn = server.txn(client_id).await?;
let client = txn.get_client().await?.unwrap();
let snapshot = client.snapshot.unwrap();
assert_eq!(snapshot.version_id, version_id);
assert_eq!(snapshot.versions_since, 0);
assert_eq!(
txn.get_snapshot_data(version_id).unwrap(),
txn.get_snapshot_data(version_id).await.unwrap(),
Some(vec![1, 2, 3])
);
Ok(())
}
#[test]
fn add_snapshot_success_older() -> anyhow::Result<()> {
let (server, (client_id, version_id_1)) = setup(|txn, client_id| {
let version_id_1 = Uuid::new_v4();
let version_id_2 = Uuid::new_v4();
#[tokio::test]
async fn add_snapshot_success_older() -> anyhow::Result<()> {
let (storage, client_id) = setup();
let version_id_1 = Uuid::new_v4();
let version_id_2 = Uuid::new_v4();
{
let mut txn = storage.txn(client_id).await?;
// set up a task DB with two versions in it
txn.new_client(version_id_2)?;
txn.add_version(version_id_1, NIL_VERSION_ID, vec![])?;
txn.add_version(version_id_2, version_id_1, vec![])?;
txn.new_client(version_id_2).await?;
txn.add_version(version_id_1, NIL_VERSION_ID, vec![])
.await?;
txn.add_version(version_id_2, version_id_1, vec![]).await?;
txn.commit().await?;
}
Ok((client_id, version_id_1))
})?;
// add a snapshot for version 1
server.add_snapshot(client_id, version_id_1, vec![1, 2, 3])?;
let server = into_server(storage);
server
.add_snapshot(client_id, version_id_1, vec![1, 2, 3])
.await?;
// verify the snapshot
let mut txn = server.txn(client_id)?;
let client = txn.get_client()?.unwrap();
let mut txn = server.txn(client_id).await?;
let client = txn.get_client().await?.unwrap();
let snapshot = client.snapshot.unwrap();
assert_eq!(snapshot.version_id, version_id_1);
assert_eq!(snapshot.versions_since, 0);
assert_eq!(
txn.get_snapshot_data(version_id_1).unwrap(),
txn.get_snapshot_data(version_id_1).await.unwrap(),
Some(vec![1, 2, 3])
);
Ok(())
}
#[test]
fn add_snapshot_fails_no_such() -> anyhow::Result<()> {
let (server, client_id) = setup(|txn, client_id| {
let version_id_1 = Uuid::new_v4();
let version_id_2 = Uuid::new_v4();
#[tokio::test]
async fn add_snapshot_fails_no_such() -> anyhow::Result<()> {
let (storage, client_id) = setup();
let version_id_1 = Uuid::new_v4();
let version_id_2 = Uuid::new_v4();
{
let mut txn = storage.txn(client_id).await?;
// set up a task DB with two versions in it
txn.new_client(version_id_2)?;
txn.add_version(version_id_1, NIL_VERSION_ID, vec![])?;
txn.add_version(version_id_2, version_id_1, vec![])?;
txn.new_client(version_id_2).await?;
txn.add_version(version_id_1, NIL_VERSION_ID, vec![])
.await?;
txn.add_version(version_id_2, version_id_1, vec![]).await?;
// add a snapshot for unknown version
Ok(client_id)
})?;
txn.commit().await?;
}
// add a snapshot for unknown version
let server = into_server(storage);
let version_id_unk = Uuid::new_v4();
server.add_snapshot(client_id, version_id_unk, vec![1, 2, 3])?;
server
.add_snapshot(client_id, version_id_unk, vec![1, 2, 3])
.await?;
// verify the snapshot does not exist
let mut txn = server.txn(client_id)?;
let client = txn.get_client()?.unwrap();
let mut txn = server.txn(client_id).await?;
let client = txn.get_client().await?.unwrap();
assert!(client.snapshot.is_none());
Ok(())
}
#[test]
fn add_snapshot_fails_too_old() -> anyhow::Result<()> {
let (server, (client_id, version_ids)) = setup(|txn, client_id| {
let mut version_id = Uuid::new_v4();
let mut parent_version_id = Uuid::nil();
let mut version_ids = vec![];
#[tokio::test]
async fn add_snapshot_fails_too_old() -> anyhow::Result<()> {
let (storage, client_id) = setup();
let mut version_id = Uuid::new_v4();
let mut parent_version_id = Uuid::nil();
let mut version_ids = vec![];
{
let mut txn = storage.txn(client_id).await?;
// set up a task DB with 10 versions in it (oldest to newest)
txn.new_client(Uuid::nil())?;
txn.new_client(Uuid::nil()).await?;
for _ in 0..10 {
txn.add_version(version_id, parent_version_id, vec![])?;
txn.add_version(version_id, parent_version_id, vec![])
.await?;
version_ids.push(version_id);
parent_version_id = version_id;
version_id = Uuid::new_v4();
}
// add a snapshot for the earliest of those
Ok((client_id, version_ids))
})?;
server.add_snapshot(client_id, version_ids[0], vec![1, 2, 3])?;
txn.commit().await?;
}
// add a snapshot for the earliest of those
let server = into_server(storage);
server
.add_snapshot(client_id, version_ids[0], vec![1, 2, 3])
.await?;
// verify the snapshot does not exist
let mut txn = server.txn(client_id)?;
let client = txn.get_client()?.unwrap();
let mut txn = server.txn(client_id).await?;
let client = txn.get_client().await?.unwrap();
assert!(client.snapshot.is_none());
Ok(())
}
#[test]
fn add_snapshot_fails_newer_exists() -> anyhow::Result<()> {
let (server, (client_id, version_ids)) = setup(|txn, client_id| {
let mut version_id = Uuid::new_v4();
let mut parent_version_id = Uuid::nil();
let mut version_ids = vec![];
#[tokio::test]
async fn add_snapshot_fails_newer_exists() -> anyhow::Result<()> {
let (storage, client_id) = setup();
let mut version_id = Uuid::new_v4();
let mut parent_version_id = Uuid::nil();
let mut version_ids = vec![];
{
let mut txn = storage.txn(client_id).await?;
// set up a task DB with 5 versions in it (oldest to newest) and a snapshot of the
// middle one
txn.new_client(Uuid::nil())?;
txn.new_client(Uuid::nil()).await?;
for _ in 0..5 {
txn.add_version(version_id, parent_version_id, vec![])?;
txn.add_version(version_id, parent_version_id, vec![])
.await?;
version_ids.push(version_id);
parent_version_id = version_id;
version_id = Uuid::new_v4();
@ -771,55 +870,64 @@ mod test {
timestamp: Utc.with_ymd_and_hms(2001, 9, 9, 1, 46, 40).unwrap(),
},
vec![1, 2, 3],
)?;
)
.await?;
// add a snapshot for the earliest of those
Ok((client_id, version_ids))
})?;
txn.commit().await?;
}
server.add_snapshot(client_id, version_ids[0], vec![9, 9, 9])?;
// add a snapshot for the earliest of those
let server = into_server(storage);
server
.add_snapshot(client_id, version_ids[0], vec![9, 9, 9])
.await?;
// verify the snapshot was not replaced
let mut txn = server.txn(client_id)?;
let client = txn.get_client()?.unwrap();
let mut txn = server.txn(client_id).await?;
let client = txn.get_client().await?.unwrap();
let snapshot = client.snapshot.unwrap();
assert_eq!(snapshot.version_id, version_ids[2]);
assert_eq!(snapshot.versions_since, 2);
assert_eq!(
txn.get_snapshot_data(version_ids[2]).unwrap(),
txn.get_snapshot_data(version_ids[2]).await.unwrap(),
Some(vec![1, 2, 3])
);
Ok(())
}
#[test]
fn add_snapshot_fails_nil_version() -> anyhow::Result<()> {
let (server, client_id) = setup(|txn, client_id| {
#[tokio::test]
async fn add_snapshot_fails_nil_version() -> anyhow::Result<()> {
let (storage, client_id) = setup();
{
let mut txn = storage.txn(client_id).await?;
// just set up the client
txn.new_client(NIL_VERSION_ID)?;
txn.new_client(NIL_VERSION_ID).await?;
txn.commit().await?;
}
// add a snapshot for the nil version
Ok(client_id)
})?;
server.add_snapshot(client_id, NIL_VERSION_ID, vec![9, 9, 9])?;
let server = into_server(storage);
server
.add_snapshot(client_id, NIL_VERSION_ID, vec![9, 9, 9])
.await?;
// verify the snapshot does not exist
let mut txn = server.txn(client_id)?;
let client = txn.get_client()?.unwrap();
let mut txn = server.txn(client_id).await?;
let client = txn.get_client().await?.unwrap();
assert!(client.snapshot.is_none());
Ok(())
}
#[test]
fn get_snapshot_found() -> anyhow::Result<()> {
let (server, (client_id, data, snapshot_version_id)) = setup(|txn, client_id| {
let data = vec![1, 2, 3];
let snapshot_version_id = Uuid::new_v4();
#[tokio::test]
async fn get_snapshot_found() -> anyhow::Result<()> {
let (storage, client_id) = setup();
let data = vec![1, 2, 3];
let snapshot_version_id = Uuid::new_v4();
txn.new_client(snapshot_version_id)?;
{
let mut txn = storage.txn(client_id).await?;
txn.new_client(snapshot_version_id).await?;
txn.set_snapshot(
Snapshot {
version_id: snapshot_version_id,
@ -827,25 +935,31 @@ mod test {
timestamp: Utc.with_ymd_and_hms(2001, 9, 9, 1, 46, 40).unwrap(),
},
data.clone(),
)?;
Ok((client_id, data, snapshot_version_id))
})?;
)
.await?;
txn.commit().await?;
}
let server = into_server(storage);
assert_eq!(
server.get_snapshot(client_id)?,
server.get_snapshot(client_id).await?,
Some((snapshot_version_id, data))
);
Ok(())
}
#[test]
fn get_snapshot_not_found() -> anyhow::Result<()> {
let (server, client_id) = setup(|txn, client_id| {
txn.new_client(NIL_VERSION_ID)?;
Ok(client_id)
})?;
#[tokio::test]
async fn get_snapshot_not_found() -> anyhow::Result<()> {
let (storage, client_id) = setup();
{
let mut txn = storage.txn(client_id).await?;
txn.new_client(NIL_VERSION_ID).await?;
txn.commit().await?;
}
assert_eq!(server.get_snapshot(client_id)?, None);
let server = into_server(storage);
assert_eq!(server.get_snapshot(client_id).await?, None);
Ok(())
}

View file

@ -44,32 +44,35 @@ pub struct Version {
///
/// Changes in a transaction that is dropped without calling `commit` must not appear in any other
/// transaction.
#[async_trait::async_trait(?Send)]
pub trait StorageTxn {
/// Get information about the client for this transaction
fn get_client(&mut self) -> anyhow::Result<Option<Client>>;
async fn get_client(&mut self) -> anyhow::Result<Option<Client>>;
/// Create the client for this transaction, with the given latest_version_id. The client must
/// not already exist.
fn new_client(&mut self, latest_version_id: Uuid) -> anyhow::Result<()>;
async fn new_client(&mut self, latest_version_id: Uuid) -> anyhow::Result<()>;
/// Set the client's most recent snapshot.
fn set_snapshot(&mut self, snapshot: Snapshot, data: Vec<u8>) -> anyhow::Result<()>;
async fn set_snapshot(&mut self, snapshot: Snapshot, data: Vec<u8>) -> anyhow::Result<()>;
/// Get the data for the most recent snapshot. The version_id
/// is used to verify that the snapshot is for the correct version.
fn get_snapshot_data(&mut self, version_id: Uuid) -> anyhow::Result<Option<Vec<u8>>>;
async fn get_snapshot_data(&mut self, version_id: Uuid) -> anyhow::Result<Option<Vec<u8>>>;
/// Get a version, indexed by parent version id
fn get_version_by_parent(&mut self, parent_version_id: Uuid)
-> anyhow::Result<Option<Version>>;
async fn get_version_by_parent(
&mut self,
parent_version_id: Uuid,
) -> anyhow::Result<Option<Version>>;
/// Get a version, indexed by its own version id
fn get_version(&mut self, version_id: Uuid) -> anyhow::Result<Option<Version>>;
async fn get_version(&mut self, version_id: Uuid) -> anyhow::Result<Option<Version>>;
/// Add a version (that must not already exist), and
/// - update latest_version_id
/// - increment snapshot.versions_since
fn add_version(
async fn add_version(
&mut self,
version_id: Uuid,
parent_version_id: Uuid,
@ -78,12 +81,13 @@ pub trait StorageTxn {
/// Commit any changes made in the transaction. It is an error to call this more than
/// once. It is safe to skip this call for read-only operations.
fn commit(&mut self) -> anyhow::Result<()>;
async fn commit(&mut self) -> anyhow::Result<()>;
}
/// A trait for objects able to act as storage. Most of the interesting behavior is in the
/// [`crate::storage::StorageTxn`] trait.
#[async_trait::async_trait]
pub trait Storage: Send + Sync {
/// Begin a transaction for the given client ID.
fn txn(&self, client_id: Uuid) -> anyhow::Result<Box<dyn StorageTxn + '_>>;
async fn txn(&self, client_id: Uuid) -> anyhow::Result<Box<dyn StorageTxn + '_>>;
}