if redis.call('HGET', KEYS[1], 'state') ~= 'CREATING' then return {'STATE_MISMATCH'} end
if redis.call('HGET', KEYS[1], 'snapshotId') ~= ARGV[1] then return {'CONFLICT'} end
local count = tonumber(ARGV[2])
local added = 0
local cursor = 5
for index = 1, count do
local shardKey = KEYS[index + 1]
local shardIndex = ARGV[cursor]
if redis.call('EXISTS', shardKey) == 0 then
redis.call('HSET', shardKey,
'fanoutId', ARGV[cursor + 1], 'rootRunId', ARGV[cursor + 2],
'schedulerQualifier', ARGV[3], 'protocolVersion', ARGV[4],
'snapshotId', ARGV[1], 'workerName', ARGV[cursor + 3],
'contractRevision', ARGV[cursor + 4], 'schemaId', ARGV[cursor + 5],
'wireCodec', ARGV[cursor + 6], 'shardIndex', shardIndex,
'shardTotal', ARGV[cursor + 7], 'seq', ARGV[cursor + 8],
'executionKey', ARGV[cursor + 9], 'targetNodeIdentity', ARGV[cursor + 10],
'targetStartupId', ARGV[cursor + 11], 'assignmentEpoch', 0,
'assignmentCount', 1, 'receiptRetryCount', 0, 'readyWakeupCount', 0,
'targetHeartbeatRevision', ARGV[cursor + 12],
'originExecutorId', ARGV[cursor + 13], 'parameterPayload', ARGV[cursor + 14],
'state', 'AWAITING_RECEIPT', 'attempt', 0)
added = added + 1
elseif redis.call('HGET', shardKey, 'shardIndex') ~= shardIndex then
return {'CONFLICT'}
end
cursor = cursor + 15
end
if added > 0 then redis.call('HINCRBY', KEYS[1], 'createdShardCount', added) end
return {'OK', tostring(added), redis.call('HGET', KEYS[1], 'createdShardCount')}