kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 04d7447ddfe460ec8861cfc2fe9c6788594f4c427279d51df4e4bcd6982856c0 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-02-06 18:31 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-02-11 12:51 +01:00 |
| Parents | 32fe6b31edc5 |
3 files changed
modified driver-core/CHANGELOG.rst +2 −0
| @@ -5,6 +5,8 @@ | ||
| 5 | 5 | -------- |
| 6 | 6 | |
| 7 | 7 | - [bug] Fix edge cases where a connection was released twice (JAVA-651). |
| 8 | +- [bug] Fix edge cases in query cancellation (JAVA-653). | |
| 9 | + | |
| 8 | 10 | |
| 9 | 11 | 2.0.9.1: |
| 10 | 12 | -------- |
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +19 −3
| @@ -19,6 +19,7 @@ | ||
| 19 | 19 | import java.net.InetSocketAddress; |
| 20 | 20 | import java.util.Iterator; |
| 21 | 21 | import java.util.concurrent.*; |
| 22 | +import java.util.concurrent.atomic.AtomicBoolean; | |
| 22 | 23 | import java.util.concurrent.atomic.AtomicInteger; |
| 23 | 24 | import java.util.concurrent.atomic.AtomicReference; |
| 24 | 25 | |
| @@ -368,6 +369,10 @@ | ||
| 368 | 369 | } |
| 369 | 370 | |
| 370 | 371 | public ResponseHandler write(ResponseCallback callback) throws ConnectionException, BusyConnectionException { |
| 372 | + return write(callback, true); | |
| 373 | + } | |
| 374 | + | |
| 375 | + public ResponseHandler write(ResponseCallback callback, boolean startTimeout) throws ConnectionException, BusyConnectionException { | |
| 371 | 376 | |
| 372 | 377 | Message.Request request = callback.request(); |
| 373 | 378 | |
| @@ -394,6 +399,10 @@ | ||
| 394 | 399 | logger.trace("{} writing request {}", this, request); |
| 395 | 400 | writer.incrementAndGet(); |
| 396 | 401 | channel.write(request).addListener(writeHandler(request, handler)); |
| 402 | + | |
| 403 | + if (startTimeout) | |
| 404 | + handler.startTimeout(); | |
| 405 | + | |
| 397 | 406 | return handler; |
| 398 | 407 | } |
| 399 | 408 | |
| @@ -864,8 +873,10 @@ | ||
| 864 | 873 | public final ResponseCallback callback; |
| 865 | 874 | public final int retryCount; |
| 866 | 875 | |
| 867 | - private final Timeout timeout; | |
| 868 | 876 | private final long startTime; |
| 877 | + private volatile Timeout timeout; | |
| 878 | + | |
| 879 | + private final AtomicBoolean isCancelled = new AtomicBoolean(); | |
| 869 | 880 | |
| 870 | 881 | public ResponseHandler(Connection connection, ResponseCallback callback) throws BusyConnectionException { |
| 871 | 882 | this.connection = connection; |
| @@ -873,10 +884,12 @@ | ||
| 873 | 884 | this.callback = callback; |
| 874 | 885 | this.retryCount = callback.retryCount(); |
| 875 | 886 | |
| 887 | + this.startTime = System.nanoTime(); | |
| 888 | + } | |
| 889 | + | |
| 890 | + void startTimeout() { | |
| 876 | 891 | long timeoutMs = connection.factory.getReadTimeoutMillis(); |
| 877 | 892 | this.timeout = timeoutMs <= 0 ? null : connection.factory.timer.newTimeout(onTimeoutTask(), timeoutMs, TimeUnit.MILLISECONDS); |
| 878 | - | |
| 879 | - this.startTime = System.nanoTime(); | |
| 880 | 893 | } |
| 881 | 894 | |
| 882 | 895 | void cancelTimeout() { |
| @@ -885,6 +898,9 @@ | ||
| 885 | 898 | } |
| 886 | 899 | |
| 887 | 900 | public void cancelHandler() { |
| 901 | + if (!isCancelled.compareAndSet(false, true)) | |
| 902 | + return; | |
| 903 | + | |
| 888 | 904 | // We haven't really received a response: we want to remove the handle because we gave up on that |
| 889 | 905 | // request and there is no point in holding the handler, but we don't release the streamId. If we |
| 890 | 906 | // were, a new request could reuse that ID but get the answer to the request we just gave up on instead |
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +41 −23
| @@ -56,7 +56,6 @@ | ||
| 56 | 56 | private volatile List<Host> triedHosts; |
| 57 | 57 | private volatile HostConnectionPool currentPool; |
| 58 | 58 | private final AtomicReference<QueryState> queryStateRef; |
| 59 | - private volatile boolean shouldCancelConnectionHandler; | |
| 60 | 59 | |
| 61 | 60 | // This represents the number of times a retry has been triggered by the RetryPolicy (this is different from |
| 62 | 61 | // queryStateRef.get().retryCount, because some retries don't involve the policy, for example after an |
| @@ -99,7 +98,7 @@ | ||
| 99 | 98 | |
| 100 | 99 | public void sendRequest() { |
| 101 | 100 | try { |
| 102 | - while (queryPlan.hasNext() && queryStateRef.get() != QueryState.CANCELLED) { | |
| 101 | + while (queryPlan.hasNext() && !queryStateRef.get().isCancelled()) { | |
| 103 | 102 | Host host = queryPlan.next(); |
| 104 | 103 | logger.trace("Querying node {}", host); |
| 105 | 104 | if (query(host)) |
| @@ -159,25 +158,30 @@ | ||
| 159 | 158 | // before connection.write has completed |
| 160 | 159 | connectionHandler = null; |
| 161 | 160 | |
| 162 | - // Set query state to "in progress" at next iteration | |
| 161 | + // Ensure query state is "in progress" (can be already if connection.write failed on a previous node and we're retrying) | |
| 163 | 162 | while (true) { |
| 164 | 163 | QueryState previous = queryStateRef.get(); |
| 165 | - assert !previous.inProgress; | |
| 166 | - if (previous == QueryState.CANCELLED) { | |
| 164 | + if (previous.isCancelled()) { | |
| 165 | + if (connection instanceof PooledConnection) | |
| 166 | + ((PooledConnection)connection).release(); | |
| 167 | 167 | return; |
| 168 | 168 | } |
| 169 | - if (queryStateRef.compareAndSet(previous, previous.startNext())) | |
| 169 | + if (previous.inProgress || queryStateRef.compareAndSet(previous, previous.startNext())) | |
| 170 | 170 | break; |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | - connectionHandler = connection.write(responseCallback); | |
| 174 | - | |
| 175 | - // N.B: onSet/onException could have already been called at this point | |
| 176 | - | |
| 177 | - // If cancel was called after we set the state to "in progress", but before connection.write, it might have seen a null connectionHandler. | |
| 178 | - // In this case we need to cancel it now to release the connection (because the next onSet/onException/onTimeout will see the CANCELLED | |
| 179 | - // state and return immediately). | |
| 180 | - if (shouldCancelConnectionHandler) | |
| 173 | + connectionHandler = connection.write(responseCallback, false); | |
| 174 | + // Only start the timeout when we're sure connectionHandler is set. This avoids an edge case where onTimeout() was triggered | |
| 175 | + // *before* the call to connection.write had returned. | |
| 176 | + connectionHandler.startTimeout(); | |
| 177 | + | |
| 178 | + // Note that we could have already received the response here (so onSet() / onException() would have been called). This is | |
| 179 | + // why we only test for CANCELLED_WHILE_IN_PROGRESS below. | |
| 180 | + | |
| 181 | + // If cancel() was called after we set the state to "in progress", but before connection.write had completed, it might have | |
| 182 | + // missed the new value of connectionHandler. So make sure that cancelHandler() gets called here (we might call it twice, | |
| 183 | + // but it knows how to deal with it). | |
| 184 | + if (queryStateRef.get() == QueryState.CANCELLED_WHILE_IN_PROGRESS) | |
| 181 | 185 | connectionHandler.cancelHandler(); |
| 182 | 186 | } |
| 183 | 187 | |
| @@ -196,7 +200,7 @@ | ||
| 196 | 200 | manager.executor().execute(new Runnable() { |
| 197 | 201 | @Override |
| 198 | 202 | public void run() { |
| 199 | - if (queryStateRef.get() == QueryState.CANCELLED) | |
| 203 | + if (queryStateRef.get().isCancelled()) | |
| 200 | 204 | return; |
| 201 | 205 | try { |
| 202 | 206 | if (retryCurrent) { |
| @@ -212,12 +216,21 @@ | ||
| 212 | 216 | } |
| 213 | 217 | |
| 214 | 218 | public void cancel() { |
| 215 | - QueryState previous = queryStateRef.getAndSet(QueryState.CANCELLED); | |
| 216 | - if (previous.inProgress) { | |
| 217 | - if (connectionHandler != null) | |
| 218 | - connectionHandler.cancelHandler(); | |
| 219 | - else | |
| 220 | - shouldCancelConnectionHandler = true; | |
| 219 | + // Atomically set a special QueryState, that will cause any further operation to abort. | |
| 220 | + // We want to remember whether a request was in progress when we did this, so there are two cancel states. | |
| 221 | + while (true) { | |
| 222 | + QueryState previous = queryStateRef.get(); | |
| 223 | + if (previous.isCancelled()) { | |
| 224 | + return; | |
| 225 | + } else if (previous.inProgress && queryStateRef.compareAndSet(previous, QueryState.CANCELLED_WHILE_IN_PROGRESS)) { | |
| 226 | + // The connectionHandler should be non-null, but we might miss the update if we're racing with write(). | |
| 227 | + // If it's still null, this will be handled by re-checking queryStateRef at the end of write(). | |
| 228 | + if (connectionHandler != null) | |
| 229 | + connectionHandler.cancelHandler(); | |
| 230 | + return; | |
| 231 | + } else if (!previous.inProgress && queryStateRef.compareAndSet(previous, QueryState.CANCELLED_WHILE_COMPLETE)) { | |
| 232 | + return; | |
| 233 | + } | |
| 221 | 234 | } |
| 222 | 235 | } |
| 223 | 236 | |
| @@ -615,7 +628,8 @@ | ||
| 615 | 628 | // The count increments on each retry. |
| 616 | 629 | static class QueryState { |
| 617 | 630 | static final QueryState INITIAL = new QueryState(-1, false); |
| 618 | - static final QueryState CANCELLED = new QueryState(Integer.MIN_VALUE, false); | |
| 631 | + static final QueryState CANCELLED_WHILE_IN_PROGRESS = new QueryState(Integer.MIN_VALUE, false); | |
| 632 | + static final QueryState CANCELLED_WHILE_COMPLETE = new QueryState(Integer.MIN_VALUE + 1, false); | |
| 619 | 633 | |
| 620 | 634 | final int retryCount; |
| 621 | 635 | final boolean inProgress; |
| @@ -639,9 +653,13 @@ | ||
| 639 | 653 | return new QueryState(retryCount + 1, true); |
| 640 | 654 | } |
| 641 | 655 | |
| 656 | + public boolean isCancelled() { | |
| 657 | + return this == CANCELLED_WHILE_IN_PROGRESS || this == CANCELLED_WHILE_COMPLETE; | |
| 658 | + } | |
| 659 | + | |
| 642 | 660 | @Override |
| 643 | 661 | public String toString() { |
| 644 | - return String.format("QueryState(count=%d, inProgress=%s)", retryCount, inProgress); | |
| 662 | + return String.format("QueryState(count=%d, inProgress=%s, cancelled=%s)", retryCount, inProgress, isCancelled()); | |
| 645 | 663 | } |
| 646 | 664 | } |
| 647 | 665 | } |