Skip to content

Connection: memoize connection owner's ID #44282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 15, 2025

Conversation

sergeymitr
Copy link
Contributor

@sergeymitr sergeymitr commented Jul 11, 2025

Proposed changes:

  • Prevent excessive database queries by memoizing connection owner ID throughout the request.
  • If connection owner gets changed or removed, clean the memoized value.

WordPress is efficient in caching user data, so it doesn't rerun database queries to fetch user data if it's already been fetched.
The problems appear when there's a mismatch, and connection owner ID does not match any user accounts. In that case WordPress will not be able to cache the results of the user data query, so it will be queried multiple times.

Memoizing connection ID prevents this kind of situation by storing 0 if there's no connection owner, or if its invalid (e.g. mismatch described above). Therefore, the user info query will not be repeated.

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Does this pull request change what data or activity we track or use?

No.

Testing instructions:

Confirm the issue is fixed:

  1. First, try to reproduce the issue on trunk:
  2. Connect Jetpack.
  3. Use "Jetpack Debug -> Broken Token" to "Randomize Primary User ID and move the user token together". Confirm that the new connection owner ID does not match any of existing user accounts.
  4. Install "Query Monitor" plugin.
  5. Go to Jetpack Dashboard and check for "Database Queries -> Duplicate Queries". There should be a huge number of duplicate requests (~80) trying to fetch user data by this random ID (SELECT * FROM wp_users WHERE ID = '12345' LIMIT 1)
  6. Now, switch to this branch: add/connection-owner-id-memoization.
  7. Repeat step 5, confirm the number of duplicated queries is now pretty low (less than 10).

Play around with the connection, confirm it continues to work as expected:

  1. Connect Jetpack.
  2. Disconnect user, then reconnect.
  3. Disconnect Jetpack, reconnect it in site-only mode.
  4. Connect the user.
  5. Connect a secondary user, then disconnect them.

@sergeymitr sergeymitr added this to the jetpack/14.9 milestone Jul 11, 2025
@sergeymitr sergeymitr requested a review from Copilot July 11, 2025 13:00
@sergeymitr sergeymitr self-assigned this Jul 11, 2025
@sergeymitr sergeymitr added [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Status] In Progress labels Jul 11, 2025
Copy link
Contributor

github-actions bot commented Jul 11, 2025

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack or WordPress.com Site Helper), and enable the add/connection-owner-id-memoization branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack add/connection-owner-id-memoization
bin/jetpack-downloader test jetpack-mu-wpcom-plugin add/connection-owner-id-memoization

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

Copy link
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

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:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces memoization for the connection owner ID to avoid redundant database queries, and ensures the cached value is cleared whenever the owner changes or is removed.

  • Add a private static $connection_owner_id to cache the owner ID and update get_connection_owner_id() to use it.
  • Hook into option updates and key methods (update_connection_owner, disconnect_user, delete_all_connection_tokens) to reset the cached ID when the owner is modified.
  • Add a unit test to verify that the memoized value is reset correctly and update the changelog.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
projects/packages/connection/src/class-manager.php Introduce $connection_owner_id, update methods to set/reset it, and add invalidation hooks.
projects/packages/connection/tests/php/ManagerTest.php Add test test_update_connection_owner_with_memoization_reset to verify cache invalidation logic.
projects/packages/connection/changelog/add-connection-owner-id-memoization Document the new memoization feature.
Comments suppressed due to low confidence (2)

projects/packages/connection/tests/php/ManagerTest.php:606

  • [nitpick] Consider renaming this test method to something more concise and descriptive, e.g., test_update_connection_owner_resets_memoization.
	public function test_update_connection_owner_with_memoization_reset() {

projects/packages/connection/tests/php/ManagerTest.php:678

  • Add tests for disconnect_user() and delete_all_connection_tokens() to ensure the memoized connection owner ID is cleared in those scenarios.
		$this->assertEquals( $admin3_id, $this->manager->get_connection_owner_id() );

Copy link

jp-launch-control bot commented Jul 11, 2025

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/packages/connection/src/class-manager.php 607/1010 (60.10%) -0.92% 12 💔

Full summary · PHP report · JS report

If appropriate, add one of these labels to override the failing coverage check: Covered by non-unit tests Use to ignore the Code coverage requirement check when E2Es or other non-unit tests cover the code Coverage tests to be added later Use to ignore the Code coverage requirement check when tests will be added in a follow-up PR I don't care about code coverage for this PR Use this label to ignore the check for insufficient code coveage.

Co-authored-by: Copilot <[email protected]>
@sergeymitr sergeymitr force-pushed the add/connection-owner-id-memoization branch from ceab7db to cdbae24 Compare July 11, 2025 17:15
@sergeymitr sergeymitr force-pushed the add/connection-owner-id-memoization branch from cdbae24 to 9bdb753 Compare July 11, 2025 17:58
@sergeymitr sergeymitr force-pushed the add/connection-owner-id-memoization branch from bfb9986 to 14343af Compare July 11, 2025 18:49
@sergeymitr sergeymitr added [Status] Needs Review This PR is ready for review. and removed [Status] In Progress labels Jul 11, 2025
@sergeymitr sergeymitr requested a review from bindlegirl July 11, 2025 19:47
Copy link
Contributor

@bindlegirl bindlegirl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good and works as advertised. Thank you for taking care of this!

@bindlegirl bindlegirl added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review This PR is ready for review. labels Jul 15, 2025
@sergeymitr sergeymitr merged commit 8546682 into trunk Jul 15, 2025
92 of 94 checks passed
@sergeymitr sergeymitr deleted the add/connection-owner-id-memoization branch July 15, 2025 14:04
@github-actions github-actions bot removed the [Status] Ready to Merge Go ahead, you can push that green button! label Jul 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Connection [Tests] Includes Tests [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants