Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2016 The Chromium 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 import copy | |
| 6 from datetime import datetime | |
| 7 import logging | |
| 8 import time | |
| 9 | |
| 10 from google.appengine.ext import ndb | |
| 11 | |
| 12 from common.http_client_appengine import HttpClientAppengine as HttpClient | |
| 13 from common.pipeline_wrapper import BasePipeline | |
| 14 from model import analysis_status | |
| 15 from waterfall import swarming_util | |
| 16 from waterfall import waterfall_config | |
| 17 | |
| 18 | |
| 19 SLEEP_TIME = 10 | |
| 20 DEADLINE_SECONDS = 5 * 60 # 5 minutes | |
|
lijeffrey
2016/07/29 20:06:32
nit: comment ends with .
| |
| 21 | |
| 22 | |
| 23 class TriggerBaseSwarmingTaskPipeline(BasePipeline): | |
| 24 """A pipeline to trigger a Swarming task to re-run selected tests of a step. | |
| 25 | |
| 26 This pipeline only supports test steps that run on Swarming and support the | |
| 27 gtest filter. | |
| 28 """ | |
| 29 | |
| 30 def _GetSwarmingTaskName(self, ref_task_id): # pragma: no cover. | |
| 31 return 'findit/deflake/ref_task_id/%s/%s' % ( | |
| 32 ref_task_id, datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S %f')) | |
| 33 | |
| 34 def _CreateNewSwarmingTaskRequest(self, ref_task_id, ref_request, master_name, | |
| 35 builder_name, build_number,step_name, | |
| 36 tests, iterations): | |
| 37 """Returns a SwarmingTaskRequest instance to run the given tests only.""" | |
| 38 # Make a copy of the referred request and drop or overwrite some fields. | |
| 39 new_request = copy.deepcopy(ref_request) | |
| 40 new_request.name = self._GetSwarmingTaskName(ref_task_id) | |
| 41 new_request.parent_task_id = '' | |
| 42 new_request.user = '' | |
| 43 | |
| 44 # To force a fresh re-run and ignore cached result of any equivalent run. | |
| 45 new_request.idempotent = False | |
| 46 | |
| 47 # Set the gtest_filter to run the given tests only. | |
| 48 new_request.extra_args.append('--gtest_repeat=%s' % iterations) | |
| 49 new_request.extra_args.append('--test-launcher-retry-limit=0') | |
| 50 new_request.extra_args = [ | |
| 51 a for a in new_request.extra_args if not a.startswith('--gtest_filter') | |
| 52 ] | |
| 53 new_request.extra_args.append('--gtest_filter=%s' % ':'.join(tests)) | |
| 54 | |
| 55 # Remove the env setting for sharding. | |
| 56 sharding_settings = ['GTEST_SHARD_INDEX', 'GTEST_TOTAL_SHARDS'] | |
| 57 new_request.env = [ | |
| 58 e for e in new_request.env if e['key'] not in sharding_settings | |
| 59 ] | |
| 60 | |
| 61 # Reset tags for searching and monitoring. | |
| 62 ref_name = swarming_util.GetTagValue(ref_request.tags, 'name') | |
| 63 new_request.tags = [] | |
| 64 new_request.tags.append('purpose:deflake') | |
| 65 new_request.tags.append('ref_master:%s' % master_name) | |
| 66 new_request.tags.append('ref_buildername:%s' % builder_name) | |
| 67 new_request.tags.append('ref_buildnumber:%s' % build_number) | |
| 68 new_request.tags.append('ref_stepname:%s' % step_name) | |
| 69 new_request.tags.append('ref_task_id:%s' % ref_task_id) | |
| 70 new_request.tags.append('ref_name:%s' % ref_name) | |
| 71 | |
| 72 return new_request | |
| 73 | |
| 74 def _GetArgs(self, master_name, builder_name, build_number, step_name, tests): | |
| 75 #returns an array you can pass into _GetSwarmingTask, _CreateSwarmingTask, | |
| 76 #_NeedANewSwarmingTask as the arguments | |
| 77 #Should be overwritten in child method | |
| 78 raise NotImplementedError( | |
| 79 '_GetArgs should be implemented in child class') | |
| 80 | |
| 81 def _GetSwarmingTask(self): | |
| 82 # Get the appropriate kind of Swarming Task (Wf or Flake) | |
| 83 # Should be overwritten in child method | |
| 84 raise NotImplementedError( | |
| 85 '_GetSwarmingTask should be implemented in child class') | |
| 86 | |
| 87 def _CreateSwarmingTask(self): | |
| 88 # Create the appropriate kind of Swarming Task (Wf or Flake) | |
| 89 # Should be overwritten in child method | |
| 90 raise NotImplementedError( | |
| 91 '_CreateSwarmingTask should be implemented in child class') | |
| 92 | |
| 93 def _NeedANewSwarmingTask(self, *args): | |
| 94 swarming_task = self._GetSwarmingTask(*args) | |
| 95 if not swarming_task: | |
| 96 swarming_task = self._CreateSwarmingTask(*args) | |
| 97 swarming_task.status = analysis_status.PENDING | |
| 98 swarming_task.put() | |
| 99 return True | |
| 100 else: | |
| 101 # TODO(http://crbug.com/585676): Rerun the Swarming task if it runs into | |
| 102 # unexpected infra errors. | |
| 103 return False | |
| 104 | |
| 105 def _GetSwarmingTaskId(self, *args): | |
| 106 | |
| 107 deadline = time.time() + DEADLINE_SECONDS # Wait for 5 minutes. | |
|
lijeffrey
2016/07/29 20:06:32
nit: The comment here is no longer needed and may
| |
| 108 while time.time() < deadline: | |
| 109 swarming_task = self._GetSwarmingTask(*args) | |
| 110 | |
| 111 if not swarming_task: # pragma: no cover. Pipeline will retry. | |
| 112 raise Exception('Swarming task was deleted unexpectedly!') | |
| 113 | |
| 114 if swarming_task.task_id: | |
| 115 return swarming_task.task_id | |
| 116 | |
| 117 # Wait for the existing pipeline to start the Swarming task. | |
| 118 time.sleep(SLEEP_TIME) | |
|
lijeffrey
2016/07/29 20:06:32
nit: Sorry make this SLEEP_TIME_SECONDS for readab
| |
| 119 | |
| 120 raise Exception('Time out!') # pragma: no cover. Pipeline will retry. | |
| 121 | |
| 122 def _GetIterationsToRerun(self): | |
| 123 # How many times we want to run the swarming rerun | |
| 124 # By default, it's what's in wf_config | |
| 125 raise NotImplementedError( | |
| 126 '_GetIterationsToRerun should be implemented in child class') | |
| 127 | |
| 128 # Arguments number differs from overridden method - pylint: disable=W0221 | |
| 129 def run(self, master_name, builder_name, build_number, step_name, tests): | |
| 130 """Triggers a new Swarming task to run the given tests. | |
| 131 | |
| 132 Args: | |
| 133 master_name (str): The master name. | |
| 134 builder_name (str): The builder name. | |
| 135 build_number (str): The build number. | |
| 136 step_name (str): The failed test step name. | |
| 137 tests (list): A list of test cases, eg: ['suite1.test1', 'suite2.testw2'] | |
| 138 | |
| 139 Returns: | |
| 140 task_id (str): The new Swarming task that re-run the given tests. | |
| 141 """ | |
| 142 call_args = self._GetArgs(master_name, builder_name, | |
| 143 build_number, step_name, tests) | |
| 144 # Check if a new Swarming Task is really needed. | |
| 145 if not self._NeedANewSwarmingTask(*call_args): | |
| 146 return self._GetSwarmingTaskId(*call_args) | |
| 147 assert tests | |
| 148 http_client = HttpClient() | |
| 149 | |
| 150 # 0. Retrieve existing Swarming task ids for the given step. | |
| 151 swarming_task_items = swarming_util.ListSwarmingTasksDataByTags( | |
| 152 master_name, builder_name, build_number, http_client, step_name) | |
| 153 assert len(swarming_task_items) > 0, 'No Swarming task was run.' | |
| 154 ref_task_id = swarming_task_items[0]['task_id'] | |
| 155 | |
| 156 # 1. Retrieve Swarming task parameters from a given Swarming task id. | |
| 157 ref_request = swarming_util.GetSwarmingTaskRequest( | |
| 158 ref_task_id, http_client) | |
| 159 | |
| 160 # 2. Update/Overwrite parameters for the re-run. | |
| 161 iterations_to_rerun = self._GetIterationsToRerun() | |
| 162 | |
| 163 new_request = self._CreateNewSwarmingTaskRequest( | |
| 164 ref_task_id, ref_request, master_name, builder_name, build_number, | |
| 165 step_name, tests, iterations_to_rerun) | |
| 166 | |
| 167 # 3. Trigger a new Swarming task to re-run the failed tests. | |
| 168 task_id = swarming_util.TriggerSwarmingTask(new_request, http_client) | |
| 169 | |
| 170 # Save the task id. | |
| 171 swarming_task = self._GetSwarmingTask(*call_args) | |
| 172 swarming_task.task_id = task_id | |
| 173 swarming_task.parameters['tests'] = tests | |
| 174 swarming_task.parameters['iterations_to_rerun'] = iterations_to_rerun | |
| 175 swarming_task.parameters['ref_name'] = swarming_util.GetTagValue( | |
| 176 new_request.tags, 'ref_name') | |
| 177 swarming_task.put() | |
| 178 | |
| 179 logging.info('A Swarming task was triggered:%s', task_id) | |
| 180 return task_id | |
| OLD | NEW |