diff options
Diffstat (limited to 'src/pipeline/publish.rs')
| -rw-r--r-- | src/pipeline/publish.rs | 138 |
1 files changed, 42 insertions, 96 deletions
diff --git a/src/pipeline/publish.rs b/src/pipeline/publish.rs index f8efeff..7340e53 100644 --- a/src/pipeline/publish.rs +++ b/src/pipeline/publish.rs @@ -1,11 +1,3 @@ -//! Publishing and serving the index (§9). -//! -//! Publishing is the only externally visible step, and it is atomic and -//! serialized per repo (I4). The served database is a materialized view of the -//! DB: we render it in scratch space beside the live file, fsync, commit the DB, -//! then `rename` over the real one. A crash anywhere in there leaves the *old* -//! index intact, and [`reconcile`] re-materializes from the DB on startup. - use std::collections::BTreeSet; use std::path::{Path, PathBuf}; use std::process::Command; @@ -17,27 +9,15 @@ use crate::error::{Error, Result}; use crate::pipeline::{Collect, Ctx}; use crate::time; -/// One writer to the served repo at a time. -/// -/// This is *the* serialization guarantee for the index (§8.2, §9, I4): a publish -/// renders a new pacman database in scratch space, commits the DB, then renames -/// over the live one, and a second writer interleaving anywhere in that sequence -/// would produce an index that matches neither DB state. It is held by -/// [`publish`] for the whole of it — staging, render, DB commit, rename, sign — -/// and by [`withdraw_package`] for the same reason. With -/// `max_concurrent_builds > 1` it is what stops two workers racing on one file. -/// -/// It is a process-local mutex, which is exactly the guarantee the design asks -/// for: one `aur-build` process owns one repo directory. Two processes sharing -/// a directory is not a configuration this system supports. +/// One writer to the served repo at a time — *the* serialization guarantee for +/// the index (§8.2, §9, I4). A publish renders a new database in scratch space, +/// commits the DB, then renames over the live one; a second writer interleaving +/// anywhere in that sequence produces an index matching neither DB state. Held by +/// [`publish`] and [`withdraw_package`] for the whole sequence; [`reconcile`] skips +/// it because it runs before the dispatcher exists. /// -/// [`reconcile`] rewrites the index without it because it runs at startup, -/// before the dispatcher exists, when there is nothing to race with. -/// -/// A panic while holding it is recovered from rather than propagated: atomic -/// publish means a poisoned lock cannot have left a half-written database — the -/// live file is whatever was last renamed into place — so clearing the poison -/// continues instead of wedging the queue forever. +/// A panic while holding it is recovered rather than propagated: atomic publish +/// means a poisoned lock cannot have left a half-written database. static REPO_LOCK: Mutex<()> = Mutex::new(()); fn lock_repo() -> std::sync::MutexGuard<'static, ()> { @@ -61,7 +41,7 @@ pub fn index_world(db: &Db) -> Result<Vec<WorldMember>> { } /// Package files that belong in the served index right now, plus the extra ones -/// being published (which are not in the DB yet when the new index is rendered). +/// being published (not in the DB yet when the new index is rendered). fn index_artifacts(db: &Db, extra: &[PathBuf]) -> Result<Vec<PathBuf>> { let mut out: Vec<PathBuf> = Vec::new(); for p in db::index_packages(db)? { @@ -77,8 +57,7 @@ fn index_artifacts(db: &Db, extra: &[PathBuf]) -> Result<Vec<PathBuf>> { /// Publish the collected artifacts of a build. Returns the new release id. /// -/// Serialized against every other writer of the index by [`REPO_LOCK`], which -/// is held for the whole function. +/// Serialized against every other writer of the index by [`REPO_LOCK`]. pub fn publish(ctx: &Ctx) -> Result<i64> { let _guard = lock_repo(); let collect = Collect::load(&ctx.collect_path)?; @@ -91,13 +70,11 @@ pub fn publish(ctx: &Ctx) -> Result<i64> { // Artifacts are copied into the served tree under their real filenames. let mut staged: Vec<(PathBuf, PathBuf)> = Vec::new(); - // Set once the render starts, so the failure path below can sweep the scratch - // directory even when the publish died after repo-add returned. + // Set once the render starts, so the failure path can sweep the scratch + // directory even if the publish died after repo-add returned. let mut scratch_dir: Option<PathBuf> = None; - // Files this publish created that were not there before. If the publish does - // not land, they are orphans: no index names them and no consumer will ever - // ask for them (§6 m-16). A file that already existed belongs to the release - // that is still live, so it is never on this list. + // Files this publish created that were not there before: if the publish does + // not land they are orphans no index names (§6 m-16). let mut fresh: Vec<PathBuf> = Vec::new(); for p in &collect.packages { let from = PathBuf::from(&p.path); @@ -122,25 +99,21 @@ pub fn publish(ctx: &Ctx) -> Result<i64> { } let outcome = (|| -> Result<i64> { - // 1. render the new database and fsync it. The live one is untouched - // until step 3's rename. + // 1. render the new database; the live one is untouched until step 3. let db_name = ctx.cfg.repo_db_name(); let final_db = arch_dir.join(format!("{db_name}.tar.gz")); let mut pkgs = index_artifacts(&ctx.db, &staged.iter().map(|(d, _)| d.clone()).collect::<Vec<_>>())?; - // Deduplicate: the same package name must appear once, newest wins. pkgs = dedupe_by_name(pkgs); let rendered = render_db(&ctx.cfg, &arch_dir, "publish", &pkgs, &mut scratch_dir)?; - // Fault injection so I4 can be tested for real: die between the render - // and the rename, and the old index must still be what consumers get. - // Compiled out of a production build (§6 M-9). + // Fault injection so I4 can be tested for real (§6 M-9). #[cfg(feature = "test-support")] if ctx.cfg.hooks.publish_fault.as_deref() == Some("before-rename") { return Err(Error::other("injected fault: before-rename")); } - // 2. commit the DB: release + package rows + the accepted pointer, in one - // transaction, with the pointer moved by compare-and-swap. + // 2. commit the DB: release + package rows + the accepted pointer, in + // one transaction, with the pointer moved by compare-and-swap. let release_id = commit_release(ctx, &collect)?; // 3. atomic swap: rename over the real database, then sign the new bytes. @@ -168,9 +141,8 @@ pub fn publish(ctx: &Ctx) -> Result<i64> { })(); if outcome.is_err() { - // The publish did not land, so nothing in the served tree may look like it - // did: the bytes this call copied in are removed again (m-16). Files that - // were already there belong to the release still being served and stay. + // Nothing in the served tree may look like a publish that did not land + // (m-16); files already there belong to the release still served. for p in &fresh { let _ = std::fs::remove_file(p); } @@ -222,9 +194,7 @@ fn commit_release(ctx: &Ctx, collect: &Collect) -> Result<i64> { .and_then(|b| b.world_snapshot_id); let repo_id = db::repo_id(&ctx.db)?; // The compare-and-swap baseline is the release the *approval* was taken - // against, not whatever the pointer happens to say now. If they differ, - // someone accepted a newer version while we were building, and publishing - // this one would roll it back. + // against, not whatever the pointer says now. let expected_base: Option<i64> = match ctx.build.kind.as_str() { "manual_rebuild" => ctx.build.release_id, _ => ctx @@ -235,16 +205,15 @@ fn commit_release(ctx: &Ctx, collect: &Collect) -> Result<i64> { }; ctx.db.writer.tx(move |tx| { - // Compare-and-swap on the accepted pointer, inside the write - // transaction: two publishes cannot both land (I4). + // Compare-and-swap inside the write transaction (I4). let actual_base = db::accepted_pointer_in(tx, pkgbase_id)?; if actual_base != expected_base { return Err(Error::conflict( "accepted pointer moved while this build was running; refusing to publish", )); } - // Retire whatever the index held for these names *before* inserting the - // new rows: the index holds exactly the accepted pointer, nothing else. + // The index holds exactly the accepted pointer, so what it held for these + // names goes before the new rows go in. db::remove_index_packages_in(tx, &names)?; let release_id = db::insert_release_in( tx, @@ -280,10 +249,8 @@ fn commit_release(ctx: &Ctx, collect: &Collect) -> Result<i64> { }) } -/// Remove scratch directories a crash left behind. [`render_db`] cleans up after -/// itself on every path it controls; these are the ones it did not get to, and -/// they are unambiguous — the leading dot means nothing a consumer asks for, and -/// only a render creates one. +/// Remove scratch directories a crash left behind: the leading dot means nothing +/// a consumer asks for, and only a render creates one. pub fn sweep_render_dirs(arch_dir: &Path) { let Ok(entries) = std::fs::read_dir(arch_dir) else { return; @@ -303,9 +270,8 @@ struct Rendered { } impl Rendered { - /// The publish point: rename the rendered database over the live one, fsync - /// the directory entry, then drop the scratch directory. Before the rename a - /// consumer still gets the old index; after it, the new one. + /// The publish point: rename over the live database, fsync the directory + /// entry, then drop the scratch directory. fn swap_into(self, final_db: &Path) -> Result<()> { std::fs::rename(&self.db, final_db)?; fsync_dir(&self.arch_dir)?; @@ -317,18 +283,12 @@ impl Rendered { /// Render a fresh database with repo-add, ready to be renamed into place. /// /// repo-add refuses any database argument that is not a name pacman itself would -/// accept (`<repo>.db.tar.gz` and friends) — a `.tmp` suffix is a usage error to -/// it, not a convention. So the render cannot happen at `<db>.tar.gz.tmp` beside -/// the live file, which is what this code used to do: every publish of a real -/// package failed the moment it met a real repo-add, while 200+ tests stayed -/// green because the shim accepted any filename. It renders instead into a -/// scratch directory *under* the served directory, under the exact name a -/// consumer will download. Same filesystem, so the caller's `rename` is still the -/// atomic swap I4 requires, and the live database is never the file repo-add is -/// writing into. +/// accept, so the render happens in a scratch directory *under* the served +/// directory, under the exact name a consumer will download. Same filesystem, so +/// the caller's `rename` is still the atomic swap I4 requires. /// -/// `scratch` is filled in as soon as the directory exists, so the caller can -/// sweep it even if this function is never reached again. +/// `scratch` is filled in as soon as the directory exists, so the caller can sweep +/// it even if this function is never reached again. fn render_db( cfg: &Config, arch_dir: &Path, @@ -343,8 +303,6 @@ fn render_db( *scratch = Some(dir.clone()); let db = dir.join(format!("{}.tar.gz", cfg.repo_db_name())); let rendered = Rendered { db, dir, arch_dir: arch_dir.to_path_buf() }; - // Anything that can still fail lives here, so the scratch directory does not - // outlive the attempt on the error path. let attempt = (|| -> Result<()> { run_repo_add(cfg, &rendered.db, pkgs)?; fsync_file(&rendered.db)?; @@ -358,12 +316,8 @@ fn render_db( Ok(rendered) } -/// `repo-add -R <db> <pkgs...>`. -/// -/// Deliberately *not* `-s`: repo-add would sign the database into whatever -/// directory it was rendered in, which is scratch space that gets deleted, and -/// the hub signs the live file itself in [`sign`] — one key, one code path, and -/// the signature is always over the bytes that were renamed into place (§9). +/// `repo-add -R <db> <pkgs...>`, deliberately *not* `-s`: repo-add would sign into +/// scratch space that gets deleted. The hub signs the live file itself in [`sign`]. fn run_repo_add(cfg: &Config, db_path: &Path, pkgs: &[PathBuf]) -> Result<()> { let mut cmd = Command::new(&cfg.repo_add_bin); cmd.arg("-R"); @@ -390,10 +344,8 @@ fn sign(cfg: &Config, path: &Path) -> Result<()> { sign_to(cfg, path, Path::new(&sig)) } -/// `gpg --detach-sign` over `path`, written to `sig_out` atomically. Shared by -/// publish (the database) and ingest (the packages): one key, one code path, -/// and the key never leaves the hub — a build sandbox signs nothing (§8, -/// remote-builder design). +/// `gpg --detach-sign` over `path`, written to `sig_out` atomically. The key never +/// leaves the hub — a build sandbox signs nothing (§8). pub(crate) fn sign_to(cfg: &Config, path: &Path, sig_out: &Path) -> Result<()> { if cfg.signing_key_id.is_empty() { return Ok(()); @@ -416,11 +368,8 @@ pub(crate) fn sign_to(cfg: &Config, path: &Path, sig_out: &Path) -> Result<()> { Ok(()) } -/// fsync, and *say so* when it fails (m-7). The whole point of writing to a -/// scratch file, fsyncing, and renaming is the promise that a consumer never -/// sees a half-written database; swallowing the error made that a comment rather -/// than a guarantee — on a filesystem that refuses `fsync` (full, read-only, a -/// network mount that dropped), the rename still "succeeded". +/// fsync, and *say so* when it fails (m-7). Swallowing the error would make the +/// write-scratch-fsync-rename promise a comment rather than a guarantee. fn fsync_file(p: &Path) -> Result<()> { let f = std::fs::File::open(p) .map_err(|e| Error::other(format!("open {} for fsync: {e}", p.display())))?; @@ -439,8 +388,7 @@ fn fsync_dir(p: &Path) -> Result<()> { /// Keep one entry per package name; the newest file wins. The name comes from -/// `.PKGINFO` inside the tarball, not from the filename, because package names -/// contain dashes. +/// `.PKGINFO`, not the filename, because package names contain dashes. fn dedupe_by_name(paths: Vec<PathBuf>) -> Vec<PathBuf> { let mut seen: BTreeSet<String> = BTreeSet::new(); let mut out: Vec<PathBuf> = Vec::new(); @@ -460,10 +408,8 @@ fn dedupe_by_name(paths: Vec<PathBuf>) -> Vec<PathBuf> { /// The package name a built artifact declares, read from its own `.PKGINFO`. /// -/// This is the real parser in `crate::pkginfo`, not the shim harness's: the -/// name that goes into the index must come from the same read of the bytes that -/// the pipeline signs, and a second parser in the test scaffolding is one -/// refactor away from disagreeing about what a package calls itself (§6 M-4). +/// The name that goes into the index must come from the same read of the bytes the +/// pipeline signs, so this uses the real parser (§6 M-4). fn package_name(path: &Path) -> Option<String> { let bytes = std::fs::read(path).ok()?; let info = crate::pkginfo::pkginfo_text(&bytes).ok()?; |
