aboutsummaryrefslogtreecommitdiffstats
path: root/includes/libs/rdbms/database/replication/MysqlReplicationReporter.php
blob: 2f8b772fcb429c4683944bc0c49046def53c228d (plain) (blame)
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
<?php
/**
 * 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
 */
namespace Wikimedia\Rdbms\Replication;

use InvalidArgumentException;
use RuntimeException;
use stdClass;
use Wikimedia\Rdbms\DBPrimaryPos;
use Wikimedia\Rdbms\DBQueryError;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\MySQLPrimaryPos;
use Wikimedia\Rdbms\Platform\ISQLPlatform;
use Wikimedia\Rdbms\Query;

/**
 * @internal
 * @ingroup Database
 * @since 1.40
 */
class MysqlReplicationReporter extends ReplicationReporter {
	/** @var MySQLPrimaryPos */
	protected $lastKnownReplicaPos;
	/** @var string Method to detect replica DB lag */
	protected $lagDetectionMethod;
	/** @var array Method to detect replica DB lag */
	protected $lagDetectionOptions = [];
	/** @var bool bool Whether to use GTID methods */
	protected $useGTIDs = false;
	/** @var stdClass|null */
	private $replicationInfoRow;
	// Cache getServerId() for 24 hours
	private const SERVER_ID_CACHE_TTL = 86400;

	/** @var float Warn if lag estimates are made for transactions older than this many seconds */
	private const LAG_STALE_WARN_THRESHOLD = 0.100;

	public function __construct(
		$topologyRole,
		$logger,
		$srvCache,
		$lagDetectionMethod,
		$lagDetectionOptions,
		$useGTIDs
	) {
		parent::__construct( $topologyRole, $logger, $srvCache );
		$this->lagDetectionMethod = $lagDetectionMethod;
		$this->lagDetectionOptions = $lagDetectionOptions;
		$this->useGTIDs = $useGTIDs;
	}

	protected function doGetLag( IDatabase $conn ) {
		if ( $this->lagDetectionMethod === 'pt-heartbeat' ) {
			return $this->getLagFromPtHeartbeat( $conn );
		} else {
			return $this->getLagFromSlaveStatus( $conn );
		}
	}

	/**
	 * @param IDatabase $conn To make queries
	 * @return int|false Second of lag
	 */
	protected function getLagFromSlaveStatus( IDatabase $conn ) {
		$query = new Query(
			'SHOW SLAVE STATUS',
			ISQLPlatform::QUERY_SILENCE_ERRORS | ISQLPlatform::QUERY_IGNORE_DBO_TRX | ISQLPlatform::QUERY_CHANGE_NONE,
			'SHOW',
			null,
			'SHOW SLAVE STATUS'
		);
		$res = $conn->query( $query, __METHOD__ );
		$row = $res ? $res->fetchObject() : false;
		// If the server is not replicating, there will be no row
		if ( $row && strval( $row->Seconds_Behind_Master ) !== '' ) {
			// https://mariadb.com/kb/en/delayed-replication/
			// https://dev.mysql.com/doc/refman/5.6/en/replication-delayed.html
			return intval( $row->Seconds_Behind_Master + ( $row->SQL_Remaining_Delay ?? 0 ) );
		}

		return false;
	}

	/**
	 * @param IDatabase $conn To make queries
	 * @return float|false Seconds of lag
	 */
	protected function getLagFromPtHeartbeat( IDatabase $conn ) {
		$currentTrxInfo = $this->getRecordedTransactionLagStatus( $conn );
		if ( $currentTrxInfo ) {
			// There is an active transaction and the initial lag was already queried
			$staleness = microtime( true ) - $currentTrxInfo['since'];
			if ( $staleness > self::LAG_STALE_WARN_THRESHOLD ) {
				// Avoid returning higher and higher lag value due to snapshot age
				// given that the isolation level will typically be REPEATABLE-READ
				// but UTC_TIMESTAMP() is not affected by point-in-time snapshots
				$this->logger->warning(
					"Using cached lag value for {db_server} due to active transaction",
					$this->getLogContext( $conn, [
						'method' => __METHOD__,
						'age' => $staleness,
						'exception' => new RuntimeException()
					] )
				);
			}

			return $currentTrxInfo['lag'];
		}

		$ago = $this->fetchSecondsSinceHeartbeat( $conn );
		if ( $ago !== null ) {
			return max( $ago, 0.0 );
		}

		$this->logger->error(
			"Unable to find pt-heartbeat row for {db_server}",
			$this->getLogContext( $conn, [
				'method' => __METHOD__
			] )
		);

		return false;
	}

