Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcao13 committed Aug 3, 2023
1 parent 404f8a2 commit b7974ab
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
19 changes: 10 additions & 9 deletions src/main/java/io/cryostat/recordings/RecordingHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package io.cryostat.recordings;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
Expand Down Expand Up @@ -490,19 +489,20 @@ private Tagging createMetadataTagging(Metadata metadata) {

// jfr-datasource handling
@Blocking
public Response doPost(long targetEntityId, String recordingName, URL uploadUrl)
throws Exception {
public Response doPost(long targetEntityId, long remoteId, URL uploadUrl) throws Exception {
Target target = Target.findById(targetEntityId);
Objects.requireNonNull(target, "Target from targetId not found");
ActiveRecording recording = ActiveRecording.findById(remoteId);
Objects.requireNonNull(recording, "ActiveRecording from remoteId not found");
Path recordingPath =
connectionManager.executeConnectedTask(
target,
connection -> {
return getRecordingCopyPath(
connection, target.targetId(), recordingName)
return getRecordingCopyPath(connection, target, recording.name)
.orElseThrow(
() ->
new RecordingNotFoundException(
target.targetId(), recordingName));
target.targetId(), recording.name));
});

MultipartForm form =
Expand Down Expand Up @@ -543,16 +543,17 @@ public Response doPost(long targetEntityId, String recordingName, URL uploadUrl)
}

Optional<Path> getRecordingCopyPath(
JFRConnection connection, String targetId, String recordingName) throws Exception {
JFRConnection connection, Target target, String recordingName) throws Exception {
return connection.getService().getAvailableRecordings().stream()
.filter(recording -> recording.getName().equals(recordingName))
.findFirst()
.map(
descriptor -> {
try {
Path tempFile = fs.createTempFile(null, null);
try (InputStream stream =
connection.getService().openStream(descriptor, false)) {
try (var stream =
remoteRecordingStreamFactory.open(
connection, target, descriptor)) {
fs.copy(stream, tempFile, StandardCopyOption.REPLACE_EXISTING);
}
return tempFile;
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/io/cryostat/recordings/Recordings.java
Original file line number Diff line number Diff line change
Expand Up @@ -673,21 +673,26 @@ static void safeCloseRecording(JFRConnection conn, IRecordingDescriptor rec, Log
@RolesAllowed("write")
public Response uploadToGrafanaV1(@RestPath URI connectUrl, @RestPath String recordingName) {
Target target = Target.getTargetByConnectUrl(connectUrl);
long remoteId =
target.activeRecordings.stream()
.filter(r -> Objects.equals(r.name, recordingName))
.findFirst()
.map(r -> r.remoteId)
.orElseThrow(() -> new NotFoundException());
return Response.status(RestResponse.Status.PERMANENT_REDIRECT)
.location(
URI.create(
String.format(
"/api/v3/targets/%d/recordings/%s/upload",
target.id, recordingName)))
"/api/v3/targets/%d/recordings/%d/upload",
target.id, remoteId)))
.build();
}

@POST
@Path("/api/v3/targets/{id}/recordings/{recordingName}/upload")
@Path("/api/v3/targets/{targetId}/recordings/{remoteId}/upload")
@RolesAllowed("write")
@Blocking
public Response uploadToGrafana(@RestPath long id, @RestPath String recordingName)
throws Exception {
public Response uploadToGrafana(@RestPath long id, @RestPath long remoteId) throws Exception {
try {
URL uploadUrl =
new URL(
Expand All @@ -705,7 +710,7 @@ public Response uploadToGrafana(@RestPath long id, @RestPath String recordingNam
ConfigProperties.GRAFANA_DATASOURCE_URL, uploadUrl.toString()));
}

return recordingHelper.doPost(id, recordingName, uploadUrl);
return recordingHelper.doPost(id, remoteId, uploadUrl);
} catch (MalformedURLException e) {
throw new NotImplementedException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.openjdk.jmc.rjmx.services.jfr.IRecordingDescriptor;

import io.cryostat.ProgressInputStream;
import io.cryostat.core.net.JFRConnection;
import io.cryostat.targets.Target;
import io.cryostat.targets.TargetConnectionManager;

Expand All @@ -33,15 +34,18 @@ public class RemoteRecordingInputStreamFactory {

public ProgressInputStream open(Target target, ActiveRecording activeRecording)
throws Exception {
InputStream bareStream =
connectionManager.executeConnectedTask(
target,
conn -> {
IRecordingDescriptor desc =
RecordingHelper.getDescriptor(conn, activeRecording)
.orElseThrow();
return conn.getService().openStream(desc, false);
});
return connectionManager.executeConnectedTask(
target,
conn -> {
IRecordingDescriptor desc =
RecordingHelper.getDescriptor(conn, activeRecording).orElseThrow();
return open(conn, target, desc);
});
}

public ProgressInputStream open(JFRConnection conn, Target target, IRecordingDescriptor desc)
throws Exception {
InputStream bareStream = conn.getService().openStream(desc, false);
return new ProgressInputStream(
bareStream, n -> connectionManager.markConnectionInUse(target));
}
Expand Down

0 comments on commit b7974ab

Please sign in to comment.