Skip to content

Commit 3b860f5

Browse files
authored
Merge pull request #6510 from magento-performance/MCP-89
MCP-89 Move product batch size variable to environment
2 parents 49d4a2f + 7ac16e7 commit 3b860f5

File tree

5 files changed

+119
-14
lines changed

5 files changed

+119
-14
lines changed

app/code/Magento/Catalog/Model/Indexer/Category/Product/Action/Full.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Magento\Catalog\Model\Config;
1313
use Magento\Catalog\Model\Indexer\Category\Product\AbstractAction;
1414
use Magento\Catalog\Model\ResourceModel\Indexer\ActiveTableSwitcher;
15+
use Magento\Catalog\Model\Indexer\Category\Product;
16+
use Magento\Framework\App\DeploymentConfig;
1517
use Magento\Framework\App\ObjectManager;
1618
use Magento\Framework\DB\Adapter\AdapterInterface;
1719
use Magento\Framework\DB\Query\Generator as QueryGenerator;
@@ -63,6 +65,18 @@ class Full extends AbstractAction
6365
*/
6466
private $processManager;
6567

68+
/**
69+
* @var DeploymentConfig|null
70+
*/
71+
private $deploymentConfig;
72+
73+
/**
74+
* Deployment config path
75+
*
76+
* @var string
77+
*/
78+
private const DEPLOYMENT_CONFIG_INDEXER_BATCHES = 'indexer/batch_size/';
79+
6680
/**
6781
* @param ResourceConnection $resource
6882
* @param StoreManagerInterface $storeManager
@@ -73,7 +87,8 @@ class Full extends AbstractAction
7387
* @param MetadataPool|null $metadataPool
7488
* @param int|null $batchRowsCount
7589
* @param ActiveTableSwitcher|null $activeTableSwitcher
76-
* @param ProcessManager $processManager
90+
* @param ProcessManager|null $processManager
91+
* @param DeploymentConfig|null $deploymentConfig
7792
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
7893
*/
7994
public function __construct(
@@ -86,7 +101,8 @@ public function __construct(
86101
MetadataPool $metadataPool = null,
87102
$batchRowsCount = null,
88103
ActiveTableSwitcher $activeTableSwitcher = null,
89-
ProcessManager $processManager = null
104+
ProcessManager $processManager = null,
105+
?DeploymentConfig $deploymentConfig = null
90106
) {
91107
parent::__construct(
92108
$resource,
@@ -107,6 +123,7 @@ public function __construct(
107123
$this->batchRowsCount = $batchRowsCount;
108124
$this->activeTableSwitcher = $activeTableSwitcher ?: $objectManager->get(ActiveTableSwitcher::class);
109125
$this->processManager = $processManager ?: $objectManager->get(ProcessManager::class);
126+
$this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
110127
}
111128

112129
/**
@@ -266,6 +283,11 @@ private function reindexCategoriesBySelect(Select $basicSelect, $whereCondition,
266283
$columns = array_keys(
267284
$this->connection->describeTable($this->tableMaintainer->getMainTmpTable((int)$store->getId()))
268285
);
286+
287+
$this->batchRowsCount = $this->deploymentConfig->get(
288+
self::DEPLOYMENT_CONFIG_INDEXER_BATCHES . Product::INDEXER_ID
289+
) ?? $this->batchRowsCount;
290+
269291
$this->batchSizeManagement->ensureBatchSize($this->connection, $this->batchRowsCount);
270292

271293
$select = $this->connection->select();

app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/Price/BatchSizeCalculator.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
namespace Magento\Catalog\Model\ResourceModel\Product\Indexer\Price;
88

9+
use Magento\Framework\App\DeploymentConfig;
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Catalog\Model\Indexer\Product\Price\Processor;
12+
913
/**
1014
* Ensure that size of index MEMORY table is enough for configured rows count in batch.
1115
*/
@@ -26,17 +30,34 @@ class BatchSizeCalculator
2630
*/
2731
private $batchSizeAdjusters;
2832

33+
/**
34+
* @var DeploymentConfig|null
35+
*/
36+
private $deploymentConfig;
37+
38+
/**
39+
* Deployment config path
40+
*
41+
* @var string
42+
*/
43+
private const DEPLOYMENT_CONFIG_INDEXER_BATCHES = 'indexer/batch_size/';
44+
2945
/**
3046
* BatchSizeCalculator constructor.
3147
* @param array $batchRowsCount
3248
* @param array $estimators
3349
* @param array $batchSizeAdjusters
3450
*/
35-
public function __construct(array $batchRowsCount, array $estimators, array $batchSizeAdjusters)
36-
{
51+
public function __construct(
52+
array $batchRowsCount,
53+
array $estimators,
54+
array $batchSizeAdjusters,
55+
?DeploymentConfig $deploymentConfig = null
56+
) {
3757
$this->batchRowsCount = $batchRowsCount;
3858
$this->estimators = $estimators;
3959
$this->batchSizeAdjusters = $batchSizeAdjusters;
60+
$this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
4061
}
4162

4263
/**
@@ -50,9 +71,18 @@ public function __construct(array $batchRowsCount, array $estimators, array $bat
5071
*/
5172
public function estimateBatchSize(\Magento\Framework\DB\Adapter\AdapterInterface $connection, $indexerTypeId)
5273
{
53-
$batchRowsCount = isset($this->batchRowsCount[$indexerTypeId])
54-
? $this->batchRowsCount[$indexerTypeId]
55-
: $this->batchRowsCount['default'];
74+
$batchRowsCount = $this->deploymentConfig->get(
75+
self::DEPLOYMENT_CONFIG_INDEXER_BATCHES . Processor::INDEXER_ID . '/' . $indexerTypeId,
76+
$batchRowsCount = $this->deploymentConfig->get(
77+
self::DEPLOYMENT_CONFIG_INDEXER_BATCHES . Processor::INDEXER_ID . '/' . 'default'
78+
)
79+
);
80+
81+
if (is_null($batchRowsCount)) {
82+
$batchRowsCount = isset($this->batchRowsCount[$indexerTypeId])
83+
? $this->batchRowsCount[$indexerTypeId]
84+
: $this->batchRowsCount['default'];
85+
}
5686

5787
/** @var \Magento\Framework\Indexer\BatchSizeManagementInterface $calculator */
5888
$calculator = isset($this->estimators[$indexerTypeId])

app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/Indexer/Price/BatchSizeCalculatorTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\Indexer\BatchSizeManagementInterface;
1313
use PHPUnit\Framework\MockObject\MockObject;
1414
use PHPUnit\Framework\TestCase;
15+
use Magento\Framework\App\DeploymentConfig;
1516

1617
class BatchSizeCalculatorTest extends TestCase
1718
{
@@ -30,14 +31,20 @@ class BatchSizeCalculatorTest extends TestCase
3031
*/
3132
private $batchRowsCount;
3233

34+
/**
35+
* @var DeploymentConfig|MockObject
36+
*/
37+
private $deploymentConfigMock;
38+
3339
protected function setUp(): void
3440
{
3541
$this->estimatorMock = $this->getMockForAbstractClass(BatchSizeManagementInterface::class);
3642
$this->batchRowsCount = 200;
3743
$this->model = new BatchSizeCalculator(
3844
['default' => $this->batchRowsCount],
3945
['default' => $this->estimatorMock],
40-
[]
46+
[],
47+
$this->createMock(DeploymentConfig::class)
4148
);
4249
}
4350

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
use Magento\Framework\Exception\LocalizedException;
2929
use Magento\CatalogInventory\Model\Indexer\Stock\AbstractAction;
3030
use Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\StockInterface;
31+
use Magento\Framework\App\DeploymentConfig;
32+
use Magento\CatalogInventory\Model\Indexer\Stock\Processor;
3133

3234
/**
3335
* Class Full reindex action
@@ -71,6 +73,18 @@ class Full extends AbstractAction
7173
*/
7274
private $batchQueryGenerator;
7375

76+
/**
77+
* @var DeploymentConfig|null
78+
*/
79+
private $deploymentConfig;
80+
81+
/**
82+
* Deployment config path
83+
*
84+
* @var string
85+
*/
86+
private const DEPLOYMENT_CONFIG_INDEXER_BATCHES = 'indexer/batch_size/';
87+
7488
/**
7589
* @param ResourceConnection $resource
7690
* @param StockFactory $indexerFactory
@@ -83,6 +97,7 @@ class Full extends AbstractAction
8397
* @param array $batchRowsCount
8498
* @param ActiveTableSwitcher|null $activeTableSwitcher
8599
* @param QueryGenerator|null $batchQueryGenerator
100+
* @param DeploymentConfig|null $deploymentConfig
86101
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
87102
*/
88103
public function __construct(
@@ -96,7 +111,8 @@ public function __construct(
96111
BatchProviderInterface $batchProvider = null,
97112
array $batchRowsCount = [],
98113
ActiveTableSwitcher $activeTableSwitcher = null,
99-
QueryGenerator $batchQueryGenerator = null
114+
QueryGenerator $batchQueryGenerator = null,
115+
?DeploymentConfig $deploymentConfig = null
100116
) {
101117
parent::__construct(
102118
$resource,
@@ -115,6 +131,7 @@ public function __construct(
115131
$this->activeTableSwitcher = $activeTableSwitcher ?: ObjectManager::getInstance()
116132
->get(ActiveTableSwitcher::class);
117133
$this->batchQueryGenerator = $batchQueryGenerator ?: ObjectManager::getInstance()->get(QueryGenerator::class);
134+
$this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
118135
}
119136

120137
/**
@@ -141,9 +158,18 @@ public function execute($ids = null): void
141158
$connection = $indexer->getConnection();
142159
$tableName = $this->activeTableSwitcher->getAdditionalTableName($indexer->getMainTable());
143160

144-
$batchRowCount = isset($this->batchRowsCount[$indexer->getTypeId()])
145-
? $this->batchRowsCount[$indexer->getTypeId()]
146-
: $this->batchRowsCount['default'];
161+
$batchRowCount = $this->deploymentConfig->get(
162+
self::DEPLOYMENT_CONFIG_INDEXER_BATCHES . Processor::INDEXER_ID . '/' . $indexer->getTypeId(),
163+
$this->deploymentConfig->get(
164+
self::DEPLOYMENT_CONFIG_INDEXER_BATCHES . Processor::INDEXER_ID . '/' . 'default'
165+
)
166+
);
167+
168+
if (is_null($batchRowCount)) {
169+
$batchRowCount = isset($this->batchRowsCount[$indexer->getTypeId()])
170+
? $this->batchRowsCount[$indexer->getTypeId()]
171+
: $this->batchRowsCount['default'];
172+
}
147173

148174
$this->batchSizeManagement->ensureBatchSize($connection, $batchRowCount);
149175

app/code/Magento/CatalogSearch/Model/Indexer/Fulltext.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Framework\Indexer\SaveHandler\IndexerInterface;
1616
use Magento\Store\Model\StoreDimensionProvider;
1717
use Magento\Indexer\Model\ProcessManager;
18+
use Magento\Framework\App\DeploymentConfig;
1819

1920
/**
2021
* Provide functionality for Fulltext Search indexing.
@@ -88,6 +89,18 @@ class Fulltext implements
8889
*/
8990
private $batchSize;
9091

92+
/**
93+
* @var DeploymentConfig|null
94+
*/
95+
private $deploymentConfig;
96+
97+
/**
98+
* Deployment config path
99+
*
100+
* @var string
101+
*/
102+
private const DEPLOYMENT_CONFIG_INDEXER_BATCHES = 'indexer/batch_size/';
103+
91104
/**
92105
* @param FullFactory $fullActionFactory
93106
* @param IndexerHandlerFactory $indexerHandlerFactory
@@ -96,8 +109,9 @@ class Fulltext implements
96109
* @param StateFactory $indexScopeStateFactory
97110
* @param DimensionProviderInterface $dimensionProvider
98111
* @param array $data
99-
* @param ProcessManager $processManager
112+
* @param ProcessManager|null $processManager
100113
* @param int|null $batchSize
114+
* @param DeploymentConfig $deploymentConfig
101115
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
102116
*/
103117
public function __construct(
@@ -109,7 +123,8 @@ public function __construct(
109123
DimensionProviderInterface $dimensionProvider,
110124
array $data,
111125
ProcessManager $processManager = null,
112-
?int $batchSize = null
126+
?int $batchSize = null,
127+
?DeploymentConfig $deploymentConfig = null
113128
) {
114129
$this->fullAction = $fullActionFactory->create(['data' => $data]);
115130
$this->indexerHandlerFactory = $indexerHandlerFactory;
@@ -120,6 +135,7 @@ public function __construct(
120135
$this->dimensionProvider = $dimensionProvider;
121136
$this->processManager = $processManager ?: ObjectManager::getInstance()->get(ProcessManager::class);
122137
$this->batchSize = $batchSize ?? self::BATCH_SIZE;
138+
$this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
123139
}
124140

125141
/**
@@ -165,6 +181,10 @@ public function executeByDimensions(array $dimensions, \Traversable $entityIds =
165181
$currentBatch = [];
166182
$i = 0;
167183

184+
$this->batchSize = $this->deploymentConfig->get(
185+
self::DEPLOYMENT_CONFIG_INDEXER_BATCHES . self::INDEXER_ID
186+
) ?? $this->batchSize;
187+
168188
foreach ($entityIds as $entityId) {
169189
$currentBatch[] = $entityId;
170190
if (++$i === $this->batchSize) {

0 commit comments

Comments
 (0)