todc-utils 0.1.1

Utilities for building and testing distributed systems.
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
//! A sequence of operations applied to a shared object.
use std::collections::VecDeque;
use std::iter::repeat_with;
use std::ops::{Index, IndexMut};

/// A identifier for an [`Entry`]
pub type EntryId = usize;

/// A process identifier.
pub type ProcessId = usize;

/// An action that occurs as part of an operation on a shared object.
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum Action<T> {
    /// A `Call` indicates the beginning of an operation.
    Call(T),
    /// A `Response` indicates the end of an operation.
    Response(T),
}

/// An entry in a history that represents the call to an operation.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct CallEntry<T> {
    /// The identifier for this [`CallEntry`].
    pub id: EntryId,
    /// The operation being called.
    pub operation: T,
    /// The identifier of the [`ResponseEntry`] that stores the response to this
    /// operation.
    pub response: EntryId,
}

/// An entry in a history that represents the response from an operation.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ResponseEntry<T> {
    /// The identifier for this [`ResponseEntry`].
    pub id: EntryId,
    /// The operation being responded to.
    pub operation: T,
}

/// An entry in a history.
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum Entry<T> {
    Call(CallEntry<T>),
    Response(ResponseEntry<T>),
}

impl<T> Entry<T> {
    /// Returns a unique identifier for this [`Entry`].
    pub fn id(&self) -> EntryId {
        match self {
            Entry::Call(entry) => entry.id,
            Entry::Response(entry) => entry.id,
        }
    }
}

/// A sequence of operations applied to a shared object.
///
/// A history is a sequence of operations that have been applied to a shared
/// object. Each [`Entry`] in the history indicates either a call to or response
/// from an operation performed by a specific process. It is possible for
/// operations from different processes to be performed concurrently, which
/// is modeled in a history by interleaving the call and response entries
/// for those operations.
///
/// # Examples
///
/// Consider a history of operations performed on a shared register, where
/// processes can write values and read values that have been written. In
/// the following example process `P0` performs a write, after which process
/// `P1` performs a read.
///
/// ```
/// use std::matches;
/// use todc_utils::{History, Action::{Call, Response}};
/// use todc_utils::linearizability::history::Entry;
/// use todc_utils::specifications::register::RegisterOperation::{Read, Write};
///
/// // P0 |--------|            Write("Hello, World!")
/// // P1            |--------| Read("Hello, World!")
/// let actions = vec![
///     (0, Call(Write("Hello, World!"))),
///     (0, Response(Write("World, World!"))),
///     (1, Call(Read(None))),
///     (1, Response(Read(Some("Hello, World!")))),
/// ];
///
/// let history = History::from_actions(actions);
/// assert!(matches!(&history[0], Entry::Call(x)));
/// ```
///
/// In the next example processes `P0`, `P1`, and `P2` each perform
/// a write while `P3` performs three reads. Notice that the reads performed by
/// `P3` occur concurrently with the writes performed by other processes.
///
/// ```
/// # use std::matches;
/// # use todc_utils::{History, Action::{Call, Response}};
/// # use todc_utils::linearizability::history::Entry;
/// # use todc_utils::specifications::register::RegisterOperation::{Read, Write};
/// // P0 |--------------------| Write(0)
/// // P1 |--------------------| Write(1)
/// // P2 |--------------------| Write(2)
/// // P3   |--|                 Read(2)
/// // P3          |--|          Read(1)
/// // P3                 |--|   Read(0)
/// let actions = vec![
///     (0, Call(Write(0))),
///     (1, Call(Write(1))),
///     (2, Call(Write(2))),
///     (3, Call(Read(None))),
///     (3, Response(Read(Some(2)))),
///     (3, Call(Read(None))),
///     (3, Response(Read(Some(1)))),
///     (3, Call(Read(None))),
///     (3, Response(Read(Some(0)))),
///     (0, Response(Write(0))),
///     (1, Response(Write(1))),
///     (2, Response(Write(2))),
/// ];
///
/// let history = History::from_actions(actions);
/// assert!(matches!(&history[0], Entry::Call(x)));
/// ```
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct History<T> {
    pub(super) entries: Vec<Entry<T>>,
    // When an entry is removed from this history, its index is recorded here.
    removed_from: Vec<Option<EntryId>>,
}

