diff --git a/src/main/java/io/cryostat/expressions/MatchExpressionEvaluator.java b/src/main/java/io/cryostat/expressions/MatchExpressionEvaluator.java index 4181a17c4..ac33b70e3 100644 --- a/src/main/java/io/cryostat/expressions/MatchExpressionEvaluator.java +++ b/src/main/java/io/cryostat/expressions/MatchExpressionEvaluator.java @@ -19,7 +19,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.Optional; import java.util.concurrent.CompletionException; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -29,11 +28,8 @@ import io.cryostat.targets.Target.Annotations; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import io.quarkus.cache.Cache; -import io.quarkus.cache.CacheInvalidate; import io.quarkus.cache.CacheManager; import io.quarkus.cache.CacheResult; -import io.quarkus.cache.CaffeineCache; import io.quarkus.cache.CompositeCacheKey; import io.quarkus.vertx.ConsumeEvent; import jakarta.annotation.Nullable; @@ -53,7 +49,7 @@ @ApplicationScoped public class MatchExpressionEvaluator { - private static final String CACHE_NAME = "match-expression-cache"; + private static final String CACHE_NAME = "matchexpressions"; @Inject ScriptHost scriptHost; @Inject Logger logger; @@ -68,6 +64,9 @@ void onMessage(ExpressionEvent event) { invalidate(event.expression().script); break; case UPDATED: + // expression scripts aren't meant to be updatable, but handle them by invalidating + // cached results just in case + invalidate(event.expression().script); break; default: break; @@ -100,37 +99,20 @@ boolean load(String matchExpression, Target target) throws ScriptException { return script.execute(Boolean.class, Map.of("target", SimplifiedTarget.from(target))); } - @CacheInvalidate(cacheName = CACHE_NAME) - void invalidate(String matchExpression, Target target) {} - void invalidate(String matchExpression) { - Optional cache = cacheManager.getCache(CACHE_NAME); - if (cache.isPresent()) { - var it = cache.get().as(CaffeineCache.class).keySet().iterator(); - while (it.hasNext()) { - CompositeCacheKey entry = (CompositeCacheKey) it.next(); - String matchExpressionKey = (String) entry.getKeyElements()[0]; - if (Objects.equals(matchExpression, matchExpressionKey)) { - cache.get() - .invalidate(entry) - .invoke( - () -> { - logger.debugv( - "Expression {0} invalidated in cache {1}", - matchExpression, CACHE_NAME); - }) - .subscribe() - .with( - (v) -> {}, - (e) -> { - logger.errorv( - "Error invalidating expression {0} in cache {1}:" - + " {2}", - matchExpression, CACHE_NAME, e); - }); - } - } - } + // 0-index is important here. the argument order of the load() method determines the + // composite key order + cacheManager + .getCache(CACHE_NAME) + .ifPresent( + c -> + c.invalidateIf( + k -> + Objects.equals( + (String) + ((CompositeCacheKey) k) + .getKeyElements()[0], + matchExpression))); } public boolean applies(MatchExpression matchExpression, Target target) throws ScriptException { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e0652a7c6..46b4bf36b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -12,6 +12,7 @@ cryostat.messaging.queue.size=1024 cryostat.services.reports.url= quarkus.cache.enabled=true cryostat.services.reports.memory-cache.enabled=true +quarkus.cache.caffeine.matchexpressions.maximum-size=512 quarkus.cache.caffeine.activereports.expire-after-write=10s quarkus.cache.caffeine.archivedreports.expire-after-access=10m cryostat.services.reports.storage-cache.enabled=true