From 941688d2c9fa45b7739c9c626023f8774e5ed7da Mon Sep 17 00:00:00 2001 From: nelson-baptista Date: Fri, 4 Dec 2026 13:20:12 +0000 Subject: [PATCH] fix(archive): skip rebuildDirectory when another worker holds the advisory lock (BIT-33539) Two workers could pick the same due source from rebuildDue (the SELECT has no locking), both call deleteMany + createMany in overlapping transactions, and the second commit hit archive_team_sides_pkey with P2002. Add pg_try_advisory_xact_lock(hashtext(sourceId)) at the start of the transaction: the second worker skips instead of colliding. Paperclip-Issue: BIT-33539 --- apps/api/src/archive/service.test.ts | 53 ++++++++++++++++++++++++++++ apps/api/src/archive/service.ts | 6 +++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/apps/api/src/archive/service.test.ts b/apps/api/src/archive/service.test.ts index 47f2eff..003dc90 100644 --- a/apps/api/src/archive/service.test.ts +++ b/apps/api/src/archive/service.test.ts @@ -345,3 +345,56 @@ describe("walk recovers from a P2002 race on cloudFile.create (BIT-33532)", () = ).rejects.toThrow("boom"); }); }); + +describe("rebuildDirectory skips when another worker holds the advisory lock (BIT-33539)", () => { + const makeDb = (lockOk: boolean) => { + const calls: string[] = []; + const archiveTeamSide = { + deleteMany: async () => { calls.push("deleteMany"); return { count: 0 }; }, + createMany: async () => { calls.push("createMany"); return { count: 1 }; }, + }; + const cloudSource = { + findUnique: async () => ({ id: "src-1", look: "club", baseUrl: "https://cloud.example" }), + updateMany: async () => { calls.push("updateMany"); return { count: 1 }; }, + }; + const tx = { + $queryRaw: async () => [{ ok: lockOk }], + archiveTeamSide, + cloudSource: { updateMany: cloudSource.updateMany }, + }; + return { + calls, + db: { + cloudSource, + sportsTeam: { findMany: async () => [] }, + archiveClubAlias: { findMany: async () => [] }, + cloudFolder: { + findMany: async () => [ + { event: { id: "evt-1", home: "SLB", away: "SCP" } }, + ], + }, + $transaction: async (cb: (tx: typeof tx) => Promise) => cb(tx), + }, + }; + }; + + it("writes rows when the lock is acquired", async () => { + const { calls, db } = makeDb(true); + const service = new ArchiveService({ db, log: { info() {}, debug() {}, error() {}, warn() {} } } as never); + const sides = await (service as unknown as { rebuildDirectory: (sourceId: string) => Promise }).rebuildDirectory("src-1"); + expect(sides).toBeGreaterThan(0); + expect(calls).toContain("deleteMany"); + expect(calls).toContain("createMany"); + expect(calls).toContain("updateMany"); + }); + + it("skips deleteMany and createMany when the lock is held by another worker", async () => { + const { calls, db } = makeDb(false); + const service = new ArchiveService({ db, log: { info() {}, debug() {}, error() {}, warn() {} } } as never); + const sides = await (service as unknown as { rebuildDirectory: (sourceId: string) => Promise }).rebuildDirectory("src-1"); + expect(sides).toBe(0); + expect(calls).not.toContain("deleteMany"); + expect(calls).not.toContain("createMany"); + expect(calls).not.toContain("updateMany"); + }); +}); diff --git a/apps/api/src/archive/service.ts b/apps/api/src/archive/service.ts index b05f38a..9d40b39 100644 --- a/apps/api/src/archive/service.ts +++ b/apps/api/src/archive/service.ts @@ -1465,7 +1465,11 @@ export class ArchiveService { const games = new Map(); for (const folder of folders) if (folder.event) games.set(folder.event.id, folder.event); const rows = teamSideRows(games.values(), teamMatcher(teams), new Map(aliases.map((alias) => [alias.aliasKey, alias.name]))); + let stored = rows.length; await db.$transaction(async (tx) => { + // Two workers can pick the same due source (BIT-33539): the second skips instead of colliding on the PK. + const [locked] = await tx.$queryRaw<{ ok: boolean }[]>`SELECT pg_try_advisory_xact_lock(hashtext(${sourceId})::bigint) AS ok`; + if (!locked?.ok) { stored = 0; return; } await tx.archiveTeamSide.deleteMany({ where: { sourceId } }); const chunk = 2000; for (let i = 0; i < rows.length; i += chunk) { @@ -1478,7 +1482,7 @@ export class ArchiveService { data: { directoryBuiltAt: new Date(), directoryDirtyAt: null }, }); }, { timeout: 60_000, maxWait: 60_000 }); - return rows.length; + return stored; } /** One archive whose teams were never stored, or have changed. */ -- 2.47.3