Merge "Revised design of Special:Userlogin"
[lhc/web/wiklou.git] / includes / db / ChronologyProtector.php
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 $startupPos;
30 var $shutdownPos = array();
31
32 /**
33 * Initialise a LoadBalancer to give it appropriate chronology protection.
34 *
35 * @param $lb LoadBalancer
36 */
37 function initLB( $lb ) {
38 if ( $this->startupPos === null ) {
39 if ( !empty( $_SESSION[__CLASS__] ) ) {
40 $this->startupPos = $_SESSION[__CLASS__];
41 }
42 }
43 if ( !$this->startupPos ) {
44 return;
45 }
46 $masterName = $lb->getServerName( 0 );
47
48 if ( $lb->getServerCount() > 1 && !empty( $this->startupPos[$masterName] ) ) {
49 $info = $lb->parentInfo();
50 $pos = $this->startupPos[$masterName];
51 wfDebug( __METHOD__ . ": LB " . $info['id'] . " waiting for master pos $pos\n" );
52 $lb->waitFor( $this->startupPos[$masterName] );
53 }
54 }
55
56 /**
57 * Notify the ChronologyProtector that the LoadBalancer is about to shut
58 * down. Saves replication positions.
59 *
60 * @param $lb LoadBalancer
61 */
62 function shutdownLB( $lb ) {
63 // Don't start a session, don't bother with non-replicated setups
64 if ( strval( session_id() ) == '' || $lb->getServerCount() <= 1 ) {
65 return;
66 }
67 $masterName = $lb->getServerName( 0 );
68 if ( isset( $this->shutdownPos[$masterName] ) ) {
69 // Already done
70 return;
71 }
72 // Only save the position if writes have been done on the connection
73 $db = $lb->getAnyOpenConnection( 0 );
74 $info = $lb->parentInfo();
75 if ( !$db || !$db->doneWrites() ) {
76 wfDebug( __METHOD__ . ": LB {$info['id']}, no writes done\n" );
77 return;
78 }
79 $pos = $db->getMasterPos();
80 wfDebug( __METHOD__ . ": LB {$info['id']} has master pos $pos\n" );
81 $this->shutdownPos[$masterName] = $pos;
82 }
83
84 /**
85 * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now.
86 * May commit chronology data to persistent storage.
87 */
88 function shutdown() {
89 if ( session_id() != '' && count( $this->shutdownPos ) ) {
90 wfDebug( __METHOD__ . ": saving master pos for " .
91 count( $this->shutdownPos ) . " master(s)\n" );
92 $_SESSION[__CLASS__] = $this->shutdownPos;
93 }
94 }
95 }