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(credentials): ensure deletion occurs within transaction context #755

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/main/java/io/cryostat/JsonRequestFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -44,7 +44,7 @@ public class JsonRequestFilter implements ContainerRequestFilter {
"/api/v4/matchExpressions",
"/api/v4/graphql");

private final Map<String, Pattern> compiledPatterns = new HashMap<>();
private final Map<String, Pattern> compiledPatterns = new ConcurrentHashMap<>();
@Inject ObjectMapper objectMapper;

@Override
Expand Down
58 changes: 32 additions & 26 deletions src/main/java/io/cryostat/expressions/MatchExpressionEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import io.quarkus.cache.CacheResult;
import io.quarkus.cache.CompositeCacheKey;
import io.quarkus.logging.Log;
import io.quarkus.narayana.jta.QuarkusTransaction;
import io.quarkus.vertx.ConsumeEvent;
import jakarta.annotation.Nullable;
import jakarta.enterprise.context.ApplicationScoped;
Expand Down Expand Up @@ -174,34 +175,39 @@ public boolean applies(MatchExpression matchExpression, Target target) throws Sc
}

public List<Target> getMatchedTargets(MatchExpression matchExpression) {
var targets =
Target.<Target>listAll().stream()
.filter(
target -> {
try {
return applies(matchExpression, target);
} catch (ScriptException e) {
logger.error(
"Error while processing expression: "
+ matchExpression,
e);
return false;
}
})
.collect(Collectors.toList());
return QuarkusTransaction.joiningExisting()
.call(
() -> {
var targets =
Target.<Target>listAll().stream()
.filter(
target -> {
try {
return applies(matchExpression, target);
} catch (ScriptException e) {
logger.error(
"Error while processing"
+ " expression: "
+ matchExpression,
e);
return false;
}
})
.collect(Collectors.toList());

var ids = new HashSet<>();
var it = targets.iterator();
while (it.hasNext()) {
var t = it.next();
if (ids.contains(t.jvmId)) {
it.remove();
continue;
}
ids.add(t.jvmId);
}
var ids = new HashSet<>();
var it = targets.iterator();
while (it.hasNext()) {
var t = it.next();
if (ids.contains(t.jvmId)) {
it.remove();
continue;
}
ids.add(t.jvmId);
}

return targets;
return targets;
});
}

@Name("io.cryostat.rules.MatchExpressionEvaluator.MatchExpressionApplies")
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/io/cryostat/targets/TargetUpdateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;
import org.quartz.JobBuilder;
Expand Down Expand Up @@ -77,17 +78,20 @@ void onStop(@Observes ShutdownEvent evt) throws SchedulerException {
scheduler.shutdown();
}

@ConsumeEvent(Credential.CREDENTIALS_STORED)
@ConsumeEvent(value = Credential.CREDENTIALS_STORED, blocking = true)
@Transactional
void onCredentialsStored(Credential credential) {
updateTargetsForExpression(credential);
}

@ConsumeEvent(Credential.CREDENTIALS_UPDATED)
@ConsumeEvent(value = Credential.CREDENTIALS_UPDATED, blocking = true)
@Transactional
void onCredentialsUpdated(Credential credential) {
updateTargetsForExpression(credential);
}

@ConsumeEvent(Credential.CREDENTIALS_DELETED)
@ConsumeEvent(value = Credential.CREDENTIALS_DELETED, blocking = true)
@Transactional
void onCredentialsDeleted(Credential credential) {
updateTargetsForExpression(credential);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ quarkus.http.cors.access-control-allow-credentials=true
# quarkus.http.cors.methods=GET,PUT,POST,PATCH,OPTIONS
# quarkus.http.cors.access-control-max-age=1s

quarkus.log.level=ALL
quarkus.hibernate-orm.log.sql=true

quarkus.log.category."org.jboss.resteasy.reactive.common.core.AbstractResteasyReactiveContext".level=DEBUG
Expand Down
Loading