hyperi-rustlib 2.7.1

Opinionated, drop-in Rust toolkit for production services at scale. The patterns from blog posts as actual code: 8-layer config cascade, structured logging with PII masking, Prometheus + OpenTelemetry, Kafka/gRPC transports, tiered disk-spillover, adaptive worker pools, graceful shutdown.
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
// Project:   hyperi-rustlib
// File:      src/worker/engine/mod.rs
// Purpose:   SIMD-optimised batch processing engine for DFE pipelines
// Language:  Rust
//
// License:   FSL-1.1-ALv2
// Copyright: (c) 2026 HYPERI PTY LIMITED

pub mod config;
pub mod intern;
pub mod metrics;
pub mod parse;
pub mod pre_route;
pub mod types;

pub use config::{BatchProcessingConfig, ParseErrorAction, PreRouteFilterConfig};
pub use intern::FieldInterner;
pub use types::{MessageMetadata, ParsedMessage, PreRouteResult, RawMessage};

/// Errors returned by [`BatchEngine::run`] and [`BatchEngine::run_raw`].
///
/// Only available when the `transport` feature is enabled.
#[cfg(feature = "transport")]
#[derive(Debug, thiserror::Error)]
pub enum EngineError {
    /// Transport receive or commit failed.
    #[error("transport error: {0}")]
    Transport(#[from] crate::TransportError),
    /// Sink callback returned an error.
    #[error("sink error: {0}")]
    Sink(String),
    /// Shutdown was requested via cancellation token.
    #[error("shutdown")]
    Shutdown,
}

use std::sync::Arc;

use rayon::prelude::*;

use super::pool::AdaptiveWorkerPool;
use super::stats::PipelineStats;

use self::pre_route::{PreRouteOutcome, apply_filters, extract_routing_field, filters_from_config};
use self::types::PayloadFormat;
use super::config::WorkerPoolConfig;

/// Core batch processing engine for DFE pipelines.
///
/// Provides two processing modes:
///
/// - [`process_mid_tier`](Self::process_mid_tier) — parse JSON via SIMD, extract
///   known fields, apply pre-route filters, then parallel transform via rayon.
///   The standard path for most DFE apps (loader, archiver, transforms).
///
/// - [`process_raw`](Self::process_raw) — skip parsing, apply pre-route on raw
///   bytes, then parallel transform via rayon. For apps that handle raw bytes
///   (receiver, binary protocols).
///
/// Both modes chunk large batches, track stats atomically, and pause between
/// chunks when memory pressure is detected.
pub struct BatchEngine {
    config: BatchProcessingConfig,
    pool: Arc<AdaptiveWorkerPool>,
    stats: Arc<PipelineStats>,
    interner: Arc<FieldInterner>,
    filters: Vec<pre_route::PreRouteFilter>,
    #[cfg(feature = "memory")]
    memory_guard: Option<Arc<crate::memory::MemoryGuard>>,
}

impl BatchEngine {
    /// Create a standalone engine with its own worker pool.
    ///
    /// Uses `WorkerPoolConfig::default()` for the pool. Prefer
    /// [`with_pool`](Self::with_pool) when a `ServiceRuntime` pool exists.
    #[must_use]
    pub fn new(config: BatchProcessingConfig) -> Self {
        let pool = Arc::new(AdaptiveWorkerPool::new(WorkerPoolConfig::default()));
        Self::with_pool(pool, config)
    }

    /// Create an engine that reuses an existing worker pool.
    ///
    /// This is the preferred constructor when `ServiceRuntime` is available,
    /// as it avoids creating a second rayon thread pool.
    #[must_use]
    pub fn with_pool(pool: Arc<AdaptiveWorkerPool>, config: BatchProcessingConfig) -> Self {
        let known_refs: Vec<&str> = config.known_fields.iter().map(String::as_str).collect();
        let interner = Arc::new(FieldInterner::with_known_fields(&known_refs));
        let filters = filters_from_config(&config.pre_route_filters);
        Self {
            config,
            pool,
            stats: Arc::new(PipelineStats::new()),
            interner,
            filters,
            #[cfg(feature = "memory")]
            memory_guard: None,
        }
    }

    /// Load configuration from the cascade and create a standalone engine.
    ///
    /// # Errors
    ///
    /// Returns `ConfigError` if the cascade contains invalid data.
    pub fn from_cascade(key: &str) -> Result<Self, crate::config::ConfigError> {
        let config = BatchProcessingConfig::from_cascade(key)?;
        Ok(Self::new(config))
    }

    /// Pipeline statistics (atomic, lock-free).
    #[must_use]
    pub fn stats(&self) -> &Arc<PipelineStats> {
        &self.stats
    }

    /// Underlying worker pool.
    #[must_use]
    pub fn pool(&self) -> &Arc<AdaptiveWorkerPool> {
        &self.pool
    }

    /// Engine configuration.
    #[must_use]
    pub fn config(&self) -> &BatchProcessingConfig {
        &self.config
    }

