Class: Phronomy::EventLoop

Inherits:
Object
  • Object
show all
Defined in:
lib/phronomy/engine/event_loop.rb

Overview

Runtime-owned FIFO event loop for FSMSession instances.

EventLoop owns the framework's sole control-plane OS thread. All session lifecycle progression happens by short event dispatches on this thread.

Constant Summary collapse

SYSTEM_CHANNEL_ID =
"__event_loop__"
QUEUE_BACKLOG_WARNING_THRESHOLD =
1_000
QUEUE_BACKLOG_WARNING_INTERVAL_SECONDS =
60.0

Instance Method Summary collapse

Constructor Details

#initialize(runtime:) ⇒ EventLoop

Returns a new instance of EventLoop.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/phronomy/engine/event_loop.rb', line 21

def initialize(runtime:)
  @runtime = runtime
  @queue = Phronomy::Concurrency::AsyncQueue.new
  @queue_metrics_mutex = Mutex.new
  @queue_depth = 0
  @max_queue_depth = 0
  @last_queue_backlog_warning_at = nil

  @fsms = {}
  @waiting = {}
  @admitted_session_ids = Set.new
  @workflow_admissions = {}

  @lifecycle_mutex = Mutex.new
  @idle_cond = ConditionVariable.new
  @shutdown_mutex = Mutex.new
  @state = :running
  @outstanding_sessions = 0
  @shutdown_status = nil

  @lag_mutex = Mutex.new
  @last_lag_ns = 0
  @max_lag_ns = 0
  @dispatch_count = 0
  @total_lag_ns = 0

  @thread = Thread.new { run_loop }
  @thread.name = "phronomy-event-loop"
end

Instance Method Details

#admit_workflow(thread_id, owner_fsm_session_id:) ⇒ Object

Reserves one logical Workflow thread for one concrete FSMSession execution. thread_id is durable Workflow identity; owner_fsm_session_id is the Runtime-only identity of the invocation/resume that currently owns it.

Raises:

  • (ArgumentError)


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/phronomy/engine/event_loop.rb', line 159

def admit_workflow(thread_id, owner_fsm_session_id:)
  key = thread_id.to_s
  owner = owner_fsm_session_id.to_s
  raise ArgumentError, "thread_id must not be empty" if key.empty?
  raise ArgumentError, "owner_fsm_session_id must not be empty" if owner.empty?

  @lifecycle_mutex.synchronize do
    ensure_accepting_registrations!
    current_owner = @workflow_admissions[key]
    if current_owner
      raise Phronomy::Error,
        "Workflow thread #{key.inspect} is already owned by " \
        "FSMSession #{current_owner.inspect}"
    end
    @workflow_admissions[key] = owner
  end
  true
end

#admitted_session?(session_id) ⇒ Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/phronomy/engine/event_loop.rb', line 231

def admitted_session?(session_id)
  @lifecycle_mutex.synchronize { @admitted_session_ids.include?(session_id) }
end

#average_lag_secondsObject



59
60
61
62
63
64
# File 'lib/phronomy/engine/event_loop.rb', line 59

def average_lag_seconds
  @lag_mutex.synchronize do
    return 0.0 if @dispatch_count.zero?
    @total_lag_ns.to_f / @dispatch_count / 1_000_000_000.0
  end
end

#begin_drainingObject



243
244
245
246
247
248
# File 'lib/phronomy/engine/event_loop.rb', line 243

def begin_draining
  @lifecycle_mutex.synchronize do
    @state = :draining if @state == :running
  end
  self
end

#current?Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/phronomy/engine/event_loop.rb', line 235

def current?
  Thread.current.equal?(@thread)
end

#idle?Boolean

Returns:

  • (Boolean)


250
251
252
# File 'lib/phronomy/engine/event_loop.rb', line 250

def idle?
  @lifecycle_mutex.synchronize { runtime_idle_locked? }
end

#last_lag_secondsObject



51
52
53
# File 'lib/phronomy/engine/event_loop.rb', line 51

def last_lag_seconds
  @lag_mutex.synchronize { @last_lag_ns } / 1_000_000_000.0
end

#max_lag_secondsObject



55
56
57
# File 'lib/phronomy/engine/event_loop.rb', line 55

def max_lag_seconds
  @lag_mutex.synchronize { @max_lag_ns } / 1_000_000_000.0
end

#max_queue_depthObject



70
71
72
# File 'lib/phronomy/engine/event_loop.rb', line 70

def max_queue_depth
  @queue_metrics_mutex.synchronize { @max_queue_depth }
end

#post(event) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/phronomy/engine/event_loop.rb', line 112

def post(event)
  queued_depth = nil
  accepted = @lifecycle_mutex.synchronize do
    next false unless accepting_events?

    terminal_session_id = nil
    if terminal_management_event?(event)
      terminal_session_id = event.payload.fetch(:session_id)
      @admitted_session_ids.delete(terminal_session_id)
    end

    begin
      queued_depth = enqueue([event, monotonic_nanoseconds])
    rescue
      @admitted_session_ids.add(terminal_session_id) if terminal_session_id
      raise
    end
    true
  end
  return false unless accepted

  check_queue_backlog(queued_depth, event)
  true
end

#post_to_session(event) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/phronomy/engine/event_loop.rb', line 137

def post_to_session(event)
  if event.target_id == SYSTEM_CHANNEL_ID
    raise ArgumentError, "post_to_session cannot target the system channel"
  end

  queued_depth = nil
  accepted = @lifecycle_mutex.synchronize do
    next false unless accepting_events?
    next false unless @admitted_session_ids.include?(event.target_id)

    queued_depth = enqueue([event, monotonic_nanoseconds])
    true
  end
  return false unless accepted

  check_queue_backlog(queued_depth, event)
  true
