From 4ac9fa081a7c045f6a9f1cfc529d82423f485b2e Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Sun, 8 Dec 2013 09:55:49 +0100 Subject: Update to MediaWiki 1.22.0 --- includes/db/ChronologyProtector.php | 106 ++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 includes/db/ChronologyProtector.php (limited to 'includes/db/ChronologyProtector.php') diff --git a/includes/db/ChronologyProtector.php b/includes/db/ChronologyProtector.php new file mode 100644 index 00000000..de5e72c3 --- /dev/null +++ b/includes/db/ChronologyProtector.php @@ -0,0 +1,106 @@ + position) */ + protected $startupPositions = array(); + /** @var Array (DB master name => position) */ + protected $shutdownPositions = array(); + + protected $initialized = false; // bool; whether the session data was loaded + + /** + * 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 $lb LoadBalancer + * @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 $lb LoadBalancer + * @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; + } + } +} -- cgit v1.2.2