Skip to content

Commit 8c64127

Browse files
MAGETWO-71648: Disabling temp table removal on non-persistent connections for performance
1 parent 4381a6b commit 8c64127

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

lib/internal/Magento/Framework/Search/Adapter/Mysql/TemporaryStorage.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\Framework\Search\Adapter\Mysql;
88

9+
use Magento\Framework\App\DeploymentConfig;
10+
use Magento\Framework\App\ObjectManager;
911
use Magento\Framework\DB\Adapter\AdapterInterface;
1012
use Magento\Framework\DB\Ddl\Table;
1113
use Magento\Framework\DB\Select;
@@ -25,12 +27,21 @@ class TemporaryStorage
2527
*/
2628
private $resource;
2729

30+
/**
31+
* @var DeploymentConfig
32+
*/
33+
private $config;
34+
2835
/**
2936
* @param \Magento\Framework\App\ResourceConnection $resource
37+
* @param DeploymentConfig|null $config
3038
*/
31-
public function __construct(\Magento\Framework\App\ResourceConnection $resource)
32-
{
39+
public function __construct(
40+
\Magento\Framework\App\ResourceConnection $resource,
41+
DeploymentConfig $config = null
42+
) {
3343
$this->resource = $resource;
44+
$this->config = $config !== null ? $config : ObjectManager::getInstance()->get(DeploymentConfig::class);
3445
}
3546

3647
/**
@@ -117,7 +128,9 @@ private function createTemporaryTable()
117128
$connection = $this->getConnection();
118129
$tableName = $this->resource->getTableName(str_replace('.', '_', uniqid(self::TEMPORARY_TABLE_PREFIX, true)));
119130
$table = $connection->newTable($tableName);
120-
$connection->dropTemporaryTable($table->getName());
131+
if ($this->config->get('db/connection/indexer/persistent')) {
132+
$connection->dropTemporaryTable($table->getName());
133+
}
121134
$table->addColumn(
122135
self::FIELD_ENTITY_ID,
123136
Table::TYPE_INTEGER,

lib/internal/Magento/Framework/Search/Test/Unit/Adapter/Mysql/TemporaryStorageTest.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class TemporaryStorageTest extends \PHPUnit\Framework\TestCase
2626
*/
2727
private $model;
2828

29+
/**
30+
* @var \Magento\Framework\App\DeploymentConfig|\PHPUnit_Framework_MockObject_MockObject
31+
*/
32+
private $config;
33+
2934
protected function setUp()
3035
{
3136
$this->tableName = 'some_table_name';
@@ -44,9 +49,13 @@ protected function setUp()
4449
->method('getTableName')
4550
->willReturn($this->tableName);
4651

52+
$this->config = $this->getMockBuilder(\Magento\Framework\App\DeploymentConfig::class)
53+
->disableOriginalConstructor()
54+
->getMock();
55+
4756
$this->model = (new ObjectManager($this))->getObject(
4857
\Magento\Framework\Search\Adapter\Mysql\TemporaryStorage::class,
49-
['resource' => $resource]
58+
['resource' => $resource, 'config' => $this->config]
5059
);
5160
}
5261

@@ -138,15 +147,38 @@ public function testStoreApiDocuments()
138147
$this->assertEquals($result, $table);
139148
}
140149

150+
public function testNoDropIfNotPersistent()
151+
{
152+
$this->createTemporaryTable(false);
153+
154+
$this->adapter->expects($this->never())
155+
->method('dropTemporaryTable');
156+
157+
// model->createTemporaryTable() is a private method; this will call it
158+
$this->model->storeApiDocuments([]);
159+
}
160+
141161
/**
142162
* @return \Magento\Framework\DB\Ddl\Table|\PHPUnit_Framework_MockObject_MockObject
143163
*/
144-
private function createTemporaryTable()
164+
private function createTemporaryTable($persistentConnection = true)
145165
{
166+
$this->config->expects($this->any())
167+
->method('get')
168+
->with('db/connection/indexer/persistent')
169+
->willReturn($persistentConnection);
170+
146171
$table = $this->getMockBuilder(\Magento\Framework\DB\Ddl\Table::class)
147172
->disableOriginalConstructor()
148173
->getMock();
149-
$table->expects($this->at(1))
174+
175+
$tableInteractionCount = 0;
176+
if ($persistentConnection) {
177+
$this->adapter->expects($this->once())
178+
->method('dropTemporaryTable');
179+
$tableInteractionCount += 1;
180+
}
181+
$table->expects($this->at($tableInteractionCount))
150182
->method('addColumn')
151183
->with(
152184
TemporaryStorage::FIELD_ENTITY_ID,
@@ -155,7 +187,8 @@ private function createTemporaryTable()
155187
['unsigned' => true, 'nullable' => false, 'primary' => true],
156188
'Entity ID'
157189
);
158-
$table->expects($this->at(2))
190+
$tableInteractionCount += 1;
191+
$table->expects($this->at($tableInteractionCount))
159192
->method('addColumn')
160193
->with(
161194
'score',
@@ -172,8 +205,6 @@ private function createTemporaryTable()
172205
->method('newTable')
173206
->with($this->tableName)
174207
->willReturn($table);
175-
$this->adapter->expects($this->once())
176-
->method('dropTemporaryTable');
177208
$this->adapter->expects($this->once())
178209
->method('createTemporaryTable')
179210
->with($table);

0 commit comments

Comments
 (0)