elasticq 0.3.0

Thread-safe, dynamically resizable queues with lock-based and lock-free implementations for high-throughput scenarios
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//! Lock-free MPSC (Multi-Producer Single-Consumer) queue implementation.
//!
//! This module provides a lock-free alternative to the traditional mutex-based
//! approach, designed specifically for MQTT proxy use cases where multiple
//! producers (message publishers) feed into a single consumer (message processor).

use std::alloc::{self, Layout};
use std::ptr::{self, NonNull};
use std::sync::atomic::{AtomicUsize, AtomicU32, AtomicBool, Ordering};
use std::cell::UnsafeCell;
use crossbeam_epoch::{self as epoch, Atomic, Owned, Guard};

#[cfg(feature = "lock_free")]
use portable_atomic::AtomicU64;

use crate::{Config, BufferError, BufferResult};

/// Ring buffer data structure for storing elements.
struct RingBuffer<T> {
    data: UnsafeCell<NonNull<T>>,
    capacity: usize,
    /// Capacity mask for fast modulo operations (capacity must be power of 2)
    mask: usize,
}

impl<T> RingBuffer<T> {
    /// Creates a new ring buffer with the given capacity.
    /// Capacity is rounded up to the next power of 2.
    fn new(capacity: usize) -> BufferResult<Self> {
        let capacity = capacity.next_power_of_two();
        let mask = capacity - 1;
        
        let layout = Layout::array::<T>(capacity)
            .map_err(|_| BufferError::ResizeError("Invalid capacity".to_string()))?;
        
        let data = unsafe {
            let ptr = alloc::alloc(layout) as *mut T;
            NonNull::new(ptr).ok_or_else(|| {
                BufferError::ResizeError("Failed to allocate memory".to_string())
            })?
        };
        
        Ok(Self { 
            data: UnsafeCell::new(data), 
            capacity, 
            mask 
        })
    }
    
    /// Writes a value to the given index without dropping the existing value.
    unsafe fn write(&self, index: usize, value: T) {
        let masked_index = index & self.mask;
        let data_ptr = (*self.data.get()).as_ptr();
        ptr::write(data_ptr.add(masked_index), value);
    }
    
    /// Reads a value from the given index without dropping it.
    unsafe fn read(&self, index: usize) -> T {
        let masked_index = index & self.mask;
        let data_ptr = (*self.data.get()).as_ptr();
        ptr::read(data_ptr.add(masked_index))
    }
}

impl<T> Drop for RingBuffer<T> {
    fn drop(&mut self) {
        unsafe {
            let layout = Layout::array::<T>(self.capacity).unwrap();
            let data_ptr = (*self.data.get()).as_ptr();
            alloc::dealloc(data_ptr as *mut u8, layout);
        }
    }
}

/// Lock-free MPSC queue with dynamic resizing capability.
pub struct LockFreeMPSCQueue<T> {
    /// Current ring buffer (protected by epoch-based reclamation)
    buffer: Atomic<RingBuffer<T>>,
    
    /// Write position for producers (combines position + generation for ABA protection)
    head: AtomicU64,
    
    /// Read position for consumer
    tail: AtomicUsize,
    
    /// Current generation for ABA protection
    generation: AtomicU32,
    
    /// Resize coordination flag
    resizing: AtomicBool,
    
    /// Configuration parameters
    config: Config,
    
    /// Statistics
    messages_enqueued: AtomicUsize,
    messages_dequeued: AtomicUsize,
    messages_dropped: AtomicUsize,
}

impl<T> LockFreeMPSCQueue<T> {
    /// Creates a new lock-free MPSC queue with the given configuration.
    pub fn new(config: Config) -> BufferResult<Self> {
        config.validate()?;
        
        let initial_buffer = RingBuffer::new(config.initial_capacity)?;
        let buffer = Atomic::new(initial_buffer);
        
        Ok(Self {
            buffer,
            head: AtomicU64::new(0),
            tail: AtomicUsize::new(0),
            generation: AtomicU32::new(0),
            resizing: AtomicBool::new(false),
            config,
            messages_enqueued: AtomicUsize::new(0),
            messages_dequeued: AtomicUsize::new(0),
            messages_dropped: AtomicUsize::new(0),
        })
    }
    
