kino 0.7.1-aarch64-linux → 0.8.0-aarch64-linux
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 +286 -0
- 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/clippy.toml +10 -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/kino.so +0 -0
- 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 +14 -2
data/lib/kino/configuration.rb
CHANGED
|
@@ -9,7 +9,7 @@ module Kino
|
|
|
9
9
|
DEFAULTS = {
|
|
10
10
|
bind: "127.0.0.1",
|
|
11
11
|
port: 0,
|
|
12
|
-
workers: nil, # resolved
|
|
12
|
+
workers: nil, # resolved per mode in Server: CPUs, capped at RUBY_MAX_CPU in :ractor
|
|
13
13
|
max_workers: nil, # nil = fixed pool; above workers = elastic pool
|
|
14
14
|
scale_down_after: nil, # resolved to 30 seconds in Server
|
|
15
15
|
threads: nil, # resolved per mode in Server: 1 in :ractor, 3 in :threaded
|
|
@@ -20,13 +20,17 @@ module Kino
|
|
|
20
20
|
max_connections: nil, # nil = derive from the open-file limit
|
|
21
21
|
max_body_size: 50 * 1024 * 1024, # 50 MB; nil/0 = unlimited
|
|
22
22
|
batch: 1,
|
|
23
|
-
lanes:
|
|
23
|
+
lanes: nil, # resolved in Server: on with a worker_loop, off without
|
|
24
|
+
websocket_max_message: 1_048_576, # 1 MiB
|
|
25
|
+
websocket_max: nil, # nil = only max_connections bounds open sockets
|
|
26
|
+
websocket_ping_interval: 30, # seconds; nil or 0 = no pings, no dead-peer drops
|
|
24
27
|
log_requests: false,
|
|
25
28
|
on_error: nil,
|
|
26
29
|
after_boot: nil,
|
|
27
30
|
after_worker_boot: nil,
|
|
28
31
|
after_request_complete: nil,
|
|
29
32
|
on_worker_exit: nil,
|
|
33
|
+
worker_loop: nil,
|
|
30
34
|
shutdown_timeout: 30,
|
|
31
35
|
io_shards: false,
|
|
32
36
|
io_threads: nil,
|
|
@@ -130,9 +134,7 @@ module Kino
|
|
|
130
134
|
|
|
131
135
|
# @return [Hash{Symbol => Object}] every setting, defaults filled in
|
|
132
136
|
def to_h
|
|
133
|
-
SETTINGS.to_h { |key| [key, self[key]] }
|
|
134
|
-
h[:workers] ||= Kino.available_parallelism
|
|
135
|
-
end
|
|
137
|
+
SETTINGS.to_h { |key| [key, self[key]] }
|
|
136
138
|
end
|
|
137
139
|
|
|
138
140
|
# The settings Server.new accepts: everything except the keys only the
|
|
@@ -176,8 +178,9 @@ module Kino
|
|
|
176
178
|
# Port to listen on; 0 picks an ephemeral port.
|
|
177
179
|
def port(port) = @config.set(:port, Integer(port))
|
|
178
180
|
|
|
179
|
-
# Worker count (ractors in :ractor mode)
|
|
180
|
-
#
|
|
181
|
+
# Worker count (ractors in :ractor mode). Defaults to the CPU count;
|
|
182
|
+
# in :ractor mode, at most RUBY_MAX_CPU (8 unless set). The pool floor
|
|
183
|
+
# when max_workers is set.
|
|
181
184
|
def workers(count) = @config.set(:workers, Integer(count))
|
|
182
185
|
|
|
183
186
|
# Pool ceiling: under sustained queue pressure the pool grows past
|
|
@@ -187,6 +190,7 @@ module Kino
|
|
|
187
190
|
|
|
188
191
|
# Seconds a worker above the floor must sit idle before it is
|
|
189
192
|
# retired (default 30). Only meaningful with max_workers.
|
|
193
|
+
# A worker with open sockets or hijacked connections is busy, not idle.
|
|
190
194
|
def scale_down_after(seconds) = @config.set(:scale_down_after, seconds)
|
|
191
195
|
|
|
192
196
|
# Threads per worker (I/O concurrency inside one ractor); default is
|
|
@@ -196,7 +200,7 @@ module Kino
|
|
|
196
200
|
# Dispatch mode: :auto, :ractor, or :threaded.
|
|
197
201
|
def mode(mode) = @config.set(:mode, mode.to_sym)
|
|
198
202
|
|
|
199
|
-
# Bounded request-queue depth; overflow earns clients a 503.
|
|
203
|
+
# Bounded request-queue depth, at least 1; overflow earns clients a 503.
|
|
200
204
|
def queue_depth(depth) = @config.set(:queue_depth, Integer(depth))
|
|
201
205
|
|
|
202
206
|
# Seconds a request may wait for queue space before the 503.
|
|
@@ -216,9 +220,22 @@ module Kino
|
|
|
216
220
|
# Requests a worker may grab per queue visit (default 1).
|
|
217
221
|
def batch(count) = @config.set(:batch, Integer(count))
|
|
218
222
|
|
|
219
|
-
# EXPERIMENTAL per-worker lane dispatch.
|
|
223
|
+
# EXPERIMENTAL per-worker lane dispatch (default: on with a worker_loop, else off). Native
|
|
224
|
+
# WebSockets and Kino::Worker.on_wake hooks need it.
|
|
220
225
|
def lanes(enabled) = @config.set(:lanes, !!enabled)
|
|
221
226
|
|
|
227
|
+
# Largest WebSocket message a client may send, in bytes (default
|
|
228
|
+
# 1 MiB); a bigger one closes the socket with 1009.
|
|
229
|
+
def websocket_max_message(bytes) = @config.set(:websocket_max_message, Integer(bytes))
|
|
230
|
+
|
|
231
|
+
# Most native WebSockets open at once; past it, handshakes get a
|
|
232
|
+
# 503. nil (the default) leaves only max_connections.
|
|
233
|
+
def websocket_max(count) = @config.set(:websocket_max, count && Integer(count))
|
|
234
|
+
|
|
235
|
+
# Ping a WebSocket client silent this many seconds, and drop one silent for
|
|
236
|
+
# twice that (default 30); nil or 0 sends no pings.
|
|
237
|
+
def websocket_ping_interval(seconds) = @config.set(:websocket_ping_interval, seconds)
|
|
238
|
+
|
|
222
239
|
# Native access log: one status-colored line per request to stdout.
|
|
223
240
|
def log_requests(enabled) = @config.set(:log_requests, !!enabled)
|
|
224
241
|
|
|
@@ -245,7 +262,12 @@ module Kino
|
|
|
245
262
|
# error_or_nil). error is the crash cause, or nil on a clean exit.
|
|
246
263
|
def on_worker_exit(handler = nil, &block) = @config.set(:on_worker_exit, handler || block)
|
|
247
264
|
|
|
248
|
-
#
|
|
265
|
+
# Runs each worker's loop under the app's own fiber scheduler: called on the worker thread
|
|
266
|
+
# with a Kino::Worker::Loop, whose #serve(concurrency:) it calls inside the scheduler. Turns
|
|
267
|
+
# lanes on; must be Ractor-shareable in :ractor mode.
|
|
268
|
+
def worker_loop(handler = nil, &block) = @config.set(:worker_loop, handler || block)
|
|
269
|
+
|
|
270
|
+
# Graceful-shutdown drain deadline in seconds, not negative (Float::INFINITY: no deadline).
|
|
249
271
|
def shutdown_timeout(seconds) = @config.set(:shutdown_timeout, seconds)
|
|
250
272
|
|
|
251
273
|
# Run native HTTP I/O on current-thread shards instead of Tokio's shared pool.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Kino
|
|
4
|
+
# @private
|
|
5
|
+
# Paces the respawns of one worker and its replacements, in both pools. A worker that crashed
|
|
6
|
+
# within STEADY seconds of starting, before serving a request, waits a delay that doubles per
|
|
7
|
+
# crash in a row, FIRST up to LONGEST; any other respawns at once, and the count starts over.
|
|
8
|
+
# The first crash of a run is logged with its backtrace, each later one with a line.
|
|
9
|
+
class CrashBackoff
|
|
10
|
+
FIRST = 0.1
|
|
11
|
+
LONGEST = 5.0
|
|
12
|
+
STEADY = 1.0
|
|
13
|
+
|
|
14
|
+
# The backoff of thread `position` of worker `index`, named as the log names it:
|
|
15
|
+
# `worker-3`, or `worker-3/thread-2`.
|
|
16
|
+
def self.for(index, position, threads)
|
|
17
|
+
new((threads > 1) ? "worker-#{index}/thread-#{position + 1}" : "worker-#{index}")
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def initialize(name, now = Kino.monotonic)
|
|
21
|
+
@subject = "#{name} crashed"
|
|
22
|
+
@streak = 0
|
|
23
|
+
@delay = 0.0
|
|
24
|
+
started(now)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# The worker, or its replacement, starts now.
|
|
28
|
+
def started(now = Kino.monotonic)
|
|
29
|
+
@started_at = now
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Log a crash; the seconds to wait before the respawn (`respawn: false`: only log).
|
|
33
|
+
def crashed(cause, now = Kino.monotonic, respawn: true, served: false)
|
|
34
|
+
if served || now - @started_at >= STEADY
|
|
35
|
+
@streak = 0
|
|
36
|
+
@delay = 0.0
|
|
37
|
+
Log.failure(@subject + respawning(respawn, "at once"), cause)
|
|
38
|
+
else
|
|
39
|
+
@streak += 1
|
|
40
|
+
@delay = (@delay * 2).clamp(FIRST, LONGEST)
|
|
41
|
+
quick = "#{@subject} within a second of starting"
|
|
42
|
+
if @streak == 1
|
|
43
|
+
Log.failure(quick + respawning(respawn), cause)
|
|
44
|
+
else
|
|
45
|
+
Log.error("#{quick} again, #{@streak} times in a row (#{cause.class}: #{cause.message})" +
|
|
46
|
+
respawning(respawn))
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
@delay if respawn
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def respawning(respawn, at = format("in %.1f s", @delay)) = respawn ? "; respawning #{at}" : ""
|
|
55
|
+
end
|
|
56
|
+
end
|
data/lib/kino/input.rb
CHANGED
|
@@ -5,12 +5,14 @@ module Kino
|
|
|
5
5
|
# rack.input: forward-only reader over the native streaming body.
|
|
6
6
|
# Rack 3 dropped the rewindability requirement, which is exactly what makes
|
|
7
7
|
# streaming legal here. All output is binary, per spec.
|
|
8
|
+
# Reads take from an offset into the pulled bytes: no copy or slice per read.
|
|
8
9
|
class Input
|
|
9
10
|
CHUNK_SIZE = 65_536
|
|
10
11
|
|
|
11
12
|
def initialize(request)
|
|
12
13
|
@request = request
|
|
13
14
|
@buffer = (+"").force_encoding(Encoding::BINARY)
|
|
15
|
+
@pos = 0
|
|
14
16
|
@eof = false
|
|
15
17
|
end
|
|
16
18
|
|
|
@@ -19,28 +21,34 @@ module Kino
|
|
|
19
21
|
# read(n) -> String of up to n bytes, nil at EOF
|
|
20
22
|
# read(n, buf) -> fills buf, returns it (or nil at EOF)
|
|
21
23
|
def read(length = nil, buffer = nil)
|
|
22
|
-
out = buffer ? buffer.clear.force_encoding(Encoding::BINARY) : (+"").force_encoding(Encoding::BINARY)
|
|
23
|
-
|
|
24
24
|
if length.nil?
|
|
25
25
|
fill_all
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
length = available
|
|
27
|
+
else
|
|
28
|
+
fill(length)
|
|
29
|
+
if available.zero? && length.positive?
|
|
30
|
+
buffer&.clear
|
|
31
|
+
return nil
|
|
32
|
+
end
|
|
29
33
|
end
|
|
30
34
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
35
|
+
length = available if length > available
|
|
36
|
+
piece =
|
|
37
|
+
if buffer
|
|
38
|
+
buffer.force_encoding(Encoding::BINARY).bytesplice(0, buffer.bytesize, @buffer, @pos, length)
|
|
39
|
+
else
|
|
40
|
+
@buffer.byteslice(@pos, length)
|
|
41
|
+
end
|
|
42
|
+
@pos += length
|
|
43
|
+
piece
|
|
36
44
|
end
|
|
37
45
|
|
|
38
46
|
def gets
|
|
39
47
|
fill_until_newline
|
|
40
|
-
return nil if
|
|
48
|
+
return nil if available.zero?
|
|
41
49
|
|
|
42
|
-
index = @buffer.
|
|
43
|
-
index ?
|
|
50
|
+
index = @buffer.byteindex("\n", @pos)
|
|
51
|
+
read(index ? index - @pos + 1 : available)
|
|
44
52
|
end
|
|
45
53
|
|
|
46
54
|
def each
|
|
@@ -55,15 +63,26 @@ module Kino
|
|
|
55
63
|
|
|
56
64
|
private
|
|
57
65
|
|
|
66
|
+
def available = @buffer.bytesize - @pos
|
|
67
|
+
|
|
68
|
+
# A native chunk is a fresh binary string: with nothing left over, it becomes the buffer.
|
|
58
69
|
def pull
|
|
59
70
|
return if @eof
|
|
60
71
|
|
|
61
|
-
chunk = @request.
|
|
62
|
-
|
|
72
|
+
chunk = @request.read_some(CHUNK_SIZE)
|
|
73
|
+
return @eof = true unless chunk
|
|
74
|
+
|
|
75
|
+
if available.zero?
|
|
76
|
+
@buffer = chunk
|
|
77
|
+
else
|
|
78
|
+
@buffer = @buffer.byteslice(@pos, available) if @pos.positive?
|
|
79
|
+
@buffer << chunk
|
|
80
|
+
end
|
|
81
|
+
@pos = 0
|
|
63
82
|
end
|
|
64
83
|
|
|
65
84
|
def fill(length)
|
|
66
|
-
pull while
|
|
85
|
+
pull while available < length && !@eof
|
|
67
86
|
end
|
|
68
87
|
|
|
69
88
|
def fill_all
|
|
@@ -71,7 +90,7 @@ module Kino
|
|
|
71
90
|
end
|
|
72
91
|
|
|
73
92
|
def fill_until_newline
|
|
74
|
-
pull until @buffer.
|
|
93
|
+
pull until @buffer.byteindex("\n", @pos) || @eof
|
|
75
94
|
end
|
|
76
95
|
end
|
|
77
96
|
end
|
data/lib/kino/kino.so
CHANGED
|
Binary file
|
data/lib/kino/log.rb
CHANGED
|
@@ -59,9 +59,16 @@ module Kino
|
|
|
59
59
|
# @param status [Integer] the status the client got
|
|
60
60
|
# @return [void]
|
|
61
61
|
def exception(error, env, status: 500)
|
|
62
|
+
failure("#{status} #{env["REQUEST_METHOD"]} #{env["PATH_INFO"]}", error)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# @private
|
|
66
|
+
# The failed-request report: `subject` (a request line, a socket's callback), the error and
|
|
67
|
+
# where it raised, then the backtrace.
|
|
68
|
+
def failure(subject, error)
|
|
62
69
|
frames, depth = trace(error)
|
|
63
70
|
site = frames.first ? " (#{frames.first})" : ""
|
|
64
|
-
lines = ["#{
|
|
71
|
+
lines = ["#{subject} · #{error.class}: #{error.message}#{site}"]
|
|
65
72
|
frames.each { |frame| lines << " #{frame}" }
|
|
66
73
|
lines << " … #{depth - frames.size} more" if depth > frames.size
|
|
67
74
|
error(lines.join("\n"))
|
data/lib/kino/monitor.rb
CHANGED
data/lib/kino/pool.rb
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Kino
|
|
4
|
+
# @private
|
|
5
|
+
# What RactorSupervisor and ThreadedPool share; they define add_worker, active_count, groups,
|
|
6
|
+
# replace, kill_stragglers, join_set, member? and forget_slots. @lock guards the tables only:
|
|
7
|
+
# nothing claims a slot or starts a thread under it, and what a decision reads shares the
|
|
8
|
+
# critical section of its change. Quarantine: `replace` marks slots first; a crash or exit
|
|
9
|
+
# meanwhile withholds them from the bank, and `unmark` releases them if the quarantine fails.
|
|
10
|
+
class Pool
|
|
11
|
+
# The crash of a worker thread killed short of its loop's end, which no rescue sees.
|
|
12
|
+
KILLED = "the worker's thread was killed"
|
|
13
|
+
|
|
14
|
+
def initialize(server_id, app, workers:, threads:, batch: 1, hooks: nil, on_worker_exit: nil)
|
|
15
|
+
@server_id = server_id
|
|
16
|
+
@app = app
|
|
17
|
+
@workers = workers
|
|
18
|
+
@threads = threads
|
|
19
|
+
@batch = batch
|
|
20
|
+
@hooks = hooks
|
|
21
|
+
@on_worker_exit = on_worker_exit
|
|
22
|
+
@lock = Mutex.new
|
|
23
|
+
@bank = SlotBank.new(server_id)
|
|
24
|
+
# Worker index => slot ids, and slot id => worker index. The lists are replaced, never
|
|
25
|
+
# changed in place, so a read under @lock stays a snapshot.
|
|
26
|
+
@slots = {}
|
|
27
|
+
@owner = {}
|
|
28
|
+
# Worker index => true once the scaler asked it to leave.
|
|
29
|
+
@retiring = {}
|
|
30
|
+
@next_index = -1
|
|
31
|
+
# Slot id => true once `replace` marked it; and the marked slots a crash or exit gave up.
|
|
32
|
+
@quarantined = {}
|
|
33
|
+
@withheld = {}
|
|
34
|
+
# Closed at shutdown: no respawn after it, and a waiting respawn stops waiting.
|
|
35
|
+
@halt = Thread::Queue.new
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def start
|
|
39
|
+
@workers.times { add_worker }
|
|
40
|
+
report_active
|
|
41
|
+
self
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def grow
|
|
45
|
+
index = add_worker
|
|
46
|
+
report_active
|
|
47
|
+
index
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Send worker `index` home: no new work, it leaves when idle, its sockets close with 1001.
|
|
51
|
+
def retire(index)
|
|
52
|
+
slot_ids = @lock.synchronize do
|
|
53
|
+
next nil unless member?(index) && !@retiring.key?(index)
|
|
54
|
+
|
|
55
|
+
@retiring[index] = true
|
|
56
|
+
@slots[index].map { |id| [id, @bank.generation(id)] }
|
|
57
|
+
end
|
|
58
|
+
return false unless slot_ids
|
|
59
|
+
|
|
60
|
+
slot_ids.each { |id, generation| Native.retire_slot(@server_id, id, generation) }
|
|
61
|
+
true
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Flag the drain and join the threads for up to `timeout` seconds; dead ones count as joined.
|
|
65
|
+
def shutdown(timeout)
|
|
66
|
+
@halt.close
|
|
67
|
+
Teardown.join_all(timeout) { join_set }
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def done?
|
|
71
|
+
join_set.none?(&:alive?)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
private
|
|
75
|
+
|
|
76
|
+
def next_index = @lock.synchronize { @next_index += 1 }
|
|
77
|
+
|
|
78
|
+
# Crash recovery for worker `index` on slots `ids`: release all but the marked, report the exit
|
|
79
|
+
# (the block), and pace the respawn by `backoff`; false if wedged, leaving or draining.
|
|
80
|
+
def recover(index, ids, cause, backoff, crashed_at = nil, leaving_respawns: false)
|
|
81
|
+
# Read before the bank may hand the slots on and reset their counts.
|
|
82
|
+
served = @bank.served_any?(ids)
|
|
83
|
+
released, wedged, leaving = forget_slots(index, ids)
|
|
84
|
+
@bank.release(released)
|
|
85
|
+
yield if block_given?
|
|
86
|
+
crashed_at ||= Kino.monotonic
|
|
87
|
+
respawn = !wedged && (leaving_respawns || !leaving) && !draining?
|
|
88
|
+
delay = backoff.crashed(cause, crashed_at, respawn: respawn, served: served)
|
|
89
|
+
!delay.nil? && wait_to_respawn(crashed_at + delay)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# A bank slot mapped to worker `index`; nil when the block (asked twice) calls it off.
|
|
93
|
+
def claim_slot(index)
|
|
94
|
+
return if @lock.synchronize { yield }
|
|
95
|
+
|
|
96
|
+
id = @bank.claim
|
|
97
|
+
kept = @lock.synchronize do
|
|
98
|
+
next false if yield
|
|
99
|
+
|
|
100
|
+
map_slot(index, id)
|
|
101
|
+
true
|
|
102
|
+
end
|
|
103
|
+
return id if kept
|
|
104
|
+
|
|
105
|
+
@bank.release([id])
|
|
106
|
+
nil
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Under @lock: slot `id` is worker `index`'s now.
|
|
110
|
+
def map_slot(index, id)
|
|
111
|
+
@slots[index] += [id]
|
|
112
|
+
@owner[id] = index
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Under @lock: unmap the slots of `ids` still worker `index`'s; returns them.
|
|
116
|
+
def unmap(index, ids)
|
|
117
|
+
ids.select { |id| @owner[id] == index }.each do |id|
|
|
118
|
+
@owner.delete(id)
|
|
119
|
+
@slots[index] -= [id]
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Under @lock: `ids` but the marked ones, which are withheld for `unmark`.
|
|
124
|
+
def releasable(ids)
|
|
125
|
+
marked, free = ids.partition { |id| @quarantined.key?(id) }
|
|
126
|
+
marked.each { |id| @withheld[id] = true }
|
|
127
|
+
free
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# Quarantine marked slot `id` if still at `generation` (sockets close with 1011); else unmark.
|
|
131
|
+
def quarantine(id, generation)
|
|
132
|
+
return true if Native.quarantine_slot(@server_id, id, generation)
|
|
133
|
+
|
|
134
|
+
unmark([id])
|
|
135
|
+
false
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Unmark `ids`, releasing those a crash or exit withheld meanwhile.
|
|
139
|
+
def unmark(ids)
|
|
140
|
+
withheld = @lock.synchronize do
|
|
141
|
+
ids.select do |id|
|
|
142
|
+
@quarantined.delete(id)
|
|
143
|
+
@withheld.delete(id)
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
@bank.release(withheld)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# on_worker_exit for worker (in :threaded mode, slot) `index`; `cause` is nil for a clean exit.
|
|
150
|
+
def worker_exited(index, cause) = HookFire.fire(@on_worker_exit, "on_worker_exit", index, cause)
|
|
151
|
+
|
|
152
|
+
def report_active
|
|
153
|
+
Native.set_active_workers(@server_id, active_count)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def draining? = @halt.closed?
|
|
157
|
+
|
|
158
|
+
# Wait until `deadline` for a respawn; false once the pool is shutting down.
|
|
159
|
+
def wait_to_respawn(deadline)
|
|
160
|
+
remaining = deadline - Kino.monotonic
|
|
161
|
+
@halt.pop(timeout: remaining) if remaining.positive?
|
|
162
|
+
!draining?
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def replacement_failed(index, error)
|
|
166
|
+
Log.error("worker-#{index} was not given a replacement thread: #{error.class}: #{error.message}")
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
end
|
data/lib/kino/pool_scaler.rb
CHANGED
|
@@ -12,9 +12,9 @@ module Kino
|
|
|
12
12
|
# the queue on two consecutive ticks (a burst that clears within a tick
|
|
13
13
|
# is not pressure); retire one worker per tick, the one idle longest,
|
|
14
14
|
# once it has been idle for `scale_down_after`. "Idle" means no slot of
|
|
15
|
-
# the worker held a request
|
|
16
|
-
# count did not move between them
|
|
17
|
-
#
|
|
15
|
+
# the worker held a request, an open socket or an open hijacked connection
|
|
16
|
+
# at two consecutive samples, and its served count did not move between them.
|
|
17
|
+
# Each tick also warns about worker_loops running on past their drain.
|
|
18
18
|
class PoolScaler < Monitor
|
|
19
19
|
TICK = 0.1
|
|
20
20
|
# Consecutive ticks with a non-empty queue before the pool grows.
|
|
@@ -33,9 +33,8 @@ module Kino
|
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
# One policy step over one set of observations: the monotonic time,
|
|
36
|
-
# the queue depth, and
|
|
37
|
-
#
|
|
38
|
-
# without a server.
|
|
36
|
+
# the queue depth, and a WorkerRow per slot. Public so the policy is
|
|
37
|
+
# testable without a server.
|
|
39
38
|
def step(now, queued, rows)
|
|
40
39
|
@pressure = queued.positive? ? @pressure + 1 : 0
|
|
41
40
|
if @pressure >= PRESSURE_TICKS
|
|
@@ -56,7 +55,8 @@ module Kino
|
|
|
56
55
|
|
|
57
56
|
def scan
|
|
58
57
|
queued, _in_flight = Native.queue_stats(@server_id)
|
|
59
|
-
step(
|
|
58
|
+
step(Kino.monotonic, queued, WorkerRow.all(@server_id))
|
|
59
|
+
Worker::Loop.warn_lingering(@server_id)
|
|
60
60
|
end
|
|
61
61
|
|
|
62
62
|
def grow
|
|
@@ -75,16 +75,15 @@ module Kino
|
|
|
75
75
|
Log.info("pool shrank to #{@pool.active_count} workers (worker-#{index} idle)")
|
|
76
76
|
end
|
|
77
77
|
|
|
78
|
-
# Per worker: idle when no slot
|
|
79
|
-
# served total did not move since the previous one
|
|
80
|
-
# a worker is never idle (it takes two samples to know).
|
|
78
|
+
# Per worker: idle when no slot is occupied (WorkerRow#occupied?) at this sample and the
|
|
79
|
+
# served total did not move since the previous one; a first sighting is never idle.
|
|
81
80
|
def observe_idle(now, rows)
|
|
82
|
-
by_slot = rows.to_h { |row| [row
|
|
81
|
+
by_slot = rows.to_h { |row| [row.slot, row] }
|
|
83
82
|
groups = @pool.groups
|
|
84
83
|
(@idle_since.keys - groups.keys).each { |index| forget(index) }
|
|
85
84
|
groups.each do |index, slot_ids|
|
|
86
|
-
served = slot_ids.sum { |id| by_slot
|
|
87
|
-
busy = slot_ids.any? { |id|
|
|
85
|
+
served = slot_ids.sum { |id| by_slot[id]&.served || 0 }
|
|
86
|
+
busy = slot_ids.any? { |id| by_slot[id]&.occupied? }
|
|
88
87
|
idle = !busy && @last_served[index] == served
|
|
89
88
|
@last_served[index] = served
|
|
90
89
|
if idle
|
|
@@ -19,8 +19,8 @@ module Kino
|
|
|
19
19
|
private
|
|
20
20
|
|
|
21
21
|
def scan
|
|
22
|
-
|
|
23
|
-
next if quarantined || busy_ms <= @timeout_ms
|
|
22
|
+
WorkerRow.all(@server_id).each do |row|
|
|
23
|
+
next if row.quarantined || row.busy_ms <= @timeout_ms
|
|
24
24
|
|
|
25
25
|
if @outstanding >= @max
|
|
26
26
|
unless @at_cap_logged
|
|
@@ -30,7 +30,7 @@ module Kino
|
|
|
30
30
|
next
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
-
if @replacer.replace(
|
|
33
|
+
if @replacer.replace(row.slot, row.generation)
|
|
34
34
|
Native.record_quarantine_replacement(@server_id)
|
|
35
35
|
@outstanding += 1
|
|
36
36
|
@at_cap_logged = false
|