Skip to content

Commit

Permalink
Remove netty BytesReference implementations (elastic#54025)
Browse files Browse the repository at this point in the history
Elasticsearch has a number of different BytesReference implementations.
These implementations can all implement the interface in different ways
with subtly different behavior and performance characteristics. On the
other-hand, the JVM only represents bytes as an array or a direct byte
buffer. This commit deletes the specialized Netty implementations and
moves to using a generic ByteBuffer reference type. This will allow us
to focus on standardizing performance and behave around a smaller number
of implementations that can be used by all components in Elasticsearch.
  • Loading branch information
Tim-Brooks authored Mar 25, 2020
1 parent 513985e commit 519c8c4
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 470 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.elasticsearch.common.bytes.BytesReference;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -80,28 +81,24 @@ public static ByteBuf toByteBuf(final BytesReference reference) {
if (reference.length() == 0) {
return Unpooled.EMPTY_BUFFER;
}
if (reference instanceof ByteBufBytesReference) {
return ((ByteBufBytesReference) reference).toByteBuf();
} else {
final BytesRefIterator iterator = reference.iterator();
// usually we have one, two, or three components from the header, the message, and a buffer
final List<ByteBuf> buffers = new ArrayList<>(3);
try {
BytesRef slice;
while ((slice = iterator.next()) != null) {
buffers.add(Unpooled.wrappedBuffer(slice.bytes, slice.offset, slice.length));
}
final BytesRefIterator iterator = reference.iterator();
// usually we have one, two, or three components from the header, the message, and a buffer
final List<ByteBuf> buffers = new ArrayList<>(3);
try {
BytesRef slice;
while ((slice = iterator.next()) != null) {
buffers.add(Unpooled.wrappedBuffer(slice.bytes, slice.offset, slice.length));
}

if (buffers.size() == 1) {
return buffers.get(0);
} else {
CompositeByteBuf composite = Unpooled.compositeBuffer(buffers.size());
composite.addComponents(true, buffers);
return composite;
}
} catch (IOException ex) {
throw new AssertionError("no IO happens here", ex);
if (buffers.size() == 1) {
return buffers.get(0);
} else {
CompositeByteBuf composite = Unpooled.compositeBuffer(buffers.size());
composite.addComponents(true, buffers);
return composite;
}
} catch (IOException ex) {
throw new AssertionError("no IO happens here", ex);
}
}

Expand All @@ -112,7 +109,9 @@ public static BytesReference toBytesReference(final ByteBuf buffer) {
final int readableBytes = buffer.readableBytes();
if (readableBytes == 0) {
return BytesArray.EMPTY;
} else {
final ByteBuffer[] byteBuffers = buffer.nioBuffers();
return BytesReference.fromByteBuffers(byteBuffers);
}
return new ByteBufBytesReference(buffer, buffer.readableBytes());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ public void testToChannelBuffer() throws IOException {
BytesReference ref = getRandomizedBytesReference(randomIntBetween(1, 3 * PAGE_SIZE));
ByteBuf buffer = Netty4Utils.toByteBuf(ref);
BytesReference bytesReference = Netty4Utils.toBytesReference(buffer);
if (ref instanceof ByteBufBytesReference) {
assertEquals(buffer, ((ByteBufBytesReference) ref).toByteBuf());
} else if (AbstractBytesReferenceTestCase.getNumPages(ref) > 1) { // we gather the buffers into a channel buffer
if (AbstractBytesReferenceTestCase.getNumPages(ref) > 1) { // we gather the buffers into a channel buffer
assertTrue(buffer instanceof CompositeByteBuf);
}
assertArrayEquals(BytesReference.toBytes(ref), BytesReference.toBytes(bytesReference));
Expand Down
Loading

0 comments on commit 519c8c4

Please sign in to comment.