From a41843f6a69a4b7c365dd2b427600b67524cd645 Mon Sep 17 00:00:00 2001 From: Kong Team Gateway Bot <98048765+team-gateway-bot@users.noreply.github.com> Date: Wed, 1 Mar 2023 13:43:43 -0800 Subject: [PATCH] fix(balancer) use local target cache (#10384) (#10411) * fix(balancer) use local target cache * fix(targets): remove dup code (cherry picked from commit 4f29d72d00b3278a2c12fb67a073c3c985f5b434) Co-authored-by: Vinicius Mignot --- kong/runloop/balancer/balancers.lua | 1 + kong/runloop/balancer/targets.lua | 32 +++++++++++++++++++---------- kong/runloop/balancer/upstreams.lua | 5 ----- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/kong/runloop/balancer/balancers.lua b/kong/runloop/balancer/balancers.lua index 54faeacd8821..cd45184897ba 100644 --- a/kong/runloop/balancer/balancers.lua +++ b/kong/runloop/balancer/balancers.lua @@ -102,6 +102,7 @@ local function create_balancer_exclusive(upstream) local health_threshold = upstream.healthchecks and upstream.healthchecks.threshold or nil + targets.clean_targets_cache(upstream) local targets_list, err = targets.fetch_targets(upstream) if not targets_list then return nil, "failed fetching targets:" .. err diff --git a/kong/runloop/balancer/targets.lua b/kong/runloop/balancer/targets.lua index 0b95ecd6ef30..89b4563853b4 100644 --- a/kong/runloop/balancer/targets.lua +++ b/kong/runloop/balancer/targets.lua @@ -38,6 +38,8 @@ local GLOBAL_QUERY_OPTS = { workspace = null, show_ws_id = true } local renewal_heap = require("binaryheap").minUnique() local renewal_weak_cache = setmetatable({}, { __mode = "v" }) +local targets_by_upstream_id = {} + local targets_M = {} -- forward local declarations @@ -126,9 +128,17 @@ end function targets_M.fetch_targets(upstream) local targets_cache_key = "balancer:targets:" .. upstream.id - return kong.core_cache:get( - targets_cache_key, nil, - load_targets_into_memory, upstream.id) + if targets_by_upstream_id[targets_cache_key] == nil then + targets_by_upstream_id[targets_cache_key] = load_targets_into_memory(upstream.id) + end + + return targets_by_upstream_id[targets_cache_key] +end + + +function targets_M.clean_targets_cache(upstream) + local targets_cache_key = "balancer:targets:" .. upstream.id + targets_by_upstream_id[targets_cache_key] = nil end @@ -157,7 +167,14 @@ function targets_M.on_target_event(operation, target) log(DEBUG, "target ", operation, " for upstream ", upstream_id, upstream_name and " (" .. upstream_name ..")" or "") - kong.core_cache:invalidate_local("balancer:targets:" .. upstream_id) + targets_by_upstream_id["balancer:targets:" .. upstream_id] = nil + + local upstream = upstreams.get_upstream_by_id(upstream_id) + if not upstream then + log(ERR, "target ", operation, ": upstream not found for ", upstream_id, + upstream_name and " (" .. upstream_name ..")" or "") + return + end -- cancel DNS renewal if operation ~= "create" then @@ -170,13 +187,6 @@ function targets_M.on_target_event(operation, target) end end - local upstream = upstreams.get_upstream_by_id(upstream_id) - if not upstream then - log(ERR, "target ", operation, ": upstream not found for ", upstream_id, - upstream_name and " (" .. upstream_name ..")" or "") - return - end - -- move this to upstreams? local balancer = balancers.get_balancer_by_id(upstream_id) if not balancer then diff --git a/kong/runloop/balancer/upstreams.lua b/kong/runloop/balancer/upstreams.lua index c2ff7ede1b98..66c31d8bf0ef 100644 --- a/kong/runloop/balancer/upstreams.lua +++ b/kong/runloop/balancer/upstreams.lua @@ -192,11 +192,6 @@ local function do_upstream_event(operation, upstream_data) end elseif operation == "delete" or operation == "update" then - local target_cache_key = "balancer:targets:" .. upstream_id - if kong.db.strategy ~= "off" then - kong.core_cache:invalidate_local(target_cache_key) - end - local balancer = balancers.get_balancer_by_id(upstream_id) if balancer then healthcheckers.stop_healthchecker(balancer, CLEAR_HEALTH_STATUS_DELAY)