kino 0.7.2 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.yardopts +5 -0
- data/CHANGELOG.md +281 -0
- data/Cargo.lock +164 -95
- data/README.md +269 -438
- data/doc/README.md +27 -2
- data/doc/architecture.md +862 -43
- data/doc/benchmarks.md +444 -214
- data/doc/configuration.md +215 -0
- data/doc/operations.md +258 -0
- data/doc/rails-on-ractors.md +161 -49
- data/doc/streaming.md +52 -0
- data/doc/websockets.md +469 -0
- data/doc/why-kino.md +6 -6
- data/doc/worker-loop.md +462 -0
- data/ext/kino/Cargo.toml +17 -1
- data/ext/kino/build.rs +32 -1
- data/ext/kino/clippy.toml +10 -0
- data/ext/kino/src/access_log.rs +70 -92
- data/ext/kino/src/control.rs +26 -685
- data/ext/kino/src/counters.rs +132 -0
- data/ext/kino/src/cpus.rs +1 -18
- data/ext/kino/src/dispatch/tests.rs +731 -0
- data/ext/kino/src/dispatch.rs +276 -0
- data/ext/kino/src/doorbell.rs +108 -0
- data/ext/kino/src/env_strings.rs +26 -30
- data/ext/kino/src/framing.rs +110 -0
- data/ext/kino/src/gvl.rs +292 -66
- data/ext/kino/src/hijack/tests.rs +416 -0
- data/ext/kino/src/hijack.rs +471 -0
- data/ext/kino/src/inline/answer.rs +488 -0
- data/ext/kino/src/inline/conn.rs +1124 -0
- data/ext/kino/src/inline/deadline.rs +421 -0
- data/ext/kino/src/inline/encode.rs +119 -0
- data/ext/kino/src/inline/io.rs +487 -0
- data/ext/kino/src/inline/mod.rs +766 -0
- data/ext/kino/src/inline/parity.rs +862 -0
- data/ext/kino/src/inline/parse.rs +131 -0
- data/ext/kino/src/inline/spin.rs +428 -0
- data/ext/kino/src/inline/testing.rs +628 -0
- data/ext/kino/src/io_shards.rs +240 -192
- data/ext/kino/src/lane_room.rs +99 -0
- data/ext/kino/src/lib.rs +139 -77
- data/ext/kino/src/listen.rs +180 -12
- data/ext/kino/src/log.rs +12 -47
- data/ext/kino/src/logsink.rs +3 -9
- data/ext/kino/src/mono.rs +12 -17
- data/ext/kino/src/pin.rs +3 -3
- data/ext/kino/src/queue/mod.rs +54 -0
- data/ext/kino/src/queue/take.rs +503 -0
- data/ext/kino/src/queue/testing.rs +107 -0
- data/ext/kino/src/queue/tests.rs +581 -0
- data/ext/kino/src/queue/worker.rs +501 -0
- data/ext/kino/src/registry.rs +168 -379
- data/ext/kino/src/request.rs +834 -235
- data/ext/kino/src/response.rs +532 -94
- data/ext/kino/src/server/accept.rs +322 -0
- data/ext/kino/src/server/config.rs +138 -0
- data/ext/kino/src/server/handle.rs +247 -0
- data/ext/kino/src/server/intake.rs +217 -0
- data/ext/kino/src/server/mod.rs +157 -0
- data/ext/kino/src/server/natives.rs +223 -0
- data/ext/kino/src/server/serve/intake_tests.rs +458 -0
- data/ext/kino/src/server/serve.rs +436 -0
- data/ext/kino/src/server/tests.rs +532 -0
- data/ext/kino/src/shared_gvl.rs +50 -0
- data/ext/kino/src/slot/lane.rs +138 -0
- data/ext/kino/src/slot/life.rs +111 -0
- data/ext/kino/src/slot/load.rs +193 -0
- data/ext/kino/src/slot/mod.rs +494 -0
- data/ext/kino/src/slot/park.rs +82 -0
- data/ext/kino/src/stats.rs +936 -0
- data/ext/kino/src/symbols.rs +66 -0
- data/ext/kino/src/test_support.rs +24 -14
- data/ext/kino/src/testing.rs +409 -0
- data/ext/kino/src/ticker.rs +87 -0
- data/ext/kino/src/timer.rs +4 -7
- data/ext/kino/src/tls.rs +101 -131
- data/ext/kino/src/typed_data.rs +131 -0
- data/ext/kino/src/upgrade.rs +77 -0
- data/ext/kino/src/ws/codec.rs +470 -0
- data/ext/kino/src/ws/drive.rs +600 -0
- data/ext/kino/src/ws/handshake.rs +267 -0
- data/ext/kino/src/ws/mod.rs +317 -0
- data/ext/kino/src/ws/server_tests.rs +759 -0
- data/ext/kino/src/ws/socket.rs +279 -0
- data/ext/kino/src/ws/stats.rs +88 -0
- data/ext/kino/src/ws/testing.rs +156 -0
- data/ext/kino/src/ws/topics.rs +550 -0
- data/lib/kino/cli.rb +100 -9
- data/lib/kino/configuration.rb +32 -10
- data/lib/kino/crash_backoff.rb +56 -0
- data/lib/kino/input.rb +36 -17
- data/lib/kino/log.rb +8 -1
- data/lib/kino/monitor.rb +1 -1
- data/lib/kino/pool.rb +169 -0
- data/lib/kino/pool_scaler.rb +12 -13
- data/lib/kino/quarantine_monitor.rb +3 -3
- data/lib/kino/ractor_supervisor.rb +201 -161
- data/lib/kino/server.rb +291 -185
- data/lib/kino/slot_bank.rb +21 -9
- data/lib/kino/stream.rb +2 -2
- data/lib/kino/teardown.rb +33 -0
- data/lib/kino/templates/kino.rb.tt +41 -6
- data/lib/kino/threaded_pool.rb +87 -102
- data/lib/kino/version.rb +1 -1
- data/lib/kino/web_socket.rb +125 -0
- data/lib/kino/worker/loop.rb +319 -0
- data/lib/kino/worker.rb +345 -79
- data/lib/kino/worker_hooks.rb +7 -1
- data/lib/kino/worker_row.rb +15 -0
- data/lib/kino.rb +27 -3
- data/lib/rackup/handler/kino.rb +1 -1
- data/sig/kino.rbs +93 -10
- metadata +66 -3
- data/ext/kino/src/queue.rs +0 -333
- data/ext/kino/src/server.rs +0 -1785
|
@@ -3,65 +3,26 @@
|
|
|
3
3
|
module Kino
|
|
4
4
|
# @private
|
|
5
5
|
# Spawns worker ractors and keeps them alive. One supervisor thread per
|
|
6
|
-
# ractor:
|
|
7
|
-
#
|
|
8
|
-
#
|
|
9
|
-
# worker retired by the pool scaler) end supervision.
|
|
6
|
+
# ractor: a crashed worker thread is replaced on a bank slot while its
|
|
7
|
+
# siblings serve on, and a dead ractor's requests get their 500 before it
|
|
8
|
+
# respawns, both paced by a CrashBackoff.
|
|
10
9
|
#
|
|
11
|
-
#
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
@draining = false
|
|
24
|
-
@lock = Mutex.new
|
|
10
|
+
# The :ractor-mode Pool.
|
|
11
|
+
class RactorSupervisor < Pool
|
|
12
|
+
MAIN_KILLED = "the worker ractor's main thread was killed"
|
|
13
|
+
# Seconds a worker ractor's main thread, ending early, gives its threads to end (.leave).
|
|
14
|
+
LEAVE = 1.0
|
|
15
|
+
# A worker thread's crash report, sent to its supervisor's port (.report_crash). The only
|
|
16
|
+
# other message there is Ractor#monitor's end notice (a Symbol up to Ruby 4.0, an Array from
|
|
17
|
+
# 4.1): a class of its own keeps the two apart.
|
|
18
|
+
Crash = Data.define(:position, :id, :error, :backtrace, :started_at, :crashed_at, :reply)
|
|
19
|
+
|
|
20
|
+
def initialize(...)
|
|
21
|
+
super
|
|
25
22
|
@supervisor_threads = []
|
|
26
|
-
@worker_slots = {}
|
|
27
|
-
@slot_to_worker = {}
|
|
28
23
|
@replaced = {}
|
|
29
|
-
# Worker index => true while its
|
|
30
|
-
# scaler asked it to leave. Retired workers hand their slots back to
|
|
31
|
-
# the bank for the next worker to take over.
|
|
24
|
+
# Worker index => true while its Ractor runs.
|
|
32
25
|
@live = {}
|
|
33
|
-
@retiring = {}
|
|
34
|
-
@bank = SlotBank.new(server_id)
|
|
35
|
-
# The first replacement's index; `replace` increments before using it,
|
|
36
|
-
# so this starts one below the first free index (@workers).
|
|
37
|
-
@next_worker_index = @workers - 1
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
def start
|
|
41
|
-
@supervisor_threads = Array.new(@workers) { |index| supervise(index) }
|
|
42
|
-
report_active
|
|
43
|
-
self
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
# Flag the drain and join supervisors up to the (numeric) deadline;
|
|
47
|
-
# callers wanting an unbounded wait use #join instead.
|
|
48
|
-
def shutdown(timeout)
|
|
49
|
-
@lock.synchronize { @draining = true }
|
|
50
|
-
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
|
|
51
|
-
@lock.synchronize { @supervisor_threads.dup }.each do |thread|
|
|
52
|
-
remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
53
|
-
thread.join([remaining, 0.01].max)
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
def done?
|
|
58
|
-
@lock.synchronize { @supervisor_threads.dup }.none?(&:alive?)
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
# Block until the workers exit on their own (drain elsewhere): join
|
|
62
|
-
# without flipping the draining flag.
|
|
63
|
-
def join
|
|
64
|
-
@lock.synchronize { @supervisor_threads.dup }.each(&:join)
|
|
65
26
|
end
|
|
66
27
|
|
|
67
28
|
# Ractors cannot be force-killed; their clients were already freed by
|
|
@@ -87,162 +48,241 @@ module Kino
|
|
|
87
48
|
# yet is not listed until it has).
|
|
88
49
|
def groups
|
|
89
50
|
@lock.synchronize do
|
|
90
|
-
@live.keys
|
|
91
|
-
.reject { |index| @retiring.key?(index) || @replaced.key?(index) || !@worker_slots.key?(index) }
|
|
92
|
-
.to_h { |index| [index, @worker_slots[index].dup] }
|
|
51
|
+
@slots.slice(*@live.keys).reject { |index, _| @retiring.key?(index) || @replaced.key?(index) }
|
|
93
52
|
end
|
|
94
53
|
end
|
|
95
54
|
|
|
96
|
-
#
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
# Send a worker home. Its slots stop receiving work now; the worker
|
|
106
|
-
# finishes what it holds, leaves at its next idle tick, and its slots
|
|
107
|
-
# come back to the free list once the ractor has exited. Returns
|
|
108
|
-
# false when there is no such live worker to retire.
|
|
109
|
-
def retire(worker_index)
|
|
110
|
-
slot_ids = @lock.synchronize do
|
|
111
|
-
next nil unless @live.key?(worker_index) && !@retiring.key?(worker_index)
|
|
55
|
+
# Replace the Ractor on slot `worker_id` at `generation`: spawn a fresh one, then quarantine
|
|
56
|
+
# the old one's slots; the old Ractor leaks, unrespawned. False for a slot that moved on.
|
|
57
|
+
def replace(worker_id, generation)
|
|
58
|
+
# Checked and claimed at once, so a slot that moves on cannot get a healthy Ractor
|
|
59
|
+
# quarantined; the slots are marked then (see Pool). A stale snapshot replaces a Ractor once.
|
|
60
|
+
worker_index, slot_ids = @lock.synchronize do
|
|
61
|
+
index = @owner[worker_id]
|
|
62
|
+
next unless index && @bank.generation(worker_id) == generation && !@replaced.key?(index)
|
|
112
63
|
|
|
113
|
-
@
|
|
114
|
-
@
|
|
64
|
+
@replaced[index] = true
|
|
65
|
+
@slots[index].each { |id| @quarantined[id] = true }
|
|
66
|
+
[index, @slots[index].map { |id| [id, @bank.generation(id)] }]
|
|
115
67
|
end
|
|
116
|
-
return false unless slot_ids
|
|
117
|
-
|
|
118
|
-
slot_ids.each { |id| Native.retire_slot(@server_id, id) }
|
|
119
|
-
true
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
# Replace the ractor owning slot `worker_id`: spawn a fresh supervised
|
|
123
|
-
# ractor, then quarantine the old ractor's slots. The old supervisor
|
|
124
|
-
# thread stays blocked in ractor.value on the wedged ractor (it and the
|
|
125
|
-
# ractor leak until process exit; a wedged ractor cannot be
|
|
126
|
-
# force-killed). Returns true if a replacement was spawned.
|
|
127
|
-
def replace(worker_id)
|
|
128
|
-
worker_index = @lock.synchronize { @slot_to_worker[worker_id] }
|
|
129
68
|
return false unless worker_index
|
|
130
69
|
|
|
131
|
-
# Idempotent per ractor: a stale monitor snapshot can list two sibling
|
|
132
|
-
# slots of the same ractor, only the first replaces it.
|
|
133
|
-
claimed = @lock.synchronize do
|
|
134
|
-
if @replaced.key?(worker_index)
|
|
135
|
-
false
|
|
136
|
-
else
|
|
137
|
-
@replaced[worker_index] = true
|
|
138
|
-
true
|
|
139
|
-
end
|
|
140
|
-
end
|
|
141
|
-
return false unless claimed
|
|
142
|
-
|
|
143
|
-
new_index = @lock.synchronize { @next_worker_index += 1 }
|
|
144
70
|
thread =
|
|
145
71
|
begin
|
|
146
|
-
supervise(
|
|
72
|
+
supervise(next_index) # spawn FIRST, nothing quarantined yet
|
|
147
73
|
rescue
|
|
148
74
|
@lock.synchronize { @replaced.delete(worker_index) } # allow retry next tick
|
|
75
|
+
unmark(slot_ids.map(&:first))
|
|
149
76
|
raise
|
|
150
77
|
end
|
|
151
|
-
|
|
152
|
-
slot_ids.each { |id|
|
|
78
|
+
# Quarantine only after success; each slot's sockets close with 1011.
|
|
79
|
+
slot_ids.each { |id, slot_generation| quarantine(id, slot_generation) }
|
|
153
80
|
@lock.synchronize { @supervisor_threads << thread }
|
|
154
81
|
true
|
|
155
82
|
end
|
|
156
83
|
|
|
84
|
+
# @private
|
|
85
|
+
# A worker Ractor's main thread, one worker thread per slot; a crashed one waits for a new slot.
|
|
86
|
+
# True once no worker thread is left; nil when its main thread was killed.
|
|
87
|
+
def self.run_worker(server_id, ids, app, batch, hooks, supervisor)
|
|
88
|
+
# Crashes are the supervisor's to log; no thread here prints one.
|
|
89
|
+
Thread.current.report_on_exception = false
|
|
90
|
+
ended = Thread::Queue.new
|
|
91
|
+
# The worker threads, and the threads they start.
|
|
92
|
+
workers = ThreadGroup.new
|
|
93
|
+
# Up once this thread leaves: its worker threads end with it, reporting nothing.
|
|
94
|
+
over = left = false
|
|
95
|
+
start = lambda do |id, position|
|
|
96
|
+
Thread.new do
|
|
97
|
+
Thread.current.report_on_exception = false
|
|
98
|
+
Thread.current.name = "thread-#{position + 1}" if ids.size > 1
|
|
99
|
+
started_at = Kino.monotonic
|
|
100
|
+
error = fresh = nil
|
|
101
|
+
done = false
|
|
102
|
+
begin
|
|
103
|
+
Worker.run(server_id, id, app, batch, hooks)
|
|
104
|
+
done = true
|
|
105
|
+
rescue Exception => e # rubocop:disable Lint/RescueException -- reported, then replaced
|
|
106
|
+
error = e
|
|
107
|
+
ensure
|
|
108
|
+
# Neither its end nor an exception: its thread was killed.
|
|
109
|
+
error ||= Error.new(KILLED) unless done || over
|
|
110
|
+
fresh = report_crash(supervisor, position, id, error, started_at) if error && !over
|
|
111
|
+
replaced = begin
|
|
112
|
+
fresh && !over && start.call(fresh, position)
|
|
113
|
+
rescue ThreadError
|
|
114
|
+
nil
|
|
115
|
+
end
|
|
116
|
+
# A respawn counts once its thread runs (as the main thread is killed, the ractor's).
|
|
117
|
+
if replaced
|
|
118
|
+
Native.record_respawn(server_id)
|
|
119
|
+
else
|
|
120
|
+
ended << position
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end.tap { |thread| workers.add(thread) }
|
|
124
|
+
end
|
|
125
|
+
ids.each_with_index { |id, position| start.call(id, position) }
|
|
126
|
+
ids.size.times { ended.pop }
|
|
127
|
+
left = true
|
|
128
|
+
ensure
|
|
129
|
+
over = true
|
|
130
|
+
leave(workers) unless left
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# The main thread ends with worker threads still running: they end first, while the ractor
|
|
134
|
+
# lives, as a teardown's ensures run after Ractor#value hands their slots on, and must not touch
|
|
135
|
+
# a Ractor::Port. One past LEAVE goes with the teardown, its handle holding its slot.
|
|
136
|
+
def self.leave(workers)
|
|
137
|
+
deadline = Kino.monotonic + LEAVE
|
|
138
|
+
workers.list.each(&:kill)
|
|
139
|
+
workers.list.each do |thread|
|
|
140
|
+
Teardown.join(thread, [deadline - Kino.monotonic, 0].max)
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
private_class_method :leave
|
|
144
|
+
|
|
145
|
+
# Report a crash and return the answer; the exception crosses as a copy, its backtrace aside.
|
|
146
|
+
def self.report_crash(supervisor, position, id, error, started_at)
|
|
147
|
+
crashed_at = Kino.monotonic
|
|
148
|
+
reply = Ractor::Port.new
|
|
149
|
+
crash = Crash.new(position, id, error, error.backtrace, started_at, crashed_at, reply)
|
|
150
|
+
begin
|
|
151
|
+
supervisor << crash
|
|
152
|
+
rescue TypeError, Ractor::Error
|
|
153
|
+
supervisor << crash.with(error: Error.new("#{error.class}: #{error.message}"))
|
|
154
|
+
end
|
|
155
|
+
reply.receive
|
|
156
|
+
ensure
|
|
157
|
+
reply&.close
|
|
158
|
+
end
|
|
159
|
+
private_class_method :report_crash
|
|
160
|
+
|
|
157
161
|
private
|
|
158
162
|
|
|
163
|
+
def add_worker
|
|
164
|
+
index = next_index
|
|
165
|
+
thread = supervise(index)
|
|
166
|
+
@lock.synchronize { @supervisor_threads << thread }
|
|
167
|
+
index
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def join_set = @lock.synchronize { @supervisor_threads.dup }
|
|
171
|
+
|
|
172
|
+
def member?(index) = @live.key?(index)
|
|
173
|
+
|
|
159
174
|
def supervise(index)
|
|
160
175
|
# Live from the moment it is asked for, not from when its thread gets
|
|
161
176
|
# around to spawning: `grow` reports the count right after this.
|
|
162
177
|
@lock.synchronize { @live[index] = true }
|
|
163
178
|
Thread.new do
|
|
164
179
|
Thread.current.name = "supervisor-#{index}"
|
|
165
|
-
|
|
180
|
+
backoff = CrashBackoff.new("worker-#{index}")
|
|
166
181
|
loop do
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
182
|
+
port = Ractor::Port.new
|
|
183
|
+
ractor = spawn_worker(index, port)
|
|
184
|
+
backoff.started
|
|
185
|
+
tend(index, ractor, port)
|
|
186
|
+
cause = begin
|
|
187
|
+
# A ractor whose main thread was killed ends with no value.
|
|
188
|
+
Error.new(MAIN_KILLED) unless ractor.value
|
|
173
189
|
rescue Ractor::Error => e
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
exited(index, nil)
|
|
182
|
-
break
|
|
183
|
-
end
|
|
190
|
+
(e.respond_to?(:cause) && e.cause) ? e.cause : e
|
|
191
|
+
end
|
|
192
|
+
unless cause
|
|
193
|
+
worker_exited(index, nil) # drained or retired
|
|
194
|
+
exited(index, clean: true)
|
|
195
|
+
break
|
|
196
|
+
end
|
|
184
197
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
198
|
+
# The ractor died mid-flight. Anything it was serving will never
|
|
199
|
+
# be answered by Ruby: 500 those clients NOW (not when GC gets
|
|
200
|
+
# around to dropping the dead heap), then decide on respawn.
|
|
201
|
+
# Its sockets close too: their handlers died with it.
|
|
202
|
+
dead = @lock.synchronize { @slots[index] }
|
|
203
|
+
dead.each { |id| Native.abort_inflight(@server_id, id) }
|
|
204
|
+
# A replaced worker is not respawned; a leaving one is, and the scaler retires it again.
|
|
205
|
+
respawn = recover(index, dead, cause, backoff, leaving_respawns: true) { worker_exited(index, cause) }
|
|
206
|
+
unless respawn
|
|
207
|
+
exited(index, clean: false)
|
|
208
|
+
break
|
|
196
209
|
end
|
|
210
|
+
|
|
211
|
+
Native.record_respawn(@server_id)
|
|
212
|
+
@lock.synchronize { @retiring.delete(index) }
|
|
197
213
|
end
|
|
198
214
|
end
|
|
199
215
|
end
|
|
200
216
|
|
|
217
|
+
# Answer each Crash on its own thread (each waits only its own delay) until the Ractor exits.
|
|
218
|
+
def tend(index, ractor, port)
|
|
219
|
+
ractor.monitor(port) # the notice comes after any crash report
|
|
220
|
+
backoffs = Array.new(@threads) { |position| CrashBackoff.for(index, position, @threads) }
|
|
221
|
+
run = {over: false}
|
|
222
|
+
while (crash = port.receive).is_a?(Crash)
|
|
223
|
+
Thread.new(crash) { |report| answer(index, report, backoffs, run) }
|
|
224
|
+
end
|
|
225
|
+
ensure
|
|
226
|
+
@lock.synchronize { run[:over] = true } if run
|
|
227
|
+
port.close
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def answer(index, crash, backoffs, run)
|
|
231
|
+
Thread.current.name = "supervisor-#{index}"
|
|
232
|
+
cause = crash.error
|
|
233
|
+
# A copied exception arrives without its backtrace.
|
|
234
|
+
cause.set_backtrace(crash.backtrace) if crash.backtrace && !cause.frozen?
|
|
235
|
+
backoff = backoffs[crash.position]
|
|
236
|
+
backoff.started(crash.started_at)
|
|
237
|
+
crash.reply << replace_thread(index, crash.id, cause, backoff, crash.crashed_at, run)
|
|
238
|
+
rescue Ractor::ClosedError
|
|
239
|
+
nil # the ractor is gone; the supervisor sees to its slots
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# Release the crashed thread's slot (unless wedged); after its backoff, a fresh one, if due.
|
|
243
|
+
def replace_thread(index, dead_id, cause, backoff, crashed_at, run)
|
|
244
|
+
return unless recover(index, [dead_id], cause, backoff, crashed_at) { worker_exited(index, cause) }
|
|
245
|
+
|
|
246
|
+
# The replacement counts as a respawn once it runs (run_worker).
|
|
247
|
+
claim_slot(index) { run[:over] || @retiring.key?(index) || @replaced.key?(index) }
|
|
248
|
+
rescue => e
|
|
249
|
+
replacement_failed(index, e)
|
|
250
|
+
nil
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
# Unmap before the bank may hand the slots on, so no generation pairs with a later worker.
|
|
254
|
+
def forget_slots(index, ids)
|
|
255
|
+
@lock.synchronize { [releasable(unmap(index, ids)), @replaced.key?(index), @retiring.key?(index)] }
|
|
256
|
+
end
|
|
257
|
+
|
|
201
258
|
# Fresh ractor on slots from the bank: fresh ones, or ones a retired
|
|
202
|
-
# worker handed back.
|
|
203
|
-
def spawn_worker(worker_index)
|
|
259
|
+
# worker handed back. It asks for replacement threads on `port`.
|
|
260
|
+
def spawn_worker(worker_index, port)
|
|
204
261
|
worker_ids = Array.new(@threads) { @bank.claim }
|
|
205
262
|
@lock.synchronize do
|
|
206
|
-
@
|
|
207
|
-
worker_ids.each { |id| @
|
|
263
|
+
@slots[worker_index] = worker_ids
|
|
264
|
+
worker_ids.each { |id| @owner[id] = worker_index }
|
|
208
265
|
end
|
|
209
266
|
# Named so log lines from inside say which worker spoke: the ractor
|
|
210
267
|
# alone for a single thread, `worker-N/thread-M` for more.
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
ids.each_with_index.map do |id, position|
|
|
214
|
-
Thread.new do
|
|
215
|
-
# Crashes surface via Ractor#value in the supervisor; don't also
|
|
216
|
-
# spray the backtrace to stderr from inside the dying ractor.
|
|
217
|
-
Thread.current.report_on_exception = false
|
|
218
|
-
Thread.current.name = "thread-#{position + 1}" if ids.size > 1
|
|
219
|
-
Kino::Worker.run(server_id, id, app, batch, hooks)
|
|
220
|
-
end
|
|
221
|
-
end.each(&:join)
|
|
268
|
+
Ractor.new(@server_id, worker_ids, @app, @batch, @hooks, port, name: "worker-#{worker_index}") do |*worker|
|
|
269
|
+
Kino::RactorSupervisor.run_worker(*worker)
|
|
222
270
|
end
|
|
223
|
-
[ractor, worker_ids]
|
|
224
271
|
end
|
|
225
272
|
|
|
226
273
|
# Bookkeeping for a supervisor thread that is done: the worker is no
|
|
227
274
|
# longer live, a retired worker's slots go back to the bank, and the
|
|
228
275
|
# thread leaves the join set so a long-lived elastic pool does not
|
|
229
276
|
# accumulate dead threads.
|
|
230
|
-
def exited(index,
|
|
231
|
-
|
|
277
|
+
def exited(index, clean:)
|
|
278
|
+
released = @lock.synchronize do
|
|
232
279
|
@live.delete(index)
|
|
233
280
|
@supervisor_threads.delete(Thread.current)
|
|
234
|
-
@retiring.delete(index)
|
|
281
|
+
retired = @retiring.delete(index)
|
|
282
|
+
(retired && clean) ? releasable(@slots[index]) : []
|
|
235
283
|
end
|
|
236
|
-
@bank.release(
|
|
284
|
+
@bank.release(released)
|
|
237
285
|
report_active
|
|
238
286
|
end
|
|
239
|
-
|
|
240
|
-
def report_active
|
|
241
|
-
Native.set_active_workers(@server_id, active_count)
|
|
242
|
-
end
|
|
243
|
-
|
|
244
|
-
def draining?
|
|
245
|
-
@lock.synchronize { @draining }
|
|
246
|
-
end
|
|
247
287
|
end
|
|
248
288
|
end
|