kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | f209c5702227b54d90dddd5459a8412fbafe536335ab1a019c4ddc3644e6e05f |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-02-24 15:27 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-03-24 14:57 +01:00 |
| Parents | 68d7d54014b4 |
4 files changed
modified changelog/README.md +1 −0
| @@ -4,6 +4,7 @@ | ||
| 4 | 4 | |
| 5 | 5 | - [bug] JAVA-994: Don't call on(Up|Down|Add|Remove) methods if Cluster is closed/closing. |
| 6 | 6 | - [improvement] JAVA-805: Document that metrics are null until Cluster is initialized. |
| 7 | +- [bug] JAVA-1072: Ensure defunct connections are properly evicted from the pool. | |
| 7 | 8 | |
| 8 | 9 | ### 2.0.12 |
| 9 | 10 |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +3 −8
| @@ -2016,18 +2016,13 @@ | ||
| 2016 | 2016 | } |
| 2017 | 2017 | } |
| 2018 | 2018 | |
| 2019 | - public boolean signalConnectionFailure(Host host, Connection connection, | |
| 2020 | - boolean isHostAddition) { | |
| 2021 | - boolean isDown = host.convictionPolicy.signalConnectionFailure(connection); | |
| 2022 | - | |
| 2019 | + public void signalHostDown(Host host, boolean isHostAddition) { | |
| 2023 | 2020 | // Don't mark the node down until we've fully initialized the controlConnection as this might mess up with |
| 2024 | 2021 | // the protocol detection |
| 2025 | 2022 | if (!isFullyInit || isClosed()) |
| 2026 | - return true; | |
| 2023 | + return; | |
| 2027 | 2024 | |
| 2028 | - if (isDown) | |
| 2029 | - triggerOnDown(host, isHostAddition, true); | |
| 2030 | - return isDown; | |
| 2025 | + triggerOnDown(host, isHostAddition, true); | |
| 2031 | 2026 | } |
| 2032 | 2027 | |
| 2033 | 2028 | private boolean supportsProtocolV2(Host newHost) { |
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 | |
| @@ -396,6 +397,21 @@ | ||
| 396 | 397 | else if (Host.statesLogger.isDebugEnabled()) |
| 397 | 398 | Host.statesLogger.debug("Defuncting {} because: {}", this, e.getMessage()); |
| 398 | 399 | |
| 400 | + | |
| 401 | + Host host = factory.manager.metadata.getHost(address); | |
| 402 | + if (host != null) { | |
| 403 | + // Sometimes close() can be called before defunct(); avoid decrementing the connection count twice, but | |
| 404 | + // we still want to signal the error to the conviction policy. | |
| 405 | + boolean decrement = signaled.compareAndSet(false, true); | |
| 406 | + | |
| 407 | + boolean hostDown = host.convictionPolicy.signalConnectionFailure(this, decrement); | |
| 408 | + if (hostDown) { | |
| 409 | + factory.manager.signalHostDown(host, host.wasJustAdded()); | |
| 410 | + } else { | |
| 411 | + notifyOwnerWhenDefunct(); | |
| 412 | + } | |
| 413 | + } | |
| 414 | + | |
| 399 | 415 | // Force the connection to close to make sure the future completes. Otherwise force() might never get called and |
| 400 | 416 | // threads will wait on the future forever. |
| 401 | 417 | // (this also errors out pending handlers) |
| @@ -457,12 +473,10 @@ | ||
| 457 | 473 | Connection.this.keyspace = ((SetKeyspace) response).keyspace; |
| 458 | 474 | return MoreFutures.VOID_SUCCESS; |
| 459 | 475 | } else if (response.type == ERROR) { |
| 460 | - closeAsync().force(); | |
| 461 | 476 | Responses.Error error = (Responses.Error) response; |
| 462 | - throw error.asException(address); | |
| 477 | + throw defunct(error.asException(address)); | |
| 463 | 478 | } else { |
| 464 | - closeAsync().force(); | |
| 465 | - throw new DriverInternalError("Unexpected response while setting keyspace: " + response); | |
| 479 | + throw defunct(new DriverInternalError("Unexpected response while setting keyspace: " + response)); | |
| 466 | 480 | } |
| 467 | 481 | } |
| 468 | 482 | }, factory.manager.configuration.getPoolingOptions().getInitializationExecutor()); |
| @@ -609,16 +623,10 @@ | ||
| 609 | 623 | |
| 610 | 624 | logger.debug("{} closing connection", this); |
| 611 | 625 | |
| 612 | - Host host = factory.manager.metadata.getHost(address); | |
| 613 | - if (host != null) { | |
| 614 | - if (isDefunct.get()) { | |
| 615 | - boolean isDown = factory.manager.signalConnectionFailure(host, this, host.wasJustAdded()); | |
| 616 | - | |
| 617 | - if (!isDown) | |
| 618 | - notifyOwnerWhenDefunct(); | |
| 619 | - // else the driver will destroy all pools and notify the control connection as part of marking the host down, | |
| 620 | - // so no need to notify | |
| 621 | - } else { | |
| 626 | + // Only signal if defunct hasn't done it already | |
| 627 | + if (signaled.compareAndSet(false, true)) { | |
| 628 | + Host host = factory.manager.metadata.getHost(address); | |
| 629 | + if (host != null) { | |
| 622 | 630 | host.convictionPolicy.signalConnectionClosed(this); |
| 623 | 631 | } |
| 624 | 632 | } |
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 | |
| @@ -95,13 +95,18 @@ | ||
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | @Override |
| 98 | - boolean signalConnectionFailure(Connection connection) { | |
| 99 | - if (host.state != Host.State.DOWN) | |
| 100 | - updateReconnectionTime(); | |
| 101 | - | |
| 102 | - int remaining = openConnections.decrementAndGet(); | |
| 103 | - assert remaining >= 0; | |
| 104 | - Host.statesLogger.debug("[{}] {} failed, remaining = {}", host, connection, remaining); | |
| 98 | + boolean signalConnectionFailure(Connection connection, boolean decrement) { | |
| 99 | + int remaining; | |
| 100 | + if (decrement) { | |
| 101 | + if (host.state != Host.State.DOWN) | |
| 102 | + updateReconnectionTime(); | |
| 103 | + | |
| 104 | + remaining = openConnections.decrementAndGet(); | |
| 105 | + assert remaining >= 0; | |
| 106 | + Host.statesLogger.debug("[{}] {} failed, remaining = {}", host, connection, remaining); | |
| 107 | + } else { | |
| 108 | + remaining = openConnections.get(); | |
| 109 | + } | |
| 105 | 110 | return remaining == 0; |
| 106 | 111 | } |
| 107 | 112 |