Skip to content

Commit 6fd51a1

Browse files
committed
Caffeine - Automatically register metrics cache impls if Micrometer is around
1 parent 33e786f commit 6fd51a1

File tree

3 files changed

+52
-89
lines changed

3 files changed

+52
-89
lines changed

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

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
import org.jboss.jandex.ClassInfo;
88
import org.jboss.jandex.DotName;
99

10-
import io.quarkus.caffeine.runtime.graal.CacheConstructorsFeature;
1110
import io.quarkus.deployment.annotations.BuildProducer;
1211
import io.quarkus.deployment.annotations.BuildStep;
1312
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
14-
import io.quarkus.deployment.builditem.NativeImageFeatureBuildItem;
1513
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
14+
import io.quarkus.deployment.metrics.MetricsCapabilityBuildItem;
1615
import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild;
16+
import io.quarkus.runtime.metrics.MetricsFactory;
1717

1818
public class CaffeineProcessor {
1919

@@ -26,6 +26,45 @@ public class CaffeineProcessor {
2626

2727
private static final DotName CACHE_LOADER_NAME = DotName.createSimple(CACHE_LOADER_CLASS_NAME);
2828

29+
/**
30+
* this list is not complete, but a selection of the types we expect being most useful.
31+
* unfortunately registering all of them has been shown to have a very significant impact
32+
* on executable sizes. See https://github.com/quarkusio/quarkus/issues/12961
33+
*/
34+
private static final String[] DEFAULT_CACHE_IMPLEMENTATIONS_TO_REGISTER_FOR_REFLECTION = {
35+
"com.github.benmanes.caffeine.cache.PDMS",
36+
"com.github.benmanes.caffeine.cache.PSA",
37+
"com.github.benmanes.caffeine.cache.PSMS",
38+
"com.github.benmanes.caffeine.cache.PSW",
39+
"com.github.benmanes.caffeine.cache.PSMW",
40+
"com.github.benmanes.caffeine.cache.PSWMS",
41+
"com.github.benmanes.caffeine.cache.PSWMW",
42+
"com.github.benmanes.caffeine.cache.SILMS",
43+
"com.github.benmanes.caffeine.cache.SSA",
44+
"com.github.benmanes.caffeine.cache.SSLA",
45+
"com.github.benmanes.caffeine.cache.SSLMS",
46+
"com.github.benmanes.caffeine.cache.SSMS",
47+
"com.github.benmanes.caffeine.cache.SSMSA",
48+
"com.github.benmanes.caffeine.cache.SSMSW",
49+
"com.github.benmanes.caffeine.cache.SSW"
50+
};
51+
52+
/**
53+
* When the Micrometer extension is around, we add the cache classes with metrics.
54+
* <p>
55+
* Note that we don't know if they will actually be used given the cache configuration is defined at runtime.
56+
*/
57+
private static final String[] METRICS_CACHE_IMPLEMENTATIONS_TO_REGISTER_FOR_REFLECTION = {
58+
"com.github.benmanes.caffeine.cache.SILSMS",
59+
"com.github.benmanes.caffeine.cache.SSSA",
60+
"com.github.benmanes.caffeine.cache.SSLSA",
61+
"com.github.benmanes.caffeine.cache.SSLSMS",
62+
"com.github.benmanes.caffeine.cache.SSSMS",
63+
"com.github.benmanes.caffeine.cache.SSSMSA",
64+
"com.github.benmanes.caffeine.cache.SSSMSW",
65+
"com.github.benmanes.caffeine.cache.SSSW"
66+
};
67+
2968
@BuildStep
3069
void cacheLoaders(CombinedIndexBuildItem combinedIndex, BuildProducer<ReflectiveClassBuildItem> reflectiveClasses) {
3170
final Collection<ClassInfo> implementors = combinedIndex.getIndex().getAllKnownImplementors(CACHE_LOADER_NAME);
@@ -46,7 +85,14 @@ void cacheLoaders(CombinedIndexBuildItem combinedIndex, BuildProducer<Reflective
4685
}
4786

4887
@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
49-
NativeImageFeatureBuildItem nativeImageFeature() {
50-
return new NativeImageFeatureBuildItem(CacheConstructorsFeature.class);
88+
void reflection(MetricsCapabilityBuildItem metricsCapability, BuildProducer<ReflectiveClassBuildItem> reflectiveClasses) {
89+
reflectiveClasses
90+
.produce(new ReflectiveClassBuildItem(false, false, DEFAULT_CACHE_IMPLEMENTATIONS_TO_REGISTER_FOR_REFLECTION));
91+
92+
if (metricsCapability.metricsSupported(MetricsFactory.MICROMETER)) {
93+
reflectiveClasses
94+
.produce(new ReflectiveClassBuildItem(false, false,
95+
METRICS_CACHE_IMPLEMENTATIONS_TO_REGISTER_FOR_REFLECTION));
96+
}
5197
}
5298
}

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

Lines changed: 0 additions & 85 deletions
This file was deleted.

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

Lines changed: 2 additions & 0 deletions
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)