fremkit 0.1.1

A simple broadcast log
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
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//! This module contains the implementation of the bounded `Log` type.

use crate::sync::{AtomicUsize, Ordering};
use crate::LogError;

use std::cell::UnsafeCell;
use std::sync::Arc;

use cache_padded::CachePadded;

/// This Log stores an immutable, append-only, bounded, concurrent sequence of items.
///
/// It's a performance-minded wrapper around a fixed-size vector, and is thread-safe.
///
/// A Log's primary use case is to store an immutable sequence of messages, events, or other data, and to allow
/// multiple readers to access the data concurrently.
///
/// Performance-wise, the Log aim to be almost as fast as a `Vec` for single-threaded push operations, and
/// will be equally fast for get operations.
/// For multi-threaded push operations, the Log should be as fast as a `Vec` wrapped in a `Mutex` or `RwLock`.
/// For multi-threaded get operations, the Log will be faster than a `Vec` wrapped in a `RwLock`.
/// Additional performance analysis are available in the benchmarks.
///
/// Operations on Log are lock-free, and will never block.
/// The Log also supports concurrent push get operations.
/// The Log will never be resized, and will always have the same capacity.
///
/// All data pushed on the Log will become available for get in the same order as it was pushed,
/// and will always be available at the returned index.
///
/// When the Log becomes full, push will fail and return an error. A get to an existing index will always succeed.
///
/// # Examples
/// ```
/// use fremkit::bounded::Log;
///
/// let log: Log<u64> = Log::new(100);
/// log.push(1).unwrap();
/// log.push(2).unwrap();
///
/// assert_eq!(log.get(0), Some(&1));
/// assert_eq!(log.get(1), Some(&2));
/// assert_eq!(log.get(2), None);
///
/// assert_eq!(log.len(), 2);
/// assert_eq!(log.capacity(), 100);
/// ```
#[derive(Debug)]
pub struct Log<T> {
    len: CachePadded<AtomicUsize>,
    capacity: usize,
    data: Vec<UnsafeCell<Option<T>>>,
}

impl<T> Log<T> {
    /// Create a new empty Log. It will be able to hold at least `capacity` items.
    /// If `capacity` is 0, the Log will be created with a capacity of 1.
    ///
    /// # Examples
    /// ```
    /// use fremkit::bounded::Log;
    ///
    /// let log: Log<u64> = Log::new(100);
    /// ```
    pub fn new(capacity: usize) -> Self {
        let capacity = capacity.max(1);

        // Specifying capacity here, means we are able to hold at least
        // this many items without reallocating.
        let mut data = Vec::with_capacity(capacity);

        // Initialize the data.
        for _ in 0..capacity {
            data.push(UnsafeCell::new(None));
        }

        Self {
            capacity,
            len: CachePadded::new(AtomicUsize::new(0)),
            data,
        }
    }

    /// Get the current length of the log.
    ///
    /// This is the number of items that have been pushed on the log.
    /// It will never be greater than the capacity of the log.
    ///
    /// # Examples
    /// ```
    /// use fremkit::bounded::Log;
    ///
    /// let log: Log<u64> = Log::new(100);
    /// log.push(1).unwrap();
    /// log.push(2).unwrap();
    ///
    /// assert_eq!(log.len(), 2);
    /// ```
    #[inline]
    pub fn len(&self) -> usize {
        self.len.load(Ordering::Relaxed).min(self.capacity())
    }

    /// Get the capacity of the log.
    ///
    /// This is the maximum number of items that can be pushed on the log.
    ///
    /// # Examples
    /// ```
    /// use fremkit::bounded::Log;
    ///
    /// let log: Log<u64> = Log::new(100);
    ///
    /// assert_eq!(log.capacity(), 100);
    /// ```
    #[inline]
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Is the log empty ?
    ///
    /// # Examples
    /// ```
    /// use fremkit::bounded::Log;
    ///
    /// let log: Log<u64> = Log::new(100);
    ///
    /// assert!(log.is_empty());
    /// ```
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get an item from the log.
    ///
    /// # Arguments
    /// * `index` - The index of the item to get.
    ///
    /// # Returns
    /// A reference to the item at the given index, or `None` if the index is out of bounds.
    ///
    /// # Examples
    /// ```
    /// use fremkit::bounded::Log;
    ///
    /// let log: Log<u64> = Log::new(100);
    /// log.push(1).unwrap();
    /// log.push(2).unwrap();
    ///
    /// assert_eq!(log.get(0), Some(&1));
    /// assert_eq!(log.get(1), Some(&2));
    /// assert_eq!(log.get(2), None);
    /// assert_eq!(log.get(123), None);
    /// ```
    pub fn get(&self, index: usize) -> Option<&T> {
        if index >= self.len() {
            return None;
        }

        // SAFETY: We know that the index is in bounds, and that the cell is initialized.
        // We also know that the cell will not be modified while we are holding a reference to it.
        // This is because the cell is never modified. The only way to modify the cell is to push an item,
        // and this will only happen if the cell is empty.
        // We also know that the cell will not be dropped while we are holding a reference to it.
        let cell = &self.data[index];

        unsafe { (*cell.get()).as_ref() }
    }

