Skip to content

Commit 4959d81

Browse files
committed
Add flow-control and remove auto-read in netty4 HTTP pipeline
Re-applying elastic#126441 with the extra `FlowControlHandler` needed to ensure one-chunk-per-read semantics - see elastic#127111 for related tests.
1 parent 203861b commit 4959d81

File tree

15 files changed

+420
-1082
lines changed

15 files changed

+420
-1082
lines changed

docs/changelog/126441.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 126441
2+
summary: Add flow-control and remove auto-read in netty4 http pipeline
3+
area: Network
4+
type: enhancement
5+
issues: []

modules/transport-netty4/src/internalClusterTest/java/org/elasticsearch/http/netty4/Netty4IncrementalRequestHandlingIT.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ public void testClientConnectionCloseMidStream() throws Exception {
197197

198198
// await stream handler is ready and request full content
199199
var handler = clientContext.awaitRestChannelAccepted(opaqueId);
200-
assertBusy(() -> assertNotEquals(0, handler.stream.bufSize()));
201200

202201
assertFalse(handler.isClosed());
203202

@@ -207,7 +206,6 @@ public void testClientConnectionCloseMidStream() throws Exception {
207206
assertEquals(requestTransmittedLength, handler.readUntilClose());
208207

209208
assertTrue(handler.isClosed());
210-
assertEquals(0, handler.stream.bufSize());
211209
}
212210
}
213211

@@ -224,7 +222,6 @@ public void testServerCloseConnectionMidStream() throws Exception {
224222

225223
// await stream handler is ready and request full content
226224
var handler = clientContext.awaitRestChannelAccepted(opaqueId);
227-
assertBusy(() -> assertNotEquals(0, handler.stream.bufSize()));
228225
assertFalse(handler.isClosed());
229226

230227
// terminate connection on server and wait resources are released
@@ -233,7 +230,6 @@ public void testServerCloseConnectionMidStream() throws Exception {
233230
handler.channel.request().getHttpChannel().close();
234231
assertThat(safeGet(exceptionFuture), instanceOf(ClosedChannelException.class));
235232
assertTrue(handler.isClosed());
236-
assertBusy(() -> assertEquals(0, handler.stream.bufSize()));
237233
}
238234
}
239235

@@ -249,7 +245,6 @@ public void testServerExceptionMidStream() throws Exception {
249245

250246
// await stream handler is ready and request full content
251247
var handler = clientContext.awaitRestChannelAccepted(opaqueId);
252-
assertBusy(() -> assertNotEquals(0, handler.stream.bufSize()));
253248
assertFalse(handler.isClosed());
254249

255250
// terminate connection on server and wait resources are released
@@ -261,7 +256,6 @@ public void testServerExceptionMidStream() throws Exception {
261256
final var exception = asInstanceOf(RuntimeException.class, safeGet(exceptionFuture));
262257
assertEquals(ServerRequestHandler.SIMULATED_EXCEPTION_MESSAGE, exception.getMessage());
263258
safeAwait(handler.closedLatch);
264-
assertBusy(() -> assertEquals(0, handler.stream.bufSize()));
265259
}
266260
}
267261

