allsource-core 0.19.1

High-performance event store core built in Rust
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
//! High-throughput batch processing pipeline
//!
//! This module provides an optimized batch processing system that combines:
//! - SIMD-accelerated JSON parsing for fast deserialization
//! - Lock-free sharded queues for parallel ingestion
//! - Memory pooling for zero-allocation hot paths
//!
//! # Performance Target
//! - 1M+ events/sec sustained throughput
//! - Sub-millisecond p99 latency
//! - Linear scalability with CPU cores

use crate::{
    domain::entities::Event,
    error::Result,
    infrastructure::persistence::{
        lock_free::{LockFreeMetrics, ShardedEventQueue},
        simd_json::{SimdJsonParser, SimdJsonStats},
    },
};
use bumpalo::Bump;
use serde::Deserialize;
use std::{
    sync::{
        Arc,
        atomic::{AtomicU64, AtomicUsize, Ordering},
    },
    time::{Duration, Instant},
};

/// Configuration for batch processor
#[derive(Debug, Clone)]
pub struct BatchProcessorConfig {
    /// Maximum batch size for processing
    pub max_batch_size: usize,
    /// Queue capacity for pending events
    pub queue_capacity: usize,
    /// Number of shards for the event queue
    pub shard_count: usize,
    /// Arena allocation pool size (bytes)
    pub arena_size: usize,
    /// Enable SIMD JSON parsing
    pub simd_enabled: bool,
}

impl Default for BatchProcessorConfig {
    fn default() -> Self {
        Self {
            max_batch_size: 10_000,
            queue_capacity: 1_000_000,
            shard_count: 16,
            arena_size: 64 * 1024 * 1024, // 64MB
            simd_enabled: true,
        }
    }
}

impl BatchProcessorConfig {
    /// High-throughput configuration for production use
    pub fn high_throughput() -> Self {
        Self {
            max_batch_size: 50_000,
            queue_capacity: 10_000_000,
            shard_count: 32,
            arena_size: 256 * 1024 * 1024, // 256MB
            simd_enabled: true,
        }
    }

    /// Low-latency configuration optimized for quick responses
    pub fn low_latency() -> Self {
        Self {
            max_batch_size: 1_000,
            queue_capacity: 100_000,
            shard_count: 8,
            arena_size: 16 * 1024 * 1024, // 16MB
            simd_enabled: true,
        }
    }
}

/// Raw event data for batch parsing
#[derive(Debug, Clone, Deserialize)]
pub struct RawEventData {
    pub event_type: String,
    pub entity_id: String,
    #[serde(default = "default_stream")]
    pub stream_id: String,
    pub data: serde_json::Value,
    #[serde(default)]
    pub metadata: Option<serde_json::Value>,
}

fn default_stream() -> String {
    "default".to_string()
}

/// Statistics for batch processing
#[derive(Debug, Clone)]
pub struct BatchProcessorStats {
    /// Total batches processed
    pub batches_processed: u64,
    /// Total events processed
    pub events_processed: u64,
    /// Total bytes parsed
    pub bytes_parsed: u64,
    /// Average batch size
    pub avg_batch_size: f64,
    /// Events per second
    pub events_per_sec: f64,
    /// Parse throughput in MB/s
    pub parse_throughput_mbps: f64,
    /// Current queue depth
    pub queue_depth: usize,
    /// Total processing time in nanoseconds
    pub total_time_ns: u64,
}

/// High-throughput batch event processor
///
/// Combines SIMD JSON parsing with lock-free queues for maximum throughput.
/// Designed to achieve 1M+ events/sec sustained ingestion.
pub struct BatchProcessor {
    config: BatchProcessorConfig,
    /// SIMD JSON parser
    json_parser: SimdJsonParser,
    /// Lock-free sharded queue for processed events
    event_queue: ShardedEventQueue,
    /// Lock-free metrics
    metrics: Arc<LockFreeMetrics>,
    /// Batch processing statistics
    batches_processed: AtomicU64,
    events_processed: AtomicU64,
    bytes_parsed: AtomicU64,
    total_time_ns: AtomicU64,
    /// Current arena pool index for round-robin
    arena_index: AtomicUsize,
}

impl BatchProcessor {
    /// Create a new batch processor with default configuration
    pub fn new() -> Self {
        Self::with_config(BatchProcessorConfig::default())
    }

    /// Create a new batch processor with custom configuration
    pub fn with_config(config: BatchProcessorConfig) -> Self {
        let event_queue = ShardedEventQueue::with_shards(config.queue_capacity, config.shard_count);

        Self {
            config,
            json_parser: SimdJsonParser::new(),
            event_queue,
            metrics: Arc::new(LockFreeMetrics::new()),
            batches_processed: AtomicU64::new(0),
            events_processed: AtomicU64::new(0),
            bytes_parsed: AtomicU64::new(0),
            total_time_ns: AtomicU64::new(0),
            arena_index: AtomicUsize::new(0),
        }
    }

