Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

High level file locking #16237

Merged
merged 35 commits into from
Jun 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
cdf7f91
expose locking provider in the server container
icewind1991 May 4, 2015
536e187
add locking to the storage api
icewind1991 May 4, 2015
bf7002b
add locking to the view apo
icewind1991 May 4, 2015
e64360e
always use arraycache for unit tests
icewind1991 May 4, 2015
7e418c7
high level locking wip
icewind1991 May 5, 2015
a1a25a9
fix unlocking when moving mount points
icewind1991 May 11, 2015
d519aba
fix test
icewind1991 May 11, 2015
5edf294
Add CAS methods to Null memcache
May 13, 2015
0775e9c
Use md5 for lock key
May 13, 2015
8d53dc8
Use md5 + prefix for file locking keys in memcache
May 13, 2015
35c377f
phpdoc and minor issues
icewind1991 May 13, 2015
1270c68
dont lock on meta data operations
icewind1991 May 13, 2015
2d63fd7
dont apply callback wrapper when fopen failed
icewind1991 May 13, 2015
e08423f
release all locks on shutdown
icewind1991 May 19, 2015
b98dd3c
release all locks after test
icewind1991 May 20, 2015
f0b8672
fix locking root of a view
icewind1991 May 20, 2015
006eaa8
dont release shared lock if we dont have any
icewind1991 May 20, 2015
6df502a
Fix Null memcache fallback to match interface
May 21, 2015
c72ea9f
unit test for releaseall
icewind1991 May 21, 2015
2f4f468
Added config switch for file locking
May 21, 2015
668fafd
close file handle after sending sabre response
icewind1991 May 21, 2015
437c0b5
unlock source file when we cant lock the target in a rename
icewind1991 May 22, 2015
72847db
always use locking in unit tests
icewind1991 May 22, 2015
6b965d7
add seperate config option for locking memcache backend
icewind1991 May 26, 2015
72776b1
use arraycache for locking in unit tests
icewind1991 May 26, 2015
8665a98
add locking for non-chunking webdav upload
icewind1991 May 27, 2015
ba174ac
Convert LockedException to FileLocked in Sabre connector
May 29, 2015
0451a66
Move locking exceptions
May 29, 2015
270a10b
Return 423 instead of 503 for locked files
May 29, 2015
43772e2
Adding information on file locking status to admin section
DeepDiver1975 May 29, 2015
a1372b2
add method to atomically change between shared and exclusive lock
icewind1991 May 29, 2015
661c9e2
add changeLock to the storage api
icewind1991 May 29, 2015
ce04cf6
shared lock around hooks
icewind1991 May 29, 2015
8902e2b
fix nooplockingprovider
icewind1991 Jun 1, 2015
2104c2f
Fixing undefined index 'foo'
DeepDiver1975 Jun 1, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions apps/files_sharing/lib/sharedstorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

use OC\Files\Filesystem;
use OCA\Files_Sharing\ISharedStorage;
use OCA\Files_Sharing\Propagator;
use OCA\Files_Sharing\SharedMount;
use OCP\Lock\ILockingProvider;

/**
* Convert target path to source path and pass the function call to the correct storage provider
Expand Down Expand Up @@ -608,4 +611,38 @@ public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceIntern
list($targetStorage, $targetInternalPath) = $this->resolvePath($targetInternalPath);
return $targetStorage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
}

/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
* @throws \OCP\Lock\LockedException
*/
public function acquireLock($path, $type, ILockingProvider $provider) {
/** @var \OCP\Files\Storage $targetStorage */
list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
$targetStorage->acquireLock($targetInternalPath, $type, $provider);
}

/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
*/
public function releaseLock($path, $type, ILockingProvider $provider) {
/** @var \OCP\Files\Storage $targetStorage */
list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
$targetStorage->releaseLock($targetInternalPath, $type, $provider);
}

/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
*/
public function changeLock($path, $type, ILockingProvider $provider) {
/** @var \OCP\Files\Storage $targetStorage */
list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
$targetStorage->changeLock($targetInternalPath, $type, $provider);
}
}
19 changes: 19 additions & 0 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,25 @@
*/
'max_filesize_animated_gifs_public_sharing' => 10,


/**
* Enables the EXPERIMENTAL file locking.
* This is disabled by default as it is experimental.
*
* Prevents concurrent processes to access the same files
* at the same time. Can help prevent side effects that would
* be caused by concurrent operations.
*
* WARNING: EXPERIMENTAL
*/
'filelocking.enabled' => false,

