You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by da...@apache.org on 2026/10/06 04:40:14 UTC

(jackrabbit-oak) 03/15: OAK-12425 : preserve cache metadata ordering

This is an automated email from the ASF dual-hosted git repository.

rishabhdaim pushed a commit to branch issue/OAK-12436
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git

commit 8761e4016de732d96c6f37a5f26f53ee93991f81
Author: rishabhdaim <ri...@gmail.com>
AuthorDate: Tue Sep 29 15:07:12 2026 +0530

    OAK-12425 : preserve cache metadata ordering
    
    Co-authored-by: Copilot <22...@users.noreply.github.com>
---
 .../document/persistentCache/CacheMetadata.java    | 22 ++++++++++++++++------
 .../document/persistentCache/NodeCache.java        | 18 +++++++++++++-----
 2 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheMetadata.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheMetadata.java
index 56e7d5f1f0..60c4d17ecb 100644
--- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheMetadata.java
+++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/CacheMetadata.java
@@ -35,17 +35,15 @@ import java.util.concurrent.atomic.AtomicLong;
  * - metadata.put()
  * - cache.put()
  *
- * 3. For increment():
+ * 3. For incrementIfPresent():
  *
- * - metadata.increment()
  * - cache.get()
- * - (metadata.remove() if value doesn't exists in cache)
+ * - metadata.incrementIfPresent()
  *
- * 4. For incrementAll():
+ * 4. For bulk lookup:
  *
- * - metadata.incrementAll()
  * - cache.getAll()
- * - (metadata.removeAll() on keys that returned nulls)
+ * - metadata.incrementIfPresent() for returned values
  *
  * Preserving this order will allow to avoid leaked values in the metadata without
  * an extra synchronization between cache and metadata operations. This strategy
@@ -87,6 +85,18 @@ public class CacheMetadata<K, V> {
         getOrCreate(key, value, false).incrementCount();
     }
 
+    void incrementIfPresent(K key, V value) {
+        if (!enabled) {
+            return;
+        }
+        metadataMap.computeIfPresent(key, (k, metadata) -> {
+            if (metadata.isFor(value)) {
+                metadata.incrementCount();
+            }
+            return metadata;
+        });
+    }
+
     MetadataEntry remove(Object key) {
         if (!enabled) {
             return null;
diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/NodeCache.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/NodeCache.java
index b589931efd..f846a886d2 100644
--- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/NodeCache.java
+++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/persistentCache/NodeCache.java
@@ -186,7 +186,7 @@ class NodeCache<K extends CacheValue, V extends  CacheValue>
     public V getIfPresent(K key) {
         V value = memCache.getIfPresent(key);
         if (value != null) {
-            memCacheMetadata.increment(key, value);
+            memCacheMetadata.incrementIfPresent(key, value);
             return value;
         }
 
@@ -220,8 +220,16 @@ class NodeCache<K extends CacheValue, V extends  CacheValue>
         // Track entry load time
         TimerStats.Context ctx = stats.startLoaderTimer();
         try {
-            value = memCache.get(key, mappingFunction);
-            memCacheMetadata.increment(key, value);
+            value = memCache.get(key, k -> {
+                V loaded = mappingFunction.apply(k);
+                if (loaded != null) {
+                    memCacheMetadata.put(k, loaded);
+                }
+                return loaded;
+            });
+            if (value != null) {
+                memCacheMetadata.incrementIfPresent(key, value);
+            }
             ctx.stop();
             if (!async) {
                 write((K) key, value);
@@ -238,7 +246,7 @@ class NodeCache<K extends CacheValue, V extends  CacheValue>
     public Map<K, V> getAllPresent(
             Iterable<? extends K> keys) {
         Map<K, V> result = memCache.getAllPresent(keys);
-        result.forEach(memCacheMetadata::increment);
+            result.forEach(memCacheMetadata::incrementIfPresent);
         return result;
     }
 
@@ -337,7 +345,7 @@ class NodeCache<K extends CacheValue, V extends  CacheValue>
     public void evicted(K key, V value, EvictionCause cause) {
         if (async && Objects.nonNull(cause) && EVICTION_CAUSES.contains(cause) && value != null) {
             CacheMetadata.MetadataEntry metadata = memCacheMetadata.remove(key, value);
-            if (metadata == null && memCache.asMap().containsKey(key)) {
+            if (metadata == null) {
                 return;
             }
             boolean qualifiesToPersist = true;