diff options
| author | aur-build <aur-build@localhost> | 2026-11-18 18:28:16 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-18 18:28:16 +0000 |
| commit | fdd004c50de6131be193044ae40424d691425a81 (patch) | |
| tree | 87f3ce369f943d6ac077b7af1252850d86082422 /src | |
| parent | 2a8ce53f6754217c8e24f92a7b13190b68708d7a (diff) | |
| download | aur-build-fdd004c50de6131be193044ae40424d691425a81.tar.gz aur-build-fdd004c50de6131be193044ae40424d691425a81.zip | |
db, watch: two N+1s and a version rule with one owner
m-17. `watch::staleness::dependents_of` walked the index and asked for each
package's recipe by name — one query per package, on the path that runs after
every publish, with N equal to the size of the index. `db::index_recipes` returns
the whole table in one query now. While there: a recipe that fails to parse is no
longer treated as "does not depend on this package". An unreadable srcinfo used
to hide a dependent from staleness by returning `None` into an `unwrap_or(false)`;
now it is a reason to look.
`db::builds::requeue_blocked` asked "is this name in the index?" once per
dependency of every blocked build, inside the dispatcher's hot loop. The set of
in-index names is read once per pass and tested in memory. Same answer, one
query.
m-18. `approved_but_unbuilt` assembled the version a human reads with two
`CASE WHEN epoch='0'` expressions in SQL — a second, independent copy of the
`epoch:pkgver-pkgrel` rule that `pkgver::full_version` owns. Two copies of a
formatting rule is how the Attention page ends up describing versions
differently from every other page. The query returns the parts; Rust formats
them. A guard test fails if version-string concatenation reappears in SQL.
Diffstat (limited to 'src')
| -rw-r--r-- | src/db/builds.rs | 20 | ||||
| -rw-r--r-- | src/db/queries.rs | 81 | ||||
| -rw-r--r-- | src/watch/staleness.rs | 23 |
3 files changed, 83 insertions, 41 deletions
diff --git a/src/db/builds.rs b/src/db/builds.rs index 4244034..072836b 100644 --- a/src/db/builds.rs +++ b/src/db/builds.rs @@ -265,18 +265,20 @@ pub fn requeue_blocked(db: &Db) -> Result<usize> { .collect::<rusqlite::Result<Vec<(String, String)>>>()?; out }; + // The index membership check is one query for the whole pass, not one + // per dependency of every blocked build (m-17): the old shape was an N+1 + // whose N grew with the backlog, and it ran on the dispatcher's hot loop. + let in_index: std::collections::HashSet<String> = { + let mut stmt = tx.prepare("SELECT name FROM package WHERE state='in_index'")?; + let names = stmt + .query_map([], |r| r.get::<_, String>(0))? + .collect::<rusqlite::Result<Vec<String>>>()?; + names.into_iter().collect() + }; let mut n = 0; for (id, blocked_on) in rows { let waiting: Vec<String> = serde_json::from_str(&blocked_on).unwrap_or_default(); - let satisfied = waiting.iter().all(|name| { - tx.query_row( - "SELECT count(*) FROM package WHERE name=?1 AND state='in_index'", - params![name], - |r| r.get::<_, i64>(0), - ) - .unwrap_or(0) - > 0 - }); + let satisfied = waiting.iter().all(|name| in_index.contains(name)); if satisfied && !waiting.is_empty() { tx.execute( "UPDATE build SET state='queued', blocked_on_json='[]', updated_at=?1 WHERE id=?2", diff --git a/src/db/queries.rs b/src/db/queries.rs index 66bab73..62c1045 100644 --- a/src/db/queries.rs +++ b/src/db/queries.rs @@ -928,18 +928,26 @@ pub fn served_vs_offered_pkgvers(db: &Db) -> Result<Vec<(String, String, String) /// The `srcinfo_json` of the recipe the index's copy of a package was built /// from — what its dependency list actually said. -pub fn recipe_json_of_index_package(db: &Db, name: &str) -> Result<Option<String>> { +/// Every in-index package's recipe JSON, as (name, srcinfo_json). +/// +/// One query, not one per package (m-17). `watch::staleness::dependents_of` +/// wants the whole table: asking for each package by name was an N+1 whose N is +/// the size of the index, on a code path that runs after every publish. +pub fn index_recipes(db: &Db) -> Result<Vec<(String, String)>> { db.read(|c| { - Ok(c.query_row( - "SELECT s.srcinfo_json FROM package pk + let mut stmt = c.prepare( + "SELECT pk.name, s.srcinfo_json FROM package pk JOIN release r ON r.id = pk.release_id JOIN candidate cnd ON cnd.id = r.candidate_id JOIN snapshot s ON s.id = cnd.snapshot_id - WHERE pk.name = ?1 AND pk.state='in_index'", - params![name], - |r| r.get::<_, String>(0), - ) - .optional()?) + WHERE pk.state='in_index'", + )?; + let rows = stmt.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))?; + let mut out = Vec::new(); + for r in rows { + out.push(r?); + } + Ok(out) }) } @@ -1566,16 +1574,20 @@ pub struct Unbuilt { pub fn approved_but_unbuilt(db: &Db) -> Result<Vec<Unbuilt>> { db.read(|c| { + // The version strings are assembled in Rust by `pkgver::full_version`, + // not by a CASE expression here (m-18): the epoch-omitted form is a rule + // of ours, and a second copy of it in SQL is a second rule that can + // disagree with the first. let mut stmt = c.prepare( "SELECT p.name, - (SELECT (CASE WHEN c.epoch='0' THEN '' ELSE c.epoch||':' END) || c.pkgver || '-' || c.pkgrel - FROM candidate c WHERE c.id = b.candidate_id), - COALESCE((SELECT (CASE WHEN r.epoch='0' THEN '' ELSE r.epoch||':' END) || r.pkgver || '-' || r.pkgrel - FROM release r JOIN pkgbase pb ON pb.current_accepted_release_id = r.id - WHERE pb.id = p.id AND r.state='published'), ''), + cnd.epoch, cnd.pkgver, cnd.pkgrel, + ser.epoch, ser.pkgver, ser.pkgrel, COALESCE(b.error, ''), b.id FROM build b JOIN pkgbase p ON p.id = b.pkgbase_id + LEFT JOIN candidate cnd ON cnd.id = b.candidate_id + LEFT JOIN release ser ON ser.id = p.current_accepted_release_id + AND ser.state='published' WHERE b.state = 'failed' AND b.candidate_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM release r WHERE r.candidate_id = b.candidate_id) @@ -1585,17 +1597,44 @@ pub fn approved_but_unbuilt(db: &Db) -> Result<Vec<Unbuilt>> { ORDER BY p.name", )?; let rows = stmt.query_map([], |r| { - Ok(Unbuilt { - pkgbase: r.get(0)?, - approved: r.get(1)?, - serving: r.get(2)?, - error: r.get(3)?, - build_id: r.get(4)?, - }) + Ok(( + r.get::<_, Option<String>>(0)?, + r.get::<_, Option<String>>(1)?, + r.get::<_, Option<String>>(2)?, + r.get::<_, Option<String>>(3)?, + r.get::<_, Option<String>>(4)?, + r.get::<_, Option<String>>(5)?, + r.get::<_, Option<String>>(6)?, + r.get::<_, String>(7)?, + r.get::<_, String>(8)?, + )) })?; let mut out = Vec::new(); for r in rows { - out.push(r?); + let (pkgbase, ae, av, ar, se, sv, sr, error, build_id) = r?; + let (pkgbase, av, ar) = match (pkgbase, av, ar) { + (Some(p), Some(v), Some(rel)) => (p, v, rel), + // The join guarantees a pkgbase, and a failed build with a + // candidate always has one; a missing version means the schema + // was violated, so say so rather than show a blank. + _ => { + return Err(crate::error::Error::Db( + "failed build row without a candidate version".into(), + )) + } + }; + out.push(Unbuilt { + pkgbase, + approved: crate::pkgver::full_version(&ae.unwrap_or_default(), &av, &ar), + serving: match (sv, sr) { + (Some(v), Some(rel)) => { + crate::pkgver::full_version(&se.unwrap_or_default(), &v, &rel) + } + _ => String::new(), + }, + error, + build_id, + }); } Ok(out) }) diff --git a/src/watch/staleness.rs b/src/watch/staleness.rs index cc7242c..e052b26 100644 --- a/src/watch/staleness.rs +++ b/src/watch/staleness.rs @@ -102,20 +102,21 @@ pub fn downgrades(db: &Db) -> Result<Vec<Stale>> { /// its recipe dependencies. pub fn dependents_of(db: &Db, name: &str) -> Result<Vec<String>> { let mut out: Vec<String> = Vec::new(); - for p in db::index_packages(db)? { - if p.name == name { + for (pkgname, json) in db::index_recipes(db)? { + if pkgname == name { continue; } - let depends = db::recipe_json_of_index_package(db, &p.name)? - .and_then(|j| serde_json::from_str::<crate::db::model::Recipe>(&j).ok()) - .map(|r| { - crate::pkgver::dep_names(&r.depends) - .into_iter() - .any(|d| d == name) - }) - .unwrap_or(false); + // A recipe we cannot read is not a recipe that does not depend on + // `name`: treat the unreadable as a reason to look, so a malformed + // srcinfo cannot silently hide a dependent package from staleness. + let depends = match serde_json::from_str::<crate::db::model::Recipe>(&json) { + Ok(r) => crate::pkgver::dep_names(&r.depends) + .into_iter() + .any(|d| d == name), + Err(_) => true, + }; if depends { - out.push(p.name); + out.push(pkgname); } } Ok(out) |
