Skip to content

fix(segment): restore size when loading a finalized segment (#224) - #226

Merged
ovaistariq merged 2 commits into
tigrisdata:mainfrom
hawkaa:fix/segment-size-on-reload
Aug 18, 2026
Merged

ovaistariq merged 2 commits into
tigrisdata:mainfrom
hawkaa:fix/segment-size-on-reload

Conversation

@hawkaa

@hawkaa hawkaa commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #224.

Problem

loadSegments builds every segment as NewSegment(path, 0, 0, 0, sm.segmentSize) and, on the finalized branch, restores only version / numEntries / dataBytes from the footer — size stays 0. GetFragmentationRatio short-circuits on a zero size:

segmentSize := seg.GetSize()
if segmentSize == 0 {
    return 0.0
}

so after any restart every previously finalized segment reports fragmentation 0.0 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.

Two side effects of the same missing field:

  • RefreshMetrics sums GetSize(), so ocache_segment_size_bytes omitted 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 have file == nil and are not in openSegments), but a foot-gun for any future writer-selection path.

Fix

FinalizeSegment truncates the file to seg.size before appending the footer, so the file is exactly size + SegmentFooterSize:

segment.size = stat.Size() - int64(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:

in-process size=524448, on-disk size=524468
after reload: GetSize()=0 numEntries=8
fragmentation ratio for a fully-dead reloaded segment = 0   (threshold 0.5)

After: GetSize()=524448, ratio = 1. Reloaded size equals the in-process size exactly.

go test ./... in storage/ — 11 packages green (go vet clean, gofmt clean). make test-storage was not usable locally (its proto prerequisite needs protoc-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 vs ocache_disk_usage_bytes 190 GiB live.
  • ocache_recompaction_runs_total +4,300 over 3 days; ocache_recompaction_segments_total / _bytes_freed_total 0.
  • Eviction is healthy and crediting the delete index (40.8 GiB / 63,228 keys in 3 h) — nothing reclaimed, because the FIFO frontier sits in segments written before the current process.
  • A pod recreated 40 min earlier reported 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.

…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-apps

greptile-apps Bot commented Aug 18, 2026

Copy link
Copy Markdown

Greptile Summary

This 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.

  • Derives the restored size from the finalized file length minus the fixed footer size.
  • Adds a restart regression test covering fragmentation of a reloaded finalized segment.

Confidence Score: 4/5

The 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

Filename Overview
storage/segment/manager.go Correctly restores finalized segment size from the on-disk layout; no blocking behavioral issue was identified.
storage/segment/reload_size_regression_test.go Covers the original zero-size regression, but should assert exact size equality to protect the full restoration invariant.

Reviews (1): Last reviewed commit: "fix(segment): restore size when loading ..." | Re-trigger Greptile

Comment on lines +78 to +80
if reloaded.GetSize() == 0 {
t.Errorf("BUG: reloaded finalized segment reports size 0 (file is %d bytes)", fi.Size())
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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.

Suggested change
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!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
@ovaistariq
ovaistariq merged commit 2cc249a into tigrisdata:main Aug 18, 2026
10 checks passed
@tigrisdata-argocd-bot

Copy link
Copy Markdown

🎉 This PR is included in version 1.11.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

storage: finalized segments reload with size 0, so no segment written by an earlier process is ever recompactable (physical disk grows unbounded)

3 participants