Skip to content

Commit 7445c66

Browse files
committed
MC-32427: Sign locks in order to prevent unlock from parallel request
1 parent 96ce529 commit 7445c66

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

app/code/Magento/Config/App/Config/Type/System.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use Magento\Store\Model\Config\Processor\Fallback;
2121
use Magento\Framework\Encryption\Encryptor;
2222
use Magento\Store\Model\ScopeInterface as StoreScope;
23+
use Magento\Framework\App\Cache\StateInterface;
24+
use Magento\Framework\App\Cache\Type\Config;
2325

2426
/**
2527
* System configuration type
@@ -98,6 +100,12 @@ class System implements ConfigTypeInterface
98100
private $lockQuery;
99101

100102
/**
103+
* @var StateInterface
104+
*/
105+
private $cacheState;
106+
107+
/**
108+
* System constructor.
101109
* @param ConfigSourceInterface $source
102110
* @param PostProcessorInterface $postProcessor
103111
* @param Fallback $fallback
@@ -110,6 +118,7 @@ class System implements ConfigTypeInterface
110118
* @param Encryptor|null $encryptor
111119
* @param LockManagerInterface|null $locker
112120
* @param LockGuardedCacheLoader|null $lockQuery
121+
* @param StateInterface|null $cacheState
113122
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
114123
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
115124
*/
@@ -125,7 +134,8 @@ public function __construct(
125134
Reader $reader = null,
126135
Encryptor $encryptor = null,
127136
LockManagerInterface $locker = null,
128-
LockGuardedCacheLoader $lockQuery = null
137+
LockGuardedCacheLoader $lockQuery = null,
138+
StateInterface $cacheState = null
129139
) {
130140
$this->postProcessor = $postProcessor;
131141
$this->cache = $cache;
@@ -136,6 +146,8 @@ public function __construct(
136146
?: ObjectManager::getInstance()->get(Encryptor::class);
137147
$this->lockQuery = $lockQuery
138148
?: ObjectManager::getInstance()->get(LockGuardedCacheLoader::class);
149+
$this->cacheState = $cacheState
150+
?: ObjectManager::getInstance()->get(StateInterface::class);
139151
}
140152

141153
/**
@@ -220,6 +232,10 @@ private function getWithParts($path)
220232
*/
221233
private function loadAllData()
222234
{
235+
if (!$this->cacheState->isEnabled(Config::TYPE_IDENTIFIER)) {
236+
return $this->readData();
237+
}
238+
223239
$loadAction = function () {
224240
$cachedData = $this->cache->load($this->configType);
225241
$data = false;
@@ -245,6 +261,10 @@ private function loadAllData()
245261
*/
246262
private function loadDefaultScopeData($scopeType)
247263
{
264+
if (!$this->cacheState->isEnabled(Config::TYPE_IDENTIFIER)) {
265+
return $this->readData();
266+
}
267+
248268
$loadAction = function () use ($scopeType) {
249269
$cachedData = $this->cache->load($this->configType . '_' . $scopeType);
250270
$scopeData = false;
@@ -271,6 +291,10 @@ private function loadDefaultScopeData($scopeType)
271291
*/
272292
private function loadScopeData($scopeType, $scopeId)
273293
{
294+
if (!$this->cacheState->isEnabled(Config::TYPE_IDENTIFIER)) {
295+
return $this->readData();
296+
}
297+
274298
$loadAction = function () use ($scopeType, $scopeId) {
275299
$cachedData = $this->cache->load($this->configType . '_' . $scopeType . '_' . $scopeId);
276300
$scopeData = false;
@@ -393,6 +417,10 @@ public function clean()
393417
$this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [self::CACHE_TAG]);
394418
};
395419

420+
if (!$this->cacheState->isEnabled(Config::TYPE_IDENTIFIER)) {
421+
return $cleanAction();
422+
}
423+
396424
$this->lockQuery->lockedCleanData(
397425
self::$lockName,
398426
$cleanAction

lib/internal/Magento/Framework/App/Cache/Type/AccessProxy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function load($identifier)
8080
public function save($data, $identifier, array $tags = [], $lifeTime = null)
8181
{
8282
if (!$this->_isEnabled()) {
83-
return true;
83+
return false;
8484
}
8585
return parent::save($data, $identifier, $tags, $lifeTime);
8686
}

lib/internal/Magento/Framework/Cache/LockGuardedCacheLoader.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function lockedLoadData(
9191
callable $dataSaver
9292
) {
9393
$cachedData = $dataLoader(); //optimistic read
94-
$deadline = microtime(true) + $this->loadTimeout;
94+
$deadline = microtime(true) + $this->loadTimeout / 100;
9595

9696
while ($cachedData === false) {
9797
if ($deadline <= microtime(true)) {
@@ -129,13 +129,8 @@ public function lockedCleanData(string $lockName, callable $dataCleaner)
129129
while ($this->locker->isLocked($lockName)) {
130130
usleep($this->getLookupTimeout() * 1000);
131131
}
132-
try {
133-
if ($this->locker->lock($lockName, $this->lockTimeout / 1000)) {
134-
$dataCleaner();
135-
}
136-
} finally {
137-
$this->locker->unlock($lockName);
138-
}
132+
133+
$dataCleaner();
139134
}
140135

141136
/**
@@ -147,8 +142,6 @@ public function lockedCleanData(string $lockName, callable $dataCleaner)
147142
*/
148143
private function getLookupTimeout()
149144
{
150-
$lookupTimeout = rand($this->minimalDelayTimeout, $this->delayTimeout);
151-
152-
return $lookupTimeout;
145+
return rand($this->minimalDelayTimeout, $this->delayTimeout);
153146
}
154147
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public function lock(string $name, int $timeout = -1): bool
5555
return false;
5656
}
5757

58-
$saveResult = $this->cache->save($this->lockSign, $this->getIdentifier($name), [], $timeout * 100);
58+
$this->cache->save($this->lockSign, $this->getIdentifier($name), [], $timeout * 100);
5959

6060
$data = $this->cache->load($this->getIdentifier($name));
6161

62-
if ($data === $this->lockSign && $saveResult) {
62+
if ($data === $this->lockSign) {
6363
return true;
6464
}
6565

@@ -125,7 +125,7 @@ private function generateLockSign()
125125
try {
126126
$sign .= '-' . \bin2hex(\random_bytes(4));
127127
} catch (\Exception $e) {
128-
$sign .= '-' . \rand(0, 1000);
128+
$sign .= '-' . \mt_rand(0, 1000);
129129
}
130130

131131
return $sign;

0 commit comments

Comments
 (0)