Skip to content

fix(storage): keep nanosecond precision on LRU access-time bumps (#257) - #261

Merged
ovaistariq merged 1 commit into
mainfrom
fix/257-lru-access-precision
Sep 7, 2026
Merged

ovaistariq merged 1 commit into
mainfrom
fix/257-lru-access-precision

Conversation

@ovaistariq

@ovaistariq ovaistariq commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #257.

Root cause

The LRU access index sorts entries by nanosecond, and a put records its write time at that precision. A read bump, though, was queued as whole Unix seconds (time.Now().Unix()) and rebuilt on flush with time.Unix(sec, 0), i.e. at the start of its second. A key read at hh:mm:ss.8 therefore sorted before every key written at hh:mm:ss.1 … ss.7, and eviction reached it first.

The failing CI run on #260 shows exactly that order. Reads of keys 1-5 happened at 04:21:51.81-51.88; keys 10-20 were written at 04:21:51.05-51.75. Eviction removed keys 6, 7, 8, 9 (written in second :50), then the "protected" 1, 2, 3, and never touched 10-20. The bumps had been flushed (flushed batch of size 15 on the restart) but at 51.000.

PR #230 made the flush deterministic with a graceful restart. It could not fix the timestamp being flushed, so the flake survived and now fires whenever the twenty setup writes finish within the same wall-clock second as the reads.

Fix

accessUpdater carries time.Time end to end: the queued update, the time-gate LRU, and the index key built in flushBatch. A bump now sorts after everything written before the read. Storage.SetAccessTime keeps its Unix-seconds signature, so the integration harness interface is untouched.

No change to eviction, the cleaner, the index key format, or the e2e flow (the restart stays; only a comment was added).

Verification

  • New TestAccessUpdater_ReadBumpOutranksEarlierWrites pins the ordering. Run against unfixed main it fails with the bumped entry at …661000000000 and the later write at …661880483000.
  • make test-storage, make test-integration-cleaner, make test-e2e-eviction (LRU: 5/5 read keys survive; FIFO: 0/5, as required), make lint all pass locally.

🤖 Generated with Claude Code

https://claude.ai/code/session_01YCoN4Him5ZY4hZovVp37ra


Note

Medium Risk
Changes how read-driven LRU index timestamps are persisted, which directly affects eviction order under disk caps; scope is limited to the async access updater with no index format or cleaner changes.

Overview
Fixes #257 by stopping LRU read recency bumps from being truncated to whole Unix seconds, which could sort a recently read key before keys written later in the same second and evict the “protected” entries first.

accessUpdater now carries time.Time through queued updates, the time-gate LRU, and flushBatch (via keys.MakeBucketedAccessKey), so read bumps keep the same nanosecond ordering as writes. UpdateNow uses time.Now() directly; stale gating compares durations with time.Sub instead of Unix round-trips. Storage.SetAccessTime still accepts Unix seconds for tests and converts with time.Unix(accessTime, 0).

Adds TestAccessUpdater_ReadBumpOutranksEarlierWrites and adjusts existing access-updater tests; the eviction e2e script only documents why nanosecond precision matters for LRU protection.

Reviewed by Cursor Bugbot for commit fadd5dc. Bugbot is set up for automated code reviews on this repo. Configure here.

The access index orders entries by nanosecond and a put records its write
time at that precision, but a read bump was queued as whole Unix seconds
and rebuilt with time.Unix(sec, 0), i.e. at the START of its second. A key
read at hh:mm:ss.8 therefore sorted before every key written at
hh:mm:ss.1..ss.7 and was evicted ahead of them.

That is the eviction e2e "Protection semantics" flake. The five protecting
reads land in the same wall-clock second as the tail of the twenty setup
writes whenever the writes are fast, and the failing CI run shows exactly
that order: keys 6-9 evicted, then the "protected" 1-3, with 10-20
untouched. PR #230 made the flush deterministic via a graceful restart,
but could not fix the timestamp being flushed.

The updater now carries time.Time end to end (queue, time-gate LRU, index
key), so a bump sorts after everything written before the read.
SetAccessTime keeps its Unix-seconds signature for the integration
harness. A new unit test pins the ordering and fails on main.

Fixes #257

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YCoN4Him5ZY4hZovVp37ra
Signed-off-by: Ovais Tariq <[email protected]>
@greptile-apps

greptile-apps Bot commented Sep 7, 2026

Copy link
Copy Markdown

Greptile Summary

This PR fixes LRU eviction ordering by preserving full time.Time precision throughout asynchronous access-time updates.

  • Changes queued updates and the time-gating cache from Unix seconds to time.Time.
  • Writes read bumps to the access index without truncating nanoseconds.
  • Preserves the Unix-seconds interface of Storage.SetAccessTime.
  • Adds regression coverage for reads occurring after writes within the same wall-clock second.

Confidence Score: 5/5

The PR appears safe to merge and consistently preserves nanosecond ordering across the LRU access-update pipeline.

No actionable correctness, security, or repository-rule violations remain; the changed representation matches the existing access-index encoding and production callers.

Important Files Changed

Filename Overview
storage/access_updater.go Carries full-precision timestamps through queuing, gating, batching, and access-index persistence.
storage/access_updater_test.go Updates timestamp-based tests and adds coverage proving a read bump sorts after earlier writes.
storage/storage.go Adapts the existing Unix-seconds test interface to the updater’s new time.Time input.
tests/e2e/eviction_test.sh Documents the same-second precision failure addressed by the storage change.

Reviews (1): Last reviewed commit: "fix(storage): keep nanosecond precision ..." | Re-trigger Greptile

@ovaistariq
ovaistariq merged commit 84e8b67 into main Sep 7, 2026
9 checks passed
@ovaistariq
ovaistariq deleted the fix/257-lru-access-precision branch September 7, 2026 06:24
@tigrisdata-argocd-bot

Copy link
Copy Markdown

🎉 This PR is included in version 1.12.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.

Flaky E2E: eviction 'Protection semantics' (LRU) races async access-update vs eviction

3 participants