Improve auction realtime bid responsiveness
This commit is contained in:
1 parent
c6f5a7077f
commit
b367328df0
16 files changed
+1364
-203
No files matched your search
@@ -6,6 +6,7 @@ import {
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { performance } from 'perf_hooks';
|
||||
import mongoose from 'mongoose';
|
||||
import { ConnectionService } from '../global/database/connection.service';
|
||||
import {
|
||||
@@ -25,7 +26,10 @@ import { RiotAccountEnrichmentService } from '../riot/riot-account-enrichment.se
|
||||
import type { RiotPlatformRoute, RiotRegionalRoute } from '../riot/riot.types';
|
||||
import { TournamentCodeService } from '../tournament-code/tournament-code.service';
|
||||
import { TournamentResultService } from '../tournament-result/tournament-result.service';
|
||||
import { AuctionRedisCommandService } from './redis/auction-redis-command.service';
|
||||
import {
|
||||
AuctionRedisCommandService,
|
||||
type AuctionBidCommandResult,
|
||||
} from './redis/auction-redis-command.service';
|
||||
import { buildAuctionRedisKeys } from './redis/auction-redis.keys';
|
||||
import {
|
||||
buildAuctionRedisLiveStatePatchFromStructure,
|
||||
@@ -87,6 +91,7 @@ interface AuctionStructureActionRequestParams {
|
||||
clientObservedCurrentBidRoundPlayerUserId?: string;
|
||||
clientObservedPhase?: string;
|
||||
clientObservedRevision?: number;
|
||||
shouldBroadcastStateAfterDurability?: boolean;
|
||||
}
|
||||
|
||||
interface MarkSoldParams extends AuctionStructureActionRequestParams {
|
||||
@@ -136,6 +141,30 @@ export interface AuctionHistoryView {
|
||||
|
||||
export type AuctionStateResponse = AuctionDocument | Record<string, unknown>;
|
||||
|
||||
export interface AuctionBidDeltaResponse {
|
||||
auctionBidHistoryEntry: Record<string, unknown>;
|
||||
auctionLifecycleId?: string;
|
||||
bidServerTiming?: AuctionBidServerTiming;
|
||||
bidRequestId: string;
|
||||
currentBidRoundDeadlineTimestampMS: number;
|
||||
currentHighestBidderTeamIndex: number;
|
||||
currentHighestBidPointAmount: number;
|
||||
currentServerTimestampMS: number;
|
||||
revision: number;
|
||||
}
|
||||
|
||||
export interface AuctionBidServerTiming {
|
||||
fallbackFullStatePath?: boolean;
|
||||
redisBidCommandDurationMS?: number;
|
||||
redisBidContextReadDurationMS?: number;
|
||||
serviceTotalDurationMS?: number;
|
||||
}
|
||||
|
||||
export interface AuctionBidRejectDiagnostics {
|
||||
currentBidRoundDeadlineTimestampMS?: number;
|
||||
currentServerTimestampMS?: number;
|
||||
}
|
||||
|
||||
interface AuctionEnrichmentTarget {
|
||||
userId: mongoose.Types.ObjectId;
|
||||
puuid: string;
|
||||
@@ -591,6 +620,23 @@ export class AuctionService {
|
||||
return auction;
|
||||
}
|
||||
|
||||
public async placeBidDelta(
|
||||
params: PlaceBidParams,
|
||||
): Promise<AuctionBidDeltaResponse | AuctionStateResponse> {
|
||||
this.assertValidBidPointAmount(params.amount);
|
||||
const redisBidDelta = await this.tryPlaceRedisBidDeltaFromRealtimeState(
|
||||
params,
|
||||
);
|
||||
if (redisBidDelta) return redisBidDelta;
|
||||
|
||||
const auction = await this.getByMatchId(params.matchId);
|
||||
this.assertLiveAuctionOperationSupported(auction);
|
||||
if (this.isRedisV1Auction(auction)) {
|
||||
return this.placeRedisBidDelta(auction, params);
|
||||
}
|
||||
return this.placeBid(params);
|
||||
}
|
||||
|
||||
private async openRedisBidding(
|
||||
auction: AuctionDocument,
|
||||
actionRequestParams: AuctionStructureActionRequestParams,
|
||||
@@ -627,6 +673,8 @@ export class AuctionService {
|
||||
},
|
||||
expectedPhase: 'presenting',
|
||||
liveStatePatch,
|
||||
shouldBroadcastStateAfterDurability:
|
||||
actionRequestParams.shouldBroadcastStateAfterDurability,
|
||||
state,
|
||||
structureInput,
|
||||
});
|
||||
@@ -636,6 +684,110 @@ export class AuctionService {
|
||||
auction: AuctionDocument,
|
||||
params: PlaceBidParams,
|
||||
): Promise<AuctionStateResponse> {
|
||||
const matchId = auction.matchId.toString();
|
||||
await this.executeRedisBid(auction, params);
|
||||
return this.requireRedisAuctionState(matchId);
|
||||
}
|
||||
|
||||
private async placeRedisBidDelta(
|
||||
auction: AuctionDocument,
|
||||
params: PlaceBidParams,
|
||||
): Promise<AuctionBidDeltaResponse> {
|
||||
const serviceStartedAtMS = performance.now();
|
||||
const { redisBidCommandDurationMS, result, state } =
|
||||
await this.executeRedisBid(auction, params);
|
||||
const bidDelta = this.buildBidDeltaFromRedisBidResult(state, result);
|
||||
bidDelta.bidServerTiming = this.roundBidServerTiming({
|
||||
fallbackFullStatePath: true,
|
||||
redisBidCommandDurationMS,
|
||||
serviceTotalDurationMS: performance.now() - serviceStartedAtMS,
|
||||
});
|
||||
this.logSlowBidTiming(auction.matchId.toString(), bidDelta.bidServerTiming);
|
||||
return bidDelta;
|
||||
}
|
||||
|
||||
private async tryPlaceRedisBidDeltaFromRealtimeState(
|
||||
params: PlaceBidParams,
|
||||
): Promise<AuctionBidDeltaResponse | null> {
|
||||
const serviceStartedAtMS = performance.now();
|
||||
const matchId = params.matchId.toString();
|
||||
const bidContextReadStartedAtMS = performance.now();
|
||||
const bidContext =
|
||||
await this.auctionRedisStateReaderService.readBidContext(
|
||||
matchId,
|
||||
params.teamIndex,
|
||||
);
|
||||
const redisBidContextReadDurationMS =
|
||||
performance.now() - bidContextReadStartedAtMS;
|
||||
if (!bidContext) return null;
|
||||
if (bidContext.phase !== 'bidding') {
|
||||
throw new BadRequestException('Auction is not in bidding phase.');
|
||||
}
|
||||
if (!bidContext.bidderTeamCaptainUserId) {
|
||||
throw new BadRequestException('Unknown team.');
|
||||
}
|
||||
|
||||
const auctionLifecycleId =
|
||||
params.clientObservedAuctionLifecycleId ?? bidContext.auctionLifecycleId;
|
||||
const currentBidRoundPlayerUserId =
|
||||
params.clientObservedCurrentBidRoundPlayerUserId ??
|
||||
bidContext.currentBidRoundPlayerUserId;
|
||||
if (!auctionLifecycleId || !currentBidRoundPlayerUserId) {
|
||||
throw new BadRequestException('Current bid round is not available.');
|
||||
}
|
||||
|
||||
const redisBidCommandStartedAtMS = performance.now();
|
||||
const result = await this.auctionRedisCommandService.placeBid(matchId, {
|
||||
bidPointAmount: params.amount,
|
||||
bidderTeamIndex: params.teamIndex,
|
||||
bidderUserId: bidContext.bidderTeamCaptainUserId,
|
||||
bidRequestId: params.bidRequestId ?? randomUUID(),
|
||||
clientObservedAuctionLifecycleId: auctionLifecycleId,
|
||||
clientObservedCurrentBidRoundPlayerUserId: currentBidRoundPlayerUserId,
|
||||
matchId,
|
||||
maximumAuctionTeamMemberCount: AUCTION_MAX_TEAM_SIZE,
|
||||
maximumStoredAuctionBidHistoryCount: AUCTION_MAX_STORED_BID_HISTORY_COUNT,
|
||||
maximumStoredBidRequestResultCount:
|
||||
AUCTION_MAX_STORED_BID_REQUEST_RESULT_COUNT,
|
||||
minimumBidPointReservePerRemainingAuctionTeamMemberSlot:
|
||||
AUCTION_MIN_BID_POINTS,
|
||||
minimumBidRequestResultReplayWindowMS:
|
||||
AUCTION_BID_REQUEST_REPLAY_WINDOW_MS,
|
||||
});
|
||||
const redisBidCommandDurationMS =
|
||||
performance.now() - redisBidCommandStartedAtMS;
|
||||
if ('rejected' in result) {
|
||||
const bidServerTiming = this.roundBidServerTiming({
|
||||
redisBidCommandDurationMS,
|
||||
redisBidContextReadDurationMS,
|
||||
serviceTotalDurationMS: performance.now() - serviceStartedAtMS,
|
||||
});
|
||||
this.logSlowBidTiming(matchId, bidServerTiming);
|
||||
throw this.createRedisBidRejectedException(result, bidServerTiming);
|
||||
}
|
||||
|
||||
const bidDelta = this.buildBidDeltaFromRedisBidResult(
|
||||
{ auctionLifecycleId: bidContext.auctionLifecycleId },
|
||||
result,
|
||||
);
|
||||
bidDelta.bidServerTiming = this.roundBidServerTiming({
|
||||
redisBidCommandDurationMS,
|
||||
redisBidContextReadDurationMS,
|
||||
serviceTotalDurationMS: performance.now() - serviceStartedAtMS,
|
||||
});
|
||||
this.logSlowBidTiming(matchId, bidDelta.bidServerTiming);
|
||||
return bidDelta;
|
||||
}
|
||||
|
||||
private async executeRedisBid(
|
||||
auction: AuctionDocument,
|
||||
params: PlaceBidParams,
|
||||
): Promise<{
|
||||
redisBidCommandDurationMS: number;
|
||||
result: Extract<AuctionBidCommandResult, { ok: true }>;
|
||||
state: Record<string, unknown>;
|
||||
}> {
|
||||
const serviceStartedAtMS = performance.now();
|
||||
const matchId = auction.matchId.toString();
|
||||
const state = await this.requireRedisAuctionState(matchId);
|
||||
if (state.phase !== 'bidding') {
|
||||
@@ -660,6 +812,7 @@ export class AuctionService {
|
||||
throw new BadRequestException('Current bid round is not available.');
|
||||
}
|
||||
|
||||
const redisBidCommandStartedAtMS = performance.now();
|
||||
const result = await this.auctionRedisCommandService.placeBid(matchId, {
|
||||
bidPointAmount: params.amount,
|
||||
bidderTeamIndex: params.teamIndex,
|
||||
@@ -677,11 +830,36 @@ export class AuctionService {
|
||||
minimumBidRequestResultReplayWindowMS:
|
||||
AUCTION_BID_REQUEST_REPLAY_WINDOW_MS,
|
||||
});
|
||||
const redisBidCommandDurationMS =
|
||||
performance.now() - redisBidCommandStartedAtMS;
|
||||
if ('rejected' in result) {
|
||||
throw new BadRequestException(this.formatRedisBidRejectCode(result.code));
|
||||
const bidServerTiming = this.roundBidServerTiming({
|
||||
fallbackFullStatePath: true,
|
||||
redisBidCommandDurationMS,
|
||||
serviceTotalDurationMS: performance.now() - serviceStartedAtMS,
|
||||
});
|
||||
this.logSlowBidTiming(matchId, bidServerTiming);
|
||||
throw this.createRedisBidRejectedException(result, bidServerTiming);
|
||||
}
|
||||
|
||||
return this.requireRedisAuctionState(matchId);
|
||||
return { redisBidCommandDurationMS, result, state };
|
||||
}
|
||||
|
||||
private buildBidDeltaFromRedisBidResult(
|
||||
state: Record<string, unknown>,
|
||||
result: Extract<AuctionBidCommandResult, { ok: true }>,
|
||||
): AuctionBidDeltaResponse {
|
||||
return {
|
||||
auctionBidHistoryEntry: result.auctionBidHistoryEntry,
|
||||
auctionLifecycleId: this.stringFromState(state.auctionLifecycleId),
|
||||
bidRequestId: result.bidRequestId,
|
||||
currentBidRoundDeadlineTimestampMS:
|
||||
result.currentBidRoundDeadlineTimestampMS,
|
||||
currentHighestBidderTeamIndex: result.currentHighestBidderTeamIndex,
|
||||
currentHighestBidPointAmount: result.currentHighestBidPointAmount,
|
||||
currentServerTimestampMS: result.currentServerTimestampMS,
|
||||
revision: result.revision,
|
||||
};
|
||||
}
|
||||
|
||||
private async markRedisSold(
|
||||
@@ -749,6 +927,8 @@ export class AuctionService {
|
||||
...buildAuctionRedisLiveStatePatchFromStructure(structureInput),
|
||||
phase: 'sold',
|
||||
},
|
||||
shouldBroadcastStateAfterDurability:
|
||||
params.shouldBroadcastStateAfterDurability,
|
||||
state,
|
||||
structureInput,
|
||||
});
|
||||
@@ -806,6 +986,8 @@ export class AuctionService {
|
||||
...buildAuctionRedisLiveStatePatchFromStructure(structureInput),
|
||||
phase: 'passed',
|
||||
},
|
||||
shouldBroadcastStateAfterDurability:
|
||||
actionRequestParams.shouldBroadcastStateAfterDurability,
|
||||
state,
|
||||
structureInput,
|
||||
});
|
||||
@@ -972,6 +1154,8 @@ export class AuctionService {
|
||||
currentHighestBidderTeamIndex: -1,
|
||||
phase: nextPhase,
|
||||
},
|
||||
shouldBroadcastStateAfterDurability:
|
||||
actionRequestParams.shouldBroadcastStateAfterDurability,
|
||||
state,
|
||||
structureInput,
|
||||
});
|
||||
@@ -1445,6 +1629,7 @@ export class AuctionService {
|
||||
historyMutation?: Record<string, unknown>;
|
||||
liveStatePatch: Record<string, string | number | boolean | null | undefined>;
|
||||
matchSideEffectMutation?: Record<string, unknown>;
|
||||
shouldBroadcastStateAfterDurability?: boolean;
|
||||
shouldResetBidRuntime?: boolean;
|
||||
state: Record<string, unknown>;
|
||||
structureInput: Parameters<typeof stringifyAuctionRedisStructureState>[0];
|
||||
@@ -1528,7 +1713,8 @@ export class AuctionService {
|
||||
expectedRevision,
|
||||
liveStatePatch: params.liveStatePatch,
|
||||
matchId,
|
||||
shouldBroadcastState: true,
|
||||
shouldBroadcastState:
|
||||
params.shouldBroadcastStateAfterDurability ?? true,
|
||||
shouldResetBidRuntime: params.shouldResetBidRuntime,
|
||||
actionRequest: params.actionRequest?.actionRequest,
|
||||
},
|
||||
@@ -1945,6 +2131,46 @@ export class AuctionService {
|
||||
}
|
||||
}
|
||||
|
||||
private createRedisBidRejectedException(
|
||||
result: Extract<AuctionBidCommandResult, { rejected: true }>,
|
||||
bidServerTiming?: AuctionBidServerTiming,
|
||||
): BadRequestException {
|
||||
const message = this.formatRedisBidRejectCode(result.code);
|
||||
const bidDiagnostics: AuctionBidRejectDiagnostics = {
|
||||
currentBidRoundDeadlineTimestampMS:
|
||||
result.currentBidRoundDeadlineTimestampMS,
|
||||
currentServerTimestampMS: result.currentServerTimestampMS,
|
||||
};
|
||||
return new BadRequestException({
|
||||
bidDiagnostics,
|
||||
...(bidServerTiming ? { bidServerTiming } : {}),
|
||||
code: result.code,
|
||||
message,
|
||||
});
|
||||
}
|
||||
|
||||
private roundBidServerTiming(
|
||||
timing: AuctionBidServerTiming,
|
||||
): AuctionBidServerTiming {
|
||||
return Object.fromEntries(
|
||||
Object.entries(timing).map(([key, value]) => [
|
||||
key,
|
||||
typeof value === 'number' ? Math.round(value * 10) / 10 : value,
|
||||
]),
|
||||
) as AuctionBidServerTiming;
|
||||
}
|
||||
|
||||
private logSlowBidTiming(
|
||||
matchId: string,
|
||||
timing: AuctionBidServerTiming,
|
||||
): void {
|
||||
const serviceTotalDurationMS = timing.serviceTotalDurationMS ?? 0;
|
||||
if (serviceTotalDurationMS < 250) return;
|
||||
this.logger.warn(
|
||||
`Auction bid latency match=${matchId} service=${serviceTotalDurationMS}ms context=${timing.redisBidContextReadDurationMS ?? 0}ms redisBid=${timing.redisBidCommandDurationMS ?? 0}ms fallback=${timing.fallbackFullStatePath ? '1' : '0'}`,
|
||||
);
|
||||
}
|
||||
|
||||
private applyTopTierPriority(
|
||||
order: AuctionPlayerSnapshot[],
|
||||
threshold: AuctionTopTierThreshold,
|
||||
|
||||
Reference in new issue
Block a user