mongreldb-cluster 0.63.1

MongrelDB cluster runtime: node identity, meta group, tablets, placement (Stages 2-3).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
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
//! Distributed DDL and online index jobs (spec section 12.11, Stage 3K).
//!
//! One replicated [`DdlJobRecord`] drives one schema element through the
//! spec's DDL phases — `Pending -> WriteOnly -> Backfilling -> Validating ->
//! Public` for additions, `Public -> Dropping` for drops — with the
//! transition graph enforced at apply time in the replicated [`DdlJobStore`].
//! The new-index protocol maps the spec's steps onto explicit collaborators:
//!
//! ```text
//!  1. Replicate definition as WriteOnly      (DdlCommand::SubmitJob + AdvancePhase)
//!  2. New writes maintain the hidden index   (ApplySideIndexMaintainer seam)
//!  3. Backfill every tablet at pinned snapshot (per-tablet backfill drivers,
//!     resumable through DdlJobRecord::tablet_progress)
//!  4. Catch up deltas                        (committed writes from the pinned
//!     snapshot forward, swept until drained)
//!  5. Validate counts/checksums              (TabletValidationReport per
//!     tablet, JobValidationReport aggregate)
//!  6. Publish Public atomically              (DdlCommand::PublishJob — ONE
//!     store command; planner-visible from that metadata version)
//!  7. Old generation reclaimed later         (DdlCommand::ReclaimIndex, gated
//!     on no readers below the retirement metadata version)
//! ```
//!
//! # Reconciliation notes
//!
//! - The job's administrative lifecycle reuses [`crate::meta::SchemaJobState`]
//!   verbatim (submitted `Pending`, `Running/Paused/Cancelling/RollingBack`
//!   driven, terminal `Succeeded/Failed`; the transition graph is the core job
//!   registry's, mirrored by `crate::meta`). The schema-element protocol phase
//!   ([`DdlPhase`]) is the second, orthogonal axis the spec's section 12.11
//!   declares.
//! - [`DdlJobRecord`] is the DDL-native superset of
//!   [`crate::meta::SchemaJobRecord`]: it adds the phase axis, the replicated
//!   [`DdlDefinition`], the pinned `source_schema_version`, and the per-tablet
//!   progress map. [`DdlJobKind::AddIndex`] corresponds to
//!   `SchemaJobKind::IndexBuild`, [`DdlJobKind::AlterSchema`] to
//!   `ColumnBackfill`/`SchemaValidation`; `DropIndex` has no core mirror.
//! - [`DdlJobStore`] is the deterministic, serde-versioned state-machine
//!   section the meta group replicates for distributed DDL (same apply idiom
//!   as [`crate::meta::MetaState`]: the metadata version ticks once per
//!   applied command, refusals are journaled, never faulted). This wave keeps
//!   it a sibling of `MetaState` — `crate::meta`'s format v2 command enum is
//!   frozen by the parallel Stage 3 waves — and the integration wave binds
//!   [`DdlCommand`] onto meta-group proposals. The table anchors
//!   ([`TableAnchor`]) shadow `MetaState::tables`' last-writer-wins
//!   `schema_version` semantics so stale-schema enforcement is deterministic
//!   inside this store.
//!
//! # Write-path contract (spec step 2)
//!
//! While an index record of a table sits in `WriteOnly..=Public`, the tablet
//! apply path dual-maintains it: every committed row mutation additionally
//! lands in the hidden (or public) index generation through
//! [`ApplySideIndexMaintainer`]. The handoff with the backfill driver is
//! timestamp-based: writes committed at or below a tablet's catch-up
//! watermark are covered by the delta sweep (they ride
//! [`BackfillKeyspace::deltas_after`]); writes above it are covered by the
//! maintainer. Both paths are idempotent set/remove operations, so a write
//! covered twice converges. A write that arrives before the hidden generation
//! is installed is skipped by the maintainer and covered by the sweep — the
//! pinned snapshot plus the delta stream always bound it. The engine hook
//! lands with the server wave; [`InMemoryTabletIndexes`] is the reference
//! implementation.
//!
//! # Crash safety and resume
//!
//! The job record in the replicated store is the only progress authority:
//! every per-tablet stage transition is an applied command before the driver
//! moves on. A driver crash (or a pause) leaves the record behind; a new
//! [`DdlDriver`] over the same store resumes, skips tablets already
//! `CaughtUp`/`Validated`, and restarts an interrupted tablet's build from
//! scratch — `HiddenIndexSink::begin_build` discards staged content, the same
//! idiom as the split executor's child-state sink. Resume granularity is one
//! tablet.
//!
//! # Schema versions and stale-schema retry
//!
//! Every job pins the `source_schema_version` it was submitted against
//! (spec section 12.11: the schema version rides every transaction and
//! plan). Submission and the atomic publish both check it against the table
//! anchor; a mismatch refuses with
//! [`DdlRejection::SchemaVersionMismatch`], which [`DdlError::category`]
//! maps onto [`ErrorCategory::SchemaVersionMismatch`] (retry class
//! `AfterMetadataRefresh`): the caller refreshes schema metadata and
//! resubmits.
//!
//! # Scope of this wave
//!
//! The engine binding (real index-key extraction, tablet-core snapshots and
//! committed-log delta streams) lands with the server wave, as does the
//! meta-group raft binding of [`DdlCommand`]. Split/merge topology changes
//! mid-job are reconciled by the driver re-enumerating the tablet set each
//! step ([`DdlCommand::ForgetTabletProgress`] drops departed tablets); an
//! index rebuild that retires a previous public generation is a follow-up —
//! this wave refuses duplicate index names, so the reclamation pass is
//! exercised through drops and cancellation.

use std::collections::{BTreeMap, VecDeque};
use std::fmt;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use mongreldb_types::errors::ErrorCategory;
use mongreldb_types::hlc::{ClockSkewError, HlcClock, HlcTimestamp};
use mongreldb_types::ids::{DatabaseId, MetadataVersion, SchemaVersion, TableId, TabletId};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::meta::{MetaRejectionReason, SchemaJobState};
use crate::split::{RecordStream, SnapshotPin, TabletDataError};
use crate::tablet::Key;

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// Snapshot format version of [`DdlJobStore`]; bumped on any layout change
/// (spec section 4.10).
pub const DDL_STORE_FORMAT_VERSION: u32 = 1;
/// Oldest store format version this build accepts.
pub const MIN_SUPPORTED_DDL_STORE_FORMAT_VERSION: u32 = 1;
/// Bound on the journaled command refusals (mirrors
/// `crate::meta::META_REJECTION_LIMIT`).
pub const DDL_REJECTION_LIMIT: usize = 256;

// ---------------------------------------------------------------------------
// Errors
// ---------------------------------------------------------------------------

/// Why a [`DdlCommand`] was refused at apply. Refusals are deterministic
/// (every replica reaches the same conclusion from the same state) and never
/// fault the state machine: they are journaled in [`DdlJobStore::rejections`]
/// and reported to the proposer.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, thiserror::Error)]
pub enum DdlRejection {
    /// A generic guard reused from the meta control plane's taxonomy
    /// (`StaleWrite`, `Conflict`, `NotFound`, `Invalid`).
    #[error(transparent)]
    Meta(#[from] MetaRejectionReason),
    /// The requested phase edge does not exist in the [`DdlPhase`] graph.
    #[error("illegal DDL phase transition {from} -> {to} for job {job_id}")]
    IllegalPhaseTransition {
        /// The job whose phase was to move.
        job_id: u64,
        /// Current phase.
        from: DdlPhase,
        /// Refused target phase.
        to: DdlPhase,
    },
    /// The job is not `Running`, so the driver must not advance it.
    #[error("DDL job {job_id} is {state:?}, not Running")]
    JobNotRunning {
        /// The job.
        job_id: u64,
        /// Its current administrative state.
        state: SchemaJobState,
    },
    /// The job's pinned schema version no longer matches the table's current
    /// schema version (spec section 12.11: a stale schema returns structured
    /// retry). Maps onto [`ErrorCategory::SchemaVersionMismatch`].
    #[error(
        "schema version mismatch on table {table_id}: job pinned {expected}, \
         table is now {found}; refresh schema metadata and resubmit"
    )]
    SchemaVersionMismatch {
        /// The table.
        table_id: TableId,
        /// The schema version the job pinned at submission.
        expected: SchemaVersion,
        /// The table's current schema version.
        found: SchemaVersion,
    },
    /// A per-tablet progress update would move the durable cursor backwards.
    #[error("tablet {tablet} progress regressed for job {job_id}: {reason}")]
    TabletProgressRegression {
        /// The job.
        job_id: u64,
        /// The tablet.
        tablet: TabletId,
        /// Why the update was refused.
        reason: String,
    },
    /// Publish was attempted before every registered tablet validated.
    #[error("DDL job {job_id} cannot publish: tablets pending validation: {pending:?}")]
    ValidationIncomplete {
        /// The job.
        job_id: u64,
        /// Tablets not yet `Validated`.
        pending: Vec<TabletId>,
    },
    /// A reclamation was attempted while readers below the retirement version
    /// may still hold plans over the retired generation.
    #[error(
        "index `{index_name}` on table {table_id} cannot be reclaimed: oldest reader \
         pins metadata version {oldest_reader:?}, retirement requires {required}"
    )]
    ReclaimBlocked {
        /// The table.
        table_id: TableId,
        /// The index.
        index_name: String,
        /// Oldest live reader pin, when any.
        oldest_reader: Option<MetadataVersion>,
        /// The retirement version every reader must have reached.
        required: MetadataVersion,
    },
}

/// The driver-side failure surface: store refusals, seam errors, and job
/// outcomes the caller must observe (validation failure, cancellation).
#[derive(Debug, thiserror::Error)]
pub enum DdlError {
    /// The replicated store refused a command.
    #[error(transparent)]
    Rejection(#[from] DdlRejection),
    /// A tablet keyspace/sink seam operation failed.
    #[error(transparent)]
    TabletData(#[from] TabletDataError),
    /// The HLC clock refused timestamp allocation.
    #[error(transparent)]
    Clock(#[from] ClockSkewError),
    /// Counts/checksums of the built generation did not match a from-scratch
    /// build; the job was rolled back and the hidden generation dropped.
    #[error("DDL job {job_id} failed validation: {reason}")]
    ValidationFailed {
        /// The failed job.
        job_id: u64,
        /// Which tablet mismatched and how.
        reason: String,
    },
    /// The job was cancelled; the unwind already ran.
    #[error("DDL job {job_id} cancelled")]
    Cancelled {
        /// The cancelled job.
        job_id: u64,
    },
}

impl DdlError {
    /// The stable error taxonomy mapping (spec section 9.7). A stale schema
    /// maps onto [`ErrorCategory::SchemaVersionMismatch`] so the gateway
    /// issues a structured metadata-refresh retry.
    pub fn category(&self) -> ErrorCategory {
        match self {
            Self::Rejection(DdlRejection::SchemaVersionMismatch { .. }) => {
                ErrorCategory::SchemaVersionMismatch
            }
            Self::Rejection(DdlRejection::Meta(MetaRejectionReason::StaleWrite { .. })) => {
                ErrorCategory::StaleMetadata
            }
            Self::Cancelled { .. } => ErrorCategory::Cancelled,
            Self::Rejection(_)
            | Self::TabletData(_)
            | Self::Clock(_)
            | Self::ValidationFailed { .. } => ErrorCategory::ResourceExhausted,
        }
    }
}

// ---------------------------------------------------------------------------
// DDL phases (spec section 12.11, exact)
// ---------------------------------------------------------------------------

/// The replicated protocol phase of one schema element (spec section 12.11).
/// Serde encodes variants by name; names are part of the durable contract and
/// must never change or be reused (spec section 4.10).
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum DdlPhase {
    /// Definition submitted; not yet replicated for the write path.
    Pending,
    /// New writes maintain the hidden definition; nothing planner-visible.
    WriteOnly,
    /// Per-tablet backfill at the pinned snapshot plus delta catch-up.
    Backfilling,
    /// Counts/checksums validated per tablet before publication.
    Validating,
    /// Planner-visible from the publication metadata version.
    Public,
    /// Being dropped: writes stopped maintaining it, the planner stopped
    /// using it; the generation is reclaimed once readers drain.
    Dropping,
}

impl DdlPhase {
    /// Every phase in protocol order (spec section 12.11).
    pub const ALL: [DdlPhase; 6] = [
        DdlPhase::Pending,
        DdlPhase::WriteOnly,
        DdlPhase::Backfilling,
        DdlPhase::Validating,
        DdlPhase::Public,
        DdlPhase::Dropping,
    ];

    /// Whether the `self -> next` edge exists in the spec's graph. This is
    /// the single enforcement point: every store mutation checks it before
    /// applying a phase move. Cancellation and validation failure do not move
    /// phases — they unwind through the administrative state graph and remove
    /// the unpublished record instead.
    pub fn can_transition(self, next: Self) -> bool {
        use DdlPhase::{Backfilling, Dropping, Pending, Public, Validating, WriteOnly};
        matches!(
            (self, next),
            (Pending, WriteOnly)
                | (WriteOnly, Backfilling)
                | (Backfilling, Validating)
                | (Validating, Public)
                | (Public, Dropping)
        )
    }
}

impl fmt::Display for DdlPhase {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match self {
            Self::Pending => "Pending",
            Self::WriteOnly => "WriteOnly",
            Self::Backfilling => "Backfilling",
            Self::Validating => "Validating",
            Self::Public => "Public",
            Self::Dropping => "Dropping",
        };
        f.write_str(name)
    }
}

// ---------------------------------------------------------------------------
// Job kinds and definitions
// ---------------------------------------------------------------------------

/// The Stage 3K DDL job kinds (spec section 12.11).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DdlJobKind {
    /// Online secondary-index build (the spec's worked example).
    AddIndex,
    /// Online index removal.
    DropIndex,
    /// Schema alteration (rides the same phase machine; publish advances the
    /// table's schema version).
    AlterSchema,
}

/// The replicated DDL definition of one job. The engine-opaque payloads ride
/// JSON documents, mirroring `crate::meta::TableSchemaRecord`.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum DdlDefinition {
    /// [`DdlJobKind::AddIndex`]: the new index's name and definition document.
    AddIndex {
        /// Unique index name within the table.
        index_name: String,
        /// Engine-opaque index definition (kind, columns, options).
        spec: serde_json::Value,
    },
    /// [`DdlJobKind::DropIndex`]: the index to remove.
    DropIndex {
        /// The index name; must exist and be `Public`.
        index_name: String,
    },
    /// [`DdlJobKind::AlterSchema`]: the replacement schema document.
    AlterSchema {
        /// Engine-opaque target schema; published at `source + 1`.
        target: serde_json::Value,
    },
}

impl DdlDefinition {
    /// The index name this definition carries, for the index kinds.
    pub fn index_name(&self) -> Option<&str> {
        match self {
            Self::AddIndex { index_name, .. } | Self::DropIndex { index_name } => Some(index_name),
            Self::AlterSchema { .. } => None,
        }
    }
}

// ---------------------------------------------------------------------------
// Per-tablet progress (resumable)
// ---------------------------------------------------------------------------

/// One tablet's position inside a job's `Backfilling`/`Validating` phases.
/// Declaration order is the progress order; a later stage never moves back.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum TabletDdlStage {
    /// Registered for the job; the build has not started (or was restarted).
    Pending,
    /// The build started; staged content may exist beside live state.
    Backfilling,
    /// Snapshot content installed and deltas swept through
    /// [`TabletDdlProgress::caught_up_through`].
    CaughtUp,
    /// Counts/checksums validated for this tablet.
    Validated,
}

impl fmt::Display for TabletDdlStage {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match self {
            Self::Pending => "Pending",
            Self::Backfilling => "Backfilling",
            Self::CaughtUp => "CaughtUp",
            Self::Validated => "Validated",
        };
        f.write_str(name)
    }
}

/// The durable per-tablet progress cursor of one job. A driver restart
/// resumes from this map alone.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TabletDdlProgress {
    /// The tablet.
    pub tablet_id: TabletId,
    /// Furthest stage reached.
    pub stage: TabletDdlStage,
    /// Rows scanned from the pinned snapshot (observability).
    pub rows_scanned: u64,
    /// Catch-up watermark: every committed write at or below this timestamp
    /// is reflected in the tablet's built generation.
    pub caught_up_through: Option<HlcTimestamp>,
    /// The tablet's validation report, once produced.
    pub validation: Option<TabletValidationReport>,
}

impl TabletDdlProgress {
    /// A freshly registered tablet.
    pub fn pending(tablet_id: TabletId) -> Self {
        Self {
            tablet_id,
            stage: TabletDdlStage::Pending,
            rows_scanned: 0,
            caught_up_through: None,
            validation: None,
        }
    }
}

// ---------------------------------------------------------------------------
// Validation reports (spec step 5)
// ---------------------------------------------------------------------------

