krabka-client-admin 0.7.0

Operator-side admin client for Krabka clusters
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
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
//! Transaction administration.

use std::collections::{BTreeMap, BTreeSet};

use bytes::BufMut;
use krabka_client_core::{
    Connection, ConnectionOptions, CoordinatorKeyType, build_find_coordinator,
};
use krabka_protocol::{
    Encode, ProtocolError, ProtocolRequest,
    owned::{
        describe_producers_request::{
            DescribeProducersRequest, TopicRequest as DescribeProducersTopicRequest,
        },
        describe_producers_response::{
            DescribeProducersResponse, ProducerState as WireProducerState,
        },
        describe_transactions_request::DescribeTransactionsRequest,
        describe_transactions_response::{DescribeTransactionsResponse, TransactionState},
        find_coordinator_response::FindCoordinatorResponse,
        init_producer_id_request::{self, InitProducerIdRequest},
        init_producer_id_response::InitProducerIdResponse,
        list_transactions_request::ListTransactionsRequest,
        list_transactions_response::{
            ListTransactionsResponse, TransactionState as ListedTransactionState,
        },
        metadata_request::{MetadataRequest, MetadataRequestTopic},
        metadata_response::MetadataResponse,
        write_txn_markers_request::{
            WritableTxnMarker, WritableTxnMarkerTopic, WriteTxnMarkersRequest,
        },
        write_txn_markers_response::WriteTxnMarkersResponse,
    },
    primitives::uuid::Uuid as ProtoUuid,
};
use krabka_units::{Time, convert::TimeExt as _};

#[cfg(test)]
use crate::AdminClientConfig;
use crate::{
    AdminClient, AdminError, KafkaError, format_host_port, kafka_error_name,
    partition_leaders::{PartitionKey, PartitionResults, complete_results},
    retry::{CoordinatorRetry, RetryAction, RetryDeadline, RetryPolicy, connection_failure_action},
};

/// `COORDINATOR_LOAD_IN_PROGRESS`: the coordinator is loading its state.
const COORDINATOR_LOAD_IN_PROGRESS: i16 = 14;
/// `COORDINATOR_NOT_AVAILABLE`: no broker coordinates the transactional ID now.
const COORDINATOR_NOT_AVAILABLE: i16 = 15;
/// `NOT_COORDINATOR`: the broker does not coordinate the transactional ID.
const NOT_COORDINATOR: i16 = 16;
/// `CONCURRENT_TRANSACTIONS`: the coordinator is completing a transaction.
const CONCURRENT_TRANSACTIONS: i16 = 51;
/// `LEADER_NOT_AVAILABLE`: the metadata has no leader for a partition yet.
const LEADER_NOT_AVAILABLE: i16 = 5;
/// `NOT_LEADER_OR_FOLLOWER`: the broker is no longer (or not yet) the leader
/// of a partition.
const NOT_LEADER_OR_FOLLOWER: i16 = 6;

/// Map the result of one `DescribeTransactions` attempt to a retry action, as
/// Kafka's `DescribeTransactionsHandler.handleError` does. 14 retries on the
/// same coordinator. 15 and 16 unmap the transactional ID, so the next attempt
/// finds the coordinator again. Every other result is final.
fn describe_retry_action(
    result: Result<TransactionDescription, AdminError>,
) -> RetryAction<TransactionDescription> {
    match result {
        Err(AdminError::Broker {
            code: COORDINATOR_LOAD_IN_PROGRESS,
            ..
        }) => RetryAction::SameCoordinator(result),
        Err(AdminError::Broker {
            code: COORDINATOR_NOT_AVAILABLE | NOT_COORDINATOR,
            ..
        }) => RetryAction::FindCoordinator(result),
        result => RetryAction::Done(result),
    }
}

/// Map the error code of one `InitProducerId` answer to a retry action, as
/// Kafka's `FenceProducersHandler.handleError` does. 14 and 51 retry on the
/// same coordinator. 15 and 16 unmap the transactional ID, so the next attempt
/// finds the coordinator again. Every other code is final.
fn fence_retry_action(error_code: i16) -> RetryAction<()> {
    if error_code == 0 {
        return RetryAction::Done(Ok(()));
    }
    let result = Err(AdminError::Broker {
        api: "InitProducerId",
        code: error_code,
        name: kafka_error_name(error_code),
        message: None,
    });
    match error_code {
        COORDINATOR_LOAD_IN_PROGRESS | CONCURRENT_TRANSACTIONS => {
            RetryAction::SameCoordinator(result)
        }
        COORDINATOR_NOT_AVAILABLE | NOT_COORDINATOR => RetryAction::FindCoordinator(result),
        _ => RetryAction::Done(result),
    }
}

/// The transaction coordinator's view of one transactional ID, as Kafka's
/// `TransactionDescription`.
///
/// A third party reads [`producer_id`](Self::producer_id) and
/// [`producer_epoch`](Self::producer_epoch) to verify the authority of a
/// writer. The coordinator raises the epoch each time it fences the previous
/// producer generation, so a writer that presents an older epoch no longer
/// holds authority over the transactional ID.
#[derive(Debug, Clone, PartialEq)]
pub struct TransactionDescription {
    /// The transactional ID that the caller asked about.
    pub transactional_id: String,
    /// The broker ID of the transaction coordinator that answered.
    pub coordinator_id: i32,
    /// The coordinator's state name, such as `Ongoing` or `CompleteCommit`.
    pub state: String,
    /// The producer ID that the coordinator holds for this transactional ID.
    pub producer_id: i64,
    /// The producer epoch that the coordinator holds for this transactional
    /// ID.
    pub producer_epoch: i16,
    /// The transaction timeout that the producer registered.
    pub timeout: Time,
    /// The start of the current transaction, in Kafka epoch milliseconds, or
    /// `None` when no transaction is open. The coordinator reports `-1` for
    /// none, and Kafka maps any negative value to an empty `OptionalLong`. An
    /// instant is a coordinate, so it stays a raw integer.
    pub start_time_ms: Option<i64>,
    /// The partitions of the current transaction. While the transaction
    /// prepares to commit or abort, only the partitions that have no marker
    /// yet.
    pub topic_partitions: BTreeSet<(String, i32)>,
}

/// An open connection to the transaction coordinator of one transactional
/// ID.
struct TransactionCoordinator {
    /// The broker ID that `FindCoordinator` named.
    node_id: i32,
    connection: Connection,
}

/// One transaction that `list_transactions` found on a broker, as Kafka's
/// `TransactionListing`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TransactionListing {
    /// The transactional ID.
    pub transactional_id: String,
    /// The producer ID currently bound to the transactional ID.
    pub producer_id: i64,
    /// The coordinator's state name, such as `Ongoing` or `CompleteCommit`.
    pub state: String,
}

/// Narrows a [`AdminClient::list_transactions`] call, as Kafka's
/// `ListTransactionsOptions` does. The default matches every transaction, as
/// Kafka's default options do.
#[derive(Debug, Clone, Default, PartialEq, bon::Builder)]
pub struct ListTransactionsFilter {
    /// Only transactions whose state name is in this list. Empty matches
    /// every state.
    #[builder(default)]
    pub state_filters: Vec<String>,
    /// Only transactions whose producer ID is in this list. Empty matches
    /// every producer ID.
    #[builder(default)]
    pub producer_id_filters: Vec<i64>,
    /// Only transactions that have run for at least this long. `None`
    /// matches every duration, as Kafka's `-1` filter does.
    #[builder(with = |millis: i64| Time::from_millis(millis))]
    pub min_duration: Option<Time>,
}

/// One producer's state on one partition, as Kafka's `ProducerState`
/// (`DescribeProducers`, KIP-664).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProducerStateInfo {
    /// The producer ID.
    pub producer_id: i64,
    /// The producer epoch.
    pub producer_epoch: i32,
    /// The last sequence number the producer wrote, or `-1` when it has
    /// written nothing yet.
    pub last_sequence: i32,
    /// The timestamp of the last write, in Kafka epoch milliseconds. An
    /// instant is a coordinate, so it stays a raw integer.
    pub last_timestamp_ms: i64,
    /// The epoch of the coordinator that last fenced this producer.
    pub coordinator_epoch: i32,
    /// The start offset of the producer's open transaction on this
    /// partition, or `None` when it has no open transaction.
    pub current_txn_start_offset: Option<i64>,
}

/// The broker ID and the address of the coordinator that a
/// `FindCoordinator` answer names for `transactional_id`.
fn coordinator_address(
    transactional_id: &str,
    response: FindCoordinatorResponse,
) -> Result<(i32, String), AdminError> {
    if let Some(coordinator) = response
        .coordinators
        .into_iter()
        .find(|coordinator| coordinator.key == transactional_id)
    {
        if coordinator.error_code != 0 {
            return Err(AdminError::Broker {
                api: "FindCoordinator",
                code: coordinator.error_code,
                name: kafka_error_name(coordinator.error_code),
                message: coordinator.error_message,
            });
        }
        return Ok((
            coordinator.node_id,
            format!("{}:{}", coordinator.host, coordinator.port),
        ));
    }

    if response.error_code != 0 {
        return Err(AdminError::Broker {
            api: "FindCoordinator",
            code: response.error_code,
            name: kafka_error_name(response.error_code),
            message: response.error_message,
        });
    }
    if response.host.is_empty() {
        return Err(AdminError::Protocol(format!(
            "FindCoordinator returned no entry for transactional id {transactional_id:?}"
        )));
    }
    Ok((
        response.node_id,
        format!("{}:{}", response.host, response.port),
    ))
}

/// An `InitProducerId` request limited to the released versions, v0 to v5.
///
/// `InitProducerIdRequest.json` marks v6 as its unstable latest version
/// (`"latestVersionUnstable": true`, KIP-939), and krabka-protocol's
/// generated `MAX_VERSION` includes v6. Apache Kafka's
/// `FenceProducersHandler.buildSingleRequest` uses
/// `new InitProducerIdRequest.Builder(data)`. That builder calls
/// `AbstractRequest.Builder(ApiKeys.INIT_PRODUCER_ID)`, which allows
/// `apiKey.latestVersion(false)`, the latest released version. This type
/// gives the same cap to version negotiation.
#[derive(Clone, Debug, PartialEq, Eq)]
struct ReleasedInitProducerId(InitProducerIdRequest);

impl Encode for ReleasedInitProducerId {
    fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
        self.0.encode(buf, version)
    }

    fn encoded_len(&self, version: i16) -> usize {
        self.0.encoded_len(version)
    }
}

impl ProtocolRequest for ReleasedInitProducerId {
    const API_KEY: i16 = init_producer_id_request::API_KEY;
    const MIN_VERSION: i16 = init_producer_id_request::MIN_VERSION;
    /// The latest released `InitProducerId` version.
    const MAX_VERSION: i16 = 5;
    /// The cap is a released version, so it is also the stable maximum.
    const LATEST_STABLE_VERSION: i16 = Self::MAX_VERSION;
    const FLEXIBLE_MIN: i16 = init_producer_id_request::FLEXIBLE_MIN;
    type Response = InitProducerIdResponse;
}

fn force_terminate_request(
    transactional_id: &str,
    transaction_timeout_ms: i32,
) -> ReleasedInitProducerId {
    ReleasedInitProducerId(InitProducerIdRequest {
        transactional_id: Some(transactional_id.to_owned()),
        transaction_timeout_ms,
        producer_id: -1,
        producer_epoch: -1,
        enable2_pc: false,
        keep_prepared_txn: false,
        ..Default::default()
    })
}

fn describe_transactions_request(transactional_id: &str) -> DescribeTransactionsRequest {
    DescribeTransactionsRequest {
        transactional_ids: vec![transactional_id.to_owned()],
        ..Default::default()
    }
}

/// Converts one row of coordinator `coordinator_id` into the domain struct,
/// as Kafka's `DescribeTransactionsHandler.handleResponse` does, or surfaces
/// the error code that the coordinator attached to that row.
fn described_transaction(
    coordinator_id: i32,
    state: TransactionState,
) -> Result<TransactionDescription, AdminError> {
    if state.error_code != 0 {
        return Err(AdminError::Broker {
            api: "DescribeTransactions",
            code: state.error_code,
            name: kafka_error_name(state.error_code),
            message: None,
        });
    }
    Ok(TransactionDescription {
        transactional_id: state.transactional_id,
        coordinator_id,
        state: state.transaction_state,
        producer_id: state.producer_id,
        producer_epoch: state.producer_epoch,
        timeout: Time::from_millis(i64::from(state.transaction_timeout_ms)),
        start_time_ms: (state.transaction_start_time_ms >= 0)
            .then_some(state.transaction_start_time_ms),
        topic_partitions: state
            .topics
            .into_iter()
            .flat_map(|topic| {
                topic
                    .partitions
                    .into_iter()
                    .map(move |partition| (topic.topic.clone(), partition))
            })
            .collect(),
    })
}