    /// Append an item to the log.
    ///
    /// Once the item has been appended, it will be available for get at the returned index.
    /// The index will always be in the range [0, capacity). Items cannot be removed from the log.
    /// If the log is full, the item will not be appended, and an error containing the item will be returned.
    ///
    /// # Arguments
    /// * `value` - The item to append.
    ///
    /// # Returns
    /// The index of the item in the log, or an error containing the item if the log is full.
    ///
    /// # Examples
    /// ```
    /// use fremkit::bounded::Log;
    ///
    /// let log: Log<u64> = Log::new(100);
    /// assert_eq!(log.push(1).unwrap(), 0);
    /// assert_eq!(log.push(2).unwrap(), 1);
    ///
    /// assert_eq!(log.get(0), Some(&1));
    /// assert_eq!(log.get(1), Some(&2));
    /// ```
    pub fn push(&self, value: T) -> Result<usize, LogError<T>> {
        // Get the next token.
        // This is the index the item will be written to.
        // INVARIANT: The token will always be in the range [0, capacity).
        // INVARIANT: The token will always be unique.
        // INVARIANT: The series of tokens will always be monotonically increasing.
        let token = self.len.fetch_add(1, Ordering::Relaxed);

        if token >= self.capacity() {
            return Err(LogError::LogCapacityExceeded(value));
        }

        // Get the cell to write to.
        // SAFETY: The token is always in the range [0, capacity).
        let cell = &self.data[token];

        // SAFETY: Cells can only be written to once, and we are the only writer.
        // SAFETY: It is safe to write to the cell, as it cannot be read from until we first write to it.
        let slot = unsafe { &mut *cell.get() };
        *slot = Some(value);

        Ok(token)
    }
}

unsafe impl<T: Sync + Send> Send for Log<T> {}
unsafe impl<T: Sync + Send> Sync for Log<T> {}

//
// Public API similar to std::sync::mpsc::channel simplified consumption.
// Please note that the API does not make complete sense for a bounded log.
//

impl<T> Log<T> {
    /// Convert the Log into a Sender.
    pub fn into_sender(self: Arc<Self>) -> Sender<T> {
        Sender { log: self }
    }

    /// Convert the Log into a Receiver.
    ///
    /// Please note that 'Receiver' is not a good name for the reading end of a Log,
    /// but it is used for consistency with the std::sync::mpsc::channel API.
    pub fn into_receiver(self: Arc<Self>) -> Receiver<T> {
        Receiver { log: self }
    }

    /// Create an iterator over the log.
    ///
    /// The iterator will start at the beginning of the channel.
    /// When reaching the end of the channel, the iterator will stop.
    ///
    /// # Examples
    /// ```
    /// use fremkit::bounded::Log;
    ///
    /// let log: Log<u64> = Log::new(100);
    /// log.push(1).unwrap();
    /// log.push(2).unwrap();
    ///
    /// for item in log.iter() {
    ///    println!("{}", item);
    /// }
    /// ```
    pub fn iter(&self) -> LogReaderIterator<T> {
        LogReaderIterator { idx: 0, log: self }
    }
}

/// Open a new log with a given capacity.
///
/// The capacity is the maximum number of items that can be stored in the log.
///
/// # Arguments
/// * `capacity` - The maximum number of items that can be stored in the log.
///
/// # Returns
/// A Sender and a Receiver.
pub fn open<T>(capacity: usize) -> (Sender<T>, Receiver<T>) {
    let log = Arc::new(Log::new(capacity));

    (Sender { log: log.clone() }, Receiver { log })
}

/// Sender half of a Log.
///
/// The Sender can be cloned, and the clones will all refer to the same Log.
/// Note, this struct is provided for compatibilities with the std::sync::mpsc::channel API.
#[derive(Debug, Clone)]
pub struct Sender<T> {
    log: Arc<Log<T>>,
}

