Skip to content

Commit

Permalink
refactor to extract shared code
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Feb 15, 2024
1 parent f325884 commit 273e7a9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 65 deletions.
7 changes: 7 additions & 0 deletions src/main/java/io/cryostat/ExceptionMappers.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.cryostat;

import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;

import org.openjdk.jmc.rjmx.ConnectionException;

Expand Down Expand Up @@ -109,4 +110,10 @@ public RestResponse<Void> mapCompletionException(CompletionException ex) throws
logger.warn(ex);
throw ExceptionUtils.getRootCause(ex);
}

@ServerExceptionMapper
public RestResponse<Void> mapExecutionException(ExecutionException ex) throws Throwable {
logger.warn(ex);
throw ExceptionUtils.getRootCause(ex);
}
}
108 changes: 43 additions & 65 deletions src/main/java/io/cryostat/recordings/RecordingHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.ByteBuffer;
Expand All @@ -37,6 +38,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -820,46 +822,17 @@ public Response uploadToJFRDatasource(long targetEntityId, long remoteId) throws
target.targetId(), recording.name));
});

MultipartForm form =
MultipartForm.create()
.binaryFileUpload(
"file", DATASOURCE_FILENAME, recordingPath.toString(), JFR_MIME);

try {
ResponseBuilder builder = new ResponseBuilderImpl();
var asyncRequest =
webClient
.postAbs(
grafanaDatasourceURL
.get()
.toURI()
.resolve("/load")
.normalize()
.toString())
.addQueryParam("overwrite", "true")
.timeout(connectionFailedTimeout.toMillis())
.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
return uploadToJFRDatasource(recordingPath);
} finally {
fs.deleteIfExists(recordingPath);
}
}

public Response uploadToJFRDatasource(Pair<String, String> key) throws Exception {
Objects.requireNonNull(key);
Objects.requireNonNull(key.getKey());
Objects.requireNonNull(key.getValue());
GetObjectRequest getRequest =
GetObjectRequest.builder()
.bucket(archiveBucket)
Expand All @@ -873,43 +846,48 @@ public Response uploadToJFRDatasource(Pair<String, String> key) throws Exception

storage.getObject(getRequest, recordingPath);

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

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

try {
ResponseBuilder builder = new ResponseBuilderImpl();
var asyncRequest =
webClient
.postAbs(
grafanaDatasourceURL
.get()
.toURI()
.resolve("/load")
.normalize()
.toString())
.addQueryParam("overwrite", "true")
.timeout(connectionFailedTimeout.toMillis())
.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
} finally {
fs.deleteIfExists(recordingPath);
}
ResponseBuilder builder = new ResponseBuilderImpl();
var asyncRequest =
webClient
.postAbs(
grafanaDatasourceURL
.get()
.toURI()
.resolve("/load")
.normalize()
.toString())
.addQueryParam("overwrite", "true")
.timeout(connectionFailedTimeout.toMillis())
.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
}

Optional<Path> getRecordingCopyPath(
Expand Down

0 comments on commit 273e7a9

Please sign in to comment.