Add "maxLag" parameter to LBFactory sub-classes and LoadBalancer
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 22 Nov 2017 16:03:33 +0000 (08:03 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 22 Nov 2017 20:44:03 +0000 (20:44 +0000)
Change-Id: I2559e81a4a67036a899fec10582ac1ccb3818cf1

includes/libs/rdbms/lbfactory/LBFactoryMulti.php
includes/libs/rdbms/lbfactory/LBFactorySimple.php
includes/libs/rdbms/loadbalancer/ILoadBalancer.php
includes/libs/rdbms/loadbalancer/LoadBalancer.php

index 0384588..cfa2647 100644 (file)
@@ -107,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()
         *
@@ -160,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.
         */
@@ -171,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] ) ) {
@@ -319,6 +326,7 @@ class LBFactoryMulti extends LBFactory {
                        $this->baseLoadBalancerParams(),
                        [
                                'servers' => $this->makeServerArray( $template, $loads, $groupLoads ),
+                               'maxLag' => $this->maxLag,
                                'loadMonitor' => [ 'class' => $this->loadMonitorClass ],
                                'readOnlyReason' => $readOnlyReason
                        ]
index df0a806..9a6aa3a 100644 (file)
@@ -41,6 +41,11 @@ class LBFactorySimple extends LBFactory {
 
        /** @var string */
        private $loadMonitorClass;
+       /** @var int */
+       private $maxLag;
+
+       /** @var int Default 'maxLag' when unspecified */
+       const MAX_LAG_DEFAULT = 10;
 
        /**
         * @see LBFactory::__construct()
@@ -72,6 +77,7 @@ class LBFactorySimple extends LBFactory {
                $this->loadMonitorClass = isset( $conf['loadMonitorClass'] )
                        ? $conf['loadMonitorClass']
                        : 'LoadMonitor';
+               $this->maxLag = isset( $conf['maxLag'] ) ? $conf['maxLag'] : self::MAX_LAG_DEFAULT;
        }
 
        /**
@@ -128,6 +134,7 @@ class LBFactorySimple extends LBFactory {
                        $this->baseLoadBalancerParams(),
                        [
                                'servers' => $servers,
+                               'maxLag' => $this->maxLag,
                                'loadMonitor' => [ 'class' => $this->loadMonitorClass ],
                        ]
                ) );
index 22a5805..86c4335 100644 (file)
@@ -96,6 +96,7 @@ interface ILoadBalancer {
         *  - loadMonitor : Name of a class used to fetch server lag and load.
         *  - readOnlyReason : Reason the master DB is read-only if so [optional]
         *  - waitTimeout : Maximum time to wait for replicas for consistency [optional]
+        *  - maxLag: Avoid replica DB servers with more lag than this [optional]
         *  - srvCache : BagOStuff object for server cache [optional]
         *  - wanCache : WANObjectCache object [optional]
         *  - chronologyProtector: ChronologyProtector object [optional]
index 6bb8945..b622ddc 100644 (file)
@@ -115,11 +115,13 @@ class LoadBalancer implements ILoadBalancer {
        private $disabled = false;
        /** @var bool */
        private $chronProtInitialized = false;
+       /** @var int */
+       private $maxLag = self::MAX_LAG_DEFAULT;
 
        /** @var int Warn when this many connection are held */
        const CONN_HELD_WARN_THRESHOLD = 10;
 
-       /** @var int Default 'max lag' when unspecified */
+       /** @var int Default 'maxLag' when unspecified */
        const MAX_LAG_DEFAULT = 10;
        /** @var int Seconds to cache master server read-only status */
        const TTL_CACHE_READONLY = 5;
@@ -178,6 +180,10 @@ class LoadBalancer implements ILoadBalancer {
                        $this->readOnlyReason = $params['readOnlyReason'];
                }
 
+               if ( isset( $params['maxLag'] ) ) {
+                       $this->maxLag = $params['maxLag'];
+               }
+
                if ( isset( $params['loadMonitor'] ) ) {
                        $this->loadMonitorConfig = $params['loadMonitor'];
                } else {
@@ -275,7 +281,7 @@ class LoadBalancer implements ILoadBalancer {
                                # How much lag this server nominally is allowed to have
                                $maxServerLag = isset( $this->mServers[$i]['max lag'] )
                                        ? $this->mServers[$i]['max lag']
-                                       : self::MAX_LAG_DEFAULT; // default
+                                       : $this->maxLag; // default
                                # Constrain that futher by $maxLag argument
                                $maxServerLag = min( $maxServerLag, $maxLag );