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 re...@apache.org on 2025/03/31 13:49:59 UTC

(jackrabbit-oak) branch OAK-11621 updated: OAK-11621: Reduce usage of Guava Ticker - oak-search-elastic

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

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


The following commit(s) were added to refs/heads/OAK-11621 by this push:
     new 5b9eb1cf2c OAK-11621: Reduce usage of Guava Ticker - oak-search-elastic
5b9eb1cf2c is described below

commit 5b9eb1cf2c6a54686eb58c4952d0c08f5abe007a
Author: Julian Reschke <re...@apache.org>
AuthorDate: Mon Mar 31 14:49:51 2025 +0100

    OAK-11621: Reduce usage of Guava Ticker - oak-search-elastic
---
 .../plugins/index/elastic/ElasticIndexStatistics.java  | 18 ++++++++++++------
 .../index/elastic/ElasticIndexStatisticsTest.java      | 12 +++++++-----
 2 files changed, 19 insertions(+), 11 deletions(-)

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 4d64d77ac5..7b9dc8f12b 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
@@ -17,6 +17,7 @@
 package org.apache.jackrabbit.oak.plugins.index.elastic;
 
 import java.io.IOException;
+import java.time.Clock;
 import java.util.List;
 import java.util.Objects;
 import java.util.concurrent.ExecutorService;
@@ -26,13 +27,13 @@ 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.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.base.Ticker;
 import org.apache.jackrabbit.guava.common.cache.CacheBuilder;
 import org.apache.jackrabbit.guava.common.cache.CacheLoader;
 import org.apache.jackrabbit.guava.common.cache.LoadingCache;
@@ -178,19 +179,24 @@ public class ElasticIndexStatistics implements IndexStatistics {
         ).luceneDocsDeleted;
     }
 
-    static LoadingCache<StatsRequestDescriptor, Integer> setupCountCache(long maxSize, long expireSeconds, long refreshSeconds, @Nullable Ticker ticker) {
-        return setupCache(maxSize, expireSeconds, refreshSeconds, new CountCacheLoader(), ticker);
+    static LoadingCache<StatsRequestDescriptor, Integer> setupCountCache(long maxSize, long expireSeconds, long refreshSeconds, @Nullable Clock clock) {
+        return setupCache(maxSize, expireSeconds, refreshSeconds, new CountCacheLoader(), clock);
     }
 
     static <K, V> LoadingCache<K, V> setupCache(long maxSize, long expireSeconds, long refreshSeconds,
-                                                @NotNull CacheLoader<K, V> cacheLoader, @Nullable Ticker ticker) {
+                                                @NotNull CacheLoader<K, V> cacheLoader, @Nullable Clock clock) {
         CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder()
                 .maximumSize(maxSize)
                 .expireAfterWrite(expireSeconds, TimeUnit.SECONDS)
                 // https://github.com/google/guava/wiki/CachesExplained#refresh
                 .refreshAfterWrite(refreshSeconds, TimeUnit.SECONDS);
-        if (ticker != null) {
-            cacheBuilder.ticker(ticker);
+        if (clock != null) {
+            cacheBuilder.ticker(new Ticker() {
+                @Override
+                public long read() {
+                    return TimeUnit.MILLISECONDS.toNanos(clock.millis());
+                }
+            });
         }
         return cacheBuilder.build(cacheLoader);
     }
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 a7b4be081a..48cf7d3369 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
@@ -22,6 +22,7 @@ import co.elastic.clients.elasticsearch.core.CountRequest;
 import co.elastic.clients.elasticsearch.core.CountResponse;
 import org.apache.jackrabbit.guava.common.base.Ticker;
 import org.apache.jackrabbit.guava.common.cache.LoadingCache;
+import org.apache.jackrabbit.oak.stats.Clock;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -30,6 +31,7 @@ import org.mockito.MockitoAnnotations;
 
 import java.io.IOException;
 import java.time.Duration;
+import java.util.concurrent.TimeUnit;
 
 import static org.apache.jackrabbit.oak.plugins.index.TestUtil.assertEventually;
 import static org.junit.Assert.assertEquals;
@@ -77,9 +79,9 @@ public class ElasticIndexStatisticsTest {
 
     @Test
     public void cachedStatistics() throws Exception {
-        MutableTicker ticker = new MutableTicker();
+        Clock.Virtual clock = new Clock.Virtual();
         LoadingCache<ElasticIndexStatistics.StatsRequestDescriptor, Integer> cache =
-                ElasticIndexStatistics.setupCountCache(100, 10 * 60, 60, ticker);
+                ElasticIndexStatistics.setupCountCache(100, 10 * 60, 60, clock);
         ElasticIndexStatistics indexStatistics =
                 new ElasticIndexStatistics(elasticConnectionMock, indexDefinitionMock, cache, null);
 
@@ -102,7 +104,7 @@ public class ElasticIndexStatisticsTest {
         verifyNoMoreInteractions(elasticClientMock);
 
         // move cache time ahead of 2 minutes, cache reload time expired
-        ticker.tick(Duration.ofMinutes(2));
+        clock.waitUntil(clock.millis() + Duration.ofMinutes(2).toMillis());
         // old value is returned, read fresh data from elastic in background
         assertEquals(100, indexStatistics.numDocs());
 
@@ -121,14 +123,14 @@ public class ElasticIndexStatisticsTest {
         when(countResponse.count()).thenReturn(5000L);
 
         // move cache time ahead of 15 minutes, cache value expired
-        ticker.tick(Duration.ofMinutes(15));
+        clock.waitUntil(clock.millis() + Duration.ofMinutes(15).toMillis());
 
         // cache miss, read data from elastic
         assertEquals(5000, indexStatistics.numDocs());
         verify(elasticClientMock, times(3)).count(any(CountRequest.class));
 
         // move cache time ahead of 30 minutes, cache value expired
-        ticker.tick(Duration.ofMinutes(30));
+        clock.waitUntil(clock.millis() + Duration.ofMinutes(30).toMillis());
 
         // cache miss, read data using an elastic query
         assertEquals(5000, indexStatistics.getDocCountFor(Query.of(qf -> qf.matchAll(mf -> mf))));