Make lock ttl configurable

This commit is contained in:
Robin Appelman 2016-03-24 14:07:43 +01:00 committed by Roeland Jago Douma
parent 9b930cd01d
commit cdedda99e4
6 changed files with 26 additions and 10 deletions

View File

@ -1193,6 +1193,15 @@ $CONFIG = array(
*/ */
'filelocking.enabled' => true, 'filelocking.enabled' => true,
/**
* Set the time-to-live for locks in secconds.
*
* Any lock older than this will be automatically cleaned up.
*
* If not set this defaults to either 1 hour or the php max_execution_time, whichever is higher.
*/
'filelocking.ttl' => 3600,
/** /**
* Memory caching backend for file locking * Memory caching backend for file locking
* *

View File

@ -519,14 +519,17 @@ class Server extends ServerContainer implements IServerContainer {
); );
}); });
$this->registerService('LockingProvider', function (Server $c) { $this->registerService('LockingProvider', function (Server $c) {
if ($c->getConfig()->getSystemValue('filelocking.enabled', true) or (defined('PHPUNIT_RUN') && PHPUNIT_RUN)) { $ini = $c->getIniWrapper();
$config = $c->getConfig();
$ttl = $config->getSystemValue('filelocking.ttl', max(3600, $ini->getNumeric('max_execution_time')));
if ($config->getSystemValue('filelocking.enabled', true) or (defined('PHPUNIT_RUN') && PHPUNIT_RUN)) {
/** @var \OC\Memcache\Factory $memcacheFactory */ /** @var \OC\Memcache\Factory $memcacheFactory */
$memcacheFactory = $c->getMemCacheFactory(); $memcacheFactory = $c->getMemCacheFactory();
$memcache = $memcacheFactory->createLocking('lock'); $memcache = $memcacheFactory->createLocking('lock');
if (!($memcache instanceof \OC\Memcache\NullCache)) { if (!($memcache instanceof \OC\Memcache\NullCache)) {
return new MemcacheLockingProvider($memcache); return new MemcacheLockingProvider($memcache, $ttl);
} }
return new DBLockingProvider($c->getDatabaseConnection(), $c->getLogger(), new TimeFactory()); return new DBLockingProvider($c->getDatabaseConnection(), $c->getLogger(), new TimeFactory(), $ttl);
} }
return new NoopLockingProvider(); return new NoopLockingProvider();
}); });

View File

@ -28,7 +28,7 @@ use OCP\Lock\ILockingProvider;
* to release any left over locks at the end of the request * to release any left over locks at the end of the request
*/ */
abstract class AbstractLockingProvider implements ILockingProvider { abstract class AbstractLockingProvider implements ILockingProvider {
const TTL = 3600; // how long until we clear stray locks in seconds protected $ttl; // how long until we clear stray locks in seconds
protected $acquiredLocks = [ protected $acquiredLocks = [
'shared' => [], 'shared' => [],

View File

@ -93,11 +93,13 @@ class DBLockingProvider extends AbstractLockingProvider {
* @param \OCP\IDBConnection $connection * @param \OCP\IDBConnection $connection
* @param \OCP\ILogger $logger * @param \OCP\ILogger $logger
* @param \OCP\AppFramework\Utility\ITimeFactory $timeFactory * @param \OCP\AppFramework\Utility\ITimeFactory $timeFactory
* @param int $ttl
*/ */
public function __construct(IDBConnection $connection, ILogger $logger, ITimeFactory $timeFactory) { public function __construct(IDBConnection $connection, ILogger $logger, ITimeFactory $timeFactory, $ttl = 3600) {
$this->connection = $connection; $this->connection = $connection;
$this->logger = $logger; $this->logger = $logger;
$this->timeFactory = $timeFactory; $this->timeFactory = $timeFactory;
$this->ttl = $ttl;
} }
/** /**
@ -117,7 +119,7 @@ class DBLockingProvider extends AbstractLockingProvider {
* @return int * @return int
*/ */
protected function getExpireTime() { protected function getExpireTime() {
return $this->timeFactory->getTime() + self::TTL; return $this->timeFactory->getTime() + $this->ttl;
} }
/** /**

View File

@ -33,14 +33,16 @@ class MemcacheLockingProvider extends AbstractLockingProvider {
/** /**
* @param \OCP\IMemcache $memcache * @param \OCP\IMemcache $memcache
* @param int $ttl
*/ */
public function __construct(IMemcache $memcache) { public function __construct(IMemcache $memcache, $ttl = 3600) {
$this->memcache = $memcache; $this->memcache = $memcache;
$this->ttl = $ttl;
} }
private function setTTL($path) { private function setTTL($path) {
if ($this->memcache instanceof IMemcacheTTL) { if ($this->memcache instanceof IMemcacheTTL) {
$this->memcache->setTTL($path, self::TTL); $this->memcache->setTTL($path, $this->ttl);
} }
} }

View File

@ -64,7 +64,7 @@ class DBLockingProvider extends LockingProvider {
*/ */
protected function getInstance() { protected function getInstance() {
$this->connection = \OC::$server->getDatabaseConnection(); $this->connection = \OC::$server->getDatabaseConnection();
return new \OC\Lock\DBLockingProvider($this->connection, \OC::$server->getLogger(), $this->timeFactory); return new \OC\Lock\DBLockingProvider($this->connection, \OC::$server->getLogger(), $this->timeFactory, 3600);
} }
public function tearDown() { public function tearDown() {
@ -81,7 +81,7 @@ class DBLockingProvider extends LockingProvider {
$this->instance->acquireLock('bar', ILockingProvider::LOCK_EXCLUSIVE); $this->instance->acquireLock('bar', ILockingProvider::LOCK_EXCLUSIVE);
$this->instance->changeLock('asd', ILockingProvider::LOCK_SHARED); $this->instance->changeLock('asd', ILockingProvider::LOCK_SHARED);
$this->currentTime = 150 + \OC\Lock\DBLockingProvider::TTL; $this->currentTime = 150 + 3600;
$this->assertEquals(3, $this->getLockEntryCount()); $this->assertEquals(3, $this->getLockEntryCount());