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
111 changes: 70 additions & 41 deletions lib/event_store/subscriptions/subscription_fsm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmodule EventStore.Subscriptions.SubscriptionFsm do
buffer_size: opts[:buffer_size] || 1,
checkpoint_after: opts[:checkpoint_after] || 0,
checkpoint_threshold: opts[:checkpoint_threshold] || 1,
retry_interval: Keyword.fetch!(opts, :retry_interval),
query_timeout: opts[:query_timeout] || 15_000,
checkout_opts: Keyword.get(opts, :checkout_opts, []),
telemetry_metadata: Keyword.get(opts, :telemetry_metadata, %{}),
Expand Down Expand Up @@ -736,78 +737,106 @@ defmodule EventStore.Subscriptions.SubscriptionFsm do
%SubscriptionState{
checkpoint_after: checkpoint_after,
checkpoints_pending: checkpoints_pending,
checkpoint_threshold: checkpoint_threshold,
checkpoint_timer_ref: checkpoint_timer_ref
checkpoint_threshold: checkpoint_threshold
} = data

cond do
checkpoints_pending >= checkpoint_threshold ->
persist_checkpoint(data)

checkpoint_after > 0 ->
if checkpoint_timer_ref, do: Process.cancel_timer(checkpoint_timer_ref)

checkpoint_timer_ref = Process.send_after(self(), :checkpoint, checkpoint_after)

%SubscriptionState{data | checkpoint_timer_ref: checkpoint_timer_ref}
schedule_checkpoint(data, checkpoint_after)

true ->
data
end
end

defp schedule_checkpoint(%SubscriptionState{} = data, delay) do
%SubscriptionState{checkpoint_timer_ref: checkpoint_timer_ref} = data

if checkpoint_timer_ref, do: Process.cancel_timer(checkpoint_timer_ref)

checkpoint_timer_ref = Process.send_after(self(), :checkpoint, delay)

%SubscriptionState{data | checkpoint_timer_ref: checkpoint_timer_ref}
end

defp persist_checkpoint(%SubscriptionState{transient: true} = data) do
%SubscriptionState{data | checkpoints_pending: 0}
end

defp persist_checkpoint(%SubscriptionState{transient: false} = data) do
%SubscriptionState{fencing_token: fencing_token, checkpoints_pending: checkpoints_pending} =
data

if checkpoints_pending > 0 and fencing_token != nil do
write_checkpoint(data)
else
%SubscriptionState{data | checkpoints_pending: 0}
end
end

defp write_checkpoint(%SubscriptionState{} = data) do
%SubscriptionState{
conn: conn,
stream_uuid: stream_uuid,
subscription_name: subscription_name,
fencing_token: fencing_token,
lock_ref: lock_ref,
last_ack: last_ack,
checkpoints_pending: checkpoints_pending,
telemetry_metadata: telemetry_metadata
} = data

if checkpoints_pending > 0 and fencing_token != nil do
metadata =
Map.merge(telemetry_metadata, %{
stream_uuid: stream_uuid,
subscription_name: subscription_name,
last_seen: last_ack
})

result =
Telemetry.span(:subscription_checkpoint, metadata, fn ->
Storage.Subscription.ack_last_seen_event(
conn,
fencing_token,
last_ack,
query_opts(data)
)
end)

# Reaching no row means the subscription being checkpointed has been deleted. Subscribing
# again would recreate it at the position the delete removed, so the subscription stops
# instead and leaves its subscribers to subscribe from wherever the delete left them.
case result do
{:error, :subscription_not_found} ->
send(self(), {:checkpoint_failed, :subscription_not_found})

{:error, :fenced} ->
send(self(), {:fenced, lock_ref})

_ ->
:ok
end
end
metadata =
Map.merge(telemetry_metadata, %{
stream_uuid: stream_uuid,
subscription_name: subscription_name,
last_seen: last_ack
})

result =
Telemetry.span(:subscription_checkpoint, metadata, fn ->
Storage.Subscription.ack_last_seen_event(
conn,
fencing_token,
last_ack,
query_opts(data)
)
end)

%SubscriptionState{data | checkpoints_pending: 0}
# Reaching no row means the subscription being checkpointed has been deleted. Subscribing
# again would recreate it at the position the delete removed, so the subscription stops
# instead and leaves its subscribers to subscribe from wherever the delete left them.
case result do
:ok ->
%SubscriptionState{data | checkpoints_pending: 0}

