Skip to content

Commit

Permalink
feat(db): remove postgres cleanup timer and use postgres triggers ins…
Browse files Browse the repository at this point in the history
…tead
  • Loading branch information
bungle committed Feb 28, 2023
1 parent 0374343 commit a4a2a0a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 53 deletions.
2 changes: 2 additions & 0 deletions kong-3.2.1-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ build = {
["kong.db.migrations.core.016_280_to_300"] = "kong/db/migrations/core/016_280_to_300.lua",
["kong.db.migrations.core.017_300_to_310"] = "kong/db/migrations/core/017_300_to_310.lua",
["kong.db.migrations.core.018_310_to_320"] = "kong/db/migrations/core/018_310_to_320.lua",
["kong.db.migrations.core.019_320_to_330"] = "kong/db/migrations/core/019_320_to_330.lua",
["kong.db.migrations.operations.200_to_210"] = "kong/db/migrations/operations/200_to_210.lua",
["kong.db.migrations.operations.210_to_211"] = "kong/db/migrations/operations/210_to_211.lua",
["kong.db.migrations.operations.212_to_213"] = "kong/db/migrations/operations/212_to_213.lua",
Expand Down Expand Up @@ -298,6 +299,7 @@ build = {
["kong.plugins.oauth2.migrations.003_130_to_140"] = "kong/plugins/oauth2/migrations/003_130_to_140.lua",
["kong.plugins.oauth2.migrations.004_200_to_210"] = "kong/plugins/oauth2/migrations/004_200_to_210.lua",
["kong.plugins.oauth2.migrations.005_210_to_211"] = "kong/plugins/oauth2/migrations/005_210_to_211.lua",
["kong.plugins.oauth2.migrations.006_320_to_330"] = "kong/plugins/oauth2/migrations/006_320_to_330.lua",
["kong.plugins.oauth2.handler"] = "kong/plugins/oauth2/handler.lua",
["kong.plugins.oauth2.secret"] = "kong/plugins/oauth2/secret.lua",
["kong.plugins.oauth2.access"] = "kong/plugins/oauth2/access.lua",
Expand Down
19 changes: 19 additions & 0 deletions kong/db/migrations/core/019_320_to_330.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
return {
postgres = {
up = [[
CREATE OR REPLACE FUNCTION batch_delete_expired_rows() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
IF RANDOM() < 0.01 THEN
EXECUTE FORMAT('WITH rows AS (SELECT ctid FROM %s WHERE ttl < CURRENT_TIMESTAMP AT TIME ZONE ''UTC'' ORDER BY ttl LIMIT 50000 FOR UPDATE SKIP LOCKED) DELETE FROM %s WHERE ctid IN (TABLE rows)', TG_TABLE_NAME, TG_TABLE_NAME);
END IF;
RETURN NULL;
END;
$$;
]],
},
cassandra = {
up = [[]],
}
}
1 change: 1 addition & 0 deletions kong/db/migrations/core/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ return {
"016_280_to_300",
"017_300_to_310",
"018_310_to_320",
"019_320_to_330",
}
54 changes: 1 addition & 53 deletions kong/db/strategies/postgres/connector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -314,59 +314,7 @@ function _mt:init()
end


function _mt:init_worker(strategies)
if ngx.worker.id() == 0 then

local table_names = get_names_of_tables_with_ttl(strategies)
local ttl_escaped = self:escape_identifier("ttl")
local expire_at_escaped = self:escape_identifier("expire_at")
local cleanup_statements = {}
local cleanup_statements_count = #table_names
for i = 1, cleanup_statements_count do
local table_name = table_names[i]
local column_name = table_name == "cluster_events" and expire_at_escaped
or ttl_escaped
cleanup_statements[i] = concat {
" DELETE FROM ",
self:escape_identifier(table_name),
" WHERE ",
column_name,
" < CURRENT_TIMESTAMP AT TIME ZONE 'UTC';"
}
end

local cleanup_statement = concat(cleanup_statements, "\n")

return timer_every(60, function(premature)
if premature then
return
end

local ok, err, _, num_queries = self:query(cleanup_statement)
if not ok then
if num_queries then
for i = num_queries + 1, cleanup_statements_count do
local statement = cleanup_statements[i]
local ok, err = self:query(statement)
if not ok then
if err then
log(WARN, "unable to clean expired rows from table '",
table_names[i], "' on PostgreSQL database (",
err, ")")
else
log(WARN, "unable to clean expired rows from table '",
table_names[i], "' on PostgreSQL database")
end
end
end

else
log(ERR, "unable to clean expired rows from PostgreSQL database (", err, ")")
end
end
end)
end

function _mt:init_worker()
return true
end

Expand Down
33 changes: 33 additions & 0 deletions kong/plugins/oauth2/migrations/006_320_to_330.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
return {
postgres = {
up = [[
DROP TRIGGER IF EXISTS "oauth2_authorization_codes_ttl_trigger" ON "oauth2_authorization_codes";
DO $$
BEGIN
CREATE TRIGGER "oauth2_authorization_codes_ttl_trigger"
AFTER INSERT ON "oauth2_authorization_codes"
FOR EACH STATEMENT
EXECUTE PROCEDURE batch_delete_expired_rows();
EXCEPTION WHEN UNDEFINED_COLUMN OR UNDEFINED_TABLE THEN
-- Do nothing, accept existing state
END$$;
DROP TRIGGER IF EXISTS "oauth2_tokens_ttl_trigger" ON "oauth2_tokens";
DO $$
BEGIN
CREATE TRIGGER "oauth2_tokens_ttl_trigger"
AFTER INSERT ON "oauth2_tokens"
FOR EACH STATEMENT
EXECUTE PROCEDURE batch_delete_expired_rows();
EXCEPTION WHEN UNDEFINED_COLUMN OR UNDEFINED_TABLE THEN
-- Do nothing, accept existing state
END$$;
]],
},
cassandra = {
up = [[]],
}
}
1 change: 1 addition & 0 deletions kong/plugins/oauth2/migrations/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ return {
"003_130_to_140",
"004_200_to_210",
"005_210_to_211",
"006_320_to_330",
}

0 comments on commit a4a2a0a

Please sign in to comment.