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