/// One tablet's typed validation report: the built generation's count and
/// checksum against a from-scratch build at the same watermark.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct TabletValidationReport {
    /// The tablet.
    pub tablet_id: TabletId,
    /// The watermark both sides were compared at.
    pub watermark: HlcTimestamp,
    /// From-scratch row/entry count.
    pub expected_rows: u64,
    /// Built-generation entry count.
    pub actual_rows: u64,
    /// SHA-256 of the from-scratch entry set (sorted key||entry stream).
    pub expected_checksum: [u8; 32],
    /// SHA-256 of the built generation.
    pub actual_checksum: [u8; 32],
}

impl TabletValidationReport {
    /// The tablet passes when counts and checksums both match.
    pub fn passed(&self) -> bool {
        self.expected_rows == self.actual_rows && self.expected_checksum == self.actual_checksum
    }
}

/// The aggregate validation report of one job (spec step 5: per tablet plus
/// aggregate).
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct JobValidationReport {
    /// The job.
    pub job_id: u64,
    /// Every tablet's report, in tablet-id order.
    pub tablets: Vec<TabletValidationReport>,
    /// Sum of expected rows.
    pub total_expected: u64,
    /// Sum of built rows.
    pub total_actual: u64,
    /// Every tablet passed.
    pub passed: bool,
}

/// The deterministic checksum over one built (or from-scratch) entry set:
/// SHA-256 over the sorted key||entry byte stream.
pub fn generation_checksum(entries: &BTreeMap<Key, Vec<u8>>) -> [u8; 32] {
    let mut hasher = Sha256::new();
    for (key, entry) in entries {
        hasher.update(key.as_bytes());
        hasher.update(entry);
    }
    hasher.finalize().into()
}

// ---------------------------------------------------------------------------
// Replicated records
// ---------------------------------------------------------------------------

/// One replicated DDL job record (spec section 12.11). Lives in
/// [`DdlJobStore::jobs`]; the per-tablet progress map is the resume
/// authority.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct DdlJobRecord {
    /// Job identifier (allocated by the proposer; never reused).
    pub job_id: u64,
    /// Database owning the job's target.
    pub database_id: DatabaseId,
    /// Table the job operates on.
    pub table_id: TableId,
    /// Job kind.
    pub kind: DdlJobKind,
    /// Administrative lifecycle (`crate::meta::SchemaJobState`, reused).
    pub state: SchemaJobState,
    /// Schema-element protocol phase (spec section 12.11).
    pub phase: DdlPhase,
    /// The replicated definition.
    pub definition: DdlDefinition,
    /// The table schema version the job was submitted against; checked again
    /// at publish (spec section 12.11 "schema version is included in every
    /// transaction and plan").
    pub source_schema_version: SchemaVersion,
    /// Submission timestamp stamped by the proposer.
    pub created_at: HlcTimestamp,
    /// Timestamp of the last applied mutation.
    pub updated_at: HlcTimestamp,
    /// The job-wide backfill pin, stamped when entering `Backfilling`
    /// (spec step 3: every tablet backfills at this pinned snapshot).
    pub pinned_snapshot: Option<HlcTimestamp>,
    /// Per-tablet resumable progress.
    #[serde(with = "tablet_progress_map")]
    pub tablet_progress: BTreeMap<TabletId, TabletDdlProgress>,
    /// Failure/cancellation detail for terminal states.
    pub error: Option<String>,
    /// Store [`MetadataVersion`] of the last modification.
    #[serde(default = "zero_metadata_version")]
    pub metadata_version: MetadataVersion,
}

fn zero_metadata_version() -> MetadataVersion {
    MetadataVersion::ZERO
}

/// One replicated index record: the schema element a job drives, outliving
/// the job itself. Planner visibility is a pure function of this record and a
/// metadata version (spec step 6).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct DdlIndexRecord {
    /// Table owning the index.
    pub table_id: TableId,
    /// Index name (unique within the table).
    pub index_name: String,
    /// Engine-opaque index definition document.
    pub definition: serde_json::Value,
    /// Element protocol phase (mirrors the owning job's phase while the job
    /// runs; `Public`/`Dropping` persist after it terminates).
    pub phase: DdlPhase,
    /// The job that last drove the record.
    pub job_id: u64,
    /// Creation timestamp.
    pub created_at: HlcTimestamp,
    /// The metadata version the index became planner-visible at
    /// (`planner_visible_at` includes the record at or above this version).
    pub publication_version: Option<MetadataVersion>,
    /// Publication timestamp.
    pub published_at: Option<HlcTimestamp>,
    /// The metadata version the index entered `Dropping` at (planners at or
    /// above it stop using the index; reclamation waits on reader pins below
    /// it).
    pub dropping_since: Option<MetadataVersion>,
    /// Store [`MetadataVersion`] of the last modification.
    #[serde(default = "zero_metadata_version")]
    pub metadata_version: MetadataVersion,
}

/// The replicated table anchor this module's stale-schema checks run against.
/// Shadows `crate::meta::MetaState::tables` (same last-writer-wins
/// `schema_version` semantics); the integration wave feeds it from
/// `MetaCommand::SetTableSchema` applies.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TableAnchor {
    /// The table.
    pub table_id: TableId,
    /// Database owning the table.
    pub database_id: DatabaseId,
    /// Current schema version (never reused, never lowered).
    pub schema_version: SchemaVersion,
    /// Current schema document (opaque to the meta group).
    pub schema: serde_json::Value,
    /// Store [`MetadataVersion`] of the last modification.
    #[serde(default = "zero_metadata_version")]
    pub metadata_version: MetadataVersion,
}

// ---------------------------------------------------------------------------
// Commands
// ---------------------------------------------------------------------------

/// One replicated DDL control-plane command (spec section 12.11). Every
/// variant is deterministic and idempotent at apply: records are versioned
/// and guards reject stale or conflicting writes with a typed
/// [`DdlRejection`] instead of faulting the state machine (the
/// `crate::meta::MetaCommand` idiom).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum DdlCommand {
    /// Registers (or advances) a table anchor. Last-writer-wins by
    /// `schema_version`, mirroring `MetaCommand::SetTableSchema`.
    RegisterTable {
        /// The anchor to apply.
        anchor: TableAnchor,
    },
    /// Submits a job (starts `SchemaJobState::Pending`; `DdlPhase::Pending`,
    /// or `DdlPhase::Public` for a drop, whose element already is public).
    SubmitJob {
        /// The job record.
        job: DdlJobRecord,
    },
    /// Advances one job along the [`DdlPhase`] graph. `Public` for an
    /// addition is never reached here — publication rides [`Self::PublishJob`]
    /// so validation completeness and the schema-version recheck gate the one
    /// atomic command. `pinned_snapshot` is required for `Backfilling`.
    AdvancePhase {
        /// The job.
        job_id: u64,
        /// Target phase.
        to: DdlPhase,
        /// The job-wide backfill pin (required when `to` is `Backfilling`).
        pinned_snapshot: Option<HlcTimestamp>,
        /// Optimistic-concurrency token over the job record.
        expected_version: Option<MetadataVersion>,
    },
    /// Registers or advances one tablet's durable progress cursor. Updates
    /// are monotonic: the stage never moves backwards and `rows_scanned`
    /// never shrinks within one stage.
    UpdateTabletProgress {
        /// The job.
        job_id: u64,
        /// The new cursor.
        progress: TabletDdlProgress,
    },
    /// Drops the progress cursor of a tablet that left the table's topology
    /// (split/merge reconciliation; only while the job still drives tablets).
    ForgetTabletProgress {
        /// The job.
        job_id: u64,
        /// The departed tablet.
        tablet_id: TabletId,
    },
    /// Records one tablet's validation report; a passing report moves the
    /// tablet to `Validated`.
    ReportTabletValidation {
        /// The job.
        job_id: u64,
        /// The report.
        report: TabletValidationReport,
    },
    /// The atomic publication (spec step 6): validates completeness and the
    /// pinned schema version, flips the element to `Public` stamped with this
    /// command's metadata version, and succeeds the job — one command.
    PublishJob {
        /// The job.
        job_id: u64,
        /// Publication timestamp stamped by the proposer.
        published_at: HlcTimestamp,
    },
    /// Moves the administrative lifecycle (`pause`/`resume`/`cancel`/
    /// `admit`/`succeed`/`fail`), graph-enforced.
    SetJobState {
        /// The job.
        job_id: u64,
        /// Target state.
        state: SchemaJobState,
        /// Update timestamp stamped by the proposer.
        updated_at: HlcTimestamp,
        /// Failure/cancellation detail.
        error: Option<String>,
        /// Optimistic-concurrency token over the job record.
        expected_version: Option<MetadataVersion>,
    },
    /// Unwinds an unpublished index record (cancel/validation-failure
    /// rollback). Refuses to remove a `Public` record — fail closed.
    RemoveIndexRecord {
        /// The job owning the record.
        job_id: u64,
    },
    /// The reclamation pass (spec step 7): removes a `Dropping` index record
    /// once no live reader pins a metadata version below its retirement
    /// version.
    ReclaimIndex {
        /// The table.
        table_id: TableId,
        /// The index.
        index_name: String,
    },
    /// Pins a reader at a metadata version (its plans may reference every
    /// element visible there). A reader's pin only moves forward.
    PinReader {
        /// Reader identifier.
        reader_id: u64,
        /// The pinned metadata version.
        version: MetadataVersion,
    },
    /// Releases one reader pin.
    ReleaseReader {
        /// Reader identifier.
        reader_id: u64,
    },
}

// ---------------------------------------------------------------------------
// The replicated store
// ---------------------------------------------------------------------------

/// One journaled refusal: the refused command's id and the typed reason.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct DdlStoreRejection {
    /// Leader-assigned id of the refused command (`None` when it carried
    /// none; always `Some` through the raft binding).
    pub command_id: Option<[u8; 16]>,
    /// Why it was refused.
    pub reason: DdlRejection,
}

/// The replicated DDL control-plane state (spec section 12.11): the section
/// of meta state the DDL jobs, index records, table anchors, and reader pins
/// live in. Deterministic — every replica applies the same commands in the
/// same order and reaches byte-identical state; versioned — `metadata_version`
/// ticks once per applied command (accepted or refused — a refusal appends to
/// the rejection journal, which is itself state), giving readers and the
/// reclamation gate a monotonic watermark.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct DdlJobStore {
    /// Snapshot format version; see [`DDL_STORE_FORMAT_VERSION`].
    pub format_version: u32,
    /// Monotonic per-applied-command version.
    pub metadata_version: MetadataVersion,
    /// Next job id to allocate (strictly greater than every live id).
    pub next_job_id: u64,
    /// Table anchors (stale-schema authority of this store).
    pub tables: BTreeMap<TableId, TableAnchor>,
    /// DDL jobs by id.
    pub jobs: BTreeMap<u64, DdlJobRecord>,
    /// Index records by (table, name).
    #[serde(with = "index_record_map")]
    pub indexes: BTreeMap<(TableId, String), DdlIndexRecord>,
    /// Live reader pins (reader id -> pinned metadata version).
    pub reader_pins: BTreeMap<u64, MetadataVersion>,
    /// Bounded journal of refused commands, oldest first
    /// ([`DDL_REJECTION_LIMIT`]).
    pub rejections: VecDeque<DdlStoreRejection>,
}

impl Default for DdlJobStore {
    fn default() -> Self {
        Self {
            format_version: DDL_STORE_FORMAT_VERSION,
            metadata_version: MetadataVersion::ZERO,
            next_job_id: 1,
            tables: BTreeMap::new(),
            jobs: BTreeMap::new(),
            indexes: BTreeMap::new(),
            reader_pins: BTreeMap::new(),
            rejections: VecDeque::new(),
        }
    }
}

/// JSON-safe serde for the index-record map: `(TableId, String)` tuple keys
/// are not JSON object keys, so the map is encoded as a sequence of
/// `(table, name, record)` triples (and decoded back). Deterministic in
/// `BTreeMap` order.
mod index_record_map {
    use super::{DdlIndexRecord, TableId};
    use serde::{Deserialize, Deserializer, Serialize, Serializer};
    use std::collections::BTreeMap;

    pub fn serialize<S>(
        map: &BTreeMap<(TableId, String), DdlIndexRecord>,
        serializer: S,
    ) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let triples: Vec<(TableId, String, DdlIndexRecord)> = map
            .iter()
            .map(|((table, name), record)| (*table, name.clone(), record.clone()))
            .collect();
        triples.serialize(serializer)
    }

    pub fn deserialize<'de, D>(
        deserializer: D,
    ) -> Result<BTreeMap<(TableId, String), DdlIndexRecord>, D::Error>
    where
        D: Deserializer<'de>,
    {
        let triples: Vec<(TableId, String, DdlIndexRecord)> = Vec::deserialize(deserializer)?;
        Ok(triples
            .into_iter()
            .map(|(table, name, record)| ((table, name), record))
            .collect())
    }
}

/// JSON-safe serde for the per-tablet progress map: `TabletId` is a 16-byte
/// newtype, not a JSON object key, so the map is encoded as a sequence of
/// `(tablet, progress)` pairs (and decoded back). Deterministic in
/// `BTreeMap` order.
mod tablet_progress_map {
    use super::{TabletDdlProgress, TabletId};
    use serde::{Deserialize, Deserializer, Serialize, Serializer};
    use std::collections::BTreeMap;

    pub fn serialize<S>(
        map: &BTreeMap<TabletId, TabletDdlProgress>,
        serializer: S,
    ) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let pairs: Vec<(TabletId, TabletDdlProgress)> = map
            .iter()
            .map(|(tablet, progress)| (*tablet, progress.clone()))
            .collect();
        pairs.serialize(serializer)
    }

    pub fn deserialize<'de, D>(
        deserializer: D,
    ) -> Result<BTreeMap<TabletId, TabletDdlProgress>, D::Error>
    where
        D: Deserializer<'de>,
    {
        let pairs: Vec<(TabletId, TabletDdlProgress)> = Vec::deserialize(deserializer)?;
        Ok(pairs.into_iter().collect())
    }
}

impl DdlJobStore {
    /// Applies one committed command. `metadata_version` ticks first (every
    /// applied command moves the watermark); a refusal journals the reason
    /// and leaves the records untouched (the `crate::meta::MetaState::apply`
    /// idiom).
    pub fn apply(
        &mut self,
        command: &DdlCommand,
        command_id: Option<[u8; 16]>,
        commit_ts: HlcTimestamp,
    ) -> Result<(), DdlRejection> {
        self.metadata_version = MetadataVersion(self.metadata_version.get() + 1);
        let version = self.metadata_version;
        let result = self.dispatch(command, commit_ts, version);
        if let Err(reason) = &result {
            self.rejections.push_back(DdlStoreRejection {
                command_id,
                reason: reason.clone(),
            });
            while self.rejections.len() > DDL_REJECTION_LIMIT {
                self.rejections.pop_front();
            }
        }
        result
    }

    fn dispatch(
        &mut self,
        command: &DdlCommand,
        commit_ts: HlcTimestamp,
        version: MetadataVersion,
    ) -> Result<(), DdlRejection> {
        match command {
            DdlCommand::RegisterTable { anchor } => self.apply_register_table(anchor, version),
            DdlCommand::SubmitJob { job } => self.apply_submit_job(job, commit_ts, version),
            DdlCommand::AdvancePhase {
                job_id,
                to,
                pinned_snapshot,
                expected_version,
            } => self.apply_advance_phase(
                *job_id,
                *to,
                *pinned_snapshot,
                *expected_version,
                commit_ts,
                version,
            ),
            DdlCommand::UpdateTabletProgress { job_id, progress } => {
                self.apply_update_tablet_progress(*job_id, progress, commit_ts, version)
            }
            DdlCommand::ForgetTabletProgress { job_id, tablet_id } => {
                self.apply_forget_tablet_progress(*job_id, *tablet_id, commit_ts, version)
            }
            DdlCommand::ReportTabletValidation { job_id, report } => {
                self.apply_report_tablet_validation(*job_id, report, commit_ts, version)
            }
            DdlCommand::PublishJob {
                job_id,
                published_at,
            } => self.apply_publish_job(*job_id, *published_at, commit_ts, version),
            DdlCommand::SetJobState {
                job_id,
                state,
                updated_at,
                error,
                expected_version,
            } => self.apply_set_job_state(
                *job_id,
                *state,
                *updated_at,
                error,
                *expected_version,
                version,
            ),
            DdlCommand::RemoveIndexRecord { job_id } => {
                self.apply_remove_index_record(*job_id, version)
            }
            DdlCommand::ReclaimIndex {
                table_id,
                index_name,
            } => self.apply_reclaim_index(*table_id, index_name, version),
            DdlCommand::PinReader { reader_id, version } => {
                self.apply_pin_reader(*reader_id, *version)
            }
            DdlCommand::ReleaseReader { reader_id } => {
                self.reader_pins.remove(reader_id);
                Ok(())
            }
        }
    }

