MediaWiki  1.28.0
DBLockManager.php
Go to the documentation of this file.
1 <?php
38 abstract class DBLockManager extends QuorumLockManager {
40  protected $dbServers; // (DB name => server config array)
42  protected $statusCache;
43 
44  protected $lockExpiry; // integer number of seconds
45  protected $safeDelay; // integer number of seconds
47  protected $conns = [];
48 
69  public function __construct( array $config ) {
70  parent::__construct( $config );
71 
72  $this->dbServers = $config['dbServers'];
73  // Sanitize srvsByBucket config to prevent PHP errors
74  $this->srvsByBucket = array_filter( $config['dbsByBucket'], 'is_array' );
75  $this->srvsByBucket = array_values( $this->srvsByBucket ); // consecutive
76 
77  if ( isset( $config['lockExpiry'] ) ) {
78  $this->lockExpiry = $config['lockExpiry'];
79  } else {
80  $met = ini_get( 'max_execution_time' );
81  $this->lockExpiry = $met ? $met : 60; // use some sane amount if 0
82  }
83  $this->safeDelay = ( $this->lockExpiry <= 0 )
84  ? 60 // pick a safe-ish number to match DB timeout default
85  : $this->lockExpiry; // cover worst case
86 
87  // Tracks peers that couldn't be queried recently to avoid lengthy
88  // connection timeouts. This is useless if each bucket has one peer.
89  $this->statusCache = isset( $config['srvCache'] )
90  ? $config['srvCache']
91  : new HashBagOStuff();
92  }
93 
100  protected function getLocksOnServer( $lockSrv, array $pathsByType ) {
102  foreach ( $pathsByType as $type => $paths ) {
103  $status->merge( $this->doGetLocksOnServer( $lockSrv, $paths, $type ) );
104  }
105 
106  return $status;
107  }
108 
109  abstract protected function doGetLocksOnServer( $lockSrv, array $paths, $type );
110 
111  protected function freeLocksOnServer( $lockSrv, array $pathsByType ) {
112  return StatusValue::newGood();
113  }
114 
120  protected function isServerUp( $lockSrv ) {
121  if ( !$this->cacheCheckFailures( $lockSrv ) ) {
122  return false; // recent failure to connect
123  }
124  try {
125  $this->getConnection( $lockSrv );
126  } catch ( DBError $e ) {
127  $this->cacheRecordFailure( $lockSrv );
128 
129  return false; // failed to connect
130  }
131 
132  return true;
133  }
134 
143  protected function getConnection( $lockDb ) {
144  if ( !isset( $this->conns[$lockDb] ) ) {
145  if ( $this->dbServers[$lockDb] instanceof IDatabase ) {
146  // Direct injected connection hande for $lockDB
147  $db = $this->dbServers[$lockDb];
148  } elseif ( is_array( $this->dbServers[$lockDb] ) ) {
149  // Parameters to construct a new database connection
150  $config = $this->dbServers[$lockDb];
151  $db = Database::factory( $config['type'], $config );
152  } else {
153  throw new UnexpectedValueException( "No server called '$lockDb'." );
154  }
155 
156  $db->clearFlag( DBO_TRX );
157  # If the connection drops, try to avoid letting the DB rollback
158  # and release the locks before the file operations are finished.
159  # This won't handle the case of DB server restarts however.
160  $options = [];
161  if ( $this->lockExpiry > 0 ) {
162  $options['connTimeout'] = $this->lockExpiry;
163  }
164  $db->setSessionOptions( $options );
165  $this->initConnection( $lockDb, $db );
166 
167  $this->conns[$lockDb] = $db;
168  }
169 
170  return $this->conns[$lockDb];
171  }
172 
180  protected function initConnection( $lockDb, IDatabase $db ) {
181  }
182 
190  protected function cacheCheckFailures( $lockDb ) {
191  return ( $this->safeDelay > 0 )
192  ? !$this->statusCache->get( $this->getMissKey( $lockDb ) )
193  : true;
194  }
195 
202  protected function cacheRecordFailure( $lockDb ) {
203  return ( $this->safeDelay > 0 )
204  ? $this->statusCache->set( $this->getMissKey( $lockDb ), 1, $this->safeDelay )
205  : true;
206  }
207 
214  protected function getMissKey( $lockDb ) {
215  return 'dblockmanager:downservers:' . str_replace( ' ', '_', $lockDb );
216  }
217 
221  function __destruct() {
222  $this->releaseAllLocks();
223  foreach ( $this->conns as $db ) {
224  $db->close();
225  }
226  }
227 }
__destruct()
Make sure remaining locks get cleared for sanity.
cacheRecordFailure($lockDb)
Log a lock request failure to the cache.
freeLocksOnServer($lockSrv, array $pathsByType)
Database error base class.
Definition: DBError.php:26
the array() calling protocol came about after MediaWiki 1.4rc1.
static factory($dbType, $p=[])
Construct a Database subclass instance given a database type and parameters.
Definition: Database.php:325
releaseAllLocks()
Release all locks that this session is holding.
cacheCheckFailures($lockDb)
Checks if the DB has not recently had connection/query errors.
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:2102
Version of LockManager based on using named/row DB locks.
getLocksOnServer($lockSrv, array $pathsByType)
change this code to work in one batch
BagOStuff $statusCache
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1046
isServerUp($lockSrv)
static newGood($value=null)
Factory function for good results.
Definition: StatusValue.php:76
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
doGetLocksOnServer($lockSrv, array $paths, $type)
const DBO_TRX
Definition: defines.php:9
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
array[] IDatabase[] $dbServers
Map of (DB names => server config or IDatabase)
__construct(array $config)
Construct a new instance from configuration.
getConnection($lockDb)
Get (or reuse) a connection to a lock DB.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1046
getMissKey($lockDb)
Get a cache key for recent query misses for a DB.
initConnection($lockDb, IDatabase $db)
Do additional initialization for new lock DB connection.
IDatabase[] $conns
Map Database connections (DB name => Database)
Version of LockManager that uses a quorum from peer servers for locks.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2491
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:34