    /// Auto-wire engine with infrastructure components.
    ///
    /// Called by `ServiceRuntime::build()`. Apps never call this directly.
    pub fn auto_wire(
        &mut self,
        metrics_manager: &crate::metrics::MetricsManager,
        #[cfg(feature = "memory")] memory_guard: Option<&Arc<crate::memory::MemoryGuard>>,
    ) {
        metrics::register(metrics_manager, &self.config);

        #[cfg(feature = "memory")]
        if let Some(guard) = memory_guard {
            self.memory_guard = Some(Arc::clone(guard));
        }
    }

    /// Parse, filter, and transform a batch of raw messages.
    ///
    /// Pipeline phases per chunk:
    /// 1. **Pre-route** — SIMD field extraction + filter evaluation (sequential, ~100 ns/msg)
    /// 2. **Parse** — `sonic_rs::from_slice` + known-field extraction (sequential, ~1-5 µs/msg)
    /// 3. **Transform** — user closure via rayon `par_iter_mut` (parallel)
    ///
    /// Results contain one entry per non-filtered message. Filtered messages are
    /// silently removed (their commit tokens remain accessible via the original
    /// slice). DLQ'd and parse-error messages produce `Err` entries.
    pub fn process_mid_tier<O, E, F>(
        &self,
        messages: &[RawMessage],
        transform: F,
    ) -> Vec<Result<O, E>>
    where
        O: Send,
        E: Send + From<String>,
        F: Fn(&mut ParsedMessage) -> Result<O, E> + Sync,
    {
        if messages.is_empty() {
            return Vec::new();
        }

        let chunk_size = if self.config.max_chunk_size == 0 {
            messages.len()
        } else {
            self.config.max_chunk_size
        };

        let has_routing = self.config.routing_field.is_some();
        let mut all_results = Vec::with_capacity(messages.len());

        for chunk in messages.chunks(chunk_size) {
            self.stats.add_received(chunk.len() as u64);

            // Accumulate bytes received
            let chunk_bytes: u64 = chunk.iter().map(|m| m.payload.len() as u64).sum();
            self.stats.add_bytes_received(chunk_bytes);

            // Phase 1 + 2: Pre-route and parse, building ParsedMessage vec.
            // Track which messages are included (not filtered).
            let mut parsed_msgs: Vec<Result<ParsedMessage, String>> =
                Vec::with_capacity(chunk.len());

            for msg in chunk {
                // Phase 1: Pre-route
                if has_routing {
                    let field_name = self.config.routing_field.as_ref().expect("checked above");
                    let extraction = extract_routing_field(&msg.payload, field_name);
                    let outcome = apply_filters(&extraction, &self.filters);

                    match outcome {
                        PreRouteOutcome::Continue => {}
                        PreRouteOutcome::Filtered => {
                            self.stats.incr_filtered();
                            continue; // skip this message entirely
                        }
                        PreRouteOutcome::Dlq(reason) => {
                            self.stats.incr_dlq();
                            self.stats.incr_errors();
                            parsed_msgs.push(Err(reason));
                            continue;
                        }
                    }
                }

                // Phase 2: Parse
                let format = match msg.metadata.format {
                    PayloadFormat::Auto => PayloadFormat::detect(&msg.payload),
                    other => other,
                };

                match parse::parse_payload(&msg.payload, format) {
                    Ok(value) => {
                        let extracted = self.interner.extract_known(&value);
                        parsed_msgs.push(Ok(ParsedMessage::Parsed {
                            value,
                            raw: msg.payload.clone(),
                            format,
                            key: msg.key.clone(),
                            headers: msg.headers.clone(),
                            metadata: msg.metadata.clone(),
                            extracted,
                        }));
                    }
                    Err(e) => {
                        self.stats.incr_errors();
                        match self.config.parse_error_action {
                            ParseErrorAction::Dlq => {
                                self.stats.incr_dlq();
                                parsed_msgs.push(Err(format!("parse error: {e}")));
                            }
                            ParseErrorAction::Skip => {
                                // Counted in errors above, not added to results
                            }
                            ParseErrorAction::FailBatch => {
                                // Return all accumulated results + this error,
                                // then stop processing the chunk.
                                parsed_msgs.push(Err(format!("parse error (fail_batch): {e}")));
                                let results: Vec<Result<O, E>> = parsed_msgs
                                    .into_iter()
                                    .map(|r| match r {
                                        Ok(_) => Err(E::from(
                                            "batch failed due to parse error".to_string(),
                                        )),
                                        Err(reason) => Err(E::from(reason)),
                                    })
                                    .collect();
                                all_results.extend(results);
                                return all_results;
                            }
                        }
                    }
                }
            }

            // Phase 3: Parallel transform via rayon.
            // Split into ok/err: transform only the Ok entries.
            let mut indexed: Vec<(usize, Result<ParsedMessage, String>)> =
                parsed_msgs.into_iter().enumerate().collect();

            // Separate errors from parseable messages
            let mut chunk_results: Vec<(usize, Result<O, E>)> = Vec::with_capacity(indexed.len());
            let mut to_transform: Vec<(usize, ParsedMessage)> = Vec::with_capacity(indexed.len());

            for (idx, item) in indexed.drain(..) {
                match item {
                    Ok(pm) => to_transform.push((idx, pm)),
                    Err(reason) => chunk_results.push((idx, Err(E::from(reason)))),
                }
            }

            // Parallel transform
            let transformed: Vec<(usize, Result<O, E>)> = self.pool.install(|| {
                to_transform
                    .into_par_iter()
                    .map(|(idx, mut pm)| {
                        let result = transform(&mut pm);
                        (idx, result)
                    })
                    .collect()
            });

            chunk_results.extend(transformed);

            // Sort by original index to preserve order
            chunk_results.sort_by_key(|(idx, _)| *idx);

            // Update stats
            let ok_count = chunk_results.iter().filter(|(_, r)| r.is_ok()).count();
            self.stats.add_processed(ok_count as u64);

            all_results.extend(chunk_results.into_iter().map(|(_, r)| r));

            // Memory pressure check between chunks
            self.check_memory_pressure();
        }

        all_results
    }

