Skip to content

Commit c0bcc26

Browse files
committed
Fix the read timeout implementation in NettyStream
Both the new approach and the original one achieve the guarantee that there are no concurrent read timeouts scheduled for a channel. This is an essential property needed for a timeout implementation, let us call it "at-most-one". The original approach of achieving the at-most-one property: - Schedule timeouts only by an event loop thread. - If another thread needs to schedule a timeout, it submits a new scheduleTimeout task to the channel's event loop (asynchronous timeout scheduling). - The scheduleTimeout task schedules a new timeout if none is scheduled. The original approach allowed executions in which a scheduleTimeout task runs after completion of the read operation that submitted the task, which resulted in unexpected timeout exceptions. The new approach achieves the at-most-one property by using a lock. As a result, timeouts can be scheduled by any thread and there is no asynchronous timeout scheduling. This means we cannot miss removing a timeout because it was submitted for scheduling, but has not been scheduled yet. Other notable changes: - [related bug fix] NettyStream now must always respect the requested additional timeout. The original implementation had a chance to ignore the requested additional timeout and schedule a timeout with the default delay. This is again due to the asynchronous timeout scheduling in the original approach. - [performance optimization] Public read methods do not schedule timeouts anymore if the requested number of bytes is available right away. - [performance optimization] Netty channel handlers do not try to schedule timeouts anymore, timeouts may be scheduled only by the public read methods. Trying to schedule timeouts from the method handleReadResponse was unnecessary even in the original approach. - [performance optimization] NettyStream does not produce excessive garbage by re-creating PendingReader objects each time a new piece of data arrives for a pending reader. - [code improvement] The fields NettyStream.pendingReader, pendingException are always written/read inside synchronized blocks that use the same NettyStream object, so they can be plain. Marking them volatile is unnecessary and potentially misleading. - [code improvement] ReadTimeoutHandler was removed because it wasn't acting as a handler and was not needed. JAVA-3920
1 parent f9bf950 commit c0bcc26

File tree

2 files changed

+141
-137
lines changed

2 files changed

+141
-137
lines changed

driver-core/src/main/com/mongodb/connection/netty/NettyStream.java

Lines changed: 141 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,29 @@
2424
import com.mongodb.MongoSocketOpenException;
2525
import com.mongodb.MongoSocketReadTimeoutException;
2626
import com.mongodb.ServerAddress;
27+
import com.mongodb.annotations.ThreadSafe;
2728
import com.mongodb.connection.AsyncCompletionHandler;
2829
import com.mongodb.connection.SocketSettings;
2930
import com.mongodb.connection.SslSettings;
3031
import com.mongodb.connection.Stream;
32+
import com.mongodb.lang.Nullable;
3133
import io.netty.bootstrap.Bootstrap;
3234
import io.netty.buffer.ByteBufAllocator;
3335
import io.netty.buffer.CompositeByteBuf;
3436
import io.netty.buffer.PooledByteBufAllocator;
3537
import io.netty.channel.Channel;
3638
import io.netty.channel.ChannelFuture;
3739
import io.netty.channel.ChannelFutureListener;
38-
import io.netty.channel.ChannelHandler;
3940
import io.netty.channel.ChannelHandlerContext;
41+
import io.netty.channel.ChannelInboundHandlerAdapter;
4042
import io.netty.channel.ChannelInitializer;
4143
import io.netty.channel.ChannelOption;
44+
import io.netty.channel.ChannelPipeline;
4245
import io.netty.channel.EventLoopGroup;
4346
import io.netty.channel.SimpleChannelInboundHandler;
4447
import io.netty.channel.socket.SocketChannel;
4548
import io.netty.handler.ssl.SslHandler;
4649
import io.netty.handler.timeout.ReadTimeoutException;
47-
import io.netty.util.concurrent.EventExecutor;
4850
import org.bson.ByteBuf;
4951

5052
import javax.net.ssl.SSLContext;
@@ -58,29 +60,72 @@
5860
import java.util.List;
5961
import java.util.Queue;
6062
import java.util.concurrent.CountDownLatch;
63+
import java.util.concurrent.Future;
64+
import java.util.concurrent.ScheduledFuture;
6165

66+
import static com.mongodb.assertions.Assertions.isTrueArgument;
6267
import static com.mongodb.internal.connection.SslHelper.enableHostNameVerification;
6368
import static com.mongodb.internal.connection.SslHelper.enableSni;
6469
import static java.util.concurrent.TimeUnit.MILLISECONDS;
6570

