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

waiting() and mainGap count pulls on their way from the newest 300 merged rows only #15331

Merged opened this

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

Merged

scout/forge:scout/15330-waiting-and-maingap-count-pulls-on-their → main 0 commits fork 2 threads · 2 unresolved

  • Merged. scout/15330-waiting-and-maingap-count-pulls-on-their was merged into main as cfe12e4.

Review

Sign in to review this change.
3 files changed +155 −3
src/release/landings.ts +40 −3
@@ -118,6 +118,42 @@ export function mergedRows(db: Database, limit = ROWS, before?: string): MergedR
118118 .map((r) => ({ ...r, oid: r.oid ?? "" }));
119119 }
120120
121 /** Batches read at most when looking for the oldest pull on its way. */
122 const REACH = 20;
123 /**
124 * The most merged rows `waiting` and its kin look through: more pulls on their way than this are
125 * counted as this many.
126 */
127 export const REACH_ROWS = REACH * ROWS;
128
129 /** What the last deep read found, for one kept state and one newest merged pull. */
130 const deep = new WeakMap<Database, { readonly state: LiveState; readonly newest: string; readonly rows: MergedRow[] }>();
131
outdated

Loops up to 19 more batches each call when no own row is live, and every caller re-runs it. Memoise per db and kept state (see review body, point 1).