    /// Pre-route and transform a batch of raw messages without parsing.
    ///
    /// The transform closure receives immutable `&RawMessage` references.
    /// Use this for apps that handle raw bytes directly (e.g. receiver forwarding).
    pub fn process_raw<O, E, F>(&self, messages: &[RawMessage], transform: F) -> Vec<Result<O, E>>
    where
        O: Send,
        E: Send + From<String>,
        F: Fn(&RawMessage) -> Result<O, E> + Sync,
    {
        if messages.is_empty() {
            return Vec::new();
        }

        let chunk_size = if self.config.max_chunk_size == 0 {
            messages.len()
        } else {
            self.config.max_chunk_size
        };

        let has_routing = self.config.routing_field.is_some();
        let mut all_results = Vec::with_capacity(messages.len());

        for chunk in messages.chunks(chunk_size) {
            self.stats.add_received(chunk.len() as u64);

            let chunk_bytes: u64 = chunk.iter().map(|m| m.payload.len() as u64).sum();
            self.stats.add_bytes_received(chunk_bytes);

            // Phase 1: Pre-route filter
            let to_process: Vec<&RawMessage> = if has_routing {
                let field_name = self.config.routing_field.as_ref().expect("checked above");
                let mut passed = Vec::with_capacity(chunk.len());
                for msg in chunk {
                    let extraction = extract_routing_field(&msg.payload, field_name);
                    let outcome = apply_filters(&extraction, &self.filters);
                    match outcome {
                        PreRouteOutcome::Continue => passed.push(msg),
                        PreRouteOutcome::Filtered => {
                            self.stats.incr_filtered();
                        }
                        PreRouteOutcome::Dlq(reason) => {
                            self.stats.incr_dlq();
                            self.stats.incr_errors();
                            all_results.push(Err(E::from(reason)));
                        }
                    }
                }
                passed
            } else {
                chunk.iter().collect()
            };

            // Phase 2: Parallel transform via process_batch
            let results = self.pool.process_batch(&to_process, |msg| transform(msg));

            let ok_count = results.iter().filter(|r| r.is_ok()).count();
            self.stats.add_processed(ok_count as u64);

            all_results.extend(results);

            self.check_memory_pressure();
        }

        all_results
    }

    /// Transport-wired async run loop for mid-tier (parsed JSON) processing.
    ///
    /// Receives up to `config.max_chunk_size` messages per iteration, converts them
    /// to `RawMessage`, runs [`process_mid_tier`](Self::process_mid_tier), calls the
    /// sink, then commits transport tokens only on sink success.
    ///
    /// Stops cleanly when `shutdown` is cancelled.
    ///
    /// # Errors
    ///
    /// Returns `EngineError::Transport` if recv or commit fails fatally.
    /// Returns `EngineError::Sink` if the sink callback errors (commit skipped).
    #[cfg(feature = "transport")]
    pub async fn run<R, O, E, Transform, Sink>(
        &self,
        receiver: &R,
        shutdown: tokio_util::sync::CancellationToken,
        transform: Transform,
        mut sink: Sink,
    ) -> Result<(), EngineError>
    where
        R: crate::transport::TransportReceiver,
        O: Send + 'static,
        E: Send + From<String> + std::fmt::Display + 'static,
        Transform: Fn(&mut ParsedMessage) -> Result<O, E> + Sync,
        Sink: FnMut(Vec<Result<O, E>>) -> Result<(), EngineError>,
    {
        tracing::info!(
            chunk_size = self.config.max_chunk_size,
            routing_field = ?self.config.routing_field,
            "BatchEngine starting"
        );

        loop {
            tokio::select! {
                biased;
                () = shutdown.cancelled() => {
                    tracing::info!("BatchEngine shutting down");
                    return Ok(());
                }
                recv_result = receiver.recv(self.config.max_chunk_size) => {
                    let messages = recv_result.map_err(EngineError::Transport)?;
                    if messages.is_empty() {
                        continue;
                    }

                    // Collect commit tokens before converting (move happens below).
                    let tokens: Vec<R::Token> = messages.iter()
                        .map(|m| m.token.clone())
                        .collect();

                    // Convert to RawMessage (type-erased).
                    let raw: Vec<RawMessage> = messages.into_iter()
                        .map(RawMessage::from)
                        .collect();

                    // Process: pre-route + parse + parallel transform.
                    let results = self.process_mid_tier(&raw, &transform);

                    // Sink: commit only on success.
                    if let Err(e) = sink(results) {
                        tracing::error!(error = %e, "Sink failed, skipping commit");
                        continue;
                    }

                    if let Err(e) = receiver.commit(&tokens).await {
                        tracing::error!(error = %e, "Commit failed");
                    }
                }
            }
        }
    }

