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