impl<T> Sender<T> {
    /// Send an item to the Log.
    ///
    /// # Arguments
    /// * `value` - The item to send.
    ///
    /// # Returns
    /// The index of the item in the log, or an error containing the item if the log is full.
    pub fn send(&self, value: T) -> Result<usize, LogError<T>> {
        self.log.push(value)
    }

    /// Convert the sender into its inner Log.
    pub fn into_inner(self) -> Arc<Log<T>> {
        self.log
    }
}

/// Reader half of a Log.
///
/// The Reader can be cloned, and the clones will all refer to the same Log.
/// Note, this struct is provided for compatibilities with the std::sync::mpsc::channel API.
#[derive(Debug, Clone)]
pub struct Receiver<T> {
    log: Arc<Log<T>>,
}

impl<T> Receiver<T> {
    /// Read an item from the Log at a given index.
    ///
    /// # Arguments
    /// * `index` - The index of the item to read, or receive.
    ///
    /// # Returns
    /// The item at the given index, or None if the index is out of bounds.
    pub fn recv(&self, index: usize) -> Option<&T> {
        self.log.get(index)
    }

    /// Convert the Reader into its inner Log.
    pub fn into_inner(self) -> Arc<Log<T>> {
        self.log
    }
}

/// Iterator over the items in a Log.
pub struct LogReaderIterator<'a, T> {
    idx: usize,
    log: &'a Log<T>,
}

impl<'a, T> Iterator for LogReaderIterator<'a, T> {
    type Item = &'a T;

    fn next(&mut self) -> Option<Self::Item> {
        let idx = self.idx;
        self.idx += 1;

        self.log.get(idx)
    }
}

#[cfg(test)]
mod test {
    use std::sync::Arc;

    use log::debug;

    use crate::sync::thread;

    use super::*;

    fn init() {
        let _ = env_logger::builder().is_test(true).try_init();
    }

    #[test]
    #[cfg(loom)]
    fn test_loom() {
        loom::model(test_log_capacity);
        loom::model(test_log_capacity_excess);
        loom::model(test_log_capacity_excess_len);
        loom::model(test_log_immutable_entries);
        loom::model(test_basic_log);
        loom::model(test_log_iter);
        loom::model(test_send_recv);
        loom::model(test_eventual_consistency);
    }

    #[test]
    fn test_log_capacity() {
        init();

        let log: Log<u32> = Log::new(0);

        assert_eq!(log.capacity(), 1);
    }

    #[test]
    fn test_log_capacity_excess() {
        init();

        let log = Log::new(1);

        log.push(0).unwrap();

        assert!(log.push(1).is_err());
    }

    #[test]
    fn test_log_capacity_excess_len() {
        init();

        let log = Log::new(1);

        log.push(0).unwrap();
        log.push(1).unwrap_err();
        log.push(2).unwrap_err();
        log.push(3).unwrap_err();
        log.push(4).unwrap_err();

        assert_eq!(log.len(), 1);
    }

    #[test]
    fn test_log_immutable_entries() {
        init();

        let log = Log::new(200);

        log.push(0).unwrap();
        log.push(42).unwrap();

        assert_eq!(log.get(1).map(|s| *s), Some(42));

        for i in 0..100 {
            log.push(i).unwrap();
        }

        assert_eq!(log.get(1).map(|s| *s), Some(42));
    }

    #[test]
    fn test_basic_log() {
        init();

        let log = Log::new(3);

        log.push(1).unwrap();
        log.push(2).unwrap();
        log.push(3).unwrap();

        assert_eq!(log.get(0), Some(&1));
        assert_eq!(log.get(1), Some(&2));
        assert_eq!(log.get(2), Some(&3));
        assert_eq!(log.get(3), None);
    }