end

#post_to_workflow(thread_id:, event:, payload: nil) ⇒ Object

Resolves durable Workflow identity to the currently owning FSMSession and enqueues the event atomically with that ownership check.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/phronomy/engine/event_loop.rb', line 199

def post_to_workflow(thread_id:, event:, payload: nil)
  queued_depth = nil
  posted_event = nil
  accepted = @lifecycle_mutex.synchronize do
    next false unless accepting_events?

    owner = @workflow_admissions[thread_id.to_s]
    next false unless owner
    next false unless @admitted_session_ids.include?(owner)

    posted_event = Phronomy::Event.new(
      type: event.to_sym,
      target_id: owner,
      payload: payload
    )
    queued_depth = enqueue([posted_event, monotonic_nanoseconds])
    true
  end
  return false unless accepted

  check_queue_backlog(queued_depth, posted_event)
  true
end

#queue_depthObject



66
67
68
# File 'lib/phronomy/engine/event_loop.rb', line 66

def queue_depth
  @queue_metrics_mutex.synchronize { @queue_depth }
end

#register(fsm_session, completion: nil) ⇒ Object



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
105
106
107
108
109
110
# File 'lib/phronomy/engine/event_loop.rb', line 74

def register(fsm_session, completion: nil)
  if current? && !completion.is_a?(Phronomy::Task)
    raise Phronomy::Error,
      "Cannot call a synchronous invocation API from an EventLoop action. " \
      "Schedule work asynchronously instead."
  end

  completion_handle = completion || Phronomy::Concurrency::AsyncQueue.new
  event = Phronomy::Event.new(
    type: :start,
    target_id: SYSTEM_CHANNEL_ID,
    payload: {session: fsm_session, completion: completion_handle}
  )
  queued_depth = nil

  @lifecycle_mutex.synchronize do
    ensure_accepting_registrations!
    if @admitted_session_ids.include?(fsm_session.id)
      raise Phronomy::Error,
        "FSMSession #{fsm_session.id.inspect} is already registered"
    end

    @admitted_session_ids.add(fsm_session.id)
    @outstanding_sessions += 1
    begin
      queued_depth = enqueue([event, monotonic_nanoseconds])
    rescue
      @admitted_session_ids.delete(fsm_session.id)
      @outstanding_sessions -= 1
      @idle_cond.broadcast if runtime_idle_locked?
      raise
    end
  end

  check_queue_backlog(queued_depth, event)
  completion_handle
end

#release_workflow(thread_id, owner_fsm_session_id:) ⇒ Object

Releases a Workflow reservation only when the caller is its current owner. A failed competing admission can therefore never release another session's reservation during cleanup.



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/phronomy/engine/event_loop.rb', line 181

def release_workflow(thread_id, owner_fsm_session_id:)
  key = thread_id.to_s
  owner = owner_fsm_session_id.to_s
  @lifecycle_mutex.synchronize do
    next false unless @workflow_admissions[key] == owner

    @workflow_admissions.delete(key)
    @idle_cond.broadcast if runtime_idle_locked?
    true
  end
end

#shutdown(deadline:, cancel_grace: deadline) ⇒ Object

Legacy entry point kept for any callers that pass deadline:/cancel_grace:.



292
293
294
# File 'lib/phronomy/engine/event_loop.rb', line 292

def shutdown(deadline:, cancel_grace: deadline)
  stop_and_join(deadline: deadline)
end

#stateObject



239
240
241
# File 'lib/phronomy/engine/event_loop.rb', line 239

def state
  @lifecycle_mutex.synchronize { @state }
end

#stop_and_join(deadline:) ⇒ Object

Sends STOP to the queue and joins the EventLoop thread. Assumes sessions have already been drained before this call.



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/phronomy/engine/event_loop.rb', line 267

def stop_and_join(deadline:)
  @shutdown_mutex.synchronize do
    return @shutdown_status if @shutdown_status

    if state == :failed
      join_until(deadline)
      @shutdown_status = :failed
      return @shutdown_status
    end

    begin_stopping_if_idle
    join_until(deadline)

    @shutdown_status = if thread_alive?
      @lifecycle_mutex.synchronize { @state = :failed }
      :cancel_timeout
    elsif state == :failed
      :failed
    else
      finalize_terminated(:terminated)
    end
  end
end

#thread_alive?Boolean Also known as: task_alive?

Returns:

  • (Boolean)


296
297
298
# File 'lib/phronomy/engine/event_loop.rb', line 296

def thread_alive?
  @thread&.alive? || false
end

#wait_until_idle(deadline) ⇒ Object



254
255
256
257
258
259
260
261
262
263
# File 'lib/phronomy/engine/event_loop.rb', line 254

def wait_until_idle(deadline)
  @lifecycle_mutex.synchronize do
    until runtime_idle_locked?
      remaining = deadline - monotonic_now
      return false if remaining <= 0
      @idle_cond.wait(@lifecycle_mutex, remaining)
    end
    true
  end
end

#wakeObject

Interrupts the queue wait so EventLoop can recompute the next timer deadline.



224
225
226
227
228
229
# File 'lib/phronomy/engine/event_loop.rb', line 224

def wake
  @queue.push(WAKE)
  true
rescue ClosedQueueError
  false
end

#workflow_admission_owner(thread_id) ⇒ Object



193
194
195
# File 'lib/phronomy/engine/event_loop.rb', line 193

def workflow_admission_owner(thread_id)
  @lifecycle_mutex.synchronize { @workflow_admissions[thread_id.to_s] }
end