diff --git a/kong/clustering/services/sync/hooks.lua b/kong/clustering/services/sync/hooks.lua index 96612d4927c..2debe66eb6d 100644 --- a/kong/clustering/services/sync/hooks.lua +++ b/kong/clustering/services/sync/hooks.lua @@ -3,12 +3,14 @@ local _MT = { __index = _M, } local hooks = require("kong.hooks") -local EMPTY = require("kong.tools.table").EMPTY +local kong_table = require("kong.tools.table") +local EMPTY = kong_table.EMPTY local ipairs = ipairs local ngx_log = ngx.log local ngx_ERR = ngx.ERR +local ngx_WARN = ngx.WARN local ngx_DEBUG = ngx.DEBUG @@ -76,6 +78,23 @@ end function _M:entity_delta_writer(entity, name, options, ws_id, is_delete) + local dao = kong.db[name] + + if not is_delete and dao and entity and entity.ttl then + -- Replace relative TTL value to absolute TTL value + local export_options = kong_table.cycle_aware_deep_copy(options, true) + export_options.export = true + local exported_entity = dao:select(entity, export_options) + + if exported_entity and exported_entity.ttl then + ngx_log(ngx_DEBUG, "[kong.sync.v2] Update TTL from relative value to absolute value ", exported_entity.ttl, ".") + entity.ttl = exported_entity.ttl + + else + ngx_log(ngx_WARN, "[kong.sync.v2] Cannot update TTL of entity (", name, ") to absolute value.") + end + end + local res, err = self.strategy:insert_delta() if not res then self.strategy:cancel_txn() diff --git a/kong/db/strategies/postgres/init.lua b/kong/db/strategies/postgres/init.lua index c751b4b2072..dad681a7da8 100644 --- a/kong/db/strategies/postgres/init.lua +++ b/kong/db/strategies/postgres/init.lua @@ -570,8 +570,14 @@ end function _mt:select(primary_key, options) local statement_name = "select" - if self.schema.ttl and options and options.skip_ttl then - statement_name = "select_skip_ttl" + if options then + -- If we do select for export, there is no need to skip ttl. + if options.export then + statement_name = "select_for_export" + + elseif self.schema.ttl and options.skip_ttl then + statement_name = "select_skip_ttl" + end end local res, err = execute(self, statement_name, self.collapse(primary_key), options) @@ -1248,6 +1254,23 @@ function _M.new(connector, schema, errors) } }) + -- Add statements for exporting database, avoiding exporting TTL in absolute value. + add_statement_for_export("select", { + operation = "read", + expr = select_expressions, + argn = primary_key_names, + argv = primary_key_args, + code = { + "SELECT ", select_expressions, "\n", + " FROM ", table_name_escaped, "\n", + where_clause( + " WHERE ", "(" .. pk_escaped .. ") = (" .. primary_key_placeholders .. ")", + ttl_select_where, + ws_id_select_where), + " LIMIT 1;" + } + }) + add_statement_for_export("page_first", { operation = "read", argn = { LIMIT }, diff --git a/spec/03-plugins/09-key-auth/04-hybrid_mode_spec.lua b/spec/03-plugins/09-key-auth/04-hybrid_mode_spec.lua index 66f59e40b13..55a68667821 100644 --- a/spec/03-plugins/09-key-auth/04-hybrid_mode_spec.lua +++ b/spec/03-plugins/09-key-auth/04-hybrid_mode_spec.lua @@ -59,6 +59,10 @@ for _, strategy in helpers.each_strategy({"postgres"}) do cluster_rpc = rpc, cluster_rpc_sync = rpc_sync, })) + + if rpc_sync == "on" then + assert.logfile("servroot2/logs/error.log").has.line("[kong.sync.v2] full sync ends", true, 10) + end end) lazy_teardown(function()