@feltdb/core 0.11.9 → 0.12.1
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/dist/agent-state.d.ts +489 -0
- package/dist/agent-state.d.ts.map +1 -0
- package/dist/agent-state.js +993 -0
- package/dist/build-identity.json +19 -0
- package/dist/cli/index.js +0 -0
- package/dist/create/build-identity.json +19 -0
- package/dist/create/package-versions.js +1 -1
- package/dist/create/server-source/crates/feltdb/src/lib.rs +11 -0
- package/dist/create/server-source/crates/feltdb/src/query_performance.rs +4 -1
- package/dist/create/server-source/crates/feltdb/src/state_model.rs +854 -155
- package/dist/create/server-source/crates/feltdb/tests/generalized_version_control_substrate_completion.rs +31 -13
- package/dist/create/server-source/crates/feltdb/tests/state_ref_authority.rs +918 -0
- package/dist/create/server-source/crates/feltdb/tests/workload_envelope_contract.rs +15 -2
- package/dist/create/server-source/crates/feltdb-server/Cargo.toml +4 -0
- package/dist/create/server-source/crates/feltdb-server/benches/query_shape_observer.rs +136 -0
- package/dist/create/server-source/crates/feltdb-server/src/app_state.rs +3 -0
- package/dist/create/server-source/crates/feltdb-server/src/lib.rs +1 -0
- package/dist/create/server-source/crates/feltdb-server/src/main.rs +74 -5
- package/dist/create/server-source/crates/feltdb-server/src/query_shape.rs +1137 -0
- package/dist/create/server-source/crates/feltdb-server/tests/fixtures/query-shape-trace.expected.json +300 -0
- package/dist/create/server-source/crates/feltdb-server/tests/fixtures/query-shape-trace.json +25 -0
- package/dist/create/server-source/crates/feltdb-server/tests/query_shape_trace.rs +128 -0
- package/dist/create/server-source/crates/feltdb-wasm/test.db +0 -0
- package/dist/index-core.d.ts +1 -0
- package/dist/index-core.d.ts.map +1 -1
- package/dist/index-core.js +1 -0
- package/dist/studio/app.d.ts.map +1 -1
- package/dist/studio/components/GlobalSearch.d.ts.map +1 -1
- package/dist/studio/components/index.js +1 -1
- package/dist/studio/{components-BHTKlUgT.js → components-Bivm2lX6.js} +2 -1
- package/dist/studio/control-plane/Agents.d.ts.map +1 -1
- package/dist/studio/control-plane/Data.d.ts.map +1 -1
- package/dist/studio/control-plane/Operations.d.ts.map +1 -1
- package/dist/studio/control-plane/Overview.d.ts.map +1 -1
- package/dist/studio/control-plane/Schema.d.ts.map +1 -1
- package/dist/studio/control-plane/State.d.ts +11 -0
- package/dist/studio/control-plane/State.d.ts.map +1 -1
- package/dist/studio/control-plane/index.d.ts +1 -1
- package/dist/studio/control-plane/index.d.ts.map +1 -1
- package/dist/studio/control-plane/index.js +2 -2
- package/dist/studio/control-plane/primitives.d.ts +14 -2
- package/dist/studio/control-plane/primitives.d.ts.map +1 -1
- package/dist/studio/control-plane-CLm69XwX.js +4121 -0
- package/dist/studio/index.js +266 -235
- package/dist/studio/studio.css +1 -1
- package/dist/studio-app/assets/{feltdb_wasm-DGGqm9GK.js → feltdb_wasm-DACcD-zs.js} +1 -1
- package/dist/studio-app/assets/feltdb_wasm_bg-BejThWID.wasm +0 -0
- package/dist/studio-app/assets/index-CiC8VY-a.css +1 -0
- package/dist/studio-app/assets/index-CoZaxnlm.js +30 -0
- package/dist/studio-app/index.html +2 -2
- package/dist/wasm/feltdb_wasm_bg.wasm +0 -0
- package/package.json +2 -2
- package/dist/studio/control-plane-DP8ljTDO.js +0 -3207
- package/dist/studio-app/assets/feltdb_wasm_bg-BYJo87WY.wasm +0 -0
- package/dist/studio-app/assets/index-BrsF-kWL.css +0 -1
- package/dist/studio-app/assets/index-qLeZcPW_.js +0 -30
|
@@ -0,0 +1,918 @@
|
|
|
1
|
+
//! State-model refs and garbage collection under concurrency, restart and
|
|
2
|
+
//! process death.
|
|
3
|
+
//!
|
|
4
|
+
//! These are regression tests for the defects measured by the research probe
|
|
5
|
+
//! (`docs/audits/enroute/probe/`) and decided in
|
|
6
|
+
//! `adrs/state-ref-authority-and-safe-reclamation.md`:
|
|
7
|
+
//!
|
|
8
|
+
//! - P1: concurrent ref CAS across `StateStore` instances admitted several
|
|
9
|
+
//! winners;
|
|
10
|
+
//! - P2: GC with no refs deleted every revision;
|
|
11
|
+
//! - P3: GC racing a promotion left a ref naming a deleted revision;
|
|
12
|
+
//! - P4: a ref move was a flush-only single-row write.
|
|
13
|
+
//!
|
|
14
|
+
//! Everything here uses the durable backing (`FeltDb`), because that is where
|
|
15
|
+
//! the defects were. Race tests repeat an interleaving many times. They detect
|
|
16
|
+
//! a broken implementation with high probability; they do not prove a correct
|
|
17
|
+
//! one. The argument for correctness is the transaction ordering documented in
|
|
18
|
+
//! `state_model.rs`, and these tests check that the code behaves as argued.
|
|
19
|
+
|
|
20
|
+
use feltdb::state_model::{RefError, StateRef, StateRevision, StateStore};
|
|
21
|
+
use feltdb::{FeltDBStateSystem, FeltDb, StateId};
|
|
22
|
+
use serde_json::{json, Value};
|
|
23
|
+
use std::collections::BTreeMap;
|
|
24
|
+
use std::io::{BufRead, BufReader};
|
|
25
|
+
use std::path::{Path, PathBuf};
|
|
26
|
+
use std::process::{Command, Stdio};
|
|
27
|
+
use std::sync::{Arc, Barrier};
|
|
28
|
+
use std::thread;
|
|
29
|
+
use tempfile::TempDir;
|
|
30
|
+
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
// helpers
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
|
|
35
|
+
fn open(path: &Path) -> Arc<FeltDb> {
|
|
36
|
+
Arc::new(FeltDb::open(path).expect("open"))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
fn store(db: &Arc<FeltDb>) -> StateStore {
|
|
40
|
+
FeltDBStateSystem::create_store(db).expect("store")
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
fn fresh() -> (TempDir, PathBuf, Arc<FeltDb>) {
|
|
44
|
+
let dir = TempDir::new().unwrap();
|
|
45
|
+
let path = dir.path().join("state.log");
|
|
46
|
+
let db = open(&path);
|
|
47
|
+
(dir, path, db)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
fn no_metadata() -> BTreeMap<String, Value> {
|
|
51
|
+
BTreeMap::new()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
fn log_lines(path: &Path) -> Vec<String> {
|
|
55
|
+
std::fs::read_to_string(path)
|
|
56
|
+
.unwrap_or_default()
|
|
57
|
+
.lines()
|
|
58
|
+
.map(str::to_string)
|
|
59
|
+
.collect()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/// A chain `base(0) → … → head(len-1)` on `resource`, committed normally.
|
|
63
|
+
fn chain(store: &StateStore, resource: &str, len: usize) -> Vec<StateRevision> {
|
|
64
|
+
let mut revisions = vec![store
|
|
65
|
+
.create(resource, json!({"n": 0}).to_string(), "app".into())
|
|
66
|
+
.unwrap()];
|
|
67
|
+
for n in 1..len {
|
|
68
|
+
let parent = revisions.last().unwrap().clone();
|
|
69
|
+
revisions.push(
|
|
70
|
+
store
|
|
71
|
+
.commit(json!({"n": n}).to_string(), &parent, "app".into())
|
|
72
|
+
.unwrap(),
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
revisions
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/// A child of `parent` written as a durable row *as an old revision*: its
|
|
79
|
+
/// timestamp is two hours in the past, so it is outside any reasonable GC grace
|
|
80
|
+
/// period. The identity does not cover the timestamp, so integrity still holds.
|
|
81
|
+
fn old_child(db: &FeltDb, parent: &StateRevision, content: Value) -> StateRevision {
|
|
82
|
+
let mut revision = StateRevision::child(content.to_string(), parent, "app".into());
|
|
83
|
+
revision.timestamp_ms = revision.timestamp_ms.saturating_sub(2 * 60 * 60 * 1000);
|
|
84
|
+
assert!(revision.verify_integrity());
|
|
85
|
+
db.insert(&format!("state:revision:{}", revision.id.as_hex()), &revision)
|
|
86
|
+
.unwrap();
|
|
87
|
+
revision
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/// A ref row exactly as the pre-transactional implementation wrote it.
|
|
91
|
+
fn legacy_ref(db: &FeltDb, namespace: &str, name: &str, target: &StateId, version: u64) {
|
|
92
|
+
let reference = StateRef {
|
|
93
|
+
namespace: namespace.into(),
|
|
94
|
+
name: name.into(),
|
|
95
|
+
target: target.clone(),
|
|
96
|
+
version,
|
|
97
|
+
authority: "legacy".into(),
|
|
98
|
+
updated_at_ms: 1,
|
|
99
|
+
metadata: BTreeMap::new(),
|
|
100
|
+
};
|
|
101
|
+
db.insert(&format!("state:ref:{namespace}:{name}"), &reference)
|
|
102
|
+
.unwrap();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
fn every_ref_target_exists(store: &StateStore) -> bool {
|
|
106
|
+
store
|
|
107
|
+
.list_refs(None)
|
|
108
|
+
.iter()
|
|
109
|
+
.all(|reference| store.exists(&reference.target))
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ---------------------------------------------------------------------------
|
|
113
|
+
// 1. Concurrent CAS has exactly one winner (P1)
|
|
114
|
+
// ---------------------------------------------------------------------------
|
|
115
|
+
|
|
116
|
+
const RACE_ITERATIONS: usize = 1_000;
|
|
117
|
+
const RACERS: usize = 8;
|
|
118
|
+
|
|
119
|
+
fn cas_race(stores: Vec<StateStore>) {
|
|
120
|
+
let setup = stores[0].clone();
|
|
121
|
+
let base = setup
|
|
122
|
+
.create("doc:race", json!({"v": 0}).to_string(), "app".into())
|
|
123
|
+
.unwrap();
|
|
124
|
+
let targets: Vec<StateId> = (0..RACERS)
|
|
125
|
+
.map(|i| {
|
|
126
|
+
setup
|
|
127
|
+
.commit(json!({"v": i + 1}).to_string(), &base, "app".into())
|
|
128
|
+
.unwrap()
|
|
129
|
+
.id
|
|
130
|
+
})
|
|
131
|
+
.collect();
|
|
132
|
+
|
|
133
|
+
for iteration in 0..RACE_ITERATIONS {
|
|
134
|
+
let name = format!("r{iteration}");
|
|
135
|
+
setup
|
|
136
|
+
.create_ref("race", &name, base.id.clone(), "app".into(), no_metadata())
|
|
137
|
+
.unwrap();
|
|
138
|
+
let barrier = Arc::new(Barrier::new(RACERS));
|
|
139
|
+
let handles: Vec<_> = (0..RACERS)
|
|
140
|
+
.map(|i| {
|
|
141
|
+
let store = stores[i % stores.len()].clone();
|
|
142
|
+
let barrier = barrier.clone();
|
|
143
|
+
let target = targets[i].clone();
|
|
144
|
+
let name = name.clone();
|
|
145
|
+
thread::spawn(move || {
|
|
146
|
+
barrier.wait();
|
|
147
|
+
store
|
|
148
|
+
.compare_and_swap_ref("race", &name, 1, target.clone(), format!("w{i}"), None)
|
|
149
|
+
.map(|_| target)
|
|
150
|
+
})
|
|
151
|
+
})
|
|
152
|
+
.collect();
|
|
153
|
+
let outcomes: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect();
|
|
154
|
+
let winners: Vec<&StateId> = outcomes.iter().filter_map(|o| o.as_ref().ok()).collect();
|
|
155
|
+
assert_eq!(
|
|
156
|
+
winners.len(),
|
|
157
|
+
1,
|
|
158
|
+
"iteration {iteration}: exactly one CAS from version 1 may succeed, got {}",
|
|
159
|
+
winners.len()
|
|
160
|
+
);
|
|
161
|
+
for outcome in &outcomes {
|
|
162
|
+
if let Err(error) = outcome {
|
|
163
|
+
assert!(
|
|
164
|
+
matches!(error, RefError::VersionConflict { expected: 1, current: 2 }),
|
|
165
|
+
"iteration {iteration}: a loser must see a version conflict, got {error:?}"
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
let after = setup.get_ref("race", &name).unwrap();
|
|
170
|
+
assert_eq!(after.version, 2, "iteration {iteration}: one advance");
|
|
171
|
+
assert_eq!(&after.target, winners[0], "iteration {iteration}: the winner's target");
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/// The server builds a `StateStore` per request; each has its own instance.
|
|
176
|
+
#[test]
|
|
177
|
+
fn ref_cas_has_one_winner_across_store_instances() {
|
|
178
|
+
let (_dir, _path, db) = fresh();
|
|
179
|
+
cas_race((0..RACERS).map(|_| store(&db)).collect());
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/// Two independently opened handles on one path, each with its own stores.
|
|
183
|
+
#[test]
|
|
184
|
+
fn ref_cas_has_one_winner_across_open_handles() {
|
|
185
|
+
let (_dir, path, one) = fresh();
|
|
186
|
+
let two = open(&path);
|
|
187
|
+
let stores = vec![store(&one), store(&two), store(&one), store(&two)];
|
|
188
|
+
cas_race(stores);
|
|
189
|
+
// Both handles observe the same authority, not two copies.
|
|
190
|
+
assert_eq!(
|
|
191
|
+
store(&one).get_ref("race", "r0"),
|
|
192
|
+
store(&two).get_ref("race", "r0")
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
#[test]
|
|
197
|
+
fn concurrent_ref_create_has_one_winner() {
|
|
198
|
+
let (_dir, _path, db) = fresh();
|
|
199
|
+
let setup = store(&db);
|
|
200
|
+
let targets: Vec<StateId> = (0..RACERS)
|
|
201
|
+
.map(|i| {
|
|
202
|
+
setup
|
|
203
|
+
.create(&format!("doc:c{i}"), json!({"i": i}).to_string(), "app".into())
|
|
204
|
+
.unwrap()
|
|
205
|
+
.id
|
|
206
|
+
})
|
|
207
|
+
.collect();
|
|
208
|
+
for iteration in 0..200 {
|
|
209
|
+
let name = format!("c{iteration}");
|
|
210
|
+
let barrier = Arc::new(Barrier::new(RACERS));
|
|
211
|
+
let handles: Vec<_> = (0..RACERS)
|
|
212
|
+
.map(|i| {
|
|
213
|
+
let store = store(&db);
|
|
214
|
+
let barrier = barrier.clone();
|
|
215
|
+
let target = targets[i].clone();
|
|
216
|
+
let name = name.clone();
|
|
217
|
+
thread::spawn(move || {
|
|
218
|
+
barrier.wait();
|
|
219
|
+
store.create_ref("create", &name, target, format!("w{i}"), BTreeMap::new())
|
|
220
|
+
})
|
|
221
|
+
})
|
|
222
|
+
.collect();
|
|
223
|
+
let outcomes: Vec<_> = handles.into_iter().map(|h| h.join().unwrap()).collect();
|
|
224
|
+
let created = outcomes.iter().filter(|o| o.is_ok()).count();
|
|
225
|
+
assert_eq!(created, 1, "iteration {iteration}: one creator wins");
|
|
226
|
+
assert!(outcomes
|
|
227
|
+
.iter()
|
|
228
|
+
.filter_map(|o| o.as_ref().err())
|
|
229
|
+
.all(|e| *e == RefError::AlreadyExists));
|
|
230
|
+
assert_eq!(setup.get_ref("create", &name).unwrap().version, 1);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// ---------------------------------------------------------------------------
|
|
235
|
+
// 2. Missing targets and stale versions never become accepted state
|
|
236
|
+
// ---------------------------------------------------------------------------
|
|
237
|
+
|
|
238
|
+
#[test]
|
|
239
|
+
fn missing_targets_and_stale_versions_write_nothing() {
|
|
240
|
+
let (_dir, path, db) = fresh();
|
|
241
|
+
let store = store(&db);
|
|
242
|
+
let revisions = chain(&store, "doc:v", 2);
|
|
243
|
+
store
|
|
244
|
+
.create_ref("heads", "main", revisions[0].id.clone(), "app".into(), no_metadata())
|
|
245
|
+
.unwrap();
|
|
246
|
+
let before = log_lines(&path).len();
|
|
247
|
+
let missing = StateId::compute("never committed");
|
|
248
|
+
|
|
249
|
+
assert_eq!(
|
|
250
|
+
store.create_ref("heads", "other", missing.clone(), "app".into(), no_metadata()),
|
|
251
|
+
Err(RefError::UnknownTarget(missing.clone()))
|
|
252
|
+
);
|
|
253
|
+
assert_eq!(
|
|
254
|
+
store
|
|
255
|
+
.compare_and_swap_ref("heads", "main", 1, missing.clone(), "app".into(), None)
|
|
256
|
+
.unwrap_err(),
|
|
257
|
+
RefError::UnknownTarget(missing.clone())
|
|
258
|
+
);
|
|
259
|
+
assert_eq!(
|
|
260
|
+
store
|
|
261
|
+
.compare_and_swap_ref("heads", "main", 7, revisions[1].id.clone(), "app".into(), None)
|
|
262
|
+
.unwrap_err(),
|
|
263
|
+
RefError::VersionConflict { expected: 7, current: 1 }
|
|
264
|
+
);
|
|
265
|
+
assert_eq!(
|
|
266
|
+
store.delete_ref("heads", "main", Some(7)),
|
|
267
|
+
Err(RefError::VersionConflict { expected: 7, current: 1 })
|
|
268
|
+
);
|
|
269
|
+
|
|
270
|
+
assert_eq!(log_lines(&path).len(), before, "a refused ref operation appends nothing");
|
|
271
|
+
let main = store.get_ref("heads", "main").unwrap();
|
|
272
|
+
assert_eq!((main.version, main.target), (1, revisions[0].id.clone()));
|
|
273
|
+
assert!(store.get_ref("heads", "other").is_none());
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// ---------------------------------------------------------------------------
|
|
277
|
+
// 3. GC policy: history is roots and their ancestors (Option A)
|
|
278
|
+
// ---------------------------------------------------------------------------
|
|
279
|
+
|
|
280
|
+
/// P2: with no refs at all, GC must not touch a resource's history.
|
|
281
|
+
#[test]
|
|
282
|
+
fn gc_without_refs_keeps_every_head_and_its_ancestors() {
|
|
283
|
+
let (_dir, _path, db) = fresh();
|
|
284
|
+
let store = store(&db);
|
|
285
|
+
let revisions = chain(&store, "orders:1", 10);
|
|
286
|
+
let report = store.collect_unreachable().unwrap();
|
|
287
|
+
assert!(report.collected_revisions.is_empty(), "{report:?}");
|
|
288
|
+
for revision in &revisions {
|
|
289
|
+
assert!(store.exists(&revision.id), "revision {} was collected", revision.sequence);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/// Only old revisions that are neither a ref target, nor a resource's latest
|
|
294
|
+
/// revision, nor an ancestor of either, are collected.
|
|
295
|
+
#[test]
|
|
296
|
+
fn gc_collects_exactly_the_old_unreachable_forks() {
|
|
297
|
+
let (_dir, _path, db) = fresh();
|
|
298
|
+
let store = store(&db);
|
|
299
|
+
// main line: 0 → 1 → 2 → 3, latest is 3
|
|
300
|
+
let main = chain(&store, "doc:g", 4);
|
|
301
|
+
// an old abandoned fork off 0: f1 (seq 1) → f2 (seq 2)
|
|
302
|
+
let f1 = old_child(&db, &main[0], json!({"fork": 1}));
|
|
303
|
+
let f2 = old_child(&db, &f1, json!({"fork": 2}));
|
|
304
|
+
// an old fork that a ref still names, with an old parent of its own
|
|
305
|
+
let kept_parent = old_child(&db, &main[0], json!({"kept": 1}));
|
|
306
|
+
let kept = old_child(&db, &kept_parent, json!({"kept": 2}));
|
|
307
|
+
store
|
|
308
|
+
.create_ref("candidates", "kept", kept.id.clone(), "app".into(), no_metadata())
|
|
309
|
+
.unwrap();
|
|
310
|
+
// a young fork, committed normally just now
|
|
311
|
+
let young = store
|
|
312
|
+
.commit(json!({"young": 1}).to_string(), &main[0], "app".into())
|
|
313
|
+
.unwrap();
|
|
314
|
+
// a second resource with no refs at all
|
|
315
|
+
let other = chain(&store, "doc:h", 3);
|
|
316
|
+
|
|
317
|
+
let report = store.collect_unreachable().unwrap();
|
|
318
|
+
let mut collected = report.collected_revisions.clone();
|
|
319
|
+
collected.sort();
|
|
320
|
+
let mut expected = vec![f1.id.clone(), f2.id.clone()];
|
|
321
|
+
expected.sort();
|
|
322
|
+
assert_eq!(collected, expected, "only the old, unreferenced, non-latest fork");
|
|
323
|
+
|
|
324
|
+
for kept in main
|
|
325
|
+
.iter()
|
|
326
|
+
.chain(other.iter())
|
|
327
|
+
.chain([&kept_parent, &kept, &young])
|
|
328
|
+
{
|
|
329
|
+
assert!(store.exists(&kept.id), "{} must survive", kept.id);
|
|
330
|
+
}
|
|
331
|
+
// Idempotent: nothing more to collect.
|
|
332
|
+
assert!(store.collect_unreachable().unwrap().collected_revisions.is_empty());
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// ---------------------------------------------------------------------------
|
|
336
|
+
// 4. GC against concurrent promotion and ref changes (P3)
|
|
337
|
+
// ---------------------------------------------------------------------------
|
|
338
|
+
|
|
339
|
+
/// A promotion of an old, collectable candidate races GC. Either GC wins and
|
|
340
|
+
/// the promotion is refused, or the promotion wins and the candidate survives.
|
|
341
|
+
/// Never a ref naming a deleted revision, and never lost latest history.
|
|
342
|
+
#[test]
|
|
343
|
+
fn gc_racing_promotion_never_dangles() {
|
|
344
|
+
let mut promoted = 0;
|
|
345
|
+
let mut refused = 0;
|
|
346
|
+
for iteration in 0..300 {
|
|
347
|
+
let (_dir, _path, db) = fresh();
|
|
348
|
+
let setup = store(&db);
|
|
349
|
+
let main = chain(&setup, "doc:p", 3);
|
|
350
|
+
let candidate = old_child(&db, &main[0], json!({"candidate": iteration}));
|
|
351
|
+
setup
|
|
352
|
+
.create_ref("heads", "main", main[0].id.clone(), "app".into(), no_metadata())
|
|
353
|
+
.unwrap();
|
|
354
|
+
|
|
355
|
+
let barrier = Arc::new(Barrier::new(2));
|
|
356
|
+
let promoter = {
|
|
357
|
+
let store = store(&db);
|
|
358
|
+
let barrier = barrier.clone();
|
|
359
|
+
let target = candidate.id.clone();
|
|
360
|
+
thread::spawn(move || {
|
|
361
|
+
barrier.wait();
|
|
362
|
+
store.compare_and_swap_ref("heads", "main", 1, target, "agent".into(), None)
|
|
363
|
+
})
|
|
364
|
+
};
|
|
365
|
+
let collector = {
|
|
366
|
+
let store = store(&db);
|
|
367
|
+
let barrier = barrier.clone();
|
|
368
|
+
thread::spawn(move || {
|
|
369
|
+
barrier.wait();
|
|
370
|
+
store.collect_unreachable()
|
|
371
|
+
})
|
|
372
|
+
};
|
|
373
|
+
let promotion = promoter.join().unwrap();
|
|
374
|
+
let _ = collector.join().unwrap();
|
|
375
|
+
|
|
376
|
+
let check = store(&db);
|
|
377
|
+
assert!(every_ref_target_exists(&check), "iteration {iteration}: dangling ref");
|
|
378
|
+
for revision in &main {
|
|
379
|
+
assert!(check.exists(&revision.id), "iteration {iteration}: latest history lost");
|
|
380
|
+
}
|
|
381
|
+
match promotion {
|
|
382
|
+
Ok(_) => {
|
|
383
|
+
promoted += 1;
|
|
384
|
+
assert!(check.exists(&candidate.id));
|
|
385
|
+
}
|
|
386
|
+
Err(RefError::UnknownTarget(_)) => {
|
|
387
|
+
refused += 1;
|
|
388
|
+
assert_eq!(check.get_ref("heads", "main").unwrap().version, 1);
|
|
389
|
+
}
|
|
390
|
+
Err(other) => panic!("iteration {iteration}: unexpected {other:?}"),
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
assert_eq!(promoted + refused, 300);
|
|
394
|
+
// Report both sides, so a run that never exercised one of them is visible.
|
|
395
|
+
eprintln!("gc_racing_promotion_never_dangles: promoted={promoted} refused={refused}");
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/// Refs are created, moved and deleted continuously, each round onto a freshly
|
|
399
|
+
/// written old (collectable) revision, while GC runs continuously. No ref ever
|
|
400
|
+
/// names a missing revision, and both sides make progress.
|
|
401
|
+
#[test]
|
|
402
|
+
fn gc_racing_ref_changes_never_dangles() {
|
|
403
|
+
let (_dir, _path, db) = fresh();
|
|
404
|
+
let setup = store(&db);
|
|
405
|
+
// Long enough that the old revisions (sequence 1) are not the resource's
|
|
406
|
+
// latest, so they are collectable whenever no ref names them.
|
|
407
|
+
let main = chain(&setup, "doc:r", 4);
|
|
408
|
+
|
|
409
|
+
let writer = {
|
|
410
|
+
let db = db.clone();
|
|
411
|
+
let store = store(&db);
|
|
412
|
+
let base = main[0].clone();
|
|
413
|
+
thread::spawn(move || {
|
|
414
|
+
let mut applied = 0;
|
|
415
|
+
let mut refused = 0;
|
|
416
|
+
for round in 0..600 {
|
|
417
|
+
let candidate = old_child(&db, &base, json!({"round": round}));
|
|
418
|
+
let name = format!("n{}", round % 8);
|
|
419
|
+
let outcome = match store.get_ref("live", &name) {
|
|
420
|
+
None => store
|
|
421
|
+
.create_ref("live", &name, candidate.id, "w".into(), BTreeMap::new())
|
|
422
|
+
.map(|_| ()),
|
|
423
|
+
Some(current) if round % 5 == 0 => {
|
|
424
|
+
store.delete_ref("live", &name, Some(current.version))
|
|
425
|
+
}
|
|
426
|
+
Some(current) => store
|
|
427
|
+
.compare_and_swap_ref("live", &name, current.version, candidate.id, "w".into(), None)
|
|
428
|
+
.map(|_| ()),
|
|
429
|
+
};
|
|
430
|
+
match outcome {
|
|
431
|
+
Ok(()) => applied += 1,
|
|
432
|
+
Err(RefError::UnknownTarget(_)) => refused += 1,
|
|
433
|
+
Err(other) => panic!("round {round}: unexpected {other:?}"),
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
(applied, refused)
|
|
437
|
+
})
|
|
438
|
+
};
|
|
439
|
+
let collector = {
|
|
440
|
+
let store = store(&db);
|
|
441
|
+
thread::spawn(move || {
|
|
442
|
+
let mut collected = 0;
|
|
443
|
+
for _ in 0..200 {
|
|
444
|
+
if let Ok(report) = store.collect_unreachable() {
|
|
445
|
+
collected += report.collected_revisions.len();
|
|
446
|
+
}
|
|
447
|
+
assert!(every_ref_target_exists(&store), "dangling ref observed mid-run");
|
|
448
|
+
}
|
|
449
|
+
collected
|
|
450
|
+
})
|
|
451
|
+
};
|
|
452
|
+
let (applied, refused) = writer.join().unwrap();
|
|
453
|
+
let collected = collector.join().unwrap();
|
|
454
|
+
eprintln!(
|
|
455
|
+
"gc_racing_ref_changes_never_dangles: ref writes applied={applied} \
|
|
456
|
+
refused_unknown_target={refused} revisions collected={collected}"
|
|
457
|
+
);
|
|
458
|
+
assert!(applied > 0, "the writer must have made progress");
|
|
459
|
+
assert!(collected > 0, "the collector must actually have collected");
|
|
460
|
+
let check = store(&db);
|
|
461
|
+
assert!(every_ref_target_exists(&check));
|
|
462
|
+
assert!(main.iter().all(|r| check.exists(&r.id)));
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// ---------------------------------------------------------------------------
|
|
466
|
+
// 5. Retry, restart, compaction, recreation
|
|
467
|
+
// ---------------------------------------------------------------------------
|
|
468
|
+
|
|
469
|
+
/// A CAS whose response was lost is retried with the same request after the
|
|
470
|
+
/// process restarted and the log was compacted: the retry succeeds and does
|
|
471
|
+
/// not advance the ref a second time.
|
|
472
|
+
#[test]
|
|
473
|
+
fn cas_retry_after_success_is_idempotent_across_restart_and_compaction() {
|
|
474
|
+
let (_dir, path, db) = fresh();
|
|
475
|
+
let (a, b) = {
|
|
476
|
+
let store = store(&db);
|
|
477
|
+
let revisions = chain(&store, "doc:i", 2);
|
|
478
|
+
store
|
|
479
|
+
.create_ref("heads", "main", revisions[0].id.clone(), "app".into(), no_metadata())
|
|
480
|
+
.unwrap();
|
|
481
|
+
let moved = store
|
|
482
|
+
.compare_and_swap_ref("heads", "main", 1, revisions[1].id.clone(), "app".into(), None)
|
|
483
|
+
.unwrap();
|
|
484
|
+
assert_eq!(moved.version, 2);
|
|
485
|
+
(revisions[0].id.clone(), revisions[1].id.clone())
|
|
486
|
+
};
|
|
487
|
+
drop(db);
|
|
488
|
+
|
|
489
|
+
let db = open(&path);
|
|
490
|
+
let retried = store(&db)
|
|
491
|
+
.compare_and_swap_ref("heads", "main", 1, b.clone(), "app".into(), None)
|
|
492
|
+
.expect("an identical retry after restart is the same request");
|
|
493
|
+
assert_eq!((retried.version, retried.target.clone()), (2, b.clone()));
|
|
494
|
+
assert_eq!(store(&db).get_ref("heads", "main").unwrap().version, 2);
|
|
495
|
+
|
|
496
|
+
let versions = db.operation_versions().unwrap();
|
|
497
|
+
db.acknowledge_peer_versions("peer".into(), versions).unwrap();
|
|
498
|
+
db.compact_operation_log(&["peer".into()]).unwrap();
|
|
499
|
+
drop(db);
|
|
500
|
+
|
|
501
|
+
let db = open(&path);
|
|
502
|
+
let store = store(&db);
|
|
503
|
+
let again = store
|
|
504
|
+
.compare_and_swap_ref("heads", "main", 1, b.clone(), "app".into(), None)
|
|
505
|
+
.expect("still the same request after compaction");
|
|
506
|
+
assert_eq!(again.version, 2);
|
|
507
|
+
// A different request for the same version slot is a conflict, not a replay.
|
|
508
|
+
assert_eq!(
|
|
509
|
+
store
|
|
510
|
+
.compare_and_swap_ref("heads", "main", 1, a, "app".into(), None)
|
|
511
|
+
.unwrap_err(),
|
|
512
|
+
RefError::VersionConflict { expected: 1, current: 2 }
|
|
513
|
+
);
|
|
514
|
+
assert_eq!(store.get_ref("heads", "main").unwrap().target, b);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
#[test]
|
|
518
|
+
fn create_and_delete_retries_are_idempotent_across_restart() {
|
|
519
|
+
let (_dir, path, db) = fresh();
|
|
520
|
+
let (a, b) = {
|
|
521
|
+
let store = store(&db);
|
|
522
|
+
let revisions = chain(&store, "doc:cd", 2);
|
|
523
|
+
store
|
|
524
|
+
.create_ref("heads", "main", revisions[0].id.clone(), "app".into(), no_metadata())
|
|
525
|
+
.unwrap();
|
|
526
|
+
(revisions[0].id.clone(), revisions[1].id.clone())
|
|
527
|
+
};
|
|
528
|
+
drop(db);
|
|
529
|
+
|
|
530
|
+
let db = open(&path);
|
|
531
|
+
let store_ = store(&db);
|
|
532
|
+
let created = store_
|
|
533
|
+
.create_ref("heads", "main", a.clone(), "app".into(), no_metadata())
|
|
534
|
+
.expect("identical create retried after restart");
|
|
535
|
+
assert_eq!(created.version, 1);
|
|
536
|
+
assert_eq!(
|
|
537
|
+
store_.create_ref("heads", "main", b.clone(), "app".into(), no_metadata()),
|
|
538
|
+
Err(RefError::AlreadyExists),
|
|
539
|
+
"a different create of an existing ref"
|
|
540
|
+
);
|
|
541
|
+
store_.delete_ref("heads", "main", Some(1)).unwrap();
|
|
542
|
+
drop(store_);
|
|
543
|
+
drop(db);
|
|
544
|
+
|
|
545
|
+
let db = open(&path);
|
|
546
|
+
let store = store(&db);
|
|
547
|
+
store
|
|
548
|
+
.delete_ref("heads", "main", Some(1))
|
|
549
|
+
.expect("an identical delete retried after restart");
|
|
550
|
+
assert!(store.get_ref("heads", "main").is_none());
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/// After delete and recreate, the ref is back at version 1. A CAS from 1 on the
|
|
554
|
+
/// new ref is a new request, never a replay of the first ref's CAS from 1.
|
|
555
|
+
#[test]
|
|
556
|
+
fn a_recreated_ref_does_not_replay_an_old_transaction() {
|
|
557
|
+
let (_dir, path, db) = fresh();
|
|
558
|
+
let (a, b) = {
|
|
559
|
+
let store = store(&db);
|
|
560
|
+
let revisions = chain(&store, "doc:rc", 2);
|
|
561
|
+
let (a, b) = (revisions[0].id.clone(), revisions[1].id.clone());
|
|
562
|
+
store.create_ref("heads", "main", a.clone(), "app".into(), no_metadata()).unwrap();
|
|
563
|
+
store
|
|
564
|
+
.compare_and_swap_ref("heads", "main", 1, b.clone(), "app".into(), None)
|
|
565
|
+
.unwrap();
|
|
566
|
+
store.delete_ref("heads", "main", Some(2)).unwrap();
|
|
567
|
+
(a, b)
|
|
568
|
+
};
|
|
569
|
+
drop(db);
|
|
570
|
+
|
|
571
|
+
let db = open(&path);
|
|
572
|
+
let store_ = store(&db);
|
|
573
|
+
let recreated = store_
|
|
574
|
+
.create_ref("heads", "main", a.clone(), "app".into(), no_metadata())
|
|
575
|
+
.unwrap();
|
|
576
|
+
assert_eq!((recreated.version, recreated.target), (1, a.clone()));
|
|
577
|
+
let moved = store_
|
|
578
|
+
.compare_and_swap_ref("heads", "main", 1, b.clone(), "app".into(), None)
|
|
579
|
+
.unwrap();
|
|
580
|
+
assert_eq!(moved.version, 2);
|
|
581
|
+
let current = store_.get_ref("heads", "main").unwrap();
|
|
582
|
+
assert_eq!((current.version, current.target), (2, b), "applied, not replayed");
|
|
583
|
+
drop(store_);
|
|
584
|
+
drop(db);
|
|
585
|
+
|
|
586
|
+
let db = open(&path);
|
|
587
|
+
assert_eq!(store(&db).get_ref("heads", "main").unwrap().version, 2);
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/// Refs written by the pre-transactional implementation remain usable.
|
|
591
|
+
#[test]
|
|
592
|
+
fn legacy_ref_rows_remain_casable_across_restart() {
|
|
593
|
+
let (_dir, path, db) = fresh();
|
|
594
|
+
let (a, b) = {
|
|
595
|
+
let store = store(&db);
|
|
596
|
+
let revisions = chain(&store, "doc:l", 2);
|
|
597
|
+
(revisions[0].id.clone(), revisions[1].id.clone())
|
|
598
|
+
};
|
|
599
|
+
legacy_ref(&db, "heads", "one", &a, 1);
|
|
600
|
+
legacy_ref(&db, "heads", "three", &a, 3);
|
|
601
|
+
drop(db);
|
|
602
|
+
|
|
603
|
+
let db = open(&path);
|
|
604
|
+
let store_ = store(&db);
|
|
605
|
+
assert_eq!(store_.get_ref("heads", "three").unwrap().version, 3);
|
|
606
|
+
assert_eq!(
|
|
607
|
+
store_
|
|
608
|
+
.compare_and_swap_ref("heads", "three", 1, b.clone(), "app".into(), None)
|
|
609
|
+
.unwrap_err(),
|
|
610
|
+
RefError::VersionConflict { expected: 1, current: 3 }
|
|
611
|
+
);
|
|
612
|
+
assert_eq!(
|
|
613
|
+
store_
|
|
614
|
+
.compare_and_swap_ref("heads", "one", 1, b.clone(), "app".into(), None)
|
|
615
|
+
.unwrap()
|
|
616
|
+
.version,
|
|
617
|
+
2
|
|
618
|
+
);
|
|
619
|
+
assert_eq!(
|
|
620
|
+
store_
|
|
621
|
+
.compare_and_swap_ref("heads", "three", 3, b.clone(), "app".into(), None)
|
|
622
|
+
.unwrap()
|
|
623
|
+
.version,
|
|
624
|
+
4
|
|
625
|
+
);
|
|
626
|
+
store_.delete_ref("heads", "one", Some(2)).unwrap();
|
|
627
|
+
drop(store_);
|
|
628
|
+
drop(db);
|
|
629
|
+
|
|
630
|
+
let db = open(&path);
|
|
631
|
+
let store = store(&db);
|
|
632
|
+
assert!(store.get_ref("heads", "one").is_none());
|
|
633
|
+
let three = store.get_ref("heads", "three").unwrap();
|
|
634
|
+
assert_eq!((three.version, three.target), (4, b));
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
// ---------------------------------------------------------------------------
|
|
638
|
+
// 6. Durability and atomicity of the write itself (P4)
|
|
639
|
+
// ---------------------------------------------------------------------------
|
|
640
|
+
|
|
641
|
+
fn record_type(line: &str) -> Option<String> {
|
|
642
|
+
serde_json::from_str::<Value>(line)
|
|
643
|
+
.ok()?
|
|
644
|
+
.get("record_type")?
|
|
645
|
+
.as_str()
|
|
646
|
+
.map(str::to_string)
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/// A ref move is one durable transaction record, which `FeltDb` fsyncs before
|
|
650
|
+
/// returning (`append_transaction` → `sync_data`).
|
|
651
|
+
#[test]
|
|
652
|
+
fn a_ref_move_is_one_transaction_record() {
|
|
653
|
+
let (_dir, path, db) = fresh();
|
|
654
|
+
let store = store(&db);
|
|
655
|
+
let revisions = chain(&store, "doc:d", 2);
|
|
656
|
+
store
|
|
657
|
+
.create_ref("heads", "main", revisions[0].id.clone(), "app".into(), no_metadata())
|
|
658
|
+
.unwrap();
|
|
659
|
+
let before = log_lines(&path);
|
|
660
|
+
store
|
|
661
|
+
.compare_and_swap_ref("heads", "main", 1, revisions[1].id.clone(), "app".into(), None)
|
|
662
|
+
.unwrap();
|
|
663
|
+
let after = log_lines(&path);
|
|
664
|
+
assert_eq!(after.len(), before.len() + 1, "one record per ref move");
|
|
665
|
+
assert_eq!(
|
|
666
|
+
record_type(after.last().unwrap()).as_deref(),
|
|
667
|
+
Some("feltdb.transaction.v1"),
|
|
668
|
+
"the ref move is a transaction record"
|
|
669
|
+
);
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
/// A committed revision and its content object are one transaction record.
|
|
673
|
+
#[test]
|
|
674
|
+
fn a_revision_commit_is_one_transaction_record() {
|
|
675
|
+
let (_dir, path, db) = fresh();
|
|
676
|
+
let store = store(&db);
|
|
677
|
+
let base = store
|
|
678
|
+
.create("doc:o", json!({"v": 0}).to_string(), "app".into())
|
|
679
|
+
.unwrap();
|
|
680
|
+
let before = log_lines(&path);
|
|
681
|
+
store
|
|
682
|
+
.commit(json!({"v": 1}).to_string(), &base, "app".into())
|
|
683
|
+
.unwrap();
|
|
684
|
+
let after = log_lines(&path);
|
|
685
|
+
assert_eq!(after.len(), before.len() + 1, "object and revision in one record");
|
|
686
|
+
assert_eq!(
|
|
687
|
+
record_type(after.last().unwrap()).as_deref(),
|
|
688
|
+
Some("feltdb.transaction.v1")
|
|
689
|
+
);
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/// Truncate the log at every byte inside the ref move's record: the database
|
|
693
|
+
/// always opens, and the ref is either entirely before or entirely after.
|
|
694
|
+
#[test]
|
|
695
|
+
fn a_ref_move_is_all_or_nothing_at_every_crash_point() {
|
|
696
|
+
let (_dir, path, db) = fresh();
|
|
697
|
+
let (a, b) = {
|
|
698
|
+
let store = store(&db);
|
|
699
|
+
let revisions = chain(&store, "doc:t", 2);
|
|
700
|
+
store
|
|
701
|
+
.create_ref("heads", "main", revisions[0].id.clone(), "app".into(), no_metadata())
|
|
702
|
+
.unwrap();
|
|
703
|
+
(revisions[0].id.clone(), revisions[1].id.clone())
|
|
704
|
+
};
|
|
705
|
+
let prefix = std::fs::read(&path).unwrap().len();
|
|
706
|
+
store(&db)
|
|
707
|
+
.compare_and_swap_ref("heads", "main", 1, b.clone(), "app".into(), None)
|
|
708
|
+
.unwrap();
|
|
709
|
+
drop(db);
|
|
710
|
+
let full = std::fs::read(&path).unwrap();
|
|
711
|
+
|
|
712
|
+
let scratch = TempDir::new().unwrap();
|
|
713
|
+
for cut in prefix..=full.len() {
|
|
714
|
+
let copy = scratch.path().join(format!("cut-{cut}.log"));
|
|
715
|
+
std::fs::write(©, &full[..cut]).unwrap();
|
|
716
|
+
let db = Arc::new(FeltDb::open(©).unwrap_or_else(|e| panic!("cut {cut}: {e:?}")));
|
|
717
|
+
let store = store(&db);
|
|
718
|
+
let main = store.get_ref("heads", "main").expect("the ref survives");
|
|
719
|
+
let state = (main.version, main.target.clone());
|
|
720
|
+
assert!(
|
|
721
|
+
state == (1, a.clone()) || state == (2, b.clone()),
|
|
722
|
+
"cut {cut}: torn ref {state:?}"
|
|
723
|
+
);
|
|
724
|
+
assert!(store.exists(&main.target), "cut {cut}: dangling ref");
|
|
725
|
+
drop(store);
|
|
726
|
+
drop(db);
|
|
727
|
+
std::fs::remove_file(©).ok();
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
// ---------------------------------------------------------------------------
|
|
732
|
+
// 7. Real process death
|
|
733
|
+
// ---------------------------------------------------------------------------
|
|
734
|
+
|
|
735
|
+
const CHILD: &str = "FELTDB_REF_AUTHORITY_CHILD";
|
|
736
|
+
const CHILD_PATH: &str = "FELTDB_REF_AUTHORITY_PATH";
|
|
737
|
+
|
|
738
|
+
/// The value after `marker` on any line. The test harness prints
|
|
739
|
+
/// `test <name> ... ` without a newline before the child's own output, so the
|
|
740
|
+
/// first marker can land mid-line.
|
|
741
|
+
fn marked(line: &str, marker: &str) -> Option<String> {
|
|
742
|
+
line.find(marker)
|
|
743
|
+
.map(|at| line[at + marker.len()..].trim().to_string())
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
fn child_command(test: &str, path: &Path, mode: &str) -> Command {
|
|
747
|
+
let mut command = Command::new(std::env::current_exe().unwrap());
|
|
748
|
+
command
|
|
749
|
+
.args([test, "--exact", "--nocapture", "--test-threads=1"])
|
|
750
|
+
.env(CHILD, mode)
|
|
751
|
+
.env(CHILD_PATH, path);
|
|
752
|
+
command
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/// The child acknowledges a CAS and dies by `abort()` before its caller could
|
|
756
|
+
/// have seen the result. The restarted caller retries the same request: it is
|
|
757
|
+
/// accepted as the request it already was, not applied twice.
|
|
758
|
+
#[test]
|
|
759
|
+
fn a_cas_acknowledged_before_abort_is_retried_idempotently() {
|
|
760
|
+
if std::env::var(CHILD).as_deref() == Ok("ack-then-abort") {
|
|
761
|
+
let path = PathBuf::from(std::env::var(CHILD_PATH).unwrap());
|
|
762
|
+
let db = open(&path);
|
|
763
|
+
let store = store(&db);
|
|
764
|
+
let revisions = chain(&store, "doc:k", 2);
|
|
765
|
+
store
|
|
766
|
+
.create_ref("heads", "main", revisions[0].id.clone(), "app".into(), no_metadata())
|
|
767
|
+
.unwrap();
|
|
768
|
+
store
|
|
769
|
+
.compare_and_swap_ref("heads", "main", 1, revisions[1].id.clone(), "app".into(), None)
|
|
770
|
+
.unwrap();
|
|
771
|
+
println!("ACK {}", revisions[1].id.as_hex());
|
|
772
|
+
std::process::abort();
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
let dir = TempDir::new().unwrap();
|
|
776
|
+
let path = dir.path().join("abort.log");
|
|
777
|
+
let output = child_command(
|
|
778
|
+
"a_cas_acknowledged_before_abort_is_retried_idempotently",
|
|
779
|
+
&path,
|
|
780
|
+
"ack-then-abort",
|
|
781
|
+
)
|
|
782
|
+
.output()
|
|
783
|
+
.unwrap();
|
|
784
|
+
assert!(!output.status.success(), "the child must have died");
|
|
785
|
+
let stdout = String::from_utf8_lossy(&output.stdout);
|
|
786
|
+
let acked = stdout
|
|
787
|
+
.lines()
|
|
788
|
+
.find_map(|line| marked(line, "ACK "))
|
|
789
|
+
.expect("the child acknowledged before dying");
|
|
790
|
+
let target = StateId::from_hex(acked);
|
|
791
|
+
|
|
792
|
+
let db = open(&path);
|
|
793
|
+
let store = store(&db);
|
|
794
|
+
let current = store.get_ref("heads", "main").unwrap();
|
|
795
|
+
assert_eq!((current.version, current.target.clone()), (2, target.clone()));
|
|
796
|
+
let retried = store
|
|
797
|
+
.compare_and_swap_ref("heads", "main", 1, target.clone(), "app".into(), None)
|
|
798
|
+
.expect("the retry of an acknowledged CAS is accepted");
|
|
799
|
+
assert_eq!(retried.version, 2);
|
|
800
|
+
assert_eq!(store.get_ref("heads", "main").unwrap().version, 2, "not applied twice");
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
/// The child commits a revision and dies before promoting it. After restart the
|
|
804
|
+
/// revision is durable, the ref is unchanged, and the promotion can complete.
|
|
805
|
+
#[test]
|
|
806
|
+
fn death_between_commit_and_promotion_leaves_a_durable_unpromoted_revision() {
|
|
807
|
+
if std::env::var(CHILD).as_deref() == Ok("commit-then-abort") {
|
|
808
|
+
let path = PathBuf::from(std::env::var(CHILD_PATH).unwrap());
|
|
809
|
+
let db = open(&path);
|
|
810
|
+
let store = store(&db);
|
|
811
|
+
let base = store
|
|
812
|
+
.create("doc:m", json!({"v": 0}).to_string(), "app".into())
|
|
813
|
+
.unwrap();
|
|
814
|
+
store
|
|
815
|
+
.create_ref("heads", "main", base.id.clone(), "app".into(), no_metadata())
|
|
816
|
+
.unwrap();
|
|
817
|
+
let next = store
|
|
818
|
+
.commit(json!({"v": 1}).to_string(), &base, "app".into())
|
|
819
|
+
.unwrap();
|
|
820
|
+
println!("COMMITTED {}", next.id.as_hex());
|
|
821
|
+
std::process::abort();
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
let dir = TempDir::new().unwrap();
|
|
825
|
+
let path = dir.path().join("abort.log");
|
|
826
|
+
let output = child_command(
|
|
827
|
+
"death_between_commit_and_promotion_leaves_a_durable_unpromoted_revision",
|
|
828
|
+
&path,
|
|
829
|
+
"commit-then-abort",
|
|
830
|
+
)
|
|
831
|
+
.output()
|
|
832
|
+
.unwrap();
|
|
833
|
+
assert!(!output.status.success());
|
|
834
|
+
let stdout = String::from_utf8_lossy(&output.stdout);
|
|
835
|
+
let committed = StateId::from_hex(
|
|
836
|
+
stdout
|
|
837
|
+
.lines()
|
|
838
|
+
.find_map(|line| marked(line, "COMMITTED "))
|
|
839
|
+
.expect("the child committed before dying"),
|
|
840
|
+
);
|
|
841
|
+
|
|
842
|
+
let db = open(&path);
|
|
843
|
+
let store = store(&db);
|
|
844
|
+
assert!(store.exists(&committed), "the committed revision is durable");
|
|
845
|
+
assert_eq!(store.get_ref("heads", "main").unwrap().version, 1, "not promoted");
|
|
846
|
+
assert!(store.collect_unreachable().unwrap().collected_revisions.is_empty());
|
|
847
|
+
store
|
|
848
|
+
.compare_and_swap_ref("heads", "main", 1, committed.clone(), "app".into(), None)
|
|
849
|
+
.unwrap();
|
|
850
|
+
assert_eq!(store.get_ref("heads", "main").unwrap().target, committed);
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
/// The child moves refs continuously and reports each acknowledged move; the
|
|
854
|
+
/// parent SIGKILLs it at an arbitrary point. Every acknowledged move survives,
|
|
855
|
+
/// and the ref never names a missing revision.
|
|
856
|
+
#[test]
|
|
857
|
+
fn sigkill_during_ref_moves_keeps_every_acknowledged_move() {
|
|
858
|
+
if std::env::var(CHILD).as_deref() == Ok("move-forever") {
|
|
859
|
+
let path = PathBuf::from(std::env::var(CHILD_PATH).unwrap());
|
|
860
|
+
let db = open(&path);
|
|
861
|
+
let store = store(&db);
|
|
862
|
+
let base = store
|
|
863
|
+
.create("doc:s", json!({"v": 0}).to_string(), "app".into())
|
|
864
|
+
.unwrap();
|
|
865
|
+
store
|
|
866
|
+
.create_ref("heads", "main", base.id.clone(), "app".into(), no_metadata())
|
|
867
|
+
.unwrap();
|
|
868
|
+
let mut head = base;
|
|
869
|
+
let mut version = 1;
|
|
870
|
+
loop {
|
|
871
|
+
let next = store
|
|
872
|
+
.commit(json!({"v": version}).to_string(), &head, "app".into())
|
|
873
|
+
.unwrap();
|
|
874
|
+
let moved = store
|
|
875
|
+
.compare_and_swap_ref("heads", "main", version, next.id.clone(), "app".into(), None)
|
|
876
|
+
.unwrap();
|
|
877
|
+
version = moved.version;
|
|
878
|
+
println!("ACK {version}");
|
|
879
|
+
head = next;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
let dir = TempDir::new().unwrap();
|
|
884
|
+
let path = dir.path().join("kill.log");
|
|
885
|
+
let mut child = child_command(
|
|
886
|
+
"sigkill_during_ref_moves_keeps_every_acknowledged_move",
|
|
887
|
+
&path,
|
|
888
|
+
"move-forever",
|
|
889
|
+
)
|
|
890
|
+
.stdout(Stdio::piped())
|
|
891
|
+
.stderr(Stdio::null())
|
|
892
|
+
.spawn()
|
|
893
|
+
.unwrap();
|
|
894
|
+
let mut last_acked = 0;
|
|
895
|
+
for line in BufReader::new(child.stdout.take().unwrap()).lines() {
|
|
896
|
+
let line = line.unwrap();
|
|
897
|
+
if let Some(version) = marked(&line, "ACK ") {
|
|
898
|
+
last_acked = version.parse::<u64>().unwrap();
|
|
899
|
+
if last_acked >= 40 {
|
|
900
|
+
child.kill().unwrap(); // SIGKILL
|
|
901
|
+
break;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
child.wait().unwrap();
|
|
906
|
+
assert!(last_acked >= 40);
|
|
907
|
+
|
|
908
|
+
let db = open(&path);
|
|
909
|
+
let store = store(&db);
|
|
910
|
+
let main = store.get_ref("heads", "main").unwrap();
|
|
911
|
+
assert!(
|
|
912
|
+
main.version >= last_acked,
|
|
913
|
+
"acknowledged version {last_acked} lost; recovered {}",
|
|
914
|
+
main.version
|
|
915
|
+
);
|
|
916
|
+
assert!(store.exists(&main.target), "dangling ref after SIGKILL");
|
|
917
|
+
assert!(every_ref_target_exists(&store));
|
|
918
|
+
}
|