Skip to content

Commit

Permalink
refactor to use Uni
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Feb 15, 2024
1 parent 273e7a9 commit 6451d42
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 34 deletions.
50 changes: 19 additions & 31 deletions src/main/java/io/cryostat/recordings/RecordingHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@
import io.cryostat.ws.Notification;

import io.quarkus.runtime.StartupEvent;
import io.smallrye.common.annotation.Blocking;
import io.smallrye.mutiny.Uni;
import io.vertx.ext.web.handler.HttpException;
import io.vertx.mutiny.core.eventbus.EventBus;
import io.vertx.mutiny.ext.web.client.HttpResponse;
import io.vertx.mutiny.ext.web.client.WebClient;
import io.vertx.mutiny.ext.web.multipart.MultipartForm;
import jakarta.enterprise.context.ApplicationScoped;
Expand All @@ -79,8 +82,6 @@
import jakarta.inject.Named;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.ServerErrorException;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.ResponseBuilder;
import jdk.jfr.RecordingState;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -89,7 +90,6 @@
import org.apache.hc.core5.http.HttpStatus;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.server.jaxrs.ResponseBuilderImpl;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
Expand Down Expand Up @@ -805,8 +805,8 @@ private Metadata taggingToMetadata(List<Tag> tagSet) {
return new Metadata(labels, expiry);
}

// jfr-datasource handling
public Response uploadToJFRDatasource(long targetEntityId, long remoteId) throws Exception {
@Blocking
public Uni<String> uploadToJFRDatasource(long targetEntityId, long remoteId) throws Exception {
Target target = Target.getTargetById(targetEntityId);
Objects.requireNonNull(target, "Target from targetId not found");
ActiveRecording recording = target.getRecordingById(remoteId);
Expand All @@ -822,14 +822,11 @@ public Response uploadToJFRDatasource(long targetEntityId, long remoteId) throws
target.targetId(), recording.name));
});

try {
return uploadToJFRDatasource(recordingPath);
} finally {
fs.deleteIfExists(recordingPath);
}
return uploadToJFRDatasource(recordingPath);
}

public Response uploadToJFRDatasource(Pair<String, String> key) throws Exception {
@Blocking
public Uni<String> uploadToJFRDatasource(Pair<String, String> key) throws Exception {
Objects.requireNonNull(key);
Objects.requireNonNull(key.getKey());
Objects.requireNonNull(key.getValue());
Expand All @@ -846,21 +843,16 @@ public Response uploadToJFRDatasource(Pair<String, String> key) throws Exception

storage.getObject(getRequest, recordingPath);

try {
return uploadToJFRDatasource(recordingPath);
} finally {
fs.deleteIfExists(recordingPath);
}
return uploadToJFRDatasource(recordingPath);
}

private Response uploadToJFRDatasource(Path recordingPath)
private Uni<String> uploadToJFRDatasource(Path recordingPath)
throws URISyntaxException, InterruptedException, ExecutionException {
MultipartForm form =
MultipartForm.create()
.binaryFileUpload(
"file", DATASOURCE_FILENAME, recordingPath.toString(), JFR_MIME);

ResponseBuilder builder = new ResponseBuilderImpl();
var asyncRequest =
webClient
.postAbs(
Expand All @@ -875,19 +867,15 @@ private Response uploadToJFRDatasource(Path recordingPath)
.sendMultipartForm(form);
return asyncRequest
.onItem()
.transform(
r ->
builder.status(r.statusCode(), r.statusMessage())
.entity(r.bodyAsString())
.build())
.onFailure()
.recoverWithItem(
(failure) -> {
logger.error(failure);
return Response.serverError().build();
})
.await()
.indefinitely(); // The timeout from the request should be sufficient
.transform(HttpResponse::bodyAsString)
.eventually(
() -> {
try {
fs.deleteIfExists(recordingPath);
} catch (IOException e) {
logger.warn(e);
}
});
}

Optional<Path> getRecordingCopyPath(
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/io/cryostat/recordings/Recordings.java
Original file line number Diff line number Diff line change
Expand Up @@ -819,13 +819,12 @@ public Response uploadActiveToGrafanaV1(
@Blocking
@Path("/api/v3/targets/{targetId}/recordings/{remoteId}/upload")
@RolesAllowed("write")
public Response uploadActiveToGrafana(@RestPath long targetId, @RestPath long remoteId)
public Uni<String> uploadActiveToGrafana(@RestPath long targetId, @RestPath long remoteId)
throws Exception {
return recordingHelper.uploadToJFRDatasource(targetId, remoteId);
}

@POST
@Blocking
@Path("/api/beta/fs/recordings/{jvmId}/{filename}/upload")
@RolesAllowed("write")
public Response uploadArchivedToGrafanaBeta(@RestPath String jvmId, @RestPath String filename)
Expand All @@ -843,7 +842,7 @@ public Response uploadArchivedToGrafanaBeta(@RestPath String jvmId, @RestPath St
@Blocking
@Path("/api/v3/grafana/{encodedKey}")
@RolesAllowed("write")
public Response uploadArchivedToGrafana(@RestPath String encodedKey) throws Exception {
public Uni<String> uploadArchivedToGrafana(@RestPath String encodedKey) throws Exception {
var key = recordingHelper.decodedKey(encodedKey);
var found =
recordingHelper.listArchivedRecordingObjects().stream()
Expand Down

0 comments on commit 6451d42

Please sign in to comment.