    /// Process a batch of raw JSON event strings
    ///
    /// This is the main entry point for high-throughput ingestion.
    /// Uses SIMD-accelerated parsing and lock-free queuing.
    ///
    /// # Arguments
    /// * `json_events` - Vector of JSON strings to parse and process
    ///
    /// # Returns
    /// BatchResult with success/failure counts
    #[cfg_attr(feature = "hotpath", hotpath::measure)]
    pub fn process_batch(&self, json_events: Vec<String>) -> BatchResult {
        let start = Instant::now();
        let batch_size = json_events.len();

        let mut success_count = 0;
        let mut failure_count = 0;
        let mut bytes_parsed = 0usize;

        for json_str in json_events {
            bytes_parsed += json_str.len();

            if let Ok(()) = self.parse_and_queue_event(&json_str) {
                success_count += 1;
                self.metrics.record_ingest();
            } else {
                failure_count += 1;
                self.metrics.record_error();
            }
        }

        let duration = start.elapsed();
        self.record_batch_stats(batch_size, bytes_parsed, duration);

        BatchResult {
            success_count,
            failure_count,
            duration,
            events_per_sec: success_count as f64 / duration.as_secs_f64(),
        }
    }

    /// Process a batch of raw JSON bytes (more efficient - avoids string conversion)
    ///
    /// # Arguments
    /// * `json_bytes` - Vector of JSON byte slices to parse
    ///
    /// # Returns
    /// BatchResult with success/failure counts
    #[cfg_attr(feature = "hotpath", hotpath::measure)]
    pub fn process_batch_bytes(&self, mut json_bytes: Vec<Vec<u8>>) -> BatchResult {
        let start = Instant::now();
        let batch_size = json_bytes.len();

        let mut success_count = 0;
        let mut failure_count = 0;
        let mut bytes_parsed = 0usize;

        for bytes in &mut json_bytes {
            bytes_parsed += bytes.len();

            if let Ok(()) = self.parse_and_queue_bytes(bytes) {
                success_count += 1;
                self.metrics.record_ingest();
            } else {
                failure_count += 1;
                self.metrics.record_error();
            }
        }

        let duration = start.elapsed();
        self.record_batch_stats(batch_size, bytes_parsed, duration);

        BatchResult {
            success_count,
            failure_count,
            duration,
            events_per_sec: success_count as f64 / duration.as_secs_f64(),
        }
    }

    /// Process pre-parsed events directly (fastest path)
    ///
    /// Use this when events are already parsed (e.g., from Arrow/Parquet).
    pub fn process_events(&self, events: Vec<Event>) -> BatchResult {
        let start = Instant::now();
        let batch_size = events.len();

        let success_count = self.event_queue.try_push_batch(events);
        let failure_count = batch_size - success_count;

        self.metrics.record_ingest_batch(success_count as u64);
        if failure_count > 0 {
            for _ in 0..failure_count {
                self.metrics.record_error();
            }
        }

        let duration = start.elapsed();
        self.batches_processed.fetch_add(1, Ordering::Relaxed);
        self.events_processed
            .fetch_add(success_count as u64, Ordering::Relaxed);
        self.total_time_ns
            .fetch_add(duration.as_nanos() as u64, Ordering::Relaxed);

        BatchResult {
            success_count,
            failure_count,
            duration,
            events_per_sec: success_count as f64 / duration.as_secs_f64(),
        }
    }

    /// Parse and queue a single JSON event string
    fn parse_and_queue_event(&self, json_str: &str) -> Result<()> {
        let raw: RawEventData = self.json_parser.parse_str(json_str)?;

        let event = Event::from_strings(
            raw.event_type,
            raw.entity_id,
            raw.stream_id,
            raw.data,
            raw.metadata,
        )?;

        self.event_queue.try_push(event)
    }

    /// Parse and queue from bytes (SIMD path)
    fn parse_and_queue_bytes(&self, bytes: &mut [u8]) -> Result<()> {
        let raw: RawEventData = self.json_parser.parse(bytes)?;

        let event = Event::from_strings(
            raw.event_type,
            raw.entity_id,
            raw.stream_id,
            raw.data,
            raw.metadata,
        )?;

        self.event_queue.try_push(event)
    }

    /// Record batch statistics
    fn record_batch_stats(&self, batch_size: usize, bytes: usize, duration: Duration) {
        self.batches_processed.fetch_add(1, Ordering::Relaxed);
        self.events_processed
            .fetch_add(batch_size as u64, Ordering::Relaxed);
        self.bytes_parsed.fetch_add(bytes as u64, Ordering::Relaxed);
        self.total_time_ns
            .fetch_add(duration.as_nanos() as u64, Ordering::Relaxed);
    }

