From 0d3fb93159c768515c4344d01c18478640ca3452 Mon Sep 17 00:00:00 2001 From: "fctl-sh[bot]" <343971779+fctl-sh[bot]@users.noreply.github.com> Date: Thu, 3 Dec 2026 18:01:53 +0000 Subject: [PATCH] fix(control): retry a session a full or low-disk runner declines Since #669 every decline that isn't for the driver made the runner one that couldn't start the session, which dispatch passes over until it starts. But a runner declines a session for being at capacity or low on disk when FleetDO's view of it, from heartbeats 15 s apart, lags: in a workspace with one runner the session then failed at once, where it used to try again after a backoff. schedule::passes tells those declines by the host's words, which schedule::AT_CAPACITY and schedule::low_on_disk now say for the host too, and FleetDO retries them after a backoff, as it does a decline for the driver, without counting the runner as tried. Hosts of every version say these words, so the protocol doesn't change. --- crates/fctl-control/src/do_fleet.rs | 26 +++++----- crates/fctl-domain/src/schedule.rs | 49 +++++++++++++++++++ crates/fctl-host/src/daemon.rs | 11 +++-- ...ons-a-runner-cant-start-move-to-another.md | 6 ++- docs/architecture.md | 5 +- 5 files changed, 78 insertions(+), 19 deletions(-) diff --git a/crates/fctl-control/src/do_fleet.rs b/crates/fctl-control/src/do_fleet.rs index 2cce54fa..96d479c1 100644 --- a/crates/fctl-control/src/do_fleet.rs +++ b/crates/fctl-control/src/do_fleet.rs @@ -2188,6 +2188,8 @@ impl FleetObject { /// `unready`: the runner declined it, or handed it back, because its /// driver isn't ready, so a resume still goes back to it once it is. + /// Declined for that, or because the runner was full or low on disk + /// (`schedule::passes`), it tries again in a while, there or elsewhere. /// Declined or handed back for anything else (an image the runner /// can't pull, a sandbox that doesn't start there), it goes to another /// runner that can run it, and fails when there is none. @@ -2284,7 +2286,9 @@ impl FleetObject { // Its earlier declines count: the run's, or the queue's when it was // queued again meanwhile. So do the runners that couldn't start it, // which it doesn't go to again: this one with them, unless it - // declined for its driver, which takes it back once ready. + // declined for something that passes (`again`): its driver, which + // takes it back once ready, or being full or low on disk, which + // FleetDO learns of only from heartbeats. #[derive(Deserialize)] struct A { attempts: f64, @@ -2303,6 +2307,7 @@ impl FleetObject { let queued_attempts = queued.as_ref().map_or(0, |a| a.attempts as i64); let attempts = queued_attempts.max(run.attempts.unwrap_or(0.0) as i64); let reason = error.unwrap_or_else(|| "The runner declined the session".into()); + let again = unready || fctl_domain::schedule::passes(&reason); let this = Tried { runner_id: runner_id.to_owned(), name: runner.name.clone(), @@ -2312,12 +2317,12 @@ impl FleetObject { for t in tried_from(queued.as_ref().and_then(|a| a.tried.as_deref())) { tried = fctl_domain::schedule::tried_on(tried, t); } - if !unready { + if !again { tried = fctl_domain::schedule::tried_on(tried, this.clone()); } // Out of attempts, it fails, saying why each runner couldn't start it. if attempts + 1 >= MAX_ATTEMPTS { - let why = match (tried.is_empty(), unready) { + let why = match (tried.is_empty(), again) { (true, _) => reason, (false, false) => fctl_domain::schedule::couldnt_start(&tried), (false, true) => fctl_domain::schedule::couldnt_start( @@ -2333,8 +2338,9 @@ impl FleetObject { return; } // For its driver, it tries again in a while, and goes back there - // once it is ready; otherwise it goes to another runner now. - let (not_before, said) = if unready { + // once it is ready; for room, it tries again in a while too, there + // or elsewhere; otherwise it goes to another runner now. + let (not_before, said) = if again { let backoff = 5_000 * (1 << attempts.min(5)); (now + backoff, format!("Retrying: {reason}")) } else { @@ -2343,11 +2349,7 @@ impl FleetObject { // Back in the queue before anything is awaited, so that a stop // meanwhile finds it there and takes it out; held back from dispatch // while FleetDO looks for another runner that could run it. - let held = if unready { - not_before - } else { - now + LOOKING_MS - }; + let held = if again { not_before } else { now + LOOKING_MS }; let _ = self.sql.exec( "INSERT INTO queue (session_id, enqueued_at, attempts, not_before, driver_declined, tried) VALUES (?, ?, ?, ?, ?, ?) \ ON CONFLICT (session_id) DO UPDATE SET attempts = excluded.attempts, not_before = excluded.not_before, driver_declined = excluded.driver_declined, tried = excluded.tried", @@ -2360,7 +2362,7 @@ impl FleetObject { tried_value(&tried), ]), ); - if !unready { + if !again { let none_left = self.none_left(session_id.as_str(), &tried, now).await; // Stopped meanwhile: it stays stopped. if !self.queued(session_id.as_str()) { @@ -2379,7 +2381,7 @@ impl FleetObject { } self.session_control(session_id, Some(SessionStatus::Queued), Some(said), true) .await; - if !unready { + if !again { self.dispatch().await; } self.ensure_alarm().await; diff --git a/crates/fctl-domain/src/schedule.rs b/crates/fctl-domain/src/schedule.rs index 158626f0..00ae0a8c 100644 --- a/crates/fctl-domain/src/schedule.rs +++ b/crates/fctl-domain/src/schedule.rs @@ -534,6 +534,32 @@ pub fn tried_on(mut tried: Vec, runner: Tried) -> Vec { tried } +/// What a host says as it declines a session because it runs as many +/// sessions as it may ([`passes`]). +pub const AT_CAPACITY: &str = "the host is at capacity"; + +/// How what a host says ends as it declines a session for want of disk +/// ([`low_on_disk`]). +const FOR_DISK: &str = " (min_free_disk_gb in its host.toml)"; + +/// What a host says as it declines a session that needs a new sandbox +/// while it is low on disk: runner `runner` has `below` +/// ([`crate::disk::below`]) ([`passes`]). +pub fn low_on_disk(runner: &str, below: &str) -> String { + format!("{runner} has {below}{FOR_DISK}") +} + +/// Whether a runner declined a session, as its host said (`reason`), for +/// something that passes: it ran as many sessions as it may +/// ([`AT_CAPACITY`]) or was low on disk ([`low_on_disk`]), which the +/// control plane learns only from heartbeats, 15 s apart. Such a runner +/// may take it later, as [`Wait::rules_out`] has it, so it isn't one that +/// couldn't start the session ([`Tried`]): the session tries again in a +/// while. Hosts of every version say these words. +pub fn passes(reason: &str) -> bool { + reason == AT_CAPACITY || reason.ends_with(FOR_DISK) +} + /// What a session says as it goes back to the queue to start on another /// runner, after `runner` couldn't start it. pub fn tries_another(runner: &Tried) -> String { @@ -1987,6 +2013,29 @@ mod tests { assert!(couldnt_start(&[long]).chars().count() < 1100); } + #[test] + fn a_runner_that_is_full_or_low_on_disk_may_take_the_session_later() { + let disk = low_on_disk("studio", &crate::disk::below(11 * 1024, 20 * 1024)); + assert_eq!( + disk, + "studio has 11 GiB of disk free, below the 20 GiB it keeps free (min_free_disk_gb in its host.toml)" + ); + assert!(passes(AT_CAPACITY)); + assert!(passes(&disk)); + // What it can't do however long the session waits rules it out. + for never in [ + "studio gives a sandbox at most 4 CPUs and 8 GiB of memory", + "studio doesn't run ghcr.io/acme/agent:1 (allowed_images in its host.toml)", + "studio builds no images from acme/web (allowed_builds in its host.toml)", + "This runner's machine is retiring: it takes no new sessions", + "ghcr.io/acme/agent:1 is not on this host and can't be pulled: `docker pull ghcr.io/acme/agent:1` failed: unauthorized.", + "The runner declined the session", + "", + ] { + assert!(!passes(never), "{never}"); + } + } + #[test] fn a_runner_is_ready_unless_its_driver_says_otherwise() { let info = |name: &str, available: bool| DriverInfo { diff --git a/crates/fctl-host/src/daemon.rs b/crates/fctl-host/src/daemon.rs index 506bf5d5..4730bec1 100644 --- a/crates/fctl-host/src/daemon.rs +++ b/crates/fctl-host/src/daemon.rs @@ -2069,10 +2069,9 @@ impl Daemon { // they need no new one. The control plane sends no others, but it // learns the free disk from heartbeats, which come every 15 s. if self.low_on_disk() && !self.has_stopped_sandbox(&id) { - return Err(format!( - "{} has {} (min_free_disk_gb in its host.toml)", - self.ctx.cfg.name, - self.below() + return Err(fctl_domain::schedule::low_on_disk( + &self.ctx.cfg.name, + &self.below(), )); } let waiting = self @@ -2092,7 +2091,7 @@ impl Daemon { .collect(); let running = self.sessions.len() - suspended.len(); if (running + waiting) as u32 >= self.capacity() { - return Err("the host is at capacity".into()); + return Err(fctl_domain::schedule::AT_CAPACITY.into()); } // Only one whose snapshot was saved lets its microVM go (losing // nothing), and only once a suspension: one already asked holds no @@ -3218,6 +3217,8 @@ mod tests { ), "{refused}" ); + // Which passes: the control plane tries it here again in a while. + assert!(fctl_domain::schedule::passes(&refused)); // One whose sandbox is here resumes in it (once a chore on it ends). let path = expiry::path(&d.ctx.cfg.data_dir()); expiry::note_stopped(&path, "fctl-ses-01own", expiry::now_ms())?; diff --git a/docs/adr/0107-sessions-a-runner-cant-start-move-to-another.md b/docs/adr/0107-sessions-a-runner-cant-start-move-to-another.md index aaa4b1f1..a3667b7c 100644 --- a/docs/adr/0107-sessions-a-runner-cant-start-move-to-another.md +++ b/docs/adr/0107-sessions-a-runner-cant-start-move-to-another.md @@ -39,7 +39,11 @@ when one can run it, so that a broken runner heals around itself: its reason (`queue.tried`, `assignments.tried`), and dispatch passes over it until the session starts. A new start (a message, a resume) forgets them: the runner may be fixed by then. A driver that isn't - ready still gets the session back once it is, where its sandbox is. + ready still gets the session back once it is, where its sandbox is. A + runner that declines it because it is full or low on disk + (`schedule::passes`), which FleetDO learns only from heartbeats, isn't + kept either: the session tries again in a while, as after a decline for + the driver, rather than fail in a workspace with one runner. - **It fails once no other runner could run it now.** Of the runners online, if none it hasn't tried could take it however long it waited (none runs the workspace's sessions for its person, has its labels, diff --git a/docs/architecture.md b/docs/architecture.md index 43797e7b..7777568c 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -1733,7 +1733,10 @@ other runner from it another runner: …"). Dispatch passes over the runners it tried until it starts, which a new start (a message, a resume) forgets. Each try counts towards the five attempts a session gets, so a session that every runner - of a large pool would fail fails after five. + of a large pool would fail fails after five. A runner that declines it + because it is full or low on disk (`schedule::passes`), which FleetDO + learns only from heartbeats, 15 seconds apart, isn't one of them: the + session tries again in a while, as after a decline for the driver. 3. **It fails once no other runner could run it.** When none of the runners online but those it tried could take it however long it waited (none runs its workspace's sessions for its person, has its labels