Repository navigation
Cut merge-group CI from ~46 to ~20 min: shard Gradle e2e and test legs, skip test-release in queue, cancel orphaned runs #1133
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| name: Merge queue janitor | ||
|
|
||
| # Cancels merge-group runs the queue has orphaned. Each queue entry runs CI on | ||
| # its own `gh-readonly-queue/main/pr-<n>-<base sha>` ref, so ci.yml's | ||
| # concurrency group never lets a newer run cancel an older one. When an entry | ||
| # ahead fails or is removed, the queue deletes the refs of every entry behind | ||
| # it and rebuilds them on new refs, but the runs on the deleted refs keep going | ||
| # (each one is a full ~200-job CI run, macOS legs included). A run whose ref no | ||
| # longer exists can never merge, so it is cancelled here. Every rebuild creates | ||
| # a new merge group, which triggers this sweep. | ||
| # | ||
| # Not a required check, and it never fails the merge group: a sweep error is | ||
| # only a warning. | ||
|
|
||
| on: | ||
| merge_group: | ||
| types: [checks_requested] | ||
| workflow_dispatch: | ||
|
|
||
| permissions: {} | ||
|
|
||
| concurrency: | ||
| group: merge-queue-janitor | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| cancel-orphaned-runs: | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 5 | ||
| permissions: | ||
| actions: write | ||
| contents: read | ||
| steps: | ||
| - name: Cancel merge-group runs whose queue ref is gone | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| REPO: ${{ github.repository }} | ||
| run: | | ||
| set -uo pipefail | ||
| for status in queued in_progress; do | ||
| gh api --paginate "repos/$REPO/actions/runs?event=merge_group&status=$status&per_page=100" \ | ||
| -q '.workflow_runs[] | "\(.id) \(.head_branch)"' || echo "::warning::could not list $status runs" | ||
| done | sort -u | while read -r id branch; do | ||
| case "$branch" in gh-readonly-queue/*) ;; *) continue ;; esac | ||
| # Only a definite 404 means the entry is gone; any other lookup | ||
| # error (rate limit, 5xx) leaves the run alone. | ||
| if err=$(gh api "repos/$REPO/git/ref/heads/$branch" --silent 2>&1); then | ||
| continue | ||
| fi | ||
| case "$err" in *"HTTP 404"*) ;; *) echo "::warning::ref lookup for $branch failed: $err"; continue ;; esac | ||
| echo "Cancelling run $id: $branch is no longer in the queue" | ||
| gh api -X POST "repos/$REPO/actions/runs/$id/cancel" --silent \ | ||
| || echo "::warning::could not cancel run $id" | ||
| done | ||
| exit 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| #!/usr/bin/env python3 | ||
| """Run one shard of ci.yml's `test` job: `cargo test --workspace` split over | ||
| `COUNT` runners so the macOS / Windows legs stop being the merge queue's | ||
| critical path (one leg spent ~10 min linking ~240 test binaries and ~15 min | ||
| running them). | ||
|
|
||
| Shard 1 runs the unit tests (`--lib --bins`, ~4 min of the run on Windows) | ||
| and the doctests; the integration-test targets (`cargo metadata`, kind | ||
| `test`) are dealt out over the shards by name, with shard 1 taking a smaller | ||
| share to balance its unit tests. Every target lands in exactly one shard, so | ||
| the union of the shards is the old single `cargo test --workspace` run. | ||
|
|
||
| Extra arguments after `SHARD COUNT` go to every `cargo test` invocation. | ||
| Each invocation runs with `--no-fail-fast`; the exit status is non-zero if | ||
| any of them failed. | ||
|
|
||
| python3 scripts/ci-test-shard.py 1 2 | ||
| """ | ||
|
|
||
| import json | ||
| import subprocess | ||
| import sys | ||
|
|
||
| # Shard 1's share of the integration targets, relative to every other | ||
| # shard's 1.0 (it also carries the unit tests and doctests). | ||
| FIRST_SHARD_WEIGHT = 0.5 | ||
|
|
||
|
|
||
| def integration_targets(metadata): | ||
| """Sorted, de-duplicated names of the workspace's integration tests.""" | ||
| members = set(metadata["workspace_members"]) | ||
| return sorted({t["name"] for p in metadata["packages"] if p["id"] in members | ||
| for t in p["targets"] if "test" in t["kind"]}) | ||
|
|
||
|
|
||
| def partition(names, count): | ||
| """`count` lists covering `names` exactly once, in order, with the first | ||
| list weighted by FIRST_SHARD_WEIGHT.""" | ||
| if count < 1: | ||
| raise ValueError("count must be >= 1") | ||
| weights = [FIRST_SHARD_WEIGHT if count > 1 else 1.0] + [1.0] * (count - 1) | ||
| shards = [[] for _ in range(count)] | ||
| load = [0.0] * count | ||
| for name in names: | ||
| # The least-loaded shard relative to its weight; ties go to the lower index. | ||
| i = min(range(count), key=lambda k: ((load[k] + 1) / weights[k], k)) | ||
| shards[i].append(name) | ||
| load[i] += 1 | ||
| return shards | ||
|
|
||
|
|
||
| def invocations(shard, count, names, extra=()): | ||
| """The `cargo test` argument lists shard `shard` (1-based) runs.""" | ||
| if not 1 <= shard <= count: | ||
| raise ValueError(f"shard {shard} not in 1..{count}") | ||
| base = ["cargo", "test", "--workspace", "--no-fail-fast", *extra] | ||
| mine = partition(names, count)[shard - 1] | ||
| runs = [] | ||
| if shard == 1: | ||
| runs.append(base + ["--lib", "--bins"] + [a for n in mine for a in ("--test", n)]) | ||
| runs.append(base + ["--doc"]) | ||
| elif mine: | ||
| runs.append(base + [a for n in mine for a in ("--test", n)]) | ||
| return runs | ||
|
|
||
|
|
||
| def main(argv): | ||
| if len(argv) < 2: | ||
| print(__doc__, file=sys.stderr) | ||
| return 2 | ||
| shard, count = int(argv[0]), int(argv[1]) | ||
| metadata = json.loads(subprocess.run( | ||
| ["cargo", "metadata", "--no-deps", "--format-version", "1"], | ||
| check=True, capture_output=True, text=True).stdout) | ||
| names = integration_targets(metadata) | ||
| print(f"ci-test-shard: shard {shard}/{count}: " | ||
| f"{len(partition(names, count)[shard - 1])} of {len(names)} integration targets", flush=True) | ||
| status = 0 | ||
| for args in invocations(shard, count, names, argv[2:]): | ||
| print("+ " + " ".join(args), flush=True) | ||
| if subprocess.run(args).returncode != 0: | ||
| status = 1 | ||
| return status | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main(sys.argv[1:])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.