MediaWiki master
ReplicationReporter.php
Go to the documentation of this file.
1<?php
7
8use Psr\Log\LoggerInterface;
13
21 protected $topologyRole;
23 protected $logger;
25 protected $srvCache;
27 private $trxReplicaLagStatus = null;
28
35 $this->topologyRole = $topologyRole;
36 $this->logger = $logger;
37 $this->srvCache = $srvCache;
38 }
39
43 public function getTopologyRole() {
45 }
46
50 public function getLag( IDatabase $conn ) {
51 if ( $this->topologyRole === IDatabase::ROLE_STREAMING_MASTER ) {
52 return 0; // this is the primary DB
53 } elseif ( $this->topologyRole === IDatabase::ROLE_STATIC_CLONE ) {
54 return 0; // static dataset
55 }
56
57 return $this->doGetLag( $conn );
58 }
59
71 protected function doGetLag( IDatabase $conn ) {
72 return 0;
73 }
74
84 protected function getApproximateLagStatus( IDatabase $conn ) {
85 if ( $this->topologyRole === IDatabase::ROLE_STREAMING_REPLICA ) {
86 // Avoid exceptions as this is used internally in critical sections
87 try {
88 $lag = $this->getLag( $conn );
89 } catch ( DBError ) {
90 $lag = false;
91 }
92 } else {
93 $lag = 0;
94 }
95
96 return [ 'lag' => $lag, 'since' => microtime( true ) ];
97 }
98
105 public function primaryPosWait( IDatabase $conn, DBPrimaryPos $pos, $timeout ) {
106 // Real waits are implemented in the subclass.
107 return 0;
108 }
109
113 public function getReplicaPos( IDatabase $conn ) {
114 // Stub
115 return false;
116 }
117
121 public function getPrimaryPos( IDatabase $conn ) {
122 // Stub
123 return false;
124 }
125
130 if ( $this->topologyRole === IDatabase::ROLE_STREAMING_REPLICA ) {
131 return [ 'Server is configured as a read-only replica database.', 'role' ];
132 } elseif ( $this->topologyRole === IDatabase::ROLE_STATIC_CLONE ) {
133 return [ 'Server is configured as a read-only static clone database.', 'role' ];
134 }
135
136 return null;
137 }
138
139 public function resetReplicationLagStatus( IDatabase $conn ) {
140 // With REPEATABLE-READ isolation, the first SELECT establishes the read snapshot,
141 // so get the replication lag estimate before any transaction SELECT queries come in.
142 // This way, the lag estimate reflects what will actually be read. Also, if heartbeat
143 // tables are used, this avoids counting snapshot lag as part of replication lag.
144 $this->trxReplicaLagStatus = null; // clear cached value first
145 $this->trxReplicaLagStatus = $this->getApproximateLagStatus( $conn );
146 }
147
163 final protected function getRecordedTransactionLagStatus( IDatabase $conn ) {
164 return $conn->trxLevel() ? $this->trxReplicaLagStatus : null;
165 }
166
170 public function getSessionLagStatus( IDatabase $conn ) {
171 return $this->getRecordedTransactionLagStatus( $conn ) ?: $this->getApproximateLagStatus( $conn );
172 }
173
181 protected function getLogContext( IDatabase $conn, array $extras = [] ) {
182 return $extras + [
183 'db_server' => $conn->getServerName(),
184 'db_name' => $conn->getDBname(),
185 // TODO: Add db_user
186 ];
187 }
188}
Abstract class for any ephemeral data store.
Definition BagOStuff.php:73
Database error base class.
Definition DBError.php:22
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.
Interface to a relational database.
Definition IDatabase.php:31
const ROLE_STREAMING_MASTER
Primary server than can stream writes to replica servers.
Definition IDatabase.php:94
const ROLE_STATIC_CLONE
Replica server within a static dataset.
Definition IDatabase.php:98
const ROLE_STREAMING_REPLICA
Replica server that receives writes from a primary server.
Definition IDatabase.php:96
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.