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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
//! In-memory representation of a single Dynomite message.
//!
//! [`Msg`] is the unit that flows through the engine: a request from
//! a client connection, a response from the upstream datastore, or
//! an internal control packet. It carries the parsed metadata, the
//! mbuf chain holding the on-the-wire bytes, the parser state used
//! by the protocol decoders, and the bookkeeping flags every layer
//! sets and reads.
//!
//! This stage builds the data shape and exposes the field-level
//! accessors. The connection-coupled lifecycle paths (timeout
//! tracking, queue threading, parser dispatch) land in Stage 9 once
//! the connection state machine exists.

use crate::core::types::MsgId;

use super::keypos::{ArgPos, KeyPos};
use super::msg_type::MsgType;
use super::response_mgr::ResponseMgr;
use crate::io::mbuf::MbufQueue;
use crate::proto::dnode::{Dmsg, DynParseState};

/// Stable connection identifier carried by [`Msg::owner`].
///
/// Stage 9 replaces this alias with a typed `Conn` reference. Until
/// then, the value is just a unique 64-bit tag the connection layer
/// stamps on every message it produces.
pub type ConnId = u64;

/// Parser outcome reported by datastore protocol decoders.
///
/// The variants mirror the reference engine's `msg_parse_result_t`
/// so downstream callers can dispatch on the same semantics.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
pub enum MsgParseResult {
    /// Parsing completed for this message.
    #[default]
    Ok,
    /// Parser detected unrecoverable framing error.
    Error,
    /// Parser consumed valid bytes but expects to be re-driven on
    /// the trailing bytes after a buffer split.
    Repair,
    /// Multi-key request needs to be fragmented before forwarding.
    Fragment,
    /// Need more bytes; caller must read more before retrying.
    Again,
    /// Parsing succeeded; downstream layer should take no action.
    Noop,
    /// Message was a Dynomite configuration directive.
    DynoConfig,
    /// Out-of-memory while parsing.
    OomError,
}

/// Routing override applied to a request.
///
/// Mirrors the reference engine's `msg_routing_t`. The default
/// (`Normal`) honors the configured key-hash routing; the other
/// variants short-circuit it for diagnostic and special-purpose
/// paths.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
#[repr(u8)]
pub enum MsgRouting {
    /// Apply the standard key-hash routing.
    #[default]
    Normal = 0,
    /// Send to the local node only, ignoring the key hash.
    LocalNodeOnly = 1,
    /// Apply the key hash but stay within the local rack.
    TokenOwnerLocalRackOnly = 2,
    /// Send to every node in the local rack, ignoring the key hash.
    AllNodesLocalRackOnly = 3,
    /// Send to every node in every rack of every datacenter.
    AllNodesAllRacksAllDcs = 4,
}

impl MsgRouting {
    /// Stable string name for diagnostic output.
    ///
    /// # Examples
    ///
    /// ```
    /// use dynomite::msg::MsgRouting;
    /// assert_eq!(MsgRouting::Normal.name(), "ROUTING_NORMAL");
    /// ```
    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            MsgRouting::Normal => "ROUTING_NORMAL",
            MsgRouting::LocalNodeOnly => "ROUTING_LOCAL_NODE_ONLY",
            MsgRouting::TokenOwnerLocalRackOnly => "ROUTING_TOKEN_OWNER_LOCAL_RACK_ONLY",
            MsgRouting::AllNodesLocalRackOnly => "ROUTING_ALL_NODES_LOCAL_RACK_ONLY",
            MsgRouting::AllNodesAllRacksAllDcs => "ROUTING_ALL_NODES_ALL_RACKS_ALL_DCS",
        }
    }
}

