position) */ protected $startupPositions = array(); /** @var array (DB master name => position) */ protected $shutdownPositions = array(); /** @var bool Whether the session data was loaded */ protected $initialized = false; /** * Initialise a LoadBalancer to give it appropriate chronology protection. * * If the session has a previous master position recorded, this will try to * make sure that the next query to a slave of that master will see changes up * to that position by delaying execution. The delay may timeout and allow stale * data if no non-lagged slaves are available. * * @param LoadBalancer $lb * @return void */ public function initLB( LoadBalancer $lb ) { if ( $lb->getServerCount() <= 1 ) { return; // non-replicated setup } if ( !$this->initialized ) { $this->initialized = true; if ( isset( $_SESSION[__CLASS__] ) && is_array( $_SESSION[__CLASS__] ) ) { $this->startupPositions = $_SESSION[__CLASS__]; } } $masterName = $lb->getServerName( 0 ); if ( !empty( $this->startupPositions[$masterName] ) ) { $info = $lb->parentInfo(); $pos = $this->startupPositions[$masterName]; wfDebug( __METHOD__ . ": LB " . $info['id'] . " waiting for master pos $pos\n" ); $lb->waitFor( $pos ); } } /** * Notify the ChronologyProtector that the LoadBalancer is about to shut * down. Saves replication positions. * * @param LoadBalancer $lb * @return void */ public function shutdownLB( LoadBalancer $lb ) { if ( session_id() == '' || $lb->getServerCount() <= 1 ) { return; // don't start a session; don't bother with non-replicated setups } $masterName = $lb->getServerName( 0 ); if ( isset( $this->shutdownPositions[$masterName] ) ) { return; // already done } // 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; } $pos = $db->getMasterPos(); wfDebug( __METHOD__ . ": LB {$info['id']} has master pos $pos\n" ); $this->shutdownPositions[$masterName] = $pos; } /** * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now. * May commit chronology data to persistent storage. * * @return void */ public function shutdown() { if ( session_id() != '' && count( $this->shutdownPositions ) ) { wfDebug( __METHOD__ . ": saving master pos for " . count( $this->shutdownPositions ) . " master(s)\n" ); $_SESSION[__CLASS__] = $this->shutdownPositions; } } }