kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | cd08308b337391448fdc2aa9160c05146d08b3fc3711702f64a0df824bd97e11 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-08-28 16:29 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-08-28 16:29 +02:00 |
| Parents | 6e0f80f49a45 |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +10 −5
| @@ -2002,10 +2002,11 @@ | ||
| 2002 | 2002 | * Periodically ensures that closed connections are properly terminated once they have no more pending requests. |
| 2003 | 2003 | * |
| 2004 | 2004 | * 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. | |
| 2006 | 2007 | */ |
| 2007 | 2008 | static class ConnectionReaper { |
| 2008 | - private static final int INTERVAL_MS = 5000; | |
| 2009 | + private static final int INTERVAL_MS = 15000; | |
| 2009 | 2010 | |
| 2010 | 2011 | private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1, threadFactory("Reaper-%d")); |
| 2011 | 2012 | private final Set<Connection> connections = Sets.newConcurrentHashSet(); |
| @@ -2016,14 +2017,15 @@ | ||
| 2016 | 2017 | @Override |
| 2017 | 2018 | public void run() { |
| 2018 | 2019 | reapConnections(); |
| 2019 | - executor.schedule(this, INTERVAL_MS, TimeUnit.MILLISECONDS); | |
| 2020 | + if (!executor.isShutdown()) | |
| 2021 | + executor.schedule(this, INTERVAL_MS, TimeUnit.MILLISECONDS); | |
| 2020 | 2022 | } |
| 2021 | 2023 | |
| 2022 | 2024 | private final void reapConnections() { |
| 2023 | 2025 | Iterator<Connection> iterator = connections.iterator(); |
| 2024 | 2026 | while (iterator.hasNext()) { |
| 2025 | 2027 | Connection connection = iterator.next(); |
| 2026 | - boolean terminated = connection.terminate(false); | |
| 2028 | + boolean terminated = connection.terminate(false, true); | |
| 2027 | 2029 | if (terminated) |
| 2028 | 2030 | iterator.remove(); |
| 2029 | 2031 | } |
| @@ -2038,7 +2040,7 @@ | ||
| 2038 | 2040 | if (shutdown) { |
| 2039 | 2041 | // This should not happen since the reaper is shut down after all sessions. |
| 2040 | 2042 | logger.warn("Connection registered after reaper shutdown: {}", connection); |
| 2041 | - connection.terminate(true); | |
| 2043 | + connection.terminate(true, true); | |
| 2042 | 2044 | } else { |
| 2043 | 2045 | connections.add(connection); |
| 2044 | 2046 | } |
| @@ -2047,6 +2049,9 @@ | ||
| 2047 | 2049 | void shutdown() { |
| 2048 | 2050 | shutdown = true; |
| 2049 | 2051 | 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(); | |
| 2050 | 2055 | } |
| 2051 | 2056 | } |
| 2052 | 2057 | } |
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +37 −18
| @@ -72,6 +72,8 @@ | ||
| 72 | 72 | |
| 73 | 73 | private final AtomicReference<ConnectionCloseFuture> closeFuture = new AtomicReference<ConnectionCloseFuture>(); |
| 74 | 74 | |
| 75 | + private final Object terminationLock = new Object(); | |
| 76 | + | |
| 75 | 77 | /** |
| 76 | 78 | * Create a new connection to a Cassandra node. |
| 77 | 79 | * |
| @@ -392,6 +394,17 @@ | ||
| 392 | 394 | return closeFuture.get() != null; |
| 393 | 395 | } |
| 394 | 396 | |
| 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 | + */ | |
| 395 | 408 | public CloseFuture closeAsync() { |
| 396 | 409 | |
| 397 | 410 | ConnectionCloseFuture future = new ConnectionCloseFuture(); |
| @@ -402,30 +415,36 @@ | ||
| 402 | 415 | |
| 403 | 416 | logger.debug("[{}] closing connection", name); |
| 404 | 417 | |
| 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) | |
| 411 | 420 | factory.reaper.register(this); |
| 412 | 421 | return future; |
| 413 | 422 | } |
| 414 | 423 | |
| 415 | - boolean terminate(boolean evenIfPending) { | |
| 424 | + /** | |
| 425 | + * @return whether the connection has actually terminated | |
| 426 | + */ | |
| 427 | + boolean terminate(boolean evenIfPending, boolean logWarnings) { | |
| 416 | 428 | assert isClosed(); |
| 417 | 429 | ConnectionCloseFuture future = closeFuture.get(); |
| 418 | 430 | |
| 419 | 431 | if (future.isDone()) { |
| 420 | 432 | logger.debug("{} has already terminated", this); |
| 421 | 433 | 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; | |
| 426 | 434 | } 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 | + } | |
| 429 | 448 | } |
| 430 | 449 | } |
| 431 | 450 | |
| @@ -583,8 +602,8 @@ | ||
| 583 | 602 | if (releaseStreamId) |
| 584 | 603 | streamIdHandler.release(streamId); |
| 585 | 604 | |
| 586 | - if (isClosed() && pending.isEmpty()) | |
| 587 | - closeFuture.get().force(); | |
| 605 | + if (isClosed()) | |
| 606 | + terminate(false, false); | |
| 588 | 607 | } |
| 589 | 608 | |
| 590 | 609 | @Override |
| @@ -625,10 +644,10 @@ | ||
| 625 | 644 | handler.cancelTimeout(); |
| 626 | 645 | handler.callback.onSet(Connection.this, response, System.nanoTime() - handler.startTime); |
| 627 | 646 | |
| 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 | |
| 629 | 648 | // (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); | |
| 632 | 651 | } |
| 633 | 652 | } |
| 634 | 653 |