kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 3cd7eb6efc56929771aa6178a27686857d1eafdd9e051a7148f4f4dd018b58b6 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-05-11 16:24 -07:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-05-11 16:24 -07:00 |
| Parents | 4b35bd0a42ce, cbe117e45877 |
9 files changed
modified changelog/README.md +1 −0
| @@ -704,6 +704,7 @@ | ||
| 704 | 704 | |
| 705 | 705 | - [bug] JAVA-1179: Request objects should be copied when executed. |
| 706 | 706 | - [improvement] JAVA-1182: Throw error when synchronous call made on I/O thread. |
| 707 | +- [bug] JAVA-1184: Unwrap StatementWrappers when extracting column definitions. | |
| 707 | 708 | |
| 708 | 709 | |
| 709 | 710 | ### 2.0.12.1 |
modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedResultSet.java +3 −0
| @@ -62,6 +62,9 @@ | ||
| 62 | 62 | |
| 63 | 63 | ColumnDefinitions columnDefs; |
| 64 | 64 | if (r.metadata.columns == null) { |
| 65 | + if (statement instanceof StatementWrapper) { | |
| 66 | + statement = ((StatementWrapper) statement).getWrappedStatement(); | |
| 67 | + } | |
| 65 | 68 | assert statement instanceof BoundStatement; |
| 66 | 69 | columnDefs = ((BoundStatement) statement).statement.getPreparedId().resultSetMetadata; |
| 67 | 70 | assert columnDefs != null; |
modified driver-core/src/main/java/com/datastax/driver/core/BatchStatement.java +3 −0
| @@ -134,6 +134,9 @@ | ||
| 134 | 134 | * @throws IllegalArgumentException if adding a regular statement that uses named values. |
| 135 | 135 | */ |
| 136 | 136 | public BatchStatement add(Statement statement) { |
| 137 | + if (statement instanceof StatementWrapper) { | |
| 138 | + statement = ((StatementWrapper) statement).getWrappedStatement(); | |
| 139 | + } | |
| 137 | 140 | if ((statement instanceof RegularStatement) && ((RegularStatement) statement).usesNamedValues()) { |
| 138 | 141 | throw new IllegalArgumentException("Batch statement cannot contain regular statements with named values (" |
| 139 | 142 | + ((RegularStatement) statement).getQueryString() + ")"); |
modified driver-core/src/main/java/com/datastax/driver/core/DefaultResultSetFuture.java +1 −1
| @@ -184,7 +184,7 @@ | ||
| 184 | 184 | setException(new DriverInternalError(String.format("Got unexpected %s response from %s", response.type, connection.address))); |
| 185 | 185 | break; |
| 186 | 186 | } |
| 187 | - } catch (RuntimeException e) { | |
| 187 | + } catch (Throwable e) { | |
| 188 | 188 | // If we get a bug here, the client will not get it, so better forwarding the error |
| 189 | 189 | setException(new DriverInternalError("Unexpected error while processing response from " + connection.address, e)); |
| 190 | 190 | } |
modified driver-core/src/main/java/com/datastax/driver/core/QueryLogger.java +3 −3
| @@ -627,6 +627,9 @@ | ||
| 627 | 627 | if (cluster == null) |
| 628 | 628 | throw new IllegalStateException("This method should only be called after the logger has been registered with a cluster"); |
| 629 | 629 | |
| 630 | + if (statement instanceof StatementWrapper) | |
| 631 | + statement = ((StatementWrapper) statement).getWrappedStatement(); | |
| 632 | + | |
| 630 | 633 | long latencyMs = NANOSECONDS.toMillis(newLatencyNanos); |
| 631 | 634 | if (exception == null) { |
| 632 | 635 | maybeLogNormalOrSlowQuery(host, statement, latencyMs); |
| @@ -849,9 +852,6 @@ | ||
| 849 | 852 | } |
| 850 | 853 | |
| 851 | 854 | protected int append(Statement statement, StringBuilder buffer, int remaining) { |
| 852 | - if (statement instanceof StatementWrapper) | |
| 853 | - statement = ((StatementWrapper) statement).getWrappedStatement(); | |
| 854 | - | |
| 855 | 855 | if (statement instanceof RegularStatement) { |
| 856 | 856 | RegularStatement rs = (RegularStatement) statement; |
| 857 | 857 | String query = rs.getQueryString(); |
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +3 −2
| @@ -667,9 +667,10 @@ | ||
| 667 | 667 | |
| 668 | 668 | @Override |
| 669 | 669 | protected void checkNotInEventLoop() { |
| 670 | - if (!CHECK_IO_DEADLOCKS) | |
| 670 | + Connection.Factory connectionFactory = cluster.manager.connectionFactory; | |
| 671 | + if (!CHECK_IO_DEADLOCKS || connectionFactory == null) | |
| 671 | 672 | return; |
| 672 | - for (EventExecutor executor : cluster.manager.connectionFactory.eventLoopGroup) { | |
| 673 | + for (EventExecutor executor : connectionFactory.eventLoopGroup) { | |
| 673 | 674 | if (executor.inEventLoop()) { |
| 674 | 675 | throw new IllegalStateException( |
| 675 | 676 | "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\""}}; |
| @@ -123,22 +127,34 @@ | ||
| 123 | 127 | |
| 124 | 128 | @Test(groups = "short") |
| 125 | 129 | 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; | |
| 132 | 155 | } |
| 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"); | |
| 141 | 156 | } |
| 157 | + fail("callback was not executed on io thread in 1000 attempts, something may be wrong."); | |
| 142 | 158 | } |
| 143 | 159 | |
| 144 | 160 | 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; |
| @@ -1036,6 +1037,33 @@ | ||
| 1036 | 1037 | .contains("42"); |
| 1037 | 1038 | } |
| 1038 | 1039 | |
| 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 | + | |
| 1039 | 1067 | @Override |
| 1040 | 1068 | public void onTestContextInitialized() { |
| 1041 | 1069 | 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 @@ | ||
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.exceptions.DriverException; |
| 19 | 19 | import com.datastax.driver.core.policies.*; |
| 20 | +import com.datastax.driver.core.utils.CassandraVersion; | |
| 20 | 21 | import org.testng.annotations.Test; |
| 21 | 22 | |
| 22 | 23 | import java.util.Iterator; |
| @@ -26,10 +27,18 @@ | ||
| 26 | 27 | |
| 27 | 28 | public class StatementWrapperTest extends CCMTestsSupport { |
| 28 | 29 | |
| 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 | + | |
| 29 | 33 | CustomLoadBalancingPolicy loadBalancingPolicy = new CustomLoadBalancingPolicy(); |
| 30 | 34 | CustomSpeculativeExecutionPolicy speculativeExecutionPolicy = new CustomSpeculativeExecutionPolicy(); |
| 31 | 35 | CustomRetryPolicy retryPolicy = new CustomRetryPolicy(); |
| 32 | 36 | |
| 37 | + @Override | |
| 38 | + public void onTestContextInitialized() { | |
| 39 | + execute("create table test (k text primary key, v int)"); | |
| 40 | + } | |
| 41 | + | |
| 33 | 42 | @Override |
| 34 | 43 | public Cluster.Builder createClusterBuilder() { |
| 35 | 44 | return Cluster.builder() |
| @@ -78,6 +87,56 @@ | ||
| 78 | 87 | assertThat(retryPolicy.customStatementsHandled.get()).isEqualTo(1); |
| 79 | 88 | } |
| 80 | 89 | |
| 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 | + | |
| 81 | 140 | /** |
| 82 | 141 | * A custom wrapper that's just used to mark statements. |
| 83 | 142 | */ |