Merge "Made getMaxLag() use caching to reduce connection spam"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 11 Jul 2014 18:02:10 +0000 (18:02 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 11 Jul 2014 18:02:10 +0000 (18:02 +0000)
1  2 
includes/db/LoadBalancer.php

@@@ -1048,11 -1048,19 +1048,22 @@@ 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 ) {
 +                              if ( $i == $this->getWriterIndex() ) {
 +                                      continue; // nothing to check
 +                              }
                                $conn = false;
                                if ( $wiki === false ) {
                                        $conn = $this->getAnyOpenConnection( $i );