magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

[Performance] ProductRepository cache issue

Open Nuranto opened this issue 1 year ago • 19 comments

Preconditions and environment

  • Magento version 2.4.6-p4

Steps to reproduce

$this->searchCriteriaBuilder->addFilter('entity_id', [1,2], 'in');
$searchCriteria = $this->searchCriteriaBuilder->create();
$this->productRepository->getList($searchCriteria);
  1. $this->productRepository->getById(1);

Expected result

getById method should use cache generated by getList

Actual result

Product with ID 1 is loaded twice. This is because the cache key is generated with storeId = null for getById, but with storeId = 1 (default store) for getList.

(Same thing with get instead of getById)

Additional information

get : https://github.com/magento/magento2/blob/488c103401f7b04bfea0a339d25107ada11bd2da/app/code/Magento/Catalog/Model/ProductRepository.php#L278-L280

getById : https://github.com/magento/magento2/blob/488c103401f7b04bfea0a339d25107ada11bd2da/app/code/Magento/Catalog/Model/ProductRepository.php#L308-L310

getList : https://github.com/magento/magento2/blob/488c103401f7b04bfea0a339d25107ada11bd2da/app/code/Magento/Catalog/Model/ProductRepository.php#L717-L727

Release note

No response

Triage and priority

  • [ ] Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • [ ] Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • [ ] Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • [X] Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • [ ] Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

Nuranto avatar Apr 03 '24 08:04 Nuranto

Hi @Nuranto. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:


Join Magento Community Engineering Slack and ask your questions in #github channel. :warning: According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting. :clock10: You can find the schedule on the Magento Community Calendar page. :telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

m2-assistant[bot] avatar Apr 03 '24 08:04 m2-assistant[bot]

Hi @engcom-Hotel. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
  • [ ] 3. Add Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
  • [ ] 4. Verify that the issue is reproducible on 2.4-develop branch
    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!
  • [ ] 5. Add label Issue: Confirmed once verification is complete.
  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

m2-assistant[bot] avatar Apr 03 '24 09:04 m2-assistant[bot]

Hello @Nuranto,

Thanks for the report and collaboration!

We have tried to reproduce the issue in the latest development branch i.e. 2.4-develop and we are able to reproduce the issue.

There are 2 cache keys have been generated for 2 calls below: First Call

$this->searchCriteriaBuilder->addFilter('entity_id', [1], 'in');
$searchCriteria = $this->searchCriteriaBuilder->create();
$this->productRepository->getList($searchCriteria);

Second Call

$this->productRepository->getById(1);

We have made a custom module to reproduce the issue. Please find attached the same for the reference:

Magz.zip

Hence confirming the issue.

Thanks

engcom-Hotel avatar Apr 03 '24 10:04 engcom-Hotel

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-11730 is successfully created for this GitHub issue.

github-jira-sync-bot avatar Apr 03 '24 10:04 github-jira-sync-bot

:white_check_mark: Confirmed by @engcom-Hotel. Thank you for verifying the issue.
Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

m2-assistant[bot] avatar Apr 03 '24 10:04 m2-assistant[bot]

@magento I am working on this

digitalrisedorset avatar Apr 15 '24 09:04 digitalrisedorset

Hi @digitalrisedorset! :wave: Thank you for collaboration. Only members of Community Contributors Team are allowed to be assigned to the issue. Please use @magento add to contributors team command to join Contributors team.

m2-assistant[bot] avatar Apr 15 '24 09:04 m2-assistant[bot]

@magento add to contributors team

digitalrisedorset avatar Apr 15 '24 09:04 digitalrisedorset

Hi @digitalrisedorset! :wave: Thank you for joining. Please accept team invitation :point_right: here :point_left: and add your comment one more time.

m2-assistant[bot] avatar Apr 15 '24 09:04 m2-assistant[bot]

@magento I am working on this

digitalrisedorset avatar Apr 15 '24 09:04 digitalrisedorset

@Nuranto thanks for raising this issue. I have been looking into a fix today. I have a way to ensure the cache is not duplicated and that is possibly a way forward. However, whilst I test the fix, I realise the issue you have found in fact hides another issue. I will describe the second issue for you to add comment whether I am missing something or not:

observations made during the test phase with this bug investigation

  • the getList method is a search method for the product repository
  • the getById method consists in loading the product using the product factory and the product resource model.

These 2 methods have a fundamental difference I believe:

  • getList can load some extension attributes if a file extension_attributes.xml is created and this file defines some attributes to autoload in the search feature
  • the product load method does not benefit from this mechanism. Instead, the extension attributes have to be aggregated to the product using plugins.

I have on my local (without adding any extension attribute) a situation where both methods loads different attributes.

The screenshot below shows the tier price data for instance are returned when the product load is called whilst the search does not return this data: Screenshot from 2024-04-15 12-38-44

conclusion from the observation

Because the product data for both methods getList and getById are different, it seems that we do not want the search and the product load to share the same cache hash as it would hide some data that we might need on the product page particularly

I am inclined in the end not to add a fix as we want the cache to be different? I'd agree with you that we have some code that is misleading somewhat at the moment. But the fix we are looking to do would genuinely give side-effects that would be tricky to troubleshoot

digitalrisedorset avatar Apr 15 '24 11:04 digitalrisedorset

I agree with you with the observation.

But in that case, the cache should be totally removed from getList method, because getById/get will use that cache if called with storeId parameter.

So the fix should probably be to remove the foreach from getList method. This will prevent issues for what you described, and also slightly improve performance of getList (probably not significant for most cases though...).

Nuranto avatar Apr 15 '24 12:04 Nuranto

@Nuranto I have made a commit today to remove the cache on the getList method as per your recommendation. With more thoughts on it, I see now that your idea is perfectly relevant to our situation. thanks for your input and apologies for not getting it right away in my head

digitalrisedorset avatar Apr 17 '24 06:04 digitalrisedorset

I added some integration tests for this PR

digitalrisedorset avatar Apr 23 '24 06:04 digitalrisedorset

Please also see https://github.com/magento/magento2/issues/38384

MaximGns avatar Apr 24 '24 23:04 MaximGns

@MaximGns I have added new changes as well as many more tests. Thank you for consolidating this issue

digitalrisedorset avatar Apr 25 '24 07:04 digitalrisedorset

@magento run all tests

digitalrisedorset avatar May 07 '24 13:05 digitalrisedorset

Hello @digitalrisedorset,

I need to tell you that you need to run the above command to its related PR i.e. https://github.com/magento/magento2/pull/38632

@magento run all tests

Let me run it for you.

Thanks

engcom-Hotel avatar May 07 '24 13:05 engcom-Hotel

Any updates on this PR?

MagePsycho avatar Jun 14 '25 11:06 MagePsycho