From 0d7a1247b3817015e7f82c2b2f399dd774bc6125 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Fri, 3 Jan 2025 06:31:54 +0800 Subject: [PATCH 01/19] version is string --- kong/clustering/services/sync/strategies/postgres.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kong/clustering/services/sync/strategies/postgres.lua b/kong/clustering/services/sync/strategies/postgres.lua index 7bc0784c6e6..a6290ab4946 100644 --- a/kong/clustering/services/sync/strategies/postgres.lua +++ b/kong/clustering/services/sync/strategies/postgres.lua @@ -2,6 +2,7 @@ local _M = {} local _MT = { __index = _M } +local fmt = string.format local ngx_null = ngx.null @@ -44,10 +45,10 @@ function _M:get_latest_version() local ver = res[1] and res[1].max if ver == ngx_null then - return 0 + return fmt("%032d", 0) end - return ver + return fmt("%032d", ver) end From 5a9acc61305959cee25d30c70bc2b82590de18c3 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Fri, 3 Jan 2025 09:59:17 +0800 Subject: [PATCH 02/19] change version usage --- kong/clustering/services/sync/rpc.lua | 26 +++++++++++++------------- kong/db/declarative/import.lua | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/kong/clustering/services/sync/rpc.lua b/kong/clustering/services/sync/rpc.lua index c84a10ecd76..a192dcc356f 100644 --- a/kong/clustering/services/sync/rpc.lua +++ b/kong/clustering/services/sync/rpc.lua @@ -14,6 +14,7 @@ local insert_entity_for_txn = declarative.insert_entity_for_txn local delete_entity_for_txn = declarative.delete_entity_for_txn local DECLARATIVE_HASH_KEY = constants.DECLARATIVE_HASH_KEY local CLUSTERING_DATA_PLANES_LATEST_VERSION_KEY = constants.CLUSTERING_DATA_PLANES_LATEST_VERSION_KEY +local DECLARATIVE_EMPTY_CONFIG_HASH = constants.DECLARATIVE_EMPTY_CONFIG_HASH local DECLARATIVE_DEFAULT_WORKSPACE_KEY = constants.DECLARATIVE_DEFAULT_WORKSPACE_KEY local CLUSTERING_SYNC_STATUS = constants.CLUSTERING_SYNC_STATUS local SYNC_MUTEX_OPTS = { name = "get_delta", timeout = 0, } @@ -22,7 +23,6 @@ local MAX_RETRY = 5 local assert = assert local ipairs = ipairs -local fmt = string.format local ngx_null = ngx.null local ngx_log = ngx.log local ngx_ERR = ngx.ERR @@ -60,7 +60,7 @@ function _M:init_cp(manager) -- CP -- Method: kong.sync.v2.get_delta -- Params: versions: list of current versions of the database - -- example: { default = { version = 1000, }, } + -- example: { default = { version = "1000", }, } manager.callbacks:register("kong.sync.v2.get_delta", function(node_id, current_versions) ngx_log(ngx_DEBUG, "[kong.sync.v2] config push (connected client)") @@ -75,7 +75,7 @@ function _M:init_cp(manager) return nil, "default namespace does not exist inside params" end - -- { default = { version = 1000, }, } + -- { default = { version = "1000", }, } local default_namespace_version = default_namespace.version local node_info = assert(kong.rpc:get_peer_info(node_id)) @@ -88,7 +88,7 @@ function _M:init_cp(manager) labels = node_info.labels, -- get from rpc call cert_details = node_info.cert_details, -- get from rpc call sync_status = CLUSTERING_SYNC_STATUS.NORMAL, - config_hash = fmt("%032d", default_namespace_version), + config_hash = default_namespace_version, rpc_capabilities = rpc_peers and rpc_peers[node_id] or {}, }, { ttl = purge_delay, no_broadcast_crud_event = true, }) if not ok then @@ -100,7 +100,7 @@ function _M:init_cp(manager) return nil, err end - if default_namespace_version == 0 or + if default_namespace_version == DECLARATIVE_EMPTY_CONFIG_HASH or default_namespace_version < latest_version then return full_sync_result() end @@ -115,7 +115,7 @@ function _M:init_dp(manager) -- DP -- Method: kong.sync.v2.notify_new_version -- Params: new_versions: list of namespaces and their new versions, like: - -- { default = { new_version = 1000, }, } + -- { default = { new_version = "1000", }, } manager.callbacks:register("kong.sync.v2.notify_new_version", function(node_id, new_versions) -- TODO: currently only default is supported, and anything else is ignored local default_new_version = new_versions.default @@ -128,7 +128,7 @@ function _M:init_dp(manager) return nil, "'new_version' key does not exist" end - local lmdb_ver = tonumber(declarative.get_current_hash()) or 0 + local lmdb_ver = declarative.get_current_hash() if lmdb_ver < version then -- set lastest version to shm kong_shm:set(CLUSTERING_DATA_PLANES_LATEST_VERSION_KEY, version) @@ -174,7 +174,7 @@ local function do_sync() local msg = { default = { version = - tonumber(declarative.get_current_hash()) or 0, + declarative.get_current_hash(), }, } @@ -226,13 +226,13 @@ local function do_sync() local db = kong.db - local version = 0 + local version = "" local opts = {} local crud_events = {} local crud_events_n = 0 -- delta should look like: - -- { type = ..., entity = { ... }, version = 1, ws_id = ..., } + -- { type = ..., entity = { ... }, version = "1", ws_id = ..., } for _, delta in ipairs(deltas) do local delta_version = delta.version local delta_type = delta.type @@ -308,7 +308,7 @@ local function do_sync() end -- delta.version should not be nil or ngx.null - assert(type(delta_version) == "number") + assert(type(delta_version) == "string") if delta_version ~= version then version = delta_version @@ -316,7 +316,7 @@ local function do_sync() end -- for _, delta -- store current sync version - t:set(DECLARATIVE_HASH_KEY, fmt("%032d", version)) + t:set(DECLARATIVE_HASH_KEY, version) -- store the correct default workspace uuid if default_ws_changed then @@ -391,7 +391,7 @@ function sync_once_impl(premature, retry_count) return end - local current_version = tonumber(declarative.get_current_hash()) or 0 + local current_version = declarative.get_current_hash() if current_version >= latest_notified_version then ngx_log(ngx_DEBUG, "version already updated") return diff --git a/kong/db/declarative/import.lua b/kong/db/declarative/import.lua index 1b2bfc72713..d502b550bc8 100644 --- a/kong/db/declarative/import.lua +++ b/kong/db/declarative/import.lua @@ -231,7 +231,7 @@ end local function get_current_hash() - return lmdb.get(DECLARATIVE_HASH_KEY) + return lmdb.get(DECLARATIVE_HASH_KEY) or DECLARATIVE_EMPTY_CONFIG_HASH end From 46b6747f35cb7f162f99135f471e1a6f0778bbf1 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Fri, 3 Jan 2025 10:08:51 +0800 Subject: [PATCH 03/19] clean --- kong/clustering/services/sync/rpc.lua | 6 +++--- kong/db/declarative/import.lua | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/kong/clustering/services/sync/rpc.lua b/kong/clustering/services/sync/rpc.lua index a192dcc356f..6cc8835b485 100644 --- a/kong/clustering/services/sync/rpc.lua +++ b/kong/clustering/services/sync/rpc.lua @@ -128,7 +128,7 @@ function _M:init_dp(manager) return nil, "'new_version' key does not exist" end - local lmdb_ver = declarative.get_current_hash() + local lmdb_ver = declarative.get_current_hash() or DECLARATIVE_EMPTY_CONFIG_HASH if lmdb_ver < version then -- set lastest version to shm kong_shm:set(CLUSTERING_DATA_PLANES_LATEST_VERSION_KEY, version) @@ -174,7 +174,7 @@ local function do_sync() local msg = { default = { version = - declarative.get_current_hash(), + declarative.get_current_hash() or DECLARATIVE_EMPTY_CONFIG_HASH, }, } @@ -391,7 +391,7 @@ function sync_once_impl(premature, retry_count) return end - local current_version = declarative.get_current_hash() + local current_version = declarative.get_current_hash() or DECLARATIVE_EMPTY_CONFIG_HASH if current_version >= latest_notified_version then ngx_log(ngx_DEBUG, "version already updated") return diff --git a/kong/db/declarative/import.lua b/kong/db/declarative/import.lua index d502b550bc8..1b2bfc72713 100644 --- a/kong/db/declarative/import.lua +++ b/kong/db/declarative/import.lua @@ -231,7 +231,7 @@ end local function get_current_hash() - return lmdb.get(DECLARATIVE_HASH_KEY) or DECLARATIVE_EMPTY_CONFIG_HASH + return lmdb.get(DECLARATIVE_HASH_KEY) end From 20e10f161305b0d8da9d6000320889df49f12959 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Fri, 3 Jan 2025 10:30:33 +0800 Subject: [PATCH 04/19] fix 05-sync-rpc_spec.lua --- spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua b/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua index 26aa9b4b5b1..ab97d6008de 100644 --- a/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua +++ b/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua @@ -1,3 +1,4 @@ +local constants = require("kong.constants") local helpers = require("spec.helpers") local misc = require("spec.internal.misc") local cp = require("spec.helpers.rpc_mock.cp") @@ -5,6 +6,7 @@ local dp = require("spec.helpers.rpc_mock.dp") local setup = require("spec.helpers.rpc_mock.setup") local get_node_id = misc.get_node_id local DP_PREFIX = "servroot_dp" +local DECLARATIVE_EMPTY_CONFIG_HASH = constants.DECLARATIVE_EMPTY_CONFIG_HASH local function change_config() -- the initial sync is flaky. let's trigger a sync by creating a service @@ -74,12 +76,12 @@ describe("kong.sync.v2", function() local called = false mocked_cp:mock("kong.sync.v2.get_delta", function(node_id, payload) called = true - return { default = { version = 100, deltas = {} } } + return { default = { version = "100", deltas = {} } } end) -- make a call from the mocked cp -- CP->DP: notify_new_version - assert(mocked_cp:call(node_id, "kong.sync.v2.notify_new_version", { default = { new_version = 100, } })) + assert(mocked_cp:call(node_id, "kong.sync.v2.notify_new_version", { default = { new_version = "100", } })) -- DP->CP: get_delta -- the dp after receiving the notification will make a call to the cp @@ -120,7 +122,7 @@ describe("kong.sync.v2", function() -- this is a workaround to registers the data plane node -- CP does not register the DP node until it receives a call from the DP function register_dp() - local res, err = mocked_dp:call("control_plane", "kong.sync.v2.get_delta", { default = { version = 0,},}) + local res, err = mocked_dp:call("control_plane", "kong.sync.v2.get_delta", { default = { version = "0",},}) assert.is_nil(err) assert.is_table(res and res.default and res.default.deltas) end @@ -132,7 +134,7 @@ describe("kong.sync.v2", function() end) it("rpc call", function() - local res, err = mocked_dp:call("control_plane", "kong.sync.v2.get_delta", { default = { version = 0,},}) + local res, err = mocked_dp:call("control_plane", "kong.sync.v2.get_delta", { default = { version = "0",},}) assert.is_nil(err) assert.is_table(res and res.default and res.default.deltas) From 2cd7d4f9a1987b2541b9692197ab6addf0a4246e Mon Sep 17 00:00:00 2001 From: chronolaw Date: Fri, 3 Jan 2025 10:31:16 +0800 Subject: [PATCH 05/19] clean --- spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua b/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua index ab97d6008de..bd992c38ccc 100644 --- a/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua +++ b/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua @@ -1,4 +1,3 @@ -local constants = require("kong.constants") local helpers = require("spec.helpers") local misc = require("spec.internal.misc") local cp = require("spec.helpers.rpc_mock.cp") @@ -6,7 +5,6 @@ local dp = require("spec.helpers.rpc_mock.dp") local setup = require("spec.helpers.rpc_mock.setup") local get_node_id = misc.get_node_id local DP_PREFIX = "servroot_dp" -local DECLARATIVE_EMPTY_CONFIG_HASH = constants.DECLARATIVE_EMPTY_CONFIG_HASH local function change_config() -- the initial sync is flaky. let's trigger a sync by creating a service From 0de82277e91af66a24dd0a2a20d52e760828f723 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Fri, 3 Jan 2025 12:35:09 +0800 Subject: [PATCH 06/19] fix tests --- .../18-hybrid_rpc/05-sync-rpc_spec.lua | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua b/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua index bd992c38ccc..ed6dad45493 100644 --- a/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua +++ b/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua @@ -6,6 +6,13 @@ local setup = require("spec.helpers.rpc_mock.setup") local get_node_id = misc.get_node_id local DP_PREFIX = "servroot_dp" + +-- now version must be a string +local function fmt(v) + return string.format("%032d", v) +end + + local function change_config() -- the initial sync is flaky. let's trigger a sync by creating a service local admin_client = helpers.admin_client() @@ -74,12 +81,12 @@ describe("kong.sync.v2", function() local called = false mocked_cp:mock("kong.sync.v2.get_delta", function(node_id, payload) called = true - return { default = { version = "100", deltas = {} } } + return { default = { version = fmt(100), deltas = {} } } end) -- make a call from the mocked cp -- CP->DP: notify_new_version - assert(mocked_cp:call(node_id, "kong.sync.v2.notify_new_version", { default = { new_version = "100", } })) + assert(mocked_cp:call(node_id, "kong.sync.v2.notify_new_version", { default = { new_version = fmt(100), } })) -- DP->CP: get_delta -- the dp after receiving the notification will make a call to the cp @@ -120,7 +127,7 @@ describe("kong.sync.v2", function() -- this is a workaround to registers the data plane node -- CP does not register the DP node until it receives a call from the DP function register_dp() - local res, err = mocked_dp:call("control_plane", "kong.sync.v2.get_delta", { default = { version = "0",},}) + local res, err = mocked_dp:call("control_plane", "kong.sync.v2.get_delta", { default = { version = fmt(0),},}) assert.is_nil(err) assert.is_table(res and res.default and res.default.deltas) end @@ -132,7 +139,7 @@ describe("kong.sync.v2", function() end) it("rpc call", function() - local res, err = mocked_dp:call("control_plane", "kong.sync.v2.get_delta", { default = { version = "0",},}) + local res, err = mocked_dp:call("control_plane", "kong.sync.v2.get_delta", { default = { version = fmt(0),},}) assert.is_nil(err) assert.is_table(res and res.default and res.default.deltas) From 6bd7ecd3839a6e8cfd7c0d81fc4ee3f1b692e935 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Fri, 3 Jan 2025 12:41:30 +0800 Subject: [PATCH 07/19] VERSION_FMT --- kong/clustering/services/sync/strategies/postgres.lua | 9 +++++++-- spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/kong/clustering/services/sync/strategies/postgres.lua b/kong/clustering/services/sync/strategies/postgres.lua index a6290ab4946..f46904de212 100644 --- a/kong/clustering/services/sync/strategies/postgres.lua +++ b/kong/clustering/services/sync/strategies/postgres.lua @@ -6,6 +6,11 @@ local fmt = string.format local ngx_null = ngx.null +-- version string should be greater than any hex string(ngx.md5) +-- e.g.: "R000" > "FFFF" +local VERSION_FMT = "R%031d" + + function _M.new(db) local self = { connector = db.connector, @@ -45,10 +50,10 @@ function _M:get_latest_version() local ver = res[1] and res[1].max if ver == ngx_null then - return fmt("%032d", 0) + return fmt(VERSION_FMT, 0) end - return fmt("%032d", ver) + return fmt(VERSION_FMT, ver) end diff --git a/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua b/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua index ed6dad45493..a8b3638bdf5 100644 --- a/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua +++ b/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua @@ -9,7 +9,7 @@ local DP_PREFIX = "servroot_dp" -- now version must be a string local function fmt(v) - return string.format("%032d", v) + return string.format("R%031d", v) end From 4e92824b78bc1d7c382e1e3b1eda42b9bd91a4b9 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Fri, 3 Jan 2025 13:03:54 +0800 Subject: [PATCH 08/19] get_current_hash --- kong/clustering/services/sync/rpc.lua | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/kong/clustering/services/sync/rpc.lua b/kong/clustering/services/sync/rpc.lua index 6cc8835b485..15472133665 100644 --- a/kong/clustering/services/sync/rpc.lua +++ b/kong/clustering/services/sync/rpc.lua @@ -54,6 +54,11 @@ local function full_sync_result() end +local function get_current_hash() + return declarative.get_current_hash() or DECLARATIVE_EMPTY_CONFIG_HASH +end + + function _M:init_cp(manager) local purge_delay = manager.conf.cluster_data_plane_purge_delay @@ -128,7 +133,7 @@ function _M:init_dp(manager) return nil, "'new_version' key does not exist" end - local lmdb_ver = declarative.get_current_hash() or DECLARATIVE_EMPTY_CONFIG_HASH + local lmdb_ver = get_current_hash() if lmdb_ver < version then -- set lastest version to shm kong_shm:set(CLUSTERING_DATA_PLANES_LATEST_VERSION_KEY, version) @@ -172,11 +177,7 @@ local function do_sync() return nil, "rpc is not ready" end - local msg = { default = - { version = - declarative.get_current_hash() or DECLARATIVE_EMPTY_CONFIG_HASH, - }, - } + local msg = { default = { version = get_current_hash(), }, } local ns_deltas, err = kong.rpc:call("control_plane", "kong.sync.v2.get_delta", msg) if not ns_deltas then @@ -391,7 +392,7 @@ function sync_once_impl(premature, retry_count) return end - local current_version = declarative.get_current_hash() or DECLARATIVE_EMPTY_CONFIG_HASH + local current_version = get_current_hash() if current_version >= latest_notified_version then ngx_log(ngx_DEBUG, "version already updated") return From 8d8b89cef10dad6f153b1f63308e4a53043cc796 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Fri, 3 Jan 2025 13:05:55 +0800 Subject: [PATCH 09/19] get_current_version --- kong/clustering/services/sync/rpc.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kong/clustering/services/sync/rpc.lua b/kong/clustering/services/sync/rpc.lua index 15472133665..d6e6f295ba6 100644 --- a/kong/clustering/services/sync/rpc.lua +++ b/kong/clustering/services/sync/rpc.lua @@ -54,7 +54,7 @@ local function full_sync_result() end -local function get_current_hash() +local function get_current_version() return declarative.get_current_hash() or DECLARATIVE_EMPTY_CONFIG_HASH end @@ -133,7 +133,7 @@ function _M:init_dp(manager) return nil, "'new_version' key does not exist" end - local lmdb_ver = get_current_hash() + local lmdb_ver = get_current_version() if lmdb_ver < version then -- set lastest version to shm kong_shm:set(CLUSTERING_DATA_PLANES_LATEST_VERSION_KEY, version) @@ -177,7 +177,7 @@ local function do_sync() return nil, "rpc is not ready" end - local msg = { default = { version = get_current_hash(), }, } + local msg = { default = { version = get_current_version(), }, } local ns_deltas, err = kong.rpc:call("control_plane", "kong.sync.v2.get_delta", msg) if not ns_deltas then @@ -392,7 +392,7 @@ function sync_once_impl(premature, retry_count) return end - local current_version = get_current_hash() + local current_version = get_current_version() if current_version >= latest_notified_version then ngx_log(ngx_DEBUG, "version already updated") return From 7b8bc6f269da200fae6165b2db1aebfc0848deed Mon Sep 17 00:00:00 2001 From: chronolaw Date: Fri, 3 Jan 2025 13:07:52 +0800 Subject: [PATCH 10/19] is_empty_version --- kong/clustering/services/sync/rpc.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/kong/clustering/services/sync/rpc.lua b/kong/clustering/services/sync/rpc.lua index d6e6f295ba6..ef83aa273e5 100644 --- a/kong/clustering/services/sync/rpc.lua +++ b/kong/clustering/services/sync/rpc.lua @@ -59,6 +59,11 @@ local function get_current_version() end +local function is_empty_version(v) + return v == DECLARATIVE_EMPTY_CONFIG_HASH +end + + function _M:init_cp(manager) local purge_delay = manager.conf.cluster_data_plane_purge_delay @@ -105,7 +110,7 @@ function _M:init_cp(manager) return nil, err end - if default_namespace_version == DECLARATIVE_EMPTY_CONFIG_HASH or + if is_empty_version(default_namespace_version) or default_namespace_version < latest_version then return full_sync_result() end From 3190c9daf515f2f74603432c67c2bdc85eba6e92 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Mon, 6 Jan 2025 10:44:35 +0800 Subject: [PATCH 11/19] is_empty_version --- kong/clustering/services/sync/rpc.lua | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/kong/clustering/services/sync/rpc.lua b/kong/clustering/services/sync/rpc.lua index ef83aa273e5..911285793d7 100644 --- a/kong/clustering/services/sync/rpc.lua +++ b/kong/clustering/services/sync/rpc.lua @@ -59,8 +59,15 @@ local function get_current_version() end -local function is_empty_version(v) - return v == DECLARATIVE_EMPTY_CONFIG_HASH +local is_empty_version +do + local byte = string.byte + local CHAR_R = byte("R") + + -- version string must start with char 'R' + is_empty_version = function(v) + return byte(v) ~= CHAR_R + end end From 98d9c094b41bf4a86649fb1708c89cb641968152 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Mon, 6 Jan 2025 11:15:48 +0800 Subject: [PATCH 12/19] prefix V --- kong/clustering/services/sync/rpc.lua | 6 +++--- kong/clustering/services/sync/strategies/postgres.lua | 4 ++-- spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/kong/clustering/services/sync/rpc.lua b/kong/clustering/services/sync/rpc.lua index 911285793d7..035aed2c8a1 100644 --- a/kong/clustering/services/sync/rpc.lua +++ b/kong/clustering/services/sync/rpc.lua @@ -62,11 +62,11 @@ end local is_empty_version do local byte = string.byte - local CHAR_R = byte("R") + local CHAR_V = byte("V") - -- version string must start with char 'R' + -- version string must start with char 'V' is_empty_version = function(v) - return byte(v) ~= CHAR_R + return byte(v) ~= CHAR_V end end diff --git a/kong/clustering/services/sync/strategies/postgres.lua b/kong/clustering/services/sync/strategies/postgres.lua index f46904de212..632f1903a96 100644 --- a/kong/clustering/services/sync/strategies/postgres.lua +++ b/kong/clustering/services/sync/strategies/postgres.lua @@ -7,8 +7,8 @@ local ngx_null = ngx.null -- version string should be greater than any hex string(ngx.md5) --- e.g.: "R000" > "FFFF" -local VERSION_FMT = "R%031d" +-- e.g.: "V000" > "FFFF" +local VERSION_FMT = "V%031d" function _M.new(db) diff --git a/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua b/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua index a8b3638bdf5..3dfe82fceea 100644 --- a/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua +++ b/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua @@ -9,7 +9,7 @@ local DP_PREFIX = "servroot_dp" -- now version must be a string local function fmt(v) - return string.format("R%031d", v) + return string.format("V%031d", v) end From f189dac232321ae59c1aaa53bc788b9751d17852 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Mon, 6 Jan 2025 15:15:42 +0800 Subject: [PATCH 13/19] is_invalid_version --- kong/clustering/services/sync/rpc.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/kong/clustering/services/sync/rpc.lua b/kong/clustering/services/sync/rpc.lua index 035aed2c8a1..17f3e0cfaaf 100644 --- a/kong/clustering/services/sync/rpc.lua +++ b/kong/clustering/services/sync/rpc.lua @@ -59,13 +59,13 @@ local function get_current_version() end -local is_empty_version +local is_invalid_version do local byte = string.byte local CHAR_V = byte("V") -- version string must start with char 'V' - is_empty_version = function(v) + is_invalid_version = function(v) return byte(v) ~= CHAR_V end end @@ -117,7 +117,8 @@ function _M:init_cp(manager) return nil, err end - if is_empty_version(default_namespace_version) or + -- string comparison effectively does the same as number comparison + if is_invalid_version(default_namespace_version) or default_namespace_version < latest_version then return full_sync_result() end From 7a517d0b344245fa36615b49579bdc5feac3661f Mon Sep 17 00:00:00 2001 From: chronolaw Date: Mon, 6 Jan 2025 17:35:19 +0800 Subject: [PATCH 14/19] prefix is V02_ --- kong/clustering/services/sync/rpc.lua | 8 ++++---- kong/clustering/services/sync/strategies/postgres.lua | 4 ++-- spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/kong/clustering/services/sync/rpc.lua b/kong/clustering/services/sync/rpc.lua index 17f3e0cfaaf..db54f3d2475 100644 --- a/kong/clustering/services/sync/rpc.lua +++ b/kong/clustering/services/sync/rpc.lua @@ -61,12 +61,12 @@ end local is_invalid_version do - local byte = string.byte - local CHAR_V = byte("V") + local sub = string.sub + local VER_PREFIX = "V02_" - -- version string must start with char 'V' + -- version string must start with 'V02_' is_invalid_version = function(v) - return byte(v) ~= CHAR_V + return sub(v, 1, 4) ~= VER_PREFIX end end diff --git a/kong/clustering/services/sync/strategies/postgres.lua b/kong/clustering/services/sync/strategies/postgres.lua index 632f1903a96..f74ea71206c 100644 --- a/kong/clustering/services/sync/strategies/postgres.lua +++ b/kong/clustering/services/sync/strategies/postgres.lua @@ -7,8 +7,8 @@ local ngx_null = ngx.null -- version string should be greater than any hex string(ngx.md5) --- e.g.: "V000" > "FFFF" -local VERSION_FMT = "V%031d" +-- e.g.: "V02_0000" > "FFFFFFFF" +local VERSION_FMT = "V02_%028d" function _M.new(db) diff --git a/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua b/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua index 3dfe82fceea..d3780b2ed12 100644 --- a/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua +++ b/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua @@ -9,7 +9,7 @@ local DP_PREFIX = "servroot_dp" -- now version must be a string local function fmt(v) - return string.format("V%031d", v) + return string.format("V02_%028d", v) end From 89865aeed6b70e2f9eceebb781c4a06e00c029c1 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Mon, 6 Jan 2025 17:37:51 +0800 Subject: [PATCH 15/19] is_valid_version --- kong/clustering/services/sync/rpc.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kong/clustering/services/sync/rpc.lua b/kong/clustering/services/sync/rpc.lua index db54f3d2475..a3873950db3 100644 --- a/kong/clustering/services/sync/rpc.lua +++ b/kong/clustering/services/sync/rpc.lua @@ -59,14 +59,14 @@ local function get_current_version() end -local is_invalid_version +local is_valid_version do local sub = string.sub local VER_PREFIX = "V02_" -- version string must start with 'V02_' - is_invalid_version = function(v) - return sub(v, 1, 4) ~= VER_PREFIX + is_valid_version = function(v) + return sub(v, 1, 4) == VER_PREFIX end end @@ -118,7 +118,7 @@ function _M:init_cp(manager) end -- string comparison effectively does the same as number comparison - if is_invalid_version(default_namespace_version) or + if not is_valid_version(default_namespace_version) or default_namespace_version < latest_version then return full_sync_result() end From 608ba4dd8fd2fba8253e525895db513d2b55d1e2 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Wed, 8 Jan 2025 07:01:21 +0800 Subject: [PATCH 16/19] clean --- kong/clustering/services/sync/rpc.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kong/clustering/services/sync/rpc.lua b/kong/clustering/services/sync/rpc.lua index a3873950db3..9af63560ad9 100644 --- a/kong/clustering/services/sync/rpc.lua +++ b/kong/clustering/services/sync/rpc.lua @@ -23,6 +23,7 @@ local MAX_RETRY = 5 local assert = assert local ipairs = ipairs +local sub = string.sub local ngx_null = ngx.null local ngx_log = ngx.log local ngx_ERR = ngx.ERR @@ -61,7 +62,6 @@ end local is_valid_version do - local sub = string.sub local VER_PREFIX = "V02_" -- version string must start with 'V02_' From 240beb4110f89862b213a7347c66f7972977d902 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Wed, 8 Jan 2025 10:29:10 +0800 Subject: [PATCH 17/19] change to v02 --- kong/clustering/services/sync/rpc.lua | 4 ++-- kong/clustering/services/sync/strategies/postgres.lua | 4 ++-- spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/kong/clustering/services/sync/rpc.lua b/kong/clustering/services/sync/rpc.lua index 9af63560ad9..cd1db7fe75c 100644 --- a/kong/clustering/services/sync/rpc.lua +++ b/kong/clustering/services/sync/rpc.lua @@ -62,9 +62,9 @@ end local is_valid_version do - local VER_PREFIX = "V02_" + local VER_PREFIX = "v02_" - -- version string must start with 'V02_' + -- version string must start with 'v02_' is_valid_version = function(v) return sub(v, 1, 4) == VER_PREFIX end diff --git a/kong/clustering/services/sync/strategies/postgres.lua b/kong/clustering/services/sync/strategies/postgres.lua index f74ea71206c..b7cbe4830d2 100644 --- a/kong/clustering/services/sync/strategies/postgres.lua +++ b/kong/clustering/services/sync/strategies/postgres.lua @@ -7,8 +7,8 @@ local ngx_null = ngx.null -- version string should be greater than any hex string(ngx.md5) --- e.g.: "V02_0000" > "FFFFFFFF" -local VERSION_FMT = "V02_%028d" +-- e.g.: "v02_0000" > "FFFFFFFF" +local VERSION_FMT = "v02_%028d" function _M.new(db) diff --git a/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua b/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua index d3780b2ed12..5137ca5574a 100644 --- a/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua +++ b/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua @@ -9,7 +9,7 @@ local DP_PREFIX = "servroot_dp" -- now version must be a string local function fmt(v) - return string.format("V02_%028d", v) + return string.format("v02_%028d", v) end From 4f0db45e1126a8a045095420a9dd15aec84a42fe Mon Sep 17 00:00:00 2001 From: chronolaw Date: Fri, 10 Jan 2025 16:28:43 +0800 Subject: [PATCH 18/19] format hex --- kong/clustering/services/sync/strategies/postgres.lua | 2 +- spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kong/clustering/services/sync/strategies/postgres.lua b/kong/clustering/services/sync/strategies/postgres.lua index b7cbe4830d2..2c2f7a37047 100644 --- a/kong/clustering/services/sync/strategies/postgres.lua +++ b/kong/clustering/services/sync/strategies/postgres.lua @@ -8,7 +8,7 @@ local ngx_null = ngx.null -- version string should be greater than any hex string(ngx.md5) -- e.g.: "v02_0000" > "FFFFFFFF" -local VERSION_FMT = "v02_%028d" +local VERSION_FMT = "v02_%028x" function _M.new(db) diff --git a/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua b/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua index 5137ca5574a..c9559c6601d 100644 --- a/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua +++ b/spec/02-integration/18-hybrid_rpc/05-sync-rpc_spec.lua @@ -9,7 +9,7 @@ local DP_PREFIX = "servroot_dp" -- now version must be a string local function fmt(v) - return string.format("v02_%028d", v) + return string.format("v02_%028x", v) end From c84e5320484f098a082af425af2033b85603010c Mon Sep 17 00:00:00 2001 From: chronolaw Date: Fri, 10 Jan 2025 16:58:42 +0800 Subject: [PATCH 19/19] update comment --- kong/clustering/services/sync/strategies/postgres.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/kong/clustering/services/sync/strategies/postgres.lua b/kong/clustering/services/sync/strategies/postgres.lua index 2c2f7a37047..20e594d92f3 100644 --- a/kong/clustering/services/sync/strategies/postgres.lua +++ b/kong/clustering/services/sync/strategies/postgres.lua @@ -6,8 +6,7 @@ local fmt = string.format local ngx_null = ngx.null --- version string should be greater than any hex string(ngx.md5) --- e.g.: "v02_0000" > "FFFFFFFF" +-- version string should look like: "v02_0000" local VERSION_FMT = "v02_%028x"