Merge "Fix the Rubocop offense AmbiguousRegexpLiteral"
[mediawiki.git] / includes / db / LBFactory.php
blob4551e2d760a5835d16ce86257034681f2ae3f899
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 {
29 /** @var LBFactory */
30 private static $instance;
32 /**
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 );
41 /**
42 * Get an LBFactory instance
44 * @return LBFactory
46 public 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;
58 /**
59 * Returns the LBFactory class to use and the load balancer configuration.
61 * @param array $config (e.g. $wgLBFactoryConf)
62 * @return string Class name
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',
74 $class = $config['class'];
76 if ( isset( $bcClasses[$class] ) ) {
77 $class = $bcClasses[$class];
78 wfDeprecated(
79 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
80 '1.23'
84 return $class;
87 /**
88 * Shut down, close connections and destroy the cached instance.
90 public static function destroyInstance() {
91 if ( self::$instance ) {
92 self::$instance->shutdown();
93 self::$instance->forEachLBCallMethod( 'closeAll' );
94 self::$instance = null;
98 /**
99 * Set the instance to be the given object
101 * @param LBFactory $instance
103 public static function setInstance( $instance ) {
104 self::destroyInstance();
105 self::$instance = $instance;
109 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
110 * @param array $conf
112 abstract public function __construct( array $conf );
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.
118 * @param bool|string $wiki Wiki ID, or false for the current wiki
119 * @return LoadBalancer
121 abstract public function newMainLB( $wiki = false );
124 * Get a cached (tracked) load balancer object.
126 * @param bool|string $wiki Wiki ID, or false for the current wiki
127 * @return LoadBalancer
129 abstract public function getMainLB( $wiki = false );
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.
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
140 abstract protected function newExternalLB( $cluster, $wiki = false );
143 * Get a cached (tracked) load balancer for external storage
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
149 abstract public function &getExternalLB( $cluster, $wiki = false );
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.
156 * @param callable $callback
157 * @param array $params
159 abstract public function forEachLB( $callback, array $params = array() );
162 * Prepare all tracked load balancers for shutdown
163 * STUB
165 public function shutdown() {
169 * Call a method of each tracked load balancer
171 * @param string $methodName
172 * @param array $args
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 ) );
181 * Commit changes on all master connections
183 public function commitMasterChanges() {
184 $this->forEachLBCallMethod( 'commitMasterChanges' );
188 * Rollback changes on all master connections
189 * @since 1.23
191 public function rollbackMasterChanges() {
192 $this->forEachLBCallMethod( 'rollbackMasterChanges' );
196 * Detemine if any master connection has pending changes.
197 * @since 1.23
198 * @return bool
200 public function hasMasterChanges() {
201 $ret = false;
202 $this->forEachLB( function ( $lb ) use ( &$ret ) {
203 $ret = $ret || $lb->hasMasterChanges();
204 } );
205 return $ret;
210 * A simple single-master LBFactory that gets its configuration from the b/c globals
212 class LBFactorySimple extends LBFactory {
213 /** @var LoadBalancer */
214 private $mainLB;
216 /** @var LoadBalancer[] */
217 private $extLBs = array();
219 /** @var ChronologyProtector */
220 private $chronProt;
222 public function __construct( array $conf ) {
223 $this->chronProt = new ChronologyProtector;
227 * @param bool|string $wiki
228 * @return LoadBalancer
230 public function newMainLB( $wiki = false ) {
231 global $wgDBservers;
232 if ( $wgDBservers ) {
233 $servers = $wgDBservers;
234 } else {
235 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
236 global $wgDBssl, $wgDBcompress;
238 $flags = DBO_DEFAULT;
239 if ( $wgDebugDumpSql ) {
240 $flags |= DBO_DEBUG;
242 if ( $wgDBssl ) {
243 $flags |= DBO_SSL;
245 if ( $wgDBcompress ) {
246 $flags |= DBO_COMPRESS;
249 $servers = array( array(
250 'host' => $wgDBserver,
251 'user' => $wgDBuser,
252 'password' => $wgDBpassword,
253 'dbname' => $wgDBname,
254 'type' => $wgDBtype,
255 'load' => 1,
256 'flags' => $flags
257 ) );
260 return new LoadBalancer( array(
261 'servers' => $servers,
262 ) );
266 * @param bool|string $wiki
267 * @return LoadBalancer
269 public function getMainLB( $wiki = false ) {
270 if ( !isset( $this->mainLB ) ) {
271 $this->mainLB = $this->newMainLB( $wiki );
272 $this->mainLB->parentInfo( array( 'id' => 'main' ) );
273 $this->chronProt->initLB( $this->mainLB );
276 return $this->mainLB;
280 * @throws MWException
281 * @param string $cluster
282 * @param bool|string $wiki
283 * @return LoadBalancer
285 protected function newExternalLB( $cluster, $wiki = false ) {
286 global $wgExternalServers;
287 if ( !isset( $wgExternalServers[$cluster] ) ) {
288 throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
291 return new LoadBalancer( array(
292 'servers' => $wgExternalServers[$cluster]
293 ) );
297 * @param string $cluster
298 * @param bool|string $wiki
299 * @return array
301 public function &getExternalLB( $cluster, $wiki = false ) {
302 if ( !isset( $this->extLBs[$cluster] ) ) {
303 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
304 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
305 $this->chronProt->initLB( $this->extLBs[$cluster] );
308 return $this->extLBs[$cluster];
312 * Execute a function for each tracked load balancer
313 * The callback is called with the load balancer as the first parameter,
314 * and $params passed as the subsequent parameters.
316 * @param callable $callback
317 * @param array $params
319 public function forEachLB( $callback, array $params = array() ) {
320 if ( isset( $this->mainLB ) ) {
321 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
323 foreach ( $this->extLBs as $lb ) {
324 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
328 public function shutdown() {
329 if ( $this->mainLB ) {
330 $this->chronProt->shutdownLB( $this->mainLB );
332 foreach ( $this->extLBs as $extLB ) {
333 $this->chronProt->shutdownLB( $extLB );
335 $this->chronProt->shutdown();
336 $this->commitMasterChanges();
341 * LBFactory class that throws an error on any attempt to use it.
342 * This will typically be done via wfGetDB().
343 * Call LBFactory::disableBackend() to start using this, and
344 * LBFactory::enableBackend() to return to normal behavior
346 class LBFactoryFake extends LBFactory {
347 public function __construct( array $conf ) {
350 public function newMainLB( $wiki = false ) {
351 throw new DBAccessError;
354 public function getMainLB( $wiki = false ) {
355 throw new DBAccessError;
358 protected function newExternalLB( $cluster, $wiki = false ) {
359 throw new DBAccessError;
362 public function &getExternalLB( $cluster, $wiki = false ) {
363 throw new DBAccessError;
366 public function forEachLB( $callback, array $params = array() ) {
371 * Exception class for attempted DB access
373 class DBAccessError extends MWException {
374 public function __construct() {
375 parent::__construct( "Mediawiki tried to access the database via wfGetDB(). " .
376 "This is not allowed." );