CQLSH: wait up to 10 sec for a tracing session. patch by Mikhail Stepura; reviewed by Tyler Hobbs for CASSANDRA-7222
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/115bbe43 Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/115bbe43 Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/115bbe43 Branch: refs/heads/cassandra-2.0 Commit: 115bbe435b6fe2cef99acc1cde2aa86f809de15d Parents: 563cea1 Author: Mikhail Stepura <[email protected]> Authored: Wed Aug 13 13:35:48 2014 -0700 Committer: Mikhail Stepura <[email protected]> Committed: Fri Aug 15 14:53:13 2014 -0700 ---------------------------------------------------------------------- CHANGES.txt | 1 + bin/cqlsh | 1 - pylib/cqlshlib/tracing.py | 58 +++++++++++++++++++++++------------------- 3 files changed, 33 insertions(+), 27 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/115bbe43/CHANGES.txt ---------------------------------------------------------------------- diff --git a/CHANGES.txt b/CHANGES.txt index e335484..987c227 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ 2.0.10 + * (cqlsh) Wait up to 10 sec for a tracing session (CASSANDRA-7222) * Fix NPE in FileCacheService.sizeInBytes (CASSANDRA-7756) * (cqlsh) cqlsh should automatically disable tracing when selecting from system_traces (CASSANDRA-7641) http://git-wip-us.apache.org/repos/asf/cassandra/blob/115bbe43/bin/cqlsh ---------------------------------------------------------------------- diff --git a/bin/cqlsh b/bin/cqlsh index 6aa397e..64d7bf5 100755 --- a/bin/cqlsh +++ b/bin/cqlsh @@ -911,7 +911,6 @@ class Shell(cmd.Cmd): result = self.perform_statement_untraced(statement, decoder=decoder, with_default_limit=with_default_limit) - time.sleep(0.5) # trace writes are async so we wait a little. print_trace_session(self, self.cursor, session_id) return result else: http://git-wip-us.apache.org/repos/asf/cassandra/blob/115bbe43/pylib/cqlshlib/tracing.py ---------------------------------------------------------------------- diff --git a/pylib/cqlshlib/tracing.py b/pylib/cqlshlib/tracing.py index fe0f71e..6dd4b14 100644 --- a/pylib/cqlshlib/tracing.py +++ b/pylib/cqlshlib/tracing.py @@ -21,9 +21,10 @@ from cqlshlib.displaying import MAGENTA TRACING_KS = 'system_traces' SESSIONS_CF = 'sessions' EVENTS_CF = 'events' +MAX_WAIT = 10.0 def print_trace_session(shell, cursor, session_id): - rows = fetch_trace_session(cursor, session_id) + rows = fetch_trace_session(cursor, session_id) if not rows: shell.printerr("Session %s wasn't found." % session_id) return @@ -41,35 +42,40 @@ def print_trace_session(shell, cursor, session_id): shell.writeresult('') def fetch_trace_session(cursor, session_id): - cursor.execute("SELECT request, coordinator, started_at, duration " - "FROM %s.%s " - "WHERE session_id = %s" % (TRACING_KS, SESSIONS_CF, session_id), - consistency_level='ONE') - 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 " - "WHERE session_id = %s" % (TRACING_KS, EVENTS_CF, session_id), - consistency_level='ONE') - events = cursor.fetchall() + start = time.time() + while True: + time_spent = time.time() - start + if time_spent >= MAX_WAIT: + return [] + cursor.execute("SELECT request, coordinator, started_at, duration " + "FROM %s.%s " + "WHERE session_id = %s" % (TRACING_KS, SESSIONS_CF, session_id), + consistency_level='ONE') + session = cursor.fetchone() - rows = [] - # append header row (from sessions table). - rows.append([request, format_timestamp(started_at), coordinator, 0]) - # append main rows (from events table). - for activity, event_id, source, source_elapsed in events: - rows.append([activity, format_timeuuid(event_id), source, source_elapsed]) - # append footer row (from sessions table). - if duration: + if not session or session[3] is None: #session[3] is a duration + time.sleep(0.5) + continue + + (request, coordinator, started_at, duration) = session + cursor.execute("SELECT activity, event_id, source, source_elapsed " + "FROM %s.%s " + "WHERE session_id = %s" % (TRACING_KS, EVENTS_CF, session_id), + consistency_level='ONE') + events = cursor.fetchall() + + rows = [] + # append header row (from sessions table). + rows.append([request, format_timestamp(started_at), coordinator, 0]) + # append main rows (from events table). + for activity, event_id, source, source_elapsed in events: + rows.append([activity, format_timeuuid(event_id), source, source_elapsed]) + # append footer row (from sessions table). finished_at = format_timestamp(started_at + (duration / 1000000.)) - else: - finished_at = duration = "--" - rows.append(['Request complete', finished_at, coordinator, duration]) + rows.append(['Request complete', finished_at, coordinator, duration]) - return rows + return rows def format_timestamp(value): return format_time(int(value * 1000))
