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
26 changes: 14 additions & 12 deletions crates/fctl-control/src/do_fleet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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(),
Expand All @@ -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(
Expand All @@ -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 {
Expand All @@ -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",
Expand All @@ -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()) {
Expand All @@ -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;
Expand Down
49 changes: 49 additions & 0 deletions crates/fctl-domain/src/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,32 @@ pub fn tried_on(mut tried: Vec<Tried>, runner: Tried) -> Vec<Tried> {
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 {
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions crates/fctl-host/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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())?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading