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
use crate::frame::kafka::{KafkaFrame, RequestBody, ResponseBody};
use crate::frame::{Frame, MessageType};
use crate::message::{Message, Messages};
use crate::tls::{TlsConnector, TlsConnectorConfig};
use crate::transforms::kafka::sink_cluster::shotover_node::start_shotover_peers_check;
use crate::transforms::{
ChainState, DownChainProtocol, Transform, TransformBuilder, TransformContextBuilder,
UpChainProtocol,
};
use crate::transforms::{TransformConfig, TransformContextConfig};
use anyhow::{Context, Result, anyhow};
use async_trait::async_trait;
use connections::{Connections, Destination};
use dashmap::DashMap;
use kafka_node::{ConnectionFactory, KafkaAddress, KafkaNode, KafkaNodeState};
use kafka_protocol::ResponseError;
use kafka_protocol::messages::add_partitions_to_txn_request::AddPartitionsToTxnTransaction;
use kafka_protocol::messages::delete_records_request::DeleteRecordsTopic;
use kafka_protocol::messages::delete_records_response::DeleteRecordsTopicResult;
use kafka_protocol::messages::describe_cluster_response::DescribeClusterBroker;
use kafka_protocol::messages::describe_producers_request::TopicRequest;
use kafka_protocol::messages::describe_producers_response::TopicResponse;
use kafka_protocol::messages::fetch_request::FetchTopic;
use kafka_protocol::messages::fetch_response::LeaderIdAndEpoch as FetchResponseLeaderIdAndEpoch;
use kafka_protocol::messages::list_offsets_request::ListOffsetsTopic;
use kafka_protocol::messages::metadata_request::MetadataRequestTopic;
use kafka_protocol::messages::metadata_response::MetadataResponseBroker;
use kafka_protocol::messages::offset_fetch_request::OffsetFetchRequestGroup;
use kafka_protocol::messages::offset_for_leader_epoch_request::OffsetForLeaderTopic;
use kafka_protocol::messages::produce_request::TopicProduceData;
use kafka_protocol::messages::produce_response::{
LeaderIdAndEpoch as ProduceResponseLeaderIdAndEpoch, TopicProduceResponse,
};
use kafka_protocol::messages::{
AddOffsetsToTxnRequest, AddPartitionsToTxnRequest, AddPartitionsToTxnResponse, ApiKey,
BrokerId, ConsumerGroupDescribeRequest, ConsumerGroupDescribeResponse,
ConsumerGroupHeartbeatRequest, DeleteGroupsRequest, DeleteGroupsResponse, DeleteRecordsRequest,
DeleteRecordsResponse, DescribeClusterResponse, DescribeGroupsRequest, DescribeGroupsResponse,
DescribeLogDirsResponse, DescribeProducersRequest, DescribeProducersResponse,
DescribeTransactionsRequest, DescribeTransactionsResponse, EndTxnRequest, FetchRequest,
FetchResponse, FindCoordinatorRequest, FindCoordinatorResponse, GroupId, HeartbeatRequest,
InitProducerIdRequest, JoinGroupRequest, LeaveGroupRequest, ListGroupsResponse,
ListOffsetsRequest, ListOffsetsResponse, ListTransactionsResponse, MetadataRequest,
MetadataResponse, OffsetFetchRequest, OffsetFetchResponse, OffsetForLeaderEpochRequest,
OffsetForLeaderEpochResponse, ProduceRequest, ProduceResponse, RequestHeader,
SaslAuthenticateRequest, SaslAuthenticateResponse, SaslHandshakeRequest, SyncGroupRequest,
TopicName, TransactionalId, TxnOffsetCommitRequest,
};
use kafka_protocol::protocol::StrBytes;
use metrics::{Counter, counter};
use rand::SeedableRng;
use rand::rngs::SmallRng;
use rand::seq::{IndexedRandom, IteratorRandom};
use scram_over_mtls::{
AuthorizeScramOverMtls, AuthorizeScramOverMtlsBuilder, AuthorizeScramOverMtlsConfig,
OriginalScramState,
};
use serde::{Deserialize, Serialize};
use shotover_node::{ShotoverNode, ShotoverNodeConfig};
use split::{
AddPartitionsToTxnRequestSplitAndRouter, ConsumerGroupDescribeSplitAndRouter,
DeleteGroupsSplitAndRouter, DeleteRecordsRequestSplitAndRouter, DescribeGroupsSplitAndRouter,
DescribeLogDirsSplitAndRouter, DescribeProducersRequestSplitAndRouter,
DescribeTransactionsSplitAndRouter, ListGroupsSplitAndRouter, ListOffsetsRequestSplitAndRouter,
ListTransactionsSplitAndRouter, OffsetFetchSplitAndRouter,
OffsetForLeaderEpochRequestSplitAndRouter, ProduceRequestSplitAndRouter, RequestSplitAndRouter,
};
use std::collections::{HashMap, HashSet, VecDeque};
use std::hash::Hasher;
use std::sync::Arc;
use std::sync::atomic::AtomicI64;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
use uuid::Uuid;
mod api_versions;
mod connections;
mod kafka_node;
mod scram_over_mtls;
pub mod shotover_node;
pub(crate) mod split;
const SASL_SCRAM_MECHANISMS: [&str; 2] = ["SCRAM-SHA-256", "SCRAM-SHA-512"];
#[derive(thiserror::Error, Debug)]
enum FindCoordinatorError {
#[error("Coordinator not available")]
CoordinatorNotAvailable,
#[error("{0:?}")]
Unrecoverable(#[from] anyhow::Error),
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct KafkaSinkClusterConfig {
pub first_contact_points: Vec<String>,
pub shotover_nodes: Vec<ShotoverNodeConfig>,
pub local_shotover_broker_id: i32,
pub connect_timeout_ms: u64,
pub read_timeout: Option<u64>,
pub check_shotover_peers_delay_ms: Option<u64>,
pub tls: Option<TlsConnectorConfig>,
pub authorize_scram_over_mtls: Option<AuthorizeScramOverMtlsConfig>,
}
const NAME: &str = "KafkaSinkCluster";
#[typetag::serde(name = "KafkaSinkCluster")]
#[async_trait(?Send)]
impl TransformConfig for KafkaSinkClusterConfig {
async fn get_builder(
&self,
transform_context: TransformContextConfig,
) -> Result<Box<dyn TransformBuilder>> {
let tls = self.tls.as_ref().map(TlsConnector::new).transpose()?;
let shotover_nodes: Result<Vec<_>> = self
.shotover_nodes
.iter()
.cloned()
.map(ShotoverNodeConfig::build)
.collect();
let mut shotover_nodes = shotover_nodes?;
// All shotover nodes should have unique broker ids
let mut unique_broker_ids = HashSet::new();
for node in &shotover_nodes {
if !unique_broker_ids.insert(node.broker_id) {
return Err(anyhow::anyhow!(
"Duplicate broker_id found in shotover node {}",
node.address_for_clients
));
}
}
let rack = shotover_nodes
.iter()
.find(|x| x.broker_id.0 == self.local_shotover_broker_id)
.map(|x| x.rack.clone())
.ok_or_else(|| {
anyhow!(
"local_shotover_broker_id {} was missing in shotover_nodes",
self.local_shotover_broker_id
)
})?;
shotover_nodes.sort_by_key(|x| x.broker_id);
let first_contact_points: Result<Vec<_>> = self
.first_contact_points
.iter()
.map(|x| KafkaAddress::from_str(x))
.collect();
Ok(Box::new(KafkaSinkClusterBuilder::new(
transform_context.chain_name,
first_contact_points?,
&self.authorize_scram_over_mtls,
shotover_nodes,
self.local_shotover_broker_id,
rack,
self.connect_timeout_ms,
self.read_timeout,
self.check_shotover_peers_delay_ms,
tls,
)?))
}
fn up_chain_protocol(&self) -> UpChainProtocol {
UpChainProtocol::MustBeOneOf(vec![MessageType::Kafka])
}
fn down_chain_protocol(&self) -> DownChainProtocol {
DownChainProtocol::Terminating
}
}
struct KafkaSinkClusterBuilder {
// contains address and port
first_contact_points: Vec<KafkaAddress>,
shotover_nodes: Vec<ShotoverNode>,
rack: StrBytes,
broker_id: BrokerId,
connect_timeout: Duration,
read_timeout: Option<Duration>,
controller_broker: Arc<AtomicBrokerId>,
group_to_coordinator_broker: Arc<DashMap<GroupId, BrokerId>>,
transaction_to_coordinator_broker: Arc<DashMap<TransactionalId, BrokerId>>,
topic_by_name: Arc<DashMap<TopicName, Topic>>,
topic_by_id: Arc<DashMap<Uuid, Topic>>,
nodes_shared: Arc<RwLock<Vec<KafkaNode>>>,
authorize_scram_over_mtls: Option<AuthorizeScramOverMtlsBuilder>,
tls: Option<TlsConnector>,
out_of_rack_requests: Counter,
}
impl KafkaSinkClusterBuilder {
#[expect(clippy::too_many_arguments)]
pub fn new(
chain_name: String,
first_contact_points: Vec<KafkaAddress>,
authorize_scram_over_mtls: &Option<AuthorizeScramOverMtlsConfig>,
shotover_nodes: Vec<ShotoverNode>,
local_shotover_broker_id: i32,
rack: StrBytes,
connect_timeout_ms: u64,
timeout: Option<u64>,
check_shotover_peers_delay_ms: Option<u64>,
tls: Option<TlsConnector>,
) -> Result<KafkaSinkClusterBuilder> {
let read_timeout = timeout.map(Duration::from_secs);
let connect_timeout = Duration::from_millis(connect_timeout_ms);
let shotover_peers = shotover_nodes
.iter()
.filter(|x| x.broker_id.0 != local_shotover_broker_id)
.cloned()
.collect();
if let Some(check_shotover_peers_delay_ms) = check_shotover_peers_delay_ms {
start_shotover_peers_check(
shotover_peers,
check_shotover_peers_delay_ms,
connect_timeout,
&chain_name,
);
}
Ok(KafkaSinkClusterBuilder {
first_contact_points,
authorize_scram_over_mtls: authorize_scram_over_mtls
.as_ref()
.map(|x| x.get_builder(connect_timeout, read_timeout, &chain_name))
.transpose()?,
shotover_nodes,
broker_id: BrokerId(local_shotover_broker_id),
rack,
connect_timeout,
read_timeout,
controller_broker: Arc::new(AtomicBrokerId::new()),
group_to_coordinator_broker: Arc::new(DashMap::new()),
transaction_to_coordinator_broker: Arc::new(DashMap::new()),
topic_by_name: Arc::new(DashMap::new()),
topic_by_id: Arc::new(DashMap::new()),
nodes_shared: Arc::new(RwLock::new(vec![])),
out_of_rack_requests: counter!("shotover_out_of_rack_requests_count", "chain" => chain_name, "transform" => NAME),
tls,
})
}
}
impl TransformBuilder for KafkaSinkClusterBuilder {
fn build(&self, transform_context: TransformContextBuilder) -> Box<dyn Transform> {
Box::new(KafkaSinkCluster {
connections: Connections::new(self.out_of_rack_requests.clone()),
first_contact_points: self.first_contact_points.clone(),
shotover_nodes: self.shotover_nodes.clone(),
rack: self.rack.clone(),
broker_id: self.broker_id,
nodes: vec![],
nodes_shared: self.nodes_shared.clone(),
controller_broker: self.controller_broker.clone(),
group_to_coordinator_broker: self.group_to_coordinator_broker.clone(),
transaction_to_coordinator_broker: self.transaction_to_coordinator_broker.clone(),
topic_by_name: self.topic_by_name.clone(),
topic_by_id: self.topic_by_id.clone(),
rng: SmallRng::from_rng(&mut rand::rng()),
auth_complete: false,
connection_factory: ConnectionFactory::new(
self.tls.clone(),
self.connect_timeout,
self.read_timeout,
transform_context.force_run_chain,
),
pending_requests: Default::default(),
temp_responses_buffer: Default::default(),
sasl_mechanism: None,
authorize_scram_over_mtls: self.authorize_scram_over_mtls.as_ref().map(|x| x.build()),
refetch_backoff: Duration::from_millis(1),
})
}
fn get_name(&self) -> &'static str {
NAME
}
fn is_terminating(&self) -> bool {
true
}
}
struct AtomicBrokerId(AtomicI64);
impl AtomicBrokerId {
fn new() -> Self {
AtomicBrokerId(i64::MAX.into())
}
fn set(&self, value: BrokerId) {
if value != -1 {
self.0
.store(value.0.into(), std::sync::atomic::Ordering::Relaxed)
}
}
fn clear(&self) {
self.0.store(i64::MAX, std::sync::atomic::Ordering::Relaxed)
}
/// Returns `None` when set has never been called.
/// Otherwise returns `Some` containing the latest set value.
fn get(&self) -> Option<BrokerId> {
match self.0.load(std::sync::atomic::Ordering::Relaxed) {
i64::MAX => None,
other => Some(BrokerId(other as i32)),
}
}
}
pub(crate) struct KafkaSinkCluster {
first_contact_points: Vec<KafkaAddress>,
shotover_nodes: Vec<ShotoverNode>,
rack: StrBytes,
broker_id: BrokerId,
nodes: Vec<KafkaNode>,
nodes_shared: Arc<RwLock<Vec<KafkaNode>>>,
controller_broker: Arc<AtomicBrokerId>,
group_to_coordinator_broker: Arc<DashMap<GroupId, BrokerId>>,
transaction_to_coordinator_broker: Arc<DashMap<TransactionalId, BrokerId>>,
topic_by_name: Arc<DashMap<TopicName, Topic>>,
topic_by_id: Arc<DashMap<Uuid, Topic>>,
rng: SmallRng,
auth_complete: bool,
connection_factory: ConnectionFactory,
/// Maintains the state of each request/response pair.
/// Ordering must be maintained to ensure responses match up with their request.
pending_requests: VecDeque<PendingRequest>,
/// A temporary buffer used when receiving responses, only held onto in order to avoid reallocating.
temp_responses_buffer: Vec<Message>,
sasl_mechanism: Option<String>,
authorize_scram_over_mtls: Option<AuthorizeScramOverMtls>,
connections: Connections,
refetch_backoff: Duration,
}
/// State of a Request/Response is maintained by this enum.
/// The state progresses from Routed -> Sent -> Received
#[derive(Debug)]
enum PendingRequestState {
/// A route has been determined for this request but it has not yet been sent.
Routed { request: Message },
/// The request has been sent to the specified broker and we are now awaiting a response from that broker.
Sent {
/// How many responses must be received before this response is received.
/// When this is 0 the next response from the broker will be for this request.
/// This field must be manually decremented when another response for this broker comes through.
index: usize,
/// Some message types store the request here in case they need to resend it.
request: Option<Message>,
},
/// The broker has returned a Response to this request.
/// Returning this response may be delayed until a response to an earlier request comes back from another broker.
Received {
response: Message,
/// Some message types store the request here in case they need to resend it.
// TODO: if we ever turn the Message into a CoW type we will be able to
// simplify this a lot by just storing the request field once in PendingRequest
request: Option<Message>,
},
}
impl PendingRequestState {
fn routed(request: Message) -> Self {
Self::Routed { request }
}
}
#[derive(Debug, Clone)]
enum PendingRequestTy {
Fetch {
originally_sent_at: Instant,
max_wait_ms: i32,
min_bytes: i32,
},
FindCoordinator(FindCoordinator),
// Covers multiple request types: JoinGroup, DeleteGroups etc.
RoutedToGroup(GroupId),
// Covers multiple request types: InitProducerId, EndTxn etc.
RoutedToTransaction(TransactionalId),
Other,
}
struct PendingRequest {
state: PendingRequestState,
destination: Destination,
/// Type of the request sent
ty: PendingRequestTy,
/// Combine the next N responses into a single response
/// This message should be considered the base message and will retain the shotover Message::id and kafka correlation_id
combine_responses: usize,
}
#[async_trait]
impl Transform for KafkaSinkCluster {
fn get_name(&self) -> &'static str {
NAME
}
async fn transform<'shorter, 'longer: 'shorter>(
&mut self,
chain_state: &'shorter mut ChainState<'longer>,
) -> Result<Messages> {
if chain_state.requests.is_empty() {
// there are no requests, so no point sending any, but we should check for any responses without awaiting
self.recv_responses(&mut chain_state.close_client_connection)
.await
.context("Failed to receive responses (without sending requests)")
} else {
self.update_local_nodes().await;
self.route_requests(std::mem::take(&mut chain_state.requests))
.await
.context("Failed to route requests")?;
self.send_requests()
.await
.context("Failed to send requests")?;
self.recv_responses(&mut chain_state.close_client_connection)
.await
.context("Failed to receive responses")
}
}
}
impl KafkaSinkCluster {
/// Send a request over the control connection and immediately receive the response.
/// Since we always await the response we know for sure that the response will not get mixed up with any other incoming responses.
async fn control_send_receive(&mut self, request: Message) -> Result<Message> {
match self.control_send_receive_inner(request.clone()).await {
Ok(response) => Ok(response),
Err(err) => {
// first retry on the same connection in case it was a timeout
match self
.connections
.handle_connection_error(
&self.connection_factory,
&self.authorize_scram_over_mtls,
&self.sasl_mechanism,
&self.nodes,
Destination::ControlConnection,
err,
)
.await
{
// connection recreated succesfully, retry on the original node
// if the request fails at this point its a bad request.
Ok(()) => self.control_send_receive_inner(request).await,
// connection failed, could be a bad node, retry on all known nodes
Err(err) => {
tracing::warn!("Failed to recreate original control connection {err:?}");
loop {
// remove the old control connection to force control_send_receive_inner to create a new one.
self.connections
.connections
.remove(&Destination::ControlConnection);
match self.control_send_receive_inner(request.clone()).await {
// found a new node that works
Ok(response) => return Ok(response),
// this node also doesnt work, mark as bad and try a new one.
Err(err) => {
if self.nodes.iter().all(|x| !x.is_up()) {
return Err(err.context("Failed to recreate control connection, no more brokers to retry on. Last broker gave error"));
} else {
tracing::warn!(
"Failed to recreate control connection against a new broker {err:?}"
);
// try another node
}
}
}
}
}
}
}
}
}
async fn control_send_receive_inner(&mut self, request: Message) -> Result<Message> {
assert!(
self.auth_complete,
"control_send_receive cannot be called until auth is complete. Otherwise it would collide with the control connection being used for regular routing."
);
let connection = self
.connections
.get_or_open_connection(
&mut self.rng,
&self.connection_factory,
&self.authorize_scram_over_mtls,
&self.sasl_mechanism,
&self.nodes,
&self.first_contact_points,
&self.rack,
Instant::now(),
Destination::ControlConnection,
)
.await
.context("Failed to get control connection")?;
connection.send(vec![request])?;
Ok(connection.recv().await?.remove(0))
}
fn store_topic_names(&self, topics: &mut Vec<TopicName>, topic: TopicName) {
let cache_is_missing_or_outdated = match self.topic_by_name.get(&topic) {
Some(topic) => topic.partitions.iter().any(|partition| {
// refetch the metadata if the metadata believes that a partition is stored at a down node.
// The possible results are:
// * The node is actually up and the partition is there, the node will be marked as up once a request has been succesfully routed to it.
// * The node is actually down and the partition has moved, refetching the metadata will allow us to find the new destination.
// * The node is actually down and the partition has not yet moved, refetching the metadata will have us attempt to route to the down node.
// Shotover will close the connection and the client will retry the request.
self.nodes
.iter()
.find(|node| node.broker_id == *partition.leader_id)
.map(|node| !node.is_up())
.unwrap_or(false)
}),
None => true,
};
if cache_is_missing_or_outdated && !topics.contains(&topic) && !topic.is_empty() {
topics.push(topic);
}
}
fn store_topic_ids(&self, topics: &mut Vec<Uuid>, topic: Uuid) {
let cache_is_missing_or_outdated = match self.topic_by_id.get(&topic) {
Some(topic) => topic.partitions.iter().any(|partition| {
// refetch the metadata if the metadata believes that a partition is stored at a down node.
// The possible results are:
// * The node is actually up and the partition is there, the node will be marked as up once a request has been succesfully routed to it.
// * The node is actually down and the partition has moved, refetching the metadata will allow us to find the new destination.
// * The node is actually down and the partition has not yet moved, refetching the metadata will have us attempt to route to the down node.
// Shotover will close the connection and the client will retry the request.
self.nodes
.iter()
.find(|node| node.broker_id == *partition.leader_id)
.map(|node| !node.is_up())
.unwrap_or(false)
}),
None => true,
};
if cache_is_missing_or_outdated && !topics.contains(&topic) && !topic.is_nil() {
topics.push(topic);
}
}
fn store_group(&self, groups: &mut Vec<GroupId>, group_id: GroupId) {
let cache_is_missing_or_outdated = match self.group_to_coordinator_broker.get(&group_id) {
Some(broker_id) => self
.nodes
.iter()
.find(|node| node.broker_id == *broker_id)
.map(|node| !node.is_up())
.unwrap_or(true),
None => true,
};
if cache_is_missing_or_outdated && !groups.contains(&group_id) {
debug_assert!(group_id.0.as_str() != "");
groups.push(group_id);
}
}
fn store_transaction(
&self,
transactions: &mut Vec<TransactionalId>,
transaction: TransactionalId,
) {
let cache_is_missing_or_outdated =
match self.transaction_to_coordinator_broker.get(&transaction) {
Some(broker_id) => self
.nodes
.iter()
.find(|node| node.broker_id == *broker_id)
.map(|node| !node.is_up())
.unwrap_or(true),
None => true,
};
if cache_is_missing_or_outdated && !transactions.contains(&transaction) {
debug_assert!(transaction.0.as_str() != "");
transactions.push(transaction);
}
}
async fn update_local_nodes(&mut self) {
self.nodes.clone_from(&*self.nodes_shared.read().await);
}
async fn route_requests(&mut self, mut requests: Vec<Message>) -> Result<()> {
if !self.auth_complete {
let mut handshake_request_count = 0;
for request in &mut requests {
match request.frame() {
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::SaslHandshake(SaslHandshakeRequest { mechanism, .. }),
..
})) => {
mechanism.as_str();
self.sasl_mechanism = Some(mechanism.as_str().to_owned());
self.connection_factory.add_auth_request(request.clone());
handshake_request_count += 1;
}
Some(Frame::Kafka(KafkaFrame::Request {
body:
RequestBody::SaslAuthenticate(SaslAuthenticateRequest {
auth_bytes, ..
}),
..
})) => {
if let Some(scram_over_mtls) = &mut self.authorize_scram_over_mtls {
if let Some(username) = get_username_from_scram_request(auth_bytes) {
scram_over_mtls.set_username(username).await?;
}
}
self.connection_factory.add_auth_request(request.clone());
handshake_request_count += 1;
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::ApiVersions(_),
..
})) => {
handshake_request_count += 1;
}
_ => {
// The client is no longer performing authentication, so consider auth completed
if let Some(scram_over_mtls) = &self.authorize_scram_over_mtls {
// When performing SCRAM over mTLS, we need this security check to ensure that the
// client cannot access delegation tokens that it has not successfully authenticated for.
//
// If the client were to send a request directly after the SCRAM requests,
// without waiting for responses to those scram requests first,
// this error would be triggered even if the SCRAM requests were successful.
// However that would be a violation of the SCRAM protocol as the client is supposed to check
// the server's signature contained in the server's final message in order to authenticate the server.
// So I dont think this problem is worth solving.
if !matches!(
scram_over_mtls.original_scram_state,
OriginalScramState::AuthSuccess
) {
return Err(anyhow!(
"Client attempted to send requests before a successful auth was completed or after an unsuccessful auth"
));
}
}
self.auth_complete = true;
break;
}
}
}
// route all handshake messages
for _ in 0..handshake_request_count {
let request = requests.remove(0);
self.route_to_control_connection(request);
}
if requests.is_empty() {
// all messages received in this batch are handshake messages,
// so dont continue with regular message handling
return Ok(());
} else {
// the later messages in this batch are not handshake messages,
// so continue onto the regular message handling
}
}
let mut topic_names = vec![];
let mut topic_ids = vec![];
let mut groups = vec![];
let mut transactions = vec![];
for request in &mut requests {
match request.frame() {
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::Produce(produce),
..
})) => {
for topic_data in &produce.topic_data {
self.store_topic_names(&mut topic_names, topic_data.name.clone());
}
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::ListOffsets(list_offsets),
..
})) => {
for topic in &list_offsets.topics {
self.store_topic_names(&mut topic_names, topic.name.clone());
}
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::OffsetForLeaderEpoch(body),
..
})) => {
for topic in &body.topics {
self.store_topic_names(&mut topic_names, topic.topic.clone());
}
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::DescribeProducers(body),
..
})) => {
for topic in &body.topics {
self.store_topic_names(&mut topic_names, topic.name.clone());
}
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::DeleteRecords(body),
..
})) => {
for topic in &body.topics {
self.store_topic_names(&mut topic_names, topic.name.clone());
}
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::Fetch(fetch),
..
})) => {
for topic in &fetch.topics {
self.store_topic_names(&mut topic_names, topic.topic.clone());
self.store_topic_ids(&mut topic_ids, topic.topic_id);
}
fetch.session_id = 0;
fetch.session_epoch = -1;
request.invalidate_cache();
}
Some(Frame::Kafka(KafkaFrame::Request {
body:
RequestBody::Heartbeat(HeartbeatRequest { group_id, .. })
| RequestBody::ConsumerGroupHeartbeat(ConsumerGroupHeartbeatRequest {
group_id,
..
})
| RequestBody::SyncGroup(SyncGroupRequest { group_id, .. })
| RequestBody::JoinGroup(JoinGroupRequest { group_id, .. })
| RequestBody::LeaveGroup(LeaveGroupRequest { group_id, .. })
| RequestBody::TxnOffsetCommit(TxnOffsetCommitRequest { group_id, .. }),
..
})) => {
self.store_group(&mut groups, group_id.clone());
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::ConsumerGroupDescribe(describe),
..
})) => {
for group_id in &describe.group_ids {
self.store_group(&mut groups, group_id.clone());
}
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::DeleteGroups(delete_groups),
..
})) => {
for group_id in &delete_groups.groups_names {
self.store_group(&mut groups, group_id.clone());
}
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::OffsetDelete(offset_delete),
..
})) => {
self.store_group(&mut groups, offset_delete.group_id.clone());
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::DescribeGroups(offset_delete),
..
})) => {
for group_id in &offset_delete.groups {
self.store_group(&mut groups, group_id.clone());
}
}
Some(Frame::Kafka(KafkaFrame::Request {
body:
RequestBody::InitProducerId(InitProducerIdRequest {
transactional_id: Some(transactional_id),
..
})
| RequestBody::EndTxn(EndTxnRequest {
transactional_id, ..
})
| RequestBody::AddOffsetsToTxn(AddOffsetsToTxnRequest {
transactional_id, ..
}),
..
})) => {
self.store_transaction(&mut transactions, transactional_id.clone());
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::DescribeTransactions(describe_transaction),
..
})) => {
for transactional_id in &describe_transaction.transactional_ids {
self.store_transaction(&mut transactions, transactional_id.clone());
}
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::AddPartitionsToTxn(add_partitions_to_txn_request),
header,
})) => {
if header.request_api_version <= 3 {
self.store_transaction(
&mut transactions,
add_partitions_to_txn_request
.v3_and_below_transactional_id
.clone(),
);
} else {
for transaction in &add_partitions_to_txn_request.transactions {
self.store_transaction(
&mut transactions,
transaction.transactional_id.clone(),
);
}
}
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::OffsetFetch(offset_fetch),
header,
})) => {
if header.request_api_version <= 7 {
self.store_group(&mut groups, offset_fetch.group_id.clone());
} else {
for group in &offset_fetch.groups {
self.store_group(&mut groups, group.group_id.clone());
}
}
}
_ => {}
}
}
for group in groups {
match self
.find_coordinator(CoordinatorKey::Group(group.clone()))
.await
{
Ok(node) => {
tracing::debug!(
"Storing group_to_coordinator_broker metadata, group {group:?} -> broker {:?}",
node.broker_id
);
self.group_to_coordinator_broker
.insert(group, node.broker_id);
self.add_node_if_new(node).await;
}
Err(FindCoordinatorError::CoordinatorNotAvailable) => {
// We cant find the coordinator so do nothing so that the request will be routed to a random node:
// * If it happens to be the coordinator all is well
// * If its not the coordinator then it will return a NOT_COORDINATOR message to
// the client prompting it to retry the whole process again.
}
Err(FindCoordinatorError::Unrecoverable(err)) => Err(err)?,
}
}
for transaction in transactions {
match self
.find_coordinator(CoordinatorKey::Transaction(transaction.clone()))
.await
{
Ok(node) => {
tracing::debug!(
"Storing transaction_to_coordinator_broker metadata, transaction {transaction:?} -> broker {:?}",
node.broker_id
);
self.transaction_to_coordinator_broker
.insert(transaction, node.broker_id);
self.add_node_if_new(node).await;
}
Err(FindCoordinatorError::CoordinatorNotAvailable) => {
// We cant find the coordinator so do nothing so that the request will be routed to a random node:
// * If it happens to be the coordinator all is well
// * If its not the coordinator then it will return a NOT_COORDINATOR message to
// the client prompting it to retry the whole process again.
}
Err(FindCoordinatorError::Unrecoverable(err)) => Err(err)?,
}
}
// request and process metadata if we are missing topics or the controller broker id
if !topic_names.is_empty()
|| !topic_ids.is_empty()
|| self.controller_broker.get().is_none()
|| self.nodes.is_empty()
{
let mut metadata = self
.get_metadata_of_topics_with_retry(topic_names, topic_ids)
.await?;
match metadata.frame() {
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::Metadata(metadata),
..
})) => {
for topic in &metadata.topics {
match ResponseError::try_from_code(topic.error_code) {
Some(ResponseError::UnknownTopicOrPartition) => {
// We need to look up all topics sent to us by the client
// but the client may request a topic that doesnt exist.
}
Some(err) => {
// Some other kind of error, better to terminate the connection
return Err(anyhow!(
"Kafka responded to Metadata request with error {err:?}"
));
}
None => {}
}
}
self.process_metadata_response(metadata).await
}
other => {
return Err(anyhow!(
"Unexpected response returned to metadata request {other:?}"
));
}
}
}
for mut request in requests {
// This routing is documented in transforms.md so make sure to update that when making changes here.
match request.frame() {
// split and route to partition leader
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::Produce(_),
..
})) => self.split_and_route_request::<ProduceRequestSplitAndRouter>(request)?,
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::Fetch(_),
..
})) => self.route_fetch_request(request)?,
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::ListOffsets(_),
..
})) => self.split_and_route_request::<ListOffsetsRequestSplitAndRouter>(request)?,
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::OffsetForLeaderEpoch(_),
..
})) => self.split_and_route_request::<OffsetForLeaderEpochRequestSplitAndRouter>(
request,
)?,
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::DeleteRecords(_),
..
})) => {
self.split_and_route_request::<DeleteRecordsRequestSplitAndRouter>(request)?
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::DescribeProducers(_),
..
})) => {
self.split_and_route_request::<DescribeProducersRequestSplitAndRouter>(request)?
}
// route to group coordinator
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::Heartbeat(heartbeat),
..
})) => {
let group_id = heartbeat.group_id.clone();
self.route_to_group_coordinator(request, group_id);
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::ConsumerGroupHeartbeat(heartbeat),
..
})) => {
let group_id = heartbeat.group_id.clone();
self.route_to_group_coordinator(request, group_id);
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::SyncGroup(sync_group),
..
})) => {
let group_id = sync_group.group_id.clone();
self.route_to_group_coordinator(request, group_id);
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::OffsetFetch(offset_fetch),
header,
})) => {
if header.request_api_version <= 7 {
let group_id = offset_fetch.group_id.clone();
self.route_to_group_coordinator(request, group_id);
} else {
self.split_and_route_request::<OffsetFetchSplitAndRouter>(request)?;
};
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::OffsetCommit(offset_commit),
..
})) => {
let group_id = offset_commit.group_id.clone();
self.route_to_group_coordinator(request, group_id);
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::JoinGroup(join_group),
..
})) => {
let group_id = join_group.group_id.clone();
self.route_to_group_coordinator(request, group_id);
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::LeaveGroup(leave_group),
..
})) => {
let group_id = leave_group.group_id.clone();
self.route_to_group_coordinator(request, group_id);
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::DeleteGroups(_),
..
})) => {
self.split_and_route_request::<DeleteGroupsSplitAndRouter>(request)?;
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::DescribeGroups(_),
..
})) => {
self.split_and_route_request::<DescribeGroupsSplitAndRouter>(request)?;
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::ConsumerGroupDescribe(_),
..
})) => {
self.split_and_route_request::<ConsumerGroupDescribeSplitAndRouter>(request)?;
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::OffsetDelete(offset_delete),
..
})) => {
let group_id = offset_delete.group_id.clone();
self.route_to_group_coordinator(request, group_id);
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::TxnOffsetCommit(txn_offset_commit),
..
})) => {
let group_id = txn_offset_commit.group_id.clone();
// Despite being a transaction request this request is routed by group_id
self.route_to_group_coordinator(request, group_id);
}
// route to transaction coordinator
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::EndTxn(end_txn),
..
})) => {
let transaction_id = end_txn.transactional_id.clone();
self.route_to_transaction_coordinator(request, transaction_id);
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::InitProducerId(init_producer_id),
..
})) => {
if let Some(transaction_id) = init_producer_id.transactional_id.clone() {
self.route_to_transaction_coordinator(request, transaction_id);
} else {
self.route_to_random_broker(request);
}
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::AddPartitionsToTxn(_),
..
})) => self.route_add_partitions_to_txn(request)?,
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::AddOffsetsToTxn(add_offsets_to_txn),
..
})) => {
let transaction_id = add_offsets_to_txn.transactional_id.clone();
self.route_to_transaction_coordinator(request, transaction_id);
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::FindCoordinator(_),
..
})) => {
self.route_find_coordinator(request);
}
Some(Frame::Kafka(KafkaFrame::Request {
body:
// It was determined that these message types are only sent between brokers by:
// * This paragraph https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=225153708#KIP866ZooKeepertoKRaftMigration-ControllerRPCs
// * This field containing only "zkBroker" https://github.com/apache/kafka/blob/e3f953483cb480631bf041698770b47ddb82796f/clients/src/main/resources/common/message/LeaderAndIsrRequest.json#L19
RequestBody::LeaderAndIsr(_)
| RequestBody::StopReplica(_)
| RequestBody::UpdateMetadata(_)
// It was determined that this message type is only sent between brokers by:
// * https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=177049344#KIP730:ProducerIDgenerationinKRaftmode-PublicInterfaces
| RequestBody::AllocateProducerIds(_)
// It was determined that this message type is only sent between brokers because:
// * it is not exposed via the Admin client.
// * This KIP discusses the message type and makes no mention of clients using it https://cwiki.apache.org/confluence/display/KAFKA/KIP-704:+Send+a+hint+to+the+partition+leader+to+recover+the+partition
| RequestBody::AlterPartition(_),
header:
RequestHeader {
request_api_key, ..
},
})) => {
return Err(anyhow!(
r#"Client sent request of type {request_api_key}.
This request is used only for communication between brokers and shotover does not support proxying between brokers.
The connection to the client has been closed."#
));
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::ControlledShutdown(_),
..
})) => {
// This request type specifies a broker that should shutdown.
// Since the broker ID's known to the client represent shotover nodes,
// the only reasonable interpretation of this request would be for the shotover with that broker id to shutdown.
// But its not appropriate for shotover to shutdown:
// * It cant hand off topics to other nodes like kafka does as part of its controlled shutdown process.
// * It doesnt have any ACL's, any user could send this request regardless of authorization.
//
// So we abort the connection and log an error to signal to the client that the request failed.
// It might be better to instead construct a response containing an error code,
// but its not worth the complexity for such a rare request type.
return Err(anyhow!(
"Client sent ControlledShutdown request.
Shotover cannot handle this request as it is not appropriate for shotover to shutdown.
The connection to the client has been closed."
));
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::AlterReplicaLogDirs(_),
..
})) => {
return Err(anyhow!(
"Client sent AlterReplicaLogDirs request.
Shotover has not implemented support for this message type.
It could theoretically be supported by performing the inverse of DescribeLogDirs.
i.e. Define a custom path format like `actual-kafka-broker-id3:/original/log/dir/path` and then parse the broker id and send the original path to that broker.
But it is expected that if you are using shotover as a proxy you do not care about the internal state of the kafka cluster enough to use AlterReplicaLogDirs."
));
}
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::UnregisterBroker(_),
..
})) => {
// This message types is a replacement for ControlledShutdown so the same reasoning applies.
return Err(anyhow!(
"Client sent UnregisterBroker request.
Shotover cannot handle this request as it is not appropriate for shotover to shutdown.
The connection to the client has been closed."
));
}
// route to controller broker
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::CreateTopics(_),
..
})) => self.route_to_controller(request),
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::ElectLeaders(_),
..
})) => self.route_to_controller(request),
// route to all nodes
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::ListGroups(_),
..
})) => self.split_and_route_request::<ListGroupsSplitAndRouter>(request)?,
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::DescribeTransactions(_),
..
})) => self.split_and_route_request::<DescribeTransactionsSplitAndRouter>(request)?,
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::ListTransactions(_),
..
})) => self.split_and_route_request::<ListTransactionsSplitAndRouter>(request)?,
Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::DescribeLogDirs(_),
..
})) => self.split_and_route_request::<DescribeLogDirsSplitAndRouter>(request)?,
// route to random broker
Some(Frame::Kafka(KafkaFrame::Request {
body:
RequestBody::Metadata(_)
| RequestBody::DescribeCluster(_)
| RequestBody::DescribeConfigs(_)
| RequestBody::AlterConfigs(_)
| RequestBody::CreatePartitions(_)
| RequestBody::DeleteTopics(_)
| RequestBody::CreateAcls(_)
| RequestBody::ApiVersions(_)
| RequestBody::AlterPartitionReassignments(_)
| RequestBody::ListPartitionReassignments(_),
..
})) => self.route_to_random_broker(request),
// error handling
Some(Frame::Kafka(KafkaFrame::Request { header, .. })) => {
let request_type =
format!("{:?}", ApiKey::try_from(header.request_api_key).unwrap());
tracing::warn!("Routing for request of type {request_type:?} has not been implemented yet.");
self.route_to_random_broker(request)
}
Some(_) => unreachable!("Must be a kafka request"),
None => {
tracing::warn!("Unable to parse request, routing to a random node");
self.route_to_random_broker(request)
}
}
}
Ok(())
}
fn route_to_random_broker(&mut self, request: Message) {
let destination = random_broker_id(&self.nodes, &mut self.rng, &self.rack);
tracing::debug!("Routing request to random broker {}", destination.0);
self.pending_requests.push_back(PendingRequest {
state: PendingRequestState::routed(request),
destination: Destination::Id(destination),
ty: PendingRequestTy::Other,
combine_responses: 1,
});
}
fn split_and_route_request<T: RequestSplitAndRouter>(
&mut self,
mut request: Message,
) -> Result<()> {
let request_frame = T::get_request_frame(&mut request);
let routing = T::split_by_destination(self, request_frame);
if routing.is_empty() {
// Produce contains no topics, so we can just pick a random destination.
// The request is unchanged so we can just send as is.
let destination = random_broker_id(&self.nodes, &mut self.rng, &self.rack);
self.pending_requests.push_back(PendingRequest {
state: PendingRequestState::routed(request),
destination: Destination::Id(destination),
ty: PendingRequestTy::Other,
combine_responses: 1,
});
tracing::debug!(
"Routing request to random broker {} due to being empty",
destination.0
);
} else if routing.len() == 1 {
// Only 1 destination,
// so we can just reconstruct the original request as is,
// act like this never happened 😎,
// we dont even need to invalidate the request's cache.
let (destination, topic_data) = routing.into_iter().next().unwrap();
let destination = if destination == -1 {
random_broker_id(&self.nodes, &mut self.rng, &self.rack)
} else {
destination
};
T::reassemble(request_frame, topic_data);
self.pending_requests.push_back(PendingRequest {
state: PendingRequestState::routed(request),
destination: Destination::Id(destination),
ty: PendingRequestTy::Other,
combine_responses: 1,
});
tracing::debug!("Routing request to single broker {destination:?}");
} else {
// The request has been split so it may be delivered to multiple destinations.
// We must generate a unique request for each destination.
let combine_responses = routing.len();
request.invalidate_cache();
for (i, (destination, topic_data)) in routing.into_iter().enumerate() {
let destination = if destination == -1 {
random_broker_id(&self.nodes, &mut self.rng, &self.rack)
} else {
destination
};
let mut request = if i == 0 {
// First request acts as base and retains message id
request.clone()
} else {
request.clone_with_new_id()
};
let request_frame = T::get_request_frame(&mut request);
T::reassemble(request_frame, topic_data);
self.pending_requests.push_back(PendingRequest {
state: PendingRequestState::routed(request),
destination: Destination::Id(destination),
ty: PendingRequestTy::Other,
combine_responses,
});
}
tracing::debug!("Routing request to multiple brokers");
}
Ok(())
}
/// This method removes all topics from the produce request and returns them split up by their destination
/// If any topics are unroutable they will have their destination BrokerId set to -1
fn split_produce_request_by_destination(
&mut self,
produce: &mut ProduceRequest,
) -> HashMap<BrokerId, HashMap<TopicName, TopicProduceData>> {
let mut result: HashMap<BrokerId, HashMap<TopicName, TopicProduceData>> =
Default::default();
for mut topic in produce.topic_data.drain(..) {
let name = &topic.name;
let topic_meta = self.topic_by_name.get(name);
if let Some(topic_meta) = topic_meta {
for partition in std::mem::take(&mut topic.partition_data) {
let partition_index = partition.index as usize;
let destination = if let Some(partition) =
topic_meta.partitions.get(partition_index)
{
if partition.leader_id == -1 {
tracing::warn!(
"leader_id is unknown for topic {name:?} at partition index {partition_index}"
);
}
partition.leader_id
} else {
let partition_len = topic_meta.partitions.len();
tracing::warn!(
"no known partition replica for {name:?} at partition index {partition_index} out of {partition_len} partitions, routing request to a random broker so that a NOT_LEADER_OR_FOLLOWER or similar error is returned to the client"
);
BrokerId(-1)
};
tracing::debug!(
"Routing produce request portion of partition {partition_index} in topic {} to broker {}",
name.0,
destination.0
);
// Get the topics already routed to this destination
let routed_topics = result.entry(destination).or_default();
if let Some(routed_topic) = routed_topics.get_mut(name) {
// we have already routed this topic to this broker, add another partition
routed_topic.partition_data.push(partition);
} else {
// we have not yet routed this topic to this broker, add the first partition
// Clone the original topic value, to ensure we carry over any `unknown_tagged_fields` values.
// The partition_data is empty at this point due to the previous `std::mem::take`
let mut topic = topic.clone();
topic.partition_data.push(partition);
routed_topics.insert(name.clone(), topic);
}
}
} else {
tracing::debug!(
r#"no known partition leader for {name:?}
routing request to a random broker so that:
* if auto topic creation is enabled, auto topic creation will occur
* if auto topic creation is disabled a NOT_LEADER_OR_FOLLOWER is returned to the client"#
);
let destination = BrokerId(-1);
let dest_topics = result.entry(destination).or_default();
dest_topics.insert(name.clone(), topic);
}
}
result
}
/// This method removes all topics from the fetch request and returns them split up by their destination
/// If any topics are unroutable they will have their BrokerId set to -1
fn split_fetch_request_by_destination(
&mut self,
fetch: &mut FetchRequest,
) -> HashMap<BrokerId, Vec<FetchTopic>> {
let mut result: HashMap<BrokerId, Vec<FetchTopic>> = Default::default();
for mut topic in fetch.topics.drain(..) {
// This way of constructing topic_meta is kind of crazy, but it works around borrow checker limitations
// Old clients only specify the topic name and some newer clients only specify the topic id.
// So we need to check the id first and then fallback to the name.
let topic_by_id = self.topic_by_id.get(&topic.topic_id);
let topic_by_name;
let mut topic_meta = topic_by_id.as_deref();
if topic_meta.is_none() {
topic_by_name = self.topic_by_name.get(&topic.topic);
topic_meta = topic_by_name.as_deref();
}
let format_topic_name = FormatTopicName(&topic.topic, &topic.topic_id);
if let Some(topic_meta) = topic_meta {
for partition in std::mem::take(&mut topic.partitions) {
let partition_index = partition.partition as usize;
let destination = if let Some(partition) =
topic_meta.partitions.get(partition_index)
{
// While technically kafka has some support for fetching from replicas, its quite weird.
// See https://cwiki.apache.org/confluence/display/KAFKA/KIP-392%3A+Allow+consumers+to+fetch+from+closest+replica
// We should never route to replica_nodes from the metadata response ourselves.
// Instead, when its available, we can make use of preferred_read_replica field in the fetch response as an optimization.
// However its always correct to route to the partition.leader_id which is what we do here.
if partition.leader_id == -1 {
tracing::warn!(
"leader_id is unknown for {format_topic_name} at partition index {partition_index}"
);
}
partition.leader_id
} else {
let partition_len = topic_meta.partitions.len();
tracing::warn!(
"no known partition replica for {format_topic_name} at partition index {partition_index} out of {partition_len} partitions, routing request to a random broker so that a NOT_LEADER_OR_FOLLOWER or similar error is returned to the client"
);
BrokerId(-1)
};
tracing::debug!(
"Routing fetch request portion of partition {partition_index} in {format_topic_name} to broker {}",
destination.0
);
let dest_topics = result.entry(destination).or_default();
if let Some(dest_topic) = dest_topics
.iter_mut()
.find(|x| x.topic_id == topic.topic_id && x.topic == topic.topic)
{
dest_topic.partitions.push(partition);
} else {
let mut topic = topic.clone();
topic.partitions.push(partition);
dest_topics.push(topic);
}
}
} else {
tracing::warn!(
"no known partition replica for {format_topic_name}, routing request to a random broker so that a NOT_LEADER_OR_FOLLOWER or similar error is returned to the client"
);
let destination = BrokerId(-1);
let dest_topics = result.entry(destination).or_default();
dest_topics.push(topic);
}
}
result
}
fn route_fetch_request(&mut self, mut request: Message) -> Result<()> {
if let Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::Fetch(fetch),
..
})) = request.frame()
{
let routing = self.split_fetch_request_by_destination(fetch);
if routing.is_empty() {
// Fetch contains no topics, so we can just pick a random destination.
// The message is unchanged so we can just send as is.
let destination = random_broker_id(&self.nodes, &mut self.rng, &self.rack);
self.pending_requests.push_back(PendingRequest {
state: PendingRequestState::routed(request),
destination: Destination::Id(destination),
// we dont need special handling for fetch, so just use Other
ty: PendingRequestTy::Other,
combine_responses: 1,
});
tracing::debug!(
"Routing fetch request to random broker {} due to being empty",
destination.0
);
} else if routing.len() == 1 {
// Only 1 destination,
// so we can just reconstruct the original message as is,
// act like this never happened 😎,
// we dont even need to invalidate the message's cache.
let (destination, topics) = routing.into_iter().next().unwrap();
let destination = if destination == -1 {
random_broker_id(&self.nodes, &mut self.rng, &self.rack)
} else {
destination
};
fetch.topics = topics;
self.pending_requests.push_back(PendingRequest {
state: PendingRequestState::routed(request),
destination: Destination::Id(destination),
// we dont need special handling for fetch, so just use Other
ty: PendingRequestTy::Other,
combine_responses: 1,
});
tracing::debug!("Routing fetch request to single broker {}", destination.0);
} else {
// Individual sub requests could delay the whole fetch request by up to max_wait_ms.
// So we need to rewrite max_wait_ms and min_bytes to ensure that the broker responds immediately.
// We then perform retries when receiving the response to uphold the original values
let max_wait_ms = fetch.max_wait_ms;
let min_bytes = fetch.min_bytes;
fetch.max_wait_ms = 1;
fetch.min_bytes = 1;
// The message has been split so it may be delivered to multiple destinations.
// We must generate a unique message for each destination.
let combine_responses = routing.len();
request.invalidate_cache();
for (i, (destination, topics)) in routing.into_iter().enumerate() {
let destination = if destination == -1 {
random_broker_id(&self.nodes, &mut self.rng, &self.rack)
} else {
destination
};
let mut request = if i == 0 {
// First message acts as base and retains message id
request.clone()
} else {
request.clone_with_new_id()
};
if let Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::Fetch(fetch),
..
})) = request.frame()
{
fetch.topics = topics;
}
self.pending_requests.push_back(PendingRequest {
state: PendingRequestState::routed(request),
destination: Destination::Id(destination),
ty: PendingRequestTy::Fetch {
originally_sent_at: Instant::now(),
max_wait_ms,
min_bytes,
},
combine_responses,
});
}
tracing::debug!("Routing fetch request to multiple brokers");
}
}
Ok(())
}
/// This method removes all topics from the DescribeProducers request and returns them split up by their destination.
/// If any topics are unroutable they will have their BrokerId set to -1
fn split_describe_producers_request_by_destination(
&mut self,
body: &mut DescribeProducersRequest,
) -> HashMap<BrokerId, Vec<TopicRequest>> {
let mut result: HashMap<BrokerId, Vec<TopicRequest>> = Default::default();
for mut topic in body.topics.drain(..) {
let topic_name = &topic.name;
if let Some(topic_meta) = self.topic_by_name.get(topic_name) {
for partition_index in std::mem::take(&mut topic.partition_indexes) {
let destination = if let Some(partition) =
topic_meta.partitions.get(partition_index as usize)
{
if partition.leader_id == -1 {
tracing::warn!(
"leader_id is unknown for {topic_name:?} at partition index {partition_index}",
);
}
partition.leader_id
} else {
let partition_len = topic_meta.partitions.len();
tracing::warn!(
"no known partition for {topic_name:?} at partition index {partition_index} out of {partition_len} partitions, routing request to a random broker so that a NOT_LEADER_OR_FOLLOWER or similar error is returned to the client"
);
BrokerId(-1)
};
tracing::debug!(
"Routing DescribeProducers request portion of partition {partition_index} in {topic_name:?} to broker {}",
destination.0
);
let dest_topics = result.entry(destination).or_default();
if let Some(dest_topic) = dest_topics.iter_mut().find(|x| x.name == topic.name)
{
dest_topic.partition_indexes.push(partition_index);
} else {
let mut topic = topic.clone();
topic.partition_indexes.push(partition_index);
dest_topics.push(topic);
}
}
} else {
tracing::warn!(
"no known partition replica for {topic_name:?}, routing request to a random broker so that a NOT_LEADER_OR_FOLLOWER or similar error is returned to the client"
);
let destination = BrokerId(-1);
let dest_topics = result.entry(destination).or_default();
dest_topics.push(topic);
}
}
result
}
/// This method removes all topics from the list offsets request and returns them split up by their destination
/// If any topics are unroutable they will have their BrokerId set to -1
fn split_list_offsets_request_by_destination(
&mut self,
list_offsets: &mut ListOffsetsRequest,
) -> HashMap<BrokerId, Vec<ListOffsetsTopic>> {
let mut result: HashMap<BrokerId, Vec<ListOffsetsTopic>> = Default::default();
for mut topic in list_offsets.topics.drain(..) {
let topic_name = &topic.name;
if let Some(topic_meta) = self.topic_by_name.get(topic_name) {
for partition in std::mem::take(&mut topic.partitions) {
let partition_index = partition.partition_index as usize;
let destination = if let Some(partition) =
topic_meta.partitions.get(partition_index)
{
if partition.leader_id == -1 {
tracing::warn!(
"leader_id is unknown for {topic_name:?} at partition index {partition_index}",
);
}
partition.leader_id
} else {
let partition_len = topic_meta.partitions.len();
tracing::warn!(
"no known partition for {topic_name:?} at partition index {partition_index} out of {partition_len} partitions, routing request to a random broker so that a NOT_LEADER_OR_FOLLOWER or similar error is returned to the client"
);
BrokerId(-1)
};
tracing::debug!(
"Routing list_offsets request portion of partition {partition_index} in {topic_name:?} to broker {}",
destination.0
);
let dest_topics = result.entry(destination).or_default();
if let Some(dest_topic) = dest_topics.iter_mut().find(|x| x.name == topic.name)
{
dest_topic.partitions.push(partition);
} else {
let mut topic = topic.clone();
topic.partitions.push(partition);
dest_topics.push(topic);
}
}
} else {
tracing::warn!(
"no known partition replica for {topic_name:?}, routing request to a random broker so that a NOT_LEADER_OR_FOLLOWER or similar error is returned to the client"
);
let destination = BrokerId(-1);
let dest_topics = result.entry(destination).or_default();
dest_topics.push(topic);
}
}
result
}
/// This method removes all group ids from the DeleteGroups request and returns them split up by their destination.
/// If any groups ids are unroutable they will have their BrokerId set to -1
fn split_delete_groups_request_by_destination(
&mut self,
body: &mut DeleteGroupsRequest,
) -> HashMap<BrokerId, Vec<GroupId>> {
let mut result: HashMap<BrokerId, Vec<GroupId>> = Default::default();
for group_id in body.groups_names.drain(..) {
if let Some(destination) = self.group_to_coordinator_broker.get(&group_id) {
let dest_groups = result.entry(*destination).or_default();
dest_groups.push(group_id);
} else {
tracing::warn!(
"no known coordinator for group {group_id:?}, routing request to a random broker so that a NOT_COORDINATOR or similar error is returned to the client"
);
let destination = BrokerId(-1);
let dest_groups = result.entry(destination).or_default();
dest_groups.push(group_id);
}
}
result
}
/// This method removes all transactions from the DescribeTransactions request and returns them split up by their destination.
/// If any transactions are unroutable they will have their BrokerId set to -1
fn split_describe_transactions_request_by_destination(
&mut self,
body: &mut DescribeTransactionsRequest,
) -> HashMap<BrokerId, Vec<TransactionalId>> {
let mut result: HashMap<BrokerId, Vec<TransactionalId>> = Default::default();
for transaction in body.transactional_ids.drain(..) {
if let Some(destination) = self.transaction_to_coordinator_broker.get(&transaction) {
let dest_transactions = result.entry(*destination).or_default();
dest_transactions.push(transaction);
} else {
tracing::warn!(
"no known coordinator for transactions {transaction:?}, routing request to a random broker so that a NOT_COORDINATOR or similar error is returned to the client"
);
let destination = BrokerId(-1);
let dest_transactions = result.entry(destination).or_default();
dest_transactions.push(transaction);
}
}
result
}
/// Route broadcasted requests to all brokers split across all shotover nodes.
/// That is, each shotover node in a rack will deterministically be assigned a portion of the rack to route the request to.
/// If a shotover node is the only node in its rack it will route to all kafka brokers in the rack.
/// When combined with a client that is routing this request to all shotover nodes, the request will reach each kafka broker exactly once.
///
/// The logic in this function relies on `self.shotover_nodes` and `self.nodes` being sorted by broker id.
fn split_request_by_routing_to_all_brokers(&mut self) -> HashMap<BrokerId, ()> {
// Will always be at least 1, since the shotover this code is running in will always be included.
let shotovers_in_rack = self
.shotover_nodes
.iter()
.filter(|node| node.is_up() && node.rack == self.rack)
.count();
// The offset of this shotover node within the rack
// will be 0 <= shotover_offsets_within_rack < shotovers_in_rack
// The purpose is to create a deterministic way of splitting up broadcasted requests across shotover nodes.
let local_shotover_index_in_rack = self
.shotover_nodes
.iter()
.filter(|node| node.is_up() && node.rack == self.rack)
// start enumerating AFTER filtering down to brokers in the rack
.enumerate()
.find(|node| node.1.broker_id == self.broker_id)
.unwrap()
.0;
self.nodes
.iter()
.filter(|node| {
node.is_up()
&& node
.rack
.as_ref()
.map(|rack| rack == &self.rack)
// If the cluster is not using racks, include all brokers in the list.
.unwrap_or(true)
})
// start enumerating AFTER filtering down to brokers in the rack
.enumerate()
// assign each shotover node in the rack a subset of the brokers in the rack
.filter(|(i, _)| i % shotovers_in_rack == local_shotover_index_in_rack)
.map(|(_, broker)| (broker.broker_id, ()))
.collect()
}
/// This method removes all groups from the OffsetFetch request and returns them split up by their destination.
/// If any groups are unroutable they will have their BrokerId set to -1
fn split_offset_fetch_request_by_destination(
&mut self,
body: &mut OffsetFetchRequest,
) -> HashMap<BrokerId, Vec<OffsetFetchRequestGroup>> {
let mut result: HashMap<BrokerId, Vec<OffsetFetchRequestGroup>> = Default::default();
for group in body.groups.drain(..) {
if let Some(destination) = self.group_to_coordinator_broker.get(&group.group_id) {
let dest_groups = result.entry(*destination).or_default();
dest_groups.push(group);
} else {
tracing::warn!(
"no known coordinator for group {:?}, routing request to a random broker so that a NOT_COORDINATOR or similar error is returned to the client",
group.group_id
);
let destination = BrokerId(-1);
let dest_groups = result.entry(destination).or_default();
dest_groups.push(group);
}
}
result
}
/// This method removes all groups from the DescribeGroups request and returns them split up by their destination.
/// If any groups are unroutable they will have their BrokerId set to -1
fn split_describe_groups_request_by_destination(
&mut self,
body: &mut DescribeGroupsRequest,
) -> HashMap<BrokerId, Vec<GroupId>> {
let mut result: HashMap<BrokerId, Vec<GroupId>> = Default::default();
for group in body.groups.drain(..) {
if let Some(destination) = self.group_to_coordinator_broker.get(&group) {
let dest_groups = result.entry(*destination).or_default();
dest_groups.push(group);
} else {
tracing::warn!(
"no known coordinator for group {group:?}, routing request to a random broker so that a NOT_COORDINATOR or similar error is returned to the client"
);
let destination = BrokerId(-1);
let dest_groups = result.entry(destination).or_default();
dest_groups.push(group);
}
}
result
}
/// This method removes all groups from the ConsumerGroupDescribe request and returns them split up by their destination.
/// If any groups are unroutable they will have their BrokerId set to -1
fn split_consumer_group_describe_request_by_destination(
&mut self,
body: &mut ConsumerGroupDescribeRequest,
) -> HashMap<BrokerId, Vec<GroupId>> {
let mut result: HashMap<BrokerId, Vec<GroupId>> = Default::default();
for group in body.group_ids.drain(..) {
if let Some(destination) = self.group_to_coordinator_broker.get(&group) {
let dest_groups = result.entry(*destination).or_default();
dest_groups.push(group);
} else {
tracing::warn!(
"no known coordinator for group {group:?}, routing request to a random broker so that a NOT_COORDINATOR or similar error is returned to the client"
);
let destination = BrokerId(-1);
let dest_groups = result.entry(destination).or_default();
dest_groups.push(group);
}
}
result
}
/// This method removes all topics from the list offsets request and returns them split up by their destination
/// If any topics are unroutable they will have their BrokerId set to -1
fn split_offset_for_leader_epoch_request_by_destination(
&mut self,
body: &mut OffsetForLeaderEpochRequest,
) -> HashMap<BrokerId, Vec<OffsetForLeaderTopic>> {
let mut result: HashMap<BrokerId, Vec<OffsetForLeaderTopic>> = Default::default();
for mut topic in body.topics.drain(..) {
let topic_name = &topic.topic;
if let Some(topic_meta) = self.topic_by_name.get(topic_name) {
for partition in std::mem::take(&mut topic.partitions) {
let partition_index = partition.partition as usize;
let destination = if let Some(partition) =
topic_meta.partitions.get(partition_index)
{
if partition.leader_id == -1 {
tracing::warn!(
"leader_id is unknown for {topic_name:?} at partition index {partition_index}",
);
}
partition.leader_id
} else {
let partition_len = topic_meta.partitions.len();
tracing::warn!(
"no known partition for {topic_name:?} at partition index {partition_index} out of {partition_len} partitions, routing request to a random broker so that a NOT_LEADER_OR_FOLLOWER or similar error is returned to the client"
);
BrokerId(-1)
};
tracing::debug!(
"Routing OffsetForLeaderEpoch request portion of partition {partition_index} in {topic_name:?} to broker {}",
destination.0
);
let dest_topics = result.entry(destination).or_default();
if let Some(dest_topic) =
dest_topics.iter_mut().find(|x| x.topic == topic.topic)
{
dest_topic.partitions.push(partition);
} else {
let mut topic = topic.clone();
topic.partitions.push(partition);
dest_topics.push(topic);
}
}
} else {
tracing::warn!(
"no known partition replica for {topic_name:?}, routing request to a random broker so that a NOT_LEADER_OR_FOLLOWER or similar error is returned to the client"
);
let destination = BrokerId(-1);
let dest_topics = result.entry(destination).or_default();
dest_topics.push(topic);
}
}
result
}
/// This method removes all topics from the DeleteRecords request and returns them split up by their destination.
/// If any topics are unroutable they will have their BrokerId set to -1
fn split_delete_records_request_by_destination(
&mut self,
body: &mut DeleteRecordsRequest,
) -> HashMap<BrokerId, Vec<DeleteRecordsTopic>> {
let mut result: HashMap<BrokerId, Vec<DeleteRecordsTopic>> = Default::default();
for mut topic in body.topics.drain(..) {
let topic_name = &topic.name;
if let Some(topic_meta) = self.topic_by_name.get(topic_name) {
for partition in std::mem::take(&mut topic.partitions) {
let partition_index = partition.partition_index as usize;
let destination = if let Some(partition) =
topic_meta.partitions.get(partition_index)
{
if partition.leader_id == -1 {
tracing::warn!(
"leader_id is unknown for {topic_name:?} at partition index {partition_index}",
);
}
partition.leader_id
} else {
let partition_len = topic_meta.partitions.len();
tracing::warn!(
"no known partition for {topic_name:?} at partition index {partition_index} out of {partition_len} partitions, routing request to a random broker so that a NOT_LEADER_OR_FOLLOWER or similar error is returned to the client"
);
BrokerId(-1)
};
tracing::debug!(
"Routing DeleteRecords request portion of partition {partition_index} in {topic_name:?} to broker {}",
destination.0
);
let dest_topics = result.entry(destination).or_default();
if let Some(dest_topic) = dest_topics.iter_mut().find(|x| x.name == topic.name)
{
dest_topic.partitions.push(partition);
} else {
let mut topic = topic.clone();
topic.partitions.push(partition);
dest_topics.push(topic);
}
}
} else {
tracing::warn!(
"no known partition replica for {topic_name:?}, routing request to a random broker so that a NOT_LEADER_OR_FOLLOWER or similar error is returned to the client"
);
let destination = BrokerId(-1);
let dest_topics = result.entry(destination).or_default();
dest_topics.push(topic);
}
}
result
}
/// This method removes all transactions from the AddPartitionsToTxn request and returns them split up by their destination
/// If any topics are unroutable they will have their BrokerId set to -1
fn split_add_partition_to_txn_request_by_destination(
&mut self,
body: &mut AddPartitionsToTxnRequest,
) -> HashMap<BrokerId, Vec<AddPartitionsToTxnTransaction>> {
let mut result: HashMap<BrokerId, Vec<_>> = Default::default();
for transaction in body.transactions.drain(..) {
let transaction_id = &transaction.transactional_id;
let destination = if let Some(destination) =
self.transaction_to_coordinator_broker.get(transaction_id)
{
tracing::debug!(
"Routing AddPartitionsToTxn request portion of transaction id {transaction_id:?} to broker {}",
destination.0
);
*destination
} else {
tracing::warn!(
"no known transaction for {transaction_id:?}, routing request to a random broker so that a NOT_COORDINATOR or similar error is returned to the client"
);
BrokerId(-1)
};
let dest_transactions = result.entry(destination).or_default();
dest_transactions.push(transaction);
}
result
}
fn route_add_partitions_to_txn(&mut self, mut request: Message) -> Result<()> {
if let Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::AddPartitionsToTxn(body),
header,
..
})) = request.frame()
{
if header.request_api_version <= 3 {
let transaction_id = body.v3_and_below_transactional_id.clone();
self.route_to_transaction_coordinator(request, transaction_id);
} else {
self.split_and_route_request::<AddPartitionsToTxnRequestSplitAndRouter>(request)?;
}
}
Ok(())
}
async fn find_coordinator(
&mut self,
key: CoordinatorKey,
) -> Result<KafkaNode, FindCoordinatorError> {
let request = Message::from_frame(Frame::Kafka(KafkaFrame::Request {
header: RequestHeader::default()
.with_request_api_key(ApiKey::FindCoordinator as i16)
.with_request_api_version(2)
.with_correlation_id(0),
body: RequestBody::FindCoordinator(
FindCoordinatorRequest::default()
.with_key_type(match key {
CoordinatorKey::Group(_) => 0,
CoordinatorKey::Transaction(_) => 1,
})
.with_key(match &key {
CoordinatorKey::Group(id) => id.0.clone(),
CoordinatorKey::Transaction(id) => id.0.clone(),
}),
),
}));
let mut response = self
.control_send_receive(request)
.await
.with_context(|| format!("Failed to query for coordinator of {key:?}"))?;
match response.frame() {
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::FindCoordinator(coordinator),
..
})) => match ResponseError::try_from_code(coordinator.error_code) {
None => Ok(KafkaNode::new(
coordinator.node_id,
KafkaAddress::new(coordinator.host.clone(), coordinator.port),
None,
)),
Some(ResponseError::CoordinatorNotAvailable) => {
Err(FindCoordinatorError::CoordinatorNotAvailable)
}
Some(err) => Err(FindCoordinatorError::Unrecoverable(anyhow!(
"Unexpected server error from FindCoordinator {err}"
))),
},
Some(Frame::Kafka(_)) => Err(anyhow!(
"Unexpected response returned to findcoordinator request"
))?,
None => Err(anyhow!("Response to FindCoordinator could not be parsed"))?,
_ => unreachable!("response to FindCoordinator was not a kafka response"),
}
}
/// Retry if we get an empty brokers list
/// We dont actually retry on failure since thats not a known failure mode for this request.
async fn get_metadata_of_topics_with_retry(
&mut self,
topic_names: Vec<TopicName>,
topic_ids: Vec<Uuid>,
) -> Result<Message> {
for _ in 0..3 {
let mut response = self
.get_metadata_of_topics(&topic_names, &topic_ids)
.await?;
match response.frame() {
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::Metadata(metadata),
..
})) => {
if metadata.brokers.is_empty() {
tracing::info!(
"Metadata response from broker contained an empty list of brokers, this likely indicates the cluster is still starting up, will retry metadata request after a delay."
);
tokio::time::sleep(Duration::from_millis(200)).await;
continue;
} else {
// cluster is ready, return the response
return Ok(response);
}
}
response => return Err(anyhow!("Expected metadata response but was {response:?}")),
}
}
Err(anyhow!("Broker returned empty list of brokers"))
}
async fn get_metadata_of_topics(
&mut self,
topic_names: &[TopicName],
topic_ids: &[Uuid],
) -> Result<Message> {
let api_version = if topic_ids.is_empty() { 4 } else { 12 };
let request = Message::from_frame(Frame::Kafka(KafkaFrame::Request {
header: RequestHeader::default()
.with_request_api_key(ApiKey::Metadata as i16)
.with_request_api_version(api_version)
.with_correlation_id(0),
body: RequestBody::Metadata(
MetadataRequest::default().with_topics(Some(
topic_names
.iter()
.map(|name| MetadataRequestTopic::default().with_name(Some(name.clone())))
.chain(topic_ids.iter().map(|id| {
MetadataRequestTopic::default()
.with_name(None)
.with_topic_id(*id)
}))
.collect(),
)),
),
}));
self.control_send_receive(request)
.await
.context("Failed to query metadata of topics")
}
/// Convert all PendingRequestState::Routed into PendingRequestState::Sent
async fn send_requests(&mut self) -> Result<()> {
struct RoutedRequests {
requests: Vec<Message>,
already_pending: usize,
}
let mut broker_to_routed_requests: HashMap<Destination, RoutedRequests> = HashMap::new();
for i in 0..self.pending_requests.len() {
if let PendingRequestState::Routed { .. } = &self.pending_requests[i].state {
let destination = self.pending_requests[i].destination;
let routed_requests =
broker_to_routed_requests
.entry(destination)
.or_insert_with(|| RoutedRequests {
requests: vec![],
already_pending: self
.pending_requests
.iter()
.filter(|pending_request| {
if let PendingRequestState::Sent { .. } = &pending_request.state
{
pending_request.destination == destination
} else {
false
}
})
.count(),
});
let request = match self.pending_requests[i].ty {
PendingRequestTy::Fetch { .. } => {
if let PendingRequestState::Routed { request, .. } =
&self.pending_requests[i].state
{
Some(request.clone())
} else {
unreachable!()
}
}
PendingRequestTy::RoutedToGroup(_) => None,
PendingRequestTy::RoutedToTransaction(_) => None,
PendingRequestTy::FindCoordinator(_) => None,
PendingRequestTy::Other => None,
};
let mut value = PendingRequestState::Sent {
index: routed_requests.requests.len() + routed_requests.already_pending,
request,
};
std::mem::swap(&mut self.pending_requests[i].state, &mut value);
if let PendingRequestState::Routed { request, .. } = value {
routed_requests.requests.push(request);
}
}
}
let recent_instant = Instant::now();
for (destination, mut requests) in broker_to_routed_requests {
match self
.connections
.get_or_open_connection(
&mut self.rng,
&self.connection_factory,
&self.authorize_scram_over_mtls,
&self.sasl_mechanism,
&self.nodes,
&self.first_contact_points,
&self.rack,
recent_instant,
destination,
)
.await
{
Ok(connection) => {
if let Err(err) = connection.send(requests.requests) {
// Dont retry the send on the new connection since we cant tell if the broker received the request or not.
self.connections
.handle_connection_error(
&self.connection_factory,
&self.authorize_scram_over_mtls,
&self.sasl_mechanism,
&self.nodes,
destination,
err.clone().into(),
)
.await?;
// If we succesfully recreate the outgoing connection we still need to terminate this incoming connection since the request is lost.
return Err(err.into());
}
}
Err(err) => {
// set node as down, the connection already failed to create so no point running through handle_connection_error,
// as that will recreate the connection which we already know just failed.
// Instead just directly set the node as down and return the error
// set node as down
let kafka_node = self.nodes.iter().find(|x| match destination {
Destination::Id(id) => x.broker_id == id,
Destination::ControlConnection => {
&x.kafka_address
== self
.connections
.control_connection_address
.as_ref()
.unwrap()
}
});
// kafka_node will be None when the cluster metadata hasnt been populated yet.
// This is guaranteed to happen when the cluster is down and shotover has never connected to the cluster.
// In that case we dont set anything and allow regular error handling to continue.
if let Some(node) = kafka_node {
node.set_state(KafkaNodeState::Down);
}
// bubble up error
let request_types: Vec<String> = requests
.requests
.iter_mut()
.map(|x| match x.frame() {
Some(Frame::Kafka(KafkaFrame::Request { header, .. })) => {
format!("{:?}", ApiKey::try_from(header.request_api_key).unwrap())
}
_ => "Unknown".to_owned(),
})
.collect();
return Err(err.context(format!(
"Failed to get connection to send requests {request_types:?}"
)));
}
}
}
Ok(())
}
/// Receive all responses from the outgoing connections, returns all responses that are ready to be returned.
/// For response ordering reasons, some responses will remain in self.pending_requests until other responses are received.
async fn recv_responses(&mut self, close_client_connection: &mut bool) -> Result<Vec<Message>> {
// To work around borrow checker issues, store connection errors in this temporary list before handling them.
let mut connection_errors = vec![];
// Convert all received PendingRequestState::Sent into PendingRequestState::Received
for (connection_destination, connection) in &mut self.connections.connections {
self.temp_responses_buffer.clear();
match connection
.try_recv_into(&mut self.temp_responses_buffer)
.with_context(|| format!("Failed to receive from {connection_destination:?}"))
{
Ok(()) => {
for response in self.temp_responses_buffer.drain(..) {
let mut response = Some(response);
for pending_request in &mut self.pending_requests {
if let PendingRequestState::Sent { index, request } =
&mut pending_request.state
{
if &pending_request.destination == connection_destination {
// Store the PendingRequestState::Received at the location of the next PendingRequestState::Sent
// All other PendingRequestState::Sent need to be decremented, in order to determine the PendingRequestState::Sent
// to be used next time, and the time after that, and ...
if *index == 0 {
pending_request.state = PendingRequestState::Received {
response: response.take().unwrap(),
request: request.take(),
};
} else {
*index -= 1;
}
}
}
}
}
}
Err(err) => connection_errors.push((*connection_destination, err)),
}
}
for (destination, err) in connection_errors {
if let Some(connection) = self.connections.connections.get(&destination) {
if connection.pending_requests_count() == 0 {
self.connections.connections.remove(&destination);
} else {
// Has pending requests - need to handle the error properly
self.connections
.handle_connection_error(
&self.connection_factory,
&self.authorize_scram_over_mtls,
&self.sasl_mechanism,
&self.nodes,
destination,
err,
)
.await?;
}
}
}
// Remove and return all PendingRequestState::Received that are ready to be received.
let mut responses = vec![];
while let Some(pending_request) = self.pending_requests.front() {
let combine_responses = pending_request.combine_responses;
let all_combined_received = (0..combine_responses).all(|i| {
matches!(
self.pending_requests.get(i),
Some(PendingRequest {
state: PendingRequestState::Received { .. },
..
})
)
});
if all_combined_received {
let pending_request_ty = pending_request.ty.clone();
// perform special handling for certain message types
if let PendingRequestTy::Fetch {
originally_sent_at,
max_wait_ms,
min_bytes,
} = &pending_request_ty
{
// resend the requests if we havent yet met the `max_wait_ms` and `min_bytes` requirements
if originally_sent_at.elapsed() < Duration::from_millis(*max_wait_ms as u64)
&& Self::total_fetch_record_bytes(
&mut self.pending_requests,
combine_responses,
) < *min_bytes as i64
{
tokio::time::sleep(self.refetch_backoff).await;
// exponential backoff
self.refetch_backoff *= 2;
for i in 0..combine_responses {
let pending_request = &mut self.pending_requests[i];
if let PendingRequestState::Received { request, .. } =
&mut pending_request.state
{
pending_request.state = PendingRequestState::Routed {
request: request.take().unwrap(),
}
} else {
unreachable!()
}
}
// The pending_request is not received, we need to break to maintain response ordering.
break;
} else {
self.refetch_backoff = Duration::from_millis(1);
}
}
// The next response we are waiting on has been received, add it to responses
let mut response = if combine_responses == 1 {
if let Some(PendingRequest {
state: PendingRequestState::Received { response, .. },
..
}) = self.pending_requests.pop_front()
{
response
} else {
unreachable!("Guaranteed by all_combined_received")
}
} else {
let drain = self.pending_requests.drain(..combine_responses).map(|x| {
if let PendingRequest {
state: PendingRequestState::Received { response, .. },
destination,
..
} = x
{
ResponseToBeCombined {
response,
destination,
}
} else {
unreachable!("Guaranteed by all_combined_received")
}
});
Self::combine_responses(drain)?
};
self.process_response(&mut response, pending_request_ty, close_client_connection)
.await
.context("Failed to process response")?;
responses.push(response);
} else {
// The pending_request is not received, we need to break to maintain response ordering.
break;
}
}
// In the case of fetch requests, recv_responses may set pending requests back to routed state.
// So we retry send_requests immediately so the fetch requests arent stuck waiting for another call to `KafkaSinkCluster::transform`.
self.send_requests()
.await
.context("Failed to send requests")?;
Ok(responses)
}
fn total_fetch_record_bytes(
pending_requests: &mut VecDeque<PendingRequest>,
combine_responses: usize,
) -> i64 {
let mut result = 0;
for pending_request in pending_requests.iter_mut().take(combine_responses) {
if let PendingRequestState::Received { response, .. } = &mut pending_request.state {
if let Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::Fetch(fetch),
..
})) = response.frame()
{
result += fetch
.responses
.iter()
.map(|x| {
x.partitions
.iter()
.map(|x| {
x.records
.as_ref()
.map(|x| x.len() as i64)
.unwrap_or_default()
})
.sum::<i64>()
})
.sum::<i64>();
} else {
panic!("must be called on fetch responses")
}
} else {
panic!("must be called on received responses")
}
}
result
}
fn combine_responses(mut drain: impl Iterator<Item = ResponseToBeCombined>) -> Result<Message> {
// Take this response as base.
// Then iterate over all remaining combined responses and integrate them into the base.
let mut base = drain.next().unwrap();
match base.response.frame() {
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::Fetch(base),
..
})) => Self::combine_fetch_responses(base, drain)?,
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::ListOffsets(base),
..
})) => Self::combine_list_offsets_responses(base, drain)?,
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::OffsetForLeaderEpoch(base),
..
})) => Self::combine_offset_for_leader_epoch_responses(base, drain)?,
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::Produce(base),
..
})) => Self::combine_produce_responses(base, drain)?,
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::DeleteGroups(base),
..
})) => Self::combine_delete_groups_responses(base, drain)?,
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::DeleteRecords(base),
..
})) => Self::combine_delete_records(base, drain)?,
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::OffsetFetch(base),
..
})) => Self::combine_offset_fetch(base, drain)?,
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::DescribeProducers(base),
..
})) => Self::combine_describe_producers(base, drain)?,
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::ListGroups(base),
..
})) => Self::combine_list_groups(base, drain)?,
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::DescribeTransactions(base),
..
})) => Self::combine_describe_transactions(base, drain)?,
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::ListTransactions(base),
..
})) => Self::combine_list_transactions(base, drain)?,
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::DescribeGroups(base),
..
})) => Self::combine_describe_groups(base, drain)?,
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::ConsumerGroupDescribe(base),
..
})) => Self::combine_consumer_group_describe(base, drain)?,
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::DescribeLogDirs(base_body),
..
})) => Self::combine_describe_log_dirs(base.destination, base_body, drain)?,
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::AddPartitionsToTxn(base),
version,
..
})) => {
debug_assert!(*version > 3);
Self::combine_add_partitions_to_txn(base, drain)?
}
_ => {
return Err(anyhow!(
"Combining of this message type is currently unsupported"
));
}
}
base.response.invalidate_cache();
Ok(base.response)
}
fn combine_fetch_responses(
base_fetch: &mut FetchResponse,
drain: impl Iterator<Item = ResponseToBeCombined>,
) -> Result<()> {
for mut next in drain {
if let Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::Fetch(next_fetch),
..
})) = next.response.frame()
{
for next_response in std::mem::take(&mut next_fetch.responses) {
if let Some(base_response) = base_fetch.responses.iter_mut().find(|response| {
response.topic == next_response.topic
&& response.topic_id == next_response.topic_id
}) {
for next_partition in &next_response.partitions {
for base_partition in &base_response.partitions {
if next_partition.partition_index == base_partition.partition_index
{
tracing::warn!(
"Duplicate partition indexes in combined fetch response, if this ever occurs we should investigate the repercussions"
)
}
}
}
// A partition can only be contained in one response so there is no risk of duplicating partitions
base_response.partitions.extend(next_response.partitions)
} else {
base_fetch.responses.push(next_response);
}
}
} else {
return Err(anyhow!(
"Combining Fetch responses but received another message type"
));
}
}
Ok(())
}
fn combine_list_offsets_responses(
base_list_offsets: &mut ListOffsetsResponse,
drain: impl Iterator<Item = ResponseToBeCombined>,
) -> Result<()> {
for mut next in drain {
if let Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::ListOffsets(next_list_offsets),
..
})) = next.response.frame()
{
for next_topic in std::mem::take(&mut next_list_offsets.topics) {
if let Some(base_topic) = base_list_offsets
.topics
.iter_mut()
.find(|topic| topic.name == next_topic.name)
{
for next_partition in &next_topic.partitions {
for base_partition in &base_topic.partitions {
if next_partition.partition_index == base_partition.partition_index
{
tracing::warn!(
"Duplicate partition indexes in combined fetch response, if this ever occurs we should investigate the repercussions"
)
}
}
}
// A partition can only be contained in one response so there is no risk of duplicating partitions
base_topic.partitions.extend(next_topic.partitions)
} else {
base_list_offsets.topics.push(next_topic);
}
}
} else {
return Err(anyhow!(
"Combining ListOffsets responses but received another message type"
));
}
}
Ok(())
}
fn combine_offset_for_leader_epoch_responses(
base_list_offsets: &mut OffsetForLeaderEpochResponse,
drain: impl Iterator<Item = ResponseToBeCombined>,
) -> Result<()> {
for mut next in drain {
if let Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::OffsetForLeaderEpoch(next_body),
..
})) = next.response.frame()
{
for next_topic in std::mem::take(&mut next_body.topics) {
if let Some(base_topic) = base_list_offsets
.topics
.iter_mut()
.find(|topic| topic.topic == next_topic.topic)
{
for next_partition in &next_topic.partitions {
for base_partition in &base_topic.partitions {
if next_partition.partition == base_partition.partition {
tracing::warn!(
"Duplicate partition indexes in combined OffsetForLeaderEpoch response, if this ever occurs we should investigate the repercussions"
)
}
}
}
// A partition can only be contained in one response so there is no risk of duplicating partitions
base_topic.partitions.extend(next_topic.partitions)
} else {
base_list_offsets.topics.push(next_topic);
}
}
} else {
return Err(anyhow!(
"Combining OffsetForLeaderEpoch responses but received another message type"
));
}
}
Ok(())
}
fn combine_produce_responses(
base_produce: &mut ProduceResponse,
drain: impl Iterator<Item = ResponseToBeCombined>,
) -> Result<()> {
let mut base_responses: HashMap<TopicName, TopicProduceResponse> =
std::mem::take(&mut base_produce.responses)
.into_iter()
.map(|response| (response.name.clone(), response))
.collect();
for mut next in drain {
if let Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::Produce(next_produce),
..
})) = next.response.frame()
{
for next_response in std::mem::take(&mut next_produce.responses) {
if let Some(base_response) = base_responses.get_mut(&next_response.name) {
for next_partition in &next_response.partition_responses {
for base_partition in &base_response.partition_responses {
if next_partition.index == base_partition.index {
tracing::warn!(
"Duplicate partition indexes in combined produce response, if this ever occurs we should investigate the repercussions"
)
}
}
}
// A partition can only be contained in one response so there is no risk of duplicating partitions
base_response
.partition_responses
.extend(next_response.partition_responses)
} else {
base_responses.insert(next_response.name.clone(), next_response);
}
}
} else {
return Err(anyhow!(
"Combining Produce responses but received another message type"
));
}
}
base_produce.responses.extend(base_responses.into_values());
Ok(())
}
fn combine_delete_records(
base_body: &mut DeleteRecordsResponse,
drain: impl Iterator<Item = ResponseToBeCombined>,
) -> Result<()> {
let mut base_topics: HashMap<TopicName, DeleteRecordsTopicResult> =
std::mem::take(&mut base_body.topics)
.into_iter()
.map(|response| (response.name.clone(), response))
.collect();
for mut next in drain {
if let Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::DeleteRecords(next_body),
..
})) = next.response.frame()
{
for next_response in std::mem::take(&mut next_body.topics) {
if let Some(base_response) = base_topics.get_mut(&next_response.name) {
for next_partition in &next_response.partitions {
for base_partition in &base_response.partitions {
if next_partition.partition_index == base_partition.partition_index
{
tracing::warn!(
"Duplicate partition indexes in combined DeleteRecords response, if this ever occurs we should investigate the repercussions"
)
}
}
}
// A partition can only be contained in one response so there is no risk of duplicating partitions
base_response.partitions.extend(next_response.partitions)
} else {
base_topics.insert(next_response.name.clone(), next_response);
}
}
} else {
return Err(anyhow!(
"Combining DeleteRecords responses but received another message type"
));
}
}
base_body.topics.extend(base_topics.into_values());
Ok(())
}
fn combine_delete_groups_responses(
base_delete_groups: &mut DeleteGroupsResponse,
drain: impl Iterator<Item = ResponseToBeCombined>,
) -> Result<()> {
for mut next in drain {
if let Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::DeleteGroups(next_delete_groups),
..
})) = next.response.frame()
{
base_delete_groups
.results
.extend(std::mem::take(&mut next_delete_groups.results))
}
}
Ok(())
}
fn combine_offset_fetch(
base_offset_fetch: &mut OffsetFetchResponse,
drain: impl Iterator<Item = ResponseToBeCombined>,
) -> Result<()> {
for mut next in drain {
if let Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::OffsetFetch(next_offset_fetch),
..
})) = next.response.frame()
{
base_offset_fetch
.groups
.extend(std::mem::take(&mut next_offset_fetch.groups))
}
}
Ok(())
}
fn combine_list_groups(
base_list_groups: &mut ListGroupsResponse,
drain: impl Iterator<Item = ResponseToBeCombined>,
) -> Result<()> {
for mut next in drain {
if let Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::ListGroups(next_list_groups),
..
})) = next.response.frame()
{
base_list_groups
.groups
.extend(std::mem::take(&mut next_list_groups.groups));
}
}
Ok(())
}
fn combine_describe_producers(
base: &mut DescribeProducersResponse,
drain: impl Iterator<Item = ResponseToBeCombined>,
) -> Result<()> {
let mut base_responses: HashMap<TopicName, TopicResponse> =
std::mem::take(&mut base.topics)
.into_iter()
.map(|response| (response.name.clone(), response))
.collect();
for mut next in drain {
if let Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::DescribeProducers(next),
..
})) = next.response.frame()
{
for next_response in std::mem::take(&mut next.topics) {
if let Some(base_response) = base_responses.get_mut(&next_response.name) {
for next_partition in &next_response.partitions {
for base_partition in &base_response.partitions {
if next_partition.partition_index == base_partition.partition_index
{
tracing::warn!(
"Duplicate partition indexes in combined DescribeProducers response, if this ever occurs we should investigate the repercussions"
)
}
}
}
// A partition can only be contained in one response so there is no risk of duplicating partitions
base_response.partitions.extend(next_response.partitions)
} else {
base_responses.insert(next_response.name.clone(), next_response);
}
}
} else {
return Err(anyhow!(
"Combining DescribeProducers responses but received another message type"
));
}
}
base.topics.extend(base_responses.into_values());
Ok(())
}
fn combine_describe_transactions(
base: &mut DescribeTransactionsResponse,
drain: impl Iterator<Item = ResponseToBeCombined>,
) -> Result<()> {
for mut next in drain {
if let Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::DescribeTransactions(next),
..
})) = next.response.frame()
{
base.transaction_states
.extend(std::mem::take(&mut next.transaction_states));
}
}
Ok(())
}
fn combine_list_transactions(
base_list_transactions: &mut ListTransactionsResponse,
drain: impl Iterator<Item = ResponseToBeCombined>,
) -> Result<()> {
for mut next in drain {
if let Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::ListTransactions(next_list_transactions),
..
})) = next.response.frame()
{
base_list_transactions
.transaction_states
.extend(std::mem::take(
&mut next_list_transactions.transaction_states,
));
}
}
Ok(())
}
fn combine_describe_log_dirs(
base_destination: Destination,
base_body: &mut DescribeLogDirsResponse,
drain: impl Iterator<Item = ResponseToBeCombined>,
) -> Result<()> {
Self::prepend_destination_to_log_dir(base_destination, base_body);
for mut next in drain {
if let Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::DescribeLogDirs(next_body),
..
})) = next.response.frame()
{
Self::prepend_destination_to_log_dir(next.destination, next_body);
base_body
.results
.extend(std::mem::take(&mut next_body.results));
}
}
Ok(())
}
/// Rewrite the log dir paths to a custom format to allow results to be disambiguated.
/// Usually only 1 file system path can exist on a single broker machine.
/// However since shotover represents many different brokers, there can be different log dirs with identical paths associated with a single shotover instance.
/// This would be extremely confusing to a user and the client driver could assume that paths are unique.
/// So we need to alter the path to include details of which broker the path resides on.
///
/// The downsides of this are:
/// * This leaks details of the actual kafka cluster to the user
/// * The path is no longer a valid path
///
/// The only other possible solution I see would be to have shotover error on this request type instead.
/// But I think its reasonable to instead take these downsides and provide most of the value of this message type to the user.
///
/// For example:
/// If the path starts as: /original/log/dir/path
/// It will become something like: actual-kafka-broker-id3:/original/log/dir/path
fn prepend_destination_to_log_dir(
destination: Destination,
body: &mut DescribeLogDirsResponse,
) {
for result in &mut body.results {
let log_dir = result.log_dir.as_str();
let altered_log_dir = match destination {
Destination::Id(id) => format!("actual-kafka-broker-id{id:?}:{log_dir}"),
Destination::ControlConnection => {
unreachable!("DescribeLogDirs are not sent as control connections")
}
};
result.log_dir = StrBytes::from_string(altered_log_dir);
}
}
fn combine_describe_groups(
base: &mut DescribeGroupsResponse,
drain: impl Iterator<Item = ResponseToBeCombined>,
) -> Result<()> {
for mut next in drain {
if let Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::DescribeGroups(next),
..
})) = next.response.frame()
{
base.groups.extend(std::mem::take(&mut next.groups));
}
}
Ok(())
}
fn combine_consumer_group_describe(
base: &mut ConsumerGroupDescribeResponse,
drain: impl Iterator<Item = ResponseToBeCombined>,
) -> Result<()> {
for mut next in drain {
if let Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::ConsumerGroupDescribe(next),
..
})) = next.response.frame()
{
base.groups.extend(std::mem::take(&mut next.groups));
}
}
Ok(())
}
fn combine_add_partitions_to_txn(
base_add_partitions_to_txn: &mut AddPartitionsToTxnResponse,
drain: impl Iterator<Item = ResponseToBeCombined>,
) -> Result<()> {
for mut next in drain {
if let Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::AddPartitionsToTxn(next_add_partitions_to_txn),
..
})) = next.response.frame()
{
base_add_partitions_to_txn
.results_by_transaction
.extend(std::mem::take(
&mut next_add_partitions_to_txn.results_by_transaction,
));
} else {
return Err(anyhow!(
"Combining AddPartitionsToTxn responses but received another message type"
));
}
}
Ok(())
}
async fn process_response(
&mut self,
response: &mut Message,
request_ty: PendingRequestTy,
close_client_connection: &mut bool,
) -> Result<()> {
match response.frame() {
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::FindCoordinator(find_coordinator),
version,
..
})) => {
if let PendingRequestTy::FindCoordinator(request) = request_ty {
self.process_find_coordinator_response(*version, &request, find_coordinator);
self.rewrite_find_coordinator_response(*version, &request, find_coordinator);
response.invalidate_cache();
} else {
return Err(anyhow!("Received find_coordinator but not requested"));
}
}
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::SaslHandshake(handshake),
..
})) => {
// If authorize_scram_over_mtls is disabled there is no way that scram can work through KafkaSinkCluster
// since it is specifically designed such that replay attacks wont work.
// So when authorize_scram_over_mtls is disabled report to the user that SCRAM is not enabled.
if self.authorize_scram_over_mtls.is_none() {
// remove scram from supported mechanisms
handshake
.mechanisms
.retain(|x| !SASL_SCRAM_MECHANISMS.contains(&x.as_str()));
// declare unsupported if the client requested SCRAM
if let Some(sasl_mechanism) = &self.sasl_mechanism {
if SASL_SCRAM_MECHANISMS.contains(&sasl_mechanism.as_str()) {
handshake.error_code = ResponseError::UnsupportedSaslMechanism.code();
}
}
response.invalidate_cache();
}
}
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::SaslAuthenticate(authenticate),
..
})) => {
self.process_sasl_authenticate(authenticate, close_client_connection)?;
}
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::Produce(produce),
..
})) => {
// Clear this optional field to avoid making clients try to bypass shotover
produce.node_endpoints.clear();
for response_topic in &mut produce.responses {
let topic_name = &response_topic.name;
for response_partition in &response_topic.partition_responses {
if let Some(ResponseError::NotLeaderOrFollower) =
ResponseError::try_from_code(response_partition.error_code)
{
if response_partition.current_leader.leader_id != -1 {
// The broker has informed us who the new leader is, we can just directly update the leader
if let Some(mut stored_topic) =
self.topic_by_name.get_mut(topic_name)
{
if let Some(stored_partition) = stored_topic
.partitions
.get_mut(response_partition.index as usize)
{
if response_partition.current_leader.leader_epoch
> stored_partition.leader_epoch
{
stored_partition.leader_id =
response_partition.current_leader.leader_id;
stored_partition.leader_epoch =
response_partition.current_leader.leader_epoch;
}
tracing::info!(
"Produce response included error NOT_LEADER_OR_FOLLOWER and so updated leader in topic {:?} partition {}",
topic_name,
response_partition.index
);
}
}
} else {
// The broker doesnt know who the new leader is, clear the entire topic.
self.topic_by_name.remove(topic_name);
tracing::info!(
"Produce response included error NOT_LEADER_OR_FOLLOWER and so cleared topic {topic_name:?}"
);
break;
}
}
}
for response_partition in &mut response_topic.partition_responses {
// Clear this optional field to avoid making clients try to bypass shotover
response_partition.current_leader =
ProduceResponseLeaderIdAndEpoch::default();
}
}
response.invalidate_cache();
}
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::Fetch(fetch),
..
})) => {
// Clear this optional field to avoid making clients try to bypass shotover
// partition.current_leader and partition.preferred_read_replica are cleared due to the same reason
fetch.node_endpoints.clear();
for fetch_response in &mut fetch.responses {
for partition in &mut fetch_response.partitions {
partition.current_leader = FetchResponseLeaderIdAndEpoch::default();
partition.preferred_read_replica = BrokerId(-1);
if let Some(ResponseError::NotLeaderOrFollower) =
ResponseError::try_from_code(partition.error_code)
{
// The fetch response includes the leader_id which a client could use to route a fetch request to,
// but we cant use it to fix our list of replicas, so our only option is to clear the whole thing.
self.topic_by_name.remove(&fetch_response.topic);
self.topic_by_id.remove(&fetch_response.topic_id);
tracing::info!(
"Fetch response included error NOT_LEADER_OR_FOLLOWER and so cleared metadata for topic {:?} {:?}",
fetch_response.topic,
fetch_response.topic_id
);
break;
}
}
}
response.invalidate_cache();
}
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::ListOffsets(list_offsets),
..
})) => {
for topic in &mut list_offsets.topics {
for partition in &mut topic.partitions {
if let Some(ResponseError::NotLeaderOrFollower) =
ResponseError::try_from_code(partition.error_code)
{
self.topic_by_name.remove(&topic.name);
tracing::info!(
"ListOffsets response included error NOT_LEADER_OR_FOLLOWER and so cleared metadata for topic {:?}",
topic.name,
);
break;
}
}
}
}
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::OffsetForLeaderEpoch(body),
..
})) => {
for topic in &mut body.topics {
for partition in &mut topic.partitions {
if let Some(ResponseError::NotLeaderOrFollower) =
ResponseError::try_from_code(partition.error_code)
{
self.topic_by_name.remove(&topic.topic);
tracing::info!(
"OffsetForLeaderEpoch response included error NOT_LEADER_OR_FOLLOWER and so cleared metadata for topic {:?}",
topic.topic,
);
break;
}
}
}
}
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::Heartbeat(heartbeat),
..
})) => self.handle_group_coordinator_routing_error(&request_ty, heartbeat.error_code),
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::ConsumerGroupHeartbeat(heartbeat),
..
})) => self.handle_group_coordinator_routing_error(&request_ty, heartbeat.error_code),
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::SyncGroup(sync_group),
..
})) => self.handle_group_coordinator_routing_error(&request_ty, sync_group.error_code),
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::OffsetFetch(offset_fetch),
version,
..
})) => {
if *version <= 7 {
// group id is not provided in response, so use group id stored in request_ty
self.handle_group_coordinator_routing_error(
&request_ty,
offset_fetch.error_code,
)
} else {
// group id is provided in response, so use group id in the response.
for group in &offset_fetch.groups {
let group_id = &group.group_id;
if let Some(ResponseError::NotCoordinator) =
ResponseError::try_from_code(group.error_code)
{
let broker_id = self
.group_to_coordinator_broker
.remove(group_id)
.map(|x| x.1);
tracing::info!(
"Response was error NOT_COORDINATOR and so cleared group id {group_id:?} coordinator mapping to broker {broker_id:?}"
);
}
}
}
}
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::JoinGroup(join_group),
..
})) => self.handle_group_coordinator_routing_error(&request_ty, join_group.error_code),
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::LeaveGroup(leave_group),
..
})) => self.handle_group_coordinator_routing_error(&request_ty, leave_group.error_code),
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::EndTxn(end_txn),
..
})) => {
self.handle_transaction_coordinator_routing_error(&request_ty, end_txn.error_code)
}
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::AddOffsetsToTxn(add_offsets_to_txn),
..
})) => self.handle_transaction_coordinator_routing_error(
&request_ty,
add_offsets_to_txn.error_code,
),
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::TxnOffsetCommit(txn_offset_commit),
..
})) => {
for topic in &txn_offset_commit.topics {
for partition in &topic.partitions {
self.handle_group_coordinator_routing_error(
&request_ty,
partition.error_code,
);
}
}
}
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::InitProducerId(init_producer_id),
..
})) => self.handle_transaction_coordinator_routing_error(
&request_ty,
init_producer_id.error_code,
),
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::AddPartitionsToTxn(response),
version,
..
})) => {
if *version <= 3 {
for topic_result in &response.results_by_topic_v3_and_below {
for partition_result in &topic_result.results_by_partition {
self.handle_transaction_coordinator_routing_error(
&request_ty,
partition_result.partition_error_code,
);
}
}
} else {
'outer_loop: for transaction in &response.results_by_transaction {
let transaction_id = &transaction.transactional_id;
for topic_results in &transaction.topic_results {
for partition_result in &topic_results.results_by_partition {
if let Some(ResponseError::NotCoordinator) =
ResponseError::try_from_code(
partition_result.partition_error_code,
)
{
let broker_id = self
.transaction_to_coordinator_broker
.remove(transaction_id)
.map(|x| x.1);
tracing::info!(
"Response was error NOT_COORDINATOR and so cleared transaction id {:?} coordinator mapping to broker {:?}",
transaction_id,
broker_id,
);
continue 'outer_loop;
}
}
}
}
}
}
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::DeleteGroups(delete_groups),
..
})) => {
// clear metadata that resulted in NotCoordinator error
for result in &delete_groups.results {
let group_id = &result.group_id;
if let Some(ResponseError::NotCoordinator) =
ResponseError::try_from_code(result.error_code)
{
self.group_to_coordinator_broker.remove(group_id);
}
}
}
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::CreateTopics(create_topics),
..
})) => {
for topic in &create_topics.topics {
if let Some(ResponseError::NotController) =
ResponseError::try_from_code(topic.error_code)
{
tracing::info!(
"Response to CreateTopics included error NOT_CONTROLLER and so reset controller broker, previously was {:?}",
self.controller_broker.get()
);
self.controller_broker.clear();
break;
}
}
}
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::Metadata(metadata),
..
})) => {
self.process_metadata_response(metadata).await;
self.rewrite_metadata_response(metadata)?;
response.invalidate_cache();
}
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::DescribeCluster(describe_cluster),
..
})) => {
self.process_describe_cluster_response(describe_cluster)
.await;
self.rewrite_describe_cluster_response(describe_cluster)?;
response.invalidate_cache();
}
Some(Frame::Kafka(KafkaFrame::Response {
body: ResponseBody::ApiVersions(api_versions),
..
})) => {
api_versions.api_keys.retain_mut(|api_key| {
match api_versions::versions_supported_by_key(api_key.api_key) {
Some(version) => {
if api_key.max_version > version.max {
api_key.max_version = version.max;
}
if api_key.min_version < version.min {
api_key.min_version = version.min;
}
true
}
None => false,
}
});
response.invalidate_cache();
}
_ => {}
}
Ok(())
}
/// This method must be called for every response to a request that was routed via `route_to_group_coordinator`
fn handle_group_coordinator_routing_error(
&mut self,
pending_request_ty: &PendingRequestTy,
error_code: i16,
) {
if let Some(ResponseError::NotCoordinator) = ResponseError::try_from_code(error_code) {
if let PendingRequestTy::RoutedToGroup(group_id) = pending_request_ty {
let broker_id = self
.group_to_coordinator_broker
.remove(group_id)
.map(|x| x.1);
tracing::info!(
"Response was error NOT_COORDINATOR and so cleared group id {:?} coordinator mapping to broker {:?}",
group_id,
broker_id,
);
}
}
}
/// This method must be called for every response to a request that was routed via `route_to_transaction_coordinator`
fn handle_transaction_coordinator_routing_error(
&mut self,
pending_request_ty: &PendingRequestTy,
error_code: i16,
) {
if let Some(ResponseError::NotCoordinator) = ResponseError::try_from_code(error_code) {
if let PendingRequestTy::RoutedToTransaction(transaction_id) = pending_request_ty {
let broker_id = self
.transaction_to_coordinator_broker
.remove(transaction_id)
.map(|x| x.1);
tracing::info!(
"Response was error NOT_COORDINATOR and so cleared transaction id {:?} coordinator mapping to broker {:?}",
transaction_id,
broker_id,
);
}
}
}
fn process_sasl_authenticate(
&mut self,
authenticate: &mut SaslAuthenticateResponse,
close_client_connection: &mut bool,
) -> Result<()> {
// The broker always closes the connection after an auth failure response,
// so we should do the same.
if authenticate.error_code != 0 {
tracing::debug!("Closing connection to client due to auth failure");
*close_client_connection = true;
}
if let Some(sasl_mechanism) = &self.sasl_mechanism {
if SASL_SCRAM_MECHANISMS.contains(&sasl_mechanism.as_str()) {
if let Some(scram_over_mtls) = &mut self.authorize_scram_over_mtls {
match scram_over_mtls.original_scram_state {
OriginalScramState::WaitingOnServerFirst => {
scram_over_mtls.original_scram_state = if authenticate.error_code == 0 {
OriginalScramState::WaitingOnServerFinal
} else {
OriginalScramState::AuthFailed
};
}
OriginalScramState::WaitingOnServerFinal => {
scram_over_mtls.original_scram_state = if authenticate.error_code == 0 {
OriginalScramState::AuthSuccess
} else {
OriginalScramState::AuthFailed
};
}
OriginalScramState::AuthSuccess | OriginalScramState::AuthFailed => {
return Err(anyhow!(
"SCRAM protocol does not allow a third sasl response"
));
}
}
}
}
}
Ok(())
}
fn route_to_control_connection(&mut self, request: Message) {
assert!(
!self.auth_complete,
"route_to_control_connection cannot be called after auth is complete. Otherwise it would collide with control_send_receive"
);
self.pending_requests.push_back(PendingRequest {
state: PendingRequestState::Routed { request },
destination: Destination::ControlConnection,
ty: PendingRequestTy::Other,
combine_responses: 1,
});
tracing::debug!("Routing request to control connection");
}
fn route_to_controller(&mut self, request: Message) {
let broker_id = self.controller_broker.get().unwrap();
let destination = if let Some(node) = self
.nodes
.iter_mut()
.find(|x| x.broker_id == *broker_id && x.is_up())
{
node.broker_id
} else {
tracing::warn!(
"no known broker with id {broker_id:?} that is 'up', routing request to a random broker so that a NOT_CONTROLLER or similar error is returned to the client"
);
random_broker_id(&self.nodes, &mut self.rng, &self.rack)
};
self.pending_requests.push_back(PendingRequest {
state: PendingRequestState::routed(request),
destination: Destination::Id(destination),
ty: PendingRequestTy::Other,
combine_responses: 1,
});
tracing::debug!(
"Routing request relating to controller to broker {}",
destination.0
);
}
fn route_to_group_coordinator(&mut self, request: Message, group_id: GroupId) {
let destination = self.group_to_coordinator_broker.get(&group_id);
let destination = match destination {
Some(destination) => *destination,
None => {
tracing::info!(
"no known coordinator for {group_id:?}, routing request to a random broker so that a NOT_COORDINATOR or similar error is returned to the client"
);
random_broker_id(&self.nodes, &mut self.rng, &self.rack)
}
};
tracing::debug!(
"Routing request relating to group id {group_id:?} to broker {destination:?}",
);
self.pending_requests.push_back(PendingRequest {
state: PendingRequestState::routed(request),
destination: Destination::Id(destination),
ty: PendingRequestTy::RoutedToGroup(group_id),
combine_responses: 1,
});
}
fn route_to_transaction_coordinator(
&mut self,
request: Message,
transaction_id: TransactionalId,
) {
let destination = self.transaction_to_coordinator_broker.get(&transaction_id);
let destination = match destination {
Some(destination) => *destination,
None => {
tracing::info!(
"no known coordinator for {transaction_id:?}, routing request to a random broker so that a NOT_COORDINATOR or similar error is returned to the client"
);
random_broker_id(&self.nodes, &mut self.rng, &self.rack)
}
};
tracing::debug!(
"Routing request relating to transaction id {transaction_id:?} to broker {destination:?}"
);
self.pending_requests.push_back(PendingRequest {
state: PendingRequestState::routed(request),
destination: Destination::Id(destination),
ty: PendingRequestTy::RoutedToTransaction(transaction_id),
combine_responses: 1,
});
}
fn route_find_coordinator(&mut self, mut request: Message) {
if let Some(Frame::Kafka(KafkaFrame::Request {
body: RequestBody::FindCoordinator(find_coordinator),
..
})) = request.frame()
{
let destination = random_broker_id(&self.nodes, &mut self.rng, &self.rack);
let ty = PendingRequestTy::FindCoordinator(FindCoordinator {
key: find_coordinator.key.clone(),
key_type: find_coordinator.key_type,
});
tracing::debug!("Routing FindCoordinator to random broker {}", destination.0);
self.pending_requests.push_back(PendingRequest {
state: PendingRequestState::routed(request),
destination: Destination::Id(destination),
ty,
combine_responses: 1,
});
}
}
async fn process_metadata_response(&mut self, metadata: &MetadataResponse) {
for broker in &metadata.brokers {
let node = KafkaNode::new(
broker.node_id,
KafkaAddress::new(broker.host.clone(), broker.port),
broker.rack.clone(),
);
self.add_node_if_new(node).await;
}
tracing::debug!(
"Storing controller metadata, controller is now broker {}",
metadata.controller_id.0
);
self.controller_broker.set(metadata.controller_id);
for topic in &metadata.topics {
if ResponseError::try_from_code(topic.error_code).is_none() {
// We use the response's partitions list as a base
// since if it has deleted an entry then we also want to delete that entry.
let mut new_partitions: Vec<_> = topic
.partitions
.iter()
.map(|partition| Partition {
index: partition.partition_index,
leader_id: partition.leader_id,
leader_epoch: partition.leader_epoch,
shotover_rack_replica_nodes: partition
.replica_nodes
.iter()
.cloned()
.filter(|replica_node_id| self.broker_within_rack(*replica_node_id))
.collect(),
external_rack_replica_nodes: partition
.replica_nodes
.iter()
.cloned()
.filter(|replica_node_id| !self.broker_within_rack(*replica_node_id))
.collect(),
})
.collect();
new_partitions.sort_by_key(|x| x.index);
// If topic_by_name contains any partitions with a more recent leader_epoch use that instead.
// The out of date epoch is probably caused by requesting metadata from a broker that is slightly out of date.
// We use topic_by_name instead of topic_by_id since its always used regardless of protocol version.
if let Some(topic_name) = &topic.name {
if let Some(topic) = self.topic_by_name.get(topic_name) {
for old_partition in &topic.partitions {
if let Some(new_partition) = new_partitions
.iter_mut()
.find(|p| p.index == old_partition.index)
{
if old_partition.leader_epoch > new_partition.leader_epoch {
new_partition.leader_id = old_partition.leader_id;
new_partition
.shotover_rack_replica_nodes
.clone_from(&old_partition.shotover_rack_replica_nodes);
new_partition
.external_rack_replica_nodes
.clone_from(&old_partition.external_rack_replica_nodes);
}
}
}
};
}
let has_topic_name = topic.name.is_some();
let has_topic_id = !topic.topic_id.is_nil();
// Since new_partitions can be quite long we avoid logging it twice to keep the debug logs somewhat readable.
if has_topic_name && has_topic_id {
tracing::debug!(
"Storing topic_by_name and topic_by_id metadata: topic {:?} {} -> {:?}",
topic.name,
topic.topic_id,
new_partitions
);
} else if has_topic_name {
tracing::debug!(
"Storing topic_by_name metadata: topic {:?} -> {new_partitions:?}",
topic.name
);
} else if has_topic_id {
tracing::debug!(
"Storing topic_by_id metadata: topic {} -> {new_partitions:?}",
topic.topic_id
);
}
if let Some(topic_name) = &topic.name {
self.topic_by_name.insert(
topic_name.clone(),
Topic {
partitions: new_partitions.clone(),
},
);
}
if has_topic_id {
self.topic_by_id.insert(
topic.topic_id,
Topic {
partitions: new_partitions,
},
);
}
}
}
}
async fn process_describe_cluster_response(
&mut self,
describe_cluster: &DescribeClusterResponse,
) {
for broker in &describe_cluster.brokers {
let node = KafkaNode::new(
broker.broker_id,
KafkaAddress::new(broker.host.clone(), broker.port),
broker.rack.clone(),
);
self.add_node_if_new(node).await;
}
tracing::debug!(
"Storing controller metadata, controller is now broker {}",
describe_cluster.controller_id.0
);
self.controller_broker.set(describe_cluster.controller_id);
}
fn process_find_coordinator_response(
&mut self,
version: i16,
request: &FindCoordinator,
find_coordinator: &FindCoordinatorResponse,
) {
if request.key_type == 0 {
if version <= 3 {
if find_coordinator.error_code == 0 {
self.group_to_coordinator_broker
.insert(GroupId(request.key.clone()), find_coordinator.node_id);
}
} else {
for coordinator in &find_coordinator.coordinators {
if coordinator.error_code == 0 {
self.group_to_coordinator_broker
.insert(GroupId(coordinator.key.clone()), coordinator.node_id);
}
}
}
}
}
fn rewrite_find_coordinator_response(
&self,
version: i16,
request: &FindCoordinator,
find_coordinator: &mut FindCoordinatorResponse,
) {
let up_shotover_nodes: Vec<_> = self
.shotover_nodes
.iter()
.filter(|shotover_node| shotover_node.is_up())
.collect();
if version <= 3 {
// For version <= 3 we only have one coordinator to replace,
// so we try to deterministically pick one UP shotover node in the rack of the coordinator.
// If there is no UP shotover node in the rack,
// try to deterministically pick one UP shotover node out of the rack.
// skip rewriting on error
if find_coordinator.error_code == 0 {
let coordinator_rack = &self
.nodes
.iter()
.find(|x| x.broker_id == find_coordinator.node_id)
.unwrap()
.rack
.as_ref();
let hash = hash_str_bytes(request.key.clone());
let shotover_nodes_by_rack =
partition_shotover_nodes_by_rack(&up_shotover_nodes, coordinator_rack);
let shotover_node = select_shotover_node_by_hash(shotover_nodes_by_rack, hash);
find_coordinator.host = shotover_node.address_for_clients.host.clone();
find_coordinator.port = shotover_node.address_for_clients.port;
find_coordinator.node_id = shotover_node.broker_id;
}
} else {
// For version > 3 we have to replace multiple coordinators.
// It may be tempting to include all shotover nodes in the rack of the coordinator, assuming the client will load balance between them.
// However it doesnt work like that.
// AFAIK there can only be one coordinator per unique `FindCoordinatorResponse::key`.
// In fact the java driver doesnt even support multiple coordinators of different types yet:
// https://github.com/apache/kafka/blob/4825c89d14e5f1b2da7e1f48dac97888602028d7/clients/src/main/java/org/apache/kafka/clients/consumer/internals/AbstractCoordinator.java#L921
//
// So, just like with version <= 3, we try to deterministically pick one UP shotover node in the rack of the coordinator for each coordinator.
// If there is no UP shotover node in the rack, try to deterministically pick one UP shotover node out of the rack.
for coordinator in &mut find_coordinator.coordinators {
// skip rewriting on error
if coordinator.error_code == 0 {
let coordinator_rack = &self
.nodes
.iter()
.find(|x| x.broker_id == coordinator.node_id)
.unwrap()
.rack
.as_ref();
let hash = hash_str_bytes(coordinator.key.clone());
let shotover_nodes_by_rack =
partition_shotover_nodes_by_rack(&up_shotover_nodes, coordinator_rack);
let shotover_node = select_shotover_node_by_hash(shotover_nodes_by_rack, hash);
coordinator.host = shotover_node.address_for_clients.host.clone();
coordinator.port = shotover_node.address_for_clients.port;
coordinator.node_id = shotover_node.broker_id;
}
}
}
}
/// Rewrite metadata response to appear as if the shotover cluster is the real cluster and the real kafka brokers do not exist
fn rewrite_metadata_response(&self, metadata: &mut MetadataResponse) -> Result<()> {
// Exclude DOWN shotover nodes in metadata responses
//
// TODO: Ensure metadata responses are still deterministic and consistent even when some shotover nodes are down
// Metadata responses can be temporarily inconsistent when some shotover nodes are DOWN because shotover nodes always consider themselves UP,
// meaning the list of UP shotover nodes can be different on different shotover nodes.
// This scenario should be rare since shotover nodes are usually either accessible to both clients and peer shotover nodes or not at all,
// which means the DOWN shotover nodes should not be able to send the inconsistent responses back to clients.
// This should never be empty since the local shotover node always considers itself UP
let up_shotover_nodes: Vec<_> = self
.shotover_nodes
.iter()
.filter(|shotover_node| shotover_node.is_up())
.collect();
// Overwrite list of brokers with the list of UP shotover nodes
metadata.brokers = up_shotover_nodes
.iter()
.map(|shotover_node| {
MetadataResponseBroker::default()
.with_node_id(shotover_node.broker_id)
.with_host(shotover_node.address_for_clients.host.clone())
.with_port(shotover_node.address_for_clients.port)
.with_rack(Some(shotover_node.rack.clone()))
})
.collect();
// Overwrite the list of partitions to point at UP shotover nodes within the same rack if any
// If there is no UP shotover node within the same rack, fall back to UP shotover nodes out of the rack
for topic in &mut metadata.topics {
for partition in &mut topic.partitions {
// Try to deterministically choose a single UP shotover node in the rack as leader based on topic + partition id
if let Some(leader_rack) = self
.nodes
.iter()
.find(|x| x.broker_id == *partition.leader_id)
.map(|x| x.rack.clone())
{
let hash = hash_partition(topic.topic_id, partition.partition_index);
let shotover_nodes_by_rack =
partition_shotover_nodes_by_rack(&up_shotover_nodes, &leader_rack.as_ref());
let shotover_node = select_shotover_node_by_hash(shotover_nodes_by_rack, hash);
partition.leader_id = shotover_node.broker_id;
} else {
// If the cluster detects the broker is down it will not be included in the metadata list of brokers.
// In that case we wont find a broker and we should just mark the leader as not existing by setting it to -1
// The brokers also set the leader to -1 if they detect it is down, so this matches their behaviour.
partition.leader_id = BrokerId(-1);
}
// Every replica node has its entire corresponding shotover rack included.
// Since we can set as many replica nodes as we like, we take this all out approach.
// This ensures that:
// * metadata is deterministic and therefore the same on all UP shotover nodes (note that metadata can be different on DOWN shotover nodes)
// * clients evenly distribute their queries across UP shotover nodes
let mut shotover_replica_nodes = HashSet::new();
for replica_node in &partition.replica_nodes {
if let Some(rack) = self
.nodes
.iter()
.find(|x| x.broker_id == *replica_node)
.map(|x| x.rack.clone())
{
// If broker has no rack - use all UP shotover nodes.
// If broker has rack - use all UP shotover nodes with the same rack if available,
// and fall back to use all UP shotover nodes out of the rack otherwise.
let shotover_nodes_by_rack =
partition_shotover_nodes_by_rack(&up_shotover_nodes, &rack.as_ref());
let broker_ids = collect_broker_ids(shotover_nodes_by_rack);
shotover_replica_nodes.extend(broker_ids);
} else {
// If the cluster detects the broker is down it will not be included in the metadata list of brokers.
// In that case we wont find a broker and we should just skip this broker.
}
}
partition.replica_nodes = shotover_replica_nodes.into_iter().collect();
// Every isr (in-sync-replica) node has its entire corresponding shotover rack included.
// Since we can set as many isr nodes as we like, we take this all out approach.
// This ensures that:
// * metadata is deterministic and therefore the same on all UP shotover nodes (note that metadata can be different on DOWN shotover nodes)
// * clients evenly distribute their queries across UP shotover nodes
let mut shotover_isr_nodes = HashSet::new();
for replica_node in &partition.isr_nodes {
if let Some(rack) = self
.nodes
.iter()
.find(|x| x.broker_id == *replica_node)
.map(|x| x.rack.clone())
{
// If broker has no rack - use all UP shotover nodes.
// If broker has rack - use all UP shotover nodes with the same rack if available,
// and fall back to use all UP shotover nodes out of the rack otherwise.
let shotover_nodes_by_rack =
partition_shotover_nodes_by_rack(&up_shotover_nodes, &rack.as_ref());
let broker_ids = collect_broker_ids(shotover_nodes_by_rack);
shotover_isr_nodes.extend(broker_ids);
} else {
// If the cluster detects the broker is down it will not be included in the metadata list of brokers.
// In that case we wont find a broker and we should just skip this broker.
}
}
partition.isr_nodes = shotover_isr_nodes.into_iter().collect();
// TODO: handle this properly, for now its better to just clear it than do nothing
partition.offline_replicas.clear();
}
}
self.rewrite_controller_id(&mut metadata.controller_id, &up_shotover_nodes);
Ok(())
}
/// Rewrite DescribeCluster response to appear as if the shotover cluster is the real cluster and the real kafka brokers do not exist
fn rewrite_describe_cluster_response(
&self,
describe_cluster: &mut DescribeClusterResponse,
) -> Result<()> {
// This should never be empty since the local shotover node always considers itself UP
let up_shotover_nodes: Vec<_> = self
.shotover_nodes
.iter()
.filter(|shotover_node| shotover_node.is_up())
.collect();
// Overwrite list of brokers with the list of UP shotover nodes
describe_cluster.brokers = up_shotover_nodes
.iter()
.map(|shotover_node| {
DescribeClusterBroker::default()
.with_broker_id(shotover_node.broker_id)
.with_host(shotover_node.address_for_clients.host.clone())
.with_port(shotover_node.address_for_clients.port)
.with_rack(Some(shotover_node.rack.clone()))
})
.collect();
self.rewrite_controller_id(&mut describe_cluster.controller_id, &up_shotover_nodes);
Ok(())
}
fn rewrite_controller_id(
&self,
controller_id_field: &mut BrokerId,
up_shotover_nodes: &[&ShotoverNode],
) {
if let Some(controller_node_rack) = self
.nodes
.iter()
.find(|node| node.broker_id == *controller_id_field)
.map(|x| &x.rack)
{
// If broker has no rack - use the first UP shotover node.
// If broker has rack - use the first UP shotover node with the same rack if available,
// and fall back to use the first UP shotover node out of the rack otherwise.
// This is deterministic because the list of UP shotover nodes is sorted and partitioning does not change the order.
let shotover_nodes_by_rack =
partition_shotover_nodes_by_rack(up_shotover_nodes, &controller_node_rack.as_ref());
let shotover_node = shotover_nodes_by_rack
.nodes_in_rack
.first()
.or_else(|| shotover_nodes_by_rack.nodes_out_of_rack.first())
.expect("There will always be at least one up shotover node");
*controller_id_field = shotover_node.broker_id;
} else {
// controller is either -1 or an unknown broker
// In both cases it is reasonable to set to -1 to indicate the controller is unknown.
*controller_id_field = BrokerId(-1);
}
}
async fn add_node_if_new(&mut self, new_node: KafkaNode) {
// perform an initial check with read access to allow concurrent access in the vast majority of cases.
let missing_from_shared = self
.nodes_shared
.read()
.await
.iter()
.all(|node| node.broker_id != new_node.broker_id);
if missing_from_shared {
// Need to reperform check now that we have exclusive access to nodes_shared
let mut nodes_shared = self.nodes_shared.write().await;
let missing_from_shared = nodes_shared
.iter()
.all(|node| node.broker_id != new_node.broker_id);
if missing_from_shared {
nodes_shared.push(new_node);
nodes_shared.sort_by_key(|node| node.broker_id);
}
}
// We need to run this every time, not just when missing_from_shared.
// This is because nodes_shared could already contain new_node while its missing from `self.nodes`.
// This could happen when another KafkaSinkCluster instance updates nodes_shared just before we read from it.
self.update_local_nodes().await;
}
fn broker_within_rack(&self, broker_id: BrokerId) -> bool {
self.nodes.iter().any(|node| {
node.broker_id == broker_id
&& node
.rack
.as_ref()
.map(|rack| rack == &self.rack)
.unwrap_or(false)
})
}
}
#[derive(Debug)]
enum CoordinatorKey {
Group(GroupId),
Transaction(TransactionalId),
}
fn hash_partition(topic_id: Uuid, partition_index: i32) -> usize {
let mut hasher = xxhash_rust::xxh3::Xxh3::new();
hasher.write(topic_id.as_bytes());
hasher.write(&partition_index.to_be_bytes());
hasher.finish() as usize
}
fn hash_str_bytes(str_bytes: StrBytes) -> usize {
let mut hasher = xxhash_rust::xxh3::Xxh3::new();
hasher.write(str_bytes.as_bytes());
hasher.finish() as usize
}
#[derive(Debug)]
struct Topic {
partitions: Vec<Partition>,
}
#[derive(Debug, Clone)]
struct Partition {
index: i32,
leader_epoch: i32,
leader_id: BrokerId,
shotover_rack_replica_nodes: Vec<BrokerId>,
external_rack_replica_nodes: Vec<BrokerId>,
}
#[derive(Debug, Clone)]
struct FindCoordinator {
key: StrBytes,
key_type: i8,
}
fn get_username_from_scram_request(auth_request: &[u8]) -> Option<String> {
for s in std::str::from_utf8(auth_request).ok()?.split(',') {
let mut iter = s.splitn(2, '=');
if let (Some(key), Some(value)) = (iter.next(), iter.next()) {
if key == "n" {
return Some(value.to_owned());
}
}
}
None
}
// Chooses a random broker id from the list of nodes, prioritizes "Up" nodes but fallsback to "down" nodes if needed.
fn random_broker_id_in_any_rack(nodes: &[KafkaNode], rng: &mut SmallRng) -> BrokerId {
match nodes.iter().filter(|node| node.is_up()).choose(rng) {
Some(broker) => broker.broker_id,
None => nodes.choose(rng).unwrap().broker_id,
}
}
// Chooses a random broker id within the given rack from the list of nodes, prioritizes "Up" nodes
// if no node is satisfied, fallback to the random_broker_id_in_any_rack function.
fn random_broker_id(nodes: &[KafkaNode], rng: &mut SmallRng, rack: &StrBytes) -> BrokerId {
match nodes
.iter()
.filter(|node| node.rack.as_ref() == Some(rack) && node.is_up())
.choose(rng)
{
Some(broker) => broker.broker_id,
None => random_broker_id_in_any_rack(nodes, rng),
}
}
struct FormatTopicName<'a>(&'a TopicName, &'a Uuid);
impl std::fmt::Display for FormatTopicName<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.0.is_empty() {
write!(f, "topic with id {}", self.1)
} else {
write!(f, "topic with name {:?}", self.0.as_str())
}
}
}
struct ShotoverNodesByRack<'a> {
nodes_in_rack: Vec<&'a ShotoverNode>,
nodes_out_of_rack: Vec<&'a ShotoverNode>,
}
/// Partitions the given shotover nodes based on their rack placement.
/// Returns `ShotoverNodesByRack` consisting of `nodes_in_rack` and `nodes_out_of_rack`.
/// If rack is None, all shotover nodes are considered to be in the rack.
fn partition_shotover_nodes_by_rack<'a>(
shotover_nodes: &[&'a ShotoverNode],
rack: &Option<&StrBytes>,
) -> ShotoverNodesByRack<'a> {
let (nodes_in_rack, nodes_out_of_rack) = shotover_nodes
.iter()
.partition(|shotover_node| rack.map(|rack| rack == &shotover_node.rack).unwrap_or(true));
ShotoverNodesByRack {
nodes_in_rack,
nodes_out_of_rack,
}
}
/// Tries to deterministically select a shotover node based on a hash value.
/// Prioritizes nodes in the rack if available, falls back to nodes out of the rack.
/// Panics if `nodes_in_rack` and `nodes_out_of_rack` are both empty.
/// The local shotover node is always UP, so this should never panic.
fn select_shotover_node_by_hash(
shotover_nodes_by_rack: ShotoverNodesByRack<'_>,
hash: usize,
) -> &ShotoverNode {
let nodes = if !shotover_nodes_by_rack.nodes_in_rack.is_empty() {
&shotover_nodes_by_rack.nodes_in_rack
} else {
&shotover_nodes_by_rack.nodes_out_of_rack
};
nodes
.get(hash % nodes.len())
.expect("There must be at least one shotover node")
}
/// Returns a list of broker IDs from available shotover nodes.
/// Prioritizes nodes in the rack if available, falls back to nodes out of the rack.
fn collect_broker_ids(shotover_nodes_by_rack: ShotoverNodesByRack) -> Vec<BrokerId> {
let nodes = if !shotover_nodes_by_rack.nodes_in_rack.is_empty() {
&shotover_nodes_by_rack.nodes_in_rack
} else {
&shotover_nodes_by_rack.nodes_out_of_rack
};
nodes.iter().map(|node| node.broker_id).collect()
}
struct ResponseToBeCombined {
response: Message,
destination: Destination,
}