kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 3782f3c2461de22dda659fd862110410929eee9664ab7eff7e4a0faedbd809f7 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-08-30 15:47 -07:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-08-30 16:02 -07:00 |
| Parents | a0c416644d7e, b53119758b6e |
7 files changed
modified changelog/README.md +1 −0
| @@ -5,6 +5,7 @@ | ||
| 5 | 5 | - [improvement] JAVA-1246: Driver swallows the real exception in a few cases |
| 6 | 6 | - [improvement] JAVA-1261: Throw error when attempting to page in I/O thread. |
| 7 | 7 | - [bug] JAVA-1258: Regression: Mapper cannot map a materialized view after JAVA-1126. |
| 8 | +- [bug] JAVA-1101: Batch and BatchStatement should consider inner statements to determine query idempotence | |
| 8 | 9 | |
| 9 | 10 | |
| 10 | 11 | ### 3.0.3 |
modified driver-core/src/main/java/com/datastax/driver/core/BatchStatement.java +8 −0
| @@ -243,6 +243,14 @@ | ||
| 243 | 243 | return null; |
| 244 | 244 | } |
| 245 | 245 | |
| 246 | + @Override | |
| 247 | + public Boolean isIdempotent() { | |
| 248 | + if (idempotent != null) { | |
| 249 | + return idempotent; | |
| 250 | + } | |
| 251 | + return isBatchIdempotent(statements); | |
| 252 | + } | |
| 253 | + | |
| 246 | 254 | void ensureAllSet() { |
| 247 | 255 | for (Statement statement : statements) |
| 248 | 256 | if (statement instanceof BoundStatement) |
modified driver-core/src/main/java/com/datastax/driver/core/Statement.java +31 −5
| @@ -23,6 +23,7 @@ | ||
| 23 | 23 | import com.google.common.collect.ImmutableMap; |
| 24 | 24 | |
| 25 | 25 | import java.nio.ByteBuffer; |
| 26 | +import java.util.Collection; | |
| 26 | 27 | import java.util.Map; |
| 27 | 28 | |
| 28 | 29 | /** |
| @@ -490,19 +491,31 @@ | ||
| 490 | 491 | * Whether this statement is idempotent, i.e. whether it can be applied multiple times |
| 491 | 492 | * without changing the result beyond the initial application. |
| 492 | 493 | * <p/> |
| 493 | - * Idempotence plays a role in {@link com.datastax.driver.core.policies.SpeculativeExecutionPolicy speculative executions}. | |
| 494 | - * If a statement is <em>not idempotent</em>, the driver will not schedule speculative | |
| 494 | + * Idempotence plays a role in {@link RetryPolicy retries} and | |
| 495 | + * {@link com.datastax.driver.core.policies.SpeculativeExecutionPolicy speculative executions}. | |
| 496 | + * If a statement is <em>not idempotent</em>, the driver will not schedule retries or speculative | |
| 495 | 497 | * executions for it. |
| 496 | 498 | * <p/> |
| 497 | 499 | * Note that this method can return {@code null}, in which case the driver will default to |
| 498 | 500 | * {@link QueryOptions#getDefaultIdempotence()}. |
| 499 | 501 | * <p/> |
| 500 | 502 | * By default, this method returns {@code null} for all statements, except for |
| 501 | - * {@link BuiltStatement}s, where the value will be inferred from the query: if it updates | |
| 502 | - * counters, prepends/appends to a list, or uses a function call or | |
| 503 | + * <ul> | |
| 504 | + * <li>{@link BuiltStatement} - value will be inferred from the query: if it updates counters, | |
| 505 | + * prepends/appends to a list, or uses a function call or | |
| 503 | 506 | * {@link com.datastax.driver.core.querybuilder.QueryBuilder#raw(String)} anywhere in an inserted value, |
| 504 | 507 | * the result will be {@code false}; otherwise it will be {@code true}. |
| 505 | - * In all cases, calling {@link #setIdempotent(boolean)} forces a value that overrides every other mechanism. | |
| 508 | + * </li> | |
| 509 | + * <li> | |
| 510 | + * {@link com.datastax.driver.core.querybuilder.Batch} and {@link BatchStatement}: | |
| 511 | + * <ol> | |
| 512 | + * <li>If any statement in batch has isIdempotent() false - return false</li> | |
| 513 | + * <li>If no statements with isIdempotent() false, but some have isIdempotent() null - return null</li> | |
| 514 | + * <li>Otherwise - return true</li> | |
| 515 | + * </ol> | |
| 516 | + * </li> | |
| 517 | + * </ul> | |
| 518 | + * In all cases, calling {@link #setIdempotent(boolean)} forces a value that overrides calculated value. | |
| 506 | 519 | * <p/> |
| 507 | 520 | * Note that when a statement is prepared ({@link Session#prepare(String)}), its idempotence flag will be propagated |
| 508 | 521 | * to all {@link PreparedStatement}s created from it. |
| @@ -566,4 +579,17 @@ | ||
| 566 | 579 | this.outgoingPayload = payload == null ? null : ImmutableMap.copyOf(payload); |
| 567 | 580 | return this; |
| 568 | 581 | } |
| 582 | + | |
| 583 | + protected static Boolean isBatchIdempotent(Collection<? extends Statement> statements) { | |
| 584 | + boolean hasNullIdempotentStatements = false; | |
| 585 | + for (Statement statement : statements) { | |
| 586 | + Boolean innerIdempotent = statement.isIdempotent(); | |
| 587 | + if (innerIdempotent == null) { | |
| 588 | + hasNullIdempotentStatements = true; | |
| 589 | + } else if (!innerIdempotent) { | |
| 590 | + return false; | |
| 591 | + } | |
| 592 | + } | |
| 593 | + return (hasNullIdempotentStatements) ? null : true; | |
| 594 | + } | |
| 569 | 595 | } |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Batch.java +9 −4
| @@ -15,10 +15,7 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.querybuilder; |
| 17 | 17 | |
| 18 | -import com.datastax.driver.core.CodecRegistry; | |
| 19 | -import com.datastax.driver.core.ProtocolVersion; | |
| 20 | -import com.datastax.driver.core.RegularStatement; | |
| 21 | -import com.datastax.driver.core.SimpleStatement; | |
| 18 | +import com.datastax.driver.core.*; | |
| 22 | 19 | |
| 23 | 20 | import java.nio.ByteBuffer; |
| 24 | 21 | import java.util.ArrayList; |
| @@ -175,6 +172,14 @@ | ||
| 175 | 172 | return statements.isEmpty() ? null : statements.get(0).getKeyspace(); |
| 176 | 173 | } |
| 177 | 174 | |
| 175 | + @Override | |
| 176 | + public Boolean isIdempotent() { | |
| 177 | + if (idempotent != null) { | |
| 178 | + return idempotent; | |
| 179 | + } | |
| 180 | + return isBatchIdempotent(statements); | |
| 181 | + } | |
| 182 | + | |
| 178 | 183 | /** |
| 179 | 184 | * The options of a BATCH statement. |
| 180 | 185 | */ |
added driver-core/src/test/java/com/datastax/driver/core/AbstractBatchIdempotencyTest.java +177 −0
| @@ -0,0 +1,177 @@ | ||
| 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; | |
| 17 | + | |
| 18 | +import org.testng.annotations.Test; | |
| 19 | + | |
| 20 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 21 | + | |
| 22 | +public abstract class AbstractBatchIdempotencyTest { | |
| 23 | + | |
| 24 | + protected abstract TestBatch createBatch(); | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * Unify Batch and BatchStatement to avoid duplicating all tests | |
| 28 | + */ | |
| 29 | + protected interface TestBatch { | |
| 30 | + //Batch only accepts RegularStatement, so we use it for common interface | |
| 31 | + void add(RegularStatement statement); | |
| 32 | + | |
| 33 | + Boolean isIdempotent(); | |
| 34 | + | |
| 35 | + void setIdempotent(boolean idempotent); | |
| 36 | + } | |
| 37 | + | |
| 38 | + @Test(groups = "unit") | |
| 39 | + public void isIdempotent_should_return_true_if_no_statements_added() { | |
| 40 | + TestBatch batch = createBatch(); | |
| 41 | + assertThat(batch.isIdempotent()).isTrue(); | |
| 42 | + } | |
| 43 | + | |
| 44 | + @Test(groups = "unit") | |
| 45 | + public void isIdempotent_should_return_true_if_all_statements_are_idempotent() { | |
| 46 | + TestBatch batch = createBatch(); | |
| 47 | + assertThat(batch.isIdempotent()).isTrue(); | |
| 48 | + | |
| 49 | + batch.add(statementWithIdempotency(true)); | |
| 50 | + assertThat(batch.isIdempotent()).isTrue(); | |
| 51 | + | |
| 52 | + batch.add(statementWithIdempotency(true)); | |
| 53 | + assertThat(batch.isIdempotent()).isTrue(); | |
| 54 | + } | |
| 55 | + | |
| 56 | + @Test(groups = "unit") | |
| 57 | + public void isIdempotent_should_return_false_if_any_statements_is_nonidempotent() { | |
| 58 | + TestBatch batch = createBatch(); | |
| 59 | + assertThat(batch.isIdempotent()).isTrue(); | |
| 60 | + | |
| 61 | + batch.add(statementWithIdempotency(true)); | |
| 62 | + assertThat(batch.isIdempotent()).isTrue(); | |
| 63 | + | |
| 64 | + batch.add(statementWithIdempotency(false)); | |
| 65 | + assertThat(batch.isIdempotent()).isFalse(); | |
| 66 | + | |
| 67 | + batch.add(statementWithIdempotency(true)); | |
| 68 | + assertThat(batch.isIdempotent()).isFalse(); | |
| 69 | + } | |
| 70 | + | |
| 71 | + @Test(groups = "unit") | |
| 72 | + public void isIdempotent_should_return_null_if_no_nonidempotent_statements_and_some_are_nullidempotent() { | |
| 73 | + TestBatch batch = createBatch(); | |
| 74 | + assertThat(batch.isIdempotent()).isTrue(); | |
| 75 | + | |
| 76 | + batch.add(statementWithIdempotency(true)); | |
| 77 | + assertThat(batch.isIdempotent()).isTrue(); | |
| 78 | + | |
| 79 | + batch.add(statementWithIdempotency(null)); | |
| 80 | + assertThat(batch.isIdempotent()).isNull(); | |
| 81 | + | |
| 82 | + batch.add(statementWithIdempotency(true)); | |
| 83 | + assertThat(batch.isIdempotent()).isNull(); | |
| 84 | + } | |
| 85 | + | |
| 86 | + @Test(groups = "unit") | |
| 87 | + public void isIdempotent_should_return_false_if_both_nonidempotent_and_nullidempotent_statements_present() { | |
| 88 | + TestBatch batch = createBatch(); | |
| 89 | + assertThat(batch.isIdempotent()).isTrue(); | |
| 90 | + | |
| 91 | + batch.add(statementWithIdempotency(true)); | |
| 92 | + assertThat(batch.isIdempotent()).isTrue(); | |
| 93 | + | |
| 94 | + batch.add(statementWithIdempotency(null)); | |
| 95 | + assertThat(batch.isIdempotent()).isNull(); | |
| 96 | + | |
| 97 | + batch.add(statementWithIdempotency(false)); | |
| 98 | + assertThat(batch.isIdempotent()).isFalse(); | |
| 99 | + | |
| 100 | + batch.add(statementWithIdempotency(true)); | |
| 101 | + assertThat(batch.isIdempotent()).isFalse(); | |
| 102 | + | |
| 103 | + batch.add(statementWithIdempotency(null)); | |
| 104 | + assertThat(batch.isIdempotent()).isFalse(); | |
| 105 | + | |
| 106 | + batch.add(statementWithIdempotency(false)); | |
| 107 | + assertThat(batch.isIdempotent()).isFalse(); | |
| 108 | + } | |
| 109 | + | |
| 110 | + @Test(groups = "unit") | |
| 111 | + public void isIdempotent_should_return_override_flag_if_no_statements_added() { | |
| 112 | + TestBatch batch = createBatch(); | |
| 113 | + assertThat(batch.isIdempotent()).isTrue(); | |
| 114 | + | |
| 115 | + batch.setIdempotent(false); | |
| 116 | + assertThat(batch.isIdempotent()).isFalse(); | |
| 117 | + } | |
| 118 | + | |
| 119 | + @Test(groups = "unit") | |
| 120 | + public void isIdempotent_should_return_override_flag_if_calculated_idempotency_true() { | |
| 121 | + TestBatch batch = createBatch(); | |
| 122 | + assertThat(batch.isIdempotent()).isTrue(); | |
| 123 | + | |
| 124 | + batch.add(statementWithIdempotency(true)); | |
| 125 | + assertThat(batch.isIdempotent()).isTrue(); | |
| 126 | + | |
| 127 | + batch.setIdempotent(false); | |
| 128 | + assertThat(batch.isIdempotent()).isFalse(); | |
| 129 | + } | |
| 130 | + | |
| 131 | + @Test(groups = "unit") | |
| 132 | + public void isIdempotent_should_return_override_flag_if_calculated_idempotency_null() { | |
| 133 | + TestBatch batch = createBatch(); | |
| 134 | + assertThat(batch.isIdempotent()).isTrue(); | |
| 135 | + | |
| 136 | + batch.add(statementWithIdempotency(null)); | |
| 137 | + assertThat(batch.isIdempotent()).isNull(); | |
| 138 | + | |
| 139 | + batch.setIdempotent(false); | |
| 140 | + assertThat(batch.isIdempotent()).isFalse(); | |
| 141 | + } | |
| 142 | + | |
| 143 | + @Test(groups = "unit") | |
| 144 | + public void isIdempotent_should_return_override_flag_if_calculated_idempotency_false() { | |
| 145 | + TestBatch batch = createBatch(); | |
| 146 | + assertThat(batch.isIdempotent()).isTrue(); | |
| 147 | + | |
| 148 | + batch.add(statementWithIdempotency(false)); | |
| 149 | + assertThat(batch.isIdempotent()).isFalse(); | |
| 150 | + | |
| 151 | + batch.setIdempotent(true); | |
| 152 | + assertThat(batch.isIdempotent()).isTrue(); | |
| 153 | + } | |
| 154 | + | |
| 155 | + @Test(groups = "unit") | |
| 156 | + public void isIdempotent_should_return_override_flag_if_calculated_idempotency_equals_override_value() { | |
| 157 | + TestBatch batch = createBatch(); | |
| 158 | + assertThat(batch.isIdempotent()).isTrue(); | |
| 159 | + | |
| 160 | + batch.add(statementWithIdempotency(false)); | |
| 161 | + assertThat(batch.isIdempotent()).isFalse(); | |
| 162 | + | |
| 163 | + batch.setIdempotent(false); | |
| 164 | + assertThat(batch.isIdempotent()).isFalse(); | |
| 165 | + } | |
| 166 | + | |
| 167 | + private RegularStatement statementWithIdempotency(Boolean idempotency) { | |
| 168 | + RegularStatement statement = new SimpleStatement("fake statement"); | |
| 169 | + if (idempotency != null) { | |
| 170 | + statement.setIdempotent(idempotency); | |
| 171 | + assertThat(statement.isIdempotent()).isEqualTo(idempotency); | |
| 172 | + } else { | |
| 173 | + assertThat(statement.isIdempotent()).isNull(); | |
| 174 | + } | |
| 175 | + return statement; | |
| 176 | + } | |
| 177 | +} |
added driver-core/src/test/java/com/datastax/driver/core/BatchStatementIdempotencyTest.java +44 −0
| @@ -0,0 +1,44 @@ | ||
| 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; | |
| 17 | + | |
| 18 | +public class BatchStatementIdempotencyTest extends AbstractBatchIdempotencyTest { | |
| 19 | + | |
| 20 | + @Override | |
| 21 | + protected TestBatch createBatch() { | |
| 22 | + return new TestBatchStatementWrapper(); | |
| 23 | + } | |
| 24 | + | |
| 25 | + static class TestBatchStatementWrapper implements TestBatch { | |
| 26 | + | |
| 27 | + private final BatchStatement batch = new BatchStatement(); | |
| 28 | + | |
| 29 | + @Override | |
| 30 | + public void add(RegularStatement statement) { | |
| 31 | + batch.add(statement); | |
| 32 | + } | |
| 33 | + | |
| 34 | + @Override | |
| 35 | + public Boolean isIdempotent() { | |
| 36 | + return batch.isIdempotent(); | |
| 37 | + } | |
| 38 | + | |
| 39 | + @Override | |
| 40 | + public void setIdempotent(boolean idempotent) { | |
| 41 | + batch.setIdempotent(idempotent); | |
| 42 | + } | |
| 43 | + } | |
| 44 | +} |
added driver-core/src/test/java/com/datastax/driver/core/querybuilder/BatchIdempotencyTest.java +47 −0
| @@ -0,0 +1,47 @@ | ||
| 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.querybuilder; | |
| 17 | + | |
| 18 | +import com.datastax.driver.core.AbstractBatchIdempotencyTest; | |
| 19 | +import com.datastax.driver.core.RegularStatement; | |
| 20 | + | |
| 21 | +public class BatchIdempotencyTest extends AbstractBatchIdempotencyTest { | |
| 22 | + | |
| 23 | + @Override | |
| 24 | + protected AbstractBatchIdempotencyTest.TestBatch createBatch() { | |
| 25 | + return new TestBatchWrapper(); | |
| 26 | + } | |
| 27 | + | |
| 28 | + static class TestBatchWrapper implements TestBatch { | |
| 29 | + | |
| 30 | + private final Batch batch = QueryBuilder.batch(); | |
| 31 | + | |
| 32 | + @Override | |
| 33 | + public void add(RegularStatement statement) { | |
| 34 | + batch.add(statement); | |
| 35 | + } | |
| 36 | + | |
| 37 | + @Override | |
| 38 | + public Boolean isIdempotent() { | |
| 39 | + return batch.isIdempotent(); | |
| 40 | + } | |
| 41 | + | |
| 42 | + @Override | |
| 43 | + public void setIdempotent(boolean idempotent) { | |
| 44 | + batch.setIdempotent(idempotent); | |
| 45 | + } | |
| 46 | + } | |
| 47 | +} |