Skip to content

Commit 7bb0404

Browse files
committed
Collaboration: Replace awareness cache layer with dedicated presence table.
Introduce wp_presence table with UNIQUE KEY (room, client_id) for atomic upserts via INSERT ... ON DUPLICATE KEY UPDATE. This eliminates the read-modify-write race condition in the object cache approach and removes all cache-backend branching from the awareness path. The collaboration table becomes a clean append-only log for sync updates only — the type column, type_client_id index, and room_type_date index are removed. New file: src/wp-includes/presence.php provides wp_get_presence(), wp_set_presence(), wp_remove_presence(), wp_remove_user_presence(), and wp_delete_expired_presence_data().
1 parent 5b64704 commit 7bb0404

10 files changed

Lines changed: 262 additions & 472 deletions

File tree

src/wp-admin/includes/schema.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,16 +190,25 @@ function wp_get_db_schema( $scope = 'all', $blog_id = null ) {
190190
CREATE TABLE $wpdb->collaboration (
191191
id bigint(20) unsigned NOT NULL auto_increment,
192192
room varchar($max_index_length) NOT NULL default '',
193-
type varchar(32) NOT NULL default '',
194193
client_id varchar(32) NOT NULL default '',
195194
user_id bigint(20) unsigned NOT NULL default '0',
196195
data longtext NOT NULL,
197196
date_gmt datetime NOT NULL default '0000-00-00 00:00:00',
198197
PRIMARY KEY (id),
199-
KEY type_client_id (type,client_id),
200198
KEY room (room,id),
201-
KEY room_type_date (room,type,date_gmt),
202199
KEY date_gmt (date_gmt)
200+
) $charset_collate;
201+
CREATE TABLE $wpdb->presence (
202+
id bigint(20) unsigned NOT NULL auto_increment,
203+
room varchar($max_index_length) NOT NULL default '',
204+
client_id varchar($max_index_length) NOT NULL default '',
205+
user_id bigint(20) unsigned NOT NULL default '0',
206+
data text NOT NULL,
207+
date_gmt datetime NOT NULL default '0000-00-00 00:00:00',
208+
PRIMARY KEY (id),
209+
UNIQUE KEY room_client (room,client_id),
210+
KEY date_gmt (date_gmt),
211+
KEY user_id (user_id)
203212
) $charset_collate;\n";
204213

205214
// Single site users table. The multisite flavor of the users table is handled below.

src/wp-admin/includes/upgrade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ function upgrade_all() {
886886
upgrade_682();
887887
}
888888

889-
if ( $wp_current_db_version < 61841 ) {
889+
if ( $wp_current_db_version < 61843 ) {
890890
upgrade_700();
891891
}
892892

src/wp-includes/class-wpdb.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ class wpdb {
300300
'termmeta',
301301
'commentmeta',
302302
'collaboration',
303+
'presence',
303304
);
304305

305306
/**
@@ -414,6 +415,15 @@ class wpdb {
414415
*/
415416
public $collaboration;
416417

418+
/**
419+
* WordPress Presence table.
420+
*
421+
* @since 7.0.0
422+
*
423+
* @var string
424+
*/
425+
public $presence;
426+
417427
/**
418428
* WordPress Terms table.
419429
*

src/wp-includes/collaboration.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function wp_is_collaboration_enabled() {
2222
return (
2323
wp_is_collaboration_allowed() &&
2424
get_option( 'wp_collaboration_enabled' ) &&
25-
get_option( 'db_version' ) >= 61841
25+
get_option( 'db_version' ) >= 61843
2626
);
2727
}
2828

@@ -89,9 +89,8 @@ function wp_collaboration_inject_setting() {
8989
/**
9090
* Deletes stale collaboration data from the collaboration table.
9191
*
92-
* Removes non-awareness rows older than 7 days and awareness rows older
93-
* than 60 seconds. Rows left behind by abandoned collaborative editing
94-
* sessions are cleaned up to prevent unbounded table growth.
92+
* Removes update rows older than 7 days. Presence data is cleaned
93+
* separately via wp_delete_expired_presence_data().
9594
*
9695
* @since 7.0.0
9796
*/
@@ -107,19 +106,14 @@ function wp_delete_old_collaboration_data() {
107106
wp_clear_scheduled_hook( 'wp_delete_old_collaboration_data' );
108107
}
109108

110-
/* Clean up rows older than 7 days. */
109+
/* Clean up update rows older than 7 days. */
111110
$wpdb->query(
112111
$wpdb->prepare(
113112
"DELETE FROM {$wpdb->collaboration} WHERE date_gmt < %s",
114113
gmdate( 'Y-m-d H:i:s', time() - WEEK_IN_SECONDS )
115114
)
116115
);
117116

118-
// Clean up awareness rows older than 60 seconds.
119-
$wpdb->query(
120-
$wpdb->prepare(
121-
"DELETE FROM {$wpdb->collaboration} WHERE type = 'awareness' AND date_gmt < %s",
122-
gmdate( 'Y-m-d H:i:s', time() - 60 )
123-
)
124-
);
117+
/* Clean up expired presence entries. */
118+
wp_delete_expired_presence_data();
125119
}

0 commit comments

Comments
 (0)