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

[stable28] Add retry logic to cover deadlock situations during move operations #44551

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Changes from all commits
Commits
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
31 changes: 25 additions & 6 deletions lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

namespace OC\Files\Cache;

use Doctrine\DBAL\Exception\RetryableException;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OC\Files\Search\SearchComparison;
use OC\Files\Search\SearchQuery;
Expand Down Expand Up @@ -695,7 +696,6 @@ public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
throw new \Exception('Invalid target storage id: ' . $targetStorageId);
}

$this->connection->beginTransaction();
if ($sourceData['mimetype'] === 'httpd/unix-directory') {
//update all child entries
$sourceLength = mb_strlen($sourcePath);
Expand All @@ -718,12 +718,31 @@ public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
$query->set('encrypted', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT));
}

try {
$query->execute();
} catch (\OC\DatabaseException $e) {
$this->connection->rollBack();
throw $e;
// Retry transaction in case of RetryableException like deadlocks.
// Retry up to 4 times because we should receive up to 4 concurrent requests from the frontend
$retryLimit = 4;
for ($i = 1; $i <= $retryLimit; $i++) {
try {
$this->connection->beginTransaction();
$query->executeStatement();
break;
} catch (\OC\DatabaseException $e) {
$this->connection->rollBack();
throw $e;
} catch (RetryableException $e) {
// Simply throw if we already retried 4 times.
if ($i === $retryLimit) {
throw $e;
}

$this->connection->rollBack();

// Sleep a bit to give some time to the other transaction to finish.
usleep(100 * 1000 * $i);
}
}
} else {
$this->connection->beginTransaction();
}

$query = $this->getQueryBuilder();
Expand Down
Loading