From 4997b33de1b2cb2de1701bff4fd4731e03625a2b Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Wed, 21 Oct 2026 15:30:33 +0200 Subject: [PATCH] feat(hub): stop a stack's containers with it; a daemon's last telemetry is sent - The dashboard's stop dialog (header and sidebar) offers "Stop its containers too" (`down?infra=true`, as `devstack down --infra`). It is checked when the stack runs nothing but containers: there a stop without them left everything running and only ended the daemon. Not offered while lanes run, which keep the containers they share (ADR-0030). - The daemon sends what it kept since the last telemetry round as it exits, 2 s at most, to the pools of that round: its stop and whatever went wrong while stopping were dropped before. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 8 ++ crates/devstack-daemon/src/lib.rs | 4 + crates/devstack-daemon/src/telemetry.rs | 122 ++++++++++++++++++++---- ui/src/components/GlobalDialogs.vue | 21 ++++ ui/src/components/Sidebar.vue | 6 +- ui/src/stores/stack.ts | 17 +++- ui/src/stores/stores.test.ts | 24 +++++ ui/src/stores/ui.ts | 7 +- 8 files changed, 185 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b193a781..e6371c6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,12 @@ and listed with their assets on the ### Added +- **Stop stack can take the containers down too**: the dashboard's stop + dialog (header and sidebar) offers "Stop its containers too", as + `devstack down --infra` does; their volumes stay. Checked when the stack + runs nothing but containers, where a stop without them changed nothing + but the daemon. Not offered while lanes run: they keep the containers + they share (ADR-0030). - **Links to a stack's or a lane's dashboard page, for agents to hand on**: `devstack status`, `devstack up` and `devstack lane start` end with `dashboard: http://127.0.0.1:4250/#/s/?lane=`; the MCP tools @@ -114,6 +120,8 @@ and listed with their assets on the the lanes when their counts change (a lane on a node is read every 5 s), and the shown lane counts from its own stream. Before, a lane started on a node stayed yellow until the page switched to another tree. +- A daemon's last telemetry is sent as it exits (2 s at most): what it said + in its last minute, its stop included, was dropped before (ADR-0037). - A lane on a node no longer stops syncing when a file goes while it is sent (a package manager rewriting `node_modules`): the file is left out and the next sync sends what is there then. diff --git a/crates/devstack-daemon/src/lib.rs b/crates/devstack-daemon/src/lib.rs index ce19772e..b5e817ca 100644 --- a/crates/devstack-daemon/src/lib.rs +++ b/crates/devstack-daemon/src/lib.rs @@ -129,6 +129,9 @@ fn cgroup_root(stack: &str) -> Option { /// How long a stopping daemon waits for open requests before it stops anyway. const SHUTDOWN_GRACE: std::time::Duration = std::time::Duration::from_secs(2); +/// How long an exiting daemon waits to send its last telemetry (ADR-0037). +const TELEMETRY_FLUSH: std::time::Duration = std::time::Duration::from_secs(2); + /// How long a stopping daemon waits for its services to stop. What still /// runs then dies with the daemon (ADR-0009); a second Ctrl-C ends the wait. const SHUTDOWN_STOP: std::time::Duration = std::time::Duration::from_secs(90); @@ -413,6 +416,7 @@ pub fn serve(stack_file: &Path) -> Result<(), String> { } let _ = std::fs::remove_file(info_path(&state_dir)); diag::info("daemon_stop", serde_json::json!({})); + telemetry::flush(TELEMETRY_FLUSH); served }); // A stop still under way must not keep the process (dropping the diff --git a/crates/devstack-daemon/src/telemetry.rs b/crates/devstack-daemon/src/telemetry.rs index 3e5bee17..f1f8502b 100644 --- a/crates/devstack-daemon/src/telemetry.rs +++ b/crates/devstack-daemon/src/telemetry.rs @@ -47,6 +47,10 @@ static TUNNELS_OPEN: AtomicU64 = AtomicU64::new(0); static REPOSITORY: OnceLock = OnceLock::new(); type GaugeFn = Box Vec + Send + Sync>; static GAUGES: OnceLock = OnceLock::new(); +/// The process's name as a sender (`devstack-daemon`), once started. +static SERVICE: OnceLock<&'static str> = OnceLock::new(); +/// The pools the last round sent to: where [`flush`] sends. +static TARGETS: Mutex> = Mutex::new(Vec::new()); /// Whether diagnostic lines are kept now: a pool takes them, or none was /// asked yet. @@ -143,6 +147,7 @@ pub fn start(service: &'static str) { return; } STATE.store(UNDECIDED, Ordering::Relaxed); + let _ = SERVICE.set(service); let spawned = std::thread::Builder::new() .name("telemetry".into()) .spawn(move || run(service)); @@ -234,30 +239,76 @@ fn run(service: &str) { } Round::Send => {} } - let lines = std::mem::take(&mut *BUFFER.lock().unwrap()); - let tunnels = std::mem::take(&mut *TUNNELS.lock().unwrap()); - let repo = repository(); - let logs = (!lines.is_empty()).then(|| crate::diag::otlp_body(service, repo, &lines)); - let mut gauges = GAUGES.get().map(|f| f()).unwrap_or_default(); - gauges.extend(tunnel_gauges(repo, &tunnels)); - let mut durations = durations(repo, &lines); - durations.extend(tunnel_durations(repo, &tunnels)); - let metrics = metrics_body(service, &gauges, &durations); - for (dir, t) in &targets { - let sent = match &logs { - Some(l) => t.telemetry_send("logs", l), - None => Ok(()), - } - .and_then(|()| t.telemetry_send("metrics", &metrics)); + *TARGETS.lock().unwrap() = targets.iter().map(|(dir, _)| dir.clone()).collect(); + for dir in send(service, &targets, true) { // A coordinator that stopped collecting answers 404 (ADR-0037): // stop sending to it now, not when it is asked again. - if stopped_collecting(&sent) { - collects.insert(dir.clone(), (Instant::now(), false)); - } + collects.insert(dir, (Instant::now(), false)); } } } +/// Send what was kept since the last round to `targets`, with the gauges +/// when `gauges`; the pools whose coordinator no longer collects. +fn send(service: &str, targets: &[(PathBuf, Local)], gauges: bool) -> Vec { + let lines = std::mem::take(&mut *BUFFER.lock().unwrap()); + let tunnels = std::mem::take(&mut *TUNNELS.lock().unwrap()); + let repo = repository(); + let logs = (!lines.is_empty()).then(|| crate::diag::otlp_body(service, repo, &lines)); + let mut measured = if gauges { + GAUGES.get().map(|f| f()).unwrap_or_default() + } else { + Vec::new() + }; + measured.extend(tunnel_gauges(repo, &tunnels)); + let mut durations = durations(repo, &lines); + durations.extend(tunnel_durations(repo, &tunnels)); + let metrics = metrics_body(service, &measured, &durations); + let mut stopped = Vec::new(); + for (dir, t) in targets { + let sent = match &logs { + Some(l) => t.telemetry_send("logs", l), + None => Ok(()), + } + .and_then(|()| t.telemetry_send("metrics", &metrics)); + if stopped_collecting(&sent) { + stopped.push(dir.clone()); + } + } + stopped +} + +/// The process is about to exit: send what was kept since the last round +/// to the pools that took it, waiting `within` at most. Without it, a +/// daemon's stop (and whatever went wrong while it stopped) never left. +pub fn flush(within: Duration) { + if STATE.load(Ordering::Relaxed) != ON || BUFFER.lock().unwrap().is_empty() { + return; + } + let Some(service) = SERVICE.get().copied() else { + return; + }; + let dirs = TARGETS.lock().unwrap().clone(); + let (tx, rx) = std::sync::mpsc::channel(); + let spawned = std::thread::Builder::new() + .name("telemetry-flush".into()) + .spawn(move || { + let targets: Vec<(PathBuf, Local)> = dirs + .into_iter() + .map(|dir| { + let local = Local::new(&dir); + (dir, local) + }) + .collect(); + // The gauges read the process's state, which is being torn down. + send(service, &targets, false); + let _ = tx.send(()); + }); + if spawned.is_ok() { + let _ = rx.recv_timeout(within); + } +} + /// Whether a send's answer says the coordinator no longer collects. fn stopped_collecting(sent: &devstack_node::member::Result<()>) -> bool { matches!( @@ -358,6 +409,9 @@ fn tunnel_gauges(repo: Option<&str>, tunnels: &[TunnelClosed]) -> Vec { mod tests { use super::*; + /// Held by the tests that change the module's state and buffer. + static GLOBALS: Mutex<()> = Mutex::new(()); + #[test] fn a_command_run_is_counted_by_its_name_and_result() { let lines = vec![ @@ -481,6 +535,7 @@ mod tests { m }; let has = |n: &str| BUFFER.lock().unwrap().iter().any(|l| l["event"] == n); + let _g = GLOBALS.lock().unwrap_or_else(|e| e.into_inner()); STATE.store(UNDECIDED, Ordering::Relaxed); record(&line("telemetry_test_early")); assert!(has("telemetry_test_early")); @@ -496,6 +551,37 @@ mod tests { .retain(|l| l["event"] != "telemetry_test_early"); } + #[test] + fn an_exit_sends_what_was_kept_and_waits_no_longer_than_it_may() { + let _g = GLOBALS.lock().unwrap_or_else(|e| e.into_inner()); + let mut line = serde_json::Map::new(); + line.insert("event".into(), Value::String("telemetry_test_exit".into())); + let kept = || { + BUFFER + .lock() + .unwrap() + .iter() + .any(|l| l["event"] == "telemetry_test_exit") + }; + // Not sending: kept as it is. + STATE.store(UNDECIDED, Ordering::Relaxed); + record(&line); + flush(Duration::from_secs(2)); + assert!(kept()); + // Sending: taken and sent to the pools of the last round (one + // without a membership here refuses at once). + let _ = SERVICE.set("devstack-daemon"); + let pool = tempfile::tempdir().unwrap(); + *TARGETS.lock().unwrap() = vec![pool.path().to_path_buf()]; + STATE.store(ON, Ordering::Relaxed); + let t0 = Instant::now(); + flush(Duration::from_secs(2)); + assert!(!kept()); + assert!(t0.elapsed() < Duration::from_secs(3)); + STATE.store(IDLE, Ordering::Relaxed); + TARGETS.lock().unwrap().clear(); + } + #[test] fn a_collector_that_answers_404_stopped_collecting() { use devstack_node::member::MemberError; diff --git a/ui/src/components/GlobalDialogs.vue b/ui/src/components/GlobalDialogs.vue index 3f98fc86..093310d2 100644 --- a/ui/src/components/GlobalDialogs.vue +++ b/ui/src/components/GlobalDialogs.vue @@ -33,6 +33,13 @@ async function copy(text: string) {

{{ req?.text }}

+