Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 1 | # 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 Li | 64692a3 | 2016-11-04 13:35:19 -0700 | [diff] [blame] | 5 | import math |
| 6 | import os |
| 7 | import random |
| 8 | import re |
| 9 | import sys |
| 10 | import time |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 11 | |
| 12 | import common |
| 13 | from autotest_lib.client.common_lib import global_config |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 14 | from autotest_lib.frontend import database_settings_helper |
jamesren | a12b8a0 | 2010-06-16 23:28:23 +0000 | [diff] [blame] | 15 | from autotest_lib.tko import utils |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 16 | |
mbligh | ed4d6dd | 2008-02-27 16:49:43 +0000 | [diff] [blame] | 17 | |
Allen Li | e0bb760 | 2016-11-29 13:45:53 -0800 | [diff] [blame] | 18 | def _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 | |
| 27 | def _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 | |
mbligh | aea0960 | 2008-04-16 22:59:37 +0000 | [diff] [blame] | 36 | class MySQLTooManyRows(Exception): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 37 | """Too many records.""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 38 | pass |
mbligh | aea0960 | 2008-04-16 22:59:37 +0000 | [diff] [blame] | 39 | |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 40 | |
mbligh | 7636b3a | 2008-06-11 15:44:01 +0000 | [diff] [blame] | 41 | class db_sql(object): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 42 | """Data access.""" |
| 43 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 44 | 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) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 49 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 50 | self.con = None |
| 51 | self._init_db() |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 52 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 53 | # if not present, insert statuses |
| 54 | self.status_idx = {} |
| 55 | self.status_word = {} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 56 | status_rows = self.select('status_idx, word', 'tko_status', None) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 57 | for s in status_rows: |
| 58 | self.status_idx[s[1]] = s[0] |
| 59 | self.status_word[s[0]] = s[1] |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 60 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 61 | 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 = {} |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 68 | |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 69 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 70 | def _load_config(self, host, database, user, password): |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 71 | """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() |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 89 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 90 | # grab the host, database |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 91 | self.host = host or database_settings['HOST'] |
| 92 | self.database = database or database_settings['NAME'] |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 93 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 94 | # grab the user and password |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 95 | self.user = user or database_settings['USER'] |
| 96 | self.password = password or database_settings['PASSWORD'] |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 97 | |
Michael Spang | 7a27347 | 2014-10-08 12:08:13 -0400 | [diff] [blame] | 98 | # grab the timeout configuration |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 99 | 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 Juelich | 475b82b | 2014-09-30 11:17:07 -0700 | [diff] [blame] | 105 | self.min_delay = get_value("AUTOTEST_WEB", "global_db_min_retry_delay", |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 106 | "min_retry_delay", type=int, default=20) |
Jakob Juelich | 475b82b | 2014-09-30 11:17:07 -0700 | [diff] [blame] | 107 | self.max_delay = get_value("AUTOTEST_WEB", "global_db_max_retry_delay", |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 108 | "max_retry_delay", type=int, default=60) |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 109 | |
Richard Barnette | 2468fbd | 2014-11-07 01:12:46 +0000 | [diff] [blame] | 110 | # 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 | |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 126 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 127 | 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 |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 132 | |
Prathmesh Prabhu | ba8adaf | 2017-07-05 13:07:05 -0700 | [diff] [blame] | 133 | # create the db connection and cursor |
| 134 | self.con = self.connect(self.host, self.database, |
| 135 | self.user, self.password, self.port) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 136 | self.cur = self.con.cursor() |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 137 | |
| 138 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 139 | def _random_delay(self): |
| 140 | delay = random.randint(self.min_delay, self.max_delay) |
| 141 | time.sleep(delay) |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 142 | |
| 143 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 144 | 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. |
jadmanski | e7a6909 | 2008-05-29 21:03:13 +0000 | [diff] [blame] | 149 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 150 | It can be safely used with transactions, but the |
| 151 | transaction start & end must be completely contained |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 152 | 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 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 158 | OperationalError = _get_error_class("OperationalError") |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 159 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 160 | success = False |
| 161 | start_time = time.time() |
| 162 | while not success: |
| 163 | try: |
| 164 | result = function(*args, **dargs) |
| 165 | except OperationalError, e: |
Allen Li | e0bb760 | 2016-11-29 13:45:53 -0800 | [diff] [blame] | 166 | _log_error("%s; retrying, don't panic yet" |
| 167 | % _format_operational_error(e)) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 168 | 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 Li | e0bb760 | 2016-11-29 13:45:53 -0800 | [diff] [blame] | 177 | _log_error('%s; panic now' |
| 178 | % _format_operational_error(e)) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 179 | else: |
| 180 | success = True |
| 181 | return result |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 182 | |
| 183 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 184 | def dprint(self, value): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 185 | """Print out debug value. |
| 186 | |
| 187 | @param value: The value to print out. |
| 188 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 189 | if self.debug: |
| 190 | sys.stdout.write('SQL: ' + str(value) + '\n') |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 191 | |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 192 | |
Dan Shi | e8e0c05 | 2015-09-01 00:27:27 -0700 | [diff] [blame] | 193 | def _commit(self): |
| 194 | """Private method for function commit to call for retry. |
| 195 | """ |
| 196 | return self.con.commit() |
| 197 | |
| 198 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 199 | def commit(self): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 200 | """Commit the sql transaction.""" |
Dan Shi | e8e0c05 | 2015-09-01 00:27:27 -0700 | [diff] [blame] | 201 | if self.autocommit: |
| 202 | return self.run_with_retry(self._commit) |
| 203 | else: |
| 204 | return self._commit() |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 205 | |
| 206 | |
Simran Basi | e129a96 | 2012-08-31 13:03:53 -0700 | [diff] [blame] | 207 | def rollback(self): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 208 | """Rollback the sql transaction.""" |
Simran Basi | e129a96 | 2012-08-31 13:03:53 -0700 | [diff] [blame] | 209 | self.con.rollback() |
| 210 | |
| 211 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 212 | def get_last_autonumber_value(self): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 213 | """Gets the last auto number. |
| 214 | |
| 215 | @return: The last auto number. |
| 216 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 217 | self.cur.execute('SELECT LAST_INSERT_ID()', []) |
| 218 | return self.cur.fetchall()[0][0] |
mbligh | e12b861 | 2008-02-12 20:58:14 +0000 | [diff] [blame] | 219 | |
| 220 | |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 221 | 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): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 257 | """\ |
| 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). |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 267 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 268 | For example: |
| 269 | where = ("a = %s AND b = %s", ['val', 'val']) |
| 270 | is better than |
| 271 | where = "a = 'val' AND b = 'val'" |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 272 | |
| 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. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 279 | """ |
| 280 | cmd = ['select'] |
| 281 | if distinct: |
| 282 | cmd.append('distinct') |
| 283 | cmd += [fields, 'from', table] |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 284 | |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 285 | where_clause, values = self._where_clause(where) |
| 286 | cmd.append(where_clause) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 287 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 288 | if group_by: |
| 289 | cmd.append(' GROUP BY ' + group_by) |
mbligh | 83f63a0 | 2007-12-12 19:13:04 +0000 | [diff] [blame] | 290 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 291 | self.dprint('%s %s' % (' '.join(cmd), values)) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 292 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 293 | # create a re-runable function for executing the query |
| 294 | def exec_sql(): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 295 | """Exeuctes an the sql command.""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 296 | sql = ' '.join(cmd) |
| 297 | numRec = self.cur.execute(sql, values) |
mbligh | d876f45 | 2008-12-03 15:09:17 +0000 | [diff] [blame] | 298 | if max_rows is not None and numRec > max_rows: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 299 | msg = 'Exceeded allowed number of records' |
| 300 | raise MySQLTooManyRows(msg) |
| 301 | return self.cur.fetchall() |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 302 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 303 | # 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() |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 308 | |
mbligh | 056d0d3 | 2006-10-08 22:31:10 +0000 | [diff] [blame] | 309 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 310 | def select_sql(self, fields, table, sql, values): |
| 311 | """\ |
| 312 | select fields from table "sql" |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 313 | |
| 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. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 318 | """ |
| 319 | cmd = 'select %s from %s %s' % (fields, table, sql) |
| 320 | self.dprint(cmd) |
mbligh | 414c69e | 2007-10-05 15:13:06 +0000 | [diff] [blame] | 321 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 322 | # create a -re-runable function for executing the query |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 323 | def _exec_sql(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 324 | self.cur.execute(cmd, values) |
| 325 | return self.cur.fetchall() |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 326 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 327 | # run the query, re-trying after operational errors |
| 328 | if self.autocommit: |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 329 | return self.run_with_retry(_exec_sql) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 330 | else: |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 331 | return _exec_sql() |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 332 | |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 333 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 334 | def _exec_sql_with_commit(self, sql, values, commit): |
| 335 | if self.autocommit: |
| 336 | # re-run the query until it succeeds |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 337 | def _exec_sql(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 338 | self.cur.execute(sql, values) |
| 339 | self.con.commit() |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 340 | self.run_with_retry(_exec_sql) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 341 | else: |
| 342 | # take one shot at running the query |
| 343 | self.cur.execute(sql, values) |
| 344 | if commit: |
| 345 | self.con.commit() |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 346 | |
mbligh | 2bd4887 | 2007-09-20 18:32:25 +0000 | [diff] [blame] | 347 | |
jadmanski | b591fba | 2008-09-10 16:19:22 +0000 | [diff] [blame] | 348 | def insert(self, table, data, commit=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 349 | """\ |
| 350 | 'insert into table (keys) values (%s ... %s)', values |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 351 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 352 | data: |
| 353 | dictionary of fields and data |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 354 | |
| 355 | @param table: The name of the table. |
| 356 | @param data: The insert data. |
| 357 | @param commit: If commit the transaction . |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 358 | """ |
| 359 | fields = data.keys() |
| 360 | refs = ['%s' for field in fields] |
| 361 | values = [data[field] for field in fields] |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 362 | cmd = ('insert into %s (%s) values (%s)' % |
| 363 | (table, ','.join(self._quote(field) for field in fields), |
| 364 | ','.join(refs))) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 365 | self.dprint('%s %s' % (cmd, values)) |
mbligh | e9cf9d4 | 2007-08-31 08:56:00 +0000 | [diff] [blame] | 366 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 367 | self._exec_sql_with_commit(cmd, values, commit) |
mbligh | e9cf9d4 | 2007-08-31 08:56:00 +0000 | [diff] [blame] | 368 | |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 369 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 370 | def delete(self, table, where, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 371 | """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 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 377 | cmd = ['delete from', table] |
mbligh | d876f45 | 2008-12-03 15:09:17 +0000 | [diff] [blame] | 378 | if commit is None: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 379 | commit = self.autocommit |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 380 | where_clause, values = self._where_clause(where) |
| 381 | cmd.append(where_clause) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 382 | sql = ' '.join(cmd) |
| 383 | self.dprint('%s %s' % (sql, values)) |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 384 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 385 | self._exec_sql_with_commit(sql, values, commit) |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 386 | |
mbligh | 7a41a86 | 2007-11-30 17:44:24 +0000 | [diff] [blame] | 387 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 388 | def update(self, table, data, where, commit = None): |
| 389 | """\ |
| 390 | 'update table set data values (%s ... %s) where ...' |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 391 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 392 | data: |
| 393 | dictionary of fields and data |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 394 | |
| 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 . |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 399 | """ |
mbligh | d876f45 | 2008-12-03 15:09:17 +0000 | [diff] [blame] | 400 | if commit is None: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 401 | commit = self.autocommit |
| 402 | cmd = 'update %s ' % table |
| 403 | fields = data.keys() |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 404 | data_refs = [self._quote(field) + '=%s' for field in fields] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 405 | data_values = [data[field] for field in fields] |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 406 | cmd += ' set ' + ', '.join(data_refs) |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 407 | |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 408 | where_clause, where_values = self._where_clause(where) |
| 409 | cmd += where_clause |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 410 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 411 | values = data_values + where_values |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 412 | self.dprint('%s %s' % (cmd, values)) |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 413 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 414 | self._exec_sql_with_commit(cmd, values, commit) |
mbligh | e9cf9d4 | 2007-08-31 08:56:00 +0000 | [diff] [blame] | 415 | |
| 416 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 417 | def delete_job(self, tag, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 418 | """Delete a tko job. |
| 419 | |
| 420 | @param tag: The job tag. |
| 421 | @param commit: If commit the transaction . |
| 422 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 423 | job_idx = self.find_job(tag) |
| 424 | for test_idx in self.find_tests(job_idx): |
| 425 | where = {'test_idx' : test_idx} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 426 | self.delete('tko_iteration_result', where) |
Dennis Jeffrey | 368c54b | 2013-07-24 11:19:03 -0700 | [diff] [blame] | 427 | self.delete('tko_iteration_perf_value', where) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 428 | self.delete('tko_iteration_attributes', where) |
| 429 | self.delete('tko_test_attributes', where) |
| 430 | self.delete('tko_test_labels_tests', {'test_id': test_idx}) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 431 | where = {'job_idx' : job_idx} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 432 | self.delete('tko_tests', where) |
| 433 | self.delete('tko_jobs', where) |
apw | 7a7316b | 2008-02-21 17:42:05 +0000 | [diff] [blame] | 434 | |
apw | 7a7316b | 2008-02-21 17:42:05 +0000 | [diff] [blame] | 435 | |
Prathmesh Prabhu | 30dee86 | 2018-04-18 20:24:20 -0700 | [diff] [blame] | 436 | def insert_job(self, tag, job, commit=None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 437 | """Insert a tko job. |
| 438 | |
| 439 | @param tag: The job tag. |
| 440 | @param job: The job object. |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 441 | @param commit: If commit the transaction . |
| 442 | """ |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 443 | data = self._get_common_job_data(tag, job) |
| 444 | data.update({ |
Prathmesh Prabhu | 30dee86 | 2018-04-18 20:24:20 -0700 | [diff] [blame] | 445 | 'afe_job_id': job.afe_job_id, |
| 446 | 'afe_parent_job_id': job.afe_parent_job_id, |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 447 | }) |
Prathmesh Prabhu | c2a8a6a | 2018-04-19 16:23:32 -0700 | [diff] [blame^] | 448 | if job.job_idx is not None: |
| 449 | self.update( |
| 450 | 'tko_jobs', data, {'job_idx': job.job_idx}, commit=commit) |
showard | 0fec8a0 | 2009-12-04 01:19:54 +0000 | [diff] [blame] | 451 | else: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 452 | self.insert('tko_jobs', data, commit=commit) |
Prathmesh Prabhu | c2a8a6a | 2018-04-19 16:23:32 -0700 | [diff] [blame^] | 453 | job.job_idx = self.get_last_autonumber_value() |
Shuqian Zhao | 31425d5 | 2016-12-07 09:35:03 -0800 | [diff] [blame] | 454 | |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 455 | |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 456 | def _get_common_job_data(self, tag, job): |
| 457 | """Construct a dictionary with the common data to insert in job/task.""" |
| 458 | return { |
| 459 | 'tag':tag, |
| 460 | 'label': job.label, |
| 461 | 'username': job.user, |
| 462 | 'machine_idx': job.machine_idx, |
| 463 | 'queued_time': job.queued_time, |
| 464 | 'started_time': job.started_time, |
| 465 | 'finished_time': job.finished_time, |
| 466 | 'build': job.build, |
| 467 | 'build_version': job.build_version, |
| 468 | 'board': job.board, |
| 469 | 'suite': job.suite, |
| 470 | } |
| 471 | |
| 472 | |
| 473 | def insert_or_update_task_reference(self, job, reference_type, commit=None): |
Prathmesh Prabhu | f5030d3 | 2018-04-19 16:08:55 -0700 | [diff] [blame] | 474 | """Insert an entry in the tko_task_references table. |
| 475 | |
| 476 | The job should already have been inserted in tko_jobs. |
| 477 | @param job: tko.models.job object. |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 478 | @param reference_type: The type of reference to insert. |
| 479 | One of: {'afe', 'skylab'} |
Prathmesh Prabhu | f5030d3 | 2018-04-19 16:08:55 -0700 | [diff] [blame] | 480 | @param commit: Whether to commit this transaction. |
| 481 | """ |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 482 | assert reference_type in {'afe', 'skylab'} |
| 483 | if reference_type == 'afe': |
| 484 | task_id = job.afe_job_id |
| 485 | parent_task_id = job.afe_parent_job_id |
| 486 | else: |
| 487 | task_id = job.skylab_task_id |
| 488 | parent_task_id = job.skylab_parent_task_id |
Prathmesh Prabhu | f5030d3 | 2018-04-19 16:08:55 -0700 | [diff] [blame] | 489 | data = { |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 490 | 'reference_type': reference_type, |
Prathmesh Prabhu | c2a8a6a | 2018-04-19 16:23:32 -0700 | [diff] [blame^] | 491 | 'tko_job_idx': job.job_idx, |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 492 | 'task_id': task_id, |
| 493 | 'parent_task_id': parent_task_id, |
Prathmesh Prabhu | f5030d3 | 2018-04-19 16:08:55 -0700 | [diff] [blame] | 494 | } |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 495 | |
Prathmesh Prabhu | f5030d3 | 2018-04-19 16:08:55 -0700 | [diff] [blame] | 496 | task_reference_id = self._lookup_task_reference(job) |
| 497 | if task_reference_id is not None: |
| 498 | self.update('tko_task_references', |
| 499 | data, |
| 500 | {'id': task_reference_id}, |
| 501 | commit=commit) |
| 502 | job.task_reference_id = task_reference_id |
| 503 | else: |
| 504 | self.insert('tko_task_references', data, commit=commit) |
| 505 | job.task_reference_id = self.get_last_autonumber_value() |
| 506 | |
| 507 | |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 508 | def update_job_keyvals(self, job, commit=None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 509 | """Updates the job key values. |
| 510 | |
| 511 | @param job: The job object. |
| 512 | @param commit: If commit the transaction . |
| 513 | """ |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 514 | for key, value in job.keyval_dict.iteritems(): |
Prathmesh Prabhu | c2a8a6a | 2018-04-19 16:23:32 -0700 | [diff] [blame^] | 515 | where = {'job_id': job.job_idx, 'key': key} |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 516 | data = dict(where, value=value) |
| 517 | exists = self.select('id', 'tko_job_keyvals', where=where) |
| 518 | |
| 519 | if exists: |
| 520 | self.update('tko_job_keyvals', data, where=where, commit=commit) |
| 521 | else: |
| 522 | self.insert('tko_job_keyvals', data, commit=commit) |
| 523 | |
| 524 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 525 | def insert_test(self, job, test, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 526 | """Inserts a job test. |
| 527 | |
| 528 | @param job: The job object. |
| 529 | @param test: The test object. |
| 530 | @param commit: If commit the transaction . |
| 531 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 532 | kver = self.insert_kernel(test.kernel, commit=commit) |
Prathmesh Prabhu | c2a8a6a | 2018-04-19 16:23:32 -0700 | [diff] [blame^] | 533 | data = {'job_idx':job.job_idx, 'test':test.testname, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 534 | 'subdir':test.subdir, 'kernel_idx':kver, |
| 535 | 'status':self.status_idx[test.status], |
| 536 | 'reason':test.reason, 'machine_idx':job.machine_idx, |
| 537 | 'started_time': test.started_time, |
| 538 | 'finished_time':test.finished_time} |
jadmanski | 9b6babf | 2009-04-21 17:57:40 +0000 | [diff] [blame] | 539 | is_update = hasattr(test, "test_idx") |
| 540 | if is_update: |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 541 | test_idx = test.test_idx |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 542 | self.update('tko_tests', data, |
| 543 | {'test_idx': test_idx}, commit=commit) |
jadmanski | b591fba | 2008-09-10 16:19:22 +0000 | [diff] [blame] | 544 | where = {'test_idx': test_idx} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 545 | self.delete('tko_iteration_result', where) |
Dennis Jeffrey | 368c54b | 2013-07-24 11:19:03 -0700 | [diff] [blame] | 546 | self.delete('tko_iteration_perf_value', where) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 547 | self.delete('tko_iteration_attributes', where) |
showard | 0fec8a0 | 2009-12-04 01:19:54 +0000 | [diff] [blame] | 548 | where['user_created'] = 0 |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 549 | self.delete('tko_test_attributes', where) |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 550 | else: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 551 | self.insert('tko_tests', data, commit=commit) |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 552 | test_idx = test.test_idx = self.get_last_autonumber_value() |
| 553 | data = {'test_idx': test_idx} |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 554 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 555 | for i in test.iterations: |
| 556 | data['iteration'] = i.index |
| 557 | for key, value in i.attr_keyval.iteritems(): |
| 558 | data['attribute'] = key |
| 559 | data['value'] = value |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 560 | self.insert('tko_iteration_attributes', data, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 561 | commit=commit) |
| 562 | for key, value in i.perf_keyval.iteritems(): |
| 563 | data['attribute'] = key |
Allen Li | 64692a3 | 2016-11-04 13:35:19 -0700 | [diff] [blame] | 564 | if math.isnan(value) or math.isinf(value): |
| 565 | data['value'] = None |
| 566 | else: |
| 567 | data['value'] = value |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 568 | self.insert('tko_iteration_result', data, |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 569 | commit=commit) |
mbligh | 056d0d3 | 2006-10-08 22:31:10 +0000 | [diff] [blame] | 570 | |
Dennis Jeffrey | 368c54b | 2013-07-24 11:19:03 -0700 | [diff] [blame] | 571 | data = {'test_idx': test_idx} |
Dennis Jeffrey | 368c54b | 2013-07-24 11:19:03 -0700 | [diff] [blame] | 572 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 573 | for key, value in test.attributes.iteritems(): |
| 574 | data = {'test_idx': test_idx, 'attribute': key, |
| 575 | 'value': value} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 576 | self.insert('tko_test_attributes', data, commit=commit) |
mbligh | 2bd4887 | 2007-09-20 18:32:25 +0000 | [diff] [blame] | 577 | |
jadmanski | 9b6babf | 2009-04-21 17:57:40 +0000 | [diff] [blame] | 578 | if not is_update: |
| 579 | for label_index in test.labels: |
| 580 | data = {'test_id': test_idx, 'testlabel_id': label_index} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 581 | self.insert('tko_test_labels_tests', data, commit=commit) |
jadmanski | 9b6babf | 2009-04-21 17:57:40 +0000 | [diff] [blame] | 582 | |
mbligh | 056d0d3 | 2006-10-08 22:31:10 +0000 | [diff] [blame] | 583 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 584 | def read_machine_map(self): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 585 | """Reads the machine map.""" |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 586 | if self.machine_group or not self.machine_map: |
| 587 | return |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 588 | for line in open(self.machine_map, 'r').readlines(): |
| 589 | (machine, group) = line.split() |
| 590 | self.machine_group[machine] = group |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 591 | |
| 592 | |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 593 | def machine_info_dict(self, job): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 594 | """Reads the machine information of a job. |
| 595 | |
| 596 | @param job: The job object. |
| 597 | |
| 598 | @return: The machine info dictionary. |
| 599 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 600 | hostname = job.machine |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 601 | group = job.machine_group |
| 602 | owner = job.machine_owner |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 603 | |
| 604 | if not group: |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 605 | self.read_machine_map() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 606 | group = self.machine_group.get(hostname, hostname) |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 607 | if group == hostname and owner: |
| 608 | group = owner + '/' + hostname |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 609 | |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 610 | return {'hostname': hostname, 'machine_group': group, 'owner': owner} |
| 611 | |
| 612 | |
Prathmesh Prabhu | ec96d6e | 2018-04-18 18:13:50 -0700 | [diff] [blame] | 613 | def insert_or_update_machine(self, job, commit=None): |
| 614 | """Insert or updates machine information for the given job. |
| 615 | |
| 616 | Also updates the job object with new machine index, if any. |
| 617 | |
| 618 | @param job: tko.models.job object. |
| 619 | @param commit: Whether to commit the database transaction. |
| 620 | """ |
| 621 | job.machine_idx = self._lookup_machine(job.machine) |
| 622 | if not job.machine_idx: |
| 623 | job.machine_idx = self._insert_machine(job, commit=commit) |
| 624 | elif job.machine: |
| 625 | # Only try to update tko_machines record if machine is set. This |
| 626 | # prevents unnecessary db writes for suite jobs. |
| 627 | self._update_machine_information(job, commit=commit) |
| 628 | |
| 629 | |
Prathmesh Prabhu | f5030d3 | 2018-04-19 16:08:55 -0700 | [diff] [blame] | 630 | def _lookup_task_reference(self, job): |
| 631 | """Find the task_reference_id for a given job. Return None if not found. |
| 632 | |
| 633 | @param job: tko.models.job object. |
| 634 | """ |
Prathmesh Prabhu | c2a8a6a | 2018-04-19 16:23:32 -0700 | [diff] [blame^] | 635 | if job.job_idx is None: |
Prathmesh Prabhu | f5030d3 | 2018-04-19 16:08:55 -0700 | [diff] [blame] | 636 | return None |
| 637 | rows = self.select( |
Prathmesh Prabhu | c2a8a6a | 2018-04-19 16:23:32 -0700 | [diff] [blame^] | 638 | 'id', 'tko_task_references', {'tko_job_idx': job.job_idx}) |
Prathmesh Prabhu | f5030d3 | 2018-04-19 16:08:55 -0700 | [diff] [blame] | 639 | if not rows: |
| 640 | return None |
| 641 | if len(rows) > 1: |
| 642 | raise MySQLTooManyRows('Got %d tko_task_references for tko_job %d' |
Prathmesh Prabhu | c2a8a6a | 2018-04-19 16:23:32 -0700 | [diff] [blame^] | 643 | % (len(rows), job.job_idx)) |
Prathmesh Prabhu | f5030d3 | 2018-04-19 16:08:55 -0700 | [diff] [blame] | 644 | return rows[0][0] |
| 645 | |
| 646 | |
Prathmesh Prabhu | 432648c | 2018-04-18 18:10:50 -0700 | [diff] [blame] | 647 | def _insert_machine(self, job, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 648 | """Inserts the job machine. |
| 649 | |
| 650 | @param job: The job object. |
| 651 | @param commit: If commit the transaction . |
| 652 | """ |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 653 | machine_info = self.machine_info_dict(job) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 654 | self.insert('tko_machines', machine_info, commit=commit) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 655 | return self.get_last_autonumber_value() |
| 656 | |
| 657 | |
Prathmesh Prabhu | 432648c | 2018-04-18 18:10:50 -0700 | [diff] [blame] | 658 | def _update_machine_information(self, job, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 659 | """Updates the job machine information. |
| 660 | |
| 661 | @param job: The job object. |
| 662 | @param commit: If commit the transaction . |
| 663 | """ |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 664 | machine_info = self.machine_info_dict(job) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 665 | self.update('tko_machines', machine_info, |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 666 | where={'hostname': machine_info['hostname']}, |
| 667 | commit=commit) |
| 668 | |
| 669 | |
Prathmesh Prabhu | 432648c | 2018-04-18 18:10:50 -0700 | [diff] [blame] | 670 | def _lookup_machine(self, hostname): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 671 | """Look up the machine information. |
| 672 | |
| 673 | @param hostname: The hostname as string. |
| 674 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 675 | where = { 'hostname' : hostname } |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 676 | rows = self.select('machine_idx', 'tko_machines', where) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 677 | if rows: |
| 678 | return rows[0][0] |
| 679 | else: |
| 680 | return None |
| 681 | |
| 682 | |
| 683 | def lookup_kernel(self, kernel): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 684 | """Look up the kernel. |
| 685 | |
| 686 | @param kernel: The kernel object. |
| 687 | """ |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 688 | rows = self.select('kernel_idx', 'tko_kernels', |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 689 | {'kernel_hash':kernel.kernel_hash}) |
| 690 | if rows: |
| 691 | return rows[0][0] |
| 692 | else: |
| 693 | return None |
| 694 | |
| 695 | |
| 696 | def insert_kernel(self, kernel, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 697 | """Insert a kernel. |
| 698 | |
| 699 | @param kernel: The kernel object. |
| 700 | @param commit: If commit the transaction . |
| 701 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 702 | kver = self.lookup_kernel(kernel) |
| 703 | if kver: |
| 704 | return kver |
| 705 | |
| 706 | # If this kernel has any significant patches, append their hash |
| 707 | # as diferentiator. |
| 708 | printable = kernel.base |
| 709 | patch_count = 0 |
| 710 | for patch in kernel.patches: |
| 711 | match = re.match(r'.*(-mm[0-9]+|-git[0-9]+)\.(bz2|gz)$', |
| 712 | patch.reference) |
| 713 | if not match: |
| 714 | patch_count += 1 |
| 715 | |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 716 | self.insert('tko_kernels', |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 717 | {'base':kernel.base, |
| 718 | 'kernel_hash':kernel.kernel_hash, |
| 719 | 'printable':printable}, |
| 720 | commit=commit) |
| 721 | kver = self.get_last_autonumber_value() |
| 722 | |
| 723 | if patch_count > 0: |
| 724 | printable += ' p%d' % (kver) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 725 | self.update('tko_kernels', |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 726 | {'printable':printable}, |
| 727 | {'kernel_idx':kver}) |
| 728 | |
| 729 | for patch in kernel.patches: |
| 730 | self.insert_patch(kver, patch, commit=commit) |
| 731 | return kver |
| 732 | |
| 733 | |
| 734 | def insert_patch(self, kver, patch, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 735 | """Insert a kernel patch. |
| 736 | |
| 737 | @param kver: The kernel version. |
| 738 | @param patch: The kernel patch object. |
| 739 | @param commit: If commit the transaction . |
| 740 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 741 | print patch.reference |
| 742 | name = os.path.basename(patch.reference)[:80] |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 743 | self.insert('tko_patches', |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 744 | {'kernel_idx': kver, |
| 745 | 'name':name, |
| 746 | 'url':patch.reference, |
| 747 | 'hash':patch.hash}, |
| 748 | commit=commit) |
| 749 | |
| 750 | |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 751 | def find_test(self, job_idx, testname, subdir): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 752 | """Find a test by name. |
| 753 | |
| 754 | @param job_idx: The job index. |
| 755 | @param testname: The test name. |
| 756 | @param subdir: The test sub directory under the job directory. |
| 757 | """ |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 758 | where = {'job_idx': job_idx , 'test': testname, 'subdir': subdir} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 759 | rows = self.select('test_idx', 'tko_tests', where) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 760 | if rows: |
| 761 | return rows[0][0] |
| 762 | else: |
| 763 | return None |
| 764 | |
| 765 | |
| 766 | def find_tests(self, job_idx): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 767 | """Find all tests by job index. |
| 768 | |
| 769 | @param job_idx: The job index. |
| 770 | @return: A list of tests. |
| 771 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 772 | where = { 'job_idx':job_idx } |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 773 | rows = self.select('test_idx', 'tko_tests', where) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 774 | if rows: |
| 775 | return [row[0] for row in rows] |
| 776 | else: |
| 777 | return [] |
| 778 | |
| 779 | |
| 780 | def find_job(self, tag): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 781 | """Find a job by tag. |
| 782 | |
| 783 | @param tag: The job tag name. |
| 784 | @return: The job object or None. |
| 785 | """ |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 786 | rows = self.select('job_idx', 'tko_jobs', {'tag': tag}) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 787 | if rows: |
| 788 | return rows[0][0] |
| 789 | else: |
| 790 | return None |
mbligh | af25f06 | 2007-12-03 17:48:35 +0000 | [diff] [blame] | 791 | |
| 792 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 793 | def _get_db_type(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 794 | """Get the database type name to use from the global config.""" |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 795 | get_value = global_config.global_config.get_config_value_with_fallback |
| 796 | return "db_" + get_value("AUTOTEST_WEB", "global_db_type", "db_type", |
| 797 | default="mysql") |
mbligh | af25f06 | 2007-12-03 17:48:35 +0000 | [diff] [blame] | 798 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 799 | |
| 800 | def _get_error_class(class_name): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 801 | """Retrieves the appropriate error class by name from the database |
| 802 | module.""" |
| 803 | db_module = __import__("autotest_lib.tko." + _get_db_type(), |
| 804 | globals(), locals(), ["driver"]) |
| 805 | return getattr(db_module.driver, class_name) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 806 | |
| 807 | |
| 808 | def db(*args, **dargs): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 809 | """Creates an instance of the database class with the arguments |
| 810 | provided in args and dargs, using the database type specified by |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 811 | the global configuration (defaulting to mysql). |
| 812 | |
| 813 | @param args: The db_type arguments. |
| 814 | @param dargs: The db_type named arguments. |
| 815 | |
| 816 | @return: An db object. |
| 817 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 818 | db_type = _get_db_type() |
| 819 | db_module = __import__("autotest_lib.tko." + db_type, globals(), |
| 820 | locals(), [db_type]) |
| 821 | db = getattr(db_module, db_type)(*args, **dargs) |
| 822 | return db |