Elffuss BraidHub

kikocis/java-driver public

Merge pull request #499 from datastax/java901_2011

JAVA-901: Move call to connection.release() out of cancelHandler.
Changesetf6a1bd2734a33f94f24dd1eb468ec7d71619ed2b621ff1707fb4ed007f1eafff
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2015-10-30 16:38 +01:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2015-10-30 16:38 +01:00
Parents4a4ffe7ce8ed, 11a395190428

Browse the files at this changeset

4 files changed

modified changelog/README.md +1 −0
@@ -7,6 +7,7 @@
77 - [bug] JAVA-954: Don't trigger reconnection before initialization complete.
88 - [improvement] JAVA-914: Avoid rejected tasks at shutdown.
99 - [improvement] JAVA-921: Add SimpleStatement.getValuesCount().
10+- [bug] JAVA-901: Move call to connection.release() out of cancelHandler.
1011
1112
1213 ### 2.0.11
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +3 −7
@@ -452,10 +452,6 @@
452452 } catch (ExecutionException e) {
453453 Throwable cause = e.getCause();
454454 if (cause instanceof OperationTimedOutException) {
455- // The timeout logic released the connection, but that's wrong since we did not borrow it in the first place.
456- // JAVA-901 will fix this, in the meantime make sure the inFlight count is not off by one.
457- inFlight.incrementAndGet();
458-
459455 // Rethrow so that the caller doesn't try to use the connection, but do not defunct as we don't want to mark down
460456 logger.warn("Timeout while setting keyspace on connection to {}. "
461457 + "This should not happen but is not critical (it will be retried)", address);
@@ -1227,16 +1223,16 @@
12271223 timeout.cancel();
12281224 }
12291225
1230- public void cancelHandler() {
1226+ public boolean cancelHandler() {
12311227 if (!isCancelled.compareAndSet(false, true))
1232- return;
1228+ return false;
12331229
12341230 // We haven't really received a response: we want to remove the handle because we gave up on that
12351231 // request and there is no point in holding the handler, but we don't release the streamId. If we
12361232 // were, a new request could reuse that ID but get the answer to the request we just gave up on instead
12371233 // of its own answer, and we would have no way to detect that.
12381234 connection.dispatcher.removeHandler(this, false);
1239- connection.release();
1235+ return true;
12401236 }
12411237
12421238 private TimerTask onTimeoutTask() {
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +7 −4
@@ -352,8 +352,8 @@
352352 // If cancel() was called after we set the state to "in progress", but before connection.write had completed, it might have
353353 // missed the new value of connectionHandler. So make sure that cancelHandler() gets called here (we might call it twice,
354354 // but it knows how to deal with it).
355- if (queryStateRef.get() == QueryState.CANCELLED_WHILE_IN_PROGRESS)
356- connectionHandler.cancelHandler();
355+ if (queryStateRef.get() == QueryState.CANCELLED_WHILE_IN_PROGRESS && connectionHandler.cancelHandler())
356+ connection.release();
357357 }
358358
359359 private void retry(final boolean retryCurrent, ConsistencyLevel newConsistencyLevel) {
@@ -391,8 +391,8 @@
391391 logger.trace("[{}] Cancelled while in progress", id);
392392 // The connectionHandler should be non-null, but we might miss the update if we're racing with write().
393393 // If it's still null, this will be handled by re-checking queryStateRef at the end of write().
394- if (connectionHandler != null)
395- connectionHandler.cancelHandler();
394+ if (connectionHandler != null && connectionHandler.cancelHandler())
395+ connectionHandler.connection.release();
396396 return;
397397 } else if (!previous.inProgress && queryStateRef.compareAndSet(previous, QueryState.CANCELLED_WHILE_COMPLETE)) {
398398 if(logger.isTraceEnabled())
@@ -671,6 +671,7 @@
671671 retryCount, queryState, queryStateRef.get());
672672 return false;
673673 }
674+ connection.release();
674675 logError(connection.address, new DriverException("Timeout waiting for response to prepare message"));
675676 retry(false, null);
676677 return true;
@@ -723,6 +724,8 @@
723724 Host queriedHost = current;
724725 OperationTimedOutException timeoutException = new OperationTimedOutException(connection.address);
725726 try {
727+ connection.release();
728+
726729 logError(connection.address, timeoutException);
727730 retry(false, null);
728731 } catch (Exception e) {
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +1 −4
@@ -583,10 +583,7 @@
583583 @Override
584584 public void onFailure(Throwable t) {
585585 logger.debug(String.format("Unexpected error while preparing query (%s) on %s", query, entry.getKey()), t);
586-
587- // If the query timed out, that already released the connection, otherwise do it now
588- if (!(t instanceof OperationTimedOutException))
589- c.release();
586+ c.release();
590587 }
591588 });
592589 futures.add(future);