From bc6c6aa90f05b66c47a756ce503b8b6cf6c3760b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Reichardt?= Date: Fri, 19 Apr 2024 10:41:27 +0200 Subject: [PATCH] Use constant for cache eviction duration --- .../java/org/neo4j/gds/api/EphemeralResultStore.java | 6 ++++-- .../org/neo4j/gds/api/EphemeralResultStoreTest.java | 11 +++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/neo4j/gds/api/EphemeralResultStore.java b/core/src/main/java/org/neo4j/gds/api/EphemeralResultStore.java index 1826355e18..e606eac36c 100644 --- a/core/src/main/java/org/neo4j/gds/api/EphemeralResultStore.java +++ b/core/src/main/java/org/neo4j/gds/api/EphemeralResultStore.java @@ -28,10 +28,11 @@ import org.neo4j.gds.core.concurrency.ExecutorServiceUtil; import org.neo4j.gds.core.utils.ClockService; +import java.time.Duration; +import java.time.temporal.ChronoUnit; import java.util.Collection; import java.util.List; import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; import java.util.function.LongUnaryOperator; import java.util.stream.Stream; @@ -39,6 +40,7 @@ public class EphemeralResultStore implements ResultStore { private static final String NO_PROPERTY_KEY = ""; private static final List NO_PROPERTIES_LIST = List.of(NO_PROPERTY_KEY); + static final Duration CACHE_EVICTION_DURATION = Duration.of(10, ChronoUnit.MINUTES); private final Cache nodeProperties; private final Cache nodeIdsByLabel; @@ -164,7 +166,7 @@ public boolean hasRelationshipStream(String relationshipType, List prope private static Cache createCache(ScheduledExecutorService singleThreadScheduler) { return Caffeine.newBuilder() - .expireAfterAccess(10, TimeUnit.MINUTES) + .expireAfterAccess(CACHE_EVICTION_DURATION) .ticker(new ClockServiceWrappingTicker()) .executor(singleThreadScheduler) .scheduler(Scheduler.forScheduledExecutorService(singleThreadScheduler)) diff --git a/core/src/test/java/org/neo4j/gds/api/EphemeralResultStoreTest.java b/core/src/test/java/org/neo4j/gds/api/EphemeralResultStoreTest.java index 14a13762b9..c254006151 100644 --- a/core/src/test/java/org/neo4j/gds/api/EphemeralResultStoreTest.java +++ b/core/src/test/java/org/neo4j/gds/api/EphemeralResultStoreTest.java @@ -27,7 +27,6 @@ import org.neo4j.time.FakeClock; import java.util.List; -import java.util.concurrent.TimeUnit; import java.util.function.LongUnaryOperator; import java.util.stream.Stream; @@ -83,7 +82,7 @@ void shouldEvictNodePropertyEntryAfter10Minutes() throws InterruptedException { assertThat(resultStore.getNodePropertyValues(List.of("A"), "prop")).isNotNull(); - clock.forward(11, TimeUnit.MINUTES); + clock.forward(EphemeralResultStore.CACHE_EVICTION_DURATION.plusMinutes(1)); // make some room for the cache eviction thread to trigger a cleanup Thread.sleep(100); @@ -130,7 +129,7 @@ void shouldEvictNodeLabelEntryAfter10Minutes() throws InterruptedException { assertThat(resultStore.hasNodeLabel("Label")).isTrue(); - clock.forward(11, TimeUnit.MINUTES); + clock.forward(EphemeralResultStore.CACHE_EVICTION_DURATION.plusMinutes(1)); // make some room for the cache eviction thread to trigger a cleanup Thread.sleep(100); @@ -180,7 +179,7 @@ void shouldEvictGraphBasedRelationshipsWithoutPropertyAfter10Minutes() throws In assertThat(resultStore.hasRelationship("Type")).isTrue(); - clock.forward(11, TimeUnit.MINUTES); + clock.forward(EphemeralResultStore.CACHE_EVICTION_DURATION.plusMinutes(1)); // make some room for the cache eviction thread to trigger a cleanup Thread.sleep(100); @@ -231,7 +230,7 @@ void shouldEvictGraphBasedRelationshipsWithPropertyAfter10Minutes() throws Inter assertThat(resultStore.hasRelationship("Type", List.of("prop"))).isTrue(); - clock.forward(11, TimeUnit.MINUTES); + clock.forward(EphemeralResultStore.CACHE_EVICTION_DURATION.plusMinutes(1)); // make some room for the cache eviction thread to trigger a cleanup Thread.sleep(100); @@ -282,7 +281,7 @@ void shouldEvictStreamBasedRelationshipsAfter10Minutes() throws InterruptedExcep assertThat(resultStore.hasRelationshipStream("Type", List.of("foo"))).isTrue(); - clock.forward(11, TimeUnit.MINUTES); + clock.forward(EphemeralResultStore.CACHE_EVICTION_DURATION.plusMinutes(1)); // make some room for the cache eviction thread to trigger a cleanup Thread.sleep(100);