Add read-replica routing for Perfherder read-only endpoints#9528
Open
camd wants to merge 9 commits into
Open
Conversation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Routes 10 read-only Perfherder viewsets to the read replica (PerformanceSignatureViewSet, PerformancePlatformViewSet, PerformanceJobViewSet, PerformanceDatumViewSet, PerformanceBugTemplateViewSet, PerformanceIssueTrackerViewSet, PerformanceSummary, PerformanceAlertSummaryTasks, PerfCompareResults, TestSuiteHealthViewSet). Adds integration tests for signature list and summary endpoints. Updates test_perfcompare_api.py, test_performance_data_api.py, and test_performance_bug_template_api.py to declare read_replica database access. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Two review fixes: 1. When the kill switch is off (READ_REPLICA_ENABLED=false or READ_REPLICA_DATABASE_URL unset), the mixin previously still wrapped dispatch, set the thread-local (a no-op without a router), and would emit a misleading "db_routing_fallback" log on any primary failure. The mixin now checks connections.databases for the alias and bypasses wrapping when absent. 2. The three test files that gained databases=["default","read_replica"] implicitly relied on fixtures pulling in transactional_db. Make the transactional requirement explicit with transaction=True so the behavior survives future fixture refactors. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #9528 +/- ##
==========================================
+ Coverage 82.75% 82.84% +0.08%
==========================================
Files 604 607 +3
Lines 34973 35190 +217
Branches 3261 3276 +15
==========================================
+ Hits 28943 29153 +210
- Misses 5661 5668 +7
Partials 369 369 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| into reading from the ``read_replica`` database alias. The router only routes | ||
| models whose Django app label is in :data:`READ_REPLICA_APP_ALLOW_LIST`. | ||
|
|
||
| Design: .claude/plans/READ_REPLICA_DESIGN.md |
| The router and mixin are then active in dev. Routing is a no-op unless an | ||
| endpoint opts in via `ReadReplicaMixin`; see | ||
| `treeherder/config/db_routing.py` and the design at | ||
| `.claude/plans/READ_REPLICA_DESIGN.md`. |
| _RecordingView.raise_on_call = 0 | ||
| _RecordingView.call_count = 0 | ||
| _RecordingView.saw_use_replica = [] | ||
| yield |
Collaborator
There was a problem hiding this comment.
What's the benefit of this?
The feature is a no-op locally with the default .env (both env vars unset → no read_replica alias → mixin early-returns). Standard local dev doesn't need to know about the routing. The design, code-level docstring, and PR deploy notes cover the cases that do. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Successful replica routing was previously silent — only the fallback path emitted a log line. Adds a single DEBUG-level log per routed request so a developer (or ops) can grep \`db_routing routed_to=read_replica\` to confirm which requests hit the replica. Prod-silent by default (logger threshold is INFO); enable locally with LOGGING_LEVEL=DEBUG. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
Routes 11 read-only Perfherder GET endpoints through a separate
read_replicaPostgreSQL connection to relieve write-side contention on the primary database. Gated behind two env vars (READ_REPLICA_DATABASE_URL+READ_REPLICA_ENABLED) so it ships as a no-op until ops flips the switch.ReadReplicaRouter(DjangoDATABASE_ROUTERS) routes reads to the replica only when a thread-local flag is set and the model's app label is in an explicit allow-list ({"perf", "model"}).ReadReplicaMixinflips that flag forGET/HEAD/OPTIONSonly, on a one-shot retry against primary if the replica raisesOperationalError/InterfaceError, and emits adb_routing_fallbackwarning log. The mixin is a true no-op when the alias isn't configured.treeherder/webapp/api/performance_data.py. The 3 mutating viewsets (PerformanceAlertSummaryViewSet,PerformanceAlertViewSet,PerformanceTagViewSet) are intentionally left on primary so create→read flows stay consistent.Design doc:
.claude/plans/READ_REPLICA_DESIGN.md(local). Mechanism is reusable — applying it to Logviewer / Intermittent Failures View later is a one-line viewset change each.Test plan
CaptureQueriesContextasserting the replica alias actually serves opted-in endpoints and does NOT serve excluded ones — 4 tests.READ_REPLICA_DATABASE_URLin stage; flipREAD_REPLICA_ENABLED=true; soak; watchdb_routing_fallbacklog volume + primary DB CPU; then prod.READ_REPLICA_ENABLED=false(one env-var flip) and verify all routed endpoints continue returning correct data via primary.🤖 Generated with Claude Code