slatedb 0.14.0

A cloud native embedded storage engine built on object storage.
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
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
//! Compactor Orchestration and Naming Overview
//!
//! This module implements the event loop ("compactor") that orchestrates database
//! compactions. It coordinates three concerns:
//!
//! 1. deciding what to compact,
//! 2. executing the work,
//! 3. and persisting the effects.
//!
//! The key types follow a consistent naming hierarchy to distinguish “what should happen”
//! from “what is currently running”.
//!
//! Naming hierarchy (logical → runtime):
//! - [`CompactionSpec`]: Description of a compaction to perform. Pure specification of
//!   inputs and destination (e.g., L0 SST ULIDs and/or Sorted Run ids → destination SR id).
//! - [`Compaction`]: A concrete compaction entity the system decided to run. It has a
//!   stable id (ULID) and references a [`CompactionSpec`]. Compactions live in the
//!   process-local [`CompactorState`] and represent the lifecycle of a single planned
//!   transformation of the DB’s structure. Today each [`Compaction`] has exactly one
//!   runtime job; future retries would attach multiple jobs to a single [`Compaction`].
//! - Compaction Job: A single job attempt the executor runs. Currently there isn't a separate model
//!   type that tracks the Compaction Job entity. When starting a job the compactor creates a
//!   [`StartCompactionJobArgs`] instance which materializes the execution-time inputs (opened
//!   SST handles and Sorted Runs), carries runtime context like the logical compaction clock tick,
//!   and sends this to the executor.
//!   Currently there is a 1:1 mapping between [`Compaction`] and a Compaction Job, but
//!   if retries are introduced, multiple Compaction Jobs would be associated with a single
//!   [`Compaction`].
//!
//! Roles:
//! - [`CompactionScheduler`]: Policy that proposes [`CompactionSpec`] values based on the
//!   current [`CompactorState`] (e.g., size-tiered scheduling). It validates logical
//!   invariants (such as consecutive sources and destination rules).
//! - [`CompactionExecutor`]: Runs [`CompactionJobSpec`] attempts, merges inputs, applies
//!   merge and retention logic, writes output SSTs, and reports progress/finish.
//! - [`CompactorEventHandler`]: Event-driven controller. It polls the manifest, asks
//!   the scheduler for new [`CompactionSpec`] proposals, registers [`Compaction`] entities,
//!   spawns [`CompactionJobSpec`] attempts on the executor, and persists results by
//!   updating the manifest.
//!
//! Progress and GC safety:
//! - Progress is tracked per compaction as `bytes_processed` (approximate, for observability).
//! - The lowest start time among active compaction ids (ULIDs) is exported as a
//!   “low-watermark” hint so GC can avoid deleting inputs required by running work.
//!
//! High-level flow:
//! 1) Poll manifest and merge remote state into local [`CompactorState`].
//! 2) Ask the [`CompactionScheduler`] for candidate [`CompactionSpec`] values.
//! 3) For each accepted spec, create a [`Compaction`] (id + spec), derive a
//!    [`StartCompactionJobArgs`] with concrete inputs, and pass it to the executor.
//! 4) Upon completion, update [`CompactorState`], write a new manifest, and repeat.
//!
//! These names are used consistently across modules so it is clear whether a type
//! represents a description (Spec), a durable decision (Compaction), or a running
//! attempt (JobSpec).

use std::collections::HashMap;
use std::collections::HashSet;
use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use bytes::Bytes;
use fail_parallel::FailPointRegistry;
use futures::stream::BoxStream;
use log::{debug, error, info, warn};
use serde::{Deserialize, Serialize};
use tokio::runtime::Handle;
use tracing::instrument;
use ulid::Ulid;

#[cfg(feature = "compaction_filters")]
use crate::compaction_filter::CompactionFilterSupplier;
use crate::compaction_worker::CompactionWorkerHandler;
use crate::compactions_store::{CompactionsStore, StoredCompactions};
use crate::compactor::stats::CompactionStats;
use crate::compactor_state_protocols::CompactorStateWriter;
use crate::config::CompactorOptions;
use crate::db_state::{SortedRun, SsTableView};
use crate::db_status::ClosedResultWriter;
use crate::dispatcher::{MessageHandler, MessageHandlerExecutor, MessageTickerDef};
use crate::error::{Error, SlateDBError};
use crate::manifest::store::ManifestStore;
use crate::manifest::{LsmTreeState, ManifestCore};
use crate::merge_operator::MergeOperatorType;
use crate::tablestore::TableStore;
use crate::utils::{format_bytes_si, IdGenerator};
use slatedb_common::clock::SystemClock;
use slatedb_common::metrics::{GaugeFn, MetricsRecorderHelper};
use slatedb_common::DbRand;

pub use crate::compactor_state::{
    Compaction, CompactionSpec, CompactionStatus, CompactorState, SourceId,
};
pub use crate::compactor_state_protocols::CompactorStateView;
pub use crate::db::builder::CompactorBuilder;
pub use crate::size_tiered_compaction::SizeTieredCompactionSchedulerSupplier;

pub(crate) const COMPACTOR_TASK_NAME: &str = "compactor";

/// Supplies a concrete [`CompactionScheduler`] implementation.
///
/// This indirection lets SlateDB plug different scheduling policies (e.g. size-tiered,
/// leveled, manual) without coupling the compactor to a specific strategy.
pub trait CompactionSchedulerSupplier: Send + Sync {
    /// Creates a new [`CompactionScheduler`] using the provided options.
    ///
    /// ## Arguments
    /// - `options`: Compactor runtime tuning knobs (e.g. concurrency, polling interval).
    ///
    /// ## Returns
    /// - A boxed scheduler that decides which compactions to run next.
    fn compaction_scheduler(
        &self,
        options: &CompactorOptions,
    ) -> Box<dyn CompactionScheduler + Send + Sync>;
}

/// Policy that decides when and what to compact.
///
/// The compactor periodically invokes the scheduler with the latest [`CompactorState`].
/// Implementations return one or more candidate compaction specs, which the compactor then
/// validates and submits to the executor.
pub trait CompactionScheduler: Send + Sync {
    /// Proposes compaction specs for the current state.
    ///
    /// ## Arguments
    /// - `state`: Process-local view of the DB's manifest and compactions.
    ///
    /// ## Returns
    /// - A list of [`CompactionSpec`] describing what to compact and where to write.
    fn propose(&self, state: &CompactorStateView) -> Vec<CompactionSpec>;

    /// Validates a candidate compaction spec against scheduler-specific invariants. The
    /// default implementation accepts everything. Schedulers can override to enforce
    /// policy-specific constraints prior to execution (e.g. level rules, overlaps).
    ///
    /// Not all compactions have to originate from scheduler. Admin/“manual” compactions,
    /// persisted/resumed plans, or tests can construct a [`CompactionSpec`]`. This
    /// method can be used to validate external compaction specs against scheduler
    /// specific invariants.
    ///
    /// ## Arguments
    /// - `_state`: Process-local view of the DB's manifest and compactions.
    /// - `_spec`: Proposed [`CompactionSpec`].
    ///
    /// ## Returns
    /// - `Ok(())` if valid, or an [`Error`] if invalid.
    fn validate(&self, _state: &CompactorStateView, _spec: &CompactionSpec) -> Result<(), Error> {
        Ok(())
    }

    /// Generate one or more compaction specs from a [`CompactionRequest`].
    ///
    /// - [`CompactionRequest::Spec`] returns the caller-provided spec verbatim.
    /// - [`CompactionRequest::FullSegment`] targets a single tree (root or a
    ///   named segment): every sorted run becomes a source, the tree's lowest
    ///   sorted-run id is the destination. Returns an error if the segment
    ///   is unknown or the tree has no compacted sorted runs.
    /// - [`CompactionRequest::Full`] applies the same per-tree rule to every
    ///   tree in the DB (root + every named segment), best-effort: trees
    ///   without compacted sorted runs are silently skipped and an empty
    ///   `Vec` is returned if no tree is eligible.
    ///
    /// L0 SSTs are intentionally excluded so the size-tiered scheduler can
    /// keep compacting new flush output in parallel. Including L0 would
    /// mark those SSTs as in-use for the duration of the compaction,
    /// eventually stalling writers once L0 reaches `l0_max_ssts`.
    ///
    /// ## Arguments
    /// - `state`: Process-local view of the DB's manifest and compactions.
    /// - `request`: The compaction request to plan.
    ///
    /// ## Returns
    /// - A list of [`CompactionSpec`] describing what to compact and where to write.
    /// - An [`Error`] if the request is invalid.
    fn generate(
        &self,
        state: &CompactorStateView,
        request: &CompactionRequest,
    ) -> Result<Vec<CompactionSpec>, Error> {
        match request {
            CompactionRequest::Spec(spec) => Ok(vec![spec.clone()]),
            CompactionRequest::FullSegment { segment } => {
                let manifest = state.manifest().core();
                let Some(tree) = manifest.tree_for_segment(segment) else {
                    error!(
                        "rejected full-segment compaction: unknown segment {:?}",
                        segment
                    );
                    return Err(crate::Error::from(SlateDBError::InvalidCompaction));
                };
                match plan_full_tree(segment, tree) {
                    Some(spec) => Ok(vec![spec]),
                    None => {
                        if !tree.l0.is_empty() {
                            error!(
                                "rejected full-segment compaction: segment {:?} has L0 SSTs but no sorted runs to merge; \
                                 L0 SSTs are not eligible inputs",
                                segment
                            );
                        }
                        Err(crate::Error::from(SlateDBError::InvalidCompaction))
                    }
                }
            }
            CompactionRequest::Full => {
                let manifest = state.manifest().core();
                let specs = manifest
                    .trees_with_prefix()
                    .filter_map(|(prefix, tree)| plan_full_tree(&prefix, tree))
                    .collect();
                Ok(specs)
            }
        }
    }
}

/// Build a [`CompactionSpec`] that compacts every sorted run in `tree` into
/// the tree's lowest-id sorted run. Returns `None` when the tree has no
/// compacted sorted runs — L0 SSTs are not eligible inputs.
fn plan_full_tree(prefix: &Bytes, tree: &LsmTreeState) -> Option<CompactionSpec> {
    if tree.compacted.is_empty() {
        return None;
    }
    let sources = tree
        .compacted
        .iter()
        .map(|sr| SourceId::SortedRun(sr.id))
        .collect::<Vec<_>>();
    let destination = sources
        .iter()
        .map(|s| s.unwrap_sorted_run())
        .min()
        .expect("at least one sorted run");
    Some(CompactionSpec::for_segment(
        prefix.clone(),
        sources,
        destination,
    ))
}

/// Request to submit a compaction for execution.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum CompactionRequest {
    /// Compact every tree in the DB — the root tree plus each named segment
    /// (RFC-0024) — by merging every sorted run in each tree into that
    /// tree's lowest-id sorted run. Trees with no compacted sorted runs are
    /// silently skipped; an empty `Vec` is returned when no tree is eligible.
    ///
    /// L0 SSTs are left to regular size-tiered compaction so memtable flushes
    /// can continue producing new sorted runs while this compaction is in
    /// flight. Pulling L0 into the merge would mark those SSTs as in-use
    /// for the duration of the compaction, eventually stalling writers once
    /// L0 reaches `l0_max_ssts`.
    Full,
    /// Compact a single tree: every sorted run in the tree at `segment` is
    /// merged into the tree's lowest-id sorted run. `segment: Bytes::new()`
    /// targets the compatibility-encoded root tree (the only valid segment
    /// on a database without a segment extractor); a non-empty prefix names
    /// a segment. Rejected if the segment is unknown or the tree has no
    /// compacted sorted runs. L0 SSTs are excluded for the same reason
    /// documented on `Full`.
    FullSegment { segment: Bytes },
    /// Use a caller-provided compaction spec.
    Spec(CompactionSpec),
}

/// Messages exchanged between the compactor event loop and its collaborators.
#[derive(Debug)]
pub(crate) enum CompactorMessage {
    /// Ticker-triggered message to log DB runs and in-flight job state.
    LogStats,
    /// Ticker-triggered message to refresh the manifest and schedule compactions.
    PollManifest,
    /// Fast ticker to commit any `Compacted` entries written by workers without
    /// waiting for the full `poll_interval` cycle.
    CommitCompacted,
}

/// The compactor is responsible for taking groups of sorted runs (this doc uses the term
/// sorted run to refer to both sorted runs and l0 ssts) and compacting them together to
/// reduce space amplification (by removing old versions of rows that have been updated/deleted)
/// and read amplification (by reducing the number of sorted runs that need to be searched on
/// a read). It's made up of a few different components:
///
/// - [`Compactor`]: The main event loop that orchestrates the compaction process.
/// - [`CompactorEventHandler`]: The event handler that handles events from the compactor.
/// - [`CompactionScheduler`]: The scheduler that discovers compactions that should be performed.
/// - [`CompactionExecutor`]: The executor that runs the compaction tasks.
///
/// The main event loop listens on the manifest poll ticker to react to manifest poll ticks, the
/// executor worker channel to react to updates about running compactions, and the shutdown
/// channel to discover when it should terminate. It doesn't actually implement the logic for
/// reacting to these events. This is implemented by [`CompactorEventHandler`].
///
/// The Scheduler is responsible for deciding what sorted runs should be compacted together.
/// It implements the [`CompactionScheduler`] trait. The implementation is specified by providing
/// an implementation of [`crate::config::CompactionSchedulerSupplier`] so different scheduling
/// policies can be plugged into slatedb. Currently, the only implemented policy is the size-tiered
/// scheduler supplied by [`crate::size_tiered_compaction::SizeTieredCompactionSchedulerSupplier`]
///
/// The Executor does the actual work of compacting sorted runs by sort-merging them into a new
/// sorted run. It implements the [`CompactionExecutor`] trait. Currently, the only implementation
/// is the [`TokioCompactionExecutor`] which runs compaction on a local tokio runtime.
#[derive(Clone)]
pub struct Compactor {
    manifest_store: Arc<ManifestStore>,
    compactions_store: Arc<CompactionsStore>,
    table_store: Arc<TableStore>,
    options: Arc<CompactorOptions>,
    scheduler_supplier: Arc<dyn CompactionSchedulerSupplier>,
    task_executor: Arc<MessageHandlerExecutor>,
    compactor_runtime: Handle,
    rand: Arc<DbRand>,
    stats: Arc<CompactionStats>,
    recorder: MetricsRecorderHelper,
    system_clock: Arc<dyn SystemClock>,
    fp_registry: Arc<FailPointRegistry>,
    merge_operator: Option<MergeOperatorType>,
    #[cfg(feature = "compaction_filters")]
    compaction_filter_supplier: Option<Arc<dyn CompactionFilterSupplier>>,
}

impl Compactor {
    #[cfg_attr(not(feature = "compaction_filters"), allow(clippy::too_many_arguments))]
    pub(crate) fn new(
        manifest_store: Arc<ManifestStore>,
        compactions_store: Arc<CompactionsStore>,
        table_store: Arc<TableStore>,
        options: CompactorOptions,
        scheduler_supplier: Arc<dyn CompactionSchedulerSupplier>,
        compactor_runtime: Handle,
        rand: Arc<DbRand>,
        recorder: &MetricsRecorderHelper,
        system_clock: Arc<dyn SystemClock>,
        fp_registry: Arc<FailPointRegistry>,
        closed_result: Arc<dyn ClosedResultWriter>,
        merge_operator: Option<MergeOperatorType>,
        #[cfg(feature = "compaction_filters")] compaction_filter_supplier: Option<
            Arc<dyn CompactionFilterSupplier>,
        >,
    ) -> Self {
        let stats = Arc::new(CompactionStats::new(recorder));
        let task_executor = Arc::new(MessageHandlerExecutor::new(
            closed_result,
            system_clock.clone(),
        ));
        Self {
            manifest_store,
            compactions_store,
            table_store,
            options: Arc::new(options),
            scheduler_supplier,
            task_executor,
            compactor_runtime,
            rand,
            stats,
            recorder: recorder.clone(),
            system_clock,
            fp_registry,
            merge_operator,
            #[cfg(feature = "compaction_filters")]
            compaction_filter_supplier,
        }
    }

    /// Starts the compactor. This method runs the actual compaction event loop. The
    /// compactor runs until the cancellation token is cancelled. The compactor's event
    /// loop always runs on the current runtime, while the compactor executor runs on the
    /// provided runtime in the [`Compactor::new`] constructor. This is to keep
    /// long-running compaction tasks from blocking the main runtime.
    ///
    /// ## Returns
    /// - `Ok(())` when the compactor task exits cleanly, or [`SlateDBError`] on failure.
    pub async fn run(&self) -> Result<(), crate::Error> {
        self.start().await?;
        self.join().await
    }

    pub(crate) async fn start(&self) -> Result<(), crate::Error> {
        // The coordinator delegates compaction execution to [`crate::compaction_worker::CompactionWorker`]
        // either spawned in this process (set `worker: Some`) or running standalone (set `worker: None`).
        let (_tx, rx) = async_channel::unbounded::<CompactorMessage>();
        let scheduler = Arc::from(self.scheduler_supplier.compaction_scheduler(&self.options));
        let handler = CompactorEventHandler::new(
            self.manifest_store.clone(),
            self.compactions_store.clone(),
            self.options.clone(),
            scheduler,
            self.rand.clone(),
            self.stats.clone(),
            self.system_clock.clone(),
            self.recorder.clone(),
        )
        .await?;
        self.task_executor
            .add_handler(
                COMPACTOR_TASK_NAME.to_string(),
                Box::new(handler),
                rx,
                &Handle::current(),
            )
            .map_err(crate::Error::from)?;

        // Spawn an in-process worker if configured. The worker runs under its
        // own cancellation token; Compactor::stop and run() are responsible for
        // shutting it down alongside the coordinator.
        if let Some(worker_options) = self.options.worker.clone() {
            let (worker_handler, worker_rx) = CompactionWorkerHandler::build_worker_handler(
                self.manifest_store.clone(),
                self.compactions_store.clone(),
                self.table_store.clone(),
                Arc::new(worker_options),
                self.compactor_runtime.clone(),
                self.rand.clone(),
                self.stats.clone(),
                self.recorder.clone(),
                self.system_clock.clone(),
                self.fp_registry.clone(),
                self.merge_operator.clone(),
                #[cfg(feature = "compaction_filters")]
                self.compaction_filter_supplier.clone(),
            );
            self.task_executor
                .add_handler(
                    crate::compaction_worker::COMPACTION_WORKER_TASK_NAME.to_string(),
                    Box::new(worker_handler),
                    worker_rx,
                    &Handle::current(),
                )
                .map_err(crate::Error::from)?;
        }

        self.task_executor.monitor_on(&Handle::current())?;
        Ok(())
    }

    pub(crate) async fn join(&self) -> Result<(), crate::Error> {
        self.task_executor
            .join_task(COMPACTOR_TASK_NAME)
            .await
            .map_err(crate::Error::from)?;
        if self.options.worker.is_some() {
            self.task_executor
                .join_task(crate::compaction_worker::COMPACTION_WORKER_TASK_NAME)
                .await
                .map_err(crate::Error::from)?;
        }
        Ok(())
    }

    /// Gracefully stops the compactor task and waits for it to finish.
    ///
    /// ## Returns
    /// - `Ok(())` once the task has shut down, or [`SlateDBError`] if shutdown fails.
    pub async fn stop(&self) -> Result<(), crate::Error> {
        self.task_executor
            .shutdown_task(COMPACTOR_TASK_NAME)
            .await
            .map_err(crate::Error::from)?;
        if self.options.worker.is_some() {
            self.task_executor
                .shutdown_task(crate::compaction_worker::COMPACTION_WORKER_TASK_NAME)
                .await
                .map_err(crate::Error::from)?;
        }
        Ok(())
    }

    /// Persist a [`CompactionSpec`] as a new [`Compaction`] in the compactions store.
    ///
    /// ## Returns
    /// - `Ok(Ulid)` with the submitted compaction id.
    /// - `SlateDBError::InvalidDBState` if the compactions file doesn't exist.
    /// - `SlateDBError::InvalidCompaction` if a full compaction has no sorted run sources.
    /// - `SlateDBError` if the compaction could not be persisted.
    pub(crate) async fn submit(
        spec: CompactionSpec,
        compactions_store: Arc<CompactionsStore>,
        rand: Arc<DbRand>,
        system_clock: Arc<dyn SystemClock>,
    ) -> Result<Ulid, crate::Error> {
        let compaction_id = rand.rng().gen_ulid(system_clock.as_ref());
        let compaction = Compaction::new(compaction_id, spec);
        let mut stored_compactions =
            match StoredCompactions::try_load(compactions_store.clone()).await? {
                Some(stored) => stored,
                None => return Err(crate::Error::from(SlateDBError::InvalidDBState)),
            };

        loop {
            let mut dirty = stored_compactions.prepare_dirty()?;
            dirty.value.insert(compaction.clone());
            match stored_compactions.update(dirty).await {
                Ok(()) => return Ok(compaction_id),
                Err(err) if err.is_sequenced_write_conflict() => {
                    stored_compactions.refresh().await?;
                }
                Err(err) => return Err(crate::Error::from(err)),
            }
        }
    }
}

/// Event-driven controller for compaction orchestration.
///
/// The [`CompactorEventHandler`] implements [`MessageHandler<CompactorMessage>`] and
/// runs inside the compactor task's message loop. It reacts to periodic tickers and
/// executor callbacks, and coordinates scheduling and execution of compactions.
pub(crate) struct CompactorEventHandler {
    state_writer: CompactorStateWriter,
    options: Arc<CompactorOptions>,
    scheduler: Arc<dyn CompactionScheduler + Send + Sync>,
    rand: Arc<DbRand>,
    stats: Arc<CompactionStats>,
    system_clock: Arc<dyn SystemClock>,
    recorder: MetricsRecorderHelper,
    /// Job ids the coordinator has observed claimed by a worker (`Running` or
    /// `Compacted` with a worker assigned). Initially empty, so the first metrics
    /// update counts every inherited claim before establishing the set-difference
    /// baseline. See [`Self::update_distributed_compaction_metrics`].
    prev_claimed: HashSet<Ulid>,
    /// Cached handles for per-worker `worker_last_heartbeat_ms` gauges. Handles are
    /// retained for every worker id observed by this coordinator process.
    worker_heartbeat_gauges: HashMap<String, Arc<dyn GaugeFn>>,
}

#[async_trait]
impl MessageHandler<CompactorMessage> for CompactorEventHandler {
    fn tickers(&mut self) -> Vec<MessageTickerDef<CompactorMessage>> {
        vec![
            MessageTickerDef::new(
                self.options.poll_interval,
                Box::new(|| CompactorMessage::PollManifest),
            ),
            MessageTickerDef::new(
                Duration::from_secs(10),
                Box::new(|| CompactorMessage::LogStats),
            ),
            MessageTickerDef::new(
                self.options.commit_compacted_interval,
                Box::new(|| CompactorMessage::CommitCompacted),
            ),
        ]
    }

    async fn handle(&mut self, message: CompactorMessage) -> Result<(), SlateDBError> {
        match message {
            CompactorMessage::LogStats => self.handle_log_ticker(),
            CompactorMessage::PollManifest => self.handle_ticker().await?,
            CompactorMessage::CommitCompacted => {
                self.state_writer.load_compactions().await?;
                self.update_distributed_compaction_metrics();
                self.commit_compacted_entries().await?;
            }
        }
        Ok(())
    }

    async fn cleanup(
        &mut self,
        mut _messages: BoxStream<'async_trait, CompactorMessage>,
        _result: Result<(), SlateDBError>,
    ) -> Result<(), SlateDBError> {
        Ok(())
    }
}

impl CompactorEventHandler {
    pub(crate) async fn new(
        manifest_store: Arc<ManifestStore>,
        compactions_store: Arc<CompactionsStore>,
        options: Arc<CompactorOptions>,
        scheduler: Arc<dyn CompactionScheduler + Send + Sync>,
        rand: Arc<DbRand>,
        stats: Arc<CompactionStats>,
        system_clock: Arc<dyn SystemClock>,
        recorder: MetricsRecorderHelper,
    ) -> Result<Self, SlateDBError> {
        let state_writer = CompactorStateWriter::new(
            manifest_store,
            compactions_store,
            system_clock.clone(),
            options.as_ref(),
            rand.clone(),
        )
        .await?;
        let compactor_epoch = state_writer.state.manifest().value.compactor_epoch;
        stats.compactor_epoch.set(compactor_epoch as i64);
        Ok(Self {
            state_writer,
            options,
            scheduler,
            rand,
            stats,
            system_clock,
            recorder,
            prev_claimed: HashSet::new(),
            worker_heartbeat_gauges: HashMap::new(),
        })
    }

    fn state(&self) -> &CompactorState {
        &self.state_writer.state
    }

    fn state_mut(&mut self) -> &mut CompactorState {
        &mut self.state_writer.state
    }

    /// Emits the current compaction state and per-job progress.
    fn handle_log_ticker(&self) {
        self.log_compaction_state();
        self.log_compaction_throughput();
    }

    /// Logs per-job compaction progress and updates aggregate throughput metrics.
    fn log_compaction_throughput(&self) {
        let current_time = self.system_clock.now();
        let current_time_ms = current_time.timestamp_millis() as u64;
        let db_state = self.state().db_state();
        let mut total_estimated_bytes = 0u64;
        let mut total_bytes_processed = 0u64;
        let mut total_elapsed_secs = 0.0f64;

        for compaction in self
            .state()
            .active_compactions()
            .filter(|c| c.status() != CompactionStatus::Compacted)
        {
            let estimated_source_bytes =
                Self::calculate_estimated_source_bytes(compaction, db_state);
            total_estimated_bytes += estimated_source_bytes;
            total_bytes_processed += compaction.bytes_processed();

            // Calculate elapsed time using ULID timestamp
            let start_time_ms = compaction
                .id()
                .datetime()
                .duration_since(std::time::UNIX_EPOCH)
                .expect("invalid duration")
                .as_millis() as u64;
            let elapsed_secs = if start_time_ms > 0 {
                (current_time_ms as f64 - start_time_ms as f64) / 1000.0
            } else {
                0.0
            };
            total_elapsed_secs += elapsed_secs;

            // Per-job throughput for logging
            let throughput = if elapsed_secs > 0.0 {
                compaction.bytes_processed() as f64 / elapsed_secs
            } else {
                0.0
            };

            let percentage = if estimated_source_bytes > 0 {
                (compaction.bytes_processed() * 100 / estimated_source_bytes) as u32
            } else {
                0
            };
            debug!(
                "compaction progress [id={}, progress={}%, processed_bytes={}, estimated_source_bytes={}, elapsed={:.2}s, throughput={}/s]",
                compaction.id(),
                percentage,
                format_bytes_si(compaction.bytes_processed()),
                format_bytes_si(estimated_source_bytes),
                elapsed_secs,
                format_bytes_si(throughput as u64),
            );
        }

        let total_throughput = if total_elapsed_secs > 0.0 {
            total_bytes_processed as f64 / total_elapsed_secs
        } else {
            0.0
        };

        self.stats
            .total_bytes_being_compacted
            .set(total_estimated_bytes as i64);
        self.stats.total_throughput.set(total_throughput as i64);
    }

