Public: anyone can read this; the crew can change it.
Sentinel: queue:check was degraded 36 minutes today: 10 checks wait behind one lane; the oldest since 02:14; the lane runs #4838 for 3 min (limit 60). #16591
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/5130-sentinel-queue-check-was-degraded-36-min → main 0 commits fork 2 threads · 2 unresolved
6 files
changed +223
−13
src/checks/red_main.ts +27 −5
| @@ -19,6 +19,7 @@ | ||
| 19 | 19 | |
| 20 | 20 | import type { Database } from "bun:sqlite"; |
| 21 | 21 | import type { Run } from "../core/runs.ts"; |
| 22 | import { checkLimitSentence } from "../settings/quotas.ts"; | |
| 22 | 23 | |
| 23 | 24 | export interface RedMain { |
| 24 | 25 | readonly repoId: string; |
| @@ -240,20 +241,28 @@ export function laneSince( | ||
| 240 | 241 | /** |
| 241 | 242 | * The queued checks no red main holds (startable now): how many, and the |
| 242 | 243 | * oldest one's `laneSince`. A held check's age says nothing about the lane, |
| 243 | * and nor does the time a released one spent held. | |
| 244 | * and nor does the time a released one spent held. Those their own | |
| 245 | * account's concurrent-checks limit holds (`checkLimitSentence`, the rule the | |
| 246 | * scheduler keeps them queued by) wait for it, not the lane: counted apart in | |
| 247 | * `byAccount`, and never the oldest. One query for the queue, then the limit | |
| 248 | * asked once per distinct owner, never per check. | |
| 244 | 249 | */ |
| 245 | 250 | export function freeChecks( |
| 246 | 251 | db: Database, |
| 247 | 252 | redRepoIds: readonly string[], |
| 248 | 253 | now: number, |
| 249 | 254 | holdMs: number = RED_HOLD_MS, |
| 250 | ): { n: number; oldest: string | null } { | |
| 255 | ): { n: number; oldest: string | null; byAccount: number } { | |
| 251 | 256 | const rows = db |
| 252 | .query<ReleaseRow & { issue_id: string | null; reason: string; created_at: string }, [string, string]>( | |
| 253 | `SELECT q.issue_id, q.reason, q.created_at, | |
| 257 | .query< | |
| 258 | ReleaseRow & { owner: string | null; issue_id: string | null; reason: string; created_at: string }, | |
| 259 | [string, string] | |
| 260 | >( | |
| 261 | `SELECT o.owner_id AS owner, q.issue_id, q.reason, q.created_at, | |
| 254 | 262 | last.state AS last_state, last.finished_at AS green_at, |
| 255 | 263 | prev.state AS prev_state, prev.summary AS prev_summary, prev.finished_at AS red_at |
| 256 | 264 | FROM run q |
| 265 | LEFT JOIN repo o ON o.id = q.repo_id | |
| 257 | 266 | LEFT JOIN run last ON last.id = ${mainBefore("")} |
| 258 | 267 | LEFT JOIN run prev ON prev.id = ${mainBefore("AND r.id < last.id")} |
| 259 | 268 | WHERE q.state = 'queued' AND q.kind = 'check' |
| @@ -263,7 +272,20 @@ export function freeChecks( | ||
| 263 | 272 | ) |
| 264 | 273 | .all(new Date(now).toISOString(), JSON.stringify(redRepoIds)); |
| 265 | 274 | let oldest: string | null = null; |
| 275 | let byAccount = 0; | |
| 276 | const atLimit = new Map<string, boolean>(); | |
| 277 | // A check whose repository is gone has no owner to hold it, as in the scheduler. | |
| 278 | const held = (owner: string | null): boolean => { | |
| 279 | if (owner === null) return false; | |
| 280 | let full = atLimit.get(owner); | |
| 281 | if (full === undefined) atLimit.set(owner, (full = checkLimitSentence(db, owner) !== undefined)); | |
| 282 | return full; | |
| 283 | }; | |
| 266 | 284 | for (const row of rows) { |
| 285 | if (held(row.owner)) { | |
| 286 | byAccount++; | |
| 287 | continue; | |
| 288 | } | |
| 267 | 289 | const since = laneSince( |
| 268 | 290 | { issueId: row.issue_id, reason: row.reason, createdAt: row.created_at }, |
| 269 | 291 | toRelease(row), |
| @@ -271,7 +293,7 @@ export function freeChecks( | ||
| 271 | 293 | ); |
| 272 | 294 | if (oldest === null || Date.parse(since) < Date.parse(oldest)) oldest = since; |
| 273 | 295 | } |
| 274 | return { n: rows.length, oldest }; | |
| 296 | return { n: rows.length - byAccount, oldest, byAccount }; | |
| 275 | 297 | } |
| 276 | 298 | |
| 277 | 299 | /** |
src/checks/scheduler.ts +2 −4
| @@ -9,7 +9,7 @@ import { rebasesFirst } from "./finishing_first.ts"; | ||
| 9 | 9 | import { inVerifyLane } from "./verify.ts"; |
| 10 | 10 | import { appendRunLog } from "../runs/log.ts"; |
| 11 | 11 | import { memoryHold } from "../runs/memory_hold.ts"; |
| 12 | import { runningLimitSentence, runningRuns } from "../settings/quotas.ts"; | |
| 12 | import { checkLimitSentence } from "../settings/quotas.ts"; | |
| 13 | 13 | import { usedRefusal } from "../apps/disk_guard.ts"; |
| 14 | 14 | import { rankOnce } from "./ranked_first.ts"; |
| 15 | 15 | import { gateQueued, gatesInFlight } from "./queue.ts"; |
| @@ -27,9 +27,7 @@ function atLimit(lane: LaneState, run: Run): boolean { | ||
| 27 | 27 | const { app } = lane; |
| 28 | 28 | const owner = getRepoById(app.db, run.repoId)?.ownerId; |
| 29 | 29 | if (owner === undefined) return false; |
| 30 | const full = runningLimitSentence(app.db, "checks", "concurrent checks", owner, () => | |
| 31 | runningRuns(app.db, "check", owner, lane.inFlight.keys()), | |
| 32 | ); | |
| 30 | const full = checkLimitSentence(app.db, owner, lane.inFlight.keys()); | |
| 33 | 31 | if (full === undefined) return false; |
| 34 | 32 | if (!lane.waiting.has(run.id)) { |
| 35 | 33 | lane.waiting.add(run.id); |
src/kernel/vitals.ts +7 −2
| @@ -390,7 +390,8 @@ export const QUEUE_AGENT_FULL = "queue:agent:full"; | ||
| 390 | 390 | * what runs. With one slot: "22 checks wait behind one lane; the oldest since |
| 391 | 391 | * 04:13; the lane runs #1212 for 31 min (limit 60)". With several: "11 checks |
| 392 | 392 | * wait for 7 slots, all busy; the oldest since 01:04; the longest running is |
| 393 | * #4656 at 5 min (limit 60)". | |
| 393 | * #4656 at 5 min (limit 60)". Checks a red main holds (`held`) and checks | |
| 394 | * their own account's limit holds (`byAccount`, #5130) are named after it. | |
| 394 | 395 | */ |
| 395 | 396 | export function laneSentence( |
| 396 | 397 | queued: number, |
| @@ -399,10 +400,13 @@ export function laneSentence( | ||
| 399 | 400 | slots: number, |
| 400 | 401 | now: number, |
| 401 | 402 | held = 0, |
| 403 | byAccount = 0, | |
| 402 | 404 | ): string { |
| 403 | 405 | const since = new Date(oldest).toISOString().slice(11, 16); |
| 404 | 406 | const plural = `check${queued === 1 ? "" : "s"} ${queued === 1 ? "waits" : "wait"}`; |
| 405 | const more = held > 0 ? `; ${held} more held by a red main` : ""; | |
| 407 | const more = | |
| 408 | (held > 0 ? `; ${held} more held by a red main` : "") + | |
| 409 | (byAccount > 0 ? `; ${byAccount} more held by their account's limit` : ""); | |
| 406 | 410 | const minutes = (r: LaneRun) => |
| 407 | 411 | Math.max(0, Math.round((now - new Date(r.since).getTime()) / 60_000)); |
| 408 | 412 | const longest = runs.reduce<LaneRun | null>( |
| @@ -566,6 +570,7 @@ export function vitals(input: VitalsInput): Vitals { | ||
| 566 | 570 | Number(capacityValue(input.db, "checks.concurrency")), |
| 567 | 571 | now, |
| 568 | 572 | Math.min(held, row.queued), |
| 573 | free.byAccount, | |
| 569 | 574 | ), |
| 570 | 575 | new Date(free.oldest).toISOString(), |
| 571 | 576 | "/settings/capacity", |
src/settings/quotas.ts +17
| @@ -131,6 +131,23 @@ export function runningLimitSentence( | ||
| 131 | 131 | return n >= limit ? `${n} of ${limit} ${noun}` : undefined; |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | /** | |
| 135 | * Whether `ownerId`'s concurrent-checks limit holds one more check of theirs: | |
| 136 | * the sentence the hold says ("1 of 1 concurrent checks"), or nothing when | |
| 137 | * there is room. The one rule both the scheduler (`checks/scheduler.ts`, | |
| 138 | * `atLimit`, with its in-flight `also` ids) and the lane's vitals | |
| 139 | * (`checks/red_main.ts`, `freeChecks`) ask (#5130). | |
| 140 | */ | |
| 141 | export function checkLimitSentence( | |
| 142 | db: Database, | |
| 143 | ownerId: string, | |
| 144 | also: Iterable<string> = [], | |
| 145 | ): string | undefined { | |
| 146 | return runningLimitSentence(db, "checks", "concurrent checks", ownerId, () => | |
| 147 | runningRuns(db, "check", ownerId, also), | |
| 148 | ); | |
| 149 | } | |
| 150 | ||
| 134 | 151 | /** |
| 135 | 152 | * Runs of `kind` running now for repositories `ownerId` owns, plus the |
| 136 | 153 | * `also` ids (started a moment ago, not yet claimed in the database). |
test/account-held-lane.test.ts added +163
| @@ -0,0 +1,163 @@ | ||
| 1 | /** | |
| 2 | * A check its own account's limit holds is not the lane's wait (#5130): the | |
| 3 | * scheduler keeps it queued until one of the account's checks ends, so | |
| 4 | * `queue:check` neither counts it nor dates itself from it, and the sentence | |
| 5 | * names it apart. The scheduler and the vitals ask one rule, | |
| 6 | * `checkLimitSentence`; the last block pins them together. On in-memory | |
| 7 | * SQLite. | |
| 8 | */ | |
| 9 | import { beforeEach, describe, expect, test } from "bun:test"; | |
| 10 | import type { Database } from "bun:sqlite"; | |
| 11 | import { tmpdir } from "node:os"; | |
| 12 | import { migrations } from "../src/core/schema.ts"; | |
| 13 | import { openDb } from "../src/kernel/db.ts"; | |
| 14 | import { createRun, type Run } from "../src/core/runs.ts"; | |
| 15 | import { freeChecks } from "../src/checks/red_main.ts"; | |
| 16 | import { laneSentence, vitals } from "../src/kernel/vitals.ts"; | |
| 17 | import { checkLimitSentence, setQuota } from "../src/settings/quotas.ts"; | |
| 18 | import { ErrorLedger, clearErrors } from "../src/kernel/log.ts"; | |
| 19 | import { RefusedLedger } from "../src/kernel/internal_refused.ts"; | |
| 20 | ||
| 21 | const ago = (min: number) => new Date(Date.now() - min * 60_000).toISOString(); | |
| 22 | ||
| 23 | let db: Database; | |
| 24 | beforeEach(() => { | |
| 25 | clearErrors(); | |
| 26 | db = openDb(":memory:", migrations); | |
| 27 | db.run("INSERT INTO user (id, username, email, password_hash) VALUES ('u1', 'guest', 'g@x', '')"); | |
| 28 | db.run("INSERT INTO user (id, username, email, password_hash) VALUES ('u2', 'other', 'o@x', '')"); | |
| 29 | db.run("INSERT INTO repo (id, owner_id, name) VALUES ('r1', 'u1', 'app')"); | |
| 30 | db.run("INSERT INTO repo (id, owner_id, name) VALUES ('r2', 'u2', 'app')"); | |
| 31 | }); | |
| 32 | ||
| 33 | const running = (repoId: string): Run => { | |
| 34 | const run = createRun(db, { repoId, kind: "check" }); | |
| 35 | db.run("UPDATE run SET state = 'running', started_at = ?2, heartbeat_at = ?3 WHERE id = ?1", [ | |
| 36 | run.id, | |
| 37 | ago(5), | |
| 38 | new Date().toISOString(), | |
| 39 | ]); | |
| 40 | return run; | |
| 41 | }; | |
| 42 | const queued = (repoId: string, min: number): Run => { | |
| 43 | const run = createRun(db, { repoId, kind: "check" }); | |
| 44 | db.run("UPDATE run SET created_at = ?2 WHERE id = ?1", [run.id, ago(min)]); | |
| 45 | return run; | |
| 46 | }; | |
| 47 | const held = () => freeChecks(db, [], Date.now()).byAccount; | |
| 48 | const lane = () => | |
| 49 | vitals({ | |
| 50 | db, | |
| 51 | signals: { errors: new ErrorLedger(), refused: new RefusedLedger() }, | |
| 52 | dataDir: tmpdir(), | |
| 53 | disk: () => ({ freeBytes: 95, totalBytes: 100 }), | |
| 54 | }).degraded.find((d) => d.cause === "queue:check"); | |
| 55 | ||
| 56 | describe("checks held by their own account's limit", () => { | |
| 57 | test("an account at its limit holds its queued checks; under it, or with no limit, none", () => { | |
| 58 | running("r1"); | |
| 59 | queued("r1", 30); | |
| 60 | queued("r1", 20); | |
| 61 | expect(held()).toBe(0); | |
| 62 | setQuota(db, "checks", 1, "u1"); | |
| 63 | expect(held()).toBe(2); | |
| 64 | setQuota(db, "checks", 2, "u1"); | |
| 65 | expect(held()).toBe(0); | |
| 66 | }); | |
| 67 | ||
| 68 | test("a forge admin is never held", () => { | |
| 69 | setQuota(db, "checks", 1, "u1"); | |
| 70 | db.run("UPDATE user SET is_admin = 1 WHERE id = 'u1'"); | |
| 71 | running("r1"); | |
| 72 | queued("r1", 30); | |
| 73 | expect(held()).toBe(0); | |
| 74 | }); | |
| 75 | ||
| 76 | test("only account-held checks waiting, slots free: queue:check is not raised", () => { | |
| 77 | setQuota(db, "checks", 1, "u1"); | |
| 78 | running("r1"); | |
| 79 | queued("r1", 30); | |
| 80 | queued("r1", 20); | |
| 81 | expect(lane()).toBeUndefined(); | |
| 82 | // Without the limit the same two checks are the lane's to answer for. | |
| 83 | setQuota(db, "checks", null, "u1"); | |
| 84 | expect(lane()?.sentence).toStartWith("2 checks wait"); | |
| 85 | }); | |
| 86 | ||
| 87 | test("a free check waiting too: counted alone, dated from itself, the held named after", () => { | |
| 88 | setQuota(db, "checks", 1, "u1"); | |
| 89 | running("r1"); | |
| 90 | queued("r1", 40); | |
| 91 | queued("r1", 35); | |
| 92 | queued("r2", 15); | |
| 93 | const found = lane(); | |
| 94 | expect(found?.sentence).toStartWith("1 check waits behind one lane;"); | |
| 95 | expect(found?.sentence).toEndWith("; 2 more held by their account's limit."); | |
| 96 | expect(Date.parse(found!.since!)).toBeGreaterThan(Date.now() - 16 * 60_000); | |
| 97 | }); | |
| 98 | ||
| 99 | test("laneSentence names the red main's hold and the account's, in that order", () => { | |
| 100 | const now = Date.parse("2026-01-01T02:00:00Z"); | |
| 101 | expect(laneSentence(1, "2026-01-01T01:04:00Z", [], 4, now, 2, 3)).toBe( | |
| 102 | "1 check waits for 4 slots, none busy; the oldest since 01:04; nothing runs; 2 more held by a red main; 3 more held by their account's limit.", | |
| 103 | ); | |
| 104 | }); | |
| 105 | }); | |
| 106 | ||
| 107 | describe("the scheduler's hold and the lane's reading agree", () => { | |
| 108 | // The scheduler's `atLimit` holds a check exactly when `checkLimitSentence` | |
| 109 | // answers for its owner; `freeChecks` must count the same checks held. | |
| 110 | const agree = (owner: string, repoId: string) => { | |
| 111 | const scheduler = checkLimitSentence(db, owner) !== undefined; | |
| 112 | const lanes = freeChecks(db, [], Date.now()); | |
| 113 | const queuedHere = db | |
| 114 | .query<{ n: number }, [string]>("SELECT count(*) AS n FROM run WHERE repo_id = ?1 AND state = 'queued'") | |
| 115 | .get(repoId)!.n; | |
| 116 | expect(lanes.byAccount).toBe(scheduler ? queuedHere : 0); | |
| 117 | return scheduler; | |
| 118 | }; | |
| 119 | const setStored = (value: string) => | |
| 120 | db.run( | |
| 121 | "INSERT OR REPLACE INTO forge_setting (key, value, updated_by, updated_at) VALUES ('quota.checks', ?1, 'test', 'now')", | |
| 122 | [value], | |
| 123 | ); | |
| 124 | const newcomer = (id: string) => | |
| 125 | db.run("INSERT INTO account_request (user_id, state, asked_at) VALUES (?1, 'allowed', 'now')", [id]); | |
| 126 | ||
| 127 | beforeEach(() => { | |
| 128 | running("r1"); | |
| 129 | queued("r1", 30); | |
| 130 | }); | |
| 131 | ||
| 132 | test("no limit: neither holds", () => { | |
| 133 | expect(agree("u1", "r1")).toBe(false); | |
| 134 | }); | |
| 135 | test("a stored limit reached: both hold", () => { | |
| 136 | setQuota(db, "checks", 1, "u1"); | |
| 137 | expect(agree("u1", "r1")).toBe(true); | |
| 138 | }); | |
| 139 | test("a stored limit with room: neither holds", () => { | |
| 140 | setQuota(db, "checks", 2, "u1"); | |
| 141 | expect(agree("u1", "r1")).toBe(false); | |
| 142 | }); | |
| 143 | test("a newcomer's limit, none stored: both hold", () => { | |
| 144 | newcomer("u1"); | |
| 145 | expect(agree("u1", "r1")).toBe(true); | |
| 146 | }); | |
| 147 | test("a stored limit wins over a newcomer's: neither holds with room", () => { | |
| 148 | newcomer("u1"); | |
| 149 | setQuota(db, "checks", 2, "u1"); | |
| 150 | expect(agree("u1", "r1")).toBe(false); | |
| 151 | }); | |
| 152 | test("a forge admin at the limit: neither holds", () => { | |
| 153 | setQuota(db, "checks", 1, "u1"); | |
| 154 | db.run("UPDATE user SET is_admin = 1 WHERE id = 'u1'"); | |
| 155 | expect(agree("u1", "r1")).toBe(false); | |
| 156 | }); | |
| 157 | test("an invalid stored value is no limit: neither holds, a newcomer's then applies to both", () => { | |
| 158 | setStored("lots"); | |
| 159 | expect(agree("u1", "r1")).toBe(false); | |
| 160 | newcomer("u1"); | |
| 161 | expect(agree("u1", "r1")).toBe(true); | |
| 162 | }); | |
| 163 | }); |
test/vitals.test.ts +7 −2
| @@ -170,6 +170,9 @@ test("the number of queries does not grow with runs", () => { | ||
| 170 | 170 | vitals({ db: counting, signals: app.signals, dataDir, disk: roomy }); |
| 171 | 171 | return n; |
| 172 | 172 | }; |
| 173 | // One queued check first: its owner's limit is read once per owner (#5130), and 40 more | |
| 174 | // checks of the same owner must add nothing. | |
| 175 | createRun(app.db, { repoId, kind: "check" }); | |
| 173 | 176 | const before = count(); |
| 174 | 177 | for (let i = 0; i < 40; i++) { |
| 175 | 178 | const r = createRun(app.db, { repoId, kind: "check" }); |
| @@ -181,8 +184,10 @@ test("the number of queries does not grow with runs", () => { | ||
| 181 | 184 | // red on a full disk only while the disk is full), and the newest drill of every domain, the |
| 182 | 185 | // checks no red main holds, the week's flake list, and the database's daily size readings |
| 183 | 186 | // (#12010, at most 60 rows), the memory floor the forge keeps for itself (#12300), and the |
| 184 | // hourly cloud watch's readings, one row an account (#16329). | |
| 185 | expect(before).toBe(13); | |
| 187 | // hourly cloud watch's readings, one row an account (#16329). And, once per distinct owner of a | |
| 188 | // queued check, the concurrent-checks limit the scheduler asks (#5130): its stored value, then | |
| 189 | // whether the owner is a newcomer; two for this fixture's one owner, however many checks. | |
| 190 | expect(before).toBe(15); | |
| 186 | 191 | }); |
| 187 | 192 | |
| 188 | 193 | test("errors are counted, bounded, redacted, and a burst is said", async () => { |
new above
offline