4 * Simple version of LockManager based on using FS lock files.
5 * All locks are non-blocking, which avoids deadlocks.
7 * This should work fine for small sites running off one server.
8 * Do not use this with 'lockDirectory' set to an NFS mount unless the
9 * NFS client is at least version 2.6.12. Otherwise, the BSD flock()
10 * locks will be ignored; see http://nfs.sourceforge.net/#section_d.
12 * @ingroup LockManager
15 class FSLockManager
extends LockManager
{
16 /** @var Array Mapping of lock types to the type actually used */
17 protected $lockTypeMap = array(
18 self
::LOCK_SH
=> self
::LOCK_SH
,
19 self
::LOCK_UW
=> self
::LOCK_SH
,
20 self
::LOCK_EX
=> self
::LOCK_EX
23 protected $lockDir; // global dir for all servers
25 /** @var Array Map of (locked key => lock type => lock file handle) */
26 protected $handles = array();
29 * Construct a new instance from configuration.
32 * 'lockDirectory' : Directory containing the lock files
34 * @param array $config
36 function __construct( array $config ) {
37 parent
::__construct( $config );
38 $this->lockDir
= $config['lockDirectory'];
41 protected function doLock( array $paths, $type ) {
42 $status = Status
::newGood();
44 $lockedPaths = array(); // files locked in this attempt
45 foreach ( $paths as $path ) {
46 $subStatus = $this->doSingleLock( $path, $type );
47 $status->merge( $subStatus );
48 if ( $status->isOK() ) {
49 // Don't append to $lockedPaths if $path is already locked.
50 // We do NOT want to unlock the key if we have to rollback.
51 if ( $subStatus->isGood() ) { // no warnings/fatals?
52 $lockedPaths[] = $path;
55 // Abort and unlock everything
56 $status->merge( $this->doUnlock( $lockedPaths, $type ) );
64 protected function doUnlock( array $paths, $type ) {
65 $status = Status
::newGood();
67 foreach ( $paths as $path ) {
68 $status->merge( $this->doSingleUnlock( $path, $type ) );
75 * Lock a single resource key
78 * @param $type integer
81 protected function doSingleLock( $path, $type ) {
82 $status = Status
::newGood();
84 if ( isset( $this->locksHeld
[$path][$type] ) ) {
85 ++
$this->locksHeld
[$path][$type];
86 } elseif ( isset( $this->locksHeld
[$path][self
::LOCK_EX
] ) ) {
87 $this->locksHeld
[$path][$type] = 1;
90 $handle = fopen( $this->getLockPath( $path ), 'a+' );
92 if ( !$handle ) { // lock dir missing?
93 wfMkdirParents( $this->lockDir
);
94 $handle = fopen( $this->getLockPath( $path ), 'a+' ); // try again
97 // Either a shared or exclusive lock
98 $lock = ( $type == self
::LOCK_SH
) ? LOCK_SH
: LOCK_EX
;
99 if ( flock( $handle, $lock | LOCK_NB
) ) {
100 // Record this lock as active
101 $this->locksHeld
[$path][$type] = 1;
102 $this->handles
[$path][$type] = $handle;
105 $status->fatal( 'lockmanager-fail-acquirelock', $path );
108 $status->fatal( 'lockmanager-fail-openlock', $path );
116 * Unlock a single resource key
118 * @param $path string
119 * @param $type integer
122 protected function doSingleUnlock( $path, $type ) {
123 $status = Status
::newGood();
125 if ( !isset( $this->locksHeld
[$path] ) ) {
126 $status->warning( 'lockmanager-notlocked', $path );
127 } elseif ( !isset( $this->locksHeld
[$path][$type] ) ) {
128 $status->warning( 'lockmanager-notlocked', $path );
130 $handlesToClose = array();
131 --$this->locksHeld
[$path][$type];
132 if ( $this->locksHeld
[$path][$type] <= 0 ) {
133 unset( $this->locksHeld
[$path][$type] );
134 // If a LOCK_SH comes in while we have a LOCK_EX, we don't
135 // actually add a handler, so check for handler existence.
136 if ( isset( $this->handles
[$path][$type] ) ) {
137 // Mark this handle to be unlocked and closed
138 $handlesToClose[] = $this->handles
[$path][$type];
139 unset( $this->handles
[$path][$type] );
142 // Unlock handles to release locks and delete
143 // any lock files that end up with no locks on them...
144 if ( wfIsWindows() ) {
145 // Windows: for any process, including this one,
146 // calling unlink() on a locked file will fail
147 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
148 $status->merge( $this->pruneKeyLockFiles( $path ) );
150 // Unix: unlink() can be used on files currently open by this
151 // process and we must do so in order to avoid race conditions
152 $status->merge( $this->pruneKeyLockFiles( $path ) );
153 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
160 private function closeLockHandles( $path, array $handlesToClose ) {
161 $status = Status
::newGood();
162 foreach ( $handlesToClose as $handle ) {
163 wfSuppressWarnings();
164 if ( !flock( $handle, LOCK_UN
) ) {
165 $status->fatal( 'lockmanager-fail-releaselock', $path );
167 if ( !fclose( $handle ) ) {
168 $status->warning( 'lockmanager-fail-closelock', $path );
175 private function pruneKeyLockFiles( $path ) {
176 $status = Status
::newGood();
177 if ( !count( $this->locksHeld
[$path] ) ) {
178 wfSuppressWarnings();
179 # No locks are held for the lock file anymore
180 if ( !unlink( $this->getLockPath( $path ) ) ) {
181 $status->warning( 'lockmanager-fail-deletelock', $path );
184 unset( $this->locksHeld
[$path] );
185 unset( $this->handles
[$path] );
191 * Get the path to the lock file for a key
192 * @param $path string
195 protected function getLockPath( $path ) {
196 $hash = self
::sha1Base36( $path );
197 return "{$this->lockDir}/{$hash}.lock";
200 function __destruct() {
201 // Make sure remaining locks get cleared for sanity
202 foreach ( $this->locksHeld
as $path => $locks ) {
203 $this->doSingleUnlock( $path, self
::LOCK_EX
);
204 $this->doSingleUnlock( $path, self
::LOCK_SH
);