Skip to content

Commit

Permalink
fix(db) cluster events deadlock as reported by #4986 (#5118)
Browse files Browse the repository at this point in the history
  • Loading branch information
bungle authored Oct 14, 2019
1 parent 9ad6c12 commit 536643f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
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

0 comments on commit 536643f

Please sign in to comment.