blob: c3088a94fd91ae825b3d50c3fe6173cc5b1730d4 [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
18
19
20# Number of seconds to wait before checking queue again for uploading data.
21_REPORT_INTERVAL_SECONDS = 5
22
23_MAX_METADATA_QUEUE_SIZE = 100000
24# Queue to buffer metadata to be reported.
25metadata_queue = Queue.Queue(_MAX_METADATA_QUEUE_SIZE)
26
27_report_lock = threading.Lock()
28_abort = threading.Event()
29_queue_full = threading.Event()
30
31def queue(data):
32 """Queue metadata to be uploaded in reporter thread.
33
34 If the queue is full, an error will be logged for the first time the queue
35 becomes full. The call does not wait or raise Queue.Full exception, so
36 there is no overhead on the performance of caller, e.g., scheduler.
37
38 @param data: A metadata entry, which should be a dictionary.
39 """
40 try:
41 metadata_queue.put_nowait(data)
42 if _queue_full.is_set():
43 logging.info('Metadata queue is available to receive new data '
44 'again.')
45 _queue_full.clear()
46 except Queue.Full:
47 if not _queue_full.is_set():
48 _queue_full.set()
49 logging.error('Metadata queue is full, cannot report data. '
50 'Consider increasing the value of '
51 '_MAX_METADATA_QUEUE_SIZE. Its current value is set '
52 'to %d.', _MAX_METADATA_QUEUE_SIZE)
53
54
55def _run():
56 """Report metadata in the queue until being aborted.
57 """
58 try:
59 while True:
60 start_time = time.time()
61 data_list = []
62 while (not metadata_queue.empty() and
63 len(data_list) < _MAX_METADATA_QUEUE_SIZE):
64 data_list.append(metadata_queue.get_nowait())
65 if data_list:
66 autotest_es.bulk_post(data_list=data_list)
67 logging.info('%d entries of metadata uploaded in %s '
68 'seconds.', len(data_list),
69 time.time()-start_time)
70 sleep_time = _REPORT_INTERVAL_SECONDS - time.time() + start_time
71 if sleep_time < 0:
72 sleep_time = 0.5
73 _abort.wait(timeout=sleep_time)
74 except Exception as e:
75 logging.error('Metadata reporter thread failed with error: %s', e)
76 raise
77 finally:
78 logging.info('Metadata reporting thread is exiting.')
79 _abort.clear()
80 _report_lock.release()
81
82
83def start():
84 """Start the thread to report metadata.
85 """
86 # The lock makes sure there is only one reporting thread working.
87 if _report_lock.locked():
88 logging.error('There is already a metadata reporter thread.')
89 return
90
91 _report_lock.acquire()
92 reporting_thread = threading.Thread(target=_run)
93 # Make it a daemon thread so it doesn't need to be closed explicitly.
94 reporting_thread.setDaemon(True)
95 reporting_thread.start()
96 logging.info('Metadata reporting thread is started.')
97
98
99def abort():
100 """Abort the thread to report metadata.
101
102 The call will wait up to 5 seconds for existing data to be uploaded.
103 """
104 if not _report_lock.locked():
105 logging.error('The metadata reporting thread has already exited.')
106 return
107
108 _abort.set()
109 logging.info('Waiting up to %s seconds for metadata reporting thread to '
110 'complete.', _REPORT_INTERVAL_SECONDS)
111 _abort.wait(_REPORT_INTERVAL_SECONDS)