/// Bag of boolean lifecycle flags used by the request and response
/// pipelines.
#[allow(clippy::struct_excessive_bools)]
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct MsgFlags {
    /// Message is currently in the error state.
    pub is_error: bool,
    /// At least one fragment of this multi-message vector errored.
    pub is_ferror: bool,
    /// Caller issued a `quit` request.
    pub quit: bool,
    /// Caller expects the datastore to produce a reply.
    pub expect_datastore_reply: bool,
    /// Reply has been routed to the caller.
    pub done: bool,
    /// Every fragment of this vector finished.
    pub fdone: bool,
    /// Discard the corresponding response on arrival.
    pub swallow: bool,
    /// A DNODE header has already been prepended to the mbuf chain.
    pub dnode_header_prepended: bool,
    /// Response for this request has been written to the wire.
    pub rsp_sent: bool,
    /// Read request (vs write).
    pub is_read: bool,
    /// Marked by the dispatcher that a repair must be issued.
    pub needs_repair: bool,
    /// Request body can be safely rewritten with a timestamp.
    pub rewrite_with_ts_possible: bool,
    /// Response managers have been initialised.
    pub rspmgrs_inited: bool,
}

impl MsgFlags {
    /// Construct flags with the same default state the reference
    /// engine sets in `_msg_get`.
    #[must_use]
    fn default_for_msg() -> Self {
        Self {
            expect_datastore_reply: true,
            is_read: true,
            rewrite_with_ts_possible: true,
            ..Self::default()
        }
    }
}

/// One Dynomite message: the in-memory representation of a request
/// or a response on its way through the engine.
#[derive(Debug)]
pub struct Msg {
    id: MsgId,
    parent_id: MsgId,
    ty: MsgType,
    orig_type: MsgType,
    is_request: bool,
    mbufs: MbufQueue,
    mlen: u32,
    parse_result: MsgParseResult,
    dyn_parse_state: DynParseState,
    dmsg: Option<Dmsg>,
    routing: MsgRouting,
    consistency: super::ConsistencyLevel,
    timestamp_us: u64,
    error_code: i32,
    dyn_error_code: super::DynErrorCode,
    awaiting_rsps: u32,
    fragment_ids: Vec<MsgId>,
    response_ids: Vec<MsgId>,
    selected_rsp: Option<MsgId>,
    owner: Option<ConnId>,
    flags: MsgFlags,
    rspmgr: Option<ResponseMgr>,
    additional_rspmgrs: Vec<ResponseMgr>,
    parser_state: u32,
    parser_pos: usize,
    parser_token: Option<usize>,
    rlen: u32,
    rntokens: u32,
    ntokens: u32,
    nkeys: u32,
    vlen: u32,
    integer: i64,
    keys: Vec<KeyPos>,
    args: Vec<ArgPos>,
    end_marker: Option<usize>,
    ntoken_start: Option<usize>,
    ntoken_end: Option<usize>,
    frag_id: u64,
}

impl Msg {
    /// Construct a new message with `id`, type tag `ty`, and the
    /// request/response orientation `is_request`. The mbuf chain
    /// starts empty and the parser is reset to `DynParseState::Start`.
    ///
    /// # Examples
    ///
    /// ```
    /// use dynomite::msg::{Msg, MsgType};
    /// let m = Msg::new(1, MsgType::ReqRedisGet, true);
    /// assert_eq!(m.id(), 1);
    /// assert!(m.is_request());
    /// ```
    #[must_use]
    pub fn new(id: MsgId, ty: MsgType, is_request: bool) -> Self {
        Self {
            id,
            parent_id: 0,
            ty,
            orig_type: MsgType::Unknown,
            is_request,
            mbufs: MbufQueue::new(),
            mlen: 0,
            parse_result: MsgParseResult::default(),
            dyn_parse_state: DynParseState::Start,
            dmsg: None,
            routing: MsgRouting::Normal,
            consistency: super::ConsistencyLevel::DcOne,
            timestamp_us: 0,
            error_code: 0,
            dyn_error_code: super::DynErrorCode::Ok,
            awaiting_rsps: 0,
            fragment_ids: Vec::new(),
            response_ids: Vec::new(),
            selected_rsp: None,
            owner: None,
            flags: MsgFlags::default_for_msg(),
            rspmgr: None,
            additional_rspmgrs: Vec::new(),
            parser_state: 0,
            parser_pos: 0,
            parser_token: None,
            rlen: 0,
            rntokens: 0,
            ntokens: 0,
            nkeys: 0,
            vlen: 0,
            integer: 0,
            keys: Vec::new(),
            args: Vec::new(),
            end_marker: None,
            ntoken_start: None,
            ntoken_end: None,
            frag_id: 0,
        }
    }

