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
//! This module contains an implementation of a threshold clock.
//!
//! The concept of threshold-union is explained in detail in [this blog post](https://vitorenes.org/post/2018/11/threshold-union/).
//!
//! # Examples
//! ```
//! use threshold::{clock, *};
//!
//! let vclock_0 = clock::vclock_from_seqs(vec![10, 5, 5]);
//! let vclock_1 = clock::vclock_from_seqs(vec![8, 10, 6]);
//!
//! let mut tclock = TClock::new();
//! tclock.add(vclock_0);
//! tclock.add(vclock_1);
//!
//! let vclock_t1 = clock::vclock_from_seqs(vec![10, 10, 6]);
//! let vclock_t2 = clock::vclock_from_seqs(vec![8, 5, 5]);
//!
//! assert_eq!(tclock.threshold_union(1), (vclock_t1, true));
//! assert_eq!(tclock.threshold_union(2), (vclock_t2, false));
//! ```

use crate::*;
use std::collections::HashMap;
use std::marker::PhantomData;

type EventCount = (u64, u64);

#[derive(Debug, Clone)]
pub struct TClock<A: Actor, E: EventSet> {
    /// A `MultiSet` per `Actor`
    occurrences: HashMap<A, MultiSet<u64, EventCount>>,
    phantom: PhantomData<E>,
}

impl<A: Actor, E: EventSet> TClock<A, E> {
    /// Returns a new `TClock` instance.
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        TClock {
            occurrences: HashMap::new(),
            phantom: PhantomData,
        }
    }

    /// Returns a new `TClock` instance with a given capacity.
    pub fn with_capacitiy(capacity: usize) -> Self {
        TClock {
            occurrences: HashMap::with_capacity(capacity),
            phantom: PhantomData,
        }
    }

    /// Add a `Clock` to the `TClock`.
    ///
    /// # Examples
    /// ```
    /// use threshold::{clock, *};
    ///
    /// let mut tset = TClock::new();
    ///
    /// let vclock = clock::vclock_from_seqs(1..10);
    /// tset.add(vclock);
    /// ```
    pub fn add(&mut self, clock: Clock<A, E>) {
        for (actor, eset) in clock {
            self.add_entry(actor, eset);
        }
    }

    /// Adds a single clock entry to the `TClock`.
    fn add_entry(&mut self, actor: A, eset: E) {
        // compute event count
        let count = event_count(eset);
        // get current multi set for this actor
        let mset = self.occurrences.entry(actor).or_insert_with(MultiSet::new);
        // add new events
        mset.add(count);
    }
}

