1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
use super::LeaderStateSnapshot;
use super::RaftRole;
use super::SharedState;
use super::StateSnapshot;
use super::buffers::BatchBuffer;
use super::buffers::ProposeBatchBuffer;
use super::candidate_state::CandidateState;
use super::role_state::RaftRoleState;
use super::role_state::check_and_trigger_snapshot;
use super::role_state::send_replay_raft_event;
use crate::BackgroundSnapshotTransfer;
use crate::ConnectionType;
use crate::ConsensusError;
use crate::Error;
use crate::MaybeCloneOneshotSender;
use crate::Membership;
use crate::MembershipError;
use crate::NetworkError;
use crate::PeerUpdate;
use crate::PurgeExecutor;
use crate::RaftContext;
use crate::RaftEvent;
use crate::RaftLog;
use crate::RaftNodeConfig;
use crate::RaftRequestWithSignal;
use crate::ReadConsistencyPolicy as ServerReadConsistencyPolicy;
use crate::ReplicationConfig;
use crate::ReplicationCore;
use crate::ReplicationError;
use crate::ReplicationTimer;
use crate::Result;
use crate::RetryPolicies;
use crate::RoleEvent;
use crate::SnapshotConfig;
use crate::StateMachine;
use crate::StateMachineHandler;
use crate::StateTransitionError;
use crate::SystemError;
use crate::TypeConfig;
use crate::alias::MOF;
use crate::alias::ROF;
use crate::alias::SMHOF;
use crate::alias::TROF;
use crate::client::{ClientReadRequest, ClientResponse, ErrorCode};
use crate::event::ClientCmd;
use crate::network::Transport;
use crate::stream::create_production_snapshot_stream;
use async_trait::async_trait;
use d_engine_proto::common::AddNode;
use d_engine_proto::common::BatchPromote;
use d_engine_proto::common::BatchRemove;
use d_engine_proto::common::EntryPayload;
use d_engine_proto::common::LogId;
use d_engine_proto::common::NodeRole::Leader;
use d_engine_proto::common::NodeStatus;
use d_engine_proto::common::membership_change::Change;
use d_engine_proto::server::cluster::ClusterConfUpdateResponse;
use d_engine_proto::server::cluster::JoinRequest;
use d_engine_proto::server::cluster::JoinResponse;
use d_engine_proto::server::cluster::LeaderDiscoveryResponse;
use d_engine_proto::server::cluster::NodeMeta;
use d_engine_proto::server::election::VoteResponse;
use d_engine_proto::server::election::VotedFor;
use d_engine_proto::server::replication::AppendEntriesRequest;
use d_engine_proto::server::replication::AppendEntriesResponse;
use d_engine_proto::server::replication::append_entries_response;
use d_engine_proto::server::storage::SnapshotChunk;
use d_engine_proto::server::storage::SnapshotMetadata;
use rand::distr::SampleString;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::fmt::Debug;
use std::marker::PhantomData;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::AtomicU32;
use std::sync::atomic::Ordering;
use std::time::Duration;
use tokio::sync::mpsc;
use tokio::sync::oneshot;
use tokio::time::Instant;
use tonic::Status;
use tracing::debug;
use tracing::error;
use tracing::info;
use tracing::instrument;
use tracing::trace;
use tracing::warn;
// Type aliases for improved readability
type LinearizableReadRequest = (
ClientReadRequest,
MaybeCloneOneshotSender<std::result::Result<ClientResponse, Status>>,
);
/// Metadata for an in-flight write batch awaiting quorum commit.
/// Stored in `pending_client_writes` keyed by `end_log_index`.
pub(super) struct WriteMetadata {
pub(super) start_idx: u64,
pub(super) senders: Vec<MaybeCloneOneshotSender<std::result::Result<ClientResponse, Status>>>,
pub(super) wait_for_apply: bool,
/// Wall-clock deadline; batch is failed with deadline_exceeded if commit has not
/// arrived by this time. Set in execute_and_process_raft_rpc Phase 2.
// NOTE: expiry is checked on each tick; actual timeout may exceed deadline by up to
// one tick interval (~150-300ms). Acceptable for Raft workloads.
pub(super) deadline: Instant,
}
/// A lease read request waiting for quorum ACK to refresh the lease.
/// Stored in `pending_lease_reads` (VecDeque); drained after `update_lease_timestamp()`.
pub(super) struct PendingLeaseRead {
pub(super) request: ClientReadRequest,
pub(super) sender: MaybeCloneOneshotSender<std::result::Result<ClientResponse, Status>>,
pub(super) deadline: Instant,
}
/// A batch of linearizable read requests waiting for the state machine to reach `read_index`.
/// Stored in `pending_reads` keyed by `read_index`.
pub(super) struct PendingReadBatch {
/// Deadline inherited from the first flush that created this batch.
/// Subsequent flushes at the same read_index share this deadline (not overwritten).
// NOTE: same tick-granularity caveat as WriteMetadata::deadline.
pub(super) deadline: Instant,
pub(super) requests: VecDeque<LinearizableReadRequest>,
}
/// Action to execute once a specific log index is committed.
/// Stored in `pending_commit_actions` keyed by the log index that must commit first.
pub(super) struct PostCommitEntry {
/// Deadline for the commit to arrive; action fails with timeout on expiry.
// NOTE: checked at tick granularity, same caveat as WriteMetadata::deadline.
pub(super) deadline: Instant,
pub(super) action: PostCommitAction,
}
/// Post-commit actions for Raft protocol internal control operations.
///
/// These actions fire immediately when their log entry commits (reaches majority),
/// WITHOUT waiting for state machine apply. This is correct because they represent
/// Raft protocol metadata (quorum confirmation, membership changes), not user data.
///
/// Contrast with `pending_client_writes`, which may require state machine apply
/// (e.g., CAS operations need read-modify-write through the SM).
///
/// # Lifecycle
/// 1. Action registered when entry appended (e.g., `handle_join_cluster`, `initiate_noop_commit`)
/// 2. Action triggered when `commit_index` advances past the entry's index
/// 3. Fired via `drain_commit_actions` → `RoleEvent` → `raft.rs` handler
///
/// # Invariants
/// - Actions only move forward with commit_index advancement
/// - Each action fires exactly once (ensured by `split_off` pattern)
/// - Expired actions (deadline passed) trigger error responses in `tick()`
pub(super) enum PostCommitAction {
/// Leader noop entry committed: confirms quorum and enables lease-based reads.
/// Fired once the noop entry reaches majority, proving leader has quorum.
LeaderNoop { term: u64 },
/// Membership change (AddNode) entry committed: responds to join request.
/// Fired when the AddNode config entry commits (majority replication).
/// The joining node becomes a cluster member at this point (Raft protocol level).
NodeJoin {
node_id: u32,
addr: String,
sender: MaybeCloneOneshotSender<std::result::Result<JoinResponse, Status>>,
},
}
// Supporting data structures
#[derive(Debug, Clone)]
pub struct PendingPromotion {
pub node_id: u32,
pub ready_since: Instant,
}
impl PendingPromotion {
pub fn new(
node_id: u32,
ready_since: Instant,
) -> Self {
PendingPromotion {
node_id,
ready_since,
}
}
}
/// Cached cluster topology metadata for hot path optimization.
///
/// This metadata is immutable during leadership and only updated on membership changes.
/// Caching avoids repeated async calls to membership queries in hot paths like append_entries.
#[derive(Debug, Clone)]
pub struct ClusterMetadata {
/// Single-voter cluster (quorum = 1, used for election and commit optimization)
pub single_voter: bool,
/// Total number of voters including self (updated on membership changes)
pub total_voters: usize,
/// Cached replication targets (voters + learners, excluding self)
pub replication_targets: Vec<NodeMeta>,
}
/// Metrics context for backpressure monitoring (zero-allocation hot path)
///
/// Encapsulates all metrics-related state to avoid polluting the core LeaderState structure.
/// Pre-allocated labels ensure zero allocations in the request hot path.
pub struct BackpressureMetrics {
/// Pre-allocated labels for write rejections: [("node_id", "\<id>"), ("type", "write")]
labels_write: Arc<[(String, String)]>,
/// Pre-allocated labels for read rejections: [("node_id", "\<id>"), ("type", "read")]
labels_read: Arc<[(String, String)]>,
/// Runtime switch (branch prediction optimized, ~0ns overhead when false)
enabled: bool,
/// Sample counter for gauge metrics (reduces overhead at high QPS)
sample_counter: AtomicU32,
/// Sampling rate (1 = no sampling, 10 = sample 1 in 10)
sample_rate: u32,
}
impl BackpressureMetrics {
/// Create new metrics context with pre-allocated labels
pub fn new(
node_id: u32,
enabled: bool,
sample_rate: u32,
) -> Self {
let sample_rate = if sample_rate == 0 { 1 } else { sample_rate };
let node_id_str = node_id.to_string();
let labels_write = Arc::new([
("node_id".to_string(), node_id_str.clone()),
("type".to_string(), "write".to_string()),
]);
let labels_read = Arc::new([
("node_id".to_string(), node_id_str),
("type".to_string(), "read".to_string()),
]);
Self {
labels_write,
labels_read,
enabled,
sample_counter: AtomicU32::new(0),
sample_rate,
}
}
/// Record rejection metric (always counted, not sampled)
#[inline]
pub fn record_rejection(
&self,
is_write: bool,
) {
if self.enabled {
let labels = if is_write {
&self.labels_write
} else {
&self.labels_read
};
metrics::counter!("backpressure.rejections", labels.as_ref()).increment(1);
}
}
/// Record buffer utilization gauge (with sampling)
#[inline]
pub fn record_buffer_utilization(
&self,
utilization: f64,
is_write: bool,
) {
if self.enabled {
let counter = self.sample_counter.fetch_add(1, Ordering::Relaxed);
if counter % self.sample_rate == 0 {
let labels = if is_write {
&self.labels_write
} else {
&self.labels_read
};
metrics::gauge!("backpressure.buffer_utilization", labels.as_ref())
.set(utilization);
}
}
}
}
/// Task routed to a per-follower replication worker.
enum ReplicationTask {
/// Normal AppendEntries replication.
Append(AppendEntriesRequest),
/// Peer's next_index fell below the purge boundary; transfer the latest snapshot.
Snapshot(SnapshotMetadata),
}
/// Immutable configuration passed to every per-follower replication worker.
///
/// Bundles the seven items that are always passed together to `spawn_worker`,
/// `run_replication_worker`, and `send_to_worker_or_spawn`, keeping those
/// function signatures within the clippy `too_many_arguments` limit.
struct ReplicationWorkerConfig<T: TypeConfig> {
transport: Arc<TROF<T>>,
membership: Arc<MOF<T>>,
retry_policies: RetryPolicies,
response_compress_enabled: bool,
role_event_tx: mpsc::UnboundedSender<RoleEvent>,
state_machine_handler: Arc<SMHOF<T>>,
snapshot_config: SnapshotConfig,
}
/// Handle for a per-follower replication worker task.
///
/// Dropping this handle (via `LeaderState::replication_workers`) signals the worker to exit:
/// when `task_tx` is dropped, `task_rx.recv()` returns `None` and the worker exits cleanly.
struct ReplicationWorkerHandle {
task_tx: mpsc::UnboundedSender<ReplicationTask>,
/// Consecutive snapshot push failures for this peer since the last success.
snapshot_failure_count: u32,
/// Earliest time the next snapshot push attempt is allowed (exponential backoff window).
/// `None` means no active backoff — the next heartbeat may attempt immediately.
snapshot_next_retry_at: Option<Instant>,
}
/// Leader node's state in Raft consensus algorithm.
///
/// This structure maintains all state that should be persisted on leader crashes,
/// including replication progress tracking and log compaction management.
///
/// # Type Parameters
/// - `T`: Application-specific Raft type configuration
pub struct LeaderState<T: TypeConfig> {
// -- Core Raft Leader State --
/// Shared cluster state with lock protection
pub shared_state: SharedState,
/// === Volatile State ===
/// For each server (node_id), index of the next log entry to send to that server
///
/// Raft Paper: §5.3 Figure 2 (nextIndex)
pub next_index: HashMap<u32, u64>,
/// === Volatile State ===
/// For each server (node_id), index of highest log entry known to be replicated
///
/// Raft Paper: §5.3 Figure 2 (matchIndex)
pub(super) match_index: HashMap<u32, u64>,
/// === Volatile State ===
/// Temporary storage for no-op entry log ID during leader initialization
#[doc(hidden)]
pub noop_log_id: Option<u64>,
// -- Log Compaction & Purge --
/// === Volatile State ===
/// The upper bound (exclusive) of log entries scheduled for asynchronous physical deletion.
///
/// This value is set immediately after a new snapshot is successfully created.
/// It represents the next log position that will trigger compaction.
///
/// The actual log purge is performed by a background task, which may be delayed
/// due to resource constraints or retry mechanisms.
pub scheduled_purge_upto: Option<LogId>,
/// === Persistent State (MUST be on disk) ===
/// The last log position that has been **physically removed** from stable storage.
///
/// This value is atomically updated when:
/// 1. A new snapshot is persisted (marking logs up to `last_included_index` as purgeable)
/// 2. The background purge task completes successfully
///
/// Raft safety invariant:
/// Any log entry with index ≤ `last_purged_index` is guaranteed to be
/// reflected in the latest snapshot.
pub last_purged_index: Option<LogId>,
/// Record if there is on-going snapshot creation activity
pub snapshot_in_progress: AtomicBool,
// -- Request Processing --
/// Batched proposal buffer for client requests
///
/// Accumulates requests until either:
/// 1. Batch reaches configured size limit
/// 2. Explicit flush is triggered
/// SoA layout: payloads and senders stored in separate contiguous Vecs.
/// flush() builds one RaftRequestWithSignal via mem::take — true O(1), no unzip scan.
pub(super) propose_buffer: Box<ProposeBatchBuffer>,
// -- Timing & Scheduling --
/// Replication heartbeat timer manager
///
/// Handles:
/// - Heartbeat interval tracking
/// - Election timeout prevention
/// - Batch proposal flushing
timer: Box<ReplicationTimer>,
// -- Cluster Configuration --
/// Cached Raft node configuration (shared reference)
///
/// This includes:
/// - Cluster membership
/// - Timeout parameters
/// - Performance tuning knobs
pub(super) node_config: Arc<RaftNodeConfig>,
/// Last time we checked for learners
pub last_learner_check: Instant,
// -- Stale Learner Handling --
/// Earliest time the oldest pending learner becomes stale.
///
/// Set to `front().ready_since + stale_learner_threshold` when a learner is enqueued.
/// `None` when `pending_promotions` is empty (O(1) short-circuit in tick).
/// After `drain_batch`, call `refresh_stale_deadline` to recompute from the new front.
///
/// Design: VecDeque is FIFO so the front is always the oldest entry — no need to
/// scan the whole queue. `min()` over all entries would always select the front,
/// so we directly bind the deadline to `front().ready_since + threshold`.
pub stale_check_deadline: Option<Instant>,
/// Queue of learners that have caught up and are pending promotion to voter.
pub pending_promotions: VecDeque<PendingPromotion>,
/// Monotonic instant of the last quorum ACK, used to check lease validity.
/// `None` until the first ACK; replaced on every subsequent quorum confirmation.
/// Must be `Instant` (not `SystemTime`) — wall clock can move backward under NTP.
pub(super) lease_instant: Mutex<Option<Instant>>,
// -- Cluster Topology Cache --
/// Cached cluster metadata (updated on membership changes)
/// Avoids repeated async calls in hot paths
pub(super) cluster_metadata: ClusterMetadata,
/// Buffered linearizable read requests awaiting batch processing
/// Only LinearizableRead policy uses batching with size/timeout thresholds
pub(super) linearizable_read_buffer: Box<BatchBuffer<LinearizableReadRequest>>,
/// Lease-based read requests awaiting processing
/// Processed immediately in flush_cmd_buffers if lease is valid
pub(super) lease_read_queue: VecDeque<(
ClientReadRequest,
MaybeCloneOneshotSender<std::result::Result<ClientResponse, Status>>,
)>,
/// Eventual consistency read requests awaiting processing
/// Processed immediately in flush_cmd_buffers without any verification
pub(super) eventual_read_queue: VecDeque<(
ClientReadRequest,
MaybeCloneOneshotSender<std::result::Result<ClientResponse, Status>>,
)>,
// -- Client Request Tracking --
/// Writes committed but awaiting state machine apply before responding.
/// Key: log index. Value: response sender.
/// Populated when `wait_for_apply=true` (e.g. CAS, or any write needing SM result).
/// Drained by `handle_apply_completed`; cleared with error on step-down / fatal error.
pub(super) pending_write_apply:
HashMap<u64, MaybeCloneOneshotSender<std::result::Result<ClientResponse, Status>>>,
/// Pending linearizable reads waiting for SM to apply up to read_index.
/// Key: read_index (SM must reach this index before reads can be served)
/// Value: PendingReadBatch (deadline + requests registered at that read_index)
/// Drained on role change; processed in ApplyCompleted handler.
pub(super) pending_reads: BTreeMap<u64, PendingReadBatch>,
/// Pending lease reads waiting for quorum ACK to refresh the lease.
/// Pushed when lease is expired; drained after update_lease_timestamp().
/// Drained with error on step-down or deadline exceeded (tick).
pub(super) pending_lease_reads: VecDeque<PendingLeaseRead>,
/// Pending client write batches indexed by end_log_index.
/// Key: end_log_index of this write batch. Value: WriteMetadata (senders + deadline).
/// Drained when commit_index advances past end_log_index (quorum achieved or single-voter flush).
/// Drained with error on step-down / HigherTerm / deadline exceeded.
pub(super) pending_client_writes: BTreeMap<u64, WriteMetadata>,
/// Post-commit action queue for Raft protocol internal operations (noop, join).
///
/// **Key semantics**: Log index → Action to fire when that index commits.
///
/// **Trigger condition**: commit_index advances (NOT apply_index).
/// These are protocol-level operations that respond immediately upon majority replication,
/// without waiting for state machine apply.
///
/// **Drain pattern**: `split_off(&(new_commit + 1))` ensures each action fires exactly once.
///
/// **Lifecycle**:
/// 1. Insert: When entry appended (e.g., noop at index 5 → insert(5, LeaderNoop))
/// 2. Drain: When commit_index ≥ 5 → `drain_commit_actions` → send RoleEvent
/// 3. Timeout: `tick()` scans deadlines → expired entries get error responses
///
/// **Contrast with `pending_client_writes`**:
/// - `pending_commit_actions`: Protocol metadata, respond on commit
/// - `pending_client_writes`: User data, may require SM apply (CAS, etc.)
pub(super) pending_commit_actions: BTreeMap<u64, PostCommitEntry>,
/// DIAG: wall-clock timestamp when each write entry was registered (propose time).
/// Key: log entry index. Cleared on apply or error drain.
write_propose_times: HashMap<u64, std::time::Instant>,
/// Per-follower replication worker handles. Key = follower node_id.
/// Workers send AppendEntries via transport and relay results back as RoleEvent::AppendResult.
/// Dropped on role change (LeaderState drop) → workers exit via channel close.
replication_workers: HashMap<u32, ReplicationWorkerHandle>,
// -- Metrics (optional, encapsulated) --
/// Backpressure metrics context (None when metrics disabled)
backpressure_metrics: Option<Arc<BackpressureMetrics>>,
// -- Type System Marker --
/// Phantom data for type parameter anchoring
_marker: PhantomData<T>,
}
#[async_trait]
impl<T: TypeConfig> RaftRoleState for LeaderState<T> {
type T = T;
fn shared_state(&self) -> &SharedState {
&self.shared_state
}
fn shared_state_mut(&mut self) -> &mut SharedState {
&mut self.shared_state
}
///Overwrite default behavior.
/// As leader, I should not receive commit index,
/// which is lower than my current one
#[tracing::instrument]
fn update_commit_index(
&mut self,
new_commit_index: u64,
) -> Result<()> {
if self.commit_index() < new_commit_index {
debug!("update_commit_index to: {:?}", new_commit_index);
self.shared_state.commit_index = new_commit_index;
} else {
warn!(
"Illegal operation, might be a bug! I am Leader old_commit_index({}) >= new_commit_index:({})",
self.commit_index(),
new_commit_index
)
}
Ok(())
}
/// As Leader should not vote any more
fn voted_for(&self) -> Result<Option<VotedFor>> {
self.shared_state().voted_for()
}
/// As Leader might also be able to vote ,
/// if new legal Leader found
fn update_voted_for(
&mut self,
voted_for: VotedFor,
) -> Result<bool> {
self.shared_state_mut().update_voted_for(voted_for)
}
fn next_index(
&self,
node_id: u32,
) -> Option<u64> {
Some(if let Some(n) = self.next_index.get(&node_id) {
*n
} else {
1
})
}
fn update_next_index(
&mut self,
node_id: u32,
new_next_id: u64,
) -> Result<()> {
// Pipeline ACKs can arrive out of order: a stale conflict response must never
// retreat next_index below what match_index already confirms.
// Floor = match_index + 1 (the minimum safe starting point for the next send).
let floor = self.match_index.get(&node_id).copied().unwrap_or(0) + 1;
let safe = new_next_id.max(floor);
debug!(
"update_next_index({}) to {} (floor={})",
node_id, safe, floor
);
self.next_index.insert(node_id, safe);
Ok(())
}
fn update_match_index(
&mut self,
node_id: u32,
new_match_id: u64,
) -> Result<()> {
// Pipeline responses can arrive out of order; only advance, never retreat.
let current = self.match_index.get(&node_id).copied().unwrap_or(0);
if new_match_id > current {
self.match_index.insert(node_id, new_match_id);
}
Ok(())
}
fn match_index(
&self,
node_id: u32,
) -> Option<u64> {
self.match_index.get(&node_id).copied()
}
fn init_peers_next_index_and_match_index(
&mut self,
last_entry_id: u64,
peer_ids: Vec<u32>,
) -> Result<()> {
for peer_id in peer_ids {
debug!("init leader state for peer_id: {:?}", peer_id);
let new_next_id = last_entry_id + 1;
self.update_next_index(peer_id, new_next_id)?;
self.update_match_index(peer_id, 0)?;
}
Ok(())
}
async fn handle_zombie_detected(
&mut self,
node_id: u32,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
ctx: &RaftContext<T>,
) -> Result<()> {
self.handle_zombie_node(node_id, role_tx, ctx).await
}
fn handle_snapshot_push_completed(
&mut self,
peer_id: u32,
success: bool,
policy: &crate::InstallSnapshotBackoffPolicy,
node_id: u32,
) {
self.handle_snapshot_push_completed(peer_id, success, policy, node_id);
}
fn noop_log_id(&self) -> Result<Option<u64>> {
Ok(self.noop_log_id)
}
/// Override: Initialize cluster metadata after becoming leader.
/// Must be called once after leader election succeeds.
async fn init_cluster_metadata(
&mut self,
membership: &Arc<T::M>,
) -> Result<()> {
// Calculate total voter count including self as leader
let voters = membership.voters().await;
let total_voters = voters.len() + 1; // +1 for leader (self)
// Get all replication targets (voters + learners, excluding self)
let replication_targets = membership.replication_peers().await;
// Single-voter cluster: only this node is a voter (quorum = 1)
let single_voter = total_voters == 1;
self.cluster_metadata = ClusterMetadata {
single_voter,
total_voters,
replication_targets: replication_targets.clone(),
};
debug!(
"Initialized cluster metadata: single_voter={}, total_voters={}, replication_targets={}",
single_voter,
total_voters,
replication_targets.len()
);
Ok(())
}
fn is_leader(&self) -> bool {
true
}
fn become_leader(&self) -> Result<RaftRole<T>> {
warn!("I am leader already");
Err(StateTransitionError::InvalidTransition.into())
}
fn become_candidate(&self) -> Result<RaftRole<T>> {
error!("Leader can not become Candidate");
Err(StateTransitionError::InvalidTransition.into())
}
fn become_follower(&self) -> Result<RaftRole<T>> {
info!(
"Node {} term {} transitioning to Follower",
self.node_id(),
self.current_term(),
);
println!(
"[Node {}] Leader → Follower (term {})",
self.node_id(),
self.current_term()
);
Ok(RaftRole::Follower(Box::new(self.into())))
}
fn become_learner(&self) -> Result<RaftRole<T>> {
error!("Leader can not become Learner");
Err(StateTransitionError::InvalidTransition.into())
}
fn is_timer_expired(&self) -> bool {
self.timer.is_expired()
}
/// Raft starts, we will check if we need reset all timer
fn reset_timer(&mut self) {
self.timer.reset_replication();
}
fn next_deadline(&self) -> Instant {
self.timer.next_deadline()
}
/// Trigger heartbeat now
async fn tick(
&mut self,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
_raft_tx: &mpsc::Sender<RaftEvent>,
ctx: &RaftContext<T>,
) -> Result<()> {
let now = Instant::now();
// Keep syncing leader_id (hot-path: ~5ns atomic store)
self.shared_state().set_current_leader(self.node_id());
// 1. Stale learner O(1) deadline check — None short-circuits immediately
if self.stale_check_deadline.is_some_and(|d| now >= d)
&& let Err(e) = self.check_and_purge_stale_front(role_tx, ctx).await
{
error!("Stale learner check failed: {}", e);
}
// 2. Heartbeat trigger check
// Send heartbeat if the replication timer expires
if now >= self.timer.replication_deadline() {
debug!(?now, "tick::reset_replication timer");
// Piggyback pending writes onto heartbeat; if nothing to write,
// send an empty AppendEntries to maintain leadership and reset timer.
let request = self.propose_buffer.flush();
self.send_heartbeat_or_batch(request, role_tx, ctx).await?;
}
// 3. Drain expired pending client writes.
// NOTE: expiry checked at tick granularity; actual timeout may be up to one tick late.
self.pending_client_writes.retain(|_, meta| {
if now >= meta.deadline {
for sender in meta.senders.drain(..) {
let _ = sender.send(Err(Status::deadline_exceeded("write request timeout")));
}
false
} else {
true
}
});
// 4. Drain expired pending linearizable reads.
self.pending_reads.retain(|_, batch| {
if now >= batch.deadline {
for (_, sender) in batch.requests.drain(..) {
let _ = sender.send(Err(Status::deadline_exceeded("read request timeout")));
}
false
} else {
true
}
});
// 4b. Drain expired pending lease reads.
let mut unexpired_reads = VecDeque::new();
for entry in self.pending_lease_reads.drain(..) {
if now >= entry.deadline {
let _ = entry.sender.send(Err(Status::deadline_exceeded("lease read timeout")));
} else {
unexpired_reads.push_back(entry);
}
}
self.pending_lease_reads = unexpired_reads;
// 5. Drain expired post-commit actions (e.g. noop/join timed out before quorum).
// A noop timeout means we cannot confirm leadership — step down immediately.
// A join timeout sends a deadline_exceeded error to the waiting client.
let expired_keys: Vec<u64> = self
.pending_commit_actions
.iter()
.filter(|(_, e)| now >= e.deadline)
.map(|(&k, _)| k)
.collect();
let mut noop_timed_out = false;
for key in expired_keys {
if let Some(entry) = self.pending_commit_actions.remove(&key) {
match entry.action {
PostCommitAction::LeaderNoop { .. } => {
noop_timed_out = true;
}
PostCommitAction::NodeJoin { sender, .. } => {
let _ = sender.send(Err(Status::deadline_exceeded("join commit timeout")));
}
}
}
}
if noop_timed_out {
warn!("LeaderNoop commit timed out — stepping down");
let _ = role_tx.send(RoleEvent::BecomeFollower(None));
}
Ok(())
}
fn drain_read_buffer(&mut self) -> Result<()> {
// Drain linearizable read buffer
let batch = self.linearizable_read_buffer.take_all();
if !batch.is_empty() {
warn!(
"Read batch: draining {} linearizable read requests due to role change",
batch.len()
);
for (_, sender) in batch {
let _ = sender.send(Err(tonic::Status::unavailable("Leader stepped down")));
}
}
// Drain lease read queue
if !self.lease_read_queue.is_empty() {
warn!(
"Read batch: draining {} lease read requests due to role change",
self.lease_read_queue.len()
);
for (_, sender) in self.lease_read_queue.drain(..) {
let _ = sender.send(Err(tonic::Status::unavailable("Leader stepped down")));
}
}
// Drain eventual read queue
if !self.eventual_read_queue.is_empty() {
warn!(
"Read batch: draining {} eventual read requests due to role change",
self.eventual_read_queue.len()
);
for (_, sender) in self.eventual_read_queue.drain(..) {
let _ = sender.send(Err(tonic::Status::unavailable("Leader stepped down")));
}
}
// Drain pending linearizable reads awaiting ApplyCompleted
if !self.pending_reads.is_empty() {
let count: usize = self.pending_reads.values().map(|b| b.requests.len()).sum();
warn!(
"Read batch: draining {} pending linearizable reads due to role change",
count
);
for (_, batch) in std::mem::take(&mut self.pending_reads) {
for (_, sender) in batch.requests {
let _ = sender.send(Err(tonic::Status::unavailable("Leader stepped down")));
}
}
}
// Drain pending lease reads awaiting quorum ACK
if !self.pending_lease_reads.is_empty() {
warn!(
"Read batch: draining {} pending lease reads due to role change",
self.pending_lease_reads.len()
);
for entry in self.pending_lease_reads.drain(..) {
let _ = entry.sender.send(Err(tonic::Status::unavailable("Leader stepped down")));
}
}
// Drain write buffer (propose_buffer)
if !self.propose_buffer.is_empty() {
warn!(
"Write batch: draining {} pending write requests due to role change",
self.propose_buffer.len()
);
if let Some(batch) = self.propose_buffer.flush() {
for sender in batch.senders {
let _ = sender.send(Err(tonic::Status::failed_precondition("Not leader")));
}
}
}
// Drain pending client writes (already in log, quorum not yet achieved).
if !self.pending_client_writes.is_empty() {
let count: usize = self.pending_client_writes.values().map(|m| m.senders.len()).sum();
warn!(
"Draining {} pending write responses due to role change",
count
);
self.drain_pending_writes_with_error(ErrorCode::ProposeFailed);
}
Ok(())
}
fn push_client_cmd(
&mut self,
cmd: ClientCmd,
ctx: &RaftContext<Self::T>,
) {
use crate::client_command_to_entry_payloads;
let backpressure = &ctx.node_config.raft.backpressure;
match cmd {
ClientCmd::Propose(req, sender) => {
let current_pending = self.propose_buffer.len();
// Record buffer utilization metric (with sampling)
if let Some(ref metrics) = self.backpressure_metrics
&& backpressure.max_pending_writes > 0
{
let utilization =
(current_pending as f64 / backpressure.max_pending_writes as f64) * 100.0;
metrics.record_buffer_utilization(utilization, true);
}
// Check write backpressure limit
if backpressure.should_reject_write(current_pending) {
// Record rejection metric
if let Some(ref metrics) = self.backpressure_metrics {
metrics.record_rejection(true);
}
let _ = sender.send(Err(Status::resource_exhausted(
"Too many pending write requests",
)));
return;
}
// Convert command to payload (will be merged in drain_batch)
if let Some(cmd) = req.command {
let payload = client_command_to_entry_payloads(vec![write_op_to_proto(cmd)])
.into_iter()
.next()
.expect("client_command_to_entry_payloads should return 1 element");
// Push lightweight tuple directly (zero-copy, no Vec allocation)
self.propose_buffer.push(payload, sender);
} else {
// Empty command: reject
let _ = sender.send(Err(Status::invalid_argument("Command is empty")));
}
}
ClientCmd::Read(req, sender) => {
// Determine effective read consistency policy
let effective_policy = self.determine_read_policy(&req);
// Route to appropriate buffer based on policy
match effective_policy {
ServerReadConsistencyPolicy::LinearizableRead => {
let current_pending = self.linearizable_read_buffer.len();
// Record buffer utilization metric (with sampling)
if let Some(ref metrics) = self.backpressure_metrics
&& backpressure.max_pending_reads > 0
{
let utilization = (current_pending as f64
/ backpressure.max_pending_reads as f64)
* 100.0;
metrics.record_buffer_utilization(utilization, false);
}
// Check read backpressure limit
if backpressure.should_reject_read(current_pending) {
// Record rejection metric
if let Some(ref metrics) = self.backpressure_metrics {
metrics.record_rejection(false);
}
let _ = sender.send(Err(Status::resource_exhausted(
"Too many pending read requests",
)));
return;
}
self.linearizable_read_buffer.push((req, sender));
}
ServerReadConsistencyPolicy::LeaseRead => {
let current_pending = self.lease_read_queue.len();
// Record buffer utilization metric (with sampling)
if let Some(ref metrics) = self.backpressure_metrics
&& backpressure.max_pending_reads > 0
{
let utilization = (current_pending as f64
/ backpressure.max_pending_reads as f64)
* 100.0;
metrics.record_buffer_utilization(utilization, false);
}
// Check read backpressure limit
if backpressure.should_reject_read(current_pending) {
// Record rejection metric
if let Some(ref metrics) = self.backpressure_metrics {
metrics.record_rejection(false);
}
let _ = sender.send(Err(Status::resource_exhausted(
"Too many pending read requests",
)));
return;
}
self.lease_read_queue.push_back((req, sender));
}
ServerReadConsistencyPolicy::EventualConsistency => {
let current_pending = self.eventual_read_queue.len();
// Record buffer utilization metric (with sampling)
if let Some(ref metrics) = self.backpressure_metrics
&& backpressure.max_pending_reads > 0
{
let utilization = (current_pending as f64
/ backpressure.max_pending_reads as f64)
* 100.0;
metrics.record_buffer_utilization(utilization, false);
}
// Check read backpressure limit
if backpressure.should_reject_read(current_pending) {
// Record rejection metric
if let Some(ref metrics) = self.backpressure_metrics {
metrics.record_rejection(false);
}
let _ = sender.send(Err(Status::resource_exhausted(
"Too many pending read requests",
)));
return;
}
self.eventual_read_queue.push_back((req, sender));
}
}
}
ClientCmd::Scan(prefix, sender) => {
let result = ctx.state_machine().scan_prefix(&prefix);
let _ = sender.send(result.map_err(|e| Status::internal(e.to_string())));
}
}
}
async fn flush_cmd_buffers(
&mut self,
ctx: &RaftContext<Self::T>,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
) -> Result<()> {
// Drain-based: unconditionally flush all buffered commands
// No timeout/size checks - drain from channel already collected the batch
let has_writes = !self.propose_buffer.is_empty();
let has_reads = !self.linearizable_read_buffer.is_empty();
// Fast path: pure write workload (most common case, ~95%)
// Preserves original high-performance code path for write-heavy workloads
if has_writes && !has_reads {
if let Some(request) = self.propose_buffer.flush() {
self.process_batch(std::iter::once(request), role_tx, ctx).await?;
}
}
// Unified path: mixed write+read or pure read workloads
// Merges RPC for 2*RTT → 1*RTT optimization in mixed scenarios
else if has_writes || has_reads {
self.unified_write_and_linear_read(ctx, role_tx).await?;
}
// Process lease reads (immediate, no batching)
while let Some((req, sender)) = self.lease_read_queue.pop_front() {
if let Err(e) = self.process_lease_read(req, sender, ctx, role_tx).await {
error!("process_lease_read failed: {:?}", e);
}
}
// Process eventual consistency reads (immediate, no batching)
while let Some((req, sender)) = self.eventual_read_queue.pop_front() {
self.process_eventual_read(req, sender, ctx);
}
Ok(())
}
async fn handle_raft_event(
&mut self,
raft_event: RaftEvent,
ctx: &RaftContext<T>,
role_tx: mpsc::UnboundedSender<RoleEvent>,
) -> Result<()> {
let my_id = self.shared_state.node_id;
let my_term = self.current_term();
match raft_event {
// Leader receives RequestVote(term=X, candidate=Y)
// 1. If X > currentTerm:
// - Leader → Follower, currentTerm = X
// - Replay event
// 2. Else:
// - Reply with VoteGranted=false, currentTerm=currentTerm
RaftEvent::ReceiveVoteRequest(vote_request, sender) => {
debug!(
"handle_raft_event::RaftEvent::ReceiveVoteRequest: {:?}",
&vote_request
);
let my_term = self.current_term();
if my_term < vote_request.term {
self.update_current_term(vote_request.term);
// Step down as Follower
self.send_become_follower_event(None, &role_tx)?;
info!("Leader will not process Vote request, it should let Follower do it.");
send_replay_raft_event(
&role_tx,
RaftEvent::ReceiveVoteRequest(vote_request, sender),
)?;
} else {
let last_log_id =
ctx.raft_log().last_log_id().unwrap_or(LogId { index: 0, term: 0 });
let response = VoteResponse {
term: my_term,
vote_granted: false,
last_log_index: last_log_id.index,
last_log_term: last_log_id.term,
};
sender.send(Ok(response)).map_err(|e| {
let error_str = format!("{e:?}");
error!("Failed to send: {}", error_str);
NetworkError::SingalSendFailed(error_str)
})?;
}
}
RaftEvent::ClusterConf(_metadata_request, sender) => {
// Hide leader ID until noop commits: before noop, the leader has not confirmed
// quorum readiness. Exposing current_leader_id too early causes clients to route
// to this leader, which then rejects them with LeaderNotReady.
// `Option::and` returns None if noop_log_id is None, Some(id) otherwise.
let current_leader = self.noop_log_id.and(self.shared_state().current_leader());
let cluster_conf =
ctx.membership().retrieve_cluster_membership_config(current_leader).await;
debug!("Leader receive ClusterConf: {:?}", &cluster_conf);
if let Err(e) = sender.send(Ok(cluster_conf)) {
// Receiver timed out and dropped — this is normal, do not crash the node
error!(
"Failed to send ClusterConf response (receiver dropped): {:?}",
e
);
}
}
RaftEvent::ClusterConfUpdate(cluste_conf_change_request, sender) => {
let current_conf_version = ctx.membership().get_cluster_conf_version().await;
debug!(%current_conf_version, ?cluste_conf_change_request,
"handle_raft_event::RaftEvent::ClusterConfUpdate",
);
// Reject the fake Leader append entries request
if my_term >= cluste_conf_change_request.term {
let response = ClusterConfUpdateResponse::higher_term(
my_id,
my_term,
current_conf_version,
);
sender.send(Ok(response)).map_err(|e| {
let error_str = format!("{e:?}");
error!("Failed to send: {}", error_str);
NetworkError::SingalSendFailed(error_str)
})?;
} else {
// Step down as Follower as new Leader found
info!(
"my({}) term < request one, now I will step down to Follower",
my_id
);
//TODO: if there is a bug? self.update_current_term(vote_request.term);
self.send_become_follower_event(Some(cluste_conf_change_request.id), &role_tx)?;
info!(
"Leader will not process append_entries_request, it should let Follower do it."
);
send_replay_raft_event(
&role_tx,
RaftEvent::ClusterConfUpdate(cluste_conf_change_request, sender),
)?;
}
}
RaftEvent::AppendEntries(append_entries_request, sender) => {
debug!(
"handle_raft_event::RaftEvent::AppendEntries: {:?}",
&append_entries_request
);
// Reject the fake Leader append entries request
if my_term >= append_entries_request.term {
let response = AppendEntriesResponse::higher_term(my_id, my_term);
sender.send(Ok(response)).map_err(|e| {
let error_str = format!("{e:?}");
error!("Failed to send: {}", error_str);
NetworkError::SingalSendFailed(error_str)
})?;
} else {
// Step down as Follower as new Leader found
info!(
"my({}) term < request one, now I will step down to Follower",
my_id
);
//TODO: if there is a bug? self.update_current_term(vote_request.term);
self.send_become_follower_event(
Some(append_entries_request.leader_id),
&role_tx,
)?;
info!(
"Leader will not process append_entries_request, it should let Follower do it."
);
send_replay_raft_event(
&role_tx,
RaftEvent::AppendEntries(append_entries_request, sender),
)?;
}
}
RaftEvent::InstallSnapshotChunk(_streaming, sender) => {
sender
.send(Err(Status::permission_denied("Not Follower or Learner. ")))
.map_err(|e| {
let error_str = format!("{e:?}");
error!("Failed to send: {}", error_str);
NetworkError::SingalSendFailed(error_str)
})?;
return Err(ConsensusError::RoleViolation {
current_role: "Leader",
required_role: "Follower or Learner",
context: format!(
"Leader node {} receives RaftEvent::InstallSnapshotChunk",
ctx.node_id
),
}
.into());
}
RaftEvent::CreateSnapshotEvent => {
// Prevent duplicate snapshot creation
if self.snapshot_in_progress.load(std::sync::atomic::Ordering::Acquire) {
info!("Snapshot creation already in progress. Skipping duplicate request.");
return Ok(());
}
self.snapshot_in_progress.store(true, std::sync::atomic::Ordering::Release);
let state_machine_handler = ctx.state_machine_handler().clone();
// Use spawn to perform snapshot creation in the background
tokio::spawn(async move {
let result = state_machine_handler.create_snapshot().await;
info!("SnapshotCreated event will be processed in another event thread");
if let Err(e) =
send_replay_raft_event(&role_tx, RaftEvent::SnapshotCreated(result))
{
error!("Failed to send snapshot creation result: {}", e);
}
});
}
RaftEvent::SnapshotCreated(result) => {
self.snapshot_in_progress.store(false, Ordering::SeqCst);
match result {
Err(e) => {
error!(%e, "State machine snapshot creation failed");
}
Ok((
SnapshotMetadata {
last_included: last_included_option,
checksum: _,
},
_final_path,
)) => {
info!("Initiating log purge after snapshot creation");
if let Some(last_included) = last_included_option {
// ----------------------
// Phase 1: Schedule log purge if possible
// ----------------------
trace!("Phase 1: Schedule log purge if possible");
if self.can_purge_logs(self.last_purged_index, last_included) {
trace!(?last_included, "Phase 1: Scheduling log purge");
self.scheduled_purge_upto(last_included);
}
// ----------------------
// Phase 2: Execute local purge
// ----------------------
// Per Raft §7: Leader purges independently without peer coordination
trace!("Phase 2: Execute scheduled purge task");
debug!(?last_included, "Execute scheduled purge task");
if let Some(scheduled) = self.scheduled_purge_upto {
let purge_executor = ctx.purge_executor();
match purge_executor.execute_purge(scheduled).await {
Ok(_) => {
if let Err(e) = send_replay_raft_event(
&role_tx,
RaftEvent::LogPurgeCompleted(scheduled),
) {
error!(%e, "Failed to notify purge completion");
}
}
Err(e) => {
error!(?e, ?scheduled, "Log purge execution failed");
}
}
}
}
}
}
}
RaftEvent::LogPurgeCompleted(purged_id) => {
// Ensure we don't regress the purge index
if self.last_purged_index.map_or(true, |current| purged_id.index > current.index) {
debug!(
?purged_id,
"Updating last purged index after successful execution"
);
self.last_purged_index = Some(purged_id);
} else {
warn!(
?purged_id,
?self.last_purged_index,
"Received outdated purge completion, ignoring"
);
}
}
RaftEvent::JoinCluster(join_request, sender) => {
debug!(?join_request, "Leader::RaftEvent::JoinCluster");
self.handle_join_cluster(join_request, sender, ctx, &role_tx).await?;
}
RaftEvent::DiscoverLeader(request, sender) => {
debug!(?request, "Leader::RaftEvent::DiscoverLeader");
if let Some(meta) = ctx.membership().retrieve_node_meta(my_id).await {
let response = LeaderDiscoveryResponse {
leader_id: my_id,
leader_address: meta.address,
term: my_term,
};
sender.send(Ok(response)).map_err(|e| {
let error_str = format!("{e:?}");
error!("Failed to send: {}", error_str);
NetworkError::SingalSendFailed(error_str)
})?;
return Ok(());
} else {
let msg = "Leader can not find its address? It must be a bug.";
error!("{}", msg);
panic!("{}", msg);
}
}
RaftEvent::StreamSnapshot(request, sender) => {
debug!("Leader::RaftEvent::StreamSnapshot");
// Get the latest snapshot metadata
if let Some(metadata) = ctx.state_machine().snapshot_metadata() {
// Create response channel
let (response_tx, response_rx) =
mpsc::channel::<std::result::Result<Arc<SnapshotChunk>, Status>>(32);
// Convert to properly encoded tonic stream
let size = 1024 * 1024 * 1024; // 1GB max message size
let response_stream = create_production_snapshot_stream(response_rx, size);
// Immediately respond with the stream
sender.send(Ok(response_stream)).map_err(|e| {
let error_str = format!("{e:?}");
error!("Stream response failed: {}", error_str);
NetworkError::SingalSendFailed(error_str)
})?;
// Spawn background transfer task
let state_machine_handler = ctx.state_machine_handler().clone();
let config = ctx.node_config.raft.snapshot.clone();
// Load snapshot data stream
let data_stream =
state_machine_handler.load_snapshot_data(metadata.clone()).await?;
tokio::spawn(async move {
if let Err(e) = BackgroundSnapshotTransfer::<T>::run_pull_transfer(
request,
response_tx,
data_stream,
config,
)
.await
{
error!("StreamSnapshot failed: {:?}", e);
}
});
} else {
warn!("No snapshot available for streaming");
sender.send(Err(Status::not_found("Snapshot not found"))).map_err(|e| {
let error_str = format!("{e:?}");
error!("Stream response failed: {}", error_str);
NetworkError::SingalSendFailed(error_str)
})?;
}
}
RaftEvent::PromoteReadyLearners => {
// SAFETY: Called from main event loop, no reentrancy issues
info!(
"[Leader {}] ⚡ PromoteReadyLearners event received, pending_promotions: {:?}",
self.node_id(),
self.pending_promotions.iter().map(|p| p.node_id).collect::<Vec<_>>()
);
self.process_pending_promotions(ctx, &role_tx).await?;
}
RaftEvent::MembershipApplied => {
// Save old membership for comparison
let old_replication_targets = self.cluster_metadata.replication_targets.clone();
// Refresh cluster metadata cache after membership change is applied
debug!("Refreshing cluster metadata cache after membership change");
if let Err(e) = self.update_cluster_metadata(&ctx.membership()).await {
warn!("Failed to update cluster metadata: {:?}", e);
}
// CRITICAL FIX #218: Initialize replication state for newly added peers
// Per Raft protocol: When new members join, Leader must initialize their next_index
let newly_added: Vec<u32> = self
.cluster_metadata
.replication_targets
.iter()
.filter(|new_peer| {
!old_replication_targets.iter().any(|old_peer| old_peer.id == new_peer.id)
})
.map(|peer| peer.id)
.collect();
if !newly_added.is_empty() {
debug!(
"Initializing replication state for {} new peer(s): {:?}",
newly_added.len(),
newly_added
);
let last_entry_id = ctx.raft_log().last_entry_id();
if let Err(e) = self
.init_peers_next_index_and_match_index(last_entry_id, newly_added.clone())
{
warn!("Failed to initialize next_index for new peers: {:?}", e);
// Non-fatal: next_index will use default value of 1,
// replication will still work but may be less efficient
} else {
trace!(
"[MEMBERSHIP-APPLIED-LEADER] Successfully initialized next_index/match_index for peers: {:?}",
newly_added
);
}
} else {
trace!("[MEMBERSHIP-APPLIED-LEADER] No newly added peers detected");
}
// Drop workers for peers that were removed from membership.
// Dropping ReplicationWorkerHandle closes task_tx → worker exits naturally.
let removed: Vec<u32> = old_replication_targets
.iter()
.filter(|old_peer| {
!self
.cluster_metadata
.replication_targets
.iter()
.any(|new_peer| new_peer.id == old_peer.id)
})
.map(|peer| peer.id)
.collect();
for removed_id in &removed {
if self.replication_workers.remove(removed_id).is_some() {
debug!("Dropped replication worker for removed peer {}", removed_id);
}
}
}
RaftEvent::FatalError { source, error } => {
error!("[Leader] Fatal error from {}: {}", source, error);
let fatal_status = || tonic::Status::internal(format!("Node fatal error: {error}"));
// Notify all pending write requests
let pending: Vec<_> = self.pending_write_apply.drain().collect();
if !pending.is_empty() {
warn!(
"[Leader] FatalError: notifying {} pending write requests",
pending.len()
);
for (_index, sender) in pending {
let _ = sender.send(Err(fatal_status()));
}
}
// Notify buffered linearizable reads (pre-flush)
let lin_buf = self.linearizable_read_buffer.take_all();
if !lin_buf.is_empty() {
warn!(
"[Leader] FatalError: notifying {} buffered linearizable reads",
lin_buf.len()
);
for (_, sender) in lin_buf {
let _ = sender.send(Err(fatal_status()));
}
}
// Notify pending linearizable reads awaiting ApplyCompleted
if !self.pending_reads.is_empty() {
let count: usize = self.pending_reads.values().map(|b| b.requests.len()).sum();
warn!(
"[Leader] FatalError: notifying {} pending linearizable reads",
count
);
for (_, batch) in std::mem::take(&mut self.pending_reads) {
for (_, sender) in batch.requests {
let _ = sender.send(Err(fatal_status()));
}
}
}
// Notify queued lease reads
if !self.lease_read_queue.is_empty() {
warn!(
"[Leader] FatalError: notifying {} queued lease reads",
self.lease_read_queue.len()
);
for (_, sender) in self.lease_read_queue.drain(..) {
let _ = sender.send(Err(fatal_status()));
}
}
// Notify queued eventual reads
if !self.eventual_read_queue.is_empty() {
warn!(
"[Leader] FatalError: notifying {} queued eventual reads",
self.eventual_read_queue.len()
);
for (_, sender) in self.eventual_read_queue.drain(..) {
let _ = sender.send(Err(fatal_status()));
}
}
return Err(crate::Error::Fatal(format!(
"Fatal error from {source}: {error}"
)));
}
RaftEvent::StepDownSelfRemoved => {
// Only Leader can propose configuration changes and remove itself
// Per Raft protocol: Leader steps down immediately after self-removal
warn!(
"[Leader-{}] Removed from cluster membership, stepping down to Follower",
self.node_id()
);
role_tx.send(RoleEvent::BecomeFollower(None)).map_err(|e| {
error!(
"[Leader-{}] Failed to send BecomeFollower after self-removal: {:?}",
self.node_id(),
e
);
NetworkError::SingalSendFailed(format!(
"BecomeFollower after self-removal: {e:?}"
))
})?;
return Ok(());
}
}
Ok(())
}
async fn handle_apply_completed(
&mut self,
last_index: u64,
results: Vec<crate::ApplyResult>,
ctx: &RaftContext<T>,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
) -> Result<()> {
let num_results = results.len();
// Match apply results to pending client requests and send responses.
let responses: Vec<_> = results
.into_iter()
.filter_map(|r| self.pending_write_apply.remove(&r.index).map(|sender| (r, sender)))
.collect();
for (result, sender) in responses {
let e2e_ms = self
.write_propose_times
.remove(&result.index)
.map(|t| t.elapsed().as_millis())
.unwrap_or(0);
if e2e_ms > 0 {
metrics::histogram!("raft.write.propose_to_apply_ms").record(e2e_ms as f64);
}
let response = if result.succeeded {
ClientResponse::write_success()
} else {
ClientResponse::cas_failure()
};
trace!(
"[Leader-{}] Sending response to client for index {}: succeeded={}",
self.node_id(),
result.index,
result.succeeded
);
if sender.send(Ok(response)).is_err() {
trace!(
"[Leader-{}] Client receiver dropped for index {}",
self.node_id(),
result.index
);
}
}
// Serve pending linearizable reads whose read_index <= last_index.
let reads_to_serve: Vec<_> =
self.pending_reads.range(..=last_index).map(|(k, _)| *k).collect();
for read_index in reads_to_serve {
if let Some(batch) = self.pending_reads.remove(&read_index) {
self.execute_pending_reads(batch.requests, ctx);
}
}
// Check snapshot after SM apply — last_applied is now accurate.
check_and_trigger_snapshot(last_index, Leader as i32, self.current_term(), ctx, role_tx)?;
trace!(
"[Leader-{}] TIMING: process_apply_completed({} results)",
self.node_id(),
num_results,
);
Ok(())
}
/// Handle LogFlushed(durable): re-calculate commit_index after local log entries are crash-safe.
///
/// Leader also writes to the local raft log. process_batch reads durable_index() synchronously,
/// which may be 0 (flush not yet complete) — so commit never advances without this handler.
/// Follower ACK: handled via pending_flush. Leader commit: handled here.
async fn handle_log_flushed(
&mut self,
durable: u64,
ctx: &RaftContext<T>,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
) {
let new_commit_index = if self.cluster_metadata.single_voter {
// MemFirst single-voter: LogFlushed(durable) is the IO checkpoint.
// Commit to last_entry_id() — not just durable — to allow pipelining
// across IO batch boundaries. Matches multi-voter MemFirst where leader
// contributes last_entry_id() to quorum (not durable_index).
let last_log_index = ctx.raft_log().last_entry_id();
debug_assert!(
last_log_index >= durable,
"last_entry_id ({last_log_index}) must be >= durable ({durable})"
);
if last_log_index > self.commit_index() {
Some(last_log_index)
} else {
None
}
} else {
// Multi-voter: quorum of match_index determines commit.
// Only voter peers (non-Learner role) may contribute to majority.
// Learners replicate entries but must never count toward commit quorum.
self.calculate_new_commit_index(ctx.raft_log())
};
if let Some(new_commit) = new_commit_index {
if let Err(e) = self.update_commit_index_with_signal(
Leader as i32,
self.current_term(),
new_commit,
role_tx,
) {
error!(
?e,
"handle_log_flushed: update_commit_index_with_signal failed"
);
} else {
// Drain pending writes committed via local flush (single-voter or flush path).
self.drain_pending_client_writes(new_commit);
// Fire post-commit actions (noop confirmation, join responses) for this commit.
self.drain_commit_actions(new_commit, ctx, role_tx).await;
// Single-voter: log flush confirms leadership (leader is the entire quorum).
// Refresh lease timestamp after all post-commit work to eliminate the yield-point
// race window introduced when drain_commit_actions became async.
if self.cluster_metadata.single_voter {
self.update_lease_timestamp();
self.drain_pending_lease_reads(ctx);
}
}
}
}
async fn handle_append_result(
&mut self,
follower_id: u32,
result: Result<AppendEntriesResponse>,
ctx: &RaftContext<T>,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
) -> Result<()> {
let leader_term = self.current_term();
let response = match result {
Err(e) => {
// ResponseChannelClosed means the follower's pending_response_tx was replaced
// by a newer AppendEntries (pipeline replication). The old sender was dropped,
// but the follower still has the entry in memory and will flush it.
// Safety: by Raft Log Matching Property, if follower ACKs index=N,
// it implicitly confirms all entries up to N including this one.
// Do NOT retry — the higher-index ACK will update match_index correctly.
if matches!(
e,
Error::System(SystemError::Network(NetworkError::ResponseChannelClosed))
) {
debug!(
"follower {} sender superseded (ResponseChannelClosed) — \
waiting for higher-index ACK",
follower_id
);
return Ok(());
}
// Real network error — log and continue; worker handles retries per BackoffPolicy.
warn!(
"AppendEntries to peer {} network error: {:?}",
follower_id, e
);
return Ok(());
}
Ok(resp) => resp,
};
// Skip stale responses from earlier terms.
if response.term < leader_term {
debug!(
"Ignoring stale AppendResult from peer {} (term {} < {})",
follower_id, response.term, leader_term
);
return Ok(());
}
// Higher term: step down immediately.
if response.term > leader_term {
warn!(
"HigherTerm {} from peer {} — stepping down",
response.term, follower_id
);
self.update_current_term(response.term);
self.drain_pending_writes_with_error(ErrorCode::TermOutdated);
self.send_become_follower_event(None, role_tx)?;
return Err(ReplicationError::HigherTerm(response.term).into());
}
// Determine if this peer is a voter (affects quorum calculation and learner checks).
let is_voter = self.cluster_metadata.replication_targets.iter().any(|n| {
n.id == follower_id && n.role != d_engine_proto::common::NodeRole::Learner as i32
});
// Process success / conflict / embedded higher-term.
let peer_update = match response.result {
Some(append_entries_response::Result::Success(success)) => ctx
.replication_handler()
.handle_success_response(follower_id, response.term, success, leader_term)?,
Some(append_entries_response::Result::Conflict(conflict)) => {
let current_next_index = self.next_index.get(&follower_id).copied().unwrap_or(1);
ctx.replication_handler().handle_conflict_response(
follower_id,
conflict,
ctx.raft_log(),
current_next_index,
)?
}
Some(append_entries_response::Result::HigherTerm(term)) => {
if term > leader_term {
self.update_current_term(term);
self.drain_pending_writes_with_error(ErrorCode::TermOutdated);
self.send_become_follower_event(None, role_tx)?;
return Err(ReplicationError::HigherTerm(term).into());
}
return Ok(());
}
None => {
error!(
"AppendResult from peer {} has no result variant",
follower_id
);
return Ok(());
}
};
// Update next_index and match_index for this peer.
let peer_updates = HashMap::from([(follower_id, peer_update.clone())]);
self.update_peer_indexes(&peer_updates);
// Check learner promotion progress.
if !is_voter {
// Collect the current progress of all learners
let learner_progress: HashMap<u32, Option<u64>> = self
.match_index
.iter()
.filter_map(|(&peer_id, &match_idx)| {
// Check whether this peer is a learner
let is_learner = self.cluster_metadata.replication_targets.iter().any(|n| {
n.id == peer_id
&& n.role == d_engine_proto::common::NodeRole::Learner as i32
});
if is_learner {
Some((peer_id, Some(match_idx)))
} else {
None
}
})
.collect();
if !learner_progress.is_empty()
&& let Err(e) = self.check_learner_progress(&learner_progress, ctx, role_tx).await
{
error!(?e, "check_learner_progress failed");
}
} else {
trace!(
"[APPEND-RESULT] Node {} is a voter, skipping learner check",
follower_id
);
}
// Re-calculate commit index after updating this voter's match_index.
if peer_update.success && is_voter {
if let Some(new_commit) = self.calculate_new_commit_index(ctx.raft_log()) {
self.update_commit_index_with_signal(
Leader as i32,
self.current_term(),
new_commit,
role_tx,
)?;
self.drain_pending_client_writes(new_commit);
self.drain_commit_actions(new_commit, ctx, role_tx).await;
}
// Lease refresh and pending_lease_reads drain are triggered by quorum ACK,
// independent of whether commit_index advanced. When an expired lease fires an
// empty AppendEntries heartbeat, commit_index does not change (nothing new to
// commit), so calculate_new_commit_index returns None and the block above is
// skipped. We must check quorum confirmation separately here.
let quorum_confirmed = ctx
.raft_log()
.calculate_majority_matched_index(
self.current_term(),
self.commit_index(),
self.match_index
.iter()
.filter(|(id, _)| {
self.cluster_metadata.replication_targets.iter().any(|n| {
n.id == **id
&& n.role != d_engine_proto::common::NodeRole::Learner as i32
})
})
.map(|(_, idx)| *idx)
.collect(),
)
.is_some();
if quorum_confirmed {
// Refresh after all post-commit work to eliminate the yield-point race window
// introduced when drain_commit_actions became async.
self.update_lease_timestamp();
self.drain_pending_lease_reads(ctx);
// Path A drain (Bug #381 fix): serve linearizable reads that have been
// waiting for quorum confirmation. Pure-read batches never advance
// commit_index, so handle_apply_completed (Path B) would never fire for
// them. Drain here once leadership is confirmed and SM is ready.
let last_applied = ctx.state_machine().last_applied().index;
let to_serve: Vec<u64> =
self.pending_reads.range(..=last_applied).map(|(k, _)| *k).collect();
for idx in to_serve {
if let Some(batch) = self.pending_reads.remove(&idx) {
self.execute_pending_reads(batch.requests, ctx);
}
}
}
}
Ok(())
}
}
/// Computes the exponential backoff delay for a snapshot push failure.
///
/// Doubles `base_delay_ms` on each consecutive failure, capped at
/// `push_backoff_max_delay_ms`. Saturating arithmetic prevents overflow for
/// large failure counts.
///
/// Leader protection is the highest priority: the cap ensures the leader never
/// stalls healthy-follower replication waiting for an unreachable peer.
fn snapshot_push_backoff_duration(
failure_count: u32,
policy: &crate::InstallSnapshotBackoffPolicy,
) -> std::time::Duration {
let millis = policy
.base_delay_ms
.saturating_mul(1u64 << failure_count.min(20))
.min(policy.push_backoff_max_delay_ms);
std::time::Duration::from_millis(millis)
}
impl<T: TypeConfig> LeaderState<T> {
// ---- Per-follower ReplicationWorker management ----------------------------------------
/// Spawn a long-running replication worker task for the given peer.
///
/// The worker runs a FIFO loop: receives AppendEntriesRequests from `task_tx`,
/// calls `transport.send_append_request`, and sends results back via `role_event_tx`.
/// It exits cleanly when `task_tx` is dropped (LeaderState step-down).
fn spawn_worker(
peer_id: u32,
cfg: ReplicationWorkerConfig<T>,
) -> ReplicationWorkerHandle {
let (task_tx, task_rx) = mpsc::unbounded_channel::<ReplicationTask>();
tokio::spawn(async move {
Self::run_replication_worker(peer_id, task_rx, cfg).await;
});
ReplicationWorkerHandle {
task_tx,
snapshot_failure_count: 0,
snapshot_next_retry_at: None,
}
}
/// Worker loop: pipeline replication for one peer.
///
/// Handles two task types:
/// - `Append`: send AppendEntries RPC (skipped while snapshot is in progress)
/// - `Snapshot`: push latest snapshot to a lagging peer.
///
/// Snapshot dedup: `snapshot_in_progress` is set when a snapshot is spawned and
/// cleared when it completes. While true, duplicate `Snapshot` tasks and stale
/// `Append` tasks (whose prev_log indices are meaningless before the snapshot
/// base is installed) are discarded.
async fn run_replication_worker(
peer_id: u32,
mut task_rx: mpsc::UnboundedReceiver<ReplicationTask>,
cfg: ReplicationWorkerConfig<T>,
) {
let ReplicationWorkerConfig {
transport,
membership,
retry_policies,
response_compress_enabled,
role_event_tx,
state_machine_handler,
snapshot_config,
} = cfg;
// NOTE: snapshot_in_progress is intentionally outside the reconnect loop.
// A snapshot transfer must complete (or fail) regardless of stream reconnects;
// the bidi stream is only used for AppendEntries, not snapshot chunks.
let snapshot_in_progress = Arc::new(AtomicBool::new(false));
let base_delay_ms = retry_policies.append_entries.base_delay_ms;
let max_delay_ms = retry_policies.append_entries.max_delay_ms;
// Open persistent bidi stream with backoff reconnection
loop {
let mut backoff_ms = base_delay_ms;
let stream = loop {
if task_rx.is_closed() {
debug!(peer_id, "Replication worker exiting: task channel closed");
return;
}
match transport
.open_replication_stream(peer_id, membership.clone(), response_compress_enabled)
.await
{
Ok(s) => {
info!(peer_id, "Bidi replication stream established");
break s;
}
Err(e) => {
info!(
peer_id,
"Bidi stream reconnecting (backoff={}ms): {:?}", backoff_ms, e
);
tokio::time::sleep(Duration::from_millis(backoff_ms)).await;
backoff_ms = (backoff_ms * 2).min(max_delay_ms);
}
}
};
let stream_sender = stream.sender;
let mut stream_receiver = stream.receiver;
// Shared flag: both recv task and main loop can detect stream breakage
let stream_broken = Arc::new(AtomicBool::new(false));
let recv_broken = stream_broken.clone();
// Spawn recv task to monitor ACKs and detect stream disconnection
let recv_role_event_tx = role_event_tx.clone();
let recv_handle = tokio::spawn(async move {
use futures::StreamExt;
while let Some(result) = stream_receiver.next().await {
match result {
Ok(response) => {
let _ = recv_role_event_tx.send(RoleEvent::AppendResult {
follower_id: peer_id,
result: Ok(response),
});
}
Err(status) => {
warn!(peer_id, "Bidi stream recv error: {:?}", status);
recv_broken.store(true, Ordering::Release);
let _ = recv_role_event_tx.send(RoleEvent::PeerStreamError { peer_id });
break;
}
}
}
debug!(peer_id, "Recv task exiting (stream closed)");
});
// Main worker loop: process replication tasks
while let Some(task) = task_rx.recv().await {
// Check stream health before processing task
if stream_broken.load(Ordering::Acquire) {
debug!(peer_id, "Stream broken detected, reconnecting...");
break;
}
match task {
ReplicationTask::Append(request) => {
if snapshot_in_progress.load(Ordering::Acquire) {
debug!(
peer_id,
"Skipping AppendEntries while snapshot is in progress"
);
continue;
}
// Push batch directly into the persistent bidi stream (non-blocking)
if stream_sender.send(request).await.is_err() {
warn!(peer_id, "Bidi stream sender closed, reconnecting");
stream_broken.store(true, Ordering::Release);
let _ = role_event_tx.send(RoleEvent::PeerStreamError { peer_id });
break;
}
}
ReplicationTask::Snapshot(metadata) => {
if snapshot_in_progress.load(Ordering::Acquire) {
debug!(
peer_id,
"Skipping duplicate Snapshot task (already in progress)"
);
continue;
}
snapshot_in_progress.store(true, Ordering::Release);
// Spawn snapshot transfer; bidi stream stays open
let flag = snapshot_in_progress.clone();
let t = transport.clone();
let smh = state_machine_handler.clone();
let m = membership.clone();
let c = snapshot_config.clone();
let tx = role_event_tx.clone();
tokio::spawn(async move {
let result = t.send_snapshot(peer_id, metadata, smh, m, c).await;
let success = result.is_ok();
if !success {
warn!(peer_id, "Snapshot push failed: {:?}", result);
}
flag.store(false, Ordering::Release);
let _ = tx.send(RoleEvent::SnapshotPushCompleted { peer_id, success });
});
}
}
}
// task_rx closed (leader stepped down) or stream broken
recv_handle.abort();
if task_rx.is_closed() {
debug!(peer_id, "Replication worker exiting (leader stepped down)");
break;
}
// Otherwise reconnect (flag persists across reconnects)
}
}
/// Send a task to the given peer's worker, spawning it first if needed.
/// If the existing worker has died (task_tx.send returns Err), rebuilds it transparently.
fn send_to_worker_or_spawn(
&mut self,
peer_id: u32,
mut task: ReplicationTask,
cfg: ReplicationWorkerConfig<T>,
) {
// Try existing worker; if dead, recover task and fall through to respawn.
if let Some(handle) = self.replication_workers.get(&peer_id) {
match handle.task_tx.send(task) {
Ok(()) => return,
Err(mpsc::error::SendError(t)) => {
warn!("Replication worker for peer {} died, rebuilding", peer_id);
task = t;
}
}
}
// Worker absent or just died — spawn and send.
let handle = Self::spawn_worker(peer_id, cfg);
let _ = handle.task_tx.send(task);
self.replication_workers.insert(peer_id, handle);
}
// ---- Snapshot push backoff + alerting -------------------------------------------------
/// Update per-peer snapshot push state after a completed (success or failure) attempt.
///
/// On success: resets the failure counter and clears any active backoff window.
/// On failure: increments the counter, schedules an exponential backoff window, and
/// emits an error-level alert once `push_failure_alert_threshold` is reached.
///
/// Leader protection is the highest priority: the backoff window prevents a permanently
/// unreachable peer from consuming the leader's I/O bandwidth on every heartbeat cycle.
pub(crate) fn handle_snapshot_push_completed(
&mut self,
peer_id: u32,
success: bool,
policy: &crate::InstallSnapshotBackoffPolicy,
node_id: u32,
) {
let Some(handle) = self.replication_workers.get_mut(&peer_id) else {
return;
};
if success {
handle.snapshot_failure_count = 0;
handle.snapshot_next_retry_at = None;
return;
}
handle.snapshot_failure_count += 1;
let count = handle.snapshot_failure_count;
let delay = snapshot_push_backoff_duration(count, policy);
handle.snapshot_next_retry_at = Some(Instant::now() + delay);
if count >= policy.push_failure_alert_threshold {
error!(
peer_id,
failure_count = count,
next_retry_secs = delay.as_secs(),
"Snapshot push to peer has failed {} consecutive times; \
peer may be unreachable or out of disk space. \
Next attempt in {}s.",
count,
delay.as_secs()
);
metrics::counter!(
"raft.snapshot.push_consecutive_failures",
"peer_id" => peer_id.to_string(),
"node_id" => node_id.to_string(),
)
.increment(1);
}
}
// ---- Pending client write drain -------------------------------------------------------
/// Drain pending client write batches whose end_log_index <= new_commit.
///
/// For `wait_for_apply=false` batches: send write_success immediately.
/// For `wait_for_apply=true` batches (e.g. CAS): move senders to `pending_write_apply`
/// so they are resolved by the ApplyCompleted handler.
fn drain_pending_client_writes(
&mut self,
new_commit: u64,
) {
// split_off returns the sub-map with keys > new_commit; we keep that for later.
let remaining = if new_commit < u64::MAX {
self.pending_client_writes.split_off(&(new_commit + 1))
} else {
BTreeMap::new()
};
let committed = std::mem::replace(&mut self.pending_client_writes, remaining);
for (_, meta) in committed {
let (start_idx, senders, wait_for_apply) =
(meta.start_idx, meta.senders, meta.wait_for_apply);
if wait_for_apply {
for (i, sender) in senders.into_iter().enumerate() {
let idx = start_idx + i as u64;
if let Some(t) = self.write_propose_times.get(&idx) {
let ms = t.elapsed().as_millis();
metrics::histogram!("raft.write.propose_to_commit_ms").record(ms as f64);
}
self.pending_write_apply.insert(idx, sender);
}
} else {
for sender in senders {
let _ = sender.send(Ok(ClientResponse::write_success()));
}
}
}
}
/// Fire-and-forget noop entry to confirm quorum after election.
///
/// Writes a noop `EntryPayload::noop()` to the local log (no sender → no inline wait),
/// then records the expected commit index in `pending_commit_actions` keyed by the
/// noop log index. When that index commits, `drain_commit_actions` fires
/// `RoleEvent::NoopCommitted { term }` which the Raft loop handles by calling
/// `on_noop_committed()` and notifying watch listeners.
///
/// Returns `Err` only on storage failure (write to raft log). All other paths are
/// handled asynchronously via the post-commit action queue.
pub(super) async fn initiate_noop_commit(
&mut self,
ctx: &RaftContext<T>,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
) -> Result<()> {
let term = self.current_term();
let deadline = Instant::now()
+ Duration::from_millis(
ctx.node_config.raft.membership.verify_leadership_persistent_timeout.as_millis()
as u64,
);
// Predict the noop log index BEFORE writing: last_entry_id + 1.
// Must insert BEFORE execute_request_immediately so that Phase 4's
// synchronous drain_commit_actions call (single-voter path) can find the entry.
// No concurrent writes: Raft loop is single-threaded.
let noop_index = ctx.raft_log().last_entry_id() + 1;
self.pending_commit_actions.insert(
noop_index,
PostCommitEntry {
deadline,
action: PostCommitAction::LeaderNoop { term },
},
);
// Write noop with no sender — purely for commit confirmation.
if let Err(e) = self
.execute_request_immediately(
RaftRequestWithSignal {
id: { rand::distr::Alphanumeric.sample_string(&mut rand::rng(), 21) },
payloads: vec![EntryPayload::noop()],
senders: vec![],
wait_for_apply_event: false,
},
ctx,
role_tx,
)
.await
{
// Storage failure: clean up the pre-inserted entry and propagate.
self.pending_commit_actions.remove(&noop_index);
return Err(e);
}
debug!(
"initiate_noop_commit: noop_index={} term={}",
noop_index, term
);
Ok(())
}
/// Track no-op entry index for linearizable read optimization.
/// Called directly by drain_commit_actions when the noop entry commits.
fn on_noop_committed(
&mut self,
ctx: &RaftContext<T>,
) -> Result<()> {
let noop_index = ctx.raft_log().last_entry_id();
self.noop_log_id = Some(noop_index);
debug!("Tracked noop_log_id: {}", noop_index);
Ok(())
}
/// Drain and fire Raft protocol internal actions whose log entries have committed.
///
/// **Purpose**: Respond to protocol-level operations (noop, join) when their entries
/// reach majority replication. These actions DO NOT wait for state machine apply,
/// because they represent Raft metadata, not user data.
///
/// **Call sites**:
/// 1. `handle_log_flushed`: Single-voter or multi-voter durability path
/// 2. `handle_append_result`: Multi-voter quorum path (when peer ACK satisfies majority)
///
/// Both call sites are necessary due to commit timing uncertainty:
/// - Multi-voter: Quorum may arrive before or after leader's local flush
/// - Single-voter: Only LogFlushed triggers commit (no peers)
///
/// **Idempotency**: `split_off` pattern ensures each action fires exactly once,
/// even if commit_index advances multiple times to the same value.
///
/// # Example
/// ```ignore
/// // T1: Insert action when noop appended
/// pending_commit_actions.insert(5, PostCommitEntry::LeaderNoop{term: 2});
///
/// // T2: Commit advances to 5
/// drain_commit_actions(5, role_tx);
/// // → Sends RoleEvent::NoopCommitted{term: 2}
/// // → pending_commit_actions now empty (or has entries > 5)
///
/// // T3: Commit stays at 5 (redundant call)
/// drain_commit_actions(5, role_tx);
/// // → No action fired (already drained)
/// ```
///
/// Uses the same `split_off` pattern as `drain_pending_client_writes`:
/// entries with index ≤ new_commit are drained; entries with index > new_commit
/// remain in the map for future commits.
pub(super) async fn drain_commit_actions(
&mut self,
new_commit: u64,
ctx: &RaftContext<T>,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
) {
let remaining = if new_commit < u64::MAX {
self.pending_commit_actions.split_off(&(new_commit + 1))
} else {
BTreeMap::new()
};
let committed = std::mem::replace(&mut self.pending_commit_actions, remaining);
for (_, entry) in committed {
match entry.action {
PostCommitAction::LeaderNoop { term } => {
// Call on_noop_committed directly — establishes noop_log_id without
// waiting for a second loop iteration through role_rx (P2).
// Only send the event for notify_leader_change, which lives on Raft<T>.
if let Err(e) = self.on_noop_committed(ctx) {
warn!(
?e,
"on_noop_committed failed — skipping notify_leader_change"
);
} else {
let _ = role_tx.send(RoleEvent::NoopCommitted { term });
}
}
PostCommitAction::NodeJoin {
node_id,
addr,
sender,
} => {
// Call send_join_success directly — no channel roundtrip needed.
// drain_commit_actions is async, so we can await the membership queries inline.
if let Err(e) = self.send_join_success(node_id, &addr, sender, ctx).await {
error!(
?e,
node_id, "send_join_success failed after NodeJoin commit"
);
}
}
}
}
}
/// Drain all pending client writes with an error response.
/// Called on step-down or HigherTerm detection.
fn drain_pending_writes_with_error(
&mut self,
error_code: ErrorCode,
) {
self.write_propose_times.clear();
for (_, meta) in std::mem::take(&mut self.pending_client_writes) {
for sender in meta.senders {
let _ = sender.send(Ok(ClientResponse::client_error(error_code)));
}
}
}
// ---- Cluster metadata management -------------------------------------------------------
/// Initialize cluster metadata after becoming leader.
/// Update cluster metadata when membership changes.
pub async fn update_cluster_metadata(
&mut self,
membership: &Arc<T::M>,
) -> Result<()> {
// Calculate total voter count including self as leader
let voters = membership.voters().await;
let total_voters = voters.len() + 1; // +1 for leader (self)
// Get all replication targets (voters + learners, excluding self)
let replication_targets = membership.replication_peers().await;
// Single-voter cluster: only this node is a voter (quorum = 1)
let single_voter = total_voters == 1;
self.cluster_metadata = ClusterMetadata {
single_voter,
total_voters,
replication_targets: replication_targets.clone(),
};
debug!(
"Updated cluster metadata: single_voter={}, total_voters={}, replication_targets={}",
single_voter,
total_voters,
replication_targets.len()
);
Ok(())
}
/// The fun will retrieve current state snapshot
pub fn state_snapshot(&self) -> StateSnapshot {
StateSnapshot {
current_term: self.current_term(),
voted_for: None,
commit_index: self.commit_index(),
role: Leader as i32,
}
}
/// The fun will retrieve current Leader state snapshot
#[tracing::instrument]
pub fn leader_state_snapshot(&self) -> LeaderStateSnapshot {
LeaderStateSnapshot {
next_index: self.next_index.clone(),
match_index: self.match_index.clone(),
noop_log_id: self.noop_log_id,
}
}
/// # Params
/// Execute a Raft request immediately, bypassing the normal batching mechanism.
///
/// This is used for quorum verification requests that must be sent immediately
/// without waiting for batching (e.g., config changes, leadership verification).
pub async fn execute_request_immediately(
&mut self,
raft_request_with_signal: RaftRequestWithSignal,
ctx: &RaftContext<T>,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
) -> Result<()> {
debug!(
"Leader::execute_request_immediately, request_id: {}",
raft_request_with_signal.id
);
// Always bypass buffer for immediate execution (quorum verification)
let batch = VecDeque::from([raft_request_with_signal]);
self.process_batch(batch, role_tx, ctx).await?;
Ok(())
}
/// Scenario handling summary:
///
/// 1. Quorum achieved:
/// - Client response: `write_success()`
/// - State update: update peer indexes and commit index
/// - Return: `Ok(())`
///
/// 2. Quorum NOT achieved (verifiable):
/// - Client response: `RetryRequired`
/// - State update: update peer indexes
/// - Return: `Ok(())`
///
/// 3. Quorum NOT achieved (non-verifiable):
/// - Client response: `ProposeFailed`
/// - State update: update peer indexes
/// - Return: `Ok(())`
///
/// 4. Partial timeouts:
/// - Client response: `ProposeFailed`
/// - State update: update only the peer indexes that responded
/// - Return: `Ok(())`
///
/// 5. All timeouts:
/// - Client response: `ProposeFailed`
/// - State update: no update
/// - Return: `Ok(())`
///
/// 6. Higher term detected:
/// - Client response: `TermOutdated`
/// - State update: update term and convert to follower
/// - Return: `Err(HigherTerm)`
///
/// 7. Critical failure (e.g., system or logic error):
/// - Client response: `ProposeFailed`
/// - State update: none
/// - Return: original error
pub async fn process_batch(
&mut self,
batch: impl IntoIterator<Item = RaftRequestWithSignal>,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
ctx: &RaftContext<T>,
) -> Result<()> {
self.timer.reset_replication();
let start_index = ctx.raft_log().last_entry_id() + 1;
let (payloads, write_metadata) = Self::merge_batch_to_write_metadata(batch, start_index);
if !payloads.is_empty() {
trace!(?payloads, "[Node-{}] process_batch", ctx.node_id);
}
self.execute_and_process_raft_rpc(payloads, write_metadata, None, ctx, role_tx)
.await
}
/// Heartbeat dispatch: piggyback a write batch if available, otherwise send
/// an empty AppendEntries to maintain leadership. Always resets the replication
/// timer — the intent is explicit here rather than hidden inside `process_batch`.
async fn send_heartbeat_or_batch(
&mut self,
request: Option<RaftRequestWithSignal>,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
ctx: &RaftContext<T>,
) -> Result<()> {
self.timer.reset_replication();
match request {
Some(req) => {
// Has pending writes — process as a real batch.
let start_index = ctx.raft_log().last_entry_id() + 1;
let (payloads, write_metadata) =
Self::merge_batch_to_write_metadata(std::iter::once(req), start_index);
self.execute_and_process_raft_rpc(payloads, write_metadata, None, ctx, role_tx)
.await
}
None => {
// No pending writes — send empty AppendEntries as heartbeat.
self.execute_and_process_raft_rpc(vec![], None, None, ctx, role_tx).await
}
}
}
/// Update peer node index
#[instrument(skip(self))]
fn update_peer_indexes(
&mut self,
peer_updates: &HashMap<u32, PeerUpdate>,
) {
for (peer_id, update) in peer_updates {
if let Err(e) = self.update_next_index(*peer_id, update.next_index) {
error!("Failed to update next index: {:?}", e);
}
trace!(
"Updated next index for peer {}-{}",
peer_id, update.next_index
);
if let Some(match_index) = update.match_index {
if let Err(e) = self.update_match_index(*peer_id, match_index) {
error!("Failed to update match index: {:?}", e);
}
trace!("Updated match index for peer {}-{}", peer_id, match_index);
}
}
}
pub async fn check_learner_progress(
&mut self,
learner_progress: &HashMap<u32, Option<u64>>,
ctx: &RaftContext<T>,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
) -> Result<()> {
debug!(?learner_progress, "check_learner_progress");
trace!(
"[CHECK-LEARNER-PROGRESS] Leader {} checking progress for learners: {:?}",
self.node_id(),
learner_progress
);
if !self.should_check_learner_progress(ctx) {
return Ok(());
}
if learner_progress.is_empty() {
return Ok(());
}
let _tlp = std::time::Instant::now();
let ready_learners = self.find_promotable_learners(learner_progress, ctx).await;
trace!(
"[FIND-PROMOTABLE] Leader {} found {} promotable learners: {:?} (took {:?})",
self.node_id(),
ready_learners.len(),
ready_learners,
_tlp.elapsed()
);
let new_promotions = self.deduplicate_promotions(ready_learners);
trace!(
"[DEDUPLICATE] Leader {} has {} new promotions after dedup: {:?}",
self.node_id(),
new_promotions.len(),
new_promotions
);
if !new_promotions.is_empty() {
trace!(
"[ENQUEUE-PROMOTIONS] Leader {} enqueueing {} promotions",
self.node_id(),
new_promotions.len()
);
self.enqueue_and_notify_promotions(new_promotions, role_tx)?;
} else {
trace!(
"[NO-PROMOTIONS] Leader {} has no new promotions to enqueue",
self.node_id()
);
}
Ok(())
}
/// Check if enough time has elapsed since last learner progress check
fn should_check_learner_progress(
&mut self,
ctx: &RaftContext<T>,
) -> bool {
let throttle_interval =
Duration::from_millis(ctx.node_config().raft.learner_check_throttle_ms);
if self.last_learner_check.elapsed() < throttle_interval {
return false;
}
self.last_learner_check = Instant::now();
true
}
/// Find learners that are caught up and eligible for promotion
async fn find_promotable_learners(
&self,
learner_progress: &HashMap<u32, Option<u64>>,
ctx: &RaftContext<T>,
) -> Vec<u32> {
let leader_commit = self.commit_index();
let threshold = ctx.node_config().raft.learner_catchup_threshold;
let membership = ctx.membership();
trace!(
"[FIND-PROMOTABLE-START] Leader {} checking {} learners, leader_commit={}, threshold={}",
self.node_id(),
learner_progress.len(),
leader_commit,
threshold
);
let mut ready_learners = Vec::new();
for (&node_id, &match_index_opt) in learner_progress.iter() {
trace!(
"[FIND-PROMOTABLE-LOOP] Checking learner {}, match_index={:?}",
node_id, match_index_opt
);
if !membership.contains_node(node_id).await {
trace!(
"[FIND-PROMOTABLE-LOOP] ❌ Learner {} NOT in membership, skipping",
node_id
);
continue;
}
trace!(
"[FIND-PROMOTABLE-LOOP] ✅ Learner {} is in membership",
node_id
);
let match_index = match_index_opt.unwrap_or(0);
let gap = leader_commit.saturating_sub(match_index);
let is_caught_up = self.is_learner_caught_up(match_index_opt, leader_commit, threshold);
trace!(
"[FIND-PROMOTABLE-LOOP] Learner {} catchup check: match_index={}, leader_commit={}, gap={}, threshold={}, caught_up={}",
node_id, match_index, leader_commit, gap, threshold, is_caught_up
);
if !is_caught_up {
trace!(
"[FIND-PROMOTABLE-LOOP] ❌ Learner {} NOT caught up (gap {} > threshold {})",
node_id, gap, threshold
);
continue;
}
trace!("[FIND-PROMOTABLE-LOOP] ✅ Learner {} IS caught up", node_id);
let node_status =
membership.get_node_status(node_id).await.unwrap_or(NodeStatus::ReadOnly);
if !node_status.is_promotable() {
debug!(
?node_id,
?node_status,
"Learner caught up but status is not Promotable, skipping"
);
continue;
}
debug!(
?node_id,
match_index = ?match_index_opt.unwrap_or(0),
?leader_commit,
gap = leader_commit.saturating_sub(match_index_opt.unwrap_or(0)),
"Learner caught up"
);
ready_learners.push(node_id);
}
ready_learners
}
/// Check if learner has caught up with leader based on log gap
fn is_learner_caught_up(
&self,
match_index: Option<u64>,
leader_commit: u64,
threshold: u64,
) -> bool {
let match_index = match_index.unwrap_or(0);
let gap = leader_commit.saturating_sub(match_index);
gap <= threshold
}
/// Remove learners already in pending promotions queue
fn deduplicate_promotions(
&self,
ready_learners: Vec<u32>,
) -> Vec<u32> {
let already_pending: std::collections::HashSet<_> =
self.pending_promotions.iter().map(|p| p.node_id).collect();
ready_learners.into_iter().filter(|id| !already_pending.contains(id)).collect()
}
/// Add promotions to queue and send notification event
fn enqueue_and_notify_promotions(
&mut self,
new_promotions: Vec<u32>,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
) -> Result<()> {
info!(
?new_promotions,
"Learners caught up, adding to pending promotions"
);
let threshold = self.node_config.raft.membership.promotion.stale_learner_threshold;
for node_id in new_promotions {
self.pending_promotions
.push_back(PendingPromotion::new(node_id, Instant::now()));
}
// Only update deadline if queue was previously empty (new front is the oldest)
if self.stale_check_deadline.is_none() {
self.refresh_stale_deadline(threshold);
}
role_tx
.send(RoleEvent::ReprocessEvent(Box::new(
RaftEvent::PromoteReadyLearners,
)))
.map_err(|e| {
let error_str = format!("{e:?}");
error!("Failed to send PromoteReadyLearners: {}", error_str);
Error::System(SystemError::Network(NetworkError::SingalSendFailed(
error_str,
)))
})?;
Ok(())
}
/// Calculate new submission index
#[instrument(skip(self))]
fn calculate_new_commit_index(
&self,
raft_log: &Arc<ROF<T>>,
) -> Option<u64> {
let old_commit_index = self.commit_index();
let current_term = self.current_term();
let replication_targets = &self.cluster_metadata.replication_targets;
let learner_role = d_engine_proto::common::NodeRole::Learner as i32;
// Only voter peers (non-Learner) contribute to the commit quorum.
let matched_ids: Vec<u64> = self
.match_index
.iter()
.filter(|(id, _)| {
replication_targets.iter().any(|n| n.id == **id && n.role != learner_role)
})
.map(|(_, idx)| *idx)
.collect();
let new_commit_index =
raft_log.calculate_majority_matched_index(current_term, old_commit_index, matched_ids);
if new_commit_index.is_some() && new_commit_index.unwrap() > old_commit_index {
new_commit_index
} else {
None
}
}
/// Calculate safe read index for linearizable reads.
///
/// Returns max(commitIndex, noopIndex) to ensure:
/// 1. All committed data is visible
/// 2. Current term's no-op entry is accounted for
/// 3. Read index is fixed at request arrival time (avoids waiting for concurrent writes)
///
/// This optimization prevents reads from waiting for writes that arrived
/// after the read request started processing.
#[doc(hidden)]
pub fn calculate_read_index(&self) -> u64 {
let commit_index = self.commit_index();
let noop_index = self.noop_log_id.unwrap_or(0);
std::cmp::max(commit_index, noop_index)
}
/// Wait for state machine to apply up to target index.
///
/// This is used by linearizable reads to ensure they see all committed data
/// up to the read_index calculated at request arrival time.
#[doc(hidden)]
pub async fn wait_until_applied(
&self,
target_index: u64,
state_machine_handler: &Arc<SMHOF<T>>,
last_applied: u64,
) -> Result<()> {
if last_applied < target_index {
state_machine_handler.update_pending(target_index);
let timeout_ms = self.node_config.raft.read_consistency.state_machine_sync_timeout_ms;
state_machine_handler
.wait_applied(target_index, std::time::Duration::from_millis(timeout_ms))
.await?;
debug!("wait_until_applied: target_index={} success", target_index);
}
Ok(())
}
#[instrument(skip(self))]
fn scheduled_purge_upto(
&mut self,
received_last_included: LogId,
) {
if let Some(existing) = self.scheduled_purge_upto
&& existing.index >= received_last_included.index
{
warn!(
?received_last_included,
?existing,
"Will not update scheduled_purge_upto, received invalid last_included log"
);
return;
}
info!(?self.scheduled_purge_upto, ?received_last_included, "Updte scheduled_purge_upto.");
self.scheduled_purge_upto = Some(received_last_included);
}
fn send_become_follower_event(
&self,
new_leader_id: Option<u32>,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
) -> Result<()> {
info!(
?new_leader_id,
"Leader is going to step down as Follower..."
);
role_tx.send(RoleEvent::BecomeFollower(new_leader_id)).map_err(|e| {
let error_str = format!("{e:?}");
error!("Failed to send: {}", error_str);
NetworkError::SingalSendFailed(error_str)
})?;
Ok(())
}
/// Determines if logs prior to `last_included_in_snapshot` can be permanently discarded.
///
/// Implements Leader-side log compaction safety checks per Raft paper §7.2:
/// > "The leader uses a new RPC called InstallSnapshot to send snapshots to followers that are
/// > too far behind"
///
/// # Safety Invariants (ALL must hold)
///
/// 1. **Committed Entry Guarantee** `last_included_in_snapshot.index < self.commit_index`
/// - Ensures we never discard uncommitted entries (Raft §5.4.2)
/// - Maintains at least one committed entry after purge for log matching property
///
/// 2. **Monotonic Snapshot Advancement** `last_purge_index < last_included_in_snapshot.index`
/// - Enforces snapshot indices strictly increase (prevents rollback attacks)
/// - Maintains sequential purge ordering (FSM safety requirement)
///
/// 3. **Operation Atomicity** `pending_purge.is_none()`
/// - Ensures only one concurrent purge operation
/// - Critical for linearizable state machine semantics
///
/// # Implementation Notes
/// - Per Raft §7: "Each server compacts its log independently"
/// - Leader purges immediately after snapshot without waiting for followers
/// - Lagging followers recover via InstallSnapshot RPC
/// - Actual log discard should be deferred until storage confirms snapshot persistence
#[instrument(skip(self))]
pub fn can_purge_logs(
&self,
last_purge_index: Option<LogId>,
last_included_in_snapshot: LogId,
) -> bool {
let commit_index = self.commit_index();
debug!(
?commit_index,
?last_purge_index,
?last_included_in_snapshot,
"can_purge_logs"
);
let monotonic_check = last_purge_index
.map(|lid| lid.index < last_included_in_snapshot.index)
.unwrap_or(true);
// Per Raft §7: Leader purges independently after snapshot
// No peer coordination required - lagging followers get InstallSnapshot
last_included_in_snapshot.index < commit_index && monotonic_check
}
pub async fn handle_join_cluster(
&mut self,
join_request: JoinRequest,
sender: MaybeCloneOneshotSender<std::result::Result<JoinResponse, Status>>,
ctx: &RaftContext<T>,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
) -> Result<()> {
let node_id = join_request.node_id;
let node_role = join_request.node_role;
let address = join_request.address;
let status = join_request.status;
let membership = ctx.membership();
// 1. Validate join request
debug!("1. Validate join request");
if membership.contains_node(node_id).await {
let error_msg = format!("Node {node_id} already exists in cluster");
warn!(%error_msg);
return self.send_join_error(sender, MembershipError::NodeAlreadyExists(node_id)).await;
}
// 2. Create configuration change payload
debug!("2. Create configuration change payload");
if let Err(e) = membership.can_rejoin(node_id, node_role).await {
let error_msg = format!("Node {node_id} cannot rejoin: {e}",);
warn!(%error_msg);
return self
.send_join_error(sender, MembershipError::JoinClusterError(error_msg))
.await;
}
let config_change = Change::AddNode(AddNode {
node_id,
address: address.clone(),
status,
});
// 3. Submit config change fire-and-forget; JoinCommitted event delivers the response.
debug!("3. Submit config change (fire-and-forget)");
let deadline = Instant::now()
+ Duration::from_millis(
ctx.node_config.raft.membership.verify_leadership_persistent_timeout.as_millis()
as u64,
);
if let Err(e) = self
.execute_request_immediately(
RaftRequestWithSignal {
id: { rand::distr::Alphanumeric.sample_string(&mut rand::rng(), 21) },
payloads: vec![EntryPayload::config(config_change)],
senders: vec![],
wait_for_apply_event: false,
},
ctx,
role_tx,
)
.await
{
let _ = sender.send(Err(Status::failed_precondition(format!(
"Join request submission failed: {e}"
))));
return Err(e);
}
// Record post-commit action: when the config index commits, send JoinResponse.
let join_index = ctx.raft_log().last_entry_id();
self.pending_commit_actions.insert(
join_index,
PostCommitEntry {
deadline,
action: PostCommitAction::NodeJoin {
node_id,
addr: address,
sender,
},
},
);
debug!("JoinCluster: awaiting commit at index {}", join_index);
Ok(())
}
pub(super) async fn send_join_success(
&self,
node_id: u32,
address: &str,
sender: MaybeCloneOneshotSender<std::result::Result<JoinResponse, Status>>,
ctx: &RaftContext<T>,
) -> Result<()> {
// Retrieve latest snapshot metadata, if there is
let snapshot_metadata = ctx.state_machine_handler().get_latest_snapshot_metadata();
// Prepare response
let response = JoinResponse {
success: true,
error: String::new(),
config: Some(
ctx.membership()
.retrieve_cluster_membership_config(self.shared_state().current_leader())
.await,
),
config_version: ctx.membership().get_cluster_conf_version().await,
snapshot_metadata,
leader_id: self.node_id(),
};
sender.send(Ok(response)).map_err(|e| {
error!("Failed to send join response: {:?}", e);
NetworkError::SingalSendFailed(format!("{e:?}"))
})?;
info!(
"Node {} ({}) successfully added as learner",
node_id, address
);
// Print leader accepting new node message (Plan B)
crate::utils::cluster_printer::print_leader_accepting_new_node(
self.node_id(),
node_id,
address,
d_engine_proto::common::NodeRole::Learner as i32,
);
Ok(())
}
async fn send_join_error(
&self,
sender: MaybeCloneOneshotSender<std::result::Result<JoinResponse, Status>>,
error: impl Into<Error>,
) -> Result<()> {
let error = error.into();
let status = Status::failed_precondition(error.to_string());
sender.send(Err(status)).map_err(|e| {
error!("Failed to send join error: {:?}", e);
NetworkError::SingalSendFailed(format!("{e:?}"))
})?;
Err(error)
}
#[cfg(any(test, feature = "__test_support"))]
pub fn new(
node_id: u32,
node_config: Arc<RaftNodeConfig>,
) -> Self {
let ReplicationConfig {
rpc_append_entries_clock_in_ms,
..
} = node_config.raft.replication;
let batch_size = node_config.raft.batching.max_batch_size;
let enable_batch = node_config.raft.metrics.enable_batch;
// Initialize backpressure metrics (None if disabled)
let backpressure_metrics = if node_config.raft.metrics.enable_backpressure {
Some(Arc::new(BackpressureMetrics::new(
node_id,
true,
node_config.raft.metrics.sample_rate,
)))
} else {
None
};
LeaderState {
cluster_metadata: ClusterMetadata {
single_voter: false,
total_voters: 0,
replication_targets: vec![],
},
shared_state: SharedState::new(node_id, None, None),
timer: Box::new(ReplicationTimer::new(rpc_append_entries_clock_in_ms)),
next_index: HashMap::new(),
match_index: HashMap::new(),
noop_log_id: None,
propose_buffer: Box::new(ProposeBatchBuffer::new(batch_size).with_length_gauge(
node_id,
"propose",
enable_batch,
)),
node_config,
scheduled_purge_upto: None,
last_purged_index: None, //TODO
last_learner_check: Instant::now(),
snapshot_in_progress: AtomicBool::new(false),
stale_check_deadline: None,
pending_promotions: VecDeque::new(),
lease_instant: Mutex::new(None),
linearizable_read_buffer: Box::new(BatchBuffer::new(batch_size).with_length_gauge(
node_id,
"linearizable",
enable_batch,
)),
lease_read_queue: VecDeque::new(),
eventual_read_queue: VecDeque::new(),
pending_write_apply: HashMap::new(),
pending_reads: BTreeMap::new(),
pending_lease_reads: VecDeque::new(),
replication_workers: HashMap::new(),
pending_client_writes: BTreeMap::new(),
pending_commit_actions: BTreeMap::new(),
write_propose_times: HashMap::new(),
backpressure_metrics,
_marker: PhantomData,
}
}
pub async fn trigger_background_snapshot(
node_id: u32,
metadata: SnapshotMetadata,
state_machine_handler: Arc<SMHOF<T>>,
membership: Arc<MOF<T>>,
config: SnapshotConfig,
) -> Result<()> {
let (result_tx, result_rx) = oneshot::channel();
// Delegate the actual transfer to a dedicated thread pool
tokio::task::spawn_blocking(move || {
let rt = tokio::runtime::Handle::current();
let result = rt.block_on(async move {
let bulk_channel = membership
.get_peer_channel(node_id, ConnectionType::Bulk)
.await
.ok_or(NetworkError::PeerConnectionNotFound(node_id))?;
let data_stream =
state_machine_handler.load_snapshot_data(metadata.clone()).await?;
BackgroundSnapshotTransfer::<T>::run_push_transfer(
node_id,
data_stream,
bulk_channel,
config,
)
.await
});
// Non-blocking send result
let _ = result_tx.send(result);
});
// Non-blocking check result
tokio::spawn(async move {
match result_rx.await {
Ok(Ok(_)) => info!("Snapshot to {} completed", node_id),
Ok(Err(e)) => error!("Snapshot to {} failed: {:?}", node_id, e),
Err(_) => warn!("Snapshot result channel closed unexpectedly"),
}
});
Ok(())
}
/// Processes all pending promotions while respecting the cluster's odd-node constraint
pub async fn process_pending_promotions(
&mut self,
ctx: &RaftContext<T>,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
) -> Result<()> {
debug!(
"[Leader {}] 🔄 process_pending_promotions called, pending: {:?}",
self.node_id(),
self.pending_promotions.iter().map(|p| p.node_id).collect::<Vec<_>>()
);
// Get promotion configuration from the node config
let config = &ctx.node_config().raft.membership.promotion;
// Step 1: Remove stale entries (older than configured threshold)
let now = Instant::now();
self.pending_promotions.retain(|entry| {
now.saturating_duration_since(entry.ready_since) <= config.stale_learner_threshold
});
// Refresh deadline after potential removals
self.refresh_stale_deadline(config.stale_learner_threshold);
if self.pending_promotions.is_empty() {
debug!(
"[Leader {}] ❌ pending_promotions is empty after stale cleanup",
self.node_id()
);
return Ok(());
}
// Step 2: Get current voter count (including self as leader)
let membership = ctx.membership();
let current_voters = membership.voters().await.len() + 1; // +1 for self (leader)
debug!(
"[Leader {}] 📊 current_voters: {}, pending: {}",
self.node_id(),
current_voters,
self.pending_promotions.len()
);
// Step 3: Calculate the maximum batch size that preserves an odd total
let max_batch_size =
calculate_safe_batch_size(current_voters, self.pending_promotions.len());
debug!(
"[Leader {}] 🎯 max_batch_size: {}",
self.node_id(),
max_batch_size
);
if max_batch_size == 0 {
// Nothing we can safely promote now
debug!(
"[Leader {}] ⚠️ max_batch_size is 0, cannot promote now",
self.node_id()
);
return Ok(());
}
// Step 4: Extract the batch from the queue (FIFO order)
let promotion_entries = self.drain_batch(max_batch_size);
let promotion_node_ids = promotion_entries.iter().map(|e| e.node_id).collect::<Vec<_>>();
// Step 5: Execute batch promotion
if !promotion_node_ids.is_empty() {
// Log the batch promotion
info!(
"Promoting learner batch of {} nodes: {:?} (total voters: {} -> {})",
promotion_node_ids.len(),
promotion_node_ids,
current_voters,
current_voters + promotion_node_ids.len()
);
// Attempt promotion and restore batch on failure
let result = self.safe_batch_promote(promotion_node_ids.clone(), ctx, role_tx).await;
if let Err(e) = result {
// Restore entries to the front of the queue in reverse order
for entry in promotion_entries.into_iter().rev() {
self.pending_promotions.push_front(entry);
}
// Refresh deadline to account for restored entries at front
let threshold = config.stale_learner_threshold;
self.refresh_stale_deadline(threshold);
return Err(e);
}
// Refresh stale deadline to reflect the new queue front after successful drain
let threshold = config.stale_learner_threshold;
self.refresh_stale_deadline(threshold);
info!(
"Promotion successful. Cluster members: {:?}",
membership.voters().await
);
}
trace!(
?self.pending_promotions,
"Step 6: Reschedule if any pending promotions remain"
);
// Step 6: Reschedule if any pending promotions remain
if !self.pending_promotions.is_empty() {
debug!(
"[Leader {}] 🔁 Re-sending PromoteReadyLearners for remaining pending: {:?}",
self.node_id(),
self.pending_promotions.iter().map(|p| p.node_id).collect::<Vec<_>>()
);
// Important: Re-send the event to trigger next cycle
role_tx
.send(RoleEvent::ReprocessEvent(Box::new(
RaftEvent::PromoteReadyLearners,
)))
.map_err(|e| {
let error_str = format!("{e:?}");
error!("Send PromoteReadyLearners event failed: {}", error_str);
NetworkError::SingalSendFailed(error_str)
})?;
}
Ok(())
}
/// Removes the first `count` nodes from the pending queue and returns them
pub(super) fn drain_batch(
&mut self,
count: usize,
) -> Vec<PendingPromotion> {
let mut batch = Vec::with_capacity(count);
for _ in 0..count {
if let Some(entry) = self.pending_promotions.pop_front() {
batch.push(entry);
} else {
break;
}
}
batch
}
/// Merge a batch of requests into unified write metadata for RPC processing.
///
/// OR-combines wait_for_apply flags: if any request needs apply confirmation, all wait.
/// This is safe because in practice all requests in a batch share the same flag
/// (writes always true, noop/config always false — never mixed).
pub(super) fn merge_batch_to_write_metadata(
batch: impl IntoIterator<Item = RaftRequestWithSignal>,
start_idx: u64,
) -> (Vec<EntryPayload>, Option<WriteMetadata>) {
let mut all_payloads = Vec::new();
let mut all_senders = Vec::new();
let mut any_wait_for_apply = false;
for mut req in batch {
all_payloads.extend(std::mem::take(&mut req.payloads));
all_senders.extend(req.senders);
any_wait_for_apply |= req.wait_for_apply_event;
}
if all_payloads.is_empty() && all_senders.is_empty() {
return (all_payloads, None);
}
(
all_payloads,
Some(WriteMetadata {
start_idx,
senders: all_senders,
wait_for_apply: any_wait_for_apply,
deadline: Instant::now(), // placeholder; overwritten in Phase 2
}),
)
}
/// Core RPC execution and unified result processing.
///
/// Shared by `process_batch` (write-only) and `unified_write_and_linear_read` (write+read).
/// Handles all 4 result cases: quorum success, quorum failure, higher term, other errors.
/// Core RPC execution: non-blocking replication via per-follower workers.
///
/// Phase 1 (serial, in Raft loop): write entries to local log + build per-peer requests.
/// Phase 2: store pending client writes indexed by end_log_index.
/// Phase 3: fire requests to per-follower workers (fire-and-forget).
///
/// Commit and client responses are deferred:
/// - Multi-voter: driven by handle_append_result (RoleEvent::AppendResult from workers)
/// - Single-voter: driven by handle_log_flushed (RoleEvent::LogFlushed from batch_processor)
async fn execute_and_process_raft_rpc(
&mut self,
payloads: Vec<EntryPayload>,
write_metadata: Option<WriteMetadata>,
mut read_batch: Option<Vec<LinearizableReadRequest>>,
ctx: &RaftContext<T>,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
) -> Result<()> {
trace!(
cluster_size = self.cluster_metadata.total_voters,
payload_count = payloads.len(),
);
// Phase 1: write entries to local log + prepare per-peer requests (serial in Raft loop).
let requests = match ctx
.replication_handler()
.prepare_batch_requests(
payloads,
self.state_snapshot(),
self.leader_state_snapshot(),
&self.cluster_metadata,
ctx,
)
.await
{
Ok(r) => r,
Err(e) => {
error!("prepare_batch_requests failed: {:?}", e);
if let Some(meta) = write_metadata {
for sender in meta.senders {
let _ =
sender.send(Ok(ClientResponse::client_error(ErrorCode::ProposeFailed)));
}
}
if let Some(read_batch) = read_batch {
let status = tonic::Status::failed_precondition(format!("Prepare failed: {e}"));
for (_, sender) in read_batch {
let _ = sender.send(Err(status.clone()));
}
}
return Err(e);
}
};
debug!(
"execute_and_process_raft_rpc: {} append requests, {} snapshot targets",
requests.append_requests.len(),
requests.snapshot_targets.len(),
);
// Phase 2: store pending client writes, keyed by end_log_index.
// Responses are sent when commit_index advances past end_log_index.
if let Some(mut meta) = write_metadata
&& !meta.senders.is_empty()
{
let end_log_index = meta.start_idx + meta.senders.len() as u64 - 1;
let propose_ts = std::time::Instant::now();
meta.deadline = Instant::now()
+ Duration::from_millis(self.node_config.raft.general_raft_timeout_duration_in_ms);
for i in 0..meta.senders.len() as u64 {
self.write_propose_times.insert(meta.start_idx + i, propose_ts);
}
self.pending_client_writes.insert(end_log_index, meta);
}
// Guard: reject LinearizableRead before noop commits (Raft §8 linearizability).
// noop_log_id = None means this leader has not yet confirmed quorum.
// calculate_read_index() would fall back to prev-term commit_index with no quorum check,
// allowing stale reads if the leader is in a minority partition.
// Writes continue normally — they will commit alongside or after noop.
if self.noop_log_id.is_none()
&& let Some(reads) = read_batch.take()
{
let status = tonic::Status::unavailable("LeaderNotReady: noop not committed");
for (_, sender) in reads {
let _ = sender.send(Err(status.clone()));
}
}
// Phase 3: route linearizable reads.
//
// single_voter: self IS the quorum. Serve immediately when SM is ready.
//
// multi_voter, lease valid: is_lease_valid() proves no higher-term leader
// exists (Raft §6.4 — majority ACKed within lease_duration_ms).
// last_applied >= read_index proves SM is current. Serve without RTT.
// Config validation enforces lease_duration < election_timeout, making
// "lease valid" and "new leader elected" mutually exclusive on the timeline.
//
// multi_voter, lease expired: quorum confirmation required (Raft §8 step 2).
// Queue the read; drain via Path A (handle_append_result quorum ACK) or
// Path B (handle_apply_completed when a concurrent write commits). An expired
// lease signals potential minority partition — serving here would violate
// linearizability (Bug #381).
if let Some(read_batch) = read_batch {
let read_index = self.calculate_read_index();
let last_applied = ctx.state_machine().last_applied().index;
if (self.cluster_metadata.single_voter || self.is_lease_valid(ctx))
&& last_applied >= read_index
{
self.execute_pending_reads(read_batch, ctx);
} else {
let deadline = Instant::now()
+ Duration::from_millis(
self.node_config.raft.general_raft_timeout_duration_in_ms,
);
self.pending_reads
.entry(read_index)
.or_insert_with(|| PendingReadBatch {
deadline,
requests: VecDeque::new(),
})
.requests
.extend(read_batch);
}
}
// Phase 4: no peer work at all — nothing to send (single-voter or empty heartbeat).
// Single-voter commit is driven asynchronously by LogFlushed events.
if requests.append_requests.is_empty() && requests.snapshot_targets.is_empty() {
return Ok(());
}
let transport = ctx.transport.clone();
let membership = ctx.membership.clone();
let retry_policies = ctx.node_config.retry.clone();
let response_compress_enabled = ctx.node_config.raft.rpc_compression.replication_response;
let state_machine_handler = ctx.state_machine_handler().clone();
let snapshot_config = ctx.node_config.raft.snapshot.clone();
// Phase 5: fire AppendEntries requests to per-follower workers (non-blocking).
for (peer_id, request) in requests.append_requests {
self.send_to_worker_or_spawn(
peer_id,
ReplicationTask::Append(request),
ReplicationWorkerConfig {
transport: transport.clone(),
membership: membership.clone(),
retry_policies: retry_policies.clone(),
response_compress_enabled,
role_event_tx: role_tx.clone(),
state_machine_handler: state_machine_handler.clone(),
snapshot_config: snapshot_config.clone(),
},
);
}
// Phase 6: trigger snapshot push for peers behind the purge boundary.
// Each peer has a dedicated worker with a `snapshot_in_progress` flag that
// prevents duplicate transfers when multiple heartbeats arrive before completion.
// Peers within an active backoff window (after consecutive failures) are skipped
// to protect the leader from repeatedly pushing to unreachable peers.
if !requests.snapshot_targets.is_empty() {
let snapshot_metadata = ctx.state_machine().snapshot_metadata();
if let Some(metadata) = snapshot_metadata {
for peer_id in requests.snapshot_targets {
// Respect per-peer backoff window: if recent push attempts failed,
// skip until the backoff expires rather than retrying every heartbeat.
if let Some(handle) = self.replication_workers.get(&peer_id)
&& let Some(retry_at) = handle.snapshot_next_retry_at
&& Instant::now() < retry_at
{
debug!(
peer_id,
"Snapshot push backoff active, skipping this heartbeat"
);
continue;
}
self.send_to_worker_or_spawn(
peer_id,
ReplicationTask::Snapshot(metadata.clone()),
ReplicationWorkerConfig {
transport: transport.clone(),
membership: membership.clone(),
retry_policies: retry_policies.clone(),
response_compress_enabled,
role_event_tx: role_tx.clone(),
state_machine_handler: state_machine_handler.clone(),
snapshot_config: snapshot_config.clone(),
},
);
}
} else {
debug!(
"Snapshot targets present but no snapshot available yet; will retry next heartbeat"
);
}
}
Ok(())
}
async fn safe_batch_promote(
&mut self,
batch: Vec<u32>,
ctx: &RaftContext<T>,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
) -> Result<()> {
let change = Change::BatchPromote(BatchPromote {
node_ids: batch.clone(),
new_status: NodeStatus::Active as i32,
});
// Submit batch activation (fire-and-forget; next tick re-tries if not committed).
self.execute_request_immediately(
RaftRequestWithSignal {
id: { rand::distr::Alphanumeric.sample_string(&mut rand::rng(), 21) },
payloads: vec![EntryPayload::config(change)],
senders: vec![],
wait_for_apply_event: false,
},
ctx,
role_tx,
)
.await?;
Ok(())
}
/// Recompute `stale_check_deadline` from the current queue front.
///
/// Must be called after any push_back or drain_batch that changes the front.
/// VecDeque is FIFO so the front is always the oldest entry; the deadline is
/// bound to `front().ready_since + threshold`.
/// Sets `None` when the queue is empty (O(1) short-circuit in tick).
pub fn refresh_stale_deadline(
&mut self,
threshold: Duration,
) {
self.stale_check_deadline =
self.pending_promotions.front().map(|front| front.ready_since + threshold);
}
/// Called from tick() when `stale_check_deadline` fires.
///
/// Pops all front entries whose `ready_since + threshold` has elapsed and
/// calls `handle_stale_learner` for each. After draining, refreshes the deadline
/// so the next stale check is bound to the new front.
pub async fn check_and_purge_stale_front(
&mut self,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
ctx: &RaftContext<T>,
) -> Result<()> {
let threshold = ctx.node_config.raft.membership.promotion.stale_learner_threshold;
let now = Instant::now();
let mut stale_ids = Vec::new();
// Pop all expired front entries (VecDeque is FIFO; front is always oldest)
while let Some(entry) = self.pending_promotions.front() {
if now >= entry.ready_since + threshold {
let entry = self.pending_promotions.pop_front().unwrap();
stale_ids.push(entry.node_id);
} else {
break;
}
}
// Refresh deadline to reflect the new front (or None if queue emptied)
self.refresh_stale_deadline(threshold);
for node_id in stale_ids {
warn!(
node_id,
"Stale learner detected: pending promotion threshold exceeded"
);
metrics::counter!(
"membership.stale_learner_removed",
&[("node_id", node_id.to_string())]
)
.increment(1);
if let Err(e) = self.handle_stale_learner(node_id, role_tx, ctx).await {
error!(node_id, ?e, "Failed to handle stale learner");
}
}
Ok(())
}
/// Handle a ZombieDetected signal from the health monitor.
///
/// Emits a warning log only. Membership removal is a high-risk operation that
/// must not be automated: a node that fails N connection attempts may simply be
/// restarting, and an incorrect BatchRemove would permanently eject it from the
/// cluster. Upper-layer tooling or operators should act on these warnings.
pub async fn handle_zombie_node(
&mut self,
node_id: u32,
_role_tx: &mpsc::UnboundedSender<RoleEvent>,
ctx: &RaftContext<T>,
) -> Result<()> {
let status = ctx.membership().get_node_status(node_id).await;
if status.is_none() {
debug!(node_id, "Zombie signal ignored: node not in cluster");
return Ok(());
}
warn!(
node_id,
?status,
"Zombie detected: node is persistently unreachable — manual intervention may be required"
);
Ok(())
}
/// Remove stalled learner via membership change consensus
pub async fn handle_stale_learner(
&mut self,
node_id: u32,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
ctx: &RaftContext<T>,
) -> Result<()> {
// Stalled learner detected - remove via membership change (requires consensus)
warn!(
"Learner {} is stalled, removing from cluster via consensus",
node_id
);
let change = Change::BatchRemove(BatchRemove {
node_ids: vec![node_id],
});
// Submit removal (fire-and-forget; maintenance re-tries next tick if not committed).
self.execute_request_immediately(
RaftRequestWithSignal {
id: { rand::distr::Alphanumeric.sample_string(&mut rand::rng(), 21) },
payloads: vec![EntryPayload::config(change)],
senders: vec![],
wait_for_apply_event: false,
},
ctx,
role_tx,
)
.await?;
Ok(())
}
/// Returns true if the leader's lease is still within its validity window.
///
/// Uses a monotonic `Instant` so NTP clock adjustments cannot extend the lease.
/// Returns false if no quorum ACK has been received yet (lease_instant is None).
pub fn is_lease_valid(
&self,
ctx: &RaftContext<T>,
) -> bool {
let lease_duration_ms = ctx.node_config().raft.read_consistency.lease_duration_ms;
match *self.lease_instant.lock().expect("lease_instant poisoned") {
Some(t) => (t.elapsed().as_millis() as u64) < lease_duration_ms,
None => false,
}
}
/// Records the current monotonic instant as the last quorum-confirmed timestamp.
/// Called after every quorum ACK (handle_append_result, handle_log_flushed).
fn update_lease_timestamp(&self) {
*self.lease_instant.lock().expect("lease_instant poisoned") = Some(Instant::now());
}
/// Unified write + linearizable read: single RPC for both (2*RTT → 1*RTT).
///
/// Safety: write quorum verification also confirms leadership for reads (Raft §6.4).
pub(super) async fn unified_write_and_linear_read(
&mut self,
ctx: &RaftContext<T>,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
) -> Result<()> {
// Extract write metadata
let (payloads, write_metadata) = if !self.propose_buffer.is_empty() {
if let Some(req) = self.propose_buffer.flush() {
let start_idx = ctx.raft_log().last_entry_id() + 1;
(
req.payloads,
Some(WriteMetadata {
start_idx,
senders: req.senders,
wait_for_apply: req.wait_for_apply_event,
deadline: Instant::now(), // placeholder; overwritten in Phase 2
}),
)
} else {
(Vec::new(), None)
}
} else {
(Vec::new(), None)
};
// Extract read batch
let read_batch = if !self.linearizable_read_buffer.is_empty() {
Some(self.linearizable_read_buffer.take_all())
} else {
None
};
self.execute_and_process_raft_rpc(payloads, write_metadata, read_batch, ctx, role_tx)
.await
}
/// Execute a batch of reads against the state machine and respond to clients.
/// Caller must ensure SM has already applied up to the required read_index.
fn execute_pending_reads(
&self,
read_batch: impl IntoIterator<Item = LinearizableReadRequest>,
ctx: &RaftContext<T>,
) {
for (req, sender) in read_batch {
let results = ctx
.handlers
.state_machine_handler
.read_from_state_machine(req.keys)
.unwrap_or_default();
let _ = sender.send(Ok(ClientResponse::read_results(results)));
}
}
/// Drain all pending lease reads: read from SM and send success response to each sender.
/// Called after update_lease_timestamp() confirms quorum (multi-voter: handle_append_result,
/// single-voter: handle_log_flushed).
fn drain_pending_lease_reads(
&mut self,
ctx: &RaftContext<T>,
) {
while let Some(entry) = self.pending_lease_reads.pop_front() {
let results = ctx
.handlers
.state_machine_handler
.read_from_state_machine(entry.request.keys)
.unwrap_or_default();
let _ = entry.sender.send(Ok(ClientResponse::read_results(results)));
}
}
#[cfg(test)]
pub(crate) fn test_update_lease_timestamp(&mut self) {
self.update_lease_timestamp();
}
/// Returns the current match_index for `peer_id`, or 0 if not yet tracked.
/// Used by tests to verify pipeline out-of-order response handling.
#[cfg(test)]
pub(crate) fn match_index_for_test(
&self,
peer_id: u32,
) -> u64 {
self.match_index.get(&peer_id).copied().unwrap_or(0)
}
/// Returns the number of live per-follower worker handles.
/// Used by tests to assert that workers are spawned / reused / rebuilt correctly.
#[cfg(test)]
pub(crate) fn worker_count_for_test(&self) -> usize {
self.replication_workers.len()
}
/// Injects a dead worker handle for `peer_id` so that the next `send_to_worker_or_spawn`
/// call for that peer observes a closed channel and must rebuild the worker.
///
/// The handle is created by dropping the receiver side immediately after creating the
/// channel, so any subsequent `task_tx.send()` returns `Err(SendError(_))`.
#[cfg(test)]
pub(crate) fn inject_dead_worker_for_test(
&mut self,
peer_id: u32,
) {
let (dead_tx, dead_rx) = tokio::sync::mpsc::unbounded_channel::<ReplicationTask>();
drop(dead_rx); // receiver gone → sends will fail
self.replication_workers.insert(
peer_id,
ReplicationWorkerHandle {
task_tx: dead_tx,
snapshot_failure_count: 0,
snapshot_next_retry_at: None,
},
);
}
/// Determine effective read consistency policy based on client request and server config
pub(super) fn determine_read_policy(
&self,
req: &ClientReadRequest,
) -> ServerReadConsistencyPolicy {
use crate::config::ReadConsistencyPolicy as ClientReadConsistencyPolicy;
if let Some(policy) = &req.consistency_policy {
// Client explicitly specified policy
if self.node_config.raft.read_consistency.allow_client_override {
match policy {
ClientReadConsistencyPolicy::LeaseRead => {
ServerReadConsistencyPolicy::LeaseRead
}
ClientReadConsistencyPolicy::LinearizableRead => {
ServerReadConsistencyPolicy::LinearizableRead
}
ClientReadConsistencyPolicy::EventualConsistency => {
ServerReadConsistencyPolicy::EventualConsistency
}
}
} else {
// Client override not allowed - use server default
self.node_config.raft.read_consistency.default_policy.clone()
}
} else {
// No client policy specified - use server default
self.node_config.raft.read_consistency.default_policy.clone()
}
}
/// Process single lease-based read request
pub(super) async fn process_lease_read(
&mut self,
req: ClientReadRequest,
sender: MaybeCloneOneshotSender<std::result::Result<ClientResponse, Status>>,
ctx: &RaftContext<T>,
role_tx: &mpsc::UnboundedSender<RoleEvent>,
) -> Result<()> {
if self.is_lease_valid(ctx) {
// Lease valid - serve immediately
let results = ctx
.handlers
.state_machine_handler
.read_from_state_machine(req.keys)
.unwrap_or_default();
let response = ClientResponse::read_results(results);
let _ = sender.send(Ok(response));
} else {
// Lease expired - need to confirm leadership before serving.
if self.cluster_metadata.single_voter {
// Single-voter: self is the entire quorum, refresh immediately and serve.
self.update_lease_timestamp();
let results = ctx
.handlers
.state_machine_handler
.read_from_state_machine(req.keys)
.unwrap_or_default();
let _ = sender.send(Ok(ClientResponse::read_results(results)));
} else {
// Multi-voter: queue the request and trigger a heartbeat.
// drain_pending_lease_reads() is called from handle_append_result after
// update_lease_timestamp() confirms quorum ACK from majority.
let deadline = Instant::now()
+ Duration::from_millis(
self.node_config.raft.general_raft_timeout_duration_in_ms,
);
self.pending_lease_reads.push_back(PendingLeaseRead {
request: req,
sender,
deadline,
});
// Fire an empty AppendEntries to trigger lease refresh (fire-and-forget).
self.execute_and_process_raft_rpc(vec![], None, None, ctx, role_tx).await?;
}
}
Ok(())
}
/// Process single eventual consistency read request
fn process_eventual_read(
&mut self,
req: ClientReadRequest,
sender: MaybeCloneOneshotSender<std::result::Result<ClientResponse, Status>>,
ctx: &RaftContext<T>,
) {
// Eventual consistency: serve immediately without any verification
let results = ctx
.handlers
.state_machine_handler
.read_from_state_machine(req.keys)
.unwrap_or_default();
let response = ClientResponse::read_results(results);
let _ = sender.send(Ok(response));
}
}
impl<T: TypeConfig> From<&CandidateState<T>> for LeaderState<T> {
fn from(candidate: &CandidateState<T>) -> Self {
let ReplicationConfig {
rpc_append_entries_clock_in_ms,
..
} = candidate.node_config.raft.replication;
// Clone shared_state and set self as leader immediately
let shared_state = candidate.shared_state.clone();
shared_state.set_current_leader(candidate.node_id());
// Initialize backpressure metrics (None if disabled)
let backpressure_metrics = if candidate.node_config.raft.metrics.enable_backpressure {
Some(Arc::new(BackpressureMetrics::new(
candidate.node_id(),
true,
candidate.node_config.raft.metrics.sample_rate,
)))
} else {
None
};
Self {
shared_state,
timer: Box::new(ReplicationTimer::new(rpc_append_entries_clock_in_ms)),
next_index: HashMap::new(),
match_index: HashMap::new(),
noop_log_id: None,
propose_buffer: Box::new(
ProposeBatchBuffer::new(candidate.node_config.raft.batching.max_batch_size)
.with_length_gauge(
candidate.node_id(),
"propose",
candidate.node_config.raft.metrics.enable_batch,
),
),
node_config: candidate.node_config.clone(),
scheduled_purge_upto: None,
last_purged_index: candidate.last_purged_index,
last_learner_check: Instant::now(),
snapshot_in_progress: AtomicBool::new(false),
stale_check_deadline: None,
pending_promotions: VecDeque::new(),
cluster_metadata: ClusterMetadata {
single_voter: false,
total_voters: 0,
replication_targets: vec![],
},
lease_instant: Mutex::new(None),
linearizable_read_buffer: Box::new(
BatchBuffer::new(candidate.node_config.raft.batching.max_batch_size)
.with_length_gauge(
candidate.node_id(),
"linearizable",
candidate.node_config.raft.metrics.enable_batch,
),
),
lease_read_queue: VecDeque::new(),
eventual_read_queue: VecDeque::new(),
pending_write_apply: HashMap::new(),
pending_reads: BTreeMap::new(),
pending_lease_reads: VecDeque::new(),
replication_workers: HashMap::new(),
pending_client_writes: BTreeMap::new(),
pending_commit_actions: BTreeMap::new(),
write_propose_times: HashMap::new(),
backpressure_metrics,
_marker: PhantomData,
}
}
}
impl<T: TypeConfig> Debug for LeaderState<T> {
fn fmt(
&self,
f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
f.debug_struct("LeaderState")
.field("shared_state", &self.shared_state)
.field("next_index", &self.next_index)
.field("match_index", &self.match_index)
.field("noop_log_id", &self.noop_log_id)
.finish()
}
}
/// Calculates the maximum number of nodes we can promote while keeping the total voter count
/// odd
///
/// - `current`: current number of voting nodes
/// - `available`: number of ready learners pending promotion
///
/// Returns the maximum number of nodes to promote (0 if no safe promotion exists)
pub fn calculate_safe_batch_size(
current: usize,
available: usize,
) -> usize {
if (current + available) % 2 == 1 {
// Promoting all is safe
available
} else {
// We can only promote (available - 1) to keep the invariant
// But if available - 1 == 0, then we cannot promote any?
available.saturating_sub(1)
}
}
// Serialize a native WriteOperation to a proto WriteCommand for Raft log encoding.
// Proto bytes in the log must remain stable; this conversion is the only place
// that touches prost types on the write path.
#[inline]
pub(crate) fn write_op_to_proto(
op: crate::client::WriteOperation
) -> d_engine_proto::client::WriteCommand {
use crate::client::WriteOperation;
use d_engine_proto::client::WriteCommand;
use d_engine_proto::client::write_command::{CompareAndSwap, Delete, Insert, Operation};
match op {
WriteOperation::Insert {
key,
value,
ttl_secs,
} => WriteCommand {
operation: Some(Operation::Insert(Insert {
key,
value,
ttl_secs: ttl_secs.unwrap_or(0),
})),
},
WriteOperation::Delete { key } => WriteCommand {
operation: Some(Operation::Delete(Delete { key })),
},
WriteOperation::CompareAndSwap {
key,
expected,
new_value,
} => WriteCommand {
operation: Some(Operation::CompareAndSwap(CompareAndSwap {
key,
expected_value: expected,
new_value,
})),
},
}
}