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