You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by me...@apache.org on 2020/07/27 05:47:23 UTC

[incubator-apisix] branch master updated: bugfix: failed to get `host` in health check configuration. (#1871)

This is an automated email from the ASF dual-hosted git repository.

membphis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new a108d2e  bugfix: failed to get `host` in health check configuration. (#1871)
a108d2e is described below

commit a108d2eee17b823fb7df90d66e340e2795b6e229
Author: Janko <sh...@gmail.com>
AuthorDate: Mon Jul 27 13:47:14 2020 +0800

    bugfix: failed to get `host` in health check configuration. (#1871)
    
    The `host` of health check should be sub-item of `check.active` .
    
    FIX #1869
---
 apisix/balancer.lua  | 17 ++++++------
 t/node/healthcheck.t | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+), 9 deletions(-)

diff --git a/apisix/balancer.lua b/apisix/balancer.lua
index 50488cd..cd9edca 100644
--- a/apisix/balancer.lua
+++ b/apisix/balancer.lua
@@ -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)
@@ -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)
@@ -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
 
diff --git a/t/node/healthcheck.t b/t/node/healthcheck.t
index 96ff4f4..d2cca60 100644
--- a/t/node/healthcheck.t
+++ b/t/node/healthcheck.t
@@ -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/