Elffuss BraidHub

kikocis/java-driver public

JAVA-733: Handle function calls and raw strings as non-idempotent in QueryBuilder.
Changesetec16eac7e0227998284029d1604bbfd2fd8f63f589ef43e5b4db5b3012781685
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-06-12 19:01 +02:00
CommitterAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-06-30 08:53 +02:00
Parents454582a51f31

Browse the files at this changeset

10 files changed

modified changelog/README.md +1 −0
@@ -21,6 +21,7 @@
2121 - [improvement] Optimize internal copies of Request objects (JAVA-726)
2222 - [bug] Preserve tracing across retries (JAVA-815)
2323 - [improvement] New RetryDecision.tryNextHost() (JAVA-709)
24+- [bug] Handle function calls and raw strings as non-idempotent in QueryBuilder (JAVA-733)
2425
2526 Merged from 2.0.10_fixes branch:
2627
modified driver-core/src/main/java/com/datastax/driver/core/Statement.java +4 −3
@@ -386,9 +386,10 @@
386386 * <p>
387387 * By default, this method returns {@code null} for all statements, except for
388388 * {@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.
392393 *
393394 * @return whether this statement is idempotent, or {@code null} to use
394395 * {@link QueryOptions#getDefaultIdempotence()}.
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Assignment.java +1 −1
@@ -54,7 +54,7 @@
5454
5555 @Override
5656 boolean isIdempotent() {
57- return true;
57+ return Utils.isIdempotent(value);
5858 }
5959 }
6060
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Insert.java +7 −2
@@ -80,6 +80,8 @@
8080 names.add(name);
8181 values.add(value);
8282 checkForBindMarkers(value);
83+ if (!hasNonIdempotentOps() && !Utils.isIdempotent(value))
84+ this.setNonIdempotentOps();
8385 maybeAddRoutingKey(name, value);
8486 return this;
8587 }
@@ -114,8 +116,11 @@
114116 this.names.addAll(names);
115117 this.values.addAll(values);
116118 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();
119124 }
120125 return this;
121126 }
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/QueryBuilder.java +18 −0
@@ -935,6 +935,24 @@
935935 return new Utils.FCall(name, parameters);
936936 }
937937
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+
938956 /**
939957 * Declares that the name in argument should be treated as a column name.
940958 * <p>
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Update.java +1 −1
@@ -175,7 +175,7 @@
175175 */
176176 public Assignments and(Assignment assignment) {
177177 statement.setCounterOp(assignment instanceof CounterAssignment);
178- if (!assignment.isIdempotent())
178+ if (!hasNonIdempotentOps() && !Utils.isIdempotent(assignment))
179179 statement.setNonIdempotentOps();
180180 assignments.add(assignment);
181181 checkForBindMarkers(assignment);
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Utils.java +30 −1
@@ -22,6 +22,8 @@
2222 import java.util.*;
2323 import java.util.regex.Pattern;
2424
25+import static com.google.common.base.Preconditions.checkNotNull;
26+
2527 import com.datastax.driver.core.DataType;
2628 import com.datastax.driver.core.utils.Bytes;
2729
@@ -30,7 +32,6 @@
3032
3133 private static final Pattern cnamePattern = Pattern.compile("\\w+(?:\\[.+\\])?");
3234
33-
3435 static StringBuilder joinAndAppend(StringBuilder sb, String separator, List<? extends Appendeable> values, List<ByteBuffer> variables) {
3536 for (int i = 0; i < values.size(); i++) {
3637 if (i > 0)
@@ -247,6 +248,31 @@
247248 return false;
248249 }
249250
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+
250276 private static StringBuilder appendMap(Map<?, ?> m, StringBuilder sb, boolean rawValue) {
251277 sb.append('{');
252278 boolean first = true;
@@ -363,10 +389,12 @@
363389 }
364390
365391 static class FCall {
392+
366393 private final String name;
367394 private final Object[] parameters;
368395
369396 FCall(String name, Object... parameters) {
397+ checkNotNull(name);
370398 this.name = name;
371399 this.parameters = parameters;
372400 }
@@ -383,6 +411,7 @@
383411 sb.append(')');
384412 return sb.toString();
385413 }
414+
386415 }
387416
388417 static class CName {
modified driver-core/src/test/java/com/datastax/driver/core/StatementIdempotenceTest.java +56 −3
@@ -16,6 +16,9 @@
1616 package com.datastax.driver.core;
1717
1818 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;
1922 import org.testng.annotations.Test;
2023
2124 import static org.assertj.core.api.Assertions.assertThat;
@@ -89,15 +92,65 @@
8992 return ImmutableList.<BuiltStatement>of(
9093 update("foo").with(set("v", 1)).where(eq("k", 1)), // set simple value
9194 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+
93103 );
94104 }
95105
96106 private static ImmutableList<BuiltStatement> nonIdempotentBuiltStatements() {
97- return ImmutableList.<BuiltStatement>of(
107+ return ImmutableList.of(
98108 update("foo").with(append("l", 1)).where(eq("k", 1)), // append to list
99109 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+
101154 );
102155 }
103156 }
modified features/speculative_execution/README.md +8 −6
@@ -78,12 +78,14 @@
7878 about what the query actually does. Therefore:
7979
8080 * **`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);
8789 * **for all other types of statements, it defaults to `false`.** You'll
8890 need to set it manually, with one of the mechanism described below.
8991
modified upgrade_guide/README.md +4 −0
@@ -13,6 +13,10 @@
1313 because of a network partition) but can still answer to the client normally.
1414 In this case, trying another node has a chance of success.
1515 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.
1620
1721 ### 2.0.x to 2.0.10
1822