-
Notifications
You must be signed in to change notification settings - Fork 58
Track projectors state (replaying/activating/active) in the database #472
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 8 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
afff88b
Track state of projectors during migration and verify on events
erikrozendaal bc9115c
Prefer `migration_class.version` over `new_version`
erikrozendaal 813f0d8
Stricter projector version check
erikrozendaal 9570e10
Make `is_active?` private and simplify a little
erikrozendaal ae48ef9
Deactivate projectors before replaying in tests
erikrozendaal a324057
Implement after_commit/after_rollback for read-only transactions
erikrozendaal 5172f53
Remove unused variable
erikrozendaal 00442e6
Test locking of projector states table
erikrozendaal 2d89810
Prevent processing events when different version is activating
erikrozendaal 683ced4
Review fixes
erikrozendaal c5a81ab
Remove deprecated EventStore#replay_events
erikrozendaal 4c86077
Also cache empty projector states table
erikrozendaal 9a0dcc2
Make projector state activation optional to ease upgrading and testing
erikrozendaal 5439291
Update CHANGELOG
erikrozendaal f9f0bba
Default to use transaction strategy for database cleaner
erikrozendaal 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
26 changes: 26 additions & 0 deletions
26
db/migrate/20250430125000_create_projector_states_table.rb
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,26 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class CreateProjectorStatesTable < ActiveRecord::Migration[7.2] | ||
| def change | ||
| Sequent::Support::Database.with_search_path(Sequent.configuration.event_store_schema_name) do | ||
| create_table :projector_states, id: false, primary_key: :name do |t| | ||
| t.primary_key :name, :text, null: false | ||
| t.integer :active_version | ||
| t.integer :activating_version | ||
| t.integer :replaying_version | ||
| t.timestamptz :created_at, precision: 6, null: false, default: -> { 'NOW()' } | ||
| t.timestamptz :updated_at, precision: 6, null: false, default: -> { 'NOW()' } | ||
| end | ||
|
|
||
| add_check_constraint :projector_states, | ||
| 'replaying_version IS NULL OR activating_version IS NULL', | ||
| name: 'replaying_conflicts_with_activating' | ||
| add_check_constraint :projector_states, | ||
| 'replaying_version > active_version', | ||
| name: 'replaying_newer_then_active' | ||
| add_check_constraint :projector_states, | ||
| 'activating_version > active_version', | ||
| name: 'activating_newer_than_active' | ||
| end | ||
| 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
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
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 |
|---|---|---|
|
|
@@ -2,6 +2,11 @@ | |
|
|
||
| module Sequent | ||
| module Core | ||
| class ProjectorMigrationError < RuntimeError; end | ||
| class UnknownActiveProjectorError < ProjectorMigrationError; end | ||
| class ReplayingProjectorMismatchError < ProjectorMigrationError; end | ||
| class NewerProjectorIsActiveError < ProjectorMigrationError; end | ||
|
|
||
| # | ||
| # EventPublisher ensures that, for every thread, events will be published | ||
| # in the order in which they are queued for publishing. | ||
|
|
@@ -30,6 +35,15 @@ def message | |
| def publish_events(events) | ||
| return if configuration.disable_event_handlers | ||
|
|
||
| ensure_no_unknown_active_projectors! | ||
|
|
||
| events.each { |event| events_queue.push(event) } | ||
| process_events | ||
| end | ||
|
|
||
| def replay_events(events) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this should be in an |
||
| ensure_only_replaying_projectors_subscribed! | ||
|
|
||
| events.each { |event| events_queue.push(event) } | ||
| process_events | ||
| end | ||
|
|
@@ -55,6 +69,8 @@ def process_event(event) | |
|
|
||
| configuration.event_handlers.each do |handler| | ||
| handler.handle_message event | ||
| rescue ProjectorMigrationError | ||
| raise | ||
| rescue StandardError | ||
| raise PublishEventError.new(handler.class, event) | ||
| end | ||
|
|
@@ -63,6 +79,38 @@ def process_event(event) | |
| def configuration | ||
| Sequent.configuration | ||
| end | ||
|
|
||
| def ensure_no_unknown_active_projectors! | ||
| expected_version = Sequent.migrations_class&.version | ||
| return if expected_version.nil? | ||
|
|
||
| registered_projectors = Migratable.projectors.to_set(&:name) | ||
| active_projectors = Projectors | ||
| .projector_states | ||
| .values | ||
| .select { |s| s.active_version == expected_version } | ||
| .to_set(&:name) | ||
| unknown_active_projectors = active_projectors - registered_projectors | ||
| if unknown_active_projectors.present? | ||
| fail UnknownActiveProjectorError, | ||
| "cannot publish event when unknown projectors are active #{unknown_active_projectors}" | ||
| end | ||
| end | ||
|
|
||
| def ensure_only_replaying_projectors_subscribed! | ||
| return unless Sequent.migrations_class | ||
|
|
||
| registered_projectors = Migratable.projectors.to_set(&:name) | ||
| projector_states = Projectors.projector_states | ||
| replaying_projectors = projector_states | ||
| .values | ||
| .select { |state| state.replaying? || state.activating? } | ||
| .to_set(&:name) | ||
| if registered_projectors != replaying_projectors | ||
| fail ReplayingProjectorMismatchError, | ||
| "cannot replay event when different projectors are replaying #{replaying_projectors}" | ||
| end | ||
| end | ||
| end | ||
| 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
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.