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
8 changes: 8 additions & 0 deletions api/crates/layerfs-api/tests/doctor_targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,14 @@ fn a_sqlite_project_without_a_manifest_fails_project_doctor() {
&& f.get_text("severity") == Some("error")),
"full={full}: {report:?}"
);
// the record's failure hides no other check: the Store's list is
// still checked
let rows = list(report.get("tree").unwrap(), "checks");
assert!(
rows.iter()
.any(|row| row.get_text("check") == Some("project.listed")),
"full={full}: {report:?}"
);
}

// PostgreSQL project records hold no manifest; project.record still passes.
Expand Down
23 changes: 23 additions & 0 deletions api/crates/layerfs-api/tests/project_family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,12 +834,35 @@ fn move_skips_unreadable_project_record_and_still_repairs_incomplete_ones() {
let damaged_path = paths::project_record(&home, "shop", "damaged");
std::fs::write(&damaged_path, b"{").unwrap();
write_legacy_stub(11111);
// a record of a stack the Store does not hold cannot be rebuilt: the
// move still ends, and says so in log/repair.jsonl
let orphan_path = paths::project_record(&home, "shop", "orphan");
home::private_dir(orphan_path.parent().unwrap()).unwrap();
write_record(
&orphan_path,
&SdkValue::map([
("v", SdkValue::Int(1)),
("name", t("orphan")),
("stack", t("0101010101010101010101010101010101")),
("created", SdkValue::Int(1)),
]),
)
.unwrap();

ok(
&home,
"store.move",
&[("store", t("shop")), ("machine", t("box"))],
);
let repair_log = std::fs::read_to_string(paths::log_dir(&home).join("repair.jsonl")).unwrap();
assert!(
repair_log
.lines()
.any(|line| line.contains("\"project\":\"orphan\"")
&& line.contains("\"call\":\"store.move\"")),
"{repair_log}"
);
assert!(!repair_log.contains("\"legacy\""), "{repair_log}");
let store_rec = layerfs_api::home::read_record(&paths::store_record(&home, "shop"))
.unwrap()
.unwrap();
Expand Down
34 changes: 24 additions & 10 deletions control/crates/layerfs-control/src/doctor/project_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,22 @@ pub(super) fn run(probe: &Probe<'_>, node: &mut Node) {
});
match recorded {
Ok((record, Some(project))) => {
let needs_manifest = matches!(record.store_ref(), Ok(StoreRef::Sqlite { .. }))
&& project
.op()
.is_none_or(|op| op.name() != layerfs_store::project::op::INIT);
if needs_manifest && !layerfs_store::backup::restored::complete(&project) {
if opened_here(probe.home, &record, &project)
&& !layerfs_store::backup::restored::complete(&project)
{
checks.fail(
RECORD,
format!(
"project {handle}'s record has no complete manifest (a Sandbox cannot open it)"
"project {handle}'s record has no complete manifest (a Sandbox on this computer cannot open it)"
),
);
} else {
checks.pass(RECORD, "the project and its Store are on record");
let stack = listed(probe, (&record, &project), handle, &mut checks);
if let (Some(stack), true) = (stack, probe.full) {
data(probe, &record, stack, &mut checks);
}
}
// the Store's list and data are checked whatever the record holds
let stack = listed(probe, (&record, &project), handle, &mut checks);
if let (Some(stack), true) = (stack, probe.full) {
data(probe, &record, stack, &mut checks);
}
}
Ok((_, None)) => {
Expand All @@ -89,6 +88,21 @@ pub(super) fn run(probe: &Probe<'_>, node: &mut Node) {
checks.fill(node, probe.checks, probe.budget.expired());
}

/// Whether a Sandbox of `project` opens its Store from this home's record:
/// a sqlite Store whose calls run in this process, with no Init under way.
/// A worker's Sandbox reads the entry that worker keeps, which its own Init
/// wrote complete (#749), so a host record without one is not a fault there.
fn opened_here(home: &Path, store: &StoreRecord, project: &ProjectRecord) -> bool {
matches!(store.store_ref(), Ok(StoreRef::Sqlite { .. }))
&& project
.op()
.is_none_or(|op| op.name() != layerfs_store::project::op::INIT)
&& matches!(
place::on_machine_read(home, &store.machine().unwrap_or_default()),
Ok(Executor::Local)
)
}

/// `project.listed`: the Store's own list holds the project its record
/// names, by its stack id, or by its name for a record without one. The
/// stack the list names, when it does.
Expand Down
72 changes: 40 additions & 32 deletions control/crates/layerfs-sandbox/src/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,42 +132,23 @@ fn installed(host: &Host) -> Result<OpenedStore, SdkRefusal> {
let name = &host.project;
let identity = layerfs_store::identity::identity(&host.open_args())?;
layerfs_store::identity::sqlite_only(&identity, "a Sandbox of a Store file")?;
let project = match layerfs_store::project::discovery::project(
let found = layerfs_store::project::discovery::project(
Some(&host.home),
Some(&host.store),
Some(&host.project),
) {
Ok(project) => project,
Err(refusal)
if refusal.code == "NoSuchProject"
&& refusal.message == "no complete project manifest" =>
{
let repaired = host
.open_args()
.store_path()
.ok()
.filter(|path| path.is_file())
.map(|path| {
layerfs_store::backup::restored::repair_project(
&host.home,
&host.store,
&host.project,
identity,
&path,
)
})
.transpose()?
.unwrap_or(false);
if !repaired {
return Err(refusal);
}
layerfs_store::project::discovery::project(
Some(&host.home),
Some(&host.store),
Some(&host.project),
)?
);
// a record without a complete `manifest` and `installed` (#749: one an
// older worker Init wrote) is rebuilt from the Store file here, once;
// a repair that cannot complete it leaves the refusal as it was
let project = match found {
Ok(project) if project.installed.is_some() => project,
Err(refusal) if !matches!(refusal.code.as_str(), "NoSuchProject" | "Failed") => {
return Err(refusal)
}
Err(refusal) => return Err(refusal),
found => match repaired(host, identity) {
Some(project) => project,
None => found?,
},
};
let Some(installed) = project.installed.clone() else {
return Err(refused(
Expand All @@ -178,6 +159,33 @@ fn installed(host: &Host) -> Result<OpenedStore, SdkRefusal> {
Ok(OpenedStore::File(Box::new((project, installed))))
}

/// The project of `host`, its record completed from the Store file this
/// kernel holds ([`restored::repair_project`]); `None` when there is no such
/// file, the record needs no repair or the repair does not complete it.
///
/// [`restored::repair_project`]: layerfs_store::backup::restored::repair_project
fn repaired(
host: &Host,
identity: layerfs_store::identity::Identity,
) -> Option<layerfs_store::project::discovery::Project> {
let file = host.open_args().store_path().ok().filter(|p| p.is_file())?;
let record = layerfs_store::backup::restored::repair_project(
&host.home,
&host.store,
&host.project,
identity,
&file,
)
.ok()??;
layerfs_store::project::discovery::project_of(
Some(&host.home),
Some(&host.store),
Some(&host.project),
record.to_record(),
)
.ok()
}

/// 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
Expand Down
16 changes: 11 additions & 5 deletions control/crates/layerfs-sandbox/tests/installed_repair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,17 @@ fn sandbox_create_repairs_an_incomplete_sqlite_project_record() {
message: Message::Absent,
started: None,
};
let err = lifecycle::create(&host, None).unwrap_err();
assert_ne!(
err.code, "NoSuchProject",
"must heal manifest instead of failing NoSuchProject: {err:?}"
);
// the repair happens before launch; a host that can launch a daemon
// starts one, which is removed again
match lifecycle::create(&host, None) {
Ok(_) => {
lifecycle::close(&host, true, &[]).expect("the started Sandbox is removed");
}
Err(err) => assert_ne!(
err.code, "NoSuchProject",
"must heal manifest instead of failing NoSuchProject: {err:?}"
),
}
let repaired = ProjectRecord::read(&home, "shop", "api").unwrap().unwrap();
assert_eq!(repaired.created(), Some(&SdkValue::Int(55)));
assert!(restored::complete(&repaired));
Expand Down
27 changes: 3 additions & 24 deletions control/crates/layerfs-store/src/backup/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use super::family::write_link;
use super::progress::Shown;
use super::record::{BucketRecord, StoreLink};
use super::restored::{records, Found, Main, Restored};
use super::restored::{records, Found, Restored};
use crate::get::hex_text;
use crate::identity::{Identity, Kind};
use crate::intent;
Expand All @@ -12,7 +12,6 @@ use crate::Related;
use layerfs_bridge::control::{SdkRefusal, SdkValue};
use layerfs_bridge::sdk_values::hex;
use layerfs_bucket::backup as remote;
use layerfs_history::{HistoryName, NamedSnapshot};
use layerfs_home::events::Events;
use layerfs_home::home::flock::{try_lock, unlock};
use layerfs_home::home::{self, paths};
Expand Down Expand Up @@ -449,30 +448,10 @@ fn projects_in(home: &Path, store: &str, record: &StoreRecord, file: &Path) -> R
..Default::default()
};
crate::service::with_service(&open, |service| {
// the project's own line of Layers: `main`, through its name index
let main =
HistoryName::new(layerfs_store_service::history::branch::MAIN).map_err(failed)?;
// each project's own line of Layers: `main`, through its name index
let projects = service
.store
.published(|history| {
let mut found = Vec::new();
for stack in layerfs_project::store::projects(history)? {
let snapshot = match history.branch_snapshot_named(&stack.name, &main)? {
NamedSnapshot::Found(snapshot) => Some(Main {
stack: snapshot.branch.stack,
branch: snapshot.branch.id,
root: snapshot.base_root.to_bytes(),
}),
NamedSnapshot::NoStack | NamedSnapshot::NoBranch => None,
};
found.push(Found {
name: stack.name.as_str().to_owned(),
stack: SdkValue::text(hex(&stack.id.to_bytes())),
main: snapshot,
});
}
Ok(found)
})
.published(super::restored::found_in)
.map_err(failed)?;
Ok(Listing {
projects,
Expand Down
Loading