Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: appengine/findit/waterfall/trigger_base_swarming_task_pipeline.py

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

Powered by Google App Engine
This is Rietveld 408576698