Elffuss BraidHub

kikocis/java-driver public

Merge pull request #426 from datastax/java629

JAVA-629: Don't use connection timeout for unrelated internal queries.
Changesetd6fac0e91dd46ca0801842a302c6731a7a710da018ab1b9c4d6c8376d637f0eb
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2015-08-28 15:14 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2015-08-28 15:14 +02:00
Parents6d032b5eb9c1, 6ded3837992c

Browse the files at this changeset

3 files changed

modified changelog/README.md +1 −0
@@ -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
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +16 −18
@@ -439,26 +439,28 @@
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 @@
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 }
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +0 −13
@@ -473,19 +473,6 @@
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();