diff --git a/lib/resty/healthcheck.lua b/lib/resty/healthcheck.lua index 89d1284a..9be05cd6 100644 --- a/lib/resty/healthcheck.lua +++ b/lib/resty/healthcheck.lua @@ -31,6 +31,7 @@ local ngx_log = ngx.log local tostring = tostring local ipairs = ipairs local cjson = require("cjson.safe").new() +local table_insert = table.insert local table_remove = table.remove local worker_events = require("resty.worker.events") local resty_lock = require ("resty.lock") @@ -281,6 +282,7 @@ end function checker:add_target(ip, port, hostname, is_healthy, hostheader) ip = tostring(assert(ip, "no ip address provided")) port = assert(tonumber(port), "no port number provided") + hostname = hostname or ip if is_healthy == nil then is_healthy = true end @@ -288,13 +290,17 @@ function checker:add_target(ip, port, hostname, is_healthy, hostheader) local internal_health = is_healthy and "healthy" or "unhealthy" local ok, err = locking_target_list(self, function(target_list) + local found = false -- check whether we already have this target for _, target in ipairs(target_list) do if target.ip == ip and target.port == port and target.hostname == hostname then self:log(DEBUG, "adding an existing target: ", hostname or "", " ", ip, ":", port, " (ignoring)") - return false + target.purge_time = nil + found = true + internal_health = self:get_target_status(ip, port, hostname) and + "healthy" or "unhealthy" end end @@ -308,12 +314,14 @@ function checker:add_target(ip, port, hostname, is_healthy, hostheader) end -- target does not exist, go add it - target_list[#target_list + 1] = { - ip = ip, - port = port, - hostname = hostname, - hostheader = hostheader, - } + if not found then + target_list[#target_list + 1] = { + ip = ip, + port = port, + hostname = hostname, + hostheader = hostheader, + } + end target_list = serialize(target_list) ok, err = self.shm:set(self.TARGET_LIST, target_list) @@ -433,6 +441,28 @@ function checker:clear() end +function checker:delayed_clear(delay) + assert(tonumber(delay), "no delay provided") + + return locking_target_list(self, function(target_list) + local purge_time = ngx_now() + delay + + -- add purge time to all targets + for _, target in ipairs(target_list) do + target.purge_time = purge_time + end + + target_list = serialize(target_list) + local ok, err = self.shm:set(self.TARGET_LIST, target_list) + if not ok then + return nil, "failed to store target_list in shm: " .. err + end + + return true + end) +end + + --- Get the current status of the target. -- @param ip IP address of the target being checked. -- @param port the port being checked against. @@ -629,7 +659,7 @@ function checker:report_failure(ip, port, hostname, check) ctr_type = CTR_HTTP end - return incr_counter(self, "unhealthy", ip, port, hostname, limit, ctr_type) + return incr_counter(self, "unhealthy", ip, port, hostname or ip, limit, ctr_type) end @@ -648,7 +678,7 @@ function checker:report_success(ip, port, hostname, check) local limit = self.checks[check or "passive"].healthy.successes - return incr_counter(self, "healthy", ip, port, hostname, limit, CTR_SUCCESS) + return incr_counter(self, "healthy", ip, port, hostname or ip, limit, CTR_SUCCESS) end @@ -686,7 +716,7 @@ function checker:report_http_status(ip, port, hostname, http_status, check) return end - return incr_counter(self, status_type, ip, port, hostname, limit, ctr) + return incr_counter(self, status_type, ip, port, hostname or ip, limit, ctr) end @@ -708,7 +738,7 @@ function checker:report_tcp_failure(ip, port, hostname, operation, check) local limit = self.checks[check or "passive"].unhealthy.tcp_failures -- TODO what do we do with the `operation` information - return incr_counter(self, "unhealthy", ip, port, hostname, limit, CTR_TCP) + return incr_counter(self, "unhealthy", ip, port, hostname or ip, limit, CTR_TCP) end @@ -725,7 +755,7 @@ function checker:report_timeout(ip, port, hostname, check) local limit = self.checks[check or "passive"].unhealthy.timeouts - return incr_counter(self, "unhealthy", ip, port, hostname, limit, CTR_TIMEOUT) + return incr_counter(self, "unhealthy", ip, port, hostname or ip, limit, CTR_TIMEOUT) end @@ -766,6 +796,7 @@ end function checker:set_target_status(ip, port, hostname, is_healthy) ip = tostring(assert(ip, "no ip address provided")) port = assert(tonumber(port), "no port number provided") + hostname = hostname or ip assert(type(is_healthy) == "boolean") local health_report = is_healthy and "healthy" or "unhealthy" @@ -1085,7 +1116,7 @@ function checker:event_handler(event_name, ip, port, hostname) then if not target_found then -- it is a new target, must add it first - target_found = { ip = ip, port = port, hostname = hostname } + target_found = { ip = ip, port = port, hostname = hostname or ip } self.targets[target_found.ip] = self.targets[target_found.ip] or {} self.targets[target_found.ip][target_found.port] = self.targets[target_found.ip][target_found.port] or {} self.targets[target_found.ip][target_found.port][target_found.hostname or ip] = target_found @@ -1126,7 +1157,7 @@ end -- Raises an event for a target status change. function checker:raise_event(event_name, ip, port, hostname) - local target = { ip = ip, port = port, hostname = hostname } + local target = { ip = ip, port = port, hostname = hostname or ip} worker_events.post(self.EVENT_SOURCE, event_name, target) end @@ -1451,9 +1482,8 @@ function _M.new(opts) return nil, err end - -- if active checker is needed and not running, start it - if (self.checks.active.healthy.active or self.checks.active.unhealthy.active) - and active_check_timer == nil then + -- if active checker is not running, start it + if active_check_timer == nil then self:log(DEBUG, "worker ", ngx_worker_id(), " (pid: ", ngx_worker_pid(), ") ", "starting active check timer") @@ -1475,6 +1505,29 @@ function _M.new(opts) local cur_time = ngx_now() for _, checker_obj in ipairs(hcs) do + -- clear targets marked for delayed removal + locking_target_list(checker_obj, function(target_list) + local removed_targets = {} + for i, target in ipairs(target_list) do + if target.purge_time and target.purge_time <= cur_time then + table_insert(removed_targets, target) + table_remove(target_list, i) + end + end + + target_list = serialize(target_list) + + local ok, err = shm:set(checker_obj.TARGET_LIST, target_list) + if not ok then + return nil, "failed to store target_list in shm: " .. err + end + + for _, target in ipairs(removed_targets) do + clear_target_data_from_shm(checker_obj, target.ip, target.port, target.hostname) + checker_obj:raise_event(checker_obj.events.remove, target.ip, target.port, target.hostname) + end + end) + if checker_obj.checks.active.healthy.active and (checker_obj.checks.active.healthy.last_run + checker_obj.checks.active.healthy.interval <= cur_time) diff --git a/t/04-report_success.t b/t/04-report_success.t index 8fb5fb6e..084d02da 100644 --- a/t/04-report_success.t +++ b/t/04-report_success.t @@ -84,14 +84,14 @@ GET /t true true --- error_log -healthy SUCCESS increment (1/3) for '(127.0.0.1:2116)' -healthy SUCCESS increment (2/3) for '(127.0.0.1:2116)' -healthy SUCCESS increment (3/3) for '(127.0.0.1:2116)' -event: target status '(127.0.0.1:2116)' from 'false' to 'true' -healthy SUCCESS increment (1/3) for '(127.0.0.1:2116)' -healthy SUCCESS increment (2/3) for '(127.0.0.1:2116)' -healthy SUCCESS increment (3/3) for '(127.0.0.1:2116)' -event: target status '(127.0.0.1:2118)' from 'false' to 'true' +healthy SUCCESS increment (1/3) for '127.0.0.1(127.0.0.1:2116)' +healthy SUCCESS increment (2/3) for '127.0.0.1(127.0.0.1:2116)' +healthy SUCCESS increment (3/3) for '127.0.0.1(127.0.0.1:2116)' +event: target status '127.0.0.1(127.0.0.1:2116)' from 'false' to 'true' +healthy SUCCESS increment (1/3) for '127.0.0.1(127.0.0.1:2116)' +healthy SUCCESS increment (2/3) for '127.0.0.1(127.0.0.1:2116)' +healthy SUCCESS increment (3/3) for '127.0.0.1(127.0.0.1:2116)' +event: target status '127.0.0.1(127.0.0.1:2118)' from 'false' to 'true' === TEST 2: report_success() recovers TCP active = passive @@ -159,14 +159,14 @@ GET /t true true --- error_log -healthy SUCCESS increment (1/3) for '(127.0.0.1:2116)' -healthy SUCCESS increment (2/3) for '(127.0.0.1:2116)' -healthy SUCCESS increment (3/3) for '(127.0.0.1:2116)' -event: target status '(127.0.0.1:2116)' from 'false' to 'true' -healthy SUCCESS increment (1/3) for '(127.0.0.1:2116)' -healthy SUCCESS increment (2/3) for '(127.0.0.1:2116)' -healthy SUCCESS increment (3/3) for '(127.0.0.1:2116)' -event: target status '(127.0.0.1:2118)' from 'false' to 'true' +healthy SUCCESS increment (1/3) for '127.0.0.1(127.0.0.1:2116)' +healthy SUCCESS increment (2/3) for '127.0.0.1(127.0.0.1:2116)' +healthy SUCCESS increment (3/3) for '127.0.0.1(127.0.0.1:2116)' +event: target status '127.0.0.1(127.0.0.1:2116)' from 'false' to 'true' +healthy SUCCESS increment (1/3) for '127.0.0.1(127.0.0.1:2116)' +healthy SUCCESS increment (2/3) for '127.0.0.1(127.0.0.1:2116)' +healthy SUCCESS increment (3/3) for '127.0.0.1(127.0.0.1:2116)' +event: target status '127.0.0.1(127.0.0.1:2118)' from 'false' to 'true' === TEST 3: report_success() is a nop when active.healthy.sucesses == 0 --- http_config eval @@ -292,4 +292,4 @@ GET /t false --- no_error_log healthy SUCCESS increment -event: target status '(127.0.0.1:2118)' from 'false' to 'true' +event: target status '127.0.0.1(127.0.0.1:2118)' from 'false' to 'true' diff --git a/t/05-report_failure.t b/t/05-report_failure.t index 47f8c7e9..05eefa25 100644 --- a/t/05-report_failure.t +++ b/t/05-report_failure.t @@ -84,14 +84,14 @@ GET /t false false --- error_log -unhealthy HTTP increment (1/3) for '(127.0.0.1:2117)' -unhealthy HTTP increment (2/3) for '(127.0.0.1:2117)' -unhealthy HTTP increment (3/3) for '(127.0.0.1:2117)' -event: target status '(127.0.0.1:2117)' from 'true' to 'false' -unhealthy HTTP increment (1/3) for '(127.0.0.1:2113)' -unhealthy HTTP increment (2/3) for '(127.0.0.1:2113)' -unhealthy HTTP increment (3/3) for '(127.0.0.1:2113)' -event: target status '(127.0.0.1:2113)' from 'true' to 'false' +unhealthy HTTP increment (1/3) for '127.0.0.1(127.0.0.1:2117)' +unhealthy HTTP increment (2/3) for '127.0.0.1(127.0.0.1:2117)' +unhealthy HTTP increment (3/3) for '127.0.0.1(127.0.0.1:2117)' +event: target status '127.0.0.1(127.0.0.1:2117)' from 'true' to 'false' +unhealthy HTTP increment (1/3) for '127.0.0.1(127.0.0.1:2113)' +unhealthy HTTP increment (2/3) for '127.0.0.1(127.0.0.1:2113)' +unhealthy HTTP increment (3/3) for '127.0.0.1(127.0.0.1:2113)' +event: target status '127.0.0.1(127.0.0.1:2113)' from 'true' to 'false' === TEST 2: report_failure() fails TCP active + passive @@ -159,12 +159,12 @@ GET /t false false --- error_log -unhealthy TCP increment (1/2) for '(127.0.0.1:2117)' -unhealthy TCP increment (2/2) for '(127.0.0.1:2117)' -event: target status '(127.0.0.1:2117)' from 'true' to 'false' -unhealthy TCP increment (1/2) for '(127.0.0.1:2113)' -unhealthy TCP increment (2/2) for '(127.0.0.1:2113)' -event: target status '(127.0.0.1:2113)' from 'true' to 'false' +unhealthy TCP increment (1/2) for '127.0.0.1(127.0.0.1:2117)' +unhealthy TCP increment (2/2) for '127.0.0.1(127.0.0.1:2117)' +event: target status '127.0.0.1(127.0.0.1:2117)' from 'true' to 'false' +unhealthy TCP increment (1/2) for '127.0.0.1(127.0.0.1:2113)' +unhealthy TCP increment (2/2) for '127.0.0.1(127.0.0.1:2113)' +event: target status '127.0.0.1(127.0.0.1:2113)' from 'true' to 'false' === TEST 3: report_failure() is a nop when failure counters == 0 @@ -232,9 +232,9 @@ GET /t true true --- no_error_log -unhealthy TCP increment (1/2) for '(127.0.0.1:2117)' -unhealthy TCP increment (2/2) for '(127.0.0.1:2117)' -event: target status '(127.0.0.1:2117)' from 'true' to 'false' -unhealthy TCP increment (1/2) for '(127.0.0.1:2113)' -unhealthy TCP increment (2/2) for '(127.0.0.1:2113)' -event: target status '(127.0.0.1:2113)' from 'true' to 'false' +unhealthy TCP increment (1/2) for '127.0.0.1(127.0.0.1:2117)' +unhealthy TCP increment (2/2) for '127.0.0.1(127.0.0.1:2117)' +event: target status '127.0.0.1(127.0.0.1:2117)' from 'true' to 'false' +unhealthy TCP increment (1/2) for '127.0.0.1(127.0.0.1:2113)' +unhealthy TCP increment (2/2) for '127.0.0.1(127.0.0.1:2113)' +event: target status '127.0.0.1(127.0.0.1:2113)' from 'true' to 'false' diff --git a/t/06-report_http_status.t b/t/06-report_http_status.t index 0d2c1cdd..5d694832 100644 --- a/t/06-report_http_status.t +++ b/t/06-report_http_status.t @@ -84,14 +84,14 @@ GET /t false false --- error_log -unhealthy HTTP increment (1/3) for '(127.0.0.1:2119)' -unhealthy HTTP increment (2/3) for '(127.0.0.1:2119)' -unhealthy HTTP increment (3/3) for '(127.0.0.1:2119)' -event: target status '(127.0.0.1:2119)' from 'true' to 'false' -unhealthy HTTP increment (1/3) for '(127.0.0.1:2113)' -unhealthy HTTP increment (2/3) for '(127.0.0.1:2113)' -unhealthy HTTP increment (3/3) for '(127.0.0.1:2113)' -event: target status '(127.0.0.1:2113)' from 'true' to 'false' +unhealthy HTTP increment (1/3) for '127.0.0.1(127.0.0.1:2119)' +unhealthy HTTP increment (2/3) for '127.0.0.1(127.0.0.1:2119)' +unhealthy HTTP increment (3/3) for '127.0.0.1(127.0.0.1:2119)' +event: target status '127.0.0.1(127.0.0.1:2119)' from 'true' to 'false' +unhealthy HTTP increment (1/3) for '127.0.0.1(127.0.0.1:2113)' +unhealthy HTTP increment (2/3) for '127.0.0.1(127.0.0.1:2113)' +unhealthy HTTP increment (3/3) for '127.0.0.1(127.0.0.1:2113)' +event: target status '127.0.0.1(127.0.0.1:2113)' from 'true' to 'false' @@ -162,16 +162,16 @@ GET /t true true --- error_log -healthy SUCCESS increment (1/4) for '(127.0.0.1:2119)' -healthy SUCCESS increment (2/4) for '(127.0.0.1:2119)' -healthy SUCCESS increment (3/4) for '(127.0.0.1:2119)' -healthy SUCCESS increment (4/4) for '(127.0.0.1:2119)' -event: target status '(127.0.0.1:2119)' from 'false' to 'true' -healthy SUCCESS increment (1/4) for '(127.0.0.1:2113)' -healthy SUCCESS increment (2/4) for '(127.0.0.1:2113)' -healthy SUCCESS increment (3/4) for '(127.0.0.1:2113)' -healthy SUCCESS increment (4/4) for '(127.0.0.1:2113)' -event: target status '(127.0.0.1:2113)' from 'false' to 'true' +healthy SUCCESS increment (1/4) for '127.0.0.1(127.0.0.1:2119)' +healthy SUCCESS increment (2/4) for '127.0.0.1(127.0.0.1:2119)' +healthy SUCCESS increment (3/4) for '127.0.0.1(127.0.0.1:2119)' +healthy SUCCESS increment (4/4) for '127.0.0.1(127.0.0.1:2119)' +event: target status '127.0.0.1(127.0.0.1:2119)' from 'false' to 'true' +healthy SUCCESS increment (1/4) for '127.0.0.1(127.0.0.1:2113)' +healthy SUCCESS increment (2/4) for '127.0.0.1(127.0.0.1:2113)' +healthy SUCCESS increment (3/4) for '127.0.0.1(127.0.0.1:2113)' +healthy SUCCESS increment (4/4) for '127.0.0.1(127.0.0.1:2113)' +event: target status '127.0.0.1(127.0.0.1:2113)' from 'false' to 'true' === TEST 3: report_http_status() with success is a nop when passive.healthy.successes == 0 @@ -427,7 +427,7 @@ GET /t true --- no_error_log unhealthy HTTP increment -event: target status '(127.0.0.1:2119)' from 'true' to 'false' +event: target status '127.0.0.1(127.0.0.1:2119)' from 'true' to 'false' === TEST 5: report_http_status() must work in log phase diff --git a/t/07-report_tcp_failure.t b/t/07-report_tcp_failure.t index 9e4e0e7c..f2355e96 100644 --- a/t/07-report_tcp_failure.t +++ b/t/07-report_tcp_failure.t @@ -84,14 +84,14 @@ GET /t false false --- error_log -unhealthy TCP increment (1/3) for '(127.0.0.1:2120)' -unhealthy TCP increment (2/3) for '(127.0.0.1:2120)' -unhealthy TCP increment (3/3) for '(127.0.0.1:2120)' -event: target status '(127.0.0.1:2120)' from 'true' to 'false' -unhealthy TCP increment (1/3) for '(127.0.0.1:2113)' -unhealthy TCP increment (2/3) for '(127.0.0.1:2113)' -unhealthy TCP increment (3/3) for '(127.0.0.1:2113)' -event: target status '(127.0.0.1:2113)' from 'true' to 'false' +unhealthy TCP increment (1/3) for '127.0.0.1(127.0.0.1:2120)' +unhealthy TCP increment (2/3) for '127.0.0.1(127.0.0.1:2120)' +unhealthy TCP increment (3/3) for '127.0.0.1(127.0.0.1:2120)' +event: target status '127.0.0.1(127.0.0.1:2120)' from 'true' to 'false' +unhealthy TCP increment (1/3) for '127.0.0.1(127.0.0.1:2113)' +unhealthy TCP increment (2/3) for '127.0.0.1(127.0.0.1:2113)' +unhealthy TCP increment (3/3) for '127.0.0.1(127.0.0.1:2113)' +event: target status '127.0.0.1(127.0.0.1:2113)' from 'true' to 'false' === TEST 2: report_tcp_failure() for active is a nop when active.unhealthy.tcp_failures == 0 @@ -154,7 +154,7 @@ GET /t true --- no_error_log unhealthy TCP increment -event: target status '(127.0.0.1:2120)' from 'true' to 'false' +event: target status '127.0.0.1(127.0.0.1:2120)' from 'true' to 'false' @@ -218,4 +218,4 @@ GET /t true --- no_error_log unhealthy TCP increment -event: target status '(127.0.0.1:2120)' from 'true' to 'false' +event: target status '127.0.0.1(127.0.0.1:2120)' from 'true' to 'false' diff --git a/t/08-report_timeout.t b/t/08-report_timeout.t index fb61ea18..1dd01358 100644 --- a/t/08-report_timeout.t +++ b/t/08-report_timeout.t @@ -84,12 +84,12 @@ GET /t false false --- error_log -unhealthy TIMEOUT increment (1/2) for '(127.0.0.1:2122)' -unhealthy TIMEOUT increment (2/2) for '(127.0.0.1:2122)' -event: target status '(127.0.0.1:2122)' from 'true' to 'false' -unhealthy TIMEOUT increment (1/2) for '(127.0.0.1:2113)' -unhealthy TIMEOUT increment (2/2) for '(127.0.0.1:2113)' -event: target status '(127.0.0.1:2113)' from 'true' to 'false' +unhealthy TIMEOUT increment (1/2) for '127.0.0.1(127.0.0.1:2122)' +unhealthy TIMEOUT increment (2/2) for '127.0.0.1(127.0.0.1:2122)' +event: target status '127.0.0.1(127.0.0.1:2122)' from 'true' to 'false' +unhealthy TIMEOUT increment (1/2) for '127.0.0.1(127.0.0.1:2113)' +unhealthy TIMEOUT increment (2/2) for '127.0.0.1(127.0.0.1:2113)' +event: target status '127.0.0.1(127.0.0.1:2113)' from 'true' to 'false' === TEST 2: report_timeout() for active is a nop when active.unhealthy.timeouts == 0 @@ -154,7 +154,7 @@ GET /t true --- no_error_log unhealthy TCP increment -event: target status '(127.0.0.1:2122)' from 'true' to 'false' +event: target status '127.0.0.1(127.0.0.1:2122)' from 'true' to 'false' @@ -220,4 +220,4 @@ GET /t true --- no_error_log unhealthy TCP increment -event: target status '(127.0.0.1:2122)' from 'true' to 'false' +event: target status '127.0.0.1(127.0.0.1:2122)' from 'true' to 'false' diff --git a/t/09-active_probes.t b/t/09-active_probes.t index 86c523cd..dd68faf4 100644 --- a/t/09-active_probes.t +++ b/t/09-active_probes.t @@ -67,10 +67,10 @@ GET /t false --- error_log checking unhealthy targets: nothing to do -unhealthy HTTP increment (1/3) for '(127.0.0.1:2114)' -unhealthy HTTP increment (2/3) for '(127.0.0.1:2114)' -unhealthy HTTP increment (3/3) for '(127.0.0.1:2114)' -event: target status '(127.0.0.1:2114)' from 'true' to 'false' +unhealthy HTTP increment (1/3) for '127.0.0.1(127.0.0.1:2114)' +unhealthy HTTP increment (2/3) for '127.0.0.1(127.0.0.1:2114)' +unhealthy HTTP increment (3/3) for '127.0.0.1(127.0.0.1:2114)' +event: target status '127.0.0.1(127.0.0.1:2114)' from 'true' to 'false' checking healthy targets: nothing to do @@ -123,10 +123,10 @@ GET /t true --- error_log checking healthy targets: nothing to do -healthy SUCCESS increment (1/3) for '(127.0.0.1:2114)' -healthy SUCCESS increment (2/3) for '(127.0.0.1:2114)' -healthy SUCCESS increment (3/3) for '(127.0.0.1:2114)' -event: target status '(127.0.0.1:2114)' from 'false' to 'true' +healthy SUCCESS increment (1/3) for '127.0.0.1(127.0.0.1:2114)' +healthy SUCCESS increment (2/3) for '127.0.0.1(127.0.0.1:2114)' +healthy SUCCESS increment (3/3) for '127.0.0.1(127.0.0.1:2114)' +event: target status '127.0.0.1(127.0.0.1:2114)' from 'false' to 'true' checking unhealthy targets: nothing to do === TEST 3: active probes, custom http status (regression test for pre-filled defaults) @@ -179,10 +179,10 @@ true checking unhealthy targets: nothing to do --- no_error_log checking healthy targets: nothing to do -unhealthy HTTP increment (1/3) for '(127.0.0.1:2114)' -unhealthy HTTP increment (2/3) for '(127.0.0.1:2114)' -unhealthy HTTP increment (3/3) for '(127.0.0.1:2114)' -event: target status '(127.0.0.1:2114)' from 'true' to 'false' +unhealthy HTTP increment (1/3) for '127.0.0.1(127.0.0.1:2114)' +unhealthy HTTP increment (2/3) for '127.0.0.1(127.0.0.1:2114)' +unhealthy HTTP increment (3/3) for '127.0.0.1(127.0.0.1:2114)' +event: target status '127.0.0.1(127.0.0.1:2114)' from 'true' to 'false' === TEST 4: active probes, custom http status, node failing @@ -234,10 +234,10 @@ GET /t false --- error_log checking unhealthy targets: nothing to do -unhealthy HTTP increment (1/3) for '(127.0.0.1:2114)' -unhealthy HTTP increment (2/3) for '(127.0.0.1:2114)' -unhealthy HTTP increment (3/3) for '(127.0.0.1:2114)' -event: target status '(127.0.0.1:2114)' from 'true' to 'false' +unhealthy HTTP increment (1/3) for '127.0.0.1(127.0.0.1:2114)' +unhealthy HTTP increment (2/3) for '127.0.0.1(127.0.0.1:2114)' +unhealthy HTTP increment (3/3) for '127.0.0.1(127.0.0.1:2114)' +event: target status '127.0.0.1(127.0.0.1:2114)' from 'true' to 'false' checking healthy targets: nothing to do @@ -340,10 +340,10 @@ GET /t false --- error_log checking unhealthy targets: nothing to do -unhealthy TCP increment (1/3) for '(127.0.0.1:2114)' -unhealthy TCP increment (2/3) for '(127.0.0.1:2114)' -unhealthy TCP increment (3/3) for '(127.0.0.1:2114)' -event: target status '(127.0.0.1:2114)' from 'true' to 'false' +unhealthy TCP increment (1/3) for '127.0.0.1(127.0.0.1:2114)' +unhealthy TCP increment (2/3) for '127.0.0.1(127.0.0.1:2114)' +unhealthy TCP increment (3/3) for '127.0.0.1(127.0.0.1:2114)' +event: target status '127.0.0.1(127.0.0.1:2114)' from 'true' to 'false' checking healthy targets: nothing to do @@ -396,10 +396,10 @@ GET /t true --- error_log checking healthy targets: nothing to do -healthy SUCCESS increment (1/3) for '(127.0.0.1:2114)' -healthy SUCCESS increment (2/3) for '(127.0.0.1:2114)' -healthy SUCCESS increment (3/3) for '(127.0.0.1:2114)' -event: target status '(127.0.0.1:2114)' from 'false' to 'true' +healthy SUCCESS increment (1/3) for '127.0.0.1(127.0.0.1:2114)' +healthy SUCCESS increment (2/3) for '127.0.0.1(127.0.0.1:2114)' +healthy SUCCESS increment (3/3) for '127.0.0.1(127.0.0.1:2114)' +event: target status '127.0.0.1(127.0.0.1:2114)' from 'false' to 'true' checking unhealthy targets: nothing to do diff --git a/t/11-clear.t b/t/11-clear.t index d3a9ebd7..7eb33375 100644 --- a/t/11-clear.t +++ b/t/11-clear.t @@ -3,7 +3,7 @@ use Cwd qw(cwd); workers(1); -plan tests => repeat_each() * 23; +plan tests => repeat_each() * 27; my $pwd = cwd(); @@ -164,7 +164,119 @@ GET /t true --- error_log -unhealthy HTTP increment (1/3) for '(127.0.0.1:21120)' -unhealthy HTTP increment (2/3) for '(127.0.0.1:21120)' +unhealthy HTTP increment (1/3) for '127.0.0.1(127.0.0.1:21120)' +unhealthy HTTP increment (2/3) for '127.0.0.1(127.0.0.1:21120)' --- no_error_log -unhealthy HTTP increment (3/3) for '(127.0.0.1:21120)' +unhealthy HTTP increment (3/3) for '127.0.0.1(127.0.0.1:21120)' + + +=== TEST 4: delayed_clear() clears the list, after interval new checkers don't see it +--- http_config eval: $::HttpConfig +--- config + location = /t { + content_by_lua_block { + local we = require "resty.worker.events" + assert(we.configure{ shm = "my_worker_events", interval = 0.1 }) + local healthcheck = require("resty.healthcheck") + local config = { + name = "testing", + shm_name = "test_shm", + checks = { + active = { + healthy = { + interval = 0.1 + }, + unhealthy = { + interval = 0.1 + } + } + } + } + local checker1 = healthcheck.new(config) + for i = 1, 10 do + checker1:add_target("127.0.0.1", 10000 + i, nil, false) + end + ngx.sleep(0.2) -- wait twice the interval + ngx.say(checker1:get_target_status("127.0.0.1", 10001)) + checker1:delayed_clear(0.2) + + local checker2 = healthcheck.new(config) + ngx.say(checker2:get_target_status("127.0.0.1", 10001)) + ngx.sleep(0.4) -- wait while the targets are cleared + local status, err = checker2:get_target_status("127.0.0.1", 10001) + if status ~= nil then + ngx.say(status) + else + ngx.say(err) + end + } + } +--- request +GET /t +--- response_body +false +false +target not found + +=== TEST 5: delayed_clear() would clear tgt list, but adding again keeps the previous status +--- http_config eval: $::HttpConfig +--- config + location = /t { + content_by_lua_block { + local we = require "resty.worker.events" + assert(we.configure{ shm = "my_worker_events", interval = 0.1 }) + local healthcheck = require("resty.healthcheck") + local config = { + name = "testing", + shm_name = "test_shm", + checks = { + active = { + healthy = { + interval = 0.1 + }, + unhealthy = { + interval = 0.1 + } + } + } + } + local checker1 = healthcheck.new(config) + checker1:add_target("127.0.0.1", 10001, nil, false) + checker1:add_target("127.0.0.1", 10002, nil, false) + checker1:add_target("127.0.0.1", 10003, nil, false) + ngx.sleep(0.2) -- wait twice the interval + ngx.say(checker1:get_target_status("127.0.0.1", 10002)) + checker1:delayed_clear(0.2) + + local checker2 = healthcheck.new(config) + checker2:add_target("127.0.0.1", 10002, nil, true) + ngx.say(checker2:get_target_status("127.0.0.1", 10002)) + ngx.sleep(0.4) -- wait while the targets would be cleared + local status, err = checker2:get_target_status("127.0.0.1", 10001) + if status ~= nil then + ngx.say(status) + else + ngx.say(err) + end + status, err = checker2:get_target_status("127.0.0.1", 10002) + if status ~= nil then + ngx.say(status) + else + ngx.say(err) + end + status, err = checker2:get_target_status("127.0.0.1", 10003) + if status ~= nil then + ngx.say(status) + else + ngx.say(err) + end + } + } +--- request +GET /t +--- response_body +false +false +target not found +false +target not found diff --git a/t/15-get_virtualhost_target_status.t b/t/15-get_virtualhost_target_status.t index 9bfbc92c..2a3f5fce 100644 --- a/t/15-get_virtualhost_target_status.t +++ b/t/15-get_virtualhost_target_status.t @@ -147,8 +147,8 @@ true true false --- error_log -unhealthy HTTP increment (1/1) for '(127.0.0.1:2116)' -event: target status '(127.0.0.1:2116)' from 'true' to 'false' +unhealthy HTTP increment (1/1) for '127.0.0.1(127.0.0.1:2116)' +event: target status '127.0.0.1(127.0.0.1:2116)' from 'true' to 'false' @@ -293,7 +293,7 @@ false false false --- error_log -unhealthy HTTP increment (1/1) for '(127.0.0.1:2118)' -event: target status '(127.0.0.1:2118)' from 'true' to 'false' +unhealthy HTTP increment (1/1) for '127.0.0.1(127.0.0.1:2118)' +event: target status '127.0.0.1(127.0.0.1:2118)' from 'true' to 'false' unhealthy HTTP increment (1/1) for '127.0.0.1(127.0.0.1:2119)' event: target status '127.0.0.1(127.0.0.1:2119)' from 'true' to 'false'