Implement extension registration from an extension.json file
[mediawiki.git] / includes / db / ChronologyProtector.php
blob0c7b612ebae0ed9348f0919f852323b1505da45c
1 <?php
2 /**
3 * Generator of database load balancing objects.
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.
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.
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
20 * @file
21 * @ingroup Database
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]].
28 class ChronologyProtector {
29 /** @var array (DB master name => position) */
30 protected $startupPositions = array();
32 /** @var array (DB master name => position) */
33 protected $shutdownPositions = array();
35 /** @var bool Whether the session data was loaded */
36 protected $initialized = false;
38 /**
39 * Initialise a LoadBalancer to give it appropriate chronology protection.
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.
46 * @param LoadBalancer $lb
47 * @return void
49 public function initLB( LoadBalancer $lb ) {
50 if ( $lb->getServerCount() <= 1 ) {
51 return; // non-replicated setup
53 if ( !$this->initialized ) {
54 $this->initialized = true;
55 if ( isset( $_SESSION[__CLASS__] ) && is_array( $_SESSION[__CLASS__] ) ) {
56 $this->startupPositions = $_SESSION[__CLASS__];
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 );
68 /**
69 * Notify the ChronologyProtector that the LoadBalancer is about to shut
70 * down. Saves replication positions.
72 * @param LoadBalancer $lb
73 * @return void
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
79 $masterName = $lb->getServerName( 0 );
80 if ( isset( $this->shutdownPositions[$masterName] ) ) {
81 return; // already done
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" );
89 return;
91 $pos = $db->getMasterPos();
92 wfDebug( __METHOD__ . ": LB {$info['id']} has master pos $pos\n" );
93 $this->shutdownPositions[$masterName] = $pos;
96 /**
97 * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now.
98 * May commit chronology data to persistent storage.
100 * @return void
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;