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