summaryrefslogtreecommitdiff
path: root/www/wiki/includes/libs/rdbms/ChronologyProtector.php
blob: e11528659d4a9334d3fed6466b4b9b7514e05e38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/**
 * Generator of database load balancing objects.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Database
 */

namespace Wikimedia\Rdbms;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Wikimedia\WaitConditionLoop;
use BagOStuff;

/**
 * Class for ensuring a consistent ordering of events as seen by the user, despite replication.
 * Kind of like Hawking's [[Chronology Protection Agency]].
 */
class ChronologyProtector implements LoggerAwareInterface {
	/** @var BagOStuff */
	protected $store;
	/** @var LoggerInterface */
	protected $logger;

	/** @var string Storage key name */
	protected $key;
	/** @var string Hash of client parameters */
	protected $clientId;
	/** @var int|null Expected minimum index of the last write to the position store */
	protected $waitForPosIndex;
	/** @var int Max seconds to wait on positions to appear */
	protected $waitForPosStoreTimeout = self::POS_STORE_WAIT_TIMEOUT;
	/** @var bool Whether to no-op all method calls */
	protected $enabled = true;
	/** @var bool Whether to check and wait on positions */
	protected $wait = true;

	/** @var bool Whether the client data was loaded */
	protected $initialized = false;
	/** @var DBMasterPos[] Map of (DB master name => position) */
	protected $startupPositions = [];
	/** @var DBMasterPos[] Map of (DB master name => position) */
	protected $shutdownPositions = [];
	/** @var float[] Map of (DB master name => 1) */
	protected $shutdownTouchDBs = [];

	/** @var int Seconds to store positions */
	const POSITION_TTL = 60;
	/** @var int Max time to wait for positions to appear */
	const POS_STORE_WAIT_TIMEOUT = 5;

	/**
	 * @param BagOStuff $store
	 * @param array[] $client Map of (ip: <IP>, agent: <user-agent>)
	 * @param int|null $posIndex Write counter index [optional]
	 * @since 1.27
	 */
	public function __construct( BagOStuff $store, array $client, $posIndex = null ) {
		$this->store = $store;
		$this->clientId = md5( $client['ip'] . "\n" . $client['agent'] );
		$this->key = $store->makeGlobalKey( __CLASS__, $this->clientId, 'v2' );
		$this->waitForPosIndex = $posIndex;
		$this->logger = new NullLogger();
	}

	public function setLogger( LoggerInterface $logger ) {
		$this->logger = $logger;
	}

	/**
	 * @param bool $enabled Whether to no-op all method calls
	 * @since 1.27
	 */
	public function setEnabled( $enabled ) {
		$this->enabled = $enabled;
	}

	/**
	 * @param bool $enabled Whether to check and wait on positions
	 * @since 1.27
	 */
	public function setWaitEnabled( $enabled ) {
		$this->wait = $enabled;
	}

	/**
	 * Initialise a ILoadBalancer to give it appropriate chronology protection.
	 *
	 * If the stash has a previous master position recorded, this will try to
	 * make sure that the next query to a replica DB of that master will see changes up
	 * to that position by delaying execution. The delay may timeout and allow stale
	 * data if no non-lagged replica DBs are available.
	 *
	 * @param ILoadBalancer $lb
	 * @return void
	 */
	public function initLB( ILoadBalancer $lb ) {
		if ( !$this->enabled || $lb->getServerCount() <= 1 ) {
			return; // non-replicated setup or disabled
		}

		$this->initPositions();

		$masterName = $lb->getServerName( $lb->getWriterIndex() );
		if (
			isset( $this->startupPositions[$masterName] ) &&
			$this->startupPositions[$masterName] instanceof DBMasterPos
		) {
			$pos = $this->startupPositions[$masterName];
			$this->logger->debug( __METHOD__ . ": LB for '$masterName' set to pos $pos\n" );
			$lb->waitFor( $pos );
		}
	}

	/**
	 * Notify the ChronologyProtector that the ILoadBalancer is about to shut
	 * down. Saves replication positions.
	 *
	 * @param ILoadBalancer $lb
	 * @return void
	 */
	public function shutdownLB( ILoadBalancer $lb ) {
		if ( !$this->enabled ) {
			return; // not enabled
		} elseif ( !$lb->hasOrMadeRecentMasterChanges( INF ) ) {
			// Only save the position if writes have been done on the connection
			return;
		}

		$masterName = $lb->getServerName( $lb->getWriterIndex() );
		if ( $lb->getServerCount() > 1 ) {
			$pos = $lb->getMasterPos();
			if ( $pos ) {
				$this->logger->debug( __METHOD__ . ": LB for '$masterName' has pos $pos\n" );
				$this->shutdownPositions[$masterName] = $pos;
			}
		} else {
			$this->logger->debug( __METHOD__ . ": DB '$masterName' touched\n" );
		}
		$this->shutdownTouchDBs[$masterName] = 1;
	}

