Files
paula-alves d2616ce539
ci / typescript (push) Skipped
ci / rust (push) Skipped
ci / rust (pull_request) Successful in 6m9s
ci / typescript (pull_request) Successful in 11m47s
fix(worker): account for worker deploy overlap in DB connection budget (BIT-33530)
dbConnectionBudget reserved room for an API deploy overlap (apiReloadOverlap,
BIT-31550) but not for a worker deploy overlap. PM2 startOrReload on the
fork-mode photoreport-worker starts the new process before the old one finishes
its 25 s drain, so both Prisma pools and both broadcasters are open at once.
With defaults that is 2 * (47 + 1) + 26 = 122 > 100 max_connections, tipping
Postgres into error 53300 "sorry, too many clients already".

Add workerReloadOverlap (default 1) that divides the remaining budget by the
number of concurrent workers, so the total stays under max_connections even
during a reload. The worker's Prisma pool drops from 47 to 23 with defaults —
enough for the concurrent jobs the capacity planner sizes, and the headroom
and ad-hoc connection margin are preserved.

Paperclip-Issue: BIT-33530
2026-12-04 09:20:43 +00:00

80 lines
4.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { dbConnectionBudget, planCapacity, Pressure, type Reading } from "../src/lib/capacity.js";
const GB = 1e9;
describe("capacity", () => {
it("does more at once on a bigger container", () => {
const small = planCapacity({}, { cpus: 4, memoryBytes: 8 * GB });
const big = planCapacity({}, { cpus: 32, memoryBytes: 64 * GB });
expect(small).toMatchObject({ faces: 3, ingest: 4, archive: 2, render: 2, autopilot: 2, delivery: 4 });
expect(big).toMatchObject({ cpus: 32, faces: 31, ingest: 8, archive: 9, archiveBurst: 91, render: 16, autopilot: 16, delivery: 8 });
expect(planCapacity({}, { cpus: 48, memoryBytes: 64 * GB })).toMatchObject({ faces: 47, archiveBurst: 91, archiveDownloads: 960 });
// A fixed ARCHIVE_CONCURRENCY never holds a backlog back; downloads run many at once even on a small machine.
expect(planCapacity({ archive: 2 }, { cpus: 4, memoryBytes: 8 * GB })).toMatchObject({ archive: 2, archiveBurst: 7, archiveDownloads: 80 });
});
it("is held back by memory when cores are many and memory is short", () => {
const tight = planCapacity({}, { cpus: 32, memoryBytes: 6 * GB });
expect(tight.faces).toBe(2);
expect(tight.archive).toBe(2);
});
it("keeps what the environment sets", () => {
expect(planCapacity({ faces: 3, archive: 40 }, { cpus: 32, memoryBytes: 64 * GB })).toMatchObject({ faces: 3, archive: 40 });
});
it("lets bulk work step aside while the machine is busy", () => {
const at = (r: Partial<Reading>) => new Pressure(() => ({ cpuStall: null, load: 0, cpus: 32, free: 32 * GB, total: 64 * GB, ...r }));
expect(at({ cpuStall: 5 }).allow(16)).toBe(16);
expect(at({ cpuStall: 45 }).allow(16)).toBe(8);
expect(at({ cpuStall: 80 }).allow(16)).toBe(4);
expect(at({ load: 40 }).allow(16)).toBe(4);
expect(at({ free: 3 * GB }).allow(16)).toBe(4);
expect(at({ cpuStall: 80 }).allow(2)).toBe(1);
// A big backlog: full speed through a saturated CPU, back off only near the limits.
expect(at({ cpuStall: 80 }).allow(36, true)).toBe(36);
expect(at({ cpuStall: 95 }).allow(36, true)).toBe(18);
expect(at({ free: 6 * GB }).allow(36, true)).toBe(18);
});
});
describe("dbConnectionBudget", () => {
it("stays under what the API's cluster, a deploy's reload overlaps, the superuser reserve, the broadcaster and headroom leave of max_connections", () => {
// 100 - 3 superuser reserved - ((2 api + 1 reload) * (10 pool + 3 realtime)) - 10 headroom = 48.
// Divide by (1 + 1 worker overlap) = 24, minus 1 broadcaster = 23.
expect(dbConnectionBudget(500)).toBe(23);
expect(dbConnectionBudget(10)).toBe(10);
});
it("follows a differently sized API cluster or Postgres limit", () => {
// 200 - 3 - ((4 + 1) * (15 + 3)) - 10 = 200 - 3 - 90 - 10 = 97; floor(97 / 2) - 1 = 47.
expect(dbConnectionBudget(500, { max: 200, apiInstances: 4, apiPoolSize: 15 })).toBe(47);
// 40 - 3 - ((2 + 1) * (10 + 3)) - 10 = 40 - 3 - 39 - 10 = -12; floor(-12 / 2) - 1 = -7; clamped to 1.
expect(dbConnectionBudget(500, { max: 40, apiInstances: 2, apiPoolSize: 10 })).toBe(1);
});
it("can be told a deploy never overlaps API instances (e.g. a non-cluster or single-instance API)", () => {
// 100 - 3 - (2 * 13) - 10 = 61; floor(61 / 2) - 1 = 29.
expect(dbConnectionBudget(500, { apiReloadOverlap: 0 })).toBe(29);
});
it("can be told the worker never overlaps during a deploy", () => {
// 100 - 3 - (3 * 13) - 10 = 48; floor(48 / 1) - 1 = 47 (the old steady-state budget, BIT-31550 era).
expect(dbConnectionBudget(500, { workerReloadOverlap: 0 })).toBe(47);
});
it("never goes below 1, even if the API and headroom already exceed max_connections", () => {
expect(dbConnectionBudget(50, { max: 20, apiInstances: 2, apiPoolSize: 10 })).toBe(1);
});
it("keeps the total at or below max_connections during a worker deploy overlap", () => {
const max = 100;
const budget = dbConnectionBudget(500, { max });
// Two workers (old draining + new), each with `budget` Prisma + 1 broadcaster, plus API at deploy overlap.
const apiConnections = 3 * (10 + 3);
const total = apiConnections + 2 * (budget + 1) + 3 + 10;
expect(total).toBeLessThanOrEqual(max);
});
});