Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(matchexpr): correct invalidation logic #255

Merged
merged 5 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 17 additions & 35 deletions src/main/java/io/cryostat/expressions/MatchExpressionEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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> 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 {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading