Skip to content

Commit

Permalink
refactor, don't pass around Target reference when ActiveRecording alr…
Browse files Browse the repository at this point in the history
…eady carries that relation
  • Loading branch information
andrewazores committed Aug 29, 2023
1 parent d7cf261 commit 53cfa65
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
20 changes: 10 additions & 10 deletions src/main/java/io/cryostat/recordings/RecordingHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -357,25 +357,25 @@ public List<S3Object> listArchivedRecordingObjects(String jvmId) {
.toList();
}

public String saveRecording(Target target, ActiveRecording activeRecording) throws Exception {
return saveRecording(target, activeRecording, null);
public String saveRecording(ActiveRecording recording) throws Exception {
return saveRecording(recording, null);
}

public String saveRecording(Target target, ActiveRecording activeRecording, Instant expiry)
throws Exception {
public String saveRecording(ActiveRecording recording, Instant expiry) throws Exception {
// AWS object key name guidelines advise characters to avoid (% so we should not pass url
// encoded characters)
String transformedAlias =
URLDecoder.decode(target.alias, StandardCharsets.UTF_8).replaceAll("[\\._/]+", "-");
URLDecoder.decode(recording.target.alias, StandardCharsets.UTF_8)
.replaceAll("[\\._/]+", "-");
String timestamp =
clock.now().truncatedTo(ChronoUnit.SECONDS).toString().replaceAll("[-:]+", "");
String filename =
String.format("%s_%s_%s.jfr", transformedAlias, activeRecording.name, timestamp);
String.format("%s_%s_%s.jfr", transformedAlias, recording.name, timestamp);
int mib = 1024 * 1024;
String key = String.format("%s/%s", target.jvmId, filename);
String key = String.format("%s/%s", recording.target.jvmId, filename);
String multipartId = null;
List<Pair<Integer, String>> parts = new ArrayList<>();
try (var stream = remoteRecordingStreamFactory.open(activeRecording);
try (var stream = remoteRecordingStreamFactory.open(recording);
var ch = Channels.newChannel(stream)) {
ByteBuffer buf = ByteBuffer.allocate(20 * mib);
CreateMultipartUploadRequest.Builder builder =
Expand All @@ -385,7 +385,7 @@ public String saveRecording(Target target, ActiveRecording activeRecording, Inst
.contentType(JFR_MIME)
.tagging(
createMetadataTagging(
new Metadata(activeRecording.metadata, expiry)));
new Metadata(recording.metadata, expiry)));
if (expiry != null && expiry.isAfter(Instant.now())) {
builder = builder.expires(expiry);
}
Expand Down Expand Up @@ -475,7 +475,7 @@ public String saveRecording(Target target, ActiveRecording activeRecording, Inst
new Notification(
"ActiveRecordingSaved",
new RecordingEvent(
target.connectUrl, toExternalForm(activeRecording))));
recording.target.connectUrl, toExternalForm(recording))));
}
return filename;
}
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/io/cryostat/recordings/Recordings.java
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ public String patch(@RestPath long targetId, @RestPath long remoteId, String bod
activeRecording.persist();
return null;
case "save":
return recordingHelper.saveRecording(target, activeRecording);
return recordingHelper.saveRecording(activeRecording);
default:
throw new BadRequestException(body);
}
Expand Down Expand Up @@ -582,7 +582,7 @@ void stopRecording(ActiveRecording recording, boolean archive) {
recording.state = RecordingState.STOPPED;
recording.persist();
if (archive) {
recordingHelper.saveRecording(recording.target, recording);
recordingHelper.saveRecording(recording);
}
} catch (Exception e) {
logger.error("couldn't update recording", e);
Expand Down Expand Up @@ -789,9 +789,7 @@ public Response createAndRedirectPresignedDownload(@RestPath long id) throws Exc
}
String filename =
recordingHelper.saveRecording(
recording.target,
recording,
Instant.now().plusSeconds(60)); // TODO make expiry configurable
recording, Instant.now().plusSeconds(60)); // TODO make expiry configurable
String encodedKey = recordingHelper.encodedKey(recording.target.jvmId, filename);
return Response.status(RestResponse.Status.PERMANENT_REDIRECT)
.header(
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/io/cryostat/rules/ScheduledArchiveJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void execute(JobExecutionContext ctx) throws JobExecutionException {
while (previousRecordings.size() >= rule.preservedArchives) {
pruneArchive(target, previousRecordings, previousRecordings.remove());
}
performArchival(target, recording, previousRecordings);
performArchival(recording, previousRecordings);
} catch (Exception e) {
logger.error(e);
// TODO: Handle JMX/SSL errors
Expand Down Expand Up @@ -87,10 +87,9 @@ private void initPreviousRecordings(
});
}

private void performArchival(
Target target, ActiveRecording recording, Queue<String> previousRecordings)
private void performArchival(ActiveRecording recording, Queue<String> previousRecordings)
throws Exception {
String filename = recordingHelper.saveRecording(target, recording);
String filename = recordingHelper.saveRecording(recording);
previousRecordings.add(filename);
}

Expand Down

0 comments on commit 53cfa65

Please sign in to comment.