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/04/11 13:34:10 UTC

(jackrabbit-oak) branch OAK-12152 created (now 0353192a43)

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

daim pushed a change to branch OAK-12152
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git


      at 0353192a43 OAK-12152 : reomved guava cache with caffeine in oak-search-elastic module

This branch includes the following new commits:

     new 0353192a43 OAK-12152 : reomved guava cache with caffeine in oak-search-elastic module

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(jackrabbit-oak) 01/01: OAK-12152 : reomved guava cache with caffeine in oak-search-elastic module

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 0353192a438c1dfc05f1757364061a04284a693b
Author: rishabhdaim <ri...@gmail.com>
AuthorDate: Sat Apr 11 19:03:54 2026 +0530

    OAK-12152 : reomved guava cache with caffeine in oak-search-elastic module
---
 .../jackrabbit/oak/cache/api/CacheBuilder.java     | 19 ++++++
 .../index/elastic/ElasticIndexStatistics.java      | 77 +++++-----------------
 .../index/elastic/ElasticIndexStatisticsTest.java  |  2 +-
 3 files changed, 38 insertions(+), 60 deletions(-)

diff --git a/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/cache/api/CacheBuilder.java b/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/cache/api/CacheBuilder.java
index 508ee68f9d..d31fabc9d0 100644
--- a/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/cache/api/CacheBuilder.java
+++ b/oak-core-spi/src/main/java/org/apache/jackrabbit/oak/cache/api/CacheBuilder.java
@@ -17,6 +17,7 @@
 package org.apache.jackrabbit.oak.cache.api;
 
 import java.time.Duration;
+import java.util.function.Supplier;
 
 import com.github.benmanes.caffeine.cache.Caffeine;
 
