Skip to content

Commit 3c69210

Browse files
author
Stanislav Idolov
committed
MAGETWO-65388: Indexation process in non-locking way for stock indexer
1 parent ef2a3dd commit 3c69210

File tree

5 files changed

+63
-29
lines changed

5 files changed

+63
-29
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Bundle\Model\ResourceModel\Indexer;
77

88
use Magento\Catalog\Api\Data\ProductInterface;
9+
use Magento\CatalogInventory\Model\Indexer\Stock\Action\Full;
910

1011
/**
1112
* Bundle Stock Status Indexer Resource Model
@@ -65,7 +66,9 @@ protected function _prepareBundleOptionStockData($entityIds = null, $usePrimaryT
6566
{
6667
$this->_cleanBundleOptionStockData();
6768
$linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
68-
$table = $this->isFull ? $this->getMainTable() : $this->indexerStockFrontendResource->getMainTable();
69+
$table = $this->getActionType() === Full::ACTION_TYPE
70+
? $this->getMainTable()
71+
: $this->indexerStockFrontendResource->getMainTable();
6972
$idxTable = $usePrimaryTable ? $table : $this->getIdxTable();
7073
$connection = $this->getConnection();
7174
$select = $connection->select()->from(

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

Lines changed: 15 additions & 8 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\BatchSizeManagementInterface as BatchSizeManagement;
17+
use Magento\Framework\Indexer\BatchSizeManagementInterface;
1818
use Magento\Framework\Indexer\BatchProviderInterface;
1919
use Magento\Framework\App\ObjectManager;
2020
use Magento\Framework\Exception\LocalizedException;
@@ -29,13 +29,18 @@
2929
*/
3030
class Full extends AbstractAction
3131
{
32+
/**
33+
* Action type representation
34+
*/
35+
const ACTION_TYPE = 'full';
36+
3237
/**
3338
* @var MetadataPool
3439
*/
3540
private $metadataPool;
3641

3742
/**
38-
* @var BatchSizeManagement
43+
* @var BatchSizeManagementInterface
3944
*/
4045
private $batchSizeManagement;
4146

@@ -57,7 +62,7 @@ class Full extends AbstractAction
5762
* @param EventManager $eventManager
5863
* @param null|\Magento\Indexer\Model\ResourceModel\FrontendResource $indexerStockFrontendResource
5964
* @param MetadataPool|null $metadataPool
60-
* @param BatchSizeManagement|null $batchSizeManagement
65+
* @param BatchSizeManagementInterface|null $batchSizeManagement
6166
* @param BatchProviderInterface|null $batchProvider
6267
* @param array $batchRowsCount
6368
*
@@ -71,7 +76,7 @@ public function __construct(
7176
EventManager $eventManager,
7277
FrontendResource $indexerStockFrontendResource = null,
7378
MetadataPool $metadataPool = null,
74-
BatchSizeManagement $batchSizeManagement = null,
79+
BatchSizeManagementInterface $batchSizeManagement = null,
7580
BatchProviderInterface $batchProvider = null,
7681
array $batchRowsCount = []
7782
) {
@@ -86,8 +91,9 @@ public function __construct(
8691

8792
$this->metadataPool = $metadataPool ?: ObjectManager::getInstance()->get(MetadataPool::class);
8893
$this->batchProvider = $batchProvider ?: ObjectManager::getInstance()->get(BatchProviderInterface::class);
89-
$this->batchSizeManagement = $batchSizeManagement ?:
90-
ObjectManager::getInstance()->get(\Magento\CatalogInventory\Model\Indexer\Stock\BatchSizeManagement::class);
94+
$this->batchSizeManagement = $batchSizeManagement ?: ObjectManager::getInstance()->get(
95+
\Magento\CatalogInventory\Model\Indexer\Stock\BatchSizeManagement::class
96+
);
9197
$this->batchRowsCount = $batchRowsCount;
9298
}
9399

@@ -109,8 +115,9 @@ public function execute($ids = null)
109115

110116
$columns = array_keys($this->_getConnection()->describeTable($this->_getIdxTable()));
111117

112-
/** @var \Magento\Catalog\Model\ResourceModel\Product\Indexer\AbstractIndexer $indexer */
118+
/** @var \Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\DefaultStock $indexer */
113119
foreach ($this->_getTypeIndexers() as $indexer) {
120+
$indexer->setActionType(self::ACTION_TYPE);
114121
$connection = $indexer->getConnection();
115122
$tableName = $indexer->getMainTable();
116123

@@ -135,7 +142,7 @@ public function execute($ids = null)
135142
$select->where('type_id = ?', $indexer->getTypeId());
136143

137144
$entityIds = $this->batchProvider->getBatchIds($connection, $select, $batch);
138-
$indexer->reindexBatch($entityIds);
145+
$indexer->reindexEntity($entityIds);
139146

140147
$select = $connection->select()->from($this->_getIdxTable(), $columns);
141148
$query = $select->insertFromSelect($tableName, $columns);

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

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,14 @@
1111
use Magento\Framework\DB\Adapter\AdapterInterface;
1212
use Magento\CatalogInventory\Api\StockConfigurationInterface;
1313
use Magento\Framework\App\ObjectManager;
14+
use Magento\CatalogInventory\Model\Indexer\Stock\Action\Full;
1415

1516
/**
1617
* CatalogInventory Default Stock Status Indexer Resource Model
1718
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1819
*/
1920
class DefaultStock extends AbstractIndexer implements StockInterface
2021
{
21-
/**
22-
* @var bool
23-
*/
24-
protected $isFull;
25-
2622
/**
2723
* Current Product Type Id
2824
*
@@ -64,6 +60,13 @@ class DefaultStock extends AbstractIndexer implements StockInterface
6460
*/
6561
private $indexerStockFrontendResource;
6662

63+
/**
64+
* Param for switching logic which depends on action type (full reindex or partial)
65+
*
66+
* @var string
67+
*/
68+
private $actionType;
69+
6770
/**
6871
* Class constructor
6972
*
@@ -130,11 +133,38 @@ public function reindexAll()
130133
*/
131134
public function reindexEntity($entityIds)
132135
{
133-
$this->isFull = false;
136+
if ($this->getActionType() === Full::ACTION_TYPE) {
137+
$this->tableStrategy->setUseIdxTable(false);
138+
$this->_prepareIndexTable($entityIds);
139+
return $this;
140+
}
141+
134142
$this->_updateIndex($entityIds);
135143
return $this;
136144
}
137145

146+
/**
147+
* Returns action run type
148+
*
149+
* @return string
150+
*/
151+
public function getActionType()
152+
{
153+
return $this->actionType;
154+
}
155+
156+
/**
157+
* Set action run type
158+
*
159+
* @param string $type
160+
* @return $this
161+
*/
162+
public function setActionType($type)
163+
{
164+
$this->actionType = $type;
165+
return $this;
166+
}
167+
138168
/**
139169
* Set active Product Type Id
140170
*
@@ -254,18 +284,6 @@ protected function _prepareIndexTable($entityIds = null)
254284
return $this;
255285
}
256286

257-
/**
258-
* @param array $entityIds
259-
* @return $this
260-
*/
261-
public function reindexBatch(array $entityIds)
262-
{
263-
$this->isFull = true;
264-
$this->tableStrategy->setUseIdxTable(false);
265-
$this->_prepareIndexTable($entityIds);
266-
return $this;
267-
}
268-
269287
/**
270288
* Update Stock status index by product ids
271289
*

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* @author Magento Core Team <core@magentocommerce.com>
1414
*/
1515
use Magento\Catalog\Model\Product\Attribute\Source\Status as ProductStatus;
16+
use Magento\CatalogInventory\Model\Indexer\Stock\Action\Full;
1617

1718
class Configurable extends \Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\DefaultStock
1819
{
@@ -57,7 +58,9 @@ protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = f
5758
{
5859
$metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
5960
$connection = $this->getConnection();
60-
$table = $this->isFull ? $this->getMainTable() : $this->indexerStockFrontendResource->getMainTable();
61+
$table = $this->getActionType() === Full::ACTION_TYPE
62+
? $this->getMainTable()
63+
: $this->indexerStockFrontendResource->getMainTable();
6164
$idxTable = $usePrimaryTable ? $table : $this->getIdxTable();
6265
$select = parent::_getStockStatusSelect($entityIds, $usePrimaryTable);
6366
$linkField = $metadata->getLinkField();

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Magento\GroupedProduct\Model\ResourceModel\Indexer\Stock;
1313

1414
use Magento\Catalog\Model\Product\Attribute\Source\Status as ProductStatus;
15+
use Magento\CatalogInventory\Model\Indexer\Stock\Action\Full;
1516

1617
class Grouped extends \Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\DefaultStock
1718
{
@@ -55,7 +56,9 @@ public function __construct(
5556
protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = false)
5657
{
5758
$connection = $this->getConnection();
58-
$table = $this->isFull ? $this->getMainTable() : $this->indexerStockFrontendResource->getMainTable();
59+
$table = $this->getActionType() === Full::ACTION_TYPE
60+
? $this->getMainTable()
61+
: $this->indexerStockFrontendResource->getMainTable();
5962
$idxTable = $usePrimaryTable ? $table : $this->getIdxTable();
6063
$metadata = $this->getMetadataPool()->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
6164
$select = parent::_getStockStatusSelect($entityIds, $usePrimaryTable);

0 commit comments

Comments
 (0)