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 2021/04/16 11:09:56 UTC

[apisix] branch master updated: fix: ensure upstream with domain is cached (#4061)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new ce4d8fb  fix: ensure upstream with domain is cached (#4061)
ce4d8fb is described below

commit ce4d8fb1e55f94c03c3a641f8f0d1a62c189ceaf
Author: 罗泽轩 <sp...@gmail.com>
AuthorDate: Fri Apr 16 19:09:45 2021 +0800

    fix: ensure upstream with domain is cached (#4061)
    
    Fix #4047
    
    Signed-off-by: spacewander <sp...@gmail.com>
---
 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 d71fd70..70be796 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 4f240a2..157b858 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 3b1b98b..63069fd 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]