r105491 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r105490‎ | r105491 | r105492 >
Date:23:48, 7 December 2011
Author:aaron
Status:deferred
Tags:
Comment:
* Renamed getScopedLock() => getScopedFileLocks()
* Made unlock() functions require a $type param and avoided bug with lock nesting (outer function locks getting released by innter function if one did an SH lock on a key and another an EX lock)
* More documentation fixes
Modified paths:
  • /branches/FileBackend/phase3/includes/filerepo/backend/FileBackend.php (modified) (history)
  • /branches/FileBackend/phase3/includes/filerepo/backend/FileBackendMultiWrite.php (modified) (history)
  • /branches/FileBackend/phase3/includes/filerepo/backend/LockManager.php (modified) (history)

Diff [purge]

Index: branches/FileBackend/phase3/includes/filerepo/backend/FileBackendMultiWrite.php
@@ -84,7 +84,7 @@
8585 }
8686
8787 // Try to lock those files for the scope of this function...
88 - $scopedLock = $this->getScopedLock( $filesToLock, $status );
 88+ $scopedLock = $this->getScopedFileLocks( $filesToLock, $status );
8989 if ( !$status->isOK() ) {
9090 return $status; // abort
9191 }
Index: branches/FileBackend/phase3/includes/filerepo/backend/LockManager.php
@@ -38,15 +38,16 @@
3939 * Unlock the resources at the given abstract paths
4040 *
4141 * @param $paths Array List of storage paths
 42+ * @param $type integer LockManager::LOCK_EX, LockManager::LOCK_SH
