From f2eec479d9cc3a1afcfdeb8d109f47769cf33fde Mon Sep 17 00:00:00 2001 From: Park Dahyeon Date: Mon, 9 Nov 2026 18:40:51 +0900 Subject: [PATCH] fix(api): correct champion history and deduplicate stats builds Apply participant and champion filters before pagination and exclude timeline payloads. Route result statistics through the durable outbox, reuse current fragments, and serialize projection effects without spending retries on contention. Validated with 716 API tests, 32 isolated Redis integration tests, API typecheck and build, and lint on changed files. --- .../match-stats-idempotency.spec.ts | 173 +++++++++++++++ .../match-stats.repository.spec.ts | 29 +++ .../community-stats/match-stats.repository.ts | 19 ++ .../match-stats.service.spec.ts | 7 + .../community-stats/match-stats.service.ts | 18 ++ .../match-history/match-history.query.spec.ts | 208 ++++++++++++++++++ .../match-history/match-history.service.ts | 29 +-- .../competition-outbox.processor.spec.ts | 119 ++++++++++ .../competition-outbox.processor.ts | 27 ++- .../tournament-result.cutover.spec.ts | 15 +- ...tournament-result.poll-reliability.spec.ts | 1 - .../tournament-result.service.spec.ts | 1 - .../tournament-result.service.ts | 21 +- .../redis-optimization.integration-spec.ts | 84 +++++++ 14 files changed, 704 insertions(+), 47 deletions(-) create mode 100644 apps/api/src/modules/community-stats/match-stats-idempotency.spec.ts create mode 100644 apps/api/src/modules/match-history/match-history.query.spec.ts diff --git a/apps/api/src/modules/community-stats/match-stats-idempotency.spec.ts b/apps/api/src/modules/community-stats/match-stats-idempotency.spec.ts new file mode 100644 index 00000000..892c47fb --- /dev/null +++ b/apps/api/src/modules/community-stats/match-stats-idempotency.spec.ts @@ -0,0 +1,173 @@ +import mongoose from 'mongoose'; +import { ConnectionService } from '../global/database/connection.service'; +import { MatchStatsFragmentBuilder } from './match-stats-fragment.builder'; +import { MatchStatsRepository } from './match-stats.repository'; +import { MatchStatsService } from './match-stats.service'; +import { MATCH_STATS_BUILDER_VERSION } from './match-stats.types'; + +describe('MatchStatsService outbox idempotency', () => { + it('reuses the latest stored revision when an older outbox event is delivered', async () => { + const h = createHarness(); + h.isCurrent.mockResolvedValue(true); + + await h.service.buildAndUpsert(h.matchId, { sourceRevision: 10 }); + + expect(h.isCurrent).toHaveBeenCalledWith({ + matchId: h.matchId, + sourceCompetitionId: h.competitionId, + sourceRevision: 12, + projectorVersion: 3, + builderVersion: MATCH_STATS_BUILDER_VERSION, + }); + expect(h.riotFind).not.toHaveBeenCalled(); + expect(h.builder.buildFragment).not.toHaveBeenCalled(); + expect(h.upsert).not.toHaveBeenCalled(); + }); + + it('builds once across repeated deliveries after the write succeeds', async () => { + const h = createHarness(); + h.upsert.mockImplementation(async () => { + h.isCurrent.mockResolvedValue(true); + }); + + await h.service.buildAndUpsert(h.matchId, { sourceRevision: 12 }); + await h.service.buildAndUpsert(h.matchId, { sourceRevision: 12 }); + + expect(h.riotFind).toHaveBeenCalledTimes(1); + expect(h.builder.buildFragment).toHaveBeenCalledTimes(1); + expect(h.upsert).toHaveBeenCalledTimes(1); + }); + + it('retries a failed write without treating an attempted build as complete', async () => { + const h = createHarness(); + h.upsert.mockRejectedValueOnce(new Error('database unavailable')); + await expect( + h.service.buildAndUpsert(h.matchId, { sourceRevision: 12 }), + ).rejects.toThrow('database unavailable'); + + await h.service.buildAndUpsert(h.matchId, { sourceRevision: 12 }); + + expect(h.builder.buildFragment).toHaveBeenCalledTimes(2); + expect(h.upsert).toHaveBeenCalledTimes(2); + }); + + it.each([{}, { sourceRevision: 12, includeTimeline: true }])( + 'still performs explicit rebuilds and timeline hydration (%j)', + async (options) => { + const h = createHarness(); + h.isCurrent.mockResolvedValue(true); + + await h.service.buildAndUpsert(h.matchId, options); + + expect(h.isCurrent).not.toHaveBeenCalled(); + expect(h.builder.buildFragment).toHaveBeenCalledTimes(1); + expect(h.upsert).toHaveBeenCalledTimes(1); + if ('includeTimeline' in options) { + expect(h.getMatchTimeline).toHaveBeenCalledWith('KR_1', { + region: 'asia', + }); + } + }, + ); + + it('deletes reversed zero-game stats even if their versions were current', async () => { + const h = createHarness(); + h.competition.projection.games = []; + h.isCurrent.mockResolvedValue(true); + + await h.service.buildAndUpsert(h.matchId, { sourceRevision: 12 }); + + expect(h.isCurrent).not.toHaveBeenCalled(); + expect(h.deleteByMatch).toHaveBeenCalledWith(h.matchId, { + sourceCompetitionId: h.competitionId, + sourceRevision: 12, + }); + }); +}); + +function createHarness() { + const matchId = new mongoose.Types.ObjectId(); + const competitionId = new mongoose.Types.ObjectId(); + const teamAId = new mongoose.Types.ObjectId(); + const teamBId = new mongoose.Types.ObjectId(); + const match = { + _id: matchId, + communityId: new mongoose.Types.ObjectId(), + status: 'in-progress', + scheduledAt: new Date(), + teams: [], + }; + const competition = { + _id: competitionId, + revision: 12, + projectorVersion: 3, + teams: [ + { teamId: teamAId, displayOrder: 1 }, + { teamId: teamBId, displayOrder: 2 }, + ], + projection: { + phase: 'running', + games: [ + { + displayNumber: 1, + evidence: { type: 'riot', riotMatchId: 'KR_1' }, + teams: { blue: { teamId: teamAId }, red: { teamId: teamBId } }, + winnerTeamId: teamAId, + }, + ], + }, + }; + const riotFind = jest.fn(() => query([{ matchId: 'KR_1', raw: {} }])); + const models = { + match: { findById: () => query(match) }, + competition: { findOne: () => query(competition) }, + riotMatch: { find: riotFind }, + community: { findById: () => query({ riotRegion: 'asia' }) }, + }; + const connection = { + getModel: async (name: keyof typeof models) => models[name], + } as unknown as ConnectionService; + const isCurrent = jest.fn().mockResolvedValue(false); + const upsert = jest.fn().mockResolvedValue(undefined); + const deleteByMatch = jest.fn().mockResolvedValue(undefined); + const repository = { + hasCurrentCompetitionFragment: isCurrent, + upsert, + deleteByMatch, + } as unknown as MatchStatsRepository; + const builder = { + buildFragment: jest.fn().mockReturnValue({ + matchCard: {}, + records: [], + gameDetails: [], + userIds: [], + championIds: [], + gameCreationMin: 1, + gameCreationMax: 2, + }), + } as unknown as jest.Mocked; + const getMatchTimeline = jest.fn().mockResolvedValue(undefined); + const service = new MatchStatsService(connection, repository, builder, { + getMatchTimeline, + } as never); + return { + service, + matchId, + competitionId, + competition, + isCurrent, + upsert, + deleteByMatch, + riotFind, + builder, + getMatchTimeline, + }; +} + +function query(value: T) { + return { + select: jest.fn().mockReturnThis(), + lean: jest.fn().mockReturnThis(), + exec: async () => value, + }; +} diff --git a/apps/api/src/modules/community-stats/match-stats.repository.spec.ts b/apps/api/src/modules/community-stats/match-stats.repository.spec.ts index 6513d10f..9a2d1df7 100644 --- a/apps/api/src/modules/community-stats/match-stats.repository.spec.ts +++ b/apps/api/src/modules/community-stats/match-stats.repository.spec.ts @@ -4,6 +4,35 @@ import type { MatchStats } from '../global/database/schema'; import { MatchStatsRepository } from './match-stats.repository'; describe('MatchStatsRepository', () => { + it.each([null, { _id: new Types.ObjectId() }])( + 'checks persisted source identity and every build version without loading payloads (%j)', + async (stored) => { + const exists = jest.fn(() => ({ exec: async () => stored })); + const connection = { + getModel: jest.fn().mockResolvedValue({ exists }), + } as unknown as ConnectionService; + const repository = new MatchStatsRepository(connection); + const source = { + matchId: new Types.ObjectId(), + sourceCompetitionId: new Types.ObjectId(), + sourceRevision: 12, + projectorVersion: 3, + builderVersion: 10, + }; + + await expect( + repository.hasCurrentCompetitionFragment(source), + ).resolves.toBe(Boolean(stored)); + expect(exists).toHaveBeenCalledWith({ + matchId: source.matchId, + sourceCompetitionId: source.sourceCompetitionId, + sourceRevision: { $gte: 12 }, + projectorVersion: { $gte: 3 }, + builderVersion: { $gte: 10 }, + }); + }, + ); + it('does not let a legacy upsert overwrite a Competition-backed fragment', async () => { const fragment = { ...statsFragment(12), diff --git a/apps/api/src/modules/community-stats/match-stats.repository.ts b/apps/api/src/modules/community-stats/match-stats.repository.ts index f56a3baa..0c5fd3ea 100644 --- a/apps/api/src/modules/community-stats/match-stats.repository.ts +++ b/apps/api/src/modules/community-stats/match-stats.repository.ts @@ -22,6 +22,25 @@ export interface MatchStatsDeleteGuard { export class MatchStatsRepository { constructor(private readonly connectionService: ConnectionService) {} + public async hasCurrentCompetitionFragment(params: { + matchId: mongoose.Types.ObjectId; + sourceCompetitionId: mongoose.Types.ObjectId; + sourceRevision: number; + projectorVersion: number; + builderVersion: number; + }): Promise { + const MatchStatsModel = await this.connectionService.getModel('matchStats'); + return Boolean( + await MatchStatsModel.exists({ + matchId: params.matchId, + sourceCompetitionId: params.sourceCompetitionId, + sourceRevision: { $gte: params.sourceRevision }, + projectorVersion: { $gte: params.projectorVersion }, + builderVersion: { $gte: params.builderVersion }, + }).exec(), + ); + } + public async upsert( fragment: Omit, ): Promise { diff --git a/apps/api/src/modules/community-stats/match-stats.service.spec.ts b/apps/api/src/modules/community-stats/match-stats.service.spec.ts index 251412ad..dd6ff5a6 100644 --- a/apps/api/src/modules/community-stats/match-stats.service.spec.ts +++ b/apps/api/src/modules/community-stats/match-stats.service.spec.ts @@ -24,6 +24,7 @@ describe('MatchStatsService', () => { }), } as unknown as ConnectionService; const repository = { + hasCurrentCompetitionFragment: jest.fn().mockResolvedValue(false), deleteByMatch: jest.fn().mockResolvedValue(undefined), upsert: jest.fn(), } as unknown as jest.Mocked; @@ -72,6 +73,7 @@ describe('MatchStatsService', () => { }), } as unknown as ConnectionService; const repository = { + hasCurrentCompetitionFragment: jest.fn().mockResolvedValue(false), deleteByMatch: jest.fn().mockResolvedValue(undefined), upsert: jest.fn(), } as unknown as jest.Mocked; @@ -132,6 +134,7 @@ describe('MatchStatsService', () => { }), } as unknown as ConnectionService; const repository = { + hasCurrentCompetitionFragment: jest.fn().mockResolvedValue(false), upsert: jest.fn().mockResolvedValue(undefined), deleteByMatch: jest.fn(), } as unknown as jest.Mocked; @@ -273,6 +276,7 @@ describe('MatchStatsService', () => { }), } as unknown as ConnectionService; const repository = { + hasCurrentCompetitionFragment: jest.fn().mockResolvedValue(false), upsert: jest.fn().mockResolvedValue(undefined), deleteByMatch: jest.fn(), deleteLegacyByMatch: jest.fn(), @@ -408,6 +412,7 @@ describe('MatchStatsService', () => { }), } as unknown as ConnectionService; const repository = { + hasCurrentCompetitionFragment: jest.fn().mockResolvedValue(false), upsert: jest.fn().mockResolvedValue(undefined), deleteByMatch: jest.fn(), } as unknown as jest.Mocked; @@ -512,6 +517,7 @@ describe('MatchStatsService', () => { }), } as unknown as ConnectionService; const repository = { + hasCurrentCompetitionFragment: jest.fn().mockResolvedValue(false), upsert: jest.fn().mockResolvedValue(undefined), deleteByMatch: jest.fn().mockResolvedValue(undefined), deleteLegacyByMatch: jest.fn(), @@ -573,6 +579,7 @@ describe('MatchStatsService', () => { }), } as unknown as ConnectionService; const repository = { + hasCurrentCompetitionFragment: jest.fn().mockResolvedValue(false), upsert: jest.fn(), deleteByMatch: jest.fn(), } as unknown as jest.Mocked; diff --git a/apps/api/src/modules/community-stats/match-stats.service.ts b/apps/api/src/modules/community-stats/match-stats.service.ts index 6978276a..e099c294 100644 --- a/apps/api/src/modules/community-stats/match-stats.service.ts +++ b/apps/api/src/modules/community-stats/match-stats.service.ts @@ -59,6 +59,24 @@ export class MatchStatsService { return; } + // Outbox redelivery (including older events observing the latest revision) + // must not reload Riot payloads or replace an already enriched fragment. + // Explicit rebuilds and timeline hydration still run at the same revision. + if ( + options.sourceRevision !== undefined && + !options.includeTimeline && + competitionSource && + (await this.repository.hasCurrentCompetitionFragment({ + matchId, + sourceCompetitionId: competitionSource.sourceCompetitionId, + sourceRevision: competitionSource.sourceRevision, + projectorVersion: competitionSource.projectorVersion, + builderVersion: MATCH_STATS_BUILDER_VERSION, + })) + ) { + return; + } + const riotIds = competitionSource?.riotMatchIds ?? [ ...new Set( (match.result?.games ?? []) diff --git a/apps/api/src/modules/match-history/match-history.query.spec.ts b/apps/api/src/modules/match-history/match-history.query.spec.ts new file mode 100644 index 00000000..d786d7d2 --- /dev/null +++ b/apps/api/src/modules/match-history/match-history.query.spec.ts @@ -0,0 +1,208 @@ +import mongoose from 'mongoose'; +import { ConnectionService } from '../global/database/connection.service'; +import { MatchHistoryService } from './match-history.service'; + +const SEASON_START = Date.parse('2026-01-01T00:00:00Z'); + +describe('MatchHistoryService champion filtering', () => { + it('paginates matching games beyond 120 newer games and excludes opponents using the champion', async () => { + const harness = createHarness(); + const filters = { championId: 99, rank: 'solo', limit: 2 }; + const first = await harness.service.getRecentMatches({ + ...harness.scope, + filters, + }); + + expect(first.matches.map((match) => match.matchId)).toEqual([ + 'KR_3', + 'KR_2', + ]); + expect(first.nextCursor).toBe(SEASON_START + 2_000); + const second = await harness.service.getRecentMatches({ + ...harness.scope, + filters: { ...filters, cursor: first.nextCursor! }, + }); + expect(second.matches.map((match) => match.matchId)).toEqual(['KR_1']); + expect(second.nextCursor).toBeNull(); + expect(harness.returnedCounts).toEqual([3, 1]); + expect(harness.find).toHaveBeenLastCalledWith({ + participantPuuids: 'player', + queueId: { $in: [420] }, + gameCreation: { $gte: SEASON_START, $lt: first.nextCursor }, + 'raw.info.participants': { + $elemMatch: { puuid: 'player', championId: 99 }, + }, + }); + }); + + it('summarizes matching games across the former scan boundary', async () => { + const { service, scope, returnedCounts } = createHarness(); + const summary = await service.getRecentSummary({ + ...scope, + filters: { championId: 99, rank: 'solo', limit: 20 }, + }); + + expect(summary).toMatchObject({ windowSize: 3, wins: 3, losses: 0 }); + expect(summary.topChampions).toEqual([ + expect.objectContaining({ championId: 99, games: 3 }), + ]); + expect(returnedCounts).toEqual([3]); + }); + + it('keeps unfiltered pagination and does not transfer timeline payloads', async () => { + const { service, scope, find, select, returnedCounts } = createHarness(); + const result = await service.getRecentMatches({ + ...scope, + filters: { rank: 'solo', limit: 2 }, + }); + + expect(result.matches.map((match) => match.matchId)).toEqual([ + 'KR_130', + 'KR_129', + ]); + expect(result.nextCursor).toBe(SEASON_START + 129_000); + expect(find.mock.calls[0][0]['raw.info.participants']).toBeUndefined(); + expect(select).toHaveBeenCalledWith({ timeline: 0 }); + expect(returnedCounts).toEqual([3]); + }); +}); + +interface HistoryQuery { + participantPuuids: string; + queueId: { $in: number[] }; + gameCreation?: { $gte?: number; $lt?: number }; + 'raw.info.participants'?: { + $elemMatch: { puuid: string; championId: number }; + }; +} + +function createHarness() { + const communityId = new mongoose.Types.ObjectId(); + const userId = new mongoose.Types.ObjectId(); + // In the newer games only the opponent played champion 99. + const rows = [ + ...Array.from({ length: 127 }, (_, index) => storedMatch(index + 4, 1)), + storedMatch(3, 99), + storedMatch(2, 99), + storedMatch(1, 99), + storedMatch(131, 99, 450), // Wrong queue. + storedMatch(-1, 99), // Before the season. + ]; + const returnedCounts: number[] = []; + const select = jest.fn(); + const find = jest.fn((filter: HistoryQuery) => { + let limit = rows.length; + const query = { + sort: jest.fn().mockReturnThis(), + limit: (value: number) => { + limit = value; + return query; + }, + select: (projection: unknown) => { + select(projection); + return query; + }, + exec: async () => { + const participant = filter['raw.info.participants']?.$elemMatch; + const result = rows + .filter( + (row) => + row.participantPuuids.includes(filter.participantPuuids) && + filter.queueId.$in.includes(row.queueId) && + (filter.gameCreation?.$gte === undefined || + row.gameCreation >= filter.gameCreation.$gte) && + (filter.gameCreation?.$lt === undefined || + row.gameCreation < filter.gameCreation.$lt) && + (!participant || + row.raw.info.participants.some( + (entry) => + entry.puuid === participant.puuid && + entry.championId === participant.championId, + )), + ) + .sort((a, b) => b.gameCreation - a.gameCreation) + .slice(0, limit); + returnedCounts.push(result.length); + return result; + }, + }; + return query; + }); + const models = { + user: { + findById: () => ({ + exec: async () => ({ + _id: userId, + communityId, + riotAccounts: [{ puuid: 'player', isPrimary: true }], + }), + }), + }, + community: { + findById: () => ({ + exec: async () => ({ + _id: communityId, + seasonStartedAt: new Date(SEASON_START), + }), + }), + }, + riotMatch: { find }, + }; + const connection = { + getModel: async (name: keyof typeof models) => models[name], + } as unknown as ConnectionService; + return { + service: new MatchHistoryService(connection), + scope: { communityId, userId }, + find, + select, + returnedCounts, + }; +} + +function storedMatch(sequence: number, championId: number, queueId = 420) { + const gameCreation = SEASON_START + sequence * 1_000; + const participant = { + puuid: 'player', + championId, + teamId: 100, + win: true, + kills: 5, + deaths: 2, + assists: 3, + totalMinionsKilled: 100, + neutralMinionsKilled: 20, + goldEarned: 10_000, + totalDamageDealtToChampions: 10_000, + totalDamageTaken: 5_000, + perks: { styles: [] }, + }; + return { + gameCreation, + queueId, + participantPuuids: ['player', 'opponent'], + fetchedAt: new Date(), + raw: { + metadata: { matchId: `KR_${sequence}` }, + info: { + gameCreation, + queueId, + gameDuration: 1_800, + participants: [ + participant, + { + ...participant, + puuid: 'opponent', + teamId: 200, + championId: 99, + win: false, + }, + ], + teams: [ + { teamId: 100, win: true }, + { teamId: 200, win: false }, + ], + }, + }, + }; +} diff --git a/apps/api/src/modules/match-history/match-history.service.ts b/apps/api/src/modules/match-history/match-history.service.ts index fa343b4f..b1d6c518 100644 --- a/apps/api/src/modules/match-history/match-history.service.ts +++ b/apps/api/src/modules/match-history/match-history.service.ts @@ -36,7 +36,6 @@ const DEFAULT_RANK: RiotRankFilter = 'all'; const DEFAULT_RECENT_LIMIT = 10; const DEFAULT_SUMMARY_LIMIT = 20; const MAX_AGGREGATE_MATCHES = 500; -const MAX_RECENT_SCAN = 120; interface HistoryContext { community: CommunityDocument; @@ -184,7 +183,6 @@ export class MatchHistoryService { queueIds, championId: params.filters.championId, limit, - scanLimit: params.filters.championId ? MAX_RECENT_SCAN : undefined, }); const views = docs .map((doc) => this.toRecentMatchView(doc, context.account.puuid)) @@ -215,7 +213,6 @@ export class MatchHistoryService { championId: params.filters.championId, limit: limit + 1, cursor: params.filters.cursor, - scanLimit: params.filters.championId ? MAX_RECENT_SCAN : undefined, }); const views = docs .map((doc) => this.toRecentMatchView(doc, context.account.puuid)) @@ -337,7 +334,6 @@ export class MatchHistoryService { limit: number; cursor?: number; championId?: number; - scanLimit?: number; }): Promise { if (params.queueIds.length === 0) return []; const RiotMatchModel = await this.connectionService.getModel('riotMatch'); @@ -352,22 +348,21 @@ export class MatchHistoryService { queueId: { $in: params.queueIds }, }; if (Object.keys(gameCreation).length > 0) query.gameCreation = gameCreation; + if (params.championId !== undefined) { + // Both conditions must match the same participant, before pagination. + query['raw.info.participants'] = { + $elemMatch: { + puuid: params.context.account.puuid, + championId: params.championId, + }, + }; + } - const docs = await RiotMatchModel.find(query) + return RiotMatchModel.find(query) .sort({ gameCreation: -1 }) - .limit(params.scanLimit ?? params.limit) + .limit(params.limit) + .select({ timeline: 0 }) .exec(); - - if (!params.championId) return docs.slice(0, params.limit); - return docs - .filter((doc) => { - const participant = this.findParticipant( - doc.raw as unknown as RiotMatchDto, - params.context.account.puuid, - ); - return participant?.championId === params.championId; - }) - .slice(0, params.limit); } private async aggregateChampionStats(params: { diff --git a/apps/api/src/modules/tournament-result/competition-outbox.processor.spec.ts b/apps/api/src/modules/tournament-result/competition-outbox.processor.spec.ts index 92aba73e..bef5903f 100644 --- a/apps/api/src/modules/tournament-result/competition-outbox.processor.spec.ts +++ b/apps/api/src/modules/tournament-result/competition-outbox.processor.spec.ts @@ -3,6 +3,7 @@ import { Logger } from '@nestjs/common'; import { CommunityStatsService } from '../community-stats/community-stats.service'; import { MatchStatsService } from '../community-stats/match-stats.service'; import { ConnectionService } from '../global/database/connection.service'; +import { DistributedLockService } from '../global/redis/distributed-lock.service'; import { CompetitionAssignmentProvisioner } from './competition-assignment.provisioner'; import { CompetitionOutboxProcessor } from './competition-outbox.processor'; @@ -82,6 +83,7 @@ describe('CompetitionOutboxProcessor', () => { provisioner, stats, communityStats, + availableLock(), ); await expect(processor.drainOnce()).resolves.toBe(1); @@ -147,6 +149,7 @@ describe('CompetitionOutboxProcessor', () => { { provisionReadyAssignments: jest.fn() } as never, { buildAndUpsert: jest.fn() } as never, { invalidateCommunity: jest.fn() } as never, + availableLock(), ); await expect(processor.drainOnce()).resolves.toBe(1); @@ -198,6 +201,7 @@ describe('CompetitionOutboxProcessor', () => { { provisionReadyAssignments: jest.fn() } as never, { buildAndUpsert: jest.fn() } as never, { invalidateCommunity: jest.fn() } as never, + availableLock(), ); await expect(processor.drainOnce()).resolves.toBe(1); @@ -215,6 +219,113 @@ describe('CompetitionOutboxProcessor', () => { expect.stringContaining('moved to dead-letter after 10 attempts'), ); }); + + it('keeps a competing event retryable while another instance is applying the same competition', async () => { + const competitionId = new mongoose.Types.ObjectId(); + const matchId = new mongoose.Types.ObjectId(); + const event = { + _id: new mongoose.Types.ObjectId(), + competitionId, + revision: 2, + type: 'competition.projection-updated', + state: 'pending', + attemptCount: 1, + }; + const updates = jest.fn(() => executable({ matchedCount: 1 })); + const models = { + competitionOutbox: { + findOneAndUpdate: jest.fn(() => leanExecutable(event)), + updateOne: updates, + }, + competition: { + findById: () => + leanExecutable({ + _id: competitionId, + matchId, + communityId: new mongoose.Types.ObjectId(), + revision: 2, + phase: 'running', + teams: [], + projection: { phase: 'running', games: [] }, + }), + }, + match: { + findById: () => leanExecutable({ status: 'in-progress' }), + updateOne: () => executable({ matchedCount: 1 }), + }, + }; + const connection = { + getModel: async (name: keyof typeof models) => models[name], + } as unknown as ConnectionService; + let releaseBuild!: () => void; + let startedBuild!: () => void; + const started = new Promise((resolve) => { + startedBuild = resolve; + }); + const blocked = new Promise((resolve) => { + releaseBuild = resolve; + }); + const buildAndUpsert = jest.fn(async () => { + startedBuild(); + await blocked; + }); + const heldKeys = new Set(); + const runWithRenewedLock = jest.fn( + async ( + options: { lockKey: string }, + operation: () => Promise, + ) => { + if (heldKeys.has(options.lockKey)) return null; + heldKeys.add(options.lockKey); + try { + return await operation(); + } finally { + heldKeys.delete(options.lockKey); + } + }, + ); + const makeProcessor = () => + new CompetitionOutboxProcessor( + connection, + { + provisionReadyAssignments: jest.fn().mockResolvedValue(undefined), + } as never, + { buildAndUpsert } as never, + { + invalidateCommunity: jest.fn().mockResolvedValue(undefined), + } as never, + { runWithRenewedLock } as unknown as DistributedLockService, + ); + const first = makeProcessor().drainOnce(1); + await started; + try { + await expect(makeProcessor().drainOnce(1)).resolves.toBe(1); + expect(buildAndUpsert).toHaveBeenCalledTimes(1); + expect(updates).toHaveBeenCalledWith( + expect.anything(), + expect.objectContaining({ + $set: { state: 'pending', availableAt: expect.any(Date) }, + $inc: { attemptCount: -1 }, + }), + ); + } finally { + releaseBuild(); + await first; + } + expect(runWithRenewedLock).toHaveBeenCalledWith( + expect.objectContaining({ + lockKey: `competition:projection-effects:${competitionId.toString()}`, + }), + expect.any(Function), + ); + expect(heldKeys.size).toBe(0); + expect(updates).toHaveBeenLastCalledWith( + expect.anything(), + expect.objectContaining({ + $set: expect.objectContaining({ state: 'published' }), + }), + ); + }); }); function leanExecutable(value: T) { @@ -229,3 +340,11 @@ function leanExecutable(value: T) { function executable(value: T) { return { exec: jest.fn().mockResolvedValue(value) }; } + +function availableLock(): DistributedLockService { + return { + runWithRenewedLock: jest.fn( + (_options: unknown, operation: () => Promise) => operation(), + ), + } as unknown as DistributedLockService; +} diff --git a/apps/api/src/modules/tournament-result/competition-outbox.processor.ts b/apps/api/src/modules/tournament-result/competition-outbox.processor.ts index c515c650..8a88442d 100644 --- a/apps/api/src/modules/tournament-result/competition-outbox.processor.ts +++ b/apps/api/src/modules/tournament-result/competition-outbox.processor.ts @@ -8,6 +8,7 @@ import mongoose from 'mongoose'; import { CommunityStatsService } from '../community-stats/community-stats.service'; import { MatchStatsService } from '../community-stats/match-stats.service'; import { ConnectionService } from '../global/database/connection.service'; +import { DistributedLockService } from '../global/redis/distributed-lock.service'; import { CompetitionAssignmentProvisioner } from './competition-assignment.provisioner'; import type { CompetitionResultSnapshot } from './competition-result.adapter'; @@ -54,6 +55,7 @@ export class CompetitionOutboxProcessor private readonly assignmentProvisioner: CompetitionAssignmentProvisioner, private readonly matchStatsService: MatchStatsService, private readonly communityStatsService: CommunityStatsService, + private readonly distributedLockService: DistributedLockService, ) {} public onModuleInit(): void { @@ -125,7 +127,30 @@ export class CompetitionOutboxProcessor if (event.type !== 'competition.projection-updated') { throw new Error(`Unsupported Competition outbox type ${event.type}.`); } - await this.applyProjectionEffects(event); + // Different revision events can be leased by different API instances. + // Serialize their effects so the next builder can reuse committed stats. + const applied = await this.distributedLockService.runWithRenewedLock( + { + lockKey: `competition:projection-effects:${event.competitionId.toString()}`, + lockExpirationDurationMilliseconds: PROCESSING_LEASE_MS, + lockRenewalIntervalMilliseconds: PROCESSING_LEASE_MS / 3, + }, + async () => { + await this.applyProjectionEffects(event); + return true; + }, + ); + if (!applied) { + // Contention is not a failed delivery and must not exhaust retries. + await OutboxModel.updateOne(leaseFilter(event), { + $set: { + state: 'pending', + availableAt: new Date(Date.now() + DRAIN_INTERVAL_MS), + }, + $inc: { attemptCount: -1 }, + }).exec(); + return; + } await OutboxModel.updateOne(leaseFilter(event), { $set: { state: 'published', publishedAt: new Date() }, $unset: { lastError: 1 }, diff --git a/apps/api/src/modules/tournament-result/tournament-result.cutover.spec.ts b/apps/api/src/modules/tournament-result/tournament-result.cutover.spec.ts index e197c50a..7cbf9e4a 100644 --- a/apps/api/src/modules/tournament-result/tournament-result.cutover.spec.ts +++ b/apps/api/src/modules/tournament-result/tournament-result.cutover.spec.ts @@ -1,10 +1,17 @@ import mongoose from 'mongoose'; +import { MatchStatsService } from '../community-stats/match-stats.service'; import type { MatchDocument } from '../global/database/schema'; import type { RiotMatchDto } from '../riot/riot.types'; import { TournamentResultService } from './tournament-result.service'; describe('TournamentResultService Competition cutover', () => { + afterEach(() => jest.restoreAllMocks()); + it('accepts a manually collected Riot game through Competition without mutating legacy result state', async () => { + const buildStats = jest.spyOn( + MatchStatsService.prototype, + 'buildAndUpsert', + ); const communityId = new mongoose.Types.ObjectId(); const matchId = new mongoose.Types.ObjectId(); const competitionId = new mongoose.Types.ObjectId(); @@ -192,15 +199,11 @@ describe('TournamentResultService Competition cutover', () => { replayed: false, }), }; - const matchStatsService = { - buildAndUpsert: jest.fn().mockResolvedValue(undefined), - }; const service = new TournamentResultService( connectionService as never, { getMatch: jest.fn().mockResolvedValue(detail) } as never, {} as never, { invalidateCommunity: jest.fn().mockResolvedValue(undefined) } as never, - matchStatsService as never, { runWithLock: jest.fn(), runWithRenewedLock: jest.fn(), @@ -255,9 +258,7 @@ describe('TournamentResultService Competition cutover', () => { expect(match.result).toBe(legacyResult); expect(match.doubleElimination).toBe(legacyBracket); expect(match.status).toBe('finished'); - expect(matchStatsService.buildAndUpsert).toHaveBeenCalledWith(matchId, { - sourceRevision: 1, - }); + expect(buildStats).not.toHaveBeenCalled(); }); }); diff --git a/apps/api/src/modules/tournament-result/tournament-result.poll-reliability.spec.ts b/apps/api/src/modules/tournament-result/tournament-result.poll-reliability.spec.ts index 6d6cebbf..8c5ae94e 100644 --- a/apps/api/src/modules/tournament-result/tournament-result.poll-reliability.spec.ts +++ b/apps/api/src/modules/tournament-result/tournament-result.poll-reliability.spec.ts @@ -208,7 +208,6 @@ function createHarness( { getMatchIdsByPuuid: () => Promise.resolve([]) } as never, queue as never, {} as never, - {} as never, locks as never, { bootstrap: () => Promise.resolve(competition) } as never, {} as never, diff --git a/apps/api/src/modules/tournament-result/tournament-result.service.spec.ts b/apps/api/src/modules/tournament-result/tournament-result.service.spec.ts index 71d43812..2521588a 100644 --- a/apps/api/src/modules/tournament-result/tournament-result.service.spec.ts +++ b/apps/api/src/modules/tournament-result/tournament-result.service.spec.ts @@ -313,7 +313,6 @@ function pollHarness(params: { riotMatchService as never, riotJobQueueService as never, { invalidateCommunity: jest.fn().mockResolvedValue(undefined) } as never, - { buildAndUpsert: jest.fn().mockResolvedValue(undefined) } as never, distributedLockService as never, { bootstrap: jest.fn().mockResolvedValue(readyCompetition) } as never, commandService as never, diff --git a/apps/api/src/modules/tournament-result/tournament-result.service.ts b/apps/api/src/modules/tournament-result/tournament-result.service.ts index 7b4a5309..1e8cd46a 100644 --- a/apps/api/src/modules/tournament-result/tournament-result.service.ts +++ b/apps/api/src/modules/tournament-result/tournament-result.service.ts @@ -7,7 +7,6 @@ import { import { createHash } from 'node:crypto'; import mongoose from 'mongoose'; import { CommunityStatsService } from '../community-stats/community-stats.service'; -import { MatchStatsService } from '../community-stats/match-stats.service'; import { CompetitionBootstrapService, type CompetitionBootstrapMatch, @@ -95,7 +94,6 @@ export class TournamentResultService { private readonly riotMatchService: RiotMatchService, private readonly riotJobQueueService: RiotJobQueueService, private readonly communityStatsService: CommunityStatsService, - private readonly matchStatsService: MatchStatsService, private readonly distributedLockService: DistributedLockService, private readonly competitionBootstrapService: CompetitionBootstrapService, private readonly competitionCommandService: CompetitionCommandService, @@ -516,9 +514,7 @@ export class TournamentResultService { params.match, competition, ); - if (!result.replayed) { - await this.rebuildMatchStatsSafe(params.match, result.revision); - } + // acceptGame commits the stats rebuild to the durable Competition outbox. return { kind: 'accepted', displayNumber: mapped.displayNumber, @@ -708,21 +704,6 @@ export class TournamentResultService { } } - private async rebuildMatchStatsSafe( - match: MatchDocument, - sourceRevision: number, - ): Promise { - try { - await this.matchStatsService.buildAndUpsert(match._id, { - sourceRevision, - }); - } catch (error) { - this.logger.warn( - `Failed to rebuild match stats for ${match._id.toString()} at Competition revision ${sourceRevision}: ${errorMessage(error)}`, - ); - } - } - private async ensureRiotMatchPersisted( riotMatchId: string, region: RiotRegionalRoute, diff --git a/apps/api/test/redis-optimization.integration-spec.ts b/apps/api/test/redis-optimization.integration-spec.ts index 7acb9599..c16de17c 100644 --- a/apps/api/test/redis-optimization.integration-spec.ts +++ b/apps/api/test/redis-optimization.integration-spec.ts @@ -22,6 +22,7 @@ import { import { CommunityStatsService } from '../src/modules/community-stats/community-stats.service'; import { PROMOTE_SCHEDULED_RIOT_JOB_SCRIPT } from '../src/modules/riot-job-queue/riot-job-queue.scripts'; import mongoose from 'mongoose'; +import { CompetitionOutboxProcessor } from '../src/modules/tournament-result/competition-outbox.processor'; const client = createClient({ url: process.env.REDIS_TEST_URL, @@ -165,6 +166,89 @@ describe('Redis native operations', () => { expect(await client.exists('lock')).toBe(0); }); + it('defers locked Competition effects without spending a retry, then releases its own lock after delivery', async () => { + const locks = new DistributedLockService(redis); + const competitionId = new mongoose.Types.ObjectId(); + const matchId = new mongoose.Types.ObjectId(); + const lockKey = `competition:projection-effects:${competitionId.toString()}`; + const owner = await locks.acquireLock({ + lockKey, + lockExpirationDurationMilliseconds: 60_000, + }); + expect(owner).not.toBeNull(); + const event = { + _id: new mongoose.Types.ObjectId(), + competitionId, + revision: 2, + type: 'competition.projection-updated', + state: 'pending', + attemptCount: 10, + }; + const query = (value: T) => ({ + lean: jest.fn().mockReturnThis(), + exec: async () => value, + }); + const publish = jest.fn(() => query({ matchedCount: 1 })); + const models = { + competitionOutbox: { + findOneAndUpdate: () => query(event), + updateOne: publish, + }, + competition: { + findById: () => + query({ + _id: competitionId, + matchId, + communityId: new mongoose.Types.ObjectId(), + revision: 2, + phase: 'running', + teams: [], + projection: { phase: 'running', games: [] }, + }), + }, + match: { + findById: () => query({ status: 'in-progress' }), + updateOne: () => query({ matchedCount: 1 }), + }, + }; + const buildAndUpsert = jest.fn().mockResolvedValue(undefined); + const invalidateCommunity = jest.fn().mockResolvedValue(undefined); + const processor = new CompetitionOutboxProcessor( + { getModel: async (name: keyof typeof models) => models[name] } as never, + { + provisionReadyAssignments: jest.fn().mockResolvedValue(undefined), + } as never, + { buildAndUpsert } as never, + { invalidateCommunity } as never, + locks, + ); + try { + await processor.drainOnce(1); + expect(buildAndUpsert).not.toHaveBeenCalled(); + expect(publish).toHaveBeenCalledWith( + expect.objectContaining({ attemptCount: 10 }), + { + $set: { state: 'pending', availableAt: expect.any(Date) }, + $inc: { attemptCount: -1 }, + }, + ); + expect(await client.get(lockKey)).toBe(owner!.lockOwnershipToken); + } finally { + await locks.releaseLockIfOwned(owner!); + } + + await processor.drainOnce(1); + expect(buildAndUpsert).toHaveBeenCalledTimes(1); + expect(invalidateCommunity).toHaveBeenCalledTimes(1); + expect(publish).toHaveBeenLastCalledWith( + expect.anything(), + expect.objectContaining({ + $set: expect.objectContaining({ state: 'published' }), + }), + ); + expect(await client.exists(lockKey)).toBe(0); + }); + it('does not clear the dedup token of a newer refresh job', async () => { const job = { jobId: 'old',