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
{
35 * Disables all access to the load balancer, will cause all database access
36 * to throw a DBAccessError
38 public static function disableBackend() {
39 global $wgLBFactoryConf;
40 self
::$instance = new LBFactoryFake( $wgLBFactoryConf );
44 * Get an LBFactory instance
48 static function &singleton() {
49 if ( is_null( self
::$instance ) ) {
50 $LBFactoryConf = self
::getLBFactoryClass();
52 self
::$instance = new $LBFactoryConf[0]( $LBFactoryConf[1] );
55 return self
::$instance;
59 * Returns the LBFactory class to use and the load balancer configuration.
61 * @return array ( factory class, $wgLBFactoryConf )
63 static function getLBFactoryClass() {
64 global $wgLBFactoryConf;
66 // For configuration backward compatibility after removing
67 // underscores from class names in MediaWiki 1.23.
69 'LBFactory_Simple' => 'LBFactorySimple',
70 'LBFactory_Single' => 'LBFactorySingle',
71 'LBFactory_Multi' => 'LBFactoryMulti',
72 'LBFactory_Fake' => 'LBFactoryFake',
75 $class = $wgLBFactoryConf['class'];
77 if ( in_array( $class, array_keys( $bcClasses ) ) ) {
78 $class = $bcClasses[$class];
80 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
85 return array( $class, $wgLBFactoryConf );
89 * Shut down, close connections and destroy the cached instance.
91 static function destroyInstance() {
92 if ( self
::$instance ) {
93 self
::$instance->shutdown();
94 self
::$instance->forEachLBCallMethod( 'closeAll' );
95 self
::$instance = null;
100 * Set the instance to be the given object
102 * @param $instance LBFactory
104 static function setInstance( $instance ) {
105 self
::destroyInstance();
106 self
::$instance = $instance;
110 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
113 abstract function __construct( $conf );
116 * Create a new load balancer object. The resulting object will be untracked,
117 * not chronology-protected, and the caller is responsible for cleaning it up.
119 * @param string $wiki wiki ID, or false for the current wiki
120 * @return LoadBalancer
122 abstract function newMainLB( $wiki = false );
125 * Get a cached (tracked) load balancer object.
127 * @param string $wiki wiki ID, or false for the current wiki
128 * @return LoadBalancer
130 abstract function getMainLB( $wiki = false );
133 * Create a new load balancer for external storage. The resulting object will be
134 * untracked, not chronology-protected, and the caller is responsible for
137 * @param string $cluster external storage cluster, or false for core
138 * @param string $wiki wiki ID, or false for the current wiki
140 * @return LoadBalancer
142 abstract function newExternalLB( $cluster, $wiki = false );
145 * Get a cached (tracked) load balancer for external storage
147 * @param string $cluster external storage cluster, or false for core
148 * @param string $wiki wiki ID, or false for the current wiki
150 * @return LoadBalancer
152 abstract function &getExternalLB( $cluster, $wiki = false );
155 * Execute a function for each tracked load balancer
156 * The callback is called with the load balancer as the first parameter,
157 * and $params passed as the subsequent parameters.
158 * @param $callback string|array
159 * @param array $params
161 abstract function forEachLB( $callback, $params = array() );
164 * Prepare all tracked load balancers for shutdown
167 function shutdown() {
171 * Call a method of each tracked load balancer
172 * @param $methodName string
175 function forEachLBCallMethod( $methodName, $args = array() ) {
176 $this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) );
180 * Private helper for forEachLBCallMethod
181 * @param $loadBalancer
182 * @param $methodName string
185 function callMethod( $loadBalancer, $methodName, $args ) {
186 call_user_func_array( array( $loadBalancer, $methodName ), $args );
190 * Commit changes on all master connections
192 function commitMasterChanges() {
193 $this->forEachLBCallMethod( 'commitMasterChanges' );
198 * A simple single-master LBFactory that gets its configuration from the b/c globals
200 class LBFactorySimple
extends LBFactory
{
206 var $extLBs = array();
208 # Chronology protector
211 function __construct( $conf ) {
212 $this->chronProt
= new ChronologyProtector
;
217 * @return LoadBalancer
219 function newMainLB( $wiki = false ) {
220 global $wgDBservers, $wgMasterWaitTimeout;
221 if ( $wgDBservers ) {
222 $servers = $wgDBservers;
224 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
225 global $wgDBssl, $wgDBcompress;
227 $flags = ( $wgDebugDumpSql ? DBO_DEBUG
: 0 ) | DBO_DEFAULT
;
231 if ( $wgDBcompress ) {
232 $flags |
= DBO_COMPRESS
;
235 $servers = array( array(
236 'host' => $wgDBserver,
238 'password' => $wgDBpassword,
239 'dbname' => $wgDBname,
246 return new LoadBalancer( array(
247 'servers' => $servers,
248 'masterWaitTimeout' => $wgMasterWaitTimeout
254 * @return LoadBalancer
256 function getMainLB( $wiki = false ) {
257 if ( !isset( $this->mainLB
) ) {
258 $this->mainLB
= $this->newMainLB( $wiki );
259 $this->mainLB
->parentInfo( array( 'id' => 'main' ) );
260 $this->chronProt
->initLB( $this->mainLB
);
263 return $this->mainLB
;
267 * @throws MWException
270 * @return LoadBalancer
272 function newExternalLB( $cluster, $wiki = false ) {
273 global $wgExternalServers;
274 if ( !isset( $wgExternalServers[$cluster] ) ) {
275 throw new MWException( __METHOD__
. ": Unknown cluster \"$cluster\"" );
278 return new LoadBalancer( array(
279 'servers' => $wgExternalServers[$cluster]
288 function &getExternalLB( $cluster, $wiki = false ) {
289 if ( !isset( $this->extLBs
[$cluster] ) ) {
290 $this->extLBs
[$cluster] = $this->newExternalLB( $cluster, $wiki );
291 $this->extLBs
[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
292 $this->chronProt
->initLB( $this->extLBs
[$cluster] );
295 return $this->extLBs
[$cluster];
299 * Execute a function for each tracked load balancer
300 * The callback is called with the load balancer as the first parameter,
301 * and $params passed as the subsequent parameters.
303 * @param $params array
305 function forEachLB( $callback, $params = array() ) {
306 if ( isset( $this->mainLB
) ) {
307 call_user_func_array( $callback, array_merge( array( $this->mainLB
), $params ) );
309 foreach ( $this->extLBs
as $lb ) {
310 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
314 function shutdown() {
315 if ( $this->mainLB
) {
316 $this->chronProt
->shutdownLB( $this->mainLB
);
318 foreach ( $this->extLBs
as $extLB ) {
319 $this->chronProt
->shutdownLB( $extLB );
321 $this->chronProt
->shutdown();
322 $this->commitMasterChanges();
327 * LBFactory class that throws an error on any attempt to use it.
328 * This will typically be done via wfGetDB().
329 * Call LBFactory::disableBackend() to start using this, and
330 * LBFactory::enableBackend() to return to normal behavior
332 class LBFactoryFake
extends LBFactory
{
333 function __construct( $conf ) {
336 function newMainLB( $wiki = false ) {
337 throw new DBAccessError
;
340 function getMainLB( $wiki = false ) {
341 throw new DBAccessError
;
344 function newExternalLB( $cluster, $wiki = false ) {
345 throw new DBAccessError
;
348 function &getExternalLB( $cluster, $wiki = false ) {
349 throw new DBAccessError
;
352 function forEachLB( $callback, $params = array() ) {
357 * Exception class for attempted DB access
359 class DBAccessError
extends MWException
{
360 function __construct() {
361 parent
::__construct( "Mediawiki tried to access the database via wfGetDB(). " .
362 "This is not allowed." );