fix: recover stale auction durability history
This commit is contained in:
1 parent
37f1f4225d
commit
8567948084
2 files changed
+294
No files matched your search
@@ -242,6 +242,9 @@ export class AuctionDurabilityScheduler
|
||||
|
||||
try {
|
||||
session?.startTransaction();
|
||||
const historyMutation = parseAuctionHistoryMutation(
|
||||
payload.historyMutation,
|
||||
);
|
||||
if (typeof AuctionModel.findById === 'function') {
|
||||
const currentProjection =
|
||||
await readAuctionProjectionIdentityForMutation(
|
||||
@@ -252,6 +255,18 @@ export class AuctionDurabilityScheduler
|
||||
if (
|
||||
!shouldProcessAuctionDurabilityPayload(currentProjection, payload)
|
||||
) {
|
||||
// 뒤 payload가 먼저 projection revision을 올리면 앞 append payload가 stale이 된다.
|
||||
// 이때 projection/side effect는 건드리지 않고 undo용 history만 멱등 복구한다.
|
||||
if (
|
||||
historyMutation.type === 'append' &&
|
||||
currentProjection.auctionLifecycleId === payload.auctionLifecycleId
|
||||
) {
|
||||
await this.applyStaleAppendHistoryRecovery({
|
||||
AuctionHistoryModel,
|
||||
payload,
|
||||
session,
|
||||
});
|
||||
}
|
||||
if (session) await session.commitTransaction();
|
||||
return;
|
||||
}
|
||||
@@ -516,6 +531,75 @@ export class AuctionDurabilityScheduler
|
||||
}
|
||||
}
|
||||
|
||||
private async applyStaleAppendHistoryRecovery(params: {
|
||||
AuctionHistoryModel: {
|
||||
findOne?: (...args: never[]) => unknown;
|
||||
updateOne: (...args: never[]) => unknown;
|
||||
};
|
||||
payload: AuctionDurabilityPayload;
|
||||
session: mongoose.ClientSession | null | undefined;
|
||||
}): Promise<void> {
|
||||
const invalidated =
|
||||
await this.isAppendHistoryInvalidatedByLaterDestructiveMutation(params);
|
||||
if (invalidated) return;
|
||||
|
||||
const historyOperation = buildAuctionHistoryUpsert(params.payload).updateOne;
|
||||
await execMaybe(
|
||||
params.AuctionHistoryModel.updateOne(
|
||||
historyOperation.filter as never,
|
||||
historyOperation.update as never,
|
||||
{
|
||||
session: params.session,
|
||||
upsert: true,
|
||||
} as never,
|
||||
) as Promise<unknown> | { exec(): Promise<unknown> },
|
||||
);
|
||||
}
|
||||
|
||||
private async isAppendHistoryInvalidatedByLaterDestructiveMutation(params: {
|
||||
AuctionHistoryModel: {
|
||||
findOne?: (...args: never[]) => unknown;
|
||||
};
|
||||
payload: AuctionDurabilityPayload;
|
||||
session: mongoose.ClientSession | null | undefined;
|
||||
}): Promise<boolean> {
|
||||
if (!params.AuctionHistoryModel.findOne) return true;
|
||||
|
||||
// 늦게 도착한 append는 history gap을 메울 수 있지만,
|
||||
// 이후 undo/reset이 의도적으로 삭제한 sequence를 되살리면 안 된다.
|
||||
const query = params.AuctionHistoryModel.findOne({
|
||||
auctionId: params.payload.auctionId,
|
||||
sequence: { $gt: params.payload.auctionHistorySequence },
|
||||
$or: [
|
||||
{
|
||||
'historyMutation.targetHistorySequence': {
|
||||
$lt: params.payload.auctionHistorySequence,
|
||||
},
|
||||
'historyMutation.type': 'deleteAfterSequenceAndAppendUndo',
|
||||
},
|
||||
{
|
||||
'historyMutation.resetMode': { $ne: 'preserve-audit' },
|
||||
'historyMutation.type': 'reset',
|
||||
},
|
||||
],
|
||||
} as never);
|
||||
const scoped =
|
||||
query && typeof query === 'object' && 'session' in query
|
||||
? (query as { session: (...args: never[]) => unknown }).session(
|
||||
params.session as never,
|
||||
)
|
||||
: query;
|
||||
const leaned =
|
||||
scoped && typeof scoped === 'object' && 'lean' in scoped
|
||||
? (scoped as { lean: () => unknown }).lean()
|
||||
: scoped;
|
||||
const result =
|
||||
leaned && typeof leaned === 'object' && 'exec' in leaned
|
||||
? await (leaned as { exec: () => Promise<unknown> }).exec()
|
||||
: await Promise.resolve(leaned);
|
||||
return Boolean(result);
|
||||
}
|
||||
|
||||
private async applyResetMatchSideEffect(
|
||||
payload: AuctionDurabilityPayload,
|
||||
mutation: ParsedMatchSideEffectMutation,
|
||||
|
||||
Reference in new issue
Block a user