Skip to content

Commit

Permalink
Merge branch 'master' into replay-using-postgres-minimum-transaction-id
Browse files Browse the repository at this point in the history
  • Loading branch information
erikrozendaal committed Feb 6, 2024
2 parents 896f47b + fcc7bcc commit 338f5cb
Show file tree
Hide file tree
Showing 14 changed files with 457 additions and 252 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Changelog 7.0.x (changes since 7.0.0)

- Replaying all events for the view schema (using `sequent:migrate:online` and `sequent:migrate:offline`) now make use of the PostgreSQL committed transaction id to track events that have already been replayed. The replayed ids table (specified by the removed `Sequent::configuration.replayed_ids_table_name` option) is no longer used and can be dropped from your database.
- The `MessageDispatcher` class has been removed.
- Instance-of routes in projectors and other message handlers now use an optimized lookup mechanism. These are the most common handlers (`on MyEvent do ... end`).
- Many optimizations were applied to the `ReplayOptimizedPostgresPersistor`:
- Multi-value indexes are no longer supported, each column is now individually indexed. When a where clauses references multiple indexed columns all applicable indexes are used. For backwards compatibility multi-column index definitions are automatically changed to single-column indexes (one for each colum in the multi-column definition).
- Default indexed columns can be specified when instantiating the `ReplayOptimizedPostgresPersistor`.
- Indexed values are now automatically frozen.
- Array matching and string/symbol matching in where-clauses now work for indexed columns as well.
- The internal struct classes are now generated differently and these classes are no longer associated with a Ruby constant so cannot be referenced from your code.

# Changelog 7.0.0 (changes since 6.0.1)
- Added possibility `enable_autoregistration` for automatically registering all Command and EventHandlers
Expand Down
1 change: 1 addition & 0 deletions lib/sequent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
require_relative 'sequent/core/core'
require_relative 'sequent/util/util'
require_relative 'sequent/migrations/migrations'
require_relative 'sequent/dry_run/dry_run'

require_relative 'notices'
23 changes: 0 additions & 23 deletions lib/sequent/core/helpers/message_dispatcher.rb

This file was deleted.

19 changes: 8 additions & 11 deletions lib/sequent/core/helpers/message_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require_relative 'message_handler_option_registry'
require_relative 'message_router'
require_relative 'message_dispatcher'

module Sequent
module Core
Expand Down Expand Up @@ -60,11 +59,7 @@ def option(name, &block)
end

def message_mapping
message_router
.routes
.select { |matcher, _handlers| matcher.is_a?(MessageMatchers::InstanceOf) }
.map { |k, v| [k.expected_class, v] }
.to_h
message_router.instanceof_routes
end

def handles_message?(message)
Expand Down Expand Up @@ -106,13 +101,15 @@ def self.included(host_class)
end

def handle_message(message)
message_dispatcher.dispatch_message(message)
handlers = self.class.message_router.match_message(message)
dispatch_message(message, handlers) unless handlers.empty?
end

private

def message_dispatcher
MessageDispatcher.new(self.class.message_router, self)
def dispatch_message(message, handlers)
handlers.each do |handler|
Sequent.logger.debug("[MessageHandler] Handler #{@context.class} handling #{message.class}")
instance_exec(message, &handler)
end
end
end
end
Expand Down
20 changes: 13 additions & 7 deletions lib/sequent/core/helpers/message_router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Sequent
module Core
module Helpers
class MessageRouter
attr_reader :routes
attr_reader :routes, :instanceof_routes

def initialize
clear_routes
Expand All @@ -21,19 +21,24 @@ def initialize
#
def register_matchers(*matchers, handler)
matchers.each do |matcher|
@routes[matcher] << handler
if matcher.is_a?(MessageMatchers::InstanceOf)
@instanceof_routes[matcher.expected_class] << handler
else
@routes[matcher] << handler
end
end
end

##
# Returns a set of handlers that match the given message, or an empty set when none match.
#
def match_message(message)
@routes
.reduce(Set.new) do |memo, (matcher, handlers)|
memo = memo.merge(handlers) if matcher.matches_message?(message)
memo
end
result = Set.new
result.merge(@instanceof_routes[message.class])
@routes.each do |matcher, handlers|
result.merge(handlers) if matcher.matches_message?(message)
end
result
end

##
Expand All @@ -47,6 +52,7 @@ def matches_message?(message)
# Removes all routes from the router.
#
def clear_routes
@instanceof_routes = Hash.new { |h, k| h[k] = Set.new }
@routes = Hash.new { |h, k| h[k] = Set.new }
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/sequent/core/persistors/active_record_persistor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def execute_sql(statement)
Sequent::ApplicationRecord.connection.execute(statement)
end

def prepare
# noop
end

def commit
# noop
end
Expand Down
5 changes: 5 additions & 0 deletions lib/sequent/core/persistors/persistor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ def last_record
fail 'Method not supported in this persistor'
end

# Hook to implement for instance the persistor batches statements
def prepare
fail 'Method not supported in this persistor'
end

# Hook to implement for instance the persistor batches statements
def commit
fail 'Method not supported in this persistor'
Expand Down
Loading

0 comments on commit 338f5cb

Please sign in to comment.