    /// Calculates the estimated total source bytes for a compaction.
    fn calculate_estimated_source_bytes(compaction: &Compaction, db_state: &ManifestCore) -> u64 {
        let tree = db_state
            .tree_for_segment(compaction.spec().segment())
            .expect("compaction target segment missing from manifest");

        let views_by_id: HashMap<Ulid, &SsTableView> =
            tree.l0.iter().map(|view| (view.id, view)).collect();
        let srs_by_id: HashMap<u32, &SortedRun> =
            tree.compacted.iter().map(|sr| (sr.id, sr)).collect();

        compaction
            .spec()
            .sources()
            .iter()
            .map(|source| match source {
                SourceId::SstView(id) => views_by_id
                    .get(id)
                    .expect("compaction source view not found in L0")
                    .estimate_size(),
                SourceId::SortedRun(id) => srs_by_id
                    .get(id)
                    .expect("compaction source sorted run not found")
                    .estimate_size(),
            })
            .sum()
    }

    /// Handles a polling tick by refreshing compactions and the manifest, then possibly scheduling compactions.
    async fn handle_ticker(&mut self) -> Result<(), SlateDBError> {
        self.state_writer.refresh().await?;
        self.reclaim_stale_workers().await?;
        self.update_distributed_compaction_metrics();
        self.commit_compacted_entries().await?;
        self.maybe_schedule_compactions().await?;
        self.maybe_validate_submitted_compactions().await?;
        Ok(())
    }

    /// Reclaims `Running` compactions whose workers have not emitted a heartbeat
    /// within [`CompactorOptions::worker_heartbeat_timeout`], as well as any
    /// `Running` entry with no worker at all. Reclaimed compactions are reset to
    /// `Scheduled` (with no worker) so they can be picked up again while
    /// preserving any persisted job context needed for resume.
    ///
    /// This is the coordinator-side half of the failure-detection protocol described
    /// in RFC-0025. The worker side emits heartbeats (updating `last_heartbeat_ms`)
    /// periodically; the coordinator scans for stale entries on each tick.
    async fn reclaim_stale_workers(&mut self) -> Result<(), SlateDBError> {
        let now_ms = self.system_clock.now().timestamp_millis() as u64;
        let timeout_ms = self.options.worker_heartbeat_timeout.as_millis() as u64;

        // A Running compaction is stale if its worker's heartbeat has aged past
        // the timeout, or if it has no worker at all. The protocol shouldn't
        // produce the latter, but it would otherwise be stuck in Running forever.
        // Capture the owning worker id (None in the no-worker case) for the log.
        let stale: Vec<(Ulid, Option<String>)> = self
            .state()
            .compactions_with_status(&[CompactionStatus::Running])
            .filter(|c| match c.worker() {
                Some(w) => now_ms.saturating_sub(w.last_heartbeat_ms) > timeout_ms,
                None => true,
            })
            .map(|c| (c.id(), c.worker().map(|w| w.worker_id.clone())))
            .collect();

        if stale.is_empty() {
            return Ok(());
        }

        for (id, worker_id) in &stale {
            match worker_id {
                Some(worker_id) => info!(
                    "reclaiming stale compaction whose worker's heartbeat timed out \
                     [worker_id={}, id={}]",
                    worker_id, id
                ),
                None => {
                    let message = format!(
                        "reclaiming Running compaction that has no worker; this should \
                         not happen. please open issue. [id={}]",
                        id
                    );
                    debug_assert!(false, "{message}");
                    error!("{message}");
                }
            }
            self.state_mut().update_compaction(id, |c| {
                c.set_status(CompactionStatus::Scheduled);
                c.set_worker(None);
            });
        }

        self.state_writer.write_compactions_safely().await?;
        self.stats.jobs_reclaimed.increment(stale.len() as u64);
        Ok(())
    }

    /// Updates coordinator-side distributed-compaction metrics after each tick:
    /// - `jobs_claimed`: counts jobs claimed by a worker. A job is "claimed"
    ///   once it carries a worker and is `Running` or `Compacted` (the worker
    ///   keeps its ownership through `Compacted`), so a job that finishes
    ///   execution between two ticks is still counted. The first update counts
    ///   every inherited claim once; later updates count set differences.
    /// - `worker_last_heartbeat_ms`: a per-worker gauge of the last heartbeat
    ///   timestamp. Gauge handles are cached for every worker id observed by this
    ///   coordinator process.
    fn update_distributed_compaction_metrics(&mut self) {
        use crate::compactor::stats::{WORKER_ID_LABEL, WORKER_LAST_HEARTBEAT_MS};

        let claimed: Vec<(Ulid, crate::compactor_state::WorkerSpec)> = self
            .state()
            .compactions_with_status(&[CompactionStatus::Running, CompactionStatus::Compacted])
            .filter_map(|c| c.worker().cloned().map(|w| (c.id(), w)))
            .collect();

        let current_ids: HashSet<Ulid> = claimed.iter().map(|(id, _)| *id).collect();
        // The initially empty set counts all inherited claims once.
        // This includes work claimed while the coordinator was unavailable;
        // thereafter the counter records only newly observed claim ids.
        let newly_claimed = current_ids.difference(&self.prev_claimed).count() as u64;
        if newly_claimed > 0 {
            self.stats.jobs_claimed.increment(newly_claimed);
        }
        self.prev_claimed = current_ids;

        // Refresh the cached gauge handle for every worker that owns an in-flight job.
        let recorder = self.recorder.clone();
        let mut last_heartbeat_per_worker: HashMap<String, u64> = HashMap::new();
        for (_, w) in &claimed {
            last_heartbeat_per_worker
                .entry(w.worker_id.clone())
                .and_modify(|last| *last = (*last).max(w.last_heartbeat_ms))
                .or_insert(w.last_heartbeat_ms);
        }

        for (id, last_heartbeat_ms) in &last_heartbeat_per_worker {
            let gauge = self
                .worker_heartbeat_gauges
                .entry(id.clone())
                .or_insert_with(|| {
                    recorder
                        .gauge(WORKER_LAST_HEARTBEAT_MS)
                        .labels(&[(WORKER_ID_LABEL, id.as_str())])
                        .register()
                });
            gauge.set(*last_heartbeat_ms as i64);
        }
    }

    /// Commits any compactions in the `Compacted` state to the manifest.
    ///
    /// `Compacted` is the distributed intermediate state written by a worker after it
    /// finishes execution. The coordinator is solely responsible for transitioning
    /// `Compacted → Completed` (or `Compacted → Failed`) by writing the manifest first
    /// and then updating `.compactions`. See RFC-0025 §Manifest Commit Protocol.
    ///
    /// Recovery: on coordinator restart, `Compacted` entries are left intact in
    /// `.compactions`. The first call to this method after startup retries the manifest
    /// write. `validate_compaction` will fail if the sources are already absent (meaning
    /// the manifest was written before the crash), in which case the entry is marked
    /// `Failed` even though the compaction may have actually succeeded. This is preferred
    /// over trying to detect success after the fact because any heuristic (e.g. checking
    /// whether the destination SR is present in the manifest) is fragile: the destination
    /// SR id may coincide with a source SR id (e.g. `[L0:1, SR:1] -> SR:1`), so the
    /// destination SR can exist both before and after the compaction runs and its presence
    /// alone does not confirm the compaction's output was committed. Marking `Failed` is
    /// safe in both crash scenarios:
    /// - Crash before manifest write: sources are still present, so the scheduler
    ///   re-compacts them on the next tick.
    /// - Crash after manifest write: sources are already absent and the manifest is
    ///   already in the correct post-compaction state, so marking `Failed` has no
    ///   effect on correctness.
    async fn commit_compacted_entries(&mut self) -> Result<(), SlateDBError> {
        let compacted = self
            .state()
            .compactions_with_status(&[CompactionStatus::Compacted])
            .cloned()
            .collect::<Vec<_>>();

        if compacted.is_empty() {
            return Ok(());
        }

        for compaction in compacted {
            let id = compaction.id();
            match self.validate_compaction(&compaction) {
                Ok(()) => {
                    let destination = compaction
                        .spec()
                        .destination()
                        .expect("Compacted tiered compaction must have a destination SR id");
                    let output_sr = SortedRun {
                        id: destination,
                        sst_views: compaction
                            .output_ssts()
                            .iter()
                            .map(|sst| SsTableView::identity(sst.clone()))
                            .collect(),
                    };
                    self.state_mut().finish_compaction(id, output_sr);
                    self.stats
                        .last_compaction_ts
                        .set(self.system_clock.now().timestamp());
                }
                Err(_) => {
                    // Validation failed against the current manifest. Mark Failed so the
                    // entry isn't retried on the next tick.
                    info!(
                        "compacted entry failed validation, marking Failed [id={}]",
                        id
                    );
                    self.state_mut()
                        .update_compaction(&id, |c| c.set_status(CompactionStatus::Failed));
                }
            }
        }

        self.log_compaction_state();
        self.state_writer.write_state_safely().await?;

        Ok(())
    }

    /// Validates a Submitted compaction against the current manifest before starting it.
    ///
    /// Runs when a Submitted compaction is accepted and when a Compacted entry is
    /// committed, so this is the canonical gate for compaction-against-current-state
    /// validity. Cross-compaction conflicts (destination collisions across active
    /// compactions, concurrent drains on the same segment) are enforced upstream in
    /// [`CompactorState::add_compaction`] and are not re-checked here.
    ///
    /// Invariants checked:
    /// - Compaction has sources
    /// - Drain specs do not target the empty-prefix (root) segment
    /// - The target segment exists in the manifest
    /// - All sources exist in the target segment's tree
    /// - Submitted L0-only tiered compactions have a destination > highest SR id across all trees
    /// - Compacted L0-only tiered compactions have a destination > highest SR id in their segment
    /// - A tiered destination does not overwrite a committed SR in any tree unless the SR is among sources
    /// - Drain L0 sources cover every L0 at or below the newest drained L0 in the target tree
    /// - At most one L0 compaction is Running per segment
    /// - Scheduler-specific policy via [`CompactionScheduler::validate_compaction`]
    fn validate_compaction(&self, compaction: &Compaction) -> Result<(), SlateDBError> {
        let spec = compaction.spec();
        // Validate compaction sources exist
        if spec.sources().is_empty() {
            warn!("submitted compaction is empty: {:?}", spec.sources());
            return Err(SlateDBError::InvalidCompaction);
        }

        // Drain specs cannot target the empty-prefix segment (RFC-0024): the
        // root-tree compatibility encoding is not retired by drain.
        if spec.is_drain() && spec.segment().is_empty() {
            warn!("rejected drain compaction targeting the empty-prefix segment");
            return Err(SlateDBError::InvalidCompaction);
        }

        // Validate compaction sources exist in the spec's target segment tree.
        // RFC-0024: every spec names exactly one segment, and its sources must
        // live in that segment's tree.
        let db_state = self.state().db_state();
        let Some(tree) = db_state.tree_for_segment(spec.segment()) else {
            warn!(
                "submitted compaction targets unknown segment: {:?}",
                spec.segment()
            );
            return Err(SlateDBError::InvalidCompaction);
        };
        let l0_view_ids = tree
            .l0
            .iter()
            .map(|view| view.id)
            .collect::<std::collections::HashSet<_>>();
        let sr_ids = tree
            .compacted
            .iter()
            .map(|sr| sr.id)
            .collect::<std::collections::HashSet<_>>();

        if let Some(missing) = spec.sources().iter().find(|source| match source {
            SourceId::SstView(id) => !l0_view_ids.contains(id),
            SourceId::SortedRun(id) => !sr_ids.contains(id),
        }) {
            warn!("compaction source missing from db state: {:?}", missing);
            return Err(SlateDBError::InvalidCompaction);
        }

        // Validate L0-only compactions create a new SR after every SR that matters
        // for their lifecycle stage. Submitted specs reserve a fresh globally
        // unique SR id, so they must be above every committed SR in every segment.
        // Compacted entries are being committed into one target segment and may
        // validly finish after a different segment has already committed a higher
        // SR id, but they still must preserve local SR ordering within the target
        // segment.
        if spec.has_l0_sources() && !spec.has_sr_sources() {
            let highest_id = if compaction.status() == CompactionStatus::Submitted {
                db_state
                    .trees()
                    .flat_map(|t| t.compacted.iter())
                    .map(|sr| sr.id)
                    .max()
                    .map_or(0, |id| id + 1)
            } else if compaction.status() == CompactionStatus::Compacted {
                tree.compacted
                    .iter()
                    .map(|sr| sr.id)
                    .max()
                    .map_or(0, |id| id + 1)
            } else {
                warn!(
                    "validate_compaction called with unexpected compaction status [id={:?}, status={:?}]",
                    compaction.id(),
                    compaction.status()
                );
                return Err(SlateDBError::InvalidCompaction);
            };
            // Drain specs have no destination and aren't subject to this check.
            if let Some(dst) = spec.destination() {
                if dst < highest_id {
                    warn!(
                        "compaction destination is lesser than the expected L0-only highest_id: {:?} {:?}",
                        dst, highest_id
                    );
                    return Err(SlateDBError::InvalidCompaction);
                }
            }
        }

        Self::validate_destination_overwrite(spec, db_state)?;
        Self::validate_drain_watermark_advance(spec, tree)?;

        // Reject parallel L0 compactions within the same segment. Each
        // segment owns its own `last_compacted_l0_sst_view_id` watermark
        // (RFC-0024), so out-of-order completion is only a hazard for
        // compactions sharing a target tree. L0 compactions in disjoint
        // segments — including drain specs in different segments — are
        // safe to run concurrently.
        if spec.has_l0_sources() {
            let target_segment = spec.segment();
            // Only Scheduled and Running represent live claims; Submitted is still
            // being validated (and would see itself), Compacted is pending commit.
            let active_l0_in_same_segment = self
                .state()
                .compactions_with_status(&[CompactionStatus::Scheduled, CompactionStatus::Running])
                .any(|c| c.spec().has_l0_sources() && c.spec().segment() == target_segment);
            if active_l0_in_same_segment {
                warn!(
                    "rejected compaction: parallel L0 compaction already active in segment {:?}",
                    target_segment
                );
                return Err(SlateDBError::InvalidCompaction);
            }
        }

        self.scheduler
            .validate(&self.state().into(), spec)
            .map_err(|_e| SlateDBError::InvalidCompaction)
    }

    /// Rejects a tiered compaction whose destination SR id already exists as a
    /// committed SR anywhere in the manifest unless that SR is among the spec's
    /// sources (i.e. the compaction overwrites it as part of the merge). SR ids
    /// are globally unique across all segment trees (RFC-0024). Drain specs
    /// produce no output SR and pass through unchecked.
    fn validate_destination_overwrite(
        compaction: &CompactionSpec,
        db_state: &ManifestCore,
    ) -> Result<(), SlateDBError> {
        let Some(dst) = compaction.destination() else {
            return Ok(());
        };
        let sr_exists_anywhere = db_state
            .trees()
            .flat_map(|t| t.compacted.iter())
            .any(|sr| sr.id == dst);
        let sr_in_sources = compaction.sources().iter().any(|src| match src {
            SourceId::SortedRun(sr) => *sr == dst,
            SourceId::SstView(_) => false,
        });
        if sr_exists_anywhere && !sr_in_sources {
            warn!(
                "compaction destination overwrites committed SR not in sources: {:?}",
                dst
            );
            return Err(SlateDBError::InvalidCompaction);
        }
        Ok(())
    }

    /// Rejects a drain spec that would leave one or more L0s below the new
    /// watermark. The watermark advances to the newest drained L0; every L0
    /// at or below that point in `tree.l0` (which is sorted newest-first)
    /// must be listed among the drain sources, or it would survive in
    /// `tree.l0` while being eclipsed by the watermark — a manifest state
    /// the writer cannot merge. Tiered specs do not advance the watermark
    /// and pass through unchecked.
    fn validate_drain_watermark_advance(
        compaction: &CompactionSpec,
        tree: &LsmTreeState,
    ) -> Result<(), SlateDBError> {
        if !compaction.is_drain() {
            return Ok(());
        }
        let drained_l0_ids: std::collections::HashSet<ulid::Ulid> = compaction
            .sources()
            .iter()
            .filter_map(|s| match s {
                SourceId::SstView(id) => Some(*id),
                SourceId::SortedRun(_) => None,
            })
            .collect();
        if drained_l0_ids.is_empty() {
            return Ok(());
        }
        // tree.l0 is sorted newest-first. Find the newest drained L0's index;
        // every L0 at or after that index (older or equal) must also be drained.
        let Some(newest_drained_idx) = tree
            .l0
            .iter()
            .position(|view| drained_l0_ids.contains(&view.id))
        else {
            return Ok(());
        };
        for view in tree.l0.iter().skip(newest_drained_idx) {
            if !drained_l0_ids.contains(&view.id) {
                warn!(
                    "drain spec leaves L0 below the new watermark: segment={:?}, surviving_l0={:?}",
                    compaction.segment(),
                    view.id
                );
                return Err(SlateDBError::InvalidCompaction);
            }
        }
        Ok(())
    }

    /// Requests new compactions from the scheduler, validates them, and adds them to the
    /// state up to the the max concurrency limit. This method does not actually start
    /// the compactions; that is done in [`CompactorEventHandler::maybe_start_compactions`].
    async fn maybe_schedule_compactions(&mut self) -> Result<(), SlateDBError> {
        let running_compaction_count = self.running_compaction_count();
        let available_capacity = self.options.max_concurrent_compactions - running_compaction_count;

        if available_capacity == 0 {
            debug!(
                "skipping compaction scheduling since at capacity [running_compactions={}, max_concurrent_compactions={}]",
                running_compaction_count,
                self.options.max_concurrent_compactions
            );
            return Ok(());
        }

        let mut specs = self.scheduler.propose(&self.state().into());

        // Add new compactions up to the max concurrency limit
        let num_specs_added = specs
            .drain(..available_capacity.min(specs.len()))
            .map(|spec| -> Result<(), SlateDBError> {
                let compaction_id = self.rand.rng().gen_ulid(self.system_clock.as_ref());
                debug!(
                    "scheduling new compaction [spec={:?}, id={}]",
                    spec, compaction_id
                );
                self.state_mut()
                    .add_compaction(Compaction::new(compaction_id, spec))?;
                Ok(())
            })
            .count();

        // Save newly added compactions
        if num_specs_added > 0 {
            self.state_writer.write_compactions_safely().await?;
        }

        Ok(())
    }

    /// Validates every `Submitted` compaction against the current manifest and
    /// promotes the valid tiered specs to `Scheduled` — the coordinator's
    /// "ready for a worker to claim" state. Drain specs short-circuit the
    /// executor and are applied directly to the in-memory manifest (→
    /// `Completed`). Invalid specs are marked `Failed`. State changes are
    /// persisted before any worker (including the local executor) can act on
    /// them: when any submission was a drain the manifest and `.compactions`
    /// are written together, otherwise `.compactions` alone.
    ///
    /// Workers exclusively claim `Scheduled` entries; they never act on
    /// `Submitted`. Routing validation through this single chokepoint keeps
    /// the coordinator the gatekeeper for spec-against-manifest validity and
    /// guarantees the coordinator has the entry in local state before any
    /// remote worker can transition it onward.
    async fn maybe_validate_submitted_compactions(&mut self) -> Result<(), SlateDBError> {
        let submitted_compactions = self
            .state()
            .compactions_with_status(&[CompactionStatus::Submitted])
            .cloned()
            .collect::<Vec<_>>();

        if submitted_compactions.is_empty() {
            return Ok(());
        }

        let any_drain = submitted_compactions.iter().any(|c| c.spec().is_drain());

        for compaction in &submitted_compactions {
            // Validate the candidate compaction; mark as failed if invalid.
            if let Err(e) = self.validate_compaction(compaction) {
                error!(
                    "compaction validation failed [error={:?}, compaction={:?}]",
                    compaction, e
                );
                self.state_mut().update_compaction(&compaction.id(), |c| {
                    c.set_status(CompactionStatus::Failed)
                });
                continue;
            }

            // Drain specs apply the watermark advance and SR removal directly,
            // marking the compaction Completed. They never enter Scheduled
            // because no worker runs them. Tiered specs become Scheduled so a
            // worker can claim them.
            if compaction.spec().is_drain() {
                self.state_mut().finish_drain_compaction(compaction.id());
            } else {
                self.state_mut().update_compaction(&compaction.id(), |c| {
                    c.clear_ctx();
                    c.set_status(CompactionStatus::Scheduled)
                });
            }
        }

        if any_drain {
            self.state_writer.write_state_safely().await?;
        } else {
            self.state_writer.write_compactions_safely().await?;
        }

        Ok(())
    }

    /// Records a failed compaction attempt.
    #[allow(dead_code)]
    async fn finish_failed_compaction(&mut self, id: Ulid) -> Result<(), SlateDBError> {
        self.state_mut()
            .update_compaction(&id, |c| c.set_status(CompactionStatus::Failed));
        self.state_writer.write_compactions_safely().await?;
        Ok(())
    }

    /// Records a successful compaction, persists the manifest, and checks for new compactions
    /// to schedule.
    #[allow(dead_code)]
    #[instrument(level = "debug", skip_all, fields(id = %id))]
    async fn finish_compaction(
        &mut self,
        id: Ulid,
        output_sr: SortedRun,
    ) -> Result<(), SlateDBError> {
        self.state_mut().finish_compaction(id, output_sr);
        self.log_compaction_state();
        self.state_writer.write_state_safely().await?;
        self.maybe_schedule_compactions().await?;
        self.maybe_validate_submitted_compactions().await?;
        self.stats
            .last_compaction_ts
            .set(self.system_clock.now().timestamp());
        Ok(())
    }

    /// Logs the current DB runs and in-flight compactions.
    fn log_compaction_state(&self) {
        self.state().db_state().log_db_runs();
        let compactions = self.state().active_compactions();
        for compaction in compactions {
            if log::log_enabled!(log::Level::Debug) {
                debug!("in-flight compaction [compaction={:?}]", compaction);
            } else {
                info!("in-flight compaction [compaction={}]", compaction);
            }
        }
    }

    fn running_compaction_count(&self) -> usize {
        self.state()
            .active_compactions()
            .filter(|c| c.status() == CompactionStatus::Running)
            .count()
    }
}

pub mod stats {
    use slatedb_common::metrics::{CounterFn, GaugeFn, MetricsRecorderHelper, UpDownCounterFn};
    use std::sync::Arc;

    pub use crate::merge_operator::MERGE_OPERATOR_OPERANDS;

    use crate::merge_operator::{
        MERGE_OPERATOR_COMPACT_PATH, MERGE_OPERATOR_OPERANDS_DESCRIPTION, MERGE_OPERATOR_PATH_LABEL,
    };

    macro_rules! compactor_stat_name {
        ($suffix:expr) => {
            concat!("slatedb.compactor.", $suffix)
        };
    }

    pub const BYTES_COMPACTED: &str = compactor_stat_name!("bytes_compacted");
    pub const COMPACTOR_EPOCH: &str = compactor_stat_name!("epoch");
    pub const LAST_COMPACTION_TS_SEC: &str = compactor_stat_name!("last_compaction_timestamp_sec");
    pub const RUNNING_COMPACTIONS: &str = compactor_stat_name!("running_compactions");
    pub const SSTS_WRITTEN: &str = compactor_stat_name!("ssts_written");
    pub const JOBS_CLAIMED: &str = compactor_stat_name!("jobs_claimed");
    pub const JOBS_RECLAIMED: &str = compactor_stat_name!("jobs_reclaimed");
    pub const WORKER_LAST_HEARTBEAT_MS: &str = compactor_stat_name!("worker_last_heartbeat_ms");
    /// Label key carrying a worker's id on per-worker metrics.
    pub const WORKER_ID_LABEL: &str = "worker_id";
    pub const TOTAL_BYTES_BEING_COMPACTED: &str =
        compactor_stat_name!("total_bytes_being_compacted");
    pub const TOTAL_THROUGHPUT_BYTES_PER_SEC: &str =
        compactor_stat_name!("total_throughput_bytes_per_sec");
    pub const EXPIRED_ENTRIES_PURGED: &str = compactor_stat_name!("expired_entries_purged");
    pub const EXPIRED_ENTRIES_PURGED_DESCRIPTION: &str =
        "Count of expired entries purged by the RetentionIterator. \
         `entry_type=\"value\"` counts expired value entries rewritten as tombstones; \
         `entry_type=\"merge\"` counts expired merge entries dropped without a tombstone.";
    pub const ENTRY_TYPE_LABEL: &str = "entry_type";
    pub const ENTRY_TYPE_VALUE: &str = "value";
    pub const ENTRY_TYPE_MERGE: &str = "merge";

    /// Coordinator-side compaction metrics.
    ///
    /// Per-worker throughput (`bytes_compacted`, `running_compactions`,
    /// `ssts_written`) lives in [`WorkerStats`] instead: those are emitted by the
    /// executor running inside a worker and tagged with `{worker_id}`. The
    /// coordinator runs no executor, so it does not emit them (RFC-0025
    /// Observability).
    pub(crate) struct CompactionStats {
        pub(crate) compactor_epoch: Arc<dyn GaugeFn>,
        pub(crate) last_compaction_ts: Arc<dyn GaugeFn>,
        pub(crate) total_bytes_being_compacted: Arc<dyn GaugeFn>,
        pub(crate) total_throughput: Arc<dyn GaugeFn>,
        pub(crate) merge_operator_compact_operands: Arc<dyn CounterFn>,
        pub(crate) expired_entries_purged_value: Arc<dyn CounterFn>,
        pub(crate) expired_entries_purged_merge: Arc<dyn CounterFn>,
        /// `Scheduled → Running` transitions the coordinator observed in `.compactions`.
        pub(crate) jobs_claimed: Arc<dyn CounterFn>,
        /// Stale jobs the coordinator reset `Running → Submitted`.
        pub(crate) jobs_reclaimed: Arc<dyn CounterFn>,
    }

