net-mesh 0.27.3

High-performance, schema-agnostic, backend-agnostic event bus
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
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
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
//! Shard management for parallel event ingestion.
//!
//! The shard module provides:
//! - Lock-free ring buffers for high-throughput event queuing
//! - Per-shard timestamp generation (no cross-shard contention)
//! - Batch assembly with adaptive sizing
//! - Shard manager for coordinating multiple shards
//! - Dynamic shard scaling with weighted producer routing

mod batch;
mod mapper;
mod ring_buffer;

pub use batch::{AdaptiveBatcher, BatchWorker};
pub use mapper::{
    ScalingDecision, ScalingError, ShardMapper, ShardMetrics, ShardMetricsCollector, ShardState,
};
// `RingBuffer` and `BufferFullError` are intentionally NOT re-exported.
// External callers go through `EventBus` / `ShardManager`, which
// uphold the SPSC contract via `Mutex<Shard>`. Exposing the raw ring
// buffer publicly was a silent-UB footgun — anyone wrapping it in an
// `Arc` and pushing from two threads got data corruption with no
// compile-time signal. `BufferFullError` is not
// re-exported here either: callers see it as `IngestionError::Backpressure`.
pub(crate) use ring_buffer::RingBuffer;

// Re-export ScalingPolicy from config for convenience
pub use crate::config::ScalingPolicy;

use bytes::Bytes;

use crate::config::BackpressureMode;
use crate::error::IngestionError;
use crate::event::{InternalEvent, RawEvent};
use crate::timestamp::TimestampGenerator;

use serde_json::Value as JsonValue;
use std::sync::atomic::{AtomicU64, Ordering as AtomicOrdering};
use std::sync::Arc;

/// Atomic counters for a single shard. Kept outside `Shard` as `Arc`s
/// so `ShardManager::stats()` can aggregate them without locking each
/// shard's mutex.
#[derive(Debug, Default)]
pub struct ShardCounters {
    /// Total events ingested into this shard.
    pub events_ingested: AtomicU64,
    /// Events dropped due to backpressure.
    pub events_dropped: AtomicU64,
    /// Batches successfully dispatched to the adapter.
    pub batches_dispatched: AtomicU64,
}

/// Statistics for a single shard (snapshot).
#[derive(Debug, Default, Clone, Copy)]
pub struct ShardStats {
    /// Total events ingested.
    pub events_ingested: u64,
    /// Events dropped due to backpressure.
    pub events_dropped: u64,
    /// Batches dispatched to adapter.
    pub batches_dispatched: u64,
    /// Events that arrived at `ingest_raw_batch` but had no resolvable
    /// shard (e.g. the routing table was rebuilt mid-dispatch and the
    /// hashed shard id is no longer present). These cannot be
    /// attributed to a per-shard counter, so they are tracked at the
    /// `ShardManager` level and surfaced through aggregated `stats()`.
    pub events_unrouted: u64,
}

impl ShardCounters {
    /// Load a consistent snapshot of the counters.
    ///
    /// `events_unrouted` is left at zero here — it is a manager-level
    /// counter, not a per-shard one. `ShardManager::stats()` fills it
    /// in after summing per-shard fields.
    #[inline]
    pub fn snapshot(&self) -> ShardStats {
        ShardStats {
            events_ingested: self.events_ingested.load(AtomicOrdering::Relaxed),
            events_dropped: self.events_dropped.load(AtomicOrdering::Relaxed),
            batches_dispatched: self.batches_dispatched.load(AtomicOrdering::Relaxed),
            events_unrouted: 0,
        }
    }
}

/// PERF_AUDIT §1.3 — sampling stride for dynamic-scaling
/// instrumentation. Push-latency and buffer-length measurements
/// run once every `METRICS_SAMPLE_STRIDE` successful pushes. The
/// per-event counters still update at full resolution; only the
/// expensive paths (clock read pair + push_latency CAS loop)
/// subsample. With stride 64, sample/event count divergence
/// stays under 2% at typical sustained event rates and the
/// average estimator's standard error is below 1 ns within a
/// metrics tick window.
const METRICS_SAMPLE_STRIDE: u8 = 64;

/// A single shard with its own ring buffer and timestamp generator.
pub struct Shard {
    /// Shard identifier.
    pub id: u16,
    /// Ring buffer for event queuing.
    ring_buffer: RingBuffer<InternalEvent>,
    /// Shard-local timestamp generator (no contention).
    timestamp_gen: TimestampGenerator,
    /// Shared atomic counters (also referenced from `ShardTable` for
    /// lock-free aggregation).
    counters: Arc<ShardCounters>,
    /// Optional metrics collector for dynamic scaling.
    metrics_collector: Option<Arc<ShardMetricsCollector>>,
    /// Ring buffer capacity (for metrics).
    capacity: usize,
    /// PERF_AUDIT §1.3 — rolling counter modulo
    /// `METRICS_SAMPLE_STRIDE` driving the push-path sampling
    /// decision. Cheap byte-sized increment + masked compare per
    /// push. SPSC-safe because the producing thread holds the
    /// shard mutex throughout `try_push_raw` / `try_push`, which
    /// is where this field is read and written.
    metrics_sample_phase: u8,
}

impl Shard {
    /// Create a new shard.
    pub fn new(id: u16, capacity: usize) -> Self {
        Self {
            id,
            ring_buffer: RingBuffer::new(capacity),
            timestamp_gen: TimestampGenerator::new(),
            counters: Arc::new(ShardCounters::default()),
            metrics_collector: None,
            capacity,
            metrics_sample_phase: 0,
        }
    }

    /// Create a new shard with a metrics collector for dynamic scaling.
    pub fn with_metrics(id: u16, capacity: usize, metrics: Arc<ShardMetricsCollector>) -> Self {
        Self {
            id,
            ring_buffer: RingBuffer::new(capacity),
            timestamp_gen: TimestampGenerator::new(),
            counters: Arc::new(ShardCounters::default()),
            metrics_collector: Some(metrics),
            capacity,
            metrics_sample_phase: 0,
        }
    }

    /// Clone the atomic counter handle (for lock-free aggregation).
    #[inline]
    pub fn counters(&self) -> Arc<ShardCounters> {
        self.counters.clone()
    }

    /// Set the metrics collector.
    pub fn set_metrics_collector(&mut self, metrics: Arc<ShardMetricsCollector>) {
        self.metrics_collector = Some(metrics);
    }

    /// Try to push a raw event (pre-serialized bytes) into the shard's ring buffer.
    /// Returns the assigned insertion timestamp on success.
    ///
    /// This is the fastest ingestion path - no serialization or hashing needed.
    ///
    /// PERF_AUDIT §1.3 — dynamic-scaling instrumentation
    /// subsamples on a 1-in-`METRICS_SAMPLE_STRIDE` cadence.
    /// Pre-fix the path took `Instant::now()` twice (Windows QPC,
    /// ~15–30 ns each) plus a CAS-loop on push_latency under the
    /// shard mutex per event when a metrics_collector was set —
    /// ~60–120 ns of pure instrumentation overhead per event for
    /// dynamic-scaling deployments. Now: per-event counters
    /// (events_in_window / pushes_since_drain_start) still bump
    /// at full resolution, but the latency clock-read pair +
    /// CAS update + buffer-length store only fire every Nth
    /// successful push. The quanta TSC clock (~1–5 ns per read)
    /// replaces Instant::now() at the sampling boundary so even
    /// the sampled cost is ~10× smaller than the pre-fix path.
    #[inline]
    pub fn try_push_raw(&mut self, raw: Bytes) -> Result<u64, IngestionError> {
        // Snapshot the sampling decision and start-tick BEFORE
        // the ring push so a slow push doesn't bias the
        // measurement. The phase increment under the shard
        // mutex is race-free with the SPSC contract.
        let push_start_raw = if self.metrics_collector.is_some() {
            self.metrics_sample_phase = self.metrics_sample_phase.wrapping_add(1);
            if self
                .metrics_sample_phase
                .is_multiple_of(METRICS_SAMPLE_STRIDE)
            {
                Some(self.timestamp_gen.now_raw())
            } else {
                None
            }
        } else {
            None
        };
        let ts = self.timestamp_gen.next();
        let event = InternalEvent::new(raw, ts, self.id);

        match self.ring_buffer.try_push(event) {
            Ok(()) => {
                self.counters
                    .events_ingested
                    .fetch_add(1, AtomicOrdering::Relaxed);
                if let Some(collector) = &self.metrics_collector {
                    collector.record_event_only();
                    if let Some(start_raw) = push_start_raw {
                        let latency_ns = self
                            .timestamp_gen
                            .delta_ns(start_raw, self.timestamp_gen.now_raw());
                        collector.record_latency_sample(latency_ns);
                        collector.record_buffer_len(self.ring_buffer.len());
                    }
                }
                Ok(ts)
            }
            Err(_) => {
                self.counters
                    .events_dropped
                    .fetch_add(1, AtomicOrdering::Relaxed);
                Err(IngestionError::Backpressure)
            }
        }
    }

    /// Try to push a JSON value into the shard's ring buffer.
    /// Returns the assigned insertion timestamp on success.
    ///
    /// This serializes the value once before storing.
    ///
    /// Same dynamic-scaling subsampling discipline as
    /// [`Self::try_push_raw`] — see that method's PERF_AUDIT
    /// §1.3 commentary for the rationale.
    #[inline]
    pub fn try_push(&mut self, raw: JsonValue) -> Result<u64, IngestionError> {
        let push_start_raw = if self.metrics_collector.is_some() {
            self.metrics_sample_phase = self.metrics_sample_phase.wrapping_add(1);
            if self
                .metrics_sample_phase
                .is_multiple_of(METRICS_SAMPLE_STRIDE)
            {
                Some(self.timestamp_gen.now_raw())
            } else {
                None
            }
        } else {
            None
        };
        let ts = self.timestamp_gen.next();
        let event = InternalEvent::from_value(raw, ts, self.id);

        match self.ring_buffer.try_push(event) {
            Ok(()) => {
                self.counters
                    .events_ingested
                    .fetch_add(1, AtomicOrdering::Relaxed);
                if let Some(collector) = &self.metrics_collector {
                    collector.record_event_only();
                    if let Some(start_raw) = push_start_raw {
                        let latency_ns = self
                            .timestamp_gen
                            .delta_ns(start_raw, self.timestamp_gen.now_raw());
                        collector.record_latency_sample(latency_ns);
                        collector.record_buffer_len(self.ring_buffer.len());
                    }
                }
                Ok(ts)
            }
            Err(_) => {
                self.counters
                    .events_dropped
                    .fetch_add(1, AtomicOrdering::Relaxed);
                Err(IngestionError::Backpressure)
            }
        }
    }

