upyun / lua-resty-checkups

Manage Nginx upstreams in pure Lua.
259 stars 65 forks source link

后端选择策略如果是consistent_hash时,若节点挂掉,无法及时failover到好的节点 #3

Closed xiewenhui closed 7 years ago

xiewenhui commented 7 years ago

后端cls选择节点策略如果是consistent_hash时, 若选到的节点突然挂掉,无法及时fail over到好的节点,会有部分502

qleein commented 7 years ago

@xiewenhui, I agree with you. In the function "next_consistent_hash_server" in file consistent_hash.lua, line 64.

    local circle = chash.circle
    local st = binary_search(circle, hash_string(hash_key))
    local ed = #circle
    for i = st, ed do  -- TODO: algorithm O(n)
        local idx = circle[i][2]
        if peer_cb(idx, servers[idx]) then
            return servers[idx]
        end
    end

When hash_key maps to the end of circle, as st equals ed after binary_search, and the node is down, It would never retry another node again. The action is not a hash ring, but a linear array.