    /// Debug-only invariant checking for development and testing.
    /// This function verifies critical invariants of the lock-free queue.
    #[cfg(debug_assertions)]
    #[allow(dead_code)] // Used in non-test builds
    fn debug_assert_invariants(&self) {
        let guard = &epoch::pin();
        
        // Load current buffer safely
        if let Some(buffer) = unsafe { self.buffer.load(Ordering::Acquire, guard).as_ref() } {
            // Ring buffer capacity is power of 2
            debug_assert!(buffer.capacity.is_power_of_two(), 
                         "Ring buffer capacity must be power of 2, got {}", buffer.capacity);
            
            // Capacity bounds
            debug_assert!(buffer.capacity >= self.config.min_capacity,
                         "Capacity {} below minimum {}", buffer.capacity, self.config.min_capacity);
            debug_assert!(buffer.capacity <= self.config.max_capacity,
                         "Capacity {} exceeds maximum {}", buffer.capacity, self.config.max_capacity);
            
            // Head/tail positions consistency
            let head_packed = self.head.load(Ordering::Relaxed);
            let (head_pos, generation) = Self::unpack_head(head_packed);
            let tail_pos = self.tail.load(Ordering::Relaxed);
            let queue_size = head_pos.wrapping_sub(tail_pos);
            
            debug_assert!(queue_size <= buffer.capacity,
                         "Queue size {} exceeds capacity {}", queue_size, buffer.capacity);
            
            // Generation monotonicity
            let current_gen = self.generation.load(Ordering::Relaxed);
            debug_assert!(generation <= current_gen,
                         "Head generation {} exceeds current generation {}", generation, current_gen);
            
            // Stats consistency
            let stats = self.stats();
            debug_assert!(stats.messages_dequeued <= stats.messages_enqueued,
                         "Dequeued {} exceeds enqueued {}", stats.messages_dequeued, stats.messages_enqueued);
            // Note: Skip exact size comparison due to potential races between readings
            // The important invariant is the conservation equation below
            debug_assert!(stats.current_capacity == buffer.capacity,
                         "Stats capacity {} doesn't match buffer capacity {}", stats.current_capacity, buffer.capacity);
            
            // Message conservation equation
            let expected_total = stats.messages_dequeued + stats.current_size + stats.messages_dropped;
            debug_assert!(expected_total == stats.messages_enqueued,
                         "Message conservation violated: {} + {} + {} != {}", 
                         stats.messages_dequeued, stats.current_size, stats.messages_dropped, stats.messages_enqueued);
        }
    }
    
    /// Relaxed version of invariant checking for release builds.
    #[cfg(not(debug_assertions))]
    #[inline(always)]
    fn debug_assert_invariants(&self) {
        // No-op in release builds
    }
    
    /// Extracts position and generation from the packed head value.
    fn unpack_head(head: u64) -> (usize, u32) {
        let position = (head & 0xFFFF_FFFF) as usize;
        let generation = (head >> 32) as u32;
        (position, generation)
    }
    
    /// Packs position and generation into a single u64 value.
    fn pack_head(position: usize, generation: u32) -> u64 {
        ((generation as u64) << 32) | (position as u64 & 0xFFFF_FFFF)
    }
    
    /// Attempts to enqueue a message (producer operation).
    /// Returns Ok(()) on success, or an error if the queue is full or at capacity.
    pub fn try_enqueue(&self, item: T) -> BufferResult<()> {
        let guard = &epoch::pin();
        
        // Check if resize is in progress
        if self.resizing.load(Ordering::Acquire) {
            self.messages_dropped.fetch_add(1, Ordering::Relaxed);
            return Err(BufferError::ResizeError("Resize in progress".to_string()));
        }
        
        loop {
            // Load current buffer and positions
            let buffer_ref = self.buffer.load(Ordering::Acquire, guard);
            let buffer = unsafe { buffer_ref.as_ref() }
                .ok_or_else(|| BufferError::ResizeError("Invalid buffer reference".to_string()))?;
            
            let head_packed = self.head.load(Ordering::Acquire);
            let (head_pos, head_gen) = Self::unpack_head(head_packed);
            let tail_pos = self.tail.load(Ordering::Acquire);
            
            // Check capacity constraints
            let current_size = head_pos.wrapping_sub(tail_pos);
            if current_size >= buffer.capacity {
                // Buffer is full - this is where we should decide to drop vs. retry
                if buffer.capacity >= self.config.max_capacity {
                    // At max capacity, drop message
                    self.messages_dropped.fetch_add(1, Ordering::Relaxed);
                    return Err(BufferError::MaxCapacityReached(self.config.max_capacity));
                } else {
                    // Could potentially resize, but that's consumer's responsibility
                    // For now, just return Full without incrementing drop counter
                    // The caller can decide whether to retry or drop
                    return Err(BufferError::Full);
                }
            }
            
            // Attempt to reserve a slot by advancing head
            let new_head_packed = Self::pack_head(head_pos + 1, head_gen);
            
            match self.head.compare_exchange_weak(
                head_packed,
                new_head_packed,
                Ordering::Release,
                Ordering::Relaxed,
            ) {
                Ok(_) => {
                    // Successfully reserved slot, now write the item
                    let buffer_index = head_pos & (buffer.capacity - 1);
                    unsafe {
                        buffer.write(buffer_index, item);
                    }
                    
                    self.messages_enqueued.fetch_add(1, Ordering::Relaxed);
                    
                    // Skip debug assertions in tests to avoid race conditions in concurrent tests
                    #[cfg(all(debug_assertions, not(test)))]
                    self.debug_assert_invariants();
                    return Ok(());
                }
                Err(_) => {
                    // CAS failed, retry
                    continue;
                }
            }
        }
    }
    
