Skip to content

Commit 508d51b

Browse files
MAGETWO-85904: Investigate fluctuations of PAT Nightly build
1 parent d30fb35 commit 508d51b

File tree

9 files changed

+132
-14
lines changed

9 files changed

+132
-14
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Product/Indexer/LinkedProductSelectBuilderByIndexPrice.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public function build($productId)
8686
->where('t.website_id = ?', $this->storeManager->getStore()->getWebsiteId())
8787
->where('t.customer_group_id = ?', $this->customerSession->getCustomerGroupId())
8888
->order('t.min_price ' . Select::SQL_ASC)
89+
->order(BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS . '.' . $linkField . ' ' . Select::SQL_ASC)
8990
->limit(1);
9091
$priceSelect = $this->baseSelectProcessor->process($priceSelect);
9192

app/code/Magento/Catalog/Model/ResourceModel/Product/LinkedProductSelectBuilderByBasePrice.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public function build($productId)
9595
->where('t.attribute_id = ?', $priceAttribute->getAttributeId())
9696
->where('t.value IS NOT NULL')
9797
->order('t.value ' . Select::SQL_ASC)
98+
->order(BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS . '.' . $linkField . ' ' . Select::SQL_ASC)
9899
->limit(1);
99100
$priceSelect = $this->baseSelectProcessor->process($priceSelect);
100101

app/code/Magento/Catalog/Model/ResourceModel/Product/LinkedProductSelectBuilderBySpecialPrice.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public function build($productId)
139139
'special_to.value IS NULL OR ' . $connection->getDatePartSql('special_to.value') .' >= ?',
140140
$currentDate
141141
)->order('t.value ' . Select::SQL_ASC)
142+
->order(BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS . '.' . $linkField . ' ' . Select::SQL_ASC)
142143
->limit(1);
143144
$specialPrice = $this->baseSelectProcessor->process($specialPrice);
144145

app/code/Magento/Catalog/Model/ResourceModel/Product/LinkedProductSelectBuilderByTierPrice.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public function build($productId)
9797
->where('t.all_groups = 1 OR customer_group_id = ?', $this->customerSession->getCustomerGroupId())
9898
->where('t.qty = ?', 1)
9999
->order('t.value ' . Select::SQL_ASC)
100+
->order(BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS . '.' . $linkField . ' ' . Select::SQL_ASC)
100101
->limit(1);
101102
$priceSelect = $this->baseSelectProcessor->process($priceSelect);
102103

app/code/Magento/CatalogRule/Model/ResourceModel/Product/LinkedProductSelectBuilderByCatalogRulePrice.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public function build($productId)
105105
->where('t.customer_group_id = ?', $this->customerSession->getCustomerGroupId())
106106
->where('t.rule_date = ?', $currentDate)
107107
->order('t.rule_price ' . Select::SQL_ASC)
108+
->order(BaseSelectProcessorInterface::PRODUCT_TABLE_ALIAS . '.' . $linkField . ' ' . Select::SQL_ASC)
108109
->limit(1);
109110
$priceSelect = $this->baseSelectProcessor->process($priceSelect);
110111

app/code/Magento/Security/Model/AdminSessionsManager.php

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ class AdminSessionsManager
5858
*/
5959
private $remoteAddress;
6060

61+
/**
62+
* Max lifetime for session prolong to be valid (sec)
63+
*
64+
* Means that after session was prolonged
65+
* all other prolongs will be ignored within this period
66+
*/
67+
private $maxIntervalBetweenConsecutiveProlongs = 60;
68+
6169
/**
6270
* @param ConfigInterface $securityConfig
6371
* @param \Magento\Backend\Model\Auth\Session $authSession
@@ -114,11 +122,16 @@ public function processLogin()
114122
*/
115123
public function processProlong()
116124
{
117-
$this->getCurrentSession()->setData(
118-
'updated_at',
119-
$this->authSession->getUpdatedAt()
120-
);
121-
$this->getCurrentSession()->save();
125+
if ($this->lastProlongIsOldEnough()) {
126+
$this->getCurrentSession()->setData(
127+
'updated_at',
128+
date(
129+
\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT,
130+
$this->authSession->getUpdatedAt()
131+
)
132+
);
133+
$this->getCurrentSession()->save();
134+
}
122135

123136
return $this;
124137
}
@@ -279,4 +292,45 @@ protected function createAdminSessionInfoCollection()
279292
{
280293
return $this->adminSessionInfoCollectionFactory->create();
281294
}
295+
296+
/**
297+
* Calculates diff between now and last session updated_at
298+
* and decides whether new prolong must be triggered or not
299+
*
300+
* This is done to limit amount of session prolongs and updates to database
301+
* within some period of time - X
302+
* X - is calculated in getIntervalBetweenConsecutiveProlongs()
303+
*
304+
* @see getIntervalBetweenConsecutiveProlongs()
305+
* @return bool
306+
*/
307+
private function lastProlongIsOldEnough()
308+
{
309+
$lastProlongTimestamp = strtotime($this->getCurrentSession()->getUpdatedAt());
310+
$nowTimestamp = $this->authSession->getUpdatedAt();
311+
312+
$diff = $nowTimestamp - $lastProlongTimestamp;
313+
314+
return (float) $diff > $this->getIntervalBetweenConsecutiveProlongs();
315+
}
316+
317+
/**
318+
* Calculates lifetime for session prolong to be valid
319+
*
320+
* Calculation is based on admin session lifetime
321+
* Calculated result is in seconds and is in the interval
322+
* between 1 (including) and MAX_INTERVAL_BETWEEN_CONSECUTIVE_PROLONGS (including)
323+
*
324+
* @return float
325+
*/
326+
private function getIntervalBetweenConsecutiveProlongs()
327+
{
328+
return (float) max(
329+
1,
330+
min(
331+
log((float)$this->securityConfig->getAdminSessionLifetime()),
332+
$this->maxIntervalBetweenConsecutiveProlongs
333+
)
334+
);
335+
}
282336
}