    /// Pop a batch of events from the ring buffer.
    ///
    /// Allocates a fresh `Vec`. Prefer [`pop_batch_into`] in drain
    /// loops where the per-cycle `Vec` allocation should happen
    /// outside the shard mutex.
    ///
    /// [`pop_batch_into`]: Self::pop_batch_into
    #[inline]
    pub fn pop_batch(&mut self, max: usize) -> Vec<InternalEvent> {
        let out = self.ring_buffer.pop_batch(max);
        if let Some(collector) = &self.metrics_collector {
            collector.record_buffer_len(self.ring_buffer.len());
        }
        out
    }

    /// Pop a batch of events into a caller-owned buffer.
    ///
    /// Append semantics: does **not** clear `dst`; reserves
    /// `count` slots and pushes drained elements onto the end.
    /// Returns the number drained this call. Use this in
    /// steady-state drain loops where the caller keeps a scratch
    /// `Vec` across cycles, so the per-cycle allocation moves out
    /// of the consumer's critical section.
    #[inline]
    pub fn pop_batch_into(&mut self, dst: &mut Vec<InternalEvent>, max: usize) -> usize {
        let n = self.ring_buffer.pop_batch_into(dst, max);
        if let Some(collector) = &self.metrics_collector {
            collector.record_buffer_len(self.ring_buffer.len());
        }
        n
    }

    /// Try to pop a single event from the ring buffer.
    #[inline]
    pub fn try_pop(&mut self) -> Option<InternalEvent> {
        let out = self.ring_buffer.try_pop();
        if let Some(collector) = &self.metrics_collector {
            collector.record_buffer_len(self.ring_buffer.len());
        }
        out
    }

    /// Producer-side eviction of the oldest event.
    ///
    /// Used by `BackpressureMode::DropOldest` to make room for a
    /// new push when the buffer is full. Bypasses the ring buffer's
    /// consumer-thread tracking (the producer thread is calling
    /// what is normally a consumer-side operation). Safe because
    /// the outer shard mutex serializes this against any concurrent
    /// `try_pop` from the legitimate consumer (the batch worker).
    #[inline]
    pub(crate) fn evict_oldest(&mut self) -> Option<InternalEvent> {
        self.ring_buffer.evict_oldest()
    }

    /// Get the current buffer length.
    #[inline]
    pub fn len(&self) -> usize {
        self.ring_buffer.len()
    }

    /// Check if the buffer is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.ring_buffer.is_empty()
    }

    /// Check if the buffer is full.
    #[inline]
    pub fn is_full(&self) -> bool {
        self.ring_buffer.is_full()
    }

    /// Get the fill ratio (0.0 - 1.0).
    #[inline]
    pub fn fill_ratio(&self) -> f64 {
        if self.capacity == 0 {
            0.0
        } else {
            self.ring_buffer.len() as f64 / self.capacity as f64
        }
    }

    /// Get the ring buffer capacity.
    #[inline]
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Get a snapshot of shard statistics.
    pub fn stats(&self) -> ShardStats {
        self.counters.snapshot()
    }

    /// Record a batch dispatch.
    pub fn record_batch_dispatch(&self) {
        self.counters
            .batches_dispatched
            .fetch_add(1, AtomicOrdering::Relaxed);
    }
}

/// Immutable routing table: shards + index + counter handles.
///
/// Placed behind an `ArcSwap` on `ShardManager` so the common read
/// path (`ingest`, `ingest_raw`, `with_shard`, `stats`) is
/// lock-free. Rebuilt on scale up/down via RCU-style swap.
pub struct ShardTable {
    /// All shards, indexed by position. `Arc<Mutex<Shard>>` lets a new
    /// table share shard handles with the previous table (cheap Arc
    /// clones during rebuild).
    shards: Vec<Arc<parking_lot::Mutex<Shard>>>,
    /// Parallel vector of counter handles. Exposes stats without
    /// locking the shard mutex.
    counters: Vec<Arc<ShardCounters>>,
    /// Map from shard ID to index in `shards`/`counters`.
    ///
    /// PERF_AUDIT §1.5 — uses `BuildU16IdentityHasher`, a hand-rolled
    /// `BuildHasher` that returns the key bytes verbatim (no SipHash
    /// mixing). Shard IDs are internally allocated by the bus / mapper,
    /// not influenced by external input, so the DoS-resistance SipHash
    /// is there to provide is irrelevant here — identity hashing on a
    /// `u16` is collision-free and ~10× faster than the std default.
    shard_index: std::collections::HashMap<u16, usize, BuildU16IdentityHasher>,
}

/// Identity hasher for `u16` map keys. Shard IDs live in `0..=65535`
/// and are allocated by the bus / mapper — never by external input —
/// so the SipHash mixing the std default provides for DoS-resistance
/// adds ~15-25 ns per lookup with zero benefit. The identity hash
/// drops that to ~1 ns and stays collision-free across the entire
/// `u16` range.
///
/// Per PERF_AUDIT_2026_06_10_FULL_CRATE.md §1.5.
#[derive(Default, Clone)]
struct U16IdentityHasher(u64);

impl std::hash::Hasher for U16IdentityHasher {
    #[inline]
    fn finish(&self) -> u64 {
        self.0
    }
    #[inline]
    fn write_u16(&mut self, v: u16) {
        self.0 = v as u64;
    }
    /// Defensive fallback. The std `Hash for u16` impl calls
    /// `write_u16` directly, so this byte path is only reached if
    /// someone hashes a non-u16 key against this hasher (which would
    /// be a bug). Pack the first 8 bytes into a u64 so the result is
    /// at least defined, and let it surface as a runtime hash collision
    /// rather than a panic.
    #[inline]
    fn write(&mut self, bytes: &[u8]) {
        for &b in bytes.iter().take(8) {
            self.0 = self.0.rotate_left(8) ^ (b as u64);
        }
    }
}

type BuildU16IdentityHasher = std::hash::BuildHasherDefault<U16IdentityHasher>;

impl ShardTable {
    fn new(shards: Vec<Shard>) -> Self {
        let mut shard_index = std::collections::HashMap::with_capacity_and_hasher(
            shards.len(),
            BuildU16IdentityHasher::default(),
        );
        let mut counters = Vec::with_capacity(shards.len());
        let shards: Vec<_> = shards
            .into_iter()
            .enumerate()
            .map(|(idx, s)| {
                shard_index.insert(s.id, idx);
                counters.push(s.counters());
                Arc::new(parking_lot::Mutex::new(s))
            })
            .collect();
        Self {
            shards,
            counters,
            shard_index,
        }
    }
}

/// Manager for multiple shards.
///
/// The ShardManager can operate in two modes:
/// 1. Static mode (default): Fixed number of shards, simple hash-based routing
/// 2. Dynamic mode: Shards can be added/removed based on load, weighted routing
pub struct ShardManager {
    /// Routing table. Swapped atomically on scale up/down so readers
    /// never see a partially-updated `(shards, shard_index)` pair.
    table: arc_swap::ArcSwap<ShardTable>,
    /// Current number of active shards.
    num_shards: std::sync::atomic::AtomicU16,
    /// Backpressure mode.
    backpressure_mode: BackpressureMode,
    /// Ring buffer capacity for new shards.
    ring_buffer_capacity: usize,
    /// Optional shard mapper for dynamic scaling.
    mapper: Option<Arc<ShardMapper>>,
    /// Serializes concurrent `add_shard` / `remove_shard` rebuilds.
    /// Not on the ingest path.
    rebuild_lock: parking_lot::Mutex<()>,
    /// Events dropped because no destination shard was resolvable.
    /// Distinct from per-shard `events_dropped` (which tracks
    /// backpressure on a known shard) — this counts events whose
    /// hashed shard id was missing from the routing table at lookup
    /// time, e.g. due to a concurrent scale-down. Surfaced via
    /// `stats().events_unrouted`.
    events_unrouted: AtomicU64,
}

impl ShardManager {
    /// Create a new shard manager (static mode).
    pub fn new(
        num_shards: u16,
        ring_buffer_capacity: usize,
        backpressure_mode: BackpressureMode,
    ) -> Self {
        let shards: Vec<Shard> = (0..num_shards)
            .map(|id| Shard::new(id, ring_buffer_capacity))
            .collect();

        Self {
            table: arc_swap::ArcSwap::from_pointee(ShardTable::new(shards)),
            num_shards: std::sync::atomic::AtomicU16::new(num_shards),
            backpressure_mode,
            ring_buffer_capacity,
            mapper: None,
            rebuild_lock: parking_lot::Mutex::new(()),
            events_unrouted: AtomicU64::new(0),
        }
    }

    /// Create a new shard manager with dynamic scaling enabled.
    pub fn with_mapper(
        num_shards: u16,
        ring_buffer_capacity: usize,
        backpressure_mode: BackpressureMode,
        policy: ScalingPolicy,
    ) -> Result<Self, ScalingError> {
        let mapper = Arc::new(ShardMapper::new(num_shards, ring_buffer_capacity, policy)?);

        let shards: Vec<Shard> = (0..num_shards)
            .map(|id| {
                let metrics = mapper.metrics_collector(id).ok_or_else(|| {
                    ScalingError::InvalidPolicy(format!("no metrics collector for shard {}", id))
                })?;
                Ok(Shard::with_metrics(id, ring_buffer_capacity, metrics))
            })
            .collect::<Result<Vec<_>, ScalingError>>()?;

        Ok(Self {
            table: arc_swap::ArcSwap::from_pointee(ShardTable::new(shards)),
            num_shards: std::sync::atomic::AtomicU16::new(num_shards),
            backpressure_mode,
            ring_buffer_capacity,
            mapper: Some(mapper),
            rebuild_lock: parking_lot::Mutex::new(()),
            events_unrouted: AtomicU64::new(0),
        })
    }

    /// Get the shard mapper (if dynamic scaling is enabled).
    pub fn mapper(&self) -> Option<&Arc<ShardMapper>> {
        self.mapper.as_ref()
    }

    /// Get the number of active shards.
    #[inline]
    pub fn num_shards(&self) -> u16 {
        self.num_shards.load(std::sync::atomic::Ordering::Acquire)
    }

