Skip to content

Commit 709b275

Browse files
committed
MC-42025: Bad performance and outages after upgrade to MariaDB 10.2.34
1 parent d4f3adf commit 709b275

File tree

3 files changed

+115
-7
lines changed

3 files changed

+115
-7
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sales\Model\Grid;
9+
10+
use Magento\Framework\App\CacheInterface;
11+
12+
/**
13+
* Cache for last grid update time.
14+
*/
15+
class LastUpdateTimeCache
16+
{
17+
/**
18+
* Prefix for cache key.
19+
*/
20+
private const CACHE_PREFIX = 'LAST_GRID_UPDATE_TIME';
21+
22+
/**
23+
* @var CacheInterface
24+
*/
25+
private $cache;
26+
27+
/**
28+
* @param CacheInterface $cache
29+
*/
30+
public function __construct(CacheInterface $cache)
31+
{
32+
$this->cache = $cache;
33+
}
34+
35+
/**
36+
* Save last grid update time.
37+
*
38+
* @param string $gridTableName
39+
* @param string $lastUpdatedAt
40+
* @return void
41+
*/
42+
public function save(string $gridTableName, string $lastUpdatedAt): void
43+
{
44+
$this->cache->save(
45+
$lastUpdatedAt,
46+
$this->getCacheKey($gridTableName),
47+
[],
48+
3600
49+
);
50+
}
51+
52+
/**
53+
* Get last grid update time.
54+
*
55+
* @param string $gridTableName
56+
* @return string|null
57+
*/
58+
public function get(string $gridTableName): ?string
59+
{
60+
$lastUpdatedAt = $this->cache->load($this->getCacheKey($gridTableName));
61+
62+
return $lastUpdatedAt ?: null;
63+
}
64+
65+
/**
66+
* Generate cache key.
67+
*
68+
* @param string $gridTableName
69+
* @return string
70+
*/
71+
private function getCacheKey(string $gridTableName): string
72+
{
73+
return self::CACHE_PREFIX . $gridTableName;
74+
}
75+
}

app/code/Magento/Sales/Model/ResourceModel/Grid.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
use Magento\Framework\App\ObjectManager;
99
use Magento\Framework\DB\Adapter\AdapterInterface;
1010
use Magento\Framework\Model\ResourceModel\Db\Context;
11+
use Magento\Sales\Model\Grid\LastUpdateTimeCache;
1112
use Magento\Sales\Model\ResourceModel\Provider\NotSyncedDataProviderInterface;
1213

1314
/**
14-
* Class Grid
15+
* Sales order grid resource model.
1516
*/
1617
class Grid extends AbstractGrid
1718
{
@@ -45,6 +46,11 @@ class Grid extends AbstractGrid
4546
*/
4647
private $notSyncedDataProvider;
4748

49+
/**
50+
* @var LastUpdateTimeCache
51+
*/
52+
private $lastUpdateTimeCache;
53+
4854
/**
4955
* Order grid rows batch size
5056
*/
@@ -58,7 +64,8 @@ class Grid extends AbstractGrid
5864
* @param array $joins
5965
* @param array $columns
6066
* @param string $connectionName
61-
* @param NotSyncedDataProviderInterface $notSyncedDataProvider
67+
* @param NotSyncedDataProviderInterface|null $notSyncedDataProvider
68+
* @param LastUpdateTimeCache|null $lastUpdateTimeCache
6269
*/
6370
public function __construct(
6471
Context $context,
@@ -68,15 +75,19 @@ public function __construct(
6875
array $joins = [],
6976
array $columns = [],
7077
$connectionName = null,
71-
NotSyncedDataProviderInterface $notSyncedDataProvider = null
78+
NotSyncedDataProviderInterface $notSyncedDataProvider = null,
79+
LastUpdateTimeCache $lastUpdateTimeCache = null
7280
) {
7381
$this->mainTableName = $mainTableName;
7482
$this->gridTableName = $gridTableName;
7583
$this->orderIdField = $orderIdField;
7684
$this->joins = $joins;
7785
$this->columns = $columns;
78-
$this->notSyncedDataProvider =
79-
$notSyncedDataProvider ?: ObjectManager::getInstance()->get(NotSyncedDataProviderInterface::class);
86+
$this->notSyncedDataProvider = $notSyncedDataProvider ??
87+
ObjectManager::getInstance()->get(NotSyncedDataProviderInterface::class);
88+
$this->lastUpdateTimeCache = $lastUpdateTimeCache ??
89+
ObjectManager::getInstance()->get(LastUpdateTimeCache::class);
90+
8091
parent::__construct($context, $connectionName);
8192
}
8293

@@ -118,6 +129,7 @@ public function refresh($value, $field = null)
118129
*/
119130
public function refreshBySchedule()
120131
{
132+
$lastUpdatedAt = null;
121133
$notSyncedIds = $this->notSyncedDataProvider->getIds($this->mainTableName, $this->gridTableName);
122134
foreach (array_chunk($notSyncedIds, self::BATCH_SIZE) as $bunch) {
123135
$select = $this->getGridOriginSelect()->where($this->mainTableName . '.entity_id IN (?)', $bunch);
@@ -127,6 +139,12 @@ public function refreshBySchedule()
127139
$fetchResult,
128140
array_keys($this->columns)
129141
);
142+
143+
$lastUpdatedAt = max(array_column($fetchResult, 'updated_at'));
144+
}
145+
146+
if ($lastUpdatedAt) {
147+
$this->lastUpdateTimeCache->save($this->gridTableName, $lastUpdatedAt);
130148
}
131149
}
132150

app/code/Magento/Sales/Model/ResourceModel/Provider/UpdatedAtListProvider.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Framework\App\ResourceConnection;
99
use Magento\Framework\DB\Adapter\AdapterInterface;
10+
use Magento\Sales\Model\Grid\LastUpdateTimeCache;
1011

1112
/**
1213
* Retrieves ID's of not synced by `updated_at` column entities.
@@ -25,13 +26,22 @@ class UpdatedAtListProvider implements NotSyncedDataProviderInterface
2526
*/
2627
private $connection;
2728

29+
/**
30+
* @var LastUpdateTimeCache
31+
*/
32+
private $lastUpdateTimeCache;
33+
2834
/**
2935
* @param ResourceConnection $resourceConnection
36+
* @param LastUpdateTimeCache $lastUpdateTimeCache
3037
*/
31-
public function __construct(ResourceConnection $resourceConnection)
32-
{
38+
public function __construct(
39+
ResourceConnection $resourceConnection,
40+
LastUpdateTimeCache $lastUpdateTimeCache
41+
) {
3342
$this->connection = $resourceConnection->getConnection('sales');
3443
$this->resourceConnection = $resourceConnection;
44+
$this->lastUpdateTimeCache = $lastUpdateTimeCache;
3545
}
3646

3747
/**
@@ -55,6 +65,11 @@ public function getIds($mainTableName, $gridTableName)
5565
[]
5666
);
5767

68+
$lastUpdatedAt = $this->lastUpdateTimeCache->get($gridTableName);
69+
if ($lastUpdatedAt) {
70+
$select->where($mainTableName . '.updated_at > ?', $lastUpdatedAt);
71+
}
72+
5873
return $this->connection->fetchAll($select, [], \Zend_Db::FETCH_COLUMN);
5974
}
6075
}

0 commit comments

Comments
 (0)