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;
32 /** @var string|bool Reason all LBs are read-only or false if not */
33 protected $readOnlyReason = false;
36 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
39 public function __construct( array $conf ) {
40 if ( isset( $conf['readOnlyReason'] ) && is_string( $conf['readOnlyReason'] ) ) {
41 $this->readOnlyReason
= $conf['readOnlyReason'];
46 * Disables all access to the load balancer, will cause all database access
47 * to throw a DBAccessError
49 public static function disableBackend() {
50 global $wgLBFactoryConf;
51 self
::$instance = new LBFactoryFake( $wgLBFactoryConf );
55 * Get an LBFactory instance
59 public static function singleton() {
60 global $wgLBFactoryConf;
62 if ( is_null( self
::$instance ) ) {
63 $class = self
::getLBFactoryClass( $wgLBFactoryConf );
64 $config = $wgLBFactoryConf;
65 if ( !isset( $config['readOnlyReason'] ) ) {
66 $config['readOnlyReason'] = wfConfiguredReadOnlyReason();
68 self
::$instance = new $class( $config );
71 return self
::$instance;
75 * Returns the LBFactory class to use and the load balancer configuration.
77 * @param array $config (e.g. $wgLBFactoryConf)
78 * @return string Class name
80 public static function getLBFactoryClass( array $config ) {
81 // For configuration backward compatibility after removing
82 // underscores from class names in MediaWiki 1.23.
84 'LBFactory_Simple' => 'LBFactorySimple',
85 'LBFactory_Single' => 'LBFactorySingle',
86 'LBFactory_Multi' => 'LBFactoryMulti',
87 'LBFactory_Fake' => 'LBFactoryFake',
90 $class = $config['class'];
92 if ( isset( $bcClasses[$class] ) ) {
93 $class = $bcClasses[$class];
95 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
104 * Shut down, close connections and destroy the cached instance.
106 public static function destroyInstance() {
107 if ( self
::$instance ) {
108 self
::$instance->shutdown();
109 self
::$instance->forEachLBCallMethod( 'closeAll' );
110 self
::$instance = null;
115 * Set the instance to be the given object
117 * @param LBFactory $instance
119 public static function setInstance( $instance ) {
120 self
::destroyInstance();
121 self
::$instance = $instance;
125 * Create a new load balancer object. The resulting object will be untracked,
126 * not chronology-protected, and the caller is responsible for cleaning it up.
128 * @param bool|string $wiki Wiki ID, or false for the current wiki
129 * @return LoadBalancer
131 abstract public function newMainLB( $wiki = false );
134 * Get a cached (tracked) load balancer object.
136 * @param bool|string $wiki Wiki ID, or false for the current wiki
137 * @return LoadBalancer
139 abstract public function getMainLB( $wiki = false );
142 * Create a new load balancer for external storage. The resulting object will be
143 * untracked, not chronology-protected, and the caller is responsible for
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 protected function newExternalLB( $cluster, $wiki = false );
153 * Get a cached (tracked) load balancer for external storage
155 * @param string $cluster External storage cluster, or false for core
156 * @param bool|string $wiki Wiki ID, or false for the current wiki
157 * @return LoadBalancer
159 abstract public function &getExternalLB( $cluster, $wiki = false );
162 * Execute a function for each tracked load balancer
163 * The callback is called with the load balancer as the first parameter,
164 * and $params passed as the subsequent parameters.
166 * @param callable $callback
167 * @param array $params
169 abstract public function forEachLB( $callback, array $params = array() );
172 * Prepare all tracked load balancers for shutdown
175 public function shutdown() {
179 * Call a method of each tracked load balancer
181 * @param string $methodName
184 private function forEachLBCallMethod( $methodName, array $args = array() ) {
185 $this->forEachLB( function ( LoadBalancer
$loadBalancer, $methodName, array $args ) {
186 call_user_func_array( array( $loadBalancer, $methodName ), $args );
187 }, array( $methodName, $args ) );
191 * Commit on all connections. Done for two reasons:
192 * 1. To commit changes to the masters.
193 * 2. To release the snapshot on all connections, master and slave.
195 public function commitAll() {
196 $this->forEachLBCallMethod( 'commitAll' );
200 * Commit changes on all master connections
202 public function commitMasterChanges() {
203 $start = microtime( true );
204 $this->forEachLBCallMethod( 'commitMasterChanges' );
205 $timeMs = 1000 * ( microtime( true ) - $start );
206 RequestContext
::getMain()->getStats()->timing( "db.commit-masters", $timeMs );
210 * Rollback changes on all master connections
213 public function rollbackMasterChanges() {
214 $this->forEachLBCallMethod( 'rollbackMasterChanges' );
218 * Determine if any master connection has pending changes
222 public function hasMasterChanges() {
224 $this->forEachLB( function ( LoadBalancer
$lb ) use ( &$ret ) {
225 $ret = $ret ||
$lb->hasMasterChanges();
232 * Detemine if any lagged slave connection was used
236 public function laggedSlaveUsed() {
238 $this->forEachLB( function ( LoadBalancer
$lb ) use ( &$ret ) {
239 $ret = $ret ||
$lb->laggedSlaveUsed();
246 * Determine if any master connection has pending/written changes from this request
250 public function hasOrMadeRecentMasterChanges() {
252 $this->forEachLB( function ( LoadBalancer
$lb ) use ( &$ret ) {
253 $ret = $ret ||
$lb->hasOrMadeRecentMasterChanges();
260 * Exception class for attempted DB access
262 class DBAccessError
extends MWException
{
263 public function __construct() {
264 parent
::__construct( "Mediawiki tried to access the database via wfGetDB(). " .
265 "This is not allowed." );