#15252·rocksdb

FIFO max_data_files_size drops far more user data than its budget requires: the trimming loop subtracts a flat per-file average instead of each file's real size

Author: hanke580Created Sep 22, 2026Updated Sep 22, 2026

Summary

CompactionOptionsFIFO::max_data_files_size is documented as a straightforward substitution for max_table_files_size (include/rocksdb/advanced_options.h:132-142):

When non-zero, FIFO compaction uses the combined size of SST files and blob files for size-based trimming decisions. When the total data size (SST + blob) exceeds this limit, the oldest SST files are dropped along with their associated blob files.

When non-zero, this takes precedence over max_table_files_size for all FIFO compaction decisions …

In FIFOCompactionPicker::PickSizeCompaction() the two budgets take different subtractions inside the same loop (db/compaction/compaction_picker_fifo.cc:256-283):

cpp
    uint64_t remaining_size = effective_size;
    const uint64_t num_files = last_level_files.size();
    const uint64_t data_per_file =
        (fifo_opts.max_data_files_size > 0 && num_files > 0)
            ? std::max(effective_size / num_files, uint64_t{1})
            : 0;

    for (auto ritr = last_level_files.rbegin(); ritr != last_level_files.rend(); ++ritr) {
      auto f = *ritr;
      if (fifo_opts.max_data_files_size > 0) {
        remaining_size -= std::min(remaining_size, data_per_file);   // flat average
      } else {
        remaining_size -= std::min(remaining_size, f->fd.file_size); // actual size
      }
      inputs[0].files.push_back(f);
      ...
      if (remaining_size <= effective_max) {
        break;
      }
    }

max_table_files_size subtracts what dropping each file actually frees. max_data_files_size subtracts a flat effective_size / num_files no matter how large the file being dropped really is.

With no blob files the two options must agree. GetEffectiveSizeAndLimit() (:37-55) only adds blob bytes when the blob-aware budget is on:

cpp
  *effective_size = total_sst_size;
  *effective_max  = fifo_opts.max_table_files_size;
  if (fifo_opts.max_data_files_size > 0) {
    *effective_size += total_blob_size;
    *effective_max  = fifo_opts.max_data_files_size;
  }

so with total_blob_size == 0 the picker sees identical inputs under both options and only the subtraction differs.

The same flat-average pattern appears a second time for the non-L0 path (:295-309, effective_size -= std::min(effective_size, data_per_file)), so a fix needs to touch both.

Reproduction

Self-contained C++ probe (probe_fifo.cc, attached below). Each row writes ~10 MiB, closes the DB, then reopens it at a 2 MiB budget so a single trimming decision sees the whole set. No blob files are ever created.

=== FIFO trimming: identical data, identical budget, no blob files ===

-- non-uniform: one 9 MiB oldest SST + 30 tiny ones --
  nonuniform-table         limit=2.00 MiB | 31 ssts/  9.98 MiB -> 31 ssts/  0.98 MiB | newest keys surviving: 30/30
  nonuniform-data          limit=2.00 MiB | 31 ssts/  9.98 MiB ->  7 ssts/  0.20 MiB | newest keys surviving:  6/30

-- control, uniform: the same ~10 MiB in 39 equal SSTs --
  uniform-table            limit=2.00 MiB | 39 ssts/  9.80 MiB ->  8 ssts/  1.76 MiB | newest keys surviving:  7/30
  uniform-data             limit=2.00 MiB | 39 ssts/  9.80 MiB ->  8 ssts/  1.76 MiB | newest keys surviving:  7/30

probe_fifo.cc file

cpp
// BUG-191 probe.
//
// FIFO size trimming has two budget options. max_table_files_size subtracts
// each candidate file's ACTUAL size while walking oldest-first:
//
//     remaining_size -= std::min(remaining_size, f->fd.file_size);
//
// max_data_files_size instead subtracts a UNIFORM per-file estimate:
//
//     data_per_file  = std::max(effective_size / num_files, uint64_t{1});
//     remaining_size -= std::min(remaining_size, data_per_file);
//
// (db/compaction/compaction_picker_fifo.cc, PickSizeCompaction)
//
// With no blob files total_blob_size is 0, so effective_size and effective_max
// are identical under both options and advanced_options.h documents
// max_data_files_size as simply taking "precedence over max_table_files_size
// for all FIFO compaction decisions". Any divergence is the uniform estimate
// misjudging how much space dropping a file actually frees.
#include <rocksdb/db.h>
#include <rocksdb/options.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <random>
#include <string>
#include <vector>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
using namespace rocksdb;

struct Sst { int count; uint64_t total; };
static Sst ScanSst(const std::string& dir) {
  Sst a{0, 0};
  DIR* d = opendir(dir.c_str());
  if (!d) return a;
  struct dirent* e;
  while ((e = readdir(d))) {
    if (!strstr(e->d_name, ".sst")) continue;
    struct stat st;
    if (stat((dir + "/" + e->d_name).c_str(), &st) == 0) { a.count++; a.total += st.st_size; }
  }
  closedir(d);
  return a;
}

static std::string Rand(size_t n, std::mt19937& rng) {
  std::string s(n, '\0');
  for (size_t i = 0; i < n; i++) s[i] = (char)('a' + rng() % 26);
  return s;
}

static const int kSmall = 30;

// use_data_limit: budget via max_data_files_size (uniform estimate) rather
// than max_table_files_size (actual sizes).
// uniform: make every SST the same size, so the estimate is exact.
static void Run(const char* name, bool use_data_limit, bool uniform, uint64_t limit) {
  std::string dir = std::string("/mydata/scratch/bug191_") + name;
  std::string rm = "rm -rf " + dir;
  if (system(rm.c_str()) != 0) return;

  auto mk = [&](uint64_t lim) {
    Options o;
    o.create_if_missing = true;
    o.compaction_style = kCompactionStyleFIFO;
    o.compression = kNoCompression;
    o.write_buffer_size = 256ull << 20;    // flush only when asked
    if (use_data_limit) o.compaction_options_fifo.max_data_files_size = lim;
    else                o.compaction_options_fifo.max_table_files_size = lim;
    return o;
  };

  // Phase 1: build the whole set under a budget that never trims.
  std::unique_ptr<DB> db;
  Status s = DB::Open(mk(1ull << 30), dir, &db);
  if (!s.ok()) { printf("  %-24s open: %s\n", name, s.ToString().c_str()); return; }

  std::mt19937 rng(1234);
  // Both shapes write ~10 MiB in total. Only the size DISTRIBUTION differs.
  std::vector<std::string> newest;    // the 30 most recently written keys
  if (uniform) {
    // 39 equal SSTs of ~256 KiB: the per-file estimate is exact.
    for (int i = 0; i < 9 + kSmall; i++) {
      char k[16]; snprintf(k, sizeof k, "u%03d", i);
      db->Put(WriteOptions(), k, Rand(262144, rng));
      db->Flush(FlushOptions());
      if (i >= 9) newest.emplace_back(k);
    }
  } else {
    // One 9 MiB oldest SST, then 30 tiny ones of ~32 KiB.
    for (int i = 0; i < 9; i++) {
      db->Put(WriteOptions(), "big" + std::to_string(i), Rand(1 << 20, rng));
    }
    db->Flush(FlushOptions());
    for (int i = 0; i < kSmall; i++) {
      char k[16]; snprintf(k, sizeof k, "small%03d", i);
      db->Put(WriteOptions(), k, Rand(32 << 10, rng));
      db->Flush(FlushOptions());
      newest.emplace_back(k);
    }
  }
  db.reset();
  Sst before = ScanSst(dir);

  // Phase 2: reopen at the real budget so one trimming decision sees the
  // whole set.
  s = DB::Open(mk(limit), dir, &db);
  if (!s.ok()) { printf("  %-24s reopen: %s\n", name, s.ToString().c_str()); return; }
  db->Put(WriteOptions(), "trigger", "x");
  db->Flush(FlushOptions());
  for (int i = 0; i < 60; i++) {
    uint64_t pending = 0, running = 0;
    db->GetIntProperty("rocksdb.compaction-pending", &pending);
    db->GetIntProperty("rocksdb.num-running-compactions", &running);
    if (!pending && !running) break;
    usleep(100000);
  }

  int small_left = 0;
  for (const auto& k : newest) {
    std::string v;
    if (db->Get(ReadOptions(), k, &v).ok()) small_left++;
  }
  Sst after = ScanSst(dir);
  db.reset();

  printf("ROW %s|%d|%d|%llu|%llu|%d|%llu|%d\n", name, use_data_limit ? 1 : 0,
         before.count, (unsigned long long)before.total,
         (unsigned long long)limit, after.count,
         (unsigned long long)after.total, small_left);
  printf("  %-24s limit=%4.2f MiB | %2d ssts/%6.2f MiB -> %2d ssts/%6.2f MiB "
         "| newest keys surviving: %2d/%d\n",
         name, limit / 1048576.0, before.count, before.total / 1048576.0,
         after.count, after.total / 1048576.0, small_left, kSmall);
}

