Skip to content

Fix two eviction bugs in segcache#18

Merged
brayniac merged 2 commits into
pelikan-io:mainfrom
brayniac:fix/eviction-bugs
May 23, 2026
Merged

Fix two eviction bugs in segcache#18
brayniac merged 2 commits into
pelikan-io:mainfrom
brayniac:fix/eviction-bugs

Conversation

@brayniac

Copy link
Copy Markdown
Contributor

Summary

  • compare_fifo was LIFO, not FIFO (eviction/mod.rs): the comparator called .reverse() on the timestamp sort, causing sort_by (ascending) to place the newest segment at index 0 — the slot evicted first. Removing .reverse() restores oldest-first ordering.

  • can_evict() permanently blocked short-TTL segments (segments/header.rs): the previous condition (create_at + ttl) >= (now + SEG_MATURE_TIME) is equivalent to remaining TTL ≥ 20 s. Any segment holding items with TTL < 20 s could never be explicitly evicted, causing NoFreeSegments when such a cache was full. The fix removes the maturity condition entirely — a segment is evictable when it is Active and has a next_seg (i.e. is not the current write target).

Test plan

  • cargo test -p segcache — 57 tests pass (40 unit, 2 integration_basic, 2 integration_eviction, 13 doc-tests)
  • integration_eviction::random_evicts_short_ttl_segment_when_full — fills a 2-segment cache with 10 s-TTL items, asserts the next insert evicts successfully
  • integration_eviction::fifo_evicts_oldest_segment_first — fills two generations of segments (1.1 s sleep for clocksource::coarse 1-second resolution), asserts the older generation is evicted and the newer survives
  • cargo clippy --all-targets --all-features -- -D warnings — clean

🤖 Generated with Claude Code

brayniac and others added 2 commits May 23, 2026 11:09
compare_fifo sorted by newest-first (LIFO) due to a spurious .reverse()
on the timestamp comparison. sort_by is ascending and index 0 is evicted
first, so the correct sort is ascending by age — oldest segment first.

can_evict() blocked eviction of any segment whose remaining TTL was less
than 20 s (the former SEG_MATURE_TIME constant). For items stored with
Duration::ZERO or any TTL under 20 s the cache would refuse to evict,
returning NoFreeSegments on every insert once full. The fix is to remove
the maturity condition entirely: a segment is evictable when it is Active
and has a next-segment pointer (i.e. is not the current write target).

Adds integration_eviction.rs with two new tests, one per bug. The FIFO
test sleeps 1.1 s between fills to produce distinguishable timestamps at
clocksource::coarse 1-second resolution.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@brayniac brayniac merged commit c2f43b8 into pelikan-io:main May 23, 2026
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant