From 6b41818459b78448135d13b20688d32e4e272d10 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Mon, 5 Sep 2016 11:49:36 -0700 Subject: [PATCH] Fix default handling in LoadBalancer::getRandomNonLagged() * Previously, setting "max lag" greater than MAX_LAG had no effect. Although that only affects really large (11+ seconds) values for a production setup, it could be useful for testing. * Also make it so INF works, which disables auto read-only mode altogether. This is useful for testing bugs with REPEATABLE-READ and replica DB usage. Setting $wgDBServers two have two entries to the same single DB makes LB treat it like a cluster, thus DB_SLAVE uses a second connection (and snapshot). Using the INF "max lag" value avoids read-only mode in this testing setup. Change-Id: I4c17fe8f812307e90bcc24820f57c6a1257e9ad0 --- includes/db/loadbalancer/LoadBalancer.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/includes/db/loadbalancer/LoadBalancer.php b/includes/db/loadbalancer/LoadBalancer.php index 7ca0b33e8d..7d255b1ecd 100644 --- a/includes/db/loadbalancer/LoadBalancer.php +++ b/includes/db/loadbalancer/LoadBalancer.php @@ -78,7 +78,7 @@ class LoadBalancer { /** @var integer Warn when this many connection are held */ const CONN_HELD_WARN_THRESHOLD = 10; /** @var integer Default 'max lag' when unspecified */ - const MAX_LAG = 10; + const MAX_LAG_DEFAULT = 10; /** @var integer Max time to wait for a replica DB to catch up (e.g. ChronologyProtector) */ const POS_WAIT_TIMEOUT = 10; /** @var integer Seconds to cache master server read-only status */ @@ -189,19 +189,21 @@ class LoadBalancer { * @param int $maxLag Restrict the maximum allowed lag to this many seconds * @return bool|int|string */ - private function getRandomNonLagged( array $loads, $wiki = false, $maxLag = self::MAX_LAG ) { + private function getRandomNonLagged( array $loads, $wiki = false, $maxLag = INF ) { $lags = $this->getLagTimes( $wiki ); # Unset excessively lagged servers foreach ( $lags as $i => $lag ) { if ( $i != 0 ) { - $maxServerLag = $maxLag; - if ( isset( $this->mServers[$i]['max lag'] ) ) { - $maxServerLag = min( $maxServerLag, $this->mServers[$i]['max lag'] ); - } + # 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 + # Constrain that futher by $maxLag argument + $maxServerLag = min( $maxServerLag, $maxLag ); $host = $this->getServerName( $i ); - if ( $lag === false ) { + if ( $lag === false && !is_infinite( $maxServerLag ) ) { wfDebugLog( 'replication', "Server $host (#$i) is not replicating?" ); unset( $loads[$i] ); } elseif ( $lag > $maxServerLag ) { -- 2.20.1