    impl CompactionStats {
        pub(crate) fn new(recorder: &MetricsRecorderHelper) -> Self {
            Self {
                compactor_epoch: recorder.gauge(COMPACTOR_EPOCH).register(),
                last_compaction_ts: recorder.gauge(LAST_COMPACTION_TS_SEC).register(),
                jobs_claimed: recorder.counter(JOBS_CLAIMED).register(),
                jobs_reclaimed: recorder.counter(JOBS_RECLAIMED).register(),
                total_bytes_being_compacted: recorder.gauge(TOTAL_BYTES_BEING_COMPACTED).register(),
                total_throughput: recorder.gauge(TOTAL_THROUGHPUT_BYTES_PER_SEC).register(),
                merge_operator_compact_operands: recorder
                    .counter(MERGE_OPERATOR_OPERANDS)
                    .labels(&[(MERGE_OPERATOR_PATH_LABEL, MERGE_OPERATOR_COMPACT_PATH)])
                    .description(MERGE_OPERATOR_OPERANDS_DESCRIPTION)
                    .register(),
                expired_entries_purged_value: recorder
                    .counter(EXPIRED_ENTRIES_PURGED)
                    .labels(&[(ENTRY_TYPE_LABEL, ENTRY_TYPE_VALUE)])
                    .description(EXPIRED_ENTRIES_PURGED_DESCRIPTION)
                    .register(),
                expired_entries_purged_merge: recorder
                    .counter(EXPIRED_ENTRIES_PURGED)
                    .labels(&[(ENTRY_TYPE_LABEL, ENTRY_TYPE_MERGE)])
                    .description(EXPIRED_ENTRIES_PURGED_DESCRIPTION)
                    .register(),
            }
        }

        pub(crate) fn retention_metrics(&self) -> crate::retention_iterator::RetentionMetrics {
            crate::retention_iterator::RetentionMetrics {
                expired_entries_purged_value: self.expired_entries_purged_value.clone(),
                expired_entries_purged_merge: self.expired_entries_purged_merge.clone(),
            }
        }
    }

    /// Per-worker compaction throughput, tagged `{worker_id=<id>}`.
    ///
    /// Registered once per worker from the worker's recorder with the worker id
    /// baked into the label set, and incremented by the executor (which always
    /// runs inside a worker). A shared metrics backend can therefore attribute
    /// throughput to individual workers in multi-worker deployments.
    #[derive(Clone)]
    pub(crate) struct WorkerStats {
        /// Bytes written to output SSTs by this worker (cumulative).
        pub(crate) bytes_compacted: Arc<dyn CounterFn>,
        /// Compaction jobs currently executing on this worker.
        pub(crate) running_compactions: Arc<dyn UpDownCounterFn>,
        /// Output SSTs produced by this worker (cumulative).
        pub(crate) ssts_written: Arc<dyn CounterFn>,
    }

    impl WorkerStats {
        pub(crate) fn new(recorder: &MetricsRecorderHelper, worker_id: &str) -> Self {
            Self {
                bytes_compacted: recorder
                    .counter(BYTES_COMPACTED)
                    .labels(&[(WORKER_ID_LABEL, worker_id)])
                    .register(),
                running_compactions: recorder
                    .up_down_counter(RUNNING_COMPACTIONS)
                    .labels(&[(WORKER_ID_LABEL, worker_id)])
                    .register(),
                ssts_written: recorder
                    .counter(SSTS_WRITTEN)
                    .labels(&[(WORKER_ID_LABEL, worker_id)])
                    .register(),
            }
        }

