Skip to content

Commit

Permalink
bugfix: failed to get host in health check configuration. (#1871)
Browse files Browse the repository at this point in the history
The `host` of health check should be sub-item of `check.active` .

FIX #1869
  • Loading branch information
shuaijinchao authored Jul 27, 2020
1 parent 45f84a2 commit a108d2e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 9 deletions.
17 changes: 8 additions & 9 deletions apisix/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ local function fetch_health_nodes(upstream, checker)
return new_nodes
end

local host = upstream.checks and upstream.checks.host
local host = upstream.checks and upstream.checks.active and upstream.checks.active.host
local up_nodes = core.table.new(0, #nodes)
for _, node in ipairs(nodes) do
local ok = checker:get_target_status(node.host, node.port, host)
Expand Down Expand Up @@ -91,8 +91,10 @@ local function create_checker(upstream, healthcheck_parent)
shm_name = "upstream-healthcheck",
checks = upstream.checks,
})

local host = upstream.checks and upstream.checks.active and upstream.checks.active.host
for _, node in ipairs(upstream.nodes) do
local ok, err = checker:add_target(node.host, node.port, upstream.checks.host)
local ok, err = checker:add_target(node.host, node.port, host)
if not ok then
core.log.error("failed to add new health check target: ", node.host, ":", node.port,
" err: ", err)
Expand Down Expand Up @@ -192,18 +194,15 @@ local function pick_server(route, ctx)
ctx.balancer_try_count = (ctx.balancer_try_count or 0) + 1
if checker and ctx.balancer_try_count > 1 then
local state, code = get_last_failure()
local host = up_conf.checks and up_conf.checks.active and up_conf.checks.active.host
if state == "failed" then
if code == 504 then
checker:report_timeout(ctx.balancer_ip, ctx.balancer_port,
up_conf.checks.host)
checker:report_timeout(ctx.balancer_ip, ctx.balancer_port, host)
else
checker:report_tcp_failure(ctx.balancer_ip,
ctx.balancer_port, up_conf.checks.host)
checker:report_tcp_failure(ctx.balancer_ip, ctx.balancer_port, host)
end

else
checker:report_http_status(ctx.balancer_ip, ctx.balancer_port,
up_conf.checks.host, code)
checker:report_http_status(ctx.balancer_ip, ctx.balancer_port, host, code)
end
end

Expand Down
73 changes: 73 additions & 0 deletions t/node/healthcheck.t
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,76 @@ code: 200 body: passed
delete code: 200
--- no_error_log
[error]



=== TEST 12: add route (test health check config `host` valid)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/server_port",
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1,
"127.0.0.1:1988": 1
},
"checks": {
"active": {
"http_path": "/status",
"host": "foo.com",
"healthy": {
"interval": 1,
"successes": 1
},
"unhealthy": {
"interval": 1,
"http_failures": 2
}
}
}
}
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed



=== TEST 13: test health check config `host` valid
--- config
location /t {
content_by_lua_block {
local http = require "resty.http"
local uri = "http://127.0.0.1:" .. ngx.var.server_port
.. "/server_port"

local httpc = http.new()
local res, err = httpc:request_uri(uri, {method = "GET", keepalive = false})

ngx.sleep(2)

ngx.say(res.status)
}
}
--- request
GET /t
--- response_body
200
--- grep_error_log eval
qr/^.*?\[warn\].*/
--- grep_error_log_out eval
qr/unhealthy TCP increment.*foo.com/

0 comments on commit a108d2e

Please sign in to comment.