fix cancelled run handling in workflow prefetcher#38579
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refines the logic for detecting flaky tests by improving how cancelled workflow runs are processed. By introducing more granular handling for cancelled scheduled runs and integrating additional job count data, the system can more accurately identify genuine failures versus transient cancellations, thereby reducing noise in flakiness metrics. Highlights
New Features🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces logic to refine flakiness metrics by treating certain cancelled GitHub workflow runs as failures, specifically those that are scheduled and either exceed a one-hour duration or have zero jobs. It adds asynchronous API calls to fetch job counts and updates the data models to support this. Feedback focuses on optimizing performance and resource management, including reusing a single aiohttp.ClientSession for batch requests, adding a giveup condition to the retry logic for non-recoverable errors, and reordering operations to slice the run list before performing API-intensive enrichment.
| async def enrich_cancelled_schedule_run_jobs(workflows, semaphore, headers): | ||
| tasks = [] | ||
| for workflow in workflows: | ||
| for run in workflow.runs: | ||
| if run.event != "schedule" or run.status != "cancelled": | ||
| continue | ||
| if run_duration(run.started_at, run.updated_at) > CANCELLED_FAILURE_MIN_DURATION: | ||
| continue | ||
| jobs_url = ( | ||
| f"https://api.github.com/repos/{GIT_ORG}/beam/actions/runs/{run.id}/jobs" | ||
| ) | ||
| tasks.append(fetch_jobs_count(jobs_url, run, semaphore, headers)) | ||
|
|
||
| if tasks: | ||
| await asyncio.gather(*tasks) |
There was a problem hiding this comment.
Creating a new aiohttp.ClientSession for every request is inefficient and can lead to resource exhaustion. It is recommended to create a single session and reuse it for all requests in the batch. Additionally, consider passing this session to fetch_jobs_count.
| async def enrich_cancelled_schedule_run_jobs(workflows, semaphore, headers): | |
| tasks = [] | |
| for workflow in workflows: | |
| for run in workflow.runs: | |
| if run.event != "schedule" or run.status != "cancelled": | |
| continue | |
| if run_duration(run.started_at, run.updated_at) > CANCELLED_FAILURE_MIN_DURATION: | |
| continue | |
| jobs_url = ( | |
| f"https://api.github.com/repos/{GIT_ORG}/beam/actions/runs/{run.id}/jobs" | |
| ) | |
| tasks.append(fetch_jobs_count(jobs_url, run, semaphore, headers)) | |
| if tasks: | |
| await asyncio.gather(*tasks) | |
| async def enrich_cancelled_schedule_run_jobs(workflows, semaphore, headers): | |
| tasks = [] | |
| async with aiohttp.ClientSession() as session: | |
| for workflow in workflows: | |
| for run in workflow.runs: | |
| if run.event != "schedule" or run.status != "cancelled": | |
| continue | |
| if run_duration(run.started_at, run.updated_at) > CANCELLED_FAILURE_MIN_DURATION: | |
| continue | |
| jobs_url = ( | |
| f"https://api.github.com/repos/{GIT_ORG}/beam/actions/runs/{run.id}/jobs" | |
| ) | |
| tasks.append(fetch_jobs_count(session, jobs_url, run, semaphore, headers)) | |
| if tasks: | |
| await asyncio.gather(*tasks) |
| @backoff.on_exception(backoff.constant, aiohttp.ClientResponseError, max_tries=5) | ||
| async def fetch_jobs_count(url, run, semaphore, headers): | ||
| async with semaphore: | ||
| async with aiohttp.ClientSession() as session: | ||
| async with session.get(url, headers=headers) as response: | ||
| if response.status == 200: | ||
| result = await response.json() | ||
| run.jobs_count = result.get("total_count", 0) | ||
| return | ||
| if response.status == 403: | ||
| print(f"Retry for: {url}") | ||
| headers["Authorization"] = get_token() | ||
| raise aiohttp.ClientResponseError( | ||
| response.request_info, | ||
| response.history, | ||
| status=response.status, | ||
| message=response.reason, | ||
| headers=response.headers, | ||
| ) |
There was a problem hiding this comment.
The backoff decorator should include a giveup condition to avoid retrying on non-recoverable errors like 404 (Not Found). Also, the function signature should be updated to accept a shared session to avoid creating one per request.
@backoff.on_exception(
backoff.constant,
aiohttp.ClientResponseError,
max_tries=5,
giveup=lambda e: e.status not in [403, 429, 500, 502, 503, 504]
)
async def fetch_jobs_count(session, url, run, semaphore, headers):
async with semaphore:
async with session.get(url, headers=headers) as response:
if response.status == 200:
result = await response.json()
run.jobs_count = result.get("total_count", 0)
return
if response.status == 403:
print(f"Retry for: {url}")
headers["Authorization"] = get_token()
raise aiohttp.ClientResponseError(
response.request_info,
response.history,
status=response.status,
message=response.reason,
headers=response.headers,
)| workflows_list = list(workflows.values()) | ||
| await enrich_cancelled_schedule_run_jobs(workflows_list, semaphore, headers) | ||
| for workflow in workflows_list: | ||
| runs = sorted(workflow.runs, key=lambda r: r.started_at, reverse=True) | ||
| workflow.runs = runs[: int(GH_NUMBER_OF_WORKFLOW_RUNS_TO_FETCH)] | ||
| workflow.runs = prepare_workflow_runs_for_flakiness( | ||
| runs[: int(GH_NUMBER_OF_WORKFLOW_RUNS_TO_FETCH)] | ||
| ) |
There was a problem hiding this comment.
It is more efficient to sort and slice the workflow runs before calling enrich_cancelled_schedule_run_jobs. This avoids making unnecessary API calls to fetch job counts for runs that will eventually be discarded by the slice.
workflows_list = list(workflows.values())
for workflow in workflows_list:
workflow.runs = sorted(workflow.runs, key=lambda r: r.started_at, reverse=True)[: int(GH_NUMBER_OF_WORKFLOW_RUNS_TO_FETCH)]
await enrich_cancelled_schedule_run_jobs(workflows_list, semaphore, headers)
for workflow in workflows_list:
workflow.runs = prepare_workflow_runs_for_flakiness(workflow.runs)|
Assigning reviewers: R: @chamikaramj added as fallback since no labels match configuration Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
This change updates how cancelled workflow runs are counted for flaky test detection. Pull request runs that end as cancelled are ignored and for scheduled runs that are cancelled, we count them as failed if they ran for more than one hour or if they have no jobs. Short cancelled scheduled runs that still have jobs are ignored and for short cancelled schedule runs we call the GitHub jobs API only when we need to check if there are zero jobs
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, commentfixes #<ISSUE NUMBER>instead.CHANGES.mdwith noteworthy changes.See the Contributor Guide for more tips on how to make review process smoother.
To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md
GitHub Actions Tests Status (on master branch)
See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.