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 global $wgLBFactoryConf;
51 if ( is_null( self
::$instance ) ) {
52 $class = self
::getLBFactoryClass( $wgLBFactoryConf );
54 self
::$instance = new $class( $wgLBFactoryConf );
57 return self
::$instance;
61 * Returns the LBFactory class to use and the load balancer configuration.
63 * @param array $config (e.g. $wgLBFactoryConf)
65 * @return string class name
67 public static function getLBFactoryClass( array $config ) {
68 // For configuration backward compatibility after removing
69 // underscores from class names in MediaWiki 1.23.
71 'LBFactory_Simple' => 'LBFactorySimple',
72 'LBFactory_Single' => 'LBFactorySingle',
73 'LBFactory_Multi' => 'LBFactoryMulti',
74 'LBFactory_Fake' => 'LBFactoryFake',
77 $class = $config['class'];
79 if ( isset( $bcClasses[$class] ) ) {
80 $class = $bcClasses[$class];
82 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
91 * Shut down, close connections and destroy the cached instance.
93 static function destroyInstance() {
94 if ( self
::$instance ) {
95 self
::$instance->shutdown();
96 self
::$instance->forEachLBCallMethod( 'closeAll' );
97 self
::$instance = null;
102 * Set the instance to be the given object
104 * @param $instance LBFactory
106 static function setInstance( $instance ) {
107 self
::destroyInstance();
108 self
::$instance = $instance;
112 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
115 abstract function __construct( $conf );
118 * Create a new load balancer object. The resulting object will be untracked,
119 * not chronology-protected, and the caller is responsible for cleaning it up.
121 * @param string $wiki wiki ID, or false for the current wiki
122 * @return LoadBalancer
124 abstract function newMainLB( $wiki = false );
127 * Get a cached (tracked) load balancer object.
129 * @param string $wiki wiki ID, or false for the current wiki
130 * @return LoadBalancer
132 abstract function getMainLB( $wiki = false );
135 * Create a new load balancer for external storage. The resulting object will be
136 * untracked, not chronology-protected, and the caller is responsible for
139 * @param string $cluster external storage cluster, or false for core
140 * @param string $wiki wiki ID, or false for the current wiki
142 * @return LoadBalancer
144 abstract function newExternalLB( $cluster, $wiki = false );
147 * Get a cached (tracked) load balancer for external storage
149 * @param string $cluster external storage cluster, or false for core
150 * @param string $wiki wiki ID, or false for the current wiki
152 * @return LoadBalancer
154 abstract function &getExternalLB( $cluster, $wiki = false );
157 * Execute a function for each tracked load balancer
158 * The callback is called with the load balancer as the first parameter,
159 * and $params passed as the subsequent parameters.
160 * @param $callback string|array
161 * @param array $params
163 abstract function forEachLB( $callback, $params = array() );
166 * Prepare all tracked load balancers for shutdown
169 function shutdown() {
173 * Call a method of each tracked load balancer
174 * @param $methodName string
177 function forEachLBCallMethod( $methodName, $args = array() ) {
178 $this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) );
182 * Private helper for forEachLBCallMethod
183 * @param $loadBalancer
184 * @param $methodName string
187 function callMethod( $loadBalancer, $methodName, $args ) {
188 call_user_func_array( array( $loadBalancer, $methodName ), $args );
192 * Commit changes on all master connections
194 function commitMasterChanges() {
195 $this->forEachLBCallMethod( 'commitMasterChanges' );
200 * A simple single-master LBFactory that gets its configuration from the b/c globals
202 class LBFactorySimple
extends LBFactory
{
208 var $extLBs = array();
210 # Chronology protector
213 function __construct( $conf ) {
214 $this->chronProt
= new ChronologyProtector
;
219 * @return LoadBalancer
221 function newMainLB( $wiki = false ) {
222 global $wgDBservers, $wgMasterWaitTimeout;
223 if ( $wgDBservers ) {
224 $servers = $wgDBservers;
226 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
227 global $wgDBssl, $wgDBcompress;
229 $flags = ( $wgDebugDumpSql ? DBO_DEBUG
: 0 ) | DBO_DEFAULT
;
233 if ( $wgDBcompress ) {
234 $flags |
= DBO_COMPRESS
;
237 $servers = array( array(
238 'host' => $wgDBserver,
240 'password' => $wgDBpassword,
241 'dbname' => $wgDBname,
248 return new LoadBalancer( array(
249 'servers' => $servers,
250 'masterWaitTimeout' => $wgMasterWaitTimeout
256 * @return LoadBalancer
258 function getMainLB( $wiki = false ) {
259 if ( !isset( $this->mainLB
) ) {
260 $this->mainLB
= $this->newMainLB( $wiki );
261 $this->mainLB
->parentInfo( array( 'id' => 'main' ) );
262 $this->chronProt
->initLB( $this->mainLB
);
265 return $this->mainLB
;
269 * @throws MWException
272 * @return LoadBalancer
274 function newExternalLB( $cluster, $wiki = false ) {
275 global $wgExternalServers;
276 if ( !isset( $wgExternalServers[$cluster] ) ) {
277 throw new MWException( __METHOD__
. ": Unknown cluster \"$cluster\"" );
280 return new LoadBalancer( array(
281 'servers' => $wgExternalServers[$cluster]
290 function &getExternalLB( $cluster, $wiki = false ) {
291 if ( !isset( $this->extLBs
[$cluster] ) ) {
292 $this->extLBs
[$cluster] = $this->newExternalLB( $cluster, $wiki );
293 $this->extLBs
[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
294 $this->chronProt
->initLB( $this->extLBs
[$cluster] );
297 return $this->extLBs
[$cluster];
301 * Execute a function for each tracked load balancer
302 * The callback is called with the load balancer as the first parameter,
303 * and $params passed as the subsequent parameters.
305 * @param $params array
307 function forEachLB( $callback, $params = array() ) {
308 if ( isset( $this->mainLB
) ) {
309 call_user_func_array( $callback, array_merge( array( $this->mainLB
), $params ) );
311 foreach ( $this->extLBs
as $lb ) {
312 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
316 function shutdown() {
317 if ( $this->mainLB
) {
318 $this->chronProt
->shutdownLB( $this->mainLB
);
320 foreach ( $this->extLBs
as $extLB ) {
321 $this->chronProt
->shutdownLB( $extLB );
323 $this->chronProt
->shutdown();
324 $this->commitMasterChanges();
329 * LBFactory class that throws an error on any attempt to use it.
330 * This will typically be done via wfGetDB().
331 * Call LBFactory::disableBackend() to start using this, and
332 * LBFactory::enableBackend() to return to normal behavior
334 class LBFactoryFake
extends LBFactory
{
335 function __construct( $conf ) {
338 function newMainLB( $wiki = false ) {
339 throw new DBAccessError
;
342 function getMainLB( $wiki = false ) {
343 throw new DBAccessError
;
346 function newExternalLB( $cluster, $wiki = false ) {
347 throw new DBAccessError
;
350 function &getExternalLB( $cluster, $wiki = false ) {
351 throw new DBAccessError
;
354 function forEachLB( $callback, $params = array() ) {
359 * Exception class for attempted DB access
361 class DBAccessError
extends MWException
{
362 function __construct() {
363 parent
::__construct( "Mediawiki tried to access the database via wfGetDB(). " .
364 "This is not allowed." );