Elffuss BraidHub

kikocis/java-driver public

Merge branch '2.1' into 3.0.x
Changeset3cd7eb6efc56929771aa6178a27686857d1eafdd9e051a7148f4f4dd018b58b6
Authorolim7t <omichallat+github@gmail.com> · 2016-05-11 16:24 -07:00
Committerolim7t <omichallat+github@gmail.com> · 2016-05-11 16:24 -07:00
Parents4b35bd0a42ce, cbe117e45877

Browse the files at this changeset

9 files changed

modified changelog/README.md +1 −0
@@ -704,6 +704,7 @@
704704
705705 - [bug] JAVA-1179: Request objects should be copied when executed.
706706 - [improvement] JAVA-1182: Throw error when synchronous call made on I/O thread.
707+- [bug] JAVA-1184: Unwrap StatementWrappers when extracting column definitions.
707708
708709
709710 ### 2.0.12.1
modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedResultSet.java +3 −0
@@ -62,6 +62,9 @@
6262
6363 ColumnDefinitions columnDefs;
6464 if (r.metadata.columns == null) {
65+ if (statement instanceof StatementWrapper) {
66+ statement = ((StatementWrapper) statement).getWrappedStatement();
67+ }
6568 assert statement instanceof BoundStatement;
6669 columnDefs = ((BoundStatement) statement).statement.getPreparedId().resultSetMetadata;
6770 assert columnDefs != null;
modified driver-core/src/main/java/com/datastax/driver/core/BatchStatement.java +3 −0
@@ -134,6 +134,9 @@
134134 * @throws IllegalArgumentException if adding a regular statement that uses named values.
135135 */
136136 public BatchStatement add(Statement statement) {
137+ if (statement instanceof StatementWrapper) {
138+ statement = ((StatementWrapper) statement).getWrappedStatement();
139+ }
137140 if ((statement instanceof RegularStatement) && ((RegularStatement) statement).usesNamedValues()) {
138141 throw new IllegalArgumentException("Batch statement cannot contain regular statements with named values ("
139142 + ((RegularStatement) statement).getQueryString() + ")");
modified driver-core/src/main/java/com/datastax/driver/core/DefaultResultSetFuture.java +1 −1
@@ -184,7 +184,7 @@
184184 setException(new DriverInternalError(String.format("Got unexpected %s response from %s", response.type, connection.address)));
185185 break;
186186 }
187- } catch (RuntimeException e) {
187+ } catch (Throwable e) {
188188 // If we get a bug here, the client will not get it, so better forwarding the error
189189 setException(new DriverInternalError("Unexpected error while processing response from " + connection.address, e));
190190 }
modified driver-core/src/main/java/com/datastax/driver/core/QueryLogger.java +3 −3
@@ -627,6 +627,9 @@
627627 if (cluster == null)
628628 throw new IllegalStateException("This method should only be called after the logger has been registered with a cluster");
629629
630+ if (statement instanceof StatementWrapper)
631+ statement = ((StatementWrapper) statement).getWrappedStatement();
632+
630633 long latencyMs = NANOSECONDS.toMillis(newLatencyNanos);
631634 if (exception == null) {
632635 maybeLogNormalOrSlowQuery(host, statement, latencyMs);
@@ -849,9 +852,6 @@
849852 }
850853
851854 protected int append(Statement statement, StringBuilder buffer, int remaining) {
852- if (statement instanceof StatementWrapper)
853- statement = ((StatementWrapper) statement).getWrappedStatement();
854-
855855 if (statement instanceof RegularStatement) {
856856 RegularStatement rs = (RegularStatement) statement;
857857 String query = rs.getQueryString();
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +3 −2
@@ -667,9 +667,10 @@
667667
668668 @Override
669669 protected void checkNotInEventLoop() {
670- if (!CHECK_IO_DEADLOCKS)
670+ Connection.Factory connectionFactory = cluster.manager.connectionFactory;
671+ if (!CHECK_IO_DEADLOCKS || connectionFactory == null)
671672 return;
672- for (EventExecutor executor : cluster.manager.connectionFactory.eventLoopGroup) {
673+ for (EventExecutor executor : connectionFactory.eventLoopGroup) {
673674 if (executor.inEventLoop()) {
674675 throw new IllegalStateException(
675676 "Detected a synchronous Session call (execute() or prepare()) on an I/O thread, " +
modified driver-core/src/test/java/com/datastax/driver/core/AsyncQueryTest.java +30 −14
@@ -20,6 +20,8 @@
2020 import com.google.common.base.Throwables;
2121 import com.google.common.collect.Lists;
2222 import com.google.common.util.concurrent.*;
23+import org.slf4j.Logger;
24+import org.slf4j.LoggerFactory;
2325 import org.testng.annotations.DataProvider;
2426 import org.testng.annotations.Test;
2527
@@ -33,6 +35,8 @@
3335
3436 public class AsyncQueryTest extends CCMTestsSupport {
3537
38+ Logger logger = LoggerFactory.getLogger(AsyncQueryTest.class);
39+
3640 @DataProvider(name = "keyspace")
3741 public static Object[][] keyspace() {
3842 return new Object[][]{{"asyncquerytest"}, {"\"AsyncQueryTest\""}};
@@ -123,22 +127,34 @@
123127
124128 @Test(groups = "short")
125129 public void should_fail_when_synchronous_call_on_io_thread() throws Exception {
126- ResultSetFuture f = session().executeAsync("select release_version from system.local");
127- ListenableFuture<Void> f2 = Futures.transform(f, new Function<ResultSet, Void>() {
128- @Override
129- public Void apply(ResultSet input) {
130- session().execute("select release_version from system.local");
131- return null;
130+ final Thread sameThread = Thread.currentThread();
131+ for (int i = 0; i < 1000; i++) {
132+ ResultSetFuture f = session().executeAsync("select release_version from system.local");
133+ ListenableFuture<Thread> f2 = Futures.transform(f, new Function<ResultSet, Thread>() {
134+ @Override
135+ public Thread apply(ResultSet input) {
136+ session().execute("select release_version from system.local");
137+ return Thread.currentThread();
138+ }
139+ });
140+ try {
141+ Thread executedThread = f2.get();
142+ if(executedThread != sameThread) {
143+ fail("Expected a failed future, callback was executed on " + executedThread);
144+ } else {
145+ // Callback was invoked on the same thread, which indicates that the future completed
146+ // before the transform callback was registered. Try again to produce case where callback
147+ // is called on io thread.
148+ logger.warn("Future completed before transform callback registered, will try again.");
149+ }
150+ } catch (Exception e) {
151+ assertThat(Throwables.getRootCause(e))
152+ .isInstanceOf(IllegalStateException.class)
153+ .hasMessageContaining("Detected a synchronous Session call");
154+ return;
132155 }
133- });
134- try {
135- f2.get();
136- fail("Expected a failed future");
137- } catch (Exception e) {
138- assertThat(Throwables.getRootCause(e))
139- .isInstanceOf(IllegalStateException.class)
140- .hasMessageContaining("Detected a synchronous Session call");
141156 }
157+ fail("callback was not executed on io thread in 1000 attempts, something may be wrong.");
142158 }
143159
144160 private ListenableFuture<Integer> connectAndQuery(String keyspace, Executor executor) {
modified driver-core/src/test/java/com/datastax/driver/core/QueryLoggerTest.java +28 −0
@@ -15,6 +15,7 @@
1515 */
1616 package com.datastax.driver.core;
1717
18+import com.datastax.driver.core.StatementWrapperTest.CustomStatement;
1819 import com.datastax.driver.core.exceptions.DriverException;
1920 import com.datastax.driver.core.utils.CassandraVersion;
2021 import com.google.common.base.Function;
@@ -1036,6 +1037,33 @@
10361037 .contains("42");
10371038 }
10381039
1040+ @CassandraVersion(major=2.0)
1041+ @Test(groups = "short")
1042+ public void should_log_wrapped_bound_statement() throws Exception {
1043+ // given
1044+ normal.setLevel(TRACE);
1045+ queryLogger = QueryLogger.builder(cluster())
1046+ .withConstantThreshold(Long.MAX_VALUE)
1047+ .withMaxQueryStringLength(Integer.MAX_VALUE)
1048+ .build();
1049+ cluster().register(queryLogger);
1050+ // when
1051+ String query = "UPDATE test SET c_text = :param1 WHERE pk = :param2";
1052+ PreparedStatement ps = session().prepare(query);
1053+ BoundStatement bs = ps.bind();
1054+ bs.setString("param1", "foo");
1055+ bs.setInt("param2", 42);
1056+ session().execute(new CustomStatement(bs));
1057+ // then
1058+ String line = normalAppender.waitAndGet(10000);
1059+ assertThat(line)
1060+ .contains("Query completed normally")
1061+ .contains(ipOfNode(1))
1062+ .contains(query)
1063+ .contains("param2:42")
1064+ .contains("param1:'foo'");
1065+ }
1066+
10391067 @Override
10401068 public void onTestContextInitialized() {
10411069 execute("CREATE TABLE test (pk int PRIMARY KEY, " + definitions + ")");
modified driver-core/src/test/java/com/datastax/driver/core/StatementWrapperTest.java +59 −0
@@ -17,6 +17,7 @@
1717
1818 import com.datastax.driver.core.exceptions.DriverException;
1919 import com.datastax.driver.core.policies.*;
20+import com.datastax.driver.core.utils.CassandraVersion;
2021 import org.testng.annotations.Test;
2122
2223 import java.util.Iterator;
@@ -26,10 +27,18 @@
2627
2728 public class StatementWrapperTest extends CCMTestsSupport {
2829
30+ private static final String INSERT_QUERY = "insert into test (k, v) values (?, ?)";
31+ private static final String SELECT_QUERY = "select * from test where k = ?";
32+
2933 CustomLoadBalancingPolicy loadBalancingPolicy = new CustomLoadBalancingPolicy();
3034 CustomSpeculativeExecutionPolicy speculativeExecutionPolicy = new CustomSpeculativeExecutionPolicy();
3135 CustomRetryPolicy retryPolicy = new CustomRetryPolicy();
3236
37+ @Override
38+ public void onTestContextInitialized() {
39+ execute("create table test (k text primary key, v int)");
40+ }
41+
3342 @Override
3443 public Cluster.Builder createClusterBuilder() {
3544 return Cluster.builder()
@@ -78,6 +87,56 @@
7887 assertThat(retryPolicy.customStatementsHandled.get()).isEqualTo(1);
7988 }
8089
90+ @CassandraVersion(major = 2.0)
91+ @Test(groups = "short")
92+ public void should_execute_wrapped_simple_statement() {
93+ session().execute(new CustomStatement(new SimpleStatement(INSERT_QUERY, "key_simple", 1)));
94+
95+ ResultSet rs = session().execute(new CustomStatement(new SimpleStatement(SELECT_QUERY, "key_simple")));
96+ assertThat(rs.one().getInt("v")).isEqualTo(1);
97+ }
98+
99+ @Test(groups = "short")
100+ public void should_execute_wrapped_bound_statement() {
101+ PreparedStatement preparedStatement = session().prepare(new SimpleStatement(INSERT_QUERY));
102+ session().execute(new CustomStatement(preparedStatement.bind("key_bound", 1)));
103+
104+ preparedStatement = session().prepare(new SimpleStatement(SELECT_QUERY));
105+ ResultSet rs = session().execute(new CustomStatement(preparedStatement.bind("key_bound")));
106+ assertThat(rs.one().getInt("v")).isEqualTo(1);
107+ }
108+
109+ @CassandraVersion(major = 2.0)
110+ @Test(groups = "short")
111+ public void should_execute_wrapped_batch_statement() {
112+ BatchStatement batchStatement = new BatchStatement();
113+ batchStatement.add(new SimpleStatement(INSERT_QUERY, "key_batch", 1));
114+
115+ session().execute(new CustomStatement(batchStatement));
116+
117+ ResultSet rs = session().execute(SELECT_QUERY, "key_batch");
118+ assertThat(rs.one().getInt("v")).isEqualTo(1);
119+ }
120+
121+ @CassandraVersion(major = 2.0)
122+ @Test(groups = "short")
123+ public void should_add_wrapped_batch_statement_to_batch_statement() {
124+ BatchStatement batchStatementForWrapping = new BatchStatement();
125+ batchStatementForWrapping.add(new SimpleStatement(INSERT_QUERY, "key1", 1));
126+
127+ BatchStatement batchStatement = new BatchStatement();
128+ batchStatement.add(new CustomStatement(new SimpleStatement(INSERT_QUERY, "key2", 2)));
129+ batchStatement.add(new CustomStatement(batchStatementForWrapping));
130+
131+ session().execute(batchStatement);
132+
133+ ResultSet rs = session().execute(SELECT_QUERY, "key1");
134+ assertThat(rs.one().getInt("v")).isEqualTo(1);
135+
136+ rs = session().execute(SELECT_QUERY, "key2");
137+ assertThat(rs.one().getInt("v")).isEqualTo(2);
138+ }
139+
81140 /**
82141 * A custom wrapper that's just used to mark statements.
83142 */