fix: close competition cutover integrity gaps
This commit is contained in:
1 parent
7ea2002975
commit
683d602d63
57 files changed
+4805
-1582
No files matched your search
@@ -14,6 +14,7 @@ import {
|
||||
} from './domain/competition-reversal';
|
||||
|
||||
type PersistenceId = string | mongoose.Types.ObjectId;
|
||||
const COMMAND_TRANSACTION_ATTEMPTS = 3;
|
||||
|
||||
export type CompetitionCommandActor =
|
||||
| { type: 'system' }
|
||||
@@ -211,283 +212,316 @@ export class CompetitionCommandService {
|
||||
throw invalidCommandError();
|
||||
}
|
||||
|
||||
const session = await this.connectionService.createSession();
|
||||
if (!session) {
|
||||
throw new CompetitionCommandError(
|
||||
'COMPETITION_TRANSACTION_UNAVAILABLE',
|
||||
'A MongoDB transaction is required for Competition commands.',
|
||||
500,
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
session.startTransaction();
|
||||
const [CompetitionModel, FactModel, OutboxModel, AssignmentModel] =
|
||||
await Promise.all([
|
||||
this.connectionService.getModel('competition'),
|
||||
this.connectionService.getModel('competitionFact'),
|
||||
this.connectionService.getModel('competitionOutbox'),
|
||||
this.connectionService.getModel('tournamentCodeAssignment'),
|
||||
]);
|
||||
const requestHash = requestHashFor(envelope);
|
||||
const commandId = commandIdFor(envelope);
|
||||
const receipt = await OutboxModel.findOne({
|
||||
competitionId: command.competitionId,
|
||||
idempotencyKey: command.idempotencyKey,
|
||||
})
|
||||
.session(session)
|
||||
.lean()
|
||||
.exec();
|
||||
if (receipt) {
|
||||
const replay = await this.replay(
|
||||
CompetitionModel,
|
||||
command.competitionId,
|
||||
requestHash,
|
||||
receipt,
|
||||
session,
|
||||
let lastRetryableError: unknown;
|
||||
for (
|
||||
let attempt = 1;
|
||||
attempt <= COMMAND_TRANSACTION_ATTEMPTS;
|
||||
attempt += 1
|
||||
) {
|
||||
const session = await this.connectionService.createSession();
|
||||
if (!session) {
|
||||
throw new CompetitionCommandError(
|
||||
'COMPETITION_TRANSACTION_UNAVAILABLE',
|
||||
'A MongoDB transaction is required for Competition commands.',
|
||||
500,
|
||||
);
|
||||
await session.commitTransaction();
|
||||
return replay;
|
||||
}
|
||||
|
||||
const competition = await CompetitionModel.findById(command.competitionId)
|
||||
.session(session)
|
||||
.lean()
|
||||
.exec();
|
||||
if (!competition) throw competitionNotFound(command.competitionId);
|
||||
assertExpectedRevision(competition.revision, command.expectedRevision);
|
||||
|
||||
const [existingFacts, assignments] = await Promise.all([
|
||||
FactModel.find({ competitionId: command.competitionId })
|
||||
.sort({ revision: 1, ordinalWithinRevision: 1 })
|
||||
try {
|
||||
session.startTransaction();
|
||||
const [CompetitionModel, FactModel, OutboxModel, AssignmentModel] =
|
||||
await Promise.all([
|
||||
this.connectionService.getModel('competition'),
|
||||
this.connectionService.getModel('competitionFact'),
|
||||
this.connectionService.getModel('competitionOutbox'),
|
||||
this.connectionService.getModel('tournamentCodeAssignment'),
|
||||
]);
|
||||
const requestHash = requestHashFor(envelope);
|
||||
const commandId = commandIdFor(envelope);
|
||||
const receipt = await OutboxModel.findOne({
|
||||
competitionId: command.competitionId,
|
||||
idempotencyKey: command.idempotencyKey,
|
||||
})
|
||||
.session(session)
|
||||
.lean()
|
||||
.exec(),
|
||||
AssignmentModel.find({ competitionId: command.competitionId })
|
||||
.session(session)
|
||||
.lean()
|
||||
.exec() as Promise<AssignmentRecord[]>,
|
||||
]);
|
||||
const input = reversalInput(
|
||||
competition,
|
||||
command,
|
||||
existingFacts,
|
||||
assignments,
|
||||
);
|
||||
const preview = assertReversalPreview(input, command.impactHash);
|
||||
const nextRevision = command.expectedRevision + 1;
|
||||
const recordedAt = new Date();
|
||||
const occurredAt = command.occurredAt ?? recordedAt;
|
||||
const rawFacts = buildReversalFacts({
|
||||
preview,
|
||||
facts: input.facts,
|
||||
competitionId: idString(command.competitionId),
|
||||
commandId,
|
||||
nextRevision,
|
||||
actor: toPlain(command.actor) as Readonly<Record<string, unknown>>,
|
||||
reason: command.reason.trim(),
|
||||
occurredAt,
|
||||
});
|
||||
const reversalFacts = rawFacts.map((rawFact) =>
|
||||
persistenceReversalFact(rawFact, command, commandId, requestHash),
|
||||
);
|
||||
const projection = projectCompetition({
|
||||
competitionId: idString(command.competitionId),
|
||||
revision: nextRevision,
|
||||
projectorVersion: competition.projectorVersion,
|
||||
teams: toPlain(competition.teams) as never,
|
||||
format: toPlain(competition.format) as never,
|
||||
seriesGraph: toPlain(competition.seriesGraph) as never,
|
||||
facts: [...existingFacts, ...reversalFacts].map(factForProjection),
|
||||
});
|
||||
|
||||
await FactModel.insertMany(reversalFacts, { session });
|
||||
const updated = await CompetitionModel.findOneAndUpdate(
|
||||
{ _id: command.competitionId, revision: command.expectedRevision },
|
||||
{
|
||||
$inc: { revision: 1 },
|
||||
$set: {
|
||||
projection,
|
||||
projectorVersion: competition.projectorVersion,
|
||||
phase: projection.phase,
|
||||
},
|
||||
},
|
||||
{ new: true, runValidators: true, session },
|
||||
)
|
||||
.lean()
|
||||
.exec();
|
||||
if (!updated) throw staleRevisionError(command.expectedRevision);
|
||||
|
||||
await this.applyReversalAssignments(
|
||||
command.competitionId,
|
||||
preview,
|
||||
assignments,
|
||||
commandId,
|
||||
recordedAt,
|
||||
session,
|
||||
);
|
||||
await OutboxModel.insertMany(
|
||||
[
|
||||
buildOutbox(
|
||||
.exec();
|
||||
if (receipt) {
|
||||
const replay = await this.replay(
|
||||
CompetitionModel,
|
||||
command.competitionId,
|
||||
envelope,
|
||||
commandId,
|
||||
requestHash,
|
||||
nextRevision,
|
||||
reversalFacts,
|
||||
projection,
|
||||
updated,
|
||||
recordedAt,
|
||||
),
|
||||
],
|
||||
{ session },
|
||||
);
|
||||
await session.commitTransaction();
|
||||
return {
|
||||
competition: updated,
|
||||
commandId,
|
||||
revision: nextRevision,
|
||||
replayed: false,
|
||||
};
|
||||
} catch (error) {
|
||||
await session.abortTransaction().catch(() => undefined);
|
||||
throw error;
|
||||
} finally {
|
||||
await session.endSession();
|
||||
receipt,
|
||||
session,
|
||||
);
|
||||
await session.commitTransaction();
|
||||
return replay;
|
||||
}
|
||||
|
||||
const competition = await CompetitionModel.findById(
|
||||
command.competitionId,
|
||||
)
|
||||
.session(session)
|
||||
.lean()
|
||||
.exec();
|
||||
if (!competition) throw competitionNotFound(command.competitionId);
|
||||
assertExpectedRevision(competition.revision, command.expectedRevision);
|
||||
|
||||
const [existingFacts, assignments] = await Promise.all([
|
||||
FactModel.find({ competitionId: command.competitionId })
|
||||
.sort({ revision: 1, ordinalWithinRevision: 1 })
|
||||
.session(session)
|
||||
.lean()
|
||||
.exec(),
|
||||
AssignmentModel.find({ competitionId: command.competitionId })
|
||||
.session(session)
|
||||
.lean()
|
||||
.exec() as Promise<AssignmentRecord[]>,
|
||||
]);
|
||||
const input = reversalInput(
|
||||
competition,
|
||||
command,
|
||||
existingFacts,
|
||||
assignments,
|
||||
);
|
||||
const preview = assertReversalPreview(input, command.impactHash);
|
||||
const nextRevision = command.expectedRevision + 1;
|
||||
const recordedAt = new Date();
|
||||
const occurredAt = command.occurredAt ?? recordedAt;
|
||||
const rawFacts = buildReversalFacts({
|
||||
preview,
|
||||
facts: input.facts,
|
||||
competitionId: idString(command.competitionId),
|
||||
commandId,
|
||||
nextRevision,
|
||||
actor: toPlain(command.actor) as Readonly<Record<string, unknown>>,
|
||||
reason: command.reason.trim(),
|
||||
occurredAt,
|
||||
});
|
||||
const reversalFacts = rawFacts.map((rawFact) =>
|
||||
persistenceReversalFact(rawFact, command, commandId, requestHash),
|
||||
);
|
||||
const projection = projectCompetition({
|
||||
competitionId: idString(command.competitionId),
|
||||
revision: nextRevision,
|
||||
projectorVersion: competition.projectorVersion,
|
||||
teams: toPlain(competition.teams) as never,
|
||||
format: toPlain(competition.format) as never,
|
||||
seriesGraph: toPlain(competition.seriesGraph) as never,
|
||||
facts: [...existingFacts, ...reversalFacts].map(factForProjection),
|
||||
});
|
||||
|
||||
await FactModel.insertMany(reversalFacts, { session });
|
||||
const updated = await CompetitionModel.findOneAndUpdate(
|
||||
{ _id: command.competitionId, revision: command.expectedRevision },
|
||||
{
|
||||
$inc: { revision: 1 },
|
||||
$set: {
|
||||
projection,
|
||||
projectorVersion: competition.projectorVersion,
|
||||
phase: projection.phase,
|
||||
},
|
||||
},
|
||||
{ new: true, runValidators: true, session },
|
||||
)
|
||||
.lean()
|
||||
.exec();
|
||||
if (!updated) throw staleRevisionError(command.expectedRevision);
|
||||
|
||||
await this.applyReversalAssignments(
|
||||
command.competitionId,
|
||||
competition.matchId,
|
||||
preview,
|
||||
assignments,
|
||||
commandId,
|
||||
recordedAt,
|
||||
session,
|
||||
);
|
||||
await OutboxModel.insertMany(
|
||||
[
|
||||
buildOutbox(
|
||||
command.competitionId,
|
||||
envelope,
|
||||
commandId,
|
||||
requestHash,
|
||||
nextRevision,
|
||||
reversalFacts,
|
||||
projection,
|
||||
updated,
|
||||
recordedAt,
|
||||
),
|
||||
],
|
||||
{ session },
|
||||
);
|
||||
await session.commitTransaction();
|
||||
return {
|
||||
competition: updated,
|
||||
commandId,
|
||||
revision: nextRevision,
|
||||
replayed: false,
|
||||
};
|
||||
} catch (error) {
|
||||
await session.abortTransaction().catch(() => undefined);
|
||||
if (
|
||||
attempt < COMMAND_TRANSACTION_ATTEMPTS &&
|
||||
isRetryableCommandTransactionError(error)
|
||||
) {
|
||||
lastRetryableError = error;
|
||||
continue;
|
||||
}
|
||||
throw error;
|
||||
} finally {
|
||||
await session.endSession();
|
||||
}
|
||||
}
|
||||
throw lastRetryableError;
|
||||
}
|
||||
|
||||
private async runCommand(
|
||||
command: CommandEnvelope,
|
||||
): Promise<CompetitionCommandResult> {
|
||||
validateEnvelope(command);
|
||||
const session = await this.connectionService.createSession();
|
||||
if (!session) {
|
||||
throw new CompetitionCommandError(
|
||||
'COMPETITION_TRANSACTION_UNAVAILABLE',
|
||||
'A MongoDB transaction is required for Competition commands.',
|
||||
500,
|
||||
);
|
||||
}
|
||||
let lastRetryableError: unknown;
|
||||
for (
|
||||
let attempt = 1;
|
||||
attempt <= COMMAND_TRANSACTION_ATTEMPTS;
|
||||
attempt += 1
|
||||
) {
|
||||
const session = await this.connectionService.createSession();
|
||||
if (!session) {
|
||||
throw new CompetitionCommandError(
|
||||
'COMPETITION_TRANSACTION_UNAVAILABLE',
|
||||
'A MongoDB transaction is required for Competition commands.',
|
||||
500,
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
session.startTransaction();
|
||||
const CompetitionModel =
|
||||
await this.connectionService.getModel('competition');
|
||||
const FactModel =
|
||||
await this.connectionService.getModel('competitionFact');
|
||||
const OutboxModel =
|
||||
await this.connectionService.getModel('competitionOutbox');
|
||||
const requestHash = requestHashFor(command);
|
||||
const commandId = commandIdFor(command);
|
||||
const competitionId = command.competitionId;
|
||||
try {
|
||||
session.startTransaction();
|
||||
const CompetitionModel =
|
||||
await this.connectionService.getModel('competition');
|
||||
const FactModel =
|
||||
await this.connectionService.getModel('competitionFact');
|
||||
const OutboxModel =
|
||||
await this.connectionService.getModel('competitionOutbox');
|
||||
const requestHash = requestHashFor(command);
|
||||
const commandId = commandIdFor(command);
|
||||
const competitionId = command.competitionId;
|
||||
|
||||
const receipt = await OutboxModel.findOne({
|
||||
competitionId,
|
||||
idempotencyKey: command.idempotencyKey,
|
||||
})
|
||||
.session(session)
|
||||
.lean()
|
||||
.exec();
|
||||
if (receipt) {
|
||||
const replay = await this.replay(
|
||||
CompetitionModel,
|
||||
const receipt = await OutboxModel.findOne({
|
||||
competitionId,
|
||||
idempotencyKey: command.idempotencyKey,
|
||||
})
|
||||
.session(session)
|
||||
.lean()
|
||||
.exec();
|
||||
if (receipt) {
|
||||
const replay = await this.replay(
|
||||
CompetitionModel,
|
||||
competitionId,
|
||||
requestHash,
|
||||
receipt,
|
||||
session,
|
||||
);
|
||||
await session.commitTransaction();
|
||||
return replay;
|
||||
}
|
||||
|
||||
const competition = await CompetitionModel.findById(competitionId)
|
||||
.session(session)
|
||||
.lean()
|
||||
.exec();
|
||||
if (!competition) {
|
||||
throw new CompetitionCommandError(
|
||||
'COMPETITION_NOT_FOUND',
|
||||
'Competition not found.',
|
||||
404,
|
||||
{ competitionId: idString(competitionId) },
|
||||
);
|
||||
}
|
||||
assertExpectedRevision(competition.revision, command.expectedRevision);
|
||||
|
||||
const existingFacts = await FactModel.find({ competitionId })
|
||||
.sort({ revision: 1, ordinalWithinRevision: 1 })
|
||||
.session(session)
|
||||
.lean()
|
||||
.exec();
|
||||
const nextRevision = command.expectedRevision + 1;
|
||||
const recordedAt = new Date();
|
||||
const fact = buildFact(
|
||||
command,
|
||||
commandId,
|
||||
requestHash,
|
||||
receipt,
|
||||
session,
|
||||
nextRevision,
|
||||
recordedAt,
|
||||
);
|
||||
const projection = projectCompetition({
|
||||
competitionId: idString(competitionId),
|
||||
revision: nextRevision,
|
||||
projectorVersion: competition.projectorVersion,
|
||||
teams: toPlain(competition.teams) as never,
|
||||
format: toPlain(competition.format) as never,
|
||||
seriesGraph: toPlain(competition.seriesGraph) as never,
|
||||
facts: [...existingFacts, fact].map(factForProjection),
|
||||
});
|
||||
|
||||
await FactModel.insertMany([fact], { session });
|
||||
const updated = await CompetitionModel.findOneAndUpdate(
|
||||
{ _id: competitionId, revision: command.expectedRevision },
|
||||
{
|
||||
$inc: { revision: 1 },
|
||||
$set: {
|
||||
projection,
|
||||
projectorVersion: competition.projectorVersion,
|
||||
phase: projection.phase,
|
||||
},
|
||||
},
|
||||
{ new: true, runValidators: true, session },
|
||||
)
|
||||
.lean()
|
||||
.exec();
|
||||
if (!updated) {
|
||||
throw staleRevisionError(command.expectedRevision);
|
||||
}
|
||||
|
||||
await this.applyTournamentCode(command, commandId, recordedAt, session);
|
||||
await OutboxModel.insertMany(
|
||||
[
|
||||
buildOutbox(
|
||||
competitionId,
|
||||
command,
|
||||
commandId,
|
||||
requestHash,
|
||||
nextRevision,
|
||||
fact,
|
||||
projection,
|
||||
updated,
|
||||
recordedAt,
|
||||
),
|
||||
],
|
||||
{ session },
|
||||
);
|
||||
await session.commitTransaction();
|
||||
return replay;
|
||||
return {
|
||||
competition: updated,
|
||||
commandId,
|
||||
revision: nextRevision,
|
||||
replayed: false,
|
||||
};
|
||||
} catch (error) {
|
||||
await session.abortTransaction().catch(() => undefined);
|
||||
if (
|
||||
attempt < COMMAND_TRANSACTION_ATTEMPTS &&
|
||||
isRetryableCommandTransactionError(error)
|
||||
) {
|
||||
lastRetryableError = error;
|
||||
continue;
|
||||
}
|
||||
throw error;
|
||||
} finally {
|
||||
await session.endSession();
|
||||
}
|
||||
|
||||
const competition = await CompetitionModel.findById(competitionId)
|
||||
.session(session)
|
||||
.lean()
|
||||
.exec();
|
||||
if (!competition) {
|
||||
throw new CompetitionCommandError(
|
||||
'COMPETITION_NOT_FOUND',
|
||||
'Competition not found.',
|
||||
404,
|
||||
{ competitionId: idString(competitionId) },
|
||||
);
|
||||
}
|
||||
assertExpectedRevision(competition.revision, command.expectedRevision);
|
||||
|
||||
const existingFacts = await FactModel.find({ competitionId })
|
||||
.sort({ revision: 1, ordinalWithinRevision: 1 })
|
||||
.session(session)
|
||||
.lean()
|
||||
.exec();
|
||||
const nextRevision = command.expectedRevision + 1;
|
||||
const recordedAt = new Date();
|
||||
const fact = buildFact(
|
||||
command,
|
||||
commandId,
|
||||
requestHash,
|
||||
nextRevision,
|
||||
recordedAt,
|
||||
);
|
||||
const projection = projectCompetition({
|
||||
competitionId: idString(competitionId),
|
||||
revision: nextRevision,
|
||||
projectorVersion: competition.projectorVersion,
|
||||
teams: toPlain(competition.teams) as never,
|
||||
format: toPlain(competition.format) as never,
|
||||
seriesGraph: toPlain(competition.seriesGraph) as never,
|
||||
facts: [...existingFacts, fact].map(factForProjection),
|
||||
});
|
||||
|
||||
await FactModel.insertMany([fact], { session });
|
||||
const updated = await CompetitionModel.findOneAndUpdate(
|
||||
{ _id: competitionId, revision: command.expectedRevision },
|
||||
{
|
||||
$inc: { revision: 1 },
|
||||
$set: {
|
||||
projection,
|
||||
projectorVersion: competition.projectorVersion,
|
||||
phase: projection.phase,
|
||||
},
|
||||
},
|
||||
{ new: true, runValidators: true, session },
|
||||
)
|
||||
.lean()
|
||||
.exec();
|
||||
if (!updated) {
|
||||
throw staleRevisionError(command.expectedRevision);
|
||||
}
|
||||
|
||||
await this.applyTournamentCode(command, commandId, recordedAt, session);
|
||||
await OutboxModel.insertMany(
|
||||
[
|
||||
buildOutbox(
|
||||
competitionId,
|
||||
command,
|
||||
commandId,
|
||||
requestHash,
|
||||
nextRevision,
|
||||
fact,
|
||||
projection,
|
||||
updated,
|
||||
recordedAt,
|
||||
),
|
||||
],
|
||||
{ session },
|
||||
);
|
||||
await session.commitTransaction();
|
||||
return {
|
||||
competition: updated,
|
||||
commandId,
|
||||
revision: nextRevision,
|
||||
replayed: false,
|
||||
};
|
||||
} catch (error) {
|
||||
await session.abortTransaction().catch(() => undefined);
|
||||
throw error;
|
||||
} finally {
|
||||
await session.endSession();
|
||||
}
|
||||
throw lastRetryableError;
|
||||
}
|
||||
|
||||
private async replay(
|
||||
@@ -595,6 +629,7 @@ export class CompetitionCommandService {
|
||||
|
||||
private async applyReversalAssignments(
|
||||
competitionId: PersistenceId,
|
||||
matchId: PersistenceId,
|
||||
preview: SeriesReversalPreview,
|
||||
assignments: AssignmentRecord[],
|
||||
commandId: string,
|
||||
@@ -638,6 +673,14 @@ export class CompetitionCommandService {
|
||||
tournamentCodeReversalStatePipeline(recordedAt),
|
||||
{ session },
|
||||
);
|
||||
const MatchModel = (await this.connectionService.getModel(
|
||||
'match',
|
||||
)) as mongoose.Model<any>;
|
||||
await MatchModel.updateOne(
|
||||
{ _id: matchId },
|
||||
{ $pull: { tournamentCodeIds: { $in: codeIds } } },
|
||||
{ session },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -956,6 +999,22 @@ function tournamentCodeReversalStatePipeline(
|
||||
releasedAt: {
|
||||
$cond: [{ $eq: ['$state', 'reserved'] }, recordedAt, '$releasedAt'],
|
||||
},
|
||||
matchId: {
|
||||
$cond: [{ $eq: ['$state', 'reserved'] }, '$$REMOVE', '$matchId'],
|
||||
},
|
||||
gameNumber: {
|
||||
$cond: [{ $eq: ['$state', 'reserved'] }, '$$REMOVE', '$gameNumber'],
|
||||
},
|
||||
bracketMatchId: {
|
||||
$cond: [
|
||||
{ $eq: ['$state', 'reserved'] },
|
||||
'$$REMOVE',
|
||||
'$bracketMatchId',
|
||||
],
|
||||
},
|
||||
reservedAt: {
|
||||
$cond: [{ $eq: ['$state', 'reserved'] }, '$$REMOVE', '$reservedAt'],
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
@@ -968,6 +1027,21 @@ function persistenceObjectId(value: PersistenceId): PersistenceId {
|
||||
: value;
|
||||
}
|
||||
|
||||
function isRetryableCommandTransactionError(error: unknown): boolean {
|
||||
const candidate = error as {
|
||||
code?: number;
|
||||
codeName?: string;
|
||||
hasErrorLabel?: (label: string) => boolean;
|
||||
};
|
||||
return Boolean(
|
||||
candidate?.code === 11_000 ||
|
||||
candidate?.code === 112 ||
|
||||
candidate?.codeName === 'WriteConflict' ||
|
||||
candidate?.hasErrorLabel?.('TransientTransactionError') ||
|
||||
candidate?.hasErrorLabel?.('UnknownTransactionCommitResult'),
|
||||
);
|
||||
}
|
||||
|
||||
function competitionNotFound(
|
||||
competitionId: PersistenceId,
|
||||
): CompetitionCommandError {
|
||||
|
||||
Reference in new issue
Block a user