MediaWiki PHP Cross Reference Collaborative Wikis

Source: /includes/db/ChronologyProtector.php - 106 lines - 3702 bytes - Summary - Text - Print

Description: Generator of database load balancing objects. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

   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      /** @var Array (DB master name => position) */
  32      protected $shutdownPositions = array();
  33  
  34      protected $initialized = false; // bool; whether the session data was loaded
  35  
  36      /**
  37       * Initialise a LoadBalancer to give it appropriate chronology protection.
  38       *
  39       * If the session has a previous master position recorded, this will try to
  40       * make sure that the next query to a slave of that master will see changes up
  41       * to that position by delaying execution. The delay may timeout and allow stale
  42       * data if no non-lagged slaves are available.
  43       *
  44       * @param $lb LoadBalancer
  45       * @return void
  46       */
  47  	public function initLB( LoadBalancer $lb ) {
  48          if ( $lb->getServerCount() <= 1 ) {
  49              return; // non-replicated setup
  50          }
  51          if ( !$this->initialized ) {
  52              $this->initialized = true;
  53              if ( isset( $_SESSION[__CLASS__] ) && is_array( $_SESSION[__CLASS__] ) ) {
  54                  $this->startupPositions = $_SESSION[__CLASS__];
  55              }
  56          }
  57          $masterName = $lb->getServerName( 0 );
  58          if ( !empty( $this->startupPositions[$masterName] ) ) {
  59              $info = $lb->parentInfo();
  60              $pos = $this->startupPositions[$masterName];
  61              wfDebug( __METHOD__ . ": LB " . $info['id'] . " waiting for master pos $pos\n" );
  62              $lb->waitFor( $pos );
  63          }
  64      }
  65  
  66      /**
  67       * Notify the ChronologyProtector that the LoadBalancer is about to shut
  68       * down. Saves replication positions.
  69       *
  70       * @param $lb LoadBalancer
  71       * @return void
  72       */
  73  	public function shutdownLB( LoadBalancer $lb ) {
  74          if ( session_id() == '' || $lb->getServerCount() <= 1 ) {
  75              return; // don't start a session; don't bother with non-replicated setups
  76          }
  77          $masterName = $lb->getServerName( 0 );
  78          if ( isset( $this->shutdownPositions[$masterName] ) ) {
  79              return; // already done
  80          }
  81          // Only save the position if writes have been done on the connection
  82          $db = $lb->getAnyOpenConnection( 0 );
  83          $info = $lb->parentInfo();
  84          if ( !$db || !$db->doneWrites() ) {
  85              wfDebug( __METHOD__ . ": LB {$info['id']}, no writes done\n" );
  86              return;
  87          }
  88          $pos = $db->getMasterPos();
  89          wfDebug( __METHOD__ . ": LB {$info['id']} has master pos $pos\n" );
  90          $this->shutdownPositions[$masterName] = $pos;
  91      }
  92  
  93      /**
  94       * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now.
  95       * May commit chronology data to persistent storage.
  96       *
  97       * @return void
  98       */
  99  	public function shutdown() {
 100          if ( session_id() != '' && count( $this->shutdownPositions ) ) {
 101              wfDebug( __METHOD__ . ": saving master pos for " .
 102                  count( $this->shutdownPositions ) . " master(s)\n" );
 103              $_SESSION[__CLASS__] = $this->shutdownPositions;
 104          }
 105      }
 106  }

title

Description

title

Description

title

Description

title

title

Body