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
// Project: hyperi-rustlib
// File: src/worker/batch.rs
// Purpose: BatchProcessor trait and BatchPipeline for parallel-then-sequential processing
// Language: Rust
//
// License: FSL-1.1-ALv2
// Copyright: (c) 2026 HYPERI PTY LIMITED
//! Batch processing framework for DFE pipeline parallelisation.
//!
//! Provides the [`BatchProcessor`] trait for defining parallel-safe message
//! processing, and [`BatchPipeline`] for orchestrating the parallel (rayon) →
//! sequential (state mutation) pipeline.
//!
//! ## The Pattern
//!
//! Every DFE app follows the same structure:
//!
//! 1. **Parallel phase:** Process each message through a pure `&self` function
//! (parse, route, transform, enrich) — via rayon `process_batch()`
//! 2. **Sequential phase:** Apply results to mutable state (buffer push,
//! mark_pending, stats update, DLQ routing)
//!
//! The [`BatchProcessor`] trait captures phase 1. Phase 2 is app-specific
//! (each app has different buffers, caches, and sinks).
//!
//! ## Example
//!
//! ```rust,ignore
//! use hyperi_rustlib::worker::{BatchPipeline, BatchProcessor};
//!
//! struct MyProcessor<'a> { router: &'a Router, ... }
//!
//! impl BatchProcessor for MyProcessor<'_> {
//! type Input = KafkaMessage;
//! type Output = ProcessedMessage;
//! type Error = MyError;
//!
//! fn process(&self, msg: &KafkaMessage) -> Result<ProcessedMessage, MyError> {
//! let parsed = sonic_rs::from_slice(&msg.payload)?;
//! let table = self.router.route(&parsed)?;
//! Ok(ProcessedMessage { table, data: parsed })
//! }
//! }
//!
//! // In event loop:
//! let processor = MyProcessor { router: &router, ... };
//! let results = pipeline.process_batch(&processor, &batch);
//! drop(processor); // release immutable borrows
//! // Sequential phase: apply results to mutable state
//! ```
use Arc;
use AdaptiveWorkerPool;
use PipelineStats;
/// Trait for parallel-safe message processing.
///
/// Implement this with a struct that holds only `&` references to immutable
/// dependencies. The `process` method must be pure — no mutable state, no I/O,
/// no `.await`. Safe for rayon `par_iter()`.
///
/// The struct is typically created per-batch in the event loop (borrows released
/// before the sequential phase begins). The borrow checker enforces this.
/// Orchestrates parallel batch processing via [`AdaptiveWorkerPool`].
///
/// Wraps the worker pool with common DFE pipeline concerns: stats tracking,
/// memory accounting, and metrics emission. Apps provide a [`BatchProcessor`]
/// implementation; the pipeline handles the rest.