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 b723968f1c..657c599860 100644 --- a/api/crates/layerfs-api/src/store/repair_mount_holders.rs +++ b/api/crates/layerfs-api/src/store/repair_mount_holders.rs @@ -4,13 +4,15 @@ //! 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. +//! and records the operator label; `settle_legacy` settles only rows without +//! owner or place evidence; `audit` pages the Branch's retained rows by +//! `(updated_ms, op)`. //! 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, Project, Text}, }; -use crate::calls::{Context, Paging}; +use crate::calls::Context; use layerfs_bridge::control::SdkValue; use layerfs_control::store::RepairRequest; use layerfs_home::outcome::Outcome; @@ -27,10 +29,12 @@ pub(crate) const CALL: Call = call( opt("limit", Int), opt("cursor", Text).redact(), ], - "list, preview, settle, release or audit mount holders (operator)", + "list, preview, settle, release, settle_legacy or audit mount holders (operator)", run, -) -.paged(Paging::Cursor); +); +// It pages by `limit`/`cursor`, but its answers are not on the 2.0 answer +// form yet, so it declares no `Paging` (an entry without a form declares +// none; calls_table checks this). fn run(context: &mut Context<'_>, args: &SdkValue) -> Outcome { let text = |name: &str| args.get_text(name).map(str::to_owned); diff --git a/api/crates/layerfs-cli/tests/gc.rs b/api/crates/layerfs-cli/tests/gc.rs index bd803e89f7..101bdfd001 100644 --- a/api/crates/layerfs-cli/tests/gc.rs +++ b/api/crates/layerfs-cli/tests/gc.rs @@ -37,7 +37,7 @@ const CALLS: &[(&[&str], &str, &str)] = &[ ( &["store", "repair-mount-holders"], "layerfs store repair-mount-holders ", - "list, preview, settle, release or audit mount holders (operator)", + "list, preview, settle, release, settle_legacy or audit mount holders (operator)", ), ]; diff --git a/control/crates/layerfs-control/tests/store_repair_worker.rs b/control/crates/layerfs-control/tests/store_repair_worker.rs index 8539ea663c..5d720a1c46 100644 --- a/control/crates/layerfs-control/tests/store_repair_worker.rs +++ b/control/crates/layerfs-control/tests/store_repair_worker.rs @@ -273,3 +273,100 @@ fn repair_previews_settles_and_releases_through_the_worker() { assert_eq!(rows.len(), 2, "no worker preview wrote a row: {audit:?}"); placed.remove(); } + +/// The worker judges with the view this home sends, not its own: once the +/// host home records the tombstone of the live holder's Sandbox generation +/// (and no Sandbox record of that name), the worker's preview calls the +/// holder `dead` and a `settle` without `--force` succeeds, where it was +/// `live` and refused `InUse` before. The audit cursor crosses to the worker +/// and back as the last row's `(updated_ms, op)`; one the call cannot read +/// is `InvalidInput`. +#[test] +fn the_worker_judges_with_the_sent_view_and_pages_the_audit_by_key() { + let placed = worker_store::placed_seeded("repair-view", seed, serve); + let liveness = |placed: &worker_store::Placed| { + let preview = repair(placed, "preview", None, false); + let row = holders(&preview) + .iter() + .find(|row| row.get_text("op") == Some("21".repeat(16).as_str())) + .expect("the placed holder") + .clone(); + ( + row.get_text("liveness").map(str::to_owned), + row.get("safe").cloned(), + ) + }; + assert_eq!( + liveness(&placed), + (Some("live".to_owned()), Some(SdkValue::Bool(false))) + ); + let refused = + layerfs_control::store::repair(&placed.home, &request("settle", Some(0x21), false)) + .unwrap_err(); + assert_eq!(refused.code, "InUse", "{refused:?}"); + + // The host home now holds the tombstone of generation `generation:1` of + // Sandbox `box` on machine `box`, and no Sandbox record: dead. + let retired = layerfs_home::home::paths::sandbox_retired_dir(&placed.home, "box"); + std::fs::create_dir_all(&retired).unwrap(); + layerfs_home::home::write_record( + &retired.join("box.json"), + &SdkValue::map([("generation", SdkValue::text("generation:1"))]), + ) + .unwrap(); + assert_eq!( + liveness(&placed), + (Some("dead".to_owned()), Some(SdkValue::Bool(true))) + ); + let settled = repair(&placed, "settle", Some(0x21), false); + assert_eq!(settled.get_text("state"), Some("uncertain"), "{settled:?}"); + repair(&placed, "release", Some(0x21), false); + repair(&placed, "settle_legacy", None, false); + repair(&placed, "release", Some(0x31), false); + + // Two retained rows, one a page: the cursor names the first row's + // place, the second page holds the other row and ends. + let page = |cursor: Option| { + let request = RepairRequest { + limit: Some(1), + cursor, + ..request("audit", None, false) + }; + layerfs_control::store::repair(&placed.home, &request) + }; + let first = page(None).expect("the first audit page"); + let cursor = first + .get_text("next_cursor") + .map(str::to_owned) + .unwrap_or_else(|| panic!("a next page: {first:?}")); + let second = page(Some(cursor)).expect("the second audit page"); + let op = |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:?}"), + }; + let mut ops = vec![op(&first), op(&second)]; + ops.sort(); + assert_eq!( + ops, + vec![Some("21".repeat(16)), Some("31".repeat(16))], + "each row once" + ); + assert_eq!(second.get("next_cursor"), Some(&SdkValue::Null)); + + // A cursor this call made, around a place it cannot read. + let filters = [ + ("project", SdkValue::text("shop/app")), + ("branch", SdkValue::text("main")), + ("action", SdkValue::text("audit")), + ]; + for place in [ + SdkValue::text("nope"), + SdkValue::text("12:abcd"), + SdkValue::Int(1), + ] { + let made = layerfs_home::cursor::make("store.repair_mount_holders", place, &filters); + let refused = page(Some(made)).unwrap_err(); + assert_eq!(refused.code, "InvalidInput", "{refused:?}"); + } + placed.remove(); +} diff --git a/control/crates/layerfs-sandbox/src/create.rs b/control/crates/layerfs-sandbox/src/create.rs index a50c665a7f..b6d1cd9332 100644 --- a/control/crates/layerfs-sandbox/src/create.rs +++ b/control/crates/layerfs-sandbox/src/create.rs @@ -96,6 +96,18 @@ pub fn create( home: &Path, events: &mut Events<'_>, request: &CreateRequest, +) -> Result { + create_on(home, events, request, crate::dispatch::call) +} + +/// [`create`], whose start runs through `local` on a machine whose calls +/// run in this process (`crate::dispatch::call` in [`create`]; a test may +/// stand in for the Sandbox's own side there). +pub fn create_on( + home: &Path, + events: &mut Events<'_>, + request: &CreateRequest, + local: crate::route::Local, ) -> Result { let home = home.to_path_buf(); let project = request.project.as_str(); @@ -217,8 +229,8 @@ pub fn create( image: request.image.clone(), start: Some((0, total(request.mount))), }; - let started = on_machine(&home, events, &record, asked, crate::dispatch::call) - .map(|value| Started::read(&value)); + let started = + on_machine(&home, events, &record, asked, local).map(|value| Started::read(&value)); // a worker's Sandbox whose daemon this host reaches directly (#535 G4): // its forward opens before the locks are taken let opened = (started.as_ref().ok()).map(|started| direct::open(&home, &record, started)); diff --git a/control/crates/layerfs-sandbox/src/family.rs b/control/crates/layerfs-sandbox/src/family.rs index d487807121..b61c9df2df 100644 --- a/control/crates/layerfs-sandbox/src/family.rs +++ b/control/crates/layerfs-sandbox/src/family.rs @@ -18,7 +18,7 @@ //! (`create`, `list`, `get`, `remove`), the state of a Sandbox in //! `state.rs`, the route of a host operation in `route.rs`, and the //! `workspace` calls in `workspace/calls.rs`. -pub use super::create::{create, CreateRequest, Created}; +pub use super::create::{create, create_on, CreateRequest, Created}; pub use super::get::{get, Down, GetRequest, Got, ListedWorkspace}; pub use super::list::{list, ListRequest, Listed, ListedSandbox}; use super::record::SandboxRecord; diff --git a/control/crates/layerfs-sandbox/src/files.rs b/control/crates/layerfs-sandbox/src/files.rs index fa836cd6d7..9116f0875a 100644 --- a/control/crates/layerfs-sandbox/src/files.rs +++ b/control/crates/layerfs-sandbox/src/files.rs @@ -55,11 +55,12 @@ pub fn started(directory: &Path) -> Result> { /// Records started Sandbox `state` in its folder `directory`: its daemon /// keys in `control.json`, then what reopens it in its record (`daemon`, /// with its backend, route and runtime id, and `platform`, which `create` -/// checked before any effect, #529), the record's other fields kept. +/// checked before any effect, #529), the record's other fields kept, and +/// its place in the same write ([`write_started`]). pub fn save_sandbox( directory: &Path, state: &SandboxState, - (host, platform): (&Host, &str), + (host, platform, place): (&Host, &str, Option<&(String, String)>), secret: [u8; 32], peer: [u8; 32], instance: [u8; 32], @@ -104,40 +105,49 @@ pub fn save_sandbox( ("instance", SdkValue::Bytes(instance.to_vec())), ]), )?; - // on `native` this is the home's record, which keeps its other fields - // (its `op` among them): changed under the Sandbox lock (#536 §8) - let _sandbox = crate::record::lock_in(directory, &state.id)?; - let record = SandboxRecord::read_in(directory, &state.id)? - .unwrap_or_else(|| SandboxRecord::from_record(&state.id, SdkValue::Null)); - let store = &host.store; let facts = [ crate::runtime::kind(runtime).to_owned(), route(runtime).to_owned(), runtime.identity().to_string(), ]; - let record = record.with_start(format!("{store}/{}", state.project), facts, daemon); - record.write_in(directory) + let project = format!("{}/{}", host.store, state.project); + let worker = layerfs_home::home::guest::is_root(&host.home); + write_started( + directory, + &state.id, + (project, facts, daemon), + (place, worker), + ) } -/// Writes the machine and generation a worker's caller minted (`place`) -/// into the worker's own record of Sandbox `name` in `directory`, so the -/// mounts it places carry them (#783 gap 1). The caller's values always win: -/// a record an interrupted removal left behind may name an older generation, -/// and a mount recording that one would look retired beside the home's -/// newer record. On `native` the record is the home's own and already names -/// them (nothing is written). Best effort: a record left without them only -/// means its mounts carry no place evidence, and missing evidence is never -/// death. -pub fn save_place(directory: &Path, name: &str, (machine, generation): &(String, String)) { - let _ = (|| -> Result<()> { - let _sandbox = crate::record::lock_in(directory, name)?; - let Some(record) = SandboxRecord::read_in(directory, name)? else { - return Ok(()); - }; - if record.generation() == Some(generation) && record.machine() == machine { - return Ok(()); - } - record.with_place(machine, generation).write_in(directory) - })(); + +/// Writes the record of Sandbox `name` in `directory` once it started: its +/// `project`, backend, route and runtime id (`facts`) and `daemon` +/// ([`SandboxRecord::with_start`]), with its place in the same write, so a +/// mount never reads a place from another write (#783 gap 1). `place` is +/// the machine and generation the caller's home minted; the caller's values +/// always win. A worker's own record (`worker`) keeps no other place: a +/// record an interrupted removal left behind may name an older generation, +/// which the home's newer record would prove retired, so without `place` +/// it names none (missing evidence is never death, wrong evidence is). +/// On `native` the record is the home's own, which names its place already. +/// Changed under the Sandbox lock (#536 §8), its other fields kept (its +/// `op` among them). +pub fn write_started( + directory: &Path, + name: &str, + (project, facts, daemon): (String, [String; 3], DaemonRecord), + (place, worker): (Option<&(String, String)>, bool), +) -> Result<()> { + let _sandbox = crate::record::lock_in(directory, name)?; + let record = SandboxRecord::read_in(directory, name)? + .unwrap_or_else(|| SandboxRecord::from_record(name, SdkValue::Null)) + .with_start(project, facts, daemon); + match (place, worker) { + (Some((machine, generation)), _) => record.with_place(machine, generation), + (None, true) => record.without_place(), + (None, false) => record, + } + .write_in(directory) } /// The kernel route a running Sandbox's Workspaces use: loopback NFS for a diff --git a/control/crates/layerfs-sandbox/src/launch/local.rs b/control/crates/layerfs-sandbox/src/launch/local.rs index 5eb8f10c65..e2f5e81525 100644 --- a/control/crates/layerfs-sandbox/src/launch/local.rs +++ b/control/crates/layerfs-sandbox/src/launch/local.rs @@ -38,7 +38,7 @@ use std::sync::Arc; /// Store, whose daemon dials the server (#535 §7.4). `platform` is the /// one `create` checked, recorded with it. pub fn start( - (host, platform): (&Host, &str), + (host, platform, place): (&Host, &str, Option<&(String, String)>), directory: &Path, project: OpenedStore, backend: crate::runtime::Backend, @@ -77,7 +77,7 @@ pub fn start( }; let id = host.sandbox()?; start_named( - (host, platform), + (host, platform, place), directory, id, project, @@ -88,7 +88,7 @@ pub fn start( /// Starts Sandbox `id`: directory and daemon on the installed Store, its /// first control connection, then its records in its folder `directory`. fn start_named( - (host, platform): (&Host, &str), + (host, platform, place): (&Host, &str, Option<&(String, String)>), directory: &Path, id: &str, project: OpenedStore, @@ -243,7 +243,7 @@ fn start_named( crate::files::save_sandbox( &state.directory, &state, - (host, platform), + (host, platform, place), controller.private, daemon.public, instance, diff --git a/control/crates/layerfs-sandbox/src/lifecycle.rs b/control/crates/layerfs-sandbox/src/lifecycle.rs index ea5e16800d..db81958ff4 100644 --- a/control/crates/layerfs-sandbox/src/lifecycle.rs +++ b/control/crates/layerfs-sandbox/src/lifecycle.rs @@ -189,9 +189,10 @@ fn repaired( /// Starts the Sandbox the call names (`sandbox`) as its deployment says /// (`deploy.sandbox`: native or namespaces), its daemon on `store`. Called /// under the guard of its folder `directory`, so no other call on it runs -/// meanwhile. +/// meanwhile. `place`, the machine and generation the caller's home minted, +/// is written with its record ([`crate::files::save_sandbox`]). pub fn start( - (host, platform): (&Host, &str), + (host, platform, place): (&Host, &str, Option<&(String, String)>), directory: &Path, store: OpenedStore, ) -> Result, SdkRefusal> { @@ -202,11 +203,11 @@ pub fn start( let backend = crate::runtime::backend(deploy)?; #[cfg(any(target_os = "linux", target_os = "macos"))] { - crate::launch::local::start((host, platform), directory, store, backend) + crate::launch::local::start((host, platform, place), directory, store, backend) } #[cfg(not(any(target_os = "linux", target_os = "macos")))] { - drop((directory, store, backend, platform)); + drop((directory, store, backend, platform, place)); Err(refused( "Unsupported", "a Sandbox runs on Linux (namespaces) or macOS (native)", @@ -224,7 +225,8 @@ pub fn start( /// `namespaces`; a native one's `image` is `local`. `platform` (#529) is /// checked against that backend and recorded. /// `place`, the machine and generation the caller's home minted, goes into -/// a worker's own record once the Sandbox started ([`crate::files::save_place`]). +/// a worker's own record in the same write that records it started +/// ([`crate::files::save_sandbox`]). /// The only call that starts a Sandbox. Failures are `Failed` or /// `Unknown`, never retried. pub fn create(host: &Host, named: Option<&str>, place: Option<&(String, String)>) -> Outcome { @@ -261,11 +263,7 @@ pub fn create(host: &Host, named: Option<&str>, place: Option<&(String, String)> } None => {} } - let state = start((host, &platform), &directory, store)?; - if let Some(place) = place { - crate::files::save_place(&directory, &state.id, place); - } - Ok(state) + start((host, &platform, place), &directory, store) })(); let state = match made { Ok(state) => state, @@ -306,9 +304,12 @@ pub fn create(host: &Host, named: Option<&str>, place: Option<&(String, String)> /// Stop, then delete, each attempted once: a failed stop is not followed by /// a delete and keeps its cause; the folder goes after the delete. A /// Sandbox whose runtime ended by itself is reclaimed ([`reclaim`]) and -/// answered `NoSuchSandbox`, as gone. A local record this build does not -/// read is forgotten ([`forget_unread`]), no signal sent: a worker's own -/// record, when the caller's home keeps only the forward to it. +/// answered `NoSuchSandbox`, as gone (its `left` detail names a retired +/// tombstone the reclaim could not write). A local record this build does +/// not read is forgotten ([`forget_unread`]), no signal sent: a worker's own +/// record, when the caller's home keeps only the forward to it. Neither it +/// nor a retired docker record ([`forget`]) gets a retired tombstone: its +/// runtime was not stopped, so nothing proves its mounts dead. /// Once the runtime is gone, each drained Workspace's mount intent is /// ended once (`left` names each one not ended); on a PostgreSQL Store /// whose URL this machine cannot dial, those bound by id go back to the @@ -324,19 +325,23 @@ pub fn close(host: &Host, force: bool, held: &[String]) -> Outcome { )); }; if recorded.retired() { - let _ = retired(host, &id); return forget(&id, &directory, &recorded); } if let Some(why) = crate::reopen::unread(&recorded) { - let unretired = retired(host, &id); - return forget_unread(&id, &directory, &recorded, &why, unretired); + return forget_unread(&id, &directory, &recorded, &why); } let state = match crate::registry::sandbox_live(host) { Ok(state) => state, Err(gone) if gone.detail.get_bool("containerGone") => { let container = gone.detail.get_text("container").unwrap_or("").to_owned(); - reclaim(host, &container, gone.detail.get_bool("containerStopped"))?; - return Err(gone); + let reclaimed = reclaim(host, &container, gone.detail.get_bool("containerStopped"))?; + return Err(match reclaimed.get("left") { + Some(left @ SdkValue::List(items)) if !items.is_empty() => SdkRefusal { + detail: layerfs_home::home::with_field(gone.detail, "left", left.clone()), + ..gone + }, + _ => gone, + }); } Err(refusal) => return Err(refusal), }; @@ -516,25 +521,18 @@ fn forget(id: &str, directory: &Path, recorded: &DaemonRecord) -> Outcome { /// build does not read (`why`, [`crate::reopen::unread`]; RV3 FAIL 2, 3): /// its identity cannot be checked, so nothing is signalled. Its folder goes /// under its guard, and the answer (`Forgotten`, with `left`) says what may -/// remain: its daemon or init process and its run folder. -/// `unretired` is the sentence of a retirement tombstone that was not -/// written, which `left` carries too. -fn forget_unread( - id: &str, - directory: &Path, - recorded: &DaemonRecord, - why: &str, - unretired: Option, -) -> Outcome { +/// remain: its daemon or init process and its run folder. No retired +/// tombstone is written: that process may still run. +fn forget_unread(id: &str, directory: &Path, recorded: &DaemonRecord, why: &str) -> Outcome { let _guard = layerfs_store::project::discovery::guard(directory)?; discard(directory, recorded)?; std::fs::remove_dir_all(directory).map_err(failed)?; layerfs_home::home::generation::moved_above(directory); - let left = std::iter::once(SdkValue::text(unread_left(recorded, why))).chain(unretired); + let left = vec![SdkValue::text(unread_left(recorded, why))]; Ok(SdkValue::map([ ("kind", SdkValue::text("Forgotten")), ("sandbox", SdkValue::text(id)), - ("left", SdkValue::List(left.collect())), + ("left", SdkValue::List(left)), ])) } @@ -582,7 +580,9 @@ const STOP_S: u16 = 5; /// stopped (`container`, its runtime id; `stopped`): one Delete of a /// stopped runtime, then its folder. Under its folder's guard the record is /// read again: a Sandbox started again meanwhile has another runtime id -/// and is left alone (`Busy`). +/// and is left alone (`Busy`). Its runtime is confirmed gone, so the home +/// record's retired tombstone is written before the folder goes; one that +/// was not written is the sentence of its answer's `left`. pub fn reclaim(host: &Host, container: &str, stopped: bool) -> Outcome { let id = host.sandbox()?; let directory = crate::files::sandbox_dir(host)?; @@ -606,13 +606,15 @@ pub fn reclaim(host: &Host, container: &str, stopped: bool) -> Outcome { .map_err(values::wire)?; } // its runtime is gone: the home record's tombstone first, best effort - // (the close that reclaimed it answers `NoSuchSandbox`, with no `left`) - let _ = retired(host, id); + // (the close that reclaimed it answers `NoSuchSandbox`, its `left` + // detail naming a tombstone that was not written) + let unretired = retired(host, id); lock(&SANDBOXES)?.remove(&directory); std::fs::remove_dir_all(&directory).map_err(failed)?; layerfs_home::home::generation::moved_above(&directory); Ok(SdkValue::map([ ("kind", SdkValue::text("Reclaimed")), ("id", SdkValue::text(id)), + ("left", SdkValue::List(unretired.into_iter().collect())), ])) } diff --git a/control/crates/layerfs-sandbox/src/record/sandbox.rs b/control/crates/layerfs-sandbox/src/record/sandbox.rs index 1d756d264b..8fef08c254 100644 --- a/control/crates/layerfs-sandbox/src/record/sandbox.rs +++ b/control/crates/layerfs-sandbox/src/record/sandbox.rs @@ -280,6 +280,14 @@ impl SandboxRecord { self } + /// The record naming no machine or generation: a worker's own record + /// whose caller sent no place keeps none an earlier one left. + pub fn without_place(mut self) -> Self { + self.fields.remove("machine"); + self.fields.remove("generation"); + self + } + /// The intent the record holds, if any. pub fn op(&self) -> Option { self.fields diff --git a/control/crates/layerfs-sandbox/src/record/tombstone.rs b/control/crates/layerfs-sandbox/src/record/tombstone.rs index e329514e0b..3b657128ba 100644 --- a/control/crates/layerfs-sandbox/src/record/tombstone.rs +++ b/control/crates/layerfs-sandbox/src/record/tombstone.rs @@ -1,10 +1,16 @@ //! Retired Sandbox tombstones: `machine//sandbox-retired/.json`. //! -//! Every removal of a Sandbox's home record writes the tombstone **before** it -//! deletes that record, best effort: a close or force close on `native`, a -//! reclaim of a runtime that ended by itself, a forget, and `sandbox.remove` -//! on every route (normal, forgotten, unstarted), which writes the home's -//! tombstone for a worker's Sandbox. A crash leaves the record or both, never +//! A removal of a Sandbox's home record writes the tombstone **before** it +//! deletes that record, best effort, only when the Sandbox's runtime is +//! confirmed stopped, confirmed gone or never started: a close or force +//! close on `native`, a reclaim of a runtime that ended by itself, and +//! `sandbox.remove` after its side answered `Closed` or `NoSuchSandbox`, +//! on a gone VM or stopped machine, or of a create that ended claiming the +//! name (it writes the home's tombstone for a worker's Sandbox). A removal +//! that did not stop the runtime (a record this build does not read or +//! reopen, a retired docker record, a worker that keeps its record) writes +//! none: a tombstone proves the mounts of its generation dead, and no +//! evidence is the safe outcome. A crash leaves the record or both, never //! neither; a write that fails is reported in `left` and the removal goes on, //! since a missing tombstone is only "no proof". The path is outside //! `sandbox/`, so a Sandbox listing never mistakes a tombstone for a Sandbox. diff --git a/control/crates/layerfs-sandbox/src/remove.rs b/control/crates/layerfs-sandbox/src/remove.rs index 334c106a1b..f096a0ff06 100644 --- a/control/crates/layerfs-sandbox/src/remove.rs +++ b/control/crates/layerfs-sandbox/src/remove.rs @@ -10,6 +10,16 @@ //! `Busy`; a gone owner's uncertain intent does not: `sandbox.remove` is the //! call that clears it. Its progress is its op's phases: `cancel` (forced, //! one event per command this home records), `stop` and `delete`. +//! +//! The home's retired tombstone of the Sandbox is written before its record +//! goes only when its runtime is confirmed stopped (a close answered +//! `Closed`), confirmed gone (its side answered `NoSuchSandbox` with +//! `containerGone`, or its machine is recorded stopped) or never started (a +//! create that ended claiming the name). A removal that leaves a runtime it +//! did not stop (a record this build does not read or reopen, a worker that +//! keeps its record, a machine whose record has no 2.0 name or whose VM a +//! live read found missing, which may come back) writes none: no evidence +//! is the safe outcome, a tombstone would prove a live mount dead. use super::create::remove_folder; use super::direct; use super::family::named; @@ -96,8 +106,9 @@ pub fn remove(home: &Path, events: &mut Events<'_>, request: &RemoveRequest) -> ); return forget(&home, events, name, container, Worker::Ask); } - // a Sandbox on a machine whose record has no 2.0 name (RV3 FAIL 1): - // nothing on that machine is reached, its worker included + // a Sandbox on a machine whose record has no 2.0 name (RV3 FAIL 1), or + // whose VM a live read found missing: nothing on that machine is + // reached, its worker included, and neither proves its runtime gone let machine = found.machine(); let on = layerfs_machine::record::MachineRecord::read(&home, machine)?; if let Some(on) = on.filter(|record| record.gone()) { @@ -107,10 +118,7 @@ pub fn remove(home: &Path, events: &mut Events<'_>, request: &RemoveRequest) -> Sandbox's daemon and run folder there, if any, stay in the VM or container an \ earlier build made, which no call reaches" ), - false => format!( - "machine {machine}'s VM is gone, so only the Sandbox's records were removed: \ - its daemon and run folder were in that VM, which its provider no longer holds" - ), + false => missing_vm(machine, found.project()), }; return forget(&home, events, name, first, Worker::Skip); } @@ -132,7 +140,7 @@ pub fn remove(home: &Path, events: &mut Events<'_>, request: &RemoveRequest) -> // with `force`, goes by its records alone; without either, the remove // says how it can go if let Some(first) = stopped_on(&home, &found, request.force)? { - return forget(&home, events, name, first, Worker::Skip); + return forget(&home, events, name, first, Worker::Gone); } let force = request.force; // the commands this home records, which a forced remove cancels first @@ -225,24 +233,28 @@ pub fn remove(home: &Path, events: &mut Events<'_>, request: &RemoveRequest) -> }; match stopped { // a record its side does not read went by its records alone, no - // signal sent (RV3 FAIL 2, 3): as gone, `left` saying what may remain + // signal sent (RV3 FAIL 2, 3): as gone, `left` saying what may + // remain. Its daemon may still run, so no tombstone is written Ok(value) if value.get_text("kind") == Some("Forgotten") && value.get("left").is_some() => { phase(events, "delete", None); direct::release(&home, &record); - let unretired = retire_home(&home, &record, &dir); remove_folder(&dir)?; done(events); let left = match value.get("left") { Some(SdkValue::List(items)) => texts(items).map(str::to_owned).collect(), _ => Vec::new(), }; - let left = left.into_iter().chain(unretired).collect(); Ok(Removed::AlreadyGone { left }) } Ok(value) => { phase(events, "delete", None); direct::release(&home, &record); - let unretired = retire_home(&home, &record, &dir); + // only a close that stopped the runtime proves its mounts dead; + // a retired docker record its side forgot left its container + let unretired = match value.get_text("kind") { + Some("Closed") => retire_home(&home, &record, &dir), + _ => None, + }; remove_folder(&dir)?; let unmounted = match value.get("unknown") { Some(SdkValue::List(paths)) => texts(paths).map(&handle_of).collect(), @@ -263,11 +275,23 @@ pub fn remove(home: &Path, events: &mut Events<'_>, request: &RemoveRequest) -> done(events); Ok(Removed::Stopped { unmounted, left }) } - // a Sandbox already gone loses its record too + // a Sandbox already gone loses its record too; its side's reclaim + // names a tombstone it could not write in its `left` detail. Only a + // daemon its side found gone or stopped (`containerGone`) proves its + // mounts dead: any other `NoSuchSandbox` (no started record, no + // daemon keys) inspected no runtime, so no tombstone is written Err(refusal) if refusal.code == "NoSuchSandbox" => { phase(events, "delete", None); direct::release(&home, &record); - let left = retire_home(&home, &record, &dir).into_iter().collect(); + let reclaimed: Vec = match refusal.detail.get("left") { + Some(SdkValue::List(items)) => texts(items).map(str::to_owned).collect(), + _ => Vec::new(), + }; + let unretired = match refusal.detail.get_bool("containerGone") { + true => retire_home(&home, &record, &dir), + false => None, + }; + let left = reclaimed.into_iter().chain(unretired).collect(); remove_folder(&dir)?; done(events); Ok(Removed::AlreadyGone { left }) @@ -375,11 +399,15 @@ fn foreign(home: &Path, record: &SandboxRecord) -> Result> { /// Removes the record of Sandbox `name` that this build does not reopen: /// one recorded under the retired docker backend (D-8, /// layerfs-project#111), one whose daemon's name this home does not give -/// ([`foreign`]), or one on a machine whose record has no 2.0 name. Its +/// ([`foreign`]), one on a machine whose record has no 2.0 name or whose VM +/// a live read found missing, or one on a machine recorded stopped. Its /// machine's worker is asked once to drop its own record of it (a worker /// that cannot be reached or refuses leaves that record, and `left` says /// so), unless `worker` skips it, then the home's record goes. Nothing it -/// ran is touched; `first`, the first of `left`, says what was left. +/// ran is touched; `first`, the first of `left`, says what was left. The +/// home's retired tombstone is written only for [`Worker::Gone`], whose +/// runtime is confirmed gone: a worker that keeps its record, or one that +/// forgot it without stopping it, proves nothing. fn forget( home: &Path, events: &mut Events<'_>, @@ -400,7 +428,7 @@ fn forget( let left = match (here, worker) { // the record of a Sandbox on a machine whose calls run in this // process is the folder removed below - (true, _) | (_, Worker::Skip) => None, + (true, _) | (_, Worker::Skip | Worker::Gone) => None, _ => on_machine( home, events, @@ -422,7 +450,10 @@ fn forget( let _sandbox = record::lock(home, name)?; direct::release(home, &record); } - let unretired = retire_home(home, &record, &dir); + let unretired = match worker { + Worker::Gone => retire_home(home, &record, &dir), + Worker::Ask | Worker::Skip => None, + }; remove_folder(&dir)?; done(events); let kept = left.map(|left| { @@ -481,14 +512,37 @@ fn stopped_on(home: &Path, record: &SandboxRecord, force: bool) -> Result