kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | dbbfbb772ad42fe3a69481354bb7c35dfc7bbc4825502c1b47bbce35905d571a |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2016-02-10 15:44 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-03-07 16:57 +01:00 |
| Parents | bb1e66449200 |
9 files changed
modified changelog/README.md +1 −0
| @@ -33,6 +33,7 @@ | ||
| 33 | 33 | - [new feature] JAVA-1019: SchemaBuilder support for CREATE/ALTER/DROP KEYSPACE. |
| 34 | 34 | - [bug] JAVA-1070: The Mapper should not prepare queries synchronously. |
| 35 | 35 | - [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. | |
| 36 | 37 | |
| 37 | 38 | Merged from 2.0 branch: |
| 38 | 39 |
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +2 −1
| @@ -382,7 +382,8 @@ | ||
| 382 | 382 | |
| 383 | 383 | private void retry(final boolean retryCurrent, ConsistencyLevel newConsistencyLevel) { |
| 384 | 384 | final Host h = current; |
| 385 | - this.retryConsistencyLevel = newConsistencyLevel; | |
| 385 | + if (newConsistencyLevel != null) | |
| 386 | + this.retryConsistencyLevel = newConsistencyLevel; | |
| 386 | 387 | |
| 387 | 388 | // We should not retry on the current thread as this will be an IO thread. |
| 388 | 389 | manager.executor().execute(new Runnable() { |
modified driver-core/src/main/java/com/datastax/driver/core/policies/DefaultRetryPolicy.java +16 −43
| @@ -42,9 +42,9 @@ | ||
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | /** |
| 45 | - * Defines whether to retry and at which consistency level on a read timeout. | |
| 45 | + * {@inheritDoc} | |
| 46 | 46 | * <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 | |
| 48 | 48 | * replicas had responded to the read request but data was not retrieved |
| 49 | 49 | * amongst those. Indeed, that case usually means that enough replica |
| 50 | 50 | * are alive to satisfy the consistency but the coordinator picked a |
| @@ -53,15 +53,6 @@ | ||
| 53 | 53 | * timeout the dead replica will likely have been detected as dead and |
| 54 | 54 | * the retry has a high chance of success. |
| 55 | 55 | * |
| 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. | |
| 65 | 56 | * @return {@code RetryDecision.retry(cl)} if no retry attempt has yet been tried and |
| 66 | 57 | * {@code receivedResponses >= requiredResponses && !dataRetrieved}, {@code RetryDecision.rethrow()} otherwise. |
| 67 | 58 | */ |
| @@ -74,9 +65,9 @@ | ||
| 74 | 65 | } |
| 75 | 66 | |
| 76 | 67 | /** |
| 77 | - * Defines whether to retry and at which consistency level on a write timeout. | |
| 68 | + * {@inheritDoc} | |
| 78 | 69 | * <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 | |
| 80 | 71 | * a {@code WriteType.BATCH_LOG} write. The reasoning for the retry in |
| 81 | 72 | * that case is that write to the distributed batch log is tried by the |
| 82 | 73 | * coordinator of the write against a small subset of all the nodes alive |
| @@ -86,14 +77,6 @@ | ||
| 86 | 77 | * nodes will likely have been detected as dead and the retry has thus a |
| 87 | 78 | * high chance of success. |
| 88 | 79 | * |
| 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. | |
| 97 | 80 | * @return {@code RetryDecision.retry(cl)} if no retry attempt has yet been tried and |
| 98 | 81 | * {@code writeType == WriteType.BATCH_LOG}, {@code RetryDecision.rethrow()} otherwise. |
| 99 | 82 | */ |
| @@ -103,37 +86,27 @@ | ||
| 103 | 86 | return RetryDecision.rethrow(); |
| 104 | 87 | |
| 105 | 88 | // 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. | |
| 106 | 90 | return writeType == WriteType.BATCH_LOG ? RetryDecision.retry(cl) : RetryDecision.rethrow(); |
| 107 | 91 | } |
| 108 | 92 | |
| 109 | 93 | /** |
| 110 | - * Defines whether to retry and at which consistency level on an | |
| 111 | - * unavailable exception. | |
| 94 | + * {@inheritDoc} | |
| 112 | 95 | * <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> | |
| 132 | 105 | */ |
| 133 | 106 | @Override |
| 134 | 107 | public RetryDecision onUnavailable(Statement statement, ConsistencyLevel cl, int requiredReplica, int aliveReplica, int nbRetry) { |
| 135 | 108 | return (nbRetry == 0) |
| 136 | - ? RetryDecision.tryNextHost(cl) | |
| 109 | + ? RetryDecision.tryNextHost(null) | |
| 137 | 110 | : RetryDecision.rethrow(); |
| 138 | 111 | } |
| 139 | 112 |
modified driver-core/src/main/java/com/datastax/driver/core/policies/DowngradingConsistencyRetryPolicy.java +11 −38
| @@ -85,25 +85,14 @@ | ||
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | /** |
| 88 | - * Defines whether to retry and at which consistency level on a read timeout. | |
| 88 | + * {@inheritDoc} | |
| 89 | 89 | * <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 | |
| 91 | 91 | * responded than required by the consistency level (but at least one |
| 92 | 92 | * replica did respond), the operation is retried at a lower |
| 93 | 93 | * consistency level. If enough replica responded but data was not |
| 94 | 94 | * retrieve, the operation is retried with the initial consistency |
| 95 | 95 | * 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. | |
| 107 | 96 | */ |
| 108 | 97 | @Override |
| 109 | 98 | public RetryDecision onReadTimeout(Statement statement, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataRetrieved, int nbRetry) { |
| @@ -126,9 +115,9 @@ | ||
| 126 | 115 | } |
| 127 | 116 | |
| 128 | 117 | /** |
| 129 | - * Defines whether to retry and at which consistency level on a write timeout. | |
| 118 | + * {@inheritDoc} | |
| 130 | 119 | * <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 == | |
| 132 | 121 | * WriteType.BATCH_LOG}, the write is retried with the initial |
| 133 | 122 | * consistency level. If {@code writeType == WriteType.UNLOGGED_BATCH} |
| 134 | 123 | * and at least one replica acknowledged, the write is retried with a |
| @@ -137,16 +126,6 @@ | ||
| 137 | 126 | * all, even if {@code receivedAcks > 0}). For other write types ({@code WriteType.SIMPLE} |
| 138 | 127 | * and {@code WriteType.BATCH}), if we know the write has been persisted on at |
| 139 | 128 | * 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. | |
| 150 | 129 | */ |
| 151 | 130 | @Override |
| 152 | 131 | public RetryDecision onWriteTimeout(Statement statement, ConsistencyLevel cl, WriteType writeType, int requiredAcks, int receivedAcks, int nbRetry) { |
| @@ -170,28 +149,22 @@ | ||
| 170 | 149 | } |
| 171 | 150 | |
| 172 | 151 | /** |
| 173 | - * Defines whether to retry and at which consistency level on an | |
| 174 | - * unavailable exception. | |
| 152 | + * {@inheritDoc} | |
| 175 | 153 | * <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 | |
| 177 | 155 | * is know to be alive, the operation is retried at a lower consistency |
| 178 | 156 | * 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. | |
| 189 | 157 | */ |
| 190 | 158 | @Override |
| 191 | 159 | public RetryDecision onUnavailable(Statement statement, ConsistencyLevel cl, int requiredReplica, int aliveReplica, int nbRetry) { |
| 192 | 160 | if (nbRetry != 0) |
| 193 | 161 | return RetryDecision.rethrow(); |
| 194 | 162 | |
| 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 | + | |
| 195 | 168 | // Tries the biggest CL that is expected to work |
| 196 | 169 | return maxLikelyToWorkCL(aliveReplica); |
| 197 | 170 | } |
modified driver-core/src/main/java/com/datastax/driver/core/policies/ExtendedRetryPolicy.java +3 −1
| @@ -49,7 +49,9 @@ | ||
| 49 | 49 | * is known to be idempotent. |
| 50 | 50 | * |
| 51 | 51 | * @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. | |
| 53 | 55 | * @param e the exception that caused this request to fail. |
| 54 | 56 | * @param nbRetry the number of retries already performed for this operation. |
| 55 | 57 | * @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 @@ | ||
| 20 | 20 | import com.datastax.driver.core.WriteType; |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | - * A retry policy that never retry (nor ignore). | |
| 23 | + * A retry policy that never retries (nor ignores). | |
| 24 | 24 | * <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. | |
| 27 | 27 | */ |
| 28 | 28 | public class FallthroughRetryPolicy implements ExtendedRetryPolicy { |
| 29 | 29 | |
| @@ -33,18 +33,9 @@ | ||
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | /** |
| 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()}. | |
| 48 | 39 | */ |
| 49 | 40 | @Override |
| 50 | 41 | public RetryDecision onReadTimeout(Statement statement, ConsistencyLevel cl, int requiredResponses, int receivedResponses, boolean dataRetrieved, int nbRetry) { |
| @@ -52,17 +43,9 @@ | ||
| 52 | 43 | } |
| 53 | 44 | |
| 54 | 45 | /** |
| 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()}. | |
| 66 | 49 | */ |
| 67 | 50 | @Override |
| 68 | 51 | public RetryDecision onWriteTimeout(Statement statement, ConsistencyLevel cl, WriteType writeType, int requiredAcks, int receivedAcks, int nbRetry) { |
| @@ -70,18 +53,9 @@ | ||
| 70 | 53 | } |
| 71 | 54 | |
| 72 | 55 | /** |
| 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()}. | |
| 85 | 59 | */ |
| 86 | 60 | @Override |
| 87 | 61 | 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 @@ | ||
| 25 | 25 | /** |
| 26 | 26 | * A retry policy that wraps another policy, logging the decision made by its sub-policy. |
| 27 | 27 | * <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}. | |
| 31 | 36 | */ |
| 32 | 37 | public class LoggingRetryPolicy implements ExtendedRetryPolicy { |
| 33 | 38 |
modified driver-core/src/main/java/com/datastax/driver/core/policies/RetryPolicy.java +40 −8
| @@ -75,7 +75,7 @@ | ||
| 75 | 75 | * {@code IGNORE} or a {@code RETHROW} decision; |
| 76 | 76 | * for a {@code RETRY} decision, the consistency level can be {@code null}, |
| 77 | 77 | * in which case the retry is done at the same consistency level |
| 78 | - * as in the original attempt. | |
| 78 | + * as in the previous attempt. | |
| 79 | 79 | * |
| 80 | 80 | * @return the consistency level for a retry decision. |
| 81 | 81 | */ |
| @@ -106,8 +106,16 @@ | ||
| 106 | 106 | /** |
| 107 | 107 | * Creates a {@link RetryDecision.Type#RETRY} retry decision using |
| 108 | 108 | * 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}. | |
| 109 | 116 | * |
| 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. | |
| 111 | 119 | * @return a {@link RetryDecision.Type#RETRY} decision using |
| 112 | 120 | * the same host and the provided consistency level |
| 113 | 121 | */ |
| @@ -127,19 +135,30 @@ | ||
| 127 | 135 | /** |
| 128 | 136 | * Creates a {@link RetryDecision.Type#RETRY} retry decision using the next host |
| 129 | 137 | * 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}. | |
| 130 | 145 | * |
| 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. | |
| 131 | 148 | * @return a {@link RetryDecision.Type#RETRY} retry decision using the next host |
| 132 | 149 | * in the query plan, and using the provided consistency level. |
| 133 | 150 | */ |
| 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); | |
| 136 | 153 | } |
| 137 | 154 | |
| 138 | 155 | @Override |
| 139 | 156 | public String toString() { |
| 140 | 157 | switch (type) { |
| 141 | 158 | 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."; | |
| 143 | 162 | case RETHROW: |
| 144 | 163 | return "Rethrow"; |
| 145 | 164 | case IGNORE: |
| @@ -158,7 +177,9 @@ | ||
| 158 | 177 | * {@link com.datastax.driver.core.exceptions.ReadTimeoutException#wasDataRetrieved}). |
| 159 | 178 | * |
| 160 | 179 | * @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. | |
| 162 | 183 | * @param requiredResponses the number of responses that were required to |
| 163 | 184 | * achieve the requested consistency level. |
| 164 | 185 | * @param receivedResponses the number of responses that had been received |
| @@ -176,7 +197,13 @@ | ||
| 176 | 197 | * Defines whether to retry and at which consistency level on a write timeout. |
| 177 | 198 | * |
| 178 | 199 | * @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}. | |
| 180 | 207 | * @param writeType the type of the write that timed out. |
| 181 | 208 | * @param requiredAcks the number of acknowledgments that were required to |
| 182 | 209 | * achieve the requested consistency level. |
| @@ -195,7 +222,12 @@ | ||
| 195 | 222 | * |
| 196 | 223 | * @param statement the original query for which the consistency level cannot |
| 197 | 224 | * 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>. | |
| 199 | 231 | * @param requiredReplica the number of replica that should have been |
| 200 | 232 | * (known) alive for the operation to be attempted. |
| 201 | 233 | * @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 | +} |