Skip to content
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
1 change: 1 addition & 0 deletions lib/event_store/sql/statements.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule EventStore.Sql.Statements do
{:try_advisory_lock, [:schema]},
{:try_advisory_session_lock, [:schema]},
{:advisory_unlock, [:schema]},
{:bound_lock_wait, []},
{:claim_subscription_lock, [:schema]},
{:subscription_ack, [:schema]},
{:insert_snapshot, [:schema]},
Expand Down
5 changes: 5 additions & 0 deletions lib/event_store/sql/statements/bound_lock_wait.sql.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SELECT set_config('lock_timeout', $1::bigint::text, true)
FROM pg_settings
WHERE name = 'lock_timeout'
AND $1::bigint > 0
AND (setting::bigint = 0 OR setting::bigint > $1::bigint);
37 changes: 36 additions & 1 deletion lib/event_store/storage/lock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ defmodule EventStore.Storage.Lock do
alias EventStore.Storage.Session
alias EventStore.Subscriptions.FencingToken

@default_timeout 15_000

@doc """
Use two 32-bit key values for advisory locks where the first key acts as the
namespace.
Expand Down Expand Up @@ -61,7 +63,12 @@ defmodule EventStore.Storage.Lock do

query = Statements.claim_subscription_lock(schema)

case Postgrex.query(conn, query, [subscription_id], opts) do
reply =
with_bounded_lock_wait(conn, opts, fn conn ->
Postgrex.query(conn, query, [subscription_id], opts)
end)

case reply do
{:ok, %Postgrex.Result{rows: [[false, _backend_pid, _session_id, _generation, _last_seen]]}} ->
{:error, :lock_already_taken}

Expand Down Expand Up @@ -96,4 +103,32 @@ defmodule EventStore.Storage.Lock do
{:error, _error} = reply -> reply
end
end

# A query that outlives its timeout drops the connection along with every lock
# the session holds, so waiting on a lock held on the subscriptions table gives
# up well before that.
defp with_bounded_lock_wait(conn, opts, fun) do
Postgrex.transaction(
conn,
fn conn ->
with {:ok, _result} <-
Postgrex.query(conn, Statements.bound_lock_wait(), [lock_wait(opts)], opts),
{:ok, result} <- fun.(conn) do
result
else
{:error, error} -> Postgrex.rollback(conn, error)
end
end,
opts
)
rescue
error in [DBConnection.ConnectionError, Postgrex.Error] -> {:error, error}
end

defp lock_wait(opts) do
case Keyword.get(opts, :timeout, @default_timeout) do
:infinity -> 0
timeout -> div(timeout, 2)
end
end
end
52 changes: 52 additions & 0 deletions test/advisory_locks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,31 @@ defmodule EventStore.AdvisoryLocksTest do
AdvisoryLocks.try_subscription_lock(@locks, subscription_id)
end

test "should keep every lock on the node while a table lock outlasts a claim", context do
%{conn: conn, schema: schema} = context
%{subscription_id: held_id} = create_subscription(context)

{:ok, %AdvisoryLocks.Lease{ref: ref}} = AdvisoryLocks.try_subscription_lock(@locks, held_id)

other = create_subscription(context, "other_subscription")

table_lock = lock_subscriptions_table(context)

try do
assert {:error, %Postgrex.Error{postgres: %{code: :lock_not_available}}} =
AdvisoryLocks.try_subscription_lock(@locks, other.subscription_id, 1_000)
after
release_table_lock(table_lock)
end

_state = :sys.get_state(@locks)

refute_received {AdvisoryLocks, :lock_released, ^ref, _reason}

assert {:error, :lock_already_taken} =
Storage.Lock.try_acquire_exclusive_lock(conn, held_id, schema: schema)
end

test "should fail to acquire the lock of a subscription that does not exist" do
assert {:error, :subscription_not_found} =
AdvisoryLocks.try_subscription_lock(@locks, 999_999)
Expand Down Expand Up @@ -941,6 +966,33 @@ defmodule EventStore.AdvisoryLocksTest do
holder
end

defp lock_subscriptions_table(%{conn: conn, schema: schema}) do
reply_to = self()

table_lock =
Task.async(fn ->
Postgrex.transaction(conn, fn holder_conn ->
Postgrex.query!(holder_conn, "LOCK TABLE \"#{schema}\".subscriptions", [])

send(reply_to, :table_locked)

receive do
:release -> :ok
end
end)
end)

assert_receive :table_locked

table_lock
end

defp release_table_lock(%Task{pid: pid} = table_lock) do
send(pid, :release)

{:ok, :ok} = Task.await(table_lock)
end

defp wait_until_waiting_for_connection do
locks = Process.whereis(@locks)

Expand Down
Loading