3 * Generator of database load balancing objects
10 * An interface for generating database load balancers
13 abstract class LBFactory
{
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 );
30 * Get an LBFactory instance
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;
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;
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;
65 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
68 abstract function __construct( $conf );
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 );
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 );
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
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 );
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
122 function shutdown() {}
125 * Call a method of each tracked load balancer
126 * @param $methodName string
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
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
{
160 var $extLBs = array();
162 # Chronology protector
165 function __construct( $conf ) {
166 $this->chronProt
= new ChronologyProtector
;
171 * @return LoadBalancer
173 function newMainLB( $wiki = false ) {
174 global $wgDBservers, $wgMasterWaitTimeout;
175 if ( $wgDBservers ) {
176 $servers = $wgDBservers;
178 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
179 $servers = array(array(
180 'host' => $wgDBserver,
182 'password' => $wgDBpassword,
183 'dbname' => $wgDBname,
186 'flags' => ($wgDebugDumpSql ? DBO_DEBUG
: 0) | DBO_DEFAULT
190 return new LoadBalancer( array(
191 'servers' => $servers,
192 'masterWaitTimeout' => $wgMasterWaitTimeout
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
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]
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.
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
{
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
) {
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 ) {
339 $masterName = $lb->getServerName( 0 );
340 if ( isset( $this->shutdownPos
[$masterName] ) ) {
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" );
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
;