Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send batch requests when scheduling new commit builds #1999

Merged
merged 3 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app_dart/lib/src/service/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ class Config {
/// Size of the shards to send to buildBucket when scheduling builds.
int get schedulingShardSize => 5;

/// Batch size of builds to schedule in each swarming request.
int get batchSize => 5;

/// Max retries when scheduling builds.
static const RetryOptions schedulerRetry = RetryOptions(maxAttempts: 3);

Expand Down
18 changes: 16 additions & 2 deletions app_dart/lib/src/service/scheduler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math';
import 'dart:typed_data';

import 'package:cocoon_service/src/service/build_status_provider.dart';
Expand Down Expand Up @@ -154,11 +155,24 @@ class Scheduler {
log.severe('Failed to add commit ${commit.sha!}: $error');
}

await luciBuildService.schedulePostsubmitBuilds(commit: commit, toBeScheduled: toBeScheduled);

await _batchScheduleBuilds(commit, toBeScheduled);
await _uploadToBigQuery(commit);
}

/// Schedule all builds in batch requests instead of a single request.
///
/// Each batch request contains `kBatchSize` builds to be scheduled.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Each batch request contains `kBatchSize` builds to be scheduled.
/// Each batch request contains [Config.batchSize] builds to be scheduled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Future<void> _batchScheduleBuilds(Commit commit, List<Tuple<Target, Task, int>> toBeScheduled) async {
final List<Future> futures = <Future>[];
for (int i = 0; i < toBeScheduled.length; i += config.batchSize) {
futures.add(luciBuildService.schedulePostsubmitBuilds(
commit: commit,
toBeScheduled: toBeScheduled.sublist(i, min(i + config.batchSize, toBeScheduled.length)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is sublist upper bound index non-inclusive?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct.

));
}
await Future.wait<void>(futures);
}

/// Return subset of [commits] not stored in Datastore.
Future<List<Commit>> _getMissingCommits(List<Commit> commits) async {
final List<Commit> newCommits = <Commit>[];
Expand Down
32 changes: 32 additions & 0 deletions app_dart/test/service/scheduler_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ void main() {
});

group('add commits', () {
final FakePubSub pubsub = FakePubSub();
List<Commit> createCommitList(
List<String> shas, {
String repo = 'flutter',
Expand Down Expand Up @@ -255,6 +256,37 @@ void main() {
final Iterable<Task> tasks = db.values.values.whereType<Task>();
expect(tasks.singleWhere((Task task) => task.name == 'Linux A').status, Task.statusInProgress);
});

test('schedules cocoon based targets - multiple batch requests', () async {
final MockBuildBucketClient mockBuildBucketClient = MockBuildBucketClient();
final FakeLuciBuildService luciBuildService = FakeLuciBuildService(
config,
buildbucket: mockBuildBucketClient,
gerritService: FakeGerritService(),
pubsub: pubsub,
);
when(mockBuildBucketClient.listBuilders(any)).thenAnswer((_) async {
return const ListBuildersResponse(builders: [
BuilderItem(id: BuilderId(bucket: 'prod', project: 'flutter', builder: 'Linux A')),
BuilderItem(id: BuilderId(bucket: 'prod', project: 'flutter', builder: 'Linux runIf')),
]);
});
buildStatusService =
FakeBuildStatusService(commitStatuses: <CommitStatus>[CommitStatus(generateCommit(1), const <Stage>[])]);
config.batchSizeValue = 1;
scheduler = Scheduler(
cache: cache,
config: config,
buildStatusProvider: (_) => buildStatusService,
datastoreProvider: (DatastoreDB db) => DatastoreService(db, 2),
githubChecksService: GithubChecksService(config, githubChecksUtil: mockGithubChecksUtil),
httpClientProvider: () => httpClient,
luciBuildService: luciBuildService,
);

await scheduler.addCommits(createCommitList(<String>['1']));
expect(pubsub.messages.length, 2);
});
});

group('add pull request', () {
Expand Down
5 changes: 5 additions & 0 deletions app_dart/test/src/datastore/fake_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class FakeConfig implements Config {
this.supportedBranchesValue,
this.luciBuildersValue,
this.supportedReposValue,
this.batchSizeValue,
FakeDatastoreDB? dbValue,
}) : dbValue = dbValue ?? FakeDatastoreDB();

Expand All @@ -69,6 +70,7 @@ class FakeConfig implements Config {
ServiceAccountInfo? deviceLabServiceAccountValue;
int? maxTaskRetriesValue;
int? maxLuciTaskRetriesValue;
int? batchSizeValue;
FakeKeyHelper? keyHelperValue;
String? oauthClientIdValue;
String? githubOAuthTokenValue;
Expand Down Expand Up @@ -132,6 +134,9 @@ class FakeConfig implements Config {
@override
int get schedulingShardSize => 5;

@override
int get batchSize => batchSizeValue ?? 5;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making this const in config.dart would prevent need to add this stub in the test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to have customized batchSize values to validate from test.


@override
int get maxLuciTaskRetries => maxLuciTaskRetriesValue!;

Expand Down