cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sn...@apache.org
Subject cassandra git commit: Add bind variables to trace
Date Fri, 10 Jun 2016 20:14:33 GMT
Repository: cassandra
Updated Branches:
  refs/heads/trunk 8ef1e2ce2 -> 9530b27ad


Add bind variables to trace

patch by Mahdi Mohammadi; reviewed by Robert Stupp for CASSANDRA-11719


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/9530b27a
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/9530b27a
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/9530b27a

Branch: refs/heads/trunk
Commit: 9530b27ade1098d6a648ee4f6abe4ce8c43c94d8
Parents: 8ef1e2c
Author: Mahdi Mohammadi <mahdix@gmail.com>
Authored: Fri Jun 10 22:12:43 2016 +0200
Committer: Robert Stupp <snazy@snazy.de>
Committed: Fri Jun 10 22:12:43 2016 +0200

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 .../transport/messages/ExecuteMessage.java      | 17 ++++-
 .../org/apache/cassandra/cql3/TraceCqlTest.java | 79 +++++++++++++++++++-
 3 files changed, 94 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/9530b27a/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index d18fc9d..309a48d 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 3.8
+ * Add bind variables to trace (CASSANDRA-11719)
  * Switch counter shards' clock to timestamps (CASSANDRA-9811)
  * Introduce HdrHistogram and response/service/wait separation to stress tool (CASSANDRA-11853)
  * entry-weighers in QueryProcessor should respect partitionKeyBindIndexes field (CASSANDRA-11718)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/9530b27a/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 c5e775e..a5348a4 100644
--- a/src/java/org/apache/cassandra/transport/messages/ExecuteMessage.java
+++ b/src/java/org/apache/cassandra/transport/messages/ExecuteMessage.java
@@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableMap;
 import io.netty.buffer.ByteBuf;
 
 import org.apache.cassandra.cql3.CQLStatement;
+import org.apache.cassandra.cql3.ColumnSpecification;
 import org.apache.cassandra.cql3.QueryHandler;
 import org.apache.cassandra.cql3.QueryOptions;
 import org.apache.cassandra.cql3.statements.ParsedStatement;
@@ -121,7 +122,21 @@ public class ExecuteMessage extends Message.Request
                     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.
+                for(int i=0;i<prepared.boundNames.size();i++)
+                {
+                    ColumnSpecification cs = prepared.boundNames.get(i);
+                    String boundName = cs.name.toString();
+                    String boundValue = cs.type.asCQL3Type().toCQLLiteral(options.getValues().get(i),
options.getProtocolVersion());
+                    if ( boundValue.length() > 1000 )
+                    {
+                        boundValue = boundValue.substring(0, 1000) + "...'";
+                    }
+
+                    //Here we prefix boundName with the index to avoid possible collission
in builder keys due to
+                    //having multiple boundValues for the same variable
+                    builder.put("bound_var_" + Integer.toString(i) + "_" + boundName, boundValue);
+                }
+
                 Tracing.instance.begin("Execute CQL3 prepared query", state.getClientAddress(),
builder.build());
             }
 

http://git-wip-us.apache.org/repos/asf/cassandra/blob/9530b27a/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
index bd68940..735fb6a 100644
--- a/test/unit/org/apache/cassandra/cql3/TraceCqlTest.java
+++ b/test/unit/org/apache/cassandra/cql3/TraceCqlTest.java
@@ -18,13 +18,16 @@
 
 package org.apache.cassandra.cql3;
 
-import java.util.List;
-
 import org.junit.Test;
 
+import com.datastax.driver.core.CodecRegistry;
+import com.datastax.driver.core.DataType;
 import com.datastax.driver.core.PreparedStatement;
+import com.datastax.driver.core.ProtocolVersion;
 import com.datastax.driver.core.QueryTrace;
 import com.datastax.driver.core.Session;
+import com.datastax.driver.core.TupleType;
+import com.datastax.driver.core.TupleValue;
 
 import static org.junit.Assert.assertEquals;
 
@@ -46,6 +49,78 @@ public class TraceCqlTest extends CQLTester
                                              .enableTracing();
             QueryTrace trace = session.execute(pstmt.bind(1)).getExecutionInfo().getQueryTrace();
             assertEquals(cql, trace.getParameters().get("query"));
