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 |
jamesren | dd85524 | 2010-03-02 22:23:44 +0000 | [diff] [blame] | 24 | from autotest_lib.frontend.afe import model_attributes |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 25 | from autotest_lib.scheduler import drone_manager, drones, email_manager |
showard | 043c62a | 2009-06-10 19:48:57 +0000 | [diff] [blame] | 26 | from autotest_lib.scheduler import monitor_db_cleanup |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 27 | from autotest_lib.scheduler import status_server, scheduler_config |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 28 | from autotest_lib.scheduler import gc_stats, metahost_scheduler |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 29 | from autotest_lib.scheduler import scheduler_models |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 30 | BABYSITTER_PID_FILE_PREFIX = 'monitor_db_babysitter' |
| 31 | PID_FILE_PREFIX = 'monitor_db' |
mbligh | b090f14 | 2008-02-27 21:33:46 +0000 | [diff] [blame] | 32 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 33 | RESULTS_DIR = '.' |
| 34 | AUTOSERV_NICE_LEVEL = 10 |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 35 | DB_CONFIG_SECTION = 'AUTOTEST_WEB' |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 36 | AUTOTEST_PATH = os.path.join(os.path.dirname(__file__), '..') |
| 37 | |
| 38 | if os.environ.has_key('AUTOTEST_DIR'): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 39 | AUTOTEST_PATH = os.environ['AUTOTEST_DIR'] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 40 | AUTOTEST_SERVER_DIR = os.path.join(AUTOTEST_PATH, 'server') |
| 41 | AUTOTEST_TKO_DIR = os.path.join(AUTOTEST_PATH, 'tko') |
| 42 | |
| 43 | if AUTOTEST_SERVER_DIR not in sys.path: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 44 | sys.path.insert(0, AUTOTEST_SERVER_DIR) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 45 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 46 | # error message to leave in results dir when an autoserv process disappears |
| 47 | # mysteriously |
| 48 | _LOST_PROCESS_ERROR = """\ |
| 49 | Autoserv failed abnormally during execution for this job, probably due to a |
| 50 | system error on the Autotest server. Full results may not be available. Sorry. |
| 51 | """ |
| 52 | |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 53 | _db = None |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 54 | _shutdown = False |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 55 | _autoserv_path = os.path.join(drones.AUTOTEST_INSTALL_DIR, 'server', 'autoserv') |
| 56 | _parser_path = os.path.join(drones.AUTOTEST_INSTALL_DIR, 'tko', 'parse') |
mbligh | 4314a71 | 2008-02-29 22:44:30 +0000 | [diff] [blame] | 57 | _testing_mode = False |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 58 | _drone_manager = None |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 59 | |
| 60 | |
showard | ec6a3b9 | 2009-09-25 20:29:13 +0000 | [diff] [blame] | 61 | def _get_pidfile_timeout_secs(): |
| 62 | """@returns How long to wait for autoserv to write pidfile.""" |
| 63 | pidfile_timeout_mins = global_config.global_config.get_config_value( |
| 64 | scheduler_config.CONFIG_SECTION, 'pidfile_timeout_mins', type=int) |
| 65 | return pidfile_timeout_mins * 60 |
| 66 | |
| 67 | |
mbligh | 83c1e9e | 2009-05-01 23:10:41 +0000 | [diff] [blame] | 68 | def _site_init_monitor_db_dummy(): |
| 69 | return {} |
| 70 | |
| 71 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 72 | get_site_metahost_schedulers = utils.import_site_function( |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 73 | __file__, 'autotest_lib.scheduler.site_metahost_scheduler', |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 74 | 'get_metahost_schedulers', lambda : ()) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 75 | |
| 76 | |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 77 | def _verify_default_drone_set_exists(): |
| 78 | if (models.DroneSet.drone_sets_enabled() and |
| 79 | not models.DroneSet.default_drone_set_name()): |
| 80 | raise SchedulerError('Drone sets are enabled, but no default is set') |
| 81 | |
| 82 | |
| 83 | def _sanity_check(): |
| 84 | """Make sure the configs are consistent before starting the scheduler""" |
| 85 | _verify_default_drone_set_exists() |
| 86 | |
| 87 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 88 | def main(): |
showard | 27f3387 | 2009-04-07 18:20:53 +0000 | [diff] [blame] | 89 | try: |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 90 | try: |
| 91 | main_without_exception_handling() |
| 92 | except SystemExit: |
| 93 | raise |
| 94 | except: |
| 95 | logging.exception('Exception escaping in monitor_db') |
| 96 | raise |
| 97 | finally: |
| 98 | utils.delete_pid_file_if_exists(PID_FILE_PREFIX) |
showard | 27f3387 | 2009-04-07 18:20:53 +0000 | [diff] [blame] | 99 | |
| 100 | |
| 101 | def main_without_exception_handling(): |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 102 | setup_logging() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 103 | |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 104 | usage = 'usage: %prog [options] results_dir' |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 105 | parser = optparse.OptionParser(usage) |
| 106 | parser.add_option('--recover-hosts', help='Try to recover dead hosts', |
| 107 | action='store_true') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 108 | parser.add_option('--test', help='Indicate that scheduler is under ' + |
| 109 | 'test and should use dummy autoserv and no parsing', |
| 110 | action='store_true') |
| 111 | (options, args) = parser.parse_args() |
| 112 | if len(args) != 1: |
| 113 | parser.print_usage() |
| 114 | return |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 115 | |
showard | 5613c66 | 2009-06-08 23:30:33 +0000 | [diff] [blame] | 116 | scheduler_enabled = global_config.global_config.get_config_value( |
| 117 | scheduler_config.CONFIG_SECTION, 'enable_scheduler', type=bool) |
| 118 | |
| 119 | if not scheduler_enabled: |
| 120 | msg = ("Scheduler not enabled, set enable_scheduler to true in the " |
| 121 | "global_config's SCHEDULER section to enabled it. Exiting.") |
mbligh | 6fbdb80 | 2009-08-03 16:42:55 +0000 | [diff] [blame] | 122 | logging.error(msg) |
showard | 5613c66 | 2009-06-08 23:30:33 +0000 | [diff] [blame] | 123 | sys.exit(1) |
| 124 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 125 | global RESULTS_DIR |
| 126 | RESULTS_DIR = args[0] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 127 | |
mbligh | 83c1e9e | 2009-05-01 23:10:41 +0000 | [diff] [blame] | 128 | site_init = utils.import_site_function(__file__, |
| 129 | "autotest_lib.scheduler.site_monitor_db", "site_init_monitor_db", |
| 130 | _site_init_monitor_db_dummy) |
| 131 | site_init() |
| 132 | |
showard | cca334f | 2009-03-12 20:38:34 +0000 | [diff] [blame] | 133 | # Change the cwd while running to avoid issues incase we were launched from |
| 134 | # somewhere odd (such as a random NFS home directory of the person running |
| 135 | # sudo to launch us as the appropriate user). |
| 136 | os.chdir(RESULTS_DIR) |
| 137 | |
jamesren | c7d387e | 2010-08-10 21:48:30 +0000 | [diff] [blame^] | 138 | # This is helpful for debugging why stuff a scheduler launches is |
| 139 | # misbehaving. |
| 140 | logging.info('os.environ: %s', os.environ) |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 141 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 142 | if options.test: |
| 143 | global _autoserv_path |
| 144 | _autoserv_path = 'autoserv_dummy' |
| 145 | global _testing_mode |
| 146 | _testing_mode = True |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 147 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 148 | server = status_server.StatusServer() |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 149 | server.start() |
| 150 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 151 | try: |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 152 | initialize() |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 153 | dispatcher = Dispatcher() |
showard | 915958d | 2009-04-22 21:00:58 +0000 | [diff] [blame] | 154 | dispatcher.initialize(recover_hosts=options.recover_hosts) |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 155 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 156 | while not _shutdown: |
| 157 | dispatcher.tick() |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 158 | time.sleep(scheduler_config.config.tick_pause_sec) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 159 | except: |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 160 | email_manager.manager.log_stacktrace( |
| 161 | "Uncaught exception; terminating monitor_db") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 162 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 163 | email_manager.manager.send_queued_emails() |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 164 | server.shutdown() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 165 | _drone_manager.shutdown() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 166 | _db.disconnect() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 167 | |
| 168 | |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 169 | def setup_logging(): |
| 170 | log_dir = os.environ.get('AUTOTEST_SCHEDULER_LOG_DIR', None) |
| 171 | log_name = os.environ.get('AUTOTEST_SCHEDULER_LOG_NAME', None) |
| 172 | logging_manager.configure_logging( |
| 173 | scheduler_logging_config.SchedulerLoggingConfig(), log_dir=log_dir, |
| 174 | logfile_name=log_name) |
| 175 | |
| 176 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 177 | def handle_sigint(signum, frame): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 178 | global _shutdown |
| 179 | _shutdown = True |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 180 | logging.info("Shutdown request received.") |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 181 | |
| 182 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 183 | def initialize(): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 184 | logging.info("%s> dispatcher starting", time.strftime("%X %x")) |
| 185 | logging.info("My PID is %d", os.getpid()) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 186 | |
showard | 8de3713 | 2009-08-31 18:33:08 +0000 | [diff] [blame] | 187 | if utils.program_is_alive(PID_FILE_PREFIX): |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 188 | logging.critical("monitor_db already running, aborting!") |
| 189 | sys.exit(1) |
| 190 | utils.write_pid(PID_FILE_PREFIX) |
mbligh | fb67603 | 2009-04-01 18:25:38 +0000 | [diff] [blame] | 191 | |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 192 | if _testing_mode: |
| 193 | global_config.global_config.override_config_value( |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 194 | DB_CONFIG_SECTION, 'database', 'stresstest_autotest_web') |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 195 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 196 | os.environ['PATH'] = AUTOTEST_SERVER_DIR + ':' + os.environ['PATH'] |
| 197 | global _db |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 198 | _db = database_connection.DatabaseConnection(DB_CONFIG_SECTION) |
showard | b21b8c8 | 2009-12-07 19:39:39 +0000 | [diff] [blame] | 199 | _db.connect(db_type='django') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 200 | |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 201 | # ensure Django connection is in autocommit |
| 202 | setup_django_environment.enable_autocommit() |
showard | 844960a | 2009-05-29 18:41:18 +0000 | [diff] [blame] | 203 | # bypass the readonly connection |
| 204 | readonly_connection.ReadOnlyConnection.set_globally_disabled(True) |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 205 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 206 | logging.info("Setting signal handler") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 207 | signal.signal(signal.SIGINT, handle_sigint) |
| 208 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 209 | initialize_globals() |
| 210 | scheduler_models.initialize() |
| 211 | |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 212 | drones = global_config.global_config.get_config_value( |
| 213 | scheduler_config.CONFIG_SECTION, 'drones', default='localhost') |
| 214 | drone_list = [hostname.strip() for hostname in drones.split(',')] |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 215 | results_host = global_config.global_config.get_config_value( |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 216 | scheduler_config.CONFIG_SECTION, 'results_host', default='localhost') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 217 | _drone_manager.initialize(RESULTS_DIR, drone_list, results_host) |
| 218 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 219 | logging.info("Connected! Running...") |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 220 | |
| 221 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 222 | def initialize_globals(): |
| 223 | global _drone_manager |
| 224 | _drone_manager = drone_manager.instance() |
| 225 | |
| 226 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 227 | def _autoserv_command_line(machines, extra_args, job=None, queue_entry=None, |
| 228 | verbose=True): |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 229 | """ |
| 230 | @returns The autoserv command line as a list of executable + parameters. |
| 231 | |
| 232 | @param machines - string - A machine or comma separated list of machines |
| 233 | for the (-m) flag. |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 234 | @param extra_args - list - Additional arguments to pass to autoserv. |
| 235 | @param job - Job object - If supplied, -u owner and -l name parameters |
| 236 | will be added. |
| 237 | @param queue_entry - A HostQueueEntry object - If supplied and no Job |
| 238 | object was supplied, this will be used to lookup the Job object. |
| 239 | """ |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 240 | autoserv_argv = [_autoserv_path, '-p', |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 241 | '-r', drone_manager.WORKING_DIRECTORY] |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 242 | if machines: |
| 243 | autoserv_argv += ['-m', machines] |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 244 | if job or queue_entry: |
| 245 | if not job: |
| 246 | job = queue_entry.job |
| 247 | autoserv_argv += ['-u', job.owner, '-l', job.name] |
showard | e9c6936 | 2009-06-30 01:58:03 +0000 | [diff] [blame] | 248 | if verbose: |
| 249 | autoserv_argv.append('--verbose') |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 250 | return autoserv_argv + extra_args |
| 251 | |
| 252 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 253 | class SchedulerError(Exception): |
| 254 | """Raised by HostScheduler when an inconsistent state occurs.""" |
| 255 | |
| 256 | |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 257 | class HostScheduler(metahost_scheduler.HostSchedulingUtility): |
| 258 | """Handles the logic for choosing when to run jobs and on which hosts. |
| 259 | |
| 260 | This class makes several queries to the database on each tick, building up |
| 261 | some auxiliary data structures and using them to determine which hosts are |
| 262 | eligible to run which jobs, taking into account all the various factors that |
| 263 | affect that. |
| 264 | |
| 265 | In the past this was done with one or two very large, complex database |
| 266 | queries. It has proven much simpler and faster to build these auxiliary |
| 267 | data structures and perform the logic in Python. |
| 268 | """ |
| 269 | def __init__(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 270 | self._metahost_schedulers = metahost_scheduler.get_metahost_schedulers() |
| 271 | |
| 272 | # load site-specific scheduler selected in global_config |
| 273 | site_schedulers_str = global_config.global_config.get_config_value( |
| 274 | scheduler_config.CONFIG_SECTION, 'site_metahost_schedulers', |
| 275 | default='') |
| 276 | site_schedulers = set(site_schedulers_str.split(',')) |
| 277 | for scheduler in get_site_metahost_schedulers(): |
| 278 | if type(scheduler).__name__ in site_schedulers: |
| 279 | # always prepend, so site schedulers take precedence |
| 280 | self._metahost_schedulers = ( |
| 281 | [scheduler] + self._metahost_schedulers) |
| 282 | logging.info('Metahost schedulers: %s', |
| 283 | ', '.join(type(scheduler).__name__ for scheduler |
| 284 | in self._metahost_schedulers)) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 285 | |
| 286 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 287 | def _get_ready_hosts(self): |
| 288 | # avoid any host with a currently active queue entry against it |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 289 | hosts = scheduler_models.Host.fetch( |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 290 | joins='LEFT JOIN afe_host_queue_entries AS active_hqe ' |
| 291 | 'ON (afe_hosts.id = active_hqe.host_id AND ' |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 292 | 'active_hqe.active)', |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 293 | where="active_hqe.host_id IS NULL " |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 294 | "AND NOT afe_hosts.locked " |
| 295 | "AND (afe_hosts.status IS NULL " |
| 296 | "OR afe_hosts.status = 'Ready')") |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 297 | return dict((host.id, host) for host in hosts) |
| 298 | |
| 299 | |
| 300 | @staticmethod |
| 301 | def _get_sql_id_list(id_list): |
| 302 | return ','.join(str(item_id) for item_id in id_list) |
| 303 | |
| 304 | |
| 305 | @classmethod |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 306 | def _get_many2many_dict(cls, query, id_list, flip=False): |
mbligh | 849a0f6 | 2008-08-28 20:12:19 +0000 | [diff] [blame] | 307 | if not id_list: |
| 308 | return {} |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 309 | query %= cls._get_sql_id_list(id_list) |
| 310 | rows = _db.execute(query) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 311 | return cls._process_many2many_dict(rows, flip) |
| 312 | |
| 313 | |
| 314 | @staticmethod |
| 315 | def _process_many2many_dict(rows, flip=False): |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 316 | result = {} |
| 317 | for row in rows: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 318 | left_id, right_id = int(row[0]), int(row[1]) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 319 | if flip: |
| 320 | left_id, right_id = right_id, left_id |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 321 | result.setdefault(left_id, set()).add(right_id) |
| 322 | return result |
| 323 | |
| 324 | |
| 325 | @classmethod |
| 326 | def _get_job_acl_groups(cls, job_ids): |
| 327 | query = """ |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 328 | SELECT afe_jobs.id, afe_acl_groups_users.aclgroup_id |
| 329 | FROM afe_jobs |
| 330 | INNER JOIN afe_users ON afe_users.login = afe_jobs.owner |
| 331 | INNER JOIN afe_acl_groups_users ON |
| 332 | afe_acl_groups_users.user_id = afe_users.id |
| 333 | WHERE afe_jobs.id IN (%s) |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 334 | """ |
| 335 | return cls._get_many2many_dict(query, job_ids) |
| 336 | |
| 337 | |
| 338 | @classmethod |
| 339 | def _get_job_ineligible_hosts(cls, job_ids): |
| 340 | query = """ |
| 341 | SELECT job_id, host_id |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 342 | FROM afe_ineligible_host_queues |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 343 | WHERE job_id IN (%s) |
| 344 | """ |
| 345 | return cls._get_many2many_dict(query, job_ids) |
| 346 | |
| 347 | |
| 348 | @classmethod |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 349 | def _get_job_dependencies(cls, job_ids): |
| 350 | query = """ |
| 351 | SELECT job_id, label_id |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 352 | FROM afe_jobs_dependency_labels |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 353 | WHERE job_id IN (%s) |
| 354 | """ |
| 355 | return cls._get_many2many_dict(query, job_ids) |
| 356 | |
| 357 | |
| 358 | @classmethod |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 359 | def _get_host_acls(cls, host_ids): |
| 360 | query = """ |
showard | d9ac445 | 2009-02-07 02:04:37 +0000 | [diff] [blame] | 361 | SELECT host_id, aclgroup_id |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 362 | FROM afe_acl_groups_hosts |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 363 | WHERE host_id IN (%s) |
| 364 | """ |
| 365 | return cls._get_many2many_dict(query, host_ids) |
| 366 | |
| 367 | |
| 368 | @classmethod |
| 369 | def _get_label_hosts(cls, host_ids): |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 370 | if not host_ids: |
| 371 | return {}, {} |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 372 | query = """ |
| 373 | SELECT label_id, host_id |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 374 | FROM afe_hosts_labels |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 375 | WHERE host_id IN (%s) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 376 | """ % cls._get_sql_id_list(host_ids) |
| 377 | rows = _db.execute(query) |
| 378 | labels_to_hosts = cls._process_many2many_dict(rows) |
| 379 | hosts_to_labels = cls._process_many2many_dict(rows, flip=True) |
| 380 | return labels_to_hosts, hosts_to_labels |
| 381 | |
| 382 | |
| 383 | @classmethod |
| 384 | def _get_labels(cls): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 385 | return dict((label.id, label) for label |
| 386 | in scheduler_models.Label.fetch()) |
| 387 | |
| 388 | |
| 389 | def recovery_on_startup(self): |
| 390 | for metahost_scheduler in self._metahost_schedulers: |
| 391 | metahost_scheduler.recovery_on_startup() |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 392 | |
| 393 | |
| 394 | def refresh(self, pending_queue_entries): |
| 395 | self._hosts_available = self._get_ready_hosts() |
| 396 | |
| 397 | relevant_jobs = [queue_entry.job_id |
| 398 | for queue_entry in pending_queue_entries] |
| 399 | self._job_acls = self._get_job_acl_groups(relevant_jobs) |
| 400 | self._ineligible_hosts = self._get_job_ineligible_hosts(relevant_jobs) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 401 | self._job_dependencies = self._get_job_dependencies(relevant_jobs) |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 402 | |
| 403 | host_ids = self._hosts_available.keys() |
| 404 | self._host_acls = self._get_host_acls(host_ids) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 405 | self._label_hosts, self._host_labels = self._get_label_hosts(host_ids) |
| 406 | |
| 407 | self._labels = self._get_labels() |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 408 | |
jamesren | e21bf41 | 2010-02-26 02:30:07 +0000 | [diff] [blame] | 409 | |
| 410 | def tick(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 411 | for metahost_scheduler in self._metahost_schedulers: |
| 412 | metahost_scheduler.tick() |
| 413 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 414 | |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 415 | def hosts_in_label(self, label_id): |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 416 | return set(self._label_hosts.get(label_id, ())) |
| 417 | |
| 418 | |
| 419 | def remove_host_from_label(self, host_id, label_id): |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 420 | self._label_hosts[label_id].remove(host_id) |
| 421 | |
| 422 | |
| 423 | def pop_host(self, host_id): |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 424 | return self._hosts_available.pop(host_id) |
| 425 | |
| 426 | |
| 427 | def ineligible_hosts_for_entry(self, queue_entry): |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 428 | return set(self._ineligible_hosts.get(queue_entry.job_id, ())) |
| 429 | |
| 430 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 431 | def _is_acl_accessible(self, host_id, queue_entry): |
| 432 | job_acls = self._job_acls.get(queue_entry.job_id, set()) |
| 433 | host_acls = self._host_acls.get(host_id, set()) |
| 434 | return len(host_acls.intersection(job_acls)) > 0 |
| 435 | |
| 436 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 437 | def _check_job_dependencies(self, job_dependencies, host_labels): |
| 438 | missing = job_dependencies - host_labels |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 439 | return len(missing) == 0 |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 440 | |
| 441 | |
| 442 | def _check_only_if_needed_labels(self, job_dependencies, host_labels, |
| 443 | queue_entry): |
showard | ade14e2 | 2009-01-26 22:38:32 +0000 | [diff] [blame] | 444 | if not queue_entry.meta_host: |
| 445 | # bypass only_if_needed labels when a specific host is selected |
| 446 | return True |
| 447 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 448 | for label_id in host_labels: |
| 449 | label = self._labels[label_id] |
| 450 | if not label.only_if_needed: |
| 451 | # we don't care about non-only_if_needed labels |
| 452 | continue |
| 453 | if queue_entry.meta_host == label_id: |
| 454 | # if the label was requested in a metahost it's OK |
| 455 | continue |
| 456 | if label_id not in job_dependencies: |
| 457 | return False |
| 458 | return True |
| 459 | |
| 460 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 461 | def _check_atomic_group_labels(self, host_labels, queue_entry): |
| 462 | """ |
| 463 | Determine if the given HostQueueEntry's atomic group settings are okay |
| 464 | to schedule on a host with the given labels. |
| 465 | |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 466 | @param host_labels: A list of label ids that the host has. |
| 467 | @param queue_entry: The HostQueueEntry being considered for the host. |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 468 | |
| 469 | @returns True if atomic group settings are okay, False otherwise. |
| 470 | """ |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 471 | return (self._get_host_atomic_group_id(host_labels, queue_entry) == |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 472 | queue_entry.atomic_group_id) |
| 473 | |
| 474 | |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 475 | def _get_host_atomic_group_id(self, host_labels, queue_entry=None): |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 476 | """ |
| 477 | Return the atomic group label id for a host with the given set of |
| 478 | labels if any, or None otherwise. Raises an exception if more than |
| 479 | one atomic group are found in the set of labels. |
| 480 | |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 481 | @param host_labels: A list of label ids that the host has. |
| 482 | @param queue_entry: The HostQueueEntry we're testing. Only used for |
| 483 | extra info in a potential logged error message. |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 484 | |
| 485 | @returns The id of the atomic group found on a label in host_labels |
| 486 | or None if no atomic group label is found. |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 487 | """ |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 488 | atomic_labels = [self._labels[label_id] for label_id in host_labels |
| 489 | if self._labels[label_id].atomic_group_id is not None] |
| 490 | atomic_ids = set(label.atomic_group_id for label in atomic_labels) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 491 | if not atomic_ids: |
| 492 | return None |
| 493 | if len(atomic_ids) > 1: |
showard | 6157c63 | 2009-07-06 20:19:31 +0000 | [diff] [blame] | 494 | logging.error('More than one Atomic Group on HQE "%s" via: %r', |
| 495 | queue_entry, atomic_labels) |
| 496 | return atomic_ids.pop() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 497 | |
| 498 | |
| 499 | def _get_atomic_group_labels(self, atomic_group_id): |
| 500 | """ |
| 501 | Lookup the label ids that an atomic_group is associated with. |
| 502 | |
| 503 | @param atomic_group_id - The id of the AtomicGroup to look up. |
| 504 | |
| 505 | @returns A generator yeilding Label ids for this atomic group. |
| 506 | """ |
| 507 | return (id for id, label in self._labels.iteritems() |
| 508 | if label.atomic_group_id == atomic_group_id |
| 509 | and not label.invalid) |
| 510 | |
| 511 | |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 512 | def _get_eligible_host_ids_in_group(self, group_hosts, queue_entry): |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 513 | """ |
| 514 | @param group_hosts - A sequence of Host ids to test for usability |
| 515 | and eligibility against the Job associated with queue_entry. |
| 516 | @param queue_entry - The HostQueueEntry that these hosts are being |
| 517 | tested for eligibility against. |
| 518 | |
| 519 | @returns A subset of group_hosts Host ids that are eligible for the |
| 520 | supplied queue_entry. |
| 521 | """ |
| 522 | return set(host_id for host_id in group_hosts |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 523 | if self.is_host_usable(host_id) |
| 524 | and self.is_host_eligible_for_job(host_id, queue_entry)) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 525 | |
| 526 | |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 527 | def is_host_eligible_for_job(self, host_id, queue_entry): |
showard | 2924b0a | 2009-06-18 23:16:15 +0000 | [diff] [blame] | 528 | if self._is_host_invalid(host_id): |
| 529 | # if an invalid host is scheduled for a job, it's a one-time host |
| 530 | # and it therefore bypasses eligibility checks. note this can only |
| 531 | # happen for non-metahosts, because invalid hosts have their label |
| 532 | # relationships cleared. |
| 533 | return True |
| 534 | |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 535 | job_dependencies = self._job_dependencies.get(queue_entry.job_id, set()) |
| 536 | host_labels = self._host_labels.get(host_id, set()) |
mbligh | c993bee | 2008-10-03 03:42:34 +0000 | [diff] [blame] | 537 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 538 | return (self._is_acl_accessible(host_id, queue_entry) and |
| 539 | self._check_job_dependencies(job_dependencies, host_labels) and |
| 540 | self._check_only_if_needed_labels( |
| 541 | job_dependencies, host_labels, queue_entry) and |
| 542 | self._check_atomic_group_labels(host_labels, queue_entry)) |
showard | 989f25d | 2008-10-01 11:38:11 +0000 | [diff] [blame] | 543 | |
| 544 | |
showard | 2924b0a | 2009-06-18 23:16:15 +0000 | [diff] [blame] | 545 | def _is_host_invalid(self, host_id): |
| 546 | host_object = self._hosts_available.get(host_id, None) |
| 547 | return host_object and host_object.invalid |
| 548 | |
| 549 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 550 | def _schedule_non_metahost(self, queue_entry): |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 551 | 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] | 552 | return None |
| 553 | return self._hosts_available.pop(queue_entry.host_id, None) |
| 554 | |
| 555 | |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 556 | def is_host_usable(self, host_id): |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 557 | if host_id not in self._hosts_available: |
| 558 | # host was already used during this scheduling cycle |
| 559 | return False |
| 560 | if self._hosts_available[host_id].invalid: |
| 561 | # Invalid hosts cannot be used for metahosts. They're included in |
| 562 | # the original query because they can be used by non-metahosts. |
| 563 | return False |
| 564 | return True |
| 565 | |
| 566 | |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 567 | def schedule_entry(self, queue_entry): |
| 568 | if queue_entry.host_id is not None: |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 569 | return self._schedule_non_metahost(queue_entry) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 570 | |
| 571 | for scheduler in self._metahost_schedulers: |
| 572 | if scheduler.can_schedule_metahost(queue_entry): |
| 573 | scheduler.schedule_metahost(queue_entry, self) |
| 574 | return None |
| 575 | |
| 576 | raise SchedulerError('No metahost scheduler to handle %s' % queue_entry) |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 577 | |
| 578 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 579 | def find_eligible_atomic_group(self, queue_entry): |
| 580 | """ |
| 581 | Given an atomic group host queue entry, locate an appropriate group |
| 582 | of hosts for the associated job to run on. |
| 583 | |
| 584 | The caller is responsible for creating new HQEs for the additional |
| 585 | hosts returned in order to run the actual job on them. |
| 586 | |
| 587 | @returns A list of Host instances in a ready state to satisfy this |
| 588 | atomic group scheduling. Hosts will all belong to the same |
| 589 | atomic group label as specified by the queue_entry. |
| 590 | An empty list will be returned if no suitable atomic |
| 591 | group could be found. |
| 592 | |
| 593 | TODO(gps): what is responsible for kicking off any attempted repairs on |
| 594 | a group of hosts? not this function, but something needs to. We do |
| 595 | not communicate that reason for returning [] outside of here... |
| 596 | For now, we'll just be unschedulable if enough hosts within one group |
| 597 | enter Repair Failed state. |
| 598 | """ |
| 599 | assert queue_entry.atomic_group_id is not None |
| 600 | job = queue_entry.job |
| 601 | assert job.synch_count and job.synch_count > 0 |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 602 | atomic_group = queue_entry.atomic_group |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 603 | if job.synch_count > atomic_group.max_number_of_machines: |
| 604 | # Such a Job and HostQueueEntry should never be possible to |
| 605 | # create using the frontend. Regardless, we can't process it. |
| 606 | # Abort it immediately and log an error on the scheduler. |
| 607 | queue_entry.set_status(models.HostQueueEntry.Status.ABORTED) |
showard | 7629f14 | 2009-03-27 21:02:02 +0000 | [diff] [blame] | 608 | logging.error( |
| 609 | 'Error: job %d synch_count=%d > requested atomic_group %d ' |
| 610 | 'max_number_of_machines=%d. Aborted host_queue_entry %d.', |
| 611 | job.id, job.synch_count, atomic_group.id, |
| 612 | atomic_group.max_number_of_machines, queue_entry.id) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 613 | return [] |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 614 | hosts_in_label = self.hosts_in_label(queue_entry.meta_host) |
| 615 | ineligible_host_ids = self.ineligible_hosts_for_entry(queue_entry) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 616 | |
| 617 | # Look in each label associated with atomic_group until we find one with |
| 618 | # enough hosts to satisfy the job. |
| 619 | for atomic_label_id in self._get_atomic_group_labels(atomic_group.id): |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 620 | group_hosts = set(self.hosts_in_label(atomic_label_id)) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 621 | if queue_entry.meta_host is not None: |
| 622 | # If we have a metahost label, only allow its hosts. |
| 623 | group_hosts.intersection_update(hosts_in_label) |
| 624 | group_hosts -= ineligible_host_ids |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 625 | eligible_host_ids_in_group = self._get_eligible_host_ids_in_group( |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 626 | group_hosts, queue_entry) |
| 627 | |
| 628 | # Job.synch_count is treated as "minimum synch count" when |
| 629 | # scheduling for an atomic group of hosts. The atomic group |
| 630 | # number of machines is the maximum to pick out of a single |
| 631 | # atomic group label for scheduling at one time. |
| 632 | min_hosts = job.synch_count |
| 633 | max_hosts = atomic_group.max_number_of_machines |
| 634 | |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 635 | if len(eligible_host_ids_in_group) < min_hosts: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 636 | # Not enough eligible hosts in this atomic group label. |
| 637 | continue |
| 638 | |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 639 | eligible_hosts_in_group = [self._hosts_available[id] |
| 640 | for id in eligible_host_ids_in_group] |
showard | ef51921 | 2009-05-08 02:29:53 +0000 | [diff] [blame] | 641 | # So that they show up in a sane order when viewing the job. |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 642 | eligible_hosts_in_group.sort(cmp=scheduler_models.Host.cmp_for_sort) |
showard | ef51921 | 2009-05-08 02:29:53 +0000 | [diff] [blame] | 643 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 644 | # Limit ourselves to scheduling the atomic group size. |
| 645 | if len(eligible_hosts_in_group) > max_hosts: |
showard | ef51921 | 2009-05-08 02:29:53 +0000 | [diff] [blame] | 646 | eligible_hosts_in_group = eligible_hosts_in_group[:max_hosts] |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 647 | |
| 648 | # Remove the selected hosts from our cached internal state |
| 649 | # of available hosts in order to return the Host objects. |
| 650 | host_list = [] |
showard | 54c1ea9 | 2009-05-20 00:32:58 +0000 | [diff] [blame] | 651 | for host in eligible_hosts_in_group: |
| 652 | hosts_in_label.discard(host.id) |
| 653 | self._hosts_available.pop(host.id) |
| 654 | host_list.append(host) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 655 | return host_list |
| 656 | |
| 657 | return [] |
| 658 | |
| 659 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 660 | class Dispatcher(object): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 661 | def __init__(self): |
| 662 | self._agents = [] |
showard | 3bb499f | 2008-07-03 19:42:20 +0000 | [diff] [blame] | 663 | self._last_clean_time = time.time() |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 664 | self._host_scheduler = HostScheduler() |
mbligh | f3294cc | 2009-04-08 21:17:38 +0000 | [diff] [blame] | 665 | user_cleanup_time = scheduler_config.config.clean_interval |
| 666 | self._periodic_cleanup = monitor_db_cleanup.UserCleanup( |
| 667 | _db, user_cleanup_time) |
| 668 | self._24hr_upkeep = monitor_db_cleanup.TwentyFourHourUpkeep(_db) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 669 | self._host_agents = {} |
| 670 | self._queue_entry_agents = {} |
showard | f13a9e2 | 2009-12-18 22:54:09 +0000 | [diff] [blame] | 671 | self._tick_count = 0 |
| 672 | self._last_garbage_stats_time = time.time() |
| 673 | self._seconds_between_garbage_stats = 60 * ( |
| 674 | global_config.global_config.get_config_value( |
| 675 | scheduler_config.CONFIG_SECTION, |
| 676 | 'gc_stats_interval_mins', type=int, default=6*60)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 677 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 678 | |
showard | 915958d | 2009-04-22 21:00:58 +0000 | [diff] [blame] | 679 | def initialize(self, recover_hosts=True): |
| 680 | self._periodic_cleanup.initialize() |
| 681 | self._24hr_upkeep.initialize() |
| 682 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 683 | # always recover processes |
| 684 | self._recover_processes() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 685 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 686 | if recover_hosts: |
| 687 | self._recover_hosts() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 688 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 689 | self._host_scheduler.recovery_on_startup() |
| 690 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 691 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 692 | def tick(self): |
showard | f13a9e2 | 2009-12-18 22:54:09 +0000 | [diff] [blame] | 693 | self._garbage_collection() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 694 | _drone_manager.refresh() |
mbligh | f3294cc | 2009-04-08 21:17:38 +0000 | [diff] [blame] | 695 | self._run_cleanup() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 696 | self._find_aborting() |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 697 | self._process_recurring_runs() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 698 | self._schedule_delay_tasks() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 699 | self._schedule_running_host_queue_entries() |
| 700 | self._schedule_special_tasks() |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 701 | self._schedule_new_jobs() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 702 | self._handle_agents() |
jamesren | e21bf41 | 2010-02-26 02:30:07 +0000 | [diff] [blame] | 703 | self._host_scheduler.tick() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 704 | _drone_manager.execute_actions() |
| 705 | email_manager.manager.send_queued_emails() |
showard | 402934a | 2009-12-21 22:20:47 +0000 | [diff] [blame] | 706 | django.db.reset_queries() |
showard | f13a9e2 | 2009-12-18 22:54:09 +0000 | [diff] [blame] | 707 | self._tick_count += 1 |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 708 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 709 | |
mbligh | f3294cc | 2009-04-08 21:17:38 +0000 | [diff] [blame] | 710 | def _run_cleanup(self): |
| 711 | self._periodic_cleanup.run_cleanup_maybe() |
| 712 | self._24hr_upkeep.run_cleanup_maybe() |
showard | a3ab0d5 | 2008-11-03 19:03:47 +0000 | [diff] [blame] | 713 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 714 | |
showard | f13a9e2 | 2009-12-18 22:54:09 +0000 | [diff] [blame] | 715 | def _garbage_collection(self): |
| 716 | threshold_time = time.time() - self._seconds_between_garbage_stats |
| 717 | if threshold_time < self._last_garbage_stats_time: |
| 718 | # Don't generate these reports very often. |
| 719 | return |
| 720 | |
| 721 | self._last_garbage_stats_time = time.time() |
| 722 | # Force a full level 0 collection (because we can, it doesn't hurt |
| 723 | # at this interval). |
| 724 | gc.collect() |
| 725 | logging.info('Logging garbage collector stats on tick %d.', |
| 726 | self._tick_count) |
| 727 | gc_stats._log_garbage_collector_stats() |
| 728 | |
| 729 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 730 | def _register_agent_for_ids(self, agent_dict, object_ids, agent): |
| 731 | for object_id in object_ids: |
| 732 | agent_dict.setdefault(object_id, set()).add(agent) |
| 733 | |
| 734 | |
| 735 | def _unregister_agent_for_ids(self, agent_dict, object_ids, agent): |
| 736 | for object_id in object_ids: |
| 737 | assert object_id in agent_dict |
| 738 | agent_dict[object_id].remove(agent) |
| 739 | |
| 740 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 741 | def add_agent_task(self, agent_task): |
| 742 | agent = Agent(agent_task) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 743 | self._agents.append(agent) |
| 744 | agent.dispatcher = self |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 745 | self._register_agent_for_ids(self._host_agents, agent.host_ids, agent) |
| 746 | self._register_agent_for_ids(self._queue_entry_agents, |
| 747 | agent.queue_entry_ids, agent) |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 748 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 749 | |
| 750 | def get_agents_for_entry(self, queue_entry): |
| 751 | """ |
| 752 | Find agents corresponding to the specified queue_entry. |
| 753 | """ |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 754 | return list(self._queue_entry_agents.get(queue_entry.id, set())) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 755 | |
| 756 | |
| 757 | def host_has_agent(self, host): |
| 758 | """ |
| 759 | Determine if there is currently an Agent present using this host. |
| 760 | """ |
| 761 | return bool(self._host_agents.get(host.id, None)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 762 | |
| 763 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 764 | def remove_agent(self, agent): |
| 765 | self._agents.remove(agent) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 766 | self._unregister_agent_for_ids(self._host_agents, agent.host_ids, |
| 767 | agent) |
| 768 | self._unregister_agent_for_ids(self._queue_entry_agents, |
| 769 | agent.queue_entry_ids, agent) |
showard | ec11316 | 2008-05-08 00:52:49 +0000 | [diff] [blame] | 770 | |
| 771 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 772 | def _host_has_scheduled_special_task(self, host): |
| 773 | return bool(models.SpecialTask.objects.filter(host__id=host.id, |
| 774 | is_active=False, |
| 775 | is_complete=False)) |
| 776 | |
| 777 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 778 | def _recover_processes(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 779 | agent_tasks = self._create_recovery_agent_tasks() |
| 780 | self._register_pidfiles(agent_tasks) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 781 | _drone_manager.refresh() |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 782 | self._recover_tasks(agent_tasks) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 783 | self._recover_pending_entries() |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 784 | self._check_for_unrecovered_verifying_entries() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 785 | self._reverify_remaining_hosts() |
| 786 | # reinitialize drones after killing orphaned processes, since they can |
| 787 | # leave around files when they die |
| 788 | _drone_manager.execute_actions() |
| 789 | _drone_manager.reinitialize_drones() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 790 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 791 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 792 | def _create_recovery_agent_tasks(self): |
| 793 | return (self._get_queue_entry_agent_tasks() |
| 794 | + self._get_special_task_agent_tasks(is_active=True)) |
| 795 | |
| 796 | |
| 797 | def _get_queue_entry_agent_tasks(self): |
| 798 | # host queue entry statuses handled directly by AgentTasks (Verifying is |
| 799 | # handled through SpecialTasks, so is not listed here) |
| 800 | statuses = (models.HostQueueEntry.Status.STARTING, |
| 801 | models.HostQueueEntry.Status.RUNNING, |
| 802 | models.HostQueueEntry.Status.GATHERING, |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 803 | models.HostQueueEntry.Status.PARSING, |
| 804 | models.HostQueueEntry.Status.ARCHIVING) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 805 | status_list = ','.join("'%s'" % status for status in statuses) |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 806 | queue_entries = scheduler_models.HostQueueEntry.fetch( |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 807 | where='status IN (%s)' % status_list) |
| 808 | |
| 809 | agent_tasks = [] |
| 810 | used_queue_entries = set() |
| 811 | for entry in queue_entries: |
| 812 | if self.get_agents_for_entry(entry): |
| 813 | # already being handled |
| 814 | continue |
| 815 | if entry in used_queue_entries: |
| 816 | # already picked up by a synchronous job |
| 817 | continue |
| 818 | agent_task = self._get_agent_task_for_queue_entry(entry) |
| 819 | agent_tasks.append(agent_task) |
| 820 | used_queue_entries.update(agent_task.queue_entries) |
| 821 | return agent_tasks |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 822 | |
| 823 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 824 | def _get_special_task_agent_tasks(self, is_active=False): |
| 825 | special_tasks = models.SpecialTask.objects.filter( |
| 826 | is_active=is_active, is_complete=False) |
| 827 | return [self._get_agent_task_for_special_task(task) |
| 828 | for task in special_tasks] |
| 829 | |
| 830 | |
| 831 | def _get_agent_task_for_queue_entry(self, queue_entry): |
| 832 | """ |
| 833 | Construct an AgentTask instance for the given active HostQueueEntry, |
| 834 | if one can currently run it. |
| 835 | @param queue_entry: a HostQueueEntry |
| 836 | @returns an AgentTask to run the queue entry |
| 837 | """ |
| 838 | task_entries = queue_entry.job.get_group_entries(queue_entry) |
| 839 | self._check_for_duplicate_host_entries(task_entries) |
| 840 | |
| 841 | if queue_entry.status in (models.HostQueueEntry.Status.STARTING, |
| 842 | models.HostQueueEntry.Status.RUNNING): |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 843 | if queue_entry.is_hostless(): |
| 844 | return HostlessQueueTask(queue_entry=queue_entry) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 845 | return QueueTask(queue_entries=task_entries) |
| 846 | if queue_entry.status == models.HostQueueEntry.Status.GATHERING: |
| 847 | return GatherLogsTask(queue_entries=task_entries) |
| 848 | if queue_entry.status == models.HostQueueEntry.Status.PARSING: |
| 849 | return FinalReparseTask(queue_entries=task_entries) |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 850 | if queue_entry.status == models.HostQueueEntry.Status.ARCHIVING: |
| 851 | return ArchiveResultsTask(queue_entries=task_entries) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 852 | |
| 853 | raise SchedulerError('_get_agent_task_for_queue_entry got entry with ' |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 854 | 'invalid status %s: %s' |
| 855 | % (queue_entry.status, queue_entry)) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 856 | |
| 857 | |
| 858 | def _check_for_duplicate_host_entries(self, task_entries): |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 859 | non_host_statuses = (models.HostQueueEntry.Status.PARSING, |
| 860 | models.HostQueueEntry.Status.ARCHIVING) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 861 | for task_entry in task_entries: |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 862 | using_host = (task_entry.host is not None |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 863 | and task_entry.status not in non_host_statuses) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 864 | if using_host: |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 865 | self._assert_host_has_no_agent(task_entry) |
| 866 | |
| 867 | |
| 868 | def _assert_host_has_no_agent(self, entry): |
| 869 | """ |
| 870 | @param entry: a HostQueueEntry or a SpecialTask |
| 871 | """ |
| 872 | if self.host_has_agent(entry.host): |
| 873 | agent = tuple(self._host_agents.get(entry.host.id))[0] |
| 874 | raise SchedulerError( |
| 875 | 'While scheduling %s, host %s already has a host agent %s' |
| 876 | % (entry, entry.host, agent.task)) |
| 877 | |
| 878 | |
| 879 | def _get_agent_task_for_special_task(self, special_task): |
| 880 | """ |
| 881 | Construct an AgentTask class to run the given SpecialTask and add it |
| 882 | to this dispatcher. |
| 883 | @param special_task: a models.SpecialTask instance |
| 884 | @returns an AgentTask to run this SpecialTask |
| 885 | """ |
| 886 | self._assert_host_has_no_agent(special_task) |
| 887 | |
| 888 | special_agent_task_classes = (CleanupTask, VerifyTask, RepairTask) |
| 889 | for agent_task_class in special_agent_task_classes: |
| 890 | if agent_task_class.TASK_TYPE == special_task.task: |
| 891 | return agent_task_class(task=special_task) |
| 892 | |
| 893 | raise SchedulerError('No AgentTask class for task', str(special_task)) |
| 894 | |
| 895 | |
| 896 | def _register_pidfiles(self, agent_tasks): |
| 897 | for agent_task in agent_tasks: |
| 898 | agent_task.register_necessary_pidfiles() |
| 899 | |
| 900 | |
| 901 | def _recover_tasks(self, agent_tasks): |
| 902 | orphans = _drone_manager.get_orphaned_autoserv_processes() |
| 903 | |
| 904 | for agent_task in agent_tasks: |
| 905 | agent_task.recover() |
| 906 | if agent_task.monitor and agent_task.monitor.has_process(): |
| 907 | orphans.discard(agent_task.monitor.get_process()) |
| 908 | self.add_agent_task(agent_task) |
| 909 | |
| 910 | self._check_for_remaining_orphan_processes(orphans) |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 911 | |
| 912 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 913 | def _get_unassigned_entries(self, status): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 914 | for entry in scheduler_models.HostQueueEntry.fetch(where="status = '%s'" |
| 915 | % status): |
showard | 0db3d43 | 2009-10-12 20:29:15 +0000 | [diff] [blame] | 916 | if entry.status == status and not self.get_agents_for_entry(entry): |
| 917 | # The status can change during iteration, e.g., if job.run() |
| 918 | # sets a group of queue entries to Starting |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 919 | yield entry |
| 920 | |
| 921 | |
showard | 6878e8b | 2009-07-20 22:37:45 +0000 | [diff] [blame] | 922 | def _check_for_remaining_orphan_processes(self, orphans): |
| 923 | if not orphans: |
| 924 | return |
| 925 | subject = 'Unrecovered orphan autoserv processes remain' |
| 926 | message = '\n'.join(str(process) for process in orphans) |
| 927 | email_manager.manager.enqueue_notify_email(subject, message) |
mbligh | 5fa9e11 | 2009-08-03 16:46:06 +0000 | [diff] [blame] | 928 | |
| 929 | die_on_orphans = global_config.global_config.get_config_value( |
| 930 | scheduler_config.CONFIG_SECTION, 'die_on_orphans', type=bool) |
| 931 | |
| 932 | if die_on_orphans: |
| 933 | raise RuntimeError(subject + '\n' + message) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 934 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 935 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 936 | def _recover_pending_entries(self): |
| 937 | for entry in self._get_unassigned_entries( |
| 938 | models.HostQueueEntry.Status.PENDING): |
showard | 5682407 | 2009-10-12 20:30:21 +0000 | [diff] [blame] | 939 | logging.info('Recovering Pending entry %s', entry) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 940 | entry.on_pending() |
| 941 | |
| 942 | |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 943 | def _check_for_unrecovered_verifying_entries(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 944 | queue_entries = scheduler_models.HostQueueEntry.fetch( |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 945 | where='status = "%s"' % models.HostQueueEntry.Status.VERIFYING) |
| 946 | unrecovered_hqes = [] |
| 947 | for queue_entry in queue_entries: |
| 948 | special_tasks = models.SpecialTask.objects.filter( |
| 949 | task__in=(models.SpecialTask.Task.CLEANUP, |
| 950 | models.SpecialTask.Task.VERIFY), |
| 951 | queue_entry__id=queue_entry.id, |
| 952 | is_complete=False) |
| 953 | if special_tasks.count() == 0: |
| 954 | unrecovered_hqes.append(queue_entry) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 955 | |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 956 | if unrecovered_hqes: |
| 957 | message = '\n'.join(str(hqe) for hqe in unrecovered_hqes) |
showard | e8e3707 | 2009-08-20 23:31:30 +0000 | [diff] [blame] | 958 | raise SchedulerError( |
showard | 37757f3 | 2009-10-19 18:34:24 +0000 | [diff] [blame] | 959 | '%d unrecovered verifying host queue entries:\n%s' % |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 960 | (len(unrecovered_hqes), message)) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 961 | |
| 962 | |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 963 | def _get_prioritized_special_tasks(self): |
| 964 | """ |
| 965 | Returns all queued SpecialTasks prioritized for repair first, then |
| 966 | cleanup, then verify. |
| 967 | """ |
| 968 | queued_tasks = models.SpecialTask.objects.filter(is_active=False, |
| 969 | is_complete=False, |
| 970 | host__locked=False) |
| 971 | # exclude hosts with active queue entries unless the SpecialTask is for |
| 972 | # that queue entry |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 973 | queued_tasks = models.SpecialTask.objects.add_join( |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 974 | queued_tasks, 'afe_host_queue_entries', 'host_id', |
| 975 | join_condition='afe_host_queue_entries.active', |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 976 | join_from_key='host_id', force_left_join=True) |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 977 | queued_tasks = queued_tasks.extra( |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 978 | where=['(afe_host_queue_entries.id IS NULL OR ' |
| 979 | 'afe_host_queue_entries.id = ' |
| 980 | 'afe_special_tasks.queue_entry_id)']) |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 981 | |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 982 | # reorder tasks by priority |
| 983 | task_priority_order = [models.SpecialTask.Task.REPAIR, |
| 984 | models.SpecialTask.Task.CLEANUP, |
| 985 | models.SpecialTask.Task.VERIFY] |
| 986 | def task_priority_key(task): |
| 987 | return task_priority_order.index(task.task) |
| 988 | return sorted(queued_tasks, key=task_priority_key) |
| 989 | |
| 990 | |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 991 | def _schedule_special_tasks(self): |
| 992 | """ |
| 993 | Execute queued SpecialTasks that are ready to run on idle hosts. |
| 994 | """ |
| 995 | for task in self._get_prioritized_special_tasks(): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 996 | if self.host_has_agent(task.host): |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 997 | continue |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 998 | self.add_agent_task(self._get_agent_task_for_special_task(task)) |
showard | 1ff7b2e | 2009-05-15 23:17:18 +0000 | [diff] [blame] | 999 | |
| 1000 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1001 | def _reverify_remaining_hosts(self): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1002 | # recover active hosts that have not yet been recovered, although this |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1003 | # should never happen |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1004 | message = ('Recovering active host %s - this probably indicates a ' |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1005 | 'scheduler bug') |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1006 | self._reverify_hosts_where( |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1007 | "status IN ('Repairing', 'Verifying', 'Cleaning')", |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1008 | print_message=message) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1009 | |
| 1010 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1011 | def _reverify_hosts_where(self, where, |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1012 | print_message='Reverifying host %s'): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1013 | full_where='locked = 0 AND invalid = 0 AND ' + where |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1014 | for host in scheduler_models.Host.fetch(where=full_where): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1015 | if self.host_has_agent(host): |
| 1016 | # host has already been recovered in some way |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1017 | continue |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1018 | if self._host_has_scheduled_special_task(host): |
| 1019 | # host will have a special task scheduled on the next cycle |
| 1020 | continue |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1021 | if print_message: |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 1022 | logging.info(print_message, host.hostname) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1023 | models.SpecialTask.objects.create( |
| 1024 | task=models.SpecialTask.Task.CLEANUP, |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1025 | host=models.Host.objects.get(id=host.id)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1026 | |
| 1027 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1028 | def _recover_hosts(self): |
| 1029 | # recover "Repair Failed" hosts |
| 1030 | message = 'Reverifying dead host %s' |
| 1031 | self._reverify_hosts_where("status = 'Repair Failed'", |
| 1032 | print_message=message) |
mbligh | 62ba2ed | 2008-04-30 17:09:25 +0000 | [diff] [blame] | 1033 | |
| 1034 | |
showard | 04c82c5 | 2008-05-29 19:38:12 +0000 | [diff] [blame] | 1035 | |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 1036 | def _get_pending_queue_entries(self): |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 1037 | # prioritize by job priority, then non-metahost over metahost, then FIFO |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1038 | return list(scheduler_models.HostQueueEntry.fetch( |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 1039 | joins='INNER JOIN afe_jobs ON (job_id=afe_jobs.id)', |
showard | ac9ce22 | 2008-12-03 18:19:44 +0000 | [diff] [blame] | 1040 | where='NOT complete AND NOT active AND status="Queued"', |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 1041 | order_by='afe_jobs.priority DESC, meta_host, job_id')) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1042 | |
| 1043 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 1044 | def _refresh_pending_queue_entries(self): |
| 1045 | """ |
| 1046 | Lookup the pending HostQueueEntries and call our HostScheduler |
| 1047 | refresh() method given that list. Return the list. |
| 1048 | |
| 1049 | @returns A list of pending HostQueueEntries sorted in priority order. |
| 1050 | """ |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 1051 | queue_entries = self._get_pending_queue_entries() |
| 1052 | if not queue_entries: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 1053 | return [] |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 1054 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 1055 | self._host_scheduler.refresh(queue_entries) |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 1056 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 1057 | return queue_entries |
| 1058 | |
| 1059 | |
| 1060 | def _schedule_atomic_group(self, queue_entry): |
| 1061 | """ |
| 1062 | Schedule the given queue_entry on an atomic group of hosts. |
| 1063 | |
| 1064 | Returns immediately if there are insufficient available hosts. |
| 1065 | |
| 1066 | Creates new HostQueueEntries based off of queue_entry for the |
| 1067 | scheduled hosts and starts them all running. |
| 1068 | """ |
| 1069 | # This is a virtual host queue entry representing an entire |
| 1070 | # atomic group, find a group and schedule their hosts. |
| 1071 | group_hosts = self._host_scheduler.find_eligible_atomic_group( |
| 1072 | queue_entry) |
| 1073 | if not group_hosts: |
| 1074 | return |
showard | cbe6f94 | 2009-06-17 19:33:49 +0000 | [diff] [blame] | 1075 | |
| 1076 | logging.info('Expanding atomic group entry %s with hosts %s', |
| 1077 | queue_entry, |
| 1078 | ', '.join(host.hostname for host in group_hosts)) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 1079 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 1080 | for assigned_host in group_hosts[1:]: |
| 1081 | # Create a new HQE for every additional assigned_host. |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1082 | new_hqe = scheduler_models.HostQueueEntry.clone(queue_entry) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 1083 | new_hqe.save() |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 1084 | new_hqe.set_host(assigned_host) |
| 1085 | self._run_queue_entry(new_hqe) |
| 1086 | |
| 1087 | # The first assigned host uses the original HostQueueEntry |
| 1088 | queue_entry.set_host(group_hosts[0]) |
| 1089 | self._run_queue_entry(queue_entry) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 1090 | |
| 1091 | |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1092 | def _schedule_hostless_job(self, queue_entry): |
| 1093 | self.add_agent_task(HostlessQueueTask(queue_entry)) |
jamesren | 47bd737 | 2010-03-13 00:58:17 +0000 | [diff] [blame] | 1094 | queue_entry.set_status(models.HostQueueEntry.Status.STARTING) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1095 | |
| 1096 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 1097 | def _schedule_new_jobs(self): |
| 1098 | queue_entries = self._refresh_pending_queue_entries() |
| 1099 | if not queue_entries: |
| 1100 | return |
| 1101 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 1102 | for queue_entry in queue_entries: |
showard | e55955f | 2009-10-07 20:48:58 +0000 | [diff] [blame] | 1103 | is_unassigned_atomic_group = ( |
| 1104 | queue_entry.atomic_group_id is not None |
| 1105 | and queue_entry.host_id is None) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 1106 | |
| 1107 | if queue_entry.is_hostless(): |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1108 | self._schedule_hostless_job(queue_entry) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 1109 | elif is_unassigned_atomic_group: |
| 1110 | self._schedule_atomic_group(queue_entry) |
showard | e55955f | 2009-10-07 20:48:58 +0000 | [diff] [blame] | 1111 | else: |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 1112 | assigned_host = self._host_scheduler.schedule_entry(queue_entry) |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 1113 | if assigned_host and not self.host_has_agent(assigned_host): |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 1114 | assert assigned_host.id == queue_entry.host_id |
| 1115 | self._run_queue_entry(queue_entry) |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 1116 | |
| 1117 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1118 | def _schedule_running_host_queue_entries(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1119 | for agent_task in self._get_queue_entry_agent_tasks(): |
| 1120 | self.add_agent_task(agent_task) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1121 | |
| 1122 | |
| 1123 | def _schedule_delay_tasks(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1124 | for entry in scheduler_models.HostQueueEntry.fetch( |
| 1125 | where='status = "%s"' % models.HostQueueEntry.Status.WAITING): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1126 | task = entry.job.schedule_delayed_callback_task(entry) |
| 1127 | if task: |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1128 | self.add_agent_task(task) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1129 | |
| 1130 | |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 1131 | def _run_queue_entry(self, queue_entry): |
| 1132 | queue_entry.schedule_pre_job_tasks() |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 1133 | |
| 1134 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1135 | def _find_aborting(self): |
jamesren | e7c65cb | 2010-06-08 20:38:10 +0000 | [diff] [blame] | 1136 | jobs_to_stop = set() |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1137 | for entry in scheduler_models.HostQueueEntry.fetch( |
| 1138 | where='aborted and not complete'): |
showard | f4a2e50 | 2009-07-28 20:06:39 +0000 | [diff] [blame] | 1139 | logging.info('Aborting %s', entry) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1140 | for agent in self.get_agents_for_entry(entry): |
| 1141 | agent.abort() |
| 1142 | entry.abort(self) |
jamesren | e7c65cb | 2010-06-08 20:38:10 +0000 | [diff] [blame] | 1143 | jobs_to_stop.add(entry.job) |
| 1144 | for job in jobs_to_stop: |
| 1145 | job.stop_if_necessary() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1146 | |
| 1147 | |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 1148 | def _can_start_agent(self, agent, num_started_this_cycle, |
| 1149 | have_reached_limit): |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1150 | # always allow zero-process agents to run |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1151 | if agent.task.num_processes == 0: |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1152 | return True |
| 1153 | # don't allow any nonzero-process agents to run after we've reached a |
| 1154 | # limit (this avoids starvation of many-process agents) |
| 1155 | if have_reached_limit: |
| 1156 | return False |
| 1157 | # total process throttling |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1158 | max_runnable_processes = _drone_manager.max_runnable_processes( |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1159 | agent.task.owner_username, |
| 1160 | agent.task.get_drone_hostnames_allowed()) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1161 | if agent.task.num_processes > max_runnable_processes: |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1162 | return False |
| 1163 | # if a single agent exceeds the per-cycle throttling, still allow it to |
| 1164 | # run when it's the first agent in the cycle |
| 1165 | if num_started_this_cycle == 0: |
| 1166 | return True |
| 1167 | # per-cycle throttling |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1168 | if (num_started_this_cycle + agent.task.num_processes > |
| 1169 | scheduler_config.config.max_processes_started_per_cycle): |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1170 | return False |
| 1171 | return True |
| 1172 | |
| 1173 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1174 | def _handle_agents(self): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1175 | num_started_this_cycle = 0 |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1176 | have_reached_limit = False |
| 1177 | # iterate over copy, so we can remove agents during iteration |
| 1178 | for agent in list(self._agents): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1179 | if not agent.started: |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 1180 | if not self._can_start_agent(agent, num_started_this_cycle, |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1181 | have_reached_limit): |
| 1182 | have_reached_limit = True |
| 1183 | continue |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1184 | num_started_this_cycle += agent.task.num_processes |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1185 | agent.tick() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1186 | if agent.is_done(): |
| 1187 | logging.info("agent finished") |
| 1188 | self.remove_agent(agent) |
showard | a9435c0 | 2009-05-13 21:28:17 +0000 | [diff] [blame] | 1189 | logging.info('%d running processes', |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 1190 | _drone_manager.total_running_processes()) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1191 | |
| 1192 | |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1193 | def _process_recurring_runs(self): |
| 1194 | recurring_runs = models.RecurringRun.objects.filter( |
| 1195 | start_date__lte=datetime.datetime.now()) |
| 1196 | for rrun in recurring_runs: |
| 1197 | # Create job from template |
| 1198 | job = rrun.job |
| 1199 | info = rpc_utils.get_job_info(job) |
showard | a9435c0 | 2009-05-13 21:28:17 +0000 | [diff] [blame] | 1200 | options = job.get_object_dict() |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1201 | |
| 1202 | host_objects = info['hosts'] |
| 1203 | one_time_hosts = info['one_time_hosts'] |
| 1204 | metahost_objects = info['meta_hosts'] |
| 1205 | dependencies = info['dependencies'] |
| 1206 | atomic_group = info['atomic_group'] |
| 1207 | |
| 1208 | for host in one_time_hosts or []: |
| 1209 | this_host = models.Host.create_one_time_host(host.hostname) |
| 1210 | host_objects.append(this_host) |
| 1211 | |
| 1212 | try: |
| 1213 | rpc_utils.create_new_job(owner=rrun.owner.login, |
showard | a9435c0 | 2009-05-13 21:28:17 +0000 | [diff] [blame] | 1214 | options=options, |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1215 | host_objects=host_objects, |
| 1216 | metahost_objects=metahost_objects, |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1217 | atomic_group=atomic_group) |
| 1218 | |
| 1219 | except Exception, ex: |
| 1220 | logging.exception(ex) |
| 1221 | #TODO send email |
| 1222 | |
| 1223 | if rrun.loop_count == 1: |
| 1224 | rrun.delete() |
| 1225 | else: |
| 1226 | if rrun.loop_count != 0: # if not infinite loop |
| 1227 | # calculate new start_date |
| 1228 | difference = datetime.timedelta(seconds=rrun.loop_period) |
| 1229 | rrun.start_date = rrun.start_date + difference |
| 1230 | rrun.loop_count -= 1 |
| 1231 | rrun.save() |
| 1232 | |
| 1233 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1234 | class PidfileRunMonitor(object): |
| 1235 | """ |
| 1236 | Client must call either run() to start a new process or |
| 1237 | attach_to_existing_process(). |
| 1238 | """ |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1239 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1240 | class _PidfileException(Exception): |
| 1241 | """ |
| 1242 | Raised when there's some unexpected behavior with the pid file, but only |
| 1243 | used internally (never allowed to escape this class). |
| 1244 | """ |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1245 | |
| 1246 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1247 | def __init__(self): |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1248 | self.lost_process = False |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1249 | self._start_time = None |
| 1250 | self.pidfile_id = None |
| 1251 | self._state = drone_manager.PidfileContents() |
showard | 2bab8f4 | 2008-11-12 18:15:22 +0000 | [diff] [blame] | 1252 | |
| 1253 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1254 | def _add_nice_command(self, command, nice_level): |
| 1255 | if not nice_level: |
| 1256 | return command |
| 1257 | return ['nice', '-n', str(nice_level)] + command |
| 1258 | |
| 1259 | |
| 1260 | def _set_start_time(self): |
| 1261 | self._start_time = time.time() |
| 1262 | |
| 1263 | |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1264 | def run(self, command, working_directory, num_processes, nice_level=None, |
| 1265 | log_file=None, pidfile_name=None, paired_with_pidfile=None, |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1266 | username=None, drone_hostnames_allowed=None): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1267 | assert command is not None |
| 1268 | if nice_level is not None: |
| 1269 | command = ['nice', '-n', str(nice_level)] + command |
| 1270 | self._set_start_time() |
| 1271 | self.pidfile_id = _drone_manager.execute_command( |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1272 | command, working_directory, pidfile_name=pidfile_name, |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1273 | num_processes=num_processes, log_file=log_file, |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1274 | paired_with_pidfile=paired_with_pidfile, username=username, |
| 1275 | drone_hostnames_allowed=drone_hostnames_allowed) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1276 | |
| 1277 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1278 | def attach_to_existing_process(self, execution_path, |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1279 | pidfile_name=drone_manager.AUTOSERV_PID_FILE, |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1280 | num_processes=None): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1281 | self._set_start_time() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1282 | self.pidfile_id = _drone_manager.get_pidfile_id_from( |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1283 | execution_path, pidfile_name=pidfile_name) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1284 | if num_processes is not None: |
| 1285 | _drone_manager.declare_process_count(self.pidfile_id, num_processes) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1286 | |
| 1287 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1288 | def kill(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1289 | if self.has_process(): |
| 1290 | _drone_manager.kill_process(self.get_process()) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1291 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1292 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1293 | def has_process(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1294 | self._get_pidfile_info() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1295 | return self._state.process is not None |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1296 | |
| 1297 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1298 | def get_process(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1299 | self._get_pidfile_info() |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1300 | assert self._state.process is not None |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1301 | return self._state.process |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1302 | |
| 1303 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1304 | def _read_pidfile(self, use_second_read=False): |
| 1305 | assert self.pidfile_id is not None, ( |
| 1306 | 'You must call run() or attach_to_existing_process()') |
| 1307 | contents = _drone_manager.get_pidfile_contents( |
| 1308 | self.pidfile_id, use_second_read=use_second_read) |
| 1309 | if contents.is_invalid(): |
| 1310 | self._state = drone_manager.PidfileContents() |
| 1311 | raise self._PidfileException(contents) |
| 1312 | self._state = contents |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1313 | |
| 1314 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1315 | def _handle_pidfile_error(self, error, message=''): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1316 | message = error + '\nProcess: %s\nPidfile: %s\n%s' % ( |
| 1317 | self._state.process, self.pidfile_id, message) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1318 | email_manager.manager.enqueue_notify_email(error, message) |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1319 | self.on_lost_process(self._state.process) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1320 | |
| 1321 | |
| 1322 | def _get_pidfile_info_helper(self): |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1323 | if self.lost_process: |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1324 | return |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1325 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1326 | self._read_pidfile() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1327 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1328 | if self._state.process is None: |
| 1329 | self._handle_no_process() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1330 | return |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1331 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1332 | if self._state.exit_status is None: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1333 | # double check whether or not autoserv is running |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1334 | if _drone_manager.is_process_running(self._state.process): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1335 | return |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1336 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1337 | # pid but no running process - maybe process *just* exited |
| 1338 | self._read_pidfile(use_second_read=True) |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1339 | if self._state.exit_status is None: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1340 | # autoserv exited without writing an exit code |
| 1341 | # to the pidfile |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1342 | self._handle_pidfile_error( |
| 1343 | 'autoserv died without writing exit code') |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1344 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1345 | |
| 1346 | def _get_pidfile_info(self): |
| 1347 | """\ |
| 1348 | After completion, self._state will contain: |
| 1349 | pid=None, exit_status=None if autoserv has not yet run |
| 1350 | pid!=None, exit_status=None if autoserv is running |
| 1351 | pid!=None, exit_status!=None if autoserv has completed |
| 1352 | """ |
| 1353 | try: |
| 1354 | self._get_pidfile_info_helper() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1355 | except self._PidfileException, exc: |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1356 | self._handle_pidfile_error('Pidfile error', traceback.format_exc()) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1357 | |
| 1358 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1359 | def _handle_no_process(self): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1360 | """\ |
| 1361 | Called when no pidfile is found or no pid is in the pidfile. |
| 1362 | """ |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1363 | message = 'No pid found at %s' % self.pidfile_id |
showard | ec6a3b9 | 2009-09-25 20:29:13 +0000 | [diff] [blame] | 1364 | if time.time() - self._start_time > _get_pidfile_timeout_secs(): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1365 | email_manager.manager.enqueue_notify_email( |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1366 | 'Process has failed to write pidfile', message) |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1367 | self.on_lost_process() |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1368 | |
| 1369 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1370 | def on_lost_process(self, process=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1371 | """\ |
| 1372 | Called when autoserv has exited without writing an exit status, |
| 1373 | or we've timed out waiting for autoserv to write a pid to the |
| 1374 | pidfile. In either case, we just return failure and the caller |
| 1375 | should signal some kind of warning. |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1376 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1377 | process is unimportant here, as it shouldn't be used by anyone. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1378 | """ |
| 1379 | self.lost_process = True |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1380 | self._state.process = process |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1381 | self._state.exit_status = 1 |
| 1382 | self._state.num_tests_failed = 0 |
mbligh | 90a549d | 2008-03-25 23:52:34 +0000 | [diff] [blame] | 1383 | |
| 1384 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1385 | def exit_code(self): |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1386 | self._get_pidfile_info() |
| 1387 | return self._state.exit_status |
| 1388 | |
| 1389 | |
| 1390 | def num_tests_failed(self): |
showard | 6bba3d1 | 2009-08-20 23:31:41 +0000 | [diff] [blame] | 1391 | """@returns The number of tests that failed or -1 if unknown.""" |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1392 | self._get_pidfile_info() |
showard | 6bba3d1 | 2009-08-20 23:31:41 +0000 | [diff] [blame] | 1393 | if self._state.num_tests_failed is None: |
| 1394 | return -1 |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1395 | return self._state.num_tests_failed |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1396 | |
| 1397 | |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1398 | def try_copy_results_on_drone(self, **kwargs): |
| 1399 | if self.has_process(): |
| 1400 | # copy results logs into the normal place for job results |
| 1401 | _drone_manager.copy_results_on_drone(self.get_process(), **kwargs) |
| 1402 | |
| 1403 | |
| 1404 | def try_copy_to_results_repository(self, source, **kwargs): |
| 1405 | if self.has_process(): |
| 1406 | _drone_manager.copy_to_results_repository(self.get_process(), |
| 1407 | source, **kwargs) |
| 1408 | |
| 1409 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1410 | class Agent(object): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1411 | """ |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1412 | An agent for use by the Dispatcher class to perform a task. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1413 | |
| 1414 | The following methods are required on all task objects: |
| 1415 | poll() - Called periodically to let the task check its status and |
| 1416 | update its internal state. If the task succeeded. |
| 1417 | is_done() - Returns True if the task is finished. |
| 1418 | abort() - Called when an abort has been requested. The task must |
| 1419 | set its aborted attribute to True if it actually aborted. |
| 1420 | |
| 1421 | The following attributes are required on all task objects: |
| 1422 | aborted - bool, True if this task was aborted. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1423 | success - bool, True if this task succeeded. |
| 1424 | queue_entry_ids - A sequence of HostQueueEntry ids this task handles. |
| 1425 | host_ids - A sequence of Host ids this task represents. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1426 | """ |
| 1427 | |
| 1428 | |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1429 | def __init__(self, task): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1430 | """ |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1431 | @param task: A task as described in the class docstring. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1432 | """ |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1433 | self.task = task |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1434 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1435 | # This is filled in by Dispatcher.add_agent() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1436 | self.dispatcher = None |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1437 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1438 | self.queue_entry_ids = task.queue_entry_ids |
| 1439 | self.host_ids = task.host_ids |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1440 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1441 | self.started = False |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1442 | self.finished = False |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1443 | |
| 1444 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1445 | def tick(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1446 | self.started = True |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1447 | if not self.finished: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1448 | self.task.poll() |
| 1449 | if self.task.is_done(): |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1450 | self.finished = True |
showard | ec11316 | 2008-05-08 00:52:49 +0000 | [diff] [blame] | 1451 | |
| 1452 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1453 | def is_done(self): |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1454 | return self.finished |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1455 | |
| 1456 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1457 | def abort(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1458 | if self.task: |
| 1459 | self.task.abort() |
| 1460 | if self.task.aborted: |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1461 | # tasks can choose to ignore aborts |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1462 | self.finished = True |
showard | 20f9bdd | 2009-04-29 19:48:33 +0000 | [diff] [blame] | 1463 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1464 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1465 | class AgentTask(object): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1466 | class _NullMonitor(object): |
| 1467 | pidfile_id = None |
| 1468 | |
| 1469 | def has_process(self): |
| 1470 | return True |
| 1471 | |
| 1472 | |
| 1473 | def __init__(self, log_file_name=None): |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1474 | """ |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1475 | @param log_file_name: (optional) name of file to log command output to |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1476 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1477 | self.done = False |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1478 | self.started = False |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1479 | self.success = None |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1480 | self.aborted = False |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1481 | self.monitor = None |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1482 | self.queue_entry_ids = [] |
| 1483 | self.host_ids = [] |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1484 | self._log_file_name = log_file_name |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1485 | |
| 1486 | |
| 1487 | def _set_ids(self, host=None, queue_entries=None): |
| 1488 | if queue_entries and queue_entries != [None]: |
| 1489 | self.host_ids = [entry.host.id for entry in queue_entries] |
| 1490 | self.queue_entry_ids = [entry.id for entry in queue_entries] |
| 1491 | else: |
| 1492 | assert host |
| 1493 | self.host_ids = [host.id] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1494 | |
| 1495 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1496 | def poll(self): |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1497 | if not self.started: |
| 1498 | self.start() |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1499 | if not self.done: |
| 1500 | self.tick() |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1501 | |
| 1502 | |
| 1503 | def tick(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1504 | assert self.monitor |
| 1505 | exit_code = self.monitor.exit_code() |
| 1506 | if exit_code is None: |
| 1507 | return |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1508 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1509 | success = (exit_code == 0) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1510 | self.finished(success) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1511 | |
| 1512 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1513 | def is_done(self): |
| 1514 | return self.done |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1515 | |
| 1516 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1517 | def finished(self, success): |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1518 | if self.done: |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1519 | assert self.started |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1520 | return |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1521 | self.started = True |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1522 | self.done = True |
| 1523 | self.success = success |
| 1524 | self.epilog() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1525 | |
| 1526 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1527 | def prolog(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1528 | """ |
| 1529 | To be overridden. |
| 1530 | """ |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1531 | assert not self.monitor |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1532 | self.register_necessary_pidfiles() |
| 1533 | |
| 1534 | |
| 1535 | def _log_file(self): |
| 1536 | if not self._log_file_name: |
| 1537 | return None |
| 1538 | return os.path.join(self._working_directory(), self._log_file_name) |
mbligh | d64e570 | 2008-04-04 21:39:28 +0000 | [diff] [blame] | 1539 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1540 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1541 | def cleanup(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1542 | log_file = self._log_file() |
| 1543 | if self.monitor and log_file: |
| 1544 | self.monitor.try_copy_to_results_repository(log_file) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1545 | |
| 1546 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1547 | def epilog(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1548 | """ |
| 1549 | To be overridden. |
| 1550 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1551 | self.cleanup() |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1552 | logging.info("%s finished with success=%s", type(self).__name__, |
| 1553 | self.success) |
| 1554 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1555 | |
| 1556 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1557 | def start(self): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1558 | if not self.started: |
| 1559 | self.prolog() |
| 1560 | self.run() |
| 1561 | |
| 1562 | self.started = True |
| 1563 | |
| 1564 | |
| 1565 | def abort(self): |
| 1566 | if self.monitor: |
| 1567 | self.monitor.kill() |
| 1568 | self.done = True |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1569 | self.aborted = True |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1570 | self.cleanup() |
| 1571 | |
| 1572 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1573 | def _get_consistent_execution_path(self, execution_entries): |
| 1574 | first_execution_path = execution_entries[0].execution_path() |
| 1575 | for execution_entry in execution_entries[1:]: |
| 1576 | assert execution_entry.execution_path() == first_execution_path, ( |
| 1577 | '%s (%s) != %s (%s)' % (execution_entry.execution_path(), |
| 1578 | execution_entry, |
| 1579 | first_execution_path, |
| 1580 | execution_entries[0])) |
| 1581 | return first_execution_path |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1582 | |
| 1583 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1584 | def _copy_results(self, execution_entries, use_monitor=None): |
| 1585 | """ |
| 1586 | @param execution_entries: list of objects with execution_path() method |
| 1587 | """ |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 1588 | if use_monitor is not None and not use_monitor.has_process(): |
| 1589 | return |
| 1590 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1591 | assert len(execution_entries) > 0 |
showard | 6b73341 | 2009-04-27 20:09:18 +0000 | [diff] [blame] | 1592 | if use_monitor is None: |
| 1593 | assert self.monitor |
| 1594 | use_monitor = self.monitor |
| 1595 | assert use_monitor.has_process() |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1596 | execution_path = self._get_consistent_execution_path(execution_entries) |
| 1597 | results_path = execution_path + '/' |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1598 | use_monitor.try_copy_to_results_repository(results_path) |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1599 | |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1600 | |
| 1601 | def _parse_results(self, queue_entries): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1602 | for queue_entry in queue_entries: |
| 1603 | queue_entry.set_status(models.HostQueueEntry.Status.PARSING) |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1604 | |
| 1605 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1606 | def _archive_results(self, queue_entries): |
| 1607 | for queue_entry in queue_entries: |
| 1608 | queue_entry.set_status(models.HostQueueEntry.Status.ARCHIVING) |
showard | a1e74b3 | 2009-05-12 17:32:04 +0000 | [diff] [blame] | 1609 | |
| 1610 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1611 | def _command_line(self): |
| 1612 | """ |
| 1613 | Return the command line to run. Must be overridden. |
| 1614 | """ |
| 1615 | raise NotImplementedError |
| 1616 | |
| 1617 | |
| 1618 | @property |
| 1619 | def num_processes(self): |
| 1620 | """ |
| 1621 | Return the number of processes forked by this AgentTask's process. It |
| 1622 | may only be approximate. To be overridden if necessary. |
| 1623 | """ |
| 1624 | return 1 |
| 1625 | |
| 1626 | |
| 1627 | def _paired_with_monitor(self): |
| 1628 | """ |
| 1629 | If this AgentTask's process must run on the same machine as some |
| 1630 | previous process, this method should be overridden to return a |
| 1631 | PidfileRunMonitor for that process. |
| 1632 | """ |
| 1633 | return self._NullMonitor() |
| 1634 | |
| 1635 | |
| 1636 | @property |
| 1637 | def owner_username(self): |
| 1638 | """ |
| 1639 | Return login of user responsible for this task. May be None. Must be |
| 1640 | overridden. |
| 1641 | """ |
| 1642 | raise NotImplementedError |
| 1643 | |
| 1644 | |
| 1645 | def _working_directory(self): |
| 1646 | """ |
| 1647 | Return the directory where this AgentTask's process executes. Must be |
| 1648 | overridden. |
| 1649 | """ |
| 1650 | raise NotImplementedError |
| 1651 | |
| 1652 | |
| 1653 | def _pidfile_name(self): |
| 1654 | """ |
| 1655 | Return the name of the pidfile this AgentTask's process uses. To be |
| 1656 | overridden if necessary. |
| 1657 | """ |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1658 | return drone_manager.AUTOSERV_PID_FILE |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1659 | |
| 1660 | |
| 1661 | def _check_paired_results_exist(self): |
| 1662 | if not self._paired_with_monitor().has_process(): |
| 1663 | email_manager.manager.enqueue_notify_email( |
| 1664 | 'No paired results in task', |
| 1665 | 'No paired results in task %s at %s' |
| 1666 | % (self, self._paired_with_monitor().pidfile_id)) |
| 1667 | self.finished(False) |
| 1668 | return False |
| 1669 | return True |
| 1670 | |
| 1671 | |
| 1672 | def _create_monitor(self): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1673 | assert not self.monitor |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1674 | self.monitor = PidfileRunMonitor() |
| 1675 | |
| 1676 | |
| 1677 | def run(self): |
| 1678 | if not self._check_paired_results_exist(): |
| 1679 | return |
| 1680 | |
| 1681 | self._create_monitor() |
| 1682 | self.monitor.run( |
| 1683 | self._command_line(), self._working_directory(), |
| 1684 | num_processes=self.num_processes, |
| 1685 | nice_level=AUTOSERV_NICE_LEVEL, log_file=self._log_file(), |
| 1686 | pidfile_name=self._pidfile_name(), |
| 1687 | paired_with_pidfile=self._paired_with_monitor().pidfile_id, |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1688 | username=self.owner_username, |
| 1689 | drone_hostnames_allowed=self.get_drone_hostnames_allowed()) |
| 1690 | |
| 1691 | |
| 1692 | def get_drone_hostnames_allowed(self): |
| 1693 | if not models.DroneSet.drone_sets_enabled(): |
| 1694 | return None |
| 1695 | |
| 1696 | hqes = models.HostQueueEntry.objects.filter(id__in=self.queue_entry_ids) |
| 1697 | if not hqes: |
| 1698 | # Only special tasks could be missing host queue entries |
| 1699 | assert isinstance(self, SpecialAgentTask) |
| 1700 | return self._user_or_global_default_drone_set( |
| 1701 | self.task, self.task.requested_by) |
| 1702 | |
| 1703 | job_ids = hqes.values_list('job', flat=True).distinct() |
| 1704 | assert job_ids.count() == 1, ("AgentTask's queue entries " |
| 1705 | "span multiple jobs") |
| 1706 | |
| 1707 | job = models.Job.objects.get(id=job_ids[0]) |
| 1708 | drone_set = job.drone_set |
| 1709 | if not drone_set: |
jamesren | dd77e01 | 2010-04-28 18:07:30 +0000 | [diff] [blame] | 1710 | return self._user_or_global_default_drone_set(job, job.user()) |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 1711 | |
| 1712 | return drone_set.get_drone_hostnames() |
| 1713 | |
| 1714 | |
| 1715 | def _user_or_global_default_drone_set(self, obj_with_owner, user): |
| 1716 | """ |
| 1717 | Returns the user's default drone set, if present. |
| 1718 | |
| 1719 | Otherwise, returns the global default drone set. |
| 1720 | """ |
| 1721 | default_hostnames = models.DroneSet.get_default().get_drone_hostnames() |
| 1722 | if not user: |
| 1723 | logging.warn('%s had no owner; using default drone set', |
| 1724 | obj_with_owner) |
| 1725 | return default_hostnames |
| 1726 | if not user.drone_set: |
| 1727 | logging.warn('User %s has no default drone set, using global ' |
| 1728 | 'default', user.login) |
| 1729 | return default_hostnames |
| 1730 | return user.drone_set.get_drone_hostnames() |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1731 | |
| 1732 | |
| 1733 | def register_necessary_pidfiles(self): |
| 1734 | pidfile_id = _drone_manager.get_pidfile_id_from( |
| 1735 | self._working_directory(), self._pidfile_name()) |
| 1736 | _drone_manager.register_pidfile(pidfile_id) |
| 1737 | |
| 1738 | paired_pidfile_id = self._paired_with_monitor().pidfile_id |
| 1739 | if paired_pidfile_id: |
| 1740 | _drone_manager.register_pidfile(paired_pidfile_id) |
| 1741 | |
| 1742 | |
| 1743 | def recover(self): |
| 1744 | if not self._check_paired_results_exist(): |
| 1745 | return |
| 1746 | |
| 1747 | self._create_monitor() |
| 1748 | self.monitor.attach_to_existing_process( |
| 1749 | self._working_directory(), pidfile_name=self._pidfile_name(), |
| 1750 | num_processes=self.num_processes) |
| 1751 | if not self.monitor.has_process(): |
| 1752 | # no process to recover; wait to be started normally |
| 1753 | self.monitor = None |
| 1754 | return |
| 1755 | |
| 1756 | self.started = True |
| 1757 | logging.info('Recovering process %s for %s at %s' |
| 1758 | % (self.monitor.get_process(), type(self).__name__, |
| 1759 | self._working_directory())) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1760 | |
| 1761 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1762 | def _check_queue_entry_statuses(self, queue_entries, allowed_hqe_statuses, |
| 1763 | allowed_host_statuses=None): |
jamesren | b8f3f35 | 2010-06-10 00:44:06 +0000 | [diff] [blame] | 1764 | class_name = self.__class__.__name__ |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1765 | for entry in queue_entries: |
| 1766 | if entry.status not in allowed_hqe_statuses: |
jamesren | b8f3f35 | 2010-06-10 00:44:06 +0000 | [diff] [blame] | 1767 | raise SchedulerError('%s attempting to start ' |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1768 | 'entry with invalid status %s: %s' |
jamesren | b8f3f35 | 2010-06-10 00:44:06 +0000 | [diff] [blame] | 1769 | % (class_name, entry.status, entry)) |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1770 | invalid_host_status = ( |
| 1771 | allowed_host_statuses is not None |
| 1772 | and entry.host.status not in allowed_host_statuses) |
| 1773 | if invalid_host_status: |
jamesren | b8f3f35 | 2010-06-10 00:44:06 +0000 | [diff] [blame] | 1774 | raise SchedulerError('%s attempting to start on queue ' |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1775 | 'entry with invalid host status %s: %s' |
jamesren | b8f3f35 | 2010-06-10 00:44:06 +0000 | [diff] [blame] | 1776 | % (class_name, entry.host.status, entry)) |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1777 | |
| 1778 | |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1779 | class TaskWithJobKeyvals(object): |
| 1780 | """AgentTask mixin providing functionality to help with job keyval files.""" |
| 1781 | _KEYVAL_FILE = 'keyval' |
| 1782 | def _format_keyval(self, key, value): |
| 1783 | return '%s=%s' % (key, value) |
| 1784 | |
| 1785 | |
| 1786 | def _keyval_path(self): |
| 1787 | """Subclasses must override this""" |
lmr | b7c5d27 | 2010-04-16 06:34:04 +0000 | [diff] [blame] | 1788 | raise NotImplementedError |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1789 | |
| 1790 | |
| 1791 | def _write_keyval_after_job(self, field, value): |
| 1792 | assert self.monitor |
| 1793 | if not self.monitor.has_process(): |
| 1794 | return |
| 1795 | _drone_manager.write_lines_to_file( |
| 1796 | self._keyval_path(), [self._format_keyval(field, value)], |
| 1797 | paired_with_process=self.monitor.get_process()) |
| 1798 | |
| 1799 | |
| 1800 | def _job_queued_keyval(self, job): |
| 1801 | return 'job_queued', int(time.mktime(job.created_on.timetuple())) |
| 1802 | |
| 1803 | |
| 1804 | def _write_job_finished(self): |
| 1805 | self._write_keyval_after_job("job_finished", int(time.time())) |
| 1806 | |
| 1807 | |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame] | 1808 | def _write_keyvals_before_job_helper(self, keyval_dict, keyval_path): |
| 1809 | keyval_contents = '\n'.join(self._format_keyval(key, value) |
| 1810 | for key, value in keyval_dict.iteritems()) |
| 1811 | # always end with a newline to allow additional keyvals to be written |
| 1812 | keyval_contents += '\n' |
showard | 493beaa | 2009-12-18 22:44:45 +0000 | [diff] [blame] | 1813 | _drone_manager.attach_file_to_execution(self._working_directory(), |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame] | 1814 | keyval_contents, |
| 1815 | file_path=keyval_path) |
| 1816 | |
| 1817 | |
| 1818 | def _write_keyvals_before_job(self, keyval_dict): |
| 1819 | self._write_keyvals_before_job_helper(keyval_dict, self._keyval_path()) |
| 1820 | |
| 1821 | |
| 1822 | def _write_host_keyvals(self, host): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1823 | keyval_path = os.path.join(self._working_directory(), 'host_keyvals', |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame] | 1824 | host.hostname) |
| 1825 | platform, all_labels = host.platform_and_labels() |
| 1826 | keyval_dict = dict(platform=platform, labels=','.join(all_labels)) |
| 1827 | self._write_keyvals_before_job_helper(keyval_dict, keyval_path) |
| 1828 | |
| 1829 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1830 | class SpecialAgentTask(AgentTask, TaskWithJobKeyvals): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1831 | """ |
| 1832 | Subclass for AgentTasks that correspond to a SpecialTask entry in the DB. |
| 1833 | """ |
| 1834 | |
| 1835 | TASK_TYPE = None |
| 1836 | host = None |
| 1837 | queue_entry = None |
| 1838 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1839 | def __init__(self, task, extra_command_args): |
| 1840 | super(SpecialAgentTask, self).__init__() |
| 1841 | |
lmr | b7c5d27 | 2010-04-16 06:34:04 +0000 | [diff] [blame] | 1842 | assert self.TASK_TYPE is not None, 'self.TASK_TYPE must be overridden' |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1843 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1844 | self.host = scheduler_models.Host(id=task.host.id) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1845 | self.queue_entry = None |
| 1846 | if task.queue_entry: |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1847 | self.queue_entry = scheduler_models.HostQueueEntry( |
| 1848 | id=task.queue_entry.id) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1849 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1850 | self.task = task |
| 1851 | self._extra_command_args = extra_command_args |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1852 | |
| 1853 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1854 | def _keyval_path(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1855 | return os.path.join(self._working_directory(), self._KEYVAL_FILE) |
| 1856 | |
| 1857 | |
| 1858 | def _command_line(self): |
| 1859 | return _autoserv_command_line(self.host.hostname, |
| 1860 | self._extra_command_args, |
| 1861 | queue_entry=self.queue_entry) |
| 1862 | |
| 1863 | |
| 1864 | def _working_directory(self): |
| 1865 | return self.task.execution_path() |
| 1866 | |
| 1867 | |
| 1868 | @property |
| 1869 | def owner_username(self): |
| 1870 | if self.task.requested_by: |
| 1871 | return self.task.requested_by.login |
| 1872 | return None |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1873 | |
| 1874 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1875 | def prolog(self): |
| 1876 | super(SpecialAgentTask, self).prolog() |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1877 | self.task.activate() |
showard | db50276 | 2009-09-09 15:31:20 +0000 | [diff] [blame] | 1878 | self._write_host_keyvals(self.host) |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1879 | |
| 1880 | |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1881 | def _fail_queue_entry(self): |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1882 | assert self.queue_entry |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 1883 | |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1884 | if self.queue_entry.meta_host: |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 1885 | return # don't fail metahost entries, they'll be reassigned |
| 1886 | |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1887 | self.queue_entry.update_from_database() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1888 | if self.queue_entry.status != models.HostQueueEntry.Status.QUEUED: |
showard | ccbd6c5 | 2009-03-21 00:10:21 +0000 | [diff] [blame] | 1889 | return # entry has been aborted |
| 1890 | |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1891 | self.queue_entry.set_execution_subdir() |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1892 | queued_key, queued_time = self._job_queued_keyval( |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1893 | self.queue_entry.job) |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1894 | self._write_keyval_after_job(queued_key, queued_time) |
| 1895 | self._write_job_finished() |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1896 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1897 | # copy results logs into the normal place for job results |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1898 | self.monitor.try_copy_results_on_drone( |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1899 | source_path=self._working_directory() + '/', |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1900 | destination_path=self.queue_entry.execution_path() + '/') |
showard | 678df4f | 2009-02-04 21:36:39 +0000 | [diff] [blame] | 1901 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1902 | pidfile_id = _drone_manager.get_pidfile_id_from( |
| 1903 | self.queue_entry.execution_path(), |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1904 | pidfile_name=drone_manager.AUTOSERV_PID_FILE) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1905 | _drone_manager.register_pidfile(pidfile_id) |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1906 | |
| 1907 | if self.queue_entry.job.parse_failed_repair: |
| 1908 | self._parse_results([self.queue_entry]) |
| 1909 | else: |
| 1910 | self._archive_results([self.queue_entry]) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1911 | |
| 1912 | |
| 1913 | def cleanup(self): |
| 1914 | super(SpecialAgentTask, self).cleanup() |
showard | e60e44e | 2009-11-13 20:45:38 +0000 | [diff] [blame] | 1915 | |
| 1916 | # We will consider an aborted task to be "Failed" |
| 1917 | self.task.finish(bool(self.success)) |
| 1918 | |
showard | f85a0b7 | 2009-10-07 20:48:45 +0000 | [diff] [blame] | 1919 | if self.monitor: |
| 1920 | if self.monitor.has_process(): |
| 1921 | self._copy_results([self.task]) |
| 1922 | if self.monitor.pidfile_id is not None: |
| 1923 | _drone_manager.unregister_pidfile(self.monitor.pidfile_id) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1924 | |
| 1925 | |
| 1926 | class RepairTask(SpecialAgentTask): |
| 1927 | TASK_TYPE = models.SpecialTask.Task.REPAIR |
| 1928 | |
| 1929 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1930 | def __init__(self, task): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1931 | """\ |
| 1932 | queue_entry: queue entry to mark failed if this repair fails. |
| 1933 | """ |
| 1934 | protection = host_protections.Protection.get_string( |
| 1935 | task.host.protection) |
| 1936 | # normalize the protection name |
| 1937 | protection = host_protections.Protection.get_attr_name(protection) |
| 1938 | |
| 1939 | super(RepairTask, self).__init__( |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1940 | task, ['-R', '--host-protection', protection]) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1941 | |
| 1942 | # *don't* include the queue entry in IDs -- if the queue entry is |
| 1943 | # aborted, we want to leave the repair task running |
| 1944 | self._set_ids(host=self.host) |
| 1945 | |
| 1946 | |
| 1947 | def prolog(self): |
| 1948 | super(RepairTask, self).prolog() |
| 1949 | logging.info("repair_task starting") |
| 1950 | self.host.set_status(models.Host.Status.REPAIRING) |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1951 | |
| 1952 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1953 | def epilog(self): |
| 1954 | super(RepairTask, self).epilog() |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 1955 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1956 | if self.success: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1957 | self.host.set_status(models.Host.Status.READY) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1958 | else: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1959 | self.host.set_status(models.Host.Status.REPAIR_FAILED) |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 1960 | if self.queue_entry: |
showard | de634ee | 2009-01-30 01:44:24 +0000 | [diff] [blame] | 1961 | self._fail_queue_entry() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1962 | |
| 1963 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 1964 | class PreJobTask(SpecialAgentTask): |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1965 | def _copy_to_results_repository(self): |
| 1966 | if not self.queue_entry or self.queue_entry.meta_host: |
| 1967 | return |
| 1968 | |
| 1969 | self.queue_entry.set_execution_subdir() |
| 1970 | log_name = os.path.basename(self.task.execution_path()) |
| 1971 | source = os.path.join(self.task.execution_path(), 'debug', |
| 1972 | 'autoserv.DEBUG') |
| 1973 | destination = os.path.join( |
| 1974 | self.queue_entry.execution_path(), log_name) |
| 1975 | |
| 1976 | self.monitor.try_copy_to_results_repository( |
| 1977 | source, destination_path=destination) |
| 1978 | |
| 1979 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1980 | def epilog(self): |
| 1981 | super(PreJobTask, self).epilog() |
showard | cdaeae8 | 2009-08-31 18:32:48 +0000 | [diff] [blame] | 1982 | |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1983 | if self.success: |
| 1984 | return |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 1985 | |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1986 | self._copy_to_results_repository() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1987 | |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1988 | if self.host.protection == host_protections.Protection.DO_NOT_VERIFY: |
showard | 7b2d7cb | 2009-10-28 19:53:03 +0000 | [diff] [blame] | 1989 | # effectively ignore failure for these hosts |
| 1990 | self.success = True |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1991 | return |
| 1992 | |
| 1993 | if self.queue_entry: |
| 1994 | self.queue_entry.requeue() |
| 1995 | |
| 1996 | if models.SpecialTask.objects.filter( |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1997 | task=models.SpecialTask.Task.REPAIR, |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 1998 | queue_entry__id=self.queue_entry.id): |
| 1999 | self.host.set_status(models.Host.Status.REPAIR_FAILED) |
| 2000 | self._fail_queue_entry() |
| 2001 | return |
| 2002 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2003 | queue_entry = models.HostQueueEntry.objects.get( |
| 2004 | id=self.queue_entry.id) |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 2005 | else: |
| 2006 | queue_entry = None |
| 2007 | |
| 2008 | models.SpecialTask.objects.create( |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2009 | host=models.Host.objects.get(id=self.host.id), |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 2010 | task=models.SpecialTask.Task.REPAIR, |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2011 | queue_entry=queue_entry, |
| 2012 | requested_by=self.task.requested_by) |
showard | 58721a8 | 2009-08-20 23:32:40 +0000 | [diff] [blame] | 2013 | |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 2014 | |
| 2015 | class VerifyTask(PreJobTask): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2016 | TASK_TYPE = models.SpecialTask.Task.VERIFY |
| 2017 | |
| 2018 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2019 | def __init__(self, task): |
| 2020 | super(VerifyTask, self).__init__(task, ['-v']) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2021 | self._set_ids(host=self.host, queue_entries=[self.queue_entry]) |
mbligh | e258668 | 2008-02-29 22:45:46 +0000 | [diff] [blame] | 2022 | |
| 2023 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2024 | def prolog(self): |
showard | 8fe93b5 | 2008-11-18 17:53:22 +0000 | [diff] [blame] | 2025 | super(VerifyTask, self).prolog() |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2026 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 2027 | logging.info("starting verify on %s", self.host.hostname) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2028 | if self.queue_entry: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2029 | self.queue_entry.set_status(models.HostQueueEntry.Status.VERIFYING) |
| 2030 | self.host.set_status(models.Host.Status.VERIFYING) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2031 | |
jamesren | 42318f7 | 2010-05-10 23:40:59 +0000 | [diff] [blame] | 2032 | # Delete any queued manual reverifies for this host. One verify will do |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2033 | # and there's no need to keep records of other requests. |
| 2034 | queued_verifies = models.SpecialTask.objects.filter( |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 2035 | host__id=self.host.id, |
| 2036 | task=models.SpecialTask.Task.VERIFY, |
jamesren | 42318f7 | 2010-05-10 23:40:59 +0000 | [diff] [blame] | 2037 | is_active=False, is_complete=False, queue_entry=None) |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2038 | queued_verifies = queued_verifies.exclude(id=self.task.id) |
| 2039 | queued_verifies.delete() |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 2040 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2041 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2042 | def epilog(self): |
| 2043 | super(VerifyTask, self).epilog() |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 2044 | if self.success: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2045 | if self.queue_entry: |
| 2046 | self.queue_entry.on_pending() |
| 2047 | else: |
| 2048 | self.host.set_status(models.Host.Status.READY) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2049 | |
| 2050 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2051 | class CleanupTask(PreJobTask): |
| 2052 | # note this can also run post-job, but when it does, it's running standalone |
| 2053 | # against the host (not related to the job), so it's not considered a |
| 2054 | # PostJobTask |
| 2055 | |
| 2056 | TASK_TYPE = models.SpecialTask.Task.CLEANUP |
| 2057 | |
| 2058 | |
| 2059 | def __init__(self, task, recover_run_monitor=None): |
| 2060 | super(CleanupTask, self).__init__(task, ['--cleanup']) |
| 2061 | self._set_ids(host=self.host, queue_entries=[self.queue_entry]) |
| 2062 | |
| 2063 | |
| 2064 | def prolog(self): |
| 2065 | super(CleanupTask, self).prolog() |
| 2066 | logging.info("starting cleanup task for host: %s", self.host.hostname) |
| 2067 | self.host.set_status(models.Host.Status.CLEANING) |
| 2068 | if self.queue_entry: |
| 2069 | self.queue_entry.set_status(models.HostQueueEntry.Status.VERIFYING) |
| 2070 | |
| 2071 | |
| 2072 | def _finish_epilog(self): |
| 2073 | if not self.queue_entry or not self.success: |
| 2074 | return |
| 2075 | |
| 2076 | do_not_verify_protection = host_protections.Protection.DO_NOT_VERIFY |
| 2077 | should_run_verify = ( |
| 2078 | self.queue_entry.job.run_verify |
| 2079 | and self.host.protection != do_not_verify_protection) |
| 2080 | if should_run_verify: |
| 2081 | entry = models.HostQueueEntry.objects.get(id=self.queue_entry.id) |
| 2082 | models.SpecialTask.objects.create( |
| 2083 | host=models.Host.objects.get(id=self.host.id), |
| 2084 | queue_entry=entry, |
| 2085 | task=models.SpecialTask.Task.VERIFY) |
| 2086 | else: |
| 2087 | self.queue_entry.on_pending() |
| 2088 | |
| 2089 | |
| 2090 | def epilog(self): |
| 2091 | super(CleanupTask, self).epilog() |
| 2092 | |
| 2093 | if self.success: |
| 2094 | self.host.update_field('dirty', 0) |
| 2095 | self.host.set_status(models.Host.Status.READY) |
| 2096 | |
| 2097 | self._finish_epilog() |
| 2098 | |
| 2099 | |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 2100 | class AbstractQueueTask(AgentTask, TaskWithJobKeyvals): |
| 2101 | """ |
| 2102 | Common functionality for QueueTask and HostlessQueueTask |
| 2103 | """ |
| 2104 | def __init__(self, queue_entries): |
| 2105 | super(AbstractQueueTask, self).__init__() |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2106 | self.job = queue_entries[0].job |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2107 | self.queue_entries = queue_entries |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2108 | |
| 2109 | |
showard | 73ec044 | 2009-02-07 02:05:20 +0000 | [diff] [blame] | 2110 | def _keyval_path(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2111 | return os.path.join(self._working_directory(), self._KEYVAL_FILE) |
showard | 73ec044 | 2009-02-07 02:05:20 +0000 | [diff] [blame] | 2112 | |
| 2113 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 2114 | def _write_control_file(self, execution_path): |
| 2115 | control_path = _drone_manager.attach_file_to_execution( |
| 2116 | execution_path, self.job.control_file) |
| 2117 | return control_path |
| 2118 | |
| 2119 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2120 | def _command_line(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 2121 | execution_path = self.queue_entries[0].execution_path() |
| 2122 | control_path = self._write_control_file(execution_path) |
| 2123 | hostnames = ','.join(entry.host.hostname |
| 2124 | for entry in self.queue_entries |
| 2125 | if not entry.is_hostless()) |
| 2126 | |
| 2127 | execution_tag = self.queue_entries[0].execution_tag() |
| 2128 | params = _autoserv_command_line( |
| 2129 | hostnames, |
| 2130 | ['-P', execution_tag, '-n', |
| 2131 | _drone_manager.absolute_path(control_path)], |
| 2132 | job=self.job, verbose=False) |
| 2133 | |
| 2134 | if not self.job.is_server_job(): |
| 2135 | params.append('-c') |
| 2136 | |
| 2137 | return params |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2138 | |
| 2139 | |
| 2140 | @property |
| 2141 | def num_processes(self): |
| 2142 | return len(self.queue_entries) |
| 2143 | |
| 2144 | |
| 2145 | @property |
| 2146 | def owner_username(self): |
| 2147 | return self.job.owner |
| 2148 | |
| 2149 | |
| 2150 | def _working_directory(self): |
| 2151 | return self._get_consistent_execution_path(self.queue_entries) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 2152 | |
| 2153 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2154 | def prolog(self): |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 2155 | queued_key, queued_time = self._job_queued_keyval(self.job) |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 2156 | keyval_dict = self.job.keyval_dict() |
| 2157 | keyval_dict[queued_key] = queued_time |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2158 | group_name = self.queue_entries[0].get_group_name() |
| 2159 | if group_name: |
| 2160 | keyval_dict['host_group_name'] = group_name |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 2161 | self._write_keyvals_before_job(keyval_dict) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2162 | for queue_entry in self.queue_entries: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2163 | queue_entry.set_status(models.HostQueueEntry.Status.RUNNING) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 2164 | queue_entry.set_started_on_now() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2165 | |
| 2166 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 2167 | def _write_lost_process_error_file(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2168 | error_file_path = os.path.join(self._working_directory(), 'job_failure') |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 2169 | _drone_manager.write_lines_to_file(error_file_path, |
| 2170 | [_LOST_PROCESS_ERROR]) |
| 2171 | |
| 2172 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2173 | def _finish_task(self): |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 2174 | if not self.monitor: |
| 2175 | return |
| 2176 | |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 2177 | self._write_job_finished() |
| 2178 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 2179 | if self.monitor.lost_process: |
| 2180 | self._write_lost_process_error_file() |
showard | 4ac4754 | 2009-08-31 18:32:19 +0000 | [diff] [blame] | 2181 | |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 2182 | |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 2183 | def _write_status_comment(self, comment): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2184 | _drone_manager.write_lines_to_file( |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2185 | os.path.join(self._working_directory(), 'status.log'), |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2186 | ['INFO\t----\t----\t' + comment], |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 2187 | paired_with_process=self.monitor.get_process()) |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 2188 | |
| 2189 | |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 2190 | def _log_abort(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2191 | if not self.monitor or not self.monitor.has_process(): |
| 2192 | return |
| 2193 | |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 2194 | # build up sets of all the aborted_by and aborted_on values |
| 2195 | aborted_by, aborted_on = set(), set() |
| 2196 | for queue_entry in self.queue_entries: |
| 2197 | if queue_entry.aborted_by: |
| 2198 | aborted_by.add(queue_entry.aborted_by) |
| 2199 | t = int(time.mktime(queue_entry.aborted_on.timetuple())) |
| 2200 | aborted_on.add(t) |
| 2201 | |
| 2202 | # extract some actual, unique aborted by value and write it out |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 2203 | # TODO(showard): this conditional is now obsolete, we just need to leave |
| 2204 | # it in temporarily for backwards compatibility over upgrades. delete |
| 2205 | # soon. |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 2206 | assert len(aborted_by) <= 1 |
| 2207 | if len(aborted_by) == 1: |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 2208 | aborted_by_value = aborted_by.pop() |
| 2209 | aborted_on_value = max(aborted_on) |
| 2210 | else: |
| 2211 | aborted_by_value = 'autotest_system' |
| 2212 | aborted_on_value = int(time.time()) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2213 | |
showard | a038235 | 2009-02-11 23:36:43 +0000 | [diff] [blame] | 2214 | self._write_keyval_after_job("aborted_by", aborted_by_value) |
| 2215 | self._write_keyval_after_job("aborted_on", aborted_on_value) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2216 | |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 2217 | aborted_on_string = str(datetime.datetime.fromtimestamp( |
| 2218 | aborted_on_value)) |
| 2219 | self._write_status_comment('Job aborted by %s on %s' % |
| 2220 | (aborted_by_value, aborted_on_string)) |
jadmanski | c2ac77f | 2008-05-16 21:44:04 +0000 | [diff] [blame] | 2221 | |
| 2222 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2223 | def abort(self): |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 2224 | super(AbstractQueueTask, self).abort() |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 2225 | self._log_abort() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2226 | self._finish_task() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2227 | |
| 2228 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2229 | def epilog(self): |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 2230 | super(AbstractQueueTask, self).epilog() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2231 | self._finish_task() |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 2232 | |
| 2233 | |
| 2234 | class QueueTask(AbstractQueueTask): |
| 2235 | def __init__(self, queue_entries): |
| 2236 | super(QueueTask, self).__init__(queue_entries) |
| 2237 | self._set_ids(queue_entries=queue_entries) |
| 2238 | |
| 2239 | |
| 2240 | def prolog(self): |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2241 | self._check_queue_entry_statuses( |
| 2242 | self.queue_entries, |
| 2243 | allowed_hqe_statuses=(models.HostQueueEntry.Status.STARTING, |
| 2244 | models.HostQueueEntry.Status.RUNNING), |
| 2245 | allowed_host_statuses=(models.Host.Status.PENDING, |
| 2246 | models.Host.Status.RUNNING)) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 2247 | |
| 2248 | super(QueueTask, self).prolog() |
| 2249 | |
| 2250 | for queue_entry in self.queue_entries: |
| 2251 | self._write_host_keyvals(queue_entry.host) |
| 2252 | queue_entry.host.set_status(models.Host.Status.RUNNING) |
| 2253 | queue_entry.host.update_field('dirty', 1) |
| 2254 | if self.job.synch_count == 1 and len(self.queue_entries) == 1: |
| 2255 | # TODO(gps): Remove this if nothing needs it anymore. |
| 2256 | # A potential user is: tko/parser |
| 2257 | self.job.write_to_machines_file(self.queue_entries[0]) |
| 2258 | |
| 2259 | |
| 2260 | def _finish_task(self): |
| 2261 | super(QueueTask, self)._finish_task() |
| 2262 | |
| 2263 | for queue_entry in self.queue_entries: |
| 2264 | queue_entry.set_status(models.HostQueueEntry.Status.GATHERING) |
jamesren | b8f3f35 | 2010-06-10 00:44:06 +0000 | [diff] [blame] | 2265 | queue_entry.host.set_status(models.Host.Status.RUNNING) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2266 | |
| 2267 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2268 | class HostlessQueueTask(AbstractQueueTask): |
| 2269 | def __init__(self, queue_entry): |
| 2270 | super(HostlessQueueTask, self).__init__([queue_entry]) |
| 2271 | self.queue_entry_ids = [queue_entry.id] |
| 2272 | |
| 2273 | |
| 2274 | def prolog(self): |
| 2275 | self.queue_entries[0].update_field('execution_subdir', 'hostless') |
| 2276 | super(HostlessQueueTask, self).prolog() |
| 2277 | |
| 2278 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2279 | def _finish_task(self): |
| 2280 | super(HostlessQueueTask, self)._finish_task() |
showard | cc92936 | 2010-01-25 21:20:41 +0000 | [diff] [blame] | 2281 | self.queue_entries[0].set_status(models.HostQueueEntry.Status.PARSING) |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2282 | |
| 2283 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2284 | class PostJobTask(AgentTask): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2285 | def __init__(self, queue_entries, log_file_name): |
| 2286 | super(PostJobTask, self).__init__(log_file_name=log_file_name) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2287 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2288 | self.queue_entries = queue_entries |
| 2289 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2290 | self._autoserv_monitor = PidfileRunMonitor() |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2291 | self._autoserv_monitor.attach_to_existing_process( |
| 2292 | self._working_directory()) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2293 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2294 | |
| 2295 | def _command_line(self): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2296 | if _testing_mode: |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2297 | return 'true' |
| 2298 | return self._generate_command( |
| 2299 | _drone_manager.absolute_path(self._working_directory())) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2300 | |
| 2301 | |
| 2302 | def _generate_command(self, results_dir): |
| 2303 | raise NotImplementedError('Subclasses must override this') |
| 2304 | |
| 2305 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2306 | @property |
| 2307 | def owner_username(self): |
| 2308 | return self.queue_entries[0].job.owner |
| 2309 | |
| 2310 | |
| 2311 | def _working_directory(self): |
| 2312 | return self._get_consistent_execution_path(self.queue_entries) |
| 2313 | |
| 2314 | |
| 2315 | def _paired_with_monitor(self): |
| 2316 | return self._autoserv_monitor |
| 2317 | |
| 2318 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2319 | def _job_was_aborted(self): |
| 2320 | was_aborted = None |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2321 | for queue_entry in self.queue_entries: |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2322 | queue_entry.update_from_database() |
| 2323 | if was_aborted is None: # first queue entry |
| 2324 | was_aborted = bool(queue_entry.aborted) |
| 2325 | elif was_aborted != bool(queue_entry.aborted): # subsequent entries |
jamesren | 17cadd6 | 2010-06-16 23:26:55 +0000 | [diff] [blame] | 2326 | entries = ['%s (aborted: %s)' % (entry, entry.aborted) |
| 2327 | for entry in self.queue_entries] |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2328 | email_manager.manager.enqueue_notify_email( |
jamesren | 17cadd6 | 2010-06-16 23:26:55 +0000 | [diff] [blame] | 2329 | 'Inconsistent abort state', |
| 2330 | 'Queue entries have inconsistent abort state:\n' + |
| 2331 | '\n'.join(entries)) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2332 | # don't crash here, just assume true |
| 2333 | return True |
| 2334 | return was_aborted |
| 2335 | |
| 2336 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2337 | def _final_status(self): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2338 | if self._job_was_aborted(): |
| 2339 | return models.HostQueueEntry.Status.ABORTED |
| 2340 | |
| 2341 | # we'll use a PidfileRunMonitor to read the autoserv exit status |
| 2342 | if self._autoserv_monitor.exit_code() == 0: |
| 2343 | return models.HostQueueEntry.Status.COMPLETED |
| 2344 | return models.HostQueueEntry.Status.FAILED |
| 2345 | |
| 2346 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2347 | def _set_all_statuses(self, status): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2348 | for queue_entry in self.queue_entries: |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2349 | queue_entry.set_status(status) |
| 2350 | |
| 2351 | |
| 2352 | def abort(self): |
| 2353 | # override AgentTask.abort() to avoid killing the process and ending |
| 2354 | # the task. post-job tasks continue when the job is aborted. |
| 2355 | pass |
| 2356 | |
| 2357 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2358 | def _pidfile_label(self): |
| 2359 | # '.autoserv_execute' -> 'autoserv' |
| 2360 | return self._pidfile_name()[1:-len('_execute')] |
| 2361 | |
| 2362 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2363 | class GatherLogsTask(PostJobTask): |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2364 | """ |
| 2365 | Task responsible for |
| 2366 | * gathering uncollected logs (if Autoserv crashed hard or was killed) |
| 2367 | * copying logs to the results repository |
| 2368 | * spawning CleanupTasks for hosts, if necessary |
| 2369 | * spawning a FinalReparseTask for the job |
| 2370 | """ |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2371 | def __init__(self, queue_entries, recover_run_monitor=None): |
| 2372 | self._job = queue_entries[0].job |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2373 | super(GatherLogsTask, self).__init__( |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2374 | queue_entries, log_file_name='.collect_crashinfo.log') |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2375 | self._set_ids(queue_entries=queue_entries) |
| 2376 | |
| 2377 | |
| 2378 | def _generate_command(self, results_dir): |
| 2379 | host_list = ','.join(queue_entry.host.hostname |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2380 | for queue_entry in self.queue_entries) |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2381 | return [_autoserv_path , '-p', |
| 2382 | '--pidfile-label=%s' % self._pidfile_label(), |
| 2383 | '--use-existing-results', '--collect-crashinfo', |
| 2384 | '-m', host_list, '-r', results_dir] |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2385 | |
| 2386 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2387 | @property |
| 2388 | def num_processes(self): |
| 2389 | return len(self.queue_entries) |
| 2390 | |
| 2391 | |
| 2392 | def _pidfile_name(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 2393 | return drone_manager.CRASHINFO_PID_FILE |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2394 | |
| 2395 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2396 | def prolog(self): |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2397 | self._check_queue_entry_statuses( |
| 2398 | self.queue_entries, |
| 2399 | allowed_hqe_statuses=(models.HostQueueEntry.Status.GATHERING,), |
| 2400 | allowed_host_statuses=(models.Host.Status.RUNNING,)) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2401 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2402 | super(GatherLogsTask, self).prolog() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2403 | |
| 2404 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2405 | def epilog(self): |
| 2406 | super(GatherLogsTask, self).epilog() |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2407 | self._parse_results(self.queue_entries) |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2408 | self._reboot_hosts() |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 2409 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2410 | |
| 2411 | def _reboot_hosts(self): |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 2412 | if self._autoserv_monitor.has_process(): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2413 | final_success = (self._final_status() == |
showard | 6d1c143 | 2009-08-20 23:30:39 +0000 | [diff] [blame] | 2414 | models.HostQueueEntry.Status.COMPLETED) |
| 2415 | num_tests_failed = self._autoserv_monitor.num_tests_failed() |
| 2416 | else: |
| 2417 | final_success = False |
| 2418 | num_tests_failed = 0 |
| 2419 | |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2420 | reboot_after = self._job.reboot_after |
| 2421 | do_reboot = ( |
| 2422 | # always reboot after aborted jobs |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2423 | self._final_status() == models.HostQueueEntry.Status.ABORTED |
jamesren | dd85524 | 2010-03-02 22:23:44 +0000 | [diff] [blame] | 2424 | or reboot_after == model_attributes.RebootAfter.ALWAYS |
| 2425 | or (reboot_after == model_attributes.RebootAfter.IF_ALL_TESTS_PASSED |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2426 | and final_success and num_tests_failed == 0)) |
| 2427 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2428 | for queue_entry in self.queue_entries: |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 2429 | if do_reboot: |
| 2430 | # don't pass the queue entry to the CleanupTask. if the cleanup |
| 2431 | # fails, the job doesn't care -- it's over. |
| 2432 | models.SpecialTask.objects.create( |
| 2433 | host=models.Host.objects.get(id=queue_entry.host.id), |
| 2434 | task=models.SpecialTask.Task.CLEANUP, |
| 2435 | requested_by=self._job.owner_model()) |
| 2436 | else: |
| 2437 | queue_entry.host.set_status(models.Host.Status.READY) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 2438 | |
| 2439 | |
showard | 0bbfc21 | 2009-04-29 21:06:13 +0000 | [diff] [blame] | 2440 | def run(self): |
showard | 597bfd3 | 2009-05-08 18:22:50 +0000 | [diff] [blame] | 2441 | autoserv_exit_code = self._autoserv_monitor.exit_code() |
| 2442 | # only run if Autoserv exited due to some signal. if we have no exit |
| 2443 | # code, assume something bad (and signal-like) happened. |
| 2444 | if autoserv_exit_code is None or os.WIFSIGNALED(autoserv_exit_code): |
showard | 0bbfc21 | 2009-04-29 21:06:13 +0000 | [diff] [blame] | 2445 | super(GatherLogsTask, self).run() |
showard | 597bfd3 | 2009-05-08 18:22:50 +0000 | [diff] [blame] | 2446 | else: |
| 2447 | self.finished(True) |
showard | 0bbfc21 | 2009-04-29 21:06:13 +0000 | [diff] [blame] | 2448 | |
| 2449 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2450 | class SelfThrottledPostJobTask(PostJobTask): |
| 2451 | """ |
| 2452 | Special AgentTask subclass that maintains its own global process limit. |
| 2453 | """ |
| 2454 | _num_running_processes = 0 |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 2455 | |
| 2456 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2457 | @classmethod |
| 2458 | def _increment_running_processes(cls): |
| 2459 | cls._num_running_processes += 1 |
mbligh | 16c722d | 2008-03-05 00:58:44 +0000 | [diff] [blame] | 2460 | |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 2461 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2462 | @classmethod |
| 2463 | def _decrement_running_processes(cls): |
| 2464 | cls._num_running_processes -= 1 |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2465 | |
| 2466 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2467 | @classmethod |
| 2468 | def _max_processes(cls): |
| 2469 | raise NotImplementedError |
| 2470 | |
| 2471 | |
| 2472 | @classmethod |
| 2473 | def _can_run_new_process(cls): |
| 2474 | return cls._num_running_processes < cls._max_processes() |
| 2475 | |
| 2476 | |
| 2477 | def _process_started(self): |
| 2478 | return bool(self.monitor) |
| 2479 | |
| 2480 | |
| 2481 | def tick(self): |
| 2482 | # override tick to keep trying to start until the process count goes |
| 2483 | # down and we can, at which point we revert to default behavior |
| 2484 | if self._process_started(): |
| 2485 | super(SelfThrottledPostJobTask, self).tick() |
| 2486 | else: |
| 2487 | self._try_starting_process() |
| 2488 | |
| 2489 | |
| 2490 | def run(self): |
| 2491 | # override run() to not actually run unless we can |
| 2492 | self._try_starting_process() |
| 2493 | |
| 2494 | |
| 2495 | def _try_starting_process(self): |
| 2496 | if not self._can_run_new_process(): |
showard | 775300b | 2009-09-09 15:30:50 +0000 | [diff] [blame] | 2497 | return |
| 2498 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2499 | # actually run the command |
| 2500 | super(SelfThrottledPostJobTask, self).run() |
jamesren | 2566356 | 2010-04-27 18:00:55 +0000 | [diff] [blame] | 2501 | if self._process_started(): |
| 2502 | self._increment_running_processes() |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 2503 | |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 2504 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2505 | def finished(self, success): |
| 2506 | super(SelfThrottledPostJobTask, self).finished(success) |
| 2507 | if self._process_started(): |
| 2508 | self._decrement_running_processes() |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2509 | |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 2510 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2511 | class FinalReparseTask(SelfThrottledPostJobTask): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2512 | def __init__(self, queue_entries): |
| 2513 | super(FinalReparseTask, self).__init__(queue_entries, |
| 2514 | log_file_name='.parse.log') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 2515 | # don't use _set_ids, since we don't want to set the host_ids |
| 2516 | self.queue_entry_ids = [entry.id for entry in queue_entries] |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2517 | |
| 2518 | |
| 2519 | def _generate_command(self, results_dir): |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2520 | return [_parser_path, '--write-pidfile', '-l', '2', '-r', '-o', |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2521 | results_dir] |
| 2522 | |
| 2523 | |
| 2524 | @property |
| 2525 | def num_processes(self): |
| 2526 | return 0 # don't include parser processes in accounting |
| 2527 | |
| 2528 | |
| 2529 | def _pidfile_name(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 2530 | return drone_manager.PARSER_PID_FILE |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 2531 | |
| 2532 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2533 | @classmethod |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2534 | def _max_processes(cls): |
| 2535 | return scheduler_config.config.max_parse_processes |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2536 | |
| 2537 | |
| 2538 | def prolog(self): |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2539 | self._check_queue_entry_statuses( |
| 2540 | self.queue_entries, |
| 2541 | allowed_hqe_statuses=(models.HostQueueEntry.Status.PARSING,)) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 2542 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2543 | super(FinalReparseTask, self).prolog() |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2544 | |
| 2545 | |
| 2546 | def epilog(self): |
| 2547 | super(FinalReparseTask, self).epilog() |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2548 | self._archive_results(self.queue_entries) |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2549 | |
| 2550 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2551 | class ArchiveResultsTask(SelfThrottledPostJobTask): |
showard | e1575b5 | 2010-01-15 00:21:12 +0000 | [diff] [blame] | 2552 | _ARCHIVING_FAILED_FILE = '.archiver_failed' |
| 2553 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2554 | def __init__(self, queue_entries): |
| 2555 | super(ArchiveResultsTask, self).__init__(queue_entries, |
| 2556 | log_file_name='.archiving.log') |
| 2557 | # don't use _set_ids, since we don't want to set the host_ids |
| 2558 | self.queue_entry_ids = [entry.id for entry in queue_entries] |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2559 | |
| 2560 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2561 | def _pidfile_name(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 2562 | return drone_manager.ARCHIVER_PID_FILE |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2563 | |
| 2564 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2565 | def _generate_command(self, results_dir): |
| 2566 | return [_autoserv_path , '-p', |
| 2567 | '--pidfile-label=%s' % self._pidfile_label(), '-r', results_dir, |
mbligh | e0cbc91 | 2010-03-11 18:03:07 +0000 | [diff] [blame] | 2568 | '--use-existing-results', '--control-filename=control.archive', |
showard | 948eb30 | 2010-01-15 00:16:20 +0000 | [diff] [blame] | 2569 | os.path.join(drones.AUTOTEST_INSTALL_DIR, 'scheduler', |
| 2570 | 'archive_results.control.srv')] |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 2571 | |
| 2572 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2573 | @classmethod |
| 2574 | def _max_processes(cls): |
| 2575 | return scheduler_config.config.max_transfer_processes |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 2576 | |
| 2577 | |
| 2578 | def prolog(self): |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2579 | self._check_queue_entry_statuses( |
| 2580 | self.queue_entries, |
| 2581 | allowed_hqe_statuses=(models.HostQueueEntry.Status.ARCHIVING,)) |
| 2582 | |
| 2583 | super(ArchiveResultsTask, self).prolog() |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 2584 | |
| 2585 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2586 | def epilog(self): |
| 2587 | super(ArchiveResultsTask, self).epilog() |
showard | 4076c63 | 2010-01-15 20:28:49 +0000 | [diff] [blame] | 2588 | if not self.success and self._paired_with_monitor().has_process(): |
showard | e1575b5 | 2010-01-15 00:21:12 +0000 | [diff] [blame] | 2589 | failed_file = os.path.join(self._working_directory(), |
| 2590 | self._ARCHIVING_FAILED_FILE) |
| 2591 | paired_process = self._paired_with_monitor().get_process() |
| 2592 | _drone_manager.write_lines_to_file( |
| 2593 | failed_file, ['Archiving failed with exit code %s' |
| 2594 | % self.monitor.exit_code()], |
| 2595 | paired_with_process=paired_process) |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 2596 | self._set_all_statuses(self._final_status()) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 2597 | |
| 2598 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 2599 | if __name__ == '__main__': |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 2600 | main() |