	/**
	 * @param IDatabase $conn To make queries
	 * @return float|null Elapsed seconds since the newest beat or null if none was found
	 * @see https://www.percona.com/doc/percona-toolkit/2.1/pt-heartbeat.html
	 */
	protected function fetchSecondsSinceHeartbeat( IDatabase $conn ) {
		// Some setups might have pt-heartbeat running on each replica server.
		// Exclude the row for events originating on this DB server. Assume that
		// there is only one active replication channel and that any other row
		// getting updates must be the row for the primary DB server.
		$where = $conn->makeList(
			$this->lagDetectionOptions['conds'] ?? [ 'server_id != @@server_id' ],
			ISQLPlatform::LIST_AND
		);
		// User mysql server time so that query time and trip time are not counted.
		// Use ORDER BY for channel based queries since that field might not be UNIQUE.
		$query = new Query(
			"SELECT TIMESTAMPDIFF(MICROSECOND,ts,UTC_TIMESTAMP(6)) AS us_ago " .
			"FROM heartbeat.heartbeat WHERE $where ORDER BY ts DESC LIMIT 1",
			ISQLPlatform::QUERY_SILENCE_ERRORS | ISQLPlatform::QUERY_IGNORE_DBO_TRX | ISQLPlatform::QUERY_CHANGE_NONE,
			'SELECT',
			null,
			"SELECT TIMESTAMPDIFF(MICROSECOND,ts,UTC_TIMESTAMP(6)) AS us_ago " .
			"FROM heartbeat.heartbeat WHERE ? ORDER BY ts DESC LIMIT 1",
		);
		$res = $conn->query( $query, __METHOD__ );
		$row = $res ? $res->fetchObject() : false;

		return $row ? ( $row->us_ago / 1e6 ) : null;
	}

	public function getApproximateLagStatus( IDatabase $conn ) {
		if ( $this->lagDetectionMethod === 'pt-heartbeat' ) {
			// Disable caching since this is fast enough and we don't want
			// to be *too* pessimistic by having both the cache TTL and the
			// pt-heartbeat interval count as lag in getSessionLagStatus()
			return parent::getApproximateLagStatus( $conn );
		}

		$key = $this->srvCache->makeGlobalKey( 'mysql-lag', $conn->getServerName() );
		$approxLag = $this->srvCache->get( $key );
		if ( !$approxLag ) {
			$approxLag = parent::getApproximateLagStatus( $conn );
			$this->srvCache->set( $key, $approxLag, 1 );
		}

		return $approxLag;
	}

	/**
	 * @param IDatabase $conn To make queries
	 * @param string $fname
	 * @return stdClass Process cached row
	 */
	public function getReplicationSafetyInfo( IDatabase $conn, $fname ) {
		if ( $this->replicationInfoRow === null ) {
			$this->replicationInfoRow = $conn->selectRow(
				[],
				[
					'innodb_autoinc_lock_mode' => '@@innodb_autoinc_lock_mode',
					'binlog_format' => '@@binlog_format',
				],
				[],
				$fname
			);
		}
		return $this->replicationInfoRow;
	}

	/**
	 * @return bool Whether GTID support is used (mockable for testing)
	 */
	protected function useGTIDs() {
		return $this->useGTIDs;
	}

