magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.59k stars 9.32k forks source link

setup:upgrade is changing indexer mode to `save` from `schedule` slowing it down immensely #33386

Open ioweb-gr opened 3 years ago

ioweb-gr commented 3 years ago

I have a fairly large catalog on a website and I've noticed that sometimes setup:upgrade takes a very very long time to complete. More than 30 minutes. It's not always occuring which makes it hard to track but by carefully examining what is happening I noticed that the command is dropping and recreating the database triggers when the indexers are set on scheduled mode. However with the indexers on save mode, the backend takes a very long time to save products and especially categories.

So our only option on big catalogs is to use the indexers on schedule mode.

By examining further I went and forced a check on the mview_state table to see what script is changing the indexer mode during setup:upgrade and so the command threw an exception as expected when executing the following query

UPDATE `mview_state` SET `view_id` = ?, `mode` = ?, `status` = ?, `updated` = '2021-06-30 05:57:46', `version_id` = ? WHERE (state_id=5)
View.php:225, Magento\Framework\Mview\View->unsubscribe()
TriggerCleaner.php:63, Magento\Framework\Mview\TriggerCleaner->removeTriggers()
Installer.php:1726, Magento\Setup\Model\Installer->removeUnusedTriggers()
UpgradeCommand.php:145, Magento\Setup\Console\Command\UpgradeCommand->execute()
Command.php:255, Magento\Setup\Console\Command\UpgradeCommand->run()
Application.php:1009, Magento\Framework\Console\Cli->doRunCommand()
Application.php:273, Magento\Framework\Console\Cli->doRun()
Cli.php:115, Magento\Framework\Console\Cli->doRun()
Application.php:149, Magento\Framework\Console\Cli->run()
magento:36, {main}()

Then I was able to see that the functions \Magento\Framework\Mview\View::unsubscribe \Magento\Framework\Mview\View::subscribe

Are executed for all indexers regardless, which will trigger dropping and re-adding the database triggers.

However dropping a trigger and recreating in a fairly large database is taking a long time to complete causing significant downtime.

It seems I'm not the only one facing this issue https://magento.stackexchange.com/questions/330245/magento-2-indexers-sometimes-switchon-their-own-to-update-on-save-when-config

Preconditions (*)

  1. 2.4.1

Steps to reproduce (*)

  1. A fairly large catalog with lots of categories (over 5000) and products (over 60k with over 200 attributes)
  2. The indexers set on schedule mode.
  3. Execute setup:upgrade

Expected result (*)

  1. I expect that setup:upgrade finishes fast and doesn't recreate the triggers if it's not needed. If the trigger exists, it shouldn't drop and recreate it. Only If it does not exist should it create it.
  2. It should never change the mode from schedule to save. The user has put it for a reason there

Actual result (*)

  1. setup:upgrade is always recreating the triggers causing huge downtime during the command execution
  2. sometimes the indexers never revert back to the scheduled mode and stay on save mode

Additional Information


Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

m2-assistant[bot] commented 3 years ago

Hi @ioweb-gr. Thank you for your report. To help us process this issue please make sure that you provided the following information:

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

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

For more details, please, review the Magento Contributor Assistant documentation.

Please, add a comment to assign the issue: @magento I am working on this


: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, please join the Community Contributions Triage session to discuss the appropriate ticket.

:movie_camera: You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

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

ioweb-gr commented 3 years ago

I'm not able to test this on the demo instance.

m2-assistant[bot] commented 3 years ago

Hi @engcom-November. 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:

ioweb-gr commented 3 years ago

Let me add more info here. I tracked down this post

https://maxchadwick.xyz/blog/lessons-learned-during-a-recent-magento-2-deploy

It seems that during operations which lock the tables, dropping the trigger or creating it has to actually wait for all database locks to disappear.

