-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathretryBot.ts
More file actions
267 lines (239 loc) · 7.69 KB
/
Copy pathretryBot.ts
File metadata and controls
267 lines (239 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { Probot } from "probot";
import { queryClickhouseSaved } from "../clickhouse";
import { CachedConfigTracker, isPyTorchbotSupportedOrg } from "./utils";
const SUCCESS_CONCLUSIONS = ["success"];
const FAILURE_CONCLUSIONS = ["failure", "cancelled", "timed_out"];
// If these jobs fail, they will always be retried
const ALWAYS_RETRY_JOBS = [
// From @laithsakka, we want to retry this job in a different runner as it could
// fail flakily sometimes
"pr_time_benchmarks",
];
async function getFlakyJobsFromPreviousWorkflow(
owner: string,
repo: string,
branch: string,
workflowName: string,
workflowId: number
): Promise<any> {
const flakyJobs = await queryClickhouseSaved("flaky_workflows_jobs", {
branches: [branch],
maxAttempt: 1, // If the job was retried and still failed, it wasn't flaky
nextWorkflowId: workflowId, // Query the flaky status of jobs from the previous workflow
numHours: 24, // The default value
repo: `${owner}/${repo}`,
workflowId: 0,
workflowNames: [workflowName],
});
if (flakyJobs === undefined || flakyJobs.length === 0) {
return [];
}
// The query returns all the flaky jobs from the previous workflow
return flakyJobs;
}
async function retryPreviousWorkflow(
ctx: any,
owner: string,
repo: string,
branch: string,
workflowName: string,
workflowId: number
) {
const flakyJobs = await getFlakyJobsFromPreviousWorkflow(
owner,
repo,
branch,
workflowName,
workflowId
);
if (flakyJobs === undefined || flakyJobs.length === 0) {
return;
}
// If multiple jobs need to be rerun, rerun everything that failed
return await ctx.octokit.rest.actions.reRunWorkflowFailedJobs({
owner,
repo,
run_id: flakyJobs[0].workflow_id,
});
}
async function retryCurrentWorkflow(
ctx: any,
owner: string,
repo: string,
defaultBranch: string,
workflowName: string,
workflowJobs: any[],
runId: number,
retryableStepNames: string[] = []
) {
const failedJobs = workflowJobs.filter((job) =>
FAILURE_CONCLUSIONS.includes(job.conclusion!)
);
if (failedJobs.length > 5) {
// if you have more than 5 failing jobs, its probably either a real failure, a landrace,
// or a widespread outage that wouldn't be helped by retries
return;
}
const doesLookLikeUserFailure = (
job: any,
isCodeValiationStep: (_step: any) => boolean
) => {
// Ensure if any of the steps that failed are not infra related steps (e.g. they're lint, build or test steps)
return (
job.steps?.filter(
// @ts-expect-error
(step) =>
step.conclusion !== null &&
FAILURE_CONCLUSIONS.includes(step.conclusion) &&
isCodeValiationStep(step)
).length > 0
);
};
const retryJobs = failedJobs.filter((job) => {
// If the job was cancelled on main, it was probably an infra error, so rerun.
// On other branches, it could have been cancelled for valid reasons, so we won't rerun.
// Would be good to fine tune this further for non-main branches to differentiate between.
// retryable and nonretryable cancellations
if (
job.conclusion === "cancelled" &&
ctx.payload.workflow_run.head_branch === defaultBranch
) {
return true;
}
// for builds, don't rerun if it failed on the actual build step
if (
job.name.toLocaleLowerCase().startsWith("build") &&
doesLookLikeUserFailure(job, (step) =>
step.name.toLowerCase().startsWith("build")
)
) {
// we continue our retry checks even if this test passes in case this is a build-and-test job
return false;
}
// don't rerun unstable jobs as this is not needed
if (job.name.toLocaleLowerCase().includes("unstable")) {
return false;
}
for (const flakyJobName of ALWAYS_RETRY_JOBS) {
// if the job is a known flaky one, we want to retry it whenever if fails,
// even if the failed step is a test step
if (job.name.toLocaleLowerCase().includes(flakyJobName)) {
return true;
}
}
// If a retryable step name failed, always retry (e.g. CUDA Compute Check
// indicates a bad runner, not a code problem)
if (retryableStepNames.length > 0) {
const hasRetryableStepFailure = job.steps?.some(
(step: any) =>
step.conclusion !== null &&
FAILURE_CONCLUSIONS.includes(step.conclusion) &&
retryableStepNames.some(
(name) => step.name.toLowerCase() === name.toLowerCase()
)
);
if (hasRetryableStepFailure) {
return true;
}
}
// if no test steps failed, can rerun
return !doesLookLikeUserFailure(job, (step) =>
step.name.toLowerCase().includes("test")
);
});
if (retryJobs.length === 0) {
return;
}
if (retryJobs.length === 1) {
// if only one should be rerun, just rerun that job
return await ctx.octokit.rest.actions.reRunJobForWorkflowRun({
owner,
repo,
job_id: retryJobs[0].id,
});
}
// if multiple jobs need to be rerun, rerun everything that failed
return await ctx.octokit.rest.actions.reRunWorkflowFailedJobs({
owner,
repo,
run_id: runId,
});
}
function retryBot(app: Probot): void {
const tracker = new CachedConfigTracker(app);
app.on("workflow_run.completed", async (ctx) => {
const owner = ctx.payload.repository.owner.login;
if (!isPyTorchbotSupportedOrg(owner)) {
ctx.log(`${__filename} isn't enabled on ${owner}'s repos`);
return;
}
const workflowName = ctx.payload.workflow_run.name;
const attemptNumber = ctx.payload.workflow_run.run_attempt;
const defaultBranch = ctx.payload.repository.default_branch;
const repo = ctx.payload.repository.name;
const runId = ctx.payload.workflow_run.id;
const config: any = await tracker.loadConfig(ctx);
const allowedWorkflowPrefixes: string[] | undefined =
config != null ? config["retryable_workflows"] : undefined;
const retryableStepNames: string[] =
config != null ? config["retryable_step_names"] ?? [] : [];
if (allowedWorkflowPrefixes === undefined) {
return;
}
if (
(ctx.payload.workflow_run.conclusion === "cancelled" &&
ctx.payload.workflow_run.head_branch !== defaultBranch) ||
attemptNumber > 1 ||
allowedWorkflowPrefixes.every(
(allowedWorkflow) =>
!workflowName.toLowerCase().includes(allowedWorkflow.toLowerCase())
)
) {
return;
}
if (ctx.payload.workflow_run.conclusion !== "success") {
let workflowJobs = [];
let total_count = 1;
const jobs_per_page = 100;
for (let i = 0; i * jobs_per_page < total_count; i++) {
const data = (
await ctx.octokit.rest.actions.listJobsForWorkflowRunAttempt({
owner,
repo,
run_id: runId,
attempt_number: attemptNumber,
page: i + 1,
per_page: jobs_per_page,
})
).data;
total_count = data.total_count;
workflowJobs.push(...data.jobs);
}
// Retry jobs from the current workflow only when it fails
await retryCurrentWorkflow(
ctx,
owner,
repo,
defaultBranch,
workflowName,
workflowJobs,
runId,
retryableStepNames
);
}
// Check if we need to retry flaky jobs from the previous workflow. This is
// only supported in trunk. Note that this is run whether the workflow fail
// or not as it's about the previous workflow
if (ctx.payload.workflow_run.head_branch === defaultBranch) {
await retryPreviousWorkflow(
ctx,
owner,
repo,
defaultBranch,
workflowName,
runId
);
}
});
}
export default retryBot;