Skip to content

Commit

Permalink
Disable re-usage of bbaos and create bbaos of optimal size when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
nck-mlcnv committed Jul 4, 2024
1 parent 70d0a2b commit 3b5c344
Showing 1 changed file with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ public boolean successful() {

private final ResponseBodyProcessor responseBodyProcessor;

// declared here, so it can be reused across multiple method calls
private BigByteArrayOutputStream responseBodybbaos = new BigByteArrayOutputStream();

// used to read the http response body
private final byte[] buffer = new byte[BUFFER_SIZE];
private static final int BUFFER_SIZE = 4096;
Expand Down Expand Up @@ -253,17 +250,7 @@ private ExecutionStats executeQuery(Duration timeout, boolean discardOnFailure)
}

// process result
if (!responseBodyProcessor.add(result.actualContentLength().orElse(-1), result.hash().orElse(-1), result.outputStream().orElse(new BigByteArrayOutputStream()))) {
this.responseBodybbaos = result.outputStream().orElse(new BigByteArrayOutputStream());
} else {
this.responseBodybbaos = new BigByteArrayOutputStream();
}
}

try {
this.responseBodybbaos.reset();
} catch (IOException e) {
this.responseBodybbaos = new BigByteArrayOutputStream();
responseBodyProcessor.add(result.actualContentLength().orElse(-1), result.hash().orElse(-1), result.outputStream().orElse(new BigByteArrayOutputStream()));
}

if (!result.successful() && discardOnFailure) {
Expand Down Expand Up @@ -327,6 +314,7 @@ private HttpExecutionResult executeHttpRequest(Duration timeout) {
private final StreamingXXHash64 hasher = hasherFactory.newStreamingHash64(0);
private long responseSize = 0; // will be used if parseResults is false
private long responseEnd = 0; // time in nanos
private BigByteArrayOutputStream responseBodybbaos = null;

@Override
public void releaseResources() {} // nothing to release
Expand All @@ -344,10 +332,13 @@ protected int capacityIncrement() {
*/
@Override
protected void data(ByteBuffer src, boolean endOfStream) throws IOException {
if (endOfStream) {
if (endOfStream)
responseEnd = System.nanoTime();
}

if (responseBodybbaos == null)
responseBodybbaos = new BigByteArrayOutputStream();

responseSize += src.remaining();
if (config.parseResults()) {
// if the buffer uses an array, use the array directly
if (src.hasArray()) {
Expand All @@ -362,8 +353,6 @@ protected void data(ByteBuffer src, boolean endOfStream) throws IOException {
responseBodybbaos.write(buffer, 0, readCount);
}
}
} else {
responseSize += src.remaining();
}
}

Expand All @@ -377,6 +366,12 @@ protected void data(ByteBuffer src, boolean endOfStream) throws IOException {
@Override
protected void start(HttpResponse response, ContentType contentType) {
this.response = response;
final var contentLengthHeader = response.getFirstHeader("Content-Length");
Long contentLength = contentLengthHeader != null ? Long.parseLong(contentLengthHeader.getValue()) : null;
// if the content length is known, create a BigByteArrayOutputStream with the known length
if (contentLength != null && responseBodybbaos == null && config.parseResults()) {
responseBodybbaos = new BigByteArrayOutputStream(contentLength);
}
}

/**
Expand Down Expand Up @@ -404,7 +399,10 @@ protected HttpExecutionResult buildResult() {
if (contentLength != null) {
if ((!config.parseResults() && responseSize != contentLength) // if parseResults is false, the responseSize will be used
|| (config.parseResults() && responseBodybbaos.size() != contentLength)) { // if parseResults is true, the size of the bbaos will be used
return createFailedResultDuringResponse(queryIndex, response, timeStamp, duration, new HttpException("Content-Length header value doesn't match actual content length."));
if (responseSize != responseBodybbaos.size())
LOGGER.error("Error during copying the response data. (expected written data size = {}, actual written data size = {}, Content-Length-Header = {})", responseSize, responseBodybbaos.size(), contentLengthHeader.getValue());
final var exception = new HttpException(String.format("Content-Length header value doesn't match actual content length. (Content-Length-Header = %s, written data size = %s)", contentLength, config.parseResults() ? responseBodybbaos.size() : responseSize));
return createFailedResultDuringResponse(queryIndex, response, timeStamp, duration, exception);
}
}

Expand Down

0 comments on commit 3b5c344

Please sign in to comment.