Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Dan Shi | 8d7f356 | 2016-01-11 10:55:46 -0800 | [diff] [blame] | 5 | import contextlib |
Dan Shi | 15d4231 | 2015-12-15 15:37:28 -0800 | [diff] [blame] | 6 | import logging |
| 7 | import time |
| 8 | from multiprocessing import pool |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 9 | |
Dan Shi | 8446fac | 2016-03-02 22:07:39 -0800 | [diff] [blame] | 10 | import base_event, board_enumerator, build_event, deduping_scheduler |
Aviv Keshet | 4e7722b | 2013-02-14 15:07:46 -0800 | [diff] [blame] | 11 | import task, timed_event |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 12 | |
Aviv Keshet | 4e7722b | 2013-02-14 15:07:46 -0800 | [diff] [blame] | 13 | import common |
Dan Shi | 5e2efb7 | 2017-02-07 11:40:23 -0800 | [diff] [blame^] | 14 | from autotest_lib.client.common_lib import utils |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 15 | from autotest_lib.server import utils |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 16 | |
Dan Shi | 5e2efb7 | 2017-02-07 11:40:23 -0800 | [diff] [blame^] | 17 | try: |
| 18 | from chromite.lib import metrics |
| 19 | except ImportError: |
| 20 | metrics = utils.metrics_mock |
| 21 | |
Dan Shi | 098d1e2 | 2015-09-02 10:00:24 -0700 | [diff] [blame] | 22 | |
Dan Shi | c3fc599 | 2016-12-08 11:12:30 -0800 | [diff] [blame] | 23 | POOL_SIZE = 32 |
Dan Shi | 098d1e2 | 2015-09-02 10:00:24 -0700 | [diff] [blame] | 24 | |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 25 | class Driver(object): |
| 26 | """Implements the main loop of the suite_scheduler. |
| 27 | |
Chris Masone | bf8775a | 2012-09-10 10:44:18 -0700 | [diff] [blame] | 28 | @var EVENT_CLASSES: list of the event classes Driver supports. |
Chris Masone | fe5a509 | 2012-04-11 18:29:07 -0700 | [diff] [blame] | 29 | @var _LOOP_INTERVAL_SECONDS: seconds to wait between loop iterations. |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 30 | |
| 31 | @var _scheduler: a DedupingScheduler, used to schedule jobs with the AFE. |
Chris Masone | 3fba86f | 2012-04-03 10:06:56 -0700 | [diff] [blame] | 32 | @var _enumerator: a BoardEnumerator, used to list plaforms known to |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 33 | the AFE |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 34 | @var _events: dict of BaseEvents to be handled each time through main loop. |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 35 | """ |
| 36 | |
Chris Masone | bf8775a | 2012-09-10 10:44:18 -0700 | [diff] [blame] | 37 | EVENT_CLASSES = [timed_event.Nightly, timed_event.Weekly, |
| 38 | build_event.NewBuild] |
Chris Masone | fe5a509 | 2012-04-11 18:29:07 -0700 | [diff] [blame] | 39 | _LOOP_INTERVAL_SECONDS = 5 * 60 |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 40 | |
Dan Shi | fa705d2 | 2016-07-28 16:16:15 -0700 | [diff] [blame] | 41 | # Cache for known ChromeOS boards. The cache helps to avoid unnecessary |
| 42 | # repeated calls to Launch Control API. |
| 43 | _cros_boards = set() |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 44 | |
Dan Shi | 2324514 | 2015-01-22 13:22:28 -0800 | [diff] [blame] | 45 | def __init__(self, scheduler, enumerator, is_sanity=False): |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 46 | """Constructor |
| 47 | |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 48 | @param scheduler: an instance of deduping_scheduler.DedupingScheduler. |
| 49 | @param enumerator: an instance of board_enumerator.BoardEnumerator. |
Dan Shi | 2324514 | 2015-01-22 13:22:28 -0800 | [diff] [blame] | 50 | @param is_sanity: Set to True if the driver is created for sanity check. |
| 51 | Default is set to False. |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 52 | """ |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 53 | self._scheduler = scheduler |
| 54 | self._enumerator = enumerator |
Dan Shi | 2324514 | 2015-01-22 13:22:28 -0800 | [diff] [blame] | 55 | task.TotMilestoneManager.is_sanity = is_sanity |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 56 | |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 57 | |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 58 | def RereadAndReprocessConfig(self, config, mv): |
| 59 | """Re-read config, re-populate self._events and recreate task lists. |
| 60 | |
| 61 | @param config: an instance of ForgivingConfigParser. |
| 62 | @param mv: an instance of ManifestVersions. |
| 63 | """ |
| 64 | config.reread() |
| 65 | new_events = self._CreateEventsWithTasks(config, mv) |
| 66 | for keyword, event in self._events.iteritems(): |
| 67 | event.Merge(new_events[keyword]) |
| 68 | |
| 69 | |
Chris Masone | 93f51d4 | 2012-04-18 08:46:52 -0700 | [diff] [blame] | 70 | def SetUpEventsAndTasks(self, config, mv): |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 71 | """Populate self._events and create task lists from config. |
| 72 | |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 73 | @param config: an instance of ForgivingConfigParser. |
Chris Masone | 93f51d4 | 2012-04-18 08:46:52 -0700 | [diff] [blame] | 74 | @param mv: an instance of ManifestVersions. |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 75 | """ |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 76 | self._events = self._CreateEventsWithTasks(config, mv) |
| 77 | |
| 78 | |
| 79 | def _CreateEventsWithTasks(self, config, mv): |
| 80 | """Create task lists from config, and assign to newly-minted events. |
| 81 | |
| 82 | Calling multiple times should start afresh each time. |
| 83 | |
| 84 | @param config: an instance of ForgivingConfigParser. |
| 85 | @param mv: an instance of ManifestVersions. |
| 86 | """ |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 87 | events = {} |
Chris Masone | bf8775a | 2012-09-10 10:44:18 -0700 | [diff] [blame] | 88 | for klass in self.EVENT_CLASSES: |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 89 | events[klass.KEYWORD] = klass.CreateFromConfig(config, mv) |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 90 | |
| 91 | tasks = self.TasksFromConfig(config) |
Chris Masone | 645c7e4 | 2012-05-17 17:28:40 -0700 | [diff] [blame] | 92 | for keyword, task_list in tasks.iteritems(): |
| 93 | if keyword in events: |
| 94 | events[keyword].tasks = task_list |
| 95 | else: |
Ilja H. Friedel | 04be2bd | 2014-05-07 21:29:59 -0700 | [diff] [blame] | 96 | logging.warning('%s, is an unknown keyword.', keyword) |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 97 | return events |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 98 | |
| 99 | |
| 100 | def TasksFromConfig(self, config): |
| 101 | """Generate a dict of {event_keyword: [tasks]} mappings from |config|. |
| 102 | |
| 103 | For each section in |config| that encodes a Task, instantiate a Task |
| 104 | object. Determine the event that Task is supposed to run_on and |
| 105 | append the object to a list associated with the appropriate event |
| 106 | keyword. Return a dictionary of these keyword: list of task mappings. |
| 107 | |
| 108 | @param config: a ForgivingConfigParser containing tasks to be parsed. |
| 109 | @return dict of {event_keyword: [tasks]} mappings. |
| 110 | @raise MalformedConfigEntry on a task parsing error. |
| 111 | """ |
| 112 | tasks = {} |
| 113 | for section in config.sections(): |
Chris Masone | 93f51d4 | 2012-04-18 08:46:52 -0700 | [diff] [blame] | 114 | if not base_event.HonoredSection(section): |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 115 | try: |
| 116 | keyword, new_task = task.Task.CreateFromConfigSection( |
Dan Shi | 8458536 | 2016-10-03 14:11:43 -0700 | [diff] [blame] | 117 | config, section) |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 118 | except task.MalformedConfigEntry as e: |
Dan Shi | 8458536 | 2016-10-03 14:11:43 -0700 | [diff] [blame] | 119 | logging.warning('%s is malformed: %s', section, str(e)) |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 120 | continue |
| 121 | tasks.setdefault(keyword, []).append(new_task) |
| 122 | return tasks |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 123 | |
| 124 | |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 125 | def RunForever(self, config, mv): |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 126 | """Main loop of the scheduler. Runs til the process is killed. |
| 127 | |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 128 | @param config: an instance of ForgivingConfigParser. |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 129 | @param mv: an instance of manifest_versions.ManifestVersions. |
| 130 | """ |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 131 | for event in self._events.itervalues(): |
Chris Masone | 73a7838 | 2012-04-20 13:25:51 -0700 | [diff] [blame] | 132 | event.Prepare() |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 133 | while True: |
Chris Masone | 645c7e4 | 2012-05-17 17:28:40 -0700 | [diff] [blame] | 134 | try: |
| 135 | self.HandleEventsOnce(mv) |
Scott Zawalski | c15c6b4 | 2012-07-09 13:16:05 -0400 | [diff] [blame] | 136 | except board_enumerator.EnumeratorException as e: |
Ilja H. Friedel | 04be2bd | 2014-05-07 21:29:59 -0700 | [diff] [blame] | 137 | logging.warning('Failed to enumerate boards: %r', e) |
Dan Shi | c3fc599 | 2016-12-08 11:12:30 -0800 | [diff] [blame] | 138 | mv.Update() |
| 139 | task.TotMilestoneManager().refresh() |
Chris Masone | fe5a509 | 2012-04-11 18:29:07 -0700 | [diff] [blame] | 140 | time.sleep(self._LOOP_INTERVAL_SECONDS) |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 141 | self.RereadAndReprocessConfig(config, mv) |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 142 | |
| 143 | |
Dan Shi | 15d4231 | 2015-12-15 15:37:28 -0800 | [diff] [blame] | 144 | @staticmethod |
| 145 | def HandleBoard(inputs): |
| 146 | """Handle event based on given inputs. |
| 147 | |
| 148 | @param inputs: A dictionary of the arguments needed to handle an event. |
| 149 | Keys include: |
| 150 | scheduler: a DedupingScheduler, used to schedule jobs with the AFE. |
| 151 | event: An event object to be handled. |
| 152 | board: Name of the board. |
| 153 | """ |
| 154 | scheduler = inputs['scheduler'] |
| 155 | event = inputs['event'] |
| 156 | board = inputs['board'] |
| 157 | |
Dan Shi | fa705d2 | 2016-07-28 16:16:15 -0700 | [diff] [blame] | 158 | # Try to get builds from LaunchControl first. If failed, the board could |
| 159 | # be ChromeOS. Use the cache Driver._cros_boards to avoid unnecessary |
| 160 | # repeated call to LaunchControl API. |
| 161 | launch_control_builds = None |
| 162 | if board not in Driver._cros_boards: |
Dan Shi | 2121a33 | 2016-02-25 14:22:22 -0800 | [diff] [blame] | 163 | launch_control_builds = event.GetLaunchControlBuildsForBoard(board) |
Dan Shi | fa705d2 | 2016-07-28 16:16:15 -0700 | [diff] [blame] | 164 | if launch_control_builds: |
Dan Shi | 2121a33 | 2016-02-25 14:22:22 -0800 | [diff] [blame] | 165 | event.Handle(scheduler, branch_builds=None, board=board, |
| 166 | launch_control_builds=launch_control_builds) |
| 167 | else: |
| 168 | branch_builds = event.GetBranchBuildsForBoard(board) |
Dan Shi | fa705d2 | 2016-07-28 16:16:15 -0700 | [diff] [blame] | 169 | if branch_builds: |
| 170 | Driver._cros_boards.add(board) |
| 171 | logging.info('Found ChromeOS build for board %s. This should ' |
| 172 | 'be a ChromeOS board.', board) |
Dan Shi | 2121a33 | 2016-02-25 14:22:22 -0800 | [diff] [blame] | 173 | event.Handle(scheduler, branch_builds, board) |
Dan Shi | 15d4231 | 2015-12-15 15:37:28 -0800 | [diff] [blame] | 174 | logging.info('Finished handling %s event for board %s', event.keyword, |
| 175 | board) |
| 176 | |
Dan Shi | c3fc599 | 2016-12-08 11:12:30 -0800 | [diff] [blame] | 177 | @metrics.SecondsTimerDecorator('chromeos/autotest/suite_scheduler/' |
| 178 | 'handle_events_once_duration') |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 179 | def HandleEventsOnce(self, mv): |
| 180 | """One turn through the loop. Separated out for unit testing. |
| 181 | |
| 182 | @param mv: an instance of manifest_versions.ManifestVersions. |
Chris Masone | 645c7e4 | 2012-05-17 17:28:40 -0700 | [diff] [blame] | 183 | @raise EnumeratorException if we can't enumerate any supported boards. |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 184 | """ |
Chris Masone | 92874d3 | 2012-04-03 10:13:04 -0700 | [diff] [blame] | 185 | boards = self._enumerator.Enumerate() |
Dan Shi | 15d4231 | 2015-12-15 15:37:28 -0800 | [diff] [blame] | 186 | logging.info('%d boards currently in the lab: %r', len(boards), boards) |
| 187 | thread_pool = pool.ThreadPool(POOL_SIZE) |
Dan Shi | 8d7f356 | 2016-01-11 10:55:46 -0800 | [diff] [blame] | 188 | with contextlib.closing(thread_pool): |
| 189 | for e in self._events.itervalues(): |
| 190 | if not e.ShouldHandle(): |
| 191 | continue |
Dan Shi | 8446fac | 2016-03-02 22:07:39 -0800 | [diff] [blame] | 192 | # Reset the value of delay_minutes, as this is the beginning of |
| 193 | # handling an event for all boards. |
| 194 | self._scheduler.delay_minutes = 0 |
| 195 | self._scheduler.delay_minutes_interval = ( |
| 196 | deduping_scheduler.DELAY_MINUTES_INTERVAL) |
Dan Shi | 15d4231 | 2015-12-15 15:37:28 -0800 | [diff] [blame] | 197 | logging.info('Handling %s event for %d boards', e.keyword, |
| 198 | len(boards)) |
| 199 | args = [] |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 200 | for board in boards: |
Dan Shi | 15d4231 | 2015-12-15 15:37:28 -0800 | [diff] [blame] | 201 | args.append({'scheduler': self._scheduler, |
| 202 | 'event': e, |
| 203 | 'board': board}) |
| 204 | thread_pool.map(self.HandleBoard, args) |
| 205 | logging.info('Finished handling %s event for %d boards', |
| 206 | e.keyword, len(boards)) |
Chris Masone | bbde386 | 2012-05-07 14:29:51 -0700 | [diff] [blame] | 207 | e.UpdateCriteria() |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 208 | |
| 209 | |
Dan Shi | 2121a33 | 2016-02-25 14:22:22 -0800 | [diff] [blame] | 210 | def ForceEventsOnceForBuild(self, keywords, build_name, |
| 211 | os_type=task.OS_TYPE_CROS): |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 212 | """Force events with provided keywords to happen, with given build. |
| 213 | |
| 214 | @param keywords: iterable of event keywords to force |
| 215 | @param build_name: instead of looking up builds to test, test this one. |
Dan Shi | 2121a33 | 2016-02-25 14:22:22 -0800 | [diff] [blame] | 216 | @param os_type: Type of the OS to test, default to cros. |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 217 | """ |
Dan Shi | 2121a33 | 2016-02-25 14:22:22 -0800 | [diff] [blame] | 218 | branch_builds = None |
| 219 | launch_control_builds = None |
| 220 | if os_type == task.OS_TYPE_CROS: |
| 221 | board, type, milestone, manifest = utils.ParseBuildName(build_name) |
| 222 | branch_builds = {task.PickBranchName(type, milestone): [build_name]} |
| 223 | logging.info('Testing build R%s-%s on %s', milestone, manifest, |
| 224 | board) |
| 225 | else: |
| 226 | logging.info('Build is not a ChromeOS build, try to parse as a ' |
| 227 | 'Launch Control build.') |
Dan Shi | 6450e14 | 2016-03-11 11:52:20 -0800 | [diff] [blame] | 228 | _,target,_ = utils.parse_launch_control_build(build_name) |
Dan Shi | 8db7e61 | 2016-07-21 12:55:16 -0700 | [diff] [blame] | 229 | board = utils.parse_launch_control_target(target)[0] |
Dan Shi | 4327440 | 2016-11-04 15:13:43 -0700 | [diff] [blame] | 230 | # Translate board name in build target to the actual board name. |
| 231 | board = utils.ANDROID_TARGET_TO_BOARD_MAP.get(board, board) |
Dan Shi | 2121a33 | 2016-02-25 14:22:22 -0800 | [diff] [blame] | 232 | launch_control_builds = [build_name] |
| 233 | logging.info('Testing Launch Control build %s on %s', build_name, |
| 234 | board) |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 235 | |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 236 | for e in self._events.itervalues(): |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 237 | if e.keyword in keywords: |
Dan Shi | 2121a33 | 2016-02-25 14:22:22 -0800 | [diff] [blame] | 238 | e.Handle(self._scheduler, branch_builds, board, force=True, |
| 239 | launch_control_builds=launch_control_builds) |