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
69 changes: 27 additions & 42 deletions api/crates/layerfs-api/src/store/repair_mount_holders.rs
Original file line number Diff line number Diff line change
@@ -1,65 +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. List and preview write nothing: they
//! answer the typed holder view, preview with what release would do for each
//! holder; 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)
}
62 changes: 46 additions & 16 deletions api/crates/layerfs-api/tests/gc_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,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)),
];
Expand All @@ -892,13 +891,12 @@ 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("audit"), Some(&SdkValue::Null));
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| {
Expand Down Expand Up @@ -932,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()))
Expand All @@ -952,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
Expand Down Expand Up @@ -993,7 +1001,7 @@ 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:?}"
);

Expand Down Expand Up @@ -1030,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(
Expand Down
15 changes: 6 additions & 9 deletions api/crates/layerfs-cli/tests/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const CALLS: &[(&[&str], &str, &str)] = &[
),
(
&["store", "repair-mount-holders"],
"layerfs store repair-mount-holders <name> <project> <branch> <action>",
"layerfs store repair-mount-holders <project> <branch> <action>",
"list, preview, settle, release or audit mount holders (operator)",
),
];
Expand Down Expand Up @@ -112,24 +112,22 @@ 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",
],
&[
"store",
"repair-mount-holders",
"shop",
"app",
"shop/app",
"main",
"preview",
"--force",
"--prune",
],
];
for words in wrong {
Expand All @@ -154,8 +152,7 @@ fn well_formed_lines_reach_the_library() {
&[
"store",
"repair-mount-holders",
"shop",
"app",
"shop/app",
"main",
"preview",
],
Expand Down
Loading