    /// Borrow the parsed key list. Populated by the protocol parsers.
    ///
    /// # Examples
    /// ```
    /// use dynomite::msg::{Msg, MsgType};
    /// assert!(Msg::new(1, MsgType::Unknown, true).keys().is_empty());
    /// ```
    #[must_use]
    pub fn keys(&self) -> &[KeyPos] {
        &self.keys
    }

    /// Mutably borrow the parsed key list.
    pub fn keys_mut(&mut self) -> &mut Vec<KeyPos> {
        &mut self.keys
    }

    /// Append a parsed key. Used by the protocol parsers.
    pub fn push_key(&mut self, k: KeyPos) {
        self.keys.push(k);
    }

    /// Borrow the parsed argument list.
    #[must_use]
    pub fn args(&self) -> &[ArgPos] {
        &self.args
    }

    /// Mutably borrow the parsed argument list.
    pub fn args_mut(&mut self) -> &mut Vec<ArgPos> {
        &mut self.args
    }

    /// Append a parsed argument.
    pub fn push_arg(&mut self, a: ArgPos) {
        self.args.push(a);
    }

    /// Protocol-specific parser state index. Each parser defines
    /// its own state alphabet keyed on this `u32`.
    #[must_use]
    pub fn parser_state(&self) -> u32 {
        self.parser_state
    }

    /// Set the protocol parser state index.
    pub fn set_parser_state(&mut self, s: u32) {
        self.parser_state = s;
    }

    /// Cursor offset into the input buffer where the next byte
    /// should be read.
    #[must_use]
    pub fn parser_pos(&self) -> usize {
        self.parser_pos
    }

    /// Set the parser cursor offset.
    pub fn set_parser_pos(&mut self, p: usize) {
        self.parser_pos = p;
    }

    /// Optional token marker offset.
    #[must_use]
    pub fn parser_token(&self) -> Option<usize> {
        self.parser_token
    }

    /// Set the optional token marker offset.
    pub fn set_parser_token(&mut self, t: Option<usize>) {
        self.parser_token = t;
    }

    /// Remaining length of the bulk argument the parser is currently
    /// consuming.
    #[must_use]
    pub fn rlen(&self) -> u32 {
        self.rlen
    }

    /// Set the bulk-argument remaining length.
    pub fn set_rlen(&mut self, n: u32) {
        self.rlen = n;
    }

    /// Remaining unprocessed token count for the current parse.
    #[must_use]
    pub fn rntokens(&self) -> u32 {
        self.rntokens
    }

    /// Set the remaining-token counter.
    pub fn set_rntokens(&mut self, n: u32) {
        self.rntokens = n;
    }

    /// Total parsed token count for the current message.
    #[must_use]
    pub fn ntokens(&self) -> u32 {
        self.ntokens
    }

    /// Set the total parsed token count.
    pub fn set_ntokens(&mut self, n: u32) {
        self.ntokens = n;
    }

    /// Number of keys the script (EVAL/EVALSHA) declared.
    #[must_use]
    pub fn nkeys(&self) -> u32 {
        self.nkeys
    }

    /// Set the script-key count.
    pub fn set_nkeys(&mut self, n: u32) {
        self.nkeys = n;
    }

    /// Storage-command value length.
    #[must_use]
    pub fn vlen(&self) -> u32 {
        self.vlen
    }

    /// Set the storage-command value length.
    pub fn set_vlen(&mut self, n: u32) {
        self.vlen = n;
    }

    /// Integer value carried by the response (`:n\r\n`).
    #[must_use]
    pub fn integer(&self) -> i64 {
        self.integer
    }

    /// Set the integer response value.
    pub fn set_integer(&mut self, v: i64) {
        self.integer = v;
    }

