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