MediaWiki  master
ReplicationReporter.php
Go to the documentation of this file.
1 <?php
21 
22 use BagOStuff;
23 use Psr\Log\LoggerInterface;
27 
35  protected $topologyRole;
37  protected $logger;
39  protected $srvCache;
41  private $trxReplicaLagStatus = null;
42 
43  public function __construct( $topologyRole, $logger, $srvCache ) {
44  $this->topologyRole = $topologyRole;
45  $this->logger = $logger;
46  $this->srvCache = $srvCache;
47  }
48 
49  public function getTopologyRole() {
50  return $this->topologyRole;
51  }
52 
53  public function getLag( IDatabase $conn ) {
54  if ( $this->topologyRole === IDatabase::ROLE_STREAMING_MASTER ) {
55  return 0; // this is the primary DB
56  } elseif ( $this->topologyRole === IDatabase::ROLE_STATIC_CLONE ) {
57  return 0; // static dataset
58  }
59 
60  return $this->doGetLag( $conn );
61  }
62 
74  protected function doGetLag( IDatabase $conn ) {
75  return 0;
76  }
77 
87  protected function getApproximateLagStatus( IDatabase $conn ) {
88  if ( $this->topologyRole === IDatabase::ROLE_STREAMING_REPLICA ) {
89  // Avoid exceptions as this is used internally in critical sections
90  try {
91  $lag = $this->getLag( $conn );
92  } catch ( DBError $e ) {
93  $lag = false;
94  }
95  } else {
96  $lag = 0;
97  }
98 
99  return [ 'lag' => $lag, 'since' => microtime( true ) ];
100  }
101 
102  public function primaryPosWait( IDatabase $conn, DBPrimaryPos $pos, $timeout ) {
103  // Real waits are implemented in the subclass.
104  return 0;
105  }
106 
107  public function getReplicaPos( IDatabase $conn ) {
108  // Stub
109  return false;
110  }
111 
112  public function getPrimaryPos( IDatabase $conn ) {
113  // Stub
114  return false;
115  }
116 
117  public function getTopologyBasedReadOnlyReason() {
118  if ( $this->topologyRole === IDatabase::ROLE_STREAMING_REPLICA ) {
119  return [ 'Server is configured as a read-only replica database.', 'role' ];
120  } elseif ( $this->topologyRole === IDatabase::ROLE_STATIC_CLONE ) {
121  return [ 'Server is configured as a read-only static clone database.', 'role' ];
122  }
123 
124  return [];
125  }
126 
127  public function resetReplicationLagStatus( IDatabase $conn ) {
128  // With REPEATABLE-READ isolation, the first SELECT establishes the read snapshot,
129  // so get the replication lag estimate before any transaction SELECT queries come in.
130  // This way, the lag estimate reflects what will actually be read. Also, if heartbeat
131  // tables are used, this avoids counting snapshot lag as part of replication lag.
132  $this->trxReplicaLagStatus = null; // clear cached value first
133  $this->trxReplicaLagStatus = $this->getApproximateLagStatus( $conn );
134  }
135 
151  final protected function getRecordedTransactionLagStatus( IDatabase $conn ) {
152  return $conn->trxLevel() ? $this->trxReplicaLagStatus : null;
153  }
154 
155  public function getSessionLagStatus( IDatabase $conn ) {
156  return $this->getRecordedTransactionLagStatus( $conn ) ?: $this->getApproximateLagStatus( $conn );
157  }
158 
166  protected function getLogContext( IDatabase $conn, array $extras = [] ) {
167  return array_merge(
168  [
169  'db_server' => $conn->getServerName(),
170  'db_name' => $conn->getDBname(),
171  // TODO: Add db_user
172  ],
173  $extras
174  );
175  }
176 
181  public function getTopologyBasedServerId( IDatabase $conn ) {
182  return null;
183  }
184 }
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:85
Database error base class.
Definition: DBError.php:31
primaryPosWait(IDatabase $conn, DBPrimaryPos $pos, $timeout)
getLogContext(IDatabase $conn, array $extras=[])
Create a log context to pass to PSR-3 logger functions.
__construct( $topologyRole, $logger, $srvCache)
doGetLag(IDatabase $conn)
Get the amount of replication lag for this database server.
getApproximateLagStatus(IDatabase $conn)
Get a replica DB lag estimate for this server at the start of a transaction.
string $topologyRole
Replication topology role of the server; one of the class ROLE_* constants.
getRecordedTransactionLagStatus(IDatabase $conn)
Get the replica DB lag when the current transaction started.
An object representing a primary or replica DB position in a replicated setup.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:36
trxLevel()
Gets the current transaction level.
getDBname()
Get the current database name; null if there isn't one.
getServerName()
Get the readable name for the server.