dynomite-engine 0.0.2

Embeddable Dynamo-style distributed replication engine: token-ring partitioning, gossip cluster, hinted handoff, anti-entropy, RediSearch FT.* surface.
Documentation
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! CLIENT-role connection driver.
//!
//! The CLIENT role is the engine's view of an inbound connection
//! from a Redis or memcache client. The driver:
//!
//! 1. Reads bytes from the [`crate::io::reactor::Transport`] into
//!    the connection's recv mbuf chain.
//! 2. Drives the appropriate datastore parser
//!    ([`crate::proto::redis::redis_parse_req`] or
//!    [`crate::proto::memcache::memcache_parse_req`]) over the
//!    chain.
//! 3. Hands every fully-parsed request to the configured
//!    [`crate::net::Dispatcher`] and waits for a response on the
//!    per-connection mpsc channel.
//! 4. Writes the response bytes back to the transport.
//!
//! The driver runs a single tokio `select!` per iteration, draining
//! pending response bytes first so the loop's read / write
//! arms never block on a saturated peer.
//!
//! The Stage 9 implementation does not yet reach into the cluster
//! layer (Stage 10) or the entropy reconciliation (Stage 11);
//! those plug in through the [`Dispatcher`] hook the proxy
//! installs.

use std::sync::Arc;
use std::time::Duration;

use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::sync::mpsc;

use crate::conf::DataStore;
use crate::core::types::MsgId;
use crate::io::reactor::ConnRole;
use crate::msg::{response, Msg, MsgParseResult, MsgType};
use crate::net::conn::Conn;
use crate::net::dispatcher::{DispatchOutcome, Dispatcher, OutboundEnvelope};
use crate::net::NetError;

/// Stage-9 read buffer size for the client driver.
///
/// Sized to the typical Redis bulk header so inline GET/SET
/// commands fit in a single read; larger payloads are appended
/// across iterations.
const CLIENT_READ_CHUNK: usize = 4096;

/// Outcome reported by [`client_loop`] when it finishes.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ClientLoopOutcome {
    /// Peer closed (EOF) and queues drained cleanly.
    Eof,
    /// Driver was asked to exit by the dispatcher.
    Cancelled,
}

/// Client-side request handler bundle.
///
/// Built by [`crate::net::proxy::Proxy`] and passed into
/// [`client_loop`].
pub struct ClientHandler {
    dispatcher: Arc<dyn Dispatcher>,
    response_tx: mpsc::Sender<OutboundEnvelope>,
    data_store: DataStore,
    next_msg_id: u64,
    read_timeout: Option<Duration>,
    gossip: Option<Arc<crate::cluster::gossip::GossipHandler>>,
}

impl ClientHandler {
    /// Build a client handler.
    ///
    /// # Examples
    ///
    /// ```
    /// use dynomite::conf::DataStore;
    /// use dynomite::net::{ClientHandler, NoopDispatcher};
    /// use std::sync::Arc;
    /// use tokio::sync::mpsc;
    /// let (tx, _rx) = mpsc::channel(1);
    /// let _h = ClientHandler::new(Arc::new(NoopDispatcher), tx, DataStore::Redis);
    /// ```
    #[must_use]
    pub fn new(
        dispatcher: Arc<dyn Dispatcher>,
        response_tx: mpsc::Sender<OutboundEnvelope>,
        data_store: DataStore,
    ) -> Self {
        Self {
            dispatcher,
            response_tx,
            data_store,
            next_msg_id: 1,
            read_timeout: None,
            gossip: None,
        }
    }

    /// Set the per-read timeout. None disables it.
    #[must_use]
    pub fn with_read_timeout(mut self, t: Option<Duration>) -> Self {
        self.read_timeout = t;
        self
    }

    /// Attach a gossip handler. Inbound peer connections served
    /// through this handler dispatch gossip-class dnode frames
    /// into the supplied handler instead of the datastore parser.
    /// Data-plane connections (CLIENT role) leave it `None`.
    #[must_use]
    pub fn with_gossip(mut self, gossip: Arc<crate::cluster::gossip::GossipHandler>) -> Self {
        self.gossip = Some(gossip);
        self
    }

    /// Borrow the attached gossip handler, if any.
    #[must_use]
    pub fn gossip(&self) -> Option<&Arc<crate::cluster::gossip::GossipHandler>> {
        self.gossip.as_ref()
    }

    /// Datastore the handler parses.
    #[must_use]
    pub fn data_store(&self) -> DataStore {
        self.data_store
    }

    /// Borrow the dispatcher this handler routes parsed requests
    /// into. Exposed so role-specific drivers (CLIENT,
    /// DNODE_PEER_CLIENT) can share the same dispatch contract.
    ///
    /// # Examples
    ///
    /// ```
    /// use dynomite::conf::DataStore;
    /// use dynomite::net::{ClientHandler, NoopDispatcher};
    /// use std::sync::Arc;
    /// use tokio::sync::mpsc;
    /// let (tx, _rx) = mpsc::channel(1);
    /// let h = ClientHandler::new(Arc::new(NoopDispatcher), tx, DataStore::Redis);
    /// let _ = h.dispatcher();
    /// ```
    #[must_use]
    pub fn dispatcher(&self) -> &Arc<dyn Dispatcher> {
        &self.dispatcher
    }

