Skip to content

Commit

Permalink
fix(balancer) use local target cache (#10384) (#10411)
Browse files Browse the repository at this point in the history
* fix(balancer) use local target cache

* fix(targets): remove dup code

(cherry picked from commit 4f29d72)

Co-authored-by: Vinicius Mignot <[email protected]>
  • Loading branch information
team-gateway-bot and locao authored Mar 1, 2023
1 parent 2934db7 commit a41843f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
1 change: 1 addition & 0 deletions kong/runloop/balancer/balancers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 21 additions & 11 deletions kong/runloop/balancer/targets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 0 additions & 5 deletions kong/runloop/balancer/upstreams.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

3 comments on commit a41843f

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docker image avaialble kong/kong:3.0.x-apk
Artifacts availabe https://github.com/Kong/kong/actions/runs/4308225240

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docker image avaialble kong/kong:3.0.x-deb
Artifacts availabe https://github.com/Kong/kong/actions/runs/4308225240

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docker image avaialble kong/kong:3.0.x-rpm
Artifacts availabe https://github.com/Kong/kong/actions/runs/4308225240

Please sign in to comment.