Skip to content

Commit

Permalink
Caffeine - Automatically register metrics cache impls if Micrometer i…
Browse files Browse the repository at this point in the history
…s around
  • Loading branch information
gsmet committed Feb 2, 2023
1 parent 33e786f commit 6fd51a1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;

import io.quarkus.caffeine.runtime.graal.CacheConstructorsFeature;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.NativeImageFeatureBuildItem;
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 {

Expand All @@ -26,6 +26,45 @@ public class CaffeineProcessor {

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

/**
* 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
*/
private static final String[] DEFAULT_CACHE_IMPLEMENTATIONS_TO_REGISTER_FOR_REFLECTION = {
"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",
"com.github.benmanes.caffeine.cache.SSA",
"com.github.benmanes.caffeine.cache.SSLA",
"com.github.benmanes.caffeine.cache.SSLMS",
"com.github.benmanes.caffeine.cache.SSMS",
"com.github.benmanes.caffeine.cache.SSMSA",
"com.github.benmanes.caffeine.cache.SSMSW",
"com.github.benmanes.caffeine.cache.SSW"
};

/**
* When the Micrometer extension is around, we add the cache classes with metrics.
* <p>
* Note that we don't know if they will actually be used given the cache configuration is defined at runtime.
*/
private static final String[] METRICS_CACHE_IMPLEMENTATIONS_TO_REGISTER_FOR_REFLECTION = {
"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"
};

@BuildStep
void cacheLoaders(CombinedIndexBuildItem combinedIndex, BuildProducer<ReflectiveClassBuildItem> reflectiveClasses) {
final Collection<ClassInfo> implementors = combinedIndex.getIndex().getAllKnownImplementors(CACHE_LOADER_NAME);
Expand All @@ -46,7 +85,14 @@ void cacheLoaders(CombinedIndexBuildItem combinedIndex, BuildProducer<Reflective
}

@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
NativeImageFeatureBuildItem nativeImageFeature() {
return new NativeImageFeatureBuildItem(CacheConstructorsFeature.class);
void reflection(MetricsCapabilityBuildItem metricsCapability, BuildProducer<ReflectiveClassBuildItem> reflectiveClasses) {
reflectiveClasses
.produce(new ReflectiveClassBuildItem(false, false, DEFAULT_CACHE_IMPLEMENTATIONS_TO_REGISTER_FOR_REFLECTION));

if (metricsCapability.metricsSupported(MetricsFactory.MICROMETER)) {
reflectiveClasses
.produce(new ReflectiveClassBuildItem(false, false,
METRICS_CACHE_IMPLEMENTATIONS_TO_REGISTER_FOR_REFLECTION));
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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}

0 comments on commit 6fd51a1

Please sign in to comment.