From 2fd62f5dc9941cc269d6c4faa9a8fbe11975d479 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Thu, 5 Jul 2018 13:50:57 +0100 Subject: [PATCH] rdbms: add resolveDomainID() method to LBFactory/LoadBalancer Also add LBFactory::getLocalDomainID to match the one in LoadBalancer Change-Id: Ia31f0800bd3b692194c08b1eab9cfb2f43679c7a --- includes/libs/rdbms/lbfactory/ILBFactory.php | 16 ++++++++++++++++ includes/libs/rdbms/lbfactory/LBFactory.php | 8 ++++++++ .../libs/rdbms/loadbalancer/ILoadBalancer.php | 16 ++++++++++++++++ .../libs/rdbms/loadbalancer/LoadBalancer.php | 17 +++++++---------- tests/phpunit/includes/db/LBFactoryTest.php | 7 +++++++ tests/phpunit/includes/db/LoadBalancerTest.php | 7 +++++++ 6 files changed, 61 insertions(+), 10 deletions(-) diff --git a/includes/libs/rdbms/lbfactory/ILBFactory.php b/includes/libs/rdbms/lbfactory/ILBFactory.php index 85ab1157c5..b577fcdb2b 100644 --- a/includes/libs/rdbms/lbfactory/ILBFactory.php +++ b/includes/libs/rdbms/lbfactory/ILBFactory.php @@ -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. diff --git a/includes/libs/rdbms/lbfactory/LBFactory.php b/includes/libs/rdbms/lbfactory/LBFactory.php index 856bd32623..e47e75e178 100644 --- a/includes/libs/rdbms/lbfactory/LBFactory.php +++ b/includes/libs/rdbms/lbfactory/LBFactory.php @@ -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, diff --git a/includes/libs/rdbms/loadbalancer/ILoadBalancer.php b/includes/libs/rdbms/loadbalancer/ILoadBalancer.php index 4eaa4e8c04..5dcafd0411 100644 --- a/includes/libs/rdbms/loadbalancer/ILoadBalancer.php +++ b/includes/libs/rdbms/loadbalancer/ILoadBalancer.php @@ -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 * diff --git a/includes/libs/rdbms/loadbalancer/LoadBalancer.php b/includes/libs/rdbms/loadbalancer/LoadBalancer.php index fe0c6228a1..22c0db2e9e 100644 --- a/includes/libs/rdbms/loadbalancer/LoadBalancer.php +++ b/includes/libs/rdbms/loadbalancer/LoadBalancer.php @@ -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 ) ); diff --git a/tests/phpunit/includes/db/LBFactoryTest.php b/tests/phpunit/includes/db/LBFactoryTest.php index 82ca66a5fb..5cd55bae69 100644 --- a/tests/phpunit/includes/db/LBFactoryTest.php +++ b/tests/phpunit/includes/db/LBFactoryTest.php @@ -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(); } diff --git a/tests/phpunit/includes/db/LoadBalancerTest.php b/tests/phpunit/includes/db/LoadBalancerTest.php index d6b43e530b..5a748ccd0e 100644 --- a/tests/phpunit/includes/db/LoadBalancerTest.php +++ b/tests/phpunit/includes/db/LoadBalancerTest.php @@ -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 ); -- 2.20.1