fix(2meet-data-optimizer): add declared flat-table indexes from WP-CLI or WP-Cron only, one process at a time
- ensure_group_indexes() runs the ALTER only under WP-CLI or WP-Cron (filter tmdo_schema_ddl_allowed); web requests, including /wp-admin/ before auth_redirect() and admin-post.php, schedule tmdo_ensure_group_indexes instead - MySQL GET_LOCK keeps it to one process (add_option() is INSERT ... ON DUPLICATE KEY UPDATE and cannot serve as a lock); released by MySQL if the process dies - the applied signature is recorded only when every ALTER succeeds; failures log schema.index_failed and stay in pending_indexes(), which doctor reports - new tables get their declared cidx_ indexes in CREATE TABLE - test stub wpdb::query()/insert() return false on SQL errors like wpdb - Verify: GroupCompositeIndexTest 7/7, unit 698, integration 576, phpcs, stan - PR: PR-090a
This commit is contained in:
@@ -306,6 +306,9 @@ class TMDO_Core {
|
||||
add_action( 'add_option_' . TMDO_Hook_Bus_Bridge::OPTION, array( 'TMDO_Drift_Check', 'on_hook_bus_added' ), 10, 2 );
|
||||
add_action( TMDO_Drift_Check::RESYNC_HOOK, array( 'TMDO_Drift_Check', 'run_scheduled_fix' ) );
|
||||
|
||||
// 1.3.0: declared indexes a visitor request left for WP-Cron.
|
||||
add_action( TMDO_Schema_Manager::INDEXES_HOOK, array( 'TMDO_Schema_Manager', 'ensure_pending_indexes' ) );
|
||||
|
||||
// v2.3.0 M6: daily snapshot prune cron (paired with health check).
|
||||
add_action( 'wpdo_snapshot_prune_daily', array( 'TMDO_Snapshot_Pruner', 'cron_run' ) );
|
||||
|
||||
|
||||
@@ -789,6 +789,7 @@ class TMDO_Installer {
|
||||
wp_clear_scheduled_hook( 'wpdo_post_shadow_verify' );
|
||||
wp_clear_scheduled_hook( 'wpdo_term_comment_shadow_verify' );
|
||||
wp_clear_scheduled_hook( 'tmdo_drift_resync' );
|
||||
wp_clear_scheduled_hook( 'tmdo_ensure_group_indexes' );
|
||||
wp_clear_scheduled_hook( 'wpdo_post_stress_test_batch' );
|
||||
wp_clear_scheduled_hook( 'wpdo_user_stress_test_batch' );
|
||||
wp_clear_scheduled_hook( 'wpdo_term_stress_test_batch' );
|
||||
@@ -993,6 +994,7 @@ class TMDO_Installer {
|
||||
'wpdo_post_shadow_verify',
|
||||
'wpdo_term_comment_shadow_verify',
|
||||
'tmdo_drift_resync',
|
||||
'tmdo_ensure_group_indexes',
|
||||
'wpdo_post_stress_test_batch',
|
||||
'wpdo_user_stress_test_batch',
|
||||
'wpdo_term_stress_test_batch',
|
||||
|
||||
@@ -23,6 +23,9 @@ final class TMDO_Schema_Manager {
|
||||
/** Option: "entity/group" → md5 of the composite index definition already applied. */
|
||||
public const INDEXES_OPTION = 'tmdo_entity_indexes';
|
||||
|
||||
/** Single cron event that adds declared indexes a visitor request had to leave out. */
|
||||
public const INDEXES_HOOK = 'tmdo_ensure_group_indexes';
|
||||
|
||||
/** Request-scoped cache for table_exists() — prevents repeated SHOW TABLES per request. */
|
||||
private static array $table_exists_cache = array();
|
||||
|
||||
@@ -144,6 +147,11 @@ final class TMDO_Schema_Manager {
|
||||
}
|
||||
}
|
||||
|
||||
// A new table gets its declared composite indexes while it is empty.
|
||||
foreach ( TMDO_Entity_Registry::get_group_indexes( $entity_type, $group_name ) as $name => $keys ) {
|
||||
$sql_indexes[] = 'KEY `cidx_' . self::sanitize_column_name( (string) $name ) . '` (' . implode( ', ', array_map( static fn( $key ) => '`' . self::sanitize_column_name( $key ) . '`', $keys ) ) . ')';
|
||||
}
|
||||
|
||||
$columns_sql = implode( ",\n ", $sql_columns );
|
||||
$indexes_sql = implode( ",\n ", $sql_indexes );
|
||||
|
||||
@@ -176,6 +184,12 @@ final class TMDO_Schema_Manager {
|
||||
* applied definition is remembered per group (one autoloaded option), so
|
||||
* requests only run SHOW INDEX / ALTER when the declaration changes.
|
||||
*
|
||||
* On a large table the ALTER takes seconds, so it runs only from WP-CLI or
|
||||
* WP-Cron (ddl_allowed(); other requests schedule INDEXES_HOOK) and in one
|
||||
* process at a time (MySQL GET_LOCK on index_lock_name()). Until every
|
||||
* ALTER succeeds the group stays in pending_indexes() and the table works
|
||||
* without the index.
|
||||
*
|
||||
* @param string $entity_type Entity type.
|
||||
* @param string $group_name Group name.
|
||||
* @return void
|
||||
@@ -193,23 +207,112 @@ final class TMDO_Schema_Manager {
|
||||
if ( ( $applied[ $id ] ?? '' ) === $signature ) {
|
||||
return;
|
||||
}
|
||||
if ( ! self::ddl_allowed() ) {
|
||||
if ( ! wp_next_scheduled( self::INDEXES_HOOK ) ) {
|
||||
wp_schedule_single_event( time(), self::INDEXES_HOOK );
|
||||
}
|
||||
return;
|
||||
}
|
||||
$table = self::get_table_name( $entity_type, $group_name );
|
||||
if ( ! self::table_exists( $table ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$existing = array_unique( array_column( (array) $wpdb->get_results( "SHOW INDEX FROM `{$table}`", ARRAY_A ), 'Key_name' ) );
|
||||
foreach ( $declared as $name => $keys ) {
|
||||
$index = 'cidx_' . self::sanitize_column_name( $name );
|
||||
if ( in_array( $index, $existing, true ) ) {
|
||||
continue;
|
||||
}
|
||||
$columns = implode( ', ', array_map( static fn( $key ) => '`' . self::sanitize_column_name( $key ) . '`', $keys ) );
|
||||
$wpdb->query( "ALTER TABLE `{$table}` ADD INDEX `{$index}` ({$columns})" );
|
||||
// add_option() is INSERT ... ON DUPLICATE KEY UPDATE and would let two
|
||||
// processes both "acquire" an option lock; GET_LOCK is atomic and is
|
||||
// released by MySQL if this process dies.
|
||||
$lock = self::index_lock_name();
|
||||
if ( '1' !== (string) $wpdb->get_var( $wpdb->prepare( 'SELECT GET_LOCK(MD5(CONCAT(DATABASE(), %s)), 0)', $lock ) ) ) {
|
||||
return; // Another process is adding indexes.
|
||||
}
|
||||
try {
|
||||
// SHOW INDEX (not the option, cached for this request) tells what another process already added.
|
||||
$existing = array_unique( array_column( (array) $wpdb->get_results( "SHOW INDEX FROM `{$table}`", ARRAY_A ), 'Key_name' ) );
|
||||
$failed = false;
|
||||
foreach ( $declared as $name => $keys ) {
|
||||
$index = 'cidx_' . self::sanitize_column_name( $name );
|
||||
if ( in_array( $index, $existing, true ) ) {
|
||||
continue;
|
||||
}
|
||||
$columns = implode( ', ', array_map( static fn( $key ) => '`' . self::sanitize_column_name( $key ) . '`', $keys ) );
|
||||
if ( false === $wpdb->query( "ALTER TABLE `{$table}` ADD INDEX `{$index}` ({$columns})" ) ) {
|
||||
$failed = true;
|
||||
TMDO_Logger::warning(
|
||||
'schema.index_failed',
|
||||
array(
|
||||
'table' => $table,
|
||||
'index' => $index,
|
||||
'error' => $wpdb->last_error,
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$applied[ $id ] = $signature;
|
||||
update_option( self::INDEXES_OPTION, $applied, true );
|
||||
if ( ! $failed ) {
|
||||
$applied[ $id ] = $signature;
|
||||
update_option( self::INDEXES_OPTION, $applied, true );
|
||||
}
|
||||
} finally {
|
||||
$wpdb->query( $wpdb->prepare( 'SELECT RELEASE_LOCK(MD5(CONCAT(DATABASE(), %s)))', $lock ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* INDEXES_HOOK handler: add the indexes a visitor request could not.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function ensure_pending_indexes(): void {
|
||||
foreach ( self::pending_indexes() as $id ) {
|
||||
list( $entity_type, $group_name ) = explode( '/', $id, 2 );
|
||||
self::ensure_group_indexes( $entity_type, $group_name );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Key of the MySQL lock held while indexes are added. Lock names are
|
||||
* server-wide, so SQL hashes it with DATABASE() to keep sites apart.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function index_lock_name(): string {
|
||||
global $wpdb;
|
||||
return $wpdb->prefix . self::INDEXES_OPTION;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this request may alter flat tables: WP-CLI or WP-Cron only.
|
||||
* Front-end, REST, admin-ajax, admin-post and even /wp-admin/ requests
|
||||
* (init runs before auth_redirect()) can be a visitor's, who would wait
|
||||
* for the ALTER. Filter: `tmdo_schema_ddl_allowed`.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function ddl_allowed(): bool {
|
||||
$allowed = ( defined( 'WP_CLI' ) && WP_CLI )
|
||||
|| ( function_exists( 'wp_doing_cron' ) && wp_doing_cron() );
|
||||
return (bool) apply_filters( 'tmdo_schema_ddl_allowed', $allowed );
|
||||
}
|
||||
|
||||
/**
|
||||
* Registered groups whose declared indexes are not on their table yet.
|
||||
*
|
||||
* @return string[] "entity/group"
|
||||
*/
|
||||
public static function pending_indexes(): array {
|
||||
$applied = get_option( self::INDEXES_OPTION, array() );
|
||||
$applied = is_array( $applied ) ? $applied : array();
|
||||
$pending = array();
|
||||
foreach ( TMDO_Entity_Registry::VALID_ENTITY_TYPES as $entity_type ) {
|
||||
foreach ( TMDO_Entity_Registry::get_groups_for_type( $entity_type ) as $group_name ) {
|
||||
$declared = TMDO_Entity_Registry::get_group_indexes( $entity_type, $group_name );
|
||||
if ( $declared && ( $applied[ "{$entity_type}/{$group_name}" ] ?? '' ) !== md5( (string) wp_json_encode( $declared ) )
|
||||
&& self::table_exists( self::get_table_name( $entity_type, $group_name ) ) ) {
|
||||
$pending[] = "{$entity_type}/{$group_name}";
|
||||
}
|
||||
}
|
||||
}
|
||||
return $pending;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user