2008-03-30 09:48:15 +00:00
|
|
|
<?php
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
/**
|
2012-04-26 08:47:10 +00:00
|
|
|
* Generator of database load balancing objects.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-08 11:55:47 +00:00
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Database
|
|
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* An interface for generating database load balancers
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Database
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
abstract class LBFactory {
|
2011-03-09 17:09:10 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var LBFactory
|
|
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
static $instance;
|
|
|
|
|
|
2010-05-07 12:25:01 +00:00
|
|
|
/**
|
|
|
|
|
* Disables all access to the load balancer, will cause all database access
|
|
|
|
|
* to throw a DBAccessError
|
|
|
|
|
*/
|
|
|
|
|
public static function disableBackend() {
|
|
|
|
|
global $wgLBFactoryConf;
|
|
|
|
|
self::$instance = new LBFactory_Fake( $wgLBFactoryConf );
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Get an LBFactory instance
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @return LBFactory
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
static function &singleton() {
|
|
|
|
|
if ( is_null( self::$instance ) ) {
|
|
|
|
|
global $wgLBFactoryConf;
|
|
|
|
|
$class = $wgLBFactoryConf['class'];
|
|
|
|
|
self::$instance = new $class( $wgLBFactoryConf );
|
|
|
|
|
}
|
|
|
|
|
return self::$instance;
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-30 17:43:51 +00:00
|
|
|
/**
|
2008-07-08 10:41:08 +00:00
|
|
|
* Shut down, close connections and destroy the cached instance.
|
2008-03-30 17:43:51 +00:00
|
|
|
*/
|
2008-07-08 10:41:08 +00:00
|
|
|
static function destroyInstance() {
|
|
|
|
|
if ( self::$instance ) {
|
|
|
|
|
self::$instance->shutdown();
|
|
|
|
|
self::$instance->forEachLBCallMethod( 'closeAll' );
|
|
|
|
|
self::$instance = null;
|
|
|
|
|
}
|
2008-03-30 17:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
* Fixed a bug causing the installer to ignore the "engine" and "charset" settings when installing a MySQL database.
* Fixed a bug causing the engine and charset settings to not be properly preserved when adding new tables on upgrade.
* Fixed total breakage of SQLite upgrade, by reusing the administrative connection to the SQLite database instead of creating a new one when wfGetDB() is called. Added LBFactory_Single to support this.
* Introduced a "schema variable" concept to DatabaseBase to avoid the use of globals for communication between the installer and the Database. Removed a lot of old global variable names from Database::replaceVars(), most were only added on a whim and were never used.
* Introduced DatabaseInstaller::getSchemaVars(), to allow schema variables to be supplied by the DatabaseInstaller child classes.
* Removed messages config-mysql-egine-mismatch [sic] and config-mysql-charset-mismatch. In the old installer it was possible for users to request a certain character set for an upgrade, but in the new installer the question is never asked. So these warnings were shown whenever a non-default character set or engine was used in the old database.
* In MysqlInstaller::preUpgrade(), fixed the incorrect strings used to identify the MySQL character sets: mysql5 instead of utf8 and mysql5-binary instead of binary.
* On install, initialise the site_stats table, using code copied from the old installer. Unlike the old installer, use SiteStats to increment the user count when the initial user is added.
* Fixed several instances of inappropriate call-by-reference.
* Replaced call_user_func_array() with call_user_func() where possible, it is shorter and simpler.
* Moved the caching boilerplate for DatabaseInstaller::getConnection() to the base class, and have the derived classes override an uncached function openConnection() instead. Updates r80892.
* In MysqlInstaller::getLocalSettings(), escape PHP strings correctly with LocalSettingsGenerator::escapePhpString().
* Reduce timeout for checks in dirIsExecutable() to 3 seconds, so that it doesn't take 30s to run when apache is in single-threaded mode for debugging.
* MySQL and SQLite have been tested and they appear to work. PostgreSQL upgrade is totally broken, apparently it was like that before I started. The Oracle code is untested.
2011-01-25 07:37:48 +00:00
|
|
|
/**
|
|
|
|
|
* Set the instance to be the given object
|
2011-05-25 18:58:02 +00:00
|
|
|
*
|
|
|
|
|
* @param $instance LBFactory
|
* Fixed a bug causing the installer to ignore the "engine" and "charset" settings when installing a MySQL database.
* Fixed a bug causing the engine and charset settings to not be properly preserved when adding new tables on upgrade.
* Fixed total breakage of SQLite upgrade, by reusing the administrative connection to the SQLite database instead of creating a new one when wfGetDB() is called. Added LBFactory_Single to support this.
* Introduced a "schema variable" concept to DatabaseBase to avoid the use of globals for communication between the installer and the Database. Removed a lot of old global variable names from Database::replaceVars(), most were only added on a whim and were never used.
* Introduced DatabaseInstaller::getSchemaVars(), to allow schema variables to be supplied by the DatabaseInstaller child classes.
* Removed messages config-mysql-egine-mismatch [sic] and config-mysql-charset-mismatch. In the old installer it was possible for users to request a certain character set for an upgrade, but in the new installer the question is never asked. So these warnings were shown whenever a non-default character set or engine was used in the old database.
* In MysqlInstaller::preUpgrade(), fixed the incorrect strings used to identify the MySQL character sets: mysql5 instead of utf8 and mysql5-binary instead of binary.
* On install, initialise the site_stats table, using code copied from the old installer. Unlike the old installer, use SiteStats to increment the user count when the initial user is added.
* Fixed several instances of inappropriate call-by-reference.
* Replaced call_user_func_array() with call_user_func() where possible, it is shorter and simpler.
* Moved the caching boilerplate for DatabaseInstaller::getConnection() to the base class, and have the derived classes override an uncached function openConnection() instead. Updates r80892.
* In MysqlInstaller::getLocalSettings(), escape PHP strings correctly with LocalSettingsGenerator::escapePhpString().
* Reduce timeout for checks in dirIsExecutable() to 3 seconds, so that it doesn't take 30s to run when apache is in single-threaded mode for debugging.
* MySQL and SQLite have been tested and they appear to work. PostgreSQL upgrade is totally broken, apparently it was like that before I started. The Oracle code is untested.
2011-01-25 07:37:48 +00:00
|
|
|
*/
|
|
|
|
|
static function setInstance( $instance ) {
|
|
|
|
|
self::destroyInstance();
|
|
|
|
|
self::$instance = $instance;
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
2008-04-14 07:45:50 +00:00
|
|
|
* Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
|
2011-11-29 21:04:20 +00:00
|
|
|
* @param $conf
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
abstract function __construct( $conf );
|
|
|
|
|
|
|
|
|
|
/**
|
2010-01-06 03:42:30 +00:00
|
|
|
* Create a new load balancer object. The resulting object will be untracked,
|
2008-07-08 10:41:08 +00:00
|
|
|
* not chronology-protected, and the caller is responsible for cleaning it up.
|
|
|
|
|
*
|
2010-03-11 20:59:25 +00:00
|
|
|
* @param $wiki String: wiki ID, or false for the current wiki
|
2008-07-08 10:41:08 +00:00
|
|
|
* @return LoadBalancer
|
|
|
|
|
*/
|
|
|
|
|
abstract function newMainLB( $wiki = false );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a cached (tracked) load balancer object.
|
2008-03-30 09:48:15 +00:00
|
|
|
*
|
2010-03-11 20:59:25 +00:00
|
|
|
* @param $wiki String: wiki ID, or false for the current wiki
|
2008-03-30 09:48:15 +00:00
|
|
|
* @return LoadBalancer
|
|
|
|
|
*/
|
|
|
|
|
abstract function getMainLB( $wiki = false );
|
|
|
|
|
|
2011-05-21 19:35:16 +00:00
|
|
|
/**
|
2010-01-06 03:42:30 +00:00
|
|
|
* Create a new load balancer for external storage. The resulting object will be
|
|
|
|
|
* untracked, not chronology-protected, and the caller is responsible for
|
2008-07-08 10:41:08 +00:00
|
|
|
* cleaning it up.
|
|
|
|
|
*
|
2010-03-11 20:59:25 +00:00
|
|
|
* @param $cluster String: external storage cluster, or false for core
|
|
|
|
|
* @param $wiki String: wiki ID, or false for the current wiki
|
2011-05-28 18:58:51 +00:00
|
|
|
*
|
|
|
|
|
* @return LoadBalancer
|
2008-07-08 10:41:08 +00:00
|
|
|
*/
|
|
|
|
|
abstract function newExternalLB( $cluster, $wiki = false );
|
|
|
|
|
|
2011-05-21 19:35:16 +00:00
|
|
|
/**
|
2008-07-08 10:41:08 +00:00
|
|
|
* Get a cached (tracked) load balancer for external storage
|
2008-03-30 09:48:15 +00:00
|
|
|
*
|
2010-03-11 20:59:25 +00:00
|
|
|
* @param $cluster String: external storage cluster, or false for core
|
|
|
|
|
* @param $wiki String: wiki ID, or false for the current wiki
|
2011-05-28 18:58:51 +00:00
|
|
|
*
|
|
|
|
|
* @return LoadBalancer
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
2008-03-30 15:16:50 +00:00
|
|
|
abstract function &getExternalLB( $cluster, $wiki = false );
|
2008-03-30 09:48:15 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute a function for each tracked load balancer
|
|
|
|
|
* The callback is called with the load balancer as the first parameter,
|
|
|
|
|
* and $params passed as the subsequent parameters.
|
2011-12-05 16:50:58 +00:00
|
|
|
* @param $callback string|array
|
2011-11-29 21:04:20 +00:00
|
|
|
* @param array $params
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
abstract function forEachLB( $callback, $params = array() );
|
|
|
|
|
|
|
|
|
|
/**
|
2008-07-08 10:41:08 +00:00
|
|
|
* Prepare all tracked load balancers for shutdown
|
2008-03-30 09:48:15 +00:00
|
|
|
* STUB
|
|
|
|
|
*/
|
|
|
|
|
function shutdown() {}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-07-08 10:41:08 +00:00
|
|
|
* Call a method of each tracked load balancer
|
2011-12-05 16:50:58 +00:00
|
|
|
* @param $methodName string
|
2011-11-29 21:04:20 +00:00
|
|
|
* @param $args array
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
function forEachLBCallMethod( $methodName, $args = array() ) {
|
|
|
|
|
$this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Private helper for forEachLBCallMethod
|
2011-11-29 21:04:20 +00:00
|
|
|
* @param $loadBalancer
|
2011-12-05 16:50:58 +00:00
|
|
|
* @param $methodName string
|
2011-11-29 21:04:20 +00:00
|
|
|
* @param $args
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
function callMethod( $loadBalancer, $methodName, $args ) {
|
|
|
|
|
call_user_func_array( array( $loadBalancer, $methodName ), $args );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Commit changes on all master connections
|
|
|
|
|
*/
|
|
|
|
|
function commitMasterChanges() {
|
|
|
|
|
$this->forEachLBCallMethod( 'commitMasterChanges' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A simple single-master LBFactory that gets its configuration from the b/c globals
|
|
|
|
|
*/
|
|
|
|
|
class LBFactory_Simple extends LBFactory {
|
2011-03-09 17:09:10 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var LoadBalancer
|
|
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
var $mainLB;
|
|
|
|
|
var $extLBs = array();
|
|
|
|
|
|
|
|
|
|
# Chronology protector
|
|
|
|
|
var $chronProt;
|
|
|
|
|
|
|
|
|
|
function __construct( $conf ) {
|
|
|
|
|
$this->chronProt = new ChronologyProtector;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-25 18:58:02 +00:00
|
|
|
/**
|
|
|
|
|
* @param $wiki
|
|
|
|
|
* @return LoadBalancer
|
|
|
|
|
*/
|
2008-07-08 10:41:08 +00:00
|
|
|
function newMainLB( $wiki = false ) {
|
|
|
|
|
global $wgDBservers, $wgMasterWaitTimeout;
|
|
|
|
|
if ( $wgDBservers ) {
|
|
|
|
|
$servers = $wgDBservers;
|
|
|
|
|
} else {
|
|
|
|
|
global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
|
|
|
|
|
$servers = array(array(
|
|
|
|
|
'host' => $wgDBserver,
|
|
|
|
|
'user' => $wgDBuser,
|
|
|
|
|
'password' => $wgDBpassword,
|
|
|
|
|
'dbname' => $wgDBname,
|
|
|
|
|
'type' => $wgDBtype,
|
|
|
|
|
'load' => 1,
|
|
|
|
|
'flags' => ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new LoadBalancer( array(
|
2010-01-06 03:42:30 +00:00
|
|
|
'servers' => $servers,
|
|
|
|
|
'masterWaitTimeout' => $wgMasterWaitTimeout
|
2008-07-08 10:41:08 +00:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-25 18:58:02 +00:00
|
|
|
/**
|
|
|
|
|
* @param $wiki
|
|
|
|
|
* @return LoadBalancer
|
|
|
|
|
*/
|
2008-03-30 09:48:15 +00:00
|
|
|
function getMainLB( $wiki = false ) {
|
|
|
|
|
if ( !isset( $this->mainLB ) ) {
|
2008-07-08 10:41:08 +00:00
|
|
|
$this->mainLB = $this->newMainLB( $wiki );
|
2008-03-30 09:48:15 +00:00
|
|
|
$this->mainLB->parentInfo( array( 'id' => 'main' ) );
|
|
|
|
|
$this->chronProt->initLB( $this->mainLB );
|
|
|
|
|
}
|
|
|
|
|
return $this->mainLB;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-25 18:58:02 +00:00
|
|
|
/**
|
|
|
|
|
* @throws MWException
|
|
|
|
|
* @param $cluster
|
|
|
|
|
* @param $wiki
|
|
|
|
|
* @return LoadBalancer
|
|
|
|
|
*/
|
2008-07-08 10:41:08 +00:00
|
|
|
function newExternalLB( $cluster, $wiki = false ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
global $wgExternalServers;
|
2008-07-08 10:41:08 +00:00
|
|
|
if ( !isset( $wgExternalServers[$cluster] ) ) {
|
|
|
|
|
throw new MWException( __METHOD__.": Unknown cluster \"$cluster\"" );
|
|
|
|
|
}
|
|
|
|
|
return new LoadBalancer( array(
|
2010-01-06 03:42:30 +00:00
|
|
|
'servers' => $wgExternalServers[$cluster]
|
2008-07-08 10:41:08 +00:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-25 18:58:02 +00:00
|
|
|
/**
|
|
|
|
|
* @param $cluster
|
|
|
|
|
* @param $wiki
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2008-07-08 10:41:08 +00:00
|
|
|
function &getExternalLB( $cluster, $wiki = false ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( !isset( $this->extLBs[$cluster] ) ) {
|
2008-07-08 10:41:08 +00:00
|
|
|
$this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
|
2008-03-30 09:48:15 +00:00
|
|
|
$this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
|
|
|
|
|
}
|
|
|
|
|
return $this->extLBs[$cluster];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute a function for each tracked load balancer
|
|
|
|
|
* The callback is called with the load balancer as the first parameter,
|
|
|
|
|
* and $params passed as the subsequent parameters.
|
2011-11-29 21:04:20 +00:00
|
|
|
* @param $callback
|
|
|
|
|
* @param $params array
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
function forEachLB( $callback, $params = array() ) {
|
|
|
|
|
if ( isset( $this->mainLB ) ) {
|
|
|
|
|
call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
|
|
|
|
|
}
|
|
|
|
|
foreach ( $this->extLBs as $lb ) {
|
|
|
|
|
call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function shutdown() {
|
|
|
|
|
if ( $this->mainLB ) {
|
|
|
|
|
$this->chronProt->shutdownLB( $this->mainLB );
|
|
|
|
|
}
|
|
|
|
|
$this->chronProt->shutdown();
|
|
|
|
|
$this->commitMasterChanges();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-07 12:25:01 +00:00
|
|
|
/**
|
|
|
|
|
* LBFactory class that throws an error on any attempt to use it.
|
|
|
|
|
* This will typically be done via wfGetDB().
|
2010-05-07 12:47:23 +00:00
|
|
|
* Call LBFactory::disableBackend() to start using this, and
|
|
|
|
|
* LBFactory::enableBackend() to return to normal behavior
|
2010-05-07 12:25:01 +00:00
|
|
|
*/
|
|
|
|
|
class LBFactory_Fake extends LBFactory {
|
|
|
|
|
function __construct( $conf ) {}
|
|
|
|
|
|
|
|
|
|
function newMainLB( $wiki = false) {
|
|
|
|
|
throw new DBAccessError;
|
|
|
|
|
}
|
|
|
|
|
function getMainLB( $wiki = false ) {
|
|
|
|
|
throw new DBAccessError;
|
|
|
|
|
}
|
|
|
|
|
function newExternalLB( $cluster, $wiki = false ) {
|
|
|
|
|
throw new DBAccessError;
|
|
|
|
|
}
|
|
|
|
|
function &getExternalLB( $cluster, $wiki = false ) {
|
|
|
|
|
throw new DBAccessError;
|
|
|
|
|
}
|
|
|
|
|
function forEachLB( $callback, $params = array() ) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Exception class for attempted DB access
|
|
|
|
|
*/
|
|
|
|
|
class DBAccessError extends MWException {
|
|
|
|
|
function __construct() {
|
|
|
|
|
parent::__construct( "Mediawiki tried to access the database via wfGetDB(). This is not allowed." );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-30 09:48:15 +00:00
|
|
|
/**
|
|
|
|
|
* Class for ensuring a consistent ordering of events as seen by the user, despite replication.
|
|
|
|
|
* Kind of like Hawking's [[Chronology Protection Agency]].
|
|
|
|
|
*/
|
|
|
|
|
class ChronologyProtector {
|
|
|
|
|
var $startupPos;
|
|
|
|
|
var $shutdownPos = array();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialise a LoadBalancer to give it appropriate chronology protection.
|
|
|
|
|
*
|
2010-03-11 20:59:25 +00:00
|
|
|
* @param $lb LoadBalancer
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
2008-04-14 07:45:50 +00:00
|
|
|
function initLB( $lb ) {
|
2008-03-30 09:48:15 +00:00
|
|
|
if ( $this->startupPos === null ) {
|
|
|
|
|
if ( !empty( $_SESSION[__CLASS__] ) ) {
|
|
|
|
|
$this->startupPos = $_SESSION[__CLASS__];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( !$this->startupPos ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$masterName = $lb->getServerName( 0 );
|
|
|
|
|
|
|
|
|
|
if ( $lb->getServerCount() > 1 && !empty( $this->startupPos[$masterName] ) ) {
|
|
|
|
|
$info = $lb->parentInfo();
|
|
|
|
|
$pos = $this->startupPos[$masterName];
|
|
|
|
|
wfDebug( __METHOD__.": LB " . $info['id'] . " waiting for master pos $pos\n" );
|
|
|
|
|
$lb->waitFor( $this->startupPos[$masterName] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Notify the ChronologyProtector that the LoadBalancer is about to shut
|
|
|
|
|
* down. Saves replication positions.
|
|
|
|
|
*
|
2010-03-11 20:59:25 +00:00
|
|
|
* @param $lb LoadBalancer
|
2008-03-30 09:48:15 +00:00
|
|
|
*/
|
|
|
|
|
function shutdownLB( $lb ) {
|
2009-02-17 14:06:42 +00:00
|
|
|
// Don't start a session, don't bother with non-replicated setups
|
|
|
|
|
if ( strval( session_id() ) == '' || $lb->getServerCount() <= 1 ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$masterName = $lb->getServerName( 0 );
|
|
|
|
|
if ( isset( $this->shutdownPos[$masterName] ) ) {
|
|
|
|
|
// Already done
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Only save the position if writes have been done on the connection
|
|
|
|
|
$db = $lb->getAnyOpenConnection( 0 );
|
|
|
|
|
$info = $lb->parentInfo();
|
|
|
|
|
if ( !$db || !$db->doneWrites() ) {
|
|
|
|
|
wfDebug( __METHOD__.": LB {$info['id']}, no writes done\n" );
|
|
|
|
|
return;
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|
2009-02-17 14:06:42 +00:00
|
|
|
$pos = $db->getMasterPos();
|
|
|
|
|
wfDebug( __METHOD__.": LB {$info['id']} has master pos $pos\n" );
|
|
|
|
|
$this->shutdownPos[$masterName] = $pos;
|
2008-03-30 09:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now.
|
|
|
|
|
* May commit chronology data to persistent storage.
|
|
|
|
|
*/
|
|
|
|
|
function shutdown() {
|
|
|
|
|
if ( session_id() != '' && count( $this->shutdownPos ) ) {
|
2008-04-14 07:45:50 +00:00
|
|
|
wfDebug( __METHOD__.": saving master pos for " .
|
2008-03-30 09:48:15 +00:00
|
|
|
count( $this->shutdownPos ) . " master(s)\n" );
|
|
|
|
|
$_SESSION[__CLASS__] = $this->shutdownPos;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|