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