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
23 use Psr\Log\LoggerAwareInterface
;
24 use Psr\Log\LoggerInterface
;
27 * Class for ensuring a consistent ordering of events as seen by the user, despite replication.
28 * Kind of like Hawking's [[Chronology Protection Agency]].
30 class ChronologyProtector
implements LoggerAwareInterface
{
33 /** @var LoggerInterface */
36 /** @var string Storage key name */
38 /** @var string Hash of client parameters */
40 /** @var float|null Minimum UNIX timestamp of 1+ expected startup positions */
41 protected $waitForPosTime;
42 /** @var int Max seconds to wait on positions to appear */
43 protected $waitForPosTimeout = self
::POS_WAIT_TIMEOUT
;
44 /** @var bool Whether to no-op all method calls */
45 protected $enabled = true;
46 /** @var bool Whether to check and wait on positions */
47 protected $wait = true;
49 /** @var bool Whether the client data was loaded */
50 protected $initialized = false;
51 /** @var DBMasterPos[] Map of (DB master name => position) */
52 protected $startupPositions = [];
53 /** @var DBMasterPos[] Map of (DB master name => position) */
54 protected $shutdownPositions = [];
55 /** @var float[] Map of (DB master name => 1) */
56 protected $shutdownTouchDBs = [];
58 /** @var integer Seconds to store positions */
59 const POSITION_TTL
= 60;
60 /** @var integer Max time to wait for positions to appear */
61 const POS_WAIT_TIMEOUT
= 5;
64 * @param BagOStuff $store
65 * @param array $client Map of (ip: <IP>, agent: <user-agent>)
66 * @param float $posTime UNIX timestamp
69 public function __construct( BagOStuff
$store, array $client, $posTime = null ) {
70 $this->store
= $store;
71 $this->clientId
= md5( $client['ip'] . "\n" . $client['agent'] );
72 $this->key
= $store->makeGlobalKey( __CLASS__
, $this->clientId
);
73 $this->waitForPosTime
= $posTime;
74 $this->logger
= new \Psr\Log\
NullLogger();
77 public function setLogger( LoggerInterface
$logger ) {
78 $this->logger
= $logger;
82 * @param bool $enabled Whether to no-op all method calls
85 public function setEnabled( $enabled ) {
86 $this->enabled
= $enabled;
90 * @param bool $enabled Whether to check and wait on positions
93 public function setWaitEnabled( $enabled ) {
94 $this->wait
= $enabled;
98 * Initialise a ILoadBalancer to give it appropriate chronology protection.
100 * If the stash has a previous master position recorded, this will try to
101 * make sure that the next query to a replica DB of that master will see changes up
102 * to that position by delaying execution. The delay may timeout and allow stale
103 * data if no non-lagged replica DBs are available.
105 * @param ILoadBalancer $lb
108 public function initLB( ILoadBalancer
$lb ) {
109 if ( !$this->enabled ||
$lb->getServerCount() <= 1 ) {
110 return; // non-replicated setup or disabled
113 $this->initPositions();
115 $masterName = $lb->getServerName( $lb->getWriterIndex() );
116 if ( !empty( $this->startupPositions
[$masterName] ) ) {
117 $pos = $this->startupPositions
[$masterName];
118 $this->logger
->info( __METHOD__
. ": LB for '$masterName' set to pos $pos\n" );
119 $lb->waitFor( $pos );
124 * Notify the ChronologyProtector that the ILoadBalancer is about to shut
125 * down. Saves replication positions.
127 * @param ILoadBalancer $lb
130 public function shutdownLB( ILoadBalancer
$lb ) {
131 if ( !$this->enabled
) {
132 return; // not enabled
133 } elseif ( !$lb->hasOrMadeRecentMasterChanges( INF
) ) {
134 // Only save the position if writes have been done on the connection
138 $masterName = $lb->getServerName( $lb->getWriterIndex() );
139 if ( $lb->getServerCount() > 1 ) {
140 $pos = $lb->getMasterPos();
141 $this->logger
->info( __METHOD__
. ": LB for '$masterName' has pos $pos\n" );
142 $this->shutdownPositions
[$masterName] = $pos;
144 $this->logger
->info( __METHOD__
. ": DB '$masterName' touched\n" );
146 $this->shutdownTouchDBs
[$masterName] = 1;
150 * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now.
151 * May commit chronology data to persistent storage.
153 * @param callable|null $workCallback Work to do instead of waiting on syncing positions
154 * @param string $mode One of (sync, async); whether to wait on remote datacenters
155 * @return DBMasterPos[] Empty on success; returns the (db name => position) map on failure
157 public function shutdown( callable
$workCallback = null, $mode = 'sync' ) {
158 if ( !$this->enabled
) {
162 $store = $this->store
;
163 // Some callers might want to know if a user recently touched a DB.
164 // These writes do not need to block on all datacenters receiving them.
165 foreach ( $this->shutdownTouchDBs
as $dbName => $unused ) {
167 $this->getTouchedKey( $this->store
, $dbName ),
173 if ( !count( $this->shutdownPositions
) ) {
174 return []; // nothing to save
177 $this->logger
->info( __METHOD__
. ": saving master pos for " .
178 implode( ', ', array_keys( $this->shutdownPositions
) ) . "\n"
181 // CP-protected writes should overwhemingly go to the master datacenter, so get DC-local
182 // lock to merge the values. Use a DC-local get() and a synchronous all-DC set(). This
183 // makes it possible for the BagOStuff class to write in parallel to all DCs with one RTT.
184 if ( $store->lock( $this->key
, 3 ) ) {
185 if ( $workCallback ) {
186 // Let the store run the work before blocking on a replication sync barrier. By the
187 // time it's done with the work, the barrier should be fast if replication caught up.
188 $store->addBusyCallback( $workCallback );
192 self
::mergePositions( $store->get( $this->key
), $this->shutdownPositions
),
194 ( $mode === 'sync' ) ?
$store::WRITE_SYNC
: 0
196 $store->unlock( $this->key
);
202 $bouncedPositions = $this->shutdownPositions
;
203 // Raced out too many times or stash is down
204 $this->logger
->warning( __METHOD__
. ": failed to save master pos for " .
205 implode( ', ', array_keys( $this->shutdownPositions
) ) . "\n"
207 } elseif ( $mode === 'sync' &&
208 $store->getQoS( $store::ATTR_SYNCWRITES
) < $store::QOS_SYNCWRITES_BE
210 // Positions may not be in all datacenters, force LBFactory to play it safe
211 $this->logger
->info( __METHOD__
. ": store may not support synchronous writes." );
212 $bouncedPositions = $this->shutdownPositions
;
214 $bouncedPositions = [];
217 return $bouncedPositions;
221 * @param string $dbName DB master name (e.g. "db1052")
222 * @return float|bool UNIX timestamp when client last touched the DB; false if not on record
225 public function getTouched( $dbName ) {
226 return $this->store
->get( $this->getTouchedKey( $this->store
, $dbName ) );
230 * @param BagOStuff $store
231 * @param string $dbName
234 private function getTouchedKey( BagOStuff
$store, $dbName ) {
235 return $store->makeGlobalKey( __CLASS__
, 'mtime', $this->clientId
, $dbName );
239 * Load in previous master positions for the client
241 protected function initPositions() {
242 if ( $this->initialized
) {
246 $this->initialized
= true;
248 // If there is an expectation to see master positions with a certain min
249 // timestamp, then block until they appear, or until a timeout is reached.
250 if ( $this->waitForPosTime
> 0.0 ) {
252 $loop = new WaitConditionLoop(
253 function () use ( &$data ) {
254 $data = $this->store
->get( $this->key
);
256 return ( self
::minPosTime( $data ) >= $this->waitForPosTime
)
257 ? WaitConditionLoop
::CONDITION_REACHED
258 : WaitConditionLoop
::CONDITION_CONTINUE
;
260 $this->waitForPosTimeout
262 $result = $loop->invoke();
263 $waitedMs = $loop->getLastWaitTime() * 1e3
;
265 if ( $result == $loop::CONDITION_REACHED
) {
266 $msg = "expected and found pos time {$this->waitForPosTime} ({$waitedMs}ms)";
267 $this->logger
->debug( $msg );
269 $msg = "expected but missed pos time {$this->waitForPosTime} ({$waitedMs}ms)";
270 $this->logger
->info( $msg );
273 $data = $this->store
->get( $this->key
);
276 $this->startupPositions
= $data ?
$data['positions'] : [];
277 $this->logger
->info( __METHOD__
. ": key is {$this->key} (read)\n" );
279 $this->startupPositions
= [];
280 $this->logger
->info( __METHOD__
. ": key is {$this->key} (unread)\n" );
285 * @param array|bool $data
288 private static function minPosTime( $data ) {
289 if ( !isset( $data['positions'] ) ) {
294 foreach ( $data['positions'] as $pos ) {
295 /** @var DBMasterPos $pos */
296 $min = $min ?
min( $pos->asOfTime(), $min ) : $pos->asOfTime();
303 * @param array|bool $curValue
304 * @param DBMasterPos[] $shutdownPositions
307 private static function mergePositions( $curValue, array $shutdownPositions ) {
308 /** @var $curPositions DBMasterPos[] */
309 if ( $curValue === false ) {
310 $curPositions = $shutdownPositions;
312 $curPositions = $curValue['positions'];
313 // Use the newest positions for each DB master
314 foreach ( $shutdownPositions as $db => $pos ) {
315 if ( !isset( $curPositions[$db] )
316 ||
$pos->asOfTime() > $curPositions[$db]->asOfTime()
318 $curPositions[$db] = $pos;
323 return [ 'positions' => $curPositions ];