Skip to content

Commit 70b6bcb

Browse files
committed
Fusion checks are not scheduled if any engine checks are failed so no need to look up for failed checks in both, only last guard might have failed checks.
1 parent 234b2a4 commit 70b6bcb

File tree

3 files changed

+71
-66
lines changed

3 files changed

+71
-66
lines changed

app_dart/lib/src/service/firestore/unified_check_run.dart

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,21 @@ final class UnifiedCheckRun {
3737
config.flags.isUnifiedCheckRunFlowEnabledForUser(
3838
pullRequest.user!.login!,
3939
)) {
40+
// Create the UnifiedCheckRun and UnifiedCheckRunBuilds.
4041
log.info(
4142
'Storing UnifiedCheckRun data for ${slug.fullName}#${pullRequest.number} as it enabled for user ${pullRequest.user!.login}.',
4243
);
43-
// Create the UnifiedCheckRun and UnifiedCheckRunBuilds.
44+
// We store the creation time of the guard since there might be several
45+
// guards for the same PR created and each new one created after previous
46+
// was succeeded so we are interested in a state of the last one.
47+
final creationTime = DateTime.now().toUtc().microsecondsSinceEpoch;
4448
final guard = PresubmitGuard(
4549
checkRun: checkRun,
4650
commitSha: sha,
4751
slug: slug,
4852
pullRequestId: pullRequest.number!,
4953
stage: stage,
50-
creationTime: pullRequest.createdAt!.microsecondsSinceEpoch,
54+
creationTime: creationTime,
5155
author: pullRequest.user!.login!,
5256
remainingBuilds: tasks.length,
5357
failedBuilds: 0,
@@ -58,7 +62,7 @@ final class UnifiedCheckRun {
5862
PresubmitCheck.init(
5963
buildName: task,
6064
checkRunId: checkRun.id!,
61-
creationTime: pullRequest.createdAt!.microsecondsSinceEpoch,
65+
creationTime: creationTime,
6266
),
6367
];
6468
await firestoreService.writeViaTransaction(
@@ -89,63 +93,67 @@ final class UnifiedCheckRun {
8993
log.info('$logCrumb Re-Running failed checks.');
9094
final transaction = await firestoreService.beginTransaction();
9195

92-
final guards = await getPresubmitGuardsForCheckRun(
96+
// New guard created only if previous is succeeded so failed checks might be
97+
// only in last guard.
98+
final guard = await getLastPresubmitGuardForCheckRun(
9399
firestoreService: firestoreService,
94100
slug: slug,
95101
pullRequestId: pullRequestId,
96102
checkRunId: checkRunId,
97103
transaction: transaction,
98104
);
99105

100-
for (final guard in guards) {
101-
// Copy the failed build names to a local variable to avoid losing the
102-
// failed build names after resetting the failed guard.builds.
103-
final failedBuildNames = guard.failedBuildNames;
104-
if (failedBuildNames.isNotEmpty) {
105-
guard.failedBuilds = 0;
106-
guard.remainingBuilds = failedBuildNames.length;
107-
final builds = guard.builds;
108-
for (final buildName in failedBuildNames) {
109-
builds[buildName] = TaskStatus.waitingForBackfill;
110-
}
111-
guard.builds = builds;
112-
final checks = [
113-
for (final buildName in failedBuildNames)
114-
PresubmitCheck.init(
115-
buildName: buildName,
116-
checkRunId: checkRunId,
117-
creationTime: DateTime.now().toUtc().microsecondsSinceEpoch,
118-
attemptNumber:
119-
((await getLatestPresubmitCheck(
120-
firestoreService: firestoreService,
121-
checkRunId: checkRunId,
122-
buildName: buildName,
123-
transaction: transaction,
124-
))?.attemptNumber ??
125-
0) +
126-
1, // Increment the latest attempt number.
127-
),
128-
];
129-
try {
130-
final response = await firestoreService.commit(
131-
transaction,
132-
documentsToWrites([...checks, guard]),
133-
);
134-
log.info(
135-
'$logCrumb: results = ${response.writeResults?.map((e) => e.toJson())}',
136-
);
137-
return FailedChecksForRerun(
138-
checkRunGuard: guard.checkRun,
139-
checkNames: failedBuildNames,
140-
stage: guard.stage,
141-
);
142-
} catch (e) {
143-
log.info('$logCrumb: failed to update presubmit check', e);
144-
rethrow;
145-
}
106+
if (guard == null) {
107+
return null;
108+
}
109+
110+
// Copy the failed build names to a local variable to avoid losing the
111+
// failed build names after resetting the failed guard.builds.
112+
final creationTime = DateTime.now().toUtc().microsecondsSinceEpoch;
113+
final failedBuildNames = guard.failedBuildNames;
114+
if (failedBuildNames.isNotEmpty) {
115+
guard.failedBuilds = 0;
116+
guard.remainingBuilds = failedBuildNames.length;
117+
final builds = guard.builds;
118+
for (final buildName in failedBuildNames) {
119+
builds[buildName] = TaskStatus.waitingForBackfill;
120+
}
121+
guard.builds = builds;
122+
final checks = [
123+
for (final buildName in failedBuildNames)
124+
PresubmitCheck.init(
125+
buildName: buildName,
126+
checkRunId: checkRunId,
127+
creationTime: creationTime,
128+
attemptNumber:
129+
((await getLatestPresubmitCheck(
130+
firestoreService: firestoreService,
131+
checkRunId: checkRunId,
132+
buildName: buildName,
133+
transaction: transaction,
134+
))?.attemptNumber ??
135+
0) +
136+
1, // Increment the latest attempt number.
137+
),
138+
];
139+
try {
140+
final response = await firestoreService.commit(
141+
transaction,
142+
documentsToWrites([...checks, guard]),
143+
);
144+
log.info(
145+
'$logCrumb: results = ${response.writeResults?.map((e) => e.toJson())}',
146+
);
147+
return FailedChecksForRerun(
148+
checkRunGuard: guard.checkRun,
149+
checkNames: failedBuildNames,
150+
stage: guard.stage,
151+
);
152+
} catch (e) {
153+
log.info('$logCrumb: failed to update presubmit check', e);
154+
rethrow;
146155
}
147156
}
148-
return null;
149157
}
150158

151159
/// Returns _all_ checks running against the specified github [checkRunId].
@@ -203,19 +211,19 @@ final class UnifiedCheckRun {
203211
}
204212

205213
/// Returns [PresubmitGuard]s for the specified github [checkRunId].
206-
static Future<List<PresubmitGuard>> getPresubmitGuardsForCheckRun({
214+
static Future<PresubmitGuard?> getLastPresubmitGuardForCheckRun({
207215
required FirestoreService firestoreService,
208216
required RepositorySlug slug,
209217
required int pullRequestId,
210218
required int checkRunId,
211219
Transaction? transaction,
212220
}) async {
213-
return await _queryPresubmitGuards(
221+
return (await _queryPresubmitGuards(
214222
firestoreService: firestoreService,
215223
checkRunId: checkRunId,
216224
transaction: transaction,
217-
orderMap: const {PresubmitGuard.fieldStage: kQueryOrderAscending},
218-
);
225+
limit: 1,
226+
)).firstOrNull;
219227
}
220228

221229
static Future<List<PresubmitGuard>> _queryPresubmitGuards({

app_dart/test/service/firestore/unified_check_run_test.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ void main() {
484484
});
485485
});
486486

487-
test('getPresubmitGuardsForCheckRun returns correct guards', () async {
487+
test('getLastPresubmitGuardForCheckRun returns newest guard', () async {
488488
final sha = 'sha';
489489
final slug = RepositorySlug('flutter', 'flutter');
490490
final checkRun = CheckRun.fromJson(const {
@@ -513,7 +513,7 @@ void main() {
513513
slug: slug,
514514
pullRequestId: 1,
515515
stage: CiStage.fusionTests,
516-
creationTime: 1000,
516+
creationTime: 2000,
517517
author: 'dash',
518518
remainingBuilds: 1,
519519
failedBuilds: 0,
@@ -527,17 +527,16 @@ void main() {
527527
], exists: false),
528528
);
529529

530-
final guards = await UnifiedCheckRun.getPresubmitGuardsForCheckRun(
530+
final guard = await UnifiedCheckRun.getLastPresubmitGuardForCheckRun(
531531
firestoreService: firestoreService,
532532
slug: slug,
533533
pullRequestId: 1,
534534
checkRunId: 123,
535535
);
536536

537-
expect(guards, hasLength(2));
538-
expect(guards[0].stage, CiStage.fusionEngineBuild);
539-
expect(guards[1].stage, CiStage.fusionTests);
540-
expect(guards.every((g) => g.checkRunId == 123), isTrue);
537+
expect(guard, isNotNull);
538+
expect(guard!.stage, CiStage.fusionTests);
539+
expect(guard.checkRunId, 123);
541540
});
542541
});
543542
}

app_dart/test/service/scheduler_test.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,8 +1290,6 @@ targets:
12901290
expect(updatedGuard.builds['Linux A'], TaskStatus.waitingForBackfill);
12911291
});
12921292

1293-
1294-
12951293
test('rerequested action reschedules failed checks for fusion', () async {
12961294
final mockGithubChecksService = MockGithubChecksService();
12971295
when(
@@ -1323,7 +1321,7 @@ targets:
13231321
slug: slug,
13241322
pullRequestId: 1,
13251323
stage: CiStage.fusionTests,
1326-
creationTime: 1000,
1324+
creationTime: 2000,
13271325
author: 'dash',
13281326
builds: {'Linux A': TaskStatus.failed},
13291327
failedBuilds: 1,
@@ -1333,7 +1331,7 @@ targets:
13331331
final check = PresubmitCheck(
13341332
buildName: 'Linux A',
13351333
checkRunId: 1,
1336-
creationTime: 1000,
1334+
creationTime: 2000,
13371335
status: TaskStatus.failed,
13381336
attemptNumber: 1,
13391337
);

0 commit comments

Comments
 (0)