    /// Attempts to dequeue a message (consumer operation).
    /// Returns Some(item) on success, or None if the queue is empty.
    pub fn try_dequeue(&self) -> BufferResult<Option<T>> {
        let guard = &epoch::pin();
        
        loop {
            // Load current buffer and positions
            let buffer_ref = self.buffer.load(Ordering::Acquire, guard);
            let buffer = unsafe { buffer_ref.as_ref() }
                .ok_or_else(|| BufferError::ResizeError("Invalid buffer reference".to_string()))?;
            
            let head_packed = self.head.load(Ordering::Acquire);
            let (head_pos, _) = Self::unpack_head(head_packed);
            let tail_pos = self.tail.load(Ordering::Acquire);
            
            // Check if queue is empty
            if tail_pos >= head_pos {
                return Ok(None);
            }
            
            // Advance tail position first, then read if successful
            if self.tail.compare_exchange_weak(
                tail_pos,
                tail_pos + 1,
                Ordering::Release,
                Ordering::Relaxed,
            ).is_ok() {
                // Successfully advanced tail, now safe to read
                let buffer_index = tail_pos & (buffer.capacity - 1);
                let item = unsafe { buffer.read(buffer_index) };
                
                self.messages_dequeued.fetch_add(1, Ordering::Relaxed);
                
                // Check if resize is needed (consumer-driven)
                self.try_resize_if_needed(guard)?;
                
                // Skip debug assertions in tests to avoid race conditions in concurrent tests
                #[cfg(all(debug_assertions, not(test)))]
                self.debug_assert_invariants();
                return Ok(Some(item));
            }
            
            // CAS failed, retry (another consumer got there first)
            continue;
        }
    }
    
    /// Consumer-driven resize operation.
    fn try_resize_if_needed(&self, guard: &Guard) -> BufferResult<()> {
        let buffer_ref = self.buffer.load(Ordering::Acquire, guard);
        let buffer = unsafe { buffer_ref.as_ref() }
            .ok_or_else(|| BufferError::ResizeError("Invalid buffer reference".to_string()))?;
        
        let head_packed = self.head.load(Ordering::Acquire);
        let (head_pos, _) = Self::unpack_head(head_packed);
        let tail_pos = self.tail.load(Ordering::Acquire);
        let current_size = head_pos.wrapping_sub(tail_pos);
        
        // Check if resize is needed (buffer utilization > 50% and can grow)
        // This triggers resize more aggressively to prevent producers from hitting capacity limits
        if current_size * 2 >= buffer.capacity && buffer.capacity < self.config.max_capacity {
            self.resize(guard)?;
        }
        
        Ok(())
    }
    
