Elffuss BraidHub

kikocis/java-driver public

Avoid releasing connection twice when we get a timeout while re-preparing to another host (JAVA-651).

Introduced new ConnectionException subclass to avoid check on the
message string.
Changeset0707b5d7e98b4733c33498a6e34e7f221c3fe578540acf292669079064da3170
Authorolim7t <omichallat+github@gmail.com> · 2015-02-06 13:44 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-02-11 12:28 +01:00
Parents3b424b26e723

Browse the files at this changeset

4 files changed

modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +1 −1
@@ -833,7 +833,7 @@
833833 public boolean onTimeout(Connection connection, long latency, int retryCount) {
834834 assert connection != null; // We always timeout on a specific connection, so this shouldn't be null
835835 this.address = connection.address;
836- super.setException(new ConnectionException(connection.address, "Operation timed out"));
836+ super.setException(new OperationTimedOutException(connection.address));
837837 return true;
838838 }
839839
modified driver-core/src/main/java/com/datastax/driver/core/DefaultResultSetFuture.java +1 −1
@@ -141,7 +141,7 @@
141141 public boolean onTimeout(Connection connection, long latency, int retryCount) {
142142 // This is only called for internal calls (i.e, when the future is not wrapped in RequestHandler).
143143 // So just set an exception for the final result, which should be handled correctly by said internal call.
144- setException(new ConnectionException(connection.address, "Operation timed out"));
144+ setException(new OperationTimedOutException(connection.address));
145145 return true;
146146 }
147147
added driver-core/src/main/java/com/datastax/driver/core/OperationTimedOutException.java +13 −0
@@ -0,0 +1,13 @@
1+package com.datastax.driver.core;
2+
3+import java.net.InetSocketAddress;
4+
5+/**
6+ * Thrown on a client-side timeout, i.e. when the client didn't hear back from the server within
7+ * {@link SocketOptions#getReadTimeoutMillis()}.
8+ */
9+class OperationTimedOutException extends ConnectionException {
10+ public OperationTimedOutException(InetSocketAddress address) {
11+ super(address, "Operation timed out");
12+ }
13+}
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +4 −1
@@ -516,6 +516,7 @@
516516 // Let's not wait too long if we can't get a connection. Things
517517 // will fix themselves once the user tries a query anyway.
518518 PooledConnection c = null;
519+ boolean timedOut = false;
519520 try {
520521 c = entry.getValue().borrowConnection(200, TimeUnit.MILLISECONDS);
521522 c.write(new Requests.Prepare(query)).get();
@@ -529,8 +530,10 @@
529530 // We shouldn't really get exception while preparing a
530531 // query, so log this (but ignore otherwise as it's not a big deal)
531532 logger.error(String.format("Unexpected error while preparing query (%s) on %s", query, entry.getKey()), e);
533+ // If the query timed out, that already released the connection
534+ timedOut = e.getCause() instanceof OperationTimedOutException;
532535 } finally {
533- if (c != null)
536+ if (c != null && !timedOut)
534537 c.release();
535538 }
536539 }