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
//! Read-Atomic Multi-Partition (RAMP) transactions.
//!
//! RAMP gives multi-key *read-atomic isolation* without locks and
//! without a reader ever blocking on a writer. A transaction either
//! sees ALL of another transaction's writes or NONE of them: no
//! *fractured read* where a reader observes transaction `T`'s write to
//! key `a` but misses `T`'s write to key `b`. This is a lower-latency,
//! availability-native complement to the heavyweight cross-node XA /
//! 2PC path in [`crate::datastore::xa`] for the common
//! "atomic multi-key read/write without full serializability" case.
//!
//! This module implements **RAMP-Fast** (Bailis et al., SIGMOD 2014):
//!
//! * **Writes are two-phase but non-blocking.** A write transaction
//! picks one monotonic timestamp `ts` for the whole batch. In the
//! PREPARE phase every item is written as a *versioned, invisible*
//! record keyed by `ts`, carrying metadata = the SET of sibling keys
//! the same transaction wrote. In the COMMIT phase each key's
//! *latest-visible pointer* is advanced to `ts`. A reader never waits
//! on a writer: it reads whatever pointer is currently visible.
//!
//! * **Reads are one round plus a conditional second round.** Round 1
//! fetches the latest-visible version and its metadata for every key.
//! The reader then checks: does any returned version's metadata name
//! a sibling key for which the reader saw an *older* version than the
//! sibling's transaction wrote? If so the first-round snapshot is
//! fractured; round 2 fetches exactly those missing versions by their
//! timestamp (which PREPARE guarantees is present even before it is
//! the visible one). The repaired snapshot is fracture-free. In the
//! common (contention-free) case round 1 already returns a
//! fracture-free snapshot and round 2 is skipped.
//!
//! The read-atomic decision logic -- what a reader keeps from round 1,
//! which siblings it must re-fetch in round 2, and how the repaired
//! snapshot is assembled -- lives in [`select`] as a pure,
//! side-effect-free core. The production coordinator
//! ([`RampCoordinator`](crate::RampCoordinator)) and the deterministic simulation model
//! (`crates/model-tests/src/ramp.rs`) both drive that same core, so the
//! model gates the real decision logic and not a re-imagining of it.
//!
//! # Scope
//!
//! This slice implements RAMP-Fast for the **single-node, local
//! multi-key** case: a transaction's keys all live in one process's
//! store, and the coordinator fans PREPARE / COMMIT / read rounds
//! across them in-process. The multi-partition wire fan-out over the
//! dnode peer plane (a [`dynomite::proto::dnode::DmsgType::RampPrepare`]
//! message analogous to the XA legs) is the documented next step; the
//! isolation algorithm itself -- fractured-read prevention -- is fully
//! implemented and gated here and does not change when the fan-out
//! moves cross-node, because RAMP's atomicity is a property of the
//! per-item versioning + metadata, not of where the items live.
//!
//! # Examples
//!
//! ```
//! use dyniak::ramp::{RampItem, select};
//!
//! // Round 1 saw key `a` at ts=5 (which names sibling `b`) and key
//! // `b` at ts=2 (an older, unrelated version). The reader must
//! // re-fetch `b` at ts=5 in round 2 to avoid a fractured read.
//! let a = RampItem::new(b"a".to_vec(), 5, vec![b"b".to_vec()], b"va".to_vec());
//! let b_old = RampItem::new(b"b".to_vec(), 2, vec![], b"vb-old".to_vec());
//! let round1 = vec![a, b_old];
//! let missing = select(&round1);
//! assert_eq!(missing, vec![(b"b".to_vec(), 5)]);
//! ```
use BTreeMap;
use ;
/// A monotonically increasing transaction timestamp.
///
/// RAMP requires timestamps to be unique per write transaction and
/// comparable across transactions; the low bits carry a per-coordinator
/// counter and the high bits a coordinator id so two coordinators never
/// mint the same value (see [`RampClock`]).
pub type Timestamp = u64;
/// One versioned item as returned by a read round.
///
/// A RAMP item is the value a single write transaction stored for one
/// key, tagged with that transaction's timestamp and the set of sibling
/// keys the same transaction wrote. The sibling set is the RAMP-Fast
/// metadata that drives the second-round repair.
/// The pure RAMP-Fast read-atomic core.
///
/// Given the versions a reader observed in round 1 (one [`RampItem`]
/// per key it is reading, the latest *visible* version of each), decide
/// which `(key, timestamp)` pairs the reader must fetch in round 2 to
/// guarantee a fracture-free snapshot.
///
/// The algorithm is exactly RAMP-Fast's: for every item `i` in the
/// round-1 set and every sibling `s` that `i`'s metadata names, if the
/// reader's current version of `s` has a timestamp *older* than `i.ts`,
/// then `i`'s transaction also wrote `s` at `i.ts` and the reader is
/// currently missing that write -- a fractured read. The reader must
/// upgrade `s` to `i.ts`. When several observed items name the same
/// sibling, the reader upgrades to the *highest* required timestamp
/// (the freshest transaction that the reader has already partially
/// observed), which subsumes the lower requirements.
///
/// Returns the `(key, ts)` pairs to fetch in round 2, sorted for
/// determinism. An empty result means round 1 was already
/// fracture-free and round 2 is skipped (the common case).
///
/// # Examples
///
/// ```
/// use dyniak::ramp::{RampItem, select};
///
/// // Fracture-free: `a` names `b`, and `b` is already at `a`'s ts.
/// let a = RampItem::new(b"a".to_vec(), 7, vec![b"b".to_vec()], b"va".to_vec());
/// let b = RampItem::new(b"b".to_vec(), 7, vec![b"a".to_vec()], b"vb".to_vec());
/// assert!(select(&[a, b]).is_empty());
/// ```
/// A per-coordinator monotonic timestamp source for RAMP writes.
///
/// The high 16 bits are the coordinator id and the low 48 bits a
/// strictly increasing counter, so two coordinators never mint the same
/// timestamp and each coordinator's timestamps are monotonic. That is
/// all RAMP-Fast needs from the clock: uniqueness and per-writer
/// monotonicity (it does not require a global total order, which is why
/// RAMP is AP-native and lock-free).