Skip to content
26 changes: 11 additions & 15 deletions apisix/healthcheck_manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -362,25 +362,21 @@ local function timer_create_checker()
goto continue
end

-- Reuse path: if a checker exists and the `checks` config is unchanged
-- (only the nodes changed), reconcile its targets in place instead of
-- rebuilding. Rebuilding leaves fetch_checker with no checker for the
-- rebuild window (traffic then skips health filtering) and discards the
-- accumulated health state. sync_checker_targets is the last condition so
-- it runs only when the checker is reuse-eligible; a partial failure makes
-- the guard false and falls through to the full rebuild below.
-- upstream.nodes is non-empty here (guaranteed by the 0-node guard above).
-- A nodes-only change can reuse the live checker. If updating its
-- targets fails, keep the old version so the working-pool timer
-- retries without losing health filtering or requiring route traffic.
local existing_checker = working_pool[resource_path]
if existing_checker and existing_checker.checker
and not existing_checker.checker.dead
and upstream.checks
and core.table.deep_eq(existing_checker.checks, upstream.checks)
and sync_checker_targets(existing_checker.checker, upstream) then
add_working_pool(resource_path, resource_ver, existing_checker.checker,
upstream.checks)
core.log.info("reused checker with incremental targets: ",
tostring(existing_checker.checker), " for resource: ",
resource_path, " and version: ", resource_ver)
and core.table.deep_eq(existing_checker.checks, upstream.checks) then
if sync_checker_targets(existing_checker.checker, upstream) then
add_working_pool(resource_path, resource_ver, existing_checker.checker,
upstream.checks)
core.log.info("reused checker with incremental targets: ",
tostring(existing_checker.checker), " for resource: ",
resource_path, " and version: ", resource_ver)
end
goto continue
end

Expand Down
204 changes: 204 additions & 0 deletions t/lib/healthcheck_retry.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
--
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements. See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You under the Apache License, Version 2.0
-- (the "License"); you may not use this file except in compliance with
-- the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local healthcheck = require("resty.healthcheck")
local new = healthcheck.new
local state = {
failures = 0, restore_failures = 0, created = 0, add_calls = 0, remove_calls = 0,
healthy_add_calls = 0, healthy_remove_calls = 0, stale_remove_calls = 0,
}

-- Fail the selected dictionary write. A staging failure rejects the proposed
-- change. A failed primary write can still commit the staged list.
local methods = getmetatable(ngx.shared["upstream-healthcheck"]).__index
local shm_set = methods.set
for _, operation in ipairs({ "set", "safe_set", "safe_add" }) do
local original = methods[operation]
methods[operation] = function(dict, key, value, ...)
if state.fail_write == key then
state.fail_write = nil
state.failures = state.failures + 1
if state.delete_on_failure then
shm_set(dict, key, nil)
end
if state.keep_primary_unavailable then
state.unavailable_primary = key
end
return nil, "no memory"
end
if state.unavailable_primary == key then
state.restore_failures = state.restore_failures + 1
return nil, "no memory"
end
return original(dict, key, value, ...)
end
end

function state.arm_list_write(checker)
local primary = checker.TARGET_LIST
local stage = checker.TARGET_LIST_STAGING
if stage and not state.fail_primary and ngx.shared["upstream-healthcheck"]:get(primary) then
state.fail_write = stage
else
state.fail_write = primary
end
end

healthcheck.new = function(...)
local checker, err = new(...)
if not checker then
return nil, err
end
state.created = state.created + 1
state.current = checker
for _, operation in ipairs({ "add", "remove" }) do
local method = checker[operation .. "_target"]
checker[operation .. "_target"] = function(self, ip, port, ...)
if port == 1980 then
local key = "healthy_" .. operation .. "_calls"
state[key] = state[key] + 1
end
if port == 1970 then
state[operation .. "_calls"] = state[operation .. "_calls"] + 1
end
if port == 1960 and operation == "remove" then
state.stale_remove_calls = state.stale_remove_calls + 1
end
local armed = state.armed == operation and port == (state.armed_port or 1970)
if armed then
local armed_port = state.armed_port or 1970
state.armed = nil
local list = healthcheck.get_target_list(self.name,
"upstream-healthcheck") or {}
for _, target in ipairs(list) do
if target.port == armed_port then
state.failed_purge_time = target.purge_time
end
end
state.arm_list_write(self)
end
local ok, method_err = method(self, ip, port, ...)
if armed then
state.update_committed = ok == true
state.republication_failed = state.restore_failures > 0
assert(not state.fail_write, "the selected target-list write did not run")
end
return ok, method_err
end
end
return checker
end

function state.wait_for(condition, timeout)
local deadline = ngx.now() + (timeout or 10)
while not condition() do
ngx.update_time()
if ngx.now() >= deadline then
return false
end
ngx.sleep(0.05)
end
return true
end

function state.put(interval, keep_dead, extra_port)
local nodes = { { host = "127.0.0.1", port = 1980, weight = 1 } }
if keep_dead then
nodes[#nodes + 1] = { host = "127.0.0.1", port = 1970, weight = 1 }
end
if extra_port then
nodes[#nodes + 1] = { host = "127.0.0.1", port = extra_port, weight = 1 }
end
local code = require("lib.test_admin").test("/apisix/admin/routes/1", ngx.HTTP_PUT, {
uri = state.uri,
upstream = {
type = "roundrobin", retries = 0, nodes = nodes,
checks = { active = {
type = "tcp", timeout = 1,
healthy = { interval = interval, successes = 1 },
unhealthy = { interval = 1, tcp_failures = 1 },
} },
},
})
assert(code < 300, code)
end

function state.seed()
-- The Admin API can return before this worker observes the route.
-- Stop sending requests once either upstream response proves a match.
local test = require("lib.test_admin").test
local code
assert(state.wait_for(function()
code = test(state.uri, ngx.HTTP_GET)
return code == 200 or code == 502
end), "route did not match, last status: " .. tostring(code))
end

function state.targets()
local targets, err = healthcheck.get_target_list("upstream#/apisix/routes/1",
"upstream-healthcheck")
return assert(targets, err)
end

function state.unhealthy()
return state.current
and state.current:get_target_status("127.0.0.1", 1970) == false
end

function state.prepare_nodes_update(extra_port)
state.put(1, true, extra_port)
state.seed()
assert(state.wait_for(function()
return state.unhealthy() and (not extra_port or
state.current:get_target_status("127.0.0.1", extra_port) == false)
end), "initial probes did not finish")

-- Slow down probes of healthy targets before the nodes-only change.
-- A probe must not repair a wrongly reset target during the retry.
state.put(60, true, extra_port)
assert(state.wait_for(function()
return state.created == 2 and state.unhealthy()
end), "the slow-probe checker was not ready")
state.delete_on_failure = true
return state.current
end

function state.shared_status(port)
local key = state.current.TARGET_STATE .. ":127.0.0.1:" .. port .. ":127.0.0.1"
return ngx.shared["upstream-healthcheck"]:get(key)
end

function state.removed()
local status, err
if state.current then
status, err = state.current:get_target_status("127.0.0.1", 1970)
end
return status == nil and err == "target not found"
end

function state.traffic(label, second_healthy)
local test = require("lib.test_admin").test
local healthy = 0
for _ = 1, 40 do
local code, _, body = test(state.uri, ngx.HTTP_GET)
if code == 200 and (body == "1980" or (second_healthy and body == second_healthy)) then
healthy = healthy + 1
end
end
ngx.say(label or "healthy responses: ", healthy)
end

return state
Loading
Loading