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

[Adjustments] Reduce adjustments callbacks #7036

Merged

Conversation

Matt-Yorkley
Copy link
Contributor

@Matt-Yorkley Matt-Yorkley commented Mar 7, 2021

What? Why?

Removes the after_save callbacks in Spree::Adjustment that trigger expensive calls to order.update!, which in turn can trigger a number of other transactions and callbacks.

Some quick testing shows this cuts the number of transactions during checkout processing in half. Using production data and submitting a fairly simple order with 3 line items triggers over 1100 SQL hits, and order.update! appears 700 times in the backtraces for those queries/transactions. After this, the total SQL count is down to ~500. This should have a similar effect in various other parts of the app where adjustments are touched, like updating orders via admin (which is also a big pain point currently).

🎉

Note: target branch needs to be updated after #6858 is merged.

What should we test?

Order totals should be correct when editing an order or it's adjustments.

Release notes

Removed adjustment callbacks that call order.update!

Changelog Category: Technical changes

@Matt-Yorkley Matt-Yorkley self-assigned this Mar 7, 2021
@Matt-Yorkley Matt-Yorkley force-pushed the adjustments-callbacks branch 2 times, most recently from e0d9b0a to 09f731d Compare March 7, 2021 16:12
@Matt-Yorkley Matt-Yorkley changed the title [Adjustments] Reduce callbacks [Adjustments] Reduce adjustments callbacks Mar 7, 2021
@@ -20,6 +20,8 @@ def recreate_all_fees!
create_line_item_fees!
create_order_fees!
end

order.update!
Copy link
Contributor Author

@Matt-Yorkley Matt-Yorkley Mar 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling order.update! here (in #recreate_all_fees!) is useful, it means we can stop manually calling order.update! in various places where we call order.recreate_all_fees!, which simplifies things.

@@ -95,6 +95,7 @@ def process_return
credit.save

order.return if inventory_units.all?(&:returned?)
order.update!
Copy link
Contributor Author

@Matt-Yorkley Matt-Yorkley Mar 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return authorizations add an adjustment on the order (which was updating the order totals automatically via the callback). It needs to be called once after a return is processed, so we can just do it explicitly.

def self.without_callbacks
skip_callback :save, :after, :update_adjustable
skip_callback :destroy, :after, :update_adjustable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 😄

# during cart population, for both taxation and enterprise fees. This operation triggers a
# costly Spree::Order#update!, which only needs to be run once. We avoid this by disabling
# callbacks on Spree::Adjustment and then manually invoke Spree::Order#update! on success.
Spree::Adjustment.without_callbacks do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 😄

@Matt-Yorkley Matt-Yorkley marked this pull request as ready for review March 8, 2021 16:42
@Matt-Yorkley Matt-Yorkley force-pushed the adjustments-callbacks branch from 7a1db62 to 1b97e65 Compare March 9, 2021 12:37
@Matt-Yorkley
Copy link
Contributor Author

Not sure how indicative this is, but the build just finished in ~16 minutes total, and other builds seem to be taking ~24 minutes total...

Copy link
Contributor

@andrewpbrett andrewpbrett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty reasonable, and like it will definitely help with performance. It is a little surprising that we didn't end up with more failing tests that needed to be changed. I feel like the test coverage around order totals is pretty robust though, right? So the green build should give us a good degree of confidence here.

app/controllers/cart_controller.rb Show resolved Hide resolved
Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 😄

I'm wondering if we need some more specific testing notes. The risk is that we are forgetting about cases that need to update the order.

@filipefurtad0
Copy link
Contributor

Note: target branch needs to be updated after #6858 is merged.

Should #6858 be tested/merged before this PR?

@Matt-Yorkley
Copy link
Contributor Author

I'm wondering if we need some more specific testing notes.

Lets see... cart and checkout, then admin order edit (line items and adjustments sections), customer order edit (if the hub allows editing open orders), Bulk Order Management, any other places? I guess subs as well.

Should #6858 be tested/merged before this PR?

If we stage this PR it'll include #6858, so it should be fine. But yeah if we merge #6858 I'll update this PR to point at master.

@filipefurtad0
Copy link
Contributor

filipefurtad0 commented Mar 10, 2021

Ok, great - so let's stage this PR, and include the test cases from #6858 as well as those you just pointed out Matt, thanks 👍

@filipefurtad0 filipefurtad0 self-assigned this Mar 10, 2021
@filipefurtad0 filipefurtad0 added the pr-staged-fr staging.coopcircuits.fr label Mar 10, 2021
@lin-d-hop
Copy link
Contributor

🎉 Hooray for this one team :-D
🤞 we can get it in the next release!

@filipefurtad0 filipefurtad0 added pr-staged-au staging.openfoodnetwork.org.au and removed pr-staged-fr staging.coopcircuits.fr pr-staged-au staging.openfoodnetwork.org.au labels Mar 10, 2021
@filipefurtad0
Copy link
Contributor

filipefurtad0 commented Mar 10, 2021

Hey @Matt-Yorkley ,

I'm splitting the testing in two comments, to reflect the separate PRs. So this first go at this concerns the changes introduced by #6858.

Test cases on shipping fees

I think this PR does not really touch much zones/shipping categories, at least directly. Anyway, I thought it's probably a good idea to have a quick test to make sure that:

  1. Setting SM1 (Zone A) and SM2 (Zone B), customers with a billing address in Zone A can checkout (shopfront)
    i) choosing SM1 - OK
    ii) choosing SM2 - OK
    iii) respective shipment fees apply, for test cases i) and ii) - OK

  2. Setting an inclusive tax on the shipment fee, under /admin/tax_settings/edit and
    i) placing an order -> correctly calculates inclusive tax and includes it in the price - OK