int main() {
  setvbuf(stdout, nullptr, _IONBF, 0);
  const uint64_t kLimit = 2ull << 20;   // dropping the single 9 MiB file suffices
  printf("=== FIFO trimming: identical data, identical budget, no blob files ===\n\n");
  printf("-- non-uniform: one 9 MiB oldest SST + 30 tiny ones --\n");
  Run("nonuniform-table", false, false, kLimit);
  Run("nonuniform-data",  true,  false, kLimit);
  printf("\n-- control, uniform: the same ~10 MiB in 39 equal SSTs --\n");
  Run("uniform-table", false, true, kLimit);
  Run("uniform-data",  true,  true, kLimit);
  printf("\n");
  return 0;
}

Expected

Dropping the single 9 MiB oldest file brings the column family to 0.98 MiB, comfortably inside the 2 MiB budget. max_table_files_size does exactly that and keeps every recent key (30/30).

Pointed at the same byte budget over the same data with no blob files, max_data_files_size should reach the same decision — the documentation describes it as taking precedence over max_table_files_size, not as computing something different.

Actual

max_data_files_size keeps deleting: it destroys 24 more recent keys (6/30 survive instead of 30/30) and trims the column family to 0.20 MiB — 9.8 % of its own 2 MiB budget. FIFO deletion is unrecoverable, so this is permanent, silent user-data loss well past what the configured limit requires.

The uniform control pins the cause

With 39 equal-sized SSTs the average is exact and the two options agree precisely — same file count, same bytes, same survivors. The divergence appears only once file sizes are non-uniform, which is the normal state of an L0 that has seen a large write batch, a change to write_buffer_size, or any flush that retires a partly-filled memtable.

Mechanism

With one large oldest file of size B among n-1 tiny ones, data_per_file ≈ B/n. Dropping the large file frees B, but the loop credits itself only B/n, so it believes it is still roughly B over budget and carries on deleting. The number of files dropped converges on the purely count-based n · (1 − max/total) — the same answer it would give if every file were average-sized — which is why the buggy row retains ~7 of 31 files regardless of where the bytes actually live.

Suggested fix

Blob-aware sizing needs a per-file estimate of blob bytes, not a replacement for the SST size that is already known exactly. Charge each file its real SST size plus its estimated share of blob data:

cpp
    const uint64_t blob_per_file =
        (fifo_opts.max_data_files_size > 0 && num_files > 0)
            ? total_blob_size / num_files
            : 0;
    ...
    const uint64_t charged = f->fd.file_size + blob_per_file;
    remaining_size -= std::min(remaining_size, charged);

This keeps the intended blob accounting while making the no-blob case exactly equivalent to max_table_files_size, as documented. The non-L0 path at :295-309 needs the same change.

A regression test needs non-uniform SST sizes: equal sizes cannot distinguish the two formulas, as the control above shows. Asserting that max_data_files_size and max_table_files_size pick the same input set when total_blob_size == 0 would cover it directly.

Environment

RocksDB main @ 507206e46bdf876174532b8eb6b26f54f698b8dc (2026-09-21)
built with `make -j62 static_lib`, g++ 11.4.0, C++20
Ubuntu 22.04.2 LTS, Linux 5.15.0-187-generic, x86_64
compression: snappy, zlib, bzip2, lz4, zstd
also reproduced on the released 11.8.1 with identical numbers