`WAL_size_limit_MB` is enforced as a file count derived from the largest archived WAL, so the archive is pruned far below the limit — and wiped entirely at the boundary
Expected behavior
include/rocksdb/options.h documents WAL_size_limit_MB:
When
WAL_size_limit_MBis nonzero, archived WALs starting with the earliest will be deleted until the total size of the archive falls below this limit. All empty WALs will be deleted.
So with an 8 MB limit, roughly 8 MB of the newest archived WALs should survive, whatever the individual file sizes happen to be.
Actual behavior
WalManager::PurgeObsoleteWALFiles() never sums the archive. It divides the byte
budget by the size of the single largest archived WAL and treats the result as a
file count. With uniform WAL sizes that is a fair proxy; as soon as the sizes differ
the archive is pruned far below the limit, and once one archived WAL reaches the
limit the count becomes zero and the entire archive is deleted.
WAL_size_limit_MB = 8, WAL_ttl_seconds = 0:
=== A. uniform WAL sizes - the case the count-based rule approximates ===
100 x 128 KB, no large WAL before 100 files/ 12.5 MB (largest 0.13 MB) -> after 63 files/ 7.88 MB keep=63
=== B. one larger WAL among the small ones, sweeping its size ===
largest 2 MB (0.25x limit) before 101 files/ 14.5 MB (largest 2.00 MB) -> after 3 files/ 0.38 MB keep=3 <<<
largest 4 MB (0.50x limit) before 101 files/ 16.5 MB (largest 4.00 MB) -> after 1 files/ 0.13 MB keep=1 <<<
largest 6 MB (0.75x limit) before 101 files/ 18.5 MB (largest 6.00 MB) -> after 1 files/ 0.13 MB keep=1 <<<
largest 7 MB (0.88x limit) before 101 files/ 19.5 MB (largest 7.00 MB) -> after 1 files/ 0.13 MB keep=1 <<<
=== C. the boundary: largest WAL reaching the limit ===
largest 8 MB (1.00x limit) before 101 files/ 20.5 MB (largest 8.00 MB) -> after 0 files/ 0.00 MB keep=0 <<< ARCHIVE WIPED
largest 12 MB (1.50x limit) before 101 files/ 24.5 MB (largest 12.00 MB) -> after 0 files/ 0.00 MB keep=0 <<< ARCHIVE WIPEDSection A is the control: with uniform sizes the archive settles at 7.88 MB, just under the limit, exactly as documented. That rules out the harness and shows the feature working.
Adding a single 2 MB WAL to the same 100 small ones drops retention from 7.88 MB to 0.38 MB — 21× under-retention. At 4 MB and above only one 128 KB file survives. At 8 MB — the limit itself — the integer division yields 0 and every archived WAL is deleted, including 12.5 MB of small ones that together never approached the limit.
The consequence is a silent truncation of the replication stream.
GetUpdatesSince() keeps returning Status::OK over a stream that now starts later
than requested:
case limit archive before archive after GetUpdatesSince(1)
nonuniform-under-limit 16 MiB 31 files / 9.00 MiB -> 2 files / 0.00 MiB 2 batches from seq 39 [OK] <<<
control-ttl-only 0 MiB 31 files / 9.00 MiB -> 33 files / 9.00 MiB 40 batches from seq 1 [OK]
control-uniform 16 MiB 30 files / 0.00 MiB -> 31 files / 0.00 MiB 31 batches from seq 1 [OK]
control-over-limit 4 MiB 31 files / 9.00 MiB -> 1 files / 0.00 MiB 1 batch from seq 40 [OK]In the first row the archive total (9.00 MiB) is well under the 16 MiB limit and
29 of the 31 files are deleted anyway, because the budget was divided by the single
9 MiB WAL. GetUpdatesSince(1) then returns OK while beginning at sequence 39,
dropping sequences 1–38 with no error. The two controls that disable the size limit
or use uniform sizes retain everything and replay from sequence 1.
Non-uniform archived WAL sizes are not exotic — a large write batch, a change to
write_buffer_size, or any explicit flush that retires a partly-filled WAL produces
them.
Steps to reproduce the behavior
- Open a DB with
WAL_size_limit_MB = 8andWAL_ttl_seconds = 0. - Build an archive of ~100 small WALs (~128 KB each) plus one larger WAL. A WAL is archived when it becomes obsolete, so writing and flushing in a loop is enough.
- Reopen the DB.
purge_wal_files_last_run_starts at 0, so the purge runs immediately on open. - List
<dbname>/archiveand total the.logfiles.
Expected ~8 MB retained. Observed: 0.38 MB when the largest WAL is 2 MB, and 0 files when the largest WAL is 8 MB or more.
Version
main at 507206e46bdf876174532b8eb6b26f54f698b8dc (2026-09-21), reporting version
11.11.0, built with make static_lib. Also reproduces on released 11.8.1.
Linux 5.15, gcc 11.4.
Root cause
In db/wal_manager.cc, the walk over the archive accumulates a maximum, not a
sum:
size_t log_files_num = 0; // :179
uint64_t log_file_size = 0; // :180
...
log_file_size = std::max(log_file_size, file_size); // :226
++log_files_num; // :227and the byte budget is then converted into a file count by dividing by that maximum:
size_t const files_keep_num = static_cast<size_t>(
db_options_.WAL_size_limit_MB * 1024 * 1024 / log_file_size); // :250
if (log_files_num <= files_keep_num) {
return;
}
size_t files_del_num = log_files_num - files_keep_num; // :256files_keep_num is therefore "how many copies of the biggest archived WAL fit under
the limit". Two things follow:
- when the files are not uniform, the whole budget is divided by the largest one, so the count collapses toward 1 and nearly everything is deleted although the total never approached the limit;
- when the largest archived WAL reaches
WAL_size_limit_MB, the integer division is exactly 0,files_del_num == log_files_num, and the archive is wiped.
No accounting of the actual total exists anywhere in the function.
Why existing tests pass
WalManagerTest.WALArchivalSizeLimit creates equal-sized logs —
CreateArchiveLogs(20, 5000) — which is precisely the case in which the count-based
proxy happens to be exact. Nothing in the suite archives WALs of differing sizes.
Suggested fix
Sum the archive and delete oldest-first until the running total falls below the limit, which is what the option documents. The sizes are already being read in the same pass that currently computes the maximum, so the sum is available for free:
uint64_t log_file_total = 0;
...
log_file_total += file_size;
++log_files_num;
...
const uint64_t limit_bytes = db_options_.WAL_size_limit_MB * 1024 * 1024;
if (log_file_total <= limit_bytes) {
return;
}
// walk archived_logs oldest-first, subtracting each file's real size,
// and stop as soon as log_file_total <= limit_bytesDeleting by real size also fixes the second half of what #14181 describes ("not counting the deleted WAL file's actual size").
Severity
Silent, unreported loss of the WAL replication history.
GetUpdatesSince() / TransactionLogIterator consumers lose archived WALs with no
error and no status, and at the boundary they lose all of them. A deployment that
sets WAL_size_limit_MB to bound archive growth gets an archive that is effectively
empty, while every API involved reports success.
Source: facebook/rocksdb