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