Repository navigation
fix(cache): keep second-chance victim scans from orphaning entries - #2060
mbrandenburger merged 1 commit into
Conversation
|
Hey @mbrandenburger 👋 thanks for the PR! This comment updates automatically as you push changes -- think of it as your PR's live scoreboard! 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:
You'll need to sign your commits with GPG (e.g. ✅ 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! |
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>
d7f9a3a to
485afd9
Compare
|
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. |
Fixes #1985.
secondcache's typed cache evicted
max(size/3, 1)victims per scan and stored the new item in every victim's slot. Withsize >= 6one item occupied several slots, the nextAddoverwrote a live slot, and the overwritten entry stayed in the table without a slot, so it was never evicted.Addnow evicts one victim, likeNewBytes, andbufferis removed. Panurus (NewTypedwith 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/cachesecond-chance policy (#1955, not yet released) keepsbuffer. A wrapping scan could free the same slot twice and let the cache grow pastsize, soscannow skips slots it already freed. Droppingbufferas 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 withbuffer = size/3.Touchno longer takes a lock: the referenced bits areatomic.Bool, and the policy relies onevictionCache'sRWMutex(Push under the write lock, Touch and String under the read lock). For that,evictionCache.Stringnow takes the read lock; it formatted the map unlocked and raced withPut. The policy constructor is unexported, so nothing drives it outsideevictionCache. Panurus doesn't call it.BenchmarkSecondChanceParallelGet, warm cache, 8 cores:Tests:
TestSecondChanceCacheVictimScanKeepsEntriesEvictable(secondcache) andTestSecondChanceHoldsSizeUnderChurn(now reads every entry) fail onmainand pass here.TestSecondChanceConcurrentalso callsString(), which the race detector flags onmain.go test -racepasses forutils/cache/..., kvs, vault and msp/cache (Postgres skipped);make checksis clean.