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
data/ext/kino/src/registry.rs
CHANGED
|
@@ -4,36 +4,31 @@
|
|
|
4
4
|
//! keeps TypedData objects from ever crossing a ractor boundary.
|
|
5
5
|
|
|
6
6
|
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
|
|
7
|
-
use std::sync::{Arc, OnceLock
|
|
7
|
+
use std::sync::{Arc, OnceLock};
|
|
8
8
|
use std::time::{Duration, Instant};
|
|
9
9
|
|
|
10
10
|
use parking_lot::{Mutex, RwLock};
|
|
11
11
|
|
|
12
|
+
use crate::doorbell::Doorbell;
|
|
12
13
|
use crate::request::RequestCtx;
|
|
13
|
-
use crate::
|
|
14
|
+
use crate::server::ServerConfig;
|
|
15
|
+
use crate::slot::{WorkerSlot, WS_INBOX};
|
|
14
16
|
|
|
15
17
|
/// Lifecycle as seen by the control plane's /ready.
|
|
16
18
|
pub const STATE_BOOTING: u8 = 0;
|
|
17
19
|
pub const STATE_READY: u8 = 1;
|
|
18
20
|
pub const STATE_DRAINING: u8 = 2;
|
|
19
21
|
|
|
20
|
-
/// Boot-time configuration echoed by /stats. Stored resolved: mode is
|
|
21
|
-
/// "ractor" or "threaded", never "auto".
|
|
22
|
-
pub struct Topology {
|
|
23
|
-
pub mode: String,
|
|
24
|
-
/// The pool floor: the configured worker count.
|
|
25
|
-
pub workers: usize,
|
|
26
|
-
/// The pool ceiling: equal to `workers` for a fixed pool. HTTP/2
|
|
27
|
-
/// admission (SETTINGS_MAX_CONCURRENT_STREAMS) is advertised from it.
|
|
28
|
-
pub max_workers: usize,
|
|
29
|
-
pub threads: usize,
|
|
30
|
-
pub batch: usize,
|
|
31
|
-
}
|
|
32
|
-
|
|
33
22
|
/// Requests travel through channels boxed: one heap allocation at accept
|
|
34
23
|
/// time instead of moving ~300 bytes by value through every channel hop.
|
|
35
24
|
pub type BoxedCtx = Box<RequestCtx>;
|
|
36
25
|
|
|
26
|
+
/// A request, or a doorbell: the worker's WebSocket inbox has events (WorkerSlot::notify).
|
|
27
|
+
pub enum LaneItem {
|
|
28
|
+
Request(BoxedCtx),
|
|
29
|
+
Bell,
|
|
30
|
+
}
|
|
31
|
+
|
|
37
32
|
/// Probed on every take; keys are our own ids, so ahash over SipHash.
|
|
38
33
|
type HashMap<K, V> = std::collections::HashMap<K, V, ahash::RandomState>;
|
|
39
34
|
|
|
@@ -84,8 +79,13 @@ impl RuntimeHandle {
|
|
|
84
79
|
|
|
85
80
|
/// One per `Kino::Server`. Owns the tokio runtime, the request queue and the
|
|
86
81
|
/// worker slots.
|
|
82
|
+
/// Its last Arc may go in a thread-safe free, so what it holds keeps typed_data.rs's promise. The
|
|
83
|
+
/// runtime, whose drop does not, is out by then: the registry holds the server until
|
|
84
|
+
/// shutdown_runtime took it.
|
|
87
85
|
pub struct ServerInner {
|
|
88
86
|
pub id: u64,
|
|
87
|
+
pub config: ServerConfig,
|
|
88
|
+
pub counters: Counters,
|
|
89
89
|
/// Senders' side of the request queue. `close_queue` takes it; once all
|
|
90
90
|
/// clones drop, blocked workers see Disconnected and exit their loops.
|
|
91
91
|
pub req_tx: Mutex<Option<flume::Sender<BoxedCtx>>>,
|
|
@@ -96,90 +96,51 @@ pub struct ServerInner {
|
|
|
96
96
|
/// takes ownership out of the Arc.
|
|
97
97
|
pub runtime: Mutex<RuntimeHandle>,
|
|
98
98
|
pub slots: RwLock<Vec<Arc<WorkerSlot>>>,
|
|
99
|
-
pub in_flight: AtomicUsize,
|
|
100
|
-
/// Requests handed to Ruby workers (admitted), and requests rejected
|
|
101
|
-
/// with a 503 (queue full / draining). Relaxed: stats-only counters.
|
|
102
|
-
pub served: AtomicU64,
|
|
103
|
-
pub rejected: AtomicU64,
|
|
104
|
-
pub queue_timeout_ms: u64,
|
|
105
|
-
/// 0 = no request timeout; otherwise the response head must arrive
|
|
106
|
-
/// within this many ms or the client gets a 504.
|
|
107
|
-
pub request_timeout_ms: u64,
|
|
108
|
-
/// 0 = unlimited; otherwise the max request-body bytes accepted before a
|
|
109
|
-
/// 413 (truthful Content-Length) or a mid-stream abort (chunked/lying).
|
|
110
|
-
pub max_body_size: usize,
|
|
111
|
-
pub timeouts: AtomicU64,
|
|
112
99
|
/// Lifecycle for /ready: booting until Ruby reports the workers up,
|
|
113
100
|
/// draining once stop_accepting runs. Relaxed everywhere (advisory).
|
|
114
101
|
pub state: std::sync::atomic::AtomicU8,
|
|
115
|
-
/// Worker respawns, recorded from the Ruby supervisor. Lives here so
|
|
116
|
-
/// the control plane reads it without touching Ruby.
|
|
117
|
-
pub respawns: AtomicU64,
|
|
118
|
-
/// Replacements spawned by the quarantine monitor (Relaxed, advisory).
|
|
119
|
-
pub quarantine_replacements: AtomicU64,
|
|
120
|
-
/// Workers staying in the pool (a worker told to retire no longer
|
|
121
|
-
/// counts), reported by the Ruby pool as it grows and shrinks; starts
|
|
122
|
-
/// at the floor. Relaxed, advisory.
|
|
123
|
-
pub active_workers: AtomicUsize,
|
|
124
|
-
/// Pool scaler events (Relaxed, advisory).
|
|
125
|
-
pub scale_ups: AtomicU64,
|
|
126
|
-
pub scale_downs: AtomicU64,
|
|
127
|
-
pub topology: Topology,
|
|
128
|
-
pub https: bool,
|
|
129
|
-
/// HTTP/2 serving (ALPN over TLS, prior-knowledge h2c on plaintext);
|
|
130
|
-
/// false pins every connection to HTTP/1.
|
|
131
|
-
pub http2: bool,
|
|
132
|
-
/// The socket file of a `unix://` bind, removed at shutdown.
|
|
133
|
-
pub unix_path: Option<std::path::PathBuf>,
|
|
134
102
|
/// Native access log sink (None unless log_requests is on).
|
|
135
103
|
pub access_log: Option<crate::logsink::Sink>,
|
|
136
|
-
/// Lane-dispatch mode: per-worker queues, awake-preferring dispatch.
|
|
137
|
-
pub lanes: bool,
|
|
138
104
|
/// Round-robin cursor for lane dispatch.
|
|
139
105
|
pub lane_cursor: AtomicUsize,
|
|
106
|
+
/// Lane dispatch: stamps each park, so the dispatcher can tell which
|
|
107
|
+
/// idle worker parked last (see Park::parked_at).
|
|
108
|
+
pub park_seq: AtomicU64,
|
|
109
|
+
pub lane_room: crate::lane_room::LaneRoom,
|
|
110
|
+
pub ticker: Mutex<Option<crate::ticker::Ticker>>,
|
|
140
111
|
/// GC roots for zero-copy response buffers (pin.rs). The Ruby Server
|
|
141
112
|
/// object holds the marking PinKeeper for this slab.
|
|
142
113
|
pub pin_slab: Arc<crate::pin::PinSlab>,
|
|
143
|
-
///
|
|
144
|
-
|
|
145
|
-
|
|
114
|
+
/// Warnings printed once per server (Worker#claim_warning).
|
|
115
|
+
pub warned: Mutex<std::collections::HashSet<String>>,
|
|
116
|
+
/// How far the drain has closed hijacked connections (hijack::Closed).
|
|
117
|
+
pub bridges_closed: tokio::sync::watch::Sender<crate::hijack::Closed>,
|
|
118
|
+
/// Counted until hyper has taken each bridge's answer; close_bridges waits for them.
|
|
119
|
+
pub bridges_running: Arc<AtomicUsize>,
|
|
120
|
+
/// Where a crashed worker's requests wait for lane room; set once the I/O runtime is up.
|
|
121
|
+
pub io_runtime: std::sync::OnceLock<tokio::runtime::Handle>,
|
|
122
|
+
/// Inline serving (inline/); None when off (ServerConfig::inline).
|
|
123
|
+
pub inline: Option<Arc<crate::inline::InlineServer>>,
|
|
146
124
|
}
|
|
147
125
|
|
|
148
|
-
///
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
/// churn, so no select-style blocking anywhere). The in-flight list lets
|
|
152
|
-
/// the supervisor 500 every request a dead ractor was holding; workers
|
|
153
|
-
/// take requests in small batches, so there can be several.
|
|
154
|
-
pub struct WorkerSlot {
|
|
155
|
-
pub interrupted: std::sync::atomic::AtomicBool,
|
|
156
|
-
pub current: Mutex<smallvec::SmallVec<[Weak<Responder>; 8]>>,
|
|
157
|
-
/// Lane mode only: this worker's private queue and its parked flag.
|
|
158
|
-
/// The dispatcher prefers awake (non-parked) lanes so a hot worker
|
|
159
|
-
/// keeps taking without ever paying the futex wake.
|
|
160
|
-
pub lane_tx: Mutex<Option<flume::Sender<BoxedCtx>>>,
|
|
161
|
-
pub lane_rx: Option<flume::Receiver<BoxedCtx>>,
|
|
162
|
-
pub parked: std::sync::atomic::AtomicBool,
|
|
163
|
-
/// Per-slot sensors (Relaxed, advisory). served/in_flight mirror the
|
|
164
|
-
/// global counters at slot granularity; last_started_ms (stamped on
|
|
165
|
-
/// admit) drives busy-age (wedge) reporting.
|
|
166
|
-
pub served: AtomicU64,
|
|
126
|
+
/// Relaxed and advisory: the control plane reads them without Ruby.
|
|
127
|
+
#[derive(Default)]
|
|
128
|
+
pub struct Counters {
|
|
167
129
|
pub in_flight: AtomicUsize,
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
pub
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
///
|
|
175
|
-
|
|
176
|
-
pub
|
|
130
|
+
/// Requests handed to workers, and 503s (queue full, or draining).
|
|
131
|
+
pub served: AtomicU64,
|
|
132
|
+
pub rejected: AtomicU64,
|
|
133
|
+
pub timeouts: AtomicU64,
|
|
134
|
+
pub respawns: AtomicU64,
|
|
135
|
+
pub quarantine_replacements: AtomicU64,
|
|
136
|
+
/// Workers staying in the pool (not retiring), as the Ruby pool reports; starts at the floor.
|
|
137
|
+
pub active_workers: AtomicUsize,
|
|
138
|
+
pub scale_ups: AtomicU64,
|
|
139
|
+
pub scale_downs: AtomicU64,
|
|
140
|
+
pub wakes: AtomicU64,
|
|
141
|
+
pub queue_histogram: QueueHistogram,
|
|
177
142
|
}
|
|
178
143
|
|
|
179
|
-
/// Per-lane depth cap: small, so a slow handler can only ever delay this
|
|
180
|
-
/// many queued neighbors (work stealing rescues them anyway).
|
|
181
|
-
pub const LANE_DEPTH: usize = 4;
|
|
182
|
-
|
|
183
144
|
/// Fixed queue-wait bucket boundaries in microseconds (0.5ms .. 10s),
|
|
184
145
|
/// ascending. Emitted in seconds. Not a knob (YAGNI).
|
|
185
146
|
pub const QUEUE_BOUNDS_US: [u64; 14] = [
|
|
@@ -190,6 +151,7 @@ pub const QUEUE_BOUNDS_US: [u64; 14] = [
|
|
|
190
151
|
/// Queue-wait histogram: per-bucket counts plus an overflow (the implicit
|
|
191
152
|
/// +Inf bucket), the sum of waits, and the total count. Relaxed atomics,
|
|
192
153
|
/// advisory like the other counters.
|
|
154
|
+
#[derive(Default)]
|
|
193
155
|
pub struct QueueHistogram {
|
|
194
156
|
pub buckets: [AtomicU64; QUEUE_BOUNDS_US.len()],
|
|
195
157
|
pub overflow: AtomicU64,
|
|
@@ -198,6 +160,7 @@ pub struct QueueHistogram {
|
|
|
198
160
|
}
|
|
199
161
|
|
|
200
162
|
/// A plain (non-atomic) snapshot for the control thread to emit.
|
|
163
|
+
#[cfg_attr(test, derive(Default))]
|
|
201
164
|
pub struct QueueHistogramSnapshot {
|
|
202
165
|
pub buckets: [u64; QUEUE_BOUNDS_US.len()],
|
|
203
166
|
pub overflow: u64,
|
|
@@ -206,15 +169,6 @@ pub struct QueueHistogramSnapshot {
|
|
|
206
169
|
}
|
|
207
170
|
|
|
208
171
|
impl QueueHistogram {
|
|
209
|
-
pub fn new() -> Self {
|
|
210
|
-
QueueHistogram {
|
|
211
|
-
buckets: std::array::from_fn(|_| AtomicU64::new(0)),
|
|
212
|
-
overflow: AtomicU64::new(0),
|
|
213
|
-
sum_us: AtomicU64::new(0),
|
|
214
|
-
count: AtomicU64::new(0),
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
172
|
/// Place one wait in its bucket (first bound >= wait, else overflow) and
|
|
219
173
|
/// update sum and count. A linear scan over 14 bounds is trivial.
|
|
220
174
|
pub fn record(&self, wait_us: u64) {
|
|
@@ -236,12 +190,6 @@ impl QueueHistogram {
|
|
|
236
190
|
}
|
|
237
191
|
}
|
|
238
192
|
|
|
239
|
-
impl Default for QueueHistogram {
|
|
240
|
-
fn default() -> Self {
|
|
241
|
-
Self::new()
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
193
|
impl QueueHistogramSnapshot {
|
|
246
194
|
/// Summed queue wait, converted from the stored microseconds to seconds.
|
|
247
195
|
pub fn sum_seconds(&self) -> f64 {
|
|
@@ -249,55 +197,6 @@ impl QueueHistogramSnapshot {
|
|
|
249
197
|
}
|
|
250
198
|
}
|
|
251
199
|
|
|
252
|
-
impl WorkerSlot {
|
|
253
|
-
fn new(lanes: bool) -> Self {
|
|
254
|
-
let (lane_tx, lane_rx) = if lanes {
|
|
255
|
-
let (tx, rx) = flume::bounded(LANE_DEPTH);
|
|
256
|
-
(Some(tx), Some(rx))
|
|
257
|
-
} else {
|
|
258
|
-
(None, None)
|
|
259
|
-
};
|
|
260
|
-
WorkerSlot {
|
|
261
|
-
interrupted: std::sync::atomic::AtomicBool::new(false),
|
|
262
|
-
current: Mutex::new(smallvec::SmallVec::new()),
|
|
263
|
-
lane_tx: Mutex::new(lane_tx),
|
|
264
|
-
lane_rx,
|
|
265
|
-
parked: std::sync::atomic::AtomicBool::new(false),
|
|
266
|
-
served: AtomicU64::new(0),
|
|
267
|
-
in_flight: AtomicUsize::new(0),
|
|
268
|
-
last_started_ms: AtomicU64::new(0),
|
|
269
|
-
quarantined: std::sync::atomic::AtomicBool::new(false),
|
|
270
|
-
retired: std::sync::atomic::AtomicBool::new(false),
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/// Send this slot's worker home once it is idle. The flag is raised
|
|
275
|
-
/// under the lane lock, the same lock the lane dispatcher holds while
|
|
276
|
-
/// it checks the flag and sends: any dispatch that saw "not retired"
|
|
277
|
-
/// has landed in the lane before the worker can see the flag and
|
|
278
|
-
/// drain, so no request is orphaned.
|
|
279
|
-
pub fn retire(&self) {
|
|
280
|
-
let _lane = self.lane_tx.lock();
|
|
281
|
-
self.retired.store(true, Ordering::SeqCst);
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
/// Return a retired slot to its fresh state so a new worker can take
|
|
285
|
-
/// it over (slots are never removed; recycling keeps the registry
|
|
286
|
-
/// from growing with every scale-up). The lane channel stays, the new
|
|
287
|
-
/// occupant simply starts taking from it; the quarantine mark stays
|
|
288
|
-
/// too, a quarantined slot is never handed out.
|
|
289
|
-
pub fn reset(&self) {
|
|
290
|
-
let _lane = self.lane_tx.lock();
|
|
291
|
-
self.current.lock().clear();
|
|
292
|
-
self.retired.store(false, Ordering::SeqCst);
|
|
293
|
-
self.parked.store(false, Ordering::SeqCst);
|
|
294
|
-
self.interrupted.store(false, Ordering::SeqCst);
|
|
295
|
-
self.served.store(0, Ordering::Relaxed);
|
|
296
|
-
self.in_flight.store(0, Ordering::Relaxed);
|
|
297
|
-
self.last_started_ms.store(0, Ordering::Relaxed);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
200
|
static REGISTRY: OnceLock<RwLock<HashMap<u64, Arc<ServerInner>>>> = OnceLock::new();
|
|
302
201
|
static NEXT_SERVER_ID: AtomicU64 = AtomicU64::new(1);
|
|
303
202
|
|
|
@@ -330,29 +229,108 @@ pub fn try_get(id: u64) -> Option<Arc<ServerInner>> {
|
|
|
330
229
|
}
|
|
331
230
|
|
|
332
231
|
impl ServerInner {
|
|
333
|
-
///
|
|
232
|
+
/// A booting server, not registered yet, with no runtime and no slots.
|
|
233
|
+
pub fn new(config: ServerConfig) -> Self {
|
|
234
|
+
let (req_tx, req_rx) = flume::bounded(config.queue_depth);
|
|
235
|
+
// The accept loop subscribes once there is one.
|
|
236
|
+
let (shutdown_tx, _) = tokio::sync::watch::channel(false);
|
|
237
|
+
ServerInner {
|
|
238
|
+
id: next_server_id(),
|
|
239
|
+
counters: Counters {
|
|
240
|
+
active_workers: AtomicUsize::new(config.topology.workers),
|
|
241
|
+
..Counters::default()
|
|
242
|
+
},
|
|
243
|
+
req_tx: Mutex::new(Some(req_tx)),
|
|
244
|
+
req_rx,
|
|
245
|
+
shutdown_tx,
|
|
246
|
+
runtime: Mutex::default(),
|
|
247
|
+
slots: RwLock::default(),
|
|
248
|
+
state: std::sync::atomic::AtomicU8::new(STATE_BOOTING),
|
|
249
|
+
access_log: config
|
|
250
|
+
.log_requests
|
|
251
|
+
.then(|| crate::logsink::Sink::new(std::io::stdout())),
|
|
252
|
+
lane_cursor: AtomicUsize::new(0),
|
|
253
|
+
park_seq: AtomicU64::new(0),
|
|
254
|
+
lane_room: Default::default(),
|
|
255
|
+
ticker: Mutex::new(None),
|
|
256
|
+
pin_slab: Arc::new(crate::pin::PinSlab::for_responses(
|
|
257
|
+
config.topology.mode == "ractor",
|
|
258
|
+
)),
|
|
259
|
+
warned: Mutex::default(),
|
|
260
|
+
bridges_closed: tokio::sync::watch::Sender::new(crate::hijack::Closed::Nothing),
|
|
261
|
+
bridges_running: Arc::default(),
|
|
262
|
+
io_runtime: std::sync::OnceLock::new(),
|
|
263
|
+
inline: config.inline.then(|| {
|
|
264
|
+
let mut inline = crate::inline::InlineServer::default();
|
|
265
|
+
*inline.spin_on.get_mut() = config.inline_spin;
|
|
266
|
+
Arc::new(inline)
|
|
267
|
+
}),
|
|
268
|
+
config,
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/// Requests in each lane (None without lanes).
|
|
334
273
|
pub fn lane_depths(&self) -> Option<Vec<usize>> {
|
|
335
|
-
if !self.lanes {
|
|
274
|
+
if !self.config.lanes {
|
|
336
275
|
return None;
|
|
337
276
|
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
.read()
|
|
341
|
-
.iter()
|
|
342
|
-
.filter_map(|s| s.lane_rx.as_ref().map(|rx| rx.len()))
|
|
343
|
-
.collect(),
|
|
344
|
-
)
|
|
277
|
+
let slots = self.slots.read();
|
|
278
|
+
Some(slots.iter().filter_map(|s| s.lane.requests()).collect())
|
|
345
279
|
}
|
|
346
280
|
|
|
347
|
-
/// Requests waiting anywhere: the global queue
|
|
281
|
+
/// Requests waiting anywhere: the global queue, any open lanes, the line for lane room.
|
|
348
282
|
pub fn queued(&self) -> usize {
|
|
349
|
-
|
|
283
|
+
let lanes = self.lane_depths().map_or(0, |d| d.iter().sum::<usize>());
|
|
284
|
+
self.req_rx.len() + lanes + self.lane_room.waiting()
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
pub fn is_draining(&self) -> bool {
|
|
288
|
+
self.state.load(Ordering::Relaxed) == STATE_DRAINING
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/// The drain begins: /ready reports it and the accept loop stops. Idle inline connections
|
|
292
|
+
/// close (inline::drain), and none spins any more.
|
|
293
|
+
pub fn stop_accepting(&self) {
|
|
294
|
+
self.state.store(STATE_DRAINING, Ordering::Relaxed);
|
|
295
|
+
let _ = self.shutdown_tx.send(true);
|
|
296
|
+
if let Some(inline) = &self.inline {
|
|
297
|
+
inline.spin_on.store(false, Ordering::Relaxed);
|
|
298
|
+
crate::inline::drain(self);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/// The drain is over: every queue closes, and requests waiting for lane room find it so.
|
|
303
|
+
/// Unplaced connections go to hyper, draining, which ends them.
|
|
304
|
+
pub fn close_queues(&self) {
|
|
305
|
+
self.req_tx.lock().take();
|
|
306
|
+
for slot in self.slots.read().iter() {
|
|
307
|
+
slot.close_lane();
|
|
308
|
+
crate::inline::unplace(slot);
|
|
309
|
+
}
|
|
310
|
+
self.lane_room.notify();
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/// WebSockets whose on_close some worker of this server is still owed.
|
|
314
|
+
pub fn open_sockets(&self) -> usize {
|
|
315
|
+
self.slots
|
|
316
|
+
.read()
|
|
317
|
+
.iter()
|
|
318
|
+
.map(|slot| slot.sockets.open())
|
|
319
|
+
.sum()
|
|
350
320
|
}
|
|
351
321
|
|
|
352
|
-
|
|
322
|
+
/// One registered after close_queue (a late respawn) gets a closed lane, so its worker leaves.
|
|
323
|
+
/// Fails only for a worker_loop slot whose doorbell cannot be made (out of descriptors).
|
|
324
|
+
pub fn register_worker(&self) -> std::io::Result<usize> {
|
|
325
|
+
let doorbell = self.config.worker_loop.then(Doorbell::new).transpose()?;
|
|
353
326
|
let mut slots = self.slots.write();
|
|
354
|
-
|
|
355
|
-
|
|
327
|
+
let inline = self.inline.is_some();
|
|
328
|
+
let slot = WorkerSlot::new(self.config.lanes, WS_INBOX, doorbell, inline);
|
|
329
|
+
if self.req_tx.lock().is_none() {
|
|
330
|
+
slot.close_lane();
|
|
331
|
+
}
|
|
332
|
+
slots.push(Arc::new(slot));
|
|
333
|
+
Ok(slots.len() - 1)
|
|
356
334
|
}
|
|
357
335
|
|
|
358
336
|
pub fn slot(
|
|
@@ -369,55 +347,11 @@ impl ServerInner {
|
|
|
369
347
|
}
|
|
370
348
|
}
|
|
371
349
|
|
|
372
|
-
/// A ServerInner with no runtime and no registry entry, for pure-Rust
|
|
373
|
-
/// tests of queue accounting and dispatch. Ids are unique so tests that
|
|
374
|
-
/// do insert into the global registry can run in parallel.
|
|
375
|
-
#[cfg(test)]
|
|
376
|
-
pub fn test_server(lanes: bool, queue_depth: usize) -> Arc<ServerInner> {
|
|
377
|
-
let (req_tx, req_rx) = flume::bounded(queue_depth);
|
|
378
|
-
let (shutdown_tx, _shutdown_rx) = tokio::sync::watch::channel(false);
|
|
379
|
-
Arc::new(ServerInner {
|
|
380
|
-
id: next_server_id(),
|
|
381
|
-
req_tx: Mutex::new(Some(req_tx)),
|
|
382
|
-
req_rx,
|
|
383
|
-
shutdown_tx,
|
|
384
|
-
runtime: Mutex::new(RuntimeHandle::None),
|
|
385
|
-
slots: RwLock::new(Vec::new()),
|
|
386
|
-
in_flight: AtomicUsize::new(0),
|
|
387
|
-
served: AtomicU64::new(0),
|
|
388
|
-
rejected: AtomicU64::new(0),
|
|
389
|
-
queue_timeout_ms: 10,
|
|
390
|
-
request_timeout_ms: 0,
|
|
391
|
-
max_body_size: 0,
|
|
392
|
-
timeouts: AtomicU64::new(0),
|
|
393
|
-
state: std::sync::atomic::AtomicU8::new(STATE_BOOTING),
|
|
394
|
-
respawns: AtomicU64::new(0),
|
|
395
|
-
quarantine_replacements: AtomicU64::new(0),
|
|
396
|
-
active_workers: AtomicUsize::new(0),
|
|
397
|
-
scale_ups: AtomicU64::new(0),
|
|
398
|
-
scale_downs: AtomicU64::new(0),
|
|
399
|
-
topology: Topology {
|
|
400
|
-
mode: "threaded".to_string(),
|
|
401
|
-
workers: 0,
|
|
402
|
-
max_workers: 0,
|
|
403
|
-
threads: 0,
|
|
404
|
-
batch: 1,
|
|
405
|
-
},
|
|
406
|
-
https: false,
|
|
407
|
-
http2: true,
|
|
408
|
-
unix_path: None,
|
|
409
|
-
access_log: None,
|
|
410
|
-
lanes,
|
|
411
|
-
lane_cursor: AtomicUsize::new(0),
|
|
412
|
-
pin_slab: Arc::new(crate::pin::PinSlab::for_responses(false)),
|
|
413
|
-
queue_histogram: QueueHistogram::new(),
|
|
414
|
-
})
|
|
415
|
-
}
|
|
416
|
-
|
|
417
350
|
#[cfg(test)]
|
|
418
351
|
mod tests {
|
|
419
352
|
use super::*;
|
|
420
353
|
use crate::request::test_ctx;
|
|
354
|
+
use crate::testing::{assign, slot, TestServer};
|
|
421
355
|
|
|
422
356
|
#[test]
|
|
423
357
|
fn shards_shutdown_joins_threads_that_observe_the_signal() {
|
|
@@ -458,58 +392,41 @@ mod tests {
|
|
|
458
392
|
}
|
|
459
393
|
|
|
460
394
|
#[test]
|
|
461
|
-
fn
|
|
462
|
-
let server =
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
assert_eq!(server.register_worker(), 1);
|
|
466
|
-
assert_eq!(server.slots.read().len(), 2);
|
|
467
|
-
// Shared-queue mode creates no lane channels.
|
|
468
|
-
assert!(server.slots.read()[0].lane_rx.is_none());
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
#[test]
|
|
472
|
-
fn lane_mode_slots_get_bounded_lanes() {
|
|
473
|
-
let server = test_server(true, 4);
|
|
474
|
-
server.register_worker();
|
|
475
|
-
|
|
476
|
-
let slots = server.slots.read();
|
|
477
|
-
let lane_rx = slots[0].lane_rx.as_ref().expect("lane created");
|
|
478
|
-
assert_eq!(lane_rx.capacity(), Some(LANE_DEPTH));
|
|
479
|
-
assert!(slots[0].lane_tx.lock().is_some());
|
|
480
|
-
}
|
|
395
|
+
fn a_slot_registered_once_the_queue_closed_has_its_lane_closed_too() {
|
|
396
|
+
let server = TestServer::lanes().workers(1).build();
|
|
397
|
+
server.req_tx.lock().take(); // close_queue
|
|
398
|
+
slot(&server, 0).lane.tx.lock().take();
|
|
481
399
|
|
|
482
|
-
|
|
483
|
-
fn queued_counts_the_shared_queue() {
|
|
484
|
-
let server = test_server(false, 4);
|
|
485
|
-
assert_eq!(server.queued(), 0);
|
|
400
|
+
server.register_worker().expect("a worker slot");
|
|
486
401
|
|
|
487
|
-
let
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
assert_eq!(server.queued(), 2);
|
|
492
|
-
assert!(server.lane_depths().is_none());
|
|
402
|
+
let lane = slot(&server, 1).lane.rx.clone().expect("lane");
|
|
403
|
+
let why = "a worker that comes up during shutdown must see its lane closed and leave";
|
|
404
|
+
assert!(lane.is_disconnected(), "{why}");
|
|
493
405
|
}
|
|
494
406
|
|
|
495
407
|
#[test]
|
|
496
|
-
fn
|
|
497
|
-
let
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
assert_eq!(
|
|
408
|
+
fn queued_counts_the_requests_in_the_shared_queue_or_the_lanes_not_their_doorbells() {
|
|
409
|
+
let shared = TestServer::shared_queue().build();
|
|
410
|
+
let tx = shared.req_tx.lock().clone().expect("queue open");
|
|
411
|
+
for _ in 0..2 {
|
|
412
|
+
tx.send(test_ctx()).expect("queue has room");
|
|
413
|
+
}
|
|
414
|
+
assert_eq!((shared.queued(), shared.lane_depths()), (2, None));
|
|
415
|
+
|
|
416
|
+
let lanes = TestServer::lanes().workers(2).build();
|
|
417
|
+
assign(&slot(&lanes, 0));
|
|
418
|
+
assert!(slot(&lanes, 1).wake(), "a doorbell in the other lane");
|
|
419
|
+
assert_eq!(lanes.lane_depths(), Some(vec![1, 0]));
|
|
420
|
+
assert_eq!(
|
|
421
|
+
lanes.queued(),
|
|
422
|
+
1,
|
|
423
|
+
"the pool scaler must not see a worker's own events as queue pressure"
|
|
424
|
+
);
|
|
508
425
|
}
|
|
509
426
|
|
|
510
427
|
#[test]
|
|
511
428
|
fn registry_lifecycle_insert_lookup_remove() {
|
|
512
|
-
let server =
|
|
429
|
+
let server = TestServer::shared_queue().build();
|
|
513
430
|
let id = server.id;
|
|
514
431
|
|
|
515
432
|
assert!(try_get(id).is_none());
|
|
@@ -523,137 +440,9 @@ mod tests {
|
|
|
523
440
|
assert!(remove(id).is_none());
|
|
524
441
|
}
|
|
525
442
|
|
|
526
|
-
#[test]
|
|
527
|
-
fn server_ids_are_unique() {
|
|
528
|
-
let a = next_server_id();
|
|
529
|
-
let b = next_server_id();
|
|
530
|
-
assert_ne!(a, b);
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
#[test]
|
|
534
|
-
fn servers_boot_in_the_booting_state_with_zero_respawns() {
|
|
535
|
-
let server = test_server(false, 4);
|
|
536
|
-
assert_eq!(server.state.load(Ordering::Relaxed), STATE_BOOTING);
|
|
537
|
-
assert_eq!(server.respawns.load(Ordering::Relaxed), 0);
|
|
538
|
-
assert_eq!(server.topology.batch, 1);
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
#[test]
|
|
542
|
-
fn pool_counters_start_at_the_configured_floor() {
|
|
543
|
-
let server = test_server(false, 4);
|
|
544
|
-
assert_eq!(
|
|
545
|
-
server.active_workers.load(Ordering::Relaxed),
|
|
546
|
-
server.topology.workers
|
|
547
|
-
);
|
|
548
|
-
assert_eq!(server.topology.max_workers, server.topology.workers);
|
|
549
|
-
assert_eq!(server.scale_ups.load(Ordering::Relaxed), 0);
|
|
550
|
-
assert_eq!(server.scale_downs.load(Ordering::Relaxed), 0);
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
#[test]
|
|
554
|
-
fn fresh_slot_has_zeroed_per_worker_sensors() {
|
|
555
|
-
let server = test_server(false, 4);
|
|
556
|
-
server.register_worker();
|
|
557
|
-
let slots = server.slots.read();
|
|
558
|
-
let slot = &slots[0];
|
|
559
|
-
assert_eq!(slot.served.load(Ordering::Relaxed), 0);
|
|
560
|
-
assert_eq!(slot.in_flight.load(Ordering::Relaxed), 0);
|
|
561
|
-
assert_eq!(slot.last_started_ms.load(Ordering::Relaxed), 0);
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
#[test]
|
|
565
|
-
fn fresh_slot_is_not_quarantined() {
|
|
566
|
-
let server = test_server(false, 4);
|
|
567
|
-
server.register_worker();
|
|
568
|
-
assert!(!server.slots.read()[0].quarantined.load(Ordering::Relaxed));
|
|
569
|
-
assert_eq!(server.quarantine_replacements.load(Ordering::Relaxed), 0);
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
#[test]
|
|
573
|
-
fn fresh_slot_is_not_retired() {
|
|
574
|
-
let server = test_server(false, 4);
|
|
575
|
-
server.register_worker();
|
|
576
|
-
assert!(!server.slots.read()[0].retired.load(Ordering::Relaxed));
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
#[test]
|
|
580
|
-
fn retire_then_reset_returns_a_slot_to_its_fresh_state() {
|
|
581
|
-
let server = test_server(true, 4);
|
|
582
|
-
server.register_worker();
|
|
583
|
-
let slot = server.slots.read()[0].clone();
|
|
584
|
-
slot.parked.store(true, Ordering::Relaxed);
|
|
585
|
-
slot.interrupted.store(true, Ordering::Relaxed);
|
|
586
|
-
slot.served.store(5, Ordering::Relaxed);
|
|
587
|
-
slot.in_flight.store(1, Ordering::Relaxed);
|
|
588
|
-
slot.last_started_ms.store(9, Ordering::Relaxed);
|
|
589
|
-
slot.current.lock().push(std::sync::Weak::new());
|
|
590
|
-
|
|
591
|
-
slot.retire();
|
|
592
|
-
assert!(slot.retired.load(Ordering::Relaxed));
|
|
593
|
-
|
|
594
|
-
slot.reset();
|
|
595
|
-
assert!(!slot.retired.load(Ordering::Relaxed));
|
|
596
|
-
assert!(!slot.parked.load(Ordering::Relaxed));
|
|
597
|
-
assert!(!slot.interrupted.load(Ordering::Relaxed));
|
|
598
|
-
assert_eq!(slot.served.load(Ordering::Relaxed), 0);
|
|
599
|
-
assert_eq!(slot.in_flight.load(Ordering::Relaxed), 0);
|
|
600
|
-
assert_eq!(slot.last_started_ms.load(Ordering::Relaxed), 0);
|
|
601
|
-
assert!(slot.current.lock().is_empty());
|
|
602
|
-
// The lane survives reuse: the next occupant takes from it.
|
|
603
|
-
assert!(slot.lane_tx.lock().is_some());
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
#[test]
|
|
607
|
-
fn reset_keeps_a_quarantine_mark() {
|
|
608
|
-
let server = test_server(false, 4);
|
|
609
|
-
server.register_worker();
|
|
610
|
-
let slot = server.slots.read()[0].clone();
|
|
611
|
-
slot.quarantined.store(true, Ordering::Relaxed);
|
|
612
|
-
|
|
613
|
-
slot.retire();
|
|
614
|
-
slot.reset();
|
|
615
|
-
|
|
616
|
-
// A wedged slot stays flagged even if it ever came back around.
|
|
617
|
-
assert!(slot.quarantined.load(Ordering::Relaxed));
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
#[test]
|
|
621
|
-
fn retire_releases_the_lane_lock() {
|
|
622
|
-
let server = test_server(true, 4);
|
|
623
|
-
server.register_worker();
|
|
624
|
-
let slot = server.slots.read()[0].clone();
|
|
625
|
-
|
|
626
|
-
slot.retire();
|
|
627
|
-
|
|
628
|
-
assert!(slot.lane_tx.try_lock().is_some());
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
#[test]
|
|
632
|
-
fn retire_waits_for_a_dispatcher_holding_the_lane_lock() {
|
|
633
|
-
let server = test_server(true, 4);
|
|
634
|
-
server.register_worker();
|
|
635
|
-
let slot = server.slots.read()[0].clone();
|
|
636
|
-
|
|
637
|
-
// A dispatcher that has already read "not retired" and is sending.
|
|
638
|
-
let sending = slot.lane_tx.lock();
|
|
639
|
-
let retiring = std::thread::spawn({
|
|
640
|
-
let slot = slot.clone();
|
|
641
|
-
move || slot.retire()
|
|
642
|
-
});
|
|
643
|
-
std::thread::sleep(Duration::from_millis(30));
|
|
644
|
-
assert!(
|
|
645
|
-
!slot.retired.load(Ordering::SeqCst),
|
|
646
|
-
"the flag must not go up under a dispatcher's lock"
|
|
647
|
-
);
|
|
648
|
-
|
|
649
|
-
drop(sending);
|
|
650
|
-
retiring.join().expect("retire thread");
|
|
651
|
-
assert!(slot.retired.load(Ordering::SeqCst));
|
|
652
|
-
}
|
|
653
|
-
|
|
654
443
|
#[test]
|
|
655
444
|
fn queue_histogram_buckets_by_wait() {
|
|
656
|
-
let h = QueueHistogram::
|
|
445
|
+
let h = QueueHistogram::default();
|
|
657
446
|
h.record(400); // <= 500 -> bucket 0
|
|
658
447
|
h.record(500); // == 500 -> bucket 0 (inclusive)
|
|
659
448
|
h.record(600); // (500, 1000] -> bucket 1
|