@@ -49,6 +50,7 @@ public final class CacheBuilder<K, V> {
     private Duration expireAfterAccess;
     private Duration expireAfterWrite;
     private Duration refreshAfterWrite;
+    private Supplier<Long> ticker;
 
     private CacheBuilder() {
     }
@@ -171,6 +173,19 @@ public final class CacheBuilder<K, V> {
         return this;
     }
 
+    /**
+     * Sets the nanosecond ticker used to measure time for expiry and refresh.
+     * Intended for testing with a controllable clock.
+     *
+     * @param ticker a supplier returning the current time in nanoseconds (must not be null)
+     * @return this builder
+     */
+    @NotNull
+    public CacheBuilder<K, V> ticker(@NotNull Supplier<Long> ticker) {
+        this.ticker = ticker;
+        return this;
+    }
+
     /**
      * Builds and returns a cache with no auto-loading behaviour.
      *
@@ -237,6 +252,10 @@ public final class CacheBuilder<K, V> {
         if (refreshAfterWrite != null) {
             caffeineBuilder = caffeineBuilder.refreshAfterWrite(refreshAfterWrite);
         }
+        if (ticker != null) {
+            Supplier<Long> t = ticker;
+            caffeineBuilder = caffeineBuilder.ticker(t::get);
+        }
         return (Caffeine<K, V>) caffeineBuilder;
     }
 
diff --git a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexStatistics.java b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexStatistics.java
index 240b22de89..bdeab1c898 100644
--- a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexStatistics.java
+++ b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexStatistics.java
@@ -18,29 +18,22 @@ package org.apache.jackrabbit.oak.plugins.index.elastic;
 
 import java.io.IOException;
 import java.time.Clock;
+import java.time.Duration;
 import java.util.List;
 import java.util.Objects;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.CompletionException;
-import java.util.concurrent.ExecutorService;
 import java.util.concurrent.TimeUnit;
 
 import co.elastic.clients.elasticsearch._types.query_dsl.Query;
 
-import org.apache.jackrabbit.guava.common.base.Ticker;
-import org.apache.jackrabbit.oak.commons.internal.concurrent.ExecutorHelper;
-import org.apache.jackrabbit.oak.commons.internal.concurrent.FutureConverter;
+import org.apache.jackrabbit.oak.cache.api.CacheBuilder;
+import org.apache.jackrabbit.oak.cache.api.CacheLoader;
+import org.apache.jackrabbit.oak.cache.api.LoadingCache;
 import org.apache.jackrabbit.oak.plugins.index.elastic.util.ElasticIndexUtils;
 import org.apache.jackrabbit.oak.plugins.index.search.IndexStatistics;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.jetbrains.annotations.TestOnly;
 
-import org.apache.jackrabbit.guava.common.cache.CacheBuilder;
-import org.apache.jackrabbit.guava.common.cache.CacheLoader;
-import org.apache.jackrabbit.guava.common.cache.LoadingCache;
-import org.apache.jackrabbit.guava.common.util.concurrent.ListenableFuture;
-
 import co.elastic.clients.elasticsearch._types.Bytes;
 import co.elastic.clients.elasticsearch.cat.indices.IndicesRecord;
 import co.elastic.clients.elasticsearch.core.CountRequest;
@@ -67,10 +60,6 @@ public class ElasticIndexStatistics implements IndexStatistics {
     private static final String REFRESH_SECONDS = "oak.elastic.statsRefreshSeconds";
     private static final Long REFRESH_SECONDS_DEFAULT = 60L;
 
-    private static final int REFRESH_POOL_SIZE = 4;
-
-    private static final ExecutorService REFRESH_EXECUTOR = ExecutorHelper.linkedQueueExecutor(
-            REFRESH_POOL_SIZE, "elastic-statistics-cache-refresh-%d");
     private final ElasticConnection elasticConnection;
     private final ElasticIndexDefinition indexDefinition;
     private final LoadingCache<StatsRequestDescriptor, Integer> countCache;
@@ -100,7 +89,7 @@ public class ElasticIndexStatistics implements IndexStatistics {
      */
     @Override
     public int numDocs() {
-        return countCache.getUnchecked(new StatsRequestDescriptor(elasticConnection, indexDefinition.getIndexAlias()));
+        return countCache.get(new StatsRequestDescriptor(elasticConnection, indexDefinition.getIndexAlias()));
     }
 
     /**
@@ -110,7 +99,7 @@ public class ElasticIndexStatistics implements IndexStatistics {
     @Override
     public int getDocCountFor(String field) {
         String elasticField = ElasticIndexUtils.fieldName(field);
-        return countCache.getUnchecked(
+        return countCache.get(
                 new StatsRequestDescriptor(elasticConnection, indexDefinition.getIndexAlias(), elasticField, null)
         );
     }
@@ -120,7 +109,7 @@ public class ElasticIndexStatistics implements IndexStatistics {
      * {@code ElasticIndexDefinition}.
      */
     public int getDocCountFor(Query query) {
-        return countCache.getUnchecked(
+        return countCache.get(
                 new StatsRequestDescriptor(elasticConnection, indexDefinition.getIndexAlias(), null, query)
         );
     }
@@ -130,7 +119,7 @@ public class ElasticIndexStatistics implements IndexStatistics {
      * {@code ElasticIndexDefinition}.
      */
     public long primaryStoreSize() {
-        return statsCache.getUnchecked(
+        return statsCache.get(
                 new StatsRequestDescriptor(elasticConnection, indexDefinition.getIndexAlias())
         ).primaryStoreSize;
     }
@@ -140,7 +129,7 @@ public class ElasticIndexStatistics implements IndexStatistics {
      * primary shards and replica shards.
      */
     public long storeSize() {
-        return statsCache.getUnchecked(
+        return statsCache.get(
                 new StatsRequestDescriptor(elasticConnection, indexDefinition.getIndexAlias())
         ).storeSize;
     }
@@ -149,7 +138,7 @@ public class ElasticIndexStatistics implements IndexStatistics {
      * Returns the creation date for the remote index bound to the {@code ElasticIndexDefinition}.
      */
     public long creationDate() {
-        return statsCache.getUnchecked(
+        return statsCache.get(
                 new StatsRequestDescriptor(elasticConnection, indexDefinition.getIndexAlias())
         ).creationDate;
     }
@@ -159,7 +148,7 @@ public class ElasticIndexStatistics implements IndexStatistics {
      * {@code ElasticIndexDefinition}. This document count includes hidden nested documents.
      */
     public int luceneNumDocs() {
-        return statsCache.getUnchecked(
+        return statsCache.get(
                 new StatsRequestDescriptor(elasticConnection, indexDefinition.getIndexAlias())
         ).luceneDocsCount;
     }
@@ -169,7 +158,7 @@ public class ElasticIndexStatistics implements IndexStatistics {
      * {@code ElasticIndexDefinition}. This document count includes hidden nested documents.
      */
     public int luceneNumDeletedDocs() {
-        return statsCache.getUnchecked(
+        return statsCache.get(
                 new StatsRequestDescriptor(elasticConnection, indexDefinition.getIndexAlias())
         ).luceneDocsDeleted;
     }
@@ -180,18 +169,12 @@ public class ElasticIndexStatistics implements IndexStatistics {
 
     static <K, V> LoadingCache<K, V> setupCache(long maxSize, long expireSeconds, long refreshSeconds,
                                                 @NotNull CacheLoader<K, V> cacheLoader, @Nullable Clock clock) {
-        CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder()
+        CacheBuilder<K, V> cacheBuilder = CacheBuilder.<K, V>newBuilder()
                 .maximumSize(maxSize)
-                .expireAfterWrite(expireSeconds, TimeUnit.SECONDS)
-                // https://github.com/google/guava/wiki/CachesExplained#refresh
-                .refreshAfterWrite(refreshSeconds, TimeUnit.SECONDS);
+                .expireAfterWrite(Duration.ofSeconds(expireSeconds))
+                .refreshAfterWrite(Duration.ofSeconds(refreshSeconds));
         if (clock != null) {
-            cacheBuilder.ticker(new Ticker() {
-                @Override
-                public long read() {
-                    return TimeUnit.MILLISECONDS.toNanos(clock.millis());
-                }
-            });
+            cacheBuilder = cacheBuilder.ticker(() -> TimeUnit.MILLISECONDS.toNanos(clock.millis()));
         }
         return cacheBuilder.build(cacheLoader);
     }
@@ -208,25 +191,13 @@ public class ElasticIndexStatistics implements IndexStatistics {
         return Long.getLong(REFRESH_SECONDS, REFRESH_SECONDS_DEFAULT);
     }
 
-    static class CountCacheLoader extends CacheLoader<StatsRequestDescriptor, Integer> {
+    static class CountCacheLoader implements CacheLoader<StatsRequestDescriptor, Integer> {
 
         @Override
         public @NotNull Integer load(@NotNull StatsRequestDescriptor countRequestDescriptor) throws IOException {
             return count(countRequestDescriptor);
         }
 
-        @Override
-        public @NotNull ListenableFuture<Integer> reload(@NotNull StatsRequestDescriptor crd, @NotNull Integer oldValue) {
-            CompletableFuture<Integer> task = CompletableFuture.supplyAsync(() -> {
-                try {
-                    return count(crd);
-                } catch (IOException e) {
-                    throw new CompletionException(e);
-                }
-            }, REFRESH_EXECUTOR);
-            return FutureConverter.toListenableFuture(task);
-        }
-
         private int count(StatsRequestDescriptor crd) throws IOException {
             CountRequest.Builder cBuilder = new CountRequest.Builder();
             cBuilder.index(crd.index);
@@ -241,25 +212,13 @@ public class ElasticIndexStatistics implements IndexStatistics {
         }
     }
 
-    static class StatsCacheLoader extends CacheLoader<StatsRequestDescriptor, StatsResponse> {
+    static class StatsCacheLoader implements CacheLoader<StatsRequestDescriptor, StatsResponse> {
 
         @Override
         public @NotNull StatsResponse load(@NotNull StatsRequestDescriptor countRequestDescriptor) throws IOException {
             return stats(countRequestDescriptor);
         }
 
-        @Override
-        public @NotNull ListenableFuture<StatsResponse> reload(@NotNull StatsRequestDescriptor crd, @NotNull StatsResponse oldValue) {
-            CompletableFuture<StatsResponse> task = CompletableFuture.supplyAsync(() -> {
-                try {
-                    return stats(crd);
-                } catch (IOException e) {
-                    throw new CompletionException(e);
-                }
-            }, REFRESH_EXECUTOR);
-            return FutureConverter.toListenableFuture(task);
-        }
-
         private StatsResponse stats(StatsRequestDescriptor crd) throws IOException {
             List<IndicesRecord> records = crd.connection.getClient().cat().indices(i -> i
                             .index(crd.index)
diff --git a/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexStatisticsTest.java b/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexStatisticsTest.java
index 644e88ed3c..89491e72f6 100644
--- a/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexStatisticsTest.java
+++ b/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexStatisticsTest.java
@@ -20,7 +20,7 @@ import co.elastic.clients.elasticsearch.ElasticsearchClient;
 import co.elastic.clients.elasticsearch._types.query_dsl.Query;
 import co.elastic.clients.elasticsearch.core.CountRequest;
 import co.elastic.clients.elasticsearch.core.CountResponse;
-import org.apache.jackrabbit.guava.common.cache.LoadingCache;
+import org.apache.jackrabbit.oak.cache.api.LoadingCache;
 import org.apache.jackrabbit.oak.stats.Clock;
 import org.junit.After;
 import org.junit.Before;