Skip to content

Commit

Permalink
handle updating JVM ID on credential change, null out JVM ID if updat…
Browse files Browse the repository at this point in the history
…e attempt fails
  • Loading branch information
andrewazores committed Oct 24, 2024
1 parent 2d13dd8 commit 3301825
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
18 changes: 12 additions & 6 deletions src/main/java/io/cryostat/targets/TargetUpdateJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,18 @@ private void updateTargetTx(long id) {
}

private void updateTarget(Target target) {
target.jvmId =
connectionManager
.executeConnectedTaskUni(target, JFRConnection::getJvmIdentifier)
.map(JvmIdentifier::getHash)
.await()
.atMost(connectionTimeout);
try {
target.jvmId =
connectionManager
.executeConnectedTaskUni(target, JFRConnection::getJvmIdentifier)
.map(JvmIdentifier::getHash)
.await()
.atMost(connectionTimeout);
} catch (Exception e) {
target.jvmId = null;
target.persist();
throw e;
}
target.activeRecordings = recordingHelper.listActiveRecordings(target);
target.persist();
}
Expand Down
51 changes: 42 additions & 9 deletions src/main/java/io/cryostat/targets/TargetUpdateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Map;

import io.cryostat.ConfigProperties;
import io.cryostat.credentials.Credential;
import io.cryostat.expressions.MatchExpressionEvaluator;
import io.cryostat.targets.Target.TargetDiscovery;

import io.quarkus.runtime.ShutdownEvent;
Expand All @@ -44,6 +46,7 @@ public class TargetUpdateService {

@Inject Logger logger;
@Inject Scheduler scheduler;
@Inject MatchExpressionEvaluator matchExpressionEvaluator;

@ConfigProperty(name = ConfigProperties.CONNECTIONS_FAILED_TIMEOUT)
Duration connectionTimeout;
Expand Down Expand Up @@ -74,25 +77,55 @@ void onStop(@Observes ShutdownEvent evt) throws SchedulerException {
scheduler.shutdown();
}

@ConsumeEvent(Credential.CREDENTIALS_STORED)
void onCredentialsStored(Credential credential) {
updateTargetsForExpression(credential);
}

@ConsumeEvent(Credential.CREDENTIALS_UPDATED)
void onCredentialsUpdated(Credential credential) {
updateTargetsForExpression(credential);
}

@ConsumeEvent(Credential.CREDENTIALS_DELETED)
void onCredentialsDeleted(Credential credential) {
updateTargetsForExpression(credential);
}

private void updateTargetsForExpression(Credential credential) {
for (Target target :
matchExpressionEvaluator.getMatchedTargets(credential.matchExpression)) {
try {
fireTargetUpdate(target);
} catch (SchedulerException se) {
logger.warn(se);
}
}
}

@ConsumeEvent(value = Target.TARGET_JVM_DISCOVERY)
void onMessage(TargetDiscovery event) throws SchedulerException {
switch (event.kind()) {
case MODIFIED:
// fall-through
case FOUND:
JobDetail jobDetail = JobBuilder.newJob(TargetUpdateJob.class).build();
Map<String, Object> data = jobDetail.getJobDataMap();
data.put("targetId", event.serviceRef().id);
Trigger trigger =
TriggerBuilder.newTrigger()
.startAt(Date.from(Instant.now().plusSeconds(1)))
.usingJobData(jobDetail.getJobDataMap())
.build();
scheduler.scheduleJob(jobDetail, trigger);
fireTargetUpdate(event.serviceRef());
break;
default:
// no-op
break;
}
}

private void fireTargetUpdate(Target target) throws SchedulerException {
JobDetail jobDetail = JobBuilder.newJob(TargetUpdateJob.class).build();
Map<String, Object> data = jobDetail.getJobDataMap();
data.put("targetId", target.id);
Trigger trigger =
TriggerBuilder.newTrigger()
.startAt(Date.from(Instant.now().plusSeconds(1)))
.usingJobData(jobDetail.getJobDataMap())
.build();
scheduler.scheduleJob(jobDetail, trigger);
}
}

0 comments on commit 3301825

Please sign in to comment.