-
Notifications
You must be signed in to change notification settings - Fork 274
world-postgres: enforce per-(run, correlation) uniqueness for entity-creating events #1878
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0acdad6
Enforce per-(run, correlation) uniqueness for entity-creating events …
TooTallNate 2801296
Merge branch 'main' into world-postgres-event-uniqueness
TooTallNate 4cfbb5a
Merge branch 'main' into world-postgres-event-uniqueness
TooTallNate d12ce1d
Merge branch 'main' into world-postgres-event-uniqueness
TooTallNate 774f4fe
Merge branch 'main' into world-postgres-event-uniqueness
TooTallNate 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@workflow/world-postgres": patch | ||
| --- | ||
|
|
||
| Fix race in `events.create()` where concurrent `step_created` / `hook_created` / `wait_created` writes with the same `correlationId` would persist duplicate event rows. Adds a unique partial index and surfaces the violation as `EntityConflictError`. |
40 changes: 40 additions & 0 deletions
40
...es/world-postgres/src/drizzle/migrations/0010_add_events_entity_creation_unique_index.sql
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,40 @@ | ||
| -- Enforce uniqueness of (run_id, correlation_id, event_type) for the | ||
| -- entity-creating events (step_created, hook_created, wait_created). | ||
| -- | ||
| -- Without this constraint, two concurrent runtime invocations producing | ||
| -- identical correlationIds (e.g. the snapshot runtime's deterministic | ||
| -- ULIDs across replays of the same resumption) can both insert events, | ||
| -- causing duplicate step/hook/wait events in the log. The unique | ||
| -- violation is caught in events.create and surfaced as | ||
| -- EntityConflictError, which the runtime already handles as a dedup | ||
| -- signal. | ||
| -- | ||
| -- Existing installations may already contain duplicate | ||
| -- (run_id, correlation_id, type) rows for these event types — the | ||
| -- previous storage behavior allowed them through. Deduplicate before | ||
| -- creating the unique partial index, otherwise the CREATE UNIQUE INDEX | ||
| -- statement would fail at migration time. We keep the earliest-inserted | ||
| -- row for each (run_id, correlation_id, type) tuple (lowest ctid) and | ||
| -- drop the rest. The duplicates that this removes are exactly the rows | ||
| -- that would have been rejected as `EntityConflictError` had the unique | ||
| -- index existed when they were inserted. | ||
| WITH "ranked_workflow_events" AS ( | ||
| SELECT | ||
| ctid, | ||
| ROW_NUMBER() OVER ( | ||
| PARTITION BY "run_id", "correlation_id", "type" | ||
| ORDER BY ctid | ||
| ) AS "row_num" | ||
| FROM "workflow"."workflow_events" | ||
| WHERE "type" IN ('step_created', 'hook_created', 'wait_created') | ||
| ) | ||
| DELETE FROM "workflow"."workflow_events" | ||
| WHERE ctid IN ( | ||
| SELECT ctid | ||
| FROM "ranked_workflow_events" | ||
| WHERE "row_num" > 1 | ||
| ); | ||
|
|
||
| CREATE UNIQUE INDEX IF NOT EXISTS "workflow_events_entity_creation_unique" | ||
| ON "workflow"."workflow_events" ("run_id", "correlation_id", "type") | ||
| WHERE "type" IN ('step_created', 'hook_created', 'wait_created'); | ||
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
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.
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.