Merge "jquery.makeCollapsible: cleanup"
[mediawiki.git] / includes / db / LoadMonitor.php
blob519e2dfd0e8167ad6a4261329566034aa60cb1e1
1 <?php
2 /**
3 * Database load monitoring.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Database
24 /**
25 * An interface for database load monitoring
27 * @ingroup Database
29 interface LoadMonitor {
30 /**
31 * Construct a new LoadMonitor with a given LoadBalancer parent
33 * @param LoadBalancer $parent
35 function __construct( $parent );
37 /**
38 * Perform pre-connection load ratio adjustment.
39 * @param $loads array
40 * @param string $group the selected query group
41 * @param $wiki String
43 function scaleLoads( &$loads, $group = false, $wiki = false );
45 /**
46 * Perform post-connection backoff.
48 * If the connection is in overload, this should return a backoff factor
49 * which will be used to control polling time. The number of threads
50 * connected is a good measure.
52 * If there is no overload, zero can be returned.
54 * A threshold thread count is given, the concrete class may compare this
55 * to the running thread count. The threshold may be false, which indicates
56 * that the sysadmin has not configured this feature.
58 * @param $conn DatabaseBase
59 * @param $threshold Float
61 function postConnectionBackoff( $conn, $threshold );
63 /**
64 * Return an estimate of replication lag for each server
66 * @param $serverIndexes
67 * @param $wiki
69 * @return array
71 function getLagTimes( $serverIndexes, $wiki );
74 class LoadMonitor_Null implements LoadMonitor {
75 function __construct( $parent ) {
78 function scaleLoads( &$loads, $group = false, $wiki = false ) {
81 function postConnectionBackoff( $conn, $threshold ) {
84 /**
85 * @param $serverIndexes
86 * @param $wiki
87 * @return array
89 function getLagTimes( $serverIndexes, $wiki ) {
90 return array_fill_keys( $serverIndexes, 0 );
94 /**
95 * Basic MySQL load monitor with no external dependencies
96 * Uses memcached to cache the replication lag for a short time
98 * @ingroup Database
100 class LoadMonitor_MySQL implements LoadMonitor {
103 * @var LoadBalancer
105 var $parent;
108 * @param LoadBalancer $parent
110 function __construct( $parent ) {
111 $this->parent = $parent;
115 * @param $loads
116 * @param $group bool
117 * @param $wiki bool
119 function scaleLoads( &$loads, $group = false, $wiki = false ) {
123 * @param $serverIndexes
124 * @param $wiki
125 * @return array
127 function getLagTimes( $serverIndexes, $wiki ) {
128 if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == 0 ) {
129 // Single server only, just return zero without caching
130 return array( 0 => 0 );
133 wfProfileIn( __METHOD__ );
134 $expiry = 5;
135 $requestRate = 10;
137 global $wgMemc;
138 if ( empty( $wgMemc ) ) {
139 $wgMemc = wfGetMainCache();
142 $masterName = $this->parent->getServerName( 0 );
143 $memcKey = wfMemcKey( 'lag_times', $masterName );
144 $times = $wgMemc->get( $memcKey );
145 if ( is_array( $times ) ) {
146 # Randomly recache with probability rising over $expiry
147 $elapsed = time() - $times['timestamp'];
148 $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
149 if ( mt_rand( 0, $chance ) != 0 ) {
150 unset( $times['timestamp'] ); // hide from caller
151 wfProfileOut( __METHOD__ );
152 return $times;
154 wfIncrStats( 'lag_cache_miss_expired' );
155 } else {
156 wfIncrStats( 'lag_cache_miss_absent' );
159 # Cache key missing or expired
160 if ( $wgMemc->add( "$memcKey:lock", 1, 10 ) ) {
161 # Let this process alone update the cache value
162 $unlocker = new ScopedCallback( function() use ( $wgMemc, $memcKey ) {
163 $wgMemc->delete( $memcKey );
164 } );
165 } elseif ( is_array( $times ) ) {
166 # Could not acquire lock but an old cache exists, so use it
167 unset( $times['timestamp'] ); // hide from caller
168 wfProfileOut( __METHOD__ );
169 return $times;
172 $times = array();
173 foreach ( $serverIndexes as $i ) {
174 if ( $i == 0 ) { # Master
175 $times[$i] = 0;
176 } elseif ( false !== ( $conn = $this->parent->getAnyOpenConnection( $i ) ) ) {
177 $times[$i] = $conn->getLag();
178 } elseif ( false !== ( $conn = $this->parent->openConnection( $i, $wiki ) ) ) {
179 $times[$i] = $conn->getLag();
183 # Add a timestamp key so we know when it was cached
184 $times['timestamp'] = time();
185 $wgMemc->set( $memcKey, $times, $expiry + 10 );
186 unset( $times['timestamp'] ); // hide from caller
188 wfProfileOut( __METHOD__ );
189 return $times;
193 * @param $conn DatabaseBase
194 * @param $threshold
195 * @return int
197 function postConnectionBackoff( $conn, $threshold ) {
198 if ( !$threshold ) {
199 return 0;
201 $status = $conn->getMysqlStatus( "Thread%" );
202 if ( $status['Threads_running'] > $threshold ) {
203 $server = $conn->getProperty( 'mServer' );
204 wfLogDBError( "LB backoff from $server - Threads_running = {$status['Threads_running']}\n" );
205 return $status['Threads_connected'];
206 } else {
207 return 0;