    /// Performs the actual resize operation (consumer only).
    fn resize(&self, guard: &Guard) -> BufferResult<()> {
        // Set resize flag to prevent producers from enqueueing
        if self.resizing.compare_exchange(
            false,
            true,
            Ordering::AcqRel,
            Ordering::Relaxed,
        ).is_err() {
            // Already resizing
            return Ok(());
        }
        
        let current_buffer_ref = self.buffer.load(Ordering::Acquire, guard);
        let current_buffer = unsafe { current_buffer_ref.as_ref() }
            .ok_or_else(|| BufferError::ResizeError("Invalid buffer reference".to_string()))?;
        
        // Calculate new capacity
        let new_capacity = (current_buffer.capacity as f64 * self.config.growth_factor)
            .ceil() as usize;
        let new_capacity = new_capacity.min(self.config.max_capacity);
        
        if new_capacity <= current_buffer.capacity {
            // No point in resizing
            self.resizing.store(false, Ordering::Release);
            return Ok(());
        }
        
        // Create new buffer
        let new_buffer = RingBuffer::new(new_capacity)?;
        
        // Copy existing items to new buffer, resetting indices
        let head_packed = self.head.load(Ordering::Acquire);
        let (head_pos, _head_gen) = Self::unpack_head(head_packed);
        let tail_pos = self.tail.load(Ordering::Acquire);

        let item_count = head_pos.wrapping_sub(tail_pos);

        for (new_index, i) in (0..item_count).enumerate() {
            let old_index = tail_pos.wrapping_add(i) & (current_buffer.capacity - 1);
            let item = unsafe { current_buffer.read(old_index) };
            unsafe { new_buffer.write(new_index, item); }
        }
        
        // Increment generation for ABA protection
        let new_generation = self.generation.fetch_add(1, Ordering::AcqRel) + 1;
        
        // Reset indices in new buffer: tail=0, head=item_count
        self.tail.store(0, Ordering::Release);
        let new_head_packed = Self::pack_head(item_count, new_generation);
        self.head.store(new_head_packed, Ordering::Release);
        
        // Atomically swap the buffer
        let new_buffer_owned = Owned::new(new_buffer);
        let old_buffer = self.buffer.swap(new_buffer_owned, Ordering::AcqRel, guard);
        
        // Schedule old buffer for reclamation
        unsafe {
            guard.defer_destroy(old_buffer);
        }
        
        // Clear resize flag
        self.resizing.store(false, Ordering::Release);
        
        Ok(())
    }
    
    /// Returns the current number of items in the queue.
    pub fn len(&self) -> usize {
        let head_packed = self.head.load(Ordering::Acquire);
        let (head_pos, _) = Self::unpack_head(head_packed);
        let tail_pos = self.tail.load(Ordering::Acquire);
        head_pos.wrapping_sub(tail_pos)
    }
    
    /// Returns true if the queue is empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
    
    /// Returns the current capacity of the queue.
    pub fn capacity(&self) -> usize {
        let guard = &epoch::pin();
        let buffer_ref = self.buffer.load(Ordering::Acquire, guard);
        if let Some(buffer) = unsafe { buffer_ref.as_ref() } {
            buffer.capacity
        } else {
            0
        }
    }
    
    /// Returns statistics about queue operations.
    pub fn stats(&self) -> QueueStats {
        // Read all values atomically for consistency
        let messages_enqueued = self.messages_enqueued.load(Ordering::Acquire);
        let messages_dequeued = self.messages_dequeued.load(Ordering::Acquire);
        let messages_dropped = self.messages_dropped.load(Ordering::Acquire);
        
        // Read head/tail positions atomically
        let head_packed = self.head.load(Ordering::Acquire);
        let (head_pos, _) = Self::unpack_head(head_packed);
        let tail_pos = self.tail.load(Ordering::Acquire);
        let current_size = head_pos.wrapping_sub(tail_pos);
        
        QueueStats {
            messages_enqueued,
            messages_dequeued,
            messages_dropped,
            current_size,
            current_capacity: self.capacity(),
        }
    }
}

/// Statistics about queue operations.
#[derive(Debug, Clone)]
pub struct QueueStats {
    pub messages_enqueued: usize,
    pub messages_dequeued: usize,
    pub messages_dropped: usize,
    pub current_size: usize,
    pub current_capacity: usize,
}

// Safety: The queue is designed to be thread-safe for MPSC access patterns
unsafe impl<T: Send> Send for LockFreeMPSCQueue<T> {}
unsafe impl<T: Send> Sync for LockFreeMPSCQueue<T> {}

#[cfg(test)]
mod tests {
    use super::*;
    use std::thread;
    use std::sync::Arc;
    
    fn test_config() -> Config {
        Config::default()
            .with_initial_capacity(4)
            .with_min_capacity(2)
            .with_max_capacity(64)
            .with_growth_factor(2.0)
    }
    
    #[test]
    fn test_basic_enqueue_dequeue() {
        let queue = LockFreeMPSCQueue::new(test_config()).unwrap();
        
        // Test enqueue
        queue.try_enqueue(42).unwrap();
        queue.try_enqueue(24).unwrap();
        
        assert_eq!(queue.len(), 2);
        assert!(!queue.is_empty());
        
        // Test dequeue
        assert_eq!(queue.try_dequeue().unwrap(), Some(42));
        assert_eq!(queue.try_dequeue().unwrap(), Some(24));
        assert_eq!(queue.try_dequeue().unwrap(), None);
        
        assert_eq!(queue.len(), 0);
        assert!(queue.is_empty());
    }
    
