diff --git a/.lww b/.lww deleted file mode 100644 index 28fc1927d4..0000000000 --- a/.lww +++ /dev/null @@ -1,4 +0,0 @@ -%include .gitignore -# The Git index is regenerated by every git command; keep the newer side -# whole instead of conflicting on the binary file. -/.git/index diff --git a/api/crates/layerfs-api/src/store/repair_mount_holders.rs b/api/crates/layerfs-api/src/store/repair_mount_holders.rs index daae5f618b..b723968f1c 100644 --- a/api/crates/layerfs-api/src/store/repair_mount_holders.rs +++ b/api/crates/layerfs-api/src/store/repair_mount_holders.rs @@ -1,66 +1,50 @@ //! `store.repair_mount_holders`: explicit first-class repair of one Branch's //! mount admissions, on the machine whose worker holds a SQLite Store and in -//! this process for a PostgreSQL Store. Preview changes no live admission, -//! answers the typed holder view and writes its own retained done row -//! (`action=preview`, no operation); settle/release name one op; `audit` reads -//! the bounded retained done rows of that Branch. Release retains the row as -//! the durable operator audit and records -//! the operator label; every call is also in the home's `trace/calls.jsonl` -//! call log. +//! this process for a PostgreSQL Store. `list` and `preview` write nothing: +//! one page of holders, each with its row `state` and the `liveness` +//! admission would give it now; `settle` refuses a holder still `live` +//! unless forced; `release` retains a settled row as the durable done audit +//! and records the operator label; `audit` pages the Branch's retained rows. +//! Every call is also in the home's `trace/calls.jsonl` call log. use crate::calls::table::{ a, call, opt, Call, - Kind::{Bool, Int, Name, Text}, + Kind::{Bool, Int, Name, Project, Text}, }; -use crate::calls::Context; +use crate::calls::{Context, Paging}; use layerfs_bridge::control::SdkValue; -use layerfs_home::names::StoreName; -use layerfs_home::outcome::{refused, Outcome}; +use layerfs_control::store::RepairRequest; +use layerfs_home::outcome::Outcome; pub(crate) const CALL: Call = call( "store", "repair_mount_holders", - &[ - a("name", Name), - a("project", Name), - a("branch", Name), - a("action", Name), - ], + &[a("project", Project), a("branch", Name), a("action", Name)], &[ opt("op", Text), opt("legacy", Bool), + opt("force", Bool), opt("operator", Text), - opt("page", Int), opt("limit", Int), + opt("cursor", Text).redact(), ], "list, preview, settle, release or audit mount holders (operator)", run, -); +) +.paged(Paging::Cursor); fn run(context: &mut Context<'_>, args: &SdkValue) -> Outcome { - let name = StoreName::parse(args.get_text("name").unwrap_or(""))?; - let project = args.get_text("project").unwrap_or("").to_owned(); - let branch = args.get_text("branch").unwrap_or("").to_owned(); - let action = args.get_text("action").unwrap_or("preview").to_owned(); - let op = match args.get_text("op") { - None | Some("") => None, - Some(text) => Some( - layerfs_bridge::sdk_values::unhex(text) - .and_then(|bytes| bytes.try_into().ok()) - .ok_or_else(|| refused("InvalidInput", "op must be 16 bytes of hex"))?, - ), + let text = |name: &str| args.get_text(name).map(str::to_owned); + let request = RepairRequest { + project: text("project").unwrap_or_default(), + branch: text("branch").unwrap_or_default(), + action: text("action").unwrap_or_default(), + op: text("op"), + legacy: matches!(args.get("legacy"), Some(SdkValue::Bool(true))), + force: matches!(args.get("force"), Some(SdkValue::Bool(true))), + operator: text("operator"), + limit: args.get_int("limit"), + cursor: text("cursor"), }; - let legacy = matches!(args.get("legacy"), Some(SdkValue::Bool(true))); - let operator = args.get_text("operator").unwrap_or(""); - let page = args - .get_int("page") - .and_then(|page| u32::try_from(page).ok()) - .unwrap_or(0); - let limit = args - .get_int("limit") - .and_then(|limit| u32::try_from(limit).ok()) - .unwrap_or(100); let (home, _) = context.parts(); - layerfs_control::store::repair( - home, &name, &project, &branch, &action, op, legacy, operator, page, limit, - ) + layerfs_control::store::repair(home, &request) } diff --git a/api/crates/layerfs-api/tests/gc_calls.rs b/api/crates/layerfs-api/tests/gc_calls.rs index d697fd8dba..b4ebb56384 100644 --- a/api/crates/layerfs-api/tests/gc_calls.rs +++ b/api/crates/layerfs-api/tests/gc_calls.rs @@ -7,7 +7,7 @@ use layerfs_history::records::{GcIntent, GcIntentKind, GcScopeKind}; use layerfs_history::PlaceAuthority; use layerfs_store::open_args::OpenArgs; use layerfs_store::record::StoreRecord; -use layerfs_store::service::{branch_named, with_service}; +use layerfs_store::service::with_service; use layerfs_store_service::history::lookup; use std::os::unix::fs::PermissionsExt; use std::path::{Path, PathBuf}; @@ -810,16 +810,8 @@ fn a_bare_project_name_is_refused_by_every_project_call() { std::fs::remove_dir_all(home).unwrap(); } -fn future_ms() -> i64 { - let now = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .expect("the clock is after the epoch") - .as_millis(); - i64::try_from(now).expect("the clock fits an i64") + 60_000 -} - /// `store.repair_mount_holders` is the explicit two-step operator repair: -/// preview answers the typed holder view and changes no intent; release needs +/// preview answers the typed holder view and writes nothing; release needs /// a preceding settle; `settle_legacy` marks only rows with no owner or place /// evidence; release retains the row as the durable done audit, and the audit /// action reads it back in bounded pages. @@ -878,8 +870,7 @@ fn store_repair_mount_holders_is_an_explicit_two_step_operator_call() { let legacy_op = "31".repeat(16); let repair = |action: &str, op: Option<&str>, legacy: bool| { let mut fields = vec![ - ("name", t("shop")), - ("project", t("api")), + ("project", t("shop/api")), ("branch", t("main")), ("action", t(action)), ]; @@ -900,13 +891,34 @@ fn store_repair_mount_holders_is_an_explicit_two_step_operator_call() { "store.repair_mount_holders", &repair("preview", None, false), ); - let Some(SdkValue::List(holders)) = preview.get("holders") else { + let Some(SdkValue::List(holders)) = preview.get("items") else { panic!("preview holders: {preview:?}"); }; assert_eq!(holders.len(), 2, "{preview:?}"); assert_eq!(preview.get_text("action"), Some("preview")); - assert_eq!(preview.get("op"), Some(&SdkValue::Null)); - assert_eq!(preview.get_text("audit"), Some("gc_intent")); + assert_eq!(preview.get("next_cursor"), Some(&SdkValue::Null)); + // Preview is read-only: the Branch's intents are as registered, and no + // retained row was written. + let intents = with_service(&open, |service| { + (service.store.history()) + .gc_intents(GcScopeKind::Branch, branch.as_slice()) + .map_err(lookup::history) + }) + .unwrap(); + assert_eq!(intents.len(), 2, "{intents:?}"); + assert!( + intents.iter().all(|intent| !intent.uncertain), + "{intents:?}" + ); + let audit = ok( + &home, + "store.repair_mount_holders", + &repair("audit", None, false), + ); + assert!( + matches!(audit.get("items"), Some(SdkValue::List(rows)) if rows.is_empty()), + "preview wrote a row: {audit:?}" + ); let live_row = holders .iter() .find(|row| row.get_text("op") == Some(live_op.as_str())) @@ -918,6 +930,10 @@ fn store_repair_mount_holders_is_an_explicit_two_step_operator_call() { Some("generation:1") ); assert_eq!(live_row.get("legacy"), Some(&SdkValue::Bool(false))); + // nothing proves its owner gone: the row still looks live + assert_eq!(live_row.get_text("liveness"), Some("live")); + assert_eq!(live_row.get("safe"), Some(&SdkValue::Bool(false))); + assert!(live_row.get("reason").is_none(), "state words, no prose"); let legacy_row = holders .iter() .find(|row| row.get_text("op") == Some(legacy_op.as_str())) @@ -938,17 +954,23 @@ fn store_repair_mount_holders_is_an_explicit_two_step_operator_call() { ); assert_eq!(code, "InvalidInput", "{message}"); - ok( + // a holder that still looks live is settled only when forced + let (code, message) = refused( &home, "store.repair_mount_holders", &repair("settle", Some(&live_op), false), ); + assert_eq!(code, "InUse", "{message}"); + let mut forced = repair("settle", Some(&live_op), false); + forced.push(("force", SdkValue::Bool(true))); + let settled = ok(&home, "store.repair_mount_holders", &forced); + assert_eq!(settled.get_text("state"), Some("uncertain"), "{settled:?}"); let preview = ok( &home, "store.repair_mount_holders", &repair("preview", None, false), ); - let Some(SdkValue::List(holders)) = preview.get("holders") else { + let Some(SdkValue::List(holders)) = preview.get("items") else { panic!("preview holders: {preview:?}"); }; let settled_row = holders @@ -979,14 +1001,12 @@ fn store_repair_mount_holders_is_an_explicit_two_step_operator_call() { &repair("preview", None, false), ); assert!( - matches!(preview.get("holders"), Some(SdkValue::List(rows)) if rows.is_empty()), + matches!(preview.get("items"), Some(SdkValue::List(rows)) if rows.is_empty()), "{preview:?}" ); // Both releases retained their row as the durable operator audit, and - // every preview retained its own done row: the release rows keep the op, - // the preview rows are the operator action with no op. Admission still - // filters every done row. + // the three previews wrote none. Admission still filters every done row. let audit = ok( &home, "store.repair_mount_holders", @@ -1003,17 +1023,7 @@ fn store_repair_mount_holders_is_an_explicit_two_step_operator_call() { }) .collect(); assert_eq!(released.len(), 2, "{audit:?}"); - let previews: Vec<_> = rows - .iter() - .filter(|row| row.get_text("evidence") == Some("action=preview;operator=alice")) - .collect(); - assert_eq!(previews.len(), 3, "{audit:?}"); - for row in &previews { - assert_eq!(row.get("phase"), Some(&SdkValue::Null)); - assert_eq!(row.get_text("op").map(str::len), Some(32)); - assert_eq!(row.get_text("kind"), Some("mount")); - assert_eq!(row.get_text("scope_kind"), Some("branch")); - } + assert_eq!(rows.len(), 2, "only the releases are retained: {audit:?}"); let released = released .iter() .find(|row| row.get_text("op") == Some(live_op.as_str())) @@ -1028,16 +1038,38 @@ fn store_repair_mount_holders_is_an_explicit_two_step_operator_call() { evidence.contains(&format!("action=release;operator=alice;op={live_op}")), "{evidence}" ); - assert!(matches!(audit.get("has_more"), Some(SdkValue::Bool(false)))); + assert_eq!(audit.get("next_cursor"), Some(&SdkValue::Null), "{audit:?}"); - // Paging is bounded by the call, not by the caller. - let mut paged = repair("audit", None, false); - paged.push(("limit", SdkValue::Int(i64::from(u32::MAX)))); - let paged = ok(&home, "store.repair_mount_holders", &paged); - assert!( - matches!(paged.get("limit"), Some(SdkValue::Int(limit)) if *limit == i64::from(layerfs_history::MAXIMUM_AUDIT_PAGE)), - "{paged:?}" + // Paging is by `limit` and `cursor`: one row a page, the cursor carries + // the project, Branch and action, and another action's cursor is refused. + let mut first = repair("audit", None, false); + first.push(("limit", SdkValue::Int(1))); + let first = ok(&home, "store.repair_mount_holders", &first); + let Some(cursor) = first.get_text("next_cursor").map(str::to_owned) else { + panic!("a first page of one names the next: {first:?}"); + }; + let mut second = repair("audit", None, false); + second.push(("limit", SdkValue::Int(1))); + second.push(("cursor", t(&cursor))); + let second = ok(&home, "store.repair_mount_holders", &second); + let op_of = |page: &SdkValue| match page.get("items") { + Some(SdkValue::List(rows)) if rows.len() == 1 => rows[0].get_text("op").map(str::to_owned), + other => panic!("one row: {other:?}"), + }; + assert_ne!(op_of(&first), op_of(&second), "the second page moves on"); + assert_eq!( + second.get("next_cursor"), + Some(&SdkValue::Null), + "{second:?}" ); + let mut crossed = repair("preview", None, false); + crossed.push(("cursor", t(&cursor))); + let (code, message) = refused(&home, "store.repair_mount_holders", &crossed); + assert_eq!(code, "InvalidInput", "{message}"); + let mut too_many = repair("audit", None, false); + too_many.push(("limit", SdkValue::Int(i64::from(u32::MAX)))); + let (code, message) = refused(&home, "store.repair_mount_holders", &too_many); + assert_eq!(code, "InvalidInput", "{message}"); // A refused call changes nothing. let (code, message) = refused( @@ -1063,23 +1095,3 @@ fn store_repair_mount_holders_is_an_explicit_two_step_operator_call() { std::fs::remove_dir_all(home).unwrap(); let _ = std::fs::remove_dir_all(folder); } - -/// The bounded read of the Store's retained done rows, as the audit action -/// answers it. -fn audit_items(home: &Path) -> Vec { - let audit = ok( - home, - "store.repair_mount_holders", - &[ - ("name", t("shop")), - ("project", t("api")), - ("branch", t("main")), - ("action", t("audit")), - ], - ); - match audit.get("items") { - Some(SdkValue::List(items)) => items.clone(), - other => panic!("audit items: {other:?}"), - } -} - diff --git a/api/crates/layerfs-api/tests/machine_no_start.rs b/api/crates/layerfs-api/tests/machine_no_start.rs index 0de8ec970d..bbed32ab0a 100644 --- a/api/crates/layerfs-api/tests/machine_no_start.rs +++ b/api/crates/layerfs-api/tests/machine_no_start.rs @@ -101,6 +101,7 @@ fn every_read(home: &Path) { ("store.get", vec![("store", "shop")]), ("store.get", vec![("store", "shop"), ("with", "live")]), ("project.list", vec![]), + ("project.get", vec![("project", "shop/api")]), ("sandbox.list", vec![]), ("backup.list", vec![]), ] { diff --git a/api/crates/layerfs-api/tests/project_export.rs b/api/crates/layerfs-api/tests/project_export.rs index f5094afe3d..bb87481e72 100644 --- a/api/crates/layerfs-api/tests/project_export.rs +++ b/api/crates/layerfs-api/tests/project_export.rs @@ -483,8 +483,8 @@ fn schema_version_1_store_is_refused_before_prepare_and_keeps_destination() { let store_file = paths::store_file(&home, "shop"); let current = read_user_version(&store_file); assert!( - (39..=47).contains(¤t), - "expected a schema version 3 Store, read user_version {current}" + (36..=44).contains(¤t), + "expected a schema version 2 Store, read user_version {current}" ); write_user_version(&store_file, 22); assert_eq!(read_user_version(&store_file), 22); diff --git a/api/crates/layerfs-cli/tests/gc.rs b/api/crates/layerfs-cli/tests/gc.rs index b533960fef..bd803e89f7 100644 --- a/api/crates/layerfs-cli/tests/gc.rs +++ b/api/crates/layerfs-cli/tests/gc.rs @@ -36,7 +36,7 @@ const CALLS: &[(&[&str], &str, &str)] = &[ ), ( &["store", "repair-mount-holders"], - "layerfs store repair-mount-holders ", + "layerfs store repair-mount-holders ", "list, preview, settle, release or audit mount holders (operator)", ), ]; @@ -112,12 +112,11 @@ fn wrong_arity_and_options_are_usage_errors() { &["store", "compact", "shop", "extra"], &["store", "compact", "shop", "--force"], &["store", "repair-mount-holders"], - &["store", "repair-mount-holders", "shop", "app", "main"], + &["store", "repair-mount-holders", "shop/app", "main"], &[ "store", "repair-mount-holders", - "shop", - "app", + "shop/app", "main", "preview", "extra", @@ -125,11 +124,10 @@ fn wrong_arity_and_options_are_usage_errors() { &[ "store", "repair-mount-holders", - "shop", - "app", + "shop/app", "main", "preview", - "--force", + "--prune", ], ]; for words in wrong { @@ -154,8 +152,7 @@ fn well_formed_lines_reach_the_library() { &[ "store", "repair-mount-holders", - "shop", - "app", + "shop/app", "main", "preview", ], diff --git a/control/crates/layerfs-control/src/doctor/mod.rs b/control/crates/layerfs-control/src/doctor/mod.rs index 87ba5a120a..2af46fce46 100644 --- a/control/crates/layerfs-control/src/doctor/mod.rs +++ b/control/crates/layerfs-control/src/doctor/mod.rs @@ -9,8 +9,8 @@ //! `layerfs_sandbox::sweep` and the sweeps of image copies and backups). mod capacity; mod checks; -mod records; mod mount_holders; +mod records; mod scan; mod check_report; diff --git a/control/crates/layerfs-control/src/doctor/mount_holders.rs b/control/crates/layerfs-control/src/doctor/mount_holders.rs index a82c996476..ffd7369a88 100644 --- a/control/crates/layerfs-control/src/doctor/mount_holders.rs +++ b/control/crates/layerfs-control/src/doctor/mount_holders.rs @@ -6,59 +6,8 @@ use layerfs_home::{home::paths, names::SandboxName, outcome::refused}; use layerfs_sandbox::record::{SandboxRecord, WorkspaceRecord}; use std::path::Path; -pub(super) const STORE: &str = "store.mount_holders"; -pub(super) const PROJECT: &str = "project.mount_holders"; pub(super) const AUDIT: &str = "store.gc_audit"; -pub(super) fn result( - home: &Path, - store: &str, - shared: bool, - check: &str, - result: Result, - checks: &mut Checks, -) { - match result { - Ok(rows) => report(home, store, shared, check, Some(&rows), checks), - Err(error) => checks.unknown(check, error.message), - } -} - -/// The local SQLite path: read the rows and report them against this home. -pub(super) fn local( - home: &Path, - store: &str, - name: &str, - identity: &layerfs_store::identity::Identity, - checks: &mut Checks, -) { - let path = paths::store_file(home, name); - result( - home, - store, - false, - STORE, - layerfs_store::diagnostic::mount_holders(&path, identity), - checks, - ); -} - -/// The local SQLite path: one bounded page of the retained `done` -/// operator-audit rows, read on a diagnostic open. The doctor only reports -/// them; it never releases or settles one. -pub(super) fn audit( - home: &Path, - name: &str, - identity: &layerfs_store::identity::Identity, - checks: &mut Checks, -) { - let path = paths::store_file(home, name); - audit_result( - layerfs_store::diagnostic::done_audit(&path, identity, 0, 16), - checks, - ); -} - /// The worker's forwarded `store.gc_audit` event, or the local result: a /// read that answers is evidence, never a warning; a read that fails is /// `unknown`, so it cannot turn into a release. @@ -153,4 +102,3 @@ pub(super) fn report( ); } } - diff --git a/control/crates/layerfs-control/src/doctor/store_target.rs b/control/crates/layerfs-control/src/doctor/store_target.rs index c4270c9cec..d9d495a222 100644 --- a/control/crates/layerfs-control/src/doctor/store_target.rs +++ b/control/crates/layerfs-control/src/doctor/store_target.rs @@ -316,27 +316,16 @@ fn sqlite(probe: &Probe<'_>, record: &StoreRecord, identity: &Identity, checks: // the live mount admissions and one bounded page of the retained done // audit: reported as evidence, never released or settled by a doctor match found.get("mount_holders") { - Some(rows) => super::mount_holders::report( - probe.home, - name, - false, - MOUNT_HOLDERS, - Some(rows), - checks, - ), - None => checks.unknown( - MOUNT_HOLDERS, - "the diagnostic did not report mount holders", - ), + Some(rows) => { + super::mount_holders::report(probe.home, name, false, MOUNT_HOLDERS, Some(rows), checks) + } + None => checks.unknown(MOUNT_HOLDERS, "the diagnostic did not report mount holders"), } match found.get("gc_audit") { Some(page) if page.get("items").is_some() => { super::mount_holders::audit_result(Ok(page.clone()), checks) } - _ => checks.unknown( - GC_AUDIT, - "the diagnostic did not report the done audit", - ), + _ => checks.unknown(GC_AUDIT, "the diagnostic did not report the done audit"), } if heads { backups(record, text("heads"), text("heads_error"), checks); diff --git a/control/crates/layerfs-control/src/store.rs b/control/crates/layerfs-control/src/store.rs index 7eb2bf5720..395163aafd 100644 --- a/control/crates/layerfs-control/src/store.rs +++ b/control/crates/layerfs-control/src/store.rs @@ -7,7 +7,6 @@ //! reaches it as a Sandbox call would. use crate::inventory; use layerfs_bridge::control::{SdkRefusal, SdkValue}; -use layerfs_bridge::sdk_values::hex; use layerfs_home::events::Events; use layerfs_home::names::{MachineName, StoreName}; use layerfs_home::outcome::shell_word; @@ -69,62 +68,118 @@ pub fn compact(home: &Path, events: &mut Events<'_>, name: &StoreName) -> Outcom }) } +/// `store.repair_mount_holders(project, branch, action, { op, legacy, +/// force, operator, limit, cursor })` as the call surface takes it. +#[derive(Clone, Debug, Default)] +pub struct RepairRequest { + /// The project, `/`. + pub project: String, + /// The Branch whose mount holders it repairs. + pub branch: String, + /// `list`, `preview`, `settle`, `release`, `settle_legacy` or `audit`. + pub action: String, + /// The holder `settle` and `release` act on, as hex. + pub op: Option, + /// `settle` only a row without owner or place evidence. + pub legacy: bool, + /// `settle` a holder that still looks live. + pub force: bool, + /// The operator label a release records. + pub operator: Option, + /// The rows of one page (`list`, `preview`, `audit`). + pub limit: Option, + /// The `next_cursor` of the page before. + pub cursor: Option, +} + +/// The call a repair cursor belongs to. +const REPAIR: &str = "store.repair_mount_holders"; + /// `store.repair_mount_holders`: first-class explicit operator repair of -/// one Branch's mount intents. Preview lists them and writes its own retained -/// done row; settle/release act on one explicit op; release retains the row as -/// the durable done audit; `audit` reads the retained rows of that Branch. -/// Nothing is automatic. A SQLite -/// Store on another machine is repaired by that machine's worker -/// (`store.repair`), so the live Docker Store is reachable; a PostgreSQL -/// Store runs here, as its calls do. -#[allow(clippy::too_many_arguments)] -pub fn repair( - home: &Path, - name: &StoreName, - project: &str, - branch: &str, - action: &str, - op: Option<[u8; 16]>, - legacy: bool, - operator: &str, - page: u32, - limit: u32, -) -> Outcome { +/// one Branch's mount intents ([`layerfs_store::repair`]). `list` and +/// `preview` page the holders, each with its row `state` and the +/// `liveness` admission would give it now; `settle` refuses a holder still +/// `live` unless forced; `release` retains a settled row as the durable done +/// audit; `audit` pages the Branch's retained rows. The cursor carries the +/// project, Branch and action. Nothing is automatic. A SQLite Store on +/// another machine is repaired by that machine's worker (`store.repair`), +/// sent this home's place view; a PostgreSQL Store runs here, as its calls +/// do. +pub fn repair(home: &Path, request: &RepairRequest) -> Outcome { + let limit = layerfs_home::cursor::limit(REPAIR, request.limit, 100)?; + let filters = [ + ("project", SdkValue::text(&request.project)), + ("branch", SdkValue::text(&request.branch)), + ("action", SdkValue::text(&request.action)), + ]; + let carried = (request.cursor.as_deref()) + .map(|made| layerfs_home::cursor::resume(REPAIR, made, &filters)) + .transpose()?; + let target = layerfs_store::resolve::target(home, &request.project)?; + let sent = layerfs_store::repair::Repair { + project: target.project.clone(), + branch: request.branch.clone(), + action: request.action.clone(), + op: layerfs_store::repair::op_of(request.op.as_deref())?, + legacy: request.legacy, + force: request.force, + operator: request.operator.clone().unwrap_or_default(), + after: carried.as_ref().map(|carried| carried.place().clone()), + limit: limit as u32, + }; // The host holds the Store's lease while it repairs or sends the call, // as `store_call` does: a Store an operation is moving is left. - let (_lease, record) = layerfs_store::lease::claim(home, name.as_str())?; - let store_ref = record.store_ref()?; - let executor = place::plan(home, store_ref)?; - match executor { + let (_lease, record) = layerfs_store::lease::claim(home, &target.store)?; + let answer = match place::plan(home, record.store_ref()?)? { Executor::Local => { - let open = OpenArgs::of_record(home, name.as_str(), &record)?; - layerfs_store::service::repair_mount_holders_at( - &open, project, branch, action, op, legacy, operator, page, limit, - ) + let open = OpenArgs::of_record(home, &target.store, &record)?; + layerfs_store::repair::repair_at(&open, &sent) } Executor::Worker(mut worker) => { let mut fields = vec![ - ("store".to_owned(), SdkValue::text(name.as_str())), + ("store".to_owned(), SdkValue::text(&target.store)), ("store_record".to_owned(), record.to_record().clone()), - ("project".to_owned(), SdkValue::text(project)), - ("branch".to_owned(), SdkValue::text(branch)), - ("action".to_owned(), SdkValue::text(action)), - ( - "op".to_owned(), - op.map_or(SdkValue::Null, |op| SdkValue::text(hex(&op))), - ), - ("legacy".to_owned(), SdkValue::Bool(legacy)), - ("operator".to_owned(), SdkValue::text(operator)), - ("page".to_owned(), SdkValue::Int(i64::from(page))), - ("limit".to_owned(), SdkValue::Int(i64::from(limit))), ]; - if let Some(place) = layerfs_store::place_view::encoded_from_home(home) { - fields.push(("place_view".to_owned(), place)); + fields.extend(sent.to_fields()); + // list, preview and settle judge holders with this home's places + if let Some(view) = layerfs_store::place_view::encoded_from_home(home) { + fields.push(("place_view".to_owned(), view)); } - let request = SdkValue::Map(fields); - worker.call("store.repair", request, &mut |_: SdkValue| {}) + worker.call("store.repair", SdkValue::Map(fields), &mut |_: SdkValue| {}) } - } + }; + let answer = answer.map_err(|refusal| match refusal.code.as_str() { + "InUse" => with_fix( + refusal, + format!( + "layerfs store repair-mount-holders {} {} settle --op {} --force", + shell_word(&request.project), + shell_word(&request.branch), + request.op.as_deref().unwrap_or_default() + ), + ), + _ => refusal, + })?; + Ok(paged(answer, &filters)) +} + +/// A repair answer with its `next` place made the `next_cursor` the caller +/// takes back; an answer that is no page is as the Store side gave it. +fn paged(answer: SdkValue, filters: &[(&str, SdkValue)]) -> SdkValue { + let SdkValue::Map(fields) = answer else { + return answer; + }; + let fields = (fields.into_iter()) + .map(|(name, value)| match (name.as_str(), value) { + ("next", SdkValue::Null) => ("next_cursor".to_owned(), SdkValue::Null), + ("next", place) => ( + "next_cursor".to_owned(), + SdkValue::Text(layerfs_home::cursor::make(REPAIR, place, filters)), + ), + (_, value) => (name, value), + }) + .collect(); + SdkValue::Map(fields) } /// Who uses a Store or a project, read from the Sandbox records. diff --git a/control/crates/layerfs-control/src/worker_server.rs b/control/crates/layerfs-control/src/worker_server.rs index 316a9be68f..a7defbd141 100644 --- a/control/crates/layerfs-control/src/worker_server.rs +++ b/control/crates/layerfs-control/src/worker_server.rs @@ -100,11 +100,12 @@ struct Server { faults: Arc, } -/// One fresh, ordered incarnation for a worker starting now: a newer start -/// compares greater than every younger one across restarts and pid reuse, so -/// admission can tell an old placement from the current one. +/// One fresh incarnation for a worker no host supplied one to (an older +/// host, or a test). The worker knows no home, so the value carries no home +/// id and orders against nothing: it is only reported, and a host whose +/// record names another value skips its reconcile. fn incarnation() -> String { - layerfs_home::helpers::ordered_token("worker") + layerfs_home::helpers::ordered_token("", "worker", &[]) } /// Serves `listener` until it fails or the process is ended (`machine.stop`; @@ -128,10 +129,6 @@ pub fn serve_with_incarnation( supplied: Option, ) -> std::io::Result { let server = Server::new(secret, root, supplied); - // Recorded before any request is served: every mount intent this worker - // places carries this incarnation, and the host's start reconcile writes - // the same value into each Store and settles what the previous one left. - layerfs_store_service::history::admission::set_machine_incarnation(server.incarnation.clone()); server.run(&listener) } diff --git a/control/crates/layerfs-control/src/worker_server/ops.rs b/control/crates/layerfs-control/src/worker_server/ops.rs index cd7e4acd87..d935acd2c0 100644 --- a/control/crates/layerfs-control/src/worker_server/ops.rs +++ b/control/crates/layerfs-control/src/worker_server/ops.rs @@ -191,45 +191,17 @@ pub(super) fn run( }) } // the operator's repair of one Branch's mount holders on the Store - // under this worker's root: `{ store, store_record, project, branch, - // action, op?, legacy, operator? }`. Preview changes no intent: it - // lists the typed holders and the call log records the read. - // settle/release/settle_legacy act on one explicit op or the legacy - // rows; release retains the row as the durable done audit, and the - // call log records every action. + // under this worker's root: `{ store, store_record, place_view?, + // }`. `list`, `preview` and + // `settle` judge each holder with the caller's place view, installed + // here as a Store call's is; the rest is `layerfs_store::repair`. "store.repair" => { let store = args .get_text("store") .ok_or_else(|| refused("InvalidInput", "a Store repair names its Store"))? .to_owned(); layerfs_home::names::StoreName::parse(&store)?; - let project = args - .get_text("project") - .ok_or_else(|| refused("InvalidInput", "a Store repair names its project"))?; - let branch = args - .get_text("branch") - .ok_or_else(|| refused("InvalidInput", "a Store repair names its Branch"))?; - let action = args - .get_text("action") - .ok_or_else(|| refused("InvalidInput", "a Store repair names its action"))?; - let op = match args.get_text("op") { - None | Some("") => None, - Some(text) => Some( - layerfs_bridge::sdk_values::unhex(text) - .and_then(|bytes| bytes.try_into().ok()) - .ok_or_else(|| refused("InvalidInput", "op must be 16 bytes of hex"))?, - ), - }; - let legacy = matches!(args.get("legacy"), Some(SdkValue::Bool(true))); - let operator = args.get_text("operator").unwrap_or(""); - let page = args - .get_int("page") - .and_then(|page| u32::try_from(page).ok()) - .unwrap_or(0); - let limit = args - .get_int("limit") - .and_then(|limit| u32::try_from(limit).ok()) - .unwrap_or(100); + let request = layerfs_store::repair::Repair::read(&args)?; let file = layerfs_home::home::paths::store_file(root, &store); let open = layerfs_store::open_args::OpenArgs { home: Some(root.to_path_buf()), @@ -243,9 +215,7 @@ pub(super) fn run( args.get("place_view"), ); layerfs_store_service::history::admission::with_place_view(place, || { - layerfs_store::service::repair_mount_holders_at( - &open, project, branch, action, op, legacy, operator, page, limit, - ) + layerfs_store::repair::repair_at(&open, &request) }) } // one step of a Store move, remove or create, or a live read's, on diff --git a/control/crates/layerfs-control/tests/mount_place_authority.rs b/control/crates/layerfs-control/tests/mount_place_authority.rs index ad45a7fced..137e356a05 100644 --- a/control/crates/layerfs-control/tests/mount_place_authority.rs +++ b/control/crates/layerfs-control/tests/mount_place_authority.rs @@ -12,9 +12,7 @@ mod support; use layerfs_bridge::control::SdkValue; use layerfs_history::records::GcScopeKind; -use layerfs_history::{ - BranchId, ForkRequest, ForkSource, HistoryName, LayerStackId, WorkspaceId, -}; +use layerfs_history::{BranchId, ForkRequest, ForkSource, HistoryName, LayerStackId, WorkspaceId}; use layerfs_machine::record::MachineRecord; use layerfs_sandbox::internal::Control; use layerfs_sandbox::record::{mount_place, retire_in, SandboxRecord}; @@ -30,13 +28,13 @@ fn a_daemon_mount_records_the_home_place_and_its_tombstone_settles_it() { let home = f.host.directory.join("home"); std::fs::create_dir_all(&home).unwrap(); let machine = "docker"; - let incarnation = "p1:worker:10:aa"; + let incarnation = "p1:aa11.worker:10:aa"; MachineRecord::built_in(machine, "home-id") .with_incarnation(incarnation) .write(&home) .unwrap(); let sandbox = "place-sbx"; - let generation = "p1:sandbox:10:bb"; + let generation = "p1:aa11.sandbox:10:bb"; let directory = layerfs_home::home::paths::sandbox_dir(&home, sandbox); std::fs::create_dir_all(&directory).unwrap(); SandboxRecord::from_record( @@ -77,7 +75,11 @@ fn a_daemon_mount_records_the_home_place_and_its_tombstone_settles_it() { let service = f.service.clone(); let (channel, worker) = support::pair(move |connection| { for _ in 0..2 { - drop(service.serve_one(connection, |_, _, _| unreachable!()).unwrap()); + drop( + service + .serve_one(connection, |_, _, _| unreachable!()) + .unwrap(), + ); } }); let mut client = Control::new(channel); diff --git a/control/crates/layerfs-control/tests/store_repair_live.rs b/control/crates/layerfs-control/tests/store_repair_live.rs index d7dd7db60c..cf539e31c0 100644 --- a/control/crates/layerfs-control/tests/store_repair_live.rs +++ b/control/crates/layerfs-control/tests/store_repair_live.rs @@ -12,7 +12,6 @@ mod worker_store; use layerfs_bridge::control::{SdkCall, SdkRefusal, SdkValue}; -use layerfs_history::error::HistoryError; use layerfs_history::records::{GcIntent, GcIntentKind, GcScopeKind}; use layerfs_history::PlaceAuthority; use layerfs_home::events::Events; @@ -54,16 +53,6 @@ fn source(tag: &str) -> PathBuf { directory } -fn future_ms() -> i64 { - let now = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .expect("the clock is after the epoch") - .as_millis(); - // An hour: seeding the 331 retired Branches must never let a lease the - // test calls live expire underneath it. - i64::try_from(now).expect("the clock fits an i64") + 3_600_000 -} - fn text(value: &str) -> SdkValue { SdkValue::text(value) } @@ -183,7 +172,6 @@ fn seed(home: &Path) { .named(Some("app"), Some(&name)), }; history.gc_register(&intent).unwrap(); - } // The 331 already retired behind the six: fork, a wholly dead pack, @@ -224,6 +212,9 @@ fn seed(home: &Path) { .unwrap(); } +/// One repair of `branch` on the worker. A settle here is forced: these +/// holders name owners nothing on this host can judge, and the operator +/// confirmed them gone (the guard itself is `store_repair_worker`'s). fn repair( placed: &worker_store::Placed, branch: &str, @@ -231,24 +222,23 @@ fn repair( op: Option, legacy: bool, ) -> SdkValue { - let name = StoreName::parse("shop").unwrap(); - layerfs_control::store::repair( - &placed.home, - &name, - "app", - branch, - action, - op.map(|op| [op; 16]), + let request = layerfs_control::store::RepairRequest { + project: "shop/app".to_owned(), + branch: branch.to_owned(), + action: action.to_owned(), + op: op.map(op_hex), legacy, - "alice", - 0, - 100, - ) - .unwrap_or_else(|refusal| panic!("{action} {branch}: {refusal:?}")) + force: action == "settle", + operator: Some("alice".to_owned()), + limit: None, + cursor: None, + }; + layerfs_control::store::repair(&placed.home, &request) + .unwrap_or_else(|refusal| panic!("{action} {branch}: {refusal:?}")) } fn holders(value: &SdkValue) -> &[SdkValue] { - match value.get("holders") { + match value.get("items") { Some(SdkValue::List(holders)) => holders, other => panic!("no holder list: {other:?}"), } @@ -402,8 +392,8 @@ fn seed_reconcile(home: &Path) { // admission consults its caller's home, so the mounts must exist before // the new incarnation does for the worker start to be the first caller // that can settle the older placement. - let current = "p1:worker:0000000000000020:aa"; - let retired = "p1:sandbox:0000000000000010:aa"; + let current = "p1:aa11.worker:0000000000000020:aa"; + let retired = "p1:aa11.sandbox:0000000000000010:aa"; let record = StoreRecord::read(home, "shop").unwrap().unwrap(); let open = OpenArgs::of_record(home, "shop", &record).unwrap(); with_service(&open, |service| { @@ -445,8 +435,8 @@ fn seed_reconcile(home: &Path) { .gc_register(&mount( 0x82, "live", - "p1:worker:0000000000000030:aa", - "p1:sandbox:0000000000000020:aa", + "p1:aa11.worker:0000000000000030:aa", + "p1:aa11.sandbox:0000000000000020:aa", "boot:82", )) .unwrap(); @@ -457,8 +447,8 @@ fn seed_reconcile(home: &Path) { .gc_register(&mount( 0x81, "live", - "p1:worker:0000000000000010:aa", - "p1:sandbox:0000000000000020:aa", + "p1:aa11.worker:0000000000000010:aa", + "p1:aa11.sandbox:0000000000000020:aa", "boot:81", )) .unwrap(); @@ -494,13 +484,13 @@ fn a_worker_start_reconciles_the_incarnations_its_home_records() { &placed.home, "shop", "box", - "p1:worker:0000000000000020:aa", + "p1:aa11.worker:0000000000000020:aa", ) .expect("the start reconciles the worker's Store"); assert_eq!(outcome.get_text("machine"), Some("box"), "{outcome:?}"); assert_eq!( outcome.get_text("incarnation"), - Some("p1:worker:0000000000000020:aa"), + Some("p1:aa11.worker:0000000000000020:aa"), "{outcome:?}" ); let mut settled = Vec::new(); @@ -555,7 +545,7 @@ fn the_live_six_of_three_hundred_thirty_one_is_repaired_removed_and_reclaimed() ); // The operator's per-Branch preview answers the same typed view and - // settles nothing. + // writes nothing. for tag in FIRST..FIRST + 6 { let name = format!("stuck-{}", tag - FIRST); let preview = repair(&placed, &name, "preview", None, false); @@ -617,7 +607,7 @@ fn the_live_six_of_three_hundred_thirty_one_is_repaired_removed_and_reclaimed() assert_eq!(holders(&preview).len(), 0, "{name}: {preview:?}"); } // The audit is scoped to the Branch it names: each Branch's page holds - // exactly its own two preview rows and one release row. + // exactly its own release row; its two previews wrote none. for tag in FIRST..FIRST + 6 { let name = format!("stuck-{}", tag - FIRST); let audit = repair(&placed, &name, "audit", None, false); @@ -638,28 +628,21 @@ fn the_live_six_of_three_hundred_thirty_one_is_repaired_removed_and_reclaimed() .is_some_and(|evidence| evidence.contains("action=release;operator=alice;op=")) }) .count(); - let previews = rows - .iter() - .filter(|row| row.get_text("evidence") == Some("action=preview;operator=alice")) - .count(); assert_eq!(releases, 1, "{name}: {audit:?}"); - assert_eq!(previews, 2, "{name}: {audit:?}"); + assert_eq!(rows.len(), 1, "{name}: {audit:?}"); } - // 3c. The doctor's worker-side audit is the same bounded page (16 rows at - // limit 16) over the same retained rows, as evidence; it releases nothing. + // 3c. The doctor's worker-side audit is the same bounded page (limit 16) + // over the same retained rows, as evidence; it releases nothing. The six + // releases are every retained row. let doctor = diagnostic_audit(&placed); let Some(SdkValue::List(rows)) = doctor.get("items") else { panic!("doctor audit items: {doctor:?}"); }; - assert_eq!( - rows.len(), - 16, - "the worker audit page is bounded: {doctor:?}" - ); + assert_eq!(rows.len(), 6, "{doctor:?}"); assert_eq!( doctor.get("has_more"), - Some(&SdkValue::Bool(true)), + Some(&SdkValue::Bool(false)), "{doctor:?}" ); let releases = rows @@ -669,12 +652,7 @@ fn the_live_six_of_three_hundred_thirty_one_is_repaired_removed_and_reclaimed() .is_some_and(|evidence| evidence.starts_with("action=release;")) }) .count(); - let previews = rows - .iter() - .filter(|row| row.get_text("evidence") == Some("action=preview;operator=alice")) - .count(); assert_eq!(releases, 6, "{doctor:?}"); - assert_eq!(previews, 10, "{doctor:?}"); // 4. The six now remove, and their names stay retired until compaction. for tag in FIRST..FIRST + 6 { @@ -714,17 +692,13 @@ fn the_live_six_of_three_hundred_thirty_one_is_repaired_removed_and_reclaimed() // durable operator audit. Preview still filters them. let preview = repair(&placed, "main", "preview", None, false); assert_eq!(holders(&preview).len(), 0, "no holder remains"); - // The audit is scoped: main has only the preview this step wrote, and - // each repaired Branch keeps its own six rows through the rebuild. + // The audit is scoped: main has no row (its preview wrote none), and + // each repaired Branch keeps its own release row through the rebuild. let audit = repair(&placed, "main", "audit", None, false); let Some(SdkValue::List(rows)) = audit.get("items") else { panic!("audit items: {audit:?}"); }; - assert_eq!(rows.len(), 1, "main's audit is the preview it wrote: {audit:?}"); - assert_eq!( - rows[0].get_text("evidence"), - Some("action=preview;operator=alice") - ); + assert!(rows.is_empty(), "main's preview wrote a row: {audit:?}"); for tag in FIRST..FIRST + 6 { let name = format!("stuck-{}", tag - FIRST); let audit = repair(&placed, &name, "audit", None, false); @@ -738,12 +712,11 @@ fn the_live_six_of_three_hundred_thirty_one_is_repaired_removed_and_reclaimed() .is_some_and(|evidence| evidence.starts_with("action=release;")) }) .count(); - let previews = rows - .iter() - .filter(|row| row.get_text("evidence") == Some("action=preview;operator=alice")) - .count(); - assert_eq!(releases, 1, "the compaction kept {name}'s release: {audit:?}"); - assert_eq!(previews, 2, "the compaction kept {name}'s previews: {audit:?}"); + assert_eq!( + releases, 1, + "the compaction kept {name}'s release: {audit:?}" + ); + assert_eq!(rows.len(), 1, "{name}: {audit:?}"); } placed.remove(); diff --git a/control/crates/layerfs-control/tests/store_repair_worker.rs b/control/crates/layerfs-control/tests/store_repair_worker.rs index 41be3a533a..8539ea663c 100644 --- a/control/crates/layerfs-control/tests/store_repair_worker.rs +++ b/control/crates/layerfs-control/tests/store_repair_worker.rs @@ -8,6 +8,8 @@ mod worker_store; use layerfs_bridge::control::SdkValue; +use layerfs_bridge::sdk_values::hex; +use layerfs_control::store::RepairRequest; use layerfs_history::records::{GcIntent, GcIntentKind, GcScopeKind}; use layerfs_history::{HistoryName, NamedSnapshot, PlaceAuthority}; use layerfs_home::names::StoreName; @@ -35,14 +37,6 @@ fn source(tag: &str) -> PathBuf { directory } -fn future_ms() -> i64 { - let now = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .expect("the clock is after the epoch") - .as_millis(); - i64::try_from(now).expect("the clock fits an i64") + 60_000 -} - fn mount(stack: &[u8], branch: &[u8], op: u8) -> GcIntent { GcIntent::shared( [op; 16], @@ -121,25 +115,27 @@ fn serve(listener: std::net::TcpListener, root: PathBuf) { }); } -fn repair(placed: &worker_store::Placed, action: &str, op: Option, legacy: bool) -> SdkValue { - let name = StoreName::parse("shop").unwrap(); - layerfs_control::store::repair( - &placed.home, - &name, - "app", - "main", - action, - op.map(|op| [op; 16]), +fn request(action: &str, op: Option, legacy: bool) -> RepairRequest { + RepairRequest { + project: "shop/app".to_owned(), + branch: "main".to_owned(), + action: action.to_owned(), + op: op.map(|op| hex(&[op; 16])), legacy, - "alice", - 0, - 100, - ) - .unwrap_or_else(|refusal| panic!("{action}: {refusal:?}")) + force: false, + operator: Some("alice".to_owned()), + limit: None, + cursor: None, + } +} + +fn repair(placed: &worker_store::Placed, action: &str, op: Option, legacy: bool) -> SdkValue { + layerfs_control::store::repair(&placed.home, &request(action, op, legacy)) + .unwrap_or_else(|refusal| panic!("{action}: {refusal:?}")) } fn holders(value: &SdkValue) -> &[SdkValue] { - match value.get("holders") { + match value.get("items") { Some(SdkValue::List(holders)) => holders, other => panic!("no holder list: {other:?}"), } @@ -187,8 +183,14 @@ fn repair_previews_settles_and_releases_through_the_worker() { assert_eq!(live.get_text("machine_incarnation"), Some("worker:1")); assert_eq!(live.get_text("state"), Some("live")); - // Preview over the worker answers the typed view and settles nothing. + // Preview over the worker answers the typed view and writes nothing. let preview = repair(&placed, "preview", None, false); + assert_eq!(preview.get_text("action"), Some("preview"), "{preview:?}"); + let audit = repair(&placed, "audit", None, false); + assert!( + matches!(audit.get("items"), Some(SdkValue::List(rows)) if rows.is_empty()), + "preview wrote a row: {audit:?}" + ); let rows = holders(&preview); assert_eq!(rows.len(), 2); let live = rows @@ -196,6 +198,9 @@ fn repair_previews_settles_and_releases_through_the_worker() { .find(|row| row.get_text("op") == Some("21".repeat(16).as_str())) .expect("the live holder"); assert_eq!(live.get_text("state"), Some("live")); + // nothing proves its owner gone: it still looks live, so not safe + assert_eq!(live.get_text("liveness"), Some("live")); + assert_eq!(live.get("safe"), Some(&SdkValue::Bool(false))); assert_eq!(live.get_text("sandbox"), Some("box")); assert_eq!(live.get_text("machine_incarnation"), Some("worker:1")); assert_eq!(live.get_text("sandbox_generation"), Some("generation:1")); @@ -206,23 +211,28 @@ fn repair_previews_settles_and_releases_through_the_worker() { .expect("the legacy holder"); assert_eq!(legacy.get("legacy"), Some(&SdkValue::Bool(true))); - // Release without settle is refused; settle marks the one named op. - let name = StoreName::parse("shop").unwrap(); - let refused = layerfs_control::store::repair( - &placed.home, - &name, - "app", - "main", - "release", - Some([0x21; 16]), - false, - "alice", - 0, - 100, - ) - .unwrap_err(); + // Release without settle is refused; a settle of a holder that still + // looks live is refused `InUse` unless forced, and marks the one op. + let refused = + layerfs_control::store::repair(&placed.home, &request("release", Some(0x21), false)) + .unwrap_err(); assert_eq!(refused.code, "Conflict", "{refused:?}"); - repair(&placed, "settle", Some(0x21), false); + let refused = + layerfs_control::store::repair(&placed.home, &request("settle", Some(0x21), false)) + .unwrap_err(); + assert_eq!(refused.code, "InUse", "{refused:?}"); + assert!( + refused + .detail + .get_text("fix") + .is_some_and(|fix| fix.ends_with("--force")), + "{refused:?}" + ); + let forced = RepairRequest { + force: true, + ..request("settle", Some(0x21), false) + }; + layerfs_control::store::repair(&placed.home, &forced).expect("a forced settle"); let preview = repair(&placed, "preview", None, false); let settled = holders(&preview) .iter() @@ -260,10 +270,6 @@ fn repair_previews_settles_and_releases_through_the_worker() { let evidence = row.get_text("evidence").expect("evidence"); assert!(evidence.contains("operator=alice"), "{row:?}"); } - let previews: Vec<_> = rows - .iter() - .filter(|row| row.get_text("evidence") == Some("action=preview;operator=alice")) - .collect(); - assert_eq!(previews.len(), 4, "every worker preview: {audit:?}"); + assert_eq!(rows.len(), 2, "no worker preview wrote a row: {audit:?}"); placed.remove(); } diff --git a/control/crates/layerfs-home/src/helpers.rs b/control/crates/layerfs-home/src/helpers.rs index 2b69489dd5..ca06d75d6e 100644 --- a/control/crates/layerfs-home/src/helpers.rs +++ b/control/crates/layerfs-home/src/helpers.rs @@ -29,20 +29,46 @@ pub fn required<'a>(value: Option<&'a str>, name: &str) -> Result<&'a str, SdkRe value.ok_or_else(|| refused("InvalidInput", format!("missing text argument `{name}`"))) } -/// One ordered place value, `p1: