MediaWiki  REL1_24
ChronologyProtector.php
Go to the documentation of this file.
00001 <?php
00028 class ChronologyProtector {
00030     protected $startupPositions = array();
00031 
00033     protected $shutdownPositions = array();
00034 
00036     protected $initialized = false;
00037 
00049     public function initLB( LoadBalancer $lb ) {
00050         if ( $lb->getServerCount() <= 1 ) {
00051             return; // non-replicated setup
00052         }
00053         if ( !$this->initialized ) {
00054             $this->initialized = true;
00055             if ( isset( $_SESSION[__CLASS__] ) && is_array( $_SESSION[__CLASS__] ) ) {
00056                 $this->startupPositions = $_SESSION[__CLASS__];
00057             }
00058         }
00059         $masterName = $lb->getServerName( 0 );
00060         if ( !empty( $this->startupPositions[$masterName] ) ) {
00061             $info = $lb->parentInfo();
00062             $pos = $this->startupPositions[$masterName];
00063             wfDebug( __METHOD__ . ": LB " . $info['id'] . " waiting for master pos $pos\n" );
00064             $lb->waitFor( $pos );
00065         }
00066     }
00067 
00075     public function shutdownLB( LoadBalancer $lb ) {
00076         if ( session_id() == '' || $lb->getServerCount() <= 1 ) {
00077             return; // don't start a session; don't bother with non-replicated setups
00078         }
00079         $masterName = $lb->getServerName( 0 );
00080         if ( isset( $this->shutdownPositions[$masterName] ) ) {
00081             return; // already done
00082         }
00083         // Only save the position if writes have been done on the connection
00084         $db = $lb->getAnyOpenConnection( 0 );
00085         $info = $lb->parentInfo();
00086         if ( !$db || !$db->doneWrites() ) {
00087             wfDebug( __METHOD__ . ": LB {$info['id']}, no writes done\n" );
00088 
00089             return;
00090         }
00091         $pos = $db->getMasterPos();
00092         wfDebug( __METHOD__ . ": LB {$info['id']} has master pos $pos\n" );
00093         $this->shutdownPositions[$masterName] = $pos;
00094     }
00095 
00102     public function shutdown() {
00103         if ( session_id() != '' && count( $this->shutdownPositions ) ) {
00104             wfDebug( __METHOD__ . ": saving master pos for " .
00105                 count( $this->shutdownPositions ) . " master(s)\n" );
00106             $_SESSION[__CLASS__] = $this->shutdownPositions;
00107         }
00108     }
00109 }