Remove debugging code
[mediawiki.git] / includes / db / LoadMonitor.php
blob8e16f1a1a725268eeeb411d4f6916b42ba6af797
1 <?php
3 /**
4 * An interface for database load monitoring
5 */
7 interface LoadMonitor {
8 /**
9 * Construct a new LoadMonitor with a given LoadBalancer parent
11 function __construct( $parent );
13 /**
14 * Perform pre-connection load ratio adjustment.
15 * @param array $loads
16 * @param string $group The selected query group
17 * @param string $wiki
19 function scaleLoads( &$loads, $group = false, $wiki = false );
21 /**
22 * Perform post-connection backoff.
24 * If the connection is in overload, this should return a backoff factor
25 * which will be used to control polling time. The number of threads
26 * connected is a good measure.
28 * If there is no overload, zero can be returned.
30 * A threshold thread count is given, the concrete class may compare this
31 * to the running thread count. The threshold may be false, which indicates
32 * that the sysadmin has not configured this feature.
34 * @param Database $conn
35 * @param float $threshold
37 function postConnectionBackoff( $conn, $threshold );
39 /**
40 * Return an estimate of replication lag for each server
42 function getLagTimes( $serverIndexes, $wiki );
46 /**
47 * Basic MySQL load monitor with no external dependencies
48 * Uses memcached to cache the replication lag for a short time
51 class LoadMonitor_MySQL implements LoadMonitor {
52 var $parent; // LoadBalancer
54 function __construct( $parent ) {
55 $this->parent = $parent;
58 function scaleLoads( &$loads, $group = false, $wiki = false ) {
61 function getLagTimes( $serverIndexes, $wiki ) {
62 wfProfileIn( __METHOD__ );
63 $expiry = 5;
64 $requestRate = 10;
66 global $wgMemc;
67 $masterName = $this->parent->getServerName( 0 );
68 $memcKey = wfMemcKey( 'lag_times', $masterName );
69 $times = $wgMemc->get( $memcKey );
70 if ( $times ) {
71 # Randomly recache with probability rising over $expiry
72 $elapsed = time() - $times['timestamp'];
73 $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
74 if ( mt_rand( 0, $chance ) != 0 ) {
75 unset( $times['timestamp'] );
76 wfProfileOut( __METHOD__ );
77 return $times;
79 wfIncrStats( 'lag_cache_miss_expired' );
80 } else {
81 wfIncrStats( 'lag_cache_miss_absent' );
84 # Cache key missing or expired
86 $times = array();
87 foreach ( $serverIndexes as $i ) {
88 if ($i == 0) { # Master
89 $times[$i] = 0;
90 } elseif ( false !== ( $conn = $this->parent->getAnyOpenConnection( $i ) ) ) {
91 $times[$i] = $conn->getLag();
92 } elseif ( false !== ( $conn = $this->parent->openConnection( $i, $wiki ) ) ) {
93 $times[$i] = $conn->getLag();
97 # Add a timestamp key so we know when it was cached
98 $times['timestamp'] = time();
99 $wgMemc->set( $memcKey, $times, $expiry );
101 # But don't give the timestamp to the caller
102 unset($times['timestamp']);
103 $lagTimes = $times;
105 wfProfileOut( __METHOD__ );
106 return $lagTimes;
109 function postConnectionBackoff( $conn, $threshold ) {
110 if ( !$threshold ) {
111 return 0;
113 $status = $conn->getStatus("Thread%");
114 if ( $status['Threads_running'] > $threshold ) {
115 return $status['Threads_connected'];
116 } else {
117 return 0;