bad04f9c1f684f2e5734bcabd00365c20863b9e1
[lhc/web/wiklou.git] / includes / db / loadbalancer / 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 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
34 * @param array $conf
35 */
36 public function __construct( array $conf ) {
37 }
38
39 /**
40 * Disables all access to the load balancer, will cause all database access
41 * to throw a DBAccessError
42 */
43 public static function disableBackend() {
44 global $wgLBFactoryConf;
45 self::$instance = new LBFactoryFake( $wgLBFactoryConf );
46 }
47
48 /**
49 * Get an LBFactory instance
50 *
51 * @return LBFactory
52 */
53 public static function singleton() {
54 global $wgLBFactoryConf;
55
56 if ( is_null( self::$instance ) ) {
57 $class = self::getLBFactoryClass( $wgLBFactoryConf );
58
59 self::$instance = new $class( $wgLBFactoryConf );
60 }
61
62 return self::$instance;
63 }
64
65 /**
66 * Returns the LBFactory class to use and the load balancer configuration.
67 *
68 * @param array $config (e.g. $wgLBFactoryConf)
69 * @return string Class name
70 */
71 public static function getLBFactoryClass( array $config ) {
72 // For configuration backward compatibility after removing
73 // underscores from class names in MediaWiki 1.23.
74 $bcClasses = array(
75 'LBFactory_Simple' => 'LBFactorySimple',
76 'LBFactory_Single' => 'LBFactorySingle',
77 'LBFactory_Multi' => 'LBFactoryMulti',
78 'LBFactory_Fake' => 'LBFactoryFake',
79 );
80
81 $class = $config['class'];
82
83 if ( isset( $bcClasses[$class] ) ) {
84 $class = $bcClasses[$class];
85 wfDeprecated(
86 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
87 '1.23'
88 );
89 }
90
91 return $class;
92 }
93
94 /**
95 * Shut down, close connections and destroy the cached instance.
96 */
97 public static function destroyInstance() {
98 if ( self::$instance ) {
99 self::$instance->shutdown();
100 self::$instance->forEachLBCallMethod( 'closeAll' );
101 self::$instance = null;
102 }
103 }
104
105 /**
106 * Set the instance to be the given object
107 *
108 * @param LBFactory $instance
109 */
110 public static function setInstance( $instance ) {
111 self::destroyInstance();
112 self::$instance = $instance;
113 }
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 bool|string $wiki Wiki ID, or false for the current wiki
120 * @return LoadBalancer
121 */
122 abstract public function newMainLB( $wiki = false );
123
124 /**
125 * Get a cached (tracked) load balancer object.
126 *
127 * @param bool|string $wiki Wiki ID, or false for the current wiki
128 * @return LoadBalancer
129 */
130 abstract public 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 bool|string $wiki Wiki ID, or false for the current wiki
139 * @return LoadBalancer
140 */
141 abstract protected function newExternalLB( $cluster, $wiki = false );
142
143 /**
144 * Get a cached (tracked) load balancer for external storage
145 *
146 * @param string $cluster External storage cluster, or false for core
147 * @param bool|string $wiki Wiki ID, or false for the current wiki
148 * @return LoadBalancer
149 */
150 abstract public 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 *
157 * @param callable $callback
158 * @param array $params
159 */
160 abstract public function forEachLB( $callback, array $params = array() );
161
162 /**
163 * Prepare all tracked load balancers for shutdown
164 * STUB
165 */
166 public function shutdown() {
167 }
168
169 /**
170 * Call a method of each tracked load balancer
171 *
172 * @param string $methodName
173 * @param array $args
174 */
175 private function forEachLBCallMethod( $methodName, array $args = array() ) {
176 $this->forEachLB( function ( LoadBalancer $loadBalancer, $methodName, array $args ) {
177 call_user_func_array( array( $loadBalancer, $methodName ), $args );
178 }, array( $methodName, $args ) );
179 }
180
181 /**
182 * Commit on all connections. Done for two reasons:
183 * 1. To commit changes to the masters.
184 * 2. To release the snapshot on all connections, master and slave.
185 */
186 public function commitAll() {
187 $this->forEachLBCallMethod( 'commitAll' );
188 }
189
190 /**
191 * Commit changes on all master connections
192 */
193 public function commitMasterChanges() {
194 $this->forEachLBCallMethod( 'commitMasterChanges' );
195 }
196
197 /**
198 * Rollback changes on all master connections
199 * @since 1.23
200 */
201 public function rollbackMasterChanges() {
202 $this->forEachLBCallMethod( 'rollbackMasterChanges' );
203 }
204
205 /**
206 * Determine if any master connection has pending changes
207 * @return bool
208 * @since 1.23
209 */
210 public function hasMasterChanges() {
211 $ret = false;
212 $this->forEachLB( function ( LoadBalancer $lb ) use ( &$ret ) {
213 $ret = $ret || $lb->hasMasterChanges();
214 } );
215
216 return $ret;
217 }
218
219 /**
220 * Detemine if any lagged slave connection was used
221 * @since 1.27
222 * @return bool
223 */
224 public function laggedSlaveUsed() {
225 $ret = false;
226 $this->forEachLB( function ( LoadBalancer $lb ) use ( &$ret ) {
227 $ret = $ret || $lb->laggedSlaveUsed();
228 } );
229
230 return $ret;
231 }
232
233 /**
234 * Determine if any master connection has pending/written changes from this request
235 * @return bool
236 * @since 1.27
237 */
238 public function hasOrMadeRecentMasterChanges() {
239 $ret = false;
240 $this->forEachLB( function ( LoadBalancer $lb ) use ( &$ret ) {
241 $ret = $ret || $lb->hasOrMadeRecentMasterChanges();
242 } );
243 return $ret;
244 }
245 }
246
247 /**
248 * Exception class for attempted DB access
249 */
250 class DBAccessError extends MWException {
251 public function __construct() {
252 parent::__construct( "Mediawiki tried to access the database via wfGetDB(). " .
253 "This is not allowed." );
254 }
255 }