	public function primaryPosWait( IDatabase $conn, DBPrimaryPos $pos, $timeout ) {
		if ( !( $pos instanceof MySQLPrimaryPos ) ) {
			throw new InvalidArgumentException( "Position not an instance of MySQLPrimaryPos" );
		}

		if ( $this->topologyRole === IDatabase::ROLE_STATIC_CLONE ) {
			$this->logger->debug(
				"Bypassed replication wait; database has a static dataset",
				$this->getLogContext( $conn, [ 'method' => __METHOD__, 'raw_pos' => $pos ] )
			);

			return 0; // this is a copy of a read-only dataset with no primary DB
		} elseif ( $this->lastKnownReplicaPos && $this->lastKnownReplicaPos->hasReached( $pos ) ) {
			$this->logger->debug(
				"Bypassed replication wait; replication known to have reached {raw_pos}",
				$this->getLogContext( $conn, [ 'method' => __METHOD__, 'raw_pos' => $pos ] )
			);

			return 0; // already reached this point for sure
		}

		// Call doQuery() directly, to avoid opening a transaction if DBO_TRX is set
		if ( $pos->getGTIDs() ) {
			// Get the GTIDs from this replica server too see the domains (channels)
			$refPos = $this->getReplicaPos( $conn );
			if ( !$refPos ) {
				$this->logger->error(
					"Could not get replication position on replica DB to compare to {raw_pos}",
					$this->getLogContext( $conn, [ 'method' => __METHOD__, 'raw_pos' => $pos ] )
				);

				return -1; // this is the primary DB itself?
			}
			// GTIDs with domains (channels) that are active and are present on the replica
			$gtidsWait = $pos::getRelevantActiveGTIDs( $pos, $refPos );
			if ( !$gtidsWait ) {
				$this->logger->error(
					"No active GTIDs in {raw_pos} share a domain with those in {current_pos}",
					$this->getLogContext( $conn, [
						'method' => __METHOD__,
						'raw_pos' => $pos,
						'current_pos' => $refPos
					] )
				);

				return -1; // $pos is from the wrong cluster?
			}
			// Wait on the GTID set
			$gtidArg = $conn->addQuotes( implode( ',', $gtidsWait ) );
			if ( strpos( $gtidArg, ':' ) !== false ) {
				// MySQL GTIDs, e.g "source_id:transaction_id"
				$query = new Query(
					"SELECT WAIT_FOR_EXECUTED_GTID_SET($gtidArg, $timeout)",
					ISQLPlatform::QUERY_IGNORE_DBO_TRX | ISQLPlatform::QUERY_CHANGE_NONE,
					'SELECT',
					null,
					"SELECT WAIT_FOR_EXECUTED_GTID_SET(?, ?)"
				);
			} else {
				// MariaDB GTIDs, e.g."domain:server:sequence"
				$query = new Query(
					"SELECT MASTER_GTID_WAIT($gtidArg, $timeout)",
					ISQLPlatform::QUERY_IGNORE_DBO_TRX | ISQLPlatform::QUERY_CHANGE_NONE,
					'SELECT',
					null,
					"SELECT MASTER_GTID_WAIT(?, ?)"
				);
			}
			$waitPos = implode( ',', $gtidsWait );
		} else {
			// Wait on the binlog coordinates
			$encFile = $conn->addQuotes( $pos->getLogFile() );
			// @phan-suppress-next-line PhanTypeArraySuspiciousNullable
			$encPos = intval( $pos->getLogPosition()[$pos::CORD_EVENT] );
			$query = new Query(
				"SELECT MASTER_POS_WAIT($encFile, $encPos, $timeout)",
				ISQLPlatform::QUERY_IGNORE_DBO_TRX | ISQLPlatform::QUERY_CHANGE_NONE,
				'SELECT',
				null,
				"SELECT MASTER_POS_WAIT(?, ?, ?)"
			);
			$waitPos = $pos->__toString();
		}

		$start = microtime( true );
		$res = $conn->query( $query, __METHOD__ );
		$row = $res->fetchRow();
		$seconds = max( microtime( true ) - $start, 0 );

		// Result can be NULL (error), -1 (timeout), or 0+ per the MySQL manual
		$status = ( $row[0] !== null ) ? intval( $row[0] ) : null;
		if ( $status === null ) {
			$this->logger->error(
				"An error occurred while waiting for replication to reach {wait_pos}",
				$this->getLogContext( $conn, [
					'raw_pos' => $pos,
					'wait_pos' => $waitPos,
					'sql' => $query->getSQL(),
					'seconds_waited' => $seconds,
					'exception' => new RuntimeException()
				] )
			);
		} elseif ( $status < 0 ) {
			$this->logger->info(
				"Timed out waiting for replication to reach {wait_pos}",
				$this->getLogContext( $conn, [
					'raw_pos' => $pos,
					'wait_pos' => $waitPos,
					'timeout' => $timeout,
					'sql' => $query->getSQL(),
					'seconds_waited' => $seconds,
				] )
			);
		} elseif ( $status >= 0 ) {
			$this->logger->debug(
				"Replication has reached {wait_pos}",
				$this->getLogContext( $conn, [
					'raw_pos' => $pos,
					'wait_pos' => $waitPos,
					'seconds_waited' => $seconds,
				] )
			);
			// Remember that this position was reached to save queries next time
			$this->lastKnownReplicaPos = $pos;
		}

		return $status;
	}