    /// Get a batch of processed events from the queue
    ///
    /// # Arguments
    /// * `max_count` - Maximum number of events to retrieve
    #[cfg_attr(feature = "hotpath", hotpath::measure)]
    pub fn get_batch(&self, max_count: usize) -> Vec<Event> {
        self.event_queue.try_pop_batch(max_count)
    }

    /// Get a single event from the queue
    pub fn get_event(&self) -> Option<Event> {
        self.event_queue.try_pop_any()
    }

    /// Get current queue depth
    pub fn queue_depth(&self) -> usize {
        self.event_queue.len()
    }

    /// Check if queue is empty
    pub fn is_queue_empty(&self) -> bool {
        self.event_queue.is_empty()
    }

    /// Get processing statistics
    pub fn stats(&self) -> BatchProcessorStats {
        let batches = self.batches_processed.load(Ordering::Relaxed);
        let events = self.events_processed.load(Ordering::Relaxed);
        let bytes = self.bytes_parsed.load(Ordering::Relaxed);
        let time_ns = self.total_time_ns.load(Ordering::Relaxed);

        let time_secs = time_ns as f64 / 1_000_000_000.0;
        let events_per_sec = if time_secs > 0.0 {
            events as f64 / time_secs
        } else {
            0.0
        };
        let throughput_mbps = if time_secs > 0.0 {
            (bytes as f64 / 1_000_000.0) / time_secs
        } else {
            0.0
        };

        BatchProcessorStats {
            batches_processed: batches,
            events_processed: events,
            bytes_parsed: bytes,
            avg_batch_size: if batches > 0 {
                events as f64 / batches as f64
            } else {
                0.0
            },
            events_per_sec,
            parse_throughput_mbps: throughput_mbps,
            queue_depth: self.event_queue.len(),
            total_time_ns: time_ns,
        }
    }

    /// Get JSON parser statistics
    pub fn parser_stats(&self) -> &SimdJsonStats {
        self.json_parser.stats()
    }

    /// Get metrics collector
    pub fn metrics(&self) -> Arc<LockFreeMetrics> {
        self.metrics.clone()
    }

    /// Get the event queue for direct access
    pub fn event_queue(&self) -> &ShardedEventQueue {
        &self.event_queue
    }

    /// Reset all statistics
    pub fn reset_stats(&self) {
        self.batches_processed.store(0, Ordering::Relaxed);
        self.events_processed.store(0, Ordering::Relaxed);
        self.bytes_parsed.store(0, Ordering::Relaxed);
        self.total_time_ns.store(0, Ordering::Relaxed);
        self.json_parser.reset_stats();
        self.metrics.reset();
    }
}

impl Default for BatchProcessor {
    fn default() -> Self {
        Self::new()
    }
}

/// Result of batch processing operation
#[derive(Debug, Clone)]
pub struct BatchResult {
    /// Number of successfully processed events
    pub success_count: usize,
    /// Number of failed events
    pub failure_count: usize,
    /// Total processing duration
    pub duration: Duration,
    /// Events processed per second
    pub events_per_sec: f64,
}

impl BatchResult {
    /// Get total events (success + failure)
    pub fn total(&self) -> usize {
        self.success_count + self.failure_count
    }

    /// Get success rate (0.0 to 1.0)
    pub fn success_rate(&self) -> f64 {
        let total = self.total();
        if total > 0 {
            self.success_count as f64 / total as f64
        } else {
            1.0
        }
    }
}

/// Arena-backed batch buffer for zero-allocation processing
///
/// Uses bumpalo for fast arena allocation during batch processing.
/// All allocations are freed together when the arena is reset.
pub struct ArenaBatchBuffer {
    arena: Bump,
    capacity: usize,
}

impl ArenaBatchBuffer {
    /// Create a new arena buffer with specified capacity
    pub fn new(capacity_bytes: usize) -> Self {
        Self {
            arena: Bump::with_capacity(capacity_bytes),
            capacity: capacity_bytes,
        }
    }

    /// Allocate a byte slice in the arena
    pub fn alloc_bytes(&self, data: &[u8]) -> &[u8] {
        self.arena.alloc_slice_copy(data)
    }

    /// Allocate a string in the arena
    pub fn alloc_str(&self, s: &str) -> &str {
        self.arena.alloc_str(s)
    }

    /// Get current arena allocation
    pub fn allocated(&self) -> usize {
        self.arena.allocated_bytes()
    }

    /// Reset the arena (frees all allocations)
    pub fn reset(&mut self) {
        self.arena.reset();
    }

