Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.91% |
90 / 99 |
|
70.59% |
12 / 17 |
CRAP | |
0.00% |
0 / 1 |
| MySQLPrimaryPos | |
90.91% |
90 / 99 |
|
70.59% |
12 / 17 |
55.11 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| init | |
89.47% |
17 / 19 |
|
0.00% |
0 / 1 |
7.06 | |||
| asOfTime | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasReached | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
10.03 | |||
| getLogPosition | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| getLogFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| getGTIDs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setActiveDomain | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setActiveOriginServerId | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setActiveOriginServerUUID | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getRelevantActiveGTIDs | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| getActiveGtidCoordinates | |
84.62% |
11 / 13 |
|
0.00% |
0 / 1 |
12.52 | |||
| parseGTID | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
3.00 | |||
| getBinlogCoordinates | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| newFromArray | |
62.50% |
5 / 8 |
|
0.00% |
0 / 1 |
4.84 | |||
| toArray | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Wikimedia\Rdbms; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | use Stringable; |
| 7 | |
| 8 | /** |
| 9 | * DBPrimaryPos implementation for MySQL and MariaDB. |
| 10 | * |
| 11 | * Note that primary positions and sync logic here make some assumptions: |
| 12 | * |
| 13 | * - Binlog-based usage assumes single-source replication and non-hierarchical replication. |
| 14 | * - GTID-based usage allows getting/syncing with multi-source replication. It is assumed |
| 15 | * that GTID sets are complete (e.g. include all domains on the server). |
| 16 | * |
| 17 | * @see https://mariadb.com/kb/en/library/gtid/ |
| 18 | * @see https://dev.mysql.com/doc/refman/5.6/en/replication-gtids-concepts.html |
| 19 | * @internal |
| 20 | */ |
| 21 | class MySQLPrimaryPos implements Stringable, DBPrimaryPos { |
| 22 | /** @var string One of (BINARY_LOG, GTID_MYSQL, GTID_MARIA) */ |
| 23 | private $style; |
| 24 | /** @var string|null Base name of all Binary Log files */ |
| 25 | private $binLog; |
| 26 | /** @var array<int,int|string>|null Binary Log position tuple (index number, event number) */ |
| 27 | private $logPos; |
| 28 | /** @var string[] Map of (server_uuid/gtid_domain_id => GTID) */ |
| 29 | private $gtids = []; |
| 30 | /** @var string|null Active GTID domain ID */ |
| 31 | private $activeDomain; |
| 32 | /** @var string|null ID of the server were DB writes originate */ |
| 33 | private $activeServerId; |
| 34 | /** @var string|null UUID of the server were DB writes originate */ |
| 35 | private $activeServerUUID; |
| 36 | /** @var float UNIX timestamp */ |
| 37 | private $asOfTime = 0.0; |
| 38 | |
| 39 | private const BINARY_LOG = 'binary-log'; |
| 40 | private const GTID_MARIA = 'gtid-maria'; |
| 41 | private const GTID_MYSQL = 'gtid-mysql'; |
| 42 | |
| 43 | /** Key name of the 6 digit binary log index number of a position tuple */ |
| 44 | public const CORD_INDEX = 0; |
| 45 | /** Key name of the 64 bit binary log event number of a position tuple */ |
| 46 | public const CORD_EVENT = 1; |
| 47 | |
| 48 | /** |
| 49 | * @param string $position One of (comma separated GTID list, <binlog file>/<64 bit integer>) |
| 50 | * @param float $asOfTime UNIX timestamp |
| 51 | */ |
| 52 | public function __construct( $position, $asOfTime ) { |
| 53 | $this->init( $position, $asOfTime ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @param string $position |
| 58 | * @param float $asOfTime |
| 59 | */ |
| 60 | protected function init( $position, $asOfTime ) { |
| 61 | $m = []; |
| 62 | if ( preg_match( '!^(.+)\.(\d+)/(\d+)$!', $position, $m ) ) { |
| 63 | $this->binLog = $m[1]; // ideally something like host name |
| 64 | $this->logPos = [ self::CORD_INDEX => (int)$m[2], self::CORD_EVENT => $m[3] ]; |
| 65 | $this->style = self::BINARY_LOG; |
| 66 | } else { |
| 67 | $gtids = array_filter( array_map( 'trim', explode( ',', $position ) ) ); |
| 68 | foreach ( $gtids as $gtid ) { |
| 69 | $components = self::parseGTID( $gtid ); |
| 70 | if ( !$components ) { |
| 71 | throw new InvalidArgumentException( "Invalid GTID '$gtid'." ); |
| 72 | } |
| 73 | |
| 74 | [ $domain, $eventNumber, , $this->style ] = $components; |
| 75 | if ( isset( $this->gtids[$domain] ) ) { |
| 76 | // For MySQL, handle the case where some past issue caused a gap in the |
| 77 | // executed GTID set, e.g. [last_purged+1,N-1] and [N+1,N+2+K]. Ignore the |
| 78 | // gap by using the GTID with the highest ending event number. |
| 79 | [ , $otherEventNumber ] = self::parseGTID( $this->gtids[$domain] ); |
| 80 | if ( $eventNumber > $otherEventNumber ) { |
| 81 | $this->gtids[$domain] = $gtid; |
| 82 | } |
| 83 | } else { |
| 84 | $this->gtids[$domain] = $gtid; |
| 85 | } |
| 86 | } |
| 87 | if ( !$this->gtids ) { |
| 88 | throw new InvalidArgumentException( "GTID set cannot be empty." ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | $this->asOfTime = $asOfTime; |
| 93 | } |
| 94 | |
| 95 | /** @inheritDoc */ |
| 96 | public function asOfTime() { |
| 97 | return $this->asOfTime; |
| 98 | } |
| 99 | |
| 100 | /** @inheritDoc */ |
| 101 | public function hasReached( DBPrimaryPos $pos ) { |
| 102 | if ( !( $pos instanceof self ) ) { |
| 103 | throw new InvalidArgumentException( "Position not an instance of " . __CLASS__ ); |
| 104 | } |
| 105 | |
| 106 | // Prefer GTID comparisons, which work with multi-tier replication |
| 107 | $thisPosByDomain = $this->getActiveGtidCoordinates(); |
| 108 | $thatPosByDomain = $pos->getActiveGtidCoordinates(); |
| 109 | if ( $thisPosByDomain && $thatPosByDomain ) { |
| 110 | $comparisons = []; |
| 111 | // Check that this has positions reaching those in $pos for all domains in common |
| 112 | foreach ( $thatPosByDomain as $domain => $thatPos ) { |
| 113 | if ( isset( $thisPosByDomain[$domain] ) ) { |
| 114 | $comparisons[] = ( $thatPos <= $thisPosByDomain[$domain] ); |
| 115 | } |
| 116 | } |
| 117 | // Check that $this has a GTID for at least one domain also in $pos; due to MariaDB |
| 118 | // quirks, prior primary switch-overs may result in inactive garbage GTIDs that cannot |
| 119 | // be cleaned up. Assume that the domains in both this and $pos cover the relevant |
| 120 | // active channels. |
| 121 | return ( $comparisons && !in_array( false, $comparisons, true ) ); |
| 122 | } |
| 123 | |
| 124 | // Fallback to the binlog file comparisons |
| 125 | $thisBinPos = $this->getBinlogCoordinates(); |
| 126 | $thatBinPos = $pos->getBinlogCoordinates(); |
| 127 | if ( $thisBinPos && $thatBinPos && $thisBinPos['binlog'] === $thatBinPos['binlog'] ) { |
| 128 | return ( $thisBinPos['pos'] >= $thatBinPos['pos'] ); |
| 129 | } |
| 130 | |
| 131 | // Comparing totally different binlogs does not make sense |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @return array<int,int|string>|null Tuple of (binary log file number, 64 bit event number) |
| 137 | * @since 1.31 |
| 138 | */ |
| 139 | public function getLogPosition() { |
| 140 | return $this->gtids ? null : $this->logPos; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @return string|null Name of the binary log file for this position |
| 145 | * @since 1.31 |
| 146 | */ |
| 147 | public function getLogFile() { |
| 148 | // @phan-suppress-next-line PhanTypeArraySuspiciousNullable |
| 149 | return $this->gtids ? null : "{$this->binLog}.{$this->logPos[self::CORD_INDEX]}"; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @return array<string,string> Map of (server_uuid/gtid_domain_id => GTID) |
| 154 | * @since 1.31 |
| 155 | */ |
| 156 | public function getGTIDs() { |
| 157 | return $this->gtids; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Set the GTID domain known to be used in new commits on a replication stream of interest |
| 162 | * |
| 163 | * This makes getRelevantActiveGTIDs() filter out GTIDs from other domains |
| 164 | * |
| 165 | * @see MySQLPrimaryPos::getRelevantActiveGTIDs() |
| 166 | * @see https://mariadb.com/kb/en/library/gtid/#gtid_domain_id |
| 167 | * |
| 168 | * @param string|int|null $id @@gtid_domain_id of the active replication stream |
| 169 | * @return MySQLPrimaryPos This instance (since 1.34) |
| 170 | * @since 1.31 |
| 171 | */ |
| 172 | public function setActiveDomain( $id ) { |
| 173 | $this->activeDomain = (string)$id; |
| 174 | |
| 175 | return $this; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Set the server ID known to be used in new commits on a replication stream of interest |
| 180 | * |
| 181 | * This makes getRelevantActiveGTIDs() filter out GTIDs from other origin servers |
| 182 | * |
| 183 | * @see MySQLPrimaryPos::getRelevantActiveGTIDs() |
| 184 | * |
| 185 | * @param string|int|null $id @@server_id of the server were writes originate |
| 186 | * @return MySQLPrimaryPos This instance (since 1.34) |
| 187 | * @since 1.31 |
| 188 | */ |
| 189 | public function setActiveOriginServerId( $id ) { |
| 190 | $this->activeServerId = (string)$id; |
| 191 | |
| 192 | return $this; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Set the server UUID known to be used in new commits on a replication stream of interest |
| 197 | * |
| 198 | * This makes getRelevantActiveGTIDs() filter out GTIDs from other origin servers |
| 199 | * |
| 200 | * @see MySQLPrimaryPos::getRelevantActiveGTIDs() |
| 201 | * |
| 202 | * @param string|null $id @@server_uuid of the server were writes originate |
| 203 | * @return MySQLPrimaryPos This instance (since 1.34) |
| 204 | * @since 1.31 |
| 205 | */ |
| 206 | public function setActiveOriginServerUUID( $id ) { |
| 207 | $this->activeServerUUID = $id; |
| 208 | |
| 209 | return $this; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * @param MySQLPrimaryPos $pos |
| 214 | * @param MySQLPrimaryPos $refPos |
| 215 | * @return string[] List of active GTIDs from $pos that have domains in $refPos |
| 216 | * @since 1.34 |
| 217 | */ |
| 218 | public static function getRelevantActiveGTIDs( MySQLPrimaryPos $pos, MySQLPrimaryPos $refPos ) { |
| 219 | return array_values( array_intersect_key( |
| 220 | $pos->gtids, |
| 221 | $pos->getActiveGtidCoordinates(), |
| 222 | $refPos->gtids |
| 223 | ) ); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @see https://mariadb.com/kb/en/mariadb/gtid |
| 228 | * @see https://dev.mysql.com/doc/refman/5.6/en/replication-gtids-concepts.html |
| 229 | * @return array<string,int> Map of (server_uuid/gtid_domain_id => integer position) |
| 230 | */ |
| 231 | protected function getActiveGtidCoordinates() { |
| 232 | $gtidInfos = []; |
| 233 | |
| 234 | foreach ( $this->gtids as $gtid ) { |
| 235 | [ $domain, $pos, $server ] = self::parseGTID( $gtid ); |
| 236 | |
| 237 | $ignore = false; |
| 238 | // Filter out GTIDs from non-active replication domains |
| 239 | if ( $this->style === self::GTID_MARIA && $this->activeDomain !== null ) { |
| 240 | $ignore = $ignore || ( $domain !== $this->activeDomain ); |
| 241 | } |
| 242 | // Likewise for GTIDs from non-active replication origin servers |
| 243 | if ( $this->style === self::GTID_MARIA && $this->activeServerId !== null ) { |
| 244 | $ignore = $ignore || ( $server !== $this->activeServerId ); |
| 245 | } elseif ( $this->style === self::GTID_MYSQL && $this->activeServerUUID !== null ) { |
| 246 | $ignore = $ignore || ( $server !== $this->activeServerUUID ); |
| 247 | } |
| 248 | |
| 249 | if ( !$ignore ) { |
| 250 | $gtidInfos[$domain] = $pos; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | return $gtidInfos; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * @param string $id GTID |
| 259 | * @return string[]|null (domain ID, event number, source server ID, style) for MariaDB, |
| 260 | * (source server UUID, event number, source server UUID, style) for MySQL, or null |
| 261 | */ |
| 262 | protected static function parseGTID( $id ) { |
| 263 | $m = []; |
| 264 | if ( preg_match( '!^(\d+)-(\d+)-(\d+)$!', $id, $m ) ) { |
| 265 | // MariaDB style: "<32 bit domain ID>-<32 bit server id>-<64 bit event number>" |
| 266 | $channelId = $m[1]; |
| 267 | $originServerId = $m[2]; |
| 268 | $eventNumber = $m[3]; |
| 269 | $style = self::GTID_MARIA; |
| 270 | } elseif ( preg_match( '!^(\w{8}-\w{4}-\w{4}-\w{4}-\w{12}):(?:\d+-|)(\d+)$!', $id, $m ) ) { |
| 271 | // MySQL style: "<server UUID>:<64 bit event number>[-<64 bit event number>]". |
| 272 | // Normally, the first number should reflect the point (gtid_purged) where older |
| 273 | // binary logs where purged to save space. When doing comparisons, it may as well |
| 274 | // be 1 in that case. Assume that this is generally the situation. |
| 275 | $channelId = $m[1]; |
| 276 | $originServerId = $m[1]; |
| 277 | $eventNumber = $m[2]; |
| 278 | $style = self::GTID_MYSQL; |
| 279 | } else { |
| 280 | return null; |
| 281 | } |
| 282 | |
| 283 | return [ $channelId, $eventNumber, $originServerId, $style ]; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * @see https://dev.mysql.com/doc/refman/5.7/en/show-master-status.html |
| 288 | * @see https://dev.mysql.com/doc/refman/5.7/en/show-slave-status.html |
| 289 | * @return array|false Map of (binlog:<string>, pos:(<integer>, <integer>)) or false |
| 290 | */ |
| 291 | protected function getBinlogCoordinates() { |
| 292 | return ( $this->binLog !== null && $this->logPos !== null ) |
| 293 | ? [ 'binlog' => $this->binLog, 'pos' => $this->logPos ] |
| 294 | : false; |
| 295 | } |
| 296 | |
| 297 | /** @inheritDoc */ |
| 298 | public static function newFromArray( array $data ) { |
| 299 | $pos = new self( $data['position'], $data['asOfTime'] ); |
| 300 | |
| 301 | if ( isset( $data['activeDomain'] ) ) { |
| 302 | $pos->setActiveDomain( $data['activeDomain'] ); |
| 303 | } |
| 304 | if ( isset( $data['activeServerId'] ) ) { |
| 305 | $pos->setActiveOriginServerId( $data['activeServerId'] ); |
| 306 | } |
| 307 | if ( isset( $data['activeServerUUID'] ) ) { |
| 308 | $pos->setActiveOriginServerUUID( $data['activeServerUUID'] ); |
| 309 | } |
| 310 | return $pos; |
| 311 | } |
| 312 | |
| 313 | public function toArray(): array { |
| 314 | return [ |
| 315 | '_type_' => get_class( $this ), |
| 316 | 'position' => $this->__toString(), |
| 317 | 'activeDomain' => $this->activeDomain, |
| 318 | 'activeServerId' => $this->activeServerId, |
| 319 | 'activeServerUUID' => $this->activeServerUUID, |
| 320 | 'asOfTime' => $this->asOfTime |
| 321 | ]; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * @return string GTID set or <binary log file>/<position> (e.g db1034-bin.000976/843431247) |
| 326 | */ |
| 327 | public function __toString() { |
| 328 | return $this->gtids |
| 329 | ? implode( ',', $this->gtids ) |
| 330 | // @phan-suppress-next-line PhanTypeArraySuspiciousNullable |
| 331 | : $this->getLogFile() . "/{$this->logPos[self::CORD_EVENT]}"; |
| 332 | } |
| 333 | |
| 334 | } |