local state = redis.call('HGET', KEYS[1], 'state')
if not state then
return 'NOTFOUND'
end
if redis.call('HGET', KEYS[1], 'group_id') ~= ARGV[2] then
return 'WRONGGROUP'
end
local operation_epoch = redis.call('HGET', KEYS[1], 'epoch')
if not operation_epoch or tonumber(operation_epoch) ~= tonumber(ARGV[3]) then
return 'OPSTALE:' .. (operation_epoch or '0')
end
if state == 'COMPLETE' or state == 'FAILED' or state == 'ABORTED' then
return 'OK:' .. state
end
local current_epoch = redis.call('HGET', KEYS[3], 'epoch')
if not current_epoch or tonumber(current_epoch) ~= tonumber(ARGV[3]) then
return 'STALE:' .. (current_epoch or '0')
end
if redis.call('HGET', KEYS[3], 'state') ~= 'READY' then
return 'NOTREADY'
end
local admitted = false
local participants = redis.call('HVALS', KEYS[4])
for i = 1, #participants do
local worker_id, role, index, joined_epoch = parse_participant(participants[i])
if not worker_id or tonumber(joined_epoch) ~= tonumber(ARGV[3])
or redis.call('EXISTS', 'mx:refit:worker:' .. worker_id) ~= 1 then
return 'NOTREADY'
end
if worker_id == ARGV[4] then
admitted = true
end
end
if not admitted then
return 'NOTADMITTED'
end
redis.call('SADD', KEYS[2], ARGV[4])
if ARGV[5] == '0' then
redis.call('HSET', KEYS[1], 'state', 'FAILED', 'failure_message', ARGV[6])
redis.call('SREM', KEYS[5], ARGV[1])
local next_epoch = tonumber(current_epoch) + 1
redis.call('HSET', KEYS[3],
'epoch', next_epoch,
'state', 'FORMING',
'plan_source_worker_id', '',
'plan_source_endpoint', '',
'plan_source_digest', '')
for i = 6, #KEYS do
redis.call('DEL', KEYS[i])
end
return 'OK:FAILED'
end
local reported = redis.call('SCARD', KEYS[2])
local expected = tonumber(redis.call('HGET', KEYS[3], 'expected_total'))
if expected and reported == expected then
redis.call('HSET', KEYS[1], 'state', 'COMPLETE')
redis.call('SREM', KEYS[5], ARGV[1])
return 'OK:COMPLETE'
end
redis.call('HSET', KEYS[1], 'state', 'RUNNING')
return 'OK:RUNNING'