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

[Performance] Loading order through repository loads the order multiple times from DB. (ShippingBuilder) #36636

Closed
1 of 5 tasks
Nuranto opened this issue Dec 19, 2022 · 9 comments · Fixed by #36972
Closed
1 of 5 tasks
Assignees
Labels
Area: Framework Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: done Reported on 2.4.5-p1 Indicates original Magento version for the Issue report. Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Comments

@Nuranto
Copy link
Contributor

Nuranto commented Dec 19, 2022

Summary

Magento 2.4.5-p1

Loading order through repository loads the order three times from DB :

$this->orderRepository->get($orderId);

Loads the order from database, then calls setShippingAssignments() which calls TaxManagement::getShippingAssignmentBuilderDependency() which loads the order from database again.

https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/Sales/Model/Order/ShippingBuilder.php#L79

and

https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/Sales/Model/Order/ShippingAssignmentBuilder.php#L79

Examples

$this->orderRepository->get($orderId);

Steps to reproduce

  1. Login in frontend with the user which has atleast 1 order
  2. Enable DB log
  3. Add breakpoints in the below lines:
    $entity = $this->metadata->getNewInstance()->load($id);

$order = $this->orderFactory->create()->load($this->getOrderId());

$this->order = $this->orderFactory->create()->load($this->getOrderId());

4. Try to access My Account page
5. The function $this->orderFactory->create()->load($this->getOrderId()) is calling 3 times for order id 2. Below is the MySQL query fetched from db.log:

SELECT `sales_order`.* FROM `sales_order` WHERE (`sales_order`.`entity_id`='2')
SELECT `sales_order`.* FROM `sales_order` WHERE (`sales_order`.`entity_id`='2')
SELECT `sales_order`.* FROM `sales_order` WHERE (`sales_order`.`entity_id`='2')

Proposed solution

I guess line
$shippingAssignments->setOrderId($order->getEntityId());
should be replaced somehow by
$shippingAssignments->setOrder($order);

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.
  • 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 Nuranto added the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Dec 19, 2022
@m2-assistant
Copy link

m2-assistant bot commented Dec 19, 2022

Hi @Nuranto. Thank you for your report.
To speed up processing of this issue, make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

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:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


⚠️ According to the Magento Contribution requirements, all issues 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 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.

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@Nuranto Nuranto changed the title [Performance] Loading order through repository loads the order twice from DB. (ShippingBuilder) [Performance] Loading order through repository loads the order multiple times from DB. (ShippingBuilder) Dec 19, 2022
@engcom-Hotel engcom-Hotel added the Reported on 2.4.5-p1 Indicates original Magento version for the Issue report. label Dec 20, 2022
@engcom-Hotel engcom-Hotel self-assigned this Jan 4, 2023
@m2-assistant
Copy link

m2-assistant bot commented Jan 4, 2023

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: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • 3. Add Component: XXXXX label(s) to the ticket, indicating the components 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.

@engcom-Hotel
Copy link
Contributor

engcom-Hotel commented Jan 4, 2023

Hello @Nuranto,

Thanks for the report and collaboration!

We need some more information in order to reproduce the issue. As per my understanding, the method $this->getOrderId() is calling 3 times in the below function:

public function create()
{
$shipping = null;
if ($this->getOrderId()) {
$this->order = $this->orderFactory->create()->load($this->getOrderId());
if ($this->order->getEntityId()) {
/** @var ShippingInterface $shipping */
$shipping = $this->shippingFactory->create();
$shippingAddress = $this->order->getShippingAddress();
if ($shippingAddress) {
$shipping->setAddress($shippingAddress);
}
$shipping->setMethod($this->order->getShippingMethod());
$shipping->setTotal($this->getTotal());
}
}
return $shipping;
}

We can save the value of the output of $this->getOrderId() and use it wherever required. Am I on the same page? Or please let us know the steps to reproduce the issue.

Thanks

@engcom-Hotel engcom-Hotel added the Issue: needs update Additional information is require, waiting for response label Jan 4, 2023
@Nuranto
Copy link
Contributor Author

Nuranto commented Jan 4, 2023

Hello @engcom-Hotel,

I'm not sure we're on the same page. Calling $this->getOrderId() is not an issue to me.

