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
{
29 /** @var Array (DB master name => position) */
30 protected $startupPositions = array();
31 /** @var Array (DB master name => position) */
32 protected $shutdownPositions = array();
34 protected $initialized = false; // bool; whether the session data was loaded
37 * Initialise a LoadBalancer to give it appropriate chronology protection.
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.
44 * @param $lb LoadBalancer
47 public function initLB( LoadBalancer
$lb ) {
48 if ( $lb->getServerCount() <= 1 ) {
49 return; // non-replicated setup
51 if ( !$this->initialized
) {
52 $this->initialized
= true;
53 if ( isset( $_SESSION[__CLASS__
] ) && is_array( $_SESSION[__CLASS__
] ) ) {
54 $this->startupPositions
= $_SESSION[__CLASS__
];
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" );
67 * Notify the ChronologyProtector that the LoadBalancer is about to shut
68 * down. Saves replication positions.
70 * @param $lb LoadBalancer
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
77 $masterName = $lb->getServerName( 0 );
78 if ( isset( $this->shutdownPositions
[$masterName] ) ) {
79 return; // already done
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" );
88 $pos = $db->getMasterPos();
89 wfDebug( __METHOD__
. ": LB {$info['id']} has master pos $pos\n" );
90 $this->shutdownPositions
[$masterName] = $pos;
94 * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now.
95 * May commit chronology data to persistent storage.
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
;