Skip to content

Commit b714634

Browse files
MAGETWO-64954: [Performance] Fix innodb_lock_wait_timeout in the application
1 parent 15c21ad commit b714634

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ public function convertDateTime($datetime)
356356
*
357357
* @return void
358358
* @throws \Zend_Db_Adapter_Exception
359+
* @throws \Zend_Db_Statement_Exception
359360
*/
360361
protected function _connect()
361362
{
@@ -399,7 +400,10 @@ protected function _connect()
399400
$this->_connection->query("SET time_zone = '+00:00'");
400401

401402
if (isset($this->_config['initStatements'])) {
402-
$this->query($this->_config['initStatements']);
403+
$statements = $this->_splitMultiQuery($this->_config['initStatements']);
404+
foreach ($statements as $statement) {
405+
$this->_query($statement);
406+
}
403407
}
404408

405409
if (!$this->_connectionFlagsSet) {

lib/internal/Magento/Framework/Mview/Test/Unit/ViewTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ public function testUpdate()
299299
$currentVersionId = 3;
300300
$lastVersionId = 1;
301301
$listId = [2, 3];
302+
$defaultBatchSize = 1000;
303+
302304
$this->stateMock->expects($this->any())
303305
->method('getViewId')
304306
->will($this->returnValue(1));
@@ -331,7 +333,7 @@ public function testUpdate()
331333
'getList'
332334
)->with(
333335
$lastVersionId,
334-
$currentVersionId
336+
$lastVersionId + $defaultBatchSize
335337
)->will(
336338
$this->returnValue($listId)
337339
);
@@ -361,6 +363,8 @@ public function testUpdateWithException()
361363
$currentVersionId = 3;
362364
$lastVersionId = 1;
363365
$listId = [2, 3];
366+
$defaultBatchSize = 1000;
367+
364368
$this->stateMock->expects($this->any())
365369
->method('getViewId')
366370
->will($this->returnValue(1));
@@ -392,7 +396,7 @@ public function testUpdateWithException()
392396
'getList'
393397
)->with(
394398
$lastVersionId,
395-
$currentVersionId
399+
$lastVersionId + $defaultBatchSize
396400
)->will(
397401
$this->returnValue($listId)
398402
);

lib/internal/Magento/Framework/Mview/View.php

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ class View extends \Magento\Framework\DataObject implements ViewInterface
4646
*/
4747
protected $state;
4848

49+
/**
50+
* @var array
51+
*/
52+
protected $changelogBatchSize = [
53+
'default_size' => 1000,
54+
'catalog_category_product' => 1000,
55+
'catalog_product_category' => 1000,
56+
'catalog_product_price' => 1000,
57+
'catalog_product_attribute' => 1000,
58+
'cataloginventory_stock' => 1000,
59+
'catalogrule_rule' => 1000,
60+
'catalogrule_product' => 1000,
61+
'catalogsearch_fulltext' => 1000,
62+
];
63+
4964
/**
5065
* @param ConfigInterface $config
5166
* @param ActionFactory $actionFactory
@@ -253,26 +268,36 @@ public function update()
253268
} catch (ChangelogTableNotExistsException $e) {
254269
return;
255270
}
256-
$lastVersionId = $this->getState()->getVersionId();
257-
$ids = $this->getChangelog()->getList($lastVersionId, $currentVersionId);
258-
if ($ids) {
259-
$action = $this->actionFactory->get($this->getActionClass());
271+
$lastVersionId = (int) $this->getState()->getVersionId();
272+
$action = $this->actionFactory->get($this->getActionClass());
273+
274+
try {
260275
$this->getState()->setStatus(View\StateInterface::STATUS_WORKING)->save();
261-
try {
262-
$action->execute($ids);
263-
$this->getState()->loadByView($this->getId());
264-
$statusToRestore = $this->getState()->getStatus() == View\StateInterface::STATUS_SUSPENDED
265-
? View\StateInterface::STATUS_SUSPENDED
266-
: View\StateInterface::STATUS_IDLE;
267-
$this->getState()->setVersionId($currentVersionId)->setStatus($statusToRestore)->save();
268-
} catch (\Exception $exception) {
269-
$this->getState()->loadByView($this->getId());
270-
$statusToRestore = $this->getState()->getStatus() == View\StateInterface::STATUS_SUSPENDED
271-
? View\StateInterface::STATUS_SUSPENDED
272-
: View\StateInterface::STATUS_IDLE;
273-
$this->getState()->setStatus($statusToRestore)->save();
274-
throw $exception;
276+
277+
$batchSize = isset($this->changelogBatchSize[$this->getChangelog()->getViewId()])
278+
? $this->changelogBatchSize[$this->getChangelog()->getViewId()]
279+
: $this->changelogBatchSize['default_size'];
280+
281+
for ($versionFrom = $lastVersionId; $versionFrom < $currentVersionId; $versionFrom += $batchSize) {
282+
$ids = $this->getChangelog()->getList($versionFrom, $versionFrom + $batchSize);
283+
284+
if (!empty($ids)) {
285+
$action->execute($ids);
286+
}
275287
}
288+
289+
$this->getState()->loadByView($this->getId());
290+
$statusToRestore = $this->getState()->getStatus() == View\StateInterface::STATUS_SUSPENDED
291+
? View\StateInterface::STATUS_SUSPENDED
292+
: View\StateInterface::STATUS_IDLE;
293+
$this->getState()->setVersionId($currentVersionId)->setStatus($statusToRestore)->save();
294+
} catch (\Exception $exception) {
295+
$this->getState()->loadByView($this->getId());
296+
$statusToRestore = $this->getState()->getStatus() == View\StateInterface::STATUS_SUSPENDED
297+
? View\StateInterface::STATUS_SUSPENDED
298+
: View\StateInterface::STATUS_IDLE;
299+
$this->getState()->setStatus($statusToRestore)->save();
300+
throw $exception;
276301
}
277302
}
278303
}

0 commit comments

Comments
 (0)