@@ -302,7 +296,7 @@ public void testClientBackpressure() throws Exception {
302296
});
303297
handler.readBytes(partSize);
304298
}
305-
assertTrue(handler.stream.hasLast());
299+
assertTrue(handler.receivedLastChunk);
306300
}
307301
}
308302

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.http.netty4;
11+
12+
import io.netty.channel.ChannelDuplexHandler;
13+
import io.netty.channel.ChannelHandlerContext;
14+
import io.netty.util.concurrent.ScheduledFuture;
15+
16+
import org.apache.logging.log4j.LogManager;
17+
import org.apache.logging.log4j.Logger;
18+
import org.elasticsearch.common.time.TimeProvider;
19+
import org.elasticsearch.common.util.concurrent.FutureUtils;
20+
21+
import java.util.concurrent.TimeUnit;
22+
23+
/**
24+
* When channel auto-read is disabled handlers are responsible to read from channel.
25+
* But it's hard to detect when read is missing. This helper class print warnings
26+
* when no reads where detected in given time interval. Normally, in tests, 10 seconds is enough
27+
* to avoid test hang for too long, but can be increased if needed.
28+
*/
29+
class MissingReadDetector extends ChannelDuplexHandler {
30+
31+
private static final Logger logger = LogManager.getLogger(MissingReadDetector.class);
32+
33+
private final long interval;
34+
private final TimeProvider timer;
35+
private boolean pendingRead;
36+
private long lastRead;
37+
private ScheduledFuture<?> checker;
38+
39+
MissingReadDetector(TimeProvider timer, long missingReadIntervalMillis) {
40+
this.interval = missingReadIntervalMillis;
41+
this.timer = timer;
42+
}
43+
44+
@Override
45+
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
46+
checker = ctx.channel().eventLoop().scheduleAtFixedRate(() -> {
47+
if (pendingRead == false) {
48+
long now = timer.absoluteTimeInMillis();
49+
if (now >= lastRead + interval) {
50+
logger.warn("chan-id={} haven't read from channel for [{}ms]", ctx.channel().id(), (now - lastRead));
51+
}
52+
}
53+
}, interval, interval, TimeUnit.MILLISECONDS);
54+
super.handlerAdded(ctx);
55+
}
56+
57+
@Override
58+
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
59+
if (checker != null) {
60+
FutureUtils.cancel(checker);
61+
}
62+
super.handlerRemoved(ctx);
63+
}
64+
65+
@Override
66+
public void read(ChannelHandlerContext ctx) throws Exception {
67+
pendingRead = true;
68+
ctx.read();
69+
}
70+
71+
@Override
72+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
73+
assert ctx.channel().config().isAutoRead() == false : "auto-read must be always disabled";
74+
pendingRead = false;
75+
lastRead = timer.absoluteTimeInMillis();
76+
ctx.fireChannelRead(msg);
77+
}
78+
}

modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpAggregator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.netty.handler.codec.http.HttpObjectAggregator;
1616
import io.netty.handler.codec.http.HttpRequest;
1717
import io.netty.handler.codec.http.HttpRequestDecoder;
18+
import io.netty.handler.codec.http.LastHttpContent;
1819

1920
import org.elasticsearch.http.HttpPreRequest;
2021
import org.elasticsearch.http.netty4.internal.HttpHeadersAuthenticatorUtils;
@@ -48,6 +49,9 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
4849
}
4950
if (aggregating || msg instanceof FullHttpRequest) {
5051
super.channelRead(ctx, msg);
52+
if (msg instanceof LastHttpContent == false) {
53+
ctx.read(); // HttpObjectAggregator is tricky with auto-read off, it might not call read again, calling on its behalf
54+
}
5155
} else {
5256
streamContentSizeHandler.channelRead(ctx, msg);
5357
}

modules/transport-netty4/src/main/java/org/elasticsearch/http/netty4/Netty4HttpContentSizeHandler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ private void handleRequest(ChannelHandlerContext ctx, HttpRequest request) {
123123
isContinueExpected = true;
124124
} else {
125125
ctx.writeAndFlush(EXPECTATION_FAILED_CLOSE.retainedDuplicate()).addListener(ChannelFutureListener.CLOSE);
126+
ctx.read();
126127
return;
127128
}
128129
}
@@ -136,6 +137,7 @@ private void handleRequest(ChannelHandlerContext ctx, HttpRequest request) {
136137
decoder.reset();
137138
}
138139
ctx.writeAndFlush(TOO_LARGE.retainedDuplicate()).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
140+
ctx.read();
139141
} else {
140142
ignoreContent = false;
141143
currentContentLength = 0;
@@ -150,11 +152,13 @@ private void handleRequest(ChannelHandlerContext ctx, HttpRequest request) {
150152
private void handleContent(ChannelHandlerContext ctx, HttpContent msg) {
151153
if (ignoreContent) {
152154
msg.release();
155+
ctx.read();
153156
} else {
154157
currentContentLength += msg.content().readableBytes();
155158
if (currentContentLength > maxContentLength) {
156159
msg.release();
157160
ctx.writeAndFlush(TOO_LARGE_CLOSE.retainedDuplicate()).addListener(ChannelFutureListener.CLOSE);
161+
ctx.read();
158162
} else {
159163
ctx.fireChannelRead(msg);
160164
}

0 commit comments

Comments
 (0)