Skip to content

Commit 7257f46

Browse files
committed
Merge remote-tracking branch 'origin/ACP2E-3995' into PR_2025_07_18_chittima
2 parents afa01c8 + 1605ca8 commit 7257f46

File tree

3 files changed

+80
-17
lines changed

3 files changed

+80
-17
lines changed

app/code/Magento/Indexer/etc/crontab.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2011 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
@@ -14,7 +14,7 @@
1414
<schedule>* * * * *</schedule>
1515
</job>
1616
<job name="indexer_clean_all_changelogs" instance="Magento\Indexer\Cron\ClearChangelog" method="execute">
17-
<schedule>0 * * * *</schedule>
17+
<schedule>*/5 * * * *</schedule>
1818
</job>
1919
</group>
2020
</config>

dev/tests/integration/testsuite/Magento/Framework/Mview/View/ChangelogTest.php

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2014 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Framework\Mview\View;
77

@@ -107,21 +107,72 @@ public function testGetVersion()
107107
}
108108

109109
/**
110-
* Test for clear() method
111-
*
112110
* @return void
113111
* @throws ChangelogTableNotExistsException
114-
* @throws \Magento\Framework\Exception\RuntimeException
115112
*/
116-
public function testClear()
113+
public function testChangelogClearOversizeBatchSize()
117114
{
118-
$this->assertEquals(0, $this->model->getVersion());
119-
//the same that a table is empty
120-
$changelogName = $this->resource->getTableName($this->model->getName());
121-
$this->connection->insert($changelogName, ['version_id' => 1, 'entity_id' => 1]);
122-
$this->assertEquals(1, $this->model->getVersion());
123-
$this->model->clear(1);
124-
$this->assertEquals(1, $this->model->getVersion()); //the same that a table is empty
115+
$stateVersionId = 10100;
116+
$changelog = $this->generateChangelog(20000);
117+
118+
// #0: check if changelog generated correctly
119+
$this->assertEquals(1, $this->getChangelogFirstEntityId($changelog));
120+
$this->assertEquals(20000, $this->getChangelogEntitiesCount($changelog));
121+
122+
// #1: check if changelog reduced by batch size value
123+
$this->model->clear($stateVersionId);
124+
$this->assertEquals(10001, $this->getChangelogFirstEntityId($changelog));
125+
$this->assertEquals(10000, $this->getChangelogEntitiesCount($changelog));
126+
127+
// #2: check if changelog reduced to the mview state version id
128+
$this->model->clear($stateVersionId);
129+
$this->assertEquals(10100, $this->getChangelogFirstEntityId($changelog));
130+
$this->assertEquals(9901, $this->getChangelogEntitiesCount($changelog));
131+
132+
// #3: check if changelog stays the same size and values on the next iteration
133+
$this->model->clear($stateVersionId);
134+
$this->assertEquals(10100, $this->getChangelogFirstEntityId($changelog));
135+
$this->assertEquals(9901, $this->getChangelogEntitiesCount($changelog));
136+
}
137+
138+
/**
139+
* @param string $changelog
140+
* @return int
141+
*/
142+
private function getChangelogFirstEntityId(string $changelog): int
143+
{
144+
return (int) $this->connection->fetchOne($this->connection->select()->from($changelog, 'version_id'));
145+
}
146+
147+
/**
148+
* @param string $changelog
149+
* @return int
150+
*/
151+
private function getChangelogEntitiesCount(string $changelog): int
152+
{
153+
return (int) $this->connection->fetchOne($this->connection->select()->from($changelog, 'COUNT(1)'));
154+
}
155+
156+
/**
157+
* @param int $entitiesCount
158+
* @return string
159+
*/
160+
private function generateChangelog(int $entitiesCount = 100): string
161+
{
162+
$data = [];
163+
$changelog = $this->resource->getTableName($this->model->getName());
164+
165+
for ($i = 1; $i <= $entitiesCount; $i++) {
166+
$data[$i]['version_id'] = $i;
167+
$data[$i]['entity_id'] = $i;
168+
}
169+
$this->connection->insertArray(
170+
$changelog,
171+
['version_id', 'entity_id'],
172+
$data
173+
);
174+
175+
return $changelog;
125176
}
126177

127178
/**

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class Changelog implements ChangelogInterface
3737
*/
3838
public const VERSION_ID_COLUMN_NAME = 'version_id';
3939

40+
/**
41+
* Batch size for changelog cleaning operation
42+
*/
43+
private const CHANGELOG_CLEAR_BATCH_SIZE = 10000;
44+
4045
/**
4146
* Database connection
4247
*
@@ -249,7 +254,14 @@ public function clear($versionId)
249254
throw new ChangelogTableNotExistsException(new Phrase("Table %1 does not exist", [$changelogTableName]));
250255
}
251256

252-
$this->connection->delete($changelogTableName, ['version_id < ?' => (int)$versionId]);
257+
$this->connection->query(
258+
sprintf(
259+
'DELETE FROM `%s` WHERE %s LIMIT %d',
260+
$changelogTableName,
261+
'version_id < ' . (int) $versionId,
262+
self::CHANGELOG_CLEAR_BATCH_SIZE
263+
)
264+
);
253265

254266
return true;
255267
}

0 commit comments

Comments
 (0)