kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | cbe117e45877b5f3070c0155298d1df904e295801d82baee2e9c72f4d89b36fa |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-05-11 16:22 -07:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-05-11 16:22 -07:00 |
| Parents | 876983ef4dbf, b364a0e42c7c |
9 files changed
modified changelog/README.md +1 −0
| @@ -403,6 +403,7 @@ | ||
| 403 | 403 | |
| 404 | 404 | - [bug] JAVA-1179: Request objects should be copied when executed. |
| 405 | 405 | - [improvement] JAVA-1182: Throw error when synchronous call made on I/O thread. |
| 406 | +- [bug] JAVA-1184: Unwrap StatementWrappers when extracting column definitions. | |
| 406 | 407 | |
| 407 | 408 | |
| 408 | 409 | ### 2.0.12.1 |
modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedResultSet.java +3 −0
| @@ -59,6 +59,9 @@ | ||
| 59 | 59 | |
| 60 | 60 | ColumnDefinitions columnDefs; |
| 61 | 61 | if (r.metadata.columns == null) { |
| 62 | + if (statement instanceof StatementWrapper) { | |
| 63 | + statement = ((StatementWrapper) statement).getWrappedStatement(); | |
| 64 | + } | |
| 62 | 65 | assert statement instanceof BoundStatement; |
| 63 | 66 | columnDefs = ((BoundStatement) statement).statement.getPreparedId().resultSetMetadata; |
| 64 | 67 | assert columnDefs != null; |
modified driver-core/src/main/java/com/datastax/driver/core/BatchStatement.java +3 −0
| @@ -127,6 +127,9 @@ | ||
| 127 | 127 | * of statements for a BatchStatement allowed by the underlying protocol). |
| 128 | 128 | */ |
| 129 | 129 | public BatchStatement add(Statement statement) { |
| 130 | + if (statement instanceof StatementWrapper) { | |
| 131 | + statement = ((StatementWrapper) statement).getWrappedStatement(); | |
| 132 | + } | |
| 130 | 133 | |
| 131 | 134 | // We handle BatchStatement here (rather than in getIdAndValues) as it make it slightly |
| 132 | 135 | // 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 @@ | ||
| 156 | 156 | setException(new DriverInternalError(String.format("Got unexpected %s response from %s", response.type, connection.address))); |
| 157 | 157 | break; |
| 158 | 158 | } |
| 159 | - } catch (RuntimeException e) { | |
| 159 | + } catch (Throwable e) { | |
| 160 | 160 | // If we get a bug here, the client will not get it, so better forwarding the error |
| 161 | 161 | setException(new DriverInternalError("Unexpected error while processing response from " + connection.address, e)); |
| 162 | 162 | } |
modified driver-core/src/main/java/com/datastax/driver/core/QueryLogger.java +3 −3
| @@ -618,6 +618,9 @@ | ||
| 618 | 618 | */ |
| 619 | 619 | @Override |
| 620 | 620 | public void update(Host host, Statement statement, Exception exception, long newLatencyNanos) { |
| 621 | + if (statement instanceof StatementWrapper) | |
| 622 | + statement = ((StatementWrapper) statement).getWrappedStatement(); | |
| 623 | + | |
| 621 | 624 | long latencyMs = NANOSECONDS.toMillis(newLatencyNanos); |
| 622 | 625 | if (exception == null) { |
| 623 | 626 | maybeLogNormalOrSlowQuery(host, statement, latencyMs); |
| @@ -827,9 +830,6 @@ | ||
| 827 | 830 | } |
| 828 | 831 | |
| 829 | 832 | protected int append(Statement statement, StringBuilder buffer, int remaining) { |
| 830 | - if (statement instanceof StatementWrapper) | |
| 831 | - statement = ((StatementWrapper) statement).getWrappedStatement(); | |
| 832 | - | |
| 833 | 833 | if (statement instanceof RegularStatement) { |
| 834 | 834 | remaining = append(((RegularStatement) statement).getQueryString().trim(), buffer, remaining); |
| 835 | 835 | } else if (statement instanceof BoundStatement) { |
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +3 −2
| @@ -645,9 +645,10 @@ | ||
| 645 | 645 | |
| 646 | 646 | @Override |
| 647 | 647 | protected void checkNotInEventLoop() { |
| 648 | - if (!CHECK_IO_DEADLOCKS) | |
| 648 | + Connection.Factory connectionFactory = cluster.manager.connectionFactory; | |
| 649 | + if (!CHECK_IO_DEADLOCKS || connectionFactory == null) | |
| 649 | 650 | return; |
| 650 | - for (EventExecutor executor : cluster.manager.connectionFactory.eventLoopGroup) { | |
| 651 | + for (EventExecutor executor : connectionFactory.eventLoopGroup) { | |
| 651 | 652 | if (executor.inEventLoop()) { |
| 652 | 653 | throw new IllegalStateException( |
| 653 | 654 | "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 @@ | ||
| 20 | 20 | import com.google.common.base.Throwables; |
| 21 | 21 | import com.google.common.collect.Lists; |
| 22 | 22 | import com.google.common.util.concurrent.*; |
| 23 | +import org.slf4j.Logger; | |
| 24 | +import org.slf4j.LoggerFactory; | |
| 23 | 25 | import org.testng.annotations.DataProvider; |
| 24 | 26 | import org.testng.annotations.Test; |
| 25 | 27 | |
| @@ -33,6 +35,8 @@ | ||
| 33 | 35 | |
| 34 | 36 | public class AsyncQueryTest extends CCMTestsSupport { |
| 35 | 37 | |
| 38 | + Logger logger = LoggerFactory.getLogger(AsyncQueryTest.class); | |
| 39 | + | |
| 36 | 40 | @DataProvider(name = "keyspace") |
| 37 | 41 | public static Object[][] keyspace() { |
| 38 | 42 | return new Object[][]{{"asyncquerytest"}, {"\"AsyncQueryTest\""}}; |
| @@ -129,22 +133,34 @@ | ||
| 129 | 133 | |
| 130 | 134 | @Test(groups = "short") |
| 131 | 135 | 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; | |
| 138 | 161 | } |
| 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"); | |
| 147 | 162 | } |
| 163 | + fail("callback was not executed on io thread in 1000 attempts, something may be wrong."); | |
| 148 | 164 | } |
| 149 | 165 | |
| 150 | 166 | 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 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import com.datastax.driver.core.StatementWrapperTest.CustomStatement; | |
| 18 | 19 | import com.datastax.driver.core.exceptions.DriverException; |
| 19 | 20 | import com.datastax.driver.core.utils.CassandraVersion; |
| 20 | 21 | import com.google.common.base.Function; |
| @@ -949,6 +950,33 @@ | ||
| 949 | 950 | .contains("42"); |
| 950 | 951 | } |
| 951 | 952 | |
| 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 | + | |
| 952 | 980 | @Override |
| 953 | 981 | public void onTestContextInitialized() { |
| 954 | 982 | 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 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.policies.*; |
| 19 | +import com.datastax.driver.core.utils.CassandraVersion; | |
| 19 | 20 | import org.testng.annotations.Test; |
| 20 | 21 | |
| 21 | 22 | import java.util.Iterator; |
| @@ -25,10 +26,18 @@ | ||
| 25 | 26 | |
| 26 | 27 | public class StatementWrapperTest extends CCMTestsSupport { |
| 27 | 28 | |
| 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 | + | |
| 28 | 32 | CustomLoadBalancingPolicy loadBalancingPolicy = new CustomLoadBalancingPolicy(); |
| 29 | 33 | CustomSpeculativeExecutionPolicy speculativeExecutionPolicy = new CustomSpeculativeExecutionPolicy(); |
| 30 | 34 | CustomRetryPolicy retryPolicy = new CustomRetryPolicy(); |
| 31 | 35 | |
| 36 | + @Override | |
| 37 | + public void onTestContextInitialized() { | |
| 38 | + execute("create table test (k text primary key, v int)"); | |
| 39 | + } | |
| 40 | + | |
| 32 | 41 | @Override |
| 33 | 42 | public Cluster.Builder createClusterBuilder() { |
| 34 | 43 | return Cluster.builder() |
| @@ -77,6 +86,56 @@ | ||
| 77 | 86 | assertThat(retryPolicy.customStatementsHandled.get()).isEqualTo(1); |
| 78 | 87 | } |
| 79 | 88 | |
| 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 | + | |
| 80 | 139 | /** |
| 81 | 140 | * A custom wrapper that's just used to mark statements. |
| 82 | 141 | */ |