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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ and listed with their assets on the

## [Unreleased]

### Changed

- **A stack starts sooner** (measured in the pool's telemetry: a lane on a
node took about 154 s, of which 51 s waiting for its containers before
any build, and 37 s building what an earlier lane had built). The
services are built while the containers start, Compose groups that no
`after` orders start side by side (here and on a node), and services
whose `after` is done start side by side. A node keeps the member's
build artifacts with its store (ADR-0062 addendum): the member's next
lane with the same inputs copies them instead of building.

### Added

- **A pool grows by machines a provisioner makes** (ADR-0051): with
Expand Down
102 changes: 99 additions & 3 deletions crates/devstack-daemon/src/supervisor/artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ impl Supervisor {
}
let base = build_id(&snap.manifest, svc).map_err(|e| e.to_string())?;
let mut id = base.clone();
let mut from_store = false;
// `--rebuild`: the same inputs once more, under `<id>-<n>`; the artifact
// that exists may be running and is never overwritten (ADR-0002).
if forced {
Expand All @@ -163,16 +164,36 @@ impl Supervisor {
"why": "an artifact for these inputs exists (watch paths, build files above them, build command); `devstack restart --rebuild` builds anyway",
}),
);
} else if let Some(ms) = self.copy_from_node_store(name, &id) {
from_store = true;
crate::diag::info(
"build_reused",
serde_json::json!({
"lane": self.lane,
"service": name,
"build_id": id,
"source": "store",
"ms": ms,
"why": "a lane of this member built these inputs on this node before (its artifact store); `devstack restart --rebuild` builds anyway",
}),
);
}
if self.builder.artifact(name, &id).is_none() {
building(Some(id.clone()));
let missing = self.builder.artifact(name, &id).is_none();
// An artifact from the node's store came without its `prepare`: the
// lane's worktree gets what `prepare` makes as a build's would.
if missing || from_store {
if missing {
building(Some(id.clone()));
}
if let Some(job) = self.prepare_job(name, svc, &snap)? {
drop(snap);
let view = self.run_prepare(name, job, cancel)?;
self.set_prepare(name, view);
snap = self.snapshot.lock().unwrap();
}
self.link_prepared(name, svc);
}
if missing {
let t0 = Instant::now();
let mut running = self
.builder
Expand Down Expand Up @@ -216,7 +237,10 @@ impl Supervisor {
};
self.build_done(name, &id, t0, result);
match outcome {
Outcome::Published { .. } => self.prune_artifacts(name),
Outcome::Published { .. } => {
self.put_in_node_store(name, &id, t0.elapsed());
self.prune_artifacts(name)
}
Outcome::Stale { .. } => {
crate::diag::warn(
"build_discarded",
Expand Down Expand Up @@ -245,6 +269,78 @@ impl Supervisor {
Ok(Some((id.clone(), self.layout.artifact(name, &id))))
}

/// In a lane on a node: the member's artifact store there (card on the
/// pull request of 2026-10-28): `artifacts` in the member's store the
/// node mounts read-only, new entries through the lane's outbox, which
/// the agent takes in. Keyed by the build id and the lane image, whose
/// toolchain built it. `None` elsewhere, or from an agent that gives no
/// store or no image.
fn node_artifacts(&self) -> Option<(devstack_build::store::Store, String)> {
let (root, outbox) = super::copy::node_store()?;
let image = std::env::var("DEVSTACK_LANE_IMAGE")
.ok()
.filter(|i| !i.is_empty())?;
let image = blake3::hash(image.as_bytes()).to_hex()[..12].to_string();
Some((
devstack_build::store::Store::with_outbox(
root.join("artifacts"),
outbox.join("artifacts"),
),
image,
))
}

/// The artifact of `id` from the node's store, into this lane's own:
/// how long the copy took, `None` when the store has none.
fn copy_from_node_store(&self, name: &str, id: &str) -> Option<u64> {
let (store, image) = self.node_artifacts()?;
let key = format!("{id}-{image}");
store.get(name, &key)?;
let t0 = Instant::now();
let dir = self.layout.artifacts(name);
// Copied beside it, then moved: an artifact is there whole or not.
let tmp = dir.join(format!(".store-{}-{id}", std::process::id()));
let _ = std::fs::remove_dir_all(&tmp);
let copied = std::fs::create_dir_all(&tmp)
.and_then(|()| store.copy_into(name, &key, &tmp).map(|_| ()))
.and_then(|()| std::fs::rename(tmp.join(id), dir.join(id)));
let _ = std::fs::remove_dir_all(&tmp);
match copied {
Ok(()) if self.builder.artifact(name, id).is_some() => {
Some(t0.elapsed().as_millis() as u64)
}
Ok(()) => None,
Err(e) => {
crate::diag::warn(
"build_store_copy_failed",
serde_json::json!({ "lane": self.lane, "service": name, "build_id": id, "error": e.to_string() }),
);
None
}
}
}

/// A build of `id` published: into the node's store for the member's
/// next lane with the same inputs.
fn put_in_node_store(&self, name: &str, id: &str, took: Duration) {
let Some((store, image)) = self.node_artifacts() else {
return;
};
let key = format!("{id}-{image}");
if let Err(e) = store.put(
name,
&key,
&self.layout.artifacts(name),
&[id.to_string()],
took,
) {
crate::diag::warn(
"build_store_put_failed",
serde_json::json!({ "lane": self.lane, "service": name, "build_id": id, "error": e.to_string() }),
);
}
}

/// A build ended: how long it ran and how (ADR-0037 point 4).
fn build_done(&self, name: &str, id: &str, t0: Instant, result: &str) {
crate::diag::info(
Expand Down
2 changes: 1 addition & 1 deletion crates/devstack-daemon/src/supervisor/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ fn store_key_of(key: &str, outputs: &[String], version: Option<&str>) -> String
/// In a lane on a node: the member's store the node mounts read-only
/// (`DEVSTACK_STORE`), and the outbox in the lane's slot its agent takes new
/// entries from (`DEVSTACK_STORE_OUT`), ADR-0062 points 3, 4.
fn node_store() -> Option<(PathBuf, PathBuf)> {
pub(super) fn node_store() -> Option<(PathBuf, PathBuf)> {
if !crate::remote_lane() {
return None;
}
Expand Down
38 changes: 35 additions & 3 deletions crates/devstack-daemon/src/supervisor/infra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,40 @@ impl Supervisor {
// The node's agent starts it, after this daemon (ADR-0025, #164).
return self.node_infra_up(&file);
}
for g in self.group_order() {
// Groups no `after` orders start side by side.
let deps: BTreeMap<String, Vec<String>> = self
.stack
.compose
.iter()
.map(|(k, g)| (k.clone(), g.after.clone()))
.collect();
for level in super::levels(&deps) {
let results: Vec<Result<(), String>> = std::thread::scope(|s| {
let running: Vec<_> = level
.iter()
.map(|g| s.spawn(move || self.group_up(g)))
.collect();
running
.into_iter()
.map(|h| {
h.join().unwrap_or_else(|_| {
Err("a compose group's start failed inside devstack".into())
})
})
.collect()
});
if let Some(e) = results.into_iter().find_map(Result::err) {
return Err(e);
}
}
self.refresh_infra();
Ok(())
}

/// Start compose group `g` and wait until it is ready.
fn group_up(&self, g: &str) -> Result<(), String> {
let g = g.to_string();
{
self.emit(EventBody::Infra {
group: g.clone(),
message: "starting".into(),
Expand Down Expand Up @@ -450,7 +483,7 @@ impl Supervisor {
if self.lane.is_empty() {
self.hold_ports(&g, &held);
}
continue;
return Ok(());
}
if let Err(e) = self.docker.up(&group, Duration::from_secs(300)) {
let msg = e.to_string();
Expand All @@ -470,7 +503,6 @@ impl Supervisor {
message: "ready".into(),
});
}
self.refresh_infra();
Ok(())
}

Expand Down
124 changes: 102 additions & 22 deletions crates/devstack-daemon/src/supervisor/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,19 @@ impl Supervisor {
// volumes go first, once per start, so its seed jobs fill new
// ones. On a node its agent does it (ADR-0025).
self.fresh_data();
// Every lane has its own infrastructure (ADR-0030).
self.infra_up()?;
// Every lane has its own infrastructure (ADR-0030). A build needs
// no container: the services are built while it starts.
let infra = {
let me = self.clone();
std::thread::Builder::new()
.name("infra-up".into())
.spawn(move || me.infra_up())
.map_err(|e| e.to_string())?
};
self.prebuild();
infra
.join()
.map_err(|_| "starting the infrastructure failed inside devstack".to_string())??;
// Healthy inside Docker is not reachable from here: say so
// (a service that needs the container repairs it, ensure_after).
for (g, svc, lost) in self.check_forwards(true) {
Expand All @@ -142,26 +153,42 @@ impl Supervisor {
}
// A failed service holds back only what comes `after` it; everything
// independent of it still starts (one broken build must not keep the
// rest of the stack down).
// rest of the stack down). Services whose `after` is done start at
// once, side by side.
let mut failed: BTreeSet<String> = BTreeSet::new();
for name in self.service_order() {
let svc = self.service(&name)?;
if !svc.autostart || !self.runs_here(svc) {
continue;
}
if let Some(dep) = svc.after.iter().find(|d| failed.contains(*d)) {
self.set_status(
&name,
Status::Failed {
reason: format!("not started: `{dep}` failed"),
},
);
failed.insert(name);
continue;
}
if self.start(&name).is_err() {
failed.insert(name);
for level in super::levels(&self.service_deps()) {
let mut go = Vec::new();
for name in level {
let svc = self.service(&name)?;
if !svc.autostart || !self.runs_here(svc) {
continue;
}
if let Some(dep) = svc.after.iter().find(|d| failed.contains(*d)) {
self.set_status(
&name,
Status::Failed {
reason: format!("not started: `{dep}` failed"),
},
);
failed.insert(name);
continue;
}
go.push(name);
}
let results: Vec<(String, bool)> = std::thread::scope(|s| {
let running: Vec<_> = go
.into_iter()
.map(|name| {
let me = self.clone();
s.spawn(move || {
let ok = me.start(&name).is_ok();
(name, ok)
})
})
.collect();
running.into_iter().filter_map(|h| h.join().ok()).collect()
});
failed.extend(results.into_iter().filter(|(_, ok)| !ok).map(|(n, _)| n));
}
if failed.is_empty() {
Ok(())
Expand All @@ -173,6 +200,45 @@ impl Supervisor {
}
}

/// Build what the start will run, while the infrastructure starts: each
/// service with `build` that starts with the tree and has no artifact
/// to restore (ADR-0016). Its start then finds the artifact. One that
/// fails here (a build that needs a container after all) is built again
/// by its start, which says why.
fn prebuild(self: &Arc<Self>) {
let first: Vec<String> = self.started.lock().unwrap().iter().cloned().collect();
for name in self.service_order() {
let Ok(declared) = self.service(&name) else {
continue;
};
if !declared.autostart || !self.runs_here(declared) || first.contains(&name) {
continue;
}
let svc = self.effective(&name, declared);
if svc.build.is_none()
|| !super::on_this_platform(&svc)
|| self.restorable(&name).is_some()
{
continue;
}
let epoch = self.stop_epoch(&name);
let _op = self.op_locks[&name].lock().unwrap();
if self.slots.lock().unwrap()[&name].view.status.is_active() {
continue;
}
let cancel = || self.stopped_since(&name, epoch);
let t0 = std::time::Instant::now();
let r = self.build_locked(&name, &svc, &cancel, epoch);
crate::diag::info(
"prebuilt",
serde_json::json!({ "lane": self.lane, "service": name,
"ms": t0.elapsed().as_millis() as u64, "error": r.as_ref().err() }),
);
// Built, not started: it waits for the infrastructure.
self.set_status(&name, Status::Stopped);
}
}

fn fresh_data(&self) {
let fresh = self
.stack
Expand Down Expand Up @@ -543,7 +609,7 @@ impl Supervisor {
return Ok(p.clone());
}
let port = svc.port.expect("proxied services have a port");
if port_in_use(port) {
if !port_freed(port) {
return Err(super::port_taken(port));
}
let p =
Expand Down Expand Up @@ -674,7 +740,7 @@ impl Supervisor {
None
} else {
if let Some(port) = svc.port
&& port_in_use(port)
&& !port_freed(port)
{
return Err(super::port_taken(port));
}
Expand Down Expand Up @@ -1344,3 +1410,17 @@ impl Supervisor {
Ok(())
}
}

/// Whether `port` is free, or becomes free within a while: held a moment
/// longer by something on its way out (the instance of a daemon that just
/// restarted, ADR-0052), as for a front.
fn port_freed(port: u16) -> bool {
let t0 = std::time::Instant::now();
while port_in_use(port) {
if t0.elapsed() >= Duration::from_secs(10) {
return false;
}
std::thread::sleep(Duration::from_millis(250));
}
true
}
Loading
Loading