/// Picks the row for `transactional_id` out of the `DescribeTransactions`
/// response of coordinator `coordinator_id` and maps it to the domain struct.
fn transaction_description(
    transactional_id: &str,
    coordinator_id: i32,
    response: DescribeTransactionsResponse,
) -> Result<TransactionDescription, AdminError> {
    response
        .transaction_states
        .into_iter()
        .find(|state| state.transactional_id == transactional_id)
        .ok_or_else(|| {
            AdminError::Protocol(format!(
                "DescribeTransactions returned no state for transactional id {transactional_id:?}"
            ))
        })
        .and_then(|state| described_transaction(coordinator_id, state))
}

/// A `Metadata` request for `topics`, as Kafka's leader-routed handlers send
/// it. An empty slice asks for the broker list only, with no topics, as
/// `list_transactions` uses it.
fn build_topic_metadata(topics: &[&str]) -> MetadataRequest {
    MetadataRequest {
        topics: if topics.is_empty() {
            Some(Vec::new())
        } else {
            Some(
                topics
                    .iter()
                    .map(|name| MetadataRequestTopic {
                        topic_id: ProtoUuid::ZERO,
                        name: Some((*name).to_owned()),
                        ..Default::default()
                    })
                    .collect(),
            )
        },
        allow_auto_topic_creation: false,
        ..Default::default()
    }
}

/// The `host:port` of the leader of `topic`-`partition` in `metadata`, as
/// Kafka's `PartitionLeaderStrategy` resolves it.
fn partition_leader_address(
    topic: &str,
    partition: i32,
    metadata: &MetadataResponse,
) -> Result<String, AdminError> {
    let topic_metadata = metadata
        .topics
        .iter()
        .find(|entry| entry.name.as_deref() == Some(topic))
        .ok_or_else(|| {
            AdminError::Protocol(format!("Metadata returned no entry for topic {topic:?}"))
        })?;
    if topic_metadata.error_code != 0 {
        return Err(AdminError::Broker {
            api: "Metadata",
            code: topic_metadata.error_code,
            name: kafka_error_name(topic_metadata.error_code),
            message: None,
        });
    }
    let partition_metadata = topic_metadata
        .partitions
        .iter()
        .find(|entry| entry.partition_index == partition)
        .ok_or_else(|| {
            AdminError::Protocol(format!(
                "Metadata returned no entry for partition {topic}-{partition}"
            ))
        })?;
    if partition_metadata.error_code != 0 {
        return Err(AdminError::Broker {
            api: "Metadata",
            code: partition_metadata.error_code,
            name: kafka_error_name(partition_metadata.error_code),
            message: None,
        });
    }
    metadata
        .brokers
        .iter()
        .find(|broker| broker.node_id == partition_metadata.leader_id)
        .map(|broker| format_host_port(&broker.host, broker.port))
        .ok_or_else(|| {
            AdminError::Protocol(format!(
                "Metadata named leader {} for {topic}-{partition} but listed no such broker",
                partition_metadata.leader_id
            ))
        })
}

/// The identity of a transaction to abort, as Kafka's `AbortTransactionSpec`
/// bundles it: the partition it last wrote to, and the producer generation
/// that owns it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AbortTransactionSpec {
    /// The topic that the transaction wrote to.
    pub topic: String,
    /// The partition that the transaction wrote to.
    pub partition: i32,
    /// The producer ID of the transaction to fence.
    pub producer_id: i64,
    /// The producer epoch of the transaction to fence.
    pub producer_epoch: i16,
    /// The epoch of the coordinator that assigned `producer_id`.
    pub coordinator_epoch: i32,
}

/// The `WriteTxnMarkers` request that aborts the transaction of `spec`.
fn abort_transaction_request(spec: &AbortTransactionSpec) -> WriteTxnMarkersRequest {
    WriteTxnMarkersRequest {
        markers: vec![WritableTxnMarker {
            producer_id: spec.producer_id,
            producer_epoch: spec.producer_epoch,
            transaction_result: false,
            topics: vec![WritableTxnMarkerTopic {
                name: spec.topic.clone(),
                partition_indexes: vec![spec.partition],
                ..Default::default()
            }],
            coordinator_epoch: spec.coordinator_epoch,
            ..Default::default()
        }],
        ..Default::default()
    }
}

/// Picks the result of `spec`'s topic and partition out of a `WriteTxnMarkers`
/// response and maps it to an [`AdminError`].
fn abort_transaction_result(
    spec: &AbortTransactionSpec,
    response: WriteTxnMarkersResponse,
) -> Result<(), AdminError> {
    let marker = response
        .markers
        .into_iter()
        .find(|marker| marker.producer_id == spec.producer_id)
        .ok_or_else(|| {
            AdminError::Protocol(format!(
                "WriteTxnMarkers returned no result for producer id {}",
                spec.producer_id
            ))
        })?;
    let topic_result = marker
        .topics
        .into_iter()
        .find(|entry| entry.name == spec.topic)
        .ok_or_else(|| {
            AdminError::Protocol(format!(
                "WriteTxnMarkers returned no result for topic {:?}",
                spec.topic
            ))
        })?;
    let partition_result = topic_result
        .partitions
        .into_iter()
        .find(|entry| entry.partition_index == spec.partition)
        .ok_or_else(|| {
            AdminError::Protocol(format!(
                "WriteTxnMarkers returned no result for partition {}-{}",
                spec.topic, spec.partition
            ))
        })?;
    if partition_result.error_code != 0 {
        return Err(AdminError::Broker {
            api: "WriteTxnMarkers",
            code: partition_result.error_code,
            name: kafka_error_name(partition_result.error_code),
            message: None,
        });
    }
    Ok(())
}

/// Maps the outcome of one `WriteTxnMarkers` attempt to a retry action.
/// `NOT_LEADER_OR_FOLLOWER` and `LEADER_NOT_AVAILABLE` look the leader up
/// again, as Kafka's `AbortTransactionHandler.handleResponse` does. Every
/// other outcome is final.
fn abort_transaction_retry_action(result: Result<(), AdminError>) -> RetryAction<()> {
    match result {
        Err(AdminError::Broker {
            code: NOT_LEADER_OR_FOLLOWER | LEADER_NOT_AVAILABLE,
            ..
        }) => RetryAction::FindCoordinator(result),
        result => RetryAction::Done(result),
    }
}

/// The `DescribeProducers` request that asks for every partition in `keys`,
/// grouped by topic, as Kafka's `DescribeProducersHandler.buildBatchedRequest`
/// does.
fn describe_producers_request(keys: &[(String, i32)]) -> DescribeProducersRequest {
    let mut by_topic = BTreeMap::<&str, Vec<i32>>::new();
    for (topic, partition) in keys {
        by_topic.entry(topic.as_str()).or_default().push(*partition);
    }
    DescribeProducersRequest {
        topics: by_topic
            .into_iter()
            .map(|(name, partition_indexes)| DescribeProducersTopicRequest {
                name: name.to_owned(),
                partition_indexes,
                ..Default::default()
            })
            .collect(),
        ..Default::default()
    }
}

/// Converts one wire `ProducerState` row into the domain struct. `-1` marks
/// no open transaction, as Kafka's `ProducerState` does.
fn producer_state_info(state: &WireProducerState) -> ProducerStateInfo {
    ProducerStateInfo {
        producer_id: state.producer_id,
        producer_epoch: state.producer_epoch,
        last_sequence: state.last_sequence,
        last_timestamp_ms: state.last_timestamp,
        coordinator_epoch: state.coordinator_epoch,
        current_txn_start_offset: (state.current_txn_start_offset != -1)
            .then_some(state.current_txn_start_offset),
    }
}

/// Maps a `DescribeProducers` response to a result for each of `keys`. A
/// partition that the response omits gets `UNKNOWN_SERVER_ERROR`, as Kafka's
/// `DescribeProducersHandler.handleResponse` does for a missing result.
fn describe_producers_results(
    keys: &[PartitionKey],
    response: DescribeProducersResponse,
) -> PartitionResults<Vec<ProducerStateInfo>> {
    let mut out = BTreeMap::new();
    for topic in response.topics {
        for partition in topic.partitions {
            let result = if partition.error_code == 0 {
                Ok(partition
                    .active_producers
                    .iter()
                    .map(producer_state_info)
                    .collect())
            } else {
                Err(KafkaError {
                    code: partition.error_code,
                    name: kafka_error_name(partition.error_code),
                    message: partition.error_message,
                })
            };
            out.insert((topic.name.clone(), partition.partition_index), result);
        }
    }
    complete_results("DescribeProducers", keys, out)
}

/// Whether a `DescribeProducers` partition code sends the partition back to
/// the leader lookup. Kafka's `DescribeProducersHandler.handlePartitionError`
/// unmaps the partition on `NOT_LEADER_OR_FOLLOWER` only.
const fn describe_producers_retry_code(code: i16) -> bool {
    code == NOT_LEADER_OR_FOLLOWER
}

/// Sends one `DescribeProducers` request for `keys` to the leader at
/// `address`.
async fn describe_producers_on_leader(
    address: String,
    options: ConnectionOptions,
    keys: Vec<PartitionKey>,
) -> Result<PartitionResults<Vec<ProducerStateInfo>>, AdminError> {
    let connection = AdminClient::connect_one(&address, options).await?;
    let response = connection.send(describe_producers_request(&keys)).await?;
    Ok(describe_producers_results(&keys, response))
}

/// The `ListTransactions` request of `filter`, as Kafka's
/// `ListTransactionsHandler.buildRequest` builds it for every broker.
fn list_transactions_request(filter: &ListTransactionsFilter) -> ListTransactionsRequest {
    ListTransactionsRequest {
        state_filters: filter.state_filters.clone(),
        producer_id_filters: filter.producer_id_filters.clone(),
        duration_filter: filter
            .min_duration
            .map_or(-1, krabka_units::convert::TimeExt::millis_i64),
        ..Default::default()
    }
}

/// Converts one wire `TransactionState` row into the domain struct.
fn transaction_listing(state: ListedTransactionState) -> TransactionListing {
    TransactionListing {
        transactional_id: state.transactional_id,
        producer_id: state.producer_id,
        state: state.transaction_state,
    }
}

/// Sends `request` to one broker and returns its transaction listings, or the
/// broker's top-level error.
async fn list_transactions_on_broker(
    address: String,
    options: ConnectionOptions,
    request: ListTransactionsRequest,
) -> Result<Vec<TransactionListing>, AdminError> {
    let connection = AdminClient::connect_one(&address, options).await?;
    let response: ListTransactionsResponse = connection.send(request).await?;
    if response.error_code != 0 {
        return Err(AdminError::Broker {
            api: "ListTransactions",
            code: response.error_code,
            name: kafka_error_name(response.error_code),
            message: None,
        });
    }
    Ok(response
        .transaction_states
        .into_iter()
        .map(transaction_listing)
        .collect())
}

impl AdminClient {
    /// Fences the current producer generation and aborts any ongoing
    /// transaction for `transactional_id`.
    ///
    /// This is Kafka's `forceTerminateTransaction` operation: it discovers the
    /// transaction coordinator and sends `InitProducerId` with no producer
    /// identity and `keepPreparedTxn=false`. It is safe to call when no
    /// transaction is open; the coordinator still advances the producer
    /// generation so stale writers are fenced.
    ///
    /// The client negotiates `InitProducerId` v5 or lower, as Kafka's
    /// `FenceProducersHandler` does. v6 is an unstable version.
    ///
    /// The call retries as Apache Kafka's `FenceProducersHandler.handleError` does:
    ///
    /// - `COORDINATOR_LOAD_IN_PROGRESS` (14) and `CONCURRENT_TRANSACTIONS` (51): send the request again to the same coordinator.
    /// - `COORDINATOR_NOT_AVAILABLE` (15) and `NOT_COORDINATOR` (16): find the
    ///   coordinator again, then send the request again.
    ///
    /// A `FindCoordinator` answer of 14 or 15, and a failed or lost connection
    /// to the coordinator (including a TLS or SASL handshake that stops
    /// before the broker gives a verdict), also make the call find the
    /// coordinator again. The call waits between attempts with Kafka's
    /// backoff, and it stops when Kafka's default `default.api.timeout.ms`
    /// (60 s) elapses. A call whose retries are unresolved at the deadline, or
    /// whose attempt is still running, fails with a timeout error, as Kafka
    /// gives a `TimeoutException`.
    ///
    /// # Errors
    ///
    /// Returns [`AdminError::Protocol`] for an empty transactional ID, or the
    /// coordinator lookup, connection, transport, and broker errors returned
    /// by Kafka. Returns [`ClientError::IncompatibleVersion`] when the
    /// coordinator does not support `InitProducerId` v5 or lower.
    ///
    /// [`ClientError::IncompatibleVersion`]: krabka_client_core::ClientError::IncompatibleVersion
    pub async fn force_terminate_transaction(
        &self,
        transactional_id: &str,
    ) -> Result<(), AdminError> {
        if transactional_id.is_empty() {
            return Err(AdminError::Protocol(
                "transactional id must not be empty".to_owned(),
            ));
        }

        self.force_terminate_transaction_with_retry(transactional_id, self.retry)
            .await
    }

