Skip to content

Commit

Permalink
include jvmId in responses and notifications rather than connectUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Sep 5, 2024
1 parent 70566da commit 5b6faf0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 59 deletions.
22 changes: 4 additions & 18 deletions src/main/java/io/cryostat/recordings/ActiveRecording.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
*/
package io.cryostat.recordings;

import java.net.URI;
import java.util.Objects;
import java.util.Optional;

import org.openjdk.jmc.common.unit.UnitLookup;
import org.openjdk.jmc.flightrecorder.configuration.IRecordingDescriptor;
Expand Down Expand Up @@ -215,19 +213,14 @@ public record ActiveRecordingEvent(
Objects.requireNonNull(payload);
}

public record Payload(
String target, LinkedRecordingDescriptor recording, String jvmId) {
public record Payload(LinkedRecordingDescriptor recording, String jvmId) {
public Payload {
Objects.requireNonNull(target);
Objects.requireNonNull(recording);
Objects.requireNonNull(jvmId);
}

public static Payload of(RecordingHelper helper, ActiveRecording recording) {
return new Payload(
recording.target.connectUrl.toString(),
helper.toExternalForm(recording),
recording.target.jvmId);
return new Payload(helper.toExternalForm(recording), recording.target.jvmId);
}
}
}
Expand All @@ -239,21 +232,14 @@ public record ArchivedRecordingEvent(
Objects.requireNonNull(payload);
}