    fn apply_register_table(
        &mut self,
        anchor: &TableAnchor,
        version: MetadataVersion,
    ) -> Result<(), DdlRejection> {
        if anchor.table_id == TableId::ZERO {
            return Err(MetaRejectionReason::Invalid {
                reason: "reserved zero table id".to_owned(),
            }
            .into());
        }
        if anchor.schema_version == SchemaVersion::ZERO {
            return Err(MetaRejectionReason::Invalid {
                reason: "reserved zero schema version".to_owned(),
            }
            .into());
        }
        match self.tables.get(&anchor.table_id) {
            Some(existing) => {
                if anchor.schema_version > existing.schema_version {
                    let mut anchor = anchor.clone();
                    anchor.metadata_version = version;
                    self.tables.insert(anchor.table_id, anchor);
                    Ok(())
                } else if anchor.schema_version == existing.schema_version {
                    if existing.database_id == anchor.database_id
                        && existing.schema == anchor.schema
                    {
                        Ok(())
                    } else {
                        Err(MetaRejectionReason::Conflict {
                            resource: format!("table {}", anchor.table_id),
                            reason: "schema version already used for different content".to_owned(),
                        }
                        .into())
                    }
                } else {
                    Err(MetaRejectionReason::StaleWrite {
                        resource: format!("table {}", anchor.table_id),
                        current: MetadataVersion(existing.schema_version.get()),
                        attempted: MetadataVersion(anchor.schema_version.get()),
                    }
                    .into())
                }
            }
            None => {
                let mut anchor = anchor.clone();
                anchor.metadata_version = version;
                self.tables.insert(anchor.table_id, anchor);
                Ok(())
            }
        }
    }

    fn apply_submit_job(
        &mut self,
        job: &DdlJobRecord,
        commit_ts: HlcTimestamp,
        version: MetadataVersion,
    ) -> Result<(), DdlRejection> {
        if job.job_id == 0 {
            return Err(MetaRejectionReason::Invalid {
                reason: "reserved zero job id".to_owned(),
            }
            .into());
        }
        // Idempotent replay: an identical record under an existing id is a
        // no-op; a different one conflicts (the `SubmitSchemaJob` idiom).
        if let Some(existing) = self.jobs.get(&job.job_id) {
            let mut comparable = existing.clone();
            comparable.metadata_version = job.metadata_version;
            return if comparable == *job {
                Ok(())
            } else {
                Err(MetaRejectionReason::Conflict {
                    resource: format!("DDL job {}", job.job_id),
                    reason: "job id already exists with different content".to_owned(),
                }
                .into())
            };
        }
        let anchor = self
            .tables
            .get(&job.table_id)
            .ok_or(MetaRejectionReason::NotFound {
                resource: format!("table {}", job.table_id),
            })?;
        if anchor.database_id != job.database_id {
            return Err(MetaRejectionReason::Conflict {
                resource: format!("table {}", job.table_id),
                reason: "table belongs to a different database".to_owned(),
            }
            .into());
        }
        // Stale schema at submission: structured retry (spec section 12.11).
        if anchor.schema_version != job.source_schema_version {
            return Err(DdlRejection::SchemaVersionMismatch {
                table_id: job.table_id,
                expected: job.source_schema_version,
                found: anchor.schema_version,
            });
        }
        if job.state != SchemaJobState::Pending {
            return Err(MetaRejectionReason::Invalid {
                reason: "submitted jobs start Pending".to_owned(),
            }
            .into());
        }
        let expected_phase = match job.kind {
            DdlJobKind::AddIndex | DdlJobKind::AlterSchema => DdlPhase::Pending,
            DdlJobKind::DropIndex => DdlPhase::Public,
        };
        if job.phase != expected_phase {
            return Err(MetaRejectionReason::Invalid {
                reason: format!(
                    "{:?} jobs start in phase {expected_phase}, not {}",
                    job.kind, job.phase
                ),
            }
            .into());
        }
        // One non-terminal DDL job per table (serialization keeps the
        // per-tablet progress maps deterministic; relaxing this is a
        // documented follow-up).
        if let Some(active) = self
            .jobs
            .values()
            .find(|existing| existing.table_id == job.table_id && !existing.state.is_terminal())
        {
            return Err(MetaRejectionReason::Conflict {
                resource: format!("table {}", job.table_id),
                reason: format!("table already has active DDL job {}", active.job_id),
            }
            .into());
        }
        match &job.definition {
            DdlDefinition::AddIndex { index_name, spec } => {
                if index_name.trim().is_empty() {
                    return Err(MetaRejectionReason::Invalid {
                        reason: "index name is empty".to_owned(),
                    }
                    .into());
                }
                let key = (job.table_id, index_name.clone());
                if self.indexes.contains_key(&key) {
                    return Err(MetaRejectionReason::Conflict {
                        resource: format!("index `{index_name}` on table {}", job.table_id),
                        reason: "index name already exists".to_owned(),
                    }
                    .into());
                }
                self.indexes.insert(
                    key,
                    DdlIndexRecord {
                        table_id: job.table_id,
                        index_name: index_name.clone(),
                        definition: spec.clone(),
                        phase: DdlPhase::Pending,
                        job_id: job.job_id,
                        created_at: commit_ts,
                        publication_version: None,
                        published_at: None,
                        dropping_since: None,
                        metadata_version: version,
                    },
                );
            }
            DdlDefinition::DropIndex { index_name } => {
                let key = (job.table_id, index_name.clone());
                match self.indexes.get(&key) {
                    None => {
                        return Err(MetaRejectionReason::NotFound {
                            resource: format!("index `{index_name}` on table {}", job.table_id),
                        }
                        .into());
                    }
                    Some(record) if record.phase != DdlPhase::Public => {
                        return Err(MetaRejectionReason::Conflict {
                            resource: format!("index `{index_name}` on table {}", job.table_id),
                            reason: format!("index is {}, not Public", record.phase),
                        }
                        .into());
                    }
                    Some(_) => {}
                }
            }
            DdlDefinition::AlterSchema { .. } => {}
        }
        let mut job = job.clone();
        job.metadata_version = version;
        job.updated_at = commit_ts;
        self.next_job_id = self.next_job_id.max(job.job_id + 1);
        self.jobs.insert(job.job_id, job);
        Ok(())
    }

    fn apply_advance_phase(
        &mut self,
        job_id: u64,
        to: DdlPhase,
        pinned_snapshot: Option<HlcTimestamp>,
        expected_version: Option<MetadataVersion>,
        commit_ts: HlcTimestamp,
        version: MetadataVersion,
    ) -> Result<(), DdlRejection> {
        let job = self
            .jobs
            .get(&job_id)
            .ok_or(MetaRejectionReason::NotFound {
                resource: format!("DDL job {job_id}"),
            })?;
        if let Some(expected) = expected_version {
            if expected != job.metadata_version {
                return Err(MetaRejectionReason::StaleWrite {
                    resource: format!("DDL job {job_id}"),
                    current: job.metadata_version,
                    attempted: expected,
                }
                .into());
            }
        }
        if job.state.is_terminal() {
            return Err(MetaRejectionReason::Conflict {
                resource: format!("DDL job {job_id}"),
                reason: format!("terminal state {:?}", job.state),
            }
            .into());
        }
        if job.state != SchemaJobState::Running {
            return Err(DdlRejection::JobNotRunning {
                job_id,
                state: job.state,
            });
        }
        // Idempotent replay of an already-applied advance.
        if job.phase == to {
            if to == DdlPhase::Backfilling && job.pinned_snapshot != pinned_snapshot {
                return Err(MetaRejectionReason::Conflict {
                    resource: format!("DDL job {job_id}"),
                    reason: "phase already reached with a different pinned snapshot".to_owned(),
                }
                .into());
            }
            return Ok(());
        }
        if !job.phase.can_transition(to) {
            return Err(DdlRejection::IllegalPhaseTransition {
                job_id,
                from: job.phase,
                to,
            });
        }
        // Addition kinds publish only through PublishJob, so the atomic
        // command carries the completeness and schema-version gates.
        if to == DdlPhase::Public && job.kind != DdlJobKind::DropIndex {
            return Err(MetaRejectionReason::Invalid {
                reason: "Public is reached through PublishJob, never AdvancePhase".to_owned(),
            }
            .into());
        }
        if to == DdlPhase::Backfilling && pinned_snapshot.is_none() {
            return Err(MetaRejectionReason::Invalid {
                reason: "Backfilling requires the job-wide pinned snapshot".to_owned(),
            }
            .into());
        }
        let job = self.jobs.get_mut(&job_id).expect("job existence checked");
        job.phase = to;
        if to == DdlPhase::Backfilling {
            job.pinned_snapshot = pinned_snapshot;
        }
        job.updated_at = commit_ts;
        job.metadata_version = version;
        // Index records mirror the element phase while a job drives them.
        if let Some(index_name) = job.definition.index_name() {
            let key = (job.table_id, index_name.to_owned());
            if let Some(record) = self.indexes.get_mut(&key) {
                record.phase = to;
                if to == DdlPhase::Dropping {
                    record.dropping_since = Some(version);
                }
                record.metadata_version = version;
            }
        }
        Ok(())
    }

    fn apply_update_tablet_progress(
        &mut self,
        job_id: u64,
        progress: &TabletDdlProgress,
        commit_ts: HlcTimestamp,
        version: MetadataVersion,
    ) -> Result<(), DdlRejection> {
        let job = self
            .jobs
            .get_mut(&job_id)
            .ok_or(MetaRejectionReason::NotFound {
                resource: format!("DDL job {job_id}"),
            })?;
        if job.phase != DdlPhase::Backfilling {
            // Replay tolerance: once the job moved on, only dominated
            // (already-covered) updates remain legal.
            let dominated = job
                .tablet_progress
                .get(&progress.tablet_id)
                .is_some_and(|existing| {
                    existing.stage >= progress.stage
                        && existing.rows_scanned >= progress.rows_scanned
                });
            if dominated {
                return Ok(());
            }
            return Err(MetaRejectionReason::Conflict {
                resource: format!("DDL job {job_id}"),
                reason: format!("job is {}, not Backfilling", job.phase),
            }
            .into());
        }
        if let Some(existing) = job.tablet_progress.get(&progress.tablet_id) {
            if progress.stage < existing.stage {
                return Err(DdlRejection::TabletProgressRegression {
                    job_id,
                    tablet: progress.tablet_id,
                    reason: format!("stage {} -> {}", existing.stage, progress.stage),
                });
            }
            if progress.stage == existing.stage && progress.rows_scanned < existing.rows_scanned {
                return Err(DdlRejection::TabletProgressRegression {
                    job_id,
                    tablet: progress.tablet_id,
                    reason: format!(
                        "rows_scanned {} -> {}",
                        existing.rows_scanned, progress.rows_scanned
                    ),
                });
            }
        }
        job.tablet_progress
            .insert(progress.tablet_id, progress.clone());
        job.updated_at = commit_ts;
        job.metadata_version = version;
        Ok(())
    }

    fn apply_forget_tablet_progress(
        &mut self,
        job_id: u64,
        tablet_id: TabletId,
        commit_ts: HlcTimestamp,
        version: MetadataVersion,
    ) -> Result<(), DdlRejection> {
        let job = self
            .jobs
            .get_mut(&job_id)
            .ok_or(MetaRejectionReason::NotFound {
                resource: format!("DDL job {job_id}"),
            })?;
        if !matches!(job.phase, DdlPhase::Backfilling | DdlPhase::Validating) {
            return Err(MetaRejectionReason::Conflict {
                resource: format!("DDL job {job_id}"),
                reason: format!(
                    "job is {}; tablet cursors are only mutable while driving",
                    job.phase
                ),
            }
            .into());
        }
        if job.tablet_progress.remove(&tablet_id).is_some() {
            job.updated_at = commit_ts;
            job.metadata_version = version;
        }
        Ok(())
    }

    fn apply_report_tablet_validation(
        &mut self,
        job_id: u64,
        report: &TabletValidationReport,
        commit_ts: HlcTimestamp,
        version: MetadataVersion,
    ) -> Result<(), DdlRejection> {
        let job = self
            .jobs
            .get_mut(&job_id)
            .ok_or(MetaRejectionReason::NotFound {
                resource: format!("DDL job {job_id}"),
            })?;
        if job.phase != DdlPhase::Validating {
            return Err(MetaRejectionReason::Conflict {
                resource: format!("DDL job {job_id}"),
                reason: format!("job is {}, not Validating", job.phase),
            }
            .into());
        }
        let progress = job.tablet_progress.get_mut(&report.tablet_id).ok_or(
            MetaRejectionReason::NotFound {
                resource: format!("tablet {} progress of job {job_id}", report.tablet_id),
            },
        )?;
        if progress.stage < TabletDdlStage::CaughtUp {
            return Err(DdlRejection::TabletProgressRegression {
                job_id,
                tablet: report.tablet_id,
                reason: "validation reported before catch-up completed".to_owned(),
            });
        }
        if progress.stage == TabletDdlStage::Validated
            && progress.validation.as_ref() == Some(report)
        {
            return Ok(());
        }
        progress.validation = Some(report.clone());
        progress.caught_up_through = Some(report.watermark);
        if report.passed() {
            progress.stage = TabletDdlStage::Validated;
        }
        job.updated_at = commit_ts;
        job.metadata_version = version;
        Ok(())
    }

    fn apply_publish_job(
        &mut self,
        job_id: u64,
        published_at: HlcTimestamp,
        commit_ts: HlcTimestamp,
        version: MetadataVersion,
    ) -> Result<(), DdlRejection> {
        let job = self
            .jobs
            .get(&job_id)
            .ok_or(MetaRejectionReason::NotFound {
                resource: format!("DDL job {job_id}"),
            })?;
        if job.state == SchemaJobState::Succeeded
            && job.phase == DdlPhase::Public
            && job.kind != DdlJobKind::DropIndex
        {
            return Ok(()); // idempotent replay of the publication
        }
        if job.kind == DdlJobKind::DropIndex {
            return Err(MetaRejectionReason::Invalid {
                reason: "drop jobs ride AdvancePhase(Public -> Dropping), never PublishJob"
                    .to_owned(),
            }
            .into());
        }
        if job.state != SchemaJobState::Running {
            return Err(DdlRejection::JobNotRunning {
                job_id,
                state: job.state,
            });
        }
        if job.phase != DdlPhase::Validating {
            return Err(DdlRejection::IllegalPhaseTransition {
                job_id,
                from: job.phase,
                to: DdlPhase::Public,
            });
        }
        // Stale-schema recheck (spec section 12.11: a stale schema returns
        // structured retry): the table must not have moved since submission.
        let anchor = self
            .tables
            .get(&job.table_id)
            .ok_or(MetaRejectionReason::NotFound {
                resource: format!("table {}", job.table_id),
            })?;
        if anchor.schema_version != job.source_schema_version {
            return Err(DdlRejection::SchemaVersionMismatch {
                table_id: job.table_id,
                expected: job.source_schema_version,
                found: anchor.schema_version,
            });
        }
        let pending: Vec<TabletId> = job
            .tablet_progress
            .values()
            .filter(|progress| {
                progress.stage != TabletDdlStage::Validated
                    || progress
                        .validation
                        .as_ref()
                        .is_none_or(|report| !report.passed())
            })
            .map(|progress| progress.tablet_id)
            .collect();
        if job.tablet_progress.is_empty() || !pending.is_empty() {
            return Err(DdlRejection::ValidationIncomplete { job_id, pending });
        }
        match &job.definition {
            DdlDefinition::AddIndex { index_name, .. } => {
                let key = (job.table_id, index_name.clone());
                let record = self
                    .indexes
                    .get_mut(&key)
                    .ok_or(MetaRejectionReason::NotFound {
                        resource: format!("index `{index_name}` on table {}", job.table_id),
                    })?;
                record.phase = DdlPhase::Public;
                record.publication_version = Some(version);
                record.published_at = Some(published_at);
                record.metadata_version = version;
            }
            DdlDefinition::AlterSchema { target } => {
                let anchor = self
                    .tables
                    .get_mut(&job.table_id)
                    .expect("anchor existence checked");
                anchor.schema_version = SchemaVersion(job.source_schema_version.get() + 1);
                anchor.schema = target.clone();
                anchor.metadata_version = version;
            }
            DdlDefinition::DropIndex { .. } => unreachable!("drop kind refused above"),
        }
        let job = self.jobs.get_mut(&job_id).expect("job existence checked");
        job.phase = DdlPhase::Public;
        job.state = SchemaJobState::Succeeded;
        job.updated_at = commit_ts;
        job.metadata_version = version;
        Ok(())
    }

