mark ApiEditPageTest as being slow tests
[lhc/web/wiklou.git] / includes / db / LBFactory.php
1 <?php
2 /**
3 * Generator of database load balancing objects.
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 generating database load balancers
26 * @ingroup Database
27 */
28 abstract class LBFactory {
29
30 /**
31 * @var LBFactory
32 */
33 static $instance;
34
35 /**
36 * Disables all access to the load balancer, will cause all database access
37 * to throw a DBAccessError
38 */
39 public static function disableBackend() {
40 global $wgLBFactoryConf;
41 self::$instance = new LBFactory_Fake( $wgLBFactoryConf );
42 }
43
44 /**
45 * Get an LBFactory instance
46 *
47 * @return LBFactory
48 */
49 static function &singleton() {
50 if ( is_null( self::$instance ) ) {
51 global $wgLBFactoryConf;
52 $class = $wgLBFactoryConf['class'];
53 self::$instance = new $class( $wgLBFactoryConf );
54 }
55 return self::$instance;
56 }
57
58 /**
59 * Shut down, close connections and destroy the cached instance.
60 */
61 static function destroyInstance() {
62 if ( self::$instance ) {
63 self::$instance->shutdown();
64 self::$instance->forEachLBCallMethod( 'closeAll' );
65 self::$instance = null;
66 }
67 }
68
69 /**
70 * Set the instance to be the given object
71 *
72 * @param $instance LBFactory
73 */
74 static function setInstance( $instance ) {
75 self::destroyInstance();
76 self::$instance = $instance;
77 }
78
79 /**
80 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
81 * @param $conf
82 */
83 abstract function __construct( $conf );
84
85 /**
86 * Create a new load balancer object. The resulting object will be untracked,
87 * not chronology-protected, and the caller is responsible for cleaning it up.
88 *
89 * @param $wiki String: wiki ID, or false for the current wiki
90 * @return LoadBalancer
91 */
92 abstract function newMainLB( $wiki = false );
93
94 /**
95 * Get a cached (tracked) load balancer object.
96 *
97 * @param $wiki String: wiki ID, or false for the current wiki
98 * @return LoadBalancer
99 */
100 abstract function getMainLB( $wiki = false );
101
102 /**
103 * Create a new load balancer for external storage. The resulting object will be
104 * untracked, not chronology-protected, and the caller is responsible for
105 * cleaning it up.
106 *
107 * @param $cluster String: external storage cluster, or false for core
108 * @param $wiki String: wiki ID, or false for the current wiki
109 *
110 * @return LoadBalancer
111 */
112 abstract function newExternalLB( $cluster, $wiki = false );
113
114 /**
115 * Get a cached (tracked) load balancer for external storage
116 *
117 * @param $cluster String: external storage cluster, or false for core
118 * @param $wiki String: wiki ID, or false for the current wiki
119 *
120 * @return LoadBalancer
121 */
122 abstract function &getExternalLB( $cluster, $wiki = false );
123
124 /**
125 * Execute a function for each tracked load balancer
126 * The callback is called with the load balancer as the first parameter,
127 * and $params passed as the subsequent parameters.
128 * @param $callback string|array
129 * @param array $params
130 */
131 abstract function forEachLB( $callback, $params = array() );
132
133 /**
134 * Prepare all tracked load balancers for shutdown
135 * STUB
136 */
137 function shutdown() {}
138
139 /**
140 * Call a method of each tracked load balancer
141 * @param $methodName string
142 * @param $args array
143 */
144 function forEachLBCallMethod( $methodName, $args = array() ) {
145 $this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) );
146 }
147
148 /**
149 * Private helper for forEachLBCallMethod
150 * @param $loadBalancer
151 * @param $methodName string
152 * @param $args
153 */
154 function callMethod( $loadBalancer, $methodName, $args ) {
155 call_user_func_array( array( $loadBalancer, $methodName ), $args );
156 }
157
158 /**
159 * Commit changes on all master connections
160 */
161 function commitMasterChanges() {
162 $this->forEachLBCallMethod( 'commitMasterChanges' );
163 }
164 }
165
166 /**
167 * A simple single-master LBFactory that gets its configuration from the b/c globals
168 */
169 class LBFactory_Simple extends LBFactory {
170
171 /**
172 * @var LoadBalancer
173 */
174 var $mainLB;
175 var $extLBs = array();
176
177 # Chronology protector
178 var $chronProt;
179
180 function __construct( $conf ) {
181 $this->chronProt = new ChronologyProtector;
182 }
183
184 /**
185 * @param $wiki
186 * @return LoadBalancer
187 */
188 function newMainLB( $wiki = false ) {
189 global $wgDBservers, $wgMasterWaitTimeout;
190 if ( $wgDBservers ) {
191 $servers = $wgDBservers;
192 } else {
193 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
194 global $wgDBssl, $wgDBcompress;
195
196 $flags = ( $wgDebugDumpSql ? DBO_DEBUG : 0 ) | DBO_DEFAULT;
197 if ( $wgDBssl ) {
198 $flags |= DBO_SSL;
199 }
200 if ( $wgDBcompress ) {
201 $flags |= DBO_COMPRESS;
202 }
203
204 $servers = array(array(
205 'host' => $wgDBserver,
206 'user' => $wgDBuser,
207 'password' => $wgDBpassword,
208 'dbname' => $wgDBname,
209 'type' => $wgDBtype,
210 'load' => 1,
211 'flags' => $flags
212 ));
213 }
214
215 return new LoadBalancer( array(
216 'servers' => $servers,
217 'masterWaitTimeout' => $wgMasterWaitTimeout
218 ));
219 }
220
221 /**
222 * @param $wiki
223 * @return LoadBalancer
224 */
225 function getMainLB( $wiki = false ) {
226 if ( !isset( $this->mainLB ) ) {
227 $this->mainLB = $this->newMainLB( $wiki );
228 $this->mainLB->parentInfo( array( 'id' => 'main' ) );
229 $this->chronProt->initLB( $this->mainLB );
230 }
231 return $this->mainLB;
232 }
233
234 /**
235 * @throws MWException
236 * @param $cluster
237 * @param $wiki
238 * @return LoadBalancer
239 */
240 function newExternalLB( $cluster, $wiki = false ) {
241 global $wgExternalServers;
242 if ( !isset( $wgExternalServers[$cluster] ) ) {
243 throw new MWException( __METHOD__.": Unknown cluster \"$cluster\"" );
244 }
245 return new LoadBalancer( array(
246 'servers' => $wgExternalServers[$cluster]
247 ));
248 }
249
250 /**
251 * @param $cluster
252 * @param $wiki
253 * @return array
254 */
255 function &getExternalLB( $cluster, $wiki = false ) {
256 if ( !isset( $this->extLBs[$cluster] ) ) {
257 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
258 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
259 }
260 return $this->extLBs[$cluster];
261 }
262
263 /**
264 * Execute a function for each tracked load balancer
265 * The callback is called with the load balancer as the first parameter,
266 * and $params passed as the subsequent parameters.
267 * @param $callback
268 * @param $params array
269 */
270 function forEachLB( $callback, $params = array() ) {
271 if ( isset( $this->mainLB ) ) {
272 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
273 }
274 foreach ( $this->extLBs as $lb ) {
275 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
276 }
277 }
278
279 function shutdown() {
280 if ( $this->mainLB ) {
281 $this->chronProt->shutdownLB( $this->mainLB );
282 }
283 $this->chronProt->shutdown();
284 $this->commitMasterChanges();
285 }
286 }
287
288 /**
289 * LBFactory class that throws an error on any attempt to use it.
290 * This will typically be done via wfGetDB().
291 * Call LBFactory::disableBackend() to start using this, and
292 * LBFactory::enableBackend() to return to normal behavior
293 */
294 class LBFactory_Fake extends LBFactory {
295 function __construct( $conf ) {}
296
297 function newMainLB( $wiki = false) {
298 throw new DBAccessError;
299 }
300 function getMainLB( $wiki = false ) {
301 throw new DBAccessError;
302 }
303 function newExternalLB( $cluster, $wiki = false ) {
304 throw new DBAccessError;
305 }
306 function &getExternalLB( $cluster, $wiki = false ) {
307 throw new DBAccessError;
308 }
309 function forEachLB( $callback, $params = array() ) {}
310 }
311
312 /**
313 * Exception class for attempted DB access
314 */
315 class DBAccessError extends MWException {
316 function __construct() {
317 parent::__construct( "Mediawiki tried to access the database via wfGetDB(). This is not allowed." );
318 }
319 }
320
321 /**
322 * Class for ensuring a consistent ordering of events as seen by the user, despite replication.
323 * Kind of like Hawking's [[Chronology Protection Agency]].
324 */
325 class ChronologyProtector {
326 var $startupPos;
327 var $shutdownPos = array();
328
329 /**
330 * Initialise a LoadBalancer to give it appropriate chronology protection.
331 *
332 * @param $lb LoadBalancer
333 */
334 function initLB( $lb ) {
335 if ( $this->startupPos === null ) {
336 if ( !empty( $_SESSION[__CLASS__] ) ) {
337 $this->startupPos = $_SESSION[__CLASS__];
338 }
339 }
340 if ( !$this->startupPos ) {
341 return;
342 }
343 $masterName = $lb->getServerName( 0 );
344
345 if ( $lb->getServerCount() > 1 && !empty( $this->startupPos[$masterName] ) ) {
346 $info = $lb->parentInfo();
347 $pos = $this->startupPos[$masterName];
348 wfDebug( __METHOD__.": LB " . $info['id'] . " waiting for master pos $pos\n" );
349 $lb->waitFor( $this->startupPos[$masterName] );
350 }
351 }
352
353 /**
354 * Notify the ChronologyProtector that the LoadBalancer is about to shut
355 * down. Saves replication positions.
356 *
357 * @param $lb LoadBalancer
358 */
359 function shutdownLB( $lb ) {
360 // Don't start a session, don't bother with non-replicated setups
361 if ( strval( session_id() ) == '' || $lb->getServerCount() <= 1 ) {
362 return;
363 }
364 $masterName = $lb->getServerName( 0 );
365 if ( isset( $this->shutdownPos[$masterName] ) ) {
366 // Already done
367 return;
368 }
369 // Only save the position if writes have been done on the connection
370 $db = $lb->getAnyOpenConnection( 0 );
371 $info = $lb->parentInfo();
372 if ( !$db || !$db->doneWrites() ) {
373 wfDebug( __METHOD__.": LB {$info['id']}, no writes done\n" );
374 return;
375 }
376 $pos = $db->getMasterPos();
377 wfDebug( __METHOD__.": LB {$info['id']} has master pos $pos\n" );
378 $this->shutdownPos[$masterName] = $pos;
379 }
380
381 /**
382 * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now.
383 * May commit chronology data to persistent storage.
384 */
385 function shutdown() {
386 if ( session_id() != '' && count( $this->shutdownPos ) ) {
387 wfDebug( __METHOD__.": saving master pos for " .
388 count( $this->shutdownPos ) . " master(s)\n" );
389 $_SESSION[__CLASS__] = $this->shutdownPos;
390 }
391 }
392 }