// FIXME the target connectUrl URI may no longer be known if the target
// has disappeared and we are emitting an event regarding an archived recording
// originally sourced from that target, or if we are accepting a recording upload from a
// client.
// This should embed the target jvmId and optionally the database ID.
public record Payload(String target, ArchivedRecordings.ArchivedRecording recording) {
public Payload {
Objects.requireNonNull(recording);
}

public static Payload of(
URI connectUrl, ArchivedRecordings.ArchivedRecording recording) {
return new Payload(
Optional.ofNullable(connectUrl).map(URI::toString).orElse(null),
recording);
String jvmId, ArchivedRecordings.ArchivedRecording recording) {
return new Payload(jvmId, recording);
}
}
}
Expand Down
32 changes: 6 additions & 26 deletions src/main/java/io/cryostat/recordings/ArchivedRecordings.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public void agentPush(
new ArchivedRecordingEvent(
ActiveRecordings.RecordingEventCategory.ARCHIVED_DELETED,
ArchivedRecordingEvent.Payload.of(
target.map(t -> t.connectUrl).orElse(null),
target.map(t -> t.jvmId).orElseThrow(),
new ArchivedRecording(
jvmId,
recording.fileName(),
Expand Down Expand Up @@ -291,12 +291,11 @@ Map<String, Object> doUpload(FileUpload recording, Metadata metadata, String jvm
RequestBody.fromFile(recording.filePath()));
logger.trace("Upload complete");

var target = Target.getTargetByJvmId(jvmId);
var event =
new ArchivedRecordingEvent(
ActiveRecordings.RecordingEventCategory.ARCHIVED_CREATED,
ArchivedRecordingEvent.Payload.of(
target.map(t -> t.connectUrl).orElse(null),
jvmId,
new ArchivedRecording(
jvmId,
filename,
Expand Down Expand Up @@ -348,14 +347,12 @@ public Collection<ArchivedRecordingDirectory> listFsArchives() {
.getArchivedRecordingMetadata(jvmId, filename)
.orElseGet(Metadata::empty);

String connectUrl =
metadata.labels().computeIfAbsent("connectUrl", k -> jvmId);
var dir =
map.computeIfAbsent(
jvmId,
id ->
new ArchivedRecordingDirectory(
connectUrl, id, new ArrayList<>()));
id, new ArrayList<>()));
dir.recordings.add(
new ArchivedRecording(
jvmId,
Expand Down Expand Up @@ -386,14 +383,12 @@ public Collection<ArchivedRecordingDirectory> listFsArchives(@RestPath String jv
.getArchivedRecordingMetadata(jvmId, filename)
.orElseGet(Metadata::empty);

String connectUrl =
metadata.labels().computeIfAbsent("connectUrl", k -> jvmId);
var dir =
map.computeIfAbsent(
jvmId,
id ->
new ArchivedRecordingDirectory(
connectUrl, id, new ArrayList<>()));
id, new ArrayList<>()));
dir.recordings.add(
new ArchivedRecording(
jvmId,
Expand All @@ -419,19 +414,6 @@ public void deleteArchivedRecording(@RestPath String jvmId, @RestPath String fil
.getArchivedRecordingMetadata(jvmId, filename)
.orElseGet(Metadata::empty);

var connectUrl =
Target.getTargetByJvmId(jvmId)
.map(t -> t.connectUrl)
.map(c -> c.toString())
.filter(StringUtils::isNotBlank)
.orElseGet(
() ->
metadata.labels()
.computeIfAbsent(
"connectUrl", k -> "lost-" + jvmId));
logger.tracev(
"Archived recording from connectUrl \"{0}\" has metadata: {1}",
connectUrl, metadata);
logger.tracev(
"Sending S3 deletion request for {0} {1}",
bucket, recordingHelper.archivedRecordingKey(jvmId, filename));
Expand All @@ -449,7 +431,7 @@ public void deleteArchivedRecording(@RestPath String jvmId, @RestPath String fil
new ArchivedRecordingEvent(
ActiveRecordings.RecordingEventCategory.ARCHIVED_DELETED,
ArchivedRecordingEvent.Payload.of(
URI.create(connectUrl),
jvmId,
new ArchivedRecording(
jvmId,
filename,
Expand Down Expand Up @@ -568,10 +550,8 @@ public record ArchivedRecording(
}

@SuppressFBWarnings("EI_EXPOSE_REP")
public record ArchivedRecordingDirectory(
String connectUrl, String jvmId, List<ArchivedRecording> recordings) {
public record ArchivedRecordingDirectory(String jvmId, List<ArchivedRecording> recordings) {
public ArchivedRecordingDirectory {
Objects.requireNonNull(connectUrl);
Objects.requireNonNull(jvmId);
if (recordings == null) {
recordings = Collections.emptyList();
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/io/cryostat/recordings/RecordingHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
Expand Down Expand Up @@ -903,12 +902,11 @@ public ArchivedRecording archiveRecording(
accum,
now.getEpochSecond());

URI connectUrl = recording.target.connectUrl;

var event =
new ArchivedRecordingEvent(
ActiveRecordings.RecordingEventCategory.ARCHIVED_CREATED,
ArchivedRecordingEvent.Payload.of(connectUrl, archivedRecording));
ArchivedRecordingEvent.Payload.of(
recording.target.jvmId, archivedRecording));
bus.publish(event.category().category(), event.payload().recording());
bus.publish(
MessagingServer.class.getName(),
Expand Down Expand Up @@ -1059,7 +1057,7 @@ public void deleteArchivedRecording(String jvmId, String filename) {
new ArchivedRecordingEvent(
ActiveRecordings.RecordingEventCategory.ARCHIVED_DELETED,
ArchivedRecordingEvent.Payload.of(
target.map(t -> t.connectUrl).orElse(null),
target.map(t -> t.jvmId).orElseThrow(),
new ArchivedRecording(
jvmId,
filename,
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/io/cryostat/targets/Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import io.cryostat.ConfigProperties;
Expand Down Expand Up @@ -134,15 +133,6 @@ public static Optional<Target> getTargetByJvmId(String jvmId) {
return find("jvmId", jvmId).firstResultOptional();
}

public static Optional<Target> getTarget(Predicate<Target> predicate) {
List<Target> targets = listAll();
return targets.stream().filter(predicate).findFirst();
}

public static boolean deleteByConnectUrl(URI connectUrl) {
return delete("connectUrl", connectUrl) > 0;
}

public static List<Target> findByRealm(String realm) {
List<Target> targets = findAll().list();

Expand Down

0 comments on commit 5b6faf0

Please sign in to comment.