    fn apply_set_job_state(
        &mut self,
        job_id: u64,
        state: SchemaJobState,
        updated_at: HlcTimestamp,
        error: &Option<String>,
        expected_version: Option<MetadataVersion>,
        version: MetadataVersion,
    ) -> Result<(), DdlRejection> {
        let record = self
            .jobs
            .get_mut(&job_id)
            .ok_or(MetaRejectionReason::NotFound {
                resource: format!("DDL job {job_id}"),
            })?;
        if let Some(expected) = expected_version {
            if expected != record.metadata_version {
                return Err(MetaRejectionReason::StaleWrite {
                    resource: format!("DDL job {job_id}"),
                    current: record.metadata_version,
                    attempted: expected,
                }
                .into());
            }
        }
        if record.state.is_terminal() {
            return Err(MetaRejectionReason::Conflict {
                resource: format!("DDL job {job_id}"),
                reason: format!("terminal state {:?}", record.state),
            }
            .into());
        }
        if record.state != state && !record.state.can_transition(state) {
            return Err(MetaRejectionReason::Conflict {
                resource: format!("DDL job {job_id}"),
                reason: format!("illegal transition {:?} -> {:?}", record.state, state),
            }
            .into());
        }
        if record.state == state && record.error == *error {
            return Ok(());
        }
        record.state = state;
        record.updated_at = updated_at;
        record.error = error.clone();
        record.metadata_version = version;
        Ok(())
    }

    fn apply_remove_index_record(
        &mut self,
        job_id: u64,
        version: MetadataVersion,
    ) -> Result<(), DdlRejection> {
        let job = self
            .jobs
            .get(&job_id)
            .ok_or(MetaRejectionReason::NotFound {
                resource: format!("DDL job {job_id}"),
            })?;
        if !matches!(
            job.state,
            SchemaJobState::RollingBack | SchemaJobState::Failed
        ) {
            return Err(MetaRejectionReason::Conflict {
                resource: format!("DDL job {job_id}"),
                reason: "index records are removed only during rollback".to_owned(),
            }
            .into());
        }
        let Some(index_name) = job.definition.index_name() else {
            return Ok(());
        };
        let key = (job.table_id, index_name.to_owned());
        match self.indexes.get(&key) {
            None => Ok(()),
            Some(record) if record.phase == DdlPhase::Public => {
                Err(MetaRejectionReason::Conflict {
                    resource: format!("index `{index_name}` on table {}", job.table_id),
                    reason: "refusing to remove a published index".to_owned(),
                }
                .into())
            }
            Some(_) => {
                self.indexes.remove(&key);
                let job = self.jobs.get_mut(&job_id).expect("job existence checked");
                job.metadata_version = version;
                Ok(())
            }
        }
    }

    fn apply_reclaim_index(
        &mut self,
        table_id: TableId,
        index_name: &str,
        _version: MetadataVersion,
    ) -> Result<(), DdlRejection> {
        let key = (table_id, index_name.to_owned());
        let Some(record) = self.indexes.get(&key) else {
            return Ok(()); // already reclaimed (idempotent)
        };
        if record.phase != DdlPhase::Dropping {
            return Err(MetaRejectionReason::Conflict {
                resource: format!("index `{index_name}` on table {table_id}"),
                reason: format!("index is {}, not Dropping", record.phase),
            }
            .into());
        }
        let required = record
            .dropping_since
            .expect("Dropping records carry dropping_since");
        let oldest_reader = self.oldest_reader_version();
        if oldest_reader.is_some_and(|oldest| oldest < required) {
            return Err(DdlRejection::ReclaimBlocked {
                table_id,
                index_name: index_name.to_owned(),
                oldest_reader,
                required,
            });
        }
        self.indexes.remove(&key);
        Ok(())
    }

    fn apply_pin_reader(
        &mut self,
        reader_id: u64,
        version: MetadataVersion,
    ) -> Result<(), DdlRejection> {
        if reader_id == 0 {
            return Err(MetaRejectionReason::Invalid {
                reason: "reserved zero reader id".to_owned(),
            }
            .into());
        }
        if let Some(existing) = self.reader_pins.get(&reader_id) {
            if version < *existing {
                return Err(MetaRejectionReason::Conflict {
                    resource: format!("reader {reader_id}"),
                    reason: format!("reader pins only move forward ({} -> {version})", *existing),
                }
                .into());
            }
        }
        self.reader_pins.insert(reader_id, version);
        Ok(())
    }

    // -- Queries ------------------------------------------------------------

    /// One job record.
    pub fn job(&self, job_id: u64) -> Option<&DdlJobRecord> {
        self.jobs.get(&job_id)
    }

    /// One index record.
    pub fn index(&self, table_id: TableId, index_name: &str) -> Option<&DdlIndexRecord> {
        self.indexes.get(&(table_id, index_name.to_owned()))
    }

    /// One table anchor.
    pub fn table_anchor(&self, table_id: TableId) -> Option<&TableAnchor> {
        self.tables.get(&table_id)
    }

    /// The oldest live reader pin, the reclamation gate's watermark.
    pub fn oldest_reader_version(&self) -> Option<MetadataVersion> {
        self.reader_pins.values().copied().min()
    }

    /// The indexes a planner at metadata version `version` may use (spec
    /// step 6): published at or below `version`, and not already `Dropping`
    /// at `version`. The atomic publication flips visibility at exactly one
    /// metadata version.
    pub fn planner_visible_at(
        &self,
        table_id: TableId,
        version: MetadataVersion,
    ) -> Vec<DdlIndexRecord> {
        self.indexes
            .values()
            .filter(|record| {
                record.table_id == table_id
                    && record
                        .publication_version
                        .is_some_and(|publication| publication <= version)
                    && record
                        .dropping_since
                        .is_none_or(|dropping| version < dropping)
            })
            .cloned()
            .collect()
    }

    /// The indexes the planner of the current metadata version may use.
    pub fn planner_visible(&self, table_id: TableId) -> Vec<DdlIndexRecord> {
        self.planner_visible_at(table_id, self.metadata_version)
    }

    /// The index definitions a tablet apply path dual-maintains for
    /// committed writes (spec step 2): every record in `WriteOnly`,
    /// `Backfilling`, `Validating`, or `Public` — hidden from planners but
    /// maintained from `WriteOnly` on. Deterministic name order.
    pub fn write_maintained(&self, table_id: TableId) -> Vec<DdlIndexRecord> {
        self.indexes
            .values()
            .filter(|record| {
                record.table_id == table_id
                    && matches!(
                        record.phase,
                        DdlPhase::WriteOnly
                            | DdlPhase::Backfilling
                            | DdlPhase::Validating
                            | DdlPhase::Public
                    )
            })
            .cloned()
            .collect()
    }
}

// ---------------------------------------------------------------------------
// The tablet seams (spec steps 2-5)
// ---------------------------------------------------------------------------

/// The boxed stream of timestamped committed mutations
/// ([`BackfillKeyspace::deltas_after`]). Unlike the split executor's delta
/// stream, the DDL catch-up needs commit timestamps to define its watermark:
/// `None` values are deletes (tombstones).
pub type DeltaStream<'a> = Box<dyn Iterator<Item = (HlcTimestamp, Key, Option<Vec<u8>>)> + 'a>;

/// The applied keyspace of one tablet replica, as the DDL backfill driver
/// reads it (spec steps 3-5). The engine binding lands with the server wave:
/// the snapshot is the engine's MVCC snapshot mechanics and the deltas are
/// the committed-log stream from the pinned snapshot forward. All methods
/// take `&self`; implementors synchronize internally (the in-memory
/// reference does), so one provider hands out independent handles.
pub trait BackfillKeyspace {
    /// Pins the snapshot at `ts` (spec step 3). Idempotent: re-pinning the
    /// same timestamp returns an equivalent pin; dropping releases it.
    fn pin_snapshot(&self, ts: HlcTimestamp) -> Result<Box<dyn SnapshotPin>, TabletDataError>;

    /// The keyspace contents visible at `ts`, in key order (tombstoned keys
    /// absent).
    fn snapshot_at(&self, ts: HlcTimestamp) -> Result<RecordStream<'_>, TabletDataError>;

    /// Mutations committed after `ts`, oldest first (spec step 4's catch-up
    /// stream). Multiple versions of one key arrive in commit order; a `None`
    /// value is a delete.
    fn deltas_after(&self, ts: HlcTimestamp) -> Result<DeltaStream<'_>, TabletDataError>;
}

/// The hidden-index build sink of one tablet (spec steps 3-4), staged like
/// the engine's snapshot-install idiom: a build is staged beside live state
/// (`begin_build`/`stage_entry`) and installed atomically
/// (`install_staged`), after which catch-up deltas and dual-maintained writes
/// apply (`apply_delta`). Restarting a build discards the staged content, so
/// a resumed tablet build is idempotent.
pub trait HiddenIndexSink {
    /// Starts (or restarts) a staged build of `index`, discarding prior
    /// staged content for it.
    fn begin_build(&mut self, index: &str) -> Result<(), TabletDataError>;

    /// Adds one snapshot entry to the staged build.
    fn stage_entry(&mut self, index: &str, key: &Key, entry: &[u8]) -> Result<(), TabletDataError>;

    /// Atomically installs the staged build as the tablet's hidden
    /// generation of `index`.
    fn install_staged(&mut self, index: &str) -> Result<(), TabletDataError>;

    /// Applies one committed mutation to the installed generation (spec
    /// steps 2 and 4): `Some(entry)` sets, `None` removes. Idempotent —
    /// catch-up sweeps and the apply-path maintainer may both cover one
    /// write.
    fn apply_delta(
        &mut self,
        index: &str,
        key: &Key,
        entry: Option<&[u8]>,
    ) -> Result<(), TabletDataError>;

    /// Drops the generation (and any staged build) of `index`. Idempotent:
    /// rollback and reclamation call this on every tablet of the table.
    fn drop_generation(&mut self, index: &str) -> Result<(), TabletDataError>;

    /// The installed generation's entries (validation and tests).
    fn generation_entries(&self, index: &str) -> Result<BTreeMap<Key, Vec<u8>>, TabletDataError>;

    /// The installed generation's entry count.
    fn entry_count(&self, index: &str) -> Result<u64, TabletDataError> {
        Ok(self.generation_entries(index)?.len() as u64)
    }
}

/// The tablet apply path's dual-maintenance seam (spec step 2). While an
/// index record of the tablet's table sits in `WriteOnly..=Public`, every
/// committed row mutation additionally lands in the hidden (or public) index
/// generation. The engine binding hooks this into the tablet's apply loop;
/// it lands with the server wave.
pub trait ApplySideIndexMaintainer {
    /// Refreshes the maintainer's view: the names of every index the table
    /// currently maintains (`DdlJobStore::write_maintained`). The apply path
    /// calls this whenever its applied metadata view changes.
    fn sync_definitions(&mut self, maintained: Vec<String>) -> Result<(), TabletDataError>;

    /// Dual-maintains one committed row mutation (`None` = delete) into
    /// every installed generation of the synced definitions. A mutation
    /// arriving before its generation is installed is skipped here and
    /// covered by the catch-up sweep instead (the documented handoff).
    fn apply_committed_write(
        &mut self,
        key: &Key,
        row: Option<&[u8]>,
    ) -> Result<(), TabletDataError>;
}

/// The tablet topology and seam registry the [`DdlDriver`] drives. Handles
/// are independent (interior synchronization), so the driver can hold a
/// keyspace and a sink of one tablet at once.
pub trait DdlTabletProvider {
    /// The table's current tablets, in tablet-id order (deterministic).
    fn tablets_of(&self, table: TableId) -> Vec<TabletId>;

    /// The backfill read seam of one tablet.
    fn keyspace(&self, tablet: TabletId) -> Result<Box<dyn BackfillKeyspace>, TabletDataError>;

    /// The hidden-index build seam of one tablet.
    fn sink(&self, tablet: TabletId) -> Result<Box<dyn HiddenIndexSink>, TabletDataError>;

    /// The apply-path dual-maintenance seam of one tablet.
    fn maintainer(
        &self,
        tablet: TabletId,
    ) -> Result<Box<dyn ApplySideIndexMaintainer>, TabletDataError>;
}

/// Derives one index entry from one row (the engine binding supplies real
/// index-key extraction; tests ride [`identity_projection`]).
pub type IndexProjection = Arc<dyn Fn(&DdlIndexRecord, &Key, &[u8]) -> Vec<u8> + Send + Sync>;

/// The default projection: the entry is the row's value bytes (the hidden
/// index is a full row copy). Both the backfill path and the from-scratch
/// comparison use it, so validation compares the two paths' convergence.
pub fn identity_projection() -> IndexProjection {
    Arc::new(|_index, _key, value| value.to_vec())
}

// ---------------------------------------------------------------------------
// In-memory reference implementations
// ---------------------------------------------------------------------------

/// The in-memory version chain behind [`InMemoryDdlKeyspace`]: per key, the
/// committed versions (timestamp, put-or-tombstone) in ascending order.
type VersionedRows = BTreeMap<Key, Vec<(HlcTimestamp, Option<Vec<u8>>)>>;

/// The reference [`BackfillKeyspace`]: a shared in-memory multi-version map
/// with tombstones. Versions are kept per key so `snapshot_at` /
/// `deltas_after` split the timeline exactly at a timestamp; clones share
/// state, so a "crashed" driver's writes survive into the resumed one.
#[derive(Clone, Default)]
pub struct InMemoryDdlKeyspace {
    state: Arc<Mutex<VersionedRows>>,
}

impl InMemoryDdlKeyspace {
    /// An empty keyspace.
    pub fn new() -> Self {
        Self::default()
    }

    /// Commits one put of `key` at `ts`.
    pub fn insert(&self, key: Key, ts: HlcTimestamp, value: Vec<u8>) {
        let mut rows = self.state.lock().expect("keyspace lock poisoned");
        let chain = rows.entry(key).or_default();
        chain.push((ts, Some(value)));
        chain.sort_by_key(|(version, _)| *version);
    }

    /// Commits one delete of `key` at `ts`.
    pub fn delete(&self, key: Key, ts: HlcTimestamp) {
        let mut rows = self.state.lock().expect("keyspace lock poisoned");
        let chain = rows.entry(key).or_default();
        chain.push((ts, None));
        chain.sort_by_key(|(version, _)| *version);
    }

    /// Every key's newest live value at or below `ts` (assertion helper).
    pub fn rows_at(&self, ts: HlcTimestamp) -> BTreeMap<Key, Vec<u8>> {
        let rows = self.state.lock().expect("keyspace lock poisoned");
        rows.iter()
            .filter_map(|(key, chain)| {
                let (_, value) = chain.iter().rfind(|(version, _)| *version <= ts)?;
                value.clone().map(|value| (key.clone(), value))
            })
            .collect()
    }
}

impl BackfillKeyspace for InMemoryDdlKeyspace {
    fn pin_snapshot(&self, ts: HlcTimestamp) -> Result<Box<dyn SnapshotPin>, TabletDataError> {
        Ok(Box::new(DdlSnapshotPin { ts }))
    }

    fn snapshot_at(&self, ts: HlcTimestamp) -> Result<RecordStream<'_>, TabletDataError> {
        Ok(Box::new(self.rows_at(ts).into_iter()))
    }

    fn deltas_after(&self, ts: HlcTimestamp) -> Result<DeltaStream<'_>, TabletDataError> {
        let rows = self.state.lock().expect("keyspace lock poisoned");
        let mut deltas: Vec<(HlcTimestamp, Key, Option<Vec<u8>>)> = Vec::new();
        for (key, chain) in rows.iter() {
            for (version, value) in chain {
                if *version > ts {
                    deltas.push((*version, key.clone(), value.clone()));
                }
            }
        }
        deltas.sort_by(|(left_ts, left_key, _), (right_ts, right_key, _)| {
            (left_ts, left_key).cmp(&(right_ts, right_key))
        });
        Ok(Box::new(deltas.into_iter()))
    }
}

/// A [`InMemoryDdlKeyspace`] snapshot pin. The in-memory keyspace keeps its
/// whole version chain, so the pin is a pure timestamp record (a real engine
/// releases the pinned read generation on drop).
struct DdlSnapshotPin {
    ts: HlcTimestamp,
}

impl SnapshotPin for DdlSnapshotPin {
    fn pinned_at(&self) -> HlcTimestamp {
        self.ts
    }
}

/// The reference [`HiddenIndexSink`] + [`ApplySideIndexMaintainer`]: per
/// tablet, staged builds beside installed generations, with the apply-path
/// dual maintenance writing into installed generations only. Clones share
/// state (the same idiom as `MapChildSink`).
#[derive(Clone, Default)]
pub struct InMemoryTabletIndexes {
    state: Arc<Mutex<IndexesState>>,
}

#[derive(Default)]
struct IndexesState {
    /// Synced maintained definition names (`sync_definitions`).
    maintained: Vec<String>,
    /// Staged builds by index name (beside live state).
    staged: BTreeMap<String, BTreeMap<Key, Vec<u8>>>,
    /// Installed generations by index name.
    generations: BTreeMap<String, BTreeMap<Key, Vec<u8>>>,
    /// Total `begin_build` calls (restart observability for tests).
    begin_builds: usize,
}

impl InMemoryTabletIndexes {
    /// An empty tablet index set.
    pub fn new() -> Self {
        Self::default()
    }