    /// Get capacity
    pub fn capacity(&self) -> usize {
        self.capacity
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn create_test_json(id: u32) -> String {
        json!({
            "event_type": "test.event",
            "entity_id": format!("entity-{}", id),
            "stream_id": "test-stream",
            "data": {"value": id}
        })
        .to_string()
    }

    #[test]
    fn test_create_batch_processor() {
        let processor = BatchProcessor::new();
        assert!(processor.is_queue_empty());
        assert_eq!(processor.queue_depth(), 0);
    }

    #[test]
    fn test_process_single_batch() {
        let processor = BatchProcessor::new();

        let events: Vec<String> = (0..100).map(create_test_json).collect();
        let result = processor.process_batch(events);

        assert_eq!(result.success_count, 100);
        assert_eq!(result.failure_count, 0);
        assert_eq!(processor.queue_depth(), 100);
    }

    #[test]
    fn test_process_batch_bytes() {
        let processor = BatchProcessor::new();

        let events: Vec<Vec<u8>> = (0..50).map(|i| create_test_json(i).into_bytes()).collect();
        let result = processor.process_batch_bytes(events);

        assert_eq!(result.success_count, 50);
        assert_eq!(result.failure_count, 0);
    }

    #[test]
    fn test_get_batch() {
        let processor = BatchProcessor::new();

        let events: Vec<String> = (0..100).map(create_test_json).collect();
        processor.process_batch(events);

        let batch = processor.get_batch(30);
        assert_eq!(batch.len(), 30);
        assert_eq!(processor.queue_depth(), 70);
    }

    #[test]
    fn test_stats() {
        let processor = BatchProcessor::new();

        let events: Vec<String> = (0..100).map(create_test_json).collect();
        processor.process_batch(events);

        let stats = processor.stats();
        assert_eq!(stats.batches_processed, 1);
        assert_eq!(stats.events_processed, 100);
        assert!(stats.events_per_sec > 0.0);
    }

    #[test]
    fn test_invalid_json() {
        let processor = BatchProcessor::new();

        let events = vec![
            create_test_json(1),
            "invalid json".to_string(),
            create_test_json(3),
        ];
        let result = processor.process_batch(events);

        assert_eq!(result.success_count, 2);
        assert_eq!(result.failure_count, 1);
    }

    #[test]
    fn test_batch_result_metrics() {
        let result = BatchResult {
            success_count: 90,
            failure_count: 10,
            duration: Duration::from_millis(100),
            events_per_sec: 900.0,
        };

        assert_eq!(result.total(), 100);
        assert!((result.success_rate() - 0.9).abs() < 0.001);
    }

    #[test]
    fn test_arena_batch_buffer() {
        let mut buffer = ArenaBatchBuffer::new(1024);

        let s1 = buffer.alloc_str("hello");
        let s2 = buffer.alloc_str("world");

        assert_eq!(s1, "hello");
        assert_eq!(s2, "world");
        let allocated_before = buffer.allocated();
        assert!(allocated_before > 0);

        // Reset makes memory available for reuse (but allocated_bytes may not change)
        buffer.reset();

        // After reset, new allocations should reuse the memory
        let s3 = buffer.alloc_str("test");
        assert_eq!(s3, "test");

        // Verify the buffer is functional after reset
        assert!(buffer.capacity() >= 1024);
    }

    #[test]
    fn test_config_presets() {
        let default = BatchProcessorConfig::default();
        let high_throughput = BatchProcessorConfig::high_throughput();
        let low_latency = BatchProcessorConfig::low_latency();

        assert!(high_throughput.queue_capacity > default.queue_capacity);
        assert!(low_latency.max_batch_size < default.max_batch_size);
    }

    #[test]
    fn test_concurrent_processing() {
        let processor = Arc::new(BatchProcessor::new());

        std::thread::scope(|s| {
            // Multiple producer threads
            for t in 0..4 {
                let proc = processor.clone();
                s.spawn(move || {
                    let events: Vec<String> =
                        (0..100).map(|i| create_test_json(t * 100 + i)).collect();
                    proc.process_batch(events);
                });
            }
        });

        // All 400 events should be in the queue
        assert_eq!(processor.queue_depth(), 400);
    }

    #[test]
    fn test_process_events_direct() {
        let processor = BatchProcessor::new();

        let events: Vec<Event> = (0..50)
            .map(|i| {
                Event::from_strings(
                    "test.event".to_string(),
                    format!("entity-{i}"),
                    "test-stream".to_string(),
                    json!({"value": i}),
                    None,
                )
                .unwrap()
            })
            .collect();

        let result = processor.process_events(events);
        assert_eq!(result.success_count, 50);
        assert_eq!(processor.queue_depth(), 50);
    }
}