Public: anyone can read this; the crew can change it.
The 5,000-row prune test proves the write lock is short without a wall-clock bound that fails under load #15149
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15146-the-5-000-row-prune-test-proves-the-writ → main 1 commit fork
1 file
changed +62
−6
test/retention-events.test.ts +62 −6
| @@ -6,7 +6,7 @@ import { countNotes } from "../src/maintain/keeper/record.ts"; | ||
| 6 | 6 | import { openDb } from "../src/kernel/db.ts"; |
| 7 | 7 | import { LIMITS } from "../src/settings/limits.ts"; |
| 8 | 8 | import { expectWork } from "./helpers/work.ts"; |
| 9 | import { BATCH_ROWS, KEEP_MS, keptKind, LIVE_KEEP_MS, LIVE_SLICE_ROWS, pruneEvents, pruneLiveEvents, REST_MS } from "../src/system/retention_events.ts"; | |
| 9 | import { BATCH_ROWS, CHUNK_ROWS, KEEP_MS, keptKind, LIVE_KEEP_MS, LIVE_SLICE_ROWS, pruneEvents, pruneLiveEvents, REST_MS } from "../src/system/retention_events.ts"; | |
| 10 | 10 | |
| 11 | 11 | const DAY = 86_400_000; |
| 12 | 12 | const NOW = Date.parse("2026-11-13T12:00:00.000Z"); |
| @@ -117,7 +117,7 @@ describe("pruneEvents", () => { | ||
| 117 | 117 | for (const id of inside) expect(db.query("SELECT 1 FROM event WHERE id = ?1").get(id)).not.toBeNull(); |
| 118 | 118 | }); |
| 119 | 119 | |
| 120 | test("one 5,000-row batch holds the write lock under 50 ms", () => { | |
| 120 | test("one 5,000-row batch takes the write lock a chunk at a time: one delete of CHUNK_ROWS per transaction, released between", () => { | |
| 121 | 121 | const db = seeded(); |
| 122 | 122 | const add = db.query("INSERT INTO event (id, kind, repo_id, payload, created_at) VALUES (?1, 'secret.read', 'r1', ?2, ?3)"); |
| 123 | 123 | db.transaction(() => { |
| @@ -126,10 +126,66 @@ describe("pruneEvents", () => { | ||
| 126 | 126 | })(); |
| 127 | 127 | db.exec("PRAGMA foreign_keys = ON"); |
| 128 | 128 | pruneEvents(db, NOW, 100); // warm the statements |
| 129 | const { deleted, lockMs } = pruneEvents(db, NOW, BATCH_ROWS, KEEP_MS, Infinity); | |
| 130 | expect(deleted).toBe(BATCH_ROWS); | |
| 131 | expect(lockMs).toBeGreaterThan(0); | |
| 132 | expect(lockMs).toBeLessThan(50); | |
| 129 | ||
| 130 | // Every transaction the batch opens, measured by what it did rather than how long it took: | |
| 131 | // the event rows it removed, the DELETEs it ran, and whether the lock was let go after it. | |
| 132 | type Txn = { rows: number; deletes: number; releasedAfter: boolean }; | |
| 133 | const txns: Txn[] = []; | |
| 134 | const door = db as unknown as { transaction: Database["transaction"]; query: Database["query"] }; | |
| 135 | const openTxn = db.transaction.bind(db); | |
| 136 | const query = db.query.bind(db); | |
| 137 | const events = () => (query("SELECT COUNT(*) AS n FROM event").get() as { n: number }).n; | |
| 138 | let current: Txn | null = null; | |
| 139 | door.query = ((sql: string) => { | |
| 140 | if (current && /^\s*DELETE\b/i.test(sql)) current.deletes++; | |
| 141 | return query(sql); | |
| 142 | }) as Database["query"]; | |
| 143 | door.transaction = ((fn: (...a: unknown[]) => unknown) => { | |
| 144 | const run = openTxn(fn); | |
| 145 | return (...a: unknown[]) => { | |
| 146 | expect(db.inTransaction).toBe(false); // never nested inside another chunk's lock | |
| 147 | const before = events(); | |
| 148 | const txn: Txn = { rows: 0, deletes: 0, releasedAfter: false }; | |
| 149 | current = txn; | |
| 150 | try { | |
| 151 | return run(...a); | |
| 152 | } finally { | |
| 153 | current = null; | |
| 154 | txn.rows = before - events(); | |
| 155 | txn.releasedAfter = !db.inTransaction; | |
| 156 | txns.push(txn); | |
| 157 | } | |
| 158 | }; | |
| 159 | }) as unknown as Database["transaction"]; | |
| 160 | let batch: ReturnType<typeof pruneEvents>; | |
| 161 | try { | |
| 162 | batch = pruneEvents(db, NOW, BATCH_ROWS, KEEP_MS, Infinity); | |
| 163 | } finally { | |
| 164 | door.transaction = openTxn as Database["transaction"]; | |
| 165 | door.query = query as Database["query"]; | |
| 166 | } | |
| 167 | ||
| 168 | expect(batch.deleted).toBe(BATCH_ROWS); | |
| 169 | expect(txns.reduce((n, t) => n + t.rows, 0)).toBe(BATCH_ROWS); | |
| 170 | // The lock is held for a chunk, never for the batch: a chunk is CHUNK_ROWS rows in one | |
| 171 | // statement. | |
| 172 | expect(txns.length).toBe(Math.ceil(BATCH_ROWS / CHUNK_ROWS)); | |
| 173 | expect(Math.max(...txns.map((t) => t.rows))).toBeLessThanOrEqual(CHUNK_ROWS); | |
| 174 | for (const t of txns) { | |
| 175 | expect(t.deletes).toBe(1); | |
| 176 | expect(t.releasedAfter).toBe(true); | |
| 177 | } | |
| 178 | // A batch never walks past its cap, whatever it is asked for. | |
| 179 | expect(pruneEvents(db, NOW, BATCH_ROWS * 4, KEEP_MS, Infinity).deleted).toBe(BATCH_ROWS); | |
| 180 | ||
| 181 | // The guard above is the counts. One coarse clock check stays, only to catch a chunk turning | |
| 182 | // pathological: the best of three batches (noise only ever adds time, so the fewest drops it), | |
| 183 | // against a ceiling far above the ~1 ms a chunk takes warm, because one run in a full-suite | |
| 184 | // shard of four files at once took 63 ms against the old 50 ms bound (#15145). | |
| 185 | let best = Infinity; | |
| 186 | for (let i = 0; i < 3; i++) best = Math.min(best, pruneEvents(db, NOW, 1_000, KEEP_MS, Infinity).lockMs); | |
| 187 | expect(best).toBeGreaterThan(0); | |
| 188 | expect(best).toBeLessThan(250); | |
| 133 | 189 | }); |
| 134 | 190 | }); |
| 135 | 191 |
new above
offline