326 lines
12 KiB
TypeScript
326 lines
12 KiB
TypeScript
import type { AuctionRedisKeys } from './auction-redis.keys';
|
|
import { defineAuctionRedisScript } from './auction-redis.scripts';
|
|
|
|
export type ApplyTransitionDeadlineInput =
|
|
| { mode: 'none' }
|
|
| { mode: 'remove' }
|
|
| { durationSeconds: number; mode: 'set-relative' };
|
|
|
|
export interface ApplyTransitionActionRequestInput {
|
|
observedProcessingLeaseVersion: number;
|
|
processingOwnerToken: string;
|
|
scopeKey: string;
|
|
}
|
|
|
|
export interface AuctionApplyTransitionScriptInput {
|
|
actionRequest?: ApplyTransitionActionRequestInput;
|
|
afterAuctionStructureStateJson: string;
|
|
auctionHistorySequence: number;
|
|
auctionLifecycleId: string;
|
|
auctionStateTransitionId: string;
|
|
broadcastStateRevision: number;
|
|
deadline: ApplyTransitionDeadlineInput;
|
|
durabilityPayloadJson: string;
|
|
expectedCurrentAuctionPlayerOrderIndex?: number;
|
|
expectedCurrentBidRoundPlayerUserId?: string;
|
|
expectedAuctionHistorySequence?: number;
|
|
expectedPhase: string;
|
|
expectedRevision: number;
|
|
liveStatePatch: Record<string, string | number | boolean | null | undefined>;
|
|
matchId: string;
|
|
shouldBroadcastState: boolean;
|
|
shouldResetBidRuntime?: boolean;
|
|
}
|
|
|
|
export interface AuctionApplyTransitionScriptInvocation {
|
|
keys: string[];
|
|
script: typeof AUCTION_APPLY_TRANSITION_SCRIPT;
|
|
values: string[];
|
|
}
|
|
|
|
function stringifyRedisValue(value: string | number | boolean | null): string {
|
|
if (typeof value === 'boolean') return value ? '1' : '0';
|
|
if (value === null) return '';
|
|
return String(value);
|
|
}
|
|
|
|
function flattenLiveStatePatch(
|
|
liveStatePatch: AuctionApplyTransitionScriptInput['liveStatePatch'],
|
|
): string[] {
|
|
return Object.entries(liveStatePatch)
|
|
.filter(([, value]) => value !== undefined)
|
|
.sort(([a], [b]) => a.localeCompare(b))
|
|
.flatMap(([key, value]) => [
|
|
key,
|
|
stringifyRedisValue(value as string | number | boolean | null),
|
|
]);
|
|
}
|
|
|
|
export function buildAuctionApplyTransitionScriptInvocation(
|
|
keys: AuctionRedisKeys,
|
|
input: AuctionApplyTransitionScriptInput,
|
|
): AuctionApplyTransitionScriptInvocation {
|
|
const liveStatePatchPairs = flattenLiveStatePatch(input.liveStatePatch);
|
|
const deadlineDurationSeconds =
|
|
input.deadline.mode === 'set-relative'
|
|
? String(input.deadline.durationSeconds)
|
|
: '0';
|
|
|
|
return {
|
|
keys: [
|
|
keys.liveStateKey,
|
|
keys.structureStateKey,
|
|
keys.appliedTransitionIdsKey,
|
|
keys.durabilityPayloadKey(input.auctionStateTransitionId),
|
|
keys.durabilityRetryQueueKey,
|
|
keys.actionRequestResultsKey,
|
|
keys.actionRequestResultOrderKey,
|
|
keys.bidHistoryKey,
|
|
keys.bidRequestResultsKey,
|
|
keys.bidRequestResultOrderKey,
|
|
keys.deadlineQueueKey,
|
|
],
|
|
script: AUCTION_APPLY_TRANSITION_SCRIPT,
|
|
values: [
|
|
input.matchId,
|
|
input.auctionLifecycleId,
|
|
input.auctionStateTransitionId,
|
|
String(input.expectedRevision),
|
|
String(
|
|
input.expectedAuctionHistorySequence ??
|
|
input.auctionHistorySequence - 1,
|
|
),
|
|
input.expectedPhase,
|
|
input.expectedCurrentBidRoundPlayerUserId ?? '',
|
|
input.expectedCurrentAuctionPlayerOrderIndex === undefined
|
|
? ''
|
|
: String(input.expectedCurrentAuctionPlayerOrderIndex),
|
|
input.afterAuctionStructureStateJson,
|
|
String(input.auctionHistorySequence),
|
|
input.durabilityPayloadJson,
|
|
input.shouldBroadcastState ? '1' : '0',
|
|
String(input.broadcastStateRevision),
|
|
input.deadline.mode,
|
|
deadlineDurationSeconds,
|
|
input.shouldResetBidRuntime ? '1' : '0',
|
|
input.actionRequest?.scopeKey ?? '',
|
|
input.actionRequest?.processingOwnerToken ?? '',
|
|
input.actionRequest?.observedProcessingLeaseVersion === undefined
|
|
? ''
|
|
: String(input.actionRequest.observedProcessingLeaseVersion),
|
|
String(liveStatePatchPairs.length / 2),
|
|
...liveStatePatchPairs,
|
|
],
|
|
};
|
|
}
|
|
|
|
export const AUCTION_APPLY_TRANSITION_SCRIPT = defineAuctionRedisScript(
|
|
'auction.applyTransition',
|
|
`
|
|
local liveAuctionStateKey = KEYS[1]
|
|
local auctionStructureStateKey = KEYS[2]
|
|
local appliedAuctionStateTransitionIdsKey = KEYS[3]
|
|
local durabilityPayloadKey = KEYS[4]
|
|
local durabilityRetryQueueKey = KEYS[5]
|
|
local auctionActionRequestResultsKey = KEYS[6]
|
|
local auctionActionRequestResultOrderKey = KEYS[7]
|
|
local auctionBidHistoryKey = KEYS[8]
|
|
local bidRequestResultsKey = KEYS[9]
|
|
local bidRequestResultOrderKey = KEYS[10]
|
|
local bidRoundDeadlineQueueKey = KEYS[11]
|
|
|
|
local matchId = ARGV[1]
|
|
local auctionLifecycleId = ARGV[2]
|
|
local auctionStateTransitionId = ARGV[3]
|
|
local expectedRevision = tonumber(ARGV[4])
|
|
local expectedAuctionHistorySequence = tonumber(ARGV[5])
|
|
local expectedPhase = ARGV[6]
|
|
local expectedCurrentBidRoundPlayerUserId = ARGV[7]
|
|
local expectedCurrentAuctionPlayerOrderIndex = ARGV[8]
|
|
local afterAuctionStructureStateJson = ARGV[9]
|
|
local auctionHistorySequence = tonumber(ARGV[10])
|
|
local durabilityPayloadJson = ARGV[11]
|
|
local shouldBroadcastState = ARGV[12]
|
|
local broadcastStateRevision = tonumber(ARGV[13])
|
|
local deadlineMode = ARGV[14]
|
|
local deadlineDurationSeconds = tonumber(ARGV[15])
|
|
local shouldResetBidRuntime = ARGV[16]
|
|
local actionRequestScopeKey = ARGV[17]
|
|
local processingOwnerToken = ARGV[18]
|
|
local observedProcessingLeaseVersion = ARGV[19]
|
|
local liveStatePatchPairCount = tonumber(ARGV[20])
|
|
|
|
local redisTime = redis.call('TIME')
|
|
local currentRedisTimestampMS = (tonumber(redisTime[1]) * 1000) + math.floor(tonumber(redisTime[2]) / 1000)
|
|
local validatedActionRequestScopeKey = ''
|
|
|
|
local function storeValidatedActionRequestRejection(code)
|
|
if validatedActionRequestScopeKey == '' then
|
|
return
|
|
end
|
|
local actionRequestPrefix = validatedActionRequestScopeKey .. ':'
|
|
redis.call(
|
|
'HSET',
|
|
auctionActionRequestResultsKey,
|
|
actionRequestPrefix .. 'status',
|
|
'rejected',
|
|
actionRequestPrefix .. 'rejectCode',
|
|
code,
|
|
actionRequestPrefix .. 'requiresRefetch',
|
|
'1',
|
|
actionRequestPrefix .. 'responseServerTimestampMS',
|
|
tostring(currentRedisTimestampMS)
|
|
)
|
|
end
|
|
|
|
local function reject(code)
|
|
storeValidatedActionRequestRejection(code)
|
|
return {'REJECTED', code, auctionStateTransitionId, tostring(currentRedisTimestampMS)}
|
|
end
|
|
|
|
if not liveStatePatchPairCount then
|
|
return reject('INVALID_PATCH_ARITY')
|
|
end
|
|
if #ARGV ~= 20 + (liveStatePatchPairCount * 2) then
|
|
return reject('INVALID_PATCH_ARITY')
|
|
end
|
|
if not durabilityPayloadJson or durabilityPayloadJson == '' then
|
|
return reject('MISSING_DURABILITY_PAYLOAD')
|
|
end
|
|
if shouldBroadcastState ~= '0' and shouldBroadcastState ~= '1' then
|
|
return reject('INVALID_SHOULD_BROADCAST_STATE')
|
|
end
|
|
if shouldBroadcastState == '1' and not broadcastStateRevision then
|
|
return reject('INVALID_BROADCAST_STATE_REVISION')
|
|
end
|
|
if deadlineMode ~= 'none' and deadlineMode ~= 'remove' and deadlineMode ~= 'set-relative' then
|
|
return reject('INVALID_DEADLINE_MODE')
|
|
end
|
|
if deadlineMode == 'set-relative' and (not deadlineDurationSeconds or deadlineDurationSeconds <= 0) then
|
|
return reject('INVALID_DEADLINE_DURATION')
|
|
end
|
|
|
|
if actionRequestScopeKey ~= '' then
|
|
local actionRequestPrefix = actionRequestScopeKey .. ':'
|
|
local storedTransitionId = redis.call('HGET', auctionActionRequestResultsKey, actionRequestPrefix .. 'auctionStateTransitionId')
|
|
if not storedTransitionId then
|
|
return reject('ACTION_REQUEST_NOT_RESERVED')
|
|
end
|
|
if storedTransitionId ~= auctionStateTransitionId then
|
|
return reject('ACTION_REQUEST_TRANSITION_MISMATCH')
|
|
end
|
|
local storedOwnerToken = redis.call('HGET', auctionActionRequestResultsKey, actionRequestPrefix .. 'processingOwnerToken')
|
|
local leaseExpiresAtTimestampMS = tonumber(redis.call('HGET', auctionActionRequestResultsKey, actionRequestPrefix .. 'processingLeaseExpiresAtTimestampMS') or '0')
|
|
if leaseExpiresAtTimestampMS <= currentRedisTimestampMS then
|
|
return reject('ACTION_REQUEST_LEASE_EXPIRED')
|
|
end
|
|
if storedOwnerToken ~= processingOwnerToken then
|
|
return reject('ACTION_REQUEST_IN_PROGRESS')
|
|
end
|
|
local storedLeaseVersion = redis.call('HGET', auctionActionRequestResultsKey, actionRequestPrefix .. 'processingLeaseVersion') or ''
|
|
if storedLeaseVersion ~= observedProcessingLeaseVersion then
|
|
return reject('ACTION_REQUEST_LEASE_VERSION_MISMATCH')
|
|
end
|
|
validatedActionRequestScopeKey = actionRequestScopeKey
|
|
end
|
|
|
|
local currentRevision = tonumber(redis.call('HGET', liveAuctionStateKey, 'revision') or '-1')
|
|
if currentRevision ~= expectedRevision then
|
|
return reject('CONFLICT')
|
|
end
|
|
local currentAuctionHistorySequence = tonumber(redis.call('HGET', liveAuctionStateKey, 'currentAuctionHistorySequence') or '-1')
|
|
if currentAuctionHistorySequence ~= expectedAuctionHistorySequence then
|
|
return reject('CONFLICT')
|
|
end
|
|
if redis.call('HGET', liveAuctionStateKey, 'auctionLifecycleId') ~= auctionLifecycleId then
|
|
return reject('CONFLICT')
|
|
end
|
|
if redis.call('HGET', liveAuctionStateKey, 'phase') ~= expectedPhase then
|
|
return reject('CONFLICT')
|
|
end
|
|
if expectedCurrentBidRoundPlayerUserId ~= '' and redis.call('HGET', liveAuctionStateKey, 'currentBidRoundPlayerUserId') ~= expectedCurrentBidRoundPlayerUserId then
|
|
return reject('CONFLICT')
|
|
end
|
|
if expectedCurrentAuctionPlayerOrderIndex ~= '' and redis.call('HGET', liveAuctionStateKey, 'currentAuctionPlayerOrderIndex') ~= expectedCurrentAuctionPlayerOrderIndex then
|
|
return reject('CONFLICT')
|
|
end
|
|
|
|
local transitionDedupeMember = auctionLifecycleId .. ':' .. auctionStateTransitionId
|
|
if redis.call('SISMEMBER', appliedAuctionStateTransitionIdsKey, transitionDedupeMember) == 1 then
|
|
return {'REPLAY', 'APPLIED', auctionStateTransitionId, tostring(currentRedisTimestampMS)}
|
|
end
|
|
|
|
local newRevision = redis.call('HINCRBY', liveAuctionStateKey, 'revision', 1)
|
|
redis.call('SET', auctionStructureStateKey, afterAuctionStructureStateJson)
|
|
|
|
local hsetArgs = {
|
|
liveAuctionStateKey,
|
|
'auctionLifecycleId',
|
|
auctionLifecycleId,
|
|
'currentAuctionHistorySequence',
|
|
tostring(auctionHistorySequence),
|
|
'lastAppliedAuctionStateTransitionId',
|
|
auctionStateTransitionId
|
|
}
|
|
local patchIndex = 21
|
|
for i = 1, liveStatePatchPairCount do
|
|
table.insert(hsetArgs, ARGV[patchIndex])
|
|
table.insert(hsetArgs, ARGV[patchIndex + 1])
|
|
patchIndex = patchIndex + 2
|
|
end
|
|
redis.call('HSET', unpack(hsetArgs))
|
|
|
|
if deadlineMode == 'set-relative' then
|
|
local currentBidRoundDeadlineTimestampMS = currentRedisTimestampMS + (deadlineDurationSeconds * 1000)
|
|
redis.call('HSET', liveAuctionStateKey, 'currentBidRoundDeadlineTimestampMS', tostring(currentBidRoundDeadlineTimestampMS))
|
|
redis.call('ZADD', bidRoundDeadlineQueueKey, currentBidRoundDeadlineTimestampMS, matchId)
|
|
elseif deadlineMode == 'remove' then
|
|
redis.call('ZREM', bidRoundDeadlineQueueKey, matchId)
|
|
end
|
|
|
|
if shouldResetBidRuntime == '1' then
|
|
redis.call('DEL', auctionBidHistoryKey, bidRequestResultsKey, bidRequestResultOrderKey)
|
|
end
|
|
|
|
redis.call('SADD', appliedAuctionStateTransitionIdsKey, transitionDedupeMember)
|
|
redis.call(
|
|
'HSET',
|
|
durabilityPayloadKey,
|
|
'payloadJson',
|
|
durabilityPayloadJson,
|
|
'preparedAtRedisTimestampMS',
|
|
tostring(currentRedisTimestampMS),
|
|
'committedAtRedisTimestampMS',
|
|
tostring(currentRedisTimestampMS),
|
|
'auctionLifecycleId',
|
|
auctionLifecycleId,
|
|
'revision',
|
|
tostring(newRevision),
|
|
'shouldBroadcastState',
|
|
shouldBroadcastState,
|
|
'broadcastStateRevision',
|
|
tostring(broadcastStateRevision)
|
|
)
|
|
redis.call('ZADD', durabilityRetryQueueKey, currentRedisTimestampMS, durabilityPayloadKey)
|
|
|
|
if actionRequestScopeKey ~= '' then
|
|
local actionRequestPrefix = actionRequestScopeKey .. ':'
|
|
redis.call(
|
|
'HSET',
|
|
auctionActionRequestResultsKey,
|
|
actionRequestPrefix .. 'status',
|
|
'applied',
|
|
actionRequestPrefix .. 'revision',
|
|
tostring(newRevision),
|
|
actionRequestPrefix .. 'requiresRefetch',
|
|
'1',
|
|
actionRequestPrefix .. 'responseServerTimestampMS',
|
|
tostring(currentRedisTimestampMS)
|
|
)
|
|
redis.call('RPUSH', auctionActionRequestResultOrderKey, actionRequestScopeKey)
|
|
end
|
|
|
|
return {'OK', auctionStateTransitionId, tostring(newRevision), tostring(currentRedisTimestampMS)}
|
|
`,
|
|
);
|