Skip to content

perf(worker): Optimize flake processing by batch fetching testruns#801

Open
sentry[bot] wants to merge 1 commit intomainfrom
seer/perf/optimize-flake-processing
Open

perf(worker): Optimize flake processing by batch fetching testruns#801
sentry[bot] wants to merge 1 commit intomainfrom
seer/perf/optimize-flake-processing

Conversation

@sentry
Copy link
Copy Markdown
Contributor

@sentry sentry bot commented Apr 14, 2026

Fixes WORKER-Y77. The issue was that: N+1 queries occur because get_relevant_uploads doesn't eager-load ReportSession relationships, leading to repeated database hits per upload.

  • Optimized get_relevant_uploads to eagerly load related report, commit, and repository objects using select_related.
  • Refactored flake processing to batch fetch all relevant testruns for multiple uploads in a single query, significantly reducing database calls.
  • Introduced get_testruns_for_uploads to efficiently retrieve testruns for a list of upload IDs.
  • Modified process_single_upload to accept pre-fetched testruns, decoupling data retrieval from processing logic.

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

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 database query patterns and refactors process_single_upload to use pre-fetched testruns, which could affect which testruns are processed per upload if the grouping/filtering is wrong.

Overview
Improves test flake processing performance by eliminating per-upload DB fetches.

get_relevant_uploads now uses select_related to eager-load report/commit/repository, and process_flakes_for_commit batch-fetches all recent Testruns for the commit’s uploads via new get_testruns_for_uploads, passing them into process_single_upload instead of querying per upload.

Reviewed by Cursor Bugbot for commit 01a2798. 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 01a2798. Configure here.

).order_by("timestamp")


def get_testruns_for_uploads(upload_ids: list[int]) -> dict[int, list[Testrun]]:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Dead function get_testruns left after refactoring

Low Severity

The get_testruns function in ta_process_flakes.py is now unused dead code. It was previously called by process_single_upload, but this refactoring replaced that call with the new batch-fetching get_testruns_for_uploads. No other file imports get_testruns from this module (the one in detect_flakes.py is a separate, locally-defined function). This dead function adds confusion about which function is canonical for fetching testruns.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 01a2798. Configure here.

@sentry
Copy link
Copy Markdown
Contributor Author

sentry bot commented Apr 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.25%. Comparing base (8c332b1) to head (01a2798).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #801      +/-   ##
==========================================
- Coverage   92.25%   92.25%   -0.01%     
==========================================
  Files        1306     1306              
  Lines       48004    48012       +8     
  Branches     1636     1636              
==========================================
+ Hits        44286    44292       +6     
- Misses       3407     3409       +2     
  Partials      311      311              
Flag Coverage Δ
workerintegration 58.56% <9.09%> (-0.03%) ⬇️
workerunit 90.38% <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-notifications bot commented Apr 14, 2026

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