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
|
@@ -0,0 +1,936 @@
|
|
|
1
|
+
//! A server's stats, taken once and rendered three ways: /stats JSON and
|
|
2
|
+
//! Server#stats through one `Emit` visitor, /metrics from a table of rows.
|
|
3
|
+
|
|
4
|
+
use std::fmt::{Display, Write};
|
|
5
|
+
use std::sync::atomic::Ordering;
|
|
6
|
+
|
|
7
|
+
use magnus::value::ReprValue;
|
|
8
|
+
use magnus::{Error, RArray, RHash, Ruby, Value};
|
|
9
|
+
|
|
10
|
+
use crate::counters::Tallied;
|
|
11
|
+
use crate::registry::{
|
|
12
|
+
QueueHistogramSnapshot, ServerInner, QUEUE_BOUNDS_US, STATE_DRAINING, STATE_READY,
|
|
13
|
+
};
|
|
14
|
+
use crate::server::Topology;
|
|
15
|
+
use crate::ws::{Cause, Refusal, WsCount, WsSnapshot};
|
|
16
|
+
|
|
17
|
+
/// One dispatch slot's sensors, captured for a response. busy_ms is the
|
|
18
|
+
/// age of the current in-flight work (0 when idle), the wedge signal.
|
|
19
|
+
/// A worker_loop worker's is how long its thread has been away from its loop with work waiting.
|
|
20
|
+
pub struct WorkerStat {
|
|
21
|
+
pub index: usize,
|
|
22
|
+
pub served: u64,
|
|
23
|
+
pub in_flight: usize,
|
|
24
|
+
pub busy_ms: u64,
|
|
25
|
+
pub quarantined: bool,
|
|
26
|
+
/// Sent home by the pool scaler; leaves at its next idle tick.
|
|
27
|
+
pub retired: bool,
|
|
28
|
+
/// WebSockets the slot's worker accepted and still owes an on_close.
|
|
29
|
+
pub sockets: usize,
|
|
30
|
+
/// Connections its requests handed over through rack.hijack, still open.
|
|
31
|
+
pub hijacked: usize,
|
|
32
|
+
pub inline_conns: usize,
|
|
33
|
+
/// Which worker the slot is on (WorkerSlot::generation).
|
|
34
|
+
pub generation: u64,
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// Read every slot's per-worker sensors in one pass under the slots read
|
|
38
|
+
/// lock (the same lock lane_depths uses), computing busy_ms against one
|
|
39
|
+
/// "now". No Ruby, so it is safe on the control thread and below the GVL.
|
|
40
|
+
pub fn collect_worker_status(server: &ServerInner) -> Vec<WorkerStat> {
|
|
41
|
+
let now = crate::mono::mono_us();
|
|
42
|
+
server
|
|
43
|
+
.slots
|
|
44
|
+
.read()
|
|
45
|
+
.iter()
|
|
46
|
+
.enumerate()
|
|
47
|
+
.map(|(index, slot)| {
|
|
48
|
+
let quarantined = slot.life.quarantined();
|
|
49
|
+
WorkerStat {
|
|
50
|
+
index,
|
|
51
|
+
served: slot.load.served(),
|
|
52
|
+
in_flight: slot.load.in_flight(),
|
|
53
|
+
// A quarantined slot is a known, handled wedge: report 0 so
|
|
54
|
+
// it never re-trips detection or reads as a live wedge.
|
|
55
|
+
busy_ms: match slot.busy_since() {
|
|
56
|
+
Some(started) if !quarantined => now.saturating_sub(started) / 1000,
|
|
57
|
+
_ => 0,
|
|
58
|
+
},
|
|
59
|
+
quarantined,
|
|
60
|
+
retired: slot.life.seems_retired(),
|
|
61
|
+
sockets: slot.sockets.open(),
|
|
62
|
+
hijacked: slot.hijacked.load(Ordering::Relaxed),
|
|
63
|
+
inline_conns: slot.inline.conns.load(Ordering::Relaxed),
|
|
64
|
+
generation: slot.generation(),
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
.collect()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/// Everything the endpoints report, captured in one pass so a response
|
|
71
|
+
/// is internally consistent.
|
|
72
|
+
#[cfg_attr(test, derive(Default))]
|
|
73
|
+
pub struct Snapshot {
|
|
74
|
+
pub topology: Topology,
|
|
75
|
+
pub respawns: u64,
|
|
76
|
+
pub queued: usize,
|
|
77
|
+
pub in_flight: usize,
|
|
78
|
+
pub served: u64,
|
|
79
|
+
pub rejected: u64,
|
|
80
|
+
pub timeouts: u64,
|
|
81
|
+
pub lane_depths: Option<Vec<usize>>,
|
|
82
|
+
pub state: u8,
|
|
83
|
+
pub worker_status: Vec<WorkerStat>,
|
|
84
|
+
pub quarantined_count: usize,
|
|
85
|
+
pub quarantine_replacements: u64,
|
|
86
|
+
pub active_workers: usize,
|
|
87
|
+
pub scale_ups: u64,
|
|
88
|
+
pub scale_downs: u64,
|
|
89
|
+
pub queue_histogram: QueueHistogramSnapshot,
|
|
90
|
+
pub wakes: u64,
|
|
91
|
+
/// The worker rows' hijacked connections together.
|
|
92
|
+
pub hijacked_connections: usize,
|
|
93
|
+
/// Process-wide: streamed responses cut, by cause.
|
|
94
|
+
pub responses_cut: [(&'static str, u64); crate::response::Cut::ALL.len()],
|
|
95
|
+
/// Process-wide, like the topics a publish reaches.
|
|
96
|
+
pub websockets: WsSnapshot,
|
|
97
|
+
/// None when the server serves nothing inline.
|
|
98
|
+
pub inline: Option<InlineSnapshot>,
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/// A server's inline serving: its connections now, and totals of the requests served, the
|
|
102
|
+
/// connections declined at accept, handed back to hyper or cut off, and the spins.
|
|
103
|
+
pub struct InlineSnapshot {
|
|
104
|
+
pub connections: usize,
|
|
105
|
+
pub requests: u64,
|
|
106
|
+
pub declined: u64,
|
|
107
|
+
pub handbacks: u64,
|
|
108
|
+
pub closed: u64,
|
|
109
|
+
pub spins: u64,
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
impl InlineSnapshot {
|
|
113
|
+
/// None when `server` serves nothing inline; its connections now are its worker rows'.
|
|
114
|
+
fn take(server: &ServerInner, workers: &[WorkerStat]) -> Option<Self> {
|
|
115
|
+
let inline = server.inline.as_ref()?;
|
|
116
|
+
let (mut requests, mut closed, mut spins) = (0, 0, 0);
|
|
117
|
+
for seats in server.slots.read().iter().map(|slot| &slot.inline) {
|
|
118
|
+
requests += seats.requests.load(Ordering::Relaxed);
|
|
119
|
+
closed += seats.closed.load(Ordering::Relaxed);
|
|
120
|
+
spins += seats.spins.load(Ordering::Relaxed);
|
|
121
|
+
}
|
|
122
|
+
Some(InlineSnapshot {
|
|
123
|
+
connections: workers.iter().map(|w| w.inline_conns).sum(),
|
|
124
|
+
requests,
|
|
125
|
+
declined: inline.declined(),
|
|
126
|
+
handbacks: inline.handbacks(),
|
|
127
|
+
closed,
|
|
128
|
+
spins,
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/// Its counts, in /stats's order: each one's key there, its metric's name and help, and its
|
|
133
|
+
/// value.
|
|
134
|
+
// A table, as Snapshot::counts's.
|
|
135
|
+
#[rustfmt::skip]
|
|
136
|
+
fn counts(&self) -> [(&'static str, Metric, u64); 6] {
|
|
137
|
+
[
|
|
138
|
+
("connections", ("kino_inline_connections",
|
|
139
|
+
"Connections served inline on the workers' threads now."),
|
|
140
|
+
self.connections as u64),
|
|
141
|
+
("requests", ("kino_inline_requests_total", "Requests served inline."), self.requests),
|
|
142
|
+
("declined", ("kino_inline_declined_total",
|
|
143
|
+
"Plain TCP and unix-socket connections left with hyper at accept, as no worker could take them (or none served yet)."),
|
|
144
|
+
self.declined),
|
|
145
|
+
("handbacks", ("kino_inline_handbacks_total", "Inline connections handed back to hyper."),
|
|
146
|
+
self.handbacks),
|
|
147
|
+
("closed", ("kino_inline_closed_total", "Inline connections Kino cut off."), self.closed),
|
|
148
|
+
("spins", ("kino_inline_spins_total",
|
|
149
|
+
"Spins of an inline connection alone on its worker for its next request, before its job waits on the socket."),
|
|
150
|
+
self.spins),
|
|
151
|
+
]
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/// One stat's value; a name is a fixed identifier, a Symbol in Ruby.
|
|
156
|
+
/// Nil is a section the server lacks: null in JSON, nil in Ruby.
|
|
157
|
+
enum Stat<'a> {
|
|
158
|
+
Count(u64),
|
|
159
|
+
Flag(bool),
|
|
160
|
+
Seconds(f64),
|
|
161
|
+
Name(&'a str),
|
|
162
|
+
Nil,
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/// What `nest` opens: a Map keeps its entries' keys, a List drops them.
|
|
166
|
+
enum Shape {
|
|
167
|
+
Map,
|
|
168
|
+
List,
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/// The key a List's item is given, which the List drops.
|
|
172
|
+
const ITEM: &str = "";
|
|
173
|
+
|
|
174
|
+
/// Where a Snapshot goes, serde-style: each stat under its key, Maps and
|
|
175
|
+
/// Lists through `nest`.
|
|
176
|
+
trait Emit {
|
|
177
|
+
fn stat(&mut self, key: &str, stat: Stat);
|
|
178
|
+
|
|
179
|
+
fn nest(&mut self, key: &str, shape: Shape, body: impl FnOnce(&mut Self));
|
|
180
|
+
|
|
181
|
+
fn counts<'a>(&mut self, counts: impl IntoIterator<Item = (&'a str, u64)>) {
|
|
182
|
+
for (key, count) in counts {
|
|
183
|
+
self.stat(key, Stat::Count(count));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
impl Snapshot {
|
|
189
|
+
pub fn take(server: &ServerInner) -> Self {
|
|
190
|
+
let worker_status = collect_worker_status(server);
|
|
191
|
+
let quarantined_count = worker_status.iter().filter(|w| w.quarantined).count();
|
|
192
|
+
let hijacked_connections = worker_status.iter().map(|w| w.hijacked).sum();
|
|
193
|
+
let inline = InlineSnapshot::take(server, &worker_status);
|
|
194
|
+
let c = &server.counters;
|
|
195
|
+
Snapshot {
|
|
196
|
+
topology: server.config.topology.clone(),
|
|
197
|
+
respawns: c.respawns.load(Ordering::Relaxed),
|
|
198
|
+
queued: server.queued(),
|
|
199
|
+
in_flight: c.in_flight.load(Ordering::Relaxed),
|
|
200
|
+
served: c.served.load(Ordering::Relaxed),
|
|
201
|
+
rejected: c.rejected.load(Ordering::Relaxed),
|
|
202
|
+
timeouts: c.timeouts.load(Ordering::Relaxed),
|
|
203
|
+
lane_depths: server.lane_depths(),
|
|
204
|
+
state: server.state.load(Ordering::Relaxed),
|
|
205
|
+
worker_status,
|
|
206
|
+
quarantined_count,
|
|
207
|
+
quarantine_replacements: c.quarantine_replacements.load(Ordering::Relaxed),
|
|
208
|
+
active_workers: c.active_workers.load(Ordering::Relaxed),
|
|
209
|
+
scale_ups: c.scale_ups.load(Ordering::Relaxed),
|
|
210
|
+
scale_downs: c.scale_downs.load(Ordering::Relaxed),
|
|
211
|
+
queue_histogram: c.queue_histogram.snapshot(),
|
|
212
|
+
wakes: c.wakes.load(Ordering::Relaxed),
|
|
213
|
+
hijacked_connections,
|
|
214
|
+
responses_cut: crate::response::responses_cut(),
|
|
215
|
+
websockets: crate::ws::snapshot(),
|
|
216
|
+
inline,
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
pub fn state_name(&self) -> &'static str {
|
|
221
|
+
match self.state {
|
|
222
|
+
STATE_READY => "ready",
|
|
223
|
+
STATE_DRAINING => "draining",
|
|
224
|
+
_ => "booting",
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/// The server's plain counts, in /stats's order: each one's key there, its metric's name
|
|
229
|
+
/// and help (`batch` has none), and its value.
|
|
230
|
+
// A table: rustfmt would give each row's key, metric and value a line each.
|
|
231
|
+
#[rustfmt::skip]
|
|
232
|
+
fn counts(&self) -> [(&'static str, Option<Metric>, u64); 15] {
|
|
233
|
+
let topology = &self.topology;
|
|
234
|
+
[
|
|
235
|
+
("workers", Some(("kino_workers", "Configured worker count.")), topology.workers as u64),
|
|
236
|
+
("threads", Some(("kino_threads_per_worker", "Configured threads per worker.")),
|
|
237
|
+
topology.threads as u64),
|
|
238
|
+
("batch", None, topology.batch as u64),
|
|
239
|
+
("respawns", Some(("kino_worker_respawns_total",
|
|
240
|
+
"Crashed workers replaced: a ractor respawned, or a worker thread replaced.")),
|
|
241
|
+
self.respawns),
|
|
242
|
+
("queued", Some(("kino_queue_depth", "Requests waiting for a worker.")), self.queued as u64),
|
|
243
|
+
("in_flight", Some(("kino_requests_in_flight", "Requests currently inside Ruby workers.")),
|
|
244
|
+
self.in_flight as u64),
|
|
245
|
+
("served", Some(("kino_requests_served_total", "Requests handed to Ruby workers.")),
|
|
246
|
+
self.served),
|
|
247
|
+
("rejected", Some(("kino_requests_rejected_total", "Requests rejected with a 503.")),
|
|
248
|
+
self.rejected),
|
|
249
|
+
("timeouts", Some(("kino_request_timeouts_total",
|
|
250
|
+
"Responses past the request timeout (client got a 504).")),
|
|
251
|
+
self.timeouts),
|
|
252
|
+
("max_workers", Some(("kino_max_workers",
|
|
253
|
+
"Worker pool ceiling (equals kino_workers for a fixed pool).")),
|
|
254
|
+
topology.max_workers as u64),
|
|
255
|
+
("active_workers", Some(("kino_active_workers", "Workers currently alive and serving.")),
|
|
256
|
+
self.active_workers as u64),
|
|
257
|
+
("scale_ups", Some(("kino_scale_ups_total", "Workers added by the pool scaler.")),
|
|
258
|
+
self.scale_ups),
|
|
259
|
+
("scale_downs", Some(("kino_scale_downs_total", "Idle workers retired by the pool scaler.")),
|
|
260
|
+
self.scale_downs),
|
|
261
|
+
("wakes", Some(("kino_wakes_total",
|
|
262
|
+
"Wake hook runs handed to workers; wakes that coalesce count once.")),
|
|
263
|
+
self.wakes),
|
|
264
|
+
("hijacked_connections", Some(("kino_hijacked_connections",
|
|
265
|
+
"Connections taken over through rack.hijack, or switched by a 101 with a callable, still open.")),
|
|
266
|
+
self.hijacked_connections as u64),
|
|
267
|
+
]
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/// Every stat /stats and Server#stats share, in that order.
|
|
271
|
+
fn emit(&self, e: &mut impl Emit) {
|
|
272
|
+
use Shape::{List, Map};
|
|
273
|
+
let topology = &self.topology;
|
|
274
|
+
e.stat("mode", Stat::Name(&topology.mode));
|
|
275
|
+
e.stat("lanes", Stat::Flag(self.lane_depths.is_some()));
|
|
276
|
+
e.counts(self.counts().map(|(key, _, count)| (key, count)));
|
|
277
|
+
e.nest("responses_cut", Map, |e| e.counts(self.responses_cut));
|
|
278
|
+
let ws = &self.websockets;
|
|
279
|
+
e.nest("websockets", Map, |e| {
|
|
280
|
+
e.stat("open", Stat::Count(ws.open));
|
|
281
|
+
e.counts(WsCount::named(ws.counts));
|
|
282
|
+
let refused = Refusal::named(ws.handshakes_refused);
|
|
283
|
+
e.nest("handshakes_refused", Map, |e| e.counts(refused));
|
|
284
|
+
e.nest("closed", Map, |e| e.counts(Cause::named(ws.closed)));
|
|
285
|
+
});
|
|
286
|
+
match &self.inline {
|
|
287
|
+
Some(inline) => e.nest("inline", Map, |e| {
|
|
288
|
+
e.counts(inline.counts().map(|(key, _, count)| (key, count)));
|
|
289
|
+
}),
|
|
290
|
+
None => e.stat("inline", Stat::Nil),
|
|
291
|
+
}
|
|
292
|
+
if let Some(depths) = &self.lane_depths {
|
|
293
|
+
let depths = depths.iter().map(|&depth| (ITEM, depth as u64));
|
|
294
|
+
e.nest("lane_depths", List, |e| e.counts(depths));
|
|
295
|
+
}
|
|
296
|
+
e.nest("worker_status", List, |e| {
|
|
297
|
+
for w in &self.worker_status {
|
|
298
|
+
e.nest(ITEM, Map, |e| {
|
|
299
|
+
e.counts([
|
|
300
|
+
("index", w.index as u64),
|
|
301
|
+
("served", w.served),
|
|
302
|
+
("in_flight", w.in_flight as u64),
|
|
303
|
+
("busy_ms", w.busy_ms),
|
|
304
|
+
]);
|
|
305
|
+
e.stat("quarantined", Stat::Flag(w.quarantined));
|
|
306
|
+
e.stat("retired", Stat::Flag(w.retired));
|
|
307
|
+
e.counts([
|
|
308
|
+
("websockets_open", w.sockets as u64),
|
|
309
|
+
("hijacked_connections", w.hijacked as u64),
|
|
310
|
+
("inline_connections", w.inline_conns as u64),
|
|
311
|
+
]);
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
let queue = &self.queue_histogram;
|
|
316
|
+
e.nest("queue_time", Map, |e| {
|
|
317
|
+
e.stat("count", Stat::Count(queue.count));
|
|
318
|
+
e.stat("sum_seconds", Stat::Seconds(queue.sum_seconds()));
|
|
319
|
+
});
|
|
320
|
+
e.stat("quarantined", Stat::Count(self.quarantined_count as u64));
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/// /stats: what Server#stats reports, plus the state and the version.
|
|
324
|
+
pub fn json(&self) -> String {
|
|
325
|
+
let mut json = Json {
|
|
326
|
+
out: String::with_capacity(1024),
|
|
327
|
+
first: true,
|
|
328
|
+
shape: Shape::List,
|
|
329
|
+
};
|
|
330
|
+
json.nest(ITEM, Shape::Map, |json| {
|
|
331
|
+
self.emit(json);
|
|
332
|
+
json.stat("state", Stat::Name(self.state_name()));
|
|
333
|
+
json.stat("version", Stat::Name(env!("CARGO_PKG_VERSION")));
|
|
334
|
+
});
|
|
335
|
+
json.out
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/// Server#stats.
|
|
339
|
+
pub fn hash(&self, ruby: &Ruby) -> Result<RHash, Error> {
|
|
340
|
+
let hash = ruby.hash_new();
|
|
341
|
+
let mut into = RubyHash {
|
|
342
|
+
ruby,
|
|
343
|
+
into: Container::Hash(hash),
|
|
344
|
+
failed: Ok(()),
|
|
345
|
+
};
|
|
346
|
+
self.emit(&mut into);
|
|
347
|
+
into.failed.map(|()| hash)
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/// Prometheus text exposition (version 0.0.4): each metric a counter
|
|
351
|
+
/// where its name ends in `_total`, else a gauge, but the histogram.
|
|
352
|
+
pub fn metrics(&self) -> String {
|
|
353
|
+
let mut out = String::with_capacity(8192);
|
|
354
|
+
self.write_metrics(&mut out)
|
|
355
|
+
.expect("writing to a String cannot fail");
|
|
356
|
+
out
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
fn write_metrics(&self, out: &mut String) -> std::fmt::Result {
|
|
360
|
+
for (name, help, samples) in self.metric_rows() {
|
|
361
|
+
let kind = match samples {
|
|
362
|
+
Samples::Histogram(_) => "histogram",
|
|
363
|
+
_ if name.ends_with("_total") => "counter",
|
|
364
|
+
_ => "gauge",
|
|
365
|
+
};
|
|
366
|
+
writeln!(out, "# HELP {name} {help}\n# TYPE {name} {kind}")?;
|
|
367
|
+
match samples {
|
|
368
|
+
Samples::One(value) => writeln!(out, "{name} {value}")?,
|
|
369
|
+
Samples::By(label, values) => {
|
|
370
|
+
for (key, value) in values {
|
|
371
|
+
writeln!(out, "{name}{{{label}=\"{key}\"}} {value}")?;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
Samples::Histogram(h) => {
|
|
375
|
+
let mut cumulative = 0;
|
|
376
|
+
for (count, bound_us) in h.buckets.iter().zip(QUEUE_BOUNDS_US) {
|
|
377
|
+
cumulative += count;
|
|
378
|
+
let le = bound_us as f64 / 1_000_000.0;
|
|
379
|
+
writeln!(out, "{name}_bucket{{le=\"{le}\"}} {cumulative}")?;
|
|
380
|
+
}
|
|
381
|
+
let total = cumulative + h.overflow;
|
|
382
|
+
writeln!(out, "{name}_bucket{{le=\"+Inf\"}} {total}")?;
|
|
383
|
+
writeln!(out, "{name}_sum {}\n{name}_count {total}", h.sum_seconds())?;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
Ok(())
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/// One sample per dispatch slot: `count` of its row.
|
|
391
|
+
fn per_worker(&self, count: fn(&WorkerStat) -> u64) -> Samples<'_> {
|
|
392
|
+
let sample = |w: &WorkerStat| (w.index.to_string(), count(w));
|
|
393
|
+
Samples::By("worker", self.worker_status.iter().map(sample).collect())
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/// Each metric's name, help and samples, in exposition order: /stats's, then the per-worker
|
|
397
|
+
/// series and what only /metrics has.
|
|
398
|
+
fn metric_rows(&self) -> Vec<(&'static str, &'static str, Samples<'_>)> {
|
|
399
|
+
use Samples::{By, One};
|
|
400
|
+
let ws = &self.websockets;
|
|
401
|
+
let plain = self.counts().into_iter().filter_map(|(_, metric, count)| {
|
|
402
|
+
let (name, help) = metric?;
|
|
403
|
+
Some((name, help, One(count)))
|
|
404
|
+
});
|
|
405
|
+
let sockets = WsCount::ALL.map(|count| {
|
|
406
|
+
let (name, help) = ws_metric(count);
|
|
407
|
+
(name, help, One(ws.count(count)))
|
|
408
|
+
});
|
|
409
|
+
let lane = self.lane_depths.as_ref().map(|depths| {
|
|
410
|
+
let sample = |(lane, &n): (usize, &usize)| (lane.to_string(), n as u64);
|
|
411
|
+
let samples = By("lane", depths.iter().enumerate().map(sample).collect());
|
|
412
|
+
let help = "Queued requests in each worker lane.";
|
|
413
|
+
("kino_lane_depth", help, samples)
|
|
414
|
+
});
|
|
415
|
+
// A table: rustfmt would give each row's name, help and samples a line each.
|
|
416
|
+
#[rustfmt::skip]
|
|
417
|
+
let (head, workers, tail) = ([
|
|
418
|
+
("kino_ready", "1 when serving, 0 while booting or draining.",
|
|
419
|
+
One(u64::from(self.state == STATE_READY))),
|
|
420
|
+
("kino_responses_cut_total",
|
|
421
|
+
"Streamed responses in this process that ended before the app finished them, by cause.",
|
|
422
|
+
by_cause(&self.responses_cut)),
|
|
423
|
+
("kino_websockets_open", "Native WebSockets open in this process.", One(ws.open)),
|
|
424
|
+
], [
|
|
425
|
+
("kino_websocket_handshakes_refused_total",
|
|
426
|
+
"WebSocket handshakes the app accepted that Kino answered with a 503, by cause.",
|
|
427
|
+
by_cause(&Refusal::named(ws.handshakes_refused))),
|
|
428
|
+
("kino_websockets_closed_total", "Native WebSockets closed in this process, by cause.",
|
|
429
|
+
by_cause(&Cause::named(ws.closed))),
|
|
430
|
+
("kino_worker_requests_served_total", "Requests handed to each dispatch slot.",
|
|
431
|
+
self.per_worker(|w| w.served)),
|
|
432
|
+
("kino_worker_in_flight",
|
|
433
|
+
"Requests, socket callbacks or wake hooks executing in each dispatch slot.",
|
|
434
|
+
self.per_worker(|w| w.in_flight as u64)),
|
|
435
|
+
("kino_worker_busy_ms",
|
|
436
|
+
"Age in ms of each slot's current request, socket callback or wake hook (0 when idle).",
|
|
437
|
+
self.per_worker(|w| w.busy_ms)),
|
|
438
|
+
("kino_worker_websockets_open",
|
|
439
|
+
"Native WebSockets each dispatch slot accepted whose close has not reached its worker yet.",
|
|
440
|
+
self.per_worker(|w| w.sockets as u64)),
|
|
441
|
+
("kino_worker_hijacked_connections",
|
|
442
|
+
"Connections each dispatch slot's requests handed over through rack.hijack, still open.",
|
|
443
|
+
self.per_worker(|w| w.hijacked as u64)),
|
|
444
|
+
], [
|
|
445
|
+
("kino_quarantined_workers", "Dispatch slots abandoned as wedged.",
|
|
446
|
+
One(self.quarantined_count as u64)),
|
|
447
|
+
("kino_quarantine_replacements_total", "Replacement workers spawned after a wedge.",
|
|
448
|
+
One(self.quarantine_replacements)),
|
|
449
|
+
("kino_request_queue_seconds",
|
|
450
|
+
"Seconds requests waited in the queue before a worker admitted them.",
|
|
451
|
+
Samples::Histogram(&self.queue_histogram)),
|
|
452
|
+
]);
|
|
453
|
+
// Only a server that serves inline has its series.
|
|
454
|
+
let inline = self.inline.iter().flat_map(|inline| {
|
|
455
|
+
let counts = inline.counts().into_iter();
|
|
456
|
+
let help = "Connections each dispatch slot's worker serves inline now.";
|
|
457
|
+
let per_worker = self.per_worker(|w| w.inline_conns as u64);
|
|
458
|
+
counts
|
|
459
|
+
.map(|(_, (name, help), count)| (name, help, One(count)))
|
|
460
|
+
.chain([("kino_worker_inline_connections", help, per_worker)])
|
|
461
|
+
});
|
|
462
|
+
let stats = plain.chain(head).chain(sockets).chain(lane).chain(workers);
|
|
463
|
+
stats.chain(inline).chain(tail).collect()
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/// A metric's name and help.
|
|
468
|
+
type Metric = (&'static str, &'static str);
|
|
469
|
+
|
|
470
|
+
// A table: rustfmt would give each row's name and help a line each.
|
|
471
|
+
#[rustfmt::skip]
|
|
472
|
+
fn ws_metric(count: WsCount) -> Metric {
|
|
473
|
+
match count {
|
|
474
|
+
WsCount::Opened => ("kino_websockets_opened_total", "Native WebSockets opened in this process."),
|
|
475
|
+
WsCount::MessagesReceived =>
|
|
476
|
+
("kino_websocket_messages_received_total", "WebSocket messages received from clients."),
|
|
477
|
+
WsCount::MessagesSent => ("kino_websocket_messages_sent_total",
|
|
478
|
+
"WebSocket messages written to clients: writes and publishes, one per write_raw."),
|
|
479
|
+
WsCount::MessagesSkipped => ("kino_websocket_messages_skipped_total",
|
|
480
|
+
"Publishes left out for when_full: :skip subscribers whose queue was full, one per subscriber per publish."),
|
|
481
|
+
WsCount::WritesRefused => ("kino_websocket_writes_refused_total",
|
|
482
|
+
"WebSocket writes and closes the app made that a full queue refused."),
|
|
483
|
+
WsCount::PublishesRefused => ("kino_websocket_publishes_refused_total",
|
|
484
|
+
"Publishes refused because the fan-out queue was full."),
|
|
485
|
+
WsCount::InboundPauses => ("kino_websocket_inbound_pauses_total",
|
|
486
|
+
"Times a WebSocket's reader paused with 16 of its messages waiting for its worker."),
|
|
487
|
+
WsCount::ReceivedBytes =>
|
|
488
|
+
("kino_websocket_received_bytes_total", "Bytes read from WebSocket clients."),
|
|
489
|
+
WsCount::SentBytes => ("kino_websocket_sent_bytes_total", "Bytes written to WebSocket clients."),
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/// One sample per cause.
|
|
494
|
+
fn by_cause(counts: &[(&str, u64)]) -> Samples<'static> {
|
|
495
|
+
let sample = |&(cause, n): &(&str, u64)| (cause.to_string(), n);
|
|
496
|
+
Samples::By("cause", counts.iter().map(sample).collect())
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/// A metric's samples: one, one per value of its label, or a histogram.
|
|
500
|
+
enum Samples<'a> {
|
|
501
|
+
One(u64),
|
|
502
|
+
By(&'static str, Vec<(String, u64)>),
|
|
503
|
+
Histogram(&'a QueueHistogramSnapshot),
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/// /stats's JSON. Keys and names are fixed identifiers: nothing to escape.
|
|
507
|
+
struct Json {
|
|
508
|
+
out: String,
|
|
509
|
+
/// Nothing written yet in the current object or list.
|
|
510
|
+
first: bool,
|
|
511
|
+
/// Whether the current container is an object or a list.
|
|
512
|
+
shape: Shape,
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
impl Json {
|
|
516
|
+
fn put(&mut self, key: &str, value: impl Display) {
|
|
517
|
+
if !std::mem::take(&mut self.first) {
|
|
518
|
+
self.out.push(',');
|
|
519
|
+
}
|
|
520
|
+
let written = match self.shape {
|
|
521
|
+
Shape::Map => write!(self.out, "\"{key}\":{value}"),
|
|
522
|
+
Shape::List => write!(self.out, "{value}"),
|
|
523
|
+
};
|
|
524
|
+
written.expect("writing to a String cannot fail");
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
impl Emit for Json {
|
|
529
|
+
fn stat(&mut self, key: &str, stat: Stat) {
|
|
530
|
+
match stat {
|
|
531
|
+
Stat::Count(count) => self.put(key, count),
|
|
532
|
+
Stat::Flag(on) => self.put(key, on),
|
|
533
|
+
Stat::Seconds(seconds) => self.put(key, seconds),
|
|
534
|
+
Stat::Name(name) => self.put(key, format_args!("\"{name}\"")),
|
|
535
|
+
Stat::Nil => self.put(key, "null"),
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
fn nest(&mut self, key: &str, shape: Shape, body: impl FnOnce(&mut Self)) {
|
|
540
|
+
let (open, close) = match shape {
|
|
541
|
+
Shape::Map => ('{', '}'),
|
|
542
|
+
Shape::List => ('[', ']'),
|
|
543
|
+
};
|
|
544
|
+
self.put(key, open);
|
|
545
|
+
let outer = std::mem::replace(&mut self.shape, shape);
|
|
546
|
+
self.first = true;
|
|
547
|
+
body(self);
|
|
548
|
+
self.shape = outer;
|
|
549
|
+
self.out.push(close);
|
|
550
|
+
self.first = false;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/// Server#stats's Hash: Symbol keys, names as Symbols. Keeps the first
|
|
555
|
+
/// error a Hash or Array raised, and fills nothing more after it.
|
|
556
|
+
struct RubyHash<'r> {
|
|
557
|
+
ruby: &'r Ruby,
|
|
558
|
+
into: Container,
|
|
559
|
+
failed: Result<(), Error>,
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/// The Hash or Array being filled.
|
|
563
|
+
enum Container {
|
|
564
|
+
Hash(RHash),
|
|
565
|
+
List(RArray),
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
impl RubyHash<'_> {
|
|
569
|
+
fn put(&mut self, key: &str, value: Value) {
|
|
570
|
+
if self.failed.is_err() {
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
self.failed = match self.into {
|
|
574
|
+
Container::Hash(hash) => hash.aset(self.ruby.to_symbol(key), value),
|
|
575
|
+
Container::List(list) => list.push(value),
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
impl Emit for RubyHash<'_> {
|
|
581
|
+
fn stat(&mut self, key: &str, stat: Stat) {
|
|
582
|
+
let ruby = self.ruby;
|
|
583
|
+
let value = match stat {
|
|
584
|
+
Stat::Count(count) => ruby.into_value(count),
|
|
585
|
+
Stat::Flag(on) => ruby.into_value(on),
|
|
586
|
+
Stat::Seconds(seconds) => ruby.into_value(seconds),
|
|
587
|
+
Stat::Name(name) => ruby.to_symbol(name).as_value(),
|
|
588
|
+
Stat::Nil => ruby.qnil().as_value(),
|
|
589
|
+
};
|
|
590
|
+
self.put(key, value);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
fn nest(&mut self, key: &str, shape: Shape, body: impl FnOnce(&mut Self)) {
|
|
594
|
+
let (inner, value) = match shape {
|
|
595
|
+
Shape::Map => {
|
|
596
|
+
let hash = self.ruby.hash_new();
|
|
597
|
+
(Container::Hash(hash), hash.as_value())
|
|
598
|
+
}
|
|
599
|
+
Shape::List => {
|
|
600
|
+
let list = self.ruby.ary_new();
|
|
601
|
+
(Container::List(list), list.as_value())
|
|
602
|
+
}
|
|
603
|
+
};
|
|
604
|
+
self.put(key, value);
|
|
605
|
+
let outer = std::mem::replace(&mut self.into, inner);
|
|
606
|
+
body(self);
|
|
607
|
+
self.into = outer;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
#[cfg(test)]
|
|
612
|
+
mod tests {
|
|
613
|
+
use std::sync::atomic::Ordering::Relaxed;
|
|
614
|
+
use std::sync::Arc;
|
|
615
|
+
use std::time::{Duration, Instant};
|
|
616
|
+
|
|
617
|
+
use super::*;
|
|
618
|
+
use crate::queue::TICK;
|
|
619
|
+
use crate::registry::{QueueHistogramSnapshot, STATE_BOOTING, STATE_READY};
|
|
620
|
+
use crate::testing::{assign, slot, TestServer};
|
|
621
|
+
|
|
622
|
+
/// A snapshot with every count distinct: lanes, a busy worker that owns
|
|
623
|
+
/// two WebSockets, and a retired idle one whose requests handed three
|
|
624
|
+
/// connections over through rack.hijack.
|
|
625
|
+
fn snapshot() -> Snapshot {
|
|
626
|
+
let worker = |index, served, in_flight, busy_ms, retired, sockets, hijacked| WorkerStat {
|
|
627
|
+
index,
|
|
628
|
+
served,
|
|
629
|
+
in_flight,
|
|
630
|
+
busy_ms,
|
|
631
|
+
quarantined: false,
|
|
632
|
+
retired,
|
|
633
|
+
sockets,
|
|
634
|
+
hijacked,
|
|
635
|
+
inline_conns: 0,
|
|
636
|
+
generation: 0,
|
|
637
|
+
};
|
|
638
|
+
let mut buckets = [0; crate::registry::QUEUE_BOUNDS_US.len()];
|
|
639
|
+
buckets[0] = 3; // <= 0.5 ms
|
|
640
|
+
buckets[2] = 1; // <= 2.5 ms
|
|
641
|
+
Snapshot {
|
|
642
|
+
topology: Topology {
|
|
643
|
+
mode: "ractor".to_string(),
|
|
644
|
+
workers: 8,
|
|
645
|
+
max_workers: 32,
|
|
646
|
+
threads: 1,
|
|
647
|
+
batch: 1,
|
|
648
|
+
},
|
|
649
|
+
respawns: 2,
|
|
650
|
+
queued: 3,
|
|
651
|
+
in_flight: 4,
|
|
652
|
+
served: 100,
|
|
653
|
+
rejected: 5,
|
|
654
|
+
timeouts: 6,
|
|
655
|
+
lane_depths: Some(vec![2, 0]),
|
|
656
|
+
state: STATE_READY,
|
|
657
|
+
worker_status: vec![
|
|
658
|
+
worker(0, 10, 1, 4, false, 2, 0),
|
|
659
|
+
worker(1, 7, 0, 0, true, 0, 3),
|
|
660
|
+
],
|
|
661
|
+
quarantined_count: 1,
|
|
662
|
+
quarantine_replacements: 7,
|
|
663
|
+
active_workers: 12,
|
|
664
|
+
scale_ups: 3,
|
|
665
|
+
scale_downs: 1,
|
|
666
|
+
queue_histogram: QueueHistogramSnapshot {
|
|
667
|
+
buckets,
|
|
668
|
+
overflow: 1,
|
|
669
|
+
sum_us: 1_500_000,
|
|
670
|
+
count: 5,
|
|
671
|
+
},
|
|
672
|
+
wakes: 9,
|
|
673
|
+
hijacked_connections: 2,
|
|
674
|
+
responses_cut: [("client_gone", 21), ("shutdown", 22), ("crash", 23)],
|
|
675
|
+
websockets: crate::ws::WsSnapshot {
|
|
676
|
+
open: 7,
|
|
677
|
+
counts: [1000, 41, 43, 5, 12, 1, 13, 4096, 8192],
|
|
678
|
+
closed: std::array::from_fn(|n| 100 + n as u64),
|
|
679
|
+
handshakes_refused: [11, 14, 15],
|
|
680
|
+
},
|
|
681
|
+
inline: None,
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
#[test]
|
|
686
|
+
fn stats_json_reports_every_field_in_order_and_the_state_name() {
|
|
687
|
+
let expected = concat!(
|
|
688
|
+
r#"{"mode":"ractor","lanes":true,"workers":8,"threads":1,"batch":1,"respawns":2,"#,
|
|
689
|
+
r#""queued":3,"in_flight":4,"served":100,"rejected":5,"timeouts":6,"max_workers":32,"#,
|
|
690
|
+
r#""active_workers":12,"scale_ups":3,"scale_downs":1,"wakes":9,"#,
|
|
691
|
+
r#""hijacked_connections":2,"#,
|
|
692
|
+
r#""responses_cut":{"client_gone":21,"shutdown":22,"crash":23},"#,
|
|
693
|
+
r#""websockets":{"open":7,"opened":1000,"messages_received":41,"messages_sent":43,"#,
|
|
694
|
+
r#""messages_skipped":5,"writes_refused":12,"publishes_refused":1,"#,
|
|
695
|
+
r#""inbound_pauses":13,"received_bytes":4096,"sent_bytes":8192,"#,
|
|
696
|
+
r#""handshakes_refused":{"websocket_max":11,"draining":14,"worker_closing":15},"#,
|
|
697
|
+
r#""closed":{"normal":100,"going_away":101,"protocol":102,"too_big":103,"slow":104,"#,
|
|
698
|
+
r#""dead_peer":105,"client_gone":106,"crash":107,"handshake_failed":108}},"#,
|
|
699
|
+
r#""inline":null,"lane_depths":[2,0],"worker_status":["#,
|
|
700
|
+
r#"{"index":0,"served":10,"in_flight":1,"busy_ms":4,"quarantined":false,"#,
|
|
701
|
+
r#""retired":false,"websockets_open":2,"hijacked_connections":0,"#,
|
|
702
|
+
r#""inline_connections":0},"#,
|
|
703
|
+
r#"{"index":1,"served":7,"in_flight":0,"busy_ms":0,"quarantined":false,"#,
|
|
704
|
+
r#""retired":true,"websockets_open":0,"hijacked_connections":3,"#,
|
|
705
|
+
r#""inline_connections":0}],"#,
|
|
706
|
+
r#""queue_time":{"count":5,"sum_seconds":1.5},"quarantined":1,"state":"ready","#,
|
|
707
|
+
);
|
|
708
|
+
let version = env!("CARGO_PKG_VERSION");
|
|
709
|
+
assert_eq!(
|
|
710
|
+
snapshot().json(),
|
|
711
|
+
format!(r#"{expected}"version":"{version}"}}"#)
|
|
712
|
+
);
|
|
713
|
+
|
|
714
|
+
let plain = Snapshot {
|
|
715
|
+
lane_depths: None,
|
|
716
|
+
worker_status: Vec::new(),
|
|
717
|
+
state: STATE_BOOTING,
|
|
718
|
+
..snapshot()
|
|
719
|
+
};
|
|
720
|
+
let json = plain.json();
|
|
721
|
+
for (needle, present) in [
|
|
722
|
+
(r#""lanes":false"#, true),
|
|
723
|
+
("lane_depths", false),
|
|
724
|
+
(r#""worker_status":[]"#, true),
|
|
725
|
+
(r#""state":"booting""#, true),
|
|
726
|
+
] {
|
|
727
|
+
assert_eq!(json.contains(needle), present, "{needle} in {json}");
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/// A labeled series: its name, kind, label, and each label's sample.
|
|
732
|
+
type Labeled = (
|
|
733
|
+
&'static str,
|
|
734
|
+
&'static str,
|
|
735
|
+
&'static str,
|
|
736
|
+
&'static [(&'static str, u64)],
|
|
737
|
+
);
|
|
738
|
+
|
|
739
|
+
/// That `text` has each series: a plain one's name, kind and sample, a labeled one's.
|
|
740
|
+
fn has_series(text: &str, plain: &[(&str, &str, u64)], labeled: &[Labeled]) {
|
|
741
|
+
let plain = plain
|
|
742
|
+
.iter()
|
|
743
|
+
.map(|(name, kind, value)| format!("# TYPE {name} {kind}\n{name} {value}\n"));
|
|
744
|
+
let labeled = labeled.iter().map(|(name, kind, label, values)| {
|
|
745
|
+
let sample =
|
|
746
|
+
|(at, value): &(&str, u64)| format!("{name}{{{label}=\"{at}\"}} {value}\n");
|
|
747
|
+
let samples: String = values.iter().map(sample).collect();
|
|
748
|
+
format!("# TYPE {name} {kind}\n{samples}")
|
|
749
|
+
});
|
|
750
|
+
for series in plain.chain(labeled) {
|
|
751
|
+
assert!(text.contains(&series), "expected\n{series}in:\n{text}");
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
#[test]
|
|
756
|
+
fn metrics_text_is_prometheus_shaped() {
|
|
757
|
+
let text = snapshot().metrics();
|
|
758
|
+
let samples = [
|
|
759
|
+
("kino_requests_served_total", "counter", 100),
|
|
760
|
+
("kino_ready", "gauge", 1),
|
|
761
|
+
("kino_max_workers", "gauge", 32),
|
|
762
|
+
("kino_active_workers", "gauge", 12),
|
|
763
|
+
("kino_scale_ups_total", "counter", 3),
|
|
764
|
+
("kino_scale_downs_total", "counter", 1),
|
|
765
|
+
("kino_quarantined_workers", "gauge", 1),
|
|
766
|
+
("kino_quarantine_replacements_total", "counter", 7),
|
|
767
|
+
("kino_wakes_total", "counter", 9),
|
|
768
|
+
("kino_hijacked_connections", "gauge", 2),
|
|
769
|
+
("kino_websockets_open", "gauge", 7),
|
|
770
|
+
("kino_websockets_opened_total", "counter", 1000),
|
|
771
|
+
("kino_websocket_messages_received_total", "counter", 41),
|
|
772
|
+
("kino_websocket_messages_sent_total", "counter", 43),
|
|
773
|
+
("kino_websocket_messages_skipped_total", "counter", 5),
|
|
774
|
+
("kino_websocket_writes_refused_total", "counter", 12),
|
|
775
|
+
("kino_websocket_publishes_refused_total", "counter", 1),
|
|
776
|
+
("kino_websocket_inbound_pauses_total", "counter", 13),
|
|
777
|
+
("kino_websocket_received_bytes_total", "counter", 4096),
|
|
778
|
+
("kino_websocket_sent_bytes_total", "counter", 8192),
|
|
779
|
+
];
|
|
780
|
+
// One row per case; rustfmt would split each row field by field.
|
|
781
|
+
#[rustfmt::skip]
|
|
782
|
+
let labeled: [Labeled; 9] = [
|
|
783
|
+
("kino_responses_cut_total", "counter", "cause",
|
|
784
|
+
&[("client_gone", 21), ("shutdown", 22), ("crash", 23)]),
|
|
785
|
+
("kino_websocket_handshakes_refused_total", "counter", "cause",
|
|
786
|
+
&[("websocket_max", 11), ("draining", 14), ("worker_closing", 15)]),
|
|
787
|
+
("kino_websockets_closed_total", "counter", "cause",
|
|
788
|
+
&[("normal", 100), ("going_away", 101), ("protocol", 102), ("too_big", 103),
|
|
789
|
+
("slow", 104), ("dead_peer", 105), ("client_gone", 106), ("crash", 107),
|
|
790
|
+
("handshake_failed", 108)]),
|
|
791
|
+
("kino_lane_depth", "gauge", "lane", &[("0", 2), ("1", 0)]),
|
|
792
|
+
("kino_worker_requests_served_total", "counter", "worker", &[("0", 10), ("1", 7)]),
|
|
793
|
+
("kino_worker_in_flight", "gauge", "worker", &[("0", 1), ("1", 0)]),
|
|
794
|
+
("kino_worker_busy_ms", "gauge", "worker", &[("0", 4), ("1", 0)]),
|
|
795
|
+
("kino_worker_websockets_open", "gauge", "worker", &[("0", 2), ("1", 0)]),
|
|
796
|
+
("kino_worker_hijacked_connections", "gauge", "worker", &[("0", 0), ("1", 3)]),
|
|
797
|
+
];
|
|
798
|
+
has_series(&text, &samples, &labeled);
|
|
799
|
+
// Cumulative: le=0.0025 holds buckets 0 to 2.
|
|
800
|
+
let histogram = concat!(
|
|
801
|
+
"# TYPE kino_request_queue_seconds histogram\n",
|
|
802
|
+
"kino_request_queue_seconds_bucket{le=\"0.0005\"} 3\n",
|
|
803
|
+
"kino_request_queue_seconds_bucket{le=\"0.001\"} 3\n",
|
|
804
|
+
"kino_request_queue_seconds_bucket{le=\"0.0025\"} 4\n",
|
|
805
|
+
);
|
|
806
|
+
let total = concat!(
|
|
807
|
+
"kino_request_queue_seconds_bucket{le=\"+Inf\"} 5\n",
|
|
808
|
+
"kino_request_queue_seconds_sum 1.5\n",
|
|
809
|
+
"kino_request_queue_seconds_count 5\n",
|
|
810
|
+
);
|
|
811
|
+
assert!(text.contains(histogram) && text.contains(total), "{text}");
|
|
812
|
+
let draining = Snapshot {
|
|
813
|
+
state: crate::registry::STATE_DRAINING,
|
|
814
|
+
..snapshot()
|
|
815
|
+
};
|
|
816
|
+
assert!(draining.metrics().contains("\nkino_ready 0\n"));
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
#[test]
|
|
820
|
+
fn a_quarantined_worker_reads_as_idle() {
|
|
821
|
+
let server = TestServer::shared_queue().workers(1).build();
|
|
822
|
+
let wedged = slot(&server, 0);
|
|
823
|
+
// In a request since the clock began, but given up on as wedged.
|
|
824
|
+
wedged.load.in_flight.store(1, Relaxed);
|
|
825
|
+
wedged.life.quarantined.store(true, Relaxed);
|
|
826
|
+
|
|
827
|
+
let row = &Snapshot::take(&server).worker_status[0];
|
|
828
|
+
assert_eq!((row.quarantined, row.busy_ms), (true, 0));
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
/// How long a worker_loop worker's thread stays away from its loop in
|
|
832
|
+
/// these tests, past any wait on its doorbell.
|
|
833
|
+
const AWAY: Duration = Duration::from_millis(100);
|
|
834
|
+
|
|
835
|
+
fn busy_ms(server: &ServerInner) -> u64 {
|
|
836
|
+
collect_worker_status(server)[0].busy_ms
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
#[test]
|
|
840
|
+
fn a_worker_loop_worker_reads_busy_once_its_thread_stays_away_with_work_waiting() {
|
|
841
|
+
use crate::queue::testing::{admit_next, nonblock, socket_sends};
|
|
842
|
+
use crate::queue::Room::{All, Events, Requests};
|
|
843
|
+
use crate::registry::BoxedCtx;
|
|
844
|
+
type Leave = fn(&ServerInner, &Arc<crate::slot::WorkerSlot>) -> Option<BoxedCtx>;
|
|
845
|
+
// How its thread left the loop, what came after, and whether it
|
|
846
|
+
// reads busy once the thread stayed away past any wait.
|
|
847
|
+
// One row per case; rustfmt would split each row field by field.
|
|
848
|
+
#[rustfmt::skip]
|
|
849
|
+
let rows: [(&str, Leave, bool); 8] = [
|
|
850
|
+
("to wait on its doorbell, nothing to run",
|
|
851
|
+
|server, slot| { nonblock(server, slot, All); None }, false),
|
|
852
|
+
("with a request it took", |server, slot| { assign(slot); Some(admit_next(server, slot)) }, true),
|
|
853
|
+
("to wait on its doorbell, a request half a tick in",
|
|
854
|
+
|server, slot| { assert_eq!(nonblock(server, slot, All), "nothing"); std::thread::sleep(TICK / 2); assign(slot); None }, true),
|
|
855
|
+
("to wait for events, an event half a tick in",
|
|
856
|
+
|server, slot| { assert_eq!(nonblock(server, slot, Events), "nothing"); std::thread::sleep(TICK / 2); socket_sends(slot, 0); None }, true),
|
|
857
|
+
("to wait for requests, its wake hook due, its doorbell taken",
|
|
858
|
+
|server, slot| { assert!(slot.wake()); nonblock(server, slot, Requests); None }, true),
|
|
859
|
+
("handed its end at the drain, then woken",
|
|
860
|
+
|server, slot| { server.close_queues(); nonblock(server, slot, All); slot.wake(); None }, false),
|
|
861
|
+
("handed its end at its retirement, then woken",
|
|
862
|
+
|server, slot| { slot.retire(); nonblock(server, slot, All); slot.wake(); None }, false),
|
|
863
|
+
("as its worker crashed, then woken",
|
|
864
|
+
|_, slot| { slot.abort(crate::response::Cut::Crash); slot.wake(); None }, false),
|
|
865
|
+
];
|
|
866
|
+
for (left, leave, busy) in rows {
|
|
867
|
+
let server = TestServer::worker_loop().workers(1).build();
|
|
868
|
+
let slot = slot(&server, 0);
|
|
869
|
+
let left_at = Instant::now();
|
|
870
|
+
let open = leave(&server, &slot);
|
|
871
|
+
let read = busy_ms(&server);
|
|
872
|
+
// Against the time that really passed: a loaded runner can
|
|
873
|
+
// oversleep half a tick by more than half a tick.
|
|
874
|
+
let grace = if open.is_some() { Duration::ZERO } else { TICK };
|
|
875
|
+
let overdue = left_at.elapsed().saturating_sub(grace).as_millis() as u64;
|
|
876
|
+
assert!(
|
|
877
|
+
read <= overdue,
|
|
878
|
+
"{left}: a wait on the doorbell is time in the loop until it is overdue: read {read} ms busy, at most {overdue} ms overdue"
|
|
879
|
+
);
|
|
880
|
+
|
|
881
|
+
std::thread::sleep(TICK + AWAY);
|
|
882
|
+
|
|
883
|
+
let read = busy_ms(&server);
|
|
884
|
+
assert_eq!(
|
|
885
|
+
read >= AWAY.as_millis() as u64,
|
|
886
|
+
busy,
|
|
887
|
+
"a worker_loop worker whose thread left its loop {left}, {AWAY:?} past any wait, read {read} ms busy"
|
|
888
|
+
);
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
#[test]
|
|
893
|
+
fn inline_serving_is_reported_in_metrics_and_json_only_by_a_server_that_serves_inline() {
|
|
894
|
+
let mut s = snapshot();
|
|
895
|
+
s.worker_status[1].inline_conns = 2;
|
|
896
|
+
// 3 connections now, 10 requests served, 2 connections declined, 4 handed back and 1
|
|
897
|
+
// cut off; 7 spins for a next request.
|
|
898
|
+
s.inline = Some(InlineSnapshot {
|
|
899
|
+
connections: 3,
|
|
900
|
+
requests: 10,
|
|
901
|
+
declined: 2,
|
|
902
|
+
handbacks: 4,
|
|
903
|
+
closed: 1,
|
|
904
|
+
spins: 7,
|
|
905
|
+
});
|
|
906
|
+
let plain = [
|
|
907
|
+
("kino_inline_connections", "gauge", 3),
|
|
908
|
+
("kino_inline_requests_total", "counter", 10),
|
|
909
|
+
("kino_inline_declined_total", "counter", 2),
|
|
910
|
+
("kino_inline_handbacks_total", "counter", 4),
|
|
911
|
+
("kino_inline_closed_total", "counter", 1),
|
|
912
|
+
("kino_inline_spins_total", "counter", 7),
|
|
913
|
+
];
|
|
914
|
+
let per_worker: Labeled = (
|
|
915
|
+
"kino_worker_inline_connections",
|
|
916
|
+
"gauge",
|
|
917
|
+
"worker",
|
|
918
|
+
&[("0", 0), ("1", 2)],
|
|
919
|
+
);
|
|
920
|
+
has_series(&s.metrics(), &plain, &[per_worker]);
|
|
921
|
+
let json = s.json();
|
|
922
|
+
let inline = concat!(
|
|
923
|
+
r#""inline":{"connections":3,"requests":10,"declined":2,"handbacks":4,"#,
|
|
924
|
+
r#""closed":1,"spins":7}"#
|
|
925
|
+
);
|
|
926
|
+
assert!(json.contains(inline), "expected {inline} in {json}");
|
|
927
|
+
|
|
928
|
+
// None of it from a server that serves nothing inline.
|
|
929
|
+
let text = snapshot().metrics();
|
|
930
|
+
assert!(
|
|
931
|
+
!text.contains("kino_inline_") && !text.contains("kino_worker_inline_"),
|
|
932
|
+
"inline series without inline serving:\n{text}"
|
|
933
|
+
);
|
|
934
|
+
assert!(snapshot().json().contains(r#""inline":null"#));
|
|
935
|
+
}
|
|
936
|
+
}
|