kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | ee3bc252f5fc7b845eaad77f18e4a446b24d8806c7ed8d320df0b801768f8c6a |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-03-23 12:03 +01:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-03-23 12:03 +01:00 |
| Parents | cd885c8b27a6, 5d15971424c6 |
5 files changed
modified changelog/README.md +1 −0
| @@ -40,6 +40,7 @@ | ||
| 40 | 40 | - [bug] JAVA-1094: Backport TypeCodec parse and format fixes from 3.0. |
| 41 | 41 | - [improvement] JAVA-852: Ignore peers with null entries during discovery. |
| 42 | 42 | - [bug] JAVA-1132: Executing bound statement with no variables results in exception with protocol v1. |
| 43 | +- [bug] JAVA-1005: DowngradingConsistencyRetryPolicy does not work with EACH_QUORUM when 1 DC is down. | |
| 43 | 44 | |
| 44 | 45 | Merged from 2.0 branch: |
| 45 | 46 |
modified driver-core/src/main/java/com/datastax/driver/core/policies/DCAwareRoundRobinPolicy.java +1 −1
| @@ -381,7 +381,7 @@ | ||
| 381 | 381 | * When used in conjunction with {@link #withUsedHostsPerRemoteDc(int) usedHostsPerRemoteDc} > 0, this overrides the policy of |
| 382 | 382 | * never using remote datacenter nodes for {@code LOCAL_ONE} and {@code LOCAL_QUORUM} queries. It is however inadvisable to do |
| 383 | 383 | * so in almost all cases, as this would potentially break consistency guarantees and if you are fine with that, it's probably |
| 384 | - * better to use a weaker consitency like {@code ONE}, {@code TWO} or {@code THREE}. As such, this method should generally be | |
| 384 | + * better to use a weaker consistency like {@code ONE}, {@code TWO} or {@code THREE}. As such, this method should generally be | |
| 385 | 385 | * avoided; use it only if you know and understand what you do. |
| 386 | 386 | * |
| 387 | 387 | * @return this builder. |
modified driver-core/src/main/java/com/datastax/driver/core/policies/DowngradingConsistencyRetryPolicy.java +31 −18
| @@ -20,39 +20,47 @@ | ||
| 20 | 20 | import com.datastax.driver.core.WriteType; |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | - * A retry policy that sometimes retry with a lower consistency level than | |
| 23 | + * A retry policy that sometimes retries with a lower consistency level than | |
| 24 | 24 | * the one initially requested. |
| 25 | 25 | * <p/> |
| 26 | - * <b>BEWARE</b>: This policy may retry queries using a lower consistency | |
| 26 | + * <b>BEWARE</b>: this policy may retry queries using a lower consistency | |
| 27 | 27 | * level than the one initially requested. By doing so, it may break |
| 28 | 28 | * consistency guarantees. In other words, if you use this retry policy, |
| 29 | - * there is cases (documented below) where a read at {@code QUORUM} | |
| 29 | + * there are cases (documented below) where a read at {@code QUORUM} | |
| 30 | 30 | * <b>may not</b> see a preceding write at {@code QUORUM}. Do not use this |
| 31 | 31 | * policy unless you have understood the cases where this can happen and |
| 32 | 32 | * are ok with that. It is also highly recommended to always wrap this |
| 33 | 33 | * policy into {@link LoggingRetryPolicy} to log the occurrences of |
| 34 | - * such consistency break. | |
| 34 | + * such consistency breaks. | |
| 35 | 35 | * <p/> |
| 36 | 36 | * This policy implements the same retries than the {@link DefaultRetryPolicy} |
| 37 | 37 | * policy. But on top of that, it also retries in the following cases: |
| 38 | 38 | * <ul> |
| 39 | - * <li>On a read timeout: if the number of replica that responded is | |
| 40 | - * greater than one but lower than is required by the requested | |
| 39 | + * <li>On a read timeout: if the number of replicas that responded is | |
| 40 | + * greater than one, but lower than is required by the requested | |
| 41 | 41 | * consistency level, the operation is retried at a lower consistency |
| 42 | 42 | * level.</li> |
| 43 | - * <li>On a write timeout: if the operation is an {@code | |
| 43 | + * <li>On a write timeout: if the operation is a {@code | |
| 44 | 44 | * WriteType.UNLOGGED_BATCH} and at least one replica acknowledged the |
| 45 | 45 | * write, the operation is retried at a lower consistency level. |
| 46 | - * Furthermore, for other operation, if at least one replica acknowledged | |
| 46 | + * Furthermore, for other operations, if at least one replica acknowledged | |
| 47 | 47 | * the write, the timeout is ignored.</li> |
| 48 | 48 | * <li>On an unavailable exception: if at least one replica is alive, the |
| 49 | 49 | * operation is retried at a lower consistency level.</li> |
| 50 | 50 | * </ul> |
| 51 | + * The lower consistency level to use for retries is determined by the following rules: | |
| 52 | + * <ul> | |
| 53 | + * <li>if more than 3 replicas responded, use {@code THREE}.</li> | |
| 54 | + * <li>if 1, 2 or 3 replicas responded, use the corresponding level {@code ONE}, {@code TWO} or {@code THREE}.</li> | |
| 55 | + * </ul> | |
| 56 | + * Note that if the initial consistency level was {@code EACH_QUORUM}, Cassandra returns the number of live replicas | |
| 57 | + * <em>in the datacenter that failed to reach consistency</em>, not the overall number in the cluster. Therefore if this | |
| 58 | + * number is 0, we still retry at {@code ONE}, on the assumption that a host may still be up in another datacenter. | |
| 51 | 59 | * <p/> |
| 52 | 60 | * The reasoning being this retry policy is the following one. If, based |
| 53 | 61 | * on the information the Cassandra coordinator node returns, retrying the |
| 54 | - * operation with the initially requested consistency has a change to | |
| 55 | - * succeed, do it. Otherwise, if based on these information we know <b>the | |
| 62 | + * operation with the initially requested consistency has a chance to | |
| 63 | + * succeed, do it. Otherwise, if based on this information we know <b>the | |
| 56 | 64 | * initially requested consistency level cannot be achieve currently</b>, then: |
| 57 | 65 | * <ul> |
| 58 | 66 | * <li>For writes, ignore the exception (thus silently failing the |
| @@ -73,15 +81,20 @@ | ||
| 73 | 81 | private DowngradingConsistencyRetryPolicy() { |
| 74 | 82 | } |
| 75 | 83 | |
| 76 | - private RetryDecision maxLikelyToWorkCL(int knownOk) { | |
| 84 | + private RetryDecision maxLikelyToWorkCL(int knownOk, ConsistencyLevel currentCL) { | |
| 77 | 85 | if (knownOk >= 3) |
| 78 | 86 | return RetryDecision.retry(ConsistencyLevel.THREE); |
| 79 | - else if (knownOk == 2) | |
| 87 | + | |
| 88 | + if (knownOk == 2) | |
| 80 | 89 | return RetryDecision.retry(ConsistencyLevel.TWO); |
| 81 | - else if (knownOk == 1) | |
| 90 | + | |
| 91 | + // JAVA-1005: EACH_QUORUM does not report a global number of alive replicas | |
| 92 | + // so even if we get 0 alive replicas, there might be | |
| 93 | + // a node up in some other datacenter | |
| 94 | + if (knownOk == 1 || currentCL == ConsistencyLevel.EACH_QUORUM) | |
| 82 | 95 | return RetryDecision.retry(ConsistencyLevel.ONE); |
| 83 | - else | |
| 84 | - return RetryDecision.rethrow(); | |
| 96 | + | |
| 97 | + return RetryDecision.rethrow(); | |
| 85 | 98 | } |
| 86 | 99 | |
| 87 | 100 | /** |
| @@ -108,7 +121,7 @@ | ||
| 108 | 121 | |
| 109 | 122 | if (receivedResponses < requiredResponses) { |
| 110 | 123 | // Tries the biggest CL that is expected to work |
| 111 | - return maxLikelyToWorkCL(receivedResponses); | |
| 124 | + return maxLikelyToWorkCL(receivedResponses, cl); | |
| 112 | 125 | } |
| 113 | 126 | |
| 114 | 127 | return !dataRetrieved ? RetryDecision.retry(cl) : RetryDecision.rethrow(); |
| @@ -140,7 +153,7 @@ | ||
| 140 | 153 | case UNLOGGED_BATCH: |
| 141 | 154 | // Since only part of the batch could have been persisted, |
| 142 | 155 | // retry with whatever consistency should allow to persist all |
| 143 | - return maxLikelyToWorkCL(receivedAcks); | |
| 156 | + return maxLikelyToWorkCL(receivedAcks, cl); | |
| 144 | 157 | case BATCH_LOG: |
| 145 | 158 | return RetryDecision.retry(cl); |
| 146 | 159 | } |
| @@ -166,7 +179,7 @@ | ||
| 166 | 179 | return RetryDecision.tryNextHost(null); |
| 167 | 180 | |
| 168 | 181 | // Tries the biggest CL that is expected to work |
| 169 | - return maxLikelyToWorkCL(aliveReplica); | |
| 182 | + return maxLikelyToWorkCL(aliveReplica, cl); | |
| 170 | 183 | } |
| 171 | 184 | |
| 172 | 185 | /** |
modified driver-core/src/test/java/com/datastax/driver/core/policies/AbstractRetryPolicyIntegrationTest.java +5 −0
| @@ -135,6 +135,11 @@ | ||
| 135 | 135 | return query(session); |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | + protected ResultSet queryWithCL(ConsistencyLevel cl) { | |
| 139 | + Statement statement = new SimpleStatement("mock query").setConsistencyLevel(cl); | |
| 140 | + return session.execute(statement); | |
| 141 | + } | |
| 142 | + | |
| 138 | 143 | protected ResultSet query(Session session) { |
| 139 | 144 | return session.execute("mock query"); |
| 140 | 145 | } |
modified driver-core/src/test/java/com/datastax/driver/core/policies/DowngradingConsistencyRetryPolicyIntegrationTest.java +26 −0
| @@ -81,6 +81,32 @@ | ||
| 81 | 81 | }; |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | + /** | |
| 85 | + * Ensures that when handling a read timeout with {@link DowngradingConsistencyRetryPolicy} that a retry is | |
| 86 | + * reattempted with {@link ConsistencyLevel#ONE} if the consistency level on the statement executed is | |
| 87 | + * {@link ConsistencyLevel#EACH_QUORUM}, even if the number of known alive replicas was 0. | |
| 88 | + * | |
| 89 | + * @jira_ticket JAVA-1005 | |
| 90 | + * @test_category retry_policy | |
| 91 | + */ | |
| 92 | + @Test(groups = "short") | |
| 93 | + public void should_retry_once_on_same_host_from_each_quorum_to_one() { | |
| 94 | + simulateError(1, read_request_timeout, new ReadTimeoutConfig(0, 3, false)); | |
| 95 | + | |
| 96 | + try { | |
| 97 | + queryWithCL(ConsistencyLevel.EACH_QUORUM); | |
| 98 | + } catch (ReadTimeoutException e) { | |
| 99 | + assertThat(e.getConsistencyLevel()).isEqualTo(ConsistencyLevel.ONE); | |
| 100 | + } | |
| 101 | + | |
| 102 | + assertOnReadTimeoutWasCalled(2); | |
| 103 | + assertThat(errors.getRetries().getCount()).isEqualTo(1); | |
| 104 | + assertThat(errors.getReadTimeouts().getCount()).isEqualTo(2); | |
| 105 | + assertThat(errors.getRetriesOnReadTimeout().getCount()).isEqualTo(1); | |
| 106 | + assertQueried(1, 2); | |
| 107 | + assertQueried(2, 0); | |
| 108 | + assertQueried(3, 0); | |
| 109 | + } | |
| 84 | 110 | |
| 85 | 111 | /** |
| 86 | 112 | * Ensures that when handling a read timeout with {@link DowngradingConsistencyRetryPolicy} that a retry is |