Skip to content

Commit

Permalink
No-op reset in SlicedInputStream (elastic#118437)
Browse files Browse the repository at this point in the history
Previously if reset was called at the exact marked offset, it
would unnecessarily re-open the current slice and skip bytes.
We now detect this situation, and just do nothing in this case.

Closes ES-10235
  • Loading branch information
kingherc authored Dec 11, 2024
1 parent 03fa270 commit 912d37a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ public void reset() throws IOException {
if (markedSlice < 0 || markedSliceOffset < 0) {
throw new IOException("Mark has not been set");
}
if (initialized && nextSlice == markedSlice + 1 && currentSliceOffset == markedSliceOffset) {
// Reset at the marked offset should return immediately without re-opening the slice
return;
}

nextSlice = markedSlice;
initialized = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ protected InputStream openSlice(int slice) throws IOException {

// Mark
input.mark(randomNonNegativeInt());
int slicesOpenedAtMark = streamsOpened.size();

// Read or skip up to another random point
final int moreBytes = randomIntBetween(0, bytes.length - mark);
int moreBytes = randomIntBetween(0, bytes.length - mark);
if (moreBytes > 0) {
if (randomBoolean()) {
final var moreBytesRead = new byte[moreBytes];
Expand All @@ -171,11 +172,13 @@ protected InputStream openSlice(int slice) throws IOException {

// Randomly read to EOF
if (randomBoolean()) {
input.readAllBytes();
moreBytes += input.readAllBytes().length;
}

// Reset
input.reset();
int slicesOpenedAfterReset = streamsOpened.size();
assert moreBytes > 0 || mark == 0 || slicesOpenedAfterReset == slicesOpenedAtMark : "Reset at mark should not re-open slices";

// Read all remaining bytes, which should be the bytes from mark up to the end
final int remainingBytes = bytes.length - mark;
Expand Down

0 comments on commit 912d37a

Please sign in to comment.