app/code/Magento/Security/Test/Unit/Model/AdminSessionsManagerTest.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ public function setUp()
113113
'setIsOtherSessionsTerminated',
114114
'save',
115115
'getUserId',
116-
'getSessionId'
116+
'getSessionId',
117+
'getUpdatedAt'
117118
],
118119
[],
119120
'',
@@ -240,7 +241,8 @@ public function testProcessLogin()
240241
public function testProcessProlong()
241242
{
242243
$sessionId = 50;
243-
$updatedAt = '2015-12-31 23:59:59';
244+
$lastUpdatedAt = '2015-12-31 23:59:59';
245+
$newUpdatedAt = '2016-01-01 00:00:30';
244246

245247
$this->adminSessionInfoFactoryMock->expects($this->any())
246248
->method('create')
@@ -254,13 +256,21 @@ public function testProcessProlong()
254256
->method('load')
255257
->willReturnSelf();
256258

257-
$this->authSessionMock->expects($this->once())
259+
$this->currentSessionMock->expects($this->once())
260+
->method('getUpdatedAt')
261+
->willReturn($lastUpdatedAt);
262+
263+
$this->authSessionMock->expects($this->exactly(2))
258264
->method('getUpdatedAt')
259-
->willReturn($updatedAt);
265+
->willReturn(strtotime($newUpdatedAt));
266+
267+
$this->securityConfigMock->expects($this->once())
268+
->method('getAdminSessionLifetime')
269+
->willReturn(100);
260270

261271
$this->currentSessionMock->expects($this->once())
262272
->method('setData')
263-
->with('updated_at', $updatedAt)
273+
->with('updated_at', $newUpdatedAt)
264274
->willReturnSelf();
265275

266276
$this->currentSessionMock->expects($this->once())

dev/tests/integration/testsuite/Magento/Security/Model/Plugin/AuthSessionTest.php

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class AuthSessionTest extends \PHPUnit_Framework_TestCase
4747
*/
4848
private $sessionManager;
4949

50+
/**
51+
* @var \Magento\Security\Model\ConfigInterface
52+
*/
53+
protected $securityConfig;
54+
5055
/**
5156
* Set up
5257
*/
@@ -62,8 +67,9 @@ protected function setUp()
6267
$this->authSession = $this->objectManager->create(\Magento\Backend\Model\Auth\Session::class);
6368
$this->adminSessionInfo = $this->objectManager->create(\Magento\Security\Model\AdminSessionInfo::class);
6469
$this->auth->setAuthStorage($this->authSession);
65-
$this->adminSessionsManager = $this->objectManager->create(\Magento\Security\Model\AdminSessionsManager::class);
70+
$this->adminSessionsManager = $this->objectManager->get(\Magento\Security\Model\AdminSessionsManager::class);
6671
$this->dateTime = $this->objectManager->create(\Magento\Framework\Stdlib\DateTime::class);
72+
$this->securityConfig = $this->objectManager->create(\Magento\Security\Model\ConfigInterface::class);
6773
}
6874

