File size: 1,746 Bytes
2ebf97a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
--[[
  lock_key.lua  v4
  ─────────────────────────────────────────────────────────────────────────────
  Called ONLY on upstream error (4xx / 5xx).
  Flags the failed model and checks whether other models on the same key remain.

  Rules:
    - Set a per-model error flag (m{idx}:failed) so acquire_slot skips it.
    - Advance midx to the next model.
    - If at least one other model is NOT flagged failed → return "rotated"
      (key is still usable; balancer will retry immediately).
    - If ALL models on this key are flagged failed → lock the key for lockTtl.
      The key unlocks automatically when the TTL expires.

  KEYS[1] = prefix
  ARGV[1] = keyId
  ARGV[2] = failed model index (0-based)
  ARGV[3] = total model count
  ARGV[4] = lock TTL in seconds

  Returns: "rotated" | "all_exhausted"
]]

local prefix      = KEYS[1]
local keyId       = ARGV[1]
local failedMIdx  = tonumber(ARGV[2])
local numModels   = tonumber(ARGV[3])
local lockTtl     = tonumber(ARGV[4])

local midxK   = prefix .. ":key:" .. keyId .. ":midx"
local lockK   = prefix .. ":key:" .. keyId .. ":lock"

local failedK = prefix .. ":key:" .. keyId .. ":m" .. failedMIdx .. ":failed"
redis.call("SET", failedK, "1")

local nextMIdx = (failedMIdx + 1) % numModels
redis.call("SET", midxK, tostring(nextMIdx))

for i = 0, numModels - 1 do
  if i ~= failedMIdx then
    local otherFailedK = prefix .. ":key:" .. keyId .. ":m" .. i .. ":failed"
    if redis.call("GET", otherFailedK) ~= "1" then
      return "rotated"
    end
  end
end

redis.call("SET", lockK, "1", "EX", lockTtl)
return "all_exhausted"