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