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

Fixes #14081: Fix cached counters on delete for parent-child items #14131

Merged
merged 2 commits into from
Dec 12, 2023

Conversation

arthanson
Copy link
Collaborator

@arthanson arthanson commented Oct 27, 2023

Fixes: #14081

@jeremystretch this will cause one extra db call on delete to an object tracked for a cached counter. The other option would be to save/check the objects to a request-specific queue (like webhooks uses) this code would be less clear but would obviate the need for another DB call. As it is just on delete, not sure if the extra DB call is a problem here?

The root issue is when you have a parent-child relationship (like inventory items) using MPTT and you do bulk-delete on them you wind up getting three delete signals instead of two:

1 for the child object being deleted from the cascade delete of the parent
1 for the parent object
1 additional one for the child object (not cascade deleted)

As can be seen from below, all the params to post_delete are the same except for the instance and origin not matching for the child delete case, but this is needed if you aren't doing bulk_delete and just delete the parent item:

sender: <class 'dcim.models.device_components.InventoryItem'> instance: invc1 instance_pk: 40 origin: inv1 parent_pk: 88, parent_model: <class 'dcim.models.devices.Device'> kwargs: {'signal': <django.db.models.signals.ModelSignal object at 0x101ad9510>, 'using': 'default'}
sender: <class 'dcim.models.device_components.InventoryItem'> instance: inv1 instance_pk: 39 origin: inv1 parent_pk: 88, parent_model: <class 'dcim.models.devices.Device'> kwargs: {'signal': <django.db.models.signals.ModelSignal object at 0x101ad9510>, 'using': 'default'}
sender: <class 'dcim.models.device_components.InventoryItem'> instance: invc1 instance_pk: 40 origin: invc1 parent_pk: 88, parent_model: <class 'dcim.models.devices.Device'> kwargs: {'signal': <django.db.models.signals.ModelSignal object at 0x101ad9510>, 'using': 'default'}

Because of the way MPTT is doing the delete the instance between the calls is actually two different instances, so I tried directly setting _previously_deleted inside post_delete on the instance, but it isn't there on the second call.

This causes an additional DB call when deleting a cached_counted item. The other way I thought of potentially handling this was to store the deleted ids in a request queue (like webhooks code uses) and check if it is in the queue (already deleted) before decrementing the counter. The queue would need to be cleared at the end of the request. However this makes the code less-clear and splits the queue init and handling.

@jeremystretch jeremystretch marked this pull request as ready for review December 12, 2023 20:33
@jeremystretch jeremystretch changed the title DRAFT: 14081 fixed cached counters on delete for parent-child items Fixes #1408: Fix cached counters on delete for parent-child items Dec 12, 2023
@jeremystretch jeremystretch changed the title Fixes #1408: Fix cached counters on delete for parent-child items Fixes #14081: Fix cached counters on delete for parent-child items Dec 12, 2023
@jeremystretch jeremystretch merged commit b937358 into develop Dec 12, 2023
8 checks passed
@jeremystretch jeremystretch deleted the 14081-cached-counters branch December 12, 2023 21:53
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Removing Module Bays with Children Power Ports breaks Power Ports count
2 participants