dyniak 1.6.0

Riak-compatible protocol surface (HTTP + PBC) and storage bridge for the Dynomite Rust port
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
//! Wire codec for the cross-node XA phase messages.
//!
//! The cross-node [`crate::datastore::xa::XaCoordinator`] carries the
//! same prepare / vote / commit / rollback phases the local
//! coordinator already drives, but to a remote peer over the dnode
//! peer plane. Each phase travels as the payload of a dnode frame
//! whose [`dynomite::proto::dnode::DmsgType`] selects the phase
//! ([`XaPrepare`](dynomite::proto::dnode::DmsgType::XaPrepare),
//! [`XaVote`](dynomite::proto::dnode::DmsgType::XaVote),
//! [`XaCommit`](dynomite::proto::dnode::DmsgType::XaCommit),
//! [`XaRollback`](dynomite::proto::dnode::DmsgType::XaRollback),
//! [`XaAck`](dynomite::proto::dnode::DmsgType::XaAck)).
//!
//! The payload is a self-describing, length-prefixed byte stream
//! built with the standard library only (no external codec). Every
//! length is a big-endian `u32`; every byte string is a length
//! prefix followed by its bytes. The format is deliberately minimal:
//! a [`WireXid`] plus, for the prepare phase, the branch's writes.
//!
//! # Examples
//!
//! ```
//! use dyniak::datastore::xa_wire::{WireXid, XaPrepareMsg, XaWriteOp};
//!
//! let msg = XaPrepareMsg {
//!     xid: WireXid { format_id: 7, gtrid: b"g".to_vec(), bqual: b"east".to_vec() },
//!     env: b"east".to_vec(),
//!     writes: vec![XaWriteOp::Put {
//!         bucket: b"u".to_vec(),
//!         key: b"alice".to_vec(),
//!         value: b"a".to_vec(),
//!         indexes: vec![(b"age_int".to_vec(), b"42".to_vec())],
//!     }],
//! };
//! let bytes = msg.encode();
//! let back = XaPrepareMsg::decode(&bytes).expect("round trip");
//! assert_eq!(back, msg);
//! ```

use crate::txn::TxnOp;

/// Error raised when an XA wire payload is malformed or truncated.
#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)]
#[non_exhaustive]
pub enum XaWireError {
    /// The buffer ended before a declared field finished.
    #[error("xa wire payload truncated")]
    Truncated,
    /// A discriminator byte was outside its documented range.
    #[error("xa wire payload: unknown tag {0}")]
    BadTag(u8),
}

/// Portable form of a [`noxu::xa::Xid`] for the wire.
///
/// `noxu`'s `Xid` is not serialisable here without pulling the
/// engine type into the wire surface, so the codec carries the three
/// raw components and the receiver rebuilds the `Xid`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct WireXid {
    /// XA format identifier.
    pub format_id: i32,
    /// Global transaction id (at most 64 bytes).
    pub gtrid: Vec<u8>,
    /// Branch qualifier (at most 64 bytes).
    pub bqual: Vec<u8>,
}

/// One write applied by the receiving branch during prepare.
///
/// Mirrors [`TxnOp`] but is owned by the wire module so the codec
/// does not depend on the `TxnOp` serde derive (the local coordinator
/// path uses `TxnOp` directly; the remote path lowers to this).
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum XaWriteOp {
    /// Store an object, fanning `indexes` into the 2i layer.
    Put {
        /// Bucket name.
        bucket: Vec<u8>,
        /// Object key.
        key: Vec<u8>,
        /// Object value bytes.
        value: Vec<u8>,
        /// `(index_name, value)` 2i entries.
        indexes: Vec<(Vec<u8>, Vec<u8>)>,
    },
    /// Remove an object and its 2i entries.
    Delete {
        /// Bucket name.
        bucket: Vec<u8>,
        /// Object key.
        key: Vec<u8>,
    },
}

impl XaWriteOp {
    /// Lower a [`TxnOp`] into its wire form.
    #[must_use]
    pub fn from_txn_op(op: &TxnOp) -> Self {
        match op {
            TxnOp::Put {
                bucket,
                key,
                value,
                indexes,
            } => Self::Put {
                bucket: bucket.clone(),
                key: key.clone(),
                value: value.clone(),
                indexes: indexes.clone(),
            },
            TxnOp::Delete { bucket, key } => Self::Delete {
                bucket: bucket.clone(),
                key: key.clone(),
            },
        }
    }

    /// Raise the wire form back into a [`TxnOp`] the local apply path
    /// can replay.
    #[must_use]
    pub fn into_txn_op(self) -> TxnOp {
        match self {
            Self::Put {
                bucket,
                key,
                value,
                indexes,
            } => TxnOp::Put {
                bucket,
                key,
                value,
                indexes,
            },
            Self::Delete { bucket, key } => TxnOp::Delete { bucket, key },
        }
    }
}

/// Prepare-phase request: deliver a branch's writes and elicit a
/// vote. One round-trip carries start + apply + end + prepare.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct XaPrepareMsg {
    /// Transaction branch identifier.
    pub xid: WireXid,
    /// Name of the environment (resource manager) that owns the
    /// branch on the receiver.
    pub env: Vec<u8>,
    /// Writes to apply before voting.
    pub writes: Vec<XaWriteOp>,
}

/// Branch vote returned for an [`XaPrepareMsg`].
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum XaVote {
    /// Branch prepared durably and must be committed.
    Ok,
    /// Branch performed no writes; nothing to commit or roll back.
    ReadOnly,
    /// Branch could not prepare; the coordinator must roll back.
    Abort,
}

/// Commit / rollback request body: just the branch identifier.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct XaResolveMsg {
    /// Transaction branch identifier.
    pub xid: WireXid,
    /// Name of the environment that owns the branch.
    pub env: Vec<u8>,
}

/// Acknowledgement returned for a commit / rollback request.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct XaAckMsg {
    /// True when the resolution completed (or was already complete);
    /// false when the receiver could not resolve the branch.
    pub ok: bool,
}

// --- encoding helpers (big-endian, length-prefixed) ---------------

fn put_bytes(out: &mut Vec<u8>, b: &[u8]) {
    let len = u32::try_from(b.len()).unwrap_or(u32::MAX);
    out.extend_from_slice(&len.to_be_bytes());
    out.extend_from_slice(b);
}

fn put_xid(out: &mut Vec<u8>, xid: &WireXid) {
    out.extend_from_slice(&xid.format_id.to_be_bytes());
    put_bytes(out, &xid.gtrid);
    put_bytes(out, &xid.bqual);
}

struct Reader<'a> {
    buf: &'a [u8],
    pos: usize,
}

impl<'a> Reader<'a> {
    fn new(buf: &'a [u8]) -> Self {
        Self { buf, pos: 0 }
    }

    fn take(&mut self, n: usize) -> Result<&'a [u8], XaWireError> {
        let end = self.pos.checked_add(n).ok_or(XaWireError::Truncated)?;
        if end > self.buf.len() {
            return Err(XaWireError::Truncated);
        }
        let out = &self.buf[self.pos..end];
        self.pos = end;
        Ok(out)
    }

    fn u32(&mut self) -> Result<u32, XaWireError> {
        let b = self.take(4)?;
        Ok(u32::from_be_bytes([b[0], b[1], b[2], b[3]]))
    }

    fn i32(&mut self) -> Result<i32, XaWireError> {
        let b = self.take(4)?;
        Ok(i32::from_be_bytes([b[0], b[1], b[2], b[3]]))
    }

    fn u8(&mut self) -> Result<u8, XaWireError> {
        Ok(self.take(1)?[0])
    }

    fn bytes(&mut self) -> Result<Vec<u8>, XaWireError> {
        let len = self.u32()? as usize;
        Ok(self.take(len)?.to_vec())
    }

    fn xid(&mut self) -> Result<WireXid, XaWireError> {
        let format_id = self.i32()?;
        let gtrid = self.bytes()?;
        let bqual = self.bytes()?;
        Ok(WireXid {
            format_id,
            gtrid,
            bqual,
        })
    }
}

impl XaPrepareMsg {
    /// Serialise the prepare request to its wire payload.
    #[must_use]
    pub fn encode(&self) -> Vec<u8> {
        let mut out = Vec::with_capacity(64);
        put_xid(&mut out, &self.xid);
        put_bytes(&mut out, &self.env);
        let count = u32::try_from(self.writes.len()).unwrap_or(u32::MAX);
        out.extend_from_slice(&count.to_be_bytes());
        for w in &self.writes {
            match w {
                XaWriteOp::Put {
                    bucket,
                    key,
                    value,
                    indexes,
                } => {
                    out.push(0u8);
                    put_bytes(&mut out, bucket);
                    put_bytes(&mut out, key);
                    put_bytes(&mut out, value);
                    let icount = u32::try_from(indexes.len()).unwrap_or(u32::MAX);
                    out.extend_from_slice(&icount.to_be_bytes());
                    for (name, val) in indexes {
                        put_bytes(&mut out, name);
                        put_bytes(&mut out, val);
                    }
                }
                XaWriteOp::Delete { bucket, key } => {
                    out.push(1u8);
                    put_bytes(&mut out, bucket);
                    put_bytes(&mut out, key);
                }
            }
        }
        out
    }

    /// Parse a prepare request from its wire payload.
    ///
    /// # Errors
    ///
    /// [`XaWireError::Truncated`] when the buffer is short and
    /// [`XaWireError::BadTag`] when a write discriminator is out of
    /// range.
    pub fn decode(buf: &[u8]) -> Result<Self, XaWireError> {
        let mut r = Reader::new(buf);
        let xid = r.xid()?;
        let env = r.bytes()?;
        let count = r.u32()? as usize;
        let mut writes = Vec::with_capacity(count.min(1024));
        for _ in 0..count {
            let tag = r.u8()?;
            match tag {
                0 => {
                    let bucket = r.bytes()?;
                    let key = r.bytes()?;
                    let value = r.bytes()?;
                    let icount = r.u32()? as usize;
                    let mut indexes = Vec::with_capacity(icount.min(1024));
                    for _ in 0..icount {
                        let name = r.bytes()?;
                        let val = r.bytes()?;
                        indexes.push((name, val));
                    }
                    writes.push(XaWriteOp::Put {
                        bucket,
                        key,
                        value,
                        indexes,
                    });
                }
                1 => {
                    let bucket = r.bytes()?;
                    let key = r.bytes()?;
                    writes.push(XaWriteOp::Delete { bucket, key });
                }
                other => return Err(XaWireError::BadTag(other)),
            }
        }
        Ok(Self { xid, env, writes })
    }
}

impl XaVote {
    /// Encode the vote as a single tagged byte.
    #[must_use]
    pub fn encode(self) -> Vec<u8> {
        vec![match self {
            Self::Ok => 0,
            Self::ReadOnly => 1,
            Self::Abort => 2,
        }]
    }

    /// Decode a vote byte.
    ///
    /// # Errors
    ///
    /// [`XaWireError::Truncated`] on an empty buffer and
    /// [`XaWireError::BadTag`] on an out-of-range byte.
    pub fn decode(buf: &[u8]) -> Result<Self, XaWireError> {
        match buf.first() {
            Some(0) => Ok(Self::Ok),
            Some(1) => Ok(Self::ReadOnly),
            Some(2) => Ok(Self::Abort),
            Some(other) => Err(XaWireError::BadTag(*other)),
            None => Err(XaWireError::Truncated),
        }
    }
}

impl XaResolveMsg {
    /// Serialise the commit / rollback request.
    #[must_use]
    pub fn encode(&self) -> Vec<u8> {
        let mut out = Vec::with_capacity(32);
        put_xid(&mut out, &self.xid);
        put_bytes(&mut out, &self.env);
        out
    }

    /// Parse a commit / rollback request.
    ///
    /// # Errors
    ///
    /// [`XaWireError::Truncated`] when the buffer is short.
    pub fn decode(buf: &[u8]) -> Result<Self, XaWireError> {
        let mut r = Reader::new(buf);
        let xid = r.xid()?;
        let env = r.bytes()?;
        Ok(Self { xid, env })
    }
}

impl XaAckMsg {
    /// Encode the ack as a single boolean byte.
    #[must_use]
    pub fn encode(self) -> Vec<u8> {
        vec![u8::from(self.ok)]
    }

    /// Decode an ack byte.
    ///
    /// # Errors
    ///
    /// [`XaWireError::Truncated`] on an empty buffer.
    pub fn decode(buf: &[u8]) -> Result<Self, XaWireError> {
        match buf.first() {
            Some(0) => Ok(Self { ok: false }),
            Some(_) => Ok(Self { ok: true }),
            None => Err(XaWireError::Truncated),
        }
    }
}

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

    fn sample_xid() -> WireXid {
        WireXid {
            format_id: 0x6479_6e6b,
            gtrid: vec![0, 0, 0, 0, 0, 0, 0, 7],
            bqual: b"east".to_vec(),
        }
    }

    #[test]
    fn prepare_round_trips_with_mixed_writes() {
        let msg = XaPrepareMsg {
            xid: sample_xid(),
            env: b"east".to_vec(),
            writes: vec![
                XaWriteOp::Put {
                    bucket: b"u".to_vec(),
                    key: b"alice".to_vec(),
                    value: b"a".to_vec(),
                    indexes: vec![(b"age_int".to_vec(), b"42".to_vec())],
                },
                XaWriteOp::Delete {
                    bucket: b"u".to_vec(),
                    key: b"bob".to_vec(),
                },
            ],
        };
        let back = XaPrepareMsg::decode(&msg.encode()).expect("round trip");
        assert_eq!(back, msg);
    }

    #[test]
    fn vote_round_trips() {
        for v in [XaVote::Ok, XaVote::ReadOnly, XaVote::Abort] {
            assert_eq!(XaVote::decode(&v.encode()).unwrap(), v);
        }
        assert_eq!(XaVote::decode(&[]), Err(XaWireError::Truncated));
        assert_eq!(XaVote::decode(&[9]), Err(XaWireError::BadTag(9)));
    }

    #[test]
    fn resolve_round_trips() {
        let msg = XaResolveMsg {
            xid: sample_xid(),
            env: b"west".to_vec(),
        };
        let back = XaResolveMsg::decode(&msg.encode()).expect("round trip");
        assert_eq!(back, msg);
    }

    #[test]
    fn ack_round_trips() {
        for ok in [true, false] {
            assert_eq!(XaAckMsg::decode(&XaAckMsg { ok }.encode()).unwrap().ok, ok);
        }
        assert_eq!(XaAckMsg::decode(&[]), Err(XaWireError::Truncated));
    }

    #[test]
    fn truncated_prepare_is_rejected_not_panicked() {
        let full = XaPrepareMsg {
            xid: sample_xid(),
            env: b"east".to_vec(),
            writes: vec![XaWriteOp::Delete {
                bucket: b"u".to_vec(),
                key: b"bob".to_vec(),
            }],
        }
        .encode();
        // Every truncation point must produce an error, never a panic.
        for cut in 0..full.len() {
            assert!(XaPrepareMsg::decode(&full[..cut]).is_err());
        }
    }

    #[test]
    fn txn_op_lowering_round_trips() {
        let op = TxnOp::Put {
            bucket: b"u".to_vec(),
            key: b"k".to_vec(),
            value: b"v".to_vec(),
            indexes: vec![(b"i".to_vec(), b"1".to_vec())],
        };
        let wire = XaWriteOp::from_txn_op(&op);
        assert_eq!(wire.into_txn_op(), op);
    }
}