Repository navigation
feat(storage): record which lock owner may write a subscription's progress #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yordis
merged 5 commits into
yordis/fix-subscription-stale-lock-notice
from
yordis/feat-subscription-lock-generation
Oct 23, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a16a80d
chore(test): show subscriptions have no record of which lock owner ma…
yordis 5eb179e
feat(storage): record which lock owner may write a subscription's pro…
yordis 32a028a
chore(test): show a node on the previous version keeps working agains…
yordis baad3fe
chore(test): show a claim that fails after taking the advisory lock l…
yordis 8485fad
fix(storage): release the advisory lock when a claim fails after taki…
yordis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
lib/event_store/sql/statements/claim_subscription_lock.sql.eex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| WITH lock AS ( | ||
| SELECT pg_try_advisory_lock( | ||
| '"<%= schema %>".subscriptions'::regclass::oid::int, | ||
| (CASE WHEN $1 > 2147483647 THEN mod($1, 2147483647) ELSE $1 END)::int | ||
| ) AS acquired, pg_backend_pid() AS pid, COALESCE( | ||
| NULLIF(current_setting('eventstore.session_id', true), ''), | ||
| set_config('eventstore.session_id', md5(random()::text || clock_timestamp()::text), false) | ||
| ) AS session_id | ||
| ), lease AS ( | ||
| UPDATE "<%= schema %>".subscriptions | ||
| SET lock_generation = lock_generation + 1 | ||
| WHERE subscription_id = $1 AND (SELECT acquired FROM lock) | ||
| RETURNING lock_generation, last_seen | ||
| ) | ||
| SELECT lock.acquired, lock.pid, lock.session_id, lease.lock_generation, lease.last_seen | ||
| FROM lock LEFT JOIN lease ON true; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| defmodule EventStore.Storage.Lock.Lease do | ||
| @moduledoc false | ||
|
|
||
| alias EventStore.Storage.Session | ||
| alias EventStore.Subscriptions.FencingToken | ||
|
|
||
| @type t :: %__MODULE__{ | ||
| token: FencingToken.t(), | ||
| last_seen: non_neg_integer() | nil, | ||
| session: Session.t() | ||
| } | ||
|
|
||
| @enforce_keys [:token, :last_seen, :session] | ||
| defstruct [:token, :last_seen, :session] | ||
|
|
||
| def new(%FencingToken{} = token, last_seen, %Session{} = session) do | ||
| %__MODULE__{token: token, last_seen: last_seen, session: session} | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| defmodule EventStore.Subscriptions.FencingToken do | ||
| @moduledoc false | ||
|
|
||
| @type t :: %__MODULE__{ | ||
| subscription_id: pos_integer(), | ||
| generation: pos_integer() | ||
| } | ||
|
|
||
| @enforce_keys [:subscription_id, :generation] | ||
| defstruct [:subscription_id, :generation] | ||
|
|
||
| def new(subscription_id, generation) | ||
| when is_integer(subscription_id) and is_integer(generation) and generation > 0 do | ||
| %__MODULE__{subscription_id: subscription_id, generation: generation} | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| DO $migration$ | ||
| BEGIN | ||
| ALTER TABLE subscriptions ADD COLUMN lock_generation bigint NOT NULL DEFAULT 0; | ||
|
|
||
| -- Record schema migration | ||
| INSERT INTO schema_migrations (major_version, minor_version, patch_version) VALUES (1, 5, 0); | ||
| END; | ||
| $migration$ LANGUAGE plpgsql; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.