2008-07-07 03:31:00 +00:00
|
|
|
<?php
|
2010-08-08 11:55:47 +00:00
|
|
|
/**
|
2012-04-26 08:47:10 +00:00
|
|
|
* Database load monitoring.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-08 11:55:47 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Database
|
|
|
|
|
*/
|
2008-07-07 03:31:00 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* An interface for database load monitoring
|
2010-08-08 11:55:47 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup Database
|
2008-07-07 03:31:00 +00:00
|
|
|
*/
|
|
|
|
|
interface LoadMonitor {
|
|
|
|
|
/**
|
|
|
|
|
* Construct a new LoadMonitor with a given LoadBalancer parent
|
2011-03-09 17:09:10 +00:00
|
|
|
*
|
|
|
|
|
* @param LoadBalancer $parent
|
2008-07-07 03:31:00 +00:00
|
|
|
*/
|
2014-02-11 03:03:05 +00:00
|
|
|
public function __construct( $parent );
|
2011-06-30 01:27:03 +00:00
|
|
|
|
2008-07-07 03:31:00 +00:00
|
|
|
/**
|
|
|
|
|
* Perform pre-connection load ratio adjustment.
|
2013-10-04 13:43:09 +00:00
|
|
|
* @param array $loads
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param string|bool $group The selected query group. Default: false
|
2013-10-04 13:43:09 +00:00
|
|
|
* @param string|bool $wiki Default: false
|
2008-07-07 03:31:00 +00:00
|
|
|
*/
|
2014-02-11 03:03:05 +00:00
|
|
|
public function scaleLoads( &$loads, $group = false, $wiki = false );
|
2008-07-07 03:31:00 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return an estimate of replication lag for each server
|
2011-05-28 18:58:51 +00:00
|
|
|
*
|
2013-12-27 01:54:51 +00:00
|
|
|
* @param array $serverIndexes
|
|
|
|
|
* @param string $wiki
|
2011-06-30 01:27:03 +00:00
|
|
|
*
|
2014-09-20 20:53:16 +00:00
|
|
|
* @return array Map of (server index => seconds)
|
2008-07-07 03:31:00 +00:00
|
|
|
*/
|
2014-02-11 03:03:05 +00:00
|
|
|
public function getLagTimes( $serverIndexes, $wiki );
|
2008-07-07 03:31:00 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-27 10:17:06 +00:00
|
|
|
class LoadMonitorNull implements LoadMonitor {
|
2014-02-11 03:03:05 +00:00
|
|
|
public function __construct( $parent ) {
|
2011-06-23 03:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
2014-02-11 03:03:05 +00:00
|
|
|
public function scaleLoads( &$loads, $group = false, $wiki = false ) {
|
2011-06-23 03:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
2014-02-11 03:03:05 +00:00
|
|
|
public function getLagTimes( $serverIndexes, $wiki ) {
|
2011-06-23 03:14:11 +00:00
|
|
|
return array_fill_keys( $serverIndexes, 0 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-07 03:31:00 +00:00
|
|
|
/**
|
|
|
|
|
* Basic MySQL load monitor with no external dependencies
|
|
|
|
|
* Uses memcached to cache the replication lag for a short time
|
2010-08-08 11:55:47 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup Database
|
2008-07-07 03:31:00 +00:00
|
|
|
*/
|
2013-11-27 10:17:06 +00:00
|
|
|
class LoadMonitorMySQL implements LoadMonitor {
|
2013-12-27 01:54:51 +00:00
|
|
|
/** @var LoadBalancer */
|
2013-10-04 13:43:09 +00:00
|
|
|
public $parent;
|
2014-08-21 17:33:09 +00:00
|
|
|
/** @var BagOStuff */
|
|
|
|
|
protected $cache;
|
2011-03-09 17:09:10 +00:00
|
|
|
|
2014-02-11 03:03:05 +00:00
|
|
|
public function __construct( $parent ) {
|
2014-08-21 17:33:09 +00:00
|
|
|
global $wgMemc;
|
|
|
|
|
|
2008-07-07 03:31:00 +00:00
|
|
|
$this->parent = $parent;
|
2014-08-21 17:33:09 +00:00
|
|
|
$this->cache = $wgMemc ?: wfGetMainCache();
|
2008-07-07 03:31:00 +00:00
|
|
|
}
|
|
|
|
|
|
2014-02-11 03:03:05 +00:00
|
|
|
public function scaleLoads( &$loads, $group = false, $wiki = false ) {
|
2008-07-07 03:31:00 +00:00
|
|
|
}
|
|
|
|
|
|
2014-02-11 03:03:05 +00:00
|
|
|
public function getLagTimes( $serverIndexes, $wiki ) {
|
2011-08-29 04:42:26 +00:00
|
|
|
if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == 0 ) {
|
|
|
|
|
// Single server only, just return zero without caching
|
|
|
|
|
return array( 0 => 0 );
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-07 03:31:00 +00:00
|
|
|
$expiry = 5;
|
|
|
|
|
$requestRate = 10;
|
|
|
|
|
|
2014-08-21 17:33:09 +00:00
|
|
|
$cache = $this->cache;
|
2008-07-07 03:31:00 +00:00
|
|
|
$masterName = $this->parent->getServerName( 0 );
|
|
|
|
|
$memcKey = wfMemcKey( 'lag_times', $masterName );
|
2014-08-21 17:33:09 +00:00
|
|
|
$times = $cache->get( $memcKey );
|
2013-05-22 23:19:16 +00:00
|
|
|
if ( is_array( $times ) ) {
|
2008-07-07 03:31:00 +00:00
|
|
|
# Randomly recache with probability rising over $expiry
|
|
|
|
|
$elapsed = time() - $times['timestamp'];
|
|
|
|
|
$chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
|
|
|
|
|
if ( mt_rand( 0, $chance ) != 0 ) {
|
2013-05-22 23:19:16 +00:00
|
|
|
unset( $times['timestamp'] ); // hide from caller
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2008-07-07 03:31:00 +00:00
|
|
|
return $times;
|
|
|
|
|
}
|
2015-06-03 22:38:02 +00:00
|
|
|
wfIncrStats( 'lag_cache.miss.expired' );
|
2008-07-07 03:31:00 +00:00
|
|
|
} else {
|
2015-06-03 22:38:02 +00:00
|
|
|
wfIncrStats( 'lag_cache.miss.absent' );
|
2008-07-07 03:31:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Cache key missing or expired
|
2015-04-27 20:14:21 +00:00
|
|
|
if ( $cache->lock( $memcKey, 0, 10 ) ) {
|
2013-05-22 23:19:16 +00:00
|
|
|
# Let this process alone update the cache value
|
2014-08-21 17:33:09 +00:00
|
|
|
$unlocker = new ScopedCallback( function () use ( $cache, $memcKey ) {
|
2015-04-27 20:14:21 +00:00
|
|
|
$cache->unlock( $memcKey );
|
2013-05-22 23:19:16 +00:00
|
|
|
} );
|
|
|
|
|
} elseif ( is_array( $times ) ) {
|
|
|
|
|
# Could not acquire lock but an old cache exists, so use it
|
|
|
|
|
unset( $times['timestamp'] ); // hide from caller
|
2013-11-20 06:58:22 +00:00
|
|
|
|
2013-05-22 23:19:16 +00:00
|
|
|
return $times;
|
|
|
|
|
}
|
2008-07-07 03:31:00 +00:00
|
|
|
|
|
|
|
|
$times = array();
|
|
|
|
|
foreach ( $serverIndexes as $i ) {
|
2013-02-03 18:47:42 +00:00
|
|
|
if ( $i == 0 ) { # Master
|
2008-07-07 03:31:00 +00:00
|
|
|
$times[$i] = 0;
|
|
|
|
|
} elseif ( false !== ( $conn = $this->parent->getAnyOpenConnection( $i ) ) ) {
|
|
|
|
|
$times[$i] = $conn->getLag();
|
|
|
|
|
} elseif ( false !== ( $conn = $this->parent->openConnection( $i, $wiki ) ) ) {
|
|
|
|
|
$times[$i] = $conn->getLag();
|
2014-08-21 17:33:09 +00:00
|
|
|
// Close the connection to avoid sleeper connections piling up.
|
|
|
|
|
// Note that the caller will pick one of these DBs and reconnect,
|
|
|
|
|
// which is slightly inefficient, but this only matters for the lag
|
|
|
|
|
// time cache miss cache, which is far less common that cache hits.
|
|
|
|
|
$this->parent->closeConnection( $conn );
|
2008-07-07 03:31:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Add a timestamp key so we know when it was cached
|
|
|
|
|
$times['timestamp'] = time();
|
2014-08-21 17:33:09 +00:00
|
|
|
$cache->set( $memcKey, $times, $expiry + 10 );
|
2013-05-22 23:19:16 +00:00
|
|
|
unset( $times['timestamp'] ); // hide from caller
|
2008-07-07 03:31:00 +00:00
|
|
|
|
2013-05-22 23:19:16 +00:00
|
|
|
return $times;
|
2008-07-07 03:31:00 +00:00
|
|
|
}
|
|
|
|
|
}
|