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 | |
Aviv Keshet | 7f4071c | 2018-07-12 14:15:11 -0700 | [diff] [blame] | 5 | try: |
| 6 | import MySQLdb as driver |
| 7 | except ImportError: |
| 8 | # This module (tko) is unconditionally imported by autoserv, |
| 9 | # even in environments where MyQSLdb is unavailable. Thus, we |
| 10 | # need to cope with import failure here. |
| 11 | # See https://bugs.chromium.org/p/chromium/issues/detail?id=860166#c17 for |
| 12 | # context. |
| 13 | class UtterlyFakeDb(object): |
| 14 | """Lame fake of MySQLdb for import time needs of this file.""" |
| 15 | OperationalError = object() |
| 16 | |
| 17 | driver = UtterlyFakeDb |
| 18 | |
Allen Li | 64692a3 | 2016-11-04 13:35:19 -0700 | [diff] [blame] | 19 | import math |
| 20 | import os |
| 21 | import random |
| 22 | import re |
| 23 | import sys |
| 24 | import time |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 25 | |
| 26 | import common |
| 27 | from autotest_lib.client.common_lib import global_config |
Aviv Keshet | c53e005 | 2018-07-12 18:37:44 -0700 | [diff] [blame] | 28 | from autotest_lib.client.common_lib import utils |
Aviv Keshet | 7f4071c | 2018-07-12 14:15:11 -0700 | [diff] [blame] | 29 | from autotest_lib.client.common_lib.cros import retry |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 30 | from autotest_lib.frontend import database_settings_helper |
mbligh | ed4d6dd | 2008-02-27 16:49:43 +0000 | [diff] [blame] | 31 | |
Aviv Keshet | c53e005 | 2018-07-12 18:37:44 -0700 | [diff] [blame] | 32 | try: |
| 33 | from chromite.lib import metrics |
| 34 | except ImportError: |
| 35 | metrics = utils.metrics_mock |
| 36 | |
| 37 | |
Allen Li | e0bb760 | 2016-11-29 13:45:53 -0800 | [diff] [blame] | 38 | def _log_error(msg): |
| 39 | """Log an error message. |
| 40 | |
| 41 | @param msg: Message string |
| 42 | """ |
| 43 | print >> sys.stderr, msg |
| 44 | sys.stderr.flush() # we want these msgs to show up immediately |
| 45 | |
| 46 | |
| 47 | def _format_operational_error(e): |
Laurence Goodby | 0cb6195 | 2018-07-04 16:52:06 +0000 | [diff] [blame] | 48 | """Format OperationalError. |
Allen Li | e0bb760 | 2016-11-29 13:45:53 -0800 | [diff] [blame] | 49 | |
Laurence Goodby | 0cb6195 | 2018-07-04 16:52:06 +0000 | [diff] [blame] | 50 | @param e: OperationalError instance. |
Allen Li | e0bb760 | 2016-11-29 13:45:53 -0800 | [diff] [blame] | 51 | """ |
| 52 | return ("%s: An operational error occurred during a database " |
| 53 | "operation: %s" % (time.strftime("%X %x"), str(e))) |
| 54 | |
| 55 | |
mbligh | aea0960 | 2008-04-16 22:59:37 +0000 | [diff] [blame] | 56 | class MySQLTooManyRows(Exception): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 57 | """Too many records.""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 58 | pass |
mbligh | aea0960 | 2008-04-16 22:59:37 +0000 | [diff] [blame] | 59 | |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 60 | |
Aviv Keshet | c53e005 | 2018-07-12 18:37:44 -0700 | [diff] [blame] | 61 | def _connection_retry_callback(): |
| 62 | """Callback method used to increment a retry metric.""" |
| 63 | metrics.Counter('chromeos/autotest/tko/connection_retries').increment() |
| 64 | |
| 65 | |
Laurence Goodby | 0cb6195 | 2018-07-04 16:52:06 +0000 | [diff] [blame] | 66 | class db_sql(object): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 67 | """Data access.""" |
| 68 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 69 | def __init__(self, debug=False, autocommit=True, host=None, |
| 70 | database=None, user=None, password=None): |
| 71 | self.debug = debug |
| 72 | self.autocommit = autocommit |
| 73 | self._load_config(host, database, user, password) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 74 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 75 | self.con = None |
| 76 | self._init_db() |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 77 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 78 | # if not present, insert statuses |
| 79 | self.status_idx = {} |
| 80 | self.status_word = {} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 81 | status_rows = self.select('status_idx, word', 'tko_status', None) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 82 | for s in status_rows: |
| 83 | self.status_idx[s[1]] = s[0] |
| 84 | self.status_word[s[0]] = s[1] |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 85 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 86 | machine_map = os.path.join(os.path.dirname(__file__), |
| 87 | 'machines') |
| 88 | if os.path.exists(machine_map): |
| 89 | self.machine_map = machine_map |
| 90 | else: |
| 91 | self.machine_map = None |
| 92 | self.machine_group = {} |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 93 | |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 94 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 95 | def _load_config(self, host, database, user, password): |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 96 | """Loads configuration settings required to connect to the database. |
| 97 | |
| 98 | This will try to connect to use the settings prefixed with global_db_. |
| 99 | If they do not exist, they un-prefixed settings will be used. |
| 100 | |
| 101 | If parameters are supplied, these will be taken instead of the values |
| 102 | in global_config. |
| 103 | |
| 104 | @param host: If set, this host will be used, if not, the host will be |
| 105 | retrieved from global_config. |
| 106 | @param database: If set, this database will be used, if not, the |
| 107 | database will be retrieved from global_config. |
| 108 | @param user: If set, this user will be used, if not, the |
| 109 | user will be retrieved from global_config. |
| 110 | @param password: If set, this password will be used, if not, the |
| 111 | password will be retrieved from global_config. |
| 112 | """ |
| 113 | database_settings = database_settings_helper.get_global_db_config() |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 114 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 115 | # grab the host, database |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 116 | self.host = host or database_settings['HOST'] |
| 117 | self.database = database or database_settings['NAME'] |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 118 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 119 | # grab the user and password |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 120 | self.user = user or database_settings['USER'] |
| 121 | self.password = password or database_settings['PASSWORD'] |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 122 | |
Michael Spang | 7a27347 | 2014-10-08 12:08:13 -0400 | [diff] [blame] | 123 | # grab the timeout configuration |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 124 | self.query_timeout =( |
| 125 | database_settings.get('OPTIONS', {}).get('timeout', 3600)) |
| 126 | |
| 127 | # Using fallback to non-global in order to work without configuration |
| 128 | # overhead on non-shard instances. |
| 129 | get_value = global_config.global_config.get_config_value_with_fallback |
Jakob Juelich | 475b82b | 2014-09-30 11:17:07 -0700 | [diff] [blame] | 130 | self.min_delay = get_value("AUTOTEST_WEB", "global_db_min_retry_delay", |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 131 | "min_retry_delay", type=int, default=20) |
Jakob Juelich | 475b82b | 2014-09-30 11:17:07 -0700 | [diff] [blame] | 132 | self.max_delay = get_value("AUTOTEST_WEB", "global_db_max_retry_delay", |
Jakob Juelich | 934f0dc | 2014-10-14 18:21:13 -0700 | [diff] [blame] | 133 | "max_retry_delay", type=int, default=60) |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 134 | |
Richard Barnette | 2468fbd | 2014-11-07 01:12:46 +0000 | [diff] [blame] | 135 | # TODO(beeps): Move this to django settings once we have routers. |
| 136 | # On test instances mysql connects through a different port. No point |
| 137 | # piping this through our entire infrastructure when it is only really |
| 138 | # used for testing; Ideally we would specify this through django |
| 139 | # settings and default it to the empty string so django will figure out |
| 140 | # the default based on the database backend (eg: mysql, 3306), but until |
| 141 | # we have database routers in place any django settings will apply to |
| 142 | # both tko and afe. |
| 143 | # The intended use of this port is to allow a testing shard vm to |
| 144 | # update the master vm's database with test results. Specifying |
| 145 | # and empty string will fallback to not even specifying the port |
| 146 | # to the backend in tko/db.py. Unfortunately this means retries |
| 147 | # won't work on the test cluster till we've migrated to routers. |
| 148 | self.port = global_config.global_config.get_config_value( |
| 149 | "AUTOTEST_WEB", "global_db_port", type=str, default='') |
| 150 | |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 151 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 152 | def _init_db(self): |
| 153 | # make sure we clean up any existing connection |
| 154 | if self.con: |
| 155 | self.con.close() |
| 156 | self.con = None |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 157 | |
Prathmesh Prabhu | ba8adaf | 2017-07-05 13:07:05 -0700 | [diff] [blame] | 158 | # create the db connection and cursor |
Laurence Goodby | 0cb6195 | 2018-07-04 16:52:06 +0000 | [diff] [blame] | 159 | self.con = self.connect(self.host, self.database, |
Prathmesh Prabhu | ba8adaf | 2017-07-05 13:07:05 -0700 | [diff] [blame] | 160 | self.user, self.password, self.port) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 161 | self.cur = self.con.cursor() |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 162 | |
| 163 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 164 | def _random_delay(self): |
| 165 | delay = random.randint(self.min_delay, self.max_delay) |
| 166 | time.sleep(delay) |
mbligh | 65acae5 | 2008-04-24 20:21:55 +0000 | [diff] [blame] | 167 | |
| 168 | |
Aviv Keshet | 7f4071c | 2018-07-12 14:15:11 -0700 | [diff] [blame] | 169 | @retry.retry(driver.OperationalError, timeout_min=10, |
Aviv Keshet | c53e005 | 2018-07-12 18:37:44 -0700 | [diff] [blame] | 170 | delay_sec=5, callback=_connection_retry_callback) |
Aviv Keshet | 7f4071c | 2018-07-12 14:15:11 -0700 | [diff] [blame] | 171 | def connect(self, host, database, user, password, port): |
| 172 | """Open and return a connection to mysql database.""" |
| 173 | connection_args = { |
| 174 | 'host': host, |
| 175 | 'user': user, |
| 176 | 'db': database, |
| 177 | 'passwd': password, |
| 178 | 'connect_timeout': 20, |
| 179 | } |
| 180 | if port: |
| 181 | connection_args['port'] = int(port) |
| 182 | return driver.connect(**connection_args) |
| 183 | |
| 184 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 185 | def run_with_retry(self, function, *args, **dargs): |
| 186 | """Call function(*args, **dargs) until either it passes |
| 187 | without an operational error, or a timeout is reached. |
| 188 | This will re-connect to the database, so it is NOT safe |
| 189 | to use this inside of a database transaction. |
jadmanski | e7a6909 | 2008-05-29 21:03:13 +0000 | [diff] [blame] | 190 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 191 | It can be safely used with transactions, but the |
| 192 | transaction start & end must be completely contained |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 193 | within the call to 'function'. |
| 194 | |
| 195 | @param function: The function to run with retry. |
| 196 | @param args: The arguments |
| 197 | @param dargs: The named arguments. |
| 198 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 199 | success = False |
| 200 | start_time = time.time() |
| 201 | while not success: |
| 202 | try: |
| 203 | result = function(*args, **dargs) |
Aviv Keshet | 7f4071c | 2018-07-12 14:15:11 -0700 | [diff] [blame] | 204 | except driver.OperationalError, e: |
Allen Li | e0bb760 | 2016-11-29 13:45:53 -0800 | [diff] [blame] | 205 | _log_error("%s; retrying, don't panic yet" |
| 206 | % _format_operational_error(e)) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 207 | stop_time = time.time() |
| 208 | elapsed_time = stop_time - start_time |
| 209 | if elapsed_time > self.query_timeout: |
| 210 | raise |
| 211 | else: |
| 212 | try: |
| 213 | self._random_delay() |
| 214 | self._init_db() |
Aviv Keshet | 7f4071c | 2018-07-12 14:15:11 -0700 | [diff] [blame] | 215 | except driver.OperationalError, e: |
Allen Li | e0bb760 | 2016-11-29 13:45:53 -0800 | [diff] [blame] | 216 | _log_error('%s; panic now' |
| 217 | % _format_operational_error(e)) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 218 | else: |
| 219 | success = True |
| 220 | return result |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 221 | |
| 222 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 223 | def dprint(self, value): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 224 | """Print out debug value. |
| 225 | |
| 226 | @param value: The value to print out. |
| 227 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 228 | if self.debug: |
| 229 | sys.stdout.write('SQL: ' + str(value) + '\n') |
mbligh | 8e1ab17 | 2007-09-13 17:29:56 +0000 | [diff] [blame] | 230 | |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 231 | |
Dan Shi | e8e0c05 | 2015-09-01 00:27:27 -0700 | [diff] [blame] | 232 | def _commit(self): |
| 233 | """Private method for function commit to call for retry. |
| 234 | """ |
| 235 | return self.con.commit() |
| 236 | |
| 237 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 238 | def commit(self): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 239 | """Commit the sql transaction.""" |
Dan Shi | e8e0c05 | 2015-09-01 00:27:27 -0700 | [diff] [blame] | 240 | if self.autocommit: |
| 241 | return self.run_with_retry(self._commit) |
| 242 | else: |
| 243 | return self._commit() |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 244 | |
| 245 | |
Simran Basi | e129a96 | 2012-08-31 13:03:53 -0700 | [diff] [blame] | 246 | def rollback(self): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 247 | """Rollback the sql transaction.""" |
Simran Basi | e129a96 | 2012-08-31 13:03:53 -0700 | [diff] [blame] | 248 | self.con.rollback() |
| 249 | |
| 250 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 251 | def get_last_autonumber_value(self): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 252 | """Gets the last auto number. |
| 253 | |
| 254 | @return: The last auto number. |
| 255 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 256 | self.cur.execute('SELECT LAST_INSERT_ID()', []) |
| 257 | return self.cur.fetchall()[0][0] |
mbligh | e12b861 | 2008-02-12 20:58:14 +0000 | [diff] [blame] | 258 | |
| 259 | |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 260 | def _quote(self, field): |
| 261 | return '`%s`' % field |
| 262 | |
| 263 | |
| 264 | def _where_clause(self, where): |
| 265 | if not where: |
| 266 | return '', [] |
| 267 | |
| 268 | if isinstance(where, dict): |
| 269 | # key/value pairs (which should be equal, or None for null) |
| 270 | keys, values = [], [] |
| 271 | for field, value in where.iteritems(): |
| 272 | quoted_field = self._quote(field) |
| 273 | if value is None: |
| 274 | keys.append(quoted_field + ' is null') |
| 275 | else: |
| 276 | keys.append(quoted_field + '=%s') |
| 277 | values.append(value) |
| 278 | where_clause = ' and '.join(keys) |
| 279 | elif isinstance(where, basestring): |
| 280 | # the exact string |
| 281 | where_clause = where |
| 282 | values = [] |
| 283 | elif isinstance(where, tuple): |
| 284 | # preformatted where clause + values |
| 285 | where_clause, values = where |
| 286 | assert where_clause |
| 287 | else: |
| 288 | raise ValueError('Invalid "where" value: %r' % where) |
| 289 | |
| 290 | return ' WHERE ' + where_clause, values |
| 291 | |
| 292 | |
| 293 | |
| 294 | def select(self, fields, table, where, distinct=False, group_by=None, |
| 295 | max_rows=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 296 | """\ |
| 297 | This selects all the fields requested from a |
| 298 | specific table with a particular where clause. |
| 299 | The where clause can either be a dictionary of |
| 300 | field=value pairs, a string, or a tuple of (string, |
| 301 | a list of values). The last option is what you |
| 302 | should use when accepting user input as it'll |
| 303 | protect you against sql injection attacks (if |
| 304 | all user data is placed in the array rather than |
| 305 | the raw SQL). |
mbligh | 12eebfa | 2008-01-03 02:01:53 +0000 | [diff] [blame] | 306 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 307 | For example: |
| 308 | where = ("a = %s AND b = %s", ['val', 'val']) |
| 309 | is better than |
| 310 | where = "a = 'val' AND b = 'val'" |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 311 | |
| 312 | @param fields: The list of selected fields string. |
| 313 | @param table: The name of the database table. |
| 314 | @param where: The where clause string. |
| 315 | @param distinct: If select distinct values. |
| 316 | @param group_by: Group by clause. |
| 317 | @param max_rows: unused. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 318 | """ |
| 319 | cmd = ['select'] |
| 320 | if distinct: |
| 321 | cmd.append('distinct') |
| 322 | cmd += [fields, 'from', table] |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 323 | |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 324 | where_clause, values = self._where_clause(where) |
| 325 | cmd.append(where_clause) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 326 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 327 | if group_by: |
| 328 | cmd.append(' GROUP BY ' + group_by) |
mbligh | 83f63a0 | 2007-12-12 19:13:04 +0000 | [diff] [blame] | 329 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 330 | self.dprint('%s %s' % (' '.join(cmd), values)) |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 331 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 332 | # create a re-runable function for executing the query |
| 333 | def exec_sql(): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 334 | """Exeuctes an the sql command.""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 335 | sql = ' '.join(cmd) |
| 336 | numRec = self.cur.execute(sql, values) |
mbligh | d876f45 | 2008-12-03 15:09:17 +0000 | [diff] [blame] | 337 | if max_rows is not None and numRec > max_rows: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 338 | msg = 'Exceeded allowed number of records' |
| 339 | raise MySQLTooManyRows(msg) |
| 340 | return self.cur.fetchall() |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 341 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 342 | # run the query, re-trying after operational errors |
| 343 | if self.autocommit: |
| 344 | return self.run_with_retry(exec_sql) |
| 345 | else: |
| 346 | return exec_sql() |
mbligh | d5c33db | 2006-10-08 21:34:16 +0000 | [diff] [blame] | 347 | |
mbligh | 056d0d3 | 2006-10-08 22:31:10 +0000 | [diff] [blame] | 348 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 349 | def select_sql(self, fields, table, sql, values): |
| 350 | """\ |
| 351 | select fields from table "sql" |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 352 | |
| 353 | @param fields: The list of selected fields string. |
| 354 | @param table: The name of the database table. |
| 355 | @param sql: The sql string. |
| 356 | @param values: The sql string parameter values. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 357 | """ |
| 358 | cmd = 'select %s from %s %s' % (fields, table, sql) |
| 359 | self.dprint(cmd) |
mbligh | 414c69e | 2007-10-05 15:13:06 +0000 | [diff] [blame] | 360 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 361 | # create a -re-runable function for executing the query |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 362 | def _exec_sql(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 363 | self.cur.execute(cmd, values) |
| 364 | return self.cur.fetchall() |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 365 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 366 | # run the query, re-trying after operational errors |
| 367 | if self.autocommit: |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 368 | return self.run_with_retry(_exec_sql) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 369 | else: |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 370 | return _exec_sql() |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 371 | |
mbligh | 608c325 | 2007-08-31 13:53:00 +0000 | [diff] [blame] | 372 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 373 | def _exec_sql_with_commit(self, sql, values, commit): |
| 374 | if self.autocommit: |
| 375 | # re-run the query until it succeeds |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 376 | def _exec_sql(): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 377 | self.cur.execute(sql, values) |
| 378 | self.con.commit() |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 379 | self.run_with_retry(_exec_sql) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 380 | else: |
| 381 | # take one shot at running the query |
| 382 | self.cur.execute(sql, values) |
| 383 | if commit: |
| 384 | self.con.commit() |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 385 | |
mbligh | 2bd4887 | 2007-09-20 18:32:25 +0000 | [diff] [blame] | 386 | |
jadmanski | b591fba | 2008-09-10 16:19:22 +0000 | [diff] [blame] | 387 | def insert(self, table, data, commit=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 388 | """\ |
| 389 | 'insert into table (keys) values (%s ... %s)', values |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 390 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 391 | data: |
| 392 | dictionary of fields and data |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 393 | |
| 394 | @param table: The name of the table. |
| 395 | @param data: The insert data. |
| 396 | @param commit: If commit the transaction . |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 397 | """ |
| 398 | fields = data.keys() |
| 399 | refs = ['%s' for field in fields] |
| 400 | values = [data[field] for field in fields] |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 401 | cmd = ('insert into %s (%s) values (%s)' % |
| 402 | (table, ','.join(self._quote(field) for field in fields), |
| 403 | ','.join(refs))) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 404 | self.dprint('%s %s' % (cmd, values)) |
mbligh | e9cf9d4 | 2007-08-31 08:56:00 +0000 | [diff] [blame] | 405 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 406 | self._exec_sql_with_commit(cmd, values, commit) |
mbligh | e9cf9d4 | 2007-08-31 08:56:00 +0000 | [diff] [blame] | 407 | |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 408 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 409 | def delete(self, table, where, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 410 | """Delete entries. |
| 411 | |
| 412 | @param table: The name of the table. |
| 413 | @param where: The where clause. |
| 414 | @param commit: If commit the transaction . |
| 415 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 416 | cmd = ['delete from', table] |
mbligh | d876f45 | 2008-12-03 15:09:17 +0000 | [diff] [blame] | 417 | if commit is None: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 418 | commit = self.autocommit |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 419 | where_clause, values = self._where_clause(where) |
| 420 | cmd.append(where_clause) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 421 | sql = ' '.join(cmd) |
| 422 | self.dprint('%s %s' % (sql, values)) |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 423 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 424 | self._exec_sql_with_commit(sql, values, commit) |
mbligh | 048e1c9 | 2007-10-07 00:10:33 +0000 | [diff] [blame] | 425 | |
mbligh | 7a41a86 | 2007-11-30 17:44:24 +0000 | [diff] [blame] | 426 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 427 | def update(self, table, data, where, commit = None): |
| 428 | """\ |
| 429 | 'update table set data values (%s ... %s) where ...' |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 430 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 431 | data: |
| 432 | dictionary of fields and data |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 433 | |
| 434 | @param table: The name of the table. |
| 435 | @param data: The sql parameter values. |
| 436 | @param where: The where clause. |
| 437 | @param commit: If commit the transaction . |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 438 | """ |
mbligh | d876f45 | 2008-12-03 15:09:17 +0000 | [diff] [blame] | 439 | if commit is None: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 440 | commit = self.autocommit |
| 441 | cmd = 'update %s ' % table |
| 442 | fields = data.keys() |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 443 | data_refs = [self._quote(field) + '=%s' for field in fields] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 444 | data_values = [data[field] for field in fields] |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 445 | cmd += ' set ' + ', '.join(data_refs) |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 446 | |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 447 | where_clause, where_values = self._where_clause(where) |
| 448 | cmd += where_clause |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 449 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 450 | values = data_values + where_values |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 451 | self.dprint('%s %s' % (cmd, values)) |
mbligh | 2aaeb67 | 2007-10-01 14:54:18 +0000 | [diff] [blame] | 452 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 453 | self._exec_sql_with_commit(cmd, values, commit) |
mbligh | e9cf9d4 | 2007-08-31 08:56:00 +0000 | [diff] [blame] | 454 | |
| 455 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 456 | def delete_job(self, tag, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 457 | """Delete a tko job. |
| 458 | |
| 459 | @param tag: The job tag. |
| 460 | @param commit: If commit the transaction . |
| 461 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 462 | job_idx = self.find_job(tag) |
| 463 | for test_idx in self.find_tests(job_idx): |
| 464 | where = {'test_idx' : test_idx} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 465 | self.delete('tko_iteration_result', where) |
Dennis Jeffrey | 368c54b | 2013-07-24 11:19:03 -0700 | [diff] [blame] | 466 | self.delete('tko_iteration_perf_value', where) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 467 | self.delete('tko_iteration_attributes', where) |
| 468 | self.delete('tko_test_attributes', where) |
| 469 | self.delete('tko_test_labels_tests', {'test_id': test_idx}) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 470 | where = {'job_idx' : job_idx} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 471 | self.delete('tko_tests', where) |
| 472 | self.delete('tko_jobs', where) |
apw | 7a7316b | 2008-02-21 17:42:05 +0000 | [diff] [blame] | 473 | |
apw | 7a7316b | 2008-02-21 17:42:05 +0000 | [diff] [blame] | 474 | |
Prathmesh Prabhu | 30dee86 | 2018-04-18 20:24:20 -0700 | [diff] [blame] | 475 | def insert_job(self, tag, job, commit=None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 476 | """Insert a tko job. |
| 477 | |
| 478 | @param tag: The job tag. |
| 479 | @param job: The job object. |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 480 | @param commit: If commit the transaction . |
| 481 | """ |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 482 | data = self._get_common_job_data(tag, job) |
| 483 | data.update({ |
Prathmesh Prabhu | 30dee86 | 2018-04-18 20:24:20 -0700 | [diff] [blame] | 484 | 'afe_job_id': job.afe_job_id, |
| 485 | 'afe_parent_job_id': job.afe_parent_job_id, |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 486 | }) |
Prathmesh Prabhu | c2a8a6a | 2018-04-19 16:23:32 -0700 | [diff] [blame] | 487 | if job.job_idx is not None: |
| 488 | self.update( |
| 489 | 'tko_jobs', data, {'job_idx': job.job_idx}, commit=commit) |
showard | 0fec8a0 | 2009-12-04 01:19:54 +0000 | [diff] [blame] | 490 | else: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 491 | self.insert('tko_jobs', data, commit=commit) |
Prathmesh Prabhu | c2a8a6a | 2018-04-19 16:23:32 -0700 | [diff] [blame] | 492 | job.job_idx = self.get_last_autonumber_value() |
Shuqian Zhao | 31425d5 | 2016-12-07 09:35:03 -0800 | [diff] [blame] | 493 | |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 494 | |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 495 | def _get_common_job_data(self, tag, job): |
| 496 | """Construct a dictionary with the common data to insert in job/task.""" |
| 497 | return { |
| 498 | 'tag':tag, |
| 499 | 'label': job.label, |
| 500 | 'username': job.user, |
| 501 | 'machine_idx': job.machine_idx, |
| 502 | 'queued_time': job.queued_time, |
| 503 | 'started_time': job.started_time, |
| 504 | 'finished_time': job.finished_time, |
| 505 | 'build': job.build, |
| 506 | 'build_version': job.build_version, |
| 507 | 'board': job.board, |
| 508 | 'suite': job.suite, |
| 509 | } |
| 510 | |
| 511 | |
| 512 | def insert_or_update_task_reference(self, job, reference_type, commit=None): |
Prathmesh Prabhu | f5030d3 | 2018-04-19 16:08:55 -0700 | [diff] [blame] | 513 | """Insert an entry in the tko_task_references table. |
| 514 | |
| 515 | The job should already have been inserted in tko_jobs. |
| 516 | @param job: tko.models.job object. |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 517 | @param reference_type: The type of reference to insert. |
| 518 | One of: {'afe', 'skylab'} |
Prathmesh Prabhu | f5030d3 | 2018-04-19 16:08:55 -0700 | [diff] [blame] | 519 | @param commit: Whether to commit this transaction. |
| 520 | """ |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 521 | assert reference_type in {'afe', 'skylab'} |
| 522 | if reference_type == 'afe': |
| 523 | task_id = job.afe_job_id |
| 524 | parent_task_id = job.afe_parent_job_id |
| 525 | else: |
| 526 | task_id = job.skylab_task_id |
| 527 | parent_task_id = job.skylab_parent_task_id |
Prathmesh Prabhu | f5030d3 | 2018-04-19 16:08:55 -0700 | [diff] [blame] | 528 | data = { |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 529 | 'reference_type': reference_type, |
Prathmesh Prabhu | c2a8a6a | 2018-04-19 16:23:32 -0700 | [diff] [blame] | 530 | 'tko_job_idx': job.job_idx, |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 531 | 'task_id': task_id, |
| 532 | 'parent_task_id': parent_task_id, |
Prathmesh Prabhu | f5030d3 | 2018-04-19 16:08:55 -0700 | [diff] [blame] | 533 | } |
Prathmesh Prabhu | 1790588 | 2018-04-18 22:09:08 -0700 | [diff] [blame] | 534 | |
Prathmesh Prabhu | f5030d3 | 2018-04-19 16:08:55 -0700 | [diff] [blame] | 535 | task_reference_id = self._lookup_task_reference(job) |
| 536 | if task_reference_id is not None: |
| 537 | self.update('tko_task_references', |
| 538 | data, |
| 539 | {'id': task_reference_id}, |
| 540 | commit=commit) |
| 541 | job.task_reference_id = task_reference_id |
| 542 | else: |
| 543 | self.insert('tko_task_references', data, commit=commit) |
| 544 | job.task_reference_id = self.get_last_autonumber_value() |
| 545 | |
| 546 | |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 547 | def update_job_keyvals(self, job, commit=None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 548 | """Updates the job key values. |
| 549 | |
| 550 | @param job: The job object. |
| 551 | @param commit: If commit the transaction . |
| 552 | """ |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 553 | for key, value in job.keyval_dict.iteritems(): |
Prathmesh Prabhu | c2a8a6a | 2018-04-19 16:23:32 -0700 | [diff] [blame] | 554 | where = {'job_id': job.job_idx, 'key': key} |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 555 | data = dict(where, value=value) |
| 556 | exists = self.select('id', 'tko_job_keyvals', where=where) |
| 557 | |
| 558 | if exists: |
| 559 | self.update('tko_job_keyvals', data, where=where, commit=commit) |
| 560 | else: |
| 561 | self.insert('tko_job_keyvals', data, commit=commit) |
| 562 | |
| 563 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 564 | def insert_test(self, job, test, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 565 | """Inserts a job test. |
| 566 | |
| 567 | @param job: The job object. |
| 568 | @param test: The test object. |
| 569 | @param commit: If commit the transaction . |
| 570 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 571 | kver = self.insert_kernel(test.kernel, commit=commit) |
Prathmesh Prabhu | c2a8a6a | 2018-04-19 16:23:32 -0700 | [diff] [blame] | 572 | data = {'job_idx':job.job_idx, 'test':test.testname, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 573 | 'subdir':test.subdir, 'kernel_idx':kver, |
| 574 | 'status':self.status_idx[test.status], |
| 575 | 'reason':test.reason, 'machine_idx':job.machine_idx, |
| 576 | 'started_time': test.started_time, |
| 577 | 'finished_time':test.finished_time} |
jadmanski | 9b6babf | 2009-04-21 17:57:40 +0000 | [diff] [blame] | 578 | is_update = hasattr(test, "test_idx") |
| 579 | if is_update: |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 580 | test_idx = test.test_idx |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 581 | self.update('tko_tests', data, |
| 582 | {'test_idx': test_idx}, commit=commit) |
jadmanski | b591fba | 2008-09-10 16:19:22 +0000 | [diff] [blame] | 583 | where = {'test_idx': test_idx} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 584 | self.delete('tko_iteration_result', where) |
Dennis Jeffrey | 368c54b | 2013-07-24 11:19:03 -0700 | [diff] [blame] | 585 | self.delete('tko_iteration_perf_value', where) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 586 | self.delete('tko_iteration_attributes', where) |
showard | 0fec8a0 | 2009-12-04 01:19:54 +0000 | [diff] [blame] | 587 | where['user_created'] = 0 |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 588 | self.delete('tko_test_attributes', where) |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 589 | else: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 590 | self.insert('tko_tests', data, commit=commit) |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 591 | test_idx = test.test_idx = self.get_last_autonumber_value() |
| 592 | data = {'test_idx': test_idx} |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 593 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 594 | for i in test.iterations: |
| 595 | data['iteration'] = i.index |
| 596 | for key, value in i.attr_keyval.iteritems(): |
| 597 | data['attribute'] = key |
| 598 | data['value'] = value |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 599 | self.insert('tko_iteration_attributes', data, |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 600 | commit=commit) |
| 601 | for key, value in i.perf_keyval.iteritems(): |
| 602 | data['attribute'] = key |
Allen Li | 64692a3 | 2016-11-04 13:35:19 -0700 | [diff] [blame] | 603 | if math.isnan(value) or math.isinf(value): |
| 604 | data['value'] = None |
| 605 | else: |
| 606 | data['value'] = value |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 607 | self.insert('tko_iteration_result', data, |
mbligh | 432bad4 | 2007-10-09 19:56:07 +0000 | [diff] [blame] | 608 | commit=commit) |
mbligh | 056d0d3 | 2006-10-08 22:31:10 +0000 | [diff] [blame] | 609 | |
Dennis Jeffrey | 368c54b | 2013-07-24 11:19:03 -0700 | [diff] [blame] | 610 | data = {'test_idx': test_idx} |
Dennis Jeffrey | 368c54b | 2013-07-24 11:19:03 -0700 | [diff] [blame] | 611 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 612 | for key, value in test.attributes.iteritems(): |
| 613 | data = {'test_idx': test_idx, 'attribute': key, |
| 614 | 'value': value} |
Allen Li | e3051e9 | 2018-07-30 17:40:54 -0700 | [diff] [blame^] | 615 | try: |
| 616 | self.insert('tko_test_attributes', data, commit=commit) |
| 617 | except: |
| 618 | _log_error('Uploading attribute %r' % (data)) |
| 619 | raise |
mbligh | 2bd4887 | 2007-09-20 18:32:25 +0000 | [diff] [blame] | 620 | |
jadmanski | 9b6babf | 2009-04-21 17:57:40 +0000 | [diff] [blame] | 621 | if not is_update: |
| 622 | for label_index in test.labels: |
| 623 | data = {'test_id': test_idx, 'testlabel_id': label_index} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 624 | self.insert('tko_test_labels_tests', data, commit=commit) |
jadmanski | 9b6babf | 2009-04-21 17:57:40 +0000 | [diff] [blame] | 625 | |
mbligh | 056d0d3 | 2006-10-08 22:31:10 +0000 | [diff] [blame] | 626 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 627 | def read_machine_map(self): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 628 | """Reads the machine map.""" |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 629 | if self.machine_group or not self.machine_map: |
| 630 | return |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 631 | for line in open(self.machine_map, 'r').readlines(): |
| 632 | (machine, group) = line.split() |
| 633 | self.machine_group[machine] = group |
mbligh | 96b9a5a | 2007-11-24 19:32:20 +0000 | [diff] [blame] | 634 | |
| 635 | |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 636 | def machine_info_dict(self, job): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 637 | """Reads the machine information of a job. |
| 638 | |
| 639 | @param job: The job object. |
| 640 | |
| 641 | @return: The machine info dictionary. |
| 642 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 643 | hostname = job.machine |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 644 | group = job.machine_group |
| 645 | owner = job.machine_owner |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 646 | |
| 647 | if not group: |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 648 | self.read_machine_map() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 649 | group = self.machine_group.get(hostname, hostname) |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 650 | if group == hostname and owner: |
| 651 | group = owner + '/' + hostname |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 652 | |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 653 | return {'hostname': hostname, 'machine_group': group, 'owner': owner} |
| 654 | |
| 655 | |
Prathmesh Prabhu | ec96d6e | 2018-04-18 18:13:50 -0700 | [diff] [blame] | 656 | def insert_or_update_machine(self, job, commit=None): |
| 657 | """Insert or updates machine information for the given job. |
| 658 | |
| 659 | Also updates the job object with new machine index, if any. |
| 660 | |
| 661 | @param job: tko.models.job object. |
| 662 | @param commit: Whether to commit the database transaction. |
| 663 | """ |
| 664 | job.machine_idx = self._lookup_machine(job.machine) |
| 665 | if not job.machine_idx: |
| 666 | job.machine_idx = self._insert_machine(job, commit=commit) |
| 667 | elif job.machine: |
| 668 | # Only try to update tko_machines record if machine is set. This |
| 669 | # prevents unnecessary db writes for suite jobs. |
| 670 | self._update_machine_information(job, commit=commit) |
| 671 | |
| 672 | |
Prathmesh Prabhu | f5030d3 | 2018-04-19 16:08:55 -0700 | [diff] [blame] | 673 | def _lookup_task_reference(self, job): |
| 674 | """Find the task_reference_id for a given job. Return None if not found. |
| 675 | |
| 676 | @param job: tko.models.job object. |
| 677 | """ |
Prathmesh Prabhu | c2a8a6a | 2018-04-19 16:23:32 -0700 | [diff] [blame] | 678 | if job.job_idx is None: |
Prathmesh Prabhu | f5030d3 | 2018-04-19 16:08:55 -0700 | [diff] [blame] | 679 | return None |
| 680 | rows = self.select( |
Prathmesh Prabhu | c2a8a6a | 2018-04-19 16:23:32 -0700 | [diff] [blame] | 681 | 'id', 'tko_task_references', {'tko_job_idx': job.job_idx}) |
Prathmesh Prabhu | f5030d3 | 2018-04-19 16:08:55 -0700 | [diff] [blame] | 682 | if not rows: |
| 683 | return None |
| 684 | if len(rows) > 1: |
| 685 | raise MySQLTooManyRows('Got %d tko_task_references for tko_job %d' |
Prathmesh Prabhu | c2a8a6a | 2018-04-19 16:23:32 -0700 | [diff] [blame] | 686 | % (len(rows), job.job_idx)) |
Prathmesh Prabhu | f5030d3 | 2018-04-19 16:08:55 -0700 | [diff] [blame] | 687 | return rows[0][0] |
| 688 | |
| 689 | |
Prathmesh Prabhu | 432648c | 2018-04-18 18:10:50 -0700 | [diff] [blame] | 690 | def _insert_machine(self, job, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 691 | """Inserts the job machine. |
| 692 | |
| 693 | @param job: The job object. |
| 694 | @param commit: If commit the transaction . |
| 695 | """ |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 696 | machine_info = self.machine_info_dict(job) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 697 | self.insert('tko_machines', machine_info, commit=commit) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 698 | return self.get_last_autonumber_value() |
| 699 | |
| 700 | |
Prathmesh Prabhu | 432648c | 2018-04-18 18:10:50 -0700 | [diff] [blame] | 701 | def _update_machine_information(self, job, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 702 | """Updates the job machine information. |
| 703 | |
| 704 | @param job: The job object. |
| 705 | @param commit: If commit the transaction . |
| 706 | """ |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 707 | machine_info = self.machine_info_dict(job) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 708 | self.update('tko_machines', machine_info, |
showard | 71b9431 | 2009-08-20 23:40:02 +0000 | [diff] [blame] | 709 | where={'hostname': machine_info['hostname']}, |
| 710 | commit=commit) |
| 711 | |
| 712 | |
Prathmesh Prabhu | 432648c | 2018-04-18 18:10:50 -0700 | [diff] [blame] | 713 | def _lookup_machine(self, hostname): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 714 | """Look up the machine information. |
| 715 | |
| 716 | @param hostname: The hostname as string. |
| 717 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 718 | where = { 'hostname' : hostname } |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 719 | rows = self.select('machine_idx', 'tko_machines', where) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 720 | if rows: |
| 721 | return rows[0][0] |
| 722 | else: |
| 723 | return None |
| 724 | |
| 725 | |
| 726 | def lookup_kernel(self, kernel): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 727 | """Look up the kernel. |
| 728 | |
| 729 | @param kernel: The kernel object. |
| 730 | """ |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 731 | rows = self.select('kernel_idx', 'tko_kernels', |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 732 | {'kernel_hash':kernel.kernel_hash}) |
| 733 | if rows: |
| 734 | return rows[0][0] |
| 735 | else: |
| 736 | return None |
| 737 | |
| 738 | |
| 739 | def insert_kernel(self, kernel, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 740 | """Insert a kernel. |
| 741 | |
| 742 | @param kernel: The kernel object. |
| 743 | @param commit: If commit the transaction . |
| 744 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 745 | kver = self.lookup_kernel(kernel) |
| 746 | if kver: |
| 747 | return kver |
| 748 | |
| 749 | # If this kernel has any significant patches, append their hash |
| 750 | # as diferentiator. |
| 751 | printable = kernel.base |
| 752 | patch_count = 0 |
| 753 | for patch in kernel.patches: |
| 754 | match = re.match(r'.*(-mm[0-9]+|-git[0-9]+)\.(bz2|gz)$', |
| 755 | patch.reference) |
| 756 | if not match: |
| 757 | patch_count += 1 |
| 758 | |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 759 | self.insert('tko_kernels', |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 760 | {'base':kernel.base, |
| 761 | 'kernel_hash':kernel.kernel_hash, |
| 762 | 'printable':printable}, |
| 763 | commit=commit) |
| 764 | kver = self.get_last_autonumber_value() |
| 765 | |
| 766 | if patch_count > 0: |
| 767 | printable += ' p%d' % (kver) |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 768 | self.update('tko_kernels', |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 769 | {'printable':printable}, |
| 770 | {'kernel_idx':kver}) |
| 771 | |
| 772 | for patch in kernel.patches: |
| 773 | self.insert_patch(kver, patch, commit=commit) |
| 774 | return kver |
| 775 | |
| 776 | |
| 777 | def insert_patch(self, kver, patch, commit = None): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 778 | """Insert a kernel patch. |
| 779 | |
| 780 | @param kver: The kernel version. |
| 781 | @param patch: The kernel patch object. |
| 782 | @param commit: If commit the transaction . |
| 783 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 784 | print patch.reference |
| 785 | name = os.path.basename(patch.reference)[:80] |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 786 | self.insert('tko_patches', |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 787 | {'kernel_idx': kver, |
| 788 | 'name':name, |
| 789 | 'url':patch.reference, |
| 790 | 'hash':patch.hash}, |
| 791 | commit=commit) |
| 792 | |
| 793 | |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 794 | def find_test(self, job_idx, testname, subdir): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 795 | """Find a test by name. |
| 796 | |
| 797 | @param job_idx: The job index. |
| 798 | @param testname: The test name. |
| 799 | @param subdir: The test sub directory under the job directory. |
| 800 | """ |
jadmanski | 74eebf3 | 2008-07-15 20:04:42 +0000 | [diff] [blame] | 801 | where = {'job_idx': job_idx , 'test': testname, 'subdir': subdir} |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 802 | rows = self.select('test_idx', 'tko_tests', where) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 803 | if rows: |
| 804 | return rows[0][0] |
| 805 | else: |
| 806 | return None |
| 807 | |
| 808 | |
| 809 | def find_tests(self, job_idx): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 810 | """Find all tests by job index. |
| 811 | |
| 812 | @param job_idx: The job index. |
| 813 | @return: A list of tests. |
| 814 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 815 | where = { 'job_idx':job_idx } |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 816 | rows = self.select('test_idx', 'tko_tests', where) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 817 | if rows: |
| 818 | return [row[0] for row in rows] |
| 819 | else: |
| 820 | return [] |
| 821 | |
| 822 | |
| 823 | def find_job(self, tag): |
Michael Tang | 5f74ffd | 2016-10-31 10:34:53 -0700 | [diff] [blame] | 824 | """Find a job by tag. |
| 825 | |
| 826 | @param tag: The job tag name. |
| 827 | @return: The job object or None. |
| 828 | """ |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 829 | rows = self.select('job_idx', 'tko_jobs', {'tag': tag}) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 830 | if rows: |
| 831 | return rows[0][0] |
| 832 | else: |
| 833 | return None |
mbligh | af25f06 | 2007-12-03 17:48:35 +0000 | [diff] [blame] | 834 | |
| 835 | |
mbligh | 96cf051 | 2008-04-17 15:25:38 +0000 | [diff] [blame] | 836 | def db(*args, **dargs): |
Laurence Goodby | 0cb6195 | 2018-07-04 16:52:06 +0000 | [diff] [blame] | 837 | """Creates an instance of the database class with the arguments |
| 838 | provided in args and dargs, using the database type specified by |
| 839 | the global configuration (defaulting to mysql). |
| 840 | |
| 841 | @param args: The db_type arguments. |
| 842 | @param dargs: The db_type named arguments. |
| 843 | |
| 844 | @return: An db object. |
| 845 | """ |
Aviv Keshet | 7f4071c | 2018-07-12 14:15:11 -0700 | [diff] [blame] | 846 | return db_sql(*args, **dargs) |