impl<A: Actor> TClock<A, MaxSet> {
    /// Computes the [threshold-union](https://vitorenes.org/post/2018/11/threshold-union/)
    /// of all `VClock` added to the `TClock`.
    ///
    /// Assume multiset `X` is `{10: 1, 8: 2, 6: 3, 5: 1}`.
    /// This means that event `10` was seen once, event `8` twice, and so on.
    ///
    /// (Recall that for vector clocks, seeing event 10 means seeing all events
    /// from 1 to 10.)
    ///
    /// If, for example, we want the event that was seen at least 4 times (i.e.
    /// our threshold is 4), we should get event `6`.
    ///
    /// Assume `threshold(u64, X) -> Option<u64>` where the first argument is
    /// the threshold desired. Then:
    /// - `threshold(1, X) = Some(10)`
    /// - `threshold(2, X) = Some(8)`
    /// - `threshold(3, X) = Some(8)`
    /// - `threshold(4, X) = Some(6)`
    /// - `threshold(7, X) = Some(5)`
    /// - `threshold(8, X) = None`
    ///
    /// # Examples
    /// ```
    /// use threshold::{clock, *};
    /// let vclock_0 = clock::vclock_from_seqs(vec![10, 5, 5]);
    /// let vclock_1 = clock::vclock_from_seqs(vec![8, 10, 6]);
    /// let vclock_2 = clock::vclock_from_seqs(vec![9, 8, 7]);
    ///
    /// let mut tclock = TClock::new();
    /// tclock.add(vclock_0);
    /// tclock.add(vclock_1);
    /// tclock.add(vclock_2);
    ///
    /// let vclock_t1 = clock::vclock_from_seqs(vec![10, 10, 7]);
    /// let vclock_t2 = clock::vclock_from_seqs(vec![9, 8, 6]);
    /// let vclock_t3 = clock::vclock_from_seqs(vec![8, 5, 5]);
    ///
    /// assert_eq!(tclock.threshold_union(1), (vclock_t1, true));
    /// assert_eq!(tclock.threshold_union(2), (vclock_t2, false));
    /// assert_eq!(tclock.threshold_union(3), (vclock_t3, false));
    /// ```
    pub fn threshold_union(&self, threshold: u64) -> (VClock<A>, bool) {
        // the highest sequence seen for each process
        let mut equal_to_union = true;

        let iter = self.occurrences.iter().map(|(actor, tset)| {
            let mut total_positives = 0;

            // get the highest sequence that passes the threshold
            let seq = tset
                .iter()
                .rev()
                .find(|(_, &(positives, _))| {
                    // `total_pos` records the implicit number of observations:
                    // since we are iterating from the highest event to the
                    // lowest, and the observation of event X counts as an
                    // observation of event Y when X > Y, we can simply
                    // accumulate all observations in `total_pos` and stop the
                    // once `total_pos` reaches the threshold
                    total_positives += positives;
                    total_positives >= threshold
                })
                // if there is an event that passes the threshold, return it
                // otherwise, return `0`
                .map_or(0, |(&seq, _)| seq);

            // get highest sequence for this actor
            let highest = tset.iter().rev().next().map_or(0, |(&seq, _)| seq);
            // check if equal to union for this process
            equal_to_union = equal_to_union && highest == seq;

            // compute vclock entry
            (actor.clone(), MaxSet::from_event(seq))
        });

        (VClock::from(iter), equal_to_union)
    }

    /// Computes the union of all `VClock` added to the `TClock`.
    /// A boolean is also returned indicating whether all `VClock` added are
    /// equal.
    ///
    /// # Examples
    /// ```
    /// use threshold::{clock, *};
    ///
    /// let vclock_0 = clock::vclock_from_seqs(vec![10, 5, 5]);
    /// let vclock_1 = clock::vclock_from_seqs(vec![9, 8, 7]);
    ///
    /// let mut tclock = TClock::new();
    /// tclock.add(vclock_0.clone());
    /// tclock.add(vclock_1.clone());
    ///
    /// let expected = clock::vclock_from_seqs(vec![10, 8, 7]);
    /// assert_eq!(tclock.union(), (expected, false));
    ///
    /// let mut tclock = TClock::new();
    /// tclock.add(vclock_0.clone());
    /// tclock.add(vclock_0.clone());
    /// tclock.add(vclock_0.clone());
    ///
    /// let expected = clock::vclock_from_seqs(vec![10, 5, 5]);
    /// assert_eq!(tclock.union(), (expected, true));
    /// ```
    pub fn union(&self) -> (VClock<A>, bool) {
        let mut all_equal = true;

        let iter = self.occurrences.iter().map(|(actor, tset)| {
            // get the highest sequence
            let mut tset = tset.iter().rev();
            let (highest, _) = tset
                .next()
                .expect("there should be at least one event per actor");

            // if there's another sequence, then the clocks reported are not all
            // equal
            if tset.next().is_some() {
                all_equal = false;
            }

            // compute vclock entry
            (actor.clone(), MaxSet::from_event(*highest))
        });

        (VClock::from(iter), all_equal)
    }
}