    /// Offset of the multi-bulk `END` marker in the response, if any.
    #[must_use]
    pub fn end_marker(&self) -> Option<usize> {
        self.end_marker
    }

    /// Set the response `END` marker offset.
    pub fn set_end_marker(&mut self, m: Option<usize>) {
        self.end_marker = m;
    }

    /// Start offset of the multi-bulk argument count token.
    #[must_use]
    pub fn ntoken_start(&self) -> Option<usize> {
        self.ntoken_start
    }

    /// End offset (exclusive) of the multi-bulk argument count token.
    #[must_use]
    pub fn ntoken_end(&self) -> Option<usize> {
        self.ntoken_end
    }

    /// Set the multi-bulk argument count span.
    pub fn set_ntoken_span(&mut self, start: Option<usize>, end: Option<usize>) {
        self.ntoken_start = start;
        self.ntoken_end = end;
    }

    /// Fragment id grouping all sub-messages produced from a
    /// multi-key request.
    #[must_use]
    pub fn frag_id(&self) -> u64 {
        self.frag_id
    }

    /// Set the fragment id.
    pub fn set_frag_id(&mut self, id: u64) {
        self.frag_id = id;
    }

    /// Message id.
    ///
    /// # Examples
    /// ```
    /// use dynomite::msg::{Msg, MsgType};
    /// assert_eq!(Msg::new(99, MsgType::Unknown, true).id(), 99);
    /// ```
    #[must_use]
    pub fn id(&self) -> MsgId {
        self.id
    }

    /// Parent id (zero when not a fragment).
    ///
    /// # Examples
    /// ```
    /// use dynomite::msg::{Msg, MsgType};
    /// assert_eq!(Msg::new(1, MsgType::Unknown, true).parent_id(), 0);
    /// ```
    #[must_use]
    pub fn parent_id(&self) -> MsgId {
        self.parent_id
    }

    /// Set the parent id.
    ///
    /// # Examples
    /// ```
    /// use dynomite::msg::{Msg, MsgType};
    /// let mut m = Msg::new(2, MsgType::Unknown, true);
    /// m.set_parent_id(1);
    /// assert_eq!(m.parent_id(), 1);
    /// ```
    pub fn set_parent_id(&mut self, parent: MsgId) {
        self.parent_id = parent;
    }

    /// Message type tag.
    ///
    /// # Examples
    /// ```
    /// use dynomite::msg::{Msg, MsgType};
    /// assert_eq!(Msg::new(1, MsgType::ReqMcGet, true).ty(), MsgType::ReqMcGet);
    /// ```
    #[must_use]
    pub fn ty(&self) -> MsgType {
        self.ty
    }

    /// Override the message type. The previous value is preserved as
    /// the original type so query rewriters can recover it.
    ///
    /// # Examples
    /// ```
    /// use dynomite::msg::{Msg, MsgType};
    /// let mut m = Msg::new(1, MsgType::ReqRedisGet, true);
    /// m.set_type(MsgType::ReqRedisSet);
    /// assert_eq!(m.ty(), MsgType::ReqRedisSet);
    /// assert_eq!(m.orig_type(), MsgType::ReqRedisGet);
    /// ```
    pub fn set_type(&mut self, ty: MsgType) {
        self.orig_type = self.ty;
        self.ty = ty;
    }

    /// Original message type before any rewrite.
    ///
    /// # Examples
    /// ```
    /// use dynomite::msg::{Msg, MsgType};
    /// assert_eq!(
    ///     Msg::new(1, MsgType::ReqRedisGet, true).orig_type(),
    ///     MsgType::Unknown,
    /// );
    /// ```
    #[must_use]
    pub fn orig_type(&self) -> MsgType {
        self.orig_type
    }

    /// True for requests.
    ///
    /// # Examples
    /// ```
    /// use dynomite::msg::{Msg, MsgType};
    /// assert!(Msg::new(1, MsgType::ReqMcGet, true).is_request());
    /// assert!(!Msg::new(1, MsgType::RspMcStored, false).is_request());
    /// ```
    #[must_use]
    pub fn is_request(&self) -> bool {
        self.is_request
    }

