Merge "Watch user page and user talk page by default"
[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 /**
75 * @todo FIXME: Should be LoadMonitorNull per naming conventions.
76 * PHP CodeSniffer Squiz.Classes.ValidClassName.NotCamelCaps
77 */
78 class LoadMonitor_Null implements LoadMonitor {
79 function __construct( $parent ) {
80 }
81
82 function scaleLoads( &$loads, $group = false, $wiki = false ) {
83 }
84
85 function postConnectionBackoff( $conn, $threshold ) {
86 }
87
88 /**
89 * @param $serverIndexes
90 * @param $wiki
91 * @return array
92 */
93 function getLagTimes( $serverIndexes, $wiki ) {
94 return array_fill_keys( $serverIndexes, 0 );
95 }
96 }
97
98 /**
99 * Basic MySQL load monitor with no external dependencies
100 * Uses memcached to cache the replication lag for a short time
101 *
102 * @ingroup Database
103 * @todo FIXME: Should be LoadMonitorMySQL per naming conventions.
104 * PHP CodeSniffer Squiz.Classes.ValidClassName.NotCamelCaps
105 */
106 class LoadMonitor_MySQL implements LoadMonitor {
107 /**
108 * @var LoadBalancer
109 */
110 public $parent;
111
112 /**
113 * @param LoadBalancer $parent
114 */
115 function __construct( $parent ) {
116 $this->parent = $parent;
117 }
118
119 /**
120 * @param $loads
121 * @param $group bool
122 * @param $wiki bool
123 */
124 function scaleLoads( &$loads, $group = false, $wiki = false ) {
125 }
126
127 /**
128 * @param $serverIndexes
129 * @param $wiki
130 * @return array
131 */
132 function getLagTimes( $serverIndexes, $wiki ) {
133 if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == 0 ) {
134 // Single server only, just return zero without caching
135 return array( 0 => 0 );
136 }
137
138 wfProfileIn( __METHOD__ );
139 $expiry = 5;
140 $requestRate = 10;
141
142 global $wgMemc;
143 if ( empty( $wgMemc ) ) {
144 $wgMemc = wfGetMainCache();
145 }
146
147 $masterName = $this->parent->getServerName( 0 );
148 $memcKey = wfMemcKey( 'lag_times', $masterName );
149 $times = $wgMemc->get( $memcKey );
150 if ( is_array( $times ) ) {
151 # Randomly recache with probability rising over $expiry
152 $elapsed = time() - $times['timestamp'];
153 $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
154 if ( mt_rand( 0, $chance ) != 0 ) {
155 unset( $times['timestamp'] ); // hide from caller
156 wfProfileOut( __METHOD__ );
157
158 return $times;
159 }
160 wfIncrStats( 'lag_cache_miss_expired' );
161 } else {
162 wfIncrStats( 'lag_cache_miss_absent' );
163 }
164
165 # Cache key missing or expired
166 if ( $wgMemc->add( "$memcKey:lock", 1, 10 ) ) {
167 # Let this process alone update the cache value
168 $unlocker = new ScopedCallback( function () use ( $wgMemc, $memcKey ) {
169 $wgMemc->delete( $memcKey );
170 } );
171 } elseif ( is_array( $times ) ) {
172 # Could not acquire lock but an old cache exists, so use it
173 unset( $times['timestamp'] ); // hide from caller
174 wfProfileOut( __METHOD__ );
175
176 return $times;
177 }
178
179 $times = array();
180 foreach ( $serverIndexes as $i ) {
181 if ( $i == 0 ) { # Master
182 $times[$i] = 0;
183 } elseif ( false !== ( $conn = $this->parent->getAnyOpenConnection( $i ) ) ) {
184 $times[$i] = $conn->getLag();
185 } elseif ( false !== ( $conn = $this->parent->openConnection( $i, $wiki ) ) ) {
186 $times[$i] = $conn->getLag();
187 }
188 }
189
190 # Add a timestamp key so we know when it was cached
191 $times['timestamp'] = time();
192 $wgMemc->set( $memcKey, $times, $expiry + 10 );
193 unset( $times['timestamp'] ); // hide from caller
194
195 wfProfileOut( __METHOD__ );
196
197 return $times;
198 }
199
200 /**
201 * @param $conn DatabaseBase
202 * @param $threshold
203 * @return int
204 */
205 function postConnectionBackoff( $conn, $threshold ) {
206 if ( !$threshold ) {
207 return 0;
208 }
209 $status = $conn->getMysqlStatus( "Thread%" );
210 if ( $status['Threads_running'] > $threshold ) {
211 $server = $conn->getProperty( 'mServer' );
212 wfLogDBError( "LB backoff from $server - Threads_running = {$status['Threads_running']}\n" );
213
214 return $status['Threads_connected'];
215 } else {
216 return 0;
217 }
218 }
219 }