aft-store 0.64.0

On-disk index formats for Agent File Tools
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//! An exhaustive check of the publication and reclamation protocol.
//!
//! The model has the same steps as the code in `writer.rs`, `snapshot.rs`
//! and `lease.rs`, each taken as one atomic file operation, and explores
//! every interleaving of one writer and two readers, with readers crashing
//! at any point. The files a reclaimer judges liveness by are modelled as
//! files: each reader's owner file (present and locked while its process
//! lives, present and unlocked once it has ended, or removed), and its
//! lease record. A lease scan lists lease records and owner files in two
//! separate steps, as the code does, and removes the leases it judges dead.
//! It checks two properties in every reachable state:
//!
//! - a reader never reads a segment that has been removed;
//! - the newest manifest and every segment it names exist.
//!
//! Each control below removes one rule of the protocol, and the checker must
//! find a violation for it. That keeps the check honest: a model too weak to
//! fail would pass the real protocol for no reason.

use std::collections::{BTreeMap, BTreeSet, HashSet};

/// One way to break the protocol, for the controls.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Rule {
    /// The protocol as written.
    All,
    /// The horizon ignores the newest generation, so with no lease it has no
    /// bound and everything is reclaimed.
    NoNewestBound,
    /// The reclaimer does not check the leases again after writing the
    /// horizon.
    NoRescan,
    /// A reader does not check the horizon after taking its lease.
    NoReaderCheck,
    /// A reader takes its lease after checking the horizon instead of before.
    LeaseAfterCheck,
    /// A lease scan lists owner files before lease records, so a process
    /// that appears in between has its new lease judged dead and removed.
    OwnersFirst,
}

/// A reader's owner file in `leases/`.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
enum OwnerFile {
    Absent,
    /// Locked by its live process.
    Locked,
    /// Its process has ended: the lock is free.
    Unlocked,
}

/// A lease record: the generation it names, and which of its reader's
/// leases it is (each lease is a new file).
type Lease = (u8, u8);

/// What a lease scan has listed so far.
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
enum Listing {
    /// Neither listing taken yet.
    None,
    /// Lease records listed, by reader.
    Leases(BTreeMap<usize, Lease>),
    /// Owner files listed: the readers whose owner file was present and
    /// locked.
    Owners(BTreeSet<usize>),
}

/// Which lease scan of a reclamation is running.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
enum Scan {
    /// The first, before the horizon is computed.
    First,
    /// The second, after the horizon is written, with that horizon.
    Second(u8),
}

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
enum Writer {
    Idle,
    /// The new segment is in `segments/`; the manifest is not written yet.
    Publishing {
        segment: u8,
    },
    /// Partway through a lease scan.
    Scanning {
        scan: Scan,
        listing: Listing,
    },
    /// The horizon is computed and not written yet.
    HorizonComputed {
        horizon: u8,
    },
    /// Leases checked again; the files are not removed yet. Holds the
    /// leases the second scan judged dead, removed after the files.
    Cleared {
        horizon: u8,
        dead: BTreeMap<usize, Lease>,
    },
}

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
enum Reader {
    /// The process has not created its owner file yet.
    NoOwner,
    Start {
        tries: u8,
    },
    /// Read the newest manifest: its generation and the segments it names.
    ManifestRead {
        tries: u8,
        generation: u8,
        names: BTreeSet<u8>,
    },
    /// The lease record is written.
    Leased {
        tries: u8,
        generation: u8,
        names: BTreeSet<u8>,
    },
    /// (`LeaseAfterCheck` only) the horizon was checked; no lease yet.
    Checked {
        generation: u8,
        names: BTreeSet<u8>,
    },
    /// Passed the horizon check; reading the named segments.
    Reading {
        generation: u8,
        names: BTreeSet<u8>,
    },
    Done,
    Crashed,
}

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
struct State {
    segments: BTreeSet<u8>,
    manifests: BTreeMap<u8, BTreeSet<u8>>,
    newest: u8,
    horizon: u8,
    next_segment: u8,
    writer: Writer,
    publications_left: u8,
    reclamations_left: u8,
    readers: Vec<Reader>,
    owners: Vec<OwnerFile>,
    leases: Vec<Option<Lease>>,
    /// How many leases each reader has taken, to name the next one.
    taken: Vec<u8>,
}

impl State {
    fn initial(readers: usize) -> Self {
        Self {
            segments: BTreeSet::from([1]),
            manifests: BTreeMap::from([(1, BTreeSet::from([1]))]),
            newest: 1,
            horizon: 0,
            next_segment: 2,
            writer: Writer::Idle,
            publications_left: 2,
            reclamations_left: 3,
            readers: vec![Reader::NoOwner; readers],
            owners: vec![OwnerFile::Absent; readers],
            leases: vec![None; readers],
            taken: vec![0; readers],
        }
    }

    fn list_leases(&self) -> BTreeMap<usize, Lease> {
        self.leases
            .iter()
            .enumerate()
            .filter_map(|(index, lease)| lease.map(|lease| (index, lease)))
            .collect()
    }

