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 
120  public function getTopologyBasedReadOnlyReason() {
121  if ( $this->topologyRole === IDatabase::ROLE_STREAMING_REPLICA ) {
122  return [ 'Server is configured as a read-only replica database.', 'role' ];
123  } elseif ( $this->topologyRole === IDatabase::ROLE_STATIC_CLONE ) {
124  return [ 'Server is configured as a read-only static clone database.', 'role' ];
125  }
126 
127  return null;
128  }
129 
130  public function resetReplicationLagStatus( IDatabase $conn ) {
131  // With REPEATABLE-READ isolation, the first SELECT establishes the read snapshot,
132  // so get the replication lag estimate before any transaction SELECT queries come in.
133  // This way, the lag estimate reflects what will actually be read. Also, if heartbeat
134  // tables are used, this avoids counting snapshot lag as part of replication lag.
135  $this->trxReplicaLagStatus = null; // clear cached value first
136  $this->trxReplicaLagStatus = $this->getApproximateLagStatus( $conn );
137  }
138 
154  final protected function getRecordedTransactionLagStatus( IDatabase $conn ) {
155  return $conn->trxLevel() ? $this->trxReplicaLagStatus : null;
156  }
157 
158  public function getSessionLagStatus( IDatabase $conn ) {
159  return $this->getRecordedTransactionLagStatus( $conn ) ?: $this->getApproximateLagStatus( $conn );
160  }
161 
169  protected function getLogContext( IDatabase $conn, array $extras = [] ) {
170  return array_merge(
171  [
172  'db_server' => $conn->getServerName(),
173  'db_name' => $conn->getDBname(),
174  // TODO: Add db_user
175  ],
176  $extras
177  );
178  }
179 
184  public function getTopologyBasedServerId( IDatabase $conn ) {
185  return null;
186  }
187 }
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:85
Database error base class.
Definition: DBError.php:37
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.