You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2022/08/17 09:29:03 UTC

[apisix] 01/12: fix: distinguish different upstreams even they have the same addr (#7213)

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

spacewander pushed a commit to branch release/2.13
in repository https://gitbox.apache.org/repos/asf/apisix.git

commit 33bd7416d76d8c1516af6335f0df7d0166a75878
Author: 罗泽轩 <sp...@gmail.com>
AuthorDate: Mon Jun 13 16:52:29 2022 +0800

    fix: distinguish different upstreams even they have the same addr (#7213)
    
    Thanks for the report of @redynasc
    Signed-off-by: spacewander <sp...@gmail.com>
---
 apisix/upstream.lua |  7 ++++--
 t/node/upstream.t   | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+), 2 deletions(-)

diff --git a/apisix/upstream.lua b/apisix/upstream.lua
index 75c589e9c..e153a1f1c 100644
--- a/apisix/upstream.lua
+++ b/apisix/upstream.lua
@@ -285,8 +285,11 @@ function _M.set_by_route(route, api_ctx)
         end
     end
 
-    set_directly(api_ctx, up_conf.type .. "#upstream_" .. tostring(up_conf),
-                 tostring(up_conf), up_conf)
+    local id = up_conf.parent.value.id
+    local conf_version = up_conf.parent.modifiedIndex
+    -- include the upstream object as part of the version, because the upstream will be changed
+    -- by service discovery or dns resolver.
+    set_directly(api_ctx, id, conf_version .. "#" .. tostring(up_conf), up_conf)
 
     local nodes_count = up_conf.nodes and #up_conf.nodes or 0
     if nodes_count == 0 then
diff --git a/t/node/upstream.t b/t/node/upstream.t
index 9a248bb10..f5501d019 100644
--- a/t/node/upstream.t
+++ b/t/node/upstream.t
@@ -510,3 +510,73 @@ GET /uri
 qr/host: localhost/
 --- error_log
 proxy request to 127.0.0.1:1980
+
+
+
+=== TEST 21: distinguish different upstreams even they have the same addr
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/upstreams/1',
+                 ngx.HTTP_PUT,
+                 {
+                    nodes = {["localhost:1980"] = 1},
+                    type = "roundrobin"
+                 }
+                )
+            assert(code < 300)
+
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "upstream_id": "1",
+                        "uri": "/server_port"
+                }]]
+                )
+            assert(code < 300)
+
+            local http = require "resty.http"
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port
+                        .. "/server_port"
+
+            local ports_count = {}
+            for i = 1, 24 do
+                local httpc = http.new()
+                local res, err = httpc:request_uri(uri)
+                if not res then
+                    ngx.say(err)
+                    return
+                end
+                ports_count[res.body] = (ports_count[res.body] or 0) + 1
+
+                local code, body = t('/apisix/admin/upstreams/1',
+                    ngx.HTTP_PUT,
+                    {
+                        nodes = {["localhost:" .. (1980 + i % 3)] = 1},
+                        type = "roundrobin"
+                    }
+                    )
+                assert(code < 300)
+            end
+
+            local ports_arr = {}
+            for port, count in pairs(ports_count) do
+                table.insert(ports_arr, {port = port, count = count})
+            end
+
+            local function cmd(a, b)
+                return a.port > b.port
+            end
+            table.sort(ports_arr, cmd)
+
+            ngx.say(require("toolkit.json").encode(ports_arr))
+        }
+    }
+--- request
+GET /t
+--- timeout: 5
+--- response_body
+[{"count":8,"port":"1982"},{"count":8,"port":"1981"},{"count":8,"port":"1980"}]
+--- no_error_log
+[error]