3 * Version of LockManager based on using DB table locks.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup LockManager
25 * Version of LockManager based on using named/row DB locks.
27 * This is meant for multi-wiki systems that may share files.
29 * All lock requests for a resource, identified by a hash string, will map to one bucket.
30 * Each bucket maps to one or several peer DBs, each on their own server.
31 * A majority of peer DBs must agree for a lock to be acquired.
33 * Caching is used to avoid hitting servers that are down.
35 * @ingroup LockManager
38 abstract class DBLockManager
extends QuorumLockManager
{
39 /** @var array[]|IDatabase[] Map of (DB names => server config or IDatabase) */
40 protected $dbServers; // (DB name => server config array)
42 protected $statusCache;
44 protected $lockExpiry; // integer number of seconds
45 protected $safeDelay; // integer number of seconds
46 /** @var IDatabase[] Map Database connections (DB name => Database) */
47 protected $conns = [];
50 * Construct a new instance from configuration.
52 * @param array $config Parameters include:
53 * - dbServers : Associative array of DB names to server configuration.
54 * Configuration is an associative array that includes:
55 * - host : DB server name
57 * - type : DB type (mysql,postgres,...)
59 * - password : DB user password
60 * - tablePrefix : DB table prefix
61 * - flags : DB flags; bitfield of IDatabase::DBO_* constants
62 * - dbsByBucket : Array of 1-16 consecutive integer keys, starting from 0,
63 * each having an odd-numbered list of DB names (peers) as values.
64 * - lockExpiry : Lock timeout (seconds) for dropped connections. [optional]
65 * This tells the DB server how long to wait before assuming
66 * connection failure and releasing all the locks for a session.
67 * - srvCache : A BagOStuff instance using APC or the like.
69 public function __construct( array $config ) {
70 parent
::__construct( $config );
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
77 if ( isset( $config['lockExpiry'] ) ) {
78 $this->lockExpiry
= $config['lockExpiry'];
80 $met = ini_get( 'max_execution_time' );
81 $this->lockExpiry
= $met ?
$met : 60; // use some sane amount if 0
83 $this->safeDelay
= ( $this->lockExpiry
<= 0 )
84 ?
60 // pick a safe-ish number to match DB timeout default
85 : $this->lockExpiry
; // cover worst case
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'] )
91 : new HashBagOStuff();
95 * @TODO change this code to work in one batch
96 * @param string $lockSrv
97 * @param array $pathsByType
100 protected function getLocksOnServer( $lockSrv, array $pathsByType ) {
101 $status = StatusValue
::newGood();
102 foreach ( $pathsByType as $type => $paths ) {
103 $status->merge( $this->doGetLocksOnServer( $lockSrv, $paths, $type ) );
109 abstract protected function doGetLocksOnServer( $lockSrv, array $paths, $type );
111 protected function freeLocksOnServer( $lockSrv, array $pathsByType ) {
112 return StatusValue
::newGood();
116 * @see QuorumLockManager::isServerUp()
117 * @param string $lockSrv
120 protected function isServerUp( $lockSrv ) {
121 if ( !$this->cacheCheckFailures( $lockSrv ) ) {
122 return false; // recent failure to connect
125 $this->getConnection( $lockSrv );
126 } catch ( DBError
$e ) {
127 $this->cacheRecordFailure( $lockSrv );
129 return false; // failed to connect
136 * Get (or reuse) a connection to a lock DB
138 * @param string $lockDb
141 * @throws UnexpectedValueException
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 );
153 throw new UnexpectedValueException( "No server called '$lockDb'." );
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.
161 if ( $this->lockExpiry
> 0 ) {
162 $options['connTimeout'] = $this->lockExpiry
;
164 $db->setSessionOptions( $options );
165 $this->initConnection( $lockDb, $db );
167 $this->conns
[$lockDb] = $db;
170 return $this->conns
[$lockDb];
174 * Do additional initialization for new lock DB connection
176 * @param string $lockDb
177 * @param IDatabase $db
180 protected function initConnection( $lockDb, IDatabase
$db ) {
184 * Checks if the DB has not recently had connection/query errors.
185 * This just avoids wasting time on doomed connection attempts.
187 * @param string $lockDb
190 protected function cacheCheckFailures( $lockDb ) {
191 return ( $this->safeDelay
> 0 )
192 ?
!$this->statusCache
->get( $this->getMissKey( $lockDb ) )
197 * Log a lock request failure to the cache
199 * @param string $lockDb
200 * @return bool Success
202 protected function cacheRecordFailure( $lockDb ) {
203 return ( $this->safeDelay
> 0 )
204 ?
$this->statusCache
->set( $this->getMissKey( $lockDb ), 1, $this->safeDelay
)
209 * Get a cache key for recent query misses for a DB
211 * @param string $lockDb
214 protected function getMissKey( $lockDb ) {
215 return 'dblockmanager:downservers:' . str_replace( ' ', '_', $lockDb );
219 * Make sure remaining locks get cleared for sanity
221 function __destruct() {
222 $this->releaseAllLocks();
223 foreach ( $this->conns
as $db ) {