Skip to content

Commit

Permalink
Merge pull request #1006 from magento-folks/eav_switching
Browse files Browse the repository at this point in the history
Story
- MAGETWO-64475 [Indexer optimizations] Indexation process in non-locking way for EAV indexer
- MAGETWO-64908 Indexation process in non-locking way for EAV indexer
  • Loading branch information
VladimirZaets authored Apr 10, 2017
2 parents 896e10f + af95804 commit 3cb0463
Show file tree
Hide file tree
Showing 19 changed files with 377 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,36 @@
*/
namespace Magento\Catalog\Model\ResourceModel\Layer\Filter;

use Magento\Framework\App\ObjectManager;

/**
* Catalog Layer Attribute Filter Resource Model
*
* @author Magento Core Team <[email protected]>
*/
class Attribute extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
/**
* @var \Magento\Indexer\Model\ResourceModel\FrontendResource
*/
private $frontendResource;

/**
* Attribute constructor.
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
* @param null $connectionName
* @param \Magento\Indexer\Model\ResourceModel\FrontendResource|null $frontendResource
*/
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context,
$connectionName = null,
\Magento\Indexer\Model\ResourceModel\FrontendResource $frontendResource = null
) {
$this->frontendResource = $frontendResource ?: ObjectManager::getInstance()
->get(\Magento\Catalog\Model\ResourceModel\Product\Indexer\Eav\FrontendResource::class);
parent::__construct($context, $connectionName);
}

/**
* Initialize connection and define main table name
*
Expand Down Expand Up @@ -88,4 +111,12 @@ public function getCount(\Magento\Catalog\Model\Layer\Filter\FilterInterface $fi

return $connection->fetchPairs($select);
}

/**
* @inheritdoc
*/
public function getMainTable()
{
return $this->frontendResource->getMainTable();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,36 @@
*/
namespace Magento\Catalog\Model\ResourceModel\Layer\Filter;

use Magento\Framework\App\ObjectManager;

/**
* Catalog Layer Decimal attribute Filter Resource Model
*
* @author Magento Core Team <[email protected]>
*/
class Decimal extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
/**
* @var \Magento\Indexer\Model\ResourceModel\FrontendResource
*/
private $frontendResource;

/**
* Attribute constructor.
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
* @param null $connectionName
* @param \Magento\Indexer\Model\ResourceModel\FrontendResource|null $frontendResource
*/
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context,
$connectionName = null,
\Magento\Indexer\Model\ResourceModel\FrontendResource $frontendResource = null
) {
$this->frontendResource = $frontendResource ?: ObjectManager::getInstance()
->get(\Magento\Catalog\Model\ResourceModel\Product\Indexer\EavDecimal\FrontendResource::class);
parent::__construct($context, $connectionName);
}

/**
* Initialize connection and define main table name
*
Expand Down Expand Up @@ -141,4 +164,12 @@ public function getCount(\Magento\Catalog\Model\Layer\Filter\FilterInterface $fi

return $connection->fetchPairs($select);
}

/**
* @inheritdoc
*/
public function getMainTable()
{
return $this->frontendResource->getMainTable();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
namespace Magento\Catalog\Model\ResourceModel\Product\Indexer\Eav;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\App\ObjectManager;

/**
* Catalog Product Eav Attributes abstract indexer resource model
*
* @author Magento Core Team <[email protected]>
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
abstract class AbstractEav extends \Magento\Catalog\Model\ResourceModel\Product\Indexer\AbstractIndexer
{
Expand All @@ -22,22 +24,39 @@ abstract class AbstractEav extends \Magento\Catalog\Model\ResourceModel\Product\
protected $_eventManager = null;

/**
* Construct
*
* @var \Magento\Indexer\Model\Indexer\StateFactory
*/
private $indexerStateFactory;

/**
* @var mixed
*/
private $frontendResource;

/**
* AbstractEav constructor.
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
* @param \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy
* @param \Magento\Eav\Model\Config $eavConfig
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param string $connectionName
* @param null $connectionName
* @param \Magento\Indexer\Model\Indexer\StateFactory|null $stateFactory
* @param \Magento\Indexer\Model\ResourceModel\FrontendResource|null $frontendResource
*/
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context,
\Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy,
\Magento\Eav\Model\Config $eavConfig,
\Magento\Framework\Event\ManagerInterface $eventManager,
$connectionName = null
$connectionName = null,
\Magento\Indexer\Model\Indexer\StateFactory $stateFactory = null,
\Magento\Indexer\Model\ResourceModel\FrontendResource $frontendResource = null
) {
$this->_eventManager = $eventManager;
$this->indexerStateFactory = $stateFactory ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Indexer\Model\Indexer\StateFactory::class);
$this->frontendResource = $frontendResource ?: ObjectManager::getInstance()
->get(\Magento\Catalog\Model\ResourceModel\Product\Indexer\EavDecimal\FrontendResource::class);
parent::__construct($context, $tableStrategy, $eavConfig, $connectionName);
}