    /// Transport-wired async run loop for raw byte processing.
    ///
    /// Like [`run`](Self::run) but uses [`process_raw`](Self::process_raw): the
    /// transform closure receives `&RawMessage` bytes without JSON parsing.
    ///
    /// # Errors
    ///
    /// Returns `EngineError::Transport` if recv or commit fails fatally.
    /// Returns `EngineError::Sink` if the sink callback errors (commit skipped).
    #[cfg(feature = "transport")]
    pub async fn run_raw<R, O, E, Transform, Sink>(
        &self,
        receiver: &R,
        shutdown: tokio_util::sync::CancellationToken,
        transform: Transform,
        mut sink: Sink,
    ) -> Result<(), EngineError>
    where
        R: crate::transport::TransportReceiver,
        O: Send + 'static,
        E: Send + From<String> + std::fmt::Display + 'static,
        Transform: Fn(&RawMessage) -> Result<O, E> + Sync,
        Sink: FnMut(Vec<Result<O, E>>) -> Result<(), EngineError>,
    {
        tracing::info!(
            chunk_size = self.config.max_chunk_size,
            "BatchEngine (raw) starting"
        );

        loop {
            tokio::select! {
                biased;
                () = shutdown.cancelled() => {
                    tracing::info!("BatchEngine (raw) shutting down");
                    return Ok(());
                }
                recv_result = receiver.recv(self.config.max_chunk_size) => {
                    let messages = recv_result.map_err(EngineError::Transport)?;
                    if messages.is_empty() {
                        continue;
                    }

                    let tokens: Vec<R::Token> = messages.iter()
                        .map(|m| m.token.clone())
                        .collect();

                    let raw: Vec<RawMessage> = messages.into_iter()
                        .map(RawMessage::from)
                        .collect();

                    let results = self.process_raw(&raw, &transform);

                    if let Err(e) = sink(results) {
                        tracing::error!(error = %e, "Sink failed (raw), skipping commit");
                        continue;
                    }

                    if let Err(e) = receiver.commit(&tokens).await {
                        tracing::error!(error = %e, "Commit failed (raw)");
                    }
                }
            }
        }
    }

    /// Transport-wired async run loop with full control over commit semantics.
    ///
    /// Like [`run`](Self::run) but with two key differences:
    ///
    /// 1. **Async sink** — the sink is an async closure, enabling async I/O
    ///    (ClickHouse inserts, Kafka produce, storage writes) inside the sink.
    ///
    /// 2. **Sink-managed commits** — the sink receives commit tokens and decides
    ///    when to commit. The engine does NOT auto-commit. This enables deferred
    ///    commit patterns (e.g., commit after ClickHouse flush, not after buffer push).
    ///
    /// An optional ticker fires on a fixed interval within the select! loop,
    /// enabling flush timers and periodic maintenance without breaking out of
    /// the engine loop.
    ///
    /// # Errors
    ///
    /// Returns `EngineError::Transport` if recv fails fatally.
    /// Returns `EngineError::Sink` if the sink callback errors.
    #[cfg(feature = "transport")]
    pub async fn run_async<R, O, E, Transform, Sink, SinkFut, Ticker, TickerFut>(
        &self,
        receiver: &R,
        shutdown: tokio_util::sync::CancellationToken,
        transform: Transform,
        mut sink: Sink,
        ticker: Option<(std::time::Duration, Ticker)>,
    ) -> Result<(), EngineError>
    where
        R: crate::transport::TransportReceiver,
        O: Send + 'static,
        E: Send + From<String> + std::fmt::Display + 'static,
        Transform: Fn(&mut ParsedMessage) -> Result<O, E> + Sync,
        Sink: FnMut(Vec<Result<O, E>>, Vec<R::Token>) -> SinkFut,
        SinkFut: std::future::Future<Output = Result<(), EngineError>>,
        Ticker: FnMut() -> TickerFut,
        TickerFut: std::future::Future<Output = Result<(), EngineError>>,
    {
        tracing::info!(
            chunk_size = self.config.max_chunk_size,
            routing_field = ?self.config.routing_field,
            ticker = ticker.is_some(),
            "BatchEngine (async) starting"
        );

        // Ticker interval — if None, create a dummy that never fires.
        let mut tick_interval = ticker.as_ref().map(|(d, _)| tokio::time::interval(*d));
        let mut ticker_fn = ticker.map(|(_, f)| f);

        // Consume the first tick (fires immediately).
        if let Some(ref mut interval) = tick_interval {
            interval.tick().await;
        }

        loop {
            tokio::select! {
                biased;

                () = shutdown.cancelled() => {
                    tracing::info!("BatchEngine (async) shutting down");
                    return Ok(());
                }

                _ = async {
                    match tick_interval.as_mut() {
                        Some(interval) => interval.tick().await,
                        None => std::future::pending().await,
                    }
                } => {
                    if let Some(ref mut f) = ticker_fn
                        && let Err(e) = f().await
                    {
                        tracing::error!(error = %e, "Ticker failed");
                    }
                }

                recv_result = receiver.recv(self.config.max_chunk_size) => {
                    let messages = recv_result.map_err(EngineError::Transport)?;
                    if messages.is_empty() {
                        continue;
                    }

                    // Collect commit tokens before converting (move happens below).
                    let tokens: Vec<R::Token> = messages.iter()
                        .map(|m| m.token.clone())
                        .collect();

                    // Convert to RawMessage (type-erased).
                    let raw: Vec<RawMessage> = messages.into_iter()
                        .map(RawMessage::from)
                        .collect();

                    // Process: pre-route + parse + parallel transform.
                    let results = self.process_mid_tier(&raw, &transform);

                    // Sink receives results AND tokens — sink decides when to commit.
                    if let Err(e) = sink(results, tokens).await {
                        tracing::error!(error = %e, "Sink failed (async)");
                    }
                }
            }
        }
    }

