Skip to content

Commit

Permalink
fix: ensure upstream with domain is cached (#4061)
Browse files Browse the repository at this point in the history
Fix #4047

Signed-off-by: spacewander <spacewanderlzx@gmail.com>
  • Loading branch information
spacewander authored Apr 16, 2021
1 parent 77f8926 commit ce4d8fb
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 14 deletions.
28 changes: 16 additions & 12 deletions apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,16 @@ local function parse_domain_in_up(up)
return up
end

local up_new = core.table.clone(up)
up_new.modifiedIndex = up.modifiedIndex .. "#" .. ngx_now()
up_new.dns_value = core.table.clone(up.value)
up_new.dns_value.nodes = new_nodes
if not up.orig_modifiedIndex then
up.orig_modifiedIndex = up.modifiedIndex
end
up.modifiedIndex = up.orig_modifiedIndex .. "#" .. ngx_now()

up.dns_value = core.table.clone(up.value)
up.dns_value.nodes = new_nodes
core.log.info("resolve upstream which contain domain: ",
core.json.delay_encode(up_new))
return up_new
core.json.delay_encode(up, true))
return up
end


Expand All @@ -234,14 +237,14 @@ local function parse_domain_in_route(route)
return route
end

local route_new = core.table.clone(route)
route_new.modifiedIndex = route.modifiedIndex .. "#" .. ngx_now()
-- don't modify the modifiedIndex to avoid plugin cache miss because of DNS resolve result
-- has changed

route_new.dns_value = core.table.deepcopy(route.value)
route_new.dns_value.upstream.nodes = new_nodes
route.dns_value = core.table.deepcopy(route.value)
route.dns_value.upstream.nodes = new_nodes
core.log.info("parse route which contain domain: ",
core.json.delay_encode(route))
return route_new
core.json.delay_encode(route, true))
return route
end


Expand Down Expand Up @@ -428,6 +431,7 @@ function _M.http_access_phase()
return core.response.exit(500)
end

api_ctx.conf_version = route.modifiedIndex
api_ctx.matched_route = route
end

Expand Down
57 changes: 57 additions & 0 deletions t/node/route-domain.t
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,60 @@ qr/"Host": "httpbin.org"/
--- no_error_log
[error]
--- timeout: 10



=== TEST 8: test domain with roundrobin
--- 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,
[[{
"upstream": {
"nodes": {
"localhost:1981": 2,
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/server_port"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 9: hit
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local bodys = {}
for i = 1, 3 do
local _, _, body = t('/server_port', ngx.HTTP_GET)
bodys[i] = body
end
table.sort(bodys)
ngx.say(table.concat(bodys, ", "))
}
}
--- request
GET /t
--- response_body
1980, 1981, 1981
--- no_error_log
[error]
74 changes: 72 additions & 2 deletions t/node/upstream-domain.t
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,77 @@ passed
GET /hello
--- response_body
hello world
--- no_error_log
[error]
--- error_log eval
qr/dns resolver domain: foo.com to \d+.\d+.\d+.\d+/
--- no_error_log
[error]



=== TEST 13: set route(with upstream)
--- 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:1981": 2,
"127.0.0.1:1980": 1
},
"type": "roundrobin",
"desc": "new upstream"
}]]
)

if code >= 300 then
ngx.status = code
ngx.say(body)
return
end

local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/server_port",
"service_id": "1",
"upstream_id": "1"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 14: roundrobin
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local bodys = {}
for i = 1, 3 do
local _, _, body = t('/server_port', ngx.HTTP_GET)
bodys[i] = body
end
table.sort(bodys)
ngx.say(table.concat(bodys, ", "))
}
}
--- request
GET /t
--- response_body
1980, 1981, 1981
--- no_error_log
[error]

0 comments on commit ce4d8fb

Please sign in to comment.