feat: redesign auction engine with redis sot
This commit is contained in:
1 parent
737702d78c
commit
e8bb20d988
75 files changed
+12737
-542
No files matched your search
+336
@@ -0,0 +1,336 @@
|
||||
interface MigrationCollection {
|
||||
find(filter: Record<string, unknown>): {
|
||||
limit(limit: number): AsyncIterable<AnyDoc>;
|
||||
};
|
||||
bulkWrite(
|
||||
writes: Array<{
|
||||
updateOne: {
|
||||
filter: { _id: unknown };
|
||||
update: { $set?: Record<string, unknown>; $unset?: Record<string, ''> };
|
||||
};
|
||||
}>,
|
||||
options: { ordered: boolean },
|
||||
): Promise<unknown>;
|
||||
}
|
||||
|
||||
interface MigrationDb {
|
||||
collection(name: string): MigrationCollection;
|
||||
}
|
||||
|
||||
interface MigrationOptions {
|
||||
dryRun?: boolean;
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
interface MigrationStats {
|
||||
changedAuctions: number;
|
||||
changedHistoryEntries: number;
|
||||
duplicateHistorySequences: number;
|
||||
scannedAuctions: number;
|
||||
scannedHistoryEntries: number;
|
||||
}
|
||||
|
||||
type AnyDoc = Record<string, unknown> & { _id: unknown };
|
||||
type UpdateDoc = { $set?: Record<string, unknown>; $unset?: Record<string, ''> };
|
||||
|
||||
const AUCTION_FIELD_RENAMES: Array<{
|
||||
from: string;
|
||||
to: string;
|
||||
transform?: (value: unknown) => unknown;
|
||||
}> = [
|
||||
{ from: 'teams', to: 'auctionTeams', transform: transformAuctionTeams },
|
||||
{ from: 'order', to: 'auctionPlayerOrder' },
|
||||
{ from: 'passedQueue', to: 'passedAuctionPlayerQueue' },
|
||||
{ from: 'auctionRound', to: 'currentAuctionRound' },
|
||||
{ from: 'currentIndex', to: 'currentAuctionPlayerOrderIndex' },
|
||||
{ from: 'bids', to: 'auctionBidHistory', transform: transformAuctionBidHistory },
|
||||
{ from: 'currentBid', to: 'currentHighestBidPointAmount' },
|
||||
{ from: 'currentBidderTeamIndex', to: 'currentHighestBidderTeamIndex' },
|
||||
{
|
||||
from: 'currentDeadline',
|
||||
to: 'currentBidRoundDeadlineTimestampMS',
|
||||
transform: toTimestampMS,
|
||||
},
|
||||
{ from: 'maxTimerSeconds', to: 'maximumBidRoundDurationSeconds' },
|
||||
{ from: 'bidExtensionSeconds', to: 'bidRoundExtensionSeconds' },
|
||||
{ from: 'initialPoints', to: 'initialTeamBidPointAmount' },
|
||||
{ from: 'topTierThreshold', to: 'topTierAuctionPriorityThreshold' },
|
||||
{
|
||||
from: 'manualTopTierUserIds',
|
||||
to: 'manualTopTierAuctionPriorityUserIds',
|
||||
},
|
||||
{
|
||||
from: 'startedAt',
|
||||
to: 'auctionStartedAtTimestampMS',
|
||||
transform: toTimestampMS,
|
||||
},
|
||||
{
|
||||
from: 'completedAt',
|
||||
to: 'auctionCompletedAtTimestampMS',
|
||||
transform: toTimestampMS,
|
||||
},
|
||||
];
|
||||
|
||||
export async function migrateAuctionRedisFieldRename(
|
||||
db: MigrationDb,
|
||||
options: MigrationOptions = {},
|
||||
): Promise<MigrationStats> {
|
||||
const dryRun = options.dryRun ?? true;
|
||||
const stats: MigrationStats = {
|
||||
changedAuctions: 0,
|
||||
changedHistoryEntries: 0,
|
||||
duplicateHistorySequences: 0,
|
||||
scannedAuctions: 0,
|
||||
scannedHistoryEntries: 0,
|
||||
};
|
||||
|
||||
stats.duplicateHistorySequences = await countDuplicateHistorySequences(
|
||||
db.collection('auctionHistory'),
|
||||
options.limit,
|
||||
);
|
||||
if (!dryRun && stats.duplicateHistorySequences > 0) {
|
||||
throw new Error(
|
||||
`Duplicate auctionHistory sequence entries found: ${stats.duplicateHistorySequences}`,
|
||||
);
|
||||
}
|
||||
|
||||
await migrateCollection(
|
||||
db.collection('auction'),
|
||||
options.limit,
|
||||
dryRun,
|
||||
stats,
|
||||
'auction',
|
||||
);
|
||||
await migrateCollection(
|
||||
db.collection('auctionHistory'),
|
||||
options.limit,
|
||||
dryRun,
|
||||
stats,
|
||||
'auctionHistory',
|
||||
);
|
||||
|
||||
return stats;
|
||||
}
|
||||
|
||||
async function migrateCollection(
|
||||
collection: MigrationCollection,
|
||||
limit: number | undefined,
|
||||
dryRun: boolean,
|
||||
stats: MigrationStats,
|
||||
kind: 'auction' | 'auctionHistory',
|
||||
) {
|
||||
const writes: Array<{
|
||||
updateOne: {
|
||||
filter: { _id: unknown };
|
||||
update: UpdateDoc;
|
||||
};
|
||||
}> = [];
|
||||
|
||||
for await (const doc of collection.find({}).limit(limit ?? 0)) {
|
||||
if (kind === 'auction') stats.scannedAuctions += 1;
|
||||
if (kind === 'auctionHistory') stats.scannedHistoryEntries += 1;
|
||||
|
||||
const update =
|
||||
kind === 'auction' ? migrateAuctionDoc(doc) : migrateHistoryDoc(doc);
|
||||
if (!update) continue;
|
||||
|
||||
if (kind === 'auction') stats.changedAuctions += 1;
|
||||
if (kind === 'auctionHistory') stats.changedHistoryEntries += 1;
|
||||
writes.push({
|
||||
updateOne: {
|
||||
filter: { _id: doc._id },
|
||||
update,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (!dryRun && writes.length > 0) {
|
||||
await collection.bulkWrite(writes, { ordered: false });
|
||||
}
|
||||
}
|
||||
|
||||
async function countDuplicateHistorySequences(
|
||||
collection: MigrationCollection,
|
||||
limit: number | undefined,
|
||||
): Promise<number> {
|
||||
const seen = new Set<string>();
|
||||
const duplicates = new Set<string>();
|
||||
for await (const doc of collection.find({}).limit(limit ?? 0)) {
|
||||
const auctionId = stringifyId(doc.auctionId);
|
||||
const sequence = typeof doc.sequence === 'number' ? doc.sequence : undefined;
|
||||
if (!auctionId || sequence === undefined) continue;
|
||||
|
||||
const key = `${auctionId}:${sequence}`;
|
||||
if (seen.has(key)) duplicates.add(key);
|
||||
seen.add(key);
|
||||
}
|
||||
return duplicates.size;
|
||||
}
|
||||
|
||||
function migrateAuctionDoc(doc: AnyDoc): UpdateDoc | null {
|
||||
const set: Record<string, unknown> = {};
|
||||
const unset: Record<string, ''> = {};
|
||||
|
||||
for (const rename of AUCTION_FIELD_RENAMES) {
|
||||
renameField(doc, rename.from, rename.to, set, unset, rename.transform);
|
||||
}
|
||||
|
||||
setDefault(doc, set, 'revision', 0);
|
||||
setDefault(doc, set, 'currentAuctionHistorySequence', 0);
|
||||
setDefault(doc, set, 'auctionEngineVersion', 'legacy-renamed');
|
||||
setDefault(doc, set, 'auctionLifecycleId', legacyAuctionLifecycleId(doc._id));
|
||||
setDefault(doc, set, 'matchSideEffectStatus', 'none');
|
||||
setDefault(doc, set, 'matchSideEffectRevision', 0);
|
||||
|
||||
return buildUpdate(set, unset);
|
||||
}
|
||||
|
||||
function migrateHistoryDoc(doc: AnyDoc): UpdateDoc | null {
|
||||
const set: Record<string, unknown> = {};
|
||||
const unset: Record<string, ''> = {};
|
||||
const auctionId = stringifyId(doc.auctionId);
|
||||
const sequence = typeof doc.sequence === 'number' ? doc.sequence : 0;
|
||||
|
||||
setDefault(
|
||||
doc,
|
||||
set,
|
||||
'auctionLifecycleId',
|
||||
auctionId ? legacyAuctionLifecycleId(auctionId) : undefined,
|
||||
);
|
||||
setDefault(
|
||||
doc,
|
||||
set,
|
||||
'auctionStateTransitionId',
|
||||
auctionId ? `legacy-history:${auctionId}:${sequence}` : undefined,
|
||||
);
|
||||
setDefault(doc, set, 'revision', sequence);
|
||||
setDefault(doc, set, 'historyMutation', { type: 'append' });
|
||||
setDefault(doc, set, 'matchSideEffectMutation', { type: 'none' });
|
||||
|
||||
const snapshotBefore = isRecord(doc.snapshotBefore)
|
||||
? migrateAuctionSnapshot(doc.snapshotBefore)
|
||||
: undefined;
|
||||
if (snapshotBefore?.changed) {
|
||||
set.snapshotBefore = snapshotBefore.value;
|
||||
}
|
||||
|
||||
return buildUpdate(set, unset);
|
||||
}
|
||||
|
||||
function migrateAuctionSnapshot(snapshot: Record<string, unknown>): {
|
||||
changed: boolean;
|
||||
value: Record<string, unknown>;
|
||||
} {
|
||||
const value = { ...snapshot };
|
||||
let changed = false;
|
||||
for (const rename of AUCTION_FIELD_RENAMES) {
|
||||
if (!(rename.from in value)) continue;
|
||||
if (!(rename.to in value)) {
|
||||
value[rename.to] = rename.transform
|
||||
? rename.transform(value[rename.from])
|
||||
: value[rename.from];
|
||||
}
|
||||
delete value[rename.from];
|
||||
changed = true;
|
||||
}
|
||||
return { changed, value };
|
||||
}
|
||||
|
||||
function renameField(
|
||||
doc: Record<string, unknown>,
|
||||
from: string,
|
||||
to: string,
|
||||
set: Record<string, unknown>,
|
||||
unset: Record<string, ''>,
|
||||
transform?: (value: unknown) => unknown,
|
||||
) {
|
||||
if (!(from in doc)) return;
|
||||
if (!(to in doc)) {
|
||||
set[to] = transform ? transform(doc[from]) : doc[from];
|
||||
}
|
||||
unset[from] = '';
|
||||
}
|
||||
|
||||
function setDefault(
|
||||
doc: Record<string, unknown>,
|
||||
set: Record<string, unknown>,
|
||||
key: string,
|
||||
value: unknown,
|
||||
) {
|
||||
if (value === undefined || key in doc || key in set) return;
|
||||
set[key] = value;
|
||||
}
|
||||
|
||||
function buildUpdate(
|
||||
set: Record<string, unknown>,
|
||||
unset: Record<string, ''>,
|
||||
): UpdateDoc | null {
|
||||
const update: UpdateDoc = {};
|
||||
if (Object.keys(set).length > 0) update.$set = set;
|
||||
if (Object.keys(unset).length > 0) update.$unset = unset;
|
||||
return update.$set || update.$unset ? update : null;
|
||||
}
|
||||
|
||||
function transformAuctionTeams(value: unknown): unknown[] {
|
||||
return asArray(value).map((team) => {
|
||||
const next = { ...team };
|
||||
if ('points' in next && !('remainingBidPointAmount' in next)) {
|
||||
next.remainingBidPointAmount = next.points;
|
||||
}
|
||||
delete next.points;
|
||||
next.members = asArray(next.members).map((member) => {
|
||||
const migratedMember = { ...member };
|
||||
if (
|
||||
'cost' in migratedMember &&
|
||||
!('memberAcquisitionBidPointAmount' in migratedMember)
|
||||
) {
|
||||
migratedMember.memberAcquisitionBidPointAmount = migratedMember.cost;
|
||||
}
|
||||
delete migratedMember.cost;
|
||||
return migratedMember;
|
||||
});
|
||||
return next;
|
||||
});
|
||||
}
|
||||
|
||||
function transformAuctionBidHistory(value: unknown): unknown[] {
|
||||
return asArray(value).map((bid) => {
|
||||
const next: Record<string, unknown> = {};
|
||||
if ('teamIndex' in bid) next.bidderTeamIndex = bid.teamIndex;
|
||||
if ('amount' in bid) next.bidPointAmount = bid.amount;
|
||||
if ('bidderUserId' in bid) next.bidderUserId = bid.bidderUserId;
|
||||
if ('timestamp' in bid) {
|
||||
next.bidPlacedAtTimestampMS = toTimestampMS(bid.timestamp);
|
||||
}
|
||||
return next;
|
||||
});
|
||||
}
|
||||
|
||||
function toTimestampMS(value: unknown): unknown {
|
||||
if (value instanceof Date) return value.getTime();
|
||||
if (typeof value === 'number') return value;
|
||||
if (typeof value === 'string') {
|
||||
const parsed = Date.parse(value);
|
||||
return Number.isNaN(parsed) ? value : parsed;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function legacyAuctionLifecycleId(value: unknown): string {
|
||||
return `legacy-auction:${stringifyId(value) ?? 'unknown'}`;
|
||||
}
|
||||
|
||||
function stringifyId(value: unknown): string | undefined {
|
||||
if (value === undefined || value === null) return undefined;
|
||||
return typeof value === 'string' ? value : String(value);
|
||||
}
|
||||
|
||||
function asArray(value: unknown): Record<string, unknown>[] {
|
||||
return Array.isArray(value)
|
||||
? value.filter(isRecord).map((item) => ({ ...item }))
|
||||
: [];
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return Boolean(value && typeof value === 'object' && !Array.isArray(value));
|
||||
}
|
||||
Reference in new issue
Block a user