3.a) While editing orders (for SM with per order calculators)
i) it is possible to change a shipping method - OK
ii) which updates the order total, accordingly - OK

3.b) While editing orders (for SM with per item/amount calculators)
i) it is possible to change a shipping method - OK
ii) which updates the order total, accordingly - OK

  1. CRUD operations on shipping methods - OK (other than some known issues)

This is looking pretty good - that's awesome 🎉
For the sake of keeping scope: Anything else which you feel should be tested, concerning #6858 @Matt-Yorkley ?

(To be continued, on what concerns #7036)

@Matt-Yorkley
Copy link
Contributor Author

Anything else which you feel should be tested, concerning #6858 @Matt-Yorkley ?

With #6858 it's mostly just that the shipping cost should be recorded correctly and show up on the order and in the totals as usual. I think it's pretty well covered by specs as well 👍

@filipefurtad0 filipefurtad0 added the pr-staged-uk staging.openfoodnetwork.org.uk label Mar 11, 2021
@filipefurtad0
Copy link
Contributor

Ok, thanks @Matt-Yorkley 👍

Continuing with testing this PR...

Observed no significant difference in the deployment time, in staging-UK:

Master:
image

This PR
image

Checked updating order totals with taxes and fees as:

as Customer, at

  • cart - OK
  • checkout - OK
  • order edit -OK

as admin, while

  • updating shipping fee: it works, but one needs to set "Associated Adjustment Closed" as "Yes"; only after doing so, changes take effect (this was already the case before this PR)
  • editing an order (order edit) - OK, but (and related to the point above), the shipping fee does not update by clicking "Update and Recalculate Fees"; this needs to be done explicitly by manually changing the fee (related to the previous point, see video below)
  • editing an order (bulk order management) - OK
  • placing a subscription - OK

Comparison between backoffice and frontoffice. The mismatch after clicking "Update and Recalculate Fees" relates to the shipment fee, which needs to be updated manually - I think this the expected behavior:

Peek 2021-03-11 09-40

I found this it was possible to update shipment fees, manually - is this the expected behavior @Matt-Yorkley? I'm not so clear on the "Associated Adjustment Closed" - is there some inception or documentation on this? (adding the feedback label)

Peek 2021-03-11 09-43

So I think we are good here. Moving to ready to go.

PS: It seems we do observe bug #5368 (shipping fee with inclusive tax) I've scratched this from the previous comment. Although the extra tax value does not end up on the order total.

@filipefurtad0 filipefurtad0 added feedback-needed and removed pr-staged-uk staging.openfoodnetwork.org.uk labels Mar 11, 2021
@Matt-Yorkley
Copy link
Contributor Author

the shipping fee does not update by clicking "Update and Recalculate Fees"; this needs to be done explicitly by manually changing the fee

So when changing an order's contents in admin, you have to explicitly update the shipping fee if you want it to be recalculated? Sounds pretty reasonable to me. There haven't been any specific changes made to that part of the form.

It works and all the totals are correct, right?

@filipefurtad0
Copy link
Contributor

It works and all the totals are correct, right?

Yes, totals match 👍

I'm wondering if the fact that it needs to be done explicitly does not contribute to the shipment of orders with incorrect shipping fees, after editing line items, but agree: this does not related to this PR.

@Matt-Yorkley Matt-Yorkley changed the base branch from adjustments-shipping-cost to master March 11, 2021 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants