Skip to content

fix(cache): keep second-chance victim scans from orphaning entries - #2060

Merged
mbrandenburger merged 1 commit into
hyperledger-labs:mainfrom
mbrandenburger:fix/1985-second-chance-scan
Oct 12, 2026
Merged

mbrandenburger merged 1 commit into
hyperledger-labs:mainfrom
mbrandenburger:fix/1985-second-chance-scan

Conversation

@mbrandenburger

Copy link
Copy Markdown
Member

Fixes #1985.

secondcache's typed cache evicted max(size/3, 1) victims per scan and stored the new item in every victim's slot. With size >= 6 one item occupied several slots, the next Add overwrote a live slot, and the overwritten entry stayed in the table without a slot, so it was never evicted. Add now evicts one victim, like NewBytes, and buffer is removed. Panurus (NewTyped with sizes 50 and 5000) and the KVS, MSP and vault caches use this variant. Delete's tombstone behavior, which Panurus relies on, is unchanged.

The utils/cache second-chance policy (#1955, not yet released) keeps buffer. A wrapping scan could free the same slot twice and let the cache grow past size, so scan now skips slots it already freed. Dropping buffer as the issue suggested would also fix it, but at size 1024 a single-victim scan costs about 145 ns/op and 2 allocs per insert, against 95 ns/op and 0 allocs with buffer = size/3.

Touch no longer takes a lock: the referenced bits are atomic.Bool, and the policy relies on evictionCache's RWMutex (Push under the write lock, Touch and String under the read lock). For that, evictionCache.String now takes the read lock; it formatted the map unlocked and raced with Put. The policy constructor is unexported, so nothing drives it outside evictionCache. Panurus doesn't call it.

BenchmarkSecondChanceParallelGet, warm cache, 8 cores:

  • before: ~118 ns/op
  • after: ~68 ns/op
  • secondcache for comparison: ~67 ns/op

Tests:

  • TestSecondChanceCacheVictimScanKeepsEntriesEvictable (secondcache) and TestSecondChanceHoldsSizeUnderChurn (now reads every entry) fail on main and pass here.
  • TestSecondChanceConcurrent also calls String(), which the race detector flags on main.
  • go test -race passes for utils/cache/..., kvs, vault and msp/cache (Postgres skipped); make checks is clean.

@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Hey @mbrandenburger 👋 thanks for the PR!
I'm your friendly PR Helper Bot 🤖 and I'll be riding shotgun on this one, keeping track of your PR's status to help you get it approved and merged.

This comment updates automatically as you push changes -- think of it as your PR's live scoreboard!
Here's the latest:


PR Checks

✅ DCO Sign-off -- All commits have valid sign-offs. Nice work!


❌ GPG Signature -- Heads up! The following commits don't have a verified GPG signature:

  • 485afd9 fix(cache): keep second-chance victim scans from orphaning entries

You'll need to sign your commits with GPG (e.g. git commit -S). See the Signing Guide for a step-by-step walkthrough.


✅ Merge Conflicts -- No merge conflicts detected. Smooth sailing!


✅ Issue Link -- Linked to #1985 (assigned to you).


⏳ All checks must pass before this PR can be reviewed. You've got this!

@github-actions github-actions Bot added the status: needs revision The pull request requires changes from the author before it can be reviewed or merged label Oct 12, 2026
@mbrandenburger mbrandenburger added this to the 26Q4 milestone Oct 12, 2026

@SaidAltury-ibm SaidAltury-ibm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

secondcache's typed cache evicted max(size/3, 1) victims per scan and
stored the new item in every victim's slot. With size >= 6 one item
occupied several slots, the next Add took the "not full" branch and
overwrote a live slot, and the overwritten entry stayed in the table
without a slot, so the scan never evicted it. Add now evicts one victim,
as NewBytes and the standard CLOCK algorithm do, and the buffer field is
gone. Panurus and the KVS, MSP and vault caches use this variant.

The utils/cache second-chance policy can free buffer slots per scan. A
scan that wrapped around the ring freed the same slot twice, handing it
to two keys and letting the cache grow past size. scan now skips slots
it already freed. buffer stays: batch eviction avoids the per-insert
allocations of a single-victim scan.

Touch ran under the policy mutex on every cache hit and serialized
concurrent readers. The referenced bits are atomic and Touch takes no
lock. The policy relies on evictionCache's RWMutex for everything else:
Push runs under the write lock, Touch and String under the read lock.
evictionCache.String takes the read lock, since it formatted the map
while Put could write it. The policy constructor is unexported so it is
only driven through evictionCache. On 8 cores, parallel Get on a warm
cache drops from about 118 to 68 ns/op, level with secondcache.

Also fix a Debugf in evict that used %k with no argument.

Fixes: hyperledger-labs#1985
Signed-off-by: Marcus Brandenburger <bur@zurich.ibm.com>
@SaidAltury-ibm
SaidAltury-ibm force-pushed the fix/1985-second-chance-scan branch from d7f9a3a to 485afd9 Compare October 12, 2026 13:41
@mergify

mergify Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again.

@mbrandenburger
mbrandenburger merged commit f88c28f into hyperledger-labs:main Oct 12, 2026
@mbrandenburger
mbrandenburger deleted the fix/1985-second-chance-scan branch October 12, 2026 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: needs revision The pull request requires changes from the author before it can be reviewed or merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(secondcache): typed victim scan stores one item in several slots and pins entries that are never evicted

2 participants