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
25 * An interface for generating database load balancers
28 abstract class LBFactory
{
30 private static $instance;
33 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
36 public function __construct( array $conf ) {
40 * Disables all access to the load balancer, will cause all database access
41 * to throw a DBAccessError
43 public static function disableBackend() {
44 global $wgLBFactoryConf;
45 self
::$instance = new LBFactoryFake( $wgLBFactoryConf );
49 * Get an LBFactory instance
53 public static function singleton() {
54 global $wgLBFactoryConf;
56 if ( is_null( self
::$instance ) ) {
57 $class = self
::getLBFactoryClass( $wgLBFactoryConf );
59 self
::$instance = new $class( $wgLBFactoryConf );
62 return self
::$instance;
66 * Returns the LBFactory class to use and the load balancer configuration.
68 * @param array $config (e.g. $wgLBFactoryConf)
69 * @return string Class name
71 public static function getLBFactoryClass( array $config ) {
72 // For configuration backward compatibility after removing
73 // underscores from class names in MediaWiki 1.23.
75 'LBFactory_Simple' => 'LBFactorySimple',
76 'LBFactory_Single' => 'LBFactorySingle',
77 'LBFactory_Multi' => 'LBFactoryMulti',
78 'LBFactory_Fake' => 'LBFactoryFake',
81 $class = $config['class'];
83 if ( isset( $bcClasses[$class] ) ) {
84 $class = $bcClasses[$class];
86 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
95 * Shut down, close connections and destroy the cached instance.
97 public static function destroyInstance() {
98 if ( self
::$instance ) {
99 self
::$instance->shutdown();
100 self
::$instance->forEachLBCallMethod( 'closeAll' );
101 self
::$instance = null;
106 * Set the instance to be the given object
108 * @param LBFactory $instance
110 public static function setInstance( $instance ) {
111 self
::destroyInstance();
112 self
::$instance = $instance;
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.
119 * @param bool|string $wiki Wiki ID, or false for the current wiki
120 * @return LoadBalancer
122 abstract public function newMainLB( $wiki = false );
125 * Get a cached (tracked) load balancer object.
127 * @param bool|string $wiki Wiki ID, or false for the current wiki
128 * @return LoadBalancer
130 abstract public function getMainLB( $wiki = false );
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
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
141 abstract protected function newExternalLB( $cluster, $wiki = false );
144 * Get a cached (tracked) load balancer for external storage
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
150 abstract public function &getExternalLB( $cluster, $wiki = false );
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.
157 * @param callable $callback
158 * @param array $params
160 abstract public function forEachLB( $callback, array $params = array() );
163 * Prepare all tracked load balancers for shutdown
166 public function shutdown() {
170 * Call a method of each tracked load balancer
172 * @param string $methodName
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 ) );
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.
186 public function commitAll() {
187 $this->forEachLBCallMethod( 'commitAll' );
191 * Commit changes on all master connections
193 public function commitMasterChanges() {
194 $this->forEachLBCallMethod( 'commitMasterChanges' );
198 * Rollback changes on all master connections
201 public function rollbackMasterChanges() {
202 $this->forEachLBCallMethod( 'rollbackMasterChanges' );
206 * Determine if any master connection has pending changes
210 public function hasMasterChanges() {
212 $this->forEachLB( function ( LoadBalancer
$lb ) use ( &$ret ) {
213 $ret = $ret ||
$lb->hasMasterChanges();
220 * Detemine if any lagged slave connection was used
224 public function laggedSlaveUsed() {
226 $this->forEachLB( function ( LoadBalancer
$lb ) use ( &$ret ) {
227 $ret = $ret ||
$lb->laggedSlaveUsed();
234 * Determine if any master connection has pending/written changes from this request
238 public function hasOrMadeRecentMasterChanges() {
240 $this->forEachLB( function ( LoadBalancer
$lb ) use ( &$ret ) {
241 $ret = $ret ||
$lb->hasOrMadeRecentMasterChanges();
248 * Exception class for attempted DB access
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." );