1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! What:
//! The starvation reservation of the next landing slot:
//! a transaction that lost `landing.reserveAfterLostRaces` races writes `reservation-request`,
//! takes the reservation lock `reservation.lock` in the registry when it is granted,
//! and every other transaction waits before it takes the landing lock.
//! Why:
//! Without it a commit that keeps losing the race can starve behind a stream of landings.
//! Grant order is the oldest live requester, so the transaction that waited longest goes first.
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! await using reservation = await openLandingReservation({ registryRoot, transactionDirectory, transactionId });
//! await reservation.reserve(lostRaces);
//! ```
/// The backstop every wait on a live process is bounded by.
use super::live_lock::{LIVE_WAIT_VARIABLE, live_wait_limit};
/// Owner-lock records and their liveness.
use super::owner_lock::{OwnerLock, OwnerLockError, try_acquire_owner_lock};
use super::owner_lock_record::{
OwnerLockRecord, OwnerRecordError, PublishedOwner, owner_lock_holder_is_alive, read_owner_lock_record,
};
/// Registry layout and grant order.
use super::recovery_inspect::{InspectedEntry, OwnerEvidence, oldest_first};
use super::transaction_owner::OwnerLiveness;
use super::transaction_registry::{EntryKind, RESERVATION_LOCK_NAME, list_transaction_entries};
/// Private file writes that survive a crash.
use super::private_storage::{sync_directory, write_private_file};
use super::recovery_error::RecoveryError;
/// `Path`/`PathBuf` are borrowed/owned filesystem paths.
use std::path::{Path, PathBuf};
/// Delay between reservation checks, and the clock a wait is measured on.
use std::time::{Duration, Instant};
/// Name of the request file inside a transaction directory.
pub const RESERVATION_REQUEST_FILENAME: &str = "reservation-request";
/// Delay between reservation and holder checks, as the incumbent's `RESERVATION_POLL_MS`.
pub const RESERVATION_POLL: Duration = Duration::from_millis(20);
/// What:
/// A reservation failure:
/// the registry, a lock record, or a request file could not be read or written.
/// Why:
/// Replay fails the transaction with the underlying reason instead of landing without the reservation.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// throw new CommitTransactionGitError(...);
/// ```
#[derive(Debug)]
pub enum ReservationError {
/// The transaction registry could not be listed or inspected.
Registry(RecoveryError),
/// The reservation lock's record could not be read.
Record(OwnerRecordError),
/// The reservation lock could not be taken or released.
Lock(OwnerLockError),
/// The holder's process identity could not be probed.
Identity(String),
/// A request file or its directory could not be written.
Write(std::io::Error),
/// A live reservation holder or an older live requester kept the slot until the wait limit ran out.
WaitTimedOut {
/// How long this transaction waited.
waited: Duration,
/// The reservation holder's process, when one holds it and its record could be read.
holder_pid: Option<i64>,
},
}
/// Text of each failure:
/// the debug form for the failures that carry another error, a sentence for a refused wait.
impl std::fmt::Display for ReservationError {
/// Writes the diagnostic text of the failure.
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
return match self {
Self::WaitTimedOut { waited, holder_pid } => {
let who: String = match holder_pid {
Some(pid) => format!("process {pid} holds the reservation"),
None => String::from("an older live landing request holds the next slot"),
};
write!(
formatter,
"gave up after waiting {} seconds for the next landing slot because {who}; \
{LIVE_WAIT_VARIABLE} sets how long to wait, and nothing was taken from that process",
waited.as_secs()
)
}
other => write!(formatter, "{other:?}"),
};
}
}
/// What:
/// The request file bytes:
/// `{"schemaVersion":2,"state":"reservation-request","lostRaces":<n>}` and a newline.
/// Why:
/// The TypeScript writes this object, not the empty marker SPEC names;
/// both wrappers read the same file, so the bytes match the incumbent.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// `${JSON.stringify({ schemaVersion: 2, state: 'reservation-request', lostRaces })}\n`
/// ```
pub fn reservation_request_bytes(lost_races: u64) -> Vec<u8> {
return format!(
"{{\"schemaVersion\":2,\"state\":\"reservation-request\",\"lostRaces\":{lost_races}}}\n"
)
.into_bytes();
}
/// What:
/// Whether a lost race earns the reservation:
/// the `reserve_after`-th lost race and every later one.
/// Why:
/// `landing.reserveAfterLostRaces` counts races lost so far, from 1; the check runs after each loss.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// lostRaces >= input.reserveAfterLostRaces
/// ```
pub fn reservation_earned(lost_races: u64, reserve_after: u64) -> bool {
return lost_races >= reserve_after;
}
/// What:
/// The transactions with a live request, oldest first; the first one is the next grant.
/// Why:
/// Grant order is the oldest live requester, with the registry's creation key breaking ties.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// await liveRequesters(registryRoot); // transaction IDs, oldest invocation first
/// ```
pub fn live_requesters(registry_root: &Path) -> Result<Vec<String>, ReservationError> {
let entries = list_transaction_entries(registry_root).map_err(ReservationError::Registry)?;
// `mut` lets the requesting entries be gathered from the registry listing.
let mut requesting: Vec<InspectedEntry> = Vec::new();
for entry in entries {
if !matches!(entry.kind, EntryKind::Transaction) || !entry.path.join(RESERVATION_REQUEST_FILENAME).exists() {
continue;
}
requesting.push(super::recovery_inspect::inspect_registry_entry(&entry).map_err(ReservationError::Registry)?);
}
return Ok(oldest_first(requesting)
.into_iter()
.filter(|inspected| { return matches!(inspected.owner, OwnerEvidence::Owned(_, OwnerLiveness::Alive)); })
.map(|inspected| { return inspected.entry.transaction_id; })
.collect());
}
/// What:
/// The live holder of the reservation lock:
/// its record when the lock is published and its owner is alive, otherwise nothing.
/// Why:
/// A dead holder's reservation no longer blocks anyone; its lock is retired by the next acquisition.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// liveHolder(lockDirectory); // OwnerLockRecord | NO_HOLDER
/// ```
pub fn live_holder(lock_directory: &Path) -> Result<Option<OwnerLockRecord>, ReservationError> {
let published: PublishedOwner = read_owner_lock_record(lock_directory).map_err(ReservationError::Record)?;
let record = match published {
PublishedOwner::Owner(found) => found,
PublishedOwner::Busy => return Ok(None),
};
let alive: bool = owner_lock_holder_is_alive(&record)
.map_err(|error| { return ReservationError::Identity(format!("{error:?}")); })?;
return Ok(if alive { Some(record) } else { None });
}
/// What:
/// One transaction's view of the reservation:
/// whether another transaction holds it, waiting for it, requesting it, and releasing it.
/// Why:
/// The landing loop takes the reservation after its lost races and must give it back when the loop ends,
/// landed or failed.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type LandingReservation = AsyncDisposable & { awaitSlot; heldByAnother; reserve };
/// ```
pub struct LandingReservation {
/// Transaction registry.
registry_root: PathBuf,
/// This transaction's directory, which holds the request file.
transaction_directory: PathBuf,
/// This transaction's ID.
transaction_id: String,
/// Whether this invocation lands under an ancestor's landing lock, which neither waits nor takes the reservation.
nested: bool,
/// The reservation, once granted to this transaction.
held: Option<OwnerLock>,
}
impl LandingReservation {
/// What:
/// Opens the reservation view for one landing loop.
/// Why:
/// The caller decides `nested` from its inherited landing lease, which this module does not read.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// await openLandingReservation({ registryRoot, transactionDirectory, transactionId });
/// ```
pub fn open(registry_root: &Path, transaction_directory: &Path, transaction_id: &str, nested: bool) -> Self {
return Self {
registry_root: registry_root.to_path_buf(),
transaction_directory: transaction_directory.to_path_buf(),
transaction_id: String::from(transaction_id),
nested,
held: None,
};
}
/// The reservation lock's directory.
fn lock_directory(&self) -> PathBuf {
return self.registry_root.join(RESERVATION_LOCK_NAME);
}
/// What:
/// Whether another transaction holds a live reservation right now.
/// Why:
/// The holder's own token never counts against itself.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// await reservation.heldByAnother();
/// ```
pub fn held_by_another(&self) -> Result<bool, ReservationError> {
if self.nested {
return Ok(false);
}
let holder = live_holder(self.lock_directory().as_path())?;
return Ok(match (holder, self.held.as_ref()) {
(None, _) => false,
(Some(record), Some(lock)) => record.token != lock.token(),
(Some(_), None) => true,
});
}
/// What:
/// Waits while a live reservation owned by another transaction exists.
/// Why:
/// A landing may take the landing lock only after the holder has finished its landing loop.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// await reservation.awaitSlot();
/// ```
pub fn await_slot(&self) -> Result<(), ReservationError> {
return self.await_slot_for(live_wait_limit());
}
/// What:
/// Waits while a live reservation owned by another transaction exists, for at most `limit`.
/// Why:
/// Tests and the default wait share one loop; only the limit differs.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// await reservation.awaitSlot({ limitMs });
/// ```
pub fn await_slot_for(&self, limit: Duration) -> Result<(), ReservationError> {
let started: Instant = Instant::now();
while self.held_by_another()? {
self.refuse_after(started, limit)?;
std::thread::sleep(RESERVATION_POLL);
}
return Ok(());
}
/// What:
/// Refuse to keep waiting once `limit` has passed since `started`.
/// Why:
/// A live process that never finishes its landing would otherwise hold every other transaction for good;
/// the refusal names it and leaves its lock and request alone.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// if (Date.now() - startedMs >= limitMs) throw new ReservationTimeout(holderPid);
/// ```
fn refuse_after(&self, started: Instant, limit: Duration) -> Result<(), ReservationError> {
let waited: Duration = started.elapsed();
if !limit_reached(waited, limit) {
return Ok(());
}
let holder_pid: Option<i64> = live_holder(self.lock_directory().as_path())?.map(|record| {
return record.owner_pid;
});
return Err(ReservationError::WaitTimedOut { waited, holder_pid });
}
/// What:
/// Writes the request, then waits until this transaction is the oldest live requester and the lock is free,
/// and takes the reservation.
/// Why:
/// The first requester in grant order gets the next slot, so a starving commit cannot be overtaken again.
/// An inherited landing lease neither asks nor waits.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// await reservation.reserve(lostRaces); // false when already held or not applicable
/// ```
pub fn reserve(&mut self, lost_races: u64) -> Result<bool, ReservationError> {
return self.reserve_for(lost_races, live_wait_limit());
}
/// What:
/// Writes the request and waits for the grant, for at most `limit`.
/// Why:
/// Tests and the default wait share one loop; only the limit differs.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// await reservation.reserve(lostRaces, { limitMs });
/// ```
pub fn reserve_for(&mut self, lost_races: u64, limit: Duration) -> Result<bool, ReservationError> {
if self.nested || self.held.is_some() {
return Ok(false);
}
write_private_file(
self.transaction_directory.join(RESERVATION_REQUEST_FILENAME).as_path(),
&reservation_request_bytes(lost_races),
)
.map_err(ReservationError::Write)?;
sync_directory(self.transaction_directory.as_path()).map_err(ReservationError::Write)?;
let started: Instant = Instant::now();
loop {
let lock_directory: PathBuf = self.lock_directory();
let free: bool = live_holder(lock_directory.as_path())?.is_none();
let first: bool = live_requesters(self.registry_root.as_path())?.first() == Some(&self.transaction_id);
let granted: Option<OwnerLock> = if free && first {
try_acquire_owner_lock(lock_directory.as_path(), Some(self.transaction_id.as_str()))
.map_err(ReservationError::Lock)?
} else {
None
};
if let Some(lock) = granted {
self.held = Some(lock);
return Ok(true);
}
self.refuse_after(started, limit)?;
std::thread::sleep(RESERVATION_POLL);
}
}
/// What:
/// Releases the reservation if held and removes the request file.
/// Why:
/// The request goes even when the reservation was never granted, so no stale request blocks the next grant.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// await reservation[Symbol.asyncDispose]();
/// ```
pub fn release(mut self) -> Result<(), ReservationError> {
let request: PathBuf = self.transaction_directory.join(RESERVATION_REQUEST_FILENAME);
match std::fs::remove_file(&request) {
Ok(()) => {}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
Err(error) => return Err(ReservationError::Write(error)),
}
if let Some(lock) = self.held.take() {
lock.release().map_err(ReservationError::Lock)?;
}
return Ok(());
}
}
/// What:
/// Whether a wait has used up its limit; a wait of exactly the limit has.
/// Why:
/// The clock cannot be set to land on the limit, so the comparison is a function a test can call with chosen values.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const limitReached = (waitedMs: number, limitMs: number): boolean => waitedMs >= limitMs;
/// ```
fn limit_reached(waited: Duration, limit: Duration) -> bool {
return waited >= limit;
}
/// What:
/// Takes the landing lock once no other transaction's reservation stands in the way,
/// checking again under the lock, so a reservation granted while this landing waited is honoured.
/// Why:
/// The landing lock is acquired by the caller's closure because its module belongs to the landing lane.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// await acquireReservedLandingLock({ gitPath, cwd, registryRoot, reservation });
/// ```
pub fn acquire_reserved_landing_lock<F>(
reservation: &LandingReservation,
mut acquire_landing: F,
) -> Result<OwnerLock, ReservationError>
where
F: FnMut() -> Result<OwnerLock, ReservationError>,
{
loop {
reservation.await_slot()?;
let lock: OwnerLock = acquire_landing()?;
if !reservation.held_by_another()? {
return Ok(lock);
}
lock.release().map_err(ReservationError::Lock)?;
}
}
#[cfg(test)]
#[path = "replay_reservation_tests.rs"]
mod tests;