Merge pull request #12434 from nextcloud/backport/12411-12413/unique-constraint-fix-13

[stable13] Unique contraint and deadlock fixes for filecache and file_locks
This commit is contained in:
Morris Jobke 2018-11-13 15:03:51 +01:00 committed by GitHub
commit 204466c714
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 8 deletions

View File

@ -37,6 +37,7 @@
namespace OC\Files\Cache; namespace OC\Files\Cache;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\DB\QueryBuilder\IQueryBuilder;
use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Driver\Statement;
use OCP\Files\Cache\ICache; use OCP\Files\Cache\ICache;
@ -239,6 +240,8 @@ class Cache implements ICache {
* *
* @return int file id * @return int file id
* @throws \RuntimeException * @throws \RuntimeException
*
* @suppress SqlInjectionChecker
*/ */
public function insert($file, array $data) { public function insert($file, array $data) {
// normalize file // normalize file
@ -269,20 +272,28 @@ class Cache implements ICache {
return trim($item, "`"); return trim($item, "`");
}, $queryParts); }, $queryParts);
$values = array_combine($queryParts, $params); $values = array_combine($queryParts, $params);
if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [
'storage', try {
'path_hash', $builder = $this->connection->getQueryBuilder();
]) $builder->insert('filecache');
) {
foreach ($values as $column => $value) {
$builder->setValue($column, $builder->createNamedParameter($value));
}
if ($builder->execute()) {
return (int)$this->connection->lastInsertId('*PREFIX*filecache'); return (int)$this->connection->lastInsertId('*PREFIX*filecache');
} }
} catch(UniqueConstraintViolationException $e) {
// entry exists already
}
// The file was created in the mean time // The file was created in the mean time
if (($id = $this->getId($file)) > -1) { if (($id = $this->getId($file)) > -1) {
$this->update($id, $data); $this->update($id, $data);
return $id; return $id;
} else { } else {
throw new \RuntimeException('File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.'); throw new \RuntimeException('File entry could not be inserted but could also not be selected with getId() in order to perform an update. Please try again.');
} }
} }

View File

@ -26,6 +26,7 @@
namespace OC\Lock; namespace OC\Lock;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OC\DB\QueryBuilder\Literal; use OC\DB\QueryBuilder\Literal;
use OCP\AppFramework\Utility\ITimeFactory; use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\DB\QueryBuilder\IQueryBuilder;
@ -116,7 +117,17 @@ class DBLockingProvider extends AbstractLockingProvider {
protected function initLockField($path, $lock = 0) { protected function initLockField($path, $lock = 0) {
$expire = $this->getExpireTime(); $expire = $this->getExpireTime();
return $this->connection->insertIfNotExist('*PREFIX*file_locks', ['key' => $path, 'lock' => $lock, 'ttl' => $expire], ['key']);
try {
$builder = $this->connection->getQueryBuilder();
return $builder->insert('file_locks')
->setValue('key', $builder->createNamedParameter($path))
->setValue('lock', $builder->createNamedParameter($lock))
->setValue('ttl', $builder->createNamedParameter($expire))
->execute();
} catch(UniqueConstraintViolationException $e) {
return 0;
}
} }
/** /**