kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 849a601f9afc2e9b28b40c384663bd333aa2172ae1fc3751000d8dbcebd8c1e4 |
|---|---|
| Author | Miroslaw Partyka <miroslaw.partyka@allegro.pl> · 2015-07-31 21:34 +02:00 |
| Committer | Miroslaw Partyka <miroslaw.partyka@allegro.pl> · 2015-08-10 15:05 +02:00 |
| Parents | 811679f7e41a |
5 files changed
modified changelog/README.md +1 −0
| @@ -33,6 +33,7 @@ | ||
| 33 | 33 | - [improvement] Downgrade "Asked to rebuild table" log from ERROR to INFO level (JAVA-861) |
| 34 | 34 | - [improvement] Provide an option to prepare statements only on one node (JAVA-797) |
| 35 | 35 | - [improvement] Provide an option to not re-prepare all statements in onUp (JAVA-658) |
| 36 | +- [improvement] Customizable creation of netty timer (JAVA-853) | |
| 36 | 37 | |
| 37 | 38 | Merged from 2.0.10_fixes branch: |
| 38 | 39 |
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +4 −3
| @@ -37,6 +37,7 @@ | ||
| 37 | 37 | import io.netty.handler.timeout.IdleStateHandler; |
| 38 | 38 | import io.netty.util.HashedWheelTimer; |
| 39 | 39 | import io.netty.util.Timeout; |
| 40 | +import io.netty.util.Timer; | |
| 40 | 41 | import io.netty.util.TimerTask; |
| 41 | 42 | import io.netty.util.concurrent.GlobalEventExecutor; |
| 42 | 43 | import javax.net.ssl.SSLEngine; |
| @@ -658,7 +659,7 @@ | ||
| 658 | 659 | |
| 659 | 660 | public static class Factory { |
| 660 | 661 | |
| 661 | - public final HashedWheelTimer timer; | |
| 662 | + public final Timer timer; | |
| 662 | 663 | |
| 663 | 664 | private final EventLoopGroup eventLoopGroup; |
| 664 | 665 | private final Class<? extends Channel> channelClass; |
| @@ -687,7 +688,7 @@ | ||
| 687 | 688 | this.nettyOptions = configuration.getNettyOptions(); |
| 688 | 689 | this.eventLoopGroup = nettyOptions.eventLoopGroup(manager.threadFactory("nio-worker")); |
| 689 | 690 | this.channelClass = nettyOptions.channelClass(); |
| 690 | - this.timer = new HashedWheelTimer(manager.threadFactory("timeouter")); | |
| 691 | + this.timer = nettyOptions.timer(manager.threadFactory("timeouter")); | |
| 691 | 692 | } |
| 692 | 693 | |
| 693 | 694 | public int getPort() { |
| @@ -816,7 +817,7 @@ | ||
| 816 | 817 | allChannels.close().awaitUninterruptibly(); |
| 817 | 818 | |
| 818 | 819 | nettyOptions.onClusterClose(eventLoopGroup); |
| 819 | - timer.stop(); | |
| 820 | + nettyOptions.onClusterClose(timer); | |
| 820 | 821 | } |
| 821 | 822 | } |
| 822 | 823 |
modified driver-core/src/main/java/com/datastax/driver/core/NettyOptions.java +44 −1
| @@ -21,13 +21,14 @@ | ||
| 21 | 21 | import static java.util.concurrent.TimeUnit.SECONDS; |
| 22 | 22 | |
| 23 | 23 | import io.netty.bootstrap.Bootstrap; |
| 24 | -import io.netty.buffer.ByteBufAllocator; | |
| 25 | 24 | import io.netty.buffer.PooledByteBufAllocator; |
| 26 | 25 | import io.netty.channel.ChannelOption; |
| 27 | 26 | import io.netty.channel.EventLoopGroup; |
| 28 | 27 | import io.netty.channel.nio.NioEventLoopGroup; |
| 29 | 28 | import io.netty.channel.socket.SocketChannel; |
| 30 | 29 | import io.netty.channel.socket.nio.NioSocketChannel; |
| 30 | +import io.netty.util.HashedWheelTimer; | |
| 31 | +import io.netty.util.Timer; | |
| 31 | 32 | |
| 32 | 33 | /** |
| 33 | 34 | * A set of hooks that allow clients to customize the driver's underlying Netty layer. |
| @@ -199,4 +200,46 @@ | ||
| 199 | 200 | // it is safe to reduce the quiet period to 0 seconds |
| 200 | 201 | eventLoopGroup.shutdownGracefully(0, 15, SECONDS).syncUninterruptibly(); |
| 201 | 202 | } |
| 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 | + } | |
| 202 | 245 | } |
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +1 −2
| @@ -27,7 +27,6 @@ | ||
| 27 | 27 | |
| 28 | 28 | import com.codahale.metrics.Timer; |
| 29 | 29 | import com.google.common.collect.Sets; |
| 30 | -import io.netty.util.HashedWheelTimer; | |
| 31 | 30 | import io.netty.util.Timeout; |
| 32 | 31 | import io.netty.util.TimerTask; |
| 33 | 32 | import org.slf4j.Logger; |
| @@ -56,7 +55,7 @@ | ||
| 56 | 55 | private final Set<SpeculativeExecution> runningExecutions = Sets.newCopyOnWriteArraySet(); |
| 57 | 56 | private final Set<Timeout> scheduledExecutions = Sets.newCopyOnWriteArraySet(); |
| 58 | 57 | private final Statement statement; |
| 59 | - private final HashedWheelTimer scheduler; | |
| 58 | + private final io.netty.util.Timer scheduler; | |
| 60 | 59 | |
| 61 | 60 | private volatile List<Host> triedHosts; |
| 62 | 61 |
modified driver-core/src/test/java/com/datastax/driver/core/NettyOptionsTest.java +9 −4
| @@ -23,6 +23,8 @@ | ||
| 23 | 23 | import io.netty.channel.EventLoopGroup; |
| 24 | 24 | import io.netty.channel.nio.NioEventLoopGroup; |
| 25 | 25 | import io.netty.channel.socket.SocketChannel; |
| 26 | +import io.netty.util.HashedWheelTimer; | |
| 27 | +import io.netty.util.Timer; | |
| 26 | 28 | import org.mockito.ArgumentCaptor; |
| 27 | 29 | import org.mockito.invocation.InvocationOnMock; |
| 28 | 30 | import org.mockito.stubbing.Answer; |
| @@ -53,7 +55,9 @@ | ||
| 53 | 55 | try { |
| 54 | 56 | NettyOptions nettyOptions = mock(NettyOptions.class, CALLS_REAL_METHODS.get()); |
| 55 | 57 | 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)); | |
| 57 | 61 | final ChannelHandler handler = mock(ChannelHandler.class); |
| 58 | 62 | doAnswer(new Answer() { |
| 59 | 63 | @Override |
| @@ -74,17 +78,18 @@ | ||
| 74 | 78 | cluster.connect();// force session creation to populate pools |
| 75 | 79 | cluster.close(); |
| 76 | 80 | // then |
| 77 | - ArgumentCaptor<EventLoopGroup> captor = ArgumentCaptor.forClass(EventLoopGroup.class); | |
| 78 | 81 | verify(nettyOptions, times(1)).eventLoopGroup(any(ThreadFactory.class)); |
| 79 | 82 | verify(nettyOptions, times(1)).channelClass(); |
| 83 | + verify(nettyOptions, times(1)).timer(any(ThreadFactory.class)); | |
| 80 | 84 | // per-connection hooks will be called coreConnections * hosts + 1 times: |
| 81 | 85 | // the extra call is for the control connection |
| 82 | 86 | verify(nettyOptions, times(expectedNumberOfCalls)).afterBootstrapInitialized(any(Bootstrap.class)); |
| 83 | 87 | verify(nettyOptions, times(expectedNumberOfCalls)).afterChannelInitialized(any(SocketChannel.class)); |
| 84 | 88 | verify(handler, times(expectedNumberOfCalls)).handlerAdded(any(ChannelHandlerContext.class)); |
| 85 | 89 | 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); | |
| 88 | 93 | } finally { |
| 89 | 94 | if (cluster != null) |
| 90 | 95 | cluster.close(); |