mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1 | #!/usr/bin/python -u |
| 2 | |
| 3 | """ |
| 4 | Autotest scheduler |
| 5 | """ |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 6 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 7 | |
showard | ef51921 | 2009-05-08 02:29:53 +0000 | [diff] [blame] | 8 | import datetime, errno, optparse, os, pwd, Queue, re, shutil, signal |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame] | 9 | import smtplib, socket, stat, subprocess, sys, tempfile, time, traceback |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 10 | import itertools, logging, weakref |
mbligh | 70feeee | 2008-06-11 16:20:49 +0000 | [diff] [blame] | 11 | import common |
mbligh | 8bcd23a | 2009-02-03 19:14:06 +0000 | [diff] [blame] | 12 | import MySQLdb |
showard | 043c62a | 2009-06-10 19:48:57 +0000 | [diff] [blame] | 13 | from autotest_lib.scheduler import scheduler_logging_config |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 14 | from autotest_lib.frontend import setup_django_environment |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 15 | from autotest_lib.client.common_lib import global_config, logging_manager |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 16 | from autotest_lib.client.common_lib import host_protections, utils |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 17 | from autotest_lib.database import database_connection |
showard | 844960a | 2009-05-29 18:41:18 +0000 | [diff] [blame] | 18 | from autotest_lib.frontend.afe import models, rpc_utils, readonly_connection |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 19 | from autotest_lib.scheduler import drone_manager, drones, email_manager |
showard | 043c62a | 2009-06-10 19:48:57 +0000 | [diff] [blame] | 20 | from autotest_lib.scheduler import monitor_db_cleanup |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 21 | from autotest_lib.scheduler import status_server, scheduler_config |
mbligh | 70feeee | 2008-06-11 16:20:49 +0000 | [diff] [blame] | 22 | |
mbligh | b090f14 | 2008-02-27 21:33:46 +0000 | [diff] [blame] | 23 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 24 | RESULTS_DIR = '.' |
| 25 | AUTOSERV_NICE_LEVEL = 10 |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 26 | DB_CONFIG_SECTION = 'AUTOTEST_WEB' |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 27 | AUTOTEST_PATH = os.path.join(os.path.dirname(__file__), '..') |
| 28 | |
| 29 | if os.environ.has_key('AUTOTEST_DIR'): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 30 | AUTOTEST_PATH = os.environ['AUTOTEST_DIR'] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 31 | AUTOTEST_SERVER_DIR = os.path.join(AUTOTEST_PATH, 'server') |
| 32 | AUTOTEST_TKO_DIR = os.path.join(AUTOTEST_PATH, 'tko') |
| 33 | |
| 34 | if AUTOTEST_SERVER_DIR not in sys.path: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 35 | sys.path.insert(0, AUTOTEST_SERVER_DIR) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 36 | |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 37 | # how long to wait for autoserv to write a pidfile |
| 38 | PIDFILE_TIMEOUT = 5 * 60 # 5 min |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 39 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 40 | _AUTOSERV_PID_FILE = '.autoserv_execute' |
| 41 | _CRASHINFO_PID_FILE = '.collect_crashinfo_execute' |
| 42 | _PARSER_PID_FILE = '.parser_execute' |
| 43 | |
| 44 | _ALL_PIDFILE_NAMES = (_AUTOSERV_PID_FILE, _CRASHINFO_PID_FILE, |
| 45 | _PARSER_PID_FILE) |
| 46 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 47 | # error message to leave in results dir when an autoserv process disappears |
| 48 | # mysteriously |
| 49 | _LOST_PROCESS_ERROR = """\ |
| 50 | Autoserv failed abnormally during execution for this job, probably due to a |
| 51 | system error on the Autotest server. Full results may not be available. Sorry. |
| 52 | """ |
| 53 | |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 54 | _db = None |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 55 | _shutdown = False |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 56 | _autoserv_path = os.path.join(drones.AUTOTEST_INSTALL_DIR, 'server', 'autoserv') |
| 57 | _parser_path = os.path.join(drones.AUTOTEST_INSTALL_DIR, 'tko', 'parse') |
mbligh | 4314a71 | 2008-02-29 22:44:30 +0000 | [diff] [blame] | 58 | _testing_mode = False |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame] | 59 | _base_url = None |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 60 | _notify_email_statuses = [] |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 61 | _drone_manager = drone_manager.DroneManager() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 62 | |
| 63 | |
mbligh | 83c1e9e | 2009-05-01 23:10:41 +0000 | [diff] [blame] | 64 | def _site_init_monitor_db_dummy(): |
| 65 | return {} |
| 66 | |
| 67 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 68 | def main(): |
showard | 27f3387 | 2009-04-07 18:20:53 +0000 | [diff] [blame] | 69 | try: |
| 70 | main_without_exception_handling() |
showard | 29caa4b | 2009-05-26 19:27:09 +0000 | [diff] [blame] | 71 | except SystemExit: |
| 72 | raise |
showard | 27f3387 | 2009-04-07 18:20:53 +0000 | [diff] [blame] | 73 | except: |
| 74 | logging.exception('Exception escaping in monitor_db') |
| 75 | raise |
| 76 | |
| 77 | |
| 78 | def main_without_exception_handling(): |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 79 | setup_logging() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 80 | |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 81 | usage = 'usage: %prog [options] results_dir' |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 82 | parser = optparse.OptionParser(usage) |
| 83 | parser.add_option('--recover-hosts', help='Try to recover dead hosts', |
| 84 | action='store_true') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 85 | parser.add_option('--test', help='Indicate that scheduler is under ' + |
| 86 | 'test and should use dummy autoserv and no parsing', |
| 87 | action='store_true') |
| 88 | (options, args) = parser.parse_args() |
| 89 | if len(args) != 1: |
| 90 | parser.print_usage() |
| 91 | return |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 92 | |
showard | 5613c66 | 2009-06-08 23:30:33 +0000 | [diff] [blame] | 93 | scheduler_enabled = global_config.global_config.get_config_value( |
| 94 | scheduler_config.CONFIG_SECTION, 'enable_scheduler', type=bool) |
| 95 | |
| 96 | if not scheduler_enabled: |
| 97 | msg = ("Scheduler not enabled, set enable_scheduler to true in the " |
| 98 | "global_config's SCHEDULER section to enabled it. Exiting.") |
| 99 | print msg |
| 100 | sys.exit(1) |
| 101 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 102 | global RESULTS_DIR |
| 103 | RESULTS_DIR = args[0] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 104 | |
mbligh | 83c1e9e | 2009-05-01 23:10:41 +0000 | [diff] [blame] | 105 | site_init = utils.import_site_function(__file__, |
| 106 | "autotest_lib.scheduler.site_monitor_db", "site_init_monitor_db", |
| 107 | _site_init_monitor_db_dummy) |
| 108 | site_init() |
| 109 | |
showard | cca334f | 2009-03-12 20:38:34 +0000 | [diff] [blame] | 110 | # Change the cwd while running to avoid issues incase we were launched from |
| 111 | # somewhere odd (such as a random NFS home directory of the person running |
| 112 | # sudo to launch us as the appropriate user). |
| 113 | os.chdir(RESULTS_DIR) |
| 114 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 115 | c = global_config.global_config |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 116 | notify_statuses_list = c.get_config_value(scheduler_config.CONFIG_SECTION, |
| 117 | "notify_email_statuses", |
| 118 | default='') |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 119 | global _notify_email_statuses |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 120 | _notify_email_statuses = [status for status in |
| 121 | re.split(r'[\s,;:]', notify_statuses_list.lower()) |
| 122 | if status] |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 123 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 124 | if options.test: |
| 125 | global _autoserv_path |
| 126 | _autoserv_path = 'autoserv_dummy' |
| 127 | global _testing_mode |
| 128 | _testing_mode = True |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 129 | |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 130 | # AUTOTEST_WEB.base_url is still a supported config option as some people |
| 131 | # may wish to override the entire url. |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame] | 132 | global _base_url |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 133 | config_base_url = c.get_config_value(DB_CONFIG_SECTION, 'base_url', |
| 134 | default='') |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 135 | if config_base_url: |
| 136 | _base_url = config_base_url |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame] | 137 | else: |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 138 | # For the common case of everything running on a single server you |
| 139 | # can just set the hostname in a single place in the config file. |
| 140 | server_name = c.get_config_value('SERVER', 'hostname') |
| 141 | if not server_name: |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 142 | logging.critical('[SERVER] hostname missing from the config file.') |
mbligh | 37eceaa | 2008-12-15 22:56:37 +0000 | [diff] [blame] | 143 | sys.exit(1) |
| 144 | _base_url = 'http://%s/afe/' % server_name |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame] | 145 | |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 146 | server = status_server.StatusServer(_drone_manager) |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 147 | server.start() |
| 148 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 149 | try: |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 150 | init() |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 151 | dispatcher = Dispatcher() |
showard | 915958d | 2009-04-22 21:00:58 +0000 | [diff] [blame] | 152 | dispatcher.initialize(recover_hosts=options.recover_hosts) |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 153 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 154 | while not _shutdown: |
| 155 | dispatcher.tick() |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 156 | time.sleep(scheduler_config.config.tick_pause_sec) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 157 | except: |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 158 | email_manager.manager.log_stacktrace( |
| 159 | "Uncaught exception; terminating monitor_db") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 160 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 161 | email_manager.manager.send_queued_emails() |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 162 | server.shutdown() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 163 | _drone_manager.shutdown() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 164 | _db.disconnect() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 165 | |
| 166 | |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 167 | def setup_logging(): |
| 168 | log_dir = os.environ.get('AUTOTEST_SCHEDULER_LOG_DIR', None) |
| 169 | log_name = os.environ.get('AUTOTEST_SCHEDULER_LOG_NAME', None) |
| 170 | logging_manager.configure_logging( |
| 171 | scheduler_logging_config.SchedulerLoggingConfig(), log_dir=log_dir, |
| 172 | logfile_name=log_name) |
| 173 | |
| 174 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 175 | def handle_sigint(signum, frame): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 176 | global _shutdown |
| 177 | _shutdown = True |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 178 | logging.info("Shutdown request received.") |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 179 | |
| 180 | |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 181 | def init(): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 182 | logging.info("%s> dispatcher starting", time.strftime("%X %x")) |
| 183 | logging.info("My PID is %d", os.getpid()) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 184 | |
mbligh | fb67603 | 2009-04-01 18:25:38 +0000 | [diff] [blame] | 185 | utils.write_pid("monitor_db") |
| 186 | |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 187 | if _testing_mode: |
| 188 | global_config.global_config.override_config_value( |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 189 | DB_CONFIG_SECTION, 'database', 'stresstest_autotest_web') |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 190 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 191 | os.environ['PATH'] = AUTOTEST_SERVER_DIR + ':' + os.environ['PATH'] |
| 192 | global _db |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 193 | _db = database_connection.DatabaseConnection(DB_CONFIG_SECTION) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 194 | _db.connect() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 195 | |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 196 | # ensure Django connection is in autocommit |
| 197 | setup_django_environment.enable_autocommit() |
showard | 844960a | 2009-05-29 18:41:18 +0000 | [diff] [blame] | 198 | # bypass the readonly connection |
| 199 | readonly_connection.ReadOnlyConnection.set_globally_disabled(True) |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 200 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 201 | logging.info("Setting signal handler") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 202 | signal.signal(signal.SIGINT, handle_sigint) |
| 203 | |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 204 | drones = global_config.global_config.get_config_value( |
| 205 | scheduler_config.CONFIG_SECTION, 'drones', default='localhost') |
| 206 | drone_list = [hostname.strip() for hostname in drones.split(',')] |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 207 | results_host = global_config.global_config.get_config_value( |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 208 | scheduler_config.CONFIG_SECTION, 'results_host', default='localhost') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 209 | _drone_manager.initialize(RESULTS_DIR, drone_list, results_host) |
| 210 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 211 | logging.info("Connected! Running...") |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 212 | |
| 213 | |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 214 | def _autoserv_command_line(machines, results_dir, extra_args, job=None, |
showard | e9c6936 | 2009-06-30 01:58:03 +0000 | [diff] [blame] | 215 | queue_entry=None, verbose=True): |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 216 | """ |
| 217 | @returns The autoserv command line as a list of executable + parameters. |
| 218 | |
| 219 | @param machines - string - A machine or comma separated list of machines |
| 220 | for the (-m) flag. |
| 221 | @param results_dir - string - Where the results will be written (-r). |
| 222 | @param extra_args - list - Additional arguments to pass to autoserv. |
| 223 | @param job - Job object - If supplied, -u owner and -l name parameters |
| 224 | will be added. |
| 225 | @param queue_entry - A HostQueueEntry object - If supplied and no Job |
| 226 | object was supplied, this will be used to lookup the Job object. |
| 227 | """ |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 228 | autoserv_argv = [_autoserv_path, '-p', '-m', machines, |
| 229 | '-r', _drone_manager.absolute_path(results_dir)] |
| 230 | if job or queue_entry: |
| 231 | if not job: |
| 232 | job = queue_entry.job |
| 233 | autoserv_argv += ['-u', job.owner, '-l', job.name] |
showard | e9c6936 | 2009-06-30 01:58:03 +0000 | [diff] [blame] | 234 | if verbose: |
| 235 | autoserv_argv.append('--verbose') |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 236 | return autoserv_argv + extra_args |
| 237 | |
| 238 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 239 | class SchedulerError(Exception): |
| 240 | """Raised by HostScheduler when an inconsistent state occurs.""" |
| 241 | |
| 242 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 243 | class HostScheduler(object): |
| 244 | def _get_ready_hosts(self): |
| 245 | # avoid any host with a currently active queue entry against it |
| 246 | hosts = Host.fetch( |
| 247 | joins='LEFT JOIN host_queue_entries AS active_hqe ' |
| 248 | 'ON (hosts.id = active_hqe.host_id AND ' |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 249 | 'active_hqe.active)', |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 250 | where="active_hqe.host_id IS NULL " |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 251 | "AND NOT hosts.locked " |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 252 | "AND (hosts.status IS NULL OR hosts.status = 'Ready')") |
| 253 | return dict((host.id, host) for host in hosts) |
| 254 | |
| 255 | |
| 256 | @staticmethod |
| 257 | def _get_sql_id_list(id_list): |
| 258 | return ','.join(str(item_id) for item_id in id_list) |
| 259 | |
| 260 | |
| 261 | @classmethod |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 262 | def _get_many2many_dict(cls, query, id_list, flip=False): |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 263 | if not id_list: |
| 264 | return {} |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 265 | query %= cls._get_sql_id_list(id_list) |
| 266 | rows = _db.execute(query) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 267 | return cls._process_many2many_dict(rows, flip) |
| 268 | |
| 269 | |
| 270 | @staticmethod |
| 271 | def _process_many2many_dict(rows, flip=False): |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 272 | result = {} |
| 273 | for row in rows: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 274 | left_id, right_id = int(row[0]), int(row[1]) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 275 | if flip: |
| 276 | left_id, right_id = right_id, left_id |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 277 | result.setdefault(left_id, set()).add(right_id) |
| 278 | return result |
| 279 | |
| 280 | |
| 281 | @classmethod |
| 282 | def _get_job_acl_groups(cls, job_ids): |
| 283 | query = """ |
showard | d9ac445 | 2009-02-07 02:04:37 +0000 | [diff] [blame] | 284 | SELECT jobs.id, acl_groups_users.aclgroup_id |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 285 | FROM jobs |
| 286 | INNER JOIN users ON users.login = jobs.owner |
| 287 | INNER JOIN acl_groups_users ON acl_groups_users.user_id = users.id |
| 288 | WHERE jobs.id IN (%s) |
| 289 | """ |
| 290 | return cls._get_many2many_dict(query, job_ids) |
| 291 | |
| 292 | |
| 293 | @classmethod |
| 294 | def _get_job_ineligible_hosts(cls, job_ids): |
| 295 | query = """ |
| 296 | SELECT job_id, host_id |
| 297 | FROM ineligible_host_queues |
| 298 | WHERE job_id IN (%s) |
| 299 | """ |
| 300 | return cls._get_many2many_dict(query, job_ids) |
| 301 | |
| 302 | |
| 303 | @classmethod |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 304 | def _get_job_dependencies(cls, job_ids): |
| 305 | query = """ |
| 306 | SELECT job_id, label_id |
| 307 | FROM jobs_dependency_labels |
| 308 | WHERE job_id IN (%s) |
| 309 | """ |
| 310 | return cls._get_many2many_dict(query, job_ids) |
| 311 | |
| 312 | |
| 313 | @classmethod |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 314 | def _get_host_acls(cls, host_ids): |
| 315 | query = """ |
showard | d9ac445 | 2009-02-07 02:04:37 +0000 | [diff] [blame] | 316 | SELECT host_id, aclgroup_id |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 317 | FROM acl_groups_hosts |
| 318 | WHERE host_id IN (%s) |
| 319 | """ |
| 320 | return cls._get_many2many_dict(query, host_ids) |
| 321 | |
| 322 | |
| 323 | @classmethod |
| 324 | def _get_label_hosts(cls, host_ids): |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 325 | if not host_ids: |
| 326 | return {}, {} |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 327 | query = """ |
| 328 | SELECT label_id, host_id |
| 329 | FROM hosts_labels |
| 330 | WHERE host_id IN (%s) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 331 | """ % cls._get_sql_id_list(host_ids) |
| 332 | rows = _db.execute(query) |
| 333 | labels_to_hosts = cls._process_many2many_dict(rows) |
| 334 | hosts_to_labels = cls._process_many2many_dict(rows, flip=True) |
| 335 | return labels_to_hosts, hosts_to_labels |
| 336 | |
| 337 | |
| 338 | @classmethod |
| 339 | def _get_labels(cls): |
| 340 | return dict((label.id, label) for label in Label.fetch()) |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 341 | |
| 342 | |
| 343 | def refresh(self, pending_queue_entries): |
| 344 | self._hosts_available = self._get_ready_hosts() |
| 345 | |
| 346 | relevant_jobs = [queue_entry.job_id |
| 347 | for queue_entry in pending_queue_entries] |
| 348 | self._job_acls = self._get_job_acl_groups(relevant_jobs) |
| 349 | self._ineligible_hosts = self._get_job_ineligible_hosts(relevant_jobs) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 350 | self._job_dependencies = self._get_job_dependencies(relevant_jobs) |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 351 | |
| 352 | host_ids = self._hosts_available.keys() |
| 353 | self._host_acls = self._get_host_acls(host_ids) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 354 | self._label_hosts, self._host_labels = self._get_label_hosts(host_ids) |
| 355 | |
| 356 | self._labels = self._get_labels() |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 357 | |
| 358 | |
| 359 | def _is_acl_accessible(self, host_id, queue_entry): |
| 360 | job_acls = self._job_acls.get(queue_entry.job_id, set()) |
| 361 | host_acls = self._host_acls.get(host_id, set()) |
| 362 | return len(host_acls.intersection(job_acls)) > 0 |
| 363 | |
| 364 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 365 | def _check_job_dependencies(self, job_dependencies, host_labels): |
| 366 | missing = job_dependencies - host_labels |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 367 | return len(missing) == 0 |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 368 | |
| 369 | |
| 370 | def _check_only_if_needed_labels(self, job_dependencies, host_labels, |
| 371 | queue_entry): |
showard | ade14e2 | 2009-01-26 22:38:32 +0000 | [diff] [blame] | 372 | if not queue_entry.meta_host: |
| 373 | # bypass only_if_needed labels when a specific host is selected |
| 374 | return True |
| 375 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 376 | for label_id in host_labels: |
| 377 | label = self._labels[label_id] |
| 378 | if not label.only_if_needed: |
| 379 | # we don't care about non-only_if_needed labels |
| 380 | continue |
| 381 | if queue_entry.meta_host == label_id: |
| 382 | # if the label was requested in a metahost it's OK |
| 383 | continue |
| 384 | if label_id not in job_dependencies: |
| 385 | return False |
| 386 | return True |
| 387 | |
| 388 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 389 | def _check_atomic_group_labels(self, host_labels, queue_entry): |
| 390 | """ |
| 391 | Determine if the given HostQueueEntry's atomic group settings are okay |
| 392 | to schedule on a host with the given labels. |
| 393 | |
| 394 | @param host_labels - A list of label ids that the host has. |
| 395 | @param queue_entry - The HostQueueEntry being considered for the host. |
| 396 | |
| 397 | @returns True if atomic group settings are okay, False otherwise. |
| 398 | """ |
| 399 | return (self._get_host_atomic_group_id(host_labels) == |
| 400 | queue_entry.atomic_group_id) |
| 401 | |
| 402 | |
| 403 | def _get_host_atomic_group_id(self, host_labels): |
| 404 | """ |
| 405 | Return the atomic group label id for a host with the given set of |
| 406 | labels if any, or None otherwise. Raises an exception if more than |
| 407 | one atomic group are found in the set of labels. |
| 408 | |
| 409 | @param host_labels - A list of label ids that the host has. |
| 410 | |
| 411 | @returns The id of the atomic group found on a label in host_labels |
| 412 | or None if no atomic group label is found. |
| 413 | @raises SchedulerError - If more than one atomic group label is found. |
| 414 | """ |
| 415 | atomic_ids = [self._labels[label_id].atomic_group_id |
| 416 | for label_id in host_labels |
| 417 | if self._labels[label_id].atomic_group_id is not None] |
| 418 | if not atomic_ids: |
| 419 | return None |
| 420 | if len(atomic_ids) > 1: |
| 421 | raise SchedulerError('More than one atomic label on host.') |
| 422 | return atomic_ids[0] |
| 423 | |
| 424 | |
| 425 | def _get_atomic_group_labels(self, atomic_group_id): |
| 426 | """ |
| 427 | Lookup the label ids that an atomic_group is associated with. |
| 428 | |
| 429 | @param atomic_group_id - The id of the AtomicGroup to look up. |
| 430 | |
| 431 | @returns A generator yeilding Label ids for this atomic group. |
| 432 | """ |
| 433 | return (id for id, label in self._labels.iteritems() |
| 434 | if label.atomic_group_id == atomic_group_id |
| 435 | and not label.invalid) |
| 436 | |
| 437 | |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 438 | def _get_eligible_host_ids_in_group(self, group_hosts, queue_entry): |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 439 | """ |
| 440 | @param group_hosts - A sequence of Host ids to test for usability |
| 441 | and eligibility against the Job associated with queue_entry. |
| 442 | @param queue_entry - The HostQueueEntry that these hosts are being |
| 443 | tested for eligibility against. |
| 444 | |
| 445 | @returns A subset of group_hosts Host ids that are eligible for the |
| 446 | supplied queue_entry. |
| 447 | """ |
| 448 | return set(host_id for host_id in group_hosts |
| 449 | if self._is_host_usable(host_id) |
| 450 | and self._is_host_eligible_for_job(host_id, queue_entry)) |
| 451 | |
| 452 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 453 | def _is_host_eligible_for_job(self, host_id, queue_entry): |
showard | 2924b0a | 2009-06-18 23:16:15 +0000 | [diff] [blame] | 454 | if self._is_host_invalid(host_id): |
| 455 | # if an invalid host is scheduled for a job, it's a one-time host |
| 456 | # and it therefore bypasses eligibility checks. note this can only |
| 457 | # happen for non-metahosts, because invalid hosts have their label |
| 458 | # relationships cleared. |
| 459 | return True |
| 460 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 461 | job_dependencies = self._job_dependencies.get(queue_entry.job_id, set()) |
| 462 | host_labels = self._host_labels.get(host_id, set()) |
mbligh | c993bee | 2008-10-03 03:42:34 +0000 | [diff] [blame] | 463 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 464 | return (self._is_acl_accessible(host_id, queue_entry) and |
| 465 | self._check_job_dependencies(job_dependencies, host_labels) and |
| 466 | self._check_only_if_needed_labels( |
| 467 | job_dependencies, host_labels, queue_entry) and |
| 468 | self._check_atomic_group_labels(host_labels, queue_entry)) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 469 | |
| 470 | |
showard | 2924b0a | 2009-06-18 23:16:15 +0000 | [diff] [blame] | 471 | def _is_host_invalid(self, host_id): |
| 472 | host_object = self._hosts_available.get(host_id, None) |
| 473 | return host_object and host_object.invalid |
| 474 | |
| 475 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 476 | def _schedule_non_metahost(self, queue_entry): |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 477 | if not self._is_host_eligible_for_job(queue_entry.host_id, queue_entry): |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 478 | return None |
| 479 | return self._hosts_available.pop(queue_entry.host_id, None) |
| 480 | |
| 481 | |
| 482 | def _is_host_usable(self, host_id): |
| 483 | if host_id not in self._hosts_available: |
| 484 | # host was already used during this scheduling cycle |
| 485 | return False |
| 486 | if self._hosts_available[host_id].invalid: |
| 487 | # Invalid hosts cannot be used for metahosts. They're included in |
| 488 | # the original query because they can be used by non-metahosts. |
| 489 | return False |
| 490 | return True |
| 491 | |
| 492 | |
| 493 | def _schedule_metahost(self, queue_entry): |
| 494 | label_id = queue_entry.meta_host |
| 495 | hosts_in_label = self._label_hosts.get(label_id, set()) |
| 496 | ineligible_host_ids = self._ineligible_hosts.get(queue_entry.job_id, |
| 497 | set()) |
| 498 | |
| 499 | # must iterate over a copy so we can mutate the original while iterating |
| 500 | for host_id in list(hosts_in_label): |
| 501 | if not self._is_host_usable(host_id): |
| 502 | hosts_in_label.remove(host_id) |
| 503 | continue |
| 504 | if host_id in ineligible_host_ids: |
| 505 | continue |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 506 | if not self._is_host_eligible_for_job(host_id, queue_entry): |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 507 | continue |
| 508 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 509 | # Remove the host from our cached internal state before returning |
| 510 | # the host object. |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 511 | hosts_in_label.remove(host_id) |
| 512 | return self._hosts_available.pop(host_id) |
| 513 | return None |
| 514 | |
| 515 | |
| 516 | def find_eligible_host(self, queue_entry): |
| 517 | if not queue_entry.meta_host: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 518 | assert queue_entry.host_id is not None |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 519 | return self._schedule_non_metahost(queue_entry) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 520 | assert queue_entry.atomic_group_id is None |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 521 | return self._schedule_metahost(queue_entry) |
| 522 | |
| 523 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 524 | def find_eligible_atomic_group(self, queue_entry): |
| 525 | """ |
| 526 | Given an atomic group host queue entry, locate an appropriate group |
| 527 | of hosts for the associated job to run on. |
| 528 | |
| 529 | The caller is responsible for creating new HQEs for the additional |
| 530 | hosts returned in order to run the actual job on them. |
| 531 | |
| 532 | @returns A list of Host instances in a ready state to satisfy this |
| 533 | atomic group scheduling. Hosts will all belong to the same |
| 534 | atomic group label as specified by the queue_entry. |
| 535 | An empty list will be returned if no suitable atomic |
| 536 | group could be found. |
| 537 | |
| 538 | TODO(gps): what is responsible for kicking off any attempted repairs on |
| 539 | a group of hosts? not this function, but something needs to. We do |
| 540 | not communicate that reason for returning [] outside of here... |
| 541 | For now, we'll just be unschedulable if enough hosts within one group |
| 542 | enter Repair Failed state. |
| 543 | """ |
| 544 | assert queue_entry.atomic_group_id is not None |
| 545 | job = queue_entry.job |
| 546 | assert job.synch_count and job.synch_count > 0 |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 547 | atomic_group = queue_entry.atomic_group |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 548 | if job.synch_count > atomic_group.max_number_of_machines: |
| 549 | # Such a Job and HostQueueEntry should never be possible to |
| 550 | # create using the frontend. Regardless, we can't process it. |
| 551 | # Abort it immediately and log an error on the scheduler. |
| 552 | queue_entry.set_status(models.HostQueueEntry.Status.ABORTED) |
showard | 7629f14 | 2009-03-27 21:02:02 +0000 | [diff] [blame] | 553 | logging.error( |
| 554 | 'Error: job %d synch_count=%d > requested atomic_group %d ' |
| 555 | 'max_number_of_machines=%d. Aborted host_queue_entry %d.', |
| 556 | job.id, job.synch_count, atomic_group.id, |
| 557 | atomic_group.max_number_of_machines, queue_entry.id) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 558 | return [] |
| 559 | hosts_in_label = self._label_hosts.get(queue_entry.meta_host, set()) |
| 560 | ineligible_host_ids = self._ineligible_hosts.get(queue_entry.job_id, |
| 561 | set()) |
| 562 | |
| 563 | # Look in each label associated with atomic_group until we find one with |
| 564 | # enough hosts to satisfy the job. |
| 565 | for atomic_label_id in self._get_atomic_group_labels(atomic_group.id): |
| 566 | group_hosts = set(self._label_hosts.get(atomic_label_id, set())) |
| 567 | if queue_entry.meta_host is not None: |
| 568 | # If we have a metahost label, only allow its hosts. |
| 569 | group_hosts.intersection_update(hosts_in_label) |
| 570 | group_hosts -= ineligible_host_ids |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 571 | eligible_host_ids_in_group = self._get_eligible_host_ids_in_group( |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 572 | group_hosts, queue_entry) |
| 573 | |
| 574 | # Job.synch_count is treated as "minimum synch count" when |
| 575 | # scheduling for an atomic group of hosts. The atomic group |
| 576 | # number of machines is the maximum to pick out of a single |
| 577 | # atomic group label for scheduling at one time. |
| 578 | min_hosts = job.synch_count |
| 579 | max_hosts = atomic_group.max_number_of_machines |
| 580 | |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 581 | if len(eligible_host_ids_in_group) < min_hosts: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 582 | # Not enough eligible hosts in this atomic group label. |
| 583 | continue |
| 584 | |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 585 | eligible_hosts_in_group = [self._hosts_available[id] |
| 586 | for id in eligible_host_ids_in_group] |
showard | ef51921 | 2009-05-08 02:29:53 +0000 | [diff] [blame] | 587 | # So that they show up in a sane order when viewing the job. |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 588 | eligible_hosts_in_group.sort(cmp=Host.cmp_for_sort) |
showard | ef51921 | 2009-05-08 02:29:53 +0000 | [diff] [blame] | 589 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 590 | # Limit ourselves to scheduling the atomic group size. |
| 591 | if len(eligible_hosts_in_group) > max_hosts: |
showard | ef51921 | 2009-05-08 02:29:53 +0000 | [diff] [blame] | 592 | eligible_hosts_in_group = eligible_hosts_in_group[:max_hosts] |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 593 | |
| 594 | # Remove the selected hosts from our cached internal state |
| 595 | # of available hosts in order to return the Host objects. |
| 596 | host_list = [] |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 597 | for host in eligible_hosts_in_group: |
| 598 | hosts_in_label.discard(host.id) |
| 599 | self._hosts_available.pop(host.id) |
| 600 | host_list.append(host) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 601 | return host_list |
| 602 | |
| 603 | return [] |
| 604 | |
| 605 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 606 | class Dispatcher(object): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 607 | def __init__(self): |
| 608 | self._agents = [] |
showard | 3bb499f | 2008-07-03 19:42:20 +0000 | [diff] [blame] | 609 | self._last_clean_time = time.time() |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 610 | self._host_scheduler = HostScheduler() |
mbligh | f3294cc | 2009-04-08 21:17:38 +0000 | [diff] [blame] | 611 | user_cleanup_time = scheduler_config.config.clean_interval |
| 612 | self._periodic_cleanup = monitor_db_cleanup.UserCleanup( |
| 613 | _db, user_cleanup_time) |
| 614 | self._24hr_upkeep = monitor_db_cleanup.TwentyFourHourUpkeep(_db) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 615 | self._host_agents = {} |
| 616 | self._queue_entry_agents = {} |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 617 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 618 | |
showard | 915958d | 2009-04-22 21:00:58 +0000 | [diff] [blame] | 619 | def initialize(self, recover_hosts=True): |
| 620 | self._periodic_cleanup.initialize() |
| 621 | self._24hr_upkeep.initialize() |
| 622 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 623 | # always recover processes |
| 624 | self._recover_processes() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 625 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 626 | if recover_hosts: |
| 627 | self._recover_hosts() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 628 | |
| 629 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 630 | def tick(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 631 | _drone_manager.refresh() |
mbligh | f3294cc | 2009-04-08 21:17:38 +0000 | [diff] [blame] | 632 | self._run_cleanup() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 633 | self._find_aborting() |
showard | 1ff7b2e | 2009-05-15 23:17:18 +0000 | [diff] [blame] | 634 | self._find_reverify() |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 635 | self._process_recurring_runs() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 636 | self._schedule_new_jobs() |
| 637 | self._handle_agents() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 638 | _drone_manager.execute_actions() |
| 639 | email_manager.manager.send_queued_emails() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 640 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 641 | |
mbligh | f3294cc | 2009-04-08 21:17:38 +0000 | [diff] [blame] | 642 | def _run_cleanup(self): |
| 643 | self._periodic_cleanup.run_cleanup_maybe() |
| 644 | self._24hr_upkeep.run_cleanup_maybe() |
showard | a3ab0d5 | 2008-11-03 19:03:47 +0000 | [diff] [blame] | 645 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 646 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 647 | def _register_agent_for_ids(self, agent_dict, object_ids, agent): |
| 648 | for object_id in object_ids: |
| 649 | agent_dict.setdefault(object_id, set()).add(agent) |
| 650 | |
| 651 | |
| 652 | def _unregister_agent_for_ids(self, agent_dict, object_ids, agent): |
| 653 | for object_id in object_ids: |
| 654 | assert object_id in agent_dict |
| 655 | agent_dict[object_id].remove(agent) |
| 656 | |
| 657 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 658 | def add_agent(self, agent): |
| 659 | self._agents.append(agent) |
| 660 | agent.dispatcher = self |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 661 | self._register_agent_for_ids(self._host_agents, agent.host_ids, agent) |
| 662 | self._register_agent_for_ids(self._queue_entry_agents, |
| 663 | agent.queue_entry_ids, agent) |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 664 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 665 | |
| 666 | def get_agents_for_entry(self, queue_entry): |
| 667 | """ |
| 668 | Find agents corresponding to the specified queue_entry. |
| 669 | """ |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 670 | return list(self._queue_entry_agents.get(queue_entry.id, set())) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 671 | |
| 672 | |
| 673 | def host_has_agent(self, host): |
| 674 | """ |
| 675 | Determine if there is currently an Agent present using this host. |
| 676 | """ |
| 677 | return bool(self._host_agents.get(host.id, None)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 678 | |
| 679 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 680 | def remove_agent(self, agent): |
| 681 | self._agents.remove(agent) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 682 | self._unregister_agent_for_ids(self._host_agents, agent.host_ids, |
| 683 | agent) |
| 684 | self._unregister_agent_for_ids(self._queue_entry_agents, |
| 685 | agent.queue_entry_ids, agent) |
showard | ec11316 | 2008-05-08 00:52:49 +0000 | [diff] [blame] | 686 | |
| 687 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 688 | def _recover_processes(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 689 | self._register_pidfiles() |
| 690 | _drone_manager.refresh() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 691 | self._recover_all_recoverable_entries() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 692 | self._requeue_other_active_entries() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 693 | self._reverify_remaining_hosts() |
| 694 | # reinitialize drones after killing orphaned processes, since they can |
| 695 | # leave around files when they die |
| 696 | _drone_manager.execute_actions() |
| 697 | _drone_manager.reinitialize_drones() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 698 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 699 | |
| 700 | def _register_pidfiles(self): |
| 701 | # during recovery we may need to read pidfiles for both running and |
| 702 | # parsing entries |
| 703 | queue_entries = HostQueueEntry.fetch( |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 704 | where="status IN ('Running', 'Gathering', 'Parsing')") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 705 | for queue_entry in queue_entries: |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 706 | for pidfile_name in _ALL_PIDFILE_NAMES: |
| 707 | pidfile_id = _drone_manager.get_pidfile_id_from( |
| 708 | queue_entry.execution_tag(), pidfile_name=pidfile_name) |
| 709 | _drone_manager.register_pidfile(pidfile_id) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 710 | |
| 711 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 712 | def _recover_entries_with_status(self, status, orphans, pidfile_name, |
| 713 | recover_entries_fn): |
| 714 | queue_entries = HostQueueEntry.fetch(where="status = '%s'" % status) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 715 | for queue_entry in queue_entries: |
| 716 | if self.get_agents_for_entry(queue_entry): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 717 | # synchronous job we've already recovered |
| 718 | continue |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 719 | queue_entries = queue_entry.job.get_group_entries(queue_entry) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 720 | execution_tag = queue_entry.execution_tag() |
| 721 | run_monitor = PidfileRunMonitor() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 722 | run_monitor.attach_to_existing_process(execution_tag, |
| 723 | pidfile_name=pidfile_name) |
showard | 597bfd3 | 2009-05-08 18:22:50 +0000 | [diff] [blame] | 724 | |
| 725 | log_message = ('Recovering %s entry %s ' % |
| 726 | (status.lower(), |
| 727 | ', '.join(str(entry) for entry in queue_entries))) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 728 | if not run_monitor.has_process(): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 729 | # execution apparently never happened |
showard | 597bfd3 | 2009-05-08 18:22:50 +0000 | [diff] [blame] | 730 | logging.info(log_message + 'without process') |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 731 | recover_entries_fn(queue_entry.job, queue_entries, None) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 732 | continue |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 733 | |
showard | 597bfd3 | 2009-05-08 18:22:50 +0000 | [diff] [blame] | 734 | logging.info(log_message + '(process %s)', |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 735 | run_monitor.get_process()) |
| 736 | recover_entries_fn(queue_entry.job, queue_entries, run_monitor) |
| 737 | orphans.discard(run_monitor.get_process()) |
| 738 | |
| 739 | |
| 740 | def _kill_remaining_orphan_processes(self, orphans): |
| 741 | for process in orphans: |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 742 | logging.info('Killing orphan %s', process) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 743 | _drone_manager.kill_process(process) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 744 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 745 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 746 | def _recover_running_entries(self, orphans): |
| 747 | def recover_entries(job, queue_entries, run_monitor): |
| 748 | if run_monitor is not None: |
| 749 | queue_task = RecoveryQueueTask(job=job, |
| 750 | queue_entries=queue_entries, |
| 751 | run_monitor=run_monitor) |
| 752 | self.add_agent(Agent(tasks=[queue_task], |
| 753 | num_processes=len(queue_entries))) |
| 754 | # else, _requeue_other_active_entries will cover this |
| 755 | |
| 756 | self._recover_entries_with_status(models.HostQueueEntry.Status.RUNNING, |
| 757 | orphans, '.autoserv_execute', |
| 758 | recover_entries) |
| 759 | |
| 760 | |
| 761 | def _recover_gathering_entries(self, orphans): |
| 762 | def recover_entries(job, queue_entries, run_monitor): |
| 763 | gather_task = GatherLogsTask(job, queue_entries, |
| 764 | run_monitor=run_monitor) |
| 765 | self.add_agent(Agent([gather_task])) |
| 766 | |
| 767 | self._recover_entries_with_status( |
| 768 | models.HostQueueEntry.Status.GATHERING, |
| 769 | orphans, _CRASHINFO_PID_FILE, recover_entries) |
| 770 | |
| 771 | |
| 772 | def _recover_parsing_entries(self, orphans): |
| 773 | def recover_entries(job, queue_entries, run_monitor): |
| 774 | reparse_task = FinalReparseTask(queue_entries, |
| 775 | run_monitor=run_monitor) |
| 776 | self.add_agent(Agent([reparse_task], num_processes=0)) |
| 777 | |
| 778 | self._recover_entries_with_status(models.HostQueueEntry.Status.PARSING, |
| 779 | orphans, _PARSER_PID_FILE, |
| 780 | recover_entries) |
| 781 | |
| 782 | |
| 783 | def _recover_all_recoverable_entries(self): |
| 784 | orphans = _drone_manager.get_orphaned_autoserv_processes() |
| 785 | self._recover_running_entries(orphans) |
| 786 | self._recover_gathering_entries(orphans) |
| 787 | self._recover_parsing_entries(orphans) |
| 788 | self._kill_remaining_orphan_processes(orphans) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 789 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 790 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 791 | def _requeue_other_active_entries(self): |
| 792 | queue_entries = HostQueueEntry.fetch( |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 793 | where='active AND NOT complete AND ' |
| 794 | '(aborted OR status != "Pending")') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 795 | for queue_entry in queue_entries: |
| 796 | if self.get_agents_for_entry(queue_entry): |
| 797 | # entry has already been recovered |
| 798 | continue |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 799 | if queue_entry.aborted: |
| 800 | queue_entry.abort(self) |
| 801 | continue |
| 802 | |
| 803 | logging.info('Requeuing active QE %s (status=%s)', queue_entry, |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 804 | queue_entry.status) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 805 | if queue_entry.host: |
| 806 | tasks = queue_entry.host.reverify_tasks() |
| 807 | self.add_agent(Agent(tasks)) |
| 808 | agent = queue_entry.requeue() |
| 809 | |
| 810 | |
showard | 1ff7b2e | 2009-05-15 23:17:18 +0000 | [diff] [blame] | 811 | def _find_reverify(self): |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 812 | tasks = models.SpecialTask.objects.filter( |
| 813 | task=models.SpecialTask.Task.REVERIFY, is_active=False, |
| 814 | is_complete=False) |
| 815 | |
| 816 | host_ids = [str(task.host.id) for task in tasks] |
| 817 | |
| 818 | if host_ids: |
| 819 | where = 'id IN (%s)' % ','.join(host_ids) |
| 820 | host_ids_reverifying = self._reverify_hosts_where( |
| 821 | where, cleanup=False) |
| 822 | tasks = tasks.filter(host__id__in=host_ids_reverifying) |
| 823 | for task in tasks: |
| 824 | task.is_active=True |
| 825 | task.save() |
showard | 1ff7b2e | 2009-05-15 23:17:18 +0000 | [diff] [blame] | 826 | |
| 827 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 828 | def _reverify_remaining_hosts(self): |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 829 | # reverify hosts that were in the middle of verify, repair or cleanup |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 830 | self._reverify_hosts_where("""(status = 'Repairing' OR |
| 831 | status = 'Verifying' OR |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 832 | status = 'Cleaning')""") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 833 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 834 | # recover "Running" hosts with no active queue entries, although this |
| 835 | # should never happen |
| 836 | message = ('Recovering running host %s - this probably indicates a ' |
| 837 | 'scheduler bug') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 838 | self._reverify_hosts_where("""status = 'Running' AND |
| 839 | id NOT IN (SELECT host_id |
| 840 | FROM host_queue_entries |
| 841 | WHERE active)""", |
| 842 | print_message=message) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 843 | |
| 844 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 845 | def _reverify_hosts_where(self, where, |
showard | a64e52a | 2009-06-08 23:24:08 +0000 | [diff] [blame] | 846 | print_message='Reverifying host %s', |
| 847 | cleanup=True): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 848 | full_where='locked = 0 AND invalid = 0 AND ' + where |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 849 | host_ids_reverifying = [] |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 850 | for host in Host.fetch(where=full_where): |
| 851 | if self.host_has_agent(host): |
| 852 | # host has already been recovered in some way |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 853 | continue |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 854 | if print_message: |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 855 | logging.info(print_message, host.hostname) |
showard | a64e52a | 2009-06-08 23:24:08 +0000 | [diff] [blame] | 856 | tasks = host.reverify_tasks(cleanup) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 857 | self.add_agent(Agent(tasks)) |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 858 | host_ids_reverifying.append(host.id) |
| 859 | return host_ids_reverifying |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 860 | |
| 861 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 862 | def _recover_hosts(self): |
| 863 | # recover "Repair Failed" hosts |
| 864 | message = 'Reverifying dead host %s' |
| 865 | self._reverify_hosts_where("status = 'Repair Failed'", |
| 866 | print_message=message) |
mbligh | 62ba2ed | 2008-04-30 17:09:25 +0000 | [diff] [blame] | 867 | |
| 868 | |
showard | 04c82c5 | 2008-05-29 19:38:12 +0000 | [diff] [blame] | 869 | |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 870 | def _get_pending_queue_entries(self): |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 871 | # prioritize by job priority, then non-metahost over metahost, then FIFO |
| 872 | return list(HostQueueEntry.fetch( |
showard | 25cbdbd | 2009-02-17 20:57:21 +0000 | [diff] [blame] | 873 | joins='INNER JOIN jobs ON (job_id=jobs.id)', |
showard | ac9ce22 | 2008-12-03 18:19:44 +0000 | [diff] [blame] | 874 | where='NOT complete AND NOT active AND status="Queued"', |
showard | 25cbdbd | 2009-02-17 20:57:21 +0000 | [diff] [blame] | 875 | order_by='jobs.priority DESC, meta_host, job_id')) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 876 | |
| 877 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 878 | def _refresh_pending_queue_entries(self): |
| 879 | """ |
| 880 | Lookup the pending HostQueueEntries and call our HostScheduler |
| 881 | refresh() method given that list. Return the list. |
| 882 | |
| 883 | @returns A list of pending HostQueueEntries sorted in priority order. |
| 884 | """ |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 885 | queue_entries = self._get_pending_queue_entries() |
| 886 | if not queue_entries: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 887 | return [] |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 888 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 889 | self._host_scheduler.refresh(queue_entries) |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 890 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 891 | return queue_entries |
| 892 | |
| 893 | |
| 894 | def _schedule_atomic_group(self, queue_entry): |
| 895 | """ |
| 896 | Schedule the given queue_entry on an atomic group of hosts. |
| 897 | |
| 898 | Returns immediately if there are insufficient available hosts. |
| 899 | |
| 900 | Creates new HostQueueEntries based off of queue_entry for the |
| 901 | scheduled hosts and starts them all running. |
| 902 | """ |
| 903 | # This is a virtual host queue entry representing an entire |
| 904 | # atomic group, find a group and schedule their hosts. |
| 905 | group_hosts = self._host_scheduler.find_eligible_atomic_group( |
| 906 | queue_entry) |
| 907 | if not group_hosts: |
| 908 | return |
showard | cbe6f94 | 2009-06-17 19:33:49 +0000 | [diff] [blame] | 909 | |
| 910 | logging.info('Expanding atomic group entry %s with hosts %s', |
| 911 | queue_entry, |
| 912 | ', '.join(host.hostname for host in group_hosts)) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 913 | # The first assigned host uses the original HostQueueEntry |
| 914 | group_queue_entries = [queue_entry] |
| 915 | for assigned_host in group_hosts[1:]: |
| 916 | # Create a new HQE for every additional assigned_host. |
| 917 | new_hqe = HostQueueEntry.clone(queue_entry) |
| 918 | new_hqe.save() |
| 919 | group_queue_entries.append(new_hqe) |
| 920 | assert len(group_queue_entries) == len(group_hosts) |
| 921 | for queue_entry, host in itertools.izip(group_queue_entries, |
| 922 | group_hosts): |
| 923 | self._run_queue_entry(queue_entry, host) |
| 924 | |
| 925 | |
| 926 | def _schedule_new_jobs(self): |
| 927 | queue_entries = self._refresh_pending_queue_entries() |
| 928 | if not queue_entries: |
| 929 | return |
| 930 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 931 | for queue_entry in queue_entries: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 932 | if (queue_entry.atomic_group_id is None or |
| 933 | queue_entry.host_id is not None): |
| 934 | assigned_host = self._host_scheduler.find_eligible_host( |
| 935 | queue_entry) |
| 936 | if assigned_host: |
| 937 | self._run_queue_entry(queue_entry, assigned_host) |
| 938 | else: |
| 939 | self._schedule_atomic_group(queue_entry) |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 940 | |
| 941 | |
| 942 | def _run_queue_entry(self, queue_entry, host): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 943 | agent = queue_entry.run_pre_job_tasks(assigned_host=host) |
| 944 | self.add_agent(agent) |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 945 | |
| 946 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 947 | def _find_aborting(self): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 948 | for entry in HostQueueEntry.fetch(where='aborted and not complete'): |
| 949 | for agent in self.get_agents_for_entry(entry): |
| 950 | agent.abort() |
| 951 | entry.abort(self) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 952 | |
| 953 | |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 954 | def _can_start_agent(self, agent, num_started_this_cycle, |
| 955 | have_reached_limit): |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 956 | # always allow zero-process agents to run |
| 957 | if agent.num_processes == 0: |
| 958 | return True |
| 959 | # don't allow any nonzero-process agents to run after we've reached a |
| 960 | # limit (this avoids starvation of many-process agents) |
| 961 | if have_reached_limit: |
| 962 | return False |
| 963 | # total process throttling |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 964 | if agent.num_processes > _drone_manager.max_runnable_processes(): |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 965 | return False |
| 966 | # if a single agent exceeds the per-cycle throttling, still allow it to |
| 967 | # run when it's the first agent in the cycle |
| 968 | if num_started_this_cycle == 0: |
| 969 | return True |
| 970 | # per-cycle throttling |
| 971 | if (num_started_this_cycle + agent.num_processes > |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 972 | scheduler_config.config.max_processes_started_per_cycle): |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 973 | return False |
| 974 | return True |
| 975 | |
| 976 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 977 | def _handle_agents(self): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 978 | num_started_this_cycle = 0 |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 979 | have_reached_limit = False |
| 980 | # iterate over copy, so we can remove agents during iteration |
| 981 | for agent in list(self._agents): |
| 982 | if agent.is_done(): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 983 | logging.info("agent finished") |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 984 | self.remove_agent(agent) |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 985 | continue |
| 986 | if not agent.is_running(): |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 987 | if not self._can_start_agent(agent, num_started_this_cycle, |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 988 | have_reached_limit): |
| 989 | have_reached_limit = True |
| 990 | continue |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 991 | num_started_this_cycle += agent.num_processes |
| 992 | agent.tick() |
showard | a9435c0 | 2009-05-13 21:28:17 +0000 | [diff] [blame] | 993 | logging.info('%d running processes', |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 994 | _drone_manager.total_running_processes()) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 995 | |
| 996 | |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 997 | def _process_recurring_runs(self): |
| 998 | recurring_runs = models.RecurringRun.objects.filter( |
| 999 | start_date__lte=datetime.datetime.now()) |
| 1000 | for rrun in recurring_runs: |
| 1001 | # Create job from template |
| 1002 | job = rrun.job |
| 1003 | info = rpc_utils.get_job_info(job) |
showard | a9435c0 | 2009-05-13 21:28:17 +0000 | [diff] [blame] | 1004 | options = job.get_object_dict() |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1005 | |
| 1006 | host_objects = info['hosts'] |
| 1007 | one_time_hosts = info['one_time_hosts'] |
| 1008 | metahost_objects = info['meta_hosts'] |
| 1009 | dependencies = info['dependencies'] |
| 1010 | atomic_group = info['atomic_group'] |
| 1011 | |
| 1012 | for host in one_time_hosts or []: |
| 1013 | this_host = models.Host.create_one_time_host(host.hostname) |
| 1014 | host_objects.append(this_host) |
| 1015 | |
| 1016 | try: |
| 1017 | rpc_utils.create_new_job(owner=rrun.owner.login, |
showard | a9435c0 | 2009-05-13 21:28:17 +0000 | [diff] [blame] | 1018 | options=options, |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1019 | host_objects=host_objects, |
| 1020 | metahost_objects=metahost_objects, |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1021 | atomic_group=atomic_group) |
| 1022 | |
| 1023 | except Exception, ex: |
| 1024 | logging.exception(ex) |
| 1025 | #TODO send email |
| 1026 | |
| 1027 | if rrun.loop_count == 1: |
| 1028 | rrun.delete() |
| 1029 | else: |
| 1030 | if rrun.loop_count != 0: # if not infinite loop |
| 1031 | # calculate new start_date |
| 1032 | difference = datetime.timedelta(seconds=rrun.loop_period) |
| 1033 | rrun.start_date = rrun.start_date + difference |
| 1034 | rrun.loop_count -= 1 |
| 1035 | rrun.save() |
| 1036 | |
| 1037 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1038 | class PidfileRunMonitor(object): |
| 1039 | """ |
| 1040 | Client must call either run() to start a new process or |
| 1041 | attach_to_existing_process(). |
| 1042 | """ |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1043 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1044 | class _PidfileException(Exception): |
| 1045 | """ |
| 1046 | Raised when there's some unexpected behavior with the pid file, but only |
| 1047 | used internally (never allowed to escape this class). |
| 1048 | """ |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1049 | |
| 1050 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1051 | def __init__(self): |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1052 | self.lost_process = False |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1053 | self._start_time = None |
| 1054 | self.pidfile_id = None |
| 1055 | self._state = drone_manager.PidfileContents() |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1056 | |
| 1057 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1058 | def _add_nice_command(self, command, nice_level): |
| 1059 | if not nice_level: |
| 1060 | return command |
| 1061 | return ['nice', '-n', str(nice_level)] + command |
| 1062 | |
| 1063 | |
| 1064 | def _set_start_time(self): |
| 1065 | self._start_time = time.time() |
| 1066 | |
| 1067 | |
| 1068 | def run(self, command, working_directory, nice_level=None, log_file=None, |
| 1069 | pidfile_name=None, paired_with_pidfile=None): |
| 1070 | assert command is not None |
| 1071 | if nice_level is not None: |
| 1072 | command = ['nice', '-n', str(nice_level)] + command |
| 1073 | self._set_start_time() |
| 1074 | self.pidfile_id = _drone_manager.execute_command( |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1075 | command, working_directory, pidfile_name=pidfile_name, |
| 1076 | log_file=log_file, paired_with_pidfile=paired_with_pidfile) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1077 | |
| 1078 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1079 | def attach_to_existing_process(self, execution_tag, |
| 1080 | pidfile_name=_AUTOSERV_PID_FILE): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1081 | self._set_start_time() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1082 | self.pidfile_id = _drone_manager.get_pidfile_id_from( |
| 1083 | execution_tag, pidfile_name=pidfile_name) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1084 | _drone_manager.register_pidfile(self.pidfile_id) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1085 | |
| 1086 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1087 | def kill(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1088 | if self.has_process(): |
| 1089 | _drone_manager.kill_process(self.get_process()) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1090 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1091 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1092 | def has_process(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1093 | self._get_pidfile_info() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1094 | return self._state.process is not None |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1095 | |
| 1096 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1097 | def get_process(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1098 | self._get_pidfile_info() |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1099 | assert self._state.process is not None |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1100 | return self._state.process |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1101 | |
| 1102 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1103 | def _read_pidfile(self, use_second_read=False): |
| 1104 | assert self.pidfile_id is not None, ( |
| 1105 | 'You must call run() or attach_to_existing_process()') |
| 1106 | contents = _drone_manager.get_pidfile_contents( |
| 1107 | self.pidfile_id, use_second_read=use_second_read) |
| 1108 | if contents.is_invalid(): |
| 1109 | self._state = drone_manager.PidfileContents() |
| 1110 | raise self._PidfileException(contents) |
| 1111 | self._state = contents |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1112 | |
| 1113 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1114 | def _handle_pidfile_error(self, error, message=''): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1115 | message = error + '\nProcess: %s\nPidfile: %s\n%s' % ( |
| 1116 | self._state.process, self.pidfile_id, message) |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 1117 | logging.info(message) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1118 | email_manager.manager.enqueue_notify_email(error, message) |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1119 | self.on_lost_process(self._state.process) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1120 | |
| 1121 | |
| 1122 | def _get_pidfile_info_helper(self): |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1123 | if self.lost_process: |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1124 | return |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1125 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1126 | self._read_pidfile() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1127 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1128 | if self._state.process is None: |
| 1129 | self._handle_no_process() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1130 | return |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1131 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1132 | if self._state.exit_status is None: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1133 | # double check whether or not autoserv is running |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1134 | if _drone_manager.is_process_running(self._state.process): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1135 | return |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1136 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1137 | # pid but no running process - maybe process *just* exited |
| 1138 | self._read_pidfile(use_second_read=True) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1139 | if self._state.exit_status is None: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1140 | # autoserv exited without writing an exit code |
| 1141 | # to the pidfile |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1142 | self._handle_pidfile_error( |
| 1143 | 'autoserv died without writing exit code') |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1144 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1145 | |
| 1146 | def _get_pidfile_info(self): |
| 1147 | """\ |
| 1148 | After completion, self._state will contain: |
| 1149 | pid=None, exit_status=None if autoserv has not yet run |
| 1150 | pid!=None, exit_status=None if autoserv is running |
| 1151 | pid!=None, exit_status!=None if autoserv has completed |
| 1152 | """ |
| 1153 | try: |
| 1154 | self._get_pidfile_info_helper() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1155 | except self._PidfileException, exc: |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1156 | self._handle_pidfile_error('Pidfile error', traceback.format_exc()) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1157 | |
| 1158 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1159 | def _handle_no_process(self): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1160 | """\ |
| 1161 | Called when no pidfile is found or no pid is in the pidfile. |
| 1162 | """ |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1163 | message = 'No pid found at %s' % self.pidfile_id |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 1164 | logging.info(message) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1165 | if time.time() - self._start_time > PIDFILE_TIMEOUT: |
| 1166 | email_manager.manager.enqueue_notify_email( |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1167 | 'Process has failed to write pidfile', message) |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1168 | self.on_lost_process() |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1169 | |
| 1170 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1171 | def on_lost_process(self, process=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1172 | """\ |
| 1173 | Called when autoserv has exited without writing an exit status, |
| 1174 | or we've timed out waiting for autoserv to write a pid to the |
| 1175 | pidfile. In either case, we just return failure and the caller |
| 1176 | should signal some kind of warning. |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1177 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1178 | process is unimportant here, as it shouldn't be used by anyone. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1179 | """ |
| 1180 | self.lost_process = True |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1181 | self._state.process = process |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1182 | self._state.exit_status = 1 |
| 1183 | self._state.num_tests_failed = 0 |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1184 | |
| 1185 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1186 | def exit_code(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1187 | self._get_pidfile_info() |
| 1188 | return self._state.exit_status |
| 1189 | |
| 1190 | |
| 1191 | def num_tests_failed(self): |
| 1192 | self._get_pidfile_info() |
| 1193 | assert self._state.num_tests_failed is not None |
| 1194 | return self._state.num_tests_failed |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1195 | |
| 1196 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1197 | class Agent(object): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1198 | """ |
| 1199 | An agent for use by the Dispatcher class to perform a sequence of tasks. |
| 1200 | |
| 1201 | The following methods are required on all task objects: |
| 1202 | poll() - Called periodically to let the task check its status and |
| 1203 | update its internal state. If the task succeeded. |
| 1204 | is_done() - Returns True if the task is finished. |
| 1205 | abort() - Called when an abort has been requested. The task must |
| 1206 | set its aborted attribute to True if it actually aborted. |
| 1207 | |
| 1208 | The following attributes are required on all task objects: |
| 1209 | aborted - bool, True if this task was aborted. |
| 1210 | failure_tasks - A sequence of tasks to be run using a new Agent |
| 1211 | by the dispatcher should this task fail. |
| 1212 | success - bool, True if this task succeeded. |
| 1213 | queue_entry_ids - A sequence of HostQueueEntry ids this task handles. |
| 1214 | host_ids - A sequence of Host ids this task represents. |
| 1215 | |
| 1216 | The following attribute is written to all task objects: |
| 1217 | agent - A reference to the Agent instance that the task has been |
| 1218 | added to. |
| 1219 | """ |
| 1220 | |
| 1221 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1222 | def __init__(self, tasks, num_processes=1): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1223 | """ |
| 1224 | @param tasks: A list of tasks as described in the class docstring. |
| 1225 | @param num_processes: The number of subprocesses the Agent represents. |
| 1226 | This is used by the Dispatcher for managing the load on the |
| 1227 | system. Defaults to 1. |
| 1228 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1229 | self.active_task = None |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1230 | self.queue = None |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1231 | # This is filled in by Dispatcher.add_agent() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1232 | self.dispatcher = None |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1233 | self.num_processes = num_processes |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1234 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1235 | self.queue_entry_ids = self._union_ids(task.queue_entry_ids |
| 1236 | for task in tasks) |
| 1237 | self.host_ids = self._union_ids(task.host_ids for task in tasks) |
| 1238 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1239 | self._clear_queue() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1240 | for task in tasks: |
| 1241 | self.add_task(task) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1242 | |
| 1243 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1244 | def _clear_queue(self): |
| 1245 | self.queue = Queue.Queue(0) |
| 1246 | |
| 1247 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1248 | def _union_ids(self, id_lists): |
| 1249 | return set(itertools.chain(*id_lists)) |
| 1250 | |
| 1251 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1252 | def add_task(self, task): |
| 1253 | self.queue.put_nowait(task) |
| 1254 | task.agent = self |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1255 | |
| 1256 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1257 | def tick(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1258 | while not self.is_done(): |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1259 | if self.active_task: |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1260 | self.active_task.poll() |
| 1261 | if not self.active_task.is_done(): |
| 1262 | return |
| 1263 | self._next_task() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1264 | |
| 1265 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1266 | def _next_task(self): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 1267 | logging.info("agent picking task") |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1268 | if self.active_task is not None: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1269 | assert self.active_task.is_done() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1270 | if not self.active_task.success: |
| 1271 | self.on_task_failure() |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1272 | self.active_task = None |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1273 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1274 | if not self.is_done(): |
| 1275 | self.active_task = self.queue.get_nowait() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1276 | |
| 1277 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1278 | def on_task_failure(self): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1279 | self._clear_queue() |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 1280 | # run failure tasks in a new Agent, so host_ids and queue_entry_ids will |
| 1281 | # get reset. |
| 1282 | new_agent = Agent(self.active_task.failure_tasks) |
| 1283 | self.dispatcher.add_agent(new_agent) |
mbligh | 16c722d | 2008-03-05 00:58:44 +0000 | [diff] [blame] | 1284 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1285 | |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1286 | def is_running(self): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1287 | return self.active_task is not None |
showard | ec11316 | 2008-05-08 00:52:49 +0000 | [diff] [blame] | 1288 | |
| 1289 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1290 | def is_done(self): |
mbligh | d876f45 | 2008-12-03 15:09:17 +0000 | [diff] [blame] | 1291 | return self.active_task is None and self.queue.empty() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1292 | |
| 1293 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1294 | def abort(self): |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1295 | # abort tasks until the queue is empty or a task ignores the abort |
| 1296 | while not self.is_done(): |
| 1297 | if not self.active_task: |
| 1298 | self._next_task() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1299 | self.active_task.abort() |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1300 | if not self.active_task.aborted: |
| 1301 | # tasks can choose to ignore aborts |
showard | 20f9bdd | 2009-04-29 19:48:33 +0000 | [diff] [blame] | 1302 | return |
| 1303 | self.active_task = None |
| 1304 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1305 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1306 | class DelayedCallTask(object): |
| 1307 | """ |
| 1308 | A task object like AgentTask for an Agent to run that waits for the |
| 1309 | specified amount of time to have elapsed before calling the supplied |
| 1310 | callback once and finishing. If the callback returns anything, it is |
| 1311 | assumed to be a new Agent instance and will be added to the dispatcher. |
| 1312 | |
| 1313 | @attribute end_time: The absolute posix time after which this task will |
| 1314 | call its callback when it is polled and be finished. |
| 1315 | |
| 1316 | Also has all attributes required by the Agent class. |
| 1317 | """ |
| 1318 | def __init__(self, delay_seconds, callback, now_func=None): |
| 1319 | """ |
| 1320 | @param delay_seconds: The delay in seconds from now that this task |
| 1321 | will call the supplied callback and be done. |
| 1322 | @param callback: A callable to be called by this task once after at |
| 1323 | least delay_seconds time has elapsed. It must return None |
| 1324 | or a new Agent instance. |
| 1325 | @param now_func: A time.time like function. Default: time.time. |
| 1326 | Used for testing. |
| 1327 | """ |
| 1328 | assert delay_seconds > 0 |
| 1329 | assert callable(callback) |
| 1330 | if not now_func: |
| 1331 | now_func = time.time |
| 1332 | self._now_func = now_func |
| 1333 | self._callback = callback |
| 1334 | |
| 1335 | self.end_time = self._now_func() + delay_seconds |
| 1336 | |
| 1337 | # These attributes are required by Agent. |
| 1338 | self.aborted = False |
| 1339 | self.failure_tasks = () |
| 1340 | self.host_ids = () |
| 1341 | self.success = False |
| 1342 | self.queue_entry_ids = () |
| 1343 | # This is filled in by Agent.add_task(). |
| 1344 | self.agent = None |
| 1345 | |
| 1346 | |
| 1347 | def poll(self): |
| 1348 | if self._callback and self._now_func() >= self.end_time: |
| 1349 | new_agent = self._callback() |
| 1350 | if new_agent: |
| 1351 | self.agent.dispatcher.add_agent(new_agent) |
| 1352 | self._callback = None |
| 1353 | self.success = True |
| 1354 | |
| 1355 | |
| 1356 | def is_done(self): |
| 1357 | return not self._callback |
| 1358 | |
| 1359 | |
| 1360 | def abort(self): |
| 1361 | self.aborted = True |
| 1362 | self._callback = None |
| 1363 | |
| 1364 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1365 | class AgentTask(object): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1366 | def __init__(self, cmd, working_directory=None, failure_tasks=[], |
| 1367 | pidfile_name=None, paired_with_pidfile=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1368 | self.done = False |
| 1369 | self.failure_tasks = failure_tasks |
| 1370 | self.started = False |
| 1371 | self.cmd = cmd |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1372 | self._working_directory = working_directory |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1373 | self.task = None |
| 1374 | self.agent = None |
| 1375 | self.monitor = None |
| 1376 | self.success = None |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1377 | self.aborted = False |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1378 | self.queue_entry_ids = [] |
| 1379 | self.host_ids = [] |
| 1380 | self.log_file = None |
| 1381 | |
| 1382 | |
| 1383 | def _set_ids(self, host=None, queue_entries=None): |
| 1384 | if queue_entries and queue_entries != [None]: |
| 1385 | self.host_ids = [entry.host.id for entry in queue_entries] |
| 1386 | self.queue_entry_ids = [entry.id for entry in queue_entries] |
| 1387 | else: |
| 1388 | assert host |
| 1389 | self.host_ids = [host.id] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1390 | |
| 1391 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1392 | def poll(self): |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1393 | if not self.started: |
| 1394 | self.start() |
| 1395 | self.tick() |
| 1396 | |
| 1397 | |
| 1398 | def tick(self): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1399 | if self.monitor: |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1400 | exit_code = self.monitor.exit_code() |
| 1401 | if exit_code is None: |
| 1402 | return |
| 1403 | success = (exit_code == 0) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1404 | else: |
| 1405 | success = False |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1406 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1407 | self.finished(success) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1408 | |
| 1409 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1410 | def is_done(self): |
| 1411 | return self.done |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1412 | |
| 1413 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1414 | def finished(self, success): |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1415 | if self.done: |
| 1416 | return |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1417 | self.done = True |
| 1418 | self.success = success |
| 1419 | self.epilog() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1420 | |
| 1421 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1422 | def prolog(self): |
| 1423 | pass |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1424 | |
| 1425 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1426 | def create_temp_resultsdir(self, suffix=''): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1427 | self.temp_results_dir = _drone_manager.get_temporary_path('agent_task') |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1428 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1429 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1430 | def cleanup(self): |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1431 | if self.monitor and self.monitor.has_process() and self.log_file: |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1432 | _drone_manager.copy_to_results_repository( |
| 1433 | self.monitor.get_process(), self.log_file) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1434 | |
| 1435 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1436 | def epilog(self): |
| 1437 | self.cleanup() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1438 | |
| 1439 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1440 | def start(self): |
| 1441 | assert self.agent |
| 1442 | |
| 1443 | if not self.started: |
| 1444 | self.prolog() |
| 1445 | self.run() |
| 1446 | |
| 1447 | self.started = True |
| 1448 | |
| 1449 | |
| 1450 | def abort(self): |
| 1451 | if self.monitor: |
| 1452 | self.monitor.kill() |
| 1453 | self.done = True |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1454 | self.aborted = True |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1455 | self.cleanup() |
| 1456 | |
| 1457 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1458 | def set_host_log_file(self, base_name, host): |
| 1459 | filename = '%s.%s' % (time.time(), base_name) |
| 1460 | self.log_file = os.path.join('hosts', host.hostname, filename) |
| 1461 | |
| 1462 | |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1463 | def _get_consistent_execution_tag(self, queue_entries): |
| 1464 | first_execution_tag = queue_entries[0].execution_tag() |
| 1465 | for queue_entry in queue_entries[1:]: |
| 1466 | assert queue_entry.execution_tag() == first_execution_tag, ( |
| 1467 | '%s (%s) != %s (%s)' % (queue_entry.execution_tag(), |
| 1468 | queue_entry, |
| 1469 | first_execution_tag, |
| 1470 | queue_entries[0])) |
| 1471 | return first_execution_tag |
| 1472 | |
| 1473 | |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1474 | def _copy_results(self, queue_entries, use_monitor=None): |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1475 | assert len(queue_entries) > 0 |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1476 | if use_monitor is None: |
| 1477 | assert self.monitor |
| 1478 | use_monitor = self.monitor |
| 1479 | assert use_monitor.has_process() |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1480 | execution_tag = self._get_consistent_execution_tag(queue_entries) |
showard | 678df4f | 2009-02-04 21:36:39 +0000 | [diff] [blame] | 1481 | results_path = execution_tag + '/' |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1482 | _drone_manager.copy_to_results_repository(use_monitor.get_process(), |
showard | 678df4f | 2009-02-04 21:36:39 +0000 | [diff] [blame] | 1483 | results_path) |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1484 | |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1485 | |
| 1486 | def _parse_results(self, queue_entries): |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1487 | reparse_task = FinalReparseTask(queue_entries) |
| 1488 | self.agent.dispatcher.add_agent(Agent([reparse_task], num_processes=0)) |
| 1489 | |
| 1490 | |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1491 | def _copy_and_parse_results(self, queue_entries, use_monitor=None): |
| 1492 | self._copy_results(queue_entries, use_monitor) |
| 1493 | self._parse_results(queue_entries) |
| 1494 | |
| 1495 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1496 | def run(self, pidfile_name=_AUTOSERV_PID_FILE, paired_with_pidfile=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1497 | if self.cmd: |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1498 | self.monitor = PidfileRunMonitor() |
| 1499 | self.monitor.run(self.cmd, self._working_directory, |
| 1500 | nice_level=AUTOSERV_NICE_LEVEL, |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1501 | log_file=self.log_file, |
| 1502 | pidfile_name=pidfile_name, |
| 1503 | paired_with_pidfile=paired_with_pidfile) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1504 | |
| 1505 | |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1506 | class TaskWithJobKeyvals(object): |
| 1507 | """AgentTask mixin providing functionality to help with job keyval files.""" |
| 1508 | _KEYVAL_FILE = 'keyval' |
| 1509 | def _format_keyval(self, key, value): |
| 1510 | return '%s=%s' % (key, value) |
| 1511 | |
| 1512 | |
| 1513 | def _keyval_path(self): |
| 1514 | """Subclasses must override this""" |
| 1515 | raise NotImplemented |
| 1516 | |
| 1517 | |
| 1518 | def _write_keyval_after_job(self, field, value): |
| 1519 | assert self.monitor |
| 1520 | if not self.monitor.has_process(): |
| 1521 | return |
| 1522 | _drone_manager.write_lines_to_file( |
| 1523 | self._keyval_path(), [self._format_keyval(field, value)], |
| 1524 | paired_with_process=self.monitor.get_process()) |
| 1525 | |
| 1526 | |
| 1527 | def _job_queued_keyval(self, job): |
| 1528 | return 'job_queued', int(time.mktime(job.created_on.timetuple())) |
| 1529 | |
| 1530 | |
| 1531 | def _write_job_finished(self): |
| 1532 | self._write_keyval_after_job("job_finished", int(time.time())) |
| 1533 | |
| 1534 | |
| 1535 | class RepairTask(AgentTask, TaskWithJobKeyvals): |
showard | e788ea6 | 2008-11-17 21:02:47 +0000 | [diff] [blame] | 1536 | def __init__(self, host, queue_entry=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1537 | """\ |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1538 | queue_entry: queue entry to mark failed if this repair fails. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1539 | """ |
jadmanski | fb7cfb1 | 2008-07-09 14:13:21 +0000 | [diff] [blame] | 1540 | protection = host_protections.Protection.get_string(host.protection) |
jadmanski | 542537f | 2008-07-24 14:14:56 +0000 | [diff] [blame] | 1541 | # normalize the protection name |
| 1542 | protection = host_protections.Protection.get_attr_name(protection) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1543 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1544 | self.host = host |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 1545 | self.queue_entry_to_fail = queue_entry |
| 1546 | # *don't* include the queue entry in IDs -- if the queue entry is |
| 1547 | # aborted, we want to leave the repair task running |
| 1548 | self._set_ids(host=host) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1549 | |
| 1550 | self.create_temp_resultsdir('.repair') |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 1551 | cmd = _autoserv_command_line(host.hostname, self.temp_results_dir, |
| 1552 | ['-R', '--host-protection', protection], |
| 1553 | queue_entry=queue_entry) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1554 | super(RepairTask, self).__init__(cmd, self.temp_results_dir) |
| 1555 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1556 | self.set_host_log_file('repair', self.host) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1557 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1558 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1559 | def prolog(self): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 1560 | logging.info("repair_task starting") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1561 | self.host.set_status('Repairing') |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 1562 | if self.queue_entry_to_fail: |
| 1563 | self.queue_entry_to_fail.requeue() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1564 | |
| 1565 | |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1566 | def _keyval_path(self): |
| 1567 | return os.path.join(self.temp_results_dir, self._KEYVAL_FILE) |
| 1568 | |
| 1569 | |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1570 | def _fail_queue_entry(self): |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 1571 | assert self.queue_entry_to_fail |
| 1572 | |
| 1573 | if self.queue_entry_to_fail.meta_host: |
| 1574 | return # don't fail metahost entries, they'll be reassigned |
| 1575 | |
| 1576 | self.queue_entry_to_fail.update_from_database() |
| 1577 | if self.queue_entry_to_fail.status != 'Queued': |
| 1578 | return # entry has been aborted |
| 1579 | |
| 1580 | self.queue_entry_to_fail.set_execution_subdir() |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1581 | queued_key, queued_time = self._job_queued_keyval( |
| 1582 | self.queue_entry_to_fail.job) |
| 1583 | self._write_keyval_after_job(queued_key, queued_time) |
| 1584 | self._write_job_finished() |
showard | 678df4f | 2009-02-04 21:36:39 +0000 | [diff] [blame] | 1585 | # copy results logs into the normal place for job results |
| 1586 | _drone_manager.copy_results_on_drone( |
| 1587 | self.monitor.get_process(), |
| 1588 | source_path=self.temp_results_dir + '/', |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 1589 | destination_path=self.queue_entry_to_fail.execution_tag() + '/') |
showard | 678df4f | 2009-02-04 21:36:39 +0000 | [diff] [blame] | 1590 | |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1591 | self._copy_results([self.queue_entry_to_fail]) |
| 1592 | if self.queue_entry_to_fail.job.parse_failed_repair: |
| 1593 | self._parse_results([self.queue_entry_to_fail]) |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 1594 | self.queue_entry_to_fail.handle_host_failure() |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1595 | |
| 1596 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1597 | def epilog(self): |
| 1598 | super(RepairTask, self).epilog() |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 1599 | |
| 1600 | tasks = models.SpecialTask.objects.filter(host__id=self.host.id, |
| 1601 | is_active=True) |
| 1602 | for task in tasks: |
| 1603 | task.is_complete = True |
| 1604 | task.save() |
| 1605 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1606 | if self.success: |
| 1607 | self.host.set_status('Ready') |
| 1608 | else: |
| 1609 | self.host.set_status('Repair Failed') |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 1610 | if self.queue_entry_to_fail: |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1611 | self._fail_queue_entry() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1612 | |
| 1613 | |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 1614 | class PreJobTask(AgentTask): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1615 | def epilog(self): |
| 1616 | super(PreJobTask, self).epilog() |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 1617 | should_copy_results = (self.queue_entry and not self.success |
| 1618 | and not self.queue_entry.meta_host) |
| 1619 | if should_copy_results: |
| 1620 | self.queue_entry.set_execution_subdir() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1621 | destination = os.path.join(self.queue_entry.execution_tag(), |
| 1622 | os.path.basename(self.log_file)) |
| 1623 | _drone_manager.copy_to_results_repository( |
| 1624 | self.monitor.get_process(), self.log_file, |
| 1625 | destination_path=destination) |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 1626 | |
| 1627 | |
| 1628 | class VerifyTask(PreJobTask): |
showard | 9976ce9 | 2008-10-15 20:28:13 +0000 | [diff] [blame] | 1629 | def __init__(self, queue_entry=None, host=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1630 | assert bool(queue_entry) != bool(host) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1631 | self.host = host or queue_entry.host |
| 1632 | self.queue_entry = queue_entry |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1633 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1634 | self.create_temp_resultsdir('.verify') |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 1635 | cmd = _autoserv_command_line(self.host.hostname, self.temp_results_dir, |
| 1636 | ['-v'], queue_entry=queue_entry) |
showard | e788ea6 | 2008-11-17 21:02:47 +0000 | [diff] [blame] | 1637 | failure_tasks = [RepairTask(self.host, queue_entry=queue_entry)] |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1638 | super(VerifyTask, self).__init__(cmd, self.temp_results_dir, |
| 1639 | failure_tasks=failure_tasks) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1640 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1641 | self.set_host_log_file('verify', self.host) |
| 1642 | self._set_ids(host=host, queue_entries=[queue_entry]) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 1643 | |
| 1644 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1645 | def prolog(self): |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 1646 | super(VerifyTask, self).prolog() |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 1647 | logging.info("starting verify on %s", self.host.hostname) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1648 | if self.queue_entry: |
| 1649 | self.queue_entry.set_status('Verifying') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1650 | self.host.set_status('Verifying') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1651 | |
| 1652 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1653 | def epilog(self): |
| 1654 | super(VerifyTask, self).epilog() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1655 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1656 | if self.success: |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 1657 | tasks = models.SpecialTask.objects.filter(host__id=self.host.id, |
| 1658 | is_active=True) |
| 1659 | for task in tasks: |
| 1660 | task.is_complete=True |
| 1661 | task.save() |
| 1662 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1663 | self.host.set_status('Ready') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1664 | |
| 1665 | |
showard | b562645 | 2009-06-30 01:57:28 +0000 | [diff] [blame] | 1666 | class CleanupHostsMixin(object): |
| 1667 | def _reboot_hosts(self, job, queue_entries, final_success, |
| 1668 | num_tests_failed): |
| 1669 | reboot_after = job.reboot_after |
| 1670 | do_reboot = ( |
| 1671 | # always reboot after aborted jobs |
| 1672 | self._final_status == models.HostQueueEntry.Status.ABORTED |
| 1673 | or reboot_after == models.RebootAfter.ALWAYS |
| 1674 | or (reboot_after == models.RebootAfter.IF_ALL_TESTS_PASSED |
| 1675 | and final_success and num_tests_failed == 0)) |
| 1676 | |
| 1677 | for queue_entry in queue_entries: |
| 1678 | if do_reboot: |
| 1679 | # don't pass the queue entry to the CleanupTask. if the cleanup |
| 1680 | # fails, the job doesn't care -- it's over. |
| 1681 | cleanup_task = CleanupTask(host=queue_entry.host) |
| 1682 | self.agent.dispatcher.add_agent(Agent([cleanup_task])) |
| 1683 | else: |
| 1684 | queue_entry.host.set_status('Ready') |
| 1685 | |
| 1686 | |
| 1687 | class QueueTask(AgentTask, TaskWithJobKeyvals, CleanupHostsMixin): |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1688 | def __init__(self, job, queue_entries, cmd, group_name=''): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1689 | self.job = job |
| 1690 | self.queue_entries = queue_entries |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1691 | self.group_name = group_name |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1692 | super(QueueTask, self).__init__(cmd, self._execution_tag()) |
| 1693 | self._set_ids(queue_entries=queue_entries) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1694 | |
| 1695 | |
showard | 73ec044 | 2009-02-07 02:05:20 +0000 | [diff] [blame] | 1696 | def _keyval_path(self): |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1697 | return os.path.join(self._execution_tag(), self._KEYVAL_FILE) |
showard | 73ec044 | 2009-02-07 02:05:20 +0000 | [diff] [blame] | 1698 | |
| 1699 | |
| 1700 | def _write_keyvals_before_job_helper(self, keyval_dict, keyval_path): |
| 1701 | keyval_contents = '\n'.join(self._format_keyval(key, value) |
| 1702 | for key, value in keyval_dict.iteritems()) |
| 1703 | # always end with a newline to allow additional keyvals to be written |
| 1704 | keyval_contents += '\n' |
| 1705 | _drone_manager.attach_file_to_execution(self._execution_tag(), |
| 1706 | keyval_contents, |
| 1707 | file_path=keyval_path) |
| 1708 | |
| 1709 | |
| 1710 | def _write_keyvals_before_job(self, keyval_dict): |
| 1711 | self._write_keyvals_before_job_helper(keyval_dict, self._keyval_path()) |
| 1712 | |
| 1713 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1714 | def _write_host_keyvals(self, host): |
| 1715 | keyval_path = os.path.join(self._execution_tag(), 'host_keyvals', |
| 1716 | host.hostname) |
| 1717 | platform, all_labels = host.platform_and_labels() |
showard | 73ec044 | 2009-02-07 02:05:20 +0000 | [diff] [blame] | 1718 | keyval_dict = dict(platform=platform, labels=','.join(all_labels)) |
| 1719 | self._write_keyvals_before_job_helper(keyval_dict, keyval_path) |
showard | d8e548a | 2008-09-09 03:04:57 +0000 | [diff] [blame] | 1720 | |
| 1721 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1722 | def _execution_tag(self): |
| 1723 | return self.queue_entries[0].execution_tag() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1724 | |
| 1725 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1726 | def prolog(self): |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1727 | queued_key, queued_time = self._job_queued_keyval(self.job) |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1728 | keyval_dict = {queued_key: queued_time} |
| 1729 | if self.group_name: |
| 1730 | keyval_dict['host_group_name'] = self.group_name |
| 1731 | self._write_keyvals_before_job(keyval_dict) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1732 | for queue_entry in self.queue_entries: |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1733 | self._write_host_keyvals(queue_entry.host) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1734 | queue_entry.set_status('Running') |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 1735 | queue_entry.update_field('started_on', datetime.datetime.now()) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1736 | queue_entry.host.set_status('Running') |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1737 | queue_entry.host.update_field('dirty', 1) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1738 | if self.job.synch_count == 1: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1739 | assert len(self.queue_entries) == 1 |
| 1740 | self.job.write_to_machines_file(self.queue_entries[0]) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1741 | |
| 1742 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1743 | def _write_lost_process_error_file(self): |
| 1744 | error_file_path = os.path.join(self._execution_tag(), 'job_failure') |
| 1745 | _drone_manager.write_lines_to_file(error_file_path, |
| 1746 | [_LOST_PROCESS_ERROR]) |
| 1747 | |
| 1748 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1749 | def _finish_task(self): |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1750 | if not self.monitor: |
| 1751 | return |
| 1752 | |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1753 | self._write_job_finished() |
| 1754 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1755 | # both of these conditionals can be true, iff the process ran, wrote a |
| 1756 | # pid to its pidfile, and then exited without writing an exit code |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1757 | if self.monitor.has_process(): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1758 | gather_task = GatherLogsTask(self.job, self.queue_entries) |
| 1759 | self.agent.dispatcher.add_agent(Agent(tasks=[gather_task])) |
showard | b562645 | 2009-06-30 01:57:28 +0000 | [diff] [blame] | 1760 | else: |
| 1761 | self._reboot_hosts(self.job, self.queue_entries, |
| 1762 | final_success=False, num_tests_failed=0) |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1763 | |
| 1764 | if self.monitor.lost_process: |
| 1765 | self._write_lost_process_error_file() |
| 1766 | for queue_entry in self.queue_entries: |
| 1767 | queue_entry.set_status(models.HostQueueEntry.Status.FAILED) |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 1768 | |
| 1769 | |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 1770 | def _write_status_comment(self, comment): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1771 | _drone_manager.write_lines_to_file( |
| 1772 | os.path.join(self._execution_tag(), 'status.log'), |
| 1773 | ['INFO\t----\t----\t' + comment], |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1774 | paired_with_process=self.monitor.get_process()) |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 1775 | |
| 1776 | |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 1777 | def _log_abort(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1778 | if not self.monitor or not self.monitor.has_process(): |
| 1779 | return |
| 1780 | |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 1781 | # build up sets of all the aborted_by and aborted_on values |
| 1782 | aborted_by, aborted_on = set(), set() |
| 1783 | for queue_entry in self.queue_entries: |
| 1784 | if queue_entry.aborted_by: |
| 1785 | aborted_by.add(queue_entry.aborted_by) |
| 1786 | t = int(time.mktime(queue_entry.aborted_on.timetuple())) |
| 1787 | aborted_on.add(t) |
| 1788 | |
| 1789 | # extract some actual, unique aborted by value and write it out |
| 1790 | assert len(aborted_by) <= 1 |
| 1791 | if len(aborted_by) == 1: |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 1792 | aborted_by_value = aborted_by.pop() |
| 1793 | aborted_on_value = max(aborted_on) |
| 1794 | else: |
| 1795 | aborted_by_value = 'autotest_system' |
| 1796 | aborted_on_value = int(time.time()) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1797 | |
showard | a038235 | 2009-02-11 23:36:43 +0000 | [diff] [blame] | 1798 | self._write_keyval_after_job("aborted_by", aborted_by_value) |
| 1799 | self._write_keyval_after_job("aborted_on", aborted_on_value) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1800 | |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 1801 | aborted_on_string = str(datetime.datetime.fromtimestamp( |
| 1802 | aborted_on_value)) |
| 1803 | self._write_status_comment('Job aborted by %s on %s' % |
| 1804 | (aborted_by_value, aborted_on_string)) |
jadmanski | c2ac77f | 2008-05-16 21:44:04 +0000 | [diff] [blame] | 1805 | |
| 1806 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1807 | def abort(self): |
| 1808 | super(QueueTask, self).abort() |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 1809 | self._log_abort() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1810 | self._finish_task() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1811 | |
| 1812 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1813 | def epilog(self): |
| 1814 | super(QueueTask, self).epilog() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1815 | self._finish_task() |
| 1816 | logging.info("queue_task finished with success=%s", self.success) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1817 | |
| 1818 | |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1819 | class RecoveryQueueTask(QueueTask): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1820 | def __init__(self, job, queue_entries, run_monitor): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1821 | super(RecoveryQueueTask, self).__init__(job, queue_entries, cmd=None) |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 1822 | self.monitor = run_monitor |
| 1823 | self.started = True |
| 1824 | # since we set started=True here, prolog() and run() shouldn't be called |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1825 | |
| 1826 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1827 | def run(self): |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 1828 | raise NotImplemented('This should never be called') |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1829 | |
| 1830 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1831 | def prolog(self): |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 1832 | raise NotImplemented('This should never be called') |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1833 | |
| 1834 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1835 | class PostJobTask(AgentTask): |
| 1836 | def __init__(self, queue_entries, pidfile_name, logfile_name, |
| 1837 | run_monitor=None): |
| 1838 | """ |
| 1839 | If run_monitor != None, we're recovering a running task. |
| 1840 | """ |
| 1841 | self._queue_entries = queue_entries |
| 1842 | self._pidfile_name = pidfile_name |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1843 | |
| 1844 | self._execution_tag = self._get_consistent_execution_tag(queue_entries) |
| 1845 | self._results_dir = _drone_manager.absolute_path(self._execution_tag) |
| 1846 | self._autoserv_monitor = PidfileRunMonitor() |
| 1847 | self._autoserv_monitor.attach_to_existing_process(self._execution_tag) |
| 1848 | self._paired_with_pidfile = self._autoserv_monitor.pidfile_id |
| 1849 | |
| 1850 | if _testing_mode: |
| 1851 | command = 'true' |
| 1852 | else: |
| 1853 | command = self._generate_command(self._results_dir) |
| 1854 | |
| 1855 | super(PostJobTask, self).__init__(cmd=command, |
| 1856 | working_directory=self._execution_tag) |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 1857 | # this must happen *after* the super call |
| 1858 | self.monitor = run_monitor |
| 1859 | if run_monitor: |
| 1860 | self.started = True |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1861 | |
| 1862 | self.log_file = os.path.join(self._execution_tag, logfile_name) |
| 1863 | self._final_status = self._determine_final_status() |
| 1864 | |
| 1865 | |
| 1866 | def _generate_command(self, results_dir): |
| 1867 | raise NotImplementedError('Subclasses must override this') |
| 1868 | |
| 1869 | |
| 1870 | def _job_was_aborted(self): |
| 1871 | was_aborted = None |
| 1872 | for queue_entry in self._queue_entries: |
| 1873 | queue_entry.update_from_database() |
| 1874 | if was_aborted is None: # first queue entry |
| 1875 | was_aborted = bool(queue_entry.aborted) |
| 1876 | elif was_aborted != bool(queue_entry.aborted): # subsequent entries |
| 1877 | email_manager.manager.enqueue_notify_email( |
| 1878 | 'Inconsistent abort state', |
| 1879 | 'Queue entries have inconsistent abort state: ' + |
| 1880 | ', '.join('%s (%s)' % (queue_entry, queue_entry.aborted))) |
| 1881 | # don't crash here, just assume true |
| 1882 | return True |
| 1883 | return was_aborted |
| 1884 | |
| 1885 | |
| 1886 | def _determine_final_status(self): |
| 1887 | if self._job_was_aborted(): |
| 1888 | return models.HostQueueEntry.Status.ABORTED |
| 1889 | |
| 1890 | # we'll use a PidfileRunMonitor to read the autoserv exit status |
| 1891 | if self._autoserv_monitor.exit_code() == 0: |
| 1892 | return models.HostQueueEntry.Status.COMPLETED |
| 1893 | return models.HostQueueEntry.Status.FAILED |
| 1894 | |
| 1895 | |
| 1896 | def run(self): |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 1897 | assert not self.monitor |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1898 | |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 1899 | # make sure we actually have results to work with. |
| 1900 | # this should never happen in normal operation. |
| 1901 | if not self._autoserv_monitor.has_process(): |
| 1902 | email_manager.manager.enqueue_notify_email( |
| 1903 | 'No results in post-job task', |
| 1904 | 'No results in post-job task at %s' % |
| 1905 | self._autoserv_monitor.pidfile_id) |
| 1906 | self.finished(False) |
| 1907 | return |
| 1908 | |
| 1909 | super(PostJobTask, self).run( |
| 1910 | pidfile_name=self._pidfile_name, |
| 1911 | paired_with_pidfile=self._paired_with_pidfile) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1912 | |
| 1913 | |
| 1914 | def _set_all_statuses(self, status): |
| 1915 | for queue_entry in self._queue_entries: |
| 1916 | queue_entry.set_status(status) |
| 1917 | |
| 1918 | |
| 1919 | def abort(self): |
| 1920 | # override AgentTask.abort() to avoid killing the process and ending |
| 1921 | # the task. post-job tasks continue when the job is aborted. |
| 1922 | pass |
| 1923 | |
| 1924 | |
showard | b562645 | 2009-06-30 01:57:28 +0000 | [diff] [blame] | 1925 | class GatherLogsTask(PostJobTask, CleanupHostsMixin): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1926 | """ |
| 1927 | Task responsible for |
| 1928 | * gathering uncollected logs (if Autoserv crashed hard or was killed) |
| 1929 | * copying logs to the results repository |
| 1930 | * spawning CleanupTasks for hosts, if necessary |
| 1931 | * spawning a FinalReparseTask for the job |
| 1932 | """ |
| 1933 | def __init__(self, job, queue_entries, run_monitor=None): |
| 1934 | self._job = job |
| 1935 | super(GatherLogsTask, self).__init__( |
| 1936 | queue_entries, pidfile_name=_CRASHINFO_PID_FILE, |
| 1937 | logfile_name='.collect_crashinfo.log', run_monitor=run_monitor) |
| 1938 | self._set_ids(queue_entries=queue_entries) |
| 1939 | |
| 1940 | |
| 1941 | def _generate_command(self, results_dir): |
| 1942 | host_list = ','.join(queue_entry.host.hostname |
| 1943 | for queue_entry in self._queue_entries) |
| 1944 | return [_autoserv_path , '-p', '--collect-crashinfo', '-m', host_list, |
| 1945 | '-r', results_dir] |
| 1946 | |
| 1947 | |
| 1948 | def prolog(self): |
| 1949 | super(GatherLogsTask, self).prolog() |
| 1950 | self._set_all_statuses(models.HostQueueEntry.Status.GATHERING) |
| 1951 | |
| 1952 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1953 | def epilog(self): |
| 1954 | super(GatherLogsTask, self).epilog() |
showard | ebc0fb7 | 2009-05-13 21:28:07 +0000 | [diff] [blame] | 1955 | if self._autoserv_monitor.has_process(): |
| 1956 | self._copy_and_parse_results(self._queue_entries, |
| 1957 | use_monitor=self._autoserv_monitor) |
showard | b562645 | 2009-06-30 01:57:28 +0000 | [diff] [blame] | 1958 | |
| 1959 | final_success = ( |
| 1960 | self._final_status == models.HostQueueEntry.Status.COMPLETED) |
| 1961 | num_tests_failed = self._autoserv_monitor.num_tests_failed() |
| 1962 | self._reboot_hosts(self._job, self._queue_entries, final_success, |
| 1963 | num_tests_failed) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1964 | |
| 1965 | |
showard | 0bbfc21 | 2009-04-29 21:06:13 +0000 | [diff] [blame] | 1966 | def run(self): |
showard | 597bfd3 | 2009-05-08 18:22:50 +0000 | [diff] [blame] | 1967 | autoserv_exit_code = self._autoserv_monitor.exit_code() |
| 1968 | # only run if Autoserv exited due to some signal. if we have no exit |
| 1969 | # code, assume something bad (and signal-like) happened. |
| 1970 | if autoserv_exit_code is None or os.WIFSIGNALED(autoserv_exit_code): |
showard | 0bbfc21 | 2009-04-29 21:06:13 +0000 | [diff] [blame] | 1971 | super(GatherLogsTask, self).run() |
showard | 597bfd3 | 2009-05-08 18:22:50 +0000 | [diff] [blame] | 1972 | else: |
| 1973 | self.finished(True) |
showard | 0bbfc21 | 2009-04-29 21:06:13 +0000 | [diff] [blame] | 1974 | |
| 1975 | |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 1976 | class CleanupTask(PreJobTask): |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1977 | def __init__(self, host=None, queue_entry=None): |
| 1978 | assert bool(host) ^ bool(queue_entry) |
| 1979 | if queue_entry: |
| 1980 | host = queue_entry.get_host() |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 1981 | self.queue_entry = queue_entry |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1982 | self.host = host |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1983 | |
| 1984 | self.create_temp_resultsdir('.cleanup') |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 1985 | self.cmd = _autoserv_command_line(host.hostname, self.temp_results_dir, |
| 1986 | ['--cleanup'], |
| 1987 | queue_entry=queue_entry) |
showard | e788ea6 | 2008-11-17 21:02:47 +0000 | [diff] [blame] | 1988 | repair_task = RepairTask(host, queue_entry=queue_entry) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1989 | super(CleanupTask, self).__init__(self.cmd, self.temp_results_dir, |
| 1990 | failure_tasks=[repair_task]) |
| 1991 | |
| 1992 | self._set_ids(host=host, queue_entries=[queue_entry]) |
| 1993 | self.set_host_log_file('cleanup', self.host) |
mbligh | 16c722d | 2008-03-05 00:58:44 +0000 | [diff] [blame] | 1994 | |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 1995 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1996 | def prolog(self): |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 1997 | super(CleanupTask, self).prolog() |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 1998 | logging.info("starting cleanup task for host: %s", self.host.hostname) |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 1999 | self.host.set_status("Cleaning") |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 2000 | |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 2001 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2002 | def epilog(self): |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 2003 | super(CleanupTask, self).epilog() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2004 | if self.success: |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 2005 | self.host.set_status('Ready') |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2006 | self.host.update_field('dirty', 0) |
| 2007 | |
| 2008 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2009 | class FinalReparseTask(PostJobTask): |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2010 | _num_running_parses = 0 |
| 2011 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2012 | def __init__(self, queue_entries, run_monitor=None): |
| 2013 | super(FinalReparseTask, self).__init__(queue_entries, |
| 2014 | pidfile_name=_PARSER_PID_FILE, |
| 2015 | logfile_name='.parse.log', |
| 2016 | run_monitor=run_monitor) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2017 | # don't use _set_ids, since we don't want to set the host_ids |
| 2018 | self.queue_entry_ids = [entry.id for entry in queue_entries] |
showard | 5add1c8 | 2009-05-26 19:27:46 +0000 | [diff] [blame] | 2019 | self._parse_started = (run_monitor is not None) |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2020 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2021 | |
| 2022 | @classmethod |
| 2023 | def _increment_running_parses(cls): |
| 2024 | cls._num_running_parses += 1 |
| 2025 | |
| 2026 | |
| 2027 | @classmethod |
| 2028 | def _decrement_running_parses(cls): |
| 2029 | cls._num_running_parses -= 1 |
| 2030 | |
| 2031 | |
| 2032 | @classmethod |
| 2033 | def _can_run_new_parse(cls): |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 2034 | return (cls._num_running_parses < |
| 2035 | scheduler_config.config.max_parse_processes) |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2036 | |
| 2037 | |
| 2038 | def prolog(self): |
| 2039 | super(FinalReparseTask, self).prolog() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2040 | self._set_all_statuses(models.HostQueueEntry.Status.PARSING) |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2041 | |
| 2042 | |
| 2043 | def epilog(self): |
| 2044 | super(FinalReparseTask, self).epilog() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2045 | self._set_all_statuses(self._final_status) |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2046 | |
| 2047 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2048 | def _generate_command(self, results_dir): |
mbligh | 9e93640 | 2009-05-13 20:42:17 +0000 | [diff] [blame] | 2049 | return [_parser_path, '--write-pidfile', '-l', '2', '-r', '-o', '-P', |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2050 | results_dir] |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2051 | |
| 2052 | |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 2053 | def tick(self): |
| 2054 | # override tick to keep trying to start until the parse count goes down |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2055 | # and we can, at which point we revert to default behavior |
| 2056 | if self._parse_started: |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 2057 | super(FinalReparseTask, self).tick() |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2058 | else: |
| 2059 | self._try_starting_parse() |
| 2060 | |
| 2061 | |
| 2062 | def run(self): |
| 2063 | # override run() to not actually run unless we can |
| 2064 | self._try_starting_parse() |
| 2065 | |
| 2066 | |
| 2067 | def _try_starting_parse(self): |
| 2068 | if not self._can_run_new_parse(): |
| 2069 | return |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2070 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2071 | # actually run the parse command |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2072 | super(FinalReparseTask, self).run() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2073 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2074 | self._increment_running_parses() |
| 2075 | self._parse_started = True |
| 2076 | |
| 2077 | |
| 2078 | def finished(self, success): |
| 2079 | super(FinalReparseTask, self).finished(success) |
showard | 678df4f | 2009-02-04 21:36:39 +0000 | [diff] [blame] | 2080 | if self._parse_started: |
| 2081 | self._decrement_running_parses() |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2082 | |
| 2083 | |
showard | c9ae178 | 2009-01-30 01:42:37 +0000 | [diff] [blame] | 2084 | class SetEntryPendingTask(AgentTask): |
| 2085 | def __init__(self, queue_entry): |
| 2086 | super(SetEntryPendingTask, self).__init__(cmd='') |
| 2087 | self._queue_entry = queue_entry |
| 2088 | self._set_ids(queue_entries=[queue_entry]) |
| 2089 | |
| 2090 | |
| 2091 | def run(self): |
| 2092 | agent = self._queue_entry.on_pending() |
| 2093 | if agent: |
| 2094 | self.agent.dispatcher.add_agent(agent) |
| 2095 | self.finished(True) |
| 2096 | |
| 2097 | |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2098 | class DBError(Exception): |
| 2099 | """Raised by the DBObject constructor when its select fails.""" |
| 2100 | |
| 2101 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2102 | class DBObject(object): |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2103 | """A miniature object relational model for the database.""" |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2104 | |
| 2105 | # Subclasses MUST override these: |
| 2106 | _table_name = '' |
| 2107 | _fields = () |
| 2108 | |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2109 | # A mapping from (type, id) to the instance of the object for that |
| 2110 | # particular id. This prevents us from creating new Job() and Host() |
| 2111 | # instances for every HostQueueEntry object that we instantiate as |
| 2112 | # multiple HQEs often share the same Job. |
| 2113 | _instances_by_type_and_id = weakref.WeakValueDictionary() |
| 2114 | _initialized = False |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2115 | |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2116 | |
| 2117 | def __new__(cls, id=None, **kwargs): |
| 2118 | """ |
| 2119 | Look to see if we already have an instance for this particular type |
| 2120 | and id. If so, use it instead of creating a duplicate instance. |
| 2121 | """ |
| 2122 | if id is not None: |
| 2123 | instance = cls._instances_by_type_and_id.get((cls, id)) |
| 2124 | if instance: |
| 2125 | return instance |
| 2126 | return super(DBObject, cls).__new__(cls, id=id, **kwargs) |
| 2127 | |
| 2128 | |
| 2129 | def __init__(self, id=None, row=None, new_record=False, always_query=True): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2130 | assert (bool(id) != bool(row)) |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2131 | assert self._table_name, '_table_name must be defined in your class' |
| 2132 | assert self._fields, '_fields must be defined in your class' |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2133 | if not new_record: |
| 2134 | if self._initialized and not always_query: |
| 2135 | return # We've already been initialized. |
| 2136 | if id is None: |
| 2137 | id = row[0] |
| 2138 | # Tell future constructors to use us instead of re-querying while |
| 2139 | # this instance is still around. |
| 2140 | self._instances_by_type_and_id[(type(self), id)] = self |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2141 | |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2142 | self.__table = self._table_name |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2143 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2144 | self.__new_record = new_record |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2145 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2146 | if row is None: |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 2147 | row = self._fetch_row_from_db(id) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2148 | |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2149 | if self._initialized: |
| 2150 | differences = self._compare_fields_in_row(row) |
| 2151 | if differences: |
showard | 7629f14 | 2009-03-27 21:02:02 +0000 | [diff] [blame] | 2152 | logging.warn( |
| 2153 | 'initialized %s %s instance requery is updating: %s', |
| 2154 | type(self), self.id, differences) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2155 | self._update_fields_from_row(row) |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2156 | self._initialized = True |
| 2157 | |
| 2158 | |
| 2159 | @classmethod |
| 2160 | def _clear_instance_cache(cls): |
| 2161 | """Used for testing, clear the internal instance cache.""" |
| 2162 | cls._instances_by_type_and_id.clear() |
| 2163 | |
| 2164 | |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 2165 | def _fetch_row_from_db(self, row_id): |
| 2166 | sql = 'SELECT * FROM %s WHERE ID=%%s' % self.__table |
| 2167 | rows = _db.execute(sql, (row_id,)) |
| 2168 | if not rows: |
showard | 76e29d1 | 2009-04-15 21:53:10 +0000 | [diff] [blame] | 2169 | raise DBError("row not found (table=%s, row id=%s)" |
| 2170 | % (self.__table, row_id)) |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 2171 | return rows[0] |
| 2172 | |
| 2173 | |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2174 | def _assert_row_length(self, row): |
| 2175 | assert len(row) == len(self._fields), ( |
| 2176 | "table = %s, row = %s/%d, fields = %s/%d" % ( |
| 2177 | self.__table, row, len(row), self._fields, len(self._fields))) |
| 2178 | |
| 2179 | |
| 2180 | def _compare_fields_in_row(self, row): |
| 2181 | """ |
| 2182 | Given a row as returned by a SELECT query, compare it to our existing |
| 2183 | in memory fields. |
| 2184 | |
| 2185 | @param row - A sequence of values corresponding to fields named in |
| 2186 | The class attribute _fields. |
| 2187 | |
| 2188 | @returns A dictionary listing the differences keyed by field name |
| 2189 | containing tuples of (current_value, row_value). |
| 2190 | """ |
| 2191 | self._assert_row_length(row) |
| 2192 | differences = {} |
| 2193 | for field, row_value in itertools.izip(self._fields, row): |
| 2194 | current_value = getattr(self, field) |
| 2195 | if current_value != row_value: |
| 2196 | differences[field] = (current_value, row_value) |
| 2197 | return differences |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2198 | |
| 2199 | |
| 2200 | def _update_fields_from_row(self, row): |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2201 | """ |
| 2202 | Update our field attributes using a single row returned by SELECT. |
| 2203 | |
| 2204 | @param row - A sequence of values corresponding to fields named in |
| 2205 | the class fields list. |
| 2206 | """ |
| 2207 | self._assert_row_length(row) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2208 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2209 | self._valid_fields = set() |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2210 | for field, value in itertools.izip(self._fields, row): |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2211 | setattr(self, field, value) |
| 2212 | self._valid_fields.add(field) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2213 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2214 | self._valid_fields.remove('id') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2215 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2216 | |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 2217 | def update_from_database(self): |
| 2218 | assert self.id is not None |
| 2219 | row = self._fetch_row_from_db(self.id) |
| 2220 | self._update_fields_from_row(row) |
| 2221 | |
| 2222 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2223 | def count(self, where, table = None): |
| 2224 | if not table: |
| 2225 | table = self.__table |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2226 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2227 | rows = _db.execute(""" |
| 2228 | SELECT count(*) FROM %s |
| 2229 | WHERE %s |
| 2230 | """ % (table, where)) |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 2231 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2232 | assert len(rows) == 1 |
| 2233 | |
| 2234 | return int(rows[0][0]) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2235 | |
| 2236 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2237 | def update_field(self, field, value): |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2238 | assert field in self._valid_fields |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2239 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2240 | if getattr(self, field) == value: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2241 | return |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2242 | |
mbligh | f8c624d | 2008-07-03 16:58:45 +0000 | [diff] [blame] | 2243 | query = "UPDATE %s SET %s = %%s WHERE id = %%s" % (self.__table, field) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2244 | _db.execute(query, (value, self.id)) |
| 2245 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2246 | setattr(self, field, value) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2247 | |
| 2248 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2249 | def save(self): |
| 2250 | if self.__new_record: |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2251 | keys = self._fields[1:] # avoid id |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2252 | columns = ','.join([str(key) for key in keys]) |
showard | 76e29d1 | 2009-04-15 21:53:10 +0000 | [diff] [blame] | 2253 | values = [] |
| 2254 | for key in keys: |
| 2255 | value = getattr(self, key) |
| 2256 | if value is None: |
| 2257 | values.append('NULL') |
| 2258 | else: |
| 2259 | values.append('"%s"' % value) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 2260 | values_str = ','.join(values) |
| 2261 | query = ('INSERT INTO %s (%s) VALUES (%s)' % |
| 2262 | (self.__table, columns, values_str)) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2263 | _db.execute(query) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 2264 | # Update our id to the one the database just assigned to us. |
| 2265 | self.id = _db.execute('SELECT LAST_INSERT_ID()')[0][0] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2266 | |
| 2267 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2268 | def delete(self): |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2269 | self._instances_by_type_and_id.pop((type(self), id), None) |
| 2270 | self._initialized = False |
| 2271 | self._valid_fields.clear() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2272 | query = 'DELETE FROM %s WHERE id=%%s' % self.__table |
| 2273 | _db.execute(query, (self.id,)) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2274 | |
| 2275 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 2276 | @staticmethod |
| 2277 | def _prefix_with(string, prefix): |
| 2278 | if string: |
| 2279 | string = prefix + string |
| 2280 | return string |
| 2281 | |
| 2282 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2283 | @classmethod |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 2284 | def fetch(cls, where='', params=(), joins='', order_by=''): |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 2285 | """ |
| 2286 | Construct instances of our class based on the given database query. |
| 2287 | |
| 2288 | @yields One class instance for each row fetched. |
| 2289 | """ |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 2290 | order_by = cls._prefix_with(order_by, 'ORDER BY ') |
| 2291 | where = cls._prefix_with(where, 'WHERE ') |
| 2292 | query = ('SELECT %(table)s.* FROM %(table)s %(joins)s ' |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2293 | '%(where)s %(order_by)s' % {'table' : cls._table_name, |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 2294 | 'joins' : joins, |
| 2295 | 'where' : where, |
| 2296 | 'order_by' : order_by}) |
| 2297 | rows = _db.execute(query, params) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2298 | for row in rows: |
| 2299 | yield cls(row=row) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2300 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2301 | |
| 2302 | class IneligibleHostQueue(DBObject): |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2303 | _table_name = 'ineligible_host_queues' |
| 2304 | _fields = ('id', 'job_id', 'host_id') |
showard | 04c82c5 | 2008-05-29 19:38:12 +0000 | [diff] [blame] | 2305 | |
| 2306 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 2307 | class AtomicGroup(DBObject): |
| 2308 | _table_name = 'atomic_groups' |
showard | 205fd60 | 2009-03-21 00:17:35 +0000 | [diff] [blame] | 2309 | _fields = ('id', 'name', 'description', 'max_number_of_machines', |
| 2310 | 'invalid') |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 2311 | |
| 2312 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 2313 | class Label(DBObject): |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2314 | _table_name = 'labels' |
| 2315 | _fields = ('id', 'name', 'kernel_config', 'platform', 'invalid', |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 2316 | 'only_if_needed', 'atomic_group_id') |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 2317 | |
| 2318 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2319 | class Host(DBObject): |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2320 | _table_name = 'hosts' |
| 2321 | _fields = ('id', 'hostname', 'locked', 'synch_id', 'status', |
| 2322 | 'invalid', 'protection', 'locked_by_id', 'lock_time', 'dirty') |
| 2323 | |
| 2324 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2325 | def current_task(self): |
| 2326 | rows = _db.execute(""" |
| 2327 | SELECT * FROM host_queue_entries WHERE host_id=%s AND NOT complete AND active |
| 2328 | """, (self.id,)) |
| 2329 | |
| 2330 | if len(rows) == 0: |
| 2331 | return None |
| 2332 | else: |
| 2333 | assert len(rows) == 1 |
| 2334 | results = rows[0]; |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2335 | return HostQueueEntry(row=results) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2336 | |
| 2337 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2338 | def yield_work(self): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 2339 | logging.info("%s yielding work", self.hostname) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2340 | if self.current_task(): |
| 2341 | self.current_task().requeue() |
| 2342 | |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2343 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2344 | def set_status(self,status): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 2345 | logging.info('%s -> %s', self.hostname, status) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2346 | self.update_field('status',status) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2347 | |
| 2348 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2349 | def platform_and_labels(self): |
showard | d8e548a | 2008-09-09 03:04:57 +0000 | [diff] [blame] | 2350 | """ |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2351 | Returns a tuple (platform_name, list_of_all_label_names). |
showard | d8e548a | 2008-09-09 03:04:57 +0000 | [diff] [blame] | 2352 | """ |
| 2353 | rows = _db.execute(""" |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2354 | SELECT labels.name, labels.platform |
showard | d8e548a | 2008-09-09 03:04:57 +0000 | [diff] [blame] | 2355 | FROM labels |
| 2356 | INNER JOIN hosts_labels ON labels.id = hosts_labels.label_id |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2357 | WHERE hosts_labels.host_id = %s |
showard | d8e548a | 2008-09-09 03:04:57 +0000 | [diff] [blame] | 2358 | ORDER BY labels.name |
| 2359 | """, (self.id,)) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2360 | platform = None |
| 2361 | all_labels = [] |
| 2362 | for label_name, is_platform in rows: |
| 2363 | if is_platform: |
| 2364 | platform = label_name |
| 2365 | all_labels.append(label_name) |
| 2366 | return platform, all_labels |
| 2367 | |
| 2368 | |
showard | a64e52a | 2009-06-08 23:24:08 +0000 | [diff] [blame] | 2369 | def reverify_tasks(self, cleanup=True): |
| 2370 | tasks = [VerifyTask(host=self)] |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 2371 | # just to make sure this host does not get taken away |
| 2372 | self.set_status('Verifying') |
showard | a64e52a | 2009-06-08 23:24:08 +0000 | [diff] [blame] | 2373 | if cleanup: |
| 2374 | tasks.insert(0, CleanupTask(host=self)) |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 2375 | self.set_status('Cleaning') |
showard | a64e52a | 2009-06-08 23:24:08 +0000 | [diff] [blame] | 2376 | return tasks |
showard | d8e548a | 2008-09-09 03:04:57 +0000 | [diff] [blame] | 2377 | |
| 2378 | |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 2379 | _ALPHANUM_HOST_RE = re.compile(r'^([a-z-]+)(\d+)$', re.IGNORECASE) |
| 2380 | |
| 2381 | |
| 2382 | @classmethod |
| 2383 | def cmp_for_sort(cls, a, b): |
| 2384 | """ |
| 2385 | A comparison function for sorting Host objects by hostname. |
| 2386 | |
| 2387 | This strips any trailing numeric digits, ignores leading 0s and |
| 2388 | compares hostnames by the leading name and the trailing digits as a |
| 2389 | number. If both hostnames do not match this pattern, they are simply |
| 2390 | compared as lower case strings. |
| 2391 | |
| 2392 | Example of how hostnames will be sorted: |
| 2393 | |
| 2394 | alice, host1, host2, host09, host010, host10, host11, yolkfolk |
| 2395 | |
| 2396 | This hopefully satisfy most people's hostname sorting needs regardless |
| 2397 | of their exact naming schemes. Nobody sane should have both a host10 |
| 2398 | and host010 (but the algorithm works regardless). |
| 2399 | """ |
| 2400 | lower_a = a.hostname.lower() |
| 2401 | lower_b = b.hostname.lower() |
| 2402 | match_a = cls._ALPHANUM_HOST_RE.match(lower_a) |
| 2403 | match_b = cls._ALPHANUM_HOST_RE.match(lower_b) |
| 2404 | if match_a and match_b: |
| 2405 | name_a, number_a_str = match_a.groups() |
| 2406 | name_b, number_b_str = match_b.groups() |
| 2407 | number_a = int(number_a_str.lstrip('0')) |
| 2408 | number_b = int(number_b_str.lstrip('0')) |
| 2409 | result = cmp((name_a, number_a), (name_b, number_b)) |
| 2410 | if result == 0 and lower_a != lower_b: |
| 2411 | # If they compared equal above but the lower case names are |
| 2412 | # indeed different, don't report equality. abc012 != abc12. |
| 2413 | return cmp(lower_a, lower_b) |
| 2414 | return result |
| 2415 | else: |
| 2416 | return cmp(lower_a, lower_b) |
| 2417 | |
| 2418 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2419 | class HostQueueEntry(DBObject): |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2420 | _table_name = 'host_queue_entries' |
| 2421 | _fields = ('id', 'job_id', 'host_id', 'status', 'meta_host', |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 2422 | 'active', 'complete', 'deleted', 'execution_subdir', |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 2423 | 'atomic_group_id', 'aborted', 'started_on') |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2424 | |
| 2425 | |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2426 | def __init__(self, id=None, row=None, **kwargs): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2427 | assert id or row |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2428 | super(HostQueueEntry, self).__init__(id=id, row=row, **kwargs) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2429 | self.job = Job(self.job_id) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2430 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2431 | if self.host_id: |
| 2432 | self.host = Host(self.host_id) |
| 2433 | else: |
| 2434 | self.host = None |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2435 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2436 | if self.atomic_group_id: |
| 2437 | self.atomic_group = AtomicGroup(self.atomic_group_id, |
| 2438 | always_query=False) |
| 2439 | else: |
| 2440 | self.atomic_group = None |
| 2441 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2442 | self.queue_log_path = os.path.join(self.job.tag(), |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2443 | 'queue.log.' + str(self.id)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2444 | |
| 2445 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 2446 | @classmethod |
| 2447 | def clone(cls, template): |
| 2448 | """ |
| 2449 | Creates a new row using the values from a template instance. |
| 2450 | |
| 2451 | The new instance will not exist in the database or have a valid |
| 2452 | id attribute until its save() method is called. |
| 2453 | """ |
| 2454 | assert isinstance(template, cls) |
| 2455 | new_row = [getattr(template, field) for field in cls._fields] |
| 2456 | clone = cls(row=new_row, new_record=True) |
| 2457 | clone.id = None |
| 2458 | return clone |
| 2459 | |
| 2460 | |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 2461 | def _view_job_url(self): |
| 2462 | return "%s#tab_id=view_job&object_id=%s" % (_base_url, self.job.id) |
| 2463 | |
| 2464 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2465 | def get_labels(self): |
| 2466 | """ |
| 2467 | Get all labels associated with this host queue entry (either via the |
| 2468 | meta_host or as a job dependency label). The labels yielded are not |
| 2469 | guaranteed to be unique. |
| 2470 | |
| 2471 | @yields Label instances associated with this host_queue_entry. |
| 2472 | """ |
| 2473 | if self.meta_host: |
| 2474 | yield Label(id=self.meta_host, always_query=False) |
| 2475 | labels = Label.fetch( |
| 2476 | joins="JOIN jobs_dependency_labels AS deps " |
| 2477 | "ON (labels.id = deps.label_id)", |
| 2478 | where="deps.job_id = %d" % self.job.id) |
| 2479 | for label in labels: |
| 2480 | yield label |
| 2481 | |
| 2482 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2483 | def set_host(self, host): |
| 2484 | if host: |
| 2485 | self.queue_log_record('Assigning host ' + host.hostname) |
| 2486 | self.update_field('host_id', host.id) |
| 2487 | self.update_field('active', True) |
| 2488 | self.block_host(host.id) |
| 2489 | else: |
| 2490 | self.queue_log_record('Releasing host') |
| 2491 | self.unblock_host(self.host.id) |
| 2492 | self.update_field('host_id', None) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2493 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2494 | self.host = host |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2495 | |
| 2496 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2497 | def get_host(self): |
| 2498 | return self.host |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2499 | |
| 2500 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2501 | def queue_log_record(self, log_line): |
| 2502 | now = str(datetime.datetime.now()) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2503 | _drone_manager.write_lines_to_file(self.queue_log_path, |
| 2504 | [now + ' ' + log_line]) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2505 | |
| 2506 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2507 | def block_host(self, host_id): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 2508 | logging.info("creating block %s/%s", self.job.id, host_id) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2509 | row = [0, self.job.id, host_id] |
| 2510 | block = IneligibleHostQueue(row=row, new_record=True) |
| 2511 | block.save() |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2512 | |
| 2513 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2514 | def unblock_host(self, host_id): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 2515 | logging.info("removing block %s/%s", self.job.id, host_id) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2516 | blocks = IneligibleHostQueue.fetch( |
| 2517 | 'job_id=%d and host_id=%d' % (self.job.id, host_id)) |
| 2518 | for block in blocks: |
| 2519 | block.delete() |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2520 | |
| 2521 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2522 | def set_execution_subdir(self, subdir=None): |
| 2523 | if subdir is None: |
| 2524 | assert self.get_host() |
| 2525 | subdir = self.get_host().hostname |
| 2526 | self.update_field('execution_subdir', subdir) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2527 | |
| 2528 | |
showard | 6355f6b | 2008-12-05 18:52:13 +0000 | [diff] [blame] | 2529 | def _get_hostname(self): |
| 2530 | if self.host: |
| 2531 | return self.host.hostname |
| 2532 | return 'no host' |
| 2533 | |
| 2534 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2535 | def __str__(self): |
| 2536 | return "%s/%d (%d)" % (self._get_hostname(), self.job.id, self.id) |
| 2537 | |
| 2538 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2539 | def set_status(self, status): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2540 | self.update_field('status', status) |
mbligh | f8c624d | 2008-07-03 16:58:45 +0000 | [diff] [blame] | 2541 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 2542 | logging.info("%s -> %s", self, self.status) |
mbligh | f8c624d | 2008-07-03 16:58:45 +0000 | [diff] [blame] | 2543 | |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 2544 | if status in ['Queued', 'Parsing']: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2545 | self.update_field('complete', False) |
| 2546 | self.update_field('active', False) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2547 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2548 | if status in ['Pending', 'Running', 'Verifying', 'Starting', |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2549 | 'Gathering']: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2550 | self.update_field('complete', False) |
| 2551 | self.update_field('active', True) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2552 | |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 2553 | if status in ['Failed', 'Completed', 'Stopped', 'Aborted']: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2554 | self.update_field('complete', True) |
| 2555 | self.update_field('active', False) |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 2556 | |
| 2557 | should_email_status = (status.lower() in _notify_email_statuses or |
| 2558 | 'all' in _notify_email_statuses) |
| 2559 | if should_email_status: |
| 2560 | self._email_on_status(status) |
| 2561 | |
| 2562 | self._email_on_job_complete() |
| 2563 | |
| 2564 | |
| 2565 | def _email_on_status(self, status): |
showard | 6355f6b | 2008-12-05 18:52:13 +0000 | [diff] [blame] | 2566 | hostname = self._get_hostname() |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 2567 | |
| 2568 | subject = 'Autotest: Job ID: %s "%s" Host: %s %s' % ( |
| 2569 | self.job.id, self.job.name, hostname, status) |
| 2570 | body = "Job ID: %s\nJob Name: %s\nHost: %s\nStatus: %s\n%s\n" % ( |
| 2571 | self.job.id, self.job.name, hostname, status, |
| 2572 | self._view_job_url()) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2573 | email_manager.manager.send_email(self.job.email_list, subject, body) |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame] | 2574 | |
| 2575 | |
| 2576 | def _email_on_job_complete(self): |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 2577 | if not self.job.is_finished(): |
| 2578 | return |
showard | 542e840 | 2008-09-19 20:16:18 +0000 | [diff] [blame] | 2579 | |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 2580 | summary_text = [] |
showard | 6355f6b | 2008-12-05 18:52:13 +0000 | [diff] [blame] | 2581 | hosts_queue = HostQueueEntry.fetch('job_id = %s' % self.job.id) |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 2582 | for queue_entry in hosts_queue: |
| 2583 | summary_text.append("Host: %s Status: %s" % |
showard | 6355f6b | 2008-12-05 18:52:13 +0000 | [diff] [blame] | 2584 | (queue_entry._get_hostname(), |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 2585 | queue_entry.status)) |
| 2586 | |
| 2587 | summary_text = "\n".join(summary_text) |
| 2588 | status_counts = models.Job.objects.get_status_counts( |
| 2589 | [self.job.id])[self.job.id] |
| 2590 | status = ', '.join('%d %s' % (count, status) for status, count |
| 2591 | in status_counts.iteritems()) |
| 2592 | |
| 2593 | subject = 'Autotest: Job ID: %s "%s" %s' % ( |
| 2594 | self.job.id, self.job.name, status) |
| 2595 | body = "Job ID: %s\nJob Name: %s\nStatus: %s\n%s\nSummary:\n%s" % ( |
| 2596 | self.job.id, self.job.name, status, self._view_job_url(), |
| 2597 | summary_text) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2598 | email_manager.manager.send_email(self.job.email_list, subject, body) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2599 | |
| 2600 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2601 | def run_pre_job_tasks(self, assigned_host=None): |
| 2602 | if self.meta_host is not None or self.atomic_group: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2603 | assert assigned_host |
| 2604 | # ensure results dir exists for the queue log |
showard | 08356c1 | 2009-06-15 20:24:01 +0000 | [diff] [blame] | 2605 | if self.host_id is None: |
| 2606 | self.set_host(assigned_host) |
| 2607 | else: |
| 2608 | assert assigned_host.id == self.host_id |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2609 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 2610 | logging.info("%s/%s/%s scheduled on %s, status=%s", |
| 2611 | self.job.name, self.meta_host, self.atomic_group_id, |
| 2612 | self.host.hostname, self.status) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2613 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2614 | return self._do_run_pre_job_tasks() |
| 2615 | |
| 2616 | |
| 2617 | def _do_run_pre_job_tasks(self): |
| 2618 | # Every host goes thru the Verifying stage (which may or may not |
| 2619 | # actually do anything as determined by get_pre_job_tasks). |
| 2620 | self.set_status(models.HostQueueEntry.Status.VERIFYING) |
| 2621 | |
| 2622 | # The pre job tasks always end with a SetEntryPendingTask which |
| 2623 | # will continue as appropriate through queue_entry.on_pending(). |
| 2624 | return Agent(self.job.get_pre_job_tasks(queue_entry=self)) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2625 | |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2626 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2627 | def requeue(self): |
| 2628 | self.set_status('Queued') |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 2629 | self.update_field('started_on', None) |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 2630 | # verify/cleanup failure sets the execution subdir, so reset it here |
| 2631 | self.set_execution_subdir('') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2632 | if self.meta_host: |
| 2633 | self.set_host(None) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2634 | |
| 2635 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2636 | def handle_host_failure(self): |
| 2637 | """\ |
| 2638 | Called when this queue entry's host has failed verification and |
| 2639 | repair. |
| 2640 | """ |
| 2641 | assert not self.meta_host |
| 2642 | self.set_status('Failed') |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2643 | self.job.stop_if_necessary() |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2644 | |
| 2645 | |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 2646 | @property |
| 2647 | def aborted_by(self): |
| 2648 | self._load_abort_info() |
| 2649 | return self._aborted_by |
| 2650 | |
| 2651 | |
| 2652 | @property |
| 2653 | def aborted_on(self): |
| 2654 | self._load_abort_info() |
| 2655 | return self._aborted_on |
| 2656 | |
| 2657 | |
| 2658 | def _load_abort_info(self): |
| 2659 | """ Fetch info about who aborted the job. """ |
| 2660 | if hasattr(self, "_aborted_by"): |
| 2661 | return |
| 2662 | rows = _db.execute(""" |
| 2663 | SELECT users.login, aborted_host_queue_entries.aborted_on |
| 2664 | FROM aborted_host_queue_entries |
| 2665 | INNER JOIN users |
| 2666 | ON users.id = aborted_host_queue_entries.aborted_by_id |
| 2667 | WHERE aborted_host_queue_entries.queue_entry_id = %s |
| 2668 | """, (self.id,)) |
| 2669 | if rows: |
| 2670 | self._aborted_by, self._aborted_on = rows[0] |
| 2671 | else: |
| 2672 | self._aborted_by = self._aborted_on = None |
| 2673 | |
| 2674 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 2675 | def on_pending(self): |
| 2676 | """ |
| 2677 | Called when an entry in a synchronous job has passed verify. If the |
| 2678 | job is ready to run, returns an agent to run the job. Returns None |
| 2679 | otherwise. |
| 2680 | """ |
| 2681 | self.set_status('Pending') |
showard | cfd66a3 | 2008-10-15 20:31:48 +0000 | [diff] [blame] | 2682 | self.get_host().set_status('Pending') |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2683 | return self.job.run_if_ready(queue_entry=self) |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 2684 | |
| 2685 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2686 | def abort(self, dispatcher): |
| 2687 | assert self.aborted and not self.complete |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 2688 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2689 | Status = models.HostQueueEntry.Status |
| 2690 | has_running_job_agent = ( |
| 2691 | self.status in (Status.RUNNING, Status.GATHERING, Status.PARSING) |
| 2692 | and dispatcher.get_agents_for_entry(self)) |
| 2693 | if has_running_job_agent: |
| 2694 | # do nothing; post-job tasks will finish and then mark this entry |
| 2695 | # with status "Aborted" and take care of the host |
| 2696 | return |
| 2697 | |
| 2698 | if self.status in (Status.STARTING, Status.PENDING): |
| 2699 | self.host.set_status(models.Host.Status.READY) |
| 2700 | elif self.status == Status.VERIFYING: |
| 2701 | dispatcher.add_agent(Agent(tasks=self.host.reverify_tasks())) |
| 2702 | |
| 2703 | self.set_status(Status.ABORTED) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2704 | |
| 2705 | def execution_tag(self): |
| 2706 | assert self.execution_subdir |
mbligh | e7d9c60 | 2009-07-02 19:02:33 +0000 | [diff] [blame^] | 2707 | return "%s/%s" % (self.job.tag(), self.execution_subdir) |
showard | 1be9743 | 2008-10-17 15:30:45 +0000 | [diff] [blame] | 2708 | |
| 2709 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2710 | class Job(DBObject): |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2711 | _table_name = 'jobs' |
| 2712 | _fields = ('id', 'owner', 'name', 'priority', 'control_file', |
| 2713 | 'control_type', 'created_on', 'synch_count', 'timeout', |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 2714 | 'run_verify', 'email_list', 'reboot_before', 'reboot_after', |
showard | 12f3e32 | 2009-05-13 21:27:42 +0000 | [diff] [blame] | 2715 | 'parse_failed_repair', 'max_runtime_hrs') |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2716 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2717 | # This does not need to be a column in the DB. The delays are likely to |
| 2718 | # be configured short. If the scheduler is stopped and restarted in |
| 2719 | # the middle of a job's delay cycle, the delay cycle will either be |
| 2720 | # repeated or skipped depending on the number of Pending machines found |
| 2721 | # when the restarted scheduler recovers to track it. Not a problem. |
| 2722 | # |
| 2723 | # A reference to the DelayedCallTask that will wake up the job should |
| 2724 | # no other HQEs change state in time. Its end_time attribute is used |
| 2725 | # by our run_with_ready_delay() method to determine if the wait is over. |
| 2726 | _delay_ready_task = None |
| 2727 | |
| 2728 | # TODO(gps): On scheduler start/recovery we need to call HQE.on_pending() on |
| 2729 | # all status='Pending' atomic group HQEs incase a delay was running when the |
| 2730 | # scheduler was restarted and no more hosts ever successfully exit Verify. |
showard | 6ae5ea9 | 2009-02-25 00:11:51 +0000 | [diff] [blame] | 2731 | |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2732 | def __init__(self, id=None, row=None, **kwargs): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2733 | assert id or row |
showard | a3c5857 | 2009-03-12 20:36:59 +0000 | [diff] [blame] | 2734 | super(Job, self).__init__(id=id, row=row, **kwargs) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2735 | |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2736 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2737 | def is_server_job(self): |
| 2738 | return self.control_type != 2 |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2739 | |
| 2740 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2741 | def tag(self): |
| 2742 | return "%s-%s" % (self.id, self.owner) |
| 2743 | |
| 2744 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2745 | def get_host_queue_entries(self): |
| 2746 | rows = _db.execute(""" |
| 2747 | SELECT * FROM host_queue_entries |
| 2748 | WHERE job_id= %s |
| 2749 | """, (self.id,)) |
| 2750 | entries = [HostQueueEntry(row=i) for i in rows] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2751 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2752 | assert len(entries)>0 |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2753 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2754 | return entries |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2755 | |
| 2756 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2757 | def set_status(self, status, update_queues=False): |
| 2758 | self.update_field('status',status) |
| 2759 | |
| 2760 | if update_queues: |
| 2761 | for queue_entry in self.get_host_queue_entries(): |
| 2762 | queue_entry.set_status(status) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2763 | |
| 2764 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2765 | def _atomic_and_has_started(self): |
| 2766 | """ |
| 2767 | @returns True if any of the HostQueueEntries associated with this job |
| 2768 | have entered the Status.STARTING state or beyond. |
| 2769 | """ |
| 2770 | atomic_entries = models.HostQueueEntry.objects.filter( |
| 2771 | job=self.id, atomic_group__isnull=False) |
| 2772 | if atomic_entries.count() <= 0: |
| 2773 | return False |
| 2774 | |
showard | af8b4ca | 2009-06-16 18:47:26 +0000 | [diff] [blame] | 2775 | # These states may *only* be reached if Job.run() has been called. |
| 2776 | started_statuses = (models.HostQueueEntry.Status.STARTING, |
| 2777 | models.HostQueueEntry.Status.RUNNING, |
| 2778 | models.HostQueueEntry.Status.COMPLETED) |
| 2779 | |
| 2780 | started_entries = atomic_entries.filter(status__in=started_statuses) |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2781 | return started_entries.count() > 0 |
| 2782 | |
| 2783 | |
| 2784 | def _pending_count(self): |
| 2785 | """The number of HostQueueEntries for this job in the Pending state.""" |
| 2786 | pending_entries = models.HostQueueEntry.objects.filter( |
| 2787 | job=self.id, status=models.HostQueueEntry.Status.PENDING) |
| 2788 | return pending_entries.count() |
| 2789 | |
| 2790 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2791 | def is_ready(self): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2792 | # NOTE: Atomic group jobs stop reporting ready after they have been |
| 2793 | # started to avoid launching multiple copies of one atomic job. |
| 2794 | # Only possible if synch_count is less than than half the number of |
| 2795 | # machines in the atomic group. |
| 2796 | return (self._pending_count() >= self.synch_count |
| 2797 | and not self._atomic_and_has_started()) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2798 | |
| 2799 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2800 | def num_machines(self, clause = None): |
| 2801 | sql = "job_id=%s" % self.id |
| 2802 | if clause: |
| 2803 | sql += " AND (%s)" % clause |
| 2804 | return self.count(sql, table='host_queue_entries') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2805 | |
| 2806 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2807 | def num_queued(self): |
| 2808 | return self.num_machines('not complete') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2809 | |
| 2810 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2811 | def num_active(self): |
| 2812 | return self.num_machines('active') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2813 | |
| 2814 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2815 | def num_complete(self): |
| 2816 | return self.num_machines('complete') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2817 | |
| 2818 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2819 | def is_finished(self): |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 2820 | return self.num_complete() == self.num_machines() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2821 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2822 | |
showard | 6bb7c29 | 2009-01-30 01:44:51 +0000 | [diff] [blame] | 2823 | def _not_yet_run_entries(self, include_verifying=True): |
| 2824 | statuses = [models.HostQueueEntry.Status.QUEUED, |
| 2825 | models.HostQueueEntry.Status.PENDING] |
| 2826 | if include_verifying: |
| 2827 | statuses.append(models.HostQueueEntry.Status.VERIFYING) |
| 2828 | return models.HostQueueEntry.objects.filter(job=self.id, |
| 2829 | status__in=statuses) |
| 2830 | |
| 2831 | |
| 2832 | def _stop_all_entries(self): |
| 2833 | entries_to_stop = self._not_yet_run_entries( |
| 2834 | include_verifying=False) |
| 2835 | for child_entry in entries_to_stop: |
showard | 4f9e537 | 2009-01-07 21:33:38 +0000 | [diff] [blame] | 2836 | assert not child_entry.complete, ( |
| 2837 | '%s status=%s, active=%s, complete=%s' % |
| 2838 | (child_entry.id, child_entry.status, child_entry.active, |
| 2839 | child_entry.complete)) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2840 | if child_entry.status == models.HostQueueEntry.Status.PENDING: |
| 2841 | child_entry.host.status = models.Host.Status.READY |
| 2842 | child_entry.host.save() |
| 2843 | child_entry.status = models.HostQueueEntry.Status.STOPPED |
| 2844 | child_entry.save() |
| 2845 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2846 | def stop_if_necessary(self): |
showard | 6bb7c29 | 2009-01-30 01:44:51 +0000 | [diff] [blame] | 2847 | not_yet_run = self._not_yet_run_entries() |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2848 | if not_yet_run.count() < self.synch_count: |
showard | 6bb7c29 | 2009-01-30 01:44:51 +0000 | [diff] [blame] | 2849 | self._stop_all_entries() |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2850 | |
| 2851 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2852 | def write_to_machines_file(self, queue_entry): |
| 2853 | hostname = queue_entry.get_host().hostname |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2854 | file_path = os.path.join(self.tag(), '.machines') |
| 2855 | _drone_manager.write_lines_to_file(file_path, [hostname]) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2856 | |
| 2857 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2858 | def _next_group_name(self, group_name=''): |
| 2859 | """@returns a directory name to use for the next host group results.""" |
| 2860 | if group_name: |
| 2861 | # Sanitize for use as a pathname. |
| 2862 | group_name = group_name.replace(os.path.sep, '_') |
| 2863 | if group_name.startswith('.'): |
| 2864 | group_name = '_' + group_name[1:] |
| 2865 | # Add a separator between the group name and 'group%d'. |
| 2866 | group_name += '.' |
| 2867 | group_count_re = re.compile(r'%sgroup(\d+)' % re.escape(group_name)) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2868 | query = models.HostQueueEntry.objects.filter( |
| 2869 | job=self.id).values('execution_subdir').distinct() |
| 2870 | subdirs = (entry['execution_subdir'] for entry in query) |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2871 | group_matches = (group_count_re.match(subdir) for subdir in subdirs) |
| 2872 | ids = [int(match.group(1)) for match in group_matches if match] |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2873 | if ids: |
| 2874 | next_id = max(ids) + 1 |
| 2875 | else: |
| 2876 | next_id = 0 |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2877 | return '%sgroup%d' % (group_name, next_id) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2878 | |
| 2879 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2880 | def _write_control_file(self, execution_tag): |
| 2881 | control_path = _drone_manager.attach_file_to_execution( |
| 2882 | execution_tag, self.control_file) |
| 2883 | return control_path |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2884 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 2885 | |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2886 | def get_group_entries(self, queue_entry_from_group): |
| 2887 | execution_subdir = queue_entry_from_group.execution_subdir |
showard | e788ea6 | 2008-11-17 21:02:47 +0000 | [diff] [blame] | 2888 | return list(HostQueueEntry.fetch( |
| 2889 | where='job_id=%s AND execution_subdir=%s', |
| 2890 | params=(self.id, execution_subdir))) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2891 | |
| 2892 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 2893 | def _get_autoserv_params(self, queue_entries): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2894 | assert queue_entries |
| 2895 | execution_tag = queue_entries[0].execution_tag() |
| 2896 | control_path = self._write_control_file(execution_tag) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2897 | hostnames = ','.join([entry.get_host().hostname |
| 2898 | for entry in queue_entries]) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2899 | |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 2900 | params = _autoserv_command_line( |
| 2901 | hostnames, execution_tag, |
| 2902 | ['-P', execution_tag, '-n', |
| 2903 | _drone_manager.absolute_path(control_path)], |
showard | e9c6936 | 2009-06-30 01:58:03 +0000 | [diff] [blame] | 2904 | job=self, verbose=False) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2905 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2906 | if not self.is_server_job(): |
| 2907 | params.append('-c') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2908 | |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 2909 | return params |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2910 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2911 | |
showard | c9ae178 | 2009-01-30 01:42:37 +0000 | [diff] [blame] | 2912 | def _should_run_cleanup(self, queue_entry): |
showard | 0fc3830 | 2008-10-23 00:44:07 +0000 | [diff] [blame] | 2913 | if self.reboot_before == models.RebootBefore.ALWAYS: |
showard | c9ae178 | 2009-01-30 01:42:37 +0000 | [diff] [blame] | 2914 | return True |
showard | 0fc3830 | 2008-10-23 00:44:07 +0000 | [diff] [blame] | 2915 | elif self.reboot_before == models.RebootBefore.IF_DIRTY: |
showard | c9ae178 | 2009-01-30 01:42:37 +0000 | [diff] [blame] | 2916 | return queue_entry.get_host().dirty |
| 2917 | return False |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2918 | |
showard | c9ae178 | 2009-01-30 01:42:37 +0000 | [diff] [blame] | 2919 | |
| 2920 | def _should_run_verify(self, queue_entry): |
| 2921 | do_not_verify = (queue_entry.host.protection == |
| 2922 | host_protections.Protection.DO_NOT_VERIFY) |
| 2923 | if do_not_verify: |
| 2924 | return False |
| 2925 | return self.run_verify |
| 2926 | |
| 2927 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2928 | def get_pre_job_tasks(self, queue_entry): |
| 2929 | """ |
| 2930 | Get a list of tasks to perform before the host_queue_entry |
| 2931 | may be used to run this Job (such as Cleanup & Verify). |
| 2932 | |
| 2933 | @returns A list of tasks to be done to the given queue_entry before |
| 2934 | it should be considered be ready to run this job. The last |
| 2935 | task in the list calls HostQueueEntry.on_pending(), which |
| 2936 | continues the flow of the job. |
| 2937 | """ |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2938 | tasks = [] |
showard | c9ae178 | 2009-01-30 01:42:37 +0000 | [diff] [blame] | 2939 | if self._should_run_cleanup(queue_entry): |
showard | 45ae819 | 2008-11-05 19:32:53 +0000 | [diff] [blame] | 2940 | tasks.append(CleanupTask(queue_entry=queue_entry)) |
showard | c9ae178 | 2009-01-30 01:42:37 +0000 | [diff] [blame] | 2941 | if self._should_run_verify(queue_entry): |
| 2942 | tasks.append(VerifyTask(queue_entry=queue_entry)) |
| 2943 | tasks.append(SetEntryPendingTask(queue_entry)) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2944 | return tasks |
| 2945 | |
| 2946 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2947 | def _assign_new_group(self, queue_entries, group_name=''): |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2948 | if len(queue_entries) == 1: |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2949 | group_subdir_name = queue_entries[0].get_host().hostname |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2950 | else: |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2951 | group_subdir_name = self._next_group_name(group_name) |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 2952 | logging.info('Running synchronous job %d hosts %s as %s', |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2953 | self.id, [entry.host.hostname for entry in queue_entries], |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2954 | group_subdir_name) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2955 | |
| 2956 | for queue_entry in queue_entries: |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2957 | queue_entry.set_execution_subdir(group_subdir_name) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2958 | |
| 2959 | |
| 2960 | def _choose_group_to_run(self, include_queue_entry): |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2961 | """ |
| 2962 | @returns A tuple containing a list of HostQueueEntry instances to be |
| 2963 | used to run this Job, a string group name to suggest giving |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 2964 | to this job in the results database. |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2965 | """ |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 2966 | atomic_group = include_queue_entry.atomic_group |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2967 | chosen_entries = [include_queue_entry] |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2968 | if atomic_group: |
| 2969 | num_entries_wanted = atomic_group.max_number_of_machines |
| 2970 | else: |
| 2971 | num_entries_wanted = self.synch_count |
| 2972 | num_entries_wanted -= len(chosen_entries) |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2973 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2974 | if num_entries_wanted > 0: |
| 2975 | where_clause = 'job_id = %s AND status = "Pending" AND id != %s' |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 2976 | pending_entries = list(HostQueueEntry.fetch( |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2977 | where=where_clause, |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 2978 | params=(self.id, include_queue_entry.id))) |
| 2979 | |
| 2980 | # Sort the chosen hosts by hostname before slicing. |
| 2981 | def cmp_queue_entries_by_hostname(entry_a, entry_b): |
| 2982 | return Host.cmp_for_sort(entry_a.host, entry_b.host) |
| 2983 | pending_entries.sort(cmp=cmp_queue_entries_by_hostname) |
| 2984 | chosen_entries += pending_entries[:num_entries_wanted] |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 2985 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2986 | # Sanity check. We'll only ever be called if this can be met. |
| 2987 | assert len(chosen_entries) >= self.synch_count |
| 2988 | |
| 2989 | if atomic_group: |
| 2990 | # Look at any meta_host and dependency labels and pick the first |
| 2991 | # one that also specifies this atomic group. Use that label name |
| 2992 | # as the group name if possible (it is more specific). |
| 2993 | group_name = atomic_group.name |
| 2994 | for label in include_queue_entry.get_labels(): |
| 2995 | if label.atomic_group_id: |
| 2996 | assert label.atomic_group_id == atomic_group.id |
| 2997 | group_name = label.name |
| 2998 | break |
| 2999 | else: |
| 3000 | group_name = '' |
| 3001 | |
| 3002 | self._assign_new_group(chosen_entries, group_name=group_name) |
| 3003 | return chosen_entries, group_name |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 3004 | |
| 3005 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3006 | def run_if_ready(self, queue_entry): |
| 3007 | """ |
| 3008 | @returns An Agent instance to ultimately run this job if enough hosts |
| 3009 | are ready for it to run. |
| 3010 | @returns None and potentially cleans up excess hosts if this Job |
| 3011 | is not ready to run. |
| 3012 | """ |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 3013 | if not self.is_ready(): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3014 | self.stop_if_necessary() |
| 3015 | return None |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3016 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3017 | if queue_entry.atomic_group: |
| 3018 | return self.run_with_ready_delay(queue_entry) |
| 3019 | |
| 3020 | return self.run(queue_entry) |
| 3021 | |
| 3022 | |
| 3023 | def run_with_ready_delay(self, queue_entry): |
| 3024 | """ |
| 3025 | Start a delay to wait for more hosts to enter Pending state before |
| 3026 | launching an atomic group job. Once set, the a delay cannot be reset. |
| 3027 | |
| 3028 | @param queue_entry: The HostQueueEntry object to get atomic group |
| 3029 | info from and pass to run_if_ready when the delay is up. |
| 3030 | |
| 3031 | @returns An Agent to run the job as appropriate or None if a delay |
| 3032 | has already been set. |
| 3033 | """ |
| 3034 | assert queue_entry.job_id == self.id |
| 3035 | assert queue_entry.atomic_group |
| 3036 | delay = scheduler_config.config.secs_to_wait_for_atomic_group_hosts |
| 3037 | pending_threshold = queue_entry.atomic_group.max_number_of_machines |
| 3038 | over_max_threshold = (self._pending_count() >= pending_threshold) |
| 3039 | delay_expired = (self._delay_ready_task and |
| 3040 | time.time() >= self._delay_ready_task.end_time) |
| 3041 | |
| 3042 | # Delay is disabled or we already have enough? Do not wait to run. |
| 3043 | if not delay or over_max_threshold or delay_expired: |
| 3044 | return self.run(queue_entry) |
| 3045 | |
| 3046 | # A delay was previously scheduled. |
| 3047 | if self._delay_ready_task: |
| 3048 | return None |
| 3049 | |
| 3050 | def run_job_after_delay(): |
| 3051 | logging.info('Job %s done waiting for extra hosts.', self.id) |
| 3052 | return self.run(queue_entry) |
| 3053 | |
| 3054 | self._delay_ready_task = DelayedCallTask(delay_seconds=delay, |
| 3055 | callback=run_job_after_delay) |
| 3056 | |
| 3057 | return Agent([self._delay_ready_task], num_processes=0) |
| 3058 | |
| 3059 | |
| 3060 | def run(self, queue_entry): |
| 3061 | """ |
| 3062 | @param queue_entry: The HostQueueEntry instance calling this method. |
| 3063 | @returns An Agent instance to run this job or None if we've already |
| 3064 | been run. |
| 3065 | """ |
| 3066 | if queue_entry.atomic_group and self._atomic_and_has_started(): |
| 3067 | logging.error('Job.run() called on running atomic Job %d ' |
| 3068 | 'with HQE %s.', self.id, queue_entry) |
| 3069 | return None |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 3070 | queue_entries, group_name = self._choose_group_to_run(queue_entry) |
| 3071 | return self._finish_run(queue_entries, group_name) |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 3072 | |
| 3073 | |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 3074 | def _finish_run(self, queue_entries, group_name): |
showard | b2ccdda | 2008-10-28 20:39:05 +0000 | [diff] [blame] | 3075 | for queue_entry in queue_entries: |
| 3076 | queue_entry.set_status('Starting') |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 3077 | params = self._get_autoserv_params(queue_entries) |
| 3078 | queue_task = QueueTask(job=self, queue_entries=queue_entries, |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 3079 | cmd=params, group_name=group_name) |
| 3080 | tasks = [queue_task] |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 3081 | if self._delay_ready_task: |
| 3082 | # Cancel any pending callback that would try to run again |
| 3083 | # as we are already running. |
| 3084 | self._delay_ready_task.abort() |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 3085 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 3086 | return Agent(tasks, num_processes=len(queue_entries)) |
showard | b2e2c32 | 2008-10-14 17:33:55 +0000 | [diff] [blame] | 3087 | |
| 3088 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3089 | if __name__ == '__main__': |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 3090 | main() |