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

Fixes #30744
  • Loading branch information
gsmet committed Feb 3, 2023
1 parent 33e786f commit 743d6d5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -49,4 +53,18 @@ void cacheLoaders(CombinedIndexBuildItem combinedIndex, BuildProducer<Reflective
NativeImageFeatureBuildItem nativeImageFeature() {
return new NativeImageFeatureBuildItem(CacheConstructorsFeature.class);
}

@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
NativeImageSystemPropertyBuildItem registerRecordStatsImplementationsIfMicrometerAround(
Optional<MetricsCapabilityBuildItem> metricsCapability) {
if (metricsCapability.isEmpty()) {
return null;
}
if (!metricsCapability.get().metricsSupported(MetricsFactory.MICROMETER)) {
return null;
}

return new NativeImageSystemPropertyBuildItem(CacheConstructorsFeature.REGISTER_RECORD_STATS_IMPLEMENTATIONS,
"true");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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(
Expand All @@ -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",
Expand All @@ -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"
};
}
}
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 743d6d5

Please sign in to comment.