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
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
//! Share consumer implementation (KIP-932).
//!
//! > ⚠️ **Unstable**: This module requires the `unstable-protocol` feature flag.
//! > APIs may change without semver notice until KIP-932 is finalized in a stable
//! > Kafka release.
//!
//! Share groups provide queue-like semantics on top of Kafka topics. Multiple
//! consumers in the same share group receive non-overlapping subsets of records
//! without client-side partition assignment — all assignment is performed by
//! the server.
//!
//! # Delivery Semantics
//!
//! Share groups support **at-least-once** delivery with explicit or implicit
//! acknowledgement:
//!
//! - **Implicit** (default): previously fetched records are automatically
//! accepted when the next `poll()` is called.
//! - **Explicit**: the application calls
//! [`acknowledge()`](ShareConsumer::acknowledge) per record and then
//! [`commit_sync()`](ShareConsumer::commit_sync) to flush.
//!
//! Records that are released or not acknowledged within the acquisition lock
//! timeout are redelivered to other consumers.
//!
//! # Example
//!
//! ```ignore
//! use krafka::share_consumer::{ShareConsumer, AcknowledgementMode};
//!
//! let consumer = ShareConsumer::builder()
//! .bootstrap_servers("localhost:9092")
//! .group_id("my-share-group")
//! .build()
//! .await?;
//!
//! consumer.subscribe(&["events"]).await?;
//!
//! loop {
//! let records = consumer.poll(Duration::from_secs(1)).await?;
//! for record in &records {
//! process(record);
//! }
//! // Implicit mode: records are auto-accepted on next poll()
//! }
//! ```
mod config;
mod session;
mod stream;
pub use config::{AcknowledgeType, AcknowledgementMode, ShareConsumerConfig};
pub use stream::ShareConsumerStream;
use ahash::{AHashMap as HashMap, AHashSet as HashSet};
use std::collections::VecDeque;
use std::future::{Future, Ready, ready};
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, AtomicI32, AtomicU64, Ordering};
use std::sync::{Arc, Mutex as SyncMutex, Weak};
use std::task::{Context, Poll};
use std::time::Duration;
use arc_swap::ArcSwap;
use tokio::sync::{Notify, RwLock};
use tracing::{debug, info, warn};
use crate::auth::AuthConfig;
use crate::barrier::InFlightBarrier;
use crate::consumer::ConsumerRecord;
use crate::error::{ErrorCode, KrafkaError, ProtocolErrorKind, Result};
use crate::metadata::ClusterMetadata;
use crate::metrics::ConnectionMetrics;
use crate::network::{ConnectionConfig, ConnectionPool};
use crate::protocol::{
ApiKey, FindCoordinatorRequest, FindCoordinatorResponse, RecordBatch,
ShareAcknowledgePartition, ShareAcknowledgeRequest, ShareAcknowledgeTopic,
ShareAcknowledgementBatch, ShareFetchPartition, ShareFetchRequest, ShareFetchTopic,
ShareGroupHeartbeatRequest, ShareGroupHeartbeatResponse, ShareGroupTopicPartitions,
VersionedDecode, VersionedEncode, versions,
};
use crate::{BrokerId, Offset, PartitionId};
use session::{FINAL_EPOCH, ShareSessionCache};
/// Key for tracking unacknowledged records in explicit mode.
type RecordKey = (String, PartitionId, Offset);
/// Key for piggybacked acknowledgements grouped by broker and partition.
type BrokerAckKey = ([u8; 16], PartitionId);
/// Pending piggybacked acknowledgements grouped by broker and partition.
type BrokerPendingAcks = HashMap<BrokerId, HashMap<BrokerAckKey, Vec<PendingAck>>>;
/// Sentinel broker ID used when the partition leader is not yet known at acknowledge
/// time (e.g., immediately after `subscribe()` before the first metadata refresh, or
/// after a `restore_ack_state()` where metadata is unavailable).
/// Acks with this sentinel are re-routed using fresh metadata in `poll()`.
const UNROUTED_BROKER_ID: BrokerId = -2;
/// Wire value for the KIP-932 "gap" acknowledgement type.
///
/// A gap tells the broker that the client is *not* taking delivery of an offset
/// inside an acquired range — typically because the record could not be
/// decoded. The broker archives the offset instead of redelivering it, which is
/// what prevents an undecodable offset from being redelivered forever with an
/// ever-climbing `delivery_count`.
///
/// Deliberately not exposed on [`AcknowledgeType`]: applications never choose
/// it, the client emits it on their behalf.
const GAP_ACK_TYPE: i8 = 0;
/// Minimum negotiated `ShareFetch`/`ShareAcknowledge` version that understands
/// [`AcknowledgeType::Renew`] (KIP-1222, Kafka 4.2+).
///
/// Older brokers reject an entire acknowledgement batch with `INVALID_REQUEST`
/// when it contains an unknown acknowledgement type, so `Renew` acks are
/// dropped rather than sent to a broker that negotiated a lower version.
const RENEW_MIN_VERSION: i16 = 2;
/// Maximum number of times a `ShareAcknowledge` is retried after the broker
/// reports that the share session is stale or unavailable.
const SHARE_SESSION_RETRY_LIMIT: usize = 2;
/// Backoff applied before retrying after `SHARE_SESSION_LIMIT_REACHED` (133),
/// which is a capacity signal rather than a stale-state signal.
const SHARE_SESSION_LIMIT_BACKOFF: Duration = Duration::from_millis(100);
/// Minimum interval between two `recv()` fetch attempts when the broker keeps
/// returning empty responses, so an idle topic cannot spin the CPU.
const RECV_EMPTY_POLL_BACKOFF: Duration = Duration::from_millis(100);
/// Returns `true` when a `ShareAcknowledge`/`ShareFetch` error means the
/// per-broker share session must be torn down and re-established.
fn is_share_session_error(code: ErrorCode) -> bool {
matches!(
code,
ErrorCode::ShareSessionNotFound
| ErrorCode::InvalidShareSessionEpoch
| ErrorCode::ShareSessionLimitReached
)
}
/// Restores in-flight acknowledgements if the future holding them is dropped.
///
/// `poll()` and the commit paths drain `pending_acks` into a local `Vec` before
/// they can possibly succeed. Without this guard, dropping the future — a
/// `select!` shutdown arm, or simply dropping the record stream — would
/// silently discard every drained acknowledgement, including explicit
/// `Reject`/`Release` decisions the application already made.
///
/// Call [`disarm`](Self::disarm) once the acknowledgements are known to have
/// been handled; anything still armed at drop time is re-queued under
/// [`UNROUTED_BROKER_ID`] for the next `poll()` to re-route.
struct PendingAckGuard {
acks: Vec<PendingAck>,
pending_acks: Arc<RwLock<BrokerPendingAcks>>,
current_generation: Arc<AtomicU64>,
captured_generation: u64,
explicit_flush_retry_required: Arc<AtomicBool>,
require_explicit_retry: bool,
}
impl PendingAckGuard {
fn new(
acks: Vec<PendingAck>,
pending_acks: Arc<RwLock<BrokerPendingAcks>>,
current_generation: Arc<AtomicU64>,
captured_generation: u64,
explicit_flush_retry_required: Arc<AtomicBool>,
require_explicit_retry: bool,
) -> Self {
Self {
acks,
pending_acks,
current_generation,
captured_generation,
explicit_flush_retry_required,
require_explicit_retry,
}
}
/// Borrow the protected acknowledgements.
fn acks(&self) -> &[PendingAck] {
&self.acks
}
/// Take the acknowledgements out of the guard, disarming it.
fn disarm(&mut self) -> Vec<PendingAck> {
std::mem::take(&mut self.acks)
}
}
impl Drop for PendingAckGuard {
fn drop(&mut self) {
if self.acks.is_empty() {
return;
}
let mut acks = std::mem::take(&mut self.acks);
// Restoring needs the async RwLock, so it cannot happen inline in
// `drop`. Take the uncontended path when possible and only fall back to
// spawning when the lock is held elsewhere.
if let Ok(mut pending) = self.pending_acks.try_write() {
if self.current_generation.load(Ordering::SeqCst) == self.captured_generation {
if self.require_explicit_retry {
self.explicit_flush_retry_required
.store(true, Ordering::SeqCst);
}
for ack in acks.drain(..) {
pending
.entry(UNROUTED_BROKER_ID)
.or_default()
.entry((ack.topic_id, ack.partition))
.or_default()
.push(ack);
}
}
return;
}
let pending_acks = self.pending_acks.clone();
let current_generation = self.current_generation.clone();
let explicit_flush_retry_required = self.explicit_flush_retry_required.clone();
let captured_generation = self.captured_generation;
let require_explicit_retry = self.require_explicit_retry;
if tokio::runtime::Handle::try_current().is_ok() {
tokio::spawn(async move {
ShareConsumer::restore_ack_state(
current_generation.as_ref(),
pending_acks.as_ref(),
explicit_flush_retry_required.as_ref(),
captured_generation,
require_explicit_retry,
&mut acks,
)
.await;
});
} else {
warn!(
count = acks.len(),
"share acknowledgements dropped outside a Tokio runtime; \
the affected records will be redelivered"
);
}
}
}
/// Result of a multi-broker `ShareAcknowledge` round.
///
/// Acknowledgements are grouped by partition leader and sent per broker, so a
/// single round can partially succeed. Only the acknowledgements in
/// [`failed`](Self::failed) must be re-queued; re-queueing the whole batch
/// would make the retry re-acknowledge offsets other brokers already accepted,
/// which they reject with `INVALID_RECORD_STATE`.
#[derive(Default)]
struct ShareAcknowledgeOutcome {
/// Acknowledgements that were not accepted by their broker.
failed: Vec<PendingAck>,
/// First error observed, if any.
error: Option<KrafkaError>,
}
impl ShareAcknowledgeOutcome {
fn fail(&mut self, acks: impl IntoIterator<Item = PendingAck>, error: KrafkaError) {
self.failed.extend(acks);
if self.error.is_none() {
self.error = Some(error);
}
}
}
#[derive(Clone)]
struct ShareAcknowledgeContext {
metadata: Arc<ClusterMetadata>,
pool: Arc<ConnectionPool>,
share_sessions: Arc<tokio::sync::Mutex<ShareSessionCache>>,
group_id: String,
member_id: String,
current_ack_state_generation: Arc<AtomicU64>,
ack_state_generation: u64,
}
/// Pending acknowledgement for a share group record.
#[derive(Debug, Clone)]
struct PendingAck {
topic: String,
topic_id: [u8; 16],
partition: PartitionId,
first_offset: Offset,
last_offset: Offset,
ack_type: i8,
}
fn flatten_partition_acks(
partition_acks: HashMap<BrokerAckKey, Vec<PendingAck>>,
) -> Vec<PendingAck> {
partition_acks.into_values().flatten().collect()
}
fn drain_broker_partition_acks(
broker_acks: &mut HashMap<BrokerAckKey, Vec<PendingAck>>,
topic_id: [u8; 16],
partition: PartitionId,
) -> Vec<PendingAck> {
broker_acks
.remove(&(topic_id, partition))
.unwrap_or_default()
}
fn drain_broker_acks(broker_acks: &mut BrokerPendingAcks, broker_id: BrokerId) -> Vec<PendingAck> {
broker_acks
.remove(&broker_id)
.map(flatten_partition_acks)
.unwrap_or_default()
}
/// Remove [`AcknowledgeType::Renew`] entries from acknowledgement batches that
/// are about to be sent to a broker that does not support KIP-1222.
///
/// Returns the number of batches removed. A batch whose only acknowledgement
/// type is `Renew` is dropped entirely; mixed batches keep their other types.
fn strip_unsupported_renew_acks<'a, I>(batches: I) -> usize
where
I: Iterator<Item = &'a mut Vec<ShareAcknowledgementBatch>>,
{
let renew = AcknowledgeType::Renew.to_i8();
let mut dropped = 0usize;
for batch_list in batches {
let before = batch_list.len();
batch_list.retain_mut(|batch| {
batch.acknowledge_types.retain(|&t| t != renew);
!batch.acknowledge_types.is_empty()
});
dropped += before - batch_list.len();
}
dropped
}
/// Build the offset → delivery-count map for one partition response.
///
/// A malformed or desynchronised response can carry an inverted range
/// (`last_offset < first_offset`) or an absurdly wide one such as
/// `0..=i64::MAX`. Materialising that range would allocate until the process is
/// killed, inside a loop that never yields to the runtime. Inverted ranges are
/// rejected and the total number of tracked offsets is capped at `max_offsets`,
/// which callers derive from the size of the encoded record data — each record
/// occupies at least one byte, so the byte length is an upper bound on the
/// number of records that can possibly be decoded.
fn build_delivery_counts(
acquired: &[crate::protocol::ShareAcquiredRecords],
max_offsets: usize,
) -> HashMap<Offset, i16> {
let mut counts: HashMap<Offset, i16> = HashMap::new();
if max_offsets == 0 {
return counts;
}
for range in acquired {
if range.last_offset < range.first_offset {
warn!(
first_offset = range.first_offset,
last_offset = range.last_offset,
"ignoring inverted acquired-record range in ShareFetch response"
);
continue;
}
// `last - first` cannot overflow because last >= first, but the +1 can.
let width = (range.last_offset - range.first_offset).saturating_add(1);
let remaining = max_offsets.saturating_sub(counts.len());
if remaining == 0 {
warn!("acquired-record ranges exceed the decodable record count; truncating");
break;
}
let take = width.min(remaining as i64);
if take < width {
warn!(
first_offset = range.first_offset,
last_offset = range.last_offset,
take,
"acquired-record range is wider than the decodable record count; truncating"
);
}
for offset in range.first_offset..range.first_offset.saturating_add(take) {
counts.insert(offset, range.delivery_count);
}
}
counts
}
/// Build [`GAP_ACK_TYPE`] acknowledgements for offsets the broker acquired for
/// this client but that could not be decoded.
///
/// Without these, an undecodable offset is never acknowledged, so the broker
/// redelivers it after every acquisition-lock timeout and `delivery_count`
/// climbs without bound. Contiguous missing offsets are coalesced into ranges.
fn build_gap_acks(
topic: &str,
topic_id: [u8; 16],
partition: PartitionId,
acquired: &HashMap<Offset, i16>,
decoded: &HashSet<Offset>,
) -> Vec<PendingAck> {
let mut missing: Vec<Offset> = acquired
.keys()
.copied()
.filter(|offset| !decoded.contains(offset))
.collect();
if missing.is_empty() {
return Vec::new();
}
missing.sort_unstable();
let mut acks = Vec::new();
let mut i = 0;
while i < missing.len() {
let first = missing[i];
let mut last = first;
while i + 1 < missing.len() && missing[i + 1] == last + 1 {
i += 1;
last = missing[i];
}
acks.push(PendingAck {
topic: topic.to_string(),
topic_id,
partition,
first_offset: first,
last_offset: last,
ack_type: GAP_ACK_TYPE,
});
i += 1;
}
acks
}
fn describe_share_fetch_join_error(error: &tokio::task::JoinError) -> &'static str {
if error.is_panic() {
"panicked"
} else if error.is_cancelled() {
"was cancelled"
} else {
"failed"
}
}
/// Handle returned by [`ShareConsumer::commit_async`].
///
/// Await the handle to observe the final broker outcome. Dropping it detaches
/// the background task and discards the result.
#[must_use = "await the returned handle to observe share-commit outcome"]
#[non_exhaustive]
pub enum ShareCommitHandle {
/// Immediate commit result without spawning a background task.
Ready(Ready<Result<()>>),
/// Background task handle that resolves to the commit result.
Task(tokio::task::JoinHandle<Result<()>>),
}
impl ShareCommitHandle {
fn ready(result: Result<()>) -> Self {
Self::Ready(ready(result))
}
}
impl Future for ShareCommitHandle {
type Output = Result<()>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.get_mut() {
Self::Ready(fut) => Pin::new(fut).poll(cx),
Self::Task(handle) => match Pin::new(handle).poll(cx) {
Poll::Ready(Ok(result)) => Poll::Ready(result),
Poll::Ready(Err(error)) => Poll::Ready(Err(KrafkaError::invalid_state(format!(
"share commit task failed: {error}"
)))),
Poll::Pending => Poll::Pending,
},
}
}
}
/// All internal state shared between `ShareConsumer` handles.
struct ShareConsumerInner {
/// Configuration.
config: ShareConsumerConfig,
/// Cluster metadata.
metadata: Arc<ClusterMetadata>,
/// Connection pool.
pool: Arc<ConnectionPool>,
/// Whether this client owns its connection pool.
///
/// `false` when the pool was borrowed from a
/// [`KrafkaClient`](crate::client::KrafkaClient) via `with_client`.
pool_owned: bool,
/// Application-level metrics.
///
/// Reuses [`ConsumerMetrics`](crate::metrics::ConsumerMetrics) rather than defining a share-group-specific
/// type: a share consumer polls, receives records, acknowledges and hits
/// errors exactly as a classic consumer does, and the counters mean the
/// same thing. `commits` counts acknowledgement flushes, which is the
/// share-group analogue of an offset commit.
///
/// Rebalance, lag and partition gauges are left at zero — the coordinator
/// owns assignment and there is no per-partition position to lag behind.
metrics: Arc<crate::metrics::ConsumerMetrics>,
/// Subscribed topics.
subscriptions: RwLock<HashSet<String>>,
/// Current partition assignments from the coordinator.
/// Maps topic name → partition IDs.
assignments: RwLock<HashMap<String, Vec<PartitionId>>>,
/// Client-generated member ID (UUID, per KIP-932).
member_id: ArcSwap<String>,
/// Current member epoch.
member_epoch: AtomicI32,
/// Heartbeat interval returned by the coordinator.
heartbeat_interval_ms: AtomicI32,
/// Whether the consumer is closed.
closed: AtomicBool,
/// Per-broker share session cache.
share_sessions: Arc<tokio::sync::Mutex<ShareSessionCache>>,
/// Pending acknowledgements sharded by broker ID.
///
/// Pre-routing at acknowledge time means `poll()` can hand each broker its
/// acks in O(1) without rescanning the entire flat map. Acks whose leader
/// is not yet known are stored under `UNROUTED_BROKER_ID` and re-routed
/// using fresh metadata at the start of the next `poll()`.
pending_acks: Arc<RwLock<BrokerPendingAcks>>,
/// Monotonic token for the current local ack state.
///
/// Incremented whenever local ack state is cleared or invalidated so
/// detached flush tasks cannot send or requeue stale acknowledgements
/// from an older membership/session after assignment changes,
/// `unsubscribe()`, or `close()`.
ack_state_generation: Arc<AtomicU64>,
/// Explicit-mode barrier raised after a commit flush fails.
///
/// While this is set, `poll()` refuses to fetch more records until the
/// application retries `commit_sync()`/`commit_async()` successfully or
/// local state is cleared during unsubscribe/close.
explicit_flush_retry_required: Arc<AtomicBool>,
/// Topic name → UUID cache (populated from heartbeat assignments and metadata).
topic_ids: RwLock<HashMap<String, [u8; 16]>>,
/// Records fetched but not yet handed to the application.
///
/// Holds the tail of a `ShareFetch` response that exceeded
/// `max_poll_records`. Records in this buffer are **not** yet
/// acknowledgement-tracked: implicit accepts are queued and explicit
/// `unacked_offsets` entries are created only when a record is actually
/// returned to the caller, so a record can never be acknowledged without
/// having been delivered.
recv_buffer: RwLock<VecDeque<ConsumerRecord>>,
/// Coordinator broker ID (discovered via FindCoordinator).
coordinator_id: RwLock<Option<BrokerId>>,
/// Coordinator address (host:port).
coordinator_address: RwLock<Option<String>>,
/// Tracks unacknowledged records from the previous `poll()` in explicit mode.
/// Must be empty before the next `poll()` can fetch new records.
unacked_offsets: Arc<RwLock<HashSet<RecordKey>>>,
/// Background heartbeat task. Spawned on the first `subscribe()` call;
/// aborted by `close()` or `unsubscribe()`.
heartbeat_task: SyncMutex<Option<tokio::task::JoinHandle<()>>>,
/// Set by `wakeup()` to interrupt an in-progress `poll()`.
/// Cleared at the start of each `poll()` call.
wakeup_flag: AtomicBool,
/// Signalled by `wakeup()` so a `poll()` already blocked on a `ShareFetch`
/// is interrupted instead of having to run to completion.
wakeup_notify: Notify,
/// Tracks polls that have started but not finished.
///
/// `poll()` drains every pending acknowledgement into a `PendingAckGuard`
/// for the duration of its `ShareFetch`, so during that window the acks are
/// invisible to `pending_acks`. Without this barrier a concurrent
/// `commit_sync()` or `close()` would drain an empty map, report success,
/// and leave those acknowledgements to be restored by the guard moments
/// later with nobody left to send them.
///
/// That is not a theoretical interleaving: the documented shutdown is
/// `wakeup()` followed by `close()`, and `wakeup()` does not wait for the
/// poll it interrupts to unwind.
in_flight_polls: Arc<InFlightBarrier>,
/// Acquisition-lock duration most recently reported by a broker
/// (KIP-1222), in milliseconds, or `-1` before the first `ShareFetch`.
acquisition_lock_timeout_ms: AtomicI32,
/// Optional decoder applied to every consumed record's key.
key_deserializer: Option<Arc<dyn crate::serdes::Deserializer>>,
/// Optional decoder applied to every consumed record's value.
value_deserializer: Option<Arc<dyn crate::serdes::Deserializer>>,
}
impl Drop for ShareConsumerInner {
fn drop(&mut self) {
// The background heartbeat task only holds a `Weak` reference, so it
// would exit on its own; aborting makes that immediate rather than
// waiting up to one heartbeat interval.
if let Some(handle) = self
.heartbeat_task
.lock()
.unwrap_or_else(|e| e.into_inner())
.take()
{
handle.abort();
}
// Fires when the last `ShareConsumer` clone is dropped.
// Warn if close() was never called — pending acks are silently lost
// and the coordinator will only reclaim partitions after the heartbeat
// lease expires. Skip the warning during panic unwinding.
if !self.closed.load(Ordering::Relaxed) && !std::thread::panicking() {
warn!(
"ShareConsumer dropped without close(); pending acks may be lost and \
share-group rebalance will be delayed. Call `ShareConsumer::close()` before drop."
);
}
}
}
/// A Kafka share consumer (KIP-932).
///
/// Provides queue-like consumption semantics where the server controls
/// partition assignment and record delivery. Unlike traditional consumer
/// groups, share consumers do not track offsets — instead they acknowledge
/// individual records.
///
/// `ShareConsumer` is cheaply cloneable: all clones share the same
/// connection pool, coordinator state, and acknowledgement buffers via an
/// internal [`Arc`]. A background heartbeat task is started on the first
/// [`subscribe()`](Self::subscribe) call and stopped on
/// [`close()`](Self::close) / [`unsubscribe()`](Self::unsubscribe).
#[derive(Clone)]
pub struct ShareConsumer(Arc<ShareConsumerInner>);
impl std::fmt::Debug for ShareConsumer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ShareConsumer")
.field("group_id", &self.0.config.group_id)
.field("closed", &self.0.closed.load(Ordering::Relaxed))
.finish_non_exhaustive()
}
}
impl ShareConsumer {
/// Create a new share consumer builder.
pub fn builder() -> ShareConsumerBuilder {
ShareConsumerBuilder::default()
}
/// Create a new share consumer with the given configuration.
async fn new(
config: ShareConsumerConfig,
shared: Option<(Arc<ConnectionPool>, Arc<ClusterMetadata>)>,
key_deserializer: Option<Arc<dyn crate::serdes::Deserializer>>,
value_deserializer: Option<Arc<dyn crate::serdes::Deserializer>>,
) -> Result<Self> {
let pool_owned = shared.is_none();
let (pool, metadata) = if let Some((pool, metadata)) = shared {
(pool, metadata)
} else {
let mut pool_config_builder = config.transport.apply(
ConnectionConfig::builder()
.client_id(&config.client_id)
.request_timeout(config.request_timeout)
.connect_timeout(config.connect_timeout),
);
if let Some(ref auth) = config.auth {
pool_config_builder = pool_config_builder.auth(auth.clone());
}
let mut pool_config = pool_config_builder.build()?;
pool_config.init_tls().await?;
// Every client builds its pool through `TransportConfig::build_pool`,
// which applies the pool-level settings and starts the background
// tasks (idle eviction, OAUTHBEARER refresh, KIP-1288 TLS reload).
// Routing all construction sites through one function is what stops
// them drifting apart again.
let pool = config.transport.build_pool(pool_config);
let bootstrap_servers =
crate::util::parse_bootstrap_servers(&config.bootstrap_servers)?;
let metadata = Arc::new({
let mut meta =
ClusterMetadata::new(bootstrap_servers, pool.clone(), config.metadata_max_age)
.with_recovery_strategy(config.metadata_recovery_strategy)
.with_rebootstrap_trigger(config.metadata_recovery_rebootstrap_trigger);
if let Some(ttl) = config.metadata_topic_cache_ttl {
meta = meta.with_topic_cache_ttl(ttl);
} else {
meta = meta.with_topic_cache_ttl_disabled();
}
meta = meta.with_auto_create_topics(config.allow_auto_create_topics);
meta
});
metadata.refresh().await?;
(pool, metadata)
};
info!(
"ShareConsumer initialized with {} brokers, group_id='{}'",
metadata.brokers().len(),
config.group_id
);
Ok(ShareConsumer(Arc::new(ShareConsumerInner {
config,
metadata,
pool,
pool_owned,
metrics: Arc::new(crate::metrics::ConsumerMetrics::new()),
subscriptions: RwLock::new(HashSet::new()),
assignments: RwLock::new(HashMap::new()),
member_id: ArcSwap::new(Arc::new(crate::util::random_uuid_v4())),
member_epoch: AtomicI32::new(0),
heartbeat_interval_ms: AtomicI32::new(5000),
closed: AtomicBool::new(false),
share_sessions: Arc::new(tokio::sync::Mutex::new(ShareSessionCache::new())),
pending_acks: Arc::new(RwLock::new(HashMap::new())),
ack_state_generation: Arc::new(AtomicU64::new(0)),
explicit_flush_retry_required: Arc::new(AtomicBool::new(false)),
topic_ids: RwLock::new(HashMap::new()),
recv_buffer: RwLock::new(VecDeque::new()),
coordinator_id: RwLock::new(None),
coordinator_address: RwLock::new(None),
unacked_offsets: Arc::new(RwLock::new(HashSet::new())),
heartbeat_task: SyncMutex::new(None),
wakeup_flag: AtomicBool::new(false),
wakeup_notify: Notify::new(),
in_flight_polls: Arc::new(InFlightBarrier::new()),
acquisition_lock_timeout_ms: AtomicI32::new(-1),
key_deserializer,
value_deserializer,
})))
}
/// Subscribe to topics.
///
/// Replaces the current subscription. The coordinator is notified on
/// the next heartbeat (during `poll()`).
pub async fn subscribe(&self, topics: &[&str]) -> Result<()> {
if self.0.closed.load(Ordering::SeqCst) {
return Err(KrafkaError::invalid_state("share consumer is closed"));
}
{
let mut subs = self.0.subscriptions.write().await;
subs.clear();
for topic in topics {
subs.insert((*topic).to_string());
}
}
let topic_refs: Vec<&str> = topics.to_vec();
self.0
.metadata
.refresh_for_topics(Some(&topic_refs))
.await?;
// Resolve topic UUIDs from metadata.
{
let mut ids = self.0.topic_ids.write().await;
for topic in topics {
if let Some(uuid) = self.0.metadata.topic_id_for_name(topic) {
ids.insert((*topic).to_string(), uuid);
}
}
}
// Discover the coordinator and send the initial heartbeat.
self.ensure_coordinator().await?;
self.send_heartbeat(true).await?;
// Spawn the background heartbeat task if not already running.
// The task sends periodic heartbeats independent of poll() so the
// share-group session stays alive even when poll() is slow.
let mut task_guard = self
.0
.heartbeat_task
.lock()
.unwrap_or_else(|e| e.into_inner());
if task_guard.as_ref().is_none_or(|h| h.is_finished()) {
// Hand the task a *weak* reference. A strong `Arc` would form a
// reference cycle (inner -> JoinHandle -> task -> inner) that keeps
// the consumer, its connection pool, and its group membership alive
// forever when the application drops every handle without calling
// `close()` — and would make the drop warning below unreachable.
let bg = Arc::downgrade(&self.0);
*task_guard = Some(tokio::spawn(async move {
Self::run_heartbeat_loop(bg).await;
}));
}
drop(task_guard);
debug!(
"Subscribed to {} topic(s) in share group '{}'",
topics.len(),
self.0.config.group_id
);
Ok(())
}
/// Returns the current subscription.
pub async fn subscription(&self) -> HashSet<String> {
self.0.subscriptions.read().await.clone()
}
/// Returns the current partition assignments.
pub async fn assignment(&self) -> HashMap<String, Vec<PartitionId>> {
self.0.assignments.read().await.clone()
}
/// Returns the member ID assigned by the coordinator.
pub fn member_id(&self) -> String {
(**self.0.member_id.load()).clone()
}
/// Returns the current member epoch.
pub fn member_epoch(&self) -> i32 {
self.0.member_epoch.load(Ordering::Acquire)
}
/// Poll for new records.
///
/// In implicit acknowledgement mode, previously fetched records are
/// automatically accepted. In explicit mode, all records from the
/// previous poll must be acknowledged before calling this again.
///
/// At most `max_poll_records` records are returned. A `ShareFetch` that
/// acquires more than that is **not** truncated on the floor: the surplus
/// is buffered and returned by subsequent `poll()` calls, and only the
/// records actually handed to the caller are acknowledgement-tracked.
pub async fn poll(&self, timeout: Duration) -> Result<Vec<ConsumerRecord>> {
let max = self.0.config.max_poll_records as usize;
let _timer = self.0.metrics.poll_latency.start();
self.0.metrics.polls.inc();
let result = self.poll_inner(timeout, max).await;
match &result {
Ok(records) if records.is_empty() => self.0.metrics.empty_polls.inc(),
Ok(_) => {}
Err(_) => self.0.metrics.record_error(),
}
result
}
/// Shared implementation of [`poll()`](Self::poll) and [`recv()`](Self::recv).
///
/// `max_records` bounds how many records are returned to the caller. Every
/// returned record — and only a returned record — is registered for
/// acknowledgement before this function hands it over. Surplus records go
/// to `recv_buffer` untracked.
async fn poll_inner(
&self,
timeout: Duration,
max_records: usize,
) -> Result<Vec<ConsumerRecord>> {
if self.0.closed.load(Ordering::SeqCst) {
return Err(KrafkaError::invalid_state("share consumer is closed"));
}
// Clear and check the wakeup flag before doing any work so callers
// get an immediate error if wakeup() was called before this poll.
if self.0.wakeup_flag.swap(false, Ordering::AcqRel) {
return Err(KrafkaError::invalid_state("wakeup() was called"));
}
let max_records = max_records.max(1);
// Explicit mode: reject poll if records from the previous batch are unacknowledged.
if self.0.config.acknowledgement_mode == AcknowledgementMode::Explicit {
let unacked = self.0.unacked_offsets.read().await;
if !unacked.is_empty() {
return Err(KrafkaError::invalid_state(
"all records from the previous poll() must be acknowledged before calling poll() again",
));
}
if self.0.explicit_flush_retry_required.load(Ordering::SeqCst) {
return Err(KrafkaError::invalid_state(
"the previous commit_sync()/commit_async() flush failed; retry the commit before calling poll() again",
));
}
}
// If recv_buffer is at capacity, skip only the fetch step (after
// heartbeat/coordination) so group membership remains healthy.
let max_buffered = self.0.config.max_buffered_records;
let skip_fetch_due_to_buffer_cap = if max_buffered > 0 {
let buf_len = self.0.recv_buffer.read().await.len();
buf_len >= max_buffered as usize
} else {
false
};
// Send heartbeat to maintain membership and receive assignments.
// Cap the heartbeat RPC at 10 s so a slow/stuck coordinator does not
// block the entire poll() for the full connection-level request_timeout.
let heartbeat_result =
tokio::time::timeout(Duration::from_secs(10), self.send_heartbeat(false)).await;
let heartbeat_err = match heartbeat_result {
Ok(Ok(())) => None,
Ok(Err(e)) => Some(e),
Err(_elapsed) => Some(KrafkaError::timeout("share group heartbeat")),
};
if let Some(e) = heartbeat_err {
if let KrafkaError::Broker {
code: ErrorCode::FencedMemberEpoch,
..
} = &e
{
warn!(
"Heartbeat fenced during poll for group '{}'; resetting member state",
self.0.config.group_id
);
self.0.member_epoch.store(0, Ordering::Release);
self.clear_ack_state().await;
}
warn!("Heartbeat failed during poll: {e}");
self.invalidate_coordinator().await;
if let Err(e2) = self.ensure_coordinator().await {
warn!("Coordinator rediscovery failed: {e2}");
}
}
// Drain previously buffered records first so mixed `recv()`/`poll()`
// callers do not strand available data. These records were fetched but
// never delivered, so they are registered for acknowledgement here —
// at the moment they are actually handed to the application.
{
let buffered: Vec<ConsumerRecord> = {
let mut buffer = self.0.recv_buffer.write().await;
let take = max_records.min(buffer.len());
buffer.drain(..take).collect()
};
if !buffered.is_empty() {
self.register_delivered_records(&buffered).await;
return Ok(buffered);
}
}
let assignments = self.0.assignments.read().await.clone();
if assignments.is_empty() || skip_fetch_due_to_buffer_cap {
return Ok(Vec::new());
}
// Group partitions by leader broker.
let mut partitions_by_broker: HashMap<BrokerId, Vec<(String, PartitionId, [u8; 16])>> =
HashMap::new();
let topic_ids = self.0.topic_ids.read().await;
for (topic, partitions) in &assignments {
let Some(&topic_id) = topic_ids.get(topic) else {
debug!("No topic UUID for '{topic}', skipping");
continue;
};
for &partition in partitions {
if let Some(leader) = self.0.metadata.leader(topic, partition) {
partitions_by_broker.entry(leader).or_default().push((
topic.clone(),
partition,
topic_id,
));
}
}
}
drop(topic_ids);
let ack_state_generation = self.0.ack_state_generation.load(Ordering::SeqCst);
let sendable_ack_partitions: HashSet<(&str, PartitionId)> = partitions_by_broker
.values()
.flat_map(|partitions| {
partitions
.iter()
.map(|(topic, partition, _)| (topic.as_str(), *partition))
})
.collect();
// Hold an in-flight slot for as long as the acknowledgements are out of
// `pending_acks`. `commit_sync()` and `close()` wait on this before
// draining, so they cannot flush an empty map while this poll is
// holding the acks — see `ShareConsumerInner::in_flight_polls`.
let _poll_guard = self.0.in_flight_polls.start("share consumer")?;
// Drain acknowledgement batches to piggyback on fetch requests.
//
// The drained acks are immediately handed to a `PendingAckGuard` so
// that dropping this future (a `select!` shutdown arm, or dropping the
// record stream) re-queues them instead of silently discarding explicit
// `Reject`/`Release` decisions.
let drained_acks: Vec<PendingAck> = {
let mut pending = self.0.pending_acks.write().await;
std::mem::take(&mut *pending)
.into_values()
.flat_map(|partition_acks| partition_acks.into_values().flatten())
.collect()
};
let mut ack_guard = PendingAckGuard::new(
drained_acks.clone(),
self.0.pending_acks.clone(),
self.0.ack_state_generation.clone(),
ack_state_generation,
self.0.explicit_flush_retry_required.clone(),
false,
);
// Route every ack to the *current* partition leader. Leadership can
// change between `acknowledge()` and `poll()`, so the pre-routing done
// at acknowledge time is treated as a hint only.
let mut failed_piggyback_acks: Vec<PendingAck> = Vec::new();
let mut ack_batches_by_broker: BrokerPendingAcks = HashMap::new();
for ack in drained_acks {
if !sendable_ack_partitions.contains(&(ack.topic.as_str(), ack.partition)) {
failed_piggyback_acks.push(ack);
continue;
}
match self.0.metadata.leader(&ack.topic, ack.partition) {
Some(broker_id) => {
let key = (ack.topic_id, ack.partition);
ack_batches_by_broker
.entry(broker_id)
.or_default()
.entry(key)
.or_default()
.push(ack);
}
None => failed_piggyback_acks.push(ack),
}
}
// Fetch from all brokers concurrently.
let mut fetch_tasks = Vec::with_capacity(partitions_by_broker.len());
let member_id = (**self.0.member_id.load()).clone();
let group_id = self.0.config.group_id.clone();
let current_ack_state_generation = self.0.ack_state_generation.clone();
for (broker_id, partitions) in &partitions_by_broker {
let session_epoch = {
let mut sessions = self.0.share_sessions.lock().await;
sessions.get_or_create(*broker_id).epoch()
};
// Build per-topic partition requests with piggybacked acks.
let mut topics_map: HashMap<[u8; 16], Vec<ShareFetchPartition>> = HashMap::new();
let broker_ack_partitions = ack_batches_by_broker.get(broker_id);
for (_, partition, topic_id) in partitions {
let ack_batches_for_partition: Vec<ShareAcknowledgementBatch> =
broker_ack_partitions
.and_then(|partition_acks| partition_acks.get(&(*topic_id, *partition)))
.map(|partition_acks| {
partition_acks
.iter()
.map(|a| ShareAcknowledgementBatch {
first_offset: a.first_offset,
last_offset: a.last_offset,
acknowledge_types: vec![a.ack_type],
})
.collect()
})
.unwrap_or_default();
topics_map
.entry(*topic_id)
.or_default()
.push(ShareFetchPartition {
partition_index: *partition,
acknowledgement_batches: ack_batches_for_partition,
});
}
let topics: Vec<ShareFetchTopic> = topics_map
.into_iter()
.map(|(topic_id, partitions)| ShareFetchTopic {
topic_id,
partitions,
})
.collect();
// Wait at most the caller's poll timeout, and never longer than the
// configured `fetch_max_wait`, so a long poll timeout does not
// silently override the fetch-side setting.
let poll_wait_ms = crate::util::duration_to_millis_i32(timeout);
let max_wait_ms = poll_wait_ms.min(crate::util::duration_to_millis_i32(
self.0.config.fetch_max_wait,
));
let mut request = ShareFetchRequest {
group_id: Some(group_id.clone()),
member_id: Some(member_id.clone()),
share_session_epoch: session_epoch,
max_wait_ms,
min_bytes: self.0.config.fetch_min_bytes,
max_bytes: self.0.config.fetch_max_bytes,
max_records: self.0.config.max_records,
batch_size: self.0.config.batch_size,
topics,
forgotten_topics: Vec::new(),
};
let bid = *broker_id;
let metadata = self.0.metadata.clone();
let pool = self.0.pool.clone();
let current_ack_state_generation = current_ack_state_generation.clone();
let task = tokio::spawn(async move {
ShareConsumer::ensure_ack_state_current(
current_ack_state_generation.as_ref(),
ack_state_generation,
)?;
let broker_addr = metadata
.broker(bid)
.map(|b| b.address().to_string())
.ok_or_else(|| {
KrafkaError::invalid_state(format!("broker {bid} not found in metadata"))
})?;
let conn = pool.get_connection_by_id(bid, &broker_addr).await?;
let version = conn
.negotiate_api_version(
ApiKey::ShareFetch,
versions::SHARE_FETCH_MAX,
versions::SHARE_FETCH_MIN,
)
.ok_or_else(|| {
KrafkaError::protocol_kind(
ProtocolErrorKind::UnknownApiVersion,
"broker does not support ShareFetch",
)
})?;
ShareConsumer::ensure_ack_state_current(
current_ack_state_generation.as_ref(),
ack_state_generation,
)?;
// KIP-1222 `Renew` is only understood by newer brokers; sending
// it to an older one fails the *entire* acknowledgement batch
// with INVALID_REQUEST. Drop those acks instead: the acquisition
// lock then simply expires, the same outcome as not renewing.
if version < RENEW_MIN_VERSION {
let dropped =
strip_unsupported_renew_acks(request.topics.iter_mut().flat_map(|topic| {
topic
.partitions
.iter_mut()
.map(|partition| &mut partition.acknowledgement_batches)
}));
if dropped > 0 {
warn!(
broker_id = bid,
version,
dropped,
"broker does not support KIP-1222 Renew acknowledgements; \
dropping them from the ShareFetch"
);
}
}
let buf = conn
.send_request(ApiKey::ShareFetch, version, |buf| match version {
2 => request.encode_v2(buf, 0, false),
_ => request.encode_v1(buf),
})
.await?;
let response = crate::protocol::ShareFetchResponse::decode_versioned(
version,
&mut buf.as_ref(),
)?;
// KIP-219: honour broker-reported throttle time.
conn.notify_throttle(response.throttle_time_ms);
Result::<(BrokerId, crate::protocol::ShareFetchResponse)>::Ok((bid, response))
});
fetch_tasks.push((bid, task));
}
// Collect results from all brokers.
//
// Snapshot the UUID → name mapping instead of holding the `topic_ids`
// read guard across the network awaits below. tokio's `RwLock` is
// write-preferring, so a long-lived reader lets one slow `ShareFetch`
// block `apply_assignment()`'s writer, which in turn blocks the
// heartbeat path and can get the member evicted from the group.
let mut all_records: Vec<ConsumerRecord> = Vec::new();
let mut gap_acks: Vec<PendingAck> = Vec::new();
let topic_names_by_id: HashMap<[u8; 16], String> = {
let guard = self.0.topic_ids.read().await;
guard.iter().map(|(name, &id)| (id, name.clone())).collect()
};
// Race the collection loop against `wakeup()` so an in-flight
// `ShareFetch` can be interrupted instead of having to run to
// completion.
let mut wakeup_interrupted = false;
{
let collect_fetch_results = async {
for (broker_id, task) in fetch_tasks {
match task.await {
Ok(Ok((_, response))) => {
let mut broker_acks =
ack_batches_by_broker.remove(&broker_id).unwrap_or_default();
if !response.error_code.is_ok() {
failed_piggyback_acks.extend(flatten_partition_acks(broker_acks));
warn!(
"ShareFetch to broker {broker_id} returned {:?}: {}",
response.error_code,
response.error_message.as_deref().unwrap_or("unknown error")
);
let mut sessions = self.0.share_sessions.lock().await;
sessions.reset_broker(broker_id);
continue;
}
// Update session state on success.
{
let mut sessions = self.0.share_sessions.lock().await;
sessions.get_or_create(broker_id).on_success();
}
// KIP-1222: the broker reports how long an
// acquisition lock lasts. Record it — without it an
// application has no way to learn the deadline
// `AcknowledgeType::Renew` exists to extend, since
// `group.share.record.lock.duration.ms` is a
// broker-side setting a client cannot read.
if response.acquisition_lock_timeout_ms > 0 {
self.0
.acquisition_lock_timeout_ms
.store(response.acquisition_lock_timeout_ms, Ordering::Relaxed);
}
// Decode records from the response and restore only the
// partitions whose piggybacked acknowledgements failed.
for topic_response in &response.responses {
let topic_name = if let Some(name) =
self.0.metadata.topic_name_for_id(&topic_response.topic_id)
{
name
} else {
match topic_names_by_id.get(&topic_response.topic_id) {
Some(name) => name.clone(),
None => {
debug!(
"Unknown topic UUID {:?} in ShareFetch response, skipping",
topic_response.topic_id
);
continue;
}
}
};
for partition_response in &topic_response.partitions {
let partition_acks = drain_broker_partition_acks(
&mut broker_acks,
topic_response.topic_id,
partition_response.partition_index,
);
if !partition_response.error_code.is_ok() {
failed_piggyback_acks.extend(partition_acks);
warn!(
"ShareFetch error for {topic_name}-{}: {:?}",
partition_response.partition_index,
partition_response.error_code
);
continue;
}
if !partition_response.acknowledge_error_code.is_ok() {
failed_piggyback_acks.extend(partition_acks);
warn!(
"Piggybacked ShareFetch acknowledge error for {topic_name}-{}: {:?}: {}",
partition_response.partition_index,
partition_response.acknowledge_error_code,
partition_response
.acknowledge_error_message
.as_deref()
.unwrap_or("unknown error")
);
continue;
}
// Build the delivery-count map from acquired_records.
// The encoded record bytes bound how many records
// can possibly be decoded, which caps a malformed
// range such as `0..=i64::MAX`.
let raw_len = partition_response
.records
.as_ref()
.map(|raw| raw.len())
.unwrap_or(0);
let delivery_counts = build_delivery_counts(
&partition_response.acquired_records,
raw_len,
);
// Decode record batches.
let mut decoded_offsets: HashSet<Offset> = HashSet::new();
let mut decode_failed = false;
if let Some(ref raw) = partition_response.records {
let mut cursor = raw.as_ref();
while !cursor.is_empty() {
match RecordBatch::decode_with_limit(
&mut cursor,
self.0.config.max_decompressed_size,
) {
Ok(batch) => {
for record in batch.records {
let record_offset = batch.base_offset
+ record.offset_delta as i64;
let delivery_count = delivery_counts
.get(&record_offset)
.copied();
decoded_offsets.insert(record_offset);
all_records.push(ConsumerRecord {
topic: topic_name.clone(),
partition: partition_response
.partition_index,
offset: record_offset,
timestamp: batch
.base_timestamp
.saturating_add(
record.timestamp_delta,
),
timestamp_type: batch
.attributes
.timestamp_type
as i8,
key: record.key,
value: record.value,
headers: record
.headers
.into_iter()
.map(|h| (h.key, h.value))
.collect(),
leader_epoch: None,
delivery_count,
});
}
}
Err(e) => {
debug!(
"Failed to decode record batch for {topic_name}-{}: {e}",
partition_response.partition_index
);
decode_failed = true;
break;
}
}
}
}
// Offsets the broker acquired for us but that we
// could not decode would otherwise be redelivered
// forever, with `delivery_count` climbing without
// bound. Acknowledge them as gaps so the broker
// archives them instead.
if decode_failed {
gap_acks.extend(build_gap_acks(
&topic_name,
topic_response.topic_id,
partition_response.partition_index,
&delivery_counts,
&decoded_offsets,
));
}
}
}
failed_piggyback_acks.extend(flatten_partition_acks(broker_acks));
}
Ok(Err(e)) => {
failed_piggyback_acks
.extend(drain_broker_acks(&mut ack_batches_by_broker, broker_id));
warn!("ShareFetch to broker {broker_id} failed: {e}");
}
Err(e) => {
failed_piggyback_acks
.extend(drain_broker_acks(&mut ack_batches_by_broker, broker_id));
warn!(
"ShareFetch task for broker {broker_id} {}: {e}",
describe_share_fetch_join_error(&e)
);
}
}
}
};
tokio::pin!(collect_fetch_results);
tokio::select! {
biased;
() = self.0.wakeup_notify.notified() => {
wakeup_interrupted = true;
}
() = &mut collect_fetch_results => {}
}
}
if wakeup_interrupted {
// Consume the flag so the *next* poll() is not failed spuriously by
// the same wakeup, keep anything already decoded, and re-queue the
// drained acknowledgements.
self.0.wakeup_flag.store(false, Ordering::Release);
if !all_records.is_empty() {
let mut buffer = self.0.recv_buffer.write().await;
buffer.extend(std::mem::take(&mut all_records));
}
let acks = ack_guard.disarm();
self.restore_pending_acks(ack_state_generation, acks, false)
.await;
return Err(KrafkaError::invalid_state("wakeup() was called"));
}
failed_piggyback_acks.extend(
ack_batches_by_broker
.drain()
.flat_map(|(_, acks)| flatten_partition_acks(acks))
.collect::<Vec<_>>(),
);
// Everything that could be sent has been sent; the guard's copy is no
// longer needed and only the genuinely failed acks are re-queued.
let _ = ack_guard.disarm();
drop(ack_guard);
failed_piggyback_acks.append(&mut gap_acks);
self.restore_pending_acks(ack_state_generation, failed_piggyback_acks, false)
.await;
// Split *before* any acknowledgement bookkeeping. Acknowledging a
// record that is then discarded would consume it permanently without
// ever delivering it (implicit mode), or wedge `poll()` forever behind
// an offset the application can never acknowledge (explicit mode).
let overflow = if all_records.len() > max_records {
all_records.split_off(max_records)
} else {
Vec::new()
};
if !overflow.is_empty() {
let mut buffer = self.0.recv_buffer.write().await;
buffer.extend(overflow);
}
// Only the records actually returned to the caller are tracked.
self.register_delivered_records(&all_records).await;
// Deserialization runs *after* registration, which is the opposite of
// the subscription consumer and is deliberate.
//
// A share consumer cannot `seek()` past a record it cannot decode —
// there are no positions to seek. Its escape hatch is
// `acknowledge_by_offset(topic, partition, offset, Reject)`, and that
// call requires the offset to be registered as pending. Deserializing
// first would leave the poison record unregistered and therefore
// unrejectable: the acquisition lock would expire, the broker would
// redeliver it, and the same poll would fail again forever with no way
// out.
//
// The cost is that in *implicit* mode the batch has already been
// queued for `Accept` by the time the failure is seen. That is what
// implicit mode means — `poll()` acknowledges what it delivers — and
// an application that needs to arbitrate poison records should use
// explicit mode, where nothing is acknowledged without its say-so.
self.deserialize_batch(&mut all_records).await?;
Ok(all_records)
}
/// Apply the configured key/value deserializers to a delivered batch.
///
/// Fails on the first record either decoder rejects, naming it precisely so
/// the caller can reject that offset and move on.
async fn deserialize_batch(&self, records: &mut [ConsumerRecord]) -> Result<()> {
if self.0.key_deserializer.is_none() && self.0.value_deserializer.is_none() {
return Ok(());
}
for record in records.iter_mut() {
if let (Some(decoder), Some(value)) =
(&self.0.value_deserializer, record.value.as_ref())
{
let decoded = decoder
.deserialize(value.clone(), &record.topic, false)
.await
.map_err(|e| {
KrafkaError::record_deserialization(
&record.topic,
record.partition,
record.offset,
"value",
e.to_string(),
)
})?;
record.value = Some(decoded);
}
if let (Some(decoder), Some(key)) = (&self.0.key_deserializer, record.key.as_ref()) {
let decoded = decoder
.deserialize(key.clone(), &record.topic, true)
.await
.map_err(|e| {
KrafkaError::record_deserialization(
&record.topic,
record.partition,
record.offset,
"key",
e.to_string(),
)
})?;
record.key = Some(decoded);
}
}
Ok(())
}
/// Register records that are about to be handed to the application.
///
/// In implicit mode this queues coalesced `Accept` acknowledgements to be
/// piggybacked on the next `ShareFetch`. In explicit mode it records the
/// offsets in `unacked_offsets`, which `poll()` requires to be empty before
/// it will fetch again.
///
/// This is deliberately called at *delivery* time rather than at fetch
/// time, so a record can never be acknowledged or required-to-be-acked
/// without the application having seen it.
async fn register_delivered_records(&self, records: &[ConsumerRecord]) {
if records.is_empty() {
return;
}
// The single choke point for "handed to the application", so it is the
// honest place to count. Instrumenting each `poll_inner` return instead
// would miss the buffered-surplus path, which is a real delivery.
let bytes: u64 = records
.iter()
.map(|r| r.value.as_ref().map_or(0, |v| v.len() as u64))
.sum();
self.0.metrics.record_receive(records.len() as u64, bytes);
match self.0.config.acknowledgement_mode {
AcknowledgementMode::Implicit => {
let ids = self.0.topic_ids.read().await.clone();
let mut pending = self.0.pending_acks.write().await;
Self::coalesce_implicit_acks(records, &ids, &mut pending, &self.0.metadata);
}
AcknowledgementMode::Explicit => {
let mut unacked = self.0.unacked_offsets.write().await;
for record in records {
unacked.insert((record.topic.clone(), record.partition, record.offset));
}
}
}
}
/// Acknowledge a record with the given type (explicit mode only).
///
/// In explicit acknowledgement mode, call this for each record before
/// calling [`commit_sync()`](Self::commit_sync). All records from the
/// previous `poll()` must be acknowledged before calling `poll()` again.
pub async fn acknowledge(
&self,
record: &ConsumerRecord,
ack_type: AcknowledgeType,
) -> Result<()> {
if self.0.config.acknowledgement_mode != AcknowledgementMode::Explicit {
return Err(KrafkaError::invalid_state(
"acknowledge() requires explicit acknowledgement mode",
));
}
let topic_ids = self.0.topic_ids.read().await;
let topic_id = topic_ids.get(&record.topic).copied().ok_or_else(|| {
KrafkaError::invalid_state(format!("no topic UUID for '{}'", record.topic))
})?;
drop(topic_ids);
let record_key = (record.topic.clone(), record.partition, record.offset);
let mut pending = self.0.pending_acks.write().await;
let mut unacked = self.0.unacked_offsets.write().await;
if !unacked.contains(&record_key) {
return Err(KrafkaError::invalid_state(format!(
"record {}-{}@{} is not pending acknowledgement",
record.topic, record.partition, record.offset
)));
}
// Route the ack to the current partition leader so poll() can piggyback
// it on the correct broker's ShareFetch without re-scanning the whole map.
// Fall back to UNROUTED_BROKER_ID when the leader is not yet in metadata;
// poll() will re-route it using fresh metadata.
let broker_id = self
.0
.metadata
.leader(&record.topic, record.partition)
.unwrap_or(UNROUTED_BROKER_ID);
pending
.entry(broker_id)
.or_default()
.entry((topic_id, record.partition))
.or_default()
.push(PendingAck {
topic: record.topic.clone(),
topic_id,
partition: record.partition,
first_offset: record.offset,
last_offset: record.offset,
ack_type: ack_type.to_i8(),
});
unacked.remove(&record_key);
Ok(())
}
async fn restore_pending_acks(
&self,
ack_state_generation: u64,
mut acks: Vec<PendingAck>,
require_explicit_retry: bool,
) {
Self::restore_ack_state(
self.0.ack_state_generation.as_ref(),
self.0.pending_acks.as_ref(),
self.0.explicit_flush_retry_required.as_ref(),
ack_state_generation,
require_explicit_retry,
&mut acks,
)
.await;
}
async fn restore_ack_state(
current_generation: &AtomicU64,
pending_acks: &RwLock<BrokerPendingAcks>,
explicit_flush_retry_required: &AtomicBool,
ack_state_generation: u64,
require_explicit_retry: bool,
acks: &mut Vec<PendingAck>,
) {
if acks.is_empty() {
return;
}
let mut pending = pending_acks.write().await;
if current_generation.load(Ordering::SeqCst) != ack_state_generation {
acks.clear();
return;
}
if require_explicit_retry {
explicit_flush_retry_required.store(true, Ordering::SeqCst);
}
// Re-queue under UNROUTED_BROKER_ID; poll() will re-route using fresh
// metadata on the next call (handles leadership changes after failure).
for ack in acks.drain(..) {
pending
.entry(UNROUTED_BROKER_ID)
.or_default()
.entry((ack.topic_id, ack.partition))
.or_default()
.push(ack);
}
}
fn share_acknowledge_response_error(
response: &crate::protocol::ShareAcknowledgeResponse,
) -> Option<KrafkaError> {
if !response.error_code.is_ok() {
return Some(KrafkaError::broker(
response.error_code,
response
.error_message
.clone()
.unwrap_or_else(|| "ShareAcknowledge failed".to_string()),
));
}
for topic_response in &response.responses {
for part_response in &topic_response.partitions {
if !part_response.error_code.is_ok() {
return Some(KrafkaError::broker(
part_response.error_code,
part_response.error_message.clone().unwrap_or_else(|| {
format!(
"ShareAcknowledge error for partition {}",
part_response.partition_index
)
}),
));
}
}
}
None
}
/// Commit all pending acknowledgements synchronously.
///
/// Sends a `ShareAcknowledge` request for all outstanding acknowledgements.
/// In implicit mode, this flushes any buffered accepts.
pub async fn commit_sync(&self) -> Result<()> {
if self.0.closed.load(Ordering::SeqCst) {
return Err(KrafkaError::invalid_state("share consumer is closed"));
}
self.flush_pending_acks().await
}
async fn flush_pending_acks(&self) -> Result<()> {
// Wait for any poll that is currently holding the acknowledgements out
// of `pending_acks`. Draining first would take an empty map, report
// success, and strand whatever the poll's guard restores a moment
// later. The documented shutdown — `wakeup()` then `close()` — hits
// exactly this interleaving, because `wakeup()` does not wait for the
// poll it interrupts to unwind.
self.0
.in_flight_polls
.wait_for(self.0.in_flight_polls.snapshot())
.await;
let ack_state_generation = self.0.ack_state_generation.load(Ordering::SeqCst);
// If there are acks in the UNROUTED shard (no leader known at
// acknowledge() time), refresh metadata first so that
// `send_share_acknowledge_with_state` can resolve their leaders.
// This avoids an immediate "no leader for topic-partition" error on
// the very first `commit_sync()` call after a fetch that piggybacked
// the acks but hadn't yet routed them.
//
// IMPORTANT: collect the topic names while holding the read lock, then
// drop the lock *before* the async metadata refresh. Holding an async
// RwLock guard across an await point stalls every writer that needs
// `pending_acks.write()` (including the `std::mem::take` below).
let unrouted_topics: HashSet<String> = {
let pending = self.0.pending_acks.read().await;
pending
.get(&UNROUTED_BROKER_ID)
.into_iter()
.flat_map(|m| m.values().flatten())
.map(|ack| ack.topic.clone())
.collect()
}; // read lock released here
if !unrouted_topics.is_empty() {
let topic_refs: Vec<&str> = unrouted_topics.iter().map(String::as_str).collect();
if let Err(err) = self.0.metadata.refresh_for_topics(Some(&topic_refs)).await {
warn!(
error = %err,
"commit_sync: metadata refresh for unrouted acks failed; \
commit may fail with a leader-not-found error"
);
}
}
let acks: Vec<PendingAck> = {
let mut pending = self.0.pending_acks.write().await;
std::mem::take(&mut *pending)
.into_values()
.flat_map(|broker_acks| broker_acks.into_values().flatten())
.collect()
};
if acks.is_empty() {
return Ok(());
}
// Arm a restore guard: if this future is dropped mid-flush (a `select!`
// shutdown arm, a `commit_sync_with_timeout` that elapses) the drained
// acknowledgements are re-queued instead of silently lost.
let mut guard = PendingAckGuard::new(
acks,
self.0.pending_acks.clone(),
self.0.ack_state_generation.clone(),
ack_state_generation,
self.0.explicit_flush_retry_required.clone(),
true,
);
let outcome = self.send_share_acknowledge(guard.acks()).await;
let _ = guard.disarm();
match outcome.error {
None => {
self.0
.explicit_flush_retry_required
.store(false, Ordering::SeqCst);
// A successful acknowledgement flush is the share-group
// analogue of an offset commit.
self.0.metrics.record_commit();
Ok(())
}
Some(error) => {
self.restore_pending_acks(ack_state_generation, outcome.failed, true)
.await;
Err(error)
}
}
}
/// Commit all pending acknowledgements asynchronously.
///
/// Await the returned handle to observe transport, decode, and broker
/// errors. If the handle is dropped, the task continues in the background
/// and its result is discarded.
pub fn commit_async(&self) -> ShareCommitHandle {
if self.0.closed.load(Ordering::SeqCst) {
return ShareCommitHandle::ready(Err(KrafkaError::invalid_state(
"share consumer is closed",
)));
}
let member_id = (**self.0.member_id.load()).clone();
let ack_state_generation = self.0.ack_state_generation.load(Ordering::SeqCst);
let pending_acks = self.0.pending_acks.clone();
let current_ack_state_generation = self.0.ack_state_generation.clone();
let explicit_flush_retry_required = self.0.explicit_flush_retry_required.clone();
let Ok(mut pending) = self.0.pending_acks.try_write() else {
return ShareCommitHandle::ready(Err(KrafkaError::invalid_state(
"commit_async: pending_acks lock contention",
)));
};
let acks: Vec<PendingAck> = std::mem::take(&mut *pending)
.into_values()
.flat_map(|broker_acks| broker_acks.into_values().flatten())
.collect();
drop(pending);
if acks.is_empty() {
return ShareCommitHandle::ready(Ok(()));
}
let metadata = self.0.metadata.clone();
let pool = self.0.pool.clone();
let share_sessions = self.0.share_sessions.clone();
let group_id = self.0.config.group_id.clone();
let send_ack_state_generation = current_ack_state_generation.clone();
ShareCommitHandle::Task(tokio::spawn(async move {
let restore_acks = |mut acks: Vec<PendingAck>| {
let pending_acks = pending_acks.clone();
let current_ack_state_generation = current_ack_state_generation.clone();
let explicit_flush_retry_required = explicit_flush_retry_required.clone();
async move {
ShareConsumer::restore_ack_state(
current_ack_state_generation.as_ref(),
pending_acks.as_ref(),
explicit_flush_retry_required.as_ref(),
ack_state_generation,
true,
&mut acks,
)
.await;
}
};
let outcome = ShareConsumer::send_share_acknowledge_with_state(
ShareAcknowledgeContext {
metadata,
pool,
share_sessions,
group_id,
member_id,
current_ack_state_generation: send_ack_state_generation,
ack_state_generation,
},
&acks,
)
.await;
if let Some(error) = outcome.error {
// Only the acks their broker did not accept are re-queued.
restore_acks(outcome.failed).await;
return Err(error);
}
explicit_flush_retry_required.store(false, Ordering::SeqCst);
Ok(())
}))
}
/// Receive a single record, waiting until one is available.
///
/// Records fetched but not yet returned are buffered internally, so
/// repeated calls do not issue a `ShareFetch` per record.
///
/// `Ok(None)` means the consumer has been **closed** — and nothing else.
/// An idle topic simply makes this call wait: an empty `ShareFetch` is
/// retried until a record arrives, the consumer is closed, or
/// [`wakeup()`](Self::wakeup) interrupts it (which surfaces as `Err`).
pub async fn recv(&self) -> Result<Option<ConsumerRecord>> {
loop {
if self.0.closed.load(Ordering::SeqCst) {
return Ok(None);
}
let started = tokio::time::Instant::now();
let records = self.poll_inner(Duration::from_secs(1), 1).await?;
if let Some(record) = records.into_iter().next() {
return Ok(Some(record));
}
if self.0.closed.load(Ordering::SeqCst) {
return Ok(None);
}
// A poll can return empty immediately (no assignment yet, buffer
// cap reached, heartbeat-only cycle). Pace the retry so an idle or
// unassigned consumer cannot spin the CPU.
let elapsed = started.elapsed();
if elapsed < RECV_EMPTY_POLL_BACKOFF {
tokio::time::sleep(RECV_EMPTY_POLL_BACKOFF - elapsed).await;
}
}
}
/// Create an async stream of records.
pub fn stream(&self) -> ShareConsumerStream<'_> {
ShareConsumerStream::new(self)
}
/// Unsubscribe from all topics.
///
/// Flushes pending acknowledgements, sends a leave heartbeat
/// (member_epoch = -1) and clears local state.
///
/// The flush is best-effort: a failure is logged and unsubscribe still
/// proceeds. Without it, explicit `Reject`/`Release` decisions the
/// application already made would be discarded by the state clear below and
/// the affected records would be redelivered as if nothing was decided.
pub async fn unsubscribe(&self) {
// Stop the background heartbeat task before leaving the group.
if let Some(handle) = self
.0
.heartbeat_task
.lock()
.unwrap_or_else(|e| e.into_inner())
.take()
{
handle.abort();
}
// Flush before clearing state (close() does the same).
if let Err(e) = self.flush_pending_acks().await {
warn!("Flushing pending acknowledgements during unsubscribe failed: {e}");
}
// Leave group via heartbeat with epoch -1.
if let Err(e) = self.leave_group().await {
warn!("Leave group failed during unsubscribe: {e}");
}
self.0.subscriptions.write().await.clear();
self.0.assignments.write().await.clear();
self.clear_partition_state().await;
self.0
.member_id
.store(Arc::new(crate::util::random_uuid_v4()));
self.0.member_epoch.store(0, Ordering::Release);
debug!("Unsubscribed from share group '{}'", self.0.config.group_id);
}
/// Close the share consumer.
///
/// In implicit mode, unreleased records are released (not accepted) so
/// they become available for other consumers. In explicit mode, pending
/// acks are flushed. Leaves the group and closes all connections.
/// Idempotent.
///
/// Returns the first cleanup error after local state and connections have
/// still been closed.
pub async fn close(&self) -> Result<()> {
if self.0.closed.swap(true, Ordering::SeqCst) {
return Ok(());
}
// Stop the background heartbeat task immediately so it does not
// race with the leave-group heartbeat sent below.
if let Some(handle) = self
.0
.heartbeat_task
.lock()
.unwrap_or_else(|e| e.into_inner())
.take()
{
handle.abort();
}
// In implicit mode, convert pending accepts to releases so acquired
// records are returned to the pool for redelivery (KIP-932 §close).
if self.0.config.acknowledgement_mode == AcknowledgementMode::Implicit {
let mut pending = self.0.pending_acks.write().await;
for broker_acks in pending.values_mut() {
for acks in broker_acks.values_mut() {
for ack in acks.iter_mut() {
ack.ack_type = AcknowledgeType::Release.to_i8();
}
}
}
}
let commit_result = self.flush_pending_acks().await;
// Send FINAL_EPOCH to all established share sessions so brokers release
// server-side session state immediately rather than waiting for timeout.
self.close_share_sessions().await;
// Leave the group.
let leave_result = self.leave_group().await;
// Clear state.
self.0.subscriptions.write().await.clear();
self.0.assignments.write().await.clear();
self.clear_partition_state().await;
// A pool borrowed from a `KrafkaClient` belongs to that client; closing
// it here would tear down every sibling client's connections.
if self.0.pool_owned {
self.0.pool.close_all().await;
}
info!(
"ShareConsumer closed (group '{}', pool {})",
self.0.config.group_id,
if self.0.pool_owned {
"torn down"
} else {
"shared, left open"
}
);
commit_result?;
leave_result
}
/// Whether this client owns its connection pool.
///
/// `false` when the pool was borrowed from a
/// [`KrafkaClient`](crate::client::KrafkaClient) via
/// [`with_client`](ShareConsumerBuilder::with_client). In that case
/// [`close`](Self::close) leaves the connections untouched — closing them
/// would tear down every sibling client on that `KrafkaClient` and fail
/// their in-flight requests.
#[inline]
#[must_use]
pub fn owns_pool(&self) -> bool {
self.0.pool_owned
}
/// Returns true if the consumer has been closed.
#[inline]
pub fn is_closed(&self) -> bool {
self.0.closed.load(Ordering::SeqCst)
}
/// Interrupt [`poll()`](Self::poll)/[`recv()`](Self::recv) from another
/// thread or task.
///
/// A `poll()` that is already blocked on a `ShareFetch` is interrupted and
/// returns an error without waiting for the broker; a `poll()` that has not
/// started yet returns the same error immediately. Acknowledgements drained
/// by the interrupted call are re-queued, not lost.
///
/// The consumer remains usable — the next `poll()` proceeds normally.
/// This is safe to call concurrently with any other consumer method.
#[inline]
pub fn wakeup(&self) {
self.0.wakeup_flag.store(true, Ordering::Release);
self.0.wakeup_notify.notify_waiters();
}
/// Close the consumer with a per-phase timeout.
///
/// Equivalent to [`close()`](Self::close) but each cleanup phase
/// (ack flush, leave-group) is individually limited to `timeout / 2`.
/// Any cleanup error is returned after local state has been released.
/// Idempotent.
pub async fn close_with_timeout(&self, timeout: Duration) -> Result<()> {
if self.0.closed.swap(true, Ordering::SeqCst) {
return Ok(());
}
if let Some(handle) = self
.0
.heartbeat_task
.lock()
.unwrap_or_else(|e| e.into_inner())
.take()
{
handle.abort();
}
if self.0.config.acknowledgement_mode == AcknowledgementMode::Implicit {
let mut pending = self.0.pending_acks.write().await;
for broker_acks in pending.values_mut() {
for acks in broker_acks.values_mut() {
for ack in acks.iter_mut() {
ack.ack_type = AcknowledgeType::Release.to_i8();
}
}
}
}
let phase = timeout / 2;
let commit_result = tokio::time::timeout(phase, self.flush_pending_acks())
.await
.unwrap_or_else(|_| Err(KrafkaError::timeout("ack flush timed out during close")));
self.close_share_sessions().await;
let leave_result = tokio::time::timeout(phase, self.leave_group())
.await
.unwrap_or_else(|_| Err(KrafkaError::timeout("leave-group timed out during close")));
self.0.subscriptions.write().await.clear();
self.0.assignments.write().await.clear();
self.clear_partition_state().await;
if self.0.pool_owned {
self.0.pool.close_all().await;
}
info!(
"ShareConsumer closed with timeout (group '{}', pool {})",
self.0.config.group_id,
if self.0.pool_owned {
"torn down"
} else {
"shared, left open"
}
);
commit_result?;
leave_result
}
/// Flush all pending explicit-mode acknowledgements synchronously with a timeout.
///
/// Equivalent to [`commit_sync()`](Self::commit_sync) but bounded by `timeout`.
/// Returns `Err(KrafkaError::Timeout)` if the flush does not complete in time.
pub async fn commit_sync_with_timeout(&self, timeout: Duration) -> Result<()> {
tokio::time::timeout(timeout, self.commit_sync())
.await
.unwrap_or_else(|_| Err(KrafkaError::timeout("commit_sync timed out")))
}
/// Acknowledge a record by topic, partition, and offset directly.
///
/// Use this when the record could not be deserialized but still needs to be
/// acknowledged to prevent indefinite redelivery. The record must have been
/// delivered in the current poll session.
///
/// Returns `Err` if the record was not found in the unacknowledged set.
pub async fn acknowledge_by_offset(
&self,
topic: &str,
partition: PartitionId,
offset: Offset,
ack_type: AcknowledgeType,
) -> Result<()> {
if self.0.config.acknowledgement_mode != AcknowledgementMode::Explicit {
return Err(KrafkaError::invalid_state(
"acknowledge_by_offset() requires explicit acknowledgement mode",
));
}
let record_key: RecordKey = (topic.to_string(), partition, offset);
let unacked = self.0.unacked_offsets.read().await;
if !unacked.contains(&record_key) {
return Err(KrafkaError::invalid_state(format!(
"record {topic}-{partition}@{offset} is not pending acknowledgement"
)));
}
drop(unacked);
let topic_ids = self.0.topic_ids.read().await;
let topic_id = topic_ids
.get(topic)
.copied()
.ok_or_else(|| KrafkaError::invalid_state(format!("no topic UUID for '{topic}'")))?;
drop(topic_ids);
let broker_id = self
.0
.metadata
.leader(topic, partition)
.unwrap_or(UNROUTED_BROKER_ID);
let mut pending = self.0.pending_acks.write().await;
let mut unacked = self.0.unacked_offsets.write().await;
pending
.entry(broker_id)
.or_default()
.entry((topic_id, partition))
.or_default()
.push(PendingAck {
topic: topic.to_string(),
topic_id,
partition,
first_offset: offset,
last_offset: offset,
ack_type: ack_type.to_i8(),
});
unacked.remove(&record_key);
Ok(())
}
/// Re-read TLS certificate and key files from disk and atomically install
/// the new material for all **future** connections (KIP-1288).
///
/// Existing TLS sessions are unaffected: they keep the connector they
/// handshaked with and are replaced naturally as connections cycle. On
/// error the previously loaded certificates stay active, so a call made
/// mid-rotation against a half-written PEM is safe to retry.
///
/// No-op when TLS is not configured.
///
/// Use this for event-driven rotation (an inotify watch, a sidecar
/// signal). For unattended rotation set
/// [`TransportConfig::tls_reload_interval`](crate::network::TransportConfig)
/// instead and krafka reloads on a timer.
///
/// # Errors
///
/// Returns an error if the certificate or key files cannot be read or
/// parsed.
pub async fn refresh_tls(&self) -> Result<()> {
self.0.pool.refresh_tls().await
}
/// Replace the bootstrap server list used for metadata recovery (KIP-899).
///
/// The new addresses are used on the next metadata refresh that falls back
/// to bootstrap servers. Does not close existing connections.
///
/// # Errors
///
/// Returns an error if `servers` is empty.
pub fn update_seed_brokers(&self, servers: Vec<String>) -> Result<()> {
self.0.metadata.update_seed_brokers(servers)
}
/// Force a rebootstrap: close all connections, clear the metadata cache,
/// and fall back to bootstrap servers (KIP-899).
pub async fn rebootstrap(&self) {
self.0.metadata.rebootstrap().await;
}
/// How long an acquisition lock lasts, as most recently reported by a
/// broker (KIP-1222).
///
/// `None` before the first `ShareFetch` completes, and on brokers older
/// than Kafka 4.2, which do not report it.
///
/// # Why this exists
///
/// [`AcknowledgeType::Renew`] extends a record's acquisition lock so that
/// long-running processing does not have the record redelivered to another
/// member. Renewing requires knowing *when* — and the deadline is derived
/// from `group.share.record.lock.duration.ms`, a **broker-side** setting a
/// client cannot read from its own configuration. Without this accessor an
/// application was told to renew before a deadline it had no way to learn,
/// so the only workable strategy was to renew blindly on a timer tuned by
/// guesswork.
///
/// The lock starts when the broker *acquires* the record, which is when it
/// builds the fetch response — so treat the value as an upper bound on the
/// time remaining once `poll()` returns, and renew with margin.
///
/// # Example
///
/// ```rust,ignore
/// let lock = consumer
/// .acquisition_lock_timeout()
/// .unwrap_or(Duration::from_secs(30));
/// // Renew once the record is halfway to losing its lock.
/// let renew_after = lock / 2;
///
/// for record in consumer.poll(timeout).await? {
/// let started = Instant::now();
/// while !work_is_done(&record) {
/// step(&record).await;
/// if started.elapsed() >= renew_after {
/// consumer.acknowledge(&record, AcknowledgeType::Renew).await?;
/// }
/// }
/// consumer.acknowledge(&record, AcknowledgeType::Accept).await?;
/// }
/// ```
#[inline]
#[must_use]
pub fn acquisition_lock_timeout(&self) -> Option<Duration> {
let ms = self.0.acquisition_lock_timeout_ms.load(Ordering::Relaxed);
(ms > 0).then(|| Duration::from_millis(ms as u64))
}
/// Snapshot the share consumer's application metrics.
///
/// Counts polls, empty polls, records and bytes received, acknowledgement
/// flushes (`commits`) and errors. Without this a share group was
/// operable but not observable: the transport counters from
/// [`connection_metrics`](Self::connection_metrics) showed requests, and
/// nothing showed records.
///
/// Synchronous, like every other metrics accessor in the crate.
#[inline]
pub fn metrics(&self) -> Arc<crate::metrics::ConsumerMetrics> {
self.0.metrics.clone()
}
/// Get the shared connection metrics handle used by this share consumer's broker pool.
#[inline]
pub fn connection_metrics(&self) -> Arc<ConnectionMetrics> {
self.0.pool.metrics()
}
// ── Internal helpers ──────────────────────────────────────────────────
fn invalidate_ack_state(&self) {
self.0.ack_state_generation.fetch_add(1, Ordering::SeqCst);
self.0
.explicit_flush_retry_required
.store(false, Ordering::SeqCst);
}
async fn clear_ack_state(&self) {
self.invalidate_ack_state();
self.0.pending_acks.write().await.clear();
self.0.unacked_offsets.write().await.clear();
}
/// Clear all per-partition state. Called from `unsubscribe()` and `close()`.
async fn clear_partition_state(&self) {
self.clear_ack_state().await;
self.0.recv_buffer.write().await.clear();
self.0.share_sessions.lock().await.reset_all();
*self.0.coordinator_id.write().await = None;
*self.0.coordinator_address.write().await = None;
}
/// Invalidate the cached coordinator. The next `ensure_coordinator()` call
/// will re-discover it. Called on NOT_COORDINATOR errors or heartbeat failures.
async fn invalidate_coordinator(&self) {
*self.0.coordinator_id.write().await = None;
*self.0.coordinator_address.write().await = None;
}
/// Coalesce implicit accept acks — merge consecutive offsets for the same
/// (topic, partition) into a single `PendingAck` with a contiguous range,
/// pre-routed to the current partition leader.
fn coalesce_implicit_acks(
records: &[ConsumerRecord],
topic_ids: &HashMap<String, [u8; 16]>,
pending: &mut BrokerPendingAcks,
metadata: &ClusterMetadata,
) {
// Group by (topic, partition) and sort offsets.
let mut by_tp: HashMap<(&str, PartitionId), Vec<Offset>> = HashMap::new();
for record in records {
by_tp
.entry((&record.topic, record.partition))
.or_default()
.push(record.offset);
}
for ((topic, partition), mut offsets) in by_tp {
let Some(&topic_id) = topic_ids.get(topic) else {
continue;
};
offsets.sort_unstable();
// A duplicate offset would otherwise produce two overlapping
// single-offset ranges, which the broker rejects for the whole
// partition.
offsets.dedup();
let broker_id = metadata
.leader(topic, partition)
.unwrap_or(UNROUTED_BROKER_ID);
// Merge consecutive offsets into contiguous ranges.
let mut i = 0;
while i < offsets.len() {
let first = offsets[i];
let mut last = first;
while i + 1 < offsets.len() && offsets[i + 1] == last + 1 {
i += 1;
last = offsets[i];
}
pending
.entry(broker_id)
.or_default()
.entry((topic_id, partition))
.or_default()
.push(PendingAck {
topic: topic.to_string(),
topic_id,
partition,
first_offset: first,
last_offset: last,
ack_type: AcknowledgeType::Accept.to_i8(),
});
i += 1;
}
}
}
/// Build `ShareAcknowledgeTopic` list from pending acks. Groups by
/// topic UUID and partition, sorting and coalescing ack batches per
/// partition.
///
/// Explicit acks arrive here in *application call order*, and
/// acknowledging out of order is perfectly legal at the API surface. The
/// batches are sorted by `first_offset` and merged when contiguous and of
/// one type, for three reasons:
///
/// - it is the wire form the Java client always produces (its
/// `Acknowledgements` map is a `TreeMap`), so brokers only ever see
/// ordered batches from the reference client and unordered input is
/// untested territory;
/// - duplicate or overlapping ranges reference record state a preceding
/// batch already transitioned, which fails the partition with
/// `INVALID_RECORD_STATE` — sorting makes the merge that prevents this
/// possible;
/// - N sequential `acknowledge()` calls collapse to one wire batch
/// instead of N.
fn build_acknowledge_topics(acks: &[PendingAck]) -> Vec<ShareAcknowledgeTopic> {
let mut topics_map: HashMap<
[u8; 16],
HashMap<PartitionId, Vec<ShareAcknowledgementBatch>>,
> = HashMap::new();
for ack in acks {
topics_map
.entry(ack.topic_id)
.or_default()
.entry(ack.partition)
.or_default()
.push(ShareAcknowledgementBatch {
first_offset: ack.first_offset,
last_offset: ack.last_offset,
acknowledge_types: vec![ack.ack_type],
});
}
topics_map
.into_iter()
.map(|(topic_id, partitions_map)| ShareAcknowledgeTopic {
topic_id,
partitions: partitions_map
.into_iter()
.map(|(partition_index, mut batches)| {
batches.sort_unstable_by_key(|b| b.first_offset);
let mut merged: Vec<ShareAcknowledgementBatch> =
Vec::with_capacity(batches.len());
for batch in batches {
match merged.last_mut() {
// Contiguous and uniformly of the same type —
// extend rather than append. Only single-type
// batches merge: a multi-type batch enumerates
// one type per offset and must keep its exact
// span.
Some(prev)
if prev.last_offset + 1 == batch.first_offset
&& prev.acknowledge_types.len() == 1
&& batch.acknowledge_types.len() == 1
&& prev.acknowledge_types[0]
== batch.acknowledge_types[0] =>
{
prev.last_offset = batch.last_offset;
}
_ => merged.push(batch),
}
}
ShareAcknowledgePartition {
partition_index,
acknowledgement_batches: merged,
}
})
.collect(),
})
.collect()
}
/// Discover the share group coordinator via FindCoordinator.
async fn ensure_coordinator(&self) -> Result<()> {
if self.0.coordinator_id.read().await.is_some() {
return Ok(());
}
let brokers = self.0.metadata.brokers();
if brokers.is_empty() {
return Err(KrafkaError::invalid_state("no brokers available"));
}
// Try each broker until we find the coordinator.
let request = FindCoordinatorRequest::for_group(&self.0.config.group_id);
for broker in &brokers {
let conn = match self
.0
.pool
.get_connection_by_id(broker.id(), broker.address())
.await
{
Ok(c) => c,
Err(_) => continue,
};
let version = match conn.negotiate_api_version(
ApiKey::FindCoordinator,
versions::FIND_COORDINATOR_MAX,
versions::FIND_COORDINATOR_MIN,
) {
Some(v) => v,
None => continue,
};
let result = conn
.send_request(ApiKey::FindCoordinator, version, |buf| {
request.encode_versioned(version, buf)
})
.await;
let buf = match result {
Ok(b) => b,
Err(e) => {
debug!("FindCoordinator via broker {} failed: {e}", broker.id());
continue;
}
};
let response = FindCoordinatorResponse::decode_versioned(version, &mut buf.as_ref())?;
if response.error_code.is_ok() {
let coord_id = response.node_id;
let coord_addr = format!("{}:{}", response.host, response.port);
*self.0.coordinator_id.write().await = Some(coord_id);
*self.0.coordinator_address.write().await = Some(coord_addr);
debug!(
"Share group '{}' coordinator is broker {coord_id}",
self.0.config.group_id
);
return Ok(());
}
debug!(
"FindCoordinator returned {:?} for group '{}', trying next broker",
response.error_code, self.0.config.group_id
);
}
Err(KrafkaError::invalid_state(format!(
"could not discover coordinator for share group '{}'",
self.0.config.group_id
)))
}
/// Send a ShareGroupHeartbeat to the coordinator.
///
/// If `send_subscription` is true, the subscribed topic names are included.
/// Returns the heartbeat response.
async fn send_heartbeat(&self, send_subscription: bool) -> Result<()> {
let coord_id = self
.0
.coordinator_id
.read()
.await
.ok_or_else(|| KrafkaError::invalid_state("no coordinator discovered"))?;
let member_id = (**self.0.member_id.load()).clone();
let member_epoch = self.0.member_epoch.load(Ordering::Acquire);
let subscribed_topic_names = if send_subscription {
Some(
self.0
.subscriptions
.read()
.await
.iter()
.cloned()
.collect::<Vec<_>>(),
)
} else {
None
};
let request = ShareGroupHeartbeatRequest {
group_id: self.0.config.group_id.clone(),
member_id: member_id.clone(),
member_epoch,
rack_id: self.0.config.client_rack.clone(),
subscribed_topic_names,
};
let coord_addr = self
.0
.coordinator_address
.read()
.await
.clone()
.ok_or_else(|| KrafkaError::invalid_state("no coordinator address"))?;
let conn = self
.0
.pool
.get_connection_by_id(coord_id, &coord_addr)
.await?;
let version = conn
.negotiate_api_version(
ApiKey::ShareGroupHeartbeat,
versions::SHARE_GROUP_HEARTBEAT_MAX,
versions::SHARE_GROUP_HEARTBEAT_MIN,
)
.ok_or_else(|| {
KrafkaError::protocol_kind(
ProtocolErrorKind::UnknownApiVersion,
"broker does not support ShareGroupHeartbeat",
)
})?;
let buf = conn
.send_request(ApiKey::ShareGroupHeartbeat, version, |buf| {
request.encode_versioned(version, buf)
})
.await?;
let response = ShareGroupHeartbeatResponse::decode_versioned(version, &mut buf.as_ref())?;
match response.error_code {
ErrorCode::None => {}
// The current node is no longer the coordinator. Invalidate the
// cached coordinator so `ensure_coordinator()` will rediscover it.
ErrorCode::NotCoordinator
| ErrorCode::CoordinatorNotAvailable
| ErrorCode::CoordinatorLoadInProgress => {
self.invalidate_coordinator().await;
return Err(KrafkaError::broker(
response.error_code,
response
.error_message
.unwrap_or_else(|| "ShareGroupHeartbeat failed".to_string()),
));
}
// The coordinator has advanced past our epoch. The caller is
// responsible for resetting local member state.
ErrorCode::FencedMemberEpoch => {
return Err(KrafkaError::broker(
response.error_code,
response
.error_message
.unwrap_or_else(|| "member epoch fenced".to_string()),
));
}
other => {
return Err(KrafkaError::broker(
other,
response
.error_message
.unwrap_or_else(|| "ShareGroupHeartbeat failed".to_string()),
));
}
}
// Update member state from response.
if let Some(new_member_id) = response.member_id {
self.0.member_id.store(Arc::new(new_member_id));
}
self.0
.member_epoch
.store(response.member_epoch, Ordering::Release);
// Clamp the broker-supplied heartbeat interval to [50 ms, 30 s] to
// prevent excessively fast polling (which exhausts broker connections)
// or excessively slow polling (which causes session timeouts).
let raw_interval_ms = response.heartbeat_interval_ms;
const HEARTBEAT_MIN_MS: i32 = 50;
const HEARTBEAT_MAX_MS: i32 = 30_000;
let clamped_interval_ms = raw_interval_ms.clamp(HEARTBEAT_MIN_MS, HEARTBEAT_MAX_MS);
if clamped_interval_ms != raw_interval_ms {
tracing::warn!(
raw_ms = raw_interval_ms,
clamped_ms = clamped_interval_ms,
min_ms = HEARTBEAT_MIN_MS,
max_ms = HEARTBEAT_MAX_MS,
"broker heartbeat_interval_ms is out of safe range; clamping"
);
}
self.0
.heartbeat_interval_ms
.store(clamped_interval_ms, Ordering::Release);
// Process assignment if present.
if let Some(assignment) = response.assignment {
self.apply_assignment(&assignment).await;
}
Ok(())
}
/// Apply a partition assignment from the coordinator heartbeat response.
async fn apply_assignment(&self, assignment: &[ShareGroupTopicPartitions]) {
let mut new_assignments: HashMap<String, Vec<PartitionId>> = HashMap::new();
let mut topic_ids_guard = self.0.topic_ids.write().await;
for tp in assignment {
// Resolve topic UUID to name.
let topic_name = if let Some(name) = self.0.metadata.topic_name_for_id(&tp.topic_id) {
topic_ids_guard.insert(name.clone(), tp.topic_id);
name
} else {
// Cache miss — try a metadata refresh next time.
debug!(
"Unknown topic UUID {:?} in share assignment, skipping",
tp.topic_id
);
continue;
};
new_assignments.insert(topic_name, tp.partitions.clone());
}
drop(topic_ids_guard);
// Reset share sessions for brokers whose partitions changed.
let old_assignments = self.0.assignments.read().await.clone();
if old_assignments != new_assignments {
debug!(
"Share group assignment changed: {} topic(s), {} partition(s)",
new_assignments.len(),
new_assignments.values().map(|v| v.len()).sum::<usize>()
);
self.clear_ack_state().await;
self.0.share_sessions.lock().await.reset_all();
}
*self.0.assignments.write().await = new_assignments;
}
/// Send a ShareAcknowledge request for pending acks.
///
/// Routes acknowledgements to the correct partition leaders and reports,
/// per acknowledgement, which ones the brokers did not accept.
async fn send_share_acknowledge(&self, acks: &[PendingAck]) -> ShareAcknowledgeOutcome {
let member_id = (**self.0.member_id.load()).clone();
Self::send_share_acknowledge_with_state(
ShareAcknowledgeContext {
metadata: self.0.metadata.clone(),
pool: self.0.pool.clone(),
share_sessions: self.0.share_sessions.clone(),
group_id: self.0.config.group_id.clone(),
member_id,
current_ack_state_generation: self.0.ack_state_generation.clone(),
ack_state_generation: self.0.ack_state_generation.load(Ordering::SeqCst),
},
acks,
)
.await
}
async fn send_share_acknowledge_with_state(
context: ShareAcknowledgeContext,
acks: &[PendingAck],
) -> ShareAcknowledgeOutcome {
let ShareAcknowledgeContext {
metadata,
pool,
share_sessions,
group_id,
member_id,
current_ack_state_generation,
ack_state_generation,
} = context;
let mut outcome = ShareAcknowledgeOutcome::default();
if let Err(error) = Self::ensure_ack_state_current(
current_ack_state_generation.as_ref(),
ack_state_generation,
) {
outcome.fail(acks.iter().cloned(), error);
return outcome;
}
// Group acks by partition leader.
let mut broker_acks: HashMap<BrokerId, Vec<PendingAck>> = HashMap::new();
for ack in acks {
match metadata.leader(&ack.topic, ack.partition) {
Some(broker_id) => broker_acks.entry(broker_id).or_default().push(ack.clone()),
None => outcome.fail(
std::iter::once(ack.clone()),
KrafkaError::invalid_state(format!(
"no leader for {}-{} in metadata",
ack.topic, ack.partition
)),
),
}
}
for (broker_id, broker_ack_list) in broker_acks {
// Track success per broker. Restoring a multi-broker batch wholesale
// because the last broker failed would make the retry re-acknowledge
// offsets the earlier brokers already accepted, which the broker
// rejects with INVALID_RECORD_STATE.
if let Err(error) = Self::ensure_ack_state_current(
current_ack_state_generation.as_ref(),
ack_state_generation,
) {
outcome.fail(broker_ack_list, error);
continue;
}
let result = Self::send_broker_acknowledge(
&metadata,
&pool,
&share_sessions,
&group_id,
&member_id,
broker_id,
&broker_ack_list,
)
.await;
if let Err(error) = result {
outcome.fail(broker_ack_list, error);
}
}
outcome
}
/// Send a `ShareAcknowledge` to a single broker, retrying share-session
/// failures.
///
/// On success the broker's share-session epoch is advanced. Skipping that
/// step leaves the next `ShareFetch` sending an epoch the broker has already
/// consumed, which it answers with `INVALID_SHARE_SESSION_EPOCH` — and since
/// nothing reset the session, every retry repeats the same stale epoch and
/// the consumer never recovers.
///
/// `SHARE_SESSION_NOT_FOUND` (122), `INVALID_SHARE_SESSION_EPOCH` (123) and
/// `SHARE_SESSION_LIMIT_REACHED` (133) all mean the client's view of the
/// session is unusable: the session is reset so the retry opens a fresh one
/// at epoch 0. Code 133 is a capacity signal, so it is retried after a
/// short backoff.
async fn send_broker_acknowledge(
metadata: &Arc<ClusterMetadata>,
pool: &Arc<ConnectionPool>,
share_sessions: &Arc<tokio::sync::Mutex<ShareSessionCache>>,
group_id: &str,
member_id: &str,
broker_id: BrokerId,
acks: &[PendingAck],
) -> Result<()> {
let broker_addr = metadata
.broker(broker_id)
.map(|b| b.address().to_string())
.ok_or_else(|| {
KrafkaError::invalid_state(format!("broker {broker_id} not found in metadata"))
})?;
let mut last_error: Option<KrafkaError> = None;
for attempt in 0..=SHARE_SESSION_RETRY_LIMIT {
let conn = pool.get_connection_by_id(broker_id, &broker_addr).await?;
let version = conn
.negotiate_api_version(
ApiKey::ShareAcknowledge,
versions::SHARE_ACKNOWLEDGE_MAX,
versions::SHARE_ACKNOWLEDGE_MIN,
)
.ok_or_else(|| {
KrafkaError::protocol_kind(
ProtocolErrorKind::UnknownApiVersion,
"broker does not support ShareAcknowledge",
)
})?;
let mut topics = Self::build_acknowledge_topics(acks);
if version < RENEW_MIN_VERSION {
let dropped = strip_unsupported_renew_acks(topics.iter_mut().flat_map(|topic| {
topic
.partitions
.iter_mut()
.map(|partition| &mut partition.acknowledgement_batches)
}));
if dropped > 0 {
warn!(
broker_id,
version,
dropped,
"broker does not support KIP-1222 Renew acknowledgements; dropping them"
);
}
}
let session_epoch = {
let sessions = share_sessions.lock().await;
sessions
.get(broker_id)
.map(|s: &session::ShareSessionState| s.epoch())
.unwrap_or(session::INITIAL_EPOCH)
};
let request = ShareAcknowledgeRequest {
group_id: Some(group_id.to_string()),
member_id: Some(member_id.to_string()),
share_session_epoch: session_epoch,
topics,
};
let buf = conn
.send_request(ApiKey::ShareAcknowledge, version, |buf| match version {
2 => request.encode_v2(buf, false),
_ => request.encode_v1(buf),
})
.await?;
let response = crate::protocol::ShareAcknowledgeResponse::decode_versioned(
version,
&mut buf.as_ref(),
)?;
match Self::share_acknowledge_response_error(&response) {
None => {
// Advance the share-session epoch: this request consumed it.
let mut sessions = share_sessions.lock().await;
sessions.get_or_create(broker_id).on_success();
return Ok(());
}
Some(error) => {
let session_error = matches!(
&error,
KrafkaError::Broker { code, .. } if is_share_session_error(*code)
);
if !session_error || attempt == SHARE_SESSION_RETRY_LIMIT {
return Err(error);
}
let limit_reached = matches!(
&error,
KrafkaError::Broker {
code: ErrorCode::ShareSessionLimitReached,
..
}
);
warn!(
broker_id,
attempt,
"ShareAcknowledge share-session error ({error}); resetting the session and retrying"
);
share_sessions.lock().await.reset_broker(broker_id);
last_error = Some(error);
if limit_reached {
tokio::time::sleep(SHARE_SESSION_LIMIT_BACKOFF).await;
}
}
}
}
Err(last_error.unwrap_or_else(|| {
KrafkaError::invalid_state("ShareAcknowledge exhausted share-session retries")
}))
}
fn ensure_ack_state_current(
current_generation: &AtomicU64,
ack_state_generation: u64,
) -> Result<()> {
if current_generation.load(Ordering::SeqCst) == ack_state_generation {
return Ok(());
}
Err(KrafkaError::invalid_state(
"share acknowledgement state was invalidated",
))
}
/// Leave the share group via heartbeat with member_epoch = -1.
async fn leave_group(&self) -> Result<()> {
let coord_id = match *self.0.coordinator_id.read().await {
Some(id) => id,
None => return Ok(()),
};
let member_id = (**self.0.member_id.load()).clone();
if member_id.is_empty() {
return Ok(());
}
let request = ShareGroupHeartbeatRequest {
group_id: self.0.config.group_id.clone(),
member_id,
member_epoch: -1, // Leave signal
rack_id: None,
subscribed_topic_names: None,
};
let coord_addr = match self.0.coordinator_address.read().await.clone() {
Some(addr) => addr,
None => return Ok(()),
};
let conn = self
.0
.pool
.get_connection_by_id(coord_id, &coord_addr)
.await?;
let version = match conn.negotiate_api_version(
ApiKey::ShareGroupHeartbeat,
versions::SHARE_GROUP_HEARTBEAT_MAX,
versions::SHARE_GROUP_HEARTBEAT_MIN,
) {
Some(v) => v,
None => {
return Err(KrafkaError::protocol_kind(
ProtocolErrorKind::UnknownApiVersion,
"broker does not support ShareGroupHeartbeat",
));
}
};
let buf = conn
.send_request(ApiKey::ShareGroupHeartbeat, version, |buf| {
request.encode_versioned(version, buf)
})
.await;
let response = ShareGroupHeartbeatResponse::decode_versioned(version, &mut buf?.as_ref())?;
if !response.error_code.is_ok() {
return Err(KrafkaError::broker(
response.error_code,
response
.error_message
.unwrap_or_else(|| "ShareGroupHeartbeat failed".to_string()),
));
}
debug!("Left share group '{}' successfully", self.0.config.group_id);
self.invalidate_coordinator().await;
Ok(())
}
/// Send `ShareFetch` with `share_session_epoch = FINAL_EPOCH` (-1) to each
/// broker that has an established session, allowing the broker to release
/// server-side session state immediately instead of waiting for timeout.
///
/// This is a best-effort operation: errors are logged at `debug!` level
/// and do not prevent the consumer from closing.
async fn close_share_sessions(&self) {
let broker_ids = {
let sessions = self.0.share_sessions.lock().await;
sessions.established_broker_ids()
};
if broker_ids.is_empty() {
return;
}
let member_id = (**self.0.member_id.load()).clone();
let group_id = &self.0.config.group_id;
for broker_id in broker_ids {
let broker_addr = match self.0.metadata.broker(broker_id) {
Some(b) => b.address().to_string(),
None => continue,
};
let conn = match self
.0
.pool
.get_connection_by_id(broker_id, &broker_addr)
.await
{
Ok(c) => c,
Err(e) => {
debug!("close_share_sessions: connection to broker {broker_id} failed: {e}");
continue;
}
};
let version = match conn.negotiate_api_version(
ApiKey::ShareFetch,
versions::SHARE_FETCH_MAX,
versions::SHARE_FETCH_MIN,
) {
Some(v) => v,
None => continue,
};
let request = ShareFetchRequest {
group_id: Some(group_id.clone()),
member_id: Some(member_id.clone()),
share_session_epoch: FINAL_EPOCH,
max_wait_ms: 0,
min_bytes: 0,
max_bytes: 0,
max_records: 0,
batch_size: 0,
topics: Vec::new(),
forgotten_topics: Vec::new(),
};
if let Err(e) = conn
.send_request(ApiKey::ShareFetch, version, |buf| match version {
2 => request.encode_v2(buf, 0, false),
_ => request.encode_v1(buf),
})
.await
{
debug!("close_share_sessions: FINAL_EPOCH to broker {broker_id} failed: {e}");
} else {
debug!("close_share_sessions: sent FINAL_EPOCH to broker {broker_id}");
}
}
}
/// Background heartbeat loop. ///
/// Sends periodic heartbeats at the coordinator-specified interval so the
/// share-group session stays alive independent of how often `poll()` is
/// called. Handles coordinator errors with automatic rediscovery and
/// recovers from `FencedMemberEpoch` by resetting local member state.
///
/// Stops when `closed` is set or the task is aborted.
///
/// Takes a [`Weak`] reference on purpose: the loop holds a strong reference
/// only while a heartbeat is actually in flight, so it never keeps the
/// consumer alive. It stops when `closed` is set, when the task is aborted,
/// or as soon as the application has dropped every [`ShareConsumer`] handle.
async fn run_heartbeat_loop(inner: Weak<ShareConsumerInner>) {
let group_id = match inner.upgrade() {
Some(strong) => strong.config.group_id.clone(),
None => return,
};
loop {
let interval_ms = match inner.upgrade() {
Some(strong) => strong.heartbeat_interval_ms.load(Ordering::Relaxed),
None => break,
};
tokio::time::sleep(Duration::from_millis(interval_ms.max(1) as u64)).await;
// Re-acquire a strong reference for this iteration only; it is
// dropped before the next sleep so the consumer can be reclaimed
// while the loop is idle.
let Some(strong) = inner.upgrade() else {
break;
};
let this = ShareConsumer(strong);
if this.0.closed.load(Ordering::Relaxed) {
break;
}
match this.send_heartbeat(false).await {
Ok(()) => {}
// Fenced epoch: the coordinator has advanced past our epoch.
// Reset local member state so the next heartbeat starts a new
// membership attempt.
Err(KrafkaError::Broker {
code: ErrorCode::FencedMemberEpoch,
..
}) => {
warn!(
"Background heartbeat: member epoch fenced for group '{group_id}'; resetting state"
);
this.0.member_epoch.store(0, Ordering::Release);
this.clear_ack_state().await;
this.invalidate_coordinator().await;
if let Err(e) = this.ensure_coordinator().await {
warn!(
"Background heartbeat: coordinator rediscovery after fence failed: {e}"
);
}
}
// Coordinator moved or unavailable: rediscover and retry.
Err(ref e) if e.is_retriable() => {
debug!("Background heartbeat: retryable error for group '{group_id}': {e}");
this.invalidate_coordinator().await;
if let Err(e2) = this.ensure_coordinator().await {
warn!("Background heartbeat: coordinator rediscovery failed: {e2}");
}
}
Err(e) => {
warn!("Background heartbeat error for group '{group_id}': {e}");
this.invalidate_coordinator().await;
}
}
// Release the strong reference before sleeping again.
drop(this);
}
debug!("Background heartbeat task stopped for group '{group_id}'");
}
}
/// Builder for creating share consumers.
#[derive(Default)]
#[must_use = "builders do nothing until .build() is called"]
pub struct ShareConsumerBuilder {
config: ShareConsumerConfig,
/// Pre-built pool and metadata from a [`KrafkaClient`](crate::client::KrafkaClient).
shared: Option<(Arc<ConnectionPool>, Arc<ClusterMetadata>)>,
/// Optional decoder applied to every consumed record's key.
key_deserializer: Option<Arc<dyn crate::serdes::Deserializer>>,
/// Optional decoder applied to every consumed record's value.
value_deserializer: Option<Arc<dyn crate::serdes::Deserializer>>,
}
impl ShareConsumerBuilder {
/// Share a [`KrafkaClient`](crate::client::KrafkaClient)'s connection pool
/// and metadata cache instead of creating a new one.
///
/// When multiple clients run in the same process, build one
/// [`KrafkaClient`](crate::client::KrafkaClient) and pass it to each
/// builder: they then multiplex over the same TCP connections, reducing the
/// total connection count from `N × brokers` to `brokers`.
///
/// With this set, `bootstrap_servers` is optional — the client was already
/// connected at `KrafkaClient::build` time — and
/// [`close`](ShareConsumer::close) leaves the shared pool open for its
/// owner.
pub fn with_client(mut self, client: &crate::client::KrafkaClient) -> Self {
self.shared = Some((client.pool().clone(), client.metadata().clone()));
self
}
/// Set the bootstrap servers.
pub fn bootstrap_servers(mut self, servers: impl Into<String>) -> Self {
self.config.bootstrap_servers = servers.into();
self
}
/// Set the share group ID (required).
pub fn group_id(mut self, group_id: impl Into<String>) -> Self {
self.config.group_id = group_id.into();
self
}
/// Set the client ID.
pub fn client_id(mut self, id: impl Into<String>) -> Self {
self.config.client_id = id.into();
self
}
/// Set socket- and pool-level transport tuning.
///
/// Covers TCP keepalive and nodelay, the per-connection response ceiling
/// and in-flight cap, the priority-channel depths, the Happy Eyeballs
/// stagger, idle-connection eviction, a total-connection cap, and the
/// KIP-1288 automatic TLS reload interval.
///
/// Omitting this call keeps krafka's historical defaults, which
/// [`TransportConfig::default`](crate::network::TransportConfig) reproduces
/// exactly.
pub fn transport(mut self, transport: crate::network::TransportConfig) -> Self {
self.config.transport = transport;
self
}
/// Set the acknowledgement mode.
pub fn acknowledgement_mode(mut self, mode: AcknowledgementMode) -> Self {
self.config.acknowledgement_mode = mode;
self
}
/// Set the maximum number of records returned per `poll()` call.
///
/// Must be >= 1. Rejected at build time otherwise. Defaults to 500.
pub fn max_poll_records(mut self, max: i32) -> Self {
self.config.max_poll_records = max;
self
}
/// Set maximum records buffered internally by [`recv()`](ShareConsumer::recv).
///
/// This is a soft threshold: once the buffer is at/above this value,
/// `poll()` skips fetches until it drains. A single `recv()` call may
/// buffer beyond the threshold due to batched fetch responses. Set to `0`
/// for unlimited. Negative values are rejected at build time.
/// Defaults to 500.
pub fn max_buffered_records(mut self, max: i32) -> Self {
self.config.max_buffered_records = max;
self
}
/// Set how long a broker may hold a `ShareFetch` waiting for
/// [`fetch_min_bytes`](Self::fetch_min_bytes) to accumulate.
///
/// Capped by the `poll()` timeout, so a short poll is never made to wait
/// for a long fetch. Defaults to 500 ms — the same trade as the regular
/// consumer's [`fetch_max_wait`](crate::consumer::ConsumerBuilder::fetch_max_wait).
pub fn fetch_max_wait(mut self, wait: Duration) -> Self {
self.config.fetch_max_wait = wait;
self
}
/// Set the minimum bytes a broker must have before answering a
/// `ShareFetch`.
///
/// Raising it trades latency for fewer, fuller responses; the broker still
/// answers after [`fetch_max_wait`](Self::fetch_max_wait) regardless.
/// Defaults to 1 (answer as soon as anything is available).
pub fn fetch_min_bytes(mut self, bytes: i32) -> Self {
self.config.fetch_min_bytes = bytes;
self
}
/// Set the maximum bytes one `ShareFetch` response may carry.
///
/// Defaults to 50 MiB, matching the regular consumer's `fetch_max_bytes`.
pub fn fetch_max_bytes(mut self, bytes: i32) -> Self {
self.config.fetch_max_bytes = bytes;
self
}
/// Set the maximum number of records the broker may **acquire** for this
/// member in one `ShareFetch` (KIP-932 `MaxRecords`).
///
/// This is the share-group analogue of a fetch size: acquired records hold
/// an acquisition lock until they are acknowledged or the lock expires, so
/// it bounds how much of the share group's backlog this member can hold at
/// once. Distinct from [`max_poll_records`](Self::max_poll_records), which
/// bounds what a single `poll()` hands the application. Defaults to 5000.
pub fn max_records(mut self, max: i32) -> Self {
self.config.max_records = max;
self
}
/// Set the batch size the broker should aim for when acquiring records
/// (KIP-932 `BatchSize`).
///
/// A hint, not a limit: the broker uses it to decide how to carve the
/// acquired range into acknowledgement batches. Defaults to 500.
pub fn batch_size(mut self, size: i32) -> Self {
self.config.batch_size = size;
self
}
/// Set how long metadata refreshes may keep failing before the client
/// re-bootstraps from the seed list (KIP-899).
///
/// Only consulted when the strategy is
/// [`MetadataRecoveryStrategy::Rebootstrap`](crate::metadata::MetadataRecoveryStrategy::Rebootstrap).
/// Defaults to 5 minutes.
pub fn metadata_recovery_rebootstrap_trigger(mut self, duration: Duration) -> Self {
self.config.metadata_recovery_rebootstrap_trigger = duration;
self
}
/// Set a decoder applied to every consumed record's **key**.
///
/// The share consumer hands back the same [`ConsumerRecord`] as the regular
/// consumer, so it takes the same
/// [`Deserializer`](crate::serdes::Deserializer) hook — schema-registry
/// framing, envelope decryption, application-level decompression. Without
/// it, a share-group application had to decode by hand what a subscription
/// consumer got transparently.
pub fn key_deserializer(mut self, deserializer: Arc<dyn crate::serdes::Deserializer>) -> Self {
self.key_deserializer = Some(deserializer);
self
}
/// Set a decoder applied to every consumed record's **value**.
///
/// See [`key_deserializer`](Self::key_deserializer).
pub fn value_deserializer(
mut self,
deserializer: Arc<dyn crate::serdes::Deserializer>,
) -> Self {
self.value_deserializer = Some(deserializer);
self
}
/// Set the request timeout.
pub fn request_timeout(mut self, timeout: Duration) -> Self {
self.config.request_timeout = timeout;
self
}
/// Configure SASL/OAUTHBEARER with a static token.
///
/// For a token that must be refreshed, use
/// [`auth`](Self::auth) with
/// [`AuthConfig::sasl_oauthbearer_provider`](crate::auth::AuthConfig::sasl_oauthbearer_provider),
/// or the built-in OIDC provider behind the `oauth-oidc` feature.
pub fn sasl_oauthbearer(mut self, token: impl Into<String>) -> Self {
self.config.auth = Some(crate::auth::AuthConfig::sasl_oauthbearer(token));
self
}
/// Set the metadata recovery strategy (KIP-899).
///
/// Controls what the client does when every known broker becomes
/// unreachable: keep retrying the cached broker set, or fall back to the
/// original bootstrap servers.
pub fn metadata_recovery_strategy(
mut self,
strategy: crate::metadata::MetadataRecoveryStrategy,
) -> Self {
self.config.metadata_recovery_strategy = strategy;
self
}
/// Configure SASL/PLAIN authentication.
///
/// # Errors
///
/// Returns an error if the credentials contain bytes the SASL framing
/// cannot carry.
pub fn sasl_plain(
mut self,
username: impl Into<String>,
password: impl Into<String>,
) -> Result<Self> {
self.config.auth = Some(crate::auth::AuthConfig::sasl_plain(username, password)?);
Ok(self)
}
/// Configure SASL/SCRAM-SHA-256 authentication.
pub fn sasl_scram_sha256(
mut self,
username: impl Into<String>,
password: impl Into<String>,
) -> Self {
self.config.auth = Some(crate::auth::AuthConfig::sasl_scram_sha256(
username, password,
));
self
}
/// Configure SASL/SCRAM-SHA-512 authentication.
pub fn sasl_scram_sha512(
mut self,
username: impl Into<String>,
password: impl Into<String>,
) -> Self {
self.config.auth = Some(crate::auth::AuthConfig::sasl_scram_sha512(
username, password,
));
self
}
/// Set the connect timeout: how long TCP establishment to one broker may
/// take. Default: 10 s.
///
/// [`request_timeout`](Self::request_timeout) must be at least this value,
/// so lowering this is what makes a short request timeout possible.
pub fn connect_timeout(mut self, timeout: Duration) -> Self {
self.config.connect_timeout = timeout;
self
}
/// Set the session timeout for share group membership.
pub fn session_timeout(mut self, timeout: Duration) -> Self {
self.config.session_timeout = timeout;
self
}
/// Set the heartbeat interval.
pub fn heartbeat_interval(mut self, interval: Duration) -> Self {
self.config.heartbeat_interval = interval;
self
}
/// Set authentication configuration.
pub fn auth(mut self, auth: AuthConfig) -> Self {
self.config.auth = Some(auth);
self
}
/// Set the client rack ID.
pub fn client_rack(mut self, rack: impl Into<String>) -> Self {
self.config.client_rack = Some(rack.into());
self
}
/// Set metadata max age.
pub fn metadata_max_age(mut self, duration: Duration) -> Self {
self.config.metadata_max_age = duration;
self
}
/// Set the topic cache TTL for partial metadata refreshes.
///
/// During partial refreshes, cached topics that have not been refreshed
/// within this duration are evicted to prevent unbounded cache growth.
///
/// Default: 5 minutes (matching Java's `metadata.max.idle.ms`).
pub fn metadata_topic_cache_ttl(mut self, ttl: Duration) -> Self {
self.config.metadata_topic_cache_ttl = Some(ttl);
self
}
/// Let the broker create a topic this client asks about but the cluster
/// does not have, i.e. `allow.auto.create.topics`.
///
/// The broker must additionally be configured with
/// `auto.create.topics.enable=true`; this flag only says the client is
/// willing.
///
/// Default: `false`, unlike the Java producer, which always asks for
/// auto-creation. A typo'd topic name that silently materialises a real
/// topic reports nothing until the traffic is found missing from the topic
/// it was meant for. Turn it on for development and test clusters.
///
/// Ignored when the client shares a
/// [`KrafkaClient`](crate::client::KrafkaClient)'s metadata: that client's
/// own setting governs.
pub fn allow_auto_create_topics(mut self, allow: bool) -> Self {
self.config.allow_auto_create_topics = allow;
self
}
/// Disable topic cache TTL eviction for partial metadata refreshes.
///
/// By default, cached topics are evicted after 5 minutes to prevent
/// unbounded growth on topic churn. Call this to opt out of TTL eviction;
/// entries will then persist across partial refreshes indefinitely.
pub fn disable_metadata_topic_cache_ttl(mut self) -> Self {
self.config.metadata_topic_cache_ttl = None;
self
}
/// Set SOCKS5 proxy configuration.
#[cfg(feature = "socks5")]
pub fn proxy(mut self, proxy: crate::network::ProxyConfig) -> Self {
self.config.transport.proxy = Some(proxy);
self
}
/// Set the maximum decompressed size for record batches.
///
/// Compressed payloads that decompress beyond this limit are rejected as
/// potential compression bombs. Defaults to
/// [`RecordBatch::MAX_DECOMPRESSED_SIZE`](crate::protocol::RecordBatch::MAX_DECOMPRESSED_SIZE) (128 MiB).
pub fn max_decompressed_size(mut self, size: usize) -> Self {
self.config.max_decompressed_size = size;
self
}
/// Build the share consumer.
pub async fn build(self) -> Result<ShareConsumer> {
if self.shared.is_none() && self.config.bootstrap_servers.is_empty() {
return Err(KrafkaError::config("bootstrap_servers is required"));
}
if self.config.group_id.is_empty() {
return Err(KrafkaError::config(
"group_id is required for share consumers",
));
}
if self.config.heartbeat_interval >= self.config.session_timeout {
return Err(KrafkaError::config(format!(
"heartbeat_interval ({:?}) must be less than session_timeout ({:?})",
self.config.heartbeat_interval, self.config.session_timeout,
)));
}
if self.config.max_buffered_records < 0 {
return Err(KrafkaError::config(format!(
"max_buffered_records ({}) must be >= 0 (use 0 for unlimited)",
self.config.max_buffered_records,
)));
}
if self.config.max_poll_records < 1 {
return Err(KrafkaError::config(format!(
"max_poll_records ({}) must be >= 1",
self.config.max_poll_records,
)));
}
ShareConsumer::new(
self.config,
self.shared,
self.key_deserializer,
self.value_deserializer,
)
.await
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
use super::*;
use crate::error::ErrorCode;
use crate::protocol::ShareAcquiredRecords;
/// The broker-reported acquisition-lock duration must reach the
/// application (KIP-1222).
///
/// `AcknowledgeType::Renew` extends a record's acquisition lock, and its
/// own documentation tells the caller to renew before the deadline. The
/// deadline comes from `group.share.record.lock.duration.ms`, which is a
/// **broker-side** setting no client can read from its own configuration —
/// so the only place it can come from is the `ShareFetch` response. That
/// field was decoded and dropped, leaving `Renew` documented, reachable,
/// and impossible to schedule correctly.
#[test]
fn the_acquisition_lock_timeout_reaches_the_application() {
let consumer = test_share_consumer(AcknowledgementMode::Explicit);
assert_eq!(
consumer.acquisition_lock_timeout(),
None,
"before any fetch, and on brokers that do not report it, there is \
nothing to report"
);
// What a successful ShareFetch records.
consumer
.0
.acquisition_lock_timeout_ms
.store(30_000, Ordering::Relaxed);
assert_eq!(
consumer.acquisition_lock_timeout(),
Some(Duration::from_secs(30)),
"the value the broker reported must be what the application sees"
);
// A broker older than Kafka 4.2 sends -1; that is absence, not zero.
consumer
.0
.acquisition_lock_timeout_ms
.store(-1, Ordering::Relaxed);
assert_eq!(consumer.acquisition_lock_timeout(), None);
}
fn test_share_consumer(acknowledgement_mode: AcknowledgementMode) -> ShareConsumer {
let mut config = ShareConsumer::builder()
.bootstrap_servers("localhost:9092")
.group_id("sg")
.acknowledgement_mode(acknowledgement_mode)
.config;
config.bootstrap_servers = "localhost:9092".to_string();
config.group_id = "sg".to_string();
let pool = Arc::new(ConnectionPool::new(ConnectionConfig::default()));
let metadata = Arc::new(ClusterMetadata::new(
vec!["localhost:9092".to_string()],
pool.clone(),
config.metadata_max_age,
));
ShareConsumer(Arc::new(ShareConsumerInner {
config,
metadata,
pool,
pool_owned: true,
metrics: Arc::new(crate::metrics::ConsumerMetrics::new()),
subscriptions: RwLock::new(HashSet::new()),
assignments: RwLock::new(HashMap::new()),
member_id: ArcSwap::new(Arc::new(crate::util::random_uuid_v4())),
member_epoch: AtomicI32::new(0),
heartbeat_interval_ms: AtomicI32::new(3000),
closed: AtomicBool::new(false),
share_sessions: Arc::new(tokio::sync::Mutex::new(ShareSessionCache::new())),
pending_acks: Arc::new(RwLock::new(HashMap::new())),
ack_state_generation: Arc::new(AtomicU64::new(0)),
explicit_flush_retry_required: Arc::new(AtomicBool::new(false)),
topic_ids: RwLock::new(HashMap::new()),
recv_buffer: RwLock::new(VecDeque::new()),
coordinator_id: RwLock::new(None),
coordinator_address: RwLock::new(None),
unacked_offsets: Arc::new(RwLock::new(HashSet::new())),
heartbeat_task: SyncMutex::new(None),
wakeup_flag: AtomicBool::new(false),
wakeup_notify: Notify::new(),
in_flight_polls: Arc::new(InFlightBarrier::new()),
acquisition_lock_timeout_ms: AtomicI32::new(-1),
key_deserializer: None,
value_deserializer: None,
}))
}
#[test]
fn test_share_consumer_builder_config() {
let builder = ShareConsumer::builder()
.bootstrap_servers("localhost:9092")
.group_id("my-share-group")
.client_id("test-client")
.acknowledgement_mode(AcknowledgementMode::Explicit)
.max_poll_records(100)
.session_timeout(Duration::from_secs(30))
.heartbeat_interval(Duration::from_secs(5));
assert_eq!(builder.config.bootstrap_servers, "localhost:9092");
assert_eq!(builder.config.group_id, "my-share-group");
assert_eq!(builder.config.client_id, "test-client");
assert_eq!(
builder.config.acknowledgement_mode,
AcknowledgementMode::Explicit
);
assert_eq!(builder.config.max_poll_records, 100);
assert_eq!(builder.config.session_timeout, Duration::from_secs(30));
assert_eq!(builder.config.heartbeat_interval, Duration::from_secs(5));
}
#[test]
fn test_share_consumer_builder_defaults() {
let builder = ShareConsumer::builder()
.bootstrap_servers("localhost:9092")
.group_id("sg");
assert_eq!(builder.config.client_id, "krafka");
assert_eq!(
builder.config.acknowledgement_mode,
AcknowledgementMode::Implicit
);
assert_eq!(builder.config.max_poll_records, 500);
assert!(builder.config.auth.is_none());
}
#[tokio::test]
async fn test_share_consumer_builder_validates_bootstrap() {
let result = ShareConsumer::builder().group_id("sg").build().await;
assert!(result.is_err());
let err = result.unwrap_err().to_string();
assert!(err.contains("bootstrap_servers"), "got: {err}");
}
#[tokio::test]
async fn test_share_consumer_builder_validates_group_id() {
let result = ShareConsumer::builder()
.bootstrap_servers("localhost:9092")
.build()
.await;
assert!(result.is_err());
let err = result.unwrap_err().to_string();
assert!(err.contains("group_id"), "got: {err}");
}
#[tokio::test]
async fn test_share_consumer_builder_validates_heartbeat() {
let result = ShareConsumer::builder()
.bootstrap_servers("localhost:9092")
.group_id("sg")
.session_timeout(Duration::from_secs(5))
.heartbeat_interval(Duration::from_secs(10))
.build()
.await;
assert!(result.is_err());
let err = result.unwrap_err().to_string();
assert!(err.contains("heartbeat_interval"), "got: {err}");
}
#[tokio::test]
async fn test_share_consumer_builder_rejects_negative_max_buffered_records() {
let result = ShareConsumer::builder()
.bootstrap_servers("localhost:9092")
.group_id("sg")
.max_buffered_records(-1)
.build()
.await;
assert!(result.is_err());
let err = result.unwrap_err().to_string();
assert!(err.contains("max_buffered_records"), "got: {err}");
}
#[tokio::test]
async fn test_share_consumer_builder_rejects_zero_max_poll_records() {
for bad in [0, -1, i32::MIN] {
let result = ShareConsumer::builder()
.bootstrap_servers("localhost:9092")
.group_id("sg")
.max_poll_records(bad)
.build()
.await;
assert!(result.is_err(), "expected error for max_poll_records={bad}");
let err = result.unwrap_err().to_string();
assert!(err.contains("max_poll_records"), "got: {err}");
}
}
#[test]
fn test_acknowledge_type_to_i8() {
assert_eq!(AcknowledgeType::Accept.to_i8(), 1);
assert_eq!(AcknowledgeType::Release.to_i8(), 2);
assert_eq!(AcknowledgeType::Reject.to_i8(), 3);
// KIP-932 + KIP-1222 wire values; 0 is the client-emitted "gap".
assert_eq!(AcknowledgeType::Renew.to_i8(), 4);
assert_eq!(GAP_ACK_TYPE, 0);
}
#[test]
fn test_acknowledgement_mode_default() {
assert_eq!(
AcknowledgementMode::default(),
AcknowledgementMode::Implicit
);
}
#[test]
fn test_share_consumer_config_accessors() {
let builder = ShareConsumer::builder()
.bootstrap_servers("broker:9092")
.group_id("sg-1")
.client_id("my-client")
.acknowledgement_mode(AcknowledgementMode::Explicit)
.session_timeout(Duration::from_secs(20))
.heartbeat_interval(Duration::from_secs(3));
assert_eq!(builder.config.bootstrap_servers(), "broker:9092");
assert_eq!(builder.config.group_id(), "sg-1");
assert_eq!(builder.config.client_id(), "my-client");
assert_eq!(
builder.config.acknowledgement_mode(),
AcknowledgementMode::Explicit
);
assert_eq!(builder.config.session_timeout(), Duration::from_secs(20));
assert_eq!(builder.config.heartbeat_interval(), Duration::from_secs(3));
}
#[test]
fn test_build_acknowledge_topics() {
let acks = vec![
PendingAck {
topic: "t1".into(),
topic_id: [1; 16],
partition: 0,
first_offset: 0,
last_offset: 5,
ack_type: AcknowledgeType::Accept.to_i8(),
},
PendingAck {
topic: "t1".into(),
topic_id: [1; 16],
partition: 1,
first_offset: 10,
last_offset: 15,
ack_type: AcknowledgeType::Release.to_i8(),
},
PendingAck {
topic: "t2".into(),
topic_id: [2; 16],
partition: 0,
first_offset: 0,
last_offset: 3,
ack_type: AcknowledgeType::Reject.to_i8(),
},
];
let topics = ShareConsumer::build_acknowledge_topics(&acks);
assert_eq!(topics.len(), 2); // Two distinct topic_ids
// Verify partition counts.
let total_partitions: usize = topics.iter().map(|t| t.partitions.len()).sum();
assert_eq!(total_partitions, 3);
}
/// A partition's acknowledgement batches are sorted by `first_offset`.
///
/// This is the wire form the Java client always produces (its ack map is
/// a `TreeMap`), and it is what makes the contiguous-range merge — which
/// prevents overlapping batches from failing the partition with
/// `INVALID_RECORD_STATE` — possible at all. Explicit acks arrive in
/// application call order, and acknowledging out of order is legal at the
/// API surface — so the ordering must be imposed when the wire request is
/// built, not assumed.
///
/// Negative control: removing the `sort_unstable_by_key` from
/// `build_acknowledge_topics` replays the call order (7, 3, 5) and fails.
#[test]
fn acknowledge_batches_are_sorted_regardless_of_call_order() {
let ack = |first: i64, last: i64, ack_type: AcknowledgeType| PendingAck {
topic: "t".into(),
topic_id: [1; 16],
partition: 0,
first_offset: first,
last_offset: last,
ack_type: ack_type.to_i8(),
};
// Application acked 7, then 3, then 5 — with a gap at 4 and 6, so
// nothing merges and the order alone is under test.
let acks = vec![
ack(7, 7, AcknowledgeType::Accept),
ack(3, 3, AcknowledgeType::Reject),
ack(5, 5, AcknowledgeType::Accept),
];
let topics = ShareConsumer::build_acknowledge_topics(&acks);
let batches = &topics[0].partitions[0].acknowledgement_batches;
let firsts: Vec<i64> = batches.iter().map(|b| b.first_offset).collect();
assert_eq!(firsts, vec![3, 5, 7], "batches must ascend by first_offset");
}
/// Adjacent single-type batches of the same type merge into one range, so
/// N sequential `acknowledge()` calls cost one wire batch — and a
/// different type, or a gap, starts a new batch.
#[test]
fn acknowledge_batches_merge_contiguous_same_type_ranges() {
let ack = |first: i64, last: i64, ack_type: AcknowledgeType| PendingAck {
topic: "t".into(),
topic_id: [1; 16],
partition: 0,
first_offset: first,
last_offset: last,
ack_type: ack_type.to_i8(),
};
let acks = vec![
// Acked out of order, all Accept, offsets 0..=2: one batch.
ack(1, 1, AcknowledgeType::Accept),
ack(0, 0, AcknowledgeType::Accept),
ack(2, 2, AcknowledgeType::Accept),
// Contiguous but a different type: its own batch.
ack(3, 3, AcknowledgeType::Release),
// Same type again but after a gap: its own batch.
ack(5, 6, AcknowledgeType::Release),
];
let topics = ShareConsumer::build_acknowledge_topics(&acks);
let batches = &topics[0].partitions[0].acknowledgement_batches;
let spans: Vec<(i64, i64, i8)> = batches
.iter()
.map(|b| (b.first_offset, b.last_offset, b.acknowledge_types[0]))
.collect();
assert_eq!(
spans,
vec![
(0, 2, AcknowledgeType::Accept.to_i8()),
(3, 3, AcknowledgeType::Release.to_i8()),
(5, 6, AcknowledgeType::Release.to_i8()),
],
);
}
#[test]
fn test_share_acknowledge_response_error_detects_partition_failure() {
let response = crate::protocol::ShareAcknowledgeResponse {
throttle_time_ms: 0,
error_code: ErrorCode::None,
error_message: None,
acquisition_lock_timeout_ms: -1,
responses: vec![crate::protocol::ShareAcknowledgeTopicResponse {
topic_id: [1; 16],
partitions: vec![crate::protocol::ShareAcknowledgePartitionResponse {
partition_index: 7,
error_code: ErrorCode::UnknownTopicOrPartition,
error_message: Some("gone".to_string()),
current_leader: crate::protocol::ShareLeaderIdAndEpoch {
leader_id: -1,
leader_epoch: 0,
},
}],
}],
node_endpoints: Vec::new(),
};
let error = ShareConsumer::share_acknowledge_response_error(&response)
.expect("partition error must surface as an error");
assert!(matches!(
error,
KrafkaError::Broker {
code: ErrorCode::UnknownTopicOrPartition,
..
}
));
}
#[tokio::test]
async fn test_restore_ack_state_requeues_pending_acks_without_reinserting_unacked() {
let ack_state_generation = AtomicU64::new(0);
let explicit_flush_retry_required = AtomicBool::new(false);
let pending: RwLock<BrokerPendingAcks> = RwLock::new(HashMap::new());
let mut acks_to_restore = vec![PendingAck {
topic: "topic-a".into(),
topic_id: [0; 16],
partition: 2,
first_offset: 11,
last_offset: 13,
ack_type: AcknowledgeType::Accept.to_i8(),
}];
ShareConsumer::restore_ack_state(
&ack_state_generation,
&pending,
&explicit_flush_retry_required,
0,
false,
&mut acks_to_restore,
)
.await;
assert!(acks_to_restore.is_empty());
assert!(!explicit_flush_retry_required.load(Ordering::SeqCst));
let guard = pending.read().await;
let all_acks: Vec<&PendingAck> =
guard.values().flat_map(|b| b.values().flatten()).collect();
assert_eq!(all_acks.len(), 1);
assert_eq!(all_acks[0].topic, "topic-a");
assert_eq!(all_acks[0].partition, 2);
assert_eq!(all_acks[0].first_offset, 11);
assert_eq!(all_acks[0].last_offset, 13);
assert_eq!(all_acks[0].ack_type, AcknowledgeType::Accept.to_i8());
}
#[tokio::test]
async fn test_restore_ack_state_skips_stale_generation() {
let ack_state_generation = AtomicU64::new(1);
let explicit_flush_retry_required = AtomicBool::new(false);
let pending: RwLock<BrokerPendingAcks> = RwLock::new(HashMap::new());
let mut acks_to_restore = vec![PendingAck {
topic: "topic-a".into(),
topic_id: [0; 16],
partition: 2,
first_offset: 11,
last_offset: 13,
ack_type: AcknowledgeType::Accept.to_i8(),
}];
ShareConsumer::restore_ack_state(
&ack_state_generation,
&pending,
&explicit_flush_retry_required,
0,
true,
&mut acks_to_restore,
)
.await;
assert!(pending.read().await.is_empty());
assert!(acks_to_restore.is_empty());
assert!(!explicit_flush_retry_required.load(Ordering::SeqCst));
}
#[tokio::test]
async fn test_restore_ack_state_marks_explicit_flush_retry_required() {
let ack_state_generation = AtomicU64::new(0);
let explicit_flush_retry_required = AtomicBool::new(false);
let pending: RwLock<BrokerPendingAcks> = RwLock::new(HashMap::new());
let mut acks_to_restore = vec![PendingAck {
topic: "topic-a".into(),
topic_id: [0; 16],
partition: 2,
first_offset: 11,
last_offset: 13,
ack_type: AcknowledgeType::Accept.to_i8(),
}];
ShareConsumer::restore_ack_state(
&ack_state_generation,
&pending,
&explicit_flush_retry_required,
0,
true,
&mut acks_to_restore,
)
.await;
assert!(acks_to_restore.is_empty());
assert!(explicit_flush_retry_required.load(Ordering::SeqCst));
assert_eq!(
pending
.read()
.await
.values()
.flat_map(|b| b.values().flatten())
.count(),
1
);
}
#[tokio::test]
async fn test_poll_rejects_after_failed_explicit_flush() {
let consumer = test_share_consumer(AcknowledgementMode::Explicit);
consumer
.0
.explicit_flush_retry_required
.store(true, Ordering::SeqCst);
let error = consumer
.poll(Duration::from_millis(1))
.await
.expect_err("poll must block after a failed explicit flush");
assert!(
error
.to_string()
.contains("retry the commit before calling poll() again")
);
}
#[tokio::test]
async fn test_clear_partition_state_clears_explicit_flush_retry_required() {
let consumer = test_share_consumer(AcknowledgementMode::Explicit);
consumer
.0
.explicit_flush_retry_required
.store(true, Ordering::SeqCst);
consumer.clear_partition_state().await;
assert!(
!consumer
.0
.explicit_flush_retry_required
.load(Ordering::SeqCst)
);
}
#[tokio::test]
async fn test_apply_assignment_advances_ack_state_generation_on_change() {
let consumer = test_share_consumer(AcknowledgementMode::Explicit);
consumer
.0
.assignments
.write()
.await
.insert("topic-a".to_string(), vec![0]);
consumer
.0
.pending_acks
.write()
.await
.entry(UNROUTED_BROKER_ID)
.or_default()
.entry(([1; 16], 0))
.or_default()
.push(PendingAck {
topic: "topic-a".to_string(),
topic_id: [1; 16],
partition: 0,
first_offset: 5,
last_offset: 5,
ack_type: AcknowledgeType::Accept.to_i8(),
});
consumer
.0
.unacked_offsets
.write()
.await
.insert(("topic-a".to_string(), 0, 5));
consumer
.0
.explicit_flush_retry_required
.store(true, Ordering::SeqCst);
let old_generation = consumer.0.ack_state_generation.load(Ordering::SeqCst);
consumer.apply_assignment(&[]).await;
assert_eq!(
consumer.0.ack_state_generation.load(Ordering::SeqCst),
old_generation + 1
);
assert!(
!consumer
.0
.explicit_flush_retry_required
.load(Ordering::SeqCst)
);
assert!(consumer.0.pending_acks.read().await.is_empty());
assert!(consumer.0.unacked_offsets.read().await.is_empty());
}
#[tokio::test]
async fn test_send_share_acknowledge_rejects_stale_ack_generation() {
let consumer = test_share_consumer(AcknowledgementMode::Explicit);
let outcome = ShareConsumer::send_share_acknowledge_with_state(
ShareAcknowledgeContext {
metadata: consumer.0.metadata.clone(),
pool: consumer.0.pool.clone(),
share_sessions: consumer.0.share_sessions.clone(),
group_id: consumer.0.config.group_id.clone(),
member_id: (**consumer.0.member_id.load()).clone(),
current_ack_state_generation: Arc::new(AtomicU64::new(1)),
ack_state_generation: 0,
},
&[PendingAck {
topic: "topic-a".to_string(),
topic_id: [1; 16],
partition: 0,
first_offset: 5,
last_offset: 5,
ack_type: AcknowledgeType::Accept.to_i8(),
}],
)
.await;
let error = outcome
.error
.expect("stale ack generation must be rejected before sending");
assert!(
error
.to_string()
.contains("acknowledgement state was invalidated")
);
assert_eq!(
outcome.failed.len(),
1,
"the un-sent acknowledgement must be reported back for restore"
);
}
#[tokio::test]
async fn test_share_commit_handle_ready_flattens_result() {
ShareCommitHandle::ready(Ok(()))
.await
.expect("ready ok result");
let error = ShareCommitHandle::ready(Err(KrafkaError::invalid_state("boom")))
.await
.expect_err("ready error must surface");
assert!(error.to_string().contains("boom"));
}
#[tokio::test]
async fn test_share_commit_handle_flattens_task_result() {
let error = ShareCommitHandle::Task(tokio::spawn(async {
Err(KrafkaError::invalid_state("task failed"))
}))
.await
.expect_err("task error must surface");
assert!(error.to_string().contains("task failed"));
}
#[tokio::test]
async fn test_describe_share_fetch_join_error_reports_panic() {
let error = tokio::spawn(async {
panic!("boom");
})
.await
.expect_err("panic must surface as a JoinError");
assert_eq!(describe_share_fetch_join_error(&error), "panicked");
}
#[tokio::test]
async fn test_describe_share_fetch_join_error_reports_cancellation() {
let handle = tokio::spawn(async {
std::future::pending::<()>().await;
});
handle.abort();
let error = handle
.await
.expect_err("aborted task must surface as a JoinError");
assert_eq!(describe_share_fetch_join_error(&error), "was cancelled");
}
#[tokio::test]
async fn test_acknowledge_keeps_record_pending_until_ack_is_queued() {
let consumer = Arc::new(test_share_consumer(AcknowledgementMode::Explicit));
consumer
.0
.topic_ids
.write()
.await
.insert("topic-a".to_string(), [7; 16]);
let record = ConsumerRecord {
topic: "topic-a".into(),
partition: 3,
offset: 11,
timestamp: 0,
timestamp_type: 0,
key: None,
value: None,
headers: Vec::new(),
leader_epoch: None,
delivery_count: None,
};
let record_key = (record.topic.clone(), record.partition, record.offset);
consumer
.0
.unacked_offsets
.write()
.await
.insert(record_key.clone());
let pending_guard = consumer.0.pending_acks.write().await;
let task_consumer = consumer.clone();
let task = tokio::spawn(async move {
task_consumer
.acknowledge(&record, AcknowledgeType::Accept)
.await
});
tokio::task::yield_now().await;
assert!(
consumer
.0
.unacked_offsets
.read()
.await
.contains(&record_key)
);
assert!(
!task.is_finished(),
"acknowledge should still be waiting on the pending_acks lock"
);
drop(pending_guard);
task.await
.expect("acknowledge task should join")
.expect("acknowledge should succeed once pending lock is released");
assert!(
!consumer
.0
.unacked_offsets
.read()
.await
.contains(&record_key)
);
let pending_guard = consumer.0.pending_acks.read().await;
let all_acks: Vec<&PendingAck> = pending_guard
.values()
.flat_map(|b| b.values().flatten())
.collect();
assert_eq!(all_acks.len(), 1);
assert_eq!(all_acks[0].topic, "topic-a");
assert_eq!(all_acks[0].partition, 3);
assert_eq!(all_acks[0].first_offset, 11);
assert_eq!(all_acks[0].last_offset, 11);
assert_eq!(all_acks[0].ack_type, AcknowledgeType::Accept.to_i8());
}
#[test]
fn test_coalesce_implicit_acks_merges_consecutive() {
let records = vec![
ConsumerRecord {
topic: "t1".into(),
partition: 0,
offset: 0,
timestamp: 0,
timestamp_type: 0,
key: None,
value: None,
headers: Vec::new(),
leader_epoch: None,
delivery_count: None,
},
ConsumerRecord {
topic: "t1".into(),
partition: 0,
offset: 1,
timestamp: 0,
timestamp_type: 0,
key: None,
value: None,
headers: Vec::new(),
leader_epoch: None,
delivery_count: None,
},
ConsumerRecord {
topic: "t1".into(),
partition: 0,
offset: 2,
timestamp: 0,
timestamp_type: 0,
key: None,
value: None,
headers: Vec::new(),
leader_epoch: None,
delivery_count: None,
},
// Gap: offset 3 missing
ConsumerRecord {
topic: "t1".into(),
partition: 0,
offset: 4,
timestamp: 0,
timestamp_type: 0,
key: None,
value: None,
headers: Vec::new(),
leader_epoch: None,
delivery_count: None,
},
];
let mut topic_ids = HashMap::new();
topic_ids.insert("t1".to_string(), [1u8; 16]);
let mut pending: BrokerPendingAcks = HashMap::new();
// Pass a dummy ClusterMetadata — no live brokers, so all acks route to UNROUTED_BROKER_ID.
let dummy_pool = Arc::new(ConnectionPool::new(ConnectionConfig::default()));
let dummy_metadata = ClusterMetadata::new(
vec!["localhost:9092".to_string()],
dummy_pool,
Duration::from_secs(300),
);
ShareConsumer::coalesce_implicit_acks(&records, &topic_ids, &mut pending, &dummy_metadata);
// Should produce two ranges: [0,2] and [4,4].
let mut all_acks: Vec<PendingAck> = pending
.into_values()
.flat_map(|b| b.into_values().flatten())
.collect();
assert_eq!(all_acks.len(), 2);
all_acks.sort_by_key(|a| a.first_offset);
assert_eq!(all_acks[0].first_offset, 0);
assert_eq!(all_acks[0].last_offset, 2);
assert_eq!(all_acks[1].first_offset, 4);
assert_eq!(all_acks[1].last_offset, 4);
}
#[test]
fn test_config_defaults_match_kip932() {
let builder = ShareConsumer::builder()
.bootstrap_servers("localhost:9092")
.group_id("sg");
// KIP-932 specifies 45s session timeout.
assert_eq!(builder.config.session_timeout(), Duration::from_secs(45));
assert_eq!(builder.config.heartbeat_interval(), Duration::from_secs(5));
}
// ── Regression test: F-06 / ack_state_generation ordering ────────────────
/// `ensure_ack_state_current` must accept a matching generation and reject
/// any stale one, whether the captured snapshot is behind OR ahead of the
/// current value (the latter is impossible in practice but good to guard).
///
/// This is a unit test of the pure guard function — no async machinery
/// required. The same logic is exercised end-to-end by
/// `test_send_share_acknowledge_rejects_stale_ack_generation`.
#[test]
fn test_ensure_ack_state_current_rejects_stale_generation() {
let current = AtomicU64::new(5);
// Exact match → Ok.
assert!(
ShareConsumer::ensure_ack_state_current(¤t, 5).is_ok(),
"matching generation must succeed"
);
// Stale (lower than current) — the common invalidation case.
assert!(
ShareConsumer::ensure_ack_state_current(¤t, 4).is_err(),
"stale lower generation must be rejected"
);
// Stale (higher than current) — shouldn't happen, but the guard covers it.
assert!(
ShareConsumer::ensure_ack_state_current(¤t, 6).is_err(),
"stale higher generation must be rejected"
);
}
/// Incrementing `ack_state_generation` must be visible to concurrent
/// callers that captured the old value, without any spurious success.
///
/// Simulates: flush task captures generation, assignment changes, flush
/// task calls `ensure_ack_state_current` and must receive an error.
#[tokio::test]
async fn test_ack_state_generation_flush_task_sees_invalidation() {
let consumer = Arc::new(test_share_consumer(AcknowledgementMode::Explicit));
// Capture the generation as a flush task would at spawn time.
let captured_gen = consumer.0.ack_state_generation.load(Ordering::SeqCst);
assert_eq!(captured_gen, 0);
// Simulate assignment change / unsubscribe which advances the generation.
consumer.clear_partition_state().await;
let new_gen = consumer.0.ack_state_generation.load(Ordering::SeqCst);
assert!(
new_gen > captured_gen,
"generation must advance after clear_partition_state"
);
// A detached flush task using the captured (old) generation must be blocked.
let err =
ShareConsumer::ensure_ack_state_current(&consumer.0.ack_state_generation, captured_gen)
.expect_err("stale flush task must be rejected");
assert!(err.to_string().contains("invalidated"), "got: {err}");
}
/// Cloning a `ShareConsumer` produces a second handle to the same state.
#[test]
fn test_share_consumer_clone_shares_state() {
let consumer = test_share_consumer(AcknowledgementMode::Implicit);
let cloned = consumer.clone();
// Both handles share the same Arc — pointer equality confirms this.
assert!(Arc::ptr_eq(&consumer.0, &cloned.0));
// A store via one handle is immediately visible through the other.
consumer.0.member_epoch.store(42, Ordering::Release);
assert_eq!(cloned.0.member_epoch.load(Ordering::Acquire), 42);
}
/// The background heartbeat task field starts as `None` and can be set.
#[test]
fn test_heartbeat_task_starts_none() {
let consumer = test_share_consumer(AcknowledgementMode::Implicit);
let guard = consumer.0.heartbeat_task.lock().unwrap();
assert!(guard.is_none(), "heartbeat task should start as None");
}
/// `close()` marks the consumer closed and the drop warning is suppressed.
#[tokio::test]
async fn test_close_is_idempotent_and_suppresses_drop_warning() {
let consumer = test_share_consumer(AcknowledgementMode::Implicit);
// First close: should succeed even without a coordinator.
let _ = consumer.close().await;
assert!(consumer.is_closed());
// Second close: must be idempotent (no panic, no error).
let _ = consumer.close().await;
}
// ── Helpers ──────────────────────────────────────────────────────────
fn test_record(topic: &str, partition: PartitionId, offset: Offset) -> ConsumerRecord {
ConsumerRecord {
topic: topic.to_string(),
partition,
offset,
timestamp: 0,
timestamp_type: 0,
key: None,
value: None,
headers: Vec::new(),
leader_epoch: None,
delivery_count: None,
}
}
fn acquired(first: Offset, last: Offset, delivery_count: i16) -> ShareAcquiredRecords {
ShareAcquiredRecords {
first_offset: first,
last_offset: last,
delivery_count,
}
}
// ── Overflow must be buffered, never acknowledged ───────
/// Implicit mode: a fetch larger than `max_poll_records` must queue accepts
/// only for the records actually returned. Accepting the surplus would
/// consume records that were never delivered — silent, permanent data loss.
#[tokio::test]
async fn test_register_delivered_records_only_acks_delivered_records_implicit() {
let consumer = test_share_consumer(AcknowledgementMode::Implicit);
consumer
.0
.topic_ids
.write()
.await
.insert("t".to_string(), [9; 16]);
let delivered: Vec<ConsumerRecord> = (0..3).map(|o| test_record("t", 0, o)).collect();
consumer.register_delivered_records(&delivered).await;
let pending = consumer.0.pending_acks.read().await;
let acks: Vec<&PendingAck> = pending
.values()
.flat_map(|b| b.values().flatten())
.collect();
assert_eq!(acks.len(), 1, "contiguous offsets coalesce into one range");
assert_eq!(acks[0].first_offset, 0);
assert_eq!(
acks[0].last_offset, 2,
"only the delivered offsets 0..=2 may be accepted"
);
}
/// Explicit mode: only delivered records may enter `unacked_offsets`.
/// Tracking undelivered offsets wedges `poll()` forever, because the
/// application can never acknowledge a record it never received.
#[tokio::test]
async fn test_register_delivered_records_only_tracks_delivered_records_explicit() {
let consumer = test_share_consumer(AcknowledgementMode::Explicit);
let delivered: Vec<ConsumerRecord> = (0..2).map(|o| test_record("t", 0, o)).collect();
consumer.register_delivered_records(&delivered).await;
let unacked = consumer.0.unacked_offsets.read().await;
assert_eq!(unacked.len(), 2);
assert!(unacked.contains(&("t".to_string(), 0, 0)));
assert!(unacked.contains(&("t".to_string(), 0, 1)));
assert!(
!unacked.contains(&("t".to_string(), 0, 2)),
"an undelivered offset must never be marked unacknowledged"
);
}
/// Records buffered by an oversized fetch are handed out by the next
/// `poll()` and are registered at that point — not before.
#[tokio::test]
async fn test_poll_drains_buffer_and_registers_on_delivery() {
let consumer = test_share_consumer(AcknowledgementMode::Explicit);
{
let mut buffer = consumer.0.recv_buffer.write().await;
for offset in 0..5 {
buffer.push_back(test_record("t", 0, offset));
}
}
// Nothing is tracked while the records merely sit in the buffer.
assert!(consumer.0.unacked_offsets.read().await.is_empty());
let records = consumer
.poll_inner(Duration::from_millis(1), 2)
.await
.expect("buffered records must be returned without a fetch");
assert_eq!(records.len(), 2, "poll must respect the record limit");
assert_eq!(records[0].offset, 0);
assert_eq!(records[1].offset, 1);
assert_eq!(
consumer.0.recv_buffer.read().await.len(),
3,
"the remainder stays buffered"
);
assert_eq!(
consumer.0.unacked_offsets.read().await.len(),
2,
"only the two delivered records are tracked"
);
}
// ── Acquired-range validation ───────────────────────────────────
/// An inverted range (`last < first`) is malformed and must be ignored
/// rather than iterated.
#[test]
fn test_build_delivery_counts_rejects_inverted_range() {
let counts = build_delivery_counts(&[acquired(100, 5, 1)], 1024);
assert!(counts.is_empty(), "inverted range must be dropped");
}
/// A decode desync yielding `0..=i64::MAX` must not be materialised: the
/// range is capped by the number of records that could possibly decode.
#[test]
fn test_build_delivery_counts_caps_absurd_range() {
let counts = build_delivery_counts(&[acquired(0, i64::MAX, 3)], 16);
assert_eq!(counts.len(), 16, "range must be capped, not materialised");
assert_eq!(counts.get(&0).copied(), Some(3));
assert_eq!(counts.get(&15).copied(), Some(3));
assert!(counts.get(&16).is_none());
}
/// With no record bytes there is nothing decodable, so no offset is tracked.
#[test]
fn test_build_delivery_counts_empty_when_no_record_bytes() {
assert!(build_delivery_counts(&[acquired(0, 1_000, 1)], 0).is_empty());
}
/// Well-formed ranges are expanded exactly.
#[test]
fn test_build_delivery_counts_expands_valid_ranges() {
let counts = build_delivery_counts(&[acquired(10, 12, 2), acquired(20, 20, 7)], 1024);
assert_eq!(counts.len(), 4);
assert_eq!(counts.get(&10).copied(), Some(2));
assert_eq!(counts.get(&12).copied(), Some(2));
assert_eq!(counts.get(&20).copied(), Some(7));
}
// ── Undecodable offsets are acknowledged as gaps ──────────────────────
/// Offsets acquired but not decoded must be acknowledged as gaps, otherwise
/// they are redelivered forever with a climbing `delivery_count`.
#[test]
fn test_build_gap_acks_covers_undecoded_offsets() {
let mut acquired_counts: HashMap<Offset, i16> = HashMap::new();
for offset in 0..6 {
acquired_counts.insert(offset, 1);
}
// Offsets 0,1 decoded; 2,3,4 failed; 5 decoded.
let decoded: HashSet<Offset> = [0, 1, 5].into_iter().collect();
let mut acks = build_gap_acks("t", [4; 16], 3, &acquired_counts, &decoded);
acks.sort_by_key(|a| a.first_offset);
assert_eq!(acks.len(), 1, "contiguous gaps coalesce");
assert_eq!(acks[0].first_offset, 2);
assert_eq!(acks[0].last_offset, 4);
assert_eq!(acks[0].ack_type, GAP_ACK_TYPE);
assert_eq!(acks[0].partition, 3);
assert_eq!(acks[0].topic, "t");
}
/// When everything decoded there is nothing to report as a gap.
#[test]
fn test_build_gap_acks_empty_when_all_decoded() {
let mut acquired_counts: HashMap<Offset, i16> = HashMap::new();
acquired_counts.insert(0, 1);
acquired_counts.insert(1, 1);
let decoded: HashSet<Offset> = [0, 1].into_iter().collect();
assert!(build_gap_acks("t", [0; 16], 0, &acquired_counts, &decoded).is_empty());
}
// ── Share-session error classification ──────────────────────────
/// The three share-session error codes must all trigger a session reset.
#[test]
fn test_share_session_errors_are_classified() {
assert!(is_share_session_error(ErrorCode::ShareSessionNotFound));
assert!(is_share_session_error(ErrorCode::InvalidShareSessionEpoch));
assert!(is_share_session_error(ErrorCode::ShareSessionLimitReached));
assert!(!is_share_session_error(ErrorCode::None));
assert!(!is_share_session_error(ErrorCode::NotCoordinator));
}
/// A successful `ShareAcknowledge` consumes the share-session epoch, so the
/// client must advance it. Leaving it stale makes the next `ShareFetch`
/// send an epoch the broker already used, which it rejects with
/// `INVALID_SHARE_SESSION_EPOCH` — permanently, since nothing resets it.
#[tokio::test]
async fn test_share_session_epoch_advances_on_acknowledge_success() {
let sessions = Arc::new(tokio::sync::Mutex::new(ShareSessionCache::new()));
{
let mut guard = sessions.lock().await;
guard.get_or_create(1).on_success(); // ShareFetch succeeded: epoch 1
}
assert_eq!(sessions.lock().await.get(1).map(|s| s.epoch()), Some(1));
// The success arm of `send_broker_acknowledge` performs exactly this.
sessions.lock().await.get_or_create(1).on_success();
assert_eq!(
sessions.lock().await.get(1).map(|s| s.epoch()),
Some(2),
"ShareAcknowledge must advance the epoch it consumed"
);
}
/// A share-session error resets the broker back to epoch 0 so the retry
/// opens a fresh session instead of resending the stale epoch.
#[tokio::test]
async fn test_share_session_reset_returns_to_initial_epoch() {
let sessions = Arc::new(tokio::sync::Mutex::new(ShareSessionCache::new()));
{
let mut guard = sessions.lock().await;
guard.get_or_create(7).on_success();
guard.get_or_create(7).on_success();
}
assert_eq!(sessions.lock().await.get(7).map(|s| s.epoch()), Some(2));
sessions.lock().await.reset_broker(7);
assert_eq!(
sessions.lock().await.get(7).map(|s| s.epoch()),
Some(session::INITIAL_EPOCH),
"a stale session must restart at epoch 0"
);
}
// ── Per-broker acknowledge outcome ───────────────────────────────────
/// A multi-broker acknowledge that fails on one broker must report only
/// that broker's acks as failed. Restoring the whole batch would make the
/// retry re-acknowledge offsets other brokers already accepted, which they
/// reject with `INVALID_RECORD_STATE`.
#[test]
fn test_share_acknowledge_outcome_tracks_only_failed_acks() {
let mut outcome = ShareAcknowledgeOutcome::default();
assert!(outcome.error.is_none());
assert!(outcome.failed.is_empty());
let failed_ack = PendingAck {
topic: "t".into(),
topic_id: [1; 16],
partition: 3,
first_offset: 0,
last_offset: 0,
ack_type: AcknowledgeType::Accept.to_i8(),
};
outcome.fail(
std::iter::once(failed_ack),
KrafkaError::invalid_state("broker 3 down"),
);
outcome.fail(
std::iter::empty(),
KrafkaError::invalid_state("later error"),
);
assert_eq!(outcome.failed.len(), 1);
assert_eq!(outcome.failed[0].partition, 3);
assert!(
outcome
.error
.expect("first error is kept")
.to_string()
.contains("broker 3 down"),
"the first error must be preserved"
);
}
// ── Drop guard: drained acks survive future cancellation ─────────────
/// Dropping a future that had drained `pending_acks` must re-queue them.
/// Otherwise a `select!` shutdown arm silently discards explicit
/// `Reject`/`Release` decisions the application already made.
#[tokio::test]
async fn test_pending_ack_guard_restores_on_drop() {
let pending: Arc<RwLock<BrokerPendingAcks>> = Arc::new(RwLock::new(HashMap::new()));
let generation = Arc::new(AtomicU64::new(0));
let retry = Arc::new(AtomicBool::new(false));
{
let _guard = PendingAckGuard::new(
vec![PendingAck {
topic: "t".into(),
topic_id: [2; 16],
partition: 1,
first_offset: 4,
last_offset: 6,
ack_type: AcknowledgeType::Reject.to_i8(),
}],
pending.clone(),
generation.clone(),
0,
retry.clone(),
true,
);
} // dropped without disarm
let guard = pending.read().await;
let acks: Vec<&PendingAck> = guard.values().flat_map(|b| b.values().flatten()).collect();
assert_eq!(acks.len(), 1, "the Reject decision must survive the drop");
assert_eq!(acks[0].ack_type, AcknowledgeType::Reject.to_i8());
assert_eq!(acks[0].first_offset, 4);
assert!(retry.load(Ordering::SeqCst));
}
/// A disarmed guard restores nothing — the acks were handled.
#[tokio::test]
async fn test_pending_ack_guard_disarm_suppresses_restore() {
let pending: Arc<RwLock<BrokerPendingAcks>> = Arc::new(RwLock::new(HashMap::new()));
let generation = Arc::new(AtomicU64::new(0));
let retry = Arc::new(AtomicBool::new(false));
{
let mut guard = PendingAckGuard::new(
vec![PendingAck {
topic: "t".into(),
topic_id: [2; 16],
partition: 1,
first_offset: 4,
last_offset: 4,
ack_type: AcknowledgeType::Accept.to_i8(),
}],
pending.clone(),
generation.clone(),
0,
retry.clone(),
true,
);
assert_eq!(guard.acks().len(), 1);
assert_eq!(guard.disarm().len(), 1);
}
assert!(pending.read().await.is_empty());
assert!(!retry.load(Ordering::SeqCst));
}
/// A guard whose generation was invalidated must drop its acks rather than
/// resurrect state from an old membership.
#[tokio::test]
async fn test_pending_ack_guard_ignores_stale_generation() {
let pending: Arc<RwLock<BrokerPendingAcks>> = Arc::new(RwLock::new(HashMap::new()));
let generation = Arc::new(AtomicU64::new(0));
let retry = Arc::new(AtomicBool::new(false));
{
let _guard = PendingAckGuard::new(
vec![PendingAck {
topic: "t".into(),
topic_id: [2; 16],
partition: 1,
first_offset: 4,
last_offset: 4,
ack_type: AcknowledgeType::Accept.to_i8(),
}],
pending.clone(),
generation.clone(),
0,
retry.clone(),
true,
);
// Assignment change / unsubscribe invalidates the ack state.
generation.store(1, Ordering::SeqCst);
}
assert!(pending.read().await.is_empty());
assert!(!retry.load(Ordering::SeqCst));
}
// ── KIP-1222 Renew is only sent to brokers that support it ───────────
/// Sending `Renew` to a pre-4.2 broker fails the whole batch with
/// INVALID_REQUEST, so those entries are stripped for older versions.
#[test]
fn test_strip_unsupported_renew_acks_removes_renew_only_batches() {
let mut batches = vec![
ShareAcknowledgementBatch {
first_offset: 0,
last_offset: 0,
acknowledge_types: vec![AcknowledgeType::Accept.to_i8()],
},
ShareAcknowledgementBatch {
first_offset: 1,
last_offset: 1,
acknowledge_types: vec![AcknowledgeType::Renew.to_i8()],
},
ShareAcknowledgementBatch {
first_offset: 2,
last_offset: 2,
acknowledge_types: vec![
AcknowledgeType::Renew.to_i8(),
AcknowledgeType::Reject.to_i8(),
],
},
];
let dropped = strip_unsupported_renew_acks(std::iter::once(&mut batches));
assert_eq!(dropped, 1, "only the Renew-only batch is dropped");
assert_eq!(batches.len(), 2);
assert_eq!(batches[0].first_offset, 0);
assert_eq!(batches[1].first_offset, 2);
assert_eq!(
batches[1].acknowledge_types,
vec![AcknowledgeType::Reject.to_i8()],
"the mixed batch keeps its other acknowledgement types"
);
}
/// Nothing is stripped when no `Renew` is present.
#[test]
fn test_strip_unsupported_renew_acks_is_a_noop_without_renew() {
let mut batches = vec![ShareAcknowledgementBatch {
first_offset: 0,
last_offset: 5,
acknowledge_types: vec![AcknowledgeType::Accept.to_i8()],
}];
assert_eq!(
strip_unsupported_renew_acks(std::iter::once(&mut batches)),
0
);
assert_eq!(batches.len(), 1);
}
// ── Lifecycle ─────────────────────────────────────────────
/// `recv()` reports `Ok(None)` for a closed consumer — the only case that
/// terminates the record stream.
#[tokio::test]
async fn test_recv_returns_none_only_when_closed() {
let consumer = test_share_consumer(AcknowledgementMode::Implicit);
consumer.0.closed.store(true, Ordering::SeqCst);
assert!(
consumer
.recv()
.await
.expect("closed recv is not an error")
.is_none(),
"a closed consumer ends the stream"
);
}
/// `recv()` must not terminate just because a poll came back empty: it
/// keeps waiting, and a record produced later is still delivered.
#[tokio::test]
async fn test_recv_waits_through_empty_polls() {
let consumer = Arc::new(test_share_consumer(AcknowledgementMode::Implicit));
// No assignment, so every internal poll returns empty.
let receiver = consumer.clone();
let handle = tokio::spawn(async move { receiver.recv().await });
// Give it several empty poll cycles; it must still be waiting.
tokio::time::sleep(RECV_EMPTY_POLL_BACKOFF * 3).await;
assert!(
!handle.is_finished(),
"recv() must keep waiting on an idle topic, not return None"
);
// A record arriving later is delivered.
consumer
.0
.recv_buffer
.write()
.await
.push_back(test_record("t", 0, 42));
let record = tokio::time::timeout(Duration::from_secs(5), handle)
.await
.expect("recv must finish once a record is available")
.expect("join")
.expect("recv must not error")
.expect("a record must be delivered");
assert_eq!(record.offset, 42);
}
/// The heartbeat task must hold only a weak reference, so dropping every
/// `ShareConsumer` handle lets the consumer be reclaimed even if `close()`
/// was never called.
#[tokio::test]
async fn test_heartbeat_task_does_not_keep_consumer_alive() {
let consumer = test_share_consumer(AcknowledgementMode::Implicit);
let weak = Arc::downgrade(&consumer.0);
// Spawn the heartbeat loop exactly as `subscribe()` does.
let handle = {
let bg = Arc::downgrade(&consumer.0);
tokio::spawn(async move {
ShareConsumer::run_heartbeat_loop(bg).await;
})
};
*consumer
.0
.heartbeat_task
.lock()
.unwrap_or_else(|e| e.into_inner()) = Some(handle);
// Drop the only user-facing handle without calling close().
drop(consumer);
assert!(
weak.upgrade().is_none(),
"a strong reference in the heartbeat task would leak the consumer, \
its connection pool, and its group membership"
);
}
/// The weak heartbeat loop exits promptly once the consumer is gone.
#[tokio::test]
async fn test_heartbeat_loop_exits_when_consumer_dropped() {
let consumer = test_share_consumer(AcknowledgementMode::Implicit);
consumer.0.heartbeat_interval_ms.store(5, Ordering::Release);
let weak = Arc::downgrade(&consumer.0);
let handle = tokio::spawn(async move {
ShareConsumer::run_heartbeat_loop(weak).await;
});
drop(consumer);
tokio::time::timeout(Duration::from_secs(5), handle)
.await
.expect("the heartbeat loop must exit once every handle is dropped")
.expect("heartbeat task must not panic");
}
/// `wakeup()` sets the flag *and* signals waiters so an in-flight poll can
/// be interrupted rather than having to run to completion.
#[tokio::test]
async fn test_wakeup_interrupts_a_waiting_poll() {
let consumer = Arc::new(test_share_consumer(AcknowledgementMode::Implicit));
let waiter = consumer.clone();
let notified = tokio::spawn(async move {
waiter.0.wakeup_notify.notified().await;
});
tokio::task::yield_now().await;
consumer.wakeup();
tokio::time::timeout(Duration::from_secs(5), notified)
.await
.expect("wakeup() must signal waiters, not only set a flag")
.expect("waiter task must not panic");
// The flag path still fails the next poll immediately.
let error = consumer
.poll(Duration::from_millis(1))
.await
.expect_err("a pending wakeup fails the next poll");
assert!(error.to_string().contains("wakeup"));
// ...and is consumed, so the poll after that is not failed spuriously.
assert!(!consumer.0.wakeup_flag.load(Ordering::Acquire));
}
/// `fetch_max_wait` must actually bound the fetch wait rather than being
/// ignored in favour of the poll timeout.
#[test]
fn test_fetch_max_wait_bounds_the_poll_timeout() {
let config = ShareConsumer::builder()
.bootstrap_servers("localhost:9092")
.group_id("sg")
.fetch_max_wait(Duration::from_millis(250))
.config;
let configured = crate::util::duration_to_millis_i32(config.fetch_max_wait());
let poll_wait_ms = crate::util::duration_to_millis_i32(Duration::from_secs(30));
assert_eq!(
poll_wait_ms.min(configured),
250,
"fetch_max_wait must cap the fetch wait"
);
// A poll timeout shorter than the config wins.
let short = crate::util::duration_to_millis_i32(Duration::from_millis(10));
assert_eq!(short.min(configured), 10);
}
}