perf(worker): Optimize flake processing database queries#820
perf(worker): Optimize flake processing database queries#820sentry[bot] wants to merge 1 commit intomainfrom
Conversation
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ 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"] | ||
| ) |
There was a problem hiding this comment.
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.
Reviewed by Cursor Bugbot for commit 0cccc35. Configure here.
| if ended_flakes: | ||
| Flake.objects.bulk_update( | ||
| ended_flakes, ["end_date", "count", "recent_passes_count"] | ||
| ) |
There was a problem hiding this comment.
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)
Reviewed by Cursor Bugbot for commit 0cccc35. Configure here.
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |


Fixes WORKER-Y7R. The issue was that: Individual
Flake.save()calls withinhandle_passfor each passing testrun cause an N+1 write query pattern.get_testrunsto fetch test runs for multiple upload IDs in a single query, reducing database calls.process_single_uploadto receive pre-fetched test runs, improving processing efficiency.defaultdictto 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_testrunsnow fetches all recentTestruns for a commit’s uploads in one query and groups them byupload_idbefore processing.Eliminates per-flake
save()calls when a flake expires after 30 passes by collecting those flakes and performing abulk_update(while continuing tobulk_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.