    /// Borrow the underlying mbuf chain.
    ///
    /// # Examples
    /// ```
    /// use dynomite::msg::{Msg, MsgType};
    /// let m = Msg::new(1, MsgType::Unknown, true);
    /// assert!(m.mbufs().is_empty());
    /// ```
    #[must_use]
    pub fn mbufs(&self) -> &MbufQueue {
        &self.mbufs
    }

    /// Mutably borrow the mbuf chain.
    ///
    /// # Examples
    /// ```
    /// use dynomite::io::mbuf::MbufPool;
    /// use dynomite::msg::{Msg, MsgType};
    ///
    /// let pool = MbufPool::default();
    /// let mut m = Msg::new(1, MsgType::Unknown, true);
    /// m.mbufs_mut().push_back(pool.get());
    /// assert_eq!(m.mbufs().len(), 1);
    /// ```
    pub fn mbufs_mut(&mut self) -> &mut MbufQueue {
        &mut self.mbufs
    }

    /// Cumulative readable byte count of the chain (`mlen`).
    ///
    /// # Examples
    /// ```
    /// use dynomite::msg::{Msg, MsgType};
    /// assert_eq!(Msg::new(1, MsgType::Unknown, true).mlen(), 0);
    /// ```
    #[must_use]
    pub fn mlen(&self) -> u32 {
        self.mlen
    }

    /// Refresh `mlen` from the current chain.
    ///
    /// The parser updates the length as it consumes bytes; callers
    /// that mutate the chain directly call this to keep the cached
    /// length consistent with the actual chain content.
    ///
    /// # Examples
    /// ```
    /// use dynomite::io::mbuf::MbufPool;
    /// use dynomite::msg::{Msg, MsgType};
    ///
    /// let pool = MbufPool::default();
    /// let mut m = Msg::new(1, MsgType::Unknown, true);
    /// let mut buf = pool.get();
    /// buf.recv(b"hi");
    /// m.mbufs_mut().push_back(buf);
    /// m.recompute_mlen();
    /// assert_eq!(m.mlen(), 2);
    /// ```
    pub fn recompute_mlen(&mut self) {
        let total: usize = self.mbufs.iter().map(crate::io::mbuf::Mbuf::len).sum();
        self.mlen = u32::try_from(total).unwrap_or(u32::MAX);
    }

    /// Direct setter for `mlen`. Use [`Msg::recompute_mlen`] when the
    /// chain has been mutated; this entry point exists for parsers
    /// that adjust the value as they consume bytes.
    ///
    /// # Examples
    /// ```
    /// use dynomite::msg::{Msg, MsgType};
    /// let mut m = Msg::new(1, MsgType::Unknown, true);
    /// m.set_mlen(123);
    /// assert_eq!(m.mlen(), 123);
    /// ```
    pub fn set_mlen(&mut self, mlen: u32) {
        self.mlen = mlen;
    }

    /// Last parse outcome.
    ///
    /// # Examples
    /// ```
    /// use dynomite::msg::{Msg, MsgParseResult, MsgType};
    /// assert_eq!(
    ///     Msg::new(1, MsgType::Unknown, true).parse_result(),
    ///     MsgParseResult::Ok,
    /// );
    /// ```
    #[must_use]
    pub fn parse_result(&self) -> MsgParseResult {
        self.parse_result
    }

    /// Set the parse outcome.
    ///
    /// # Examples
    /// ```
    /// use dynomite::msg::{Msg, MsgParseResult, MsgType};
    /// let mut m = Msg::new(1, MsgType::Unknown, true);
    /// m.set_parse_result(MsgParseResult::Again);
    /// assert_eq!(m.parse_result(), MsgParseResult::Again);
    /// ```
    pub fn set_parse_result(&mut self, r: MsgParseResult) {
        self.parse_result = r;
    }

