Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / db / LBFactory.php
blob16c43a009fbda2a1cc8cc4f262f08816aa5b2f26
1 <?php
2 /**
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
20 * @file
21 * @ingroup Database
24 /**
25 * An interface for generating database load balancers
26 * @ingroup Database
28 abstract class LBFactory {
30 /**
31 * @var LBFactory
33 static $instance;
35 /**
36 * Disables all access to the load balancer, will cause all database access
37 * to throw a DBAccessError
39 public static function disableBackend() {
40 global $wgLBFactoryConf;
41 self::$instance = new LBFactory_Fake( $wgLBFactoryConf );
44 /**
45 * Get an LBFactory instance
47 * @return LBFactory
49 static function &singleton() {
50 if ( is_null( self::$instance ) ) {
51 global $wgLBFactoryConf;
52 $class = $wgLBFactoryConf['class'];
53 self::$instance = new $class( $wgLBFactoryConf );
55 return self::$instance;
58 /**
59 * Shut down, close connections and destroy the cached instance.
61 static function destroyInstance() {
62 if ( self::$instance ) {
63 self::$instance->shutdown();
64 self::$instance->forEachLBCallMethod( 'closeAll' );
65 self::$instance = null;
69 /**
70 * Set the instance to be the given object
72 * @param $instance LBFactory
74 static function setInstance( $instance ) {
75 self::destroyInstance();
76 self::$instance = $instance;
79 /**
80 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
81 * @param $conf
83 abstract function __construct( $conf );
85 /**
86 * Create a new load balancer object. The resulting object will be untracked,
87 * not chronology-protected, and the caller is responsible for cleaning it up.
89 * @param string $wiki wiki ID, or false for the current wiki
90 * @return LoadBalancer
92 abstract function newMainLB( $wiki = false );
94 /**
95 * Get a cached (tracked) load balancer object.
97 * @param string $wiki wiki ID, or false for the current wiki
98 * @return LoadBalancer
100 abstract function getMainLB( $wiki = false );
103 * Create a new load balancer for external storage. The resulting object will be
104 * untracked, not chronology-protected, and the caller is responsible for
105 * cleaning it up.
107 * @param string $cluster external storage cluster, or false for core
108 * @param string $wiki wiki ID, or false for the current wiki
110 * @return LoadBalancer
112 abstract function newExternalLB( $cluster, $wiki = false );
115 * Get a cached (tracked) load balancer for external storage
117 * @param string $cluster external storage cluster, or false for core
118 * @param string $wiki wiki ID, or false for the current wiki
120 * @return LoadBalancer
122 abstract function &getExternalLB( $cluster, $wiki = false );
125 * Execute a function for each tracked load balancer
126 * The callback is called with the load balancer as the first parameter,
127 * and $params passed as the subsequent parameters.
128 * @param $callback string|array
129 * @param array $params
131 abstract function forEachLB( $callback, $params = array() );
134 * Prepare all tracked load balancers for shutdown
135 * STUB
137 function shutdown() {
141 * Call a method of each tracked load balancer
142 * @param $methodName string
143 * @param $args array
145 function forEachLBCallMethod( $methodName, $args = array() ) {
146 $this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) );
150 * Private helper for forEachLBCallMethod
151 * @param $loadBalancer
152 * @param $methodName string
153 * @param $args
155 function callMethod( $loadBalancer, $methodName, $args ) {
156 call_user_func_array( array( $loadBalancer, $methodName ), $args );
160 * Commit changes on all master connections
162 function commitMasterChanges() {
163 $this->forEachLBCallMethod( 'commitMasterChanges' );
168 * A simple single-master LBFactory that gets its configuration from the b/c globals
170 class LBFactory_Simple extends LBFactory {
173 * @var LoadBalancer
175 var $mainLB;
176 var $extLBs = array();
178 # Chronology protector
179 var $chronProt;
181 function __construct( $conf ) {
182 $this->chronProt = new ChronologyProtector;
186 * @param $wiki
187 * @return LoadBalancer
189 function newMainLB( $wiki = false ) {
190 global $wgDBservers, $wgMasterWaitTimeout;
191 if ( $wgDBservers ) {
192 $servers = $wgDBservers;
193 } else {
194 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
195 global $wgDBssl, $wgDBcompress;
197 $flags = ( $wgDebugDumpSql ? DBO_DEBUG : 0 ) | DBO_DEFAULT;
198 if ( $wgDBssl ) {
199 $flags |= DBO_SSL;
201 if ( $wgDBcompress ) {
202 $flags |= DBO_COMPRESS;
205 $servers = array( array(
206 'host' => $wgDBserver,
207 'user' => $wgDBuser,
208 'password' => $wgDBpassword,
209 'dbname' => $wgDBname,
210 'type' => $wgDBtype,
211 'load' => 1,
212 'flags' => $flags
216 return new LoadBalancer( array(
217 'servers' => $servers,
218 'masterWaitTimeout' => $wgMasterWaitTimeout
223 * @param $wiki
224 * @return LoadBalancer
226 function getMainLB( $wiki = false ) {
227 if ( !isset( $this->mainLB ) ) {
228 $this->mainLB = $this->newMainLB( $wiki );
229 $this->mainLB->parentInfo( array( 'id' => 'main' ) );
230 $this->chronProt->initLB( $this->mainLB );
232 return $this->mainLB;
236 * @throws MWException
237 * @param $cluster
238 * @param $wiki
239 * @return LoadBalancer
241 function newExternalLB( $cluster, $wiki = false ) {
242 global $wgExternalServers;
243 if ( !isset( $wgExternalServers[$cluster] ) ) {
244 throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
246 return new LoadBalancer( array(
247 'servers' => $wgExternalServers[$cluster]
252 * @param $cluster
253 * @param $wiki
254 * @return array
256 function &getExternalLB( $cluster, $wiki = false ) {
257 if ( !isset( $this->extLBs[$cluster] ) ) {
258 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
259 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
260 $this->chronProt->initLB( $this->extLBs[$cluster] );
262 return $this->extLBs[$cluster];
266 * Execute a function for each tracked load balancer
267 * The callback is called with the load balancer as the first parameter,
268 * and $params passed as the subsequent parameters.
269 * @param $callback
270 * @param $params array
272 function forEachLB( $callback, $params = array() ) {
273 if ( isset( $this->mainLB ) ) {
274 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
276 foreach ( $this->extLBs as $lb ) {
277 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
281 function shutdown() {
282 if ( $this->mainLB ) {
283 $this->chronProt->shutdownLB( $this->mainLB );
285 foreach ( $this->extLBs as $extLB ) {
286 $this->chronProt->shutdownLB( $extLB );
288 $this->chronProt->shutdown();
289 $this->commitMasterChanges();
294 * LBFactory class that throws an error on any attempt to use it.
295 * This will typically be done via wfGetDB().
296 * Call LBFactory::disableBackend() to start using this, and
297 * LBFactory::enableBackend() to return to normal behavior
299 class LBFactory_Fake extends LBFactory {
300 function __construct( $conf ) {
303 function newMainLB( $wiki = false ) {
304 throw new DBAccessError;
307 function getMainLB( $wiki = false ) {
308 throw new DBAccessError;
311 function newExternalLB( $cluster, $wiki = false ) {
312 throw new DBAccessError;
315 function &getExternalLB( $cluster, $wiki = false ) {
316 throw new DBAccessError;
319 function forEachLB( $callback, $params = array() ) {
324 * Exception class for attempted DB access
326 class DBAccessError extends MWException {
327 function __construct() {
328 parent::__construct( "Mediawiki tried to access the database via wfGetDB(). This is not allowed." );