	/**
	 * Get the position of the primary DB from SHOW SLAVE STATUS
	 *
	 * @param IDatabase $conn To make queries
	 * @return MySQLPrimaryPos|false
	 */
	public function getReplicaPos( IDatabase $conn ) {
		$now = microtime( true ); // as-of-time *before* fetching GTID variables

		if ( $this->useGTIDs() ) {
			// Try to use GTIDs, fallbacking to binlog positions if not possible
			$data = $this->getServerGTIDs( $conn, __METHOD__ );
			// Use gtid_slave_pos for MariaDB and gtid_executed for MySQL
			foreach ( [ 'gtid_slave_pos', 'gtid_executed' ] as $name ) {
				if ( isset( $data[$name] ) && strlen( $data[$name] ) ) {
					return new MySQLPrimaryPos( $data[$name], $now );
				}
			}
		}

		$data = $this->getServerRoleStatus( $conn, 'SLAVE', __METHOD__ );
		if ( $data && strlen( $data['Relay_Master_Log_File'] ) ) {
			return new MySQLPrimaryPos(
				"{$data['Relay_Master_Log_File']}/{$data['Exec_Master_Log_Pos']}",
				$now
			);
		}

		return false;
	}

	/**
	 * Get the position of the primary DB from SHOW MASTER STATUS
	 *
	 * @param IDatabase $conn To make queries
	 * @return MySQLPrimaryPos|false
	 */
	public function getPrimaryPos( IDatabase $conn ) {
		$now = microtime( true ); // as-of-time *before* fetching GTID variables

		$pos = false;
		if ( $this->useGTIDs() ) {
			// Try to use GTIDs, fallbacking to binlog positions if not possible
			$data = $this->getServerGTIDs( $conn, __METHOD__ );
			// Use gtid_binlog_pos for MariaDB and gtid_executed for MySQL
			foreach ( [ 'gtid_binlog_pos', 'gtid_executed' ] as $name ) {
				if ( isset( $data[$name] ) && strlen( $data[$name] ) ) {
					$pos = new MySQLPrimaryPos( $data[$name], $now );
					break;
				}
			}
			// Filter domains that are inactive or not relevant to the session
			if ( $pos ) {
				$pos->setActiveOriginServerId( $this->getServerId( $conn ) );
				$pos->setActiveOriginServerUUID( $this->getServerUUID( $conn ) );
				if ( isset( $data['gtid_domain_id'] ) ) {
					$pos->setActiveDomain( $data['gtid_domain_id'] );
				}
			}
		}

		if ( !$pos ) {
			$data = $this->getServerRoleStatus( $conn, 'MASTER', __METHOD__ );
			if ( $data && strlen( $data['File'] ) ) {
				$pos = new MySQLPrimaryPos( "{$data['File']}/{$data['Position']}", $now );
			}
		}

		return $pos;
	}

