Public: anyone can read this; the crew can change it.

A 5,000-row deploy_pull prune batch holds the write lock under 50 ms, measured in a test #16756

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/forge:torque/12041-a-5-000-row-deploy-pull-prune-batch-hold → main 0 commits fork 2 threads · 2 unresolved

  • Merged. torque/12041-a-5-000-row-deploy-pull-prune-batch-hold was merged into main as b9a4941.

Review

Sign in to review this change.
1 file changed +101 −0
test/retention-deploys-lock.test.ts added +101
@@ -0,0 +1,101 @@
1 /**
2 * #12041: a batch of 5,000 prunable deploy_pull rows holds the write lock under 50 ms.
3 * `pruneDeploys` is synchronous on the forge's one thread and takes the lock in its own BEGIN
4 * IMMEDIATE..COMMIT, so the whole call (slice read and run look-ups included) is an upper bound on
5 * the time any other writer waits. Asserted for one 5,000-row batch with no time budget, and for
6 * the cap the forge runs with.
7 */
8 import { afterAll, beforeAll, expect, test } from "bun:test";
9 import { mkdtemp, rm } from "node:fs/promises";
10 import { tmpdir } from "node:os";
11 import { join } from "node:path";
12 import { createApp, type App } from "../src/app.ts";
13 import { quietLogs } from "./helpers/globals.ts";
14 import { DEPLOY_PULL_SCAN, pruneDeploys } from "../src/system/retention_deploys.ts";
15
16 quietLogs();
17
18 const LOCK_BUDGET_MS = 50;
19 const ROWS = 5_000;
20 const DAY = 86_400_000;
21
22 let dataDir: string;
23 let app: App;
24 const now = new Date();
25
26 /**
27 * 5,000 rows over 50 old deploy runs of one repo (100 rows each), none of them the one an app
28 * serves.
29 */
30 const seed = (): void => {
31 app.db.query("DELETE FROM deploy_pull").run();
32 app.db.query("DELETE FROM run").run();
33 const old = new Date(now.getTime() - 10 * DAY).toISOString();
34 const run = app.db.query(
35 `INSERT INTO run (id, repo_id, kind, env, commit_oid, state, serving_at, finished_at, created_at)
36 VALUES (?1, 'repo-x', 'deploy', 'production', 'abc', 'ok', ?2, ?2, ?2)`,
37 );
38 const row = app.db.query(
39 "INSERT INTO deploy_pull (run_id, issue_id, pull_number, merged_oid) VALUES (?1, ?2, ?3, 'o')",
40 );
41 app.db.transaction(() => {
42 for (let r = 0; r < 50; r++) {
43 run.run(`run-${r}`, old);
44 for (let i = 0; i < ROWS / 50; i++) row.run(`run-${r}`, `issue-${r}-${i}`, i);
45 }
46 })();
47 };
48
49 const left = (): number => app.db.query<{ n: number }, []>("SELECT COUNT(*) AS n FROM deploy_pull").get()!.n;
50
51 beforeAll(async () => {
52 dataDir = await mkdtemp(join(tmpdir(), "forge-retention-lock-"));
53 app = createApp({ dataDir, port: 0 });
54 app.db.exec("PRAGMA foreign_keys = OFF");
55 });
56
57 afterAll(async () => {
58 app.close?.();
59 await rm(dataDir, { recursive: true, force: true });
60 });
61
62 const RUNS = 5;
63 const median = (xs: number[]): number => [...xs].sort((x, y) => x - y)[Math.floor(xs.length / 2)]!;
64
65 // A regression slows every run; a GC pause or scheduler stall on a shared machine slows one. So the
outdated

Single sample against 50 ms with ~3x headroom will fail on a loaded runner. Reseed and repeat, then assert on the minimum.

66 // batch is reseeded and run RUNS times, correctness is asserted on every run, and the fastest is
67 // timed.
68 test("one batch of 5,000 prunable rows holds the write lock under 50 ms", () => {
69 const times: number[] = [];
70 for (let run = 0; run < RUNS; run++) {
71 seed();
72 expect(left()).toBe(ROWS);
73 const started = performance.now();
74 const result = pruneDeploys(app.db, now, ROWS, 0, 1, Number.POSITIVE_INFINITY);
75 times.push(performance.now() - started);
76 expect(result.deleted).toBe(ROWS);
77 expect(left()).toBe(0);
78 }
79 expect(Math.min(...times)).toBeLessThan(LOCK_BUDGET_MS);
80 });
81
82 test("the cap the forge runs with is far inside that budget, and a pass of slices drains the rows", () => {
83 expect(DEPLOY_PULL_SCAN).toBeLessThanOrEqual(ROWS);
84 const typical: number[] = [];
85 for (let run = 0; run < RUNS; run++) {
outdated

slowest is the maximum over ~20 slices, so it is the sample most exposed to a stall. Use the median, or the minimum over repeated passes.

86 seed();
87 const slices: number[] = [];
88 let after = 0;
89 for (;;) {
90 const started = performance.now();
91 const r = pruneDeploys(app.db, now, DEPLOY_PULL_SCAN, after);
92 slices.push(performance.now() - started);
93 after = r.next;
94 if (!r.more) break;
95 }
96 expect(left()).toBe(0);
97 typical.push(median(slices));
98 }
99 // The median slice of the calmest pass, so one stalled slice or one stalled pass cannot fail it.
100 expect(Math.min(...typical)).toBeLessThan(LOCK_BUDGET_MS);
101 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs