kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 09fb9cb331782678d45b726a2baa118b51665ea10d4435a0fc8fa3b6adf1079a |
|---|---|
| Author | Alexandre Dutra <alexdut@gmail.com> · 2015-06-30 09:24 +02:00 |
| Committer | Alexandre Dutra <alexdut@gmail.com> · 2015-06-30 09:24 +02:00 |
| Parents | 454582a51f31, ec16eac7e022 |
10 files changed
modified changelog/README.md +1 −0
| @@ -21,6 +21,7 @@ | ||
| 21 | 21 | - [improvement] Optimize internal copies of Request objects (JAVA-726) |
| 22 | 22 | - [bug] Preserve tracing across retries (JAVA-815) |
| 23 | 23 | - [improvement] New RetryDecision.tryNextHost() (JAVA-709) |
| 24 | +- [bug] Handle function calls and raw strings as non-idempotent in QueryBuilder (JAVA-733) | |
| 24 | 25 | |
| 25 | 26 | Merged from 2.0.10_fixes branch: |
| 26 | 27 |
modified driver-core/src/main/java/com/datastax/driver/core/Statement.java +4 −3
| @@ -386,9 +386,10 @@ | ||
| 386 | 386 | * <p> |
| 387 | 387 | * By default, this method returns {@code null} for all statements, except for |
| 388 | 388 | * {@link BuiltStatement}s, where the value will be inferred from the query: if it updates |
| 389 | - * counters or prepends/appends to a list, the result will be {@code false}, otherwise it | |
| 390 | - * will be {@code true}. In all cases, calling {@link #setIdempotent(boolean)} forces a | |
| 391 | - * value that overrides every other mechanism. | |
| 389 | + * counters, prepends/appends to a list, or uses a function call or | |
| 390 | + * {@link com.datastax.driver.core.querybuilder.QueryBuilder#raw(String)} anywhere in an inserted value, | |
| 391 | + * the result will be {@code false}; otherwise it will be {@code true}. | |
| 392 | + * In all cases, calling {@link #setIdempotent(boolean)} forces a value that overrides every other mechanism. | |
| 392 | 393 | * |
| 393 | 394 | * @return whether this statement is idempotent, or {@code null} to use |
| 394 | 395 | * {@link QueryOptions#getDefaultIdempotence()}. |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Assignment.java +1 −1
| @@ -54,7 +54,7 @@ | ||
| 54 | 54 | |
| 55 | 55 | @Override |
| 56 | 56 | boolean isIdempotent() { |
| 57 | - return true; | |
| 57 | + return Utils.isIdempotent(value); | |
| 58 | 58 | } |
| 59 | 59 | } |
| 60 | 60 |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Insert.java +7 −2
| @@ -80,6 +80,8 @@ | ||
| 80 | 80 | names.add(name); |
| 81 | 81 | values.add(value); |
| 82 | 82 | checkForBindMarkers(value); |
| 83 | + if (!hasNonIdempotentOps() && !Utils.isIdempotent(value)) | |
| 84 | + this.setNonIdempotentOps(); | |
| 83 | 85 | maybeAddRoutingKey(name, value); |
| 84 | 86 | return this; |
| 85 | 87 | } |
| @@ -114,8 +116,11 @@ | ||
| 114 | 116 | this.names.addAll(names); |
| 115 | 117 | this.values.addAll(values); |
| 116 | 118 | for (int i = 0; i < names.size(); i++) { |
| 117 | - checkForBindMarkers(values.get(i)); | |
| 118 | - maybeAddRoutingKey(names.get(i), values.get(i)); | |
| 119 | + Object value = values.get(i); | |
| 120 | + checkForBindMarkers(value); | |
| 121 | + maybeAddRoutingKey(names.get(i), value); | |
| 122 | + if (!hasNonIdempotentOps() && !Utils.isIdempotent(value)) | |
| 123 | + this.setNonIdempotentOps(); | |
| 119 | 124 | } |
| 120 | 125 | return this; |
| 121 | 126 | } |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +18 −0
| @@ -935,6 +935,24 @@ | ||
| 935 | 935 | return new Utils.FCall(name, parameters); |
| 936 | 936 | } |
| 937 | 937 | |
| 938 | + /** | |
| 939 | + * Creates a {@code now()} function call. | |
| 940 | + * | |
| 941 | + * @return the function call. | |
| 942 | + */ | |
| 943 | + public static Object now() { | |
| 944 | + return new Utils.FCall("now"); | |
| 945 | + } | |
| 946 | + | |
| 947 | + /** | |
| 948 | + * Creates a {@code uuid()} function call. | |
| 949 | + * | |
| 950 | + * @return the function call. | |
| 951 | + */ | |
| 952 | + public static Object uuid() { | |
| 953 | + return new Utils.FCall("uuid"); | |
| 954 | + } | |
| 955 | + | |
| 938 | 956 | /** |
| 939 | 957 | * Declares that the name in argument should be treated as a column name. |
| 940 | 958 | * <p> |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Update.java +1 −1
| @@ -175,7 +175,7 @@ | ||
| 175 | 175 | */ |
| 176 | 176 | public Assignments and(Assignment assignment) { |
| 177 | 177 | statement.setCounterOp(assignment instanceof CounterAssignment); |
| 178 | - if (!assignment.isIdempotent()) | |
| 178 | + if (!hasNonIdempotentOps() && !Utils.isIdempotent(assignment)) | |
| 179 | 179 | statement.setNonIdempotentOps(); |
| 180 | 180 | assignments.add(assignment); |
| 181 | 181 | checkForBindMarkers(assignment); |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Utils.java +30 −1
| @@ -22,6 +22,8 @@ | ||
| 22 | 22 | import java.util.*; |
| 23 | 23 | import java.util.regex.Pattern; |
| 24 | 24 | |
| 25 | +import static com.google.common.base.Preconditions.checkNotNull; | |
| 26 | + | |
| 25 | 27 | import com.datastax.driver.core.DataType; |
| 26 | 28 | import com.datastax.driver.core.utils.Bytes; |
| 27 | 29 | |
| @@ -30,7 +32,6 @@ | ||
| 30 | 32 | |
| 31 | 33 | private static final Pattern cnamePattern = Pattern.compile("\\w+(?:\\[.+\\])?"); |
| 32 | 34 | |
| 33 | - | |
| 34 | 35 | static StringBuilder joinAndAppend(StringBuilder sb, String separator, List<? extends Appendeable> values, List<ByteBuffer> variables) { |
| 35 | 36 | for (int i = 0; i < values.size(); i++) { |
| 36 | 37 | if (i > 0) |
| @@ -247,6 +248,31 @@ | ||
| 247 | 248 | return false; |
| 248 | 249 | } |
| 249 | 250 | |
| 251 | + static boolean isIdempotent(Object value) { | |
| 252 | + if(value == null) { | |
| 253 | + return true; | |
| 254 | + } else if (value instanceof Assignment) { | |
| 255 | + Assignment assignment = (Assignment)value; | |
| 256 | + return assignment.isIdempotent(); | |
| 257 | + } else if (value instanceof FCall) { | |
| 258 | + return false; | |
| 259 | + } else if (value instanceof RawString) { | |
| 260 | + return false; | |
| 261 | + } else if(value instanceof Collection) { | |
| 262 | + for (Object elt : ((Collection)value)) { | |
| 263 | + if(!isIdempotent(elt)) | |
| 264 | + return false; | |
| 265 | + } | |
| 266 | + return true; | |
| 267 | + } else if (value instanceof Map) { | |
| 268 | + for (Map.Entry<?, ?> entry : ((Map<?, ?>)value).entrySet()) { | |
| 269 | + if (!isIdempotent(entry.getKey()) || !isIdempotent(entry.getValue())) | |
| 270 | + return false; | |
| 271 | + } | |
| 272 | + } | |
| 273 | + return true; | |
| 274 | + } | |
| 275 | + | |
| 250 | 276 | private static StringBuilder appendMap(Map<?, ?> m, StringBuilder sb, boolean rawValue) { |
| 251 | 277 | sb.append('{'); |
| 252 | 278 | boolean first = true; |
| @@ -363,10 +389,12 @@ | ||
| 363 | 389 | } |
| 364 | 390 | |
| 365 | 391 | static class FCall { |
| 392 | + | |
| 366 | 393 | private final String name; |
| 367 | 394 | private final Object[] parameters; |
| 368 | 395 | |
| 369 | 396 | FCall(String name, Object... parameters) { |
| 397 | + checkNotNull(name); | |
| 370 | 398 | this.name = name; |
| 371 | 399 | this.parameters = parameters; |
| 372 | 400 | } |
| @@ -383,6 +411,7 @@ | ||
| 383 | 411 | sb.append(')'); |
| 384 | 412 | return sb.toString(); |
| 385 | 413 | } |
| 414 | + | |
| 386 | 415 | } |
| 387 | 416 | |
| 388 | 417 | static class CName { |
modified driver-core/src/test/java/com/datastax/driver/core/StatementIdempotenceTest.java +56 −3
| @@ -16,6 +16,9 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import com.google.common.collect.ImmutableList; |
| 19 | +import com.google.common.collect.ImmutableMap; | |
| 20 | +import com.google.common.collect.Lists; | |
| 21 | +import com.google.common.collect.Sets; | |
| 19 | 22 | import org.testng.annotations.Test; |
| 20 | 23 | |
| 21 | 24 | import static org.assertj.core.api.Assertions.assertThat; |
| @@ -89,15 +92,65 @@ | ||
| 89 | 92 | return ImmutableList.<BuiltStatement>of( |
| 90 | 93 | update("foo").with(set("v", 1)).where(eq("k", 1)), // set simple value |
| 91 | 94 | update("foo").with(add("s", 1)).where(eq("k", 1)), // add to set |
| 92 | - update("foo").with(put("m", "a", 1)).where(eq("k", 1)) // put in map | |
| 95 | + update("foo").with(put("m", "a", 1)).where(eq("k", 1)), // put in map | |
| 96 | + | |
| 97 | + // select statements should be idempotent even with function calls | |
| 98 | + select().countAll() .from("foo").where(eq("k", 1)), | |
| 99 | + select().ttl("v") .from("foo").where(eq("k", 1)), | |
| 100 | + select().writeTime("v") .from("foo").where(eq("k", 1)), | |
| 101 | + select().fcall("token", "k").from("foo").where(eq("k", 1)) | |
| 102 | + | |
| 93 | 103 | ); |
| 94 | 104 | } |
| 95 | 105 | |
| 96 | 106 | private static ImmutableList<BuiltStatement> nonIdempotentBuiltStatements() { |
| 97 | - return ImmutableList.<BuiltStatement>of( | |
| 107 | + return ImmutableList.of( | |
| 98 | 108 | update("foo").with(append("l", 1)).where(eq("k", 1)), // append to list |
| 99 | 109 | update("foo").with(set("v", 1)).and(prepend("l", 1)).where(eq("k", 1)), // prepend to list |
| 100 | - update("foo").with(incr("c")).where(eq("k", 1)) // counter update | |
| 110 | + update("foo").with(incr("c")).where(eq("k", 1)), // counter update | |
| 111 | + | |
| 112 | + // function calls | |
| 113 | + | |
| 114 | + insertInto("foo").value("k", 1).value("v", fcall("token", "k")), | |
| 115 | + insertInto("foo").value("k", 1).value("v", now()), | |
| 116 | + insertInto("foo").value("k", 1).value("v", uuid()), | |
| 117 | + | |
| 118 | + insertInto("foo").value("k", 1).value("v", Sets.newHashSet(fcall("token", "k"))), | |
| 119 | + insertInto("foo").value("k", 1).value("v", Sets.newHashSet(now())), | |
| 120 | + insertInto("foo").value("k", 1).value("v", Sets.newHashSet(uuid())), | |
| 121 | + | |
| 122 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, fcall("token", "k")}), | |
| 123 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, now()}), | |
| 124 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, uuid()}), | |
| 125 | + | |
| 126 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of("foo", fcall("token", "k"))}), | |
| 127 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of("foo", now())}), | |
| 128 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of("foo", uuid())}), | |
| 129 | + | |
| 130 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of(fcall("token", "k"), "foo")}), | |
| 131 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of(now() , "foo")}), | |
| 132 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of(uuid() , "foo")}), | |
| 133 | + | |
| 134 | + update("foo").with(set("v", fcall("token", "k"))) .where(eq("k", 1)), | |
| 135 | + update("foo").with(set("v", now())) .where(eq("k", 1)), | |
| 136 | + update("foo").with(set("v", uuid())) .where(eq("k", 1)), | |
| 137 | + | |
| 138 | + update("foo").with(set("v", Lists.newArrayList(fcall("token", "k")))).where(eq("k", 1)), | |
| 139 | + update("foo").with(set("v", Lists.newArrayList(now()))) .where(eq("k", 1)), | |
| 140 | + update("foo").with(set("v", Lists.newArrayList(uuid()))) .where(eq("k", 1)), | |
| 141 | + | |
| 142 | + // raw() calls | |
| 143 | + | |
| 144 | + insertInto("foo").value("k", 1).value("v", raw("foo()")), | |
| 145 | + insertInto("foo").value("k", 1).value("v", Sets.newHashSet(raw("foo()"))), | |
| 146 | + | |
| 147 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, raw("foo()")}), | |
| 148 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of("foo", raw("foo()"))}), | |
| 149 | + insertInto("foo").values(new String[]{"k", "v"}, new Object[]{1, ImmutableMap.of(raw("foo()"), "foo")}), | |
| 150 | + | |
| 151 | + update("foo").with(set("v", raw("foo()"))) .where(eq("k", 1)), | |
| 152 | + update("foo").with(set("v", Lists.newArrayList(raw("foo()")))).where(eq("k", 1)) | |
| 153 | + | |
| 101 | 154 | ); |
| 102 | 155 | } |
| 103 | 156 | } |
modified features/speculative_execution/README.md +8 −6
| @@ -78,12 +78,14 @@ | ||
| 78 | 78 | about what the query actually does. Therefore: |
| 79 | 79 | |
| 80 | 80 | * **`Statement#isIdempotent()` is only computed automatically for |
| 81 | - statements built with [QueryBuilder][QueryBuilder]**. Note that there | |
| 82 | - is a known issue in 2.0.10 where calls to `now()` or `uuid()` are not | |
| 83 | - taken into account (see | |
| 84 | - [JAVA-733](https://datastax-oss.atlassian.net/browse/JAVA-733)). If | |
| 85 | - you're using these CQL functions with built statements, force | |
| 86 | - idempotence to `false` manually (see below); | |
| 81 | + statements built with [QueryBuilder][QueryBuilder]**. | |
| 82 | + Note that the driver takes a rather conservative approach with uses | |
| 83 | + of `fcall()` or `raw()`: whenever they appear in a value to be | |
| 84 | + inserted in the database (like the values of an `Insert` or the | |
| 85 | + right-hand side of an assignment in an `Update`), the statement | |
| 86 | + will be considered non-idempotent by default. If you know that your | |
| 87 | + CQL functions or expressions are safe, force idempotence to `true` | |
| 88 | + on the statement manually (see below); | |
| 87 | 89 | * **for all other types of statements, it defaults to `false`.** You'll |
| 88 | 90 | need to set it manually, with one of the mechanism described below. |
| 89 | 91 |
modified upgrade_guide/README.md +4 −0
| @@ -13,6 +13,10 @@ | ||
| 13 | 13 | because of a network partition) but can still answer to the client normally. |
| 14 | 14 | In this case, trying another node has a chance of success. |
| 15 | 15 | The previous behaviour was to always throw an exception. |
| 16 | +2. A `BuiltStatement` is now considered non-idempotent whenever a `fcall()` | |
| 17 | + or `raw()` is used to build a value to be inserted in the database. | |
| 18 | + If you know that the CQL functions or expressions are safe, use | |
| 19 | + `setIdempotent(true)` on the statement. | |
| 16 | 20 | |
| 17 | 21 | ### 2.0.x to 2.0.10 |
| 18 | 22 |