Elffuss BraidHub

kikocis/java-driver public

Merge pull request #544 from datastax/java1005

JAVA-1005: DowngradingConsistencyRetryPolicy does not work with EACH_QUORUM when 1 DC is down.
Changesetee3bc252f5fc7b845eaad77f18e4a446b24d8806c7ed8d320df0b801768f8c6a
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2016-03-23 12:03 +01:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-03-23 12:03 +01:00
Parentscd885c8b27a6, 5d15971424c6

Browse the files at this changeset

5 files changed

modified changelog/README.md +1 −0
@@ -40,6 +40,7 @@
4040 - [bug] JAVA-1094: Backport TypeCodec parse and format fixes from 3.0.
4141 - [improvement] JAVA-852: Ignore peers with null entries during discovery.
4242 - [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.
4344
4445 Merged from 2.0 branch:
4546
modified driver-core/src/main/java/com/datastax/driver/core/policies/DCAwareRoundRobinPolicy.java +1 −1
@@ -381,7 +381,7 @@
381381 * When used in conjunction with {@link #withUsedHostsPerRemoteDc(int) usedHostsPerRemoteDc} > 0, this overrides the policy of
382382 * never using remote datacenter nodes for {@code LOCAL_ONE} and {@code LOCAL_QUORUM} queries. It is however inadvisable to do
383383 * 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
385385 * avoided; use it only if you know and understand what you do.
386386 *
387387 * @return this builder.
modified driver-core/src/main/java/com/datastax/driver/core/policies/DowngradingConsistencyRetryPolicy.java +31 −18
@@ -20,39 +20,47 @@
2020 import com.datastax.driver.core.WriteType;
2121
2222 /**
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
2424 * the one initially requested.
2525 * <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
2727 * level than the one initially requested. By doing so, it may break
2828 * 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}
3030 * <b>may not</b> see a preceding write at {@code QUORUM}. Do not use this
3131 * policy unless you have understood the cases where this can happen and
3232 * are ok with that. It is also highly recommended to always wrap this
3333 * policy into {@link LoggingRetryPolicy} to log the occurrences of
34- * such consistency break.
34+ * such consistency breaks.
3535 * <p/>
3636 * This policy implements the same retries than the {@link DefaultRetryPolicy}
3737 * policy. But on top of that, it also retries in the following cases:
3838 * <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
4141 * consistency level, the operation is retried at a lower consistency
4242 * 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
4444 * WriteType.UNLOGGED_BATCH} and at least one replica acknowledged the
4545 * 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
4747 * the write, the timeout is ignored.</li>
4848 * <li>On an unavailable exception: if at least one replica is alive, the
4949 * operation is retried at a lower consistency level.</li>
5050 * </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.
5159 * <p/>
5260 * The reasoning being this retry policy is the following one. If, based
5361 * 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
5664 * initially requested consistency level cannot be achieve currently</b>, then:
5765 * <ul>
5866 * <li>For writes, ignore the exception (thus silently failing the
@@ -73,15 +81,20 @@
7381 private DowngradingConsistencyRetryPolicy() {
7482 }
7583
76- private RetryDecision maxLikelyToWorkCL(int knownOk) {
84+ private RetryDecision maxLikelyToWorkCL(int knownOk, ConsistencyLevel currentCL) {
7785 if (knownOk >= 3)
7886 return RetryDecision.retry(ConsistencyLevel.THREE);
79- else if (knownOk == 2)
87+
88+ if (knownOk == 2)
8089 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)
8295 return RetryDecision.retry(ConsistencyLevel.ONE);
83- else
84- return RetryDecision.rethrow();
96+
97+ return RetryDecision.rethrow();
8598 }
8699
87100 /**
@@ -108,7 +121,7 @@
108121
109122 if (receivedResponses < requiredResponses) {
110123 // Tries the biggest CL that is expected to work
111- return maxLikelyToWorkCL(receivedResponses);
124+ return maxLikelyToWorkCL(receivedResponses, cl);
112125 }
113126
114127 return !dataRetrieved ? RetryDecision.retry(cl) : RetryDecision.rethrow();
@@ -140,7 +153,7 @@
140153 case UNLOGGED_BATCH:
141154 // Since only part of the batch could have been persisted,
142155 // retry with whatever consistency should allow to persist all
143- return maxLikelyToWorkCL(receivedAcks);
156+ return maxLikelyToWorkCL(receivedAcks, cl);
144157 case BATCH_LOG:
145158 return RetryDecision.retry(cl);
146159 }
@@ -166,7 +179,7 @@
166179 return RetryDecision.tryNextHost(null);
167180
168181 // Tries the biggest CL that is expected to work
169- return maxLikelyToWorkCL(aliveReplica);
182+ return maxLikelyToWorkCL(aliveReplica, cl);
170183 }
171184
172185 /**
modified driver-core/src/test/java/com/datastax/driver/core/policies/AbstractRetryPolicyIntegrationTest.java +5 −0
@@ -135,6 +135,11 @@
135135 return query(session);
136136 }
137137
138+ protected ResultSet queryWithCL(ConsistencyLevel cl) {
139+ Statement statement = new SimpleStatement("mock query").setConsistencyLevel(cl);
140+ return session.execute(statement);
141+ }
142+
138143 protected ResultSet query(Session session) {
139144 return session.execute("mock query");
140145 }
modified driver-core/src/test/java/com/datastax/driver/core/policies/DowngradingConsistencyRetryPolicyIntegrationTest.java +26 −0
@@ -81,6 +81,32 @@
8181 };
8282 }
8383
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+ }
84110
85111 /**
86112 * Ensures that when handling a read timeout with {@link DowngradingConsistencyRetryPolicy} that a retry is