    #[test]
    fn test_capacity_limits() {
        let config = test_config()
            .with_initial_capacity(2)
            .with_min_capacity(1)
            .with_max_capacity(4);
        let queue = LockFreeMPSCQueue::new(config).unwrap();
        
        // Fill to max capacity by doing a sequence of enqueue/dequeue to trigger growth
        // Start: capacity=2, max=4
        queue.try_enqueue(1).unwrap(); // size=1
        queue.try_enqueue(2).unwrap(); // size=2, capacity=2 (full)
        
        // Trigger resize by consuming one item  
        queue.try_dequeue().unwrap(); // size=1, should resize to capacity=4
        
        // Fill to max capacity
        queue.try_enqueue(3).unwrap(); // size=2
        queue.try_enqueue(4).unwrap(); // size=3  
        queue.try_enqueue(5).unwrap(); // size=4, capacity=4 (at max capacity)
        
        // Now at max capacity (4), should start dropping
        assert!(matches!(
            queue.try_enqueue(6),
            Err(BufferError::Full) | Err(BufferError::MaxCapacityReached(_))
        ));
    }
    
    #[test]
    fn test_mpsc_concurrent_access() {
        let queue = Arc::new(LockFreeMPSCQueue::new(test_config()).unwrap());
        let num_producers = 2;
        let messages_per_producer = 10;
        
        // Spawn producer threads
        let mut handles = vec![];
        for producer_id in 0..num_producers {
            let queue_clone = Arc::clone(&queue);
            let handle = thread::spawn(move || {
                for i in 0..messages_per_producer {
                    let message = producer_id * 1000 + i;
                    let mut retry_count = 0;
                    while queue_clone.try_enqueue(message).is_err() {
                        thread::yield_now();
                        retry_count += 1;
                        if retry_count > 1000 {
                            // Avoid infinite loops in tests
                            break;
                        }
                    }
                }
            });
            handles.push(handle);
        }
        
        // Consumer thread
        let queue_clone = Arc::clone(&queue);
        let consumer_handle = thread::spawn(move || {
            let mut received = vec![];
            let expected_total = num_producers * messages_per_producer;
            
            let mut no_message_count = 0;
            while received.len() < expected_total {
                if let Ok(Some(message)) = queue_clone.try_dequeue() {
                    received.push(message);
                    no_message_count = 0; // Reset counter
                } else {
                    thread::yield_now();
                    no_message_count += 1;
                    if no_message_count > 10000 {
                        // Avoid infinite loops if not all messages arrive
                        break;
                    }
                }
            }
            received
        });
        
        // Wait for all producers
        for handle in handles {
            handle.join().unwrap();
        }
        
        // Wait for consumer
        let received = consumer_handle.join().unwrap();
        
        // With potential message drops due to capacity limits, just check we got most messages
        let expected_total = num_producers * messages_per_producer;
        assert!(received.len() >= expected_total / 2, 
               "Expected at least {} messages, got {}", expected_total / 2, received.len());
        
        let stats = queue.stats();
        println!("Final stats: {:?}", stats);
    }
    
    #[test]
    fn test_resize_behavior() {
        let config = test_config()
            .with_initial_capacity(4)
            .with_min_capacity(2)
            .with_max_capacity(16)
            .with_growth_factor(2.0);
        let queue = LockFreeMPSCQueue::new(config).unwrap();
        
        assert_eq!(queue.capacity(), 4);
        
        // Fill to capacity, then consume to trigger resize
        for i in 0..4 {
            queue.try_enqueue(i).unwrap();
        }
        
        // Consume some items to trigger resize check
        for _ in 0..2 {
            queue.try_dequeue().unwrap();
        }
        
        // Capacity should have grown
        assert!(queue.capacity() > 4);
        
        // Now we should be able to enqueue more items (we have capacity 8, size 2)
        for i in 4..8 {
            queue.try_enqueue(i).unwrap();
        }
        
        // Dequeue remaining items
        for _ in 2..8 {
            queue.try_dequeue().unwrap();
        }
        
        let stats = queue.stats();
        assert_eq!(stats.messages_enqueued, 8);
        assert_eq!(stats.messages_dequeued, 8);
    }
}