From 2838ffd5c7ca9323bfa888ada11834ecec3b6911 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Fri, 11 Oct 2024 13:51:39 +0800 Subject: [PATCH] check get_latest_version() --- kong/clustering/services/sync/hooks.lua | 7 ++++++- kong/clustering/services/sync/rpc.lua | 7 ++++++- kong/clustering/services/sync/strategies/postgres.lua | 8 +++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/kong/clustering/services/sync/hooks.lua b/kong/clustering/services/sync/hooks.lua index 880294368256..ee20e422b7f5 100644 --- a/kong/clustering/services/sync/hooks.lua +++ b/kong/clustering/services/sync/hooks.lua @@ -53,7 +53,12 @@ end function _M:notify_all_nodes() - local latest_version = self.strategy:get_latest_version() + local latest_version, err = self.strategy:get_latest_version() + if not latest_version then + ngx_log(ngx_ERR, "can not get the latest version: ", err) + return + end + local msg = { default = { new_version = latest_version, }, } for _, node in ipairs(get_all_nodes_with_sync_cap()) do diff --git a/kong/clustering/services/sync/rpc.lua b/kong/clustering/services/sync/rpc.lua index dfcd6b358162..fdf6ae98529f 100644 --- a/kong/clustering/services/sync/rpc.lua +++ b/kong/clustering/services/sync/rpc.lua @@ -86,9 +86,14 @@ function _M:init_cp(manager) ngx_log(ngx_ERR, "unable to update clustering data plane status: ", err) end + local latest_version, err = self.strategy:get_latest_version() + if not latest_version then + return nil, err + end + -- is the node empty? If so, just do a full sync to bring it up to date faster if default_namespace_version == 0 or - self.strategy:get_latest_version() - default_namespace_version > FULL_SYNC_THRESHOLD + latest_version - default_namespace_version > FULL_SYNC_THRESHOLD then -- we need to full sync because holes are found diff --git a/kong/clustering/services/sync/strategies/postgres.lua b/kong/clustering/services/sync/strategies/postgres.lua index 2360c77970ae..0abd2da3882a 100644 --- a/kong/clustering/services/sync/strategies/postgres.lua +++ b/kong/clustering/services/sync/strategies/postgres.lua @@ -91,7 +91,13 @@ end function _M:get_latest_version() local sql = "SELECT MAX(version) AS max_version FROM clustering_sync_version" - return self.connector:query(sql)[1].max_version + + local res, err = self.connector:query(sql) + if not res then + return nil, err + end + + return res[1].max_version end