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
commit b1159dcfb50f161820bb97e68a31980de55aa80d Author: Julian Reschke <[email protected]> AuthorDate: Tue Mar 25 20:05:52 2025 +0100 OAK-11621: Reduce usage of Guava Ticker - oak-search-elastic --- .../index/elastic/ElasticIndexStatistics.java | 16 ++++++++---- .../index/elastic/ElasticIndexStatisticsTest.java | 30 +++++++--------------- 2 files changed, 20 insertions(+), 26 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..a819fdd5dd 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 @@ -28,6 +28,7 @@ import co.elastic.clients.elasticsearch._types.query_dsl.Query; import org.apache.jackrabbit.oak.plugins.index.elastic.util.ElasticIndexUtils; import org.apache.jackrabbit.oak.plugins.index.search.IndexStatistics; +import org.apache.jackrabbit.oak.stats.Clock; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.TestOnly; @@ -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.getTime()); + } + }); } 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..7e6e9b4286 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,8 +20,8 @@ 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.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; @@ -77,9 +77,11 @@ public class ElasticIndexStatisticsTest { @Test public void cachedStatistics() throws Exception { - MutableTicker ticker = new MutableTicker(); + Clock clock = new Clock.Virtual(); + clock.waitUntil(System.currentTimeMillis()); + 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,8 @@ public class ElasticIndexStatisticsTest { verifyNoMoreInteractions(elasticClientMock); // move cache time ahead of 2 minutes, cache reload time expired - ticker.tick(Duration.ofMinutes(2)); + clock.waitUntil(clock.getTime() + Duration.ofMinutes(2).toMillis()); + // old value is returned, read fresh data from elastic in background assertEquals(100, indexStatistics.numDocs()); @@ -121,14 +124,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.getTime() + 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.getTime() + Duration.ofMinutes(30).toMillis()); // cache miss, read data using an elastic query assertEquals(5000, indexStatistics.getDocCountFor(Query.of(qf -> qf.matchAll(mf -> mf)))); @@ -142,19 +145,4 @@ public class ElasticIndexStatisticsTest { assertEquals(5000, indexStatistics.getDocCountFor(Query.of(qf -> qf.matchAll(mf -> mf.boost(100F))))); verify(elasticClientMock, times(5)).count(any(CountRequest.class)); } - - private static class MutableTicker extends Ticker { - - private long nanoOffset = 0; - - @Override - public long read() { - return systemTicker().read() + nanoOffset; - } - - public void tick(Duration duration) { - nanoOffset = duration.toNanos(); - } - } - }