+
+            assertEquals("1", trace.getParameters().get("bound_var_0_id"));
+
+            String cql2 = "SELECT id, v1, v2 FROM " + KEYSPACE + '.' + currentTable() + "
WHERE id IN (?, ?, ?)";
+            pstmt = session.prepare(cql2).enableTracing();
+            trace = session.execute(pstmt.bind(19, 15, 16)).getExecutionInfo().getQueryTrace();
+            assertEquals(cql2, trace.getParameters().get("query"));
+            assertEquals("19", trace.getParameters().get("bound_var_0_id"));
+            assertEquals("15", trace.getParameters().get("bound_var_1_id"));
+            assertEquals("16", trace.getParameters().get("bound_var_2_id"));
+
+            //some more complex tests for tables with map and tuple data types and long bound
values
+            createTable("CREATE TABLE %s (id int primary key, v1 text, v2 tuple<int, text,
float>, v3 map<int, text>)");
+            execute("INSERT INTO %s (id, v1, v2, v3) values (?, ?, ?, ?)", 12, "mahdix",
tuple(3, "bar", 2.1f),
+                    map(1290, "birthday", 39, "anniversary"));
+            execute("INSERT INTO %s (id, v1, v2, v3) values (?, ?, ?, ?)", 274, "CassandraRocks",
tuple(9, "foo", 3.14f),
+                    map(9181, "statement", 716, "public speech"));
+
+            cql = "SELECT id, v1, v2, v3 FROM " + KEYSPACE + '.' + currentTable() + " WHERE
v2 = ? ALLOW FILTERING";
+            pstmt = session.prepare(cql)
+                           .enableTracing();
+            TupleType tt = TupleType.of(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE,
DataType.cint(),
+                                        DataType.text(), DataType.cfloat());
+            TupleValue value = tt.newValue();
+            value.setInt(0, 3);
+            value.setString(1, "bar");
+            value.setFloat(2, 2.1f);
+
+            trace = session.execute(pstmt.bind(value)).getExecutionInfo().getQueryTrace();
+            assertEquals(cql, trace.getParameters().get("query"));
+            assertEquals("(3, 'bar', 2.1)", trace.getParameters().get("bound_var_0_v2"));
+
+            cql2 = "SELECT id, v1, v2, v3 FROM " + KEYSPACE + '.' + currentTable() + " WHERE
v3 CONTAINS KEY ? ALLOW FILTERING";
+            pstmt = session.prepare(cql2).enableTracing();
+            trace = session.execute(pstmt.bind(9181)).getExecutionInfo().getQueryTrace();
+
+            assertEquals(cql2, trace.getParameters().get("query"));
+            assertEquals("9181", trace.getParameters().get("bound_var_0_key(v3)"));
+
+            String boundValue = "Indulgence announcing uncommonly met she continuing two
unpleasing terminated. Now " +
+                                "busy say down the shed eyes roof paid her. Of shameless
collected suspicion existence " +
+                                "in. Share walls stuff think but the arise guest. Course
suffer to do he sussex it " +
+                                "window advice. Yet matter enable misery end extent common
men should. Her indulgence " +
+                                "but assistance favourable cultivated everything collecting."
+
+                                "On projection apartments unsatiable so if he entreaties
appearance. Rose you wife " +
+                                "how set lady half wish. Hard sing an in true felt. Welcomed
stronger if steepest " +
+                                "ecstatic an suitable finished of oh. Entered at excited
at forming between so " +
+                                "produce. Chicken unknown besides attacks gay compact out
you. Continuing no " +
+                                "simplicity no favourable on reasonably melancholy estimating.
Own hence views two " +
+                                "ask right whole ten seems. What near kept met call old west
dine. Our announcing " +
+                                "sufficient why pianoforte. Full age foo set feel her told.
Tastes giving in passed" +
+                                "direct me valley as supply. End great stood boy noisy often
way taken short. Rent the " +
+                                "size our more door. Years no place abode in \uFEFFno child
my. Man pianoforte too " +
+                                "solicitude friendship devonshire ten ask. Course sooner
its silent but formal she " +
+                                "led. Extensive he assurance extremity at breakfast. Dear
sure ye sold fine sell on. " +
+                                "Projection at up connection literature insensible motionless
projecting." +
+                                "Nor hence hoped her after other known defer his. For county
now sister engage had " +
+                                "season better had waited. Occasional mrs interested far
expression acceptance. Day " +
+                                "either mrs talent pulled men rather regret admire but. Life
ye sake it shed. Five " +
+                                "lady he cold in meet up. Service get met adapted matters
offence for. Principles man " +
+                                "any insipidity age you simplicity understood. Do offering
pleasure no ecstatic " +
+                                "whatever on mr directly. ";
+
+            String cql3 = "SELECT id, v1, v2, v3 FROM " + KEYSPACE + '.' + currentTable()
+ " WHERE v3 CONTAINS ? ALLOW FILTERING";
+            pstmt = session.prepare(cql3).enableTracing();
+            trace = session.execute(pstmt.bind(boundValue)).getExecutionInfo().getQueryTrace();
+
+            assertEquals(cql3, trace.getParameters().get("query"));
+
+            //when tracing is done, this boundValue will be surrounded by single quote, and
first 1000 characters
+            //will be filtered. Here we take into account single quotes by adding them to
the expected output
+            assertEquals("'" + boundValue.substring(0, 999) + "...'", trace.getParameters().get("bound_var_0_value(v3)"));
         }
     }
 }


Mime
View raw message