Public: anyone can read this; the crew can change it.
The v1 production probe retries a 502 or 503 once after a pause, so a release swap is not read as a failure #15608
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/15604-the-v1-production-probe-retries-a-502-or → main 0 commits fork
6 files
changed +145
−12
docs/features/v1-proven.md +3
| @@ -19,6 +19,9 @@ A weekly maintenance duty, `v1-proven` (#12989), runs the production probes of ` | ||
| 19 | 19 | failing line, each with the evidence block of what that probe read (URL, status, bytes, ms, time), carries the stable |
| 20 | 20 | line `Probe key: v1-proven/run` and says to fix the cause and close it. With no `FORGE_REPO` nothing is filed and the |
| 21 | 21 | run says "no issue filed". |
| 22 | - **A release swap.** A 502, 503 or 504 (or a reset connection) on one read is read once more 3 s later and only the | |
| 23 | second answer counts (#15604): a 200 then passes, the same answer twice fails `answers 502 twice` and the defect names | |
| 24 | both reads. A 404 or 500 is never read again. | |
| 22 | 25 | - **Cannot reach.** Probes whose read got no HTTP answer at all fold into one line, `cannot reach <origin>: <k> of <n> |
| 23 | 26 | probes got no answer`; when that is the only failure the title is `Defect of the production proof: cannot reach |
| 24 | 27 | <origin>`. An unreachable forge files one line, not one per probe. |
src/core/v1_proof.ts +52 −8
| @@ -24,6 +24,10 @@ export interface Seen { | ||
| 24 | 24 | readonly status: number; |
| 25 | 25 | readonly bytes: number; |
| 26 | 26 | readonly ms: number; |
| 27 | /** | |
| 28 | * When this read was the retry of a swap-window answer (#15604): the first read's status and ms. | |
| 29 | */ | |
| 30 | readonly first?: { readonly status: number; readonly ms: number }; | |
| 27 | 31 | } |
| 28 | 32 | interface Got { |
| 29 | 33 | readonly status: number; |
| @@ -283,33 +287,73 @@ export function ageWords(seconds: number): string { | ||
| 283 | 287 | return `${Math.floor(h / 24)} d ${h % 24} h`; |
| 284 | 288 | } |
| 285 | 289 | |
| 286 | /** Runs every probe against `base` with GET only; a probe that throws is a fail. */ | |
| 287 | export async function prove(base: string, fetcher: Fetch): Promise<Result[]> { | |
| 290 | /** A release swap answers these for a moment (#15604): one GET that gets one is read once more. */ | |
| 291 | const SWAP_STATUS: ReadonlySet<number> = new Set([502, 503, 504]); | |
| 292 | /** How long a probe waits before its second read: a swap takes about a second. */ | |
| 293 | export const RETRY_PAUSE_MS = 3_000; | |
| 294 | export type Pause = (ms: number) => Promise<void>; | |
| 295 | const sleep: Pause = (ms) => new Promise((done) => setTimeout(done, ms)); | |
| 296 | ||
| 297 | /** A connection the far end reset mid-read, as Bun and Node name it: worth one more read. */ | |
| 298 | function wasReset(e: unknown): boolean { | |
| 299 | const code = typeof e === "object" && e !== null && "code" in e ? String((e as { code: unknown }).code) : ""; | |
| 300 | return code === "ECONNRESET" || code === "ConnectionClosed"; | |
| 301 | } | |
| 302 | ||
| 303 | /** Runs every probe against `base` with GET only; a probe that throws is a fail. A 502, 503 or | |
| 304 | * 504 (or a reset connection) on one GET is read once more after `pause(RETRY_PAUSE_MS)`, and | |
| 305 | * only the second answer counts (#15604); a 404 or 500 is the answer. */ | |
| 306 | export async function prove(base: string, fetcher: Fetch, pause: Pause = sleep): Promise<Result[]> { | |
| 288 | 307 | const root = base.replace(/\/+$/, ""); |
| 289 | // Side by side: each probe is one GET at a time, so the proof takes the slowest, not the sum. | |
| 308 | // Side by side: each probe is one GET at a time, so the proof takes the slowest, not the sum; | |
| 309 | // the retry sits inside the probe's own GET, so a pause adds to that probe alone. | |
| 290 | 310 | return Promise.all( |
| 291 | 311 | probes.map(async (p): Promise<Result> => { |
| 292 | 312 | let last: Seen | undefined; |
| 293 | const get: Get = async (path) => { | |
| 294 | const url = root + path; | |
| 313 | const read = async (url: string, first?: Seen["first"]): Promise<Got> => { | |
| 295 | 314 | const asked = performance.now(); |
| 296 | last = { url, status: 0, bytes: 0, ms: 0 }; | |
| 315 | last = { url, status: 0, bytes: 0, ms: 0, ...(first === undefined ? {} : { first }) }; | |
| 297 | 316 | const res = await fetcher(url, { method: "GET", redirect: "manual", signal: AbortSignal.timeout(15_000) }); |
| 298 | 317 | const text = await res.text(); |
| 299 | last = { url, status: res.status, bytes: Buffer.byteLength(text), ms: Math.round((performance.now() - asked) * 10) / 10 }; | |
| 318 | last = { url, status: res.status, bytes: Buffer.byteLength(text), ms: Math.round((performance.now() - asked) * 10) / 10, ...(first === undefined ? {} : { first }) }; | |
| 300 | 319 | return { status: res.status, headers: res.headers, text, location: res.headers.get("location") }; |
| 301 | 320 | }; |
| 321 | const get: Get = async (path) => { | |
| 322 | const url = root + path; | |
| 323 | let got: Got; | |
| 324 | try { | |
| 325 | got = await read(url); | |
| 326 | } catch (e) { | |
| 327 | if (!wasReset(e)) throw e; | |
| 328 | await pause(RETRY_PAUSE_MS); | |
| 329 | return read(url, { status: 0, ms: last?.ms ?? 0 }); | |
| 330 | } | |
| 331 | if (!SWAP_STATUS.has(got.status)) return got; | |
| 332 | await pause(RETRY_PAUSE_MS); | |
| 333 | return read(url, { status: got.status, ms: last?.ms ?? 0 }); | |
| 334 | }; | |
| 302 | 335 | try { |
| 303 | 336 | const r = await p.run(get); |
| 304 | 337 | return r instanceof Unproven ? { issue: p.issue, item: p.item, status: "unproven", detail: r.why } : { issue: p.issue, item: p.item, status: "pass", detail: r }; |
| 305 | 338 | } catch (e) { |
| 306 | const detail = e instanceof Fail ? e.message : `probe error: ${String(e)}`; | |
| 339 | let detail = e instanceof Fail ? e.message : `probe error: ${String(e)}`; | |
| 340 | // The same swap answer on both reads is not a moment: the line says it was seen twice. | |
| 341 | if (last?.first !== undefined && last.first.status === last.status && last.status !== 0) detail = detail.replace(`answers ${last.status}`, `answers ${last.status} twice`); | |
| 307 | 342 | return { issue: p.issue, item: p.item, status: "fail", detail, ...(last === undefined ? {} : { seen: last }) }; |
| 308 | 343 | } |
| 309 | 344 | }), |
| 310 | 345 | ); |
| 311 | 346 | } |
| 312 | 347 | |
| 348 | /** | |
| 349 | * The two reads behind a retried line, in words: `first read 502 in 12 ms, read again after 3 s`. | |
| 350 | */ | |
| 351 | export function readsWords(seen: Seen | undefined): string | null { | |
| 352 | if (seen?.first === undefined) return null; | |
| 353 | const was = seen.first.status === 0 ? "a reset connection" : String(seen.first.status); | |
| 354 | return `first read ${was} in ${seen.first.ms} ms, read again after ${RETRY_PAUSE_MS / 1000} s: ${seen.status === 0 ? "no answer" : seen.status} in ${seen.ms} ms`; | |
| 355 | } | |
| 356 | ||
| 313 | 357 | export const line = (r: Result): string => `${r.status} #${r.issue} ${r.item}: ${r.detail}`; |
| 314 | 358 | |
| 315 | 359 | /** The proof a close of v1.0.0 runs: the probes against one base URL. */ |
src/maintain/proven.ts +2 −2
| @@ -10,7 +10,7 @@ | ||
| 10 | 10 | |
| 11 | 11 | import type { Database } from "bun:sqlite"; |
| 12 | 12 | import { addComment, createIssue, listComments } from "../core/issues.ts"; |
| 13 | import { line, prove, type Proof, type Result } from "../core/v1_proof.ts"; | |
| 13 | import { line, prove, readsWords, type Proof, type Result } from "../core/v1_proof.ts"; | |
| 14 | 14 | import { watcherId } from "../rules/bot.ts"; |
| 15 | 15 | import { evidenceBlock } from "./evidence.ts"; |
| 16 | 16 | |
| @@ -64,7 +64,7 @@ function failingLines(results: readonly Result[], at: Date): { title: string; te | ||
| 64 | 64 | const lost = fails.filter(unreached); |
| 65 | 65 | const parts: string[] = []; |
| 66 | 66 | const quote = (text: string, r: Result): string => |
| 67 | `> ${text.replace(/\r?\n/g, " ")}\n\n${evidenceBlock({ ...(r.seen === undefined ? {} : { where: r.seen.url, status: r.seen.status, bytes: r.seen.bytes, ms: r.seen.ms }), at: at.toISOString() })}`; | |
| 67 | `> ${text.replace(/\r?\n/g, " ")}\n\n${readsWords(r.seen) === null ? "" : `Reads: ${readsWords(r.seen)}.\n\n`}${evidenceBlock({ ...(r.seen === undefined ? {} : { where: r.seen.url, status: r.seen.status, bytes: r.seen.bytes, ms: r.seen.ms }), at: at.toISOString() })}`; | |
| 68 | 68 | const first = lost[0]; |
| 69 | 69 | if (first !== undefined) parts.push(quote(`cannot reach ${originOf(first)}: ${lost.length} of ${results.length} probes got no answer (${first.detail})`, first)); |
| 70 | 70 | for (const r of fails) if (!unreached(r)) parts.push(quote(line(r), r)); |
test/maintain-v1-proven.test.ts +14
| @@ -162,6 +162,20 @@ test("a filed defect and its still-failing comment carry the evidence block of w | ||
| 162 | 162 | expect(readEvidence(comment.body)).toMatchObject({ url: seen.url, status: "500", bytes: "12" }); |
| 163 | 163 | }); |
| 164 | 164 | |
| 165 | test("a line read twice in a release swap names both reads above its evidence block (#15604)", async () => { | |
| 166 | app.db.query("UPDATE issue SET state = 'closed' WHERE repo_id = ?1 AND (title LIKE 'Defect of %' OR instr(body, 'Probe key: v1-proven/') > 0)").run(repoId); | |
| 167 | const before = defects().length; | |
| 168 | const bad = probes[0]!; | |
| 169 | const seen = { url: "https://forge.example/", status: 502, bytes: 4, ms: 8, first: { status: 502, ms: 12.5 } }; | |
| 170 | const proof = async (): Promise<Result[]> => | |
| 171 | probes.map((p) => (p.issue === bad.issue ? { issue: p.issue, item: p.item, status: "fail", detail: "/ answers 502 twice, not 200", seen } : { issue: p.issue, item: p.item, status: "pass", detail: "ok" })); | |
| 172 | await run(undefined, undefined, proof); | |
| 173 | const body = app.db.query<{ body: string }, [string]>("SELECT body FROM issue WHERE id = ?1").get(defects().slice(before)[0]!.id)!.body; | |
| 174 | expect(body).toContain("answers 502 twice"); | |
| 175 | expect(body).toContain("Reads: first read 502 in 12.5 ms, read again after 3 s: 502 in 8 ms."); | |
| 176 | expect(readEvidence(body)).toMatchObject({ url: seen.url, status: "502", bytes: "4", ms: "8" }); | |
| 177 | }); | |
| 178 | ||
| 165 | 179 | const closeDefects = () => app.db.query("UPDATE issue SET state = 'closed' WHERE repo_id = ?1 AND (title LIKE 'Defect of %' OR instr(body, 'Probe key: v1-proven/') > 0)").run(repoId); |
| 166 | 180 | const total = () => app.db.query<{ c: number }, [string]>("SELECT count(*) AS c FROM issue WHERE repo_id = ?1").get(repoId)!.c; |
| 167 | 181 |
test/timers-named.test.ts +1
| @@ -106,6 +106,7 @@ const ALLOWED: readonly Allowed[] = [ | ||
| 106 | 106 | ...allow("setTimeout", "retries removing one emptied cgroup directory: one rmdir", [["src/apps/cgroup.ts", "() => removeDir(dir"]]), |
| 107 | 107 | ...allow("setTimeout", SLEEP, [ |
| 108 | 108 | ["src/kernel/breathe.ts", "done"], |
| 109 | ["src/core/v1_proof.ts", "done"], | |
| 109 | 110 | ["src/maintenance/checks.ts", "done"], |
| 110 | 111 | ["src/chaos/faults.ts", "r"], |
| 111 | 112 | ["src/perf/sample.ts", "r"], |
test/v1-proven.test.ts +73 −2
| @@ -1,5 +1,13 @@ | ||
| 1 | 1 | import { describe, expect, test } from "bun:test"; |
| 2 | import { ageWords, line, probes, prove, type Fetch } from "../scripts/v1-proven.ts"; | |
| 2 | import { ageWords, line, probes, prove as proveWaiting, readsWords, RETRY_PAUSE_MS, type Fetch } from "../scripts/v1-proven.ts"; | |
| 3 | ||
| 4 | // The retry's pause (#15604) is injected: each proof here is instant, and the pauses it asked are | |
| 5 | // kept. | |
| 6 | let paused: number[] = []; | |
| 7 | const prove = (base: string, fetcher: Fetch) => | |
| 8 | proveWaiting(base, fetcher, async (ms) => { | |
| 9 | paused.push(ms); | |
| 10 | }); | |
| 3 | 11 | |
| 4 | 12 | const HEADERS = { |
| 5 | 13 | "content-security-policy": "default-src 'none'", |
| @@ -183,6 +191,66 @@ describe("v1-proven", () => { | ||
| 183 | 191 | expect(ok.detail).toBe(`/changelog.atom carries 1 entry (${FEED.length} bytes)`); |
| 184 | 192 | }); |
| 185 | 193 | |
| 194 | // A fetcher answering `path` from a queue of statuses (the last repeats), counting its reads. | |
| 195 | const flaky = (path: string, statuses: number[], reads: string[] = []): Fetch => { | |
| 196 | const rest = stub(); | |
| 197 | let n = 0; | |
| 198 | return async (url, init) => { | |
| 199 | if (new URL(url).pathname !== path) return rest(url, init); | |
| 200 | reads.push(path); | |
| 201 | const status = statuses[Math.min(n++, statuses.length - 1)]!; | |
| 202 | return status === 200 ? new Response("Running dev at 058b6cf\nPress Start my forge.") : new Response("swap", { status }); | |
| 203 | }; | |
| 204 | }; | |
| 205 | ||
| 206 | test("a 502 during a release swap is read once more after the pause; a 200 then passes (#15604)", async () => { | |
| 207 | paused = []; | |
| 208 | const reads: string[] = []; | |
| 209 | const results = await prove("https://f.test", flaky("/terms", [502, 200], reads)); | |
| 210 | expect(results.filter((x) => x.status !== "pass")).toEqual([]); | |
| 211 | expect(reads.length).toBe(2); | |
| 212 | expect(paused).toEqual([RETRY_PAUSE_MS]); | |
| 213 | expect(RETRY_PAUSE_MS).toBe(3000); | |
| 214 | }); | |
| 215 | ||
| 216 | for (const status of [502, 503, 504]) | |
| 217 | test(`two ${status}s in a row are a fail that says it answered ${status} twice, naming both reads (#15604)`, async () => { | |
| 218 | const r = (await prove("https://f.test", flaky("/terms", [status, status]))).find((x) => x.issue === 13116)!; | |
| 219 | expect(r.status).toBe("fail"); | |
| 220 | expect(r.detail).toBe(`/terms answers ${status} twice, not 200 (4 bytes)`); | |
| 221 | expect(r.seen).toMatchObject({ url: "https://f.test/terms", status, bytes: 4, first: { status } }); | |
| 222 | expect(readsWords(r.seen)).toMatch(new RegExp(`^first read ${status} in [\\d.]+ ms, read again after 3 s: ${status} in [\\d.]+ ms$`)); | |
| 223 | }); | |
| 224 | ||
| 225 | test("a 404 or a 500 is the answer, never retried (#15604)", async () => { | |
| 226 | for (const status of [404, 500]) { | |
| 227 | paused = []; | |
| 228 | const reads: string[] = []; | |
| 229 | const r = (await prove("https://f.test", flaky("/terms", [status, 200], reads))).find((x) => x.issue === 13116)!; | |
| 230 | expect(r.status).toBe("fail"); | |
| 231 | expect(r.detail).toBe(`/terms answers ${status}, not 200 (4 bytes)`); | |
| 232 | expect(r.seen?.first).toBeUndefined(); | |
| 233 | expect(readsWords(r.seen)).toBeNull(); | |
| 234 | expect(reads.length).toBe(1); | |
| 235 | expect(paused).toEqual([]); | |
| 236 | } | |
| 237 | }); | |
| 238 | ||
| 239 | test("a reset connection is read once more; any other network error is not (#15604)", async () => { | |
| 240 | const rest = stub(); | |
| 241 | let n = 0; | |
| 242 | const reset: Fetch = async (url, init) => { | |
| 243 | if (new URL(url).pathname === "/terms" && n++ === 0) throw Object.assign(new Error("socket closed"), { code: "ECONNRESET" }); | |
| 244 | return rest(url, init); | |
| 245 | }; | |
| 246 | expect((await prove("https://f.test", reset)).find((x) => x.issue === 13116)!.status).toBe("pass"); | |
| 247 | paused = []; | |
| 248 | await prove("https://f.test", async () => { | |
| 249 | throw new Error("refused"); | |
| 250 | }); | |
| 251 | expect(paused).toEqual([]); | |
| 252 | }); | |
| 253 | ||
| 186 | 254 | test("an age reads in the unit a person would use", () => { |
| 187 | 255 | expect([ageWords(45), ageWords(600), ageWords(3 * 3600 + 720), ageWords(3 * 86_400 + 7200)]).toEqual(["45 s", "10 min", "3 h 12 min", "3 d 2 h"]); |
| 188 | 256 | }); |
| @@ -199,10 +267,13 @@ describe("v1-proven", () => { | ||
| 199 | 267 | }; |
| 200 | 268 | |
| 201 | 269 | test("the script exits 1 against a failing server and prints the summary", async () => { |
| 202 | const { code, out } = await run(async () => new Response("down", { status: 503 })); | |
| 270 | // A 500 is never read again, so the script answers at once; the 503 retry is proven above with | |
| 271 | // an instant pause. | |
| 272 | const { code, out } = await run(async () => new Response("down", { status: 500 })); | |
| 203 | 273 | expect(code).toBe(1); |
| 204 | 274 | expect(out).toContain("fail #11539"); |
| 205 | 275 | expect(out).toContain("v1.0.0 is not proven: 15 of 15 fail, 0 unproven"); |
| 276 | expect(out).not.toContain("twice"); | |
| 206 | 277 | }); |
| 207 | 278 | |
| 208 | 279 | test("the script exits 3, not 0, when nothing fails but some items are unproven", async () => { |
new above
offline