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