rdbms: make DOMAIN_ANY ignore bogus MySQL DB names in config
authorAaron Schulz <aschulz@wikimedia.org>
Fri, 9 Feb 2018 02:48:38 +0000 (18:48 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Fri, 9 Feb 2018 06:43:24 +0000 (06:43 +0000)
This regressed in 14ee3f2, trying to select the server config
"dbname" value as the database on connect. If that config is
bogus, then the connection attempt would fail. Instead, always
pass null to the driver's connection function if the DB domain
doesn't matter.

This affected WMF sites during lag checks, on account of the
"serverTemplate" value in $wgLBFactoryConf always using the local
wiki domain (whether the cluster had such a database or not).

Use strlen() as mysqli sees null and "" as the same for $dbname.

Bug: T186764
Change-Id: I6699d17c0125a08415046211fee7906bbaf2c366

includes/libs/rdbms/database/DatabaseMysqlBase.php
includes/libs/rdbms/loadbalancer/LoadBalancer.php
tests/phpunit/includes/db/LBFactoryTest.php

index 208f506..fcedf56 100644 (file)
@@ -159,7 +159,7 @@ abstract class DatabaseMysqlBase extends Database {
                        $this->reportConnectionError( $error );
                }
 
-               if ( $dbName != '' ) {
+               if ( strlen( $dbName ) ) {
                        MediaWiki\suppressWarnings();
                        $success = $this->selectDB( $dbName );
                        MediaWiki\restoreWarnings();
index f410882..d070c1e 100644 (file)
@@ -1006,7 +1006,17 @@ class LoadBalancer implements ILoadBalancer {
                        throw new DBAccessError();
                }
 
-               if ( $domainOverride->getDatabase() !== null ) {
+               // Handle $domainOverride being a specified or an unspecified domain
+               if ( $domainOverride->getDatabase() === null ) {
+                       // Normally, an RDBMS requires a DB name specified on connection and the $server
+                       // configuration array is assumed to already specify an appropriate DB name.
+                       if ( $server['type'] === 'mysql' ) {
+                               // For MySQL, DATABASE and SCHEMA are synonyms, connections need not specify a DB,
+                               // and the DB name in $server might not exist due to legacy reasons (the default
+                               // domain used to ignore the local LB domain, even when mismatched).
+                               $server['dbname'] = null;
+                       }
+               } else {
                        $server['dbname'] = $domainOverride->getDatabase();
                        $server['schema'] = $domainOverride->getSchema();
                }
index fa1cfc9..356ebe5 100644 (file)
@@ -376,14 +376,14 @@ class LBFactoryTest extends MediaWikiTestCase {
                $db = $lb->getConnection( DB_MASTER, [], '' );
 
                $this->assertEquals(
-                       $wgDBname,
+                       '',
                        $db->getDomainId(),
-                       'Main domain ID handle used; same DB name'
+                       'Null domain ID handle used'
                );
                $this->assertEquals(
-                       $wgDBname,
+                       '',
                        $db->getDBname(),
-                       'Main domain ID handle used; same DB name'
+                       'Null domain ID handle used'
                );
                $this->assertEquals(
                        '',
@@ -446,16 +446,16 @@ class LBFactoryTest extends MediaWikiTestCase {
                $dbname = 'unittest-domain'; // explodes if DB is selected
                $factory = $this->newLBFactoryMulti(
                        [ 'localDomain' => ( new DatabaseDomain( $dbname, null, '' ) )->getId() ],
-                       [ 'dbFilePath' => $dbPath ]
+                       [
+                               'dbFilePath' => $dbPath,
+                               'dbName' => 'do_not_select_me' // explodes if DB is selected
+                       ]
                );
                $lb = $factory->getMainLB();
                /** @var Database $db */
                $db = $lb->getConnection( DB_MASTER, [], '' );
 
-               $this->assertEquals(
-                       $wgDBname,
-                       $db->getDomainID()
-               );
+               $this->assertEquals( '', $db->getDomainID(), "Null domain used" );
 
                $this->assertEquals(
                        $this->quoteTable( $db, 'page' ),