[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/db/ -> ChronologyProtector.php (source)

   1  <?php
   2  /**
   3   * Generator of database load balancing objects.
   4   *
   5   * This program is free software; you can redistribute it and/or modify
   6   * it under the terms of the GNU General Public License as published by
   7   * the Free Software Foundation; either version 2 of the License, or
   8   * (at your option) any later version.
   9   *
  10   * This program is distributed in the hope that it will be useful,
  11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13   * GNU General Public License for more details.
  14   *
  15   * You should have received a copy of the GNU General Public License along
  16   * with this program; if not, write to the Free Software Foundation, Inc.,
  17   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18   * http://www.gnu.org/copyleft/gpl.html
  19   *
  20   * @file
  21   * @ingroup Database
  22   */
  23  
  24  /**
  25   * Class for ensuring a consistent ordering of events as seen by the user, despite replication.
  26   * Kind of like Hawking's [[Chronology Protection Agency]].
  27   */
  28  class ChronologyProtector {
  29      /** @var array (DB master name => position) */
  30      protected $startupPositions = array();
  31  
  32      /** @var array (DB master name => position) */
  33      protected $shutdownPositions = array();
  34  
  35      /** @var bool Whether the session data was loaded */
  36      protected $initialized = false;
  37  
  38      /**
  39       * Initialise a LoadBalancer to give it appropriate chronology protection.
  40       *
  41       * If the session has a previous master position recorded, this will try to
  42       * make sure that the next query to a slave of that master will see changes up
  43       * to that position by delaying execution. The delay may timeout and allow stale
  44       * data if no non-lagged slaves are available.
  45       *
  46       * @param LoadBalancer $lb
  47       * @return void
  48       */
  49  	public function initLB( LoadBalancer $lb ) {
  50          if ( $lb->getServerCount() <= 1 ) {
  51              return; // non-replicated setup
  52          }
  53          if ( !$this->initialized ) {
  54              $this->initialized = true;
  55              if ( isset( $_SESSION[__CLASS__] ) && is_array( $_SESSION[__CLASS__] ) ) {
  56                  $this->startupPositions = $_SESSION[__CLASS__];
  57              }
  58          }
  59          $masterName = $lb->getServerName( 0 );
  60          if ( !empty( $this->startupPositions[$masterName] ) ) {
  61              $info = $lb->parentInfo();
  62              $pos = $this->startupPositions[$masterName];
  63              wfDebug( __METHOD__ . ": LB " . $info['id'] . " waiting for master pos $pos\n" );
  64              $lb->waitFor( $pos );
  65          }
  66      }
  67  
  68      /**
  69       * Notify the ChronologyProtector that the LoadBalancer is about to shut
  70       * down. Saves replication positions.
  71       *
  72       * @param LoadBalancer $lb
  73       * @return void
  74       */
  75  	public function shutdownLB( LoadBalancer $lb ) {
  76          if ( session_id() == '' || $lb->getServerCount() <= 1 ) {
  77              return; // don't start a session; don't bother with non-replicated setups
  78          }
  79          $masterName = $lb->getServerName( 0 );
  80          if ( isset( $this->shutdownPositions[$masterName] ) ) {
  81              return; // already done
  82          }
  83          // Only save the position if writes have been done on the connection
  84          $db = $lb->getAnyOpenConnection( 0 );
  85          $info = $lb->parentInfo();
  86          if ( !$db || !$db->doneWrites() ) {
  87              wfDebug( __METHOD__ . ": LB {$info['id']}, no writes done\n" );
  88  
  89              return;
  90          }
  91          $pos = $db->getMasterPos();
  92          wfDebug( __METHOD__ . ": LB {$info['id']} has master pos $pos\n" );
  93          $this->shutdownPositions[$masterName] = $pos;
  94      }
  95  
  96      /**
  97       * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now.
  98       * May commit chronology data to persistent storage.
  99       *
 100       * @return void
 101       */
 102  	public function shutdown() {
 103          if ( session_id() != '' && count( $this->shutdownPositions ) ) {
 104              wfDebug( __METHOD__ . ": saving master pos for " .
 105                  count( $this->shutdownPositions ) . " master(s)\n" );
 106              $_SESSION[__CLASS__] = $this->shutdownPositions;
 107          }
 108      }
 109  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1