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