    async fn force_terminate_transaction_with_retry(
        &self,
        transactional_id: &str,
        retry: RetryPolicy,
    ) -> Result<(), AdminError> {
        self.force_terminate_transaction_until(transactional_id, retry.start())
            .await
    }

    /// `force_terminate_transaction` with a call deadline that already runs,
    /// so that every ID of one `fence_producers` call shares one deadline.
    async fn force_terminate_transaction_until(
        &self,
        transactional_id: &str,
        deadline: RetryDeadline,
    ) -> Result<(), AdminError> {
        let mut coordinator = None;
        let mut retry = CoordinatorRetry::from_deadline(deadline);
        loop {
            let action = retry
                .run(self.force_terminate_attempt(
                    transactional_id,
                    &mut coordinator,
                    retry.find_coordinator(),
                ))
                .await;
            if let Some(result) = retry.next(action).await {
                return result;
            }
        }
    }

    /// Fences the current producer generation and aborts any ongoing
    /// transaction for each ID in `transactional_ids`, as Kafka's
    /// `fenceProducers` operation does.
    ///
    /// Two transactional IDs can live on two different coordinators, so the
    /// client fences them at the same time rather than one at a time. All IDs
    /// share one call deadline, as Kafka's `FenceProducersHandler` and
    /// `AdminApiDriver` run one `invokeDriver` for the whole call. The result
    /// has one entry for each ID in `transactional_ids`, in no particular
    /// order.
    ///
    /// See [`Self::force_terminate_transaction`] for the retry behavior and
    /// the `InitProducerId` version that the client negotiates.
    ///
    /// # Errors
    ///
    /// Each entry gets [`AdminError::Protocol`] for an empty transactional
    /// ID, or the coordinator lookup, connection, transport, and broker
    /// errors returned by Kafka for that ID.
    pub async fn fence_producers(
        &self,
        transactional_ids: &[&str],
    ) -> BTreeMap<String, Result<(), AdminError>> {
        let deadline = self.retry.start();
        futures_util::future::join_all(transactional_ids.iter().map(|transactional_id| {
            let transactional_id = (*transactional_id).to_owned();
            async move {
                let result = if transactional_id.is_empty() {
                    Err(AdminError::Protocol(
                        "transactional id must not be empty".to_owned(),
                    ))
                } else {
                    self.force_terminate_transaction_until(&transactional_id, deadline)
                        .await
                };
                (transactional_id, result)
            }
        }))
        .await
        .into_iter()
        .collect()
    }

    /// Fences the current producer generation and aborts the ongoing
    /// transaction that wrote to `topic`-`partition`, without contacting the
    /// transaction coordinator.
    ///
    /// This is Kafka's `abortTransaction` operation (KIP-664): it sends
    /// `WriteTxnMarkers` straight to the partition leader with the identity of
    /// the transaction to fence, as `AbortTransactionHandler` does. A
    /// consumer that reads `describe_producers` or the partition's records
    /// supplies `spec`.
    ///
    /// The call retries as Apache Kafka's
    /// `AbortTransactionHandler.handleResponse` does: `NOT_LEADER_OR_FOLLOWER`
    /// (6) and `LEADER_NOT_AVAILABLE` (5), from either the leader lookup or
    /// the `WriteTxnMarkers` answer, look the leader up again. A failed or
    /// lost connection to the leader does the same. Every other broker error
    /// is final. The call waits between attempts with Kafka's backoff, and it
    /// stops when Kafka's default `default.api.timeout.ms` (60 s) elapses.
    ///
    /// # Errors
    ///
    /// Returns [`AdminError::Protocol`] for an empty topic name, or when the
    /// broker's answer names no result for the requested producer, topic, or
    /// partition. Returns [`AdminError::Broker`] when the leader lookup or the
    /// `WriteTxnMarkers` call fails, such as with
    /// `TRANSACTIONAL_ID_AUTHORIZATION_FAILED` or `FENCED_INSTANCE_ID`.
    /// Returns [`AdminError::Transport`] when the connection to the leader
    /// fails.
    pub async fn abort_transaction(&self, spec: &AbortTransactionSpec) -> Result<(), AdminError> {
        if spec.topic.is_empty() {
            return Err(AdminError::Protocol("topic must not be empty".to_owned()));
        }

        let mut leader = None;
        let mut retry = CoordinatorRetry::new(self.retry);
        loop {
            let action = retry
                .run(self.abort_transaction_attempt(spec, &mut leader, retry.find_coordinator()))
                .await;
            if let Some(result) = retry.next(action).await {
                return result;
            }
        }
    }

    /// One attempt of `abort_transaction`.
    async fn abort_transaction_attempt(
        &self,
        spec: &AbortTransactionSpec,
        leader: &mut Option<Connection>,
        find_leader: bool,
    ) -> RetryAction<()> {
        let connection = match self
            .partition_leader_connection(&spec.topic, spec.partition, leader, find_leader)
            .await
        {
            Ok(connection) => connection,
            Err(action) => return action,
        };
        match connection.send(abort_transaction_request(spec)).await {
            Ok(response) => {
                abort_transaction_retry_action(abort_transaction_result(spec, response))
            }
            Err(error) => connection_failure_action(error.into()),
        }
    }

    /// Returns the connection to the leader of `topic`-`partition`. When
    /// `find_leader` is set, or when no connection is open, the attempt first
    /// fetches fresh metadata and connects to the leader that it names.
    async fn partition_leader_connection<'c, T>(
        &self,
        topic: &str,
        partition: i32,
        leader: &'c mut Option<Connection>,
        find_leader: bool,
    ) -> Result<&'c Connection, RetryAction<T>> {
        if find_leader {
            *leader = None;
        }
        if let Some(connection) = leader {
            return Ok(connection);
        }
        let metadata = self
            .conn
            .send(build_topic_metadata(&[topic]))
            .await
            .map_err(connection_failure_action)?;
        let address = match partition_leader_address(topic, partition, &metadata) {
            Ok(address) => address,
            Err(
                error @ AdminError::Broker {
                    code: NOT_LEADER_OR_FOLLOWER | LEADER_NOT_AVAILABLE,
                    ..
                },
            ) => return Err(RetryAction::FindCoordinator(Err(error))),
            Err(error) => return Err(RetryAction::Done(Err(error))),
        };
        match Self::connect_one(&address, self.options.clone()).await {
            Ok(connection) => Ok(leader.insert(connection)),
            Err(error) => Err(connection_failure_action(error)),
        }
    }

    /// Reads the producer state of each requested partition, as Kafka's
    /// `describeProducers` operation does (KIP-664).
    ///
    /// The client finds the leader of each partition with `Metadata` and
    /// sends one `DescribeProducers` request to each leader, all at the same
    /// time, batching every partition of that leader into it, as Kafka's
    /// `DescribeProducersHandler` with its `PartitionLeaderStrategy` does. The
    /// result has one entry for each requested `(topic, partition)` pair.
    ///
    /// A partition goes back to the leader lookup, and the call finds its
    /// leader again after the backoff, when:
    ///
    /// - the lookup names no leader yet, such as for an unknown topic or a
    ///   partition with `LEADER_NOT_AVAILABLE`;
    /// - the connection to its leader fails or is lost;
    /// - the leader answers `NOT_LEADER_OR_FOLLOWER` (6) for it.
    ///
    /// A slow or failed leader does not delay the other leaders. The call
    /// stops at Kafka's default `default.api.timeout.ms` (60 s), where each
    /// unresolved partition gets `REQUEST_TIMED_OUT` (7).
    ///
    /// # Errors
    ///
    /// The call itself does not fail. Each partition gets its own
    /// [`KafkaError`] in the result, such as the final lookup or
    /// `DescribeProducers` error of that partition.
    pub async fn describe_producers(
        &self,
        partitions: &[(String, i32)],
    ) -> BTreeMap<(String, i32), Result<Vec<ProducerStateInfo>, KafkaError>> {
        self.call_partition_leaders(
            "DescribeProducers",
            partitions,
            describe_producers_retry_code,
            |address, keys| describe_producers_on_leader(address, self.options.clone(), keys),
        )
        .await
    }

    /// Lists every transaction that the cluster's coordinators currently
    /// hold, as Kafka's `listTransactions` operation does (KIP-664).
    ///
    /// The client fetches the broker list from `Metadata` and sends
    /// `ListTransactions` to every broker at the same time, since each
    /// transaction coordinator answers only for the transactions it
    /// coordinates. `filter` narrows the state, producer ID, and minimum
    /// duration of the listed transactions, as Kafka's
    /// `ListTransactionsOptions` does; the default matches every transaction.
    ///
    /// # Errors
    ///
    /// Returns [`AdminError::Broker`] when any broker gives a top-level
    /// error, and [`AdminError::Transport`] when the connection to any broker
    /// fails. A partial listing from the healthy brokers is not returned;
    /// callers that need per-broker results should read
    /// [`AdminError`] for which broker failed and retry.
    pub async fn list_transactions(
        &self,
        filter: &ListTransactionsFilter,
    ) -> Result<Vec<TransactionListing>, AdminError> {
        let metadata = self.conn.send(build_topic_metadata(&[])).await?;
        let request = list_transactions_request(filter);
        let options = self.options.clone();
        let answers = futures_util::future::join_all(metadata.brokers.into_iter().map(|broker| {
            list_transactions_on_broker(
                format_host_port(&broker.host, broker.port),
                options.clone(),
                request.clone(),
            )
        }))
        .await;

        let mut listings = Vec::new();
        for answer in answers {
            listings.extend(answer?);
        }
        Ok(listings)
    }

    /// One attempt of `force_terminate_transaction`.
    async fn force_terminate_attempt(
        &self,
        transactional_id: &str,
        coordinator: &mut Option<TransactionCoordinator>,
        find_coordinator: bool,
    ) -> RetryAction<()> {
        let coordinator = match self
            .transaction_coordinator(transactional_id, coordinator, find_coordinator)
            .await
        {
            Ok(coordinator) => coordinator,
            Err(action) => return action,
        };
        let request =
            force_terminate_request(transactional_id, self.options.request_timeout.millis_i32());
        match coordinator.connection.send(request).await {
            Ok(response) => fence_retry_action(response.error_code),
            Err(error) => connection_failure_action(error.into()),
        }
    }

    /// Reads the transaction coordinator's current state for one transactional
    /// ID.
    ///
    /// This is Kafka's `describeTransactions` operation (KIP-664). The client
    /// discovers the transaction coordinator for `transactional_id` and sends
    /// `DescribeTransactions` to that coordinator. An external system calls
    /// this to verify the authority of a writer. It joins no group and holds
    /// no producer state. The returned producer ID and producer epoch are the
    /// only generation from which the coordinator accepts writes.
    ///
    /// The call retries as Apache Kafka's `DescribeTransactionsHandler.handleError` does:
    ///
    /// - `COORDINATOR_LOAD_IN_PROGRESS` (14): send the request again to the same coordinator.
    /// - `COORDINATOR_NOT_AVAILABLE` (15) and `NOT_COORDINATOR` (16): find the
    ///   coordinator again, then send the request again.
    ///
    /// A `FindCoordinator` answer of 14 or 15, and a failed or lost connection
    /// to the coordinator (including a TLS or SASL handshake that stops
    /// before the broker gives a verdict), also make the call find the
    /// coordinator again. The call waits between attempts with Kafka's
    /// backoff, and it stops when Kafka's default `default.api.timeout.ms`
    /// (60 s) elapses. A call whose retries are unresolved at the deadline, or
    /// whose attempt is still running, fails with a timeout error, as Kafka
    /// gives a `TimeoutException`.
    ///
    /// # Errors
    ///
    /// Returns [`AdminError::Protocol`] for an empty transactional ID, and for
    /// a response that carries no row for `transactional_id`. Returns
    /// [`AdminError::Broker`] when the coordinator lookup fails, or when the
    /// coordinator attaches an error code to the row, such as
    /// `TRANSACTIONAL_ID_NOT_FOUND`,
    /// `TRANSACTIONAL_ID_AUTHORIZATION_FAILED`, or `NOT_COORDINATOR`. Returns
    /// [`AdminError::Transport`] when the connection to the coordinator fails.
    pub async fn describe_transaction(
        &self,
        transactional_id: &str,
    ) -> Result<TransactionDescription, AdminError> {
        if transactional_id.is_empty() {
            return Err(AdminError::Protocol(
                "transactional id must not be empty".to_owned(),
            ));
        }

        self.describe_transaction_with_retry(transactional_id, self.retry)
            .await
    }

    async fn describe_transaction_with_retry(
        &self,
        transactional_id: &str,
        retry: RetryPolicy,
    ) -> Result<TransactionDescription, AdminError> {
        self.describe_transaction_until(transactional_id, retry.start())
            .await
    }

    /// `describe_transaction` with a call deadline that already runs, so that
    /// the IDs of one `describe_transactions` call share one deadline.
    async fn describe_transaction_until(
        &self,
        transactional_id: &str,
        deadline: RetryDeadline,
    ) -> Result<TransactionDescription, AdminError> {
        let mut coordinator = None;
        let mut retry = CoordinatorRetry::from_deadline(deadline);
        loop {
            let action = retry
                .run(self.describe_transaction_attempt(
                    transactional_id,
                    &mut coordinator,
                    retry.find_coordinator(),
                ))
                .await;
            if let Some(result) = retry.next(action).await {
                return result;
            }
        }
    }

    /// One attempt of `describe_transaction`.
    async fn describe_transaction_attempt(
        &self,
        transactional_id: &str,
        coordinator: &mut Option<TransactionCoordinator>,
        find_coordinator: bool,
    ) -> RetryAction<TransactionDescription> {
        let coordinator = match self
            .transaction_coordinator(transactional_id, coordinator, find_coordinator)
            .await
        {
            Ok(coordinator) => coordinator,
            Err(action) => return action,
        };
        match coordinator
            .connection
            .send(describe_transactions_request(transactional_id))
            .await
        {
            Ok(response) => describe_retry_action(transaction_description(
                transactional_id,
                coordinator.node_id,
                response,
            )),
            Err(error) => connection_failure_action(error.into()),
        }
    }

    /// Returns the transaction coordinator of `transactional_id`. When
    /// `find_coordinator` is set, or when no connection is open, the attempt
    /// first sends `FindCoordinator` and connects to the coordinator that it
    /// names.
    ///
    /// A `FindCoordinator` answer of `COORDINATOR_LOAD_IN_PROGRESS` (14) or
    /// `COORDINATOR_NOT_AVAILABLE` (15) and a failed connection to the
    /// coordinator give [`RetryAction::FindCoordinator`], as Kafka's
    /// `CoordinatorStrategy.handleError` and `AdminApiDriver.onFailure` do.
    async fn transaction_coordinator<'c, T>(
        &self,
        transactional_id: &str,
        coordinator: &'c mut Option<TransactionCoordinator>,
        find_coordinator: bool,
    ) -> Result<&'c TransactionCoordinator, RetryAction<T>> {
        if find_coordinator {
            *coordinator = None;
        }
        if let Some(coordinator) = coordinator {
            return Ok(coordinator);
        }
        let response = self
            .conn
            .send(build_find_coordinator(
                transactional_id,
                CoordinatorKeyType::Transaction,
            ))
            .await
            .map_err(connection_failure_action)?;
        let (node_id, address) = match coordinator_address(transactional_id, response) {
            Ok(found) => found,
            Err(
                error @ AdminError::Broker {
                    code: COORDINATOR_LOAD_IN_PROGRESS | COORDINATOR_NOT_AVAILABLE,
                    ..
                },
            ) => return Err(RetryAction::FindCoordinator(Err(error))),
            Err(error) => return Err(RetryAction::Done(Err(error))),
        };
        match Self::connect_one(&address, self.options.clone()).await {
            Ok(connection) => Ok(coordinator.insert(TransactionCoordinator {
                node_id,
                connection,
            })),
            Err(error) => Err(connection_failure_action(error)),
        }
    }

    /// Reads the transaction coordinator's current state for each ID in
    /// `transactional_ids`.
    ///
    /// Two transactional IDs can live on two different coordinators, so the
    /// client describes them one at a time. Each ID costs one
    /// `FindCoordinator` round trip, one new connection to the coordinator
    /// that the lookup names, and one `DescribeTransactions` round trip. The
    /// cost grows linearly with the number of IDs. Callers that describe a
    /// large set should expect that cost. The results keep the order of
    /// `transactional_ids`.
    ///
    /// # Errors
    ///
    /// Stops at the first ID that fails and returns its error. Returns
    /// [`AdminError::Protocol`] for an empty transactional ID, and for a
    /// response that carries no row for the ID. Returns
    /// [`AdminError::Broker`] when the coordinator lookup fails, or when the
    /// coordinator attaches an error code to the row. Returns
    /// [`AdminError::Transport`] when the connection to a coordinator fails.
    pub async fn describe_transactions(
        &self,
        transactional_ids: &[&str],
    ) -> Result<Vec<TransactionDescription>, AdminError> {
        // Kafka's `describeTransactions` runs all IDs under one deadline
        // (`invokeDriver` with one `deadlineMs`).
        let deadline = self.retry.start();
        let mut descriptions = Vec::with_capacity(transactional_ids.len());
        for transactional_id in transactional_ids {
            if transactional_id.is_empty() {
                return Err(AdminError::Protocol(
                    "transactional id must not be empty".to_owned(),
                ));
            }
            descriptions.push(
                self.describe_transaction_until(transactional_id, deadline)
                    .await?,
            );
        }
        Ok(descriptions)
    }
}

