From 3301825008ad971a5211371b2a96652d8fe410f3 Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Thu, 24 Oct 2024 15:06:55 -0400 Subject: [PATCH] handle updating JVM ID on credential change, null out JVM ID if update attempt fails --- .../io/cryostat/targets/TargetUpdateJob.java | 18 ++++--- .../cryostat/targets/TargetUpdateService.java | 51 +++++++++++++++---- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/src/main/java/io/cryostat/targets/TargetUpdateJob.java b/src/main/java/io/cryostat/targets/TargetUpdateJob.java index 9cf066166..cda6e64f1 100644 --- a/src/main/java/io/cryostat/targets/TargetUpdateJob.java +++ b/src/main/java/io/cryostat/targets/TargetUpdateJob.java @@ -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(); } diff --git a/src/main/java/io/cryostat/targets/TargetUpdateService.java b/src/main/java/io/cryostat/targets/TargetUpdateService.java index a91a44070..54a6ac268 100644 --- a/src/main/java/io/cryostat/targets/TargetUpdateService.java +++ b/src/main/java/io/cryostat/targets/TargetUpdateService.java @@ -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; @@ -44,6 +46,7 @@ public class TargetUpdateService { @Inject Logger logger; @Inject Scheduler scheduler; + @Inject MatchExpressionEvaluator matchExpressionEvaluator; @ConfigProperty(name = ConfigProperties.CONNECTIONS_FAILED_TIMEOUT) Duration connectionTimeout; @@ -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 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 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); + } }