Example #1
0
  def fetch_query_results(self, query):
    '''Concurrently execute the query using each cursor and return a list of tuples
       containing the result information for each cursor. The tuple format is
       (<exception or None>, <data set or None>).

       If query_timeout_seconds is reached and the connection is killable then the
       query will be cancelled and the connection reset. Otherwise the query will
       continue to run in the background.

       "query" should be an instance of query.Query.
    '''
    if query.execution != 'RAW':
      self._table_or_view_name = self._create_random_table_name()

    query_threads = list()
    for sql_writer, cursor, log_file \
        in izip(self.sql_writers, self.cursors, self.query_logs):
      if cursor.db_type == IMPALA:
        self.set_impala_query_optons(cursor)
      query_thread = Thread(
          target=self._fetch_sql_results,
          args=[query, cursor, sql_writer, log_file],
          name='Query execution thread {0}'.format(current_thread().name))
      query_thread.daemon = True
      query_thread.sql = ''
      query_thread.data_set = None
      query_thread.cursor_description = None
      query_thread.exception = None
      query_thread.start()
      query_threads.append(query_thread)

    end_time = time() + self.query_timeout_seconds
    for query_thread, cursor in izip(query_threads, self.cursors):
      join_time = end_time - time()
      if join_time > 0:
        query_thread.join(join_time)
      if query_thread.is_alive():
        # Kill connection and reconnect to return cursor to initial state.
        if cursor.conn.supports_kill:
          LOG.debug('Attempting to kill connection')
          cursor.conn.kill()
          LOG.debug('Kill connection')
        try:
          # XXX: Sometimes this takes a very long time causing the program to appear to
          #      hang. Maybe this should be done in another thread so a timeout can be
          #      applied?
          cursor.close()
        except Exception as e:
          LOG.info('Error closing cursor: %s', e)
        cursor.reconnect()
        query_thread.exception = QueryTimeout(
            'Query timed out after %s seconds' % self.query_timeout_seconds)

    return [(query_thread.sql,
        query_thread.exception,
        query_thread.data_set,
        query_thread.cursor_description) for query_thread in query_threads]
  def fetch_query_results(self, query):
    '''Concurrently execute the query using each cursor and return a list of tuples
       containing the result information for each cursor. The tuple format is
       (<exception or None>, <data set or None>).

       If query_timeout_seconds is reached and the connection is killable then the
       query will be cancelled and the connection reset. Otherwise the query will
       continue to run in the background.

       "query" should be an instance of query.Query.
    '''
    if query.execution != 'RAW':
      self._table_or_view_name = self._create_random_table_name()

    query_threads = list()
    for sql_writer, cursor, log_file \
        in izip(self.sql_writers, self.cursors, self.query_logs):
      if self.ENABLE_RANDOM_QUERY_OPTIONS and cursor.db_type == IMPALA:
        self.set_impala_query_options(cursor)
      query_thread = Thread(
          target=self._fetch_sql_results,
          args=[query, cursor, sql_writer, log_file],
          name='Query execution thread {0}'.format(current_thread().name))
      query_thread.daemon = True
      query_thread.sql = ''
      query_thread.data_set = None
      query_thread.cursor_description = None
      query_thread.exception = None
      query_thread.start()
      query_threads.append(query_thread)

    end_time = time() + self.query_timeout_seconds
    for query_thread, cursor in izip(query_threads, self.cursors):
      join_time = end_time - time()
      if join_time > 0:
        query_thread.join(join_time)
      if query_thread.is_alive():
        # Kill connection and reconnect to return cursor to initial state.
        if cursor.conn.supports_kill:
          LOG.debug('Attempting to kill connection')
          cursor.conn.kill()
          LOG.debug('Kill connection')
        try:
          # XXX: Sometimes this takes a very long time causing the program to appear to
          #      hang. Maybe this should be done in another thread so a timeout can be
          #      applied?
          cursor.close()
        except Exception as e:
          LOG.info('Error closing cursor: %s', e)
        cursor.reconnect()
        query_thread.exception = QueryTimeout(
            'Query timed out after %s seconds' % self.query_timeout_seconds)

    return [(query_thread.sql,
        query_thread.exception,
        query_thread.data_set,
        query_thread.cursor_description) for query_thread in query_threads]