#[cfg(test)]
mod tests {
    use std::{
        sync::{Arc, Mutex},
        time::Duration,
    };

    use assert2::check;
    use bytes::{Buf, BytesMut};
    use krabka_client_core::{ClientError, MockBroker, MockReply};
    use krabka_protocol::{
        Decode,
        owned::{
            api_versions_request,
            api_versions_response::{ApiVersion, ApiVersionsResponse},
            describe_producers_request,
            describe_producers_response::{PartitionResponse, TopicResponse},
            describe_transactions_request,
            describe_transactions_response::TopicData,
            find_coordinator_request,
            find_coordinator_response::Coordinator,
            list_transactions_request, metadata_request,
            metadata_response::{
                MetadataResponse, MetadataResponseBroker, MetadataResponsePartition,
                MetadataResponseTopic,
            },
            write_txn_markers_request,
            write_txn_markers_response::{
                WritableTxnMarkerPartitionResult, WritableTxnMarkerResult,
                WritableTxnMarkerTopicResult,
            },
        },
    };

    use super::*;
    use crate::partition_leaders::test_support::{
        ApiVersionRange, Seen, fast_admin as leader_fast_admin,
        leader_broker as partition_leader_broker,
    };

    fn state_row(transactional_id: &str, error_code: i16) -> TransactionState {
        TransactionState {
            error_code,
            transactional_id: transactional_id.to_owned(),
            ..Default::default()
        }
    }

    /// The `InitProducerId` request that fences `payments` with no producer
    /// identity and no kept prepared transaction.
    fn fence_payments() -> InitProducerIdRequest {
        InitProducerIdRequest {
            transactional_id: Some("payments".to_owned()),
            transaction_timeout_ms: 30_000,
            producer_id: -1,
            producer_epoch: -1,
            enable2_pc: false,
            keep_prepared_txn: false,
            ..Default::default()
        }
    }

    #[test]
    fn force_termination_request_fences_without_preserving_transaction() {
        let request = force_terminate_request("payments", 30_000);

        assert2::assert!(request == ReleasedInitProducerId(fence_payments()));
    }

    /// The body of `response` at `version`, behind an empty tagged-field
    /// byte when `flexible` is set.
    fn encode_response(response: &impl Encode, version: i16, flexible: bool) -> Vec<u8> {
        let mut bytes = BytesMut::new();
        if flexible {
            bytes.extend_from_slice(&[0]);
        }
        response
            .encode(&mut bytes, version)
            .expect("response encodes");
        bytes.to_vec()
    }

    /// An `ApiVersions` response that advertises `InitProducerId` in
    /// `init_producer_id_range`.
    fn api_versions(init_producer_id_range: (i16, i16)) -> Vec<u8> {
        let api_version = |api_key, min_version, max_version| ApiVersion {
            api_key,
            min_version,
            max_version,
            ..Default::default()
        };
        encode_response(
            &ApiVersionsResponse {
                api_keys: vec![
                    api_version(api_versions_request::API_KEY, 0, 0),
                    api_version(find_coordinator_request::API_KEY, 0, 0),
                    api_version(metadata_request::API_KEY, 13, 13),
                    api_version(
                        init_producer_id_request::API_KEY,
                        init_producer_id_range.0,
                        init_producer_id_range.1,
                    ),
                ],
                ..Default::default()
            },
            0,
            false,
        )
    }

    /// The negotiated `InitProducerId` version and the result of
    /// `force_terminate_transaction`, with a version error as its ranges.
    type FenceResult = Result<(), (i16, i16, i16, i16, i16)>;

