fix: fully cancel redis auctions

This commit is contained in:
dahyeon committed 2026-06-23 03:34:45 -07:00
1 parent e8bb20d988
commit 7c372f0392
3 files changed
+221 -107

No files matched your search

@@ -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 () => {
+112 -10
View File
@@ -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<void> {
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<void> {
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<typeof buildAuctionRedisKeys>,
expectedAuctionLifecycleId?: string,
): Promise<void> {
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<string, unknown>;
return payload.auctionLifecycleId === expectedAuctionLifecycleId;
} catch {
this.logger.warn(`Failed to parse auction durability payload ${payloadKey}.`);
return false;
}
}
private enqueueBackgroundOrderEnrichment(params: {