kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 6068c037a5f559e17764f9f60113400ca49e2ea3bdb1cee29bef6318f1d4d8f0 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-12-06 19:51 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-12-09 18:30 +01:00 |
| Parents | a8f731741d38 |
3 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | - [improvement] Target schema refreshes more precisely (JAVA-543) |
| 9 | 9 | - [bug] Don't check rpc_address for control host (JAVA-546) |
| 10 | 10 | - [improvement] Improve message of NoHostAvailableException (JAVA-409) |
| 11 | +- [bug] Rework connection reaper to avoid deadlock (JAVA-556) | |
| 11 | 12 | |
| 12 | 13 | |
| 13 | 14 | 2.0.8: |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +18 −16
| @@ -20,6 +20,7 @@ | ||
| 20 | 20 | import java.net.InetSocketAddress; |
| 21 | 21 | import java.net.UnknownHostException; |
| 22 | 22 | import java.util.*; |
| 23 | +import java.util.Map.Entry; | |
| 23 | 24 | import java.util.concurrent.*; |
| 24 | 25 | import java.util.concurrent.atomic.AtomicInteger; |
| 25 | 26 | import java.util.concurrent.atomic.AtomicReference; |
| @@ -2182,45 +2183,46 @@ | ||
| 2182 | 2183 | * This is normally done when the connection errors out, or when the last request is processed; this class acts as |
| 2183 | 2184 | * a last-effort protection since unterminated connections can lead to deadlocks. If it terminates a connection, |
| 2184 | 2185 | * this indicates a bug; warnings are logged so that this can be reported. |
| 2186 | + * | |
| 2187 | + * @see Connection#tryTerminate(boolean) | |
| 2185 | 2188 | */ |
| 2186 | 2189 | static class ConnectionReaper { |
| 2187 | 2190 | private static final int INTERVAL_MS = 15000; |
| 2188 | 2191 | |
| 2189 | 2192 | private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1, threadFactory("Reaper-%d")); |
| 2190 | - private final Set<Connection> connections = Sets.newSetFromMap(new ConcurrentHashMap<Connection, Boolean>()); | |
| 2193 | + private final Map<Connection, Long> connections = new ConcurrentHashMap<Connection, Long>(); | |
| 2191 | 2194 | |
| 2192 | 2195 | private volatile boolean shutdown; |
| 2193 | 2196 | |
| 2194 | 2197 | private final Runnable reaperTask = new Runnable() { |
| 2195 | 2198 | @Override |
| 2196 | 2199 | public void run() { |
| 2197 | - reapConnections(); | |
| 2198 | - if (!executor.isShutdown()) | |
| 2199 | - executor.schedule(this, INTERVAL_MS, TimeUnit.MILLISECONDS); | |
| 2200 | - } | |
| 2201 | - | |
| 2202 | - private final void reapConnections() { | |
| 2203 | - Iterator<Connection> iterator = connections.iterator(); | |
| 2200 | + long now = System.currentTimeMillis(); | |
| 2201 | + Iterator<Entry<Connection, Long>> iterator = connections.entrySet().iterator(); | |
| 2204 | 2202 | while (iterator.hasNext()) { |
| 2205 | - Connection connection = iterator.next(); | |
| 2206 | - boolean terminated = connection.terminate(false, true); | |
| 2207 | - if (terminated) | |
| 2208 | - iterator.remove(); | |
| 2203 | + Entry<Connection, Long> entry = iterator.next(); | |
| 2204 | + Connection connection = entry.getKey(); | |
| 2205 | + Long terminateTime = entry.getValue(); | |
| 2206 | + if (terminateTime <= now) { | |
| 2207 | + boolean terminated = connection.tryTerminate(true); | |
| 2208 | + if (terminated) | |
| 2209 | + iterator.remove(); | |
| 2210 | + } | |
| 2209 | 2211 | } |
| 2210 | 2212 | } |
| 2211 | 2213 | }; |
| 2212 | 2214 | |
| 2213 | 2215 | ConnectionReaper() { |
| 2214 | - executor.schedule(reaperTask, INTERVAL_MS, TimeUnit.MILLISECONDS); | |
| 2216 | + executor.scheduleWithFixedDelay(reaperTask, INTERVAL_MS, INTERVAL_MS, TimeUnit.MILLISECONDS); | |
| 2215 | 2217 | } |
| 2216 | 2218 | |
| 2217 | - void register(Connection connection) { | |
| 2219 | + void register(Connection connection, long terminateTime) { | |
| 2218 | 2220 | if (shutdown) { |
| 2219 | 2221 | // This should not happen since the reaper is shut down after all sessions. |
| 2220 | 2222 | logger.warn("Connection registered after reaper shutdown: {}", connection); |
| 2221 | - connection.terminate(true, true); | |
| 2223 | + connection.tryTerminate(true); | |
| 2222 | 2224 | } else { |
| 2223 | - connections.add(connection); | |
| 2225 | + connections.put(connection, terminateTime); | |
| 2224 | 2226 | } |
| 2225 | 2227 | } |
| 2226 | 2228 |
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +28 −22
| @@ -71,8 +71,6 @@ | ||
| 71 | 71 | |
| 72 | 72 | private final AtomicReference<ConnectionCloseFuture> closeFuture = new AtomicReference<ConnectionCloseFuture>(); |
| 73 | 73 | |
| 74 | - private final Object terminationLock = new Object(); | |
| 75 | - | |
| 76 | 74 | /** |
| 77 | 75 | * Create a new connection to a Cassandra node. |
| 78 | 76 | * |
| @@ -409,7 +407,7 @@ | ||
| 409 | 407 | * |
| 410 | 408 | * @return a future that will complete once the connection has terminated. |
| 411 | 409 | * |
| 412 | - * @see #terminate(boolean, boolean) | |
| 410 | + * @see #tryTerminate(boolean) | |
| 413 | 411 | */ |
| 414 | 412 | public CloseFuture closeAsync() { |
| 415 | 413 | |
| @@ -421,16 +419,28 @@ | ||
| 421 | 419 | |
| 422 | 420 | logger.debug("{} closing connection", this); |
| 423 | 421 | |
| 424 | - boolean terminated = terminate(false, false); | |
| 425 | - if (!terminated) | |
| 426 | - factory.reaper.register(this); | |
| 422 | + boolean terminated = tryTerminate(false); | |
| 423 | + if (!terminated) { | |
| 424 | + // The time by which all pending requests should have normally completed (use twice the read timeout for a generous | |
| 425 | + // estimate -- note that this does not cover the eventuality that read timeout is updated dynamically, but we can live | |
| 426 | + // with that). | |
| 427 | + long terminateTime = System.currentTimeMillis() + 2 * factory.getReadTimeoutMillis(); | |
| 428 | + factory.reaper.register(this, terminateTime); | |
| 429 | + } | |
| 427 | 430 | return future; |
| 428 | 431 | } |
| 429 | 432 | |
| 430 | 433 | /** |
| 431 | - * @return whether the connection has actually terminated | |
| 434 | + * Tries to terminate a closed connection, i.e. release system resources. | |
| 435 | + * | |
| 436 | + * This is called both by "normal" code and by {@link Cluster.ConnectionReaper}. | |
| 437 | + * | |
| 438 | + * @param force whether to proceed if there are still outstanding requests. | |
| 439 | + * @return whether the connection has actually terminated. | |
| 440 | + * | |
| 441 | + * @see #closeAsync() | |
| 432 | 442 | */ |
| 433 | - boolean terminate(boolean evenIfPending, boolean logWarnings) { | |
| 443 | + boolean tryTerminate(boolean force) { | |
| 434 | 444 | assert isClosed(); |
| 435 | 445 | ConnectionCloseFuture future = closeFuture.get(); |
| 436 | 446 | |
| @@ -438,18 +448,14 @@ | ||
| 438 | 448 | logger.debug("{} has already terminated", this); |
| 439 | 449 | return true; |
| 440 | 450 | } else { |
| 441 | - // This method is used both by normal code and by ConnectionReaper. Since the latter is a bug detection | |
| 442 | - // mechanism and logs warnings when it runs, we synchronize to avoid false warnings if they race. | |
| 443 | - synchronized (terminationLock) { | |
| 444 | - if (evenIfPending || dispatcher.pending.isEmpty()) { | |
| 445 | - if (logWarnings) | |
| 446 | - logger.warn("Forcing termination of {}. This should not happen and is likely a bug, please report.", this); | |
| 447 | - future.force(); | |
| 448 | - return true; | |
| 449 | - } else { | |
| 450 | - logger.debug("Not terminating {}: there are still pending requests", this); | |
| 451 | - return false; | |
| 452 | - } | |
| 451 | + if (force || dispatcher.pending.isEmpty()) { | |
| 452 | + if (force) | |
| 453 | + logger.warn("Forcing termination of {}. This should not happen and is likely a bug, please report.", this); | |
| 454 | + future.force(); | |
| 455 | + return true; | |
| 456 | + } else { | |
| 457 | + logger.debug("Not terminating {}: there are still pending requests", this); | |
| 458 | + return false; | |
| 453 | 459 | } |
| 454 | 460 | } |
| 455 | 461 | } |
| @@ -609,7 +615,7 @@ | ||
| 609 | 615 | streamIdHandler.release(streamId); |
| 610 | 616 | |
| 611 | 617 | if (isClosed()) |
| 612 | - terminate(false, false); | |
| 618 | + tryTerminate(false); | |
| 613 | 619 | } |
| 614 | 620 | |
| 615 | 621 | @Override |
| @@ -653,7 +659,7 @@ | ||
| 653 | 659 | // If we happen to be closed and we're the last outstanding request, we need to terminate the connection |
| 654 | 660 | // (note: this is racy as the signaling can be called more than once, but that's not a problem) |
| 655 | 661 | if (isClosed()) |
| 656 | - terminate(false, false); | |
| 662 | + tryTerminate(false); | |
| 657 | 663 | } |
| 658 | 664 | } |
| 659 | 665 |