Skip to content

Commit 5f279ca

Browse files
authored
No-op reset in SlicedInputStream (#118437) (#118478)
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
1 parent 159ecaf commit 5f279ca

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

server/src/main/java/org/elasticsearch/index/snapshots/blobstore/SlicedInputStream.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ public void reset() throws IOException {
171171
if (markedSlice < 0 || markedSliceOffset < 0) {
172172
throw new IOException("Mark has not been set");
173173
}
174+
if (initialized && nextSlice == markedSlice + 1 && currentSliceOffset == markedSliceOffset) {
175+
// Reset at the marked offset should return immediately without re-opening the slice
176+
return;
177+
}
174178

175179
nextSlice = markedSlice;
176180
initialized = true;

server/src/test/java/org/elasticsearch/index/snapshots/blobstore/SlicedInputStreamTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,10 @@ protected InputStream openSlice(int slice) throws IOException {
155155

156156
// Mark
157157
input.mark(randomNonNegativeInt());
158+
int slicesOpenedAtMark = streamsOpened.size();
158159

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

172173
// Randomly read to EOF
173174
if (randomBoolean()) {
174-
input.readAllBytes();
175+
moreBytes += input.readAllBytes().length;
175176
}
176177

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

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

0 commit comments

Comments
 (0)