Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 1 | # Copyright 2017 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 | |
| 5 | """Module for executing tasks queued by suite scheduler.""" |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame] | 6 | # pylint: disable=g-bad-import-order |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 7 | |
Prathmesh Prabhu | e818231 | 2020-03-06 23:33:07 -0800 | [diff] [blame] | 8 | import collections |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 9 | import logging |
| 10 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 11 | import buildbucket |
| 12 | import constants |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 13 | |
Xixuan Wu | 0a8d3ee | 2017-10-19 11:33:26 -0700 | [diff] [blame] | 14 | import apiclient |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 15 | from google.appengine.api import taskqueue |
| 16 | from google.appengine.runtime import apiproxy_errors |
| 17 | |
| 18 | |
| 19 | SUITES_QUEUE = 'suitesQueue' |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 20 | |
Prathmesh Prabhu | 55c4003 | 2020-03-07 00:45:15 -0800 | [diff] [blame] | 21 | Options = collections.namedtuple( |
| 22 | 'Options', |
| 23 | ['batch_size', 'multirequest_size', 'per_suite_multirequest_size']) |
Prathmesh Prabhu | e818231 | 2020-03-06 23:33:07 -0800 | [diff] [blame] | 24 | |
| 25 | _DEFAULT_OPTIONS = Options( |
Xinan Lin | 3b9b6f4 | 2020-03-16 16:10:37 -0700 | [diff] [blame] | 26 | # The maximum CTP multi-requests can be sent in this run. |
| 27 | batch_size=10, |
Prathmesh Prabhu | e818231 | 2020-03-06 23:33:07 -0800 | [diff] [blame] | 28 | multirequest_size=constants.Buildbucket.MULTIREQUEST_SIZE, |
Prathmesh Prabhu | 55c4003 | 2020-03-07 00:45:15 -0800 | [diff] [blame] | 29 | # A limit on the number of tasks included per request for specific suites. |
| 30 | per_suite_multirequest_size={ |
| 31 | # crbug.com/1028732: Generates too many results to store in BuildBucket |
| 32 | # output properties. |
Prathmesh Prabhu | 3e0bfbb | 2020-03-11 12:28:17 -0700 | [diff] [blame] | 33 | 'arc-cts': 15, |
| 34 | 'arc-cts-qual': 15, |
| 35 | 'arc-cts-unibuild': 15, |
Prathmesh Prabhu | 55c4003 | 2020-03-07 00:45:15 -0800 | [diff] [blame] | 36 | 'arc-gts': 10, |
Prathmesh Prabhu | 3e0bfbb | 2020-03-11 12:28:17 -0700 | [diff] [blame] | 37 | 'crosbolt_perf_perbuild': 15, |
| 38 | 'ent-nightly': 15, |
| 39 | 'faft_bios': 15, |
Prathmesh Prabhu | 55c4003 | 2020-03-07 00:45:15 -0800 | [diff] [blame] | 40 | 'graphics_per-day': 10, |
Prathmesh Prabhu | 3e0bfbb | 2020-03-11 12:28:17 -0700 | [diff] [blame] | 41 | 'graphics_per-week': 15, |
| 42 | 'wifi_matfunc': 10, |
Prathmesh Prabhu | 55c4003 | 2020-03-07 00:45:15 -0800 | [diff] [blame] | 43 | }) |
Prathmesh Prabhu | e818231 | 2020-03-06 23:33:07 -0800 | [diff] [blame] | 44 | |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 45 | |
Prathmesh Prabhu | 4633148 | 2020-03-07 00:18:10 -0800 | [diff] [blame] | 46 | def new_task_processor(): |
| 47 | """Factory function to create a task processor appropriate to environment.""" |
Prathmesh Prabhu | 8f43d31 | 2020-03-07 00:25:06 -0800 | [diff] [blame] | 48 | # Schedule tests to PROD_BUILDER if in production project, otherwise use |
| 49 | # STAGING_BUILDER. |
| 50 | builder = constants.Buildbucket.STAGING_BUILDER |
| 51 | if (constants.environment() == constants.RunningEnv.ENV_PROD |
| 52 | and constants.application_id() == constants.AppID.PROD_APP): |
| 53 | builder = constants.Buildbucket.PROD_BUILDER |
| 54 | test_platform_client = buildbucket.TestPlatformClient( |
| 55 | constants.Buildbucket.HOST, constants.Buildbucket.PROJECT, |
| 56 | constants.Buildbucket.BUCKET, builder) |
| 57 | return TaskProcessor( |
| 58 | queue_name=SUITES_QUEUE, |
| 59 | test_platform_client=test_platform_client, |
| 60 | options=_DEFAULT_OPTIONS) |
Prathmesh Prabhu | 4633148 | 2020-03-07 00:18:10 -0800 | [diff] [blame] | 61 | |
| 62 | |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 63 | class TaskProcessor(object): |
| 64 | """A class capable of executing tasks by kicking off suites. |
| 65 | |
Prathmesh Prabhu | 4633148 | 2020-03-07 00:18:10 -0800 | [diff] [blame] | 66 | This class fetches tasks from pullqueue, and kicks off suites |
| 67 | represented by tasks' params. |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 68 | """ |
| 69 | |
Prathmesh Prabhu | 8f43d31 | 2020-03-07 00:25:06 -0800 | [diff] [blame] | 70 | def __init__(self, queue_name, test_platform_client, options): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 71 | """Initialize a task executor for further pulling & execution. |
| 72 | |
| 73 | Args: |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 74 | queue_name: The name of a pull queue. |
Prathmesh Prabhu | 8f43d31 | 2020-03-07 00:25:06 -0800 | [diff] [blame] | 75 | test_platform_client: A buildbucket.TestPlatformClient object. |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 76 | options: Options to configure the task processor. |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 77 | """ |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 78 | self.queue = taskqueue.Queue(queue_name) |
Prathmesh Prabhu | e818231 | 2020-03-06 23:33:07 -0800 | [diff] [blame] | 79 | self._options = options |
Prathmesh Prabhu | 8f43d31 | 2020-03-07 00:25:06 -0800 | [diff] [blame] | 80 | self.test_platform_client = test_platform_client |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 81 | |
| 82 | def batch_execute(self): |
Craig Bergstrom | 58263d3 | 2018-04-26 14:11:35 -0600 | [diff] [blame] | 83 | """Execute tasks.""" |
Xinan Lin | 3b9b6f4 | 2020-03-16 16:10:37 -0700 | [diff] [blame] | 84 | sent_multireq_count = 0 |
| 85 | while (sent_multireq_count < self._options.batch_size): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 86 | try: |
Prathmesh Prabhu | e818231 | 2020-03-06 23:33:07 -0800 | [diff] [blame] | 87 | tasks = self.queue.lease_tasks_by_tag( |
| 88 | 3600, self._options.multirequest_size, deadline=60) |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 89 | except (taskqueue.UnknownQueueError, |
| 90 | taskqueue.TransientError, |
| 91 | apiproxy_errors.DeadlineExceededError) as e: |
| 92 | logging.exception(e) |
| 93 | raise |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 94 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 95 | if not tasks: |
| 96 | return |
| 97 | |
Prathmesh Prabhu | 55c4003 | 2020-03-07 00:45:15 -0800 | [diff] [blame] | 98 | tasks = self._limit_heavy_tasks(tasks) |
| 99 | executed_tasks = [] |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 100 | try: |
| 101 | executed_tasks.extend( |
Prathmesh Prabhu | 55c4003 | 2020-03-07 00:45:15 -0800 | [diff] [blame] | 102 | self.test_platform_client.multirequest_run(tasks, _suite(tasks))) |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 103 | except (ValueError, |
| 104 | buildbucket.BuildbucketRunError, |
| 105 | apiclient.errors.HttpError) as e: |
| 106 | logging.exception('Failed to kick off %d tasks for suite %s', |
Prathmesh Prabhu | 55c4003 | 2020-03-07 00:45:15 -0800 | [diff] [blame] | 107 | len(tasks), _suite(tasks)) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 108 | finally: |
| 109 | if executed_tasks: |
Xinan Lin | 3b9b6f4 | 2020-03-16 16:10:37 -0700 | [diff] [blame] | 110 | sent_multireq_count += 1 |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 111 | logging.info('Successfully kicking %d tasks for suite %s', |
Prathmesh Prabhu | 55c4003 | 2020-03-07 00:45:15 -0800 | [diff] [blame] | 112 | len(executed_tasks), _suite(tasks)) |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 113 | self.queue.delete_tasks(executed_tasks) |
| 114 | |
| 115 | def purge(self): |
| 116 | """Purge the entire tasks in the task queue.""" |
| 117 | self.queue.purge() |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 118 | |
Prathmesh Prabhu | 55c4003 | 2020-03-07 00:45:15 -0800 | [diff] [blame] | 119 | def _limit_heavy_tasks(self, tasks): |
| 120 | """Further limits tasks known to cause large load on cros_test_platform.""" |
| 121 | if not tasks: |
| 122 | return tasks |
| 123 | limit = self._options.per_suite_multirequest_size.get( |
| 124 | _suite(tasks), |
| 125 | self._options.multirequest_size, |
| 126 | ) |
| 127 | keep = tasks[:limit] |
| 128 | forget = tasks[limit:] |
| 129 | for task in forget: |
| 130 | self.queue.modify_task_lease(task, 0) |
| 131 | return keep |
| 132 | |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 133 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 134 | def push(queue_name, tag=None, **suite_kwargs): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 135 | """Push suites to suite queue for later kickoff. |
| 136 | |
| 137 | Args: |
| 138 | queue_name: the name of a pull queue. |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 139 | tag: tag of a pull queue task. |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 140 | **suite_kwargs: the args for a suite to kick off. |
| 141 | """ |
| 142 | queue = taskqueue.Queue(queue_name) |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 143 | queue.add(taskqueue.Task(method='PULL', tag=tag, params=suite_kwargs)) |
Prathmesh Prabhu | 55c4003 | 2020-03-07 00:45:15 -0800 | [diff] [blame] | 144 | |
| 145 | |
| 146 | def _suite(tasks): |
| 147 | return tasks[0].tag |