mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1 | #!/usr/bin/python -u |
Aviv Keshet | 225bdfe | 2013-03-05 10:10:08 -0800 | [diff] [blame] | 2 | #pylint: disable-msg=C0111 |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 3 | |
| 4 | """ |
| 5 | Autotest scheduler |
| 6 | """ |
showard | 909c7a6 | 2008-07-15 21:52:38 +0000 | [diff] [blame] | 7 | |
Aviv Keshet | 225bdfe | 2013-03-05 10:10:08 -0800 | [diff] [blame] | 8 | import datetime, optparse, os, signal |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 9 | import sys, time |
Aviv Keshet | 225bdfe | 2013-03-05 10:10:08 -0800 | [diff] [blame] | 10 | import logging, gc |
showard | 402934a | 2009-12-21 22:20:47 +0000 | [diff] [blame] | 11 | |
Alex Miller | 05d7b4c | 2013-03-04 07:49:38 -0800 | [diff] [blame] | 12 | import common |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 13 | from autotest_lib.frontend import setup_django_environment |
showard | 402934a | 2009-12-21 22:20:47 +0000 | [diff] [blame] | 14 | |
| 15 | import django.db |
| 16 | |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 17 | from autotest_lib.client.common_lib import global_config, logging_manager |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 18 | from autotest_lib.client.common_lib import utils |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 19 | from autotest_lib.database import database_connection |
Alex Miller | 05d7b4c | 2013-03-04 07:49:38 -0800 | [diff] [blame] | 20 | from autotest_lib.frontend.afe import models, rpc_utils, readonly_connection |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 21 | from autotest_lib.scheduler import agent_task, drone_manager, drones |
| 22 | from autotest_lib.scheduler import email_manager, gc_stats, host_scheduler |
| 23 | from autotest_lib.scheduler import monitor_db_cleanup, prejob_task |
| 24 | from autotest_lib.scheduler import postjob_task, scheduler_logging_config |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 25 | from autotest_lib.scheduler import scheduler_models |
Alex Miller | 05d7b4c | 2013-03-04 07:49:38 -0800 | [diff] [blame] | 26 | from autotest_lib.scheduler import status_server, scheduler_config |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 27 | from autotest_lib.server import autoserv_utils |
Fang Deng | 1d6c2a0 | 2013-04-17 15:25:45 -0700 | [diff] [blame] | 28 | from autotest_lib.site_utils.graphite import stats |
Alex Miller | 05d7b4c | 2013-03-04 07:49:38 -0800 | [diff] [blame] | 29 | |
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 = '.' |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 34 | DB_CONFIG_SECTION = 'AUTOTEST_WEB' |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 35 | AUTOTEST_PATH = os.path.join(os.path.dirname(__file__), '..') |
| 36 | |
| 37 | if os.environ.has_key('AUTOTEST_DIR'): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 38 | AUTOTEST_PATH = os.environ['AUTOTEST_DIR'] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 39 | AUTOTEST_SERVER_DIR = os.path.join(AUTOTEST_PATH, 'server') |
| 40 | AUTOTEST_TKO_DIR = os.path.join(AUTOTEST_PATH, 'tko') |
| 41 | |
| 42 | if AUTOTEST_SERVER_DIR not in sys.path: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 43 | sys.path.insert(0, AUTOTEST_SERVER_DIR) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 44 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 45 | # error message to leave in results dir when an autoserv process disappears |
| 46 | # mysteriously |
| 47 | _LOST_PROCESS_ERROR = """\ |
| 48 | Autoserv failed abnormally during execution for this job, probably due to a |
| 49 | system error on the Autotest server. Full results may not be available. Sorry. |
| 50 | """ |
| 51 | |
mbligh | 6f8bab4 | 2008-02-29 22:45:14 +0000 | [diff] [blame] | 52 | _db = None |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 53 | _shutdown = False |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 54 | |
| 55 | # These 2 globals are replaced for testing |
| 56 | _autoserv_directory = autoserv_utils.autoserv_directory |
| 57 | _autoserv_path = autoserv_utils.autoserv_path |
mbligh | 4314a71 | 2008-02-29 22:44:30 +0000 | [diff] [blame] | 58 | _testing_mode = False |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 59 | _drone_manager = None |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 60 | |
Eric Li | e0493a4 | 2010-11-15 13:05:43 -0800 | [diff] [blame] | 61 | def _parser_path_default(install_dir): |
| 62 | return os.path.join(install_dir, 'tko', 'parse') |
| 63 | _parser_path_func = utils.import_site_function( |
| 64 | __file__, 'autotest_lib.scheduler.site_monitor_db', |
| 65 | 'parser_path', _parser_path_default) |
| 66 | _parser_path = _parser_path_func(drones.AUTOTEST_INSTALL_DIR) |
| 67 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 68 | |
mbligh | 83c1e9e | 2009-05-01 23:10:41 +0000 | [diff] [blame] | 69 | def _site_init_monitor_db_dummy(): |
| 70 | return {} |
| 71 | |
| 72 | |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 73 | def _verify_default_drone_set_exists(): |
| 74 | if (models.DroneSet.drone_sets_enabled() and |
| 75 | not models.DroneSet.default_drone_set_name()): |
Dale Curtis | aa51336 | 2011-03-01 17:27:44 -0800 | [diff] [blame] | 76 | raise host_scheduler.SchedulerError( |
| 77 | 'Drone sets are enabled, but no default is set') |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | def _sanity_check(): |
| 81 | """Make sure the configs are consistent before starting the scheduler""" |
| 82 | _verify_default_drone_set_exists() |
| 83 | |
| 84 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 85 | def main(): |
showard | 27f3387 | 2009-04-07 18:20:53 +0000 | [diff] [blame] | 86 | try: |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 87 | try: |
| 88 | main_without_exception_handling() |
| 89 | except SystemExit: |
| 90 | raise |
| 91 | except: |
| 92 | logging.exception('Exception escaping in monitor_db') |
| 93 | raise |
| 94 | finally: |
| 95 | utils.delete_pid_file_if_exists(PID_FILE_PREFIX) |
showard | 27f3387 | 2009-04-07 18:20:53 +0000 | [diff] [blame] | 96 | |
| 97 | |
| 98 | def main_without_exception_handling(): |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 99 | setup_logging() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 100 | |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 101 | usage = 'usage: %prog [options] results_dir' |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 102 | parser = optparse.OptionParser(usage) |
| 103 | parser.add_option('--recover-hosts', help='Try to recover dead hosts', |
| 104 | action='store_true') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 105 | parser.add_option('--test', help='Indicate that scheduler is under ' + |
| 106 | 'test and should use dummy autoserv and no parsing', |
| 107 | action='store_true') |
| 108 | (options, args) = parser.parse_args() |
| 109 | if len(args) != 1: |
| 110 | parser.print_usage() |
| 111 | return |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 112 | |
showard | 5613c66 | 2009-06-08 23:30:33 +0000 | [diff] [blame] | 113 | scheduler_enabled = global_config.global_config.get_config_value( |
| 114 | scheduler_config.CONFIG_SECTION, 'enable_scheduler', type=bool) |
| 115 | |
| 116 | if not scheduler_enabled: |
Aviv Keshet | 05c08ac | 2013-01-10 16:11:44 -0800 | [diff] [blame] | 117 | logging.error("Scheduler not enabled, set enable_scheduler to true in " |
| 118 | "the global_config's SCHEDULER section to enable it. " |
| 119 | "Exiting.") |
showard | 5613c66 | 2009-06-08 23:30:33 +0000 | [diff] [blame] | 120 | sys.exit(1) |
| 121 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 122 | global RESULTS_DIR |
| 123 | RESULTS_DIR = args[0] |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 124 | |
mbligh | 83c1e9e | 2009-05-01 23:10:41 +0000 | [diff] [blame] | 125 | site_init = utils.import_site_function(__file__, |
| 126 | "autotest_lib.scheduler.site_monitor_db", "site_init_monitor_db", |
| 127 | _site_init_monitor_db_dummy) |
| 128 | site_init() |
| 129 | |
showard | cca334f | 2009-03-12 20:38:34 +0000 | [diff] [blame] | 130 | # Change the cwd while running to avoid issues incase we were launched from |
| 131 | # somewhere odd (such as a random NFS home directory of the person running |
| 132 | # sudo to launch us as the appropriate user). |
| 133 | os.chdir(RESULTS_DIR) |
| 134 | |
jamesren | c7d387e | 2010-08-10 21:48:30 +0000 | [diff] [blame] | 135 | # This is helpful for debugging why stuff a scheduler launches is |
| 136 | # misbehaving. |
| 137 | logging.info('os.environ: %s', os.environ) |
showard | c85c21b | 2008-11-24 22:17:37 +0000 | [diff] [blame] | 138 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 139 | if options.test: |
| 140 | global _autoserv_path |
| 141 | _autoserv_path = 'autoserv_dummy' |
| 142 | global _testing_mode |
| 143 | _testing_mode = True |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 144 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 145 | server = status_server.StatusServer() |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 146 | server.start() |
| 147 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 148 | try: |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 149 | initialize() |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 150 | dispatcher = Dispatcher() |
showard | 915958d | 2009-04-22 21:00:58 +0000 | [diff] [blame] | 151 | dispatcher.initialize(recover_hosts=options.recover_hosts) |
showard | c5afc46 | 2009-01-13 00:09:39 +0000 | [diff] [blame] | 152 | |
Eric Li | a82dc35 | 2011-02-23 13:15:52 -0800 | [diff] [blame] | 153 | while not _shutdown and not server._shutdown_scheduler: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 154 | dispatcher.tick() |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 155 | time.sleep(scheduler_config.config.tick_pause_sec) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 156 | except: |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 157 | email_manager.manager.log_stacktrace( |
| 158 | "Uncaught exception; terminating monitor_db") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 159 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 160 | email_manager.manager.send_queued_emails() |
showard | 55b4b54 | 2009-01-08 23:30:30 +0000 | [diff] [blame] | 161 | server.shutdown() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 162 | _drone_manager.shutdown() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 163 | _db.disconnect() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 164 | |
| 165 | |
showard | 136e6dc | 2009-06-10 19:38:49 +0000 | [diff] [blame] | 166 | def setup_logging(): |
| 167 | log_dir = os.environ.get('AUTOTEST_SCHEDULER_LOG_DIR', None) |
| 168 | log_name = os.environ.get('AUTOTEST_SCHEDULER_LOG_NAME', None) |
| 169 | logging_manager.configure_logging( |
| 170 | scheduler_logging_config.SchedulerLoggingConfig(), log_dir=log_dir, |
| 171 | logfile_name=log_name) |
| 172 | |
| 173 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 174 | def handle_sigint(signum, frame): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 175 | global _shutdown |
| 176 | _shutdown = True |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 177 | logging.info("Shutdown request received.") |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 178 | |
| 179 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 180 | def initialize(): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 181 | logging.info("%s> dispatcher starting", time.strftime("%X %x")) |
| 182 | logging.info("My PID is %d", os.getpid()) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 183 | |
showard | 8de3713 | 2009-08-31 18:33:08 +0000 | [diff] [blame] | 184 | if utils.program_is_alive(PID_FILE_PREFIX): |
showard | 549afad | 2009-08-20 23:33:36 +0000 | [diff] [blame] | 185 | logging.critical("monitor_db already running, aborting!") |
| 186 | sys.exit(1) |
| 187 | utils.write_pid(PID_FILE_PREFIX) |
mbligh | fb67603 | 2009-04-01 18:25:38 +0000 | [diff] [blame] | 188 | |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 189 | if _testing_mode: |
| 190 | global_config.global_config.override_config_value( |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 191 | DB_CONFIG_SECTION, 'database', 'stresstest_autotest_web') |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 192 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 193 | os.environ['PATH'] = AUTOTEST_SERVER_DIR + ':' + os.environ['PATH'] |
| 194 | global _db |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 195 | _db = database_connection.DatabaseConnection(DB_CONFIG_SECTION) |
showard | b21b8c8 | 2009-12-07 19:39:39 +0000 | [diff] [blame] | 196 | _db.connect(db_type='django') |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 197 | |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 198 | # ensure Django connection is in autocommit |
| 199 | setup_django_environment.enable_autocommit() |
showard | 844960a | 2009-05-29 18:41:18 +0000 | [diff] [blame] | 200 | # bypass the readonly connection |
| 201 | readonly_connection.ReadOnlyConnection.set_globally_disabled(True) |
showard | fa8629c | 2008-11-04 16:51:23 +0000 | [diff] [blame] | 202 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 203 | logging.info("Setting signal handler") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 204 | signal.signal(signal.SIGINT, handle_sigint) |
| 205 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 206 | initialize_globals() |
| 207 | scheduler_models.initialize() |
| 208 | |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 209 | drones = global_config.global_config.get_config_value( |
| 210 | scheduler_config.CONFIG_SECTION, 'drones', default='localhost') |
| 211 | drone_list = [hostname.strip() for hostname in drones.split(',')] |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 212 | results_host = global_config.global_config.get_config_value( |
showard | d1ee1dd | 2009-01-07 21:33:08 +0000 | [diff] [blame] | 213 | scheduler_config.CONFIG_SECTION, 'results_host', default='localhost') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 214 | _drone_manager.initialize(RESULTS_DIR, drone_list, results_host) |
| 215 | |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 216 | logging.info("Connected! Running...") |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 217 | |
| 218 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 219 | def initialize_globals(): |
| 220 | global _drone_manager |
| 221 | _drone_manager = drone_manager.instance() |
| 222 | |
| 223 | |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 224 | def _autoserv_command_line(machines, extra_args, job=None, queue_entry=None, |
| 225 | verbose=True): |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 226 | """ |
| 227 | @returns The autoserv command line as a list of executable + parameters. |
| 228 | |
| 229 | @param machines - string - A machine or comma separated list of machines |
| 230 | for the (-m) flag. |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 231 | @param extra_args - list - Additional arguments to pass to autoserv. |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 232 | @param job - Job object - If supplied, -u owner, -l name, --test-retry, |
| 233 | and client -c or server -s parameters will be added. |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 234 | @param queue_entry - A HostQueueEntry object - If supplied and no Job |
| 235 | object was supplied, this will be used to lookup the Job object. |
| 236 | """ |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 237 | return autoserv_utils.autoserv_run_job_command(_autoserv_directory, |
| 238 | machines, results_directory=drone_manager.WORKING_DIRECTORY, |
| 239 | extra_args=extra_args, job=job, queue_entry=queue_entry, |
| 240 | verbose=verbose) |
showard | 87ba02a | 2009-04-20 19:37:32 +0000 | [diff] [blame] | 241 | |
| 242 | |
Simran Basi | a858a23 | 2012-08-21 11:04:37 -0700 | [diff] [blame] | 243 | class BaseDispatcher(object): |
Alex Miller | 05d7b4c | 2013-03-04 07:49:38 -0800 | [diff] [blame] | 244 | |
| 245 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 246 | def __init__(self): |
| 247 | self._agents = [] |
showard | 3bb499f | 2008-07-03 19:42:20 +0000 | [diff] [blame] | 248 | self._last_clean_time = time.time() |
Dale Curtis | aa51336 | 2011-03-01 17:27:44 -0800 | [diff] [blame] | 249 | self._host_scheduler = host_scheduler.HostScheduler(_db) |
mbligh | f3294cc | 2009-04-08 21:17:38 +0000 | [diff] [blame] | 250 | user_cleanup_time = scheduler_config.config.clean_interval |
| 251 | self._periodic_cleanup = monitor_db_cleanup.UserCleanup( |
| 252 | _db, user_cleanup_time) |
| 253 | self._24hr_upkeep = monitor_db_cleanup.TwentyFourHourUpkeep(_db) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 254 | self._host_agents = {} |
| 255 | self._queue_entry_agents = {} |
showard | f13a9e2 | 2009-12-18 22:54:09 +0000 | [diff] [blame] | 256 | self._tick_count = 0 |
| 257 | self._last_garbage_stats_time = time.time() |
| 258 | self._seconds_between_garbage_stats = 60 * ( |
| 259 | global_config.global_config.get_config_value( |
| 260 | scheduler_config.CONFIG_SECTION, |
Dale Curtis | 456d3c1 | 2011-07-19 11:42:51 -0700 | [diff] [blame] | 261 | 'gc_stats_interval_mins', type=int, default=6*60)) |
Simran Basi | 0ec94dd | 2012-08-28 09:50:10 -0700 | [diff] [blame] | 262 | self._tick_debug = global_config.global_config.get_config_value( |
| 263 | scheduler_config.CONFIG_SECTION, 'tick_debug', type=bool, |
| 264 | default=False) |
Simran Basi | def9287 | 2012-09-20 13:34:34 -0700 | [diff] [blame] | 265 | self._extra_debugging = global_config.global_config.get_config_value( |
| 266 | scheduler_config.CONFIG_SECTION, 'extra_debugging', type=bool, |
| 267 | default=False) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 268 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 269 | |
showard | 915958d | 2009-04-22 21:00:58 +0000 | [diff] [blame] | 270 | def initialize(self, recover_hosts=True): |
| 271 | self._periodic_cleanup.initialize() |
| 272 | self._24hr_upkeep.initialize() |
| 273 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 274 | # always recover processes |
| 275 | self._recover_processes() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 276 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 277 | if recover_hosts: |
| 278 | self._recover_hosts() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 279 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 280 | self._host_scheduler.recovery_on_startup() |
| 281 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 282 | |
Simran Basi | 0ec94dd | 2012-08-28 09:50:10 -0700 | [diff] [blame] | 283 | def _log_tick_msg(self, msg): |
| 284 | if self._tick_debug: |
| 285 | logging.debug(msg) |
| 286 | |
| 287 | |
Simran Basi | def9287 | 2012-09-20 13:34:34 -0700 | [diff] [blame] | 288 | def _log_extra_msg(self, msg): |
| 289 | if self._extra_debugging: |
| 290 | logging.debug(msg) |
| 291 | |
| 292 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 293 | def tick(self): |
Simran Basi | 0ec94dd | 2012-08-28 09:50:10 -0700 | [diff] [blame] | 294 | """ |
| 295 | This is an altered version of tick() where we keep track of when each |
| 296 | major step begins so we can try to figure out where we are using most |
| 297 | of the tick time. |
| 298 | """ |
Fang Deng | 1d6c2a0 | 2013-04-17 15:25:45 -0700 | [diff] [blame] | 299 | timer = stats.Timer('scheduler.tick') |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 300 | self._log_tick_msg('Calling new tick, starting garbage collection().') |
showard | f13a9e2 | 2009-12-18 22:54:09 +0000 | [diff] [blame] | 301 | self._garbage_collection() |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 302 | self._log_tick_msg('Calling _drone_manager.refresh().') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 303 | _drone_manager.refresh() |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 304 | self._log_tick_msg('Calling _run_cleanup().') |
mbligh | f3294cc | 2009-04-08 21:17:38 +0000 | [diff] [blame] | 305 | self._run_cleanup() |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 306 | self._log_tick_msg('Calling _find_aborting().') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 307 | self._find_aborting() |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 308 | self._log_tick_msg('Calling _find_aborted_special_tasks().') |
| 309 | self._find_aborted_special_tasks() |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 310 | self._log_tick_msg('Calling _process_recurring_runs().') |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 311 | self._process_recurring_runs() |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 312 | self._log_tick_msg('Calling _schedule_delay_tasks().') |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 313 | self._schedule_delay_tasks() |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 314 | self._log_tick_msg('Calling _schedule_running_host_queue_entries().') |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 315 | self._schedule_running_host_queue_entries() |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 316 | self._log_tick_msg('Calling _schedule_special_tasks().') |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 317 | self._schedule_special_tasks() |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 318 | self._log_tick_msg('Calling _schedule_new_jobs().') |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 319 | self._schedule_new_jobs() |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 320 | self._log_tick_msg('Calling _handle_agents().') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 321 | self._handle_agents() |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 322 | self._log_tick_msg('Calling _host_scheduler.tick().') |
jamesren | e21bf41 | 2010-02-26 02:30:07 +0000 | [diff] [blame] | 323 | self._host_scheduler.tick() |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 324 | self._log_tick_msg('Calling _drone_manager.execute_actions().') |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 325 | _drone_manager.execute_actions() |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 326 | self._log_tick_msg('Calling ' |
Simran Basi | 0ec94dd | 2012-08-28 09:50:10 -0700 | [diff] [blame] | 327 | 'email_manager.manager.send_queued_emails().') |
Fang Deng | 1d6c2a0 | 2013-04-17 15:25:45 -0700 | [diff] [blame] | 328 | with timer.get_client('email_manager_send_queued_emails'): |
| 329 | email_manager.manager.send_queued_emails() |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 330 | self._log_tick_msg('Calling django.db.reset_queries().') |
Fang Deng | 1d6c2a0 | 2013-04-17 15:25:45 -0700 | [diff] [blame] | 331 | with timer.get_client('django_db_reset_queries'): |
| 332 | django.db.reset_queries() |
showard | f13a9e2 | 2009-12-18 22:54:09 +0000 | [diff] [blame] | 333 | self._tick_count += 1 |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 334 | |
showard | 97aed50 | 2008-11-04 02:01:24 +0000 | [diff] [blame] | 335 | |
mbligh | f3294cc | 2009-04-08 21:17:38 +0000 | [diff] [blame] | 336 | def _run_cleanup(self): |
| 337 | self._periodic_cleanup.run_cleanup_maybe() |
| 338 | self._24hr_upkeep.run_cleanup_maybe() |
showard | a3ab0d5 | 2008-11-03 19:03:47 +0000 | [diff] [blame] | 339 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 340 | |
showard | f13a9e2 | 2009-12-18 22:54:09 +0000 | [diff] [blame] | 341 | def _garbage_collection(self): |
| 342 | threshold_time = time.time() - self._seconds_between_garbage_stats |
| 343 | if threshold_time < self._last_garbage_stats_time: |
| 344 | # Don't generate these reports very often. |
| 345 | return |
| 346 | |
| 347 | self._last_garbage_stats_time = time.time() |
| 348 | # Force a full level 0 collection (because we can, it doesn't hurt |
| 349 | # at this interval). |
| 350 | gc.collect() |
| 351 | logging.info('Logging garbage collector stats on tick %d.', |
| 352 | self._tick_count) |
| 353 | gc_stats._log_garbage_collector_stats() |
| 354 | |
| 355 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 356 | def _register_agent_for_ids(self, agent_dict, object_ids, agent): |
| 357 | for object_id in object_ids: |
| 358 | agent_dict.setdefault(object_id, set()).add(agent) |
| 359 | |
| 360 | |
| 361 | def _unregister_agent_for_ids(self, agent_dict, object_ids, agent): |
| 362 | for object_id in object_ids: |
| 363 | assert object_id in agent_dict |
| 364 | agent_dict[object_id].remove(agent) |
Dan Shi | 76af802 | 2013-10-19 01:59:49 -0700 | [diff] [blame] | 365 | # If an ID has no more active agent associated, there is no need to |
| 366 | # keep it in the dictionary. Otherwise, scheduler will keep an |
| 367 | # unnecessarily big dictionary until being restarted. |
| 368 | if not agent_dict[object_id]: |
| 369 | agent_dict.pop(object_id) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 370 | |
| 371 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 372 | def add_agent_task(self, agent_task): |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 373 | """ |
| 374 | Creates and adds an agent to the dispatchers list. |
| 375 | |
| 376 | In creating the agent we also pass on all the queue_entry_ids and |
| 377 | host_ids from the special agent task. For every agent we create, we |
| 378 | add it to 1. a dict against the queue_entry_ids given to it 2. A dict |
| 379 | against the host_ids given to it. So theoritically, a host can have any |
| 380 | number of agents associated with it, and each of them can have any |
| 381 | special agent task, though in practice we never see > 1 agent/task per |
| 382 | host at any time. |
| 383 | |
| 384 | @param agent_task: A SpecialTask for the agent to manage. |
| 385 | """ |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 386 | agent = Agent(agent_task) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 387 | self._agents.append(agent) |
| 388 | agent.dispatcher = self |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 389 | self._register_agent_for_ids(self._host_agents, agent.host_ids, agent) |
| 390 | self._register_agent_for_ids(self._queue_entry_agents, |
| 391 | agent.queue_entry_ids, agent) |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 392 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 393 | |
| 394 | def get_agents_for_entry(self, queue_entry): |
| 395 | """ |
| 396 | Find agents corresponding to the specified queue_entry. |
| 397 | """ |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 398 | return list(self._queue_entry_agents.get(queue_entry.id, set())) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 399 | |
| 400 | |
| 401 | def host_has_agent(self, host): |
| 402 | """ |
| 403 | Determine if there is currently an Agent present using this host. |
| 404 | """ |
| 405 | return bool(self._host_agents.get(host.id, None)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 406 | |
| 407 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 408 | def remove_agent(self, agent): |
| 409 | self._agents.remove(agent) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 410 | self._unregister_agent_for_ids(self._host_agents, agent.host_ids, |
| 411 | agent) |
| 412 | self._unregister_agent_for_ids(self._queue_entry_agents, |
| 413 | agent.queue_entry_ids, agent) |
showard | ec11316 | 2008-05-08 00:52:49 +0000 | [diff] [blame] | 414 | |
| 415 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 416 | def _host_has_scheduled_special_task(self, host): |
| 417 | return bool(models.SpecialTask.objects.filter(host__id=host.id, |
| 418 | is_active=False, |
| 419 | is_complete=False)) |
| 420 | |
| 421 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 422 | def _recover_processes(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 423 | agent_tasks = self._create_recovery_agent_tasks() |
| 424 | self._register_pidfiles(agent_tasks) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 425 | _drone_manager.refresh() |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 426 | self._recover_tasks(agent_tasks) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 427 | self._recover_pending_entries() |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 428 | self._check_for_unrecovered_verifying_entries() |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 429 | self._reverify_remaining_hosts() |
| 430 | # reinitialize drones after killing orphaned processes, since they can |
| 431 | # leave around files when they die |
| 432 | _drone_manager.execute_actions() |
| 433 | _drone_manager.reinitialize_drones() |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 434 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 435 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 436 | def _create_recovery_agent_tasks(self): |
| 437 | return (self._get_queue_entry_agent_tasks() |
| 438 | + self._get_special_task_agent_tasks(is_active=True)) |
| 439 | |
| 440 | |
| 441 | def _get_queue_entry_agent_tasks(self): |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 442 | """ |
| 443 | Get agent tasks for all hqe in the specified states. |
| 444 | |
| 445 | Loosely this translates to taking a hqe in one of the specified states, |
| 446 | say parsing, and getting an AgentTask for it, like the FinalReparseTask, |
| 447 | through _get_agent_task_for_queue_entry. Each queue entry can only have |
| 448 | one agent task at a time, but there might be multiple queue entries in |
| 449 | the group. |
| 450 | |
| 451 | @return: A list of AgentTasks. |
| 452 | """ |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 453 | # host queue entry statuses handled directly by AgentTasks (Verifying is |
| 454 | # handled through SpecialTasks, so is not listed here) |
| 455 | statuses = (models.HostQueueEntry.Status.STARTING, |
| 456 | models.HostQueueEntry.Status.RUNNING, |
| 457 | models.HostQueueEntry.Status.GATHERING, |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 458 | models.HostQueueEntry.Status.PARSING, |
| 459 | models.HostQueueEntry.Status.ARCHIVING) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 460 | status_list = ','.join("'%s'" % status for status in statuses) |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 461 | queue_entries = scheduler_models.HostQueueEntry.fetch( |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 462 | where='status IN (%s)' % status_list) |
Alex Miller | 47cd247 | 2013-11-25 15:20:04 -0800 | [diff] [blame^] | 463 | stats.Gauge('scheduler.jobs_per_tick').send( |
| 464 | 'running', len(queue_entries)) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 465 | |
| 466 | agent_tasks = [] |
| 467 | used_queue_entries = set() |
| 468 | for entry in queue_entries: |
| 469 | if self.get_agents_for_entry(entry): |
| 470 | # already being handled |
| 471 | continue |
| 472 | if entry in used_queue_entries: |
| 473 | # already picked up by a synchronous job |
| 474 | continue |
| 475 | agent_task = self._get_agent_task_for_queue_entry(entry) |
| 476 | agent_tasks.append(agent_task) |
| 477 | used_queue_entries.update(agent_task.queue_entries) |
| 478 | return agent_tasks |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 479 | |
| 480 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 481 | def _get_special_task_agent_tasks(self, is_active=False): |
| 482 | special_tasks = models.SpecialTask.objects.filter( |
| 483 | is_active=is_active, is_complete=False) |
| 484 | return [self._get_agent_task_for_special_task(task) |
| 485 | for task in special_tasks] |
| 486 | |
| 487 | |
| 488 | def _get_agent_task_for_queue_entry(self, queue_entry): |
| 489 | """ |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 490 | Construct an AgentTask instance for the given active HostQueueEntry. |
| 491 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 492 | @param queue_entry: a HostQueueEntry |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 493 | @return: an AgentTask to run the queue entry |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 494 | """ |
| 495 | task_entries = queue_entry.job.get_group_entries(queue_entry) |
| 496 | self._check_for_duplicate_host_entries(task_entries) |
| 497 | |
| 498 | if queue_entry.status in (models.HostQueueEntry.Status.STARTING, |
| 499 | models.HostQueueEntry.Status.RUNNING): |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 500 | if queue_entry.is_hostless(): |
| 501 | return HostlessQueueTask(queue_entry=queue_entry) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 502 | return QueueTask(queue_entries=task_entries) |
| 503 | if queue_entry.status == models.HostQueueEntry.Status.GATHERING: |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 504 | return postjob_task.GatherLogsTask(queue_entries=task_entries) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 505 | if queue_entry.status == models.HostQueueEntry.Status.PARSING: |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 506 | return postjob_task.FinalReparseTask(queue_entries=task_entries) |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 507 | if queue_entry.status == models.HostQueueEntry.Status.ARCHIVING: |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 508 | return postjob_task.ArchiveResultsTask(queue_entries=task_entries) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 509 | |
Dale Curtis | aa51336 | 2011-03-01 17:27:44 -0800 | [diff] [blame] | 510 | raise host_scheduler.SchedulerError( |
| 511 | '_get_agent_task_for_queue_entry got entry with ' |
| 512 | 'invalid status %s: %s' % (queue_entry.status, queue_entry)) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 513 | |
| 514 | |
| 515 | def _check_for_duplicate_host_entries(self, task_entries): |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 516 | non_host_statuses = (models.HostQueueEntry.Status.PARSING, |
| 517 | models.HostQueueEntry.Status.ARCHIVING) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 518 | for task_entry in task_entries: |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 519 | using_host = (task_entry.host is not None |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 520 | and task_entry.status not in non_host_statuses) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 521 | if using_host: |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 522 | self._assert_host_has_no_agent(task_entry) |
| 523 | |
| 524 | |
| 525 | def _assert_host_has_no_agent(self, entry): |
| 526 | """ |
| 527 | @param entry: a HostQueueEntry or a SpecialTask |
| 528 | """ |
| 529 | if self.host_has_agent(entry.host): |
| 530 | agent = tuple(self._host_agents.get(entry.host.id))[0] |
Dale Curtis | aa51336 | 2011-03-01 17:27:44 -0800 | [diff] [blame] | 531 | raise host_scheduler.SchedulerError( |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 532 | 'While scheduling %s, host %s already has a host agent %s' |
| 533 | % (entry, entry.host, agent.task)) |
| 534 | |
| 535 | |
| 536 | def _get_agent_task_for_special_task(self, special_task): |
| 537 | """ |
| 538 | Construct an AgentTask class to run the given SpecialTask and add it |
| 539 | to this dispatcher. |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 540 | |
| 541 | A special task is create through schedule_special_tasks, but only if |
| 542 | the host doesn't already have an agent. This happens through |
| 543 | add_agent_task. All special agent tasks are given a host on creation, |
| 544 | and a Null hqe. To create a SpecialAgentTask object, you need a |
| 545 | models.SpecialTask. If the SpecialTask used to create a SpecialAgentTask |
| 546 | object contains a hqe it's passed on to the special agent task, which |
| 547 | creates a HostQueueEntry and saves it as it's queue_entry. |
| 548 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 549 | @param special_task: a models.SpecialTask instance |
| 550 | @returns an AgentTask to run this SpecialTask |
| 551 | """ |
| 552 | self._assert_host_has_no_agent(special_task) |
| 553 | |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 554 | special_agent_task_classes = (prejob_task.CleanupTask, |
| 555 | prejob_task.VerifyTask, |
| 556 | prejob_task.RepairTask, |
| 557 | prejob_task.ResetTask, |
| 558 | prejob_task.ProvisionTask) |
| 559 | |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 560 | for agent_task_class in special_agent_task_classes: |
| 561 | if agent_task_class.TASK_TYPE == special_task.task: |
| 562 | return agent_task_class(task=special_task) |
| 563 | |
Dale Curtis | aa51336 | 2011-03-01 17:27:44 -0800 | [diff] [blame] | 564 | raise host_scheduler.SchedulerError( |
| 565 | 'No AgentTask class for task', str(special_task)) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 566 | |
| 567 | |
| 568 | def _register_pidfiles(self, agent_tasks): |
| 569 | for agent_task in agent_tasks: |
| 570 | agent_task.register_necessary_pidfiles() |
| 571 | |
| 572 | |
| 573 | def _recover_tasks(self, agent_tasks): |
| 574 | orphans = _drone_manager.get_orphaned_autoserv_processes() |
| 575 | |
| 576 | for agent_task in agent_tasks: |
| 577 | agent_task.recover() |
| 578 | if agent_task.monitor and agent_task.monitor.has_process(): |
| 579 | orphans.discard(agent_task.monitor.get_process()) |
| 580 | self.add_agent_task(agent_task) |
| 581 | |
| 582 | self._check_for_remaining_orphan_processes(orphans) |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 583 | |
| 584 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 585 | def _get_unassigned_entries(self, status): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 586 | for entry in scheduler_models.HostQueueEntry.fetch(where="status = '%s'" |
| 587 | % status): |
showard | 0db3d43 | 2009-10-12 20:29:15 +0000 | [diff] [blame] | 588 | if entry.status == status and not self.get_agents_for_entry(entry): |
| 589 | # The status can change during iteration, e.g., if job.run() |
| 590 | # sets a group of queue entries to Starting |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 591 | yield entry |
| 592 | |
| 593 | |
showard | 6878e8b | 2009-07-20 22:37:45 +0000 | [diff] [blame] | 594 | def _check_for_remaining_orphan_processes(self, orphans): |
| 595 | if not orphans: |
| 596 | return |
| 597 | subject = 'Unrecovered orphan autoserv processes remain' |
| 598 | message = '\n'.join(str(process) for process in orphans) |
| 599 | email_manager.manager.enqueue_notify_email(subject, message) |
mbligh | 5fa9e11 | 2009-08-03 16:46:06 +0000 | [diff] [blame] | 600 | |
| 601 | die_on_orphans = global_config.global_config.get_config_value( |
| 602 | scheduler_config.CONFIG_SECTION, 'die_on_orphans', type=bool) |
| 603 | |
| 604 | if die_on_orphans: |
| 605 | raise RuntimeError(subject + '\n' + message) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 606 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 607 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 608 | def _recover_pending_entries(self): |
| 609 | for entry in self._get_unassigned_entries( |
| 610 | models.HostQueueEntry.Status.PENDING): |
showard | 5682407 | 2009-10-12 20:30:21 +0000 | [diff] [blame] | 611 | logging.info('Recovering Pending entry %s', entry) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 612 | entry.on_pending() |
| 613 | |
| 614 | |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 615 | def _check_for_unrecovered_verifying_entries(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 616 | queue_entries = scheduler_models.HostQueueEntry.fetch( |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 617 | where='status = "%s"' % models.HostQueueEntry.Status.VERIFYING) |
| 618 | unrecovered_hqes = [] |
| 619 | for queue_entry in queue_entries: |
| 620 | special_tasks = models.SpecialTask.objects.filter( |
| 621 | task__in=(models.SpecialTask.Task.CLEANUP, |
| 622 | models.SpecialTask.Task.VERIFY), |
| 623 | queue_entry__id=queue_entry.id, |
| 624 | is_complete=False) |
| 625 | if special_tasks.count() == 0: |
| 626 | unrecovered_hqes.append(queue_entry) |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 627 | |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 628 | if unrecovered_hqes: |
| 629 | message = '\n'.join(str(hqe) for hqe in unrecovered_hqes) |
Dale Curtis | aa51336 | 2011-03-01 17:27:44 -0800 | [diff] [blame] | 630 | raise host_scheduler.SchedulerError( |
showard | 37757f3 | 2009-10-19 18:34:24 +0000 | [diff] [blame] | 631 | '%d unrecovered verifying host queue entries:\n%s' % |
showard | b890045 | 2009-10-12 20:31:01 +0000 | [diff] [blame] | 632 | (len(unrecovered_hqes), message)) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 633 | |
| 634 | |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 635 | def _get_prioritized_special_tasks(self): |
| 636 | """ |
| 637 | Returns all queued SpecialTasks prioritized for repair first, then |
| 638 | cleanup, then verify. |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 639 | |
| 640 | @return: list of afe.models.SpecialTasks sorted according to priority. |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 641 | """ |
| 642 | queued_tasks = models.SpecialTask.objects.filter(is_active=False, |
| 643 | is_complete=False, |
| 644 | host__locked=False) |
| 645 | # exclude hosts with active queue entries unless the SpecialTask is for |
| 646 | # that queue entry |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 647 | queued_tasks = models.SpecialTask.objects.add_join( |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 648 | queued_tasks, 'afe_host_queue_entries', 'host_id', |
| 649 | join_condition='afe_host_queue_entries.active', |
showard | 7e67b43 | 2010-01-20 01:13:04 +0000 | [diff] [blame] | 650 | join_from_key='host_id', force_left_join=True) |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 651 | queued_tasks = queued_tasks.extra( |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 652 | where=['(afe_host_queue_entries.id IS NULL OR ' |
| 653 | 'afe_host_queue_entries.id = ' |
| 654 | 'afe_special_tasks.queue_entry_id)']) |
showard | 6d7b2ff | 2009-06-10 00:16:47 +0000 | [diff] [blame] | 655 | |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 656 | # reorder tasks by priority |
| 657 | task_priority_order = [models.SpecialTask.Task.REPAIR, |
| 658 | models.SpecialTask.Task.CLEANUP, |
Dan Shi | 07e09af | 2013-04-12 09:31:29 -0700 | [diff] [blame] | 659 | models.SpecialTask.Task.VERIFY, |
Alex Miller | dfff2fd | 2013-05-28 13:05:06 -0700 | [diff] [blame] | 660 | models.SpecialTask.Task.RESET, |
| 661 | models.SpecialTask.Task.PROVISION] |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 662 | def task_priority_key(task): |
| 663 | return task_priority_order.index(task.task) |
| 664 | return sorted(queued_tasks, key=task_priority_key) |
| 665 | |
| 666 | |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 667 | def _schedule_special_tasks(self): |
| 668 | """ |
| 669 | Execute queued SpecialTasks that are ready to run on idle hosts. |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 670 | |
| 671 | Special tasks include PreJobTasks like verify, reset and cleanup. |
| 672 | They are created through _schedule_new_jobs and associated with a hqe |
| 673 | This method translates SpecialTasks to the appropriate AgentTask and |
| 674 | adds them to the dispatchers agents list, so _handle_agents can execute |
| 675 | them. |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 676 | """ |
| 677 | for task in self._get_prioritized_special_tasks(): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 678 | if self.host_has_agent(task.host): |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 679 | continue |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 680 | self.add_agent_task(self._get_agent_task_for_special_task(task)) |
showard | 1ff7b2e | 2009-05-15 23:17:18 +0000 | [diff] [blame] | 681 | |
| 682 | |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 683 | def _reverify_remaining_hosts(self): |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 684 | # recover active hosts that have not yet been recovered, although this |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 685 | # should never happen |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 686 | message = ('Recovering active host %s - this probably indicates a ' |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 687 | 'scheduler bug') |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 688 | self._reverify_hosts_where( |
Alex Miller | dfff2fd | 2013-05-28 13:05:06 -0700 | [diff] [blame] | 689 | "status IN ('Repairing', 'Verifying', 'Cleaning', 'Provisioning')", |
showard | ed2afea | 2009-07-07 20:54:07 +0000 | [diff] [blame] | 690 | print_message=message) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 691 | |
| 692 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 693 | def _reverify_hosts_where(self, where, |
showard | 2fe3f1d | 2009-07-06 20:19:11 +0000 | [diff] [blame] | 694 | print_message='Reverifying host %s'): |
Dale Curtis | 456d3c1 | 2011-07-19 11:42:51 -0700 | [diff] [blame] | 695 | full_where='locked = 0 AND invalid = 0 AND ' + where |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 696 | for host in scheduler_models.Host.fetch(where=full_where): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 697 | if self.host_has_agent(host): |
| 698 | # host has already been recovered in some way |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 699 | continue |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 700 | if self._host_has_scheduled_special_task(host): |
| 701 | # host will have a special task scheduled on the next cycle |
| 702 | continue |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 703 | if print_message: |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 704 | logging.info(print_message, host.hostname) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 705 | models.SpecialTask.objects.create( |
| 706 | task=models.SpecialTask.Task.CLEANUP, |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 707 | host=models.Host.objects.get(id=host.id)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 708 | |
| 709 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 710 | def _recover_hosts(self): |
| 711 | # recover "Repair Failed" hosts |
| 712 | message = 'Reverifying dead host %s' |
| 713 | self._reverify_hosts_where("status = 'Repair Failed'", |
| 714 | print_message=message) |
mbligh | 62ba2ed | 2008-04-30 17:09:25 +0000 | [diff] [blame] | 715 | |
| 716 | |
showard | 04c82c5 | 2008-05-29 19:38:12 +0000 | [diff] [blame] | 717 | |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 718 | def _get_pending_queue_entries(self): |
beeps | 7d8a1b1 | 2013-10-29 17:58:34 -0700 | [diff] [blame] | 719 | """ |
| 720 | Fetch a list of new host queue entries. |
| 721 | |
| 722 | The ordering of this list is important, as every new agent |
| 723 | we schedule can potentially contribute to the process count |
| 724 | on the drone, which has a static limit. The sort order |
| 725 | prioritizes jobs as follows: |
| 726 | 1. High priority jobs: Based on the afe_job's priority |
| 727 | 2. With hosts and metahosts: This will only happen if we don't |
| 728 | activate the hqe after assigning a host to it in |
| 729 | schedule_new_jobs. |
| 730 | 3. With hosts but without metahosts: When tests are scheduled |
| 731 | through the frontend the owner of the job would have chosen |
| 732 | a host for it. |
| 733 | 4. Without hosts but with metahosts: This is the common case of |
| 734 | a new test that needs a DUT. We assign a host and set it to |
| 735 | active so it shouldn't show up in case 2 on the next tick. |
| 736 | 5. Without hosts and without metahosts: Hostless suite jobs, that |
| 737 | will result in new jobs that fall under category 4. |
| 738 | |
| 739 | A note about the ordering of cases 3 and 4: |
| 740 | Prioritizing one case above the other leads to earlier acquisition |
| 741 | of the following resources: 1. process slots on the drone 2. machines. |
| 742 | - When a user schedules a job through the afe they choose a specific |
| 743 | host for it. Jobs with metahost can utilize any host that satisfies |
| 744 | the metahost criterion. This means that if we had scheduled 4 before |
| 745 | 3 there is a good chance that a job which could've used another host, |
| 746 | will now use the host assigned to a metahost-less job. Given the |
| 747 | availability of machines in pool:suites, this almost guarantees |
| 748 | starvation for jobs scheduled through the frontend. |
| 749 | - Scheduling 4 before 3 also has its pros however, since a suite |
| 750 | has the concept of a time out, whereas users can wait. If we hit the |
| 751 | process count on the drone a suite can timeout waiting on the test, |
| 752 | but a user job generally has a much longer timeout, and relatively |
| 753 | harmless consequences. |
| 754 | The current ordering was chosed because it is more likely that we will |
| 755 | run out of machines in pool:suites than processes on the drone. |
| 756 | |
| 757 | @returns A list of HQEs ordered according to sort_order. |
| 758 | """ |
| 759 | sort_order = ('afe_jobs.priority DESC, ' |
| 760 | 'ISNULL(host_id), ' |
| 761 | 'ISNULL(meta_host), ' |
| 762 | 'job_id') |
beeps | 7d8273b | 2013-11-06 09:44:34 -0800 | [diff] [blame] | 763 | query=('NOT complete AND NOT active AND status="Queued"' |
| 764 | 'AND NOT aborted') |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 765 | return list(scheduler_models.HostQueueEntry.fetch( |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 766 | joins='INNER JOIN afe_jobs ON (job_id=afe_jobs.id)', |
beeps | 7d8273b | 2013-11-06 09:44:34 -0800 | [diff] [blame] | 767 | where=query, order_by=sort_order)) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 768 | |
| 769 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 770 | def _refresh_pending_queue_entries(self): |
| 771 | """ |
| 772 | Lookup the pending HostQueueEntries and call our HostScheduler |
| 773 | refresh() method given that list. Return the list. |
| 774 | |
| 775 | @returns A list of pending HostQueueEntries sorted in priority order. |
| 776 | """ |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 777 | queue_entries = self._get_pending_queue_entries() |
| 778 | if not queue_entries: |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 779 | return [] |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 780 | |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 781 | self._host_scheduler.refresh(queue_entries) |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 782 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 783 | return queue_entries |
| 784 | |
| 785 | |
| 786 | def _schedule_atomic_group(self, queue_entry): |
| 787 | """ |
| 788 | Schedule the given queue_entry on an atomic group of hosts. |
| 789 | |
| 790 | Returns immediately if there are insufficient available hosts. |
| 791 | |
| 792 | Creates new HostQueueEntries based off of queue_entry for the |
| 793 | scheduled hosts and starts them all running. |
| 794 | """ |
| 795 | # This is a virtual host queue entry representing an entire |
| 796 | # atomic group, find a group and schedule their hosts. |
| 797 | group_hosts = self._host_scheduler.find_eligible_atomic_group( |
| 798 | queue_entry) |
| 799 | if not group_hosts: |
| 800 | return |
showard | cbe6f94 | 2009-06-17 19:33:49 +0000 | [diff] [blame] | 801 | |
| 802 | logging.info('Expanding atomic group entry %s with hosts %s', |
| 803 | queue_entry, |
| 804 | ', '.join(host.hostname for host in group_hosts)) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 805 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 806 | for assigned_host in group_hosts[1:]: |
| 807 | # Create a new HQE for every additional assigned_host. |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 808 | new_hqe = scheduler_models.HostQueueEntry.clone(queue_entry) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 809 | new_hqe.save() |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 810 | new_hqe.set_host(assigned_host) |
| 811 | self._run_queue_entry(new_hqe) |
| 812 | |
| 813 | # The first assigned host uses the original HostQueueEntry |
| 814 | queue_entry.set_host(group_hosts[0]) |
| 815 | self._run_queue_entry(queue_entry) |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 816 | |
| 817 | |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 818 | def _schedule_hostless_job(self, queue_entry): |
| 819 | self.add_agent_task(HostlessQueueTask(queue_entry)) |
jamesren | 47bd737 | 2010-03-13 00:58:17 +0000 | [diff] [blame] | 820 | queue_entry.set_status(models.HostQueueEntry.Status.STARTING) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 821 | |
| 822 | |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 823 | def _schedule_new_jobs(self): |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 824 | """ |
| 825 | Find any new HQEs and call schedule_pre_job_tasks for it. |
| 826 | |
| 827 | This involves setting the status of the HQE and creating a row in the |
| 828 | db corresponding the the special task, through |
| 829 | scheduler_models._queue_special_task. The new db row is then added as |
| 830 | an agent to the dispatcher through _schedule_special_tasks and |
| 831 | scheduled for execution on the drone through _handle_agents. |
| 832 | """ |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 833 | queue_entries = self._refresh_pending_queue_entries() |
showard | 89f84db | 2009-03-12 20:39:13 +0000 | [diff] [blame] | 834 | |
beeps | b255fc5 | 2013-10-13 23:28:54 -0700 | [diff] [blame] | 835 | new_hostless_jobs = 0 |
| 836 | new_atomic_groups = 0 |
| 837 | new_jobs_with_hosts = 0 |
| 838 | new_jobs_need_hosts = 0 |
| 839 | |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 840 | logging.debug('Processing %d queue_entries', len(queue_entries)) |
showard | 63a3477 | 2008-08-18 19:32:50 +0000 | [diff] [blame] | 841 | for queue_entry in queue_entries: |
Simran Basi | def9287 | 2012-09-20 13:34:34 -0700 | [diff] [blame] | 842 | self._log_extra_msg('Processing queue_entry: %s' % queue_entry) |
showard | e55955f | 2009-10-07 20:48:58 +0000 | [diff] [blame] | 843 | is_unassigned_atomic_group = ( |
| 844 | queue_entry.atomic_group_id is not None |
| 845 | and queue_entry.host_id is None) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 846 | |
| 847 | if queue_entry.is_hostless(): |
Simran Basi | def9287 | 2012-09-20 13:34:34 -0700 | [diff] [blame] | 848 | self._log_extra_msg('Scheduling hostless job.') |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 849 | self._schedule_hostless_job(queue_entry) |
beeps | b255fc5 | 2013-10-13 23:28:54 -0700 | [diff] [blame] | 850 | new_hostless_jobs = new_hostless_jobs + 1 |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 851 | elif is_unassigned_atomic_group: |
| 852 | self._schedule_atomic_group(queue_entry) |
beeps | b255fc5 | 2013-10-13 23:28:54 -0700 | [diff] [blame] | 853 | new_atmoic_groups = new_atomic_groups + 1 |
showard | e55955f | 2009-10-07 20:48:58 +0000 | [diff] [blame] | 854 | else: |
beeps | b255fc5 | 2013-10-13 23:28:54 -0700 | [diff] [blame] | 855 | new_jobs_need_hosts = new_jobs_need_hosts + 1 |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 856 | assigned_host = self._host_scheduler.schedule_entry(queue_entry) |
showard | 65db393 | 2009-10-28 19:54:35 +0000 | [diff] [blame] | 857 | if assigned_host and not self.host_has_agent(assigned_host): |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 858 | assert assigned_host.id == queue_entry.host_id |
| 859 | self._run_queue_entry(queue_entry) |
beeps | b255fc5 | 2013-10-13 23:28:54 -0700 | [diff] [blame] | 860 | new_jobs_with_hosts = new_jobs_with_hosts + 1 |
| 861 | |
| 862 | key = 'scheduler.jobs_per_tick' |
| 863 | stats.Gauge(key).send('new_hostless_jobs', new_hostless_jobs) |
| 864 | stats.Gauge(key).send('new_atomic_groups', new_atomic_groups) |
| 865 | stats.Gauge(key).send('new_jobs_with_hosts', new_jobs_with_hosts) |
| 866 | stats.Gauge(key).send('new_jobs_without_hosts', |
| 867 | new_jobs_need_hosts - new_jobs_with_hosts) |
showard | b95b1bd | 2008-08-15 18:11:04 +0000 | [diff] [blame] | 868 | |
| 869 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 870 | def _schedule_running_host_queue_entries(self): |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 871 | """ |
| 872 | Adds agents to the dispatcher. |
| 873 | |
| 874 | Any AgentTask, like the QueueTask, is wrapped in an Agent. The |
| 875 | QueueTask for example, will have a job with a control file, and |
| 876 | the agent will have methods that poll, abort and check if the queue |
| 877 | task is finished. The dispatcher runs the agent_task, as well as |
| 878 | other agents in it's _agents member, through _handle_agents, by |
| 879 | calling the Agents tick(). |
| 880 | |
| 881 | This method creates an agent for each HQE in one of (starting, running, |
| 882 | gathering, parsing, archiving) states, and adds it to the dispatcher so |
| 883 | it is handled by _handle_agents. |
| 884 | """ |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 885 | for agent_task in self._get_queue_entry_agent_tasks(): |
| 886 | self.add_agent_task(agent_task) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 887 | |
| 888 | |
| 889 | def _schedule_delay_tasks(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 890 | for entry in scheduler_models.HostQueueEntry.fetch( |
| 891 | where='status = "%s"' % models.HostQueueEntry.Status.WAITING): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 892 | task = entry.job.schedule_delayed_callback_task(entry) |
| 893 | if task: |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 894 | self.add_agent_task(task) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 895 | |
| 896 | |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 897 | def _run_queue_entry(self, queue_entry): |
Simran Basi | def9287 | 2012-09-20 13:34:34 -0700 | [diff] [blame] | 898 | self._log_extra_msg('Scheduling pre job tasks for queue_entry: %s' % |
| 899 | queue_entry) |
jamesren | 883492a | 2010-02-12 00:45:18 +0000 | [diff] [blame] | 900 | queue_entry.schedule_pre_job_tasks() |
mbligh | d5c9580 | 2008-03-05 00:33:46 +0000 | [diff] [blame] | 901 | |
| 902 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 903 | def _find_aborting(self): |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 904 | """ |
| 905 | Looks through the afe_host_queue_entries for an aborted entry. |
| 906 | |
| 907 | The aborted bit is set on an HQE in many ways, the most common |
| 908 | being when a user requests an abort through the frontend, which |
| 909 | results in an rpc from the afe to abort_host_queue_entries. |
| 910 | """ |
jamesren | e7c65cb | 2010-06-08 20:38:10 +0000 | [diff] [blame] | 911 | jobs_to_stop = set() |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 912 | for entry in scheduler_models.HostQueueEntry.fetch( |
Alex Miller | 9fe3966 | 2013-08-09 11:53:09 -0700 | [diff] [blame] | 913 | where='aborted=1 and complete=0'): |
showard | f4a2e50 | 2009-07-28 20:06:39 +0000 | [diff] [blame] | 914 | logging.info('Aborting %s', entry) |
beeps | e50d875 | 2013-11-20 18:23:02 -0800 | [diff] [blame] | 915 | |
| 916 | # The task would have started off with both is_complete and |
| 917 | # is_active = False. Aborted tasks are neither active nor complete. |
| 918 | # For all currently active tasks this will happen through the agent, |
| 919 | # but we need to manually update the special tasks that haven't |
| 920 | # started yet, because they don't have agents. |
| 921 | models.SpecialTask.objects.filter(is_active=False, |
| 922 | queue_entry_id=entry.id).update(is_complete=True) |
| 923 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 924 | for agent in self.get_agents_for_entry(entry): |
| 925 | agent.abort() |
| 926 | entry.abort(self) |
jamesren | e7c65cb | 2010-06-08 20:38:10 +0000 | [diff] [blame] | 927 | jobs_to_stop.add(entry.job) |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 928 | logging.debug('Aborting %d jobs this tick.', len(jobs_to_stop)) |
jamesren | e7c65cb | 2010-06-08 20:38:10 +0000 | [diff] [blame] | 929 | for job in jobs_to_stop: |
| 930 | job.stop_if_necessary() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 931 | |
| 932 | |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 933 | def _find_aborted_special_tasks(self): |
| 934 | """ |
| 935 | Find SpecialTasks that have been marked for abortion. |
| 936 | |
| 937 | Poll the database looking for SpecialTasks that are active |
| 938 | and have been marked for abortion, then abort them. |
| 939 | """ |
| 940 | |
| 941 | # The completed and active bits are very important when it comes |
| 942 | # to scheduler correctness. The active bit is set through the prolog |
| 943 | # of a special task, and reset through the cleanup method of the |
| 944 | # SpecialAgentTask. The cleanup is called both through the abort and |
| 945 | # epilog. The complete bit is set in several places, and in general |
| 946 | # a hanging job will have is_active=1 is_complete=0, while a special |
| 947 | # task which completed will have is_active=0 is_complete=1. To check |
| 948 | # aborts we directly check active because the complete bit is set in |
| 949 | # several places, including the epilog of agent tasks. |
| 950 | aborted_tasks = models.SpecialTask.objects.filter(is_active=True, |
| 951 | is_aborted=True) |
| 952 | for task in aborted_tasks: |
| 953 | # There are 2 ways to get the agent associated with a task, |
| 954 | # through the host and through the hqe. A special task |
| 955 | # always needs a host, but doesn't always need a hqe. |
| 956 | for agent in self._host_agents.get(task.host.id, []): |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 957 | if isinstance(agent.task, agent_task.SpecialAgentTask): |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 958 | |
| 959 | # The epilog preforms critical actions such as |
| 960 | # queueing the next SpecialTask, requeuing the |
| 961 | # hqe etc, however it doesn't actually kill the |
| 962 | # monitor process and set the 'done' bit. Epilogs |
| 963 | # assume that the job failed, and that the monitor |
| 964 | # process has already written an exit code. The |
| 965 | # done bit is a necessary condition for |
| 966 | # _handle_agents to schedule any more special |
| 967 | # tasks against the host, and it must be set |
| 968 | # in addition to is_active, is_complete and success. |
| 969 | agent.task.epilog() |
| 970 | agent.task.abort() |
| 971 | |
| 972 | |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 973 | def _can_start_agent(self, agent, num_started_this_cycle, |
| 974 | have_reached_limit): |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 975 | # always allow zero-process agents to run |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 976 | if agent.task.num_processes == 0: |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 977 | return True |
| 978 | # don't allow any nonzero-process agents to run after we've reached a |
| 979 | # limit (this avoids starvation of many-process agents) |
| 980 | if have_reached_limit: |
| 981 | return False |
| 982 | # total process throttling |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 983 | max_runnable_processes = _drone_manager.max_runnable_processes( |
jamesren | 76fcf19 | 2010-04-21 20:39:50 +0000 | [diff] [blame] | 984 | agent.task.owner_username, |
| 985 | agent.task.get_drone_hostnames_allowed()) |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 986 | if agent.task.num_processes > max_runnable_processes: |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 987 | return False |
| 988 | # if a single agent exceeds the per-cycle throttling, still allow it to |
| 989 | # run when it's the first agent in the cycle |
| 990 | if num_started_this_cycle == 0: |
| 991 | return True |
| 992 | # per-cycle throttling |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 993 | if (num_started_this_cycle + agent.task.num_processes > |
| 994 | scheduler_config.config.max_processes_started_per_cycle): |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 995 | return False |
| 996 | return True |
| 997 | |
| 998 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 999 | def _handle_agents(self): |
beeps | 8bb1f7d | 2013-08-05 01:30:09 -0700 | [diff] [blame] | 1000 | """ |
| 1001 | Handles agents of the dispatcher. |
| 1002 | |
| 1003 | Appropriate Agents are added to the dispatcher through |
| 1004 | _schedule_running_host_queue_entries. These agents each |
| 1005 | have a task. This method runs the agents task through |
| 1006 | agent.tick() leading to: |
| 1007 | agent.start |
| 1008 | prolog -> AgentTasks prolog |
| 1009 | For each queue entry: |
| 1010 | sets host status/status to Running |
| 1011 | set started_on in afe_host_queue_entries |
| 1012 | run -> AgentTasks run |
| 1013 | Creates PidfileRunMonitor |
| 1014 | Queues the autoserv command line for this AgentTask |
| 1015 | via the drone manager. These commands are executed |
| 1016 | through the drone managers execute actions. |
| 1017 | poll -> AgentTasks/BaseAgentTask poll |
| 1018 | checks the monitors exit_code. |
| 1019 | Executes epilog if task is finished. |
| 1020 | Executes AgentTasks _finish_task |
| 1021 | finish_task is usually responsible for setting the status |
| 1022 | of the HQE/host, and updating it's active and complete fileds. |
| 1023 | |
| 1024 | agent.is_done |
| 1025 | Removed the agent from the dispatchers _agents queue. |
| 1026 | Is_done checks the finished bit on the agent, that is |
| 1027 | set based on the Agents task. During the agents poll |
| 1028 | we check to see if the monitor process has exited in |
| 1029 | it's finish method, and set the success member of the |
| 1030 | task based on this exit code. |
| 1031 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1032 | num_started_this_cycle = 0 |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1033 | have_reached_limit = False |
| 1034 | # iterate over copy, so we can remove agents during iteration |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 1035 | logging.debug('Handling %d Agents', len(self._agents)) |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1036 | for agent in list(self._agents): |
Simran Basi | def9287 | 2012-09-20 13:34:34 -0700 | [diff] [blame] | 1037 | self._log_extra_msg('Processing Agent with Host Ids: %s and ' |
| 1038 | 'queue_entry ids:%s' % (agent.host_ids, |
| 1039 | agent.queue_entry_ids)) |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1040 | if not agent.started: |
showard | 324bf81 | 2009-01-20 23:23:38 +0000 | [diff] [blame] | 1041 | if not self._can_start_agent(agent, num_started_this_cycle, |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1042 | have_reached_limit): |
| 1043 | have_reached_limit = True |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 1044 | logging.debug('Reached Limit of allowed running Agents.') |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1045 | continue |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1046 | num_started_this_cycle += agent.task.num_processes |
Simran Basi | def9287 | 2012-09-20 13:34:34 -0700 | [diff] [blame] | 1047 | self._log_extra_msg('Starting Agent') |
showard | 4c5374f | 2008-09-04 17:02:56 +0000 | [diff] [blame] | 1048 | agent.tick() |
Simran Basi | def9287 | 2012-09-20 13:34:34 -0700 | [diff] [blame] | 1049 | self._log_extra_msg('Agent tick completed.') |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1050 | if agent.is_done(): |
Simran Basi | def9287 | 2012-09-20 13:34:34 -0700 | [diff] [blame] | 1051 | self._log_extra_msg("Agent finished") |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1052 | self.remove_agent(agent) |
Simran Basi | 3f6717d | 2012-09-13 15:21:22 -0700 | [diff] [blame] | 1053 | logging.info('%d running processes. %d added this cycle.', |
| 1054 | _drone_manager.total_running_processes(), |
| 1055 | num_started_this_cycle) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1056 | |
| 1057 | |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1058 | def _process_recurring_runs(self): |
| 1059 | recurring_runs = models.RecurringRun.objects.filter( |
| 1060 | start_date__lte=datetime.datetime.now()) |
| 1061 | for rrun in recurring_runs: |
| 1062 | # Create job from template |
| 1063 | job = rrun.job |
| 1064 | info = rpc_utils.get_job_info(job) |
showard | a9435c0 | 2009-05-13 21:28:17 +0000 | [diff] [blame] | 1065 | options = job.get_object_dict() |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1066 | |
| 1067 | host_objects = info['hosts'] |
| 1068 | one_time_hosts = info['one_time_hosts'] |
| 1069 | metahost_objects = info['meta_hosts'] |
| 1070 | dependencies = info['dependencies'] |
| 1071 | atomic_group = info['atomic_group'] |
| 1072 | |
| 1073 | for host in one_time_hosts or []: |
| 1074 | this_host = models.Host.create_one_time_host(host.hostname) |
| 1075 | host_objects.append(this_host) |
| 1076 | |
| 1077 | try: |
| 1078 | rpc_utils.create_new_job(owner=rrun.owner.login, |
showard | a9435c0 | 2009-05-13 21:28:17 +0000 | [diff] [blame] | 1079 | options=options, |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1080 | host_objects=host_objects, |
| 1081 | metahost_objects=metahost_objects, |
showard | 29f7cd2 | 2009-04-29 21:16:24 +0000 | [diff] [blame] | 1082 | atomic_group=atomic_group) |
| 1083 | |
| 1084 | except Exception, ex: |
| 1085 | logging.exception(ex) |
| 1086 | #TODO send email |
| 1087 | |
| 1088 | if rrun.loop_count == 1: |
| 1089 | rrun.delete() |
| 1090 | else: |
| 1091 | if rrun.loop_count != 0: # if not infinite loop |
| 1092 | # calculate new start_date |
| 1093 | difference = datetime.timedelta(seconds=rrun.loop_period) |
| 1094 | rrun.start_date = rrun.start_date + difference |
| 1095 | rrun.loop_count -= 1 |
| 1096 | rrun.save() |
| 1097 | |
| 1098 | |
Simran Basi | a858a23 | 2012-08-21 11:04:37 -0700 | [diff] [blame] | 1099 | SiteDispatcher = utils.import_site_class( |
| 1100 | __file__, 'autotest_lib.scheduler.site_monitor_db', |
| 1101 | 'SiteDispatcher', BaseDispatcher) |
| 1102 | |
| 1103 | class Dispatcher(SiteDispatcher): |
| 1104 | pass |
| 1105 | |
| 1106 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1107 | class Agent(object): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1108 | """ |
Alex Miller | 47715eb | 2013-07-24 03:34:01 -0700 | [diff] [blame] | 1109 | An agent for use by the Dispatcher class to perform a task. An agent wraps |
| 1110 | around an AgentTask mainly to associate the AgentTask with the queue_entry |
| 1111 | and host ids. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1112 | |
| 1113 | The following methods are required on all task objects: |
| 1114 | poll() - Called periodically to let the task check its status and |
| 1115 | update its internal state. If the task succeeded. |
| 1116 | is_done() - Returns True if the task is finished. |
| 1117 | abort() - Called when an abort has been requested. The task must |
| 1118 | set its aborted attribute to True if it actually aborted. |
| 1119 | |
| 1120 | The following attributes are required on all task objects: |
| 1121 | aborted - bool, True if this task was aborted. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1122 | success - bool, True if this task succeeded. |
| 1123 | queue_entry_ids - A sequence of HostQueueEntry ids this task handles. |
| 1124 | host_ids - A sequence of Host ids this task represents. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1125 | """ |
| 1126 | |
| 1127 | |
showard | 418785b | 2009-11-23 20:19:59 +0000 | [diff] [blame] | 1128 | def __init__(self, task): |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1129 | """ |
Alex Miller | 47715eb | 2013-07-24 03:34:01 -0700 | [diff] [blame] | 1130 | @param task: An instance of an AgentTask. |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1131 | """ |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1132 | self.task = task |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1133 | |
showard | 7718256 | 2009-06-10 00:16:05 +0000 | [diff] [blame] | 1134 | # This is filled in by Dispatcher.add_agent() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1135 | self.dispatcher = None |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1136 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1137 | self.queue_entry_ids = task.queue_entry_ids |
| 1138 | self.host_ids = task.host_ids |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1139 | |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1140 | self.started = False |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1141 | self.finished = False |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1142 | |
| 1143 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1144 | def tick(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1145 | self.started = True |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1146 | if not self.finished: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1147 | self.task.poll() |
| 1148 | if self.task.is_done(): |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1149 | self.finished = True |
showard | ec11316 | 2008-05-08 00:52:49 +0000 | [diff] [blame] | 1150 | |
| 1151 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1152 | def is_done(self): |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1153 | return self.finished |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1154 | |
| 1155 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1156 | def abort(self): |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1157 | if self.task: |
| 1158 | self.task.abort() |
| 1159 | if self.task.aborted: |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1160 | # tasks can choose to ignore aborts |
showard | 9bb960b | 2009-11-19 01:02:11 +0000 | [diff] [blame] | 1161 | self.finished = True |
showard | 20f9bdd | 2009-04-29 19:48:33 +0000 | [diff] [blame] | 1162 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1163 | |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 1164 | class AbstractQueueTask(agent_task.AgentTask, agent_task.TaskWithJobKeyvals): |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1165 | """ |
| 1166 | Common functionality for QueueTask and HostlessQueueTask |
| 1167 | """ |
| 1168 | def __init__(self, queue_entries): |
| 1169 | super(AbstractQueueTask, self).__init__() |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1170 | self.job = queue_entries[0].job |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1171 | self.queue_entries = queue_entries |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1172 | |
| 1173 | |
showard | 73ec044 | 2009-02-07 02:05:20 +0000 | [diff] [blame] | 1174 | def _keyval_path(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1175 | return os.path.join(self._working_directory(), self._KEYVAL_FILE) |
showard | 73ec044 | 2009-02-07 02:05:20 +0000 | [diff] [blame] | 1176 | |
| 1177 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1178 | def _write_control_file(self, execution_path): |
| 1179 | control_path = _drone_manager.attach_file_to_execution( |
| 1180 | execution_path, self.job.control_file) |
| 1181 | return control_path |
| 1182 | |
| 1183 | |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 1184 | # TODO: Refactor into autoserv_utils. crbug.com/243090 |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1185 | def _command_line(self): |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1186 | execution_path = self.queue_entries[0].execution_path() |
| 1187 | control_path = self._write_control_file(execution_path) |
| 1188 | hostnames = ','.join(entry.host.hostname |
| 1189 | for entry in self.queue_entries |
| 1190 | if not entry.is_hostless()) |
| 1191 | |
| 1192 | execution_tag = self.queue_entries[0].execution_tag() |
| 1193 | params = _autoserv_command_line( |
| 1194 | hostnames, |
Alex Miller | 9f01d5d | 2013-08-08 02:26:01 -0700 | [diff] [blame] | 1195 | ['-P', execution_tag, '-n', |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1196 | _drone_manager.absolute_path(control_path)], |
| 1197 | job=self.job, verbose=False) |
Dale Curtis | 30cb8eb | 2011-06-09 12:22:26 -0700 | [diff] [blame] | 1198 | if self.job.is_image_update_job(): |
| 1199 | params += ['--image', self.job.update_image_path] |
| 1200 | |
jamesren | c44ae99 | 2010-02-19 00:12:54 +0000 | [diff] [blame] | 1201 | return params |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1202 | |
| 1203 | |
| 1204 | @property |
| 1205 | def num_processes(self): |
| 1206 | return len(self.queue_entries) |
| 1207 | |
| 1208 | |
| 1209 | @property |
| 1210 | def owner_username(self): |
| 1211 | return self.job.owner |
| 1212 | |
| 1213 | |
| 1214 | def _working_directory(self): |
| 1215 | return self._get_consistent_execution_path(self.queue_entries) |
mbligh | bb42185 | 2008-03-11 22:36:16 +0000 | [diff] [blame] | 1216 | |
| 1217 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1218 | def prolog(self): |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1219 | queued_key, queued_time = self._job_queued_keyval(self.job) |
showard | c1a98d1 | 2010-01-15 00:22:22 +0000 | [diff] [blame] | 1220 | keyval_dict = self.job.keyval_dict() |
| 1221 | keyval_dict[queued_key] = queued_time |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1222 | group_name = self.queue_entries[0].get_group_name() |
| 1223 | if group_name: |
| 1224 | keyval_dict['host_group_name'] = group_name |
showard | f1ae354 | 2009-05-11 19:26:02 +0000 | [diff] [blame] | 1225 | self._write_keyvals_before_job(keyval_dict) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1226 | for queue_entry in self.queue_entries: |
showard | 8cc058f | 2009-09-08 16:26:33 +0000 | [diff] [blame] | 1227 | queue_entry.set_status(models.HostQueueEntry.Status.RUNNING) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1228 | queue_entry.set_started_on_now() |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1229 | |
| 1230 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1231 | def _write_lost_process_error_file(self): |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1232 | error_file_path = os.path.join(self._working_directory(), 'job_failure') |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1233 | _drone_manager.write_lines_to_file(error_file_path, |
| 1234 | [_LOST_PROCESS_ERROR]) |
| 1235 | |
| 1236 | |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1237 | def _finish_task(self): |
showard | 08a3641 | 2009-05-05 01:01:13 +0000 | [diff] [blame] | 1238 | if not self.monitor: |
| 1239 | return |
| 1240 | |
showard | d920518 | 2009-04-27 20:09:55 +0000 | [diff] [blame] | 1241 | self._write_job_finished() |
| 1242 | |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1243 | if self.monitor.lost_process: |
| 1244 | self._write_lost_process_error_file() |
showard | 4ac4754 | 2009-08-31 18:32:19 +0000 | [diff] [blame] | 1245 | |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 1246 | |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 1247 | def _write_status_comment(self, comment): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1248 | _drone_manager.write_lines_to_file( |
showard | d119565 | 2009-12-08 22:21:02 +0000 | [diff] [blame] | 1249 | os.path.join(self._working_directory(), 'status.log'), |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1250 | ['INFO\t----\t----\t' + comment], |
showard | 35162b0 | 2009-03-03 02:17:30 +0000 | [diff] [blame] | 1251 | paired_with_process=self.monitor.get_process()) |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 1252 | |
| 1253 | |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 1254 | def _log_abort(self): |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1255 | if not self.monitor or not self.monitor.has_process(): |
| 1256 | return |
| 1257 | |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 1258 | # build up sets of all the aborted_by and aborted_on values |
| 1259 | aborted_by, aborted_on = set(), set() |
| 1260 | for queue_entry in self.queue_entries: |
| 1261 | if queue_entry.aborted_by: |
| 1262 | aborted_by.add(queue_entry.aborted_by) |
| 1263 | t = int(time.mktime(queue_entry.aborted_on.timetuple())) |
| 1264 | aborted_on.add(t) |
| 1265 | |
| 1266 | # extract some actual, unique aborted by value and write it out |
showard | 64a9595 | 2010-01-13 21:27:16 +0000 | [diff] [blame] | 1267 | # TODO(showard): this conditional is now obsolete, we just need to leave |
| 1268 | # it in temporarily for backwards compatibility over upgrades. delete |
| 1269 | # soon. |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 1270 | assert len(aborted_by) <= 1 |
| 1271 | if len(aborted_by) == 1: |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 1272 | aborted_by_value = aborted_by.pop() |
| 1273 | aborted_on_value = max(aborted_on) |
| 1274 | else: |
| 1275 | aborted_by_value = 'autotest_system' |
| 1276 | aborted_on_value = int(time.time()) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1277 | |
showard | a038235 | 2009-02-11 23:36:43 +0000 | [diff] [blame] | 1278 | self._write_keyval_after_job("aborted_by", aborted_by_value) |
| 1279 | self._write_keyval_after_job("aborted_on", aborted_on_value) |
showard | 170873e | 2009-01-07 00:22:26 +0000 | [diff] [blame] | 1280 | |
showard | cbd7461 | 2008-11-19 21:42:02 +0000 | [diff] [blame] | 1281 | aborted_on_string = str(datetime.datetime.fromtimestamp( |
| 1282 | aborted_on_value)) |
| 1283 | self._write_status_comment('Job aborted by %s on %s' % |
| 1284 | (aborted_by_value, aborted_on_string)) |
jadmanski | c2ac77f | 2008-05-16 21:44:04 +0000 | [diff] [blame] | 1285 | |
| 1286 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1287 | def abort(self): |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1288 | super(AbstractQueueTask, self).abort() |
jadmanski | f7fa2cc | 2008-10-01 14:13:23 +0000 | [diff] [blame] | 1289 | self._log_abort() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1290 | self._finish_task() |
showard | 21baa45 | 2008-10-21 00:08:39 +0000 | [diff] [blame] | 1291 | |
| 1292 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1293 | def epilog(self): |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1294 | super(AbstractQueueTask, self).epilog() |
showard | d3dc199 | 2009-04-22 21:01:40 +0000 | [diff] [blame] | 1295 | self._finish_task() |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1296 | |
| 1297 | |
| 1298 | class QueueTask(AbstractQueueTask): |
| 1299 | def __init__(self, queue_entries): |
| 1300 | super(QueueTask, self).__init__(queue_entries) |
| 1301 | self._set_ids(queue_entries=queue_entries) |
| 1302 | |
| 1303 | |
| 1304 | def prolog(self): |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1305 | self._check_queue_entry_statuses( |
| 1306 | self.queue_entries, |
| 1307 | allowed_hqe_statuses=(models.HostQueueEntry.Status.STARTING, |
| 1308 | models.HostQueueEntry.Status.RUNNING), |
| 1309 | allowed_host_statuses=(models.Host.Status.PENDING, |
| 1310 | models.Host.Status.RUNNING)) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1311 | |
| 1312 | super(QueueTask, self).prolog() |
| 1313 | |
| 1314 | for queue_entry in self.queue_entries: |
| 1315 | self._write_host_keyvals(queue_entry.host) |
| 1316 | queue_entry.host.set_status(models.Host.Status.RUNNING) |
| 1317 | queue_entry.host.update_field('dirty', 1) |
showard | a9545c0 | 2009-12-18 22:44:26 +0000 | [diff] [blame] | 1318 | |
| 1319 | |
| 1320 | def _finish_task(self): |
| 1321 | super(QueueTask, self)._finish_task() |
| 1322 | |
| 1323 | for queue_entry in self.queue_entries: |
| 1324 | queue_entry.set_status(models.HostQueueEntry.Status.GATHERING) |
jamesren | b8f3f35 | 2010-06-10 00:44:06 +0000 | [diff] [blame] | 1325 | queue_entry.host.set_status(models.Host.Status.RUNNING) |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1326 | |
| 1327 | |
Alex Miller | 9f01d5d | 2013-08-08 02:26:01 -0700 | [diff] [blame] | 1328 | def _command_line(self): |
| 1329 | invocation = super(QueueTask, self)._command_line() |
| 1330 | return invocation + ['--verify_job_repo_url'] |
| 1331 | |
| 1332 | |
Dan Shi | 1a18905 | 2013-10-28 14:41:35 -0700 | [diff] [blame] | 1333 | class HostlessQueueTask(AbstractQueueTask): |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1334 | def __init__(self, queue_entry): |
| 1335 | super(HostlessQueueTask, self).__init__([queue_entry]) |
| 1336 | self.queue_entry_ids = [queue_entry.id] |
| 1337 | |
| 1338 | |
| 1339 | def prolog(self): |
| 1340 | self.queue_entries[0].update_field('execution_subdir', 'hostless') |
| 1341 | super(HostlessQueueTask, self).prolog() |
| 1342 | |
| 1343 | |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1344 | def _finish_task(self): |
| 1345 | super(HostlessQueueTask, self)._finish_task() |
Dan Shi | 76af802 | 2013-10-19 01:59:49 -0700 | [diff] [blame] | 1346 | |
| 1347 | # When a job is added to database, its initial status is always |
| 1348 | # Starting. In a scheduler tick, scheduler finds all jobs in Starting |
| 1349 | # status, check if any of them can be started. If scheduler hits some |
| 1350 | # limit, e.g., max_hostless_jobs_per_drone, max_jobs_started_per_cycle, |
| 1351 | # scheduler will leave these jobs in Starting status. Otherwise, the |
| 1352 | # jobs' status will be changed to Running, and an autoserv process will |
| 1353 | # be started in drone for each of these jobs. |
| 1354 | # If the entry is still in status Starting, the process has not started |
| 1355 | # yet. Therefore, there is no need to parse and collect log. Without |
| 1356 | # this check, exception will be raised by scheduler as execution_subdir |
| 1357 | # for this queue entry does not have a value yet. |
| 1358 | hqe = self.queue_entries[0] |
| 1359 | if hqe.status != models.HostQueueEntry.Status.STARTING: |
| 1360 | hqe.set_status(models.HostQueueEntry.Status.PARSING) |
mbligh | 4608b00 | 2010-01-05 18:22:35 +0000 | [diff] [blame] | 1361 | |
| 1362 | |
mbligh | 36768f0 | 2008-02-22 18:28:33 +0000 | [diff] [blame] | 1363 | if __name__ == '__main__': |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1364 | main() |