	/**
	 * @param IDatabase $conn To make queries
	 * @return string Value of server_id (32-bit integer, unique to the replication topology)
	 * @throws DBQueryError
	 */
	protected function getServerId( IDatabase $conn ) {
		$fname = __METHOD__;
		return $this->srvCache->getWithSetCallback(
			$this->srvCache->makeGlobalKey( 'mysql-server-id', $conn->getServerName() ),
			self::SERVER_ID_CACHE_TTL,
			static function () use ( $conn, $fname ) {
				$query = new Query(
					"SELECT @@server_id AS id",
					ISQLPlatform::QUERY_IGNORE_DBO_TRX | ISQLPlatform::QUERY_CHANGE_NONE,
					'SELECT',
					null,
					"SELECT @@server_id AS id"
				);
				$res = $conn->query( $query, $fname );

				return $res->fetchObject()->id;
			}
		);
	}

	/**
	 * @param IDatabase $conn To make queries
	 * @return string|null Value of server_uuid (hyphenated 128-bit hex string, globally unique)
	 * @throws DBQueryError
	 */
	protected function getServerUUID( IDatabase $conn ) {
		$fname = __METHOD__;
		return $this->srvCache->getWithSetCallback(
			$this->srvCache->makeGlobalKey( 'mysql-server-uuid', $conn->getServerName() ),
			self::SERVER_ID_CACHE_TTL,
			static function () use ( $conn, $fname ) {
				$query = new Query(
					"SHOW GLOBAL VARIABLES LIKE 'server_uuid'",
					ISQLPlatform::QUERY_IGNORE_DBO_TRX | ISQLPlatform::QUERY_CHANGE_NONE,
					'SHOW',
					null,
					"SHOW GLOBAL VARIABLES LIKE 'server_uuid'"
				);
				$res = $conn->query( $query, $fname );
				$row = $res->fetchObject();

				return $row ? $row->Value : null;
			}
		);
	}

	/**
	 * @param IDatabase $conn To make queries
	 * @param string $fname
	 * @return string[]
	 */
	protected function getServerGTIDs( IDatabase $conn, $fname ) {
		$map = [];

		$flags = ISQLPlatform::QUERY_IGNORE_DBO_TRX | ISQLPlatform::QUERY_CHANGE_NONE;

		// Get global-only variables like gtid_executed
		$query = new Query(
			"SHOW GLOBAL VARIABLES LIKE 'gtid_%'",
			$flags,
			'SHOW',
			null,
			"SHOW GLOBAL VARIABLES LIKE 'gtid_%'"
		);
		$res = $conn->query( $query, $fname );
		foreach ( $res as $row ) {
			$map[$row->Variable_name] = $row->Value;
		}
		// Get session-specific (e.g. gtid_domain_id since that is were writes will log)
		$query = new Query(
			"SHOW SESSION VARIABLES LIKE 'gtid_%'",
			$flags,
			'SHOW',
			null,
			"SHOW SESSION VARIABLES LIKE 'gtid_%'"
		);
		$res = $conn->query( $query, $fname );
		foreach ( $res as $row ) {
			$map[$row->Variable_name] = $row->Value;
		}

		return $map;
	}

	/**
	 * @param IDatabase $conn To make queries
	 * @param string $role One of "MASTER"/"SLAVE"
	 * @param string $fname
	 * @return array<string,mixed>|null Latest available server status row; false on failure
	 */
	protected function getServerRoleStatus( IDatabase $conn, $role, $fname ) {
		$query = new Query(
			"SHOW $role STATUS",
			ISQLPlatform::QUERY_SILENCE_ERRORS | ISQLPlatform::QUERY_IGNORE_DBO_TRX | ISQLPlatform::QUERY_CHANGE_NONE,
			'SHOW',
			null,
			"SHOW $role STATUS"
		);
		$res = $conn->query( $query, $fname );
		$row = $res ? $res->fetchRow() : false;

		return ( $row ?: null );
	}

}