Class: Phronomy::Runtime
- Inherits:
-
Object
show all
- Defined in:
- lib/phronomy/engine/runtime.rb,
lib/phronomy/engine/runtime/scheduler.rb,
lib/phronomy/engine/runtime/timer_queue.rb,
lib/phronomy/engine/runtime/task_registry.rb,
lib/phronomy/engine/runtime/timer_service.rb,
lib/phronomy/engine/runtime/fake_scheduler.rb,
lib/phronomy/engine/runtime/runtime_metrics.rb,
lib/phronomy/engine/runtime/shutdown_result.rb,
lib/phronomy/engine/runtime/thread_scheduler.rb,
lib/phronomy/engine/runtime/deterministic_scheduler.rb,
lib/phronomy/engine/runtime/scheduler_timer_adapter.rb
Overview
Central authority for concurrent primitives.
Defined Under Namespace
Classes: DeterministicScheduler, FakeScheduler, RuntimeMetrics, Scheduler, SchedulerTimerAdapter, ShutdownResult, TaskRegistry, ThreadScheduler, TimerQueue, TimerService
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(scheduler: ThreadScheduler.new) ⇒ Runtime
Returns a new instance of Runtime.
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/phronomy/engine/runtime.rb', line 108
def initialize(scheduler: ThreadScheduler.new)
@scheduler = scheduler
@event_loop_scheduler = ThreadScheduler.new
@task_registry = TaskRegistry.new
@metrics = RuntimeMetrics.new
@timer_service = TimerService.new(scheduler)
@pool_registry = Phronomy::Concurrency::PoolRegistry.new(
timer_queue_provider: -> { @timer_service.timer_queue }
)
@lifecycle_mutex = Mutex.new
@shutdown_mutex = Mutex.new
@state = :running
@event_loop = nil
@failure = nil
@shutdown_result = nil
end
|
Instance Attribute Details
#scheduler ⇒ Object
Returns the value of attribute scheduler.
102
103
104
|
# File 'lib/phronomy/engine/runtime.rb', line 102
def scheduler
@scheduler
end
|
Class Method Details
.default_if_initialized_for_test ⇒ Object
26
27
28
|
# File 'lib/phronomy/engine/runtime.rb', line 26
def default_if_initialized_for_test
instance_mutex.synchronize { @instance }
end
|
.in_event_loop_context? ⇒ Boolean
58
59
60
61
|
# File 'lib/phronomy/engine/runtime.rb', line 58
def in_event_loop_context?
runtime = instance_mutex.synchronize { @instance }
runtime&.event_loop_current? || false
end
|
.in_scheduler_context? ⇒ Boolean
91
92
93
|
# File 'lib/phronomy/engine/runtime.rb', line 91
def self.in_scheduler_context?
!Task.current.nil?
end
|
.instance ⇒ Object
20
21
22
23
24
|
# File 'lib/phronomy/engine/runtime.rb', line 20
def instance
instance_mutex.synchronize do
@instance ||= build_default_runtime
end
end
|
.measure_ms ⇒ Object
95
96
97
98
99
100
|
# File 'lib/phronomy/engine/runtime.rb', line 95
def self.measure_ms
t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
result = yield
elapsed_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0) * 1000).round
[result, elapsed_ms]
end
|
.replace_default_for_test(runtime) ⇒ Object
30
31
32
33
34
35
36
|
# File 'lib/phronomy/engine/runtime.rb', line 30
def replace_default_for_test(runtime)
instance_mutex.synchronize do
previous = @instance
@instance = runtime
previous
end
end
|
.reset_default!(timeout: Phronomy.configuration.event_loop_stop_grace_seconds) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/phronomy/engine/runtime.rb', line 42
def reset_default!(timeout: Phronomy.configuration.event_loop_stop_grace_seconds)
runtime = instance_mutex.synchronize { @instance }
return ShutdownResult.not_started unless runtime
result = runtime.shutdown(timeout: timeout)
unless result.cleanup_complete?
raise Phronomy::RuntimeShutdownError,
"Runtime cleanup is incomplete; default Runtime was retained"
end
instance_mutex.synchronize do
@instance = nil if @instance.equal?(runtime)
end
result
end
|
.restore_default_for_test(runtime) ⇒ Object
38
39
40
|
# File 'lib/phronomy/engine/runtime.rb', line 38
def restore_default_for_test(runtime)
instance_mutex.synchronize { @instance = runtime }
end
|
Instance Method Details
#__event_loop_failed(error) ⇒ Object
233
234
235
236
237
238
239
240
|
# File 'lib/phronomy/engine/runtime.rb', line 233
def __event_loop_failed(error)
@lifecycle_mutex.synchronize do
return if @shutdown_result || @state == :terminated
@failure ||= error
@state = :failed
end
end
|
#__spawn_event_loop_service(&block) ⇒ Object
229
230
231
|
# File 'lib/phronomy/engine/runtime.rb', line 229
def __spawn_event_loop_service(&block)
@event_loop_scheduler.spawn(name: "event-loop", parent: nil, &block)
end
|
#blocking_io(pool_size: Phronomy.configuration.blocking_io_pool_size, queue_size: Phronomy.configuration.blocking_io_queue_size) ⇒ Object
189
190
191
192
193
194
195
|
# File 'lib/phronomy/engine/runtime.rb', line 189
def blocking_io(
pool_size: Phronomy.configuration.blocking_io_pool_size,
queue_size: Phronomy.configuration.blocking_io_queue_size
)
ensure_accepting_work!
@pool_registry.default_pool(pool_size: pool_size, queue_size: queue_size)
end
|
#event_loop ⇒ Object
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
# File 'lib/phronomy/engine/runtime.rb', line 207
def event_loop
@lifecycle_mutex.synchronize do
case @state
when :running
@event_loop ||= EventLoop.new(runtime: self)
when :draining
return @event_loop if @event_loop
raise Phronomy::RuntimeShutdownError,
"EventLoop was not initialized before Runtime shutdown began"
else
raise Phronomy::RuntimeShutdownError,
"Runtime is #{@state}; EventLoop is unavailable"
end
end
end
|
#event_loop_current? ⇒ Boolean
224
225
226
227
|
# File 'lib/phronomy/engine/runtime.rb', line 224
def event_loop_current?
event_loop = @lifecycle_mutex.synchronize { @event_loop }
event_loop&.current? || false
end
|
#non_yield_threshold_violation_count ⇒ Object
144
145
146
|
# File 'lib/phronomy/engine/runtime.rb', line 144
def non_yield_threshold_violation_count
@metrics.starvation_count
end
|
#pool(name, size: 10, queue_size: 100) ⇒ Object
197
198
199
200
|
# File 'lib/phronomy/engine/runtime.rb', line 197
def pool(name, size: 10, queue_size: 100)
ensure_accepting_work!
@pool_registry.named_pool(name, size: size, queue_size: queue_size)
end
|
#shutdown(timeout: Phronomy.configuration.event_loop_stop_grace_seconds, cancel_grace: timeout) ⇒ Object
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
# File 'lib/phronomy/engine/runtime.rb', line 242
def shutdown(
timeout: Phronomy.configuration.event_loop_stop_grace_seconds,
cancel_grace: timeout
)
if Phronomy::Task.current
raise Phronomy::RuntimeShutdownReentrancyError,
"Runtime#shutdown must be called from an external management thread"
end
validate_timeout!(timeout, :timeout)
validate_timeout!(cancel_grace, :cancel_grace)
@shutdown_mutex.synchronize do
return @shutdown_result if @shutdown_result
deadline = monotonic_now + timeout
event_loop = @lifecycle_mutex.synchronize do
@state = :draining unless @state == :failed
@event_loop
end
event_loop&.begin_draining
task_status = drain_runtime_work(event_loop, deadline)
@lifecycle_mutex.synchronize do
@state = :stopping unless @state == :failed
end
event_loop_status = if event_loop
event_loop.shutdown(deadline: deadline, cancel_grace: cancel_grace)
else
:not_started
end
subsystem_error = shutdown_pools_and_timer
final_task_status = @task_registry.empty? ? :empty : task_status
cleanup_complete = final_task_status == :empty &&
(!event_loop || !event_loop.task_alive?) &&
event_loop_status != :cancel_timeout &&
subsystem_error.nil?
failure = @lifecycle_mutex.synchronize { @failure } || subsystem_error
runtime_outcome = if failure || event_loop_status == :failed
:failed
else
:terminated
end
result = ShutdownResult.new(
runtime_outcome: runtime_outcome,
cleanup_status: cleanup_complete ? :complete : :incomplete,
event_loop_status: event_loop_status,
task_registry_status: final_task_status,
error: failure
)
@lifecycle_mutex.synchronize do
@state = cleanup_complete ? runtime_outcome : :failed
@shutdown_result = result
end
result
end
end
|
#spawn(name: nil, &block) ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/phronomy/engine/runtime.rb', line 157
def spawn(name: nil, &block)
ensure_accepting_work!
type = _task_type(name)
spawn_at = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
@metrics.record_start(type)
task = @scheduler.spawn(name: name, parent: Task.current) do
run_start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
@metrics.record_wait(run_start - spawn_at)
begin
result = block.call
@metrics.record_end(type, :completed, run_start)
result
rescue CancellationError
@metrics.record_end(type, :cancelled, run_start)
raise
rescue => error
@metrics.record_end(type, :failed, run_start)
raise error
ensure
current = Task.current
@task_registry.deregister(current) if current
end
end
@task_registry.register(task)
task
end
|
#state ⇒ Object
104
105
106
|
# File 'lib/phronomy/engine/runtime.rb', line 104
def state
@lifecycle_mutex.synchronize { @state }
end
|
#task_group(limit: Float::INFINITY, failure_policy: :fail_fast) ⇒ Object
152
153
154
155
|
# File 'lib/phronomy/engine/runtime.rb', line 152
def task_group(limit: Float::INFINITY, failure_policy: :fail_fast)
ensure_accepting_work!
TaskGroup.new(limit: limit, failure_policy: failure_policy, runtime: self)
end
|
#task_snapshot ⇒ Object
185
186
187
|
# File 'lib/phronomy/engine/runtime.rb', line 185
def task_snapshot
@metrics.snapshot
end
|
#timer_queue ⇒ Object
202
203
204
205
|
# File 'lib/phronomy/engine/runtime.rb', line 202
def timer_queue
ensure_accepting_work!
@timer_service.timer_queue
end
|
#yield ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/phronomy/engine/runtime.rb', line 125
def yield
if (threshold = Phronomy.configuration.blocking_detect_threshold_ms)
slice_start = Task.current_cpu_slice_start_ms
if slice_start
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) - slice_start
if elapsed > threshold
name = Task.current&.name || "unknown"
Phronomy.configuration.logger&.warn(
"[Phronomy] CPU-bound task detected: '#{name}' ran #{elapsed.round}ms " \
"without yielding (threshold: #{threshold}ms)"
)
@metrics.increment_starvation
end
end
end
Task.record_yield!
@scheduler.yield
end
|
#yield_if_needed(every: 1000) ⇒ Object
148
149
150
|
# File 'lib/phronomy/engine/runtime.rb', line 148
def yield_if_needed(every: 1000)
self.yield if (Task.increment_yield_counter! % every).zero?
end
|