You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/05/23 03:12:21 UTC

[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #15707: [fix][ML]Fix NPE when put value to `RangeCache`.

Demogorgon314 commented on code in PR #15707:
URL: https://github.com/apache/pulsar/pull/15707#discussion_r878992514


##########
managed-ledger/src/main/java/org/apache/bookkeeper/mledger/util/RangeCache.java:
##########
@@ -73,12 +73,11 @@ public RangeCache(Weighter<Value> weighter, TimestampExtractor<Value> timestampE
      * @return whether the entry was inserted in the cache
      */
     public boolean put(Key key, Value value) {
-        if (entries.putIfAbsent(key, value) == null) {
+        Value v = entries.computeIfAbsent(key, (k) -> {
             size.addAndGet(weighter.getSize(value));
-            return true;
-        } else {
-            return false;
-        }
+            return value;
+        });
+        return v == value;

Review Comment:
   Nice catch! But look like it changed the original behavior. See the following test. Its success in the old code but failed in this change.
   ```java
       @Test
       public void test() {
           RangeCache<Integer, RefString> cache = new RangeCache<>();
           RefString s0 = new RefString("zero");
           assertEquals(s0.refCnt(), 1);
           assertTrue(cache.put(0, s0));
           assertFalse(cache.put(0, s0));
       }
   ```



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org