Public: anyone can read this; the crew can change it.
A handed-over runner job is judged by its own lease, so a slow boot or the orphan lapse no longer loses it #17096
Merged 2d 3h ago, live in production
adopt-lease → main 0 commits 1 thread · 1 unresolved
4 files
changed +66
−26
src/checks/adopt.ts +5 −4
| @@ -7,14 +7,15 @@ | ||
| 7 | 7 | * forge waits on it (`holdForAdoption`). The forge that comes up takes each such check back here, |
| 8 | 8 | * before anything is scheduled, and waits on the runner's answer as the first one would have. |
| 9 | 9 | * |
| 10 | * A check whose lease went quiet longer than `STALE_AFTER_MS` is not taken back: the abandoned | |
| 11 | * sweep queues it again (`checks/abandoned.ts`), as before. An adopted check whose runner has | |
| 10 | * A handed-over job is judged by its own lease, which the runner's reports push out, not by the | |
| 11 | * run's heartbeat: no forge beats for it, and a slow boot must not lose it. Neither the boot's | |
| 12 | * orphan lapse nor the abandoned sweep touches it while that lease holds (`core/runs.ts`); one | |
| 13 | * whose lease ran out is queued again by the sweep, as before. An adopted check whose runner has | |
| 12 | 14 | * gone quiet too is queued again by the lease on its job, as any silent runner's is. |
| 13 | 15 | */ |
| 14 | 16 | |
| 15 | 17 | import { getRepoById } from "../core/repos.ts"; |
| 16 | 18 | import { getRun } from "../core/runs.ts"; |
| 17 | import { STALE_AFTER_MS } from "../runs/lease.ts"; | |
| 18 | 19 | import type { LaneState } from "./lane_state.ts"; |
| 19 | 20 | import { adoptableJobs, deskOf, takeBackJob } from "./runner_jobs.ts"; |
| 20 | 21 | import { start } from "./start.ts"; |
| @@ -25,7 +26,7 @@ export function adoptRunnerJobs(lane: LaneState, now: number = Date.now()): numb | ||
| 25 | 26 | const { db } = lane.app; |
| 26 | 27 | const desk = deskOf(lane.app); |
| 27 | 28 | let taken = 0; |
| 28 | for (const { jobId, runId } of adoptableJobs(db, now, STALE_AFTER_MS)) { | |
| 29 | for (const { jobId, runId } of adoptableJobs(db, now)) { | |
| 29 | 30 | if (lane.inFlight.has(runId)) continue; |
| 30 | 31 | const run = getRun(db, runId); |
| 31 | 32 | const repo = run === undefined ? undefined : getRepoById(db, run.repoId); |
src/checks/runner_jobs.ts +11 −10
| @@ -818,21 +818,22 @@ export interface Adoptable { | ||
| 818 | 818 | } |
| 819 | 819 | |
| 820 | 820 | /** |
| 821 | * The claimed jobs a forge handed over (`handOverJob`) whose check is still running and was held | |
| 822 | * within `freshMs`: their runner may still be at work. A check whose lease went quiet longer than | |
| 823 | * that is the abandoned sweep's (`checks/abandoned.ts`), and a forge that died without handing | |
| 824 | * over leaves its checks to that sweep too. | |
| 821 | * The claimed jobs a forge handed over (`handOverJob`) whose check is still running and whose | |
| 822 | * claim still holds at `now`: their runner is still at work, its reports pushing the lease out | |
| 823 | * (`holdForAdoption`). Judged by the job's lease, never the run's heartbeat: no forge beats for a | |
| 824 | * handed-over check, so a boot slower than the heartbeat's bound must not lose it (the 09:42 | |
| 825 | * release on 2026-12-01 lapsed 6). A job whose lease ran out is the abandoned sweep's | |
| 826 | * (`checks/abandoned.ts`), as is a check a forge left without handing it over. | |
| 825 | 827 | */ |
| 826 | export function adoptableJobs(db: Database, now: number, freshMs: number): Adoptable[] { | |
| 827 | const since = new Date(now - freshMs).toISOString(); | |
| 828 | export function adoptableJobs(db: Database, now: number): Adoptable[] { | |
| 828 | 829 | return db |
| 829 | .query<{ jobId: string; runId: string }, [string]>( | |
| 830 | .query<{ jobId: string; runId: string }, [number]>( | |
| 830 | 831 | `SELECT check_job.id AS jobId, check_job.run_id AS runId FROM check_job JOIN run ON run.id = check_job.run_id |
| 831 | WHERE check_job.state = 'claimed' AND check_job.result = 'handed-over' AND run.kind = 'check' AND run.state = 'running' | |
| 832 | AND run.heartbeat_at IS NOT NULL AND run.heartbeat_at >= ?1 | |
| 832 | WHERE check_job.state = 'claimed' AND check_job.result = 'handed-over' AND check_job.lease_until > ?1 | |
| 833 | AND run.kind = 'check' AND run.state = 'running' | |
| 833 | 834 | ORDER BY check_job.id`, |
| 834 | 835 | ) |
| 835 | .all(since); | |
| 836 | .all(now); | |
| 836 | 837 | } |
| 837 | 838 | |
| 838 | 839 | /** The job's own spec, as it was offered: an adopted check is judged by the steps it was sent. */ |
src/core/runs.ts +15 −6
| @@ -603,6 +603,14 @@ export function heartbeatRuns(db: Database, runIds: readonly string[]): void { | ||
| 603 | 603 | ).run(HOLDER, ...runIds); |
| 604 | 604 | } |
| 605 | 605 | |
| 606 | /** | |
| 607 | * SQL true for a run whose check a forge handed over to its runner and whose claim still holds at | |
| 608 | * `?{n}` (ms): adoption's to take back (`checks/adopt.ts`), never a sweep's to lapse or queue again. | |
| 609 | */ | |
| 610 | const handedOverHolds = (n: number): string => | |
| 611 | `EXISTS (SELECT 1 FROM check_job WHERE check_job.run_id = run.id AND check_job.state = 'claimed' | |
| 612 | AND check_job.result = 'handed-over' AND check_job.lease_until > ?${n})`; | |
| 613 | ||
| 606 | 614 | /** |
| 607 | 615 | * Lapse the lease of every running run whose holder was a process of this |
| 608 | 616 | * host that is gone (#12143): the next sweep settles it at once, instead of |
| @@ -615,11 +623,12 @@ export function lapseOrphans( | ||
| 615 | 623 | alive: (pid: number) => boolean = pidAlive, |
| 616 | 624 | ): number { |
| 617 | 625 | const host = self.split(":")[0]!; |
| 626 | // A check its forge handed over is left to adoption while its runner holds the job. | |
| 618 | 627 | const rows = db |
| 619 | .query<{ id: string; holder: string }, []>( | |
| 620 | "SELECT id, holder FROM run WHERE state = 'running' AND holder IS NOT NULL", | |
| 628 | .query<{ id: string; holder: string }, [number]>( | |
| 629 | `SELECT id, holder FROM run WHERE state = 'running' AND holder IS NOT NULL AND NOT ${handedOverHolds(1)}`, | |
| 621 | 630 | ) |
| 622 | .all(); | |
| 631 | .all(Date.now()); | |
| 623 | 632 | let lapsed = 0; |
| 624 | 633 | for (const row of rows) { |
| 625 | 634 | if (row.holder === self) continue; |
| @@ -967,7 +976,7 @@ export function sweepAbandonedChecks( | ||
| 967 | 976 | ): AbandonedChecks { |
| 968 | 977 | const cutoff = new Date(Date.now() - staleMs).toISOString(); |
| 969 | 978 | const rows = db |
| 970 | .query<RunRow & { live: number }, [string]>( | |
| 979 | .query<RunRow & { live: number }, [string, number]>( | |
| 971 | 980 | `SELECT run.*, EXISTS ( |
| 972 | 981 | SELECT 1 FROM pull JOIN issue ON issue.id = pull.issue_id |
| 973 | 982 | WHERE pull.issue_id = run.issue_id AND issue.state = 'open' |
| @@ -975,9 +984,9 @@ export function sweepAbandonedChecks( | ||
| 975 | 984 | ) AS live |
| 976 | 985 | FROM run |
| 977 | 986 | WHERE kind = 'check' AND state = 'running' |
| 978 | AND (heartbeat_at IS NULL OR heartbeat_at < ?1)`, | |
| 987 | AND (heartbeat_at IS NULL OR heartbeat_at < ?1) AND NOT ${handedOverHolds(2)}`, | |
| 979 | 988 | ) |
| 980 | .all(cutoff); | |
| 989 | .all(cutoff, Date.now()); | |
| 981 | 990 | const out = { |
| 982 | 991 | requeued: [] as { cancelled: Run; again: Run }[], |
| 983 | 992 | failed: [] as Run[], |
test/runner-adopt.test.ts +35 −6
| @@ -10,7 +10,9 @@ import { openPull } from "../src/actions/pulls.ts"; | ||
| 10 | 10 | import { commitToBranch } from "../src/actions/repos.ts"; |
| 11 | 11 | import { adoptableJobs, deskOf, enrolRunner, viewJob, type RunnerJob } from "../src/checks/runner_jobs.ts"; |
| 12 | 12 | import { startChecks } from "../src/checks/service.ts"; |
| 13 | import { getRun, RELEASE_SUMMARY } from "../src/core/runs.ts"; | |
| 13 | import { hostname } from "node:os"; | |
| 14 | import { requeueAbandonedChecks } from "../src/checks/abandoned.ts"; | |
| 15 | import { getRun, lapseOrphans, RELEASE_SUMMARY } from "../src/core/runs.ts"; | |
| 14 | 16 | import { STALE_AFTER_MS } from "../src/runs/lease.ts"; |
| 15 | 17 | import { readRunLog } from "../src/runs/log.ts"; |
| 16 | 18 | import { checkJson, checksForge } from "./helpers/checks-forge.ts"; |
| @@ -102,21 +104,48 @@ describe("a restart with a job on a runner", () => { | ||
| 102 | 104 | expect((await api(door, c.token, { step: "check", lines: ["late"] })).status).toBe(404); |
| 103 | 105 | }); |
| 104 | 106 | |
| 105 | test("a check whose lease went quiet past the abandoned bound is not taken back", async () => { | |
| 106 | const c = await claimed("quiet"); | |
| 107 | test("a boot slower than the heartbeat's bound still takes back a job whose runner holds it; the orphan lapse and the sweep leave it", async () => { | |
| 108 | const c = await claimed("slow"); | |
| 109 | const door = `/api/v1/runner/jobs/${c.job.id}`; | |
| 107 | 110 | await f.checks.stop(); |
| 108 | expect(adoptableJobs(f.app.db, Date.now(), STALE_AFTER_MS).map((a) => a.runId)).toContain(c.runId); | |
| 111 | // 150 s pass with no forge up: the run's heartbeat is past every bound, and its holder, a | |
| 112 | // process of this host, is gone (as the 09:42 release on 2026-12-01 left 6 checks). | |
| 113 | f.app.db | |
| 114 | .query("UPDATE run SET heartbeat_at = ?2, holder = ?3 WHERE id = ?1") | |
| 115 | .run(c.runId, new Date(Date.now() - STALE_AFTER_MS - 60_000).toISOString(), `${hostname()}:999999999:1`); | |
| 116 | // The runner kept reporting through it, so its claim holds. | |
| 117 | expect((await api(door, c.token, { step: "check", lines: ["through a slow boot"] })).status).toBe(503); | |
| 118 | // The boot: the orphan lapse, then the abandoned sweep, then the checks lane. | |
| 119 | expect(lapseOrphans(f.app.db)).toBe(0); | |
| 120 | requeueAbandonedChecks(f.app, STALE_AFTER_MS); | |
| 121 | expect(getRun(f.app.db, c.runId)!.state).toBe("running"); | |
| 122 | expect(adoptableJobs(f.app.db, Date.now()).map((a) => a.runId)).toContain(c.runId); | |
| 123 | f.checks = startChecks(f.app, options()); | |
| 124 | await f.until(() => deskOf(f.app).ears.has(c.job.id), "the new forge waiting on the job"); | |
| 125 | expect((await api(door, c.token, { done: { steps: [{ name: "check", ok: true, ms: 1 }], contained: true, note: "" } })).status).toBe(200); | |
| 126 | expect((await f.settled((r) => r.id === c.runId, 30_000)).state).toBe("ok"); | |
| 127 | expect(f.checksOf(c.oid).filter((r) => r.state === "queued")).toEqual([]); | |
| 128 | }); | |
| 129 | ||
| 130 | test("a handed-over job whose claim ran out (its runner went with the forge) is the sweep's, queued again", async () => { | |
| 131 | const c = await claimed("gone"); | |
| 132 | await f.checks.stop(); | |
| 133 | expect(adoptableJobs(f.app.db, Date.now()).map((a) => a.runId)).toContain(c.runId); | |
| 134 | f.app.db.query("UPDATE check_job SET lease_until = ?2 WHERE id = ?1").run(c.job.id, Date.now() - 1); | |
| 109 | 135 | f.app.db |
| 110 | 136 | .query("UPDATE run SET heartbeat_at = ?2 WHERE id = ?1") |
| 111 | 137 | .run(c.runId, new Date(Date.now() - STALE_AFTER_MS - 60_000).toISOString()); |
| 112 | expect(adoptableJobs(f.app.db, Date.now(), STALE_AFTER_MS).map((a) => a.runId)).not.toContain(c.runId); | |
| 138 | expect(adoptableJobs(f.app.db, Date.now()).map((a) => a.runId)).not.toContain(c.runId); | |
| 139 | requeueAbandonedChecks(f.app, STALE_AFTER_MS); | |
| 140 | expect(getRun(f.app.db, c.runId)!.state).toBe("cancelled"); | |
| 141 | expect(f.checksOf(c.oid).filter((r) => r.state === "queued")).toHaveLength(1); | |
| 113 | 142 | f.checks = startChecks(f.app, options()); |
| 114 | 143 | }); |
| 115 | 144 | |
| 116 | 145 | test("a job a live forge still waits on is never taken back: only one its forge handed over", async () => { |
| 117 | 146 | const c = await claimed("live"); |
| 118 | 147 | // The forge is up and waiting on it: not adoptable, though its heartbeat is fresh. |
| 119 | expect(adoptableJobs(f.app.db, Date.now(), STALE_AFTER_MS).map((a) => a.runId)).not.toContain(c.runId); | |
| 148 | expect(adoptableJobs(f.app.db, Date.now()).map((a) => a.runId)).not.toContain(c.runId); | |
| 120 | 149 | const door = `/api/v1/runner/jobs/${c.job.id}`; |
| 121 | 150 | expect((await api(door, c.token, { done: { steps: [{ name: "check", ok: true, ms: 1 }], contained: true, note: "" } })).status).toBe(200); |
| 122 | 151 | expect((await f.settled((r) => r.id === c.runId, 30_000)).state).toBe("ok"); |
new above
offline
Move this
node:osimport above the local imports to match the file's import order.