Elffuss BraidHub

kikocis/java-driver public

Merge branch '2.0' into 2.1
Changesetcbe117e45877b5f3070c0155298d1df904e295801d82baee2e9c72f4d89b36fa
Authorolim7t <omichallat+github@gmail.com> · 2016-05-11 16:22 -07:00
Committerolim7t <omichallat+github@gmail.com> · 2016-05-11 16:22 -07:00
Parents876983ef4dbf, b364a0e42c7c

Browse the files at this changeset

9 files changed

modified changelog/README.md +1 −0
@@ -403,6 +403,7 @@
403403
404404 - [bug] JAVA-1179: Request objects should be copied when executed.
405405 - [improvement] JAVA-1182: Throw error when synchronous call made on I/O thread.
406+- [bug] JAVA-1184: Unwrap StatementWrappers when extracting column definitions.
406407
407408
408409 ### 2.0.12.1
modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedResultSet.java +3 −0
@@ -59,6 +59,9 @@
5959
6060 ColumnDefinitions columnDefs;
6161 if (r.metadata.columns == null) {
62+ if (statement instanceof StatementWrapper) {
63+ statement = ((StatementWrapper) statement).getWrappedStatement();
64+ }
6265 assert statement instanceof BoundStatement;
6366 columnDefs = ((BoundStatement) statement).statement.getPreparedId().resultSetMetadata;
6467 assert columnDefs != null;
modified driver-core/src/main/java/com/datastax/driver/core/BatchStatement.java +3 −0
@@ -127,6 +127,9 @@
127127 * of statements for a BatchStatement allowed by the underlying protocol).
128128 */
129129 public BatchStatement add(Statement statement) {
130+ if (statement instanceof StatementWrapper) {
131+ statement = ((StatementWrapper) statement).getWrappedStatement();
132+ }
130133
131134 // We handle BatchStatement here (rather than in getIdAndValues) as it make it slightly
132135 // easier to avoid endless loop if the use mistakenly pass a batch that depends on this
modified driver-core/src/main/java/com/datastax/driver/core/DefaultResultSetFuture.java +1 −1
@@ -156,7 +156,7 @@
156156 setException(new DriverInternalError(String.format("Got unexpected %s response from %s", response.type, connection.address)));
157157 break;
158158 }
159- } catch (RuntimeException e) {
159+ } catch (Throwable e) {
160160 // If we get a bug here, the client will not get it, so better forwarding the error
161161 setException(new DriverInternalError("Unexpected error while processing response from " + connection.address, e));
162162 }
modified driver-core/src/main/java/com/datastax/driver/core/QueryLogger.java +3 −3
@@ -618,6 +618,9 @@
618618 */
619619 @Override
620620 public void update(Host host, Statement statement, Exception exception, long newLatencyNanos) {
621+ if (statement instanceof StatementWrapper)
622+ statement = ((StatementWrapper) statement).getWrappedStatement();
623+
621624 long latencyMs = NANOSECONDS.toMillis(newLatencyNanos);
622625 if (exception == null) {
623626 maybeLogNormalOrSlowQuery(host, statement, latencyMs);
@@ -827,9 +830,6 @@
827830 }
828831
829832 protected int append(Statement statement, StringBuilder buffer, int remaining) {
830- if (statement instanceof StatementWrapper)
831- statement = ((StatementWrapper) statement).getWrappedStatement();
832-
833833 if (statement instanceof RegularStatement) {
834834 remaining = append(((RegularStatement) statement).getQueryString().trim(), buffer, remaining);
835835 } else if (statement instanceof BoundStatement) {
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +3 −2
@@ -645,9 +645,10 @@
645645
646646 @Override
647647 protected void checkNotInEventLoop() {
648- if (!CHECK_IO_DEADLOCKS)
648+ Connection.Factory connectionFactory = cluster.manager.connectionFactory;
649+ if (!CHECK_IO_DEADLOCKS || connectionFactory == null)
649650 return;
650- for (EventExecutor executor : cluster.manager.connectionFactory.eventLoopGroup) {
651+ for (EventExecutor executor : connectionFactory.eventLoopGroup) {
651652 if (executor.inEventLoop()) {
652653 throw new IllegalStateException(
653654 "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\""}};
@@ -129,22 +133,34 @@
129133
130134 @Test(groups = "short")
131135 public void should_fail_when_synchronous_call_on_io_thread() throws Exception {
132- ResultSetFuture f = session().executeAsync("select release_version from system.local");
133- ListenableFuture<Void> f2 = Futures.transform(f, new Function<ResultSet, Void>() {
134- @Override
135- public Void apply(ResultSet input) {
136- session().execute("select release_version from system.local");
137- return null;
136+ final Thread sameThread = Thread.currentThread();
137+ for (int i = 0; i < 1000; i++) {
138+ ResultSetFuture f = session().executeAsync("select release_version from system.local");
139+ ListenableFuture<Thread> f2 = Futures.transform(f, new Function<ResultSet, Thread>() {
140+ @Override
141+ public Thread apply(ResultSet input) {
142+ session().execute("select release_version from system.local");
143+ return Thread.currentThread();
144+ }
145+ });
146+ try {
147+ Thread executedThread = f2.get();
148+ if(executedThread != sameThread) {
149+ fail("Expected a failed future, callback was executed on " + executedThread);
150+ } else {
151+ // Callback was invoked on the same thread, which indicates that the future completed
152+ // before the transform callback was registered. Try again to produce case where callback
153+ // is called on io thread.
154+ logger.warn("Future completed before transform callback registered, will try again.");
155+ }
156+ } catch (Exception e) {
157+ assertThat(Throwables.getRootCause(e))
158+ .isInstanceOf(IllegalStateException.class)
159+ .hasMessageContaining("Detected a synchronous Session call");
160+ return;
138161 }
139- });
140- try {
141- f2.get();
142- fail("Expected a failed future");
143- } catch (Exception e) {
144- assertThat(Throwables.getRootCause(e))
145- .isInstanceOf(IllegalStateException.class)
146- .hasMessageContaining("Detected a synchronous Session call");
147162 }
163+ fail("callback was not executed on io thread in 1000 attempts, something may be wrong.");
148164 }
149165
150166 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;
@@ -949,6 +950,33 @@
949950 .contains("42");
950951 }
951952
953+ @CassandraVersion(major=2.0)
954+ @Test(groups = "short")
955+ public void should_log_wrapped_bound_statement() throws Exception {
956+ // given
957+ normal.setLevel(TRACE);
958+ queryLogger = QueryLogger.builder(cluster())
959+ .withConstantThreshold(Long.MAX_VALUE)
960+ .withMaxQueryStringLength(Integer.MAX_VALUE)
961+ .build();
962+ cluster().register(queryLogger);
963+ // when
964+ String query = "UPDATE test SET c_text = :param1 WHERE pk = :param2";
965+ PreparedStatement ps = session().prepare(query);
966+ BoundStatement bs = ps.bind();
967+ bs.setString("param1", "foo");
968+ bs.setInt("param2", 42);
969+ session().execute(new CustomStatement(bs));
970+ // then
971+ String line = normalAppender.waitAndGet(10000);
972+ assertThat(line)
973+ .contains("Query completed normally")
974+ .contains(ipOfNode(1))
975+ .contains(query)
976+ .contains("param2:42")
977+ .contains("param1:'foo'");
978+ }
979+
952980 @Override
953981 public void onTestContextInitialized() {
954982 execute("CREATE TABLE test (pk int PRIMARY KEY, " + definitions + ")");
modified driver-core/src/test/java/com/datastax/driver/core/StatementWrapperTest.java +59 −0
@@ -16,6 +16,7 @@
1616 package com.datastax.driver.core;
1717
1818 import com.datastax.driver.core.policies.*;
19+import com.datastax.driver.core.utils.CassandraVersion;
1920 import org.testng.annotations.Test;
2021
2122 import java.util.Iterator;
@@ -25,10 +26,18 @@
2526
2627 public class StatementWrapperTest extends CCMTestsSupport {
2728
29+ private static final String INSERT_QUERY = "insert into test (k, v) values (?, ?)";
30+ private static final String SELECT_QUERY = "select * from test where k = ?";
31+
2832 CustomLoadBalancingPolicy loadBalancingPolicy = new CustomLoadBalancingPolicy();
2933 CustomSpeculativeExecutionPolicy speculativeExecutionPolicy = new CustomSpeculativeExecutionPolicy();
3034 CustomRetryPolicy retryPolicy = new CustomRetryPolicy();
3135
36+ @Override
37+ public void onTestContextInitialized() {
38+ execute("create table test (k text primary key, v int)");
39+ }
40+
3241 @Override
3342 public Cluster.Builder createClusterBuilder() {
3443 return Cluster.builder()
@@ -77,6 +86,56 @@
7786 assertThat(retryPolicy.customStatementsHandled.get()).isEqualTo(1);
7887 }
7988
89+ @CassandraVersion(major = 2.0)
90+ @Test(groups = "short")
91+ public void should_execute_wrapped_simple_statement() {
92+ session().execute(new CustomStatement(new SimpleStatement(INSERT_QUERY, "key_simple", 1)));
93+
94+ ResultSet rs = session().execute(new CustomStatement(new SimpleStatement(SELECT_QUERY, "key_simple")));
95+ assertThat(rs.one().getInt("v")).isEqualTo(1);
96+ }
97+
98+ @Test(groups = "short")
99+ public void should_execute_wrapped_bound_statement() {
100+ PreparedStatement preparedStatement = session().prepare(new SimpleStatement(INSERT_QUERY));
101+ session().execute(new CustomStatement(preparedStatement.bind("key_bound", 1)));
102+
103+ preparedStatement = session().prepare(new SimpleStatement(SELECT_QUERY));
104+ ResultSet rs = session().execute(new CustomStatement(preparedStatement.bind("key_bound")));
105+ assertThat(rs.one().getInt("v")).isEqualTo(1);
106+ }
107+
108+ @CassandraVersion(major = 2.0)
109+ @Test(groups = "short")
110+ public void should_execute_wrapped_batch_statement() {
111+ BatchStatement batchStatement = new BatchStatement();
112+ batchStatement.add(new SimpleStatement(INSERT_QUERY, "key_batch", 1));
113+
114+ session().execute(new CustomStatement(batchStatement));
115+
116+ ResultSet rs = session().execute(SELECT_QUERY, "key_batch");
117+ assertThat(rs.one().getInt("v")).isEqualTo(1);
118+ }
119+
120+ @CassandraVersion(major = 2.0)
121+ @Test(groups = "short")
122+ public void should_add_wrapped_batch_statement_to_batch_statement() {
123+ BatchStatement batchStatementForWrapping = new BatchStatement();
124+ batchStatementForWrapping.add(new SimpleStatement(INSERT_QUERY, "key1", 1));
125+
126+ BatchStatement batchStatement = new BatchStatement();
127+ batchStatement.add(new CustomStatement(new SimpleStatement(INSERT_QUERY, "key2", 2)));
128+ batchStatement.add(new CustomStatement(batchStatementForWrapping));
129+
130+ session().execute(batchStatement);
131+
132+ ResultSet rs = session().execute(SELECT_QUERY, "key1");
133+ assertThat(rs.one().getInt("v")).isEqualTo(1);
134+
135+ rs = session().execute(SELECT_QUERY, "key2");
136+ assertThat(rs.one().getInt("v")).isEqualTo(2);
137+ }
138+
80139 /**
81140 * A custom wrapper that's just used to mark statements.
82141 */