From 3005a259218ba50a2b5a6277f531bf02782bb994 Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Sun, 25 Oct 2026 15:02:53 -0400 Subject: [PATCH 1/2] test(subscriptions): show a checkpoint that failed to write never being written Signed-off-by: Yordis Prieto --- test/subscriptions/subscription_fsm_test.exs | 49 +++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/test/subscriptions/subscription_fsm_test.exs b/test/subscriptions/subscription_fsm_test.exs index 153d875e..4b17bde6 100644 --- a/test/subscriptions/subscription_fsm_test.exs +++ b/test/subscriptions/subscription_fsm_test.exs @@ -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. @@ -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) @@ -416,6 +462,7 @@ 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), query_timeout: 15_000, event_store: TestEventStore, serializer: context.serializer, From 9accec648d3b6804fef680c73234a588662ec860 Mon Sep 17 00:00:00 2001 From: Yordis Prieto Date: Sun, 25 Oct 2026 15:46:28 -0400 Subject: [PATCH 2/2] fix(subscriptions): keep a checkpoint that failed to write so the next one retries it Signed-off-by: Yordis Prieto --- .../subscriptions/subscription_fsm.ex | 111 +++++++++++------- .../subscriptions/subscription_state.ex | 1 + test/subscriptions/subscription_fsm_test.exs | 1 + .../support/stream_subscription_test_case.ex | 1 + 4 files changed, 73 insertions(+), 41 deletions(-) diff --git a/lib/event_store/subscriptions/subscription_fsm.ex b/lib/event_store/subscriptions/subscription_fsm.ex index cafd3972..f07bf2da 100644 --- a/lib/event_store/subscriptions/subscription_fsm.ex +++ b/lib/event_store/subscriptions/subscription_fsm.ex @@ -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, %{}), @@ -736,8 +737,7 @@ 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 @@ -745,22 +745,39 @@ defmodule EventStore.Subscriptions.SubscriptionFsm do 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, @@ -768,46 +785,58 @@ defmodule EventStore.Subscriptions.SubscriptionFsm do 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 diff --git a/lib/event_store/subscriptions/subscription_state.ex b/lib/event_store/subscriptions/subscription_state.ex index d04164b4..80b5e64a 100644 --- a/lib/event_store/subscriptions/subscription_state.ex +++ b/lib/event_store/subscriptions/subscription_state.ex @@ -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", diff --git a/test/subscriptions/subscription_fsm_test.exs b/test/subscriptions/subscription_fsm_test.exs index 4b17bde6..1d276697 100644 --- a/test/subscriptions/subscription_fsm_test.exs +++ b/test/subscriptions/subscription_fsm_test.exs @@ -463,6 +463,7 @@ defmodule EventStore.Subscriptions.SubscriptionFsmTest do 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, diff --git a/test/subscriptions/support/stream_subscription_test_case.ex b/test/subscriptions/support/stream_subscription_test_case.ex index b8a68e26..7c5404f5 100644 --- a/test/subscriptions/support/stream_subscription_test_case.ex +++ b/test/subscriptions/support/stream_subscription_test_case.ex @@ -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)