Skip to content
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

Implementation of ResponseWriter using pooled Netty buffers #2805

Merged
merged 14 commits into from
Mar 3, 2021

Conversation

spericas
Copy link
Member

@spericas spericas commented Feb 25, 2021

Changes in this PR:

  • Some new methods in DataChunk
  • New DataChunk implementation based on ByteBuf (Netty)
  • Changes in ResponseWriter to use the new DataChunk implementation (ByteBufDataChunk)
  • Changes in BareResponseImpl to consume ByteBufDataChunk's
  • Allocation of underlying ByteBuf's in ByteBufDataChunk using Netty's pool

With all these changes, the DataChunk's passed from Jersey/Helidon's layer are now backed by pooled Netty's ByteBufs. This should reduced the amount of time spent running GC's in MP applications.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
…it tests are passing.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
…d up release logic to avoid warnings in logs. Better implementation of isFlushChunk.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
@spericas spericas self-assigned this Feb 25, 2021
@spericas spericas added webserver enhancement New feature or request labels Feb 25, 2021
@spericas spericas added this to the 2.3.0 milestone Feb 25, 2021
Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
…nstream using a semaphore and volatiles. Ensure onComplete is called at most once.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
@spericas spericas changed the title Implementation of ResponseWriter using pooled Netty buffers WIP: Implementation of ResponseWriter using pooled Netty buffers Feb 26, 2021
Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
@spericas spericas changed the title WIP: Implementation of ResponseWriter using pooled Netty buffers Implementation of ResponseWriter using pooled Netty buffers Feb 26, 2021
req.next();
});
return new OutputStream() {
@Override
public void write(int b) throws IOException {
public void write(int b) {
Copy link
Member

Choose a reason for hiding this comment

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

(This could be a static class.)

Copy link
Contributor

Choose a reason for hiding this comment

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

This could be OutputStream.nullOutputStream();

while (len > 0) {
if (byteBuf == null) {
awaitRequest();
byteBuf = PooledByteBufAllocator.DEFAULT.buffer(BYTEBUF_DEFAULT_SIZE);
Copy link
Member

Choose a reason for hiding this comment

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

@spericas This was what I was asking about earlier today. This will not make use of direct buffers, and perhaps that is just fine. On the other hand, if you use ByteBufAllocator.DEFAULT you'll get some perhaps useful behavior.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry; I got slightly confused here. PooledByteBufAllocator will indeed use direct buffers. I guess my comment is more: if you simply use ByteBufAllocator.DEFAULT you pick up whatever default behavior Netty wants in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

But If we use ByteBufAllocator today, are we guaranteed to get a pooled buffer now? After all, that is the whole point of this PR.

Copy link
Member

Choose a reason for hiding this comment

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

If you see https://github.com/netty/netty/blob/1529ef1794e0a6654fa4334fd979b769d6940e61/buffer/src/main/java/io/netty/buffer/ByteBufUtil.java#L77-L93 you can see what it does. In terms of guarantees, there is very little documentation so effectively no contract as to what you can expect or depend upon.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
Copy link
Contributor

@romain-grecourt romain-grecourt left a comment

Choose a reason for hiding this comment

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

How about merging ByteBufDataChunk and ByteBufRequestChunk ?

@spericas
Copy link
Member Author

spericas commented Mar 1, 2021

How about merging ByteBufDataChunk and ByteBufRequestChunk ?

That class includes the reference queue's logic. Perhaps we can connect the two at some point, but that's a task for a different day in my view.

@Override
public int remaining() {
int remaining = 0;
for (ByteBuf byteBuf : byteBufs) {
Copy link
Member

Choose a reason for hiding this comment

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

If you're going to do stuff with multiple ByteBuf instances, consider Netty's built-in CompositeByteBuf. It may or may not suit your needs.

Copy link
Member

@ljnelson ljnelson left a comment

Choose a reason for hiding this comment

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

LGTM; I made a note about Netty's built-in CompositeByteBuf.

@spericas spericas merged commit bc4f4e8 into helidon-io:master Mar 3, 2021
paulparkinson pushed a commit that referenced this pull request Mar 29, 2021
* Initial prototype based on Netty buffers.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Revised implementation of ByteBufDataChunk and ResponseWriter. All unit tests are passing.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Removed test.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Checkstyle problems.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Ensure proper release of buffers.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Renamed DataChunkImpl as we now have multiple implementations. Cleaned up release logic to avoid warnings in logs. Better implementation of isFlushChunk.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Fixed checkstyle.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Fixed checkstyle.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Copyright in module-info.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Lazy creation of DataChunkOutputStream publisher. Guard access to downstream using a semaphore and volatiles. Ensure onComplete is called at most once.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Implement old method in new ByteBufDataChunk class for testing purposes.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Use a static create() method instead of a public constructor.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
aseovic pushed a commit to aseovic/helidon that referenced this pull request Apr 26, 2021
…io#2805)

* Initial prototype based on Netty buffers.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Revised implementation of ByteBufDataChunk and ResponseWriter. All unit tests are passing.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Removed test.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Checkstyle problems.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Ensure proper release of buffers.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Renamed DataChunkImpl as we now have multiple implementations. Cleaned up release logic to avoid warnings in logs. Better implementation of isFlushChunk.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Fixed checkstyle.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Fixed checkstyle.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Copyright in module-info.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Lazy creation of DataChunkOutputStream publisher. Guard access to downstream using a semaphore and volatiles. Ensure onComplete is called at most once.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Implement old method in new ByteBufDataChunk class for testing purposes.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>

* Use a static create() method instead of a public constructor.

Signed-off-by: Santiago Pericasgeertsen <santiago.pericasgeertsen@oracle.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request webserver
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants