Remove underscore from classes LBFactory_*, LoadBalancer_*, LoadMonitor_*
[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 array $loads
40 * @param string|bool $group the selected query group. Default: false
41 * @param string|bool $wiki Default: false
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 LoadMonitorNull 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 LoadMonitorMySQL implements LoadMonitor {
101 /**
102 * @var LoadBalancer
103 */
104 public $parent;
105
106 /**
107 * @param LoadBalancer $parent
108 */
109 function __construct( $parent ) {
110 $this->parent = $parent;
111 }
112
113 /**
114 * @param $loads
115 * @param $group bool
116 * @param $wiki bool
117 */
118 function scaleLoads( &$loads, $group = false, $wiki = false ) {
119 }
120
121 /**
122 * @param $serverIndexes
123 * @param $wiki
124 * @return array
125 */
126 function getLagTimes( $serverIndexes, $wiki ) {
127 if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == 0 ) {
128 // Single server only, just return zero without caching
129 return array( 0 => 0 );
130 }
131
132 wfProfileIn( __METHOD__ );
133 $expiry = 5;
134 $requestRate = 10;
135
136 global $wgMemc;
137 if ( empty( $wgMemc ) ) {
138 $wgMemc = wfGetMainCache();
139 }
140
141 $masterName = $this->parent->getServerName( 0 );
142 $memcKey = wfMemcKey( 'lag_times', $masterName );
143 $times = $wgMemc->get( $memcKey );
144 if ( is_array( $times ) ) {
145 # Randomly recache with probability rising over $expiry
146 $elapsed = time() - $times['timestamp'];
147 $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
148 if ( mt_rand( 0, $chance ) != 0 ) {
149 unset( $times['timestamp'] ); // hide from caller
150 wfProfileOut( __METHOD__ );
151
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 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
170 return $times;
171 }
172
173 $times = array();
174 foreach ( $serverIndexes as $i ) {
175 if ( $i == 0 ) { # Master
176 $times[$i] = 0;
177 } elseif ( false !== ( $conn = $this->parent->getAnyOpenConnection( $i ) ) ) {
178 $times[$i] = $conn->getLag();
179 } elseif ( false !== ( $conn = $this->parent->openConnection( $i, $wiki ) ) ) {
180 $times[$i] = $conn->getLag();
181 }
182 }
183
184 # Add a timestamp key so we know when it was cached
185 $times['timestamp'] = time();
186 $wgMemc->set( $memcKey, $times, $expiry + 10 );
187 unset( $times['timestamp'] ); // hide from caller
188
189 wfProfileOut( __METHOD__ );
190
191 return $times;
192 }
193
194 /**
195 * @param $conn DatabaseBase
196 * @param $threshold
197 * @return int
198 */
199 function postConnectionBackoff( $conn, $threshold ) {
200 if ( !$threshold ) {
201 return 0;
202 }
203 $status = $conn->getMysqlStatus( "Thread%" );
204 if ( $status['Threads_running'] > $threshold ) {
205 $server = $conn->getProperty( 'mServer' );
206 wfLogDBError( "LB backoff from $server - Threads_running = {$status['Threads_running']}\n" );
207
208 return $status['Threads_connected'];
209 } else {
210 return 0;
211 }
212 }
213 }