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