From 941cd456ae1d0fd933a540468d2f9f13764d6de3 Mon Sep 17 00:00:00 2001 From: Aleksey Yeschenko Date: Wed, 23 Oct 2013 22:35:14 +0800 Subject: [PATCH] cqlsh: add SHOW SESSION command patch by Mikhail Stepura; reviewed by Aleksey Yeschenko for CASSANDRA-6228 --- CHANGES.txt | 1 + bin/cqlsh | 14 ++++++++++++-- pylib/cqlshlib/cql3handling.py | 2 +- pylib/cqlshlib/tracing.py | 8 +++++++- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 47e52f1709..9f7f3f0bac 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,6 +1,7 @@ 2.0.3 * Reject bootstrapping if the node already exists in gossip (CASSANDRA-5571) * Fix NPE while loading paxos state (CASSANDRA-6211) + * cqlsh: add SHOW SESSION command (CASSANDRA-6228) Merged from 1.2: * Fix altering column types (CASSANDRA-6185) * cqlsh: fix CREATE/ALTER WITH completion (CASSANDRA-6196) diff --git a/bin/cqlsh b/bin/cqlsh index 82c9906f76..33821118c8 100755 --- a/bin/cqlsh +++ b/bin/cqlsh @@ -32,7 +32,7 @@ exit 1 from __future__ import with_statement description = "CQL Shell for Apache Cassandra" -version = "4.0.2" +version = "4.1.0" from StringIO import StringIO from itertools import groupby @@ -245,7 +245,7 @@ cqlsh_extra_syntax_rules = r''' | "EACH_QUORUM" ; - ::= "SHOW" what=( "VERSION" | "HOST" ) + ::= "SHOW" what=( "VERSION" | "HOST" | "SESSION" sessionid= ) ; ::= "SOURCE" fname= @@ -569,6 +569,9 @@ class Shell(cmd.Cmd): vers['cql'] = self.cql_version print "[cqlsh %(shver)s | Cassandra %(build)s | CQL spec %(cql)s | Thrift protocol %(thrift)s]" % vers + def show_session(self, sessionid): + print_trace_session(self, self.cursor, sessionid) + def get_connection_versions(self): self.cursor.execute("select * from system.local where key = 'local'") result = self.fetchdict() @@ -1554,6 +1557,10 @@ class Shell(cmd.Cmd): SHOW HOST Shows where cqlsh is currently connected. + + SHOW SESSION + + Pretty-prints the requested tracing session. """ showwhat = parsed.get_binding('what').lower() if showwhat == 'version': @@ -1561,6 +1568,9 @@ class Shell(cmd.Cmd): self.show_version() elif showwhat == 'host': self.show_host() + elif showwhat.startswith('session'): + session_id = parsed.get_binding('sessionid').lower() + self.show_session(session_id) else: self.printerr('Wait, how do I show %r?' % (showwhat,)) diff --git a/pylib/cqlshlib/cql3handling.py b/pylib/cqlshlib/cql3handling.py index bc349a7644..e7aa9e122a 100644 --- a/pylib/cqlshlib/cql3handling.py +++ b/pylib/cqlshlib/cql3handling.py @@ -177,8 +177,8 @@ JUNK ::= /([ \t\r\f\v]+|(--|[/][/])[^\n\r]*([\n\r]|$)|[/][*].*?[*][/])/ ; ::= /'([^']|'')*'/ ; ::= /"([^"]|"")*"/ ; ::= /-?[0-9]+\.[0-9]+/ ; - ::= /[0-9]+/ ; ::= /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/ ; + ::= /[0-9]+/ ; ::= /[a-z][a-z0-9_]*/ ; ::= ":" ; ::= "*" ; diff --git a/pylib/cqlshlib/tracing.py b/pylib/cqlshlib/tracing.py index fb8525d731..02d3296177 100644 --- a/pylib/cqlshlib/tracing.py +++ b/pylib/cqlshlib/tracing.py @@ -24,6 +24,9 @@ EVENTS_CF = 'events' def print_trace_session(shell, cursor, session_id): rows = fetch_trace_session(cursor, session_id) + if not rows: + shell.printerr("Session %s wasn't found." % session_id) + return names = ['activity', 'timestamp', 'source', 'source_elapsed'] types = [UTF8Type, UTF8Type, InetAddressType, Int32Type] @@ -42,7 +45,10 @@ def fetch_trace_session(cursor, session_id): "FROM %s.%s " "WHERE session_id = %s" % (TRACING_KS, SESSIONS_CF, session_id), consistency_level='ONE') - (request, coordinator, started_at, duration) = cursor.fetchone() + session = cursor.fetchone() + if not session: + return [] + (request, coordinator, started_at, duration) = session cursor.execute("SELECT activity, event_id, source, source_elapsed " "FROM %s.%s "