Class: Temporalio::Internal::Worker::WorkflowInstance::Scheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/temporalio/internal/worker/workflow_instance/scheduler.rb

Overview

Deterministic Fiber::Scheduler implementation.

Instance Method Summary collapse

Constructor Details

#initialize(instance) ⇒ Scheduler

Returns a new instance of Scheduler.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/temporalio/internal/worker/workflow_instance/scheduler.rb', line 17

def initialize(instance)
  @instance = instance
  @fibers = []
  @ready = []
  @wait_conditions = {}
  @wait_condition_counter = 0
  @thread_blocking_fibers = {}
  @thread_blocking_mutex = Mutex.new
  @thread_blocking_condition = ConditionVariable.new
  @workflow_thread = nil
end

Instance Method Details

#block(blocker, timeout = nil) ⇒ Object

Fiber::Scheduler methods

Note, we do not implement many methods here such as io_read and such. While it might seem to make sense to implement them and raise, we actually want to default to the blocking behavior of them not being present. This is so advanced things like logging still work inside of workflows. So we only implement the bare minimum.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/temporalio/internal/worker/workflow_instance/scheduler.rb', line 125

def block(blocker, timeout = nil)
  # TODO(cretz): Make the blocker visible in the stack trace?

  # Protobuf's object cache uses a process-local Mutex. We allowlist that Mutex use, but it is not a durable
  # workflow yield: if it blocks the only runnable workflow fiber, the workflow task must remain blocked so
  # deadlock detection can fail it instead of completing a partial command set.
  if timeout.nil? && thread_blocking_protobuf_mutex_wait?(blocker)
    fiber = Fiber.current
    synchronize_thread_blocking_fibers { @thread_blocking_fibers[fiber] = true }
    begin
      Fiber.yield
      return true
    ensure
      synchronize_thread_blocking_fibers do
        @thread_blocking_fibers.delete(fiber)
        @thread_blocking_condition.broadcast
      end
    end
  end

  # We just yield because unblock will resume this. We will just wrap in timeout if needed.
  if timeout
    begin
      Workflow.timeout(timeout) { Fiber.yield }
      true
    rescue Timeout::Error
      false
    end
  else
    Fiber.yield
    true
  end
end

#closeObject



159
160
161
# File 'lib/temporalio/internal/worker/workflow_instance/scheduler.rb', line 159

def close
  # Nothing to do here, lifetime of scheduler is controlled by the instance
end

#contextObject



29
30
31
# File 'lib/temporalio/internal/worker/workflow_instance/scheduler.rb', line 29

def context
  @instance.context
end

#fiber(&block) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/temporalio/internal/worker/workflow_instance/scheduler.rb', line 163

def fiber(&block)
  if @instance.context_frozen
    raise Workflow::InvalidWorkflowStateError, 'Cannot schedule fibers in this context'
  end

  fiber = Fiber.new do
    block.call # steep:ignore
  ensure
    @fibers.delete(Fiber.current)
  end
  @fibers << fiber
  @ready << fiber
  fiber
end

#fiber_interrupt(fiber, exception) ⇒ Object



178
179
180
# File 'lib/temporalio/internal/worker/workflow_instance/scheduler.rb', line 178

def fiber_interrupt(fiber, exception)
  fiber.raise(exception) if fiber.alive?
end

#io_wait(io, events, timeout) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/temporalio/internal/worker/workflow_instance/scheduler.rb', line 182

def io_wait(io, events, timeout)
  # Do not allow if IO disabled
  unless @instance.io_enabled
    raise Workflow::NondeterminismError,
          'Cannot perform IO from inside a workflow. If this is known to be safe, ' \
          'the code can be run in a Temporalio::Workflow::Unsafe.durable_scheduler_disabled ' \
          'or Temporalio::Workflow::Unsafe.io_enabled block.'
  end

  # Use regular Ruby behavior of blocking this thread. There is no Ruby implementation of io_wait we can just
  # delegate to at this time (or default scheduler or anything like that), so we had to implement this
  # ourselves.
  readers = events.nobits?(IO::READABLE) ? nil : [io]
  writers = events.nobits?(IO::WRITABLE) ? nil : [io]
  priority = events.nobits?(IO::PRIORITY) ? nil : [io]
  ready = IO.select(readers, writers, priority, timeout) # steep:ignore

  result = 0
  unless ready.nil?
    result |= IO::READABLE if ready[0]&.include?(io)
    result |= IO::WRITABLE if ready[1]&.include?(io)
    result |= IO::PRIORITY if ready[2]&.include?(io)
  end
  result