    fn list_owners(&self) -> BTreeSet<usize> {
        self.owners
            .iter()
            .enumerate()
            .filter(|(_, owner)| **owner == OwnerFile::Locked)
            .map(|(index, _)| index)
            .collect()
    }

    /// Removes the lease records in `dead` that are still the same files.
    fn remove_leases(&mut self, dead: &BTreeMap<usize, Lease>) {
        for (index, lease) in dead {
            if self.leases[*index] == Some(*lease) {
                self.leases[*index] = None;
            }
        }
    }

    fn take_lease(&mut self, index: usize, generation: u8) {
        self.leases[index] = Some((generation, self.taken[index]));
        self.taken[index] += 1;
    }
}

/// Splits listed leases into live ones and dead ones by the owners listed.
fn judge(
    leases: &BTreeMap<usize, Lease>,
    owners: &BTreeSet<usize>,
) -> (Vec<u8>, BTreeMap<usize, Lease>) {
    let mut live = Vec::new();
    let mut dead = BTreeMap::new();
    for (index, lease) in leases {
        if owners.contains(index) {
            live.push(lease.0);
        } else {
            dead.insert(*index, *lease);
        }
    }
    (live, dead)
}

/// The writer's possible steps from `state`.
fn writer_steps(state: &State, rule: Rule, next: &mut Vec<State>) {
    let leases_first = rule != Rule::OwnersFirst;
    match &state.writer {
        Writer::Idle => {
            if state.publications_left > 0 {
                let mut s = state.clone();
                s.publications_left -= 1;
                s.segments.insert(s.next_segment);
                s.writer = Writer::Publishing {
                    segment: s.next_segment,
                };
                s.next_segment += 1;
                next.push(s);
            }
            if state.reclamations_left > 0 {
                let mut s = state.clone();
                s.reclamations_left -= 1;
                s.writer = Writer::Scanning {
                    scan: Scan::First,
                    listing: Listing::None,
                };
                next.push(s);
            }
        }
        Writer::Publishing { segment } => {
            // Each publication replaces every segment, so a reclamation can
            // remove all of the previous generation's files.
            let mut s = state.clone();
            s.newest += 1;
            s.manifests.insert(s.newest, BTreeSet::from([*segment]));
            s.writer = Writer::Idle;
            next.push(s);
        }
        Writer::Scanning {
            scan,
            listing: Listing::None,
        } => {
            let mut s = state.clone();
            let listing = if leases_first {
                Listing::Leases(state.list_leases())
            } else {
                Listing::Owners(state.list_owners())
            };
            s.writer = Writer::Scanning {
                scan: *scan,
                listing,
            };
            next.push(s);
        }
        Writer::Scanning { scan, listing } => {
            let (leases, owners) = match listing {
                Listing::Leases(leases) => (leases.clone(), state.list_owners()),
                Listing::Owners(owners) => (state.list_leases(), owners.clone()),
                Listing::None => unreachable!("matched above"),
            };
            let (live, dead) = judge(&leases, &owners);
            let lowest = live.iter().copied().min();
            let mut s = state.clone();
            match scan {
                Scan::First => {
                    // Ended processes' files go first.
                    s.remove_leases(&dead);
                    let horizon = match rule {
                        Rule::NoNewestBound => lowest.unwrap_or(u8::MAX),
                        _ => lowest.map_or(state.newest, |lowest| lowest.min(state.newest)),
                    };
                    s.writer = Writer::HorizonComputed { horizon };
                }
                Scan::Second(horizon) => {
                    s.writer = if lowest.is_some_and(|lowest| lowest < *horizon) {
                        Writer::Idle
                    } else {
                        Writer::Cleared {
                            horizon: *horizon,
                            dead,
                        }
                    };
                }
            }
            next.push(s);
        }
        Writer::HorizonComputed { horizon } => {
            let mut s = state.clone();
            if *horizon < state.horizon {
                s.writer = Writer::Idle;
            } else {
                s.horizon = *horizon;
                s.writer = if rule == Rule::NoRescan {
                    Writer::Cleared {
                        horizon: *horizon,
                        dead: BTreeMap::new(),
                    }
                } else {
                    Writer::Scanning {
                        scan: Scan::Second(*horizon),
                        listing: Listing::None,
                    }
                };
            }
            next.push(s);
        }
        Writer::Cleared { horizon, dead } => {
            let mut s = state.clone();
            let keep = state
                .manifests
                .range(*horizon..)
                .flat_map(|(_, names)| names.iter().copied())
                .collect::<BTreeSet<_>>();
            s.segments.retain(|segment| keep.contains(segment));
            s.manifests.retain(|generation, _| generation >= horizon);
            s.remove_leases(dead);
            for (index, owner) in s.owners.iter_mut().enumerate() {
                // A dead owner file goes after its leases.
                if *owner == OwnerFile::Unlocked && s.leases[index].is_none() {
                    *owner = OwnerFile::Absent;
                }
            }
            s.writer = Writer::Idle;
            next.push(s);
        }
    }
}

