Eliminate lazy repository FK fetches in worker tasks and push health_summary - #9785
Open
camd wants to merge 1 commit into
Open
Eliminate lazy repository FK fetches in worker tasks and push health_summary#9785camd wants to merge 1 commit into
camd wants to merge 1 commit into
Conversation
…summary Production logs show chained tracebacks (KeyError: 'repository' as __context__) from Django's ForwardManyToOneDescriptor: model instances loaded without select_related trigger a one-off Repository fetch deep inside long-running celery tasks, where the DB connection may have gone stale since the instance was first loaded (e.g. after slow log downloads), so the lazy fetch fails mid-task instead of failing fast at task start under the normal retry path. Add select_related for the repository FK chain at the task entry-point querysets (log parser, perfherder ingestion, alert generation, classification loader, artifact loading) and in the health_summary revision branch, which was missing the select_related its author branch already had. This also drops a query per task/request.
✅ Deploy Preview for treeherder ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #9785 +/- ##
===========================================
+ Coverage 69.15% 83.42% +14.26%
===========================================
Files 637 638 +1
Lines 38505 38537 +32
Branches 3466 3472 +6
===========================================
+ Hits 26630 32150 +5520
+ Misses 11481 6236 -5245
+ Partials 394 151 -243 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Production GCP logs show chained tracebacks of this shape:
That
KeyErroris Django's normal FK cache-miss path: it is caught insideForwardManyToOneDescriptor.__get__, which then fetches theRepositoryrow from the DB. When that one-off fetch fails, the failure is chained onto theKeyError("During handling of the above exception...").The trigger is model instances loaded without
select_related("repository")in long-running celery tasks: the instance is fetched at task start, the task then spends a long time on other work (e.g. downloading logs over HTTP), and only afterwards touches.repository— by which point the connection backing the lazy fetch can have gone stale, so the task blows up mid-work instead of failing fast at task start under the normal retry path.Fix
Add
select_relatedfor the repository FK chain at the entry-point querysets:JobLogqueryset getsselect_related("job__repository")— the lazy accesses are onjob_log.job.repository(two lazy hops) inpost_log_artifacts,failureline.py, and the error-summary pathjob__repositorytreatment; coversetl/perf.py'sjob.repository.performance_alerts_enabledreadsselect_related("repository", "framework")— both are read repeatedly ingenerate_new_alerts_in_series, including inside theselect_for_updatetransaction added in Bug 2035204 - Prevent creation of duplicate AlertSummary rows for the same push #9754select_related("repository", "job_type", "push")— all three FKs are readJoblookup (Job.__str__readsself.repositorywhen interpolated into a log line) andget_push'sPushlookup now select_relatedhealth_summaryAPI: therevision=branch gets theselect_related("repository")itsauthor=sibling branch already hadBesides removing the failure mode, this drops a query (or several) per task/request.
Testing
Ran the backend suites for every touched module —
tests/log_parser/,tests/etl/test_classification_loader.py,test_load_artifacts.py,test_perf_data_load.py,tests/webapp/api/test_push_api.py,tests/perfalert/test_alerts.py: 231 passed, 1 pre-existing xfail. No new tests: the change is query-shape only and all modified paths are covered by existing functional tests.