split off keeping track of acquire locks

This commit is contained in:
Robin Appelman 2015-07-15 15:44:51 +02:00
parent af7bcb43b1
commit a8552a1b24
3 changed files with 130 additions and 36 deletions

View file

@ -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);
}
}
}

View file

@ -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);
}
}

View file

@ -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);