Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: failed to get host in health check configuration. #1871

Merged
merged 6 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
70 changes: 70 additions & 0 deletions t/node/healthcheck.t
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,73 @@ code: 200 body: passed
delete code: 200
--- no_error_log
[error]



=== TEST 12: add route (test health check config 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 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.say(res.status)
shuaijinchao marked this conversation as resolved.
Show resolved Hide resolved
}
}
--- request
GET /t
--- response_body
200
--- grep_error_log eval
qr/^.*?\[warn\].*/
--- grep_error_log_out eval
qr/unhealthy TCP increment.*foo.com/