Aaron Schulz has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/242754

Change subject: Made LBFactorySimple set master flag like LBFactoryMulti
......................................................................

Made LBFactorySimple set master flag like LBFactoryMulti

* wfGetDB( DB_MASTER )->getLBInfo() now shows the master flag
* A corresponding slave flag was also added
* Added a few badly needed LBFactory/LoadBalancer tests

Change-Id: I9254b12cff63af7d754a3a14c5db44276f58d280
---
M includes/db/loadbalancer/LBFactoryMulti.php
M includes/db/loadbalancer/LBFactorySimple.php
M tests/phpunit/includes/db/LBFactoryTest.php
3 files changed, 98 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/54/242754/1

diff --git a/includes/db/loadbalancer/LBFactoryMulti.php 
b/includes/db/loadbalancer/LBFactoryMulti.php
index 92fbccd..4a23a67 100644
--- a/includes/db/loadbalancer/LBFactoryMulti.php
+++ b/includes/db/loadbalancer/LBFactoryMulti.php
@@ -319,6 +319,8 @@
                                        $serverInfo = 
$this->masterTemplateOverrides + $serverInfo;
                                }
                                $master = false;
+                       } else {
+                               $serverInfo['slave'] = true;
                        }
                        if ( isset( 
$this->templateOverridesByServer[$serverName] ) ) {
                                $serverInfo = 
$this->templateOverridesByServer[$serverName] + $serverInfo;
diff --git a/includes/db/loadbalancer/LBFactorySimple.php 
b/includes/db/loadbalancer/LBFactorySimple.php
index 23cdbc6..e419361 100644
--- a/includes/db/loadbalancer/LBFactorySimple.php
+++ b/includes/db/loadbalancer/LBFactorySimple.php
@@ -44,8 +44,13 @@
         */
        public function newMainLB( $wiki = false ) {
                global $wgDBservers;
-               if ( $wgDBservers ) {
+
+               if ( is_array( $wgDBservers ) ) {
                        $servers = $wgDBservers;
+                       $servers[0]['master'] = true;
+                       for ( $i = 1; $i < count( $servers ); ++$i ) {
+                               $servers[$i]['slave'] = true;
+                       }
                } else {
                        global $wgDBserver, $wgDBuser, $wgDBpassword, 
$wgDBname, $wgDBtype, $wgDebugDumpSql;
                        global $wgDBssl, $wgDBcompress;
@@ -68,7 +73,8 @@
                                'dbname' => $wgDBname,
                                'type' => $wgDBtype,
                                'load' => 1,
-                               'flags' => $flags
+                               'flags' => $flags,
+                               'master' => true
                        ) );
                }
 
diff --git a/tests/phpunit/includes/db/LBFactoryTest.php 
b/tests/phpunit/includes/db/LBFactoryTest.php
index 81d6840..d9ec2b9 100644
--- a/tests/phpunit/includes/db/LBFactoryTest.php
+++ b/tests/phpunit/includes/db/LBFactoryTest.php
@@ -57,4 +57,92 @@
                        array( 'LBFactoryFake', 'LBFactory_Fake' ),
                );
        }
+
+       public function testLBFactorySimpleServer() {
+               $this->setMwGlobals( 'wgDBservers', false );
+
+               $factory = new LBFactorySimple( array() );
+               $lb = $factory->getMainLB();
+
+               $dbw = $lb->getConnection( DB_MASTER );
+               $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows 
as master' );
+
+               $dbr = $lb->getConnection( DB_SLAVE );
+               $this->assertTrue( $dbr->getLBInfo( 'master' ), 'DB_SLAVE also 
gets the master' );
+
+               $factory->shutdown();
+               $lb->closeAll();
+       }
+
+       public function testLBFactorySimpleServers() {
+               global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, 
$wgDBtype;
+
+               $this->setMwGlobals( 'wgDBservers', array(
+                       array( // master
+                               'host'          => $wgDBserver,
+                               'dbname'    => $wgDBname,
+                               'user'          => $wgDBuser,
+                               'password'      => $wgDBpassword,
+                               'type'          => $wgDBtype,
+                               'load'      => 0,
+                               'flags'     => DBO_TRX // REPEATABLE-READ for 
consistency
+                       ),
+                       array( // emulated slave
+                               'host'          => $wgDBserver,
+                               'dbname'    => $wgDBname,
+                               'user'          => $wgDBuser,
+                               'password'      => $wgDBpassword,
+                               'type'          => $wgDBtype,
+                               'load'      => 100,
+                               'flags'     => DBO_TRX // REPEATABLE-READ for 
consistency
+                       )
+               ) );
+
+               $factory = new LBFactorySimple( array() );
+               $lb = $factory->getMainLB();
+
+               $dbw = $lb->getConnection( DB_MASTER );
+               $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows 
as master' );
+
+               $dbr = $lb->getConnection( DB_SLAVE );
+               $this->assertTrue( $dbr->getLBInfo( 'slave' ), 'slave shows as 
slave' );
+
+               $factory->shutdown();
+               $lb->closeAll();
+       }
+
+       public function testLBFactoryMulti() {
+               global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, 
$wgDBtype;
+
+               $factory = new LBFactoryMulti( array(
+                       'sectionsByDB' => array(),
+                       'sectionLoads' => array(
+                               'DEFAULT' => array(
+                                       'test-db1' => 0,
+                                       'test-db2' => 100,
+                               ),
+                       ),
+                       'serverTemplate' => array(
+                               'dbname'          => $wgDBname,
+                               'user'            => $wgDBuser,
+                               'password'        => $wgDBpassword,
+                               'type'            => $wgDBtype,
+                               'flags'           => DBO_DEFAULT
+                       ),
+                       'hostsByName' => array(
+                               'test-db1'  => $wgDBserver,
+                               'test-db2'  => $wgDBserver
+                       ),
+               ) );
+               $lb = $factory->getMainLB();
+
+               $dbw = $lb->getConnection( DB_MASTER );
+               $this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows 
as master' );
+
+               $dbr = $lb->getConnection( DB_SLAVE );
+               $this->assertTrue( $dbr->getLBInfo( 'slave' ), 'slave shows as 
slave' );
+
+               $factory->shutdown();
+               $lb->closeAll();
+       }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/242754
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9254b12cff63af7d754a3a14c5db44276f58d280
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to