447b238ff9daf0e98073956ef35440c3a83bd76d
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
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
{
30 var $shutdownPos = array();
33 * Initialise a LoadBalancer to give it appropriate chronology protection.
35 * @param $lb LoadBalancer
37 function initLB( $lb ) {
38 if ( $this->startupPos
=== null ) {
39 if ( !empty( $_SESSION[__CLASS__
] ) ) {
40 $this->startupPos
= $_SESSION[__CLASS__
];
43 if ( !$this->startupPos
) {
46 $masterName = $lb->getServerName( 0 );
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] );
57 * Notify the ChronologyProtector that the LoadBalancer is about to shut
58 * down. Saves replication positions.
60 * @param $lb LoadBalancer
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 ) {
67 $masterName = $lb->getServerName( 0 );
68 if ( isset( $this->shutdownPos
[$masterName] ) ) {
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" );
79 $pos = $db->getMasterPos();
80 wfDebug( __METHOD__
. ": LB {$info['id']} has master pos $pos\n" );
81 $this->shutdownPos
[$masterName] = $pos;
85 * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now.
86 * May commit chronology data to persistent storage.
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
;