Repository navigation
fix(segment): restore size when loading a finalized segment (#224) - #226
Conversation
…ta#224) loadSegments builds every segment as NewSegment(path, 0, 0, 0, segmentSize) and, on the finalized branch, restores only version/numEntries/dataBytes from the footer — size stays 0. GetFragmentationRatio short-circuits to 0.0 on a zero-size segment, so after any restart every previously finalized segment reports no fragmentation and RecompactFragmentedSegments skips it, however dead it is: only segments created by the running process are reclaimable, and their dead bytes become unreclaimable the moment it restarts. FinalizeSegment truncates the file to seg.size before appending the footer, so stat size minus SegmentFooterSize is exactly the written-byte count. Also fixes ocache_segment_size_bytes (RefreshMetrics sums GetSize(), so it omitted every segment loaded from disk and read far below the real footprint) and Remaining(), which reported a fully written segment as entirely free. Observed on a TAG fleet (v1.17.3 / ocache v1.10.0, FIFO, 200 GiB cap): 352 GiB of segments on disk vs 190 GiB live, 0 recompactions across 4,300 scans in 3 days while eviction credited 40.8 GiB / 63,228 keys in 3 hours; physical usage grew 20-25 GiB/h/pod until ENOSPC. Signed-off-by: Håkon Åmdal <[email protected]>
Greptile SummaryThis PR restores the written-byte size of finalized segments when loading them from disk, allowing fragmentation calculations, recompaction, and segment-size metrics to continue working after restarts.
Confidence Score: 4/5The PR appears safe to merge, with only a non-blocking opportunity to strengthen the regression test's size assertion. The finalized-file layout supports subtracting the fixed footer size, and no reachable blocking failure was found; the remaining concern is that the test permits positive but inaccurate restored sizes. Files Needing Attention: storage/segment/reload_size_regression_test.go Important Files Changed
Reviews (1): Last reviewed commit: "fix(segment): restore size when loading ..." | Re-trigger Greptile |
| if reloaded.GetSize() == 0 { | ||
| t.Errorf("BUG: reloaded finalized segment reports size 0 (file is %d bytes)", fi.Size()) | ||
| } |
There was a problem hiding this comment.
Assert the exact restored size
The test only rejects a zero size, so any positive but incorrect restoration can pass while leaving segment metrics and capacity accounting inaccurate. Compare GetSize() with the captured liveBytes to enforce the exact pre-footer size invariant.
| if reloaded.GetSize() == 0 { | |
| t.Errorf("BUG: reloaded finalized segment reports size 0 (file is %d bytes)", fi.Size()) | |
| } | |
| if reloaded.GetSize() != liveBytes { | |
| t.Errorf("reloaded finalized segment reports size %d, want %d", reloaded.GetSize(), liveBytes) | |
| } |
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
Done in e45af53 — the test now pins GetSize() to the in-process liveBytes exactly, plus the restored entry count, and requires the fully-dead ratio to be exactly 1 rather than merely above threshold.
A restored size that is positive but wrong still skews every fragmentation ratio the recompactor computes, so pin size and entry count to the values the in-process segment had and the ratio of a fully dead segment to 1 (review feedback on tigrisdata#226). Signed-off-by: Håkon Åmdal <[email protected]>
|
🎉 This PR is included in version 1.11.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
Fixes #224.
Problem
loadSegmentsbuilds every segment asNewSegment(path, 0, 0, 0, sm.segmentSize)and, on the finalized branch, restores onlyversion/numEntries/dataBytesfrom the footer —sizestays 0.GetFragmentationRatioshort-circuits on a zero size:so after any restart every previously finalized segment reports fragmentation 0.0 and
RecompactFragmentedSegmentsskips it, however dead it is. Only segments created by the running process are reclaimable, and their dead bytes become unreclaimable the moment it restarts.Two side effects of the same missing field:
RefreshMetricssumsGetSize(), soocache_segment_size_bytesomitted every segment loaded from disk — it read far below the real footprint and looked like "live content", which hides the leak instead of exposing it.Remaining()(maxSupportedSize - size) reported a fully written segment as entirely free. Inert today (finalized segments havefile == niland are not inopenSegments), but a foot-gun for any future writer-selection path.Fix
FinalizeSegmenttruncates the file toseg.sizebefore appending the footer, so the file is exactlysize + SegmentFooterSize:Testing
New
TestReloadedFinalizedSegment_KeepsSizeForFragmentation(storage/segment, no RocksDB fixtures): write 8 × 64 KiB entries, finalize, close, reopen the manager on the same directory, then ask for the fragmentation ratio of a segment whose every entry is dead.Before:
After:
GetSize()=524448, ratio= 1. Reloaded size equals the in-process size exactly.go test ./...instorage/— 11 packages green (go vetclean,gofmtclean).make test-storagewas not usable locally (itsprotoprerequisite needsprotoc-gen-go), so the module tests were run directly with the same CGO flags.Production evidence
TAG v1.17.3 / ocache v1.10.0, 5 pods, 500 GiB PVCs, 200 GiB cap, FIFO,
TTL=720h:segments/352 GiB in 1,411 files vsocache_disk_usage_bytes190 GiB live.ocache_recompaction_runs_total+4,300 over 3 days;ocache_recompaction_segments_total/_bytes_freed_total0.ocache_segment_size_bytes= 5.16 GiB with 1,112 segments loaded and 277 GiB on disk.This also closes the gap that keeps #204's terminal ENOSPC reachable with #211 merged: the statfs backstop frees logical bytes only, and its premise ("evicting segment entries raises their fragmentation, which triggers the recompaction that frees them") cannot hold for a segment whose size reads 0.
Not recovered by this fix: dead bytes whose delete-index record was already lost (#218) or never written (#225) — those still need a wipe.
Note
Cursor Bugbot is generating a summary for commit 6540375. Configure here.