rdbms: add resolveDomainID() method to LBFactory/LoadBalancer
authorAaron Schulz <aschulz@wikimedia.org>
Thu, 5 Jul 2018 12:50:57 +0000 (13:50 +0100)
committerAaron Schulz <aschulz@wikimedia.org>
Thu, 5 Jul 2018 13:07:04 +0000 (14:07 +0100)
Also add LBFactory::getLocalDomainID to match the one in LoadBalancer

Change-Id: Ia31f0800bd3b692194c08b1eab9cfb2f43679c7a

includes/libs/rdbms/lbfactory/ILBFactory.php
includes/libs/rdbms/lbfactory/LBFactory.php
includes/libs/rdbms/loadbalancer/ILoadBalancer.php
includes/libs/rdbms/loadbalancer/LoadBalancer.php
tests/phpunit/includes/db/LBFactoryTest.php
tests/phpunit/includes/db/LoadBalancerTest.php

index 85ab115..b577fcd 100644 (file)
@@ -67,6 +67,22 @@ interface ILBFactory {
         */
        public function destroy();
 
+       /**
+        * Get the local (and default) database domain ID of connection handles
+        *
+        * @see DatabaseDomain
+        * @return string Database domain ID; this specifies DB name, schema, and table prefix
+        * @since 1.32
+        */
+       public function getLocalDomainID();
+
+       /**
+        * @param DatabaseDomain|string|bool $domain Database domain
+        * @return string Value of $domain if provided or the local domain otherwise
+        * @since 1.32
+        */
+       public function resolveDomainID( $domain );
+
        /**
         * Create a new load balancer object. The resulting object will be untracked,
         * not chronology-protected, and the caller is responsible for cleaning it up.
index 856bd32..e47e75e 100644 (file)
@@ -152,6 +152,14 @@ abstract class LBFactory implements ILBFactory {
                $this->forEachLBCallMethod( 'disable' );
        }
 
+       public function getLocalDomainID() {
+               return $this->localDomain->getId();
+       }
+
+       public function resolveDomainID( $domain ) {
+               return ( $domain !== false ) ? (string)$domain : $this->getLocalDomainID();
+       }
+
        public function shutdown(
                $mode = self::SHUTDOWN_CHRONPROT_SYNC,
                callable $workCallback = null,
index 4eaa4e8..5dcafd0 100644 (file)
@@ -120,6 +120,22 @@ interface ILoadBalancer {
         */
        public function __construct( array $params );
 
+       /**
+        * Get the local (and default) database domain ID of connection handles
+        *
+        * @see DatabaseDomain
+        * @return string Database domain ID; this specifies DB name, schema, and table prefix
+        * @since 1.31
+        */
+       public function getLocalDomainID();
+
+       /**
+        * @param DatabaseDomain|string|bool $domain Database domain
+        * @return string Value of $domain if provided or the local domain otherwise
+        * @since 1.32
+        */
+       public function resolveDomainID( $domain );
+
        /**
         * Get the index of the reader connection, which may be a replica DB
         *
index fe0c622..22c0db2 100644 (file)
@@ -269,17 +269,14 @@ class LoadBalancer implements ILoadBalancer {
                $this->defaultGroup = $params['defaultGroup'] ?? null;
        }
 
-       /**
-        * Get the local (and default) database domain ID of connection handles
-        *
-        * @see DatabaseDomain
-        * @return string Database domain ID; this specifies DB name, schema, and table prefix
-        * @since 1.31
-        */
        public function getLocalDomainID() {
                return $this->localDomain->getId();
        }
 
+       public function resolveDomainID( $domain ) {
+               return ( $domain !== false ) ? (string)$domain : $this->getLocalDomainID();
+       }
+
        /**
         * Get a LoadMonitor instance
         *
@@ -848,19 +845,19 @@ class LoadBalancer implements ILoadBalancer {
        }
 
        public function getConnectionRef( $db, $groups = [], $domain = false, $flags = 0 ) {
-               $domain = ( $domain !== false ) ? $domain : $this->localDomain;
+               $domain = $this->resolveDomainID( $domain );
 
                return new DBConnRef( $this, $this->getConnection( $db, $groups, $domain, $flags ) );
        }
 
        public function getLazyConnectionRef( $db, $groups = [], $domain = false, $flags = 0 ) {
-               $domain = ( $domain !== false ) ? $domain : $this->localDomain;
+               $domain = $this->resolveDomainID( $domain );
 
                return new DBConnRef( $this, [ $db, $groups, $domain, $flags ] );
        }
 
        public function getMaintenanceConnectionRef( $db, $groups = [], $domain = false, $flags = 0 ) {
-               $domain = ( $domain !== false ) ? $domain : $this->localDomain;
+               $domain = $this->resolveDomainID( $domain );
 
                return new MaintainableDBConnRef(
                        $this, $this->getConnection( $db, $groups, $domain, $flags ) );
index 82ca66a..5cd55ba 100644 (file)
@@ -74,6 +74,10 @@ class LBFactoryTest extends MediaWikiTestCase {
                ];
        }
 
+       /**
+        * @covers LBFactory::getLocalDomainID()
+        * @covers LBFactory::resolveDomainID()
+        */
        public function testLBFactorySimpleServer() {
                global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
 
@@ -99,6 +103,9 @@ class LBFactoryTest extends MediaWikiTestCase {
                $dbr = $lb->getConnection( DB_REPLICA );
                $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_REPLICA also gets the master' );
 
+               $this->assertSame( 'my_test_wiki', $factory->resolveDomainID( 'my_test_wiki' ) );
+               $this->assertSame( $factory->getLocalDomainID(), $factory->resolveDomainID( false ) );
+
                $factory->shutdown();
                $lb->closeAll();
        }
index d6b43e5..5a748cc 100644 (file)
@@ -48,6 +48,10 @@ class LoadBalancerTest extends MediaWikiTestCase {
                ];
        }
 
+       /**
+        * @covers LoadBalancer::getLocalDomainID()
+        * @covers LoadBalancer::resolveDomainID()
+        */
        public function testWithoutReplica() {
                global $wgDBname;
 
@@ -64,6 +68,9 @@ class LoadBalancerTest extends MediaWikiTestCase {
                $ld = DatabaseDomain::newFromId( $lb->getLocalDomainID() );
                $this->assertEquals( $wgDBname, $ld->getDatabase(), 'local domain DB set' );
                $this->assertEquals( $this->dbPrefix(), $ld->getTablePrefix(), 'local domain prefix set' );
+               $this->assertSame( 'my_test_wiki', $lb->resolveDomainID( 'my_test_wiki' ) );
+               $this->assertSame( $ld->getId(), $lb->resolveDomainID( false ) );
+               $this->assertSame( $ld->getId(), $lb->resolveDomainID( $ld ) );
 
                $this->assertFalse( $called );
                $dbw = $lb->getConnection( DB_MASTER );