MediaWiki  1.28.1
MySQLMasterPos.php
Go to the documentation of this file.
1 <?php
10 class MySQLMasterPos implements DBMasterPos {
12  public $file;
14  public $pos;
16  public $gtids = [];
18  public $asOfTime = 0.0;
19 
25  function __construct( $file, $pos, $gtid = '' ) {
26  $this->file = $file;
27  $this->pos = $pos;
28  $this->gtids = array_map( 'trim', explode( ',', $gtid ) );
29  $this->asOfTime = microtime( true );
30  }
31 
35  function __toString() {
36  return "{$this->file}/{$this->pos}";
37  }
38 
39  function asOfTime() {
40  return $this->asOfTime;
41  }
42 
43  function hasReached( DBMasterPos $pos ) {
44  if ( !( $pos instanceof self ) ) {
45  throw new InvalidArgumentException( "Position not an instance of " . __CLASS__ );
46  }
47 
48  // Prefer GTID comparisons, which work with multi-tier replication
49  $thisPosByDomain = $this->getGtidCoordinates();
50  $thatPosByDomain = $pos->getGtidCoordinates();
51  if ( $thisPosByDomain && $thatPosByDomain ) {
52  $reached = true;
53  // Check that this has positions GTE all of those in $pos for all domains in $pos
54  foreach ( $thatPosByDomain as $domain => $thatPos ) {
55  $thisPos = isset( $thisPosByDomain[$domain] ) ? $thisPosByDomain[$domain] : -1;
56  $reached = $reached && ( $thatPos <= $thisPos );
57  }
58 
59  return $reached;
60  }
61 
62  // Fallback to the binlog file comparisons
63  $thisBinPos = $this->getBinlogCoordinates();
64  $thatBinPos = $pos->getBinlogCoordinates();
65  if ( $thisBinPos && $thatBinPos && $thisBinPos['binlog'] === $thatBinPos['binlog'] ) {
66  return ( $thisBinPos['pos'] >= $thatBinPos['pos'] );
67  }
68 
69  // Comparing totally different binlogs does not make sense
70  return false;
71  }
72 
74  if ( !( $pos instanceof self ) ) {
75  throw new InvalidArgumentException( "Position not an instance of " . __CLASS__ );
76  }
77 
78  // Prefer GTID comparisons, which work with multi-tier replication
79  $thisPosDomains = array_keys( $this->getGtidCoordinates() );
80  $thatPosDomains = array_keys( $pos->getGtidCoordinates() );
81  if ( $thisPosDomains && $thatPosDomains ) {
82  // Check that this has GTIDs for all domains in $pos
83  return !array_diff( $thatPosDomains, $thisPosDomains );
84  }
85 
86  // Fallback to the binlog file comparisons
87  $thisBinPos = $this->getBinlogCoordinates();
88  $thatBinPos = $pos->getBinlogCoordinates();
89 
90  return ( $thisBinPos && $thatBinPos && $thisBinPos['binlog'] === $thatBinPos['binlog'] );
91  }
92 
99  protected function getGtidCoordinates() {
100  $gtidInfos = [];
101  foreach ( $this->gtids as $gtid ) {
102  $m = [];
103  // MariaDB style: <domain>-<server id>-<sequence number>
104  if ( preg_match( '!^(\d+)-\d+-(\d+)$!', $gtid, $m ) ) {
105  $gtidInfos[(int)$m[1]] = (int)$m[2];
106  // MySQL style: <UUID domain>:<sequence number>
107  } elseif ( preg_match( '!^(\w{8}-\w{4}-\w{4}-\w{4}-\w{12}):(\d+)$!', $gtid, $m ) ) {
108  $gtidInfos[$m[1]] = (int)$m[2];
109  } else {
110  $gtidInfos = [];
111  break; // unrecognized GTID
112  }
113 
114  }
115 
116  return $gtidInfos;
117  }
118 
124  protected function getBinlogCoordinates() {
125  $m = [];
126  if ( preg_match( '!^(.+)\.(\d+)/(\d+)$!', (string)$this, $m ) ) {
127  return [ 'binlog' => $m[1], 'pos' => [ (int)$m[2], (int)$m[3] ] ];
128  }
129 
130  return false;
131  }
132 }
string[] $gtids
GTID list.
__construct($file, $pos, $gtid= '')
An object representing a master or replica DB position in a replicated setup.
Definition: DBMasterPos.php:7
channelsMatch(DBMasterPos $pos)
hasReached(DBMasterPos $pos)
DBMasterPos class for MySQL/MariaDB.
We ve cleaned up the code here by removing clumps of infrequently used code and moving them off somewhere else It s much easier for someone working with this code to see what s _really_ going and make changes or fix bugs In we can take all the code that deals with the little used title reversing we can concentrate it all in an extension file
Definition: hooks.txt:93
int $pos
Binglog file position.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
string $file
Binlog file.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
float $asOfTime
UNIX timestamp.