3 * Generator of database load balancing objects.
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.
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.
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
25 * An interface for generating database load balancers
28 abstract class LBFactory
{
30 protected static $instance;
33 * Disables all access to the load balancer, will cause all database access
34 * to throw a DBAccessError
36 public static function disableBackend() {
37 global $wgLBFactoryConf;
38 self
::$instance = new LBFactoryFake( $wgLBFactoryConf );
42 * Get an LBFactory instance
46 static function &singleton() {
47 global $wgLBFactoryConf;
49 if ( is_null( self
::$instance ) ) {
50 $class = self
::getLBFactoryClass( $wgLBFactoryConf );
52 self
::$instance = new $class( $wgLBFactoryConf );
55 return self
::$instance;
59 * Returns the LBFactory class to use and the load balancer configuration.
61 * @param array $config (e.g. $wgLBFactoryConf)
62 * @return string class name
64 public static function getLBFactoryClass( array $config ) {
65 // For configuration backward compatibility after removing
66 // underscores from class names in MediaWiki 1.23.
68 'LBFactory_Simple' => 'LBFactorySimple',
69 'LBFactory_Single' => 'LBFactorySingle',
70 'LBFactory_Multi' => 'LBFactoryMulti',
71 'LBFactory_Fake' => 'LBFactoryFake',
74 $class = $config['class'];
76 if ( isset( $bcClasses[$class] ) ) {
77 $class = $bcClasses[$class];
79 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
88 * Shut down, close connections and destroy the cached instance.
90 static function destroyInstance() {
91 if ( self
::$instance ) {
92 self
::$instance->shutdown();
93 self
::$instance->forEachLBCallMethod( 'closeAll' );
94 self
::$instance = null;
99 * Set the instance to be the given object
101 * @param LBFactory $instance
103 static function setInstance( $instance ) {
104 self
::destroyInstance();
105 self
::$instance = $instance;
109 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
112 abstract function __construct( $conf );
115 * Create a new load balancer object. The resulting object will be untracked,
116 * not chronology-protected, and the caller is responsible for cleaning it up.
118 * @param bool|string $wiki Wiki ID, or false for the current wiki
119 * @return LoadBalancer
121 abstract function newMainLB( $wiki = false );
124 * Get a cached (tracked) load balancer object.
126 * @param bool|string $wiki Wiki ID, or false for the current wiki
127 * @return LoadBalancer
129 abstract function getMainLB( $wiki = false );
132 * Create a new load balancer for external storage. The resulting object will be
133 * untracked, not chronology-protected, and the caller is responsible for
136 * @param string $cluster External storage cluster, or false for core
137 * @param bool|string $wiki Wiki ID, or false for the current wiki
138 * @return LoadBalancer
140 abstract function newExternalLB( $cluster, $wiki = false );
143 * Get a cached (tracked) load balancer for external storage
145 * @param string $cluster external storage cluster, or false for core
146 * @param bool|string $wiki Wiki ID, or false for the current wiki
147 * @return LoadBalancer
149 abstract function &getExternalLB( $cluster, $wiki = false );
152 * Execute a function for each tracked load balancer
153 * The callback is called with the load balancer as the first parameter,
154 * and $params passed as the subsequent parameters.
156 * @param callable $callback
157 * @param array $params
159 abstract function forEachLB( $callback, $params = array() );
162 * Prepare all tracked load balancers for shutdown
165 function shutdown() {
169 * Call a method of each tracked load balancer
171 * @param string $methodName
174 function forEachLBCallMethod( $methodName, $args = array() ) {
175 $this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) );
179 * Private helper for forEachLBCallMethod
180 * @param LoadBalancer $loadBalancer
181 * @param string $methodName
184 function callMethod( $loadBalancer, $methodName, $args ) {
185 call_user_func_array( array( $loadBalancer, $methodName ), $args );
189 * Commit changes on all master connections
191 function commitMasterChanges() {
192 $this->forEachLBCallMethod( 'commitMasterChanges' );
196 * Rollback changes on all master connections
199 function rollbackMasterChanges() {
200 $this->forEachLBCallMethod( 'rollbackMasterChanges' );
204 * Detemine if any master connection has pending changes.
208 function hasMasterChanges() {
210 $this->forEachLB( function ( $lb ) use ( &$ret ) {
211 $ret = $ret ||
$lb->hasMasterChanges();
218 * A simple single-master LBFactory that gets its configuration from the b/c globals
220 class LBFactorySimple
extends LBFactory
{
221 /** @var LoadBalancer */
224 /** @var LoadBalancer[] */
225 protected $extLBs = array();
227 /** @var ChronologyProtector */
228 protected $chronProt;
230 function __construct( $conf ) {
231 $this->chronProt
= new ChronologyProtector
;
235 * @param bool|string $wiki
236 * @return LoadBalancer
238 function newMainLB( $wiki = false ) {
239 global $wgDBservers, $wgMasterWaitTimeout;
240 if ( $wgDBservers ) {
241 $servers = $wgDBservers;
243 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
244 global $wgDBssl, $wgDBcompress;
246 $flags = ( $wgDebugDumpSql ? DBO_DEBUG
: 0 ) | DBO_DEFAULT
;
250 if ( $wgDBcompress ) {
251 $flags |
= DBO_COMPRESS
;
254 $servers = array( array(
255 'host' => $wgDBserver,
257 'password' => $wgDBpassword,
258 'dbname' => $wgDBname,
265 return new LoadBalancer( array(
266 'servers' => $servers,
267 'masterWaitTimeout' => $wgMasterWaitTimeout
272 * @param bool|string $wiki
273 * @return LoadBalancer
275 function getMainLB( $wiki = false ) {
276 if ( !isset( $this->mainLB
) ) {
277 $this->mainLB
= $this->newMainLB( $wiki );
278 $this->mainLB
->parentInfo( array( 'id' => 'main' ) );
279 $this->chronProt
->initLB( $this->mainLB
);
282 return $this->mainLB
;
286 * @throws MWException
287 * @param string $cluster
288 * @param bool|string $wiki
289 * @return LoadBalancer
291 function newExternalLB( $cluster, $wiki = false ) {
292 global $wgExternalServers;
293 if ( !isset( $wgExternalServers[$cluster] ) ) {
294 throw new MWException( __METHOD__
. ": Unknown cluster \"$cluster\"" );
297 return new LoadBalancer( array(
298 'servers' => $wgExternalServers[$cluster]
303 * @param string $cluster
304 * @param bool|string $wiki
307 function &getExternalLB( $cluster, $wiki = false ) {
308 if ( !isset( $this->extLBs
[$cluster] ) ) {
309 $this->extLBs
[$cluster] = $this->newExternalLB( $cluster, $wiki );
310 $this->extLBs
[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
311 $this->chronProt
->initLB( $this->extLBs
[$cluster] );
314 return $this->extLBs
[$cluster];
318 * Execute a function for each tracked load balancer
319 * The callback is called with the load balancer as the first parameter,
320 * and $params passed as the subsequent parameters.
322 * @param callable $callback
323 * @param array $params
325 function forEachLB( $callback, $params = array() ) {
326 if ( isset( $this->mainLB
) ) {
327 call_user_func_array( $callback, array_merge( array( $this->mainLB
), $params ) );
329 foreach ( $this->extLBs
as $lb ) {
330 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
334 function shutdown() {
335 if ( $this->mainLB
) {
336 $this->chronProt
->shutdownLB( $this->mainLB
);
338 foreach ( $this->extLBs
as $extLB ) {
339 $this->chronProt
->shutdownLB( $extLB );
341 $this->chronProt
->shutdown();
342 $this->commitMasterChanges();
347 * LBFactory class that throws an error on any attempt to use it.
348 * This will typically be done via wfGetDB().
349 * Call LBFactory::disableBackend() to start using this, and
350 * LBFactory::enableBackend() to return to normal behavior
352 class LBFactoryFake
extends LBFactory
{
353 function __construct( $conf ) {
356 function newMainLB( $wiki = false ) {
357 throw new DBAccessError
;
360 function getMainLB( $wiki = false ) {
361 throw new DBAccessError
;
364 function newExternalLB( $cluster, $wiki = false ) {
365 throw new DBAccessError
;
368 function &getExternalLB( $cluster, $wiki = false ) {
369 throw new DBAccessError
;
372 function forEachLB( $callback, $params = array() ) {
377 * Exception class for attempted DB access
379 class DBAccessError
extends MWException
{
380 function __construct() {
381 parent
::__construct( "Mediawiki tried to access the database via wfGetDB(). " .
382 "This is not allowed." );