4243 * @return Status
4344 */
44 - final public function unlock( array $paths ) {
 45+ final public function unlock( array $paths, $type = self::LOCK_EX ) {
4546 $keys = array_unique( array_map( 'sha1', $paths ) );
46 - return $this->doUnlock( $keys, 0 );
 47+ return $this->doUnlock( $keys, $type );
4748 }
4849
4950 /**
50 - * Lock a resource with the given key
 51+ * Lock resources with the given keys and lock type
5152 *
5253 * @param $key Array List of keys to lock (40 char hex hashes)
5354 * @param $type integer LockManager::LOCK_EX, LockManager::LOCK_SH
@@ -55,11 +56,10 @@
5657 abstract protected function doLock( array $keys, $type );
5758
5859 /**
59 - * Unlock a resource with the given key.
60 - * If $type is given, then only locks of that type should be cleared.
 60+ * Unlock resources with the given keys and lock type
6161 *
6262 * @param $key Array List of keys to unlock (40 char hex hashes)
63 - * @param $type integer LockManager::LOCK_EX, LockManager::LOCK_SH, or 0
 63+ * @param $type integer LockManager::LOCK_EX, LockManager::LOCK_SH
6464 * @return string
6565 */
6666 abstract protected function doUnlock( array $keys, $type );
@@ -173,25 +173,20 @@
174174
175175 if ( !isset( $this->locksHeld[$key] ) ) {
176176 $status->warning( 'lockmanager-notlocked', $key );
177 - } elseif ( $type && !isset( $this->locksHeld[$key][$type] ) ) {
 177+ } elseif ( !isset( $this->locksHeld[$key][$type] ) ) {
178178 $status->warning( 'lockmanager-notlocked', $key );
179179 } else {
180180 $handlesToClose = array();
181 - foreach ( $this->locksHeld[$key] as $lockType => $count ) {
182 - if ( $type && $lockType != $type ) {
183 - continue; // only unlock locks of type $type
 181+ --$this->locksHeld[$key][$type];
 182+ if ( $this->locksHeld[$key][$type] <= 0 ) {
 183+ unset( $this->locksHeld[$key][$type] );
 184+ // If a LOCK_SH comes in while we have a LOCK_EX, we don't
 185+ // actually add a handler, so check for handler existence.
 186+ if ( isset( $this->handles[$key][$type] ) ) {
 187+ // Mark this handle to be unlocked and closed
 188+ $handlesToClose[] = $this->handles[$key][$type];
 189+ unset( $this->handles[$key][$type] );
184190 }
185 - --$this->locksHeld[$key][$lockType];
186 - if ( $this->locksHeld[$key][$lockType] <= 0 ) {
187 - unset( $this->locksHeld[$key][$lockType] );
188 - // If a LOCK_SH comes in while we have a LOCK_EX, we don't
189 - // actually add a handler, so check for handler existence.
190 - if ( isset( $this->handles[$key][$lockType] ) ) {
191 - // Mark this handle to be unlocked and closed
192 - $handlesToClose[] = $this->handles[$key][$lockType];
193 - unset( $this->handles[$key][$lockType] );
194 - }
195 - }
196191 }
197192 // Unlock handles to release locks and delete
198193 // any lock files that end up with no locks on them...
@@ -391,17 +386,12 @@
392387 foreach ( $keys as $key ) {
393388 if ( !isset( $this->locksHeld[$key] ) ) {
394389 $status->warning( 'lockmanager-notlocked', $key );
395 - } elseif ( $type && !isset( $this->locksHeld[$key][$type] ) ) {
 390+ } elseif ( !isset( $this->locksHeld[$key][$type] ) ) {
396391 $status->warning( 'lockmanager-notlocked', $key );
397392 } else {
398 - foreach ( $this->locksHeld[$key] as $lockType => $count ) {
399 - if ( $type && $lockType != $type ) {
400 - continue; // only unlock locks of type $type
401 - }
402 - --$this->locksHeld[$key][$lockType];
403 - if ( $this->locksHeld[$key][$lockType] <= 0 ) {
404 - unset( $this->locksHeld[$key][$lockType] );
405 - }
 393+ --$this->locksHeld[$key][$type];
 394+ if ( $this->locksHeld[$key][$type] <= 0 ) {
 395+ unset( $this->locksHeld[$key][$type] );
406396 }
407397 if ( !count( $this->locksHeld[$key] ) ) {
408398 unset( $this->locksHeld[$key] ); // no SH or EX locks left for key
Index: branches/FileBackend/phase3/includes/filerepo/backend/FileBackend.php
@@ -253,11 +253,11 @@
254254 *
255255 * Avoid using this function outside of FileBackendScopedLock.
256256 *
257 - * @param $sources Array Source storage paths
 257+ * @param $paths Array Storage paths
258258 * @return Status
259259 */
260260 final public function lockFiles( array $paths ) {
261 - return $this->lockManager->lock( $paths );
 261+ return $this->lockManager->lock( $paths, LockManager::LOCK_EX );
262262 }
263263
264264 /**
@@ -265,11 +265,11 @@
266266 *
267267 * Avoid using this function outside of FileBackendScopedLock.
268268 *
269 - * @param $sources Array Source storage paths
 269+ * @param $paths Array Storage paths
270270 * @return Status
271271 */
272272 final public function unlockFiles( array $paths ) {
273 - return $this->lockManager->unlock( $paths );
 273+ return $this->lockManager->unlock( $paths, LockManager::LOCK_EX );
274274 }
275275
276276 /**
@@ -280,11 +280,11 @@
281281 * Once the return value goes out scope, the locks will be released and
282282 * the status updated. Unlock fatals will not change the status "OK" value.
283283 *
284 - * @param $sources Array Source storage paths
 284+ * @param $paths Array Storage paths
285285 * @param $status Status Status to update on lock/unlock
286286 * @return FileBackendScopedLock|null Returns null on failure
287287 */
288 - final public function getScopedLock( array $paths, Status $status ) {
 288+ final public function getScopedFileLocks( array $paths, Status $status ) {
289289 return FileBackendScopedLock::factory( $this, $paths, $status );
290290 }
291291 }
@@ -499,7 +499,7 @@
500500 }
501501
502502 // Try to lock those files for the scope of this function...
503 - $scopedLock = $this->getScopedLock( $filesToLock, $status );
 503+ $scopedLock = $this->getScopedFileLocks( $filesToLock, $status );
504504 if ( !$status->isOK() ) {
505505 return $status; // abort
506506 }

Status & tagging log