Elffuss BraidHub

kikocis/java-driver public

JAVA-764: Retry with the normal consistency level (not the serial one) when a write times out on the Paxos phase.
Changesetdbbfbb772ad42fe3a69481354bb7c35dfc7bbc4825502c1b47bbce35905d571a
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2016-02-10 15:44 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-03-07 16:57 +01:00
Parentsbb1e66449200

Browse the files at this changeset

9 files changed

modified changelog/README.md +1 −0
@@ -33,6 +33,7 @@
3333 - [new feature] JAVA-1019: SchemaBuilder support for CREATE/ALTER/DROP KEYSPACE.
3434 - [bug] JAVA-1070: The Mapper should not prepare queries synchronously.
3535 - [new feature] JAVA-982: Introduce new method ConsistencyLevel.isSerial().
36+- [bug] JAVA-764: Retry with the normal consistency level (not the serial one) when a write times out on the Paxos phase.
3637
3738 Merged from 2.0 branch:
3839
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +2 −1
@@ -382,7 +382,8 @@
382382
383383 private void retry(final boolean retryCurrent, ConsistencyLevel newConsistencyLevel) {
384384 final Host h = current;
385- this.retryConsistencyLevel = newConsistencyLevel;
385+ if (newConsistencyLevel != null)
386+ this.retryConsistencyLevel = newConsistencyLevel;
386387
387388 // We should not retry on the current thread as this will be an IO thread.
388389 manager.executor().execute(new Runnable() {
modified driver-core/src/main/java/com/datastax/driver/core/policies/DefaultRetryPolicy.java +16 −43
@@ -42,9 +42,9 @@
4242 }
4343
4444 /**
45- * Defines whether to retry and at which consistency level on a read timeout.
45+ * {@inheritDoc}
4646 * <p/>
47- * This method triggers a maximum of one retry, and only if enough
47+ * This implementation triggers a maximum of one retry, and only if enough
4848 * replicas had responded to the read request but data was not retrieved
4949 * amongst those. Indeed, that case usually means that enough replica
5050 * are alive to satisfy the consistency but the coordinator picked a
@@ -53,15 +53,6 @@
5353 * timeout the dead replica will likely have been detected as dead and
5454 * the retry has a high chance of success.
5555 *
56- * @param statement the original query that timed out.
57- * @param cl the original consistency level of the read that timed out.
58- * @param requiredResponses the number of responses that were required to
59- * achieve the requested consistency level.
60- * @param receivedResponses the number of responses that had been received
61- * by the time the timeout exception was raised.
62- * @param dataRetrieved whether actual data (by opposition to data checksum)
63- * was present in the received responses.
64- * @param nbRetry the number of retries already performed for this operation.
6556 * @return {@code RetryDecision.retry(cl)} if no retry attempt has yet been tried and
6657 * {@code receivedResponses >= requiredResponses && !dataRetrieved}, {@code RetryDecision.rethrow()} otherwise.
6758 */
@@ -74,9 +65,9 @@
7465 }
7566
7667 /**
77- * Defines whether to retry and at which consistency level on a write timeout.
68+ * {@inheritDoc}
7869 * <p/>
79- * This method triggers a maximum of one retry, and only in the case of
70+ * This implementation triggers a maximum of one retry, and only in the case of
8071 * a {@code WriteType.BATCH_LOG} write. The reasoning for the retry in
8172 * that case is that write to the distributed batch log is tried by the
8273 * coordinator of the write against a small subset of all the nodes alive
@@ -86,14 +77,6 @@
8677 * nodes will likely have been detected as dead and the retry has thus a
8778 * high chance of success.
8879 *
89- * @param statement the original query that timed out.
90- * @param cl the original consistency level of the write that timed out.
91- * @param writeType the type of the write that timed out.
92- * @param requiredAcks the number of acknowledgments that were required to
93- * achieve the requested consistency level.
94- * @param receivedAcks the number of acknowledgments that had been received
95- * by the time the timeout exception was raised.
96- * @param nbRetry the number of retry already performed for this operation.
9780 * @return {@code RetryDecision.retry(cl)} if no retry attempt has yet been tried and
9881 * {@code writeType == WriteType.BATCH_LOG}, {@code RetryDecision.rethrow()} otherwise.
9982 */
@@ -103,37 +86,27 @@
10386 return RetryDecision.rethrow();
10487
10588 // If the batch log write failed, retry the operation as this might just be we were unlucky at picking candidates
89+ // JAVA-764: testing the write type automatically filters out serial consistency levels as these have always WriteType.CAS.
10690 return writeType == WriteType.BATCH_LOG ? RetryDecision.retry(cl) : RetryDecision.rethrow();
10791 }
10892
10993 /**
110- * Defines whether to retry and at which consistency level on an
111- * unavailable exception.
94+ * {@inheritDoc}
11295 * <p/>
113- * This method triggers a retry iff no retry has been executed before
114- * (nbRetry == 0), with
115- * {@link RetryPolicy.RetryDecision#tryNextHost(ConsistencyLevel) RetryDecision.tryNextHost(cl)},
116- * otherwise it throws an exception. The retry will be processed on the next host
117- * in the query plan according to the current Load Balancing Policy.
118- * Where retrying on the same host in the event of an Unavailable exception
119- * has almost no chance of success, if the first replica tried happens to
120- * be "network" isolated from all the other nodes but can still answer to
121- * the client, it makes sense to retry the query on another node.
122- *
123- * @param statement the original query for which the consistency level cannot
124- * be achieved.
125- * @param cl the original consistency level for the operation.
126- * @param requiredReplica the number of replica that should have been
127- * (known) alive for the operation to be attempted.
128- * @param aliveReplica the number of replica that were know to be alive by
129- * the coordinator of the operation.
130- * @param nbRetry the number of retry already performed for this operation.
131- * @return {@code RetryDecision.rethrow()}.
96+ * This implementation does the following:
97+ * <ul>
98+ * <li>if this is the first retry ({@code nbRetry == 0}), it triggers a retry on the next host in the query plan
99+ * with the same consistency level ({@link RetryPolicy.RetryDecision#tryNextHost(ConsistencyLevel) RetryDecision#tryNextHost(null)}.
100+ * The rationale is that the first coordinator might have been network-isolated from all other nodes (thinking
101+ * they're down), but still able to communicate with the client; in that case, retrying on the same host has almost
102+ * no chance of success, but moving to the next host might solve the issue.</li>
103+ * <li>otherwise, the exception is rethrow.</li>
104+ * </ul>
132105 */
133106 @Override
134107 public RetryDecision onUnavailable(Statement statement, ConsistencyLevel cl, int requiredReplica, int aliveReplica, int nbRetry) {
135108 return (nbRetry == 0)
136- ? RetryDecision.tryNextHost(cl)
109+ ? RetryDecision.tryNextHost(null)
137110 : RetryDecision.rethrow();
138111 }
139112
modified driver-core/src/main/java/com/datastax/driver/core/policies/DowngradingConsistencyRetryPolicy.java +11 −38
@@ -85,25 +85,14 @@
8585 }
8686
8787 /**
88- * Defines whether to retry and at which consistency level on a read timeout.
88+ * {@inheritDoc}
8989 * <p/>
90- * This method triggers a maximum of one retry. If less replica
90+ * This implementation triggers a maximum of one retry. If less replica
9191 * responded than required by the consistency level (but at least one
9292 * replica did respond), the operation is retried at a lower
9393 * consistency level. If enough replica responded but data was not
9494 * retrieve, the operation is retried with the initial consistency
9595 * level. Otherwise, an exception is thrown.
96- *
97- * @param statement the original query that timed out.
98- * @param cl the original consistency level of the read that timed out.
99- * @param requiredResponses the number of responses that were required to
100- * achieve the requested consistency level.
101- * @param receivedResponses the number of responses that had been received
102- * by the time the timeout exception was raised.
103- * @param dataRetrieved whether actual data (by opposition to data checksum)
104- * was present in the received responses.
105- * @param nbRetry the number of retry already performed for this operation.
106- * @return a RetryDecision as defined above.
10796 */
10897 @Override
10998 public RetryDecision onReadTimeout(Statement statement, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataRetrieved, int nbRetry) {
@@ -126,9 +115,9 @@
126115 }
127116
128117 /**
129- * Defines whether to retry and at which consistency level on a write timeout.
118+ * {@inheritDoc}
130119 * <p/>
131- * This method triggers a maximum of one retry. If {@code writeType ==
120+ * This implementation triggers a maximum of one retry. If {@code writeType ==
132121 * WriteType.BATCH_LOG}, the write is retried with the initial
133122 * consistency level. If {@code writeType == WriteType.UNLOGGED_BATCH}
134123 * and at least one replica acknowledged, the write is retried with a
@@ -137,16 +126,6 @@
137126 * all, even if {@code receivedAcks > 0}). For other write types ({@code WriteType.SIMPLE}
138127 * and {@code WriteType.BATCH}), if we know the write has been persisted on at
139128 * least one replica, we ignore the exception. Otherwise, an exception is thrown.
140- *
141- * @param statement the original query that timed out.
142- * @param cl the original consistency level of the write that timed out.
143- * @param writeType the type of the write that timed out.
144- * @param requiredAcks the number of acknowledgments that were required to
145- * achieve the requested consistency level.
146- * @param receivedAcks the number of acknowledgments that had been received
147- * by the time the timeout exception was raised.
148- * @param nbRetry the number of retry already performed for this operation.
149- * @return a RetryDecision as defined above.
150129 */
151130 @Override
152131 public RetryDecision onWriteTimeout(Statement statement, ConsistencyLevel cl, WriteType writeType, int requiredAcks, int receivedAcks, int nbRetry) {
@@ -170,28 +149,22 @@
170149 }
171150
172151 /**
173- * Defines whether to retry and at which consistency level on an
174- * unavailable exception.
152+ * {@inheritDoc}
175153 * <p/>
176- * This method triggers a maximum of one retry. If at least one replica
154+ * This implementation triggers a maximum of one retry. If at least one replica
177155 * is know to be alive, the operation is retried at a lower consistency
178156 * level.
179- *
180- * @param statement the original query for which the consistency level cannot
181- * be achieved.
182- * @param cl the original consistency level for the operation.
183- * @param requiredReplica the number of replica that should have been
184- * (known) alive for the operation to be attempted.
185- * @param aliveReplica the number of replica that were know to be alive by
186- * the coordinator of the operation.
187- * @param nbRetry the number of retry already performed for this operation.
188- * @return a RetryDecision as defined above.
189157 */
190158 @Override
191159 public RetryDecision onUnavailable(Statement statement, ConsistencyLevel cl, int requiredReplica, int aliveReplica, int nbRetry) {
192160 if (nbRetry != 0)
193161 return RetryDecision.rethrow();
194162
163+ // JAVA-764: if the requested consistency level is serial, it means that the operation failed at the paxos phase of a LWT.
164+ // Retry on the next host, on the assumption that the initial coordinator could be network-isolated.
165+ if (cl.isSerial())
166+ return RetryDecision.tryNextHost(null);
167+
195168 // Tries the biggest CL that is expected to work
196169 return maxLikelyToWorkCL(aliveReplica);
197170 }
modified driver-core/src/main/java/com/datastax/driver/core/policies/ExtendedRetryPolicy.java +3 −1
@@ -49,7 +49,9 @@
4949 * is known to be idempotent.
5050 *
5151 * @param statement the original query that failed.
52- * @param cl the original consistency level for the operation.
52+ * @param cl the requested consistency level for the operation.
53+ * Note that this is not necessarily the achieved consistency level (if any),
54+ * and it is never a {@link ConsistencyLevel#isSerial() serial} one.
5355 * @param e the exception that caused this request to fail.
5456 * @param nbRetry the number of retries already performed for this operation.
5557 * @return the retry decision. If {@code RetryDecision.RETHROW} is returned,
modified driver-core/src/main/java/com/datastax/driver/core/policies/FallthroughRetryPolicy.java +12 −38
@@ -20,10 +20,10 @@
2020 import com.datastax.driver.core.WriteType;
2121
2222 /**
23- * A retry policy that never retry (nor ignore).
23+ * A retry policy that never retries (nor ignores).
2424 * <p/>
25- * All of the methods of this retry policy unconditionally return {@link RetryPolicy.RetryDecision#rethrow}.
26- * If this policy is used, retry will have to be implemented in business code.
25+ * All of the methods of this retry policy unconditionally return {@link RetryDecision#rethrow()}.
26+ * If this policy is used, retry logic will have to be implemented in business code.
2727 */
2828 public class FallthroughRetryPolicy implements ExtendedRetryPolicy {
2929
@@ -33,18 +33,9 @@
3333 }
3434
3535 /**
36- * Defines whether to retry and at which consistency level on a read timeout.
37- *
38- * @param statement the original query that timed out.
39- * @param cl the original consistency level of the read that timed out.
40- * @param requiredResponses the number of responses that were required to
41- * achieve the requested consistency level.
42- * @param receivedResponses the number of responses that had been received
43- * by the time the timeout exception was raised.
44- * @param dataRetrieved whether actual data (by opposition to data checksum)
45- * was present in the received responses.
46- * @param nbRetry the number of retry already performed for this operation.
47- * @return {@code RetryDecision.rethrow()}.
36+ * {@inheritDoc}
37+ * <p/>
38+ * This implementation always returns {@code RetryDecision.rethrow()}.
4839 */
4940 @Override
5041 public RetryDecision onReadTimeout(Statement statement, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataRetrieved, int nbRetry) {
@@ -52,17 +43,9 @@
5243 }
5344
5445 /**
55- * Defines whether to retry and at which consistency level on a write timeout.
56- *
57- * @param statement the original query that timed out.
58- * @param cl the original consistency level of the write that timed out.
59- * @param writeType the type of the write that timed out.
60- * @param requiredAcks the number of acknowledgments that were required to
61- * achieve the requested consistency level.
62- * @param receivedAcks the number of acknowledgments that had been received
63- * by the time the timeout exception was raised.
64- * @param nbRetry the number of retry already performed for this operation.
65- * @return {@code RetryDecision.rethrow()}.
46+ * {@inheritDoc}
47+ * <p/>
48+ * This implementation always returns {@code RetryDecision.rethrow()}.
6649 */
6750 @Override
6851 public RetryDecision onWriteTimeout(Statement statement, ConsistencyLevel cl, WriteType writeType, int requiredAcks, int receivedAcks, int nbRetry) {
@@ -70,18 +53,9 @@
7053 }
7154
7255 /**
73- * Defines whether to retry and at which consistency level on an
74- * unavailable exception.
75- *
76- * @param statement the original query for which the consistency level cannot
77- * be achieved.
78- * @param cl the original consistency level for the operation.
79- * @param requiredReplica the number of replica that should have been
80- * (known) alive for the operation to be attempted.
81- * @param aliveReplica the number of replica that were know to be alive by
82- * the coordinator of the operation.
83- * @param nbRetry the number of retry already performed for this operation.
84- * @return {@code RetryDecision.rethrow()}.
56+ * {@inheritDoc}
57+ * <p/>
58+ * This implementation always returns {@code RetryDecision.rethrow()}.
8559 */
8660 @Override
8761 public RetryDecision onUnavailable(Statement statement, ConsistencyLevel cl, int requiredReplica, int aliveReplica, int nbRetry) {
modified driver-core/src/main/java/com/datastax/driver/core/policies/LoggingRetryPolicy.java +8 −3
@@ -25,9 +25,14 @@
2525 /**
2626 * A retry policy that wraps another policy, logging the decision made by its sub-policy.
2727 * <p/>
28- * Note that this policy only log the IGNORE and RETRY decisions (since
29- * RETHROW decisions are just meant to propagate the cassandra exception). The
30- * logging is done at the INFO level.
28+ * Note that this policy only logs
29+ * {@link com.datastax.driver.core.policies.RetryPolicy.RetryDecision.Type#RETRY RETRY} and
30+ * {@link com.datastax.driver.core.policies.RetryPolicy.RetryDecision.Type#IGNORE IGNORE} decisions (since
31+ * {@link com.datastax.driver.core.policies.RetryPolicy.RetryDecision.Type#RETHROW RETHROW} decisions
32+ * are just meant to propagate the Cassandra exception).
33+ * <p/>
34+ * The logging is done at the INFO level and the logger name is
35+ * {@code com.datastax.driver.core.policies.LoggingRetryPolicy}.
3136 */
3237 public class LoggingRetryPolicy implements ExtendedRetryPolicy {
3338
modified driver-core/src/main/java/com/datastax/driver/core/policies/RetryPolicy.java +40 −8
@@ -75,7 +75,7 @@
7575 * {@code IGNORE} or a {@code RETHROW} decision;
7676 * for a {@code RETRY} decision, the consistency level can be {@code null},
7777 * in which case the retry is done at the same consistency level
78- * as in the original attempt.
78+ * as in the previous attempt.
7979 *
8080 * @return the consistency level for a retry decision.
8181 */
@@ -106,8 +106,16 @@
106106 /**
107107 * Creates a {@link RetryDecision.Type#RETRY} retry decision using
108108 * the same host and the provided consistency level.
109+ * <p/>
110+ * If the provided consistency level is {@code null}, the retry will be done at the same consistency level as
111+ * the previous attempt.
112+ * <p/>
113+ * Beware that {@link ConsistencyLevel#isSerial() serial} consistency levels
114+ * should never be passed to this method; attempting to do so would trigger an
115+ * {@link com.datastax.driver.core.exceptions.InvalidQueryException InvalidQueryException}.
109116 *
110- * @param consistency the consistency level to use for the retry.
117+ * @param consistency the consistency level to use for the retry; if {@code null},
118+ * the same level as the previous attempt will be used.
111119 * @return a {@link RetryDecision.Type#RETRY} decision using
112120 * the same host and the provided consistency level
113121 */
@@ -127,19 +135,30 @@
127135 /**
128136 * Creates a {@link RetryDecision.Type#RETRY} retry decision using the next host
129137 * in the query plan, and using the provided consistency level.
138+ * <p/>
139+ * If the provided consistency level is {@code null}, the retry will be done at the same consistency level as
140+ * the previous attempt.
141+ * <p/>
142+ * Beware that {@link ConsistencyLevel#isSerial() serial} consistency levels
143+ * should never be passed to this method; attempting to do so would trigger an
144+ * {@link com.datastax.driver.core.exceptions.InvalidQueryException InvalidQueryException}.
130145 *
146+ * @param consistency the consistency level to use for the retry; if {@code null},
147+ * the same level as the previous attempt will be used.
131148 * @return a {@link RetryDecision.Type#RETRY} retry decision using the next host
132149 * in the query plan, and using the provided consistency level.
133150 */
134- public static RetryDecision tryNextHost(ConsistencyLevel retryCL) {
135- return new RetryDecision(Type.RETRY, retryCL, false);
151+ public static RetryDecision tryNextHost(ConsistencyLevel consistency) {
152+ return new RetryDecision(Type.RETRY, consistency, false);
136153 }
137154
138155 @Override
139156 public String toString() {
140157 switch (type) {
141158 case RETRY:
142- return "Retry at " + retryCL + " on " + (retryCurrent ? "same " : "next ") + "host.";
159+ String retryClDesc = (retryCL == null) ? "same CL" : retryCL.toString();
160+ String hostDesc = retryCurrent ? "same" : "next";
161+ return "Retry at " + retryClDesc + " on " + hostDesc + " host.";
143162 case RETHROW:
144163 return "Rethrow";
145164 case IGNORE:
@@ -158,7 +177,9 @@
158177 * {@link com.datastax.driver.core.exceptions.ReadTimeoutException#wasDataRetrieved}).
159178 *
160179 * @param statement the original query that timed out.
161- * @param cl the original consistency level of the read that timed out.
180+ * @param cl the requested consistency level of the read that timed out.
181+ * Note that this can never be a {@link ConsistencyLevel#isSerial() serial}
182+ * consistency level.
162183 * @param requiredResponses the number of responses that were required to
163184 * achieve the requested consistency level.
164185 * @param receivedResponses the number of responses that had been received
@@ -176,7 +197,13 @@
176197 * Defines whether to retry and at which consistency level on a write timeout.
177198 *
178199 * @param statement the original query that timed out.
179- * @param cl the original consistency level of the write that timed out.
200+ * @param cl the requested consistency level of the write that timed out.
201+ * If the timeout occurred at the "paxos" phase of a
202+ * <a href="https://docs.datastax.com/en/cassandra/2.1/cassandra/dml/dml_ltwt_transaction_c.html">Lightweight transaction</a>,
203+ * then {@code cl} will actually be the requested {@link ConsistencyLevel#isSerial() serial} consistency level.
204+ * <em>Beware that serial consistency levels should never be passed to a {@link RetryDecision RetryDecision} as this would
205+ * invariably trigger an {@link com.datastax.driver.core.exceptions.InvalidQueryException InvalidQueryException}</em>.
206+ * Also, when {@code cl} is {@link ConsistencyLevel#isSerial() serial}, then {@code writeType} is always {@link WriteType#CAS CAS}.
180207 * @param writeType the type of the write that timed out.
181208 * @param requiredAcks the number of acknowledgments that were required to
182209 * achieve the requested consistency level.
@@ -195,7 +222,12 @@
195222 *
196223 * @param statement the original query for which the consistency level cannot
197224 * be achieved.
198- * @param cl the original consistency level for the operation.
225+ * @param cl the requested consistency level for the operation.
226+ * If the operation failed at the "paxos" phase of a
227+ * <a href="https://docs.datastax.com/en/cassandra/2.1/cassandra/dml/dml_ltwt_transaction_c.html">Lightweight transaction</a>,
228+ * then {@code cl} will actually be the requested {@link ConsistencyLevel#isSerial() serial} consistency level.
229+ * <em>Beware that serial consistency levels should never be passed to a {@link RetryDecision RetryDecision} as this would
230+ * invariably trigger an {@link com.datastax.driver.core.exceptions.InvalidQueryException InvalidQueryException}</em>.
199231 * @param requiredReplica the number of replica that should have been
200232 * (known) alive for the operation to be attempted.
201233 * @param aliveReplica the number of replica that were know to be alive by
added driver-core/src/test/java/com/datastax/driver/core/policies/CASRetryPolicyIntegrationTest.java +108 −0
@@ -0,0 +1,108 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.core.policies;
17+
18+import com.datastax.driver.core.*;
19+import com.datastax.driver.core.exceptions.NoHostAvailableException;
20+import com.datastax.driver.core.exceptions.UnavailableException;
21+import com.datastax.driver.core.utils.CassandraVersion;
22+import org.testng.annotations.Test;
23+
24+import static com.datastax.driver.core.Assertions.assertThat;
25+import static com.datastax.driver.core.ConsistencyLevel.ANY;
26+import static com.datastax.driver.core.ConsistencyLevel.SERIAL;
27+import static com.datastax.driver.core.CreateCCM.TestMode.PER_METHOD;
28+import static org.assertj.core.api.Assertions.fail;
29+import static org.mockito.Matchers.eq;
30+import static org.mockito.Mockito.*;
31+
32+/**
33+ * Tests for retry policies in combination with lightweight transactions.
34+ * <p/>
35+ * Note: we can't extend {@link AbstractRetryPolicyIntegrationTest} here, because we are
36+ * testing LWT statements and Scassandra doesn't support priming consistency levels yet.
37+ *
38+ * @jira_ticket JAVA-764
39+ */
40+@CCMConfig(
41+ numberOfNodes = 3,
42+ dirtiesContext = true,
43+ createCluster = false
44+)
45+@CreateCCM(PER_METHOD)
46+@CassandraVersion(major = 2.0)
47+public class CASRetryPolicyIntegrationTest extends CCMTestsSupport {
48+
49+ @Test(groups = "long")
50+ public void should_rethrow_on_unavailable_with_default_policy_if_CAS() {
51+ testLWT(spy(DefaultRetryPolicy.INSTANCE));
52+ }
53+
54+ @Test(groups = "long")
55+ public void should_rethrow_on_unavailable_with_downgrading_policy_if_CAS() {
56+ testLWT(spy(DowngradingConsistencyRetryPolicy.INSTANCE));
57+ }
58+
59+ private void testLWT(RetryPolicy retryPolicy) {
60+
61+ Cluster cluster = register(Cluster.builder()
62+ .addContactPoints(getContactPoints().get(0))
63+ .withPort(ccm().getBinaryPort())
64+ .withSocketOptions(new SocketOptions().setReadTimeoutMillis(120000))
65+ .withRetryPolicy(retryPolicy)
66+ .build());
67+
68+ Session session = cluster.connect();
69+
70+ String ks = TestUtils.generateIdentifier("ks_");
71+ session.execute(String.format("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3}", ks));
72+ useKeyspace(session, ks);
73+ session.execute("CREATE TABLE IF NOT EXISTS foo (k int primary key, c int)");
74+
75+ session.execute("INSERT INTO foo(k, c) VALUES (0, 0)");
76+
77+ ccm().stop(1);
78+ ccm().waitForDown(1);
79+ TestUtils.waitForDown(ccm().addressOfNode(1).getHostName(), cluster);
80+
81+ ccm().stop(2);
82+ ccm().waitForDown(2);
83+ TestUtils.waitForDown(ccm().addressOfNode(2).getHostName(), cluster);
84+
85+ Statement s = new SimpleStatement("UPDATE foo SET c = 1 WHERE k = 0 IF c = 0")
86+ // the following will cause the paxos phase to fail
87+ // given the number of available replicas (1)
88+ .setConsistencyLevel(ANY)
89+ .setSerialConsistencyLevel(SERIAL);
90+
91+ try {
92+ session.execute(s);
93+ // Expect a NHAE since a retry should have been attempted on unavailable error with CL SERIAL.
94+ fail("Expected NoHostAvailableException");
95+ } catch (NoHostAvailableException e) {
96+ Throwable error = e.getErrors().values().iterator().next();
97+ assertThat(error).isInstanceOf(UnavailableException.class);
98+ UnavailableException unavailable = (UnavailableException) error;
99+ assertThat(unavailable.getConsistencyLevel()).isEqualTo(SERIAL);
100+ assertThat(unavailable.getRequiredReplicas()).isEqualTo(2);
101+ assertThat(unavailable.getAliveReplicas()).isEqualTo(1);
102+ }
103+
104+ verify(retryPolicy, times(1)).onUnavailable(
105+ eq(s), eq(SERIAL), eq(2), eq(1), eq(0));
106+ }
107+
108+}