Merge "bug 44136: Don't rollback after successful commit"
[lhc/web/wiklou.git] / includes / filebackend / lockmanager / FSLockManager.php
1 <?php
2 /**
3 * Simple version of LockManager based on using FS lock files.
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 * Simple version of LockManager based on using FS lock files.
26 * All locks are non-blocking, which avoids deadlocks.
27 *
28 * This should work fine for small sites running off one server.
29 * Do not use this with 'lockDirectory' set to an NFS mount unless the
30 * NFS client is at least version 2.6.12. Otherwise, the BSD flock()
31 * locks will be ignored; see http://nfs.sourceforge.net/#section_d.
32 *
33 * @ingroup LockManager
34 * @since 1.19
35 */
36 class FSLockManager extends LockManager {
37 /** @var Array Mapping of lock types to the type actually used */
38 protected $lockTypeMap = array(
39 self::LOCK_SH => self::LOCK_SH,
40 self::LOCK_UW => self::LOCK_SH,
41 self::LOCK_EX => self::LOCK_EX
42 );
43
44 protected $lockDir; // global dir for all servers
45
46 /** @var Array Map of (locked key => lock type => lock file handle) */
47 protected $handles = array();
48
49 /**
50 * Construct a new instance from configuration.
51 *
52 * $config includes:
53 * - lockDirectory : Directory containing the lock files
54 *
55 * @param array $config
56 */
57 function __construct( array $config ) {
58 parent::__construct( $config );
59
60 $this->lockDir = $config['lockDirectory'];
61 }
62
63 /**
64 * @see LockManager::doLock()
65 * @param $paths array
66 * @param $type int
67 * @return Status
68 */
69 protected function doLock( array $paths, $type ) {
70 $status = Status::newGood();
71
72 $lockedPaths = array(); // files locked in this attempt
73 foreach ( $paths as $path ) {
74 $status->merge( $this->doSingleLock( $path, $type ) );
75 if ( $status->isOK() ) {
76 $lockedPaths[] = $path;
77 } else {
78 // Abort and unlock everything
79 $status->merge( $this->doUnlock( $lockedPaths, $type ) );
80 return $status;
81 }
82 }
83
84 return $status;
85 }
86
87 /**
88 * @see LockManager::doUnlock()
89 * @param $paths array
90 * @param $type int
91 * @return Status
92 */
93 protected function doUnlock( array $paths, $type ) {
94 $status = Status::newGood();
95
96 foreach ( $paths as $path ) {
97 $status->merge( $this->doSingleUnlock( $path, $type ) );
98 }
99
100 return $status;
101 }
102
103 /**
104 * Lock a single resource key
105 *
106 * @param $path string
107 * @param $type integer
108 * @return Status
109 */
110 protected function doSingleLock( $path, $type ) {
111 $status = Status::newGood();
112
113 if ( isset( $this->locksHeld[$path][$type] ) ) {
114 ++$this->locksHeld[$path][$type];
115 } elseif ( isset( $this->locksHeld[$path][self::LOCK_EX] ) ) {
116 $this->locksHeld[$path][$type] = 1;
117 } else {
118 wfSuppressWarnings();
119 $handle = fopen( $this->getLockPath( $path ), 'a+' );
120 wfRestoreWarnings();
121 if ( !$handle ) { // lock dir missing?
122 wfMkdirParents( $this->lockDir );
123 $handle = fopen( $this->getLockPath( $path ), 'a+' ); // try again
124 }
125 if ( $handle ) {
126 // Either a shared or exclusive lock
127 $lock = ( $type == self::LOCK_SH ) ? LOCK_SH : LOCK_EX;
128 if ( flock( $handle, $lock | LOCK_NB ) ) {
129 // Record this lock as active
130 $this->locksHeld[$path][$type] = 1;
131 $this->handles[$path][$type] = $handle;
132 } else {
133 fclose( $handle );
134 $status->fatal( 'lockmanager-fail-acquirelock', $path );
135 }
136 } else {
137 $status->fatal( 'lockmanager-fail-openlock', $path );
138 }
139 }
140
141 return $status;
142 }
143
144 /**
145 * Unlock a single resource key
146 *
147 * @param $path string
148 * @param $type integer
149 * @return Status
150 */
151 protected function doSingleUnlock( $path, $type ) {
152 $status = Status::newGood();
153
154 if ( !isset( $this->locksHeld[$path] ) ) {
155 $status->warning( 'lockmanager-notlocked', $path );
156 } elseif ( !isset( $this->locksHeld[$path][$type] ) ) {
157 $status->warning( 'lockmanager-notlocked', $path );
158 } else {
159 $handlesToClose = array();
160 --$this->locksHeld[$path][$type];
161 if ( $this->locksHeld[$path][$type] <= 0 ) {
162 unset( $this->locksHeld[$path][$type] );
163 // If a LOCK_SH comes in while we have a LOCK_EX, we don't
164 // actually add a handler, so check for handler existence.
165 if ( isset( $this->handles[$path][$type] ) ) {
166 if ( $type === self::LOCK_EX
167 && isset( $this->locksHeld[$path][self::LOCK_SH] )
168 && !isset( $this->handles[$path][self::LOCK_SH] ) )
169 {
170 // EX lock came first: move this handle to the SH one
171 $this->handles[$path][self::LOCK_SH] = $this->handles[$path][$type];
172 } else {
173 // Mark this handle to be unlocked and closed
174 $handlesToClose[] = $this->handles[$path][$type];
175 }
176 unset( $this->handles[$path][$type] );
177 }
178 }
179 if ( !count( $this->locksHeld[$path] ) ) {
180 unset( $this->locksHeld[$path] ); // no locks on this path
181 }
182 // Unlock handles to release locks and delete
183 // any lock files that end up with no locks on them...
184 if ( wfIsWindows() ) {
185 // Windows: for any process, including this one,
186 // calling unlink() on a locked file will fail
187 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
188 $status->merge( $this->pruneKeyLockFiles( $path ) );
189 } else {
190 // Unix: unlink() can be used on files currently open by this
191 // process and we must do so in order to avoid race conditions
192 $status->merge( $this->pruneKeyLockFiles( $path ) );
193 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
194 }
195 }
196
197 return $status;
198 }
199
200 /**
201 * @param $path string
202 * @param $handlesToClose array
203 * @return Status
204 */
205 private function closeLockHandles( $path, array $handlesToClose ) {
206 $status = Status::newGood();
207 foreach ( $handlesToClose as $handle ) {
208 if ( !flock( $handle, LOCK_UN ) ) {
209 $status->fatal( 'lockmanager-fail-releaselock', $path );
210 }
211 if ( !fclose( $handle ) ) {
212 $status->warning( 'lockmanager-fail-closelock', $path );
213 }
214 }
215 return $status;
216 }
217
218 /**
219 * @param $path string
220 * @return Status
221 */
222 private function pruneKeyLockFiles( $path ) {
223 $status = Status::newGood();
224 if ( !isset( $this->locksHeld[$path] ) ) {
225 # No locks are held for the lock file anymore
226 if ( !unlink( $this->getLockPath( $path ) ) ) {
227 $status->warning( 'lockmanager-fail-deletelock', $path );
228 }
229 unset( $this->handles[$path] );
230 }
231 return $status;
232 }
233
234 /**
235 * Get the path to the lock file for a key
236 * @param $path string
237 * @return string
238 */
239 protected function getLockPath( $path ) {
240 return "{$this->lockDir}/{$this->sha1Base36Absolute( $path )}.lock";
241 }
242
243 /**
244 * Make sure remaining locks get cleared for sanity
245 */
246 function __destruct() {
247 while ( count( $this->locksHeld ) ) {
248 foreach ( $this->locksHeld as $path => $locks ) {
249 $this->doSingleUnlock( $path, self::LOCK_EX );
250 $this->doSingleUnlock( $path, self::LOCK_SH );
251 }
252 }
253 }
254 }