fix: make LRU2Cache.computeIfAbsent thread-safe#16170
Open
daguimu wants to merge 1 commit intoapache:3.3from
Open
fix: make LRU2Cache.computeIfAbsent thread-safe#16170daguimu wants to merge 1 commit intoapache:3.3from
daguimu wants to merge 1 commit intoapache:3.3from
Conversation
…s entire operation LRU2Cache.computeIfAbsent() released the lock between get() and put(), breaking the atomicity that all other methods in this class maintain. This caused duplicate function computation under concurrency and could defeat the LRU-2 promotion logic. The fix holds the lock across the entire computeIfAbsent operation, inlining the LRU-2 promotion logic to avoid nested lock acquisition.
abd1304 to
7a5fa6b
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 3.3 #16170 +/- ##
============================================
- Coverage 60.81% 60.81% -0.01%
+ Complexity 11765 11750 -15
============================================
Files 1953 1953
Lines 89118 89123 +5
Branches 13444 13445 +1
============================================
+ Hits 54197 54200 +3
+ Misses 29364 29361 -3
- Partials 5557 5562 +5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Problem
LRU2Cache.computeIfAbsent()releases the lock betweenget()andput(), breaking the atomicity that all other methods in this class maintain. Every other method (get,put,remove,size,clear,containsKey,setMaxCapacity) correctly holds the lock for the entire operation, butcomputeIfAbsentdoes not:This is called from the Triple protocol hot path in
StreamUtils.lruHeaderMap.computeIfAbsent(key, k -> k.toLowerCase())for every RPC request.Impact
Under concurrency, multiple threads can:
get(key) == nullfn.apply(key)(duplicate work)put(key, value)— the double-put defeats the LRU-2 promotion logic (first put goes to pre-cache, second put unintentionally promotes to main cache)Root Cause
The method was composed from the public
get()andput()methods (which each independently acquire/release the lock) instead of operating on the underlyingLinkedHashMapdirectly under a single lock span.Fix
Hold the lock across the entire
computeIfAbsentoperation, usingsuper.get()/super.put()directly and inlining the LRU-2 promotion logic to avoid nested lock acquisition:Tests Added
testComputeIfAbsentBasic— Verifies correct LRU-2 semantics: first call goes to pre-cache, second promotes, third returns from main cache without calling the functiontestComputeIfAbsentConcurrency— 10 threads × 1000 iterations callingcomputeIfAbsentconcurrently, verifying no errors or corruptionAll 6 tests in
LRU2CacheTestpass.Impact