Feature Flags: key the wpcom override cache by blog - #52558
Conversation
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! |
Code Coverage SummaryCoverage changed in 1 file.
|
Wpcom_Feature_Flags memoized the override map on the first resolution and kept it for the rest of the request, even after switch_to_blog(). On WordPress.com's public API a flag is resolved before the request switches to the requested site, so that site's overrides never applied to its REST responses. Drop the cache on switch_blog and pin it with a test. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…ing on switch_blog Replace the switch_blog reset with a single cache slot guarded by get_current_blog_id(). It does not depend on switch_blog firing or on hook priority, and a switch/restore pair no longer re-reads the non-autoloaded option twice. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ac4zq7xVjwnnmCNRV8rNeb
…h blog's exact map Address review: the saving over a switch_blog reset only applies to a switch/restore pair that resolves no flag, so the docblock now argues from hook reliance instead. The blog test asserts each blog's exact override map, so an empty map can no longer satisfy it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ac4zq7xVjwnnmCNRV8rNeb
tear_down() restored blog_id to a hardcoded 1; it now restores the value the test started with, and the blog-switching test sets its starting blog itself. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ac4zq7xVjwnnmCNRV8rNeb
Phan flagged the repeated is_enabled() assertion as a duplicate adjacent statement; the second read now goes through get_overrides(). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ac4zq7xVjwnnmCNRV8rNeb
31136cd to
654df59
Compare
dognose24
left a comment
There was a problem hiding this comment.
Both threads are resolved in the follow-up commits: the docblock now argues from correctness rather than a saving that a single slot cannot deliver, and the blog-switching test pins each blog's exact map with blog 1 overriding to true, so an empty map fails every step. Merged onto trunk locally and ran the jetpack-mu-wpcom suite: 1225 tests pass.
Proposed changes
Alternative to #52555, for the same bug.
Wpcom_Feature_Flagsmemoizes a site's flag overrides in a static property the first time any flag is resolved, and never re-reads them. On WordPress.com Simple, the public API process resolves flags before it switches to the requested site (premium-analytics-dashboard-compositionis checked oninit), so the empty map cached then answers for the requested site too. Overrides set on Tools → Feature Flags therefore apply in that site's wp-admin but not to itswpcom/v2REST responses.#52555 resets the cache on
switch_blog. This PR keys it by blog instead:get_current_blog_id(). A resolution on a different blog re-reads that blog's option; a repeat on the same blog answers from the cache. Memory stays bounded — a CLI process that walks many blogs holds one map, not one per blog.switch_bloghook, so the fix does not depend on that action firing, or on firing before another callback that resolves a flag. Re-reads match a reset-on-switch_blogwhenever flags resolve inside the switched scope; the only saving is aswitch_to_blog()/restore_current_blog()pair that resolves no flag, and after the first read that saving is an object-cache lookup, not a query.get_current_blog_id()is always1on single-site, so Atomic and self-hosted sites behave as before.get_overrides(),save_overrides()andreset_overrides_cache()keep their signatures.switch_blog, and asserts each blog's exact override map, so an empty map cannot pass (it fails against Feature Flags: re-read wpcom overrides after switch_to_blog() #52555's version). The WorDBless suite is single-site, so it movesget_current_blog_id()through$GLOBALS['blog_id']and serves each blog's option through apre_option_filter instead of callingswitch_to_blog(). A second test checks that repeat resolutions on one blog read the option once.Behaviour change for reviewers: any process that resolves flags on more than one blog (the public API, cron, wp-cli walking sites) now gets each blog's own overrides, where it used to get the first blog's map for the whole process. That is the fix, but it reaches more than the REST path described here, while the changelog entry is
patch/fixed.Related product discussion/links
Does this pull request change what data or activity we track or use?
No.
Testing instructions
Automated:
jp test php packages/jetpack-mu-wpcom --filter Wpcom_Feature_FlagsOn a WordPress.com Simple sandbox, with a site whose Premium Analytics preview is switched on through its own opt-in and no
jetpack-premium-analyticsoverride (Traffic and Insights only), and with #52549 applied:public-api.wordpress.com.premium-analytics-a11n-all-sectionson, and save.https://public-api.wordpress.com/wpcom/v2/sites/<blog-id>/dashboards/jetpack-premium-analytics_dashboard/sections.trafficandinsights. With this PR, it lists every section the site qualifies for.🤖 Generated with Claude Code
https://claude.ai/code/session_01Ac4zq7xVjwnnmCNRV8rNeb