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: ensure upstream with domain is cached #4061

Merged
merged 1 commit into from
Apr 16, 2021
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
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]