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

fix(medusa): Fix hanging inventory item migration script #3624

Merged
merged 9 commits into from
Mar 30, 2023

Conversation

pKorsholm
Copy link
Contributor

@pKorsholm pKorsholm commented Mar 29, 2023

What

  • Wrap the batch creation of inventory items with a single transaction pr batch rather than have them each be a separate transaction

How

  • Wrap each batch in a transaction
  • Update interfaces for stock locations and inventory services to support passing contexts

Why

  • Each variant in the batch would have it's own connection from the connection pool causing the migration to hang. Either running the migration sequentially or with a smaller batch size than the configured connection pool size would solve the issue (batch size would have to be n-1 where n is the size of the connectionpool). @adrien2p has theorized that the issue comes from concurrently creating a new transaction and overwhelming the connection pool. 100 transaction creating concurrently will try to get 100 connection from the pool which is higher than the number of available connections

Discussion point
How should the operation be wrapped in a transaction?
The solution for now wraps each batch in a transaction, meaning a partial migration is possible. However, It was also possible beforehand, but where it would causing a single variant to not be migrated, here a batch would fail.
A different take would be to wrap it all in a single transaction making the operation entirely atomic.
wdyt?

@changeset-bot
Copy link

changeset-bot bot commented Mar 29, 2023

🦋 Changeset detected

Latest commit: c98aa70

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 10 packages
Name Type
@medusajs/medusa Patch
@medusajs/admin-ui Patch
@medusajs/admin Patch
@medusajs/medusa-js Patch
medusa-payment-paypal Patch
medusa-payment-stripe Patch
medusa-plugin-restock-notification Patch
medusa-react Patch
@medusajs/medusa-oas-cli Patch
@medusajs/oas-github-ci Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Mar 29, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated
medusa-docs ⬜️ Ignored (Inspect) Mar 30, 2023 at 7:22AM (UTC)

@pKorsholm pKorsholm marked this pull request as ready for review March 29, 2023 11:12
@pKorsholm pKorsholm requested a review from a team as a code owner March 29, 2023 11:12
Copy link
Member

@adrien2p adrien2p left a comment

Choose a reason for hiding this comment

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

LGTM

@olivermrbl
Copy link
Contributor

@adrien2p did you have anything for the discussion point? If not, I will merge.

@adrien2p
Copy link
Member

One thing, since a bbatch can succeed and the next one fail, it should be taken i to account it the query. We might want to only migrate the variants that have not inventory items no? So that if you re run, it will restart where it failed

Cc @pKorsholm

@pKorsholm
Copy link
Contributor Author

@adrien2p I added a check to validate if product variant inventory items exist for the variant (i.e. attach has completed and not been reverted). I think that should suffice since, even if and I have no clue how, it fails to create the level but the attachement is persisted, levels can be created manually 😄

wdyt?

@pKorsholm pKorsholm force-pushed the fix/inventory-item-migration-script branch from 4d86863 to ec7ebed Compare March 29, 2023 20:04
@olivermrbl olivermrbl changed the title Fix(medusa): Fix hanging inventory item migration script fix(medusa): Fix hanging inventory item migration script Mar 30, 2023
@olivermrbl olivermrbl merged commit 999aeb1 into develop Mar 30, 2023
@olivermrbl olivermrbl deleted the fix/inventory-item-migration-script branch March 30, 2023 07:29
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.

3 participants