Issue is when we call Magento\Sales\Model\OrderRepository::get():

public function get($id)
{
if (!$id) {
throw new InputException(__('An ID is needed. Set the ID and try again.'));
}
if (!isset($this->registry[$id])) {
/** @var OrderInterface $entity */
$entity = $this->metadata->getNewInstance()->load($id);
if (!$entity->getEntityId()) {
throw new NoSuchEntityException(
__("The entity that was requested doesn't exist. Verify the entity and try again.")
);
}
$this->setOrderTaxDetails($entity);
$this->setShippingAssignments($entity);

You can see on line 137 that the order is loaded from database.

But line 144 is calling those two lines :

$this->order = $this->orderFactory->create()->load($this->getOrderId());

$order = $this->orderFactory->create()->load($this->getOrderId());

Result is that when we call Magento\Sales\Model\OrderRepository::get(), the order gets loaded from database 3 times instead of 1. (that's actually more than 3 times, but I've created distinct issues for each concerned modules)

I think the best thing to do is to replace ->setOrderId($order->getEntityId()) by ->setOrder($order) here :

$shippingAssignments->setOrderId($order->getEntityId());

and refactor Magento\Sales\Model\Order\ShippingAssignmentBuilder accordingly :

  • Add class property order
  • Add setOrder($order) { $this->order = $order; return $this; } method
  • Refactor create method :
public function create()
    {
        $shippingAssignments = null;
        if (!$this->order && $this->getOrderId()) {
            // we can keep this logic to avoid breaking change
            $this->order = $this->orderFactory->create()->load($this->getOrderId());
        }
        if ($this->order) {
            /** @var ShippingAssignmentInterface $shippingAssignment */
            $shippingAssignment =  $this->shippingAssignmentFactory->create();

            $shipping = $this->shippingBuilderFactory->create();
            $shipping->setOrder($this->order); // We replace setOrderId by setOrder here
            $shippingAssignment->setShipping($shipping->create());
            $shippingAssignment->setItems($this->order->getItems());
            $shippingAssignment->setStockId($this->order->getStockId());
            //for now order has only one shipping assignment
            $shippingAssignments = [$shippingAssignment];
        }
        return $shippingAssignments;
    }

and finally refactor Magento\Sales\Model\Order\ShippingBuilder the same way.

@engcom-Hotel
Copy link
Contributor

Hello @Nuranto,

Thanks for the detailed explanation!

We have debugged the code via following the below steps:

  1. Login in frontend with the user which has atleast 1 order
  2. Enable DB log
  3. Add breakpoints in the below lines:
    $entity = $this->metadata->getNewInstance()->load($id);

$order = $this->orderFactory->create()->load($this->getOrderId());

$this->order = $this->orderFactory->create()->load($this->getOrderId());

4. Try to access My Account page
5. The function $this->orderFactory->create()->load($this->getOrderId()) is calling 3 times for order id 2. Below is the MySQL query fetched from db.log:

SELECT `sales_order`.* FROM `sales_order` WHERE (`sales_order`.`entity_id`='2')
SELECT `sales_order`.* FROM `sales_order` WHERE (`sales_order`.`entity_id`='2')
SELECT `sales_order`.* FROM `sales_order` WHERE (`sales_order`.`entity_id`='2')

Hence confirming the issue.

Thanks

@engcom-Hotel engcom-Hotel added Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Area: Framework labels Jan 9, 2023
@engcom-Hotel engcom-Hotel added Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Area: Framework and removed Issue: needs update Additional information is require, waiting for response Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Area: Framework labels Jan 9, 2023
@github-jira-sync-bot
Copy link

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

@m2-assistant
Copy link

m2-assistant bot commented Jan 13, 2023

✅ 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.

@o-iegorov
Copy link
Contributor

The internal team start working on. the issue

@o-iegorov o-iegorov self-assigned this Aug 15, 2023
@m2-assistant
Copy link

m2-assistant bot commented Aug 15, 2023

Hi @o-iegorov. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:

    1. Add/Edit Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
    1. 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!
    1. If the issue is not relevant or is not reproducible any more, feel free to close it.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Framework Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: done Reported on 2.4.5-p1 Indicates original Magento version for the Issue report. Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
4 participants