Skip to content

Commit 854dede

Browse files
fix: maintain node_version for independent upstream (#12856)
1 parent b971b19 commit 854dede

File tree

2 files changed

+124
-4
lines changed

2 files changed

+124
-4
lines changed

apisix/utils/upstream.lua

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
--
1717
local core = require("apisix.core")
1818
local ipmatcher = require("resty.ipmatcher")
19-
local ngx_now = ngx.now
2019
local ipairs = ipairs
2120
local type = type
2221
local tostring = tostring
22+
local resource = require("apisix.resource")
23+
2324

2425

2526
local _M = {}
@@ -117,11 +118,15 @@ function _M.parse_domain_in_up(up)
117118
return up
118119
end
119120

120-
if not up.orig_modifiedIndex then
121-
up.orig_modifiedIndex = up.modifiedIndex
121+
local nodes_ver = resource.get_nodes_ver(up.value.resource_key)
122+
if not nodes_ver then
123+
nodes_ver = 0
122124
end
123-
up.modifiedIndex = up.orig_modifiedIndex .. "#" .. ngx_now()
125+
nodes_ver = nodes_ver + 1
126+
up.value._nodes_ver = nodes_ver
124127
up.value.nodes = new_nodes
128+
resource.set_nodes_ver_and_nodes(up.value.resource_key, nodes_ver, new_nodes)
129+
125130
core.log.info("resolve upstream which contain domain: ",
126131
core.json.delay_encode(up, true))
127132
return up
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
use t::APISIX 'no_plan';
18+
19+
repeat_each(1);
20+
log_level('info');
21+
worker_connections(256);
22+
no_root_location();
23+
no_shuffle();
24+
25+
run_tests();
26+
27+
__DATA__
28+
29+
=== TEST 1: using route with an upstream id reference should also trigger healthcheck_manager
30+
--- extra_init_by_lua
31+
local utils = require("apisix.core.utils")
32+
local count = 0
33+
utils.dns_parse = function (domain) -- mock: DNS parser
34+
35+
count = count + 1
36+
if domain == "test1.com" then
37+
return {address = "127.0.0." .. count}
38+
end
39+
if domain == "test2.com" then
40+
return {address = "127.0.0." .. count+100}
41+
end
42+
43+
error("unknown domain: " .. domain)
44+
end
45+
--- config
46+
location /t {
47+
content_by_lua_block {
48+
local t = require("lib.test_admin").test
49+
local code, body = t('/apisix/admin/upstreams/1',
50+
ngx.HTTP_PUT,
51+
[[{
52+
"nodes": {
53+
"test1.com:1980": 1,
54+
"test2.com:1980": 1
55+
},
56+
"type": "roundrobin",
57+
"desc": "new upstream",
58+
"checks": {
59+
"active": {
60+
"http_path": "/status",
61+
"healthy": {
62+
"interval": 1,
63+
"successes": 4
64+
},
65+
"unhealthy": {
66+
"interval": 1,
67+
"http_failures": 1
68+
}
69+
}
70+
}
71+
}]]
72+
)
73+
74+
if code >= 300 then
75+
ngx.status = code
76+
return
77+
end
78+
79+
code, body = t('/apisix/admin/routes/1',
80+
ngx.HTTP_PUT,
81+
[[{
82+
"uri": "/hello",
83+
"upstream_id": "1"
84+
}]]
85+
)
86+
87+
if code >= 300 then
88+
ngx.status = code
89+
return
90+
end
91+
92+
for _, _ in ipairs({1, 2, 3, 4, 5}) do
93+
code, body = t('/hello', ngx.HTTP_GET)
94+
if code >= 300 then
95+
ngx.status = code
96+
return
97+
end
98+
ngx.sleep(1)
99+
end
100+
ngx.say(body)
101+
}
102+
}
103+
--- timeout: 10
104+
--- request
105+
GET /t
106+
--- response_body
107+
passed
108+
--- grep_error_log eval
109+
qr/create new checker: table:/
110+
--- grep_error_log_out
111+
create new checker: table:
112+
create new checker: table:
113+
create new checker: table:
114+
create new checker: table:
115+
create new checker: table:

0 commit comments

Comments
 (0)