diff options
Diffstat (limited to 'includes/db/LoadMonitor.php')
-rw-r--r-- | includes/db/LoadMonitor.php | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/includes/db/LoadMonitor.php b/includes/db/LoadMonitor.php index 146ac61e..519e2dfd 100644 --- a/includes/db/LoadMonitor.php +++ b/includes/db/LoadMonitor.php @@ -37,7 +37,7 @@ interface LoadMonitor { /** * Perform pre-connection load ratio adjustment. * @param $loads array - * @param $group String: the selected query group + * @param string $group the selected query group * @param $wiki String */ function scaleLoads( &$loads, $group = false, $wiki = false ); @@ -135,18 +135,19 @@ class LoadMonitor_MySQL implements LoadMonitor { $requestRate = 10; global $wgMemc; - if ( empty( $wgMemc ) ) + if ( empty( $wgMemc ) ) { $wgMemc = wfGetMainCache(); + } $masterName = $this->parent->getServerName( 0 ); $memcKey = wfMemcKey( 'lag_times', $masterName ); $times = $wgMemc->get( $memcKey ); - if ( $times ) { + if ( is_array( $times ) ) { # Randomly recache with probability rising over $expiry $elapsed = time() - $times['timestamp']; $chance = max( 0, ( $expiry - $elapsed ) * $requestRate ); if ( mt_rand( 0, $chance ) != 0 ) { - unset( $times['timestamp'] ); + unset( $times['timestamp'] ); // hide from caller wfProfileOut( __METHOD__ ); return $times; } @@ -156,10 +157,21 @@ class LoadMonitor_MySQL implements LoadMonitor { } # Cache key missing or expired + if ( $wgMemc->add( "$memcKey:lock", 1, 10 ) ) { + # Let this process alone update the cache value + $unlocker = new ScopedCallback( function() use ( $wgMemc, $memcKey ) { + $wgMemc->delete( $memcKey ); + } ); + } elseif ( is_array( $times ) ) { + # Could not acquire lock but an old cache exists, so use it + unset( $times['timestamp'] ); // hide from caller + wfProfileOut( __METHOD__ ); + return $times; + } $times = array(); foreach ( $serverIndexes as $i ) { - if ($i == 0) { # Master + if ( $i == 0 ) { # Master $times[$i] = 0; } elseif ( false !== ( $conn = $this->parent->getAnyOpenConnection( $i ) ) ) { $times[$i] = $conn->getLag(); @@ -170,14 +182,11 @@ class LoadMonitor_MySQL implements LoadMonitor { # Add a timestamp key so we know when it was cached $times['timestamp'] = time(); - $wgMemc->set( $memcKey, $times, $expiry ); - - # But don't give the timestamp to the caller - unset($times['timestamp']); - $lagTimes = $times; + $wgMemc->set( $memcKey, $times, $expiry + 10 ); + unset( $times['timestamp'] ); // hide from caller wfProfileOut( __METHOD__ ); - return $lagTimes; + return $times; } /** @@ -189,7 +198,7 @@ class LoadMonitor_MySQL implements LoadMonitor { if ( !$threshold ) { return 0; } - $status = $conn->getMysqlStatus("Thread%"); + $status = $conn->getMysqlStatus( "Thread%" ); if ( $status['Threads_running'] > $threshold ) { $server = $conn->getProperty( 'mServer' ); wfLogDBError( "LB backoff from $server - Threads_running = {$status['Threads_running']}\n" ); @@ -199,4 +208,3 @@ class LoadMonitor_MySQL implements LoadMonitor { } } } - |