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

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


The following commit(s) were added to refs/heads/master by this push:
     new 314edfda2 fix: running stale healthchecker when new node count <= 1 
(#12118)
314edfda2 is described below

commit 314edfda2062ae4653f948d024a51a4b17ecd5c8
Author: Ashish Tiwari <[email protected]>
AuthorDate: Mon Apr 7 15:31:12 2025 +0530

    fix: running stale healthchecker when new node count <= 1 (#12118)
---
 apisix/upstream.lua                      |   6 +-
 docs/en/latest/tutorials/health-check.md |   1 -
 docs/zh/latest/tutorials/health-check.md |   1 -
 t/discovery/reset-healthchecker.t        | 114 +++++++++++++++++++++++++++++++
 4 files changed, 116 insertions(+), 6 deletions(-)

diff --git a/apisix/upstream.lua b/apisix/upstream.lua
index e33f70533..c5a8cccaf 100644
--- a/apisix/upstream.lua
+++ b/apisix/upstream.lua
@@ -359,10 +359,8 @@ function _M.set_by_route(route, api_ctx)
         return 503, err
     end
 
-    if nodes_count > 1 then
-        local checker = fetch_healthchecker(up_conf)
-        api_ctx.up_checker = checker
-    end
+    local checker = fetch_healthchecker(up_conf)
+    api_ctx.up_checker = checker
 
     local scheme = up_conf.scheme
     if (scheme == "https" or scheme == "grpcs") and up_conf.tls then
diff --git a/docs/en/latest/tutorials/health-check.md 
b/docs/en/latest/tutorials/health-check.md
index e51f32f4b..cf344a9c6 100644
--- a/docs/en/latest/tutorials/health-check.md
+++ b/docs/en/latest/tutorials/health-check.md
@@ -52,7 +52,6 @@ Since unhealthy nodes cannot receive requests, nodes cannot 
be re-marked as heal
 
 - We only start the health check when the upstream is hit by a request. There 
won't be any health check if an upstream is configured but isn't in used.
 - If there is no healthy node can be chosen, we will continue to access the 
upstream.
-- We won't start the health check when the upstream only has one node, as we 
will access it whether this unique node is healthy or not.
 
 :::
 
diff --git a/docs/zh/latest/tutorials/health-check.md 
b/docs/zh/latest/tutorials/health-check.md
index 4aa50cc09..870c4b466 100644
--- a/docs/zh/latest/tutorials/health-check.md
+++ b/docs/zh/latest/tutorials/health-check.md
@@ -51,7 +51,6 @@ description: 本文介绍了如何使用 API 网关 Apache APISIX 的健康检
 
 - 只有在 `upstream` 被请求时才会开始健康检查,如果 `upstream` 被配置但没有被请求,不会触发启动健康检查。
 - 如果没有健康的节点,那么请求会继续发送给上游。
-- 如果 `upstream` 中只有一个节点时不会触发启动健康检查,该唯一节点无论是否健康,请求都将转发给上游。
 
 :::
 
diff --git a/t/discovery/reset-healthchecker.t 
b/t/discovery/reset-healthchecker.t
new file mode 100644
index 000000000..1ace8a33b
--- /dev/null
+++ b/t/discovery/reset-healthchecker.t
@@ -0,0 +1,114 @@
+#
+# 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.
+#
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+log_level('info');
+no_root_location();
+no_shuffle();
+workers(1);
+
+
+
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+    if ($block->apisix_yaml) {
+        my $upstream = <<_EOC_;
+upstreams:
+    - service_name: mock
+      discovery_type: mock
+      type: roundrobin
+      checks:
+        active:
+          http_path: /
+          timeout: 1
+          unhealthy:
+            tcp_failures: 30
+            interval: 1
+          healthy:
+            interval: 1
+      id: 1
+#END
+_EOC_
+
+        $block->set_value("apisix_yaml", $block->apisix_yaml . $upstream);
+    }
+});
+
+run_tests();
+
+__DATA__
+
+=== TEST 1: Validate healthchecker recreation on node count reduces to 1
+--- http_config
+server {
+    listen 3000 ;
+    location / {
+      return 200 'ok';
+    }
+}
+--- apisix_yaml
+routes:
+  -
+    uris:
+        - /
+    upstream_id: 1
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local discovery = require("apisix.discovery.init").discovery
+            discovery.mock = {
+                nodes = function()
+                    return {
+                        {host = "127.0.0.1", port = 3000, weight = 50},
+                        {host = "127.0.0.1", port = 8000, weight = 50},
+                    }
+                end
+            }
+            local http = require "resty.http"
+            local uri = "http://127.0.0.1:"; .. ngx.var.server_port .. "/"
+            local httpc = http.new()
+            local res, err = httpc:request_uri(uri, {method = "GET", keepalive 
= false})
+            ngx.sleep(5)
+            discovery.mock = {
+                nodes = function()
+                    return {
+                        {host = "127.0.0.1", port = 3000, weight = 1}
+                    }
+                end
+            }
+            local http = require "resty.http"
+            local uri = "http://127.0.0.1:"; .. ngx.var.server_port .. "/"
+            local httpc = http.new()
+            local res, err = httpc:request_uri(uri, {method = "GET", keepalive 
= false})
+            ngx.say(res.body)
+            ngx.sleep(5)
+        }
+    }
+--- request
+GET /t
+--- response_body
+ok
+--- timeout: 22
+--- no_error_log
+unhealthy TCP increment (10/30)

Reply via email to