Looking further into the process I noticed that in my case it was trying to build the catalog price replica tables for the indexes which were locking the tables like this


 DELETE FROM `catalogrule_product_price_replica` WHERE (product_id NOT IN ((SELECT `crp`.`product_id` FROM `catalogrule_product_price`

Causing a lot of queries to wait during setup:upgrade making the downtime extremely high. Unfortunately even if you build the files in a different server the setup:upgrade procedure has to run on the production.

This should never ever happen and cause such unpredictable downtimes

This is what the end result in mytop looks like in such cases

image

onlinebizsoft commented 3 years ago

I think I have same issue. Will track this behavior and see

ioweb-gr commented 3 years ago

I've seen it happen in 3 installations on 3 different servers and now before setup:upgrade I make sure all indexers are green to avoid the issue. I will report back if this stops occuring like this but it would be great if it can be verified

ioweb-gr commented 3 years ago

Just an update, while following this practice the downtimes have reduced significantly because the locks are staying for a smaller period. The issue still persists but is minified like this.

Basically I can verify that if the indexers are running on scheduled mode, the issue can happen with setup:upgrade if they have items in the backlog

m2-assistant[bot] commented 3 years ago

Hi @engcom-Delta. 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:

engcom-Delta commented 3 years ago

Hi @ioweb-gr
it works fine on Magento 2.4-develop instance, I would request to try once again & confirm .Hence added the label ''Needs update'

Thanks

ioweb-gr commented 3 years ago

Hi, what do you mean it's working fine?

Let's take it one step at a time.

Did you verify that during setup upgrade triggers are dropped and recreated?

engcom-Delta commented 3 years ago

Hi @ioweb-gr , We could not notice that during setup upgrade triggers are dropped & recreated. Thanks

ioweb-gr commented 3 years ago

@engcom-Delta

I see. Here's how to guarantee that you'll see triggers are dropped and recreated.

Step 1: Put all indexers in scheduled mode. Step 2: Modify database table mview_state and add the following check

CHECK (mode = 'enabled')

Step 3: Execute setup upgrade

How does this guarantee you'll notice the issue?

First of all when indexers change mode from save to schedule and vice versa, triggers are created and dropped in order for the indexers to function.

By adding the above check, when setup:upgrade executes it's going to throw an exception because of a DB level constraint.

At that point you'll know for sure, that the mode on the indexer was changed from scheduled to save and that the setup:upgrade command tried to drop / recreate the triggers.

Could you give it a try?

m2-assistant[bot] commented 3 years ago

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:

engcom-Hotel commented 2 years ago

Hello @ioweb-gr,

We have tried to reproduce the issue in Magento 2.4-develop branch with the following data:

But for us the setup:upgrade not taking that much time. We have recorded a video related to that. Please have a look and let us know if we have missed anything:

https://user-images.githubusercontent.com/51681618/147472638-931ed215-9988-4b1a-b1a9-0ded96009612.mp4

Thanks

ioweb-gr commented 2 years ago

Hi, that only means that you cannot simulate real conditions this way.

On our client's store we have around 60k products, 500 orders daily, 50-150 concurrent customers browsing the website at all times, 5 people doing data entry, multiple catalog price rules, 5 sources, 11 stocks etc etc.

The indexers are constantly having items on queue to reindex and while it's not always happening that setup:upgrade is extremely slow I have seen cases where it will take a long time (even 15minutes) contrary to the normal ones when it needs a few seconds.

Usually the queries running in the backend as showcased by mytop are related to MSI indexers running for stock or catalog rule pricing ones in our specific case.

The best way I found to replicate this is to actually have a reindexing process running on an amount of data, that will lock the tables and then try to do a setup:upgrade. Because the lock exists, the process will hang for an indefinite period of time until the lock is released.

But in spite of this side-effect, the underlying issue is still puzzling me.

If I've decided to set my indexers to schedule mode. Why would they need to change to work on save and then back to schedule during setup:upgrade? There's nothing to gain by that, it only adds time to the setup:upgrade process.

hostep commented 2 years ago

@kandy: maybe it helps if a senior developer could have a look here? The engcom squad seems to not understand this problem because I think they miss some deeper knowledge about this sort of complicated functionality of Magento.

joeshelton-wagento commented 2 years ago

Same issue here on a busy store.

kadirakinkorkunc commented 2 years ago

Any solution? @joeshelton-wagento @ioweb-gr

ioweb-gr commented 2 years ago

Not really, expecting Magento to confirm this is happening at the very least so a few more voices might help

sdzhepa commented 2 years ago

After an internal discussion and based on multiple complaints we confirmed this issue. For such type of issue is not trivial to describe simple(or end-user) steps to reproduce.

Also, it is hard to predict final performance results. Theoretically, numbers should be better So, as Acceptance Criteria for this issue should be performance measurement before and after

Suggested approach

  1. Check triggers declaration -> update only if it changes
  2. Check referenced tables from the trigger body -> update trigger if table structure was changed

cc: @ioweb-gr @hostep cc2: @engcom-Hotel @kandy @sidolov @akaplya

github-jira-sync-bot commented 2 years ago

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

m2-assistant[bot] commented 2 years ago

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

abukatar commented 2 years ago

Started work analysis for AC-2423

sdzhepa commented 2 years ago

Hello

Due to questions in slack channels about the status of this issue, I think it is better to provide answers here. This post is based on information that I found in several Jira tickets related to this issue and reflects the current status.

  1. Development/fix for this is going on in ACP2E-963 this internal ticket. So after merge related commits can be found by this ticket id. like https://github.com/magento/magento2/search?q=ACP2E-963&type=commits

  2. As I can see initial development(fix) and testing were already done. Ticket in the preparation for delivery stage. I think it will be merged in 2.4-develop soon

  3. The target release version for this fix is 2.4.6

cc: @coderimus

sdzhepa commented 2 years ago

The issue has been fixed by the Adobe team in the scope of ACP2E-963 Jira ticket Related commits: https://github.com/magento/magento2/search?q=ACP2E-963&type=commits

lauraseidler commented 1 year ago

@sdzhepa is the target release for this fix still 2.4.6 (aka, not before March)?

sdzhepa commented 1 year ago

@lauraseidler

According to Jira ticket, ACP2E-963 Target release is 2.4.6

jonathanribas commented 1 year ago

Hi guys, thanks for this fix!

We were struggling with this bug.

Any chance to be released in a Quality Patch before 2.4.6 release?

It would help a lot of merchants!

ioweb-gr commented 1 year ago

Any chance to be released in a Quality Patch before 2.4.6 release?

Indeed, just yesterday I had to wait for 35 minutes for Magento to finish running setup:upgrade due to dropping and readding the triggers. It was painful and the downtime cost a lot of money to the merchant. A quality patch would be awesome

jonathanribas commented 1 year ago

We have created a patch with the check statements part of ACP2E-963 on 2.4.5-p1 but unfortunately it's not solving the issue, at each deployment, triggers are created again ...

ACP2E-963.patch

willbrammer commented 1 year ago

Appear to be replicating on 2.4.3 as well - just to add

Nuranto commented 1 year ago

We are still experiencing this issue on 2.4.6.

see also https://github.com/magento/magento2/issues/19220

ioweb-gr commented 1 year ago

@sdzhepa we're also still facing this, could you reopen please?

engcom-Hotel commented 1 year ago

We are reopening this issue for further analysis.

m2-assistant[bot] commented 1 year ago

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:

engcom-Hotel commented 1 year ago

Hello @ioweb-gr,

We have rechecked this issue and it seems the triggers are still dropping and creating on each bin/magento setup:upgrade command. In order to reproduce the issue, we have followed the below steps:

  1. Enable the db.log via the following command: bin/magento dev:query-log:enable

  2. Debug the subscribe and unsubscribe methods from the below class: https://github.com/magento/magento2/blob/adc4105fcfbeee29d534482d8c6d9c5c1a193a0c/lib/internal/Magento/Framework/Mview/View.php#L1

  3. Run the bin/magento setup:upgrade command

  4. We can see in the below screenshot of db.log triggers are dropping and creating always: Dropping Triggers image

Creating Triggers image

In the case of a huge catalog and categories, it might happen that the bin/magento setup:upgrade command will take time. Hence confirming the issue.

Thanks

github-jira-sync-bot commented 1 year ago

:x: Cannot export the issue. This GitHub issue is already linked to Jira issue(s): https://jira.corp.adobe.com/browse/AC-2423

sanderjongsma commented 1 year ago

@engcom-Hotel is there any progress on this issue?

ioweb-gr commented 1 year ago

@engcom-Hotel let me also add one more relation here to the issue #36667 because when the queries for the related / upsell / cross-sell blocks run as well and are in a stuck state (in sending data phase) , setup:upgrade won't be able to progress either as in order to drop and readd the triggers it needs to take an exclusive lock on the tables leading to an indefinite time of processing.

engcom-Hotel commented 1 year ago

Hello,

As I can see this issue was fixed in the scope of the internal Jira ticket ACP2E-963 by the internal team Related commits: https://github.com/search?q=repo%3Amagento%2Fmagento2+ACP2E-963&type=commits

Based on the Jira ticket, the target version is 2.4.6.

Thanks

ioweb-gr commented 1 year ago

@engcom-Hotel we are still seeing this in 2.4.6

sanderjongsma commented 1 year ago

I can confirm it is still happening in 2.4.6

engcom-Hotel commented 1 year ago

Let me reopen it for further investigation.

Alexander-Khohklov commented 1 year ago

@magento I am working on this

engcom-Hotel commented 1 year ago

According to this comment https://github.com/magento/magento2/issues/33386#issuecomment-1043290448 and multiple complaints related to this issue, we are reconfirming this issue.

Thanks

github-jira-sync-bot commented 1 year ago

:x: Cannot export the issue. This GitHub issue is already linked to Jira issue(s): https://jira.corp.adobe.com/browse/AC-2423

MaikJaek commented 8 months ago

According to the release notes, this issue has been solved with Magento 2.4.6.:

image
ioweb-gr commented 8 months ago

Apparently it's not working properly though

bhouillon commented 3 months ago

Hi, Is there a quality patch already existing for this issue ? We are facing it on 2.4.7-p2

jonathanribas commented 3 months ago

Hi, in order to battle this core issue, we use the following module for a while now without any issue: https://github.com/pykettk/module-indexer-deploy-config