Expand Down Expand Up @@ -100,10 +119,11 @@ public function reindexEntities($processIds)
try {
// remove old index
$where = $connection->quoteInto('entity_id IN(?)', $processIds);
$connection->delete($this->getMainTable(), $where);
$mainTable = $this->frontendResource->getMainTable();
$connection->delete($this->frontendResource->getMainTable(), $where);

// insert new index
$this->insertFromTable($this->getIdxTable(), $this->getMainTable());
$this->insertFromTable($this->getIdxTable(), $mainTable);
$connection->commit();
} catch (\Exception $e) {
$connection->rollBack();
Expand Down Expand Up @@ -325,4 +345,22 @@ protected function _synchronizeAttributeIndexData($attributeId)
}
return $this;
}

/**
* @inheritdoc
* Returns main table name in depends of the suffix stored in the 'indexer_state' table
*
* @return string
*/
public function getMainTable()
{
$table = parent::getMainTable();
$indexerState = $this->indexerStateFactory->create()->loadByIndexer(
\Magento\Catalog\Model\Indexer\Product\Eav\Processor::INDEXER_ID
);
$destinationTableSuffix = ($indexerState->getTableSuffix() === '')
? \Magento\Framework\Indexer\StateInterface::ADDITIONAL_TABLE_SUFFIX
: '';
return $table . $destinationTableSuffix;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* Catalog Product Eav Select and Multiply Select Attributes Indexer resource model
*
* @author Magento Core Team <[email protected]>
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Source extends AbstractEav
{
Expand All @@ -30,18 +30,30 @@ class Source extends AbstractEav
* @param \Magento\Eav\Model\Config $eavConfig
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper
* @param string $connectionName
* @param null|string $connectionName
* @param \Magento\Indexer\Model\Indexer\StateFactory|null $stateFactory
* @param \Magento\Indexer\Model\ResourceModel\FrontendResource|null $frontendResource
*/
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context,
\Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy,
\Magento\Eav\Model\Config $eavConfig,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Catalog\Model\ResourceModel\Helper $resourceHelper,
$connectionName = null
$connectionName = null,
\Magento\Indexer\Model\Indexer\StateFactory $stateFactory = null,
\Magento\Indexer\Model\ResourceModel\FrontendResource $frontendResource = null
) {
parent::__construct(
$context,
$tableStrategy,
$eavConfig,
$eventManager,
$connectionName,
$stateFactory,
$frontendResource
);
$this->_resourceHelper = $resourceHelper;
parent::__construct($context, $tableStrategy, $eavConfig, $eventManager, $connectionName);
}

/**
Expand Down
27 changes: 27 additions & 0 deletions app/code/Magento/Catalog/Setup/UpgradeSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con

if (version_compare($context->getVersion(), '2.2.0', '<')) {
$this->addProductPriceIndexReplicaTable($setup);
$this->addProductEavIndexReplicaTables($setup);
}
$setup->endSetup();
}
Expand Down Expand Up @@ -471,4 +472,30 @@ private function addProductPriceIndexReplicaTable(SchemaSetupInterface $setup)
)
);
}

/**
* Add Replica for Catalog Product Eav Index Tables.
*
* By adding 'catalog_product_index_eav_replica', 'catalog_product_index_eav_decimal_replica' we provide separation
* of tables used for indexation write and read operations and affected models.
*
* @param SchemaSetupInterface $setup
* @return void
*/
private function addProductEavIndexReplicaTables(SchemaSetupInterface $setup)
{
$setup->getConnection()->createTable(
$setup->getConnection()->createTableByDdl(
$setup->getTable('catalog_product_index_eav'),
$setup->getTable('catalog_product_index_eav_replica')
)
);

$setup->getConnection()->createTable(
$setup->getConnection()->createTableByDdl(
$setup->getTable('catalog_product_index_eav_decimal'),
$setup->getTable('catalog_product_index_eav_decimal_replica')
)
);
}
}
36 changes: 36 additions & 0 deletions app/code/Magento/Catalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,42 @@
<arguments>
<argument name="tableStrategy" xsi:type="object">Magento\Catalog\Model\ResourceModel\Product\Indexer\TemporaryTableStrategy</argument>
<argument name="connectionName" xsi:type="string">indexer</argument>
<argument name="frontendResource" xsi:type="object">Magento\Catalog\Model\ResourceModel\Product\Indexer\Eav\FrontendResource</argument>
</arguments>
</type>
<virtualType name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Eav\FrontendResource" type="Magento\Indexer\Model\ResourceModel\FrontendResource">
<arguments>
<argument name="indexerId" xsi:type="const">Magento\Catalog\Model\Indexer\Product\Eav\Processor::INDEXER_ID</argument>
<argument name="indexerBaseTable" xsi:type="string">catalog_product_index_eav</argument>
<argument name="idFieldName" xsi:type="string">entity_id</argument>
</arguments>
</virtualType>
<virtualType name="Magento\Catalog\Model\ResourceModel\Product\Indexer\EavDecimal\FrontendResource" type="Magento\Indexer\Model\ResourceModel\FrontendResource">
<arguments>
<argument name="indexerId" xsi:type="const">Magento\Catalog\Model\Indexer\Product\Eav\Processor::INDEXER_ID</argument>
<argument name="indexerBaseTable" xsi:type="string">catalog_product_index_eav_decimal</argument>
<argument name="idFieldName" xsi:type="string">entity_id</argument>
</arguments>
</virtualType>
<type name="Magento\Catalog\Model\Indexer\Product\Eav\Action\Full">
<arguments>
<argument name="batchSizeCalculator" xsi:type="object">Magento\Catalog\Model\ResourceModel\Product\Indexer\Eav\BatchSizeCalculator</argument>
</arguments>
</type>
<type name="Magento\Catalog\Model\ResourceModel\Layer\Filter\Attribute">
<arguments>
<argument name="frontendResource" xsi:type="object">Magento\Catalog\Model\ResourceModel\Product\Indexer\Eav\FrontendResource</argument>
</arguments>
</type>
<type name="Magento\Catalog\Model\ResourceModel\Layer\Filter\Decimal">
<arguments>
<argument name="frontendResource" xsi:type="object">Magento\Catalog\Model\ResourceModel\Product\Indexer\EavDecimal\FrontendResource</argument>
</arguments>
</type>
<type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Eav\Source">
<arguments>
<argument name="connectionName" xsi:type="string">indexer</argument>
<argument name="frontendResource" xsi:type="object">Magento\Catalog\Model\ResourceModel\Product\Indexer\Eav\FrontendResource</argument>
</arguments>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,26 @@ class DataProvider implements DataProviderInterface
*/
private $indexerFrontendResource;