    /// The installed entries of one generation, empty when absent (assertion
    /// helper).
    pub fn entries(&self, index: &str) -> BTreeMap<Key, Vec<u8>> {
        self.state
            .lock()
            .expect("indexes lock poisoned")
            .generations
            .get(index)
            .cloned()
            .unwrap_or_default()
    }

    /// How often a build was (re)started (assertion helper for resume
    /// tests: an already-finished tablet is never rebuilt).
    pub fn begin_build_count(&self) -> usize {
        self.state
            .lock()
            .expect("indexes lock poisoned")
            .begin_builds
    }

    /// Whether `index` has an installed generation.
    pub fn is_installed(&self, index: &str) -> bool {
        self.state
            .lock()
            .expect("indexes lock poisoned")
            .generations
            .contains_key(index)
    }

    /// The currently synced maintained definition names.
    pub fn maintained(&self) -> Vec<String> {
        self.state
            .lock()
            .expect("indexes lock poisoned")
            .maintained
            .clone()
    }

    /// Seeds staged content without installing it (simulates a driver crash
    /// mid-build; the resumed driver's `begin_build` must discard it).
    #[cfg(test)]
    pub fn seed_staged(&self, index: &str, key: Key, entry: Vec<u8>) {
        self.state
            .lock()
            .expect("indexes lock poisoned")
            .staged
            .entry(index.to_owned())
            .or_default()
            .insert(key, entry);
    }
}

impl HiddenIndexSink for InMemoryTabletIndexes {
    fn begin_build(&mut self, index: &str) -> Result<(), TabletDataError> {
        let mut state = self.state.lock().expect("indexes lock poisoned");
        state.staged.insert(index.to_owned(), BTreeMap::new());
        state.begin_builds += 1;
        Ok(())
    }

    fn stage_entry(&mut self, index: &str, key: &Key, entry: &[u8]) -> Result<(), TabletDataError> {
        let mut state = self.state.lock().expect("indexes lock poisoned");
        let Some(staged) = state.staged.get_mut(index) else {
            return Err(TabletDataError::NoStagedBuild);
        };
        staged.insert(key.clone(), entry.to_vec());
        Ok(())
    }

    fn install_staged(&mut self, index: &str) -> Result<(), TabletDataError> {
        let mut state = self.state.lock().expect("indexes lock poisoned");
        let Some(staged) = state.staged.remove(index) else {
            return Err(TabletDataError::NoStagedBuild);
        };
        state.generations.insert(index.to_owned(), staged);
        Ok(())
    }

    fn apply_delta(
        &mut self,
        index: &str,
        key: &Key,
        entry: Option<&[u8]>,
    ) -> Result<(), TabletDataError> {
        let mut state = self.state.lock().expect("indexes lock poisoned");
        let Some(generation) = state.generations.get_mut(index) else {
            return Err(TabletDataError::Sink(format!(
                "generation `{index}` is not installed"
            )));
        };
        match entry {
            Some(entry) => {
                generation.insert(key.clone(), entry.to_vec());
            }
            None => {
                generation.remove(key);
            }
        }
        Ok(())
    }

    fn drop_generation(&mut self, index: &str) -> Result<(), TabletDataError> {
        let mut state = self.state.lock().expect("indexes lock poisoned");
        state.generations.remove(index);
        state.staged.remove(index);
        Ok(())
    }

    fn generation_entries(&self, index: &str) -> Result<BTreeMap<Key, Vec<u8>>, TabletDataError> {
        Ok(self.entries(index))
    }
}

impl ApplySideIndexMaintainer for InMemoryTabletIndexes {
    fn sync_definitions(&mut self, maintained: Vec<String>) -> Result<(), TabletDataError> {
        self.state.lock().expect("indexes lock poisoned").maintained = maintained;
        Ok(())
    }

    fn apply_committed_write(
        &mut self,
        key: &Key,
        row: Option<&[u8]>,
    ) -> Result<(), TabletDataError> {
        let mut state = self.state.lock().expect("indexes lock poisoned");
        for index in state.maintained.clone() {
            // Writes before the generation install are covered by the
            // catch-up sweep (the documented handoff), never lost.
            if let Some(generation) = state.generations.get_mut(&index) {
                match row {
                    Some(row) => {
                        generation.insert(key.clone(), row.to_vec());
                    }
                    None => {
                        generation.remove(key);
                    }
                }
            }
        }
        Ok(())
    }
}

/// One in-memory tablet: its keyspace plus its hidden-index state.
struct InMemoryTablet {
    table_id: TableId,
    keyspace: InMemoryDdlKeyspace,
    indexes: InMemoryTabletIndexes,
}

/// The reference [`DdlTabletProvider`]: a shared in-memory tablet set.
/// `commit_write` models the tablet apply path (spec step 2): the mutation
/// lands in the applied keyspace AND flows through the dual-maintenance seam
/// in one call.
#[derive(Clone, Default)]
pub struct InMemoryDdlTablets {
    tablets: Arc<Mutex<BTreeMap<TabletId, InMemoryTablet>>>,
}

impl InMemoryDdlTablets {
    /// An empty tablet set.
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds one tablet of `table`.
    pub fn add_tablet(&self, tablet: TabletId, table: TableId) {
        self.tablets
            .lock()
            .expect("tablets lock poisoned")
            .entry(tablet)
            .or_insert_with(|| InMemoryTablet {
                table_id: table,
                keyspace: InMemoryDdlKeyspace::new(),
                indexes: InMemoryTabletIndexes::new(),
            });
    }

    /// Removes one tablet (simulates a split/merge topology change; the
    /// driver forgets its progress cursor on the next step).
    #[cfg(test)]
    pub fn remove_tablet(&self, tablet: TabletId) {
        self.tablets
            .lock()
            .expect("tablets lock poisoned")
            .remove(&tablet);
    }

    /// The keyspace handle of one tablet (test seeding).
    pub fn keyspace_handle(&self, tablet: TabletId) -> InMemoryDdlKeyspace {
        self.tablets
            .lock()
            .expect("tablets lock poisoned")
            .get(&tablet)
            .expect("unknown tablet")
            .keyspace
            .clone()
    }

    /// The index-state handle of one tablet (test assertions).
    pub fn indexes_handle(&self, tablet: TabletId) -> InMemoryTabletIndexes {
        self.tablets
            .lock()
            .expect("tablets lock poisoned")
            .get(&tablet)
            .expect("unknown tablet")
            .indexes
            .clone()
    }

    /// Models one committed write through the tablet apply path: the row
    /// mutation lands in the applied keyspace and is dual-maintained into
    /// every installed hidden/public generation (spec step 2). `None` deletes.
    pub fn commit_write(
        &self,
        tablet: TabletId,
        key: Key,
        ts: HlcTimestamp,
        row: Option<Vec<u8>>,
    ) -> Result<(), TabletDataError> {
        let (keyspace, mut indexes) = {
            let tablets = self.tablets.lock().expect("tablets lock poisoned");
            let tablet = tablets.get(&tablet).expect("unknown tablet");
            (tablet.keyspace.clone(), tablet.indexes.clone())
        };
        match row {
            Some(row) => {
                keyspace.insert(key.clone(), ts, row.clone());
                indexes.apply_committed_write(&key, Some(&row))
            }
            None => {
                keyspace.delete(key.clone(), ts);
                indexes.apply_committed_write(&key, None)
            }
        }
    }
}

impl DdlTabletProvider for InMemoryDdlTablets {
    fn tablets_of(&self, table: TableId) -> Vec<TabletId> {
        self.tablets
            .lock()
            .expect("tablets lock poisoned")
            .iter()
            .filter(|(_, tablet)| tablet.table_id == table)
            .map(|(tablet_id, _)| *tablet_id)
            .collect()
    }

    fn keyspace(&self, tablet: TabletId) -> Result<Box<dyn BackfillKeyspace>, TabletDataError> {
        let tablets = self.tablets.lock().expect("tablets lock poisoned");
        let Some(tablet) = tablets.get(&tablet) else {
            return Err(TabletDataError::Keyspace(format!(
                "unknown tablet {tablet}"
            )));
        };
        Ok(Box::new(tablet.keyspace.clone()))
    }

    fn sink(&self, tablet: TabletId) -> Result<Box<dyn HiddenIndexSink>, TabletDataError> {
        let tablets = self.tablets.lock().expect("tablets lock poisoned");
        let Some(tablet) = tablets.get(&tablet) else {
            return Err(TabletDataError::Sink(format!("unknown tablet {tablet}")));
        };
        Ok(Box::new(tablet.indexes.clone()))
    }

    fn maintainer(
        &self,
        tablet: TabletId,
    ) -> Result<Box<dyn ApplySideIndexMaintainer>, TabletDataError> {
        let tablets = self.tablets.lock().expect("tablets lock poisoned");
        let Some(tablet) = tablets.get(&tablet) else {
            return Err(TabletDataError::Sink(format!("unknown tablet {tablet}")));
        };
        Ok(Box::new(tablet.indexes.clone()))
    }
}

// ---------------------------------------------------------------------------
// The driver
// ---------------------------------------------------------------------------

/// The outcome of one [`DdlDriver::drive_job`] call.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DriveOutcome {
    /// The job reached `Succeeded` (`Public` for additions, `Dropping` for
    /// drops).
    Completed,
    /// The job is paused; a later drive resumes from the durable cursors.
    Parked,
    /// The job was cancelled (or crashed mid-rollback) and the unwind ran:
    /// hidden generations dropped, unpublished records removed.
    RolledBack,
}

/// One atomic step of [`DdlDriver::step_job`]; tests drive jobs stepwise to
/// interleave writes, pauses, and crashes between protocol actions.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum DriveStep {
    /// `Pending -> Running`.
    Admitted,
    /// One [`DdlPhase`] edge applied.
    PhaseAdvanced {
        /// Previous phase.
        from: DdlPhase,
        /// New phase.
        to: DdlPhase,
    },
    /// One tablet's snapshot backfill + catch-up completed and was recorded.
    TabletBackfilled {
        /// The tablet.
        tablet: TabletId,
    },
    /// One tablet validated.
    TabletValidated {
        /// The tablet.
        tablet: TabletId,
    },
    /// The atomic publication landed at this metadata version.
    Published {
        /// The publication metadata version.
        version: MetadataVersion,
    },
    /// The job is paused.
    Parked,
    /// The job's rollback ran.
    RolledBack,
    /// The job is in a terminal state.
    Terminal,
}

/// The outcome of one [`DdlDriver::reclaim_index`] call (spec step 7).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ReclaimOutcome {
    /// The record was removed; per-tablet generations were dropped.
    Reclaimed,
    /// Readers below the retirement version still pin the metadata; retry
    /// once they drain.
    Blocked {
        /// Oldest live reader pin, when any.
        oldest_reader: Option<MetadataVersion>,
        /// The retirement version every reader must have reached.
        required: MetadataVersion,
    },
}

/// The operator-facing status of one job (spec section 12.11 admin surface).
#[derive(Clone, Debug, PartialEq)]
pub struct DdlJobStatus {
    /// The replicated record.
    pub record: DdlJobRecord,
    /// Tablets with a registered progress cursor.
    pub tablets_registered: usize,
    /// Tablets that completed snapshot backfill + catch-up.
    pub tablets_caught_up: usize,
    /// Tablets that validated.
    pub tablets_validated: usize,
    /// Total rows scanned from pinned snapshots.
    pub rows_backfilled: u64,
}

/// The distributed DDL driver (spec section 12.11): submits jobs, drives
/// them through the phase machine against the replicated [`DdlJobStore`],
/// and runs the per-tablet backfill/catch-up/validation through the tablet
/// seams. One driver per cluster admin surface; the store's applied commands
/// are the only progress authority, so a replacement driver over the same
/// store resumes exactly.
pub struct DdlDriver<P: DdlTabletProvider> {
    store: Arc<Mutex<DdlJobStore>>,
    provider: P,
    clock: HlcClock,
    projection: IndexProjection,
}

impl<P: DdlTabletProvider> DdlDriver<P> {
    /// A driver over `store` and `provider` with the [`identity_projection`].
    pub fn new(store: Arc<Mutex<DdlJobStore>>, provider: P) -> Self {
        Self::with_projection(store, provider, identity_projection())
    }

    /// A driver with an explicit row-to-entry projection.
    pub fn with_projection(
        store: Arc<Mutex<DdlJobStore>>,
        provider: P,
        projection: IndexProjection,
    ) -> Self {
        Self {
            store,
            provider,
            clock: HlcClock::new(0, Duration::from_secs(30)),
            projection,
        }
    }

    /// The shared store handle (tests and the raft binding).
    pub fn store_handle(&self) -> Arc<Mutex<DdlJobStore>> {
        self.store.clone()
    }

    // -- Admin surface: submissions ----------------------------------------

    /// Submits an online index build (spec steps 1-6). The job pins
    /// `source_schema_version`; a stale schema is refused with the structured
    /// [`DdlRejection::SchemaVersionMismatch`] retry.
    pub fn submit_add_index(
        &self,
        database_id: DatabaseId,
        table_id: TableId,
        index_name: impl Into<String>,
        spec: serde_json::Value,
        source_schema_version: SchemaVersion,
    ) -> Result<u64, DdlError> {
        let created_at = self.clock.now()?;
        let job = DdlJobRecord {
            job_id: self.peek_next_job_id(),
            database_id,
            table_id,
            kind: DdlJobKind::AddIndex,
            state: SchemaJobState::Pending,
            phase: DdlPhase::Pending,
            definition: DdlDefinition::AddIndex {
                index_name: index_name.into(),
                spec,
            },
            source_schema_version,
            created_at,
            updated_at: created_at,
            pinned_snapshot: None,
            tablet_progress: BTreeMap::new(),
            error: None,
            metadata_version: MetadataVersion::ZERO,
        };
        self.apply(DdlCommand::SubmitJob { job: job.clone() })?;
        Ok(job.job_id)
    }

    /// Submits an online index drop (`Public -> Dropping -> reclaim`).
    pub fn submit_drop_index(
        &self,
        database_id: DatabaseId,
        table_id: TableId,
        index_name: impl Into<String>,
        source_schema_version: SchemaVersion,
    ) -> Result<u64, DdlError> {
        let created_at = self.clock.now()?;
        let job = DdlJobRecord {
            job_id: self.peek_next_job_id(),
            database_id,
            table_id,
            kind: DdlJobKind::DropIndex,
            state: SchemaJobState::Pending,
            phase: DdlPhase::Public,
            definition: DdlDefinition::DropIndex {
                index_name: index_name.into(),
            },
            source_schema_version,
            created_at,
            updated_at: created_at,
            pinned_snapshot: None,
            tablet_progress: BTreeMap::new(),
            error: None,
            metadata_version: MetadataVersion::ZERO,
        };
        self.apply(DdlCommand::SubmitJob { job: job.clone() })?;
        Ok(job.job_id)
    }

    /// Submits a schema alteration. Publication advances the table anchor to
    /// `source_schema_version + 1` and swaps the schema document atomically.
    pub fn submit_alter_schema(
        &self,
        database_id: DatabaseId,
        table_id: TableId,
        target: serde_json::Value,
        source_schema_version: SchemaVersion,
    ) -> Result<u64, DdlError> {
        let created_at = self.clock.now()?;
        let job = DdlJobRecord {
            job_id: self.peek_next_job_id(),
            database_id,
            table_id,
            kind: DdlJobKind::AlterSchema,
            state: SchemaJobState::Pending,
            phase: DdlPhase::Pending,
            definition: DdlDefinition::AlterSchema { target },
            source_schema_version,
            created_at,
            updated_at: created_at,
            pinned_snapshot: None,
            tablet_progress: BTreeMap::new(),
            error: None,
            metadata_version: MetadataVersion::ZERO,
        };
        self.apply(DdlCommand::SubmitJob { job: job.clone() })?;
        Ok(job.job_id)
    }

    // -- Admin surface: job control -----------------------------------------

    /// Parks a `Running` job at the next tablet boundary (graph-enforced).
    /// Refuses from every other state (a double pause is an operator error,
    /// not an idempotent replay).
    pub fn pause_job(&self, job_id: u64) -> Result<(), DdlError> {
        let record = self.job_record(job_id)?;
        if record.state != SchemaJobState::Running {
            return Err(DdlError::Rejection(DdlRejection::JobNotRunning {
                job_id,
                state: record.state,
            }));
        }
        self.apply(DdlCommand::SetJobState {
            job_id,
            state: SchemaJobState::Paused,
            updated_at: self.clock.now()?,
            error: None,
            expected_version: Some(record.metadata_version),
        })
    }

    /// Requeues a `Paused` job; the next drive admits and resumes it from the
    /// durable per-tablet cursors. Refuses from every other state.
    pub fn resume_job(&self, job_id: u64) -> Result<(), DdlError> {
        let record = self.job_record(job_id)?;
        if record.state != SchemaJobState::Paused {
            return Err(DdlError::Rejection(DdlRejection::Meta(
                MetaRejectionReason::Conflict {
                    resource: format!("DDL job {job_id}"),
                    reason: format!("cannot resume from {:?}", record.state),
                },
            )));
        }
        self.apply(DdlCommand::SetJobState {
            job_id,
            state: SchemaJobState::Pending,
            updated_at: self.clock.now()?,
            error: None,
            expected_version: Some(record.metadata_version),
        })
    }

