Elffuss BraidHub

kikocis/java-driver public

Merge branch '2.0' into 2.1
Changeset77c0c373cb1a00679adf8262466d880119ea80fbc8234bdb95b5e9e87eaf5e67
Authorolim7t <omichallat+github@gmail.com> · 2016-03-25 15:24 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-03-25 15:25 +01:00
Parents96c0afc7f127, fc99c0c842e2

Browse the files at this changeset

4 files changed

modified changelog/README.md +2 −0
@@ -47,6 +47,7 @@
4747
4848 - [bug] JAVA-994: Don't call on(Up|Down|Add|Remove) methods if Cluster is closed/closing.
4949 - [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.
5051
5152
5253 ### 2.1.9
@@ -388,6 +389,7 @@
388389
389390 - [bug] JAVA-994: Don't call on(Up|Down|Add|Remove) methods if Cluster is closed/closing.
390391 - [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.
391393
392394 ### 2.0.12
393395
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +3 −8
@@ -2133,18 +2133,13 @@
21332133 }
21342134 }
21352135
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) {
21402137 // Don't mark the node down until we've fully initialized the controlConnection as this might mess up with
21412138 // the protocol detection
21422139 if (!isFullyInit || isClosed())
2143- return true;
2140+ return;
21442141
2145- if (isDown)
2146- triggerOnDown(host, isHostAddition, true);
2147- return isDown;
2142+ triggerOnDown(host, isHostAddition, true);
21482143 }
21492144
21502145 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 @@
9292
9393 private volatile boolean isInitialized;
9494 private final AtomicBoolean isDefunct = new AtomicBoolean();
95+ private final AtomicBoolean signaled = new AtomicBoolean();
9596
9697 private final AtomicReference<ConnectionCloseFuture> closeFuture = new AtomicReference<ConnectionCloseFuture>();
9798
@@ -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