    /// Borrow the per-connection response sender. The dispatcher
    /// uses a clone of this channel to push asynchronously-produced
    /// responses back to the FSM.
    ///
    /// # Examples
    ///
    /// ```
    /// use dynomite::conf::DataStore;
    /// use dynomite::net::{ClientHandler, NoopDispatcher};
    /// use std::sync::Arc;
    /// use tokio::sync::mpsc;
    /// let (tx, _rx) = mpsc::channel(1);
    /// let h = ClientHandler::new(Arc::new(NoopDispatcher), tx, DataStore::Redis);
    /// let _clone = h.response_tx().clone();
    /// ```
    #[must_use]
    pub fn response_tx(&self) -> &mpsc::Sender<OutboundEnvelope> {
        &self.response_tx
    }

    fn alloc_msg_id(&mut self) -> MsgId {
        let id = self.next_msg_id;
        self.next_msg_id = self.next_msg_id.wrapping_add(1).max(1);
        id
    }
}

/// Drive the client FSM until the peer closes or the dispatcher
/// asks the driver to exit.
///
/// `rx` receives responses produced by the dispatcher; the driver
/// writes the response bytes to the transport in the order it
/// received them.
///
/// # Errors
/// Any transport-level error is returned. Parse errors are surfaced
/// as [`NetError::Parse`] and end the loop after sending a synthetic
/// error response when possible.
#[tracing::instrument(
    name = "client_loop",
    skip_all,
    fields(
        role = ?conn.role(),
        peer = tracing::field::Empty,
    ),
)]
pub async fn client_loop(
    mut conn: Conn,
    mut handler: ClientHandler,
    mut rx: mpsc::Receiver<OutboundEnvelope>,
) -> Result<(), NetError> {
    debug_assert!(matches!(
        conn.role(),
        ConnRole::Client | ConnRole::DnodePeerClient
    ));

    let mut read_buf = vec![0u8; CLIENT_READ_CHUNK];
    let mut accumulated: Vec<u8> = Vec::new();
    let mut pending_writes: Vec<u8> = Vec::new();

    loop {
        // Flush any buffered response bytes first so the loop
        // exit conditions never block on a full peer.
        if !pending_writes.is_empty() {
            let transport = conn.transport_mut().ok_or(NetError::Closed)?;
            transport.write_all(&pending_writes).await?;
            conn.record_send(pending_writes.len());
            pending_writes.clear();
        }

        if conn.is_eof() && conn.imsg_q().is_empty() && conn.omsg_q().is_empty() {
            conn.set_done();
            return Ok(());
        }

        let read_fut = async {
            let n = match conn.transport_mut() {
                Some(t) => t.read(&mut read_buf).await,
                None => return Ok::<usize, std::io::Error>(0),
            };
            n
        };

        tokio::select! {
            res = read_fut => {
                let n = res?;
                if n == 0 {
                    conn.set_eof();
                    if conn.omsg_q().is_empty() {
                        conn.set_done();
                        return Ok(());
                    }
                    continue;
                }
                conn.record_recv(n);
                accumulated.extend_from_slice(&read_buf[..n]);
                drive_parser(&mut conn, &mut handler, &mut accumulated).await?;
            }
            Some(env) = rx.recv() => {
                handle_response(&mut conn, &env, &mut pending_writes);
            }
            else => {
                conn.set_done();
                return Ok(());
            }
        }
    }
}

