Fix two eviction bugs in segcache#18
Merged
Merged
Conversation
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]>
Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
compare_fifowas LIFO, not FIFO (eviction/mod.rs): the comparator called.reverse()on the timestamp sort, causingsort_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, causingNoFreeSegmentswhen such a cache was full. The fix removes the maturity condition entirely — a segment is evictable when it isActiveand has anext_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 successfullyintegration_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 survivescargo clippy --all-targets --all-features -- -D warnings— clean🤖 Generated with Claude Code