Skip to content

Commit 1b01023

Browse files
Fix RecyclerBytesStreamOutput corrupting when ending write on page boundary (#95114) (#96322)
Fixes the fact that we don't put the current page offset to zero when we already have capacity in the buffer but are not on the last page because of a seek.
1 parent 37674f8 commit 1b01023

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

docs/changelog/95114.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 95114
2+
summary: Fix `RecyclerBytesStreamOutput` corrupting when ending write on page boundary
3+
area: Network
4+
type: bug
5+
issues: []

server/src/main/java/org/elasticsearch/common/io/stream/RecyclerBytesStreamOutput.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,6 @@ public void writeWithSizePrefix(Writeable writeable) throws IOException {
144144
}
145145
}
146146

147-
public void reset() {
148-
Releasables.close(pages);
149-
pages.clear();
150-
pageIndex = -1;
151-
currentPageOffset = pageSize;
152-
}
153-
154147
@Override
155148
public void flush() {
156149
// nothing to do
@@ -235,11 +228,11 @@ private void ensureCapacityFromPosition(long newPosition) {
235228
assert pageSize == newPage.v().length;
236229
pages.add(newPage);
237230
// We are at the end of the current page, increment page index
238-
if (currentPageOffset == pageSize) {
239-
pageIndex++;
240-
currentPageOffset = 0;
241-
}
242231
currentCapacity += pageSize;
243232
}
233+
if (currentPageOffset == pageSize) {
234+
pageIndex++;
235+
currentPageOffset = 0;
236+
}
244237
}
245238
}

server/src/test/java/org/elasticsearch/common/io/stream/RecyclerBytesStreamOutputTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,22 @@ public void testReadWriteGeoPoint() throws IOException {
712712
}
713713
}
714714

715+
public void testWriteLongToCompletePage() throws IOException {
716+
try (RecyclerBytesStreamOutput out = new RecyclerBytesStreamOutput(recycler)) {
717+
out.seek(PageCacheRecycler.BYTE_PAGE_SIZE + 1);
718+
int longPos = PageCacheRecycler.BYTE_PAGE_SIZE - Long.BYTES;
719+
out.seek(longPos);
720+
long longValue = randomLong();
721+
out.writeLong(longValue);
722+
byte byteValue = randomByte();
723+
out.writeByte(byteValue);
724+
var input = out.bytes().streamInput();
725+
assertEquals(longPos, input.skip(longPos));
726+
assertEquals(longValue, input.readLong());
727+
assertEquals(byteValue, input.readByte());
728+
}
729+
}
730+
715731
private static class TestWriteable implements Writeable {
716732

717733
private boolean value;

0 commit comments

Comments
 (0)