Skip to content

Fix RecyclerBytesStreamOutput corrupting when ending write on page boundary #95114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/95114.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 95114
summary: Fix `RecyclerBytesStreamOutput` corrupting when ending write on page boundary
area: Network
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,6 @@ public void writeWithSizePrefix(Writeable writeable) throws IOException {
}
}

public void reset() {
Releasables.close(pages);
pages.clear();
pageIndex = -1;
currentPageOffset = pageSize;
}

@Override
public void flush() {
// nothing to do
Expand Down Expand Up @@ -235,11 +228,11 @@ private void ensureCapacityFromPosition(long newPosition) {
assert pageSize == newPage.v().length;
pages.add(newPage);
// We are at the end of the current page, increment page index
if (currentPageOffset == pageSize) {
pageIndex++;
currentPageOffset = 0;
}
currentCapacity += pageSize;
}
if (currentPageOffset == pageSize) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also faster for the rare case where we have to allocate multiple pages in a loop to make space here.

pageIndex++;
currentPageOffset = 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,22 @@ public void testReadWriteGeoPoint() throws IOException {
}
}

public void testWriteLongToCompletePage() throws IOException {
try (RecyclerBytesStreamOutput out = new RecyclerBytesStreamOutput(recycler)) {
out.seek(PageCacheRecycler.BYTE_PAGE_SIZE + 1);
int longPos = PageCacheRecycler.BYTE_PAGE_SIZE - Long.BYTES;
out.seek(longPos);
long longValue = randomLong();
out.writeLong(longValue);
byte byteValue = randomByte();
out.writeByte(byteValue);
var input = out.bytes().streamInput();
assertEquals(longPos, input.skip(longPos));
assertEquals(longValue, input.readLong());
assertEquals(byteValue, input.readByte());
}
}

private static class TestWriteable implements Writeable {

private boolean value;
Expand Down