    /// Get the backpressure mode.
    #[inline]
    pub fn backpressure_mode(&self) -> BackpressureMode {
        self.backpressure_mode
    }

    /// Select a shard for an event based on its content hash.
    /// Uses weighted selection if dynamic scaling is enabled.
    ///
    /// **Prefer [`select_shard_by_hash`].** This method serializes the
    /// `JsonValue` to bytes just to compute the hash; if you already
    /// have a `RawEvent` (or any pre-computed `xxh3_64` of the
    /// canonical bytes), pass that hash directly. The internal
    /// ingest paths all do — this method exists for ad-hoc external
    /// callers that haven't yet adopted the `RawEvent` pattern.
    ///
    /// [`select_shard_by_hash`]: Self::select_shard_by_hash
    #[inline]
    #[deprecated(
        since = "0.10.0",
        note = "serializes the value just to hash it; prefer `RawEvent::from_value(v).hash()` + `select_shard_by_hash` to avoid the duplicate serialization"
    )]
    #[expect(
        clippy::expect_used,
        reason = "serde_json::to_vec on a JsonValue (which round-tripped from JSON or was built via the type's own constructors) is infallible"
    )]
    pub fn select_shard(&self, event: &JsonValue) -> u16 {
        // Use xxhash for fast, deterministic hashing. `to_vec` avoids the
        // extra UTF-8 validation that `to_string` performs on the serialized
        // buffer, since we only need the bytes for hashing.
        let bytes = serde_json::to_vec(event).expect("Value serialization is infallible");
        let hash = xxhash_rust::xxh3::xxh3_64(&bytes);
        self.select_shard_by_hash(hash)
    }

    /// Select a shard using a pre-computed hash.
    ///
    /// This is faster than `select_shard` when you already have the hash.
    #[inline]
    pub fn select_shard_by_hash(&self, hash: u64) -> u16 {
        if let Some(ref mapper) = self.mapper {
            // Dynamic mode: use weighted selection
            mapper.select_shard(hash)
        } else {
            // Static mode: Lemire's bias-free multiply-shift mapping.
            // Pre-fix this ran `hash % num_shards` per event — modulo
            // by a non-constant `u64` is a `div` on every modern uarch
            // (~20-25 cycles) and dwarfs the upstream xxh3 hash cost
            // for the static-mode hot path. The multiply-shift form
            // `((hash * n) >> 64)` is ~3 cycles and is the same
            // unbiased reduction already used in `mapper.rs:660`. Per
            // net-perf #7.
            //
            // The defensive guard against `num_shards == 0` stays —
            // config validation rejects 0 at startup and `scale_down`
            // requires `current > min_shards >= 1` so this branch is
            // unreachable today, but a stray 0 here would otherwise
            // make the multiply land at index 0 silently while the
            // legacy modulo would have panicked. Returning 0
            // explicitly preserves the legacy "any select returns
            // shard 0" failure mode without the panic.
            let num_shards = self.num_shards.load(std::sync::atomic::Ordering::Acquire);
            debug_assert!(num_shards > 0, "num_shards must be > 0");
            if num_shards == 0 {
                return 0;
            }
            ((hash as u128 * num_shards as u128) >> 64) as u16
        }
    }

    /// Resolve a shard ID to its table index, using the fast path in
    /// static mode (shard_id == index).
    #[inline]
    fn resolve_idx(&self, table: &ShardTable, shard_id: u16) -> Option<usize> {
        if self.mapper.is_none() {
            Some(shard_id as usize)
        } else {
            table.shard_index.get(&shard_id).copied()
        }
    }

    /// Push `raw` into `shard`, handling backpressure. Only clones the
    /// bytes when `DropOldest` needs them for the retry path.
    #[inline]
    fn push_with_backpressure(
        &self,
        shard: &mut Shard,
        shard_id: u16,
        raw: Bytes,
    ) -> Result<(u16, u64), IngestionError> {
        match self.backpressure_mode {
            BackpressureMode::DropOldest => match shard.try_push_raw(raw.clone()) {
                Ok(ts) => Ok((shard_id, ts)),
                Err(IngestionError::Backpressure) => {
                    // The failed try_push_raw incremented events_dropped for
                    // the *new* event, but the new event isn't actually
                    // dropped — the oldest is. Correct the stats: undo the
                    // spurious drop count, evict the oldest (which is the real
                    // drop), and retry with the same ref-counted bytes.
                    //
                    // Use the producer-side `evict_oldest` rather
                    // than `try_pop`. Calling `try_pop` from the
                    // producer thread would violate the SPSC consumer
                    // contract (the
                    // legitimate consumer is the batch worker, on a
                    // different task / thread).
                    //
                    // Transient stats note: a concurrent reader of
                    // `manager.stats().events_dropped` between the
                    // `fetch_sub` and the second `fetch_add` would
                    // briefly observe the pre-correction value
                    // (one less than reality). The net delta over
                    // the whole retry is `+1`, matching the real
                    // drop. Documented as snapshot-not-coherent
                    // per `ShardCounters::snapshot`'s contract.
                    shard
                        .counters
                        .events_dropped
                        .fetch_sub(1, AtomicOrdering::Relaxed);
                    let _ = shard.evict_oldest();
                    shard
                        .counters
                        .events_dropped
                        .fetch_add(1, AtomicOrdering::Relaxed);
                    shard.try_push_raw(raw).map(|ts| (shard_id, ts))
                }
                Err(e) => Err(e),
            },
            BackpressureMode::Sample { .. } => match shard.try_push_raw(raw) {
                Ok(ts) => Ok((shard_id, ts)),
                Err(IngestionError::Backpressure) => Err(IngestionError::Sampled),
                Err(e) => Err(e),
            },
            BackpressureMode::DropNewest | BackpressureMode::FailProducer => {
                shard.try_push_raw(raw).map(|ts| (shard_id, ts))
            }
        }
    }

    /// Ingest an event into the appropriate shard.
    pub fn ingest(&self, event: JsonValue) -> Result<(u16, u64), IngestionError> {
        // Serialize once upfront - avoids clone on retry
        let raw = Bytes::from(serde_json::to_vec(&event)?);
        let hash = xxhash_rust::xxh3::xxh3_64(&raw);
        let shard_id = self.select_shard_by_hash(hash);

        let table = self.table.load();
        // Surface "no routable destination" as `Unrouted` (not
        // `Backpressure`) and bump the manager-level
        // `events_unrouted` counter so per-event vs. batch-path
        // accounting agree. The secondary `table.shards.get(idx)`
        // miss should be impossible by the `shard_index ↔ shards`
        // invariant — keep returning `Unrouted` defensively rather
        // than panicking.
        let Some(idx) = self.resolve_idx(&table, shard_id) else {
            self.events_unrouted.fetch_add(1, AtomicOrdering::Relaxed);
            return Err(IngestionError::Unrouted);
        };
        let Some(shard_lock) = table.shards.get(idx) else {
            self.events_unrouted.fetch_add(1, AtomicOrdering::Relaxed);
            return Err(IngestionError::Unrouted);
        };

        let mut shard = shard_lock.lock();
        self.push_with_backpressure(&mut shard, shard_id, raw)
    }

    /// Ingest a raw event (pre-serialized with cached hash).
    ///
    /// This is the fastest ingestion path:
    /// - Uses pre-computed hash for shard selection (no serialization)
    /// - Stores bytes directly (no clone needed, reference-counted)
    #[inline]
    pub fn ingest_raw(&self, event: RawEvent) -> Result<(u16, u64), IngestionError> {
        let shard_id = self.select_shard_by_hash(event.hash());

        let table = self.table.load();
        // See `ingest` above for the `Unrouted` rationale.
        let Some(idx) = self.resolve_idx(&table, shard_id) else {
            self.events_unrouted.fetch_add(1, AtomicOrdering::Relaxed);
            return Err(IngestionError::Unrouted);
        };
        let Some(shard_lock) = table.shards.get(idx) else {
            self.events_unrouted.fetch_add(1, AtomicOrdering::Relaxed);
            return Err(IngestionError::Unrouted);
        };

        let mut shard = shard_lock.lock();
        self.push_with_backpressure(&mut shard, shard_id, event.bytes())
    }

    /// Ingest a batch of pre-serialized events, grouped by shard.
    ///
    /// Each destination shard's mutex is acquired once and all of that
    /// shard's events are pushed before releasing. With a uniform hash
    /// distribution this amortizes lock acquisitions from O(events) to
    /// O(shards). Backpressure semantics match per-event `ingest_raw`.
    ///
    /// Returns `(success, unrouted)` where `success` is the count of
    /// events successfully pushed onto a shard's ring buffer and
    /// `unrouted` is the count of events whose destination shard was
    /// not present in the routing table at the time of dispatch
    /// (e.g. concurrent scale-down). The remainder
    /// (`total - success - unrouted`) is the backpressure-class drop
    /// count.
    ///
    /// Returns `(success, unrouted)` rather than just `success`
    /// so the bus can subtract `unrouted` before publishing
    /// `events_dropped`. Returning only `success` would let the
    /// bus's `dropped = total - success` accounting double-count
    /// unrouted events — they're already tallied on
    /// `events_unrouted` inside this function.
    pub fn ingest_raw_batch(&self, events: Vec<RawEvent>) -> (usize, usize) {
        if events.is_empty() {
            return (0, 0);
        }

        let table = self.table.load();

        // Bucket by table index. Using a Vec<Vec<_>> keyed by index is
        // cheaper than a HashMap for the common case of a small
        // shard count.
        //
        // PERF_AUDIT §1.7 — pre-size each per-shard `Vec<Bytes>` to
        // the expected per-shard event count (events.len() / nshards),
        // doubled for headroom. Pre-fix every group started at
        // capacity 0 and grew by doubling, paying `nshards + 1`
        // allocations and ~2× redundant memmove of the 32-byte
        // `Bytes` handles per batch. `events.len() / shards.len() * 2`
        // covers the typical uniformly-distributed batch with a small
        // overshoot, and the rare worst-case (all events in one shard)
        // still grows by doubling from a non-zero base.
        let per_group_hint = (events.len() / table.shards.len().max(1)) * 2;
        let mut groups: Vec<Vec<Bytes>> = (0..table.shards.len())
            .map(|_| Vec::with_capacity(per_group_hint))
            .collect();
        let mut group_ids: Vec<u16> = vec![0; groups.len()];

        let mut unrouted = 0usize;
        for event in events {
            let shard_id = self.select_shard_by_hash(event.hash());
            let Some(idx) = self.resolve_idx(&table, shard_id) else {
                // Routing table doesn't contain the chosen shard
                // (e.g. concurrent scale-down removed it). The drop
                // can't be attributed to a per-shard counter; track
                // it on the manager-level `events_unrouted` so
                // bus-level vs. per-shard reconciliation is exact.
                unrouted += 1;
                continue;
            };
            if let Some(g) = groups.get_mut(idx) {
                if g.is_empty() {
                    group_ids[idx] = shard_id;
                }
                g.push(event.bytes());
            }
        }
        if unrouted > 0 {
            self.events_unrouted
                .fetch_add(unrouted as u64, AtomicOrdering::Relaxed);
        }

        let mut success = 0usize;
        for (idx, group) in groups.into_iter().enumerate() {
            if group.is_empty() {
                continue;
            }
            let shard_id = group_ids[idx];
            let Some(shard_lock) = table.shards.get(idx) else {
                continue;
            };
            let mut shard = shard_lock.lock();
            for bytes in group {
                if self
                    .push_with_backpressure(&mut shard, shard_id, bytes)
                    .is_ok()
                {
                    success += 1;
                }
            }
        }

        (success, unrouted)
    }

    /// Get a reference to a shard by ID.
    pub fn shard(&self, id: u16) -> Option<ShardRef> {
        let table = self.table.load();
        let idx = self.resolve_idx(&table, id)?;
        let shard = table.shards.get(idx)?.clone();
        Some(ShardRef { shard })
    }

    /// Lock-free per-batch counter bump for the supplied shard. The
    /// `batches_dispatched` field lives on the parallel
    /// `Vec<Arc<ShardCounters>>` precisely so stats can be recorded
    /// without taking the producer-hot shard mutex. Returns `false`
    /// when `id` is unknown (e.g. the shard was just removed); the
    /// caller treats that as a no-op. Per PERF_AUDIT §1.4.
    pub fn record_batch_dispatch(&self, id: u16) -> bool {
        let table = self.table.load();
        let Some(idx) = self.resolve_idx(&table, id) else {
            return false;
        };
        let Some(counters) = table.counters.get(idx) else {
            return false;
        };
        counters
            .batches_dispatched
            .fetch_add(1, AtomicOrdering::Relaxed);
        true
    }

    /// Execute a function with exclusive access to a shard.
    pub fn with_shard<F, R>(&self, id: u16, f: F) -> Option<R>
    where
        F: FnOnce(&mut Shard) -> R,
    {
        let table = self.table.load();
        let idx = self.resolve_idx(&table, id)?;
        table.shards.get(idx).map(|shard_lock| {
            let mut shard = shard_lock.lock();
            f(&mut shard)
        })
    }

    /// Returns true if every shard's ring buffer is empty.
    ///
    /// Cheaper than `shard_ids()` + repeated `with_shard`: loads the
    /// routing table once and checks each shard behind a brief lock.
    pub fn all_shards_empty(&self) -> bool {
        let table = self.table.load();
        table.shards.iter().all(|s| s.lock().is_empty())
    }

    /// Iterate over all active shard IDs.
    pub fn shard_ids(&self) -> Vec<u16> {
        self.table.load().shard_index.keys().copied().collect()
    }

    /// Sum of `len()` across every shard's ring buffer.
    pub fn total_pending_in_rings(&self) -> u64 {
        let table = self.table.load();
        table.shards.iter().map(|s| s.lock().len() as u64).sum()
    }

    /// Best-effort variant of [`Self::total_pending_in_rings`] that
    /// never blocks: every shard whose mutex is currently held is
    /// skipped (counted as zero). Use this from `Drop` or any path
    /// that may run on a thread already holding a shard lock
    /// (single-thread runtime + panic during shutdown is the
    /// canonical hazard); the blocking variant would self-deadlock
    /// there.
    ///
    /// Returns `(sum_counted, uncounted_shard_count)` so the caller
    /// can log the uncertainty in the result.
    pub fn try_total_pending_in_rings(&self) -> (u64, usize) {
        let table = self.table.load();
        let mut sum: u64 = 0;
        let mut uncounted: usize = 0;
        for s in table.shards.iter() {
            match s.try_lock() {
                Some(guard) => sum += guard.len() as u64,
                None => uncounted += 1,
            }
        }
        (sum, uncounted)
    }

    /// Get aggregated statistics from all shards.
    ///
    /// Lock-free: reads each shard's atomic counters directly via the
    /// parallel `counters` vector on the routing table, with no per-
    /// shard mutex acquisition. `events_unrouted` is sourced from the
    /// `ShardManager` itself rather than the per-shard counters since
    /// unrouted events have no shard to attribute to.
    pub fn stats(&self) -> ShardStats {
        let table = self.table.load();
        let mut total = ShardStats::default();
        for counters in table.counters.iter() {
            let snap = counters.snapshot();
            total.events_ingested += snap.events_ingested;
            total.events_dropped += snap.events_dropped;
            total.batches_dispatched += snap.batches_dispatched;
        }
        total.events_unrouted = self.events_unrouted.load(AtomicOrdering::Relaxed);
        total
    }

    /// Rebuild the routing table with a closure that sees the old
    /// `(shards, counters, shard_index)` and produces the new ones.
    /// Serialized by `rebuild_lock` so concurrent scaling operations
    /// can't race on read-modify-write of the table.
    fn rebuild_table<F>(&self, f: F)
    where
        F: FnOnce(
            &Vec<Arc<parking_lot::Mutex<Shard>>>,
            &Vec<Arc<ShardCounters>>,
            &std::collections::HashMap<u16, usize, BuildU16IdentityHasher>,
        ) -> ShardTable,
    {
        let _guard = self.rebuild_lock.lock();
        let old = self.table.load();
        let new = f(&old.shards, &old.counters, &old.shard_index);
        self.table.store(Arc::new(new));
    }

    /// Add a new shard (for dynamic scaling).
    /// Returns the new shard ID. The shard is in the routing table
    /// and ready to be the destination of `select_shard` calls
    /// **only after** [`activate_shard`] is called for it.
    ///
    /// Previously the mapper marked the shard `Active` *before* the
    /// routing table was rebuilt and *before* any worker was wired up
    /// to drain its ring buffer. Producers could `select_shard` to
    /// the new id, push into its ring buffer, and have the events
    /// stranded with no consumer. The fix uses
    /// `scale_up_provisioning` so the mapper records the shard but
    /// `select_shard` skips it, then `activate_shard` flips it to
    /// `Active` once workers are ready.
    ///
    /// [`activate_shard`]: Self::activate_shard
    pub fn add_shard(&self) -> Result<u16, ScalingError> {
        self.add_shard_inner(false)
    }

    /// Like [`add_shard`] but bypasses the auto-scaling cooldown.
    ///
    /// Used by operator-initiated `manual_scale_up` paths. The
    /// auto-scaling cooldown protects against the auto-scaling
    /// monitor reacting too quickly to transient load spikes;
    /// a deliberate operator action should not be rate-limited
    /// by that cadence. The `max_shards` budget check still
    /// applies.
    ///
    /// [`add_shard`]: Self::add_shard
    pub fn add_shard_force(&self) -> Result<u16, ScalingError> {
        self.add_shard_inner(true)
    }

    fn add_shard_inner(&self, force: bool) -> Result<u16, ScalingError> {
        let mapper = self.mapper.as_ref().ok_or(ScalingError::InvalidPolicy(
            "Dynamic scaling not enabled".into(),
        ))?;

        // Allocate the shard in `Provisioning` state — not yet
        // selectable.
        let new_ids = if force {
            mapper.scale_up_provisioning_force(1)?
        } else {
            mapper.scale_up_provisioning(1)?
        };
        let new_id = new_ids[0];

        let metrics = mapper.metrics_collector(new_id).ok_or_else(|| {
            ScalingError::InvalidPolicy(format!("no metrics collector for shard {}", new_id))
        })?;
        let new_shard = Shard::with_metrics(new_id, self.ring_buffer_capacity, metrics);
        let new_counters = new_shard.counters();
        let new_shard = Arc::new(parking_lot::Mutex::new(new_shard));

        // Publish to the routing table so `with_shard` works (the
        // drain worker the caller is about to spawn needs this) but
        // the shard is still `Provisioning` so `select_shard` will
        // not route producer pushes to it yet.
        self.rebuild_table(|shards, counters, shard_index| {
            let mut shards = shards.clone();
            let mut counters = counters.clone();
            let mut shard_index = shard_index.clone();
            let idx = shards.len();
            shards.push(new_shard.clone());
            counters.push(new_counters.clone());
            shard_index.insert(new_id, idx);
            ShardTable {
                shards,
                counters,
                shard_index,
            }
        });

        // Don't bump `num_shards` yet — `activate_shard` does that
        // when the shard becomes selectable.
        Ok(new_id)
    }

    /// Activate a previously-provisioned shard. After this returns,
    /// `select_shard` will route to the shard and producer pushes
    /// will land in its ring buffer.
    ///
    /// Idempotent: calling on an already-`Active` shard is `Ok(())`.
    ///
    /// Pre-fix this unconditionally `fetch_add(1)`d
    /// `num_shards` even when the mapper's `activate()` early-
    /// returned for an already-`Active` shard. After repeated
    /// activate calls, `num_shards` exceeded both the mapper's
    /// `active_count` and the actual shard count, breaking
    /// modulo-based shard selection (`select_shard`) and
    /// producing stale routing decisions.  Post-fix gates the
    /// `fetch_add` on the mapper's transition signal.
    pub fn activate_shard(&self, shard_id: u16) -> Result<(), ScalingError> {
        let mapper = self.mapper.as_ref().ok_or(ScalingError::InvalidPolicy(
            "Dynamic scaling not enabled".into(),
        ))?;
        let transitioned = mapper.activate(shard_id)?;
        if transitioned {
            self.num_shards
                .fetch_add(1, std::sync::atomic::Ordering::Release);
        }
        Ok(())
    }

    /// Start draining a shard (for dynamic scaling).
    ///
    /// Previously only flipped the metrics collector's `draining`
    /// atomic, leaving `MappedShard.state` untouched. Result:
    /// `select_shard` (which filters on `state == Active`) still
    /// routed new producers to the shard. The fix calls into the
    /// mapper, which atomically transitions the state to `Draining`
    /// and (for accounting) decrements `active_count`, mirroring
    /// `scale_down(N)` for a single targeted shard.
    pub fn drain_shard(&self, shard_id: u16) -> Result<(), ScalingError> {
        let mapper = self.mapper.as_ref().ok_or(ScalingError::InvalidPolicy(
            "Dynamic scaling not enabled".into(),
        ))?;
        mapper.drain_specific(shard_id)
    }

    /// Remove a shard from the routing table.
    ///
    /// Previously this only unmapped the shard from the routing
    /// table. The drain worker, on its next `with_shard` call,
    /// observed `None` and exited — leaving any events still in the
    /// ring buffer permanently stranded. The fix drains the ring
    /// buffer into a caller-supplied scratch `Vec` **before** the
    /// unmap, then returns the drained events so the caller
    /// (typically `EventBus::remove_shard_internal`) can flush them
    /// through to the adapter rather than dropping them.
    ///
    /// Returns `Ok(events)` where `events` is whatever was still
    /// queued in the ring buffer at unmap time (possibly empty).
    /// Caller is responsible for handing those off to the adapter.
    pub fn remove_shard(
        &self,
        shard_id: u16,
    ) -> Result<Vec<crate::event::InternalEvent>, ScalingError> {
        let mapper = self.mapper.as_ref().ok_or(ScalingError::InvalidPolicy(
            "Dynamic scaling not enabled".into(),
        ))?;

        // Capture the mapper-side state *before* we unmap. This
        // gates the `num_shards` decrement at the end so it stays
        // symmetric with `activate_shard`'s `fetch_add`. The
        // activate-failure rollback path (`bus.rs`) calls us on a
        // shard that's still `Provisioning` — `add_shard` never
        // bumped `num_shards` for it, so an unconditional
        // `fetch_sub` here would leave the counter one below the
        // table's actual size, breaking modulo-based shard
        // selection. `Active` / `Draining` / `Stopped` shards all
        // had `activate_shard` succeed against them at some point
        // (it's the only way out of `Provisioning`), so they did
        // bump `num_shards` and must decrement here.
        let was_activated = matches!(
            mapper.shard_state(shard_id),
            Some(ShardState::Active) | Some(ShardState::Draining) | Some(ShardState::Stopped)
        );

        // Drain whatever is left in the ring buffer before unmapping.
        // `with_shard` returns `None` once the shard is gone, so we
        // do this *before* `rebuild_table`. We cap drain to a sane
        // upper bound (`ring_buffer_capacity`) so a malformed shard
        // can't pin us here forever.
        let cap = self.ring_buffer_capacity;
        let drained: Vec<crate::event::InternalEvent> = self
            .with_shard(shard_id, |shard| {
                let mut buf = Vec::with_capacity(shard.len().min(cap));
                shard.pop_batch_into(&mut buf, cap);
                buf
            })
            .unwrap_or_default();

        let mut removed = false;
        self.rebuild_table(|shards, counters, shard_index| {
            let mut shards = shards.clone();
            let mut counters = counters.clone();
            let mut shard_index = shard_index.clone();

            if let Some(idx) = shard_index.remove(&shard_id) {
                removed = true;
                shards.swap_remove(idx);
                counters.swap_remove(idx);
                // swap_remove moved the last element into `idx`: update its
                // index mapping.
                if idx < shards.len() {
                    let moved_shard_id = shards[idx].lock().id;
                    shard_index.insert(moved_shard_id, idx);
                }
            }

            ShardTable {
                shards,
                counters,
                shard_index,
            }
        });

        if removed && was_activated {
            self.num_shards
                .fetch_sub(1, std::sync::atomic::Ordering::Release);
        }

        // Ask the mapper to drop the corresponding `MappedShard`
        // record. Without this sweep the mapper's
        // `shards: RwLock<Vec<MappedShard>>` would keep growing
        // across scale-up/down cycles (every scale-up appends a
        // fresh entry; `Stopped` entries are only removed by an
        // explicit `remove_specific_stopped_shard` /
        // `remove_stopped_shards` call). `evaluate_scaling`
        // filters by state but still iterates the full list, so
        // per-tick cost would grow with cumulative scaling history.
        //
        // The scaling monitor calls `mapper.finalize_draining()`
        // before invoking `bus.remove_shard_internal(id)` (which is
        // what calls us), so by the time we run the matching
        // `MappedShard` is already in `Stopped` state. We prune
        // ONLY this shard here, not every Stopped one — a bulk
        // sweep would prune sibling Stopped shards that a
        // sequential `manual_scale_down` is about to look up
        // state for in its next iteration's `remove_shard`. Once
        // the mapper had `None` for a sibling shard, the
        // `was_activated` gate above would observe it as
        // never-activated and skip the `num_shards` decrement,
        // leaving the counter one below the actual table size.
        mapper.remove_specific_stopped_shard(shard_id);

        Ok(drained)
    }

    /// Collect metrics from all shards (for dynamic scaling decisions).
    pub fn collect_metrics(&self) -> Option<Vec<ShardMetrics>> {
        self.mapper.as_ref().map(|m| m.collect_metrics())
    }

    /// Evaluate and optionally execute scaling.
    pub fn evaluate_scaling(&self) -> ScalingDecision {
        self.mapper
            .as_ref()
            .map(|m| m.evaluate_scaling())
            .unwrap_or(ScalingDecision::None)
    }
}

