441ffea2d598c579d56da9d0b41832012a830aec
3 * MySQL version of DBLockManager that supports shared locks.
5 * All lock servers must have the innodb table defined in locking/filelocks.sql.
6 * All locks are non-blocking, which avoids deadlocks.
10 class MySqlLockManager
extends DBLockManager
{
11 /** @var array Mapping of lock types to the type actually used */
12 protected $lockTypeMap = [
13 self
::LOCK_SH
=> self
::LOCK_SH
,
14 self
::LOCK_UW
=> self
::LOCK_SH
,
15 self
::LOCK_EX
=> self
::LOCK_EX
18 protected function initConnection( $lockDb, IDatabase
$db ) {
19 # Let this transaction see lock rows from other transactions
20 $db->query( "SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;" );
21 # Do everything in a transaction as it all gets rolled back eventually
22 $db->startAtomic( __CLASS__
);
26 * Get a connection to a lock DB and acquire locks on $paths.
27 * This does not use GET_LOCK() per http://bugs.mysql.com/bug.php?id=1118.
29 * @see DBLockManager::getLocksOnServer()
30 * @param string $lockSrv
35 protected function doGetLocksOnServer( $lockSrv, array $paths, $type ) {
36 $status = StatusValue
::newGood();
38 $db = $this->getConnection( $lockSrv ); // checked in isServerUp()
40 $keys = []; // list of hash keys for the paths
41 $data = []; // list of rows to insert
42 $checkEXKeys = []; // list of hash keys that this has no EX lock on
43 # Build up values for INSERT clause
44 foreach ( $paths as $path ) {
45 $key = $this->sha1Base36Absolute( $path );
47 $data[] = [ 'fls_key' => $key, 'fls_session' => $this->session
];
48 if ( !isset( $this->locksHeld
[$path][self
::LOCK_EX
] ) ) {
49 $checkEXKeys[] = $key;
53 # Block new writers (both EX and SH locks leave entries here)...
54 $db->insert( 'filelocks_shared', $data, __METHOD__
, [ 'IGNORE' ] );
55 # Actually do the locking queries...
56 if ( $type == self
::LOCK_SH
) { // reader locks
58 # Bail if there are any existing writers...
59 if ( count( $checkEXKeys ) ) {
60 $blocked = $db->selectField( 'filelocks_exclusive', '1',
61 [ 'fle_key' => $checkEXKeys ],
65 # Other prospective writers that haven't yet updated filelocks_exclusive
66 # will recheck filelocks_shared after doing so and bail due to this entry.
67 } else { // writer locks
68 $encSession = $db->addQuotes( $this->session
);
69 # Bail if there are any existing writers...
70 # This may detect readers, but the safe check for them is below.
71 # Note: if two writers come at the same time, both bail :)
72 $blocked = $db->selectField( 'filelocks_shared', '1',
73 [ 'fls_key' => $keys, "fls_session != $encSession" ],
77 # Build up values for INSERT clause
79 foreach ( $keys as $key ) {
80 $data[] = [ 'fle_key' => $key ];
82 # Block new readers/writers...
83 $db->insert( 'filelocks_exclusive', $data, __METHOD__
);
84 # Bail if there are any existing readers...
85 $blocked = $db->selectField( 'filelocks_shared', '1',
86 [ 'fls_key' => $keys, "fls_session != $encSession" ],
93 foreach ( $paths as $path ) {
94 $status->fatal( 'lockmanager-fail-acquirelock', $path );
102 * @see QuorumLockManager::releaseAllLocks()
103 * @return StatusValue
105 protected function releaseAllLocks() {
106 $status = StatusValue
::newGood();
108 foreach ( $this->conns
as $lockDb => $db ) {
109 if ( $db->trxLevel() ) { // in transaction
111 $db->rollback( __METHOD__
); // finish transaction and kill any rows
112 } catch ( DBError
$e ) {
113 $status->fatal( 'lockmanager-fail-db-release', $lockDb );