/**
* Memory caching backend for file locking
* Because most memcache backends can clean values without warning using redis is recommended
*/
'memcache.locking' => '\OC\Memcache\Redis',

/**
* This entry is just here to show a warning in case somebody copied the sample
* configuration. DO NOT ADD THIS SWITCH TO YOUR CONFIGURATION!
Expand Down
2 changes: 2 additions & 0 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,8 @@ public static function init() {
//make sure temporary files are cleaned up
$tmpManager = \OC::$server->getTempManager();
register_shutdown_function(array($tmpManager, 'clean'));
$lockProvider = \OC::$server->getLockingProvider();
register_shutdown_function(array($lockProvider, 'releaseAll'));

if ($systemConfig->getValue('installed', false) && !self::checkUpgrade(false)) {
if (\OC::$server->getConfig()->getAppValue('core', 'backgroundjobs_mode', 'ajax') == 'ajax') {
Expand Down
17 changes: 13 additions & 4 deletions lib/private/connector/sabre/directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
namespace OC\Connector\Sabre;

use OC\Connector\Sabre\Exception\InvalidPath;
use OC\Connector\Sabre\Exception\FileLocked;
use OCP\Lock\LockedException;

class Directory extends \OC\Connector\Sabre\Node
implements \Sabre\DAV\ICollection, \Sabre\DAV\IQuota {
Expand Down Expand Up @@ -102,6 +104,8 @@ public function createFile($name, $data = null) {
return $node->put($data);
} catch (\OCP\Files\StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
}

Expand All @@ -127,6 +131,8 @@ public function createDirectory($name) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (\OCP\Files\InvalidPathException $ex) {
throw new InvalidPath($ex->getMessage());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
}

Expand Down Expand Up @@ -211,11 +217,14 @@ public function delete() {
throw new \Sabre\DAV\Exception\Forbidden();
}

if (!$this->fileView->rmdir($this->path)) {
// assume it wasn't possible to remove due to permission issue
throw new \Sabre\DAV\Exception\Forbidden();
try {
if (!$this->fileView->rmdir($this->path)) {
// assume it wasn't possible to remove due to permission issue
throw new \Sabre\DAV\Exception\Forbidden();
}
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}

}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/private/connector/sabre/exception/filelocked.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ public function __construct($message = "", $code = 0, Exception $previous = null
*/
public function getHTTPCode() {

return 503;
return 423;
}
}
17 changes: 17 additions & 0 deletions lib/private/connector/sabre/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
use OCP\Files\LockNotAcquiredException;
use OCP\Files\NotPermittedException;
use OCP\Files\StorageNotAvailableException;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use Sabre\DAV\Exception;
use Sabre\DAV\Exception\BadRequest;
use Sabre\DAV\Exception\Forbidden;
Expand Down Expand Up @@ -79,6 +81,7 @@ class File extends Node implements IFile {
* @throws Exception
* @throws EntityTooLarge
* @throws ServiceUnavailable
* @throws FileLocked
* @return string|null
*/
public function put($data) {
Expand Down Expand Up @@ -110,6 +113,12 @@ public function put($data) {
$partFilePath = $this->path;
}

try {
$this->fileView->lockFile($this->path, ILockingProvider::LOCK_EXCLUSIVE);
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}

// the part file and target file might be on a different storage in case of a single file storage (e.g. single file share)
/** @var \OC\Files\Storage\Storage $partStorage */
list($partStorage, $internalPartPath) = $this->fileView->resolvePath($partFilePath);
Expand Down Expand Up @@ -232,6 +241,8 @@ public function put($data) {
throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage());
}

$this->fileView->unlockFile($this->path, ILockingProvider::LOCK_EXCLUSIVE);

return '"' . $this->info->getEtag() . '"';
}

Expand All @@ -252,6 +263,8 @@ public function get() {
throw new ServiceUnavailable("Encryption not ready: " . $e->getMessage());
} catch (StorageNotAvailableException $e) {
throw new ServiceUnavailable("Failed to open file: " . $e->getMessage());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
}

Expand All @@ -273,6 +286,8 @@ public function delete() {
}
} catch (StorageNotAvailableException $e) {
throw new ServiceUnavailable("Failed to unlink: " . $e->getMessage());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
}

Expand Down Expand Up @@ -378,6 +393,8 @@ private function createFileChunked($data) {
return $info->getEtag();
} catch (StorageNotAvailableException $e) {
throw new ServiceUnavailable("Failed to put file: " . $e->getMessage());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
}

