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