#15251·rocksdb

kTriggerCompaction is never re-registered, so time-based compaction set via SetOptions never fires on an idle DB

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

Summary

DBImpl::StartPeriodicTaskScheduler() registers PeriodicTaskType::kTriggerCompaction once, at open, with a period computed from the options in force at that moment. The period is never recomputed and the task is never re-registered. Every input to that period is mutable at runtime, so enabling ttl or periodic_compaction_seconds after open leaves the trigger running at max_compaction_trigger_wakeup_seconds, whose default is 43200 s (12 hours).

On a database that is still being written to this is invisible — a flush or compaction re-evaluates scores anyway. On a quiet database, which is exactly the case the periodic trigger exists to serve, the newly-enabled time-based compaction never runs.

Reproduction

ttl = 20s, nothing else that could wake the DB (stats_dump_period_sec = 0, stats_persist_period_sec = 0). After the initial load the DB is completely quiet: no writes, no flushes, no manual compaction. Only the periodic trigger can notice that the files have aged past the TTL. Each case waits up to 90 s.

--- the mechanism works when the option is known at open ---
  A. ttl at OPEN (control)                          ttl=20s -> FIRED after 19s   (waited 19s)

--- the same option enabled at runtime ---
  B. ttl via SetOptions AFTER open                  ttl=20s -> NEVER FIRED       (waited 91s)
  C. ttl on a CF created AFTER open                 ttl=20s -> NEVER FIRED       (waited 91s)

--- lowering the wakeup threshold AT OPEN makes it reachable again ---
  D. wakeup=2 at open, ttl via SetOptions           ttl=20s -> FIRED after 21s   (waited 21s)

--- but lowering it via SetDBOptions does not ---
  E. wakeup=1 via SetDBOptions, ttl via SetOptions  ttl=20s -> NEVER FIRED       (waited 91s)

A is the control: with the TTL known at open the period is 20/5 = 4 s and the compaction lands 19 s in, as the files become eligible.

B and C are the defect. The same TTL, applied one moment later, leaves the period at max_compaction_trigger_wakeup_seconds — 12 hours — so nothing happens. 91 s is already 4.5x the TTL.

D is the proof that the period registered at open is the only thing that matters. Setting max_compaction_trigger_wakeup_seconds = 2 at open, with no TTL configured at that point, makes the identical runtime SetOptions(ttl=20) work, firing at 21 s.

E is a second consequence of the same root cause, and is independently a documentation violation: the option's own comment says "Dynamically changeable through SetDBOptions() API", and setting it to 1 s through that API changes nothing, because the already-registered task keeps its old period.

The full probe is attached below.

Root cause

The period is computed from six mutable inputs (DBImpl::ComputeTriggerCompactionPeriod(), db/db_impl/db_impl.cc):

cpp
uint64_t period_sec = mutable_db_options_.max_compaction_trigger_wakeup_seconds;
if (mutable_db_options_.stats_dump_period_sec > 0)    { period_sec = std::min(...); }
if (mutable_db_options_.stats_persist_period_sec > 0) { period_sec = std::min(...); }
uint64_t compaction_trigger_sec = UINT64_MAX;
for (auto cfd : *versions_->GetColumnFamilySet()) {
  ...
  uint64_t cf_min = GetMinTimeBasedCompactionInterval(cfd->GetLatestCFOptions());
  if (cf_min > 0) compaction_trigger_sec = std::min(compaction_trigger_sec, cf_min);
}
period_sec = std::min(period_sec, compaction_trigger_sec / kTriggerDivisor);

GetMinTimeBasedCompactionInterval reads per-CF periodic_compaction_seconds, ttl, compaction_options_fifo.file_temperature_age_thresholds and bottommost_file_compaction_delay. All of those are settable at runtime, as are the two DB-level stats periods, and the set of column families changes too.

The task is registered exactly once (DBImpl::StartPeriodicTaskScheduler()):

cpp
s = periodic_task_scheduler_.Register(
    PeriodicTaskType::kTriggerCompaction,
    periodic_task_functions_.at(PeriodicTaskType::kTriggerCompaction),
    ComputeTriggerCompactionPeriod(), /*run_immediately=*/false);

On main today, kTriggerCompaction appears in db/db_impl/db_impl.cc only three times: once in the function map (line 288) and twice in that single registration (lines 1263–1264). There is no Unregister/Register pair for it anywhere.

This is the one place the established pattern is missing

Every other periodic task whose period can change is re-registered when it changes:

  • SetDBOptions unregisters and re-registers kDumpStats and kPersistStats when stats_dump_period_sec / stats_persist_period_sec change (db/db_impl/db_impl.cc, around lines 1892–1909).
  • RegisterRecordSeqnoTimeWorker() (line 1270) recomputes the period, then Unregisters and Registers kRecordSeqnoTime. It is called from exactly the three sites where a CF option change or a CF-set change could alter its period: DBImpl::SetOptions (line 1764), DBImpl::WrapUpCreateColumnFamilies (line 4215) and DBImpl::DropColumnFamilyImpl (line 4496).

kTriggerCompaction is called from none of them, although its period depends on the same CF options and the same CF set.

Environment

  • main at 507206e46bdf876174532b8eb6b26f54f698b8dc (2026-09-21)
  • CMake Release build, gcc 11.4.0, Ubuntu 22.04 x86-64
  • Also present in 11.8.1