Merge "Vector: Rewrite footer styling with nesting"
[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 DatabaseBase $conn
59 * @param float $threshold
60 */
61 function postConnectionBackoff( $conn, $threshold );
62
63 /**
64 * Return an estimate of replication lag for each server
65 *
66 * @param array $serverIndexes
67 * @param string $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 array $serverIndexes
86 * @param string $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 /** @var LoadBalancer */
102 public $parent;
103
104 /**
105 * @param LoadBalancer $parent
106 */
107 function __construct( $parent ) {
108 $this->parent = $parent;
109 }
110
111 /**
112 * @param array $loads
113 * @param bool $group
114 * @param bool $wiki
115 */
116 function scaleLoads( &$loads, $group = false, $wiki = false ) {
117 }
118
119 /**
120 * @param array $serverIndexes
121 * @param string $wiki
122 * @return array
123 */
124 function getLagTimes( $serverIndexes, $wiki ) {
125 if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == 0 ) {
126 // Single server only, just return zero without caching
127 return array( 0 => 0 );
128 }
129
130 wfProfileIn( __METHOD__ );
131 $expiry = 5;
132 $requestRate = 10;
133
134 global $wgMemc;
135 if ( empty( $wgMemc ) ) {
136 $wgMemc = wfGetMainCache();
137 }
138
139 $masterName = $this->parent->getServerName( 0 );
140 $memcKey = wfMemcKey( 'lag_times', $masterName );
141 $times = $wgMemc->get( $memcKey );
142 if ( is_array( $times ) ) {
143 # Randomly recache with probability rising over $expiry
144 $elapsed = time() - $times['timestamp'];
145 $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
146 if ( mt_rand( 0, $chance ) != 0 ) {
147 unset( $times['timestamp'] ); // hide from caller
148 wfProfileOut( __METHOD__ );
149
150 return $times;
151 }
152 wfIncrStats( 'lag_cache_miss_expired' );
153 } else {
154 wfIncrStats( 'lag_cache_miss_absent' );
155 }
156
157 # Cache key missing or expired
158 if ( $wgMemc->add( "$memcKey:lock", 1, 10 ) ) {
159 # Let this process alone update the cache value
160 $unlocker = new ScopedCallback( function () use ( $wgMemc, $memcKey ) {
161 $wgMemc->delete( $memcKey );
162 } );
163 } elseif ( is_array( $times ) ) {
164 # Could not acquire lock but an old cache exists, so use it
165 unset( $times['timestamp'] ); // hide from caller
166 wfProfileOut( __METHOD__ );
167
168 return $times;
169 }
170
171 $times = array();
172 foreach ( $serverIndexes as $i ) {
173 if ( $i == 0 ) { # Master
174 $times[$i] = 0;
175 } elseif ( false !== ( $conn = $this->parent->getAnyOpenConnection( $i ) ) ) {
176 $times[$i] = $conn->getLag();
177 } elseif ( false !== ( $conn = $this->parent->openConnection( $i, $wiki ) ) ) {
178 $times[$i] = $conn->getLag();
179 }
180 }
181
182 # Add a timestamp key so we know when it was cached
183 $times['timestamp'] = time();
184 $wgMemc->set( $memcKey, $times, $expiry + 10 );
185 unset( $times['timestamp'] ); // hide from caller
186
187 wfProfileOut( __METHOD__ );
188
189 return $times;
190 }
191
192 /**
193 * @param DatabaseBase|DatabaseMySQLBase $conn
194 * @param int $threshold
195 * @return int
196 */
197 function postConnectionBackoff( $conn, $threshold ) {
198 if ( !$threshold ) {
199 return 0;
200 }
201 $status = $conn->getMysqlStatus( "Thread%" );
202 if ( $status['Threads_running'] > $threshold ) {
203 $server = $conn->getProperty( 'mServer' );
204 wfLogDBError( "LB backoff from $server - Threads_running = {$status['Threads_running']}\n" );
205
206 return $status['Threads_connected'];
207 } else {
208 return 0;
209 }
210 }
211 }