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