* LBFactory is not really much of a factory if it doesn't have new*() functions,...
authorTim Starling <tstarling@users.mediawiki.org>
Tue, 8 Jul 2008 10:41:08 +0000 (10:41 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Tue, 8 Jul 2008 10:41:08 +0000 (10:41 +0000)
* Came up with a neater way to micro-optimise local section determination.
* Renamed LBFactory::destroy() to LBFactory::destroyInstance(), and made it close connections since apparently that's not done by default when the connection resources are destroyed.
* Removed Database::$mOut, hasn't been used for a while and it dirties up var_dump($db)

includes/db/Database.php
includes/db/LBFactory.php
includes/db/LBFactory_Multi.php

index 3be45d3..751fadc 100644 (file)
@@ -29,7 +29,7 @@ class Database {
        protected $mPHPError = false;
 
        protected $mServer, $mUser, $mPassword, $mConn = null, $mDBname;
-       protected $mOut, $mOpened = false;
+       protected $mOpened = false;
 
        protected $mFailFunction;
        protected $mTablePrefix;
@@ -57,7 +57,7 @@ class Database {
         * FALSE means discard output
         */
        function setOutputPage( $out ) {
-               $this->mOut = $out;
+               wfDeprecated();
        }
 
        /**
@@ -261,7 +261,6 @@ class Database {
                if ( !isset( $wgOut ) ) {
                        $wgOut = NULL;
                }
-               $this->mOut =& $wgOut;
 
                $this->mFailFunction = $failFunction;
                $this->mFlags = $flags;
index b354788..256875d 100644 (file)
@@ -24,12 +24,15 @@ abstract class LBFactory {
        }
 
        /**
-        * Destory the instance
-        * Actually used by maintenace/parserTests.inc to force to reopen connection
-        * when $wgDBprefix has changed
+        * Shut down, close connections and destroy the cached instance.
+        * 
         */
-       static function destroy(){
-               self::$instance = null;
+       static function destroyInstance() {
+               if ( self::$instance ) {
+                       self::$instance->shutdown();
+                       self::$instance->forEachLBCallMethod( 'closeAll' );
+                       self::$instance = null;
+               }
        }
 
        /**
@@ -38,7 +41,16 @@ abstract class LBFactory {
        abstract function __construct( $conf );
 
        /**
-        * Get a load balancer object.
+        * Create a new load balancer object. The resulting object will be untracked, 
+        * not chronology-protected, and the caller is responsible for cleaning it up.
+        *
+        * @param string $wiki Wiki ID, or false for the current wiki
+        * @return LoadBalancer
+        */
+       abstract function newMainLB( $wiki = false );
+
+       /**
+        * Get a cached (tracked) load balancer object.
         *
         * @param string $wiki Wiki ID, or false for the current wiki
         * @return LoadBalancer
@@ -46,7 +58,17 @@ abstract class LBFactory {
        abstract function getMainLB( $wiki = false );
 
        /*
-        * Get a load balancer for external storage
+        * Create a new load balancer for external storage. The resulting object will be 
+        * untracked, not chronology-protected, and the caller is responsible for 
+        * cleaning it up.
+        *
+        * @param string $cluster External storage cluster, or false for core
+        * @param string $wiki Wiki ID, or false for the current wiki
+        */
+       abstract function newExternalLB( $cluster, $wiki = false );
+
+       /*
+        * Get a cached (tracked) load balancer for external storage
         *
         * @param string $cluster External storage cluster, or false for core
         * @param string $wiki Wiki ID, or false for the current wiki
@@ -61,13 +83,13 @@ abstract class LBFactory {
        abstract function forEachLB( $callback, $params = array() );
 
        /**
-        * Prepare all load balancers for shutdown
+        * Prepare all tracked load balancers for shutdown
         * STUB
         */
        function shutdown() {}
 
        /**
-        * Call a method of each load balancer
+        * Call a method of each tracked load balancer
         */
        function forEachLBCallMethod( $methodName, $args = array() ) {
                $this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) );
@@ -102,41 +124,51 @@ class LBFactory_Simple extends LBFactory {
                $this->chronProt = new ChronologyProtector;
        }
 
+       function newMainLB( $wiki = false ) {
+               global $wgDBservers, $wgMasterWaitTimeout;
+               if ( $wgDBservers ) {
+                       $servers = $wgDBservers;
+               } else {
+                       global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
+                       $servers = array(array(
+                               'host' => $wgDBserver,
+                               'user' => $wgDBuser,
+                               'password' => $wgDBpassword,
+                               'dbname' => $wgDBname,
+                               'type' => $wgDBtype,
+                               'load' => 1,
+                               'flags' => ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT
+                       ));
+               }
+
+               return new LoadBalancer( array(
+                       'servers' => $servers, 
+                       'masterWaitTimeout' => $wgMasterWaitTimeout 
+               ));
+       }
+
        function getMainLB( $wiki = false ) {
                if ( !isset( $this->mainLB ) ) {
-                       global $wgDBservers, $wgMasterWaitTimeout;
-                       if ( !$wgDBservers ) {
-                               global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
-                               $wgDBservers = array(array(
-                                       'host' => $wgDBserver,
-                                       'user' => $wgDBuser,
-                                       'password' => $wgDBpassword,
-                                       'dbname' => $wgDBname,
-                                       'type' => $wgDBtype,
-                                       'load' => 1,
-                                       'flags' => ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT
-                               ));
-                       }
-
-                       $this->mainLB = new LoadBalancer( array(
-                               'servers' => $wgDBservers, 
-                               'masterWaitTimeout' => $wgMasterWaitTimeout 
-                       ));
+                       $this->mainLB = $this->newMainLB( $wiki );
                        $this->mainLB->parentInfo( array( 'id' => 'main' ) );
                        $this->chronProt->initLB( $this->mainLB );
                }
                return $this->mainLB;
        }
 
-       function &getExternalLB( $cluster, $wiki = false ) {
+       function newExternalLB( $cluster, $wiki = false ) {
                global $wgExternalServers;
+               if ( !isset( $wgExternalServers[$cluster] ) ) {
+                       throw new MWException( __METHOD__.": Unknown cluster \"$cluster\"" );
+               }
+               return new LoadBalancer( array(
+                       'servers' => $wgExternalServers[$cluster] 
+               ));
+       }
+
+       function &getExternalLB( $cluster, $wiki = false ) {
                if ( !isset( $this->extLBs[$cluster] ) ) {
-                       if ( !isset( $wgExternalServers[$cluster] ) ) {
-                               throw new MWException( __METHOD__.": Unknown cluster \"$cluster\"" );
-                       }
-                       $this->extLBs[$cluster] = new LoadBalancer( array(
-                               'servers' => $wgExternalServers[$cluster] 
-                       ));
+                       $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
                        $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
                }
                return $this->extLBs[$cluster];
index 68eee56..48c2d99 100644 (file)
@@ -47,7 +47,7 @@ class LBFactory_Multi extends LBFactory {
        var $templateOverridesByCluster, $masterTemplateOverrides;
        // Other stuff
        var $conf, $mainLBs = array(), $extLBs = array();
-       var $localSection = null;
+       var $lastWiki, $lastSection;
 
        function __construct( $conf ) {
                $this->chronProt = new ChronologyProtector;
@@ -71,56 +71,63 @@ class LBFactory_Multi extends LBFactory {
                }
        }
 
-       function getSectionForWiki( $wiki ) {
+       function getSectionForWiki( $wiki = false ) {
+               if ( $this->lastWiki === $wiki ) {
+                       return $this->lastSection;
+               }
                list( $dbName, $prefix ) = $this->getDBNameAndPrefix( $wiki );
                if ( isset( $this->sectionsByDB[$dbName] ) ) {
-                       return $this->sectionsByDB[$dbName];
+                       $section = $this->sectionsByDB[$dbName];
                } else {
-                       return 'DEFAULT';
+                       $section = 'DEFAULT';
                }
+               $this->lastSection = $section;
+               $this->lastWiki = $wiki;
+               return $section;
        }
 
-       function getMainLB( $wiki = false ) {
-               // Determine section
-               if ( $wiki === false ) {
-                       if ( $this->localSection === null ) {
-                               $this->localSection = $this->getSectionForWiki( $wiki );
-                       }
-                       $section = $this->localSection;
-               } else {
-                       $section = $this->getSectionForWiki( $wiki );
+       function newMainLB( $wiki = false ) {
+               list( $dbName, $prefix ) = $this->getDBNameAndPrefix( $wiki );
+               $section = $this->getSectionForWiki( $wiki );
+               $groupLoads = array();
+               if ( isset( $this->groupLoadsByDB[$dbName] ) ) {
+                       $groupLoads = $this->groupLoadsByDB[$dbName];
+               }
+               if ( isset( $this->groupLoadsBySection[$section] ) ) {
+                       $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] );
                }
+               return $this->newLoadBalancer( $this->serverTemplate, $this->sectionLoads[$section], $groupLoads );
+       }
 
+       function getMainLB( $wiki = false ) {
+               $section = $this->getSectionForWiki( $wiki );
                if ( !isset( $this->mainLBs[$section] ) ) {
-                       list( $dbName, $prefix ) = $this->getDBNameAndPrefix( $wiki );
-                       $groupLoads = array();
-                       if ( isset( $this->groupLoadsByDB[$dbName] ) ) {
-                               $groupLoads = $this->groupLoadsByDB[$dbName];
-                       }
-                       if ( isset( $this->groupLoadsBySection[$section] ) ) {
-                               $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] );
-                       }
-                       $this->mainLBs[$section] = $this->newLoadBalancer( $this->serverTemplate,
-                               $this->sectionLoads[$section], $groupLoads, "main-$section" );
-                       $this->chronProt->initLB( $this->mainLBs[$section] );
+                       $lb = $this->newMainLB( $wiki, $section );
+                       $this->chronProt->initLB( $lb );
+                       $lb->parentInfo( array( 'id' => "main-$section" ) );
+                       $this->mainLBs[$section] = $lb;
                }
                return $this->mainLBs[$section];
        }
 
+       function newExternalLB( $cluster, $wiki = false ) {
+               if ( !isset( $this->externalLoads[$cluster] ) ) {
+                       throw new MWException( __METHOD__.": Unknown cluster \"$cluster\"" );
+               }
+               $template = $this->serverTemplate;
+               if ( isset( $this->externalTemplateOverrides ) ) {
+                       $template = $this->externalTemplateOverrides + $template;
+               }
+               if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
+                       $template = $this->templateOverridesByCluster[$cluster] + $template;
+               }
+               return $this->newLoadBalancer( $template, $this->externalLoads[$cluster], array() );
+       }
+
        function &getExternalLB( $cluster, $wiki = false ) {
                if ( !isset( $this->extLBs[$cluster] ) ) {
-                       if ( !isset( $this->externalLoads[$cluster] ) ) {
-                               throw new MWException( __METHOD__.": Unknown cluster \"$cluster\"" );
-                       }
-                       $template = $this->serverTemplate;
-                       if ( isset( $this->externalTemplateOverrides ) ) {
-                               $template = $this->externalTemplateOverrides + $template;
-                       }
-                       if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
-                               $template = $this->templateOverridesByCluster[$cluster] + $template;
-                       }
-                       $this->extLBs[$cluster] = $this->newLoadBalancer( $template,
-                               $this->externalLoads[$cluster], array(), "ext-$cluster" );
+                       $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
+                       $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
                }
                return $this->extLBs[$cluster];
        }
@@ -128,14 +135,13 @@ class LBFactory_Multi extends LBFactory {
        /**
         * Make a new load balancer object based on template and load array
         */
-       function newLoadBalancer( $template, $loads, $groupLoads, $id ) {
+       function newLoadBalancer( $template, $loads, $groupLoads ) {
                global $wgMasterWaitTimeout;
                $servers = $this->makeServerArray( $template, $loads, $groupLoads );
                $lb = new LoadBalancer( array(
                        'servers' => $servers,
                        'masterWaitTimeout' => $wgMasterWaitTimeout 
                ));
-               $lb->parentInfo( array( 'id' => $id ) );
                return $lb;
        }