Skip to content

Add DirectByteBuffer strategy for transport-nio#36289

Merged
Tim-Brooks merged 17 commits intoelastic:masterfrom
Tim-Brooks:add_io_buffer
Dec 7, 2018
Merged

Add DirectByteBuffer strategy for transport-nio#36289
Tim-Brooks merged 17 commits intoelastic:masterfrom
Tim-Brooks:add_io_buffer

Conversation

@Tim-Brooks
Copy link
Copy Markdown
Contributor

This is related to #27260. In Elasticsearch all of the messages that we
serialize to write to the network are composed of heap bytes. When you
read or write to a nio socket in java, the heap memory you passed down
must be copied to/from direct memory. The JVM internally does some
buffering of the direct memory, however it is essentially unbounded.

This commit introduces a simple mechanism of buffering and copying the
memory in transport-nio. Each network event loop is given a 64kb
DirectByteBuffer. When we go to read we use this buffer and copy the
data after the read. Additionally, when we go to write, we copy the data
to the direct memory before calling write. 64KB is chosen as this is the
default receive buffer size we use for transport-netty4
(NETTY_RECEIVE_PREDICTOR_SIZE).

Since we only have one buffer per thread, we could afford larger.
However, if we the buffer is large and not all of the data is flushed in
a write call, we will do excess copies. This is something we can
explore in the future.

@Tim-Brooks Tim-Brooks added >non-issue :Distributed/Network Http and internode communication implementations v7.0.0 labels Dec 6, 2018
@elasticmachine
Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-distributed

ioBuffer.flip();
try {
return rawChannel.write(buffer);
return rawChannel.write(ioBuffer);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Shouldn't we move the position on buffer back again by the number of writes not written if the write doesn't succeed in writing all of ioBuffer, i.e. when ioBuffer.remaining() < rawChannel.write(ioBuffer)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is an issue. The passed buffer needs to reflect partial flushes.

@Tim-Brooks
Copy link
Copy Markdown
Contributor Author

@original-brownbear - Thanks for the review. I have added a number of tests. And fixed the issue you identified.

Copy link
Copy Markdown
Contributor

@original-brownbear original-brownbear left a comment

Choose a reason for hiding this comment

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

@tbrooks8 thanks LGTM :)

@Tim-Brooks
Copy link
Copy Markdown
Contributor Author

@elasticmachine run sample packaging tests

@Tim-Brooks Tim-Brooks merged commit 373c67d into elastic:master Dec 7, 2018
@Tim-Brooks Tim-Brooks deleted the add_io_buffer branch December 18, 2019 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Distributed/Network Http and internode communication implementations >non-issue v7.0.0-beta1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants