From: Aaron Schulz Date: Thu, 10 Jul 2014 17:46:42 +0000 (-0700) Subject: Made getMaxLag() use caching to reduce connection spam X-Git-Tag: 1.31.0-rc.0~14993^2 X-Git-Url: https://git.cyclocoop.org//%22?a=commitdiff_plain;h=f9ddcb04539398dc7432aacd456e1ba9ed0b64a6;p=lhc%2Fweb%2Fwiklou.git Made getMaxLag() use caching to reduce connection spam * This method is currently hit when bots use the maxlag API parameter * Also added a $fallback param to ObjectCache::newAccelerator so code does not have to keep reinventing that logic. Change-Id: I45bf1085301c5cf69b29b494a146c6a067534581 --- diff --git a/includes/db/LoadBalancer.php b/includes/db/LoadBalancer.php index b3f9210120..7e9c08cd69 100644 --- a/includes/db/LoadBalancer.php +++ b/includes/db/LoadBalancer.php @@ -1048,7 +1048,18 @@ class LoadBalancer { $maxLag = -1; $host = ''; $maxIndex = 0; - if ( $this->getServerCount() > 1 ) { // no replication = no lag + + if ( $this->getServerCount() <= 1 ) { // no replication = no lag + return array( $host, $maxLag, $maxIndex ); + } + + // Try to get the max lag info from the server cache + $key = 'loadbalancer:maxlag:cluster:' . $this->mServers[0]['host']; + $cache = ObjectCache::newAccelerator( array(), 'hash' ); + $maxLagInfo = $cache->get( $key ); // (host, lag, index) + + // Fallback to connecting to each slave and getting the lag + if ( !$maxLagInfo ) { foreach ( $this->mServers as $i => $conn ) { $conn = false; if ( $wiki === false ) { @@ -1067,9 +1078,11 @@ class LoadBalancer { $maxIndex = $i; } } + $maxLagInfo = array( $host, $maxLag, $maxIndex ); + $cache->set( $key, $maxLagInfo, 5 ); } - return array( $host, $maxLag, $maxIndex ); + return $maxLagInfo; } /** diff --git a/includes/objectcache/ObjectCache.php b/includes/objectcache/ObjectCache.php index 0009999aed..633b34a2c9 100644 --- a/includes/objectcache/ObjectCache.php +++ b/includes/objectcache/ObjectCache.php @@ -119,11 +119,15 @@ class ObjectCache { /** * Factory function referenced from DefaultSettings.php for CACHE_ACCEL. * + * This will look for any APC style server-local cache. + * A fallback cache can be specified if none is found. + * * @param array $params + * @param int|string $fallback Fallback cache, e.g. (CACHE_NONE, "hash") (since 1.24) * @throws MWException * @return BagOStuff */ - static function newAccelerator( $params ) { + static function newAccelerator( $params, $fallback = null ) { if ( function_exists( 'apc_fetch' ) ) { $id = 'apc'; } elseif ( function_exists( 'xcache_get' ) && wfIniGetBool( 'xcache.var_size' ) ) { @@ -131,6 +135,9 @@ class ObjectCache { } elseif ( function_exists( 'wincache_ucache_get' ) ) { $id = 'wincache'; } else { + if ( $fallback ) { + return self::newFromId( $fallback ); + } throw new MWException( "CACHE_ACCEL requested but no suitable object " . "cache is present. You may want to install APC." ); }