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