Move up devunt's name to Developers
[lhc/web/wiklou.git] / includes / filebackend / lockmanager / DBLockManager.php
1 <?php
2 /**
3 * Version of LockManager based on using DB table locks.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup LockManager
22 */
23
24 /**
25 * Version of LockManager based on using named/row DB locks.
26 *
27 * This is meant for multi-wiki systems that may share files.
28 *
29 * All lock requests for a resource, identified by a hash string, will map
30 * to one bucket. Each bucket maps to one or several peer DBs, each on their
31 * own server, all having the filelocks.sql tables (with row-level locking).
32 * A majority of peer DBs must agree for a lock to be acquired.
33 *
34 * Caching is used to avoid hitting servers that are down.
35 *
36 * @ingroup LockManager
37 * @since 1.19
38 */
39 abstract class DBLockManager extends QuorumLockManager {
40 /** @var array Map of DB names to server config */
41 protected $dbServers; // (DB name => server config array)
42 /** @var BagOStuff */
43 protected $statusCache;
44
45 protected $lockExpiry; // integer number of seconds
46 protected $safeDelay; // integer number of seconds
47
48 protected $session = 0; // random integer
49 /** @var array Map Database connections (DB name => Database) */
50 protected $conns = array();
51
52 /**
53 * Construct a new instance from configuration.
54 *
55 * @param array $config Parameters include:
56 * - dbServers : Associative array of DB names to server configuration.
57 * Configuration is an associative array that includes:
58 * - host : DB server name
59 * - dbname : DB name
60 * - type : DB type (mysql,postgres,...)
61 * - user : DB user
62 * - password : DB user password
63 * - tablePrefix : DB table prefix
64 * - flags : DB flags (see DatabaseBase)
65 * - dbsByBucket : Array of 1-16 consecutive integer keys, starting from 0,
66 * each having an odd-numbered list of DB names (peers) as values.
67 * Any DB named 'localDBMaster' will automatically use the DB master
68 * settings for this wiki (without the need for a dbServers entry).
69 * Only use 'localDBMaster' if the domain is a valid wiki ID.
70 * - lockExpiry : Lock timeout (seconds) for dropped connections. [optional]
71 * This tells the DB server how long to wait before assuming
72 * connection failure and releasing all the locks for a session.
73 */
74 public function __construct( array $config ) {
75 parent::__construct( $config );
76
77 $this->dbServers = isset( $config['dbServers'] )
78 ? $config['dbServers']
79 : array(); // likely just using 'localDBMaster'
80 // Sanitize srvsByBucket config to prevent PHP errors
81 $this->srvsByBucket = array_filter( $config['dbsByBucket'], 'is_array' );
82 $this->srvsByBucket = array_values( $this->srvsByBucket ); // consecutive
83
84 if ( isset( $config['lockExpiry'] ) ) {
85 $this->lockExpiry = $config['lockExpiry'];
86 } else {
87 $met = ini_get( 'max_execution_time' );
88 $this->lockExpiry = $met ? $met : 60; // use some sane amount if 0
89 }
90 $this->safeDelay = ( $this->lockExpiry <= 0 )
91 ? 60 // pick a safe-ish number to match DB timeout default
92 : $this->lockExpiry; // cover worst case
93
94 foreach ( $this->srvsByBucket as $bucket ) {
95 if ( count( $bucket ) > 1 ) { // multiple peers
96 // Tracks peers that couldn't be queried recently to avoid lengthy
97 // connection timeouts. This is useless if each bucket has one peer.
98 try {
99 $this->statusCache = ObjectCache::newAccelerator( array() );
100 } catch ( Exception $e ) {
101 trigger_error( __CLASS__ .
102 " using multiple DB peers without apc, xcache, or wincache." );
103 }
104 break;
105 }
106 }
107
108 $this->session = wfRandomString( 31 );
109 }
110
111 // @todo change this code to work in one batch
112 protected function getLocksOnServer( $lockSrv, array $pathsByType ) {
113 $status = Status::newGood();
114 foreach ( $pathsByType as $type => $paths ) {
115 $status->merge( $this->doGetLocksOnServer( $lockSrv, $paths, $type ) );
116 }
117
118 return $status;
119 }
120
121 protected function freeLocksOnServer( $lockSrv, array $pathsByType ) {
122 return Status::newGood();
123 }
124
125 /**
126 * @see QuorumLockManager::isServerUp()
127 * @param string $lockSrv
128 * @return bool
129 */
130 protected function isServerUp( $lockSrv ) {
131 if ( !$this->cacheCheckFailures( $lockSrv ) ) {
132 return false; // recent failure to connect
133 }
134 try {
135 $this->getConnection( $lockSrv );
136 } catch ( DBError $e ) {
137 $this->cacheRecordFailure( $lockSrv );
138
139 return false; // failed to connect
140 }
141
142 return true;
143 }
144
145 /**
146 * Get (or reuse) a connection to a lock DB
147 *
148 * @param string $lockDb
149 * @return DatabaseBase
150 * @throws DBError
151 */
152 protected function getConnection( $lockDb ) {
153 if ( !isset( $this->conns[$lockDb] ) ) {
154 $db = null;
155 if ( $lockDb === 'localDBMaster' ) {
156 $lb = wfGetLBFactory()->getMainLB( $this->domain );
157 $db = $lb->getConnection( DB_MASTER, array(), $this->domain );
158 } elseif ( isset( $this->dbServers[$lockDb] ) ) {
159 $config = $this->dbServers[$lockDb];
160 $db = DatabaseBase::factory( $config['type'], $config );
161 }
162 if ( !$db ) {
163 return null; // config error?
164 }
165 $this->conns[$lockDb] = $db;
166 $this->conns[$lockDb]->clearFlag( DBO_TRX );
167 # If the connection drops, try to avoid letting the DB rollback
168 # and release the locks before the file operations are finished.
169 # This won't handle the case of DB server restarts however.
170 $options = array();
171 if ( $this->lockExpiry > 0 ) {
172 $options['connTimeout'] = $this->lockExpiry;
173 }
174 $this->conns[$lockDb]->setSessionOptions( $options );
175 $this->initConnection( $lockDb, $this->conns[$lockDb] );
176 }
177 if ( !$this->conns[$lockDb]->trxLevel() ) {
178 $this->conns[$lockDb]->begin( __METHOD__ ); // start transaction
179 }
180
181 return $this->conns[$lockDb];
182 }
183
184 /**
185 * Do additional initialization for new lock DB connection
186 *
187 * @param string $lockDb
188 * @param DatabaseBase $db
189 * @throws DBError
190 */
191 protected function initConnection( $lockDb, DatabaseBase $db ) {
192 }
193
194 /**
195 * Checks if the DB has not recently had connection/query errors.
196 * This just avoids wasting time on doomed connection attempts.
197 *
198 * @param string $lockDb
199 * @return bool
200 */
201 protected function cacheCheckFailures( $lockDb ) {
202 return ( $this->statusCache && $this->safeDelay > 0 )
203 ? !$this->statusCache->get( $this->getMissKey( $lockDb ) )
204 : true;
205 }
206
207 /**
208 * Log a lock request failure to the cache
209 *
210 * @param string $lockDb
211 * @return bool Success
212 */
213 protected function cacheRecordFailure( $lockDb ) {
214 return ( $this->statusCache && $this->safeDelay > 0 )
215 ? $this->statusCache->set( $this->getMissKey( $lockDb ), 1, $this->safeDelay )
216 : true;
217 }
218
219 /**
220 * Get a cache key for recent query misses for a DB
221 *
222 * @param string $lockDb
223 * @return string
224 */
225 protected function getMissKey( $lockDb ) {
226 $lockDb = ( $lockDb === 'localDBMaster' ) ? wfWikiID() : $lockDb; // non-relative
227 return 'dblockmanager:downservers:' . str_replace( ' ', '_', $lockDb );
228 }
229
230 /**
231 * Make sure remaining locks get cleared for sanity
232 */
233 function __destruct() {
234 $this->releaseAllLocks();
235 foreach ( $this->conns as $db ) {
236 $db->close();
237 }
238 }
239 }
240
241 /**
242 * MySQL version of DBLockManager that supports shared locks.
243 * All locks are non-blocking, which avoids deadlocks.
244 *
245 * @ingroup LockManager
246 */
247 class MySqlLockManager extends DBLockManager {
248 /** @var array Mapping of lock types to the type actually used */
249 protected $lockTypeMap = array(
250 self::LOCK_SH => self::LOCK_SH,
251 self::LOCK_UW => self::LOCK_SH,
252 self::LOCK_EX => self::LOCK_EX
253 );
254
255 /**
256 * @param string $lockDb
257 * @param DatabaseBase $db
258 */
259 protected function initConnection( $lockDb, DatabaseBase $db ) {
260 # Let this transaction see lock rows from other transactions
261 $db->query( "SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;" );
262 }
263
264 /**
265 * Get a connection to a lock DB and acquire locks on $paths.
266 * This does not use GET_LOCK() per http://bugs.mysql.com/bug.php?id=1118.
267 *
268 * @see DBLockManager::getLocksOnServer()
269 * @param string $lockSrv
270 * @param array $paths
271 * @param string $type
272 * @return Status
273 */
274 protected function doGetLocksOnServer( $lockSrv, array $paths, $type ) {
275 $status = Status::newGood();
276
277 $db = $this->getConnection( $lockSrv ); // checked in isServerUp()
278
279 $keys = array(); // list of hash keys for the paths
280 $data = array(); // list of rows to insert
281 $checkEXKeys = array(); // list of hash keys that this has no EX lock on
282 # Build up values for INSERT clause
283 foreach ( $paths as $path ) {
284 $key = $this->sha1Base36Absolute( $path );
285 $keys[] = $key;
286 $data[] = array( 'fls_key' => $key, 'fls_session' => $this->session );
287 if ( !isset( $this->locksHeld[$path][self::LOCK_EX] ) ) {
288 $checkEXKeys[] = $key;
289 }
290 }
291
292 # Block new writers (both EX and SH locks leave entries here)...
293 $db->insert( 'filelocks_shared', $data, __METHOD__, array( 'IGNORE' ) );
294 # Actually do the locking queries...
295 if ( $type == self::LOCK_SH ) { // reader locks
296 $blocked = false;
297 # Bail if there are any existing writers...
298 if ( count( $checkEXKeys ) ) {
299 $blocked = $db->selectField( 'filelocks_exclusive', '1',
300 array( 'fle_key' => $checkEXKeys ),
301 __METHOD__
302 );
303 }
304 # Other prospective writers that haven't yet updated filelocks_exclusive
305 # will recheck filelocks_shared after doing so and bail due to this entry.
306 } else { // writer locks
307 $encSession = $db->addQuotes( $this->session );
308 # Bail if there are any existing writers...
309 # This may detect readers, but the safe check for them is below.
310 # Note: if two writers come at the same time, both bail :)
311 $blocked = $db->selectField( 'filelocks_shared', '1',
312 array( 'fls_key' => $keys, "fls_session != $encSession" ),
313 __METHOD__
314 );
315 if ( !$blocked ) {
316 # Build up values for INSERT clause
317 $data = array();
318 foreach ( $keys as $key ) {
319 $data[] = array( 'fle_key' => $key );
320 }
321 # Block new readers/writers...
322 $db->insert( 'filelocks_exclusive', $data, __METHOD__ );
323 # Bail if there are any existing readers...
324 $blocked = $db->selectField( 'filelocks_shared', '1',
325 array( 'fls_key' => $keys, "fls_session != $encSession" ),
326 __METHOD__
327 );
328 }
329 }
330
331 if ( $blocked ) {
332 foreach ( $paths as $path ) {
333 $status->fatal( 'lockmanager-fail-acquirelock', $path );
334 }
335 }
336
337 return $status;
338 }
339
340 /**
341 * @see QuorumLockManager::releaseAllLocks()
342 * @return Status
343 */
344 protected function releaseAllLocks() {
345 $status = Status::newGood();
346
347 foreach ( $this->conns as $lockDb => $db ) {
348 if ( $db->trxLevel() ) { // in transaction
349 try {
350 $db->rollback( __METHOD__ ); // finish transaction and kill any rows
351 } catch ( DBError $e ) {
352 $status->fatal( 'lockmanager-fail-db-release', $lockDb );
353 }
354 }
355 }
356
357 return $status;
358 }
359 }
360
361 /**
362 * PostgreSQL version of DBLockManager that supports shared locks.
363 * All locks are non-blocking, which avoids deadlocks.
364 *
365 * @ingroup LockManager
366 */
367 class PostgreSqlLockManager extends DBLockManager {
368 /** @var array Mapping of lock types to the type actually used */
369 protected $lockTypeMap = array(
370 self::LOCK_SH => self::LOCK_SH,
371 self::LOCK_UW => self::LOCK_SH,
372 self::LOCK_EX => self::LOCK_EX
373 );
374
375 protected function doGetLocksOnServer( $lockSrv, array $paths, $type ) {
376 $status = Status::newGood();
377 if ( !count( $paths ) ) {
378 return $status; // nothing to lock
379 }
380
381 $db = $this->getConnection( $lockSrv ); // checked in isServerUp()
382 $bigints = array_unique( array_map(
383 function ( $key ) {
384 return wfBaseConvert( substr( $key, 0, 15 ), 16, 10 );
385 },
386 array_map( array( $this, 'sha1Base16Absolute' ), $paths )
387 ) );
388
389 // Try to acquire all the locks...
390 $fields = array();
391 foreach ( $bigints as $bigint ) {
392 $fields[] = ( $type == self::LOCK_SH )
393 ? "pg_try_advisory_lock_shared({$db->addQuotes( $bigint )}) AS K$bigint"
394 : "pg_try_advisory_lock({$db->addQuotes( $bigint )}) AS K$bigint";
395 }
396 $res = $db->query( 'SELECT ' . implode( ', ', $fields ), __METHOD__ );
397 $row = (array)$res->fetchObject();
398
399 if ( in_array( 'f', $row ) ) {
400 // Release any acquired locks if some could not be acquired...
401 $fields = array();
402 foreach ( $row as $kbigint => $ok ) {
403 if ( $ok === 't' ) { // locked
404 $bigint = substr( $kbigint, 1 ); // strip off the "K"
405 $fields[] = ( $type == self::LOCK_SH )
406 ? "pg_advisory_unlock_shared({$db->addQuotes( $bigint )})"
407 : "pg_advisory_unlock({$db->addQuotes( $bigint )})";
408 }
409 }
410 if ( count( $fields ) ) {
411 $db->query( 'SELECT ' . implode( ', ', $fields ), __METHOD__ );
412 }
413 foreach ( $paths as $path ) {
414 $status->fatal( 'lockmanager-fail-acquirelock', $path );
415 }
416 }
417
418 return $status;
419 }
420
421 /**
422 * @see QuorumLockManager::releaseAllLocks()
423 * @return Status
424 */
425 protected function releaseAllLocks() {
426 $status = Status::newGood();
427
428 foreach ( $this->conns as $lockDb => $db ) {
429 try {
430 $db->query( "SELECT pg_advisory_unlock_all()", __METHOD__ );
431 } catch ( DBError $e ) {
432 $status->fatal( 'lockmanager-fail-db-release', $lockDb );
433 }
434 }
435
436 return $status;
437 }
438 }