Skip to content

Commit 5a35838

Browse files
merge magento/2.3-develop into magento-epam/EPAM-PR-47
2 parents d45be36 + 06269b7 commit 5a35838

File tree

10 files changed

+347
-185
lines changed

10 files changed

+347
-185
lines changed

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

Lines changed: 50 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Config\App\Config\Type;
78

89
use Magento\Framework\App\Config\ConfigSourceInterface;
@@ -13,18 +14,20 @@
1314
use Magento\Config\App\Config\Type\System\Reader;
1415
use Magento\Framework\App\ScopeInterface;
1516
use Magento\Framework\Cache\FrontendInterface;
17+
use Magento\Framework\Cache\LockGuardedCacheLoader;
1618
use Magento\Framework\Lock\LockManagerInterface;
1719
use Magento\Framework\Serialize\SerializerInterface;
1820
use Magento\Store\Model\Config\Processor\Fallback;
19-
use Magento\Store\Model\ScopeInterface as StoreScope;
2021
use Magento\Framework\Encryption\Encryptor;
22+
use Magento\Store\Model\ScopeInterface as StoreScope;
2123

2224
/**
2325
* System configuration type
2426
*
2527
* @api
2628
* @since 100.1.2
2729
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
30+
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
2831
*/
2932
class System implements ConfigTypeInterface
3033
{
@@ -42,22 +45,6 @@ class System implements ConfigTypeInterface
4245
* @var string
4346
*/
4447
private static $lockName = 'SYSTEM_CONFIG';
45-
/**
46-
* Timeout between retrieves to load the configuration from the cache.
47-
*
48-
* Value of the variable in microseconds.
49-
*
50-
* @var int
51-
*/
52-
private static $delayTimeout = 100000;
53-
/**
54-
* Lifetime of the lock for write in cache.
55-
*
56-
* Value of the variable in seconds.
57-
*
58-
* @var int
59-
*/
60-
private static $lockTimeout = 42;
6148

6249
/**
6350
* @var array
@@ -106,9 +93,9 @@ class System implements ConfigTypeInterface
10693
private $encryptor;
10794

10895
/**
109-
* @var LockManagerInterface
96+
* @var LockGuardedCacheLoader
11097
*/
111-
private $locker;
98+
private $lockQuery;
11299

113100
/**
114101
* @param ConfigSourceInterface $source
@@ -122,6 +109,7 @@ class System implements ConfigTypeInterface
122109
* @param Reader|null $reader
123110
* @param Encryptor|null $encryptor
124111
* @param LockManagerInterface|null $locker
112+
* @param LockGuardedCacheLoader|null $lockQuery
125113
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
126114
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
127115
*/
@@ -136,7 +124,8 @@ public function __construct(
136124
$configType = self::CONFIG_TYPE,
137125
Reader $reader = null,
138126
Encryptor $encryptor = null,
139-
LockManagerInterface $locker = null
127+
LockManagerInterface $locker = null,
128+
LockGuardedCacheLoader $lockQuery = null
140129
) {
141130
$this->postProcessor = $postProcessor;
142131
$this->cache = $cache;
@@ -145,8 +134,8 @@ public function __construct(
145134
$this->reader = $reader ?: ObjectManager::getInstance()->get(Reader::class);
146135
$this->encryptor = $encryptor
147136
?: ObjectManager::getInstance()->get(Encryptor::class);
148-
$this->locker = $locker
149-
?: ObjectManager::getInstance()->get(LockManagerInterface::class);
137+
$this->lockQuery = $lockQuery
138+
?: ObjectManager::getInstance()->get(LockGuardedCacheLoader::class);
150139
}
151140

152141
/**
@@ -225,91 +214,64 @@ private function getWithParts($path)
225214
}
226215

227216
/**
228-
* Make lock on data load.
229-
*
230-
* @param callable $dataLoader
231-
* @param bool $flush
232-
* @return array
233-
*/
234-
private function lockedLoadData(callable $dataLoader, bool $flush = false): array
235-
{
236-
$cachedData = $dataLoader(); //optimistic read
237-
238-
while ($cachedData === false && $this->locker->isLocked(self::$lockName)) {
239-
usleep(self::$delayTimeout);
240-
$cachedData = $dataLoader();
241-
}
242-
243-
while ($cachedData === false) {
244-
try {
245-
if ($this->locker->lock(self::$lockName, self::$lockTimeout)) {
246-
if (!$flush) {
247-
$data = $this->readData();
248-
$this->cacheData($data);
249-
$cachedData = $data;
250-
} else {
251-
$this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [self::CACHE_TAG]);
252-
$cachedData = [];
253-
}
254-
}
255-
} finally {
256-
$this->locker->unlock(self::$lockName);
257-
}
258-
259-
if ($cachedData === false) {
260-
usleep(self::$delayTimeout);
261-
$cachedData = $dataLoader();
262-
}
263-
}
264-
265-
return $cachedData;
266-
}
267-
268-
/**
269-
* Load configuration data for all scopes
217+
* Load configuration data for all scopes.
270218
*
271219
* @return array
272220
*/
273221
private function loadAllData()
274222
{
275-
return $this->lockedLoadData(function () {
223+
$loadAction = function () {
276224
$cachedData = $this->cache->load($this->configType);
277225
$data = false;
278226
if ($cachedData !== false) {
279227
$data = $this->serializer->unserialize($this->encryptor->decrypt($cachedData));
280228
}
281229
return $data;
282-
});
230+
};
231+
232+
return $this->lockQuery->lockedLoadData(
233+
self::$lockName,
234+
$loadAction,
235+
\Closure::fromCallable([$this, 'readData']),
236+
\Closure::fromCallable([$this, 'cacheData'])
237+
);
283238
}
284239

285240
/**
286-
* Load configuration data for default scope
241+
* Load configuration data for default scope.
287242
*
288243
* @param string $scopeType
289244
* @return array
290245
*/
291246
private function loadDefaultScopeData($scopeType)
292247
{
293-
return $this->lockedLoadData(function () use ($scopeType) {
248+
$loadAction = function () use ($scopeType) {
294249
$cachedData = $this->cache->load($this->configType . '_' . $scopeType);
295250
$scopeData = false;
296251
if ($cachedData !== false) {
297252
$scopeData = [$scopeType => $this->serializer->unserialize($this->encryptor->decrypt($cachedData))];
298253
}
299254
return $scopeData;
300-
});
255+
};
256+
257+
return $this->lockQuery->lockedLoadData(
258+
self::$lockName,
259+
$loadAction,
260+
\Closure::fromCallable([$this, 'readData']),
261+
\Closure::fromCallable([$this, 'cacheData'])
262+
);
301263
}
302264

303265
/**
304-
* Load configuration data for a specified scope
266+
* Load configuration data for a specified scope.
305267
*
306268
* @param string $scopeType
307269
* @param string $scopeId
308270
* @return array
309271
*/
310272
private function loadScopeData($scopeType, $scopeId)
311273
{
312-
return $this->lockedLoadData(function () use ($scopeType, $scopeId) {
274+
$loadAction = function () use ($scopeType, $scopeId) {
313275
$cachedData = $this->cache->load($this->configType . '_' . $scopeType . '_' . $scopeId);
314276
$scopeData = false;
315277
if ($cachedData === false) {
@@ -329,7 +291,14 @@ private function loadScopeData($scopeType, $scopeId)
329291
}
330292

331293
return $scopeData;
332-
});
294+
};
295+
296+
return $this->lockQuery->lockedLoadData(
297+
self::$lockName,
298+
$loadAction,
299+
\Closure::fromCallable([$this, 'readData']),
300+
\Closure::fromCallable([$this, 'cacheData'])
301+
);
333302
}
334303

335304
/**
@@ -371,7 +340,7 @@ private function cacheData(array $data)
371340
}
372341

373342
/**
374-
* Walk nested hash map by keys from $pathParts
343+
* Walk nested hash map by keys from $pathParts.
375344
*
376345
* @param array $data to walk in
377346
* @param array $pathParts keys path
@@ -408,7 +377,7 @@ private function readData(): array
408377
}
409378

410379
/**
411-
* Clean cache and global variables cache
380+
* Clean cache and global variables cache.
412381
*
413382
* Next items cleared:
414383
* - Internal property intended to store already loaded configuration data
@@ -420,11 +389,13 @@ private function readData(): array
420389
public function clean()
421390
{
422391
$this->data = [];
423-
$this->lockedLoadData(
424-
function () {
425-
return false;
426-
},
427-
true
392+
$cleanAction = function () {
393+
$this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [self::CACHE_TAG]);
394+
};
395+
396+
$this->lockQuery->lockedCleanData(
397+
self::$lockName,
398+
$cleanAction
428399
);
429400
}
430401
}

app/code/Magento/Config/etc/di.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,18 @@
9090
<argument name="preProcessor" xsi:type="object">Magento\Framework\App\Config\PreProcessorComposite</argument>
9191
<argument name="serializer" xsi:type="object">Magento\Framework\Serialize\Serializer\Serialize</argument>
9292
<argument name="reader" xsi:type="object">Magento\Config\App\Config\Type\System\Reader\Proxy</argument>
93-
<argument name="locker" xsi:type="object">Magento\Framework\Lock\Backend\Cache</argument>
93+
<argument name="lockQuery" xsi:type="object">systemConfigQueryLocker</argument>
9494
</arguments>
9595
</type>
96+
97+
<virtualType name="systemConfigQueryLocker" type="Magento\Framework\Cache\LockGuardedCacheLoader">
98+
<arguments>
99+
<argument name="locker" xsi:type="object">Magento\Framework\Lock\Backend\Cache</argument>
100+
<argument name="lockTimeout" xsi:type="number">42000</argument>
101+
<argument name="delayTimeout" xsi:type="number">100</argument>
102+
</arguments>
103+
</virtualType>
104+
96105
<type name="Magento\Config\App\Config\Type\System\Reader">
97106
<arguments>
98107
<argument name="source" xsi:type="object">systemConfigSourceAggregated</argument>

app/code/Magento/Elasticsearch6/etc/di.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,36 @@
170170
</argument>
171171
</arguments>
172172
</type>
173+
174+
<virtualType name="elasticsearchLayerCategoryItemCollectionProvider" type="Magento\Elasticsearch\Model\Layer\Category\ItemCollectionProvider">
175+
<arguments>
176+
<argument name="factories" xsi:type="array">
177+
<item name="elasticsearch6" xsi:type="object">elasticsearchCategoryCollectionFactory</item>
178+
</argument>
179+
</arguments>
180+
</virtualType>
181+
182+
<type name="Magento\CatalogSearch\Model\Search\ItemCollectionProvider">
183+
<arguments>
184+
<argument name="factories" xsi:type="array">
185+
<item name="elasticsearch6" xsi:type="object">elasticsearchAdvancedCollectionFactory</item>
186+
</argument>
187+
</arguments>
188+
</type>
189+
190+
<type name="Magento\CatalogSearch\Model\Advanced\ProductCollectionPrepareStrategyProvider">
191+
<arguments>
192+
<argument name="strategies" xsi:type="array">
193+
<item name="elasticsearch6" xsi:type="object">Magento\Elasticsearch\Model\Advanced\ProductCollectionPrepareStrategy</item>
194+
</argument>
195+
</arguments>
196+
</type>
197+
198+
<virtualType name="elasticsearchLayerSearchItemCollectionProvider" type="Magento\Elasticsearch\Model\Layer\Search\ItemCollectionProvider">
199+
<arguments>
200+
<argument name="factories" xsi:type="array">
201+
<item name="elasticsearch6" xsi:type="object">elasticsearchFulltextSearchCollectionFactory</item>
202+
</argument>
203+
</arguments>
204+
</virtualType>
173205
</config>

app/etc/di.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,4 +1757,11 @@
17571757
</argument>
17581758
</arguments>
17591759
</type>
1760+
<type name="Magento\Framework\Cache\LockGuardedCacheLoader">
1761+
<arguments>
1762+
<argument name="locker" xsi:type="object">Magento\Framework\Lock\Backend\Cache</argument>
1763+
<argument name="lockTimeout" xsi:type="number">10000</argument>
1764+
<argument name="delayTimeout" xsi:type="number">20</argument>
1765+
</arguments>
1766+
</type>
17601767
</config>

dev/tests/static/testsuite/Magento/Test/Php/_files/phpcpd/blacklist/common.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,4 @@ Magento/CatalogSearch/Model/ResourceModel/Fulltext
213213
Magento/Elasticsearch/Model/Layer/Search
214214
Magento/Elasticsearch/Model/Adapter/FieldMapper/Product/FieldProvider/FieldName/Resolver
215215
Magento/Elasticsearch6/Model/Client
216+
Magento/Config/App/Config/Type

0 commit comments

Comments
 (0)