Skip to content

Commit 74350c7

Browse files
author
Stanislav Idolov
committed
MAGETWO-64182: [Indexer optimizations] Batch data processing for price indexer
1 parent 7935aca commit 74350c7

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

lib/internal/Magento/Framework/Indexer/BatchSizeManagement.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,22 @@ class BatchSizeManagement implements \Magento\Framework\Indexer\BatchSizeManagem
1616
*/
1717
private $rowSizeEstimator;
1818

19+
/**
20+
* @var \Psr\Log\LoggerInterface
21+
*/
22+
private $logger;
23+
1924
/**
2025
* CompositeProductBatchSizeCalculator constructor.
2126
* @param \Magento\Framework\Indexer\IndexTableRowSizeEstimatorInterface $rowSizeEstimator
27+
* @param \Psr\Log\LoggerInterface $logger
2228
*/
2329
public function __construct(
24-
\Magento\Framework\Indexer\IndexTableRowSizeEstimatorInterface $rowSizeEstimator
30+
\Magento\Framework\Indexer\IndexTableRowSizeEstimatorInterface $rowSizeEstimator,
31+
\Psr\Log\LoggerInterface $logger
2532
) {
2633
$this->rowSizeEstimator = $rowSizeEstimator;
34+
$this->logger = $logger;
2735
}
2836

2937
/**
@@ -35,10 +43,19 @@ public function ensureBatchSize(\Magento\Framework\DB\Adapter\AdapterInterface $
3543

3644
$maxHeapTableSize = $connection->fetchOne('SELECT @@max_heap_table_size;');
3745
$tmpTableSize = $connection->fetchOne('SELECT @@tmp_table_size;');
46+
$bufferPoolSize = $connection->fetchOne('SELECT @@innodb_buffer_pool_size;');
3847
$maxMemoryTableSize = min($maxHeapTableSize, $tmpTableSize);
3948

4049
$size = (int) ($rowMemory * $batchSize);
4150

51+
// Log warning if allocated memory for temp table greater than 20% of innodb_buffer_pool_size
52+
if ($size > $bufferPoolSize * .2) {
53+
$this->logger->warning(__(
54+
'Memory size allocated for temporary table is more than 20% innodb_buffer_pool_size. ' .
55+
'Please update innodb_buffer_pool_size or decrease batch size value.'
56+
));
57+
}
58+
4259
if ($maxMemoryTableSize < $size) {
4360
$connection->query('SET SESSION tmp_table_size = ' . $size . ';');
4461
$connection->query('SET SESSION max_heap_table_size = ' . $size . ';');

lib/internal/Magento/Framework/Indexer/Test/Unit/BatchSizeManagementTest.php

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,28 @@ class BatchSizeManagementTest extends \PHPUnit_Framework_TestCase
2020
*/
2121
private $rowSizeEstimatorMock;
2222

23+
/**
24+
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
private $loggerMock;
27+
2328
protected function setUp()
2429
{
2530
$this->rowSizeEstimatorMock = $this->getMock(
2631
\Magento\Framework\Indexer\IndexTableRowSizeEstimatorInterface::class
2732
);
28-
$this->model = new BatchSizeManagement($this->rowSizeEstimatorMock);
33+
$this->loggerMock = $this->getMock(\Psr\Log\LoggerInterface::class);
34+
$this->model = new BatchSizeManagement($this->rowSizeEstimatorMock, $this->loggerMock);
2935
}
3036

31-
/**
32-
* @param int $batchSize number of records in the batch
33-
* @param int $maxHeapTableSize max_heap_table_size MySQL value
34-
* @param int $tmpTableSize tmp_table_size MySQL value
35-
* @param int $size
36-
*
37-
* @dataProvider estimateBatchSizeDataProvider
38-
*/
39-
public function testEnsureBatchSize($batchSize, $maxHeapTableSize, $tmpTableSize, $size)
37+
public function testEnsureBatchSize()
4038
{
39+
$batchSize = 200;
40+
$maxHeapTableSize = 16384;
41+
$tmpTableSize = 16384;
42+
$size = 20000;
43+
$innodbPollSize = 100;
44+
4145
$this->rowSizeEstimatorMock->expects($this->once())->method('estimateRowSize')->willReturn(100);
4246
$adapterMock = $this->getMock(AdapterInterface::class);
4347
$adapterMock->expects($this->at(0))
@@ -48,28 +52,26 @@ public function testEnsureBatchSize($batchSize, $maxHeapTableSize, $tmpTableSize
4852
->method('fetchOne')
4953
->with('SELECT @@tmp_table_size;', [])
5054
->willReturn($tmpTableSize);
51-
5255
$adapterMock->expects($this->at(2))
56+
->method('fetchOne')
57+
->with('SELECT @@innodb_buffer_pool_size;', [])
58+
->willReturn($innodbPollSize);
59+
60+
$this->loggerMock->expects($this->once())
61+
->method('warning')
62+
->with(__(
63+
'Memory size allocated for temporary table is more than 20% innodb_buffer_pool_size. ' .
64+
'Please update innodb_buffer_pool_size or decrease batch size value.'
65+
));
66+
67+
$adapterMock->expects($this->at(3))
5368
->method('query')
5469
->with('SET SESSION tmp_table_size = ' . $size . ';', []);
5570

56-
$adapterMock->expects($this->at(3))
71+
$adapterMock->expects($this->at(4))
5772
->method('query')
5873
->with('SET SESSION max_heap_table_size = ' . $size . ';', []);
5974

6075
$this->model->ensureBatchSize($adapterMock, $batchSize);
6176
}
62-
63-
/**
64-
* @return array
65-
*/
66-
public function estimateBatchSizeDataProvider()
67-
{
68-
return [
69-
[200, 16384, 16384, 20000],
70-
[300, 16384, 20000, 30000],
71-
[400, 20000, 16384, 40000],
72-
[500, 2000, 2000, 50000],
73-
];
74-
}
7577
}

0 commit comments

Comments
 (0)