Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions api/crates/layerfs-api/src/store/repair_mount_holders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion api/crates/layerfs-cli/tests/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const CALLS: &[(&[&str], &str, &str)] = &[
(
&["store", "repair-mount-holders"],
"layerfs store repair-mount-holders <project> <branch> <action>",
"list, preview, settle, release or audit mount holders (operator)",
"list, preview, settle, release, settle_legacy or audit mount holders (operator)",
),
];

Expand Down
97 changes: 97 additions & 0 deletions control/crates/layerfs-control/tests/store_repair_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>| {
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();
}
16 changes: 14 additions & 2 deletions control/crates/layerfs-sandbox/src/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ pub fn create(
home: &Path,
events: &mut Events<'_>,
request: &CreateRequest,
) -> Result<Created, SdkRefusal> {
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<Created, SdkRefusal> {
let home = home.to_path_buf();
let project = request.project.as_str();
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion control/crates/layerfs-sandbox/src/family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
70 changes: 40 additions & 30 deletions control/crates/layerfs-sandbox/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ pub fn started(directory: &Path) -> Result<Option<DaemonRecord>> {
/// 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],
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions control/crates/layerfs-sandbox/src/launch/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -77,7 +77,7 @@ pub fn start(
};
let id = host.sandbox()?;
start_named(
(host, platform),
(host, platform, place),
directory,
id,
project,
Expand All @@ -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,
Expand Down Expand Up @@ -243,7 +243,7 @@ fn start_named(
crate::files::save_sandbox(
&state.directory,
&state,
(host, platform),
(host, platform, place),
controller.private,
daemon.public,
instance,
Expand Down
Loading