LoadBalancer object injection cleanups
[lhc/web/wiklou.git] / includes / db / loadbalancer / LBFactorySimple.php
index 353c47a..14baf2e 100644 (file)
@@ -28,9 +28,7 @@ class LBFactorySimple extends LBFactory {
        /** @var LoadBalancer */
        private $mainLB;
        /** @var LoadBalancer[] */
-       private $extLBs = array();
-       /** @var ChronologyProtector */
-       private $chronProt;
+       private $extLBs = [];
 
        /** @var string */
        private $loadMonitorClass;
@@ -38,7 +36,6 @@ class LBFactorySimple extends LBFactory {
        public function __construct( array $conf ) {
                parent::__construct( $conf );
 
-               $this->chronProt = new ChronologyProtector;
                $this->loadMonitorClass = isset( $conf['loadMonitorClass'] )
                        ? $conf['loadMonitorClass']
                        : null;
@@ -75,7 +72,7 @@ class LBFactorySimple extends LBFactory {
                                $flags |= DBO_COMPRESS;
                        }
 
-                       $servers = array( array(
+                       $servers = [ [
                                'host' => $wgDBserver,
                                'user' => $wgDBuser,
                                'password' => $wgDBpassword,
@@ -84,14 +81,10 @@ class LBFactorySimple extends LBFactory {
                                'load' => 1,
                                'flags' => $flags,
                                'master' => true
-                       ) );
+                       ] ];
                }
 
-               return new LoadBalancer( array(
-                       'servers' => $servers,
-                       'loadMonitor' => $this->loadMonitorClass,
-                       'readOnlyReason' => $this->readOnlyReason
-               ) );
+               return $this->newLoadBalancer( $servers );
        }
 
        /**
@@ -101,7 +94,7 @@ class LBFactorySimple extends LBFactory {
        public function getMainLB( $wiki = false ) {
                if ( !isset( $this->mainLB ) ) {
                        $this->mainLB = $this->newMainLB( $wiki );
-                       $this->mainLB->parentInfo( array( 'id' => 'main' ) );
+                       $this->mainLB->parentInfo( [ 'id' => 'main' ] );
                        $this->chronProt->initLB( $this->mainLB );
                }
 
@@ -120,11 +113,7 @@ class LBFactorySimple extends LBFactory {
                        throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
                }
 
-               return new LoadBalancer( array(
-                       'servers' => $wgExternalServers[$cluster],
-                       'loadMonitor' => $this->loadMonitorClass,
-                       'readOnlyReason' => $this->readOnlyReason
-               ) );
+               return $this->newLoadBalancer( $wgExternalServers[$cluster] );
        }
 
        /**
@@ -135,13 +124,24 @@ class LBFactorySimple extends LBFactory {
        public function &getExternalLB( $cluster, $wiki = false ) {
                if ( !isset( $this->extLBs[$cluster] ) ) {
                        $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
-                       $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
+                       $this->extLBs[$cluster]->parentInfo( [ 'id' => "ext-$cluster" ] );
                        $this->chronProt->initLB( $this->extLBs[$cluster] );
                }
 
                return $this->extLBs[$cluster];
        }
 
+       private function newLoadBalancer( array $servers ) {
+               return new LoadBalancer( [
+                       'servers' => $servers,
+                       'loadMonitor' => $this->loadMonitorClass,
+                       'readOnlyReason' => $this->readOnlyReason,
+                       'trxProfiler' => $this->trxProfiler,
+                       'srvCache' => $this->srvCache,
+                       'wanCache' => $this->wanCache
+               ] );
+       }
+
        /**
         * Execute a function for each tracked load balancer
         * The callback is called with the load balancer as the first parameter,
@@ -150,23 +150,19 @@ class LBFactorySimple extends LBFactory {
         * @param callable $callback
         * @param array $params
         */
-       public function forEachLB( $callback, array $params = array() ) {
+       public function forEachLB( $callback, array $params = [] ) {
                if ( isset( $this->mainLB ) ) {
-                       call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
+                       call_user_func_array( $callback, array_merge( [ $this->mainLB ], $params ) );
                }
                foreach ( $this->extLBs as $lb ) {
-                       call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
+                       call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
                }
        }
 
-       public function shutdown() {
-               if ( $this->mainLB ) {
-                       $this->chronProt->shutdownLB( $this->mainLB );
-               }
-               foreach ( $this->extLBs as $extLB ) {
-                       $this->chronProt->shutdownLB( $extLB );
+       public function shutdown( $flags = 0 ) {
+               if ( !( $flags & self::SHUTDOWN_NO_CHRONPROT ) ) {
+                       $this->shutdownChronologyProtector( $this->chronProt );
                }
-               $this->chronProt->shutdown();
-               $this->commitMasterChanges();
+               $this->commitMasterChanges( __METHOD__ ); // sanity
        }
 }