Elffuss BraidHub

kikocis/java-driver public

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

- defunct connection when setKeyspaceAsync call fails.
- make code more robust in case close() is called before defunct().
Changesetf209c5702227b54d90dddd5459a8412fbafe536335ab1a019c4ddc3644e6e05f
Authorolim7t <omichallat+github@gmail.com> · 2016-02-24 15:27 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-03-24 14:57 +01:00
Parents68d7d54014b4

Browse the files at this changeset

4 files changed

modified changelog/README.md +1 −0
@@ -4,6 +4,7 @@
44
55 - [bug] JAVA-994: Don't call on(Up|Down|Add|Remove) methods if Cluster is closed/closing.
66 - [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.
78
89 ### 2.0.12
910
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +3 −8
@@ -2016,18 +2016,13 @@
20162016 }
20172017 }
20182018
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) {
20232020 // Don't mark the node down until we've fully initialized the controlConnection as this might mess up with
20242021 // the protocol detection
20252022 if (!isFullyInit || isClosed())
2026- return true;
2023+ return;
20272024
2028- if (isDown)
2029- triggerOnDown(host, isHostAddition, true);
2030- return isDown;
2025+ triggerOnDown(host, isHostAddition, true);
20312026 }
20322027
20332028 private boolean supportsProtocolV2(Host newHost) {
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
@@ -396,6 +397,21 @@
396397 else if (Host.statesLogger.isDebugEnabled())
397398 Host.statesLogger.debug("Defuncting {} because: {}", this, e.getMessage());
398399
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+
399415 // Force the connection to close to make sure the future completes. Otherwise force() might never get called and
400416 // threads will wait on the future forever.
401417 // (this also errors out pending handlers)
@@ -457,12 +473,10 @@
457473 Connection.this.keyspace = ((SetKeyspace) response).keyspace;
458474 return MoreFutures.VOID_SUCCESS;
459475 } else if (response.type == ERROR) {
460- closeAsync().force();
461476 Responses.Error error = (Responses.Error) response;
462- throw error.asException(address);
477+ throw defunct(error.asException(address));
463478 } 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));
466480 }
467481 }
468482 }, factory.manager.configuration.getPoolingOptions().getInitializationExecutor());
@@ -609,16 +623,10 @@
609623
610624 logger.debug("{} closing connection", this);
611625
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) {
622630 host.convictionPolicy.signalConnectionClosed(this);
623631 }
624632 }
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
@@ -95,13 +95,18 @@
9595 }
9696
9797 @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+ }
105110 return remaining == 0;
106111 }
107112