/**
* @var \Magento\Indexer\Model\Indexer\StateFactory|null
*/
private $stateFactory;

/**
* @param Config $eavConfig
* @param ResourceConnection $resource
* @param ScopeResolverInterface $scopeResolver
* @param Session $customerSession
* @param FrontendResource $indexerFrontendResource
* @param FrontendResource|null $indexerFrontendResource
* @param \Magento\Indexer\Model\Indexer\StateFactory|null $stateFactory
*/
public function __construct(
Config $eavConfig,
ResourceConnection $resource,
ScopeResolverInterface $scopeResolver,
Session $customerSession,
FrontendResource $indexerFrontendResource = null
FrontendResource $indexerFrontendResource = null,
\Magento\Indexer\Model\Indexer\StateFactory $stateFactory = null
) {
$this->eavConfig = $eavConfig;
$this->resource = $resource;
Expand All @@ -76,6 +83,8 @@ public function __construct(
$this->indexerFrontendResource = $indexerFrontendResource ?: ObjectManager::getInstance()->get(
Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\FrontendResource::class
);
$this->stateFactory = $stateFactory ?: ObjectManager::getInstance()
->get(\Magento\Indexer\Model\Indexer\StateFactory::class);
}

/**
Expand Down Expand Up @@ -112,9 +121,13 @@ public function getDataSet(
} else {
$currentScopeId = $this->scopeResolver->getScope($currentScope)
->getId();
$tableSufix = $this->stateFactory->create()->loadByIndexer(
\Magento\Catalog\Model\Indexer\Product\Eav\Processor::INDEXER_ID
)->getTableSuffix();
$table = $this->resource->getTableName(
'catalog_product_index_eav' . ($attribute->getBackendType() === 'decimal' ? '_decimal' : '')
);
$table .= $tableSufix;
$subSelect = $select;
$subSelect->from(['main_table' => $table], ['main_table.value'])
->joinLeft(
Expand Down
Loading

0 comments on commit 3cb0463

Please sign in to comment.