end

#kernel_sleep(duration = nil) ⇒ Object



208
209
210
# File 'lib/temporalio/internal/worker/workflow_instance/scheduler.rb', line 208

def kernel_sleep(duration = nil)
  Workflow.sleep(duration)
end

#process_wait(pid, flags) ⇒ Object

Raises:

  • (NotImplementedError)


212
213
214
# File 'lib/temporalio/internal/worker/workflow_instance/scheduler.rb', line 212

def process_wait(pid, flags)
  raise NotImplementedError, 'Cannot wait on other processes in workflows'
end

#run_until_all_yieldedObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/temporalio/internal/worker/workflow_instance/scheduler.rb', line 33

def run_until_all_yielded
  @workflow_thread = Thread.current
  loop do
    # Run all fibers until all yielded
    while (fiber = @ready.shift)
      fiber.resume
    end

    # Thread-blocking fibers are not durable workflow yields. Wait for them to unblock before satisfying any
    # wait condition so the condition sees the workflow after all runnable work has settled.
    next if wait_for_thread_blocking_fiber

    # Find the _first_ resolvable wait condition and if there, resolve it, and loop again, otherwise return.
    # It is important that we both let fibers get all settled _before_ this and only allow a _single_ wait
    # condition to be satisfied before looping. This allows wait condition users to trust that the line of
    # code after the wait condition still has the condition satisfied.
    # @type var cond_fiber: Fiber?
    cond_fiber = nil
    cond_result = nil
    @wait_conditions.each do |seq, cond|
      # Evaluate condition or skip if not true
      next unless (cond_result = cond.first.call)

      # There have been reports of this fiber being completed already, so we make sure not to process if it
      # has, but we still delete it
      deleted_cond = @wait_conditions.delete(seq)
      next unless deleted_cond&.last&.alive?

      cond_fiber = deleted_cond.last
      break
    end
    return unless cond_fiber

    cond_fiber.resume(cond_result)
  end
ensure
  @workflow_thread = nil
end

#stack_traceObject



106
107
108
109
110
111
112
113
# File 'lib/temporalio/internal/worker/workflow_instance/scheduler.rb', line 106

def stack_trace
  # Collect backtraces of known fibers, separating with a blank line. We make sure to remove any lines that
  # reference Temporal paths, and we remove any empty backtraces.
  dir_path = @instance.illegal_call_tracing_disabled { File.dirname(Temporalio._root_file_path) }
  @fibers.map do |fiber|
    fiber.backtrace.reject { |s| s.start_with?(dir_path) }.join("\n")
  end.reject(&:empty?).join("\n\n")
end

#timeout_after(duration, exception_class, *exception_arguments) ⇒ Object



216
217
218
# File 'lib/temporalio/internal/worker/workflow_instance/scheduler.rb', line 216

def timeout_after(duration, exception_class, *exception_arguments, &)
  context.timeout(duration, exception_class, *exception_arguments, summary: 'Timeout timer', &)
end

#unblock(_blocker, fiber) ⇒ Object



220
221
222
223
224
225
226
# File 'lib/temporalio/internal/worker/workflow_instance/scheduler.rb', line 220

def unblock(_blocker, fiber)
  synchronize_thread_blocking_fibers do
    @thread_blocking_fibers.delete(fiber)
    @ready << fiber
    @thread_blocking_condition.broadcast
  end
end

#wait_condition(cancellation:, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/temporalio/internal/worker/workflow_instance/scheduler.rb', line 72

def wait_condition(cancellation:, &block)
  raise Workflow::InvalidWorkflowStateError, 'Cannot wait in this context' if @instance.context_frozen

  if cancellation&.canceled?
    raise Error::CanceledError,
          cancellation.canceled_reason || 'Wait condition canceled before started'
  end

  seq = (@wait_condition_counter += 1)
  @wait_conditions[seq] = [block, Fiber.current]

  # Add a cancellation callback
  cancel_callback_key = cancellation&.add_cancel_callback do
    # Only if the condition is still present
    cond = @wait_conditions.delete(seq)
    if cond&.last&.alive?
      cond&.last&.raise(Error::CanceledError.new(cancellation&.canceled_reason || 'Wait condition canceled'))
    end
  end

  # This blocks until a resume is called on this fiber
  result = begin
    Fiber.yield
  ensure
    # Remove pending
    @wait_conditions.delete(seq)
  end

  # Remove cancellation callback (only needed on success)
  cancellation&.remove_cancel_callback(cancel_callback_key) if cancel_callback_key

  result
end