-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[fix][ml] Fix thread safe issue with RangeCache.put and RangeCache.clear #21302
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for fixing it!
Codecov Report
@@ Coverage Diff @@
## master #21302 +/- ##
=============================================
+ Coverage 36.91% 73.21% +36.29%
- Complexity 12284 32488 +20204
=============================================
Files 1698 1887 +189
Lines 130510 140196 +9686
Branches 14260 15437 +1177
=============================================
+ Hits 48183 102644 +54461
+ Misses 76016 29454 -46562
- Partials 6311 8098 +1787
Flags with carried forward coverage won't be shown. Click here to find out more.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch!
The new solution is also better than #15707 to avoid any issues with the entry release. In #15707, it tries to resolve the problem by getting the size before putting it into the map, because the entry release is triggered by the clear() method. The new solution can also fix the potential issue from any other cases released the entry before getting the entry size.
And the change of this line https://github.com/apache/pulsar/pull/21302/files#diff-cf9a27d8abb87f6c4fe85e6e597230e9fc102c0ca8169eed08a44218c554c5edL245 will fix the issue that the newly added items to the cache be removed without the entry release.
…ear (apache#21302) (cherry picked from commit 70d086f)
…ear (apache#21302) (cherry picked from commit 70d086f)
Fixes #21301
Fixes #10433
Motivation
There are 2 thread safety issues in RangeCache.
RangeCache.put
:pulsar/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/util/RangeCache.java
Lines 76 to 84 in 643428b
computeIfAbsent
doesn't lock the key when ConcurrentSkipListMap is used.You can see this in the source code of
ConcurrentMap
:On the other hand, ConcurrentHashMap.computeIfAbsent does lock the key and atomically call the mappingFunction. Please check the references that @michaeljmarshall shared in the issue #21301. Kudos to @michaeljmarshall for pinpointing the issue!
Another problem is in
RangeCache.clear
. There should be no call toentries.clear()
in that method.Modifications
Fix the problems in
RangeCache.put
andRangeCache.clear
.Documentation
doc
doc-required
doc-not-needed
doc-complete