impl<A: Actor> TClock<A, BelowExSet> {
    /// Computes the [threshold-union](https://vitorenes.org/post/2018/11/threshold-union/)
    /// of all `BEClock` added to the `TClock`.
    ///
    /// # Examples
    /// ```
    /// use threshold::*;
    ///
    /// let b = String::from("B");
    /// let mut clock_a = BEClock::new();
    /// clock_a.add(&b, 5);
    /// clock_a.add(&b, 6);
    ///
    /// let mut clock_b = BEClock::new();
    /// clock_b.add(&b, 5);
    /// clock_b.add(&b, 7);
    ///
    /// let mut tclock = TClock::new();
    /// tclock.add(clock_a);
    /// tclock.add(clock_b);
    ///
    /// let mut expected = BEClock::new();
    /// expected.add(&b, 5);
    ///
    /// assert_eq!(tclock.threshold_union(2), expected);
    /// ```
    pub fn threshold_union(&self, threshold: u64) -> BEClock<A> {
        let iter = self.occurrences.iter().map(|(actor, tset)| {
            let mut total_pos = 0;

            // skip until some entry passes the threshold
            let iter = tset
                .iter()
                .rev()
                .skip_while(|(_, &(pos, _))| {
                    // `total_pos` records the implicit number of observations:
                    // since we are iterating from the highest event to the
                    // lowest, and the observation of event X counts as an
                    // observation of event Y when X > Y, we can simply
                    // accumulate all observations in `total_pos` and stop the
                    // `skip_while` once `total_pos` passes the threshold
                    total_pos += pos;
                    total_pos < threshold
                })
                // had to collect here so that the borrow of `total_pos` ends
                // TODO can we avoid this?
                .collect::<Vec<_>>();
            let mut iter = iter.iter().peekable();

            let highest = match iter.next() {
                None => Ok(0),
                Some((&seq, &(_, neg))) => {
                    // check if the highest seq that passes the positive
                    // threshold is valid, i.e. if it still passes the threshold
                    // after subtracting the negative votes
                    if total_pos - neg >= threshold {
                        // if yes, this is the highest sequence
                        Ok(seq)
                    } else {
                        // if not, the highest sequence may not have received
                        // any of vote, i.e. it is not in the structure
                        Err(seq)
                    }
                }
            }
            .unwrap_or_else(|seq| {
                // if the highest `seq` that passed the positive threshold is
                // not the highest sequence we are looking for, then any
                // sequence smaller than `seq` could be the highest sequence
                // (even if it's not part of our structure)
                let mut candidate = seq - 1;
                loop {
                    match iter.peek() {
                        None => {
                            // if the structure is empty, then the current
                            // candidate is the highest sequence
                            break candidate;
                        }
                        Some((&next_seq, &(pos, neg))) => {
                            if next_seq == candidate {
                                // if the `candidate` is in the structure
                                // advance the iterator
                                // - we can't always advance the iterator
                                //   because the element we're peeking might be
                                //   an exception, so we only advance when we're
                                //   sure that it will never be an exception
                                iter.next();

                                // accumulate more positives
                                total_pos += pos;

                                if total_pos - neg >= threshold {
                                    // if `candidate` passes the threshold, then
                                    // we've found the highest sequence
                                    break candidate;
                                } else {
                                    // otherwise, try a smaller sequence
                                    candidate -= 1;
                                }
                            } else {
                                // if the `candidate` is not in the structure,
                                // then this `candidate` is the highest sequence
                                break candidate;
                            }
                        }
                    }
                }
            });

            // compute exceptions:
            // - if there are any exceptions, they are part of our structure
            let exs = iter.filter_map(|(&seq, &(pos, neg))| {
                // accumulate more positives
                total_pos += pos;

                // we have an exception when `total_pos - neg < threshold`
                // - the `neg > total_pos` is here just to prevent that
                // `total_pos - neg` overflows
                if neg > total_pos || total_pos - neg < threshold {
                    Some(seq)
                } else {
                    None
                }
            });

            let below_exset = BelowExSet::from(highest, exs);
            (actor.clone(), below_exset)
        });

        BEClock::from(iter)
    }
}

