Skip to content

perf(worker): Optimize flake processing by batching testrun queries#880

Open
sentry[bot] wants to merge 1 commit intomainfrom
seer/perf/batch-flake-testruns-F1PN8h
Open

perf(worker): Optimize flake processing by batching testrun queries#880
sentry[bot] wants to merge 1 commit intomainfrom
seer/perf/batch-flake-testruns-F1PN8h

Conversation

@sentry
Copy link
Copy Markdown
Contributor

@sentry sentry Bot commented Apr 20, 2026

Fixes WORKER-Y9B. The issue was that: process_flakes_for_commit fetches testruns individually per upload, causing N+1 queries.

  • Refactored get_testruns to get_testruns_for_uploads to allow fetching testruns for multiple upload IDs in a single database query.
  • Modified process_single_upload to accept a pre-fetched list of Testrun objects, decoupling testrun retrieval from processing logic.
  • Updated process_flakes_for_commit to fetch all relevant testruns for a commit's uploads in a single batch query, then group them by upload ID using defaultdict.
  • Passed the pre-fetched and grouped testruns to process_single_upload to reduce database query overhead.

This fix was generated by Seer in Sentry, triggered automatically. 👁️ Run ID: 13598118

Not quite right? Click here to continue debugging with Seer.

Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. In 2022 this entity acquired Codecov and as result Sentry is going to need some rights from me in order to utilize my contributions in this PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.


Note

Medium Risk
Moderate risk: changes the flake-processing query and iteration order by batching Testrun fetches across uploads; mistakes in grouping or ordering could cause missed/incorrect outcome updates for some uploads.

Overview
Optimizes process_flakes_for_commit by eliminating per-upload Testrun queries: it now fetches all recent testruns for the commit’s uploads in one upload_id__in query, groups them by upload, and passes the pre-fetched lists into process_single_upload.

Refactors helpers accordingly (get_testruns_for_uploads, process_single_upload signature) and adjusts ordering to order_by("upload_id", "timestamp") to keep per-upload processing stable while reducing DB load.

Reviewed by Cursor Bugbot for commit 834f4ba. Bugbot is set up for automated code reviews on this repo. Configure here.

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 834f4ba. Configure here.

@sentry_sdk.trace
def process_single_upload(
upload: ReportSession, curr_flakes: dict[bytes, Flake], repo_id: int
upload_id: int,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Unused upload_id parameter in process_single_upload

Low Severity

The upload_id parameter added to process_single_upload is never referenced anywhere in the function body. It's passed in from process_flakes_for_commit at the call site but serves no purpose inside the function, making it dead code that may confuse future readers about its intended use.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 834f4ba. Configure here.

@sentry_sdk.trace
def process_single_upload(
upload: ReportSession, curr_flakes: dict[bytes, Flake], repo_id: int
upload_id: int,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Bug: The upload_id parameter in the process_single_upload function is unused and can be removed.
Severity: LOW

Suggested Fix

Remove the upload_id parameter from the function definition of process_single_upload at line 84 and update the corresponding call site at lines 132-134 to no longer pass this argument.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: apps/worker/services/test_analytics/ta_process_flakes.py#L84

Potential issue: The function `process_single_upload` is defined with a parameter
`upload_id`. However, this parameter is not used anywhere within the function's body.
The value is passed correctly from the call site, but since it is not utilized, it
serves no functional purpose. This is a result of a refactoring that decoupled testrun
retrieval from processing, leaving the parameter as a vestige. While it does not cause
any runtime errors or incorrect behavior, it represents dead code that can be confusing
for future maintenance.

Did we get this right? 👍 / 👎 to inform future reviews.

@sentry
Copy link
Copy Markdown
Contributor Author

sentry Bot commented Apr 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.26%. Comparing base (b3cda5a) to head (834f4ba).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #880   +/-   ##
=======================================
  Coverage   92.26%   92.26%           
=======================================
  Files        1307     1307           
  Lines       48011    48015    +4     
  Branches     1632     1632           
=======================================
+ Hits        44295    44299    +4     
  Misses       3407     3407           
  Partials      309      309           
Flag Coverage Δ
workerintegration 58.53% <22.22%> (-0.01%) ⬇️
workerunit 90.39% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@codecov-notifications
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants