MediaWiki  1.29.1
DBLockManager.php
Go to the documentation of this file.
1 <?php
27 
42 abstract class DBLockManager extends QuorumLockManager {
44  protected $dbServers; // (DB name => server config array)
46  protected $statusCache;
47 
48  protected $lockExpiry; // integer number of seconds
49  protected $safeDelay; // integer number of seconds
51  protected $conns = [];
52 
73  public function __construct( array $config ) {
74  parent::__construct( $config );
75 
76  $this->dbServers = $config['dbServers'];
77  // Sanitize srvsByBucket config to prevent PHP errors
78  $this->srvsByBucket = array_filter( $config['dbsByBucket'], 'is_array' );
79  $this->srvsByBucket = array_values( $this->srvsByBucket ); // consecutive
80 
81  if ( isset( $config['lockExpiry'] ) ) {
82  $this->lockExpiry = $config['lockExpiry'];
83  } else {
84  $met = ini_get( 'max_execution_time' );
85  $this->lockExpiry = $met ? $met : 60; // use some sane amount if 0
86  }
87  $this->safeDelay = ( $this->lockExpiry <= 0 )
88  ? 60 // pick a safe-ish number to match DB timeout default
89  : $this->lockExpiry; // cover worst case
90 
91  // Tracks peers that couldn't be queried recently to avoid lengthy
92  // connection timeouts. This is useless if each bucket has one peer.
93  $this->statusCache = isset( $config['srvCache'] )
94  ? $config['srvCache']
95  : new HashBagOStuff();
96  }
97 
104  protected function getLocksOnServer( $lockSrv, array $pathsByType ) {
106  foreach ( $pathsByType as $type => $paths ) {
107  $status->merge( $this->doGetLocksOnServer( $lockSrv, $paths, $type ) );
108  }
109 
110  return $status;
111  }
112 
113  abstract protected function doGetLocksOnServer( $lockSrv, array $paths, $type );
114 
115  protected function freeLocksOnServer( $lockSrv, array $pathsByType ) {
116  return StatusValue::newGood();
117  }
118 
124  protected function isServerUp( $lockSrv ) {
125  if ( !$this->cacheCheckFailures( $lockSrv ) ) {
126  return false; // recent failure to connect
127  }
128  try {
129  $this->getConnection( $lockSrv );
130  } catch ( DBError $e ) {
131  $this->cacheRecordFailure( $lockSrv );
132 
133  return false; // failed to connect
134  }
135 
136  return true;
137  }
138 
147  protected function getConnection( $lockDb ) {
148  if ( !isset( $this->conns[$lockDb] ) ) {
149  if ( $this->dbServers[$lockDb] instanceof IDatabase ) {
150  // Direct injected connection hande for $lockDB
151  $db = $this->dbServers[$lockDb];
152  } elseif ( is_array( $this->dbServers[$lockDb] ) ) {
153  // Parameters to construct a new database connection
154  $config = $this->dbServers[$lockDb];
155  $db = Database::factory( $config['type'], $config );
156  } else {
157  throw new UnexpectedValueException( "No server called '$lockDb'." );
158  }
159 
160  $db->clearFlag( DBO_TRX );
161  # If the connection drops, try to avoid letting the DB rollback
162  # and release the locks before the file operations are finished.
163  # This won't handle the case of DB server restarts however.
164  $options = [];
165  if ( $this->lockExpiry > 0 ) {
166  $options['connTimeout'] = $this->lockExpiry;
167  }
168  $db->setSessionOptions( $options );
169  $this->initConnection( $lockDb, $db );
170 
171  $this->conns[$lockDb] = $db;
172  }
173 
174  return $this->conns[$lockDb];
175  }
176 
184  protected function initConnection( $lockDb, IDatabase $db ) {
185  }
186 
194  protected function cacheCheckFailures( $lockDb ) {
195  return ( $this->safeDelay > 0 )
196  ? !$this->statusCache->get( $this->getMissKey( $lockDb ) )
197  : true;
198  }
199 
206  protected function cacheRecordFailure( $lockDb ) {
207  return ( $this->safeDelay > 0 )
208  ? $this->statusCache->set( $this->getMissKey( $lockDb ), 1, $this->safeDelay )
209  : true;
210  }
211 
218  protected function getMissKey( $lockDb ) {
219  return 'dblockmanager:downservers:' . str_replace( ' ', '_', $lockDb );
220  }
221 
225  function __destruct() {
226  $this->releaseAllLocks();
227  foreach ( $this->conns as $db ) {
228  $db->close();
229  }
230  }
231 }
Wikimedia\Rdbms\Database
Relational database abstraction object.
Definition: Database.php:45
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:31
DBLockManager\isServerUp
isServerUp( $lockSrv)
Definition: DBLockManager.php:124
$status
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup 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:1049
DBLockManager\cacheRecordFailure
cacheRecordFailure( $lockDb)
Log a lock request failure to the cache.
Definition: DBLockManager.php:206
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
DBLockManager\__construct
__construct(array $config)
Construct a new instance from configuration.
Definition: DBLockManager.php:73
DBLockManager\getLocksOnServer
getLocksOnServer( $lockSrv, array $pathsByType)
@TODO change this code to work in one batch
Definition: DBLockManager.php:104
BagOStuff
interface is intended to be more or less compatible with the PHP memcached client.
Definition: BagOStuff.php:47
DBLockManager\$safeDelay
$safeDelay
Definition: DBLockManager.php:49
$type
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 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:2536
Wikimedia\Rdbms\DBError
Database error base class.
Definition: DBError.php:30
DBO_TRX
const DBO_TRX
Definition: defines.php:12
php
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
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:40
DBLockManager\cacheCheckFailures
cacheCheckFailures( $lockDb)
Checks if the DB has not recently had connection/query errors.
Definition: DBLockManager.php:194
QuorumLockManager
Version of LockManager that uses a quorum from peer servers for locks.
Definition: QuorumLockManager.php:31
QuorumLockManager\releaseAllLocks
releaseAllLocks()
Release all locks that this session is holding.
DBLockManager\getMissKey
getMissKey( $lockDb)
Get a cache key for recent query misses for a DB.
Definition: DBLockManager.php:218
DBLockManager\doGetLocksOnServer
doGetLocksOnServer( $lockSrv, array $paths, $type)
DBLockManager\initConnection
initConnection( $lockDb, IDatabase $db)
Do additional initialization for new lock DB connection.
Definition: DBLockManager.php:184
DBLockManager\$dbServers
array[] IDatabase[] $dbServers
Map of (DB names => server config or IDatabase)
Definition: DBLockManager.php:44
DBLockManager\$conns
IDatabase[] $conns
Map Database connections (DB name => Database)
Definition: DBLockManager.php:51
DBLockManager\__destruct
__destruct()
Make sure remaining locks get cleared for sanity.
Definition: DBLockManager.php:225
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2122
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:76
DBLockManager\$lockExpiry
$lockExpiry
Definition: DBLockManager.php:48
DBLockManager\$statusCache
BagOStuff $statusCache
Definition: DBLockManager.php:46
as
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
DBLockManager\getConnection
getConnection( $lockDb)
Get (or reuse) a connection to a lock DB.
Definition: DBLockManager.php:147
DBLockManager
Version of LockManager based on using named/row DB locks.
Definition: DBLockManager.php:42
$options
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup 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:1049
array
the array() calling protocol came about after MediaWiki 1.4rc1.
DBLockManager\freeLocksOnServer
freeLocksOnServer( $lockSrv, array $pathsByType)
Get a connection to a lock server and release locks on $paths.
Definition: DBLockManager.php:115