/// Every state reachable in one step, or the violation a step runs into.
fn successors(state: &State, rule: Rule) -> Result<Vec<State>, String> {
    let mut next = Vec::new();
    writer_steps(state, rule, &mut next);

    for (index, reader) in state.readers.iter().enumerate() {
        let mut step = |reader: Reader, change: &dyn Fn(&mut State)| {
            let mut s = state.clone();
            s.readers[index] = reader;
            change(&mut s);
            next.push(s);
        };
        match reader {
            Reader::Done | Reader::Crashed => continue,
            Reader::NoOwner => step(Reader::Start { tries: 2 }, &|s| {
                s.owners[index] = OwnerFile::Locked;
            }),
            Reader::Start { tries } => {
                if *tries == 0 {
                    step(Reader::Done, &|_| {});
                } else {
                    // The newest manifest is never removed (checked for every
                    // state in `check`), so it can be read.
                    let names = state.manifests[&state.newest].clone();
                    step(
                        Reader::ManifestRead {
                            tries: tries - 1,
                            generation: state.newest,
                            names,
                        },
                        &|_| {},
                    );
                }
            }
            Reader::ManifestRead {
                tries,
                generation,
                names,
            } => {
                if rule == Rule::LeaseAfterCheck {
                    if state.horizon > *generation {
                        step(Reader::Start { tries: *tries }, &|_| {});
                    } else {
                        step(
                            Reader::Checked {
                                generation: *generation,
                                names: names.clone(),
                            },
                            &|_| {},
                        );
                    }
                } else {
                    let generation = *generation;
                    step(
                        Reader::Leased {
                            tries: *tries,
                            generation,
                            names: names.clone(),
                        },
                        &|s| s.take_lease(index, generation),
                    );
                }
            }
            Reader::Checked { generation, names } => {
                let generation = *generation;
                step(
                    Reader::Reading {
                        generation,
                        names: names.clone(),
                    },
                    &|s| s.take_lease(index, generation),
                );
            }
            Reader::Leased {
                tries,
                generation,
                names,
            } => {
                if rule != Rule::NoReaderCheck && state.horizon > *generation {
                    // Dropping the snapshot removes its lease record.
                    step(Reader::Start { tries: *tries }, &|s| {
                        s.leases[index] = None;
                    });
                } else {
                    step(
                        Reader::Reading {
                            generation: *generation,
                            names: names.clone(),
                        },
                        &|_| {},
                    );
                }
            }
            Reader::Reading { generation, names } => {
                if let Some(missing) = names.iter().find(|name| !state.segments.contains(name)) {
                    return Err(format!(
                        "reader {index} read segment {missing} of generation {generation} after it was removed"
                    ));
                }
                step(Reader::Done, &|s| s.leases[index] = None);
            }
        }
        if !matches!(reader, Reader::Done | Reader::Crashed) {
            // The process ends: its owner file's lock is released, and its
            // lease record stays until a reclaimer removes it.
            let mut s = state.clone();
            s.readers[index] = Reader::Crashed;
            if s.owners[index] == OwnerFile::Locked {
                s.owners[index] = OwnerFile::Unlocked;
            }
            next.push(s);
        }
    }
    Ok(next)
}

/// Explores every reachable state. Returns the number of states, or the
/// first violation.
fn check(rule: Rule, readers: usize) -> Result<usize, String> {
    let initial = State::initial(readers);
    let mut seen = HashSet::from([initial.clone()]);
    let mut stack = vec![initial];
    while let Some(state) = stack.pop() {
        let Some(names) = state.manifests.get(&state.newest) else {
            return Err(format!(
                "the newest manifest, {}, was removed",
                state.newest
            ));
        };
        if let Some(missing) = names.iter().find(|name| !state.segments.contains(name)) {
            return Err(format!(
                "segment {missing} of the newest generation {} was removed",
                state.newest
            ));
        }
        for next in successors(&state, rule)? {
            if seen.insert(next.clone()) {
                stack.push(next);
            }
        }
    }
    Ok(seen.len())
}

#[test]
fn the_protocol_holds_in_every_interleaving() {
    for readers in 1..=2 {
        let states = check(Rule::All, readers).unwrap();
        println!("{readers} readers: {states} states");
        assert!(
            states > 1_000,
            "{readers} readers: only {states} states explored"
        );
    }
}

#[test]
fn removing_any_rule_breaks_the_protocol() {
    for rule in [
        Rule::NoNewestBound,
        Rule::NoRescan,
        Rule::NoReaderCheck,
        Rule::LeaseAfterCheck,
        Rule::OwnersFirst,
    ] {
        let found = check(rule, 2);
        println!("{rule:?}: {found:?}");
        assert!(
            found.is_err(),
            "{rule:?} found no violation in {found:?} states"
        );
    }
}