diff --git a/apps/api/src/modules/auction/auction.service.spec.ts b/apps/api/src/modules/auction/auction.service.spec.ts index 93f5cf06..9d0a7cad 100644 --- a/apps/api/src/modules/auction/auction.service.spec.ts +++ b/apps/api/src/modules/auction/auction.service.spec.ts @@ -1870,7 +1870,7 @@ describe('AuctionService', () => { expect(session.commitTransaction).toHaveBeenCalledTimes(1); }); - it('uses Redis transition when resetting a redis-v1 auction', async () => { + it('fully deletes redis-v1 auction data when cancelling an auction', async () => { const matchId = new mongoose.Types.ObjectId(); const communityId = new mongoose.Types.ObjectId(); const auctionId = new mongoose.Types.ObjectId(); @@ -1889,58 +1889,37 @@ describe('AuctionService', () => { deleteOne: jest.fn().mockResolvedValue(undefined), }; const match = { + gameSchedule: [{ gameNumber: 1, teamAIndex: 1, teamBIndex: 2 }], communityId, status: 'team-built', - teams: [], - tournamentCodeIds: [], - gameSchedule: [], - participants: [], + teams: [{ teamIndex: 1 }], + tournamentCodeIds: [new mongoose.Types.ObjectId()], + participants: [{ excluded: true }, { excluded: false }], + result: { games: [] }, + resultPollState: { state: 'polling' }, + teamBuiltAt: new Date(), save: jest.fn().mockResolvedValue(undefined), }; - const currentRedisState = { - auctionLifecycleId: 'life-1', - auctionPlayerOrder: [{ userId: 'player-1', passCount: 0 }], - auctionTeams: [ - { - captainUserId: new mongoose.Types.ObjectId(), - members: [], - remainingBidPointAmount: 1000, - teamIndex: 1, - }, - ], - bidRoundExtensionSeconds: 5, - currentAuctionHistorySequence: 4, - currentAuctionPlayerOrderIndex: 0, - currentAuctionRound: 1, - currentBidRoundPlayerUserId: 'player-1', - currentHighestBidPointAmount: 20, - currentHighestBidderTeamIndex: 1, - initialTeamBidPointAmount: 1000, - maximumBidRoundDurationSeconds: 30, - passedAuctionPlayerQueue: [], - phase: 'bidding', - revision: 4, - topTierAuctionPriorityThreshold: 'NONE', - }; - const resetRedisState = { - ...currentRedisState, - auctionPlayerOrder: [], - auctionTeams: [], - currentAuctionHistorySequence: 5, - currentAuctionPlayerOrderIndex: 0, - currentHighestBidPointAmount: 0, - currentHighestBidderTeamIndex: -1, - matchSideEffectRevision: 1, - matchSideEffectStatus: 'pending', - phase: 'waiting', - revision: 5, - }; - const { service, connectionService, auctionRedisCommandService, auctionRedisStateReaderService } = createService({ + const sideEffectDeleteMany = jest.fn().mockReturnValue({ + session: jest.fn().mockResolvedValue(undefined), + }); + const { + service, + connectionService, + tournamentCodeService, + tournamentResultService, + auctionHistoryModel, + auctionRedisCommandService, + auctionRedisService, + } = createService({ auction: { findOne: jest.fn().mockReturnValue({ session: jest.fn().mockResolvedValue(auction), }), }, + auctionSideEffectApplication: { + deleteMany: sideEffectDeleteMany, + }, match: { findById: jest.fn().mockReturnValue({ session: jest.fn().mockResolvedValue(match), @@ -1948,9 +1927,10 @@ describe('AuctionService', () => { }, }); connectionService.createSession.mockResolvedValue(session); - auctionRedisStateReaderService.readAuctionStateView - .mockResolvedValueOnce(currentRedisState) - .mockResolvedValueOnce(resetRedisState); + const redisClient = auctionRedisService.raw(); + redisClient.sendCommand + .mockResolvedValueOnce(1) + .mockResolvedValueOnce(['0', []]); await service.reset({ auctionActionRequestId: '00000000-0000-4000-8000-000000000080', @@ -1963,46 +1943,39 @@ describe('AuctionService', () => { communityId, }); - expect(auction.deleteOne).not.toHaveBeenCalled(); - expect(match.save).not.toHaveBeenCalled(); - expect(auctionRedisCommandService.reserveActionRequest).toHaveBeenCalledWith( - matchId.toString(), - expect.objectContaining({ - auctionActionRequestId: '00000000-0000-4000-8000-000000000080', - auctionStateTransitionId: - 'reset:life-1:00000000-0000-4000-8000-000000000080', - clientObservedAuctionLifecycleId: 'life-1', - clientObservedCurrentAuctionPlayerOrderIndex: 0, - clientObservedCurrentBidRoundPlayerUserId: 'player-1', - clientObservedPhase: 'bidding', - clientObservedRevision: 4, - }), - ); - expect(auctionRedisCommandService.applyTransition).toHaveBeenCalledTimes(1); - const transitionInput = - auctionRedisCommandService.applyTransition.mock.calls[0][1]; - expect(transitionInput.deadline).toEqual({ mode: 'remove' }); - expect(transitionInput.expectedPhase).toBe('bidding'); - expect(transitionInput.liveStatePatch).toMatchObject({ - currentBidRoundDeadlineTimestampMS: 0, - currentBidRoundPlayerUserId: '', - currentHighestBidPointAmount: 0, - currentHighestBidderTeamIndex: -1, - matchSideEffectRevision: 1, - matchSideEffectStatus: 'pending', - phase: 'waiting', - 'team:1:assignedAuctionTeamMemberCount': 0, - 'team:1:remainingBidPointAmount': 0, + expect( + auctionRedisCommandService.reserveActionRequest, + ).not.toHaveBeenCalled(); + expect(auctionRedisCommandService.applyTransition).not.toHaveBeenCalled(); + expect(tournamentCodeService.releaseFromMatch).toHaveBeenCalledWith(match); + expect(tournamentResultService.cancelPolling).toHaveBeenCalledWith(match); + expect(match).toMatchObject({ + gameSchedule: [], + result: undefined, + resultPollState: undefined, + status: 'recruiting', + teamBuiltAt: undefined, + teams: [], + tournamentCodeIds: [], }); - expect(transitionInput.shouldResetBidRuntime).toBe(true); - const durabilityPayload = JSON.parse( - transitionInput.durabilityPayloadJson, - ); - expect(durabilityPayload).toMatchObject({ - auctionActionType: 'reset', - historyMutation: { resetMode: 'clear-history', type: 'reset' }, - matchSideEffectMutation: { type: 'resetMatchAfterAuctionReset' }, + expect(match.participants).toEqual([ + { excluded: false }, + { excluded: false }, + ]); + expect(match.save).toHaveBeenCalledWith({ session }); + expect(sideEffectDeleteMany).toHaveBeenCalledWith({ auctionId }); + expect(auctionHistoryModel.deleteMany).toHaveBeenCalledWith({ + auctionId, }); + expect(auction.deleteOne).toHaveBeenCalledWith({ session }); + expect(redisClient.sendCommand).toHaveBeenCalledWith( + expect.arrayContaining([ + 'auction:bidRoundDeadlineQueue', + matchId.toString(), + 'life-1', + ]), + ); + expect(session.commitTransaction).toHaveBeenCalledTimes(1); }); it('undoes to a history snapshot and releases resources when leaving complete', async () => { diff --git a/apps/api/src/modules/auction/auction.service.ts b/apps/api/src/modules/auction/auction.service.ts index aca77cde..cae1b3bf 100644 --- a/apps/api/src/modules/auction/auction.service.ts +++ b/apps/api/src/modules/auction/auction.service.ts @@ -15,6 +15,7 @@ import { AuctionPlayerSnapshot, AuctionTeam, AuctionTopTierThreshold, + MatchDocument, MatchParticipant, MatchTeamMember, Position, @@ -1128,16 +1129,18 @@ export class AuctionService { const AuctionModel = await this.connectionService.getModel('auction'); const AuctionHistoryModel = await this.connectionService.getModel('auctionHistory'); + const AuctionSideEffectApplicationModel = + await this.connectionService.getModel('auctionSideEffectApplication'); const MatchModel = await this.connectionService.getModel('match'); const session = await this.connectionService.createSession(); if (!session) { throw new InternalServerErrorException('Failed to start DB session.'); } - let redisResetContext: + let redisRuntimeToClear: | { - actionRequestParams?: AuctionStructureActionRequestParams; - auction: AuctionDocument; + auctionLifecycleId?: string; + matchId: string; } | null = null; try { @@ -1154,10 +1157,18 @@ export class AuctionService { } if (this.isRedisV1Auction(auction)) { - redisResetContext = { - auction, - actionRequestParams: params, + redisRuntimeToClear = { + auctionLifecycleId: auction.auctionLifecycleId, + matchId: params.matchId.toString(), }; + await this.resetMatchAfterAuctionCancellation(match, session); + await AuctionSideEffectApplicationModel.deleteMany({ + auctionId: auction._id, + }).session(session); + await AuctionHistoryModel.deleteMany({ auctionId: auction._id }).session( + session, + ); + await auction.deleteOne({ session }); } else { await auction.deleteOne({ session }); await AuctionHistoryModel.deleteMany({ auctionId: auction._id }).session( @@ -1186,8 +1197,11 @@ export class AuctionService { await session.endSession(); } - if (redisResetContext) { - await this.resetRedisAuction(redisResetContext); + if (redisRuntimeToClear) { + await this.clearRedisAuctionRuntimeForLifecycle( + redisRuntimeToClear.matchId, + redisRuntimeToClear.auctionLifecycleId, + ); } } @@ -2223,6 +2237,25 @@ export class AuctionService { ); } + private async resetMatchAfterAuctionCancellation( + match: MatchDocument, + session: mongoose.ClientSession, + ): Promise { + await this.tournamentCodeService.releaseFromMatch(match); + await this.tournamentResultService.cancelPolling(match); + match.status = 'recruiting'; + match.teams = []; + match.tournamentCodeIds = []; + match.gameSchedule = []; + match.participants.forEach((participant) => { + participant.excluded = false; + }); + match.teamBuiltAt = undefined; + match.result = undefined; + match.resultPollState = undefined; + await match.save({ session }); + } + private assertLiveAuctionOperationSupported(auction: AuctionDocument): void { if (auction.auctionEngineVersion && !this.isRedisV1Auction(auction)) { throw new BadRequestException( @@ -2385,13 +2418,13 @@ export class AuctionService { expectedAuctionLifecycleId?: string, ): Promise { const keys = buildAuctionRedisKeys(matchId); - await this.auctionRedisService.raw().sendCommand([ + const cleared = await this.auctionRedisService.raw().sendCommand([ 'EVAL', [ "local expectedAuctionLifecycleId = ARGV[2]", "if expectedAuctionLifecycleId ~= '' then", " local currentAuctionLifecycleId = redis.call('HGET', KEYS[1], 'auctionLifecycleId')", - " if currentAuctionLifecycleId ~= expectedAuctionLifecycleId then", + " if currentAuctionLifecycleId and currentAuctionLifecycleId ~= expectedAuctionLifecycleId then", " return 0", ' end', 'end', @@ -2414,6 +2447,75 @@ export class AuctionService { matchId, expectedAuctionLifecycleId ?? '', ]); + if (Number(cleared) !== 1) return; + await this.clearRedisDurabilityPayloadsForLifecycle( + keys, + expectedAuctionLifecycleId, + ); + } + + private async clearRedisDurabilityPayloadsForLifecycle( + keys: ReturnType, + expectedAuctionLifecycleId?: string, + ): Promise { + let cursor = '0'; + do { + const reply = await this.auctionRedisService.raw().sendCommand([ + 'SCAN', + cursor, + 'MATCH', + `${keys.durabilityPayloadKeyPrefix}*`, + 'COUNT', + '100', + ]); + if (!Array.isArray(reply) || reply.length < 2) return; + cursor = String(reply[0]); + const payloadKeys = Array.isArray(reply[1]) ? reply[1].map(String) : []; + if (payloadKeys.length === 0) continue; + + const values = await this.auctionRedisService + .raw() + .sendCommand(['MGET', ...payloadKeys]); + const payloadKeysToDelete = payloadKeys.filter((payloadKey, index) => + this.shouldDeleteRedisDurabilityPayload( + Array.isArray(values) ? values[index] : undefined, + expectedAuctionLifecycleId, + payloadKey, + ), + ); + if (payloadKeysToDelete.length === 0) continue; + + await this.auctionRedisService.raw().sendCommand([ + 'ZREM', + keys.durabilityRetryQueueKey, + ...payloadKeysToDelete, + ]); + await this.auctionRedisService.raw().sendCommand([ + 'ZREM', + keys.durabilityDeadLetterQueueKey, + ...payloadKeysToDelete, + ]); + await this.auctionRedisService.raw().sendCommand([ + 'DEL', + ...payloadKeysToDelete, + ]); + } while (cursor !== '0'); + } + + private shouldDeleteRedisDurabilityPayload( + payloadValue: unknown, + expectedAuctionLifecycleId: string | undefined, + payloadKey: string, + ): boolean { + if (!expectedAuctionLifecycleId) return true; + if (typeof payloadValue !== 'string') return false; + try { + const payload = JSON.parse(payloadValue) as Record; + return payload.auctionLifecycleId === expectedAuctionLifecycleId; + } catch { + this.logger.warn(`Failed to parse auction durability payload ${payloadKey}.`); + return false; + } } private enqueueBackgroundOrderEnrichment(params: { diff --git a/apps/api/src/scripts/auction-redis-rehearsal.ts b/apps/api/src/scripts/auction-redis-rehearsal.ts index 83f71829..2674a584 100644 --- a/apps/api/src/scripts/auction-redis-rehearsal.ts +++ b/apps/api/src/scripts/auction-redis-rehearsal.ts @@ -21,7 +21,8 @@ import { SecretService } from '../modules/global/secret/secret.service'; type RedisClient = ReturnType; const REHEARSAL_PREFIX = 'redis-sot-rehearsal'; -const WAIT_FOR_EXPIRY_MS = 6200; +const WAIT_FOR_EXPIRY_POLL_INTERVAL_MS = 1000; +const WAIT_FOR_EXPIRY_TIMEOUT_MS = 20_000; interface RehearsalResources { auctionIds: mongoose.Types.ObjectId[]; @@ -345,6 +346,27 @@ async function flushDurability( } } +async function waitForDeadlineCompletion( + deadlineScheduler: AuctionDeadlineScheduler, +): Promise { + const startedAt = Date.now(); + let lastResult: Awaited< + ReturnType + > | null = null; + + while (Date.now() - startedAt < WAIT_FOR_EXPIRY_TIMEOUT_MS) { + await new Promise((resolve) => + setTimeout(resolve, WAIT_FOR_EXPIRY_POLL_INTERVAL_MS), + ); + lastResult = await deadlineScheduler.processDueBidRoundDeadlines(20); + if (lastResult.completed >= 1) return; + } + + throw new Error( + `Expected deadline completion, got ${JSON.stringify(lastResult)}`, + ); +} + function assertState( state: Record | null, expectedPhase: string, @@ -508,14 +530,29 @@ async function main(): Promise { auctionActionRequestId: randomUUID(), }); await flushDurability(durabilityScheduler); - logStep(`waiting ${WAIT_FOR_EXPIRY_MS}ms for Redis TIME deadline`); - await new Promise((resolve) => setTimeout(resolve, WAIT_FOR_EXPIRY_MS)); - const deadlineResult = await deadlineScheduler.processDueBidRoundDeadlines(20); - if (deadlineResult.completed < 1) { + const secondBiddingState = assertState( + await auctionRedisStateReaderService.readAuctionStateView( + resources.matchId.toString(), + ), + 'bidding', + ); + const secondBiddingKeys = buildAuctionRedisKeys(resources.matchId.toString()); + const queuedDeadlineScore = await auctionRedisService.raw().sendCommand([ + 'ZSCORE', + secondBiddingKeys.deadlineQueueKey, + resources.matchId.toString(), + ]); + if (!queuedDeadlineScore) { throw new Error( - `Expected deadline completion, got ${JSON.stringify(deadlineResult)}`, + `Expected deadline queue item after second open-bidding, state deadline=${String( + secondBiddingState.currentBidRoundDeadlineTimestampMS, + )}`, ); } + logStep( + `waiting up to ${WAIT_FOR_EXPIRY_TIMEOUT_MS}ms for Redis TIME deadline`, + ); + await waitForDeadlineCompletion(deadlineScheduler); await flushDurability(durabilityScheduler); assertState( await auctionRedisStateReaderService.readAuctionStateView( @@ -546,13 +583,15 @@ async function main(): Promise { matchId: resources.matchId, }); await flushDurability(durabilityScheduler); - assertState( - await auctionRedisStateReaderService.readAuctionStateView( - resources.matchId.toString(), - ), - 'waiting', + const resetState = await auctionRedisStateReaderService.readAuctionStateView( + resources.matchId.toString(), ); - logStep('reset -> waiting + side effect OK'); + if (resetState) { + throw new Error( + `Expected reset to clear Redis state, got phase=${String(resetState.phase)}`, + ); + } + logStep('reset -> auction data cleared OK'); const restartedAuction = await auctionService.start({ bidRoundExtensionSeconds: 1,