Skip to content

Commit

Permalink
Refactor how HANDLE_SIGNALS_FIRST SDK flag is managed
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffschoner committed Aug 23, 2023
1 parent f174ff1 commit 1e9dd73
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions lib/temporal/workflow/state_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,29 @@ def self.signal_event?(event)
attr_reader :dispatcher, :command_tracker, :marker_ids, :side_effects, :releases, :sdk_flags

def use_signals_first(raw_events)
raw_events.any? { |event| StateManager.signal_event?(event) } &&
if sdk_flags.include?(SDKFlags::HANDLE_SIGNALS_FIRST)
# If signals were handled first when this task or a previous one in this run were first
# played, we must continue to do so in order to ensure determinism regardless of what
# the configuration value is set to.
(
sdk_flags.include?(SDKFlags::HANDLE_SIGNALS_FIRST) ||
# If this is being played for the first time, use the configuration flag to choose
(!replay? && !@config.legacy_signals)
) &&
# In order to preserve determinism, the server must support SDK metadata to order signals
# first
@config.capabilities.sdk_metadata
# the configuration value is set to. Even the capabilities can be ignored because the
# server must have returned SDK metadata for this to be true.
true
elsif raw_events.any? { |event| StateManager.signal_event?(event) } &&
# If this is being played for the first time, use the configuration flag to choose
(!replay? && !@config.legacy_signals) &&
# In order to preserve determinism, the server must support SDK metadata to order signals
# first. This is checked last because it will result in a Temporal server call the first
# time it's called in the worker process.
@config.capabilities.sdk_metadata
report_flag_used(SDKFlags::HANDLE_SIGNALS_FIRST)
true
else
false
end
end

def order_events(raw_events)
signals_first = use_signals_first(raw_events)

report_flag_used(SDKFlags::HANDLE_SIGNALS_FIRST) if signals_first

raw_events.sort_by.with_index do |event, index|
# sort_by is not stable, so include index to preserve order
[StateManager.event_order(event, signals_first), index]
Expand Down

0 comments on commit 1e9dd73

Please sign in to comment.