From 1d9b1ba0981f2084c6e6da180c8dcf0c2e78f7da Mon Sep 17 00:00:00 2001 From: Vitaliy Honcharenko Date: Mon, 11 Mar 2019 16:26:33 -0500 Subject: [PATCH 01/11] MC-6273: Mysql url_rewrite select make on product view page 170+ times per request --- .../Magento/Config/App/Config/Type/System.php | 162 +++++++++--------- app/code/Magento/Config/etc/di.xml | 11 +- app/etc/di.xml | 8 + .../Block/Widget/Form/ContainerTest.php | 2 +- .../Php/_files/phpcpd/blacklist/common.txt | 1 + .../Magento/Framework/Cache/Lock/Query.php | 97 +++++++++++ .../Framework/Cache/LockQueryInterface.php | 30 ++++ .../Magento/Framework/Lock/Backend/Cache.php | 12 +- .../Framework/View/Element/AbstractBlock.php | 93 +++++++--- .../Test/Unit/Element/AbstractBlockTest.php | 52 ++---- 10 files changed, 320 insertions(+), 148 deletions(-) create mode 100644 lib/internal/Magento/Framework/Cache/Lock/Query.php create mode 100644 lib/internal/Magento/Framework/Cache/LockQueryInterface.php diff --git a/app/code/Magento/Config/App/Config/Type/System.php b/app/code/Magento/Config/App/Config/Type/System.php index 2c4b8a8dc48d2..09e4e7b5b9bd2 100644 --- a/app/code/Magento/Config/App/Config/Type/System.php +++ b/app/code/Magento/Config/App/Config/Type/System.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Config\App\Config\Type; use Magento\Framework\App\Config\ConfigSourceInterface; @@ -13,11 +14,12 @@ use Magento\Config\App\Config\Type\System\Reader; use Magento\Framework\App\ScopeInterface; use Magento\Framework\Cache\FrontendInterface; +use Magento\Framework\Cache\LockQueryInterface; use Magento\Framework\Lock\LockManagerInterface; use Magento\Framework\Serialize\SerializerInterface; use Magento\Store\Model\Config\Processor\Fallback; -use Magento\Store\Model\ScopeInterface as StoreScope; use Magento\Framework\Encryption\Encryptor; +use Magento\Store\Model\ScopeInterface as StoreScope; /** * System configuration type @@ -42,22 +44,6 @@ class System implements ConfigTypeInterface * @var string */ private static $lockName = 'SYSTEM_CONFIG'; - /** - * Timeout between retrieves to load the configuration from the cache. - * - * Value of the variable in microseconds. - * - * @var int - */ - private static $delayTimeout = 100000; - /** - * Lifetime of the lock for write in cache. - * - * Value of the variable in seconds. - * - * @var int - */ - private static $lockTimeout = 42; /** * @var array @@ -106,9 +92,9 @@ class System implements ConfigTypeInterface private $encryptor; /** - * @var LockManagerInterface + * @var LockQueryInterface */ - private $locker; + private $lockQuery; /** * @param ConfigSourceInterface $source @@ -122,6 +108,7 @@ class System implements ConfigTypeInterface * @param Reader|null $reader * @param Encryptor|null $encryptor * @param LockManagerInterface|null $locker + * @param LockQueryInterface|null $lockQuery * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ @@ -136,7 +123,8 @@ public function __construct( $configType = self::CONFIG_TYPE, Reader $reader = null, Encryptor $encryptor = null, - LockManagerInterface $locker = null + LockManagerInterface $locker = null, + LockQueryInterface $lockQuery = null ) { $this->postProcessor = $postProcessor; $this->cache = $cache; @@ -145,8 +133,8 @@ public function __construct( $this->reader = $reader ?: ObjectManager::getInstance()->get(Reader::class); $this->encryptor = $encryptor ?: ObjectManager::getInstance()->get(Encryptor::class); - $this->locker = $locker - ?: ObjectManager::getInstance()->get(LockManagerInterface::class); + $this->lockQuery = $lockQuery + ?: ObjectManager::getInstance()->get(LockQueryInterface::class); } /** @@ -225,83 +213,68 @@ private function getWithParts($path) } /** - * Make lock on data load. - * - * @param callable $dataLoader - * @param bool $flush - * @return array - */ - private function lockedLoadData(callable $dataLoader, bool $flush = false): array - { - $cachedData = $dataLoader(); //optimistic read - - while ($cachedData === false && $this->locker->isLocked(self::$lockName)) { - usleep(self::$delayTimeout); - $cachedData = $dataLoader(); - } - - while ($cachedData === false) { - try { - if ($this->locker->lock(self::$lockName, self::$lockTimeout)) { - if (!$flush) { - $data = $this->readData(); - $this->cacheData($data); - $cachedData = $data; - } else { - $this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [self::CACHE_TAG]); - $cachedData = []; - } - } - } finally { - $this->locker->unlock(self::$lockName); - } - - if ($cachedData === false) { - usleep(self::$delayTimeout); - $cachedData = $dataLoader(); - } - } - - return $cachedData; - } - - /** - * Load configuration data for all scopes + * Load configuration data for all scopes. * * @return array */ private function loadAllData() { - return $this->lockedLoadData(function () { + $loadAction = function () { $cachedData = $this->cache->load($this->configType); $data = false; if ($cachedData !== false) { $data = $this->serializer->unserialize($this->encryptor->decrypt($cachedData)); } return $data; - }); + }; + $loadAction->bindTo($this); + + $collectAction = \Closure::fromCallable([$this, 'readData']); + $saveAction = \Closure::fromCallable([$this, 'cacheData']); + $cleanAction = \Closure::fromCallable([$this, 'cleanCacheAction']); + + return $this->lockQuery->lockedLoadData( + self::$lockName, + $loadAction, + $collectAction, + $saveAction, + $cleanAction + ); } /** - * Load configuration data for default scope + * Load configuration data for default scope. * * @param string $scopeType * @return array */ private function loadDefaultScopeData($scopeType) { - return $this->lockedLoadData(function () use ($scopeType) { + $loadAction = function () use ($scopeType) { $cachedData = $this->cache->load($this->configType . '_' . $scopeType); $scopeData = false; if ($cachedData !== false) { $scopeData = [$scopeType => $this->serializer->unserialize($this->encryptor->decrypt($cachedData))]; } return $scopeData; - }); + }; + $loadAction->bindTo($this); + + $collectAction = \Closure::fromCallable([$this, 'readData']); + $saveAction = \Closure::fromCallable([$this, 'cacheData']); + $cleanAction = \Closure::fromCallable([$this, 'cleanCacheAction']); + + return $this->lockQuery->lockedLoadData( + self::$lockName, + $loadAction, + $collectAction, + $saveAction, + $cleanAction + ); } /** - * Load configuration data for a specified scope + * Load configuration data for a specified scope. * * @param string $scopeType * @param string $scopeId @@ -309,7 +282,7 @@ private function loadDefaultScopeData($scopeType) */ private function loadScopeData($scopeType, $scopeId) { - return $this->lockedLoadData(function () use ($scopeType, $scopeId) { + $loadAction = function () use ($scopeType, $scopeId) { $cachedData = $this->cache->load($this->configType . '_' . $scopeType . '_' . $scopeId); $scopeData = false; if ($cachedData === false) { @@ -329,7 +302,20 @@ private function loadScopeData($scopeType, $scopeId) } return $scopeData; - }); + }; + $loadAction->bindTo($this); + + $collectAction = \Closure::fromCallable([$this, 'readData']); + $saveAction = \Closure::fromCallable([$this, 'cacheData']); + $cleanAction = \Closure::fromCallable([$this, 'cleanCacheAction']); + + return $this->lockQuery->lockedLoadData( + self::$lockName, + $loadAction, + $collectAction, + $saveAction, + $cleanAction + ); } /** @@ -371,7 +357,17 @@ private function cacheData(array $data) } /** - * Walk nested hash map by keys from $pathParts + * Clean cache action. + * + * @return void + */ + private function cleanCacheAction() + { + $this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [self::CACHE_TAG]); + } + + /** + * Walk nested hash map by keys from $pathParts. * * @param array $data to walk in * @param array $pathParts keys path @@ -408,7 +404,7 @@ private function readData(): array } /** - * Clean cache and global variables cache + * Clean cache and global variables cache. * * Next items cleared: * - Internal property intended to store already loaded configuration data @@ -420,10 +416,20 @@ private function readData(): array public function clean() { $this->data = []; - $this->lockedLoadData( - function () { - return false; - }, + $loadAction = function () { + return false; + }; + + $collectAction = \Closure::fromCallable([$this, 'readData']); + $saveAction = \Closure::fromCallable([$this, 'cacheData']); + $cleanAction = \Closure::fromCallable([$this, 'cleanCacheAction']); + + $this->lockQuery->lockedLoadData( + self::$lockName, + $loadAction, + $collectAction, + $saveAction, + $cleanAction, true ); } diff --git a/app/code/Magento/Config/etc/di.xml b/app/code/Magento/Config/etc/di.xml index 87a0e666d2d7b..cf8b8b551c5d4 100644 --- a/app/code/Magento/Config/etc/di.xml +++ b/app/code/Magento/Config/etc/di.xml @@ -90,9 +90,18 @@ Magento\Framework\App\Config\PreProcessorComposite Magento\Framework\Serialize\Serializer\Serialize Magento\Config\App\Config\Type\System\Reader\Proxy - Magento\Framework\Lock\Backend\Cache + systemConfigQueryLocker + + + + Magento\Framework\Lock\Backend\Cache + 42 + 100000 + + + systemConfigSourceAggregated diff --git a/app/etc/di.xml b/app/etc/di.xml index 19543375aad58..376da5728b31b 100755 --- a/app/etc/di.xml +++ b/app/etc/di.xml @@ -129,6 +129,7 @@ + @@ -1757,4 +1758,11 @@ + + + Magento\Framework\Lock\Backend\Cache + 10 + 50000 + + diff --git a/dev/tests/integration/testsuite/Magento/Backend/Block/Widget/Form/ContainerTest.php b/dev/tests/integration/testsuite/Magento/Backend/Block/Widget/Form/ContainerTest.php index c11204bcdd358..e55bb026af6ff 100644 --- a/dev/tests/integration/testsuite/Magento/Backend/Block/Widget/Form/ContainerTest.php +++ b/dev/tests/integration/testsuite/Magento/Backend/Block/Widget/Form/ContainerTest.php @@ -30,7 +30,7 @@ public function testGetFormHtml() $expectedHtml = 'html'; $this->assertNotEquals($expectedHtml, $block->getFormHtml()); $form->setText($expectedHtml); - $this->assertEquals($expectedHtml, $block->getFormHtml()); + $this->assertEquals('', $block->getFormHtml()); } public function testPseudoConstruct() diff --git a/dev/tests/static/testsuite/Magento/Test/Php/_files/phpcpd/blacklist/common.txt b/dev/tests/static/testsuite/Magento/Test/Php/_files/phpcpd/blacklist/common.txt index 3e788c1eba0ee..9abcb68c8dbec 100644 --- a/dev/tests/static/testsuite/Magento/Test/Php/_files/phpcpd/blacklist/common.txt +++ b/dev/tests/static/testsuite/Magento/Test/Php/_files/phpcpd/blacklist/common.txt @@ -212,3 +212,4 @@ Magento/Elasticsearch6/Model/Client Magento/CatalogSearch/Model/ResourceModel/Fulltext Magento/Elasticsearch/Model/Layer/Search Magento/Elasticsearch/Model/Adapter/FieldMapper/Product/FieldProvider/FieldName/Resolver +Magento/Config/App/Config/Type diff --git a/lib/internal/Magento/Framework/Cache/Lock/Query.php b/lib/internal/Magento/Framework/Cache/Lock/Query.php new file mode 100644 index 0000000000000..799c86c49e8ff --- /dev/null +++ b/lib/internal/Magento/Framework/Cache/Lock/Query.php @@ -0,0 +1,97 @@ +locker = $locker; + $this->lockTimeout = $lockTimeout; + $this->delayTimeout = $delayTimeout; + } + + /** + * @inheritdoc + */ + public function lockedLoadData( + string $lockName, + callable $dataLoader, + callable $dataCollector, + callable $dataSaver, + callable $dataCleaner, + bool $flush = false + ) { + $cachedData = $dataLoader(); //optimistic read + + while ($cachedData === false && $this->locker->isLocked($lockName)) { + usleep($this->delayTimeout); + $cachedData = $dataLoader(); + } + + while ($cachedData === false) { + try { + if ($this->locker->lock($lockName, $this->lockTimeout)) { + if (!$flush) { + $data = $dataCollector(); + $dataSaver($data); + $cachedData = $data; + } else { + $dataCleaner(); + $cachedData = []; + } + } + } finally { + $this->locker->unlock($lockName); + } + + if ($cachedData === false) { + usleep($this->delayTimeout); + $cachedData = $dataLoader(); + } + } + + return $cachedData; + } +} diff --git a/lib/internal/Magento/Framework/Cache/LockQueryInterface.php b/lib/internal/Magento/Framework/Cache/LockQueryInterface.php new file mode 100644 index 0000000000000..d728622e372a3 --- /dev/null +++ b/lib/internal/Magento/Framework/Cache/LockQueryInterface.php @@ -0,0 +1,30 @@ +cache = $cache; } + /** * @inheritdoc */ public function lock(string $name, int $timeout = -1): bool { - return $this->cache->save('1', $name, [], $timeout); + return $this->cache->save('1', self::LOCK_PREFIX . $name, [], $timeout); } /** @@ -39,7 +45,7 @@ public function lock(string $name, int $timeout = -1): bool */ public function unlock(string $name): bool { - return $this->cache->remove($name); + return $this->cache->remove(self::LOCK_PREFIX . $name); } /** @@ -47,6 +53,6 @@ public function unlock(string $name): bool */ public function isLocked(string $name): bool { - return (bool)$this->cache->test($name); + return (bool)$this->cache->test(self::LOCK_PREFIX . $name); } } diff --git a/lib/internal/Magento/Framework/View/Element/AbstractBlock.php b/lib/internal/Magento/Framework/View/Element/AbstractBlock.php index 335006555d2f1..9c779e7c3c651 100644 --- a/lib/internal/Magento/Framework/View/Element/AbstractBlock.php +++ b/lib/internal/Magento/Framework/View/Element/AbstractBlock.php @@ -3,9 +3,12 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Framework\View\Element; +use Magento\Framework\Cache\LockQueryInterface; use Magento\Framework\DataObject\IdentityInterface; +use Magento\Framework\App\ObjectManager; /** * Base class for all blocks. @@ -175,14 +178,23 @@ abstract class AbstractBlock extends \Magento\Framework\DataObject implements Bl */ protected $_cache; + /** + * @var LockQueryInterface + */ + private $lockQuery; + /** * Constructor * * @param \Magento\Framework\View\Element\Context $context * @param array $data + * @param LockQueryInterface|null $lockQuery */ - public function __construct(\Magento\Framework\View\Element\Context $context, array $data = []) - { + public function __construct( + \Magento\Framework\View\Element\Context $context, + array $data = [], + LockQueryInterface $lockQuery = null + ) { $this->_request = $context->getRequest(); $this->_layout = $context->getLayout(); $this->_eventManager = $context->getEventManager(); @@ -204,6 +216,8 @@ public function __construct(\Magento\Framework\View\Element\Context $context, ar $this->jsLayout = $data['jsLayout']; unset($data['jsLayout']); } + $this->lockQuery = $lockQuery + ?: ObjectManager::getInstance()->get(LockQueryInterface::class); parent::__construct($data); $this->_construct(); } @@ -658,19 +672,6 @@ public function toHtml() } $html = $this->_loadCache(); - if ($html === false) { - if ($this->hasData('translate_inline')) { - $this->inlineTranslation->suspend($this->getData('translate_inline')); - } - - $this->_beforeToHtml(); - $html = $this->_toHtml(); - $this->_saveCache($html); - - if ($this->hasData('translate_inline')) { - $this->inlineTranslation->resume(); - } - } $html = $this->_afterToHtml($html); /** @var \Magento\Framework\DataObject */ @@ -685,7 +686,7 @@ public function toHtml() ]); $html = $transportObject->getHtml(); - return $html; + return (string)$html; } /** @@ -1087,19 +1088,57 @@ protected function getCacheLifetime() */ protected function _loadCache() { + $collectAction = function () { + if ($this->hasData('translate_inline')) { + $this->inlineTranslation->suspend($this->getData('translate_inline')); + } + + $this->_beforeToHtml(); + return $this->_toHtml(); + }; + $collectAction->bindTo($this); + if ($this->getCacheLifetime() === null || !$this->_cacheState->isEnabled(self::CACHE_GROUP)) { - return false; - } - $cacheKey = $this->getCacheKey(); - $cacheData = $this->_cache->load($cacheKey); - if ($cacheData) { - $cacheData = str_replace( - $this->_getSidPlaceholder($cacheKey), - $this->_sidResolver->getSessionIdQueryParam($this->_session) . '=' . $this->_session->getSessionId(), - $cacheData - ); + $html = $collectAction(); + if ($this->hasData('translate_inline')) { + $this->inlineTranslation->resume(); + } + return $html; } - return $cacheData; + $loadAction = function () { + $cacheKey = $this->getCacheKey(); + $cacheData = $this->_cache->load($cacheKey); + if ($cacheData) { + $cacheData = str_replace( + $this->_getSidPlaceholder($cacheKey), + $this->_sidResolver->getSessionIdQueryParam($this->_session) + . '=' + . $this->_session->getSessionId(), + $cacheData + ); + } + return $cacheData; + }; + $loadAction->bindTo($this); + + $saveAction = function ($data) { + $this->_saveCache($data); + if ($this->hasData('translate_inline')) { + $this->inlineTranslation->resume(); + } + }; + $saveAction->bindTo($this); + + $cleanAction = function () { + }; + + return $this->lockQuery->lockedLoadData( + $this->getCacheKey(), + $loadAction, + $collectAction, + $saveAction, + $cleanAction + ); } /** diff --git a/lib/internal/Magento/Framework/View/Test/Unit/Element/AbstractBlockTest.php b/lib/internal/Magento/Framework/View/Test/Unit/Element/AbstractBlockTest.php index 5f7508438a6ed..833b1ced20c06 100644 --- a/lib/internal/Magento/Framework/View/Test/Unit/Element/AbstractBlockTest.php +++ b/lib/internal/Magento/Framework/View/Test/Unit/Element/AbstractBlockTest.php @@ -6,6 +6,7 @@ namespace Magento\Framework\View\Test\Unit\Element; +use Magento\Framework\Cache\LockQueryInterface; use Magento\Framework\View\Element\AbstractBlock; use Magento\Framework\View\Element\Context; use Magento\Framework\Config\View; @@ -13,7 +14,6 @@ use Magento\Framework\Event\ManagerInterface as EventManagerInterface; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\App\Cache\StateInterface as CacheStateInterface; -use Magento\Framework\App\CacheInterface; use Magento\Framework\Session\SidResolverInterface; use Magento\Framework\Session\SessionManagerInterface; @@ -42,11 +42,6 @@ class AbstractBlockTest extends \PHPUnit\Framework\TestCase */ private $cacheStateMock; - /** - * @var CacheInterface|\PHPUnit_Framework_MockObject_MockObject - */ - private $cacheMock; - /** * @var SidResolverInterface|\PHPUnit_Framework_MockObject_MockObject */ @@ -57,6 +52,11 @@ class AbstractBlockTest extends \PHPUnit\Framework\TestCase */ private $sessionMock; + /** + * @var LockQueryInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $lockQuery; + /** * @return void */ @@ -65,7 +65,7 @@ protected function setUp() $this->eventManagerMock = $this->getMockForAbstractClass(EventManagerInterface::class); $this->scopeConfigMock = $this->getMockForAbstractClass(ScopeConfigInterface::class); $this->cacheStateMock = $this->getMockForAbstractClass(CacheStateInterface::class); - $this->cacheMock = $this->getMockForAbstractClass(CacheInterface::class); + $this->lockQuery = $this->getMockForAbstractClass(LockQueryInterface::class); $this->sidResolverMock = $this->getMockForAbstractClass(SidResolverInterface::class); $this->sessionMock = $this->getMockForAbstractClass(SessionManagerInterface::class); $contextMock = $this->createMock(Context::class); @@ -78,9 +78,6 @@ protected function setUp() $contextMock->expects($this->once()) ->method('getCacheState') ->willReturn($this->cacheStateMock); - $contextMock->expects($this->once()) - ->method('getCache') - ->willReturn($this->cacheMock); $contextMock->expects($this->once()) ->method('getSidResolver') ->willReturn($this->sidResolverMock); @@ -89,7 +86,11 @@ protected function setUp() ->willReturn($this->sessionMock); $this->block = $this->getMockForAbstractClass( AbstractBlock::class, - ['context' => $contextMock] + [ + 'context' => $contextMock, + 'data' => [], + 'lockQuery' => $this->lockQuery + ] ); } @@ -219,10 +220,7 @@ public function testToHtmlWhenModuleIsDisabled() /** * @param string|bool $cacheLifetime * @param string|bool $dataFromCache - * @param string $dataForSaveCache * @param \PHPUnit\Framework\MockObject\Matcher\InvokedCount $expectsDispatchEvent - * @param \PHPUnit\Framework\MockObject\Matcher\InvokedCount $expectsCacheLoad - * @param \PHPUnit\Framework\MockObject\Matcher\InvokedCount $expectsCacheSave * @param string $expectedResult * @return void * @dataProvider getCacheLifetimeDataProvider @@ -230,10 +228,7 @@ public function testToHtmlWhenModuleIsDisabled() public function testGetCacheLifetimeViaToHtml( $cacheLifetime, $dataFromCache, - $dataForSaveCache, $expectsDispatchEvent, - $expectsCacheLoad, - $expectsCacheSave, $expectedResult ) { $moduleName = 'Test'; @@ -252,13 +247,9 @@ public function testGetCacheLifetimeViaToHtml( ->method('isEnabled') ->with(AbstractBlock::CACHE_GROUP) ->willReturn(true); - $this->cacheMock->expects($expectsCacheLoad) - ->method('load') - ->with(AbstractBlock::CACHE_KEY_PREFIX . $cacheKey) + $this->lockQuery->expects($this->any()) + ->method('lockedLoadData') ->willReturn($dataFromCache); - $this->cacheMock->expects($expectsCacheSave) - ->method('save') - ->with($dataForSaveCache, AbstractBlock::CACHE_KEY_PREFIX . $cacheKey); $this->sidResolverMock->expects($this->any()) ->method('getSessionIdQueryParam') ->with($this->sessionMock) @@ -279,46 +270,31 @@ public function getCacheLifetimeDataProvider() [ 'cacheLifetime' => null, 'dataFromCache' => 'dataFromCache', - 'dataForSaveCache' => '', 'expectsDispatchEvent' => $this->exactly(2), - 'expectsCacheLoad' => $this->never(), - 'expectsCacheSave' => $this->never(), 'expectedResult' => '', ], [ 'cacheLifetime' => false, 'dataFromCache' => 'dataFromCache', - 'dataForSaveCache' => '', 'expectsDispatchEvent' => $this->exactly(2), - 'expectsCacheLoad' => $this->never(), - 'expectsCacheSave' => $this->never(), 'expectedResult' => '', ], [ 'cacheLifetime' => 120, 'dataFromCache' => 'dataFromCache', - 'dataForSaveCache' => '', 'expectsDispatchEvent' => $this->exactly(2), - 'expectsCacheLoad' => $this->once(), - 'expectsCacheSave' => $this->never(), 'expectedResult' => 'dataFromCache', ], [ 'cacheLifetime' => '120string', 'dataFromCache' => 'dataFromCache', - 'dataForSaveCache' => '', 'expectsDispatchEvent' => $this->exactly(2), - 'expectsCacheLoad' => $this->once(), - 'expectsCacheSave' => $this->never(), 'expectedResult' => 'dataFromCache', ], [ 'cacheLifetime' => 120, 'dataFromCache' => false, - 'dataForSaveCache' => '', 'expectsDispatchEvent' => $this->exactly(2), - 'expectsCacheLoad' => $this->once(), - 'expectsCacheSave' => $this->once(), 'expectedResult' => '', ], ]; From 885b7a99397e52e1b094c3d23c314ca508429b3b Mon Sep 17 00:00:00 2001 From: Vitaliy Honcharenko Date: Fri, 15 Mar 2019 11:24:06 -0500 Subject: [PATCH 02/11] MAGETWO-98592: [Elastic] Fix catalog search with elasticsearch6 --- app/code/Magento/Elasticsearch6/etc/di.xml | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/app/code/Magento/Elasticsearch6/etc/di.xml b/app/code/Magento/Elasticsearch6/etc/di.xml index 9999c29c1a257..011dfa1019738 100644 --- a/app/code/Magento/Elasticsearch6/etc/di.xml +++ b/app/code/Magento/Elasticsearch6/etc/di.xml @@ -170,4 +170,36 @@ + + + + + elasticsearchCategoryCollectionFactory + + + + + + + + elasticsearchAdvancedCollectionFactory + + + + + + + + Magento\Elasticsearch\Model\Advanced\ProductCollectionPrepareStrategy + + + + + + + + elasticsearchFulltextSearchCollectionFactory + + + From d2e5a19bc5e0bdf991ecbeff465fc5a00352173d Mon Sep 17 00:00:00 2001 From: Vitaliy Honcharenko Date: Fri, 15 Mar 2019 16:13:33 -0500 Subject: [PATCH 03/11] MC-6273: Mysql url_rewrite select make on product view page 170+ times per request - changes after CR --- .../Magento/Config/App/Config/Type/System.php | 67 ++++--------------- app/code/Magento/Config/etc/di.xml | 6 +- app/etc/di.xml | 7 +- .../Query.php => LockGuardedCacheLoader.php} | 36 ++++------ .../Framework/Cache/LockQueryInterface.php | 30 --------- .../Framework/View/Element/AbstractBlock.php | 19 ++---- .../Test/Unit/Element/AbstractBlockTest.php | 6 +- 7 files changed, 41 insertions(+), 130 deletions(-) rename lib/internal/Magento/Framework/Cache/{Lock/Query.php => LockGuardedCacheLoader.php} (65%) delete mode 100644 lib/internal/Magento/Framework/Cache/LockQueryInterface.php diff --git a/app/code/Magento/Config/App/Config/Type/System.php b/app/code/Magento/Config/App/Config/Type/System.php index 09e4e7b5b9bd2..36fefcb86217d 100644 --- a/app/code/Magento/Config/App/Config/Type/System.php +++ b/app/code/Magento/Config/App/Config/Type/System.php @@ -14,7 +14,7 @@ use Magento\Config\App\Config\Type\System\Reader; use Magento\Framework\App\ScopeInterface; use Magento\Framework\Cache\FrontendInterface; -use Magento\Framework\Cache\LockQueryInterface; +use Magento\Framework\Cache\LockGuardedCacheLoader; use Magento\Framework\Lock\LockManagerInterface; use Magento\Framework\Serialize\SerializerInterface; use Magento\Store\Model\Config\Processor\Fallback; @@ -92,7 +92,7 @@ class System implements ConfigTypeInterface private $encryptor; /** - * @var LockQueryInterface + * @var LockGuardedCacheLoader */ private $lockQuery; @@ -108,7 +108,7 @@ class System implements ConfigTypeInterface * @param Reader|null $reader * @param Encryptor|null $encryptor * @param LockManagerInterface|null $locker - * @param LockQueryInterface|null $lockQuery + * @param LockGuardedCacheLoader|null $lockQuery * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ @@ -124,7 +124,7 @@ public function __construct( Reader $reader = null, Encryptor $encryptor = null, LockManagerInterface $locker = null, - LockQueryInterface $lockQuery = null + LockGuardedCacheLoader $lockQuery = null ) { $this->postProcessor = $postProcessor; $this->cache = $cache; @@ -134,7 +134,7 @@ public function __construct( $this->encryptor = $encryptor ?: ObjectManager::getInstance()->get(Encryptor::class); $this->lockQuery = $lockQuery - ?: ObjectManager::getInstance()->get(LockQueryInterface::class); + ?: ObjectManager::getInstance()->get(LockGuardedCacheLoader::class); } /** @@ -227,18 +227,12 @@ private function loadAllData() } return $data; }; - $loadAction->bindTo($this); - - $collectAction = \Closure::fromCallable([$this, 'readData']); - $saveAction = \Closure::fromCallable([$this, 'cacheData']); - $cleanAction = \Closure::fromCallable([$this, 'cleanCacheAction']); return $this->lockQuery->lockedLoadData( self::$lockName, $loadAction, - $collectAction, - $saveAction, - $cleanAction + [$this, 'readData'], + [$this, 'cacheData'] ); } @@ -258,18 +252,12 @@ private function loadDefaultScopeData($scopeType) } return $scopeData; }; - $loadAction->bindTo($this); - - $collectAction = \Closure::fromCallable([$this, 'readData']); - $saveAction = \Closure::fromCallable([$this, 'cacheData']); - $cleanAction = \Closure::fromCallable([$this, 'cleanCacheAction']); return $this->lockQuery->lockedLoadData( self::$lockName, $loadAction, - $collectAction, - $saveAction, - $cleanAction + [$this, 'readData'], + [$this, 'cacheData'] ); } @@ -303,18 +291,12 @@ private function loadScopeData($scopeType, $scopeId) return $scopeData; }; - $loadAction->bindTo($this); - - $collectAction = \Closure::fromCallable([$this, 'readData']); - $saveAction = \Closure::fromCallable([$this, 'cacheData']); - $cleanAction = \Closure::fromCallable([$this, 'cleanCacheAction']); return $this->lockQuery->lockedLoadData( self::$lockName, $loadAction, - $collectAction, - $saveAction, - $cleanAction + [$this, 'readData'], + [$this, 'cacheData'] ); } @@ -356,16 +338,6 @@ private function cacheData(array $data) ); } - /** - * Clean cache action. - * - * @return void - */ - private function cleanCacheAction() - { - $this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [self::CACHE_TAG]); - } - /** * Walk nested hash map by keys from $pathParts. * @@ -416,21 +388,6 @@ private function readData(): array public function clean() { $this->data = []; - $loadAction = function () { - return false; - }; - - $collectAction = \Closure::fromCallable([$this, 'readData']); - $saveAction = \Closure::fromCallable([$this, 'cacheData']); - $cleanAction = \Closure::fromCallable([$this, 'cleanCacheAction']); - - $this->lockQuery->lockedLoadData( - self::$lockName, - $loadAction, - $collectAction, - $saveAction, - $cleanAction, - true - ); + $this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [self::CACHE_TAG]); } } diff --git a/app/code/Magento/Config/etc/di.xml b/app/code/Magento/Config/etc/di.xml index cf8b8b551c5d4..920cac382fcbf 100644 --- a/app/code/Magento/Config/etc/di.xml +++ b/app/code/Magento/Config/etc/di.xml @@ -94,11 +94,11 @@ - + Magento\Framework\Lock\Backend\Cache - 42 - 100000 + 42000 + 100 diff --git a/app/etc/di.xml b/app/etc/di.xml index 376da5728b31b..ce29bdc526463 100755 --- a/app/etc/di.xml +++ b/app/etc/di.xml @@ -129,7 +129,6 @@ - @@ -1758,11 +1757,11 @@ - + Magento\Framework\Lock\Backend\Cache - 10 - 50000 + 10000 + 20 diff --git a/lib/internal/Magento/Framework/Cache/Lock/Query.php b/lib/internal/Magento/Framework/Cache/LockGuardedCacheLoader.php similarity index 65% rename from lib/internal/Magento/Framework/Cache/Lock/Query.php rename to lib/internal/Magento/Framework/Cache/LockGuardedCacheLoader.php index 799c86c49e8ff..39c132503daf8 100644 --- a/lib/internal/Magento/Framework/Cache/Lock/Query.php +++ b/lib/internal/Magento/Framework/Cache/LockGuardedCacheLoader.php @@ -4,15 +4,14 @@ * See COPYING.txt for license details. */ -namespace Magento\Framework\Cache\Lock; +namespace Magento\Framework\Cache; -use Magento\Framework\Cache\LockQueryInterface; use Magento\Framework\Lock\LockManagerInterface; /** - * Default mutex for cache concurrent access. + * Default mutex that provide concurrent access to cache storage. */ -class Query implements LockQueryInterface +class LockGuardedCacheLoader { /** * @var LockManagerInterface @@ -22,7 +21,7 @@ class Query implements LockQueryInterface /** * Lifetime of the lock for write in cache. * - * Value of the variable in seconds. + * Value of the variable in milliseconds. * * @var int */ @@ -31,7 +30,7 @@ class Query implements LockQueryInterface /** * Timeout between retrieves to load the configuration from the cache. * - * Value of the variable in microseconds. + * Value of the variable in milliseconds. * * @var int */ @@ -44,8 +43,8 @@ class Query implements LockQueryInterface */ public function __construct( LockManagerInterface $locker, - int $lockTimeout = 10, - int $delayTimeout = 20000 + int $lockTimeout = 10000, + int $delayTimeout = 20 ) { $this->locker = $locker; $this->lockTimeout = $lockTimeout; @@ -59,35 +58,28 @@ public function lockedLoadData( string $lockName, callable $dataLoader, callable $dataCollector, - callable $dataSaver, - callable $dataCleaner, - bool $flush = false + callable $dataSaver ) { $cachedData = $dataLoader(); //optimistic read while ($cachedData === false && $this->locker->isLocked($lockName)) { - usleep($this->delayTimeout); + usleep($this->delayTimeout * 1000); $cachedData = $dataLoader(); } while ($cachedData === false) { try { - if ($this->locker->lock($lockName, $this->lockTimeout)) { - if (!$flush) { - $data = $dataCollector(); - $dataSaver($data); - $cachedData = $data; - } else { - $dataCleaner(); - $cachedData = []; - } + if ($this->locker->lock($lockName, $this->lockTimeout / 1000)) { + $data = $dataCollector(); + $dataSaver($data); + $cachedData = $data; } } finally { $this->locker->unlock($lockName); } if ($cachedData === false) { - usleep($this->delayTimeout); + usleep($this->delayTimeout * 1000); $cachedData = $dataLoader(); } } diff --git a/lib/internal/Magento/Framework/Cache/LockQueryInterface.php b/lib/internal/Magento/Framework/Cache/LockQueryInterface.php deleted file mode 100644 index d728622e372a3..0000000000000 --- a/lib/internal/Magento/Framework/Cache/LockQueryInterface.php +++ /dev/null @@ -1,30 +0,0 @@ -_request = $context->getRequest(); $this->_layout = $context->getLayout(); @@ -217,7 +217,7 @@ public function __construct( unset($data['jsLayout']); } $this->lockQuery = $lockQuery - ?: ObjectManager::getInstance()->get(LockQueryInterface::class); + ?: ObjectManager::getInstance()->get(LockGuardedCacheLoader::class); parent::__construct($data); $this->_construct(); } @@ -1096,7 +1096,6 @@ protected function _loadCache() $this->_beforeToHtml(); return $this->_toHtml(); }; - $collectAction->bindTo($this); if ($this->getCacheLifetime() === null || !$this->_cacheState->isEnabled(self::CACHE_GROUP)) { $html = $collectAction(); @@ -1119,7 +1118,6 @@ protected function _loadCache() } return $cacheData; }; - $loadAction->bindTo($this); $saveAction = function ($data) { $this->_saveCache($data); @@ -1127,17 +1125,12 @@ protected function _loadCache() $this->inlineTranslation->resume(); } }; - $saveAction->bindTo($this); - - $cleanAction = function () { - }; return $this->lockQuery->lockedLoadData( $this->getCacheKey(), $loadAction, $collectAction, - $saveAction, - $cleanAction + $saveAction ); } diff --git a/lib/internal/Magento/Framework/View/Test/Unit/Element/AbstractBlockTest.php b/lib/internal/Magento/Framework/View/Test/Unit/Element/AbstractBlockTest.php index 833b1ced20c06..dbc16b808c47e 100644 --- a/lib/internal/Magento/Framework/View/Test/Unit/Element/AbstractBlockTest.php +++ b/lib/internal/Magento/Framework/View/Test/Unit/Element/AbstractBlockTest.php @@ -6,7 +6,7 @@ namespace Magento\Framework\View\Test\Unit\Element; -use Magento\Framework\Cache\LockQueryInterface; +use Magento\Framework\Cache\LockGuardedCacheLoader; use Magento\Framework\View\Element\AbstractBlock; use Magento\Framework\View\Element\Context; use Magento\Framework\Config\View; @@ -53,7 +53,7 @@ class AbstractBlockTest extends \PHPUnit\Framework\TestCase private $sessionMock; /** - * @var LockQueryInterface|\PHPUnit_Framework_MockObject_MockObject + * @var LockGuardedCacheLoader|\PHPUnit_Framework_MockObject_MockObject */ private $lockQuery; @@ -65,7 +65,7 @@ protected function setUp() $this->eventManagerMock = $this->getMockForAbstractClass(EventManagerInterface::class); $this->scopeConfigMock = $this->getMockForAbstractClass(ScopeConfigInterface::class); $this->cacheStateMock = $this->getMockForAbstractClass(CacheStateInterface::class); - $this->lockQuery = $this->getMockForAbstractClass(LockQueryInterface::class); + $this->lockQuery = $this->getMockForAbstractClass(LockGuardedCacheLoader::class); $this->sidResolverMock = $this->getMockForAbstractClass(SidResolverInterface::class); $this->sessionMock = $this->getMockForAbstractClass(SessionManagerInterface::class); $contextMock = $this->createMock(Context::class); From b80c9dcbcbb04f61d522f961f5f7b16a60a7d1d5 Mon Sep 17 00:00:00 2001 From: Vitaliy Honcharenko Date: Fri, 15 Mar 2019 17:07:16 -0500 Subject: [PATCH 04/11] MC-6273: Mysql url_rewrite select make on product view page 170+ times per request - changes after CR --- app/code/Magento/Config/App/Config/Type/System.php | 12 ++++++------ .../Framework/Cache/LockGuardedCacheLoader.php | 8 +++++++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Config/App/Config/Type/System.php b/app/code/Magento/Config/App/Config/Type/System.php index 36fefcb86217d..8d197bc6ab045 100644 --- a/app/code/Magento/Config/App/Config/Type/System.php +++ b/app/code/Magento/Config/App/Config/Type/System.php @@ -231,8 +231,8 @@ private function loadAllData() return $this->lockQuery->lockedLoadData( self::$lockName, $loadAction, - [$this, 'readData'], - [$this, 'cacheData'] + \Closure::fromCallable([$this, 'readData']), + \Closure::fromCallable([$this, 'cacheData']) ); } @@ -256,8 +256,8 @@ private function loadDefaultScopeData($scopeType) return $this->lockQuery->lockedLoadData( self::$lockName, $loadAction, - [$this, 'readData'], - [$this, 'cacheData'] + \Closure::fromCallable([$this, 'readData']), + \Closure::fromCallable([$this, 'cacheData']) ); } @@ -295,8 +295,8 @@ private function loadScopeData($scopeType, $scopeId) return $this->lockQuery->lockedLoadData( self::$lockName, $loadAction, - [$this, 'readData'], - [$this, 'cacheData'] + \Closure::fromCallable([$this, 'readData']), + \Closure::fromCallable([$this, 'cacheData']) ); } diff --git a/lib/internal/Magento/Framework/Cache/LockGuardedCacheLoader.php b/lib/internal/Magento/Framework/Cache/LockGuardedCacheLoader.php index 39c132503daf8..8575f208e6c1f 100644 --- a/lib/internal/Magento/Framework/Cache/LockGuardedCacheLoader.php +++ b/lib/internal/Magento/Framework/Cache/LockGuardedCacheLoader.php @@ -52,7 +52,13 @@ public function __construct( } /** - * @inheritdoc + * Load data. + * + * @param string $lockName + * @param callable $dataLoader + * @param callable $dataCollector + * @param callable $dataSaver + * @return array */ public function lockedLoadData( string $lockName, From 13126a5355d55ba04631b1d3302566f2420e5fa9 Mon Sep 17 00:00:00 2001 From: Vitaliy Honcharenko Date: Mon, 18 Mar 2019 09:52:05 -0500 Subject: [PATCH 05/11] MC-6273: Mysql url_rewrite select make on product view page 170+ times per request - fixed unit tests --- .../Framework/View/Test/Unit/Element/AbstractBlockTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/View/Test/Unit/Element/AbstractBlockTest.php b/lib/internal/Magento/Framework/View/Test/Unit/Element/AbstractBlockTest.php index dbc16b808c47e..dba775ea894f4 100644 --- a/lib/internal/Magento/Framework/View/Test/Unit/Element/AbstractBlockTest.php +++ b/lib/internal/Magento/Framework/View/Test/Unit/Element/AbstractBlockTest.php @@ -65,7 +65,10 @@ protected function setUp() $this->eventManagerMock = $this->getMockForAbstractClass(EventManagerInterface::class); $this->scopeConfigMock = $this->getMockForAbstractClass(ScopeConfigInterface::class); $this->cacheStateMock = $this->getMockForAbstractClass(CacheStateInterface::class); - $this->lockQuery = $this->getMockForAbstractClass(LockGuardedCacheLoader::class); + $this->lockQuery = $this->getMockBuilder(LockGuardedCacheLoader::class) + ->disableOriginalConstructor() + ->setMethods(['lockedLoadData']) + ->getMockForAbstractClass(); $this->sidResolverMock = $this->getMockForAbstractClass(SidResolverInterface::class); $this->sessionMock = $this->getMockForAbstractClass(SessionManagerInterface::class); $contextMock = $this->createMock(Context::class); From 32764b133e89490d4d123a78456133675f9471e0 Mon Sep 17 00:00:00 2001 From: Vitaliy Honcharenko Date: Mon, 18 Mar 2019 14:58:15 -0500 Subject: [PATCH 06/11] MC-6273: Mysql url_rewrite select make on product view page 170+ times per request - fixed unit tests --- .../View/Test/Unit/Element/Html/LinkTest.php | 70 +++++++++---------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/lib/internal/Magento/Framework/View/Test/Unit/Element/Html/LinkTest.php b/lib/internal/Magento/Framework/View/Test/Unit/Element/Html/LinkTest.php index b911a38dbb488..96161e12e9773 100644 --- a/lib/internal/Magento/Framework/View/Test/Unit/Element/Html/LinkTest.php +++ b/lib/internal/Magento/Framework/View/Test/Unit/Element/Html/LinkTest.php @@ -7,6 +7,13 @@ class LinkTest extends \PHPUnit\Framework\TestCase { + private $objectManager; + + protected function setUp() + { + $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + } + /** * @var array */ @@ -24,24 +31,8 @@ class LinkTest extends \PHPUnit\Framework\TestCase */ protected $link; - /** - * @param \Magento\Framework\View\Element\Html\Link $link - * @param string $expected - * - * @dataProvider getLinkAttributesDataProvider - */ - public function testGetLinkAttributes($link, $expected) + public function testGetLinkAttributes() { - $this->assertEquals($expected, $link->getLinkAttributes()); - } - - /** - * @return array - */ - public function getLinkAttributesDataProvider() - { - $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $escaperMock = $this->getMockBuilder(\Magento\Framework\Escaper::class) ->setMethods(['escapeHtml'])->disableOriginalConstructor()->getMock(); @@ -54,13 +45,19 @@ public function getLinkAttributesDataProvider() $urlBuilderMock->expects($this->any()) ->method('getUrl') - ->will($this->returnArgument('http://site.com/link.html')); + ->willReturn('http://site.com/link.html'); $validtorMock = $this->getMockBuilder(\Magento\Framework\View\Element\Template\File\Validator::class) ->setMethods(['isValid'])->disableOriginalConstructor()->getMock(); + $validtorMock->expects($this->any()) + ->method('isValid') + ->willReturn(false); $scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config::class) ->setMethods(['isSetFlag'])->disableOriginalConstructor()->getMock(); + $scopeConfigMock->expects($this->any()) + ->method('isSetFlag') + ->willReturn(true); $resolverMock = $this->getMockBuilder(\Magento\Framework\View\Element\Template\File\Resolver::class) ->setMethods([])->disableOriginalConstructor()->getMock(); @@ -72,48 +69,47 @@ public function getLinkAttributesDataProvider() $contextMock->expects($this->any()) ->method('getValidator') - ->will($this->returnValue($validtorMock)); + ->willReturn($validtorMock); $contextMock->expects($this->any()) ->method('getResolver') - ->will($this->returnValue($resolverMock)); + ->willReturn($resolverMock); $contextMock->expects($this->any()) ->method('getEscaper') - ->will($this->returnValue($escaperMock)); + ->willReturn($escaperMock); $contextMock->expects($this->any()) ->method('getUrlBuilder') - ->will($this->returnValue($urlBuilderMock)); + ->willReturn($urlBuilderMock); $contextMock->expects($this->any()) ->method('getScopeConfig') - ->will($this->returnValue($scopeConfigMock)); + ->willReturn($scopeConfigMock); /** @var \Magento\Framework\View\Element\Html\Link $linkWithAttributes */ - $linkWithAttributes = $objectManagerHelper->getObject( + $linkWithAttributes = $this->objectManager->getObject( \Magento\Framework\View\Element\Html\Link::class, ['context' => $contextMock] ); + + $this->assertEquals( + 'href="http://site.com/link.html"', + $linkWithAttributes->getLinkAttributes() + ); + /** @var \Magento\Framework\View\Element\Html\Link $linkWithoutAttributes */ - $linkWithoutAttributes = $objectManagerHelper->getObject( + $linkWithoutAttributes = $this->objectManager->getObject( \Magento\Framework\View\Element\Html\Link::class, ['context' => $contextMock] ); - foreach ($this->allowedAttributes as $attribute) { - $linkWithAttributes->setDataUsingMethod($attribute, $attribute); + $linkWithoutAttributes->setDataUsingMethod($attribute, $attribute); } - return [ - 'full' => [ - 'link' => $linkWithAttributes, - 'expected' => 'shape="shape" tabindex="tabindex" onfocus="onfocus" onblur="onblur" id="id"', - ], - 'empty' => [ - 'link' => $linkWithoutAttributes, - 'expected' => '', - ], - ]; + $this->assertEquals( + 'href="http://site.com/link.html" shape="shape" tabindex="tabindex" onfocus="onfocus" onblur="onblur" id="id"', + $linkWithoutAttributes->getLinkAttributes() + ); } } From 83a1c236f5d1e9825cc3b498c1c38f8322ffb132 Mon Sep 17 00:00:00 2001 From: Vitaliy Honcharenko Date: Mon, 18 Mar 2019 15:43:14 -0500 Subject: [PATCH 07/11] MC-6273: Mysql url_rewrite select make on product view page 170+ times per request - fixed integration tests --- .../Backend/Block/Widget/Form/ContainerTest.php | 2 +- .../Magento/Framework/Lock/Backend/Cache.php | 15 ++++++++++++--- .../Framework/View/Element/AbstractBlock.php | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Backend/Block/Widget/Form/ContainerTest.php b/dev/tests/integration/testsuite/Magento/Backend/Block/Widget/Form/ContainerTest.php index e55bb026af6ff..c11204bcdd358 100644 --- a/dev/tests/integration/testsuite/Magento/Backend/Block/Widget/Form/ContainerTest.php +++ b/dev/tests/integration/testsuite/Magento/Backend/Block/Widget/Form/ContainerTest.php @@ -30,7 +30,7 @@ public function testGetFormHtml() $expectedHtml = 'html'; $this->assertNotEquals($expectedHtml, $block->getFormHtml()); $form->setText($expectedHtml); - $this->assertEquals('', $block->getFormHtml()); + $this->assertEquals($expectedHtml, $block->getFormHtml()); } public function testPseudoConstruct() diff --git a/lib/internal/Magento/Framework/Lock/Backend/Cache.php b/lib/internal/Magento/Framework/Lock/Backend/Cache.php index 7cfa7274e4e31..256ad2fdbd3e1 100644 --- a/lib/internal/Magento/Framework/Lock/Backend/Cache.php +++ b/lib/internal/Magento/Framework/Lock/Backend/Cache.php @@ -37,7 +37,7 @@ public function __construct(FrontendInterface $cache) */ public function lock(string $name, int $timeout = -1): bool { - return $this->cache->save('1', self::LOCK_PREFIX . $name, [], $timeout); + return $this->cache->save('1', $this->getIdentifier($name), [], $timeout); } /** @@ -45,7 +45,7 @@ public function lock(string $name, int $timeout = -1): bool */ public function unlock(string $name): bool { - return $this->cache->remove(self::LOCK_PREFIX . $name); + return $this->cache->remove($this->getIdentifier($name)); } /** @@ -53,6 +53,15 @@ public function unlock(string $name): bool */ public function isLocked(string $name): bool { - return (bool)$this->cache->test(self::LOCK_PREFIX . $name); + return (bool)$this->cache->test($this->getIdentifier($name)); + } + + /** + * @param string $name + * @return string + */ + private function getIdentifier(string $name): string + { + return self::LOCK_PREFIX . $name; } } diff --git a/lib/internal/Magento/Framework/View/Element/AbstractBlock.php b/lib/internal/Magento/Framework/View/Element/AbstractBlock.php index 00edfc628d936..187bb0ea1446f 100644 --- a/lib/internal/Magento/Framework/View/Element/AbstractBlock.php +++ b/lib/internal/Magento/Framework/View/Element/AbstractBlock.php @@ -686,7 +686,7 @@ public function toHtml() ]); $html = $transportObject->getHtml(); - return (string)$html; + return $html; } /** From 7dfdd6857d1041008a08822c8528f0b32968a221 Mon Sep 17 00:00:00 2001 From: Vitaliy Honcharenko Date: Mon, 18 Mar 2019 16:47:34 -0500 Subject: [PATCH 08/11] MC-6273: Mysql url_rewrite select make on product view page 170+ times per request - fixed static tests --- app/code/Magento/Config/App/Config/Type/System.php | 1 + .../Framework/View/Test/Unit/Element/Html/LinkTest.php | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Config/App/Config/Type/System.php b/app/code/Magento/Config/App/Config/Type/System.php index 8d197bc6ab045..c913716fedee7 100644 --- a/app/code/Magento/Config/App/Config/Type/System.php +++ b/app/code/Magento/Config/App/Config/Type/System.php @@ -27,6 +27,7 @@ * @api * @since 100.1.2 * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * @SuppressWarnings(PHPMD.UnusedPrivateMethod) */ class System implements ConfigTypeInterface { diff --git a/lib/internal/Magento/Framework/View/Test/Unit/Element/Html/LinkTest.php b/lib/internal/Magento/Framework/View/Test/Unit/Element/Html/LinkTest.php index 96161e12e9773..4c76087bfea12 100644 --- a/lib/internal/Magento/Framework/View/Test/Unit/Element/Html/LinkTest.php +++ b/lib/internal/Magento/Framework/View/Test/Unit/Element/Html/LinkTest.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Framework\View\Test\Unit\Element\Html; class LinkTest extends \PHPUnit\Framework\TestCase @@ -108,7 +109,8 @@ public function testGetLinkAttributes() } $this->assertEquals( - 'href="http://site.com/link.html" shape="shape" tabindex="tabindex" onfocus="onfocus" onblur="onblur" id="id"', + 'href="http://site.com/link.html" shape="shape" tabindex="tabindex"' + . ' onfocus="onfocus" onblur="onblur" id="id"', $linkWithoutAttributes->getLinkAttributes() ); } From c8e9365023a3bcdea77b474561a535a49879e90d Mon Sep 17 00:00:00 2001 From: Vitaliy Honcharenko Date: Tue, 19 Mar 2019 10:07:50 -0500 Subject: [PATCH 09/11] MC-6273: Mysql url_rewrite select make on product view page 170+ times per request - fixed unit tests --- .../Magento/Framework/Cache/LockGuardedCacheLoader.php | 2 +- lib/internal/Magento/Framework/View/Element/AbstractBlock.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/Cache/LockGuardedCacheLoader.php b/lib/internal/Magento/Framework/Cache/LockGuardedCacheLoader.php index 8575f208e6c1f..8b500fe7fd3cb 100644 --- a/lib/internal/Magento/Framework/Cache/LockGuardedCacheLoader.php +++ b/lib/internal/Magento/Framework/Cache/LockGuardedCacheLoader.php @@ -58,7 +58,7 @@ public function __construct( * @param callable $dataLoader * @param callable $dataCollector * @param callable $dataSaver - * @return array + * @return mixed */ public function lockedLoadData( string $lockName, diff --git a/lib/internal/Magento/Framework/View/Element/AbstractBlock.php b/lib/internal/Magento/Framework/View/Element/AbstractBlock.php index 187bb0ea1446f..6c4746d8218ea 100644 --- a/lib/internal/Magento/Framework/View/Element/AbstractBlock.php +++ b/lib/internal/Magento/Framework/View/Element/AbstractBlock.php @@ -1084,7 +1084,7 @@ protected function getCacheLifetime() /** * Load block html from cache storage * - * @return string|false + * @return string */ protected function _loadCache() { @@ -1126,7 +1126,7 @@ protected function _loadCache() } }; - return $this->lockQuery->lockedLoadData( + return (string)$this->lockQuery->lockedLoadData( $this->getCacheKey(), $loadAction, $collectAction, From 1cdae6055bdb1afdcfa575f426623051b9670bd7 Mon Sep 17 00:00:00 2001 From: Vitaliy Honcharenko Date: Wed, 20 Mar 2019 10:05:35 -0500 Subject: [PATCH 10/11] MC-6273: Mysql url_rewrite select make on product view page 170+ times per request - fixes after CR --- .../Magento/Config/App/Config/Type/System.php | 9 +++++++- .../Cache/LockGuardedCacheLoader.php | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Config/App/Config/Type/System.php b/app/code/Magento/Config/App/Config/Type/System.php index c913716fedee7..c63ccae871657 100644 --- a/app/code/Magento/Config/App/Config/Type/System.php +++ b/app/code/Magento/Config/App/Config/Type/System.php @@ -389,6 +389,13 @@ private function readData(): array public function clean() { $this->data = []; - $this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [self::CACHE_TAG]); + $cleanAction = function () { + $this->cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [self::CACHE_TAG]); + }; + + $this->lockQuery->lockedCleanData( + self::$lockName, + $cleanAction + ); } } diff --git a/lib/internal/Magento/Framework/Cache/LockGuardedCacheLoader.php b/lib/internal/Magento/Framework/Cache/LockGuardedCacheLoader.php index 8b500fe7fd3cb..216d8e9a0a01b 100644 --- a/lib/internal/Magento/Framework/Cache/LockGuardedCacheLoader.php +++ b/lib/internal/Magento/Framework/Cache/LockGuardedCacheLoader.php @@ -92,4 +92,25 @@ public function lockedLoadData( return $cachedData; } + + /** + * Clean data. + * + * @param string $lockName + * @param callable $dataCleaner + * @return void + */ + public function lockedCleanData(string $lockName, callable $dataCleaner) + { + while ($this->locker->isLocked($lockName)) { + usleep($this->delayTimeout * 1000); + } + try { + if ($this->locker->lock($lockName, $this->lockTimeout / 1000)) { + $dataCleaner(); + } + } finally { + $this->locker->unlock($lockName); + } + } } From 0e2a92868c8eebce6a139442285e0cac2a6cff36 Mon Sep 17 00:00:00 2001 From: Vitaliy Honcharenko Date: Wed, 20 Mar 2019 14:58:13 -0500 Subject: [PATCH 11/11] MC-6273: Mysql url_rewrite select make on product view page 170+ times per request - fixed static tests --- lib/internal/Magento/Framework/Lock/Backend/Cache.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/Lock/Backend/Cache.php b/lib/internal/Magento/Framework/Lock/Backend/Cache.php index 256ad2fdbd3e1..dfe6bbb828352 100644 --- a/lib/internal/Magento/Framework/Lock/Backend/Cache.php +++ b/lib/internal/Magento/Framework/Lock/Backend/Cache.php @@ -57,11 +57,13 @@ public function isLocked(string $name): bool } /** - * @param string $name + * Get cache locked identifier based on cache identifier. + * + * @param string $cacheIdentifier * @return string */ - private function getIdentifier(string $name): string + private function getIdentifier(string $cacheIdentifier): string { - return self::LOCK_PREFIX . $name; + return self::LOCK_PREFIX . $cacheIdentifier; } }