Expand Down
6 changes: 6 additions & 0 deletions lib/private/connector/sabre/filesplugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public function initialize(\Sabre\DAV\Server $server) {
$this->server->on('afterBind', array($this, 'sendFileIdHeader'));
$this->server->on('afterWriteContent', array($this, 'sendFileIdHeader'));
$this->server->on('afterMethod:GET', [$this,'httpGet']);
$this->server->on('afterResponse', function($request, ResponseInterface $response) {
$body = $response->getBody();
if (is_resource($body)) {
fclose($body);
}
});
}

/**
Expand Down
10 changes: 8 additions & 2 deletions lib/private/connector/sabre/objecttree.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
namespace OC\Connector\Sabre;

use OC\Connector\Sabre\Exception\InvalidPath;
use OC\Connector\Sabre\Exception\FileLocked;
use OC\Files\FileInfo;
use OC\Files\Mount\MoveableMount;
use OCP\Files\StorageInvalidException;
use OCP\Files\StorageNotAvailableException;
use OCP\Lock\LockedException;

class ObjectTree extends \Sabre\DAV\Tree {

Expand Down Expand Up @@ -221,8 +223,10 @@ public function move($sourcePath, $destinationPath) {
if (!$renameOkay) {
throw new \Sabre\DAV\Exception\Forbidden('');
}
} catch (\OCP\Files\StorageNotAvailableException $e) {
} catch (StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}

$this->markDirty($sourceDir);
Expand Down Expand Up @@ -258,8 +262,10 @@ public function copy($source, $destination) {

try {
$this->fileView->copy($source, $destination);
} catch (\OCP\Files\StorageNotAvailableException $e) {
} catch (StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}

list($destinationDir,) = \Sabre\HTTP\URLUtil::splitPath($destination);
Expand Down
29 changes: 29 additions & 0 deletions lib/private/files/storage/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use OCP\Files\InvalidCharacterInPathException;
use OCP\Files\InvalidPathException;
use OCP\Files\ReservedWordException;
use OCP\Lock\ILockingProvider;

/**
* Storage backend class for providing common filesystem operation methods
Expand Down Expand Up @@ -621,4 +622,32 @@ public function getMetaData($path) {

return $data;
}

/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
* @throws \OCP\Lock\LockedException
*/
public function acquireLock($path, $type, ILockingProvider $provider) {
$provider->acquireLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type);
}

/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
*/
public function releaseLock($path, $type, ILockingProvider $provider) {
$provider->releaseLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type);
}

/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
*/
public function changeLock($path, $type, ILockingProvider $provider) {
$provider->changeLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type);
}
}
23 changes: 23 additions & 0 deletions lib/private/files/storage/storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

namespace OC\Files\Storage;
use OCP\Lock\ILockingProvider;

/**
* Provide a common interface to all different storage options
Expand Down Expand Up @@ -76,4 +77,26 @@ public function getStorageCache();
*/
public function getMetaData($path);

/**
* @param string $path The path of the file to acquire the lock for
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
* @throws \OCP\Lock\LockedException
*/
public function acquireLock($path, $type, ILockingProvider $provider);

/**
* @param string $path The path of the file to release the lock for
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
*/
public function releaseLock($path, $type, ILockingProvider $provider);

/**
* @param string $path The path of the file to change the lock for
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
* @throws \OCP\Lock\LockedException
*/
public function changeLock($path, $type, ILockingProvider $provider);
}
29 changes: 29 additions & 0 deletions lib/private/files/storage/wrapper/jail.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace OC\Files\Storage\Wrapper;

use OC\Files\Cache\Wrapper\CacheJail;
use OCP\Lock\ILockingProvider;

/**
* Jail to a subdirectory of the wrapped storage
Expand Down Expand Up @@ -424,4 +425,32 @@ public function getWatcher($path = '', $storage = null) {
public function getETag($path) {
return $this->storage->getETag($this->getSourcePath($path));
}

/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
* @throws \OCP\Lock\LockedException
*/
public function acquireLock($path, $type, ILockingProvider $provider) {
$this->storage->acquireLock($this->getSourcePath($path), $type, $provider);
}

/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
*/
public function releaseLock($path, $type, ILockingProvider $provider) {
$this->storage->releaseLock($this->getSourcePath($path), $type, $provider);
}

/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
*/
public function changeLock($path, $type, ILockingProvider $provider) {
$this->storage->changeLock($this->getSourcePath($path), $type, $provider);
}
}
Loading