From 743d6d55c6ddbd1e185b3cfd899ca82ac1d2c5eb Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Thu, 2 Feb 2023 19:01:31 +0100 Subject: [PATCH] Caffeine - Automatically register metrics cache impls if Micrometer is around Fixes #30744 --- .../deployment/CaffeineProcessor.java | 18 +++++++++++ .../graal/CacheConstructorsFeature.java | 31 ++++++++++++++++--- .../src/main/resources/application.properties | 2 ++ 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/extensions/caffeine/deployment/src/main/java/io/quarkus/caffeine/deployment/CaffeineProcessor.java b/extensions/caffeine/deployment/src/main/java/io/quarkus/caffeine/deployment/CaffeineProcessor.java index 362742fff2d4b..c58b8d285d87f 100644 --- a/extensions/caffeine/deployment/src/main/java/io/quarkus/caffeine/deployment/CaffeineProcessor.java +++ b/extensions/caffeine/deployment/src/main/java/io/quarkus/caffeine/deployment/CaffeineProcessor.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Optional; import org.jboss.jandex.ClassInfo; import org.jboss.jandex.DotName; @@ -12,8 +13,11 @@ import io.quarkus.deployment.annotations.BuildStep; import io.quarkus.deployment.builditem.CombinedIndexBuildItem; import io.quarkus.deployment.builditem.NativeImageFeatureBuildItem; +import io.quarkus.deployment.builditem.nativeimage.NativeImageSystemPropertyBuildItem; import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; +import io.quarkus.deployment.metrics.MetricsCapabilityBuildItem; import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild; +import io.quarkus.runtime.metrics.MetricsFactory; public class CaffeineProcessor { @@ -49,4 +53,18 @@ void cacheLoaders(CombinedIndexBuildItem combinedIndex, BuildProducer metricsCapability) { + if (metricsCapability.isEmpty()) { + return null; + } + if (!metricsCapability.get().metricsSupported(MetricsFactory.MICROMETER)) { + return null; + } + + return new NativeImageSystemPropertyBuildItem(CacheConstructorsFeature.REGISTER_RECORD_STATS_IMPLEMENTATIONS, + "true"); + } } diff --git a/extensions/caffeine/runtime/src/main/java/io/quarkus/caffeine/runtime/graal/CacheConstructorsFeature.java b/extensions/caffeine/runtime/src/main/java/io/quarkus/caffeine/runtime/graal/CacheConstructorsFeature.java index 599100d1aad72..8456a80860cf2 100644 --- a/extensions/caffeine/runtime/src/main/java/io/quarkus/caffeine/runtime/graal/CacheConstructorsFeature.java +++ b/extensions/caffeine/runtime/src/main/java/io/quarkus/caffeine/runtime/graal/CacheConstructorsFeature.java @@ -20,13 +20,15 @@ */ public class CacheConstructorsFeature implements Feature { - private final AtomicBoolean triggered = new AtomicBoolean(false); + public static final String REGISTER_RECORD_STATS_IMPLEMENTATIONS = "io.quarkus.caffeine.graalvm.recordStats"; /** * To set this, add `-J-Dio.quarkus.caffeine.graalvm.diagnostics=true` to the native-image parameters */ private static final boolean log = Boolean.getBoolean("io.quarkus.caffeine.graalvm.diagnostics"); + private final AtomicBoolean triggered = new AtomicBoolean(false); + @Override public void beforeAnalysis(BeforeAnalysisAccess access) { Class caffeineCoreClazz = access.findClassByName("com.github.benmanes.caffeine.cache.Caffeine"); @@ -49,6 +51,12 @@ private void registerCaffeineReflections(DuringAnalysisAccess duringAnalysisAcce for (String className : needsHavingSimpleConstructors) { registerForReflection(className, duringAnalysisAccess); } + + if (Boolean.getBoolean(REGISTER_RECORD_STATS_IMPLEMENTATIONS)) { + for (String className : typesNeedingConstructorsRegisteredWhenRecordingStats()) { + registerForReflection(className, duringAnalysisAccess); + } + } } private void registerForReflection( @@ -60,15 +68,18 @@ private void registerForReflection( RuntimeReflection.register(z); } + /** + * This list is not complete, but a selection of the types we expect being most useful. + * unfortunately registering all of them has been shown to have a very significant impact + * on executable sizes. See https://github.com/quarkusio/quarkus/issues/12961 + */ public static String[] typesNeedingConstructorsRegistered() { return new String[] { - //N.B. this list is not complete, but a selection of the types we expect being most useful. - //unfortunately registering all of them has been shown to have a very significant impact - //on executable sizes. See https://github.com/quarkusio/quarkus/issues/12961 "com.github.benmanes.caffeine.cache.PDMS", "com.github.benmanes.caffeine.cache.PSA", "com.github.benmanes.caffeine.cache.PSMS", "com.github.benmanes.caffeine.cache.PSW", + "com.github.benmanes.caffeine.cache.PSMW", "com.github.benmanes.caffeine.cache.PSWMS", "com.github.benmanes.caffeine.cache.PSWMW", "com.github.benmanes.caffeine.cache.SILMS", @@ -82,4 +93,16 @@ public static String[] typesNeedingConstructorsRegistered() { }; } + public static String[] typesNeedingConstructorsRegisteredWhenRecordingStats() { + return new String[] { + "com.github.benmanes.caffeine.cache.SILSMS", + "com.github.benmanes.caffeine.cache.SSSA", + "com.github.benmanes.caffeine.cache.SSLSA", + "com.github.benmanes.caffeine.cache.SSLSMS", + "com.github.benmanes.caffeine.cache.SSSMS", + "com.github.benmanes.caffeine.cache.SSSMSA", + "com.github.benmanes.caffeine.cache.SSSMSW", + "com.github.benmanes.caffeine.cache.SSSW" + }; + } } diff --git a/integration-tests/cache/src/main/resources/application.properties b/integration-tests/cache/src/main/resources/application.properties index 4e01418c2e2ad..b94edbb983678 100644 --- a/integration-tests/cache/src/main/resources/application.properties +++ b/integration-tests/cache/src/main/resources/application.properties @@ -5,6 +5,8 @@ quarkus.hibernate-orm.sql-load-script=import.sql # configure the caches quarkus.cache.caffeine."forest".expire-after-write=10M + +quarkus.cache.caffeine."expensiveResourceCache".expire-after-write=10M quarkus.cache.caffeine."expensiveResourceCache".metrics-enabled=true io.quarkus.it.cache.SunriseRestClient/mp-rest/url=${test.url}