    /// Transport-wired async run loop for raw byte processing with full control.
    ///
    /// Like [`run_async`](Self::run_async) but uses [`process_raw`](Self::process_raw):
    /// the transform closure receives `&RawMessage` bytes without JSON parsing.
    ///
    /// # Errors
    ///
    /// Returns `EngineError::Transport` if recv fails fatally.
    /// Returns `EngineError::Sink` if the sink callback errors.
    #[cfg(feature = "transport")]
    pub async fn run_raw_async<R, O, E, Transform, Sink, SinkFut, Ticker, TickerFut>(
        &self,
        receiver: &R,
        shutdown: tokio_util::sync::CancellationToken,
        transform: Transform,
        mut sink: Sink,
        ticker: Option<(std::time::Duration, Ticker)>,
    ) -> Result<(), EngineError>
    where
        R: crate::transport::TransportReceiver,
        O: Send + 'static,
        E: Send + From<String> + std::fmt::Display + 'static,
        Transform: Fn(&RawMessage) -> Result<O, E> + Sync,
        Sink: FnMut(Vec<Result<O, E>>, Vec<R::Token>) -> SinkFut,
        SinkFut: std::future::Future<Output = Result<(), EngineError>>,
        Ticker: FnMut() -> TickerFut,
        TickerFut: std::future::Future<Output = Result<(), EngineError>>,
    {
        tracing::info!(
            chunk_size = self.config.max_chunk_size,
            ticker = ticker.is_some(),
            "BatchEngine (raw async) starting"
        );

        let mut tick_interval = ticker.as_ref().map(|(d, _)| tokio::time::interval(*d));
        let mut ticker_fn = ticker.map(|(_, f)| f);

        if let Some(ref mut interval) = tick_interval {
            interval.tick().await;
        }

        loop {
            tokio::select! {
                biased;

                () = shutdown.cancelled() => {
                    tracing::info!("BatchEngine (raw async) shutting down");
                    return Ok(());
                }

                _ = async {
                    match tick_interval.as_mut() {
                        Some(interval) => interval.tick().await,
                        None => std::future::pending().await,
                    }
                } => {
                    if let Some(ref mut f) = ticker_fn
                        && let Err(e) = f().await
                    {
                        tracing::error!(error = %e, "Ticker (raw) failed");
                    }
                }

                recv_result = receiver.recv(self.config.max_chunk_size) => {
                    let messages = recv_result.map_err(EngineError::Transport)?;
                    if messages.is_empty() {
                        continue;
                    }

                    let tokens: Vec<R::Token> = messages.iter()
                        .map(|m| m.token.clone())
                        .collect();

                    let raw: Vec<RawMessage> = messages.into_iter()
                        .map(RawMessage::from)
                        .collect();

                    let results = self.process_raw(&raw, &transform);

                    if let Err(e) = sink(results, tokens).await {
                        tracing::error!(error = %e, "Sink failed (raw async)");
                    }
                }
            }
        }
    }