impl<T> History<T> {
    /// Creates a history from a sequence of actions.
    ///
    /// # Panics
    ///
    /// Panics if `actions` is empty.
    ///
    /// Panics if the resulting history would be incomplete. That is, if there is some
    /// `Call` action that does not have a corresponding `Response`.
    ///
    /// ```should_panic
    /// # use std::matches;
    /// # use todc_utils::{History, Action::{Call, Response}};
    /// # use todc_utils::specifications::register::RegisterOperation::{Write};
    /// let incomplete_actions = vec![
    ///     (0, Call(Write("Hello"))),
    ///     (1, Call(Write("World"))),
    ///     (0, Response(Write("Hello"))),
    ///     // <-- Missing response to the call by process 1!
    /// ];
    ///
    /// let history = History::from_actions(incomplete_actions);
    /// ```
    pub fn from_actions(actions: Vec<(ProcessId, Action<T>)>) -> Self {
        let (processes, actions): (Vec<ProcessId>, Vec<Action<T>>) = actions.into_iter().unzip();

        let num_processes = processes.iter().max().unwrap();
        let mut calls: Vec<VecDeque<usize>> = repeat_with(VecDeque::new)
            .take(*num_processes + 1)
            .collect();
        let mut responses = calls.clone();
        for (i, process) in processes.iter().enumerate() {
            match &actions[i] {
                Action::Call(_) => calls[*process].push_back(i),
                Action::Response(_) => responses[*process].push_back(i),
            }
        }

        Self {
            entries: actions
                .into_iter()
                .enumerate()
                .map(|(i, action)| match action {
                    Action::Call(operation) => Entry::Call(CallEntry {
                        id: i,
                        operation,
                        response: responses[processes[i]].pop_front().unwrap(),
                    }),
                    Action::Response(operation) => {
                        Entry::Response(ResponseEntry { id: i, operation })
                    }
                })
                .collect(),
            removed_from: repeat_with(|| None).take(processes.len()).collect(),
        }
    }

    // TODO: This operation is very expensive. Implementing History as a doubly-linked list could
    // greatly improve performance.
    pub(super) fn index_of_id(&self, id: EntryId) -> usize {
        self.iter().position(|e| e.id() == id).unwrap()
    }

    /// # Panics
    ///
    /// Panics if input entry was not previously removed from the history.
    fn insert(&mut self, entry: Entry<T>) -> usize {
        match self.removed_from[entry.id()].take() {
            Some(index) => {
                self.entries.insert(index, entry);
                index
            }
            None => panic!(
                "Index that entry {} was removed from is unknown",
                entry.id()
            ),
        }
    }

    pub(super) fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    pub(super) fn iter(&self) -> impl Iterator<Item = &Entry<T>> {
        self.entries.iter()
    }

    pub(super) fn len(&self) -> usize {
        self.entries.len()
    }

    pub(super) fn lift(&mut self, i: usize) -> (Entry<T>, Entry<T>) {
        match self.remove(i) {
            Entry::Response(_) => panic!("Cannot lift a response entry out of the history"),
            Entry::Call(call) => {
                let response = self.remove(self.index_of_id(call.response));
                (Entry::Call(call), response)
            }
        }
    }

    fn remove(&mut self, i: usize) -> Entry<T> {
        let entry = self.entries.remove(i);
        self.removed_from[entry.id()] = Some(i);
        entry
    }

    pub(super) fn unlift(&mut self, call: Entry<T>, response: Entry<T>) -> (usize, usize) {
        let response_index = self.insert(response);
        let call_index = self.insert(call);
        (call_index, response_index)
    }
}

impl<T> Index<usize> for History<T> {
    type Output = Entry<T>;

    fn index(&self, i: usize) -> &Self::Output {
        self.entries.index(i)
    }
}

impl<T> IndexMut<usize> for History<T> {
    fn index_mut(&mut self, i: usize) -> &mut Self::Output {
        self.entries.index_mut(i)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::iter::zip;
    use Action::{Call, Response};

    mod from_actions {
        use super::*;

        #[test]
        fn creates_sequential_ids() {
            let history = History::from_actions(vec![
                (0, Call("a")),
                (0, Response("a")),
                (0, Call("b")),
                (0, Response("b")),
            ]);
            for (i, entry) in history.iter().enumerate() {
                assert_eq!(entry.id(), i);
            }
        }

        #[test]
        fn links_actions_of_multiple_processes() {
            let history = History::from_actions(vec![
                (0, Call("a")),
                (1, Call("b")),
                (2, Call("c")),
                (0, Response("a")),
                (1, Response("b")),
                (2, Response("c")),
                (0, Call("e")),
                (1, Call("f")),
                (2, Call("g")),
                (0, Response("e")),
                (1, Response("f")),
                (2, Response("g")),
            ]);
            for entry in history.iter() {
                println!("{:?}", entry);
                if let Entry::Call(call) = entry {
                    match &history[history.index_of_id(call.response)] {
                        Entry::Response(response) => assert_eq!(call.operation, response.operation),
                        Entry::Call(_) => panic!("Call entry was linked to another call entry"),
                    }
                }
            }
        }

        #[test]
        fn links_actions_of_single_process() {
            let history = History::from_actions(vec![
                (0, Call("a")),
                (0, Response("a")),
                (0, Call("b")),
                (0, Response("b")),
                (0, Call("c")),
                (0, Response("c")),
            ]);
            for entry in history.iter() {
                if let Entry::Call(call) = entry {
                    match &history[history.index_of_id(call.response)] {
                        Entry::Response(response) => assert_eq!(call.operation, response.operation),
                        Entry::Call(_) => panic!("Call entry was linked to another call entry"),
                    }
                }
            }
        }
    }

    mod insert {
        use super::*;

        #[test]
        fn is_inverse_of_remove() {
            let history = History::from_actions(vec![
                (0, Call("a")),
                (1, Call("b")),
                (2, Call("c")),
                (0, Response("a")),
                (1, Response("b")),
                (2, Response("c")),
            ]);
            let mut copy = history.clone();
            let entry = copy.remove(1);
            copy.insert(entry);
            for (copy, entry) in zip(copy.iter(), history.iter()) {
                assert_eq!(copy, entry);
            }
        }
    }

    mod lift {
        use super::*;

        #[test]
        fn removes_call_and_response_entries() {
            let mut history = History::from_actions(vec![
                (0, Call("a")),
                (1, Call("b")),
                (2, Call("c")),
                (0, Response("a")),
                (1, Response("b")),
                (2, Response("c")),
            ]);
            history.lift(0);
            for (entry, letter) in zip(history.iter(), ["b", "c", "b", "c"]) {
                match entry {
                    Entry::Call(call) => assert_eq!(call.operation, letter),
                    Entry::Response(resp) => assert_eq!(resp.operation, letter),
                }
            }
        }
    }

    mod remove {
        use super::*;

        #[test]
        fn removes_only_requested_entry() {
            let mut history = History::from_actions(vec![
                (0, Call("a")),
                (1, Call("b")),
                (0, Response("a")),
                (1, Response("b")),
            ]);
            match history.remove(1) {
                Entry::Call(call) => assert_eq!(call.operation, "b"),
                Entry::Response(_) => panic!("Removed incorrect entry"),
            }
            for (entry, letter) in zip(history.iter(), ["a", "a", "b"]) {
                match entry {
                    Entry::Call(entry) => assert_eq!(entry.operation, letter),
                    Entry::Response(entry) => assert_eq!(entry.operation, letter),
                }
            }
        }
    }

    mod unlift {
        use super::*;

        #[test]
        fn is_inverse_of_lift() {
            let mut history = History::from_actions(vec![
                (0, Call("a")),
                (1, Call("b")),
                (0, Response("a")),
                (1, Response("b")),
            ]);
            let copy = history.clone();
            let (call, response) = history.lift(0);
            history.unlift(call, response);
            assert_eq!(history, copy)
        }
    }
}