mirror of
https://github.com/GothenburgBitFactory/taskchampion-sync-server.git
synced 2025-08-02 14:38:36 +02:00
Make storage transactions specific to a client_id (#67)
Transactions for different client_ids cannot interfere with one another, so this provides an opportunity for the sort of concurrency that a mult-client hosting solution might need. For example, a postgres backend could lock the client row in each transaction.
This commit is contained in:
parent
4029c03479
commit
1828a31a24
9 changed files with 246 additions and 319 deletions
|
@ -78,12 +78,12 @@ impl SqliteStorage {
|
|||
}
|
||||
|
||||
impl Storage for SqliteStorage {
|
||||
fn txn<'a>(&'a self) -> anyhow::Result<Box<dyn StorageTxn + 'a>> {
|
||||
fn txn(&self, client_id: Uuid) -> anyhow::Result<Box<dyn StorageTxn + '_>> {
|
||||
let con = self.new_connection()?;
|
||||
// Begin the transaction on this new connection. An IMMEDIATE connection is in
|
||||
// write (exclusive) mode from the start.
|
||||
con.execute("BEGIN IMMEDIATE", [])?;
|
||||
let txn = Txn { con };
|
||||
let txn = Txn { con, client_id };
|
||||
Ok(Box::new(txn))
|
||||
}
|
||||
}
|
||||
|
@ -93,6 +93,7 @@ struct Txn {
|
|||
// transactions by running `BEGIN ...` and `COMMIT` at appropriate times. So we will do
|
||||
// the same.
|
||||
con: Connection,
|
||||
client_id: Uuid,
|
||||
}
|
||||
|
||||
impl Txn {
|
||||
|
@ -126,7 +127,7 @@ impl Txn {
|
|||
}
|
||||
|
||||
impl StorageTxn for Txn {
|
||||
fn get_client(&mut self, client_id: Uuid) -> anyhow::Result<Option<Client>> {
|
||||
fn get_client(&mut self) -> anyhow::Result<Option<Client>> {
|
||||
let result: Option<Client> = self
|
||||
.con
|
||||
.query_row(
|
||||
|
@ -138,7 +139,7 @@ impl StorageTxn for Txn {
|
|||
FROM clients
|
||||
WHERE client_id = ?
|
||||
LIMIT 1",
|
||||
[&StoredUuid(client_id)],
|
||||
[&StoredUuid(self.client_id)],
|
||||
|r| {
|
||||
let latest_version_id: StoredUuid = r.get(0)?;
|
||||
let snapshot_timestamp: Option<i64> = r.get(1)?;
|
||||
|
@ -170,22 +171,17 @@ impl StorageTxn for Txn {
|
|||
Ok(result)
|
||||
}
|
||||
|
||||
fn new_client(&mut self, client_id: Uuid, latest_version_id: Uuid) -> anyhow::Result<()> {
|
||||
fn new_client(&mut self, latest_version_id: Uuid) -> anyhow::Result<()> {
|
||||
self.con
|
||||
.execute(
|
||||
"INSERT OR REPLACE INTO clients (client_id, latest_version_id) VALUES (?, ?)",
|
||||
params![&StoredUuid(client_id), &StoredUuid(latest_version_id)],
|
||||
params![&StoredUuid(self.client_id), &StoredUuid(latest_version_id)],
|
||||
)
|
||||
.context("Error creating/updating client")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_snapshot(
|
||||
&mut self,
|
||||
client_id: Uuid,
|
||||
snapshot: Snapshot,
|
||||
data: Vec<u8>,
|
||||
) -> anyhow::Result<()> {
|
||||
fn set_snapshot(&mut self, snapshot: Snapshot, data: Vec<u8>) -> anyhow::Result<()> {
|
||||
self.con
|
||||
.execute(
|
||||
"UPDATE clients
|
||||
|
@ -200,23 +196,19 @@ impl StorageTxn for Txn {
|
|||
snapshot.timestamp.timestamp(),
|
||||
snapshot.versions_since,
|
||||
data,
|
||||
&StoredUuid(client_id),
|
||||
&StoredUuid(self.client_id),
|
||||
],
|
||||
)
|
||||
.context("Error creating/updating snapshot")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_snapshot_data(
|
||||
&mut self,
|
||||
client_id: Uuid,
|
||||
version_id: Uuid,
|
||||
) -> anyhow::Result<Option<Vec<u8>>> {
|
||||
fn get_snapshot_data(&mut self, version_id: Uuid) -> anyhow::Result<Option<Vec<u8>>> {
|
||||
let r = self
|
||||
.con
|
||||
.query_row(
|
||||
"SELECT snapshot, snapshot_version_id FROM clients WHERE client_id = ?",
|
||||
params![&StoredUuid(client_id)],
|
||||
params![&StoredUuid(self.client_id)],
|
||||
|r| {
|
||||
let v: StoredUuid = r.get("snapshot_version_id")?;
|
||||
let d: Vec<u8> = r.get("snapshot")?;
|
||||
|
@ -237,29 +229,25 @@ impl StorageTxn for Txn {
|
|||
|
||||
fn get_version_by_parent(
|
||||
&mut self,
|
||||
client_id: Uuid,
|
||||
|
||||
parent_version_id: Uuid,
|
||||
) -> anyhow::Result<Option<Version>> {
|
||||
self.get_version_impl(
|
||||
"SELECT version_id, parent_version_id, history_segment FROM versions WHERE parent_version_id = ? AND client_id = ?",
|
||||
client_id,
|
||||
self.client_id,
|
||||
parent_version_id)
|
||||
}
|
||||
|
||||
fn get_version(
|
||||
&mut self,
|
||||
client_id: Uuid,
|
||||
version_id: Uuid,
|
||||
) -> anyhow::Result<Option<Version>> {
|
||||
fn get_version(&mut self, version_id: Uuid) -> anyhow::Result<Option<Version>> {
|
||||
self.get_version_impl(
|
||||
"SELECT version_id, parent_version_id, history_segment FROM versions WHERE version_id = ? AND client_id = ?",
|
||||
client_id,
|
||||
self.client_id,
|
||||
version_id)
|
||||
}
|
||||
|
||||
fn add_version(
|
||||
&mut self,
|
||||
client_id: Uuid,
|
||||
|
||||
version_id: Uuid,
|
||||
parent_version_id: Uuid,
|
||||
history_segment: Vec<u8>,
|
||||
|
@ -268,7 +256,7 @@ impl StorageTxn for Txn {
|
|||
"INSERT INTO versions (version_id, client_id, parent_version_id, history_segment) VALUES(?, ?, ?, ?)",
|
||||
params![
|
||||
StoredUuid(version_id),
|
||||
StoredUuid(client_id),
|
||||
StoredUuid(self.client_id),
|
||||
StoredUuid(parent_version_id),
|
||||
history_segment
|
||||
]
|
||||
|
@ -281,7 +269,7 @@ impl StorageTxn for Txn {
|
|||
latest_version_id = ?,
|
||||
versions_since_snapshot = versions_since_snapshot + 1
|
||||
WHERE client_id = ?",
|
||||
params![StoredUuid(version_id), StoredUuid(client_id),],
|
||||
params![StoredUuid(version_id), StoredUuid(self.client_id),],
|
||||
)
|
||||
.context("Error updating client for new version")?;
|
||||
|
||||
|
@ -306,8 +294,9 @@ mod test {
|
|||
let tmp_dir = TempDir::new()?;
|
||||
let non_existant = tmp_dir.path().join("subdir");
|
||||
let storage = SqliteStorage::new(non_existant)?;
|
||||
let mut txn = storage.txn()?;
|
||||
let maybe_client = txn.get_client(Uuid::new_v4())?;
|
||||
let client_id = Uuid::new_v4();
|
||||
let mut txn = storage.txn(client_id)?;
|
||||
let maybe_client = txn.get_client()?;
|
||||
assert!(maybe_client.is_none());
|
||||
Ok(())
|
||||
}
|
||||
|
@ -316,8 +305,9 @@ mod test {
|
|||
fn test_get_client_empty() -> anyhow::Result<()> {
|
||||
let tmp_dir = TempDir::new()?;
|
||||
let storage = SqliteStorage::new(tmp_dir.path())?;
|
||||
let mut txn = storage.txn()?;
|
||||
let maybe_client = txn.get_client(Uuid::new_v4())?;
|
||||
let client_id = Uuid::new_v4();
|
||||
let mut txn = storage.txn(client_id)?;
|
||||
let maybe_client = txn.get_client()?;
|
||||
assert!(maybe_client.is_none());
|
||||
Ok(())
|
||||
}
|
||||
|
@ -326,20 +316,20 @@ mod test {
|
|||
fn test_client_storage() -> anyhow::Result<()> {
|
||||
let tmp_dir = TempDir::new()?;
|
||||
let storage = SqliteStorage::new(tmp_dir.path())?;
|
||||
let mut txn = storage.txn()?;
|
||||
|
||||
let client_id = Uuid::new_v4();
|
||||
let latest_version_id = Uuid::new_v4();
|
||||
txn.new_client(client_id, latest_version_id)?;
|
||||
let mut txn = storage.txn(client_id)?;
|
||||
|
||||
let client = txn.get_client(client_id)?.unwrap();
|
||||
let latest_version_id = Uuid::new_v4();
|
||||
txn.new_client(latest_version_id)?;
|
||||
|
||||
let client = txn.get_client()?.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(client_id, latest_version_id, Uuid::new_v4(), vec![1, 1])?;
|
||||
txn.add_version(latest_version_id, Uuid::new_v4(), vec![1, 1])?;
|
||||
|
||||
let client = txn.get_client(client_id)?.unwrap();
|
||||
let client = txn.get_client()?.unwrap();
|
||||
assert_eq!(client.latest_version_id, latest_version_id);
|
||||
assert!(client.snapshot.is_none());
|
||||
|
||||
|
@ -348,9 +338,9 @@ mod test {
|
|||
timestamp: "2014-11-28T12:00:09Z".parse::<DateTime<Utc>>().unwrap(),
|
||||
versions_since: 4,
|
||||
};
|
||||
txn.set_snapshot(client_id, snap.clone(), vec![1, 2, 3])?;
|
||||
txn.set_snapshot(snap.clone(), vec![1, 2, 3])?;
|
||||
|
||||
let client = txn.get_client(client_id)?.unwrap();
|
||||
let client = txn.get_client()?.unwrap();
|
||||
assert_eq!(client.latest_version_id, latest_version_id);
|
||||
assert_eq!(client.snapshot.unwrap(), snap);
|
||||
|
||||
|
@ -361,8 +351,9 @@ mod test {
|
|||
fn test_gvbp_empty() -> anyhow::Result<()> {
|
||||
let tmp_dir = TempDir::new()?;
|
||||
let storage = SqliteStorage::new(tmp_dir.path())?;
|
||||
let mut txn = storage.txn()?;
|
||||
let maybe_version = txn.get_version_by_parent(Uuid::new_v4(), Uuid::new_v4())?;
|
||||
let client_id = Uuid::new_v4();
|
||||
let mut txn = storage.txn(client_id)?;
|
||||
let maybe_version = txn.get_version_by_parent(Uuid::new_v4())?;
|
||||
assert!(maybe_version.is_none());
|
||||
Ok(())
|
||||
}
|
||||
|
@ -371,18 +362,13 @@ mod test {
|
|||
fn test_add_version_and_get_version() -> anyhow::Result<()> {
|
||||
let tmp_dir = TempDir::new()?;
|
||||
let storage = SqliteStorage::new(tmp_dir.path())?;
|
||||
let mut txn = storage.txn()?;
|
||||
|
||||
let client_id = Uuid::new_v4();
|
||||
let mut txn = storage.txn(client_id)?;
|
||||
|
||||
let version_id = Uuid::new_v4();
|
||||
let parent_version_id = Uuid::new_v4();
|
||||
let history_segment = b"abc".to_vec();
|
||||
txn.add_version(
|
||||
client_id,
|
||||
version_id,
|
||||
parent_version_id,
|
||||
history_segment.clone(),
|
||||
)?;
|
||||
txn.add_version(version_id, parent_version_id, history_segment.clone())?;
|
||||
|
||||
let expected = Version {
|
||||
version_id,
|
||||
|
@ -390,12 +376,10 @@ mod test {
|
|||
history_segment,
|
||||
};
|
||||
|
||||
let version = txn
|
||||
.get_version_by_parent(client_id, parent_version_id)?
|
||||
.unwrap();
|
||||
let version = txn.get_version_by_parent(parent_version_id)?.unwrap();
|
||||
assert_eq!(version, expected);
|
||||
|
||||
let version = txn.get_version(client_id, version_id)?.unwrap();
|
||||
let version = txn.get_version(version_id)?.unwrap();
|
||||
assert_eq!(version, expected);
|
||||
|
||||
Ok(())
|
||||
|
@ -405,41 +389,40 @@ mod test {
|
|||
fn test_snapshots() -> anyhow::Result<()> {
|
||||
let tmp_dir = TempDir::new()?;
|
||||
let storage = SqliteStorage::new(tmp_dir.path())?;
|
||||
let mut txn = storage.txn()?;
|
||||
|
||||
let client_id = Uuid::new_v4();
|
||||
let mut txn = storage.txn(client_id)?;
|
||||
|
||||
txn.new_client(client_id, Uuid::new_v4())?;
|
||||
assert!(txn.get_client(client_id)?.unwrap().snapshot.is_none());
|
||||
txn.new_client(Uuid::new_v4())?;
|
||||
assert!(txn.get_client()?.unwrap().snapshot.is_none());
|
||||
|
||||
let snap = Snapshot {
|
||||
version_id: Uuid::new_v4(),
|
||||
timestamp: "2013-10-08T12:00:09Z".parse::<DateTime<Utc>>().unwrap(),
|
||||
versions_since: 3,
|
||||
};
|
||||
txn.set_snapshot(client_id, snap.clone(), vec![9, 8, 9])?;
|
||||
txn.set_snapshot(snap.clone(), vec![9, 8, 9])?;
|
||||
|
||||
assert_eq!(
|
||||
txn.get_snapshot_data(client_id, snap.version_id)?.unwrap(),
|
||||
txn.get_snapshot_data(snap.version_id)?.unwrap(),
|
||||
vec![9, 8, 9]
|
||||
);
|
||||
assert_eq!(txn.get_client(client_id)?.unwrap().snapshot, Some(snap));
|
||||
assert_eq!(txn.get_client()?.unwrap().snapshot, Some(snap));
|
||||
|
||||
let snap2 = Snapshot {
|
||||
version_id: Uuid::new_v4(),
|
||||
timestamp: "2014-11-28T12:00:09Z".parse::<DateTime<Utc>>().unwrap(),
|
||||
versions_since: 10,
|
||||
};
|
||||
txn.set_snapshot(client_id, snap2.clone(), vec![0, 2, 4, 6])?;
|
||||
txn.set_snapshot(snap2.clone(), vec![0, 2, 4, 6])?;
|
||||
|
||||
assert_eq!(
|
||||
txn.get_snapshot_data(client_id, snap2.version_id)?.unwrap(),
|
||||
txn.get_snapshot_data(snap2.version_id)?.unwrap(),
|
||||
vec![0, 2, 4, 6]
|
||||
);
|
||||
assert_eq!(txn.get_client(client_id)?.unwrap().snapshot, Some(snap2));
|
||||
assert_eq!(txn.get_client()?.unwrap().snapshot, Some(snap2));
|
||||
|
||||
// check that mismatched version is detected
|
||||
assert!(txn.get_snapshot_data(client_id, Uuid::new_v4()).is_err());
|
||||
assert!(txn.get_snapshot_data(Uuid::new_v4()).is_err());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue