Skip to content

Commit

Permalink
Merge pull request #52 from magento-performance/MCP-89
Browse files Browse the repository at this point in the history
MCP-89: Move product batch size variable to environment
  • Loading branch information
vzabaznov committed Jan 20, 2021
2 parents 20dfba7 + 3d7cdbc commit 650ea9b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\InventoryConfigurableProductIndexer\Indexer\SourceItem;

use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Indexer\SaveHandler\Batch;
Expand Down Expand Up @@ -75,6 +76,18 @@ class SourceItemIndexer
*/
private $batch;

/**
* @var DeploymentConfig|null
*/
private $deploymentConfig;

/**
* Deployment config path
*
* @var string
*/
private const DEPLOYMENT_CONFIG_INDEXER_BATCHES = 'indexer/batch_size/';

/**
* @param ResourceConnection $resourceConnection
* @param IndexNameBuilder $indexNameBuilder
Expand All @@ -85,6 +98,7 @@ class SourceItemIndexer
* @param DefaultStockProviderInterface $defaultStockProvider
* @param Batch|null $batch
* @param int|null $batchSize
* @param DeploymentConfig|null $deploymentConfig
*/
public function __construct(
ResourceConnection $resourceConnection,
Expand All @@ -95,7 +109,8 @@ public function __construct(
SiblingSkuListInStockProvider $siblingSkuListInStockProvider,
DefaultStockProviderInterface $defaultStockProvider,
?Batch $batch = null,
?int $batchSize = null
?int $batchSize = null,
?DeploymentConfig $deploymentConfig = null
) {
$this->resourceConnection = $resourceConnection;
$this->indexNameBuilder = $indexNameBuilder;
Expand All @@ -106,6 +121,7 @@ public function __construct(
$this->defaultStockProvider = $defaultStockProvider;
$this->batch = $batch ?: ObjectManager::getInstance()->get(Batch::class);
$this->batchSize = $batchSize ?? self::BATCH_SIZE;
$this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
}

/**
Expand All @@ -116,6 +132,13 @@ public function __construct(
public function executeList(array $sourceItemIds)
{
$skuListInStockList = $this->siblingSkuListInStockProvider->execute($sourceItemIds);
$this->batchSize = $this->deploymentConfig->get(
self::DEPLOYMENT_CONFIG_INDEXER_BATCHES . InventoryIndexer::INDEXER_ID . '/' . 'configurable'
) ??
$this->deploymentConfig->get(
self::DEPLOYMENT_CONFIG_INDEXER_BATCHES . InventoryIndexer::INDEXER_ID . '/' . 'default'
)
?? $this->batchSize;

foreach ($skuListInStockList as $skuListInStock) {
$stockId = $skuListInStock->getStockId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\InventoryConfigurableProductIndexer\Indexer\Stock;

use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\StateException;
Expand Down Expand Up @@ -84,6 +85,18 @@ class StockIndexer
*/
private $batch;

/**
* @var DeploymentConfig|null
*/
private $deploymentConfig;

/**
* Deployment config path
*
* @var string
*/
private const DEPLOYMENT_CONFIG_INDEXER_BATCHES = 'indexer/batch_size/';

/**
* $indexStructure is reserved name for construct variable in index internal mechanism
*
Expand All @@ -97,6 +110,7 @@ class StockIndexer
* @param PrepareIndexDataForClearingIndex|null $prepareIndexDataForClearingIndex
* @param Batch|null $batch
* @param int|null $batchSize
* @param DeploymentConfig|null $deploymentConfig
* @SuppressWarnings(PHPMD.ExcessiveParameterList) All parameters are needed for backward compatibility
*/
public function __construct(
Expand All @@ -109,7 +123,8 @@ public function __construct(
DefaultStockProviderInterface $defaultStockProvider,
?PrepareIndexDataForClearingIndex $prepareIndexDataForClearingIndex = null,
?Batch $batch = null,
?int $batchSize = null
?int $batchSize = null,
?DeploymentConfig $deploymentConfig = null
) {
$this->getAllStockIds = $getAllStockIds;
$this->indexStructure = $indexStructure;
Expand All @@ -122,6 +137,7 @@ public function __construct(
->get(PrepareIndexDataForClearingIndex::class);
$this->batch = $batch ?: ObjectManager::getInstance()->get(Batch::class);
$this->batchSize = $batchSize ?? self::BATCH_SIZE;
$this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
}

/**
Expand Down Expand Up @@ -157,6 +173,14 @@ public function executeRow(int $stockId): void
*/
public function executeList(array $stockIds): void
{
$this->batchSize = $this->deploymentConfig->get(
self::DEPLOYMENT_CONFIG_INDEXER_BATCHES . InventoryIndexer::INDEXER_ID . '/' . 'configurable'
) ??
$this->deploymentConfig->get(
self::DEPLOYMENT_CONFIG_INDEXER_BATCHES . InventoryIndexer::INDEXER_ID . '/' . 'default'
)
?? $this->batchSize;

foreach ($stockIds as $stockId) {
if ($this->defaultStockProvider->getId() === $stockId) {
continue;
Expand Down
26 changes: 24 additions & 2 deletions InventoryIndexer/Indexer/IndexHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Magento\InventoryIndexer\Indexer;

use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Indexer\SaveHandler\Batch;
use Magento\InventoryMultiDimensionalIndexerApi\Model\IndexHandlerInterface;
Expand Down Expand Up @@ -38,22 +40,37 @@ class IndexHandler implements IndexHandlerInterface
*/
private $batchSize;

/**
* @var DeploymentConfig|null
*/
private $deploymentConfig;

/**
* Deployment config path
*
* @var string
*/
private const DEPLOYMENT_CONFIG_INDEXER_BATCHES = 'indexer/batch_size/';

/**
* @param IndexNameResolverInterface $indexNameResolver
* @param Batch $batch
* @param ResourceConnection $resourceConnection
* @param int $batchSize
* @param $batchSize
* @param DeploymentConfig|null $deploymentConfig
*/
public function __construct(
IndexNameResolverInterface $indexNameResolver,
Batch $batch,
ResourceConnection $resourceConnection,
$batchSize
$batchSize,
?DeploymentConfig $deploymentConfig = null
) {
$this->indexNameResolver = $indexNameResolver;
$this->batch = $batch;
$this->resourceConnection = $resourceConnection;
$this->batchSize = $batchSize;
$this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
}

/**
Expand All @@ -65,6 +82,11 @@ public function saveIndex(IndexName $indexName, \Traversable $documents, string
$tableName = $this->indexNameResolver->resolveName($indexName);

$columns = [IndexStructure::SKU, IndexStructure::QUANTITY, IndexStructure::IS_SALABLE];

$this->batchSize = $this->deploymentConfig->get(
self::DEPLOYMENT_CONFIG_INDEXER_BATCHES . InventoryIndexer::INDEXER_ID . '/' . 'default'
) ?? $this->batchSize;

foreach ($this->batch->getItems($documents, $this->batchSize) as $batchDocuments) {
$connection->insertOnDuplicate($tableName, $batchDocuments, $columns);
}
Expand Down

0 comments on commit 650ea9b

Please sign in to comment.