-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Insert filecache data and conflict fallback read in the same transaction #36313
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -41,8 +41,11 @@ | |||||
namespace OC\Files\Cache; | ||||||
|
||||||
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; | ||||||
use OC\DB\Exceptions\DbalException; | ||||||
use OC\Files\Search\SearchComparison; | ||||||
use OC\Files\Search\SearchQuery; | ||||||
use OCP\AppFramework\Db\TTransactional; | ||||||
use OCP\DB\Exception as DBException; | ||||||
use OCP\DB\QueryBuilder\IQueryBuilder; | ||||||
use OCP\EventDispatcher\IEventDispatcher; | ||||||
use OCP\Files\Cache\CacheEntryInsertedEvent; | ||||||
|
@@ -72,6 +75,8 @@ | |||||
* - ChangePropagator: updates the mtime and etags of parent folders whenever a change to the cache is made to the cache by the updater | ||||||
*/ | ||||||
class Cache implements ICache { | ||||||
use TTransactional; | ||||||
|
||||||
use MoveFromCacheTrait { | ||||||
MoveFromCacheTrait::moveFromCache as moveFromCacheFallback; | ||||||
} | ||||||
|
@@ -271,7 +276,8 @@ public function put($file, array $data) { | |||||
* @param array $data | ||||||
* | ||||||
* @return int file id | ||||||
* @throws \RuntimeException | ||||||
* @throws Exception | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* @throws \Throwable | ||||||
*/ | ||||||
public function insert($file, array $data) { | ||||||
// normalize file | ||||||
|
@@ -300,48 +306,48 @@ public function insert($file, array $data) { | |||||
$storageId = $this->getNumericStorageId(); | ||||||
$values['storage'] = $storageId; | ||||||
|
||||||
try { | ||||||
$builder = $this->connection->getQueryBuilder(); | ||||||
$builder->insert('filecache'); | ||||||
return $this->atomic(function () use ($values, $extensionValues, $file, $storageId, $data) { | ||||||
try { | ||||||
$builder = $this->connection->getQueryBuilder(); | ||||||
$builder->insert('filecache'); | ||||||
|
||||||
foreach ($values as $column => $value) { | ||||||
$builder->setValue($column, $builder->createNamedParameter($value)); | ||||||
} | ||||||
foreach ($values as $column => $value) { | ||||||
$builder->setValue($column, $builder->createNamedParameter($value)); | ||||||
} | ||||||
|
||||||
if ($builder->execute()) { | ||||||
$fileId = $builder->getLastInsertId(); | ||||||
if ($builder->executeStatement()) { | ||||||
$fileId = $builder->getLastInsertId(); | ||||||
|
||||||
if (count($extensionValues)) { | ||||||
$query = $this->getQueryBuilder(); | ||||||
$query->insert('filecache_extended'); | ||||||
if (count($extensionValues)) { | ||||||
$query = $this->getQueryBuilder(); | ||||||
$query->insert('filecache_extended'); | ||||||
|
||||||
$query->setValue('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)); | ||||||
foreach ($extensionValues as $column => $value) { | ||||||
$query->setValue($column, $query->createNamedParameter($value)); | ||||||
$query->setValue('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)); | ||||||
foreach ($extensionValues as $column => $value) { | ||||||
$query->setValue($column, $query->createNamedParameter($value)); | ||||||
} | ||||||
$query->execute(); | ||||||
} | ||||||
$query->execute(); | ||||||
|
||||||
$event = new CacheEntryInsertedEvent($this->storage, $file, $fileId, $storageId); | ||||||
$this->eventDispatcher->dispatch(CacheInsertEvent::class, $event); | ||||||
$this->eventDispatcher->dispatchTyped($event); | ||||||
return $fileId; | ||||||
} | ||||||
} catch (DbalException $e) { | ||||||
if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) { | ||||||
throw $e; | ||||||
} | ||||||
|
||||||
$event = new CacheEntryInsertedEvent($this->storage, $file, $fileId, $storageId); | ||||||
$this->eventDispatcher->dispatch(CacheInsertEvent::class, $event); | ||||||
$this->eventDispatcher->dispatchTyped($event); | ||||||
return $fileId; | ||||||
} | ||||||
} catch (UniqueConstraintViolationException $e) { | ||||||
// entry exists already | ||||||
if ($this->connection->inTransaction()) { | ||||||
$this->connection->commit(); | ||||||
$this->connection->beginTransaction(); | ||||||
// The file was created in the mean time | ||||||
if (($id = $this->getId($file)) > -1) { | ||||||
$this->update($id, $data); | ||||||
return $id; | ||||||
} else { | ||||||
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.'); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
// The file was created in the mean time | ||||||
if (($id = $this->getId($file)) > -1) { | ||||||
$this->update($id, $data); | ||||||
return $id; | ||||||
} else { | ||||||
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.'); | ||||||
} | ||||||
}, $this->connection); | ||||||
} | ||||||
|
||||||
/** | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / Psalm
UndefinedDocblockClass