From ce4d8fb1e55f94c03c3a641f8f0d1a62c189ceaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E6=B3=BD=E8=BD=A9?= Date: Fri, 16 Apr 2021 19:09:45 +0800 Subject: [PATCH] fix: ensure upstream with domain is cached (#4061) Fix #4047 Signed-off-by: spacewander --- apisix/init.lua | 28 ++++++++------- t/node/route-domain.t | 57 +++++++++++++++++++++++++++++++ t/node/upstream-domain.t | 74 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 145 insertions(+), 14 deletions(-) diff --git a/apisix/init.lua b/apisix/init.lua index d71fd70c0ace..70be7966a830 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -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 @@ -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 @@ -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 diff --git a/t/node/route-domain.t b/t/node/route-domain.t index 4f240a2ed597..157b858d8ad7 100644 --- a/t/node/route-domain.t +++ b/t/node/route-domain.t @@ -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] diff --git a/t/node/upstream-domain.t b/t/node/upstream-domain.t index 3b1b98bca9d9..63069fda18c5 100644 --- a/t/node/upstream-domain.t +++ b/t/node/upstream-domain.t @@ -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]