    /// Current DNODE parser state.
    ///
    /// # Examples
    /// ```
    /// use dynomite::msg::{Msg, MsgType};
    /// use dynomite::proto::dnode::DynParseState;
    /// assert_eq!(
    ///     Msg::new(1, MsgType::Unknown, true).dyn_parse_state(),
    ///     DynParseState::Start,
    /// );
    /// ```
    #[must_use]
    pub fn dyn_parse_state(&self) -> DynParseState {
        self.dyn_parse_state
    }

    /// Set the DNODE parser state.
    pub fn set_dyn_parse_state(&mut self, state: DynParseState) {
        self.dyn_parse_state = state;
    }

    /// Borrow the parsed DNODE header, if any.
    ///
    /// # Examples
    /// ```
    /// use dynomite::msg::{Msg, MsgType};
    /// assert!(Msg::new(1, MsgType::Unknown, true).dmsg().is_none());
    /// ```
    #[must_use]
    pub fn dmsg(&self) -> Option<&Dmsg> {
        self.dmsg.as_ref()
    }

    /// Mutably borrow the parsed DNODE header.
    pub fn dmsg_mut(&mut self) -> Option<&mut Dmsg> {
        self.dmsg.as_mut()
    }

    /// Attach a parsed DNODE header.
    pub fn set_dmsg(&mut self, dmsg: Dmsg) {
        self.dmsg = Some(dmsg);
    }

    /// Routing override.
    #[must_use]
    pub fn routing(&self) -> MsgRouting {
        self.routing
    }

    /// Set the routing override.
    pub fn set_routing(&mut self, routing: MsgRouting) {
        self.routing = routing;
    }

    /// Consistency level for this message.
    #[must_use]
    pub fn consistency(&self) -> super::ConsistencyLevel {
        self.consistency
    }

    /// Set the consistency level.
    pub fn set_consistency(&mut self, level: super::ConsistencyLevel) {
        self.consistency = level;
    }

    /// Microsecond timestamp recorded at request creation.
    #[must_use]
    pub fn timestamp_us(&self) -> u64 {
        self.timestamp_us
    }

    /// Update the request timestamp.
    pub fn set_timestamp_us(&mut self, ts: u64) {
        self.timestamp_us = ts;
    }

    /// Datastore-level error code (`errno`-shaped).
    #[must_use]
    pub fn error_code(&self) -> i32 {
        self.error_code
    }

    /// Set the datastore error code.
    pub fn set_error_code(&mut self, e: i32) {
        self.error_code = e;
    }

    /// Dynomite-level error code.
    #[must_use]
    pub fn dyn_error_code(&self) -> super::DynErrorCode {
        self.dyn_error_code
    }

    /// Set the Dynomite error code.
    pub fn set_dyn_error_code(&mut self, e: super::DynErrorCode) {
        self.dyn_error_code = e;
    }

    /// Number of replies the request is still waiting on.
    #[must_use]
    pub fn awaiting_rsps(&self) -> u32 {
        self.awaiting_rsps
    }

    /// Increment `awaiting_rsps`.
    pub fn incr_awaiting_rsps(&mut self) {
        self.awaiting_rsps = self.awaiting_rsps.saturating_add(1);
    }

    /// Decrement `awaiting_rsps`.
    pub fn decr_awaiting_rsps(&mut self) {
        self.awaiting_rsps = self.awaiting_rsps.saturating_sub(1);
    }

    /// Set `awaiting_rsps` directly. Used by the response manager
    /// initialiser to seed the per-DC count.
    pub fn set_awaiting_rsps(&mut self, n: u32) {
        self.awaiting_rsps = n;
    }

    /// Borrow the fragment-id list.
    #[must_use]
    pub fn fragment_ids(&self) -> &[MsgId] {
        &self.fragment_ids
    }

    /// Append `id` to the fragment-id list.
    pub fn push_fragment_id(&mut self, id: MsgId) {
        self.fragment_ids.push(id);
    }

    /// Borrow the response-id list.
    #[must_use]
    pub fn response_ids(&self) -> &[MsgId] {
        &self.response_ids
    }

