*/
abstract class LBFactory {
/** @var LBFactory */
- protected static $instance;
+ private static $instance;
/**
* Disables all access to the load balancer, will cause all database access
*
* @return LBFactory
*/
- static function &singleton() {
+ public static function &singleton() {
global $wgLBFactoryConf;
if ( is_null( self::$instance ) ) {
/**
* Shut down, close connections and destroy the cached instance.
*/
- static function destroyInstance() {
+ public static function destroyInstance() {
if ( self::$instance ) {
self::$instance->shutdown();
self::$instance->forEachLBCallMethod( 'closeAll' );
*
* @param LBFactory $instance
*/
- static function setInstance( $instance ) {
+ public static function setInstance( $instance ) {
self::destroyInstance();
self::$instance = $instance;
}
* Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
* @param array $conf
*/
- abstract function __construct( array $conf );
+ public abstract function __construct( array $conf );
/**
* Create a new load balancer object. The resulting object will be untracked,
* @param bool|string $wiki Wiki ID, or false for the current wiki
* @return LoadBalancer
*/
- abstract function newMainLB( $wiki = false );
+ public abstract function newMainLB( $wiki = false );
/**
* Get a cached (tracked) load balancer object.
* @param bool|string $wiki Wiki ID, or false for the current wiki
* @return LoadBalancer
*/
- abstract function getMainLB( $wiki = false );
+ public abstract function getMainLB( $wiki = false );
/**
* Create a new load balancer for external storage. The resulting object will be
* @param bool|string $wiki Wiki ID, or false for the current wiki
* @return LoadBalancer
*/
- abstract function newExternalLB( $cluster, $wiki = false );
+ protected abstract function newExternalLB( $cluster, $wiki = false );
/**
* Get a cached (tracked) load balancer for external storage
* @param bool|string $wiki Wiki ID, or false for the current wiki
* @return LoadBalancer
*/
- abstract function &getExternalLB( $cluster, $wiki = false );
+ public abstract function &getExternalLB( $cluster, $wiki = false );
/**
* Execute a function for each tracked load balancer
* @param callable $callback
* @param array $params
*/
- abstract function forEachLB( $callback, array $params = array() );
+ public abstract function forEachLB( $callback, array $params = array() );
/**
* Prepare all tracked load balancers for shutdown
* STUB
*/
- function shutdown() {
+ public function shutdown() {
}
/**
* @param string $methodName
* @param array $args
*/
- function forEachLBCallMethod( $methodName, array $args = array() ) {
- $this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) );
- }
-
- /**
- * Private helper for forEachLBCallMethod
- * @param LoadBalancer $loadBalancer
- * @param string $methodName
- * @param array $args
- */
- function callMethod( $loadBalancer, $methodName, $args ) {
- call_user_func_array( array( $loadBalancer, $methodName ), $args );
+ private function forEachLBCallMethod( $methodName, array $args = array() ) {
+ $this->forEachLB( function( LoadBalancer $loadBalancer, $methodName, array $args ) {
+ call_user_func_array( array( $loadBalancer, $methodName ), $args );
+ }, array( $methodName, $args ) );
}
/**
* Commit changes on all master connections
*/
- function commitMasterChanges() {
+ public function commitMasterChanges() {
$this->forEachLBCallMethod( 'commitMasterChanges' );
}
* Rollback changes on all master connections
* @since 1.23
*/
- function rollbackMasterChanges() {
+ public function rollbackMasterChanges() {
$this->forEachLBCallMethod( 'rollbackMasterChanges' );
}
* @since 1.23
* @return bool
*/
- function hasMasterChanges() {
+ public function hasMasterChanges() {
$ret = false;
$this->forEachLB( function ( $lb ) use ( &$ret ) {
$ret = $ret || $lb->hasMasterChanges();
*/
class LBFactorySimple extends LBFactory {
/** @var LoadBalancer */
- protected $mainLB;
+ private $mainLB;
/** @var LoadBalancer[] */
- protected $extLBs = array();
+ private $extLBs = array();
/** @var ChronologyProtector */
- protected $chronProt;
+ private $chronProt;
- function __construct( array $conf ) {
+ public function __construct( array $conf ) {
$this->chronProt = new ChronologyProtector;
}
* @param bool|string $wiki
* @return LoadBalancer
*/
- function newMainLB( $wiki = false ) {
+ public function newMainLB( $wiki = false ) {
global $wgDBservers;
if ( $wgDBservers ) {
$servers = $wgDBservers;
* @param bool|string $wiki
* @return LoadBalancer
*/
- function getMainLB( $wiki = false ) {
+ public function getMainLB( $wiki = false ) {
if ( !isset( $this->mainLB ) ) {
$this->mainLB = $this->newMainLB( $wiki );
$this->mainLB->parentInfo( array( 'id' => 'main' ) );
* @param bool|string $wiki
* @return LoadBalancer
*/
- function newExternalLB( $cluster, $wiki = false ) {
+ protected function newExternalLB( $cluster, $wiki = false ) {
global $wgExternalServers;
if ( !isset( $wgExternalServers[$cluster] ) ) {
throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
* @param bool|string $wiki
* @return array
*/
- function &getExternalLB( $cluster, $wiki = false ) {
+ public function &getExternalLB( $cluster, $wiki = false ) {
if ( !isset( $this->extLBs[$cluster] ) ) {
$this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
$this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
* @param callable $callback
* @param array $params
*/
- function forEachLB( $callback, array $params = array() ) {
+ public function forEachLB( $callback, array $params = array() ) {
if ( isset( $this->mainLB ) ) {
call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
}
}
}
- function shutdown() {
+ public function shutdown() {
if ( $this->mainLB ) {
$this->chronProt->shutdownLB( $this->mainLB );
}
* LBFactory::enableBackend() to return to normal behavior
*/
class LBFactoryFake extends LBFactory {
- function __construct( array $conf ) {
+ public function __construct( array $conf ) {
}
- function newMainLB( $wiki = false ) {
+ public function newMainLB( $wiki = false ) {
throw new DBAccessError;
}
- function getMainLB( $wiki = false ) {
+ public function getMainLB( $wiki = false ) {
throw new DBAccessError;
}
- function newExternalLB( $cluster, $wiki = false ) {
+ protected function newExternalLB( $cluster, $wiki = false ) {
throw new DBAccessError;
}
- function &getExternalLB( $cluster, $wiki = false ) {
+ public function &getExternalLB( $cluster, $wiki = false ) {
throw new DBAccessError;
}
- function forEachLB( $callback, array $params = array() ) {
+ public function forEachLB( $callback, array $params = array() ) {
}
}
* Exception class for attempted DB access
*/
class DBAccessError extends MWException {
- function __construct() {
+ public function __construct() {
parent::__construct( "Mediawiki tried to access the database via wfGetDB(). " .
"This is not allowed." );
}
// Required settings
/** @var array A map of database names to section names */
- protected $sectionsByDB;
+ private $sectionsByDB;
/**
* @var array A 2-d map. For each section, gives a map of server names to
* load ratios
*/
- protected $sectionLoads;
+ private $sectionLoads;
/**
* @var array A server info associative array as documented for
* $wgDBservers. The host, hostName and load entries will be
* overridden
*/
- protected $serverTemplate;
+ private $serverTemplate;
// Optional settings
/** @var array A 3-d map giving server load ratios for each section and group */
- protected $groupLoadsBySection = array();
+ private $groupLoadsBySection = array();
/** @var array A 3-d map giving server load ratios by DB name */
- protected $groupLoadsByDB = array();
+ private $groupLoadsByDB = array();
/** @var array A map of hostname to IP address */
- protected $hostsByName = array();
+ private $hostsByName = array();
/** @var array A map of external storage cluster name to server load map */
- protected $externalLoads = array();
+ private $externalLoads = array();
/**
* @var array A set of server info keys overriding serverTemplate for
* external storage
*/
- protected $externalTemplateOverrides;
+ private $externalTemplateOverrides;
/**
* @var array A 2-d map overriding serverTemplate and
* externalTemplateOverrides on a server-by-server basis. Applies to both
* core and external storage
*/
- protected $templateOverridesByServer;
+ private $templateOverridesByServer;
/** @var array A 2-d map overriding the server info by external storage cluster */
- protected $templateOverridesByCluster;
+ private $templateOverridesByCluster;
/** @var array An override array for all master servers */
- protected $masterTemplateOverrides;
+ private $masterTemplateOverrides;
/**
* @var array|bool A map of section name to read-only message. Missing or
* false for read/write
*/
- protected $readOnlyBySection = array();
+ private $readOnlyBySection = array();
// Other stuff
/** @var array Load balancer factory configuration */
- protected $conf;
+ private $conf;
/** @var LoadBalancer[] */
- protected $mainLBs = array();
+ private $mainLBs = array();
/** @var LoadBalancer[] */
- protected $extLBs = array();
+ private $extLBs = array();
/** @var string */
- protected $lastWiki;
+ private $lastWiki;
/** @var string */
- protected $lastSection;
+ private $lastSection;
/**
* @param array $conf
* @throws MWException
*/
- function __construct( array $conf ) {
+ public function __construct( array $conf ) {
$this->chronProt = new ChronologyProtector;
$this->conf = $conf;
$required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' );
* @param bool|string $wiki
* @return string
*/
- function getSectionForWiki( $wiki = false ) {
+ private function getSectionForWiki( $wiki = false ) {
if ( $this->lastWiki === $wiki ) {
return $this->lastSection;
}
* @param bool|string $wiki
* @return LoadBalancer
*/
- function newMainLB( $wiki = false ) {
+ public function newMainLB( $wiki = false ) {
list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
$section = $this->getSectionForWiki( $wiki );
$groupLoads = array();
* @param bool|string $wiki
* @return LoadBalancer
*/
- function getMainLB( $wiki = false ) {
+ public function getMainLB( $wiki = false ) {
$section = $this->getSectionForWiki( $wiki );
if ( !isset( $this->mainLBs[$section] ) ) {
$lb = $this->newMainLB( $wiki, $section );
* @throws MWException
* @return LoadBalancer
*/
- function newExternalLB( $cluster, $wiki = false ) {
+ protected function newExternalLB( $cluster, $wiki = false ) {
if ( !isset( $this->externalLoads[$cluster] ) ) {
throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
}
* @param bool|string $wiki Wiki ID, or false for the current wiki
* @return LoadBalancer
*/
- function &getExternalLB( $cluster, $wiki = false ) {
+ public function &getExternalLB( $cluster, $wiki = false ) {
if ( !isset( $this->extLBs[$cluster] ) ) {
$this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
$this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
* @param array $groupLoads
* @return LoadBalancer
*/
- function newLoadBalancer( $template, $loads, $groupLoads ) {
+ private function newLoadBalancer( $template, $loads, $groupLoads ) {
$servers = $this->makeServerArray( $template, $loads, $groupLoads );
$lb = new LoadBalancer( array(
'servers' => $servers,
* @param array $groupLoads
* @return array
*/
- function makeServerArray( $template, $loads, $groupLoads ) {
+ private function makeServerArray( $template, $loads, $groupLoads ) {
$servers = array();
$master = true;
$groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
* @param array $groupLoads
* @return array
*/
- function reindexGroupLoads( $groupLoads ) {
+ private function reindexGroupLoads( $groupLoads ) {
$reindexed = array();
foreach ( $groupLoads as $group => $loads ) {
foreach ( $loads as $server => $load ) {
* @param bool|string $wiki
* @return array
*/
- function getDBNameAndPrefix( $wiki = false ) {
+ private function getDBNameAndPrefix( $wiki = false ) {
if ( $wiki === false ) {
global $wgDBname, $wgDBprefix;
* @param callable $callback
* @param array $params
*/
- function forEachLB( $callback, array $params = array() ) {
+ public function forEachLB( $callback, array $params = array() ) {
foreach ( $this->mainLBs as $lb ) {
call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
}
}
}
- function shutdown() {
+ public function shutdown() {
foreach ( $this->mainLBs as $lb ) {
$this->chronProt->shutdownLB( $lb );
}
*/
class LBFactorySingle extends LBFactory {
/** @var LoadBalancerSingle */
- protected $lb;
+ private $lb;
/**
* @param array $conf An associative array with one member:
* - connection: The DatabaseBase connection object
*/
- function __construct( array $conf ) {
+ public function __construct( array $conf ) {
$this->lb = new LoadBalancerSingle( $conf );
}
* @param bool|string $wiki
* @return LoadBalancerSingle
*/
- function newMainLB( $wiki = false ) {
+ public function newMainLB( $wiki = false ) {
return $this->lb;
}
* @param bool|string $wiki
* @return LoadBalancerSingle
*/
- function getMainLB( $wiki = false ) {
+ public function getMainLB( $wiki = false ) {
return $this->lb;
}
* @param bool|string $wiki Wiki ID, or false for the current wiki
* @return LoadBalancerSingle
*/
- function newExternalLB( $cluster, $wiki = false ) {
+ protected function newExternalLB( $cluster, $wiki = false ) {
return $this->lb;
}
* @param bool|string $wiki Wiki ID, or false for the current wiki
* @return LoadBalancerSingle
*/
- function &getExternalLB( $cluster, $wiki = false ) {
+ public function &getExternalLB( $cluster, $wiki = false ) {
return $this->lb;
}
* @param string|callable $callback
* @param array $params
*/
- function forEachLB( $callback, array $params = array() ) {
+ public function forEachLB( $callback, array $params = array() ) {
call_user_func_array( $callback, array_merge( array( $this->lb ), $params ) );
}
}
*/
class LoadBalancerSingle extends LoadBalancer {
/** @var DatabaseBase */
- protected $db;
+ private $db;
/**
* @param array $params
*/
- function __construct( array $params ) {
+ public function __construct( array $params ) {
$this->db = $params['connection'];
parent::__construct( array( 'servers' => array( array(
'type' => $this->db->getType(),
*
* @return DatabaseBase
*/
- function reallyOpenConnection( $server, $dbNameOverride = false ) {
+ protected function reallyOpenConnection( $server, $dbNameOverride = false ) {
return $this->db;
}
}
* loadMonitor Name of a class used to fetch server lag and load.
* @throws MWException
*/
- function __construct( array $params ) {
+ public function __construct( array $params ) {
if ( !isset( $params['servers'] ) ) {
throw new MWException( __CLASS__ . ': missing servers parameter' );
}
*
* @return LoadMonitor
*/
- function getLoadMonitor() {
+ private function getLoadMonitor() {
if ( !isset( $this->mLoadMonitor ) ) {
$class = $this->mLoadMonitorClass;
$this->mLoadMonitor = new $class( $this );
* @param mixed $x
* @return mixed
*/
- function parentInfo( $x = null ) {
+ public function parentInfo( $x = null ) {
return wfSetVar( $this->mParentInfo, $x );
}
* @param array $weights
* @return bool|int|string
*/
- function pickRandom( array $weights ) {
+ public function pickRandom( array $weights ) {
return ArrayUtils::pickRandom( $weights );
}
* @param bool|string $wiki Wiki to get non-lagged for
* @return bool|int|string
*/
- function getRandomNonLagged( array $loads, $wiki = false ) {
+ private function getRandomNonLagged( array $loads, $wiki = false ) {
# Unset excessively lagged servers
$lags = $this->getLagTimes( $wiki );
foreach ( $lags as $i => $lag ) {
* @throws MWException
* @return bool|int|string
*/
- function getReaderIndex( $group = false, $wiki = false ) {
+ public function getReaderIndex( $group = false, $wiki = false ) {
global $wgReadOnly, $wgDBtype;
# @todo FIXME: For now, only go through all this for mysql databases
* @param int $i
* @return DatabaseBase|bool False on failure
*/
- function getAnyOpenConnection( $i ) {
+ public function getAnyOpenConnection( $i ) {
foreach ( $this->mConns as $conns ) {
if ( !empty( $conns[$i] ) ) {
return reset( $conns[$i] );
*
* @access private
*/
- function openConnection( $i, $wiki = false ) {
+ public function openConnection( $i, $wiki = false ) {
wfProfileIn( __METHOD__ );
if ( $wiki !== false ) {
$conn = $this->openForeignConnection( $i, $wiki );
* @param string $wiki Wiki ID to open
* @return DatabaseBase
*/
- function openForeignConnection( $i, $wiki ) {
+ private function openForeignConnection( $i, $wiki ) {
wfProfileIn( __METHOD__ );
list( $dbName, $prefix ) = wfSplitWikiID( $wiki );
if ( isset( $this->mConns['foreignUsed'][$i][$wiki] ) ) {
* @access private
* @return bool
*/
- function isOpen( $index ) {
+ private function isOpen( $index ) {
if ( !is_integer( $index ) ) {
return false;
}
* @throws MWException
* @return DatabaseBase
*/
- function reallyOpenConnection( $server, $dbNameOverride = false ) {
+ protected function reallyOpenConnection( $server, $dbNameOverride = false ) {
if ( !is_array( $server ) ) {
throw new MWException( 'You must update your load-balancing configuration. ' .
'See DefaultSettings.php entry for $wgDBservers.' );
/**
* @return int
*/
- function getWriterIndex() {
+ private function getWriterIndex() {
return 0;
}
* @param string $i
* @return bool
*/
- function haveIndex( $i ) {
+ public function haveIndex( $i ) {
return array_key_exists( $i, $this->mServers );
}
* @param string $i
* @return bool
*/
- function isNonZeroLoad( $i ) {
+ public function isNonZeroLoad( $i ) {
return array_key_exists( $i, $this->mServers ) && $this->mLoads[$i] != 0;
}
*
* @return int
*/
- function getServerCount() {
+ public function getServerCount() {
return count( $this->mServers );
}
* @param string $i
* @return string
*/
- function getServerName( $i ) {
+ public function getServerName( $i ) {
if ( isset( $this->mServers[$i]['hostName'] ) ) {
return $this->mServers[$i]['hostName'];
} elseif ( isset( $this->mServers[$i]['host'] ) ) {
* @param int $i
* @return array|bool
*/
- function getServerInfo( $i ) {
+ public function getServerInfo( $i ) {
if ( isset( $this->mServers[$i] ) ) {
return $this->mServers[$i];
} else {
* @param int $i
* @param array $serverInfo
*/
- function setServerInfo( $i, array $serverInfo ) {
+ public function setServerInfo( $i, array $serverInfo ) {
$this->mServers[$i] = $serverInfo;
}
* Get the current master position for chronology control purposes
* @return mixed
*/
- function getMasterPos() {
+ public function getMasterPos() {
# If this entire request was served from a slave without opening a connection to the
# master (however unlikely that may be), then we can fetch the position from the slave.
$masterConn = $this->getAnyOpenConnection( 0 );
/**
* Close all open connections
*/
- function closeAll() {
+ public function closeAll() {
foreach ( $this->mConns as $conns2 ) {
foreach ( $conns2 as $conns3 ) {
/** @var DatabaseBase $conn */
* If you use $conn->close() directly, the load balancer won't update its state.
* @param DatabaseBase $conn
*/
- function closeConnection( $conn ) {
+ public function closeConnection( $conn ) {
$done = false;
foreach ( $this->mConns as $i1 => $conns2 ) {
foreach ( $conns2 as $i2 => $conns3 ) {
/**
* Commit transactions on all open connections
*/
- function commitAll() {
+ public function commitAll() {
foreach ( $this->mConns as $conns2 ) {
foreach ( $conns2 as $conns3 ) {
/** @var DatabaseBase[] $conns3 */
/**
* Issue COMMIT only on master, only if queries were done on connection
*/
- function commitMasterChanges() {
+ public function commitMasterChanges() {
// Always 0, but who knows.. :)
$masterIndex = $this->getWriterIndex();
foreach ( $this->mConns as $conns2 ) {
* Issue ROLLBACK only on master, only if queries were done on connection
* @since 1.23
*/
- function rollbackMasterChanges() {
+ public function rollbackMasterChanges() {
// Always 0, but who knows.. :)
$masterIndex = $this->getWriterIndex();
foreach ( $this->mConns as $conns2 ) {
* @return bool Whether a master connection is already open
* @since 1.24
*/
- function hasMasterConnection() {
+ public function hasMasterConnection() {
return $this->isOpen( $this->getWriterIndex() );
}
* @since 1.23
* @return bool
*/
- function hasMasterChanges() {
+ public function hasMasterChanges() {
// Always 0, but who knows.. :)
$masterIndex = $this->getWriterIndex();
foreach ( $this->mConns as $conns2 ) {
* @param mixed $value
* @return mixed
*/
- function waitTimeout( $value = null ) {
+ public function waitTimeout( $value = null ) {
return wfSetVar( $this->mWaitTimeout, $value );
}
/**
* @return bool
*/
- function getLaggedSlaveMode() {
+ public function getLaggedSlaveMode() {
return $this->mLaggedSlaveMode;
}
* @param null|bool $mode
* @return bool
*/
- function allowLagged( $mode = null ) {
+ public function allowLagged( $mode = null ) {
if ( $mode === null ) {
return $this->mAllowLagged;
}
/**
* @return bool
*/
- function pingAll() {
+ public function pingAll() {
$success = true;
foreach ( $this->mConns as $conns2 ) {
foreach ( $conns2 as $conns3 ) {
* @param callable $callback
* @param array $params
*/
- function forEachOpenConnection( $callback, array $params = array() ) {
+ public function forEachOpenConnection( $callback, array $params = array() ) {
foreach ( $this->mConns as $conns2 ) {
foreach ( $conns2 as $conns3 ) {
foreach ( $conns3 as $conn ) {
* @param bool|string $wiki Wiki ID, or false for the default database
* @return array ( host, max lag, index of max lagged host )
*/
- function getMaxLag( $wiki = false ) {
+ public function getMaxLag( $wiki = false ) {
$maxLag = -1;
$host = '';
$maxIndex = 0;
* @param string|bool $wiki
* @return int[] Map of (server index => seconds)
*/
- function getLagTimes( $wiki = false ) {
+ public function getLagTimes( $wiki = false ) {
if ( $this->getServerCount() <= 1 ) {
return array( 0 => 0 ); // no replication = no lag
}
* @param DatabaseBase $conn
* @return int
*/
- function safeGetLag( $conn ) {
+ public function safeGetLag( $conn ) {
if ( $this->getServerCount() == 1 ) {
return 0;
} else {
/**
* Clear the cache for slag lag delay times
*/
- function clearLagTimeCache() {
+ public function clearLagTimeCache() {
$this->mProcCache->clear( 'slave_lag' );
}
}
*/
class DBConnRef implements IDatabase {
/** @var LoadBalancer */
- protected $lb;
+ private $lb;
/** @var DatabaseBase|null */
- protected $conn;
+ private $conn;
/** @var array|null */
- protected $params;
+ private $params;
/**
* @param LoadBalancer $lb
return call_user_func_array( array( $this->conn, $name ), $arguments );
}
- function __destruct() {
+ public function __destruct() {
if ( $this->conn !== null ) {
$this->lb->reuseConnection( $this->conn );
}