Merge "Moved ScopedCallback to /libs"
[lhc/web/wiklou.git] / includes / db / LBFactory.php
1 <?php
2 /**
3 * Generator of database load balancing objects.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 /**
25 * An interface for generating database load balancers
26 * @ingroup Database
27 */
28 abstract class LBFactory {
29 /**
30 * @var LBFactory
31 */
32 static $instance;
33
34 /**
35 * Disables all access to the load balancer, will cause all database access
36 * to throw a DBAccessError
37 */
38 public static function disableBackend() {
39 global $wgLBFactoryConf;
40 self::$instance = new LBFactoryFake( $wgLBFactoryConf );
41 }
42
43 /**
44 * Get an LBFactory instance
45 *
46 * @return LBFactory
47 */
48 static function &singleton() {
49 if ( is_null( self::$instance ) ) {
50 $LBFactoryConf = self::getLBFactoryClass();
51
52 self::$instance = new $LBFactoryConf[0]( $LBFactoryConf[1] );
53 }
54
55 return self::$instance;
56 }
57
58 /**
59 * Returns the LBFactory class to use and the load balancer configuration.
60 *
61 * @return array ( factory class, $wgLBFactoryConf )
62 */
63 static function getLBFactoryClass() {
64 global $wgLBFactoryConf;
65
66 // For configuration backward compatibility after removing
67 // underscores from class names in MediaWiki 1.23.
68 $bcClasses = array(
69 'LBFactory_Simple' => 'LBFactorySimple',
70 'LBFactory_Single' => 'LBFactorySingle',
71 'LBFactory_Multi' => 'LBFactoryMulti',
72 'LBFactory_Fake' => 'LBFactoryFake',
73 );
74
75 $class = $wgLBFactoryConf['class'];
76
77 if ( in_array( $class, array_keys( $bcClasses ) ) ) {
78 $class = $bcClasses[$class];
79 wfDeprecated(
80 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
81 '1.23'
82 );
83 }
84
85 return array( $class, $wgLBFactoryConf );
86 }
87
88 /**
89 * Shut down, close connections and destroy the cached instance.
90 */
91 static function destroyInstance() {
92 if ( self::$instance ) {
93 self::$instance->shutdown();
94 self::$instance->forEachLBCallMethod( 'closeAll' );
95 self::$instance = null;
96 }
97 }
98
99 /**
100 * Set the instance to be the given object
101 *
102 * @param $instance LBFactory
103 */
104 static function setInstance( $instance ) {
105 self::destroyInstance();
106 self::$instance = $instance;
107 }
108
109 /**
110 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
111 * @param $conf
112 */
113 abstract function __construct( $conf );
114
115 /**
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.
118 *
119 * @param string $wiki wiki ID, or false for the current wiki
120 * @return LoadBalancer
121 */
122 abstract function newMainLB( $wiki = false );
123
124 /**
125 * Get a cached (tracked) load balancer object.
126 *
127 * @param string $wiki wiki ID, or false for the current wiki
128 * @return LoadBalancer
129 */
130 abstract function getMainLB( $wiki = false );
131
132 /**
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
135 * cleaning it up.
136 *
137 * @param string $cluster external storage cluster, or false for core
138 * @param string $wiki wiki ID, or false for the current wiki
139 *
140 * @return LoadBalancer
141 */
142 abstract function newExternalLB( $cluster, $wiki = false );
143
144 /**
145 * Get a cached (tracked) load balancer for external storage
146 *
147 * @param string $cluster external storage cluster, or false for core
148 * @param string $wiki wiki ID, or false for the current wiki
149 *
150 * @return LoadBalancer
151 */
152 abstract function &getExternalLB( $cluster, $wiki = false );
153
154 /**
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
160 */
161 abstract function forEachLB( $callback, $params = array() );
162
163 /**
164 * Prepare all tracked load balancers for shutdown
165 * STUB
166 */
167 function shutdown() {
168 }
169
170 /**
171 * Call a method of each tracked load balancer
172 * @param $methodName string
173 * @param $args array
174 */
175 function forEachLBCallMethod( $methodName, $args = array() ) {
176 $this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) );
177 }
178
179 /**
180 * Private helper for forEachLBCallMethod
181 * @param $loadBalancer
182 * @param $methodName string
183 * @param $args
184 */
185 function callMethod( $loadBalancer, $methodName, $args ) {
186 call_user_func_array( array( $loadBalancer, $methodName ), $args );
187 }
188
189 /**
190 * Commit changes on all master connections
191 */
192 function commitMasterChanges() {
193 $this->forEachLBCallMethod( 'commitMasterChanges' );
194 }
195 }
196
197 /**
198 * A simple single-master LBFactory that gets its configuration from the b/c globals
199 */
200 class LBFactorySimple extends LBFactory {
201
202 /**
203 * @var LoadBalancer
204 */
205 var $mainLB;
206 var $extLBs = array();
207
208 # Chronology protector
209 var $chronProt;
210
211 function __construct( $conf ) {
212 $this->chronProt = new ChronologyProtector;
213 }
214
215 /**
216 * @param $wiki
217 * @return LoadBalancer
218 */
219 function newMainLB( $wiki = false ) {
220 global $wgDBservers, $wgMasterWaitTimeout;
221 if ( $wgDBservers ) {
222 $servers = $wgDBservers;
223 } else {
224 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
225 global $wgDBssl, $wgDBcompress;
226
227 $flags = ( $wgDebugDumpSql ? DBO_DEBUG : 0 ) | DBO_DEFAULT;
228 if ( $wgDBssl ) {
229 $flags |= DBO_SSL;
230 }
231 if ( $wgDBcompress ) {
232 $flags |= DBO_COMPRESS;
233 }
234
235 $servers = array( array(
236 'host' => $wgDBserver,
237 'user' => $wgDBuser,
238 'password' => $wgDBpassword,
239 'dbname' => $wgDBname,
240 'type' => $wgDBtype,
241 'load' => 1,
242 'flags' => $flags
243 ) );
244 }
245
246 return new LoadBalancer( array(
247 'servers' => $servers,
248 'masterWaitTimeout' => $wgMasterWaitTimeout
249 ) );
250 }
251
252 /**
253 * @param $wiki
254 * @return LoadBalancer
255 */
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 );
261 }
262
263 return $this->mainLB;
264 }
265
266 /**
267 * @throws MWException
268 * @param $cluster
269 * @param $wiki
270 * @return LoadBalancer
271 */
272 function newExternalLB( $cluster, $wiki = false ) {
273 global $wgExternalServers;
274 if ( !isset( $wgExternalServers[$cluster] ) ) {
275 throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
276 }
277
278 return new LoadBalancer( array(
279 'servers' => $wgExternalServers[$cluster]
280 ) );
281 }
282
283 /**
284 * @param $cluster
285 * @param $wiki
286 * @return array
287 */
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] );
293 }
294
295 return $this->extLBs[$cluster];
296 }
297
298 /**
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.
302 * @param $callback
303 * @param $params array
304 */
305 function forEachLB( $callback, $params = array() ) {
306 if ( isset( $this->mainLB ) ) {
307 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
308 }
309 foreach ( $this->extLBs as $lb ) {
310 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
311 }
312 }
313
314 function shutdown() {
315 if ( $this->mainLB ) {
316 $this->chronProt->shutdownLB( $this->mainLB );
317 }
318 foreach ( $this->extLBs as $extLB ) {
319 $this->chronProt->shutdownLB( $extLB );
320 }
321 $this->chronProt->shutdown();
322 $this->commitMasterChanges();
323 }
324 }
325
326 /**
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
331 */
332 class LBFactoryFake extends LBFactory {
333 function __construct( $conf ) {
334 }
335
336 function newMainLB( $wiki = false ) {
337 throw new DBAccessError;
338 }
339
340 function getMainLB( $wiki = false ) {
341 throw new DBAccessError;
342 }
343
344 function newExternalLB( $cluster, $wiki = false ) {
345 throw new DBAccessError;
346 }
347
348 function &getExternalLB( $cluster, $wiki = false ) {
349 throw new DBAccessError;
350 }
351
352 function forEachLB( $callback, $params = array() ) {
353 }
354 }
355
356 /**
357 * Exception class for attempted DB access
358 */
359 class DBAccessError extends MWException {
360 function __construct() {
361 parent::__construct( "Mediawiki tried to access the database via wfGetDB(). " .
362 "This is not allowed." );
363 }
364 }