kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 77c0c373cb1a00679adf8262466d880119ea80fbc8234bdb95b5e9e87eaf5e67 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-03-25 15:24 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-03-25 15:25 +01:00 |
| Parents | 96c0afc7f127, fc99c0c842e2 |
4 files changed
modified changelog/README.md +2 −0
| @@ -47,6 +47,7 @@ | ||
| 47 | 47 | |
| 48 | 48 | - [bug] JAVA-994: Don't call on(Up|Down|Add|Remove) methods if Cluster is closed/closing. |
| 49 | 49 | - [improvement] JAVA-805: Document that metrics are null until Cluster is initialized. |
| 50 | +- [bug] JAVA-1072: Ensure defunct connections are properly evicted from the pool. | |
| 50 | 51 | |
| 51 | 52 | |
| 52 | 53 | ### 2.1.9 |
| @@ -388,6 +389,7 @@ | ||
| 388 | 389 | |
| 389 | 390 | - [bug] JAVA-994: Don't call on(Up|Down|Add|Remove) methods if Cluster is closed/closing. |
| 390 | 391 | - [improvement] JAVA-805: Document that metrics are null until Cluster is initialized. |
| 392 | +- [bug] JAVA-1072: Ensure defunct connections are properly evicted from the pool. | |
| 391 | 393 | |
| 392 | 394 | ### 2.0.12 |
| 393 | 395 |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +3 −8
| @@ -2133,18 +2133,13 @@ | ||
| 2133 | 2133 | } |
| 2134 | 2134 | } |
| 2135 | 2135 | |
| 2136 | - public boolean signalConnectionFailure(Host host, Connection connection, | |
| 2137 | - boolean isHostAddition) { | |
| 2138 | - boolean isDown = host.convictionPolicy.signalConnectionFailure(connection); | |
| 2139 | - | |
| 2136 | + public void signalHostDown(Host host, boolean isHostAddition) { | |
| 2140 | 2137 | // Don't mark the node down until we've fully initialized the controlConnection as this might mess up with |
| 2141 | 2138 | // the protocol detection |
| 2142 | 2139 | if (!isFullyInit || isClosed()) |
| 2143 | - return true; | |
| 2140 | + return; | |
| 2144 | 2141 | |
| 2145 | - if (isDown) | |
| 2146 | - triggerOnDown(host, isHostAddition, true); | |
| 2147 | - return isDown; | |
| 2142 | + triggerOnDown(host, isHostAddition, true); | |
| 2148 | 2143 | } |
| 2149 | 2144 | |
| 2150 | 2145 | public void removeHost(Host host, boolean isInitialConnection) { |
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +22 −14
| @@ -92,6 +92,7 @@ | ||
| 92 | 92 | |
| 93 | 93 | private volatile boolean isInitialized; |
| 94 | 94 | private final AtomicBoolean isDefunct = new AtomicBoolean(); |
| 95 | + private final AtomicBoolean signaled = new AtomicBoolean(); | |
| 95 | 96 | |
| 96 | 97 | private final AtomicReference<ConnectionCloseFuture> closeFuture = new AtomicReference<ConnectionCloseFuture>(); |
| 97 | 98 | |
| @@ -402,6 +403,21 @@ | ||
| 402 | 403 | else if (Host.statesLogger.isDebugEnabled()) |
| 403 | 404 | Host.statesLogger.debug("Defuncting {} because: {}", this, e.getMessage()); |
| 404 | 405 | |
| 406 | + | |
| 407 | + Host host = factory.manager.metadata.getHost(address); | |
| 408 | + if (host != null) { | |
| 409 | + // Sometimes close() can be called before defunct(); avoid decrementing the connection count twice, but | |
| 410 | + // we still want to signal the error to the conviction policy. | |
| 411 | + boolean decrement = signaled.compareAndSet(false, true); | |
| 412 | + | |
| 413 | + boolean hostDown = host.convictionPolicy.signalConnectionFailure(this, decrement); | |
| 414 | + if (hostDown) { | |
| 415 | + factory.manager.signalHostDown(host, host.wasJustAdded()); | |
| 416 | + } else { | |
| 417 | + notifyOwnerWhenDefunct(); | |
| 418 | + } | |
| 419 | + } | |
| 420 | + | |
| 405 | 421 | // Force the connection to close to make sure the future completes. Otherwise force() might never get called and |
| 406 | 422 | // threads will wait on the future forever. |
| 407 | 423 | // (this also errors out pending handlers) |
| @@ -463,12 +479,10 @@ | ||
| 463 | 479 | Connection.this.keyspace = ((SetKeyspace) response).keyspace; |
| 464 | 480 | return MoreFutures.VOID_SUCCESS; |
| 465 | 481 | } else if (response.type == ERROR) { |
| 466 | - closeAsync().force(); | |
| 467 | 482 | Responses.Error error = (Responses.Error) response; |
| 468 | - throw error.asException(address); | |
| 483 | + throw defunct(error.asException(address)); | |
| 469 | 484 | } else { |
| 470 | - closeAsync().force(); | |
| 471 | - throw new DriverInternalError("Unexpected response while setting keyspace: " + response); | |
| 485 | + throw defunct(new DriverInternalError("Unexpected response while setting keyspace: " + response)); | |
| 472 | 486 | } |
| 473 | 487 | } |
| 474 | 488 | }, factory.manager.configuration.getPoolingOptions().getInitializationExecutor()); |
| @@ -615,16 +629,10 @@ | ||
| 615 | 629 | |
| 616 | 630 | logger.debug("{} closing connection", this); |
| 617 | 631 | |
| 618 | - Host host = factory.manager.metadata.getHost(address); | |
| 619 | - if (host != null) { | |
| 620 | - if (isDefunct.get()) { | |
| 621 | - boolean isDown = factory.manager.signalConnectionFailure(host, this, host.wasJustAdded()); | |
| 622 | - | |
| 623 | - if (!isDown) | |
| 624 | - notifyOwnerWhenDefunct(); | |
| 625 | - // else the driver will destroy all pools and notify the control connection as part of marking the host down, | |
| 626 | - // so no need to notify | |
| 627 | - } else { | |
| 632 | + // Only signal if defunct hasn't done it already | |
| 633 | + if (signaled.compareAndSet(false, true)) { | |
| 634 | + Host host = factory.manager.metadata.getHost(address); | |
| 635 | + if (host != null) { | |
| 628 | 636 | host.convictionPolicy.signalConnectionClosed(this); |
| 629 | 637 | } |
| 630 | 638 | } |
modified driver-core/src/main/java/com/datastax/driver/core/ConvictionPolicy.java +13 −8
| @@ -49,7 +49,7 @@ | ||
| 49 | 49 | * |
| 50 | 50 | * @return whether the host should be considered down. |
| 51 | 51 | */ |
| 52 | - abstract boolean signalConnectionFailure(Connection connection); | |
| 52 | + abstract boolean signalConnectionFailure(Connection connection, boolean decrement); | |
| 53 | 53 | |
| 54 | 54 | abstract boolean canReconnectNow(); |
| 55 | 55 | |
| @@ -97,13 +97,18 @@ | ||
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | @Override |
| 100 | - boolean signalConnectionFailure(Connection connection) { | |
| 101 | - if (host.state != Host.State.DOWN) | |
| 102 | - updateReconnectionTime(); | |
| 103 | - | |
| 104 | - int remaining = openConnections.decrementAndGet(); | |
| 105 | - assert remaining >= 0; | |
| 106 | - Host.statesLogger.debug("[{}] {} failed, remaining = {}", host, connection, remaining); | |
| 100 | + boolean signalConnectionFailure(Connection connection, boolean decrement) { | |
| 101 | + int remaining; | |
| 102 | + if (decrement) { | |
| 103 | + if (host.state != Host.State.DOWN) | |
| 104 | + updateReconnectionTime(); | |
| 105 | + | |
| 106 | + remaining = openConnections.decrementAndGet(); | |
| 107 | + assert remaining >= 0; | |
| 108 | + Host.statesLogger.debug("[{}] {} failed, remaining = {}", host, connection, remaining); | |
| 109 | + } else { | |
| 110 | + remaining = openConnections.get(); | |
| 111 | + } | |
| 107 | 112 | return remaining == 0; |
| 108 | 113 | } |
| 109 | 114 |