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