Skip to content

Commit 8d91105

Browse files
author
Stanislav Idolov
committed
MAGETWO-64187: Batch size algorithm
1 parent 7523e1d commit 8d91105

File tree

17 files changed

+333
-267
lines changed

17 files changed

+333
-267
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,11 @@
139139
</type>
140140
<type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BatchSizeCalculator">
141141
<arguments>
142-
<argument name="calculators" xsi:type="array">
143-
<item name="bundle" xsi:type="object">\Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\CompositeProductBatchSizeCalculator</item>
142+
<argument name="batchRowsCount" xsi:type="array">
143+
<item name="bundle" xsi:type="number">100000</item>
144+
</argument>
145+
<argument name="estimators" xsi:type="array">
146+
<item name="bundle" xsi:type="object">Magento\Catalog\Model\Indexer\CompositeProductBatchSizeManagement</item>
144147
</argument>
145148
</arguments>
146149
</type>

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ class BatchSizeCalculator
1111
/**
1212
* @var array
1313
*/
14-
private $memoryTablesMinRows;
14+
private $batchRowsCount;
1515

1616
/**
17-
* @var \Magento\Framework\Indexer\BatchSizeCalculatorInterface[]
17+
* @var \Magento\Framework\Indexer\BatchSizeManagementInterface[]
1818
*/
19-
private $calculators;
19+
private $estimators;
2020

2121
/**
2222
* BatchSizeCalculator constructor.
23-
* @param array $memoryTablesMinRows
24-
* @param array $calculators
23+
* @param array $batchRowsCount
24+
* @param array $estimators
2525
*/
26-
public function __construct(array $memoryTablesMinRows, array $calculators)
26+
public function __construct(array $batchRowsCount, array $estimators)
2727
{
28-
$this->memoryTablesMinRows = $memoryTablesMinRows;
29-
$this->calculators = $calculators;
28+
$this->batchRowsCount = $batchRowsCount;
29+
$this->estimators = $estimators;
3030
}
3131

3232
/**
@@ -38,15 +38,17 @@ public function __construct(array $memoryTablesMinRows, array $calculators)
3838
*/
3939
public function estimateBatchSize(\Magento\Framework\DB\Adapter\AdapterInterface $connection, $indexerTypeId)
4040
{
41-
$memoryTableMinRows = isset($this->memoryTablesMinRows[$indexerTypeId])
42-
? $this->memoryTablesMinRows[$indexerTypeId]
43-
: $this->memoryTablesMinRows['default'];
41+
$batchRowsCount = isset($this->batchRowsCount[$indexerTypeId])
42+
? $this->batchRowsCount[$indexerTypeId]
43+
: $this->batchRowsCount['default'];
4444

45-
/** @var \Magento\Framework\Indexer\BatchSizeCalculatorInterface $calculator */
46-
$calculator = isset($this->calculators[$indexerTypeId])
47-
? $this->calculators[$indexerTypeId]
48-
: $this->calculators['default'];
45+
/** @var \Magento\Framework\Indexer\BatchSizeManagementInterface $calculator */
46+
$calculator = isset($this->estimators[$indexerTypeId])
47+
? $this->estimators[$indexerTypeId]
48+
: $this->estimators['default'];
4949

50-
return $calculator->estimateBatchSize($connection, $memoryTableMinRows);
50+
$calculator->ensureBatchSize($connection, $batchRowsCount);
51+
52+
return $batchRowsCount;
5153
}
5254
}

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

