Elffuss BraidHub

kikocis/java-driver public

Improve ConnectionReaper + slightly raise interval.
Changesetcd08308b337391448fdc2aa9160c05146d08b3fc3711702f64a0df824bd97e11
Authorolim7t <omichallat+github@gmail.com> · 2014-08-28 16:29 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2014-08-28 16:29 +02:00
Parents6e0f80f49a45

Browse the files at this changeset

2 files changed

modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +10 −5
@@ -2002,10 +2002,11 @@
20022002 * Periodically ensures that closed connections are properly terminated once they have no more pending requests.
20032003 *
20042004 * This is normally done when the connection errors out, or when the last request is processed; this class acts as
2005- * a last-effort protection since unterminated connections can lead to deadlocks.
2005+ * a last-effort protection since unterminated connections can lead to deadlocks. If it terminates a connection,
2006+ * this indicates a bug; warnings are logged so that this can be reported.
20062007 */
20072008 static class ConnectionReaper {
2008- private static final int INTERVAL_MS = 5000;
2009+ private static final int INTERVAL_MS = 15000;
20092010
20102011 private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1, threadFactory("Reaper-%d"));
20112012 private final Set<Connection> connections = Sets.newConcurrentHashSet();
@@ -2016,14 +2017,15 @@
20162017 @Override
20172018 public void run() {
20182019 reapConnections();
2019- executor.schedule(this, INTERVAL_MS, TimeUnit.MILLISECONDS);
2020+ if (!executor.isShutdown())
2021+ executor.schedule(this, INTERVAL_MS, TimeUnit.MILLISECONDS);
20202022 }
20212023
20222024 private final void reapConnections() {
20232025 Iterator<Connection> iterator = connections.iterator();
20242026 while (iterator.hasNext()) {
20252027 Connection connection = iterator.next();
2026- boolean terminated = connection.terminate(false);
2028+ boolean terminated = connection.terminate(false, true);
20272029 if (terminated)
20282030 iterator.remove();
20292031 }
@@ -2038,7 +2040,7 @@
20382040 if (shutdown) {
20392041 // This should not happen since the reaper is shut down after all sessions.
20402042 logger.warn("Connection registered after reaper shutdown: {}", connection);
2041- connection.terminate(true);
2043+ connection.terminate(true, true);
20422044 } else {
20432045 connections.add(connection);
20442046 }
@@ -2047,6 +2049,9 @@
20472049 void shutdown() {
20482050 shutdown = true;
20492051 executor.shutdown();
2052+ // Run the task one last time if a call to register raced with the two lines above (i.e.
2053+ // saw shutdown as false but added its connection after the last task in the executor ran).
2054+ reaperTask.run();
20502055 }
20512056 }
20522057 }
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +37 −18
@@ -72,6 +72,8 @@
7272
7373 private final AtomicReference<ConnectionCloseFuture> closeFuture = new AtomicReference<ConnectionCloseFuture>();
7474
75+ private final Object terminationLock = new Object();
76+
7577 /**
7678 * Create a new connection to a Cassandra node.
7779 *
@@ -392,6 +394,17 @@
392394 return closeFuture.get() != null;
393395 }
394396
397+ /**
398+ * Closes the connection: no new writes will be accepted after this method has returned.
399+ *
400+ * However, a closed connection might still have ongoing queries awaiting for their result.
401+ * When all these ongoing queries have completed, the underlying channel will be closed; we
402+ * refer to this final state as "terminated".
403+ *
404+ * @return a future that will complete once the connection has terminated.
405+ *
406+ * @see #terminate(boolean, boolean)
407+ */
395408 public CloseFuture closeAsync() {
396409
397410 ConnectionCloseFuture future = new ConnectionCloseFuture();
@@ -402,30 +415,36 @@
402415
403416 logger.debug("[{}] closing connection", name);
404417
405- // New writes will be refused now that the future is setup.
406- // We must now wait on the last ongoing queries to return, which will in turn trigger
407- // the closing of the channel. However, if there no ongoing query, signal now.
408- if (dispatcher.pending.isEmpty())
409- future.force();
410- else
418+ boolean terminated = terminate(false, false);
419+ if (!terminated)
411420 factory.reaper.register(this);
412421 return future;
413422 }
414423
415- boolean terminate(boolean evenIfPending) {
424+ /**
425+ * @return whether the connection has actually terminated
426+ */
427+ boolean terminate(boolean evenIfPending, boolean logWarnings) {
416428 assert isClosed();
417429 ConnectionCloseFuture future = closeFuture.get();
418430
419431 if (future.isDone()) {
420432 logger.debug("{} has already terminated", this);
421433 return true;
422- } else if (evenIfPending || dispatcher.pending.isEmpty()) {
423- logger.warn("Forcing termination of {}. This should not happen and is likely a bug, please report.", this);
424- future.force();
425- return true;
426434 } else {
427- logger.debug("Not terminating {}: there are still pending requests", this);
428- return false;
435+ // This method is used both by normal code and by ConnectionReaper. Since the latter is a bug detection
436+ // mechanism and logs warnings when it runs, we synchronize to avoid false warnings if they race.
437+ synchronized (terminationLock) {
438+ if (evenIfPending || dispatcher.pending.isEmpty()) {
439+ if (logWarnings)
440+ logger.warn("Forcing termination of {}. This should not happen and is likely a bug, please report.", this);
441+ future.force();
442+ return true;
443+ } else {
444+ logger.debug("Not terminating {}: there are still pending requests", this);
445+ return false;
446+ }
447+ }
429448 }
430449 }
431450
@@ -583,8 +602,8 @@
583602 if (releaseStreamId)
584603 streamIdHandler.release(streamId);
585604
586- if (isClosed() && pending.isEmpty())
587- closeFuture.get().force();
605+ if (isClosed())
606+ terminate(false, false);
588607 }
589608
590609 @Override
@@ -625,10 +644,10 @@
625644 handler.cancelTimeout();
626645 handler.callback.onSet(Connection.this, response, System.nanoTime() - handler.startTime);
627646
628- // If we happen to be closed and we're the last outstanding request, we need to signal the close future
647+ // If we happen to be closed and we're the last outstanding request, we need to terminate the connection
629648 // (note: this is racy as the signaling can be called more than once, but that's not a problem)
630- if (isClosed() && pending.isEmpty())
631- closeFuture.get().force();
649+ if (isClosed())
650+ terminate(false, false);
632651 }
633652 }
634653