Elffuss BraidHub

kikocis/java-driver public

JAVA-764: LWT / CAS Consistency bug on Retry Policy.

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

Browse the files at this changeset

12 files changed

modified changelog/README.md +2 −0
@@ -16,6 +16,8 @@
1616 - [bug] JAVA-1089: Set LWT made from BuiltStatements to non-idempotent.
1717 - [improvement] JAVA-923: Position idempotent flag on object mapper queries.
1818 - [bug] JAVA-1070: The Mapper should not prepare queries synchronously.
19+- [new feature] JAVA-982: Introduce new method ConsistencyLevel.isSerial().
20+- [bug] JAVA-764: Retry with the normal consistency level (not the serial one) when a write times out on the Paxos phase.
1921
2022
2123 ### 3.0.0
modified driver-core/src/main/java/com/datastax/driver/core/ConsistencyLevel.java +20 −1
@@ -58,11 +58,30 @@
5858 }
5959
6060 /**
61- * Whether or not the the consistency level applies to the local data-center only.
61+ * Whether or not this consistency level applies to the local data-center only.
6262 *
6363 * @return whether this consistency level is {@code LOCAL_ONE} or {@code LOCAL_QUORUM}.
6464 */
6565 public boolean isDCLocal() {
6666 return this == LOCAL_ONE || this == LOCAL_QUORUM;
6767 }
68+
69+ /**
70+ * Whether or not this consistency level is serial, that is,
71+ * applies only to the "paxos" phase of a
72+ * <a href="https://docs.datastax.com/en/cassandra/2.1/cassandra/dml/dml_ltwt_transaction_c.html">Lightweight transaction</a>.
73+ * <p/>
74+ * Serial consistency levels are only meaningful when executing conditional updates ({@code INSERT}, {@code UPDATE}
75+ * or {@code DELETE} statements with an {@code IF} condition).
76+ * <p/>
77+ * Two consistency levels belong to this category: {@link #SERIAL} and {@link #LOCAL_SERIAL}.
78+ *
79+ * @return whether this consistency level is {@link #SERIAL} or {@link #LOCAL_SERIAL}.
80+ * @see Statement#setSerialConsistencyLevel(ConsistencyLevel)
81+ * @see <a href="https://docs.datastax.com/en/cassandra/2.1/cassandra/dml/dml_ltwt_transaction_c.html">Lightweight transactions</a>
82+ */
83+ public boolean isSerial() {
84+ return this == SERIAL || this == LOCAL_SERIAL;
85+ }
86+
6887 }
modified driver-core/src/main/java/com/datastax/driver/core/DefaultPreparedStatement.java +1 −1
@@ -161,7 +161,7 @@
161161
162162 @Override
163163 public PreparedStatement setSerialConsistencyLevel(ConsistencyLevel serialConsistency) {
164- if (serialConsistency != ConsistencyLevel.SERIAL && serialConsistency != ConsistencyLevel.LOCAL_SERIAL)
164+ if (!serialConsistency.isSerial())
165165 throw new IllegalArgumentException();
166166 this.serialConsistency = serialConsistency;
167167 return this;
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +2 −1
@@ -383,7 +383,8 @@
383383
384384 private void retry(final boolean retryCurrent, ConsistencyLevel newConsistencyLevel) {
385385 final Host h = current;
386- this.retryConsistencyLevel = newConsistencyLevel;
386+ if (newConsistencyLevel != null)
387+ this.retryConsistencyLevel = newConsistencyLevel;
387388
388389 // We should not retry on the current thread as this will be an IO thread.
389390 manager.executor().execute(new Runnable() {
modified driver-core/src/main/java/com/datastax/driver/core/Statement.java +8 −7
@@ -98,8 +98,9 @@
9898 /**
9999 * Sets the serial consistency level for the query.
100100 * <p/>
101- * The serial consistency level is only used by conditional updates (so INSERT, UPDATE
102- * and DELETE with an IF condition). For those, the serial consistency level defines
101+ * The serial consistency level is only used by conditional updates ({@code INSERT}, {@code UPDATE}
102+ * or {@code DELETE} statements with an {@code IF} condition).
103+ * For those, the serial consistency level defines
103104 * the consistency level of the serial phase (or "paxos" phase) while the
104105 * normal consistency level defines the consistency for the "learn" phase, i.e. what
105106 * type of reads will be guaranteed to see the update right away. For instance, if
@@ -122,8 +123,8 @@
122123 * {@code ConsistencyLevel.SERIAL} or {@code ConsistencyLevel.LOCAL_SERIAL}.
123124 */
124125 public Statement setSerialConsistencyLevel(ConsistencyLevel serialConsistency) {
125- if (serialConsistency != ConsistencyLevel.SERIAL && serialConsistency != ConsistencyLevel.LOCAL_SERIAL)
126- throw new IllegalArgumentException();
126+ if (!serialConsistency.isSerial())
127+ throw new IllegalArgumentException("Supplied consistency level is not serial: " + serialConsistency);
127128 this.serialConsistency = serialConsistency;
128129 return this;
129130 }
@@ -131,10 +132,10 @@
131132 /**
132133 * The serial consistency level for this query.
133134 * <p/>
134- * See {@link #setSerialConsistencyLevel} for more detail on the serial consistency level.
135+ * See {@link #setSerialConsistencyLevel(ConsistencyLevel)} for more detail on the serial consistency level.
135136 *
136- * @return the consistency level for this query, or {@code null} if no serial
137- * consistency level has been specified (through {@code setSerialConsistencyLevel}).
137+ * @return the serial consistency level for this query, or {@code null} if no serial
138+ * consistency level has been specified (through {@link #setSerialConsistencyLevel(ConsistencyLevel)}).
138139 * In the latter case, the default serial consistency level will be used.
139140 */
140141 public ConsistencyLevel getSerialConsistencyLevel() {
modified driver-core/src/main/java/com/datastax/driver/core/policies/DefaultRetryPolicy.java +16 −43
@@ -44,9 +44,9 @@
4444 }
4545
4646 /**
47- * Defines whether to retry and at which consistency level on a read timeout.
47+ * {@inheritDoc}
4848 * <p/>
49- * This method triggers a maximum of one retry, and only if enough
49+ * This implementation triggers a maximum of one retry, and only if enough
5050 * replicas had responded to the read request but data was not retrieved
5151 * amongst those. Indeed, that case usually means that enough replica
5252 * are alive to satisfy the consistency but the coordinator picked a
@@ -55,15 +55,6 @@
5555 * timeout the dead replica will likely have been detected as dead and
5656 * the retry has a high chance of success.
5757 *
58- * @param statement the original query that timed out.
59- * @param cl the original consistency level of the read that timed out.
60- * @param requiredResponses the number of responses that were required to
61- * achieve the requested consistency level.
62- * @param receivedResponses the number of responses that had been received
63- * by the time the timeout exception was raised.
64- * @param dataRetrieved whether actual data (by opposition to data checksum)
65- * was present in the received responses.
66- * @param nbRetry the number of retries already performed for this operation.
6758 * @return {@code RetryDecision.retry(cl)} if no retry attempt has yet been tried and
6859 * {@code receivedResponses >= requiredResponses && !dataRetrieved}, {@code RetryDecision.rethrow()} otherwise.
6960 */
@@ -76,9 +67,9 @@
7667 }
7768
7869 /**
79- * Defines whether to retry and at which consistency level on a write timeout.
70+ * {@inheritDoc}
8071 * <p/>
81- * This method triggers a maximum of one retry, and only in the case of
72+ * This implementation triggers a maximum of one retry, and only in the case of
8273 * a {@code WriteType.BATCH_LOG} write. The reasoning for the retry in
8374 * that case is that write to the distributed batch log is tried by the
8475 * coordinator of the write against a small subset of all the nodes alive
@@ -88,14 +79,6 @@
8879 * nodes will likely have been detected as dead and the retry has thus a
8980 * high chance of success.
9081 *
91- * @param statement the original query that timed out.
92- * @param cl the original consistency level of the write that timed out.
93- * @param writeType the type of the write that timed out.
94- * @param requiredAcks the number of acknowledgments that were required to
95- * achieve the requested consistency level.
96- * @param receivedAcks the number of acknowledgments that had been received
97- * by the time the timeout exception was raised.
98- * @param nbRetry the number of retry already performed for this operation.
9982 * @return {@code RetryDecision.retry(cl)} if no retry attempt has yet been tried and
10083 * {@code writeType == WriteType.BATCH_LOG}, {@code RetryDecision.rethrow()} otherwise.
10184 */
@@ -105,37 +88,27 @@
10588 return RetryDecision.rethrow();
10689
10790 // If the batch log write failed, retry the operation as this might just be we were unlucky at picking candidates
91+ // JAVA-764: testing the write type automatically filters out serial consistency levels as these have always WriteType.CAS.
10892 return writeType == WriteType.BATCH_LOG ? RetryDecision.retry(cl) : RetryDecision.rethrow();
10993 }
11094
11195 /**
112- * Defines whether to retry and at which consistency level on an
113- * unavailable exception.
96+ * {@inheritDoc}
11497 * <p/>
115- * This method triggers a retry iff no retry has been executed before
116- * (nbRetry == 0), with
117- * {@link RetryPolicy.RetryDecision#tryNextHost(ConsistencyLevel) RetryDecision.tryNextHost(cl)},
118- * otherwise it throws an exception. The retry will be processed on the next host
119- * in the query plan according to the current Load Balancing Policy.
120- * Where retrying on the same host in the event of an Unavailable exception
121- * has almost no chance of success, if the first replica tried happens to
122- * be "network" isolated from all the other nodes but can still answer to
123- * the client, it makes sense to retry the query on another node.
124- *
125- * @param statement the original query for which the consistency level cannot
126- * be achieved.
127- * @param cl the original consistency level for the operation.
128- * @param requiredReplica the number of replica that should have been
129- * (known) alive for the operation to be attempted.
130- * @param aliveReplica the number of replica that were know to be alive by
131- * the coordinator of the operation.
132- * @param nbRetry the number of retry already performed for this operation.
133- * @return {@code RetryDecision.rethrow()}.
98+ * This implementation does the following:
99+ * <ul>
100+ * <li>if this is the first retry ({@code nbRetry == 0}), it triggers a retry on the next host in the query plan
101+ * with the same consistency level ({@link RetryPolicy.RetryDecision#tryNextHost(ConsistencyLevel) RetryDecision#tryNextHost(null)}.
102+ * The rationale is that the first coordinator might have been network-isolated from all other nodes (thinking
103+ * they're down), but still able to communicate with the client; in that case, retrying on the same host has almost
104+ * no chance of success, but moving to the next host might solve the issue.</li>
105+ * <li>otherwise, the exception is rethrow.</li>
106+ * </ul>
134107 */
135108 @Override
136109 public RetryDecision onUnavailable(Statement statement, ConsistencyLevel cl, int requiredReplica, int aliveReplica, int nbRetry) {
137110 return (nbRetry == 0)
138- ? RetryDecision.tryNextHost(cl)
111+ ? RetryDecision.tryNextHost(null)
139112 : RetryDecision.rethrow();
140113 }
141114
modified driver-core/src/main/java/com/datastax/driver/core/policies/DowngradingConsistencyRetryPolicy.java +12 −39
@@ -87,25 +87,14 @@
8787 }
8888
8989 /**
90- * Defines whether to retry and at which consistency level on a read timeout.
90+ * {@inheritDoc}
9191 * <p/>
92- * This method triggers a maximum of one retry. If less replica
92+ * This implementation triggers a maximum of one retry. If less replica
9393 * responded than required by the consistency level (but at least one
9494 * replica did respond), the operation is retried at a lower
9595 * consistency level. If enough replica responded but data was not
9696 * retrieve, the operation is retried with the initial consistency
9797 * level. Otherwise, an exception is thrown.
98- *
99- * @param statement the original query that timed out.
100- * @param cl the original consistency level of the read that timed out.
101- * @param requiredResponses the number of responses that were required to
102- * achieve the requested consistency level.
103- * @param receivedResponses the number of responses that had been received
104- * by the time the timeout exception was raised.
105- * @param dataRetrieved whether actual data (by opposition to data checksum)
106- * was present in the received responses.
107- * @param nbRetry the number of retry already performed for this operation.
108- * @return a RetryDecision as defined above.
10998 */
11099 @Override
111100 public RetryDecision onReadTimeout(Statement statement, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataRetrieved, int nbRetry) {
@@ -116,7 +105,7 @@
116105 // normal consistency levels on the committing phase. So the main use case for CAS reads is probably for
117106 // when you've timed out on a CAS write and want to make sure what happened. Downgrading in that case
118107 // would be always wrong so we just special case to rethrow.
119- if (cl == ConsistencyLevel.SERIAL || cl == ConsistencyLevel.LOCAL_SERIAL)
108+ if (cl.isSerial())
120109 return RetryDecision.rethrow();
121110
122111 if (receivedResponses < requiredResponses) {
@@ -128,9 +117,9 @@
128117 }
129118
130119 /**
131- * Defines whether to retry and at which consistency level on a write timeout.
120+ * {@inheritDoc}
132121 * <p/>
133- * This method triggers a maximum of one retry. If {@code writeType ==
122+ * This implementation triggers a maximum of one retry. If {@code writeType ==
134123 * WriteType.BATCH_LOG}, the write is retried with the initial
135124 * consistency level. If {@code writeType == WriteType.UNLOGGED_BATCH}
136125 * and at least one replica acknowledged, the write is retried with a
@@ -139,16 +128,6 @@
139128 * all, even if {@code receivedAcks > 0}). For other write types ({@code WriteType.SIMPLE}
140129 * and {@code WriteType.BATCH}), if we know the write has been persisted on at
141130 * least one replica, we ignore the exception. Otherwise, an exception is thrown.
142- *
143- * @param statement the original query that timed out.
144- * @param cl the original consistency level of the write that timed out.
145- * @param writeType the type of the write that timed out.
146- * @param requiredAcks the number of acknowledgments that were required to
147- * achieve the requested consistency level.
148- * @param receivedAcks the number of acknowledgments that had been received
149- * by the time the timeout exception was raised.
150- * @param nbRetry the number of retry already performed for this operation.
151- * @return a RetryDecision as defined above.
152131 */
153132 @Override
154133 public RetryDecision onWriteTimeout(Statement statement, ConsistencyLevel cl, WriteType writeType, int requiredAcks, int receivedAcks, int nbRetry) {
@@ -172,28 +151,22 @@
172151 }
173152
174153 /**
175- * Defines whether to retry and at which consistency level on an
176- * unavailable exception.
154+ * {@inheritDoc}
177155 * <p/>
178- * This method triggers a maximum of one retry. If at least one replica
156+ * This implementation triggers a maximum of one retry. If at least one replica
179157 * is know to be alive, the operation is retried at a lower consistency
180158 * level.
181- *
182- * @param statement the original query for which the consistency level cannot
183- * be achieved.
184- * @param cl the original consistency level for the operation.
185- * @param requiredReplica the number of replica that should have been
186- * (known) alive for the operation to be attempted.
187- * @param aliveReplica the number of replica that were know to be alive by
188- * the coordinator of the operation.
189- * @param nbRetry the number of retry already performed for this operation.
190- * @return a RetryDecision as defined above.
191159 */
192160 @Override
193161 public RetryDecision onUnavailable(Statement statement, ConsistencyLevel cl, int requiredReplica, int aliveReplica, int nbRetry) {
194162 if (nbRetry != 0)
195163 return RetryDecision.rethrow();
196164
165+ // JAVA-764: if the requested consistency level is serial, it means that the operation failed at the paxos phase of a LWT.
166+ // Retry on the next host, on the assumption that the initial coordinator could be network-isolated.
167+ if (cl.isSerial())
168+ return RetryDecision.tryNextHost(null);
169+
197170 // Tries the biggest CL that is expected to work
198171 return maxLikelyToWorkCL(aliveReplica);
199172 }
modified driver-core/src/main/java/com/datastax/driver/core/policies/FallthroughRetryPolicy.java +12 −38
@@ -22,10 +22,10 @@
2222 import com.datastax.driver.core.exceptions.DriverException;
2323
2424 /**
25- * A retry policy that never retry (nor ignore).
25+ * A retry policy that never retries (nor ignores).
2626 * <p/>
27- * All of the methods of this retry policy unconditionally return {@link RetryPolicy.RetryDecision#rethrow}.
28- * If this policy is used, retry will have to be implemented in business code.
27+ * All of the methods of this retry policy unconditionally return {@link RetryDecision#rethrow()}.
28+ * If this policy is used, retry logic will have to be implemented in business code.
2929 */
3030 public class FallthroughRetryPolicy implements RetryPolicy {
3131
@@ -35,18 +35,9 @@
3535 }
3636
3737 /**
38- * Defines whether to retry and at which consistency level on a read timeout.
39- *
40- * @param statement the original query that timed out.
41- * @param cl the original consistency level of the read that timed out.
42- * @param requiredResponses the number of responses that were required to
43- * achieve the requested consistency level.
44- * @param receivedResponses the number of responses that had been received
45- * by the time the timeout exception was raised.
46- * @param dataRetrieved whether actual data (by opposition to data checksum)
47- * was present in the received responses.
48- * @param nbRetry the number of retry already performed for this operation.
49- * @return {@code RetryDecision.rethrow()}.
38+ * {@inheritDoc}
39+ * <p/>
40+ * This implementation always returns {@code RetryDecision.rethrow()}.
5041 */
5142 @Override
5243 public RetryDecision onReadTimeout(Statement statement, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataRetrieved, int nbRetry) {
@@ -54,17 +45,9 @@
5445 }
5546
5647 /**
57- * Defines whether to retry and at which consistency level on a write timeout.
58- *
59- * @param statement the original query that timed out.
60- * @param cl the original consistency level of the write that timed out.
61- * @param writeType the type of the write that timed out.
62- * @param requiredAcks the number of acknowledgments that were required to
63- * achieve the requested consistency level.
64- * @param receivedAcks the number of acknowledgments that had been received
65- * by the time the timeout exception was raised.
66- * @param nbRetry the number of retry already performed for this operation.
67- * @return {@code RetryDecision.rethrow()}.
48+ * {@inheritDoc}
49+ * <p/>
50+ * This implementation always returns {@code RetryDecision.rethrow()}.
6851 */
6952 @Override
7053 public RetryDecision onWriteTimeout(Statement statement, ConsistencyLevel cl, WriteType writeType, int requiredAcks, int receivedAcks, int nbRetry) {
@@ -72,18 +55,9 @@
7255 }
7356
7457 /**
75- * Defines whether to retry and at which consistency level on an
76- * unavailable exception.
77- *
78- * @param statement the original query for which the consistency level cannot
79- * be achieved.
80- * @param cl the original consistency level for the operation.
81- * @param requiredReplica the number of replica that should have been
82- * (known) alive for the operation to be attempted.
83- * @param aliveReplica the number of replica that were know to be alive by
84- * the coordinator of the operation.
85- * @param nbRetry the number of retry already performed for this operation.
86- * @return {@code RetryDecision.rethrow()}.
58+ * {@inheritDoc}
59+ * <p/>
60+ * This implementation always returns {@code RetryDecision.rethrow()}.
8761 */
8862 @Override
8963 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
@@ -27,9 +27,14 @@
2727 /**
2828 * A retry policy that wraps another policy, logging the decision made by its sub-policy.
2929 * <p/>
30- * Note that this policy only log the IGNORE and RETRY decisions (since
31- * RETHROW decisions are just meant to propagate the cassandra exception). The
32- * logging is done at the INFO level.
30+ * Note that this policy only logs
31+ * {@link com.datastax.driver.core.policies.RetryPolicy.RetryDecision.Type#RETRY RETRY} and
32+ * {@link com.datastax.driver.core.policies.RetryPolicy.RetryDecision.Type#IGNORE IGNORE} decisions (since
33+ * {@link com.datastax.driver.core.policies.RetryPolicy.RetryDecision.Type#RETHROW RETHROW} decisions
34+ * are just meant to propagate the Cassandra exception).
35+ * <p/>
36+ * The logging is done at the INFO level and the logger name is
37+ * {@code com.datastax.driver.core.policies.LoggingRetryPolicy}.
3338 */
3439 public class LoggingRetryPolicy implements RetryPolicy {
3540
modified driver-core/src/main/java/com/datastax/driver/core/policies/RetryPolicy.java +43 −9
@@ -74,7 +74,7 @@
7474 * {@code IGNORE} or a {@code RETHROW} decision;
7575 * for a {@code RETRY} decision, the consistency level can be {@code null},
7676 * in which case the retry is done at the same consistency level
77- * as in the original attempt.
77+ * as in the previous attempt.
7878 *
7979 * @return the consistency level for a retry decision.
8080 */
@@ -105,8 +105,16 @@
105105 /**
106106 * Creates a {@link RetryDecision.Type#RETRY} retry decision using
107107 * the same host and the provided consistency level.
108+ * <p/>
109+ * If the provided consistency level is {@code null}, the retry will be done at the same consistency level as
110+ * the previous attempt.
111+ * <p/>
112+ * Beware that {@link ConsistencyLevel#isSerial() serial} consistency levels
113+ * should never be passed to this method; attempting to do so would trigger an
114+ * {@link com.datastax.driver.core.exceptions.InvalidQueryException InvalidQueryException}.
108115 *
109- * @param consistency the consistency level to use for the retry.
116+ * @param consistency the consistency level to use for the retry; if {@code null},
117+ * the same level as the previous attempt will be used.
110118 * @return a {@link RetryDecision.Type#RETRY} decision using
111119 * the same host and the provided consistency level
112120 */
@@ -126,19 +134,30 @@
126134 /**
127135 * Creates a {@link RetryDecision.Type#RETRY} retry decision using the next host
128136 * in the query plan, and using the provided consistency level.
137+ * <p/>
138+ * If the provided consistency level is {@code null}, the retry will be done at the same consistency level as
139+ * the previous attempt.
140+ * <p/>
141+ * Beware that {@link ConsistencyLevel#isSerial() serial} consistency levels
142+ * should never be passed to this method; attempting to do so would trigger an
143+ * {@link com.datastax.driver.core.exceptions.InvalidQueryException InvalidQueryException}.
129144 *
145+ * @param consistency the consistency level to use for the retry; if {@code null},
146+ * the same level as the previous attempt will be used.
130147 * @return a {@link RetryDecision.Type#RETRY} retry decision using the next host
131148 * in the query plan, and using the provided consistency level.
132149 */
133- public static RetryDecision tryNextHost(ConsistencyLevel retryCL) {
134- return new RetryDecision(Type.RETRY, retryCL, false);
150+ public static RetryDecision tryNextHost(ConsistencyLevel consistency) {
151+ return new RetryDecision(Type.RETRY, consistency, false);
135152 }
136153
137154 @Override
138155 public String toString() {
139156 switch (type) {
140157 case RETRY:
141- return "Retry at " + retryCL + " on " + (retryCurrent ? "same " : "next ") + "host.";
158+ String retryClDesc = (retryCL == null) ? "same CL" : retryCL.toString();
159+ String hostDesc = retryCurrent ? "same" : "next";
160+ return "Retry at " + retryClDesc + " on " + hostDesc + " host.";
142161 case RETHROW:
143162 return "Rethrow";
144163 case IGNORE:
@@ -157,7 +176,9 @@
157176 * {@link com.datastax.driver.core.exceptions.ReadTimeoutException#wasDataRetrieved}).
158177 *
159178 * @param statement the original query that timed out.
160- * @param cl the original consistency level of the read that timed out.
179+ * @param cl the requested consistency level of the read that timed out.
180+ * Note that this can never be a {@link ConsistencyLevel#isSerial() serial}
181+ * consistency level.
161182 * @param requiredResponses the number of responses that were required to
162183 * achieve the requested consistency level.
163184 * @param receivedResponses the number of responses that had been received
@@ -175,7 +196,13 @@
175196 * Defines whether to retry and at which consistency level on a write timeout.
176197 *
177198 * @param statement the original query that timed out.
178- * @param cl the original consistency level of the write that timed out.
199+ * @param cl the requested consistency level of the write that timed out.
200+ * If the timeout occurred at the "paxos" phase of a
201+ * <a href="https://docs.datastax.com/en/cassandra/2.1/cassandra/dml/dml_ltwt_transaction_c.html">Lightweight transaction</a>,
202+ * then {@code cl} will actually be the requested {@link ConsistencyLevel#isSerial() serial} consistency level.
203+ * <em>Beware that serial consistency levels should never be passed to a {@link RetryDecision RetryDecision} as this would
204+ * invariably trigger an {@link com.datastax.driver.core.exceptions.InvalidQueryException InvalidQueryException}</em>.
205+ * Also, when {@code cl} is {@link ConsistencyLevel#isSerial() serial}, then {@code writeType} is always {@link WriteType#CAS CAS}.
179206 * @param writeType the type of the write that timed out.
180207 * @param requiredAcks the number of acknowledgments that were required to
181208 * achieve the requested consistency level.
@@ -194,7 +221,12 @@
194221 *
195222 * @param statement the original query for which the consistency level cannot
196223 * be achieved.
197- * @param cl the original consistency level for the operation.
224+ * @param cl the requested consistency level for the operation.
225+ * If the operation failed at the "paxos" phase of a
226+ * <a href="https://docs.datastax.com/en/cassandra/2.1/cassandra/dml/dml_ltwt_transaction_c.html">Lightweight transaction</a>,
227+ * then {@code cl} will actually be the requested {@link ConsistencyLevel#isSerial() serial} consistency level.
228+ * <em>Beware that serial consistency levels should never be passed to a {@link RetryDecision RetryDecision} as this would
229+ * invariably trigger an {@link com.datastax.driver.core.exceptions.InvalidQueryException InvalidQueryException}</em>.
198230 * @param requiredReplica the number of replica that should have been
199231 * (known) alive for the operation to be attempted.
200232 * @param aliveReplica the number of replica that were know to be alive by
@@ -224,7 +256,9 @@
224256 * is known to be idempotent.
225257 *
226258 * @param statement the original query that failed.
227- * @param cl the original consistency level for the operation.
259+ * @param cl the requested consistency level for the operation.
260+ * Note that this is not necessarily the achieved consistency level (if any),
261+ * and it is never a {@link ConsistencyLevel#isSerial() serial} one.
228262 * @param e the exception that caused this request to fail.
229263 * @param nbRetry the number of retries already performed for this operation.
230264 * @return the retry decision. If {@code RetryDecision.RETHROW} is returned,
modified driver-core/src/test/java/com/datastax/driver/core/DataProviders.java +1 −1
@@ -33,7 +33,7 @@
3333 @Override
3434 public boolean apply(ConsistencyLevel input) {
3535 // filter out serial CLs.
36- return !input.equals(ConsistencyLevel.SERIAL) && !input.equals(ConsistencyLevel.LOCAL_SERIAL);
36+ return !input.isSerial();
3737 }
3838 }).iterator();
3939
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+}