kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 93c7dfb4d9c2b95caeaa9a9438f1144c707f250069433c3d4492e4030fd2e6b9 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-10-30 16:31 +01:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-10-30 16:31 +01:00 |
| Parents | d95f5db298a6, ae7b4cb11d32 |
8 files changed
modified changelog/README.md +1 −0
| @@ -5,6 +5,7 @@ | ||
| 5 | 5 | - [bug] JAVA-950: Fix Cluster.connect with a case-sensitive keyspace. |
| 6 | 6 | - [improvement] JAVA-920: Downgrade "error creating pool" message to WARN. |
| 7 | 7 | - [bug] JAVA-954: Don't trigger reconnection before initialization complete. |
| 8 | +- [improvement] JAVA-914: Avoid rejected tasks at shutdown. | |
| 8 | 9 | |
| 9 | 10 | |
| 10 | 11 | ### 2.0.11 |
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +18 −11
| @@ -47,7 +47,9 @@ | ||
| 47 | 47 | import static io.netty.handler.timeout.IdleState.ALL_IDLE; |
| 48 | 48 | |
| 49 | 49 | import com.datastax.driver.core.Responses.Result.SetKeyspace; |
| 50 | -import com.datastax.driver.core.exceptions.*; | |
| 50 | +import com.datastax.driver.core.exceptions.AuthenticationException; | |
| 51 | +import com.datastax.driver.core.exceptions.DriverException; | |
| 52 | +import com.datastax.driver.core.exceptions.DriverInternalError; | |
| 51 | 53 | import com.datastax.driver.core.utils.MoreFutures; |
| 52 | 54 | |
| 53 | 55 | import static com.datastax.driver.core.Message.Response.Type.ERROR; |
| @@ -568,12 +570,14 @@ | ||
| 568 | 570 | // requires its writeLock. |
| 569 | 571 | // Therefore if multiple connections in the same pool get a write error, they could deadlock; |
| 570 | 572 | // we run defunct on a separate thread to avoid that. |
| 571 | - factory.manager.executor.execute(new Runnable() { | |
| 572 | - @Override | |
| 573 | - public void run() { | |
| 574 | - handler.callback.onException(Connection.this, defunct(ce), latency, handler.retryCount); | |
| 575 | - } | |
| 576 | - }); | |
| 573 | + ListeningExecutorService executor = factory.manager.executor; | |
| 574 | + if (!executor.isShutdown()) | |
| 575 | + executor.execute(new Runnable() { | |
| 576 | + @Override | |
| 577 | + public void run() { | |
| 578 | + handler.callback.onException(Connection.this, defunct(ce), latency, handler.retryCount); | |
| 579 | + } | |
| 580 | + }); | |
| 577 | 581 | } else { |
| 578 | 582 | logger.trace("{}, stream {}, request sent successfully", Connection.this, request.getStreamId()); |
| 579 | 583 | } |
| @@ -862,9 +866,12 @@ | ||
| 862 | 866 | boolean doneWork = false; |
| 863 | 867 | FlushItem flush; |
| 864 | 868 | while (null != (flush = queued.poll())) { |
| 865 | - channels.add(flush.channel); | |
| 866 | - flush.channel.write(flush.request).addListener(flush.listener); | |
| 867 | - doneWork = true; | |
| 869 | + Channel channel = flush.channel; | |
| 870 | + if (channel.isActive()) { | |
| 871 | + channels.add(channel); | |
| 872 | + channel.write(flush.request).addListener(flush.listener); | |
| 873 | + doneWork = true; | |
| 874 | + } | |
| 868 | 875 | } |
| 869 | 876 | |
| 870 | 877 | // Always flush what we have (don't artificially delay to try to coalesce more messages) |
| @@ -884,7 +891,7 @@ | ||
| 884 | 891 | } |
| 885 | 892 | |
| 886 | 893 | EventLoop eventLoop = eventLoopRef.get(); |
| 887 | - if (eventLoop != null) { | |
| 894 | + if (eventLoop != null && !eventLoop.isShuttingDown()) { | |
| 888 | 895 | eventLoop.schedule(this, 10000, TimeUnit.NANOSECONDS); |
| 889 | 896 | } |
| 890 | 897 | } |
modified driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java +1 −1
| @@ -509,7 +509,7 @@ | ||
| 509 | 509 | } |
| 510 | 510 | |
| 511 | 511 | private void maybeSpawnNewConnection() { |
| 512 | - if (!host.convictionPolicy.canReconnectNow()) | |
| 512 | + if (isClosed() || !host.convictionPolicy.canReconnectNow()) | |
| 513 | 513 | return; |
| 514 | 514 | |
| 515 | 515 | while (true) { |
modified driver-core/src/main/java/com/datastax/driver/core/NettyOptions.java +4 −9
| @@ -183,22 +183,17 @@ | ||
| 183 | 183 | * <p> |
| 184 | 184 | * This gives the implementor a chance to close the {@link EventLoopGroup} properly, if required. |
| 185 | 185 | * <p> |
| 186 | - * The default implementation initiates a {@link EventLoopGroup#shutdownGracefully(long, long, TimeUnit) graceful shutdown} | |
| 187 | - * of the passed {@link EventLoopGroup} instance with no "quiet period" and a timeout of 15 seconds; | |
| 188 | - * then waits uninterruptibly for the shutdown to complete, or the timeout to occur, whichever happens first. | |
| 186 | + * The default implementation initiates a {@link EventLoopGroup#shutdownGracefully() graceful shutdown} | |
| 187 | + * of the passed {@link EventLoopGroup}, then waits uninterruptibly for the shutdown to complete or timeout. | |
| 189 | 188 | * <p> |
| 190 | 189 | * Implementation note: if the {@link EventLoopGroup} instance is being shared, or used for other purposes than to |
| 191 | - * coordinate Netty events for the current cluster, than it should not be shut down here; | |
| 190 | + * coordinate Netty events for the current cluster, then it should not be shut down here; | |
| 192 | 191 | * subclasses would have to override this method accordingly to take the appropriate action. |
| 193 | 192 | * |
| 194 | 193 | * @param eventLoopGroup the event loop group used by the cluster being closed |
| 195 | 194 | */ |
| 196 | 195 | public void onClusterClose(EventLoopGroup eventLoopGroup) { |
| 197 | - // shutdownGracefully with default parameters employs a quiet period of 2 seconds | |
| 198 | - // where in pre-Netty4 versions, closing a cluster instance was very quick (milliseconds). | |
| 199 | - // Since we close the channels before shutting down the eventLoopGroup, | |
| 200 | - // it is safe to reduce the quiet period to 0 seconds | |
| 201 | - eventLoopGroup.shutdownGracefully(0, 15, SECONDS).syncUninterruptibly(); | |
| 196 | + eventLoopGroup.shutdownGracefully().syncUninterruptibly(); | |
| 202 | 197 | } |
| 203 | 198 | |
| 204 | 199 | /** |
modified driver-core/src/test/java/com/datastax/driver/core/ControlConnectionTest.java +2 −0
| @@ -30,6 +30,7 @@ | ||
| 30 | 30 | |
| 31 | 31 | import static com.datastax.driver.core.Assertions.assertThat; |
| 32 | 32 | import static com.datastax.driver.core.TestUtils.nonDebouncingQueryOptions; |
| 33 | +import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions; | |
| 33 | 34 | |
| 34 | 35 | import com.datastax.driver.core.policies.DelegatingLoadBalancingPolicy; |
| 35 | 36 | import com.datastax.driver.core.policies.LoadBalancingPolicy; |
| @@ -158,6 +159,7 @@ | ||
| 158 | 159 | for(int i = 0; i < iterations; i++) { |
| 159 | 160 | Cluster cluster = Cluster.builder() |
| 160 | 161 | .addContactPoints(scassandras.addresses()) |
| 162 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 161 | 163 | .build(); |
| 162 | 164 | |
| 163 | 165 | try { |
modified driver-core/src/test/java/com/datastax/driver/core/SessionTest.java +6 −3
| @@ -33,7 +33,7 @@ | ||
| 33 | 33 | import static org.testng.Assert.assertTrue; |
| 34 | 34 | import static org.testng.Assert.fail; |
| 35 | 35 | |
| 36 | - | |
| 36 | +import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions; | |
| 37 | 37 | |
| 38 | 38 | /** |
| 39 | 39 | * Simple test of the Sessions methods against a one node cluster. |
| @@ -206,7 +206,7 @@ | ||
| 206 | 206 | // ensure sessions.size() returns to 0 with only 1 active connection |
| 207 | 207 | session.close(); |
| 208 | 208 | assertEquals(cluster.manager.sessions.size(), 0); |
| 209 | - assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1); | |
| 209 | + assertEquals((int)cluster.getMetrics().getOpenConnections().getValue(), 1); | |
| 210 | 210 | assertEquals(channelMonitor.openChannels(contactPoints).size(), 1); |
| 211 | 211 | |
| 212 | 212 | // give the driver time to close sessions |
| @@ -251,7 +251,10 @@ | ||
| 251 | 251 | |
| 252 | 252 | // Use our own cluster and session (not the ones provided by the parent class) because we want an uninitialized cluster |
| 253 | 253 | // (note the use of newSession below) |
| 254 | - final Cluster cluster = Cluster.builder().addContactPointsWithPorts(Collections.singletonList(hostAddress)).build(); | |
| 254 | + final Cluster cluster = Cluster.builder() | |
| 255 | + .addContactPointsWithPorts(Collections.singletonList(hostAddress)) | |
| 256 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 257 | + .build(); | |
| 255 | 258 | final Session session = cluster.newSession(); |
| 256 | 259 | |
| 257 | 260 | // Spawn two threads to simulate the race |
modified driver-core/src/test/java/com/datastax/driver/core/TestUtils.java +14 −0
| @@ -23,8 +23,10 @@ | ||
| 23 | 23 | import java.net.ServerSocket; |
| 24 | 24 | import java.nio.ByteBuffer; |
| 25 | 25 | import java.util.*; |
| 26 | +import java.util.concurrent.TimeUnit; | |
| 26 | 27 | |
| 27 | 28 | import com.google.common.util.concurrent.Futures; |
| 29 | +import io.netty.channel.EventLoopGroup; | |
| 28 | 30 | import org.scassandra.Scassandra; |
| 29 | 31 | import org.scassandra.ScassandraFactory; |
| 30 | 32 | import org.slf4j.Logger; |
| @@ -470,4 +472,16 @@ | ||
| 470 | 472 | .setRefreshNodeListIntervalMillis(0) |
| 471 | 473 | .setRefreshSchemaIntervalMillis(0); |
| 472 | 474 | } |
| 475 | + | |
| 476 | + /** | |
| 477 | + * A custom {@link NettyOptions} that shuts down the {@link EventLoopGroup} after | |
| 478 | + * no quiet time. This is useful for tests that consistently close clusters as | |
| 479 | + * otherwise there is a 2 second delay (from JAVA-914). | |
| 480 | + */ | |
| 481 | + public static NettyOptions nonQuietClusterCloseOptions = new NettyOptions() { | |
| 482 | + @Override | |
| 483 | + public void onClusterClose(EventLoopGroup eventLoopGroup) { | |
| 484 | + eventLoopGroup.shutdownGracefully(0, 15, TimeUnit.SECONDS).syncUninterruptibly(); | |
| 485 | + } | |
| 486 | + }; | |
| 473 | 487 | } |
modified driver-core/src/test/java/com/datastax/driver/core/utils/SocketChannelMonitor.java +7 −0
| @@ -22,6 +22,7 @@ | ||
| 22 | 22 | import com.google.common.collect.Lists; |
| 23 | 23 | import com.google.common.collect.MapMaker; |
| 24 | 24 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 25 | +import io.netty.channel.EventLoopGroup; | |
| 25 | 26 | import io.netty.channel.socket.SocketChannel; |
| 26 | 27 | import org.slf4j.Logger; |
| 27 | 28 | import org.slf4j.LoggerFactory; |
| @@ -53,10 +54,16 @@ | ||
| 53 | 54 | private final AtomicLong channelsCreated = new AtomicLong(0); |
| 54 | 55 | |
| 55 | 56 | private final NettyOptions nettyOptions = new NettyOptions() { |
| 57 | + @Override | |
| 56 | 58 | public void afterChannelInitialized(SocketChannel channel) throws Exception { |
| 57 | 59 | channels.add(channel); |
| 58 | 60 | channelsCreated.incrementAndGet(); |
| 59 | 61 | } |
| 62 | + | |
| 63 | + @Override | |
| 64 | + public void onClusterClose(EventLoopGroup eventLoopGroup) { | |
| 65 | + eventLoopGroup.shutdownGracefully(0, 15, TimeUnit.SECONDS).syncUninterruptibly(); | |
| 66 | + } | |
| 60 | 67 | }; |
| 61 | 68 | |
| 62 | 69 | @Override |