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

fix: distinguish different upstreams even they have the same addr #7213

Merged
merged 1 commit into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions apisix/upstream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,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
Expand Down
70 changes: 70 additions & 0 deletions t/node/upstream.t
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,73 @@ passed
GET /uri
--- error_log
Host: 127.0.0.1:1979



=== TEST 25: 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]