Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(db) cluster events deadlock as reported by #4986 #5118

Merged
merged 1 commit into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions kong/db/migrations/core/006_130_to_140.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ return {
-- Do nothing, accept existing state
END;
$$;


DROP TRIGGER IF EXISTS "delete_expired_cluster_events_trigger" ON "cluster_events";
DROP FUNCTION IF EXISTS "delete_expired_cluster_events" ();
]],
},

Expand Down
19 changes: 7 additions & 12 deletions kong/db/strategies/postgres/connector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -292,17 +292,13 @@ end

function _mt:init_worker(strategies)
if ngx.worker.id() == 0 then
local graph
local found = false
local graph = tsort.new()

graph:add("cluster_events")

for _, strategy in pairs(strategies) do
local schema = strategy.schema
if schema.ttl then
if not found then
graph = tsort.new()
found = true
end

local name = schema.name
graph:add(name)
for _, field in schema:each_field() do
Expand All @@ -313,21 +309,20 @@ function _mt:init_worker(strategies)
end
end

if not found then
return true
end

local sorted_strategies = graph:sort()
local ttl_escaped = self:escape_identifier("ttl")
local expire_at_escaped = self:escape_identifier("expire_at")
local cleanup_statements = {}
local cleanup_statements_count = #sorted_strategies
for i = 1, cleanup_statements_count do
local table_name = sorted_strategies[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 ",
ttl_escaped,
column_name,
" < CURRENT_TIMESTAMP AT TIME ZONE 'UTC';"
}
end
Expand Down