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