lhotari opened a new pull request, #21302:
URL: https://github.com/apache/pulsar/pull/21302

   Fixes #21301 
   Fixes #10433
   
   ### Motivation
   
   There are 2 thread safety issues in RangeCache. 
   
   `RangeCache.put`:
   
   
https://github.com/apache/pulsar/blob/643428bb295ee4781e86aa2c72e5ad5d61b98870/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/util/RangeCache.java#L76-L84
   
   `computeIfAbsent` doesn't lock the key when ConcurrentSkipListMap is used. 
   You can see this in the source code of `ConcurrentMap`:
   ```
       default V computeIfAbsent(K key,
               Function<? super K, ? extends V> mappingFunction) {
           Objects.requireNonNull(mappingFunction);
           V oldValue, newValue;
           return ((oldValue = get(key)) == null
                   && (newValue = mappingFunction.apply(key)) != null
                   && (oldValue = putIfAbsent(key, newValue)) == null)
               ? newValue
               : oldValue;
       }
   ```
   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 to 
`entries.clear()` in that method. 
   
   ### Modifications
   
   Fix the problems in `RangeCache.put` and `RangeCache.clear`.
   
   
   ### Documentation
   
   <!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
   
   - [ ] `doc` <!-- Your PR contains doc changes. -->
   - [ ] `doc-required` <!-- Your PR changes impact docs and you will update 
later -->
   - [x] `doc-not-needed` <!-- Your PR changes do not impact docs -->
   - [ ] `doc-complete` <!-- Docs have been already added -->


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to