8000 JAVA-629: Don't use connection timeout for unrelated internal queries. · tempbottle/java-driver@e2ceb0d · GitHub
[go: up one dir, main page]

Skip to content

Commit e2ceb0d

Browse files
Alexandre Dutraolim7t
authored andcommitted
JAVA-629: Don't use connection timeout for unrelated internal queries.
1 parent 37feec1 commit e2ceb0d

File tree

3 files changed

+17
-31
lines changed

3 files changed

+17
-31
lines changed

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
creation (JAVA-821, JAVA-822)
4444
- [improvement] Use parallel calls when re-preparing statement on other
4545
hosts (JAVA-725)
46+
- [bug] Don't use connection timeout for unrelated internal queries (JAVA-629)
4647

4748
Merged from 2.0.10_fixes branch:
4849

driver-core/src/main/java/com/datastax/driver/core/Connection.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -439,26 +439,28 @@ public void setKeyspace(String keyspace) throws ConnectionException {
439439
if (this.keyspace != null && this.keyspace.equals(keyspace))
440440
return;
441441

442-
ListenableFuture<Void> future = null;
443442
try {
444-
long timeout = factory.getConnectTimeoutMillis();
445-
future = setKeyspaceAsync(keyspace);
446-
Uninterruptibles.getUninterruptibly(future, timeout, TimeUnit.MILLISECONDS);
443+
Uninterruptibles.getUninterruptibly(setKeyspaceAsync(keyspace));
447444
} catch (ConnectionException e) {
448445
throw defunct(e);
449-
} catch (TimeoutException e) {
450-
// We've given up waiting on the future, but it's still running. Cancel to make sure that the request timeout logic
451-
// (readTimeout) will not kick in, because that would release the connection. This will work since connectTimeout is
452-
// generally lower than readTimeout (and if not, we'll get an ExecutionException and defunct below).
453-
future.cancel(true);
454-
logger.warn(String.format("Timeout while setting keyspace on connection to %s. This should not happen but is not critical (it will retried)", address));
455-
// Rethrow so that the caller will not try to use the connection, but do not defunct as we don't want to mark down
456-
throw new ConnectionException(address, "Timeout while setting keyspace on connection");
457446
} catch (BusyConnectionException e) {
458-
logger.warn(String.format("Tried to set the keyspace on busy connection to %s. This should not happen but is not critical (it will retried)", address));
447+
logger.warn("Tried to set the keyspace on busy connection to {}. "
448+
+ "This should not happen but is not critical (it will be retried)", address);
459449
throw new ConnectionException(address, "Tried to set the keyspace on busy connection");
460450
} catch (ExecutionException e) {
461-
throw defunct(new ConnectionException(address, "Error while setting keyspace", e.getCause()));
451+
Throwable cause = e.getCause();
452+
if (cause instanceof OperationTimedOutException) {
453+
// The timeout logic released the connection, but that's wrong since we did not borrow it in the first place.
454+
// JAVA-901 will fix this, in the meantime make sure the inFlight count is not off by one.
455+
inFlight.incrementAndGet();
456+
457+
// Rethrow so that the caller doesn't try to use the connection, but do not defunct as we don't want to mark down
458+
logger.warn("Timeout while setting keyspace on connection to {}. "
459+
+ "This should not happen but is not critical (it will be retried)", address);
460+
throw new ConnectionException(address, "Timeout while setting keyspace on connection");
461+
} else {
462+
throw defunct(new ConnectionException(address, "Error while setting keyspace", cause));
463+
}
462464
}
463465
}
464466

@@ -793,10 +795,6 @@ private AtomicInteger getIdGenerator(Host host) {
793795
return g;
794796
}
795797

796-
public long getConnectTimeoutMillis() {
797-
return configuration.getSocketOptions().getConnectTimeoutMillis();
798-
}
799-
800798
public long getReadTimeoutMillis() {
801799
return configuration.getSocketOptions().getReadTimeoutMillis();
802800
}

driver-core/src/main/java/com/datastax/driver/core/SessionManager.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -473,19 +473,6 @@ void onRemove(Host host) throws InterruptedException, ExecutionException {
473473
onDown(host);
474474
}
475475

476-
void setKeyspace(String keyspace) {
477-
long timeout = configuration().getSocketOptions().getConnectTimeoutMillis();
478-
try {
479-
Future<?> future = executeQuery(new Requests.Query("use " + keyspace), Statement.DEFAULT);
480-
// Note: using the connection timeout isn't perfectly correct, we should probably change that someday
481-
Uninterruptibles.getUninterruptibly(future, timeout, TimeUnit.MILLISECONDS);
482-
} catch (TimeoutException e) {
483-
throw new DriverInternalError(String.format("No responses after %d milliseconds while setting current keyspace. This should not happen, unless you have setup a very low connection timeout.", timeout));
484-
} catch (ExecutionException e) {
485-
throw DriverThrowables.propagateCause(e);
486-
}
487-
}
488-
489476
Message.Request makeRequestMessage(Statement statement, ByteBuffer pagingState) {
490477

491478
ConsistencyLevel consistency = statement.getConsistencyLevel();

0 commit comments

Comments
 (0)
0