	/**
	 * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now.
	 * May commit chronology data to persistent storage.
	 *
	 * @param callable|null $workCallback Work to do instead of waiting on syncing positions
	 * @param string $mode One of (sync, async); whether to wait on remote datacenters
	 * @param int|null &$cpIndex DB position key write counter; incremented on update
	 * @return DBMasterPos[] Empty on success; returns the (db name => position) map on failure
	 */
	public function shutdown( callable $workCallback = null, $mode = 'sync', &$cpIndex = null ) {
		if ( !$this->enabled ) {
			return [];
		}

		$store = $this->store;
		// Some callers might want to know if a user recently touched a DB.
		// These writes do not need to block on all datacenters receiving them.
		foreach ( $this->shutdownTouchDBs as $dbName => $unused ) {
			$store->set(
				$this->getTouchedKey( $this->store, $dbName ),
				microtime( true ),
				$store::TTL_DAY
			);
		}

		if ( !count( $this->shutdownPositions ) ) {
			return []; // nothing to save
		}

		$this->logger->debug( __METHOD__ . ": saving master pos for " .
			implode( ', ', array_keys( $this->shutdownPositions ) ) . "\n"
		);

		// CP-protected writes should overwhemingly go to the master datacenter, so get DC-local
		// lock to merge the values. Use a DC-local get() and a synchronous all-DC set(). This
		// makes it possible for the BagOStuff class to write in parallel to all DCs with one RTT.
		if ( $store->lock( $this->key, 3 ) ) {
			if ( $workCallback ) {
				// Let the store run the work before blocking on a replication sync barrier. By the
				// time it's done with the work, the barrier should be fast if replication caught up.
				$store->addBusyCallback( $workCallback );
			}
			$ok = $store->set(
				$this->key,
				$this->mergePositions(
					$store->get( $this->key ),
					$this->shutdownPositions,
					$cpIndex
				),
				self::POSITION_TTL,
				( $mode === 'sync' ) ? $store::WRITE_SYNC : 0
			);
			$store->unlock( $this->key );
		} else {
			$ok = false;
			$cpIndex = null; // nothing saved
		}

		if ( !$ok ) {
			$bouncedPositions = $this->shutdownPositions;
			// Raced out too many times or stash is down
			$this->logger->warning( __METHOD__ . ": failed to save master pos for " .
				implode( ', ', array_keys( $this->shutdownPositions ) ) . "\n"
			);
		} elseif ( $mode === 'sync' &&
			$store->getQoS( $store::ATTR_SYNCWRITES ) < $store::QOS_SYNCWRITES_BE
		) {
			// Positions may not be in all datacenters, force LBFactory to play it safe
			$this->logger->info( __METHOD__ . ": store may not support synchronous writes." );
			$bouncedPositions = $this->shutdownPositions;
		} else {
			$bouncedPositions = [];
		}

		return $bouncedPositions;
	}

	/**
	 * @param string $dbName DB master name (e.g. "db1052")
	 * @return float|bool UNIX timestamp when client last touched the DB; false if not on record
	 * @since 1.28
	 */
	public function getTouched( $dbName ) {
		return $this->store->get( $this->getTouchedKey( $this->store, $dbName ) );
	}

	/**
	 * @param BagOStuff $store
	 * @param string $dbName
	 * @return string
	 */
	private function getTouchedKey( BagOStuff $store, $dbName ) {
		return $store->makeGlobalKey( __CLASS__, 'mtime', $this->clientId, $dbName );
	}

	/**
	 * Load in previous master positions for the client
	 */
	protected function initPositions() {
		if ( $this->initialized ) {
			return;
		}

		$this->initialized = true;
		if ( $this->wait ) {
			// If there is an expectation to see master positions from a certain write
			// index or higher, then block until it appears, or until a timeout is reached.
			// Since the write index restarts each time the key is created, it is possible that
			// a lagged store has a matching key write index. However, in that case, it should
			// already be expired and thus treated as non-existing, maintaining correctness.
			if ( $this->waitForPosIndex > 0 ) {
				$data = null;
				$loop = new WaitConditionLoop(
					function () use ( &$data ) {
						$data = $this->store->get( $this->key );
						if ( !is_array( $data ) ) {
							return WaitConditionLoop::CONDITION_CONTINUE; // not found yet
						} elseif ( !isset( $data['writeIndex'] ) ) {
							return WaitConditionLoop::CONDITION_REACHED; // b/c
						}

						return ( $data['writeIndex'] >= $this->waitForPosIndex )
							? WaitConditionLoop::CONDITION_REACHED
							: WaitConditionLoop::CONDITION_CONTINUE;
					},
					$this->waitForPosStoreTimeout
				);
				$result = $loop->invoke();
				$waitedMs = $loop->getLastWaitTime() * 1e3;

				if ( $result == $loop::CONDITION_REACHED ) {
					$msg = "expected and found pos index {$this->waitForPosIndex} ({$waitedMs}ms)";
					$this->logger->debug( $msg );
				} else {
					$msg = "expected but missed pos index {$this->waitForPosIndex} ({$waitedMs}ms)";
					$this->logger->info( $msg );
				}
			} else {
				$data = $this->store->get( $this->key );
			}

			$this->startupPositions = $data ? $data['positions'] : [];
			$this->logger->debug( __METHOD__ . ": key is {$this->key} (read)\n" );
		} else {
			$this->startupPositions = [];
			$this->logger->debug( __METHOD__ . ": key is {$this->key} (unread)\n" );
		}
	}

	/**
	 * @param array|bool $curValue
	 * @param DBMasterPos[] $shutdownPositions
	 * @param int|null &$cpIndex
	 * @return array
	 */
	protected function mergePositions( $curValue, array $shutdownPositions, &$cpIndex = null ) {
		/** @var DBMasterPos[] $curPositions */
		$curPositions = isset( $curValue['positions'] ) ? $curValue['positions'] : [];
		// Use the newest positions for each DB master
		foreach ( $shutdownPositions as $db => $pos ) {
			if (
				!isset( $curPositions[$db] ) ||
				!( $curPositions[$db] instanceof DBMasterPos ) ||
				$pos->asOfTime() > $curPositions[$db]->asOfTime()
			) {
				$curPositions[$db] = $pos;
			}
		}

		$cpIndex = isset( $curValue['writeIndex'] ) ? $curValue['writeIndex'] : 0;

		return [
			'positions' => $curPositions,
			'writeIndex' => ++$cpIndex
		];
	}
}