Skip to content

Commit d6b5b87

Browse files
committed
Caffeine - Automatically register metrics cache impls if Micrometer is around
Fixes #30744
1 parent 33e786f commit d6b5b87

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

extensions/caffeine/deployment/src/main/java/io/quarkus/caffeine/deployment/CaffeineProcessor.java

+14
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
import io.quarkus.deployment.annotations.BuildStep;
1313
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
1414
import io.quarkus.deployment.builditem.NativeImageFeatureBuildItem;
15+
import io.quarkus.deployment.builditem.nativeimage.NativeImageSystemPropertyBuildItem;
1516
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
17+
import io.quarkus.deployment.metrics.MetricsCapabilityBuildItem;
1618
import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild;
19+
import io.quarkus.runtime.metrics.MetricsFactory;
1720

1821
public class CaffeineProcessor {
1922

@@ -49,4 +52,15 @@ void cacheLoaders(CombinedIndexBuildItem combinedIndex, BuildProducer<Reflective
4952
NativeImageFeatureBuildItem nativeImageFeature() {
5053
return new NativeImageFeatureBuildItem(CacheConstructorsFeature.class);
5154
}
55+
56+
@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
57+
NativeImageSystemPropertyBuildItem registerRecordStatsImplementationsIfMicrometerAround(
58+
MetricsCapabilityBuildItem metricsCapability) {
59+
if (!metricsCapability.metricsSupported(MetricsFactory.MICROMETER)) {
60+
return null;
61+
}
62+
63+
return new NativeImageSystemPropertyBuildItem(CacheConstructorsFeature.REGISTER_RECORD_STATS_IMPLEMENTATIONS,
64+
"true");
65+
}
5266
}

extensions/caffeine/runtime/src/main/java/io/quarkus/caffeine/runtime/graal/CacheConstructorsFeature.java

+26-4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
*/
2121
public class CacheConstructorsFeature implements Feature {
2222

23-
private final AtomicBoolean triggered = new AtomicBoolean(false);
23+
public static final String REGISTER_RECORD_STATS_IMPLEMENTATIONS = "io.quarkus.caffeine.graalvm.recordStats";
2424

2525
/**
2626
* To set this, add `-J-Dio.quarkus.caffeine.graalvm.diagnostics=true` to the native-image parameters
2727
*/
2828
private static final boolean log = Boolean.getBoolean("io.quarkus.caffeine.graalvm.diagnostics");
2929

30+
private final AtomicBoolean triggered = new AtomicBoolean(false);
31+
3032
@Override
3133
public void beforeAnalysis(BeforeAnalysisAccess access) {
3234
Class<?> caffeineCoreClazz = access.findClassByName("com.github.benmanes.caffeine.cache.Caffeine");
@@ -49,6 +51,12 @@ private void registerCaffeineReflections(DuringAnalysisAccess duringAnalysisAcce
4951
for (String className : needsHavingSimpleConstructors) {
5052
registerForReflection(className, duringAnalysisAccess);
5153
}
54+
55+
if (Boolean.getBoolean(REGISTER_RECORD_STATS_IMPLEMENTATIONS)) {
56+
for (String className : typesNeedingConstructorsRegisteredWhenRecordingStats()) {
57+
registerForReflection(className, duringAnalysisAccess);
58+
}
59+
}
5260
}
5361

5462
private void registerForReflection(
@@ -60,11 +68,13 @@ private void registerForReflection(
6068
RuntimeReflection.register(z);
6169
}
6270

71+
/**
72+
* This list is not complete, but a selection of the types we expect being most useful.
73+
* unfortunately registering all of them has been shown to have a very significant impact
74+
* on executable sizes. See https://github.com/quarkusio/quarkus/issues/12961
75+
*/
6376
public static String[] typesNeedingConstructorsRegistered() {
6477
return new String[] {
65-
//N.B. this list is not complete, but a selection of the types we expect being most useful.
66-
//unfortunately registering all of them has been shown to have a very significant impact
67-
//on executable sizes. See https://github.com/quarkusio/quarkus/issues/12961
6878
"com.github.benmanes.caffeine.cache.PDMS",
6979
"com.github.benmanes.caffeine.cache.PSA",
7080
"com.github.benmanes.caffeine.cache.PSMS",
@@ -82,4 +92,16 @@ public static String[] typesNeedingConstructorsRegistered() {
8292
};
8393
}
8494

95+
public static String[] typesNeedingConstructorsRegisteredWhenRecordingStats() {
96+
return new String[] {
97+
"com.github.benmanes.caffeine.cache.SILSMS",
98+
"com.github.benmanes.caffeine.cache.SSSA",
99+
"com.github.benmanes.caffeine.cache.SSLSA",
100+
"com.github.benmanes.caffeine.cache.SSLSMS",
101+
"com.github.benmanes.caffeine.cache.SSSMS",
102+
"com.github.benmanes.caffeine.cache.SSSMSA",
103+
"com.github.benmanes.caffeine.cache.SSSMSW",
104+
"com.github.benmanes.caffeine.cache.SSSW"
105+
};
106+
}
85107
}

integration-tests/cache/src/main/resources/application.properties

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ quarkus.hibernate-orm.sql-load-script=import.sql
55

66
# configure the caches
77
quarkus.cache.caffeine."forest".expire-after-write=10M
8+
9+
quarkus.cache.caffeine."expensiveResourceCache".expire-after-write=10M
810
quarkus.cache.caffeine."expensiveResourceCache".metrics-enabled=true
911

1012
io.quarkus.it.cache.SunriseRestClient/mp-rest/url=${test.url}

0 commit comments

Comments
 (0)