    /// Requests cancellation. The unwind (hidden generations dropped,
    /// unpublished records removed) runs on the next drive step. Terminal
    /// jobs refuse cancellation.
    pub fn cancel_job(&self, job_id: u64) -> Result<(), DdlError> {
        let record = self.job_record(job_id)?;
        if record.state.is_terminal() {
            return Err(DdlError::Rejection(DdlRejection::Meta(
                MetaRejectionReason::Conflict {
                    resource: format!("DDL job {job_id}"),
                    reason: format!("terminal state {:?}", record.state),
                },
            )));
        }
        self.apply(DdlCommand::SetJobState {
            job_id,
            state: SchemaJobState::Cancelling,
            updated_at: self.clock.now()?,
            error: None,
            expected_version: Some(record.metadata_version),
        })
    }

    /// The operator-facing status of one job.
    pub fn job_status(&self, job_id: u64) -> Option<DdlJobStatus> {
        let record = self
            .store
            .lock()
            .expect("store lock poisoned")
            .job(job_id)?
            .clone();
        let tablets_registered = record.tablet_progress.len();
        let tablets_caught_up = record
            .tablet_progress
            .values()
            .filter(|progress| progress.stage >= TabletDdlStage::CaughtUp)
            .count();
        let tablets_validated = record
            .tablet_progress
            .values()
            .filter(|progress| progress.stage == TabletDdlStage::Validated)
            .count();
        let rows_backfilled = record
            .tablet_progress
            .values()
            .map(|progress| progress.rows_scanned)
            .sum();
        Some(DdlJobStatus {
            record,
            tablets_registered,
            tablets_caught_up,
            tablets_validated,
            rows_backfilled,
        })
    }

    /// The aggregate validation report of one job (spec step 5), once every
    /// registered tablet reported.
    pub fn validation_report(&self, job_id: u64) -> Option<JobValidationReport> {
        let record = self
            .store
            .lock()
            .expect("store lock poisoned")
            .job(job_id)?
            .clone();
        let tablets: Vec<TabletValidationReport> = record
            .tablet_progress
            .values()
            .filter_map(|progress| progress.validation.clone())
            .collect();
        if tablets.is_empty() {
            return None;
        }
        let total_expected = tablets.iter().map(|report| report.expected_rows).sum();
        let total_actual = tablets.iter().map(|report| report.actual_rows).sum();
        let passed = tablets.iter().all(TabletValidationReport::passed);
        Some(JobValidationReport {
            job_id,
            tablets,
            total_expected,
            total_actual,
            passed,
        })
    }

    // -- Admin surface: reclamation (spec step 7) ---------------------------

    /// Pins a reader at a metadata version (its plans may reference every
    /// element visible there). The gateway wave owns real reader tracking.
    pub fn pin_reader(&self, reader_id: u64, version: MetadataVersion) -> Result<(), DdlError> {
        self.apply(DdlCommand::PinReader { reader_id, version })
    }

    /// Releases one reader pin.
    pub fn release_reader(&self, reader_id: u64) -> Result<(), DdlError> {
        self.apply(DdlCommand::ReleaseReader { reader_id })
    }

    /// Runs the reclamation pass for one dropped index: removes the record
    /// once no live reader pins a metadata version below its retirement
    /// version, and drops the per-tablet generations.
    pub fn reclaim_index(
        &self,
        table_id: TableId,
        index_name: &str,
    ) -> Result<ReclaimOutcome, DdlError> {
        match self.apply(DdlCommand::ReclaimIndex {
            table_id,
            index_name: index_name.to_owned(),
        }) {
            Ok(()) => {
                for tablet in self.provider.tablets_of(table_id) {
                    self.provider.sink(tablet)?.drop_generation(index_name)?;
                }
                Ok(ReclaimOutcome::Reclaimed)
            }
            Err(DdlError::Rejection(DdlRejection::ReclaimBlocked {
                oldest_reader,
                required,
                ..
            })) => Ok(ReclaimOutcome::Blocked {
                oldest_reader,
                required,
            }),
            Err(error) => Err(error),
        }
    }

    // -- Driving ------------------------------------------------------------

    /// Drives one job until it terminates, parks, or fails. Resumable: every
    /// step is an applied store command, so a fresh driver over the same
    /// store continues exactly where a crashed one stopped.
    pub fn drive_job(&self, job_id: u64) -> Result<DriveOutcome, DdlError> {
        loop {
            match self.step_job(job_id)? {
                DriveStep::Parked => return Ok(DriveOutcome::Parked),
                DriveStep::RolledBack => return Ok(DriveOutcome::RolledBack),
                DriveStep::Terminal => {
                    let state = self.job_record(job_id)?.state;
                    return Ok(match state {
                        SchemaJobState::Succeeded => DriveOutcome::Completed,
                        _ => DriveOutcome::RolledBack,
                    });
                }
                DriveStep::Admitted
                | DriveStep::PhaseAdvanced { .. }
                | DriveStep::TabletBackfilled { .. }
                | DriveStep::TabletValidated { .. }
                | DriveStep::Published { .. } => {}
            }
        }
    }

    /// Performs one atomic protocol action of one job. Every mutating step
    /// is exactly one applied store command (plus the seam work it records).
    fn step_job(&self, job_id: u64) -> Result<DriveStep, DdlError> {
        let job = self.job_record(job_id)?;
        match job.state {
            SchemaJobState::Pending => {
                self.set_state(job_id, SchemaJobState::Running, None)?;
                return Ok(DriveStep::Admitted);
            }
            SchemaJobState::Paused => return Ok(DriveStep::Parked),
            SchemaJobState::Cancelling | SchemaJobState::RollingBack => {
                self.rollback(&job, "cancelled by operator")?;
                return Ok(DriveStep::RolledBack);
            }
            SchemaJobState::Failed | SchemaJobState::Succeeded => {
                return Ok(DriveStep::Terminal);
            }
            SchemaJobState::Running => {}
        }
        match job.phase {
            DdlPhase::Pending => {
                // Spec step 1: the definition is replicated WriteOnly; from
                // here the tablet apply path dual-maintains it (step 2).
                self.advance_phase(job_id, DdlPhase::WriteOnly, None)?;
                self.sync_maintainers(job.table_id)?;
                Ok(DriveStep::PhaseAdvanced {
                    from: DdlPhase::Pending,
                    to: DdlPhase::WriteOnly,
                })
            }
            DdlPhase::WriteOnly => {
                // Spec step 3: stamp the job-wide pinned snapshot.
                let pin = self.clock.now()?;
                self.advance_phase(job_id, DdlPhase::Backfilling, Some(pin))?;
                Ok(DriveStep::PhaseAdvanced {
                    from: DdlPhase::WriteOnly,
                    to: DdlPhase::Backfilling,
                })
            }
            DdlPhase::Backfilling => {
                let tablets = self.provider.tablets_of(job.table_id);
                for tablet in &tablets {
                    if !job.tablet_progress.contains_key(tablet) {
                        self.update_progress(job_id, TabletDdlProgress::pending(*tablet))?;
                    }
                }
                let mut job = self.job_record(job_id)?;
                let departed: Vec<TabletId> = job
                    .tablet_progress
                    .keys()
                    .filter(|tablet| !tablets.contains(tablet))
                    .copied()
                    .collect();
                for tablet in departed {
                    self.apply(DdlCommand::ForgetTabletProgress {
                        job_id,
                        tablet_id: tablet,
                    })?;
                }
                job = self.job_record(job_id)?;
                let next = tablets.iter().copied().find(|tablet| {
                    job.tablet_progress
                        .get(tablet)
                        .is_none_or(|progress| progress.stage < TabletDdlStage::CaughtUp)
                });
                match next {
                    Some(tablet) => {
                        let progress = job.tablet_progress.get(&tablet);
                        if progress.is_none_or(|p| p.stage < TabletDdlStage::Backfilling) {
                            let mut marker = TabletDdlProgress::pending(tablet);
                            marker.stage = TabletDdlStage::Backfilling;
                            self.update_progress(job_id, marker)?;
                            job = self.job_record(job_id)?;
                        }
                        let progress = self.backfill_tablet(&job, tablet)?;
                        self.update_progress(job_id, progress)?;
                        Ok(DriveStep::TabletBackfilled { tablet })
                    }
                    None => {
                        self.advance_phase(job_id, DdlPhase::Validating, None)?;
                        Ok(DriveStep::PhaseAdvanced {
                            from: DdlPhase::Backfilling,
                            to: DdlPhase::Validating,
                        })
                    }
                }
            }
            DdlPhase::Validating => {
                // Spec step 5: validate every backfilled tablet (the progress
                // map is the tablet set the job backfilled; tablets appearing
                // mid-Validating are the split/merge follow-up's concern).
                let next = job
                    .tablet_progress
                    .values()
                    .find(|progress| progress.stage < TabletDdlStage::Validated)
                    .map(|progress| progress.tablet_id);
                match next {
                    Some(tablet) => {
                        let report = self.validate_tablet(&job, tablet)?;
                        let passed = report.passed();
                        self.apply(DdlCommand::ReportTabletValidation {
                            job_id,
                            report: report.clone(),
                        })?;
                        if !passed {
                            let reason = format!(
                                "tablet {tablet}: expected {} rows, built {} rows",
                                report.expected_rows, report.actual_rows
                            );
                            self.rollback(&job, &reason)?;
                            return Err(DdlError::ValidationFailed { job_id, reason });
                        }
                        Ok(DriveStep::TabletValidated { tablet })
                    }
                    None => {
                        // Spec step 6: ONE atomic command flips the element
                        // Public at this metadata version.
                        let published_at = self.clock.now()?;
                        match self.apply(DdlCommand::PublishJob {
                            job_id,
                            published_at,
                        }) {
                            Ok(()) => Ok(DriveStep::Published {
                                version: self
                                    .store
                                    .lock()
                                    .expect("store lock poisoned")
                                    .metadata_version,
                            }),
                            Err(DdlError::Rejection(
                                reason @ DdlRejection::SchemaVersionMismatch { .. },
                            )) => {
                                self.rollback(&job, &reason.to_string())?;
                                Err(DdlError::Rejection(reason))
                            }
                            Err(error) => Err(error),
                        }
                    }
                }
            }
            DdlPhase::Public => match job.kind {
                // Spec drop path: one command flips the element Dropping —
                // writes stop maintaining it, planners stop using it.
                DdlJobKind::DropIndex => {
                    self.advance_phase(job_id, DdlPhase::Dropping, None)?;
                    self.sync_maintainers(job.table_id)?;
                    Ok(DriveStep::PhaseAdvanced {
                        from: DdlPhase::Public,
                        to: DdlPhase::Dropping,
                    })
                }
                DdlJobKind::AddIndex | DdlJobKind::AlterSchema => Ok(DriveStep::Terminal),
            },
            DdlPhase::Dropping => {
                self.set_state(job_id, SchemaJobState::Succeeded, None)?;
                Ok(DriveStep::Terminal)
            }
        }
    }

    /// Spec steps 3-4 for one tablet: build the hidden generation from the
    /// pinned snapshot, install it, then sweep committed deltas forward until
    /// the stream drains. Returns the durable cursor.
    fn backfill_tablet(
        &self,
        job: &DdlJobRecord,
        tablet: TabletId,
    ) -> Result<TabletDdlProgress, DdlError> {
        let pin = job
            .pinned_snapshot
            .ok_or(DdlRejection::Meta(MetaRejectionReason::Invalid {
                reason: format!("job {} entered Backfilling without a pin", job.job_id),
            }))?;
        let keyspace = self.provider.keyspace(tablet)?;
        let _pin = keyspace.pin_snapshot(pin)?;
        let mut rows_scanned = 0_u64;
        match &job.definition {
            DdlDefinition::AddIndex { index_name, .. } => {
                let mut sink = self.provider.sink(tablet)?;
                let record = self.index_record(job, index_name)?;
                sink.begin_build(index_name)?;
                for (key, value) in keyspace.snapshot_at(pin)? {
                    let entry = (self.projection)(&record, &key, &value);
                    sink.stage_entry(index_name, &key, &entry)?;
                    rows_scanned += 1;
                }
                sink.install_staged(index_name)?;
            }
            DdlDefinition::AlterSchema { .. } => {
                // The engine's column backfill lands with the server wave;
                // this wave scans the pinned snapshot per tablet so the phase
                // machine and resume cursors carry real per-tablet progress.
                for (_key, _value) in keyspace.snapshot_at(pin)? {
                    rows_scanned += 1;
                }
            }
            DdlDefinition::DropIndex { .. } => {
                return Err(DdlRejection::Meta(MetaRejectionReason::Invalid {
                    reason: "drop jobs never backfill".to_owned(),
                })
                .into());
            }
        }
        let watermark = self.catch_up_tablet(job, tablet, pin)?;
        Ok(TabletDdlProgress {
            tablet_id: tablet,
            stage: TabletDdlStage::CaughtUp,
            rows_scanned,
            caught_up_through: Some(watermark),
            validation: None,
        })
    }

    /// Spec step 4: sweeps committed deltas from `from` forward until a sweep
    /// applies nothing. Each non-empty sweep strictly advances the watermark,
    /// so the loop terminates; writes racing the sweep are picked up by the
    /// next one (or by the apply-path maintainer past the final watermark).
    fn catch_up_tablet(
        &self,
        job: &DdlJobRecord,
        tablet: TabletId,
        from: HlcTimestamp,
    ) -> Result<HlcTimestamp, DdlError> {
        let keyspace = self.provider.keyspace(tablet)?;
        let mut sink = match &job.definition {
            DdlDefinition::AddIndex { index_name, .. } => {
                Some((self.provider.sink(tablet)?, index_name.clone()))
            }
            _ => None,
        };
        let mut watermark = from;
        loop {
            let mut saw_any = false;
            let mut max_ts = watermark;
            for (ts, key, value) in keyspace.deltas_after(watermark)? {
                saw_any = true;
                max_ts = max_ts.max(ts);
                if let Some((sink, index_name)) = &mut sink {
                    let entry = match &value {
                        Some(value) => {
                            let record = self.index_record(job, index_name)?;
                            Some((self.projection)(&record, &key, value))
                        }
                        None => None,
                    };
                    sink.apply_delta(index_name, &key, entry.as_deref())?;
                }
            }
            if !saw_any {
                return Ok(watermark);
            }
            watermark = max_ts;
        }
    }

    /// Spec step 5 for one tablet: a final catch-up sweep, then the built
    /// generation against a from-scratch build at the same watermark.
    fn validate_tablet(
        &self,
        job: &DdlJobRecord,
        tablet: TabletId,
    ) -> Result<TabletValidationReport, DdlError> {
        let pin = job.pinned_snapshot.expect("Validating implies a pin");
        let from = job
            .tablet_progress
            .get(&tablet)
            .and_then(|progress| progress.caught_up_through)
            .unwrap_or(pin);
        let watermark = self.catch_up_tablet(job, tablet, from)?;
        let keyspace = self.provider.keyspace(tablet)?;
        let expected: BTreeMap<Key, Vec<u8>> = match &job.definition {
            DdlDefinition::AddIndex { index_name, .. } => {
                let record = self.index_record(job, index_name)?;
                keyspace
                    .snapshot_at(watermark)?
                    .map(|(key, value)| {
                        let entry = (self.projection)(&record, &key, &value);
                        (key, entry)
                    })
                    .collect()
            }
            // AlterSchema validates scan completeness at the watermark until
            // the engine's column backfill lands (documented module scope).
            _ => keyspace.snapshot_at(watermark)?.collect(),
        };
        let actual = match &job.definition {
            DdlDefinition::AddIndex { index_name, .. } => {
                self.provider.sink(tablet)?.generation_entries(index_name)?
            }
            _ => expected.clone(),
        };
        Ok(TabletValidationReport {
            tablet_id: tablet,
            watermark,
            expected_rows: expected.len() as u64,
            actual_rows: actual.len() as u64,
            expected_checksum: generation_checksum(&expected),
            actual_checksum: generation_checksum(&actual),
        })
    }

