cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brandonwilli...@apache.org
Subject git commit: cqlsh: enable tracing Patch by Aleksey Yeschenko, reviewed by brandonwilliams for CASSANDRA-4584
Date Tue, 09 Oct 2012 17:47:57 GMT
Updated Branches:
  refs/heads/trunk 21b7bbe6a -> ed6d24e59


cqlsh: enable tracing
Patch by Aleksey Yeschenko, reviewed by brandonwilliams for
CASSANDRA-4584


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

Branch: refs/heads/trunk
Commit: ed6d24e592b3d84cac520295a5ae9f1161e2d829
Parents: 21b7bbe
Author: Brandon Williams <brandonwilliams@apache.org>
Authored: Tue Oct 9 12:46:55 2012 -0500
Committer: Brandon Williams <brandonwilliams@apache.org>
Committed: Tue Oct 9 12:46:55 2012 -0500

----------------------------------------------------------------------
 bin/cqlsh                                          |   74 ++++++++++++++-
 .../org/apache/cassandra/tracing/TraceState.java   |    7 --
 2 files changed, 72 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/ed6d24e5/bin/cqlsh
----------------------------------------------------------------------
diff --git a/bin/cqlsh b/bin/cqlsh
index 729fcdc..a848d05 100755
--- a/bin/cqlsh
+++ b/bin/cqlsh
@@ -39,6 +39,7 @@ from itertools import groupby
 from contextlib import contextmanager, closing
 from glob import glob
 from functools import partial
+from uuid import UUID
 
 import cmd
 import sys
@@ -172,6 +173,7 @@ my_commands_ending_with_newline = (
     'source',
     'capture',
     'debug',
+    'tracing',
     'exit',
     'quit'
 )
@@ -195,6 +197,7 @@ cqlsh_extra_syntax_rules = r'''
                    | <copyCommand>
                    | <debugCommand>
                    | <helpCommand>
+                   | <tracingCommand>
                    | <exitCommand>
                    ;
 
@@ -241,6 +244,9 @@ cqlsh_extra_syntax_rules = r'''
 <helpCommand> ::= ( "HELP" | "?" ) [topic]=( <identifier> | <stringLiteral>
)*
                 ;
 
+<tracingCommand> ::= "TRACING" ( switch=( "ON" | "OFF" ) )?
+                   ;
+
 <exitCommand> ::= "exit" | "quit"
                 ;
 
@@ -419,6 +425,7 @@ class Shell(cmd.Cmd):
     def __init__(self, hostname, port, transport_factory, color=False,
                  username=None, password=None, encoding=None, stdin=None, tty=True,
                  completekey='tab', use_conn=None, cqlver=None, keyspace=None,
+                 tracing_enabled=False,
                  display_time_format=DEFAULT_TIME_FORMAT,
                  display_float_precision=DEFAULT_FLOAT_PRECISION):
         cmd.Cmd.__init__(self, completekey=completekey)
@@ -428,6 +435,7 @@ class Shell(cmd.Cmd):
         self.username = username
         self.password = password
         self.keyspace = keyspace
+        self.tracing_enabled = tracing_enabled
         if use_conn is not None:
             self.conn = use_conn
         else:
@@ -694,6 +702,9 @@ class Shell(cmd.Cmd):
         except cql.cassandra.ttypes.InvalidRequestException, e:
             raise VersionNotSupported(e.why)
 
+    def trace_next_query(self):
+        return self.make_hacktastic_thrift_call('trace_next_query')
+
     def make_hacktastic_thrift_call(self, call, *args):
         client = self.conn.client
         return getattr(client, call)(*args)
