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
//! Checking [linearizability](https://en.wikipedia.org/wiki/Linearizability) of a
//! history of operations applied to a shared object.
//!
//! For more information, see the documentation of the [`WGLChecker`] and [`History`] structs.
use HashSet;
use PhantomData;
use crate;
use crateSpecification;
/// A linearizability checker.
///
/// An implementation of the algorithm originally defined by Jeannette Wing and Chun Gong
/// [\[WG93\]](https://www.cs.cmu.edu/~wing/publications/WingGong93.pdf), and
/// extended by Gavin Lowe [\[L17\]](http://www.cs.ox.ac.uk/people/gavin.lowe/LinearizabiltyTesting/).
/// This particular implementation is based on the description given by Alex Horn
/// and Daniel Kroenig [\[HK15\]](https://arxiv.org/abs/1504.00204).
///
/// Given a history of operations, the algorithm works by linearizing each operation
/// as soon as possible. When an operation cannot be linearized, it backtracks and
/// proceeds with the next operation. Memoization occurs by caching each partial
/// linearization, and preventing the algorithm from continuing its search when it
/// is already known that the state of the object and remaining operations have no
/// valid linearization.
///
///
/// # Examples
///
/// Consider the following [`Specification`] of a register containing `u32` values.
///
/// ```
/// use todc_utils::specifications::Specification;
///
/// #[derive(Copy, Clone, Debug)]
/// enum RegisterOp {
/// Read(Option<u32>),
/// Write(u32),
/// }
///
/// use RegisterOp::{Read, Write};
///
/// struct RegisterSpec;
///
/// impl Specification for RegisterSpec {
/// type State = u32;
/// type Operation = RegisterOp;
///
/// fn init() -> Self::State {
/// 0
/// }
///
/// fn apply(operation: &Self::Operation, state: &Self::State) -> (bool, Self::State) {
/// match operation {
/// // A read is valid if the value returned is equal to the
/// // current state. Reads always leave the state unchanged.
/// Read(value) => match value {
/// Some(value) => (value == state, *state),
/// None => (false, *state)
/// },
/// // Writes are always valid, and update the state to be
/// // equal to the value being written.
/// Write(value) => (true, *value),
/// }
/// }
/// }
/// ```
///
/// Using the [`Action::Call`](history::Action::Call) and
/// [`Action::Response`](history::Action::Response) types, we can model read
/// and write operations as follows:
/// * The call of a read operation is modeled by `Call(Read(None))` and a
/// response containing the value `x` is modeled by `Response(Read(Some(x)))`.
/// We are required to use an [`Option`] because the value being read cannot
/// be known until the register responds.
/// * Similarily, the call of a write operation with the value `y` is modeled
/// by `Call(Write(y))` and the response is modeled by `Response(Write(y))`.
///
/// Next, we can define a linearizability for this specification, and check some
/// histories.
/// ```
/// # use todc_utils::specifications::Specification;
/// # #[derive(Copy, Clone, Debug)]
/// # enum RegisterOp {
/// # Read(Option<u32>),
/// # Write(u32),
/// # }
/// # use RegisterOp::{Read, Write};
/// # struct RegisterSpec;
/// # impl Specification for RegisterSpec {
/// # type State = u32;
/// # type Operation = RegisterOp;
/// # fn init() -> Self::State {
/// # 0
/// # }
/// # fn apply(operation: &Self::Operation, state: &Self::State) -> (bool, Self::State) {
/// # match operation {
/// # // A read is valid if the value returned is equal to the
/// # // current state. Reads always leave the state unchanged.
/// # Read(value) => match value {
/// # Some(value) => (value == state, *state),
/// # None => (false, *state)
/// # },
/// # // Writes are always valid, and update the state to be
/// # // equal to the value being written.
/// # Write(value) => (true, *value),
/// # }
/// # }
/// # }
/// use todc_utils::linearizability::{WGLChecker, history::{History, Action::{Call, Response}}};
///
/// type RegisterChecker = WGLChecker<RegisterSpec>;
///
/// // A history of sequantial operations is always linearizable.
/// // PO |------| Write(0)
/// // P1 |------| Read(Some(0))
/// let history = History::from_actions(vec![
/// (0, Call(Write(0))),
/// (0, Response(Write(0))),
/// (1, Call(Read(None))),
/// (1, Response(Read(Some(0)))),
/// ]);
/// assert!(RegisterChecker::is_linearizable(history));
///
/// // Concurrent operations might not be linearized
/// // in the order in which they are called.
/// // PO |--------------| Write(0)
/// // P1 |--------------| Write(1)
/// // P2 |---| Read(Some(1))
/// // P3 |---| Read(Some(0))
/// let history = History::from_actions(vec![
/// (0, Call(Write(0))),
/// (1, Call(Write(1))),
/// (2, Call(Read(None))),
/// (2, Response(Read(Some(1)))),
/// (3, Call(Read(None))),
/// (3, Response(Read(Some(0)))),
/// (0, Response(Write(0))),
/// (1, Response(Write(1))),
/// ]);
/// assert!(RegisterChecker::is_linearizable(history));
///
/// // A sequentially consistent history is **not**
/// // necessarily linearizable.
/// // PO |---| Write(0)
/// // P1 |---| Write(1)
/// // P2 |---| Read(Some(1))
/// // P3 |---| Read(Some(0))
/// let history = History::from_actions(vec![
/// (0, Call(Write(0))),
/// (1, Call(Write(1))),
/// (0, Response(Write(0))),
/// (1, Response(Write(1))),
/// (2, Call(Read(None))),
/// (2, Response(Read(Some(1)))),
/// (3, Call(Read(None))),
/// (3, Response(Read(Some(0)))),
/// ]);
/// assert!(!RegisterChecker::is_linearizable(history));
/// ```
///
/// For examples of using [`WGLChecker`] to check the linearizability of more
/// complex histories, see
/// [`todc-mem/tests/snapshot/common.rs`](https://github.com/kaymanb/todc/blob/main/todc-mem/tests/snapshot/common.rs)
/// or
/// [`todc-utils/tests/linearizability/etcd.rs`](https://github.com/kaymanb/todc/blob/main/todc-utils/tests/linearizability/etcd.rs).
///
/// # Implementations in Other Languages
///
/// For an implementation in C++, see [`linearizability-checker`](https://github.com/ahorn/linearizability-checker).
/// For an implementation in Go, see [`porcupine`](https://github.com/anishathalye/porcupine).
type OperationEntry<S> = ;
type OperationCall<S> = ;