Skip to content

Commit 46511e3

Browse files
authored
Merge pull request #30826 from gsmet/cache-metrics
Caffeine - Automatically register metrics cache impls if Micrometer is around
2 parents 8cf4775 + 743d6d5 commit 46511e3

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

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

+18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.ArrayList;
44
import java.util.Collection;
55
import java.util.List;
6+
import java.util.Optional;
67

78
import org.jboss.jandex.ClassInfo;
89
import org.jboss.jandex.DotName;
@@ -12,8 +13,11 @@
1213
import io.quarkus.deployment.annotations.BuildStep;
1314
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
1415
import io.quarkus.deployment.builditem.NativeImageFeatureBuildItem;
16+
import io.quarkus.deployment.builditem.nativeimage.NativeImageSystemPropertyBuildItem;
1517
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
18+
import io.quarkus.deployment.metrics.MetricsCapabilityBuildItem;
1619
import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild;
20+
import io.quarkus.runtime.metrics.MetricsFactory;
1721

1822
public class CaffeineProcessor {
1923

@@ -49,4 +53,18 @@ void cacheLoaders(CombinedIndexBuildItem combinedIndex, BuildProducer<Reflective
4953
NativeImageFeatureBuildItem nativeImageFeature() {
5054
return new NativeImageFeatureBuildItem(CacheConstructorsFeature.class);
5155
}
56+
57+
@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
58+
NativeImageSystemPropertyBuildItem registerRecordStatsImplementationsIfMicrometerAround(
59+
Optional<MetricsCapabilityBuildItem> metricsCapability) {
60+
if (metricsCapability.isEmpty()) {
61+
return null;
62+
}
63+
if (!metricsCapability.get().metricsSupported(MetricsFactory.MICROMETER)) {
64+
return null;
65+
}
66+
67+
return new NativeImageSystemPropertyBuildItem(CacheConstructorsFeature.REGISTER_RECORD_STATS_IMPLEMENTATIONS,
68+
"true");
69+
}
5270
}

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

+27-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,15 +68,18 @@ 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",
7181
"com.github.benmanes.caffeine.cache.PSW",
82+
"com.github.benmanes.caffeine.cache.PSMW",
7283
"com.github.benmanes.caffeine.cache.PSWMS",
7384
"com.github.benmanes.caffeine.cache.PSWMW",
7485
"com.github.benmanes.caffeine.cache.SILMS",
@@ -82,4 +93,16 @@ public static String[] typesNeedingConstructorsRegistered() {
8293
};
8394
}
8495

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

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)