Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(balancer) use local target cache #10384

Merged
merged 2 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
locao marked this conversation as resolved.
Show resolved Hide resolved

-- 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 @@ -191,11 +191,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