Elffuss BraidHub

kikocis/java-driver public

JAVA-1072: Ensure defunct connections are properly evicted from the pool.

(cherry picked from 2.1)
Changeset5d16afa2bfbbe955af17600771ac19379584b0cccf2e29c084b61ae5ed26b8ba
Authorolim7t <omichallat+github@gmail.com> · 2016-03-25 15:24 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-04-01 17:57 +02:00
Parents990ca29f389b

Browse the files at this changeset

4 files changed

modified changelog/README.md +2 −0
@@ -21,6 +21,7 @@
2121 - [improvement] JAVA-852: Ignore peers with null entries during discovery.
2222 - [bug] JAVA-1005: DowngradingConsistencyRetryPolicy does not work with EACH_QUORUM when 1 DC is down.
2323 - [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.
2425
2526
2627 ### 3.0.0
@@ -659,6 +660,7 @@
659660
660661 - [bug] JAVA-994: Don't call on(Up|Down|Add|Remove) methods if Cluster is closed/closing.
661662 - [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.
662664
663665 ### 2.0.12
664666
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +3 −8
@@ -2092,18 +2092,13 @@
20922092 }
20932093 }
20942094
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) {
20992096 // Don't mark the node down until we've fully initialized the controlConnection as this might mess up with
21002097 // the protocol detection
21012098 if (!isFullyInit || isClosed())
2102- return true;
2099+ return;
21032100
2104- if (isDown)
2105- triggerOnDown(host, isHostAddition, true);
2106- return isDown;
2101+ triggerOnDown(host, isHostAddition, true);
21072102 }
21082103
21092104 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 @@
8888
8989 private volatile boolean isInitialized;
9090 private final AtomicBoolean isDefunct = new AtomicBoolean();
91+ private final AtomicBoolean signaled = new AtomicBoolean();
9192
9293 private final AtomicReference<ConnectionCloseFuture> closeFuture = new AtomicReference<ConnectionCloseFuture>();
9394
@@ -402,6 +403,21 @@
402403 else if (Host.statesLogger.isDebugEnabled())
403404 Host.statesLogger.debug("Defuncting {} because: {}", this, e.getMessage());
404405
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+
405421 // Force the connection to close to make sure the future completes. Otherwise force() might never get called and
406422 // threads will wait on the future forever.
407423 // (this also errors out pending handlers)
@@ -463,12 +479,10 @@
463479 Connection.this.keyspace = ((SetKeyspace) response).keyspace;
464480 return MoreFutures.VOID_SUCCESS;
465481 } else if (response.type == ERROR) {
466- closeAsync().force();
467482 Responses.Error error = (Responses.Error) response;
468- throw error.asException(address);
483+ throw defunct(error.asException(address));
469484 } 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));
472486 }
473487 }
474488 }, factory.manager.configuration.getPoolingOptions().getInitializationExecutor());
@@ -615,16 +629,10 @@
615629
616630 logger.debug("{} closing connection", this);
617631
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) {
628636 host.convictionPolicy.signalConnectionClosed(this);
629637 }
630638 }
modified driver-core/src/main/java/com/datastax/driver/core/ConvictionPolicy.java +13 −8
@@ -49,7 +49,7 @@
4949 *
5050 * @return whether the host should be considered down.
5151 */
52- abstract boolean signalConnectionFailure(Connection connection);
52+ abstract boolean signalConnectionFailure(Connection connection, boolean decrement);
5353
5454 abstract boolean canReconnectNow();
5555
@@ -97,13 +97,18 @@
9797 }
9898
9999 @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+ }
107112 return remaining == 0;
108113 }
109114