@feltdb/core 0.6.14 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -0
- package/dist/create/server-source/crates/feltdb/src/bin/feltdb_node.rs +116 -0
- package/dist/create/server-source/crates/feltdb/src/distributed_transactions.rs +19 -0
- package/dist/create/server-source/crates/feltdb/src/lib.rs +280 -0
- package/dist/create/server-source/crates/feltdb/src/replica_acknowledgements.rs +471 -0
- package/dist/create/server-source/crates/feltdb/src/tcp_transport.rs +72 -3
- package/dist/create/server-source/crates/feltdb/src/transaction_preconditions.rs +899 -0
- package/dist/create/server-source/crates/feltdb-server/src/main.rs +116 -4
- package/dist/db.d.ts +2 -2
- package/dist/db.d.ts.map +1 -1
- package/dist/db.js +29 -2
- package/dist/embedded-transaction.d.ts +9 -0
- package/dist/embedded-transaction.d.ts.map +1 -1
- package/dist/embedded-transaction.js +95 -3
- package/dist/feltdb.d.ts +16 -0
- package/dist/feltdb.d.ts.map +1 -1
- package/dist/file-db.d.ts.map +1 -1
- package/dist/file-db.js +1 -0
- package/dist/http-db.d.ts +11 -0
- package/dist/http-db.d.ts.map +1 -1
- package/dist/http-db.js +16 -2
- package/dist/indexeddb-db.d.ts.map +1 -1
- package/dist/indexeddb-db.js +5 -0
- package/dist/memory-db.d.ts.map +1 -1
- package/dist/memory-db.js +1 -0
- package/dist/studio-app/assets/{feltdb_wasm-C9xpYtna.js → feltdb_wasm-CD744e5D.js} +1 -1
- package/dist/studio-app/assets/feltdb_wasm_bg-CiIXhOLi.wasm +0 -0
- package/dist/studio-app/assets/index-DoROs8yx.js +28 -0
- package/dist/studio-app/index.html +1 -1
- package/dist/transaction.d.ts +127 -9
- package/dist/transaction.d.ts.map +1 -1
- package/dist/transaction.js +87 -3
- package/dist/wasm/feltdb_wasm_bg.wasm +0 -0
- package/package.json +1 -1
- package/dist/studio-app/assets/feltdb_wasm_bg-BsXHw7eX.wasm +0 -0
- package/dist/studio-app/assets/index-D4RZ44qs.js +0 -28
|
@@ -0,0 +1,899 @@
|
|
|
1
|
+
//! Transaction-level preconditions: version, epoch and lease.
|
|
2
|
+
//!
|
|
3
|
+
//! The primitive these tests hold to its contract lets a caller do the thing a
|
|
4
|
+
//! shared authority otherwise makes impossible:
|
|
5
|
+
//!
|
|
6
|
+
//! ```text
|
|
7
|
+
//! read state -> decide -> conditionally commit several records
|
|
8
|
+
//! ```
|
|
9
|
+
//!
|
|
10
|
+
//! with the condition evaluated by the authority inside the same atomic
|
|
11
|
+
//! boundary as the writes. Client-side checking cannot provide this: between a
|
|
12
|
+
//! client's check and its write, another writer commits.
|
|
13
|
+
//!
|
|
14
|
+
//! The invariant, which every test here is an attempt to break:
|
|
15
|
+
//!
|
|
16
|
+
//! ```text
|
|
17
|
+
//! all preconditions pass -> all writes commit
|
|
18
|
+
//! any precondition fails -> ZERO writes commit
|
|
19
|
+
//! ```
|
|
20
|
+
//!
|
|
21
|
+
//! There is no third outcome, and in particular no state in which the guarded
|
|
22
|
+
//! record is checked while the other staged records commit anyway.
|
|
23
|
+
//!
|
|
24
|
+
//! ## What "version" means here
|
|
25
|
+
//!
|
|
26
|
+
//! The document's `__version` -- the field a caller reads back and passes to
|
|
27
|
+
//! `updateIfVersion` -- not the row's internal storage sequence. Those are two
|
|
28
|
+
//! different numbers that happen to share a name, and fencing on the wrong one
|
|
29
|
+
//! would compare a caller's value against something the caller never sees.
|
|
30
|
+
//!
|
|
31
|
+
//! `__version` is owned by the writer: the client sets it to 1 on insert and
|
|
32
|
+
//! single-record CAS increments it. A transaction `set` writes the value it is
|
|
33
|
+
//! given, so a caller that wants successive fenced transactions to exclude each
|
|
34
|
+
//! other must advance `__version` in the value it writes. That is asserted
|
|
35
|
+
//! below rather than assumed, because a fence that silently stops fencing is
|
|
36
|
+
//! worse than no fence.
|
|
37
|
+
|
|
38
|
+
#[cfg(test)]
|
|
39
|
+
mod tests {
|
|
40
|
+
use crate::{
|
|
41
|
+
AtomicMutation, FeltDb, FlowError, PreconditionFailure, RecordPrecondition,
|
|
42
|
+
};
|
|
43
|
+
use serde_json::json;
|
|
44
|
+
use tempfile::TempDir;
|
|
45
|
+
|
|
46
|
+
fn db(dir: &TempDir) -> FeltDb {
|
|
47
|
+
FeltDb::open(dir.path().join("app.log")).expect("open")
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/// Storage keys are namespaced `collection:id`; the capability is the
|
|
51
|
+
/// collection. Every helper below takes the bare id and namespaces it, so
|
|
52
|
+
/// the tests read the way a caller thinks.
|
|
53
|
+
fn key(collection: &str, id: &str) -> String {
|
|
54
|
+
format!("{collection}:{id}")
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
fn set(collection: &str, id: &str, value: serde_json::Value) -> AtomicMutation {
|
|
58
|
+
AtomicMutation {
|
|
59
|
+
capability: collection.to_string(),
|
|
60
|
+
key: key(collection, id),
|
|
61
|
+
value: Some(value),
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
fn delete(collection: &str, id: &str) -> AtomicMutation {
|
|
66
|
+
AtomicMutation { capability: collection.to_string(), key: key(collection, id), value: None }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
fn precondition(collection: &str, id: &str) -> RecordPrecondition {
|
|
70
|
+
RecordPrecondition {
|
|
71
|
+
capability: collection.to_string(),
|
|
72
|
+
key: key(collection, id),
|
|
73
|
+
..Default::default()
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
fn expect_version(collection: &str, id: &str, version: u64) -> RecordPrecondition {
|
|
78
|
+
RecordPrecondition { expected_version: Some(version), ..precondition(collection, id) }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
fn present(db: &FeltDb, collection: &str, id: &str) -> bool {
|
|
82
|
+
db.get_value(&key(collection, id)).expect("read").is_some()
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
fn field(db: &FeltDb, collection: &str, id: &str, name: &str) -> serde_json::Value {
|
|
86
|
+
db.get_value(&key(collection, id))
|
|
87
|
+
.expect("read")
|
|
88
|
+
.and_then(|value| value.get(name).cloned())
|
|
89
|
+
.unwrap_or(serde_json::Value::Null)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
fn version_of(db: &FeltDb, collection: &str, id: &str) -> u64 {
|
|
93
|
+
field(db, collection, id, "__version").as_u64().unwrap_or(0)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/// Seed a record at a chosen version, as an application would hold it.
|
|
97
|
+
fn seed(db: &FeltDb, collection: &str, id: &str, version: u64, extra: serde_json::Value) {
|
|
98
|
+
let mut value = json!({ "id": id, "__version": version });
|
|
99
|
+
if let Some(fields) = extra.as_object() {
|
|
100
|
+
for (name, field) in fields {
|
|
101
|
+
value.as_object_mut().unwrap().insert(name.clone(), field.clone());
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
db.apply_atomic_transaction(
|
|
105
|
+
&format!("seed-{collection}-{id}-{version}"),
|
|
106
|
+
None,
|
|
107
|
+
&[],
|
|
108
|
+
&[set(collection, id, value)],
|
|
109
|
+
None,
|
|
110
|
+
)
|
|
111
|
+
.expect("seed commits");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
fn guarded(
|
|
115
|
+
db: &FeltDb,
|
|
116
|
+
id: &str,
|
|
117
|
+
preconditions: &[RecordPrecondition],
|
|
118
|
+
mutations: &[AtomicMutation],
|
|
119
|
+
) -> Result<crate::AtomicCommit, FlowError> {
|
|
120
|
+
db.apply_atomic_transaction_guarded(id, None, None, &[], preconditions, mutations, None)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
fn failure(error: FlowError) -> PreconditionFailure {
|
|
124
|
+
match error {
|
|
125
|
+
FlowError::PreconditionFailed(failure) => *failure,
|
|
126
|
+
other => panic!("expected a structured precondition failure, got {other:?}"),
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// -- the acceptance invariant -----------------------------------------
|
|
131
|
+
|
|
132
|
+
#[test]
|
|
133
|
+
fn a_satisfied_precondition_commits_every_staged_write() {
|
|
134
|
+
let dir = TempDir::new().unwrap();
|
|
135
|
+
let db = db(&dir);
|
|
136
|
+
seed(&db, "sessions", "s1", 7, json!({}));
|
|
137
|
+
seed(&db, "outbox", "o1", 1, json!({}));
|
|
138
|
+
|
|
139
|
+
let commit = guarded(
|
|
140
|
+
&db,
|
|
141
|
+
"tx-ok",
|
|
142
|
+
&[expect_version("sessions", "s1", 7)],
|
|
143
|
+
&[
|
|
144
|
+
set("sessions", "s1", json!({ "id": "s1", "__version": 8, "state": "active" })),
|
|
145
|
+
set("changes", "c1", json!({ "id": "c1" })),
|
|
146
|
+
delete("outbox", "o1"),
|
|
147
|
+
],
|
|
148
|
+
)
|
|
149
|
+
.expect("the precondition holds, so the transaction commits");
|
|
150
|
+
|
|
151
|
+
assert_eq!(commit.rows.len(), 3);
|
|
152
|
+
assert_eq!(version_of(&db, "sessions", "s1"), 8);
|
|
153
|
+
assert!(present(&db, "changes", "c1"));
|
|
154
|
+
assert!(!present(&db, "outbox", "o1"), "the staged delete applied too");
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
#[test]
|
|
158
|
+
fn a_stale_version_commits_nothing_at_all() {
|
|
159
|
+
// The failure the whole primitive exists to prevent: A is checked while
|
|
160
|
+
// B and C commit anyway.
|
|
161
|
+
let dir = TempDir::new().unwrap();
|
|
162
|
+
let db = db(&dir);
|
|
163
|
+
seed(&db, "records", "a", 7, json!({}));
|
|
164
|
+
seed(&db, "records", "b", 1, json!({ "state": "original" }));
|
|
165
|
+
|
|
166
|
+
// A moves to 8 before the transaction is evaluated.
|
|
167
|
+
seed(&db, "records", "a", 8, json!({}));
|
|
168
|
+
assert_eq!(version_of(&db, "records", "a"), 8);
|
|
169
|
+
|
|
170
|
+
let error = guarded(
|
|
171
|
+
&db,
|
|
172
|
+
"tx-stale",
|
|
173
|
+
&[expect_version("records", "a", 7)],
|
|
174
|
+
&[
|
|
175
|
+
set("records", "a", json!({ "id": "a", "__version": 8, "written": true })),
|
|
176
|
+
set("records", "b", json!({ "id": "b", "state": "overwritten" })),
|
|
177
|
+
set("records", "c", json!({ "id": "c" })),
|
|
178
|
+
],
|
|
179
|
+
)
|
|
180
|
+
.expect_err("a stale version must reject");
|
|
181
|
+
|
|
182
|
+
assert_eq!(
|
|
183
|
+
failure(error),
|
|
184
|
+
PreconditionFailure::Version {
|
|
185
|
+
collection: "records".into(),
|
|
186
|
+
key: "records:a".into(),
|
|
187
|
+
expected: 7,
|
|
188
|
+
actual: 8,
|
|
189
|
+
},
|
|
190
|
+
"and it names the predicate that failed, so a caller can tell a lost race \
|
|
191
|
+
from a broken deployment"
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
assert_eq!(version_of(&db, "records", "a"), 8, "the guarded record is untouched");
|
|
195
|
+
assert_eq!(
|
|
196
|
+
field(&db, "records", "b", "state"),
|
|
197
|
+
"original",
|
|
198
|
+
"and so is every other staged record"
|
|
199
|
+
);
|
|
200
|
+
assert!(!present(&db, "records", "c"), "a staged write to a new record did not appear");
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
#[test]
|
|
204
|
+
fn multi_record_ownership_fencing() {
|
|
205
|
+
// The Session Kernel's shape. Owner 1 read generation 12; owner 2 took
|
|
206
|
+
// 13 before owner 1 committed. Owner 1 must write nothing -- not the
|
|
207
|
+
// session, not the change record, not the outbox row.
|
|
208
|
+
let dir = TempDir::new().unwrap();
|
|
209
|
+
let db = db(&dir);
|
|
210
|
+
seed(&db, "sessions", "session", 12, json!({ "generation": 12 }));
|
|
211
|
+
|
|
212
|
+
seed(&db, "sessions", "session", 13, json!({ "generation": 13 }));
|
|
213
|
+
|
|
214
|
+
let error = guarded(
|
|
215
|
+
&db,
|
|
216
|
+
"owner-1-commit",
|
|
217
|
+
&[expect_version("sessions", "session", 12)],
|
|
218
|
+
&[
|
|
219
|
+
set("sessions", "session", json!({ "id": "session", "__version": 13, "generation": 12 })),
|
|
220
|
+
set("changes", "change-1", json!({ "id": "change-1" })),
|
|
221
|
+
set("outbox", "outbox-1", json!({ "id": "outbox-1" })),
|
|
222
|
+
],
|
|
223
|
+
)
|
|
224
|
+
.expect_err("the losing owner must be refused");
|
|
225
|
+
|
|
226
|
+
assert!(matches!(failure(error), PreconditionFailure::Version { expected: 12, actual: 13, .. }));
|
|
227
|
+
assert_eq!(
|
|
228
|
+
field(&db, "sessions", "session", "generation"),
|
|
229
|
+
13,
|
|
230
|
+
"the winning owner's generation stands"
|
|
231
|
+
);
|
|
232
|
+
assert!(!present(&db, "changes", "change-1"), "no change record");
|
|
233
|
+
assert!(!present(&db, "outbox", "outbox-1"), "no outbox record");
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// -- epoch and lease ---------------------------------------------------
|
|
237
|
+
|
|
238
|
+
#[test]
|
|
239
|
+
fn a_matching_epoch_commits_and_a_stale_one_rejects() {
|
|
240
|
+
let dir = TempDir::new().unwrap();
|
|
241
|
+
let db = db(&dir);
|
|
242
|
+
seed(&db, "cells", "c", 1, json!({ "authority": { "epoch": 4 } }));
|
|
243
|
+
|
|
244
|
+
guarded(
|
|
245
|
+
&db,
|
|
246
|
+
"tx-epoch-ok",
|
|
247
|
+
&[RecordPrecondition { expected_epoch: Some(4), ..precondition("cells", "c") }],
|
|
248
|
+
&[set("cells", "written", json!({ "id": "written" }))],
|
|
249
|
+
)
|
|
250
|
+
.expect("a matching epoch commits");
|
|
251
|
+
assert!(present(&db, "cells", "written"));
|
|
252
|
+
|
|
253
|
+
let error = guarded(
|
|
254
|
+
&db,
|
|
255
|
+
"tx-epoch-stale",
|
|
256
|
+
&[RecordPrecondition { expected_epoch: Some(3), ..precondition("cells", "c") }],
|
|
257
|
+
&[set("cells", "never", json!({ "id": "never" }))],
|
|
258
|
+
)
|
|
259
|
+
.expect_err("a stale epoch rejects");
|
|
260
|
+
|
|
261
|
+
assert_eq!(
|
|
262
|
+
failure(error),
|
|
263
|
+
PreconditionFailure::Epoch {
|
|
264
|
+
collection: "cells".into(),
|
|
265
|
+
key: "cells:c".into(),
|
|
266
|
+
expected: 3,
|
|
267
|
+
actual: 4,
|
|
268
|
+
}
|
|
269
|
+
);
|
|
270
|
+
assert!(!present(&db, "cells", "never"), "and writes nothing");
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
#[test]
|
|
274
|
+
fn a_held_lease_commits_and_a_stale_or_expired_one_rejects() {
|
|
275
|
+
let dir = TempDir::new().unwrap();
|
|
276
|
+
let db = db(&dir);
|
|
277
|
+
let future = crate::now_ms() as u64 + 600_000;
|
|
278
|
+
seed(&db, "cells", "c", 1, json!({ "lease": { "leaseId": "lease-a", "expiresAt": future } }));
|
|
279
|
+
|
|
280
|
+
let hold = |lease: &str| RecordPrecondition {
|
|
281
|
+
expected_lease_id: Some(lease.to_string()),
|
|
282
|
+
..precondition("cells", "c")
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
guarded(&db, "tx-lease-ok", &[hold("lease-a")], &[set("cells", "w1", json!({ "id": "w1" }))])
|
|
286
|
+
.expect("the held lease commits");
|
|
287
|
+
assert!(present(&db, "cells", "w1"));
|
|
288
|
+
|
|
289
|
+
let error = guarded(
|
|
290
|
+
&db,
|
|
291
|
+
"tx-lease-stale",
|
|
292
|
+
&[hold("lease-b")],
|
|
293
|
+
&[set("cells", "w2", json!({ "id": "w2" }))],
|
|
294
|
+
)
|
|
295
|
+
.expect_err("a different lease rejects");
|
|
296
|
+
assert_eq!(
|
|
297
|
+
failure(error),
|
|
298
|
+
PreconditionFailure::Lease {
|
|
299
|
+
collection: "cells".into(),
|
|
300
|
+
key: "cells:c".into(),
|
|
301
|
+
expected: "lease-b".into(),
|
|
302
|
+
actual: Some("lease-a".into()),
|
|
303
|
+
}
|
|
304
|
+
);
|
|
305
|
+
assert!(!present(&db, "cells", "w2"));
|
|
306
|
+
|
|
307
|
+
// An expired lease is not held, so naming it is a conflict: the caller
|
|
308
|
+
// believes it owns something it no longer does.
|
|
309
|
+
seed(&db, "cells", "expired", 1, json!({ "lease": { "leaseId": "lease-a", "expiresAt": 1u64 } }));
|
|
310
|
+
let error = guarded(
|
|
311
|
+
&db,
|
|
312
|
+
"tx-lease-expired",
|
|
313
|
+
&[RecordPrecondition {
|
|
314
|
+
expected_lease_id: Some("lease-a".into()),
|
|
315
|
+
..precondition("cells", "expired")
|
|
316
|
+
}],
|
|
317
|
+
&[set("cells", "w3", json!({ "id": "w3" }))],
|
|
318
|
+
)
|
|
319
|
+
.expect_err("an expired lease is not held");
|
|
320
|
+
assert!(matches!(failure(error), PreconditionFailure::Lease { .. }));
|
|
321
|
+
assert!(!present(&db, "cells", "w3"));
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
#[test]
|
|
325
|
+
fn a_transaction_rejects_if_any_predicate_of_several_fails() {
|
|
326
|
+
let dir = TempDir::new().unwrap();
|
|
327
|
+
let db = db(&dir);
|
|
328
|
+
seed(&db, "cells", "c", 5, json!({ "authority": { "epoch": 2 } }));
|
|
329
|
+
|
|
330
|
+
// Version right, epoch wrong.
|
|
331
|
+
let error = guarded(
|
|
332
|
+
&db,
|
|
333
|
+
"tx-mixed",
|
|
334
|
+
&[RecordPrecondition {
|
|
335
|
+
expected_version: Some(5),
|
|
336
|
+
expected_epoch: Some(9),
|
|
337
|
+
..precondition("cells", "c")
|
|
338
|
+
}],
|
|
339
|
+
&[set("cells", "w", json!({ "id": "w" }))],
|
|
340
|
+
)
|
|
341
|
+
.expect_err("one failing predicate refuses the whole transaction");
|
|
342
|
+
assert!(matches!(failure(error), PreconditionFailure::Epoch { expected: 9, actual: 2, .. }));
|
|
343
|
+
assert!(!present(&db, "cells", "w"));
|
|
344
|
+
|
|
345
|
+
// Several records, one of them stale.
|
|
346
|
+
seed(&db, "cells", "d", 1, json!({}));
|
|
347
|
+
let error = guarded(
|
|
348
|
+
&db,
|
|
349
|
+
"tx-mixed-2",
|
|
350
|
+
&[expect_version("cells", "c", 5), expect_version("cells", "d", 99)],
|
|
351
|
+
&[set("cells", "w", json!({ "id": "w" }))],
|
|
352
|
+
)
|
|
353
|
+
.expect_err("a failing predicate on any record refuses the transaction");
|
|
354
|
+
assert!(matches!(failure(error), PreconditionFailure::Version { expected: 99, .. }));
|
|
355
|
+
assert!(!present(&db, "cells", "w"));
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// -- shapes that must not be mistaken for protection -------------------
|
|
359
|
+
|
|
360
|
+
#[test]
|
|
361
|
+
fn a_precondition_on_a_missing_record_is_a_conflict_not_a_pass() {
|
|
362
|
+
let dir = TempDir::new().unwrap();
|
|
363
|
+
let db = db(&dir);
|
|
364
|
+
let error = guarded(
|
|
365
|
+
&db,
|
|
366
|
+
"tx-missing",
|
|
367
|
+
&[expect_version("cells", "absent", 1)],
|
|
368
|
+
&[set("cells", "w", json!({ "id": "w" }))],
|
|
369
|
+
)
|
|
370
|
+
.expect_err("expecting a version of a record that does not exist is a conflict");
|
|
371
|
+
assert_eq!(
|
|
372
|
+
failure(error),
|
|
373
|
+
PreconditionFailure::Missing { collection: "cells".into(), key: "cells:absent".into() }
|
|
374
|
+
);
|
|
375
|
+
assert!(!present(&db, "cells", "w"));
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
#[test]
|
|
379
|
+
fn an_empty_precondition_is_refused_rather_than_satisfied() {
|
|
380
|
+
// A precondition that constrains nothing would read as protection and
|
|
381
|
+
// provide none, so it is a caller error rather than a pass.
|
|
382
|
+
let dir = TempDir::new().unwrap();
|
|
383
|
+
let db = db(&dir);
|
|
384
|
+
let error = guarded(
|
|
385
|
+
&db,
|
|
386
|
+
"tx-empty",
|
|
387
|
+
&[precondition("cells", "c")],
|
|
388
|
+
&[set("cells", "w", json!({ "id": "w" }))],
|
|
389
|
+
)
|
|
390
|
+
.expect_err("an empty precondition is refused");
|
|
391
|
+
assert!(format!("{error}").contains("EMPTY_PRECONDITION"));
|
|
392
|
+
assert!(!present(&db, "cells", "w"));
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
#[test]
|
|
396
|
+
fn require_absent_still_means_absent() {
|
|
397
|
+
let dir = TempDir::new().unwrap();
|
|
398
|
+
let db = db(&dir);
|
|
399
|
+
let absent = |id: &str| RecordPrecondition { require_absent: true, ..precondition("cells", id) };
|
|
400
|
+
|
|
401
|
+
guarded(&db, "tx-new", &[absent("fresh")], &[set("cells", "fresh", json!({ "id": "fresh" }))])
|
|
402
|
+
.expect("absent record commits");
|
|
403
|
+
assert!(present(&db, "cells", "fresh"));
|
|
404
|
+
|
|
405
|
+
let error = guarded(
|
|
406
|
+
&db,
|
|
407
|
+
"tx-again",
|
|
408
|
+
&[absent("fresh")],
|
|
409
|
+
&[set("cells", "other", json!({ "id": "other" }))],
|
|
410
|
+
)
|
|
411
|
+
.expect_err("an existing record refuses a require-absent transaction");
|
|
412
|
+
assert_eq!(
|
|
413
|
+
failure(error),
|
|
414
|
+
PreconditionFailure::Present { collection: "cells".into(), key: "cells:fresh".into() }
|
|
415
|
+
);
|
|
416
|
+
assert!(!present(&db, "cells", "other"));
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// -- replay ------------------------------------------------------------
|
|
420
|
+
|
|
421
|
+
#[test]
|
|
422
|
+
fn a_committed_guarded_transaction_replays_as_a_duplicate() {
|
|
423
|
+
let dir = TempDir::new().unwrap();
|
|
424
|
+
let db = db(&dir);
|
|
425
|
+
seed(&db, "cells", "c", 3, json!({}));
|
|
426
|
+
|
|
427
|
+
let first = guarded(
|
|
428
|
+
&db,
|
|
429
|
+
"tx-replay",
|
|
430
|
+
&[expect_version("cells", "c", 3)],
|
|
431
|
+
&[set("cells", "w", json!({ "id": "w", "n": 1 }))],
|
|
432
|
+
)
|
|
433
|
+
.expect("commits");
|
|
434
|
+
assert!(!first.duplicate);
|
|
435
|
+
|
|
436
|
+
// The response was lost; the caller retries the same id. The
|
|
437
|
+
// precondition is no longer satisfiable -- but the transaction already
|
|
438
|
+
// committed, so replay returns the original outcome rather than
|
|
439
|
+
// re-evaluating it.
|
|
440
|
+
seed(&db, "cells", "c", 4, json!({}));
|
|
441
|
+
let retry = guarded(
|
|
442
|
+
&db,
|
|
443
|
+
"tx-replay",
|
|
444
|
+
&[expect_version("cells", "c", 3)],
|
|
445
|
+
&[set("cells", "w", json!({ "id": "w", "n": 2 }))],
|
|
446
|
+
)
|
|
447
|
+
.expect("a committed id replays rather than failing");
|
|
448
|
+
assert!(retry.duplicate, "the retry is recognised as the same transaction");
|
|
449
|
+
assert_eq!(
|
|
450
|
+
field(&db, "cells", "w", "n"),
|
|
451
|
+
1,
|
|
452
|
+
"and nothing is written twice"
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
#[test]
|
|
457
|
+
fn a_failed_precondition_does_not_consume_the_transaction_id() {
|
|
458
|
+
// Stated explicitly rather than left ambiguous: a refused transaction
|
|
459
|
+
// never committed, so its id is unused. A caller may retry the same id
|
|
460
|
+
// after re-reading state, or use a new one for a new decision. Both
|
|
461
|
+
// work, and neither is poisoned by the earlier refusal.
|
|
462
|
+
let dir = TempDir::new().unwrap();
|
|
463
|
+
let db = db(&dir);
|
|
464
|
+
seed(&db, "cells", "c", 1, json!({}));
|
|
465
|
+
|
|
466
|
+
guarded(
|
|
467
|
+
&db,
|
|
468
|
+
"tx-decision",
|
|
469
|
+
&[expect_version("cells", "c", 99)],
|
|
470
|
+
&[set("cells", "w", json!({ "id": "w" }))],
|
|
471
|
+
)
|
|
472
|
+
.expect_err("refused");
|
|
473
|
+
assert!(!present(&db, "cells", "w"));
|
|
474
|
+
|
|
475
|
+
// Same id, corrected expectation.
|
|
476
|
+
let commit = guarded(
|
|
477
|
+
&db,
|
|
478
|
+
"tx-decision",
|
|
479
|
+
&[expect_version("cells", "c", 1)],
|
|
480
|
+
&[set("cells", "w", json!({ "id": "w" }))],
|
|
481
|
+
)
|
|
482
|
+
.expect("the same id is still usable after a refusal");
|
|
483
|
+
assert!(!commit.duplicate, "it is a first commit, not a replay");
|
|
484
|
+
assert!(present(&db, "cells", "w"));
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
#[test]
|
|
488
|
+
fn a_guarded_transaction_survives_restart() {
|
|
489
|
+
let dir = TempDir::new().unwrap();
|
|
490
|
+
let path = dir.path().join("app.log");
|
|
491
|
+
{
|
|
492
|
+
let db = FeltDb::open(&path).expect("open");
|
|
493
|
+
seed(&db, "cells", "c", 2, json!({}));
|
|
494
|
+
guarded(
|
|
495
|
+
&db,
|
|
496
|
+
"tx-durable",
|
|
497
|
+
&[expect_version("cells", "c", 2)],
|
|
498
|
+
&[set("cells", "w", json!({ "id": "w" }))],
|
|
499
|
+
)
|
|
500
|
+
.expect("commits");
|
|
501
|
+
}
|
|
502
|
+
let reopened = FeltDb::open(&path).expect("reopen");
|
|
503
|
+
assert!(present(&reopened, "cells", "w"), "a guarded commit is as durable as any other");
|
|
504
|
+
let retry = reopened
|
|
505
|
+
.apply_atomic_transaction_guarded(
|
|
506
|
+
"tx-durable",
|
|
507
|
+
None,
|
|
508
|
+
None,
|
|
509
|
+
&[],
|
|
510
|
+
&[expect_version("cells", "c", 2)],
|
|
511
|
+
&[set("cells", "w", json!({ "id": "w", "n": 2 }))],
|
|
512
|
+
None,
|
|
513
|
+
)
|
|
514
|
+
.expect("replay after restart");
|
|
515
|
+
assert!(retry.duplicate, "transaction identity survives the restart");
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// -- concurrency -------------------------------------------------------
|
|
519
|
+
|
|
520
|
+
#[test]
|
|
521
|
+
fn exactly_one_of_two_competing_transactions_commits() {
|
|
522
|
+
// Sequential tests cannot show that the check and the write share a
|
|
523
|
+
// boundary. Two threads race on the same expected version; the
|
|
524
|
+
// authority must admit exactly one, and the loser must write neither of
|
|
525
|
+
// its records.
|
|
526
|
+
use std::sync::Arc;
|
|
527
|
+
let dir = TempDir::new().unwrap();
|
|
528
|
+
let db = Arc::new(db(&dir));
|
|
529
|
+
seed(&db, "cells", "shared", 1, json!({}));
|
|
530
|
+
|
|
531
|
+
let barrier = Arc::new(std::sync::Barrier::new(2));
|
|
532
|
+
let mut handles = Vec::new();
|
|
533
|
+
for (id, second) in [("t1", "b"), ("t2", "c")] {
|
|
534
|
+
let db = Arc::clone(&db);
|
|
535
|
+
let barrier = Arc::clone(&barrier);
|
|
536
|
+
handles.push(std::thread::spawn(move || {
|
|
537
|
+
barrier.wait();
|
|
538
|
+
db.apply_atomic_transaction_guarded(
|
|
539
|
+
id,
|
|
540
|
+
None,
|
|
541
|
+
None,
|
|
542
|
+
&[],
|
|
543
|
+
&[RecordPrecondition {
|
|
544
|
+
expected_version: Some(1),
|
|
545
|
+
..RecordPrecondition {
|
|
546
|
+
capability: "cells".into(),
|
|
547
|
+
key: "cells:shared".into(),
|
|
548
|
+
..Default::default()
|
|
549
|
+
}
|
|
550
|
+
}],
|
|
551
|
+
&[
|
|
552
|
+
set("cells", "shared", json!({ "id": "shared", "__version": 2, "by": id })),
|
|
553
|
+
set("cells", second, json!({ "id": second })),
|
|
554
|
+
],
|
|
555
|
+
None,
|
|
556
|
+
)
|
|
557
|
+
.map(|_| id)
|
|
558
|
+
}));
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
let outcomes: Vec<_> = handles.into_iter().map(|handle| handle.join().unwrap()).collect();
|
|
562
|
+
let winners: Vec<_> = outcomes.iter().filter_map(|outcome| outcome.as_ref().ok()).collect();
|
|
563
|
+
let losers: Vec<_> = outcomes.iter().filter_map(|outcome| outcome.as_ref().err()).collect();
|
|
564
|
+
|
|
565
|
+
assert_eq!(winners.len(), 1, "exactly one transaction may commit");
|
|
566
|
+
assert_eq!(losers.len(), 1);
|
|
567
|
+
assert!(
|
|
568
|
+
matches!(losers[0], FlowError::PreconditionFailed(_)),
|
|
569
|
+
"the loser receives a structured conflict, not an infrastructure error: {:?}",
|
|
570
|
+
losers[0]
|
|
571
|
+
);
|
|
572
|
+
|
|
573
|
+
let winner = *winners[0];
|
|
574
|
+
assert_eq!(version_of(&db, "cells", "shared"), 2);
|
|
575
|
+
assert_eq!(field(&db, "cells", "shared", "by"), winner);
|
|
576
|
+
|
|
577
|
+
let loser_record = if winner == "t1" { "c" } else { "b" };
|
|
578
|
+
let winner_record = if winner == "t1" { "b" } else { "c" };
|
|
579
|
+
assert!(present(&db, "cells", winner_record), "the winner's second write committed");
|
|
580
|
+
assert!(!present(&db, "cells", loser_record), "the loser wrote neither of its records");
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
// -- the boundary that used to exist ----------------------------------
|
|
584
|
+
|
|
585
|
+
#[test]
|
|
586
|
+
fn a_fenced_write_is_fenced_against_its_own_successor() {
|
|
587
|
+
// This test previously asserted the opposite, and recorded it as a
|
|
588
|
+
// known limitation: because a transaction write stored the caller's
|
|
589
|
+
// value verbatim, two transactions fencing on the same version both
|
|
590
|
+
// committed, and `ifVersion` was a check that permitted its own
|
|
591
|
+
// successor rather than a fence.
|
|
592
|
+
//
|
|
593
|
+
// The authority now advances the version of a fenced write, so the
|
|
594
|
+
// second is refused. The test is kept and inverted rather than deleted,
|
|
595
|
+
// because the gap is the reason the contract exists.
|
|
596
|
+
let dir = TempDir::new().unwrap();
|
|
597
|
+
let db = db(&dir);
|
|
598
|
+
seed(&db, "cells", "c", 1, json!({}));
|
|
599
|
+
|
|
600
|
+
guarded(
|
|
601
|
+
&db,
|
|
602
|
+
"tx-a",
|
|
603
|
+
&[expect_version("cells", "c", 1)],
|
|
604
|
+
&[set("cells", "c", json!({ "id": "c", "written": "a" }))],
|
|
605
|
+
)
|
|
606
|
+
.expect("first commits");
|
|
607
|
+
assert_eq!(version_of(&db, "cells", "c"), 2, "and advances the version");
|
|
608
|
+
|
|
609
|
+
let second = guarded(
|
|
610
|
+
&db,
|
|
611
|
+
"tx-b",
|
|
612
|
+
&[expect_version("cells", "c", 1)],
|
|
613
|
+
&[set("cells", "c", json!({ "id": "c", "written": "b" }))],
|
|
614
|
+
);
|
|
615
|
+
assert!(
|
|
616
|
+
second.is_err(),
|
|
617
|
+
"a caller no longer has to advance __version by hand to be fenced against itself"
|
|
618
|
+
);
|
|
619
|
+
assert_eq!(field(&db, "cells", "c", "written"), "a");
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// -- version advancement ----------------------------------------------
|
|
623
|
+
//
|
|
624
|
+
// docs/architecture/transaction-version-contract.md: a write fenced by
|
|
625
|
+
// expectedVersion is a conditional replacement, and the authority advances
|
|
626
|
+
// the version so `ifVersion` is a fence rather than a check that permits
|
|
627
|
+
// its own successor.
|
|
628
|
+
|
|
629
|
+
#[test]
|
|
630
|
+
fn a_version_fenced_write_advances_the_version() {
|
|
631
|
+
let dir = TempDir::new().unwrap();
|
|
632
|
+
let db = db(&dir);
|
|
633
|
+
seed(&db, "cells", "c", 1, json!({}));
|
|
634
|
+
|
|
635
|
+
guarded(
|
|
636
|
+
&db,
|
|
637
|
+
"tx-advance",
|
|
638
|
+
&[expect_version("cells", "c", 1)],
|
|
639
|
+
&[set("cells", "c", json!({ "id": "c", "state": "written" }))],
|
|
640
|
+
)
|
|
641
|
+
.expect("commits");
|
|
642
|
+
|
|
643
|
+
assert_eq!(version_of(&db, "cells", "c"), 2, "validated at 1, written at 2");
|
|
644
|
+
assert_eq!(field(&db, "cells", "c", "state"), "written");
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
#[test]
|
|
648
|
+
fn the_authority_owns_the_transition_and_replaces_a_supplied_version() {
|
|
649
|
+
// A caller that computes a different next version must not be able to
|
|
650
|
+
// install it, exactly as updateIfVersion strips __version from updates.
|
|
651
|
+
let dir = TempDir::new().unwrap();
|
|
652
|
+
let db = db(&dir);
|
|
653
|
+
seed(&db, "cells", "c", 4, json!({}));
|
|
654
|
+
|
|
655
|
+
guarded(
|
|
656
|
+
&db,
|
|
657
|
+
"tx-owned",
|
|
658
|
+
&[expect_version("cells", "c", 4)],
|
|
659
|
+
&[set("cells", "c", json!({ "id": "c", "__version": 99 }))],
|
|
660
|
+
)
|
|
661
|
+
.expect("commits");
|
|
662
|
+
assert_eq!(version_of(&db, "cells", "c"), 5, "the authority's transition, not the caller's");
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
#[test]
|
|
666
|
+
fn a_second_transaction_on_the_same_version_now_fails() {
|
|
667
|
+
// The gap this closes: before advancement, both of these committed.
|
|
668
|
+
let dir = TempDir::new().unwrap();
|
|
669
|
+
let db = db(&dir);
|
|
670
|
+
seed(&db, "cells", "c", 7, json!({}));
|
|
671
|
+
|
|
672
|
+
guarded(
|
|
673
|
+
&db,
|
|
674
|
+
"tx-first",
|
|
675
|
+
&[expect_version("cells", "c", 7)],
|
|
676
|
+
&[set("cells", "c", json!({ "id": "c", "by": "first" }))],
|
|
677
|
+
)
|
|
678
|
+
.expect("the first commits");
|
|
679
|
+
|
|
680
|
+
let second = guarded(
|
|
681
|
+
&db,
|
|
682
|
+
"tx-second",
|
|
683
|
+
&[expect_version("cells", "c", 7)],
|
|
684
|
+
&[set("cells", "c", json!({ "id": "c", "by": "second" }))],
|
|
685
|
+
);
|
|
686
|
+
assert!(second.is_err(), "ifVersion is now a fence: the successor is refused");
|
|
687
|
+
assert!(matches!(
|
|
688
|
+
failure(second.unwrap_err()),
|
|
689
|
+
PreconditionFailure::Version { expected: 7, actual: 8, .. }
|
|
690
|
+
));
|
|
691
|
+
assert_eq!(field(&db, "cells", "c", "by"), "first");
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
#[test]
|
|
695
|
+
fn sequential_transactions_each_advancing_both_succeed() {
|
|
696
|
+
let dir = TempDir::new().unwrap();
|
|
697
|
+
let db = db(&dir);
|
|
698
|
+
seed(&db, "cells", "c", 1, json!({}));
|
|
699
|
+
|
|
700
|
+
guarded(&db, "s1", &[expect_version("cells", "c", 1)], &[set("cells", "c", json!({ "id": "c" }))])
|
|
701
|
+
.expect("first");
|
|
702
|
+
guarded(&db, "s2", &[expect_version("cells", "c", 2)], &[set("cells", "c", json!({ "id": "c" }))])
|
|
703
|
+
.expect("second, on the advanced version");
|
|
704
|
+
assert_eq!(version_of(&db, "cells", "c"), 3);
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
#[test]
|
|
708
|
+
fn each_versioned_record_in_one_transaction_advances_independently() {
|
|
709
|
+
let dir = TempDir::new().unwrap();
|
|
710
|
+
let db = db(&dir);
|
|
711
|
+
seed(&db, "cells", "a", 1, json!({}));
|
|
712
|
+
seed(&db, "cells", "b", 5, json!({}));
|
|
713
|
+
|
|
714
|
+
guarded(
|
|
715
|
+
&db,
|
|
716
|
+
"tx-multi",
|
|
717
|
+
&[expect_version("cells", "a", 1), expect_version("cells", "b", 5)],
|
|
718
|
+
&[
|
|
719
|
+
set("cells", "a", json!({ "id": "a" })),
|
|
720
|
+
set("cells", "b", json!({ "id": "b" })),
|
|
721
|
+
set("cells", "c", json!({ "id": "c" })),
|
|
722
|
+
],
|
|
723
|
+
)
|
|
724
|
+
.expect("commits");
|
|
725
|
+
|
|
726
|
+
assert_eq!(version_of(&db, "cells", "a"), 2);
|
|
727
|
+
assert_eq!(version_of(&db, "cells", "b"), 6);
|
|
728
|
+
assert_eq!(
|
|
729
|
+
version_of(&db, "cells", "c"),
|
|
730
|
+
0,
|
|
731
|
+
"an unfenced write in the same transaction is stored verbatim and gains no version"
|
|
732
|
+
);
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
#[test]
|
|
736
|
+
fn a_guard_only_record_is_not_advanced() {
|
|
737
|
+
let dir = TempDir::new().unwrap();
|
|
738
|
+
let db = db(&dir);
|
|
739
|
+
seed(&db, "sessions", "s", 3, json!({}));
|
|
740
|
+
|
|
741
|
+
guarded(
|
|
742
|
+
&db,
|
|
743
|
+
"tx-guard-only",
|
|
744
|
+
&[expect_version("sessions", "s", 3)],
|
|
745
|
+
&[set("changes", "c1", json!({ "id": "c1" }))],
|
|
746
|
+
)
|
|
747
|
+
.expect("commits");
|
|
748
|
+
|
|
749
|
+
assert_eq!(
|
|
750
|
+
version_of(&db, "sessions", "s"),
|
|
751
|
+
3,
|
|
752
|
+
"a guard is read, not written: its version is untouched"
|
|
753
|
+
);
|
|
754
|
+
assert!(present(&db, "changes", "c1"));
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
#[test]
|
|
758
|
+
fn an_unconditional_write_is_stored_verbatim() {
|
|
759
|
+
// What preserves every existing transaction caller.
|
|
760
|
+
let dir = TempDir::new().unwrap();
|
|
761
|
+
let db = db(&dir);
|
|
762
|
+
seed(&db, "cells", "c", 4, json!({}));
|
|
763
|
+
|
|
764
|
+
db.apply_atomic_transaction(
|
|
765
|
+
"tx-plain",
|
|
766
|
+
None,
|
|
767
|
+
&[],
|
|
768
|
+
&[set("cells", "c", json!({ "id": "c", "__version": 4, "state": "same" }))],
|
|
769
|
+
None,
|
|
770
|
+
)
|
|
771
|
+
.expect("commits");
|
|
772
|
+
assert_eq!(version_of(&db, "cells", "c"), 4, "unchanged, because nothing fenced it");
|
|
773
|
+
|
|
774
|
+
db.apply_atomic_transaction(
|
|
775
|
+
"tx-plain-2",
|
|
776
|
+
None,
|
|
777
|
+
&[],
|
|
778
|
+
&[set("cells", "unversioned", json!({ "id": "unversioned" }))],
|
|
779
|
+
None,
|
|
780
|
+
)
|
|
781
|
+
.expect("commits");
|
|
782
|
+
assert_eq!(
|
|
783
|
+
version_of(&db, "cells", "unversioned"),
|
|
784
|
+
0,
|
|
785
|
+
"and the authority assigns no version to a record that has none"
|
|
786
|
+
);
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
#[test]
|
|
790
|
+
fn a_fenced_delete_validates_and_removes_without_a_version_transition() {
|
|
791
|
+
let dir = TempDir::new().unwrap();
|
|
792
|
+
let db = db(&dir);
|
|
793
|
+
seed(&db, "cells", "c", 2, json!({}));
|
|
794
|
+
|
|
795
|
+
let stale = guarded(
|
|
796
|
+
&db,
|
|
797
|
+
"tx-del-stale",
|
|
798
|
+
&[expect_version("cells", "c", 1)],
|
|
799
|
+
&[delete("cells", "c")],
|
|
800
|
+
);
|
|
801
|
+
assert!(stale.is_err(), "a stale fence refuses the delete");
|
|
802
|
+
assert!(present(&db, "cells", "c"));
|
|
803
|
+
|
|
804
|
+
guarded(&db, "tx-del", &[expect_version("cells", "c", 2)], &[delete("cells", "c")])
|
|
805
|
+
.expect("the current fence permits it");
|
|
806
|
+
assert!(!present(&db, "cells", "c"), "fence, then delete; there is no record left to version");
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
#[test]
|
|
810
|
+
fn a_transaction_insert_keeps_the_callers_value_and_the_authority_assigns_nothing() {
|
|
811
|
+
// Insert remains the client's: Collection.insert supplies __version 1,
|
|
812
|
+
// which is where record versioning begins.
|
|
813
|
+
let dir = TempDir::new().unwrap();
|
|
814
|
+
let db = db(&dir);
|
|
815
|
+
|
|
816
|
+
guarded(
|
|
817
|
+
&db,
|
|
818
|
+
"tx-insert",
|
|
819
|
+
&[RecordPrecondition { require_absent: true, ..precondition("cells", "new") }],
|
|
820
|
+
&[set("cells", "new", json!({ "id": "new", "__version": 1 }))],
|
|
821
|
+
)
|
|
822
|
+
.expect("commits");
|
|
823
|
+
assert_eq!(version_of(&db, "cells", "new"), 1, "the caller's value, stored as given");
|
|
824
|
+
|
|
825
|
+
let missing = guarded(
|
|
826
|
+
&db,
|
|
827
|
+
"tx-fence-absent",
|
|
828
|
+
&[expect_version("cells", "absent", 1)],
|
|
829
|
+
&[set("cells", "absent", json!({ "id": "absent" }))],
|
|
830
|
+
);
|
|
831
|
+
assert!(
|
|
832
|
+
matches!(failure(missing.unwrap_err()), PreconditionFailure::Missing { .. }),
|
|
833
|
+
"a version-fenced write against an absent record is a conflict, not an insert"
|
|
834
|
+
);
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
#[test]
|
|
838
|
+
fn replay_of_a_fenced_transaction_does_not_advance_twice() {
|
|
839
|
+
let dir = TempDir::new().unwrap();
|
|
840
|
+
let db = db(&dir);
|
|
841
|
+
seed(&db, "cells", "c", 1, json!({}));
|
|
842
|
+
|
|
843
|
+
guarded(&db, "tx-once", &[expect_version("cells", "c", 1)], &[set("cells", "c", json!({ "id": "c" }))])
|
|
844
|
+
.expect("commits");
|
|
845
|
+
assert_eq!(version_of(&db, "cells", "c"), 2);
|
|
846
|
+
|
|
847
|
+
let replay = guarded(
|
|
848
|
+
&db,
|
|
849
|
+
"tx-once",
|
|
850
|
+
&[expect_version("cells", "c", 1)],
|
|
851
|
+
&[set("cells", "c", json!({ "id": "c" }))],
|
|
852
|
+
)
|
|
853
|
+
.expect("a committed id replays");
|
|
854
|
+
assert!(replay.duplicate);
|
|
855
|
+
assert_eq!(version_of(&db, "cells", "c"), 2, "a replay cannot advance the version again");
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
#[test]
|
|
859
|
+
fn competing_fenced_transactions_leave_the_version_at_exactly_one_advance() {
|
|
860
|
+
use std::sync::Arc;
|
|
861
|
+
let dir = TempDir::new().unwrap();
|
|
862
|
+
let db = Arc::new(db(&dir));
|
|
863
|
+
seed(&db, "cells", "race", 1, json!({}));
|
|
864
|
+
|
|
865
|
+
let barrier = Arc::new(std::sync::Barrier::new(2));
|
|
866
|
+
let mut handles = Vec::new();
|
|
867
|
+
for id in ["r1", "r2"] {
|
|
868
|
+
let db = Arc::clone(&db);
|
|
869
|
+
let barrier = Arc::clone(&barrier);
|
|
870
|
+
handles.push(std::thread::spawn(move || {
|
|
871
|
+
barrier.wait();
|
|
872
|
+
db.apply_atomic_transaction_guarded(
|
|
873
|
+
id,
|
|
874
|
+
None,
|
|
875
|
+
None,
|
|
876
|
+
&[],
|
|
877
|
+
&[RecordPrecondition {
|
|
878
|
+
expected_version: Some(1),
|
|
879
|
+
..RecordPrecondition {
|
|
880
|
+
capability: "cells".into(),
|
|
881
|
+
key: "cells:race".into(),
|
|
882
|
+
..Default::default()
|
|
883
|
+
}
|
|
884
|
+
}],
|
|
885
|
+
&[set("cells", "race", json!({ "id": "race", "by": id }))],
|
|
886
|
+
None,
|
|
887
|
+
)
|
|
888
|
+
.map(|_| id)
|
|
889
|
+
}));
|
|
890
|
+
}
|
|
891
|
+
let outcomes: Vec<_> = handles.into_iter().map(|handle| handle.join().unwrap()).collect();
|
|
892
|
+
assert_eq!(outcomes.iter().filter(|outcome| outcome.is_ok()).count(), 1);
|
|
893
|
+
assert_eq!(
|
|
894
|
+
version_of(&db, "cells", "race"),
|
|
895
|
+
2,
|
|
896
|
+
"exactly one advance, whichever transaction won"
|
|
897
|
+
);
|
|
898
|
+
}
|
|
899
|
+
}
|