441ffea2d598c579d56da9d0b41832012a830aec
[lhc/web/wiklou.git] / includes / filebackend / lockmanager / MySqlLockManager.php
1 <?php
2 /**
3 * MySQL version of DBLockManager that supports shared locks.
4 *
5 * All lock servers must have the innodb table defined in locking/filelocks.sql.
6 * All locks are non-blocking, which avoids deadlocks.
7 *
8 * @ingroup LockManager
9 */
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
16 ];
17
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__ );
23 }
24
25 /**
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.
28 *
29 * @see DBLockManager::getLocksOnServer()
30 * @param string $lockSrv
31 * @param array $paths
32 * @param string $type
33 * @return StatusValue
34 */
35 protected function doGetLocksOnServer( $lockSrv, array $paths, $type ) {
36 $status = StatusValue::newGood();
37
38 $db = $this->getConnection( $lockSrv ); // checked in isServerUp()
39
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 );
46 $keys[] = $key;
47 $data[] = [ 'fls_key' => $key, 'fls_session' => $this->session ];
48 if ( !isset( $this->locksHeld[$path][self::LOCK_EX] ) ) {
49 $checkEXKeys[] = $key;
50 }
51 }
52
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
57 $blocked = false;
58 # Bail if there are any existing writers...
59 if ( count( $checkEXKeys ) ) {
60 $blocked = $db->selectField( 'filelocks_exclusive', '1',
61 [ 'fle_key' => $checkEXKeys ],
62 __METHOD__
63 );
64 }
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" ],
74 __METHOD__
75 );
76 if ( !$blocked ) {
77 # Build up values for INSERT clause
78 $data = [];
79 foreach ( $keys as $key ) {
80 $data[] = [ 'fle_key' => $key ];
81 }
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" ],
87 __METHOD__
88 );
89 }
90 }
91
92 if ( $blocked ) {
93 foreach ( $paths as $path ) {
94 $status->fatal( 'lockmanager-fail-acquirelock', $path );
95 }
96 }
97
98 return $status;
99 }
100
101 /**
102 * @see QuorumLockManager::releaseAllLocks()
103 * @return StatusValue
104 */
105 protected function releaseAllLocks() {
106 $status = StatusValue::newGood();
107
108 foreach ( $this->conns as $lockDb => $db ) {
109 if ( $db->trxLevel() ) { // in transaction
110 try {
111 $db->rollback( __METHOD__ ); // finish transaction and kill any rows
112 } catch ( DBError $e ) {
113 $status->fatal( 'lockmanager-fail-db-release', $lockDb );
114 }
115 }
116 }
117
118 return $status;
119 }
120 }