kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | bc214054949d1d5b1ffeb5414a053563028178fb8620835b1c0e0c7641360a1f |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-10-02 21:55 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-10-02 21:56 +02:00 |
| Parents | 79e97cbc6bb9, e968aa87642c |
8 files changed
modified driver-core/CHANGELOG.rst +2 −2
| @@ -19,9 +19,9 @@ | ||
| 19 | 19 | Merged from 2.0 branch: |
| 20 | 20 | |
| 21 | 21 | - [bug] Handle null pool in PooledConnection.release (JAVA-449) |
| 22 | -- [bug] Prevent concurrent retries of the same query (JAVA-449) | |
| 23 | 22 | - [improvement] Defunct connection on request timeout (JAVA-425) |
| 24 | 23 | - [improvement] Try next host when we get a SERVER_ERROR (JAVA-426) |
| 24 | +- [bug] Handle race between query timeout and completion (JAVA-449, JAVA-460, JAVA-471) | |
| 25 | 25 | |
| 26 | 26 | |
| 27 | 27 | 2.1.1: |
| @@ -89,9 +89,9 @@ | ||
| 89 | 89 | ------ |
| 90 | 90 | |
| 91 | 91 | - [bug] Handle null pool in PooledConnection.release (JAVA-449) |
| 92 | -- [bug] Prevent concurrent retries of the same query (JAVA-449) | |
| 93 | 92 | - [improvement] Defunct connection on request timeout (JAVA-425) |
| 94 | 93 | - [improvement] Try next host when we get a SERVER_ERROR (JAVA-426) |
| 94 | +- [bug] Handle race between query timeout and completion (JAVA-449, JAVA-460, JAVA-471) | |
| 95 | 95 | |
| 96 | 96 | |
| 97 | 97 | 2.0.6: |
modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedResultSet.java +9 −3
| @@ -358,21 +358,27 @@ | ||
| 358 | 358 | |
| 359 | 359 | // This is only called for internal calls, so don't bother with ExecutionInfo |
| 360 | 360 | @Override |
| 361 | - public void onSet(Connection connection, Message.Response response, long latency) { | |
| 361 | + public void onSet(Connection connection, Message.Response response, long latency, int retryCount) { | |
| 362 | 362 | onSet(connection, response, null, null, latency); |
| 363 | 363 | } |
| 364 | 364 | |
| 365 | 365 | @Override |
| 366 | - public void onException(Connection connection, Exception exception, long latency) { | |
| 366 | + public void onException(Connection connection, Exception exception, long latency, int retryCount) { | |
| 367 | 367 | future.setException(exception); |
| 368 | 368 | } |
| 369 | 369 | |
| 370 | 370 | @Override |
| 371 | - public void onTimeout(Connection connection, long latency) { | |
| 371 | + public boolean onTimeout(Connection connection, long latency, int retryCount) { | |
| 372 | 372 | // This won't be called directly since this will be wrapped by RequestHandler. |
| 373 | 373 | throw new UnsupportedOperationException(); |
| 374 | 374 | } |
| 375 | 375 | |
| 376 | + @Override | |
| 377 | + public int retryCount() { | |
| 378 | + // This is only called for internal calls (i.e, when the callback is not wrapped in RequestHandler). | |
| 379 | + // There is no retry logic in that case, so the value does not really matter. | |
| 380 | + return 0; | |
| 381 | + } | |
| 376 | 382 | }, statement); |
| 377 | 383 | |
| 378 | 384 | return future; |
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +38 −8
| @@ -50,6 +50,8 @@ | ||
| 50 | 50 | // we still want to avoid duplicating too much code so we wrap. |
| 51 | 51 | final DataWrapper wrapper; |
| 52 | 52 | |
| 53 | + private ByteBuffer routingKey; | |
| 54 | + | |
| 53 | 55 | /** |
| 54 | 56 | * Creates a new {@code BoundStatement} from the provided prepared |
| 55 | 57 | * statement. |
| @@ -99,7 +101,11 @@ | ||
| 99 | 101 | * |
| 100 | 102 | * @param name the name of the variable to check. |
| 101 | 103 | * @return whether the first occurrence of variable {@code name} has been |
| 104 | +<<<<<<< HEAD | |
| 102 | 105 | * bound. |
| 106 | +======= | |
| 107 | + * bound to a non-null value. | |
| 108 | +>>>>>>> 2.0 | |
| 103 | 109 | * |
| 104 | 110 | * @throws IllegalArgumentException if {@code name} is not a prepared |
| 105 | 111 | * variable, that is if {@code !this.preparedStatement().variables().names().contains(name)}. |
| @@ -204,28 +210,52 @@ | ||
| 204 | 210 | return this; |
| 205 | 211 | } |
| 206 | 212 | |
| 213 | + /** | |
| 214 | + * Sets the routing key for this bound statement. | |
| 215 | + * <p> | |
| 216 | + * This is useful when the routing key can neither be set on the {@code PreparedStatement} this bound statement | |
| 217 | + * was built from, nor automatically computed from bound variables. In particular, this is the case if the | |
| 218 | + * partition key is composite and only some of its components are bound. | |
| 219 | + * | |
| 220 | + * @param routingKey the raw (binary) value to use as routing key. | |
| 221 | + * @return this {@code BoundStatement} object. | |
| 222 | + * | |
| 223 | + * @see BoundStatement#getRoutingKey | |
| 224 | + */ | |
| 225 | + public BoundStatement setRoutingKey(ByteBuffer routingKey) { | |
| 226 | + this.routingKey = routingKey; | |
| 227 | + return this; | |
| 228 | + } | |
| 229 | + | |
| 207 | 230 | /** |
| 208 | 231 | * The routing key for this bound query. |
| 209 | 232 | * <p> |
| 210 | 233 | * This method will return a non-{@code null} value if either of the following occur: |
| 211 | 234 | * <ul> |
| 212 | - * <li>All the columns composing the partition key are bound | |
| 213 | - * variables of this {@code BoundStatement}. The routing key will then be | |
| 214 | - * built using the values provided for these partition key columns.</li> | |
| 215 | - * <li>The routing key has been set through {@link PreparedStatement#setRoutingKey} | |
| 216 | - * for the {@code PreparedStatement} this statement has been built from.</li> | |
| 235 | + * <li>The routing key has been set directly through {@link BoundStatement#setRoutingKey}.</li> | |
| 236 | + * <li>The routing key has been set through {@link PreparedStatement#setRoutingKey} for the | |
| 237 | + * {@code PreparedStatement} this statement has been built from.</li> | |
| 238 | + * <li>All the columns composing the partition key are bound variables of this {@code BoundStatement}. The routing | |
| 239 | + * key will then be built using the values provided for these partition key columns.</li> | |
| 217 | 240 | * </ul> |
| 218 | 241 | * Otherwise, {@code null} is returned. |
| 219 | 242 | * <p> |
| 220 | - * Note that if the routing key has been set through {@link PreparedStatement#setRoutingKey}, | |
| 221 | - * that latter value takes precedence even if the partition key is part of the bound variables. | |
| 243 | + * | |
| 244 | + * Note that if the routing key has been set through {@link BoundStatement#setRoutingKey}, then that takes | |
| 245 | + * precedence. If the routing key has been set through {@link PreparedStatement#setRoutingKey} then that is used | |
| 246 | + * next. If neither of those are set then it is computed. | |
| 222 | 247 | * |
| 223 | 248 | * @return the routing key for this statement or {@code null}. |
| 224 | 249 | */ |
| 225 | 250 | @Override |
| 226 | 251 | public ByteBuffer getRoutingKey() { |
| 227 | - if (statement.getRoutingKey() != null) | |
| 252 | + if (this.routingKey != null) { | |
| 253 | + return this.routingKey; | |
| 254 | + } | |
| 255 | + | |
| 256 | + if (statement.getRoutingKey() != null) { | |
| 228 | 257 | return statement.getRoutingKey(); |
| 258 | + } | |
| 229 | 259 | |
| 230 | 260 | int[] rkIndexes = statement.getPreparedId().routingKeyIndexes; |
| 231 | 261 | if (rkIndexes != null) { |
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +22 −12
| @@ -16,7 +16,6 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import javax.net.ssl.SSLEngine; |
| 19 | -import java.net.InetAddress; | |
| 20 | 19 | import java.net.InetSocketAddress; |
| 21 | 20 | import java.util.Iterator; |
| 22 | 21 | import java.util.concurrent.*; |
| @@ -388,7 +387,7 @@ | ||
| 388 | 387 | } else { |
| 389 | 388 | ce = new TransportException(address, "Error writing", writeFuture.getCause()); |
| 390 | 389 | } |
| 391 | - handler.callback.onException(Connection.this, defunct(ce), System.nanoTime() - handler.startTime); | |
| 390 | + handler.callback.onException(Connection.this, defunct(ce), System.nanoTime() - handler.startTime, handler.retryCount); | |
| 392 | 391 | } else { |
| 393 | 392 | logger.trace("{} request sent successfully", Connection.this); |
| 394 | 393 | } |
| @@ -659,7 +658,7 @@ | ||
| 659 | 658 | return; |
| 660 | 659 | } |
| 661 | 660 | handler.cancelTimeout(); |
| 662 | - handler.callback.onSet(Connection.this, response, System.nanoTime() - handler.startTime); | |
| 661 | + handler.callback.onSet(Connection.this, response, System.nanoTime() - handler.startTime, handler.retryCount); | |
| 663 | 662 | |
| 664 | 663 | // If we happen to be closed and we're the last outstanding request, we need to terminate the connection |
| 665 | 664 | // (note: this is racy as the signaling can be called more than once, but that's not a problem) |
| @@ -698,7 +697,7 @@ | ||
| 698 | 697 | { |
| 699 | 698 | ResponseHandler handler = iter.next(); |
| 700 | 699 | handler.cancelTimeout(); |
| 701 | - handler.callback.onException(Connection.this, ce, System.nanoTime() - handler.startTime); | |
| 700 | + handler.callback.onException(Connection.this, ce, System.nanoTime() - handler.startTime, handler.retryCount); | |
| 702 | 701 | iter.remove(); |
| 703 | 702 | } |
| 704 | 703 | } |
| @@ -765,19 +764,25 @@ | ||
| 765 | 764 | return request; |
| 766 | 765 | } |
| 767 | 766 | |
| 767 | + @Override | |
| 768 | + public int retryCount() { | |
| 769 | + // This is ignored, as there is no retry logic in this class | |
| 770 | + return 0; | |
| 771 | + } | |
| 772 | + | |
| 768 | 773 | @Override |
| 769 | 774 | public void onSet(Connection connection, Message.Response response, ExecutionInfo info, Statement statement, long latency) { |
| 770 | - onSet(connection, response, latency); | |
| 775 | + onSet(connection, response, latency, 0); | |
| 771 | 776 | } |
| 772 | 777 | |
| 773 | 778 | @Override |
| 774 | - public void onSet(Connection connection, Message.Response response, long latency) { | |
| 779 | + public void onSet(Connection connection, Message.Response response, long latency, int retryCount) { | |
| 775 | 780 | this.address = connection.address; |
| 776 | 781 | super.set(response); |
| 777 | 782 | } |
| 778 | 783 | |
| 779 | 784 | @Override |
| 780 | - public void onException(Connection connection, Exception exception, long latency) { | |
| 785 | + public void onException(Connection connection, Exception exception, long latency, int retryCount) { | |
| 781 | 786 | // If all nodes are down, we will get a null connection here. This is fine, if we have |
| 782 | 787 | // an exception, consumers shouldn't assume the address is not null. |
| 783 | 788 | if (connection != null) |
| @@ -786,10 +791,11 @@ | ||
| 786 | 791 | } |
| 787 | 792 | |
| 788 | 793 | @Override |
| 789 | - public void onTimeout(Connection connection, long latency) { | |
| 794 | + public boolean onTimeout(Connection connection, long latency, int retryCount) { | |
| 790 | 795 | assert connection != null; // We always timeout on a specific connection, so this shouldn't be null |
| 791 | 796 | this.address = connection.address; |
| 792 | 797 | super.setException(new ConnectionException(connection.address, "Operation timed out")); |
| 798 | + return true; | |
| 793 | 799 | } |
| 794 | 800 | |
| 795 | 801 | public InetSocketAddress getAddress() { |
| @@ -799,9 +805,10 @@ | ||
| 799 | 805 | |
| 800 | 806 | interface ResponseCallback { |
| 801 | 807 | public Message.Request request(); |
| 802 | - public void onSet(Connection connection, Message.Response response, long latency); | |
| 803 | - public void onException(Connection connection, Exception exception, long latency); | |
| 804 | - public void onTimeout(Connection connection, long latency); | |
| 808 | + public int retryCount(); | |
| 809 | + public void onSet(Connection connection, Message.Response response, long latency, int retryCount); | |
| 810 | + public void onException(Connection connection, Exception exception, long latency, int retryCount); | |
| 811 | + public boolean onTimeout(Connection connection, long latency, int retryCount); | |
| 805 | 812 | } |
| 806 | 813 | |
| 807 | 814 | static class ResponseHandler { |
| @@ -809,6 +816,7 @@ | ||
| 809 | 816 | public final Connection connection; |
| 810 | 817 | public final int streamId; |
| 811 | 818 | public final ResponseCallback callback; |
| 819 | + public final int retryCount; | |
| 812 | 820 | |
| 813 | 821 | private final Timeout timeout; |
| 814 | 822 | private final long startTime; |
| @@ -817,6 +825,7 @@ | ||
| 817 | 825 | this.connection = connection; |
| 818 | 826 | this.streamId = connection.dispatcher.streamIdHandler.next(); |
| 819 | 827 | this.callback = callback; |
| 828 | + this.retryCount = callback.retryCount(); | |
| 820 | 829 | |
| 821 | 830 | long timeoutMs = connection.factory.getReadTimeoutMillis(); |
| 822 | 831 | this.timeout = timeoutMs <= 0 ? null : connection.factory.timer.newTimeout(onTimeoutTask(), timeoutMs, TimeUnit.MILLISECONDS); |
| @@ -843,7 +852,8 @@ | ||
| 843 | 852 | return new TimerTask() { |
| 844 | 853 | @Override |
| 845 | 854 | public void run(Timeout timeout) { |
| 846 | - callback.onTimeout(connection, System.nanoTime() - startTime); | |
| 855 | + if (callback.onTimeout(connection, System.nanoTime() - startTime, retryCount)) | |
| 856 | + cancelHandler(); | |
| 847 | 857 | } |
| 848 | 858 | }; |
| 849 | 859 | } |
modified driver-core/src/main/java/com/datastax/driver/core/DefaultResultSetFuture.java +12 −4
| @@ -122,22 +122,23 @@ | ||
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | @Override |
| 125 | - public void onSet(Connection connection, Message.Response response, long latency) { | |
| 125 | + public void onSet(Connection connection, Message.Response response, long latency, int retryCount) { | |
| 126 | 126 | // This is only called for internal calls (i.e, when the callback is not wrapped in ResponseHandler), |
| 127 | 127 | // so don't bother with ExecutionInfo. |
| 128 | 128 | onSet(connection, response, null, null, latency); |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | @Override |
| 132 | - public void onException(Connection connection, Exception exception, long latency) { | |
| 132 | + public void onException(Connection connection, Exception exception, long latency, int retryCount) { | |
| 133 | 133 | setException(exception); |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | @Override |
| 137 | - public void onTimeout(Connection connection, long latency) { | |
| 138 | - // This is only called for internal calls (i.e, when the callback is not wrapped in ResponseHandler). | |
| 137 | + public boolean onTimeout(Connection connection, long latency, int retryCount) { | |
| 138 | + // This is only called for internal calls (i.e, when the future is not wrapped in RequestHandler). | |
| 139 | 139 | // So just set an exception for the final result, which should be handled correctly by said internal call. |
| 140 | 140 | setException(new ConnectionException(connection.address, "Operation timed out")); |
| 141 | + return true; | |
| 141 | 142 | } |
| 142 | 143 | |
| 143 | 144 | // We sometimes need (in the driver) to set the future from outside this class, |
| @@ -259,4 +260,11 @@ | ||
| 259 | 260 | else |
| 260 | 261 | throw new DriverInternalError("Unexpected exception thrown", e.getCause()); |
| 261 | 262 | } |
| 263 | + | |
| 264 | + @Override | |
| 265 | + public int retryCount() { | |
| 266 | + // This is only called for internal calls (i.e, when the future is not wrapped in RequestHandler). | |
| 267 | + // There is no retry logic in that case, so the value does not really matter. | |
| 268 | + return 0; | |
| 269 | + } | |
| 262 | 270 | } |
modified driver-core/src/main/java/com/datastax/driver/core/PreparedStatement.java +5 −0
| @@ -98,11 +98,16 @@ | ||
| 98 | 98 | * <p> |
| 99 | 99 | * Note that if the partition key is part of the prepared variables, the |
| 100 | 100 | * routing key will be automatically computed once those variables are bound. |
| 101 | + * <p> | |
| 102 | + * If the partition key is neither fixed nor part of the prepared variables (e.g. | |
| 103 | + * a composite partition key where only some of the components are bound), the | |
| 104 | + * routing key can also be set on each bound statement. | |
| 101 | 105 | * |
| 102 | 106 | * @param routingKey the raw (binary) value to use as routing key. |
| 103 | 107 | * @return this {@code PreparedStatement} object. |
| 104 | 108 | * |
| 105 | 109 | * @see Statement#getRoutingKey |
| 110 | + * @see BoundStatement#getRoutingKey | |
| 106 | 111 | */ |
| 107 | 112 | public PreparedStatement setRoutingKey(ByteBuffer routingKey); |
| 108 | 113 |
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +119 −51
| @@ -15,7 +15,6 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import java.net.InetAddress; | |
| 19 | 18 | import java.net.InetSocketAddress; |
| 20 | 19 | import java.nio.ByteBuffer; |
| 21 | 20 | import java.util.ArrayList; |
| @@ -24,9 +23,9 @@ | ||
| 24 | 23 | import java.util.Iterator; |
| 25 | 24 | import java.util.List; |
| 26 | 25 | import java.util.Map; |
| 27 | -import java.util.concurrent.*; | |
| 28 | -import java.util.concurrent.locks.Lock; | |
| 29 | -import java.util.concurrent.locks.ReentrantLock; | |
| 26 | +import java.util.concurrent.TimeUnit; | |
| 27 | +import java.util.concurrent.TimeoutException; | |
| 28 | +import java.util.concurrent.atomic.AtomicReference; | |
| 30 | 29 | |
| 31 | 30 | import com.codahale.metrics.Timer; |
| 32 | 31 | import org.slf4j.Logger; |
| @@ -39,7 +38,6 @@ | ||
| 39 | 38 | import com.datastax.driver.core.exceptions.UnavailableException; |
| 40 | 39 | import com.datastax.driver.core.exceptions.WriteTimeoutException; |
| 41 | 40 | import com.datastax.driver.core.policies.*; |
| 42 | -import com.datastax.driver.core.policies.RetryPolicy.RetryDecision; | |
| 43 | 41 | import com.datastax.driver.core.policies.RetryPolicy.RetryDecision.Type; |
| 44 | 42 | |
| 45 | 43 | /** |
| @@ -47,7 +45,6 @@ | ||
| 47 | 45 | * unavailable/timeout. |
| 48 | 46 | */ |
| 49 | 47 | class RequestHandler implements Connection.ResponseCallback { |
| 50 | - | |
| 51 | 48 | private static final Logger logger = LoggerFactory.getLogger(RequestHandler.class); |
| 52 | 49 | |
| 53 | 50 | private final SessionManager manager; |
| @@ -58,10 +55,8 @@ | ||
| 58 | 55 | private volatile Host current; |
| 59 | 56 | private volatile List<Host> triedHosts; |
| 60 | 57 | private volatile HostConnectionPool currentPool; |
| 58 | + private final AtomicReference<QueryState> queryStateRef; | |
| 61 | 59 | |
| 62 | - // Note that we know that queryRetries can only be incremented by one writer at a time so a | |
| 63 | - // volatile is good enough. | |
| 64 | - private volatile int queryRetries; | |
| 65 | 60 | private volatile ConsistencyLevel retryConsistencyLevel; |
| 66 | 61 | |
| 67 | 62 | private volatile Map<InetSocketAddress, Throwable> errors; |
| @@ -72,10 +67,6 @@ | ||
| 72 | 67 | private final Timer.Context timerContext; |
| 73 | 68 | private final long startTime; |
| 74 | 69 | |
| 75 | - // Tracks whether there is a retry already in progress | |
| 76 | - private final Lock retryLock = new ReentrantLock(); | |
| 77 | - private Future<?> currentRetry; // guarded by retryLock | |
| 78 | - | |
| 79 | 70 | public RequestHandler(SessionManager manager, Callback callback, Statement statement) { |
| 80 | 71 | this.manager = manager; |
| 81 | 72 | this.callback = callback; |
| @@ -84,6 +75,7 @@ | ||
| 84 | 75 | |
| 85 | 76 | this.queryPlan = manager.loadBalancingPolicy().newQueryPlan(manager.poolsState.keyspace, statement); |
| 86 | 77 | this.statement = statement; |
| 78 | + this.queryStateRef = new AtomicReference<QueryState>(QueryState.INITIAL); | |
| 87 | 79 | |
| 88 | 80 | this.timerContext = metricsEnabled() |
| 89 | 81 | ? metrics().getRequestsTimer().time() |
| @@ -167,34 +159,26 @@ | ||
| 167 | 159 | } |
| 168 | 160 | |
| 169 | 161 | private void retry(final boolean retryCurrent, ConsistencyLevel newConsistencyLevel) { |
| 170 | - try { | |
| 171 | - // We lock to prevent two retries from launching concurrently | |
| 172 | - retryLock.lock(); | |
| 162 | + queryStateRef.set(queryStateRef.get().startNext()); | |
| 173 | 163 | |
| 174 | - if (currentRetry != null && !currentRetry.isDone()) | |
| 175 | - return; | |
| 164 | + final Host h = current; | |
| 165 | + this.retryConsistencyLevel = newConsistencyLevel; | |
| 176 | 166 | |
| 177 | - final Host h = current; | |
| 178 | - this.retryConsistencyLevel = newConsistencyLevel; | |
| 179 | - | |
| 180 | - // We should not retry on the current thread as this will be an IO thread. | |
| 181 | - currentRetry = manager.executor().submit(new Runnable() { | |
| 182 | - @Override | |
| 183 | - public void run() { | |
| 184 | - try { | |
| 185 | - if (retryCurrent) { | |
| 186 | - if (query(h)) | |
| 187 | - return; | |
| 188 | - } | |
| 189 | - sendRequest(); | |
| 190 | - } catch (Exception e) { | |
| 191 | - setFinalException(null, new DriverInternalError("Unexpected exception while retrying query", e)); | |
| 167 | + // We should not retry on the current thread as this will be an IO thread. | |
| 168 | + manager.executor().execute(new Runnable() { | |
| 169 | + @Override | |
| 170 | + public void run() { | |
| 171 | + try { | |
| 172 | + if (retryCurrent) { | |
| 173 | + if (query(h)) | |
| 174 | + return; | |
| 192 | 175 | } |
| 176 | + sendRequest(); | |
| 177 | + } catch (Exception e) { | |
| 178 | + setFinalException(null, new DriverInternalError("Unexpected exception while retrying query", e)); | |
| 193 | 179 | } |
| 194 | - }); | |
| 195 | - } finally { | |
| 196 | - retryLock.unlock(); | |
| 197 | - } | |
| 180 | + } | |
| 181 | + }); | |
| 198 | 182 | } |
| 199 | 183 | |
| 200 | 184 | public void cancel() { |
| @@ -261,7 +245,7 @@ | ||
| 261 | 245 | info = info.withAchievedConsistency(retryConsistencyLevel); |
| 262 | 246 | callback.onSet(connection, response, info, statement, System.nanoTime() - startTime); |
| 263 | 247 | } catch (Exception e) { |
| 264 | - callback.onException(connection, new DriverInternalError("Unexpected exception while setting final result from " + response, e), System.nanoTime() - startTime); | |
| 248 | + callback.onException(connection, new DriverInternalError("Unexpected exception while setting final result from " + response, e), System.nanoTime() - startTime, retryCount()); | |
| 265 | 249 | } |
| 266 | 250 | } |
| 267 | 251 | |
| @@ -270,12 +254,20 @@ | ||
| 270 | 254 | if (timerContext != null) |
| 271 | 255 | timerContext.stop(); |
| 272 | 256 | } finally { |
| 273 | - callback.onException(connection, exception, System.nanoTime() - startTime); | |
| 257 | + callback.onException(connection, exception, System.nanoTime() - startTime, retryCount()); | |
| 274 | 258 | } |
| 275 | 259 | } |
| 276 | 260 | |
| 277 | 261 | @Override |
| 278 | - public void onSet(Connection connection, Message.Response response, long latency) { | |
| 262 | + public void onSet(Connection connection, Message.Response response, long latency, int retryCount) { | |
| 263 | + QueryState queryState = queryStateRef.get(); | |
| 264 | + if (!queryState.isInProgressAt(retryCount) || | |
| 265 | + !queryStateRef.compareAndSet(queryState, queryState.complete())) { | |
| 266 | + logger.debug("onSet triggered but the response was completed by another thread, cancelling (retryCount = {}, queryState = {}, queryStateRef = {})", | |
| 267 | + retryCount, queryState, queryStateRef.get()); | |
| 268 | + return; | |
| 269 | + } | |
| 270 | + | |
| 279 | 271 | Host queriedHost = current; |
| 280 | 272 | try { |
| 281 | 273 | if (connection instanceof PooledConnection) |
| @@ -303,7 +295,7 @@ | ||
| 303 | 295 | rte.getRequiredAcknowledgements(), |
| 304 | 296 | rte.getReceivedAcknowledgements(), |
| 305 | 297 | rte.wasDataRetrieved(), |
| 306 | - queryRetries); | |
| 298 | + retryCount); | |
| 307 | 299 | |
| 308 | 300 | if (metricsEnabled()) { |
| 309 | 301 | if (retry.getType() == Type.RETRY) |
| @@ -323,7 +315,7 @@ | ||
| 323 | 315 | wte.getWriteType(), |
| 324 | 316 | wte.getRequiredAcknowledgements(), |
| 325 | 317 | wte.getReceivedAcknowledgements(), |
| 326 | - queryRetries); | |
| 318 | + retryCount); | |
| 327 | 319 | |
| 328 | 320 | if (metricsEnabled()) { |
| 329 | 321 | if (retry.getType() == Type.RETRY) |
| @@ -342,7 +334,7 @@ | ||
| 342 | 334 | ue.getConsistencyLevel(), |
| 343 | 335 | ue.getRequiredReplicas(), |
| 344 | 336 | ue.getAliveReplicas(), |
| 345 | - queryRetries); | |
| 337 | + retryCount); | |
| 346 | 338 | |
| 347 | 339 | if (metricsEnabled()) { |
| 348 | 340 | if (retry.getType() == Type.RETRY) |
| @@ -403,6 +395,7 @@ | ||
| 403 | 395 | connection.setKeyspace(prepareKeyspace); |
| 404 | 396 | } |
| 405 | 397 | |
| 398 | + queryStateRef.set(queryStateRef.get().startNext()); | |
| 406 | 399 | try { |
| 407 | 400 | connection.write(prepareAndRetry(toPrepare.getQueryString())); |
| 408 | 401 | } finally { |
| @@ -426,9 +419,8 @@ | ||
| 426 | 419 | else { |
| 427 | 420 | switch (retry.getType()) { |
| 428 | 421 | case RETRY: |
| 429 | - ++queryRetries; | |
| 430 | 422 | if (logger.isDebugEnabled()) |
| 431 | - logger.debug("Doing retry {} for query {} at consistency {}", queryRetries, statement, retry.getRetryConsistencyLevel()); | |
| 423 | + logger.debug("Doing retry {} for query {} at consistency {}", retryCount, statement, retry.getRetryConsistencyLevel()); | |
| 432 | 424 | if (metricsEnabled()) |
| 433 | 425 | metrics().getErrorMetrics().getRetries().inc(); |
| 434 | 426 | retry(true, retry.getRetryConsistencyLevel()); |
| @@ -465,7 +457,20 @@ | ||
| 465 | 457 | } |
| 466 | 458 | |
| 467 | 459 | @Override |
| 468 | - public void onSet(Connection connection, Message.Response response, long latency) { | |
| 460 | + public int retryCount() { | |
| 461 | + return RequestHandler.this.retryCount(); | |
| 462 | + } | |
| 463 | + | |
| 464 | + @Override | |
| 465 | + public void onSet(Connection connection, Message.Response response, long latency, int retryCount) { | |
| 466 | + QueryState queryState = queryStateRef.get(); | |
| 467 | + if (!queryState.isInProgressAt(retryCount) || | |
| 468 | + !queryStateRef.compareAndSet(queryState, queryState.complete())) { | |
| 469 | + logger.debug("onSet triggered but the response was completed by another thread, cancelling (retryCount = {}, queryState = {}, queryStateRef = {})", | |
| 470 | + retryCount, queryState, queryStateRef.get()); | |
| 471 | + return; | |
| 472 | + } | |
| 473 | + | |
| 469 | 474 | // TODO should we check the response ? |
| 470 | 475 | switch (response.type) { |
| 471 | 476 | case RESULT: |
| @@ -491,20 +496,35 @@ | ||
| 491 | 496 | } |
| 492 | 497 | |
| 493 | 498 | @Override |
| 494 | - public void onException(Connection connection, Exception exception, long latency) { | |
| 495 | - RequestHandler.this.onException(connection, exception, latency); | |
| 499 | + public void onException(Connection connection, Exception exception, long latency, int retryCount) { | |
| 500 | + RequestHandler.this.onException(connection, exception, latency, retryCount); | |
| 496 | 501 | } |
| 497 | 502 | |
| 498 | 503 | @Override |
| 499 | - public void onTimeout(Connection connection, long latency) { | |
| 504 | + public boolean onTimeout(Connection connection, long latency, int retryCount) { | |
| 505 | + QueryState queryState = queryStateRef.get(); | |
| 506 | + if (!queryState.isInProgressAt(retryCount) || | |
| 507 | + !queryStateRef.compareAndSet(queryState, queryState.complete())) { | |
| 508 | + logger.debug("onTimeout triggered but the response was completed by another thread, cancelling (retryCount = {}, queryState = {}, queryStateRef = {})", | |
| 509 | + retryCount, queryState, queryStateRef.get()); | |
| 510 | + return false; | |
| 511 | + } | |
| 500 | 512 | logError(connection.address, new DriverException("Timeout waiting for response to prepare message")); |
| 501 | 513 | retry(false, null); |
| 514 | + return true; | |
| 502 | 515 | } |
| 503 | 516 | }; |
| 504 | 517 | } |
| 505 | 518 | |
| 506 | 519 | @Override |
| 507 | - public void onException(Connection connection, Exception exception, long latency) { | |
| 520 | + public void onException(Connection connection, Exception exception, long latency, int retryCount) { | |
| 521 | + QueryState queryState = queryStateRef.get(); | |
| 522 | + if (!queryState.isInProgressAt(retryCount) || | |
| 523 | + !queryStateRef.compareAndSet(queryState, queryState.complete())) { | |
| 524 | + logger.debug("onException triggered but the response was completed by another thread, cancelling (retryCount = {}, queryState = {}, queryStateRef = {})", | |
| 525 | + retryCount, queryState, queryStateRef.get()); | |
| 526 | + return; | |
| 527 | + } | |
| 508 | 528 | |
| 509 | 529 | Host queriedHost = current; |
| 510 | 530 | try { |
| @@ -530,7 +550,15 @@ | ||
| 530 | 550 | } |
| 531 | 551 | |
| 532 | 552 | @Override |
| 533 | - public void onTimeout(Connection connection, long latency) { | |
| 553 | + public boolean onTimeout(Connection connection, long latency, int retryCount) { | |
| 554 | + QueryState queryState = queryStateRef.get(); | |
| 555 | + if (!queryState.isInProgressAt(retryCount) || | |
| 556 | + !queryStateRef.compareAndSet(queryState, queryState.complete())) { | |
| 557 | + logger.debug("onTimeout triggered but the response was completed by another thread, cancelling (retryCount = {}, queryState = {}, queryStateRef = {})", | |
| 558 | + retryCount, queryState, queryStateRef.get()); | |
| 559 | + return false; | |
| 560 | + } | |
| 561 | + | |
| 534 | 562 | Host queriedHost = current; |
| 535 | 563 | try { |
| 536 | 564 | // If a query times out, we consider that the host is unstable, so we defunct |
| @@ -547,10 +575,50 @@ | ||
| 547 | 575 | if (queriedHost != null) |
| 548 | 576 | manager.cluster.manager.reportLatency(queriedHost, latency); |
| 549 | 577 | } |
| 578 | + return true; | |
| 579 | + } | |
| 580 | + | |
| 581 | + @Override | |
| 582 | + public int retryCount() { | |
| 583 | + return queryStateRef.get().retryCount; | |
| 550 | 584 | } |
| 551 | 585 | |
| 552 | 586 | interface Callback extends Connection.ResponseCallback { |
| 553 | 587 | public void onSet(Connection connection, Message.Response response, ExecutionInfo info, Statement statement, long latency); |
| 554 | 588 | public void register(RequestHandler handler); |
| 555 | 589 | } |
| 590 | + | |
| 591 | + // This is used to prevent races between request completion (either success or error) and timeout. | |
| 592 | + // A retry is in progress once we have written the request to the connection and until we get back a response or a timeout. | |
| 593 | + // The count increments on each retry. | |
| 594 | + static class QueryState { | |
| 595 | + static QueryState INITIAL = new QueryState(0, true); | |
| 596 | + | |
| 597 | + final int retryCount; | |
| 598 | + final boolean inProgress; | |
| 599 | + | |
| 600 | + private QueryState(int count, boolean inProgress) { | |
| 601 | + this.retryCount = count; | |
| 602 | + this.inProgress = inProgress; | |
| 603 | + } | |
| 604 | + | |
| 605 | + boolean isInProgressAt(int retryCount) { | |
| 606 | + return inProgress && this.retryCount == retryCount; | |
| 607 | + } | |
| 608 | + | |
| 609 | + QueryState complete() { | |
| 610 | + assert inProgress; | |
| 611 | + return new QueryState(retryCount, false); | |
| 612 | + } | |
| 613 | + | |
| 614 | + QueryState startNext() { | |
| 615 | + assert !inProgress; | |
| 616 | + return new QueryState(retryCount + 1, true); | |
| 617 | + } | |
| 618 | + | |
| 619 | + @Override | |
| 620 | + public String toString() { | |
| 621 | + return String.format("QueryState(count=%d, inProgress=%s)", retryCount, inProgress); | |
| 622 | + } | |
| 623 | + } | |
| 556 | 624 | } |
modified pom.xml +1 −0
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | <groupId>org.sonatype.oss</groupId> |
| 18 | 18 | <artifactId>oss-parent</artifactId> |
| 19 | 19 | <version>7</version> |
| 20 | + <relativePath></relativePath> | |
| 20 | 21 | </parent> |
| 21 | 22 | |
| 22 | 23 | <groupId>com.datastax.cassandra</groupId> |