mirror of
https://github.com/alibaba/higress.git
synced 2026-06-09 12:47:28 +08:00
opt(ai-load-balancer): update global least request lua script for ai-load-balancer (#2945)
This commit is contained in:
@@ -28,28 +28,16 @@ local function randomBool()
|
|||||||
return math.random() >= 0.5
|
return math.random() >= 0.5
|
||||||
end
|
end
|
||||||
|
|
||||||
local function is_healthy(addr)
|
|
||||||
for i = 4, #KEYS do
|
|
||||||
if addr == KEYS[i] then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
if redis.call('HEXISTS', hset_key, current_target) == 1 then
|
if redis.call('HEXISTS', hset_key, current_target) == 1 then
|
||||||
current_count = redis.call('HGET', hset_key, current_target)
|
current_count = redis.call('HGET', hset_key, current_target)
|
||||||
local hash = redis.call('HGETALL', hset_key)
|
for i = 4, #KEYS do
|
||||||
for i = 1, #hash, 2 do
|
if redis.call('HEXISTS', hset_key, KEYS[i]) == 1 then
|
||||||
local addr = hash[i]
|
local count = redis.call('HGET', hset_key, KEYS[i])
|
||||||
local count = hash[i+1]
|
|
||||||
if is_healthy(addr) then
|
|
||||||
if tonumber(count) < tonumber(current_count) then
|
if tonumber(count) < tonumber(current_count) then
|
||||||
current_target = addr
|
current_target = KEYS[i]
|
||||||
current_count = count
|
current_count = count
|
||||||
elseif count == current_count and randomBool() then
|
elseif count == current_count and randomBool() then
|
||||||
current_target = addr
|
current_target = KEYS[i]
|
||||||
current_count = count
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -121,17 +121,14 @@ if target == "" then
|
|||||||
target = default_target
|
target = default_target
|
||||||
if redis.call('HEXISTS', hset_key, target) == 1 then
|
if redis.call('HEXISTS', hset_key, target) == 1 then
|
||||||
current_count = redis.call('HGET', hset_key, target)
|
current_count = redis.call('HGET', hset_key, target)
|
||||||
local hash = redis.call('HGETALL', hset_key)
|
for i = 4, #KEYS do
|
||||||
for i = 1, #hash, 2 do
|
if redis.call('HEXISTS', hset_key, KEYS[i]) == 1 then
|
||||||
local addr = hash[i]
|
local count = redis.call('HGET', hset_key, KEYS[i])
|
||||||
local count = hash[i+1]
|
|
||||||
if is_healthy(addr) then
|
|
||||||
if tonumber(count) < tonumber(current_count) then
|
if tonumber(count) < tonumber(current_count) then
|
||||||
target = addr
|
target = KEYS[i]
|
||||||
current_count = count
|
current_count = count
|
||||||
elseif count == current_count and randomBool() then
|
elseif count == current_count and randomBool() then
|
||||||
target = addr
|
target = KEYS[i]
|
||||||
current_count = count
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user