    #[test]
    fn test_log_iter() {
        init();

        let log = Log::new(3);

        log.push(1).unwrap();
        log.push(2).unwrap();
        log.push(3).unwrap();

        let mut iter = log.iter();

        assert_eq!(iter.next(), Some(&1));
        assert_eq!(iter.next(), Some(&2));
        assert_eq!(iter.next(), Some(&3));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn test_send_recv() {
        init();

        let (tx, rx) = open(4);

        tx.send(1).unwrap();
        tx.send(2).unwrap();
        tx.send(3).unwrap();

        assert_eq!(rx.recv(0), Some(&1));
        assert_eq!(rx.recv(1), Some(&2));
        assert_eq!(rx.recv(2), Some(&3));
        assert_eq!(rx.recv(3), None);

        tx.into_inner().push(4).unwrap();

        assert_eq!(rx.recv(3), Some(&4));
    }

    #[test]
    fn test_eventual_consistency() {
        init();

        let vec = Arc::new(Log::new(2));
        let v1 = vec.clone();
        let v2 = vec.clone();

        let h1 = thread::spawn(move || {
            v1.push('a').unwrap();

            let x0 = v1.get(0);
            let x1 = v1.get(1);

            (x0.cloned(), x1.cloned())
        });

        let h2 = thread::spawn(move || {
            v2.push('b').unwrap();

            let x0 = v2.get(0);
            let x1 = v2.get(1);

            (x0.cloned(), x1.cloned())
        });

        let (x0h1, x1h1) = h1.join().unwrap();
        let (x0h2, x1h2) = h2.join().unwrap();
        let (x0, x1) = (vec.get(0).cloned(), vec.get(1).cloned());

        debug!(
            "0: h1(a) {:<10} h2(c) {:<10}  f {:<10}",
            format!("{:?}", x0h1),
            format!("{:?}", x0h2),
            format!("{:?}", x0)
        );
        debug!(
            "1: h1(b) {:<10} h2(d) {:<10}  f {:<10}",
            format!("{:?}", x1h1),
            format!("{:?}", x1h2),
            format!("{:?}", x1)
        );
        debug!("");

        match (x0h1, x1h1, x0h2, x1h2) {
            (None, None, _, _) | (_, _, None, None) => {
                assert!(false, "1|2: (Read your own write)");
            }
            (None, Some(_), None, Some(_)) => {
                assert!(false, "1: (Read your own write)");
            }
            (Some(_), None, Some(_), None) => {
                assert!(false, "2: (Read your own write)");
            }
            (None, Some(_), Some(_), None) => {
                assert!(false, "(Observed state are global)");
            }

            (Some(a), None, None, Some(d)) => {
                assert_eq!(Some(a), x0, "a == x0 (Observed state are immutable)");
                assert_eq!(Some(d), x1, "d == x1 (Observed state are immutable)");
            }
            (None, Some(b), Some(c), Some(d)) => {
                assert_eq!(b, d, "b == d (Observed state are in-order)");
                assert_eq!(Some(b), x1, "b == x1 (Observed state are immutable)");
                assert_eq!(Some(c), x0, "c == x0 (Observed state are immutable)");
                assert_eq!(Some(d), x1, "d == x1 (Observed state are immutable)");
            }
            (Some(a), None, Some(c), Some(d)) => {
                assert_eq!(a, c, "a == c (Observed state are in-order)");
                assert_eq!(Some(a), x0, "a == x0 (Observed state are immutable)");
                assert_eq!(Some(c), x0, "c == x0 (Observed state are immutable)");
                assert_eq!(Some(d), x1, "d == x1 (Observed state are immutable)");
            }
            (Some(a), Some(b), Some(c), None) => {
                assert_eq!(a, c, "a == c (Observed state are in-order)");
                assert_eq!(Some(a), x0, "a == x0 (Observed state are immutable)");
                assert_eq!(Some(b), x1, "b == x1 (Observed state are immutable)");
                assert_eq!(Some(c), x0, "c == x0 (Observed state are immutable)");
            }
            (Some(a), Some(b), None, Some(d)) => {
                assert_eq!(b, d, "b == d (Observed state are in-order)");
                assert_eq!(Some(a), x0, "a == x0 (Observed state are immutable)");
                assert_eq!(Some(b), x1, "b == x1 (Observed state are immutable)");
                assert_eq!(Some(d), x1, "d == x1 (Observed state are immutable)");
            }
            (Some(a), Some(b), Some(c), Some(d)) => {
                assert_eq!(a, c, "a == c");
                assert_eq!(b, d, "b == d");
                assert_eq!(Some(a), x0, "a == x0 (Observed state are immutable)");
                assert_eq!(Some(b), x1, "b == x1 (Observed state are immutable)");
                assert_eq!(Some(c), x0, "c == x0 (Observed state are immutable)");
                assert_eq!(Some(d), x1, "d == x1 (Observed state are immutable)");
            }
        }

        let pair = [x0, x1];

        assert!(
            pair == [Some('a'), Some('b')] || pair == [Some('b'), Some('a')],
            "final state is always complete."
        );
    }
}