Skip to content

perf(worker): Optimize flake processing database queries#820

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

perf(worker): Optimize flake processing database queries#820
sentry[bot] wants to merge 1 commit intomainfrom
seer/perf/ta-flake-processing-bulk-queries

Conversation

@sentry
Copy link
Copy Markdown
Contributor

@sentry sentry bot commented Apr 15, 2026

Fixes WORKER-Y7R. The issue was that: Individual Flake.save() calls within handle_pass for each passing testrun cause an N+1 write query pattern.

  • Refactored get_testruns to fetch test runs for multiple upload IDs in a single query, reducing database calls.
  • Modified process_single_upload to receive pre-fetched test runs, improving processing efficiency.
  • Implemented bulk updating for flakes that have met their pass criteria, reducing individual database save operations.
  • Utilized defaultdict to efficiently group test runs by upload ID after a single bulk fetch.

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

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
Changes flake processing to batch-query testruns and defer flake updates into bulk operations, which could affect correctness if grouping or update fields are incomplete. Logic is localized but touches production database write paths.

Overview
Speeds up flake processing by batching DB access: get_testruns now fetches all recent Testruns for a commit’s uploads in one query and groups them by upload_id before processing.

Eliminates per-flake save() calls when a flake expires after 30 passes by collecting those flakes and performing a bulk_update (while continuing to bulk_create(..., update_conflicts=True) for active/new flakes), reducing N+1 write patterns.

Reviewed by Cursor Bugbot for commit 0cccc35. 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 2 potential issues.

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 0cccc35. Configure here.

if ended_flakes:
Flake.objects.bulk_update(
ended_flakes, ["end_date", "count", "recent_passes_count"]
)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bulk update for ended flakes omits fail_count field

High Severity

The bulk_update for ended_flakes saves ["end_date", "count", "recent_passes_count"] but omits fail_count. A flake can have its fail_count incremented by handle_failure before later reaching 30 passes and being moved to ended_flakes. Since ended flakes are removed from curr_flakes, they're excluded from the subsequent bulk_create (which does include fail_count). The original save() call persisted all fields — this refactor silently drops fail_count changes for ended flakes.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 0cccc35. Configure here.

if ended_flakes:
Flake.objects.bulk_update(
ended_flakes, ["end_date", "count", "recent_passes_count"]
)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Newly created flakes in ended_flakes crash bulk_update

Medium Severity

If handle_failure creates a new in-memory Flake (with id=None since BigAutoField is unset) and it later reaches 30 consecutive passes in handle_pass, it gets appended to ended_flakes and removed from curr_flakes. When Flake.objects.bulk_update(ended_flakes, ...) runs, Django raises ValueError because all objects must have a primary key set. The original save() would correctly INSERT the new object. This crash aborts the entire commit's flake processing.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 0cccc35. Configure here.

@sentry
Copy link
Copy Markdown
Contributor Author

sentry bot commented Apr 15, 2026

Codecov Report

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

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #820   +/-   ##
=======================================
  Coverage   92.25%   92.25%           
=======================================
  Files        1306     1306           
  Lines       48004    48010    +6     
  Branches     1636     1636           
=======================================
+ Hits        44286    44292    +6     
  Misses       3407     3407           
  Partials      311      311           
Flag Coverage Δ
workerintegration 58.56% <21.42%> (-0.02%) ⬇️
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-notifications bot commented Apr 15, 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