-
Notifications
You must be signed in to change notification settings - Fork 26k
Pausable chunked HTTP responses #104851
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
Pausable chunked HTTP responses #104851
Changes from 16 commits
c92a7ec
f7250b8
ff24f3c
e957bc7
44a4482
18e65e2
1fd16fb
33fa0cd
1d927ca
b0e3ff8
708fa52
4954c8e
aa52c18
43584be
2022d7a
ffd6cac
1777bc5
befb016
3fab7ce
bbcbf5a
7b896e0
0b91c7f
d5e4031
751ce75
cf74253
38d55af
048194d
6fac79d
64ad627
e4a34ac
b3b7399
d4d1401
221b5b6
b01bda4
39b71dd
23c1a1f
30795e0
4570c92
0d923f4
ee1a4a4
a2c385e
344f75c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| package org.elasticsearch.http.netty4; | ||
|
|
||
| import io.netty.util.concurrent.PromiseCombiner; | ||
|
|
||
| import org.elasticsearch.rest.ChunkedRestResponseBody; | ||
|
|
||
| final class Netty4ChunkedHttpContinuation implements Netty4HttpResponse { | ||
| private final int sequence; | ||
| private final ChunkedRestResponseBody body; | ||
| private final PromiseCombiner combiner; | ||
|
|
||
| Netty4ChunkedHttpContinuation(int sequence, ChunkedRestResponseBody body, PromiseCombiner combiner) { | ||
| this.sequence = sequence; | ||
| this.body = body; | ||
| this.combiner = combiner; | ||
| } | ||
|
|
||
| @Override | ||
| public int getSequence() { | ||
| return sequence; | ||
| } | ||
|
|
||
| public ChunkedRestResponseBody body() { | ||
| return body; | ||
| } | ||
|
|
||
| public PromiseCombiner combiner() { | ||
| return combiner; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,8 @@ | |
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.elasticsearch.ExceptionsHelper; | ||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.common.Strings; | ||
| import org.elasticsearch.common.bytes.ReleasableBytesReference; | ||
| import org.elasticsearch.core.Booleans; | ||
| import org.elasticsearch.core.Nullable; | ||
|
|
@@ -140,6 +142,8 @@ public void write(final ChannelHandlerContext ctx, final Object msg, final Chann | |
| try { | ||
| final Netty4HttpResponse restResponse = (Netty4HttpResponse) msg; | ||
| if (restResponse.getSequence() != writeSequence) { | ||
| assert restResponse instanceof Netty4ChunkedHttpContinuation == false | ||
| : "received out-of-order continuation at [" + restResponse.getSequence() + "], expecting [" + writeSequence + "]"; | ||
| assert restResponse.getSequence() > writeSequence | ||
| : "response sequence [" + restResponse.getSequence() + "] we below write sequence [" + writeSequence + "]"; | ||
| if (outboundHoldingQueue.size() >= maxEventsHeld) { | ||
|
|
@@ -198,6 +202,8 @@ private void doWrite(ChannelHandlerContext ctx, Netty4HttpResponse readyResponse | |
| doWriteFullResponse(ctx, fullResponse, promise); | ||
| } else if (readyResponse instanceof Netty4ChunkedHttpResponse chunkedResponse) { | ||
| doWriteChunkedResponse(ctx, chunkedResponse, promise); | ||
| } else if (readyResponse instanceof Netty4ChunkedHttpContinuation chunkedContinuation) { | ||
| doWriteChunkedContinuation(ctx, chunkedContinuation, promise); | ||
| } else { | ||
| assert false : readyResponse.getClass().getCanonicalName(); | ||
| throw new IllegalStateException("illegal message type: " + readyResponse.getClass().getCanonicalName()); | ||
|
|
@@ -236,13 +242,54 @@ private void doWriteChunkedResponse(ChannelHandlerContext ctx, Netty4ChunkedHttp | |
| } | ||
| } | ||
|
|
||
| private void doWriteChunkedContinuation(ChannelHandlerContext ctx, Netty4ChunkedHttpContinuation continuation, ChannelPromise promise) | ||
| throws IOException { | ||
| final PromiseCombiner combiner = continuation.combiner(); | ||
| assert currentChunkedWrite == null; | ||
| final var responseBody = continuation.body(); | ||
| currentChunkedWrite = new ChunkedWrite(combiner, promise, responseBody); | ||
| // NB "writable" means there's space in the downstream ChannelOutboundBuffer, we aren't trying to saturate the physical channel. | ||
| while (ctx.channel().isWritable()) { | ||
| if (writeChunk(ctx, combiner, responseBody)) { | ||
| finishChunkedWrite(); | ||
| return; | ||
| } | ||
| } | ||
|
ywangd marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private void finishChunkedWrite() { | ||
| assert currentChunkedWrite != null; | ||
| assert currentChunkedWrite.responseBody().isDone(); | ||
| final var finishingWrite = currentChunkedWrite; | ||
| final var endOfResponse = finishingWrite.responseBody().isEndOfResponse(); | ||
| currentChunkedWrite = null; | ||
| writeSequence++; | ||
| finishingWrite.combiner.finish(finishingWrite.onDone()); | ||
| if (endOfResponse) { | ||
| writeSequence++; | ||
| finishingWrite.combiner.finish(finishingWrite.onDone()); | ||
| } else { | ||
| final var channel = finishingWrite.onDone().channel(); | ||
| ActionListener.run(ActionListener.assertOnce(new ActionListener<>() { | ||
| @Override | ||
| public void onResponse(ChunkedRestResponseBody continuation) { | ||
| channel.writeAndFlush( | ||
| new Netty4ChunkedHttpContinuation(writeSequence, continuation, finishingWrite.combiner()), | ||
| finishingWrite.onDone() // pass the terminal listener/promise along the line | ||
| ); | ||
|
ywangd marked this conversation as resolved.
Comment on lines
+265
to
+268
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. I wonder whether similar treatment (#105306) is needed here?
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. I don't think so, this code runs on the event loop already so these writes will still either complete or fail properly even if we're in the process of shutting down.
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. Is it always the case even when
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. Ah yes you're quite right, thanks
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. I've applied a fix in 6fac79d but it had to be a little different from #105306 since we don't have a listener to wrap any more. But then I realised we could do the same thing at the outer layer too, and extract a utility, and in fact what we do today at the outer layer is also a little questionable too. I made the relevant changes in #105486 and will migrate to that utility once it's available here. |
||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(Exception e) { | ||
| logger.error( | ||
| Strings.format("failed to get continuation of HTTP response body for [%s], closing connection", channel), | ||
| e | ||
| ); | ||
| channel.close().addListener(ignored -> { | ||
| finishingWrite.combiner().add(channel.newFailedFuture(e)); | ||
| finishingWrite.combiner().finish(finishingWrite.onDone()); | ||
|
Comment on lines
+279
to
+280
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. Similarly to the other PR, I wonder whether this could be
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. I'd rather wait for the whole combiner to complete (and wonder whether we should do that in other places too). And |
||
| }); | ||
| } | ||
| }), finishingWrite.responseBody()::getContinuation); | ||
| } | ||
| } | ||
|
|
||
| private void splitAndWrite(ChannelHandlerContext ctx, Netty4FullHttpResponse msg, ChannelPromise promise) { | ||
|
|
@@ -326,7 +373,8 @@ private boolean writeChunk(ChannelHandlerContext ctx, PromiseCombiner combiner, | |
| ); | ||
| final ByteBuf content = Netty4Utils.toByteBuf(bytes); | ||
| final boolean done = body.isDone(); | ||
| final ChannelFuture f = ctx.write(done ? new DefaultLastHttpContent(content) : new DefaultHttpContent(content)); | ||
| final boolean lastChunk = done && body.isEndOfResponse(); | ||
| final ChannelFuture f = ctx.write(lastChunk ? new DefaultLastHttpContent(content) : new DefaultHttpContent(content)); | ||
| f.addListener(ignored -> bytes.close()); | ||
| combiner.add(f); | ||
| return done; | ||
|
|
@@ -337,6 +385,11 @@ private void failQueuedWrites() { | |
| while ((queuedWrite = queuedWrites.poll()) != null) { | ||
| queuedWrite.failAsClosedChannel(); | ||
| } | ||
| if (currentChunkedWrite != null) { | ||
| final var finishingWrite = currentChunkedWrite; | ||
| currentChunkedWrite = null; | ||
| finishingWrite.combiner.finish(finishingWrite.onDone()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.