fix(zone-warm): warm 表 UNIQUE KEY + set() 改 upsert + 原子 increment(B1/B3)

- CREATE TABLE wpdo_warm 的 meta_key varchar(255)→varchar(191),
  KEY idx_post_meta → UNIQUE KEY ui_post_meta:沒有這個唯一鍵,
  ON DUPLICATE KEY UPDATE 不會觸發,會不斷 INSERT 重複列。
  (既有站台的 ALTER 遷移原本就在 installer:1329,只有 CREATE 落後)
- Zone_Warm::set() 由 SELECT→update/insert 改為 TMDO_DB::upsert()
- 新增 Zone_Warm::increment():CAST(COALESCE(meta_value,0) AS SIGNED)+N
  原子計數,取代 get()+set() 的 read-modify-write race(A v3.3.3)
- ZoneWarmTest stub 與 ZoneWarmIntegrationTest 建表同步適配

unit 379 / integration 398 GREEN

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TbG1keQQ7XBa7qMQY16KCY
This commit is contained in:
2026-07-31 05:31:34 +08:00
parent 68f7f6871c
commit 151a14a7e8
4 changed files with 81 additions and 35 deletions
+2 -2
View File
@@ -1327,12 +1327,12 @@ class TMDO_Installer {
$sqls[] = "CREATE TABLE {$p}wpdo_warm ( $sqls[] = "CREATE TABLE {$p}wpdo_warm (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT, id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
post_id bigint(20) unsigned NOT NULL DEFAULT 0, post_id bigint(20) unsigned NOT NULL DEFAULT 0,
meta_key varchar(255) NOT NULL DEFAULT '', meta_key varchar(191) NOT NULL DEFAULT '',
meta_value longtext, meta_value longtext,
expires_at datetime NULL DEFAULT NULL, expires_at datetime NULL DEFAULT NULL,
created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (id), PRIMARY KEY (id),
KEY idx_post_meta (post_id, meta_key(191)), UNIQUE KEY ui_post_meta (post_id, meta_key),
KEY idx_expires_at (expires_at) KEY idx_expires_at (expires_at)
) {$charset};"; ) {$charset};";
+38 -26
View File
@@ -100,35 +100,12 @@ class TMDO_Zone_Warm {
* @param int|null $ttl TTL in seconds. Null = no expiry. * @param int|null $ttl TTL in seconds. Null = no expiry.
*/ */
public static function set( int $post_id, string $meta_key, string $value, ?int $ttl = null ): void { public static function set( int $post_id, string $meta_key, string $value, ?int $ttl = null ): void {
global $wpdb;
$table = self::table(); $table = self::table();
$now = TMDO_DB::now(); $now = TMDO_DB::now();
$expires_at = null; $expires_at = ( $ttl && $ttl > 0 ) ? gmdate( 'Y-m-d H:i:s', time() + $ttl ) : null;
if ( $ttl && $ttl > 0 ) {
$expires_at = gmdate( 'Y-m-d H:i:s', time() + $ttl );
}
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name from TMDO_Zone_Warm::table() via TMDO_DB::table(). TMDO_DB::upsert(
$existing = $wpdb->get_var(
$wpdb->prepare(
"SELECT id FROM `{$table}` WHERE post_id = %d AND meta_key = %s LIMIT 1",
$post_id,
$meta_key
)
);
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
if ( $existing ) {
$update_data = array( 'meta_value' => $value );
$update_format = array( '%s' );
if ( null !== $expires_at ) {
$update_data['expires_at'] = $expires_at;
$update_format[] = '%s';
}
$wpdb->update( $table, $update_data, array( 'id' => (int) $existing ), $update_format, array( '%d' ) );
} else {
$wpdb->insert(
$table, $table,
array( array(
'post_id' => $post_id, 'post_id' => $post_id,
@@ -137,9 +114,44 @@ class TMDO_Zone_Warm {
'expires_at' => $expires_at, 'expires_at' => $expires_at,
'created_at' => $now, 'created_at' => $now,
), ),
array( '%d', '%s', '%s', $expires_at ? '%s' : null, '%s' ) array( 'meta_value', 'expires_at' ),
array( 'post_id', 'meta_key' ),
array( '%d', '%s', '%s', '%s', '%s' )
); );
} }
/**
* Atomically increment an integer counter in the warm zone.
*
* Uses INSERT ... ON DUPLICATE KEY UPDATE to avoid the read-then-write race
* condition present in get()+set() patterns.
*
* @param int $post_id Post ID.
* @param string $meta_key Counter key.
* @param int $by Amount to increment (default 1).
* @param int|null $ttl TTL in seconds. Null = no expiry.
*/
public static function increment( int $post_id, string $meta_key, int $by = 1, ?int $ttl = null ): void {
global $wpdb;
$table = self::table();
$now = TMDO_DB::now();
$expires_at = ( $ttl && $ttl > 0 ) ? gmdate( 'Y-m-d H:i:s', time() + $ttl ) : null;
// phpcs:disable WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table from self::table()
$wpdb->query(
$wpdb->prepare(
"INSERT INTO `{$table}` (post_id, meta_key, meta_value, expires_at, created_at)
VALUES (%d, %s, %d, %s, %s)
ON DUPLICATE KEY UPDATE meta_value = CAST(COALESCE(meta_value, 0) AS SIGNED) + %d",
$post_id,
$meta_key,
$by,
$expires_at,
$now,
$by
)
);
// phpcs:enable WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
} }
/** /**
@@ -28,7 +28,7 @@ class ZoneWarmIntegrationTest extends TestCase {
`expires_at` datetime DEFAULT NULL, `expires_at` datetime DEFAULT NULL,
`created_at` datetime NOT NULL DEFAULT \'0000-00-00 00:00:00\', `created_at` datetime NOT NULL DEFAULT \'0000-00-00 00:00:00\',
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
KEY `post_id` (`post_id`), UNIQUE KEY `ui_post_meta` (`post_id`, `meta_key`),
KEY `expires_at` (`expires_at`) KEY `expires_at` (`expires_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4' ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4'
); );
+34
View File
@@ -102,6 +102,40 @@ class ZoneWarmTest extends TestCase {
} }
public function query( string $sql ): int|bool { public function query( string $sql ): int|bool {
$store = &ZoneWarmTest::$store;
$flat = preg_replace( '/\s+/', ' ', $sql );
// INSERT ... ON DUPLICATE KEY UPDATE (from set() via TMDO_DB::upsert())
// Pattern: VALUES (post_id, 'meta_key', 'value', expires_or_NULL, 'now') ON DUPLICATE KEY
if ( stripos( $flat, 'ON DUPLICATE KEY' ) !== false && stripos( $flat, 'COALESCE' ) === false ) {
if ( preg_match( "/VALUES \((\d+), '([^']+)', '([^']*)', (NULL|'[^']+'), '[^']+'\)/i", $flat, $m ) ) {
$post_id = (int) $m[1];
$meta_key = $m[2];
$meta_value = $m[3];
$expires_at = 'NULL' === $m[4] ? null : strtotime( trim( $m[4], "'" ) );
$store[ $post_id ][ $meta_key ] = array(
'value' => $meta_value,
'expires_at' => $expires_at,
);
return 1;
}
}
// INSERT ... ON DUPLICATE KEY UPDATE meta_value = CAST(COALESCE...) + N (from increment())
if ( stripos( $flat, 'COALESCE' ) !== false ) {
if ( preg_match( "/VALUES \((\d+), '([^']+)', (\d+),/i", $flat, $m ) ) {
$post_id = (int) $m[1];
$meta_key = $m[2];
$by = (int) $m[3];
$current = (int) ( $store[ $post_id ][ $meta_key ]['value'] ?? 0 );
$store[ $post_id ][ $meta_key ] = array(
'value' => (string) ( $current + $by ),
'expires_at' => null,
);
return 1;
}
}
return 0; return 0;
} }
}; };