Skip to content
Merged
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
53 changes: 48 additions & 5 deletions test/subscriptions/subscription_zombie_lock_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ defmodule EventStore.Subscriptions.SubscriptionZombieLockTest do
end

test "a node that dies dirty cannot move the progress of the owner that replaced it",
%{conn: conn, schema: schema, proxy: proxy, subscription_name: subscription_name} do
%{
conn: conn,
postgrex_config: postgrex_config,
schema: schema,
proxy: proxy,
subscription_name: subscription_name
} do
{:ok, subscription_a} =
TestEventStore.subscribe_to_all_streams(subscription_name, self(),
name: @store_a,
Expand All @@ -49,6 +55,8 @@ defmodule EventStore.Subscriptions.SubscriptionZombieLockTest do
Wait.until(fn -> assert 3 == last_seen(conn, schema, subscription_name) end)
assert 1 == lock_generation(conn, schema, subscription_name)

hold_unrelated_advisory_locks(conn, postgrex_config, schema, subscription_name)

:ok = TcpProxy.freeze(proxy)

test_pid = self()
Expand All @@ -75,7 +83,7 @@ defmodule EventStore.Subscriptions.SubscriptionZombieLockTest do
refute_receive {:b, :subscribed, _subscription}, 2_500
assert 1 == lock_generation(conn, schema, subscription_name)

terminate_lock_holder(conn)
terminate_lock_holder(conn, schema, subscription_name)

assert_receive {:b, :subscribed, ^subscription_b}, 5_000
assert_receive {:b, :events, [%{event_number: 4} | _]}, 5_000
Expand Down Expand Up @@ -106,18 +114,53 @@ defmodule EventStore.Subscriptions.SubscriptionZombieLockTest do
assert 2 == lock_generation(conn, schema, subscription_name)
end

defp terminate_lock_holder(conn) do
defp terminate_lock_holder(conn, schema, subscription_name) do
%Postgrex.Result{rows: [[backend_pid]]} =
Postgrex.query!(
conn,
"SELECT pid FROM pg_locks WHERE locktype = 'advisory' AND granted",
[]
"""
SELECT l.pid
FROM pg_locks l
JOIN pg_database d ON d.oid = l.database
JOIN "#{schema}".subscriptions s ON s.subscription_id = l.objid::bigint
WHERE l.locktype = 'advisory'
AND l.granted
AND d.datname = current_database()
AND l.classid = '"#{schema}".subscriptions'::regclass::oid
AND s.subscription_name = $1
""",
[subscription_name]
)

%Postgrex.Result{rows: [[true]]} =
Postgrex.query!(conn, "SELECT pg_terminate_backend($1)", [backend_pid])
end

# Advisory locks are server-wide, so sessions of other databases and other
# lock users of this one must not be mistaken for the subscription's owner.
defp hold_unrelated_advisory_locks(conn, postgrex_config, schema, subscription_name) do
%Postgrex.Result{rows: [[classid, subscription_id]]} =
Postgrex.query!(
conn,
"""
SELECT '"#{schema}".subscriptions'::regclass::oid::int, subscription_id::int
FROM "#{schema}".subscriptions
WHERE subscription_name = $1
""",
[subscription_name]
)

other_database =
start_supervised!({Postgrex, Keyword.put(postgrex_config, :database, "postgres")},
id: :other_database
)

same_database = start_supervised!({Postgrex, postgrex_config}, id: :same_database)

Postgrex.query!(other_database, "SELECT pg_advisory_lock($1, $2)", [classid, subscription_id])
Postgrex.query!(same_database, "SELECT pg_advisory_lock($1)", [subscription_id])
end

defp assert_receive_through(last_event_number) do
assert_receive {:events, events}

Expand Down
Loading