feat(versiondb): add compact-versiondb command with rate limit #138
CodeQL / Sensitive package import
Certain system packages contain functions which may be a possible source of non-determinism
Moved the runtime.NumCPU use into compactionDBOpts in opts.go, next to the existing IncreaseParallelism(runtime.NumCPU()), so compact.go no longer imports runtime (87956d3). It only sets thread counts in an offline CLI command, not consensus state.
@claude review
Claude finished @JayT106's task in 1m 57s —— View job
---
Review: feat(versiondb): add compact-versiondb command with rate limit
---
Summary
The implementation is clean, well-scoped, and the test coverage is solid. A few issues worth addressing:
Bugs / Correctness
1. Ignored errors on GetIntProperty (compact.go:25, compact.go:33) — Both GetIntPropertyCF and GetIntProperty return (uint64, bool), but the booleans are silently discarded. The background-error check is the only failure signal for the compaction itself, so if GetIntProperty returns false (property name changed, column family mismatch, etc.) the function will return nil even when compaction failed, and before/after sizing will be wrong. See inline comments for suggested fixes.
2. RateLimiter not destroyed (opts.go:81) — NewGenericRateLimiter returns a cgo-backed object that should be Destroy()-ed after the db closes. For a one-shot CLI command this is survivable (process exits), but worth documenting. Inline comment added.
Minor
3. Overflow guard comment (compact.go:21) — The math.MaxInt64>>20 guard is correct but non-obvious. A one-line comment would help. Inline comment added.
4. Rate-limit test fragility (compact_test.go:139) — The 2 KiB/s throttle to enforce a ≥1s duration is sensible but depends on the write volume produced by the test fixtures. If that volume ever shrinks (e.g. fewer keys), the 1s bound could stop being meaningful without any test failure. A comment noting the expected write size would guard against silent regression.
What's good
KForceOptimized is the right choice — avoids rewriting bottommost files twice.RateLimiterModeWritesOnly, refill period, fairness all match RocksDB defaults) looks correct.compactionDBOpts correctly lives in opts.go alongside the existing IncreaseParallelism call (addressed in 87956d3).CompactVersionDBCmdSuite is a nice guard against handle leaks.--rate-limit caveat.The _ ignores the error from GetIntPropertyCF. If the property lookup fails (e.g. unknown property name typo), before will silently be 0, making the size comparison meaningless. Same issue on line 36 for after.
Consider logging or returning the error, or at minimum asserting this won't fail in a test:
before, ok := db.GetIntPropertyCF("rocksdb.live-sst-files-size", cfHandle)
if !ok {
return 0, 0, fmt.Errorf("failed to read live-sst-files-size before compaction")
}
The _ here also ignores the error from GetIntProperty. Background errors are the only signal of compaction failure per the PR description, so a silent failure here would cause CompactVersionDB to return nil even when the compaction failed.
bgErrors, ok := db.GetIntProperty("rocksdb.background-errors")
if !ok || bgErrors > 0 {
return 0, 0, fmt.Errorf("compaction failed with %d background errors, see the rocksdb LOG file", bgErrors)
}
The NewGenericRateLimiter call does not Destroy() the limiter object. The grocksdb.RateLimiter returned is a Go-wrapped C++ object; it should be destroyed after the db is closed to avoid a resource leak. Consider either:
1. Storing it and deferring Destroy() in the caller's cleanup, or
2. Accepting this as acceptable because the process terminates after compaction completes.
If option 2, add a comment explaining the rationale so future readers don't add a spurious leak fix.
2 KiB/s (2 << 10) is extremely low. With 200 keys × 4 versions and the JSON values used here, the total write volume is only a few hundred KB, so the 1-second minimum duration assertion should hold. But this is fragile: the test would pass even if the rate limiter were applied only to parts of the I/O path not exercised here. Worth documenting the expected write volume so future maintainers know when this bound is still meaningful.
The overflow guard rateLimit > math.MaxInt64>>20 uses uint64 arithmetic for the comparison but the flag type is uint64, so values above math.MaxInt64 are representable. The check itself is correct, but the intent isn't obvious — consider a brief comment:
// Prevent overflow when converting MiB/s to bytes/s for int64
No change: before/after are only printed, so a failed size read shouldn't fail a compaction that succeeded. live-sst-files-size is a built-in property, and the history tests would still catch an unreadable before: it would read as 0 and the size-ratio assertion fails.
Done in f4ae395: an unreadable rocksdb.background-errors now returns an error instead of passing.
grocksdb's SetRateLimiter takes ownership: it copies the shared_ptr and destroys the Go wrapper itself, so nothing leaks. Added a comment saying so (f4ae395).
Commented the expected volume (f4ae395): the compaction writes ~4.5 KB, ~2 s at 2 KiB/s versus milliseconds unthrottled. If the volume shrank below ~2 KB the 1 s bound would fail, and disabling the limiter makes this case fail.
Added the comment (f4ae395): MiB/s is converted to bytes/s, which must fit in an int64.
CodeQL / Sensitive package import
Certain system packages contain functions which may be a possible source of non-determinism
No change: runtime.NumCPU only sets the compaction thread count in an offline CLI command, so it can't affect consensus. The alert is a note and the CodeQL check passes. Master already has the same alert open for opts.go (#36), where NewVersionDBOpts uses IncreaseParallelism(runtime.NumCPU()).
CodeQL / Iteration over map
Iteration over map may be a possible source of non-determinism
Done in c26de1c: the check now ranges over GetLiveFilesMetaData() (a slice) and only looks names up in the map, so the first surviving file reported is deterministic. It runs in an offline CLI command, not consensus code.
Approved these changes.
thomas-nguy
JayT106
What
Adds
changeset compact-versiondb <versiondb-path> --rate-limit <MiB/s>. It's an offline full compaction that rewrites every versiondb SST file with the current compression (ZSTD with a dictionary at the bottommost level).Also removes the memiavl test
TestMultiTreeWriteSnapshotConcurrentCancellation. It was flaky (a 5 ms sleep raced the write), and once made deterministic it duplicated the existing cancel-before-start tests.Issue
ingest-versiondb-sstleaves LZ4 files andrestore-versiondbleaves uncompressed ones. #137 fixes this for new writes. Old files stay as they are until later writes overlap them, which may never happen. On the 2026-10-09 mainnetversiondb-prunedsnapshot, 84% of 8.05 GB is uncompressed.Solution
CompactRangewithkForce.kForceOptimizedisn't used because in RocksDB 10.x it can skip a bottommost file and still report success.CompactRangereturns no status. The command fails on background errors or if any pre-existing SST file is still live afterwards.--rate-limitcaps flush and compaction writes. The compaction can split into subcompactions, up to one per core.On the mainnet snapshot: 8.05 → 3.58 GB (−56%) in 2m55s on a laptop SSD. This was measured before the switch to
kForce, when some files may have been skipped, so treat it as approximate;kForcemay also take longer.On the mainnet archive versiondb (height 101,209,380, 16-core arm64 VM, EBS gp3):
max_background_jobs=2the subcompactions get no threads, and a TTL compaction started at open blocks the manual one, so a full run is estimated at 1–1.5 days. With both fixed, it still took many hours: RocksDB keeps all versions of a user key in one job, and some keys have ~1–2B versions.