kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 5d16afa2bfbbe955af17600771ac19379584b0cccf2e29c084b61ae5ed26b8ba |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-03-25 15:24 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-04-01 17:57 +02:00 |
| Parents | 990ca29f389b |
4 files changed
modified changelog/README.md +2 −0
| @@ -21,6 +21,7 @@ | ||
| 21 | 21 | - [improvement] JAVA-852: Ignore peers with null entries during discovery. |
| 22 | 22 | - [bug] JAVA-1005: DowngradingConsistencyRetryPolicy does not work with EACH_QUORUM when 1 DC is down. |
| 23 | 23 | - [bug] JAVA-1002: Avoid deadlock when re-preparing a statement on other hosts. |
| 24 | +- [bug] JAVA-1072: Ensure defunct connections are properly evicted from the pool. | |
| 24 | 25 | |
| 25 | 26 | |
| 26 | 27 | ### 3.0.0 |
| @@ -659,6 +660,7 @@ | ||
| 659 | 660 | |
| 660 | 661 | - [bug] JAVA-994: Don't call on(Up|Down|Add|Remove) methods if Cluster is closed/closing. |
| 661 | 662 | - [improvement] JAVA-805: Document that metrics are null until Cluster is initialized. |
| 663 | +- [bug] JAVA-1072: Ensure defunct connections are properly evicted from the pool. | |
| 662 | 664 | |
| 663 | 665 | ### 2.0.12 |
| 664 | 666 |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +3 −8
| @@ -2092,18 +2092,13 @@ | ||
| 2092 | 2092 | } |
| 2093 | 2093 | } |
| 2094 | 2094 | |
| 2095 | - public boolean signalConnectionFailure(Host host, Connection connection, | |
| 2096 | - boolean isHostAddition) { | |
| 2097 | - boolean isDown = host.convictionPolicy.signalConnectionFailure(connection); | |
| 2098 | - | |
| 2095 | + public void signalHostDown(Host host, boolean isHostAddition) { | |
| 2099 | 2096 | // Don't mark the node down until we've fully initialized the controlConnection as this might mess up with |
| 2100 | 2097 | // the protocol detection |
| 2101 | 2098 | if (!isFullyInit || isClosed()) |
| 2102 | - return true; | |
| 2099 | + return; | |
| 2103 | 2100 | |
| 2104 | - if (isDown) | |
| 2105 | - triggerOnDown(host, isHostAddition, true); | |
| 2106 | - return isDown; | |
| 2101 | + triggerOnDown(host, isHostAddition, true); | |
| 2107 | 2102 | } |
| 2108 | 2103 | |
| 2109 | 2104 | public void removeHost(Host host, boolean isInitialConnection) { |
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +22 −14
| @@ -88,6 +88,7 @@ | ||
| 88 | 88 | |
| 89 | 89 | private volatile boolean isInitialized; |
| 90 | 90 | private final AtomicBoolean isDefunct = new AtomicBoolean(); |
| 91 | + private final AtomicBoolean signaled = new AtomicBoolean(); | |
| 91 | 92 | |
| 92 | 93 | private final AtomicReference<ConnectionCloseFuture> closeFuture = new AtomicReference<ConnectionCloseFuture>(); |
| 93 | 94 | |
| @@ -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 |