Skip to content

Commit 77e33f9

Browse files
authored
perf: compare service discovery nodes by addresss (#12258)
1 parent 85563f0 commit 77e33f9

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

apisix/upstream.lua

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ local function create_checker(upstream)
108108
end
109109

110110
local healthcheck_parent = upstream.parent
111-
if healthcheck_parent.checker and healthcheck_parent.checker_upstream == upstream then
111+
if healthcheck_parent.checker and healthcheck_parent.checker_upstream == upstream
112+
and healthcheck_parent.checker_nodes_ver == upstream._nodes_ver then
112113
return healthcheck_parent.checker
113114
end
114115

@@ -175,6 +176,7 @@ local function create_checker(upstream)
175176

176177
healthcheck_parent.checker = checker
177178
healthcheck_parent.checker_upstream = upstream
179+
healthcheck_parent.checker_nodes_ver = upstream._nodes_ver
178180
healthcheck_parent.checker_idx = check_idx
179181

180182
upstream.is_creating_checker = nil
@@ -302,35 +304,30 @@ function _M.set_by_route(route, api_ctx)
302304

303305
local same = upstream_util.compare_upstream_node(up_conf, new_nodes)
304306
if not same then
307+
if not up_conf._nodes_ver then
308+
up_conf._nodes_ver = 0
309+
end
310+
up_conf._nodes_ver = up_conf._nodes_ver + 1
311+
305312
local pass, err = core.schema.check(core.schema.discovery_nodes, new_nodes)
306313
if not pass then
307314
return HTTP_CODE_UPSTREAM_UNAVAILABLE, "invalid nodes format: " .. err
308315
end
309316

310-
local new_up_conf = core.table.clone(up_conf)
311-
new_up_conf.nodes = new_nodes
312-
new_up_conf.original_nodes = up_conf.nodes
317+
up_conf.nodes = new_nodes
313318

314319
core.log.info("discover new upstream from ", up_conf.service_name, ", type ",
315320
up_conf.discovery_type, ": ",
316-
core.json.delay_encode(new_up_conf, true))
317-
318-
local parent = up_conf.parent
319-
if parent.value.upstream then
320-
-- the up_conf comes from route or service
321-
parent.value.upstream = new_up_conf
322-
else
323-
parent.value = new_up_conf
324-
end
325-
up_conf = new_up_conf
321+
core.json.delay_encode(up_conf, true))
326322
end
327323
end
328324

329325
local id = up_conf.parent.value.id
330326
local conf_version = up_conf.parent.modifiedIndex
331327
-- include the upstream object as part of the version, because the upstream will be changed
332328
-- by service discovery or dns resolver.
333-
set_directly(api_ctx, id, conf_version .. "#" .. tostring(up_conf), up_conf)
329+
set_directly(api_ctx, id, conf_version .. "#" .. tostring(up_conf) .. "#"
330+
.. tostring(up_conf._nodes_ver or ''), up_conf)
334331

335332
local nodes_count = up_conf.nodes and #up_conf.nodes or 0
336333
if nodes_count == 0 then

apisix/utils/upstream.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ local function compare_upstream_node(up_conf, new_t)
3535
end
3636

3737
local old_t = up_conf.original_nodes or up_conf.nodes
38+
if old_t == new_t then
39+
return true
40+
end
41+
3842
if type(old_t) ~= "table" then
3943
return false
4044
end

0 commit comments

Comments
 (0)