132 /**
133 * The merged pulls newest first, read as far as the newest live pull of the forge's own
134 * repository (every pull older than it counts as live, see `split`), bounded by `REACH_ROWS`.
135 * Synchronous, so the alarm stays so. Kept per forge for as long as the kept state and the newest
136 * merged pull stand, so the worst case (no own pull live) is paid once, not per call.
137 */
138 function rowsToLive(db: Database, state: LiveState): readonly MergedRow[] {
139 const top = mergedRows(db, 1)[0];
140 const newest = top === undefined ? "" : `${top.repo}#${top.number}@${top.at}`;
141 const held = deep.get(db);
142 if (held !== undefined && held.state === state && held.newest === newest) return held.rows;
143 const rows = mergedRows(db);
144 const own = state.own.toLowerCase();
145 for (let n = 1; n < REACH; n++) {
146 if (rows.length < n * ROWS || rows.some((r) => r.repo.toLowerCase() === own && isLive(r, state))) break;
147 const last = rows[rows.length - 1];
148 if (last === undefined) break;
149 const more = mergedRows(db, ROWS, `${last.repo}#${last.number}`);
150 if (more.length === 0) break;
151 rows.push(...more);
152 }
153 deep.set(db, { state, newest, rows });
154 return rows;
155 }
156
121157 /** Whether a public repository (`owner/name`) has a merged pull. */
122158 export function hasMerged(db: Database, named: string): boolean {
123159 const [owner = "", name = ""] = named.split("/");
@@ -182,6 +218,7 @@ export function keepLive(db: Database, own: string, oids: Iterable<string>, at:
182218 /** Forget it, so the alarm is silent until the next read. */
183219 export function forgetLive(db: Database): void {
184220 keeps.delete(db);
221 deep.delete(db);
185222 founds.delete(db);
186223 }
187224
@@ -218,7 +255,7 @@ export function split(rows: readonly MergedRow[], state: LiveState | undefined):
218255 export function waiting(db: Database): { readonly since: string; readonly count: number } | undefined {
219256 const kept = keeps.get(db);
220257 if (kept === undefined) return undefined;
221 const { onItsWay } = split(mergedRows(db), kept);
258 const { onItsWay } = split(rowsToLive(db, kept), kept);
222259 const oldest = onItsWay.at(-1);
223260 return oldest === undefined ? undefined : { since: oldest.at, count: onItsWay.length };
224261 }
@@ -230,7 +267,7 @@ export function waiting(db: Database): { readonly since: string; readonly count:
230267 export function waitingRows(db: Database): MergedRow[] {
231268 const kept = keeps.get(db);
232269 if (kept === undefined) return [];
233 const rows = mergedRows(db);
270 const rows = rowsToLive(db, kept);
234271 const way = new Set(split(rows, kept).onItsWay.map((l) => `${l.repo}#${l.number}`));
235272 return rows.filter((r) => way.has(`${r.repo}#${r.number}`)).reverse();
236273 }
@@ -363,7 +400,7 @@ export function mainGap(db: Database): MainGap | undefined {
363400 if (k === undefined || k.head === undefined) return undefined;
364401 if (k.head === k.commit) return { main: { sha: k.head }, behind: { commits: 0, pulls: 0 } };
365402 const own = k.own.toLowerCase();
366 const pulls = split(mergedRows(db), k).onItsWay.filter((l) => l.repo.toLowerCase() === own).length;
403 const pulls = split(rowsToLive(db, k), k).onItsWay.filter((l) => l.repo.toLowerCase() === own).length;
367404 // A count not yet asked for this pair is at least the pulls on their way.
368405 return { main: { sha: k.head }, behind: { commits: k.ahead ?? pulls, pulls } };
369406 }
test/waiting-beyond-300.test.ts added +55
@@ -0,0 +1,55 @@
1 /**
2 * Production trailing main by more than 300 merged pulls: the count of pulls on their way does not
3 * stop at 300 (#15330).
4 */
5 import { afterAll, beforeAll, expect, test } from "bun:test";
6 import { mkdtempSync, rmSync } from "node:fs";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { createApp, type App } from "../src/app.ts";
10 import { createRepo } from "../src/core/repos.ts";
11 import { createUser } from "../src/core/users.ts";
12 import { forgetLive, keepLive, mainGap, waiting, waitingKeys, waitingRows } from "../src/release/landings.ts";
13
14 let root: string;
15 let app: App;
16 const TOTAL = 650;
17
18 beforeAll(async () => {
19 root = mkdtempSync(join(tmpdir(), "forge-waiting-deep-"));
20 app = createApp({ dataDir: root, port: 0 });
21 const u = await createUser(app.db, { username: "trevato", email: "[email protected]", password: "a-good-passphrase", origin: "seed", trusted: true, adminIfFirst: true });
22 if (!u.ok) throw new Error(u.error);
23 const repo = await createRepo(app.db, app.reposRoot, { ownerId: u.value.id, name: "forge", visibility: "public", description: "f" });
24 if (!repo.ok) throw new Error(repo.error);
25 const db = app.db;
26 const base = repo.value.defaultBranch;
27 db.transaction(() => {
28 for (let i = 1; i <= TOTAL; i++) {
29 const id = `deep${i}`;
30 const at = new Date(Date.UTC(2026, 0, 1) + i * 60_000).toISOString();
31 db.query("INSERT INTO issue (id, repo_id, number, kind, title, author_id) VALUES (?, ?, ?, 'pull', ?, ?)").run(id, repo.value.id, i, `Pull ${i}`, u.value.id);
32 db.query("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at, merged_oid) VALUES (?, ?, ?, 'a', 'b', ?, ?)").run(id, base, `h${i}`, at, `oid${i}`);
33 }
34 })();
35 // The oldest 50 are in the running commit; the 600 after it are not.
36 keepLive(db, "trevato/forge", Array.from({ length: 50 }, (_, i) => `oid${i + 1}`), 0, { commit: "c", head: "h", ahead: 600 });
37 });
38 afterAll(() => {

Add the case with no live own row and more than 300 rows, so the REACH bound is pinned; also assert behind.commits here.

39 forgetLive(app.db);
40 app.close();
41 rmSync(root, { recursive: true, force: true });
42 });
43
44 test("waiting counts every pull on its way, past 300 merged rows", () => {
45 const w = waiting(app.db);
46 expect(w?.count).toBe(TOTAL - 50);
47 expect(w?.since).toBe(new Date(Date.UTC(2026, 0, 1) + 51 * 60_000).toISOString());
48 });
49
50 test("waitingRows, waitingKeys and mainGap agree", () => {
51 expect(waitingRows(app.db).length).toBe(TOTAL - 50);
52 expect(waitingRows(app.db)[0]?.number).toBe(51);
53 expect(waitingKeys(app.db)?.size).toBe(TOTAL - 50);
54 expect(mainGap(app.db)?.behind).toEqual({ commits: 600, pulls: TOTAL - 50 });
55 });
test/waiting-no-live-row.test.ts added +60
@@ -0,0 +1,60 @@
1 /**
2 * No own pull live and more merged pulls than the reach: the count is the bound, and repeat calls
3 * are cheap (#15330).
4 */
5 import { afterAll, beforeAll, expect, test } from "bun:test";
6 import { mkdtempSync, rmSync } from "node:fs";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { createApp, type App } from "../src/app.ts";
10 import { createRepo } from "../src/core/repos.ts";
11 import { createUser } from "../src/core/users.ts";
12 import { expectWork } from "./helpers/work.ts";
13 import { once } from "./helpers/statements.ts";
14 import { REACH_ROWS, forgetLive, keepLive, waiting, waitingKeys } from "../src/release/landings.ts";
15
16 let root: string;
17 let app: App;
18 const TOTAL = REACH_ROWS + 100;
19
20 beforeAll(async () => {
21 root = mkdtempSync(join(tmpdir(), "forge-waiting-none-"));
22 app = createApp({ dataDir: root, port: 0 });
23 const u = await createUser(app.db, { username: "trevato", email: "[email protected]", password: "a-good-passphrase", origin: "seed", trusted: true, adminIfFirst: true });
24 if (!u.ok) throw new Error(u.error);
25 const repo = await createRepo(app.db, app.reposRoot, { ownerId: u.value.id, name: "forge", visibility: "public", description: "f" });
26 if (!repo.ok) throw new Error(repo.error);
27 const db = app.db;
28 db.transaction(() => {
29 for (let i = 1; i <= TOTAL; i++) {
30 const at = new Date(Date.UTC(2026, 0, 1) + i * 1000).toISOString();
31 db.query("INSERT INTO issue (id, repo_id, number, kind, title, author_id) VALUES (?, ?, ?, 'pull', 'p', ?)").run(`n${i}`, repo.value.id, i, u.value.id);
32 db.query("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at, merged_oid) VALUES (?, ?, 'h', 'a', 'b', ?, ?)").run(`n${i}`, repo.value.defaultBranch, at, `oid${i}`);
33 }
34 })();
35 keepLive(db, "trevato/forge", [], 0);
36 });
37 afterAll(() => {
38 forgetLive(app.db);
39 app.close();
40 rmSync(root, { recursive: true, force: true });
41 });
42
43 test("with none live the count stops at the documented reach, and says so by being exactly it", () => {
44 expect(waiting(app.db)?.count).toBe(REACH_ROWS);
45 expect(waitingKeys(app.db)?.size).toBe(REACH_ROWS);
46 });
47
48 test("the deep read is paid once: a repeat call costs one statement", async () => {
49 waiting(app.db);
50 const cost = await once(app.db, () => {
51 for (let i = 0; i < 50; i++) waiting(app.db);
52 });
53 expectWork(cost, { max: 50 });
54 });
55
56 test("a new merge or a new kept state is read afresh", () => {
57 keepLive(app.db, "trevato/forge", ["oid" + TOTAL], 0);
58 // Newest own pull is live now, so nothing older waits.
59 expect(waiting(app.db)).toBeUndefined();
60 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs