f8bf3af1d3ce9d937fe8a7c686f0e526c57a388e
3 * Database load monitoring
10 * An interface for database load monitoring
14 interface LoadMonitor
{
16 * Construct a new LoadMonitor with a given LoadBalancer parent
18 * @param LoadBalancer $parent
20 function __construct( $parent );
23 * Perform pre-connection load ratio adjustment.
25 * @param $group String: the selected query group
28 function scaleLoads( &$loads, $group = false, $wiki = false );
31 * Perform post-connection backoff.
33 * If the connection is in overload, this should return a backoff factor
34 * which will be used to control polling time. The number of threads
35 * connected is a good measure.
37 * If there is no overload, zero can be returned.
39 * A threshold thread count is given, the concrete class may compare this
40 * to the running thread count. The threshold may be false, which indicates
41 * that the sysadmin has not configured this feature.
43 * @param $conn DatabaseBase
44 * @param $threshold Float
46 function postConnectionBackoff( $conn, $threshold );
49 * Return an estimate of replication lag for each server
51 function getLagTimes( $serverIndexes, $wiki );
56 * Basic MySQL load monitor with no external dependencies
57 * Uses memcached to cache the replication lag for a short time
61 class LoadMonitor_MySQL
implements LoadMonitor
{
69 * @param LoadBalancer $parent
71 function __construct( $parent ) {
72 $this->parent
= $parent;
80 function scaleLoads( &$loads, $group = false, $wiki = false ) {
84 * @param $serverIndexes
88 function getLagTimes( $serverIndexes, $wiki ) {
89 wfProfileIn( __METHOD__
);
94 if ( empty( $wgMemc ) )
95 $wgMemc = wfGetMainCache();
97 $masterName = $this->parent
->getServerName( 0 );
98 $memcKey = wfMemcKey( 'lag_times', $masterName );
99 $times = $wgMemc->get( $memcKey );
101 # Randomly recache with probability rising over $expiry
102 $elapsed = time() - $times['timestamp'];
103 $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
104 if ( mt_rand( 0, $chance ) != 0 ) {
105 unset( $times['timestamp'] );
106 wfProfileOut( __METHOD__
);
109 wfIncrStats( 'lag_cache_miss_expired' );
111 wfIncrStats( 'lag_cache_miss_absent' );
114 # Cache key missing or expired
117 foreach ( $serverIndexes as $i ) {
118 if ($i == 0) { # Master
120 } elseif ( false !== ( $conn = $this->parent
->getAnyOpenConnection( $i ) ) ) {
121 $times[$i] = $conn->getLag();
122 } elseif ( false !== ( $conn = $this->parent
->openConnection( $i, $wiki ) ) ) {
123 $times[$i] = $conn->getLag();
127 # Add a timestamp key so we know when it was cached
128 $times['timestamp'] = time();
129 $wgMemc->set( $memcKey, $times, $expiry );
131 # But don't give the timestamp to the caller
132 unset($times['timestamp']);
135 wfProfileOut( __METHOD__
);
140 * @param $conn DatabaseBase
144 function postConnectionBackoff( $conn, $threshold ) {
148 $status = $conn->getStatus("Thread%");
149 if ( $status['Threads_running'] > $threshold ) {
150 return $status['Threads_connected'];