Skip to content

Commit

Permalink
perf(runloop): load service with route.ws_id while building routers (
Browse files Browse the repository at this point in the history
…#14057)

1. From the perspective of more precise searching for services from `select` API, the route here already has a ws_id, so we should directly search for the service under the specific workspace
2. LMDB has removed all the global query keys (see #14028), if we select services without ws_id, it will search it through all the workspace, which has a performance issue.

KAG-6087, KAG-5704
  • Loading branch information
chobits authored Dec 30, 2024
1 parent 0b74d0f commit 5c78fb8
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions kong/runloop/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,9 @@ local function should_process_route(route)
end


local function load_service_from_db(service_pk)
local service, err = kong.db.services:select(service_pk, GLOBAL_QUERY_OPTS)
local function load_service_from_db(service_pk, ws_id)
local options = ws_id and { workspace = ws_id, show_ws_id = true }
local service, err = kong.db.services:select(service_pk, options)
if service == nil then
-- the third value means "do not cache"
return nil, err, -1
Expand Down Expand Up @@ -299,20 +300,21 @@ local function get_service_for_route(db, route, services_init_cache)
end

local err
local ws_id = route.ws_id

-- kong.core_cache is available, not in init phase
if kong.core_cache and db.strategy ~= "off" then
local cache_key = db.services:cache_key(service_pk.id, nil, nil, nil, nil,
route.ws_id)
ws_id)
service, err = kong.core_cache:get(cache_key, TTL_ZERO,
load_service_from_db, service_pk)
load_service_from_db, service_pk, ws_id)

else -- dbless or init phase, kong.core_cache not needed/available

-- A new service/route has been inserted while the initial route
-- was being created, on init (perhaps by a different Kong node).
-- Load the service individually and update services_init_cache with it
service, err = load_service_from_db(service_pk)
service, err = load_service_from_db(service_pk, ws_id)
services_init_cache[id] = service
end

Expand Down

1 comment on commit 5c78fb8

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Bazel Build

Docker image available kong/kong:5c78fb860c5fda1aa40d0b154c4e19944f3ddec8
Artifacts available https://github.com/Kong/kong/actions/runs/12542921415

Please sign in to comment.