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
3 changes: 2 additions & 1 deletion lib/event_store/sql/statements.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ defmodule EventStore.Sql.Statements do
{:query_stream_events_backward, [:schema]},
{:query_stream_events_forward, [:schema]},
{:query_streams, [:schema, :sort_by, :sort_dir]},
{:query_subscription, [:schema]}
{:query_subscription, [:schema]},
{:query_subscription_exists, [:schema]}
] do
file = Path.expand("statements/#{fun}.sql.eex", __DIR__)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT EXISTS (SELECT 1 FROM "<%= schema %>".subscriptions WHERE subscription_id = $1);
11 changes: 3 additions & 8 deletions lib/event_store/sql/statements/subscription_ack.sql.eex
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
WITH progress AS (
UPDATE "<%= schema %>".subscriptions
SET last_seen = GREATEST(last_seen, $2)
WHERE subscription_id = $1 AND lock_generation = $3
RETURNING subscription_id
)
SELECT EXISTS (SELECT 1 FROM progress) AS acknowledged,
EXISTS (SELECT 1 FROM "<%= schema %>".subscriptions WHERE subscription_id = $1) AS present;
UPDATE "<%= schema %>".subscriptions
SET last_seen = GREATEST(last_seen, $2)
WHERE subscription_id = $1 AND lock_generation = $3;
31 changes: 25 additions & 6 deletions lib/event_store/storage/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -148,26 +148,45 @@ defmodule EventStore.Storage.Subscription do
# Naming the subscription by its identifier, rather than by the stream and name a later
# subscription can reuse, is what keeps this from writing a checkpoint onto whichever row
# holds that name now.
{:ok, %Postgrex.Result{rows: [[true, _present]]}} ->
{:ok, %Postgrex.Result{num_rows: 1}} ->
:ok

{:ok, %Postgrex.Result{rows: [[false, true]]}} ->
{:ok, %Postgrex.Result{num_rows: 0}} ->
rejected(conn, subscription_id, schema, opts)

{:error, error} = reply ->
Logger.warning(
"Failed to ack last seen event for subscription #{subscription_id} because another lock owner has claimed it"
"Failed to ack last seen event for subscription #{subscription_id} due to: " <>
inspect(error)
)

{:error, :fenced}
reply
end
end

{:ok, %Postgrex.Result{rows: [[false, false]]}} ->
# A delete that commits while the update waits on its row is only visible to a statement that
# starts after it, so asking within the update would still find the deleted row.
defp rejected(conn, subscription_id, schema, opts) do
query = Statements.query_subscription_exists(schema)

case Postgrex.query(conn, query, [subscription_id], opts) do
{:ok, %Postgrex.Result{rows: [[false]]}} ->
Logger.warning(
"Failed to ack last seen event for subscription #{subscription_id} because it no longer exists"
)

{:error, :subscription_not_found}

{:ok, %Postgrex.Result{rows: [[true]]}} ->
Logger.warning(
"Failed to ack last seen event for subscription #{subscription_id} because another lock owner has claimed it"
)

{:error, :fenced}

{:error, error} = reply ->
Logger.warning(
"Failed to ack last seen event for subscription #{subscription_id} due to: " <>
"Failed to ack last seen event for subscription #{subscription_id}, which refused the write, due to: " <>
inspect(error)
)

Expand Down
47 changes: 47 additions & 0 deletions test/storage/subscription_persistence_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule EventStore.Storage.SubscriptionPersistenceTest do
alias EventStore.Storage
alias EventStore.Storage.Lock
alias EventStore.Subscriptions.FencingToken
alias EventStore.Wait

@all_stream "$all"
@subscription_name "test_subscription"
Expand Down Expand Up @@ -210,6 +211,52 @@ defmodule EventStore.Storage.SubscriptionPersistenceTest do
assert {:ok, %Storage.Subscription{last_seen: 10}} = read_subscription(context)
end

test "ack last seen event racing a delete reports the subscription as gone", context do
%{conn: conn, schema: schema, postgrex_config: postgrex_config} = context

{:ok, %Storage.Subscription{subscription_id: subscription_id}} = subscribe_to_stream(context)

token = claim_token(context, subscription_id)

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

test = self()

delete =
Task.async(fn ->
Postgrex.transaction(deleter, fn deleter ->
:ok =
Storage.delete_subscription(deleter, @all_stream, @subscription_name, schema: schema)

send(test, :deleted)

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

assert_receive :deleted

ack = Task.async(fn -> Storage.ack_last_seen_event(conn, token, 1, schema: schema) end)

Wait.until(5_000, fn ->
assert %Postgrex.Result{rows: [[1]]} =
Postgrex.query!(
observer,
"SELECT count(*)::int FROM pg_stat_activity WHERE datname = current_database() AND wait_event_type = 'Lock'",
[]
)
end)

send(delete.pid, :commit)

assert {:ok, :ok} = Task.await(delete)
assert {:error, :subscription_not_found} = Task.await(ack)
assert {:error, :subscription_not_found} = read_subscription(context)
end

defp read_subscription(context) do
%{conn: conn, schema: schema} = context

Expand Down
11 changes: 6 additions & 5 deletions test/subscriptions/subscription_fsm_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ defmodule EventStore.Subscriptions.SubscriptionFsmTest do
assert_received {:checkpoint_failed, :subscription_not_found}
end

test "tells the subscription it is fenced when the probe that classifies the rejection fails",
context do
test "does not take a rejection it could not classify for a fence", context do
%{conn: conn, schema: schema, subscription_id: subscription_id} = context

probe_conn = start_supervised!({Postgrex, context.postgrex_config}, id: :probe_conn)
Expand All @@ -180,10 +179,12 @@ defmodule EventStore.Subscriptions.SubscriptionFsmTest do

drop_privileges_after_update(context)

assert %SubscriptionFsm{data: %SubscriptionState{checkpoints_pending: 0}} =
SubscriptionFsm.checkpoint(fsm)
assert %SubscriptionFsm{} = SubscriptionFsm.checkpoint(fsm)

assert_received {:fenced, ^lock_ref}
# Claiming the lock again would recreate a row that a delete removed, so only a probe that
# found the row may send the subscription to do that.
refute_received {:fenced, ^lock_ref}
refute_received {:checkpoint_failed, _reason}
end

test "does not write the position onto a subscription that reused the name", context do
Expand Down
Loading