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 | |
Aviv Keshet | 4e7722b | 2013-02-14 15:07:46 -0800 | [diff] [blame] | 10 | import base_event, board_enumerator, build_event |
| 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 | 098d1e2 | 2015-09-02 10:00:24 -0700 | [diff] [blame] | 14 | from autotest_lib.client.common_lib.cros.graphite import autotest_stats |
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 | 15d4231 | 2015-12-15 15:37:28 -0800 | [diff] [blame] | 17 | POOL_SIZE = 32 |
Dan Shi | 098d1e2 | 2015-09-02 10:00:24 -0700 | [diff] [blame] | 18 | |
| 19 | _timer = autotest_stats.Timer('suite_scheduler') |
| 20 | |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 21 | class Driver(object): |
| 22 | """Implements the main loop of the suite_scheduler. |
| 23 | |
Chris Masone | bf8775a | 2012-09-10 10:44:18 -0700 | [diff] [blame] | 24 | @var EVENT_CLASSES: list of the event classes Driver supports. |
Chris Masone | fe5a509 | 2012-04-11 18:29:07 -0700 | [diff] [blame] | 25 | @var _LOOP_INTERVAL_SECONDS: seconds to wait between loop iterations. |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 26 | |
| 27 | @var _scheduler: a DedupingScheduler, used to schedule jobs with the AFE. |
Chris Masone | 3fba86f | 2012-04-03 10:06:56 -0700 | [diff] [blame] | 28 | @var _enumerator: a BoardEnumerator, used to list plaforms known to |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 29 | the AFE |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 30 | @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] | 31 | """ |
| 32 | |
Chris Masone | bf8775a | 2012-09-10 10:44:18 -0700 | [diff] [blame] | 33 | EVENT_CLASSES = [timed_event.Nightly, timed_event.Weekly, |
| 34 | build_event.NewBuild] |
Chris Masone | fe5a509 | 2012-04-11 18:29:07 -0700 | [diff] [blame] | 35 | _LOOP_INTERVAL_SECONDS = 5 * 60 |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 36 | |
| 37 | |
Dan Shi | 2324514 | 2015-01-22 13:22:28 -0800 | [diff] [blame] | 38 | def __init__(self, scheduler, enumerator, is_sanity=False): |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 39 | """Constructor |
| 40 | |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 41 | @param scheduler: an instance of deduping_scheduler.DedupingScheduler. |
| 42 | @param enumerator: an instance of board_enumerator.BoardEnumerator. |
Dan Shi | 2324514 | 2015-01-22 13:22:28 -0800 | [diff] [blame] | 43 | @param is_sanity: Set to True if the driver is created for sanity check. |
| 44 | Default is set to False. |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 45 | """ |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 46 | self._scheduler = scheduler |
| 47 | self._enumerator = enumerator |
Dan Shi | 2324514 | 2015-01-22 13:22:28 -0800 | [diff] [blame] | 48 | task.TotMilestoneManager.is_sanity = is_sanity |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 49 | |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 50 | |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 51 | def RereadAndReprocessConfig(self, config, mv): |
| 52 | """Re-read config, re-populate self._events and recreate task lists. |
| 53 | |
| 54 | @param config: an instance of ForgivingConfigParser. |
| 55 | @param mv: an instance of ManifestVersions. |
| 56 | """ |
| 57 | config.reread() |
| 58 | new_events = self._CreateEventsWithTasks(config, mv) |
| 59 | for keyword, event in self._events.iteritems(): |
| 60 | event.Merge(new_events[keyword]) |
| 61 | |
| 62 | |
Chris Masone | 93f51d4 | 2012-04-18 08:46:52 -0700 | [diff] [blame] | 63 | def SetUpEventsAndTasks(self, config, mv): |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 64 | """Populate self._events and create task lists from config. |
| 65 | |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 66 | @param config: an instance of ForgivingConfigParser. |
Chris Masone | 93f51d4 | 2012-04-18 08:46:52 -0700 | [diff] [blame] | 67 | @param mv: an instance of ManifestVersions. |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 68 | """ |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 69 | self._events = self._CreateEventsWithTasks(config, mv) |
| 70 | |
| 71 | |
| 72 | def _CreateEventsWithTasks(self, config, mv): |
| 73 | """Create task lists from config, and assign to newly-minted events. |
| 74 | |
| 75 | Calling multiple times should start afresh each time. |
| 76 | |
| 77 | @param config: an instance of ForgivingConfigParser. |
| 78 | @param mv: an instance of ManifestVersions. |
| 79 | """ |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 80 | events = {} |
Chris Masone | bf8775a | 2012-09-10 10:44:18 -0700 | [diff] [blame] | 81 | for klass in self.EVENT_CLASSES: |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 82 | events[klass.KEYWORD] = klass.CreateFromConfig(config, mv) |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 83 | |
| 84 | tasks = self.TasksFromConfig(config) |
Chris Masone | 645c7e4 | 2012-05-17 17:28:40 -0700 | [diff] [blame] | 85 | for keyword, task_list in tasks.iteritems(): |
| 86 | if keyword in events: |
| 87 | events[keyword].tasks = task_list |
| 88 | else: |
Ilja H. Friedel | 04be2bd | 2014-05-07 21:29:59 -0700 | [diff] [blame] | 89 | logging.warning('%s, is an unknown keyword.', keyword) |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 90 | return events |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 91 | |
| 92 | |
| 93 | def TasksFromConfig(self, config): |
| 94 | """Generate a dict of {event_keyword: [tasks]} mappings from |config|. |
| 95 | |
| 96 | For each section in |config| that encodes a Task, instantiate a Task |
| 97 | object. Determine the event that Task is supposed to run_on and |
| 98 | append the object to a list associated with the appropriate event |
| 99 | keyword. Return a dictionary of these keyword: list of task mappings. |
| 100 | |
| 101 | @param config: a ForgivingConfigParser containing tasks to be parsed. |
| 102 | @return dict of {event_keyword: [tasks]} mappings. |
| 103 | @raise MalformedConfigEntry on a task parsing error. |
| 104 | """ |
| 105 | tasks = {} |
| 106 | for section in config.sections(): |
Chris Masone | 93f51d4 | 2012-04-18 08:46:52 -0700 | [diff] [blame] | 107 | if not base_event.HonoredSection(section): |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 108 | try: |
| 109 | keyword, new_task = task.Task.CreateFromConfigSection( |
| 110 | config, section) |
| 111 | except task.MalformedConfigEntry as e: |
Ilja H. Friedel | 04be2bd | 2014-05-07 21:29:59 -0700 | [diff] [blame] | 112 | logging.warning('%s is malformed: %s', section, e) |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 113 | continue |
| 114 | tasks.setdefault(keyword, []).append(new_task) |
| 115 | return tasks |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 116 | |
| 117 | |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 118 | def RunForever(self, config, mv): |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 119 | """Main loop of the scheduler. Runs til the process is killed. |
| 120 | |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 121 | @param config: an instance of ForgivingConfigParser. |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 122 | @param mv: an instance of manifest_versions.ManifestVersions. |
| 123 | """ |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 124 | for event in self._events.itervalues(): |
Chris Masone | 73a7838 | 2012-04-20 13:25:51 -0700 | [diff] [blame] | 125 | event.Prepare() |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 126 | while True: |
Chris Masone | 645c7e4 | 2012-05-17 17:28:40 -0700 | [diff] [blame] | 127 | try: |
| 128 | self.HandleEventsOnce(mv) |
Scott Zawalski | c15c6b4 | 2012-07-09 13:16:05 -0400 | [diff] [blame] | 129 | except board_enumerator.EnumeratorException as e: |
Ilja H. Friedel | 04be2bd | 2014-05-07 21:29:59 -0700 | [diff] [blame] | 130 | logging.warning('Failed to enumerate boards: %r', e) |
Dan Shi | 098d1e2 | 2015-09-02 10:00:24 -0700 | [diff] [blame] | 131 | with _timer.get_client('manifest_versions_update'): |
| 132 | mv.Update() |
| 133 | with _timer.get_client('tot_milestone_manager_refresh'): |
| 134 | task.TotMilestoneManager().refresh() |
Chris Masone | fe5a509 | 2012-04-11 18:29:07 -0700 | [diff] [blame] | 135 | time.sleep(self._LOOP_INTERVAL_SECONDS) |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 136 | self.RereadAndReprocessConfig(config, mv) |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 137 | |
| 138 | |
Dan Shi | 15d4231 | 2015-12-15 15:37:28 -0800 | [diff] [blame] | 139 | @staticmethod |
| 140 | def HandleBoard(inputs): |
| 141 | """Handle event based on given inputs. |
| 142 | |
| 143 | @param inputs: A dictionary of the arguments needed to handle an event. |
| 144 | Keys include: |
| 145 | scheduler: a DedupingScheduler, used to schedule jobs with the AFE. |
| 146 | event: An event object to be handled. |
| 147 | board: Name of the board. |
| 148 | """ |
| 149 | scheduler = inputs['scheduler'] |
| 150 | event = inputs['event'] |
| 151 | board = inputs['board'] |
| 152 | |
| 153 | logging.info('Handling %s event for board %s', event.keyword, board) |
| 154 | branch_builds = event.GetBranchBuildsForBoard(board) |
| 155 | event.Handle(scheduler, branch_builds, board) |
| 156 | logging.info('Finished handling %s event for board %s', event.keyword, |
| 157 | board) |
| 158 | |
| 159 | |
Dan Shi | 098d1e2 | 2015-09-02 10:00:24 -0700 | [diff] [blame] | 160 | @_timer.decorate |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 161 | def HandleEventsOnce(self, mv): |
| 162 | """One turn through the loop. Separated out for unit testing. |
| 163 | |
| 164 | @param mv: an instance of manifest_versions.ManifestVersions. |
Chris Masone | 645c7e4 | 2012-05-17 17:28:40 -0700 | [diff] [blame] | 165 | @raise EnumeratorException if we can't enumerate any supported boards. |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 166 | """ |
Dan Shi | bba4949 | 2016-02-22 17:57:25 -0800 | [diff] [blame] | 167 | # Reset the value of delay_minutes, as this is the beginning of |
| 168 | # handling an event for all boards. |
| 169 | self._scheduler.delay_minutes = 0 |
Chris Masone | 92874d3 | 2012-04-03 10:13:04 -0700 | [diff] [blame] | 170 | boards = self._enumerator.Enumerate() |
Dan Shi | 15d4231 | 2015-12-15 15:37:28 -0800 | [diff] [blame] | 171 | logging.info('%d boards currently in the lab: %r', len(boards), boards) |
| 172 | thread_pool = pool.ThreadPool(POOL_SIZE) |
Dan Shi | 8d7f356 | 2016-01-11 10:55:46 -0800 | [diff] [blame] | 173 | with contextlib.closing(thread_pool): |
| 174 | for e in self._events.itervalues(): |
| 175 | if not e.ShouldHandle(): |
| 176 | continue |
Dan Shi | 15d4231 | 2015-12-15 15:37:28 -0800 | [diff] [blame] | 177 | logging.info('Handling %s event for %d boards', e.keyword, |
| 178 | len(boards)) |
| 179 | args = [] |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 180 | for board in boards: |
Dan Shi | 15d4231 | 2015-12-15 15:37:28 -0800 | [diff] [blame] | 181 | args.append({'scheduler': self._scheduler, |
| 182 | 'event': e, |
| 183 | 'board': board}) |
| 184 | thread_pool.map(self.HandleBoard, args) |
| 185 | logging.info('Finished handling %s event for %d boards', |
| 186 | e.keyword, len(boards)) |
Chris Masone | bbde386 | 2012-05-07 14:29:51 -0700 | [diff] [blame] | 187 | e.UpdateCriteria() |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 188 | |
| 189 | |
| 190 | def ForceEventsOnceForBuild(self, keywords, build_name): |
| 191 | """Force events with provided keywords to happen, with given build. |
| 192 | |
| 193 | @param keywords: iterable of event keywords to force |
| 194 | @param build_name: instead of looking up builds to test, test this one. |
| 195 | """ |
J. Richard Barnette | 3cbd76b | 2013-11-27 12:11:25 -0800 | [diff] [blame] | 196 | board, type, milestone, manifest = utils.ParseBuildName(build_name) |
Chris Masone | cbe4277 | 2012-04-30 22:18:23 -0700 | [diff] [blame] | 197 | branch_builds = {task.PickBranchName(type, milestone): [build_name]} |
Alex Miller | da26f4f | 2013-03-07 14:07:44 -0800 | [diff] [blame] | 198 | logging.info('Testing build R%s-%s on %s', milestone, manifest, board) |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 199 | |
Chris Masone | 855d86f | 2012-05-07 13:48:07 -0700 | [diff] [blame] | 200 | for e in self._events.itervalues(): |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 201 | if e.keyword in keywords: |
| 202 | e.Handle(self._scheduler, branch_builds, board, force=True) |