Skip to content

Commit 1bf640d

Browse files
authored
feat(native): open independent logical connections from the native wpdb (#441)
WP_Markdown_Native_WPDB::open_connection() returns a new native wpdb over the same canonical store with its own session and advisory-lock ownership: the native counterpart of a second new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST ). db.php supplies the runtime factory. Instances built without a factory fail closed. Lets consumer tests prove cross-connection GET_LOCK contention without a physical mysqli handle (the four remaining DME native failures in REMAINING_102_GAPS.md).
1 parent b60812e commit 1bf640d

3 files changed

Lines changed: 122 additions & 2 deletions

File tree

‎db.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,11 @@ function markdown_database_integration_native_plugin_dir( string $content_dir ):
220220
$markdown_db_native_state_dir = defined( 'MARKDOWN_DB_STATE_DIR' ) ? MARKDOWN_DB_STATE_DIR : $markdown_db_native_content_dir;
221221
$markdown_db_native_prefix = (string) ( $GLOBALS['table_prefix'] ?? 'wp_' );
222222
$markdown_db_native_runtime = WP_Markdown_Native_Runtime_Factory::wordpress_runtime( $markdown_db_native_state_dir, $markdown_db_native_prefix, $markdown_db_native_content_dir );
223-
$GLOBALS['wpdb'] = new WP_Markdown_Native_WPDB( $markdown_db_native_runtime, $markdown_db_native_prefix );
223+
$GLOBALS['wpdb'] = new WP_Markdown_Native_WPDB(
224+
$markdown_db_native_runtime,
225+
$markdown_db_native_prefix,
226+
static fn(): WP_Markdown_Query_Runtime => WP_Markdown_Native_Runtime_Factory::wordpress_runtime( $markdown_db_native_state_dir, $markdown_db_native_prefix, $markdown_db_native_content_dir )
227+
);
224228
define( 'MARKDOWN_DB_DROPIN', true );
225229
return;
226230
}

