Merge "Leading semicolon line-bolding made visible in printable version"
[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 protected 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 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 *
63 * @return string class name
64 */
65 public static function getLBFactoryClass( array $config ) {
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 = $config['class'];
76
77 if ( isset( $bcClasses[$class] ) ) {
78 $class = $bcClasses[$class];
79 wfDeprecated(
80 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
81 '1.23'
82 );
83 }
84
85 return $class;
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 /** @var LoadBalancer */
202 protected $mainLB;
203
204 /** @var LoadBalancer[] */
205 protected $extLBs = array();
206
207 /** @var ChronologyProtector */
208 protected $chronProt;
209
210 function __construct( $conf ) {
211 $this->chronProt = new ChronologyProtector;
212 }
213
214 /**
215 * @param $wiki
216 * @return LoadBalancer
217 */
218 function newMainLB( $wiki = false ) {
219 global $wgDBservers, $wgMasterWaitTimeout;
220 if ( $wgDBservers ) {
221 $servers = $wgDBservers;
222 } else {
223 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
224 global $wgDBssl, $wgDBcompress;
225
226 $flags = ( $wgDebugDumpSql ? DBO_DEBUG : 0 ) | DBO_DEFAULT;
227 if ( $wgDBssl ) {
228 $flags |= DBO_SSL;
229 }
230 if ( $wgDBcompress ) {
231 $flags |= DBO_COMPRESS;
232 }
233
234 $servers = array( array(
235 'host' => $wgDBserver,
236 'user' => $wgDBuser,
237 'password' => $wgDBpassword,
238 'dbname' => $wgDBname,
239 'type' => $wgDBtype,
240 'load' => 1,
241 'flags' => $flags
242 ) );
243 }
244
245 return new LoadBalancer( array(
246 'servers' => $servers,
247 'masterWaitTimeout' => $wgMasterWaitTimeout
248 ) );
249 }
250
251 /**
252 * @param $wiki
253 * @return LoadBalancer
254 */
255 function getMainLB( $wiki = false ) {
256 if ( !isset( $this->mainLB ) ) {
257 $this->mainLB = $this->newMainLB( $wiki );
258 $this->mainLB->parentInfo( array( 'id' => 'main' ) );
259 $this->chronProt->initLB( $this->mainLB );
260 }
261
262 return $this->mainLB;
263 }
264
265 /**
266 * @throws MWException
267 * @param $cluster
268 * @param $wiki
269 * @return LoadBalancer
270 */
271 function newExternalLB( $cluster, $wiki = false ) {
272 global $wgExternalServers;
273 if ( !isset( $wgExternalServers[$cluster] ) ) {
274 throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
275 }
276
277 return new LoadBalancer( array(
278 'servers' => $wgExternalServers[$cluster]
279 ) );
280 }
281
282 /**
283 * @param $cluster
284 * @param $wiki
285 * @return array
286 */
287 function &getExternalLB( $cluster, $wiki = false ) {
288 if ( !isset( $this->extLBs[$cluster] ) ) {
289 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
290 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
291 $this->chronProt->initLB( $this->extLBs[$cluster] );
292 }
293
294 return $this->extLBs[$cluster];
295 }
296
297 /**
298 * Execute a function for each tracked load balancer
299 * The callback is called with the load balancer as the first parameter,
300 * and $params passed as the subsequent parameters.
301 * @param $callback
302 * @param $params array
303 */
304 function forEachLB( $callback, $params = array() ) {
305 if ( isset( $this->mainLB ) ) {
306 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
307 }
308 foreach ( $this->extLBs as $lb ) {
309 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
310 }
311 }
312
313 function shutdown() {
314 if ( $this->mainLB ) {
315 $this->chronProt->shutdownLB( $this->mainLB );
316 }
317 foreach ( $this->extLBs as $extLB ) {
318 $this->chronProt->shutdownLB( $extLB );
319 }
320 $this->chronProt->shutdown();
321 $this->commitMasterChanges();
322 }
323 }
324
325 /**
326 * LBFactory class that throws an error on any attempt to use it.
327 * This will typically be done via wfGetDB().
328 * Call LBFactory::disableBackend() to start using this, and
329 * LBFactory::enableBackend() to return to normal behavior
330 */
331 class LBFactoryFake extends LBFactory {
332 function __construct( $conf ) {
333 }
334
335 function newMainLB( $wiki = false ) {
336 throw new DBAccessError;
337 }
338
339 function getMainLB( $wiki = false ) {
340 throw new DBAccessError;
341 }
342
343 function newExternalLB( $cluster, $wiki = false ) {
344 throw new DBAccessError;
345 }
346
347 function &getExternalLB( $cluster, $wiki = false ) {
348 throw new DBAccessError;
349 }
350
351 function forEachLB( $callback, $params = array() ) {
352 }
353 }
354
355 /**
356 * Exception class for attempted DB access
357 */
358 class DBAccessError extends MWException {
359 function __construct() {
360 parent::__construct( "Mediawiki tried to access the database via wfGetDB(). " .
361 "This is not allowed." );
362 }
363 }