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
//! Leveled compaction strategy.
//!
//! L0 → L1: merge all overlapping L0 files with overlapping L1 files.
//! Ln → Ln+1: pick a file from Ln, merge with overlapping files in Ln+1.
//!
//! After compaction, the old files are deleted and new files are installed.
use std::cmp::Ordering as CmpOrdering;
use std::collections::{HashMap, HashSet};
use std::fs::remove_file;
use std::path::Path;
use std::sync::{
Arc,
atomic::{AtomicU64, Ordering},
};
use std::thread::scope;
use crate::cache::block_cache::BlockCache;
use crate::cache::table_cache::TableCache;
use crate::error::{Error, Result, ResultExt};
use crate::iterator::merge::{IterSource, MergingIterator};
use crate::iterator::range_del::RangeTombstoneTracker;
use crate::manifest::version::{TableFile, Version};
use crate::manifest::version_edit::{FileMetaData, VersionEdit};
use crate::manifest::version_set::VersionSet;
use crate::options::{CompactionFilterDecision, DbOptions};
use crate::rate_limiter::RateLimiter;
use crate::sst::table_builder::{META_BLOCK_SPLIT_THRESHOLD, TableBuildOptions, TableBuilder};
use crate::sst::table_reader::{MAX_DECOMPRESSED_BLOCK_SIZE, TableIterator};
use crate::stats::DbStats;
use crate::types::{
InternalKey, InternalKeyRef, LazyValue, MAX_SEQUENCE_NUMBER, SequenceNumber, ValueType,
compare_internal_key, user_key,
};
/// A hint from the read path that a specific level may benefit from compaction.
/// Accumulated by the DB and drained by the compaction worker.
#[derive(Debug, Clone)]
pub struct CompactionHint {
/// The level that was read-hot.
pub level: usize,
}
/// Shared context for compaction operations, reducing argument count.
pub(crate) struct CompactionContext<'a> {
pub db_path: &'a Path,
pub options: &'a DbOptions,
pub rate_limiter: Option<&'a Arc<RateLimiter>>,
pub stats: Option<&'a Arc<DbStats>>,
pub active_snapshots: &'a [SequenceNumber],
}
/// Description of a compaction to perform.
pub struct CompactionTask {
/// Source level (files to compact from).
pub level: usize,
/// Files from the source level.
pub input_files_level: Vec<TableFile>,
/// Files from the target level (level + 1).
pub input_files_next: Vec<TableFile>,
}
/// Range tombstone user-key extents `[begin, end)` written to each output
/// SST, keyed by file number. Captured from `TableBuildResult` during the
/// unlocked I/O phase so the install-phase overlap precheck never has to
/// open or read an SST while the caller holds the DB lock.
type OutputTombstones = HashMap<u64, Vec<(Vec<u8>, Vec<u8>)>>;
/// Result of the I/O phase of compaction (no lock needed to produce this).
pub struct CompactionOutput {
/// The version edit with new files added and old files deleted.
pub edit: VersionEdit,
/// Exact input files and levels the output was built from.
pub input_files: Vec<(u32, FileMetaData)>,
/// File numbers of all input files (for cache eviction and deletion).
pub input_file_numbers: HashSet<u64>,
/// One past the highest file number consumed during I/O.
/// Used to ensure VersionSet::next_file_number doesn't fall behind
/// the actual numbers used by sub-compaction threads.
pub next_file_number_hint: u64,
/// Range tombstone extents per output file, for the install precheck.
output_tombstones: OutputTombstones,
}
/// Deferred cleanup actions that must be performed AFTER the manifest is
/// synced to disk. Executing these before sync risks data loss on crash.
pub struct PostCompactionCleanup {
/// Old SST file numbers to delete from disk.
pub files_to_delete: HashSet<u64>,
}
impl CompactionTask {}
pub struct LeveledCompaction;
/// Collect range tombstones from input files and return them as sorted
/// internal-key entries suitable for injection into the merge iterator.
/// This ensures tombstones from new-format SSTs (which store range
/// deletions in a separate block) participate in the merge.
fn collect_range_del_entries(files: &[TableFile]) -> Result<Vec<(Vec<u8>, Vec<u8>)>> {
let mut entries = Vec::new();
for tf in files {
if tf.meta.has_range_deletions {
let tombstones = tf.reader.get_range_tombstones().ctx()?;
for (begin, end, seq) in tombstones {
let ikey = InternalKey::new(&begin, seq, ValueType::RangeDeletion);
entries.push((ikey.as_bytes().to_vec(), end));
}
}
}
entries.sort_by(|a, b| compare_internal_key(&a.0, &b.0));
Ok(entries)
}
enum UserKeyRange {
/// Inclusive file metadata range.
File { smallest: Vec<u8>, largest: Vec<u8> },
/// Half-open range tombstone extent.
Tombstone { begin: Vec<u8>, end: Vec<u8> },
}
fn file_metadata_overlaps_bounds(
tf: &TableFile,
lower: Option<&[u8]>,
upper: Option<&[u8]>,
) -> bool {
let file_smallest = user_key(&tf.meta.smallest_key);
let file_largest = user_key(&tf.meta.largest_key);
let above_lower = lower.is_none_or(|lo| file_largest >= lo);
let below_upper = upper.is_none_or(|hi| file_smallest < hi);
above_lower && below_upper
}
fn tombstone_overlaps_bounds(
begin: &[u8],
end: &[u8],
lower: Option<&[u8]>,
upper: Option<&[u8]>,
) -> bool {
let above_lower = lower.is_none_or(|lo| end > lo);
let below_upper = upper.is_none_or(|hi| begin < hi);
above_lower && below_upper
}
fn file_overlaps_compact_bounds(
tf: &TableFile,
lower: Option<&[u8]>,
upper: Option<&[u8]>,
) -> bool {
if file_metadata_overlaps_bounds(tf, lower, upper) {
return true;
}
if !tf.meta.has_range_deletions {
return false;
}
match tf.reader.get_range_tombstones() {
Ok(tombstones) => tombstones
.iter()
.any(|(begin, end, _)| tombstone_overlaps_bounds(begin, end, lower, upper)),
Err(e) => {
tracing::warn!(
"failed to read range tombstones from SST {} while picking compact_range: {}",
tf.meta.number,
e
);
true
}
}
}
fn add_file_extents(tf: &TableFile, extents: &mut Vec<UserKeyRange>) -> bool {
if !tf.meta.smallest_key.is_empty() && !tf.meta.largest_key.is_empty() {
extents.push(UserKeyRange::File {
smallest: user_key(&tf.meta.smallest_key).to_vec(),
largest: user_key(&tf.meta.largest_key).to_vec(),
});
}
if tf.meta.has_range_deletions {
match tf.reader.get_range_tombstones() {
Ok(tombstones) => {
extents.extend(
tombstones
.into_iter()
.map(|(begin, end, _)| UserKeyRange::Tombstone { begin, end }),
);
}
Err(e) => {
tracing::warn!(
"failed to read range tombstones from SST {} while expanding compaction range: {}",
tf.meta.number,
e
);
return false;
}
}
}
true
}
fn file_metadata_overlaps_extent(tf: &TableFile, extent: &UserKeyRange) -> bool {
let file_smallest = user_key(&tf.meta.smallest_key);
let file_largest = user_key(&tf.meta.largest_key);
match extent {
UserKeyRange::File { smallest, largest } => {
file_largest >= smallest.as_slice() && file_smallest <= largest.as_slice()
}
UserKeyRange::Tombstone { begin, end } => {
file_largest >= begin.as_slice() && file_smallest < end.as_slice()
}
}
}
fn tombstone_overlaps_extent(begin: &[u8], end: &[u8], extent: &UserKeyRange) -> bool {
match extent {
UserKeyRange::File { smallest, largest } => {
end > smallest.as_slice() && begin <= largest.as_slice()
}
UserKeyRange::Tombstone {
begin: other_begin,
end: other_end,
} => end > other_begin.as_slice() && begin < other_end.as_slice(),
}
}
fn file_overlaps_extent(tf: &TableFile, extent: &UserKeyRange) -> bool {
if file_metadata_overlaps_extent(tf, extent) {
return true;
}
if !tf.meta.has_range_deletions {
return false;
}
match tf.reader.get_range_tombstones() {
Ok(tombstones) => tombstones
.iter()
.any(|(begin, end, _)| tombstone_overlaps_extent(begin, end, extent)),
Err(e) => {
tracing::warn!(
"failed to read range tombstones from SST {} while checking overlap: {}",
tf.meta.number,
e
);
true
}
}
}
fn overlapping_files_for_inputs(files: &[TableFile], inputs: &[TableFile]) -> Vec<TableFile> {
let mut extents = Vec::new();
for tf in inputs {
if !add_file_extents(tf, &mut extents) {
return files.to_vec();
}
}
// Seed with the aggregate [min smallest, max largest] range across ALL
// inputs, not just per-file extents. The merged compaction output may span
// key gaps between discontiguous inputs (output files are cut by size, not
// at input-file boundaries), so a target-level file sitting inside such a
// gap MUST be included as an input — otherwise the installed output would
// overlap it, breaking the L1+ disjointness invariant and making keys
// unreachable via binary search. This mirrors `total_key_range` used by
// the background pickers.
{
let mut agg_smallest: Option<&[u8]> = None;
let mut agg_largest: Option<&[u8]> = None;
for tf in inputs {
if tf.meta.smallest_key.is_empty() || tf.meta.largest_key.is_empty() {
continue;
}
let s = user_key(&tf.meta.smallest_key);
let l = user_key(&tf.meta.largest_key);
if agg_smallest.is_none_or(|cur| s < cur) {
agg_smallest = Some(s);
}
if agg_largest.is_none_or(|cur| l > cur) {
agg_largest = Some(l);
}
}
if let (Some(s), Some(l)) = (agg_smallest, agg_largest) {
extents.push(UserKeyRange::File {
smallest: s.to_vec(),
largest: l.to_vec(),
});
}
}
let mut selected = Vec::new();
let mut selected_numbers = HashSet::new();
loop {
let mut changed = false;
for tf in files {
if selected_numbers.contains(&tf.meta.number) {
continue;
}
if extents
.iter()
.any(|extent| file_overlaps_extent(tf, extent))
{
selected_numbers.insert(tf.meta.number);
selected.push(tf.clone());
if !add_file_extents(tf, &mut extents) {
return files.to_vec();
}
changed = true;
}
}
if !changed {
break;
}
}
selected
}
fn estimated_uncompressed_file_size(tf: &TableFile) -> u64 {
let data_blocks = tf
.reader
.cached_index_entries()
.map(|entries| entries.len() as u64)
.unwrap_or_else(|e| {
tracing::warn!(
"failed to parse index entries from SST {} while estimating compaction outputs: {}",
tf.meta.number,
e
);
1
});
let data_bound = data_blocks.saturating_mul(MAX_DECOMPRESSED_BLOCK_SIZE as u64);
let range_del_bound = if tf.meta.has_range_deletions {
MAX_DECOMPRESSED_BLOCK_SIZE as u64
} else {
0
};
data_bound
.saturating_add(range_del_bound)
.max(tf.meta.file_size)
}
/// A sub-task covering a key range within a compaction.
struct SubCompactionTask {
/// Inclusive lower bound (user key). None = start from beginning.
lower_bound: Option<Vec<u8>>,
/// Exclusive upper bound (user key). None = go to end.
upper_bound: Option<Vec<u8>>,
/// Files from the source level that overlap this range.
input_files_level: Vec<TableFile>,
/// Files from the target level within this range.
input_files_next: Vec<TableFile>,
}
/// Parameters shared across all sub-compactions within a single compaction I/O.
struct SubCompactionParams<'a> {
target_level: usize,
is_bottommost: bool,
build_opts: &'a TableBuildOptions,
oldest_snapshot_seq: SequenceNumber,
file_number_counter: &'a AtomicU64,
file_number_limit: u64,
all_range_del_entries: &'a [(Vec<u8>, Vec<u8>)],
all_raw_tombstones: &'a [(Vec<u8>, Vec<u8>, SequenceNumber)],
}
/// Output of a single sub-compaction (new files only; deletions handled by orchestrator).
struct SubCompactionOutput {
new_files: Vec<(u32, FileMetaData)>,
/// Range tombstone extents per output file (see `OutputTombstones`).
output_tombstones: OutputTombstones,
}
/// Compute split points from target-level file boundaries.
/// Returns user keys that divide the key space into sub-ranges.
fn compute_split_points(task: &CompactionTask, max_subs: usize) -> Vec<Vec<u8>> {
if max_subs <= 1 {
return Vec::new();
}
let next_files = &task.input_files_next;
if next_files.len() <= 1 {
return Vec::new(); // Not enough target files to split on
}
// Extract user key boundaries from target-level files (all except the last,
// since the last file's largest_key doesn't split anything).
let boundaries: Vec<Vec<u8>> = next_files[..next_files.len() - 1]
.iter()
.map(|tf| user_key(&tf.meta.largest_key).to_vec())
.collect();
if boundaries.is_empty() {
return Vec::new();
}
let desired_splits = (max_subs - 1).min(boundaries.len());
if desired_splits >= boundaries.len() {
return boundaries;
}
// Select evenly-spaced boundaries
let mut splits = Vec::with_capacity(desired_splits);
for i in 1..=desired_splits {
let idx = i * boundaries.len() / (desired_splits + 1);
splits.push(boundaries[idx.min(boundaries.len() - 1)].clone());
}
splits
}
/// Build sub-tasks from a compaction task and split points.
fn build_sub_tasks(task: &CompactionTask, split_points: &[Vec<u8>]) -> Vec<SubCompactionTask> {
let n = split_points.len() + 1;
let mut sub_tasks = Vec::with_capacity(n);
let next_files = &task.input_files_next;
for i in 0..n {
let lower = if i == 0 {
None
} else {
Some(split_points[i - 1].clone())
};
let upper = if i == n - 1 {
None
} else {
Some(split_points[i].clone())
};
// Filter target-level files to those overlapping [lower, upper)
let sub_next: Vec<TableFile> = next_files
.iter()
.filter(|tf| {
let file_smallest = user_key(&tf.meta.smallest_key);
let file_largest = user_key(&tf.meta.largest_key);
let above_lower = match &lower {
Some(lo) => file_largest >= lo.as_slice(),
None => true,
};
let below_upper = match &upper {
Some(hi) => file_smallest < hi.as_slice(),
None => true,
};
above_lower && below_upper
})
.cloned()
.collect();
// For L0: all source files (they overlap arbitrarily).
// For Ln: only files overlapping this sub-range.
let sub_level: Vec<TableFile> = if task.level == 0 {
task.input_files_level.clone()
} else {
task.input_files_level
.iter()
.filter(|tf| {
let file_smallest = user_key(&tf.meta.smallest_key);
let file_largest = user_key(&tf.meta.largest_key);
let above_lower = match &lower {
Some(lo) => file_largest >= lo.as_slice(),
None => true,
};
let below_upper = match &upper {
Some(hi) => file_smallest < hi.as_slice(),
None => true,
};
above_lower && below_upper
})
.cloned()
.collect()
};
sub_tasks.push(SubCompactionTask {
lower_bound: lower,
upper_bound: upper,
input_files_level: sub_level,
input_files_next: sub_next,
});
}
sub_tasks
}
/// Collect raw tombstone triples from input files.
type RawTombstone = (Vec<u8>, Vec<u8>, SequenceNumber);
fn task_has_range_deletions(task: &CompactionTask) -> bool {
task.input_files_level
.iter()
.chain(task.input_files_next.iter())
.any(|tf| tf.meta.has_range_deletions)
}
fn cleanup_output_files(
db_path: &Path,
new_files: &[(u32, FileMetaData)],
active_file_number: Option<u64>,
) {
for (_, meta) in new_files {
let orphan = db_path.join(format!("{:06}.sst", meta.number));
let _ = remove_file(&orphan);
}
if let Some(number) = active_file_number {
let orphan = db_path.join(format!("{:06}.sst", number));
let _ = remove_file(&orphan);
}
}
fn evict_table_cache_files(table_cache: Option<&Arc<TableCache>>, files: &[(u32, FileMetaData)]) {
if let Some(cache) = table_cache {
for (_, meta) in files {
cache.evict(meta.number);
}
}
}
fn allocate_output_file_number(params: &SubCompactionParams<'_>) -> Result<u64> {
let mut current = params.file_number_counter.load(Ordering::Relaxed);
loop {
if current >= params.file_number_limit {
return Err(Error::background(format!(
"reserved compaction output file numbers exhausted: next={}, limit={}",
current, params.file_number_limit
)));
}
let next = current
.checked_add(1)
.ok_or_else(|| Error::background("compaction output file number overflow"))?;
match params.file_number_counter.compare_exchange_weak(
current,
next,
Ordering::Relaxed,
Ordering::Relaxed,
) {
Ok(_) => return Ok(current),
Err(actual) => current = actual,
}
}
}
fn collect_raw_tombstones(files: &[TableFile]) -> Result<Vec<RawTombstone>> {
let mut tombstones = Vec::new();
for tf in files {
if tf.meta.has_range_deletions {
let ts = tf.reader.get_range_tombstones().ctx()?;
tombstones.extend(ts);
}
}
Ok(tombstones)
}
/// Execute a single sub-compaction covering [lower_bound, upper_bound).
/// File numbers are allocated from a shared atomic counter to avoid collisions.
/// `all_range_del_entries` contains range tombstone merge entries from ALL input files.
/// `all_raw_tombstones` contains raw (begin, end, seq) triples to pre-populate
/// the RangeTombstoneTracker, ensuring tombstones whose start key is before
/// this sub-task's lower_bound are still applied.
fn execute_sub_compaction_io(
ctx: &CompactionContext<'_>,
sub: &SubCompactionTask,
params: &SubCompactionParams<'_>,
) -> Result<SubCompactionOutput> {
// Build streaming merge sources
let mut sources: Vec<IterSource> = Vec::new();
for tf in &sub.input_files_level {
// Compaction scans every input block exactly once; filling the
// block cache would evict hot point-read blocks for no benefit.
let iter = TableIterator::new(tf.reader.clone()).with_fill_cache(false);
sources.push(IterSource::from_boxed(Box::new(iter)));
}
for tf in &sub.input_files_next {
// Compaction scans every input block exactly once; filling the
// block cache would evict hot point-read blocks for no benefit.
let iter = TableIterator::new(tf.reader.clone()).with_fill_cache(false);
sources.push(IterSource::from_boxed(Box::new(iter)));
}
// Inject ALL range tombstones into merge stream
if !params.all_range_del_entries.is_empty() {
sources.push(IterSource::new(params.all_range_del_entries.to_vec()));
}
let mut merger = MergingIterator::new(sources, compare_internal_key);
// If lower_bound is set, seek past it
if let Some(ref lo) = sub.lower_bound {
let seek_key = InternalKey::new(lo, MAX_SEQUENCE_NUMBER, ValueType::Value)
.as_bytes()
.to_vec();
merger.seek(&seek_key);
}
let mut new_files: Vec<(u32, FileMetaData)> = Vec::new();
let mut output_tombstones = OutputTombstones::new();
let mut builder: Option<TableBuilder> = None;
let mut current_file_number = 0u64;
let mut current_size = 0usize;
let mut current_file_user_key: Vec<u8> = Vec::new();
let mut pending_cut = false;
let mut last_point_key: Option<Vec<u8>> = None;
let mut last_range_del_key: Option<Vec<u8>> = None;
let mut last_written_seq: SequenceNumber = 0;
let mut snapshot_idx: usize = ctx.active_snapshots.len();
let mut range_tombstones = RangeTombstoneTracker::new();
// Pre-populate tracker with ALL tombstones so that tombstones whose
// start key is before this sub-task's lower_bound still take effect.
for (begin, end, seq) in params.all_raw_tombstones {
range_tombstones.add(begin.clone(), end.clone(), *seq);
}
range_tombstones.reset();
while let Some((ikey, value)) = merger.next_entry() {
if ikey.len() < 8 {
continue;
}
let ikr = InternalKeyRef::new(&ikey);
let user_key = ikr.user_key();
// Check upper bound: stop if user_key >= upper_bound
if let Some(ref hi) = sub.upper_bound
&& user_key >= hi.as_slice()
{
break;
}
// Only cut output files at a user-key boundary: all versions of one user
// key must stay in the same file, otherwise L1+ files would have
// overlapping key ranges and a point read could pick the wrong file and
// miss a visible version. A size-triggered cut is deferred until the user
// key changes here.
if pending_cut && builder.is_some() && user_key != current_file_user_key.as_slice() {
let result = match builder.take().unwrap().finish().ctx() {
Ok(result) => result,
Err(e) => {
cleanup_output_files(ctx.db_path, &new_files, Some(current_file_number));
return Err(e);
}
};
if let Some(s) = ctx.stats {
s.record_compaction_bytes(result.file_size);
}
if result.has_range_deletions {
output_tombstones.insert(current_file_number, result.range_tombstones);
}
new_files.push((
params.target_level as u32,
FileMetaData {
number: current_file_number,
file_size: result.file_size,
smallest_key: result.smallest_key.unwrap_or_default(),
largest_key: result.largest_key.unwrap_or_default(),
has_range_deletions: result.has_range_deletions,
},
));
current_size = 0;
pending_cut = false;
}
if ikr.value_type() == ValueType::RangeDeletion {
// Not re-added to `range_tombstones` here: `all_raw_tombstones`
// already pre-populated the tracker with every tombstone in the
// input set (including this one) before the loop started, so
// re-adding it on every native occurrence would just duplicate
// entries and force a full re-sort per tombstone for no benefit.
if let Some(ref last) = last_range_del_key
&& last.as_slice() == ikey.as_slice()
{
continue;
}
last_range_del_key = Some(ikey.clone());
if params.is_bottommost && ikr.sequence() < params.oldest_snapshot_seq {
continue;
}
} else if let Some(ref last) = last_point_key
&& last.as_slice() == user_key
{
while snapshot_idx > 0 && ctx.active_snapshots[snapshot_idx - 1] >= last_written_seq {
snapshot_idx -= 1;
}
if snapshot_idx > 0 && ctx.active_snapshots[snapshot_idx - 1] >= ikr.sequence() {
last_written_seq = ikr.sequence();
// A retained older version that is shadowed by a range tombstone
// below the oldest snapshot must still be dropped. Otherwise, if
// that tombstone is itself dropped at the bottommost level, the
// value would resurrect for the snapshot that retained it. (The
// newest-version branch below already applies this check; retained
// versions need it too.)
if ikr.value_type() == ValueType::Value
&& !range_tombstones.is_empty()
&& range_tombstones.is_deleted(
user_key,
ikr.sequence(),
params.oldest_snapshot_seq,
)
{
continue;
}
} else {
continue;
}
} else {
last_point_key = Some(user_key.to_vec());
last_written_seq = ikr.sequence();
snapshot_idx = ctx.active_snapshots.len();
// Only a genuine Deletion may be dropped here, and only at the
// bottommost level below the oldest snapshot — use the strict
// decode so a corrupted trailer byte fails the compaction loudly
// instead of being silently misread as Deletion and permanently
// destroyed (the original input files are untouched on error).
if params.is_bottommost
&& ikr.sequence() < params.oldest_snapshot_seq
&& ikr.value_type_checked().ctx()? == ValueType::Deletion
{
continue;
}
if ikr.value_type() == ValueType::Value && !range_tombstones.is_empty() {
let entry_seq = ikr.sequence();
if range_tombstones.is_deleted(user_key, entry_seq, params.oldest_snapshot_seq) {
continue;
}
}
}
// Apply compaction filter
let mut final_value = value;
if params.is_bottommost
&& ctx.active_snapshots.is_empty()
&& let Some(ref filter) = ctx.options.compaction_filter
&& ikr.value_type() == ValueType::Value
{
match filter.filter(params.target_level, user_key, final_value.as_slice()) {
CompactionFilterDecision::Keep => {}
CompactionFilterDecision::Remove => continue,
CompactionFilterDecision::ChangeValue(new_val) => {
final_value = LazyValue::Inline(new_val);
}
}
}
// Create new output file if needed
if builder.is_none() {
current_file_number = match allocate_output_file_number(params).ctx() {
Ok(number) => number,
Err(e) => {
cleanup_output_files(ctx.db_path, &new_files, None);
return Err(e);
}
};
let sst_path = ctx.db_path.join(format!("{:06}.sst", current_file_number));
let mut opts = params.build_opts.clone();
opts.block_property_collectors = ctx
.options
.block_property_collectors
.iter()
.map(|f| f())
.collect();
builder = match TableBuilder::new(&sst_path, opts).ctx() {
Ok(builder) => Some(builder),
Err(e) => {
cleanup_output_files(ctx.db_path, &new_files, Some(current_file_number));
return Err(e);
}
};
current_size = 0;
}
let final_ikey;
let ikey_ref = if params.is_bottommost
&& ikr.sequence() > 0
&& ikr.sequence() < params.oldest_snapshot_seq
&& ikr.value_type() == ValueType::Value
{
final_ikey = InternalKey::new(user_key, 0, ikr.value_type())
.as_bytes()
.to_vec();
&final_ikey
} else {
&ikey
};
let entry_bytes = ikey_ref.len() + final_value.len();
if let Err(e) = builder
.as_mut()
.unwrap()
.add(ikey_ref, final_value.as_slice())
.ctx()
{
cleanup_output_files(ctx.db_path, &new_files, Some(current_file_number));
return Err(e);
}
current_size += entry_bytes;
if current_file_user_key != user_key {
current_file_user_key.clear();
current_file_user_key.extend_from_slice(user_key);
}
if let Some(rl) = ctx.rate_limiter {
rl.request(entry_bytes);
}
if current_size >= ctx.options.target_file_size_base as usize
|| builder.as_ref().unwrap().projected_meta_size() >= META_BLOCK_SPLIT_THRESHOLD
{
// Defer the actual file cut to the next user-key boundary (handled at
// the top of the loop) so a key's versions are never split across files.
// The meta-size condition keeps the per-file index / range-del blocks
// well below the reader's hard cap for key-heavy data.
pending_cut = true;
}
}
// Flush remaining builder
let active_file_number = builder.as_ref().map(|_| current_file_number);
if let Some(b) = builder {
let result = match b.finish().ctx() {
Ok(result) => result,
Err(e) => {
cleanup_output_files(ctx.db_path, &new_files, Some(current_file_number));
return Err(e);
}
};
if let Some(s) = ctx.stats {
s.record_compaction_bytes(result.file_size);
}
if result.has_range_deletions {
output_tombstones.insert(current_file_number, result.range_tombstones);
}
new_files.push((
params.target_level as u32,
FileMetaData {
number: current_file_number,
file_size: result.file_size,
smallest_key: result.smallest_key.unwrap_or_default(),
largest_key: result.largest_key.unwrap_or_default(),
has_range_deletions: result.has_range_deletions,
},
));
}
if let Some(e) = merger.error() {
// Clean up orphan SST files written during the failed merge
cleanup_output_files(ctx.db_path, &new_files, active_file_number);
return Err(Error::background(format!(
"sub-compaction merge error: {}",
e
)));
}
Ok(SubCompactionOutput {
new_files,
output_tombstones,
})
}
impl LeveledCompaction {
pub(crate) fn max_output_files(task: &CompactionTask, options: &DbOptions) -> u64 {
// Output files are also cut when their projected index / range-del
// meta block reaches META_BLOCK_SPLIT_THRESHOLD (key-heavy data), so
// size the reservation for the smaller of the two cut conditions.
let target_size = options
.target_file_size_base
.max(1)
.min((META_BLOCK_SPLIT_THRESHOLD / 2) as u64);
let estimated_input_size = task
.input_files_level
.iter()
.chain(task.input_files_next.iter())
.fold(0u64, |acc, tf| {
acc.saturating_add(estimated_uncompressed_file_size(tf))
});
let size_outputs = estimated_input_size.saturating_add(target_size - 1) / target_size;
let input_count = task
.input_files_level
.len()
.saturating_add(task.input_files_next.len()) as u64;
size_outputs
.saturating_add(input_count)
.saturating_add(options.max_subcompactions.max(1) as u64)
.saturating_add(16)
.max(1)
}
/// Check if compaction is needed and return a task if so.
///
/// `in_flight` excludes file numbers already claimed by another
/// concurrent compaction's pick (see `DB::compacting_files`) — with
/// `max_background_compactions > 1`, multiple threads call this
/// independently, and without exclusion two threads could pick the same
/// input files and redundantly re-process them before
/// `install_compaction`'s stale-input check discards the loser. Pass an
/// empty set for read-only "is compaction pending"-style checks that
/// don't actually claim the result.
pub fn pick_compaction(
version: &Version,
options: &DbOptions,
in_flight: &HashSet<u64>,
) -> Option<CompactionTask> {
// Priority 1: L0 → L1 when L0 has too many files
if version.l0_file_count() >= options.l0_compaction_trigger
&& let Some(task) = Self::pick_l0_compaction(version, in_flight)
{
return Some(task);
}
// Priority 2: Check each level for size overflow
for level in 1..version.num_levels - 1 {
let level_size: u64 = version
.level_files(level)
.iter()
.map(|f| f.meta.file_size)
.sum();
let max_size = Self::max_bytes_for_level(options, level);
if level_size > max_size
&& let Some(task) = Self::pick_level_compaction(version, level, in_flight)
{
return Some(task);
}
}
None
}
/// Pick L0 → L1 compaction. Returns `None` (deferring to another
/// priority) if any current L0 file is already claimed by another
/// in-flight compaction — L0 compaction always includes the *entire*
/// current L0 file set, so a partial pick isn't meaningful here; the
/// caller's next loop iteration retries once the other compaction
/// installs or is discarded and clears its claim.
fn pick_l0_compaction(version: &Version, in_flight: &HashSet<u64>) -> Option<CompactionTask> {
let l0_files = version.level_files(0);
if l0_files.is_empty()
|| l0_files
.iter()
.any(|tf| in_flight.contains(&tf.meta.number))
{
return None;
}
// All L0 files participate (they may overlap with each other)
let input_l0: Vec<TableFile> = l0_files.to_vec();
// Find the total key range of L0 files
let (smallest, largest) = Self::total_key_range(&input_l0);
// Find overlapping L1 files
let input_l1 = if input_l0.iter().any(|tf| tf.meta.has_range_deletions) {
version.level_files(1).to_vec()
} else {
Self::overlapping_files(version.level_files(1), &smallest, &largest)
};
if input_l1
.iter()
.any(|tf| in_flight.contains(&tf.meta.number))
{
return None;
}
Some(CompactionTask {
level: 0,
input_files_level: input_l0,
input_files_next: input_l1,
})
}
/// Pick Ln → Ln+1 compaction. Returns `None` (deferring to another
/// priority/level) if the selected input file or any overlapping
/// next-level file is already claimed by another in-flight compaction.
fn pick_level_compaction(
version: &Version,
level: usize,
in_flight: &HashSet<u64>,
) -> Option<CompactionTask> {
let files = version.level_files(level);
if files.is_empty() {
return None;
}
// Pick the largest file in this level that isn't already claimed.
let target = files
.iter()
.filter(|f| !in_flight.contains(&f.meta.number))
.max_by_key(|f| f.meta.file_size)?;
let input_level = vec![target.clone()];
let (smallest, largest) = Self::total_key_range(&input_level);
let next_level = level + 1;
let input_next = if next_level < version.num_levels {
if input_level.iter().any(|tf| tf.meta.has_range_deletions) {
version.level_files(next_level).to_vec()
} else {
Self::overlapping_files(version.level_files(next_level), &smallest, &largest)
}
} else {
Vec::new()
};
if input_next
.iter()
.any(|tf| in_flight.contains(&tf.meta.number))
{
return None;
}
Some(CompactionTask {
level,
input_files_level: input_level,
input_files_next: input_next,
})
}
/// Pick compaction for a specific key range. Collects files overlapping
/// [begin, end) at each level and compacts them.
pub fn pick_compaction_for_range(
version: &Version,
begin: Option<&[u8]>,
end: Option<&[u8]>,
) -> Option<CompactionTask> {
// Check L0 first: collect L0 files overlapping the range
let l0_files = version.level_files(0);
let mut input_l0: Vec<TableFile> = Vec::new();
for tf in l0_files {
if file_overlaps_compact_bounds(tf, begin, end) {
input_l0.push(tf.clone());
}
}
if !input_l0.is_empty() {
let input_l1 = overlapping_files_for_inputs(version.level_files(1), &input_l0);
return Some(CompactionTask {
level: 0,
input_files_level: input_l0,
input_files_next: input_l1,
});
}
// Check L1+ levels
for level in 1..version.num_levels - 1 {
let files = version.level_files(level);
let mut input_level: Vec<TableFile> = Vec::new();
for tf in files {
if file_overlaps_compact_bounds(tf, begin, end) {
input_level.push(tf.clone());
}
}
if !input_level.is_empty() {
let next_level = level + 1;
let input_next = if next_level < version.num_levels {
overlapping_files_for_inputs(version.level_files(next_level), &input_level)
} else {
Vec::new()
};
return Some(CompactionTask {
level,
input_files_level: input_level,
input_files_next: input_next,
});
}
}
None
}
/// Pure I/O phase of compaction — does NOT require any lock.
///
/// Merges input files, writes output SSTs using pre-allocated file numbers,
/// and returns a `CompactionOutput` ready for `install_compaction`.
///
/// When `options.max_subcompactions > 1` and the target level has enough
/// files to split on, the work is divided into parallel sub-compactions
/// using `std::thread::scope`.
pub(crate) fn execute_compaction_io(
ctx: &CompactionContext<'_>,
task: &CompactionTask,
file_number_start: u64,
file_number_limit: u64,
is_bottommost: bool,
) -> Result<CompactionOutput> {
let target_level = task.level + 1;
// Trivial move optimization: if there's exactly one input file and no
// overlap with the next level, just move the metadata without
// rewriting. Skip it when the compaction filter could remove or
// change entries — it needs to see every key-value pair. A filter
// that reports itself a no-op (e.g. lazy-delete with no user filter
// and no registered dead keys) still permits the move. Building this
// as a normal `CompactionOutput` (rather than applying it directly)
// means it goes through `install_compaction`'s stale-input check
// like any other compaction — required now that every caller of
// this function (including concurrent background workers) can reach
// this path, not just the single-threaded `do_compaction`.
if task.input_files_level.len() == 1
&& task.input_files_next.is_empty()
&& ctx
.options
.compaction_filter
.as_ref()
.is_none_or(|f| f.is_noop())
{
let tf = &task.input_files_level[0];
let mut edit = VersionEdit::new();
edit.delete_file(task.level as u32, tf.meta.number);
edit.add_file(target_level as u32, tf.meta.clone());
return Ok(CompactionOutput {
edit,
input_files: vec![(task.level as u32, tf.meta.clone())],
// Empty, NOT `{tf.meta.number}`: a trivial move relabels the
// same physical file to a new level rather than replacing it
// with a newly-written one. `input_file_numbers` drives both
// cache eviction and (via `PostCompactionCleanup`) physical
// deletion in `install_compaction` — including this file's
// number here would delete the very file this edit just
// added back into the version.
input_file_numbers: HashSet::new(),
// No new file numbers are consumed by a metadata-only move.
next_file_number_hint: file_number_start,
output_tombstones: OutputTombstones::new(),
});
}
let oldest_snapshot_seq = ctx
.active_snapshots
.iter()
.min()
.copied()
.unwrap_or(SequenceNumber::MAX);
let target_compression = if !ctx.options.compression_per_level.is_empty()
&& target_level < ctx.options.compression_per_level.len()
{
ctx.options.compression_per_level[target_level]
} else {
ctx.options.compression
};
let build_opts = TableBuildOptions {
block_size: ctx.options.block_size,
block_restart_interval: ctx.options.block_restart_interval,
bloom_bits_per_key: ctx.options.bloom_bits_per_key,
internal_keys: true,
compression: target_compression,
prefix_len: ctx.options.prefix_len,
block_property_collectors: Vec::new(),
};
// Compute split points from target-level file boundaries.
// L0 files overlap arbitrarily, so every sub-task would have to
// include all L0 sources — redundant I/O with no benefit.
// Sub-compaction is only useful for Ln→Ln+1 (level >= 1).
let max_subs = if task.level == 0 || task_has_range_deletions(task) {
1
} else {
ctx.options.max_subcompactions.max(1)
};
let split_points = compute_split_points(task, max_subs);
let actual_subs = split_points.len() + 1;
// Build sub-tasks
let sub_tasks = build_sub_tasks(task, &split_points);
// Collect ALL range tombstones once from ALL input files.
// These must be shared across all sub-tasks because a single range
// tombstone can span multiple sub-compaction key ranges.
let all_input_files: Vec<TableFile> = task
.input_files_level
.iter()
.chain(task.input_files_next.iter())
.cloned()
.collect();
let all_range_del_entries = collect_range_del_entries(&all_input_files).ctx()?;
let all_raw_tombstones = collect_raw_tombstones(&all_input_files).ctx()?;
// Shared atomic counter for thread-safe file number allocation
let file_counter = AtomicU64::new(file_number_start);
let sub_params = SubCompactionParams {
target_level,
is_bottommost,
build_opts: &build_opts,
oldest_snapshot_seq,
file_number_counter: &file_counter,
file_number_limit,
all_range_del_entries: &all_range_del_entries,
all_raw_tombstones: &all_raw_tombstones,
};
let sub_outputs = if actual_subs <= 1 {
// Fast path: single sub-compaction (zero overhead)
vec![execute_sub_compaction_io(ctx, &sub_tasks[0], &sub_params).ctx()?]
} else {
// Parallel sub-compactions
let thread_results: Vec<Result<SubCompactionOutput>> = scope(|s| {
let handles: Vec<_> = sub_tasks
.iter()
.map(|sub| s.spawn(|| execute_sub_compaction_io(ctx, sub, &sub_params)))
.collect();
handles
.into_iter()
.map(|h| match h.join() {
Ok(r) => r,
Err(_) => Err(Error::background("sub-compaction thread panicked")),
})
.collect()
});
let mut outputs = Vec::with_capacity(thread_results.len());
let mut first_err: Option<Error> = None;
for r in thread_results {
match r {
Ok(sub_out) => outputs.push(sub_out),
Err(e) => {
if first_err.is_none() {
first_err = Some(e);
}
}
}
}
if let Some(e) = first_err {
// Collect known file numbers from successful sub-compactions.
let known: std::collections::HashSet<u64> = outputs
.iter()
.flat_map(|o| o.new_files.iter().map(|(_, m)| m.number))
.collect();
// Clean up SST files from successful sub-compactions.
for sub_out in &outputs {
for (_, meta) in &sub_out.new_files {
let orphan = ctx.db_path.join(format!("{:06}.sst", meta.number));
let _ = remove_file(&orphan);
}
}
// Clean up any orphaned SSTs from the panicked thread(s):
// file numbers were consumed from the shared counter but never
// reported, so delete any .sst file in the allocated range that
// is not accounted for in the successful outputs.
let end = file_counter.load(Ordering::Acquire);
for num in file_number_start..end {
if !known.contains(&num) {
let orphan = ctx.db_path.join(format!("{:06}.sst", num));
let _ = remove_file(&orphan);
}
}
return Err(e);
}
outputs
};
// Merge sub-compaction outputs
let mut edit = VersionEdit::new();
let mut output_tombstones = OutputTombstones::new();
for sub_out in sub_outputs {
for file_entry in sub_out.new_files {
edit.new_files.push(file_entry);
}
// File numbers come from a shared atomic counter, so per-sub maps
// are disjoint and extend cannot collide.
output_tombstones.extend(sub_out.output_tombstones);
}
// Record deletions (orchestrator responsibility)
let input_files: Vec<(u32, FileMetaData)> = task
.input_files_level
.iter()
.map(|f| (task.level as u32, f.meta.clone()))
.chain(
task.input_files_next
.iter()
.map(|f| (target_level as u32, f.meta.clone())),
)
.collect();
let input_file_numbers: HashSet<u64> =
input_files.iter().map(|(_, meta)| meta.number).collect();
for tf in &task.input_files_level {
edit.delete_file(task.level as u32, tf.meta.number);
}
for tf in &task.input_files_next {
edit.delete_file(target_level as u32, tf.meta.number);
}
Ok(CompactionOutput {
edit,
input_files,
input_file_numbers,
next_file_number_hint: file_counter.load(Ordering::Relaxed),
output_tombstones,
})
}
/// Install the result of a compaction: apply the VersionEdit, evict
/// old files from cache, and delete old SST files from disk.
/// Requires `&mut VersionSet` (hold the lock for this short phase only).
pub fn install_compaction(
mut output: CompactionOutput,
versions: &mut VersionSet,
table_cache: Option<&Arc<TableCache>>,
block_cache: Option<&Arc<BlockCache>>,
db_path: &Path,
stats: Option<&Arc<DbStats>>,
) -> Result<PostCompactionCleanup> {
// Files this compaction physically CREATED. A trivial move's "new"
// file is the live input file itself (same number, relabeled to the
// target level), so it must never be deleted or evicted by the
// discard/error paths below — the current version still references
// it at the source level, and deleting it would permanently destroy
// data the MANIFEST points at. Normal outputs use freshly reserved
// numbers that can never collide with an input's.
let input_numbers: HashSet<u64> = output
.input_files
.iter()
.map(|(_, meta)| meta.number)
.collect();
let created_files: Vec<(u32, FileMetaData)> = output
.edit
.new_files
.iter()
.filter(|(_, meta)| !input_numbers.contains(&meta.number))
.cloned()
.collect();
// Guard against stale compaction results. If any input file has already
// been removed, or if a concurrent install added an unexpected target-level
// overlap while this compaction was doing I/O, this output is based on an
// outdated version and must be discarded.
let discard = {
let version = versions.current();
let stale = output.input_files.iter().any(|(level, expected)| {
let level = *level as usize;
level >= version.num_levels
|| !version
.level_files(level)
.iter()
.any(|tf| tf.meta == *expected)
});
stale || Self::outputs_overlap_unexpected_current_files(&output, &version)
};
if discard {
cleanup_output_files(db_path, &created_files, None);
evict_table_cache_files(table_cache, &created_files);
return Ok(PostCompactionCleanup {
files_to_delete: HashSet::new(),
});
}
// Belt-and-braces: `allocate_output_file_number` enforces
// `file_number_limit`, so the sub-compaction counter cannot exceed
// the range reserved under the DB lock, and the reservation already
// advanced `next_file_number` past `file_number_limit` — this call
// is expected to be a no-op. Keep it as a cheap forward-only guard
// so file numbers stay collision-free even if a future edge case
// violates that reasoning.
versions.ensure_file_number_at_least(output.next_file_number_hint);
output
.edit
.set_next_file_number(versions.next_file_number());
if let Err(e) = versions.log_and_apply(output.edit) {
// Delete only the freshly-written SSTs (otherwise they would be
// orphaned on disk) — `created_files` excludes a trivial move's
// still-live input file.
cleanup_output_files(db_path, &created_files, None);
evict_table_cache_files(table_cache, &created_files);
return Err(e).ctx();
}
// Evict from caches (fast, safe before sync)
for num in &output.input_file_numbers {
if let Some(cache) = table_cache {
cache.evict(*num);
}
if let Some(bc) = block_cache {
bc.invalidate_file(*num);
}
}
if let Some(s) = stats {
s.record_compaction_completed();
}
// Return the file numbers that need deletion AFTER manifest sync
Ok(PostCompactionCleanup {
files_to_delete: output.input_file_numbers,
})
}
/// Check whether any compaction output overlaps a target-level file that
/// this edit neither deletes nor produced — evidence that a concurrent
/// install raced the I/O phase. Installing such an output would break the
/// L1+ disjointness invariant, so it must be discarded.
///
/// Runs under the DB lock: output extents come from the tombstone ranges
/// captured at build time (`output.output_tombstones`) — no output SST is
/// opened or read here. Current-version files are checked through their
/// long-lived readers, whose range tombstones are cached after first
/// access (same pattern the pick phase uses).
fn outputs_overlap_unexpected_current_files(
output: &CompactionOutput,
version: &Version,
) -> bool {
let deleted: HashSet<(u32, u64)> = output.edit.deleted_files.iter().copied().collect();
for (level, meta) in &output.edit.new_files {
let level_idx = *level as usize;
if level_idx == 0 || level_idx >= version.num_levels {
continue;
}
let mut extents = Vec::new();
if !meta.smallest_key.is_empty() && !meta.largest_key.is_empty() {
extents.push(UserKeyRange::File {
smallest: user_key(&meta.smallest_key).to_vec(),
largest: user_key(&meta.largest_key).to_vec(),
});
}
if let Some(tombstones) = output.output_tombstones.get(&meta.number) {
extents.extend(
tombstones
.iter()
.map(|(begin, end)| UserKeyRange::Tombstone {
begin: begin.clone(),
end: end.clone(),
}),
);
}
if extents.is_empty() {
continue;
}
for current in version.level_files(level_idx) {
if deleted.contains(&(*level, current.meta.number)) {
continue;
}
if extents
.iter()
.any(|extent| file_overlaps_extent(current, extent))
{
tracing::warn!(
"discarding stale compaction output {:06}: overlaps current L{} file {:06}",
meta.number,
level_idx,
current.meta.number
);
return true;
}
}
}
false
}
/// Delete old SST files after manifest has been synced.
pub fn run_post_compaction_cleanup(cleanup: &PostCompactionCleanup, db_path: &Path) {
for num in &cleanup.files_to_delete {
let old_path = db_path.join(format!("{:06}.sst", num));
if let Err(e) = remove_file(&old_path) {
tracing::warn!("failed to remove old SST {}: {}", old_path.display(), e);
}
}
}
/// Force-merge all files at a given level into one output at the same level.
/// Drops tombstones if this is the bottommost level.
pub(crate) fn force_merge_level(
ctx: &CompactionContext<'_>,
level: usize,
versions: &mut VersionSet,
table_cache: Option<&Arc<TableCache>>,
block_cache: Option<&Arc<BlockCache>>,
) -> Result<()> {
let oldest_snapshot_seq = ctx
.active_snapshots
.iter()
.min()
.copied()
.unwrap_or(SequenceNumber::MAX);
let version = versions.current();
let files = version.level_files(level);
if files.is_empty() {
return Ok(());
}
let is_bottommost =
Self::is_bottommost_level(&version, level, ctx.options.num_levels, files);
// A single file with a no-op filter is already in its final form —
// rewriting would waste I/O for no benefit. The same holds when a
// non-noop filter cannot fire anyway (the merge loop only applies
// filters at the bottommost level with no active snapshots): the
// rewrite would be byte-for-byte identical to its input. Skipping
// also forgoes snapshot-dedup/seq-zeroing for that file, but that
// only retains extra data — always read-safe, and picked up by the
// next compaction that touches the file. When the filter can
// actually remove or change entries (e.g. lazy-delete dead keys),
// even a single file must be reprocessed so every key-value pair
// passes through the filter.
let filter_can_apply = is_bottommost && ctx.active_snapshots.is_empty();
if files.len() == 1
&& (!filter_can_apply
|| ctx
.options
.compaction_filter
.as_ref()
.is_none_or(|f| f.is_noop()))
{
return Ok(());
}
let mut sources: Vec<IterSource> = Vec::new();
for tf in files {
// Compaction scans every input block exactly once; filling the
// block cache would evict hot point-read blocks for no benefit.
let iter = TableIterator::new(tf.reader.clone()).with_fill_cache(false);
sources.push(IterSource::from_boxed(Box::new(iter)));
}
// Inject range tombstones from new-format SSTs into the merge stream
let range_del_entries = collect_range_del_entries(files).ctx()?;
if !range_del_entries.is_empty() {
sources.push(IterSource::new(range_del_entries));
}
let mut merger = MergingIterator::new(sources, compare_internal_key);
let compression = if !ctx.options.compression_per_level.is_empty()
&& level < ctx.options.compression_per_level.len()
{
ctx.options.compression_per_level[level]
} else {
ctx.options.compression
};
// build_opts is a template; block_property_collectors are created fresh
// per output file (via factory functions) to avoid sharing mutable state.
let build_opts = TableBuildOptions {
block_size: ctx.options.block_size,
block_restart_interval: ctx.options.block_restart_interval,
bloom_bits_per_key: ctx.options.bloom_bits_per_key,
internal_keys: true,
compression,
prefix_len: ctx.options.prefix_len,
block_property_collectors: Vec::new(),
};
let mut edit = VersionEdit::new();
let mut builder: Option<TableBuilder> = None;
let mut current_file_number = 0u64;
let mut current_size = 0usize;
let mut current_file_user_key: Vec<u8> = Vec::new();
let mut pending_cut = false;
let mut last_point_key: Option<Vec<u8>> = None;
let mut last_range_del_key: Option<Vec<u8>> = None;
let mut last_written_seq: SequenceNumber = 0;
let mut snapshot_idx: usize = ctx.active_snapshots.len();
let mut range_tombstones = RangeTombstoneTracker::new();
while let Some((ikey, value)) = merger.next_entry() {
if ikey.len() < 8 {
continue;
}
let ikr = InternalKeyRef::new(&ikey);
let user_key = ikr.user_key();
// Defer size-triggered file cuts to user-key boundaries so a key's
// versions are never split across files (which would create overlapping
// same-level key ranges and possibly miss visible versions on reads).
if pending_cut && builder.is_some() && user_key != current_file_user_key.as_slice() {
let result = match builder.take().unwrap().finish().ctx() {
Ok(result) => result,
Err(e) => {
cleanup_output_files(
ctx.db_path,
&edit.new_files,
Some(current_file_number),
);
return Err(e);
}
};
if let Some(s) = ctx.stats {
s.record_compaction_bytes(result.file_size);
}
edit.add_file(
level as u32,
FileMetaData {
number: current_file_number,
file_size: result.file_size,
smallest_key: result.smallest_key.unwrap_or_default(),
largest_key: result.largest_key.unwrap_or_default(),
has_range_deletions: result.has_range_deletions,
},
);
current_size = 0;
pending_cut = false;
}
if ikr.value_type() == ValueType::RangeDeletion {
range_tombstones.add(user_key.to_vec(), value.as_slice().to_vec(), ikr.sequence());
range_tombstones.reset();
if let Some(ref last) = last_range_del_key
&& last.as_slice() == ikey.as_slice()
{
continue;
}
last_range_del_key = Some(ikey.clone());
if is_bottommost && ikr.sequence() < oldest_snapshot_seq {
continue;
}
} else if let Some(ref last) = last_point_key
&& last.as_slice() == user_key
{
// Same key — check if a snapshot needs this version.
while snapshot_idx > 0 && ctx.active_snapshots[snapshot_idx - 1] >= last_written_seq
{
snapshot_idx -= 1;
}
if snapshot_idx > 0 && ctx.active_snapshots[snapshot_idx - 1] >= ikr.sequence() {
last_written_seq = ikr.sequence();
// A retained older version shadowed by a range tombstone below
// the oldest snapshot must still be dropped, or it would
// resurrect if that tombstone is dropped at the bottommost level.
if ikr.value_type() == ValueType::Value
&& !range_tombstones.is_empty()
&& range_tombstones.is_deleted(
user_key,
ikr.sequence(),
oldest_snapshot_seq,
)
{
continue;
}
} else {
continue;
}
} else {
last_point_key = Some(user_key.to_vec());
last_written_seq = ikr.sequence();
snapshot_idx = ctx.active_snapshots.len();
// See execute_sub_compaction_io: strict decode so a corrupted
// trailer byte fails loudly instead of being silently
// misread as Deletion and permanently destroyed.
if is_bottommost
&& ikr.sequence() < oldest_snapshot_seq
&& ikr.value_type_checked().ctx()? == ValueType::Deletion
{
continue;
}
if ikr.value_type() == ValueType::Value && !range_tombstones.is_empty() {
let entry_seq = ikr.sequence();
if range_tombstones.is_deleted(user_key, entry_seq, oldest_snapshot_seq) {
continue;
}
}
}
// Apply compaction filter
let mut final_value = value;
if is_bottommost
&& ctx.active_snapshots.is_empty()
&& let Some(ref filter) = ctx.options.compaction_filter
&& ikr.value_type() == ValueType::Value
{
match filter.filter(level, user_key, final_value.as_slice()) {
CompactionFilterDecision::Keep => {}
CompactionFilterDecision::Remove => continue,
CompactionFilterDecision::ChangeValue(new_val) => {
final_value = LazyValue::Inline(new_val);
}
}
}
if builder.is_none() {
current_file_number = versions.new_file_number();
let sst_path = ctx.db_path.join(format!("{:06}.sst", current_file_number));
let mut opts = build_opts.clone();
opts.block_property_collectors = ctx
.options
.block_property_collectors
.iter()
.map(|f| f())
.collect();
builder = match TableBuilder::new(&sst_path, opts).ctx() {
Ok(builder) => Some(builder),
Err(e) => {
cleanup_output_files(
ctx.db_path,
&edit.new_files,
Some(current_file_number),
);
return Err(e);
}
};
current_size = 0;
}
// Sequence zeroing: at the bottommost level, if the entry's
// sequence falls below the minimum active snapshot, zero it out.
let final_ikey;
let ikey_ref = if is_bottommost
&& ikr.sequence() > 0
&& ikr.sequence() < oldest_snapshot_seq
&& ikr.value_type() == ValueType::Value
{
final_ikey = InternalKey::new(user_key, 0, ikr.value_type())
.as_bytes()
.to_vec();
&final_ikey
} else {
&ikey
};
if let Err(e) = builder
.as_mut()
.unwrap()
.add(ikey_ref, final_value.as_slice())
.ctx()
{
cleanup_output_files(ctx.db_path, &edit.new_files, Some(current_file_number));
return Err(e);
}
let entry_bytes = ikey_ref.len() + final_value.len();
current_size += entry_bytes;
if current_file_user_key != user_key {
current_file_user_key.clear();
current_file_user_key.extend_from_slice(user_key);
}
// Rate-limit compaction writes
if let Some(rl) = ctx.rate_limiter {
rl.request(entry_bytes);
}
if current_size >= ctx.options.target_file_size_base as usize
|| builder.as_ref().unwrap().projected_meta_size() >= META_BLOCK_SPLIT_THRESHOLD
{
// Defer the actual cut to the next user-key boundary. The
// meta-size condition keeps the per-file index / range-del
// blocks well below the reader's hard cap for key-heavy data.
pending_cut = true;
}
}
let active_file_number = builder.as_ref().map(|_| current_file_number);
if let Some(b) = builder {
let result = match b.finish().ctx() {
Ok(result) => result,
Err(e) => {
cleanup_output_files(ctx.db_path, &edit.new_files, Some(current_file_number));
return Err(e);
}
};
if let Some(s) = ctx.stats {
s.record_compaction_bytes(result.file_size);
}
edit.add_file(
level as u32,
FileMetaData {
number: current_file_number,
file_size: result.file_size,
smallest_key: result.smallest_key.unwrap_or_default(),
largest_key: result.largest_key.unwrap_or_default(),
has_range_deletions: result.has_range_deletions,
},
);
}
// Abort if the merge iterator encountered an I/O or corruption error.
if let Some(e) = merger.error() {
// Clean up orphan SST files written during the failed merge
for (_, meta) in &edit.new_files {
let orphan = ctx.db_path.join(format!("{:06}.sst", meta.number));
let _ = remove_file(&orphan);
}
if let Some(num) = active_file_number {
let orphan = ctx.db_path.join(format!("{:06}.sst", num));
let _ = remove_file(&orphan);
}
return Err(Error::background(format!(
"force_merge iterator error: {}",
e
)));
}
let input_file_numbers: HashSet<u64> = files.iter().map(|f| f.meta.number).collect();
for tf in files {
edit.delete_file(level as u32, tf.meta.number);
}
edit.set_next_file_number(versions.next_file_number());
// Capture output files so they can be deleted if the install fails,
// otherwise the freshly-written SSTs would be orphaned on disk.
let output_files = edit.new_files.clone();
if let Err(e) = versions.log_and_apply(edit) {
cleanup_output_files(ctx.db_path, &output_files, None);
return Err(e).ctx();
}
// force_merge_level holds &mut VersionSet for the duration, sync here.
versions.sync_manifest().ctx()?;
if let Some(cache) = table_cache {
for num in &input_file_numbers {
cache.evict(*num);
}
}
if let Some(bc) = block_cache {
for num in &input_file_numbers {
bc.invalidate_file(*num);
}
}
// Safe to delete old SSTs after manifest is synced
for num in &input_file_numbers {
let old_path = ctx.db_path.join(format!("{:06}.sst", num));
if let Err(e) = remove_file(&old_path) {
tracing::warn!("failed to remove old SST {}: {}", old_path.display(), e);
}
}
if let Some(s) = ctx.stats {
s.record_compaction_completed();
}
Ok(())
}
/// Pick a compaction based on a read-triggered hint. If the hinted level
/// has more than one file, pick the largest file for compaction into the
/// next level.
pub fn pick_compaction_for_hint(
version: &Version,
hint: &CompactionHint,
in_flight: &HashSet<u64>,
) -> Option<CompactionTask> {
let level = hint.level;
if level == 0 || level >= version.num_levels.saturating_sub(1) {
return None;
}
let files = version.level_files(level);
if files.len() <= 1 {
return None;
}
// Pick the largest file at the hinted level.
Self::pick_level_compaction(version, level, in_flight)
}
/// Maximum bytes for a given level.
fn max_bytes_for_level(options: &DbOptions, level: usize) -> u64 {
let mut result = options.max_bytes_for_level_base;
for _ in 1..level {
result = (result as f64 * options.max_bytes_for_level_multiplier) as u64;
}
result
}
/// Compute the total key range of a set of files.
/// Uses `compare_internal_key` for correct variable-length user key ordering.
fn total_key_range(files: &[TableFile]) -> (Vec<u8>, Vec<u8>) {
let mut smallest = Vec::new();
let mut largest = Vec::new();
for f in files {
if smallest.is_empty()
|| compare_internal_key(&f.meta.smallest_key, &smallest) == CmpOrdering::Less
{
smallest = f.meta.smallest_key.clone();
}
if largest.is_empty()
|| compare_internal_key(&f.meta.largest_key, &largest) == CmpOrdering::Greater
{
largest = f.meta.largest_key.clone();
}
}
(smallest, largest)
}
/// Check whether `target_level` is the effective bottommost level for a given
/// compaction range. A level is bottommost if no deeper level contains files
/// that overlap the compaction's user-key range. This allows tombstones to be
/// dropped early instead of being retained until the statically configured
/// last level.
pub(crate) fn is_bottommost_level(
version: &Version,
target_level: usize,
num_levels: usize,
all_inputs: &[TableFile],
) -> bool {
let mut extents = Vec::new();
for tf in all_inputs {
if !add_file_extents(tf, &mut extents) {
return false;
}
}
if extents.is_empty() {
return true;
}
let input_numbers: HashSet<u64> = all_inputs.iter().map(|tf| tf.meta.number).collect();
for level in target_level..num_levels {
for f in version.level_files(level) {
if input_numbers.contains(&f.meta.number) {
continue;
}
if extents.iter().any(|extent| file_overlaps_extent(f, extent)) {
return false;
}
}
}
true
}
/// Find files in a level that overlap with the given key range.
/// Uses **user key** comparison to detect overlap correctly.
///
/// Internal key comparison is wrong here: two files sharing the same user
/// key at different sequence numbers can appear non-overlapping in internal
/// key order (higher seq sorts first). For example, an L0 tombstone at
/// (key, seq=100) sorts *before* an L1 value at (key, seq=0), making the
/// internal-key ranges disjoint even though both files contain the same
/// user key.
fn overlapping_files(files: &[TableFile], smallest: &[u8], largest: &[u8]) -> Vec<TableFile> {
let smallest_uk = user_key(smallest);
let largest_uk = user_key(largest);
files
.iter()
.filter(|f| {
let file_largest_uk = user_key(&f.meta.largest_key);
let file_smallest_uk = user_key(&f.meta.smallest_key);
// File overlaps if: file.largest_uk >= smallest_uk AND file.smallest_uk <= largest_uk.
// This is metadata-only; range-aware callers use `overlapping_files_for_inputs`.
file_largest_uk >= smallest_uk && file_smallest_uk <= largest_uk
})
.cloned()
.collect()
}
}
#[cfg(test)]
mod tests {
use crate::db::DB;
use crate::options::DbOptions;
#[test]
fn test_discarded_trivial_move_preserves_live_input_file() {
use std::collections::HashSet;
use super::{CompactionOutput, LeveledCompaction, OutputTombstones};
use crate::manifest::version_edit::{FileMetaData, VersionEdit};
use crate::manifest::version_set::VersionSet;
use crate::sst::table_builder::{TableBuildOptions, TableBuilder};
use crate::types::{InternalKey, ValueType};
fn build_sst(
dir: &std::path::Path,
number: u64,
first: &[u8],
last: &[u8],
) -> FileMetaData {
let path = dir.join(format!("{:06}.sst", number));
let mut builder = TableBuilder::new(
&path,
TableBuildOptions {
internal_keys: true,
bloom_bits_per_key: 0,
..Default::default()
},
)
.unwrap();
for key in [first, last] {
let ik = InternalKey::new(key, 1, ValueType::Value).into_bytes();
builder.add(&ik, b"v").unwrap();
}
let result = builder.finish().unwrap();
FileMetaData {
number,
file_size: result.file_size,
smallest_key: result.smallest_key.unwrap(),
largest_key: result.largest_key.unwrap(),
has_range_deletions: false,
}
}
let dir = tempfile::tempdir().unwrap();
let mut versions = VersionSet::create(dir.path(), 4).unwrap();
// Live input file #1 at L1, range [e, f].
let meta1 = build_sst(dir.path(), 1, b"e", b"f");
let mut edit = VersionEdit::new();
edit.add_file(1, meta1.clone());
edit.set_next_file_number(versions.next_file_number());
versions.log_and_apply(edit).unwrap();
// Conflicting file #2 at L2 spanning [a, z] — simulates a concurrent
// install (e.g. a force_merge_level hull output) landing while the
// trivial move below was in its unlocked pick→install window.
let meta2 = build_sst(dir.path(), 2, b"a", b"z");
let mut edit = VersionEdit::new();
edit.add_file(2, meta2.clone());
edit.set_next_file_number(versions.next_file_number());
versions.log_and_apply(edit).unwrap();
// The trivial-move output exactly as execute_compaction_io builds it:
// relabel file #1 from L1 to L2. `input_file_numbers` is empty
// because no new physical file was written — the "new" file at L2 is
// the same live 000001.sst.
let mut edit = VersionEdit::new();
edit.delete_file(1, meta1.number);
edit.add_file(2, meta1.clone());
let output = CompactionOutput {
edit,
input_files: vec![(1, meta1.clone())],
input_file_numbers: HashSet::new(),
next_file_number_hint: versions.next_file_number(),
output_tombstones: OutputTombstones::new(),
};
// Install must DISCARD the move (file #1's range now overlaps the
// unexpected live L2 file #2, so installing would break L2
// disjointness) — and the discard must NOT physically delete the
// still-referenced input file.
let cleanup = LeveledCompaction::install_compaction(
output,
&mut versions,
None,
None,
dir.path(),
None,
)
.unwrap();
assert!(
cleanup.files_to_delete.is_empty(),
"a discarded install must not schedule any deletions"
);
assert!(
dir.path().join("000001.sst").exists(),
"discarding a trivial move must not delete the live input SST"
);
let version = versions.current();
assert!(
version
.level_files(1)
.iter()
.any(|tf| tf.meta.number == meta1.number),
"input file must remain at L1 after the discard"
);
assert!(
version
.level_files(2)
.iter()
.all(|tf| tf.meta.number != meta1.number),
"discarded move must not appear at L2"
);
}
#[test]
fn test_compaction_trigger() {
let dir = tempfile::tempdir().unwrap();
// Use small memtable to create multiple L0 files
let opts = DbOptions {
create_if_missing: true,
write_buffer_size: 512,
l0_compaction_trigger: 4,
target_file_size_base: 1024 * 1024,
..Default::default()
};
let db = DB::open(opts.clone(), dir.path()).unwrap();
// Write enough data to create multiple L0 SSTs
for i in 0..200 {
let key = format!("key_{:06}", i);
let val = format!("value_{:040}", i);
db.put(key.as_bytes(), val.as_bytes()).unwrap();
}
// Check number of SST files
let sst_count = std::fs::read_dir(dir.path())
.unwrap()
.filter(|e| {
e.as_ref()
.unwrap()
.file_name()
.to_string_lossy()
.ends_with(".sst")
})
.count();
assert!(sst_count > 0, "should have SST files");
// All data should still be readable
for i in 0..200 {
let key = format!("key_{:06}", i);
let val = format!("value_{:040}", i);
assert_eq!(
db.get(key.as_bytes()).unwrap(),
Some(val.into_bytes()),
"failed at key {}",
i
);
}
}
#[test]
fn test_manual_compaction() {
let dir = tempfile::tempdir().unwrap();
let opts = DbOptions {
create_if_missing: true,
write_buffer_size: 512,
l0_compaction_trigger: 100, // don't auto-compact
..Default::default()
};
let db = DB::open(opts.clone(), dir.path()).unwrap();
// Write and flush multiple times to create L0 files
for batch in 0..5 {
for i in 0..20 {
let key = format!("key_{:04}", batch * 20 + i);
let val = format!("val_{}", batch * 20 + i);
db.put(key.as_bytes(), val.as_bytes()).unwrap();
}
db.flush().unwrap();
}
// Trigger compaction
db.compact().unwrap();
// Verify all data
for i in 0..100 {
let key = format!("key_{:04}", i);
let val = format!("val_{}", i);
assert_eq!(
db.get(key.as_bytes()).unwrap(),
Some(val.into_bytes()),
"failed at key {} after compaction",
i
);
}
}
#[test]
fn test_compaction_removes_tombstones() {
let dir = tempfile::tempdir().unwrap();
let opts = DbOptions {
create_if_missing: true,
write_buffer_size: 512,
l0_compaction_trigger: 100,
num_levels: 2, // Only L0 and L1, so L1 is bottom
..Default::default()
};
let db = DB::open(opts, dir.path()).unwrap();
// Write then delete
for i in 0..20 {
let key = format!("key_{:04}", i);
db.put(key.as_bytes(), b"value").unwrap();
}
db.flush().unwrap();
for i in 0..10 {
let key = format!("key_{:04}", i);
db.delete(key.as_bytes()).unwrap();
}
db.flush().unwrap();
// Compact
db.compact().unwrap();
// Deleted keys should be gone
for i in 0..10 {
let key = format!("key_{:04}", i);
assert_eq!(db.get(key.as_bytes()).unwrap(), None);
}
// Remaining keys should exist
for i in 10..20 {
let key = format!("key_{:04}", i);
assert_eq!(db.get(key.as_bytes()).unwrap(), Some(b"value".to_vec()));
}
}
#[test]
fn test_compaction_with_overwrites() {
let dir = tempfile::tempdir().unwrap();
let opts = DbOptions {
create_if_missing: true,
write_buffer_size: 512,
l0_compaction_trigger: 100,
..Default::default()
};
let db = DB::open(opts, dir.path()).unwrap();
// Write v1
for i in 0..20 {
let key = format!("key_{:04}", i);
db.put(key.as_bytes(), b"v1").unwrap();
}
db.flush().unwrap();
// Overwrite with v2
for i in 0..20 {
let key = format!("key_{:04}", i);
db.put(key.as_bytes(), b"v2").unwrap();
}
db.flush().unwrap();
db.compact().unwrap();
// Should see v2
for i in 0..20 {
let key = format!("key_{:04}", i);
assert_eq!(
db.get(key.as_bytes()).unwrap(),
Some(b"v2".to_vec()),
"key {} should have value v2 after compaction",
i
);
}
}
#[test]
fn test_sub_compaction_correctness() {
let dir = tempfile::tempdir().unwrap();
let opts = DbOptions {
create_if_missing: true,
write_buffer_size: 512,
l0_compaction_trigger: 100,
target_file_size_base: 512, // Small files to create many L1 files
max_subcompactions: 4,
..Default::default()
};
let db = DB::open(opts, dir.path()).unwrap();
// Write enough data to create multiple L0 files
for batch in 0..8 {
for i in 0..50 {
let key = format!("key_{:06}", batch * 50 + i);
let val = format!("value_{:040}", batch * 50 + i);
db.put(key.as_bytes(), val.as_bytes()).unwrap();
}
db.flush().unwrap();
}
// Compact with sub-compaction
db.compact().unwrap();
// Verify all data is intact
for i in 0..400 {
let key = format!("key_{:06}", i);
let val = format!("value_{:040}", i);
assert_eq!(
db.get(key.as_bytes()).unwrap(),
Some(val.into_bytes()),
"key {} missing after sub-compaction",
i
);
}
}
#[test]
fn test_sub_compaction_with_deletions() {
let dir = tempfile::tempdir().unwrap();
let opts = DbOptions {
create_if_missing: true,
write_buffer_size: 512,
l0_compaction_trigger: 100,
target_file_size_base: 512,
max_subcompactions: 4,
num_levels: 2, // bottommost for tombstone GC
..Default::default()
};
let db = DB::open(opts, dir.path()).unwrap();
// Write keys
for i in 0..100 {
let key = format!("key_{:06}", i);
db.put(key.as_bytes(), b"v1").unwrap();
}
db.flush().unwrap();
// Delete even keys
for i in (0..100).step_by(2) {
let key = format!("key_{:06}", i);
db.delete(key.as_bytes()).unwrap();
}
db.flush().unwrap();
// Overwrite some odd keys
for i in (1..100).step_by(2) {
let key = format!("key_{:06}", i);
db.put(key.as_bytes(), b"v2").unwrap();
}
db.flush().unwrap();
db.compact().unwrap();
// Even keys should be gone
for i in (0..100).step_by(2) {
let key = format!("key_{:06}", i);
assert_eq!(
db.get(key.as_bytes()).unwrap(),
None,
"key {} should be deleted",
i
);
}
// Odd keys should have v2
for i in (1..100).step_by(2) {
let key = format!("key_{:06}", i);
assert_eq!(
db.get(key.as_bytes()).unwrap(),
Some(b"v2".to_vec()),
"key {} should be v2",
i
);
}
}
#[test]
fn test_sub_compaction_with_range_tombstones() {
let dir = tempfile::tempdir().unwrap();
let opts = DbOptions {
create_if_missing: true,
write_buffer_size: 512,
l0_compaction_trigger: 100,
target_file_size_base: 512,
max_subcompactions: 4,
num_levels: 2,
..Default::default()
};
let db = DB::open(opts, dir.path()).unwrap();
// Write keys spanning a wide range
for i in 0..200 {
let key = format!("key_{:06}", i);
db.put(key.as_bytes(), b"val").unwrap();
}
db.flush().unwrap();
// Delete a range that likely spans sub-compaction boundaries
db.delete_range(b"key_000050", b"key_000150").unwrap();
db.flush().unwrap();
db.compact().unwrap();
// Keys outside range should exist
for i in 0..50 {
let key = format!("key_{:06}", i);
assert_eq!(
db.get(key.as_bytes()).unwrap(),
Some(b"val".to_vec()),
"key {} should survive",
i
);
}
for i in 150..200 {
let key = format!("key_{:06}", i);
assert_eq!(
db.get(key.as_bytes()).unwrap(),
Some(b"val".to_vec()),
"key {} should survive",
i
);
}
// Keys in range should be deleted
for i in 50..150 {
let key = format!("key_{:06}", i);
assert_eq!(
db.get(key.as_bytes()).unwrap(),
None,
"key {} should be deleted",
i
);
}
}
#[test]
fn test_compact_range_keeps_range_tombstone_until_covered_files_included() {
let dir = tempfile::tempdir().unwrap();
let opts = DbOptions {
create_if_missing: true,
write_buffer_size: 1024 * 1024,
l0_compaction_trigger: 100,
target_file_size_base: 512,
num_levels: 2,
..Default::default()
};
let db = DB::open(opts, dir.path()).unwrap();
let value = vec![b'v'; 96];
for i in 0..200 {
let key = format!("key_{:03}", i);
db.put(key.as_bytes(), &value).unwrap();
}
db.flush().unwrap();
db.compact().unwrap();
db.delete_range(b"key_000", b"key_200").unwrap();
db.flush().unwrap();
db.compact_range(Some(b"key_000"), Some(b"key_001"))
.unwrap();
for i in [0, 1, 50, 150, 199] {
let key = format!("key_{:03}", i);
assert_eq!(
db.get(key.as_bytes()).unwrap(),
None,
"range-compacted tombstone must continue deleting {key}"
);
}
}
#[test]
fn test_sub_compaction_with_snapshots() {
let dir = tempfile::tempdir().unwrap();
let opts = DbOptions {
create_if_missing: true,
write_buffer_size: 512,
l0_compaction_trigger: 100,
target_file_size_base: 512,
max_subcompactions: 4,
..Default::default()
};
let db = DB::open(opts, dir.path()).unwrap();
// Write v1
for i in 0..100 {
let key = format!("key_{:06}", i);
db.put(key.as_bytes(), b"v1").unwrap();
}
db.flush().unwrap();
// Take snapshot
let snap = db.snapshot();
let snap_opts = snap.read_options();
// Write v2
for i in 0..100 {
let key = format!("key_{:06}", i);
db.put(key.as_bytes(), b"v2").unwrap();
}
db.flush().unwrap();
// Compact (snapshot should protect v1 versions)
db.compact().unwrap();
// Current reads see v2
for i in 0..100 {
let key = format!("key_{:06}", i);
assert_eq!(
db.get(key.as_bytes()).unwrap(),
Some(b"v2".to_vec()),
"current key {} should be v2",
i
);
}
// Snapshot reads see v1
for i in 0..100 {
let key = format!("key_{:06}", i);
assert_eq!(
db.get_with_options(&snap_opts, key.as_bytes()).unwrap(),
Some(b"v1".to_vec()),
"snapshot key {} should be v1",
i
);
}
drop(snap);
}
#[test]
fn test_sub_compaction_end_to_end() {
// Integration test: write data, compact with max_subcompactions=4,
// then compact again to exercise multi-level sub-compaction.
let dir = tempfile::tempdir().unwrap();
let opts = DbOptions {
create_if_missing: true,
write_buffer_size: 512,
l0_compaction_trigger: 100,
target_file_size_base: 512,
max_subcompactions: 4,
..Default::default()
};
let db = DB::open(opts, dir.path()).unwrap();
// Write enough data across many flushes to create multiple L1 files
for batch in 0..10 {
for i in 0..50 {
let key = format!("key_{:06}", batch * 50 + i);
let val = format!("value_{:040}", batch * 50 + i);
db.put(key.as_bytes(), val.as_bytes()).unwrap();
}
db.flush().unwrap();
}
// First compaction: L0 → L1 (creates multiple small L1 files)
db.compact().unwrap();
// Write more data and flush again to create new L0 files
for batch in 0..5 {
for i in 0..50 {
let key = format!("key_{:06}", batch * 50 + i);
let val = format!("updated_{:040}", batch * 50 + i);
db.put(key.as_bytes(), val.as_bytes()).unwrap();
}
db.flush().unwrap();
}
// Second compaction: should exercise sub-compaction on L1→L2
db.compact().unwrap();
// Verify all data: first 250 keys have "updated_" values
for i in 0..250 {
let key = format!("key_{:06}", i);
let val = format!("updated_{:040}", i);
assert_eq!(
db.get(key.as_bytes()).unwrap(),
Some(val.into_bytes()),
"key {} should have updated value",
i
);
}
// Keys 250..500 have original values
for i in 250..500 {
let key = format!("key_{:06}", i);
let val = format!("value_{:040}", i);
assert_eq!(
db.get(key.as_bytes()).unwrap(),
Some(val.into_bytes()),
"key {} should have original value",
i
);
}
}
}