‎inc/native/class-wp-markdown-native-wpdb.php‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,19 @@ final class WP_Markdown_Native_WPDB extends wpdb {
5454
private WP_Markdown_Query_Runtime $native_runtime;
5555
private string $native_table_prefix;
5656
private string $native_database_name;
57+
/** @var (Closure(): WP_Markdown_Query_Runtime)|null */
58+
private ?Closure $native_connection_factory;
5759

58-
public function __construct( WP_Markdown_Query_Runtime $runtime, string $table_prefix = 'wp_' ) {
60+
/**
61+
* @param (Closure(): WP_Markdown_Query_Runtime)|null $connection_factory Builds a
62+
* fresh runtime over the same canonical store, enabling open_connection().
63+
*/
64+
public function __construct( WP_Markdown_Query_Runtime $runtime, string $table_prefix = 'wp_', ?Closure $connection_factory = null ) {
5965
if ( 1 !== preg_match( '/^[A-Za-z0-9_]+$/D', $table_prefix ) ) {
6066
throw new InvalidArgumentException( 'The table prefix contains unsupported characters.' );
6167
}
6268
$this->native_runtime = $runtime;
69+
$this->native_connection_factory = $connection_factory;
6370
$this->native_table_prefix = $table_prefix;
6471
$this->native_database_name = defined( 'DB_NAME' ) ? (string) DB_NAME : '';
6572
// Native executes the MySQL-compatible dialect in-process; this is not a
@@ -132,6 +139,27 @@ public function supports_transactional_tables( array $tables ): bool {
132139
}
133140
}
134141

142+
/**
143+
* Open an independent logical connection to the same canonical store.
144+
*
145+
* The native counterpart of a second `new wpdb( DB_USER, DB_PASSWORD, DB_NAME,
146+
* DB_HOST )`: the returned wpdb has its own session and its own advisory-lock
147+
* ownership, so GET_LOCK held here contends with every other connection exactly
148+
* like a separate MySQL connection would. No mysqli handle is involved.
149+
*
150+
* @throws LogicException When this instance was built without a connection factory.
151+
*/
152+
public function open_connection(): self {
153+
if ( null === $this->native_connection_factory ) {
154+
throw new LogicException( 'This native wpdb was constructed without a connection factory.' );
155+
}
156+
$connection = new self( ( $this->native_connection_factory )(), $this->native_table_prefix, $this->native_connection_factory );
157+
if ( '' !== $this->prefix && $this->prefix !== $this->native_table_prefix ) {
158+
$connection->prefix = $this->prefix;
159+
}
160+
return $connection;
161+
}
162+
135163
/** Close the logical native connection while leaving its configured root intact. */
136164
public function close() {
137165
if ( ! $this->ready ) {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/** Native wpdb opens independent logical connections that contend for advisory locks like MySQL connections. */
3+
4+
declare( strict_types=1 );
5+
6+
define( 'ABSPATH', __DIR__ . '/' );
7+
define( 'DB_NAME', 'wordpress_tests' );
8+
9+
class wpdb {
10+
public string $prefix = '';
11+
public string $base_prefix = '';
12+
public bool $ready = false;
13+
public int $num_queries = 0;
14+
public int $num_rows = 0;
15+
public int $rows_affected = 0;
16+
public int $insert_id = 0;
17+
public string $last_error = '';
18+
public ?string $last_query = null;
19+
public ?bool $is_mysql = null;
20+
public string $func_call = '';
21+
public array $last_result = array();
22+
protected array $col_info = array();
23+
protected bool $check_current_query = true;
24+
protected bool $result = false;
25+
26+
public function set_prefix( string $prefix ): string {
27+
$this->prefix = $prefix;
28+
$this->base_prefix = $prefix;
29+
return $prefix;
30+
}
31+
32+
public function flush(): void {
33+
$this->last_result = array();
34+
$this->col_info = array();
35+
$this->last_error = '';
36+
}
37+
38+
public function add_placeholder_escape( string $value ): string { return $value; }
39+
public function remove_placeholder_escape( string $value ): string { return $value; }
40+
}
41+
42+
require_once __DIR__ . '/../inc/native/class-wp-markdown-native-wpdb.php';
43+
44+
$root = sys_get_temp_dir() . '/mdi-native-open-connection-' . bin2hex( random_bytes( 6 ) );
45+
if ( ! mkdir( $root . '/_options', 0777, true ) || ! mkdir( $root . '/_tables', 0777, true ) ) {
46+
throw new RuntimeException( 'Failed to create the open-connection probe fixture.' );
47+
}
48+
49+
/** @return int|string|null */
50+
function open_connection_scalar( WP_Markdown_Native_WPDB $db, string $sql ): int|string|null {
51+
$db->query( $sql );
52+
return isset( $db->last_result[0] ) ? current( get_object_vars( $db->last_result[0] ) ) : null;
53+
}
54+
55+
$factory = static fn(): WP_Markdown_Query_Runtime => WP_Markdown_Native_Runtime_Factory::runtime( $root );
56+
$primary = new WP_Markdown_Native_WPDB( $factory(), 'wp_', $factory );
57+
$second = $primary->open_connection();
58+
$third = $second->open_connection();
59+
60+
$owner_acquire = open_connection_scalar( $primary, "SELECT GET_LOCK('venue-lock', 0)" );
61+
$second_contended = open_connection_scalar( $second, "SELECT GET_LOCK('venue-lock', 0)" );
62+
$second_other_lock = open_connection_scalar( $second, "SELECT GET_LOCK('other-lock', 0)" );
63+
$third_contended = open_connection_scalar( $third, "SELECT GET_LOCK('other-lock', 0)" );
64+
$owner_release = open_connection_scalar( $primary, "SELECT RELEASE_LOCK('venue-lock')" );
65+
$second_acquire = open_connection_scalar( $second, "SELECT GET_LOCK('venue-lock', 0)" );
66+
$second->close();
67+
$after_close = open_connection_scalar( $third, "SELECT GET_LOCK('other-lock', 0)" );
68+
69+
$without_factory = new WP_Markdown_Native_WPDB( $factory(), 'wp_' );
70+
try {
71+
$without_factory->open_connection();
72+
$fails_closed = false;
73+
} catch ( LogicException ) {
74+
$fails_closed = true;
75+
}
76+
77+
$assertions = array(
78+
'open_connection returns a distinct native wpdb' => $second instanceof WP_Markdown_Native_WPDB && $second !== $primary && $third !== $second,
79+
'a held lock excludes the opened connection' => '1' === $owner_acquire && '0' === $second_contended,
80+
'the opened connection owns its own locks' => '1' === $second_other_lock && '0' === $third_contended,
81+
'release on the owner transfers the lock' => '1' === $owner_release && '1' === $second_acquire,
82+
'closing an opened connection releases its locks' => '1' === $after_close,
83+
'the opened connection keeps the table prefix' => 'wp_' === $second->prefix,
84+
'without a factory open_connection fails closed' => $fails_closed,
85+
);
86+
$passed = ! in_array( false, $assertions, true );
87+
fwrite( $passed ? STDOUT : STDERR, json_encode( array( 'assertions' => $assertions, 'passed' => $passed ), JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR ) . "\n" );
88+
exit( $passed ? 0 : 1 );

0 commit comments

Comments
 (0)