-
Notifications
You must be signed in to change notification settings - Fork 9.4k
MC-38593: Complex products children ids are not cached in Type class leading to multiple unnecessary db requests #30746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MC-38593: Complex products children ids are not cached in Type class leading to multiple unnecessary db requests #30746
Conversation
…leading to multiple unnecessary db requests
Hi @engcom-Echo. Thank you for your contribution
❗ Automated tests can be triggered manually with an appropriate comment:
You can find more information about the builds here ℹ️ Please run only needed test builds instead of all when developing. Please run all test builds before sending your PR for review. For more details, please, review the Magento Contributor Guide documentation. 🕙 You can find the schedule on the Magento Community Calendar page. 📞 The triage of Pull Requests happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket. 🎥 You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel ✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel |
@@ -14,6 +14,6 @@ | |||
</arguments> | |||
</type> | |||
<type name="Magento\Catalog\Model\Product"> | |||
<plugin name="bundle" type="Magento\Bundle\Model\Plugin\Frontend\Product" sortOrder="100" /> | |||
<plugin name="add_child_identities_bundle" type="Magento\Bundle\Model\Plugin\Frontend\ProductIdentitiesExtender"/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not remove sortOrder as this can change the order of the plugins execution for this class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you rename the plugin, please rename the tests as well
- app/code/Magento/Bundle/Test/Unit/Model/Plugin/Frontend/ProductTest.php
- dev/tests/integration/testsuite/Magento/Bundle/Model/Plugin/Frontend/ProductTest.php.
Could you take a look at \Magento\Bundle\Model\Plugin\Product::afterGetIdentities. It seems like we have the same performance issue with executing DB requests when getParentIdsByChild is executed several times.
Also, could you check behavior with API requests and described methods call, as we have two identical plugins for different areas - frontend and global?
Please check other plugins and models like \Magento\ConfigurableProduct\Model\Plugin\ProductIdentitiesExtender::afterGetIdentities for configurable and grouped products as they have the same ...type->getParentIdsByChild and ...type->getChildrenIds calls and it may need to implement caching as well.
@@ -37,12 +37,15 @@ public function __construct(Type $type) | |||
*/ | |||
public function afterGetIdentities(CatalogProduct $product, array $identities): array | |||
{ | |||
if ($product->getTypeId() !== BundleType::TYPE_CODE) { | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove an empty line here.
foreach ($this->type->getChildrenIds($product->getEntityId()) as $childIds) { | ||
foreach ($childIds as $childId) { | ||
$identities[] = CatalogProduct::CACHE_TAG . '_' . $childId; | ||
} | ||
} | ||
|
||
return array_unique($identities); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add an empty line here.
@@ -14,6 +14,6 @@ | |||
</arguments> | |||
</type> | |||
<type name="Magento\Catalog\Model\Product"> | |||
<plugin name="bundle" type="Magento\Bundle\Model\Plugin\Frontend\Product" sortOrder="100" /> | |||
<plugin name="add_child_identities_bundle" type="Magento\Bundle\Model\Plugin\Frontend\ProductIdentitiesExtender"/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would be better to rename it to add_bundle_child_identities?
@@ -173,7 +180,7 @@ protected function setUp(): void | |||
->disableOriginalConstructor() | |||
->getMockForAbstractClass(); | |||
$this->bundleModelSelection = $this->getMockBuilder(SelectionFactory::class) | |||
->setMethods(['create']) | |||
->setMethods(['create', 'getChildrenIds']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like 'getChildrenIds' is redundant in this case.
These are plugin is not identical. |
@@ -285,7 +290,12 @@ public function getRelationInfo() | |||
*/ | |||
public function getChildrenIds($parentId, $required = true) | |||
{ | |||
return $this->_bundleSelection->getChildrenIds($parentId, $required); | |||
$cacheKey = $parentId . '-' . ($required ? 1 : 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK $parentId here can be array here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it isn't. In the annotation to the function, the specified parameter takes only "int".
* @param int $parentId
* @param bool $required
* @return array
*/
public function getChildrenIds($parentId, $required = true)
{
$cacheKey = $parentId . '-' . ($required ? 1 : 0);
…leading to multiple unnecessary db requests
@@ -296,7 +311,12 @@ public function getChildrenIds($parentId, $required = true) | |||
*/ | |||
public function getParentIdsByChild($childId) | |||
{ | |||
return $this->_bundleSelection->getParentIdsByChild($childId); | |||
$cacheKey = is_array($childId) ? implode('-', $childId) : $childId; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case of array is too big it can break because of lack of memory
Please test this solution for big amount of products
https://devdocs.magento.com/guides/v2.4/config-guide/cli/config-cli-subcommands-perf-data.html
@@ -364,7 +369,12 @@ public function getChildrenIds($parentId, $required = true) | |||
*/ | |||
public function getParentIdsByChild($childId) | |||
{ | |||
return $this->_catalogProductTypeConfigurable->getParentIdsByChild($childId); | |||
$cacheKey = is_array($childId) ? implode('-', $childId) : $childId; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case of array is too big it can break because of lack of memory
Please test this solution for big amount of products
https://devdocs.magento.com/guides/v2.4/config-guide/cli/config-cli-subcommands-perf-data.html
…leading to multiple unnecessary db requests
…leading to multiple unnecessary db requests
…leading to multiple unnecessary db requests
@magento run all tests |
* @param mixed $entityId | ||
* @return array | ||
*/ | ||
private function getChildrenIds($entityId) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a return type hint.
use Magento\Bundle\Model\Product\Type as BundleType; | ||
use Magento\Catalog\Model\Product as CatalogProduct; | ||
|
||
class ProductIdentitiesExtender |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add DocBlock for class.
foreach ($this->getParentIdsByChild($product->getEntityId()) as $parentId) { | ||
$identities[] = CatalogProduct::CACHE_TAG . '_' . $parentId; | ||
} | ||
return $identities; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add an empty line before.
* @param mixed $entityId | ||
* @return array | ||
*/ | ||
private function getParentIdsByChild($entityId) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a return type hint.
use Magento\Bundle\Model\Product\Type; | ||
use Magento\Catalog\Model\Product; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ProductTest extends TestCase | ||
class ProductIdentitiesExtenderTest extends TestCase | ||
{ | ||
/** @var \Magento\Bundle\Model\Plugin\Product */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please correct @var tag
use Magento\Bundle\Model\Product\Type; | ||
use Magento\Catalog\Model\Product; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ProductTest extends TestCase | ||
class ProductIdentitiesExtenderTest extends TestCase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add DocBlock for class.
use Magento\Catalog\Model\Product; | ||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ProductTest extends TestCase | ||
class ProductIdentitiesExtenderTest extends TestCase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add DocBlock for class.
$parentProduct = $this->productRepository->getById($parentId); | ||
$identities = array_merge($identities, $parentProduct->getIdentities()); | ||
$parentProductsIdentities[] = $parentProduct->getIdentities(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would be better to rename it to $parentProductIndentities
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is an array of Products where everyone has Identities
…leading to multiple unnecessary db requests
@magento run all tests |
@magento import pull request to https://github.com/magento-tsg/magento2ce |
@engcom-Kilo the pull request successfully imported. |
Hi @engcom-Echo, thank you for your contribution! |
Description (*)
Related Pull Requests
Fixed Issues (if relevant)
Manual testing scenarios (*)
Questions or comments
Contribution checklist (*)