@@ -912,7 +923,7 @@ class Shell(cmd.Cmd):
         (CQL 3).
         """
         ksname = parsed.get_binding('ksname')
-        if self.perform_statement(parsed.extract_orig()):
+        if self.perform_statement_untraced(parsed.extract_orig()):
             self.current_keyspace = self.cql_unprotect_name(ksname)
 
     def do_select(self, parsed):
@@ -946,6 +957,16 @@ class Shell(cmd.Cmd):
         self.perform_statement(parsed.extract_orig(), decoder=decoder)
 
     def perform_statement(self, statement, decoder=None):
+        if self.tracing_enabled:
+            session_id = UUID(bytes=self.trace_next_query())
+            result = self.perform_statement_untraced(statement, decoder=None)
+            time.sleep(0.5) # trace writes are async so we wait a little.
+            self.print_trace(session_id)
+            return result
+        else:
+            return self.perform_statement_untraced(statement, decoder=None)
+
+    def perform_statement_untraced(self, statement, decoder=None):
         if not statement:
             return False
         trynum = 1
@@ -986,6 +1007,9 @@ class Shell(cmd.Cmd):
         self.flush_output()
         return True
 
+    def print_trace(self, session_id):
+        self.perform_statement_untraced("SELECT * FROM system_traces.events WHERE session_id
= '%s'" % session_id)
+
     # these next two functions are not guaranteed perfect; just checks if the
     # statement parses fully according to cqlsh's own understanding of the
     # grammar. Changes to the language in Cassandra frequently don't get
@@ -1558,7 +1582,7 @@ class Shell(cmd.Cmd):
         cql = prepq % valstring
         if self.debug:
             print "Import using CQL: %s" % cql
-        return self.perform_statement(cql)
+        return self.perform_statement_untraced(cql)
 
     def perform_csv_export(self, ks, cf, columns, fname, opts):
         dialect_options = self.csv_dialect_defaults.copy()
@@ -1820,6 +1844,52 @@ class Shell(cmd.Cmd):
         self.color = False
         print 'Now capturing query output to %r.' % (fname,)
 
+    def do_tracing(self, parsed):
+        """
+        TRACING [cqlsh with CQL 3.0.0 or higher only]
+
+          Enables or disables request tracing.
+
+        TRACING ON
+
+          Enables tracing for all further requests.
+
+        TRACING OFF
+
+          Disables tracing.
+
+        TRACING
+
+          TRACING with no arguments shows the current tracing status.
+        """
+        if not self.cqlver_atleast(3):
+            self.printerr('Tracing requires CQL version 3.0.0 or higher.')
+            return
+
+        switch = parsed.get_binding('switch')
+        if switch is None:
+            if self.tracing_enabled:
+                print "Tracing is currently enabled. Use TRACING OFF to disable"
+            else:
+                print "Tracing is currently disabled. Use TRACING ON to enable."
+            return
+
+        if switch.upper() == 'ON':
+            if self.tracing_enabled:
+                self.printerr('Tracing is already enabled. '
+                              'Use TRACING OFF to disable.')
+                return
+            self.tracing_enabled = True
+            print 'Now tracing requests.'
+            return
+
+        if switch.upper() == 'OFF':
+            if not self.tracing_enabled:
+                self.printerr('Tracing is not enabled.')
+                return
+            self.tracing_enabled = False
+            print 'Disabled tracing.'
+
     def do_exit(self, parsed=None):
         """
         EXIT/QUIT [cqlsh only]

http://git-wip-us.apache.org/repos/asf/cassandra/blob/ed6d24e5/src/java/org/apache/cassandra/tracing/TraceState.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/tracing/TraceState.java b/src/java/org/apache/cassandra/tracing/TraceState.java
index 7fca842..ae0fa20 100644
--- a/src/java/org/apache/cassandra/tracing/TraceState.java
+++ b/src/java/org/apache/cassandra/tracing/TraceState.java
@@ -17,8 +17,6 @@
  */
 package org.apache.cassandra.tracing;
 
-import static com.google.common.base.Preconditions.checkNotNull;
-
 import java.net.InetAddress;
 import java.nio.ByteBuffer;
 import java.util.UUID;
@@ -26,12 +24,7 @@ import java.util.concurrent.TimeUnit;
 
 import com.google.common.base.Stopwatch;
 
-import org.apache.cassandra.thrift.ColumnParent;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 import org.apache.cassandra.utils.ByteBufferUtil;
-import org.apache.cassandra.utils.UUIDGen;
 
 /**
  * ThreadLocal state for a tracing session. The presence of an instance of this class as
a ThreadLocal denotes that an


Mime
View raw message