kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | e968aa87642cff786e7409f8f90657538667fde9e1ae0104ff95f1c39a0f5e31 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-09-27 20:58 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-09-29 15:57 +02:00 |
| Parents | 7704c9cd2ab6 |
5 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | - [bug] Handle null pool in PooledConnection.release (JAVA-449) |
| 8 | 8 | - [improvement] Defunct connection on request timeout (JAVA-425) |
| 9 | 9 | - [improvement] Try next host when we get a SERVER_ERROR (JAVA-426) |
| 10 | +- [bug] Handle race between query timeout and completion (JAVA-449, JAVA-460, JAVA-471) | |
| 10 | 11 | |
| 11 | 12 | |
| 12 | 13 | 2.0.6: |
modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedResultSet.java +9 −3
| @@ -351,21 +351,27 @@ | ||
| 351 | 351 | |
| 352 | 352 | // This is only called for internal calls, so don't bother with ExecutionInfo |
| 353 | 353 | @Override |
| 354 | - public void onSet(Connection connection, Message.Response response, long latency) { | |
| 354 | + public void onSet(Connection connection, Message.Response response, long latency, int retryCount) { | |
| 355 | 355 | onSet(connection, response, null, null, latency); |
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | @Override |
| 359 | - public void onException(Connection connection, Exception exception, long latency) { | |
| 359 | + public void onException(Connection connection, Exception exception, long latency, int retryCount) { | |
| 360 | 360 | future.setException(exception); |
| 361 | 361 | } |
| 362 | 362 | |
| 363 | 363 | @Override |
| 364 | - public void onTimeout(Connection connection, long latency) { | |
| 364 | + public boolean onTimeout(Connection connection, long latency, int retryCount) { | |
| 365 | 365 | // This won't be called directly since this will be wrapped by RequestHandler. |
| 366 | 366 | throw new UnsupportedOperationException(); |
| 367 | 367 | } |
| 368 | 368 | |
| 369 | + @Override | |
| 370 | + public int retryCount() { | |
| 371 | + // This is only called for internal calls (i.e, when the callback is not wrapped in RequestHandler). | |
| 372 | + // There is no retry logic in that case, so the value does not really matter. | |
| 373 | + return 0; | |
| 374 | + } | |
| 369 | 375 | }, statement); |
| 370 | 376 | |
| 371 | 377 | return future; |
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.*; |
| @@ -382,7 +381,7 @@ | ||
| 382 | 381 | } else { |
| 383 | 382 | ce = new TransportException(address, "Error writing", writeFuture.getCause()); |
| 384 | 383 | } |
| 385 | - handler.callback.onException(Connection.this, defunct(ce), System.nanoTime() - handler.startTime); | |
| 384 | + handler.callback.onException(Connection.this, defunct(ce), System.nanoTime() - handler.startTime, handler.retryCount); | |
| 386 | 385 | } else { |
| 387 | 386 | logger.trace("{} request sent successfully", Connection.this); |
| 388 | 387 | } |
| @@ -642,7 +641,7 @@ | ||
| 642 | 641 | return; |
| 643 | 642 | } |
| 644 | 643 | handler.cancelTimeout(); |
| 645 | - handler.callback.onSet(Connection.this, response, System.nanoTime() - handler.startTime); | |
| 644 | + handler.callback.onSet(Connection.this, response, System.nanoTime() - handler.startTime, handler.retryCount); | |
| 646 | 645 | |
| 647 | 646 | // If we happen to be closed and we're the last outstanding request, we need to terminate the connection |
| 648 | 647 | // (note: this is racy as the signaling can be called more than once, but that's not a problem) |
| @@ -681,7 +680,7 @@ | ||
| 681 | 680 | { |
| 682 | 681 | ResponseHandler handler = iter.next(); |
| 683 | 682 | handler.cancelTimeout(); |
| 684 | - handler.callback.onException(Connection.this, ce, System.nanoTime() - handler.startTime); | |
| 683 | + handler.callback.onException(Connection.this, ce, System.nanoTime() - handler.startTime, handler.retryCount); | |
| 685 | 684 | iter.remove(); |
| 686 | 685 | } |
| 687 | 686 | } |
| @@ -748,19 +747,25 @@ | ||
| 748 | 747 | return request; |
| 749 | 748 | } |
| 750 | 749 | |
| 750 | + @Override | |
| 751 | + public int retryCount() { | |
| 752 | + // This is ignored, as there is no retry logic in this class | |
| 753 | + return 0; | |
| 754 | + } | |
| 755 | + | |
| 751 | 756 | @Override |
| 752 | 757 | public void onSet(Connection connection, Message.Response response, ExecutionInfo info, Statement statement, long latency) { |
| 753 | - onSet(connection, response, latency); | |
| 758 | + onSet(connection, response, latency, 0); | |
| 754 | 759 | } |
| 755 | 760 | |
| 756 | 761 | @Override |
| 757 | - public void onSet(Connection connection, Message.Response response, long latency) { | |
| 762 | + public void onSet(Connection connection, Message.Response response, long latency, int retryCount) { | |
| 758 | 763 | this.address = connection.address; |
| 759 | 764 | super.set(response); |
| 760 | 765 | } |
| 761 | 766 | |
| 762 | 767 | @Override |
| 763 | - public void onException(Connection connection, Exception exception, long latency) { | |
| 768 | + public void onException(Connection connection, Exception exception, long latency, int retryCount) { | |
| 764 | 769 | // If all nodes are down, we will get a null connection here. This is fine, if we have |
| 765 | 770 | // an exception, consumers shouldn't assume the address is not null. |
| 766 | 771 | if (connection != null) |
| @@ -769,10 +774,11 @@ | ||
| 769 | 774 | } |
| 770 | 775 | |
| 771 | 776 | @Override |
| 772 | - public void onTimeout(Connection connection, long latency) { | |
| 777 | + public boolean onTimeout(Connection connection, long latency, int retryCount) { | |
| 773 | 778 | assert connection != null; // We always timeout on a specific connection, so this shouldn't be null |
| 774 | 779 | this.address = connection.address; |
| 775 | 780 | super.setException(new ConnectionException(connection.address, "Operation timed out")); |
| 781 | + return true; | |
| 776 | 782 | } |
| 777 | 783 | |
| 778 | 784 | public InetSocketAddress getAddress() { |
| @@ -782,9 +788,10 @@ | ||
| 782 | 788 | |
| 783 | 789 | interface ResponseCallback { |
| 784 | 790 | public Message.Request request(); |
| 785 | - public void onSet(Connection connection, Message.Response response, long latency); | |
| 786 | - public void onException(Connection connection, Exception exception, long latency); | |
| 787 | - public void onTimeout(Connection connection, long latency); | |
| 791 | + public int retryCount(); | |
| 792 | + public void onSet(Connection connection, Message.Response response, long latency, int retryCount); | |
| 793 | + public void onException(Connection connection, Exception exception, long latency, int retryCount); | |
| 794 | + public boolean onTimeout(Connection connection, long latency, int retryCount); | |
| 788 | 795 | } |
| 789 | 796 | |
| 790 | 797 | static class ResponseHandler { |
| @@ -792,6 +799,7 @@ | ||
| 792 | 799 | public final Connection connection; |
| 793 | 800 | public final int streamId; |
| 794 | 801 | public final ResponseCallback callback; |
| 802 | + public final int retryCount; | |
| 795 | 803 | |
| 796 | 804 | private final Timeout timeout; |
| 797 | 805 | private final long startTime; |
| @@ -800,6 +808,7 @@ | ||
| 800 | 808 | this.connection = connection; |
| 801 | 809 | this.streamId = connection.dispatcher.streamIdHandler.next(); |
| 802 | 810 | this.callback = callback; |
| 811 | + this.retryCount = callback.retryCount(); | |
| 803 | 812 | |
| 804 | 813 | long timeoutMs = connection.factory.getReadTimeoutMillis(); |
| 805 | 814 | this.timeout = timeoutMs <= 0 ? null : connection.factory.timer.newTimeout(onTimeoutTask(), timeoutMs, TimeUnit.MILLISECONDS); |
| @@ -826,7 +835,8 @@ | ||
| 826 | 835 | return new TimerTask() { |
| 827 | 836 | @Override |
| 828 | 837 | public void run(Timeout timeout) { |
| 829 | - callback.onTimeout(connection, System.nanoTime() - startTime); | |
| 838 | + if (callback.onTimeout(connection, System.nanoTime() - startTime, retryCount)) | |
| 839 | + cancelHandler(); | |
| 830 | 840 | } |
| 831 | 841 | }; |
| 832 | 842 | } |
modified driver-core/src/main/java/com/datastax/driver/core/DefaultResultSetFuture.java +12 −4
| @@ -120,22 +120,23 @@ | ||
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | @Override |
| 123 | - public void onSet(Connection connection, Message.Response response, long latency) { | |
| 123 | + public void onSet(Connection connection, Message.Response response, long latency, int retryCount) { | |
| 124 | 124 | // This is only called for internal calls (i.e, when the callback is not wrapped in ResponseHandler), |
| 125 | 125 | // so don't bother with ExecutionInfo. |
| 126 | 126 | onSet(connection, response, null, null, latency); |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | @Override |
| 130 | - public void onException(Connection connection, Exception exception, long latency) { | |
| 130 | + public void onException(Connection connection, Exception exception, long latency, int retryCount) { | |
| 131 | 131 | setException(exception); |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | @Override |
| 135 | - public void onTimeout(Connection connection, long latency) { | |
| 136 | - // This is only called for internal calls (i.e, when the callback is not wrapped in ResponseHandler). | |
| 135 | + public boolean onTimeout(Connection connection, long latency, int retryCount) { | |
| 136 | + // This is only called for internal calls (i.e, when the future is not wrapped in RequestHandler). | |
| 137 | 137 | // So just set an exception for the final result, which should be handled correctly by said internal call. |
| 138 | 138 | setException(new ConnectionException(connection.address, "Operation timed out")); |
| 139 | + return true; | |
| 139 | 140 | } |
| 140 | 141 | |
| 141 | 142 | // We sometimes need (in the driver) to set the future from outside this class, |
| @@ -257,4 +258,11 @@ | ||
| 257 | 258 | else |
| 258 | 259 | throw new DriverInternalError("Unexpected exception thrown", e.getCause()); |
| 259 | 260 | } |
| 261 | + | |
| 262 | + @Override | |
| 263 | + public int retryCount() { | |
| 264 | + // This is only called for internal calls (i.e, when the future is not wrapped in RequestHandler). | |
| 265 | + // There is no retry logic in that case, so the value does not really matter. | |
| 266 | + return 0; | |
| 267 | + } | |
| 260 | 268 | } |
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +103 −20
| @@ -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; |
| @@ -26,6 +25,7 @@ | ||
| 26 | 25 | import java.util.Map; |
| 27 | 26 | import java.util.concurrent.TimeUnit; |
| 28 | 27 | import java.util.concurrent.TimeoutException; |
| 28 | +import java.util.concurrent.atomic.AtomicReference; | |
| 29 | 29 | |
| 30 | 30 | import com.codahale.metrics.Timer; |
| 31 | 31 | import org.slf4j.Logger; |
| @@ -38,7 +38,6 @@ | ||
| 38 | 38 | import com.datastax.driver.core.exceptions.UnavailableException; |
| 39 | 39 | import com.datastax.driver.core.exceptions.WriteTimeoutException; |
| 40 | 40 | import com.datastax.driver.core.policies.*; |
| 41 | -import com.datastax.driver.core.policies.RetryPolicy.RetryDecision; | |
| 42 | 41 | import com.datastax.driver.core.policies.RetryPolicy.RetryDecision.Type; |
| 43 | 42 | |
| 44 | 43 | /** |
| @@ -46,7 +45,6 @@ | ||
| 46 | 45 | * unavailable/timeout. |
| 47 | 46 | */ |
| 48 | 47 | class RequestHandler implements Connection.ResponseCallback { |
| 49 | - | |
| 50 | 48 | private static final Logger logger = LoggerFactory.getLogger(RequestHandler.class); |
| 51 | 49 | |
| 52 | 50 | private final SessionManager manager; |
| @@ -57,10 +55,8 @@ | ||
| 57 | 55 | private volatile Host current; |
| 58 | 56 | private volatile List<Host> triedHosts; |
| 59 | 57 | private volatile HostConnectionPool currentPool; |
| 58 | + private final AtomicReference<QueryState> queryStateRef; | |
| 60 | 59 | |
| 61 | - // Note that we know that queryRetries can only be incremented by one writer at a time so a | |
| 62 | - // volatile is good enough. | |
| 63 | - private volatile int queryRetries; | |
| 64 | 60 | private volatile ConsistencyLevel retryConsistencyLevel; |
| 65 | 61 | |
| 66 | 62 | private volatile Map<InetSocketAddress, Throwable> errors; |
| @@ -79,6 +75,7 @@ | ||
| 79 | 75 | |
| 80 | 76 | this.queryPlan = manager.loadBalancingPolicy().newQueryPlan(manager.poolsState.keyspace, statement); |
| 81 | 77 | this.statement = statement; |
| 78 | + this.queryStateRef = new AtomicReference<QueryState>(QueryState.INITIAL); | |
| 82 | 79 | |
| 83 | 80 | this.timerContext = metricsEnabled() |
| 84 | 81 | ? metrics().getRequestsTimer().time() |
| @@ -162,6 +159,8 @@ | ||
| 162 | 159 | } |
| 163 | 160 | |
| 164 | 161 | private void retry(final boolean retryCurrent, ConsistencyLevel newConsistencyLevel) { |
| 162 | + queryStateRef.set(queryStateRef.get().startNext()); | |
| 163 | + | |
| 165 | 164 | final Host h = current; |
| 166 | 165 | this.retryConsistencyLevel = newConsistencyLevel; |
| 167 | 166 | |
| @@ -236,7 +235,7 @@ | ||
| 236 | 235 | info = info.withAchievedConsistency(retryConsistencyLevel); |
| 237 | 236 | callback.onSet(connection, response, info, statement, System.nanoTime() - startTime); |
| 238 | 237 | } catch (Exception e) { |
| 239 | - callback.onException(connection, new DriverInternalError("Unexpected exception while setting final result from " + response, e), System.nanoTime() - startTime); | |
| 238 | + callback.onException(connection, new DriverInternalError("Unexpected exception while setting final result from " + response, e), System.nanoTime() - startTime, retryCount()); | |
| 240 | 239 | } |
| 241 | 240 | } |
| 242 | 241 | |
| @@ -245,12 +244,20 @@ | ||
| 245 | 244 | if (timerContext != null) |
| 246 | 245 | timerContext.stop(); |
| 247 | 246 | } finally { |
| 248 | - callback.onException(connection, exception, System.nanoTime() - startTime); | |
| 247 | + callback.onException(connection, exception, System.nanoTime() - startTime, retryCount()); | |
| 249 | 248 | } |
| 250 | 249 | } |
| 251 | 250 | |
| 252 | 251 | @Override |
| 253 | - public void onSet(Connection connection, Message.Response response, long latency) { | |
| 252 | + public void onSet(Connection connection, Message.Response response, long latency, int retryCount) { | |
| 253 | + QueryState queryState = queryStateRef.get(); | |
| 254 | + if (!queryState.isInProgressAt(retryCount) || | |
| 255 | + !queryStateRef.compareAndSet(queryState, queryState.complete())) { | |
| 256 | + logger.debug("onSet triggered but the response was completed by another thread, cancelling (retryCount = {}, queryState = {}, queryStateRef = {})", | |
| 257 | + retryCount, queryState, queryStateRef.get()); | |
| 258 | + return; | |
| 259 | + } | |
| 260 | + | |
| 254 | 261 | Host queriedHost = current; |
| 255 | 262 | try { |
| 256 | 263 | if (connection instanceof PooledConnection) |
| @@ -278,7 +285,7 @@ | ||
| 278 | 285 | rte.getRequiredAcknowledgements(), |
| 279 | 286 | rte.getReceivedAcknowledgements(), |
| 280 | 287 | rte.wasDataRetrieved(), |
| 281 | - queryRetries); | |
| 288 | + retryCount); | |
| 282 | 289 | |
| 283 | 290 | if (metricsEnabled()) { |
| 284 | 291 | if (retry.getType() == Type.RETRY) |
| @@ -298,7 +305,7 @@ | ||
| 298 | 305 | wte.getWriteType(), |
| 299 | 306 | wte.getRequiredAcknowledgements(), |
| 300 | 307 | wte.getReceivedAcknowledgements(), |
| 301 | - queryRetries); | |
| 308 | + retryCount); | |
| 302 | 309 | |
| 303 | 310 | if (metricsEnabled()) { |
| 304 | 311 | if (retry.getType() == Type.RETRY) |
| @@ -317,7 +324,7 @@ | ||
| 317 | 324 | ue.getConsistencyLevel(), |
| 318 | 325 | ue.getRequiredReplicas(), |
| 319 | 326 | ue.getAliveReplicas(), |
| 320 | - queryRetries); | |
| 327 | + retryCount); | |
| 321 | 328 | |
| 322 | 329 | if (metricsEnabled()) { |
| 323 | 330 | if (retry.getType() == Type.RETRY) |
| @@ -378,6 +385,7 @@ | ||
| 378 | 385 | connection.setKeyspace(prepareKeyspace); |
| 379 | 386 | } |
| 380 | 387 | |
| 388 | + queryStateRef.set(queryStateRef.get().startNext()); | |
| 381 | 389 | try { |
| 382 | 390 | connection.write(prepareAndRetry(toPrepare.getQueryString())); |
| 383 | 391 | } finally { |
| @@ -401,9 +409,8 @@ | ||
| 401 | 409 | else { |
| 402 | 410 | switch (retry.getType()) { |
| 403 | 411 | case RETRY: |
| 404 | - ++queryRetries; | |
| 405 | 412 | if (logger.isDebugEnabled()) |
| 406 | - logger.debug("Doing retry {} for query {} at consistency {}", queryRetries, statement, retry.getRetryConsistencyLevel()); | |
| 413 | + logger.debug("Doing retry {} for query {} at consistency {}", retryCount, statement, retry.getRetryConsistencyLevel()); | |
| 407 | 414 | if (metricsEnabled()) |
| 408 | 415 | metrics().getErrorMetrics().getRetries().inc(); |
| 409 | 416 | retry(true, retry.getRetryConsistencyLevel()); |
| @@ -440,7 +447,20 @@ | ||
| 440 | 447 | } |
| 441 | 448 | |
| 442 | 449 | @Override |
| 443 | - public void onSet(Connection connection, Message.Response response, long latency) { | |
| 450 | + public int retryCount() { | |
| 451 | + return RequestHandler.this.retryCount(); | |
| 452 | + } | |
| 453 | + | |
| 454 | + @Override | |
| 455 | + public void onSet(Connection connection, Message.Response response, long latency, int retryCount) { | |
| 456 | + QueryState queryState = queryStateRef.get(); | |
| 457 | + if (!queryState.isInProgressAt(retryCount) || | |
| 458 | + !queryStateRef.compareAndSet(queryState, queryState.complete())) { | |
| 459 | + logger.debug("onSet triggered but the response was completed by another thread, cancelling (retryCount = {}, queryState = {}, queryStateRef = {})", | |
| 460 | + retryCount, queryState, queryStateRef.get()); | |
| 461 | + return; | |
| 462 | + } | |
| 463 | + | |
| 444 | 464 | // TODO should we check the response ? |
| 445 | 465 | switch (response.type) { |
| 446 | 466 | case RESULT: |
| @@ -466,20 +486,35 @@ | ||
| 466 | 486 | } |
| 467 | 487 | |
| 468 | 488 | @Override |
| 469 | - public void onException(Connection connection, Exception exception, long latency) { | |
| 470 | - RequestHandler.this.onException(connection, exception, latency); | |
| 489 | + public void onException(Connection connection, Exception exception, long latency, int retryCount) { | |
| 490 | + RequestHandler.this.onException(connection, exception, latency, retryCount); | |
| 471 | 491 | } |
| 472 | 492 | |
| 473 | 493 | @Override |
| 474 | - public void onTimeout(Connection connection, long latency) { | |
| 494 | + public boolean onTimeout(Connection connection, long latency, int retryCount) { | |
| 495 | + QueryState queryState = queryStateRef.get(); | |
| 496 | + if (!queryState.isInProgressAt(retryCount) || | |
| 497 | + !queryStateRef.compareAndSet(queryState, queryState.complete())) { | |
| 498 | + logger.debug("onTimeout triggered but the response was completed by another thread, cancelling (retryCount = {}, queryState = {}, queryStateRef = {})", | |
| 499 | + retryCount, queryState, queryStateRef.get()); | |
| 500 | + return false; | |
| 501 | + } | |
| 475 | 502 | logError(connection.address, new DriverException("Timeout waiting for response to prepare message")); |
| 476 | 503 | retry(false, null); |
| 504 | + return true; | |
| 477 | 505 | } |
| 478 | 506 | }; |
| 479 | 507 | } |
| 480 | 508 | |
| 481 | 509 | @Override |
| 482 | - public void onException(Connection connection, Exception exception, long latency) { | |
| 510 | + public void onException(Connection connection, Exception exception, long latency, int retryCount) { | |
| 511 | + QueryState queryState = queryStateRef.get(); | |
| 512 | + if (!queryState.isInProgressAt(retryCount) || | |
| 513 | + !queryStateRef.compareAndSet(queryState, queryState.complete())) { | |
| 514 | + logger.debug("onException triggered but the response was completed by another thread, cancelling (retryCount = {}, queryState = {}, queryStateRef = {})", | |
| 515 | + retryCount, queryState, queryStateRef.get()); | |
| 516 | + return; | |
| 517 | + } | |
| 483 | 518 | |
| 484 | 519 | Host queriedHost = current; |
| 485 | 520 | try { |
| @@ -505,7 +540,15 @@ | ||
| 505 | 540 | } |
| 506 | 541 | |
| 507 | 542 | @Override |
| 508 | - public void onTimeout(Connection connection, long latency) { | |
| 543 | + public boolean onTimeout(Connection connection, long latency, int retryCount) { | |
| 544 | + QueryState queryState = queryStateRef.get(); | |
| 545 | + if (!queryState.isInProgressAt(retryCount) || | |
| 546 | + !queryStateRef.compareAndSet(queryState, queryState.complete())) { | |
| 547 | + logger.debug("onTimeout triggered but the response was completed by another thread, cancelling (retryCount = {}, queryState = {}, queryStateRef = {})", | |
| 548 | + retryCount, queryState, queryStateRef.get()); | |
| 549 | + return false; | |
| 550 | + } | |
| 551 | + | |
| 509 | 552 | Host queriedHost = current; |
| 510 | 553 | try { |
| 511 | 554 | // If a query times out, we consider that the host is unstable, so we defunct |
| @@ -522,10 +565,50 @@ | ||
| 522 | 565 | if (queriedHost != null) |
| 523 | 566 | manager.cluster.manager.reportLatency(queriedHost, latency); |
| 524 | 567 | } |
| 568 | + return true; | |
| 569 | + } | |
| 570 | + | |
| 571 | + @Override | |
| 572 | + public int retryCount() { | |
| 573 | + return queryStateRef.get().retryCount; | |
| 525 | 574 | } |
| 526 | 575 | |
| 527 | 576 | interface Callback extends Connection.ResponseCallback { |
| 528 | 577 | public void onSet(Connection connection, Message.Response response, ExecutionInfo info, Statement statement, long latency); |
| 529 | 578 | public void register(RequestHandler handler); |
| 530 | 579 | } |
| 580 | + | |
| 581 | + // This is used to prevent races between request completion (either success or error) and timeout. | |
| 582 | + // A retry is in progress once we have written the request to the connection and until we get back a response or a timeout. | |
| 583 | + // The count increments on each retry. | |
| 584 | + static class QueryState { | |
| 585 | + static QueryState INITIAL = new QueryState(0, true); | |
| 586 | + | |
| 587 | + final int retryCount; | |
| 588 | + final boolean inProgress; | |
| 589 | + | |
| 590 | + private QueryState(int count, boolean inProgress) { | |
| 591 | + this.retryCount = count; | |
| 592 | + this.inProgress = inProgress; | |
| 593 | + } | |
| 594 | + | |
| 595 | + boolean isInProgressAt(int retryCount) { | |
| 596 | + return inProgress && this.retryCount == retryCount; | |
| 597 | + } | |
| 598 | + | |
| 599 | + QueryState complete() { | |
| 600 | + assert inProgress; | |
| 601 | + return new QueryState(retryCount, false); | |
| 602 | + } | |
| 603 | + | |
| 604 | + QueryState startNext() { | |
| 605 | + assert !inProgress; | |
| 606 | + return new QueryState(retryCount + 1, true); | |
| 607 | + } | |
| 608 | + | |
| 609 | + @Override | |
| 610 | + public String toString() { | |
| 611 | + return String.format("QueryState(count=%d, inProgress=%s)", retryCount, inProgress); | |
| 612 | + } | |
| 613 | + } | |
| 531 | 614 | } |