6975
/**
@@ -81,7 +87,42 @@ protected function tearDown()
8187

8288
/**
8389
* Test of prolong user action
90+
* session manager will not trigger new prolong if previous prolong was less than X sec ago
91+
* X - is calculated based on current admin session lifetime
8492
*
93+
* @see \Magento\Security\Model\AdminSessionsManager::lastProlongIsOldEnough
94+
* @magentoDbIsolation enabled
95+
*/
96+
public function testConsecutiveProcessProlong()
97+
{
98+
$this->auth->login(
99+
\Magento\TestFramework\Bootstrap::ADMIN_NAME,
100+
\Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
101+
);
102+
$sessionId = $this->authSession->getSessionId();
103+
$prolongsDiff = log($this->securityConfig->getAdminSessionLifetime()) - 2; // X from comment above
104+
$dateInPast = $this->dateTime->formatDate($this->authSession->getUpdatedAt() - $prolongsDiff);
105+
$this->adminSessionsManager->getCurrentSession()
106+
->setData(
107+
'updated_at',
108+
$dateInPast
109+
)
110+
->save();
111+
$this->adminSessionInfo->load($sessionId, 'session_id');
112+
$oldUpdatedAt = $this->adminSessionInfo->getUpdatedAt();
113+
$this->authSession->prolong();
114+
$this->adminSessionInfo->load($sessionId, 'session_id');
115+
$updatedAt = $this->adminSessionInfo->getUpdatedAt();
116+
117+
$this->assertSame(strtotime($oldUpdatedAt), strtotime($updatedAt));
118+
}
119+
120+
/**
121+
* Test of prolong user action
122+
* session manager will trigger new prolong if previous prolong was more than X sec ago
123+
* X - is calculated based on current admin session lifetime
124+
*
125+
* @see \Magento\Security\Model\AdminSessionsManager::lastProlongIsOldEnough
85126
* @magentoDbIsolation enabled
86127
*/
87128
public function testProcessProlong()
@@ -91,7 +132,8 @@ public function testProcessProlong()
91132
\Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
92133
);
93134
$sessionId = $this->authSession->getSessionId();
94-
$dateInPast = $this->dateTime->formatDate($this->authSession->getUpdatedAt() - 100);
135+
$prolongsDiff = log($this->securityConfig->getAdminSessionLifetime()) + 2; // X from comment above
136+
$dateInPast = $this->dateTime->formatDate($this->authSession->getUpdatedAt() - $prolongsDiff);
95137
$this->adminSessionsManager->getCurrentSession()
96138
->setData(
97139
'updated_at',
@@ -103,6 +145,7 @@ public function testProcessProlong()
103145
$this->authSession->prolong();
104146
$this->adminSessionInfo->load($sessionId, 'session_id');
105147
$updatedAt = $this->adminSessionInfo->getUpdatedAt();
106-
$this->assertGreaterThan($oldUpdatedAt, $updatedAt);
148+
149+
$this->assertGreaterThan(strtotime($oldUpdatedAt), strtotime($updatedAt));
107150
}
108151
}

setup/performance-toolkit/benchmark.jmx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12870,6 +12870,9 @@ vars.put("admin_user", adminUser);
1287012870

1287112871
//Index of the current product from the cluster
1287212872
Random random = new Random();
12873+
if (${seedForRandom} > 0) {
12874+
random.setSeed(${seedForRandom} + ${__threadNum});
12875+
}
1287312876
int iterator = random.nextInt(clusterLength);
1287412877
if (iterator == 0) {
1287512878
iterator = 1;
@@ -32205,7 +32208,7 @@ vars.put("admin_user", adminUser);
3220532208
<stringProp name="RegexExtractor.regex">actions":\{"edit":\{"href":"(?:http|https):\\/\\/(.*?)\\/customer\\/index\\/edit\\/id\\/(\d+)\\/",</stringProp>
3220632209
<stringProp name="RegexExtractor.template">/customer/index/edit/id/$2$/</stringProp>
3220732210
<stringProp name="RegexExtractor.default"/>
32208-
<stringProp name="RegexExtractor.match_number">0</stringProp>
32211+
<stringProp name="RegexExtractor.match_number">1</stringProp>
3220932212
</RegexExtractor>
3221032213
<hashTree/>
3221132214
<ResponseAssertion guiclass="AssertionGui" testclass="ResponseAssertion" testname="Assert customer edit url" enabled="true">
@@ -34422,6 +34425,9 @@ vars.put("admin_user", adminUser);
3442234425

3442334426
//Index of the current product from the cluster
3442434427
Random random = new Random();
34428+
if (${seedForRandom} &gt; 0) {
34429+
random.setSeed(${seedForRandom} + ${__threadNum});
34430+
}
3442534431
int iterator = random.nextInt(clusterLength);
3442634432
if (iterator == 0) {
3442734433
iterator = 1;

0 commit comments

Comments
 (0)