From 130fbb16bf9d9b0ee0f6386268b291fbba16f39f Mon Sep 17 00:00:00 2001 From: Wenjie Guo Date: Mon, 9 Dec 2024 14:12:26 +0800 Subject: [PATCH 1/2] Replace infinispan with the lightweight caffeine --- pom.xml | 26 ++++++++----------- .../service/httprox/util/CacheProducer.java | 26 +++++++------------ .../httprox/util/ProxyResponseHelper.java | 10 +++---- 3 files changed, 26 insertions(+), 36 deletions(-) diff --git a/pom.xml b/pom.xml index 2d8e5e6..32cb492 100644 --- a/pom.xml +++ b/pom.xml @@ -118,7 +118,6 @@ io.quarkus quarkus-rest-client-mutiny - io.quarkus quarkus-vertx @@ -128,25 +127,28 @@ indy-model-core-java ${indyModelVersion} - org.keycloak keycloak-core ${keycloakVersion} + org.keycloak keycloak-adapter-core ${keycloakVersion} - - io.quarkus - quarkus-micrometer-registry-prometheus + io.opentelemetry + opentelemetry-sdk - io.quarkus - quarkus-opentelemetry-exporter-otlp + io.micrometer + micrometer-registry-prometheus + + + io.opentelemetry + opentelemetry-semconv @@ -170,14 +172,8 @@ - org.infinispan - infinispan-core-jakarta - ${infinispanVersion} - - - org.infinispan - infinispan-commons-jakarta - ${infinispanVersion} + com.github.ben-manes.caffeine + caffeine com.google.guava diff --git a/src/main/java/org/commonjava/indy/service/httprox/util/CacheProducer.java b/src/main/java/org/commonjava/indy/service/httprox/util/CacheProducer.java index 86e339e..4900434 100644 --- a/src/main/java/org/commonjava/indy/service/httprox/util/CacheProducer.java +++ b/src/main/java/org/commonjava/indy/service/httprox/util/CacheProducer.java @@ -15,44 +15,38 @@ */ package org.commonjava.indy.service.httprox.util; -import org.infinispan.Cache; -import org.infinispan.configuration.cache.ConfigurationBuilder; -import org.infinispan.manager.DefaultCacheManager; -import org.infinispan.manager.EmbeddedCacheManager; - +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; import jakarta.annotation.PostConstruct; import jakarta.enterprise.context.ApplicationScoped; + import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; @ApplicationScoped public class CacheProducer { - private EmbeddedCacheManager cacheManager; - private Map caches = new ConcurrentHashMap<>(); @PostConstruct public void start() { - startEmbeddedManager(); - } - private void startEmbeddedManager() - { - cacheManager = new DefaultCacheManager(); } public synchronized Cache getCache( String named ) { - return caches.computeIfAbsent( named, ( k ) -> buildCache(named)); + return caches.computeIfAbsent( named, ( k ) -> buildCache()); } - private Cache buildCache(String named) + private Cache buildCache() { - cacheManager.defineConfiguration( named, new ConfigurationBuilder().build() ); - return cacheManager.getCache(named, Boolean.TRUE); + Cache cache = Caffeine.newBuilder() + .expireAfterAccess(15, TimeUnit.MINUTES) + .build(); + return cache; } diff --git a/src/main/java/org/commonjava/indy/service/httprox/util/ProxyResponseHelper.java b/src/main/java/org/commonjava/indy/service/httprox/util/ProxyResponseHelper.java index 9a47d6e..d823ff5 100644 --- a/src/main/java/org/commonjava/indy/service/httprox/util/ProxyResponseHelper.java +++ b/src/main/java/org/commonjava/indy/service/httprox/util/ProxyResponseHelper.java @@ -33,7 +33,7 @@ import org.commonjava.indy.service.httprox.handler.ProxyCreationResult; import org.commonjava.indy.service.httprox.handler.ProxyRepositoryCreator; import org.commonjava.indy.service.httprox.model.TrackingKey; -import org.infinispan.Cache; +import com.github.benmanes.caffeine.cache.Cache; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -151,9 +151,9 @@ private ArtifactStore doGetArtifactStore(String trackingId, final URL url ) if ( trackingId != null ) { - if ( cache.get( groupName ) != null ) + if ( cache.getIfPresent( groupName ) != null ) { - return (ArtifactStore) cache.get( groupName ); + return (ArtifactStore) cache.getIfPresent( groupName ); } Group group = null; @@ -165,7 +165,7 @@ private ArtifactStore doGetArtifactStore(String trackingId, final URL url ) if ( response != null && response.getStatus() == HttpStatus.SC_OK ) { group = (Group)response.readEntity(ArtifactStore.class); - cache.put(groupName, group, 15, TimeUnit.MINUTES); + cache.put(groupName, group); } } catch ( WebApplicationException e ) @@ -176,7 +176,7 @@ private ArtifactStore doGetArtifactStore(String trackingId, final URL url ) url, trackingId ); ProxyCreationResult result = createRepo( trackingId, url, null ); group = result.getGroup(); - cache.put(groupName, group, 15, TimeUnit.MINUTES); + cache.put(groupName, group); return group; } else From 6c6d9e2575397eafb4504ea5aea53724496c97e9 Mon Sep 17 00:00:00 2001 From: Wenjie Guo Date: Mon, 9 Dec 2024 15:21:10 +0800 Subject: [PATCH 2/2] Set maximum size in case OOM --- .../commonjava/indy/service/httprox/util/CacheProducer.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/commonjava/indy/service/httprox/util/CacheProducer.java b/src/main/java/org/commonjava/indy/service/httprox/util/CacheProducer.java index 4900434..002782d 100644 --- a/src/main/java/org/commonjava/indy/service/httprox/util/CacheProducer.java +++ b/src/main/java/org/commonjava/indy/service/httprox/util/CacheProducer.java @@ -44,8 +44,9 @@ public synchronized Cache getCache( String named ) private Cache buildCache() { Cache cache = Caffeine.newBuilder() - .expireAfterAccess(15, TimeUnit.MINUTES) - .build(); + .maximumSize( 50 ) + .expireAfterAccess(15, TimeUnit.MINUTES) + .build(); return cache; }