-
Notifications
You must be signed in to change notification settings - Fork 862
Jetpack Sync: Checksums - convert blacklist to allowlist #47468
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
base: trunk
Are you sure you want to change the base?
Changes from 8 commits
94810d4
4d36a8f
a692387
a54c4f4
4fba374
5196f76
70556b1
01484f1
3adbb56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: patch | ||
| Type: changed | ||
|
|
||
| Sync: Use post_type IN (allowed types) instead of NOT IN (blacklist) for the posts checksum query, for improved performance. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -85,6 +85,16 @@ class Settings { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static $is_doing_cron; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Per-request cache for allowed post types (checksum). Cleared when post_types_blacklist is updated. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @access private | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @static | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @var array|null | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static $cached_allowed_post_types_for_checksum = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Whether we're currently syncing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -293,6 +303,11 @@ public static function update_settings( $new_settings ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $updated = update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Invalidate allowlist cache when post types blacklist changes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( 'post_types_blacklist' === $setting ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self::$cached_allowed_post_types_for_checksum = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // If we set the disabled option to true, clear the queues. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( ( 'disable' === $setting || 'network_disable' === $setting ) && (bool) $value ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $listener = Listener::get_instance(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -338,16 +353,19 @@ public static function is_network_setting( $setting ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Returns escaped SQL for blacklisted post types. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Can be injected directly into a WHERE clause. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Returns escaped SQL for allowed post types (all registered minus blacklist). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @access public | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @static | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return string SQL WHERE clause. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return string SQL WHERE clause (post_type IN). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static function get_blacklisted_post_types_sql() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 'post_type NOT IN (\'' . implode( '\', \'', array_map( 'esc_sql', static::get_setting( 'post_types_blacklist' ) ) ) . '\')'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static function get_allowed_post_types_sql() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $allowed = static::get_allowed_post_types_for_checksum(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( empty( $allowed ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return '1 = 0'; // This is an SQL condition that is always false. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 'post_type IN (\'' . implode( '\', \'', array_map( 'esc_sql', $allowed ) ) . '\')'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+378
to
+383
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -367,6 +385,25 @@ public static function get_disallowed_post_types_structured() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Get allowed post types for the posts checksum (all registered minus blacklist). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Used so the checksum query can use IN (allowed) instead of NOT IN (blacklist). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Result is cached for the request to prevent unnecessary get_post_types() calls. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @return array Allowed post type names (no DB query; get_post_types() is used). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static function get_allowed_post_types_for_checksum() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( null !== self::$cached_allowed_post_types_for_checksum ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return self::$cached_allowed_post_types_for_checksum; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $all_types = get_post_types( array(), 'names' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $blacklist = static::get_setting( 'post_types_blacklist' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $allowed = array_diff( $all_types, $blacklist ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+404
to
+417
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Get allowed post types for the posts checksum (all registered minus blacklist). | |
| * Used so the checksum query can use IN (allowed) instead of NOT IN (blacklist). | |
| * | |
| * Result is cached for the request to prevent unnecessary get_post_types() calls. | |
| * | |
| * @return array Allowed post type names (no DB query; get_post_types() is used). | |
| */ | |
| public static function get_allowed_post_types_for_checksum() { | |
| if ( null !== self::$cached_allowed_post_types_for_checksum ) { | |
| return self::$cached_allowed_post_types_for_checksum; | |
| } | |
| $all_types = get_post_types( array(), 'names' ); | |
| $blacklist = static::get_setting( 'post_types_blacklist' ); | |
| $allowed = array_diff( $all_types, $blacklist ); | |
| * Get allowed post types for the posts checksum (all post types in the DB minus blacklist). | |
| * Used so the checksum query can use IN (allowed) instead of NOT IN (blacklist). | |
| * | |
| * Result is cached for the request to prevent unnecessary database calls. | |
| * | |
| * @return array Allowed post type names. | |
| */ | |
| public static function get_allowed_post_types_for_checksum() { | |
| if ( null !== self::$cached_allowed_post_types_for_checksum ) { | |
| return self::$cached_allowed_post_types_for_checksum; | |
| } | |
| global $wpdb; | |
| // Fetch all distinct post_type values present in the posts table. | |
| $all_types = $wpdb->get_col( "SELECT DISTINCT post_type FROM {$wpdb->posts}" ); | |
| if ( ! is_array( $all_types ) ) { | |
| $all_types = array(); | |
| } | |
| $blacklist = static::get_setting( 'post_types_blacklist' ); | |
| $allowed = array_diff( $all_types, $blacklist ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is very valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
posts whose post_type exists in the DB but is no longer registered (e.g. plugin removed leaving orphan rows) will now be excluded from checksums/full-sync ranges
True, but I believe the current approach makes more sense actually :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. But I think we'd need to change how incremental sync syncs non-registered post types at the same time here. In testing a non-registered post type will get synced in incremental sync, and removed during a fix checksum, whereas on trunk it won't be removed.
Removing the non existent posts (or rather posts with no post type) from being synced via incremental sync would have a knock on effect - there have been 56 million synced jetpack_sync_save_post actions in the last month where the post_status is jetpack_sync_non_registered_post_type. Constituting approx 44000 sites]. For what reason do we need to sync 'non existent posts' anyway?
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -190,7 +190,12 @@ protected static function get_default_tables() { | |
| 'range_field' => 'ID', | ||
| 'key_fields' => array( 'ID' ), | ||
| 'checksum_fields' => array( 'post_modified_gmt' ), | ||
| 'filter_values' => Sync\Settings::get_disallowed_post_types_structured(), | ||
| 'filter_values' => array( | ||
| 'post_type' => array( | ||
| 'operator' => 'IN', | ||
| 'values' => Sync\Settings::get_allowed_post_types_for_checksum(), | ||
| ), | ||
| ), | ||
coder-karen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 'is_table_enabled_callback' => function () { | ||
| return false !== Sync\Modules::get_module( 'posts' ); | ||
| }, | ||
|
|
@@ -528,6 +533,10 @@ protected function prepare_filter_values_as_sql( $filter_values = array(), $tabl | |
| case 'IN': | ||
| case 'NOT IN': | ||
| $filter_values_count = is_countable( $filter['values'] ) ? count( $filter['values'] ) : 0; | ||
| if ( 0 === $filter_values_count ) { | ||
| $result[] = 'IN' === $filter['operator'] ? '1 = 0' : '1 = 1'; | ||
| break; | ||
| } | ||
|
Comment on lines
535
to
+539
|
||
| $values_placeholders = implode( ',', array_fill( 0, $filter_values_count, '%s' ) ); | ||
| $statement = "{$key} {$filter['operator']} ( $values_placeholders )"; | ||
|
|
||
|
|
@@ -543,7 +552,7 @@ protected function prepare_filter_values_as_sql( $filter_values = array(), $tabl | |
| } | ||
|
|
||
| /** | ||
| * Build the filter query baased off range fields and values and the additional sql. | ||
| * Build the filter query based off range fields and values and the additional sql. | ||
| * | ||
| * @param int|null $range_from Start of the range. | ||
| * @param int|null $range_to End of the range. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Significance: patch | ||
| Type: other | ||
| Comment: Sync: Minor update to existing tests. | ||
|
|
||
coder-karen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
coder-karen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.