Elffuss BraidHub

kikocis/java-driver public

JAVA-1005: DowngradingConsistencyRetryPolicy does not work with EACH_QUORUM when 1 DC is down.

(cherry picked from 2.1)
Changeset872afebb570c390513d99c04e4d825758286748877bcc698cc1a037d9c369f87
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2016-03-23 12:03 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-04-01 17:57 +02:00
Parents323011652e7f

Browse the files at this changeset

5 files changed

modified changelog/README.md +1 −0
@@ -19,6 +19,7 @@
1919 - [new feature] JAVA-982: Introduce new method ConsistencyLevel.isSerial().
2020 - [bug] JAVA-764: Retry with the normal consistency level (not the serial one) when a write times out on the Paxos phase.
2121 - [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.
2223
2324
2425 ### 3.0.0
modified driver-core/src/main/java/com/datastax/driver/core/policies/DCAwareRoundRobinPolicy.java +1 −1
@@ -342,7 +342,7 @@
342342 * When used in conjunction with {@link #withUsedHostsPerRemoteDc(int) usedHostsPerRemoteDc} > 0, this overrides the policy of
343343 * never using remote datacenter nodes for {@code LOCAL_ONE} and {@code LOCAL_QUORUM} queries. It is however inadvisable to do
344344 * 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
346346 * avoided; use it only if you know and understand what you do.
347347 *
348348 * @return this builder.
modified driver-core/src/main/java/com/datastax/driver/core/policies/DowngradingConsistencyRetryPolicy.java +31 −18
@@ -22,39 +22,47 @@
2222 import com.datastax.driver.core.exceptions.DriverException;
2323
2424 /**
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
2626 * the one initially requested.
2727 * <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
2929 * level than the one initially requested. By doing so, it may break
3030 * 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}
3232 * <b>may not</b> see a preceding write at {@code QUORUM}. Do not use this
3333 * policy unless you have understood the cases where this can happen and
3434 * are ok with that. It is also highly recommended to always wrap this
3535 * policy into {@link LoggingRetryPolicy} to log the occurrences of
36- * such consistency break.
36+ * such consistency breaks.
3737 * <p/>
3838 * This policy implements the same retries than the {@link DefaultRetryPolicy}
3939 * policy. But on top of that, it also retries in the following cases:
4040 * <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
4343 * consistency level, the operation is retried at a lower consistency
4444 * 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
4646 * WriteType.UNLOGGED_BATCH} and at least one replica acknowledged the
4747 * 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
4949 * the write, the timeout is ignored.</li>
5050 * <li>On an unavailable exception: if at least one replica is alive, the
5151 * operation is retried at a lower consistency level.</li>
5252 * </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.
5361 * <p/>
5462 * The reasoning being this retry policy is the following one. If, based
5563 * 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
5866 * initially requested consistency level cannot be achieve currently</b>, then:
5967 * <ul>
6068 * <li>For writes, ignore the exception (thus silently failing the
@@ -75,15 +83,20 @@
7583 private DowngradingConsistencyRetryPolicy() {
7684 }
7785
78- private RetryDecision maxLikelyToWorkCL(int knownOk) {
86+ private RetryDecision maxLikelyToWorkCL(int knownOk, ConsistencyLevel currentCL) {
7987 if (knownOk >= 3)
8088 return RetryDecision.retry(ConsistencyLevel.THREE);
81- else if (knownOk == 2)
89+
90+ if (knownOk == 2)
8291 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)
8497 return RetryDecision.retry(ConsistencyLevel.ONE);
85- else
86- return RetryDecision.rethrow();
98+
99+ return RetryDecision.rethrow();
87100 }
88101
89102 /**
@@ -110,7 +123,7 @@
110123
111124 if (receivedResponses < requiredResponses) {
112125 // Tries the biggest CL that is expected to work
113- return maxLikelyToWorkCL(receivedResponses);
126+ return maxLikelyToWorkCL(receivedResponses, cl);
114127 }
115128
116129 return !dataRetrieved ? RetryDecision.retry(cl) : RetryDecision.rethrow();
@@ -142,7 +155,7 @@
142155 case UNLOGGED_BATCH:
143156 // Since only part of the batch could have been persisted,
144157 // retry with whatever consistency should allow to persist all
145- return maxLikelyToWorkCL(receivedAcks);
158+ return maxLikelyToWorkCL(receivedAcks, cl);
146159 case BATCH_LOG:
147160 return RetryDecision.retry(cl);
148161 }
@@ -168,7 +181,7 @@
168181 return RetryDecision.tryNextHost(null);
169182
170183 // Tries the biggest CL that is expected to work
171- return maxLikelyToWorkCL(aliveReplica);
184+ return maxLikelyToWorkCL(aliveReplica, cl);
172185 }
173186
174187 /**
modified driver-core/src/test/java/com/datastax/driver/core/policies/AbstractRetryPolicyIntegrationTest.java +5 −0
@@ -136,6 +136,11 @@
136136 return query(session);
137137 }
138138
139+ protected ResultSet queryWithCL(ConsistencyLevel cl) {
140+ Statement statement = new SimpleStatement("mock query").setConsistencyLevel(cl);
141+ return session.execute(statement);
142+ }
143+
139144 protected ResultSet query(Session session) {
140145 return session.execute("mock query");
141146 }
modified driver-core/src/test/java/com/datastax/driver/core/policies/DowngradingConsistencyRetryPolicyIntegrationTest.java +26 −0
@@ -77,6 +77,32 @@
7777 };
7878 }
7979
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+ }
80106
81107 /**
82108 * Ensures that when handling a read timeout with {@link DowngradingConsistencyRetryPolicy} that a retry is