Elffuss BraidHub

kikocis/java-driver public

JAVA-914: Avoid rejected tasks at shutdown.
Changeset97b6be87ffc64b2e9243dca1e21674881b899e3140b714c16450b85a93fee815
Authorolim7t <omichallat+github@gmail.com> · 2015-10-29 12:11 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-10-30 16:28 +01:00
Parentsd95f5db298a6

Browse the files at this changeset

4 files changed

modified changelog/README.md +1 −0
@@ -5,6 +5,7 @@
55 - [bug] JAVA-950: Fix Cluster.connect with a case-sensitive keyspace.
66 - [improvement] JAVA-920: Downgrade "error creating pool" message to WARN.
77 - [bug] JAVA-954: Don't trigger reconnection before initialization complete.
8+- [improvement] JAVA-914: Avoid rejected tasks at shutdown.
89
910
1011 ### 2.0.11
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +18 −11
@@ -47,7 +47,9 @@
4747 import static io.netty.handler.timeout.IdleState.ALL_IDLE;
4848
4949 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;
5153 import com.datastax.driver.core.utils.MoreFutures;
5254
5355 import static com.datastax.driver.core.Message.Response.Type.ERROR;
@@ -568,12 +570,14 @@
568570 // requires its writeLock.
569571 // Therefore if multiple connections in the same pool get a write error, they could deadlock;
570572 // 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+ });
577581 } else {
578582 logger.trace("{}, stream {}, request sent successfully", Connection.this, request.getStreamId());
579583 }
@@ -862,9 +866,12 @@
862866 boolean doneWork = false;
863867 FlushItem flush;
864868 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+ }
868875 }
869876
870877 // Always flush what we have (don't artificially delay to try to coalesce more messages)
@@ -884,7 +891,7 @@
884891 }
885892
886893 EventLoop eventLoop = eventLoopRef.get();
887- if (eventLoop != null) {
894+ if (eventLoop != null && !eventLoop.isShuttingDown()) {
888895 eventLoop.schedule(this, 10000, TimeUnit.NANOSECONDS);
889896 }
890897 }
modified driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java +1 −1
@@ -509,7 +509,7 @@
509509 }
510510
511511 private void maybeSpawnNewConnection() {
512- if (!host.convictionPolicy.canReconnectNow())
512+ if (isClosed() || !host.convictionPolicy.canReconnectNow())
513513 return;
514514
515515 while (true) {
modified driver-core/src/main/java/com/datastax/driver/core/NettyOptions.java +4 −9
@@ -183,22 +183,17 @@
183183 * <p>
184184 * This gives the implementor a chance to close the {@link EventLoopGroup} properly, if required.
185185 * <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.
189188 * <p>
190189 * 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;
192191 * subclasses would have to override this method accordingly to take the appropriate action.
193192 *
194193 * @param eventLoopGroup the event loop group used by the cluster being closed
195194 */
196195 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();
202197 }
203198
204199 /**