Skip to content

Commit

Permalink
fix(matchexpr): correct invalidation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Jan 22, 2024
1 parent db4f388 commit 965b3b8
Showing 1 changed file with 13 additions and 30 deletions.
43 changes: 13 additions & 30 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,10 +28,8 @@
import io.cryostat.targets.Target.Annotations;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.quarkus.cache.Cache;
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 Down Expand Up @@ -103,33 +100,19 @@ boolean load(String matchExpression, Target target) throws ScriptException {
}

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

0 comments on commit 965b3b8

Please sign in to comment.