Skip to content

fix(worker): N+1 query in ProcessFlakesTask#1027

Open
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/worker-process-flakes-n-plus-1-zWbmnE
Open

fix(worker): N+1 query in ProcessFlakesTask#1027
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/worker-process-flakes-n-plus-1-zWbmnE

Conversation

@sentry
Copy link
Copy Markdown
Contributor

@sentry sentry Bot commented Jun 6, 2026

This PR addresses two N+1 query issues identified in app.tasks.flakes.ProcessFlakesTask, specifically within apps/worker/services/test_analytics/ta_process_flakes.py.

Problem:

  1. Testrun Fetching: The process_flakes_for_commit function iterated over uploads, and for each upload, process_single_upload called get_testruns(upload), resulting in a separate database query for testruns per upload (N+1 queries).
  2. Flake Expiration Saving: When a flake reached 30 recent passes in handle_pass, it would call .save() individually, leading to multiple single-row updates.

Solution:

  1. Batch Testrun Fetching: Introduced get_testruns_for_uploads to fetch all relevant testruns for all uploads in a single batched query using upload_id__in=[...]. The process_single_upload function was refactored to accept pre-fetched testruns.
  2. Bulk Flake Updates: Modified handle_pass to collect expired flakes into a list. After processing all uploads, these collected flakes are then updated in a single Flake.objects.bulk_update call.

These changes significantly reduce the database load and improve the performance of the ProcessFlakesTask by eliminating the N+1 query patterns.

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.

Fixes WORKER-YQ4

@codecov-notifications
Copy link
Copy Markdown

codecov-notifications Bot commented Jun 6, 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!

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sentry
Copy link
Copy Markdown
Contributor Author

sentry Bot commented Jun 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.90%. Comparing base (508927b) to head (89e4e3d).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1027   +/-   ##
=======================================
  Coverage   91.90%   91.90%           
=======================================
  Files        1318     1318           
  Lines       50711    50719    +8     
  Branches     1625     1625           
=======================================
+ Hits        46604    46612    +8     
  Misses       3801     3801           
  Partials      306      306           
Flag Coverage Δ
workerintegration 58.54% <17.64%> (-0.03%) ⬇️
workerunit 90.35% <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 Harness.
📢 Have feedback on the report? Share it here.

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