From dd3dafcd7ab8e994b4ec6537ec811e509a43dfaf Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Tue, 24 Nov 2026 11:38:55 +0100 Subject: [PATCH] feat(pool): a lane's need names its builds (ADR-0119 point 3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Need::builds`: of its memory, the most the lane's build job took. The daemon's estimate from the main tree takes the build series' peak; a node asks each lane's daemon for `builds` of `/v1/usage` every round (it sees the lane as one container) and keeps the most in the run, and the coordinator's footprint the most of the last five. The lease step says "of it builds …", `remote_lane_need` logs it. Placement still takes the whole memory; the field is left out at zero, so older peers read the rest. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 5 + crates/devstack-daemon/src/api.rs | 1 + crates/devstack-daemon/src/remote.rs | 16 ++- crates/devstack-daemon/src/usage.rs | 4 + crates/devstack-node/src/coordinator.rs | 2 +- crates/devstack-node/src/node.rs | 102 ++++++++++++++++++ crates/devstack-node/src/policy.rs | 1 + crates/devstack-node/src/runs.rs | 32 ++++++ crates/devstack-node/src/share.rs | 32 +++++- ...ounts-what-it-owns-and-names-its-builds.md | 14 ++- docs/adr/README.md | 2 +- docs/pool.md | 7 +- 12 files changed, 210 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f85cd2ec..3c66aa62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,11 @@ and listed with their assets on the "of it builds …", and the overview lists *Builds* below the groups: the build servers stay after a build so the next one is fast, and were in the lane's number but in no group. +- **A lane's need for the pool names its builds** (ADR-0119). The lease + step says "… memory (of it builds …)" and `remote_lane_need` in + `daemon.log` logs `builds`: the most the lane's build job took, as its + daemon measured it (a node asks each lane's daemon). Placement still takes + the whole memory; a node before this release sends no part. - **main is stopped when only what serves its lanes runs** (ADR-0119). A `singleton` the stop of main keeps for running lanes no longer shows main as running with a Stop button; the stop says what keeps running, and for diff --git a/crates/devstack-daemon/src/api.rs b/crates/devstack-daemon/src/api.rs index 2bd06b61..b0d47f2c 100644 --- a/crates/devstack-daemon/src/api.rs +++ b/crates/devstack-daemon/src/api.rs @@ -866,6 +866,7 @@ fn lane_estimate( let need = devstack_node::share::Need { cpus: e.cpus.max(CPUS), memory: if runs { e.memory.max(MEMORY) } else { MEMORY }, + builds: if runs { e.builds } else { 0 }, disk_repo: e.disk_repo, disk_lane: if counted { e.disk_lane } else { DISK }, gpus: None, diff --git a/crates/devstack-daemon/src/remote.rs b/crates/devstack-daemon/src/remote.rs index f5b3995a..0028bd7f 100644 --- a/crates/devstack-daemon/src/remote.rs +++ b/crates/devstack-daemon/src/remote.rs @@ -581,6 +581,7 @@ pub fn floor_of(n: &devstack_config::model::Need) -> Option 0 { + format!( + " (of it builds {})", + devstack_node::coordinator::gib(n.builds) + ) + } else { + String::new() + }; (spec.step)( 1, &format!( - "lease on {}: {:.1} CPU, {} memory, {} disk ({from})", + "lease on {}: {:.1} CPU, {} memory{builds}, {} disk ({from})", offer.node.name, n.cpus, devstack_node::coordinator::gib(n.memory), diff --git a/crates/devstack-daemon/src/usage.rs b/crates/devstack-daemon/src/usage.rs index 63892c31..0a493088 100644 --- a/crates/devstack-daemon/src/usage.rs +++ b/crates/devstack-daemon/src/usage.rs @@ -267,6 +267,8 @@ pub struct Estimate { pub cpus: f64, /// Bytes: each one's highest over the last ten minutes, summed. pub memory: u64, + /// Bytes: of `memory`, the build job's highest (ADR-0119 point 3). + pub builds: u64, /// Bytes: the tree without `.git` and its state (its sources, /// dependencies and builds), its snapshot, artifacts and logs, and its /// Compose volumes. @@ -508,10 +510,12 @@ impl Sampler { .filter(|(k, _)| *k != GPU_KEY) .map(|(_, s)| s.peak()) .fold((0.0, 0), |(c, m), (pc, pm)| (c + pc, m + pm)); + let builds = inner.series.get("build").map_or(0, |s| s.peak().1); let d = &inner.disk; Estimate { cpus, memory, + builds, disk_lane: tree + d.snapshot + d.artifacts + d.logs + d.volumes.unwrap_or(0), disk_repo: images, } diff --git a/crates/devstack-node/src/coordinator.rs b/crates/devstack-node/src/coordinator.rs index 7d2b92e0..bed7c9c7 100644 --- a/crates/devstack-node/src/coordinator.rs +++ b/crates/devstack-node/src/coordinator.rs @@ -4435,7 +4435,7 @@ mod placement_tests { memory: GIB, disk_repo: 20 * GIB, disk_lane: 10 * GIB, - gpus: None, + ..Need::default() }; // 30 GiB room: enough for a node with the repository's images // (10 + 5 free), not for a cold one (30 + 5). diff --git a/crates/devstack-node/src/node.rs b/crates/devstack-node/src/node.rs index 413e218a..ecdde033 100644 --- a/crates/devstack-node/src/node.rs +++ b/crates/devstack-node/src/node.rs @@ -3319,6 +3319,46 @@ async fn shared_tunnel( } } +/// What the lane's build job takes of its memory now, as its daemon +/// measures it (`builds` of `/v1/usage`, ADR-0119 point 3): the node sees +/// the lane as one container and cannot tell its builds apart. `None` when +/// the daemon does not answer within a few seconds. +async fn lane_builds(d: &DaemonAddr) -> Option { + use http_body_util::BodyExt; + let ask = async { + let tcp = tokio::net::TcpStream::connect(&d.addr).await.ok()?; + let (mut send, conn) = + hyper::client::conn::http1::handshake(hyper_util::rt::TokioIo::new(tcp)) + .await + .ok()?; + tokio::spawn(conn); + let req = hyper::Request::builder() + .uri("/v1/usage") + .header(hyper::header::HOST, d.host()) + .header(hyper::header::AUTHORIZATION, format!("Bearer {}", d.token)) + .body(http_body_util::Empty::::new()) + .ok()?; + let res = send.send_request(req).await.ok()?; + if !res.status().is_success() { + return None; + } + let body = res.into_body().collect().await.ok()?.to_bytes(); + builds_of(&body) + }; + tokio::time::timeout(Duration::from_secs(3), ask) + .await + .ok() + .flatten() +} + +/// `builds.memory` of a daemon's usage report. +fn builds_of(report: &[u8]) -> Option { + serde_json::from_slice::(report) + .ok()? + .pointer("/builds/memory")? + .as_u64() +} + /// Pass `req` to the lane's daemon at `path`, with its token; a tunnel is /// spliced once the daemon switched protocols. async fn pass( @@ -3891,6 +3931,25 @@ async fn background(s: Shared) { let all: Vec<&crate::policy::NodePolicy> = policies.iter().collect(); crate::policy::NodePolicy::strictest(&all).and_then(|p| p.share_percent) }; + // Of that, each lane's builds, as its daemon measures them. + let daemons: Vec<(String, DaemonAddr)> = s + .daemons + .lock() + .unwrap() + .iter() + .filter(|(id, _)| by.contains_key(*id)) + .map(|(id, d)| (id.clone(), d.clone())) + .collect(); + let mut asking = tokio::task::JoinSet::new(); + for (id, d) in daemons { + asking.spawn(async move { (id, lane_builds(&d).await) }); + } + let mut builds = Vec::new(); + while let Some(r) = asking.join_next().await { + if let Ok((id, Some(b))) = r { + builds.push((id, b)); + } + } { // Half of the most this node offers its lanes (ADR-0066). let cap = s.share.most_cpus(cores as f64, percent) / 2.0; @@ -3898,6 +3957,9 @@ async fn background(s: Shared) { for (id, a) in &by { runs.load(id, *a, cap); } + for (id, b) in &builds { + runs.builds(id, *b); + } } let used = by.values().fold(Amount::default(), |a, b| Amount { cpus: a.cpus + b.cpus, @@ -4415,6 +4477,46 @@ pub fn serve_blocking(config: NodeConfig, listen: std::net::SocketAddr) -> Resul mod tests { use super::*; + #[tokio::test] + async fn a_lanes_builds_come_from_its_daemon_with_its_token_and_host() { + // A daemon that answers its token at its loopback host only, as one does. + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = listener.local_addr().unwrap().to_string(); + let port = listener.local_addr().unwrap().port(); + let app = axum::Router::new().route( + "/v1/usage", + get(move |h: hyper::HeaderMap| async move { + let ok = h.get("authorization").is_some_and(|v| v == "Bearer t0k") + && h.get("host") + .is_some_and(|v| *v == *format!("127.0.0.1:{port}")); + if ok { + ( + StatusCode::OK, + r#"{"builds":{"name":"builds","cpu":0.0,"memory":5368709120}}"#, + ) + } else { + (StatusCode::UNAUTHORIZED, "{}") + } + }), + ); + tokio::spawn(async move { axum::serve(listener, app).await.unwrap() }); + let d = |token: &str| DaemonAddr { + addr: addr.clone(), + token: token.into(), + infra_ports: Vec::new(), + infra_udp_ports: Vec::new(), + }; + assert_eq!(lane_builds(&d("t0k")).await, Some(5 << 30)); + assert_eq!(lane_builds(&d("wrong")).await, None); + // Nobody listens: none, soon. + let gone = DaemonAddr { + addr: "127.0.0.1:1".into(), + ..d("t0k") + }; + assert_eq!(lane_builds(&gone).await, None); + assert_eq!(builds_of(br#"{"services":[]}"#), None); + } + #[test] fn a_gone_lanes_projects_are_found_by_slot_and_place() { let gone = "d4713a1a05c412cb60a5755a"; diff --git a/crates/devstack-node/src/policy.rs b/crates/devstack-node/src/policy.rs index 3efeb587..1ce51421 100644 --- a/crates/devstack-node/src/policy.rs +++ b/crates/devstack-node/src/policy.rs @@ -13,6 +13,7 @@ use serde::{Deserialize, Serialize}; pub const MIN_NEED: Need = Need { cpus: 0.25, memory: 256 << 20, + builds: 0, disk_repo: 0, disk_lane: 0, gpus: None, diff --git a/crates/devstack-node/src/runs.rs b/crates/devstack-node/src/runs.rs index 3f3cdec5..74e1dac7 100644 --- a/crates/devstack-node/src/runs.rs +++ b/crates/devstack-node/src/runs.rs @@ -138,6 +138,14 @@ impl Runs { } } + /// What its build job takes of its memory now, as its daemon says + /// (ADR-0119 point 3): part of what [`Runs::load`] measured. + pub fn builds(&mut self, id: &str, bytes: u64) { + if let Some(s) = self.by.get_mut(id) { + s.peak.builds = s.peak.builds.max(bytes); + } + } + /// What its slot and its containers' images take now. pub fn disk(&mut self, id: &str, lane: u64, images: u64) { let Some(s) = self.by.get_mut(id) else { @@ -388,6 +396,30 @@ mod tests { assert!(r.report().is_empty()); } + #[test] + fn a_runs_builds_are_its_highest_as_its_daemon_said() { + let mut r = Runs::default(); + r.begin("l1", "shop"); + r.load( + "l1", + Amount { + cpus: 1.0, + memory: 9 * GIB, + }, + 12.0, + ); + r.builds("l1", 5 * GIB); + r.builds("l1", 2 * GIB); + r.builds("gone", GIB); + r.by.get_mut("l1").unwrap().since = Instant::now().checked_sub(COUNTS_AFTER); + let p = r.report()[0].peak; + assert_eq!( + (p.memory, p.builds), + (9 * GIB, 5 * GIB), + "a part, not added" + ); + } + #[test] fn steady_use_counts_from_up_its_95th_percentile() { let mut r = Runs::default(); diff --git a/crates/devstack-node/src/share.rs b/crates/devstack-node/src/share.rs index 7ee65670..e51c2840 100644 --- a/crates/devstack-node/src/share.rs +++ b/crates/devstack-node/src/share.rs @@ -37,6 +37,12 @@ pub struct Amount { pub struct Need { pub cpus: f64, pub memory: u64, + /// Of `memory`, the most its build job took (compilers and the build + /// servers that stay warm after a build, ADR-0119 point 3): said, so a + /// need can be explained; never added, `memory` holds it already. Absent + /// from an older peer. + #[serde(default, skip_serializing_if = "is_zero")] + pub builds: u64, /// The repository's part of its disk: the lane image and the /// infrastructure's images, shared by the repository's lanes on a node. #[serde(default, skip_serializing_if = "is_zero")] @@ -87,6 +93,7 @@ impl Need { Need { cpus: self.cpus.max(o.cpus), memory: self.memory.max(o.memory), + builds: self.builds.max(o.builds), disk_repo: self.disk_repo.max(o.disk_repo), disk_lane: self.disk_lane.max(o.disk_lane), gpus: match (self.gpus, o.gpus) { @@ -507,6 +514,29 @@ mod tests { assert_eq!(idle_cpus((100, 1000), (150, 1100), 8), 4.0); } + #[test] + fn a_needs_builds_are_said_apart_and_an_older_peer_reads_without_them() { + let n = Need { + cpus: 1.0, + memory: 9 * GIB, + builds: 5 * GIB, + ..Need::default() + }; + let wire = serde_json::to_value(n).unwrap(); + assert_eq!(wire["builds"], 5 * GIB); + // What an older peer sends has none; what it reads keeps its memory. + let old: Need = serde_json::from_str(r#"{"cpus":1.0,"memory":1024}"#).unwrap(); + assert_eq!((old.memory, old.builds), (1024, 0)); + let amount: Amount = serde_json::from_value(wire).unwrap(); + assert_eq!(amount.memory, 9 * GIB); + assert!( + !serde_json::to_string(&Need::default()) + .unwrap() + .contains("builds") + ); + assert_eq!(old.max(n).builds, 5 * GIB); + } + #[test] fn a_floor_raises_each_part_and_the_disk_in_all() { let measured = Need { @@ -514,7 +544,7 @@ mod tests { memory: 2 * GIB, disk_repo: 6 * GIB, disk_lane: 3 * GIB, - gpus: None, + ..Need::default() }; let (same, raised) = measured.at_least(Need { memory: GIB, diff --git a/docs/adr/0119-a-lanes-memory-counts-what-it-owns-and-names-its-builds.md b/docs/adr/0119-a-lanes-memory-counts-what-it-owns-and-names-its-builds.md index 14efa2f9..55af67ab 100644 --- a/docs/adr/0119-a-lanes-memory-counts-what-it-owns-and-names-its-builds.md +++ b/docs/adr/0119-a-lanes-memory-counts-what-it-owns-and-names-its-builds.md @@ -1,6 +1,6 @@ # ADR-0119: A lane's memory counts what it owns and names its builds; main is stopped when only what serves its lanes runs -- **Status:** Accepted (card in the pull request); points 1, 2 and 4 built, point 3 follows +- **Status:** Accepted (card in the pull request); built (points 1, 2 and 4 in #626, point 3 in its own pull request) - **Date:** 2026-11-24 - **Deciders:** project owner - **Related:** [ADR-0009](0009-the-daemon-owns-every-process-tree.md) (process trees), [ADR-0020](0020-builds-share-one-long-lived-build-job.md) (the build job, its servers stay warm), [ADR-0049](0049-a-lanes-need-is-measured-disk-included.md) (a lane's need), [ADR-0080](0080-the-main-tree-answers-as-a-lane-shared-is-deprecated.md) (`singleton`), [ADR-0095](0095-the-daemon-ends-with-the-last-tree.md) (main stops alone), [ADR-0070](0070-a-review-card-comes-before-the-code-an-adr-records-what-was-built.md) @@ -59,6 +59,18 @@ As built (points 1, 2 and 4): `containers` and `lanes` of the daemon's `200`; the stack page's "main stopped" names what keeps running. +As built (point 3, a pull request of its own after #626): + +4. **The need's builds.** `Need::builds` in `crates/devstack-node/src/share.rs` + (left out at zero, so an older peer reads the rest; `max` keeps the + larger). The daemon's estimate from the main tree takes the build + series' peak (`Estimate::builds`); a node asks each lane's daemon for + `builds` of `/v1/usage` every round, all at once with three seconds each + (`lane_builds`), and keeps the most in the run (`Runs::builds`), whence + the coordinator's footprint keeps the most of the last five. The lease + step says "of it builds …" and `remote_lane_need` logs `builds`. + Placement is unchanged: it takes `memory`. + ## Consequences - The memory devstack shows falls for every stack with several processes diff --git a/docs/adr/README.md b/docs/adr/README.md index 5de4556e..af2b149f 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -201,4 +201,4 @@ stay. | [0116](0116-admins-see-a-machine-that-asks-to-run-lanes.md) | Admins see a machine that asks to run lanes, and are told when one starts | Accepted (card in the pull request); built | | [0117](0117-the-dashboard-pushes-a-branch-that-is-ahead-of-its-upstream.md) | The dashboard pushes a branch that is ahead of its upstream, and the start page shows how far each stack is | Accepted (card in the pull request); built | | [0118](0118-lane-list-says-which-lanes-are-done.md) | `lane list` says which lanes are done, and devstack removes none of them | Accepted (card in the pull request); built; amended 2026-11-23 (`new`, no command for a name a shell reads) | -| [0119](0119-a-lanes-memory-counts-what-it-owns-and-names-its-builds.md) | A lane's memory counts what it owns and names its builds; main is stopped when only what serves its lanes runs | Accepted (card in the pull request); points 1, 2, 4 built, point 3 follows | +| [0119](0119-a-lanes-memory-counts-what-it-owns-and-names-its-builds.md) | A lane's memory counts what it owns and names its builds; main is stopped when only what serves its lanes runs | Accepted (card in the pull request); built | diff --git a/docs/pool.md b/docs/pool.md index 3d4d309f..e76c2d8f 100644 --- a/docs/pool.md +++ b/docs/pool.md @@ -307,7 +307,12 @@ containers each took over the last ten minutes, and as disk its tree and its Compose images; with nothing running, 0.5 CPU, 1 GiB and 10 GiB of disk. `[lanes.need]` and the profile's `need` in the stack file raise each part to what they say. The lease says the need and where it came from, in -the lane's start and in `daemon.log` (`remote_lane_need`). +the lane's start and in `daemon.log` (`remote_lane_need`). Of its memory it +names the builds' part (`builds`, [ADR-0119](adr/0119-a-lanes-memory-counts-what-it-owns-and-names-its-builds.md)): +the compilers and the build servers that stay warm after a build, the most +of them the lane's daemon measured (a node asks it, since it sees the lane as +one container). The part is said, not added, and a node before 0.54.0 sends +none. The need is a peak, its build included, and decides only whether a start fits the room free now: running lanes count by what they use. Besides it a