Skip to content

Commit

Permalink
Make sure to lock records in the same order
Browse files Browse the repository at this point in the history
  • Loading branch information
korycins committed Apr 26, 2024
1 parent 4cd299b commit e1e4356
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
24 changes: 14 additions & 10 deletions saleor/discount/utils.py
Expand Up @@ -659,12 +659,15 @@ def mark_active_promotion_rules_as_dirty(channel_ids: Iterable[int]):
).values_list("id", flat=True)

with transaction.atomic():
_rules_to_update = list(
PromotionRule.objects.select_for_update(of=("self",)).filter(
id__in=rule_ids
)
rule_ids_to_update = list(
PromotionRule.objects.select_for_update(of=("self",))
.filter(id__in=rule_ids, variants_dirty=False)
.order_by("pk")
.values_list("id", flat=True)
)
PromotionRule.objects.filter(id__in=rule_ids_to_update).update(
variants_dirty=True
)
PromotionRule.objects.filter(id__in=rule_ids).update(variants_dirty=True)


def mark_promotion_rules_as_dirty(promotion_pks: Iterable[UUID]):
Expand All @@ -676,11 +679,12 @@ def mark_promotion_rules_as_dirty(promotion_pks: Iterable[UUID]):
if not promotion_pks:
return
with transaction.atomic():
_rules_to_update = list(
PromotionRule.objects.select_for_update(of=(["self"])).filter(
promotion_id__in=promotion_pks
)
rule_ids_to_update = list(
PromotionRule.objects.select_for_update(of=(["self"]))
.filter(promotion_id__in=promotion_pks, variants_dirty=False)
.order_by("pk")
.values_list("id", flat=True)
)
PromotionRule.objects.filter(promotion_id__in=promotion_pks).update(
PromotionRule.objects.filter(id__in=rule_ids_to_update).update(
variants_dirty=True
)
22 changes: 14 additions & 8 deletions saleor/product/tasks.py
Expand Up @@ -183,10 +183,15 @@ def update_variant_relations_for_active_promotion_rules_task():
existing_variant_relation + new_rule_to_variant_list
)
with transaction.atomic():
_promotion_rules = list(
PromotionRule.objects.select_for_update(of=("self",)).filter(pk__in=ids)
promotion_rule_ids = list(
PromotionRule.objects.select_for_update(of=("self",))
.filter(pk__in=ids, variants_dirty=True)
.order_by("pk")
.values_list("id", flat=True)
)
PromotionRule.objects.filter(pk__in=promotion_rule_ids).update(
variants_dirty=False
)
PromotionRule.objects.filter(pk__in=ids).update(variants_dirty=False)

mark_products_in_channels_as_dirty(channel_to_product_map, allow_replica=True)
update_variant_relations_for_active_promotion_rules_task.delay()
Expand Down Expand Up @@ -226,12 +231,13 @@ def recalculate_discounted_price_for_products_task():
).filter(id__in=products_ids)
update_discounted_prices_for_promotion(products, only_dirty_products=True)
with transaction.atomic():
_channel_listings = list(
ProductChannelListing.objects.select_for_update(of=("self",)).filter(
id__in=listing_ids
)
channel_listings_ids = list(
ProductChannelListing.objects.select_for_update(of=("self",))
.filter(id__in=listing_ids, discounted_price_dirty=True)
.order_by("pk")
.values_list("id", flat=True)
)
ProductChannelListing.objects.filter(id__in=listing_ids).update(
ProductChannelListing.objects.filter(id__in=channel_listings_ids).update(
discounted_price_dirty=False
)
recalculate_discounted_price_for_products_task.delay()
Expand Down
11 changes: 6 additions & 5 deletions saleor/product/utils/product.py
Expand Up @@ -123,11 +123,12 @@ def mark_products_in_channels_as_dirty(

if listing_ids_to_update:
with transaction.atomic():
_channel_listings = list(
ProductChannelListing.objects.select_for_update(of=("self",)).filter(
id__in=listing_ids_to_update
)
channel_listing_ids = list(
ProductChannelListing.objects.select_for_update(of=("self",))
.filter(id__in=listing_ids_to_update, discounted_price_dirty=False)
.order_by("pk")
.values_list("id", flat=True)
)
ProductChannelListing.objects.filter(id__in=listing_ids_to_update).update(
ProductChannelListing.objects.filter(id__in=channel_listing_ids).update(
discounted_price_dirty=True
)
12 changes: 9 additions & 3 deletions saleor/product/utils/variant_prices.py
Expand Up @@ -117,19 +117,25 @@ def _update_or_create_listings(
):
if changed_products_listings_to_update:
ProductChannelListing.objects.bulk_update(
changed_products_listings_to_update, ["discounted_price_amount"]
sorted(changed_products_listings_to_update, key=lambda listing: listing.id),
["discounted_price_amount"],
)
if changed_variants_listings_to_update:
ProductVariantChannelListing.objects.bulk_update(
changed_variants_listings_to_update, ["discounted_price_amount"]
sorted(changed_variants_listings_to_update, key=lambda listing: listing.id),
["discounted_price_amount"],
)
if changed_variant_listing_promotion_rule_to_create:
_create_variant_listing_promotion_rule(
changed_variant_listing_promotion_rule_to_create
)
if changed_variant_listing_promotion_rule_to_update:
VariantChannelListingPromotionRule.objects.bulk_update(
changed_variant_listing_promotion_rule_to_update, ["discount_amount"]
sorted(
changed_variant_listing_promotion_rule_to_update,
key=lambda listing: listing.id,
),
["discount_amount"],
)


Expand Down

0 comments on commit e1e4356

Please sign in to comment.