    /// Pause between chunks when memory pressure is detected.
    ///
    /// Uses `std::thread::sleep` (not tokio) because `process_mid_tier` and
    /// `process_raw` are sync methods that run within rayon context. The pause
    /// happens between chunks (cold path), not per message.
    #[allow(clippy::unused_self)]
    fn check_memory_pressure(&self) {
        #[cfg(feature = "memory")]
        if let Some(guard) = &self.memory_guard
            && guard.under_pressure()
        {
            tracing::warn!(
                pause_ms = self.config.memory_pressure_pause_ms,
                "BatchEngine: memory pressure detected, pausing between chunks"
            );
            std::thread::sleep(std::time::Duration::from_millis(
                self.config.memory_pressure_pause_ms,
            ));
        }
    }
}

impl std::fmt::Debug for BatchEngine {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut s = f.debug_struct("BatchEngine");
        s.field("config", &self.config)
            .field("pool_max_threads", &self.pool.max_threads())
            .field("stats", &self.stats.snapshot())
            .field("interner_len", &self.interner.len())
            .field("filters", &self.filters);
        #[cfg(feature = "memory")]
        s.field("memory_guard", &self.memory_guard.is_some());
        s.finish()
    }
}

#[cfg(test)]
mod engine_tests {
    use super::*;
    use bytes::Bytes;

