kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 895c38bf77aa23666dbe72971a2ebfeb5f228a2c1315b1965aab4b85451a83a9 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-02-11 18:19 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-02-11 18:19 +01:00 |
| Parents | 7d498797e847, 1a570ed7d747 |
8 files changed
modified README.rst +1 −1
| @@ -37,7 +37,7 @@ | ||
| 37 | 37 | <dependency> |
| 38 | 38 | <groupId>com.datastax.cassandra</groupId> |
| 39 | 39 | <artifactId>cassandra-driver-core</artifactId> |
| 40 | - <version>2.0.9</version> | |
| 40 | + <version>2.0.9.2</version> | |
| 41 | 41 | </dependency> |
| 42 | 42 | |
| 43 | 43 | Compatibility |
modified driver-core/CHANGELOG.rst +20 −0
| @@ -10,6 +10,26 @@ | ||
| 10 | 10 | |
| 11 | 11 | Merged from 2.0.9_fixes branch: |
| 12 | 12 | |
| 13 | +- [bug] Prevent race between cancellation and query completion (JAVA-614) | |
| 14 | +- [bug] Prevent cancel and timeout from cancelling unrelated ResponseHandler if | |
| 15 | + streamId was already released and reused (JAVA-632). | |
| 16 | +- [bug] Fix issue when newly opened pool fails before we could mark the node UP | |
| 17 | + (JAVA-642) | |
| 18 | +- [bug] Fix unwanted LBP notifications when a contact host is down (JAVA-613) | |
| 19 | +- [bug] Fix edge cases where a connection was released twice (JAVA-651). | |
| 20 | +- [bug] Fix edge cases in query cancellation (JAVA-653). | |
| 21 | + | |
| 22 | + | |
| 23 | +2.0.9.2: | |
| 24 | +-------- | |
| 25 | + | |
| 26 | +- [bug] Fix edge cases where a connection was released twice (JAVA-651). | |
| 27 | +- [bug] Fix edge cases in query cancellation (JAVA-653). | |
| 28 | + | |
| 29 | + | |
| 30 | +2.0.9.1: | |
| 31 | +-------- | |
| 32 | + | |
| 13 | 33 | - [bug] Prevent race between cancellation and query completion (JAVA-614) |
| 14 | 34 | - [bug] Prevent cancel and timeout from cancelling unrelated ResponseHandler if |
| 15 | 35 | streamId was already released and reused (JAVA-632). |
modified driver-core/README.rst +1 −1
| @@ -60,7 +60,7 @@ | ||
| 60 | 60 | <dependency> |
| 61 | 61 | <groupId>com.datastax.cassandra</groupId> |
| 62 | 62 | <artifactId>cassandra-driver-core</artifactId> |
| 63 | - <version>2.0.9</version> | |
| 63 | + <version>2.0.9.2</version> | |
| 64 | 64 | </dependency> |
| 65 | 65 | |
| 66 | 66 |
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +29 −6
| @@ -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 | |
| @@ -318,11 +319,12 @@ | ||
| 318 | 319 | if (this.keyspace != null && this.keyspace.equals(keyspace)) |
| 319 | 320 | return; |
| 320 | 321 | |
| 322 | + Future future = null; | |
| 321 | 323 | try { |
| 322 | 324 | logger.trace("{} Setting keyspace {}", this, keyspace); |
| 323 | 325 | long timeout = factory.getConnectTimeoutMillis(); |
| 324 | 326 | // Note: we quote the keyspace below, because the name is the one coming from Cassandra, so it's in the right case already |
| 325 | - Future future = write(new Requests.Query("USE \"" + keyspace + '"')); | |
| 327 | + future = write(new Requests.Query("USE \"" + keyspace + '"')); | |
| 326 | 328 | Message.Response response = Uninterruptibles.getUninterruptibly(future, timeout, TimeUnit.MILLISECONDS); |
| 327 | 329 | switch (response.type) { |
| 328 | 330 | case RESULT: |
| @@ -340,9 +342,16 @@ | ||
| 340 | 342 | } catch (ConnectionException e) { |
| 341 | 343 | throw defunct(e); |
| 342 | 344 | } catch (TimeoutException e) { |
| 345 | + // We've given up waiting on the future, but it's still running. Cancel to make sure that the request timeout logic | |
| 346 | + // (readTimeout) will not kick in, because that would release the connection. This will work since connectTimeout is | |
| 347 | + // generally lower than readTimeout (and if not, we'll get an ExecutionException and defunct below). | |
| 348 | + future.cancel(true); | |
| 343 | 349 | logger.warn(String.format("Timeout while setting keyspace on connection to %s. This should not happen but is not critical (it will retried)", address)); |
| 350 | + // Rethrow so that the caller will not try to use the connection, but do not defunct as we don't want to mark down | |
| 351 | + throw new ConnectionException(address, "Timeout while setting keyspace on connection"); | |
| 344 | 352 | } catch (BusyConnectionException e) { |
| 345 | 353 | 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)); |
| 354 | + throw new ConnectionException(address, "Tried to set the keyspace on busy connection"); | |
| 346 | 355 | } catch (ExecutionException e) { |
| 347 | 356 | throw defunct(new ConnectionException(address, "Error while setting keyspace", e)); |
| 348 | 357 | } |
| @@ -364,6 +373,10 @@ | ||
| 364 | 373 | } |
| 365 | 374 | |
| 366 | 375 | public ResponseHandler write(ResponseCallback callback) throws ConnectionException, BusyConnectionException { |
| 376 | + return write(callback, true); | |
| 377 | + } | |
| 378 | + | |
| 379 | + public ResponseHandler write(ResponseCallback callback, boolean startTimeout) throws ConnectionException, BusyConnectionException { | |
| 367 | 380 | |
| 368 | 381 | Message.Request request = callback.request(); |
| 369 | 382 | |
| @@ -390,6 +403,10 @@ | ||
| 390 | 403 | logger.trace("{} writing request {}", this, request); |
| 391 | 404 | writer.incrementAndGet(); |
| 392 | 405 | channel.write(request).addListener(writeHandler(request, handler)); |
| 406 | + | |
| 407 | + if (startTimeout) | |
| 408 | + handler.startTimeout(); | |
| 409 | + | |
| 393 | 410 | return handler; |
| 394 | 411 | } |
| 395 | 412 | |
| @@ -882,8 +899,7 @@ | ||
| 882 | 899 | public boolean onTimeout(Connection connection, long latency, int retryCount) { |
| 883 | 900 | assert connection != null; // We always timeout on a specific connection, so this shouldn't be null |
| 884 | 901 | this.address = connection.address; |
| 885 | - super.setException(new ConnectionException(connection.address, "Operation timed out")); | |
| 886 | - return true; | |
| 902 | + return super.setException(new OperationTimedOutException(connection.address)); | |
| 887 | 903 | } |
| 888 | 904 | |
| 889 | 905 | public InetSocketAddress getAddress() { |
| @@ -906,8 +922,10 @@ | ||
| 906 | 922 | public final ResponseCallback callback; |
| 907 | 923 | public final int retryCount; |
| 908 | 924 | |
| 909 | - private final Timeout timeout; | |
| 910 | 925 | private final long startTime; |
| 926 | + private volatile Timeout timeout; | |
| 927 | + | |
| 928 | + private final AtomicBoolean isCancelled = new AtomicBoolean(); | |
| 911 | 929 | |
| 912 | 930 | public ResponseHandler(Connection connection, ResponseCallback callback) throws BusyConnectionException { |
| 913 | 931 | this.connection = connection; |
| @@ -915,10 +933,12 @@ | ||
| 915 | 933 | this.callback = callback; |
| 916 | 934 | this.retryCount = callback.retryCount(); |
| 917 | 935 | |
| 936 | + this.startTime = System.nanoTime(); | |
| 937 | + } | |
| 938 | + | |
| 939 | + void startTimeout() { | |
| 918 | 940 | long timeoutMs = connection.factory.getReadTimeoutMillis(); |
| 919 | 941 | this.timeout = timeoutMs <= 0 ? null : connection.factory.timer.newTimeout(onTimeoutTask(), timeoutMs, TimeUnit.MILLISECONDS); |
| 920 | - | |
| 921 | - this.startTime = System.nanoTime(); | |
| 922 | 942 | } |
| 923 | 943 | |
| 924 | 944 | void cancelTimeout() { |
| @@ -927,6 +947,9 @@ | ||
| 927 | 947 | } |
| 928 | 948 | |
| 929 | 949 | public void cancelHandler() { |
| 950 | + if (!isCancelled.compareAndSet(false, true)) | |
| 951 | + return; | |
| 952 | + | |
| 930 | 953 | // We haven't really received a response: we want to remove the handle because we gave up on that |
| 931 | 954 | // request and there is no point in holding the handler, but we don't release the streamId. If we |
| 932 | 955 | // 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/DefaultResultSetFuture.java +1 −1
| @@ -141,7 +141,7 @@ | ||
| 141 | 141 | public boolean onTimeout(Connection connection, long latency, int retryCount) { |
| 142 | 142 | // This is only called for internal calls (i.e, when the future is not wrapped in RequestHandler). |
| 143 | 143 | // So just set an exception for the final result, which should be handled correctly by said internal call. |
| 144 | - setException(new ConnectionException(connection.address, "Operation timed out")); | |
| 144 | + setException(new OperationTimedOutException(connection.address)); | |
| 145 | 145 | return true; |
| 146 | 146 | } |
| 147 | 147 |
added driver-core/src/main/java/com/datastax/driver/core/OperationTimedOutException.java +13 −0
| @@ -0,0 +1,13 @@ | ||
| 1 | +package com.datastax.driver.core; | |
| 2 | + | |
| 3 | +import java.net.InetSocketAddress; | |
| 4 | + | |
| 5 | +/** | |
| 6 | + * Thrown on a client-side timeout, i.e. when the client didn't hear back from the server within | |
| 7 | + * {@link SocketOptions#getReadTimeoutMillis()}. | |
| 8 | + */ | |
| 9 | +class OperationTimedOutException extends ConnectionException { | |
| 10 | + public OperationTimedOutException(InetSocketAddress address) { | |
| 11 | + super(address, "Operation timed out"); | |
| 12 | + } | |
| 13 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +61 −46
| @@ -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 | |
| @@ -293,10 +306,8 @@ | ||
| 293 | 306 | } |
| 294 | 307 | |
| 295 | 308 | Host queriedHost = current; |
| 309 | + boolean releaseConnection = true; | |
| 296 | 310 | try { |
| 297 | - if (connection instanceof PooledConnection) | |
| 298 | - ((PooledConnection)connection).release(); | |
| 299 | - | |
| 300 | 311 | switch (response.type) { |
| 301 | 312 | case RESULT: |
| 302 | 313 | setFinalResult(connection, response); |
| @@ -405,30 +416,23 @@ | ||
| 405 | 416 | return; |
| 406 | 417 | } |
| 407 | 418 | |
| 408 | - logger.info("Query {} is not prepared on {}, preparing before retrying executing. " | |
| 409 | - + "Seeing this message a few times is fine, but seeing it a lot may be source of performance problems", | |
| 410 | - toPrepare.getQueryString(), connection.address); | |
| 411 | 419 | String currentKeyspace = connection.keyspace(); |
| 412 | 420 | String prepareKeyspace = toPrepare.getQueryKeyspace(); |
| 413 | - // This shouldn't happen in normal use, because a user shouldn't try to execute | |
| 414 | - // a prepared statement with the wrong keyspace set. However, if it does, we'd rather | |
| 415 | - // prepare the query correctly and let the query executing return a meaningful error message | |
| 416 | - if (prepareKeyspace != null && (currentKeyspace == null || !currentKeyspace.equals(prepareKeyspace))) | |
| 417 | - { | |
| 418 | - logger.debug("Setting keyspace for prepared query to {}", prepareKeyspace); | |
| 419 | - connection.setKeyspace(prepareKeyspace); | |
| 421 | + if (prepareKeyspace != null && (currentKeyspace == null || !currentKeyspace.equals(prepareKeyspace))) { | |
| 422 | + // This shouldn't happen in normal use, because a user shouldn't try to execute | |
| 423 | + // a prepared statement with the wrong keyspace set. | |
| 424 | + // Fail fast (we can't change the keyspace to reprepare, because we're using a pooled connection | |
| 425 | + // that's shared with other requests). | |
| 426 | + throw new IllegalStateException(String.format("Statement was prepared on keyspace %s, can't execute it on %s (%s)", | |
| 427 | + toPrepare.getQueryKeyspace(), connection.keyspace(), toPrepare.getQueryString())); | |
| 420 | 428 | } |
| 421 | 429 | |
| 422 | - try { | |
| 423 | - write(connection, prepareAndRetry(toPrepare.getQueryString())); | |
| 424 | - } finally { | |
| 425 | - // Always reset the previous keyspace if needed | |
| 426 | - if (connection.keyspace() == null || !connection.keyspace().equals(currentKeyspace)) | |
| 427 | - { | |
| 428 | - logger.debug("Setting back keyspace post query preparation to {}", currentKeyspace); | |
| 429 | - connection.setKeyspace(currentKeyspace); | |
| 430 | - } | |
| 431 | - } | |
| 430 | + logger.info("Query {} is not prepared on {}, preparing before retrying executing. " | |
| 431 | + + "Seeing this message a few times is fine, but seeing it a lot may be source of performance problems", | |
| 432 | + toPrepare.getQueryString(), connection.address); | |
| 433 | + | |
| 434 | + releaseConnection = false; // we're reusing it for the prepare call | |
| 435 | + write(connection, prepareAndRetry(toPrepare.getQueryString())); | |
| 432 | 436 | // we're done for now, the prepareAndRetry callback will handle the rest |
| 433 | 437 | return; |
| 434 | 438 | default: |
| @@ -467,6 +471,9 @@ | ||
| 467 | 471 | } catch (Exception e) { |
| 468 | 472 | setFinalException(connection, e); |
| 469 | 473 | } finally { |
| 474 | + if (releaseConnection && connection instanceof PooledConnection) | |
| 475 | + ((PooledConnection)connection).release(); | |
| 476 | + | |
| 470 | 477 | if (queriedHost != null) |
| 471 | 478 | manager.cluster.manager.reportLatency(queriedHost, latency); |
| 472 | 479 | } |
| @@ -495,6 +502,9 @@ | ||
| 495 | 502 | return; |
| 496 | 503 | } |
| 497 | 504 | |
| 505 | + if (connection instanceof PooledConnection) | |
| 506 | + ((PooledConnection)connection).release(); | |
| 507 | + | |
| 498 | 508 | // TODO should we check the response ? |
| 499 | 509 | switch (response.type) { |
| 500 | 510 | case RESULT: |
| @@ -618,7 +628,8 @@ | ||
| 618 | 628 | // The count increments on each retry. |
| 619 | 629 | static class QueryState { |
| 620 | 630 | static final QueryState INITIAL = new QueryState(-1, false); |
| 621 | - 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); | |
| 622 | 633 | |
| 623 | 634 | final int retryCount; |
| 624 | 635 | final boolean inProgress; |
| @@ -642,9 +653,13 @@ | ||
| 642 | 653 | return new QueryState(retryCount + 1, true); |
| 643 | 654 | } |
| 644 | 655 | |
| 656 | + public boolean isCancelled() { | |
| 657 | + return this == CANCELLED_WHILE_IN_PROGRESS || this == CANCELLED_WHILE_COMPLETE; | |
| 658 | + } | |
| 659 | + | |
| 645 | 660 | @Override |
| 646 | 661 | public String toString() { |
| 647 | - return String.format("QueryState(count=%d, inProgress=%s)", retryCount, inProgress); | |
| 662 | + return String.format("QueryState(count=%d, inProgress=%s, cancelled=%s)", retryCount, inProgress, isCancelled()); | |
| 648 | 663 | } |
| 649 | 664 | } |
| 650 | 665 | } |
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +4 −1
| @@ -516,6 +516,7 @@ | ||
| 516 | 516 | // Let's not wait too long if we can't get a connection. Things |
| 517 | 517 | // will fix themselves once the user tries a query anyway. |
| 518 | 518 | PooledConnection c = null; |
| 519 | + boolean timedOut = false; | |
| 519 | 520 | try { |
| 520 | 521 | c = entry.getValue().borrowConnection(200, TimeUnit.MILLISECONDS); |
| 521 | 522 | c.write(new Requests.Prepare(query)).get(); |
| @@ -529,8 +530,10 @@ | ||
| 529 | 530 | // We shouldn't really get exception while preparing a |
| 530 | 531 | // query, so log this (but ignore otherwise as it's not a big deal) |
| 531 | 532 | logger.error(String.format("Unexpected error while preparing query (%s) on %s", query, entry.getKey()), e); |
| 533 | + // If the query timed out, that already released the connection | |
| 534 | + timedOut = e.getCause() instanceof OperationTimedOutException; | |
| 532 | 535 | } finally { |
| 533 | - if (c != null) | |
| 536 | + if (c != null && !timedOut) | |
| 534 | 537 | c.release(); |
| 535 | 538 | } |
| 536 | 539 | } |