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

perf(clustering): store entity only once in lmdb #12036

Closed
wants to merge 1 commit into from
Closed
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
26 changes: 13 additions & 13 deletions kong/db/declarative/import.lua
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ local function load_into_cache(entities, meta, hash)

assert(type(ws_id) == "string")

local cache_key = dao:cache_key(id, nil, nil, nil, nil, item.ws_id)
local item_cache_key = dao:cache_key(id, nil, nil, nil, nil, item.ws_id)

item = remove_nulls(item)
if transform then
Expand All @@ -267,30 +267,30 @@ local function load_into_cache(entities, meta, hash)
return nil, err
end

t:set(cache_key, item_marshalled)
t:set(item_cache_key, item_marshalled)

local global_query_cache_key = dao:cache_key(id, nil, nil, nil, nil, "*")
t:set(global_query_cache_key, item_marshalled)
t:set(global_query_cache_key, item_cache_key)

-- insert individual entry for global query
insert(keys_by_ws["*"], cache_key)
insert(keys_by_ws["*"], item_cache_key)

-- insert individual entry for workspaced query
if ws_id ~= "" then
keys_by_ws[ws_id] = keys_by_ws[ws_id] or {}
local keys = keys_by_ws[ws_id]
insert(keys, cache_key)
insert(keys, item_cache_key)
end

if schema.cache_key then
local cache_key = dao:cache_key(item)
t:set(cache_key, item_marshalled)
t:set(cache_key, item_cache_key)
end

for i = 1, #uniques do
local unique = uniques[i]
local unique_key = item[unique]
if unique_key then
if unique_key and unique_key ~= null then
if type(unique_key) == "table" then
local _
-- this assumes that foreign keys are not composite
Expand All @@ -300,31 +300,31 @@ local function load_into_cache(entities, meta, hash)
local key = unique_field_key(entity_name, ws_id, unique, unique_key,
schema.fields[unique].unique_across_ws)

t:set(key, item_marshalled)
t:set(key, item_cache_key)
end
end

for fname, ref in pairs(foreign_fields) do
local item_fname = item[fname]
if item_fname then
if item_fname and item_fname ~= null then
local fschema = db[ref].schema

local fid = declarative_config.pk_string(fschema, item_fname)

-- insert paged search entry for global query
page_for[ref]["*"] = page_for[ref]["*"] or {}
page_for[ref]["*"][fid] = page_for[ref]["*"][fid] or {}
insert(page_for[ref]["*"][fid], cache_key)
insert(page_for[ref]["*"][fid], item_cache_key)

-- insert paged search entry for workspaced query
page_for[ref][ws_id] = page_for[ref][ws_id] or {}
page_for[ref][ws_id][fid] = page_for[ref][ws_id][fid] or {}
insert(page_for[ref][ws_id][fid], cache_key)
insert(page_for[ref][ws_id][fid], item_cache_key)
end
end

local item_tags = item.tags
if item_tags then
if item_tags and item_tags ~= null then
local ws = schema.workspaceable and ws_id or ""
for i = 1, #item_tags do
local tag_name = item_tags[i]
Expand All @@ -335,7 +335,7 @@ local function load_into_cache(entities, meta, hash)

taggings[tag_name] = taggings[tag_name] or {}
taggings[tag_name][ws] = taggings[tag_name][ws] or {}
taggings[tag_name][ws][cache_key] = true
taggings[tag_name][ws][item_cache_key] = true
end
end
end
Expand Down
21 changes: 17 additions & 4 deletions kong/db/strategies/off/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@ local function select_by_key(schema, key)
end


local function select_by_ref(schema, ref)
local key, err = lmdb_get(ref)
if not key then
return nil, err
end

return select_by_key(schema, key)
end


local function page(self, size, offset, options)
local schema = self.schema
local ws_id = ws(schema, options)
Expand All @@ -258,6 +268,9 @@ local function select(self, pk, options)
local ws_id = ws(schema, options)
local id = declarative_config.pk_string(schema, pk)
local key = schema.name .. ":" .. id .. ":::::" .. ws_id
if ws_id == "*" then
return select_by_ref(schema, key)
end
return select_by_key(schema, key)
end

Expand All @@ -271,19 +284,19 @@ local function select_by_field(self, field, value, options)
local schema = self.schema
local ws_id = ws(schema, options)

local key
local ref
if field ~= "cache_key" then
local unique_across_ws = schema.fields[field].unique_across_ws
-- only accept global query by field if field is unique across workspaces
assert(not options or options.workspace ~= null or unique_across_ws)

key = unique_field_key(schema.name, ws_id, field, value, unique_across_ws)
ref = unique_field_key(schema.name, ws_id, field, value, unique_across_ws)
else
-- if select_by_cache_key, use the provided cache_key as key directly
key = value
ref = value
end

return select_by_key(schema, key)
return select_by_ref(schema, ref)
end


Expand Down
Loading