Public: anyone can read this; the crew can change it.
A pull's check widens when the machine has room: twice the shards and cell when nothing waits and the host is idle #17383
Merged 20h 6m ago, live in production
wide-checks → main 0 commits 3 threads · 3 unresolved
4 files
changed +430
−7
scripts/unused-exports-allow.json +1
src/checks/tier_runner.ts +17 −7
| @@ -28,6 +28,7 @@ import { redDetail } from "./report.ts"; | ||
| 28 | 28 | import { parseBunTestFailure } from "./bun-test.ts"; |
| 29 | 29 | import { withFlaky } from "./rerun.ts"; |
| 30 | 30 | import { withPlan } from "./plan_line.ts"; |
| 31 | import { roomOrNone, sizeCheck } from "./wide.ts"; | |
| 31 | 32 | |
| 32 | 33 | /** The line a runner says when its verification's red was flakes alone (`runner/flake.ts`). */ |
| 33 | 34 | const FLAKY_GREEN = /^flaky: .* failed in the full run and passed alone; recorded, main stays green$/; |
| @@ -101,8 +102,20 @@ export async function tryRunner( | ||
| 101 | 102 | const lines: string[] = []; |
| 102 | 103 | // The shard count an admin set reaches a runner's check as it does one run here (#14844); the |
| 103 | 104 | // default is sized as one run here is (`checkShardCpus`), never from the runner machine's CPUs. |
| 104 | const shardCpus = checkShardCpus(resolveLimits("step", manifest.limits).cpus); | |
| 105 | write(shardsLine(shardsInForce(app.db, shardCpus, x.tier === "verify"))); | |
| 105 | const effective = resolveLimits("step", manifest.limits); | |
| 106 | const shardCpus = checkShardCpus(effective.cpus); | |
| 107 | const shards = shardsInForce(app.db, shardCpus, x.tier === "verify"); | |
| 108 | write(shardsLine(shards)); | |
| 109 | const declared = { | |
| 110 | ...(manifest.limits.cpus === undefined ? {} : { cpus: manifest.limits.cpus }), | |
| 111 | ...(manifest.limits.memoryMiB === undefined ? {} : { memoryMiB: manifest.limits.memoryMiB }), | |
| 112 | ...(manifest.limits.diskMiB === undefined ? {} : { diskMiB: manifest.limits.diskMiB }), | |
| 113 | }; | |
| 114 | // A pull's check widens when nothing waits and the host has been idle for minutes (`wide.ts`); | |
| 115 | // decided now, once: a running check never narrows, the next one is decided afresh. | |
| 116 | const room = x.tier === "verify" ? undefined : roomOrNone(app.db, run.id, write); | |
| 117 | const sized = room === undefined ? { shards: shards.count, limits: declared } : sizeCheck(room, shards.count, declared, effective); | |
| 118 | if ("line" in sized) write(sized.line); | |
| 106 | 119 | // The checkout there is seeded as one here is, read-only (`runner_seeds.ts`). |
| 107 | 120 | const seeds = prepareSeeds(app.config.dataDir, repo, run.id, builtOnBase, write); |
| 108 | 121 | let sent: Awaited<ReturnType<typeof send>> | undefined; |
| @@ -127,6 +140,7 @@ export async function tryRunner( | ||
| 127 | 140 | // Only the check's own variables cross, never the forge's; the runner adds its cell's. |
| 128 | 141 | env: { |
| 129 | 142 | ...capacityEnv(app.db, shardCpus, x.tier === "verify"), |
| 143 | FORGE_CHECK_SHARDS: String(sized.shards), | |
| 130 | 144 | CI: "1", |
| 131 | 145 | FORGE_URL: options.forgeUrl, |
| 132 | 146 | FORGE_CHECK: run.id, |
| @@ -135,11 +149,7 @@ export async function tryRunner( | ||
| 135 | 149 | ...diffEnv(base, learned, reverts), |
| 136 | 150 | FORGE_CELLS: "off", |
| 137 | 151 | }, |
| 138 | limits: { | |
| 139 | ...(manifest.limits.cpus === undefined ? {} : { cpus: manifest.limits.cpus }), | |
| 140 | ...(manifest.limits.memoryMiB === undefined ? {} : { memoryMiB: manifest.limits.memoryMiB }), | |
| 141 | ...(manifest.limits.diskMiB === undefined ? {} : { diskMiB: manifest.limits.diskMiB }), | |
| 142 | }, | |
| 152 | limits: sized.limits, | |
| 143 | 153 | timeoutS: manifest.timeout, |
| 144 | 154 | egress: [...STEP_HOSTS, ...manifest.egress], |
| 145 | 155 | egressMode: outside ? "enforce" : egressSetting(app.db), |
src/checks/wide.ts added +173
| @@ -0,0 +1,173 @@ | ||
| 1 | /** | |
| 2 | * A pull's check widens when the machine has room: twice the shards it would run, with twice the | |
| 3 | * CPU and memory its cell may use, when no other check waits and the host has had idle cores for | |
| 4 | * minutes, not one sample. A large check's test phase is about 85% of its time and uses about | |
| 5 | * 7.4 cores at 8 shards (2026-12-02: 3,359–4,039 s of slices, 441–525 s of wall); at 16 the same | |
| 6 | * work takes about half the wall. Under load nothing widens: the cores belong to the checks that | |
| 7 | * wait, and a wider check would only move CPU from one to another. | |
| 8 | * | |
| 9 | * Room is real idle, not load: vxrail's load average stays at 1.2–2.1 per core even when a fifth of | |
| 10 | * its cores are idle, since it counts the idle-priority work that runs there. Idle is read from the | |
| 11 | * machine sampler's `node` points (`boards/sampler.ts`: the host's `/proc/stat` busy time every | |
| 12 | * 15 s, which is the host's own inside the forge's container), averaged over the last one and five | |
| 13 | * minutes against the host's CPUs. Over six hours on 2026-12-02 the five-minute idle ran p10 13.7%, | |
| 14 | * median 21.6%, p90 28.8%; widening adds about 7.4 busy cores, 9% of 80, so the line is 25%: a | |
| 15 | * check widens in the quieter quarter of the day and leaves about 16% idle once it has. | |
| 16 | * | |
| 17 | * Decided once, when the check is handed to a runner: a check that is running never narrows, and | |
| 18 | * the next one is decided afresh, so a check that arrives while a wide one runs starts at the | |
| 19 | * setting's count. The same reading (`roomNow`) is what more runners for a long queue would read: | |
| 20 | * idle cores go to whichever the moment needs. | |
| 21 | * | |
| 22 | * FORGE_WIDE_IDLE the host's idle share, over one and five minutes, at or over which a check | |
| 23 | * may widen; `0` never widens. | |
| 24 | * @default 0.25 | |
| 25 | */ | |
| 26 | ||
| 27 | import type { Database } from "bun:sqlite"; | |
| 28 | import { listActive } from "../core/runs.ts"; | |
| 29 | import { inVerifyLane } from "./verify.ts"; | |
| 30 | ||
| 31 | /** | |
| 32 | * The most shards a widened check runs, and the most CPU and memory its cell asks for: a runner's | |
| 33 | * own container (`--cpus 16 --memory 24g`). The 16 is a CFS quota, not a cpuset: the runner sees | |
| 34 | * the host's 72 cores (`availableParallelism`), so it would accept a cell of 32, and the kernel | |
| 35 | * would still give the container 16. Shards past 16 would only take turns on the same cores, and a | |
| 36 | * test's own time budget would read the turns as slowness. So a widened check never passes 16 | |
| 37 | * shards, and its cell never passes the cores those shards can have. | |
| 38 | */ | |
| 39 | export const WIDE_CPUS_MAX = 16; | |
| 40 | export const WIDE_MEMORY_MIB_MAX = 24 * 1024; | |
| 41 | ||
| 42 | const DEFAULT_IDLE = 0.25; | |
| 43 | ||
| 44 | /** What the decision reads: checks waiting, and the host's idle share over one and five minutes. */ | |
| 45 | export interface Room { | |
| 46 | readonly waiting: number; | |
| 47 | /** Undefined when the sampler has no points for the window: no room is assumed. */ | |
| 48 | readonly idle1: number | undefined; | |
| 49 | readonly idle5: number | undefined; | |
| 50 | } | |
| 51 | ||
| 52 | /** | |
| 53 | * The host's idle share over the last `ms`, from the sampler's `node` points; undefined with none. | |
| 54 | */ | |
| 55 | function idleOver(db: Database, ms: number, now: number): number | undefined { | |
| 56 | const since = new Date(now - ms).toISOString(); | |
| 57 | const busy = db | |
| 58 | .query<{ cores: number | null; n: number }, [string]>( | |
| 59 | "SELECT AVG(cpu_cores) AS cores, COUNT(cpu_cores) AS n FROM machine_sample WHERE step = 15 AND target = 'node' AND at >= ?1", | |
| 60 | ) | |
| 61 | .get(since); | |
| 62 | const cpus = db | |
| 63 | .query<{ n: number }, [string]>("SELECT COUNT(DISTINCT target) AS n FROM machine_sample WHERE step = 15 AND target LIKE 'cpu:%' AND at >= ?1") | |
| 64 | .get(since)?.n ?? 0; | |
| 65 | if (busy === null || busy.cores === null || busy.n === 0 || cpus === 0) return undefined; | |
| 66 | return Math.max(0, Math.min(1, 1 - busy.cores / cpus)); | |
| 67 | } | |
| 68 | ||
| 69 | const realIdle = (db: Database, now: number) => ({ idle1: idleOver(db, 60_000, now), idle5: idleOver(db, 300_000, now) }); | |
| 70 | let idleSource: (db: Database, now: number) => { idle1: number | undefined; idle5: number | undefined } = realIdle; | |
| 71 | ||
| 72 | /** Tests swap the idle source; pass nothing to restore it. */ | |
| 73 | export function setWideIdleSource(source?: () => { idle1: number | undefined; idle5: number | undefined }): void { | |
| 74 | idleSource = source === undefined ? realIdle : () => source(); | |
| 75 | } | |
| 76 | ||
| 77 | /** `FORGE_WIDE_IDLE`: a share from 0 to 1, else the default. */ | |
| 78 | export function wideThreshold(raw: string | undefined = process.env["FORGE_WIDE_IDLE"]): number { | |
| 79 | const text = (raw ?? "").trim(); | |
| 80 | if (text === "") return DEFAULT_IDLE; | |
| 81 | const value = Number(text); | |
| 82 | return value >= 0 && value <= 1 ? value : DEFAULT_IDLE; | |
| 83 | } | |
| 84 | ||
| 85 | /** The room now: checks queued other than `self`, and the host's idle share. */ | |
| 86 | export function roomNow(db: Database, self: string, now: number = Date.now()): Room { | |
| 87 | const waiting = listActive(db, "check").filter((r) => r.state === "queued" && r.id !== self && !inVerifyLane(r.env)).length; | |
| 88 | return { waiting, ...idleSource(db, now) }; | |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * The room now, or none when it cannot be read: a reading that fails (a database busy or closed, | |
| 93 | * the sampler's table missing) never stops a check from being handed over; it starts narrow, and | |
| 94 | * the log says the room was unreadable, so a broken sampler reads differently from a busy host. | |
| 95 | */ | |
| 96 | export function roomOrNone(db: Database, self: string, say: (line: string) => void): Room | undefined { | |
| 97 | try { | |
| 98 | return roomNow(db, self); | |
| 99 | } catch (e) { | |
| 100 | say(`not widened: room unreadable (${e instanceof Error ? e.message : String(e)})`); | |
| 101 | return undefined; | |
| 102 | } | |
| 103 | } | |
| 104 | ||
| 105 | /** | |
| 106 | * Whether a check widens in `room`: nothing waits, and both idle shares are at or over the line. | |
| 107 | */ | |
| 108 | export function widens(room: Room, threshold: number = wideThreshold()): boolean { | |
| 109 | return ( | |
| 110 | threshold !== 0 && | |
| 111 | room.waiting === 0 && | |
| 112 | room.idle1 !== undefined && | |
| 113 | room.idle5 !== undefined && | |
| 114 | room.idle1 >= threshold && | |
| 115 | room.idle5 >= threshold | |
| 116 | ); | |
| 117 | } | |
| 118 | ||
| 119 | const pct = (v: number | undefined): string => (v === undefined ? "unknown" : `${Math.round(v * 100)}%`); | |
| 120 | ||
| 121 | /** A cell's limits as a runner job carries them: what forge.json declared, or a widened check's. */ | |
| 122 | export interface CellLimits { | |
| 123 | readonly cpus?: number; | |
| 124 | readonly memoryMiB?: number; | |
| 125 | readonly diskMiB?: number; | |
| 126 | } | |
| 127 | ||
| 128 | /** | |
| 129 | * A widened check's shard count and cell, from its shards and its cell as it would run narrow | |
| 130 | * (`base`: what forge.json declares, or the step default where it declares nothing, whichever is | |
| 131 | * larger). The shards double, up to {@link WIDE_CPUS_MAX}; the cell doubles with them and has a | |
| 132 | * CPU for each shard, so the cell watch's line (twice its CPUs) grows with the work. The caps bound | |
| 133 | * only the growth: a cell is never smaller wide than it is narrow. Disk is the declared figure: | |
| 134 | * more shards write no more. | |
| 135 | */ | |
| 136 | export function widened( | |
| 137 | shards: number, | |
| 138 | base: { readonly cpus: number; readonly memoryMiB: number }, | |
| 139 | diskMiB: number | undefined, | |
| 140 | ): { shards: number; limits: CellLimits } { | |
| 141 | const wide = Math.min(shards * 2, Math.max(shards, WIDE_CPUS_MAX)); | |
| 142 | return { | |
| 143 | shards: wide, | |
| 144 | limits: { | |
| 145 | cpus: Math.max(base.cpus, Math.min(WIDE_CPUS_MAX, Math.max(base.cpus * 2, wide))), | |
| 146 | memoryMiB: Math.max(base.memoryMiB, Math.min(WIDE_MEMORY_MIB_MAX, base.memoryMiB * 2)), | |
| 147 | ...(diskMiB === undefined ? {} : { diskMiB }), | |
| 148 | }, | |
| 149 | }; | |
| 150 | } | |
| 151 | ||
| 152 | /** | |
| 153 | * What a check is handed with, and the log line that says why: widened when there is room and | |
| 154 | * a runner can give more shards than the setting does; else the setting's count and the declared | |
| 155 | * cell, unchanged. | |
| 156 | */ | |
| 157 | export function sizeCheck( | |
| 158 | room: Room, | |
| 159 | shards: number, | |
| 160 | declared: CellLimits, | |
| 161 | effective: { readonly cpus: number; readonly memoryMiB: number }, | |
| 162 | threshold: number = wideThreshold(), | |
| 163 | ): { shards: number; limits: CellLimits; line: string } { | |
| 164 | const narrow = { shards, limits: declared }; | |
| 165 | const idle = `host idle ${pct(room.idle1)}/${pct(room.idle5)} over 1/5 min`; | |
| 166 | if (room.waiting > 0) return { ...narrow, line: `not widened: ${room.waiting} check${room.waiting === 1 ? "" : "s"} waiting` }; | |
| 167 | if (!widens(room, threshold)) return { ...narrow, line: `not widened: ${idle}, under ${pct(threshold)}` }; | |
| 168 | const base = { cpus: Math.max(declared.cpus ?? 0, effective.cpus), memoryMiB: Math.max(declared.memoryMiB ?? 0, effective.memoryMiB) }; | |
| 169 | const wide = widened(shards, base, declared.diskMiB); | |
| 170 | if (wide.shards <= shards) return { ...narrow, line: `not widened: ${shards} shards is already a runner's ${WIDE_CPUS_MAX} CPUs` }; | |
| 171 | const cell = `a cell of ${wide.limits.cpus} CPUs and ${wide.limits.memoryMiB} MiB`; | |
| 172 | return { ...wide, line: `widened to ${wide.shards} shards and ${cell}: nothing waits, ${idle}, at or over ${pct(threshold)}` }; | |
| 173 | } |
test/wide-checks.test.ts added +239
| @@ -0,0 +1,239 @@ | ||
| 1 | /** | |
| 2 | * A pull's check widens when the machine has room (`checks/wide.ts`): twice the shards, up to a | |
| 3 | * runner's 16 CPUs, and a cell grown with them when nothing waits and the host has had idle cores | |
| 4 | * over one and five minutes; the setting's count otherwise. Decided once, when the check is handed | |
| 5 | * to a runner: a running check keeps what it was given, and the next one is decided afresh. | |
| 6 | */ | |
| 7 | import { afterAll, afterEach, beforeAll, expect, test } from "bun:test"; | |
| 8 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createRepo } from "../src/actions/repos.ts"; | |
| 12 | import { createApp, type App } from "../src/app.ts"; | |
| 13 | import type { CheckManifest } from "../src/apps/manifest.ts"; | |
| 14 | import type { LaneState } from "../src/checks/lane_state.ts"; | |
| 15 | import type { Execution } from "../src/checks/local_runner.ts"; | |
| 16 | import type { RunnerJob } from "../src/checks/runner_jobs.ts"; | |
| 17 | import type { runRunnerStep } from "../src/checks/runner_step.ts"; | |
| 18 | import { tryRunner } from "../src/checks/tier_runner.ts"; | |
| 19 | import { roomNow, setWideIdleSource, sizeCheck, widened, widens, WIDE_CPUS_MAX, WIDE_MEMORY_MIB_MAX } from "../src/checks/wide.ts"; | |
| 20 | import { setCapacity } from "../src/core/capacity.ts"; | |
| 21 | import type { Repo } from "../src/core/repos.ts"; | |
| 22 | import { createRun } from "../src/core/runs.ts"; | |
| 23 | import { createUser } from "../src/core/users.ts"; | |
| 24 | ||
| 25 | /** | |
| 26 | * The cell memory ceiling is the machine's; the tests pin one, so their figures read the same | |
| 27 | * everywhere. | |
| 28 | */ | |
| 29 | const priorCeiling = process.env["FORGE_CELL_MEMORY_MAX_MIB"]; // env-restored: afterAll | |
| 30 | let dir = ""; | |
| 31 | let app: App; | |
| 32 | let repo: Repo; | |
| 33 | beforeAll(async () => { | |
| 34 | process.env["FORGE_CELL_MEMORY_MAX_MIB"] = "65536"; // env-restored: afterAll | |
| 35 | dir = mkdtempSync(join(tmpdir(), "wide-checks-")); | |
| 36 | app = createApp({ dataDir: dir, port: 0 }); | |
| 37 | const u = await createUser(app.db, { | |
| 38 | origin: "seed", | |
| 39 | trusted: true, | |
| 40 | username: "alice", | |
| 41 | email: "[email protected]", | |
| 42 | password: "a-good-passphrase", | |
| 43 | adminIfFirst: true, | |
| 44 | }); | |
| 45 | if (!u.ok) throw new Error(u.error); | |
| 46 | const made = await createRepo(app, u.value, { name: "game", visibility: "private" }); | |
| 47 | if (!made.ok) throw new Error(String(made.error)); | |
| 48 | repo = made.value; | |
| 49 | setCapacity(app.db, "checks.shards", "8", "admin"); | |
| 50 | }); | |
| 51 | afterEach(() => { | |
| 52 | setWideIdleSource(); | |
| 53 | app.db.query("UPDATE run SET state = 'ok' WHERE kind = 'check' AND state = 'queued'").run(); | |
| 54 | }); | |
| 55 | afterAll(() => { | |
| 56 | if (priorCeiling === undefined) delete process.env["FORGE_CELL_MEMORY_MAX_MIB"]; | |
| 57 | else process.env["FORGE_CELL_MEMORY_MAX_MIB"] = priorCeiling; | |
| 58 | app.close(); | |
| 59 | rmSync(dir, { recursive: true, force: true }); | |
| 60 | }); | |
| 61 | ||
| 62 | const manifest: CheckManifest = { | |
| 63 | run: "true", | |
| 64 | install: null, | |
| 65 | fast: "true", | |
| 66 | full: "true", | |
| 67 | verify: "true", | |
| 68 | timeout: 600, | |
| 69 | egress: [], | |
| 70 | limits: { cpus: 8, memoryMiB: 8192, diskMiB: 8192 }, | |
| 71 | }; | |
| 72 | ||
| 73 | const lane = (): LaneState => | |
| 74 | ({ app, options: { forgeUrl: "http://forge.test" }, placement: { mode: "runner", waitMs: 300 }, stopped: false }) as unknown as LaneState; | |
| 75 | ||
| 76 | /** The job a check is handed to a runner with, and its log; no runner takes it. */ | |
| 77 | async function handed(tier: Execution["tier"] = "fast", limits: CheckManifest["limits"] = manifest.limits): Promise<{ job: Omit<RunnerJob, "id">; log: string[] }> { | |
| 78 | const run = createRun(app.db, { | |
| 79 | repoId: repo.id, | |
| 80 | kind: "check", | |
| 81 | env: tier === "verify" ? "verify" : "pull-1", | |
| 82 | commitOid: "a".repeat(40), | |
| 83 | }); | |
| 84 | app.db.query("UPDATE run SET state = 'running' WHERE id = ?1").run(run.id); | |
| 85 | const log: string[] = []; | |
| 86 | const write = (l: string): void => void log.push(l); | |
| 87 | let job: Omit<RunnerJob, "id"> | undefined; | |
| 88 | const send: typeof runRunnerStep = async (_app, input) => { | |
| 89 | job = input.job; | |
| 90 | return { taken: false, why: "no runner took it" }; | |
| 91 | }; | |
| 92 | const signal = new AbortController().signal; | |
| 93 | const x: Execution = { | |
| 94 | id: run.id, | |
| 95 | runId: run.id, | |
| 96 | repo, | |
| 97 | bare: join(app.reposRoot, "alice", "game.git"), | |
| 98 | commitOid: run.commitOid, | |
| 99 | tier, | |
| 100 | ...(tier === "verify" ? { keepImpact: true, isMain: true } : {}), | |
| 101 | write, | |
| 102 | signal, | |
| 103 | }; | |
| 104 | await tryRunner( | |
| 105 | lane(), | |
| 106 | x, | |
| 107 | { | |
| 108 | run, | |
| 109 | repo, | |
| 110 | bare: x.bare, | |
| 111 | manifest: { ...manifest, limits }, | |
| 112 | command: "true", | |
| 113 | base: undefined, | |
| 114 | builtOnBase: undefined, | |
| 115 | learned: "", | |
| 116 | reverts: {}, | |
| 117 | tree: { ok: true, value: "b".repeat(40) }, | |
| 118 | key: undefined, | |
| 119 | outside: false, | |
| 120 | write, | |
| 121 | signal, | |
| 122 | }, | |
| 123 | send, | |
| 124 | ); | |
| 125 | app.db.query("UPDATE run SET state = 'ok' WHERE id = ?1").run(run.id); | |
| 126 | if (job === undefined) throw new Error("not handed"); | |
| 127 | return { job, log }; | |
| 128 | } | |
| 129 | ||
| 130 | const queueOne = (): void => { | |
| 131 | createRun(app.db, { repoId: repo.id, kind: "check", env: "pull-2", commitOid: "c".repeat(40) }); | |
| 132 | }; | |
| 133 | ||
| 134 | test("nothing waits and the host has been idle: twice the shards, twice the cell, said in the log", async () => { | |
| 135 | setWideIdleSource(() => ({ idle1: 0.4, idle5: 0.35 })); | |
| 136 | const { job, log } = await handed(); | |
| 137 | expect(job.env["FORGE_CHECK_SHARDS"]).toBe("16"); | |
| 138 | expect(job.limits).toEqual({ cpus: 16, memoryMiB: 16384, diskMiB: 8192 }); | |
| 139 | expect(log.some((l) => l.startsWith("widened to 16 shards and a cell of 16 CPUs and 16384 MiB: nothing waits, host idle 40%/35%"))).toBe(true); | |
| 140 | }); | |
| 141 | ||
| 142 | test("a check waiting keeps the setting's count and the declared cell", async () => { | |
| 143 | setWideIdleSource(() => ({ idle1: 0.5, idle5: 0.5 })); | |
| 144 | queueOne(); | |
| 145 | const { job, log } = await handed(); | |
| 146 | expect(job.env["FORGE_CHECK_SHARDS"]).toBe("8"); | |
| 147 | expect(job.limits).toEqual({ cpus: 8, memoryMiB: 8192, diskMiB: 8192 }); | |
| 148 | expect(log).toContain("not widened: 1 check waiting"); | |
| 149 | }); | |
| 150 | ||
| 151 | test("a busy host keeps it narrow, over the last minute or the last five, and so does one with no samples", async () => { | |
| 152 | for (const load of [{ idle1: 0.1, idle5: 0.4 }, { idle1: 0.4, idle5: 0.1 }, { idle1: undefined, idle5: undefined }]) { | |
| 153 | setWideIdleSource(() => load); | |
| 154 | const { job } = await handed(); | |
| 155 | expect(job.env["FORGE_CHECK_SHARDS"]).toBe("8"); | |
| 156 | } | |
| 157 | }); | |
| 158 | ||
| 159 | test("a check that arrives while a wide one runs starts narrow; the wide one keeps what it was given", async () => { | |
| 160 | setWideIdleSource(() => ({ idle1: 0.5, idle5: 0.5 })); | |
| 161 | const first = await handed(); | |
| 162 | expect(first.job.env["FORGE_CHECK_SHARDS"]).toBe("16"); | |
| 163 | queueOne(); | |
| 164 | const next = await handed(); | |
| 165 | expect(next.job.env["FORGE_CHECK_SHARDS"]).toBe("8"); | |
| 166 | expect(first.job.env["FORGE_CHECK_SHARDS"]).toBe("16"); | |
| 167 | expect(first.job.limits.cpus).toBe(16); | |
| 168 | }); | |
| 169 | ||
| 170 | test("main's whole verification never widens: it has its own count", async () => { | |
| 171 | setWideIdleSource(() => ({ idle1: 1, idle5: 1 })); | |
| 172 | const { job, log } = await handed("verify"); | |
| 173 | expect(job.limits).toEqual({ cpus: 8, memoryMiB: 8192, diskMiB: 8192 }); | |
| 174 | expect(log.some((l) => l.includes("widened"))).toBe(false); | |
| 175 | }); | |
| 176 | ||
| 177 | test("a manifest that declares no limits widens from the defaults: the cell grows with its shards", async () => { | |
| 178 | setWideIdleSource(() => ({ idle1: 0.5, idle5: 0.5 })); | |
| 179 | const narrow = await handed("fast", {}); | |
| 180 | expect(narrow.job.env["FORGE_CHECK_SHARDS"]).toBe("16"); | |
| 181 | // The step default is 2 CPUs and 2048 MiB: memory doubles, and the cell has a CPU per shard. | |
| 182 | expect(narrow.job.limits).toEqual({ cpus: 16, memoryMiB: 4096 }); | |
| 183 | queueOne(); | |
| 184 | expect((await handed("fast", {})).job.limits).toEqual({}); | |
| 185 | }); | |
| 186 | ||
| 187 | test("a cell declared larger than a runner's caps never shrinks when it widens", async () => { | |
| 188 | setWideIdleSource(() => ({ idle1: 0.5, idle5: 0.5 })); | |
| 189 | const { job } = await handed("fast", { cpus: 24, memoryMiB: 32768, diskMiB: 8192 }); | |
| 190 | expect(job.env["FORGE_CHECK_SHARDS"]).toBe("16"); | |
| 191 | expect(job.limits.cpus).toBeGreaterThanOrEqual(24); | |
| 192 | expect(job.limits.memoryMiB).toBeGreaterThanOrEqual(32768); | |
| 193 | expect(job.limits).toEqual({ cpus: 24, memoryMiB: 32768, diskMiB: 8192 }); | |
| 194 | // The rule alone: the caps bound growth, never what the cell already had. | |
| 195 | expect(widened(8, { cpus: 24, memoryMiB: 32768 }, undefined).limits).toEqual({ cpus: 24, memoryMiB: 32768 }); | |
| 196 | }); | |
| 197 | ||
| 198 | test("the policy: off at 0, and shards and cell never past a runner's own 16 CPUs and 24 GiB", () => { | |
| 199 | expect(widens({ waiting: 0, idle1: 1, idle5: 1 }, 0)).toBe(false); | |
| 200 | expect(widens({ waiting: 0, idle1: 0.25, idle5: 0.25 }, 0.25)).toBe(true); | |
| 201 | expect(widened(8, { cpus: 12, memoryMiB: 16384 }, undefined).limits).toEqual({ cpus: WIDE_CPUS_MAX, memoryMiB: WIDE_MEMORY_MIB_MAX }); | |
| 202 | // A setting of 12 widens to the runner's 16, not 24, in a cell of 16. | |
| 203 | expect(widened(12, { cpus: 2, memoryMiB: 2048 }, 8192)).toEqual({ shards: 16, limits: { cpus: 16, memoryMiB: 4096, diskMiB: 8192 } }); | |
| 204 | // A setting at 16 already has a runner's CPUs: nothing to widen, and the log says so. | |
| 205 | const room = { waiting: 0, idle1: 0.9, idle5: 0.9 }; | |
| 206 | expect(sizeCheck(room, 16, { cpus: 2 }, { cpus: 2, memoryMiB: 2048 }, 0.25)).toEqual({ | |
| 207 | shards: 16, | |
| 208 | limits: { cpus: 2 }, | |
| 209 | line: "not widened: 16 shards is already a runner's 16 CPUs", | |
| 210 | }); | |
| 211 | }); | |
| 212 | ||
| 213 | test("room is the host's idle share from the sampler's node points, over one and five minutes", () => { | |
| 214 | setWideIdleSource(); | |
| 215 | const now = Date.parse("2026-12-02T05:00:00.000Z"); | |
| 216 | const put = app.db.query("INSERT INTO machine_sample (at, step, target, cpu_cores) VALUES (?1, 15, ?2, ?3)"); | |
| 217 | // Four CPUs. The last minute: 1 of 4 busy. The four minutes before: 3 of 4 busy. | |
| 218 | for (const t of [10_000, 25_000, 40_000]) { | |
| 219 | const at = new Date(now - t).toISOString(); | |
| 220 | put.run(at, "node", 1); | |
| 221 | for (const cpu of ["cpu:000", "cpu:001", "cpu:002", "cpu:003"]) put.run(at, cpu, 0.25); | |
| 222 | } | |
| 223 | for (const t of [90_000, 150_000, 210_000]) put.run(new Date(now - t).toISOString(), "node", 3); | |
| 224 | const room = roomNow(app.db, "none", now); | |
| 225 | expect(room.idle1).toBeCloseTo(0.75); | |
| 226 | expect(room.idle5).toBeCloseTo(1 - (3 * 1 + 3 * 3) / 6 / 4); | |
| 227 | // With no points in the window, no room is assumed. | |
| 228 | expect(roomNow(app.db, "none", now + 3_600_000).idle1).toBeUndefined(); | |
| 229 | }); | |
| 230 | ||
| 231 | test("a room that cannot be read starts the check narrow, and never stops it being handed over", async () => { | |
| 232 | setWideIdleSource(() => { | |
| 233 | throw new Error("no sampler table"); | |
| 234 | }); | |
| 235 | const { job, log } = await handed(); | |
| 236 | expect(job.env["FORGE_CHECK_SHARDS"]).toBe("8"); | |
| 237 | expect(log).toContain("not widened: room unreadable (no sampler table)"); | |
| 238 | }); | |
| 239 |
new above
offline
A cap on cpus with no matching cap on shards. 12 shards double to 24 in a 16-CPU cell. Cap the shards by the cell's cpus, or drop the cap.