verify_and_heal copied the database aside before looking at the stop, so a verification or an open that was stopped right after finding a drift left a .mismatch copy behind. The look now comes first (heal_drift), before anything is done: a stopped heal leaves the database and the folder exactly as they were. The rebuild and its verification stay unstoppable once begun, since stopped half way the live ledger would be part replayed. INVARIANT 10's close bound said that heal was one pass over the journal and named no term for it. It is about six (three in the rebuild, three in the verification) plus the copy and one hash of each stored file, so it is now the fourth named exception to the bound, with that cost, and 'an open stops anywhere' says where it cannot. Test: a stop before the heal leaves no copy and no rebuild; the same drift heals when not stopped. It fails with the look moved back after the copy. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
85 lines
3.3 KiB
Rust
85 lines
3.3 KiB
Rust
//! A stop request the long loops over a book's journal and files look at
|
|
//! (a backup's drill or proof, ledger-store `backup`): a step that has to
|
|
//! end when the books are let go (a close) can't wait for a replay of a
|
|
//! journal of any length to come to its end.
|
|
//!
|
|
//! ```text
|
|
//! GOAL: a verification over a book's journal or files that was asked to
|
|
//! stop stops promptly, at its next step, and leaves nothing of its
|
|
//! own half-made, however large the book.
|
|
//! SOURCE: the books server's close (ledger-mcp local.rs INVARIANT 10 and
|
|
//! 13: a close is answered only when nothing of its backups still
|
|
//! runs, and is bounded).
|
|
//! INVARIANTS:
|
|
//! 1. A request is scoped to the thread that made it (`scope`), for as
|
|
//! long as the scope lives; code on other threads, and code outside a
|
|
//! scope, never sees one. Nothing is shared, so no other work of the
|
|
//! process can be stopped by it.
|
|
//! 2. `check` is looked at once per journal line (verifying, replaying,
|
|
//! applying to the projection), once per stored file hashed, and at
|
|
//! the boundaries between a verification's phases: the longest it
|
|
//! goes without looking is one line or one file.
|
|
//! 3. A stop is an error (`StoreError::Cancelled`), never a partial
|
|
//! success: what a stopped verification had made is the caller's to
|
|
//! remove (the books it was opened on are a scratch folder's).
|
|
//! 4. A step that can't be left half-made is shielded (`shielded`): the
|
|
//! stop is looked at before it begins, never inside it. The database's
|
|
//! rebuild from the journal (`verify_and_heal`, healing a drift) is
|
|
//! one: stopped part way it would leave the live ledger part replayed.
|
|
//! The look comes before ANYTHING is done for the heal (the database
|
|
//! copied aside included), so a stopped heal leaves nothing behind.
|
|
//! ```
|
|
|
|
use crate::error::StoreError;
|
|
use std::cell::RefCell;
|
|
use std::sync::Arc;
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
thread_local! {
|
|
static FLAG: RefCell<Option<Arc<AtomicBool>>> = const { RefCell::new(None) };
|
|
}
|
|
|
|
/// A stop request in force on this thread, until dropped.
|
|
pub struct Scope(Option<Arc<AtomicBool>>);
|
|
|
|
impl Drop for Scope {
|
|
fn drop(&mut self) {
|
|
FLAG.with(|f| *f.borrow_mut() = self.0.take());
|
|
}
|
|
}
|
|
|
|
/// Look for `flag` (INVARIANT 1) on this thread, until the returned scope
|
|
/// is dropped (the request in force before it comes back).
|
|
#[must_use]
|
|
pub fn scope(flag: Arc<AtomicBool>) -> Scope {
|
|
Scope(FLAG.with(|f| f.borrow_mut().replace(flag)))
|
|
}
|
|
|
|
/// Run `f` with no stop in force on this thread (the request in force
|
|
/// before comes back after): for the one step that can't be left half-made,
|
|
/// such as rebuilding the database from the journal, which would leave the
|
|
/// live ledger part replayed.
|
|
pub fn shielded<R>(f: impl FnOnce() -> R) -> R {
|
|
let outer = FLAG.with(|flag| flag.borrow_mut().take());
|
|
let _restore = Scope(outer);
|
|
f()
|
|
}
|
|
|
|
/// Whether a stop was asked for.
|
|
pub fn requested() -> bool {
|
|
FLAG.with(|f| {
|
|
f.borrow()
|
|
.as_ref()
|
|
.is_some_and(|c| c.load(Ordering::SeqCst))
|
|
})
|
|
}
|
|
|
|
/// Ok, or `StoreError::Cancelled` when a stop was asked for (INVARIANT 2).
|
|
pub fn check() -> Result<(), StoreError> {
|
|
if requested() {
|
|
Err(StoreError::Cancelled)
|
|
} else {
|
|
Ok(())
|
|
}
|
|
}
|