    fn make_json_messages(n: usize) -> Vec<RawMessage> {
        (0..n)
            .map(|i| RawMessage {
                payload: Bytes::from(format!(r#"{{"_table":"events","id":{i}}}"#)),
                key: None,
                headers: vec![],
                metadata: MessageMetadata {
                    timestamp_ms: None,
                    format: types::PayloadFormat::Json,
                    commit_token: None,
                },
            })
            .collect()
    }

    fn default_engine() -> BatchEngine {
        BatchEngine::new(BatchProcessingConfig::default())
    }

    #[test]
    fn process_mid_tier_basic() {
        let engine = default_engine();
        let msgs = make_json_messages(100);

        let results: Vec<Result<String, String>> = engine.process_mid_tier(&msgs, |pm| {
            Ok(pm
                .field("_table")
                .and_then(|v| sonic_rs::JsonValueTrait::as_str(v))
                .unwrap_or("unknown")
                .to_string())
        });

        assert_eq!(results.len(), 100);
        assert!(results.iter().all(|r| r.is_ok()));
        assert_eq!(results[0].as_ref().unwrap(), "events");
    }

    #[test]
    fn process_mid_tier_parse_error() {
        let engine = default_engine();
        let mut msgs = make_json_messages(2);
        // Insert an invalid JSON message
        msgs.insert(
            1,
            RawMessage {
                payload: Bytes::from_static(b"not json {{{"),
                key: None,
                headers: vec![],
                metadata: MessageMetadata {
                    timestamp_ms: None,
                    format: types::PayloadFormat::Json,
                    commit_token: None,
                },
            },
        );

        let results: Vec<Result<String, String>> =
            engine.process_mid_tier(&msgs, |pm| Ok(pm.raw_payload().len().to_string()));

        // 2 successful + 1 error (DLQ by default)
        assert_eq!(results.len(), 3);
        assert!(results[0].is_ok());
        assert!(results[1].is_err());
        assert!(results[1].as_ref().unwrap_err().contains("parse error"));
        assert!(results[2].is_ok());
    }

    #[test]
    fn process_mid_tier_empty_batch() {
        let engine = default_engine();
        let results: Vec<Result<(), String>> = engine.process_mid_tier(&[], |_| Ok(()));
        assert!(results.is_empty());
    }

    #[test]
    fn process_mid_tier_respects_chunk_size() {
        let config = BatchProcessingConfig {
            max_chunk_size: 50,
            ..Default::default()
        };
        let engine = BatchEngine::new(config);
        let msgs = make_json_messages(120);

        let results: Vec<Result<usize, String>> =
            engine.process_mid_tier(&msgs, |pm| Ok(pm.raw_payload().len()));

        assert_eq!(results.len(), 120);
        assert!(results.iter().all(|r| r.is_ok()));
        // Stats should show 120 received across 3 chunks (50+50+20)
        let snap = engine.stats().snapshot();
        assert_eq!(snap.received, 120);
    }

    #[test]
    fn stats_updated_after_processing() {
        let engine = default_engine();
        let msgs = make_json_messages(10);

        let _results: Vec<Result<(), String>> = engine.process_mid_tier(&msgs, |_| Ok(()));

        let snap = engine.stats().snapshot();
        assert_eq!(snap.received, 10);
        assert_eq!(snap.processed, 10);
        assert_eq!(snap.errors, 0);
        assert_eq!(snap.filtered, 0);
    }

    #[test]
    fn process_raw_passthrough() {
        let engine = default_engine();
        let msgs = make_json_messages(50);

        let results: Vec<Result<usize, String>> =
            engine.process_raw(&msgs, |msg| Ok(msg.payload.len()));

        assert_eq!(results.len(), 50);
        assert!(results.iter().all(|r| r.is_ok()));
        // All JSON messages have the same format: {"_table":"events","id":N}
        assert!(results[0].as_ref().unwrap() > &0);

        let snap = engine.stats().snapshot();
        assert_eq!(snap.received, 50);
        assert_eq!(snap.processed, 50);
    }

    #[test]
    fn process_mid_tier_with_pre_route() {
        let config = BatchProcessingConfig {
            routing_field: Some("_table".to_string()),
            pre_route_filters: vec![config::PreRouteFilterConfig::DlqFieldValue {
                field: "_table".to_string(),
                value: "poison".to_string(),
            }],
            ..Default::default()
        };
        let engine = BatchEngine::new(config);

        let mut msgs = make_json_messages(3);
        // Replace middle message with a poison value
        msgs[1] = RawMessage {
            payload: Bytes::from(r#"{"_table":"poison","id":999}"#),
            key: None,
            headers: vec![],
            metadata: MessageMetadata {
                timestamp_ms: None,
                format: types::PayloadFormat::Json,
                commit_token: None,
            },
        };

        let results: Vec<Result<String, String>> = engine.process_mid_tier(&msgs, |pm| {
            Ok(pm
                .field("_table")
                .and_then(|v| sonic_rs::JsonValueTrait::as_str(v))
                .unwrap_or("?")
                .to_string())
        });

        // 2 ok + 1 DLQ error
        assert_eq!(results.len(), 3);
        assert!(results[0].is_ok());
        assert!(results[1].is_err());
        assert!(results[1].as_ref().unwrap_err().contains("DLQ"));
        assert!(results[2].is_ok());

        let snap = engine.stats().snapshot();
        assert_eq!(snap.dlq, 1);
        assert_eq!(snap.errors, 1);
    }

    #[test]
    fn process_mid_tier_filtered_not_in_results() {
        let config = BatchProcessingConfig {
            routing_field: Some("_table".to_string()),
            pre_route_filters: vec![config::PreRouteFilterConfig::DropFieldMissing {
                field: "_table".to_string(),
            }],
            ..Default::default()
        };
        let engine = BatchEngine::new(config);

        let mut msgs = make_json_messages(3);
        // Replace middle message with one missing _table
        msgs[1] = RawMessage {
            payload: Bytes::from(r#"{"host":"web1"}"#),
            key: None,
            headers: vec![],
            metadata: MessageMetadata {
                timestamp_ms: None,
                format: types::PayloadFormat::Json,
                commit_token: None,
            },
        };

        let results: Vec<Result<String, String>> =
            engine.process_mid_tier(&msgs, |_pm| Ok("ok".to_string()));

        // Filtered messages are removed — only 2 results
        assert_eq!(results.len(), 2);
        assert!(results.iter().all(|r| r.is_ok()));

        let snap = engine.stats().snapshot();
        assert_eq!(snap.filtered, 1);
        assert_eq!(snap.received, 3);
    }

    #[test]
    fn from_cascade_creates_engine() {
        let engine = BatchEngine::from_cascade("batch_processing").unwrap();
        assert_eq!(engine.config().max_chunk_size, 10_000);
    }

    #[test]
    fn accessors_return_expected_types() {
        let engine = default_engine();
        let _stats = engine.stats();
        let _pool = engine.pool();
        let _config = engine.config();
        assert_eq!(engine.stats().snapshot().received, 0);
    }

    #[test]
    fn auto_wire_does_not_panic() {
        let mut engine = default_engine();
        let mgr = crate::metrics::MetricsManager::new_for_test("test_auto_wire");
        engine.auto_wire(
            &mgr,
            #[cfg(feature = "memory")]
            None,
        );
        // Engine should still work after auto_wire
        let msgs = make_json_messages(5);
        let results: Vec<Result<(), String>> = engine.process_mid_tier(&msgs, |_| Ok(()));
        assert_eq!(results.len(), 5);
    }

    #[test]
    fn debug_impl_works() {
        let engine = default_engine();
        let debug = format!("{engine:?}");
        assert!(debug.contains("BatchEngine"));
        assert!(debug.contains("config"));
    }

    #[cfg(feature = "transport-memory")]
    mod async_engine_tests {
        use super::*;
        use std::sync::atomic::{AtomicU64, Ordering};

        fn json_payload(table: &str, id: usize) -> Vec<u8> {
            format!(r#"{{"_table":"{table}","id":{id}}}"#).into_bytes()
        }

        #[tokio::test]
        async fn run_async_processes_and_passes_tokens_to_sink() {
            let config = crate::transport::memory::MemoryConfig {
                recv_timeout_ms: 50,
                ..Default::default()
            };
            let transport = crate::transport::memory::MemoryTransport::new(&config);

            // Inject 5 messages
            for i in 0..5 {
                transport
                    .inject(None, json_payload("events", i))
                    .await
                    .unwrap();
            }

            let engine = default_engine();
            let shutdown = tokio_util::sync::CancellationToken::new();
            let shutdown_clone = shutdown.clone();

            let sink_count = Arc::new(AtomicU64::new(0));
            let token_count = Arc::new(AtomicU64::new(0));
            let sink_count_clone = Arc::clone(&sink_count);
            let token_count_clone = Arc::clone(&token_count);

            // Shut down after a short delay
            tokio::spawn(async move {
                tokio::time::sleep(std::time::Duration::from_millis(200)).await;
                shutdown_clone.cancel();
            });

            let result = engine
                .run_async(
                    &transport,
                    shutdown,
                    |pm: &mut ParsedMessage| -> Result<String, String> {
                        Ok(pm
                            .field("_table")
                            .and_then(|v| sonic_rs::JsonValueTrait::as_str(v))
                            .unwrap_or("?")
                            .to_string())
                    },
                    |results, tokens| {
                        let sc = Arc::clone(&sink_count_clone);
                        let tc = Arc::clone(&token_count_clone);
                        async move {
                            sc.fetch_add(results.len() as u64, Ordering::Relaxed);
                            tc.fetch_add(tokens.len() as u64, Ordering::Relaxed);
                            // Simulate app committing tokens via receiver
                            Ok(())
                        }
                    },
                    None::<(
                        std::time::Duration,
                        fn() -> std::future::Ready<Result<(), EngineError>>,
                    )>,
                )
                .await;

            assert!(result.is_ok());
            assert_eq!(sink_count.load(Ordering::Relaxed), 5);
            assert_eq!(token_count.load(Ordering::Relaxed), 5);
        }

        #[tokio::test]
        async fn run_async_ticker_fires() {
            let config = crate::transport::memory::MemoryConfig {
                recv_timeout_ms: 50,
                ..Default::default()
            };
            let transport = crate::transport::memory::MemoryTransport::new(&config);

            let engine = default_engine();
            let shutdown = tokio_util::sync::CancellationToken::new();
            let shutdown_clone = shutdown.clone();

            let tick_count = Arc::new(AtomicU64::new(0));
            let tick_count_clone = Arc::clone(&tick_count);

            // Shut down after 350ms — ticker at 100ms should fire ~2-3 times
            tokio::spawn(async move {
                tokio::time::sleep(std::time::Duration::from_millis(350)).await;
                shutdown_clone.cancel();
            });

            let result = engine
                .run_async(
                    &transport,
                    shutdown,
                    |_pm: &mut ParsedMessage| -> Result<(), String> { Ok(()) },
                    |_results, _tokens| async { Ok(()) },
                    Some((std::time::Duration::from_millis(100), move || {
                        let tc = Arc::clone(&tick_count_clone);
                        async move {
                            tc.fetch_add(1, Ordering::Relaxed);
                            Ok(())
                        }
                    })),
                )
                .await;

            assert!(result.is_ok());
            let ticks = tick_count.load(Ordering::Relaxed);
            assert!(ticks >= 2, "Expected at least 2 ticks, got {ticks}");
        }

        #[tokio::test]
        async fn run_raw_async_processes_without_parse() {
            let config = crate::transport::memory::MemoryConfig {
                recv_timeout_ms: 50,
                ..Default::default()
            };
            let transport = crate::transport::memory::MemoryTransport::new(&config);

            for i in 0..3 {
                transport
                    .inject(None, json_payload("logs", i))
                    .await
                    .unwrap();
            }

            let engine = default_engine();
            let shutdown = tokio_util::sync::CancellationToken::new();
            let shutdown_clone = shutdown.clone();

            let total_bytes = Arc::new(AtomicU64::new(0));
            let total_bytes_clone = Arc::clone(&total_bytes);

            tokio::spawn(async move {
                tokio::time::sleep(std::time::Duration::from_millis(200)).await;
                shutdown_clone.cancel();
            });

            let result = engine
                .run_raw_async(
                    &transport,
                    shutdown,
                    |msg: &RawMessage| -> Result<usize, String> { Ok(msg.payload.len()) },
                    |results, _tokens| {
                        let tb = Arc::clone(&total_bytes_clone);
                        async move {
                            for len in results.iter().flatten() {
                                tb.fetch_add(*len as u64, Ordering::Relaxed);
                            }
                            Ok(())
                        }
                    },
                    None::<(
                        std::time::Duration,
                        fn() -> std::future::Ready<Result<(), EngineError>>,
                    )>,
                )
                .await;

            assert!(result.is_ok());
            assert!(total_bytes.load(Ordering::Relaxed) > 0);
        }

        #[tokio::test]
        async fn run_async_sink_error_does_not_crash() {
            let config = crate::transport::memory::MemoryConfig {
                recv_timeout_ms: 50,
                ..Default::default()
            };
            let transport = crate::transport::memory::MemoryTransport::new(&config);

            transport
                .inject(None, json_payload("events", 0))
                .await
                .unwrap();

            let engine = default_engine();
            let shutdown = tokio_util::sync::CancellationToken::new();
            let shutdown_clone = shutdown.clone();

            tokio::spawn(async move {
                tokio::time::sleep(std::time::Duration::from_millis(200)).await;
                shutdown_clone.cancel();
            });

            // Sink always errors — engine should continue (not crash)
            let result = engine
                .run_async(
                    &transport,
                    shutdown,
                    |_pm: &mut ParsedMessage| -> Result<(), String> { Ok(()) },
                    |_results, _tokens| async { Err(EngineError::Sink("test sink error".into())) },
                    None::<(
                        std::time::Duration,
                        fn() -> std::future::Ready<Result<(), EngineError>>,
                    )>,
                )
                .await;

            // Should shut down cleanly (not propagate sink error)
            assert!(result.is_ok());
        }
    }
}