Skip to content

Commit 8e22fda

Browse files
author
Yevhen Miroshnychenko
authored
Merge pull request #4587 from magento-thunder/MC-18834
Fixed issues: - MC-18834: [Backport] Deadlocks when consumers run from cron
2 parents c090069 + df459a4 commit 8e22fda

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

lib/internal/Magento/Framework/Lock/Backend/Database.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,35 @@
1919
*/
2020
class Database implements \Magento\Framework\Lock\LockManagerInterface
2121
{
22-
/** @var ResourceConnection */
22+
/**
23+
* Max time for lock is 1 week
24+
*
25+
* MariaDB does not support negative timeout value to get infinite timeout,
26+
* so we set 1 week for lock timeout
27+
*/
28+
const MAX_LOCK_TIME = 604800;
29+
30+
/**
31+
* @var ResourceConnection
32+
*/
2333
private $resource;
2434

25-
/** @var DeploymentConfig */
35+
/**
36+
* @var DeploymentConfig
37+
*/
2638
private $deploymentConfig;
2739

28-
/** @var string Lock prefix */
40+
/**
41+
* @var string Lock prefix
42+
*/
2943
private $prefix;
3044

31-
/** @var string|false Holds current lock name if set, otherwise false */
45+
/**
46+
* @var string|false Holds current lock name if set, otherwise false
47+
*/
3248
private $currentLock = false;
3349

3450
/**
35-
* Database constructor.
36-
*
3751
* @param ResourceConnection $resource
3852
* @param DeploymentConfig $deploymentConfig
3953
* @param string|null $prefix
@@ -81,7 +95,7 @@ public function lock(string $name, int $timeout = -1): bool
8195

8296
$result = (bool)$this->resource->getConnection()->query(
8397
"SELECT GET_LOCK(?, ?);",
84-
[(string)$name, (int)$timeout]
98+
[$name, $timeout < 0 ? self::MAX_LOCK_TIME : $timeout]
8599
)->fetchColumn();
86100

87101
if ($result === true) {
@@ -104,6 +118,7 @@ public function unlock(string $name): bool
104118
if (!$this->deploymentConfig->isDbAvailable()) {
105119
return true;
106120
};
121+
107122
$name = $this->addPrefix($name);
108123

109124
$result = (bool)$this->resource->getConnection()->query(
@@ -131,11 +146,12 @@ public function isLocked(string $name): bool
131146
if (!$this->deploymentConfig->isDbAvailable()) {
132147
return false;
133148
};
149+
134150
$name = $this->addPrefix($name);
135151

136152
return (bool)$this->resource->getConnection()->query(
137153
"SELECT IS_USED_LOCK(?);",
138-
[(string)$name]
154+
[$name]
139155
)->fetchColumn();
140156
}
141157

@@ -145,7 +161,7 @@ public function isLocked(string $name): bool
145161
* Limited to 64 characters in MySQL.
146162
*
147163
* @param string $name
148-
* @return string $name
164+
* @return string
149165
* @throws InputException
150166
*/
151167
private function addPrefix(string $name): string

0 commit comments

Comments
 (0)