CompactFiles() with allow_trivial_move never releases its SstFileManager space reservation, so compactions are eventually refused as "Compaction too large" on a nearly empty DB
Summary
DBImpl::CompactFilesImpl() reserves space with the SstFileManager before
compacting and releases it at the end of the function. The trivial-move branch
added in #14112 returns between the two on the success path, after
destroying the Compaction. Every CompactFiles() that resolves to a trivial
move therefore leaks sum(input file sizes) into
SstFileManagerImpl::cur_compactions_reserved_size_ for the lifetime of the
process.
Once the leaked total approaches max_allowed_space, EnoughRoomForCompaction()
refuses every further compaction, including non-trivial ones, with
Status::CompactionTooLarge(), however little data the DB actually holds.
Root cause
db/db_impl/db_impl_compaction_flush.cc on main @ 507206e4. Reserve
(:1812-1815):
bool sfm_reserved_compact_space = false;
// First check if we have enough room to do the compaction
bool enough_room = EnoughRoomForCompaction(
cfd, input_files, &sfm_reserved_compact_space, log_buffer);which ends in SstFileManagerImpl::EnoughRoomForCompaction()
(file/sst_file_manager_impl.cc:211):
cur_compactions_reserved_size_ += size_added_by_compaction;Trivial-move branch (:1849-1894, abridged):
if (is_trivial_move) {
...
Status status = CommitTrivialMove(*c.get(), compaction_released);
...
c.reset(); // :1886
bg_compaction_scheduled_--;
...
MaybeScheduleFlushOrCompaction();
return status; // :1893
}Release, never reached from that branch (:1962-1967):
// Need to make sure SstFileManager does its bookkeeping
auto sfm = static_cast<SstFileManagerImpl*>(
immutable_db_options_.sst_file_manager.get());
if (sfm && sfm_reserved_compact_space) {
sfm->OnCompactionCompletion(c.get());
}OnCompactionCompletion() (file/sst_file_manager_impl.cc:102-103) is the only
place cur_compactions_reserved_size_ is decremented, and it needs the
Compaction object that has already been destroyed. The automatic path does
this correctly: BackgroundCompaction() also reserves for trivial moves, but
falls through to its own release at :5149-5150. Only the CompactFiles() path
leaks.
The leak feeds the admission check directly (file/sst_file_manager_impl.cc:174-179):
size_t needed_headroom = cur_compactions_reserved_size_ +
size_added_by_compaction + compaction_buffer_size_;
if (max_allowed_space_ != 0 &&
(needed_headroom + total_files_size_ > max_allowed_space_)) {
return false;
}Reproduction
Self-contained, public API only. A 64 MiB SstFileManager budget, auto
compaction off, then 40 rounds of: write one 256 KiB SST, and walk it down the
LSM with one CompactFiles() per level. The same workload runs twice, differing
only in CompactionOptions::allow_trivial_move. The outstanding reservation is
read back with nothing compacting by binary-searching SetMaxAllowedSpaceUsage()
against IsMaxAllowedSpaceReachedIncludingCompactions(), which tests
total + reserved >= max_allowed_space.
// CompactFiles() with allow_trivial_move leaks its SstFileManager reservation.
// Public API only. Usage: ./sfm_trivial_move_leak [scratch_dir]
#include <rocksdb/db.h>
#include <rocksdb/options.h>
#include <rocksdb/sst_file_manager.h>
#include <cstdio>
#include <memory>
#include <string>
#include <vector>
using namespace ROCKSDB_NAMESPACE;
static void Run(const std::string& dir, bool allow_trivial_move) {
const uint64_t kBudget = 64ull << 20;
std::shared_ptr<SstFileManager> sfm(NewSstFileManager(Env::Default()));
sfm->SetMaxAllowedSpaceUsage(kBudget);
Options o;
o.create_if_missing = true;
o.sst_file_manager = sfm;
o.compression = kNoCompression;
o.num_levels = 7;
o.disable_auto_compactions = true; // only our CompactFiles() calls reserve
DestroyDB(dir, o);
std::unique_ptr<DB> db;
Status s = DB::Open(o, dir, &db);
if (!s.ok()) { printf("open: %s\n", s.ToString().c_str()); return; }
CompactionOptions copts;
copts.compression = kNoCompression;
copts.allow_trivial_move = allow_trivial_move;
int ok = 0, refused = 0;
std::string first_error;
for (int round = 0; round < 40; round++) {
// One 256 KiB SST per round, each in its own key range.
db->Put(WriteOptions(), "r" + std::to_string(1000 + round),
std::string(256 << 10, 'x'));
db->Flush(FlushOptions());
// Walk it down the LSM with one CompactFiles() per level.
for (int lvl = 0; lvl + 1 < o.num_levels; lvl++) {
ColumnFamilyMetaData md;
db->GetColumnFamilyMetaData(&md);
std::vector<std::string> files;
for (const auto& f : md.levels[lvl].files) files.push_back(f.name);
if (files.empty()) continue;
s = db->CompactFiles(copts, files, lvl + 1);
if (s.ok()) {
ok++;
} else {
refused++;
if (first_error.empty()) first_error = s.ToString();
}
}
}
// Nothing is compacting now. Recover the outstanding reservation through the
// public API: IsMaxAllowedSpaceReachedIncludingCompactions() is
// total_size + reserved_for_compactions >= max_allowed_space
// so the smallest budget for which it is false is total + reserved + 1.
uint64_t total = sfm->GetTotalSize();
uint64_t lo = total, hi = total + (1ull << 34);
while (lo < hi) {
uint64_t mid = lo + (hi - lo) / 2;
sfm->SetMaxAllowedSpaceUsage(mid);
if (sfm->IsMaxAllowedSpaceReachedIncludingCompactions()) lo = mid + 1;
else hi = mid;
}
uint64_t reserved = lo - 1 - total;
printf("allow_trivial_move=%d: SST data %.2f MiB, still reserved %.2f MiB "
"(budget %llu MiB), CompactFiles ok=%d refused=%d %s\n",
allow_trivial_move, total / 1048576.0, reserved / 1048576.0,
(unsigned long long)(kBudget >> 20), ok, refused, first_error.c_str());
}
int main(int argc, char** argv) {
std::string base = argc > 1 ? argv[1] : "/tmp";
Run(base + "/sfm_leak_off", false);
Run(base + "/sfm_leak_on", true);
return 0;
}Build against a make static_lib tree (add -lsnappy -llz4 -lzstd -lbz2 etc. if
your librocksdb.a was built with them):
g++ -std=c++20 -O2 -o sfm_trivial_move_leak sfm_trivial_move_leak.cc \
-I rocksdb/include rocksdb/librocksdb.a -lpthread -ldl -lz -lnuma
./sfm_trivial_move_leak /tmpOutput on main @ 507206e4:
allow_trivial_move=0: SST data 10.05 MiB, still reserved 0.00 MiB (budget 64 MiB), CompactFiles ok=240 refused=0
allow_trivial_move=1: SST data 10.05 MiB, still reserved 54.54 MiB (budget 64 MiB), CompactFiles ok=217 refused=7 Compaction too large: Expected
With no compaction running, nothing should remain reserved: 0.00 MiB in both
runs, and no CompactFiles() call should be refused while the DB holds 10 MiB
against a 64 MiB budget. The allow_trivial_move=0 run shows exactly that.
Actual
With allow_trivial_move=1, 54.54 MiB (85 % of the budget) stays reserved
by compactions that finished long ago, and 7 CompactFiles() calls fail with
Compaction too large while real usage is 16 % of the limit. The leak is about
5.4× the data written because each file is trivially moved down about six
levels, and each move leaks its size again.
It only grows. Nothing short of a process restart (a new SstFileManager)
recovers it. Once it reaches the budget, EnoughRoomForCompaction() also turns
away automatic compactions, since they share the same counter.
Needs both a non-zero max_allowed_space (the default of 0 skips the headroom
check) and allow_trivial_move = true (default false). Both are ordinary
settings for deployments that cap RocksDB's disk footprint and drive compaction
themselves.
Suggested fix
Release in the trivial-move branch while c is still alive:
if (is_trivial_move) {
...
auto sfm = static_cast<SstFileManagerImpl*>(
immutable_db_options_.sst_file_manager.get());
if (sfm && sfm_reserved_compact_space) {
sfm->OnCompactionCompletion(c.get());
}
c.reset();
...
return status;
}A scope guard keyed on sfm_reserved_compact_space would be sturdier, since the
reservation now has two exit paths that must be kept in step. A regression test
could run the loop above and assert that
IsMaxAllowedSpaceReachedIncludingCompactions() is false once
SetMaxAllowedSpaceUsage(GetTotalSize() + 1) is set.
Environment
RocksDB main @ 507206e46bdf876174532b8eb6b26f54f698b8dc (2026-09-21), version 11.11.0
built with `make static_lib` (DEBUG_LEVEL=0), g++ 11.4.0, -std=c++20
Ubuntu 22.04.2 LTS, Linux 5.15.0-187-generic, x86_64
also reproduced on the released 11.8.1; present since 10.9.0 (#14112)Source: facebook/rocksdb