Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / db / LBFactory.php
blobdec6ae16938d52b697ff514598adacbe052911d7
1 <?php
2 /**
3 * Generator of database load balancing objects
5 * @file
6 * @ingroup Database
7 */
9 /**
10 * An interface for generating database load balancers
11 * @ingroup Database
13 abstract class LBFactory {
15 /**
16 * @var LBFactory
18 static $instance;
20 /**
21 * Disables all access to the load balancer, will cause all database access
22 * to throw a DBAccessError
24 public static function disableBackend() {
25 global $wgLBFactoryConf;
26 self::$instance = new LBFactory_Fake( $wgLBFactoryConf );
29 /**
30 * Get an LBFactory instance
32 * @return LBFactory
34 static function &singleton() {
35 if ( is_null( self::$instance ) ) {
36 global $wgLBFactoryConf;
37 $class = $wgLBFactoryConf['class'];
38 self::$instance = new $class( $wgLBFactoryConf );
40 return self::$instance;
43 /**
44 * Shut down, close connections and destroy the cached instance.
46 static function destroyInstance() {
47 if ( self::$instance ) {
48 self::$instance->shutdown();
49 self::$instance->forEachLBCallMethod( 'closeAll' );
50 self::$instance = null;
54 /**
55 * Set the instance to be the given object
57 * @param $instance LBFactory
59 static function setInstance( $instance ) {
60 self::destroyInstance();
61 self::$instance = $instance;
64 /**
65 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
66 * @param $conf
68 abstract function __construct( $conf );
70 /**
71 * Create a new load balancer object. The resulting object will be untracked,
72 * not chronology-protected, and the caller is responsible for cleaning it up.
74 * @param $wiki String: wiki ID, or false for the current wiki
75 * @return LoadBalancer
77 abstract function newMainLB( $wiki = false );
79 /**
80 * Get a cached (tracked) load balancer object.
82 * @param $wiki String: wiki ID, or false for the current wiki
83 * @return LoadBalancer
85 abstract function getMainLB( $wiki = false );
87 /**
88 * Create a new load balancer for external storage. The resulting object will be
89 * untracked, not chronology-protected, and the caller is responsible for
90 * cleaning it up.
92 * @param $cluster String: external storage cluster, or false for core
93 * @param $wiki String: wiki ID, or false for the current wiki
95 * @return LoadBalancer
97 abstract function newExternalLB( $cluster, $wiki = false );
99 /**
100 * Get a cached (tracked) load balancer for external storage
102 * @param $cluster String: external storage cluster, or false for core
103 * @param $wiki String: wiki ID, or false for the current wiki
105 * @return LoadBalancer
107 abstract function &getExternalLB( $cluster, $wiki = false );
110 * Execute a function for each tracked load balancer
111 * The callback is called with the load balancer as the first parameter,
112 * and $params passed as the subsequent parameters.
113 * @param $callback string|array
114 * @param array $params
116 abstract function forEachLB( $callback, $params = array() );
119 * Prepare all tracked load balancers for shutdown
120 * STUB
122 function shutdown() {}
125 * Call a method of each tracked load balancer
126 * @param $methodName string
127 * @param $args array
129 function forEachLBCallMethod( $methodName, $args = array() ) {
130 $this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) );
134 * Private helper for forEachLBCallMethod
135 * @param $loadBalancer
136 * @param $methodName string
137 * @param $args
139 function callMethod( $loadBalancer, $methodName, $args ) {
140 call_user_func_array( array( $loadBalancer, $methodName ), $args );
144 * Commit changes on all master connections
146 function commitMasterChanges() {
147 $this->forEachLBCallMethod( 'commitMasterChanges' );
152 * A simple single-master LBFactory that gets its configuration from the b/c globals
154 class LBFactory_Simple extends LBFactory {
157 * @var LoadBalancer
159 var $mainLB;
160 var $extLBs = array();
162 # Chronology protector
163 var $chronProt;
165 function __construct( $conf ) {
166 $this->chronProt = new ChronologyProtector;
170 * @param $wiki
171 * @return LoadBalancer
173 function newMainLB( $wiki = false ) {
174 global $wgDBservers, $wgMasterWaitTimeout;
175 if ( $wgDBservers ) {
176 $servers = $wgDBservers;
177 } else {
178 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
179 $servers = array(array(
180 'host' => $wgDBserver,
181 'user' => $wgDBuser,
182 'password' => $wgDBpassword,
183 'dbname' => $wgDBname,
184 'type' => $wgDBtype,
185 'load' => 1,
186 'flags' => ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT
190 return new LoadBalancer( array(
191 'servers' => $servers,
192 'masterWaitTimeout' => $wgMasterWaitTimeout
197 * @param $wiki
198 * @return LoadBalancer
200 function getMainLB( $wiki = false ) {
201 if ( !isset( $this->mainLB ) ) {
202 $this->mainLB = $this->newMainLB( $wiki );
203 $this->mainLB->parentInfo( array( 'id' => 'main' ) );
204 $this->chronProt->initLB( $this->mainLB );
206 return $this->mainLB;
210 * @throws MWException
211 * @param $cluster
212 * @param $wiki
213 * @return LoadBalancer
215 function newExternalLB( $cluster, $wiki = false ) {
216 global $wgExternalServers;
217 if ( !isset( $wgExternalServers[$cluster] ) ) {
218 throw new MWException( __METHOD__.": Unknown cluster \"$cluster\"" );
220 return new LoadBalancer( array(
221 'servers' => $wgExternalServers[$cluster]
226 * @param $cluster
227 * @param $wiki
228 * @return array
230 function &getExternalLB( $cluster, $wiki = false ) {
231 if ( !isset( $this->extLBs[$cluster] ) ) {
232 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
233 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
235 return $this->extLBs[$cluster];
239 * Execute a function for each tracked load balancer
240 * The callback is called with the load balancer as the first parameter,
241 * and $params passed as the subsequent parameters.
242 * @param $callback
243 * @param $params array
245 function forEachLB( $callback, $params = array() ) {
246 if ( isset( $this->mainLB ) ) {
247 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
249 foreach ( $this->extLBs as $lb ) {
250 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
254 function shutdown() {
255 if ( $this->mainLB ) {
256 $this->chronProt->shutdownLB( $this->mainLB );
258 $this->chronProt->shutdown();
259 $this->commitMasterChanges();
264 * LBFactory class that throws an error on any attempt to use it.
265 * This will typically be done via wfGetDB().
266 * Call LBFactory::disableBackend() to start using this, and
267 * LBFactory::enableBackend() to return to normal behavior
269 class LBFactory_Fake extends LBFactory {
270 function __construct( $conf ) {}
272 function newMainLB( $wiki = false) {
273 throw new DBAccessError;
275 function getMainLB( $wiki = false ) {
276 throw new DBAccessError;
278 function newExternalLB( $cluster, $wiki = false ) {
279 throw new DBAccessError;
281 function &getExternalLB( $cluster, $wiki = false ) {
282 throw new DBAccessError;
284 function forEachLB( $callback, $params = array() ) {}
288 * Exception class for attempted DB access
290 class DBAccessError extends MWException {
291 function __construct() {
292 parent::__construct( "Mediawiki tried to access the database via wfGetDB(). This is not allowed." );
297 * Class for ensuring a consistent ordering of events as seen by the user, despite replication.
298 * Kind of like Hawking's [[Chronology Protection Agency]].
300 class ChronologyProtector {
301 var $startupPos;
302 var $shutdownPos = array();
305 * Initialise a LoadBalancer to give it appropriate chronology protection.
307 * @param $lb LoadBalancer
309 function initLB( $lb ) {
310 if ( $this->startupPos === null ) {
311 if ( !empty( $_SESSION[__CLASS__] ) ) {
312 $this->startupPos = $_SESSION[__CLASS__];
315 if ( !$this->startupPos ) {
316 return;
318 $masterName = $lb->getServerName( 0 );
320 if ( $lb->getServerCount() > 1 && !empty( $this->startupPos[$masterName] ) ) {
321 $info = $lb->parentInfo();
322 $pos = $this->startupPos[$masterName];
323 wfDebug( __METHOD__.": LB " . $info['id'] . " waiting for master pos $pos\n" );
324 $lb->waitFor( $this->startupPos[$masterName] );
329 * Notify the ChronologyProtector that the LoadBalancer is about to shut
330 * down. Saves replication positions.
332 * @param $lb LoadBalancer
334 function shutdownLB( $lb ) {
335 // Don't start a session, don't bother with non-replicated setups
336 if ( strval( session_id() ) == '' || $lb->getServerCount() <= 1 ) {
337 return;
339 $masterName = $lb->getServerName( 0 );
340 if ( isset( $this->shutdownPos[$masterName] ) ) {
341 // Already done
342 return;
344 // Only save the position if writes have been done on the connection
345 $db = $lb->getAnyOpenConnection( 0 );
346 $info = $lb->parentInfo();
347 if ( !$db || !$db->doneWrites() ) {
348 wfDebug( __METHOD__.": LB {$info['id']}, no writes done\n" );
349 return;
351 $pos = $db->getMasterPos();
352 wfDebug( __METHOD__.": LB {$info['id']} has master pos $pos\n" );
353 $this->shutdownPos[$masterName] = $pos;
357 * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now.
358 * May commit chronology data to persistent storage.
360 function shutdown() {
361 if ( session_id() != '' && count( $this->shutdownPos ) ) {
362 wfDebug( __METHOD__.": saving master pos for " .
363 count( $this->shutdownPos ) . " master(s)\n" );
364 $_SESSION[__CLASS__] = $this->shutdownPos;