    /// Append `id` to the response-id list.
    pub fn push_response_id(&mut self, id: MsgId) {
        self.response_ids.push(id);
    }

    /// Currently-selected response id.
    #[must_use]
    pub fn selected_rsp(&self) -> Option<MsgId> {
        self.selected_rsp
    }

    /// Set the currently-selected response id.
    pub fn set_selected_rsp(&mut self, id: Option<MsgId>) {
        self.selected_rsp = id;
    }

    /// Owner connection id (placeholder until Stage 9).
    #[must_use]
    pub fn owner(&self) -> Option<ConnId> {
        self.owner
    }

    /// Set the owner connection id.
    pub fn set_owner(&mut self, owner: Option<ConnId>) {
        self.owner = owner;
    }

    /// Borrow the lifecycle flags.
    #[must_use]
    pub fn flags(&self) -> &MsgFlags {
        &self.flags
    }

    /// Mutably borrow the lifecycle flags.
    pub fn flags_mut(&mut self) -> &mut MsgFlags {
        &mut self.flags
    }

    /// Set the `swallow` flag.
    pub fn set_swallow(&mut self, on: bool) {
        self.flags.swallow = on;
    }

    /// Set the `done` flag.
    pub fn set_done(&mut self, on: bool) {
        self.flags.done = on;
    }

    /// Set the `is_error` flag.
    pub fn set_is_error(&mut self, on: bool) {
        self.flags.is_error = on;
    }

    /// Borrow the local-DC response manager.
    #[must_use]
    pub fn rspmgr(&self) -> Option<&ResponseMgr> {
        self.rspmgr.as_ref()
    }

    /// Mutably borrow the local-DC response manager.
    pub fn rspmgr_mut(&mut self) -> Option<&mut ResponseMgr> {
        self.rspmgr.as_mut()
    }

    /// Install a fresh response manager for the local DC.
    pub fn set_rspmgr(&mut self, mgr: ResponseMgr) {
        self.flags.rspmgrs_inited = true;
        self.set_awaiting_rsps(u32::from(mgr.max_responses()));
        self.rspmgr = Some(mgr);
    }

    /// Borrow the per-remote-DC response managers.
    #[must_use]
    pub fn additional_rspmgrs(&self) -> &[ResponseMgr] {
        &self.additional_rspmgrs
    }

    /// Mutably borrow the per-remote-DC response managers.
    pub fn additional_rspmgrs_mut(&mut self) -> &mut Vec<ResponseMgr> {
        &mut self.additional_rspmgrs
    }
}

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

    #[test]
    fn defaults_match_reference() {
        let m = Msg::new(1, MsgType::ReqRedisGet, true);
        assert!(m.flags().expect_datastore_reply);
        assert!(m.flags().is_read);
        assert!(m.flags().rewrite_with_ts_possible);
        assert!(!m.flags().is_error);
        assert!(!m.flags().rspmgrs_inited);
        assert_eq!(m.consistency(), super::super::ConsistencyLevel::DcOne);
        assert_eq!(m.routing(), MsgRouting::Normal);
        assert_eq!(m.dyn_parse_state(), DynParseState::Start);
    }

    #[test]
    fn set_type_preserves_original() {
        let mut m = Msg::new(1, MsgType::ReqRedisGet, true);
        assert_eq!(m.orig_type(), MsgType::Unknown);
        m.set_type(MsgType::ReqRedisSet);
        assert_eq!(m.ty(), MsgType::ReqRedisSet);
        assert_eq!(m.orig_type(), MsgType::ReqRedisGet);
    }

    #[test]
    fn awaiting_saturates() {
        let mut m = Msg::new(1, MsgType::ReqRedisGet, true);
        m.decr_awaiting_rsps();
        assert_eq!(m.awaiting_rsps(), 0);
        m.incr_awaiting_rsps();
        m.incr_awaiting_rsps();
        assert_eq!(m.awaiting_rsps(), 2);
        m.decr_awaiting_rsps();
        assert_eq!(m.awaiting_rsps(), 1);
    }
}