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