summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2015-07-15 15:44:51 +0200
committerRobin Appelman <icewind@owncloud.com>2015-08-03 14:12:56 +0200
commita8552a1b24e7df8c4822b5b0dd7c690312ae810d (patch)
tree616ca1d6f7e3ac82f0613cccef03e3e3c7cba932
parentaf7bcb43b1b74a5f0ad94b645d400510a2ae6bc4 (diff)
downloadnextcloud-server-a8552a1b24e7df8c4822b5b0dd7c690312ae810d.tar.gz
nextcloud-server-a8552a1b24e7df8c4822b5b0dd7c690312ae810d.zip
split off keeping track of acquire locks
-rw-r--r--lib/private/lock/abstractlockingprovider.php96
-rw-r--r--lib/private/lock/memcachelockingprovider.php40
-rw-r--r--tests/lib/lock/lockingprovider.php30
3 files changed, 130 insertions, 36 deletions
diff --git a/lib/private/lock/abstractlockingprovider.php b/lib/private/lock/abstractlockingprovider.php
new file mode 100644
index 00000000000..290bb77e52f
--- /dev/null
+++ b/lib/private/lock/abstractlockingprovider.php
@@ -0,0 +1,96 @@
+<?php
+/**
+ * @author Morris Jobke <hey@morrisjobke.de>
+ * @author Robin Appelman <icewind@owncloud.com>
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OC\Lock;
+
+use OCP\Lock\ILockingProvider;
+
+abstract class AbstractLockingProvider implements ILockingProvider {
+ protected $acquiredLocks = [
+ 'shared' => [],
+ 'exclusive' => []
+ ];
+
+ /**
+ * @param string $path
+ * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
+ */
+ protected function markAcquire($path, $type) {
+ if ($type === self::LOCK_SHARED) {
+ if (!isset($this->acquiredLocks['shared'][$path])) {
+ $this->acquiredLocks['shared'][$path] = 0;
+ }
+ $this->acquiredLocks['shared'][$path]++;
+ } else {
+ $this->acquiredLocks['exclusive'][$path] = true;
+ }
+ }
+
+ /**
+ * @param string $path
+ * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
+ */
+ protected function markRelease($path, $type) {
+ if ($type === self::LOCK_SHARED) {
+ if (isset($this->acquiredLocks['shared'][$path]) and $this->acquiredLocks['shared'][$path] > 0) {
+ $this->acquiredLocks['shared'][$path]--;
+ }
+ } else if ($type === self::LOCK_EXCLUSIVE) {
+ unset($this->acquiredLocks['exclusive'][$path]);
+ }
+ }
+
+ /**
+ * Change the type of an existing lock
+ *
+ * @param string $path
+ * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE
+ */
+ protected function markChange($path, $targetType) {
+ if ($targetType === self::LOCK_SHARED) {
+ unset($this->acquiredLocks['exclusive'][$path]);
+ if (!isset($this->acquiredLocks['shared'][$path])) {
+ $this->acquiredLocks['shared'][$path] = 0;
+ }
+ $this->acquiredLocks['shared'][$path]++;
+ } else if ($targetType === self::LOCK_EXCLUSIVE) {
+ $this->acquiredLocks['exclusive'][$path] = true;
+ $this->acquiredLocks['shared'][$path]--;
+ }
+ }
+
+ /**
+ * release all lock acquired by this instance
+ */
+ public function releaseAll() {
+ foreach ($this->acquiredLocks['shared'] as $path => $count) {
+ for ($i = 0; $i < $count; $i++) {
+ $this->releaseLock($path, self::LOCK_SHARED);
+ }
+ }
+
+ foreach ($this->acquiredLocks['exclusive'] as $path => $hasLock) {
+ $this->releaseLock($path, self::LOCK_EXCLUSIVE);
+ }
+ }
+}
diff --git a/lib/private/lock/memcachelockingprovider.php b/lib/private/lock/memcachelockingprovider.php
index 5f2b5e5a4b8..871572f7e3e 100644
--- a/lib/private/lock/memcachelockingprovider.php
+++ b/lib/private/lock/memcachelockingprovider.php
@@ -23,21 +23,15 @@
namespace OC\Lock;
-use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use OCP\IMemcache;
-class MemcacheLockingProvider implements ILockingProvider {
+class MemcacheLockingProvider extends AbstractLockingProvider {
/**
* @var \OCP\IMemcache
*/
private $memcache;
- private $acquiredLocks = [
- 'shared' => [],
- 'exclusive' => []
- ];
-
/**
* @param \OCP\IMemcache $memcache
*/
@@ -71,17 +65,13 @@ class MemcacheLockingProvider implements ILockingProvider {
if (!$this->memcache->inc($path)) {
throw new LockedException($path);
}
- if (!isset($this->acquiredLocks['shared'][$path])) {
- $this->acquiredLocks['shared'][$path] = 0;
- }
- $this->acquiredLocks['shared'][$path]++;
} else {
$this->memcache->add($path, 0);
if (!$this->memcache->cas($path, 0, 'exclusive')) {
throw new LockedException($path);
}
- $this->acquiredLocks['exclusive'][$path] = true;
}
+ $this->markAcquire($path, $type);
}
/**
@@ -92,13 +82,12 @@ class MemcacheLockingProvider implements ILockingProvider {
if ($type === self::LOCK_SHARED) {
if (isset($this->acquiredLocks['shared'][$path]) and $this->acquiredLocks['shared'][$path] > 0) {
$this->memcache->dec($path);
- $this->acquiredLocks['shared'][$path]--;
$this->memcache->cad($path, 0);
}
} else if ($type === self::LOCK_EXCLUSIVE) {
$this->memcache->cad($path, 'exclusive');
- unset($this->acquiredLocks['exclusive'][$path]);
}
+ $this->markRelease($path, $type);
}
/**
@@ -113,33 +102,12 @@ class MemcacheLockingProvider implements ILockingProvider {
if (!$this->memcache->cas($path, 'exclusive', 1)) {
throw new LockedException($path);
}
- unset($this->acquiredLocks['exclusive'][$path]);
- if (!isset($this->acquiredLocks['shared'][$path])) {
- $this->acquiredLocks['shared'][$path] = 0;
- }
- $this->acquiredLocks['shared'][$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);
}
- $this->acquiredLocks['exclusive'][$path] = true;
- $this->acquiredLocks['shared'][$path]--;
- }
- }
-
- /**
- * release all lock acquired by this instance
- */
- public function releaseAll() {
- foreach ($this->acquiredLocks['shared'] as $path => $count) {
- for ($i = 0; $i < $count; $i++) {
- $this->releaseLock($path, self::LOCK_SHARED);
- }
- }
-
- foreach ($this->acquiredLocks['exclusive'] as $path => $hasLock) {
- $this->releaseLock($path, self::LOCK_EXCLUSIVE);
}
+ $this->markChange($path, $targetType);
}
}
diff --git a/tests/lib/lock/lockingprovider.php b/tests/lib/lock/lockingprovider.php
index efd6e1939f2..ca72c1bb7f3 100644
--- a/tests/lib/lock/lockingprovider.php
+++ b/tests/lib/lock/lockingprovider.php
@@ -120,6 +120,36 @@ abstract class LockingProvider extends TestCase {
$this->assertFalse($this->instance->isLocked('asd', ILockingProvider::LOCK_EXCLUSIVE));
}
+ public function testReleaseAllAfterChange() {
+ $this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
+ $this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
+ $this->instance->acquireLock('bar', ILockingProvider::LOCK_SHARED);
+ $this->instance->acquireLock('asd', ILockingProvider::LOCK_EXCLUSIVE);
+
+ $this->instance->changeLock('bar', ILockingProvider::LOCK_EXCLUSIVE);
+
+ $this->instance->releaseAll();
+
+ $this->assertFalse($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
+ $this->assertFalse($this->instance->isLocked('bar', ILockingProvider::LOCK_SHARED));
+ $this->assertFalse($this->instance->isLocked('bar', ILockingProvider::LOCK_EXCLUSIVE));
+ $this->assertFalse($this->instance->isLocked('asd', ILockingProvider::LOCK_EXCLUSIVE));
+ }
+
+ public function testReleaseAllAfterUnlock() {
+ $this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
+ $this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
+ $this->instance->acquireLock('bar', ILockingProvider::LOCK_SHARED);
+ $this->instance->acquireLock('asd', ILockingProvider::LOCK_EXCLUSIVE);
+
+ $this->instance->releaseLock('bar', ILockingProvider::LOCK_SHARED);
+
+ $this->instance->releaseAll();
+
+ $this->assertFalse($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
+ $this->assertFalse($this->instance->isLocked('asd', ILockingProvider::LOCK_EXCLUSIVE));
+ }
+
public function testReleaseAfterReleaseAll() {
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);