16a0343fb35be5a73134c1de92aed707b7f488bb
[lhc/web/wiklou.git] / includes / db / LoadMonitor.php
1 <?php
2 /**
3 * Database load monitoring
4 *
5 * @file
6 * @ingroup Database
7 */
8
9 /**
10 * An interface for database load monitoring
11 *
12 * @ingroup Database
13 */
14 interface LoadMonitor {
15 /**
16 * Construct a new LoadMonitor with a given LoadBalancer parent
17 *
18 * @param LoadBalancer $parent
19 */
20 function __construct( $parent );
21
22 /**
23 * Perform pre-connection load ratio adjustment.
24 * @param $loads array
25 * @param $group String: the selected query group
26 * @param $wiki String
27 */
28 function scaleLoads( &$loads, $group = false, $wiki = false );
29
30 /**
31 * Perform post-connection backoff.
32 *
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.
36 *
37 * If there is no overload, zero can be returned.
38 *
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.
42 *
43 * @param $conn DatabaseBase
44 * @param $threshold Float
45 */
46 function postConnectionBackoff( $conn, $threshold );
47
48 /**
49 * Return an estimate of replication lag for each server
50 *
51 * @param $serverIndexes
52 * @param $wiki
53 *
54 * @return array
55 */
56 function getLagTimes( $serverIndexes, $wiki );
57 }
58
59 class LoadMonitor_Null implements LoadMonitor {
60 function __construct( $parent ) {
61 }
62
63 function scaleLoads( &$loads, $group = false, $wiki = false ) {
64 }
65
66 function postConnectionBackoff( $conn, $threshold ) {
67 }
68
69 /**
70 * @param $serverIndexes
71 * @param $wiki
72 * @return array
73 */
74 function getLagTimes( $serverIndexes, $wiki ) {
75 return array_fill_keys( $serverIndexes, 0 );
76 }
77 }
78
79 /**
80 * Basic MySQL load monitor with no external dependencies
81 * Uses memcached to cache the replication lag for a short time
82 *
83 * @ingroup Database
84 */
85 class LoadMonitor_MySQL implements LoadMonitor {
86
87 /**
88 * @var LoadBalancer
89 */
90 var $parent;
91
92 /**
93 * @param LoadBalancer $parent
94 */
95 function __construct( $parent ) {
96 $this->parent = $parent;
97 }
98
99 /**
100 * @param $loads
101 * @param $group bool
102 * @param $wiki bool
103 */
104 function scaleLoads( &$loads, $group = false, $wiki = false ) {
105 }
106
107 /**
108 * @param $serverIndexes
109 * @param $wiki
110 * @return array
111 */
112 function getLagTimes( $serverIndexes, $wiki ) {
113 if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == 0 ) {
114 // Single server only, just return zero without caching
115 return array( 0 => 0 );
116 }
117
118 wfProfileIn( __METHOD__ );
119 $expiry = 5;
120 $requestRate = 10;
121
122 global $wgMemc;
123 if ( empty( $wgMemc ) )
124 $wgMemc = wfGetMainCache();
125
126 $masterName = $this->parent->getServerName( 0 );
127 $memcKey = wfMemcKey( 'lag_times', $masterName );
128 $times = $wgMemc->get( $memcKey );
129 if ( $times ) {
130 # Randomly recache with probability rising over $expiry
131 $elapsed = time() - $times['timestamp'];
132 $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
133 if ( mt_rand( 0, $chance ) != 0 ) {
134 unset( $times['timestamp'] );
135 wfProfileOut( __METHOD__ );
136 return $times;
137 }
138 wfIncrStats( 'lag_cache_miss_expired' );
139 } else {
140 wfIncrStats( 'lag_cache_miss_absent' );
141 }
142
143 # Cache key missing or expired
144
145 $times = array();
146 foreach ( $serverIndexes as $i ) {
147 if ($i == 0) { # Master
148 $times[$i] = 0;
149 } elseif ( false !== ( $conn = $this->parent->getAnyOpenConnection( $i ) ) ) {
150 $times[$i] = $conn->getLag();
151 } elseif ( false !== ( $conn = $this->parent->openConnection( $i, $wiki ) ) ) {
152 $times[$i] = $conn->getLag();
153 }
154 }
155
156 # Add a timestamp key so we know when it was cached
157 $times['timestamp'] = time();
158 $wgMemc->set( $memcKey, $times, $expiry );
159
160 # But don't give the timestamp to the caller
161 unset($times['timestamp']);
162 $lagTimes = $times;
163
164 wfProfileOut( __METHOD__ );
165 return $lagTimes;
166 }
167
168 /**
169 * @param $conn DatabaseBase
170 * @param $threshold
171 * @return int
172 */
173 function postConnectionBackoff( $conn, $threshold ) {
174 if ( !$threshold ) {
175 return 0;
176 }
177 $status = $conn->getMysqlStatus("Thread%");
178 if ( $status['Threads_running'] > $threshold ) {
179 $server = $conn->getProperty( 'mServer' );
180 wfLogDBError( "LB backoff from $server - Threads_running = {$status['Threads_running']}\n" );
181 return $status['Threads_connected'];
182 } else {
183 return 0;
184 }
185 }
186 }
187