Skip to content
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

Merged
merged 11 commits into from
Nov 14, 2020

Conversation

engcom-Echo
Copy link
Contributor

Description (*)

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes Complex products children ids are not cached in Type class leading to multiple unnecessary db requests #30585

Manual testing scenarios (*)

  1. See: Complex products children ids are not cached in Type class leading to multiple unnecessary db requests #30585

Questions or comments

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

@m2-assistant
Copy link

m2-assistant bot commented Nov 2, 2020

Hi @engcom-Echo. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names. Allowed build names are:

  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE,
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests

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.

⚠️ According to the Magento Contribution requirements, all Pull Requests must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 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"/>
Copy link
Contributor

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.

@ghost ghost assigned viktorpetryk Nov 2, 2020
@ghost ghost added this to Changes Requested in Pull Requests Dashboard Nov 2, 2020
@ghost ghost removed this from Pending Review in High Priority Pull Requests Dashboard Nov 2, 2020
@engcom-Echo engcom-Echo moved this from Changes Requested to Review in Progress in Pull Requests Dashboard Nov 2, 2020
@engcom-Echo engcom-Echo added the Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. label Nov 2, 2020
@ghost ghost added this to Changes Requested in High Priority Pull Requests Dashboard Nov 2, 2020
@ghost ghost removed this from Review in Progress in Pull Requests Dashboard Nov 2, 2020
@engcom-Echo engcom-Echo moved this from Changes Requested to Review in Progress in High Priority Pull Requests Dashboard Nov 2, 2020
@ghost ghost added Progress: review Severity: S1 Affects critical data or functionality and forces users to employ a workaround. and removed Progress: needs update labels Nov 2, 2020
Copy link
Contributor

@viktorpetryk viktorpetryk left a 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) {

Copy link
Contributor

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);
Copy link
Contributor

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"/>
Copy link
Contributor

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'])
Copy link
Contributor

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.

@ghost ghost moved this from Review in Progress to Changes Requested in High Priority Pull Requests Dashboard Nov 2, 2020
@engcom-Echo
Copy link
Contributor Author

@victorpetryk

Also, could you check behavior with API requests and described methods call, as we have two identical plugins for different areas - frontend and global?

These are plugin is not identical.
Magento\Bundle\Model\Plugin\Product - used getParentIdsByChild
Magento\Bundle\Model\Plugin\Frontend\ProductIdentitiesExtender - used getChildrenIds

@@ -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;
Copy link
Contributor

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

@engcom-Echo
Copy link
Contributor Author

@magento run all tests

* @param mixed $entityId
* @return array
*/
private function getChildrenIds($entityId)
Copy link
Contributor

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
Copy link
Contributor

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;
Copy link
Contributor

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)
Copy link
Contributor

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 */
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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();
Copy link
Contributor

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

Copy link
Contributor Author

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

@m2-community-project m2-community-project bot moved this from Changes Requested to Review in Progress in High Priority Pull Requests Dashboard Nov 9, 2020
@engcom-Echo
Copy link
Contributor Author

@magento run all tests

@engcom-Kilo
Copy link
Contributor

@magento import pull request to https://github.com/magento-tsg/magento2ce

@magento-engcom-team
Copy link
Contributor

@engcom-Kilo the pull request successfully imported.

@magento-engcom-team magento-engcom-team merged commit b18a387 into magento:2.4-develop Nov 14, 2020
@m2-assistant
Copy link

m2-assistant bot commented Nov 14, 2020

Hi @engcom-Echo, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Bundle Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. Release Line: 2.4 Severity: S1 Affects critical data or functionality and forces users to employ a workaround.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Complex products children ids are not cached in Type class leading to multiple unnecessary db requests
6 participants