blob: 74e2501e4881962789f4af72234375b5ae52db7b [file] [log] [blame]
mbligh96cf0512008-04-17 15:25:38 +00001#!/usr/bin/python -u
mblighc2514542008-02-19 15:54:26 +00002
Aviv Keshet687d2dc2016-10-20 15:41:16 -07003import collections
Fang Deng49822682014-10-21 16:29:22 -07004import datetime
Aviv Keshet687d2dc2016-10-20 15:41:16 -07005import errno
6import fcntl
Simran Basi1e10e922015-04-16 15:09:56 -07007import json
Aviv Keshet687d2dc2016-10-20 15:41:16 -07008import optparse
9import os
10import socket
Shuqian Zhao31425d52016-12-07 09:35:03 -080011import subprocess
Aviv Keshet687d2dc2016-10-20 15:41:16 -070012import sys
Dan Shi11e35062017-11-03 10:09:05 -070013import time
Aviv Keshet687d2dc2016-10-20 15:41:16 -070014import traceback
mblighbb7b8912006-10-08 03:59:02 +000015
mbligh96cf0512008-04-17 15:25:38 +000016import common
Dan Shi4f8c0242017-07-07 15:34:49 -070017from autotest_lib.client.bin.result_tools import utils as result_utils
18from autotest_lib.client.bin.result_tools import utils_lib as result_utils_lib
19from autotest_lib.client.bin.result_tools import runner as result_runner
20from autotest_lib.client.common_lib import control_data
Benny Peakefeb775c2017-02-08 15:14:14 -080021from autotest_lib.client.common_lib import global_config
jadmanskidb4f9b52008-12-03 22:52:53 +000022from autotest_lib.client.common_lib import mail, pidfile
Fang Deng49822682014-10-21 16:29:22 -070023from autotest_lib.client.common_lib import utils
Fang Deng49822682014-10-21 16:29:22 -070024from autotest_lib.frontend import setup_django_environment
Fang Deng9ec66802014-04-28 19:04:33 +000025from autotest_lib.frontend.tko import models as tko_models
Shuqian Zhao19e62fb2017-01-09 10:10:14 -080026from autotest_lib.server import site_utils
Fang Deng49822682014-10-21 16:29:22 -070027from autotest_lib.server.cros.dynamic_suite import constants
Benny Peaked322d3d2017-02-08 15:39:28 -080028from autotest_lib.site_utils.sponge_lib import sponge_utils
Dennis Jeffreyf9bef6c2013-08-05 11:01:27 -070029from autotest_lib.tko import db as tko_db, utils as tko_utils
Luigi Semenzatoe7064812017-02-03 14:47:59 -080030from autotest_lib.tko import models, parser_lib
Dennis Jeffreyf9bef6c2013-08-05 11:01:27 -070031from autotest_lib.tko.perf_upload import perf_uploader
mbligh74fc0462007-11-05 20:24:17 +000032
Dan Shib0af6212017-07-17 14:40:02 -070033try:
34 from chromite.lib import metrics
35except ImportError:
36 metrics = utils.metrics_mock
37
38
Aviv Keshet687d2dc2016-10-20 15:41:16 -070039_ParseOptions = collections.namedtuple(
Shuqian Zhao19e62fb2017-01-09 10:10:14 -080040 'ParseOptions', ['reparse', 'mail_on_failure', 'dry_run', 'suite_report',
41 'datastore_creds', 'export_to_gcloud_path'])
Aviv Keshet687d2dc2016-10-20 15:41:16 -070042
mbligh96cf0512008-04-17 15:25:38 +000043def parse_args():
Fang Deng49822682014-10-21 16:29:22 -070044 """Parse args."""
jadmanski0afbb632008-06-06 21:10:57 +000045 # build up our options parser and parse sys.argv
46 parser = optparse.OptionParser()
47 parser.add_option("-m", help="Send mail for FAILED tests",
48 dest="mailit", action="store_true")
49 parser.add_option("-r", help="Reparse the results of a job",
50 dest="reparse", action="store_true")
51 parser.add_option("-o", help="Parse a single results directory",
52 dest="singledir", action="store_true")
53 parser.add_option("-l", help=("Levels of subdirectories to include "
54 "in the job name"),
55 type="int", dest="level", default=1)
56 parser.add_option("-n", help="No blocking on an existing parse",
57 dest="noblock", action="store_true")
58 parser.add_option("-s", help="Database server hostname",
59 dest="db_host", action="store")
60 parser.add_option("-u", help="Database username", dest="db_user",
61 action="store")
62 parser.add_option("-p", help="Database password", dest="db_pass",
63 action="store")
64 parser.add_option("-d", help="Database name", dest="db_name",
65 action="store")
Aviv Keshet0b7bab02016-10-20 17:17:36 -070066 parser.add_option("--dry-run", help="Do not actually commit any results.",
67 dest="dry_run", action="store_true", default=False)
Prathmesh Prabhu3e319da2017-08-30 19:13:03 -070068 parser.add_option(
69 "--detach", action="store_true",
70 help="Detach parsing process from the caller process. Used by "
71 "monitor_db to safely restart without affecting parsing.",
72 default=False)
jadmanskid5ab8c52008-12-03 16:27:07 +000073 parser.add_option("--write-pidfile",
74 help="write pidfile (.parser_execute)",
75 dest="write_pidfile", action="store_true",
76 default=False)
Fang Deng49822682014-10-21 16:29:22 -070077 parser.add_option("--record-duration",
Prathmesh Prabhu77769452018-04-17 13:30:50 -070078 help="[DEPRECATED] Record timing to metadata db",
Fang Deng49822682014-10-21 16:29:22 -070079 dest="record_duration", action="store_true",
80 default=False)
Shuqian Zhao31425d52016-12-07 09:35:03 -080081 parser.add_option("--suite-report",
82 help=("Allows parsing job to attempt to create a suite "
Shuqian Zhao19e62fb2017-01-09 10:10:14 -080083 "timeline report, if it detects that the job being "
Shuqian Zhao31425d52016-12-07 09:35:03 -080084 "parsed is a suite job."),
85 dest="suite_report", action="store_true",
86 default=False)
Shuqian Zhao19e62fb2017-01-09 10:10:14 -080087 parser.add_option("--datastore-creds",
88 help=("The path to gcloud datastore credentials file, "
89 "which will be used to upload suite timeline "
90 "report to gcloud. If not specified, the one "
91 "defined in shadow_config will be used."),
92 dest="datastore_creds", action="store", default=None)
93 parser.add_option("--export-to-gcloud-path",
94 help=("The path to export_to_gcloud script. Please find "
95 "chromite path on your server. The script is under "
96 "chromite/bin/."),
97 dest="export_to_gcloud_path", action="store",
98 default=None)
jadmanski0afbb632008-06-06 21:10:57 +000099 options, args = parser.parse_args()
mbligh74fc0462007-11-05 20:24:17 +0000100
jadmanski0afbb632008-06-06 21:10:57 +0000101 # we need a results directory
102 if len(args) == 0:
103 tko_utils.dprint("ERROR: at least one results directory must "
104 "be provided")
105 parser.print_help()
106 sys.exit(1)
mbligh74fc0462007-11-05 20:24:17 +0000107
Shuqian Zhao19e62fb2017-01-09 10:10:14 -0800108 if not options.datastore_creds:
109 gcloud_creds = global_config.global_config.get_config_value(
110 'GCLOUD', 'cidb_datastore_writer_creds', default=None)
111 options.datastore_creds = (site_utils.get_creds_abspath(gcloud_creds)
112 if gcloud_creds else None)
113
114 if not options.export_to_gcloud_path:
115 export_script = 'chromiumos/chromite/bin/export_to_gcloud'
116 # If it is a lab server, the script is under ~chromeos-test/
117 if os.path.exists(os.path.expanduser('~chromeos-test/%s' %
118 export_script)):
119 path = os.path.expanduser('~chromeos-test/%s' % export_script)
120 # If it is a local workstation, it is probably under ~/
121 elif os.path.exists(os.path.expanduser('~/%s' % export_script)):
122 path = os.path.expanduser('~/%s' % export_script)
123 # If it is not found anywhere, the default will be set to None.
124 else:
125 path = None
126 options.export_to_gcloud_path = path
127
jadmanski0afbb632008-06-06 21:10:57 +0000128 # pass the options back
129 return options, args
mbligh74fc0462007-11-05 20:24:17 +0000130
131
mbligh96cf0512008-04-17 15:25:38 +0000132def format_failure_message(jobname, kernel, testname, status, reason):
Fang Deng49822682014-10-21 16:29:22 -0700133 """Format failure message with the given information.
134
135 @param jobname: String representing the job name.
136 @param kernel: String representing the kernel.
137 @param testname: String representing the test name.
138 @param status: String representing the test status.
139 @param reason: String representing the reason.
140
141 @return: Failure message as a string.
142 """
jadmanski0afbb632008-06-06 21:10:57 +0000143 format_string = "%-12s %-20s %-12s %-10s %s"
144 return format_string % (jobname, kernel, testname, status, reason)
mblighb85e6b02006-10-08 17:20:56 +0000145
mblighbb7b8912006-10-08 03:59:02 +0000146
mbligh96cf0512008-04-17 15:25:38 +0000147def mailfailure(jobname, job, message):
Fang Deng49822682014-10-21 16:29:22 -0700148 """Send an email about the failure.
149
150 @param jobname: String representing the job name.
151 @param job: A job object.
152 @param message: The message to mail.
153 """
jadmanski0afbb632008-06-06 21:10:57 +0000154 message_lines = [""]
155 message_lines.append("The following tests FAILED for this job")
156 message_lines.append("http://%s/results/%s" %
157 (socket.gethostname(), jobname))
158 message_lines.append("")
159 message_lines.append(format_failure_message("Job name", "Kernel",
160 "Test name", "FAIL/WARN",
161 "Failure reason"))
162 message_lines.append(format_failure_message("=" * 8, "=" * 6, "=" * 8,
163 "=" * 8, "=" * 14))
164 message_header = "\n".join(message_lines)
mbligh96cf0512008-04-17 15:25:38 +0000165
jadmanski0afbb632008-06-06 21:10:57 +0000166 subject = "AUTOTEST: FAILED tests from job %s" % jobname
167 mail.send("", job.user, "", subject, message_header + message)
mbligh006f2302007-09-13 20:46:46 +0000168
169
Fang Deng9ec66802014-04-28 19:04:33 +0000170def _invalidate_original_tests(orig_job_idx, retry_job_idx):
171 """Retry tests invalidates original tests.
172
173 Whenever a retry job is complete, we want to invalidate the original
174 job's test results, such that the consumers of the tko database
175 (e.g. tko frontend, wmatrix) could figure out which results are the latest.
176
177 When a retry job is parsed, we retrieve the original job's afe_job_id
178 from the retry job's keyvals, which is then converted to tko job_idx and
179 passed into this method as |orig_job_idx|.
180
181 In this method, we are going to invalidate the rows in tko_tests that are
182 associated with the original job by flipping their 'invalid' bit to True.
183 In addition, in tko_tests, we also maintain a pointer from the retry results
184 to the original results, so that later we can always know which rows in
185 tko_tests are retries and which are the corresponding original results.
186 This is done by setting the field 'invalidates_test_idx' of the tests
187 associated with the retry job.
188
189 For example, assume Job(job_idx=105) are retried by Job(job_idx=108), after
190 this method is run, their tko_tests rows will look like:
191 __________________________________________________________________________
192 test_idx| job_idx | test | ... | invalid | invalidates_test_idx
193 10 | 105 | dummy_Fail.Error| ... | 1 | NULL
194 11 | 105 | dummy_Fail.Fail | ... | 1 | NULL
195 ...
196 20 | 108 | dummy_Fail.Error| ... | 0 | 10
197 21 | 108 | dummy_Fail.Fail | ... | 0 | 11
198 __________________________________________________________________________
199 Note the invalid bits of the rows for Job(job_idx=105) are set to '1'.
200 And the 'invalidates_test_idx' fields of the rows for Job(job_idx=108)
201 are set to 10 and 11 (the test_idx of the rows for the original job).
202
203 @param orig_job_idx: An integer representing the original job's
204 tko job_idx. Tests associated with this job will
205 be marked as 'invalid'.
206 @param retry_job_idx: An integer representing the retry job's
207 tko job_idx. The field 'invalidates_test_idx'
208 of the tests associated with this job will be updated.
209
210 """
211 msg = 'orig_job_idx: %s, retry_job_idx: %s' % (orig_job_idx, retry_job_idx)
212 if not orig_job_idx or not retry_job_idx:
213 tko_utils.dprint('ERROR: Could not invalidate tests: ' + msg)
214 # Using django models here makes things easier, but make sure that
215 # before this method is called, all other relevant transactions have been
216 # committed to avoid race condition. In the long run, we might consider
217 # to make the rest of parser use django models.
218 orig_tests = tko_models.Test.objects.filter(job__job_idx=orig_job_idx)
219 retry_tests = tko_models.Test.objects.filter(job__job_idx=retry_job_idx)
220
221 # Invalidate original tests.
222 orig_tests.update(invalid=True)
223
224 # Maintain a dictionary that maps (test, subdir) to original tests.
225 # Note that within the scope of a job, (test, subdir) uniquelly
226 # identifies a test run, but 'test' does not.
227 # In a control file, one could run the same test with different
228 # 'subdir_tag', for example,
229 # job.run_test('dummy_Fail', tag='Error', subdir_tag='subdir_1')
230 # job.run_test('dummy_Fail', tag='Error', subdir_tag='subdir_2')
231 # In tko, we will get
232 # (test='dummy_Fail.Error', subdir='dummy_Fail.Error.subdir_1')
233 # (test='dummy_Fail.Error', subdir='dummy_Fail.Error.subdir_2')
234 invalidated_tests = {(orig_test.test, orig_test.subdir): orig_test
235 for orig_test in orig_tests}
236 for retry in retry_tests:
237 # It is possible that (retry.test, retry.subdir) doesn't exist
238 # in invalidated_tests. This could happen when the original job
239 # didn't run some of its tests. For example, a dut goes offline
240 # since the beginning of the job, in which case invalidated_tests
241 # will only have one entry for 'SERVER_JOB'.
242 orig_test = invalidated_tests.get((retry.test, retry.subdir), None)
243 if orig_test:
244 retry.invalidates_test = orig_test
245 retry.save()
246 tko_utils.dprint('DEBUG: Invalidated tests associated to job: ' + msg)
247
248
Dan Shi4f8c0242017-07-07 15:34:49 -0700249def _throttle_result_size(path):
250 """Limit the total size of test results for the given path.
251
252 @param path: Path of the result directory.
253 """
254 if not result_runner.ENABLE_RESULT_THROTTLING:
255 tko_utils.dprint(
256 'Result throttling is not enabled. Skipping throttling %s' %
257 path)
258 return
259
260 max_result_size_KB = control_data.DEFAULT_MAX_RESULT_SIZE_KB
261 # Client side test saves the test control to file `control`, while server
262 # side test saves the test control to file `control.srv`
263 for control_file in ['control', 'control.srv']:
264 control = os.path.join(path, control_file)
265 try:
266 max_result_size_KB = control_data.parse_control(
267 control, raise_warnings=False).max_result_size_KB
268 # Any value different from the default is considered to be the one
269 # set in the test control file.
270 if max_result_size_KB != control_data.DEFAULT_MAX_RESULT_SIZE_KB:
271 break
272 except IOError as e:
273 tko_utils.dprint(
274 'Failed to access %s. Error: %s\nDetails %s' %
275 (control, e, traceback.format_exc()))
276 except control_data.ControlVariableException as e:
277 tko_utils.dprint(
278 'Failed to parse %s. Error: %s\nDetails %s' %
279 (control, e, traceback.format_exc()))
280
281 try:
282 result_utils.execute(path, max_result_size_KB)
283 except:
284 tko_utils.dprint(
285 'Failed to throttle result size of %s.\nDetails %s' %
286 (path, traceback.format_exc()))
287
288
Michael Tangc89efa72017-08-03 14:27:10 -0700289def export_tko_job_to_file(job, jobname, filename):
290 """Exports the tko job to disk file.
291
292 @param job: database object.
293 @param jobname: the job name as string.
294 @param filename: The path to the results to be parsed.
295 """
296 try:
297 from autotest_lib.tko import job_serializer
298
299 serializer = job_serializer.JobSerializer()
300 serializer.serialize_to_binary(job, jobname, filename)
301 except ImportError:
302 tko_utils.dprint("WARNING: tko_pb2.py doesn't exist. Create by "
303 "compiling tko/tko.proto.")
304
305
Prathmesh Prabhub1241d12018-04-19 18:09:43 -0700306def parse_one(db, pid_file_manager, jobname, path, parse_options):
Fang Deng49822682014-10-21 16:29:22 -0700307 """Parse a single job. Optionally send email on failure.
308
309 @param db: database object.
Prathmesh Prabhub1241d12018-04-19 18:09:43 -0700310 @param pid_file_manager: pidfile.PidFileManager object.
Fang Deng49822682014-10-21 16:29:22 -0700311 @param jobname: the tag used to search for existing job in db,
312 e.g. '1234-chromeos-test/host1'
313 @param path: The path to the results to be parsed.
Aviv Keshet687d2dc2016-10-20 15:41:16 -0700314 @param parse_options: _ParseOptions instance.
jadmanski0afbb632008-06-06 21:10:57 +0000315 """
Aviv Keshet687d2dc2016-10-20 15:41:16 -0700316 reparse = parse_options.reparse
317 mail_on_failure = parse_options.mail_on_failure
Aviv Keshet0b7bab02016-10-20 17:17:36 -0700318 dry_run = parse_options.dry_run
Shuqian Zhao31425d52016-12-07 09:35:03 -0800319 suite_report = parse_options.suite_report
Shuqian Zhao19e62fb2017-01-09 10:10:14 -0800320 datastore_creds = parse_options.datastore_creds
321 export_to_gcloud_path = parse_options.export_to_gcloud_path
Aviv Keshet687d2dc2016-10-20 15:41:16 -0700322
jadmanski0afbb632008-06-06 21:10:57 +0000323 tko_utils.dprint("\nScanning %s (%s)" % (jobname, path))
jadmanski9b6babf2009-04-21 17:57:40 +0000324 old_job_idx = db.find_job(jobname)
Prathmesh Prabhuedac1ee2018-04-18 19:16:34 -0700325 if old_job_idx is not None and not reparse:
326 tko_utils.dprint("! Job is already parsed, done")
327 return
mbligh96cf0512008-04-17 15:25:38 +0000328
jadmanski0afbb632008-06-06 21:10:57 +0000329 # look up the status version
jadmanskidb4f9b52008-12-03 22:52:53 +0000330 job_keyval = models.job.read_keyval(path)
331 status_version = job_keyval.get("status_version", 0)
jadmanski6e8bf752008-05-14 00:17:48 +0000332
Luigi Semenzatoe7064812017-02-03 14:47:59 -0800333 parser = parser_lib.parser(status_version)
jadmanski0afbb632008-06-06 21:10:57 +0000334 job = parser.make_job(path)
Prathmesh Prabhue06c49b2018-04-18 19:01:23 -0700335 tko_utils.dprint("+ Parsing dir=%s, jobname=%s" % (path, jobname))
336 status_log_path = _find_status_log_path(path)
337 if not status_log_path:
jadmanski0afbb632008-06-06 21:10:57 +0000338 tko_utils.dprint("! Unable to parse job, no status file")
339 return
Prathmesh Prabhue06c49b2018-04-18 19:01:23 -0700340 _parse_status_log(parser, job, status_log_path)
jadmanski9b6babf2009-04-21 17:57:40 +0000341
Prathmesh Prabhuedac1ee2018-04-18 19:16:34 -0700342 if old_job_idx is not None:
343 job.job_idx = old_job_idx
344 unmatched_tests = _match_existing_tests(db, job)
Aviv Keshet0b7bab02016-10-20 17:17:36 -0700345 if not dry_run:
Prathmesh Prabhuedac1ee2018-04-18 19:16:34 -0700346 _delete_tests_from_db(db, unmatched_tests)
mbligh96cf0512008-04-17 15:25:38 +0000347
Prathmesh Prabhu30dee862018-04-18 20:24:20 -0700348 job.afe_job_id = tko_utils.get_afe_job_id(jobname)
Prathmesh Prabhu17905882018-04-18 22:09:08 -0700349 job.skylab_task_id = tko_utils.get_skylab_task_id(jobname)
Prathmesh Prabhud25f15a2018-05-03 13:49:58 -0700350 job.afe_parent_job_id = job_keyval.get(constants.PARENT_JOB_ID)
351 job.skylab_parent_task_id = job_keyval.get(constants.PARENT_JOB_ID)
Benny Peakefeb775c2017-02-08 15:14:14 -0800352 job.build = None
353 job.board = None
354 job.build_version = None
355 job.suite = None
356 if job.label:
357 label_info = site_utils.parse_job_name(job.label)
358 if label_info:
359 job.build = label_info.get('build', None)
360 job.build_version = label_info.get('build_version', None)
361 job.board = label_info.get('board', None)
362 job.suite = label_info.get('suite', None)
363
Dan Shi4f8c0242017-07-07 15:34:49 -0700364 result_utils_lib.LOG = tko_utils.dprint
365 _throttle_result_size(path)
366
Dan Shiffd5b822017-07-14 11:16:23 -0700367 # Record test result size to job_keyvals
Dan Shi11e35062017-11-03 10:09:05 -0700368 start_time = time.time()
Dan Shiffd5b822017-07-14 11:16:23 -0700369 result_size_info = site_utils.collect_result_sizes(
370 path, log=tko_utils.dprint)
Dan Shi11e35062017-11-03 10:09:05 -0700371 tko_utils.dprint('Finished collecting result sizes after %s seconds' %
372 (time.time()-start_time))
Dan Shiffd5b822017-07-14 11:16:23 -0700373 job.keyval_dict.update(result_size_info.__dict__)
374
Dan Shiffd5b822017-07-14 11:16:23 -0700375 # TODO(dshi): Update sizes with sponge_invocation.xml and throttle it.
Dan Shi96c3bdc2017-05-24 11:34:30 -0700376
jadmanski0afbb632008-06-06 21:10:57 +0000377 # check for failures
378 message_lines = [""]
Simran Basi1e10e922015-04-16 15:09:56 -0700379 job_successful = True
jadmanski0afbb632008-06-06 21:10:57 +0000380 for test in job.tests:
381 if not test.subdir:
382 continue
Sida Liuafe550a2017-09-03 19:03:40 -0700383 tko_utils.dprint("* testname, subdir, status, reason: %s %s %s %s"
384 % (test.testname, test.subdir, test.status,
385 test.reason))
Simran Basi1e10e922015-04-16 15:09:56 -0700386 if test.status != 'GOOD':
387 job_successful = False
Prathmesh Prabhub1241d12018-04-19 18:09:43 -0700388 pid_file_manager.num_tests_failed += 1
jadmanski0afbb632008-06-06 21:10:57 +0000389 message_lines.append(format_failure_message(
390 jobname, test.kernel.base, test.subdir,
391 test.status, test.reason))
Simran Basi59ca5ac2016-09-22 16:57:56 -0700392 try:
393 message = "\n".join(message_lines)
Simran Basi1e10e922015-04-16 15:09:56 -0700394
Aviv Keshet0b7bab02016-10-20 17:17:36 -0700395 if not dry_run:
396 # send out a email report of failure
397 if len(message) > 2 and mail_on_failure:
398 tko_utils.dprint("Sending email report of failure on %s to %s"
399 % (jobname, job.user))
400 mailfailure(jobname, job, message)
mbligh96cf0512008-04-17 15:25:38 +0000401
Dan Shie5d063f2017-09-29 15:37:34 -0700402 # Upload perf values to the perf dashboard, if applicable.
403 for test in job.tests:
404 perf_uploader.upload_test(job, test, jobname)
405
406 # Upload job details to Sponge.
407 sponge_url = sponge_utils.upload_results(job, log=tko_utils.dprint)
408 if sponge_url:
409 job.keyval_dict['sponge_url'] = sponge_url
410
Prathmesh Prabhu30dee862018-04-18 20:24:20 -0700411 _write_job_to_db(db, jobname, job)
mbligh96cf0512008-04-17 15:25:38 +0000412
Dan Shib0af6212017-07-17 14:40:02 -0700413 # Verify the job data is written to the database.
414 if job.tests:
Prathmesh Prabhuc2a8a6a2018-04-19 16:23:32 -0700415 tests_in_db = db.find_tests(job.job_idx)
Dan Shib0af6212017-07-17 14:40:02 -0700416 tests_in_db_count = len(tests_in_db) if tests_in_db else 0
417 if tests_in_db_count != len(job.tests):
418 tko_utils.dprint(
419 'Failed to find enough tests for job_idx: %d. The '
420 'job should have %d tests, only found %d tests.' %
Prathmesh Prabhuc2a8a6a2018-04-19 16:23:32 -0700421 (job.job_idx, len(job.tests), tests_in_db_count))
Dan Shib0af6212017-07-17 14:40:02 -0700422 metrics.Counter(
423 'chromeos/autotest/result/db_save_failure',
424 description='The number of times parse failed to '
425 'save job to TKO database.').increment()
426
Aviv Keshet0b7bab02016-10-20 17:17:36 -0700427 # Although the cursor has autocommit, we still need to force it to
428 # commit existing changes before we can use django models, otherwise
429 # it will go into deadlock when django models try to start a new
430 # trasaction while the current one has not finished yet.
431 db.commit()
Dennis Jeffreyf9bef6c2013-08-05 11:01:27 -0700432
Aviv Keshet0b7bab02016-10-20 17:17:36 -0700433 # Handle retry job.
434 orig_afe_job_id = job_keyval.get(constants.RETRY_ORIGINAL_JOB_ID,
435 None)
436 if orig_afe_job_id:
437 orig_job_idx = tko_models.Job.objects.get(
438 afe_job_id=orig_afe_job_id).job_idx
Prathmesh Prabhuc2a8a6a2018-04-19 16:23:32 -0700439 _invalidate_original_tests(orig_job_idx, job.job_idx)
Simran Basi59ca5ac2016-09-22 16:57:56 -0700440 except Exception as e:
Simran Basi59ca5ac2016-09-22 16:57:56 -0700441 tko_utils.dprint("Hit exception while uploading to tko db:\n%s" %
442 traceback.format_exc())
Simran Basi59ca5ac2016-09-22 16:57:56 -0700443 raise e
Fang Deng9ec66802014-04-28 19:04:33 +0000444
jamesren7a522042010-06-10 22:53:55 +0000445 # Serializing job into a binary file
Michael Tangc89efa72017-08-03 14:27:10 -0700446 export_tko_to_file = global_config.global_config.get_config_value(
447 'AUTOSERV', 'export_tko_job_to_file', type=bool, default=False)
Michael Tang8303a372017-08-11 11:03:50 -0700448
449 binary_file_name = os.path.join(path, "job.serialize")
Michael Tangc89efa72017-08-03 14:27:10 -0700450 if export_tko_to_file:
Michael Tangc89efa72017-08-03 14:27:10 -0700451 export_tko_job_to_file(job, jobname, binary_file_name)
jamesren4826cc42010-06-15 20:33:22 +0000452
Aviv Keshet0b7bab02016-10-20 17:17:36 -0700453 if not dry_run:
454 db.commit()
mbligh26b992b2008-02-19 15:46:21 +0000455
Shuqian Zhao31425d52016-12-07 09:35:03 -0800456 # Generate a suite report.
457 # Check whether this is a suite job, a suite job will be a hostless job, its
458 # jobname will be <JOB_ID>-<USERNAME>/hostless, the suite field will not be
Shuqian Zhaoa42bba12017-03-10 14:20:11 -0800459 # NULL. Only generate timeline report when datastore_parent_key is given.
Shuqian Zhao31425d52016-12-07 09:35:03 -0800460 try:
Shuqian Zhaoa42bba12017-03-10 14:20:11 -0800461 datastore_parent_key = job_keyval.get('datastore_parent_key', None)
Ningning Xiabbba11f2018-03-16 13:35:24 -0700462 provision_job_id = job_keyval.get('provision_job_id', None)
Shuqian Zhaoa42bba12017-03-10 14:20:11 -0800463 if (suite_report and jobname.endswith('/hostless')
Prathmesh Prabhu6d4d8b62018-04-18 18:24:54 -0700464 and job.suite and datastore_parent_key):
Shuqian Zhao31425d52016-12-07 09:35:03 -0800465 tko_utils.dprint('Start dumping suite timing report...')
466 timing_log = os.path.join(path, 'suite_timing.log')
467 dump_cmd = ("%s/site_utils/dump_suite_report.py %s "
468 "--output='%s' --debug" %
Prathmesh Prabhu6d4d8b62018-04-18 18:24:54 -0700469 (common.autotest_dir, job.afe_job_id,
Shuqian Zhao31425d52016-12-07 09:35:03 -0800470 timing_log))
Ningning Xiabbba11f2018-03-16 13:35:24 -0700471
472 if provision_job_id is not None:
473 dump_cmd += " --provision_job_id=%d" % int(provision_job_id)
474
Shuqian Zhao31425d52016-12-07 09:35:03 -0800475 subprocess.check_output(dump_cmd, shell=True)
476 tko_utils.dprint('Successfully finish dumping suite timing report')
477
Shuqian Zhao19e62fb2017-01-09 10:10:14 -0800478 if (datastore_creds and export_to_gcloud_path
479 and os.path.exists(export_to_gcloud_path)):
Shuqian Zhaoa42bba12017-03-10 14:20:11 -0800480 upload_cmd = [export_to_gcloud_path, datastore_creds,
481 timing_log, '--parent_key',
Shuqian Zhao4ff74732017-03-30 16:20:10 -0700482 datastore_parent_key]
Shuqian Zhao19e62fb2017-01-09 10:10:14 -0800483 tko_utils.dprint('Start exporting timeline report to gcloud')
Shuqian Zhaoa42bba12017-03-10 14:20:11 -0800484 subprocess.check_output(upload_cmd)
Shuqian Zhao19e62fb2017-01-09 10:10:14 -0800485 tko_utils.dprint('Successfully export timeline report to '
486 'gcloud')
487 else:
488 tko_utils.dprint('DEBUG: skip exporting suite timeline to '
489 'gcloud, because either gcloud creds or '
490 'export_to_gcloud script is not found.')
Shuqian Zhao31425d52016-12-07 09:35:03 -0800491 except Exception as e:
Shuqian Zhao19e62fb2017-01-09 10:10:14 -0800492 tko_utils.dprint("WARNING: fail to dump/export suite report. "
493 "Error:\n%s" % e)
Shuqian Zhao31425d52016-12-07 09:35:03 -0800494
Dan Shi5f626332016-01-27 15:25:58 -0800495 # Mark GS_OFFLOADER_NO_OFFLOAD in gs_offloader_instructions at the end of
496 # the function, so any failure, e.g., db connection error, will stop
497 # gs_offloader_instructions being updated, and logs can be uploaded for
498 # troubleshooting.
499 if job_successful:
500 # Check if we should not offload this test's results.
501 if job_keyval.get(constants.JOB_OFFLOAD_FAILURES_KEY, False):
502 # Update the gs_offloader_instructions json file.
503 gs_instructions_file = os.path.join(
504 path, constants.GS_OFFLOADER_INSTRUCTIONS)
505 gs_offloader_instructions = {}
506 if os.path.exists(gs_instructions_file):
507 with open(gs_instructions_file, 'r') as f:
508 gs_offloader_instructions = json.load(f)
509
510 gs_offloader_instructions[constants.GS_OFFLOADER_NO_OFFLOAD] = True
511 with open(gs_instructions_file, 'w') as f:
512 json.dump(gs_offloader_instructions, f)
513
514
Prathmesh Prabhu30dee862018-04-18 20:24:20 -0700515def _write_job_to_db(db, jobname, job):
Prathmesh Prabhu8957a342018-04-18 18:29:09 -0700516 """Write all TKO data associated with a job to DB.
517
518 This updates the job object as a side effect.
519
520 @param db: tko.db.db_sql object.
521 @param jobname: Name of the job to write.
522 @param job: tko.models.job object.
523 """
524 db.insert_or_update_machine(job)
Prathmesh Prabhu30dee862018-04-18 20:24:20 -0700525 db.insert_job(jobname, job)
Prathmesh Prabhu17905882018-04-18 22:09:08 -0700526 db.insert_or_update_task_reference(
527 job,
528 'skylab' if tko_utils.is_skylab_task(jobname) else 'afe',
529 )
Prathmesh Prabhu8957a342018-04-18 18:29:09 -0700530 db.update_job_keyvals(job)
531 for test in job.tests:
532 db.insert_test(job, test)
533
534
Prathmesh Prabhu42a2bb42018-04-18 18:56:16 -0700535def _find_status_log_path(path):
536 if os.path.exists(os.path.join(path, "status.log")):
537 return os.path.join(path, "status.log")
538 if os.path.exists(os.path.join(path, "status")):
539 return os.path.join(path, "status")
540 return ""
541
542
Prathmesh Prabhue06c49b2018-04-18 19:01:23 -0700543def _parse_status_log(parser, job, status_log_path):
544 status_lines = open(status_log_path).readlines()
545 parser.start(job)
546 tests = parser.end(status_lines)
547
548 # parser.end can return the same object multiple times, so filter out dups
549 job.tests = []
550 already_added = set()
551 for test in tests:
552 if test not in already_added:
553 already_added.add(test)
554 job.tests.append(test)
555
556
Prathmesh Prabhuedac1ee2018-04-18 19:16:34 -0700557def _match_existing_tests(db, job):
558 """Find entries in the DB corresponding to the job's tests, update job.
559
560 @return: Any unmatched tests in the db.
561 """
562 old_job_idx = job.job_idx
563 raw_old_tests = db.select("test_idx,subdir,test", "tko_tests",
564 {"job_idx": old_job_idx})
565 if raw_old_tests:
566 old_tests = dict(((test, subdir), test_idx)
567 for test_idx, subdir, test in raw_old_tests)
568 else:
569 old_tests = {}
570
571 for test in job.tests:
572 test_idx = old_tests.pop((test.testname, test.subdir), None)
573 if test_idx is not None:
574 test.test_idx = test_idx
575 else:
576 tko_utils.dprint("! Reparse returned new test "
577 "testname=%r subdir=%r" %
578 (test.testname, test.subdir))
579 return old_tests
580
581
582def _delete_tests_from_db(db, tests):
583 for test_idx in tests.itervalues():
584 where = {'test_idx' : test_idx}
585 db.delete('tko_iteration_result', where)
586 db.delete('tko_iteration_perf_value', where)
587 db.delete('tko_iteration_attributes', where)
588 db.delete('tko_test_attributes', where)
589 db.delete('tko_test_labels_tests', {'test_id': test_idx})
590 db.delete('tko_tests', where)
591
592
jadmanski8e9c2572008-11-11 00:29:02 +0000593def _get_job_subdirs(path):
594 """
595 Returns a list of job subdirectories at path. Returns None if the test
596 is itself a job directory. Does not recurse into the subdirs.
597 """
598 # if there's a .machines file, use it to get the subdirs
jadmanski0afbb632008-06-06 21:10:57 +0000599 machine_list = os.path.join(path, ".machines")
600 if os.path.exists(machine_list):
jadmanski42fbd072009-01-30 15:07:05 +0000601 subdirs = set(line.strip() for line in file(machine_list))
602 existing_subdirs = set(subdir for subdir in subdirs
603 if os.path.exists(os.path.join(path, subdir)))
604 if len(existing_subdirs) != 0:
605 return existing_subdirs
jadmanski8e9c2572008-11-11 00:29:02 +0000606
607 # if this dir contains ONLY subdirectories, return them
608 contents = set(os.listdir(path))
609 contents.discard(".parse.lock")
610 subdirs = set(sub for sub in contents if
611 os.path.isdir(os.path.join(path, sub)))
612 if len(contents) == len(subdirs) != 0:
613 return subdirs
614
615 # this is a job directory, or something else we don't understand
616 return None
617
618
Prathmesh Prabhub1241d12018-04-19 18:09:43 -0700619def parse_leaf_path(db, pid_file_manager, path, level, parse_options):
Fang Deng49822682014-10-21 16:29:22 -0700620 """Parse a leaf path.
621
622 @param db: database handle.
Prathmesh Prabhub1241d12018-04-19 18:09:43 -0700623 @param pid_file_manager: pidfile.PidFileManager object.
Fang Deng49822682014-10-21 16:29:22 -0700624 @param path: The path to the results to be parsed.
625 @param level: Integer, level of subdirectories to include in the job name.
Aviv Keshet687d2dc2016-10-20 15:41:16 -0700626 @param parse_options: _ParseOptions instance.
Fang Deng49822682014-10-21 16:29:22 -0700627
628 @returns: The job name of the parsed job, e.g. '123-chromeos-test/host1'
629 """
mbligha48eeb22009-03-11 16:44:43 +0000630 job_elements = path.split("/")[-level:]
631 jobname = "/".join(job_elements)
632 try:
Prathmesh Prabhub1241d12018-04-19 18:09:43 -0700633 db.run_with_retry(parse_one, db, pid_file_manager, jobname, path,
634 parse_options)
Simran Basi8de306c2016-12-21 12:04:21 -0800635 except Exception as e:
636 tko_utils.dprint("Error parsing leaf path: %s\nException:\n%s\n%s" %
637 (path, e, traceback.format_exc()))
Fang Deng49822682014-10-21 16:29:22 -0700638 return jobname
mbligha48eeb22009-03-11 16:44:43 +0000639
640
Prathmesh Prabhub1241d12018-04-19 18:09:43 -0700641def parse_path(db, pid_file_manager, path, level, parse_options):
Fang Deng49822682014-10-21 16:29:22 -0700642 """Parse a path
643
644 @param db: database handle.
Prathmesh Prabhub1241d12018-04-19 18:09:43 -0700645 @param pid_file_manager: pidfile.PidFileManager object.
Fang Deng49822682014-10-21 16:29:22 -0700646 @param path: The path to the results to be parsed.
647 @param level: Integer, level of subdirectories to include in the job name.
Aviv Keshet687d2dc2016-10-20 15:41:16 -0700648 @param parse_options: _ParseOptions instance.
Fang Deng49822682014-10-21 16:29:22 -0700649
650 @returns: A set of job names of the parsed jobs.
651 set(['123-chromeos-test/host1', '123-chromeos-test/host2'])
652 """
653 processed_jobs = set()
jadmanski8e9c2572008-11-11 00:29:02 +0000654 job_subdirs = _get_job_subdirs(path)
655 if job_subdirs is not None:
mbligha48eeb22009-03-11 16:44:43 +0000656 # parse status.log in current directory, if it exists. multi-machine
657 # synchronous server side tests record output in this directory. without
658 # this check, we do not parse these results.
659 if os.path.exists(os.path.join(path, 'status.log')):
Prathmesh Prabhub1241d12018-04-19 18:09:43 -0700660 new_job = parse_leaf_path(db, pid_file_manager, path, level,
661 parse_options)
Fang Deng49822682014-10-21 16:29:22 -0700662 processed_jobs.add(new_job)
jadmanski0afbb632008-06-06 21:10:57 +0000663 # multi-machine job
jadmanski8e9c2572008-11-11 00:29:02 +0000664 for subdir in job_subdirs:
665 jobpath = os.path.join(path, subdir)
Prathmesh Prabhub1241d12018-04-19 18:09:43 -0700666 new_jobs = parse_path(db, pid_file_manager, jobpath, level + 1,
667 parse_options)
Fang Deng49822682014-10-21 16:29:22 -0700668 processed_jobs.update(new_jobs)
jadmanski0afbb632008-06-06 21:10:57 +0000669 else:
670 # single machine job
Prathmesh Prabhub1241d12018-04-19 18:09:43 -0700671 new_job = parse_leaf_path(db, pid_file_manager, path, level,
672 parse_options)
Fang Deng49822682014-10-21 16:29:22 -0700673 processed_jobs.add(new_job)
674 return processed_jobs
675
676
Prathmesh Prabhu3e319da2017-08-30 19:13:03 -0700677def _detach_from_parent_process():
678 """Allow reparenting the parse process away from caller.
679
680 When monitor_db is run via upstart, restarting the job sends SIGTERM to
681 the whole process group. This makes us immune from that.
682 """
683 if os.getpid() != os.getpgid(0):
684 os.setsid()
mblighbb7b8912006-10-08 03:59:02 +0000685
mbligh96cf0512008-04-17 15:25:38 +0000686def main():
Fang Deng49822682014-10-21 16:29:22 -0700687 """Main entrance."""
688 start_time = datetime.datetime.now()
689 # Record the processed jobs so that
690 # we can send the duration of parsing to metadata db.
691 processed_jobs = set()
692
jadmanski0afbb632008-06-06 21:10:57 +0000693 options, args = parse_args()
Prathmesh Prabhu3e319da2017-08-30 19:13:03 -0700694
695 if options.detach:
696 _detach_from_parent_process()
697
Aviv Keshet0b7bab02016-10-20 17:17:36 -0700698 parse_options = _ParseOptions(options.reparse, options.mailit,
Shuqian Zhao19e62fb2017-01-09 10:10:14 -0800699 options.dry_run, options.suite_report,
700 options.datastore_creds,
701 options.export_to_gcloud_path)
jadmanski0afbb632008-06-06 21:10:57 +0000702 results_dir = os.path.abspath(args[0])
703 assert os.path.exists(results_dir)
mbligh96cf0512008-04-17 15:25:38 +0000704
Dan Shibea26912017-07-21 12:26:10 -0700705 site_utils.SetupTsMonGlobalState('tko_parse', indirect=False,
706 short_lived=True)
707
jadmanskid5ab8c52008-12-03 16:27:07 +0000708 pid_file_manager = pidfile.PidFileManager("parser", results_dir)
mbligh96cf0512008-04-17 15:25:38 +0000709
jadmanskid5ab8c52008-12-03 16:27:07 +0000710 if options.write_pidfile:
711 pid_file_manager.open_file()
mbligh96cf0512008-04-17 15:25:38 +0000712
jadmanskid5ab8c52008-12-03 16:27:07 +0000713 try:
714 # build up the list of job dirs to parse
715 if options.singledir:
716 jobs_list = [results_dir]
717 else:
718 jobs_list = [os.path.join(results_dir, subdir)
719 for subdir in os.listdir(results_dir)]
720
721 # build up the database
722 db = tko_db.db(autocommit=False, host=options.db_host,
723 user=options.db_user, password=options.db_pass,
724 database=options.db_name)
725
726 # parse all the jobs
727 for path in jobs_list:
728 lockfile = open(os.path.join(path, ".parse.lock"), "w")
729 flags = fcntl.LOCK_EX
730 if options.noblock:
mblighdb18b0e2009-01-30 00:34:32 +0000731 flags |= fcntl.LOCK_NB
jadmanskid5ab8c52008-12-03 16:27:07 +0000732 try:
733 fcntl.flock(lockfile, flags)
734 except IOError, e:
mblighdb18b0e2009-01-30 00:34:32 +0000735 # lock is not available and nonblock has been requested
jadmanskid5ab8c52008-12-03 16:27:07 +0000736 if e.errno == errno.EWOULDBLOCK:
737 lockfile.close()
738 continue
739 else:
740 raise # something unexpected happened
741 try:
Prathmesh Prabhub1241d12018-04-19 18:09:43 -0700742 new_jobs = parse_path(db, pid_file_manager, path, options.level,
743 parse_options)
Fang Deng49822682014-10-21 16:29:22 -0700744 processed_jobs.update(new_jobs)
mbligh9e936402009-05-13 20:42:17 +0000745
jadmanskid5ab8c52008-12-03 16:27:07 +0000746 finally:
747 fcntl.flock(lockfile, fcntl.LOCK_UN)
jadmanski0afbb632008-06-06 21:10:57 +0000748 lockfile.close()
mblighe97e0e62009-05-21 01:41:58 +0000749
Dan Shib7a36ea2017-02-28 21:52:20 -0800750 except Exception as e:
jadmanskid5ab8c52008-12-03 16:27:07 +0000751 pid_file_manager.close_file(1)
752 raise
753 else:
754 pid_file_manager.close_file(0)
Dan Shibea26912017-07-21 12:26:10 -0700755 finally:
756 metrics.Flush()
Fang Deng49822682014-10-21 16:29:22 -0700757 duration_secs = (datetime.datetime.now() - start_time).total_seconds()
mbligh71d340d2008-03-05 15:51:16 +0000758
mbligh532cb272007-11-26 18:54:20 +0000759
mbligh96cf0512008-04-17 15:25:38 +0000760if __name__ == "__main__":
jadmanski0afbb632008-06-06 21:10:57 +0000761 main()