forked from GothenburgBitFactory/taskchampion-sync-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
669 lines (606 loc) · 22.8 KB
/
lib.rs
File metadata and controls
669 lines (606 loc) · 22.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
//! This crate implements a Postgres storage backend for the TaskChampion sync server.
//!
//! Use the [`PostgresStorage`] type as an implementation of the [`Storage`] trait.
//!
//! This implementation is tested with Postgres version 17 but should work with any recent version.
//!
//! ## Schema Setup
//!
//! The database identified by the connection string must already exist and be set up with the
//! following schema (also available in `postgres/schema.sql` in the repository):
//!
//! ```sql
#![doc=include_str!("../schema.sql")]
//! ```
//!
//! ## Integration with External Applications
//!
//! The schema is stable, and any changes to the schema will be made in a major version with
//! migration instructions provided.
//!
//! An external application may:
//! - Add additional tables to the database
//! - Add additional columns to the `clients` table. If those columns do not have default
//! values, calls to `Txn::new_client` will fail. It is possible to configure
//! `taskchampion-sync-server` to never call this method.
//! - Insert rows into the `clients` table, using default values for all columns except
//! `client_id` and application-specific columns.
//! - Delete rows from the `clients` table, using `CASCADE` to ensure any associated data
//! is also deleted.
use anyhow::Context;
use bb8::PooledConnection;
use bb8_postgres::PostgresConnectionManager;
use chrono::{TimeZone, Utc};
use postgres_native_tls::MakeTlsConnector;
use taskchampion_sync_server_core::{Client, Snapshot, Storage, StorageTxn, Version};
use uuid::Uuid;
#[cfg(test)]
mod testing;
/// A storage backend which uses Postgres.
pub struct PostgresStorage {
pool: bb8::Pool<PostgresConnectionManager<MakeTlsConnector>>,
}
impl PostgresStorage {
pub async fn new(connection_string: impl ToString) -> anyhow::Result<Self> {
let connector = native_tls::TlsConnector::new()?;
let connector = postgres_native_tls::MakeTlsConnector::new(connector);
let manager = PostgresConnectionManager::new_from_stringlike(connection_string, connector)?;
let pool = bb8::Pool::builder().build(manager).await?;
Ok(Self { pool })
}
}
#[async_trait::async_trait]
impl Storage for PostgresStorage {
async fn txn(&self, client_id: Uuid) -> anyhow::Result<Box<dyn StorageTxn + '_>> {
let db_client = self.pool.get_owned().await?;
db_client
.execute("BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE", &[])
.await?;
Ok(Box::new(Txn {
client_id,
db_client: Some(db_client),
}))
}
}
struct Txn {
client_id: Uuid,
/// The DB client or, if `commit` has been called, None. This ensures queries aren't executed
/// after commit, and also frees connections back to the pool as quickly as possible.
db_client: Option<PooledConnection<'static, PostgresConnectionManager<MakeTlsConnector>>>,
}
impl Txn {
/// Get the db_client, or panic if it is gone (after commit).
fn db_client(&self) -> &tokio_postgres::Client {
let Some(db_client) = &self.db_client else {
panic!("Cannot use a postgres Txn after commit");
};
db_client
}
/// Implementation for queries from the versions table
async fn get_version_impl(
&mut self,
query: &'static str,
client_id: Uuid,
version_id_arg: Uuid,
) -> anyhow::Result<Option<Version>> {
Ok(self
.db_client()
.query_opt(query, &[&version_id_arg, &client_id])
.await
.context("error getting version")?
.map(|r| Version {
version_id: r.get(0),
parent_version_id: r.get(1),
history_segment: r.get("history_segment"),
}))
}
}
#[async_trait::async_trait(?Send)]
impl StorageTxn for Txn {
async fn get_client(&mut self) -> anyhow::Result<Option<Client>> {
Ok(self
.db_client()
.query_opt(
"SELECT
latest_version_id,
snapshot_timestamp,
versions_since_snapshot,
snapshot_version_id
FROM clients
WHERE client_id = $1
LIMIT 1",
&[&self.client_id],
)
.await
.context("error getting client")?
.map(|r| {
let latest_version_id: Uuid = r.get(0);
let snapshot_timestamp: Option<i64> = r.get(1);
let versions_since_snapshot: Option<i32> = r.get(2);
let snapshot_version_id: Option<Uuid> = r.get(3);
// if all of the relevant fields are non-NULL, return a snapshot
let snapshot = match (
snapshot_timestamp,
versions_since_snapshot,
snapshot_version_id,
) {
(Some(ts), Some(vs), Some(v)) => Some(Snapshot {
version_id: v,
timestamp: Utc.timestamp_opt(ts, 0).unwrap(),
versions_since: vs as u32,
}),
_ => None,
};
Client {
latest_version_id,
snapshot,
}
}))
}
async fn new_client(&mut self, latest_version_id: Uuid) -> anyhow::Result<()> {
self.db_client()
.execute(
"INSERT INTO clients (client_id, latest_version_id) VALUES ($1, $2)",
&[&self.client_id, &latest_version_id],
)
.await
.context("error creating/updating client")?;
Ok(())
}
async fn set_snapshot(&mut self, snapshot: Snapshot, data: Vec<u8>) -> anyhow::Result<()> {
let timestamp = snapshot.timestamp.timestamp();
self.db_client()
.execute(
"UPDATE clients
SET snapshot_version_id = $1,
versions_since_snapshot = $2,
snapshot_timestamp = $3,
snapshot = $4
WHERE client_id = $5",
&[
&snapshot.version_id,
&(snapshot.versions_since as i32),
×tamp,
&data,
&self.client_id,
],
)
.await
.context("error setting snapshot")?;
Ok(())
}
async fn get_snapshot_data(&mut self, version_id: Uuid) -> anyhow::Result<Option<Vec<u8>>> {
Ok(self
.db_client()
.query_opt(
"SELECT snapshot
FROM clients
WHERE client_id = $1 and snapshot_version_id = $2
LIMIT 1",
&[&self.client_id, &version_id],
)
.await
.context("error getting snapshot data")?
.map(|r| r.get(0)))
}
async fn get_version_by_parent(
&mut self,
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 = $1 AND client_id = $2",
self.client_id,
parent_version_id,
)
.await
}
async 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 = $1 AND client_id = $2",
self.client_id,
version_id,
)
.await
}
async fn add_version(
&mut self,
version_id: Uuid,
parent_version_id: Uuid,
history_segment: Vec<u8>,
) -> anyhow::Result<()> {
self.db_client()
.execute(
"INSERT INTO versions (version_id, client_id, parent_version_id, history_segment)
VALUES ($1, $2, $3, $4)",
&[
&version_id,
&self.client_id,
&parent_version_id,
&history_segment,
],
)
.await
.context("error inserting new version")?;
let rows_modified = self
.db_client()
.execute(
"UPDATE clients
SET latest_version_id = $1,
versions_since_snapshot = versions_since_snapshot + 1
WHERE client_id = $2 and latest_version_id = $3",
&[&version_id, &self.client_id, &parent_version_id],
)
.await
.context("error updating latest_version_id")?;
// If no rows were modified, this operation failed.
if rows_modified == 0 {
anyhow::bail!("clients.latest_version_id does not match parent_version_id");
}
Ok(())
}
async fn commit(&mut self) -> anyhow::Result<()> {
self.db_client().execute("COMMIT", &[]).await?;
self.db_client = None;
Ok(())
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::testing::with_db;
async fn make_client(db_client: &tokio_postgres::Client) -> anyhow::Result<Uuid> {
let client_id = Uuid::new_v4();
db_client
.execute("insert into clients (client_id) values ($1)", &[&client_id])
.await?;
Ok(client_id)
}
async fn make_version(
db_client: &tokio_postgres::Client,
client_id: Uuid,
parent_version_id: Uuid,
history_segment: &[u8],
) -> anyhow::Result<Uuid> {
let version_id = Uuid::new_v4();
db_client
.execute(
"insert into versions
(version_id, client_id, parent_version_id, history_segment)
values ($1, $2, $3, $4)",
&[
&version_id,
&client_id,
&parent_version_id,
&history_segment,
],
)
.await?;
Ok(version_id)
}
async fn set_client_latest_version_id(
db_client: &tokio_postgres::Client,
client_id: Uuid,
latest_version_id: Uuid,
) -> anyhow::Result<()> {
db_client
.execute(
"update clients set latest_version_id = $1 where client_id = $2",
&[&latest_version_id, &client_id],
)
.await?;
Ok(())
}
async fn set_client_snapshot(
db_client: &tokio_postgres::Client,
client_id: Uuid,
snapshot_version_id: Uuid,
versions_since_snapshot: u32,
snapshot_timestamp: i64,
snapshot: &[u8],
) -> anyhow::Result<()> {
db_client
.execute(
"
update clients
set snapshot_version_id = $1,
versions_since_snapshot = $2,
snapshot_timestamp = $3,
snapshot = $4
where client_id = $5",
&[
&snapshot_version_id,
&(versions_since_snapshot as i32),
&snapshot_timestamp,
&snapshot,
&client_id,
],
)
.await?;
Ok(())
}
#[tokio::test]
async fn test_get_client_none() -> anyhow::Result<()> {
with_db(async |connection_string, _db_client| {
let storage = PostgresStorage::new(connection_string).await?;
let client_id = Uuid::new_v4();
let mut txn = storage.txn(client_id).await?;
assert_eq!(txn.get_client().await?, None);
Ok(())
})
.await
}
#[tokio::test]
async fn test_get_client_exists_empty() -> anyhow::Result<()> {
with_db(async |connection_string, db_client| {
let storage = PostgresStorage::new(connection_string).await?;
let client_id = make_client(&db_client).await?;
let mut txn = storage.txn(client_id).await?;
assert_eq!(
txn.get_client().await?,
Some(Client {
latest_version_id: Uuid::nil(),
snapshot: None
})
);
Ok(())
})
.await
}
#[tokio::test]
async fn test_get_client_exists_latest() -> anyhow::Result<()> {
with_db(async |connection_string, db_client| {
let storage = PostgresStorage::new(connection_string).await?;
let client_id = make_client(&db_client).await?;
let latest_version_id = Uuid::new_v4();
set_client_latest_version_id(&db_client, client_id, latest_version_id).await?;
let mut txn = storage.txn(client_id).await?;
assert_eq!(
txn.get_client().await?,
Some(Client {
latest_version_id,
snapshot: None
})
);
Ok(())
})
.await
}
#[tokio::test]
async fn test_get_client_exists_with_snapshot() -> anyhow::Result<()> {
with_db(async |connection_string, db_client| {
let storage = PostgresStorage::new(connection_string).await?;
let client_id = make_client(&db_client).await?;
let snapshot_version_id = Uuid::new_v4();
let versions_since_snapshot = 10;
let snapshot_timestamp = 10000000;
let snapshot = b"abcd";
set_client_snapshot(
&db_client,
client_id,
snapshot_version_id,
versions_since_snapshot,
snapshot_timestamp,
snapshot,
)
.await?;
let mut txn = storage.txn(client_id).await?;
assert_eq!(
txn.get_client().await?,
Some(Client {
latest_version_id: Uuid::nil(),
snapshot: Some(Snapshot {
version_id: snapshot_version_id,
timestamp: Utc.timestamp_opt(snapshot_timestamp, 0).unwrap(),
versions_since: versions_since_snapshot,
})
})
);
Ok(())
})
.await
}
#[tokio::test]
async fn test_new_client() -> anyhow::Result<()> {
with_db(async |connection_string, _db_client| {
let storage = PostgresStorage::new(connection_string).await?;
let client_id = Uuid::new_v4();
let latest_version_id = Uuid::new_v4();
let mut txn1 = storage.txn(client_id).await?;
txn1.new_client(latest_version_id).await?;
// Client is not visible yet as txn1 is not committed.
let mut txn2 = storage.txn(client_id).await?;
assert_eq!(txn2.get_client().await?, None);
txn1.commit().await?;
// Client is now visible.
let mut txn2 = storage.txn(client_id).await?;
assert_eq!(
txn2.get_client().await?,
Some(Client {
latest_version_id,
snapshot: None
})
);
Ok(())
})
.await
}
#[tokio::test]
async fn test_set_snapshot() -> anyhow::Result<()> {
with_db(async |connection_string, db_client| {
let storage = PostgresStorage::new(connection_string).await?;
let client_id = make_client(&db_client).await?;
let mut txn = storage.txn(client_id).await?;
let snapshot_version_id = Uuid::new_v4();
let versions_since_snapshot = 10;
let snapshot_timestamp = 10000000;
let snapshot = b"abcd";
txn.set_snapshot(
Snapshot {
version_id: snapshot_version_id,
timestamp: Utc.timestamp_opt(snapshot_timestamp, 0).unwrap(),
versions_since: versions_since_snapshot,
},
snapshot.to_vec(),
)
.await?;
txn.commit().await?;
txn = storage.txn(client_id).await?;
assert_eq!(
txn.get_client().await?,
Some(Client {
latest_version_id: Uuid::nil(),
snapshot: Some(Snapshot {
version_id: snapshot_version_id,
timestamp: Utc.timestamp_opt(snapshot_timestamp, 0).unwrap(),
versions_since: versions_since_snapshot,
})
})
);
let row = db_client
.query_one(
"select snapshot from clients where client_id = $1",
&[&client_id],
)
.await?;
assert_eq!(row.get::<_, &[u8]>(0), b"abcd");
Ok(())
})
.await
}
#[tokio::test]
async fn test_get_snapshot_none() -> anyhow::Result<()> {
with_db(async |connection_string, db_client| {
let storage = PostgresStorage::new(connection_string).await?;
let client_id = make_client(&db_client).await?;
let mut txn = storage.txn(client_id).await?;
assert_eq!(txn.get_snapshot_data(Uuid::new_v4()).await?, None);
Ok(())
})
.await
}
#[tokio::test]
async fn test_get_snapshot_mismatched_version() -> anyhow::Result<()> {
with_db(async |connection_string, db_client| {
let storage = PostgresStorage::new(connection_string).await?;
let client_id = make_client(&db_client).await?;
let mut txn = storage.txn(client_id).await?;
let snapshot_version_id = Uuid::new_v4();
let versions_since_snapshot = 10;
let snapshot_timestamp = 10000000;
let snapshot = b"abcd";
txn.set_snapshot(
Snapshot {
version_id: snapshot_version_id,
timestamp: Utc.timestamp_opt(snapshot_timestamp, 0).unwrap(),
versions_since: versions_since_snapshot,
},
snapshot.to_vec(),
)
.await?;
assert_eq!(txn.get_snapshot_data(Uuid::new_v4()).await?, None);
Ok(())
})
.await
}
#[tokio::test]
async fn test_get_version() -> anyhow::Result<()> {
with_db(async |connection_string, db_client| {
let storage = PostgresStorage::new(connection_string).await?;
let client_id = make_client(&db_client).await?;
let parent_version_id = Uuid::new_v4();
let version_id = make_version(&db_client, client_id, parent_version_id, b"v1").await?;
let mut txn = storage.txn(client_id).await?;
// Different parent doesn't exist.
assert_eq!(txn.get_version_by_parent(Uuid::new_v4()).await?, None);
// Different version doesn't exist.
assert_eq!(txn.get_version(Uuid::new_v4()).await?, None);
let version = Version {
version_id,
parent_version_id,
history_segment: b"v1".to_vec(),
};
// Version found by parent.
assert_eq!(
txn.get_version_by_parent(parent_version_id).await?,
Some(version.clone())
);
// Version found by ID.
assert_eq!(txn.get_version(version_id).await?, Some(version));
Ok(())
})
.await
}
#[tokio::test]
async fn test_add_version() -> anyhow::Result<()> {
with_db(async |connection_string, db_client| {
let storage = PostgresStorage::new(connection_string).await?;
let client_id = make_client(&db_client).await?;
let mut txn = storage.txn(client_id).await?;
let version_id = Uuid::new_v4();
txn.add_version(version_id, Uuid::nil(), b"v1".to_vec())
.await?;
assert_eq!(
txn.get_version(version_id).await?,
Some(Version {
version_id,
parent_version_id: Uuid::nil(),
history_segment: b"v1".to_vec()
})
);
Ok(())
})
.await
}
#[tokio::test]
/// When an add_version call specifies an incorrect `parent_version_id, it fails. This is
/// typically avoided by calling `get_client` beforehand, which (due to repeatable reads)
/// allows the caller to check the `latest_version_id` before calling `add_version`.
async fn test_add_version_mismatch() -> anyhow::Result<()> {
with_db(async |connection_string, db_client| {
let storage = PostgresStorage::new(connection_string).await?;
let client_id = make_client(&db_client).await?;
let latest_version_id = Uuid::new_v4();
set_client_latest_version_id(&db_client, client_id, latest_version_id).await?;
let mut txn = storage.txn(client_id).await?;
let version_id = Uuid::new_v4();
let parent_version_id = Uuid::new_v4(); // != latest_version_id
let res = txn
.add_version(version_id, parent_version_id, b"v1".to_vec())
.await;
assert!(res.is_err());
Ok(())
})
.await
}
#[tokio::test]
/// Adding versions to two different clients can proceed concurrently.
async fn test_add_version_no_conflict_different_clients() -> anyhow::Result<()> {
with_db(async |connection_string, db_client| {
let storage = PostgresStorage::new(connection_string).await?;
// Clients 1 and 2 do not interfere with each other; if these are the same client, then
// this will deadlock as one transaction waits for the other. If the postgres storage
// implementation serialized _all_ transactions across clients, that would limit its
// scalability.
//
// So the asertion here is "does not deadlock".
let client_id1 = make_client(&db_client).await?;
let mut txn1 = storage.txn(client_id1).await?;
let version_id1 = Uuid::new_v4();
txn1.add_version(version_id1, Uuid::nil(), b"v1".to_vec())
.await?;
let client_id2 = make_client(&db_client).await?;
let mut txn2 = storage.txn(client_id2).await?;
let version_id2 = Uuid::new_v4();
txn2.add_version(version_id2, Uuid::nil(), b"v2".to_vec())
.await?;
txn1.commit().await?;
txn2.commit().await?;
Ok(())
})
.await
}
}