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
//! What:
//! Startup recovery across every per-transaction directory of one worktree.
//! Why:
//! Live owners are skipped;
//! dead owners are recovered oldest first;
//! dead transactions that
//! entered the landing critical section are recovered only while holding the landing lock,
//! so recovery never races a live lander (`src/policy-engine/commit-transaction-recovery-scan.ts`,
//! `commit-landing-lock.ts`).
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! const outcomes = await recoverRegisteredTransactions({ root, gitPath, effectiveCwd });
//! ```
/// Debug diagnostics.
use super::diagnostic_log::debug;
/// Owner locks.
use super::live_lock::acquire_live_owner_lock;
use super::owner_lock::{OwnerLock, retire_lock_of_dead_owner};
/// The fail-closed recovery failure.
use super::recovery_error::{RecoveryError, io_failure};
/// Landing evidence.
use super::recovery_evidence::entered_landing;
/// Owner inspection and order.
use super::recovery_inspect::{
InspectedEntry, OwnerEvidence, inspect_registry_entry, oldest_first,
};
/// Dead-transaction recovery.
use super::recovery_landing::{RecoveryAction, RecoveryPlace, recover_dead_transaction};
/// Owner liveness.
use super::transaction_owner::OwnerLiveness;
/// Registry listing and names.
use super::transaction_registry::{
EntryKind, LANDING_LOCK_NAME, RESERVATION_LOCK_NAME, RegistryEntry, list_transaction_entries,
};
/// `Path`/`PathBuf` are borrowed/owned filesystem paths.
use std::path::{Path, PathBuf};
/// What:
/// What recovery did for one directory.
/// Why:
/// `CommitTransactionRecoveryOutcome`.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type CommitTransactionRecoveryOutcome = { directory: string; action: CommitTransactionRecoveryAction };
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RecoveryOutcome {
/// The directory examined.
pub directory: PathBuf,
/// What recovery did,
/// or why it left the directory.
pub action: RecoveryAction,
}
/// What:
/// Recover or skip one inspected entry without a landing record,
/// or one whose landing the
/// caller serializes.
/// Why:
/// `recoverInspectedEntry`.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function recoverInspectedEntry({ inspected, gitPath, effectiveCwd }): Promise<CommitTransactionRecoveryAction>;
/// ```
pub fn recover_inspected_entry(
place: &RecoveryPlace,
inspected: &InspectedEntry,
) -> Result<RecoveryAction, RecoveryError> {
let path: &Path = inspected.entry.path.as_path();
match &inspected.owner {
OwnerEvidence::Vanished => {
debug(
"recoverInspectedEntry",
format!(
"transaction finished while recovery listed it: {}",
path.display()
)
.as_str(),
);
return Ok(RecoveryAction::Vanished);
}
OwnerEvidence::Unattributed => {
debug(
"recoverInspectedEntry",
format!(
"staging candidate without a complete owner record left in place: {}",
path.display()
)
.as_str(),
);
return Ok(RecoveryAction::StagingUnattributed);
}
OwnerEvidence::Owned(record, OwnerLiveness::Alive) => {
debug(
"recoverInspectedEntry",
format!(
"transaction owner {} is active; skipping {}",
record.owner_pid,
path.display()
)
.as_str(),
);
return Ok(RecoveryAction::OwnerActive);
}
OwnerEvidence::Owned(record, OwnerLiveness::Dead) => {
if inspected.entry.kind == EntryKind::Staging {
// An unpublished candidate never started capture, so it touched nothing outside
// itself and remains for diagnosis.
debug(
"recoverInspectedEntry",
format!(
"dead owner stopped before publishing; staging candidate kept: {}",
path.display()
)
.as_str(),
);
return Ok(RecoveryAction::StagingRetained);
}
return recover_dead_transaction_unless_vanished(
place,
path,
inspected.entry.transaction_id.as_str(),
record.owner_pid,
);
}
}
}
/// What:
/// `recover_dead_transaction`, answering "vanished" when the failure came from a transaction directory that is gone.
/// Why:
/// Another recoverer of the same dead transaction may finish and remove it after this recoverer inspected it;
/// the incumbent treats a directory that disappeared between listing and reading as finished,
/// whereas a failure with the directory still present is real.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// try { return await recoverDeadTransaction(request); } catch (e) { if (!(await pathExists(directory))) return 'vanished'; throw e; }
/// ```
fn recover_dead_transaction_unless_vanished(
place: &RecoveryPlace,
directory: &Path,
transaction_id: &str,
owner_pid: i64,
) -> Result<RecoveryAction, RecoveryError> {
match recover_dead_transaction(place, directory, transaction_id, owner_pid) {
Ok(action) => return Ok(action),
Err(error) => {
if directory.exists() {
return Err(error);
}
debug(
"recoverInspectedEntry",
format!("transaction finished while recovery recovered it: {}", directory.display()).as_str(),
);
return Ok(RecoveryAction::Vanished);
}
}
}
/// What:
/// Whether an inspected entry is a dead published transaction that entered the landing critical section:
/// it holds a landing record or the record of a real `index.lock` it created.
/// Why:
/// `needsLandingLock` keyed on the landing record alone, so a transaction killed holding the landing lock and
/// `index.lock` (an index-lock record, no landing record yet) was recovered without the landing lock.
/// Another recoverer under the lock could release that `index.lock` and a lander could create a new one at the
/// same path before this late release deleted it.
/// `entered_landing` is the predicate the landing pass already uses,
/// and the landing pass is documented as the only recovery that may release such a lock.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function needsLandingLock(inspected: InspectedEntry): Promise<boolean>;
/// ```
fn needs_landing_lock(inspected: &InspectedEntry) -> Result<bool, RecoveryError> {
if inspected.entry.kind != EntryKind::Transaction || inspected.dead_owner().is_none() {
return Ok(false);
}
return entered_landing_unless_vanished(inspected.entry.path.as_path());
}
/// What:
/// `entered_landing`, answering "no" for a transaction directory that is gone.
/// Why:
/// Another recoverer of the same dead transaction may finish and remove it between this recoverer's registry listing
/// and this check; a transaction that no longer exists has nothing to recover,
/// whereas a directory that exists but cannot be listed stays an error.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// try { return await enteredLanding(directory); } catch (e) { if (e.code === 'ENOENT') return false; throw e; }
/// ```
fn entered_landing_unless_vanished(directory: &Path) -> Result<bool, RecoveryError> {
match entered_landing(directory) {
Ok(entered) => return Ok(entered),
Err(error) => {
if directory.exists() {
return Err(error);
}
debug(
"recoverRegisteredTransactions",
format!("transaction finished while recovery listed it: {}", directory.display()).as_str(),
);
return Ok(false);
}
}
}
/// What:
/// Recover every dead published transaction that entered the landing critical section;
/// the
/// caller holds the landing lock.
/// Why:
/// `recoverDeadLandings`:
/// an owner killed while holding the landing lock and `index.lock`
/// has no landing record yet,
/// and its `index.lock` would block the next landing.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function recoverDeadLandings({ root, gitPath, effectiveCwd }): Promise<readonly CommitTransactionRecoveryOutcome[]>;
/// ```
pub fn recover_dead_landings(
place: &RecoveryPlace,
root: &Path,
) -> Result<Vec<RecoveryOutcome>, RecoveryError> {
let mut inspected: Vec<InspectedEntry> = Vec::new();
for entry in list_transaction_entries(root)? {
if entry.kind == EntryKind::Transaction {
inspected.push(inspect_registry_entry(&entry)?);
}
}
// `mut` allows collecting outcomes in recovery order.
let mut outcomes: Vec<RecoveryOutcome> = Vec::new();
for item in oldest_first(inspected) {
let Some(record) = item.dead_owner() else {
continue;
};
if !entered_landing_unless_vanished(item.entry.path.as_path())? {
continue;
}
debug(
"recoverDeadLandings",
format!("recovering dead landing {}", item.entry.path.display()).as_str(),
);
let action: RecoveryAction = recover_dead_transaction_unless_vanished(
place,
item.entry.path.as_path(),
item.entry.transaction_id.as_str(),
record.owner_pid,
)?;
outcomes.push(RecoveryOutcome {
directory: item.entry.path.clone(),
action,
});
}
return Ok(outcomes);
}
/// What:
/// Take the landing lock,
/// waiting while a live owner holds it.
/// Why:
/// Recovery of a landing must never race a live lander.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// await acquireOwnerLock({ lockDirectory: join(root, LANDING_LOCK_NAME) })
/// ```
pub fn acquire_registry_lock(root: &Path, name: &str) -> Result<OwnerLock, RecoveryError> {
let directory: PathBuf = root.join(name);
match acquire_live_owner_lock(directory.as_path(), "a transaction registry lock") {
Ok(lock) => return Ok(lock),
Err(error) => return Err(RecoveryError(error.to_string())),
}
}
/// What:
/// Whether an outcome already names a directory.
/// Why:
/// The landing pass may have recovered an entry the main pass would visit again.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// outcomes.some((outcome) => outcome.directory === path)
/// ```
fn recovered_already(outcomes: &[RecoveryOutcome], path: &Path) -> bool {
for outcome in outcomes {
if outcome.directory == path {
return true;
}
}
return false;
}
/// What:
/// Whether the landing pass owns an entry:
/// it was known to need the landing lock, or the pass has already recovered it.
/// Why:
/// An entry whose owner died after the first inspection is recovered by the landing pass although it did not need
/// the lock then, and one that vanished before the pass is not recovered at all;
/// in both cases the main pass must leave it alone instead of acting on its stale inspection.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// needed || outcomes.some((outcome) => outcome.directory === path)
/// ```
fn handled_by_the_landing_pass(needed: bool, outcomes: &[RecoveryOutcome], path: &Path) -> bool {
return needed || recovered_already(outcomes, path);
}
/// What:
/// Recover every dead-owner transaction of one worktree registry and remove retired
/// leftovers;
/// a dead reservation holder's lock is retired too.
/// Why:
/// `recoverRegisteredTransactions`.
/// Unlike the incumbent,
/// an entry the landing pass already
/// recovered is not recovered a second time (the incumbent would fail reading its removed
/// directory;
/// recorded in `doc/handover/cli-git-native-transactions.md`).
///
/// In TS you'd write (pseudocode):
/// ```ts
/// async function recoverRegisteredTransactions({ root, gitPath, effectiveCwd }): Promise<readonly CommitTransactionRecoveryOutcome[]>;
/// ```
pub fn recover_registered_transactions(
place: &RecoveryPlace,
root: &Path,
) -> Result<Vec<RecoveryOutcome>, RecoveryError> {
let entries: Vec<RegistryEntry> = list_transaction_entries(root)?;
let mut inspected: Vec<InspectedEntry> = Vec::new();
let mut retired: Vec<PathBuf> = Vec::new();
for entry in &entries {
if entry.kind == EntryKind::Retired {
retired.push(entry.path.clone());
} else {
inspected.push(inspect_registry_entry(entry)?);
}
}
let ordered: Vec<InspectedEntry> = oldest_first(inspected);
let mut needs: Vec<bool> = Vec::with_capacity(ordered.len());
for item in &ordered {
needs.push(needs_landing_lock(item)?);
}
let mut outcomes: Vec<RecoveryOutcome> = Vec::new();
if needs.contains(&true) {
let lock: OwnerLock = acquire_registry_lock(root, LANDING_LOCK_NAME)?;
let recovered: Result<Vec<RecoveryOutcome>, RecoveryError> =
recover_dead_landings(place, root);
let released: Result<(), super::owner_lock::OwnerLockError> = lock.release();
outcomes.extend(recovered?);
if let Err(error) = released {
return Err(RecoveryError(error.to_string()));
}
}
for (index, item) in ordered.iter().enumerate() {
if handled_by_the_landing_pass(needs[index], outcomes.as_slice(), item.entry.path.as_path()) {
continue;
}
let action: RecoveryAction = recover_inspected_entry(place, item)?;
outcomes.push(RecoveryOutcome {
directory: item.entry.path.clone(),
action,
});
}
for path in &retired {
if let Err(error) = super::private_storage::remove_tree(path.as_path()) {
return Err(io_failure(
"removing the retired transaction",
path.as_path(),
&error,
));
}
}
// A crashed reservation holder's lock is retired here as well as by the next requester.
if let Err(error) = retire_lock_of_dead_owner(root.join(RESERVATION_LOCK_NAME).as_path()) {
return Err(RecoveryError(error.to_string()));
}
for path in retired {
outcomes.push(RecoveryOutcome {
directory: path,
action: RecoveryAction::RetiredRemoved,
});
}
return Ok(outcomes);
}
/// Registry recovery controls stay out of the release executable.
#[cfg(test)]
#[path = "recovery_scan_tests.rs"]
mod tests;