Repository navigation
[5.x] Fix catalog pricing saves failing on the queue mutex - #4360
sjcallender wants to merge 1 commit into
Conversation
|
I should add that we really only see the error on staging/production. Locally, it finishes too fast, so our test forces the issue. |
|
Thanks for the PR, we will review and give feedback soon. Thanks! |
|
Thanks @lukeholder. While we have hopes for you all restructuring how all this works, this PR stays closer to the current setup. It is AI-assisted output for sure, but human-guided in shape and thought. Hope there's something valuable in here to help alleviate our builds' issues. |
|
We ran this PR against a parallel bulk-import repro on 5.7.6 and wrote the results up in #4389. It removes the mutex exception and cuts the job count (8 jobs instead of 6,644 for 64 products). The saves still wait for each other, though: the compare-and-swap |
|
Hi all Thank you for the PR and reporting. We are currently looking into this and will share an update as soon as we can. Thanks! |
|
Hi all! Thank you for your patience on this one. We've put together a fix for the catalog pricing problems reported: #4418 In which we took some inspiration from this PR, so thank you for that! It stops product saves from holding the pricing queue lock for the rest of the save, queues one pricing job per save instead of one per variant, and guards price generation against the deadlocks. Would you be willing to try it with your project and let us know what you find? Any results are useful, whether the problems go away, stay the same, or something new turns up. To get this, change your "require": {
"craftcms/commerce": "dev-feature/5x-fix-catalog-pricing-locks as 5.7.7",
"...": "..."
}Then run If you'd like us to look into it with your project, you can send the details to support@craftcms.com, along with instructions on how to reproduce the issues. Thanks! |
This one has been nagging us for a long time. And it's about to bite us again on a new build, so we dove in deeper to offer what we see is a solid fix.
Specifically, the build pulls its products in from an ERP. Running the sync piles up so many catalog pricing jobs that the sync itself starts failing on the mutex. Pausing the queue is the only way to get through it. We see the same thing on another build whenever a lot of products get created at once.
Pausing the queue works for the console command, but we're importing off of webhooks, so the same saves will be running in a web request where we can't pause anything.
[edited]
#4123 was closed asking for a new ticket if it persisted. On 5.7.3 we still get 600 jobs for 600 saves.
This doesn't fix the database deadlocks in #4003 and #4141, which are in
generateCatalogPrices()rather than the queue table. Fewer jobs means fewer of them running at once, so it should reduce how often those happen.[/end edit]
Problem
N saves push N jobs against the one row they all merged into. All but a few reserve nothing, each taking the
catalogpricingqueuemutex on the way.The two sides of that mutex disagree:
reserveCatalogPricingQueueRow()0null_queueCatalogPricingIds()5throw new RuntimeExceptionSo a background job can fail a foreground save.
Changes
UPDATE ... SET reserved = true WHERE id = ? AND reserved = false, checking affected rowsttrWe first tried dropping the merge and inserting a row per save. That made the write path much faster, 0.26s against 7.0s for 1200 saves, but it put
generateCatalogPrices()on every row: 60 IDs in one call measured 0.025s against 0.081s as 60 single-ID calls. We kept the merge.We also tried claiming every pending row up front. Killing a worker mid-drain left 200 rows reserved with nothing to free them, so we kept claiming one row at a time.
Our changes don't include any schema change, or
SKIP LOCKEDorRETURNING.Verification
Postgres,
runQueueAutomaticallyoff, no workers running.Six processes, 100
createCatalogPricingJob()calls each with a distinct purchasable ID.Mutex contention. Holding
catalogpricingqueueand then saving throwsRuntimeException: Unable to acquire the catalog pricing queue mutex.on 5.7.2, succeeds patched.Lost updates. Compare-and-swap against an unguarded read-modify-write of the same row, six processes merging 30 IDs each:
Reservation. Eight processes against 400 seeded rows: 400 claims, 400 distinct, 0 double-claimed, 0 left unreserved.
Crash. A worker that is killed after claiming leaves just its 1 row reserved; others drain the rest, and the expired-reservation release recovers it.