-
Notifications
You must be signed in to change notification settings - Fork 22
Support certain Time calls in workflows with advanced validation
#291
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 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
64 changes: 64 additions & 0 deletions
64
temporalio/lib/temporalio/worker/illegal_workflow_call_validator.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,64 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Temporalio | ||
| class Worker | ||
| # Custom validator for validating illegal workflow calls. | ||
| class IllegalWorkflowCallValidator | ||
| CallInfo = Data.define( | ||
| :class_name, | ||
| :method_name, | ||
| :trace_point | ||
| ) | ||
|
|
||
| # Call info passed to the validation block for each validation. | ||
| # | ||
| # @!attribute class_name | ||
| # @return [String] Class name the method is on. | ||
| # @!attribute method_name | ||
| # @return [String] Method name being called. | ||
| # @!attribute trace_point | ||
| # @return [TracePoint] TracePoint instance for the call. | ||
| class CallInfo; end # rubocop:disable Lint/EmptyClass | ||
|
|
||
| # @return [Array<IllegalWorkflowCallValidator>] Set of advanced validators for Time calls. | ||
| def self.default_time_validators | ||
| @default_time_validators ||= [ | ||
| # Do not consider initialize as invalid if year is present and not "true" | ||
| IllegalWorkflowCallValidator.new(method_name: :initialize) do |info| | ||
| year_val = info.trace_point.binding&.local_variable_get(:year) | ||
| raise 'can only use if passing string or explicit time info' unless year_val && year_val != true | ||
| end, | ||
| IllegalWorkflowCallValidator.new(method_name: :now) do | ||
| # When the xmlschema (aliased as iso8601) call is made, zone_offset is called which has a default parameter | ||
| # of Time.now.year. We want to prevent failing in that specific case. It is expensive to access the caller | ||
| # stack, but this is only done in the rare case they are calling this safely. | ||
| next if caller_locations&.any? { |loc| loc.label == 'Time.zone_offset' } | ||
|
|
||
| raise 'Invalid Time.now call' | ||
| end | ||
| ] | ||
| end | ||
|
|
||
| # @return [String, nil] Method name if this validator is specific to a method. | ||
| attr_reader :method_name | ||
|
|
||
| # @return [Proc] Block provided in constructor to invoke. See constructor for more details. | ||
| attr_reader :block | ||
|
|
||
| # Create a call validator. | ||
| # | ||
| # @param method_name [String, nil] Method name to check. This must be provided if the validator is in an illegal | ||
| # call array, this cannot be provided if it is a top-level class validator. | ||
| # @yield Required block that is called each time validation is needed. If the call raises, the exception message | ||
| # is used as the reason why the call is considered invalid. Return value is ignored. | ||
| # @yieldparam info [CallInfo] Information about the current call. | ||
| def initialize(method_name: nil, &block) | ||
| raise 'Block required' unless block_given? | ||
| raise TypeError, 'Method name must be Symbol' unless method_name.nil? || method_name.is_a?(Symbol) | ||
|
|
||
| @method_name = method_name | ||
| @block = block | ||
| 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
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
24 changes: 24 additions & 0 deletions
24
temporalio/sig/temporalio/worker/illegal_workflow_call_validator.rbs
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,24 @@ | ||
| module Temporalio | ||
| class Worker | ||
| class IllegalWorkflowCallValidator | ||
| class CallInfo | ||
| attr_reader class_name: String | ||
| attr_reader method_name: Symbol | ||
| attr_reader trace_point: TracePoint | ||
|
|
||
| def initialize: ( | ||
| class_name: String, | ||
| method_name: Symbol, | ||
| trace_point: TracePoint | ||
| ) -> void | ||
| end | ||
|
|
||
| def self.default_time_validators: -> Array[IllegalWorkflowCallValidator] | ||
|
|
||
| attr_reader method_name: Symbol? | ||
| attr_reader block: ^(CallInfo) -> void | ||
|
|
||
| def initialize: (?method_name: Symbol?) { (CallInfo) -> void } -> void | ||
| end | ||
| end | ||
| end |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we? I mean, it's technically possible that a WFT isn't picked up across a year boundary. Not even that hard to have it happen if it's happening right around NYE UTC time.
IE: WFT timestamp is just pre-midnight, then replay happens after midnight and we end up assuming it's that time except a year forward.
Or, is what's actually happening here that the year has to be provided and this call is still made anyway? Not immediately obvious to me.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO yes. Here's what's happening...
A user reported they can't even parse an ISO8601 time in a workflow (totally deterministic) because of this limitation. The line of code at https://github.com/ruby/ruby/blob/5124f9ac7513eb590c37717337c430cb93caa151/lib/time.rb#L640 is triggering it. During ISO-8601 parse the Ruby code needs zone offset, but obtaining zone offset (https://github.com/ruby/ruby/blob/5124f9ac7513eb590c37717337c430cb93caa151/lib/time.rb#L82) asks for current year as a parameter default it doesn't even really use for these cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, so, to clarify we're not actually getting the year from that, the call is just made anyway, even though we require the year to explicitly be set or set it ourselves?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly. It's a default parameter evaluated at invocation that is not even used in this case.