readOnlyReason = $conf['readOnlyReason']; } $this->chronProt = $this->newChronologyProtector(); $this->trxProfiler = Profiler::instance()->getTransactionProfiler(); $this->logger = LoggerFactory::getInstance( 'DBTransaction' ); } /** * 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 LBFactoryFake( $wgLBFactoryConf ); } /** * Get an LBFactory instance * * @return LBFactory */ public static function singleton() { global $wgLBFactoryConf; if ( is_null( self::$instance ) ) { $class = self::getLBFactoryClass( $wgLBFactoryConf ); $config = $wgLBFactoryConf; if ( !isset( $config['readOnlyReason'] ) ) { $config['readOnlyReason'] = wfConfiguredReadOnlyReason(); } self::$instance = new $class( $config ); } return self::$instance; } /** * Returns the LBFactory class to use and the load balancer configuration. * * @param array $config (e.g. $wgLBFactoryConf) * @return string Class name */ public static function getLBFactoryClass( array $config ) { // For configuration backward compatibility after removing // underscores from class names in MediaWiki 1.23. $bcClasses = array( 'LBFactory_Simple' => 'LBFactorySimple', 'LBFactory_Single' => 'LBFactorySingle', 'LBFactory_Multi' => 'LBFactoryMulti', 'LBFactory_Fake' => 'LBFactoryFake', ); $class = $config['class']; if ( isset( $bcClasses[$class] ) ) { $class = $bcClasses[$class]; wfDeprecated( '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details', '1.23' ); } return $class; } /** * Shut down, close connections and destroy the cached instance. */ public static function destroyInstance() { if ( self::$instance ) { self::$instance->shutdown(); self::$instance->forEachLBCallMethod( 'closeAll' ); self::$instance = null; } } /** * Set the instance to be the given object * * @param LBFactory $instance */ public static function setInstance( $instance ) { self::destroyInstance(); self::$instance = $instance; } /** * Create a new load balancer object. The resulting object will be untracked, * not chronology-protected, and the caller is responsible for cleaning it up. * * @param bool|string $wiki Wiki ID, or false for the current wiki * @return LoadBalancer */ abstract public 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 public function getMainLB( $wiki = false ); /** * Create a new load balancer for external storage. The resulting object will be * untracked, not chronology-protected, and the caller is responsible for * cleaning it up. * * @param string $cluster External storage cluster, or false for core * @param bool|string $wiki Wiki ID, or false for the current wiki * @return LoadBalancer */ abstract protected function newExternalLB( $cluster, $wiki = false ); /** * Get a cached (tracked) load balancer for external storage * * @param string $cluster External storage cluster, or false for core * @param bool|string $wiki Wiki ID, or false for the current wiki * @return LoadBalancer */ abstract public function &getExternalLB( $cluster, $wiki = false ); /** * 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. * * @param callable $callback * @param array $params */ abstract public function forEachLB( $callback, array $params = array() ); /** * Prepare all tracked load balancers for shutdown * @param integer $flags Supports SHUTDOWN_* flags * STUB */ public function shutdown( $flags = 0 ) { } /** * Call a method of each tracked load balancer * * @param string $methodName * @param array $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 on all connections. Done for two reasons: * 1. To commit changes to the masters. * 2. To release the snapshot on all connections, master and slave. * @param string $fname Caller name */ public function commitAll( $fname = __METHOD__ ) { $this->logMultiDbTransaction(); $start = microtime( true ); $this->forEachLBCallMethod( 'commitAll', array( $fname ) ); $timeMs = 1000 * ( microtime( true ) - $start ); RequestContext::getMain()->getStats()->timing( "db.commit-all", $timeMs ); } /** * Commit changes on all master connections * @param string $fname Caller name */ public function commitMasterChanges( $fname = __METHOD__ ) { $this->logMultiDbTransaction(); $start = microtime( true ); $this->forEachLBCallMethod( 'commitMasterChanges', array( $fname ) ); $timeMs = 1000 * ( microtime( true ) - $start ); RequestContext::getMain()->getStats()->timing( "db.commit-masters", $timeMs ); } /** * Rollback changes on all master connections * @param string $fname Caller name * @since 1.23 */ public function rollbackMasterChanges( $fname = __METHOD__ ) { $this->forEachLBCallMethod( 'rollbackMasterChanges', array( $fname ) ); } /** * Log query info if multi DB transactions are going to be committed now */ private function logMultiDbTransaction() { $callersByDB = array(); $this->forEachLB( function ( LoadBalancer $lb ) use ( &$callersByDB ) { $masterName = $lb->getServerName( $lb->getWriterIndex() ); $callers = $lb->pendingMasterChangeCallers(); if ( $callers ) { $callersByDB[$masterName] = $callers; } } ); if ( count( $callersByDB ) >= 2 ) { $dbs = implode( ', ', array_keys( $callersByDB ) ); $msg = "Multi-DB transaction [{$dbs}]:\n"; foreach ( $callersByDB as $db => $callers ) { $msg .= "$db: " . implode( '; ', $callers ) . "\n"; } $this->logger->info( $msg ); } } /** * Determine if any master connection has pending changes * @return bool * @since 1.23 */ public function hasMasterChanges() { $ret = false; $this->forEachLB( function ( LoadBalancer $lb ) use ( &$ret ) { $ret = $ret || $lb->hasMasterChanges(); } ); return $ret; } /** * Detemine if any lagged slave connection was used * @since 1.27 * @return bool */ public function laggedSlaveUsed() { $ret = false; $this->forEachLB( function ( LoadBalancer $lb ) use ( &$ret ) { $ret = $ret || $lb->laggedSlaveUsed(); } ); return $ret; } /** * Determine if any master connection has pending/written changes from this request * @return bool * @since 1.27 */ public function hasOrMadeRecentMasterChanges() { $ret = false; $this->forEachLB( function ( LoadBalancer $lb ) use ( &$ret ) { $ret = $ret || $lb->hasOrMadeRecentMasterChanges(); } ); return $ret; } /** * Disable the ChronologyProtector for all load balancers * * This can be called at the start of special API entry points * * @since 1.27 */ public function disableChronologyProtection() { $this->chronProt->setEnabled( false ); } /** * @return ChronologyProtector */ protected function newChronologyProtector() { $request = RequestContext::getMain()->getRequest(); $chronProt = new ChronologyProtector( ObjectCache::getMainStashInstance(), array( 'ip' => $request->getIP(), 'agent' => $request->getHeader( 'User-Agent' ) ) ); if ( PHP_SAPI === 'cli' ) { $chronProt->setEnabled( false ); } elseif ( $request->getHeader( 'ChronologyProtection' ) === 'false' ) { // Request opted out of using position wait logic. This is useful for requests // done by the job queue or background ETL that do not have a meaningful session. $chronProt->setWaitEnabled( false ); } return $chronProt; } /** * @param ChronologyProtector $cp */ protected function shutdownChronologyProtector( ChronologyProtector $cp ) { // Get all the master positions needed $this->forEachLB( function ( LoadBalancer $lb ) use ( $cp ) { $cp->shutdownLB( $lb ); } ); // Write them to the stash $unsavedPositions = $cp->shutdown(); // If the positions failed to write to the stash, at least wait on local datacenter // slaves to catch up before responding. Even if there are several DCs, this increases // the chance that the user will see their own changes immediately afterwards. As long // as the sticky DC cookie applies (same domain), this is not even an issue. $this->forEachLB( function ( LoadBalancer $lb ) use ( $unsavedPositions ) { $masterName = $lb->getServerName( $lb->getWriterIndex() ); if ( isset( $unsavedPositions[$masterName] ) ) { $lb->waitForAll( $unsavedPositions[$masterName] ); } } ); } } /** * Exception class for attempted DB access */ class DBAccessError extends MWException { public function __construct() { parent::__construct( "Mediawiki tried to access the database via wfGetDB(). " . "This is not allowed." ); } }