-
Notifications
You must be signed in to change notification settings - Fork 25.9k
Misc cleanups around Netty4HttpPipeliningHandler
#104642
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
DaveCTurner
merged 3 commits into
elastic:main
from
DaveCTurner:2024/01/23/Netty4HttpPipeliningHandler-cleanups
Jan 24, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,8 @@ | |
| import io.netty.handler.codec.DecoderResult; | ||
| import io.netty.handler.codec.http.DefaultFullHttpRequest; | ||
| import io.netty.handler.codec.http.DefaultHttpContent; | ||
| import io.netty.handler.codec.http.DefaultHttpResponse; | ||
| import io.netty.handler.codec.http.DefaultLastHttpContent; | ||
| import io.netty.handler.codec.http.FullHttpResponse; | ||
| import io.netty.handler.codec.http.HttpMethod; | ||
| import io.netty.handler.codec.http.HttpVersion; | ||
|
|
@@ -30,12 +32,14 @@ | |
| import org.elasticsearch.common.bytes.BytesArray; | ||
| import org.elasticsearch.common.bytes.BytesReference; | ||
| import org.elasticsearch.common.bytes.ReleasableBytesReference; | ||
| import org.elasticsearch.common.bytes.ZeroBytesReference; | ||
| import org.elasticsearch.common.recycler.Recycler; | ||
| import org.elasticsearch.http.HttpResponse; | ||
| import org.elasticsearch.rest.ChunkedRestResponseBody; | ||
| import org.elasticsearch.rest.RestStatus; | ||
| import org.elasticsearch.test.ESTestCase; | ||
| import org.elasticsearch.transport.netty4.Netty4Utils; | ||
| import org.elasticsearch.transport.netty4.NettyAllocator; | ||
| import org.junit.After; | ||
|
|
||
| import java.nio.channels.ClosedChannelException; | ||
|
|
@@ -56,6 +60,7 @@ | |
| import static org.hamcrest.Matchers.greaterThan; | ||
| import static org.hamcrest.Matchers.hasSize; | ||
| import static org.hamcrest.Matchers.instanceOf; | ||
| import static org.hamcrest.Matchers.not; | ||
| import static org.hamcrest.Matchers.sameInstance; | ||
| import static org.hamcrest.core.Is.is; | ||
| import static org.mockito.Mockito.mock; | ||
|
|
@@ -70,18 +75,18 @@ public class Netty4HttpPipeliningHandlerTests extends ESTestCase { | |
| @After | ||
| public void tearDown() throws Exception { | ||
| waitingRequests.keySet().forEach(this::finishRequest); | ||
| // shutdown the Executor Service | ||
| if (handlerService.isShutdown() == false) { | ||
| handlerService.shutdown(); | ||
| handlerService.awaitTermination(10, TimeUnit.SECONDS); | ||
| } | ||
| if (eventLoopService.isShutdown() == false) { | ||
| eventLoopService.shutdown(); | ||
| eventLoopService.awaitTermination(10, TimeUnit.SECONDS); | ||
| } | ||
| terminateExecutorService(handlerService); | ||
| terminateExecutorService(eventLoopService); | ||
| super.tearDown(); | ||
| } | ||
|
|
||
| private void terminateExecutorService(ExecutorService executorService) throws InterruptedException { | ||
| if (executorService.isShutdown() == false) { | ||
| executorService.shutdown(); | ||
| assertTrue(executorService.awaitTermination(10, TimeUnit.SECONDS)); | ||
| } | ||
| } | ||
|
|
||
| private CountDownLatch finishRequest(String url) { | ||
| waitingRequests.get(url).countDown(); | ||
| return finishingRequests.get(url); | ||
|
|
@@ -92,7 +97,7 @@ public void testThatPipeliningWorksWithFastSerializedRequests() throws Interrupt | |
| final EmbeddedChannel embeddedChannel = makeEmbeddedChannelWithSimulatedWork(numberOfRequests); | ||
|
|
||
| for (int i = 0; i < numberOfRequests; i++) { | ||
| embeddedChannel.writeInbound(createHttpRequest("/" + String.valueOf(i))); | ||
| embeddedChannel.writeInbound(createHttpRequest("/" + i)); | ||
| } | ||
|
|
||
| final List<CountDownLatch> latches = new ArrayList<>(); | ||
|
|
@@ -127,7 +132,7 @@ public void testThatPipeliningWorksWhenSlowRequestsInDifferentOrder() throws Int | |
| final EmbeddedChannel embeddedChannel = makeEmbeddedChannelWithSimulatedWork(numberOfRequests); | ||
|
|
||
| for (int i = 0; i < numberOfRequests; i++) { | ||
| embeddedChannel.writeInbound(createHttpRequest("/" + String.valueOf(i))); | ||
| embeddedChannel.writeInbound(createHttpRequest("/" + i)); | ||
| } | ||
|
|
||
| // random order execution | ||
|
|
@@ -156,7 +161,7 @@ public void testThatPipeliningClosesConnectionWithTooManyEvents() throws Interru | |
| final EmbeddedChannel embeddedChannel = makeEmbeddedChannelWithSimulatedWork(numberOfRequests); | ||
|
|
||
| for (int i = 0; i < 1 + numberOfRequests + 1; i++) { | ||
| embeddedChannel.writeInbound(createHttpRequest("/" + Integer.toString(i))); | ||
| embeddedChannel.writeInbound(createHttpRequest("/" + i)); | ||
| } | ||
|
|
||
| final List<CountDownLatch> latches = new ArrayList<>(); | ||
|
|
@@ -178,7 +183,7 @@ public void testThatPipeliningClosesConnectionWithTooManyEvents() throws Interru | |
| assertFalse(embeddedChannel.isOpen()); | ||
| } | ||
|
|
||
| public void testPipeliningRequestsAreReleased() throws InterruptedException { | ||
| public void testPipeliningRequestsAreReleased() { | ||
| final int numberOfRequests = 10; | ||
| final EmbeddedChannel embeddedChannel = new EmbeddedChannel(new Netty4HttpPipeliningHandler(logger, numberOfRequests + 1, null)); | ||
|
|
||
|
|
@@ -197,7 +202,7 @@ public void testPipeliningRequestsAreReleased() throws InterruptedException { | |
| ChannelPromise promise = embeddedChannel.newPromise(); | ||
| promises.add(promise); | ||
| Netty4HttpRequest pipelinedRequest = requests.get(i); | ||
| Netty4HttpResponse resp = pipelinedRequest.createResponse(RestStatus.OK, BytesArray.EMPTY); | ||
| Netty4FullHttpResponse resp = pipelinedRequest.createResponse(RestStatus.OK, BytesArray.EMPTY); | ||
| embeddedChannel.writeAndFlush(resp, promise); | ||
| } | ||
|
|
||
|
|
@@ -211,6 +216,45 @@ public void testPipeliningRequestsAreReleased() throws InterruptedException { | |
| } | ||
| } | ||
|
|
||
| public void testSmallFullResponsesAreSentDirectly() { | ||
| final List<Object> messagesSeen = new ArrayList<>(); | ||
| final var embeddedChannel = new EmbeddedChannel(capturingHandler(messagesSeen), getTestHttpHandler()); | ||
| embeddedChannel.writeInbound(createHttpRequest("/test")); | ||
| final Netty4HttpRequest request = embeddedChannel.readInbound(); | ||
| final var maxSize = (int) NettyAllocator.suggestedMaxAllocationSize() / 2; | ||
| final var content = new ZeroBytesReference(between(0, maxSize)); | ||
| final var response = request.createResponse(RestStatus.OK, content); | ||
| assertThat(response, instanceOf(FullHttpResponse.class)); | ||
| final var promise = embeddedChannel.newPromise(); | ||
| embeddedChannel.writeAndFlush(response, promise); | ||
| assertTrue(promise.isDone()); | ||
| assertThat(messagesSeen, hasSize(1)); | ||
| assertSame(response, messagesSeen.get(0)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we also assert that the seen message is an instance of full HTTP response?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep can do, see 5d30ae9 |
||
| } | ||
|
|
||
| public void testLargeFullResponsesAreSplit() { | ||
| final List<Object> messagesSeen = new ArrayList<>(); | ||
| final var embeddedChannel = new EmbeddedChannel(capturingHandler(messagesSeen), getTestHttpHandler()); | ||
| embeddedChannel.writeInbound(createHttpRequest("/test")); | ||
| final Netty4HttpRequest request = embeddedChannel.readInbound(); | ||
| final var minSize = (int) NettyAllocator.suggestedMaxAllocationSize(); | ||
| final var content = new ZeroBytesReference(between(minSize, minSize * 2)); | ||
| final var response = request.createResponse(RestStatus.OK, content); | ||
| assertThat(response, instanceOf(FullHttpResponse.class)); | ||
| final var promise = embeddedChannel.newPromise(); | ||
| embeddedChannel.writeAndFlush(response, promise); | ||
| assertTrue(promise.isDone()); | ||
| assertThat(messagesSeen, hasSize(3)); | ||
| final var headersMessage = asInstanceOf(DefaultHttpResponse.class, messagesSeen.get(0)); | ||
| assertEquals(RestStatus.OK.getStatus(), headersMessage.status().code()); | ||
| assertThat(headersMessage, not(instanceOf(FullHttpResponse.class))); | ||
| final var chunk1 = asInstanceOf(DefaultHttpContent.class, messagesSeen.get(1)); | ||
| final var chunk2 = asInstanceOf(DefaultLastHttpContent.class, messagesSeen.get(2)); | ||
| assertEquals(content.length(), chunk1.content().readableBytes() + chunk2.content().readableBytes()); | ||
| assertThat(chunk1, not(instanceOf(FullHttpResponse.class))); | ||
| assertThat(chunk2, not(instanceOf(FullHttpResponse.class))); | ||
| } | ||
|
|
||
| public void testDecoderErrorSurfacedAsNettyInboundError() { | ||
| final EmbeddedChannel embeddedChannel = new EmbeddedChannel(getTestHttpHandler()); | ||
| // a request with a decoder error | ||
|
|
@@ -304,7 +348,7 @@ public void testResumesSingleAfterChunkedMessage() { | |
| assertTrue(promise1.isDone()); | ||
| assertThat(messagesSeen, hasSize(chunks1 + 1 + 1)); | ||
| assertChunkedMessageAtIndex(messagesSeen, 0, chunks1, chunk); | ||
| assertThat(messagesSeen.get(chunks1 + 1), instanceOf(Netty4HttpResponse.class)); | ||
| assertThat(messagesSeen.get(chunks1 + 1), instanceOf(Netty4FullHttpResponse.class)); | ||
| assertContentAtIndexEquals(messagesSeen, chunks1 + 1, single); | ||
| assertTrue(promise2.isDone()); | ||
| } | ||
|
|
@@ -339,7 +383,7 @@ public void testChunkedResumesAfterSingleMessage() { | |
| embeddedChannel.flush(); | ||
| assertTrue(promise1.isDone()); | ||
| assertThat(messagesSeen, hasSize(chunks2 + 2)); | ||
| assertThat(messagesSeen.get(0), instanceOf(Netty4HttpResponse.class)); | ||
| assertThat(messagesSeen.get(0), instanceOf(Netty4FullHttpResponse.class)); | ||
| assertChunkedMessageAtIndex(messagesSeen, 1, chunks2, chunk); | ||
| assertTrue(promise2.isDone()); | ||
| } | ||
|
|
@@ -377,7 +421,7 @@ public void testChunkedWithSmallChunksResumesAfterSingleMessage() { | |
| embeddedChannel.flush(); | ||
| assertTrue(promise1.isDone()); | ||
| assertThat(messagesSeen, hasSize(chunks2 + 2)); | ||
| assertThat(messagesSeen.get(0), instanceOf(Netty4HttpResponse.class)); | ||
| assertThat(messagesSeen.get(0), instanceOf(Netty4FullHttpResponse.class)); | ||
| assertChunkedMessageAtIndex(messagesSeen, 1, chunks2, chunk); | ||
| assertTrue(promise2.isDone()); | ||
| } | ||
|
|
@@ -410,7 +454,7 @@ public void testPipeliningRequestsAreReleasedAfterFailureOnChunked() { | |
| for (Netty4HttpRequest request : requests) { | ||
| ChannelPromise promise = embeddedChannel.newPromise(); | ||
| promises.add(promise); | ||
| Netty4HttpResponse resp = request.createResponse(RestStatus.OK, BytesArray.EMPTY); | ||
| Netty4FullHttpResponse resp = request.createResponse(RestStatus.OK, BytesArray.EMPTY); | ||
| embeddedChannel.write(resp, promise); | ||
| } | ||
| assertFalse(chunkedWritePromise.isDone()); | ||
|
|
@@ -525,7 +569,7 @@ protected void channelRead0(final ChannelHandlerContext ctx, Netty4HttpRequest r | |
|
|
||
| handlerService.submit(() -> { | ||
| try { | ||
| waitingLatch.await(1000, TimeUnit.SECONDS); | ||
| assertTrue(waitingLatch.await(1000, TimeUnit.SECONDS)); | ||
| final ChannelPromise promise = ctx.newPromise(); | ||
| eventLoopService.submit(() -> { | ||
| ctx.write(httpResponse, promise); | ||
|
|
||
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we rename this interface to
Nett4HttpResponsesince the other class is now calledNetty4FullHttpResponse? The usage ofRestis inconsistent with other classes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it was a little noisy to do that in this same PR, I opened #104675.