fix(auction): harden lifecycle and durable bid storage
Serialize lifecycle changes and make cancellation, deadlines, and match side effects recoverable and idempotent. Archive accepted bids durably, quarantine invalid payloads, and preserve compatibility with empty round IDs. Synchronize HTTP polling and socket state with scoped resync events. Add cancellation cleanup index migration and regression coverage. Validated with 744 API tests, 65 Redis integration tests, and 28 Mongo integration tests.
This commit is contained in:
1 parent
f2eec479d9
commit
f649490cd2
55 files changed
+5374
-522
No files matched your search
@@ -5,8 +5,16 @@ import {
|
||||
InternalServerErrorException,
|
||||
Logger,
|
||||
NotFoundException,
|
||||
ServiceUnavailableException,
|
||||
Optional,
|
||||
} from '@nestjs/common';
|
||||
import { randomUUID } from 'crypto';
|
||||
import {
|
||||
AuctionLifecycleLock,
|
||||
AuctionLifecycleBusyError,
|
||||
} from './auction-lifecycle-lock';
|
||||
import { AUCTION_OPTIONAL_PROJECTION_FIELDS } from './auction-projection';
|
||||
import { AuctionEventBusService } from './auction-event-bus.service';
|
||||
import { performance } from 'perf_hooks';
|
||||
import mongoose from 'mongoose';
|
||||
import { ConnectionService } from '../global/database/connection.service';
|
||||
@@ -58,8 +66,6 @@ const AUCTION_BID_REQUEST_REPLAY_WINDOW_MS = 5 * 60 * 1000;
|
||||
const AUCTION_MAX_STORED_ACTION_REQUEST_RESULT_COUNT = 500;
|
||||
const AUCTION_ACTION_REQUEST_REPLAY_WINDOW_MS = 5 * 60 * 1000;
|
||||
const AUCTION_ACTION_REQUEST_LEASE_DURATION_MS = 15 * 1000;
|
||||
const AUCTION_REHYDRATE_LOCK_TTL_MS = 5 * 1000;
|
||||
const AUCTION_REHYDRATE_LOCK_WAIT_MS = 50;
|
||||
const AUCTION_HISTORY_LIST_LIMIT = 100;
|
||||
const AUCTION_HISTORY_LIST_CACHE_TTL_MS = 2 * 1000;
|
||||
const AUCTION_HISTORY_LIST_CACHE_MAX_ENTRIES = 256;
|
||||
@@ -82,6 +88,7 @@ interface StartAuctionParams {
|
||||
}
|
||||
|
||||
interface PlaceBidParams {
|
||||
clientObservedBidRoundId?: string;
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
teamIndex: number;
|
||||
amount: number;
|
||||
@@ -160,6 +167,7 @@ interface AuctionHistoryListCacheEntry {
|
||||
export type AuctionStateResponse = AuctionDocument | Record<string, unknown>;
|
||||
|
||||
export interface AuctionBidDeltaResponse {
|
||||
currentBidRoundId?: string;
|
||||
auctionBidHistoryEntry: Record<string, unknown>;
|
||||
auctionLifecycleId?: string;
|
||||
bidServerTiming?: AuctionBidServerTiming;
|
||||
@@ -212,9 +220,20 @@ export class AuctionService {
|
||||
private readonly auctionRedisService: AuctionRedisService,
|
||||
private readonly auctionRedisCommandService: AuctionRedisCommandService,
|
||||
private readonly auctionRedisStateReaderService: AuctionRedisStateReaderService,
|
||||
@Optional()
|
||||
private readonly auctionEventBusService?: AuctionEventBusService,
|
||||
) {}
|
||||
|
||||
public async start(params: StartAuctionParams): Promise<AuctionDocument> {
|
||||
return new AuctionLifecycleLock(this.auctionRedisService).run(
|
||||
params.matchId.toString(),
|
||||
() => this.startLocked(params),
|
||||
);
|
||||
}
|
||||
|
||||
private async startLocked(
|
||||
params: StartAuctionParams,
|
||||
): Promise<AuctionDocument> {
|
||||
const MatchModel = await this.connectionService.getModel('match');
|
||||
const UserModel = await this.connectionService.getModel('user');
|
||||
const AuctionModel = await this.connectionService.getModel('auction');
|
||||
@@ -231,7 +250,7 @@ export class AuctionService {
|
||||
const existing = await AuctionModel.findOne({ matchId: params.matchId });
|
||||
if (existing) {
|
||||
if (existing.startAuctionRequestId === params.startAuctionRequestId) {
|
||||
return existing;
|
||||
return this.finishAuctionStartup(existing, match);
|
||||
}
|
||||
if (this.isRestartableResetRedisAuction(existing)) {
|
||||
await this.clearRedisAuctionRuntimeForLifecycle(
|
||||
@@ -249,6 +268,19 @@ export class AuctionService {
|
||||
}
|
||||
}
|
||||
|
||||
// No Mongo auction owns this match. Remove runtime left by an interrupted
|
||||
// cancellation before creating a new lifecycle (under the lifecycle lock).
|
||||
const orphanedRuntime =
|
||||
await this.auctionRedisStateReaderService.readLiveStateIdentity(
|
||||
params.matchId.toString(),
|
||||
);
|
||||
if (orphanedRuntime?.auctionLifecycleId) {
|
||||
await this.clearRedisAuctionRuntimeForLifecycle(
|
||||
params.matchId.toString(),
|
||||
orphanedRuntime.auctionLifecycleId,
|
||||
);
|
||||
}
|
||||
|
||||
const activeParticipants = this.applyAuctionCut(match);
|
||||
const desiredAuctionCount = activeParticipants.length;
|
||||
if (desiredAuctionCount < 10) {
|
||||
@@ -381,6 +413,7 @@ export class AuctionService {
|
||||
auctionStartedByUserId: params.actorUserId,
|
||||
matchSideEffectStatus: 'none',
|
||||
matchSideEffectRevision: 0,
|
||||
startupCompleted: false,
|
||||
teams: legacyTeams,
|
||||
order: prioritizedOrder,
|
||||
passedQueue: [],
|
||||
@@ -419,12 +452,12 @@ export class AuctionService {
|
||||
matchId: params.matchId,
|
||||
});
|
||||
if (raceWinner?.startAuctionRequestId === params.startAuctionRequestId) {
|
||||
return raceWinner;
|
||||
return this.finishAuctionStartup(raceWinner, match);
|
||||
}
|
||||
throw new BadRequestException('Auction already exists for this match.');
|
||||
}
|
||||
await this.recordSnapshot(auction, 'start');
|
||||
await this.auctionRedisCommandService.hydrateState(
|
||||
const hydration = await this.auctionRedisCommandService.hydrateState(
|
||||
params.matchId.toString(),
|
||||
{
|
||||
auctionHistorySequence: 1,
|
||||
@@ -437,9 +470,18 @@ export class AuctionService {
|
||||
structureStateJson,
|
||||
},
|
||||
);
|
||||
if ('rejected' in hydration)
|
||||
throw new ConflictException(
|
||||
`Auction initialization failed: ${hydration.code}.`,
|
||||
);
|
||||
|
||||
match.status = 'team-built';
|
||||
await match.save();
|
||||
auction.startupCompleted = true;
|
||||
await AuctionModel.updateOne(
|
||||
{ _id: auction._id },
|
||||
{ $set: { startupCompleted: true } },
|
||||
);
|
||||
this.enqueueBackgroundOrderEnrichment({
|
||||
auctionId: auction._id,
|
||||
targets: enrichTargets,
|
||||
@@ -450,6 +492,61 @@ export class AuctionService {
|
||||
return auction;
|
||||
}
|
||||
|
||||
private async finishAuctionStartup(
|
||||
auction: AuctionDocument,
|
||||
match: MatchDocument,
|
||||
): Promise<AuctionDocument> {
|
||||
// Documents predating the startup marker already completed the old path.
|
||||
if (auction.startupCompleted !== false) return auction;
|
||||
// Retry the original cut; recruiting may have changed since Mongo creation.
|
||||
const roster = new Set([
|
||||
...auction.teams.map((team) => team.captainUserId.toString()),
|
||||
...auction.order.map((player) => player.userId.toString()),
|
||||
]);
|
||||
const participants = new Set(
|
||||
match.participants.map((entry) => entry.userId.toString()),
|
||||
);
|
||||
if ([...roster].some((id) => !participants.has(id))) {
|
||||
throw new ConflictException(
|
||||
'Auction startup roster has changed. Cancel and restart the auction.',
|
||||
);
|
||||
}
|
||||
const History = await this.connectionService.getModel('auctionHistory');
|
||||
await History.updateOne(
|
||||
{ auctionId: auction._id, sequence: 1 },
|
||||
{
|
||||
$setOnInsert: {
|
||||
auctionId: auction._id,
|
||||
matchId: auction.matchId,
|
||||
sequence: 1,
|
||||
action: 'start',
|
||||
snapshotBefore: this.cloneJson(
|
||||
typeof auction.toObject === 'function'
|
||||
? auction.toObject()
|
||||
: auction,
|
||||
),
|
||||
takenAt: auction.startedAt ?? new Date(),
|
||||
},
|
||||
},
|
||||
{ upsert: true },
|
||||
);
|
||||
await this.hydrateRedisStateFromAuctionDocument(auction);
|
||||
if (match.status === 'recruiting') {
|
||||
match.participants.forEach((participant) => {
|
||||
participant.excluded = !roster.has(participant.userId.toString());
|
||||
});
|
||||
match.status = 'team-built';
|
||||
await match.save();
|
||||
}
|
||||
const AuctionModel = await this.connectionService.getModel('auction');
|
||||
await AuctionModel.updateOne(
|
||||
{ _id: auction._id },
|
||||
{ $set: { startupCompleted: true } },
|
||||
);
|
||||
auction.startupCompleted = true;
|
||||
return auction;
|
||||
}
|
||||
|
||||
public async getByMatchId(
|
||||
matchId: mongoose.Types.ObjectId,
|
||||
): Promise<AuctionDocument> {
|
||||
@@ -462,18 +559,46 @@ export class AuctionService {
|
||||
public async getStateByMatchId(
|
||||
matchId: mongoose.Types.ObjectId,
|
||||
): Promise<AuctionStateResponse> {
|
||||
const auction = await this.getByMatchId(matchId);
|
||||
for (let attempt = 0; ; attempt++) {
|
||||
try {
|
||||
return await this.getStateByMatchIdOnce(matchId);
|
||||
} catch (error) {
|
||||
if (!(error instanceof AuctionLifecycleBusyError)) throw error;
|
||||
if (attempt >= 4)
|
||||
throw new ServiceUnavailableException({
|
||||
code: 'AUCTION_STATE_RECOVERING',
|
||||
retryAfterMS: 500,
|
||||
message: '경매 상태를 복구 중입니다. 잠시 후 다시 시도해 주세요.',
|
||||
});
|
||||
await new Promise((resolve) => setTimeout(resolve, 50 * (attempt + 1)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async getStateByMatchIdOnce(
|
||||
matchId: mongoose.Types.ObjectId,
|
||||
): Promise<AuctionStateResponse> {
|
||||
let auction = await this.getByMatchId(matchId);
|
||||
if (!this.isRedisV1Auction(auction)) return auction;
|
||||
|
||||
if (auction.startupCompleted === false) {
|
||||
auction = await new AuctionLifecycleLock(this.auctionRedisService).run(
|
||||
matchId.toString(),
|
||||
async () => {
|
||||
const current = await this.getByMatchId(matchId);
|
||||
const MatchModel = await this.connectionService.getModel('match');
|
||||
const match = await MatchModel.findById(matchId);
|
||||
if (!match) throw new NotFoundException('Match not found.');
|
||||
return this.finishAuctionStartup(current, match);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
const redisState = await this.readRedisAuctionStateOrRehydrate(auction);
|
||||
if (!redisState) return auction;
|
||||
|
||||
const mongoState =
|
||||
typeof auction.toObject === 'function'
|
||||
? auction.toObject({ depopulate: true })
|
||||
: auction;
|
||||
return {
|
||||
...this.cloneJson(mongoState),
|
||||
...this.buildAuctionSnapshotFromRedisState(auction, redisState),
|
||||
...redisState,
|
||||
chatMessages: auction.chatMessages ?? [],
|
||||
};
|
||||
@@ -485,12 +610,15 @@ export class AuctionService {
|
||||
const matchId = auction.matchId.toString();
|
||||
const mongoRevision = auction.revision ?? 0;
|
||||
const mongoAuctionLifecycleId = auction.auctionLifecycleId;
|
||||
if (!mongoAuctionLifecycleId || auction.phase === 'complete') {
|
||||
if (!mongoAuctionLifecycleId) {
|
||||
return this.auctionRedisStateReaderService.readAuctionStateView(matchId);
|
||||
}
|
||||
|
||||
const liveIdentity =
|
||||
await this.auctionRedisStateReaderService.readLiveStateIdentity(matchId);
|
||||
// Completed runtimes expire after the retention period. Mongo remains the
|
||||
// archive; viewing it must not create an immortal Redis runtime again.
|
||||
if (!liveIdentity && auction.phase === 'complete') return null;
|
||||
const shouldHydrate = shouldHydrateRedisAuctionState({
|
||||
live: liveIdentity,
|
||||
mongo: {
|
||||
@@ -503,29 +631,32 @@ export class AuctionService {
|
||||
return this.auctionRedisStateReaderService.readAuctionStateView(matchId);
|
||||
}
|
||||
|
||||
const keys = buildAuctionRedisKeys(matchId);
|
||||
const lockToken = randomUUID();
|
||||
const lockAcquired = await this.auctionRedisService.acquireTokenLock(
|
||||
keys.rehydrateLockKey,
|
||||
lockToken,
|
||||
AUCTION_REHYDRATE_LOCK_TTL_MS,
|
||||
await new AuctionLifecycleLock(this.auctionRedisService).run(
|
||||
matchId,
|
||||
async () => {
|
||||
const currentAuction = await this.getByMatchId(auction.matchId);
|
||||
if (currentAuction.auctionLifecycleId !== mongoAuctionLifecycleId) {
|
||||
throw new ConflictException(
|
||||
'Auction lifecycle has changed. Refresh the auction.',
|
||||
);
|
||||
}
|
||||
const currentLive =
|
||||
await this.auctionRedisStateReaderService.readLiveStateIdentity(
|
||||
matchId,
|
||||
);
|
||||
if (
|
||||
currentLive?.auctionLifecycleId &&
|
||||
currentLive.auctionLifecycleId !== mongoAuctionLifecycleId
|
||||
) {
|
||||
await this.clearRedisAuctionRuntimeForLifecycle(
|
||||
matchId,
|
||||
currentLive.auctionLifecycleId,
|
||||
);
|
||||
}
|
||||
await this.hydrateRedisStateFromAuctionDocument(currentAuction);
|
||||
},
|
||||
);
|
||||
|
||||
if (lockAcquired) {
|
||||
try {
|
||||
await this.hydrateRedisStateFromAuctionDocument(auction);
|
||||
} finally {
|
||||
await this.auctionRedisService.releaseTokenLock(
|
||||
keys.rehydrateLockKey,
|
||||
lockToken,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
await new Promise((resolve) =>
|
||||
setTimeout(resolve, AUCTION_REHYDRATE_LOCK_WAIT_MS),
|
||||
);
|
||||
}
|
||||
|
||||
return this.auctionRedisStateReaderService.readAuctionStateView(matchId, {
|
||||
shouldForceAuctionStateResync: true,
|
||||
});
|
||||
@@ -814,6 +945,7 @@ export class AuctionService {
|
||||
bidderTeamIndex: params.teamIndex,
|
||||
bidderUserId: bidContext.bidderTeamCaptainUserId,
|
||||
bidRequestId: params.bidRequestId ?? randomUUID(),
|
||||
clientObservedBidRoundId: params.clientObservedBidRoundId,
|
||||
clientObservedAuctionLifecycleId: auctionLifecycleId,
|
||||
clientObservedCurrentBidRoundPlayerUserId: currentBidRoundPlayerUserId,
|
||||
matchId,
|
||||
@@ -890,6 +1022,7 @@ export class AuctionService {
|
||||
bidderTeamIndex: params.teamIndex,
|
||||
bidderUserId: this.stringFromState(team.captainUserId),
|
||||
bidRequestId: params.bidRequestId ?? randomUUID(),
|
||||
clientObservedBidRoundId: params.clientObservedBidRoundId,
|
||||
clientObservedAuctionLifecycleId: auctionLifecycleId,
|
||||
clientObservedCurrentBidRoundPlayerUserId: currentBidRoundPlayerUserId,
|
||||
matchId,
|
||||
@@ -923,6 +1056,10 @@ export class AuctionService {
|
||||
): AuctionBidDeltaResponse {
|
||||
return {
|
||||
auctionBidHistoryEntry: result.auctionBidHistoryEntry,
|
||||
currentBidRoundId:
|
||||
typeof result.auctionBidHistoryEntry.bidRoundId === 'string'
|
||||
? result.auctionBidHistoryEntry.bidRoundId
|
||||
: undefined,
|
||||
auctionLifecycleId: this.stringFromState(state.auctionLifecycleId),
|
||||
bidRequestId: result.bidRequestId,
|
||||
currentBidRoundDeadlineTimestampMS:
|
||||
@@ -1408,6 +1545,34 @@ export class AuctionService {
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
communityId: mongoose.Types.ObjectId;
|
||||
}): Promise<void> {
|
||||
let committed = false;
|
||||
try {
|
||||
await new AuctionLifecycleLock(this.auctionRedisService).run(
|
||||
params.matchId.toString(),
|
||||
() =>
|
||||
this.resetLocked(params, () => {
|
||||
committed = true;
|
||||
}),
|
||||
);
|
||||
} catch (error) {
|
||||
if (!committed) throw error;
|
||||
// A committed cancellation is successful even if lock renewal/teardown
|
||||
// fails afterward. Cleanup is durably queued in the same Mongo transaction.
|
||||
this.logger.warn(
|
||||
`Auction cancellation committed; post-commit coordination failed: ${String(error)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async resetLocked(
|
||||
params: {
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
communityId: mongoose.Types.ObjectId;
|
||||
clientObservedAuctionLifecycleId?: string;
|
||||
clientObservedRevision?: number;
|
||||
},
|
||||
onCommitted: () => void,
|
||||
): Promise<void> {
|
||||
const AuctionModel = await this.connectionService.getModel('auction');
|
||||
const AuctionHistoryModel =
|
||||
await this.connectionService.getModel('auctionHistory');
|
||||
@@ -1419,70 +1584,155 @@ export class AuctionService {
|
||||
if (!session) {
|
||||
throw new InternalServerErrorException('Failed to start DB session.');
|
||||
}
|
||||
let redisRuntimeToClear: {
|
||||
auctionLifecycleId?: string;
|
||||
matchId: string;
|
||||
} | null = null;
|
||||
try {
|
||||
session.startTransaction();
|
||||
const auction = await AuctionModel.findOne({
|
||||
matchId: params.matchId,
|
||||
}).session(session);
|
||||
if (!auction) throw new NotFoundException('Auction not found.');
|
||||
|
||||
const match = await MatchModel.findById(params.matchId).session(session);
|
||||
if (!match) throw new NotFoundException('Match not found.');
|
||||
if (!match.communityId.equals(params.communityId)) {
|
||||
throw new BadRequestException('Community mismatch.');
|
||||
}
|
||||
this.assertCompetitionLifecycleMutable(match, 'reset the auction');
|
||||
|
||||
if (this.isRedisV1Auction(auction)) {
|
||||
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 });
|
||||
if (!auction) {
|
||||
// 이미 커밋된 취소의 재전송이면 성공으로 응답하고 남은 Redis 정리만 재시도한다.
|
||||
const cancelledMatch = params.clientObservedAuctionLifecycleId
|
||||
? await MatchModel.findById(params.matchId).session(session)
|
||||
: null;
|
||||
if (
|
||||
!cancelledMatch ||
|
||||
cancelledMatch.lastCancelledAuctionLifecycleId !==
|
||||
params.clientObservedAuctionLifecycleId
|
||||
) {
|
||||
throw new NotFoundException('Auction not found.');
|
||||
}
|
||||
if (!cancelledMatch.communityId.equals(params.communityId)) {
|
||||
throw new BadRequestException('Community mismatch.');
|
||||
}
|
||||
cancelledMatch.auctionCancellationCleanupPending = true;
|
||||
cancelledMatch.auctionCancellationCleanupRetryAt = new Date();
|
||||
await cancelledMatch.save({ session });
|
||||
} else {
|
||||
await auction.deleteOne({ session });
|
||||
await AuctionHistoryModel.deleteMany({
|
||||
auctionId: auction._id,
|
||||
}).session(session);
|
||||
if (match.status === 'team-built') {
|
||||
await this.tournamentCodeService.releaseFromMatch(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 });
|
||||
if (
|
||||
this.isRedisV1Auction(auction) &&
|
||||
(!params.clientObservedAuctionLifecycleId ||
|
||||
params.clientObservedAuctionLifecycleId !==
|
||||
auction.auctionLifecycleId)
|
||||
) {
|
||||
throw new ConflictException(
|
||||
'Auction lifecycle has changed. Refresh before cancelling.',
|
||||
);
|
||||
}
|
||||
|
||||
const match = await MatchModel.findById(params.matchId).session(
|
||||
session,
|
||||
);
|
||||
if (!match) throw new NotFoundException('Match not found.');
|
||||
if (!match.communityId.equals(params.communityId)) {
|
||||
throw new BadRequestException('Community mismatch.');
|
||||
}
|
||||
this.assertCompetitionLifecycleMutable(match, 'reset the auction');
|
||||
|
||||
if (this.isRedisV1Auction(auction)) {
|
||||
// 같은 트랜잭션에서 취소 완료를 기록해, 재전송을 멱등 처리한다.
|
||||
match.lastCancelledAuctionLifecycleId = auction.auctionLifecycleId;
|
||||
match.auctionCancellationCleanupPending = true;
|
||||
match.auctionCancellationCleanupRetryAt = new Date();
|
||||
match.auctionCancellationCleanupAttempts = 0;
|
||||
match.auctionCancellationCleanupError = undefined;
|
||||
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(session);
|
||||
if (match.status === 'team-built') {
|
||||
await this.tournamentCodeService.releaseFromMatch(match, session);
|
||||
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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
await session.commitTransaction();
|
||||
onCommitted();
|
||||
} catch (err) {
|
||||
await session.abortTransaction();
|
||||
throw err;
|
||||
} finally {
|
||||
await session.endSession();
|
||||
}
|
||||
}
|
||||
|
||||
if (redisRuntimeToClear) {
|
||||
await this.clearRedisAuctionRuntimeForLifecycle(
|
||||
redisRuntimeToClear.matchId,
|
||||
redisRuntimeToClear.auctionLifecycleId,
|
||||
);
|
||||
public async retryPendingCancellations(batchLimit = 20): Promise<void> {
|
||||
const MatchModel = await this.connectionService.getModel('match');
|
||||
const pending = await MatchModel.find({
|
||||
auctionCancellationCleanupPending: true,
|
||||
auctionCancellationCleanupRetryAt: { $lte: new Date() },
|
||||
})
|
||||
.sort({ auctionCancellationCleanupRetryAt: 1 })
|
||||
.limit(batchLimit)
|
||||
.lean()
|
||||
.exec();
|
||||
for (const match of pending) {
|
||||
const lifecycle = match.lastCancelledAuctionLifecycleId;
|
||||
if (!lifecycle) continue;
|
||||
const filter = {
|
||||
_id: match._id,
|
||||
lastCancelledAuctionLifecycleId: lifecycle,
|
||||
};
|
||||
try {
|
||||
await new AuctionLifecycleLock(this.auctionRedisService).run(
|
||||
match._id.toString(),
|
||||
async () => {
|
||||
// Compare-and-delete in Redis also protects a newly started lifecycle.
|
||||
await this.clearRedisAuctionRuntimeForLifecycle(
|
||||
match._id.toString(),
|
||||
lifecycle,
|
||||
);
|
||||
const AuctionModel =
|
||||
await this.connectionService.getModel('auction');
|
||||
if (!(await AuctionModel.exists({ matchId: match._id }))) {
|
||||
this.auctionEventBusService?.broadcastReset(
|
||||
match._id.toString(),
|
||||
lifecycle,
|
||||
);
|
||||
}
|
||||
await MatchModel.updateOne(filter, {
|
||||
$set: { auctionCancellationCleanupPending: false },
|
||||
$unset: {
|
||||
auctionCancellationCleanupRetryAt: '',
|
||||
auctionCancellationCleanupError: '',
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
const attempts = (match.auctionCancellationCleanupAttempts ?? 0) + 1;
|
||||
await MatchModel.updateOne(filter, {
|
||||
$set: {
|
||||
auctionCancellationCleanupPending: true,
|
||||
auctionCancellationCleanupRetryAt: new Date(
|
||||
Date.now() + Math.min(60000, 1000 * 2 ** Math.min(attempts, 6)),
|
||||
),
|
||||
auctionCancellationCleanupError: String(error).slice(0, 2000),
|
||||
},
|
||||
$inc: { auctionCancellationCleanupAttempts: 1 },
|
||||
});
|
||||
this.logger.warn(
|
||||
`Auction cancellation cleanup will retry for ${match._id.toString()}: ${String(error)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1497,6 +1747,19 @@ export class AuctionService {
|
||||
communityId: mongoose.Types.ObjectId;
|
||||
sequence: number;
|
||||
}): Promise<AuctionStateResponse> {
|
||||
return new AuctionLifecycleLock(this.auctionRedisService).run(
|
||||
params.matchId.toString(),
|
||||
() => this.undoToLocked(params),
|
||||
);
|
||||
}
|
||||
|
||||
private async undoToLocked(
|
||||
params: AuctionStructureActionRequestParams & {
|
||||
matchId: mongoose.Types.ObjectId;
|
||||
communityId: mongoose.Types.ObjectId;
|
||||
sequence: number;
|
||||
},
|
||||
): Promise<AuctionStateResponse> {
|
||||
const AuctionModel = await this.connectionService.getModel('auction');
|
||||
const AuctionHistoryModel =
|
||||
await this.connectionService.getModel('auctionHistory');
|
||||
@@ -1508,6 +1771,7 @@ export class AuctionService {
|
||||
}
|
||||
|
||||
let redisUndoContext: {
|
||||
state?: Record<string, unknown>;
|
||||
actionRequestParams?: AuctionStructureActionRequestParams;
|
||||
auction: AuctionDocument;
|
||||
snapshot: Record<string, unknown>;
|
||||
@@ -1543,8 +1807,12 @@ export class AuctionService {
|
||||
throw new BadRequestException('Auction history snapshot is invalid.');
|
||||
}
|
||||
|
||||
const undoingCompletedAuction =
|
||||
Boolean(auction.completedAt) && !('completedAt' in snapshot);
|
||||
const liveState = this.isRedisV1Auction(auction)
|
||||
? await this.requireRedisAuctionState(params.matchId.toString())
|
||||
: undefined;
|
||||
const undoingCompletedAuction = liveState
|
||||
? liveState.phase === 'complete' && snapshot.phase !== 'complete'
|
||||
: Boolean(auction.completedAt) && !('completedAt' in snapshot);
|
||||
if (undoingCompletedAuction) {
|
||||
this.assertCompetitionLifecycleMutable(
|
||||
match,
|
||||
@@ -1553,6 +1821,7 @@ export class AuctionService {
|
||||
}
|
||||
if (this.isRedisV1Auction(auction)) {
|
||||
redisUndoContext = {
|
||||
state: liveState,
|
||||
auction,
|
||||
actionRequestParams: params,
|
||||
snapshot: snapshot,
|
||||
@@ -1562,8 +1831,8 @@ export class AuctionService {
|
||||
await session.commitTransaction();
|
||||
} else {
|
||||
if (undoingCompletedAuction) {
|
||||
await this.tournamentCodeService.releaseFromMatch(match);
|
||||
await this.tournamentResultService.cancelPolling(match);
|
||||
await this.tournamentCodeService.releaseFromMatch(match, session);
|
||||
await this.tournamentResultService.cancelPolling(match, session);
|
||||
match.status = 'team-built';
|
||||
match.result = undefined;
|
||||
await match.save({ session });
|
||||
@@ -1758,6 +2027,14 @@ export class AuctionService {
|
||||
currentAuctionHistorySequence: auctionHistorySequence,
|
||||
lastAppliedAuctionStateTransitionId: auctionStateTransitionId,
|
||||
revision: expectedRevision + 1,
|
||||
currentBidRoundId:
|
||||
params.deadline.mode === 'set-relative'
|
||||
? auctionStateTransitionId
|
||||
: params.state.currentBidRoundId,
|
||||
currentBidRoundDeadlineTimestampMS:
|
||||
params.deadline.mode === 'remove'
|
||||
? 0
|
||||
: params.state.currentBidRoundDeadlineTimestampMS,
|
||||
};
|
||||
const afterAuctionMongoProjection =
|
||||
this.buildAuctionMongoProjectionFromState(params.auction, afterState);
|
||||
@@ -1769,8 +2046,18 @@ export class AuctionService {
|
||||
if (matchSideEffectMutation.type !== 'none') {
|
||||
afterAuctionMongoProjection.matchSideEffectStatus = 'pending';
|
||||
afterAuctionMongoProjection.matchSideEffectRevision =
|
||||
(params.auction.matchSideEffectRevision ?? 0) + 1;
|
||||
Math.max(
|
||||
params.auction.matchSideEffectRevision ?? 0,
|
||||
this.numberFromState(params.state.matchSideEffectRevision),
|
||||
) + 1;
|
||||
afterAuctionMongoProjection.matchSideEffectError = undefined;
|
||||
afterAuctionMongoProjection.matchSideEffectTransitionId =
|
||||
auctionStateTransitionId;
|
||||
params.liveStatePatch.matchSideEffectTransitionId =
|
||||
auctionStateTransitionId;
|
||||
params.liveStatePatch.matchSideEffectStatus = 'pending';
|
||||
params.liveStatePatch.matchSideEffectRevision =
|
||||
afterAuctionMongoProjection.matchSideEffectRevision as number;
|
||||
}
|
||||
|
||||
const durabilityPayload = {
|
||||
@@ -1925,6 +2212,7 @@ export class AuctionService {
|
||||
}
|
||||
|
||||
private async undoRedisAuction(params: {
|
||||
state?: Record<string, unknown>;
|
||||
actionRequestParams?: AuctionStructureActionRequestParams;
|
||||
auction: AuctionDocument;
|
||||
snapshot: Record<string, unknown>;
|
||||
@@ -1932,7 +2220,8 @@ export class AuctionService {
|
||||
undoingCompletedAuction: boolean;
|
||||
}): Promise<AuctionStateResponse> {
|
||||
const matchId = params.auction.matchId.toString();
|
||||
const state = await this.requireRedisAuctionState(matchId);
|
||||
const state =
|
||||
params.state ?? (await this.requireRedisAuctionState(matchId));
|
||||
const actionRequest = await this.reserveRedisStructureActionRequest({
|
||||
action: 'undo',
|
||||
matchId,
|
||||
@@ -2118,8 +2407,11 @@ export class AuctionService {
|
||||
typeof auction.toObject === 'function'
|
||||
? (auction.toObject({ depopulate: true }) as Record<string, unknown>)
|
||||
: (auction as unknown as Record<string, unknown>);
|
||||
const snapshotBase = this.cloneJson(base);
|
||||
for (const field of AUCTION_OPTIONAL_PROJECTION_FIELDS)
|
||||
delete snapshotBase[field];
|
||||
return {
|
||||
...this.cloneJson(base),
|
||||
...snapshotBase,
|
||||
...this.buildAuctionMongoProjectionFromState(auction, state),
|
||||
_id: auction._id,
|
||||
matchId: auction.matchId,
|
||||
@@ -2145,6 +2437,7 @@ export class AuctionService {
|
||||
);
|
||||
const phase = this.stringFromState(state.phase) || 'presenting';
|
||||
const projection: Record<string, unknown> = {
|
||||
currentBidRoundId: state.currentBidRoundId || undefined,
|
||||
auctionBidHistory,
|
||||
auctionCompletedAtTimestampMS:
|
||||
phase === 'complete' &&
|
||||
@@ -2285,6 +2578,8 @@ export class AuctionService {
|
||||
|
||||
private formatRedisBidRejectCode(code: string): string {
|
||||
switch (code) {
|
||||
case 'BID_CLIENT_UPDATE_REQUIRED':
|
||||
return '경매 화면이 업데이트되었습니다. 새로고침한 뒤 다시 입찰해 주세요.';
|
||||
case 'PHASE':
|
||||
return 'Auction is not in bidding phase.';
|
||||
case 'STALE_BID_ROUND':
|
||||
@@ -2701,8 +2996,8 @@ export class AuctionService {
|
||||
match: MatchDocument,
|
||||
session: mongoose.ClientSession,
|
||||
): Promise<void> {
|
||||
await this.tournamentCodeService.releaseFromMatch(match);
|
||||
await this.tournamentResultService.cancelPolling(match);
|
||||
await this.tournamentCodeService.releaseFromMatch(match, session);
|
||||
await this.tournamentResultService.cancelPolling(match, session);
|
||||
match.status = 'recruiting';
|
||||
match.teams = [];
|
||||
match.tournamentCodeIds = [];
|
||||
@@ -2717,6 +3012,11 @@ export class AuctionService {
|
||||
}
|
||||
|
||||
private assertLiveAuctionOperationSupported(auction: AuctionDocument): void {
|
||||
if (auction.startupCompleted === false) {
|
||||
throw new ConflictException(
|
||||
'Auction startup is not complete. Refresh to resume initialization.',
|
||||
);
|
||||
}
|
||||
if (auction.auctionEngineVersion && !this.isRedisV1Auction(auction)) {
|
||||
throw new BadRequestException(
|
||||
'Legacy auction live operations are not supported.',
|
||||
@@ -2881,7 +3181,7 @@ export class AuctionService {
|
||||
auction.topTierThreshold ??
|
||||
'NONE',
|
||||
};
|
||||
await this.auctionRedisCommandService.hydrateState(
|
||||
const hydration = await this.auctionRedisCommandService.hydrateState(
|
||||
auction.matchId.toString(),
|
||||
{
|
||||
auctionHistorySequence: auction.currentAuctionHistorySequence ?? 0,
|
||||
@@ -2889,6 +3189,10 @@ export class AuctionService {
|
||||
auction.auctionLifecycleId ??
|
||||
`auction:${auction._id.toString()}:rehydrated`,
|
||||
liveStatePatch: {
|
||||
currentBidRoundId: auction.phase === 'bidding' ? randomUUID() : '',
|
||||
matchSideEffectTransitionId: auction.matchSideEffectTransitionId,
|
||||
matchSideEffectRevision: auction.matchSideEffectRevision,
|
||||
matchSideEffectStatus: auction.matchSideEffectStatus,
|
||||
...buildAuctionRedisLiveStatePatchFromStructure(structureInput),
|
||||
currentHighestBidPointAmount:
|
||||
auction.currentHighestBidPointAmount ?? auction.currentBid ?? 0,
|
||||
@@ -2905,6 +3209,10 @@ export class AuctionService {
|
||||
structureStateJson: stringifyAuctionRedisStructureState(structureInput),
|
||||
},
|
||||
);
|
||||
if ('rejected' in hydration)
|
||||
throw new ConflictException(
|
||||
`Auction recovery failed: ${hydration.code}.`,
|
||||
);
|
||||
}
|
||||
|
||||
private async clearRedisAuctionRuntimeForLifecycle(
|
||||
@@ -2925,10 +3233,10 @@ export class AuctionService {
|
||||
' end',
|
||||
'end',
|
||||
"redis.call('ZREM', KEYS[2], ARGV[1])",
|
||||
"redis.call('DEL', KEYS[1], KEYS[3], KEYS[4], KEYS[5], KEYS[6], KEYS[7], KEYS[8], KEYS[9], KEYS[10], KEYS[11])",
|
||||
"redis.call('DEL', KEYS[1], KEYS[3], KEYS[4], KEYS[5], KEYS[6], KEYS[7], KEYS[8], KEYS[9], KEYS[10], KEYS[11], KEYS[12])",
|
||||
'return 1',
|
||||
].join('\n'),
|
||||
'11',
|
||||
'12',
|
||||
keys.liveStateKey,
|
||||
keys.deadlineQueueKey,
|
||||
keys.structureStateKey,
|
||||
@@ -2940,6 +3248,7 @@ export class AuctionService {
|
||||
keys.actionRequestResultOrderKey,
|
||||
keys.rehydrateLockKey,
|
||||
keys.stateTransitionLockKey,
|
||||
keys.rejectedBidRequestResultOrderKey,
|
||||
matchId,
|
||||
expectedAuctionLifecycleId ?? '',
|
||||
]);
|
||||
|
||||
Reference in new issue
Block a user