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

opened this

Merged 2d 3h ago, live in production

adopt-lease → main 0 commits 1 thread · 1 unresolved

  • Merged. adopt-lease was merged into main as d5ab98b.

Review

Sign in to review this change.
4 files changed +66 −26
src/checks/adopt.ts +5 −4
@@ -7,14 +7,15 @@
77 * forge waits on it (`holdForAdoption`). The forge that comes up takes each such check back here,
88 * before anything is scheduled, and waits on the runner's answer as the first one would have.
99 *
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
1214 * gone quiet too is queued again by the lease on its job, as any silent runner's is.
1315 */
1416
1517 import { getRepoById } from "../core/repos.ts";
1618 import { getRun } from "../core/runs.ts";
17 import { STALE_AFTER_MS } from "../runs/lease.ts";
1819 import type { LaneState } from "./lane_state.ts";
1920 import { adoptableJobs, deskOf, takeBackJob } from "./runner_jobs.ts";
2021 import { start } from "./start.ts";
@@ -25,7 +26,7 @@ export function adoptRunnerJobs(lane: LaneState, now: number = Date.now()): numb
2526 const { db } = lane.app;
2627 const desk = deskOf(lane.app);
2728 let taken = 0;
28 for (const { jobId, runId } of adoptableJobs(db, now, STALE_AFTER_MS)) {
29 for (const { jobId, runId } of adoptableJobs(db, now)) {
2930 if (lane.inFlight.has(runId)) continue;
3031 const run = getRun(db, runId);
3132 const repo = run === undefined ? undefined : getRepoById(db, run.repoId);
src/checks/runner_jobs.ts +11 −10
@@ -818,21 +818,22 @@ export interface Adoptable {
818818 }
819819
820820 /**
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.
825827 */
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[] {
828829 return db
829 .query<{ jobId: string; runId: string }, [string]>(
830 .query<{ jobId: string; runId: string }, [number]>(
830831 `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'
833834 ORDER BY check_job.id`,
834835 )
835 .all(since);
836 .all(now);
836837 }
837838
838839 /** 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 {
603603 ).run(HOLDER, ...runIds);
604604 }
605605
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
606614 /**
607615 * Lapse the lease of every running run whose holder was a process of this
608616 * host that is gone (#12143): the next sweep settles it at once, instead of
@@ -615,11 +623,12 @@ export function lapseOrphans(
615623 alive: (pid: number) => boolean = pidAlive,
616624 ): number {
617625 const host = self.split(":")[0]!;
626 // A check its forge handed over is left to adoption while its runner holds the job.
618627 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)}`,
621630 )
622 .all();
631 .all(Date.now());
623632 let lapsed = 0;
624633 for (const row of rows) {
625634 if (row.holder === self) continue;
@@ -967,7 +976,7 @@ export function sweepAbandonedChecks(
967976 ): AbandonedChecks {
968977 const cutoff = new Date(Date.now() - staleMs).toISOString();
969978 const rows = db
970 .query<RunRow & { live: number }, [string]>(
979 .query<RunRow & { live: number }, [string, number]>(
971980 `SELECT run.*, EXISTS (
972981 SELECT 1 FROM pull JOIN issue ON issue.id = pull.issue_id
973982 WHERE pull.issue_id = run.issue_id AND issue.state = 'open'
@@ -975,9 +984,9 @@ export function sweepAbandonedChecks(
975984 ) AS live
976985 FROM run
977986 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)}`,
979988 )
980 .all(cutoff);
989 .all(cutoff, Date.now());
981990 const out = {
982991 requeued: [] as { cancelled: Run; again: Run }[],
983992 failed: [] as Run[],
test/runner-adopt.test.ts +35 −6
@@ -10,7 +10,9 @@ import { openPull } from "../src/actions/pulls.ts";
1010 import { commitToBranch } from "../src/actions/repos.ts";
1111 import { adoptableJobs, deskOf, enrolRunner, viewJob, type RunnerJob } from "../src/checks/runner_jobs.ts";
1212 import { startChecks } from "../src/checks/service.ts";
13 import { getRun, RELEASE_SUMMARY } from "../src/core/runs.ts";
13 import { hostname } from "node:os";

Move this node:os import above the local imports to match the file's import order.

14 import { requeueAbandonedChecks } from "../src/checks/abandoned.ts";
15 import { getRun, lapseOrphans, RELEASE_SUMMARY } from "../src/core/runs.ts";
1416 import { STALE_AFTER_MS } from "../src/runs/lease.ts";
1517 import { readRunLog } from "../src/runs/log.ts";
1618 import { checkJson, checksForge } from "./helpers/checks-forge.ts";
@@ -102,21 +104,48 @@ describe("a restart with a job on a runner", () => {
102104 expect((await api(door, c.token, { step: "check", lines: ["late"] })).status).toBe(404);
103105 });
104106
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}`;
107110 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);
109135 f.app.db
110136 .query("UPDATE run SET heartbeat_at = ?2 WHERE id = ?1")
111137 .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);
113142 f.checks = startChecks(f.app, options());
114143 });
115144
116145 test("a job a live forge still waits on is never taken back: only one its forge handed over", async () => {
117146 const c = await claimed("live");
118147 // 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);
120149 const door = `/api/v1/runner/jobs/${c.job.id}`;
121150 expect((await api(door, c.token, { done: { steps: [{ name: "check", ok: true, ms: 1 }], contained: true, note: "" } })).status).toBe(200);
122151 expect((await f.settled((r) => r.id === c.runId, 30_000)).state).toBe("ok");

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs