dyniak 2.0.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
//! Multi-key transaction surface for the Riak protocol layer.
//!
//! `dyniak` extends Riak's per-key, eventually-consistent model with
//! atomic multi-key transactions. A client groups several put and
//! delete operations into one [`TxnBatch`]; the backend applies all of
//! them or none of them. Riak itself has no equivalent: a multi-key
//! atomic write in Riak would need a PAXOS-style coordination layer.
//! The dyniak engine instead leans on the transactional storage engine
//! underneath it.
//!
//! Two layers stack on top of this module:
//!
//! * **Single-environment** (this module's [`TransactionalStore`]): all
//!   ops in a batch route to one node's storage engine and commit in a
//!   single engine transaction.
//! * **Cross-node** ([`crate::datastore::xa`], gated on the `noxu`
//!   feature): ops that span keys owned by different primary nodes are
//!   coordinated with X/Open XA two-phase commit.
//!
//! The wire-facing surfaces (HTTP `POST /transactions`, the PBC
//! `DynRpbTxn*` extension) decode a request into a [`TxnBatch`], hand it
//! to a [`TransactionalStore`], and encode the [`TxnOutcome`] back.
//!
//! # Examples
//!
//! ```
//! use dyniak::txn::{TxnBatch, TxnOp};
//!
//! let batch = TxnBatch {
//!     ops: vec![
//!         TxnOp::Put {
//!             bucket: b"users".to_vec(),
//!             key: b"alice".to_vec(),
//!             value: b"hello".to_vec(),
//!             indexes: vec![(b"age_int".to_vec(), b"42".to_vec())],
//!         },
//!         TxnOp::Delete {
//!             bucket: b"users".to_vec(),
//!             key: b"bob".to_vec(),
//!         },
//!     ],
//!     force_abort: false,
//! };
//! assert_eq!(batch.ops.len(), 2);
//! ```

use serde::{Deserialize, Serialize};

/// One operation in a multi-key transaction batch.
///
/// A batch is an ordered list of these; the backend replays them in
/// order inside a single engine transaction.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TxnOp {
    /// Insert or overwrite the object at `(bucket, key)`, fanning the
    /// supplied `indexes` into the 2i layer (same semantics as
    /// [`crate::datastore::NoxuDatastore::put_object`] when the `noxu`
    /// feature is enabled).
    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 the object at `(bucket, key)` and its 2i entries.
    Delete {
        /// Bucket name.
        bucket: Vec<u8>,
        /// Object key.
        key: Vec<u8>,
    },
}

impl TxnOp {
    /// Bucket the operation targets.
    #[must_use]
    pub fn bucket(&self) -> &[u8] {
        match self {
            Self::Put { bucket, .. } | Self::Delete { bucket, .. } => bucket,
        }
    }

    /// Key the operation targets.
    #[must_use]
    pub fn key(&self) -> &[u8] {
        match self {
            Self::Put { key, .. } | Self::Delete { key, .. } => key,
        }
    }
}

/// A batch of operations to apply atomically.
///
/// `force_abort` exists for clients (and tests) that want to exercise
/// the rollback path: when set, the backend applies every operation
/// inside the transaction and then deliberately aborts, leaving the
/// keyspace untouched and reporting [`TxnOutcome::Aborted`].
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct TxnBatch {
    /// Ordered list of operations.
    pub ops: Vec<TxnOp>,
    /// When true, apply every op and then roll back instead of
    /// committing.
    pub force_abort: bool,
}

/// Result of executing a [`TxnBatch`].
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TxnOutcome {
    /// The transaction committed; `operations` is the number of ops
    /// that were applied.
    Committed {
        /// Number of operations committed.
        operations: usize,
    },
    /// The transaction rolled back without changing the keyspace.
    Aborted {
        /// Human-readable abort reason.
        reason: String,
    },
}

/// Errors surfaced by a [`TransactionalStore`].
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum TxnStoreError {
    /// The storage engine rejected the transaction. The inner string
    /// carries the backend's own error message.
    #[error("transaction backend: {0}")]
    Backend(String),
    /// The batch contained no operations.
    #[error("empty transaction batch")]
    EmptyBatch,
    /// A conflict / serialization failure was reported by the engine.
    /// The transaction was rolled back; the caller may retry.
    #[error("transaction conflict: {0}")]
    Conflict(String),
}

/// A backend that can apply a [`TxnBatch`] atomically.
///
/// Implemented by [`crate::datastore::NoxuDatastore`] under the `noxu`
/// feature. The trait is object-safe so the HTTP and PBC servers can
/// hold an `Arc<dyn TransactionalStore>` without naming the concrete
/// backend.
pub trait TransactionalStore: Send + Sync {
    /// Apply `batch` atomically.
    ///
    /// # Errors
    ///
    /// Returns [`TxnStoreError::EmptyBatch`] for an empty batch,
    /// [`TxnStoreError::Conflict`] when the engine reported a
    /// serialization failure (the batch rolled back), or
    /// [`TxnStoreError::Backend`] for any other engine error.
    fn execute_batch(&self, batch: &TxnBatch) -> Result<TxnOutcome, TxnStoreError>;
}

// ------------------------------------------------------------------
// HTTP / JSON data-transfer objects.
// ------------------------------------------------------------------

/// JSON request body for `POST /transactions`.
///
/// ```json
/// {
///   "abort": false,
///   "operations": [
///     {"op": "put", "bucket": "users", "key": "alice", "value": "hi",
///      "indexes": [{"name": "age_int", "value": "42"}]},
///     {"op": "delete", "bucket": "users", "key": "bob"}
///   ]
/// }
/// ```
///
/// Values and index values are UTF-8 strings; binary payloads are not
/// representable through the JSON endpoint (use the PBC extension for
/// arbitrary bytes).
#[derive(Clone, Debug, Deserialize)]
pub struct HttpTxnRequest {
    /// When true the transaction is rolled back after applying every
    /// op (exercises the abort path).
    #[serde(default)]
    pub abort: bool,
    /// Ordered operation list.
    pub operations: Vec<HttpTxnOp>,
}

/// One JSON operation in an [`HttpTxnRequest`].
#[derive(Clone, Debug, Deserialize)]
#[serde(tag = "op", rename_all = "lowercase")]
pub enum HttpTxnOp {
    /// Store an object.
    Put {
        /// Bucket name.
        bucket: String,
        /// Object key.
        key: String,
        /// Object value (UTF-8).
        value: String,
        /// Optional 2i entries.
        #[serde(default)]
        indexes: Vec<HttpIndexEntry>,
    },
    /// Delete an object.
    Delete {
        /// Bucket name.
        bucket: String,
        /// Object key.
        key: String,
    },
}

/// One `(name, value)` 2i entry in an [`HttpTxnOp::Put`].
#[derive(Clone, Debug, Deserialize)]
pub struct HttpIndexEntry {
    /// Index name (for example `age_int`, `email_bin`).
    pub name: String,
    /// Index value (UTF-8).
    pub value: String,
}

/// JSON response body for `POST /transactions`.
#[derive(Clone, Debug, Serialize)]
pub struct HttpTxnResponse {
    /// Either `"committed"` or `"aborted"`.
    pub result: String,
    /// Number of operations committed (present on success).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub operations: Option<usize>,
    /// Abort reason (present when `result == "aborted"`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
}

impl HttpTxnRequest {
    /// Lower the JSON request into a [`TxnBatch`] the backend can run.
    #[must_use]
    pub fn into_batch(self) -> TxnBatch {
        let ops = self
            .operations
            .into_iter()
            .map(|op| match op {
                HttpTxnOp::Put {
                    bucket,
                    key,
                    value,
                    indexes,
                } => {
                    // Wrap the value in the same `HttpObject` storage
                    // envelope the non-transactional HTTP and PBC put
                    // paths write, so a transaction-written object is
                    // readable through every read path (HTTP GET and
                    // PBC RpbGet both decode the envelope). The 2i
                    // pairs are carried both inside the envelope (so
                    // they round-trip on read) and as the `TxnOp`
                    // index list (so the storage layer builds the
                    // forward / reverse 2i records).
                    let index_pairs: Vec<(Vec<u8>, Vec<u8>)> = indexes
                        .iter()
                        .map(|i| (i.name.clone().into_bytes(), i.value.clone().into_bytes()))
                        .collect();
                    let obj = crate::proto::http::object::HttpObject {
                        value: value.into_bytes(),
                        content_type: None,
                        indexes: indexes
                            .into_iter()
                            .map(|i| crate::proto::http::object::HttpIndex {
                                name: i.name,
                                value: i.value,
                            })
                            .collect(),
                        links: Vec::new(),
                        context: Vec::new(),
                        written_at_unix: crate::server::now_unix(),
                    };
                    TxnOp::Put {
                        bucket: bucket.into_bytes(),
                        key: key.into_bytes(),
                        value: obj.to_storage_bytes(),
                        indexes: index_pairs,
                    }
                }
                HttpTxnOp::Delete { bucket, key } => TxnOp::Delete {
                    bucket: bucket.into_bytes(),
                    key: key.into_bytes(),
                },
            })
            .collect();
        TxnBatch {
            ops,
            force_abort: self.abort,
        }
    }
}