fn event_count<E: EventSet>(
    eset: E,
) -> impl Iterator<Item = (u64, EventCount)> {
    // get events
    let (left, right) = eset.events();

    // compute left event count
    let left_count = std::iter::once(left).map(|x| (x, (1, 0)));

    // compute right events count
    let right_count = right.into_iter().map(|x| (x, (0, 1)));

    // chain both
    left_count.chain(right_count)
}

#[cfg(test)]

mod tests {
    use super::*;

    #[test]
    fn regression_test_beclock() {
        let b = String::from("B");

        // Clock { clock: {B: BelowExSet { max: 6, exs: {1, 2, 3, 4} }} }
        let mut clock_a = BEClock::new();
        clock_a.add(&b, 5);
        clock_a.add(&b, 6);

        // Clock { clock: {B: BelowExSet { max: 7, exs: {1, 2, 3, 4, 6} }} }
        let mut clock_b = BEClock::new();
        clock_b.add(&b, 5);
        clock_b.add(&b, 7);

        // add both clocks to the threshold clock
        let mut tclock = TClock::new();
        tclock.add(clock_a);
        tclock.add(clock_b);

        // compute the threshold union
        let clock = tclock.threshold_union(2);

        // create the expected clock
        let mut expected = BEClock::new();
        expected.add(&b, 5);

        assert_eq!(clock, expected);
    }

    #[test]
    fn regression_test_vclock() {
        // create tclock
        let mut tclock = TClock::new();

        // n = 5

        // create clocks
        let bottom = clock::vclock_from_seqs(vec![0, 0, 0, 0, 0]);
        let c1 = clock::vclock_from_seqs(vec![1, 0, 0, 0, 0]);
        let c2 = clock::vclock_from_seqs(vec![0, 1, 0, 0, 0]);
        let both = clock::vclock_from_seqs(vec![1, 1, 0, 0, 0]);

        // add first clock
        tclock.add(c1.clone());

        // compute threshold = 1
        let (t1, equal_to_union) = tclock.threshold_union(1);
        assert_eq!(t1, c1);
        assert_eq!(equal_to_union, true);

        // compute threshold = 2
        let (t2, equal_to_union) = tclock.threshold_union(2);
        assert_eq!(t2, bottom);
        assert_eq!(equal_to_union, false);

        // add second clock
        tclock.add(c2.clone());

        // compute threshold = 1 (it changes)
        let (t1, equal_to_union) = tclock.threshold_union(1);
        assert_eq!(t1, both);
        assert_eq!(equal_to_union, true);

        // compute threshold = 2 (doesn't change)
        let (t2, equal_to_union) = tclock.threshold_union(2);
        assert_eq!(t2, bottom);
        assert_eq!(equal_to_union, false);

        // add third clock (equal to the first)
        tclock.add(c1.clone());

        // compute threshold = 1 (doesn't change)
        let (t1, equal_to_union) = tclock.threshold_union(1);
        assert_eq!(t1, both);
        assert_eq!(equal_to_union, true);

        // compute threshold = 2 (it changes)
        let (t2, equal_to_union) = tclock.threshold_union(2);
        assert_eq!(t2, c1);
        assert_eq!(equal_to_union, false);

        // add fourth clock (equal to the second)
        tclock.add(c2.clone());

        // compute threshold = 1 (doesn't change)
        let (t1, equal_to_union) = tclock.threshold_union(1);
        assert_eq!(t1, both);
        assert_eq!(equal_to_union, true);

        // compute threshold = 2 (it changes)
        let (t2, equal_to_union) = tclock.threshold_union(2);
        assert_eq!(t2, both);
        assert_eq!(equal_to_union, true);
    }
}