CQLSH: wait up to 10 sec for a tracing session.

patch by Mikhail Stepura; reviewed by Tyler Hobbs for CASSANDRA-7222
This commit is contained in:
Mikhail Stepura 2014-08-13 13:35:48 -07:00
parent 563cea14b4
commit 115bbe435b
3 changed files with 33 additions and 27 deletions

View File

@ -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)

View File

@ -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:

View File

@ -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))