    /// The unwind for cancellation and validation failure: drop the hidden
    /// generations on every tablet, remove the unpublished index record, and
    /// fail the job. Idempotent; a `RollingBack` job (crash mid-rollback)
    /// re-enters here.
    fn rollback(&self, job: &DdlJobRecord, reason: &str) -> Result<(), DdlError> {
        let current = self.job_record(job.job_id)?;
        match current.state {
            SchemaJobState::Failed => return Ok(()),
            SchemaJobState::Succeeded => {
                return Err(DdlRejection::Meta(MetaRejectionReason::Conflict {
                    resource: format!("DDL job {}", job.job_id),
                    reason: "cannot roll back a succeeded job".to_owned(),
                })
                .into());
            }
            SchemaJobState::Running | SchemaJobState::Cancelling => {
                self.set_state(
                    job.job_id,
                    SchemaJobState::RollingBack,
                    Some(reason.to_owned()),
                )?;
            }
            SchemaJobState::Pending | SchemaJobState::Paused | SchemaJobState::RollingBack => {}
        }
        if let DdlDefinition::AddIndex { index_name, .. } = &job.definition {
            for tablet in self.provider.tablets_of(job.table_id) {
                self.provider.sink(tablet)?.drop_generation(index_name)?;
            }
            self.apply(DdlCommand::RemoveIndexRecord { job_id: job.job_id })?;
            self.sync_maintainers(job.table_id)?;
        }
        self.set_state(job.job_id, SchemaJobState::Failed, Some(reason.to_owned()))
    }

    // -- Store helpers --------------------------------------------------------

    fn apply(&self, command: DdlCommand) -> Result<(), DdlError> {
        let commit_ts = self.clock.now()?;
        self.store
            .lock()
            .expect("store lock poisoned")
            .apply(&command, None, commit_ts)
            .map_err(DdlError::from)
    }

    fn job_record(&self, job_id: u64) -> Result<DdlJobRecord, DdlError> {
        self.store
            .lock()
            .expect("store lock poisoned")
            .job(job_id)
            .cloned()
            .ok_or_else(|| {
                DdlRejection::Meta(MetaRejectionReason::NotFound {
                    resource: format!("DDL job {job_id}"),
                })
                .into()
            })
    }

    fn index_record(
        &self,
        job: &DdlJobRecord,
        index_name: &str,
    ) -> Result<DdlIndexRecord, DdlError> {
        self.store
            .lock()
            .expect("store lock poisoned")
            .index(job.table_id, index_name)
            .cloned()
            .ok_or_else(|| {
                DdlRejection::Meta(MetaRejectionReason::NotFound {
                    resource: format!("index `{index_name}` on table {}", job.table_id),
                })
                .into()
            })
    }

    fn peek_next_job_id(&self) -> u64 {
        self.store.lock().expect("store lock poisoned").next_job_id
    }

    fn set_state(
        &self,
        job_id: u64,
        state: SchemaJobState,
        error: Option<String>,
    ) -> Result<(), DdlError> {
        let updated_at = self.clock.now()?;
        self.apply(DdlCommand::SetJobState {
            job_id,
            state,
            updated_at,
            error,
            expected_version: None,
        })
    }

    fn advance_phase(
        &self,
        job_id: u64,
        to: DdlPhase,
        pinned_snapshot: Option<HlcTimestamp>,
    ) -> Result<(), DdlError> {
        self.apply(DdlCommand::AdvancePhase {
            job_id,
            to,
            pinned_snapshot,
            expected_version: None,
        })
    }

    fn update_progress(&self, job_id: u64, progress: TabletDdlProgress) -> Result<(), DdlError> {
        self.apply(DdlCommand::UpdateTabletProgress { job_id, progress })
    }