{:error, :subscription_not_found} ->
send(self(), {:checkpoint_failed, :subscription_not_found})

%SubscriptionState{data | checkpoints_pending: 0}

{:error, :fenced} ->
send(self(), {:fenced, lock_ref})

%SubscriptionState{data | checkpoints_pending: 0}

# The position is still unwritten, so it is tried again even if no further ack arrives. A
# subscription that writes on every ack has no interval of its own, and waits as long as it
# would to retry subscribing against the same database.
{:error, _error} ->
schedule_checkpoint(data, checkpoint_retry_delay(data))
end
end

defp checkpoint_retry_delay(%SubscriptionState{checkpoint_after: checkpoint_after})
when checkpoint_after > 0,
do: checkpoint_after

defp checkpoint_retry_delay(%SubscriptionState{retry_interval: retry_interval}),
do: retry_interval

# Purge all subscriber in-flight events and subscription event queue.
defp purge_in_flight_events(%SubscriptionState{} = data) do
%SubscriptionState{subscribers: subscribers} = data
Expand Down
1 change: 1 addition & 0 deletions lib/event_store/subscriptions/subscription_state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ defmodule EventStore.Subscriptions.SubscriptionState do
:query_timeout,
:lock_ref,
:fencing_token,
:retry_interval,
checkout_opts: [],
telemetry_metadata: %{},
correlation_id_type: "uuid",
Expand Down
50 changes: 49 additions & 1 deletion test/subscriptions/subscription_fsm_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ defmodule EventStore.Subscriptions.SubscriptionFsmTest do

drop_privileges_after_update(context)

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

# 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.
Expand Down Expand Up @@ -216,6 +217,51 @@ defmodule EventStore.Subscriptions.SubscriptionFsmTest do
refute_received {:checkpoint_failed, _reason}
end

test "writes a position whose earlier write failed on the next checkpoint", context do
fsm = fsm(context, checkpoints_pending: 1, last_ack: 3, schema: "no_such_schema")

%SubscriptionFsm{data: data} = SubscriptionFsm.checkpoint(fsm)

fsm = %SubscriptionFsm{fsm | data: %SubscriptionState{data | schema: context.schema}}

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

assert {:ok, %Storage.Subscription{last_seen: 3}} = read_subscription(context)
end

test "retries a failed write once the checkpoint interval has passed", context do
fsm =
fsm(context,
checkpoints_pending: 1,
last_ack: 3,
schema: "no_such_schema",
checkpoint_after: 30_000
)

%SubscriptionFsm{data: %SubscriptionState{checkpoint_timer_ref: timer}} =
SubscriptionFsm.checkpoint(fsm)

assert is_reference(timer)
assert Process.read_timer(timer) in 1..30_000
end

test "retries a failed write after the retry interval when it writes on every ack", context do
fsm =
fsm(context,
checkpoints_pending: 1,
last_ack: 3,
schema: "no_such_schema",
retry_interval: 45_000
)

%SubscriptionFsm{data: %SubscriptionState{checkpoint_timer_ref: timer}} =
SubscriptionFsm.checkpoint(fsm)

assert is_reference(timer)
assert Process.read_timer(timer) in 30_001..45_000
end

test "writes from every state that owns a position", context do
for state <- @persisting_states do
fsm = fsm(context, state: state, checkpoints_pending: 1, last_ack: 3)
Expand Down Expand Up @@ -416,6 +462,8 @@ defmodule EventStore.Subscriptions.SubscriptionFsmTest do
fencing_token: context.fencing_token,
last_ack: Keyword.get(overrides, :last_ack, 0),
checkpoints_pending: Keyword.get(overrides, :checkpoints_pending, 0),
checkpoint_after: Keyword.get(overrides, :checkpoint_after, 0),
retry_interval: Keyword.get(overrides, :retry_interval, 60_000),
query_timeout: 15_000,
event_store: TestEventStore,
serializer: context.serializer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ defmodule EventStore.Subscriptions.StreamSubscriptionTestCase do
|> Keyword.put(:correlation_id_type, correlation_id_type)
|> Keyword.put(:causation_id_type, causation_id_type)
|> Keyword.put_new(:buffer_size, 3)
|> Keyword.put_new(:retry_interval, 60_000)

stream_uuid
|> SubscriptionFsm.new(@subscription_name, opts)
Expand Down
Loading