3 * Simple version of LockManager based on using FS lock files.
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.
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.
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
21 * @ingroup LockManager
25 * Simple version of LockManager based on using FS lock files.
26 * All locks are non-blocking, which avoids deadlocks.
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.
33 * @ingroup LockManager
36 class FSLockManager
extends LockManager
{
37 /** @var array Mapping of lock types to the type actually used */
38 protected $lockTypeMap = [
39 self
::LOCK_SH
=> self
::LOCK_SH
,
40 self
::LOCK_UW
=> self
::LOCK_SH
,
41 self
::LOCK_EX
=> self
::LOCK_EX
44 protected $lockDir; // global dir for all servers
46 /** @var array Map of (locked key => lock file handle) */
47 protected $handles = [];
50 * Construct a new instance from configuration.
52 * @param array $config Includes:
53 * - lockDirectory : Directory containing the lock files
55 function __construct( array $config ) {
56 parent
::__construct( $config );
58 $this->lockDir
= $config['lockDirectory'];
62 * @see LockManager::doLock()
67 protected function doLock( array $paths, $type ) {
68 $status = Status
::newGood();
70 $lockedPaths = []; // files locked in this attempt
71 foreach ( $paths as $path ) {
72 $status->merge( $this->doSingleLock( $path, $type ) );
73 if ( $status->isOK() ) {
74 $lockedPaths[] = $path;
76 // Abort and unlock everything
77 $status->merge( $this->doUnlock( $lockedPaths, $type ) );
87 * @see LockManager::doUnlock()
92 protected function doUnlock( array $paths, $type ) {
93 $status = Status
::newGood();
95 foreach ( $paths as $path ) {
96 $status->merge( $this->doSingleUnlock( $path, $type ) );
103 * Lock a single resource key
105 * @param string $path
109 protected function doSingleLock( $path, $type ) {
110 $status = Status
::newGood();
112 if ( isset( $this->locksHeld
[$path][$type] ) ) {
113 ++
$this->locksHeld
[$path][$type];
114 } elseif ( isset( $this->locksHeld
[$path][self
::LOCK_EX
] ) ) {
115 $this->locksHeld
[$path][$type] = 1;
117 if ( isset( $this->handles
[$path] ) ) {
118 $handle = $this->handles
[$path];
120 MediaWiki\
suppressWarnings();
121 $handle = fopen( $this->getLockPath( $path ), 'a+' );
122 MediaWiki\restoreWarnings
();
123 if ( !$handle ) { // lock dir missing?
124 wfMkdirParents( $this->lockDir
);
125 $handle = fopen( $this->getLockPath( $path ), 'a+' ); // try again
129 // Either a shared or exclusive lock
130 $lock = ( $type == self
::LOCK_SH
) ? LOCK_SH
: LOCK_EX
;
131 if ( flock( $handle, $lock | LOCK_NB
) ) {
132 // Record this lock as active
133 $this->locksHeld
[$path][$type] = 1;
134 $this->handles
[$path] = $handle;
137 $status->fatal( 'lockmanager-fail-acquirelock', $path );
140 $status->fatal( 'lockmanager-fail-openlock', $path );
148 * Unlock a single resource key
150 * @param string $path
154 protected function doSingleUnlock( $path, $type ) {
155 $status = Status
::newGood();
157 if ( !isset( $this->locksHeld
[$path] ) ) {
158 $status->warning( 'lockmanager-notlocked', $path );
159 } elseif ( !isset( $this->locksHeld
[$path][$type] ) ) {
160 $status->warning( 'lockmanager-notlocked', $path );
162 $handlesToClose = [];
163 --$this->locksHeld
[$path][$type];
164 if ( $this->locksHeld
[$path][$type] <= 0 ) {
165 unset( $this->locksHeld
[$path][$type] );
167 if ( !count( $this->locksHeld
[$path] ) ) {
168 unset( $this->locksHeld
[$path] ); // no locks on this path
169 if ( isset( $this->handles
[$path] ) ) {
170 $handlesToClose[] = $this->handles
[$path];
171 unset( $this->handles
[$path] );
174 // Unlock handles to release locks and delete
175 // any lock files that end up with no locks on them...
176 if ( wfIsWindows() ) {
177 // Windows: for any process, including this one,
178 // calling unlink() on a locked file will fail
179 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
180 $status->merge( $this->pruneKeyLockFiles( $path ) );
182 // Unix: unlink() can be used on files currently open by this
183 // process and we must do so in order to avoid race conditions
184 $status->merge( $this->pruneKeyLockFiles( $path ) );
185 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
193 * @param string $path
194 * @param array $handlesToClose
197 private function closeLockHandles( $path, array $handlesToClose ) {
198 $status = Status
::newGood();
199 foreach ( $handlesToClose as $handle ) {
200 if ( !flock( $handle, LOCK_UN
) ) {
201 $status->fatal( 'lockmanager-fail-releaselock', $path );
203 if ( !fclose( $handle ) ) {
204 $status->warning( 'lockmanager-fail-closelock', $path );
212 * @param string $path
215 private function pruneKeyLockFiles( $path ) {
216 $status = Status
::newGood();
217 if ( !isset( $this->locksHeld
[$path] ) ) {
218 # No locks are held for the lock file anymore
219 if ( !unlink( $this->getLockPath( $path ) ) ) {
220 $status->warning( 'lockmanager-fail-deletelock', $path );
222 unset( $this->handles
[$path] );
229 * Get the path to the lock file for a key
230 * @param string $path
233 protected function getLockPath( $path ) {
234 return "{$this->lockDir}/{$this->sha1Base36Absolute( $path )}.lock";
238 * Make sure remaining locks get cleared for sanity
240 function __destruct() {
241 while ( count( $this->locksHeld
) ) {
242 foreach ( $this->locksHeld
as $path => $locks ) {
243 $this->doSingleUnlock( $path, self
::LOCK_EX
);
244 $this->doSingleUnlock( $path, self
::LOCK_SH
);