| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Rdbms; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use Stringable; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class MySQLPrimaryPos implements Stringable, DBPrimaryPos { |
| 22 |
|
| 23 |
private $style; |
| 24 |
|
| 25 |
private $binLog; |
| 26 |
|
| 27 |
private $logPos; |
| 28 |
|
| 29 |
private $gtids = []; |
| 30 |
|
| 31 |
private $activeDomain; |
| 32 |
|
| 33 |
private $activeServerId; |
| 34 |
|
| 35 |
private $activeServerUUID; |
| 36 |
|
| 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 |
|
| 44 |
public const CORD_INDEX = 0; |
| 45 |
|
| 46 |
public const CORD_EVENT = 1; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public function __construct( $position, $asOfTime ) { |
| 53 |
$this->init( $position, $asOfTime ); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
protected function init( $position, $asOfTime ) { |
| 61 |
$m = []; |
| 62 |
if ( preg_match( '!^(.+)\.(\d+)/(\d+)$!', $position, $m ) ) { |
| 63 |
$this->binLog = $m[1]; |
| 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 |
|
| 77 |
|
| 78 |
|
| 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 |
public function asOfTime() { |
| 96 |
return $this->asOfTime; |
| 97 |
} |
| 98 |
|
| 99 |
public function hasReached( DBPrimaryPos $pos ) { |
| 100 |
if ( !( $pos instanceof self ) ) { |
| 101 |
throw new InvalidArgumentException( "Position not an instance of " . __CLASS__ ); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
$thisPosByDomain = $this->getActiveGtidCoordinates(); |
| 106 |
$thatPosByDomain = $pos->getActiveGtidCoordinates(); |
| 107 |
if ( $thisPosByDomain && $thatPosByDomain ) { |
| 108 |
$comparisons = []; |
| 109 |
|
| 110 |
foreach ( $thatPosByDomain as $domain => $thatPos ) { |
| 111 |
if ( isset( $thisPosByDomain[$domain] ) ) { |
| 112 |
$comparisons[] = ( $thatPos <= $thisPosByDomain[$domain] ); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
return ( $comparisons && !in_array( false, $comparisons, true ) ); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
$thisBinPos = $this->getBinlogCoordinates(); |
| 124 |
$thatBinPos = $pos->getBinlogCoordinates(); |
| 125 |
if ( $thisBinPos && $thatBinPos && $thisBinPos['binlog'] === $thatBinPos['binlog'] ) { |
| 126 |
return ( $thisBinPos['pos'] >= $thatBinPos['pos'] ); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public function getLogPosition() { |
| 138 |
return $this->gtids ? null : $this->logPos; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function getLogFile() { |
| 146 |
|
| 147 |
return $this->gtids ? null : "{$this->binLog}.{$this->logPos[self::CORD_INDEX]}"; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public function getGTIDs() { |
| 155 |
return $this->gtids; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public function setActiveDomain( $id ) { |
| 171 |
$this->activeDomain = (string)$id; |
| 172 |
|
| 173 |
return $this; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public function setActiveOriginServerId( $id ) { |
| 188 |
$this->activeServerId = (string)$id; |
| 189 |
|
| 190 |
return $this; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public function setActiveOriginServerUUID( $id ) { |
| 205 |
$this->activeServerUUID = $id; |
| 206 |
|
| 207 |
return $this; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public static function getRelevantActiveGTIDs( MySQLPrimaryPos $pos, MySQLPrimaryPos $refPos ) { |
| 217 |
return array_values( array_intersect_key( |
| 218 |
$pos->gtids, |
| 219 |
$pos->getActiveGtidCoordinates(), |
| 220 |
$refPos->gtids |
| 221 |
) ); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
protected function getActiveGtidCoordinates() { |
| 230 |
$gtidInfos = []; |
| 231 |
|
| 232 |
foreach ( $this->gtids as $gtid ) { |
| 233 |
[ $domain, $pos, $server ] = self::parseGTID( $gtid ); |
| 234 |
|
| 235 |
$ignore = false; |
| 236 |
|
| 237 |
if ( $this->style === self::GTID_MARIA && $this->activeDomain !== null ) { |
| 238 |
$ignore = $ignore || ( $domain !== $this->activeDomain ); |
| 239 |
} |
| 240 |
|
| 241 |
if ( $this->style === self::GTID_MARIA && $this->activeServerId !== null ) { |
| 242 |
$ignore = $ignore || ( $server !== $this->activeServerId ); |
| 243 |
} elseif ( $this->style === self::GTID_MYSQL && $this->activeServerUUID !== null ) { |
| 244 |
$ignore = $ignore || ( $server !== $this->activeServerUUID ); |
| 245 |
} |
| 246 |
|
| 247 |
if ( !$ignore ) { |
| 248 |
$gtidInfos[$domain] = $pos; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
return $gtidInfos; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
protected static function parseGTID( $id ) { |
| 261 |
$m = []; |
| 262 |
if ( preg_match( '!^(\d+)-(\d+)-(\d+)$!', $id, $m ) ) { |
| 263 |
|
| 264 |
$channelId = $m[1]; |
| 265 |
$originServerId = $m[2]; |
| 266 |
$eventNumber = $m[3]; |
| 267 |
$style = self::GTID_MARIA; |
| 268 |
} elseif ( preg_match( '!^(\w{8}-\w{4}-\w{4}-\w{4}-\w{12}):(?:\d+-|)(\d+)$!', $id, $m ) ) { |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
$channelId = $m[1]; |
| 274 |
$originServerId = $m[1]; |
| 275 |
$eventNumber = $m[2]; |
| 276 |
$style = self::GTID_MYSQL; |
| 277 |
} else { |
| 278 |
return null; |
| 279 |
} |
| 280 |
|
| 281 |
return [ $channelId, $eventNumber, $originServerId, $style ]; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
protected function getBinlogCoordinates() { |
| 290 |
return ( $this->binLog !== null && $this->logPos !== null ) |
| 291 |
? [ 'binlog' => $this->binLog, 'pos' => $this->logPos ] |
| 292 |
: false; |
| 293 |
} |
| 294 |
|
| 295 |
public static function newFromArray( array $data ) { |
| 296 |
$pos = new self( $data['position'], $data['asOfTime'] ); |
| 297 |
|
| 298 |
if ( isset( $data['activeDomain'] ) ) { |
| 299 |
$pos->setActiveDomain( $data['activeDomain'] ); |
| 300 |
} |
| 301 |
if ( isset( $data['activeServerId'] ) ) { |
| 302 |
$pos->setActiveOriginServerId( $data['activeServerId'] ); |
| 303 |
} |
| 304 |
if ( isset( $data['activeServerUUID'] ) ) { |
| 305 |
$pos->setActiveOriginServerUUID( $data['activeServerUUID'] ); |
| 306 |
} |
| 307 |
return $pos; |
| 308 |
} |
| 309 |
|
| 310 |
public function toArray(): array { |
| 311 |
return [ |
| 312 |
'_type_' => get_class( $this ), |
| 313 |
'position' => $this->__toString(), |
| 314 |
'activeDomain' => $this->activeDomain, |
| 315 |
'activeServerId' => $this->activeServerId, |
| 316 |
'activeServerUUID' => $this->activeServerUUID, |
| 317 |
'asOfTime' => $this->asOfTime |
| 318 |
]; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
public function __toString() { |
| 325 |
return $this->gtids |
| 326 |
? implode( ',', $this->gtids ) |
| 327 |
|
| 328 |
: $this->getLogFile() . "/{$this->logPos[self::CORD_EVENT]}"; |
| 329 |
} |
| 330 |
|
| 331 |
} |