Skip to content

Commit aae3d44

Browse files
MAGETWO-65388: Indexation process in non-locking way for stock indexer
1 parent 0d29aba commit aae3d44

File tree

9 files changed

+62
-21
lines changed

9 files changed

+62
-21
lines changed

app/code/Magento/Bundle/Model/ResourceModel/Indexer/Stock.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ protected function _prepareBundleOptionStockData($entityIds = null, $usePrimaryT
6565
{
6666
$this->_cleanBundleOptionStockData();
6767
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
68-
$idxTable = $usePrimaryTable ? $this->indexerStockFrontendResource->getMainTable() : $this->getIdxTable();
68+
$table = $this->isFull ? $this->getMainTable() : $this->indexerStockFrontendResource->getMainTable();
69+
$idxTable = $usePrimaryTable ? $table : $this->getIdxTable();
6970
$connection = $this->getConnection();
7071
$select = $connection->select()->from(
7172
['product' => $this->getTable('catalog_product_entity')],

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -926,14 +926,14 @@
926926
</argument>
927927
</arguments>
928928
</type>
929-
<virtualType name="Magento\Catalog\Model\Indexer\Price\CompositeProductBatchSizeManagement" type="\Magento\Framework\Indexer\BatchSizeManagement">
929+
<virtualType name="Magento\Catalog\Model\Indexer\Price\CompositeProductBatchSizeManagement" type="Magento\Framework\Indexer\BatchSizeManagement">
930930
<arguments>
931931
<argument name="rowSizeEstimator" xsi:type="object" shared="false">Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\CompositeProductRowSizeEstimator</argument>
932932
</arguments>
933933
</virtualType>
934-
<virtualType name="Magento\Catalog\Model\Indexer\Price\BatchSizeManagement" type="\Magento\Framework\Indexer\BatchSizeManagement">
934+
<virtualType name="Magento\Catalog\Model\Indexer\Price\BatchSizeManagement" type="Magento\Framework\Indexer\BatchSizeManagement">
935935
<arguments>
936-
<argument name="rowSizeEstimator" xsi:type="object" shared="false">\Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\IndexTableRowSizeEstimator</argument>
936+
<argument name="rowSizeEstimator" xsi:type="object" shared="false">Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\IndexTableRowSizeEstimator</argument>
937937
</arguments>
938938
</virtualType>
939939
<virtualType name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\FrontendResource" type="Magento\Indexer\Model\ResourceModel\FrontendResource">

app/code/Magento/CatalogInventory/Model/Indexer/Stock/Action/Full.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Magento\Framework\Indexer\CacheContext;
1515
use Magento\Framework\Event\ManagerInterface as EventManager;
1616
use Magento\Framework\EntityManager\MetadataPool;
17-
use Magento\Framework\Indexer\BatchSizeCalculatorInterface as BatchCalculator;
17+
use Magento\Framework\Indexer\BatchSizeManagementInterface as BatchSizeManagement;
1818
use Magento\Framework\Indexer\BatchProviderInterface;
1919
use Magento\Framework\App\ObjectManager;
2020
use Magento\Framework\Exception\LocalizedException;
@@ -35,9 +35,9 @@ class Full extends AbstractAction
3535
private $metadataPool;
3636

3737
/**
38-
* @var BatchCalculator
38+
* @var BatchSizeManagement
3939
*/
40-
private $batchSizeCalculator;
40+
private $batchSizeManagement;
4141

4242
/**
4343
* @var BatchProviderInterface
@@ -47,7 +47,7 @@ class Full extends AbstractAction
4747
/**
4848
* @var array
4949
*/
50-
private $memoryTablesMinRows;
50+
private $batchRowsCount;
5151

5252
/**
5353
* @param ResourceConnection $resource
@@ -57,9 +57,9 @@ class Full extends AbstractAction
5757
* @param EventManager $eventManager
5858
* @param null|\Magento\Indexer\Model\ResourceModel\FrontendResource $indexerStockFrontendResource
5959
* @param MetadataPool|null $metadataPool
60-
* @param BatchCalculator|null $batchSizeCalculator
60+
* @param BatchSizeManagement|null $batchSizeManagement
6161
* @param BatchProviderInterface|null $batchProvider
62-
* @param array $memoryTablesMinRows
62+
* @param array $batchRowsCount
6363
*
6464
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
6565
*/
@@ -71,9 +71,9 @@ public function __construct(
7171
EventManager $eventManager,
7272
FrontendResource $indexerStockFrontendResource = null,
7373
MetadataPool $metadataPool = null,
74-
BatchCalculator $batchSizeCalculator = null,
74+
BatchSizeManagement $batchSizeManagement = null,
7575
BatchProviderInterface $batchProvider = null,
76-
array $memoryTablesMinRows = []
76+
array $batchRowsCount = []
7777
) {
7878
parent::__construct(
7979
$resource,
@@ -86,8 +86,9 @@ public function __construct(
8686

8787
$this->metadataPool = $metadataPool ?: ObjectManager::getInstance()->get(MetadataPool::class);
8888
$this->batchProvider = $batchProvider ?: ObjectManager::getInstance()->get(BatchProviderInterface::class);
89-
$this->batchSizeCalculator = $batchSizeCalculator ?: ObjectManager::getInstance()->get(BatchCalculator::class);
90-
$this->memoryTablesMinRows = $memoryTablesMinRows;
89+
$this->batchSizeManagement = $batchSizeManagement ?:
90+
ObjectManager::getInstance()->get(\Magento\CatalogInventory\Model\Indexer\Stock\BatchSizeManagement::class);
91+
$this->batchRowsCount = $batchRowsCount;
9192
}
9293

9394
/**
@@ -113,15 +114,16 @@ public function execute($ids = null)
113114
$connection = $indexer->getConnection();
114115
$tableName = $indexer->getMainTable();
115116

116-
$memoryTableMinRows = isset($this->memoryTablesMinRows[$indexer->getTypeId()])
117-
? $this->memoryTablesMinRows[$indexer->getTypeId()]
118-
: $this->memoryTablesMinRows['default'];
117+
$batchRowCount = isset($this->batchRowsCount[$indexer->getTypeId()])
118+
? $this->batchRowsCount[$indexer->getTypeId()]
119+
: $this->batchRowsCount['default'];
119120

121+
$this->batchSizeManagement->ensureBatchSize($connection, $batchRowCount);
120122
$batches = $this->batchProvider->getBatches(
121123
$connection,
122124
$entityMetadata->getEntityTable(),
123125
$entityMetadata->getIdentifierField(),
124-
$this->batchSizeCalculator->estimateBatchSize($connection, $memoryTableMinRows)
126+
$batchRowCount
125127
);
126128

127129
foreach ($batches as $batch) {

app/code/Magento/CatalogInventory/Model/ResourceModel/Indexer/Stock/DefaultStock.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
*/
1919
class DefaultStock extends AbstractIndexer implements StockInterface
2020
{
21+
/**
22+
* @var bool
23+
*/
24+
protected $isFull;
25+
2126
/**
2227
* Current Product Type Id
2328
*
@@ -125,6 +130,7 @@ public function reindexAll()
125130
*/
126131
public function reindexEntity($entityIds)
127132
{
133+
$this->isFull = false;
128134
$this->_updateIndex($entityIds);
129135
return $this;
130136
}
@@ -254,6 +260,7 @@ protected function _prepareIndexTable($entityIds = null)
254260
*/
255261
public function reindexBatch(array $entityIds)
256262
{
263+
$this->isFull = true;
257264
$this->tableStrategy->setUseIdxTable(false);
258265
$this->_prepareIndexTable($entityIds);
259266
return $this;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock;
8+
9+
class IndexTableRowSizeEstimator implements \Magento\Framework\Indexer\IndexTableRowSizeEstimatorInterface
10+
{
11+
/**
12+
* @inheritdoc
13+
*/
14+
public function estimateRowSize()
15+
{
16+
return 100;
17+
}
18+
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,17 @@
8686
</argument>
8787
</arguments>
8888
</type>
89+
<virtualType name="Magento\CatalogInventory\Model\Indexer\Stock\BatchSizeManagement" type="Magento\Framework\Indexer\BatchSizeManagement">
90+
<arguments>
91+
<argument name="rowSizeEstimator" xsi:type="object" shared="false">Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\IndexTableRowSizeEstimator</argument>
92+
</arguments>
93+
</virtualType>
8994
<type name="Magento\CatalogInventory\Model\Indexer\Stock\Action\Full">
9095
<arguments>
91-
<argument name="memoryTablesMinRows" xsi:type="array">
96+
<argument name="batchRowsCount" xsi:type="array">
9297
<item name="default" xsi:type="number">200</item>
9398
</argument>
99+
<argument name="batchSizeManagement" xsi:type="object">Magento\CatalogInventory\Model\Indexer\Stock\BatchSizeManagement</argument>
94100
</arguments>
95101
</type>
96102
<virtualType name="Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\FrontendResource" type="Magento\Indexer\Model\ResourceModel\FrontendResource">

app/code/Magento/ConfigurableProduct/Model/ResourceModel/Indexer/Stock/Configurable.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = f
5757
{
5858
$metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
5959
$connection = $this->getConnection();
60-
$idxTable = $usePrimaryTable ? $this->indexerStockFrontendResource->getMainTable() : $this->getIdxTable();
60+
$table = $this->isFull ? $this->getMainTable() : $this->indexerStockFrontendResource->getMainTable();
61+
$idxTable = $usePrimaryTable ? $table : $this->getIdxTable();
6162
$select = parent::_getStockStatusSelect($entityIds, $usePrimaryTable);
6263
$linkField = $metadata->getLinkField();
6364
$select->reset(

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,9 @@
181181
</argument>
182182
</arguments>
183183
</type>
184+
<type name="Magento\ConfigurableProduct\Model\ResourceModel\Indexer\Stock\Configurable">
185+
<arguments>
186+
<argument name="indexerStockFrontendResource" xsi:type="object">Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\FrontendResource</argument>
187+
</arguments>
188+
</type>
184189
</config>

app/code/Magento/GroupedProduct/Model/ResourceModel/Indexer/Stock/Grouped.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public function __construct(
5555
protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = false)
5656
{
5757
$connection = $this->getConnection();
58-
$idxTable = $usePrimaryTable ? $this->indexerStockFrontendResource->getMainTable() : $this->getIdxTable();
58+
$table = $this->isFull ? $this->getMainTable() : $this->indexerStockFrontendResource->getMainTable();
59+
$idxTable = $usePrimaryTable ? $table : $this->getIdxTable();
5960
$metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
6061
$select = parent::_getStockStatusSelect($entityIds, $usePrimaryTable);
6162
$select->reset(

0 commit comments

Comments
 (0)