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.57k stars 9.32k forks source link

When using file storage for the lock provider, we get an ever growing directory of files without any cleanup happening #39369

Open hostep opened 1 week ago

hostep commented 1 week ago

Preconditions and environment

Steps to reproduce

  1. Setup clean Magento with sample data
  2. Make sure cronjobs are running
  3. From the root of Magento, execute these commands:
    $ mkdir "$(pwd)/var/locks"
    $ bin/magento setup:config:set --lock-provider=file --lock-file-path="$(pwd)/var/locks"
    $ bin/magento cache:enable
    $ bin/magento cache:flush
  4. Start visiting the frontend, please visit many different pages: homepage, category pages, product detail pages, ...
  5. Also create some orders
  6. Look at the contents of the var/locks directory

Ideally you should look at a shop running for many months and where file based locking has been enabled, and inspect the directory that contains those lock files.

Expected result

Actual result

Example filenames:

- BLOCK_833608638be7d42c9a8edf79b3516f6768babb37b07cb0c924ce1ede8c7b60f4-122217-final_price-EUR-20241114-1-0-
- BLOCK_8c40abe5c67c542dd31f6ed0787db94f9b6c7add8c7eccb4aeff4dc1adb10d9d-122217
- BLOCK_833608638be7d42c9a8edf79b3516f6768babb37b07cb0c924ce1ede8c7b60f4-122209-final_price-EUR-20241114-1-0-
- BLOCK_8c40abe5c67c542dd31f6ed0787db94f9b6c7add8c7eccb4aeff4dc1adb10d9d-122209
- BLOCK_833608638be7d42c9a8edf79b3516f6768babb37b07cb0c924ce1ede8c7b60f4-122213-final_price-EUR-20241114-1-0-
- BLOCK_8c40abe5c67c542dd31f6ed0787db94f9b6c7add8c7eccb4aeff4dc1adb10d9d-122213
- BLOCK_833608638be7d42c9a8edf79b3516f6768babb37b07cb0c924ce1ede8c7b60f4-122194-final_price-EUR-20241114-1-0-
- BLOCK_0dececc6f6ab51b0dc125a1d40b7c4fd8dac44300acc9927a9bbc923f0dc4b1d-122201-final_price-EUR-20241114-1-0-
- BLOCK_fd38c4b3f09eb9f6357edad8fbb6fa37f447f4b29367471c1f63330cfd2e6041
- BLOCK_2b0ceeddda2b7376874129eed2893a53db19081fd2ff426fc9a63a3ad80125d3
- PLACE_ORDER_20194
- PLACE_ORDER_20197
- PLACE_ORDER_20199
- PLACE_ORDER_20201
- CRON_54ca823af2ab2bc8e0923548a617e1d9
- CRON_597b39571a8e2e02aa3da5b0b669058c
- CRON_5d6e389185b7c7ff8ada1b70e5d5beec
- CRON_5dc531608e6750f85d979bb963298bd3
- CRON_65418d1532274c42fbc28425e8b9b7f1
- CRON_65541ac6ef52e06c546be79471cf53ce
- indexer_lock_category_product
- SYSTEM_CONFIG

We had one project that had 3.5 million files in that directory, up to a point where the filesystem started complaining with errors like Failed to open stream: No space left on device every time a lock file was needed (there was plenty of disk space, it's just that a directory can't handle that large amount of files in one directory). Another project had 1.9 million files and another had ~170.000 files.

So that's really bad. We need some kind of bugfix or cleanup mechanism to keep this under control.

Additional information

The locking mechanism in Magento is used for various things:

Most of these things result in lock names that are predictable and remain the same over time:

However, some of these are pretty dynamic and those names can change over time:

More observations:

Possible solutions:

These are just some random ideas, in case other people have other suggestions, feel free to let me know in the comments below.

Temporary solution: Setup something like this in the crontab on your server:

# removes lock files that haven't been touched in 10 days
15 0 * * * find /path/to/your/lock/file/storage -type f -mtime +10 -delete

Thanks!

Release note

No response

Triage and priority

m2-assistant[bot] commented 1 week ago

Hi @hostep. 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.

hostep commented 1 week ago

Performed some statistics calculations across the projects we maintain ourselves.

These are the number of lock files found across the production versions of these shops, per Magento version. All our shops on Magento 2.4.7 got the temporary workaround to delete lock files if they haven't been touched in 10 days. Without this workaround the numbers would exceed 1 million.

Average:
- Magento 2.3.7 => 373
- Magento 2.4.5 => 129
- Magento 2.4.6 => 14144
- Magento 2.4.7 => 87441  (without temporary cleanup workaround it would be ~1.856.666)

Median:
- Magento 2.3.7 => 75
- Magento 2.4.5 => 129
- Magento 2.4.6 => 1642
- Magento 2.4.7 => 87450  (without temporary cleanup workaround it would be ~1.900.000)

So we can indeed see a slight increase of number of lock files used since Magento 2.4.6 which can be explained in a lock file per order being needed now. And a huge increase since Magento 2.4.7, since generating cache for Blocks need a lock file per Block now, instead of using the database for those locks.

hostep commented 1 week ago

Suggestion for the simple solution, to cleanup old lock files using a cronjob: https://github.com/magento/magento2/pull/39372

This is probably only a partial fix for this issue, it's a cleanup afterwards, maybe we can also already beforehand try to prevent writing too many files somehow...

m2-assistant[bot] commented 1 week 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 week ago

Hello @hostep,

Thanks for the report and collaboration!

The issue has been reproducible for us. Hence confirming this issue.

Thanks

github-jira-sync-bot commented 1 week ago

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

m2-assistant[bot] commented 1 week ago

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