blob: 45fedee633cce5730abbbbf92fe388fa747968e8 [file] [log] [blame]
Dan Shicf2e8dd2015-05-07 17:18:48 -07001# Copyright 2015 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""
6This module is designed to report metadata in a separated thread to avoid the
7performance overhead of sending data to Elasticsearch using HTTP.
8
9"""
10
11import logging
12import Queue
13import time
14import threading
15
16import common
17from autotest_lib.client.common_lib.cros.graphite import autotest_es
Dan Shi1e290c92015-05-11 12:54:48 -070018from autotest_lib.client.common_lib.cros.graphite import autotest_stats
Dan Shicf2e8dd2015-05-07 17:18:48 -070019
20
21# Number of seconds to wait before checking queue again for uploading data.
22_REPORT_INTERVAL_SECONDS = 5
23
24_MAX_METADATA_QUEUE_SIZE = 100000
25# Queue to buffer metadata to be reported.
26metadata_queue = Queue.Queue(_MAX_METADATA_QUEUE_SIZE)
27
28_report_lock = threading.Lock()
29_abort = threading.Event()
30_queue_full = threading.Event()
31
32def queue(data):
33 """Queue metadata to be uploaded in reporter thread.
34
35 If the queue is full, an error will be logged for the first time the queue
36 becomes full. The call does not wait or raise Queue.Full exception, so
37 there is no overhead on the performance of caller, e.g., scheduler.
38
39 @param data: A metadata entry, which should be a dictionary.
40 """
41 try:
42 metadata_queue.put_nowait(data)
43 if _queue_full.is_set():
44 logging.info('Metadata queue is available to receive new data '
45 'again.')
46 _queue_full.clear()
47 except Queue.Full:
48 if not _queue_full.is_set():
49 _queue_full.set()
50 logging.error('Metadata queue is full, cannot report data. '
51 'Consider increasing the value of '
52 '_MAX_METADATA_QUEUE_SIZE. Its current value is set '
53 'to %d.', _MAX_METADATA_QUEUE_SIZE)
54
55
56def _run():
57 """Report metadata in the queue until being aborted.
58 """
59 try:
60 while True:
61 start_time = time.time()
62 data_list = []
63 while (not metadata_queue.empty() and
64 len(data_list) < _MAX_METADATA_QUEUE_SIZE):
65 data_list.append(metadata_queue.get_nowait())
66 if data_list:
67 autotest_es.bulk_post(data_list=data_list)
Dan Shi1e290c92015-05-11 12:54:48 -070068 time_used = time.time() - start_time
Dan Shicf2e8dd2015-05-07 17:18:48 -070069 logging.info('%d entries of metadata uploaded in %s '
Dan Shi1e290c92015-05-11 12:54:48 -070070 'seconds.', len(data_list), time_used)
71 autotest_stats.Timer('metadata_reporter').send(
72 'time_used', time_used)
73 autotest_stats.Counter('metadata_reporter').send(
74 'entries_uploaded', len(data_list))
Dan Shicf2e8dd2015-05-07 17:18:48 -070075 sleep_time = _REPORT_INTERVAL_SECONDS - time.time() + start_time
76 if sleep_time < 0:
77 sleep_time = 0.5
78 _abort.wait(timeout=sleep_time)
79 except Exception as e:
80 logging.error('Metadata reporter thread failed with error: %s', e)
81 raise
82 finally:
83 logging.info('Metadata reporting thread is exiting.')
84 _abort.clear()
85 _report_lock.release()
86
87
88def start():
89 """Start the thread to report metadata.
90 """
91 # The lock makes sure there is only one reporting thread working.
92 if _report_lock.locked():
93 logging.error('There is already a metadata reporter thread.')
94 return
95
96 _report_lock.acquire()
97 reporting_thread = threading.Thread(target=_run)
98 # Make it a daemon thread so it doesn't need to be closed explicitly.
99 reporting_thread.setDaemon(True)
100 reporting_thread.start()
101 logging.info('Metadata reporting thread is started.')
102
103
104def abort():
105 """Abort the thread to report metadata.
106
107 The call will wait up to 5 seconds for existing data to be uploaded.
108 """
109 if not _report_lock.locked():
110 logging.error('The metadata reporting thread has already exited.')
111 return
112
113 _abort.set()
114 logging.info('Waiting up to %s seconds for metadata reporting thread to '
115 'complete.', _REPORT_INTERVAL_SECONDS)
116 _abort.wait(_REPORT_INTERVAL_SECONDS)