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)
63 * @return string class name
65 public static function getLBFactoryClass( array $config ) {
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 = $config['class'];
77 if ( isset( $bcClasses[$class] ) ) {
78 $class = $bcClasses[$class];
80 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
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
{
201 /** @var LoadBalancer */
204 /** @var LoadBalancer[] */
205 protected $extLBs = array();
207 /** @var ChronologyProtector */
208 protected $chronProt;
210 function __construct( $conf ) {
211 $this->chronProt
= new ChronologyProtector
;
216 * @return LoadBalancer
218 function newMainLB( $wiki = false ) {
219 global $wgDBservers, $wgMasterWaitTimeout;
220 if ( $wgDBservers ) {
221 $servers = $wgDBservers;
223 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
224 global $wgDBssl, $wgDBcompress;
226 $flags = ( $wgDebugDumpSql ? DBO_DEBUG
: 0 ) | DBO_DEFAULT
;
230 if ( $wgDBcompress ) {
231 $flags |
= DBO_COMPRESS
;
234 $servers = array( array(
235 'host' => $wgDBserver,
237 'password' => $wgDBpassword,
238 'dbname' => $wgDBname,
245 return new LoadBalancer( array(
246 'servers' => $servers,
247 'masterWaitTimeout' => $wgMasterWaitTimeout
253 * @return LoadBalancer
255 function getMainLB( $wiki = false ) {
256 if ( !isset( $this->mainLB
) ) {
257 $this->mainLB
= $this->newMainLB( $wiki );
258 $this->mainLB
->parentInfo( array( 'id' => 'main' ) );
259 $this->chronProt
->initLB( $this->mainLB
);
262 return $this->mainLB
;
266 * @throws MWException
269 * @return LoadBalancer
271 function newExternalLB( $cluster, $wiki = false ) {
272 global $wgExternalServers;
273 if ( !isset( $wgExternalServers[$cluster] ) ) {
274 throw new MWException( __METHOD__
. ": Unknown cluster \"$cluster\"" );
277 return new LoadBalancer( array(
278 'servers' => $wgExternalServers[$cluster]
287 function &getExternalLB( $cluster, $wiki = false ) {
288 if ( !isset( $this->extLBs
[$cluster] ) ) {
289 $this->extLBs
[$cluster] = $this->newExternalLB( $cluster, $wiki );
290 $this->extLBs
[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
291 $this->chronProt
->initLB( $this->extLBs
[$cluster] );
294 return $this->extLBs
[$cluster];
298 * Execute a function for each tracked load balancer
299 * The callback is called with the load balancer as the first parameter,
300 * and $params passed as the subsequent parameters.
302 * @param $params array
304 function forEachLB( $callback, $params = array() ) {
305 if ( isset( $this->mainLB
) ) {
306 call_user_func_array( $callback, array_merge( array( $this->mainLB
), $params ) );
308 foreach ( $this->extLBs
as $lb ) {
309 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
313 function shutdown() {
314 if ( $this->mainLB
) {
315 $this->chronProt
->shutdownLB( $this->mainLB
);
317 foreach ( $this->extLBs
as $extLB ) {
318 $this->chronProt
->shutdownLB( $extLB );
320 $this->chronProt
->shutdown();
321 $this->commitMasterChanges();
326 * LBFactory class that throws an error on any attempt to use it.
327 * This will typically be done via wfGetDB().
328 * Call LBFactory::disableBackend() to start using this, and
329 * LBFactory::enableBackend() to return to normal behavior
331 class LBFactoryFake
extends LBFactory
{
332 function __construct( $conf ) {
335 function newMainLB( $wiki = false ) {
336 throw new DBAccessError
;
339 function getMainLB( $wiki = false ) {
340 throw new DBAccessError
;
343 function newExternalLB( $cluster, $wiki = false ) {
344 throw new DBAccessError
;
347 function &getExternalLB( $cluster, $wiki = false ) {
348 throw new DBAccessError
;
351 function forEachLB( $callback, $params = array() ) {
356 * Exception class for attempted DB access
358 class DBAccessError
extends MWException
{
359 function __construct() {
360 parent
::__construct( "Mediawiki tried to access the database via wfGetDB(). " .
361 "This is not allowed." );