Lines changed: 0 additions & 96 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Model\ResourceModel\Product\Indexer\Price;
8+
9+
class CompositeProductRowSizeEstimator implements \Magento\Framework\Indexer\IndexTableRowSizeEstimatorInterface
10+
{
11+
/**
12+
* @var \Magento\Indexer\Model\IndexTableRowSizeEstimator
13+
*/
14+
private $indexTableRowSizeEstimator;
15+
16+
/**
17+
* @var \Magento\Framework\App\ResourceConnection
18+
*/
19+
private $resourceConnection;
20+
21+
/**
22+
* @param \Magento\Framework\App\ResourceConnection $resourceConnection
23+
* @param \Magento\Indexer\Model\IndexTableRowSizeEstimator $indexTableRowSizeEstimator
24+
*/
25+
public function __construct(
26+
\Magento\Framework\App\ResourceConnection $resourceConnection,
27+
\Magento\Indexer\Model\IndexTableRowSizeEstimator $indexTableRowSizeEstimator
28+
) {
29+
$this->resourceConnection = $resourceConnection;
30+
$this->indexTableRowSizeEstimator = $indexTableRowSizeEstimator;
31+
}
32+
33+
/**
34+
* Calculate memory size for largest composite product in database.
35+
*
36+
* @inheritdoc
37+
*/
38+
public function estimateRowSize()
39+
{
40+
$connection = $this->resourceConnection->getConnection();
41+
$relationSelect = $connection->select();
42+
$relationSelect->from(
43+
['relation' => $connection->getTableName('catalog_product_relation')],
44+
['count' => new \Zend_Db_Expr('count(relation.child_id)')]
45+
);
46+
$relationSelect->group('parent_id');
47+
48+
$maxSelect = $connection->select();
49+
$maxSelect->from(
50+
['max_value' => $relationSelect],
51+
['count' => new \Zend_Db_Expr('MAX(count)')]
52+
);
53+
$maxRelatedProductCount = $connection->fetchOne($maxSelect);
54+
55+
/**
56+
* Calculate memory size for largest composite product in database.
57+
*
58+
* $maxRelatedProductCount - maximum number of related products
59+
*/
60+
return ceil($maxRelatedProductCount * $this->indexTableRowSizeEstimator->estimateRowSize());
61+
}
62+
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@ class BatchSizeCalculatorTest extends \PHPUnit_Framework_TestCase
1414
private $model;
1515

1616
/**
17-
* @var \Magento\Framework\Indexer\BatchSizeCalculatorInterface|\PHPUnit_Framework_MockObject_MockObject
17+
* @var \Magento\Framework\Indexer\BatchSizeManagementInterface|\PHPUnit_Framework_MockObject_MockObject
1818
*/
19-
private $calculatorMock;
19+
private $estimatorMock;
2020

2121
/**
2222
* @var int
2323
*/
24-
private $memoryTablesMinRows;
24+
private $batchRowsCount;
2525

2626
protected function setUp()
2727
{
28-
$this->calculatorMock = $this->getMock(\Magento\Framework\Indexer\BatchSizeCalculatorInterface::class);
29-
$this->memoryTablesMinRows = 200;
28+
$this->estimatorMock = $this->getMock(\Magento\Framework\Indexer\BatchSizeManagementInterface::class);
29+
$this->batchRowsCount = 200;
3030
$this->model = new \Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BatchSizeCalculator(
31-
['default' => $this->memoryTablesMinRows],
32-
['default' => $this->calculatorMock]
31+
['default' => $this->batchRowsCount],
32+
['default' => $this->estimatorMock]
3333
);
3434
}
3535

@@ -39,9 +39,9 @@ public function testEstimateBatchSize()
3939
$typeId = 'default';
4040
$batchSize = 100500;
4141

42-
$this->calculatorMock->expects($this->once())
43-
->method('estimateBatchSize')
44-
->with($connectionMock, $this->memoryTablesMinRows)
42+
$this->estimatorMock->expects($this->once())
43+
->method('ensureBatchSize')
44+
->with($connectionMock, $this->batchRowsCount)
4545
->willReturn($batchSize);
4646

4747
$this->model->estimateBatchSize($connectionMock, $typeId);

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -918,14 +918,19 @@
918918
<preference for="Magento\Catalog\Pricing\Price\MinimalPriceCalculatorInterface" type="Magento\Catalog\Pricing\Price\MinimalTierPriceCalculator" />
919919
<type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BatchSizeCalculator">
920920
<arguments>
921-
<argument name="memoryTablesMinRows" xsi:type="array">
922-
<item name="default" xsi:type="number">200</item>
921+
<argument name="batchRowsCount" xsi:type="array">
922+
<item name="default" xsi:type="number">200000</item>
923923
</argument>
924-
<argument name="calculators" xsi:type="array">
925-
<item name="default" xsi:type="object">\Magento\Framework\Indexer\BatchSizeCalculator</item>
924+
<argument name="estimators" xsi:type="array">
925+
<item name="default" xsi:type="object">\Magento\Framework\Indexer\BatchSizeManagement</item>
926926
</argument>
927927
</arguments>
928928
</type>
929+
<virtualType name="Magento\Catalog\Model\Indexer\CompositeProductBatchSizeManagement" type="\Magento\Framework\Indexer\BatchSizeManagement">
930+
<arguments>
931+
<argument name="rowSizeEstimator" xsi:type="object" shared="false">Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\CompositeProductRowSizeEstimator</argument>
932+
</arguments>
933+
</virtualType>
929934
<virtualType name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\FrontendResource" type="Magento\Indexer\Model\ResourceModel\FrontendResource">
930935
<arguments>
931936
<argument name="indexerId" xsi:type="const">Magento\Catalog\Model\Indexer\Product\Price\Processor::INDEXER_ID</argument>

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,11 @@
173173
</type>
174174
<type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BatchSizeCalculator">
175175
<arguments>
176-
<argument name="calculators" xsi:type="array">
177-
<item name="configurable" xsi:type="object">\Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\CompositeProductBatchSizeCalculator</item>
176+
<argument name="batchRowsCount" xsi:type="array">
177+
<item name="configurable" xsi:type="number">100000</item>
178+
</argument>
179+
<argument name="estimators" xsi:type="array">
180+
<item name="configurable" xsi:type="object">Magento\Catalog\Model\Indexer\CompositeProductBatchSizeManagement</item>
178181
</argument>
179182
</arguments>
180183
</type>

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,11 @@
9191
type="Magento\GroupedProduct\Model\ResourceModel\Product\Indexer\Price\Grouped"/>
9292
<type name="Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\BatchSizeCalculator">
9393
<arguments>
94-
<argument name="calculators" xsi:type="array">
95-
<item name="grouped" xsi:type="object">\Magento\Catalog\Model\ResourceModel\Product\Indexer\Price\CompositeProductBatchSizeCalculator</item>
94+
<argument name="batchRowsCount" xsi:type="array">
95+
<item name="grouped" xsi:type="number">100000</item>
96+
</argument>
97+
<argument name="estimators" xsi:type="array">
98+
<item name="grouped" xsi:type="object">Magento\Catalog\Model\Indexer\CompositeProductBatchSizeManagement</item>
9699
</argument>
97100
</arguments>
98101
</type>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Indexer\Model;
8+
9+
class IndexTableRowSizeEstimator implements \Magento\Framework\Indexer\IndexTableRowSizeEstimatorInterface
10+
{
11+
/**
12+
* @var \Magento\Store\Api\WebsiteManagementInterface
13+
*/
14+
private $websiteManagement;
15+
16+
/**
17+
* @var \Magento\Customer\Model\ResourceModel\Group\CollectionFactory
18+
*/
19+
private $collectionFactory;
20+
21+
/**
22+
* CompositeProductBatchSizeCalculator constructor.
23+
* @param \Magento\Store\Api\WebsiteManagementInterface $websiteManagement
24+
* @param \Magento\Customer\Model\ResourceModel\Group\CollectionFactory $collectionFactory
25+
*/
26+
public function __construct(
27+
\Magento\Store\Api\WebsiteManagementInterface $websiteManagement,
28+
\Magento\Customer\Model\ResourceModel\Group\CollectionFactory $collectionFactory
29+
) {
30+
$this->websiteManagement = $websiteManagement;
31+
$this->collectionFactory = $collectionFactory;
32+
}
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
public function estimateRowSize()
38+
{
39+
$websitesCount = $this->websiteManagement->getCount();
40+
41+
/** @var \Magento\Customer\Model\ResourceModel\Group\Collection $collection */
42+
$collection = $this->collectionFactory->create();
43+
$customerGroupCount = $collection->getSize();
44+
45+
/**
46+
* Calculate memory size for product in database.
47+
*
48+
* $websitesCount - active websites
49+
* $customerGroupCount - active customer groups
50+
* 90 - calculated memory size for one record in catalog_product_index_price table
51+
*/
52+
return ceil($websitesCount * $customerGroupCount * 90);
53+
}
54+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,10 @@
5555
</argument>
5656
</arguments>
5757
</type>
58+
<preference for="Magento\Framework\Indexer\BatchSizeManagementInterface" type="Magento\Framework\Indexer\BatchSizeManagement" />
59+
<type name="Magento\Framework\Indexer\BatchSizeManagement">
60+
<arguments>
61+
<argument name="rowSizeEstimator" xsi:type="object" shared="false">Magento\Indexer\Model\IndexTableRowSizeEstimator</argument>
62+
</arguments>
63+
</type>
5864
</config>

0 commit comments

Comments
 (0)