kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 93bd8be689d9336a46f1910206a2014949e847e8c6d1408bbb5bbdd2cdbebfba |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-05-11 11:26 -07:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-05-11 11:26 -07:00 |
| Parents | 2529393d3013, a8bc347c63de |
7 files changed
modified changelog/README.md +1 −0
| @@ -4,6 +4,7 @@ | ||
| 4 | 4 | |
| 5 | 5 | - [bug] JAVA-1179: Request objects should be copied when executed. |
| 6 | 6 | - [improvement] JAVA-1182: Throw error when synchronous call made on I/O thread. |
| 7 | +- [bug] JAVA-1184: Unwrap StatementWrappers when extracting column definitions. | |
| 7 | 8 | |
| 8 | 9 | |
| 9 | 10 | ### 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
| @@ -118,6 +118,9 @@ | ||
| 118 | 118 | * of statements for a BatchStatement allowed by the underlying protocol). |
| 119 | 119 | */ |
| 120 | 120 | public BatchStatement add(Statement statement) { |
| 121 | + if (statement instanceof StatementWrapper) { | |
| 122 | + statement = ((StatementWrapper) statement).getWrappedStatement(); | |
| 123 | + } | |
| 121 | 124 | |
| 122 | 125 | // We handle BatchStatement here (rather than in getIdAndValues) as it make it slightly |
| 123 | 126 | // 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
| @@ -143,7 +143,7 @@ | ||
| 143 | 143 | setException(new DriverInternalError(String.format("Got unexpected %s response from %s", response.type, connection.address))); |
| 144 | 144 | break; |
| 145 | 145 | } |
| 146 | - } catch (RuntimeException e) { | |
| 146 | + } catch (Throwable e) { | |
| 147 | 147 | // If we get a bug here, the client will not get it, so better forwarding the error |
| 148 | 148 | setException(new DriverInternalError("Unexpected error while processing response from " + connection.address, e)); |
| 149 | 149 | } |
modified driver-core/src/main/java/com/datastax/driver/core/QueryLogger.java +3 −3
| @@ -614,6 +614,9 @@ | ||
| 614 | 614 | */ |
| 615 | 615 | @Override |
| 616 | 616 | public void update(Host host, Statement statement, Exception exception, long newLatencyNanos) { |
| 617 | + if (statement instanceof StatementWrapper) | |
| 618 | + statement = ((StatementWrapper) statement).getWrappedStatement(); | |
| 619 | + | |
| 617 | 620 | long latencyMs = NANOSECONDS.toMillis(newLatencyNanos); |
| 618 | 621 | if (exception == null) { |
| 619 | 622 | maybeLogNormalOrSlowQuery(host, statement, latencyMs); |
| @@ -745,9 +748,6 @@ | ||
| 745 | 748 | } |
| 746 | 749 | |
| 747 | 750 | protected int append(Statement statement, StringBuilder buffer, int remaining) { |
| 748 | - if (statement instanceof StatementWrapper) | |
| 749 | - statement = ((StatementWrapper) statement).getWrappedStatement(); | |
| 750 | - | |
| 751 | 751 | if (statement instanceof RegularStatement) { |
| 752 | 752 | remaining = append(((RegularStatement) statement).getQueryString().trim(), buffer, remaining); |
| 753 | 753 | } else if (statement instanceof BoundStatement) { |
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; |
| @@ -713,6 +714,33 @@ | ||
| 713 | 714 | .contains("pk:42"); |
| 714 | 715 | } |
| 715 | 716 | |
| 717 | + @CassandraVersion(major=2.0) | |
| 718 | + @Test(groups = "short") | |
| 719 | + public void should_log_wrapped_bound_statement() throws Exception { | |
| 720 | + // given | |
| 721 | + normal.setLevel(TRACE); | |
| 722 | + queryLogger = QueryLogger.builder(cluster) | |
| 723 | + .withConstantThreshold(Long.MAX_VALUE) | |
| 724 | + .withMaxQueryStringLength(Integer.MAX_VALUE) | |
| 725 | + .build(); | |
| 726 | + cluster.register(queryLogger); | |
| 727 | + // when | |
| 728 | + String query = "UPDATE test SET c_text = :param1 WHERE pk = :param2"; | |
| 729 | + PreparedStatement ps = session.prepare(query); | |
| 730 | + BoundStatement bs = ps.bind(); | |
| 731 | + bs.setString("param1", "foo"); | |
| 732 | + bs.setInt("param2", 42); | |
| 733 | + session.execute(new CustomStatement(bs)); | |
| 734 | + // then | |
| 735 | + String line = normalAppender.waitAndGet(10000); | |
| 736 | + assertThat(line) | |
| 737 | + .contains("Query completed normally") | |
| 738 | + .contains(ipOfNode(1)) | |
| 739 | + .contains(query) | |
| 740 | + .contains("param2:42") | |
| 741 | + .contains("param1:'foo'"); | |
| 742 | + } | |
| 743 | + | |
| 716 | 744 | @Override |
| 717 | 745 | protected List<String> getTableDefinitions() { |
| 718 | 746 | return Lists.newArrayList("CREATE TABLE test (pk int PRIMARY KEY, " + definitions + ")"); |
modified driver-core/src/test/java/com/datastax/driver/core/StatementWrapperTest.java +58 −1
| @@ -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 com.google.common.collect.Lists; |
| 20 | 21 | import org.testng.annotations.Test; |
| 21 | 22 | |
| @@ -26,9 +27,15 @@ | ||
| 26 | 27 | import static org.assertj.core.api.Assertions.assertThat; |
| 27 | 28 | |
| 28 | 29 | public class StatementWrapperTest extends CCMBridge.PerClassSingleNodeCluster { |
| 30 | + | |
| 31 | + private static final String INSERT_QUERY = "insert into test (k, v) values (?, ?)"; | |
| 32 | + private static final String SELECT_QUERY = "select * from test where k = ?"; | |
| 33 | + | |
| 29 | 34 | @Override |
| 30 | 35 | protected Collection<String> getTableDefinitions() { |
| 31 | - return Lists.newArrayList(); | |
| 36 | + return Lists.newArrayList( | |
| 37 | + "create table test (k text primary key, v int)" | |
| 38 | + ); | |
| 32 | 39 | } |
| 33 | 40 | |
| 34 | 41 | CustomLoadBalancingPolicy loadBalancingPolicy = new CustomLoadBalancingPolicy(); |
| @@ -83,6 +90,56 @@ | ||
| 83 | 90 | assertThat(retryPolicy.customStatementsHandled.get()).isEqualTo(1); |
| 84 | 91 | } |
| 85 | 92 | |
| 93 | + @CassandraVersion(major=2.0) | |
| 94 | + @Test(groups = "short") | |
| 95 | + public void should_execute_wrapped_simple_statement() { | |
| 96 | + session.execute(new CustomStatement(new SimpleStatement(INSERT_QUERY, "key_simple", 1))); | |
| 97 | + | |
| 98 | + ResultSet rs = session.execute(new CustomStatement(new SimpleStatement(SELECT_QUERY, "key_simple"))); | |
| 99 | + assertThat(rs.one().getInt("v")).isEqualTo(1); | |
| 100 | + } | |
| 101 | + | |
| 102 | + @Test(groups = "short") | |
| 103 | + public void should_execute_wrapped_bound_statement() { | |
| 104 | + PreparedStatement preparedStatement = session.prepare(new SimpleStatement(INSERT_QUERY)); | |
| 105 | + session.execute(new CustomStatement(preparedStatement.bind("key_bound", 1))); | |
| 106 | + | |
| 107 | + preparedStatement = session.prepare(new SimpleStatement(SELECT_QUERY)); | |
| 108 | + ResultSet rs = session.execute(new CustomStatement(preparedStatement.bind("key_bound"))); | |
| 109 | + assertThat(rs.one().getInt("v")).isEqualTo(1); | |
| 110 | + } | |
| 111 | + | |
| 112 | + @CassandraVersion(major=2.0) | |
| 113 | + @Test(groups = "short") | |
| 114 | + public void should_execute_wrapped_batch_statement() { | |
| 115 | + BatchStatement batchStatement = new BatchStatement(); | |
| 116 | + batchStatement.add(new SimpleStatement(INSERT_QUERY, "key_batch", 1)); | |
| 117 | + | |
| 118 | + session.execute(new CustomStatement(batchStatement)); | |
| 119 | + | |
| 120 | + ResultSet rs = session.execute(SELECT_QUERY, "key_batch"); | |
| 121 | + assertThat(rs.one().getInt("v")).isEqualTo(1); | |
| 122 | + } | |
| 123 | + | |
| 124 | + @CassandraVersion(major=2.0) | |
| 125 | + @Test(groups = "short") | |
| 126 | + public void should_add_wrapped_batch_statement_to_batch_statement() { | |
| 127 | + BatchStatement batchStatementForWrapping = new BatchStatement(); | |
| 128 | + batchStatementForWrapping.add(new SimpleStatement(INSERT_QUERY, "key1", 1)); | |
| 129 | + | |
| 130 | + BatchStatement batchStatement = new BatchStatement(); | |
| 131 | + batchStatement.add(new CustomStatement(new SimpleStatement(INSERT_QUERY, "key2", 2))); | |
| 132 | + batchStatement.add(new CustomStatement(batchStatementForWrapping)); | |
| 133 | + | |
| 134 | + session.execute(batchStatement); | |
| 135 | + | |
| 136 | + ResultSet rs = session.execute(SELECT_QUERY, "key1"); | |
| 137 | + assertThat(rs.one().getInt("v")).isEqualTo(1); | |
| 138 | + | |
| 139 | + rs = session.execute(SELECT_QUERY, "key2"); | |
| 140 | + assertThat(rs.one().getInt("v")).isEqualTo(2); | |
| 141 | + } | |
| 142 | + | |
| 86 | 143 | /** |
| 87 | 144 | * A custom wrapper that's just used to mark statements. |
| 88 | 145 | */ |