blob: 8fa4976f3632945fcf4a730cda370baccd82c700 [file] [log] [blame]
Jakob Juelich934f0dc2014-10-14 18:21:13 -07001# Copyright (c) 2014 The Chromium OS 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
Allen Li64692a32016-11-04 13:35:19 -07005import math
6import os
7import random
8import re
9import sys
10import time
mbligh96cf0512008-04-17 15:25:38 +000011
12import common
13from autotest_lib.client.common_lib import global_config
Jakob Juelich934f0dc2014-10-14 18:21:13 -070014from autotest_lib.frontend import database_settings_helper
jamesrena12b8a02010-06-16 23:28:23 +000015from autotest_lib.tko import utils
mbligh96cf0512008-04-17 15:25:38 +000016
mblighed4d6dd2008-02-27 16:49:43 +000017
Allen Lie0bb7602016-11-29 13:45:53 -080018def _log_error(msg):
19 """Log an error message.
20
21 @param msg: Message string
22 """
23 print >> sys.stderr, msg
24 sys.stderr.flush() # we want these msgs to show up immediately
25
26
27def _format_operational_error(e):
28 """Format OperationalError.
29
30 @param e: OperationalError instance.
31 """
32 return ("%s: An operational error occurred during a database "
33 "operation: %s" % (time.strftime("%X %x"), str(e)))
34
35
mblighaea09602008-04-16 22:59:37 +000036class MySQLTooManyRows(Exception):
Michael Tang5f74ffd2016-10-31 10:34:53 -070037 """Too many records."""
jadmanski0afbb632008-06-06 21:10:57 +000038 pass
mblighaea09602008-04-16 22:59:37 +000039
mblighd5c33db2006-10-08 21:34:16 +000040
mbligh7636b3a2008-06-11 15:44:01 +000041class db_sql(object):
Michael Tang5f74ffd2016-10-31 10:34:53 -070042 """Data access."""
43
jadmanski0afbb632008-06-06 21:10:57 +000044 def __init__(self, debug=False, autocommit=True, host=None,
45 database=None, user=None, password=None):
46 self.debug = debug
47 self.autocommit = autocommit
48 self._load_config(host, database, user, password)
mbligh96cf0512008-04-17 15:25:38 +000049
jadmanski0afbb632008-06-06 21:10:57 +000050 self.con = None
51 self._init_db()
mblighd5c33db2006-10-08 21:34:16 +000052
jadmanski0afbb632008-06-06 21:10:57 +000053 # if not present, insert statuses
54 self.status_idx = {}
55 self.status_word = {}
showardeab66ce2009-12-23 00:03:56 +000056 status_rows = self.select('status_idx, word', 'tko_status', None)
jadmanski0afbb632008-06-06 21:10:57 +000057 for s in status_rows:
58 self.status_idx[s[1]] = s[0]
59 self.status_word[s[0]] = s[1]
mbligh048e1c92007-10-07 00:10:33 +000060
jadmanski0afbb632008-06-06 21:10:57 +000061 machine_map = os.path.join(os.path.dirname(__file__),
62 'machines')
63 if os.path.exists(machine_map):
64 self.machine_map = machine_map
65 else:
66 self.machine_map = None
67 self.machine_group = {}
mbligh048e1c92007-10-07 00:10:33 +000068
mbligh8e1ab172007-09-13 17:29:56 +000069
jadmanski0afbb632008-06-06 21:10:57 +000070 def _load_config(self, host, database, user, password):
Jakob Juelich934f0dc2014-10-14 18:21:13 -070071 """Loads configuration settings required to connect to the database.
72
73 This will try to connect to use the settings prefixed with global_db_.
74 If they do not exist, they un-prefixed settings will be used.
75
76 If parameters are supplied, these will be taken instead of the values
77 in global_config.
78
79 @param host: If set, this host will be used, if not, the host will be
80 retrieved from global_config.
81 @param database: If set, this database will be used, if not, the
82 database will be retrieved from global_config.
83 @param user: If set, this user will be used, if not, the
84 user will be retrieved from global_config.
85 @param password: If set, this password will be used, if not, the
86 password will be retrieved from global_config.
87 """
88 database_settings = database_settings_helper.get_global_db_config()
mbligh65acae52008-04-24 20:21:55 +000089
jadmanski0afbb632008-06-06 21:10:57 +000090 # grab the host, database
Jakob Juelich934f0dc2014-10-14 18:21:13 -070091 self.host = host or database_settings['HOST']
92 self.database = database or database_settings['NAME']
mbligh65acae52008-04-24 20:21:55 +000093
jadmanski0afbb632008-06-06 21:10:57 +000094 # grab the user and password
Jakob Juelich934f0dc2014-10-14 18:21:13 -070095 self.user = user or database_settings['USER']
96 self.password = password or database_settings['PASSWORD']
mbligh65acae52008-04-24 20:21:55 +000097
Michael Spang7a273472014-10-08 12:08:13 -040098 # grab the timeout configuration
Jakob Juelich934f0dc2014-10-14 18:21:13 -070099 self.query_timeout =(
100 database_settings.get('OPTIONS', {}).get('timeout', 3600))
101
102 # Using fallback to non-global in order to work without configuration
103 # overhead on non-shard instances.
104 get_value = global_config.global_config.get_config_value_with_fallback
Jakob Juelich475b82b2014-09-30 11:17:07 -0700105 self.min_delay = get_value("AUTOTEST_WEB", "global_db_min_retry_delay",
Jakob Juelich934f0dc2014-10-14 18:21:13 -0700106 "min_retry_delay", type=int, default=20)
Jakob Juelich475b82b2014-09-30 11:17:07 -0700107 self.max_delay = get_value("AUTOTEST_WEB", "global_db_max_retry_delay",
Jakob Juelich934f0dc2014-10-14 18:21:13 -0700108 "max_retry_delay", type=int, default=60)
mbligh65acae52008-04-24 20:21:55 +0000109
Richard Barnette2468fbd2014-11-07 01:12:46 +0000110 # TODO(beeps): Move this to django settings once we have routers.
111 # On test instances mysql connects through a different port. No point
112 # piping this through our entire infrastructure when it is only really
113 # used for testing; Ideally we would specify this through django
114 # settings and default it to the empty string so django will figure out
115 # the default based on the database backend (eg: mysql, 3306), but until
116 # we have database routers in place any django settings will apply to
117 # both tko and afe.
118 # The intended use of this port is to allow a testing shard vm to
119 # update the master vm's database with test results. Specifying
120 # and empty string will fallback to not even specifying the port
121 # to the backend in tko/db.py. Unfortunately this means retries
122 # won't work on the test cluster till we've migrated to routers.
123 self.port = global_config.global_config.get_config_value(
124 "AUTOTEST_WEB", "global_db_port", type=str, default='')
125
mbligh65acae52008-04-24 20:21:55 +0000126
jadmanski0afbb632008-06-06 21:10:57 +0000127 def _init_db(self):
128 # make sure we clean up any existing connection
129 if self.con:
130 self.con.close()
131 self.con = None
mbligh65acae52008-04-24 20:21:55 +0000132
Prathmesh Prabhuba8adaf2017-07-05 13:07:05 -0700133 # create the db connection and cursor
134 self.con = self.connect(self.host, self.database,
135 self.user, self.password, self.port)
jadmanski0afbb632008-06-06 21:10:57 +0000136 self.cur = self.con.cursor()
mbligh96cf0512008-04-17 15:25:38 +0000137
138
jadmanski0afbb632008-06-06 21:10:57 +0000139 def _random_delay(self):
140 delay = random.randint(self.min_delay, self.max_delay)
141 time.sleep(delay)
mbligh65acae52008-04-24 20:21:55 +0000142
143
jadmanski0afbb632008-06-06 21:10:57 +0000144 def run_with_retry(self, function, *args, **dargs):
145 """Call function(*args, **dargs) until either it passes
146 without an operational error, or a timeout is reached.
147 This will re-connect to the database, so it is NOT safe
148 to use this inside of a database transaction.
jadmanskie7a69092008-05-29 21:03:13 +0000149
jadmanski0afbb632008-06-06 21:10:57 +0000150 It can be safely used with transactions, but the
151 transaction start & end must be completely contained
Michael Tang5f74ffd2016-10-31 10:34:53 -0700152 within the call to 'function'.
153
154 @param function: The function to run with retry.
155 @param args: The arguments
156 @param dargs: The named arguments.
157 """
jadmanski0afbb632008-06-06 21:10:57 +0000158 OperationalError = _get_error_class("OperationalError")
mbligh65acae52008-04-24 20:21:55 +0000159
jadmanski0afbb632008-06-06 21:10:57 +0000160 success = False
161 start_time = time.time()
162 while not success:
163 try:
164 result = function(*args, **dargs)
165 except OperationalError, e:
Allen Lie0bb7602016-11-29 13:45:53 -0800166 _log_error("%s; retrying, don't panic yet"
167 % _format_operational_error(e))
jadmanski0afbb632008-06-06 21:10:57 +0000168 stop_time = time.time()
169 elapsed_time = stop_time - start_time
170 if elapsed_time > self.query_timeout:
171 raise
172 else:
173 try:
174 self._random_delay()
175 self._init_db()
176 except OperationalError, e:
Allen Lie0bb7602016-11-29 13:45:53 -0800177 _log_error('%s; panic now'
178 % _format_operational_error(e))
jadmanski0afbb632008-06-06 21:10:57 +0000179 else:
180 success = True
181 return result
mbligh96cf0512008-04-17 15:25:38 +0000182
183
jadmanski0afbb632008-06-06 21:10:57 +0000184 def dprint(self, value):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700185 """Print out debug value.
186
187 @param value: The value to print out.
188 """
jadmanski0afbb632008-06-06 21:10:57 +0000189 if self.debug:
190 sys.stdout.write('SQL: ' + str(value) + '\n')
mbligh8e1ab172007-09-13 17:29:56 +0000191
mblighd5c33db2006-10-08 21:34:16 +0000192
Dan Shie8e0c052015-09-01 00:27:27 -0700193 def _commit(self):
194 """Private method for function commit to call for retry.
195 """
196 return self.con.commit()
197
198
jadmanski0afbb632008-06-06 21:10:57 +0000199 def commit(self):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700200 """Commit the sql transaction."""
Dan Shie8e0c052015-09-01 00:27:27 -0700201 if self.autocommit:
202 return self.run_with_retry(self._commit)
203 else:
204 return self._commit()
mbligh432bad42007-10-09 19:56:07 +0000205
206
Simran Basie129a962012-08-31 13:03:53 -0700207 def rollback(self):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700208 """Rollback the sql transaction."""
Simran Basie129a962012-08-31 13:03:53 -0700209 self.con.rollback()
210
211
jadmanski0afbb632008-06-06 21:10:57 +0000212 def get_last_autonumber_value(self):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700213 """Gets the last auto number.
214
215 @return: The last auto number.
216 """
jadmanski0afbb632008-06-06 21:10:57 +0000217 self.cur.execute('SELECT LAST_INSERT_ID()', [])
218 return self.cur.fetchall()[0][0]
mblighe12b8612008-02-12 20:58:14 +0000219
220
showardc1a98d12010-01-15 00:22:22 +0000221 def _quote(self, field):
222 return '`%s`' % field
223
224
225 def _where_clause(self, where):
226 if not where:
227 return '', []
228
229 if isinstance(where, dict):
230 # key/value pairs (which should be equal, or None for null)
231 keys, values = [], []
232 for field, value in where.iteritems():
233 quoted_field = self._quote(field)
234 if value is None:
235 keys.append(quoted_field + ' is null')
236 else:
237 keys.append(quoted_field + '=%s')
238 values.append(value)
239 where_clause = ' and '.join(keys)
240 elif isinstance(where, basestring):
241 # the exact string
242 where_clause = where
243 values = []
244 elif isinstance(where, tuple):
245 # preformatted where clause + values
246 where_clause, values = where
247 assert where_clause
248 else:
249 raise ValueError('Invalid "where" value: %r' % where)
250
251 return ' WHERE ' + where_clause, values
252
253
254
255 def select(self, fields, table, where, distinct=False, group_by=None,
256 max_rows=None):
jadmanski0afbb632008-06-06 21:10:57 +0000257 """\
258 This selects all the fields requested from a
259 specific table with a particular where clause.
260 The where clause can either be a dictionary of
261 field=value pairs, a string, or a tuple of (string,
262 a list of values). The last option is what you
263 should use when accepting user input as it'll
264 protect you against sql injection attacks (if
265 all user data is placed in the array rather than
266 the raw SQL).
mbligh12eebfa2008-01-03 02:01:53 +0000267
jadmanski0afbb632008-06-06 21:10:57 +0000268 For example:
269 where = ("a = %s AND b = %s", ['val', 'val'])
270 is better than
271 where = "a = 'val' AND b = 'val'"
Michael Tang5f74ffd2016-10-31 10:34:53 -0700272
273 @param fields: The list of selected fields string.
274 @param table: The name of the database table.
275 @param where: The where clause string.
276 @param distinct: If select distinct values.
277 @param group_by: Group by clause.
278 @param max_rows: unused.
jadmanski0afbb632008-06-06 21:10:57 +0000279 """
280 cmd = ['select']
281 if distinct:
282 cmd.append('distinct')
283 cmd += [fields, 'from', table]
mbligh608c3252007-08-31 13:53:00 +0000284
showardc1a98d12010-01-15 00:22:22 +0000285 where_clause, values = self._where_clause(where)
286 cmd.append(where_clause)
mbligh96cf0512008-04-17 15:25:38 +0000287
jadmanski0afbb632008-06-06 21:10:57 +0000288 if group_by:
289 cmd.append(' GROUP BY ' + group_by)
mbligh83f63a02007-12-12 19:13:04 +0000290
jadmanski0afbb632008-06-06 21:10:57 +0000291 self.dprint('%s %s' % (' '.join(cmd), values))
mbligh96cf0512008-04-17 15:25:38 +0000292
jadmanski0afbb632008-06-06 21:10:57 +0000293 # create a re-runable function for executing the query
294 def exec_sql():
Michael Tang5f74ffd2016-10-31 10:34:53 -0700295 """Exeuctes an the sql command."""
jadmanski0afbb632008-06-06 21:10:57 +0000296 sql = ' '.join(cmd)
297 numRec = self.cur.execute(sql, values)
mblighd876f452008-12-03 15:09:17 +0000298 if max_rows is not None and numRec > max_rows:
jadmanski0afbb632008-06-06 21:10:57 +0000299 msg = 'Exceeded allowed number of records'
300 raise MySQLTooManyRows(msg)
301 return self.cur.fetchall()
mbligh96cf0512008-04-17 15:25:38 +0000302
jadmanski0afbb632008-06-06 21:10:57 +0000303 # run the query, re-trying after operational errors
304 if self.autocommit:
305 return self.run_with_retry(exec_sql)
306 else:
307 return exec_sql()
mblighd5c33db2006-10-08 21:34:16 +0000308
mbligh056d0d32006-10-08 22:31:10 +0000309
jadmanski0afbb632008-06-06 21:10:57 +0000310 def select_sql(self, fields, table, sql, values):
311 """\
312 select fields from table "sql"
Michael Tang5f74ffd2016-10-31 10:34:53 -0700313
314 @param fields: The list of selected fields string.
315 @param table: The name of the database table.
316 @param sql: The sql string.
317 @param values: The sql string parameter values.
jadmanski0afbb632008-06-06 21:10:57 +0000318 """
319 cmd = 'select %s from %s %s' % (fields, table, sql)
320 self.dprint(cmd)
mbligh414c69e2007-10-05 15:13:06 +0000321
jadmanski0afbb632008-06-06 21:10:57 +0000322 # create a -re-runable function for executing the query
Michael Tang5f74ffd2016-10-31 10:34:53 -0700323 def _exec_sql():
jadmanski0afbb632008-06-06 21:10:57 +0000324 self.cur.execute(cmd, values)
325 return self.cur.fetchall()
mbligh96b9a5a2007-11-24 19:32:20 +0000326
jadmanski0afbb632008-06-06 21:10:57 +0000327 # run the query, re-trying after operational errors
328 if self.autocommit:
Michael Tang5f74ffd2016-10-31 10:34:53 -0700329 return self.run_with_retry(_exec_sql)
jadmanski0afbb632008-06-06 21:10:57 +0000330 else:
Michael Tang5f74ffd2016-10-31 10:34:53 -0700331 return _exec_sql()
mbligh96b9a5a2007-11-24 19:32:20 +0000332
mbligh608c3252007-08-31 13:53:00 +0000333
jadmanski0afbb632008-06-06 21:10:57 +0000334 def _exec_sql_with_commit(self, sql, values, commit):
335 if self.autocommit:
336 # re-run the query until it succeeds
Michael Tang5f74ffd2016-10-31 10:34:53 -0700337 def _exec_sql():
jadmanski0afbb632008-06-06 21:10:57 +0000338 self.cur.execute(sql, values)
339 self.con.commit()
Michael Tang5f74ffd2016-10-31 10:34:53 -0700340 self.run_with_retry(_exec_sql)
jadmanski0afbb632008-06-06 21:10:57 +0000341 else:
342 # take one shot at running the query
343 self.cur.execute(sql, values)
344 if commit:
345 self.con.commit()
mbligh96b9a5a2007-11-24 19:32:20 +0000346
mbligh2bd48872007-09-20 18:32:25 +0000347
jadmanskib591fba2008-09-10 16:19:22 +0000348 def insert(self, table, data, commit=None):
jadmanski0afbb632008-06-06 21:10:57 +0000349 """\
350 'insert into table (keys) values (%s ... %s)', values
mbligh96cf0512008-04-17 15:25:38 +0000351
jadmanski0afbb632008-06-06 21:10:57 +0000352 data:
353 dictionary of fields and data
Michael Tang5f74ffd2016-10-31 10:34:53 -0700354
355 @param table: The name of the table.
356 @param data: The insert data.
357 @param commit: If commit the transaction .
jadmanski0afbb632008-06-06 21:10:57 +0000358 """
359 fields = data.keys()
360 refs = ['%s' for field in fields]
361 values = [data[field] for field in fields]
showardc1a98d12010-01-15 00:22:22 +0000362 cmd = ('insert into %s (%s) values (%s)' %
363 (table, ','.join(self._quote(field) for field in fields),
364 ','.join(refs)))
jadmanski0afbb632008-06-06 21:10:57 +0000365 self.dprint('%s %s' % (cmd, values))
mblighe9cf9d42007-08-31 08:56:00 +0000366
jadmanski0afbb632008-06-06 21:10:57 +0000367 self._exec_sql_with_commit(cmd, values, commit)
mblighe9cf9d42007-08-31 08:56:00 +0000368
mbligh048e1c92007-10-07 00:10:33 +0000369
jadmanski0afbb632008-06-06 21:10:57 +0000370 def delete(self, table, where, commit = None):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700371 """Delete entries.
372
373 @param table: The name of the table.
374 @param where: The where clause.
375 @param commit: If commit the transaction .
376 """
jadmanski0afbb632008-06-06 21:10:57 +0000377 cmd = ['delete from', table]
mblighd876f452008-12-03 15:09:17 +0000378 if commit is None:
jadmanski0afbb632008-06-06 21:10:57 +0000379 commit = self.autocommit
showardc1a98d12010-01-15 00:22:22 +0000380 where_clause, values = self._where_clause(where)
381 cmd.append(where_clause)
jadmanski0afbb632008-06-06 21:10:57 +0000382 sql = ' '.join(cmd)
383 self.dprint('%s %s' % (sql, values))
mbligh048e1c92007-10-07 00:10:33 +0000384
jadmanski0afbb632008-06-06 21:10:57 +0000385 self._exec_sql_with_commit(sql, values, commit)
mbligh048e1c92007-10-07 00:10:33 +0000386
mbligh7a41a862007-11-30 17:44:24 +0000387
jadmanski0afbb632008-06-06 21:10:57 +0000388 def update(self, table, data, where, commit = None):
389 """\
390 'update table set data values (%s ... %s) where ...'
mbligh2aaeb672007-10-01 14:54:18 +0000391
jadmanski0afbb632008-06-06 21:10:57 +0000392 data:
393 dictionary of fields and data
Michael Tang5f74ffd2016-10-31 10:34:53 -0700394
395 @param table: The name of the table.
396 @param data: The sql parameter values.
397 @param where: The where clause.
398 @param commit: If commit the transaction .
jadmanski0afbb632008-06-06 21:10:57 +0000399 """
mblighd876f452008-12-03 15:09:17 +0000400 if commit is None:
jadmanski0afbb632008-06-06 21:10:57 +0000401 commit = self.autocommit
402 cmd = 'update %s ' % table
403 fields = data.keys()
showardc1a98d12010-01-15 00:22:22 +0000404 data_refs = [self._quote(field) + '=%s' for field in fields]
jadmanski0afbb632008-06-06 21:10:57 +0000405 data_values = [data[field] for field in fields]
jadmanski74eebf32008-07-15 20:04:42 +0000406 cmd += ' set ' + ', '.join(data_refs)
mbligh2aaeb672007-10-01 14:54:18 +0000407
showardc1a98d12010-01-15 00:22:22 +0000408 where_clause, where_values = self._where_clause(where)
409 cmd += where_clause
mbligh2aaeb672007-10-01 14:54:18 +0000410
jadmanski0afbb632008-06-06 21:10:57 +0000411 values = data_values + where_values
jadmanski74eebf32008-07-15 20:04:42 +0000412 self.dprint('%s %s' % (cmd, values))
mbligh2aaeb672007-10-01 14:54:18 +0000413
jadmanski0afbb632008-06-06 21:10:57 +0000414 self._exec_sql_with_commit(cmd, values, commit)
mblighe9cf9d42007-08-31 08:56:00 +0000415
416
jadmanski0afbb632008-06-06 21:10:57 +0000417 def delete_job(self, tag, commit = None):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700418 """Delete a tko job.
419
420 @param tag: The job tag.
421 @param commit: If commit the transaction .
422 """
jadmanski0afbb632008-06-06 21:10:57 +0000423 job_idx = self.find_job(tag)
424 for test_idx in self.find_tests(job_idx):
425 where = {'test_idx' : test_idx}
showardeab66ce2009-12-23 00:03:56 +0000426 self.delete('tko_iteration_result', where)
Dennis Jeffrey368c54b2013-07-24 11:19:03 -0700427 self.delete('tko_iteration_perf_value', where)
showardeab66ce2009-12-23 00:03:56 +0000428 self.delete('tko_iteration_attributes', where)
429 self.delete('tko_test_attributes', where)
430 self.delete('tko_test_labels_tests', {'test_id': test_idx})
jadmanski0afbb632008-06-06 21:10:57 +0000431 where = {'job_idx' : job_idx}
showardeab66ce2009-12-23 00:03:56 +0000432 self.delete('tko_tests', where)
433 self.delete('tko_jobs', where)
apw7a7316b2008-02-21 17:42:05 +0000434
apw7a7316b2008-02-21 17:42:05 +0000435
Prathmesh Prabhu30dee862018-04-18 20:24:20 -0700436 def insert_job(self, tag, job, commit=None):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700437 """Insert a tko job.
438
439 @param tag: The job tag.
440 @param job: The job object.
Michael Tang5f74ffd2016-10-31 10:34:53 -0700441 @param commit: If commit the transaction .
442 """
Prathmesh Prabhu17905882018-04-18 22:09:08 -0700443 data = self._get_common_job_data(tag, job)
444 data.update({
Prathmesh Prabhu30dee862018-04-18 20:24:20 -0700445 'afe_job_id': job.afe_job_id,
446 'afe_parent_job_id': job.afe_parent_job_id,
Prathmesh Prabhu17905882018-04-18 22:09:08 -0700447 })
Prathmesh Prabhufc06c242018-04-18 21:47:54 -0700448 if job.index is not None:
showardeab66ce2009-12-23 00:03:56 +0000449 self.update('tko_jobs', data, {'job_idx': job.index}, commit=commit)
showard0fec8a02009-12-04 01:19:54 +0000450 else:
showardeab66ce2009-12-23 00:03:56 +0000451 self.insert('tko_jobs', data, commit=commit)
showard0fec8a02009-12-04 01:19:54 +0000452 job.index = self.get_last_autonumber_value()
Shuqian Zhao31425d52016-12-07 09:35:03 -0800453
mbligh237bed32007-09-05 13:05:57 +0000454
Prathmesh Prabhu17905882018-04-18 22:09:08 -0700455 def _get_common_job_data(self, tag, job):
456 """Construct a dictionary with the common data to insert in job/task."""
457 return {
458 'tag':tag,
459 'label': job.label,
460 'username': job.user,
461 'machine_idx': job.machine_idx,
462 'queued_time': job.queued_time,
463 'started_time': job.started_time,
464 'finished_time': job.finished_time,
465 'build': job.build,
466 'build_version': job.build_version,
467 'board': job.board,
468 'suite': job.suite,
469 }
470
471
472 def insert_or_update_task_reference(self, job, reference_type, commit=None):
Prathmesh Prabhuf5030d32018-04-19 16:08:55 -0700473 """Insert an entry in the tko_task_references table.
474
475 The job should already have been inserted in tko_jobs.
476 @param job: tko.models.job object.
Prathmesh Prabhu17905882018-04-18 22:09:08 -0700477 @param reference_type: The type of reference to insert.
478 One of: {'afe', 'skylab'}
Prathmesh Prabhuf5030d32018-04-19 16:08:55 -0700479 @param commit: Whether to commit this transaction.
480 """
Prathmesh Prabhu17905882018-04-18 22:09:08 -0700481 assert reference_type in {'afe', 'skylab'}
482 if reference_type == 'afe':
483 task_id = job.afe_job_id
484 parent_task_id = job.afe_parent_job_id
485 else:
486 task_id = job.skylab_task_id
487 parent_task_id = job.skylab_parent_task_id
Prathmesh Prabhuf5030d32018-04-19 16:08:55 -0700488 data = {
Prathmesh Prabhu17905882018-04-18 22:09:08 -0700489 'reference_type': reference_type,
Prathmesh Prabhuf5030d32018-04-19 16:08:55 -0700490 'tko_job_idx': job.index,
Prathmesh Prabhu17905882018-04-18 22:09:08 -0700491 'task_id': task_id,
492 'parent_task_id': parent_task_id,
Prathmesh Prabhuf5030d32018-04-19 16:08:55 -0700493 }
Prathmesh Prabhu17905882018-04-18 22:09:08 -0700494
Prathmesh Prabhuf5030d32018-04-19 16:08:55 -0700495 task_reference_id = self._lookup_task_reference(job)
496 if task_reference_id is not None:
497 self.update('tko_task_references',
498 data,
499 {'id': task_reference_id},
500 commit=commit)
501 job.task_reference_id = task_reference_id
502 else:
503 self.insert('tko_task_references', data, commit=commit)
504 job.task_reference_id = self.get_last_autonumber_value()
505
506
showardc1a98d12010-01-15 00:22:22 +0000507 def update_job_keyvals(self, job, commit=None):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700508 """Updates the job key values.
509
510 @param job: The job object.
511 @param commit: If commit the transaction .
512 """
showardc1a98d12010-01-15 00:22:22 +0000513 for key, value in job.keyval_dict.iteritems():
514 where = {'job_id': job.index, 'key': key}
515 data = dict(where, value=value)
516 exists = self.select('id', 'tko_job_keyvals', where=where)
517
518 if exists:
519 self.update('tko_job_keyvals', data, where=where, commit=commit)
520 else:
521 self.insert('tko_job_keyvals', data, commit=commit)
522
523
jadmanski0afbb632008-06-06 21:10:57 +0000524 def insert_test(self, job, test, commit = None):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700525 """Inserts a job test.
526
527 @param job: The job object.
528 @param test: The test object.
529 @param commit: If commit the transaction .
530 """
jadmanski0afbb632008-06-06 21:10:57 +0000531 kver = self.insert_kernel(test.kernel, commit=commit)
532 data = {'job_idx':job.index, 'test':test.testname,
533 'subdir':test.subdir, 'kernel_idx':kver,
534 'status':self.status_idx[test.status],
535 'reason':test.reason, 'machine_idx':job.machine_idx,
536 'started_time': test.started_time,
537 'finished_time':test.finished_time}
jadmanski9b6babf2009-04-21 17:57:40 +0000538 is_update = hasattr(test, "test_idx")
539 if is_update:
jadmanski74eebf32008-07-15 20:04:42 +0000540 test_idx = test.test_idx
showardeab66ce2009-12-23 00:03:56 +0000541 self.update('tko_tests', data,
542 {'test_idx': test_idx}, commit=commit)
jadmanskib591fba2008-09-10 16:19:22 +0000543 where = {'test_idx': test_idx}
showardeab66ce2009-12-23 00:03:56 +0000544 self.delete('tko_iteration_result', where)
Dennis Jeffrey368c54b2013-07-24 11:19:03 -0700545 self.delete('tko_iteration_perf_value', where)
showardeab66ce2009-12-23 00:03:56 +0000546 self.delete('tko_iteration_attributes', where)
showard0fec8a02009-12-04 01:19:54 +0000547 where['user_created'] = 0
showardeab66ce2009-12-23 00:03:56 +0000548 self.delete('tko_test_attributes', where)
jadmanski74eebf32008-07-15 20:04:42 +0000549 else:
showardeab66ce2009-12-23 00:03:56 +0000550 self.insert('tko_tests', data, commit=commit)
jadmanski74eebf32008-07-15 20:04:42 +0000551 test_idx = test.test_idx = self.get_last_autonumber_value()
552 data = {'test_idx': test_idx}
mbligh237bed32007-09-05 13:05:57 +0000553
jadmanski0afbb632008-06-06 21:10:57 +0000554 for i in test.iterations:
555 data['iteration'] = i.index
556 for key, value in i.attr_keyval.iteritems():
557 data['attribute'] = key
558 data['value'] = value
showardeab66ce2009-12-23 00:03:56 +0000559 self.insert('tko_iteration_attributes', data,
jadmanski0afbb632008-06-06 21:10:57 +0000560 commit=commit)
561 for key, value in i.perf_keyval.iteritems():
562 data['attribute'] = key
Allen Li64692a32016-11-04 13:35:19 -0700563 if math.isnan(value) or math.isinf(value):
564 data['value'] = None
565 else:
566 data['value'] = value
showardeab66ce2009-12-23 00:03:56 +0000567 self.insert('tko_iteration_result', data,
mbligh432bad42007-10-09 19:56:07 +0000568 commit=commit)
mbligh056d0d32006-10-08 22:31:10 +0000569
Dennis Jeffrey368c54b2013-07-24 11:19:03 -0700570 data = {'test_idx': test_idx}
Dennis Jeffrey368c54b2013-07-24 11:19:03 -0700571
jadmanski0afbb632008-06-06 21:10:57 +0000572 for key, value in test.attributes.iteritems():
573 data = {'test_idx': test_idx, 'attribute': key,
574 'value': value}
showardeab66ce2009-12-23 00:03:56 +0000575 self.insert('tko_test_attributes', data, commit=commit)
mbligh2bd48872007-09-20 18:32:25 +0000576
jadmanski9b6babf2009-04-21 17:57:40 +0000577 if not is_update:
578 for label_index in test.labels:
579 data = {'test_id': test_idx, 'testlabel_id': label_index}
showardeab66ce2009-12-23 00:03:56 +0000580 self.insert('tko_test_labels_tests', data, commit=commit)
jadmanski9b6babf2009-04-21 17:57:40 +0000581
mbligh056d0d32006-10-08 22:31:10 +0000582
jadmanski0afbb632008-06-06 21:10:57 +0000583 def read_machine_map(self):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700584 """Reads the machine map."""
showard71b94312009-08-20 23:40:02 +0000585 if self.machine_group or not self.machine_map:
586 return
jadmanski0afbb632008-06-06 21:10:57 +0000587 for line in open(self.machine_map, 'r').readlines():
588 (machine, group) = line.split()
589 self.machine_group[machine] = group
mbligh96b9a5a2007-11-24 19:32:20 +0000590
591
showard71b94312009-08-20 23:40:02 +0000592 def machine_info_dict(self, job):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700593 """Reads the machine information of a job.
594
595 @param job: The job object.
596
597 @return: The machine info dictionary.
598 """
jadmanski0afbb632008-06-06 21:10:57 +0000599 hostname = job.machine
showard71b94312009-08-20 23:40:02 +0000600 group = job.machine_group
601 owner = job.machine_owner
jadmanski0afbb632008-06-06 21:10:57 +0000602
603 if not group:
showard71b94312009-08-20 23:40:02 +0000604 self.read_machine_map()
jadmanski0afbb632008-06-06 21:10:57 +0000605 group = self.machine_group.get(hostname, hostname)
showard71b94312009-08-20 23:40:02 +0000606 if group == hostname and owner:
607 group = owner + '/' + hostname
jadmanski0afbb632008-06-06 21:10:57 +0000608
showard71b94312009-08-20 23:40:02 +0000609 return {'hostname': hostname, 'machine_group': group, 'owner': owner}
610
611
Prathmesh Prabhuec96d6e2018-04-18 18:13:50 -0700612 def insert_or_update_machine(self, job, commit=None):
613 """Insert or updates machine information for the given job.
614
615 Also updates the job object with new machine index, if any.
616
617 @param job: tko.models.job object.
618 @param commit: Whether to commit the database transaction.
619 """
620 job.machine_idx = self._lookup_machine(job.machine)
621 if not job.machine_idx:
622 job.machine_idx = self._insert_machine(job, commit=commit)
623 elif job.machine:
624 # Only try to update tko_machines record if machine is set. This
625 # prevents unnecessary db writes for suite jobs.
626 self._update_machine_information(job, commit=commit)
627
628
Prathmesh Prabhuf5030d32018-04-19 16:08:55 -0700629 def _lookup_task_reference(self, job):
630 """Find the task_reference_id for a given job. Return None if not found.
631
632 @param job: tko.models.job object.
633 """
634 if job.index is None:
635 return None
636 rows = self.select(
637 'id', 'tko_task_references', {'tko_job_idx': job.index})
638 if not rows:
639 return None
640 if len(rows) > 1:
641 raise MySQLTooManyRows('Got %d tko_task_references for tko_job %d'
642 % (len(rows), job.index))
643 return rows[0][0]
644
645
Prathmesh Prabhu432648c2018-04-18 18:10:50 -0700646 def _insert_machine(self, job, commit = None):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700647 """Inserts the job machine.
648
649 @param job: The job object.
650 @param commit: If commit the transaction .
651 """
showard71b94312009-08-20 23:40:02 +0000652 machine_info = self.machine_info_dict(job)
showardeab66ce2009-12-23 00:03:56 +0000653 self.insert('tko_machines', machine_info, commit=commit)
jadmanski0afbb632008-06-06 21:10:57 +0000654 return self.get_last_autonumber_value()
655
656
Prathmesh Prabhu432648c2018-04-18 18:10:50 -0700657 def _update_machine_information(self, job, commit = None):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700658 """Updates the job machine information.
659
660 @param job: The job object.
661 @param commit: If commit the transaction .
662 """
showard71b94312009-08-20 23:40:02 +0000663 machine_info = self.machine_info_dict(job)
showardeab66ce2009-12-23 00:03:56 +0000664 self.update('tko_machines', machine_info,
showard71b94312009-08-20 23:40:02 +0000665 where={'hostname': machine_info['hostname']},
666 commit=commit)
667
668
Prathmesh Prabhu432648c2018-04-18 18:10:50 -0700669 def _lookup_machine(self, hostname):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700670 """Look up the machine information.
671
672 @param hostname: The hostname as string.
673 """
jadmanski0afbb632008-06-06 21:10:57 +0000674 where = { 'hostname' : hostname }
showardeab66ce2009-12-23 00:03:56 +0000675 rows = self.select('machine_idx', 'tko_machines', where)
jadmanski0afbb632008-06-06 21:10:57 +0000676 if rows:
677 return rows[0][0]
678 else:
679 return None
680
681
682 def lookup_kernel(self, kernel):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700683 """Look up the kernel.
684
685 @param kernel: The kernel object.
686 """
showardeab66ce2009-12-23 00:03:56 +0000687 rows = self.select('kernel_idx', 'tko_kernels',
jadmanski0afbb632008-06-06 21:10:57 +0000688 {'kernel_hash':kernel.kernel_hash})
689 if rows:
690 return rows[0][0]
691 else:
692 return None
693
694
695 def insert_kernel(self, kernel, commit = None):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700696 """Insert a kernel.
697
698 @param kernel: The kernel object.
699 @param commit: If commit the transaction .
700 """
jadmanski0afbb632008-06-06 21:10:57 +0000701 kver = self.lookup_kernel(kernel)
702 if kver:
703 return kver
704
705 # If this kernel has any significant patches, append their hash
706 # as diferentiator.
707 printable = kernel.base
708 patch_count = 0
709 for patch in kernel.patches:
710 match = re.match(r'.*(-mm[0-9]+|-git[0-9]+)\.(bz2|gz)$',
711 patch.reference)
712 if not match:
713 patch_count += 1
714
showardeab66ce2009-12-23 00:03:56 +0000715 self.insert('tko_kernels',
jadmanski0afbb632008-06-06 21:10:57 +0000716 {'base':kernel.base,
717 'kernel_hash':kernel.kernel_hash,
718 'printable':printable},
719 commit=commit)
720 kver = self.get_last_autonumber_value()
721
722 if patch_count > 0:
723 printable += ' p%d' % (kver)
showardeab66ce2009-12-23 00:03:56 +0000724 self.update('tko_kernels',
jadmanski0afbb632008-06-06 21:10:57 +0000725 {'printable':printable},
726 {'kernel_idx':kver})
727
728 for patch in kernel.patches:
729 self.insert_patch(kver, patch, commit=commit)
730 return kver
731
732
733 def insert_patch(self, kver, patch, commit = None):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700734 """Insert a kernel patch.
735
736 @param kver: The kernel version.
737 @param patch: The kernel patch object.
738 @param commit: If commit the transaction .
739 """
jadmanski0afbb632008-06-06 21:10:57 +0000740 print patch.reference
741 name = os.path.basename(patch.reference)[:80]
showardeab66ce2009-12-23 00:03:56 +0000742 self.insert('tko_patches',
jadmanski0afbb632008-06-06 21:10:57 +0000743 {'kernel_idx': kver,
744 'name':name,
745 'url':patch.reference,
746 'hash':patch.hash},
747 commit=commit)
748
749
jadmanski74eebf32008-07-15 20:04:42 +0000750 def find_test(self, job_idx, testname, subdir):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700751 """Find a test by name.
752
753 @param job_idx: The job index.
754 @param testname: The test name.
755 @param subdir: The test sub directory under the job directory.
756 """
jadmanski74eebf32008-07-15 20:04:42 +0000757 where = {'job_idx': job_idx , 'test': testname, 'subdir': subdir}
showardeab66ce2009-12-23 00:03:56 +0000758 rows = self.select('test_idx', 'tko_tests', where)
jadmanski0afbb632008-06-06 21:10:57 +0000759 if rows:
760 return rows[0][0]
761 else:
762 return None
763
764
765 def find_tests(self, job_idx):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700766 """Find all tests by job index.
767
768 @param job_idx: The job index.
769 @return: A list of tests.
770 """
jadmanski0afbb632008-06-06 21:10:57 +0000771 where = { 'job_idx':job_idx }
showardeab66ce2009-12-23 00:03:56 +0000772 rows = self.select('test_idx', 'tko_tests', where)
jadmanski0afbb632008-06-06 21:10:57 +0000773 if rows:
774 return [row[0] for row in rows]
775 else:
776 return []
777
778
779 def find_job(self, tag):
Michael Tang5f74ffd2016-10-31 10:34:53 -0700780 """Find a job by tag.
781
782 @param tag: The job tag name.
783 @return: The job object or None.
784 """
showardeab66ce2009-12-23 00:03:56 +0000785 rows = self.select('job_idx', 'tko_jobs', {'tag': tag})
jadmanski0afbb632008-06-06 21:10:57 +0000786 if rows:
787 return rows[0][0]
788 else:
789 return None
mblighaf25f062007-12-03 17:48:35 +0000790
791
mbligh96cf0512008-04-17 15:25:38 +0000792def _get_db_type():
jadmanski0afbb632008-06-06 21:10:57 +0000793 """Get the database type name to use from the global config."""
Jakob Juelich934f0dc2014-10-14 18:21:13 -0700794 get_value = global_config.global_config.get_config_value_with_fallback
795 return "db_" + get_value("AUTOTEST_WEB", "global_db_type", "db_type",
796 default="mysql")
mblighaf25f062007-12-03 17:48:35 +0000797
mbligh96cf0512008-04-17 15:25:38 +0000798
799def _get_error_class(class_name):
jadmanski0afbb632008-06-06 21:10:57 +0000800 """Retrieves the appropriate error class by name from the database
801 module."""
802 db_module = __import__("autotest_lib.tko." + _get_db_type(),
803 globals(), locals(), ["driver"])
804 return getattr(db_module.driver, class_name)
mbligh96cf0512008-04-17 15:25:38 +0000805
806
807def db(*args, **dargs):
jadmanski0afbb632008-06-06 21:10:57 +0000808 """Creates an instance of the database class with the arguments
809 provided in args and dargs, using the database type specified by
Michael Tang5f74ffd2016-10-31 10:34:53 -0700810 the global configuration (defaulting to mysql).
811
812 @param args: The db_type arguments.
813 @param dargs: The db_type named arguments.
814
815 @return: An db object.
816 """
jadmanski0afbb632008-06-06 21:10:57 +0000817 db_type = _get_db_type()
818 db_module = __import__("autotest_lib.tko." + db_type, globals(),
819 locals(), [db_type])
820 db = getattr(db_module, db_type)(*args, **dargs)
821 return db