Elffuss BraidHub

kikocis/java-driver public

Clean up unsubmitted futures at shutdown.

This can cause thread starvation in some cases.
Changeset390663e21058d7912f97a401fe72a48f672554b7f301800ba41187d006990801
Authorolim7t <omichallat+github@gmail.com> · 2014-10-31 23:34 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2014-11-03 11:32 +01:00
Parentsa8e90ec9201c

Browse the files at this changeset

1 file changed

modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +13 −4
@@ -1289,9 +1289,9 @@
12891289
12901290 // If we're shutting down, there is no point in waiting on scheduled reconnections, nor on notifications
12911291 // delivery or blocking tasks so we use shutdownNow
1292- reconnectionExecutor.shutdownNow();
1293- scheduledTasksExecutor.shutdownNow();
1294- blockingExecutor.shutdownNow();
1292+ shutdownNow(reconnectionExecutor);
1293+ shutdownNow(scheduledTasksExecutor);
1294+ shutdownNow(blockingExecutor);
12951295
12961296 // but for the worker executor, we want to let submitted tasks finish unless the shutdown is forced.
12971297 executor.shutdown();
@@ -1319,6 +1319,15 @@
13191319 : closeFuture.get(); // We raced, it's ok, return the future that was actually set
13201320 }
13211321
1322+ private void shutdownNow(ExecutorService executor) {
1323+ List<Runnable> pendingTasks = executor.shutdownNow();
1324+ // If some tasks were submitted to this executor but not yet commenced, make sure the corresponding futures complete
1325+ for (Runnable pendingTask : pendingTasks) {
1326+ if (pendingTask instanceof FutureTask<?>)
1327+ ((FutureTask<?>)pendingTask).cancel(false);
1328+ }
1329+ }
1330+
13221331 void logUnsupportedVersionProtocol(Host host) {
13231332 logger.warn("Detected added or restarted Cassandra host {} but ignoring it since it does not support the version 2 of the native "
13241333 + "protocol which is currently in use. If you want to force the use of the version 1 of the native protocol, use "
@@ -2096,7 +2105,7 @@
20962105 @Override
20972106 public CloseFuture force() {
20982107 // The only ExecutorService we haven't forced yet is executor
2099- executor.shutdownNow();
2108+ shutdownNow(executor);
21002109 return super.force();
21012110 }
21022111