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