        /// A no-op instance for tests that don't assert on worker metrics.
        #[cfg(test)]
        pub(crate) fn noop() -> Self {
            Self::new(&MetricsRecorderHelper::noop(), "")
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::{HashMap, VecDeque};
    use std::future::Future;
    use std::sync::Arc;
    use std::time::{Duration, SystemTime};

    use object_store::memory::InMemory;
    use object_store::path::Path;
    use object_store::ObjectStore;
    use parking_lot::Mutex;
    use rand::RngCore;
    use slatedb_common::MockSystemClock;
    use ulid::Ulid;

    use super::*;
    use crate::compaction_worker::WorkerMessage;
    use crate::compactions_store::{FenceableCompactions, StoredCompactions};
    use crate::compactor::stats::CompactionStats;
    use crate::compactor::stats::COMPACTOR_EPOCH;
    use crate::compactor::stats::LAST_COMPACTION_TS_SEC;
    use crate::compactor_executor::{
        CompactionExecutor, TokioCompactionExecutor, TokioCompactionExecutorOptions,
    };
    use crate::compactor_state::Compaction;
    use crate::compactor_state::CompactionStatus;
    use crate::compactor_state::{SourceId, WorkerSpec};
    use crate::config::{
        CompactionWorkerOptions, FlushOptions, FlushType, MergeOptions, PutOptions, Settings,
        SizeTieredCompactionSchedulerOptions, Ttl, WriteOptions,
    };
    use crate::db::Db;
    use crate::db_state::{SortedRun, SsTableHandle, SsTableId, SsTableInfo, SsTableView};
    use crate::error::SlateDBError;
    use crate::format::sst::{SsTableFormat, SST_FORMAT_VERSION_LATEST};
    use crate::iter::RowEntryIterator;
    use crate::manifest::store::{ManifestStore, StoredManifest};
    use crate::manifest::{LsmTreeState, Manifest, ManifestCore, Segment, VersionedManifest};
    use crate::merge_operator::{MergeOperator, MergeOperatorError};
    use crate::object_stores::ObjectStores;
    use crate::proptest_util::rng;
    use crate::sst_iter::{SstIterator, SstIteratorOptions};
    use crate::tablestore::{TableStore, TableStoreKind};
    use crate::test_utils::{assert_iterator, FixedThreeBytePrefixExtractor, GatedObjectStore};
    use crate::types::KeyValue;
    use crate::types::RowEntry;
    use bytes::Bytes;
    use slatedb_common::clock::{DefaultSystemClock, SystemClock};

    const PATH: &str = "/test/db";

    #[derive(Clone)]
    struct SegmentTestScheduler {
        segment: Bytes,
        min_l0_sources: usize,
    }

    impl CompactionScheduler for SegmentTestScheduler {
        fn propose(&self, state: &CompactorStateView) -> Vec<CompactionSpec> {
            let db_state = state.manifest().core();
            let Some(tree) = db_state.tree_for_segment(self.segment.as_ref()) else {
                return vec![];
            };
            if tree.l0.len() < self.min_l0_sources {
                return vec![];
            }

            let mut sources: Vec<SourceId> = tree
                .l0
                .iter()
                .map(|view| SourceId::SstView(view.id))
                .collect();
            sources.extend(tree.compacted.iter().map(|sr| SourceId::SortedRun(sr.id)));

            let destination = db_state
                .trees()
                .flat_map(|tree| tree.compacted.iter().map(|sr| sr.id))
                .max()
                .map(|id| id.saturating_add(1))
                .unwrap_or(0);

            vec![CompactionSpec::for_segment(
                self.segment.clone(),
                sources,
                destination,
            )]
        }
    }

    struct SegmentTestSchedulerSupplier {
        scheduler: SegmentTestScheduler,
    }

    impl SegmentTestSchedulerSupplier {
        fn new(segment: Bytes, min_l0_sources: usize) -> Self {
            Self {
                scheduler: SegmentTestScheduler {
                    segment,
                    min_l0_sources,
                },
            }
        }
    }

    impl CompactionSchedulerSupplier for SegmentTestSchedulerSupplier {
        fn compaction_scheduler(
            &self,
            _options: &CompactorOptions,
        ) -> Box<dyn CompactionScheduler + Send + Sync> {
            Box::new(self.scheduler.clone())
        }
    }

    /// Test scheduler that proposes a single drain spec for the target
    /// segment, listing every observed L0 and SR as a source. Used to drive
    /// the segment-drain e2e path; there is no admin-triggered drain API.
    #[derive(Clone)]
    struct SegmentDrainTestScheduler {
        segment: Bytes,
    }

    impl CompactionScheduler for SegmentDrainTestScheduler {
        fn propose(&self, state: &CompactorStateView) -> Vec<CompactionSpec> {
            let db_state = state.manifest().core();
            let Some(tree) = db_state.tree_for_segment(self.segment.as_ref()) else {
                return vec![];
            };
            if tree.l0.is_empty() && tree.compacted.is_empty() {
                return vec![];
            }
            let active_drain = state
                .compactions()
                .map(|compactions| {
                    compactions.recent_compactions().any(|c| {
                        c.active() && c.spec().is_drain() && c.spec().segment() == &self.segment
                    })
                })
                .unwrap_or(false);
            if active_drain {
                return vec![];
            }
            let mut sources: Vec<SourceId> = tree
                .l0
                .iter()
                .map(|view| SourceId::SstView(view.id))
                .collect();
            sources.extend(tree.compacted.iter().map(|sr| SourceId::SortedRun(sr.id)));
            vec![CompactionSpec::drain_segment(self.segment.clone(), sources)]
        }
    }

    struct SegmentDrainTestSchedulerSupplier {
        scheduler: SegmentDrainTestScheduler,
    }

    impl SegmentDrainTestSchedulerSupplier {
        fn new(segment: Bytes) -> Self {
            Self {
                scheduler: SegmentDrainTestScheduler { segment },
            }
        }
    }

    impl CompactionSchedulerSupplier for SegmentDrainTestSchedulerSupplier {
        fn compaction_scheduler(
            &self,
            _options: &CompactorOptions,
        ) -> Box<dyn CompactionScheduler + Send + Sync> {
            Box::new(self.scheduler.clone())
        }
    }

    struct StringConcatMergeOperator;

    impl MergeOperator for StringConcatMergeOperator {
        fn merge(
            &self,
            _key: &Bytes,
            existing_value: Option<Bytes>,
            value: Bytes,
        ) -> Result<Bytes, MergeOperatorError> {
            let mut result = existing_value.unwrap_or_default().as_ref().to_vec();
            result.extend_from_slice(&value);
            Ok(Bytes::from(result))
        }
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_compactor_compacts_l0() {
        // given:
        let os = Arc::new(InMemory::new());
        let system_clock = Arc::new(MockSystemClock::new());
        let mut options = db_options(Some(compactor_options()));
        // Bigger than all writes so we keep all writes in a single SST.
        // This makes it easier to verify all KV pairs are in that SST.
        options.l0_sst_size_bytes = 512;
        let scheduler_options = SizeTieredCompactionSchedulerOptions {
            min_compaction_sources: 1,
            max_compaction_sources: 999,
            include_size_threshold: 4.0,
        }
        .into();
        options
            .compactor_options
            .as_mut()
            .expect("compactor options must be set")
            .scheduler_options = scheduler_options;

        let db = Db::builder(PATH, os.clone())
            .with_settings(options)
            .with_system_clock(system_clock.clone())
            .build()
            .await
            .unwrap();

        let (_, _, table_store) = build_test_stores(os.clone());
        let mut expected = HashMap::<Vec<u8>, Vec<u8>>::new();
        for i in 0..4 {
            let k = vec![b'a' + i as u8; 16];
            let v = vec![b'b' + i as u8; 48];
            expected.insert(k.clone(), v.clone());
            db.put_with_options(
                &k,
                &v,
                &PutOptions::default(),
                &WriteOptions {
                    await_durable: false,
                    ..Default::default()
                },
            )
            .await
            .unwrap();
            let k = vec![b'j' + i as u8; 16];
            let v = vec![b'k' + i as u8; 48];
            db.put_with_options(
                &k,
                &v,
                &PutOptions::default(),
                &WriteOptions {
                    await_durable: false,
                    ..Default::default()
                },
            )
            .await
            .unwrap();
            expected.insert(k.clone(), v.clone());
        }

        // Force all writes to a single L0 SST.
        db.flush_with_options(FlushOptions {
            flush_type: FlushType::MemTable,
        })
        .await
        .unwrap();

        // when:
        let db_state = await_compaction(&db, os.clone(), Some(system_clock.clone())).await;

        // then:
        let db_state = db_state.expect("db was not compacted");
        for run in db_state.tree.compacted.iter() {
            for sst in run.sst_views.iter() {
                let mut iter = SstIterator::new_borrowed_initialized(
                    ..,
                    sst,
                    table_store.clone(),
                    SstIteratorOptions::default(),
                )
                .await
                .unwrap()
                .expect("Expected Some(iter) but got None");

                // remove the key from the expected map and verify that the db matches
                while let Some(kv) = iter.next().await.unwrap().map(KeyValue::from) {
                    let expected_v = expected
                        .remove(kv.key.as_ref())
                        .expect("removing unexpected key");
                    let db_v = db.get(kv.key.as_ref()).await.unwrap().unwrap();
                    assert_eq!(expected_v, db_v.as_ref());
                }
            }
        }
        assert!(expected.is_empty());
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_compactor_compacts_only_target_segment() {
        let os = Arc::new(InMemory::new());
        let system_clock = Arc::new(MockSystemClock::new());
        let path = "/tmp/test_compactor_compacts_only_target_segment";
        let mut options = db_options(Some(compactor_options()));
        options.l0_sst_size_bytes = 512;
        options.flush_interval = None;

        let db = Db::builder(path, os.clone())
            .with_settings(options.clone())
            .with_system_clock(system_clock.clone())
            .with_segment_extractor(Arc::new(FixedThreeBytePrefixExtractor))
            .with_compactor_builder(
                CompactorBuilder::new(path, os.clone())
                    .with_options(compactor_options())
                    .with_scheduler_supplier(Arc::new(SegmentTestSchedulerSupplier::new(
                        Bytes::from_static(b"aaa"),
                        2,
                    ))),
            )
            .build()
            .await
            .unwrap();

        for (key, value) in [
            (b"aaa-001".as_slice(), b"v1".as_slice()),
            (b"aaa-002".as_slice(), b"v2".as_slice()),
            (b"aaa-003".as_slice(), b"v3".as_slice()),
            (b"bbb-001".as_slice(), b"v4".as_slice()),
        ] {
            put_and_flush_memtable(&db, key, value).await;
        }

        let db_state = run_for(Duration::from_secs(10), || async {
            system_clock
                .as_ref()
                .advance(Duration::from_millis(60000))
                .await;
            let core = read_db_state_core(&db);
            let aaa = core
                .segments
                .iter()
                .find(|segment| segment.prefix.as_ref() == b"aaa")
                .expect("missing segment aaa");
            let bbb = core
                .segments
                .iter()
                .find(|segment| segment.prefix.as_ref() == b"bbb")
                .expect("missing segment bbb");
            if aaa.tree.l0.is_empty()
                && !aaa.tree.compacted.is_empty()
                && bbb.tree.l0.len() == 1
                && bbb.tree.compacted.is_empty()
            {
                Some(core)
            } else {
                None
            }
        })
        .await
        .expect("segment-scoped compaction did not complete");

        let aaa = db_state
            .segments
            .iter()
            .find(|segment| segment.prefix.as_ref() == b"aaa")
            .expect("missing segment aaa");
        let bbb = db_state
            .segments
            .iter()
            .find(|segment| segment.prefix.as_ref() == b"bbb")
            .expect("missing segment bbb");
        assert!(aaa.tree.l0.is_empty());
        assert_eq!(aaa.tree.compacted.len(), 1);
        assert_eq!(bbb.tree.l0.len(), 1);
        assert!(bbb.tree.compacted.is_empty());

        for (key, value) in [
            (b"aaa-001".as_slice(), b"v1".as_slice()),
            (b"aaa-002".as_slice(), b"v2".as_slice()),
            (b"aaa-003".as_slice(), b"v3".as_slice()),
            (b"bbb-001".as_slice(), b"v4".as_slice()),
        ] {
            assert_eq!(
                db.get(key).await.unwrap(),
                Some(Bytes::copy_from_slice(value))
            );
        }
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_compactor_compacts_all_segments_with_size_tiered_scheduler() {
        let os = Arc::new(InMemory::new());
        let system_clock = Arc::new(MockSystemClock::new());
        let path = "/tmp/test_compactor_compacts_all_segments_with_size_tiered_scheduler";
        let mut options = db_options(Some(compactor_options()));
        options.l0_sst_size_bytes = 512;
        options.flush_interval = None;
        let scheduler_options = SizeTieredCompactionSchedulerOptions {
            min_compaction_sources: 2,
            max_compaction_sources: 999,
            include_size_threshold: 4.0,
        }
        .into();
        let compactor_opts = options
            .compactor_options
            .as_mut()
            .expect("compactor options must be set");
        compactor_opts.scheduler_options = scheduler_options;
        compactor_opts.max_concurrent_compactions = 3;

        let db = Db::builder(path, os.clone())
            .with_settings(options)
            .with_system_clock(system_clock.clone())
            .with_segment_extractor(Arc::new(FixedThreeBytePrefixExtractor))
            .build()
            .await
            .unwrap();

        let entries: &[(&[u8], &[u8])] = &[
            (b"aaa-001", b"v1"),
            (b"aaa-002", b"v2"),
            (b"bbb-001", b"v3"),
            (b"bbb-002", b"v4"),
            (b"ccc-001", b"v5"),
            (b"ccc-002", b"v6"),
        ];

        for (key, value) in entries {
            put_and_flush_memtable(&db, key, value).await;
        }

        let prefixes: [&[u8]; 3] = [b"aaa", b"bbb", b"ccc"];

        let db_state = run_for(Duration::from_secs(10), || async {
            system_clock
                .as_ref()
                .advance(Duration::from_millis(60000))
                .await;
            let core = read_db_state_core(&db);
            let all_compacted = prefixes.iter().all(|prefix| {
                core.segments
                    .iter()
                    .find(|segment| segment.prefix.as_ref() == *prefix)
                    .map(|segment| segment.tree.l0.is_empty() && !segment.tree.compacted.is_empty())
                    .unwrap_or(false)
            });
            if all_compacted {
                Some(core)
            } else {
                None
            }
        })
        .await
        .expect("not every segment compacted via size-tiered scheduler");

        for prefix in prefixes {
            let segment = db_state
                .segments
                .iter()
                .find(|segment| segment.prefix.as_ref() == prefix)
                .unwrap_or_else(|| panic!("missing segment {:?}", prefix));
            assert!(
                segment.tree.l0.is_empty(),
                "segment {:?} still has L0 SSTs",
                prefix
            );
            assert_eq!(
                segment.tree.compacted.len(),
                1,
                "segment {:?} should have a single sorted run",
                prefix
            );
        }

        for (key, value) in entries {
            assert_eq!(
                db.get(key).await.unwrap(),
                Some(Bytes::copy_from_slice(value))
            );
        }
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_compactor_drains_segment() {
        let os = Arc::new(InMemory::new());
        let system_clock = Arc::new(MockSystemClock::new());
        let path = "/tmp/test_compactor_drains_segment";
        let mut options = db_options(Some(compactor_options()));
        options.l0_sst_size_bytes = 512;
        options.flush_interval = None;

        let db = Db::builder(path, os.clone())
            .with_settings(options.clone())
            .with_system_clock(system_clock.clone())
            .with_segment_extractor(Arc::new(FixedThreeBytePrefixExtractor))
            .with_compactor_builder(
                CompactorBuilder::new(path, os.clone())
                    .with_options(compactor_options())
                    .with_scheduler_supplier(Arc::new(SegmentDrainTestSchedulerSupplier::new(
                        Bytes::from_static(b"aaa"),
                    ))),
            )
            .build()
            .await
            .unwrap();

        for (key, value) in [
            (b"aaa-001".as_slice(), b"v1".as_slice()),
            (b"aaa-002".as_slice(), b"v2".as_slice()),
            (b"bbb-001".as_slice(), b"v3".as_slice()),
        ] {
            put_and_flush_memtable(&db, key, value).await;
        }

        // The marker is pruned on the writer's next commit, so periodically
        // write a fresh bbb-* key to nudge the writer-side merge.
        let nudge = Arc::new(std::sync::atomic::AtomicU32::new(0));
        let db_state = run_for(Duration::from_secs(10), || {
            let db = &db;
            let system_clock = system_clock.clone();
            let nudge_value = nudge.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            async move {
                system_clock
                    .as_ref()
                    .advance(Duration::from_millis(60000))
                    .await;
                let key = format!("bbb-nudge-{:04}", nudge_value);
                put_and_flush_memtable(db, key.as_bytes(), b"nudge").await;
                let core = read_db_state_core(db);
                let aaa_gone = !core
                    .segments
                    .iter()
                    .any(|segment| segment.prefix.as_ref() == b"aaa");
                let bbb_present = core
                    .segments
                    .iter()
                    .any(|segment| segment.prefix.as_ref() == b"bbb");
                if aaa_gone && bbb_present {
                    Some(core)
                } else {
                    None
                }
            }
        })
        .await
        .expect("drain marker for segment aaa was not pruned");

        assert!(
            !db_state
                .segments
                .iter()
                .any(|segment| segment.prefix.as_ref() == b"aaa"),
            "aaa segment should be gone from manifest"
        );
        let bbb = db_state
            .segments
            .iter()
            .find(|segment| segment.prefix.as_ref() == b"bbb")
            .expect("missing segment bbb");
        assert!(
            !bbb.tree.l0.is_empty() || !bbb.tree.compacted.is_empty(),
            "bbb segment should still hold data"
        );

        assert_eq!(db.get(b"aaa-001").await.unwrap(), None);
        assert_eq!(db.get(b"aaa-002").await.unwrap(), None);
        assert_eq!(
            db.get(b"bbb-001").await.unwrap(),
            Some(Bytes::from_static(b"v3"))
        );
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_scan_across_segments_after_compaction() {
        let os = Arc::new(InMemory::new());
        let system_clock = Arc::new(MockSystemClock::new());
        let path = "/tmp/test_scan_across_segments_after_compaction";
        let mut options = db_options(Some(compactor_options()));
        options.l0_sst_size_bytes = 512;
        options.flush_interval = None;
        let scheduler_options = SizeTieredCompactionSchedulerOptions {
            min_compaction_sources: 2,
            max_compaction_sources: 999,
            include_size_threshold: 4.0,
        }
        .into();
        let compactor_opts = options
            .compactor_options
            .as_mut()
            .expect("compactor options must be set");
        compactor_opts.scheduler_options = scheduler_options;
        compactor_opts.max_concurrent_compactions = 3;

        let db = Db::builder(path, os.clone())
            .with_settings(options)
            .with_system_clock(system_clock.clone())
            .with_segment_extractor(Arc::new(FixedThreeBytePrefixExtractor))
            .build()
            .await
            .unwrap();

        // Two keys per segment, each flushed to its own L0 SST. Written in
        // intra-segment lex order so the post-compaction scan order is
        // deterministic without re-sorting on the test side.
        let entries: Vec<(Vec<u8>, Vec<u8>)> = [
            b"aaa-001".as_slice(),
            b"aaa-002",
            b"bbb-001",
            b"bbb-002",
            b"ccc-001",
            b"ccc-002",
        ]
        .iter()
        .enumerate()
        .map(|(i, k)| (k.to_vec(), format!("v{}", i + 1).into_bytes()))
        .collect();

        for (key, value) in &entries {
            put_and_flush_memtable(&db, key, value).await;
        }

        let prefixes: [&[u8]; 3] = [b"aaa", b"bbb", b"ccc"];
        let _db_state = run_for(Duration::from_secs(10), || async {
            system_clock
                .as_ref()
                .advance(Duration::from_millis(60000))
                .await;
            let core = read_db_state_core(&db);
            let all_compacted = prefixes.iter().all(|prefix| {
                core.segments
                    .iter()
                    .find(|segment| segment.prefix.as_ref() == *prefix)
                    .map(|segment| segment.tree.l0.is_empty() && !segment.tree.compacted.is_empty())
                    .unwrap_or(false)
            });
            if all_compacted {
                Some(core)
            } else {
                None
            }
        })
        .await
        .expect("not every segment compacted before scan");

        // Full ascending scan crosses every segment in prefix order.
        let mut iter = db.scan(..).await.unwrap();
        let mut collected: Vec<(Vec<u8>, Vec<u8>)> = Vec::new();
        while let Some(kv) = iter.next().await.unwrap() {
            collected.push((kv.key.to_vec(), kv.value.to_vec()));
        }
        assert_eq!(
            collected, entries,
            "full scan should yield every key in order"
        );

        // Per-segment prefix scan should return only that segment's keys.
        for prefix in prefixes {
            let mut iter = db.scan_prefix(prefix, ..).await.unwrap();
            let mut keys: Vec<Vec<u8>> = Vec::new();
            while let Some(kv) = iter.next().await.unwrap() {
                keys.push(kv.key.to_vec());
            }
            let expected: Vec<Vec<u8>> = entries
                .iter()
                .filter(|(k, _)| k.starts_with(prefix))
                .map(|(k, _)| k.clone())
                .collect();
            assert_eq!(keys, expected, "prefix scan for {:?}", prefix);
        }

        // Cross-segment range scan: spans the tail of aaa, all of bbb, and stops
        // before the head of ccc.
        let mut iter = db
            .scan(b"aaa-002".to_vec()..b"ccc-001".to_vec())
            .await
            .unwrap();
        let mut keys: Vec<Vec<u8>> = Vec::new();
        while let Some(kv) = iter.next().await.unwrap() {
            keys.push(kv.key.to_vec());
        }
        assert_eq!(
            keys,
            vec![
                b"aaa-002".to_vec(),
                b"bbb-001".to_vec(),
                b"bbb-002".to_vec(),
            ],
            "range scan spanning aaa and bbb"
        );
    }

    #[cfg(feature = "wal_disable")]
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_should_tombstones_in_l0() {
        use crate::test_utils::OnDemandCompactionSchedulerSupplier;

        let os = Arc::new(InMemory::new());
        let system_clock = Arc::new(MockSystemClock::new());

        let scheduler = Arc::new(OnDemandCompactionSchedulerSupplier::new(Arc::new(
            |state| {
                // compact when there are at least 2 SSTs in L0 (one for key 'a' and one for key 'b')
                state.manifest().core().tree.l0.len() == 2 ||
                // or when there is one SST in L0 and one in L1 (one for delete key 'a' and one for compacted key 'a'+'b')
                (state.manifest().core().tree.l0.len() == 1
                    && state.manifest().core().tree.compacted.len() == 1)
            },
        )));

        let mut options = db_options(None);
        options.wal_enabled = false;
        options.l0_sst_size_bytes = 128;

        let db = Db::builder(PATH, os.clone())
            .with_settings(options)
            .with_system_clock(system_clock.clone())
            .with_compactor_builder(compactor_builder_with_scheduler(
                os.clone(),
                scheduler.clone(),
                system_clock.clone(),
            ))
            .build()
            .await
            .unwrap();

        let (manifest_store, _, table_store) = build_test_stores(os.clone());

        // put key 'a' into L1 (and key 'b' so that when we delete 'a' the SST is non-empty)
        // since these are both await_durable=true, we're guaranteed to have one L0 SST for each.
        db.put(&[b'a'; 16], &[b'a'; 32]).await.unwrap();
        db.put(&[b'b'; 16], &[b'a'; 32]).await.unwrap();
        db.flush().await.unwrap();
        let db_state = await_compaction(&db, os.clone(), Some(system_clock.clone()))
            .await
            .unwrap();
        assert_eq!(db_state.tree.compacted.len(), 1);
        assert_eq!(db_state.tree.l0.len(), 0, "{:?}", db_state.tree.l0);

        // put tombstone for key a into L0
        db.delete_with_options(
            &[b'a'; 16],
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.flush().await.unwrap();

        // Then:
        // we should now have a tombstone in L0 and a value in L1
        let db_state = get_db_state(manifest_store.clone()).await;
        assert_eq!(db_state.tree.l0.len(), 1, "{:?}", db_state.tree.l0);
        assert_eq!(db_state.tree.compacted.len(), 1);

        let l0 = db_state.tree.l0.front().unwrap();
        let mut iter = SstIterator::new_borrowed_initialized(
            ..,
            l0,
            table_store.clone(),
            SstIteratorOptions::default(),
        )
        .await
        .unwrap()
        .expect("Expected Some(iter) but got None");

        let tombstone = iter.next().await.unwrap();
        assert!(tombstone.unwrap().value.is_tombstone());

        let db_state = await_compacted_compaction(
            manifest_store.clone(),
            db_state.tree.compacted.clone(),
            Some(system_clock.clone()),
        )
        .await
        .unwrap();
        assert_eq!(db_state.tree.compacted.len(), 1);

        let compacted = &db_state.tree.compacted.first().unwrap().sst_views;
        assert_eq!(compacted.len(), 1);
        let handle = compacted.first().unwrap();

        let mut iter = SstIterator::new_borrowed_initialized(
            ..,
            handle,
            table_store.clone(),
            SstIteratorOptions::default(),
        )
        .await
        .unwrap()
        .expect("Expected Some(iter) but got None");

        // should be no tombstone for key 'a' because it was filtered
        // out of the last run
        let next = iter.next().await.unwrap().map(KeyValue::from);
        assert_eq!(next.unwrap().key.as_ref(), &[b'b'; 16]);
        let next = iter.next().await.unwrap().map(KeyValue::from);
        assert!(next.is_none());
    }

    #[cfg(feature = "wal_disable")]
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_should_not_filter_tombstone_with_snapshot() {
        use crate::test_utils::OnDemandCompactionSchedulerSupplier;

        let os = Arc::new(InMemory::new());
        let system_clock = Arc::new(MockSystemClock::new());

        let scheduler = Arc::new(OnDemandCompactionSchedulerSupplier::new(Arc::new(
            |state| {
                // compact when there are at least 2 SSTs in L0
                state.manifest().core().tree.l0.len() == 2 ||
                // or when there is one SST in L0 and one in L1
                (state.manifest().core().tree.l0.len() == 1
                    && state.manifest().core().tree.compacted.len() == 1)
            },
        )));

        let mut options = db_options(None);
        options.wal_enabled = false;
        options.l0_sst_size_bytes = 128;

        let db = Db::builder(PATH, os.clone())
            .with_settings(options)
            .with_system_clock(system_clock.clone())
            .with_compactor_builder(compactor_builder_with_scheduler(
                os.clone(),
                scheduler.clone(),
                system_clock.clone(),
            ))
            .build()
            .await
            .unwrap();

        let (manifest_store, _, table_store) = build_test_stores(os.clone());

        // Write and flush key 'a' first (seq=1)
        db.put(&[b'a'; 16], &[b'a'; 32]).await.unwrap();
        db.put(&[b'b'; 16], &[b'a'; 32]).await.unwrap();

        // Create a snapshot after first flush. This protects seq >= 1
        let _snapshot = db.snapshot().await.unwrap();
        db.flush().await.unwrap();

        // Compact L0 to L1
        let db_state = await_compaction(&db, os.clone(), Some(system_clock.clone()))
            .await
            .unwrap();
        assert_eq!(db_state.tree.compacted.len(), 1);
        assert_eq!(db_state.tree.l0.len(), 0, "{:?}", db_state.tree.l0);

        // Now delete key 'a', creating a tombstone (seq=3)
        db.delete_with_options(
            &[b'a'; 16],
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.flush().await.unwrap();

        // We should now have a tombstone for 'a' in L0 and both 'a' and 'b' values in L1
        let db_state = get_db_state(manifest_store.clone()).await;
        assert_eq!(db_state.tree.l0.len(), 1, "{:?}", db_state.tree.l0);
        assert_eq!(db_state.tree.compacted.len(), 1);

        // Trigger compaction of L0 (tombstone) + L1 (values)
        let db_state = await_compacted_compaction(
            manifest_store.clone(),
            db_state.tree.compacted.clone(),
            Some(system_clock.clone()),
        )
        .await
        .unwrap();
        assert_eq!(db_state.tree.compacted.len(), 1);

        let compacted = &db_state.tree.compacted.first().unwrap().sst_views;
        assert_eq!(compacted.len(), 1);
        let handle = compacted.first().unwrap();

        let mut iter = SstIterator::new_borrowed_initialized(
            ..,
            handle,
            table_store.clone(),
            SstIteratorOptions::default(),
        )
        .await
        .unwrap()
        .expect("Expected Some(iter) but got None");

        // After compaction with an active snapshot (protecting seq >= 1):
        // - Key 'a': Both the tombstone(seq=3) and original value(seq=1) are protected
        //   The SST contains both versions
        // - Key 'b': value(seq=2) is protected
        // Expected result: both 'a' (as tombstone) and 'b' (as value) should be present
        // Note: next() returns all entries including tombstones and duplicates,
        // so we need to track whether we've seen each key and verify the expected type
        let next = iter.next().await.unwrap().unwrap();
        assert_eq!(next.key.as_ref(), &[b'a'; 16]);
        assert!(next.value.is_tombstone());

        let next = iter.next().await.unwrap().unwrap();
        assert_eq!(next.key.as_ref(), &[b'a'; 16]);
        assert_eq!(next.value.as_bytes().unwrap().as_ref(), &[b'a'; 32]);

        let next = iter.next().await.unwrap().unwrap();
        assert_eq!(next.key.as_ref(), &[b'b'; 16]);
        assert_eq!(next.value.as_bytes().unwrap().as_ref(), &[b'a'; 32]);

        let next = iter.next().await.unwrap();
        assert!(next.is_none());
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn should_apply_merge_during_l0_compaction() {
        use crate::test_utils::OnDemandCompactionSchedulerSupplier;

        // given:
        let os = Arc::new(InMemory::new());
        let system_clock = Arc::new(MockSystemClock::new());
        let compaction_scheduler = Arc::new(OnDemandCompactionSchedulerSupplier::new(Arc::new(
            |state| state.manifest().core().tree.l0.len() >= 2,
        )));
        let options = db_options(None);

        let db = Db::builder(PATH, os.clone())
            .with_settings(options)
            .with_system_clock(system_clock.clone())
            .with_compactor_builder(compactor_builder_with_scheduler(
                os.clone(),
                compaction_scheduler.clone(),
                system_clock.clone(),
            ))
            .with_merge_operator(Arc::new(StringConcatMergeOperator))
            .build()
            .await
            .unwrap();

        let (_manifest_store, _, table_store) = build_test_stores(os.clone());

        // write merge operations across multiple L0 SSTs
        db.merge_with_options(
            b"key1",
            b"a",
            &MergeOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.merge_with_options(
            b"key1",
            b"b",
            &MergeOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.put_with_options(
            &vec![b'x'; 16],
            &vec![b'p'; 128],
            &PutOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.flush().await.unwrap();

        db.merge_with_options(
            b"key1",
            b"c",
            &MergeOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.merge_with_options(
            b"key2",
            b"x",
            &MergeOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.put_with_options(
            &vec![b'y'; 16],
            &vec![b'p'; 128],
            &PutOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.flush().await.unwrap();

        // when:
        let db_state = await_compaction(&db, os.clone(), Some(system_clock)).await;

        // then:
        let db_state = db_state.expect("db was not compacted");
        assert_eq!(db_state.tree.compacted.len(), 1);
        let compacted = &db_state.tree.compacted.first().unwrap().sst_views;
        assert_eq!(compacted.len(), 1);
        let handle = compacted.first().unwrap();

        let mut iter = SstIterator::new_borrowed_initialized(
            ..,
            handle,
            table_store.clone(),
            SstIteratorOptions::default(),
        )
        .await
        .unwrap()
        .expect("Expected Some(iter) but got None");

        assert_iterator(
            &mut iter,
            vec![
                RowEntry::new_merge(b"key1", b"abc", 4).with_create_ts(0),
                RowEntry::new_merge(b"key2", b"x", 5).with_create_ts(0),
                RowEntry::new_value(&[b'x'; 16], &[b'p'; 128], 3).with_create_ts(0),
                RowEntry::new_value(&[b'y'; 16], &[b'p'; 128], 6).with_create_ts(0),
            ],
        )
        .await;

        // verify the merged values are readable from the db
        let result = db.get(b"key1").await.unwrap();
        assert_eq!(result, Some(Bytes::from("abc")));
        let result = db.get(b"key2").await.unwrap();
        assert_eq!(result, Some(Bytes::from("x")));
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn should_record_merge_operator_operands_on_compact_path() {
        use crate::merge_operator::MERGE_OPERATOR_COMPACT_PATH;
        use crate::test_utils::{
            lookup_merge_operator_operands, OnDemandCompactionSchedulerSupplier,
        };
        use slatedb_common::metrics::DefaultMetricsRecorder;

        let os = Arc::new(InMemory::new());
        let system_clock = Arc::new(MockSystemClock::new());
        let compaction_scheduler = Arc::new(OnDemandCompactionSchedulerSupplier::new(Arc::new(
            |state| state.manifest().core().tree.l0.len() >= 2,
        )));
        let metrics_recorder = Arc::new(DefaultMetricsRecorder::new());

        let db = Db::builder(PATH, os.clone())
            .with_settings(db_options(None))
            .with_system_clock(system_clock.clone())
            .with_metrics_recorder(metrics_recorder.clone())
            .with_compactor_builder(compactor_builder_with_scheduler(
                os.clone(),
                compaction_scheduler,
                system_clock.clone(),
            ))
            .with_merge_operator(Arc::new(StringConcatMergeOperator))
            .build()
            .await
            .unwrap();

        assert_eq!(
            lookup_merge_operator_operands(&metrics_recorder, MERGE_OPERATOR_COMPACT_PATH),
            Some(0)
        );

        db.merge_with_options(
            b"key1",
            b"a",
            &MergeOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.flush_with_options(FlushOptions {
            flush_type: FlushType::MemTable,
        })
        .await
        .unwrap();

        db.merge_with_options(
            b"key1",
            b"b",
            &MergeOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.flush_with_options(FlushOptions {
            flush_type: FlushType::MemTable,
        })
        .await
        .unwrap();

        let db_state = await_compaction(&db, os.clone(), Some(system_clock)).await;
        assert!(db_state.is_some(), "db was not compacted");
        assert!(
            lookup_merge_operator_operands(&metrics_recorder, MERGE_OPERATOR_COMPACT_PATH)
                .is_some_and(|value| value > 0)
        );
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn should_apply_merge_across_l0_and_sorted_runs() {
        use crate::test_utils::OnDemandCompactionSchedulerSupplier;

        // given:
        let os = Arc::new(InMemory::new());
        let system_clock = Arc::new(MockSystemClock::new());
        let compaction_scheduler = Arc::new(OnDemandCompactionSchedulerSupplier::new(Arc::new(
            |state| !state.manifest().core().tree.l0.is_empty(),
        )));
        let options = db_options(None);

        let db = Db::builder(PATH, os.clone())
            .with_settings(options)
            .with_system_clock(system_clock.clone())
            .with_compactor_builder(compactor_builder_with_scheduler(
                os.clone(),
                compaction_scheduler.clone(),
                system_clock.clone(),
            ))
            .with_merge_operator(Arc::new(StringConcatMergeOperator))
            .build()
            .await
            .unwrap();

        let (_manifest_store, _, table_store) = build_test_stores(os.clone());

        // write initial merge operations and compact to L1
        db.merge_with_options(
            b"key1",
            b"a",
            &MergeOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.merge_with_options(
            b"key1",
            b"b",
            &MergeOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.put_with_options(
            &vec![b'x'; 16],
            &vec![b'p'; 128],
            &PutOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap(); // padding to exceed 256 bytes
        db.flush().await.unwrap();

        let db_state = await_compaction(&db, os.clone(), Some(system_clock.clone())).await;
        let db_state = db_state.expect("db was not compacted");
        assert_eq!(db_state.tree.compacted.len(), 1);
        // Save current tick since we advanced it in `await_compaction`. We'll use it
        // later to verify the create_ts of the merged and normal entries.
        let expected_tick = system_clock.now().timestamp_millis();

        // write more merge operations to L0
        db.merge_with_options(
            b"key1",
            b"c",
            &MergeOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.merge_with_options(
            b"key1",
            b"d",
            &MergeOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.put_with_options(
            &vec![b'y'; 16],
            &vec![b'p'; 128],
            &PutOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap(); // padding to exceed 256 bytes
        db.flush().await.unwrap();

        // when: compact L0 with the existing sorted run
        let db_state = await_compaction(&db, os.clone(), Some(system_clock)).await;

        // then:
        let db_state = db_state.expect("db was not compacted");
        assert_eq!(db_state.tree.compacted.len(), 1);
        let compacted = &db_state.tree.compacted.first().unwrap().sst_views;
        assert_eq!(compacted.len(), 1);
        let handle = compacted.first().unwrap();

        let mut iter = SstIterator::new_borrowed_initialized(
            ..,
            handle,
            table_store.clone(),
            SstIteratorOptions::default(),
        )
        .await
        .unwrap()
        .expect("Expected Some(iter) but got None");

        assert_iterator(
            &mut iter,
            vec![
                RowEntry::new_merge(b"key1", b"abcd", 5).with_create_ts(expected_tick),
                RowEntry::new_value(&[b'x'; 16], &[b'p'; 128], 3).with_create_ts(0),
                RowEntry::new_value(&[b'y'; 16], &[b'p'; 128], 6).with_create_ts(expected_tick),
            ],
        )
        .await;

        // verify the merged value is readable from the db
        let result = db.get(b"key1").await.unwrap();
        assert_eq!(result, Some(Bytes::from("abcd")));
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn should_merge_without_base_value() {
        use crate::test_utils::OnDemandCompactionSchedulerSupplier;

        // given:
        let os = Arc::new(InMemory::new());
        let system_clock = Arc::new(MockSystemClock::new());
        let compaction_scheduler = Arc::new(OnDemandCompactionSchedulerSupplier::new(Arc::new(
            |state| state.manifest().core().tree.l0.len() >= 2,
        )));
        let options = db_options(None);

        let db = Db::builder(PATH, os.clone())
            .with_settings(options)
            .with_system_clock(system_clock.clone())
            .with_compactor_builder(compactor_builder_with_scheduler(
                os.clone(),
                compaction_scheduler.clone(),
                system_clock.clone(),
            ))
            .with_merge_operator(Arc::new(StringConcatMergeOperator))
            .build()
            .await
            .unwrap();

        let (_manifest_store, _, table_store) = build_test_stores(os.clone());

        // write only merge operations without any base value
        db.merge_with_options(
            b"key1",
            b"x",
            &MergeOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap(); // padding to exceed 256 bytes
        db.put_with_options(
            &vec![b'x'; 16],
            &vec![b'p'; 128],
            &PutOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap(); // padding to exceed 256 bytes
        db.flush().await.unwrap();

        db.merge_with_options(
            b"key1",
            b"y",
            &MergeOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.merge_with_options(
            b"key1",
            b"z",
            &MergeOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.put_with_options(
            &vec![b'y'; 16],
            &vec![b'p'; 128],
            &PutOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap(); // padding to exceed 256 bytes
        db.flush().await.unwrap();

        // when:
        let db_state = await_compaction(&db, os.clone(), Some(system_clock)).await;

        // then:
        let db_state = db_state.expect("db was not compacted");
        assert_eq!(db_state.tree.compacted.len(), 1);
        let compacted = &db_state.tree.compacted.first().unwrap().sst_views;
        assert_eq!(compacted.len(), 1);
        let handle = compacted.first().unwrap();

        let mut iter = SstIterator::new_borrowed_initialized(
            ..,
            handle,
            table_store.clone(),
            SstIteratorOptions::default(),
        )
        .await
        .unwrap()
        .expect("Expected Some(iter) but got None");

        assert_iterator(
            &mut iter,
            vec![
                RowEntry::new_merge(b"key1", b"xyz", 4).with_create_ts(0),
                RowEntry::new_value(&[b'x'; 16], &[b'p'; 128], 2).with_create_ts(0),
                RowEntry::new_value(&[b'y'; 16], &[b'p'; 128], 5).with_create_ts(0),
            ],
        )
        .await;

        // verify the merged value is readable from the db
        let result = db.get(b"key1").await.unwrap();
        assert_eq!(result, Some(Bytes::from("xyz")));
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn should_preserve_merge_order_across_multiple_ssts() {
        use crate::test_utils::OnDemandCompactionSchedulerSupplier;

        // given:
        let os = Arc::new(InMemory::new());
        let system_clock = Arc::new(MockSystemClock::new());
        let compaction_scheduler = Arc::new(OnDemandCompactionSchedulerSupplier::new(Arc::new(
            |state| state.manifest().core().tree.l0.len() >= 3,
        )));
        let options = db_options(None);

        let db = Db::builder(PATH, os.clone())
            .with_settings(options)
            .with_system_clock(system_clock.clone())
            .with_compactor_builder(compactor_builder_with_scheduler(
                os.clone(),
                compaction_scheduler.clone(),
                system_clock.clone(),
            ))
            .with_merge_operator(Arc::new(StringConcatMergeOperator))
            .build()
            .await
            .unwrap();

        let (_manifest_store, _, table_store) = build_test_stores(os.clone());

        // write merge operations across three L0 SSTs in specific order
        db.merge_with_options(
            b"key1",
            b"1",
            &MergeOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.put_with_options(
            &vec![b'a'; 16],
            &vec![b'p'; 128],
            &PutOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap(); // padding to exceed 256 bytes
        db.flush().await.unwrap();

        db.merge_with_options(
            b"key1",
            b"2",
            &MergeOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.put_with_options(
            &vec![b'b'; 16],
            &vec![b'p'; 128],
            &PutOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap(); // padding to exceed 256 bytes
        db.flush().await.unwrap();

        db.merge_with_options(
            b"key1",
            b"3",
            &MergeOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.put_with_options(
            &vec![b'c'; 16],
            &vec![b'p'; 128],
            &PutOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap(); // padding to exceed 256 bytes
        db.flush().await.unwrap();

        // when:
        let db_state = await_compaction(&db, os.clone(), Some(system_clock)).await;

        // then:
        let db_state = db_state.expect("db was not compacted");
        assert_eq!(db_state.tree.compacted.len(), 1);
        let compacted = &db_state.tree.compacted.first().unwrap().sst_views;
        assert_eq!(compacted.len(), 1);
        let handle = compacted.first().unwrap();

        let mut iter = SstIterator::new_borrowed_initialized(
            ..,
            handle,
            table_store.clone(),
            SstIteratorOptions::default(),
        )
        .await
        .unwrap()
        .expect("Expected Some(iter) but got None");

        // merges should be applied in chronological order: 1, 2, 3
        assert_iterator(
            &mut iter,
            vec![
                RowEntry::new_value(&[b'a'; 16], &[b'p'; 128], 2).with_create_ts(0),
                RowEntry::new_value(&[b'b'; 16], &[b'p'; 128], 4).with_create_ts(0),
                RowEntry::new_value(&[b'c'; 16], &[b'p'; 128], 6).with_create_ts(0),
                RowEntry::new_merge(b"key1", b"123", 5).with_create_ts(0),
            ],
        )
        .await;

        // verify the merged value is readable from the db
        let result = db.get(b"key1").await.unwrap();
        assert_eq!(result, Some(Bytes::from("123")));
    }

    #[cfg(feature = "wal_disable")]
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn should_not_compact_expired_merge_operations_in_last_run() {
        use crate::test_utils::OnDemandCompactionSchedulerSupplier;

        // given:
        let os = Arc::new(InMemory::new());
        let insert_clock = Arc::new(MockSystemClock::new());

        let scheduler = Arc::new(OnDemandCompactionSchedulerSupplier::new(Arc::new(
            |state| state.manifest().core().tree.l0.len() >= 2,
        )));

        let mut options = db_options(None);
        options.wal_enabled = false;
        options.l0_sst_size_bytes = 128;

        let db = Db::builder(PATH, os.clone())
            .with_settings(options)
            .with_system_clock(insert_clock.clone())
            .with_compactor_builder(compactor_builder_with_scheduler(
                os.clone(),
                scheduler.clone(),
                insert_clock.clone(),
            ))
            .with_merge_operator(Arc::new(StringConcatMergeOperator))
            .build()
            .await
            .unwrap();

        let (_manifest_store, _, table_store) = build_test_stores(os.clone());

        // ticker time = 0, expire time = 10
        insert_clock.set(0);
        db.merge_with_options(
            b"key1",
            &[b'a'; 32],
            &crate::config::MergeOptions {
                ttl: Ttl::ExpireAfter(10),
            },
            &WriteOptions {
                await_durable: true,
                ..Default::default()
            },
        )
        .await
        .unwrap();

        // ticker time = 20, no expire time
        insert_clock.set(20);
        db.merge_with_options(
            b"key1",
            &[b'b'; 32],
            &crate::config::MergeOptions { ttl: Ttl::NoExpiry },
            &WriteOptions {
                await_durable: true,
                ..Default::default()
            },
        )
        .await
        .unwrap();

        let db_state = await_compaction(&db, os.clone(), Some(insert_clock.clone()))
            .await
            .unwrap();
        assert_eq!(db_state.tree.compacted.len(), 1);
        assert_eq!(db_state.last_l0_clock_tick, 20);

        // then: the compacted SST should only contain the non-expired merge
        let compacted = &db_state.tree.compacted.first().unwrap().sst_views;
        assert_eq!(compacted.len(), 1);
        let handle = compacted.first().unwrap();

        let mut iter = SstIterator::new_borrowed_initialized(
            ..,
            handle,
            table_store.clone(),
            SstIteratorOptions::default(),
        )
        .await
        .unwrap()
        .expect("Expected Some(iter) but got None");

        // only the non-expired merge "b" should be present
        assert_iterator(
            &mut iter,
            vec![RowEntry::new_merge(b"key1", &[b'b'; 32], 2).with_create_ts(20)],
        )
        .await;
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn should_merge_and_then_overwrite_with_put() {
        use crate::test_utils::OnDemandCompactionSchedulerSupplier;

        // given:
        let os = Arc::new(InMemory::new());
        let system_clock = Arc::new(MockSystemClock::new());
        let compaction_scheduler = Arc::new(OnDemandCompactionSchedulerSupplier::new(Arc::new(
            |state| state.manifest().core().tree.l0.len() >= 2,
        )));
        let options = db_options(None);

        let db = Db::builder(PATH, os.clone())
            .with_settings(options)
            .with_system_clock(system_clock.clone())
            .with_compactor_builder(compactor_builder_with_scheduler(
                os.clone(),
                compaction_scheduler.clone(),
                system_clock.clone(),
            ))
            .with_merge_operator(Arc::new(StringConcatMergeOperator))
            .build()
            .await
            .unwrap();

        let (manifest_store, _compactions_store, _table_store) = build_test_stores(os.clone());

        // write merge operations
        db.merge_with_options(
            b"key1",
            b"a",
            &MergeOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.merge_with_options(
            b"key1",
            b"b",
            &MergeOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.put_with_options(
            &vec![b'x'; 16],
            &vec![b'p'; 128],
            &PutOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap(); // padding
        db.flush().await.unwrap();

        // then overwrite with a put
        db.put_with_options(
            b"key1",
            b"new_value",
            &PutOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.put_with_options(
            &vec![b'y'; 16],
            &vec![b'p'; 128],
            &PutOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap(); // padding
        db.flush().await.unwrap();

        // when:
        let _ = await_compaction(&db, os.clone(), Some(system_clock)).await;

        // then: verify the put value overwrote the merge
        let result = db.get(b"key1").await.unwrap();
        assert_eq!(result, Some(Bytes::from("new_value")));

        // verify the compacted state in manifest
        let stored_manifest =
            StoredManifest::load(manifest_store.clone(), Arc::new(DefaultSystemClock::new()))
                .await
                .unwrap();
        let db_state = stored_manifest.db_state();
        assert!(
            !db_state.tree.compacted.is_empty(),
            "compaction should have occurred"
        );
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn should_not_merge_operations_with_different_expire_times() {
        use crate::test_utils::OnDemandCompactionSchedulerSupplier;

        // given:
        let os = Arc::new(InMemory::new());
        let system_clock = Arc::new(MockSystemClock::new());
        let compaction_scheduler = Arc::new(OnDemandCompactionSchedulerSupplier::new(Arc::new(
            |state| state.manifest().core().tree.l0.len() >= 2,
        )));
        let options = db_options(None);

        let db = Db::builder(PATH, os.clone())
            .with_settings(options)
            .with_system_clock(system_clock.clone())
            .with_compactor_builder(compactor_builder_with_scheduler(
                os.clone(),
                compaction_scheduler.clone(),
                system_clock.clone(),
            ))
            .with_merge_operator(Arc::new(StringConcatMergeOperator))
            .build()
            .await
            .unwrap();

        let (manifest_store, _compactions_store, table_store) = build_test_stores(os.clone());

        // write merge operations with different TTLs
        db.merge_with_options(
            b"key1",
            b"a",
            &crate::config::MergeOptions {
                ttl: Ttl::ExpireAfter(100),
            },
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.put_with_options(
            &vec![b'x'; 16],
            &vec![b'p'; 128],
            &PutOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap(); // padding
        db.flush().await.unwrap();

        db.merge_with_options(
            b"key1",
            b"b",
            &crate::config::MergeOptions {
                ttl: Ttl::ExpireAfter(200),
            },
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.put_with_options(
            &vec![b'y'; 16],
            &vec![b'p'; 128],
            &PutOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap(); // padding
        db.flush().await.unwrap();

        // when:
        // Wait a bit for compaction to occur
        let _ = await_compaction(&db, os.clone(), Some(system_clock)).await;

        // then: verify that merges with different expire times are NOT merged together
        // Reading should get only the latest merge since they weren't combined
        let stored_manifest =
            StoredManifest::load(manifest_store.clone(), Arc::new(DefaultSystemClock::new()))
                .await
                .unwrap();
        let db_state = stored_manifest.db_state();
        assert!(
            !db_state.tree.compacted.is_empty(),
            "compaction should have occurred"
        );

        // The compacted sorted run should contain both merge operations separately
        let compacted = &db_state.tree.compacted.first().unwrap().sst_views;
        assert_eq!(compacted.len(), 1);
        let handle = compacted.first().unwrap();

        let mut iter = SstIterator::new_borrowed_initialized(
            ..,
            handle,
            table_store.clone(),
            SstIteratorOptions::default(),
        )
        .await
        .unwrap()
        .expect("Expected Some(iter) but got None");

        // merge operations should be kept separate due to different expire times
        let mut key1_entries = vec![];
        while let Some(entry) = iter.next().await.unwrap() {
            if entry.key.as_ref() == b"key1" {
                key1_entries.push(entry);
            }
        }
        // We should have at least 1 merge operation for key1
        assert!(
            !key1_entries.is_empty(),
            "should have merge operations for key1"
        );
        // All entries for key1 should be merge operations
        assert!(key1_entries
            .iter()
            .all(|e| matches!(e.value, crate::types::ValueDeletable::Merge(_))));
        // If there are 2 entries, they should have different expire times
        if key1_entries.len() == 2 {
            assert_ne!(
                key1_entries[0].expire_ts, key1_entries[1].expire_ts,
                "separate merge operations should have different expire times"
            );
        }
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn should_merge_operations_with_same_expire_at() {
        use crate::test_utils::OnDemandCompactionSchedulerSupplier;

        // given:
        let os = Arc::new(InMemory::new());
        let system_clock = Arc::new(MockSystemClock::new());
        let compaction_scheduler = Arc::new(OnDemandCompactionSchedulerSupplier::new(Arc::new(
            |state| state.manifest().core().tree.l0.len() >= 2,
        )));
        let options = db_options(None);

        let db = Db::builder(PATH, os.clone())
            .with_settings(options)
            .with_system_clock(system_clock.clone())
            .with_compactor_builder(compactor_builder_with_scheduler(
                os.clone(),
                compaction_scheduler.clone(),
                system_clock.clone(),
            ))
            .with_merge_operator(Arc::new(StringConcatMergeOperator))
            .build()
            .await
            .unwrap();

        let (manifest_store, _compactions_store, table_store) = build_test_stores(os.clone());

        let flush_opts = FlushOptions {
            flush_type: FlushType::MemTable,
        };

        // write merge operations with the SAME ExpireAt timestamp at different clock times
        system_clock.set(100);
        db.merge_with_options(
            b"key1",
            b"a",
            &MergeOptions {
                ttl: Ttl::ExpireAt(1000),
            },
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.flush_with_options(flush_opts.clone()).await.unwrap();

        system_clock.set(200);
        db.merge_with_options(
            b"key1",
            b"b",
            &MergeOptions {
                ttl: Ttl::ExpireAt(1000),
            },
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.flush_with_options(flush_opts.clone()).await.unwrap();

        // when:
        let _ = await_compaction(&db, os.clone(), Some(system_clock)).await;

        // then: verify in the compacted SST that all merge operations were combined
        let stored_manifest =
            StoredManifest::load(manifest_store.clone(), Arc::new(DefaultSystemClock::new()))
                .await
                .unwrap();
        let db_state = stored_manifest.db_state();
        assert!(
            !db_state.tree.compacted.is_empty(),
            "compaction should have occurred"
        );

        let compacted = &db_state.tree.compacted.first().unwrap().sst_views;
        assert_eq!(compacted.len(), 1);
        let handle = compacted.first().unwrap();

        let mut iter = SstIterator::new_borrowed_initialized(
            ..,
            handle,
            table_store.clone(),
            SstIteratorOptions::default(),
        )
        .await
        .unwrap()
        .expect("Expected Some(iter) but got None");

        // collect key1 entries from the compacted SST
        let mut key1_entries = vec![];
        while let Some(entry) = iter.next().await.unwrap() {
            if entry.key.as_ref() == b"key1" {
                key1_entries.push(entry);
            }
        }

        // there should be exactly one merged entry for key1
        assert_eq!(
            key1_entries.len(),
            1,
            "expected a single merged entry for key1, got {}",
            key1_entries.len()
        );
        let merged = &key1_entries[0];
        assert_eq!(merged.expire_ts, Some(1000));
        assert!(
            matches!(&merged.value, crate::types::ValueDeletable::Merge(v) if v.as_ref() == b"ab"),
            "expected merged value 'ab', got {:?}",
            merged.value
        );
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn should_compact_expired_expire_at_entries() {
        // given:
        let os = Arc::new(InMemory::new());
        let insert_clock = Arc::new(MockSystemClock::new());

        let scheduler_options = SizeTieredCompactionSchedulerOptions {
            min_compaction_sources: 2,
            max_compaction_sources: 2,
            include_size_threshold: 4.0,
        }
        .into();
        let mut options = db_options(Some(compactor_options()));
        options
            .compactor_options
            .as_mut()
            .expect("compactor options missing")
            .scheduler_options = scheduler_options;
        let db = Db::builder(PATH, os.clone())
            .with_settings(options)
            .with_system_clock(insert_clock.clone())
            .build()
            .await
            .unwrap();

        let (_, _, table_store) = build_test_stores(os.clone());

        let value = &[b'a'; 32];
        let flush_opts = FlushOptions {
            flush_type: FlushType::MemTable,
        };

        // ticker time = 0, expire at 10
        insert_clock.set(0);
        db.put_with_options(
            &[1; 16],
            value,
            &PutOptions {
                ttl: Ttl::ExpireAt(10),
            },
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();

        // ticker time = 5, expire at far future (well beyond compaction time)
        insert_clock.set(5);
        db.put_with_options(
            &[2; 16],
            value,
            &PutOptions {
                ttl: Ttl::ExpireAt(i64::MAX),
            },
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();

        db.flush_with_options(flush_opts.clone()).await.unwrap();

        // ticker time = 10, no expire time
        insert_clock.set(10);
        db.put_with_options(
            &[3; 16],
            value,
            &PutOptions { ttl: Ttl::NoExpiry },
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();

        db.flush_with_options(flush_opts.clone()).await.unwrap();

        // when: await_compaction advances clock by 60s per iteration,
        // so compaction_start_ts will be well past expire_at=10
        let db_state =
            await_compaction_matching(&db, os.clone(), Some(insert_clock), has_single_output_sst)
                .await;

        // then: key 1 should be expired (expire_at=10 < compaction_time),
        //       key 2 should survive (expire_at=i64::MAX), key 3 has no expiry
        let db_state = db_state.expect("db was not compacted");
        assert!(db_state.tree.last_compacted_l0_sst_view_id.is_some());
        assert_eq!(db_state.tree.compacted.len(), 1);
        let compacted = &db_state.tree.compacted.first().unwrap().sst_views;
        assert_eq!(compacted.len(), 1);
        let handle = compacted.first().unwrap();
        let mut iter = SstIterator::new_borrowed_initialized(
            ..,
            handle,
            table_store.clone(),
            SstIteratorOptions::default(),
        )
        .await
        .unwrap()
        .expect("Expected Some(iter) but got None");

        assert_iterator(
            &mut iter,
            vec![
                RowEntry::new_value(&[2; 16], value, 2)
                    .with_create_ts(5)
                    .with_expire_ts(i64::MAX),
                RowEntry::new_value(&[3; 16], value, 3).with_create_ts(10),
            ],
        )
        .await;
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_should_compact_expired_entries() {
        // given:
        let os = Arc::new(InMemory::new());
        let insert_clock = Arc::new(MockSystemClock::new());

        let scheduler_options = SizeTieredCompactionSchedulerOptions {
            min_compaction_sources: 2,
            max_compaction_sources: 2,
            include_size_threshold: 4.0,
        }
        .into();
        let mut options = db_options(Some(compactor_options()));
        options.default_ttl = Some(50);
        options
            .compactor_options
            .as_mut()
            .expect("compactor options missing")
            .scheduler_options = scheduler_options;
        let db = Db::builder(PATH, os.clone())
            .with_settings(options)
            .with_system_clock(insert_clock.clone())
            .build()
            .await
            .unwrap();

        let (_, _, table_store) = build_test_stores(os.clone());

        let value = &[b'a'; 64];

        // ticker time = 0, expire time = 10
        insert_clock.set(0);
        db.put_with_options(
            &[1; 16],
            value,
            &PutOptions {
                ttl: Ttl::ExpireAfter(10),
            },
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();

        // ticker time = 10, expire time = 60 (using default TTL)
        insert_clock.set(10);
        db.put_with_options(
            &[2; 16],
            value,
            &PutOptions { ttl: Ttl::Default },
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();

        db.flush().await.unwrap();

        // ticker time = 30, no expire time
        insert_clock.set(30);
        db.put_with_options(
            &[3; 16],
            value,
            &PutOptions { ttl: Ttl::NoExpiry },
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();

        // this revives key 1
        // ticker time = 70, expire time 80
        insert_clock.set(70);
        db.put_with_options(
            &[1; 16],
            value,
            &PutOptions {
                ttl: Ttl::ExpireAfter(80),
            },
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();

        db.flush().await.unwrap();

        // when:
        let db_state =
            await_compaction_matching(&db, os.clone(), Some(insert_clock), has_single_output_sst)
                .await;

        // then:
        let db_state = db_state.expect("db was not compacted");
        assert!(db_state.tree.last_compacted_l0_sst_view_id.is_some());
        assert_eq!(db_state.tree.compacted.len(), 1);
        assert_eq!(db_state.last_l0_clock_tick, 70);
        let compacted = &db_state.tree.compacted.first().unwrap().sst_views;
        assert_eq!(compacted.len(), 1);
        let handle = compacted.first().unwrap();
        let mut iter = SstIterator::new_borrowed_initialized(
            ..,
            handle,
            table_store.clone(),
            SstIteratorOptions::default(),
        )
        .await
        .unwrap()
        .expect("Expected Some(iter) but got None");

        assert_iterator(
            &mut iter,
            vec![
                RowEntry::new_value(&[1; 16], value, 4)
                    .with_create_ts(70)
                    .with_expire_ts(150),
                // no tombstone for &[2; 16] because this is the last layer of the tree,
                RowEntry::new_value(&[3; 16], value, 3).with_create_ts(30),
            ],
        )
        .await;
    }

    #[tokio::test]
    async fn test_should_track_total_bytes_and_throughput() {
        use crate::compactor::stats::{
            TOTAL_BYTES_BEING_COMPACTED, TOTAL_THROUGHPUT_BYTES_PER_SEC,
        };
        use chrono::DateTime;

        let mut fixture = CompactorEventHandlerTestFixture::new().await;

        let current_time = fixture.handler.system_clock.now();
        let current_time_ms = current_time.timestamp_millis() as u64;
        let start_time_1 =
            DateTime::from_timestamp_millis((current_time_ms - 2000) as i64).unwrap();
        let start_time_2 =
            DateTime::from_timestamp_millis((current_time_ms - 1000) as i64).unwrap();

        let mut compaction_1 = Compaction::new(
            Ulid::from_parts(start_time_1.timestamp_millis() as u64, 0),
            CompactionSpec::new(vec![], 10),
        );
        compaction_1.set_bytes_processed(500);

        let mut compaction_2 = Compaction::new(
            Ulid::from_parts(start_time_2.timestamp_millis() as u64, 0),
            CompactionSpec::new(vec![], 11),
        );
        compaction_2.set_bytes_processed(1000);

        fixture
            .handler
            .state_mut()
            .add_compaction(compaction_1)
            .expect("failed to add compaction 1");
        fixture
            .handler
            .state_mut()
            .add_compaction(compaction_2)
            .expect("failed to add compaction 2");

        fixture.handler.handle_log_ticker();

        let total_bytes = slatedb_common::metrics::lookup_metric(
            &fixture.test_recorder,
            TOTAL_BYTES_BEING_COMPACTED,
        )
        .expect("metric not found");
        assert_eq!(total_bytes, 0);

        let throughput = slatedb_common::metrics::lookup_metric(
            &fixture.test_recorder,
            TOTAL_THROUGHPUT_BYTES_PER_SEC,
        )
        .expect("metric not found");
        assert!(
            throughput > 0,
            "Expected throughput > 0, got {}",
            throughput
        );
    }

    #[test]
    fn test_calculate_estimated_source_bytes_uses_target_segment_tree() {
        let segment_l0 = SsTableView::identity(SsTableHandle::new(
            SsTableId::Compacted(Ulid::new()),
            SST_FORMAT_VERSION_LATEST,
            SsTableInfo {
                first_entry: Some(Bytes::from_static(b"seg/a")),
                index_offset: 11,
                index_len: 13,
                ..SsTableInfo::default()
            },
        ));
        let segment_sr_view = SsTableView::identity(SsTableHandle::new(
            SsTableId::Compacted(Ulid::new()),
            SST_FORMAT_VERSION_LATEST,
            SsTableInfo {
                first_entry: Some(Bytes::from_static(b"seg/m")),
                index_offset: 17,
                index_len: 19,
                ..SsTableInfo::default()
            },
        ));
        let segment_sr = SortedRun {
            id: 7,
            sst_views: vec![segment_sr_view.clone()],
        };

        let mut core = ManifestCore::new();
        core.segments = vec![Segment {
            prefix: Bytes::from_static(b"seg"),
            tree: Arc::new(LsmTreeState {
                l0: VecDeque::from(vec![segment_l0.clone()]),
                compacted: vec![segment_sr.clone()],
                ..LsmTreeState::default()
            }),
        }];

        let compaction = Compaction::new(
            Ulid::new(),
            CompactionSpec::for_segment(
                Bytes::from_static(b"seg"),
                vec![
                    SourceId::SstView(segment_l0.id),
                    SourceId::SortedRun(segment_sr.id),
                ],
                9,
            ),
        );

        let expected = segment_l0.estimate_size() + segment_sr.estimate_size();
        let actual = CompactorEventHandler::calculate_estimated_source_bytes(&compaction, &core);
        assert_eq!(actual, expected);
    }

    #[tokio::test]
    async fn test_should_track_per_job_throughput() {
        let start_time_ms = 1000u64;
        let current_time_ms = 3000u64;
        let processed_bytes = 1000u64;

        let mut compaction = Compaction::new(
            Ulid::from_parts(start_time_ms, 0),
            CompactionSpec::new(vec![], 10),
        );
        compaction.set_bytes_processed(processed_bytes);

        // Calculate throughput manually using ULID timestamp
        let elapsed_secs = (current_time_ms as f64 - start_time_ms as f64) / 1000.0;
        let throughput = processed_bytes as f64 / elapsed_secs;
        assert_eq!(throughput, 500.0);

        // At start time, throughput should be 0
        let elapsed_zero = (start_time_ms as f64 - start_time_ms as f64) / 1000.0;
        let throughput_zero = if elapsed_zero > 0.0 {
            processed_bytes as f64 / elapsed_zero
        } else {
            0.0
        };
        assert_eq!(throughput_zero, 0.0);
    }

    #[tokio::test]
    async fn test_should_track_running_compactions_count() {
        use crate::compactor::stats::RUNNING_COMPACTIONS;

        let mut fixture = CompactorEventHandlerTestFixture::new().await;

        let running =
            slatedb_common::metrics::lookup_metric(&fixture.test_recorder, RUNNING_COMPACTIONS)
                .expect("metric not found");
        assert_eq!(running, 0);

        let compaction = Compaction::new(Ulid::new(), CompactionSpec::new(vec![], 10));
        fixture
            .handler
            .state_mut()
            .add_compaction(compaction)
            .expect("failed to add compaction");

        assert_eq!(fixture.handler.state().active_compactions().count(), 1);
    }

    #[tokio::test]
    async fn test_should_record_compactor_epoch() {
        let fixture = CompactorEventHandlerTestFixture::new().await;

        let compactor_epoch =
            slatedb_common::metrics::lookup_metric(&fixture.test_recorder, COMPACTOR_EPOCH)
                .expect("metric not found");

        assert_eq!(
            compactor_epoch,
            fixture.handler.state().manifest().value.compactor_epoch as i64
        );
    }

    #[tokio::test]
    async fn test_submit_persists_compaction() {
        let os = Arc::new(InMemory::new());
        let (manifest_store, compactions_store, _table_store) = build_test_stores(os.clone());
        let system_clock: Arc<dyn SystemClock> = Arc::new(DefaultSystemClock::new());

        StoredManifest::create_new_db(
            manifest_store.clone(),
            ManifestCore::new(),
            system_clock.clone(),
        )
        .await
        .unwrap();
        let stored_manifest = StoredManifest::load(manifest_store.clone(), system_clock.clone())
            .await
            .unwrap();
        StoredCompactions::create(
            compactions_store.clone(),
            stored_manifest.manifest().compactor_epoch,
        )
        .await
        .unwrap();

        let spec = CompactionSpec::new(vec![SourceId::SortedRun(0)], 0);
        let compaction_id = Compactor::submit(
            spec.clone(),
            compactions_store.clone(),
            Arc::new(DbRand::default()),
            system_clock.clone(),
        )
        .await
        .unwrap();

        let compactions = compactions_store.read_latest_compactions().await.unwrap();
        let stored = compactions
            .compactions
            .get(&compaction_id)
            .expect("missing submitted compaction");

        assert_eq!(stored.spec(), &spec);
        assert_eq!(stored.status(), CompactionStatus::Submitted);
    }

    #[tokio::test]
    async fn test_submit_retries_on_boundary_conflict() {
        // Set up a database with an initial manifest and compactions record.
        let raw_os: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
        let gated_os = Arc::new(GatedObjectStore::new(Arc::clone(&raw_os)));
        let (manifest_store, compactions_store, _table_store) = build_test_stores(gated_os.clone());
        let external_compactions_store = Arc::new(CompactionsStore::new(
            &Path::from(PATH),
            Arc::clone(&raw_os),
        ));
        let system_clock: Arc<dyn SystemClock> = Arc::new(DefaultSystemClock::new());

        StoredManifest::create_new_db(
            manifest_store.clone(),
            ManifestCore::new(),
            system_clock.clone(),
        )
        .await
        .unwrap();
        let stored_manifest = StoredManifest::load(manifest_store.clone(), system_clock.clone())
            .await
            .unwrap();
        StoredCompactions::create(
            compactions_store.clone(),
            stored_manifest.manifest().compactor_epoch,
        )
        .await
        .unwrap();

        // Pause submit before its first write. While it is stale, an external writer creates
        // a live newer version, then GC deletes and fences the stale writer's next id.
        let start_id = compactions_store
            .read_latest_compactions()
            .await
            .unwrap()
            .id;
        let spec = CompactionSpec::new(vec![SourceId::SortedRun(0)], 0);
        let arrivals = gated_os.put_opts_gate.arrivals();
        gated_os.put_opts_gate.close();
        let submit_task = tokio::spawn({
            let spec = spec.clone();
            let compactions_store = compactions_store.clone();
            let system_clock = system_clock.clone();
            async move {
                Compactor::submit(
                    spec,
                    compactions_store,
                    Arc::new(DbRand::default()),
                    system_clock,
                )
                .await
            }
        });

        gated_os.put_opts_gate.wait_for_arrivals(arrivals + 1).await;

        // The external writer wins the stale writer's intended id.
        let mut external = StoredCompactions::load(external_compactions_store.clone())
            .await
            .unwrap();
        external
            .update(external.prepare_dirty().unwrap())
            .await
            .unwrap();
        assert_eq!(external.id(), start_id + 1);

        // A second external write leaves a live latest version above the future boundary,
        // so the stale writer can make progress after refreshing.
        external
            .update(external.prepare_dirty().unwrap())
            .await
            .unwrap();
        assert_eq!(external.id(), start_id + 2);

        // GC fences and removes the stale writer's next id, but preserves the live latest
        // version at start_id + 2.
        external_compactions_store
            .advance_boundary(start_id + 1)
            .await
            .unwrap();
        external_compactions_store
            .delete_compactions(start_id + 1)
            .await
            .unwrap();
        let latest = external_compactions_store
            .read_latest_compactions()
            .await
            .unwrap()
            .id;
        assert_eq!(latest, start_id + 2);

        gated_os.put_opts_gate.release();

        // Submit should refresh to the live newer version and retry at the next safe id.
        let compaction_id = submit_task.await.unwrap().unwrap();

        let compactions = compactions_store.read_latest_compactions().await.unwrap();
        let stored = compactions
            .compactions
            .get(&compaction_id)
            .expect("missing submitted compaction");

        assert_eq!(compactions.id, start_id + 3);
        assert_eq!(stored.spec(), &spec);
        assert_eq!(stored.status(), CompactionStatus::Submitted);
    }

    #[tokio::test]
    async fn test_submit_full_compaction_uses_sorted_run_sources_only() {
        let os = Arc::new(InMemory::new());
        let (manifest_store, compactions_store, _table_store) = build_test_stores(os.clone());
        let system_clock: Arc<dyn SystemClock> = Arc::new(DefaultSystemClock::new());

        StoredManifest::create_new_db(
            manifest_store.clone(),
            ManifestCore::new(),
            system_clock.clone(),
        )
        .await
        .unwrap();
        let mut stored_manifest =
            StoredManifest::load(manifest_store.clone(), system_clock.clone())
                .await
                .unwrap();

        let mut dirty = stored_manifest.prepare_dirty().unwrap();
        let l0_info = SsTableInfo {
            first_entry: Some(Bytes::from_static(b"a")),
            ..SsTableInfo::default()
        };
        let sr_info = SsTableInfo {
            first_entry: Some(Bytes::from_static(b"m")),
            ..SsTableInfo::default()
        };
        let l0_view_newest: SsTableView = SsTableView::identity(SsTableHandle::new(
            SsTableId::Compacted(Ulid::new()),
            SST_FORMAT_VERSION_LATEST,
            l0_info.clone(),
        ));
        let l0_view_oldest: SsTableView = SsTableView::identity(SsTableHandle::new(
            SsTableId::Compacted(Ulid::new()),
            SST_FORMAT_VERSION_LATEST,
            l0_info.clone(),
        ));
        Arc::make_mut(&mut dirty.value.core.tree).l0 =
            VecDeque::from(vec![l0_view_newest, l0_view_oldest]);
        Arc::make_mut(&mut dirty.value.core.tree).compacted = vec![
            SortedRun {
                id: 2,
                sst_views: vec![SsTableView::identity(SsTableHandle::new(
                    SsTableId::Compacted(Ulid::new()),
                    SST_FORMAT_VERSION_LATEST,
                    sr_info.clone(),
                ))],
            },
            SortedRun {
                id: 1,
                sst_views: vec![SsTableView::identity(SsTableHandle::new(
                    SsTableId::Compacted(Ulid::new()),
                    SST_FORMAT_VERSION_LATEST,
                    sr_info.clone(),
                ))],
            },
        ];
        stored_manifest.update(dirty).await.unwrap();

        StoredCompactions::create(
            compactions_store.clone(),
            stored_manifest.manifest().compactor_epoch,
        )
        .await
        .unwrap();

        let scheduler = MockScheduler::new();
        let specs = scheduler
            .generate(
                &CompactorStateView {
                    compactions: None,
                    manifest: VersionedManifest::from_manifest(
                        0,
                        stored_manifest.manifest().clone(),
                    ),
                },
                &CompactionRequest::FullSegment {
                    segment: Bytes::new(),
                },
            )
            .unwrap();
        assert_eq!(specs.len(), 1);
        let compaction_id = Compactor::submit(
            specs[0].clone(),
            compactions_store.clone(),
            Arc::new(DbRand::default()),
            system_clock.clone(),
        )
        .await
        .unwrap();

        let compactions = compactions_store.read_latest_compactions().await.unwrap();
        let stored = compactions
            .compactions
            .get(&compaction_id)
            .expect("missing submitted compaction");
        let expected_sources = vec![SourceId::SortedRun(2), SourceId::SortedRun(1)];

        assert_eq!(stored.spec().sources(), &expected_sources);
        assert_eq!(stored.spec().destination(), Some(1));
        assert_eq!(stored.status(), CompactionStatus::Submitted);
    }

    #[test]
    fn test_plan_spec_returns_spec_clone() {
        let scheduler = MockScheduler::new();
        let state = CompactorStateView {
            compactions: None,
            manifest: VersionedManifest::from_manifest(0, Manifest::initial(ManifestCore::new())),
        };
        let spec = CompactionSpec::new(vec![SourceId::SortedRun(7)], 7);

        let planned = scheduler
            .generate(&state, &CompactionRequest::Spec(spec.clone()))
            .unwrap();

        assert_eq!(planned, vec![spec]);
    }

    #[test]
    fn test_plan_full_segment_root_uses_sorted_runs_and_min_destination() {
        let scheduler = MockScheduler::new();
        let mut core = ManifestCore::new();
        let l0_info = SsTableInfo {
            first_entry: Some(Bytes::from_static(b"a")),
            ..SsTableInfo::default()
        };
        let sr_info = SsTableInfo {
            first_entry: Some(Bytes::from_static(b"m")),
            ..SsTableInfo::default()
        };
        let l0_view_first: SsTableView = SsTableView::identity(SsTableHandle::new(
            SsTableId::Compacted(Ulid::from_parts(1, 0)),
            SST_FORMAT_VERSION_LATEST,
            l0_info.clone(),
        ));
        let l0_view_second: SsTableView = SsTableView::identity(SsTableHandle::new(
            SsTableId::Compacted(Ulid::from_parts(2, 0)),
            SST_FORMAT_VERSION_LATEST,
            l0_info,
        ));
        Arc::make_mut(&mut core.tree).l0 = VecDeque::from(vec![l0_view_first, l0_view_second]);
        Arc::make_mut(&mut core.tree).compacted = vec![
            SortedRun {
                id: 5,
                sst_views: vec![SsTableView::identity(SsTableHandle::new(
                    SsTableId::Compacted(Ulid::from_parts(10, 0)),
                    SST_FORMAT_VERSION_LATEST,
                    sr_info.clone(),
                ))],
            },
            SortedRun {
                id: 2,
                sst_views: vec![SsTableView::identity(SsTableHandle::new(
                    SsTableId::Compacted(Ulid::from_parts(11, 0)),
                    SST_FORMAT_VERSION_LATEST,
                    sr_info,
                ))],
            },
        ];
        let state = CompactorStateView {
            compactions: None,
            manifest: VersionedManifest::from_manifest(0, Manifest::initial(core)),
        };

        let planned = scheduler
            .generate(
                &state,
                &CompactionRequest::FullSegment {
                    segment: Bytes::new(),
                },
            )
            .unwrap();

        let expected_sources = vec![SourceId::SortedRun(5), SourceId::SortedRun(2)];
        assert_eq!(planned.len(), 1);
        assert_eq!(planned[0].sources(), &expected_sources);
        assert_eq!(planned[0].destination(), Some(2));
    }

    #[test]
    fn test_plan_full_segment_root_without_sorted_runs_is_invalid() {
        let scheduler = MockScheduler::new();
        let mut core = ManifestCore::new();
        let l0_info = SsTableInfo {
            first_entry: Some(Bytes::from_static(b"a")),
            ..SsTableInfo::default()
        };
        Arc::make_mut(&mut core.tree).l0 = VecDeque::from(vec![
            SsTableView::identity(SsTableHandle::new(
                SsTableId::Compacted(Ulid::from_parts(1, 0)),
                SST_FORMAT_VERSION_LATEST,
                l0_info.clone(),
            )),
            SsTableView::identity(SsTableHandle::new(
                SsTableId::Compacted(Ulid::from_parts(2, 0)),
                SST_FORMAT_VERSION_LATEST,
                l0_info,
            )),
        ]);
        let state = CompactorStateView {
            compactions: None,
            manifest: VersionedManifest::from_manifest(0, Manifest::initial(core)),
        };

        let err = scheduler
            .generate(
                &state,
                &CompactionRequest::FullSegment {
                    segment: Bytes::new(),
                },
            )
            .expect_err(
                "full-segment should reject empty or L0-only inputs because L0 SSTs are excluded",
            );

        assert_eq!(err.kind(), crate::ErrorKind::Invalid);
        assert_eq!(err.to_string(), "Invalid error: invalid compaction");
    }

    #[test]
    fn test_plan_full_segment_targets_named_segment() {
        // FullSegment against a named segment must pull sources from that
        // segment's tree, not the root tree. RFC-0024 routes named-segment
        // SRs through `core.segments[..].tree.compacted`, so a request
        // against `b"key"` should ignore root-tree SRs entirely.
        let scheduler = MockScheduler::new();
        let mut core = ManifestCore::new();
        core.segment_extractor_name = Some("test".into());
        let sr_info = SsTableInfo {
            first_entry: Some(Bytes::from_static(b"key1")),
            ..SsTableInfo::default()
        };
        core.segments = vec![Segment {
            prefix: Bytes::from_static(b"key"),
            tree: Arc::new(LsmTreeState {
                last_compacted_l0_sst_view_id: None,
                last_compacted_l0_sst_id: None,
                l0: VecDeque::new(),
                compacted: vec![
                    SortedRun {
                        id: 7,
                        sst_views: vec![SsTableView::identity(SsTableHandle::new(
                            SsTableId::Compacted(Ulid::from_parts(70, 0)),
                            SST_FORMAT_VERSION_LATEST,
                            sr_info.clone(),
                        ))],
                    },
                    SortedRun {
                        id: 3,
                        sst_views: vec![SsTableView::identity(SsTableHandle::new(
                            SsTableId::Compacted(Ulid::from_parts(30, 0)),
                            SST_FORMAT_VERSION_LATEST,
                            sr_info,
                        ))],
                    },
                ],
            }),
        }];
        let state = CompactorStateView {
            compactions: None,
            manifest: VersionedManifest::from_manifest(0, Manifest::initial(core)),
        };

        let planned = scheduler
            .generate(
                &state,
                &CompactionRequest::FullSegment {
                    segment: Bytes::from_static(b"key"),
                },
            )
            .unwrap();

        assert_eq!(planned.len(), 1);
        assert_eq!(planned[0].segment().as_ref(), b"key");
        assert_eq!(
            planned[0].sources(),
            &vec![SourceId::SortedRun(7), SourceId::SortedRun(3)]
        );
        assert_eq!(planned[0].destination(), Some(3));
    }

    #[test]
    fn test_plan_full_segment_unknown_segment_is_invalid() {
        // Asking to compact a non-empty segment prefix on an unsegmented DB
        // (root has sorted runs, no segments configured) must be rejected.
        // A segment-blind implementation that ignored the prefix would
        // happily return root SRs here — this asserts the routing actually
        // looks the segment up.
        let scheduler = MockScheduler::new();
        let mut core = ManifestCore::new();
        let sr_info = SsTableInfo {
            first_entry: Some(Bytes::from_static(b"r")),
            ..SsTableInfo::default()
        };
        Arc::make_mut(&mut core.tree).compacted = vec![SortedRun {
            id: 9,
            sst_views: vec![SsTableView::identity(SsTableHandle::new(
                SsTableId::Compacted(Ulid::from_parts(90, 0)),
                SST_FORMAT_VERSION_LATEST,
                sr_info,
            ))],
        }];
        let state = CompactorStateView {
            compactions: None,
            manifest: VersionedManifest::from_manifest(0, Manifest::initial(core)),
        };

        let err = scheduler
            .generate(
                &state,
                &CompactionRequest::FullSegment {
                    segment: Bytes::from_static(b"missing"),
                },
            )
            .expect_err("full-segment against unknown segment should fail");

        assert_eq!(err.kind(), crate::ErrorKind::Invalid);
    }

    #[test]
    fn test_plan_full_segment_single_sorted_run_is_allowed() {
        // A tree with exactly one SR is a legitimate target — the merge
        // pipeline still applies retention, TTL, compaction filters, and
        // merge-operator collapses. Reject only when there are no SRs at all.
        let scheduler = MockScheduler::new();
        let mut core = ManifestCore::new();
        let sr_info = SsTableInfo {
            first_entry: Some(Bytes::from_static(b"a")),
            ..SsTableInfo::default()
        };
        Arc::make_mut(&mut core.tree).compacted = vec![SortedRun {
            id: 4,
            sst_views: vec![SsTableView::identity(SsTableHandle::new(
                SsTableId::Compacted(Ulid::from_parts(40, 0)),
                SST_FORMAT_VERSION_LATEST,
                sr_info,
            ))],
        }];
        let state = CompactorStateView {
            compactions: None,
            manifest: VersionedManifest::from_manifest(0, Manifest::initial(core)),
        };

        let planned = scheduler
            .generate(
                &state,
                &CompactionRequest::FullSegment {
                    segment: Bytes::new(),
                },
            )
            .unwrap();

        assert_eq!(planned.len(), 1);
        assert_eq!(planned[0].sources(), &vec![SourceId::SortedRun(4)]);
        assert_eq!(planned[0].destination(), Some(4));
    }

    #[test]
    fn test_plan_full_sweeps_root_and_every_named_segment() {
        // Full must produce one spec per eligible tree, ordered as
        // `trees_with_prefix()` yields them: root first, then segments by
        // ascending prefix.
        let scheduler = MockScheduler::new();
        let mut core = ManifestCore::new();
        let info = SsTableInfo {
            first_entry: Some(Bytes::from_static(b"x")),
            ..SsTableInfo::default()
        };
        Arc::make_mut(&mut core.tree).compacted = vec![
            SortedRun {
                id: 8,
                sst_views: vec![SsTableView::identity(SsTableHandle::new(
                    SsTableId::Compacted(Ulid::from_parts(80, 0)),
                    SST_FORMAT_VERSION_LATEST,
                    info.clone(),
                ))],
            },
            SortedRun {
                id: 4,
                sst_views: vec![SsTableView::identity(SsTableHandle::new(
                    SsTableId::Compacted(Ulid::from_parts(40, 0)),
                    SST_FORMAT_VERSION_LATEST,
                    info.clone(),
                ))],
            },
        ];
        core.segment_extractor_name = Some("test".into());
        core.segments = vec![
            Segment {
                prefix: Bytes::from_static(b"a/"),
                tree: Arc::new(LsmTreeState {
                    last_compacted_l0_sst_view_id: None,
                    last_compacted_l0_sst_id: None,
                    l0: VecDeque::new(),
                    compacted: vec![SortedRun {
                        id: 3,
                        sst_views: vec![SsTableView::identity(SsTableHandle::new(
                            SsTableId::Compacted(Ulid::from_parts(30, 0)),
                            SST_FORMAT_VERSION_LATEST,
                            info.clone(),
                        ))],
                    }],
                }),
            },
            Segment {
                prefix: Bytes::from_static(b"b/"),
                tree: Arc::new(LsmTreeState {
                    last_compacted_l0_sst_view_id: None,
                    last_compacted_l0_sst_id: None,
                    l0: VecDeque::new(),
                    compacted: vec![
                        SortedRun {
                            id: 9,
                            sst_views: vec![SsTableView::identity(SsTableHandle::new(
                                SsTableId::Compacted(Ulid::from_parts(90, 0)),
                                SST_FORMAT_VERSION_LATEST,
                                info.clone(),
                            ))],
                        },
                        SortedRun {
                            id: 6,
                            sst_views: vec![SsTableView::identity(SsTableHandle::new(
                                SsTableId::Compacted(Ulid::from_parts(60, 0)),
                                SST_FORMAT_VERSION_LATEST,
                                info,
                            ))],
                        },
                    ],
                }),
            },
        ];
        let state = CompactorStateView {
            compactions: None,
            manifest: VersionedManifest::from_manifest(0, Manifest::initial(core)),
        };

        let planned = scheduler
            .generate(&state, &CompactionRequest::Full)
            .unwrap();

        assert_eq!(planned.len(), 3);
        assert_eq!(planned[0].segment().as_ref(), b"");
        assert_eq!(
            planned[0].sources(),
            &vec![SourceId::SortedRun(8), SourceId::SortedRun(4)]
        );
        assert_eq!(planned[0].destination(), Some(4));
        assert_eq!(planned[1].segment().as_ref(), b"a/");
        assert_eq!(planned[1].sources(), &vec![SourceId::SortedRun(3)]);
        assert_eq!(planned[1].destination(), Some(3));
        assert_eq!(planned[2].segment().as_ref(), b"b/");
        assert_eq!(
            planned[2].sources(),
            &vec![SourceId::SortedRun(9), SourceId::SortedRun(6)]
        );
        assert_eq!(planned[2].destination(), Some(6));
    }

    #[test]
    fn test_plan_full_skips_trees_without_sorted_runs() {
        // Best-effort semantics: a tree with no compacted SRs is silently
        // skipped (even if it has L0 SSTs), but eligible peers still produce
        // specs. This is the key behavioral difference from FullSegment,
        // which errors when explicitly asked to compact such a tree.
        let scheduler = MockScheduler::new();
        let mut core = ManifestCore::new();
        let info = SsTableInfo {
            first_entry: Some(Bytes::from_static(b"x")),
            ..SsTableInfo::default()
        };
        Arc::make_mut(&mut core.tree).compacted = vec![SortedRun {
            id: 5,
            sst_views: vec![SsTableView::identity(SsTableHandle::new(
                SsTableId::Compacted(Ulid::from_parts(50, 0)),
                SST_FORMAT_VERSION_LATEST,
                info.clone(),
            ))],
        }];
        core.segment_extractor_name = Some("test".into());
        core.segments = vec![
            // L0-only: still skipped because L0 SSTs are ineligible inputs.
            Segment {
                prefix: Bytes::from_static(b"l0only/"),
                tree: Arc::new(LsmTreeState {
                    last_compacted_l0_sst_view_id: None,
                    last_compacted_l0_sst_id: None,
                    l0: VecDeque::from(vec![SsTableView::identity(SsTableHandle::new(
                        SsTableId::Compacted(Ulid::from_parts(1, 0)),
                        SST_FORMAT_VERSION_LATEST,
                        info,
                    ))]),
                    compacted: vec![],
                }),
            },
            // Fully empty tree: also skipped.
            Segment {
                prefix: Bytes::from_static(b"none/"),
                tree: Arc::new(LsmTreeState {
                    last_compacted_l0_sst_view_id: None,
                    last_compacted_l0_sst_id: None,
                    l0: VecDeque::new(),
                    compacted: vec![],
                }),
            },
        ];
        let state = CompactorStateView {
            compactions: None,
            manifest: VersionedManifest::from_manifest(0, Manifest::initial(core)),
        };

        let planned = scheduler
            .generate(&state, &CompactionRequest::Full)
            .unwrap();

        assert_eq!(planned.len(), 1);
        assert_eq!(planned[0].segment().as_ref(), b"");
        assert_eq!(planned[0].sources(), &vec![SourceId::SortedRun(5)]);
    }

    #[test]
    fn test_plan_full_returns_empty_when_no_tree_is_eligible() {
        // Empty DB / no compacted SRs anywhere → Ok(vec![]), not an error.
        let scheduler = MockScheduler::new();
        let state = CompactorStateView {
            compactions: None,
            manifest: VersionedManifest::from_manifest(0, Manifest::initial(ManifestCore::new())),
        };

        let planned = scheduler
            .generate(&state, &CompactionRequest::Full)
            .unwrap();

        assert!(planned.is_empty(), "expected empty plan, got {:?}", planned);
    }

    struct CompactorEventHandlerTestFixture {
        manifest: StoredManifest,
        manifest_store: Arc<ManifestStore>,
        compactions_store: Arc<CompactionsStore>,
        options: Settings,
        db: Db,
        scheduler: Arc<MockScheduler>,
        real_executor: Arc<TokioCompactionExecutor>,
        real_executor_rx: async_channel::Receiver<WorkerMessage>,
        test_recorder: Arc<slatedb_common::metrics::DefaultMetricsRecorder>,
        handler: CompactorEventHandler,
    }

    impl CompactorEventHandlerTestFixture {
        async fn new() -> Self {
            let compactor_options = Arc::new(compactor_options());
            let worker_options = Arc::new(CompactionWorkerOptions::default());
            let options = db_options(None);

            let os = Arc::new(InMemory::new());
            let (manifest_store, compactions_store, table_store) = build_test_stores(os.clone());
            let db = Db::builder(PATH, os.clone())
                .with_settings(options.clone())
                .build()
                .await
                .unwrap();

            let scheduler = Arc::new(MockScheduler::new());
            let (real_executor_tx, real_executor_rx) = async_channel::unbounded();
            let rand = Arc::new(DbRand::default());
            let test_recorder = Arc::new(slatedb_common::metrics::DefaultMetricsRecorder::new());
            let recorder = MetricsRecorderHelper::new(
                test_recorder.clone() as Arc<dyn slatedb_common::metrics::MetricsRecorder>,
                slatedb_common::metrics::MetricLevel::default(),
            );
            let compactor_stats = Arc::new(CompactionStats::new(&recorder));
            let real_executor = Arc::new(TokioCompactionExecutor::new(
                TokioCompactionExecutorOptions {
                    handle: Handle::current(),
                    options: worker_options.clone(),
                    worker_tx: real_executor_tx,
                    table_store,
                    rand: rand.clone(),
                    stats: compactor_stats.clone(),
                    worker_stats: stats::WorkerStats::new(&recorder, "test-worker"),
                    clock: Arc::new(DefaultSystemClock::new()),
                    manifest_store: manifest_store.clone(),
                    merge_operator: None,
                    #[cfg(feature = "compaction_filters")]
                    compaction_filter_supplier: None,
                },
            ));
            let handler = CompactorEventHandler::new(
                manifest_store.clone(),
                compactions_store.clone(),
                compactor_options.clone(),
                scheduler.clone(),
                rand.clone(),
                compactor_stats.clone(),
                Arc::new(DefaultSystemClock::new()),
                MetricsRecorderHelper::noop(),
            )
            .await
            .unwrap();
            let manifest =
                StoredManifest::load(manifest_store.clone(), Arc::new(DefaultSystemClock::new()))
                    .await
                    .unwrap();
            Self {
                manifest,
                manifest_store,
                compactions_store,
                options,
                db,
                scheduler,
                real_executor_rx,
                real_executor,
                test_recorder,
                handler,
            }
        }

        /// Like `new()` but accepts an explicit `system_clock` so tests can
        /// control time (e.g. to exercise heartbeat-timeout reclamation).
        async fn new_with_clock(
            system_clock: Arc<dyn SystemClock>,
            compactor_options: Arc<CompactorOptions>,
        ) -> Self {
            let options = db_options(None);

            let os = Arc::new(InMemory::new());
            let (manifest_store, compactions_store, table_store) = build_test_stores(os.clone());
            let db = Db::builder(PATH, os.clone())
                .with_settings(options.clone())
                .build()
                .await
                .unwrap();

            let scheduler = Arc::new(MockScheduler::new());
            let (real_executor_tx, real_executor_rx) = async_channel::unbounded();
            let rand = Arc::new(DbRand::default());
            let test_recorder = Arc::new(slatedb_common::metrics::DefaultMetricsRecorder::new());
            let recorder = MetricsRecorderHelper::new(
                test_recorder.clone() as Arc<dyn slatedb_common::metrics::MetricsRecorder>,
                slatedb_common::metrics::MetricLevel::default(),
            );
            let compactor_stats = Arc::new(CompactionStats::new(&recorder));
            let worker_options = Arc::new(CompactionWorkerOptions::default());
            let real_executor = Arc::new(TokioCompactionExecutor::new(
                TokioCompactionExecutorOptions {
                    handle: Handle::current(),
                    options: worker_options,
                    worker_tx: real_executor_tx,
                    table_store,
                    rand: rand.clone(),
                    stats: compactor_stats.clone(),
                    worker_stats: stats::WorkerStats::noop(),
                    clock: system_clock.clone(),
                    manifest_store: manifest_store.clone(),
                    merge_operator: None,
                    #[cfg(feature = "compaction_filters")]
                    compaction_filter_supplier: None,
                },
            ));
            let handler = CompactorEventHandler::new(
                manifest_store.clone(),
                compactions_store.clone(),
                compactor_options.clone(),
                scheduler.clone(),
                rand.clone(),
                compactor_stats.clone(),
                system_clock.clone(),
                recorder.clone(),
            )
            .await
            .unwrap();
            let manifest = StoredManifest::load(manifest_store.clone(), system_clock)
                .await
                .unwrap();
            Self {
                manifest,
                manifest_store,
                compactions_store,
                options,
                db,
                scheduler,
                real_executor_rx,
                real_executor,
                test_recorder,
                handler,
            }
        }

        async fn latest_db_state(&mut self) -> ManifestCore {
            self.manifest.refresh().await.unwrap().core.clone()
        }

        async fn write_l0(&mut self) {
            let mut rng = rng::new_test_rng(None);
            let manifest = self.manifest.refresh().await.unwrap();
            let l0s = manifest.core.tree.l0.len();
            // TODO: add an explicit flush_memtable fn to db and use that instead
            let mut k = vec![0u8; self.options.l0_sst_size_bytes];
            rng.fill_bytes(&mut k);
            self.db.put(&k, &[b'x'; 10]).await.unwrap();
            self.db.flush().await.unwrap();
            loop {
                let manifest = self.manifest.refresh().await.unwrap().clone();
                if manifest.core.tree.l0.len() > l0s {
                    break;
                }
            }
        }

        async fn build_l0_compaction(&mut self) -> CompactionSpec {
            let db_state = self.latest_db_state().await;
            let l0_ids_to_compact: Vec<SourceId> = db_state
                .tree
                .l0
                .iter()
                .map(|h| SourceId::SstView(h.id))
                .collect();
            CompactionSpec::new(l0_ids_to_compact, 0)
        }

        /// Returns all `Scheduled` compactions currently in `.compactions`.
        async fn get_scheduled_compactions(&self) -> Vec<Compaction> {
            self.compactions_store
                .read_latest_compactions()
                .await
                .unwrap()
                .compactions
                .iter()
                .filter(|c| c.status() == CompactionStatus::Scheduled)
                .cloned()
                .collect()
        }

        /// Simulates a worker claiming each `Scheduled` compaction, executing it
        /// with the real `TokioCompactionExecutor`, and writing `Compacted` + output
        /// SSTs back to `.compactions`. Call `handle_ticker()` afterward so the
        /// coordinator commits the results via `commit_compacted_entries`.
        async fn simulate_worker_completes(&self) {
            use crate::compactor_executor::StartCompactionJobArgs;

            let manifest = self.manifest_store.read_latest_manifest().await.unwrap();
            let db_state = manifest.core();

            let scheduled = self.get_scheduled_compactions().await;
            for compaction in scheduled {
                let destination = compaction.spec().destination().expect("tiered spec");
                let l0_sst_views = compaction.get_l0_sst_views(db_state);
                let sorted_runs = compaction.get_sorted_runs(db_state);
                let is_dest_last_run = match db_state.tree_for_segment(compaction.spec().segment())
                {
                    Some(tree) => {
                        tree.compacted.is_empty()
                            || tree.compacted.last().is_some_and(|sr| destination == sr.id)
                    }
                    None => false,
                };
                let args = StartCompactionJobArgs {
                    id: compaction.id(),
                    compaction_id: compaction.id(),
                    destination,
                    l0_sst_views,
                    sorted_runs,
                    compaction_clock_tick: db_state.last_l0_clock_tick,
                    is_dest_last_run,
                    retention_min_seq: None,
                    ctx: compaction.ctx().cloned(),
                };

                self.real_executor.start_compaction_job(args);

                let result = tokio::time::timeout(Duration::from_millis(500), async {
                    loop {
                        match self.real_executor_rx.recv().await.expect("channel closed") {
                            WorkerMessage::CompactionJobFinished { result, .. } => {
                                return result.unwrap()
                            }
                            _ => continue,
                        }
                    }
                })
                .await
                .expect("timeout waiting for compaction result");

                let mut stored = StoredCompactions::try_load(self.compactions_store.clone())
                    .await
                    .unwrap()
                    .unwrap();
                loop {
                    stored.refresh().await.unwrap();
                    let mut dirty = stored.prepare_dirty().unwrap();
                    let completed = compaction
                        .clone()
                        .with_status(CompactionStatus::Compacted)
                        .with_output_ssts(result.sst_views.iter().map(|v| v.sst.clone()).collect())
                        .with_ctx(None);
                    dirty.value.insert(completed);
                    match stored.update(dirty).await {
                        Ok(()) => break,
                        Err(e) if e.is_sequenced_write_conflict() => continue,
                        Err(e) => panic!("write_compacted failed: {e}"),
                    }
                }
            }
        }
    }

    #[tokio::test]
    async fn test_should_record_last_compaction_ts() {
        // given:
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        fixture.write_l0().await;
        let compaction = fixture.build_l0_compaction().await;
        fixture.scheduler.inject_compaction(compaction.clone());
        fixture.handler.handle_ticker().await.unwrap();
        fixture.simulate_worker_completes().await;

        let starting_last_ts =
            slatedb_common::metrics::lookup_metric(&fixture.test_recorder, LAST_COMPACTION_TS_SEC)
                .expect("metric not found");

        // when: coordinator commits the Compacted entry
        fixture.handler.handle_ticker().await.unwrap();

        // then:
        let last_ts =
            slatedb_common::metrics::lookup_metric(&fixture.test_recorder, LAST_COMPACTION_TS_SEC)
                .expect("metric not found");
        assert!(last_ts > starting_last_ts);
    }

    #[tokio::test]
    async fn test_should_write_manifest_safely() {
        // given:
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        fixture.write_l0().await;
        let compaction = fixture.build_l0_compaction().await;
        fixture.scheduler.inject_compaction(compaction.clone());
        fixture.handler.handle_ticker().await.unwrap();
        fixture.simulate_worker_completes().await;
        // write an l0 before the coordinator commits
        fixture.write_l0().await;

        // when: coordinator commits the Compacted entry
        fixture.handler.handle_ticker().await.unwrap();

        // then:
        let db_state = fixture.latest_db_state().await;
        assert_eq!(db_state.tree.l0.len(), 1);
        assert_eq!(db_state.tree.compacted.len(), 1);
        let l0_id = db_state
            .tree
            .l0
            .front()
            .unwrap()
            .sst
            .id
            .unwrap_compacted_id();
        let compacted_l0s: Vec<Ulid> = db_state
            .tree
            .compacted
            .first()
            .unwrap()
            .sst_views
            .iter()
            .map(|view| view.sst.id.unwrap_compacted_id())
            .collect();
        assert!(!compacted_l0s.contains(&l0_id));
        assert_eq!(
            db_state.tree.last_compacted_l0_sst_view_id.unwrap(),
            compaction
                .sources()
                .first()
                .and_then(|id| id.maybe_unwrap_sst_view())
                .unwrap()
        );
    }

    #[tokio::test]
    async fn test_should_persist_compactions_on_start_and_finish() {
        // given:
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        fixture.write_l0().await;
        let compaction = fixture.build_l0_compaction().await;
        fixture.scheduler.inject_compaction(compaction.clone());

        // when: schedule compaction → Scheduled persisted
        fixture.handler.handle_ticker().await.unwrap();

        let stored_compactions = fixture
            .compactions_store
            .read_latest_compactions()
            .await
            .unwrap()
            .compactions;
        assert_eq!(
            stored_compactions
                .iter()
                .collect::<Vec<&Compaction>>()
                .len(),
            1
        );
        let scheduled_id = stored_compactions
            .iter()
            .next()
            .expect("compaction should be persisted")
            .id();
        let state_id = fixture
            .handler
            .state()
            .active_compactions()
            .next()
            .expect("state missing compaction")
            .id();
        assert_eq!(scheduled_id, state_id);

        // worker executes, writes Compacted; coordinator commits on next tick
        fixture.simulate_worker_completes().await;
        fixture.handler.handle_ticker().await.unwrap();

        // then: finished compaction is retained (one entry for GC)
        let stored_compactions = fixture
            .compactions_store
            .read_latest_compactions()
            .await
            .unwrap()
            .compactions;
        let mut iter = stored_compactions.iter();
        assert_eq!(
            iter.next()
                .expect("compactions should not be empty after finish")
                .id(),
            scheduled_id,
        );
        assert!(
            iter.next().is_none(),
            "expected only one retained finished compaction for GC"
        );
    }

    #[tokio::test]
    async fn test_maybe_schedule_compactions_only_submits() {
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        fixture.write_l0().await;
        // Refresh so add_compaction sees the just-flushed L0; the source-
        // isolation check needs the spec's source view to be present in the
        // compactor's local tree.
        fixture.handler.state_writer.refresh().await.unwrap();
        let compaction = fixture.build_l0_compaction().await;
        fixture.scheduler.inject_compaction(compaction);

        fixture.handler.maybe_schedule_compactions().await.unwrap();

        let mut compactions = fixture.handler.state().active_compactions();
        let scheduled = compactions.next().expect("missing compaction");
        assert_eq!(scheduled.status(), CompactionStatus::Submitted);
        assert!(compactions.next().is_none());

        let stored_compactions = fixture
            .compactions_store
            .read_latest_compactions()
            .await
            .unwrap()
            .compactions;
        let stored = stored_compactions
            .iter()
            .next()
            .expect("compaction should be persisted");
        assert_eq!(stored.status(), CompactionStatus::Submitted);
    }

    #[tokio::test]
    async fn test_maybe_validate_submitted_compactions_promotes_to_scheduled() {
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        fixture.write_l0().await;
        fixture.handler.state_writer.refresh().await.unwrap();
        let spec = fixture.build_l0_compaction().await;
        let compaction_id = Ulid::new();
        fixture
            .handler
            .state_mut()
            .add_compaction(Compaction::new(compaction_id, spec))
            .expect("failed to add compaction");

        fixture
            .handler
            .maybe_validate_submitted_compactions()
            .await
            .unwrap();

        // Scheduled is "ready to claim", not "running" — no local execution happens.
        let compactions = &fixture.handler.state_writer.state.compactions().value;
        assert_eq!(
            compactions
                .get(&compaction_id)
                .expect("missing compaction")
                .status(),
            CompactionStatus::Scheduled
        );
        // Scheduled is persisted so remote workers (and a restarted coordinator)
        // observe the same view.
        let stored_compactions = fixture
            .compactions_store
            .read_latest_compactions()
            .await
            .unwrap()
            .compactions;
        assert_eq!(
            stored_compactions
                .get(&compaction_id)
                .expect("missing stored compaction")
                .status(),
            CompactionStatus::Scheduled
        );
    }

    #[tokio::test]
    async fn test_maybe_validate_submitted_compactions_marks_invalid_failed() {
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        let compaction_id = Ulid::new();
        fixture
            .handler
            .state_mut()
            .add_compaction(Compaction::new(
                compaction_id,
                CompactionSpec::new(Vec::new(), 0), // invalid: no sources
            ))
            .expect("failed to add compaction");

        fixture
            .handler
            .maybe_validate_submitted_compactions()
            .await
            .unwrap();

        let compactions = &fixture.handler.state_writer.state.compactions().value;
        assert_eq!(
            compactions
                .get(&compaction_id)
                .expect("missing compaction")
                .status(),
            CompactionStatus::Failed
        );
        let stored_compactions = fixture
            .compactions_store
            .read_latest_compactions()
            .await
            .unwrap()
            .compactions;
        assert_eq!(
            stored_compactions
                .get(&compaction_id)
                .expect("missing stored compaction")
                .status(),
            CompactionStatus::Failed
        );
    }

    #[tokio::test]
    async fn test_handle_ticker_starts_preexisting_submitted_compaction() {
        let compactor_options = Arc::new(compactor_options());
        let options = db_options(None);

        let os = Arc::new(InMemory::new());
        let (manifest_store, compactions_store, _table_store) = build_test_stores(os.clone());
        let db = Db::builder(PATH, os.clone())
            .with_settings(options.clone())
            .build()
            .await
            .unwrap();

        let system_clock: Arc<dyn SystemClock> = Arc::new(DefaultSystemClock::new());
        let mut stored_manifest =
            StoredManifest::load(manifest_store.clone(), system_clock.clone())
                .await
                .unwrap();

        // Create an L0 so the submitted compaction has valid sources.
        let mut rng = rng::new_test_rng(None);
        let manifest = stored_manifest.refresh().await.unwrap();
        let l0s = manifest.core.tree.l0.len();
        let mut k = vec![0u8; options.l0_sst_size_bytes];
        rng.fill_bytes(&mut k);
        db.put(&k, &[b'x'; 10]).await.unwrap();
        db.flush().await.unwrap();
        loop {
            let manifest = stored_manifest.refresh().await.unwrap().clone();
            if manifest.core.tree.l0.len() > l0s {
                break;
            }
        }

        // Seed the compactions store with a Submitted compaction before handler startup.
        let db_state = stored_manifest.refresh().await.unwrap().core.clone();
        let sources = db_state
            .tree
            .l0
            .iter()
            .map(|h| SourceId::SstView(h.id))
            .collect::<Vec<_>>();
        let spec = CompactionSpec::new(sources, 0);
        let compaction_id = Ulid::new();
        let compaction = Compaction::new(compaction_id, spec);

        let mut stored_compactions = StoredCompactions::create(
            compactions_store.clone(),
            stored_manifest.manifest().compactor_epoch,
        )
        .await
        .unwrap();
        let mut dirty = stored_compactions.prepare_dirty().unwrap();
        dirty.value.insert(compaction);
        stored_compactions.update(dirty).await.unwrap();

        // Build the handler and trigger a ticker to pick up the pre-existing Submitted entry.
        let scheduler = Arc::new(MockScheduler::new());
        let rand = Arc::new(DbRand::default());
        let recorder = slatedb_common::metrics::MetricsRecorderHelper::noop();
        let compactor_stats = Arc::new(CompactionStats::new(&recorder));
        let mut handler = CompactorEventHandler::new(
            manifest_store,
            compactions_store.clone(),
            compactor_options,
            scheduler,
            rand,
            compactor_stats,
            system_clock,
            recorder,
        )
        .await
        .unwrap();

        handler.handle_ticker().await.unwrap();

        // The pre-existing Submitted compaction should be promoted to Scheduled.
        let stored = compactions_store
            .read_latest_compactions()
            .await
            .unwrap()
            .compactions;
        assert_eq!(
            stored
                .get(&compaction_id)
                .expect("missing stored compaction")
                .status(),
            CompactionStatus::Scheduled
        );
    }

    #[tokio::test]
    async fn test_should_fail_when_compactions_store_fences_compactor() {
        // given:
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        fixture.write_l0().await;
        let compaction = fixture.build_l0_compaction().await;
        fixture.scheduler.inject_compaction(compaction.clone());

        // fence the handler by bumping the compactions epoch elsewhere
        let stored_compactions = StoredCompactions::load(fixture.compactions_store.clone())
            .await
            .unwrap();
        FenceableCompactions::init(
            stored_compactions,
            fixture.handler.options.manifest_update_timeout,
            fixture.handler.system_clock.clone(),
        )
        .await
        .unwrap();

        // when:
        let result = fixture.handler.handle_ticker().await;

        // then:
        assert!(matches!(result, Err(SlateDBError::Fenced)));
    }

    #[tokio::test]
    async fn test_should_error_when_finishing_if_compactions_fenced() {
        // given: coordinator schedules a compaction and a worker completes it,
        // leaving a `Compacted` entry for the coordinator to commit.
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        fixture.write_l0().await;
        let compaction = fixture.build_l0_compaction().await;
        fixture.scheduler.inject_compaction(compaction.clone());
        fixture.handler.handle_ticker().await.unwrap();
        fixture.simulate_worker_completes().await;

        // fence compactions before the coordinator commits the Compacted entry
        let stored_compactions = StoredCompactions::load(fixture.compactions_store.clone())
            .await
            .unwrap();
        FenceableCompactions::init(
            stored_compactions,
            fixture.handler.options.manifest_update_timeout,
            fixture.handler.system_clock.clone(),
        )
        .await
        .unwrap();

        // when: the coordinator attempts to commit while fenced
        let result = fixture.handler.handle_ticker().await;

        // then:
        assert!(matches!(result, Err(SlateDBError::Fenced)));
    }

    #[tokio::test]
    async fn test_should_not_schedule_conflicting_compaction() {
        // given: first ticker schedules a compaction (Scheduled in local state)
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        fixture.write_l0().await;
        let compaction = fixture.build_l0_compaction().await;
        fixture.scheduler.inject_compaction(compaction.clone());
        fixture.handler.handle_ticker().await.unwrap();
        assert_eq!(fixture.get_scheduled_compactions().await.len(), 1);
        fixture.write_l0().await;
        fixture.scheduler.inject_compaction(compaction.clone());

        // when: second ticker with same spec — add_compaction rejects duplicate destination
        fixture.handler.handle_ticker().await.unwrap();

        // then: still only one active compaction (no duplicate added)
        assert_eq!(1, fixture.handler.state().active_compactions().count());
    }

    #[tokio::test]
    async fn test_should_leave_checkpoint_when_removing_ssts_after_compaction() {
        // given:
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        fixture.write_l0().await;
        let compaction = fixture.build_l0_compaction().await;
        fixture.scheduler.inject_compaction(compaction.clone());
        fixture.handler.handle_ticker().await.unwrap();
        fixture.simulate_worker_completes().await;
        // when: coordinator commits
        fixture.handler.handle_ticker().await.unwrap();

        // then:
        let current_dbstate = fixture.latest_db_state().await;
        let checkpoint = current_dbstate.checkpoints.last().unwrap();
        let old_manifest = fixture
            .manifest_store
            .read_manifest(checkpoint.manifest_id)
            .await
            .unwrap();
        let l0_ids: Vec<SourceId> = old_manifest
            .core
            .tree
            .l0
            .iter()
            .map(|view| SourceId::SstView(view.id))
            .collect();
        assert_eq!(&l0_ids, compaction.sources());
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    #[cfg(feature = "zstd")]
    async fn test_compactor_compressed_block_size() {
        use crate::compactor::stats::{BYTES_COMPACTED, SSTS_WRITTEN};
        use crate::config::{CompressionCodec, SstBlockSize};
        use slatedb_common::metrics::{lookup_metric, DefaultMetricsRecorder};

        // given:
        let os = Arc::new(InMemory::new());
        let system_clock = Arc::new(MockSystemClock::new());
        let scheduler_options = SizeTieredCompactionSchedulerOptions {
            min_compaction_sources: 1,
            max_compaction_sources: 999,
            include_size_threshold: 4.0,
        }
        .into();
        let mut options = db_options(Some(compactor_options()));
        options.l0_sst_size_bytes = 128;
        options.compression_codec = Some(CompressionCodec::Zstd);
        options
            .compactor_options
            .as_mut()
            .expect("compactor options missing")
            .scheduler_options = scheduler_options;

        let metrics_recorder = Arc::new(DefaultMetricsRecorder::new());
        let db = Db::builder(PATH, os.clone())
            .with_settings(options)
            .with_system_clock(system_clock.clone())
            .with_sst_block_size(SstBlockSize::Other(128))
            .with_metrics_recorder(metrics_recorder.clone())
            .build()
            .await
            .unwrap();

        for i in 0..4 {
            let k = vec![b'a' + i as u8; 16];
            let v = vec![b'b' + i as u8; 48];
            db.put(&k, &v).await.unwrap();
            let k = vec![b'j' + i as u8; 16];
            let v = vec![b'k' + i as u8; 48];
            db.put(&k, &v).await.unwrap();
        }

        db.flush().await.unwrap();

        // when:
        await_compaction(&db, os.clone(), Some(system_clock.clone()))
            .await
            .expect("db was not compacted");

        // then: the embedded worker recorded per-worker throughput.
        let bytes_compacted = lookup_metric(&metrics_recorder, BYTES_COMPACTED).unwrap();
        assert!(bytes_compacted > 0, "bytes_compacted: {}", bytes_compacted);
        let ssts_written = lookup_metric(&metrics_recorder, SSTS_WRITTEN).unwrap();
        assert!(ssts_written > 0, "ssts_written: {}", ssts_written);
    }

    #[tokio::test]
    async fn test_validate_compaction_empty_sources_rejected() {
        let fixture = CompactorEventHandlerTestFixture::new().await;
        let c = CompactionSpec::new(Vec::new(), 0);
        let err = fixture
            .handler
            .validate_compaction(&Compaction::new(Ulid::new(), c))
            .unwrap_err();
        assert!(matches!(err, SlateDBError::InvalidCompaction));
    }

    #[tokio::test]
    async fn test_validate_compaction_rejects_missing_l0_source() {
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        fixture.handler.handle_ticker().await.unwrap();
        let c = CompactionSpec::new(vec![SourceId::SstView(Ulid::new())], 0);
        let err = fixture
            .handler
            .validate_compaction(&Compaction::new(Ulid::new(), c))
            .unwrap_err();
        assert!(matches!(err, SlateDBError::InvalidCompaction));
    }

    #[tokio::test]
    async fn test_validate_compaction_rejects_missing_sr_source() {
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        fixture.handler.handle_ticker().await.unwrap();
        let c = CompactionSpec::new(vec![SourceId::SortedRun(42)], 42);
        let err = fixture
            .handler
            .validate_compaction(&Compaction::new(Ulid::new(), c))
            .unwrap_err();
        assert!(matches!(err, SlateDBError::InvalidCompaction));
    }

    #[tokio::test]
    async fn test_validate_compaction_l0_only_ok_when_no_sr() {
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        // ensure at least one L0 exists
        fixture.write_l0().await;
        fixture.handler.handle_ticker().await.unwrap();
        let c = fixture.build_l0_compaction().await;
        fixture
            .handler
            .validate_compaction(&Compaction::new(Ulid::new(), c))
            .unwrap();
    }

    #[tokio::test]
    async fn test_validate_compaction_l0_only_rejects_when_dest_below_highest_sr() {
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        // write L0 and compact to create SR id 0
        fixture.write_l0().await;
        let c1 = fixture.build_l0_compaction().await;
        fixture.scheduler.inject_compaction(c1.clone());
        fixture.handler.handle_ticker().await.unwrap();
        fixture.simulate_worker_completes().await;
        fixture.handler.handle_ticker().await.unwrap();

        // now highest_id should be 1; build L0-only compaction with dest 0 (below highest)
        fixture.write_l0().await;
        fixture.handler.handle_ticker().await.unwrap();
        let c2 = fixture.build_l0_compaction().await; // destination 0
        let err = fixture
            .handler
            .validate_compaction(&Compaction::new(Ulid::new(), c2))
            .unwrap_err();
        assert!(matches!(err, SlateDBError::InvalidCompaction));
    }

    /// The L0-only monotonicity check is global across all segment trees, not
    /// just the target tree. A spec whose destination is above the target
    /// tree's local max but below the global max (an SR in some other
    /// segment) must be rejected.
    #[tokio::test]
    async fn test_validate_compaction_l0_only_rejects_when_dest_below_global_highest_sr() {
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        let prefix = Bytes::from_static(b"seg/");
        let l0_view = Ulid::from_parts(1, 0);
        let make_view = |id: Ulid| {
            SsTableView::identity(SsTableHandle::new(
                SsTableId::Compacted(id),
                SST_FORMAT_VERSION_LATEST,
                SsTableInfo::default(),
            ))
        };
        let core = &mut fixture
            .handler
            .state_writer
            .state
            .manifest_mut_for_test()
            .value
            .core;
        // Root tree holds SR(7) — the global max. The segment-targeted spec
        // below proposes dst=3, which is above the segment's local max (0)
        // but below the global max.
        Arc::make_mut(&mut core.tree).compacted = vec![SortedRun {
            id: 7,
            sst_views: Vec::new(),
        }];
        core.segments = vec![Segment {
            prefix: prefix.clone(),
            tree: Arc::new(LsmTreeState {
                last_compacted_l0_sst_view_id: None,
                last_compacted_l0_sst_id: None,
                l0: VecDeque::from(vec![make_view(l0_view)]),
                compacted: Vec::new(),
            }),
        }];

        let spec = CompactionSpec::for_segment(prefix, vec![SourceId::SstView(l0_view)], 3);
        let err = fixture
            .handler
            .validate_compaction(&Compaction::new(Ulid::new(), spec))
            .unwrap_err();
        assert!(matches!(err, SlateDBError::InvalidCompaction));
    }

    /// Completion validation must not reuse the Submitted-only fresh-SR check.
    /// Cross-segment L0 compactions can commit out of order: a job submitted
    /// earlier with destination 3 is still valid even if another segment has
    /// since committed destination 7.
    #[tokio::test]
    async fn test_validate_completed_l0_allows_destination_below_global_highest_sr() {
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        let prefix = Bytes::from_static(b"seg/");
        let l0_view = Ulid::from_parts(1, 0);
        let make_view = |id: Ulid| {
            SsTableView::identity(SsTableHandle::new(
                SsTableId::Compacted(id),
                SST_FORMAT_VERSION_LATEST,
                SsTableInfo::default(),
            ))
        };
        let core = &mut fixture
            .handler
            .state_writer
            .state
            .manifest_mut_for_test()
            .value
            .core;
        Arc::make_mut(&mut core.tree).compacted = vec![SortedRun {
            id: 7,
            sst_views: Vec::new(),
        }];
        core.segments = vec![Segment {
            prefix: prefix.clone(),
            tree: Arc::new(LsmTreeState {
                last_compacted_l0_sst_view_id: None,
                last_compacted_l0_sst_id: None,
                l0: VecDeque::from(vec![make_view(l0_view)]),
                compacted: Vec::new(),
            }),
        }];

        let spec = CompactionSpec::for_segment(prefix, vec![SourceId::SstView(l0_view)], 3);

        let submitted_err = fixture
            .handler
            .validate_compaction(&Compaction::new(Ulid::new(), spec.clone()))
            .unwrap_err();
        assert!(matches!(submitted_err, SlateDBError::InvalidCompaction));
        let completed = Compaction::new(Ulid::new(), spec).with_status(CompactionStatus::Compacted);
        fixture
            .handler
            .validate_compaction(&completed)
            .expect("completed L0 compaction should not require a still-fresh destination");
    }

    #[tokio::test]
    async fn test_validate_completed_l0_rejects_destination_below_segment_highest_sr() {
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        let prefix = Bytes::from_static(b"seg/");
        let l0_view = Ulid::from_parts(1, 0);
        let make_view = |id: Ulid| {
            SsTableView::identity(SsTableHandle::new(
                SsTableId::Compacted(id),
                SST_FORMAT_VERSION_LATEST,
                SsTableInfo::default(),
            ))
        };
        fixture
            .handler
            .state_writer
            .state
            .manifest_mut_for_test()
            .value
            .core
            .segments = vec![Segment {
            prefix: prefix.clone(),
            tree: Arc::new(LsmTreeState {
                last_compacted_l0_sst_view_id: None,
                last_compacted_l0_sst_id: None,
                l0: VecDeque::from(vec![make_view(l0_view)]),
                compacted: vec![SortedRun {
                    id: 7,
                    sst_views: Vec::new(),
                }],
            }),
        }];

        let spec = CompactionSpec::for_segment(prefix, vec![SourceId::SstView(l0_view)], 3);
        let completed = Compaction::new(Ulid::new(), spec).with_status(CompactionStatus::Compacted);

        let err = fixture.handler.validate_compaction(&completed).unwrap_err();
        assert!(matches!(err, SlateDBError::InvalidCompaction));
    }

    #[tokio::test]
    async fn test_validate_compaction_mixed_l0_and_sr_deferred_to_scheduler() {
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        // create one SR so we can reference its id
        fixture.write_l0().await;
        let c1 = fixture.build_l0_compaction().await;
        fixture.scheduler.inject_compaction(c1.clone());
        fixture.handler.handle_ticker().await.unwrap();
        fixture.simulate_worker_completes().await;
        fixture.handler.handle_ticker().await.unwrap();

        // prepare a mixed compaction: one SR source and one L0 source
        fixture.write_l0().await;
        fixture.handler.handle_ticker().await.unwrap();
        let state = fixture.latest_db_state().await;
        let sr_id = state.tree.compacted.first().unwrap().id;
        let l0_view_id = state.tree.l0.front().unwrap().id;
        let mixed = CompactionSpec::new(
            vec![SourceId::SortedRun(sr_id), SourceId::SstView(l0_view_id)],
            sr_id,
        );
        // Compactor-level validation should not reject (scheduler default validate returns Ok(()))
        fixture
            .handler
            .validate_compaction(&Compaction::new(Ulid::new(), mixed))
            .unwrap();
    }

    #[tokio::test]
    async fn test_validate_compaction_rejects_parallel_l0() {
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        // write two L0s so we can build two disjoint L0 compactions
        fixture.write_l0().await;
        fixture.write_l0().await;
        fixture.handler.handle_ticker().await.unwrap();

        let state = fixture.latest_db_state().await;
        assert!(state.tree.l0.len() >= 2);

        // Build first L0 compaction from the oldest L0
        let first_l0 =
            CompactionSpec::new(vec![SourceId::SstView(state.tree.l0.back().unwrap().id)], 0);
        // Inject and schedule it so it becomes active
        fixture.scheduler.inject_compaction(first_l0.clone());
        fixture.handler.handle_ticker().await.unwrap();

        // Build second L0 compaction from the newest L0 (disjoint sources)
        let second_l0 = CompactionSpec::new(
            vec![SourceId::SstView(state.tree.l0.front().unwrap().id)],
            1,
        );
        let err = fixture
            .handler
            .validate_compaction(&Compaction::new(Ulid::new(), second_l0))
            .unwrap_err();
        assert!(matches!(err, SlateDBError::InvalidCompaction));
    }

    /// L0 compactions in disjoint segments may run concurrently — each segment
    /// owns its own watermark, so cross-segment parallelism is safe (RFC-0024).
    /// The parallel-L0 rejection in `validate_compaction` must scope itself to
    /// the spec's target segment.
    ///
    /// This test inspects `validate_compaction` directly by seeding the
    /// compactor's local state with two segments and a Running spec — going
    /// through the full schedule/start round-trip would require a writer
    /// configured with a segment extractor, which the fixture doesn't model.
    #[tokio::test]
    async fn test_validate_compaction_allows_parallel_l0_in_disjoint_segments() {
        use crate::manifest::{LsmTreeState, Segment};
        use bytes::Bytes;

        let mut fixture = CompactorEventHandlerTestFixture::new().await;

        let prefix_a = Bytes::from_static(b"seg-a/");
        let prefix_b = Bytes::from_static(b"seg-b/");
        let l0_a = Ulid::from_parts(1, 0);
        let l0_b = Ulid::from_parts(2, 0);
        let make_segment = |prefix: Bytes, l0_view_id: Ulid| Segment {
            prefix,
            tree: Arc::new(LsmTreeState {
                last_compacted_l0_sst_view_id: None,
                last_compacted_l0_sst_id: None,
                l0: VecDeque::from(vec![SsTableView::identity(SsTableHandle::new(
                    SsTableId::Compacted(l0_view_id),
                    SST_FORMAT_VERSION_LATEST,
                    SsTableInfo::default(),
                ))]),
                compacted: Vec::new(),
            }),
        };

        // Seed the compactor's local state with the two segments. We never
        // call handle_ticker again, so the writer/compactor merge protocol
        // (which would reject compactor-only segments with data) doesn't
        // run.
        fixture
            .handler
            .state_writer
            .state
            .manifest_mut_for_test()
            .value
            .core
            .segments = vec![
            make_segment(prefix_a.clone(), l0_a),
            make_segment(prefix_b.clone(), l0_b),
        ];

        // Seed a Running L0 compaction in segment A.
        let running_id = Ulid::from_parts(100, 0);
        let running_spec =
            CompactionSpec::for_segment(prefix_a.clone(), vec![SourceId::SstView(l0_a)], 200);
        fixture
            .handler
            .state_writer
            .state
            .insert_compaction_for_test(
                Compaction::new(running_id, running_spec).with_status(CompactionStatus::Running),
            );

        // L0 in a different segment must validate successfully — its
        // watermark is independent of segment A's.
        let spec_b =
            CompactionSpec::for_segment(prefix_b.clone(), vec![SourceId::SstView(l0_b)], 201);
        fixture
            .handler
            .validate_compaction(&Compaction::new(Ulid::new(), spec_b))
            .expect("L0 in disjoint segment must be allowed");

        // Sanity check: a second L0 spec in the SAME segment is still rejected.
        let spec_a_dup =
            CompactionSpec::for_segment(prefix_a.clone(), vec![SourceId::SstView(l0_a)], 202);
        let err = fixture
            .handler
            .validate_compaction(&Compaction::new(Ulid::new(), spec_a_dup))
            .unwrap_err();
        assert!(matches!(err, SlateDBError::InvalidCompaction));
    }

    /// `validate_compaction` rejects a spec whose target segment does not exist in
    /// the manifest. The empty-prefix segment (root tree) always exists, so only
    /// non-empty prefixes can fail this lookup.
    #[tokio::test]
    async fn test_validate_compaction_rejects_unknown_segment() {
        let fixture = CompactorEventHandlerTestFixture::new().await;
        let spec = CompactionSpec::for_segment(
            Bytes::from_static(b"missing/"),
            vec![SourceId::SortedRun(0)],
            0,
        );
        let err = fixture
            .handler
            .validate_compaction(&Compaction::new(Ulid::new(), spec))
            .unwrap_err();
        assert!(matches!(err, SlateDBError::InvalidCompaction));
    }

    /// RFC-0024: a compaction must operate within a single segment. A spec whose
    /// sources reference a sorted run that lives in a different tree is rejected
    /// because the source is absent from the target segment's tree.
    #[tokio::test]
    async fn test_validate_compaction_rejects_sources_from_other_segment() {
        use crate::manifest::{LsmTreeState, Segment};

        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        // SR(99) lives in the root tree; segment "seg/" is empty.
        let core = &mut fixture
            .handler
            .state_writer
            .state
            .manifest_mut_for_test()
            .value
            .core;
        Arc::make_mut(&mut core.tree).compacted = vec![SortedRun {
            id: 99,
            sst_views: Vec::new(),
        }];
        let prefix = Bytes::from_static(b"seg/");
        core.segments = vec![Segment {
            prefix: prefix.clone(),
            tree: Arc::new(LsmTreeState {
                last_compacted_l0_sst_view_id: None,
                last_compacted_l0_sst_id: None,
                l0: VecDeque::new(),
                compacted: Vec::new(),
            }),
        }];

        // Segment-targeted spec lists SR(99) as a source — but SR(99) lives in
        // the root tree, not in "seg/". Must be rejected.
        let spec = CompactionSpec::for_segment(prefix, vec![SourceId::SortedRun(99)], 0);
        let err = fixture
            .handler
            .validate_compaction(&Compaction::new(Ulid::new(), spec))
            .unwrap_err();
        assert!(matches!(err, SlateDBError::InvalidCompaction));
    }

    /// SR ids are globally unique across all trees (RFC-0024), so a segment-targeted
    /// spec must be rejected if its destination already exists as an SR anywhere in
    /// the manifest — including the root tree — and the SR is not listed among its
    /// sources.
    #[tokio::test]
    async fn test_validate_compaction_destination_overwrite_check_is_global() {
        use crate::manifest::{LsmTreeState, Segment};

        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        let core = &mut fixture
            .handler
            .state_writer
            .state
            .manifest_mut_for_test()
            .value
            .core;
        // Place SR(7) in the root tree. The segment-targeted spec below uses 7 as
        // its destination but does not list it among its sources.
        Arc::make_mut(&mut core.tree).compacted = vec![SortedRun {
            id: 7,
            sst_views: Vec::new(),
        }];
        // Seed SR(99) into the segment so the source-existence check passes and
        // destination-overwrite is the rejection reason.
        let prefix = Bytes::from_static(b"seg/");
        core.segments = vec![Segment {
            prefix: prefix.clone(),
            tree: Arc::new(LsmTreeState {
                last_compacted_l0_sst_view_id: None,
                last_compacted_l0_sst_id: None,
                l0: VecDeque::new(),
                compacted: vec![SortedRun {
                    id: 99,
                    sst_views: Vec::new(),
                }],
            }),
        }];

        let spec = CompactionSpec::for_segment(prefix, vec![SourceId::SortedRun(99)], 7);
        let err = fixture
            .handler
            .validate_compaction(&Compaction::new(Ulid::new(), spec))
            .unwrap_err();
        assert!(matches!(err, SlateDBError::InvalidCompaction));
    }

    /// Drain specs cannot target the empty-prefix segment (RFC-0024): the
    /// root-tree compatibility encoding is not retired by drain.
    #[tokio::test]
    async fn test_validate_compaction_rejects_drain_for_empty_prefix() {
        let fixture = CompactorEventHandlerTestFixture::new().await;
        let spec = CompactionSpec::drain_segment(Bytes::new(), vec![SourceId::SortedRun(0)]);
        let err = fixture
            .handler
            .validate_compaction(&Compaction::new(Ulid::new(), spec))
            .unwrap_err();
        assert!(matches!(err, SlateDBError::InvalidCompaction));
    }

    /// A drain spec must list every L0 at or below the newest drained L0 in
    /// the target tree — otherwise the watermark advances over an un-listed
    /// L0 and leaves it orphaned in `tree.l0` below the watermark.
    #[tokio::test]
    async fn test_validate_compaction_rejects_drain_with_non_contiguous_l0_sources() {
        use crate::manifest::{LsmTreeState, Segment};

        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        let prefix = Bytes::from_static(b"seg/");
        // tree.l0 is newest-first: [L0_3, L0_2, L0_1]. Drain spec names
        // L0_3 and L0_1 but skips L0_2 — the watermark would advance to L0_3
        // and L0_2 would be left below it.
        let l0_3 = Ulid::from_parts(3, 0);
        let l0_2 = Ulid::from_parts(2, 0);
        let l0_1 = Ulid::from_parts(1, 0);
        let make_view = |id: Ulid| {
            SsTableView::identity(SsTableHandle::new(
                SsTableId::Compacted(id),
                SST_FORMAT_VERSION_LATEST,
                SsTableInfo::default(),
            ))
        };
        fixture
            .handler
            .state_writer
            .state
            .manifest_mut_for_test()
            .value
            .core
            .segments = vec![Segment {
            prefix: prefix.clone(),
            tree: Arc::new(LsmTreeState {
                last_compacted_l0_sst_view_id: None,
                last_compacted_l0_sst_id: None,
                l0: VecDeque::from(vec![make_view(l0_3), make_view(l0_2), make_view(l0_1)]),
                compacted: Vec::new(),
            }),
        }];

        let spec = CompactionSpec::drain_segment(
            prefix,
            vec![SourceId::SstView(l0_3), SourceId::SstView(l0_1)],
        );
        let err = fixture
            .handler
            .validate_compaction(&Compaction::new(Ulid::new(), spec))
            .unwrap_err();
        assert!(matches!(err, SlateDBError::InvalidCompaction));
    }

    async fn put_and_flush_memtable(db: &Db, key: &[u8], value: &[u8]) {
        db.put_with_options(
            key,
            value,
            &PutOptions::default(),
            &WriteOptions {
                await_durable: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();
        db.flush_with_options(FlushOptions {
            flush_type: FlushType::MemTable,
        })
        .await
        .unwrap();
    }

    fn read_db_state_core(db: &Db) -> ManifestCore {
        let db_state = db.inner.state.read();
        db_state.state().core().clone()
    }

    async fn run_for<T, F>(duration: Duration, f: impl Fn() -> F) -> Option<T>
    where
        F: Future<Output = Option<T>>,
    {
        #[allow(clippy::disallowed_methods)]
        let now = SystemTime::now();
        while now.elapsed().unwrap() < duration {
            let maybe_result = f().await;
            if maybe_result.is_some() {
                return maybe_result;
            }
            tokio::time::sleep(Duration::from_millis(100)).await;
        }
        None
    }

    fn build_test_stores(
        os: Arc<dyn ObjectStore>,
    ) -> (Arc<ManifestStore>, Arc<CompactionsStore>, Arc<TableStore>) {
        let sst_format = SsTableFormat {
            block_size: 32,
            min_filter_keys: 10,
            ..SsTableFormat::default()
        };
        let manifest_store = Arc::new(ManifestStore::new(&Path::from(PATH), os.clone()));
        let compactions_store = Arc::new(CompactionsStore::new(&Path::from(PATH), os.clone()));
        let table_store = Arc::new(TableStore::new(
            ObjectStores::new(os.clone(), None),
            sst_format,
            Path::from(PATH),
            None,
            TableStoreKind::Compactor,
        ));
        (manifest_store, compactions_store, table_store)
    }

    /// Waits until all writes have made their way to L1 or below. No data is allowed in
    /// in-memory WALs, in-memory memtables, or L0 SSTs on object storage.
    ///
    /// If a clock is provided, it will be advanced the clock by 60 seconds on each iteration to
    /// trigger time-based flushes and compactions.
    async fn await_compaction(
        db: &Db,
        os: Arc<dyn ObjectStore>,
        clock: Option<Arc<dyn SystemClock>>,
    ) -> Option<ManifestCore> {
        await_compaction_matching(db, os, clock, |_| true).await
    }

    async fn await_compaction_matching(
        db: &Db,
        os: Arc<dyn ObjectStore>,
        clock: Option<Arc<dyn SystemClock>>,
        predicate: impl Fn(&ManifestCore) -> bool,
    ) -> Option<ManifestCore> {
        let manifest_store = Arc::new(ManifestStore::new(&Path::from(PATH), os.clone()));
        run_for(Duration::from_secs(10), || async {
            if let Some(clock) = &clock {
                clock.as_ref().advance(Duration::from_millis(60000)).await;
            }
            let (empty_wal, empty_memtable, core_db_state) = {
                let db_state = db.inner.state.read();
                let cow_db_state = db_state.state();
                (
                    db.inner.wal_buffer.is_empty(),
                    db_state.memtable().is_empty() && cow_db_state.imm_memtable.is_empty(),
                    db_state.state().core().clone(),
                )
            };

            let empty_l0 = core_db_state.tree.l0.is_empty();
            let compaction_ran = !core_db_state.tree.compacted.is_empty();

            if empty_wal
                && empty_memtable
                && empty_l0
                && compaction_ran
                && predicate(&core_db_state)
            {
                return Some(get_db_state(manifest_store.clone()).await);
            }
            None
        })
        .await
    }

    fn has_single_output_sst(db_state: &ManifestCore) -> bool {
        db_state.tree.compacted.len() == 1
            && db_state
                .tree
                .compacted
                .first()
                .is_some_and(|sr| sr.sst_views.len() == 1)
    }

    /// If a clock is provided, it will be advanced the clock by 60 seconds on each iteration to
    /// trigger time-based flushes and compactions.
    #[allow(unused)] // only used with feature(wal_disable)
    async fn await_compacted_compaction(
        manifest_store: Arc<ManifestStore>,
        old_compacted: Vec<SortedRun>,
        clock: Option<Arc<dyn SystemClock>>,
    ) -> Option<ManifestCore> {
        run_for(Duration::from_secs(10), || async {
            if let Some(clock) = &clock {
                clock.as_ref().advance(Duration::from_millis(60000)).await;
            }
            let db_state = get_db_state(manifest_store.clone()).await;
            if !db_state.tree.compacted.eq(&old_compacted) {
                return Some(db_state);
            }
            None
        })
        .await
    }

    async fn get_db_state(manifest_store: Arc<ManifestStore>) -> ManifestCore {
        let stored_manifest =
            StoredManifest::load(manifest_store.clone(), Arc::new(DefaultSystemClock::new()))
                .await
                .unwrap();
        stored_manifest.db_state().clone()
    }

    fn db_options(compactor_options: Option<CompactorOptions>) -> Settings {
        Settings {
            flush_interval: Some(Duration::from_millis(100)),
            #[cfg(feature = "wal_disable")]
            wal_enabled: true,
            manifest_poll_interval: Duration::from_millis(100),
            manifest_update_timeout: Duration::from_secs(300),
            l0_sst_size_bytes: 256,
            l0_max_ssts: 8,
            compactor_options,
            ..Settings::default()
        }
    }

    fn compactor_options() -> CompactorOptions {
        CompactorOptions {
            poll_interval: Duration::from_millis(100),
            max_concurrent_compactions: 1,
            scheduler_options: Default::default(),
            worker: Some(CompactionWorkerOptions {
                compactions_poll_interval: Duration::from_millis(100),
                ..CompactionWorkerOptions::default()
            }),
            ..CompactorOptions::default()
        }
    }

    fn compactor_builder_with_scheduler(
        os: Arc<dyn ObjectStore>,
        scheduler_supplier: Arc<dyn CompactionSchedulerSupplier>,
        system_clock: Arc<dyn SystemClock>,
    ) -> CompactorBuilder<&'static str> {
        CompactorBuilder::new(PATH, os)
            .with_system_clock(system_clock)
            .with_options(compactor_options())
            .with_scheduler_supplier(scheduler_supplier)
    }

    struct MockSchedulerInner {
        compaction: Vec<CompactionSpec>,
    }

    #[derive(Clone)]
    struct MockScheduler {
        inner: Arc<Mutex<MockSchedulerInner>>,
    }

    impl MockScheduler {
        fn new() -> Self {
            Self {
                inner: Arc::new(Mutex::new(MockSchedulerInner { compaction: vec![] })),
            }
        }

        fn inject_compaction(&self, compaction: CompactionSpec) {
            let mut inner = self.inner.lock();
            inner.compaction.push(compaction);
        }
    }

    impl CompactionScheduler for MockScheduler {
        fn propose(&self, _state: &CompactorStateView) -> Vec<CompactionSpec> {
            let mut inner = self.inner.lock();
            std::mem::take(&mut inner.compaction)
        }
    }

    // Builds a fake output SsTableHandle with first_entry set so that
    // SsTableView::identity can derive a non-empty effective range.
    fn fake_output_sst() -> SsTableHandle {
        SsTableHandle::new(
            SsTableId::Compacted(Ulid::new()),
            SST_FORMAT_VERSION_LATEST,
            SsTableInfo {
                first_entry: Some(Bytes::from_static(b"a")),
                ..SsTableInfo::default()
            },
        )
    }

    // Builds a Compacted compaction whose output is recorded on a single
    // unbounded-range subcompaction, mirroring what a worker persists on finish.
    fn compacted_with_output(
        id: Ulid,
        spec: CompactionSpec,
        output: Vec<SsTableHandle>,
    ) -> Compaction {
        Compaction::new(id, spec)
            .with_status(CompactionStatus::Compacted)
            .with_output_ssts(output)
    }

    #[tokio::test]
    async fn test_commit_compacted_entries_writes_manifest() {
        // given: a handler with one L0 in the manifest
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        fixture.write_l0().await;
        fixture.handler.state_writer.refresh().await.unwrap();

        let db_state = fixture.handler.state().db_state().clone();
        let sources: Vec<SourceId> = db_state
            .tree
            .l0
            .iter()
            .map(|view| SourceId::SstView(view.id))
            .collect();
        let destination = 0u32;
        let spec = CompactionSpec::new(sources, destination);
        let compaction_id = Ulid::from_parts(1, 0);
        let output_sst = fake_output_sst();
        let compaction = compacted_with_output(compaction_id, spec, vec![output_sst.clone()]);

        // inject the Compacted compaction into state (bypassing the executor)
        fixture
            .handler
            .state_mut()
            .insert_compaction_for_test(compaction);

        fixture.handler.state_writer.refresh().await.unwrap();

        // when: the coordinator observes and commits the Compacted entry
        fixture
            .handler
            .commit_compacted_entries()
            .await
            .expect("commit_compacted_entries failed");

        // then: compaction is Completed, L0 sources removed, output SR added
        let stored = fixture
            .compactions_store
            .read_latest_compactions()
            .await
            .unwrap()
            .compactions;
        assert_eq!(
            stored
                .get(&compaction_id)
                .expect("missing compaction")
                .status(),
            CompactionStatus::Completed,
        );
        let manifest = fixture.manifest_store.read_latest_manifest().await.unwrap();
        let core = manifest.core();
        assert!(core.tree.l0.is_empty(), "L0 sources should be removed");
        let sr = core.tree.compacted.iter().find(|sr| sr.id == destination);
        assert!(
            sr.is_some(),
            "output SR {destination} not found in manifest"
        );
        assert_eq!(sr.unwrap().sst_views.first().unwrap().sst.id, output_sst.id);

        // given: a Compacted SR0→SR1 compaction to validate the SR source path removed when not in L0
        let sr1_output_sst = fake_output_sst();
        let sr_compaction_id = Ulid::from_parts(2, 0);
        let sr_compaction = compacted_with_output(
            sr_compaction_id,
            CompactionSpec::new(vec![SourceId::SortedRun(0)], 1),
            vec![sr1_output_sst.clone()],
        );
        fixture
            .handler
            .state_mut()
            .insert_compaction_for_test(sr_compaction);

        // when:
        fixture
            .handler
            .commit_compacted_entries()
            .await
            .expect("SR commit failed");

        // then: SR 0 removed, SR 1 added with the correct SST
        let manifest2 = fixture.manifest_store.read_latest_manifest().await.unwrap();
        let core2 = manifest2.core();
        assert!(
            core2.tree.compacted.iter().all(|sr| sr.id != 0),
            "SR 0 should be removed after SR compaction"
        );
        let sr1 = core2.tree.compacted.iter().find(|sr| sr.id == 1);
        assert!(sr1.is_some(), "SR 1 should exist");
        assert_eq!(
            sr1.unwrap().sst_views.first().unwrap().sst.id,
            sr1_output_sst.id
        );
        let stored2 = fixture
            .compactions_store
            .read_latest_compactions()
            .await
            .unwrap()
            .compactions;
        assert_eq!(
            stored2
                .get(&sr_compaction_id)
                .expect("missing SR compaction")
                .status(),
            CompactionStatus::Completed,
        );
    }

    #[tokio::test]
    async fn test_commit_compacted_entries_marks_failed_when_sources_absent() {
        // given: a handler where the Compacted compaction references a source
        // that no longer exists in the manifest (simulates a post-crash recovery
        // where the manifest was already written before the coordinator crashed)
        let mut fixture = CompactorEventHandlerTestFixture::new().await;
        fixture.handler.state_writer.refresh().await.unwrap();

        // Use a fake view ID that is not present in any L0
        let ghost_view_id = Ulid::from_parts(u64::MAX, 0);
        let spec = CompactionSpec::new(vec![SourceId::SstView(ghost_view_id)], 0);
        let compaction_id = Ulid::new();
        let compaction = compacted_with_output(compaction_id, spec, vec![fake_output_sst()]);

        fixture
            .handler
            .state_mut()
            .insert_compaction_for_test(compaction);

        // when:
        fixture
            .handler
            .commit_compacted_entries()
            .await
            .expect("commit_compacted_entries failed");

        // then: the compaction is marked Failed (not retried)
        let stored = fixture
            .compactions_store
            .read_latest_compactions()
            .await
            .unwrap()
            .compactions;
        assert_eq!(
            stored
                .get(&compaction_id)
                .expect("missing compaction")
                .status(),
            CompactionStatus::Failed,
        );
    }

    /// A Running compaction whose heartbeat is older than the timeout must be
    /// reset to Scheduled (worker cleared) by `reclaim_stale_workers`.
    #[tokio::test]
    async fn test_reclaim_stale_running_compaction() {
        // given: clock starts at 0 ms
        let system_clock = Arc::new(MockSystemClock::new());
        let timeout = Duration::from_secs(30);
        let options = Arc::new(CompactorOptions {
            worker_heartbeat_timeout: timeout,
            ..compactor_options()
        });
        let mut fixture = CompactorEventHandlerTestFixture::new_with_clock(
            system_clock.clone() as Arc<dyn SystemClock>,
            options,
        )
        .await;

        fixture.handler.state_writer.refresh().await.unwrap();

        // Seed a Running compaction in-memory with last_heartbeat_ms = 0 (epoch).
        // Then persist it so that the compactions_store reflects the seeded state.
        let compaction_id = Ulid::new();
        let worker = WorkerSpec::new("worker-1".to_string(), 0);
        let compaction = Compaction::new(compaction_id, CompactionSpec::new(vec![], 0))
            .with_status(CompactionStatus::Running)
            .with_worker(Some(worker));
        fixture
            .handler
            .state_mut()
            .insert_compaction_for_test(compaction);
        fixture
            .handler
            .state_writer
            .write_compactions_safely()
            .await
            .expect("failed to persist seeded compaction");

        // Advance clock well past the timeout (2× the timeout = 60 s).
        system_clock.advance(Duration::from_secs(60)).await;

        // when: call reclaim_stale_workers directly to avoid the scheduling
        // side-effects of handle_ticker (which would try to claim the reclaimed
        // Scheduled compaction with an empty spec and mark it Failed).
        fixture
            .handler
            .reclaim_stale_workers()
            .await
            .expect("reclaim_stale_workers failed");

        // then: the compaction is now Scheduled with no worker.
        let stored = fixture
            .compactions_store
            .read_latest_compactions()
            .await
            .unwrap()
            .compactions;
        let c = stored.get(&compaction_id).expect("compaction missing");
        assert_eq!(
            c.status(),
            CompactionStatus::Scheduled,
            "should be reclaimed"
        );
        assert!(c.worker().is_none(), "worker should be cleared");

        // and: the reclamation is counted.
        let reclaimed = slatedb_common::metrics::lookup_metric(
            &fixture.test_recorder,
            crate::compactor::stats::JOBS_RECLAIMED,
        )
        .expect("metric not found");
        assert_eq!(reclaimed, 1, "one job should be counted as reclaimed");
    }

    /// `jobs_claimed` counts every job inherited by the coordinator on its first
    /// metrics update, then counts each newly claimed job exactly once across the
    /// `Running` and `Compacted` states.
    #[tokio::test]
    async fn test_jobs_claimed_metric_counts_inherited_then_new_claims() {
        let mut fixture = CompactorEventHandlerTestFixture::new().await;

        let claimed_count = || {
            slatedb_common::metrics::lookup_metric(
                &fixture.test_recorder,
                crate::compactor::stats::JOBS_CLAIMED,
            )
            .expect("metric not found")
        };

        // given: a job already claimed (Running) before the coordinator's first tick.
        let id1 = Ulid::new();
        fixture.handler.state_mut().insert_compaction_for_test(
            Compaction::new(id1, CompactionSpec::new(vec![], 0))
                .with_status(CompactionStatus::Running)
                .with_worker(Some(WorkerSpec::new("worker-1".to_string(), 0))),
        );

        // when: the first snapshot inherits the existing claim.
        fixture.handler.update_distributed_compaction_metrics();
        // then: the inherited job is counted once.
        assert_eq!(claimed_count(), 1);

        // when: a new job is claimed.
        let id2 = Ulid::new();
        fixture.handler.state_mut().insert_compaction_for_test(
            Compaction::new(id2, CompactionSpec::new(vec![], 0))
                .with_status(CompactionStatus::Running)
                .with_worker(Some(WorkerSpec::new("worker-1".to_string(), 0))),
        );
        fixture.handler.update_distributed_compaction_metrics();
        // then: it is counted once.
        assert_eq!(claimed_count(), 2);

        // when: that job finishes execution (Compacted, worker retained) and we
        // tick again.
        fixture.handler.state_mut().update_compaction(&id2, |c| {
            c.set_status(CompactionStatus::Compacted);
        });
        fixture.handler.update_distributed_compaction_metrics();
        // then: a still-claimed job is not recounted.
        assert_eq!(claimed_count(), 2);
    }

    /// A Running compaction whose heartbeat is within the timeout must NOT be
    /// reclaimed by `reclaim_stale_workers`.
    #[tokio::test]
    async fn test_does_not_reclaim_fresh_running_compaction() {
        // given: clock starts at 0 ms; heartbeat is also at 0 ms.
        let system_clock = Arc::new(MockSystemClock::new());
        let timeout = Duration::from_secs(30);
        let options = Arc::new(CompactorOptions {
            worker_heartbeat_timeout: timeout,
            ..compactor_options()
        });
        let mut fixture = CompactorEventHandlerTestFixture::new_with_clock(
            system_clock.clone() as Arc<dyn SystemClock>,
            options,
        )
        .await;

        fixture.handler.state_writer.refresh().await.unwrap();

        // Seed a Running compaction with heartbeat = "now" (0 ms).
        let compaction_id = Ulid::new();
        let now_ms = system_clock.now().timestamp_millis() as u64;
        let worker = WorkerSpec::new("worker-2".to_string(), now_ms);
        let compaction = Compaction::new(compaction_id, CompactionSpec::new(vec![], 0))
            .with_status(CompactionStatus::Running)
            .with_worker(Some(worker));
        fixture
            .handler
            .state_mut()
            .insert_compaction_for_test(compaction);
        fixture
            .handler
            .state_writer
            .write_compactions_safely()
            .await
            .expect("failed to persist seeded compaction");

        // Advance clock by only 5 s — well within the 30 s timeout.
        system_clock.advance(Duration::from_secs(5)).await;

        // when:
        fixture
            .handler
            .reclaim_stale_workers()
            .await
            .expect("reclaim_stale_workers failed");

        // then: the compaction is still Running (no reclaim write happened).
        // Since nothing was written, re-read from the store and verify.
        let stored = fixture
            .compactions_store
            .read_latest_compactions()
            .await
            .unwrap()
            .compactions;
        let c = stored.get(&compaction_id).expect("compaction missing");
        assert_eq!(
            c.status(),
            CompactionStatus::Running,
            "should NOT be reclaimed"
        );
        assert!(c.worker().is_some(), "worker should still be set");
    }

    /// A Running compaction without a worker violates the claim protocol. It
    /// panics in debug builds and is reclaimed in release builds so it cannot
    /// remain stuck in Running forever.
    #[tokio::test]
    #[cfg_attr(
        debug_assertions,
        should_panic(expected = "reclaiming Running compaction that has no worker")
    )]
    async fn test_reclaim_worker_less_running_compaction() {
        let system_clock = Arc::new(MockSystemClock::new());
        let options = Arc::new(CompactorOptions {
            worker_heartbeat_timeout: Duration::from_secs(30),
            ..compactor_options()
        });
        let mut fixture = CompactorEventHandlerTestFixture::new_with_clock(
            system_clock.clone() as Arc<dyn SystemClock>,
            options,
        )
        .await;

        fixture.handler.state_writer.refresh().await.unwrap();

        // Seed a Running compaction with no worker set.
        let compaction_id = Ulid::new();
        let compaction = Compaction::new(compaction_id, CompactionSpec::new(vec![], 0))
            .with_status(CompactionStatus::Running);
        fixture
            .handler
            .state_mut()
            .insert_compaction_for_test(compaction);
        fixture
            .handler
            .state_writer
            .write_compactions_safely()
            .await
            .expect("failed to persist seeded compaction");

        // when: reclaim should treat the worker-less entry as stale regardless
        // of how much time has (not) passed.
        fixture
            .handler
            .reclaim_stale_workers()
            .await
            .expect("reclaim_stale_workers failed");

        // then: the compaction is now Scheduled with no worker.
        let stored = fixture
            .compactions_store
            .read_latest_compactions()
            .await
            .unwrap()
            .compactions;
        let c = stored.get(&compaction_id).expect("compaction missing");
        assert_eq!(
            c.status(),
            CompactionStatus::Scheduled,
            "should be reclaimed"
        );
        assert!(c.worker().is_none(), "worker should remain cleared");
    }
}