]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - includes/db/LBFactory.php
MediaWiki 1.17.0
[autoinstallsdev/mediawiki.git] / includes / db / LBFactory.php
index 256875d722bf68e53aadcccce04dab77bb11b7f8..f84a70e5733159d9fd1d01399454a893f30c5147 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 /**
+ * Generator of database load balancing objects
+ *
  * @file
  * @ingroup Database
  */
 abstract class LBFactory {
        static $instance;
 
+       /**
+        * Disables all access to the load balancer, will cause all database access
+        * to throw a DBAccessError
+        */
+       public static function disableBackend() {
+               global $wgLBFactoryConf;
+               self::$instance = new LBFactory_Fake( $wgLBFactoryConf );
+       }
+
+       /**
+        * Resets the singleton for use if it's been disabled. Does nothing otherwise
+        */
+       public static function enableBackend() {
+               if( self::$instance instanceof LBFactory_Fake )
+                       self::$instance = null;
+       }
+
        /**
         * Get an LBFactory instance
         */
@@ -25,7 +44,7 @@ abstract class LBFactory {
 
        /**
         * Shut down, close connections and destroy the cached instance.
-        * 
+        *
         */
        static function destroyInstance() {
                if ( self::$instance ) {
@@ -35,16 +54,24 @@ abstract class LBFactory {
                }
        }
 
+       /**
+        * Set the instance to be the given object
+        */
+       static function setInstance( $instance ) {
+               self::destroyInstance();
+               self::$instance = $instance;
+       }
+
        /**
         * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
         */
        abstract function __construct( $conf );
 
        /**
-        * Create a new load balancer object. The resulting object will be untracked, 
+        * 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
+        * @param $wiki String: wiki ID, or false for the current wiki
         * @return LoadBalancer
         */
        abstract function newMainLB( $wiki = false );
@@ -52,26 +79,26 @@ abstract class LBFactory {
        /**
         * Get a cached (tracked) load balancer object.
         *
-        * @param string $wiki Wiki ID, or false for the current wiki
+        * @param $wiki String: wiki ID, or false for the current wiki
         * @return LoadBalancer
         */
        abstract function getMainLB( $wiki = false );
 
        /*
-        * Create a new load balancer for external storage. The resulting object will be 
-        * untracked, not chronology-protected, and the caller is responsible for 
+        * 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
+        * @param $cluster String: external storage cluster, or false for core
+        * @param $wiki String: 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
+        * @param $cluster String: external storage cluster, or false for core
+        * @param $wiki String: wiki ID, or false for the current wiki
         */
        abstract function &getExternalLB( $cluster, $wiki = false );
 
@@ -142,8 +169,8 @@ class LBFactory_Simple extends LBFactory {
                }
 
                return new LoadBalancer( array(
-                       'servers' => $servers, 
-                       'masterWaitTimeout' => $wgMasterWaitTimeout 
+                       'servers' => $servers,
+                       'masterWaitTimeout' => $wgMasterWaitTimeout
                ));
        }
 
@@ -162,7 +189,7 @@ class LBFactory_Simple extends LBFactory {
                        throw new MWException( __METHOD__.": Unknown cluster \"$cluster\"" );
                }
                return new LoadBalancer( array(
-                       'servers' => $wgExternalServers[$cluster] 
+                       'servers' => $wgExternalServers[$cluster]
                ));
        }
 
@@ -197,6 +224,39 @@ class LBFactory_Simple extends LBFactory {
        }
 }
 
+/**
+ * LBFactory class that throws an error on any attempt to use it.
+ * This will typically be done via wfGetDB().
+ * Call LBFactory::disableBackend() to start using this, and
+ * LBFactory::enableBackend() to return to normal behavior
+ */
+class LBFactory_Fake extends LBFactory {
+       function __construct( $conf ) {}
+
+       function newMainLB( $wiki = false) {
+               throw new DBAccessError;
+       }
+       function getMainLB( $wiki = false ) {
+               throw new DBAccessError;
+       }
+       function newExternalLB( $cluster, $wiki = false ) {
+               throw new DBAccessError;
+       }
+       function &getExternalLB( $cluster, $wiki = false ) {
+               throw new DBAccessError;
+       }
+       function forEachLB( $callback, $params = array() ) {}
+}
+
+/**
+ * Exception class for attempted DB access
+ */
+class DBAccessError extends MWException {
+       function __construct() {
+               parent::__construct( "Mediawiki tried to access the database via wfGetDB(). This is not allowed." );
+       }
+}
+
 /**
  * Class for ensuring a consistent ordering of events as seen by the user, despite replication.
  * Kind of like Hawking's [[Chronology Protection Agency]].
@@ -208,7 +268,7 @@ class ChronologyProtector {
        /**
         * Initialise a LoadBalancer to give it appropriate chronology protection.
         *
-        * @param LoadBalancer $lb
+        * @param $lb LoadBalancer
         */
        function initLB( $lb ) {
                if ( $this->startupPos === null ) {
@@ -233,18 +293,28 @@ class ChronologyProtector {
         * Notify the ChronologyProtector that the LoadBalancer is about to shut
         * down. Saves replication positions.
         *
-        * @param LoadBalancer $lb
+        * @param $lb LoadBalancer
         */
        function shutdownLB( $lb ) {
-               if ( session_id() != '' && $lb->getServerCount() > 1 ) {
-                       $masterName = $lb->getServerName( 0 );
-                       if ( !isset( $this->shutdownPos[$masterName] ) ) {
-                               $pos = $lb->getMasterPos();
-                               $info = $lb->parentInfo();
-                               wfDebug( __METHOD__.": LB " . $info['id'] . " has master pos $pos\n" );
-                               $this->shutdownPos[$masterName] = $pos;
-                       }
+               // Don't start a session, don't bother with non-replicated setups
+               if ( strval( session_id() ) == '' || $lb->getServerCount() <= 1 ) {
+                       return;
+               }
+               $masterName = $lb->getServerName( 0 );
+               if ( isset( $this->shutdownPos[$masterName] ) ) {
+                       // Already done
+                       return;
+               }
+               // Only save the position if writes have been done on the connection
+               $db = $lb->getAnyOpenConnection( 0 );
+               $info = $lb->parentInfo();
+               if ( !$db || !$db->doneWrites() ) {
+                       wfDebug( __METHOD__.": LB {$info['id']}, no writes done\n" );
+                       return;
                }
+               $pos = $db->getMasterPos();
+               wfDebug( __METHOD__.": LB {$info['id']} has master pos $pos\n" );
+               $this->shutdownPos[$masterName] = $pos;
        }
 
        /**