Merge "Use pathinfo() in AutoLoaderStructureTest::testPSR4Completeness"
[lhc/web/wiklou.git] / includes / libs / rdbms / lbfactory / LBFactoryMulti.php
index a7cc16c..30074ec 100644 (file)
  * @ingroup Database
  */
 
+namespace Wikimedia\Rdbms;
+
+use InvalidArgumentException;
+
 /**
  * A multi-database, multi-master factory for Wikimedia and similar installations.
  * Ignores the old configuration globals.
@@ -103,6 +107,12 @@ class LBFactoryMulti extends LBFactory {
        /** @var string */
        private $lastSection;
 
+       /** @var int */
+       private $maxLag = self::MAX_LAG_DEFAULT;
+
+       /** @var int Default 'maxLag' when unspecified */
+       const MAX_LAG_DEFAULT = 10;
+
        /**
         * @see LBFactory::__construct()
         *
@@ -156,6 +166,7 @@ class LBFactoryMulti extends LBFactory {
         *                                 storage cluster.
         *   - masterTemplateOverrides     Server configuration map overrides for all master servers.
         *   - loadMonitorClass            Name of the LoadMonitor class to always use.
+        *   - maxLag                      Avoid replica DBs with more lag than this many seconds.
         *   - readOnlyBySection           A map of section name to read-only message.
         *                                 Missing or false for read/write.
         */
@@ -167,7 +178,7 @@ class LBFactoryMulti extends LBFactory {
                $optional = [ 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
                        'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
                        'templateOverridesByCluster', 'templateOverridesBySection', 'masterTemplateOverrides',
-                       'readOnlyBySection', 'loadMonitorClass' ];
+                       'readOnlyBySection', 'maxLag', 'loadMonitorClass' ];
 
                foreach ( $required as $key ) {
                        if ( !isset( $conf[$key] ) ) {
@@ -247,9 +258,7 @@ class LBFactoryMulti extends LBFactory {
        public function getMainLB( $domain = false ) {
                $section = $this->getSectionForDomain( $domain );
                if ( !isset( $this->mainLBs[$section] ) ) {
-                       $lb = $this->newMainLB( $domain );
-                       $this->getChronologyProtector()->initLB( $lb );
-                       $this->mainLBs[$section] = $lb;
+                       $this->mainLBs[$section] = $this->newMainLB( $domain );
                }
 
                return $this->mainLBs[$section];
@@ -260,7 +269,7 @@ class LBFactoryMulti extends LBFactory {
                        throw new InvalidArgumentException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
                }
                $template = $this->serverTemplate;
-               if ( isset( $this->externalTemplateOverrides ) ) {
+               if ( $this->externalTemplateOverrides ) {
                        $template = $this->externalTemplateOverrides + $template;
                }
                if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
@@ -278,12 +287,31 @@ class LBFactoryMulti extends LBFactory {
        public function getExternalLB( $cluster ) {
                if ( !isset( $this->extLBs[$cluster] ) ) {
                        $this->extLBs[$cluster] = $this->newExternalLB( $cluster );
-                       $this->getChronologyProtector()->initLB( $this->extLBs[$cluster] );
                }
 
                return $this->extLBs[$cluster];
        }
 
+       public function getAllMainLBs() {
+               $lbs = [];
+               foreach ( $this->sectionsByDB as $db => $section ) {
+                       if ( !isset( $lbs[$section] ) ) {
+                               $lbs[$section] = $this->getMainLB( $db );
+                       }
+               }
+
+               return $lbs;
+       }
+
+       public function getAllExternalLBs() {
+               $lbs = [];
+               foreach ( $this->externalLoads as $cluster => $unused ) {
+                       $lbs[$cluster] = $this->getExternalLB( $cluster );
+               }
+
+               return $lbs;
+       }
+
        /**
         * Make a new load balancer object based on template and load array
         *
@@ -298,6 +326,7 @@ class LBFactoryMulti extends LBFactory {
                        $this->baseLoadBalancerParams(),
                        [
                                'servers' => $this->makeServerArray( $template, $loads, $groupLoads ),
+                               'maxLag' => $this->maxLag,
                                'loadMonitor' => [ 'class' => $this->loadMonitorClass ],
                                'readOnlyReason' => $readOnlyReason
                        ]
@@ -328,7 +357,7 @@ class LBFactoryMulti extends LBFactory {
                        $serverInfo = $template;
                        if ( $master ) {
                                $serverInfo['master'] = true;
-                               if ( isset( $this->masterTemplateOverrides ) ) {
+                               if ( $this->masterTemplateOverrides ) {
                                        $serverInfo = $this->masterTemplateOverrides + $serverInfo;
                                }
                                $master = false;
@@ -393,10 +422,10 @@ class LBFactoryMulti extends LBFactory {
         */
        public function forEachLB( $callback, array $params = [] ) {
                foreach ( $this->mainLBs as $lb ) {
-                       call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
+                       $callback( $lb, ...$params );
                }
                foreach ( $this->extLBs as $lb ) {
-                       call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
+                       $callback( $lb, ...$params );
                }
        }
 }