6671
/**
6772
* A Stream implementation based on Netty 4.0.
73+
* Just like it is for the {@link java.nio.channels.AsynchronousSocketChannel},
74+
* concurrent pending<sup>1</sup> readers
75+
* (whether {@linkplain #read(int, int) synchronous} or {@linkplain #readAsync(int, AsyncCompletionHandler) asynchronous})
76+
* are not supported by {@link NettyStream}.
77+
* However, this class does not have a fail-fast mechanism checking for such situations.
78+
* <hr>
79+
* <sup>1</sup>We cannot simply say that read methods are not allowed be run concurrently because strictly speaking they are allowed,
80+
* as explained below.
81+
* <pre>{@code
82+
* NettyStream stream = ...;
83+
* stream.readAsync(1, new AsyncCompletionHandler<ByteBuf>() {//inv1
84+
* @Override
85+
* public void completed(ByteBuf o) {
86+
* stream.readAsync(//inv2
87+
* 1, ...);//ret2
88+
* }
89+
*
90+
* @Override
91+
* public void failed(Throwable t) {
92+
* }
93+
* });//ret1
94+
* }</pre>
95+
* Arrows on the diagram below represent happens-before relations.
96+
* <pre>{@code
97+
* int1 -> inv2 -> ret2
98+
* \--------> ret1
99+
* }</pre>
100+
* As shown on the diagram, the method {@link #readAsync(int, AsyncCompletionHandler)} runs concurrently with
101+
* itself in the example above. However, there are no concurrent pending readers because the second operation
102+
* is invoked after the first operation has completed reading despite the method has not returned yet.
68103
*/
69104
final class NettyStream implements Stream {
70-
private static final String READ_HANDLER_NAME = "ReadTimeoutHandler";
105+
private static final int NO_SCHEDULE_TIMEOUT = -1;
71106
private final ServerAddress address;
72107
private final SocketSettings settings;
73108
private final SslSettings sslSettings;
74109
private final EventLoopGroup workerGroup;
75110
private final Class<? extends SocketChannel> socketChannelClass;
76111
private final ByteBufAllocator allocator;
77112

78-
private volatile boolean isClosed;
113+
private boolean isClosed;
79114
private volatile Channel channel;
80115

81116
private final LinkedList<io.netty.buffer.ByteBuf> pendingInboundBuffers = new LinkedList<io.netty.buffer.ByteBuf>();
82-
private volatile PendingReader pendingReader;
83-
private volatile Throwable pendingException;
117+
/* The fields pendingReader, pendingException are always written/read inside synchronized blocks
118+
* that use the same NettyStream object, so they can be plain.*/
119+
private PendingReader pendingReader;
120+
private Throwable pendingException;
121+
/* The fields readTimeoutTask, readTimeoutMillis are each written only in the ChannelInitializer.initChannel method
122+
* (in addition to the write of the default value), and read only when NettyStream users read data,
123+
* or Netty event loop handles incoming data. Since actions done by the ChannelInitializer.initChannel method
124+
* are ordered (in the happens-before order) before user read actions and before event loop actions that handle incoming data,
125+
* these fields can be plain.*/
126+
@Nullable
127+
private ReadTimeoutTask readTimeoutTask;
128+
private long readTimeoutMillis;
84129

85130
NettyStream(final ServerAddress address, final SocketSettings settings, final SslSettings sslSettings, final EventLoopGroup workerGroup,
86131
final Class<? extends SocketChannel> socketChannelClass, final ByteBufAllocator allocator) {
@@ -143,6 +188,7 @@ private void initializeChannel(final AsyncCompletionHandler<Void> handler, final
143188
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
144189
@Override
145190
public void initChannel(final SocketChannel ch) {
191+
final ChannelPipeline pipeline = ch.pipeline();
146192
if (sslSettings.isEnabled()) {
147193
SSLEngine engine = getSslContext().createSSLEngine(address.getHost(), address.getPort());
148194
engine.setUseClientMode(true);
@@ -152,13 +198,24 @@ public void initChannel(final SocketChannel ch) {
152198
enableHostNameVerification(sslParameters);
153199
}
154200
engine.setSSLParameters(sslParameters);
155-
ch.pipeline().addFirst("ssl", new SslHandler(engine, false));
201+
pipeline.addFirst("ssl", new SslHandler(engine, false));
156202
}
203+
157204
int readTimeout = settings.getReadTimeout(MILLISECONDS);
205+
final long readTimeoutMillis;
158206
if (readTimeout > 0) {
159-
ch.pipeline().addLast(READ_HANDLER_NAME, new ReadTimeoutHandler(readTimeout));
207+
readTimeoutMillis = readTimeout;
208+
/* We need at least one handler before (in the inbound evaluation order) the InboundBufferHandler,
209+
* so that we can fire exception events (they are inbound events) using its context and the InboundBufferHandler
210+
* receives them. SslHandler is not always present, so adding a NOOP handler.*/
211+
pipeline.addLast(new ChannelInboundHandlerAdapter());
212+
readTimeoutTask = new ReadTimeoutTask(pipeline.lastContext());
213+
} else {
214+
readTimeoutMillis = NO_SCHEDULE_TIMEOUT;
160215
}
161-
ch.pipeline().addLast(new InboundBufferHandler());
216+
NettyStream.this.readTimeoutMillis = readTimeoutMillis;
217+
218+
pipeline.addLast(new InboundBufferHandler());
162219
}
163220
});
164221
final ChannelFuture channelFuture = bootstrap.connect(nextAddress);
@@ -184,9 +241,10 @@ public boolean supportsAdditionalTimeout() {
184241
}
185242

186243
@Override
187-
public ByteBuf read(final int numBytes, final int additionalTimeout) throws IOException {
244+
public ByteBuf read(final int numBytes, final int additionalTimeoutMillis) throws IOException {
245+
isTrueArgument("additionalTimeoutMillis must not be negative", additionalTimeoutMillis >= 0);
188246
FutureAsyncCompletionHandler<ByteBuf> future = new FutureAsyncCompletionHandler<ByteBuf>();
189-
readAsync(numBytes, future, additionalTimeout);
247+
readAsync(numBytes, future, combinedTimeout(readTimeoutMillis, additionalTimeoutMillis));
190248
return future.get();
191249
}
192250

@@ -211,18 +269,32 @@ public void operationComplete(final ChannelFuture future) throws Exception {
211269

212270
@Override
213271
public void readAsync(final int numBytes, final AsyncCompletionHandler<ByteBuf> handler) {
214-
readAsync(numBytes, handler, 0);
272+
readAsync(numBytes, handler, readTimeoutMillis);
215273
}
216274

217-
private void readAsync(final int numBytes, final AsyncCompletionHandler<ByteBuf> handler, final int additionalTimeout) {
218-
scheduleReadTimeout(additionalTimeout);
275+
/**
276+
* @param numBytes Must be equal to {@link #pendingReader}{@code .numBytes} when called by a Netty channel handler.
277+
* @param handler Must be equal to {@link #pendingReader}{@code .handler} when called by a Netty channel handler.
278+
* @param readTimeoutMillis Must be equal to {@link #NO_SCHEDULE_TIMEOUT} when called by a Netty channel handler.
279+
* Timeouts may be scheduled only by the public read methods. Taking into account that concurrent pending
280+
* readers are not allowed, there must not be a situation when threads attempt to schedule a timeout
281+
* before the previous one is either cancelled or completed.
282+
*/
283+
private void readAsync(final int numBytes, final AsyncCompletionHandler<ByteBuf> handler, final long readTimeoutMillis) {
219284
ByteBuf buffer = null;
220285
Throwable exceptionResult = null;
221286
synchronized (this) {
222287
exceptionResult = pendingException;
288+
PendingReader pendingReader = this.pendingReader;
223289
if (exceptionResult == null) {
224290
if (!hasBytesAvailable(numBytes)) {
225-
pendingReader = new PendingReader(numBytes, handler);
291+
if (pendingReader == null) {//called by a public read method
292+
pendingReader = new PendingReader(numBytes, handler, scheduleReadTimeout(readTimeoutTask, readTimeoutMillis));
293+
this.pendingReader = pendingReader;
294+
} else {//called by a Netty channel handler
295+
//assert pendingReader.numBytes == numBytes : "Concurrent pending readers are not allowed";
296+
//assert pendingReader.handler == handler : "Concurrent pending readers are not allowed";
297+
}
226298
} else {
227299
CompositeByteBuf composite = allocator.compositeBuffer(pendingInboundBuffers.size());
228300
int bytesNeeded = numBytes;
@@ -245,13 +317,16 @@ private void readAsync(final int numBytes, final AsyncCompletionHandler<ByteBuf>
245317
buffer = new NettyByteBuf(composite).flip();
246318
}
247319
}
320+
if (!(exceptionResult == null && buffer == null)//the read operation has completed
321+
&& pendingReader != null) {//we need to clear the pending reader
322+
cancel(pendingReader.timeout);
323+
this.pendingReader = null;
324+
}
248325
}
249326
if (exceptionResult != null) {
250-
disableReadTimeout();
251327
handler.failed(exceptionResult);
252328
}
253329
if (buffer != null) {
254-
disableReadTimeout();
255330
handler.completed(buffer);
256331
}
257332
}
@@ -275,14 +350,12 @@ private void handleReadResponse(final io.netty.buffer.ByteBuf buffer, final Thro
275350
} else {
276351
pendingException = t;
277352
}
278-
if (pendingReader != null) {
279-
localPendingReader = pendingReader;
280-
pendingReader = null;
281-
}
353+
localPendingReader = pendingReader;
282354
}
283355

284356
if (localPendingReader != null) {
285-
readAsync(localPendingReader.numBytes, localPendingReader.handler);
357+
//timeouts may be scheduled only by the public read methods
358+
readAsync(localPendingReader.numBytes, localPendingReader.handler, NO_SCHEDULE_TIMEOUT);
286359
}
287360
}
288361

@@ -358,10 +431,14 @@ public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable t)
358431
private static final class PendingReader {
359432
private final int numBytes;
360433
private final AsyncCompletionHandler<ByteBuf> handler;
434+
@Nullable
435+
private final ScheduledFuture<?> timeout;
361436

362-
private PendingReader(final int numBytes, final AsyncCompletionHandler<ByteBuf> handler) {
437+
private PendingReader(
438+
final int numBytes, final AsyncCompletionHandler<ByteBuf> handler, @Nullable final ScheduledFuture<?> timeout) {
363439
this.numBytes = numBytes;
364440
this.handler = handler;
441+
this.timeout = timeout;
365442
}
366443
}
367444

@@ -445,47 +522,52 @@ public void operationComplete(final ChannelFuture future) {
445522
}
446523
}
447524

448-
private void scheduleReadTimeout(final int additionalTimeout) {
449-
adjustTimeout(false, additionalTimeout);
525+
private static void cancel(@Nullable final Future<?> f) {
526+
if (f != null) {
527+
f.cancel(false);
528+
}
450529
}
451530

452-
private void disableReadTimeout() {
453-
adjustTimeout(true, 0);
531+
private static long combinedTimeout(final long timeout, final int additionalTimeout) {
532+
if (timeout == NO_SCHEDULE_TIMEOUT || additionalTimeout == NO_SCHEDULE_TIMEOUT) {
533+
return NO_SCHEDULE_TIMEOUT;
534+
} else {
535+
return Math.addExact(timeout, additionalTimeout);
536+
}
454537
}
455538

456-
private void adjustTimeout(final boolean disable, final int additionalTimeout) {
457-
if (isClosed) {
458-
return;
459-
}
460-
ChannelHandler timeoutHandler = channel.pipeline().get(READ_HANDLER_NAME);
461-
if (timeoutHandler != null) {
462-
final ReadTimeoutHandler readTimeoutHandler = (ReadTimeoutHandler) timeoutHandler;
463-
final ChannelHandlerContext handlerContext = channel.pipeline().context(timeoutHandler);
464-
EventExecutor executor = handlerContext.executor();
465-
466-
if (disable) {
467-
if (executor.inEventLoop()) {
468-
readTimeoutHandler.removeTimeout(handlerContext);
469-
} else {
470-
executor.submit(new Runnable() {
471-
@Override
472-
public void run() {
473-
readTimeoutHandler.removeTimeout(handlerContext);
474-
}
475-
});
476-
}
477-
} else {
478-
if (executor.inEventLoop()) {
479-
readTimeoutHandler.scheduleTimeout(handlerContext, additionalTimeout);
480-
} else {
481-
executor.submit(new Runnable() {
482-
@Override
483-
public void run() {
484-
readTimeoutHandler.scheduleTimeout(handlerContext, additionalTimeout);
485-
}
486-
});
487-
}
539+
private static ScheduledFuture<?> scheduleReadTimeout(@Nullable final ReadTimeoutTask readTimeoutTask, final long timeoutMillis) {
540+
if (timeoutMillis == NO_SCHEDULE_TIMEOUT) {
541+
return null;
542+
} else {
543+
//assert readTimeoutTask != null : "readTimeoutTask must be initialized if read timeouts are enabled";
544+
return readTimeoutTask.schedule(timeoutMillis);
545+
}
546+
}
547+
548+
@ThreadSafe
549+
private static final class ReadTimeoutTask implements Runnable {
550+
private final ChannelHandlerContext ctx;
551+
552+
private ReadTimeoutTask(final ChannelHandlerContext timeoutChannelHandlerContext) {
553+
ctx = timeoutChannelHandlerContext;
554+
}
555+
556+
@Override
557+
public void run() {
558+
try {
559+
if (ctx.channel().isOpen()) {
560+
ctx.fireExceptionCaught(ReadTimeoutException.INSTANCE);
561+
ctx.close();
488562
}
563+
} catch (final Throwable t) {
564+
ctx.fireExceptionCaught(t);
489565
}
566+
}
567+
568+
private ScheduledFuture<?> schedule(final long timeoutMillis) {
569+
//assert timeoutMillis > 0 : timeoutMillis;
570+
return ctx.executor().schedule(this, timeoutMillis, MILLISECONDS);
571+
}
490572
}
491573
}

0 commit comments

Comments
 (0)