This is an automated email from the ASF dual-hosted git repository.
ctubbsii pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/accumulo-classloaders.git
The following commit(s) were added to refs/heads/main by this push:
new 222d740 Added DeduplicationCacheTest, fixed eviction in
DeduplicationCache (#40)
222d740 is described below
commit 222d740e53bbba770c73353301a5edf9a6668914
Author: Dave Marion <[email protected]>
AuthorDate: Wed Jan 14 16:21:13 2026 -0500
Added DeduplicationCacheTest, fixed eviction in DeduplicationCache (#40)
The two Caffeine Caches in the DeduplicationCache were not removing
the entries. The addition of the `Scheduler` to the
`expireAfterAccessStrongRefs`
cache is necessary because the cleanup may only occur if the
`expireAfterAccessStrongRefs` is modified by adding another classloader.
If this does not occur for some time, then the cleanup eviction will
not occur. This behavior is noted in the Caffeine javadoc:
"If expireAfter, expireAfterWrite, or expireAfterAccess is requested,
then entries may be evicted on each cache modification, on occasional
cache accesses, or on calls to Cache.cleanUp. A scheduler(Scheduler)
may be specified to provide prompt removal of expired entries rather
than waiting until activity triggers the periodic maintenance."
The addition of the `canonicalWeakValuesCache.cleanUp()` call in the
`anyMatch` method was required to remove the entry from the
`canonicalWeakValuesCache`. The value is null due to weakValues, but since
the entry has not been cleaned up, the call to `.asMap.keySet()` still
returns the key. It's possible that using the same Scheduler approach
may work here, but that may introduce a race condition. Another possible
approach would be to register a Cleaner as documented at
https://github.com/ben-manes/caffeine/wiki/Cleanup.
---------
Co-authored-by: Christopher Tubbs <[email protected]>
---
.../lcc/LocalCachingContextClassLoaderFactory.java | 2 +-
.../classloader/lcc/util/DeduplicationCache.java | 29 +++++++--
.../lcc/util/DeduplicationCacheTest.java | 74 ++++++++++++++++++++++
3 files changed, 100 insertions(+), 5 deletions(-)
diff --git
a/modules/local-caching-classloader/src/main/java/org/apache/accumulo/classloader/lcc/LocalCachingContextClassLoaderFactory.java
b/modules/local-caching-classloader/src/main/java/org/apache/accumulo/classloader/lcc/LocalCachingContextClassLoaderFactory.java
index 3c2ad36..8c9d473 100644
---
a/modules/local-caching-classloader/src/main/java/org/apache/accumulo/classloader/lcc/LocalCachingContextClassLoaderFactory.java
+++
b/modules/local-caching-classloader/src/main/java/org/apache/accumulo/classloader/lcc/LocalCachingContextClassLoaderFactory.java
@@ -98,7 +98,7 @@ public class LocalCachingContextClassLoaderFactory implements
ContextClassLoader
// to keep this coherent with the contextDefs, updates to this should be
done in the compute
// method of contextDefs
private final DeduplicationCache<String,URL[],URLClassLoader> classloaders =
- new DeduplicationCache<>(LccUtils::createClassLoader,
Duration.ofHours(24));
+ new DeduplicationCache<>(LccUtils::createClassLoader,
Duration.ofHours(24), null);
private final AtomicReference<LocalStore> localStore = new
AtomicReference<>();
diff --git
a/modules/local-caching-classloader/src/main/java/org/apache/accumulo/classloader/lcc/util/DeduplicationCache.java
b/modules/local-caching-classloader/src/main/java/org/apache/accumulo/classloader/lcc/util/DeduplicationCache.java
index 554eebb..3a1c6c8 100644
---
a/modules/local-caching-classloader/src/main/java/org/apache/accumulo/classloader/lcc/util/DeduplicationCache.java
+++
b/modules/local-caching-classloader/src/main/java/org/apache/accumulo/classloader/lcc/util/DeduplicationCache.java
@@ -18,13 +18,20 @@
*/
package org.apache.accumulo.classloader.lcc.util;
+import static java.util.Objects.requireNonNull;
+
import java.time.Duration;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.RemovalListener;
+import com.github.benmanes.caffeine.cache.Scheduler;
/**
* A simple de-duplication cache of weakly referenced values that retains a
strong reference for a
@@ -33,15 +40,28 @@ import com.github.benmanes.caffeine.cache.Caffeine;
*/
public class DeduplicationCache<KEY,PARAMS,VALUE> {
+ private static final Logger LOG =
LoggerFactory.getLogger(DeduplicationCache.class);
+
private final Cache<KEY,VALUE> canonicalWeakValuesCache;
private final Cache<KEY,VALUE> expireAfterAccessStrongRefs;
private final BiFunction<KEY,PARAMS,VALUE> loaderFunction;
+ private final RemovalListener<KEY,VALUE> logListener = (key, value, cause)
-> LOG
+ .trace("Entry removed due to {}. K = {}, V = {}", cause, key, value);
+
public DeduplicationCache(final BiFunction<KEY,PARAMS,VALUE> loaderFunction,
- final Duration minLifetime) {
- this.loaderFunction = loaderFunction;
- this.canonicalWeakValuesCache = Caffeine.newBuilder().weakValues().build();
- this.expireAfterAccessStrongRefs =
Caffeine.newBuilder().expireAfterAccess(minLifetime).build();
+ final Duration minLifetime, final RemovalListener<KEY,VALUE> listener) {
+ this.loaderFunction = requireNonNull(loaderFunction);
+ RemovalListener<KEY,VALUE> actualListener =
+ listener == null ? logListener : (key, value, cause) -> {
+ logListener.onRemoval(key, value, cause);
+ listener.onRemoval(key, value, cause);
+ };
+ this.canonicalWeakValuesCache = Caffeine.newBuilder().weakValues()
+
.evictionListener(actualListener).scheduler(Scheduler.systemScheduler()).build();
+ this.expireAfterAccessStrongRefs =
+ Caffeine.newBuilder().expireAfterAccess(requireNonNull(minLifetime))
+
.evictionListener(actualListener).scheduler(Scheduler.systemScheduler()).build();
}
public VALUE computeIfAbsent(final KEY key, final Supplier<PARAMS> params) {
@@ -51,6 +71,7 @@ public class DeduplicationCache<KEY,PARAMS,VALUE> {
}
public boolean anyMatch(final Predicate<KEY> keyPredicate) {
+ canonicalWeakValuesCache.cleanUp();
return
canonicalWeakValuesCache.asMap().keySet().stream().anyMatch(keyPredicate);
}
diff --git
a/modules/local-caching-classloader/src/test/java/org/apache/accumulo/classloader/lcc/util/DeduplicationCacheTest.java
b/modules/local-caching-classloader/src/test/java/org/apache/accumulo/classloader/lcc/util/DeduplicationCacheTest.java
new file mode 100644
index 0000000..723ce61
--- /dev/null
+++
b/modules/local-caching-classloader/src/test/java/org/apache/accumulo/classloader/lcc/util/DeduplicationCacheTest.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.accumulo.classloader.lcc.util;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.time.Duration;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.junit.jupiter.api.Test;
+
+import com.github.benmanes.caffeine.cache.RemovalCause;
+import com.github.benmanes.caffeine.cache.RemovalListener;
+
+public class DeduplicationCacheTest {
+
+ @Test
+ public void testCollectionNotification() throws Exception {
+
+ final AtomicBoolean listenerCalled = new AtomicBoolean(false);
+
+ final RemovalListener<String,URLClassLoader> removalListener =
+ (key, value, cause) -> listenerCalled.compareAndSet(false, cause ==
RemovalCause.COLLECTED);
+
+ final DeduplicationCache<String,URL[],URLClassLoader> cache = new
DeduplicationCache<>(
+ LccUtils::createClassLoader, Duration.ofSeconds(5), removalListener);
+
+ final URL jarAOrigLocation =
+
DeduplicationCacheTest.class.getResource("/ClassLoaderTestA/TestA.jar");
+ assertNotNull(jarAOrigLocation);
+
+ Thread t = new Thread(() -> cache.computeIfAbsent("TEST", () -> new URL[]
{jarAOrigLocation}));
+ t.start();
+ t.join();
+
+ boolean exists = cache.anyMatch("TEST"::equals);
+ assertTrue(exists);
+
+ // sleep twice as long as the access time duration in the strong reference
cache
+ Thread.sleep(10_000);
+ exists = cache.anyMatch("TEST"::equals);
+ assertTrue(exists); // This is true because it's coming from the weak
reference cache
+
+ // sleep twice as long as the access time duration in the strong reference
cache
+ Thread.sleep(10_000);
+ System.gc();
+
+ exists = cache.anyMatch("TEST"::equals);
+ assertFalse(exists);
+ assertTrue(listenerCalled.get());
+
+ }
+
+}