dbConnectionBudget() sized the API cluster's connections at exactly apiInstances (2), but pm2 startOrReload on the clustered photoreport-api app brings a new instance online and confirms it healthy before stopping an old one, so for a few seconds during every deploy there are apiInstances + 1 processes alive, each with its own Prisma pool and realtime connections. That transient extra instance (13 connections with default pool/realtime sizes) was enough to push the cluster over the budget BIT-31533 set and into Postgres' superuser_reserved_connections, surfacing as the same "too many database connections ... reserved for roles with the SUPERUSER attribute" error from the worker's ArchiveService (BIT-31550, same cluster as BIT-31519/BIT-31533). dbConnectionBudget() now takes apiReloadOverlap (default 1) and folds it into the API cluster term so the worker's own pool is sized to leave room for the overlap. Default worker budget on a 100-max_connections host drops from 60 to 47. Paperclip-Agent: José Alves (urlKey jos-alves; id 11179220-3cec-4f0f-880d-2771ff64fce8) Paperclip-Issue: BIT-31550 Paperclip-Run: 71ac411e-1cce-46e6-b745-7f380228cf79
62 lines
3.2 KiB
TypeScript
62 lines
3.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, delivery: 4 });
|
|
expect(big).toMatchObject({ cpus: 32, faces: 6, ingest: 8, archive: 9, archiveBurst: 91, render: 4, delivery: 8 });
|
|
expect(planCapacity({}, { cpus: 48, memoryBytes: 64 * GB })).toMatchObject({ faces: 6, 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 overlap, the superuser reserve, the broadcaster and headroom leave of max_connections", () => {
|
|
// 100 - 3 superuser reserved - ((2 api instances + 1 reload overlap) * (10 pool + 3 realtime)) - 1 broadcaster - 10 headroom = 47.
|
|
expect(dbConnectionBudget(500)).toBe(47);
|
|
expect(dbConnectionBudget(10)).toBe(10);
|
|
});
|
|
|
|
it("follows a differently sized API cluster or Postgres limit", () => {
|
|
expect(dbConnectionBudget(500, { max: 200, apiInstances: 4, apiPoolSize: 15 })).toBe(96);
|
|
expect(dbConnectionBudget(500, { max: 40, apiInstances: 2, apiPoolSize: 10 })).toBe(1);
|
|
});
|
|
|
|
it("can be told a deploy never overlaps instances (e.g. a non-cluster or single-instance API)", () => {
|
|
expect(dbConnectionBudget(500, { apiReloadOverlap: 0 })).toBe(60);
|
|
});
|
|
|
|
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);
|
|
});
|
|
});
|