    /// Apache Kafka's `FenceProducersHandler` builds `InitProducerId` with
    /// `new InitProducerIdRequest.Builder(data)`, which allows only released
    /// versions. v6 is unstable, so the request stops at v5.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn force_terminate_transaction_sends_a_released_init_producer_id_version() {
        let sent_request = |version| vec![(version, fence_payments())];
        for (name, init_producer_id_range, expected_requests, expected_result) in [
            ("coordinator stops at v1", (0, 1), sent_request(1), Ok(())),
            ("coordinator stops at v4", (0, 4), sent_request(4), Ok(())),
            ("coordinator stops at v5", (0, 5), sent_request(5), Ok(())),
            (
                "coordinator supports unstable v6",
                (0, 6),
                sent_request(5),
                Ok(()),
            ),
            (
                "coordinator supports only unstable v6",
                (6, 6),
                Vec::new(),
                Err((init_producer_id_request::API_KEY, 6, 6, 0, 5)),
            ),
        ] {
            let requests = Arc::new(Mutex::new(Vec::new()));
            let requests_in_mock = Arc::clone(&requests);
            let coordinator =
                MockBroker::start(move |api_key, version, _, mut body| match api_key {
                    api_versions_request::API_KEY => Some(api_versions(init_producer_id_range)),
                    init_producer_id_request::API_KEY => {
                        let client_id_len = body.get_i16();
                        body.advance(usize::try_from(client_id_len).expect("client id length"));
                        let flexible = version >= init_producer_id_request::FLEXIBLE_MIN;
                        if flexible {
                            body.advance(1);
                        }
                        let request = InitProducerIdRequest::decode(&mut body, version)
                            .expect("init producer id request decodes");
                        requests_in_mock
                            .lock()
                            .expect("requests lock")
                            .push((version, request));
                        Some(encode_response(
                            &InitProducerIdResponse {
                                producer_id: 4242,
                                producer_epoch: 1,
                                ..Default::default()
                            },
                            version,
                            flexible,
                        ))
                    }
                    _ => None,
                })
                .await;
            let coordinator_addr = coordinator.addr;
            let bootstrap = MockBroker::start(move |api_key, version, _, _| match api_key {
                api_versions_request::API_KEY => Some(api_versions(init_producer_id_range)),
                find_coordinator_request::API_KEY => Some(encode_response(
                    &FindCoordinatorResponse {
                        node_id: 2,
                        host: coordinator_addr.ip().to_string(),
                        port: i32::from(coordinator_addr.port()),
                        ..Default::default()
                    },
                    version,
                    false,
                )),
                metadata_request::API_KEY => {
                    Some(encode_response(&MetadataResponse::default(), version, true))
                }
                _ => None,
            })
            .await;
            let admin = AdminClient::connect(&[bootstrap.addr.to_string()])
                .await
                .expect("admin connects");

            let result: FenceResult =
                admin
                    .force_terminate_transaction("payments")
                    .await
                    .map_err(|error| match error {
                        AdminError::Transport(ClientError::IncompatibleVersion {
                            api_key,
                            broker_min,
                            broker_max,
                            client_min,
                            client_max,
                        }) => (api_key, broker_min, broker_max, client_min, client_max),
                        other => panic!("case {name}: unexpected error {other:?}"),
                    });

            bootstrap.stop();
            coordinator.stop();
            let requests = requests.lock().expect("requests lock").clone();
            assert2::assert!(
                (requests, result) == (expected_requests, expected_result),
                "case {name}"
            );
        }
    }

    /// Kafka's `CoordinatorStrategy` reads the batched entry of the key from
    /// v4 on, and the top-level fields of an older answer.
    #[test]
    fn coordinator_lookup_names_the_node_and_address_of_the_key() {
        for (name, response) in [
            (
                "batched entry",
                FindCoordinatorResponse {
                    coordinators: vec![
                        Coordinator {
                            key: "other".to_owned(),
                            node_id: 1,
                            host: "wrong".to_owned(),
                            port: 1,
                            ..Default::default()
                        },
                        Coordinator {
                            key: "payments".to_owned(),
                            node_id: 3,
                            host: "coordinator".to_owned(),
                            port: 9092,
                            ..Default::default()
                        },
                    ],
                    ..Default::default()
                },
            ),
            (
                "top-level fields",
                FindCoordinatorResponse {
                    node_id: 3,
                    host: "coordinator".to_owned(),
                    port: 9092,
                    ..Default::default()
                },
            ),
        ] {
            let found = coordinator_address("payments", response).expect("matching coordinator");
            assert2::assert!(found == (3, "coordinator:9092".to_owned()), "case {name}");
        }
    }

    #[test]
    fn describe_request_asks_for_exactly_one_transactional_id() {
        let request = describe_transactions_request("payments");

        assert2::assert!(
            request
                == DescribeTransactionsRequest {
                    transactional_ids: vec!["payments".to_owned()],
                    ..Default::default()
                }
        );
    }

    /// Kafka's `DescribeTransactionsHandler.handleResponse` names the
    /// coordinator that answered, maps a negative start time to an empty
    /// `OptionalLong`, and collects the partitions of every topic.
    #[test]
    fn description_maps_the_matching_row_to_the_domain_struct() {
        let topic = |topic: &str, partitions: Vec<i32>| TopicData {
            topic: topic.to_owned(),
            partitions,
            ..Default::default()
        };
        let partitions = |entries: &[(&str, i32)]| {
            entries
                .iter()
                .map(|(topic, partition)| ((*topic).to_owned(), *partition))
                .collect::<BTreeSet<_>>()
        };
        for (name, state, start, topics, expected_start, expected_partitions) in [
            (
                "ongoing with partitions of two topics",
                "Ongoing",
                1_700_000_000_123,
                vec![topic("orders", vec![1, 0]), topic("audit", vec![2])],
                Some(1_700_000_000_123),
                partitions(&[("audit", 2), ("orders", 0), ("orders", 1)]),
            ),
            (
                "empty with no start time",
                "Empty",
                -1,
                Vec::new(),
                None,
                BTreeSet::new(),
            ),
            (
                "start time at the epoch",
                "Ongoing",
                0,
                Vec::new(),
                Some(0),
                BTreeSet::new(),
            ),
        ] {
            let response = DescribeTransactionsResponse {
                transaction_states: vec![
                    TransactionState {
                        transactional_id: "other".to_owned(),
                        producer_id: 11,
                        producer_epoch: 1,
                        ..Default::default()
                    },
                    TransactionState {
                        transactional_id: "payments".to_owned(),
                        transaction_state: state.to_owned(),
                        transaction_timeout_ms: 60_000,
                        transaction_start_time_ms: start,
                        producer_id: 4242,
                        producer_epoch: 7,
                        topics,
                        ..Default::default()
                    },
                ],
                ..Default::default()
            };

            let described = transaction_description("payments", 3, response).expect("matching row");

            assert2::assert!(
                described
                    == TransactionDescription {
                        transactional_id: "payments".to_owned(),
                        coordinator_id: 3,
                        state: state.to_owned(),
                        producer_id: 4242,
                        producer_epoch: 7,
                        timeout: Time::from_millis(60_000),
                        start_time_ms: expected_start,
                        topic_partitions: expected_partitions,
                    },
                "case {name}"
            );
        }
    }

    #[test]
    fn description_surfaces_the_error_code_of_the_matching_row() {
        for (_case, code, want_name) in [
            ("unknown id", 105, "TRANSACTIONAL_ID_NOT_FOUND"),
            (
                "not authorized",
                53,
                "TRANSACTIONAL_ID_AUTHORIZATION_FAILED",
            ),
            ("moved coordinator", 16, "NOT_COORDINATOR"),
            ("loading", 14, "COORDINATOR_LOAD_IN_PROGRESS"),
        ] {
            let response = DescribeTransactionsResponse {
                transaction_states: vec![state_row("other", 0), state_row("payments", code)],
                ..Default::default()
            };

            let error = transaction_description("payments", 3, response).expect_err("broker error");
            match error {
                AdminError::Broker {
                    api,
                    code: got,
                    name,
                    message,
                } => {
                    check!(
                        (api, got, name, message)
                            == ("DescribeTransactions", code, want_name, None)
                    );
                }
                other => panic!("expected AdminError::Broker, got {other:?}"),
            }
        }
    }

    #[test]
    fn description_rejects_a_response_that_omits_the_requested_id() {
        let response = DescribeTransactionsResponse {
            transaction_states: vec![state_row("other", 0)],
            ..Default::default()
        };

        let error = transaction_description("payments", 3, response).expect_err("missing row");
        assert2::assert!(matches!(error, AdminError::Protocol(_)));
    }

    /// The transaction admin call of a retry case.
    #[derive(Clone, Copy, Debug)]
    enum TransactionCall {
        Describe,
        ForceTerminate,
    }

    /// The error codes that the mock brokers answer, one per request. The last
    /// code repeats.
    #[derive(Default)]
    struct CoordinatorScript {
        find_coordinator: Vec<i16>,
        coordinator: Vec<i16>,
        find_coordinator_requests: usize,
        coordinator_requests: usize,
        /// The first `unreachable_answers` `FindCoordinator` answers name
        /// `unreachable`, an address that refuses connections.
        unreachable_answers: usize,
        unreachable: Option<std::net::SocketAddr>,
        /// The coordinator does not answer the transaction request.
        silent: bool,
        /// The first `closed_find_coordinators` `FindCoordinator` requests
        /// close the connection with no answer.
        closed_find_coordinators: usize,
    }

    /// How the coordinator of a retry case behaves.
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    enum CoordinatorBehavior {
        /// It answers from the script.
        Normal,
        /// The first `FindCoordinator` answer names an address that refuses
        /// connections.
        UnreachableOnce,
        /// It does not answer the transaction request.
        Silent,
        /// The bootstrap broker closes the connection on the first
        /// `FindCoordinator`.
        FindCoordinatorClosedOnce,
    }

    /// An address on which no listener accepts connections.
    async fn refused_address() -> std::net::SocketAddr {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind a port");
        listener.local_addr().expect("local address")
    }

    fn next_code(codes: &[i16], requests: &mut usize) -> i16 {
        let code = codes[(*requests).min(codes.len() - 1)];
        *requests += 1;
        code
    }

    /// An `ApiVersions` response for the retry mocks.
    fn retry_api_versions() -> Vec<u8> {
        let api_version = |api_key, min_version, max_version| ApiVersion {
            api_key,
            min_version,
            max_version,
            ..Default::default()
        };
        encode_response(
            &ApiVersionsResponse {
                api_keys: vec![
                    api_version(api_versions_request::API_KEY, 0, 0),
                    api_version(find_coordinator_request::API_KEY, 0, 0),
                    api_version(metadata_request::API_KEY, 13, 13),
                    api_version(init_producer_id_request::API_KEY, 0, 0),
                    api_version(describe_transactions_request::API_KEY, 0, 0),
                ],
                ..Default::default()
            },
            0,
            false,
        )
    }

    /// A mock broker that answers `FindCoordinator` with `coordinator`, and
    /// `DescribeTransactions` and `InitProducerId` with the codes of `script`.
    async fn scripted_transaction_broker(
        script: Arc<Mutex<CoordinatorScript>>,
        coordinator: Arc<Mutex<Option<std::net::SocketAddr>>>,
    ) -> MockBroker {
        MockBroker::start_with_replies(move |api_key, version, _, _| {
            let mut script = script.lock().expect("script lock");
            let script = &mut *script;
            if api_key == find_coordinator_request::API_KEY && script.closed_find_coordinators > 0 {
                script.closed_find_coordinators -= 1;
                script.find_coordinator_requests += 1;
                return MockReply::Close;
            }
            let reply = match api_key {
                api_versions_request::API_KEY => Some(retry_api_versions()),
                find_coordinator_request::API_KEY => {
                    let error_code = next_code(
                        &script.find_coordinator,
                        &mut script.find_coordinator_requests,
                    );
                    let addr = match script.unreachable {
                        Some(unreachable)
                            if script.find_coordinator_requests <= script.unreachable_answers =>
                        {
                            unreachable
                        }
                        _ => coordinator
                            .lock()
                            .expect("coordinator lock")
                            .expect("coordinator address"),
                    };
                    Some(encode_response(
                        &FindCoordinatorResponse {
                            error_code,
                            node_id: 2,
                            host: addr.ip().to_string(),
                            port: i32::from(addr.port()),
                            ..Default::default()
                        },
                        version,
                        false,
                    ))
                }
                describe_transactions_request::API_KEY => {
                    let error_code =
                        next_code(&script.coordinator, &mut script.coordinator_requests);
                    if script.silent {
                        return MockReply::Silent;
                    }
                    Some(encode_response(
                        &DescribeTransactionsResponse {
                            transaction_states: vec![TransactionState {
                                error_code,
                                transactional_id: "payments".to_owned(),
                                transaction_state: "Ongoing".to_owned(),
                                producer_id: 4242,
                                producer_epoch: 7,
                                ..Default::default()
                            }],
                            ..Default::default()
                        },
                        version,
                        true,
                    ))
                }
                init_producer_id_request::API_KEY => {
                    let error_code =
                        next_code(&script.coordinator, &mut script.coordinator_requests);
                    if script.silent {
                        return MockReply::Silent;
                    }
                    Some(encode_response(
                        &InitProducerIdResponse {
                            error_code,
                            producer_id: 4242,
                            producer_epoch: 8,
                            ..Default::default()
                        },
                        version,
                        false,
                    ))
                }
                _ => None,
            };
            reply.map_or(MockReply::Silent, MockReply::Respond)
        })
        .await
    }

    /// A call that stops at its deadline gives a timeout. The retry tests give
    /// it Kafka's `REQUEST_TIMED_OUT` code.
    const TIMED_OUT: i16 = 7;
    /// A deadline that several attempts fit in.
    const LONG: Duration = Duration::from_secs(5);
    /// One attempt fits in `NOW`, and the backoff after it reaches the
    /// deadline, so the call makes no second attempt.
    const NOW: Duration = Duration::from_millis(500);
    /// A deadline shorter than the request timeout.
    const SHORT: Duration = Duration::from_millis(300);

    type TransactionOutcome = Result<(), (&'static str, i16)>;

    type TransactionCase = (
        &'static str,
        TransactionCall,
        Vec<i16>,
        Vec<i16>,
        (Duration, CoordinatorBehavior),
        (TransactionOutcome, usize, usize),
    );

    const OK: TransactionOutcome = Ok(());

    fn failed(api: &'static str, code: i16) -> TransactionOutcome {
        Err((api, code))
    }

    /// Run one retry case against a scripted bootstrap broker and
    /// coordinator.
    async fn run_transaction_case(case: TransactionCase) {
        use TransactionCall::{Describe, ForceTerminate};

        let (name, call, find_coordinator, coordinator, (timeout, behavior), expected) = case;
        let backoff = if timeout == LONG {
            Duration::from_millis(1)
        } else {
            timeout
        };
        let script = Arc::new(Mutex::new(CoordinatorScript {
            find_coordinator,
            coordinator,
            unreachable_answers: usize::from(behavior == CoordinatorBehavior::UnreachableOnce),
            unreachable: Some(refused_address().await),
            silent: behavior == CoordinatorBehavior::Silent,
            closed_find_coordinators: usize::from(
                behavior == CoordinatorBehavior::FindCoordinatorClosedOnce,
            ),
            ..CoordinatorScript::default()
        }));
        let coordinator_addr = Arc::new(Mutex::new(None));
        let coordinator =
            scripted_transaction_broker(Arc::clone(&script), Arc::clone(&coordinator_addr)).await;
        *coordinator_addr.lock().expect("coordinator lock") = Some(coordinator.addr);
        let bootstrap =
            scripted_transaction_broker(Arc::clone(&script), Arc::clone(&coordinator_addr)).await;
        let admin = AdminClient::connect(&[bootstrap.addr.to_string()])
            .await
            .expect("admin connects");
        let retry = RetryPolicy {
            timeout,
            initial_backoff: backoff,
            max_backoff: backoff,
            jitter: 0.0,
            max_retries: u32::MAX,
        };

        let started = tokio::time::Instant::now();
        let result = match call {
            Describe => admin
                .describe_transaction_with_retry("payments", retry)
                .await
                .map(|_| ()),
            ForceTerminate => {
                admin
                    .force_terminate_transaction_with_retry("payments", retry)
                    .await
            }
        }
        .map_err(|error| match error {
            AdminError::Broker { api, code, .. } => (api, code),
            AdminError::Transport(ClientError::Timeout(_)) => ("timeout", TIMED_OUT),
            other => panic!("case {name}: unexpected error {other:?}"),
        });

        bootstrap.stop();
        coordinator.stop();
        let script = script.lock().expect("script lock");
        // A call never runs far past its deadline.
        let within_deadline = started.elapsed() < timeout + Duration::from_secs(2);
        assert2::assert!(
            (
                result,
                script.find_coordinator_requests,
                script.coordinator_requests,
                within_deadline
            ) == (expected.0, expected.1, expected.2, true),
            "case {name}"
        );
    }

    /// Apache Kafka's `DescribeTransactionsHandler.handleError` retries
    /// `COORDINATOR_LOAD_IN_PROGRESS` (14) on the same coordinator.
    /// `FenceProducersHandler.handleError` retries 14 and
    /// `CONCURRENT_TRANSACTIONS` (51) on the same coordinator. Both unmap the
    /// transactional ID on `COORDINATOR_NOT_AVAILABLE` (15) and
    /// `NOT_COORDINATOR` (16), so the driver finds the coordinator again, and
    /// fail on every other code. `CoordinatorStrategy.handleError` retries a
    /// `FindCoordinator` answer of 14 or 15. The driver stops at the call
    /// timeout.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn transaction_calls_retry_coordinator_errors_as_kafka_does() {
        use TransactionCall::{Describe, ForceTerminate};

        for case in [
            (
                "describe: no error",
                Describe,
                vec![0],
                vec![0],
                (LONG, CoordinatorBehavior::Normal),
                (OK, 1, 1),
            ),
            (
                "describe: coordinator load in progress retries on the same coordinator",
                Describe,
                vec![0],
                vec![14, 14, 0],
                (LONG, CoordinatorBehavior::Normal),
                (OK, 1, 3),
            ),
            (
                "describe: not coordinator finds the coordinator again",
                Describe,
                vec![0],
                vec![16, 0],
                (LONG, CoordinatorBehavior::Normal),
                (OK, 2, 2),
            ),
            (
                "describe: coordinator not available finds the coordinator again",
                Describe,
                vec![0],
                vec![15, 0],
                (LONG, CoordinatorBehavior::Normal),
                (OK, 2, 2),
            ),
            (
                "describe: concurrent transactions is final",
                Describe,
                vec![0],
                vec![51],
                (LONG, CoordinatorBehavior::Normal),
                (failed("DescribeTransactions", 51), 1, 1),
            ),
            (
                "describe: transactional id not found is final",
                Describe,
                vec![0],
                vec![105],
                (LONG, CoordinatorBehavior::Normal),
                (failed("DescribeTransactions", 105), 1, 1),
            ),
            (
                "describe: coordinator load in progress past the timeout times out",
                Describe,
                vec![0],
                vec![14],
                (NOW, CoordinatorBehavior::Normal),
                (failed("timeout", TIMED_OUT), 1, 1),
            ),
            (
                "describe: find coordinator not available, then the coordinator",
                Describe,
                vec![15, 0],
                vec![0],
                (LONG, CoordinatorBehavior::Normal),
                (OK, 2, 1),
            ),
            (
                "describe: find coordinator authorization failed is final",
                Describe,
                vec![53],
                vec![0],
                (LONG, CoordinatorBehavior::Normal),
                (failed("FindCoordinator", 53), 1, 0),
            ),
            (
                "fence: no error",
                ForceTerminate,
                vec![0],
                vec![0],
                (LONG, CoordinatorBehavior::Normal),
                (OK, 1, 1),
            ),
            (
                "fence: coordinator load in progress retries on the same coordinator",
                ForceTerminate,
                vec![0],
                vec![14, 0],
                (LONG, CoordinatorBehavior::Normal),
                (OK, 1, 2),
            ),
            (
                "fence: concurrent transactions retries on the same coordinator",
                ForceTerminate,
                vec![0],
                vec![51, 51, 0],
                (LONG, CoordinatorBehavior::Normal),
                (OK, 1, 3),
            ),
            (
                "fence: not coordinator finds the coordinator again",
                ForceTerminate,
                vec![0],
                vec![16, 0],
                (LONG, CoordinatorBehavior::Normal),
                (OK, 2, 2),
            ),
            (
                "fence: cluster authorization failed is final",
                ForceTerminate,
                vec![0],
                vec![31],
                (LONG, CoordinatorBehavior::Normal),
                (failed("InitProducerId", 31), 1, 1),
            ),
            (
                "fence: concurrent transactions past the timeout times out",
                ForceTerminate,
                vec![0],
                vec![51],
                (NOW, CoordinatorBehavior::Normal),
                (failed("timeout", TIMED_OUT), 1, 1),
            ),
            (
                "fence: find coordinator load in progress, then the coordinator",
                ForceTerminate,
                vec![14, 0],
                vec![0],
                (LONG, CoordinatorBehavior::Normal),
                (OK, 2, 1),
            ),
        ] {
            run_transaction_case(case).await;
        }
    }

    /// Kafka's `AdminApiDriver.onFailure` looks the coordinator up again
    /// after a disconnect, and `KafkaAdminClient` times out a call in flight
    /// at its deadline.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn transaction_calls_find_the_coordinator_again_after_connection_failures() {
        use TransactionCall::{Describe, ForceTerminate};

        for case in [
            (
                "describe: a closed connection during FindCoordinator recovers",
                Describe,
                vec![0],
                vec![0],
                (LONG, CoordinatorBehavior::FindCoordinatorClosedOnce),
                (OK, 2, 1),
            ),
            (
                "fence: a closed connection during FindCoordinator recovers",
                ForceTerminate,
                vec![0],
                vec![0],
                (LONG, CoordinatorBehavior::FindCoordinatorClosedOnce),
                (OK, 2, 1),
            ),
            (
                "describe: an unreachable coordinator finds the coordinator again",
                Describe,
                vec![0],
                vec![0],
                (LONG, CoordinatorBehavior::UnreachableOnce),
                (OK, 2, 1),
            ),
            (
                "describe: a silent coordinator stops the attempt at the deadline",
                Describe,
                vec![0],
                vec![0],
                (SHORT, CoordinatorBehavior::Silent),
                (failed("timeout", TIMED_OUT), 1, 1),
            ),
            (
                "fence: an unreachable coordinator finds the coordinator again",
                ForceTerminate,
                vec![0],
                vec![0],
                (LONG, CoordinatorBehavior::UnreachableOnce),
                (OK, 2, 1),
            ),
            (
                "fence: a silent coordinator stops the attempt at the deadline",
                ForceTerminate,
                vec![0],
                vec![0],
                (SHORT, CoordinatorBehavior::Silent),
                (failed("timeout", TIMED_OUT), 1, 1),
            ),
        ] {
            run_transaction_case(case).await;
        }
    }

    /// Kafka's `DescribeTransactionsHandler` names the broker that answered
    /// as the coordinator, which is the node that `FindCoordinator` gave.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn describe_transaction_names_the_coordinator_that_find_coordinator_gave() {
        let script = Arc::new(Mutex::new(CoordinatorScript {
            find_coordinator: vec![0],
            coordinator: vec![0],
            ..CoordinatorScript::default()
        }));
        let coordinator_addr = Arc::new(Mutex::new(None));
        let coordinator =
            scripted_transaction_broker(Arc::clone(&script), Arc::clone(&coordinator_addr)).await;
        *coordinator_addr.lock().expect("coordinator lock") = Some(coordinator.addr);
        let bootstrap =
            scripted_transaction_broker(Arc::clone(&script), Arc::clone(&coordinator_addr)).await;
        let admin = AdminClient::connect(&[bootstrap.addr.to_string()])
            .await
            .expect("admin connects");

        let described = admin.describe_transaction("payments").await;

        bootstrap.stop();
        coordinator.stop();
        assert2::assert!(let Ok(described) = described);
        assert2::assert!(
            described
                == TransactionDescription {
                    transactional_id: "payments".to_owned(),
                    coordinator_id: 2,
                    state: "Ongoing".to_owned(),
                    producer_id: 4242,
                    producer_epoch: 7,
                    timeout: Time::from_millis(0),
                    start_time_ms: Some(0),
                    topic_partitions: BTreeSet::new(),
                }
        );
    }

    /// Kafka's `describeTransactions` runs every ID under one call deadline
    /// (`invokeDriver`), so a slow first ID leaves less time for the next.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn describe_transactions_shares_one_deadline_across_ids() {
        let mut coordinator_codes = vec![14; 8];
        coordinator_codes.extend([0, 14]);
        let script = Arc::new(Mutex::new(CoordinatorScript {
            find_coordinator: vec![0],
            coordinator: coordinator_codes,
            ..CoordinatorScript::default()
        }));
        let coordinator_addr = Arc::new(Mutex::new(None));
        let coordinator =
            scripted_transaction_broker(Arc::clone(&script), Arc::clone(&coordinator_addr)).await;
        *coordinator_addr.lock().expect("coordinator lock") = Some(coordinator.addr);
        let bootstrap =
            scripted_transaction_broker(Arc::clone(&script), Arc::clone(&coordinator_addr)).await;
        let admin = AdminClient::connect_with_config(
            &[bootstrap.addr.to_string()],
            crate::AdminClientConfig {
                request_timeout: krabka_units::millis(200),
                default_api_timeout: Some(krabka_units::millis(1500)),
                retry_backoff: krabka_units::millis(100),
                retry_backoff_max: krabka_units::millis(100),
                ..AdminClientConfig::default()
            },
        )
        .await
        .expect("admin connects");

        let started = tokio::time::Instant::now();
        let result = admin.describe_transactions(&["payments", "payments"]).await;
        let elapsed = started.elapsed();

        bootstrap.stop();
        coordinator.stop();
        // The first ID needs about 800 ms. The second ID gets the rest of the
        // 1.5 s deadline, not a new 1.5 s.
        assert2::assert!(matches!(
            result,
            Err(AdminError::Transport(ClientError::Timeout(_)))
        ));
        assert2::assert!(elapsed < Duration::from_millis(2200), "{elapsed:?}");
    }

    /// The transaction that the abort tests fence.
    fn orders_spec() -> AbortTransactionSpec {
        AbortTransactionSpec {
            topic: "orders".to_owned(),
            partition: 1,
            producer_id: 4242,
            producer_epoch: 7,
            coordinator_epoch: 3,
        }
    }

    /// A `Metadata` answer in which broker 1 at `addr` leads `orders`-0 and
    /// `orders`-1, with `partition_error` on each partition, and `missing`
    /// is an unknown topic.
    fn orders_metadata(addr: std::net::SocketAddr, partition_error: i16) -> MetadataResponse {
        let partition = |partition_index| MetadataResponsePartition {
            error_code: partition_error,
            partition_index,
            leader_id: 1,
            ..Default::default()
        };
        MetadataResponse {
            brokers: vec![MetadataResponseBroker {
                node_id: 1,
                host: addr.ip().to_string(),
                port: i32::from(addr.port()),
                ..Default::default()
            }],
            topics: vec![
                MetadataResponseTopic {
                    name: Some("orders".to_owned()),
                    partitions: vec![partition(0), partition(1)],
                    ..Default::default()
                },
                MetadataResponseTopic {
                    error_code: 3,
                    name: Some("missing".to_owned()),
                    ..Default::default()
                },
            ],
            ..Default::default()
        }
    }

    /// The outcome of an admin call reduced to what the tests compare: the
    /// failing API and error code, or `("protocol", 0)` for a protocol error.
    fn outcome_of(result: Result<(), AdminError>) -> TransactionOutcome {
        result.map_err(|error| match error {
            AdminError::Broker { api, code, .. } => (api, code),
            AdminError::Protocol(_) => ("protocol", 0),
            other => panic!("unexpected error {other:?}"),
        })
    }

    #[test]
    fn partition_leader_address_resolves_the_leader_or_reports_the_metadata_error() {
        let addr: std::net::SocketAddr = "127.0.0.1:9092".parse().expect("address");
        let mut leaderless = orders_metadata(addr, 0);
        leaderless.topics[0].partitions[1].leader_id = 9;
        for (name, metadata, topic, partition, expected) in [
            (
                "leader",
                orders_metadata(addr, 0),
                "orders",
                1,
                Ok("127.0.0.1:9092".to_owned()),
            ),
            (
                "partition error",
                orders_metadata(addr, NOT_LEADER_OR_FOLLOWER),
                "orders",
                1,
                Err(("Metadata", NOT_LEADER_OR_FOLLOWER)),
            ),
            (
                "topic error",
                orders_metadata(addr, 0),
                "missing",
                0,
                Err(("Metadata", 3)),
            ),
            (
                "unlisted topic",
                orders_metadata(addr, 0),
                "absent",
                0,
                Err(("protocol", 0)),
            ),
            (
                "unlisted partition",
                orders_metadata(addr, 0),
                "orders",
                5,
                Err(("protocol", 0)),
            ),
            (
                "unlisted leader",
                leaderless,
                "orders",
                1,
                Err(("protocol", 0)),
            ),
        ] {
            let address = partition_leader_address(topic, partition, &metadata);
            let actual = match address {
                Ok(address) => Ok(address),
                Err(error) => Err(outcome_of(Err(error)).expect_err("an error")),
            };
            assert2::assert!(actual == expected, "case {name}");
        }
    }

    #[test]
    fn abort_request_writes_one_abort_marker_for_the_partition() {
        assert2::assert!(
            abort_transaction_request(&orders_spec())
                == WriteTxnMarkersRequest {
                    markers: vec![WritableTxnMarker {
                        producer_id: 4242,
                        producer_epoch: 7,
                        transaction_result: false,
                        topics: vec![WritableTxnMarkerTopic {
                            name: "orders".to_owned(),
                            partition_indexes: vec![1],
                            ..Default::default()
                        }],
                        coordinator_epoch: 3,
                        ..Default::default()
                    }],
                    ..Default::default()
                }
        );
    }

    /// A `WriteTxnMarkers` answer with `code` for `producer_id` on
    /// `topic`-`partition`.
    fn markers_response(
        producer_id: i64,
        topic: &str,
        partition: i32,
        code: i16,
    ) -> WriteTxnMarkersResponse {
        WriteTxnMarkersResponse {
            markers: vec![WritableTxnMarkerResult {
                producer_id,
                topics: vec![WritableTxnMarkerTopicResult {
                    name: topic.to_owned(),
                    partitions: vec![WritableTxnMarkerPartitionResult {
                        partition_index: partition,
                        error_code: code,
                        ..Default::default()
                    }],
                    ..Default::default()
                }],
                ..Default::default()
            }],
            ..Default::default()
        }
    }

    #[test]
    fn abort_result_maps_the_matching_partition_result() {
        for (name, response, expected) in [
            ("no error", markers_response(4242, "orders", 1, 0), OK),
            (
                "partition error",
                markers_response(4242, "orders", 1, 53),
                failed("WriteTxnMarkers", 53),
            ),
            (
                "other producer",
                markers_response(1, "orders", 1, 0),
                failed("protocol", 0),
            ),
            (
                "other topic",
                markers_response(4242, "payments", 1, 0),
                failed("protocol", 0),
            ),
            (
                "other partition",
                markers_response(4242, "orders", 0, 0),
                failed("protocol", 0),
            ),
        ] {
            let result = outcome_of(abort_transaction_result(&orders_spec(), response));
            assert2::assert!(result == expected, "case {name}");
        }
    }

    /// Apache Kafka's `AbortTransactionHandler.handleResponse` looks the
    /// leader up again after `NOT_LEADER_OR_FOLLOWER` and
    /// `LEADER_NOT_AVAILABLE`, and fails on every other code.
    #[test]
    fn abort_retries_only_leadership_errors() {
        #[derive(Debug, PartialEq, Eq)]
        enum Action {
            Done,
            FindLeader,
        }
        for (code, expected) in [
            (0, Action::Done),
            (LEADER_NOT_AVAILABLE, Action::FindLeader),
            (NOT_LEADER_OR_FOLLOWER, Action::FindLeader),
            (53, Action::Done),
            (47, Action::Done),
        ] {
            let result =
                abort_transaction_result(&orders_spec(), markers_response(4242, "orders", 1, code));
            let action = match abort_transaction_retry_action(result) {
                RetryAction::Done(_) => Action::Done,
                RetryAction::FindCoordinator(_) => Action::FindLeader,
                RetryAction::SameCoordinator(_) => panic!("code {code}: same leader"),
            };
            assert2::assert!(action == expected, "code {code}");
        }
    }

    #[test]
    fn describe_producers_request_groups_partitions_by_topic() {
        let keys = [
            ("orders".to_owned(), 1),
            ("audit".to_owned(), 0),
            ("orders".to_owned(), 0),
        ];

        assert2::assert!(
            describe_producers_request(&keys)
                == DescribeProducersRequest {
                    topics: vec![
                        DescribeProducersTopicRequest {
                            name: "audit".to_owned(),
                            partition_indexes: vec![0],
                            ..Default::default()
                        },
                        DescribeProducersTopicRequest {
                            name: "orders".to_owned(),
                            partition_indexes: vec![1, 0],
                            ..Default::default()
                        },
                    ],
                    ..Default::default()
                }
        );
    }

    /// The `DescribeProducers` answer of the tests: one producer with an open
    /// transaction and one without on `orders`-0, `NOT_LEADER_OR_FOLLOWER` on
    /// `orders`-1, and a partition that nobody asked about.
    fn orders_producers() -> DescribeProducersResponse {
        DescribeProducersResponse {
            topics: vec![TopicResponse {
                name: "orders".to_owned(),
                partitions: vec![
                    PartitionResponse {
                        partition_index: 0,
                        active_producers: vec![
                            WireProducerState {
                                producer_id: 4242,
                                producer_epoch: 7,
                                last_sequence: 12,
                                last_timestamp: 1_700_000_000_123,
                                coordinator_epoch: 3,
                                current_txn_start_offset: 88,
                                ..Default::default()
                            },
                            WireProducerState {
                                producer_id: 11,
                                ..Default::default()
                            },
                        ],
                        ..Default::default()
                    },
                    PartitionResponse {
                        partition_index: 1,
                        error_code: NOT_LEADER_OR_FOLLOWER,
                        error_message: Some("moved".to_owned()),
                        ..Default::default()
                    },
                    PartitionResponse {
                        partition_index: 9,
                        ..Default::default()
                    },
                ],
                ..Default::default()
            }],
            ..Default::default()
        }
    }

    /// The per-partition results of [`orders_producers`] for `orders`-0 and
    /// `orders`-1.
    fn orders_producer_results()
    -> BTreeMap<(String, i32), Result<Vec<ProducerStateInfo>, KafkaError>> {
        maplit::btreemap! {
            ("orders".to_owned(), 0) => Ok(vec![
            ProducerStateInfo {
                producer_id: 4242,
                producer_epoch: 7,
                last_sequence: 12,
                last_timestamp_ms: 1_700_000_000_123,
                coordinator_epoch: 3,
                current_txn_start_offset: Some(88),
            },
            ProducerStateInfo {
                producer_id: 11,
                producer_epoch: 0,
                last_sequence: -1,
                last_timestamp_ms: -1,
                coordinator_epoch: 0,
                current_txn_start_offset: None,
            },
        ]),
            ("orders".to_owned(), 1) => Err(KafkaError {
            code: NOT_LEADER_OR_FOLLOWER,
            name: "NOT_LEADER_OR_FOLLOWER",
            message: Some("moved".to_owned()),
        }),
        }
    }

    #[test]
    fn describe_producers_results_map_each_requested_partition() {
        let keys = [
            ("orders".to_owned(), 0),
            ("orders".to_owned(), 1),
            ("orders".to_owned(), 2),
        ];

        let mut expected = orders_producer_results();
        expected.insert(
            ("orders".to_owned(), 2),
            Err(KafkaError {
                code: -1,
                name: "UNKNOWN_SERVER_ERROR",
                message: Some(
                    "the DescribeProducers response did not contain a result for partition \
                     orders-2"
                        .to_owned(),
                ),
            }),
        );
        assert2::assert!(describe_producers_results(&keys, orders_producers()) == expected);
    }

    #[test]
    fn list_transactions_request_carries_the_filter() {
        for (name, filter, expected) in [
            (
                "default matches everything",
                ListTransactionsFilter::builder().build(),
                ListTransactionsRequest::default(),
            ),
            (
                "every filter",
                ListTransactionsFilter::builder()
                    .state_filters(vec!["Ongoing".to_owned()])
                    .producer_id_filters(vec![4242])
                    .min_duration(90_000)
                    .build(),
                ListTransactionsRequest {
                    state_filters: vec!["Ongoing".to_owned()],
                    producer_id_filters: vec![4242],
                    duration_filter: 90_000,
                    ..Default::default()
                },
            ),
        ] {
            assert2::assert!(
                list_transactions_request(&filter) == expected,
                "case {name}"
            );
        }
    }

    /// Decodes the request body of a mock broker: the client id, the tagged
    /// field byte of a flexible header, and the request itself.
    fn decode_request<T: for<'a> Decode<'a>>(mut body: &[u8], version: i16, flexible: bool) -> T {
        let client_id_len = body.get_i16();
        body.advance(usize::try_from(client_id_len).expect("client id length"));
        if flexible {
            body.advance(1);
        }
        T::decode(&mut body, version).expect("request decodes")
    }

    /// An `ApiVersions` response for the leader-routed mocks.
    fn leader_api_versions() -> Vec<u8> {
        let api_version = |api_key, min_version, max_version| ApiVersion {
            api_key,
            min_version,
            max_version,
            ..Default::default()
        };
        encode_response(
            &ApiVersionsResponse {
                api_keys: vec![
                    api_version(api_versions_request::API_KEY, 0, 0),
                    api_version(metadata_request::API_KEY, 13, 13),
                    api_version(write_txn_markers_request::API_KEY, 1, 2),
                    api_version(list_transactions_request::API_KEY, 0, 2),
                ],
                ..Default::default()
            },
            0,
            false,
        )
    }

    /// What a leader-routed mock broker saw and answers.
    #[derive(Default)]
    struct LeaderScript {
        /// The `WriteTxnMarkers` error codes, one per request. The last
        /// repeats.
        write_codes: Vec<i16>,
        /// The `WriteTxnMarkers` requests that the broker received.
        write_requests: Vec<WriteTxnMarkersRequest>,
        /// The `Metadata` requests that the broker received.
        metadata_requests: usize,
        /// The `ListTransactions` requests that the broker received.
        list_requests: Vec<ListTransactionsRequest>,
        /// The `ListTransactions` answer of the broker.
        listing: ListTransactionsResponse,
    }

    /// A mock broker that leads every `orders` partition. `cluster` names the
    /// brokers that its `Metadata` answer lists, itself first.
    async fn leader_broker(
        script: Arc<Mutex<LeaderScript>>,
        cluster: Arc<Mutex<Vec<std::net::SocketAddr>>>,
    ) -> MockBroker {
        MockBroker::start(move |api_key, version, _, body| {
            let mut script = script.lock().expect("script lock");
            match api_key {
                api_versions_request::API_KEY => Some(leader_api_versions()),
                metadata_request::API_KEY => {
                    script.metadata_requests += 1;
                    let cluster = cluster.lock().expect("cluster lock").clone();
                    let mut metadata = orders_metadata(cluster[0], 0);
                    metadata.brokers = cluster
                        .iter()
                        .zip(1..)
                        .map(|(addr, node_id)| MetadataResponseBroker {
                            node_id,
                            host: addr.ip().to_string(),
                            port: i32::from(addr.port()),
                            ..Default::default()
                        })
                        .collect();
                    Some(encode_response(&metadata, version, true))
                }
                write_txn_markers_request::API_KEY => {
                    let request: WriteTxnMarkersRequest = decode_request(body, version, true);
                    let code = script.write_codes[script
                        .write_requests
                        .len()
                        .min(script.write_codes.len() - 1)];
                    script.write_requests.push(request);
                    Some(encode_response(
                        &markers_response(4242, "orders", 1, code),
                        version,
                        true,
                    ))
                }
                list_transactions_request::API_KEY => {
                    script
                        .list_requests
                        .push(decode_request(body, version, true));
                    Some(encode_response(&script.listing, version, true))
                }
                _ => None,
            }
        })
        .await
    }

    /// An admin client on `bootstrap` that retries after 1 ms.
    async fn fast_admin(bootstrap: std::net::SocketAddr) -> AdminClient {
        AdminClient::connect_with_config(
            &[bootstrap.to_string()],
            crate::AdminClientConfig {
                request_timeout: krabka_units::secs(2),
                retry_backoff: krabka_units::millis(1),
                retry_backoff_max: krabka_units::millis(1),
                default_api_timeout: Some(krabka_units::secs(5)),
                ..AdminClientConfig::default()
            },
        )
        .await
        .expect("admin connects")
    }

    /// Apache Kafka's `AbortTransactionHandler` sends one `WriteTxnMarkers`
    /// to the partition leader, looks the leader up again after
    /// `NOT_LEADER_OR_FOLLOWER`, and fails on every other code.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn abort_transaction_writes_an_abort_marker_to_the_leader() {
        for (name, write_codes, expected) in [
            ("no error", vec![0], (OK, 1, 1)),
            (
                "not leader or follower finds the leader again",
                vec![NOT_LEADER_OR_FOLLOWER, 0],
                (OK, 2, 2),
            ),
            (
                "transactional id authorization failed is final",
                vec![53],
                (failed("WriteTxnMarkers", 53), 1, 1),
            ),
        ] {
            let script = Arc::new(Mutex::new(LeaderScript {
                write_codes,
                ..LeaderScript::default()
            }));
            let cluster = Arc::new(Mutex::new(Vec::new()));
            let broker = leader_broker(Arc::clone(&script), Arc::clone(&cluster)).await;
            cluster.lock().expect("cluster lock").push(broker.addr);
            let admin = fast_admin(broker.addr).await;

            let result = outcome_of(admin.abort_transaction(&orders_spec()).await);

            broker.stop();
            let script = script.lock().expect("script lock");
            let expected_requests = vec![abort_transaction_request(&orders_spec()); expected.2];
            assert2::assert!(
                (
                    result,
                    script.metadata_requests,
                    script.write_requests.clone()
                ) == (expected.0, expected.1, expected_requests),
                "case {name}"
            );
        }
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn abort_transaction_rejects_an_empty_topic() {
        let script = Arc::new(Mutex::new(LeaderScript::default()));
        let cluster = Arc::new(Mutex::new(Vec::new()));
        let broker = leader_broker(Arc::clone(&script), Arc::clone(&cluster)).await;
        cluster.lock().expect("cluster lock").push(broker.addr);
        let admin = fast_admin(broker.addr).await;

        let result = admin
            .abort_transaction(&AbortTransactionSpec {
                topic: String::new(),
                ..orders_spec()
            })
            .await;

        broker.stop();
        assert2::assert!(outcome_of(result) == failed("protocol", 0));
        assert2::assert!(script.lock().expect("script lock").metadata_requests == 0);
    }

    /// The `DescribeProducers` answer to `request`: the producers of
    /// [`orders_producers`] on `orders`-0, and `orders_1_code` on `orders`-1.
    fn producers_answer(
        request: &DescribeProducersRequest,
        orders_1_code: i16,
    ) -> DescribeProducersResponse {
        let orders_0 = orders_producers().topics[0].partitions[0].clone();
        DescribeProducersResponse {
            topics: request
                .topics
                .iter()
                .map(|topic| TopicResponse {
                    name: topic.name.clone(),
                    partitions: topic
                        .partition_indexes
                        .iter()
                        .map(|&partition_index| {
                            if partition_index == 0 {
                                orders_0.clone()
                            } else {
                                PartitionResponse {
                                    partition_index,
                                    error_code: orders_1_code,
                                    error_message: (orders_1_code != 0)
                                        .then(|| "refused".to_owned()),
                                    ..Default::default()
                                }
                            }
                        })
                        .collect(),
                    ..Default::default()
                })
                .collect(),
            ..Default::default()
        }
    }

    /// Apache Kafka's `DescribeProducersHandler` batches the partitions of
    /// one leader into one request, gives each partition its own result, and
    /// sends a partition back to the `PartitionLeaderStrategy` lookup after
    /// `NOT_LEADER_OR_FOLLOWER` or a lost connection to its leader.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn describe_producers_finds_the_leader_again_as_kafka_does() {
        let both = vec![("orders".to_owned(), 0), ("orders".to_owned(), 1)];
        let only_1 = vec![("orders".to_owned(), 1)];
        let refused = refused_address().await;
        let producers_0 = orders_producer_results()[&both[0]].clone();
        for (name, leaders, orders_1_codes, expected) in [
            (
                "success",
                vec![None],
                vec![0],
                (Ok(Vec::new()), 1, vec![both.clone()]),
            ),
            (
                "not leader or follower finds the leader again",
                vec![None],
                vec![NOT_LEADER_OR_FOLLOWER, 0],
                (Ok(Vec::new()), 2, vec![both.clone(), only_1.clone()]),
            ),
            (
                "a refused connection finds the leader again",
                vec![Some(refused), None],
                vec![0],
                (Ok(Vec::new()), 2, vec![both.clone()]),
            ),
            (
                "topic authorization failed is final",
                vec![None],
                vec![29],
                (
                    Err(KafkaError {
                        code: 29,
                        name: "TOPIC_AUTHORIZATION_FAILED",
                        message: Some("refused".to_owned()),
                    }),
                    1,
                    vec![both.clone()],
                ),
            ),
        ] {
            let seen = Arc::new(Mutex::new(Seen::default()));
            let broker = partition_leader_broker(
                vec![
                    ApiVersionRange::builder()
                        .api_key(describe_producers_request::API_KEY)
                        .build(),
                ],
                describe_producers_request::API_KEY,
                leaders,
                Arc::clone(&seen),
                move |n, version, body| {
                    let request: DescribeProducersRequest = decode_request(body, version, true);
                    let code = orders_1_codes[n.min(orders_1_codes.len() - 1)];
                    encode_response(&producers_answer(&request, code), version, true)
                },
            )
            .await;
            let admin = leader_fast_admin(broker.addr, krabka_units::secs(5)).await;

            let result = admin.describe_producers(&both).await;

            broker.stop();
            let seen = seen.lock().expect("seen lock");
            let requests = seen
                .requests
                .iter()
                .map(|(version, body)| {
                    decode_request::<DescribeProducersRequest>(body, *version, true)
                })
                .collect::<Vec<_>>();
            assert2::assert!(
                (result, seen.metadata_requests, requests)
                    == (
                        maplit::btreemap! {
                            both[0].clone() => producers_0.clone(),
                            both[1].clone() => expected.0,
                        },
                        expected.1,
                        expected
                            .2
                            .iter()
                            .map(|keys| describe_producers_request(keys))
                            .collect::<Vec<_>>(),
                    ),
                "case {name}"
            );
        }
    }

    /// A `ListTransactions` answer with `error_code` and one transaction per
    /// ID in `transactional_ids`.
    fn listing(error_code: i16, transactional_ids: &[&str]) -> ListTransactionsResponse {
        ListTransactionsResponse {
            error_code,
            transaction_states: transactional_ids
                .iter()
                .zip(1..)
                .map(|(transactional_id, producer_id)| ListedTransactionState {
                    transactional_id: (*transactional_id).to_owned(),
                    producer_id,
                    transaction_state: "Ongoing".to_owned(),
                    ..Default::default()
                })
                .collect(),
            ..Default::default()
        }
    }

    /// Apache Kafka's `ListTransactionsHandler` asks every broker
    /// (`AllBrokersStrategy`) and merges the listings. A broker error fails
    /// the call.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn list_transactions_merges_the_listing_of_every_broker() {
        let listed = |transactional_id: &str, producer_id| TransactionListing {
            transactional_id: transactional_id.to_owned(),
            producer_id,
            state: "Ongoing".to_owned(),
        };
        for (name, second_listing, expected) in [
            (
                "every broker answers",
                listing(0, &["audit"]),
                Ok(vec![
                    listed("payments", 1),
                    listed("orders", 2),
                    listed("audit", 1),
                ]),
            ),
            (
                "a broker is loading its coordinator",
                listing(COORDINATOR_LOAD_IN_PROGRESS, &[]),
                Err(("ListTransactions", COORDINATOR_LOAD_IN_PROGRESS)),
            ),
        ] {
            let first = Arc::new(Mutex::new(LeaderScript {
                listing: listing(0, &["payments", "orders"]),
                ..LeaderScript::default()
            }));
            let second = Arc::new(Mutex::new(LeaderScript {
                listing: second_listing,
                ..LeaderScript::default()
            }));
            let cluster = Arc::new(Mutex::new(Vec::new()));
            let first_broker = leader_broker(Arc::clone(&first), Arc::clone(&cluster)).await;
            let second_broker = leader_broker(Arc::clone(&second), Arc::clone(&cluster)).await;
            cluster
                .lock()
                .expect("cluster lock")
                .extend([first_broker.addr, second_broker.addr]);
            let admin = fast_admin(first_broker.addr).await;
            let filter = ListTransactionsFilter::builder()
                .state_filters(vec!["Ongoing".to_owned()])
                .build();

            let result = admin
                .list_transactions(&filter)
                .await
                .map_err(|error| match error {
                    AdminError::Broker { api, code, .. } => (api, code),
                    other => panic!("case {name}: unexpected error {other:?}"),
                });

            first_broker.stop();
            second_broker.stop();
            let requests = [&first, &second]
                .map(|script| script.lock().expect("script lock").list_requests.clone());
            let expected_request = list_transactions_request(&filter);
            assert2::assert!(
                (result, requests)
                    == (
                        expected,
                        [vec![expected_request.clone()], vec![expected_request]]
                    ),
                "case {name}"
            );
        }
    }

    /// Apache Kafka's `fenceProducers` gives each transactional ID its own
    /// result, and an invalid ID does not stop the others.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn fence_producers_gives_each_transactional_id_its_own_result() {
        let script = Arc::new(Mutex::new(CoordinatorScript {
            find_coordinator: vec![0],
            coordinator: vec![0],
            ..CoordinatorScript::default()
        }));
        let coordinator_addr = Arc::new(Mutex::new(None));
        let coordinator =
            scripted_transaction_broker(Arc::clone(&script), Arc::clone(&coordinator_addr)).await;
        *coordinator_addr.lock().expect("coordinator lock") = Some(coordinator.addr);
        let bootstrap =
            scripted_transaction_broker(Arc::clone(&script), Arc::clone(&coordinator_addr)).await;
        let admin = AdminClient::connect(&[bootstrap.addr.to_string()])
            .await
            .expect("admin connects");

        let results = admin.fence_producers(&["payments", "", "orders"]).await;

        bootstrap.stop();
        coordinator.stop();
        let results = results
            .into_iter()
            .map(|(transactional_id, result)| (transactional_id, outcome_of(result)))
            .collect::<BTreeMap<_, _>>();
        let script = script.lock().expect("script lock");
        assert2::assert!(
            (
                results,
                script.find_coordinator_requests,
                script.coordinator_requests
            ) == (
                maplit::btreemap! {
                    String::new() => failed("protocol", 0),
                    "orders".to_owned() => OK,
                    "payments".to_owned() => OK,
                },
                2,
                2
            )
        );
    }

    fn assert_send<T: Send>(_: T) {}

    /// A caller can spawn every retrying admin call on a multi-thread
    /// runtime, so each future must be `Send`.
    #[test]
    fn retrying_admin_call_futures_are_send() {
        let _ = |admin: &mut AdminClient,
                 offsets: &std::collections::BTreeMap<(String, i32), i64>| {
            assert_send(admin.alter_consumer_group_offsets("workers", offsets));
            assert_send(admin.list_consumer_group_offsets("workers"));
            assert_send(admin.describe_transaction("payments"));
            assert_send(admin.force_terminate_transaction("payments"));
            assert_send(admin.fence_producers(&["payments"]));
            assert_send(admin.abort_transaction(&orders_spec()));
            assert_send(admin.describe_producers(&[("orders".to_owned(), 0)]));
            assert_send(admin.list_transactions(&ListTransactionsFilter::builder().build()));
        };
    }
}