local time = redis.call('TIME')
local now = time[1] * 1000 + math.floor(time[2] / 1000)
if redis.call('HGET', KEYS[4], 'conflictState') == 'CONFLICT' then return {'CONTRACT_MISMATCH'} end
if redis.call('HGET', KEYS[4], 'schemaId') ~= ARGV[3] then return {'CONTRACT_MISMATCH'} end
local supported = redis.call('HGET', KEYS[4], 'wireCodecs') or ''
if not string.find(',' .. supported .. ',', ',' .. ARGV[4] .. ',', 1, true) then return {'CONTRACT_MISMATCH'} end
local candidates = redis.call('ZRANGEBYSCORE', KEYS[2], now, '+inf', 'LIMIT', 0, tonumber(ARGV[5]))
local members = {}
for _, executorId in ipairs(candidates) do
local executorKey = ARGV[6] .. 'executor:' .. executorId
local state = redis.call('HGET', executorKey, 'state')
local ready = redis.call('HGET', executorKey, 'fanoutReady')
local revision = redis.call('HGET', KEYS[3], executorId .. '|contractRevision')
local schema = redis.call('HGET', KEYS[3], executorId .. '|schemaId')
local codecs = redis.call('HGET', KEYS[3], executorId .. '|wireCodecs') or ''
if state == 'ACTIVE' and ready == 'true' and revision == ARGV[2] and schema == ARGV[3]
and string.find(',' .. codecs .. ',', ',' .. ARGV[4] .. ',', 1, true) then
members[#members + 1] = {
redis.call('HGET', KEYS[3], executorId .. '|nodeIdentity') or '', executorId,
redis.call('HGET', KEYS[3], executorId .. '|startupId') or '',
redis.call('HGET', KEYS[3], executorId .. '|applicationName') or '',
redis.call('HGET', KEYS[3], executorId .. '|runtime') or '',
redis.call('HGET', KEYS[3], executorId .. '|implementationDigest') or '',
redis.call('HGET', executorKey, 'heartbeatRevision') or '0'}
end
end
table.sort(members, function(left, right)
if left[1] == right[1] then return tonumber(left[7]) > tonumber(right[7]) end
return left[1] < right[1]
end)
local result = {'OK', tostring(now)}
local seenNodes = {}
for _, member in ipairs(members) do
if not seenNodes[member[1]] then
seenNodes[member[1]] = true
for index = 1, #member do result[#result + 1] = member[index] end
end
end
return result