Elffuss BraidHub

kikocis/java-driver public

Customizable creation of netty timer (JAVA-853)
Changeset849a601f9afc2e9b28b40c384663bd333aa2172ae1fc3751000d8dbcebd8c1e4
AuthorMiroslaw Partyka <miroslaw.partyka@allegro.pl> · 2015-07-31 21:34 +02:00
CommitterMiroslaw Partyka <miroslaw.partyka@allegro.pl> · 2015-08-10 15:05 +02:00
Parents811679f7e41a

Browse the files at this changeset

5 files changed

modified changelog/README.md +1 −0
@@ -33,6 +33,7 @@
3333 - [improvement] Downgrade "Asked to rebuild table" log from ERROR to INFO level (JAVA-861)
3434 - [improvement] Provide an option to prepare statements only on one node (JAVA-797)
3535 - [improvement] Provide an option to not re-prepare all statements in onUp (JAVA-658)
36+- [improvement] Customizable creation of netty timer (JAVA-853)
3637
3738 Merged from 2.0.10_fixes branch:
3839
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +4 −3
@@ -37,6 +37,7 @@
3737 import io.netty.handler.timeout.IdleStateHandler;
3838 import io.netty.util.HashedWheelTimer;
3939 import io.netty.util.Timeout;
40+import io.netty.util.Timer;
4041 import io.netty.util.TimerTask;
4142 import io.netty.util.concurrent.GlobalEventExecutor;
4243 import javax.net.ssl.SSLEngine;
@@ -658,7 +659,7 @@
658659
659660 public static class Factory {
660661
661- public final HashedWheelTimer timer;
662+ public final Timer timer;
662663
663664 private final EventLoopGroup eventLoopGroup;
664665 private final Class<? extends Channel> channelClass;
@@ -687,7 +688,7 @@
687688 this.nettyOptions = configuration.getNettyOptions();
688689 this.eventLoopGroup = nettyOptions.eventLoopGroup(manager.threadFactory("nio-worker"));
689690 this.channelClass = nettyOptions.channelClass();
690- this.timer = new HashedWheelTimer(manager.threadFactory("timeouter"));
691+ this.timer = nettyOptions.timer(manager.threadFactory("timeouter"));
691692 }
692693
693694 public int getPort() {
@@ -816,7 +817,7 @@
816817 allChannels.close().awaitUninterruptibly();
817818
818819 nettyOptions.onClusterClose(eventLoopGroup);
819- timer.stop();
820+ nettyOptions.onClusterClose(timer);
820821 }
821822 }
822823
modified driver-core/src/main/java/com/datastax/driver/core/NettyOptions.java +44 −1
@@ -21,13 +21,14 @@
2121 import static java.util.concurrent.TimeUnit.SECONDS;
2222
2323 import io.netty.bootstrap.Bootstrap;
24-import io.netty.buffer.ByteBufAllocator;
2524 import io.netty.buffer.PooledByteBufAllocator;
2625 import io.netty.channel.ChannelOption;
2726 import io.netty.channel.EventLoopGroup;
2827 import io.netty.channel.nio.NioEventLoopGroup;
2928 import io.netty.channel.socket.SocketChannel;
3029 import io.netty.channel.socket.nio.NioSocketChannel;
30+import io.netty.util.HashedWheelTimer;
31+import io.netty.util.Timer;
3132
3233 /**
3334 * A set of hooks that allow clients to customize the driver's underlying Netty layer.
@@ -199,4 +200,46 @@
199200 // it is safe to reduce the quiet period to 0 seconds
200201 eventLoopGroup.shutdownGracefully(0, 15, SECONDS).syncUninterruptibly();
201202 }
203+
204+ /**
205+ * Return the {@link Timer} instance used by Read Timeouts and Speculative Execution.
206+ * <p>
207+ * This hook is invoked only once at {@link Cluster} initialization;
208+ * the returned instance will be kept in use throughout the cluster lifecycle.
209+ * <p>
210+ * Typically, implementors would return a newly-created instance;
211+ * it is however possible to re-use a shared instance, but in this
212+ * case implementors should also override {@link #onClusterClose(Timer)}
213+ * to prevent the shared instance to be closed when the cluster is closed.
214+ * <p>
215+ * The default implementation returns a new instance created by {@link HashedWheelTimer#HashedWheelTimer(ThreadFactory)}.
216+ *
217+ * @param threadFactory The {@link ThreadFactory} to use when creating a new {@link HashedWheelTimer} instance;
218+ * The driver will provide its own internal thread factory here.
219+ * It is safe to ignore it and use another thread factory.
220+ * @return the {@link Timer} instance to use.
221+ */
222+ public Timer timer(ThreadFactory threadFactory) {
223+ return new HashedWheelTimer(threadFactory);
224+ }
225+
226+ /**
227+ * Hook invoked when the cluster is shutting down after a call to {@link Cluster#close()}.
228+ * <p>
229+ * This is guaranteed to be called only after all connections have been individually
230+ * closed, and their channels closed, and only once per {@link Timer} instance.
231+ * <p>
232+ * This gives the implementor a chance to close the {@link Timer} properly, if required.
233+ * <p>
234+ * The default implementation calls a {@link Timer#stop()} of the passed {@link Timer} instance.
235+ * <p>
236+ * Implementation note: if the {@link Timer} instance is being shared, or used for other purposes than to
237+ * schedule actions for the current cluster, than it should not be stopped here;
238+ * subclasses would have to override this method accordingly to take the appropriate action.
239+ *
240+ * @param timer the timer used by the cluster being closed
241+ */
242+ public void onClusterClose(Timer timer) {
243+ timer.stop();
244+ }
202245 }
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +1 −2
@@ -27,7 +27,6 @@
2727
2828 import com.codahale.metrics.Timer;
2929 import com.google.common.collect.Sets;
30-import io.netty.util.HashedWheelTimer;
3130 import io.netty.util.Timeout;
3231 import io.netty.util.TimerTask;
3332 import org.slf4j.Logger;
@@ -56,7 +55,7 @@
5655 private final Set<SpeculativeExecution> runningExecutions = Sets.newCopyOnWriteArraySet();
5756 private final Set<Timeout> scheduledExecutions = Sets.newCopyOnWriteArraySet();
5857 private final Statement statement;
59- private final HashedWheelTimer scheduler;
58+ private final io.netty.util.Timer scheduler;
6059
6160 private volatile List<Host> triedHosts;
6261
modified driver-core/src/test/java/com/datastax/driver/core/NettyOptionsTest.java +9 −4
@@ -23,6 +23,8 @@
2323 import io.netty.channel.EventLoopGroup;
2424 import io.netty.channel.nio.NioEventLoopGroup;
2525 import io.netty.channel.socket.SocketChannel;
26+import io.netty.util.HashedWheelTimer;
27+import io.netty.util.Timer;
2628 import org.mockito.ArgumentCaptor;
2729 import org.mockito.invocation.InvocationOnMock;
2830 import org.mockito.stubbing.Answer;
@@ -53,7 +55,9 @@
5355 try {
5456 NettyOptions nettyOptions = mock(NettyOptions.class, CALLS_REAL_METHODS.get());
5557 EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
56- when(nettyOptions.eventLoopGroup(any(ThreadFactory.class))).thenReturn(eventLoopGroup);
58+ Timer timer = new HashedWheelTimer();
59+ doReturn(eventLoopGroup).when(nettyOptions).eventLoopGroup(any(ThreadFactory.class));
60+ doReturn(timer).when(nettyOptions).timer(any(ThreadFactory.class));
5761 final ChannelHandler handler = mock(ChannelHandler.class);
5862 doAnswer(new Answer() {
5963 @Override
@@ -74,17 +78,18 @@
7478 cluster.connect();// force session creation to populate pools
7579 cluster.close();
7680 // then
77- ArgumentCaptor<EventLoopGroup> captor = ArgumentCaptor.forClass(EventLoopGroup.class);
7881 verify(nettyOptions, times(1)).eventLoopGroup(any(ThreadFactory.class));
7982 verify(nettyOptions, times(1)).channelClass();
83+ verify(nettyOptions, times(1)).timer(any(ThreadFactory.class));
8084 // per-connection hooks will be called coreConnections * hosts + 1 times:
8185 // the extra call is for the control connection
8286 verify(nettyOptions, times(expectedNumberOfCalls)).afterBootstrapInitialized(any(Bootstrap.class));
8387 verify(nettyOptions, times(expectedNumberOfCalls)).afterChannelInitialized(any(SocketChannel.class));
8488 verify(handler, times(expectedNumberOfCalls)).handlerAdded(any(ChannelHandlerContext.class));
8589 verify(handler, times(expectedNumberOfCalls)).handlerRemoved(any(ChannelHandlerContext.class));
86- verify(nettyOptions, times(1)).onClusterClose(captor.capture());
87- assertThat(captor.getValue()).isSameAs(eventLoopGroup);
90+ verify(nettyOptions, times(1)).onClusterClose(eventLoopGroup);
91+ verify(nettyOptions, times(1)).onClusterClose(timer);
92+ verifyNoMoreInteractions(nettyOptions);
8893 } finally {
8994 if (cluster != null)
9095 cluster.close();