Return-Path: X-Original-To: apmail-cassandra-commits-archive@www.apache.org Delivered-To: apmail-cassandra-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id A6407DC7F for ; Tue, 9 Oct 2012 17:47:57 +0000 (UTC) Received: (qmail 99301 invoked by uid 500); 9 Oct 2012 17:47:57 -0000 Delivered-To: apmail-cassandra-commits-archive@cassandra.apache.org Received: (qmail 99278 invoked by uid 500); 9 Oct 2012 17:47:57 -0000 Mailing-List: contact commits-help@cassandra.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cassandra.apache.org Delivered-To: mailing list commits@cassandra.apache.org Received: (qmail 99266 invoked by uid 99); 9 Oct 2012 17:47:57 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 09 Oct 2012 17:47:57 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 2EB7C3CC1A; Tue, 9 Oct 2012 17:47:57 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: brandonwilliams@apache.org To: commits@cassandra.apache.org X-Mailer: ASF-Git Admin Mailer Subject: git commit: cqlsh: enable tracing Patch by Aleksey Yeschenko, reviewed by brandonwilliams for CASSANDRA-4584 Message-Id: <20121009174757.2EB7C3CC1A@tyr.zones.apache.org> Date: Tue, 9 Oct 2012 17:47:57 +0000 (UTC) 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 Authored: Tue Oct 9 12:46:55 2012 -0500 Committer: Brandon Williams 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''' | | | + | | ; @@ -241,6 +244,9 @@ cqlsh_extra_syntax_rules = r''' ::= ( "HELP" | "?" ) [topic]=( | )* ; + ::= "TRACING" ( switch=( "ON" | "OFF" ) )? + ; + ::= "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