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