Merge "Don't check namespace in SpecialWantedtemplates"
[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 /** @var LBFactory */
30 private static $instance;
31
32 /**
33 * Disables all access to the load balancer, will cause all database access
34 * to throw a DBAccessError
35 */
36 public static function disableBackend() {
37 global $wgLBFactoryConf;
38 self::$instance = new LBFactoryFake( $wgLBFactoryConf );
39 }
40
41 /**
42 * Get an LBFactory instance
43 *
44 * @return LBFactory
45 */
46 public static function singleton() {
47 global $wgLBFactoryConf;
48
49 if ( is_null( self::$instance ) ) {
50 $class = self::getLBFactoryClass( $wgLBFactoryConf );
51
52 self::$instance = new $class( $wgLBFactoryConf );
53 }
54
55 return self::$instance;
56 }
57
58 /**
59 * Returns the LBFactory class to use and the load balancer configuration.
60 *
61 * @param array $config (e.g. $wgLBFactoryConf)
62 * @return string Class name
63 */
64 public static function getLBFactoryClass( array $config ) {
65 // For configuration backward compatibility after removing
66 // underscores from class names in MediaWiki 1.23.
67 $bcClasses = array(
68 'LBFactory_Simple' => 'LBFactorySimple',
69 'LBFactory_Single' => 'LBFactorySingle',
70 'LBFactory_Multi' => 'LBFactoryMulti',
71 'LBFactory_Fake' => 'LBFactoryFake',
72 );
73
74 $class = $config['class'];
75
76 if ( isset( $bcClasses[$class] ) ) {
77 $class = $bcClasses[$class];
78 wfDeprecated(
79 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
80 '1.23'
81 );
82 }
83
84 return $class;
85 }
86
87 /**
88 * Shut down, close connections and destroy the cached instance.
89 */
90 public static function destroyInstance() {
91 if ( self::$instance ) {
92 self::$instance->shutdown();
93 self::$instance->forEachLBCallMethod( 'closeAll' );
94 self::$instance = null;
95 }
96 }
97
98 /**
99 * Set the instance to be the given object
100 *
101 * @param LBFactory $instance
102 */
103 public static function setInstance( $instance ) {
104 self::destroyInstance();
105 self::$instance = $instance;
106 }
107
108 /**
109 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
110 * @param array $conf
111 */
112 abstract public function __construct( array $conf );
113
114 /**
115 * Create a new load balancer object. The resulting object will be untracked,
116 * not chronology-protected, and the caller is responsible for cleaning it up.
117 *
118 * @param bool|string $wiki Wiki ID, or false for the current wiki
119 * @return LoadBalancer
120 */
121 abstract public function newMainLB( $wiki = false );
122
123 /**
124 * Get a cached (tracked) load balancer object.
125 *
126 * @param bool|string $wiki Wiki ID, or false for the current wiki
127 * @return LoadBalancer
128 */
129 abstract public function getMainLB( $wiki = false );
130
131 /**
132 * Create a new load balancer for external storage. The resulting object will be
133 * untracked, not chronology-protected, and the caller is responsible for
134 * cleaning it up.
135 *
136 * @param string $cluster External storage cluster, or false for core
137 * @param bool|string $wiki Wiki ID, or false for the current wiki
138 * @return LoadBalancer
139 */
140 abstract protected function newExternalLB( $cluster, $wiki = false );
141
142 /**
143 * Get a cached (tracked) load balancer for external storage
144 *
145 * @param string $cluster External storage cluster, or false for core
146 * @param bool|string $wiki Wiki ID, or false for the current wiki
147 * @return LoadBalancer
148 */
149 abstract public function &getExternalLB( $cluster, $wiki = false );
150
151 /**
152 * Execute a function for each tracked load balancer
153 * The callback is called with the load balancer as the first parameter,
154 * and $params passed as the subsequent parameters.
155 *
156 * @param callable $callback
157 * @param array $params
158 */
159 abstract public function forEachLB( $callback, array $params = array() );
160
161 /**
162 * Prepare all tracked load balancers for shutdown
163 * STUB
164 */
165 public function shutdown() {
166 }
167
168 /**
169 * Call a method of each tracked load balancer
170 *
171 * @param string $methodName
172 * @param array $args
173 */
174 private function forEachLBCallMethod( $methodName, array $args = array() ) {
175 $this->forEachLB( function ( LoadBalancer $loadBalancer, $methodName, array $args ) {
176 call_user_func_array( array( $loadBalancer, $methodName ), $args );
177 }, array( $methodName, $args ) );
178 }
179
180 /**
181 * Commit on all connections. Done for two reasons:
182 * 1. To commit changes to the masters.
183 * 2. To release the snapshot on all connections, master and slave.
184 */
185 public function commitAll() {
186 $this->forEachLBCallMethod( 'commitAll' );
187 }
188
189 /**
190 * Commit changes on all master connections
191 */
192 public function commitMasterChanges() {
193 $this->forEachLBCallMethod( 'commitMasterChanges' );
194 }
195
196 /**
197 * Rollback changes on all master connections
198 * @since 1.23
199 */
200 public function rollbackMasterChanges() {
201 $this->forEachLBCallMethod( 'rollbackMasterChanges' );
202 }
203
204 /**
205 * Detemine if any master connection has pending changes.
206 * @since 1.23
207 * @return bool
208 */
209 public function hasMasterChanges() {
210 $ret = false;
211 $this->forEachLB( function ( LoadBalancer $lb ) use ( &$ret ) {
212 $ret = $ret || $lb->hasMasterChanges();
213 } );
214 return $ret;
215 }
216 }
217
218 /**
219 * A simple single-master LBFactory that gets its configuration from the b/c globals
220 */
221 class LBFactorySimple extends LBFactory {
222 /** @var LoadBalancer */
223 private $mainLB;
224
225 /** @var LoadBalancer[] */
226 private $extLBs = array();
227
228 /** @var ChronologyProtector */
229 private $chronProt;
230
231 public function __construct( array $conf ) {
232 $this->chronProt = new ChronologyProtector;
233 }
234
235 /**
236 * @param bool|string $wiki
237 * @return LoadBalancer
238 */
239 public function newMainLB( $wiki = false ) {
240 global $wgDBservers;
241 if ( $wgDBservers ) {
242 $servers = $wgDBservers;
243 } else {
244 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
245 global $wgDBssl, $wgDBcompress;
246
247 $flags = DBO_DEFAULT;
248 if ( $wgDebugDumpSql ) {
249 $flags |= DBO_DEBUG;
250 }
251 if ( $wgDBssl ) {
252 $flags |= DBO_SSL;
253 }
254 if ( $wgDBcompress ) {
255 $flags |= DBO_COMPRESS;
256 }
257
258 $servers = array( array(
259 'host' => $wgDBserver,
260 'user' => $wgDBuser,
261 'password' => $wgDBpassword,
262 'dbname' => $wgDBname,
263 'type' => $wgDBtype,
264 'load' => 1,
265 'flags' => $flags
266 ) );
267 }
268
269 return new LoadBalancer( array(
270 'servers' => $servers,
271 ) );
272 }
273
274 /**
275 * @param bool|string $wiki
276 * @return LoadBalancer
277 */
278 public function getMainLB( $wiki = false ) {
279 if ( !isset( $this->mainLB ) ) {
280 $this->mainLB = $this->newMainLB( $wiki );
281 $this->mainLB->parentInfo( array( 'id' => 'main' ) );
282 $this->chronProt->initLB( $this->mainLB );
283 }
284
285 return $this->mainLB;
286 }
287
288 /**
289 * @throws MWException
290 * @param string $cluster
291 * @param bool|string $wiki
292 * @return LoadBalancer
293 */
294 protected function newExternalLB( $cluster, $wiki = false ) {
295 global $wgExternalServers;
296 if ( !isset( $wgExternalServers[$cluster] ) ) {
297 throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
298 }
299
300 return new LoadBalancer( array(
301 'servers' => $wgExternalServers[$cluster]
302 ) );
303 }
304
305 /**
306 * @param string $cluster
307 * @param bool|string $wiki
308 * @return array
309 */
310 public function &getExternalLB( $cluster, $wiki = false ) {
311 if ( !isset( $this->extLBs[$cluster] ) ) {
312 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
313 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
314 $this->chronProt->initLB( $this->extLBs[$cluster] );
315 }
316
317 return $this->extLBs[$cluster];
318 }
319
320 /**
321 * Execute a function for each tracked load balancer
322 * The callback is called with the load balancer as the first parameter,
323 * and $params passed as the subsequent parameters.
324 *
325 * @param callable $callback
326 * @param array $params
327 */
328 public function forEachLB( $callback, array $params = array() ) {
329 if ( isset( $this->mainLB ) ) {
330 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
331 }
332 foreach ( $this->extLBs as $lb ) {
333 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
334 }
335 }
336
337 public function shutdown() {
338 if ( $this->mainLB ) {
339 $this->chronProt->shutdownLB( $this->mainLB );
340 }
341 foreach ( $this->extLBs as $extLB ) {
342 $this->chronProt->shutdownLB( $extLB );
343 }
344 $this->chronProt->shutdown();
345 $this->commitMasterChanges();
346 }
347 }
348
349 /**
350 * LBFactory class that throws an error on any attempt to use it.
351 * This will typically be done via wfGetDB().
352 * Call LBFactory::disableBackend() to start using this, and
353 * LBFactory::enableBackend() to return to normal behavior
354 */
355 class LBFactoryFake extends LBFactory {
356 public function __construct( array $conf ) {
357 }
358
359 public function newMainLB( $wiki = false ) {
360 throw new DBAccessError;
361 }
362
363 public function getMainLB( $wiki = false ) {
364 throw new DBAccessError;
365 }
366
367 protected function newExternalLB( $cluster, $wiki = false ) {
368 throw new DBAccessError;
369 }
370
371 public function &getExternalLB( $cluster, $wiki = false ) {
372 throw new DBAccessError;
373 }
374
375 public function forEachLB( $callback, array $params = array() ) {
376 }
377 }
378
379 /**
380 * Exception class for attempted DB access
381 */
382 class DBAccessError extends MWException {
383 public function __construct() {
384 parent::__construct( "Mediawiki tried to access the database via wfGetDB(). " .
385 "This is not allowed." );
386 }
387 }