2011-12-20 03:52:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Version of LockManager based on using DB table locks.
|
|
|
|
|
* This is meant for multi-wiki systems that may share files.
|
|
|
|
|
* All locks are blocking, so it might be useful to set a small
|
|
|
|
|
* lock-wait timeout via server config to curtail deadlocks.
|
|
|
|
|
*
|
|
|
|
|
* All lock requests for a resource, identified by a hash string, will map
|
|
|
|
|
* to one bucket. Each bucket maps to one or several peer DBs, each on their
|
|
|
|
|
* own server, all having the filelocks.sql tables (with row-level locking).
|
|
|
|
|
* A majority of peer DBs must agree for a lock to be acquired.
|
|
|
|
|
*
|
|
|
|
|
* Caching is used to avoid hitting servers that are down.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup LockManager
|
2012-01-13 23:30:46 +00:00
|
|
|
* @since 1.19
|
2011-12-20 03:52:06 +00:00
|
|
|
*/
|
|
|
|
|
class DBLockManager extends LockManager {
|
|
|
|
|
/** @var Array Map of DB names to server config */
|
|
|
|
|
protected $dbServers; // (DB name => server config array)
|
|
|
|
|
/** @var Array Map of bucket indexes to peer DB lists */
|
|
|
|
|
protected $dbsByBucket; // (bucket index => (ldb1, ldb2, ...))
|
|
|
|
|
/** @var BagOStuff */
|
|
|
|
|
protected $statusCache;
|
|
|
|
|
|
|
|
|
|
protected $lockExpiry; // integer number of seconds
|
|
|
|
|
protected $safeDelay; // integer number of seconds
|
|
|
|
|
|
|
|
|
|
protected $session = 0; // random integer
|
|
|
|
|
/** @var Array Map Database connections (DB name => Database) */
|
|
|
|
|
protected $conns = array();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct a new instance from configuration.
|
2012-01-07 03:46:54 +00:00
|
|
|
*
|
2011-12-20 03:52:06 +00:00
|
|
|
* $config paramaters include:
|
|
|
|
|
* 'dbServers' : Associative array of DB names to server configuration.
|
|
|
|
|
* Configuration is an associative array that includes:
|
|
|
|
|
* 'host' - DB server name
|
|
|
|
|
* 'dbname' - DB name
|
|
|
|
|
* 'type' - DB type (mysql,postgres,...)
|
|
|
|
|
* 'user' - DB user
|
|
|
|
|
* 'password' - DB user password
|
|
|
|
|
* 'tablePrefix' - DB table prefix
|
|
|
|
|
* 'flags' - DB flags (see DatabaseBase)
|
|
|
|
|
* 'dbsByBucket' : Array of 1-16 consecutive integer keys, starting from 0,
|
|
|
|
|
* each having an odd-numbered list of DB names (peers) as values.
|
|
|
|
|
* Any DB named 'localDBMaster' will automatically use the DB master
|
|
|
|
|
* settings for this wiki (without the need for a dbServers entry).
|
|
|
|
|
* 'lockExpiry' : Lock timeout (seconds) for dropped connections. [optional]
|
|
|
|
|
* This tells the DB server how long to wait before assuming
|
|
|
|
|
* connection failure and releasing all the locks for a session.
|
|
|
|
|
*
|
|
|
|
|
* @param Array $config
|
|
|
|
|
*/
|
|
|
|
|
public function __construct( array $config ) {
|
2012-03-04 21:08:55 +00:00
|
|
|
parent::__construct( $config );
|
|
|
|
|
|
2012-01-14 03:16:18 +00:00
|
|
|
$this->dbServers = isset( $config['dbServers'] )
|
|
|
|
|
? $config['dbServers']
|
|
|
|
|
: array(); // likely just using 'localDBMaster'
|
2011-12-20 03:52:06 +00:00
|
|
|
// Sanitize dbsByBucket config to prevent PHP errors
|
|
|
|
|
$this->dbsByBucket = array_filter( $config['dbsByBucket'], 'is_array' );
|
|
|
|
|
$this->dbsByBucket = array_values( $this->dbsByBucket ); // consecutive
|
|
|
|
|
|
|
|
|
|
if ( isset( $config['lockExpiry'] ) ) {
|
|
|
|
|
$this->lockExpiry = $config['lockExpiry'];
|
|
|
|
|
} else {
|
|
|
|
|
$met = ini_get( 'max_execution_time' );
|
|
|
|
|
$this->lockExpiry = $met ? $met : 60; // use some sane amount if 0
|
|
|
|
|
}
|
|
|
|
|
$this->safeDelay = ( $this->lockExpiry <= 0 )
|
|
|
|
|
? 60 // pick a safe-ish number to match DB timeout default
|
|
|
|
|
: $this->lockExpiry; // cover worst case
|
|
|
|
|
|
|
|
|
|
foreach ( $this->dbsByBucket as $bucket ) {
|
|
|
|
|
if ( count( $bucket ) > 1 ) {
|
|
|
|
|
// Tracks peers that couldn't be queried recently to avoid lengthy
|
|
|
|
|
// connection timeouts. This is useless if each bucket has one peer.
|
|
|
|
|
$this->statusCache = wfGetMainCache();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->session = '';
|
|
|
|
|
for ( $i = 0; $i < 5; $i++ ) {
|
|
|
|
|
$this->session .= mt_rand( 0, 2147483647 );
|
|
|
|
|
}
|
|
|
|
|
$this->session = wfBaseConvert( sha1( $this->session ), 16, 36, 31 );
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-24 00:16:06 +00:00
|
|
|
/**
|
|
|
|
|
* @see LockManager::doLock()
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return Status
|
2011-12-24 00:16:06 +00:00
|
|
|
*/
|
|
|
|
|
protected function doLock( array $paths, $type ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
$status = Status::newGood();
|
|
|
|
|
|
2011-12-24 00:16:06 +00:00
|
|
|
$pathsToLock = array();
|
2011-12-20 03:52:06 +00:00
|
|
|
// Get locks that need to be acquired (buckets => locks)...
|
2011-12-24 00:16:06 +00:00
|
|
|
foreach ( $paths as $path ) {
|
|
|
|
|
if ( isset( $this->locksHeld[$path][$type] ) ) {
|
|
|
|
|
++$this->locksHeld[$path][$type];
|
|
|
|
|
} elseif ( isset( $this->locksHeld[$path][self::LOCK_EX] ) ) {
|
|
|
|
|
$this->locksHeld[$path][$type] = 1;
|
2011-12-20 03:52:06 +00:00
|
|
|
} else {
|
2011-12-24 00:16:06 +00:00
|
|
|
$bucket = $this->getBucketFromKey( $path );
|
|
|
|
|
$pathsToLock[$bucket][] = $path;
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-24 00:16:06 +00:00
|
|
|
$lockedPaths = array(); // files locked in this attempt
|
2011-12-20 03:52:06 +00:00
|
|
|
// Attempt to acquire these locks...
|
2011-12-24 00:16:06 +00:00
|
|
|
foreach ( $pathsToLock as $bucket => $paths ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
// Try to acquire the locks for this bucket
|
2011-12-24 00:16:06 +00:00
|
|
|
$res = $this->doLockingQueryAll( $bucket, $paths, $type );
|
2011-12-20 03:52:06 +00:00
|
|
|
if ( $res === 'cantacquire' ) {
|
|
|
|
|
// Resources already locked by another process.
|
|
|
|
|
// Abort and unlock everything we just locked.
|
2011-12-24 00:43:06 +00:00
|
|
|
foreach ( $paths as $path ) {
|
|
|
|
|
$status->fatal( 'lockmanager-fail-acquirelock', $path );
|
|
|
|
|
}
|
2011-12-24 00:16:06 +00:00
|
|
|
$status->merge( $this->doUnlock( $lockedPaths, $type ) );
|
2011-12-20 03:52:06 +00:00
|
|
|
return $status;
|
|
|
|
|
} elseif ( $res !== true ) {
|
|
|
|
|
// Couldn't contact any DBs for this bucket.
|
|
|
|
|
// Abort and unlock everything we just locked.
|
|
|
|
|
$status->fatal( 'lockmanager-fail-db-bucket', $bucket );
|
2011-12-24 00:16:06 +00:00
|
|
|
$status->merge( $this->doUnlock( $lockedPaths, $type ) );
|
2011-12-20 03:52:06 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
// Record these locks as active
|
2011-12-24 00:16:06 +00:00
|
|
|
foreach ( $paths as $path ) {
|
|
|
|
|
$this->locksHeld[$path][$type] = 1; // locked
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
// Keep track of what locks were made in this attempt
|
2011-12-24 00:16:06 +00:00
|
|
|
$lockedPaths = array_merge( $lockedPaths, $paths );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-24 00:16:06 +00:00
|
|
|
/**
|
|
|
|
|
* @see LockManager::doUnlock()
|
2012-02-09 21:33:27 +00:00
|
|
|
* @return Status
|
2011-12-24 00:16:06 +00:00
|
|
|
*/
|
|
|
|
|
protected function doUnlock( array $paths, $type ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
$status = Status::newGood();
|
|
|
|
|
|
2011-12-24 00:16:06 +00:00
|
|
|
foreach ( $paths as $path ) {
|
|
|
|
|
if ( !isset( $this->locksHeld[$path] ) ) {
|
|
|
|
|
$status->warning( 'lockmanager-notlocked', $path );
|
|
|
|
|
} elseif ( !isset( $this->locksHeld[$path][$type] ) ) {
|
|
|
|
|
$status->warning( 'lockmanager-notlocked', $path );
|
2011-12-20 03:52:06 +00:00
|
|
|
} else {
|
2011-12-24 00:16:06 +00:00
|
|
|
--$this->locksHeld[$path][$type];
|
|
|
|
|
if ( $this->locksHeld[$path][$type] <= 0 ) {
|
|
|
|
|
unset( $this->locksHeld[$path][$type] );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
2011-12-24 00:16:06 +00:00
|
|
|
if ( !count( $this->locksHeld[$path] ) ) {
|
|
|
|
|
unset( $this->locksHeld[$path] ); // no SH or EX locks left for key
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reference count the locks held and COMMIT when zero
|
|
|
|
|
if ( !count( $this->locksHeld ) ) {
|
|
|
|
|
$status->merge( $this->finishLockTransactions() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-12-24 00:16:06 +00:00
|
|
|
* Get a connection to a lock DB and acquire locks on $paths.
|
2011-12-20 03:52:06 +00:00
|
|
|
* This does not use GET_LOCK() per http://bugs.mysql.com/bug.php?id=1118.
|
|
|
|
|
*
|
|
|
|
|
* @param $lockDb string
|
2011-12-24 00:16:06 +00:00
|
|
|
* @param $paths Array
|
2011-12-20 03:52:06 +00:00
|
|
|
* @param $type integer LockManager::LOCK_EX or LockManager::LOCK_SH
|
|
|
|
|
* @return bool Resources able to be locked
|
|
|
|
|
* @throws DBError
|
|
|
|
|
*/
|
2011-12-24 00:16:06 +00:00
|
|
|
protected function doLockingQuery( $lockDb, array $paths, $type ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
if ( $type == self::LOCK_EX ) { // writer locks
|
|
|
|
|
$db = $this->getConnection( $lockDb );
|
|
|
|
|
if ( !$db ) {
|
|
|
|
|
return false; // bad config
|
|
|
|
|
}
|
2011-12-24 00:16:06 +00:00
|
|
|
$keys = array_unique( array_map( 'LockManager::sha1Base36', $paths ) );
|
|
|
|
|
# Build up values for INSERT clause
|
2011-12-20 03:52:06 +00:00
|
|
|
$data = array();
|
|
|
|
|
foreach ( $keys as $key ) {
|
|
|
|
|
$data[] = array( 'fle_key' => $key );
|
|
|
|
|
}
|
|
|
|
|
# Wait on any existing writers and block new ones if we get in
|
|
|
|
|
$db->insert( 'filelocks_exclusive', $data, __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Attempt to acquire locks with the peers for a bucket.
|
|
|
|
|
* This should avoid throwing any exceptions.
|
|
|
|
|
*
|
|
|
|
|
* @param $bucket integer
|
2011-12-24 00:16:06 +00:00
|
|
|
* @param $paths Array List of resource keys to lock
|
2011-12-20 03:52:06 +00:00
|
|
|
* @param $type integer LockManager::LOCK_EX or LockManager::LOCK_SH
|
|
|
|
|
* @return bool|string One of (true, 'cantacquire', 'dberrors')
|
|
|
|
|
*/
|
2011-12-24 00:16:06 +00:00
|
|
|
protected function doLockingQueryAll( $bucket, array $paths, $type ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
$yesVotes = 0; // locks made on trustable DBs
|
|
|
|
|
$votesLeft = count( $this->dbsByBucket[$bucket] ); // remaining DBs
|
|
|
|
|
$quorum = floor( $votesLeft/2 + 1 ); // simple majority
|
|
|
|
|
// Get votes for each DB, in order, until we have enough...
|
2011-12-31 00:21:27 +00:00
|
|
|
foreach ( $this->dbsByBucket[$bucket] as $lockDb ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
// Check that DB is not *known* to be down
|
|
|
|
|
if ( $this->cacheCheckFailures( $lockDb ) ) {
|
|
|
|
|
try {
|
|
|
|
|
// Attempt to acquire the lock on this DB
|
2011-12-24 00:16:06 +00:00
|
|
|
if ( !$this->doLockingQuery( $lockDb, $paths, $type ) ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
return 'cantacquire'; // vetoed; resource locked
|
|
|
|
|
}
|
|
|
|
|
++$yesVotes; // success for this peer
|
|
|
|
|
if ( $yesVotes >= $quorum ) {
|
|
|
|
|
return true; // lock obtained
|
|
|
|
|
}
|
|
|
|
|
} catch ( DBConnectionError $e ) {
|
|
|
|
|
$this->cacheRecordFailure( $lockDb );
|
|
|
|
|
} catch ( DBError $e ) {
|
|
|
|
|
if ( $this->lastErrorIndicatesLocked( $lockDb ) ) {
|
|
|
|
|
return 'cantacquire'; // vetoed; resource locked
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-12-24 00:16:06 +00:00
|
|
|
--$votesLeft;
|
2011-12-20 03:52:06 +00:00
|
|
|
$votesNeeded = $quorum - $yesVotes;
|
|
|
|
|
if ( $votesNeeded > $votesLeft ) {
|
|
|
|
|
// In "trust cache" mode we don't have to meet the quorum
|
|
|
|
|
break; // short-circuit
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// At this point, we must not have meet the quorum
|
|
|
|
|
return 'dberrors'; // not enough votes to ensure correctness
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get (or reuse) a connection to a lock DB
|
|
|
|
|
*
|
|
|
|
|
* @param $lockDb string
|
2012-02-09 18:01:54 +00:00
|
|
|
* @return DatabaseBase
|
2011-12-20 03:52:06 +00:00
|
|
|
* @throws DBError
|
|
|
|
|
*/
|
|
|
|
|
protected function getConnection( $lockDb ) {
|
|
|
|
|
if ( !isset( $this->conns[$lockDb] ) ) {
|
|
|
|
|
$db = null;
|
|
|
|
|
if ( $lockDb === 'localDBMaster' ) {
|
|
|
|
|
$lb = wfGetLBFactory()->newMainLB();
|
|
|
|
|
$db = $lb->getConnection( DB_MASTER );
|
|
|
|
|
} elseif ( isset( $this->dbServers[$lockDb] ) ) {
|
|
|
|
|
$config = $this->dbServers[$lockDb];
|
|
|
|
|
$db = DatabaseBase::factory( $config['type'], $config );
|
|
|
|
|
}
|
|
|
|
|
if ( !$db ) {
|
|
|
|
|
return null; // config error?
|
|
|
|
|
}
|
|
|
|
|
$this->conns[$lockDb] = $db;
|
|
|
|
|
$this->conns[$lockDb]->clearFlag( DBO_TRX );
|
|
|
|
|
# If the connection drops, try to avoid letting the DB rollback
|
|
|
|
|
# and release the locks before the file operations are finished.
|
|
|
|
|
# This won't handle the case of DB server restarts however.
|
|
|
|
|
$options = array();
|
|
|
|
|
if ( $this->lockExpiry > 0 ) {
|
|
|
|
|
$options['connTimeout'] = $this->lockExpiry;
|
|
|
|
|
}
|
|
|
|
|
$this->conns[$lockDb]->setSessionOptions( $options );
|
|
|
|
|
$this->initConnection( $lockDb, $this->conns[$lockDb] );
|
|
|
|
|
}
|
|
|
|
|
if ( !$this->conns[$lockDb]->trxLevel() ) {
|
2012-02-26 17:19:11 +00:00
|
|
|
$this->conns[$lockDb]->begin( __METHOD__ ); // start transaction
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
return $this->conns[$lockDb];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Do additional initialization for new lock DB connection
|
|
|
|
|
*
|
|
|
|
|
* @param $lockDb string
|
|
|
|
|
* @param $db DatabaseBase
|
2012-01-12 19:41:18 +00:00
|
|
|
* @return void
|
2011-12-20 03:52:06 +00:00
|
|
|
* @throws DBError
|
|
|
|
|
*/
|
|
|
|
|
protected function initConnection( $lockDb, DatabaseBase $db ) {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Commit all changes to lock-active databases.
|
|
|
|
|
* This should avoid throwing any exceptions.
|
|
|
|
|
*
|
|
|
|
|
* @return Status
|
|
|
|
|
*/
|
|
|
|
|
protected function finishLockTransactions() {
|
|
|
|
|
$status = Status::newGood();
|
|
|
|
|
foreach ( $this->conns as $lockDb => $db ) {
|
|
|
|
|
if ( $db->trxLevel() ) { // in transaction
|
|
|
|
|
try {
|
2012-02-26 17:19:11 +00:00
|
|
|
$db->rollback( __METHOD__ ); // finish transaction and kill any rows
|
2011-12-20 03:52:06 +00:00
|
|
|
} catch ( DBError $e ) {
|
|
|
|
|
$status->fatal( 'lockmanager-fail-db-release', $lockDb );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the last DB error for $lockDb indicates
|
|
|
|
|
* that a requested resource was locked by another process.
|
|
|
|
|
* This should avoid throwing any exceptions.
|
|
|
|
|
*
|
|
|
|
|
* @param $lockDb string
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function lastErrorIndicatesLocked( $lockDb ) {
|
|
|
|
|
if ( isset( $this->conns[$lockDb] ) ) { // sanity
|
|
|
|
|
$db = $this->conns[$lockDb];
|
|
|
|
|
return ( $db->wasDeadlock() || $db->wasLockTimeout() );
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if the DB has not recently had connection/query errors.
|
|
|
|
|
* This just avoids wasting time on doomed connection attempts.
|
|
|
|
|
*
|
|
|
|
|
* @param $lockDb string
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function cacheCheckFailures( $lockDb ) {
|
|
|
|
|
if ( $this->statusCache && $this->safeDelay > 0 ) {
|
2011-12-24 00:16:06 +00:00
|
|
|
$path = $this->getMissKey( $lockDb );
|
|
|
|
|
$misses = $this->statusCache->get( $path );
|
2011-12-20 03:52:06 +00:00
|
|
|
return !$misses;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Log a lock request failure to the cache
|
|
|
|
|
*
|
|
|
|
|
* @param $lockDb string
|
|
|
|
|
* @return bool Success
|
|
|
|
|
*/
|
|
|
|
|
protected function cacheRecordFailure( $lockDb ) {
|
|
|
|
|
if ( $this->statusCache && $this->safeDelay > 0 ) {
|
2011-12-24 00:16:06 +00:00
|
|
|
$path = $this->getMissKey( $lockDb );
|
|
|
|
|
$misses = $this->statusCache->get( $path );
|
2011-12-20 03:52:06 +00:00
|
|
|
if ( $misses ) {
|
2011-12-24 00:16:06 +00:00
|
|
|
return $this->statusCache->incr( $path );
|
2011-12-20 03:52:06 +00:00
|
|
|
} else {
|
2011-12-24 00:16:06 +00:00
|
|
|
return $this->statusCache->add( $path, 1, $this->safeDelay );
|
2011-12-20 03:52:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a cache key for recent query misses for a DB
|
|
|
|
|
*
|
|
|
|
|
* @param $lockDb string
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function getMissKey( $lockDb ) {
|
|
|
|
|
return 'lockmanager:querymisses:' . str_replace( ' ', '_', $lockDb );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-12-24 00:16:06 +00:00
|
|
|
* Get the bucket for resource path.
|
2011-12-20 03:52:06 +00:00
|
|
|
* This should avoid throwing any exceptions.
|
|
|
|
|
*
|
2011-12-24 00:16:06 +00:00
|
|
|
* @param $path string
|
2011-12-20 03:52:06 +00:00
|
|
|
* @return integer
|
|
|
|
|
*/
|
2011-12-24 00:16:06 +00:00
|
|
|
protected function getBucketFromKey( $path ) {
|
|
|
|
|
$prefix = substr( sha1( $path ), 0, 2 ); // first 2 hex chars (8 bits)
|
2011-12-20 03:52:06 +00:00
|
|
|
return intval( base_convert( $prefix, 16, 10 ) ) % count( $this->dbsByBucket );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Make sure remaining locks get cleared for sanity
|
|
|
|
|
*/
|
|
|
|
|
function __destruct() {
|
|
|
|
|
foreach ( $this->conns as $lockDb => $db ) {
|
|
|
|
|
if ( $db->trxLevel() ) { // in transaction
|
|
|
|
|
try {
|
2012-02-26 17:19:11 +00:00
|
|
|
$db->rollback( __METHOD__ ); // finish transaction and kill any rows
|
2011-12-20 03:52:06 +00:00
|
|
|
} catch ( DBError $e ) {
|
|
|
|
|
// oh well
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$db->close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* MySQL version of DBLockManager that supports shared locks.
|
|
|
|
|
* All locks are non-blocking, which avoids deadlocks.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup LockManager
|
|
|
|
|
*/
|
|
|
|
|
class MySqlLockManager extends DBLockManager {
|
|
|
|
|
/** @var Array Mapping of lock types to the type actually used */
|
|
|
|
|
protected $lockTypeMap = array(
|
|
|
|
|
self::LOCK_SH => self::LOCK_SH,
|
|
|
|
|
self::LOCK_UW => self::LOCK_SH,
|
|
|
|
|
self::LOCK_EX => self::LOCK_EX
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
protected function initConnection( $lockDb, DatabaseBase $db ) {
|
|
|
|
|
# Let this transaction see lock rows from other transactions
|
|
|
|
|
$db->query( "SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;" );
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-24 00:16:06 +00:00
|
|
|
protected function doLockingQuery( $lockDb, array $paths, $type ) {
|
2011-12-20 03:52:06 +00:00
|
|
|
$db = $this->getConnection( $lockDb );
|
|
|
|
|
if ( !$db ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-12-24 00:16:06 +00:00
|
|
|
$keys = array_unique( array_map( 'LockManager::sha1Base36', $paths ) );
|
|
|
|
|
# Build up values for INSERT clause
|
2011-12-20 03:52:06 +00:00
|
|
|
$data = array();
|
|
|
|
|
foreach ( $keys as $key ) {
|
|
|
|
|
$data[] = array( 'fls_key' => $key, 'fls_session' => $this->session );
|
|
|
|
|
}
|
|
|
|
|
# Block new writers...
|
|
|
|
|
$db->insert( 'filelocks_shared', $data, __METHOD__, array( 'IGNORE' ) );
|
|
|
|
|
# Actually do the locking queries...
|
|
|
|
|
if ( $type == self::LOCK_SH ) { // reader locks
|
|
|
|
|
# Bail if there are any existing writers...
|
|
|
|
|
$blocked = $db->selectField( 'filelocks_exclusive', '1',
|
|
|
|
|
array( 'fle_key' => $keys ),
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
# Prospective writers that haven't yet updated filelocks_exclusive
|
|
|
|
|
# will recheck filelocks_shared after doing so and bail due to our entry.
|
|
|
|
|
} else { // writer locks
|
|
|
|
|
$encSession = $db->addQuotes( $this->session );
|
|
|
|
|
# Bail if there are any existing writers...
|
|
|
|
|
# The may detect readers, but the safe check for them is below.
|
|
|
|
|
# Note: if two writers come at the same time, both bail :)
|
|
|
|
|
$blocked = $db->selectField( 'filelocks_shared', '1',
|
|
|
|
|
array( 'fls_key' => $keys, "fls_session != $encSession" ),
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
if ( !$blocked ) {
|
2011-12-24 00:16:06 +00:00
|
|
|
# Build up values for INSERT clause
|
2011-12-20 03:52:06 +00:00
|
|
|
$data = array();
|
|
|
|
|
foreach ( $keys as $key ) {
|
|
|
|
|
$data[] = array( 'fle_key' => $key );
|
|
|
|
|
}
|
|
|
|
|
# Block new readers/writers...
|
|
|
|
|
$db->insert( 'filelocks_exclusive', $data, __METHOD__ );
|
|
|
|
|
# Bail if there are any existing readers...
|
|
|
|
|
$blocked = $db->selectField( 'filelocks_shared', '1',
|
|
|
|
|
array( 'fls_key' => $keys, "fls_session != $encSession" ),
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return !$blocked;
|
|
|
|
|
}
|
|
|
|
|
}
|