Optimize GCS Mock#51593
Optimize GCS Mock#51593original-brownbear merged 1 commit intoelastic:masterfrom original-brownbear:more-efficient-gcs-mock-master
Conversation
This test was still very GC heavy in Java 8 runs in particular which seems to slow down request processing to the point of timeouts in some runs. This PR completely removes the large number of O(MB) `byte[]` allocations that were happening in the mock http handler which cuts the allocation rate by about a factor of 5 in my local testing for the GC heavy `testSnapshotWithLargeSegmentFiles` run. Closes #51446
|
Pinging @elastic/es-distributed (:Distributed/Snapshot/Restore) |
|
@tlrx if this doesn't help I think we have to find a way to down-scale these tests a little maybe. They're handling multiple MB of data at once which is a little tricky with |
| final int start = getContentRangeStart(range); | ||
| final int end = getContentRangeEnd(range); | ||
|
|
||
| final ByteArrayOutputStream out = new ByteArrayOutputStream() { |
There was a problem hiding this comment.
Using the BAOS here was pretty brutal for multi MB requests :) Much nicer now with the 16kb chunks Streams.readFully(wrappedRequest) produces
| final byte[] buffer = new byte[len]; | ||
| Streams.readFully(stream, buffer); | ||
| content = Tuple.tuple(name, new BytesArray(buffer)); | ||
| content = Tuple.tuple(name, fullRequestBody.slice(startPos, len)); |
There was a problem hiding this comment.
Even though slice wastes some memory here for the wrapping around the actual blob content for smaller blobs, it's a huge efficiency gain to stick with 16kb chunks from the BytesReference compared to allocating a potentially MB sized array.
tlrx
left a comment
There was a problem hiding this comment.
LGTM, thanks for taking care of this Armin
| } | ||
| System.arraycopy(out.toByteArray(), 0, blob, start, Math.toIntExact(bytesRead)); | ||
| blobs.put(blobName, new BytesArray(blob)); | ||
| blob = new CompositeBytesReference(blob, Streams.readFully(wrappedRequest)); |
|
Thanks Tanguy! |
This test was still very GC heavy in Java 8 runs in particular which seems to slow down request processing to the point of timeouts in some runs. This PR completely removes the large number of O(MB) `byte[]` allocations that were happening in the mock http handler which cuts the allocation rate by about a factor of 5 in my local testing for the GC heavy `testSnapshotWithLargeSegmentFiles` run. Closes #51446 Closes #50754
This test was still very GC heavy in Java 8 runs in particular which seems to slow down request processing to the point of timeouts in some runs. This PR completely removes the large number of O(MB) `byte[]` allocations that were happening in the mock http handler which cuts the allocation rate by about a factor of 5 in my local testing for the GC heavy `testSnapshotWithLargeSegmentFiles` run. Closes #51446 Closes #50754
For small uploads (that can still be up to 5MB!) we needlessly reading the `InputStream` into a BAOS which entailed allocating the `byte[]` for the stream contents twice (because to `toByteArray` on the BAOS copies). Also, for resumeable uploads we were needlessly wrapping the output channel and running each individual write in its own privileged context when we could just wrap the whole upload in a single privileged context. Relates #51593
For small uploads (that can still be up to 5MB!) we needlessly reading the `InputStream` into a BAOS which entailed allocating the `byte[]` for the stream contents twice (because to `toByteArray` on the BAOS copies). Also, for resumeable uploads we were needlessly wrapping the output channel and running each individual write in its own privileged context when we could just wrap the whole upload in a single privileged context. Relates #51593
This test was still very GC heavy in Java 8 runs in particular
which seems to slow down request processing to the point of timeouts
in some runs.
This PR completely removes the large number of O(MB)
byte[]allocationsthat were happening in the mock http handler which cuts the allocation rate
by about a factor of 5 in my local testing for the GC heavy
testSnapshotWithLargeSegmentFilesrun.
Closes #51446
Closes #50754