Repository: cassandra Updated Branches: refs/heads/trunk b57b047e5 -> 07385b6ce
Add prepared query parameter to trace for "Execute CQL3 prepared query" session patch by Yasuharu Goto; reviewed by Robert Stupp for CASSANDRA-11425 Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/07385b6c Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/07385b6c Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/07385b6c Branch: refs/heads/trunk Commit: 07385b6ce90d3a230f00f2812b22e3ff158cc2d6 Parents: b57b047 Author: Yasuharu Goto <matope....@gmail.com> Authored: Thu May 5 18:46:05 2016 +0200 Committer: Robert Stupp <sn...@snazy.de> Committed: Thu May 5 18:46:05 2016 +0200 ---------------------------------------------------------------------- .../apache/cassandra/cql3/QueryProcessor.java | 5 +- .../cql3/statements/ParsedStatement.java | 9 ++++ .../transport/messages/ExecuteMessage.java | 1 + .../org/apache/cassandra/cql3/TraceCqlTest.java | 51 ++++++++++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/07385b6c/src/java/org/apache/cassandra/cql3/QueryProcessor.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cql3/QueryProcessor.java b/src/java/org/apache/cassandra/cql3/QueryProcessor.java index 39a9c03..feedfc4 100644 --- a/src/java/org/apache/cassandra/cql3/QueryProcessor.java +++ b/src/java/org/apache/cassandra/cql3/QueryProcessor.java @@ -71,7 +71,7 @@ public class QueryProcessor implements QueryHandler @Override public int weightOf(MD5Digest key, ParsedStatement.Prepared value) { - return Ints.checkedCast(measure(key) + measure(value.statement) + measure(value.boundNames)); + return Ints.checkedCast(measure(key) + measure(value.rawCQLStatement) + measure(value.statement) + measure(value.boundNames)); } }; @@ -80,7 +80,7 @@ public class QueryProcessor implements QueryHandler @Override public int weightOf(Integer key, ParsedStatement.Prepared value) { - return Ints.checkedCast(measure(key) + measure(value.statement) + measure(value.boundNames)); + return Ints.checkedCast(measure(key) + measure(value.rawCQLStatement) + measure(value.statement) + measure(value.boundNames)); } }; @@ -386,6 +386,7 @@ public class QueryProcessor implements QueryHandler return existing; ParsedStatement.Prepared prepared = getStatement(queryString, clientState); + prepared.rawCQLStatement = queryString; int boundTerms = prepared.statement.getBoundTerms(); if (boundTerms > FBUtilities.MAX_UNSIGNED_SHORT) throw new InvalidRequestException(String.format("Too many markers(?). %d markers exceed the allowed maximum of %d", boundTerms, FBUtilities.MAX_UNSIGNED_SHORT)); http://git-wip-us.apache.org/repos/asf/cassandra/blob/07385b6c/src/java/org/apache/cassandra/cql3/statements/ParsedStatement.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/cql3/statements/ParsedStatement.java b/src/java/org/apache/cassandra/cql3/statements/ParsedStatement.java index 4c3f8a9..5f6c42c 100644 --- a/src/java/org/apache/cassandra/cql3/statements/ParsedStatement.java +++ b/src/java/org/apache/cassandra/cql3/statements/ParsedStatement.java @@ -48,6 +48,14 @@ public abstract class ParsedStatement public static class Prepared { + /** + * Contains the CQL statement source if the statement has been "regularly" perpared via + * {@link org.apache.cassandra.cql3.QueryProcessor#prepare(java.lang.String, org.apache.cassandra.service.ClientState, boolean)} / + * {@link QueryHandler#prepare(java.lang.String, org.apache.cassandra.service.QueryState, java.util.Map)}. + * Other usages of this class may or may not contain the CQL statement source. + */ + public String rawCQLStatement; + public final CQLStatement statement; public final List<ColumnSpecification> boundNames; public final Short[] partitionKeyBindIndexes; @@ -57,6 +65,7 @@ public abstract class ParsedStatement this.statement = statement; this.boundNames = boundNames; this.partitionKeyBindIndexes = partitionKeyBindIndexes; + this.rawCQLStatement = ""; } public Prepared(CQLStatement statement, VariableSpecifications names, Short[] partitionKeyBindIndexes) http://git-wip-us.apache.org/repos/asf/cassandra/blob/07385b6c/src/java/org/apache/cassandra/transport/messages/ExecuteMessage.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/transport/messages/ExecuteMessage.java b/src/java/org/apache/cassandra/transport/messages/ExecuteMessage.java index df6acc0..c5e775e 100644 --- a/src/java/org/apache/cassandra/transport/messages/ExecuteMessage.java +++ b/src/java/org/apache/cassandra/transport/messages/ExecuteMessage.java @@ -119,6 +119,7 @@ public class ExecuteMessage extends Message.Request builder.put("consistency_level", options.getConsistency().name()); if(options.getSerialConsistency() != null) builder.put("serial_consistency_level", options.getSerialConsistency().name()); + builder.put("query", prepared.rawCQLStatement); // TODO we don't have [typed] access to CQL bind variables here. CASSANDRA-4560 is open to add support. Tracing.instance.begin("Execute CQL3 prepared query", state.getClientAddress(), builder.build()); http://git-wip-us.apache.org/repos/asf/cassandra/blob/07385b6c/test/unit/org/apache/cassandra/cql3/TraceCqlTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/cql3/TraceCqlTest.java b/test/unit/org/apache/cassandra/cql3/TraceCqlTest.java new file mode 100644 index 0000000..bd68940 --- /dev/null +++ b/test/unit/org/apache/cassandra/cql3/TraceCqlTest.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.cql3; + +import java.util.List; + +import org.junit.Test; + +import com.datastax.driver.core.PreparedStatement; +import com.datastax.driver.core.QueryTrace; +import com.datastax.driver.core.Session; + +import static org.junit.Assert.assertEquals; + +public class TraceCqlTest extends CQLTester +{ + @Test + public void testCqlStatementTracing() throws Throwable + { + requireNetwork(); + + createTable("CREATE TABLE %s (id int primary key, v1 text, v2 text)"); + execute("INSERT INTO %s (id, v1, v2) VALUES (?, ?, ?)", 1, "Apache", "Cassandra"); + execute("INSERT INTO %s (id, v1, v2) VALUES (?, ?, ?)", 2, "trace", "test"); + + try (Session session = sessionNet()) + { + String cql = "SELECT id, v1, v2 FROM " + KEYSPACE + '.' + currentTable() + " WHERE id = ?"; + PreparedStatement pstmt = session.prepare(cql) + .enableTracing(); + QueryTrace trace = session.execute(pstmt.bind(1)).getExecutionInfo().getQueryTrace(); + assertEquals(cql, trace.getParameters().get("query")); + } + } +}