From 651b4acb01fc729a0380da8d303de1ea93ef2a8d Mon Sep 17 00:00:00 2001 From: Xiaochen Wang Date: Wed, 23 Oct 2024 19:32:41 +0800 Subject: [PATCH 1/3] fix(incremental sync): fix cache_key handling for select_by_cache_key --- kong/db/dao/init.lua | 2 +- kong/db/declarative/import.lua | 11 ++++------- kong/db/strategies/off/init.lua | 24 ++++++++++++++++-------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/kong/db/dao/init.lua b/kong/db/dao/init.lua index 4305be4a96f9..a20cb6813e7c 100644 --- a/kong/db/dao/init.lua +++ b/kong/db/dao/init.lua @@ -1555,7 +1555,7 @@ function DAO:cache_key(key, arg2, arg3, arg4, arg5, ws_id) error("key must be a string or an entity table", 2) end - if key.ws_id ~= nil and key.ws_id ~= null then + if key.ws_id ~= nil and key.ws_id ~= null and schema.workspaceable then ws_id = key.ws_id end diff --git a/kong/db/declarative/import.lua b/kong/db/declarative/import.lua index a9a222ee7a26..d812c44017f0 100644 --- a/kong/db/declarative/import.lua +++ b/kong/db/declarative/import.lua @@ -314,13 +314,10 @@ local function _set_entity_for_txn(t, entity_name, item, options, is_delete) -- select_by_cache_key if schema.cache_key then local cache_key = dao:cache_key(item) - - for _, wid in ipairs {ws_id, GLOBAL_WORKSPACE_TAG} do - local key = unique_field_key(entity_name, wid, "cache_key", cache_key) - - -- store item_key or nil into lmdb - t:set(key, idx_value) - end + -- ws_id is a placeholder here, because cache_key is unique globally. + local _ws_id = get_default_workspace() -- avoid overwriting the outer ws_id + local key = unique_field_key(entity_name, _ws_id, "cache_key", cache_key) + t:set(key, idx_value) end for fname, fdata in schema:each_field() do diff --git a/kong/db/strategies/off/init.lua b/kong/db/strategies/off/init.lua index d3c8176c5519..5568c6333375 100644 --- a/kong/db/strategies/off/init.lua +++ b/kong/db/strategies/off/init.lua @@ -208,18 +208,26 @@ local function select_by_field(self, field, value, options) _, value = next(value) end - local ws_id = workspace_id(schema, options) - - local key - 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) + local ws_id - if unique_across_ws then + if field == "cache_key" then + -- align with cache_key insertion logic in _set_entity_for_txn ws_id = get_default_workspace() + -- value = the value generated by `:cache_key()` + + else + ws_id = workspace_id(schema, options) + + 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) + + if unique_across_ws then + ws_id = get_default_workspace() + end end - key = unique_field_key(schema.name, ws_id, field, value) + local key = unique_field_key(schema.name, ws_id, field, value) return select_by_key(schema, key, true) end From 1f652d5c026dca4a2476b6735f95a57c0912cccb Mon Sep 17 00:00:00 2001 From: Xiaochen Wang Date: Thu, 31 Oct 2024 14:25:58 +0800 Subject: [PATCH 2/3] fix some comments --- kong/db/declarative/import.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/kong/db/declarative/import.lua b/kong/db/declarative/import.lua index d812c44017f0..43fa58a4e764 100644 --- a/kong/db/declarative/import.lua +++ b/kong/db/declarative/import.lua @@ -314,9 +314,11 @@ local function _set_entity_for_txn(t, entity_name, item, options, is_delete) -- select_by_cache_key if schema.cache_key then local cache_key = dao:cache_key(item) - -- ws_id is a placeholder here, because cache_key is unique globally. - local _ws_id = get_default_workspace() -- avoid overwriting the outer ws_id - local key = unique_field_key(entity_name, _ws_id, "cache_key", cache_key) + -- The second parameter (ws_id) is a placeholder here, because the cache_key + -- is already unique globally. + local key = unique_field_key(entity_name, get_default_workspace(), + "cache_key", cache_key) + -- store item_key or nil into lmdb t:set(key, idx_value) end From de9ced8ab06921e038f55a25dfb7db01eb0e2de4 Mon Sep 17 00:00:00 2001 From: Xiaochen Wang Date: Thu, 31 Oct 2024 14:27:18 +0800 Subject: [PATCH 3/3] fix: remove unnecessary comment --- kong/db/strategies/off/init.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/kong/db/strategies/off/init.lua b/kong/db/strategies/off/init.lua index 5568c6333375..d15a2c6c7aa4 100644 --- a/kong/db/strategies/off/init.lua +++ b/kong/db/strategies/off/init.lua @@ -213,7 +213,6 @@ local function select_by_field(self, field, value, options) if field == "cache_key" then -- align with cache_key insertion logic in _set_entity_for_txn ws_id = get_default_workspace() - -- value = the value generated by `:cache_key()` else ws_id = workspace_id(schema, options)