1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//! Inbound event handling: socket-readable, cross-core ring drain, and
//! connection teardown. The event loop (`run`), transport setup
//! (accept_ready, flush_conn, flush_dirty, maybe_auto_rewrite_aof), and
//! cross-shard send/backlog plumbing live in [`crate::shard`]; the
//! *semantics* (routing, execution, reduction) live in [`crate::exec`].
//! Split out so each file stays under the 500-LOC house rule without
//! breaking the established two-impl-block layering.
use std::io;
use kevy_resp::parse_command_borrowed;
use crate::Commands;
use crate::message::{Inbound, Op};
use crate::shard::Shard;
impl<C: Commands> Shard<C> {
/// Socket readable: read until WouldBlock, then parse out every full
/// RESP command and dispatch it.
///
/// The local fast path dispatches straight from an `ArgvBorrowed` view
/// into the connection's read buffer — no per-cmd memcpy. We swap
/// `conn.input` onto the stack (`mem::take`) for the parse-and-dispatch
/// loop so the borrowed argv doesn't conflict with `&mut self`; after
/// each command we `drain(..consumed)` on the local buf, and finally
/// swap the buf back into the connection (if it still exists). Cross-
/// shard / MULTI queue / AOF call `args.to_argv()` at the handoff
/// juncture; only those paths still materialise an owned `Argv`.
pub(crate) fn conn_readable(&mut self, conn_id: u64) -> io::Result<()> {
{
let Some(conn) = self.conns.get_mut(&conn_id) else {
return Ok(());
};
loop {
match conn.sock.read(&mut self.read_buf) {
Ok(0) => {
conn.closing = true;
break;
}
Ok(n) => conn.input.extend_from_slice(&self.read_buf[..n]),
Err(e) if e.kind() == io::ErrorKind::WouldBlock => break,
Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
Err(_) => {
conn.closing = true;
break;
}
}
}
}
// Swap conn.input onto the stack so parse_command_borrowed can lend
// it to ArgvBorrowed without colliding with &mut self in dispatch.
let mut input_buf = match self.conns.get_mut(&conn_id) {
Some(c) => std::mem::take(&mut c.input),
None => return Ok(()),
};
let mut had_protocol_error = false;
loop {
let parse = parse_command_borrowed(&input_buf);
let (argv, consumed) = match parse {
Ok(Some(t)) => t,
Ok(None) => break,
Err(_) => {
had_protocol_error = true;
break;
}
};
if let Some(key) = argv.get(1) {
self.store.prefetch_for_key(key);
}
self.handle_command(conn_id, &argv);
drop(argv);
input_buf.drain(..consumed);
if !self.conns.contains_key(&conn_id) {
// Connection was closed mid-batch; drop the rest of the buf.
return Ok(());
}
}
if let Some(c) = self.conns.get_mut(&conn_id) {
c.input = input_buf;
}
if had_protocol_error {
self.protocol_error(conn_id);
}
self.flush_conn(conn_id)
}
/// Drain inbound cross-core messages from every peer ring; returns
/// whether any were processed.
pub(crate) fn drain_inbound(&mut self) -> io::Result<bool> {
let mut did = false;
for src in 0..self.nshards {
if src == self.id {
continue; // no self-ring
}
while let Some(msg) = self.inboxes[src].as_mut().expect("peer inbox").pop() {
did = true;
match msg {
Inbound::Request {
origin,
conn,
seq,
op,
} => {
let part = self.exec_op(op);
self.send_to(origin, Inbound::Response { conn, seq, part });
}
Inbound::Response { conn, seq, part } => {
self.fold(conn, seq, part);
self.flush_conn(conn)?;
}
// Batched single-key dispatches to this (owning) shard:
// exec each locally, reply as one `ResponseBatch` to the
// origin.
Inbound::RequestBatch { origin, reqs } => {
let mut resps = Vec::with_capacity(reqs.len());
for (conn, seq, argv, proto) in reqs {
let part = self.exec_op(Op::Dispatch(argv, proto));
resps.push((conn, seq, part));
}
self.send_to(origin, Inbound::ResponseBatch(resps));
}
// Batched replies: fold each by seq, then flush each
// touched conn once (dedup — pipelined replies share a
// conn).
Inbound::ResponseBatch(resps) => {
let mut to_flush: Vec<u64> = Vec::new();
for (conn, seq, part) in resps {
self.fold(conn, seq, part);
if !to_flush.contains(&conn) {
to_flush.push(conn);
}
}
for conn in to_flush {
self.flush_conn(conn)?;
}
}
// Fire-and-forget batched pub/sub delivery; appended
// subscriber output is flushed via `flush_dirty`.
Inbound::DeliverPublish(batch) => {
for m in &batch {
self.deliver_publish(&m.0, &m.1);
}
}
}
}
}
Ok(did)
}
/// Tear down a closing connection: deregister from the poller, drop
/// its channel + pattern subscriptions from the shared registries
/// and the per-shard tables, and release its `Socket` (closing the
/// fd).
pub(crate) fn close_conn(&mut self, conn_id: u64) {
if let Some(conn) = self.conns.remove(&conn_id) {
let fd = conn.sock.raw();
let _ = self.poller.delete(fd);
self.fd_to_conn.remove(&fd);
// Drop any BLPOP / BRPOP / XREAD BLOCK waiter the closing conn
// was parked in, across all its watched keys. Cheap fast-out
// when nothing is blocked (the common case).
self.blocked.drop_for_conn(conn_id);
self.unregister_subs(&conn.sub);
// Drop the conn's psub local table entries first (`unregister_psubs`
// reads `psub_local` to decide if our shard bit should be cleared).
for pat in &conn.psub {
if let Some(ids) = self.psub_local.get_mut(pat) {
ids.retain(|&id| id != conn_id);
if ids.is_empty() {
self.psub_local.remove(pat);
}
}
}
self.unregister_psubs(&conn.psub);
// conn (and its Socket) dropped here → fd closed.
}
}
}