    /// Pushes the table's maintained definition set to every tablet's
    /// apply-path maintainer (spec step 2's "tablet apply sees WriteOnly
    /// index defs for its table").
    fn sync_maintainers(&self, table_id: TableId) -> Result<(), DdlError> {
        let names: Vec<String> = self
            .store
            .lock()
            .expect("store lock poisoned")
            .write_maintained(table_id)
            .iter()
            .map(|record| record.index_name.clone())
            .collect();
        for tablet in self.provider.tablets_of(table_id) {
            self.provider
                .maintainer(tablet)?
                .sync_definitions(names.clone())?;
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    const TABLE: u64 = 1;

    fn ts(micros: u64) -> HlcTimestamp {
        HlcTimestamp {
            physical_micros: micros,
            logical: 0,
            node_tiebreaker: 0,
        }
    }

    /// A commit timestamp `n` micros after the job's pinned snapshot.
    fn after(pin: HlcTimestamp, n: u64) -> HlcTimestamp {
        HlcTimestamp {
            physical_micros: pin.physical_micros + n,
            logical: 0,
            node_tiebreaker: 0,
        }
    }

    fn tablet(n: u8) -> TabletId {
        TabletId::from_bytes([n; 16])
    }

    fn database() -> DatabaseId {
        DatabaseId::from_bytes([7; 16])
    }

    fn table() -> TableId {
        TableId(TABLE)
    }

    fn key(n: u64) -> Key {
        Key::from_bytes(n.to_be_bytes().to_vec())
    }

    fn value(n: u64) -> Vec<u8> {
        format!("value-{n}").into_bytes()
    }

    fn spec() -> serde_json::Value {
        serde_json::json!({"kind": "Bitmap", "columns": [1]})
    }

    struct Fixture {
        store: Arc<Mutex<DdlJobStore>>,
        tablets: InMemoryDdlTablets,
        driver: DdlDriver<InMemoryDdlTablets>,
    }

    impl Fixture {
        /// A store with table 1 anchored at schema version 1, `tablet_count`
        /// tablets each seeded with `rows_per_tablet` rows at ts(1..=rows).
        fn new(tablet_count: u8, rows_per_tablet: u64) -> Self {
            let store = Arc::new(Mutex::new(DdlJobStore::default()));
            let tablets = InMemoryDdlTablets::new();
            for n in 1..=tablet_count {
                tablets.add_tablet(tablet(n), table());
                let keyspace = tablets.keyspace_handle(tablet(n));
                for row in 1..=rows_per_tablet {
                    keyspace.insert(key(row), ts(row), value(row));
                }
            }
            let driver = DdlDriver::new(store.clone(), tablets.clone());
            driver
                .apply(DdlCommand::RegisterTable {
                    anchor: TableAnchor {
                        table_id: table(),
                        database_id: database(),
                        schema_version: SchemaVersion(1),
                        schema: serde_json::json!({"columns": ["id", "v"]}),
                        metadata_version: MetadataVersion::ZERO,
                    },
                })
                .expect("register table");
            Self {
                store,
                tablets,
                driver,
            }
        }

        fn store(&self) -> std::sync::MutexGuard<'_, DdlJobStore> {
            self.store.lock().expect("store lock poisoned")
        }

        fn submit_add(&self, index_name: &str) -> Result<u64, DdlError> {
            self.driver
                .submit_add_index(database(), table(), index_name, spec(), SchemaVersion(1))
        }

        fn pin_of(&self, job_id: u64) -> HlcTimestamp {
            self.store()
                .job(job_id)
                .and_then(|job| job.pinned_snapshot)
                .expect("job is pinned")
        }

        /// Drives the job until `step` returns true, returning the steps it
        /// took (fails after a bound of steps to surface livelocks).
        fn drive_until(
            &self,
            job_id: u64,
            mut stop: impl FnMut(DriveStep) -> bool,
        ) -> Vec<DriveStep> {
            let mut steps = Vec::new();
            for _ in 0..64 {
                let step = self.driver.step_job(job_id).expect("step");
                steps.push(step);
                if stop(step) || matches!(step, DriveStep::Terminal | DriveStep::Parked) {
                    return steps;
                }
            }
            panic!("job {job_id} did not reach the expected step");
        }

        fn drive_to_backfilled(&self, job_id: u64, count: usize) {
            let mut backfilled = 0_usize;
            self.drive_until(job_id, |step| {
                if matches!(step, DriveStep::TabletBackfilled { .. }) {
                    backfilled += 1;
                }
                backfilled == count
            });
        }
    }

    #[test]
    fn add_index_full_lifecycle_over_three_tablets() {
        let fixture = Fixture::new(3, 5);
        let job_id = fixture.submit_add("idx_a").expect("submit");

        // Spec step 1-2: the definition is replicated WriteOnly — maintained
        // by the write path, hidden from planners.
        let steps = fixture.drive_until(job_id, |step| {
            matches!(
                step,
                DriveStep::PhaseAdvanced {
                    from: DdlPhase::Pending,
                    to: DdlPhase::WriteOnly,
                }
            )
        });
        assert_eq!(steps.first(), Some(&DriveStep::Admitted));
        assert_eq!(fixture.store().write_maintained(table()).len(), 1);
        assert!(fixture.store().planner_visible(table()).is_empty());
        for n in 1..=3 {
            assert_eq!(
                fixture.tablets.indexes_handle(tablet(n)).maintained(),
                ["idx_a"]
            );
        }

        // Spec step 3: the job-wide snapshot is pinned.
        fixture.drive_until(job_id, |step| {
            matches!(
                step,
                DriveStep::PhaseAdvanced {
                    from: DdlPhase::WriteOnly,
                    to: DdlPhase::Backfilling,
                }
            )
        });
        let pin = fixture.pin_of(job_id);

        // Writes keep arriving. One lands before tablet 1's generation
        // install (the catch-up sweep must cover it); one lands after (the
        // apply-path maintainer must cover it); one delete exercises
        // tombstones on both paths.
        fixture
            .tablets
            .commit_write(tablet(1), key(100), after(pin, 1), Some(value(100)))
            .expect("write 100");
        fixture.drive_to_backfilled(job_id, 1);
        assert!(fixture
            .tablets
            .indexes_handle(tablet(1))
            .is_installed("idx_a"));
        fixture
            .tablets
            .commit_write(tablet(1), key(102), after(pin, 3), Some(value(102)))
            .expect("write 102");
        // Dual maintenance applied write 102 immediately.
        assert_eq!(
            fixture
                .tablets
                .indexes_handle(tablet(1))
                .entries("idx_a")
                .len(),
            7,
        );
        fixture
            .tablets
            .commit_write(tablet(2), key(101), after(pin, 2), Some(value(101)))
            .expect("write 101");
        fixture
            .tablets
            .commit_write(tablet(1), key(1), after(pin, 4), None)
            .expect("delete 1");
        fixture.drive_to_backfilled(job_id, 2);

        // Spec step 5-6: validation, then the atomic publication.
        let steps = fixture.drive_until(job_id, |step| matches!(step, DriveStep::Published { .. }));
        let publication_version = steps.iter().find_map(|step| match step {
            DriveStep::Published { version } => Some(*version),
            _ => None,
        });
        let publication_version = publication_version.expect("published");
        let store = fixture.store();
        let job = store.job(job_id).expect("job");
        assert_eq!(job.state, SchemaJobState::Succeeded);
        assert_eq!(job.phase, DdlPhase::Public);
        let record = store.index(table(), "idx_a").expect("index record");
        assert_eq!(record.phase, DdlPhase::Public);
        assert_eq!(record.publication_version, Some(publication_version));
        // The planner flips at exactly one metadata version.
        assert!(store
            .planner_visible_at(table(), MetadataVersion(publication_version.get() - 1))
            .is_empty());
        assert_eq!(
            store.planner_visible_at(table(), publication_version).len(),
            1
        );
        drop(store);

        // Spec step 5's aggregate report: per-tablet counts and checksums of
        // backfill+catch-up equal a from-scratch build.
        let report = fixture
            .driver
            .validation_report(job_id)
            .expect("validation report");
        assert!(report.passed);
        assert_eq!(report.tablets.len(), 3);
        assert_eq!(report.total_expected, report.total_actual);
        let per_tablet: BTreeMap<TabletId, &TabletValidationReport> = report
            .tablets
            .iter()
            .map(|tablet_report| (tablet_report.tablet_id, tablet_report))
            .collect();
        assert_eq!(per_tablet[&tablet(1)].expected_rows, 6); // 5 seeded - 1 deleted + 2 written
        assert_eq!(per_tablet[&tablet(2)].expected_rows, 6);
        assert_eq!(per_tablet[&tablet(3)].expected_rows, 5);

        // Direct content equality on tablet 1: built generation == projected
        // from-scratch rows at the validation watermark.
        let watermark = per_tablet[&tablet(1)].watermark;
        let expected = fixture
            .tablets
            .keyspace_handle(tablet(1))
            .rows_at(watermark);
        let actual = fixture.tablets.indexes_handle(tablet(1)).entries("idx_a");
        assert_eq!(actual, expected);

        // Status reporting aggregates the durable cursors.
        let status = fixture.driver.job_status(job_id).expect("status");
        assert_eq!(status.tablets_registered, 3);
        assert_eq!(status.tablets_caught_up, 3);
        assert_eq!(status.tablets_validated, 3);
        assert_eq!(status.rows_backfilled, 15);
    }

    #[test]
    fn catch_up_covers_pre_install_writes_and_tombstones() {
        let fixture = Fixture::new(1, 3);
        let job_id = fixture.submit_add("idx_t").expect("submit");
        fixture.drive_until(job_id, |step| {
            matches!(
                step,
                DriveStep::PhaseAdvanced {
                    from: DdlPhase::WriteOnly,
                    to: DdlPhase::Backfilling,
                }
            )
        });
        let pin = fixture.pin_of(job_id);
        // Write-then-delete a fresh key and delete a seeded key, all before
        // the generation is installed: the maintainer skips them, so the
        // delta sweep must cover them exactly once in effect.
        fixture
            .tablets
            .commit_write(tablet(1), key(50), after(pin, 1), Some(value(50)))
            .expect("write 50");
        fixture
            .tablets
            .commit_write(tablet(1), key(50), after(pin, 2), None)
            .expect("delete 50");
        fixture
            .tablets
            .commit_write(tablet(1), key(2), after(pin, 3), None)
            .expect("delete 2");
        assert_eq!(
            fixture.driver.drive_job(job_id).expect("drive"),
            DriveOutcome::Completed
        );

        let report = fixture.driver.validation_report(job_id).expect("report");
        assert!(report.passed);
        assert_eq!(report.total_expected, 2); // keys 1 and 3 survive
        let actual = fixture.tablets.indexes_handle(tablet(1)).entries("idx_t");
        assert_eq!(actual.len(), 2);
        assert!(!actual.contains_key(&key(2)));
        assert!(!actual.contains_key(&key(50)));
    }

    #[test]
    fn atomic_publish_flips_planner_visibility_at_one_metadata_version() {
        let fixture = Fixture::new(1, 2);
        let job_id = fixture.submit_add("idx_v").expect("submit");
        let before = fixture.store().metadata_version;
        assert_eq!(
            fixture.driver.drive_job(job_id).expect("drive"),
            DriveOutcome::Completed
        );
        let store = fixture.store();
        let publication = store
            .index(table(), "idx_v")
            .and_then(|record| record.publication_version)
            .expect("publication version");
        assert!(publication > before);
        assert!(
            store
                .planner_visible_at(table(), MetadataVersion(publication.get() - 1))
                .is_empty(),
            "hidden before the publication version"
        );
        let visible = store.planner_visible_at(table(), publication);
        assert_eq!(visible.len(), 1);
        assert_eq!(visible[0].index_name, "idx_v");
    }

    #[test]
    fn stale_schema_returns_structured_retry() {
        let fixture = Fixture::new(1, 2);
        let job_id = fixture.submit_add("idx_s").expect("submit");
        // Drive to the validation boundary, then move the table's schema
        // (models a concurrent alter landing mid-job).
        fixture.drive_until(job_id, |step| {
            matches!(
                step,
                DriveStep::PhaseAdvanced {
                    from: DdlPhase::Backfilling,
                    to: DdlPhase::Validating,
                }
            )
        });
        fixture
            .driver
            .apply(DdlCommand::RegisterTable {
                anchor: TableAnchor {
                    table_id: table(),
                    database_id: database(),
                    schema_version: SchemaVersion(2),
                    schema: serde_json::json!({"columns": ["id", "v", "w"]}),
                    metadata_version: MetadataVersion::ZERO,
                },
            })
            .expect("schema bump");
        let error = fixture
            .driver
            .drive_job(job_id)
            .expect_err("publish must refuse the stale schema");
        assert_eq!(error.category(), ErrorCategory::SchemaVersionMismatch);
        match &error {
            DdlError::Rejection(DdlRejection::SchemaVersionMismatch {
                table_id,
                expected,
                found,
            }) => {
                assert_eq!(*table_id, table());
                assert_eq!(*expected, SchemaVersion(1));
                assert_eq!(*found, SchemaVersion(2));
            }
            other => panic!("expected SchemaVersionMismatch, got {other:?}"),
        }
        // The failed job unwound: hidden generation dropped, record removed.
        let store = fixture.store();
        let job = store.job(job_id).expect("job");
        assert_eq!(job.state, SchemaJobState::Failed);
        assert!(store.index(table(), "idx_s").is_none());
        drop(store);
        assert!(!fixture
            .tablets
            .indexes_handle(tablet(1))
            .is_installed("idx_s"));

        // Submission against the moved schema fails at submit time too; the
        // refreshed resubmission succeeds.
        let stale = fixture.driver.submit_add_index(
            database(),
            table(),
            "idx_s2",
            spec(),
            SchemaVersion(1),
        );
        assert!(matches!(
            stale,
            Err(DdlError::Rejection(
                DdlRejection::SchemaVersionMismatch { .. }
            ))
        ));
        let job_id = fixture
            .driver
            .submit_add_index(database(), table(), "idx_s2", spec(), SchemaVersion(2))
            .expect("resubmit at the current schema version");
        assert_eq!(
            fixture.driver.drive_job(job_id).expect("drive"),
            DriveOutcome::Completed
        );
    }

    #[test]
    fn pause_resume_mid_backfill_resumes_exactly() {
        let fixture = Fixture::new(3, 4);
        let job_id = fixture.submit_add("idx_p").expect("submit");
        fixture.drive_to_backfilled(job_id, 1);
        fixture.driver.pause_job(job_id).expect("pause");
        assert!(
            fixture.driver.pause_job(job_id).is_err(),
            "double pause is refused"
        );
        assert_eq!(
            fixture.driver.drive_job(job_id).expect("drive"),
            DriveOutcome::Parked
        );
        assert_eq!(
            fixture
                .tablets
                .indexes_handle(tablet(1))
                .begin_build_count(),
            1
        );
        assert_eq!(
            fixture
                .tablets
                .indexes_handle(tablet(2))
                .begin_build_count(),
            0
        );

        fixture.driver.resume_job(job_id).expect("resume");
        assert_eq!(
            fixture.driver.drive_job(job_id).expect("drive"),
            DriveOutcome::Completed
        );
        // Tablet 1 was never rebuilt; the others built exactly once.
        assert_eq!(
            fixture
                .tablets
                .indexes_handle(tablet(1))
                .begin_build_count(),
            1
        );
        assert_eq!(
            fixture
                .tablets
                .indexes_handle(tablet(2))
                .begin_build_count(),
            1
        );
        assert_eq!(
            fixture
                .tablets
                .indexes_handle(tablet(3))
                .begin_build_count(),
            1
        );
        assert!(
            fixture
                .driver
                .validation_report(job_id)
                .expect("report")
                .passed
        );
        assert!(
            fixture.driver.resume_job(job_id).is_err(),
            "resume of a terminal job is refused"
        );
    }

    #[test]
    fn cancel_unwinds_the_hidden_generation_and_leaves_the_table_unaffected() {
        let fixture = Fixture::new(3, 4);
        let job_id = fixture.submit_add("idx_c").expect("submit");
        fixture.drive_to_backfilled(job_id, 1);
        assert!(fixture
            .tablets
            .indexes_handle(tablet(1))
            .is_installed("idx_c"));

        fixture.driver.cancel_job(job_id).expect("cancel");
        assert_eq!(
            fixture.driver.drive_job(job_id).expect("drive"),
            DriveOutcome::RolledBack
        );
        let store = fixture.store();
        let job = store.job(job_id).expect("job");
        assert_eq!(job.state, SchemaJobState::Failed);
        assert_eq!(job.error.as_deref(), Some("cancelled by operator"));
        assert!(store.index(table(), "idx_c").is_none());
        assert!(store.planner_visible(table()).is_empty());
        assert!(store.write_maintained(table()).is_empty());
        // The table is unaffected: anchor and rows untouched.
        assert_eq!(
            store.table_anchor(table()).expect("anchor").schema_version,
            SchemaVersion(1)
        );
        drop(store);
        for n in 1..=3 {
            let indexes = fixture.tablets.indexes_handle(tablet(n));
            assert!(!indexes.is_installed("idx_c"));
            assert_eq!(indexes.entries("idx_c").len(), 0);
            assert_eq!(indexes.maintained(), Vec::<String>::new());
        }
        assert_eq!(
            fixture
                .tablets
                .keyspace_handle(tablet(1))
                .rows_at(ts(u64::MAX))
                .len(),
            4
        );

        // Cancelling a terminal job is refused; a fresh job on the table is
        // admitted after the unwind.
        let second = fixture.submit_add("idx_c2").expect("submit after unwind");
        assert_eq!(
            fixture.driver.drive_job(second).expect("drive"),
            DriveOutcome::Completed
        );
        assert!(fixture.driver.cancel_job(second).is_err());
    }

    #[test]
    fn drop_index_lifecycle_reclaims_after_readers_drain() {
        let fixture = Fixture::new(3, 3);
        let add = fixture.submit_add("idx_d").expect("submit add");
        assert_eq!(
            fixture.driver.drive_job(add).expect("drive"),
            DriveOutcome::Completed
        );
        let pin = fixture.pin_of(add);
        // Public indexes stay maintained.
        fixture
            .tablets
            .commit_write(tablet(1), key(40), after(pin, 1), Some(value(40)))
            .expect("maintained write");
        assert_eq!(
            fixture
                .tablets
                .indexes_handle(tablet(1))
                .entries("idx_d")
                .len(),
            4
        );
        let pre_drop_version = fixture.store().metadata_version;

        let drop = fixture
            .driver
            .submit_drop_index(database(), table(), "idx_d", SchemaVersion(1))
            .expect("submit drop");
        assert_eq!(
            fixture.driver.drive_job(drop).expect("drive"),
            DriveOutcome::Completed
        );
        let dropping_since = {
            let store = fixture.store();
            let record = store.index(table(), "idx_d").expect("record");
            assert_eq!(record.phase, DdlPhase::Dropping);
            assert_eq!(
                store.job(drop).expect("job").state,
                SchemaJobState::Succeeded
            );
            // The planner stopped using it; an older plan still sees it.
            assert!(store.planner_visible(table()).is_empty());
            assert_eq!(store.planner_visible_at(table(), pre_drop_version).len(), 1);
            record.dropping_since.expect("dropping version")
        };
        assert!(dropping_since > pre_drop_version);
        // Writes stopped maintaining it.
        assert_eq!(
            fixture.tablets.indexes_handle(tablet(1)).maintained(),
            Vec::<String>::new()
        );
        fixture
            .tablets
            .commit_write(tablet(1), key(41), after(pin, 2), Some(value(41)))
            .expect("write after drop");
        assert_eq!(
            fixture
                .tablets
                .indexes_handle(tablet(1))
                .entries("idx_d")
                .len(),
            4
        );

        // Reclamation is gated on readers below the retirement version.
        fixture
            .driver
            .pin_reader(1, pre_drop_version)
            .expect("pin reader");
        let blocked = fixture
            .driver
            .reclaim_index(table(), "idx_d")
            .expect("reclaim attempt");
        assert_eq!(
            blocked,
            ReclaimOutcome::Blocked {
                oldest_reader: Some(pre_drop_version),
                required: dropping_since,
            }
        );
        let forward_version = fixture.store().metadata_version;
        fixture
            .driver
            .pin_reader(1, forward_version)
            .expect("reader moves forward");
        assert_eq!(
            fixture
                .driver
                .reclaim_index(table(), "idx_d")
                .expect("reclaim"),
            ReclaimOutcome::Reclaimed
        );
        assert!(fixture.store().index(table(), "idx_d").is_none());
        for n in 1..=3 {
            assert!(!fixture
                .tablets
                .indexes_handle(tablet(n))
                .is_installed("idx_d"));
        }
        // A backward reader pin is refused.
        fixture
            .driver
            .pin_reader(2, dropping_since)
            .expect("pin reader 2");
        assert!(fixture.driver.pin_reader(2, pre_drop_version).is_err());
    }

    #[test]
    fn per_tablet_progress_survives_a_driver_crash() {
        let fixture = Fixture::new(3, 4);
        let job_id = fixture.submit_add("idx_x").expect("submit");
        fixture.drive_to_backfilled(job_id, 1);
        let pin = fixture.pin_of(job_id);
        let crashed = fixture.driver;
        // The crashed driver also left an interrupted staged build behind on
        // tablet 2; the resumed driver must discard it via begin_build.
        fixture.tablets.indexes_handle(tablet(2)).seed_staged(
            "idx_x",
            key(999),
            b"garbage".to_vec(),
        );
        // A committed write lands while no driver runs.
        fixture
            .tablets
            .commit_write(tablet(2), key(60), after(pin, 1), Some(value(60)))
            .expect("write during outage");
        drop(crashed);

        let resumed = DdlDriver::new(fixture.store.clone(), fixture.tablets.clone());
        assert_eq!(
            resumed.drive_job(job_id).expect("drive"),
            DriveOutcome::Completed
        );
        // Tablet 1's durable cursor skipped its rebuild; tablet 2's staged
        // garbage was discarded and rebuilt correctly.
        assert_eq!(
            fixture
                .tablets
                .indexes_handle(tablet(1))
                .begin_build_count(),
            1
        );
        assert_eq!(
            fixture
                .tablets
                .indexes_handle(tablet(2))
                .begin_build_count(),
            1
        );
        assert!(!fixture
            .tablets
            .indexes_handle(tablet(2))
            .entries("idx_x")
            .contains_key(&key(999)));
        assert_eq!(
            fixture
                .tablets
                .indexes_handle(tablet(2))
                .entries("idx_x")
                .len(),
            5
        );
        assert!(resumed.validation_report(job_id).expect("report").passed);
    }

    #[test]
    fn phase_graph_is_enforced() {
        let edges: [(DdlPhase, DdlPhase); 5] = [
            (DdlPhase::Pending, DdlPhase::WriteOnly),
            (DdlPhase::WriteOnly, DdlPhase::Backfilling),
            (DdlPhase::Backfilling, DdlPhase::Validating),
            (DdlPhase::Validating, DdlPhase::Public),
            (DdlPhase::Public, DdlPhase::Dropping),
        ];
        for from in DdlPhase::ALL {
            for to in DdlPhase::ALL {
                assert_eq!(
                    from.can_transition(to),
                    edges.contains(&(from, to)),
                    "{from} -> {to}",
                );
            }
        }

        // The store refuses off-graph and bypass-PublishJob moves.
        let fixture = Fixture::new(1, 1);
        let job_id = fixture.submit_add("idx_g").expect("submit");
        fixture.drive_until(job_id, |step| matches!(step, DriveStep::Admitted));
        let skipped = fixture
            .driver
            .advance_phase(job_id, DdlPhase::Validating, None);
        assert!(matches!(
            skipped,
            Err(DdlError::Rejection(DdlRejection::IllegalPhaseTransition {
                from: DdlPhase::Pending,
                to: DdlPhase::Validating,
                ..
            }))
        ));
        fixture.drive_to_backfilled(job_id, 1);
        fixture.drive_until(job_id, |step| {
            matches!(
                step,
                DriveStep::PhaseAdvanced {
                    from: DdlPhase::Backfilling,
                    to: DdlPhase::Validating,
                }
            )
        });
        let bypass = fixture.driver.advance_phase(job_id, DdlPhase::Public, None);
        assert!(
            matches!(
                bypass,
                Err(DdlError::Rejection(DdlRejection::Meta(
                    MetaRejectionReason::Invalid { .. }
                )))
            ),
            "Public rides PublishJob: {bypass:?}",
        );
    }

    #[test]
    fn admin_state_graph_is_enforced() {
        let fixture = Fixture::new(1, 1);
        let job_id = fixture.submit_add("idx_a").expect("submit");
        assert!(
            fixture.driver.pause_job(job_id).is_err(),
            "Pending cannot pause"
        );
        assert!(
            fixture.driver.resume_job(job_id).is_err(),
            "Pending cannot resume"
        );
        fixture
            .driver
            .cancel_job(job_id)
            .expect("cancel from Pending");
        assert_eq!(
            fixture.driver.drive_job(job_id).expect("drive"),
            DriveOutcome::RolledBack
        );
        let job = fixture.store().job(job_id).expect("job").clone();
        assert_eq!(job.state, SchemaJobState::Failed);
        assert!(
            fixture.driver.cancel_job(job_id).is_err(),
            "terminal jobs refuse cancel"
        );
    }

    #[test]
    fn alter_schema_publishes_the_next_schema_version_atomically() {
        let fixture = Fixture::new(2, 3);
        let target = serde_json::json!({"columns": ["id", "v", "w"]});
        let job_id = fixture
            .driver
            .submit_alter_schema(database(), table(), target.clone(), SchemaVersion(1))
            .expect("submit alter");
        assert_eq!(
            fixture.driver.drive_job(job_id).expect("drive"),
            DriveOutcome::Completed
        );
        let store = fixture.store();
        let anchor = store.table_anchor(table()).expect("anchor");
        assert_eq!(anchor.schema_version, SchemaVersion(2));
        assert_eq!(anchor.schema, target);
        let job = store.job(job_id).expect("job");
        assert_eq!(job.state, SchemaJobState::Succeeded);
        assert_eq!(job.phase, DdlPhase::Public);
        assert_eq!(job.tablet_progress.len(), 2);
        drop(store);

        // The old schema version is now stale for new jobs; the new one works.
        let stale = fixture.submit_add("idx_old");
        assert!(matches!(
            stale,
            Err(DdlError::Rejection(
                DdlRejection::SchemaVersionMismatch { .. }
            ))
        ));
        let fresh = fixture
            .driver
            .submit_add_index(database(), table(), "idx_new", spec(), SchemaVersion(2))
            .expect("submit at v2");
        assert_eq!(
            fixture.driver.drive_job(fresh).expect("drive"),
            DriveOutcome::Completed
        );
    }

    #[test]
    fn topology_change_mid_backfill_is_reconciled() {
        let fixture = Fixture::new(3, 3);
        let job_id = fixture.submit_add("idx_t").expect("submit");
        fixture.drive_to_backfilled(job_id, 1);
        // A split adds tablet 4; a merge retires tablet 3.
        fixture.tablets.add_tablet(tablet(4), table());
        let keyspace = fixture.tablets.keyspace_handle(tablet(4));
        for row in 1..=3 {
            keyspace.insert(key(row + 100), ts(row), value(row + 100));
        }
        fixture.tablets.remove_tablet(tablet(3));
        assert_eq!(
            fixture.driver.drive_job(job_id).expect("drive"),
            DriveOutcome::Completed
        );
        let status = fixture.driver.job_status(job_id).expect("status");
        assert_eq!(status.tablets_registered, 3);
        assert!(status.record.tablet_progress.contains_key(&tablet(4)));
        assert!(!status.record.tablet_progress.contains_key(&tablet(3)));
        assert!(
            fixture
                .driver
                .validation_report(job_id)
                .expect("report")
                .passed
        );
    }

    #[test]
    fn conflicting_and_duplicate_submissions_are_refused() {
        let fixture = Fixture::new(1, 2);
        let job_id = fixture.submit_add("idx_1").expect("submit");
        // One active job per table.
        let concurrent = fixture.submit_add("idx_2");
        assert!(matches!(
            concurrent,
            Err(DdlError::Rejection(DdlRejection::Meta(
                MetaRejectionReason::Conflict { .. }
            )))
        ));
        assert_eq!(
            fixture.driver.drive_job(job_id).expect("drive"),
            DriveOutcome::Completed
        );
        // A completed index name cannot be re-added.
        let duplicate = fixture.submit_add("idx_1");
        assert!(matches!(
            duplicate,
            Err(DdlError::Rejection(DdlRejection::Meta(
                MetaRejectionReason::Conflict { .. }
            )))
        ));
        // Dropping a missing index fails closed.
        let missing =
            fixture
                .driver
                .submit_drop_index(database(), table(), "idx_missing", SchemaVersion(1));
        assert!(matches!(
            missing,
            Err(DdlError::Rejection(DdlRejection::Meta(
                MetaRejectionReason::NotFound { .. }
            )))
        ));
        // Identical command replay is idempotent (raft retry).
        let job = fixture.store().job(job_id).expect("job").clone();
        fixture
            .driver
            .apply(DdlCommand::SubmitJob { job })
            .expect("identical replay is a no-op");
        assert_eq!(fixture.store().jobs.len(), 1);
    }

    #[test]
    fn store_roundtrips_through_json() {
        let fixture = Fixture::new(2, 3);
        let job_id = fixture.submit_add("idx_j").expect("submit");
        fixture.drive_to_backfilled(job_id, 1);
        let snapshot = {
            let store = fixture.store();
            serde_json::to_string(&*store).expect("encode")
        };
        let decoded: DdlJobStore = serde_json::from_str(&snapshot).expect("decode");
        assert_eq!(decoded, *fixture.store());
    }

    #[test]
    fn command_roundtrips_through_json() {
        let job = DdlJobRecord {
            job_id: 9,
            database_id: database(),
            table_id: table(),
            kind: DdlJobKind::AddIndex,
            state: SchemaJobState::Pending,
            phase: DdlPhase::Pending,
            definition: DdlDefinition::AddIndex {
                index_name: "idx_serde".to_owned(),
                spec: spec(),
            },
            source_schema_version: SchemaVersion(1),
            created_at: ts(10),
            updated_at: ts(10),
            pinned_snapshot: None,
            tablet_progress: BTreeMap::new(),
            error: None,
            metadata_version: MetadataVersion::ZERO,
        };
        let command = DdlCommand::SubmitJob { job };
        let encoded = serde_json::to_string(&command).expect("encode");
        let decoded: DdlCommand = serde_json::from_str(&encoded).expect("decode");
        assert_eq!(decoded, command);
    }
}