impl HttpTxnResponse {
    /// Build a JSON response from a [`TxnOutcome`].
    #[must_use]
    pub fn from_outcome(outcome: &TxnOutcome) -> Self {
        match outcome {
            TxnOutcome::Committed { operations } => Self {
                result: "committed".to_string(),
                operations: Some(*operations),
                reason: None,
            },
            TxnOutcome::Aborted { reason } => Self {
                result: "aborted".to_string(),
                operations: None,
                reason: Some(reason.clone()),
            },
        }
    }
}

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

    #[test]
    fn http_request_lowers_to_batch() {
        let json = r#"{
            "operations": [
                {"op": "put", "bucket": "b", "key": "k", "value": "v",
                 "indexes": [{"name": "age_int", "value": "42"}]},
                {"op": "delete", "bucket": "b", "key": "old"}
            ]
        }"#;
        let req: HttpTxnRequest = serde_json::from_str(json).expect("decode");
        let batch = req.into_batch();
        assert!(!batch.force_abort);
        assert_eq!(batch.ops.len(), 2);
        // The Put op now carries the value wrapped in the canonical
        // HttpObject storage envelope (so a transaction-written
        // object reads back through the HTTP GET and PBC RpbGet
        // paths), plus the 2i pairs for the storage layer's
        // forward / reverse index fan-out.
        match &batch.ops[0] {
            TxnOp::Put {
                bucket,
                key,
                value,
                indexes,
            } => {
                assert_eq!(bucket, b"b");
                assert_eq!(key, b"k");
                assert_eq!(indexes, &vec![(b"age_int".to_vec(), b"42".to_vec())]);
                let obj = crate::proto::http::object::HttpObject::from_storage_bytes(value)
                    .expect("value is an HttpObject envelope");
                assert_eq!(obj.value, b"v".to_vec());
                assert_eq!(obj.indexes.len(), 1);
                assert_eq!(obj.indexes[0].name, "age_int");
                assert_eq!(obj.indexes[0].value, "42");
            }
            TxnOp::Delete { .. } => panic!("expected Put, got Delete"),
        }
        assert_eq!(
            batch.ops[1],
            TxnOp::Delete {
                bucket: b"b".to_vec(),
                key: b"old".to_vec(),
            }
        );
    }

    #[test]
    fn abort_flag_round_trips() {
        let json = r#"{"abort": true, "operations": []}"#;
        let req: HttpTxnRequest = serde_json::from_str(json).expect("decode");
        let batch = req.into_batch();
        assert!(batch.force_abort);
        assert!(batch.ops.is_empty());
    }

    #[test]
    fn response_from_outcome_committed() {
        let r = HttpTxnResponse::from_outcome(&TxnOutcome::Committed { operations: 3 });
        let s = serde_json::to_string(&r).expect("encode");
        assert!(s.contains("\"result\":\"committed\""));
        assert!(s.contains("\"operations\":3"));
        assert!(!s.contains("reason"));
    }

    #[test]
    fn response_from_outcome_aborted() {
        let r = HttpTxnResponse::from_outcome(&TxnOutcome::Aborted {
            reason: "client requested abort".to_string(),
        });
        let s = serde_json::to_string(&r).expect("encode");
        assert!(s.contains("\"result\":\"aborted\""));
        assert!(s.contains("client requested abort"));
        assert!(!s.contains("operations"));
    }

    #[test]
    fn txn_op_accessors() {
        let put = TxnOp::Put {
            bucket: b"bk".to_vec(),
            key: b"ky".to_vec(),
            value: b"v".to_vec(),
            indexes: vec![],
        };
        assert_eq!(put.bucket(), b"bk");
        assert_eq!(put.key(), b"ky");
    }
}