Merge pull request 'fix(archive): skip rebuildDirectory when another worker holds the advisory lock (BIT-33539)' (#330) from fix/BIT-33539-archive-rebuild-directory-pk-race into main
This commit was merged in pull request #330.
This commit is contained in:
commit
6cd8ba0b95
2 files changed
+58
-1
No files matched your search
@@ -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<void>) => 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<number> }).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<number> }).rebuildDirectory("src-1");
|
||||
expect(sides).toBe(0);
|
||||
expect(calls).not.toContain("deleteMany");
|
||||
expect(calls).not.toContain("createMany");
|
||||
expect(calls).not.toContain("updateMany");
|
||||
});
|
||||
});
|
||||
@@ -1465,7 +1465,11 @@ export class ArchiveService {
|
||||
const games = new Map<string, { id: string; home: string; away: string }>();
|
||||
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. */
|
||||
|
||||
Reference in new issue
Block a user