#[tracing::instrument(
    name = "client.parse_loop",
    skip_all,
    fields(accumulated = accumulated.len()),
)]
async fn drive_parser(
    conn: &mut Conn,
    handler: &mut ClientHandler,
    accumulated: &mut Vec<u8>,
) -> Result<(), NetError> {
    use crate::proto::memcache::memcache_parse_req;
    use crate::proto::redis::redis_parse_req;

    while !accumulated.is_empty() {
        let id = handler.alloc_msg_id();
        let mut msg = Msg::new(id, MsgType::Unknown, true);
        let consumed_before = msg.parser_pos();
        let parse_result = match handler.data_store {
            DataStore::Redis | DataStore::Noxu => redis_parse_req(&mut msg, accumulated),
            DataStore::Memcache => memcache_parse_req(&mut msg, accumulated),
        };
        match parse_result {
            MsgParseResult::Ok => {
                let consumed = msg.parser_pos();
                if consumed == 0 {
                    return Err(NetError::Parse(
                        "parser reported Ok with no bytes consumed".to_string(),
                    ));
                }
                // Per-request span - one is created for every
                // fully-parsed inbound message. Cross-task work
                // (dispatch.plan, backend.send / parse, peer.send /
                // parse, client.send) attaches as children via the
                // captured `tracing::Span` on the OutboundRequest /
                // OutboundEnvelope envelopes.
                let req_span = tracing::info_span!(
                    "client.parse",
                    msg_id = msg.id(),
                    msg_type = ?msg.ty(),
                    bytes = consumed,
                );
                let was_quit = msg.flags().quit;
                let quit_msg_id = if was_quit { Some(msg.id()) } else { None };
                let inline_send: Option<OutboundEnvelope> = req_span.in_scope(|| {
                    // Carry the consumed wire bytes inside the
                    // msg so the dispatcher can forward them to a
                    // backend without having to re-encode. The C
                    // engine keeps the request bytes in the
                    // inbound mbuf chain across recv -> filter
                    // -> forward; the Rust port stores them on
                    // the msg's own mbuf chain instead.
                    let pool = conn.mbuf_pool().clone();
                    let mut buf = pool.get();
                    buf.recv(&accumulated[..consumed]);
                    msg.mbufs_mut().push_back(buf);
                    msg.recompute_mlen();
                    accumulated.drain(0..consumed);
                    let _ = consumed_before;
                    conn.outstanding_mut().insert(msg.id(), msg.id());
                    // The placeholder enqueue cannot fail under
                    // normal operation; if it does we surface it
                    // via the outer `?`.
                    conn.enqueue_out(Msg::new(msg.id(), msg.ty(), true))
                        .map_err(|e: NetError| e)?;
                    let outcome = handler
                        .dispatcher
                        .dispatch(msg, handler.response_tx.clone());
                    let inline = match outcome {
                        DispatchOutcome::Pending | DispatchOutcome::Drop => None,
                        DispatchOutcome::Inline(rsp) | DispatchOutcome::Error(rsp) => {
                            Some(OutboundEnvelope {
                                req_id: rsp.id(),
                                rsp,
                                span: tracing::Span::current(),
                                source_peer_idx: None,
                            })
                        }
                    };
                    Ok::<Option<OutboundEnvelope>, NetError>(inline)
                })?;
                if let Some(env) = inline_send {
                    let _ = handler.response_tx.send(env).await;
                }
                if let Some(qid) = quit_msg_id {
                    // Real Redis replies `+OK\r\n` to QUIT and then
                    // closes the client connection. Synthesize the
                    // reply here (the dispatcher returned `Drop`
                    // because there is no key to route) and send it
                    // through the same `response_tx` used by every
                    // other reply, which pops the placeholder we
                    // pushed onto `omsg_q` above. Without this the
                    // outer client loop's exit condition
                    // (`omsg_q.is_empty()`) is never met and the
                    // connection deadlocks until the kernel times
                    // out the read.
                    let pool = conn.mbuf_pool().clone();
                    let mut anchor = Msg::new(qid, MsgType::ReqRedisQuit, true);
                    anchor.set_parent_id(qid);
                    let rsp = response::make_simple_redis(&anchor, &pool, b"+OK\r\n");
                    let env = OutboundEnvelope {
                        req_id: qid,
                        rsp,
                        span: req_span.clone(),
                        source_peer_idx: None,
                    };
                    let _ = handler.response_tx.send(env).await;
                    // Mirror the C engine: close after replying.
                    conn.set_eof();
                    return Ok(());
                }
            }
            MsgParseResult::Again
            | MsgParseResult::Repair
            | MsgParseResult::Fragment
            | MsgParseResult::Noop => {
                let consumed = msg.parser_pos();
                if consumed > 0 {
                    accumulated.drain(0..consumed);
                } else {
                    return Ok(());
                }
            }
            MsgParseResult::Error | MsgParseResult::OomError | MsgParseResult::DynoConfig => {
                return Err(NetError::Parse(format!("{parse_result:?}")));
            }
        }
    }
    Ok(())
}

fn handle_response(conn: &mut Conn, env: &OutboundEnvelope, pending: &mut Vec<u8>) {
    let _enter = env.span.enter();
    let bytes_len: usize = env.rsp.mbufs().iter().map(|b| b.readable().len()).sum();
    let _send_span =
        tracing::info_span!("client.send", req_id = env.req_id, bytes = bytes_len,).entered();
    for buf in env.rsp.mbufs() {
        pending.extend_from_slice(buf.readable());
    }
    // Pop the matching outstanding entry.
    conn.outstanding_mut().remove(&env.req_id);
    // Pop the placeholder we pushed on enqueue.
    if let Some(front) = conn.omsg_q_mut().front() {
        if front.id() == env.req_id {
            let _ = conn.omsg_q_mut().pop_front();
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn alloc_msg_id_is_monotonic() {
        let (tx, _rx) = mpsc::channel(1);
        let mut h = ClientHandler::new(Arc::new(crate::net::NoopDispatcher), tx, DataStore::Redis);
        let a = h.alloc_msg_id();
        let b = h.alloc_msg_id();
        assert_eq!(a + 1, b);
    }
}