/// An owned handle to a shard. Holding this does not block scaling
/// operations; the shard stays alive via `Arc` refcount even if
/// removed from the table.
pub struct ShardRef {
    shard: Arc<parking_lot::Mutex<Shard>>,
}

impl ShardRef {
    /// Lock the shard for exclusive access.
    pub fn lock(&self) -> parking_lot::MutexGuard<'_, Shard> {
        self.shard.lock()
    }
}

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

    #[test]
    fn test_shard_push_pop() {
        let mut shard = Shard::new(0, 1024);

        let ts = shard.try_push(json!({"test": 1})).unwrap();
        assert!(ts > 0);
        assert_eq!(shard.len(), 1);

        let event = shard.try_pop().unwrap();
        assert_eq!(event.shard_id, 0);
        assert_eq!(event.insertion_ts, ts);
        assert!(shard.is_empty());
    }

    /// A `Shard` configured with a `ShardMetricsCollector` must feed every
    /// successful push into the per-event counters so the dynamic-scaling
    /// `event_rate` and drain-finalize predicate see correct totals.
    ///
    /// Per PERF_AUDIT §1.3 the latency / buffer-length probes are
    /// subsampled on a 1-in-`METRICS_SAMPLE_STRIDE` cadence, so this
    /// test pushes well past one stride period (256 = 4 × stride)
    /// to deterministically guarantee at least four sampling
    /// boundaries fire — the resulting averages are statistically
    /// well-defined and the test contract stays observable.
    #[test]
    fn try_push_feeds_metrics_collector() {
        let collector = Arc::new(ShardMetricsCollector::new(0, 1024));
        let mut shard = Shard::with_metrics(0, 1024, Arc::clone(&collector));

        let pushes: u64 = (METRICS_SAMPLE_STRIDE as u64) * 4;
        for i in 0..pushes {
            shard.try_push(json!({"i": i})).unwrap();
        }

        let metrics = collector.collect_and_reset();
        assert_eq!(
            metrics.event_rate, pushes,
            "every push must increment event_rate at full resolution"
        );
        assert!(
            metrics.fill_ratio > 0.0,
            "buffer length must be observable after ≥1 sampling stride"
        );
        assert!(
            metrics.avg_push_latency_ns > 0,
            "push latency must be recorded after ≥1 sampling stride"
        );
    }

    /// `try_total_pending_in_rings` must never block, must skip
    /// shards whose mutex is currently held, and must report how
    /// many it skipped. This is what makes `EventBus::Drop`
    /// safe to call on a thread that already holds a shard lock.
    #[test]
    fn try_total_pending_in_rings_skips_held_shards() {
        let manager = ShardManager::new(2, 1024, BackpressureMode::DropNewest);
        // Push some events so a non-zero count is observable.
        manager.ingest(json!({"i": 1})).unwrap();
        manager.ingest(json!({"i": 2})).unwrap();
        manager.ingest(json!({"i": 3})).unwrap();

        // Uncontended: all shards counted, uncounted_shards == 0.
        let (sum, uncounted) = manager.try_total_pending_in_rings();
        assert_eq!(uncounted, 0);
        let baseline_sum = sum;
        assert!(baseline_sum > 0, "events should be pending in some shard");

        // Hold one shard's mutex and re-check: that shard must be
        // skipped, uncounted must be 1, and the call must return
        // immediately (this test would hang on the blocking
        // `total_pending_in_rings` variant).
        let table = manager.table.load();
        let _guard = table.shards[0].lock();
        let (sum2, uncounted2) = manager.try_total_pending_in_rings();
        assert_eq!(uncounted2, 1, "the locked shard must be uncounted");
        assert!(
            sum2 <= baseline_sum,
            "sum must not include events from the locked shard"
        );
    }

    /// Same wiring for `try_push_raw` — the byte-oriented hot path.
    /// Pushes past 4× `METRICS_SAMPLE_STRIDE` for the same
    /// statistical reason as `try_push_feeds_metrics_collector`.
    #[test]
    fn try_push_raw_feeds_metrics_collector() {
        let collector = Arc::new(ShardMetricsCollector::new(0, 1024));
        let mut shard = Shard::with_metrics(0, 1024, Arc::clone(&collector));

        let pushes: u64 = (METRICS_SAMPLE_STRIDE as u64) * 4;
        for i in 0..pushes {
            shard
                .try_push_raw(bytes::Bytes::from(format!("event-{i}")))
                .unwrap();
        }

        let metrics = collector.collect_and_reset();
        assert_eq!(metrics.event_rate, pushes);
        assert!(metrics.fill_ratio > 0.0);
        assert!(metrics.avg_push_latency_ns > 0);
    }

    /// PERF_AUDIT §1.3 regression: instrumentation MUST subsample
    /// the latency/buffer-length probes — the pre-fix code paid
    /// 2× `Instant::now()` plus a CAS loop on push_latency under
    /// the shard mutex for every event, ~60–120 ns of overhead
    /// per ingest. Pin the contract by pushing exactly
    /// `STRIDE - 1` events and asserting no latency sample fires.
    #[test]
    fn latency_and_buffer_len_are_subsampled() {
        let collector = Arc::new(ShardMetricsCollector::new(0, 1024));
        let mut shard = Shard::with_metrics(0, 1024, Arc::clone(&collector));

        // STRIDE - 1 pushes: the rolling counter never lands at
        // phase 0, so no latency / buffer_len sample fires. The
        // per-event counters still bump.
        let below_stride: u64 = (METRICS_SAMPLE_STRIDE as u64) - 1;
        for i in 0..below_stride {
            shard
                .try_push_raw(bytes::Bytes::from(format!("evt-{i}")))
                .unwrap();
        }
        let metrics = collector.collect_and_reset();
        assert_eq!(
            metrics.event_rate, below_stride,
            "per-event counters retain full resolution"
        );
        assert_eq!(
            metrics.avg_push_latency_ns, 0,
            "no latency sample must fire below the first stride boundary — \
             pre-fix this fired every event"
        );
        assert_eq!(
            metrics.fill_ratio, 0.0,
            "no buffer_len sample must fire below the first stride boundary"
        );

        // Exactly one more push crosses the stride: latency and
        // fill_ratio become observable.
        shard.try_push_raw(bytes::Bytes::from("evt-final")).unwrap();
        let metrics = collector.collect_and_reset();
        assert_eq!(
            metrics.event_rate, 1,
            "the final push counts toward event_rate"
        );
        assert!(
            metrics.avg_push_latency_ns > 0,
            "crossing the stride boundary must record a latency sample"
        );
        assert!(
            metrics.fill_ratio > 0.0,
            "crossing the stride boundary must record buffer_len"
        );
    }

    /// PERF_AUDIT §1.4 — `ShardManager::record_batch_dispatch` must
    /// hit the lock-free per-shard counter (no shard-mutex lock).
    /// Pin: a sequence of calls bumps the counter to the expected
    /// total; unknown shard ids return false and do not bump
    /// anything; the read path (`ShardManager::stats()`) reflects
    /// the increments.
    #[test]
    fn record_batch_dispatch_is_lock_free_and_aggregates() {
        let manager = ShardManager::new(2, 1024, BackpressureMode::DropNewest);
        let shard_ids = manager.shard_ids();
        assert_eq!(shard_ids.len(), 2);
        for _ in 0..7 {
            assert!(manager.record_batch_dispatch(shard_ids[0]));
        }
        for _ in 0..3 {
            assert!(manager.record_batch_dispatch(shard_ids[1]));
        }
        // Unknown id → no-op.
        assert!(!manager.record_batch_dispatch(0xFFFF));

        // Stats aggregator sums across shards.
        let stats = manager.stats();
        assert_eq!(
            stats.batches_dispatched, 10,
            "aggregated batches_dispatched must equal the sum of per-shard \
             record_batch_dispatch calls"
        );
    }

    /// PERF_AUDIT §1.4 — the lock-free counter bump must hit the
    /// RIGHT shard slot after a dynamic rescale. `remove_shard`
    /// rebuilds the table with `swap_remove`, which relocates the
    /// last shard into the removed index; a stale id→idx mapping
    /// (or a static-mode `shard_id == index` assumption leaking
    /// into dynamic mode) would credit the wrong shard's counter.
    #[test]
    fn record_batch_dispatch_hits_correct_slot_after_rescale() {
        use crate::config::ScalingPolicy;
        let policy = ScalingPolicy {
            min_shards: 1,
            max_shards: 8,
            cooldown: std::time::Duration::from_nanos(1),
            ..Default::default()
        };
        let manager =
            ShardManager::with_mapper(2, 1024, BackpressureMode::DropNewest, policy).unwrap();

        // Remove shard 0: swap_remove moves shard 1 (last) into
        // index 0, so id 1 now resolves to a different index than
        // its id.
        manager
            .remove_shard(0)
            .expect("remove_shard must succeed in dynamic mode");

        // Removed id → no-op, no counter bump anywhere.
        assert!(!manager.record_batch_dispatch(0));

        // Surviving id must bump ITS counter through the rebuilt
        // index, not slot `shard_id as usize`.
        for _ in 0..5 {
            assert!(manager.record_batch_dispatch(1));
        }
        let shard1_batches = manager
            .with_shard(1, |s| {
                s.counters.batches_dispatched.load(AtomicOrdering::Relaxed)
            })
            .expect("shard 1 still routable");
        assert_eq!(
            shard1_batches, 5,
            "post-rescale bumps must land on the surviving shard's counter"
        );
        assert_eq!(
            manager.stats().batches_dispatched,
            5,
            "aggregate must see exactly the 5 post-rescale bumps"
        );
    }

    /// PERF_AUDIT §1.5 — the identity hasher must stay collision-free
    /// across the entire u16 keyspace (that's the property that makes
    /// dropping SipHash safe here). Pin both the raw hasher contract
    /// (`finish() == key`) and the end-to-end map behavior with every
    /// possible shard id inserted at once.
    #[test]
    fn u16_identity_hasher_is_collision_free_across_keyspace() {
        use std::hash::Hasher as _;
        let mut h = U16IdentityHasher::default();
        h.write_u16(0xBEEF);
        assert_eq!(h.finish(), 0xBEEF, "hash must be the key verbatim");

        let mut map: std::collections::HashMap<u16, usize, BuildU16IdentityHasher> =
            std::collections::HashMap::with_capacity_and_hasher(
                1 << 16,
                BuildU16IdentityHasher::default(),
            );
        for id in 0..=u16::MAX {
            map.insert(id, id as usize);
        }
        assert_eq!(map.len(), 1 << 16, "all 65536 keys must coexist");
        for id in 0..=u16::MAX {
            assert_eq!(
                map.get(&id).copied(),
                Some(id as usize),
                "key {id} must round-trip through the identity-hashed map"
            );
        }
    }

    #[test]
    #[allow(deprecated)] // exercises the deprecated `select_shard` path
    fn test_shard_manager_routing() {
        let manager = ShardManager::new(4, 1024, BackpressureMode::DropNewest);

        // Same event should always go to the same shard
        let event = json!({"key": "value"});
        let shard1 = manager.select_shard(&event);
        let shard2 = manager.select_shard(&event);
        assert_eq!(shard1, shard2);

        // Different events may go to different shards
        let events: Vec<_> = (0..100).map(|i| json!({"i": i})).collect();
        let shards: std::collections::HashSet<_> =
            events.iter().map(|e| manager.select_shard(e)).collect();

        // With 100 random events and 4 shards, we should hit multiple shards
        assert!(shards.len() > 1);
    }

    /// Regression: the deprecated `select_shard(&JsonValue)` must produce
    /// the same shard id as `select_shard_by_hash` would for the
    /// equivalent `RawEvent`. They share underlying logic now, but if a
    /// future refactor splits them this test catches the divergence
    /// before consumers do.
    #[test]
    #[allow(deprecated)]
    fn test_select_shard_matches_select_shard_by_hash() {
        let manager = ShardManager::new(8, 1024, BackpressureMode::DropNewest);
        for i in 0..200 {
            let v = json!({"i": i, "tag": format!("user-{i}")});
            let raw = RawEvent::from_value(v.clone());
            assert_eq!(
                manager.select_shard(&v),
                manager.select_shard_by_hash(raw.hash()),
                "select_shard and select_shard_by_hash must agree (i={i})"
            );
        }
    }

    /// Pin net-perf #7: the static-mode `select_shard_by_hash` is now
    /// Lemire's `(hash * n) >> 64` instead of `hash % n` (one `div`
    /// per event eliminated). Lemire maps `[0, u64::MAX]` evenly into
    /// `[0, n)` for any `n` that fits in u16: the output must always
    /// land in range, and `hash == 0` must still resolve to shard 0
    /// (the multiplication overflow-pattern boundary).
    #[test]
    fn select_shard_by_hash_uses_lemire_reduction_in_static_mode() {
        for &shard_count in &[1u16, 2, 3, 4, 7, 8, 16, 64] {
            let manager = ShardManager::new(shard_count, 1024, BackpressureMode::DropNewest);

            // `hash == 0` → `(0 * n) >> 64 == 0` regardless of `n`.
            // Locking down this boundary catches a regression where
            // the multiplication is dropped or reordered.
            assert_eq!(
                manager.select_shard_by_hash(0),
                0,
                "hash 0 must resolve to shard 0 (n={shard_count})"
            );

            // Every output must be a valid shard index. A regression
            // back to `hash % shard_count` would still land in range,
            // but any wrong shift (e.g. `>> 32`) or sign-extension
            // bug would push the result past `shard_count`.
            for hash in [
                1u64,
                42,
                u64::MAX,
                u64::MAX - 1,
                0x8000_0000_0000_0000,
                0x7FFF_FFFF_FFFF_FFFF,
                0xDEAD_BEEF_DEAD_BEEF,
            ] {
                let shard = manager.select_shard_by_hash(hash);
                assert!(
                    shard < shard_count,
                    "shard {shard} out of range for n={shard_count}, hash={hash:#x}"
                );
            }
        }

        // Distribution sanity: across 10_000 successive hashes that
        // mimic the input spread of `xxh3_64`, every shard sees at
        // least one event. A regression to `>> 64` returning 0 always
        // (e.g. `(hash as u64 * n as u64) >> 64`, which truncates the
        // product) would put everything on shard 0.
        let manager = ShardManager::new(8, 1024, BackpressureMode::DropNewest);
        let mut counts = [0usize; 8];
        for i in 0u64..10_000 {
            let h = xxhash_rust::xxh3::xxh3_64(&i.to_le_bytes());
            counts[manager.select_shard_by_hash(h) as usize] += 1;
        }
        for (i, &c) in counts.iter().enumerate() {
            assert!(
                c > 0,
                "shard {i} got 0 events out of 10_000 (Lemire reduction must spread)"
            );
        }
    }

    #[test]
    fn test_shard_manager_ingest() {
        let manager = ShardManager::new(4, 1024, BackpressureMode::DropNewest);

        for i in 0..100 {
            let event = json!({"i": i});
            let result = manager.ingest(event);
            assert!(result.is_ok());
        }

        let stats = manager.stats();
        assert_eq!(stats.events_ingested, 100);
        assert_eq!(stats.events_dropped, 0);
    }

    #[test]
    fn test_backpressure_drop_newest() {
        let manager = ShardManager::new(1, 4, BackpressureMode::DropNewest);

        // Fill the buffer (capacity 4, usable 3)
        for i in 0..3 {
            manager.ingest(json!({"i": i})).unwrap();
        }

        // Next insert should fail
        let result = manager.ingest(json!({"i": 999}));
        assert!(matches!(result, Err(IngestionError::Backpressure)));

        let stats = manager.stats();
        assert_eq!(stats.events_ingested, 3);
        assert_eq!(stats.events_dropped, 1);
    }

    #[test]
    fn test_backpressure_drop_oldest() {
        let manager = ShardManager::new(1, 4, BackpressureMode::DropOldest);

        // Fill the buffer
        for i in 0..3 {
            manager.ingest(json!({"i": i})).unwrap();
        }

        // Next insert should succeed by dropping oldest
        let result = manager.ingest(json!({"i": 999}));
        assert!(result.is_ok());

        // Verify the oldest was dropped
        let shard = manager.shard(0).unwrap();
        let events = shard.lock().pop_batch(10);

        // Should have events 1, 2, 999 (0 was dropped)
        assert_eq!(events.len(), 3);
        assert_eq!(events[0].parse().unwrap(), json!({"i": 1}));
        assert_eq!(events[2].parse().unwrap(), json!({"i": 999}));
    }

    #[test]
    fn test_raw_event_ingestion() {
        let manager = ShardManager::new(4, 1024, BackpressureMode::DropNewest);

        for i in 0..100 {
            let raw = RawEvent::from_str(&format!(r#"{{"i": {}}}"#, i));
            let result = manager.ingest_raw(raw);
            assert!(result.is_ok());
        }

        let stats = manager.stats();
        assert_eq!(stats.events_ingested, 100);
        assert_eq!(stats.events_dropped, 0);
    }

    /// `ingest_raw_batch` groups events by destination shard before
    /// pushing — verify the grouping preserves FIFO within a shard,
    /// honors hash-based routing, and that totals match `ingest_raw`.
    #[test]
    fn test_ingest_raw_batch_routes_and_preserves_order() {
        let manager = ShardManager::new(4, 1024, BackpressureMode::DropNewest);
        let events: Vec<RawEvent> = (0..200)
            .map(|i| RawEvent::from_str(&format!(r#"{{"i":{}}}"#, i)))
            .collect();

        // Snapshot the expected destination for each event so we can
        // compare against what actually landed in each shard.
        let expected_dests: Vec<u16> = events
            .iter()
            .map(|e| manager.select_shard_by_hash(e.hash()))
            .collect();

        let (success, unrouted) = manager.ingest_raw_batch(events.clone());
        assert_eq!(success, 200, "all events should land with ample capacity");
        assert_eq!(unrouted, 0, "no scale-down so no unrouted events");

        // Aggregate totals must match.
        let stats = manager.stats();
        assert_eq!(stats.events_ingested, 200);
        assert_eq!(stats.events_dropped, 0);

        // Per-shard totals must match the expected routing distribution,
        // and the distribution must span more than one shard (otherwise
        // the test wouldn't exercise the grouping path).
        let mut expected_by_shard: std::collections::HashMap<u16, u64> =
            std::collections::HashMap::new();
        for d in &expected_dests {
            *expected_by_shard.entry(*d).or_default() += 1;
        }
        assert!(
            expected_by_shard.len() > 1,
            "hash distribution should span multiple shards"
        );
        for shard_id in 0..4u16 {
            let got = manager
                .with_shard(shard_id, |s| s.stats().events_ingested)
                .unwrap();
            let want = expected_by_shard.get(&shard_id).copied().unwrap_or(0);
            assert_eq!(got, want, "shard {} ingested count mismatch", shard_id);
        }

        // FIFO within a shard: the events a shard received, in the order
        // we batched them, must come out of the ring buffer in the same
        // order.
        for shard_id in 0..4u16 {
            let expected_payloads: Vec<&[u8]> = events
                .iter()
                .zip(expected_dests.iter())
                .filter(|(_, d)| **d == shard_id)
                .map(|(e, _)| e.as_bytes())
                .collect();
            let popped = manager.with_shard(shard_id, |s| s.pop_batch(1024)).unwrap();
            assert_eq!(popped.len(), expected_payloads.len());
            for (i, ev) in popped.iter().enumerate() {
                assert_eq!(
                    ev.as_bytes(),
                    expected_payloads[i],
                    "shard {} position {} out of order",
                    shard_id,
                    i
                );
            }
        }
    }

    /// Batching past a shard's capacity must account every dropped
    /// event under `DropNewest`: `success` + `events_dropped` =
    /// `len(input)`.
    #[test]
    fn test_ingest_raw_batch_drop_accounting() {
        // Single shard, usable capacity 3 (ring buffer reserves one slot).
        let manager = ShardManager::new(1, 4, BackpressureMode::DropNewest);
        let events: Vec<RawEvent> = (0..10)
            .map(|i| RawEvent::from_str(&format!(r#"{{"i":{}}}"#, i)))
            .collect();

        let (success, unrouted) = manager.ingest_raw_batch(events);
        assert_eq!(success, 3, "only 3 should fit under DropNewest");
        assert_eq!(unrouted, 0, "single-shard config has no unrouted events");

        let stats = manager.stats();
        assert_eq!(stats.events_ingested, 3);
        assert_eq!(stats.events_dropped, 7);
    }

    /// Empty batch is a no-op and must not touch stats.
    #[test]
    fn test_ingest_raw_batch_empty() {
        let manager = ShardManager::new(4, 1024, BackpressureMode::DropNewest);
        assert_eq!(manager.ingest_raw_batch(Vec::new()), (0, 0));
        let stats = manager.stats();
        assert_eq!(stats.events_ingested, 0);
        assert_eq!(stats.events_dropped, 0);
    }

    #[test]
    fn test_remove_shard_requires_dynamic_scaling() {
        // Static mode - no dynamic scaling
        let manager = ShardManager::new(4, 1024, BackpressureMode::DropNewest);

        // Should fail because dynamic scaling is not enabled
        let result = manager.remove_shard(0);
        assert!(result.is_err());
        assert!(matches!(result, Err(ScalingError::InvalidPolicy(_))));
    }

    #[test]
    fn test_add_shard_requires_dynamic_scaling() {
        // Static mode - no dynamic scaling
        let manager = ShardManager::new(4, 1024, BackpressureMode::DropNewest);

        // Should fail because dynamic scaling is not enabled
        let result = manager.add_shard();
        assert!(result.is_err());
        assert!(matches!(result, Err(ScalingError::InvalidPolicy(_))));
    }

    #[test]
    fn test_drain_shard_requires_dynamic_scaling() {
        // Static mode - no dynamic scaling
        let manager = ShardManager::new(4, 1024, BackpressureMode::DropNewest);

        // Should fail because dynamic scaling is not enabled
        let result = manager.drain_shard(0);
        assert!(result.is_err());
        assert!(matches!(result, Err(ScalingError::InvalidPolicy(_))));
    }

    #[test]
    fn test_drop_oldest_counts_dropped_events() {
        let manager = ShardManager::new(1, 4, BackpressureMode::DropOldest);

        // Fill the buffer (capacity 4, usable 3)
        for i in 0..3 {
            manager.ingest(json!({"i": i})).unwrap();
        }

        // This should succeed by dropping the oldest event
        manager.ingest(json!({"i": 999})).unwrap();

        let stats = manager.stats();
        assert_eq!(stats.events_ingested, 4);
        // The initial push fails (counted as dropped), then retry succeeds
        assert_eq!(
            stats.events_dropped, 1,
            "DropOldest cycle should count exactly one drop"
        );
    }

    #[test]
    fn test_drop_oldest_raw_counts_dropped_events() {
        let manager = ShardManager::new(1, 4, BackpressureMode::DropOldest);

        // Fill the buffer
        for i in 0..3 {
            let raw = RawEvent::from_str(&format!(r#"{{"i": {}}}"#, i));
            manager.ingest_raw(raw).unwrap();
        }

        // This should succeed by dropping the oldest event
        let raw = RawEvent::from_str(r#"{"i": 999}"#);
        manager.ingest_raw(raw).unwrap();

        let stats = manager.stats();
        assert_eq!(stats.events_ingested, 4);
        assert_eq!(
            stats.events_dropped, 1,
            "DropOldest cycle should count exactly one drop"
        );
    }

    /// Pin the current contract for `BackpressureMode::Sample`:
    /// it returns `IngestionError::Sampled` once the buffer fills,
    /// indistinguishable in shape from a `Backpressure` rejection.
    /// Sampling itself ("keep 1 in N events") is **not implemented**
    /// — the comments in `ingest` / `ingest_raw` defer it to "a
    /// higher level" that does not exist. A consumer setting this
    /// mode today gets a rejection signal, never probabilistic
    /// admission.
    ///
    /// This test pins that contract so it cannot quietly change
    /// without an explicit decision. If sampling is ever wired up,
    /// this test will fail and force an update — at which point
    /// the implementer should also add coverage for the
    /// rate-proportional admission rate.
    #[test]
    fn sample_mode_currently_returns_sampled_after_buffer_fills() {
        // TODO(coverage round 2): `BackpressureMode::Sample` is
        // dead-on-arrival until "higher level" sampling lands;
        // see comments at `ShardManager::ingest` / `ingest_raw`.
        let manager = ShardManager::new(1, 4, BackpressureMode::Sample { rate: 2 });

        // Fill the buffer (capacity 4, usable 3).
        for i in 0..3 {
            manager.ingest(json!({"i": i})).unwrap();
        }

        // Both ingest paths must report `Sampled` — not `Backpressure`,
        // not `Ok` — so callers can distinguish the (currently
        // unused) sampling rejection from a hard backpressure
        // rejection in case sampling is wired up later.
        let json_result = manager.ingest(json!({"i": 999}));
        assert!(
            matches!(json_result, Err(IngestionError::Sampled)),
            "Sample mode must return Sampled on a full buffer (got {:?})",
            json_result
        );

        let raw_result = manager.ingest_raw(RawEvent::from_str(r#"{"i": 999}"#));
        assert!(
            matches!(raw_result, Err(IngestionError::Sampled)),
            "Sample mode must return Sampled on a full buffer via ingest_raw (got {:?})",
            raw_result
        );
    }

    #[test]
    fn test_drop_oldest_multiple_cycles() {
        let manager = ShardManager::new(1, 4, BackpressureMode::DropOldest);

        // Fill the buffer (usable capacity 3)
        for i in 0..3 {
            manager.ingest(json!({"i": i})).unwrap();
        }

        // Push 5 more events, each triggers a DropOldest cycle
        for i in 3..8 {
            manager.ingest(json!({"i": i})).unwrap();
        }

        let stats = manager.stats();
        assert_eq!(stats.events_ingested, 8);
        assert_eq!(
            stats.events_dropped, 5,
            "each DropOldest cycle should count one drop"
        );
    }

    /// Regression: BUG_REPORT.md #44 — single-event ingest paths
    /// (`ingest`, `ingest_raw`) used to collapse "shard not in
    /// routing table" into `IngestionError::Backpressure` and never
    /// touch `events_unrouted`. The batch path correctly bumped the
    /// counter. Reconciliation drifts because of this divergence.
    ///
    /// We construct the routing miss by:
    ///   1. Building a dynamic-mode manager with 2 shards.
    ///   2. Calling `add_shard()` which (per the #46 fix) leaves the
    ///      shard in `Provisioning` state — present in the mapper
    ///      but not in `select_shard`'s output.
    ///   3. Then directly forcing `select_shard_by_hash` would still
    ///      return an Active shard, so we exercise the secondary
    ///      routing-table-miss path: remove a shard and have a
    ///      stale hash-derived id.
    ///
    /// The simpler robust check: drain every shard via
    /// `drain_specific` until none Active. The mapper's fallback
    /// now returns `u16::MAX`, which is never in the routing
    /// table, so `resolve_idx` misses and we should see `Unrouted`
    /// + counter bump.
    #[test]
    fn ingest_single_event_unrouted_increments_counter() {
        use crate::config::ScalingPolicy;
        // min_shards=1 so we can drain N-1 of N shards; the last
        // one we skip-mark as Draining via Stopped → drain via
        // scale_down then verify routing miss for the still-active
        // shard's hash.
        let policy = ScalingPolicy {
            min_shards: 1,
            max_shards: 8,
            cooldown: std::time::Duration::from_nanos(1),
            ..Default::default()
        };
        let manager =
            ShardManager::with_mapper(2, 1024, BackpressureMode::DropNewest, policy).unwrap();

        // Drain 1 of 2 shards via the public API.
        let mapper = manager.mapper().unwrap().clone();
        let _ = mapper.scale_down(1).unwrap();

        // Find a hash that routes to the *drained* shard (the one
        // not in `active_shard_ids`). With weighted selection and
        // only one Active shard, `select_shard` always returns the
        // Active one, so we can't easily target the drained shard
        // through hash routing — what we *can* do is verify the
        // Active shard still routes correctly (no false positives).
        let active_ids = mapper.active_shard_ids();
        assert_eq!(active_ids.len(), 1);
        let active = active_ids[0];

        // ingest a few events; all should land on the Active shard,
        // none should hit Unrouted.
        for i in 0..5 {
            let r = manager.ingest_raw(RawEvent::from_str(&format!(r#"{{"i":{}}}"#, i)));
            let (sid, _) = r.expect("active shard must accept ingest");
            assert_eq!(sid, active, "must route to the active shard");
        }
        // No unrouted events — sanity that Unrouted only fires on
        // actual routing misses.
        assert_eq!(manager.stats().events_unrouted, 0);

        // Now exercise the actual #44 fix: when *no* Active shard
        // exists, `select_shard` returns `u16::MAX` (per #51), which
        // is unmappable. To set this up without mutating private
        // fields, we rely on the fact that the manager's `with_mapper`
        // returns `Arc<ShardMapper>` and `drain_specific` will refuse
        // to take active_count below min_shards. So we simulate the
        // race by directly using `ingest_raw` with a forged
        // RawEvent whose hash WILL be modulo'd to a non-existent id
        // — but in dynamic mode the mapper rules, not modulo. We
        // can't easily get there from here, so we instead validate
        // the mechanism via a separate static-mode test below.
        //
        // The above sanity-check that Active shards still route
        // correctly + the mapper-level test
        // `select_shard_does_not_fall_back_to_draining` together
        // cover the #44 + #51 contract. Adding a routing-table-
        // miss test here would require a `#[cfg(test)] fn` that
        // can mutate the routing table, which we deliberately
        // avoid (the manager's invariants must hold even from
        // tests).
    }

    /// Regression: BUG_REPORT.md #47 — `remove_shard` previously
    /// just unmapped the shard from the routing table and let the
    /// drain worker observe `with_shard → None` and exit. Anything
    /// still queued in the ring buffer at that moment was silently
    /// stranded. The fix returns the drained events to the caller
    /// (typically `EventBus::remove_shard_internal`) so they can
    /// be flushed through to the adapter rather than dropped.
    #[test]
    fn remove_shard_returns_stranded_ring_buffer_events() {
        use crate::config::ScalingPolicy;
        let policy = ScalingPolicy {
            min_shards: 1,
            max_shards: 8,
            cooldown: std::time::Duration::from_nanos(1),
            ..Default::default()
        };
        let manager =
            ShardManager::with_mapper(2, 1024, BackpressureMode::DropNewest, policy).unwrap();

        // Pin the routing for shard 1 by ingesting events with a
        // hash known to land there. We don't actually need
        // hash-routing precision: directly push into shard 1 via
        // `with_shard`, which bypasses select_shard.
        let pushed: Vec<&str> = vec![r#"{"a":1}"#, r#"{"a":2}"#, r#"{"a":3}"#];
        let pushed_count = pushed.len();
        for s in &pushed {
            manager
                .with_shard(1, |shard| {
                    shard.try_push_raw(bytes::Bytes::from(s.as_bytes().to_vec()))
                })
                .expect("shard 1 exists")
                .expect("ring buffer has room");
        }
        assert_eq!(
            manager.with_shard(1, |s| s.len()).unwrap(),
            pushed_count,
            "events should be queued in shard 1"
        );

        // Remove shard 1 — must return the stranded events, not
        // drop them silently.
        let stranded = manager
            .remove_shard(1)
            .expect("remove_shard must succeed in dynamic mode");
        assert_eq!(
            stranded.len(),
            pushed_count,
            "remove_shard must surface every event still in the \
             ring buffer (#47); got {} stranded events, expected {}",
            stranded.len(),
            pushed_count
        );

        // Sanity: the events come back in FIFO order with the
        // bytes the producer pushed.
        for (i, ev) in stranded.iter().enumerate() {
            assert_eq!(ev.as_bytes(), pushed[i].as_bytes());
            assert_eq!(ev.shard_id, 1);
        }

        // Sanity: shard 1 is gone from routing.
        assert!(manager.with_shard(1, |s| s.id).is_none());
    }

    /// `ShardManager::activate_shard` is idempotent at
    /// the API level — two calls on the same shard return Ok(())
    /// each — but pre-fix `num_shards` was bumped on every call
    /// even when the mapper's `activate()` had already
    /// transitioned the shard to Active. After repeated calls,
    /// `num_shards` exceeded the actual count and `select_shard`'s
    /// modulo arithmetic mis-routed.
    #[test]
    fn activate_shard_is_idempotent_in_num_shards_count() {
        let policy = ScalingPolicy {
            min_shards: 1,
            max_shards: 16,
            cooldown: std::time::Duration::from_nanos(1),
            ..Default::default()
        };
        let manager = ShardManager::with_mapper(2, 1024, BackpressureMode::DropOldest, policy)
            .expect("dynamic scaling enabled");
        let initial = manager.num_shards();
        assert_eq!(initial, 2);

        // Add + activate a new shard. count goes 2 → 3.
        let new_id = manager.add_shard().expect("add_shard");
        manager.activate_shard(new_id).expect("first activate");
        assert_eq!(
            manager.num_shards(),
            3,
            "first activate must bump num_shards to 3"
        );

        // Repeat activate — must be a no-op on the count.
        manager
            .activate_shard(new_id)
            .expect("second activate (idempotent)");
        manager
            .activate_shard(new_id)
            .expect("third activate (idempotent)");
        assert_eq!(
            manager.num_shards(),
            3,
            "repeated activate_shard must NOT keep bumping num_shards; \
             pre-fix this would be 5 after three calls",
        );
    }

    /// Removing a still-`Provisioning` shard (the activate-failure
    /// rollback path) must NOT decrement `num_shards`. `add_shard`
    /// only registers a `Provisioning` entry and intentionally
    /// leaves `num_shards` alone — the bump happens in
    /// `activate_shard`. A symmetric `fetch_sub` in `remove_shard`
    /// would therefore leave the counter one below the routing
    /// table's actual size after a rollback, breaking modulo-based
    /// shard selection. This pins the gating: the rollback removal
    /// is a num_shards no-op, while removing an activated shard
    /// still decrements normally.
    #[test]
    fn remove_provisioning_shard_does_not_decrement_num_shards() {
        let policy = ScalingPolicy {
            min_shards: 1,
            max_shards: 16,
            cooldown: std::time::Duration::from_nanos(1),
            ..Default::default()
        };
        let manager = ShardManager::with_mapper(2, 1024, BackpressureMode::DropOldest, policy)
            .expect("dynamic scaling enabled");
        let initial = manager.num_shards();
        assert_eq!(initial, 2);

        // add_shard registers a Provisioning entry (no num_shards bump).
        let new_id = manager.add_shard().expect("add_shard");
        assert_eq!(
            manager.num_shards(),
            initial,
            "add_shard must NOT bump num_shards (Provisioning, not yet selectable)"
        );

        // Simulate the activate-failure rollback path: remove the
        // never-activated shard. Pre-fix this fired
        // `fetch_sub(1)` unconditionally and dropped num_shards
        // below the table size.
        let stranded = manager.remove_shard(new_id).expect("rollback remove");
        assert!(
            stranded.is_empty(),
            "fresh provisioning shard has no events"
        );
        assert_eq!(
            manager.num_shards(),
            initial,
            "removing a provisioning (never-activated) shard must NOT decrement num_shards"
        );

        // Companion: removing an activated shard still decrements,
        // so the gate is symmetric with activate_shard's fetch_add.
        let activated_id = manager.add_shard().expect("add for activated path");
        manager.activate_shard(activated_id).expect("activate");
        assert_eq!(
            manager.num_shards(),
            initial + 1,
            "activate bumps num_shards"
        );
        manager
            .remove_shard(activated_id)
            .expect("remove activated");
        assert_eq!(
            manager.num_shards(),
            initial,
            "removing an activated shard MUST decrement num_shards"
        );
    }
}