Browse Source

Merge pull request #9388 from nextcloud/log-lock-state-on-conflict

Log lock state on conflict
pull/9412/head
Morris Jobke 8 years ago
committed by GitHub
parent
commit
7f5567e758
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      lib/private/Files/Storage/Common.php
  2. 19
      lib/private/Lock/MemcacheLockingProvider.php
  3. 10
      lib/public/Lock/LockedException.php

4
lib/private/Files/Storage/Common.php

@ -776,9 +776,7 @@ abstract class Common implements Storage, ILockingStorage {
try {
$provider->changeLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type);
} catch (LockedException $e) {
if ($logger) {
$logger->logException($e);
}
\OC::$server->getLogger()->logException($e);
throw $e;
}
}

19
lib/private/Lock/MemcacheLockingProvider.php

@ -72,12 +72,12 @@ class MemcacheLockingProvider extends AbstractLockingProvider {
public function acquireLock(string $path, int $type) {
if ($type === self::LOCK_SHARED) {
if (!$this->memcache->inc($path)) {
throw new LockedException($path);
throw new LockedException($path, null, $this->getExistingLockForException($path));
}
} else {
$this->memcache->add($path, 0);
if (!$this->memcache->cas($path, 0, 'exclusive')) {
throw new LockedException($path);
throw new LockedException($path, null, $this->getExistingLockForException($path));
}
}
$this->setTTL($path);
@ -115,15 +115,26 @@ class MemcacheLockingProvider extends AbstractLockingProvider {
public function changeLock(string $path, int $targetType) {
if ($targetType === self::LOCK_SHARED) {
if (!$this->memcache->cas($path, 'exclusive', 1)) {
throw new LockedException($path);
throw new LockedException($path, null, $this->getExistingLockForException($path));
}
} else if ($targetType === self::LOCK_EXCLUSIVE) {
// we can only change a shared lock to an exclusive if there's only a single owner of the shared lock
if (!$this->memcache->cas($path, 1, 'exclusive')) {
throw new LockedException($path);
throw new LockedException($path, null, $this->getExistingLockForException($path));
}
}
$this->setTTL($path);
$this->markChange($path, $targetType);
}
private function getExistingLockForException($path) {
$existing = $this->memcache->get($path);
if (!$existing) {
return 'none';
} else if ($existing === 'exclusive') {
return $existing;
} else {
return $existing . ' shared locks';
}
}
}

10
lib/public/Lock/LockedException.php

@ -46,11 +46,15 @@ class LockedException extends \Exception {
*
* @param string $path locked path
* @param \Exception|null $previous previous exception for cascading
*
* @param string $existingLock since 14.0.0
* @since 8.1.0
*/
public function __construct(string $path, \Exception $previous = null) {
parent::__construct('"' . $path . '" is locked', 0, $previous);
public function __construct(string $path, \Exception $previous = null, string $existingLock = null) {
$message = '"' . $path . '" is locked';
if ($existingLock) {
$message .= ', existing lock on file: ' . $existingLock;
}
parent::__construct($message, 0, $previous);
$this->path = $path;
}

Loading…
Cancel
Save