feat: paginate delete blockers#8238
Draft
grantfitzsimmons wants to merge 1 commit into
Draft
Conversation
Contributor
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Member
Author
|
@melton-jason Could you base this instead on |
| from django.db.models.deletion import Collector | ||
| from django.db.models.deletion import Collector, CASCADE, PROTECT | ||
| from django.db.models import ForeignKey | ||
| from django.views.decorators.http import require_POST |
9b4c82e to
4019815
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #7515
Authored by @melton-jason
Description
This PR addresses a major performance bottleneck in Specify's delete blockers endpoint, which affects large databases. Currently, when fetching delete blockers for a resource, Specify attempts to load all referencing records at once into a single Python object using Django's
Collector. For highly-referenced resources (particularlyAgent, which is referenced bycreatedByAgentandmodifiedByAgentacross 515+ relationships) this eager collection can:At least two production instances have experienced kernel-invoked process restarts due to this issue. Even when requests don't crash, they can hold the Python GIL long enough to block other threads.
Solution: Paginated delete blockers
This PR introduces pagination support for the delete blockers endpoint. Instead of loading all referencing records at once, related record IDs are fetched in configurable chunks (
limit/offset). The new approach:CollectorPROTECT/protect_with_blockers) and deferred blockers (CASCADE), enabling the frontend to prioritize what's shownold_delete_blockers) for backward compatibilityChanges
New endpoint behavior (
/api/specify/delete_blockers/<model>/<id>/):limit(default: 20) andoffset(default: 0) query parametersresults: immediate blockers — relationships usingPROTECTorprotect_with_blockersnext: deferred blockers — relationships usingCASCADEtable,field,ids,offset,limit, and acompleteflag indicating whether all matching records for that relationship were fetchedlimit=0fetches all results (unpaginated)Future work
Further frontend changes are needed to adopt the paginated approach in the UI. Additional backend optimizations (e.g., query-level improvements) will be detailed in follow-up issues.
Developer Testing instructions
GET /api/specify/delete_blockers/agent/<id>/?limit=20&offset=0— should return the first 20 IDs per blocking relationship in under 1 secondcompleteflag: Verifycomplete: truewhen all related records fit withinlimit, andcomplete: falseotherwiseGET /api/specify/old_delete_blockers/<model>/<id>/should return the full, unpaginated result set identical to the previous behaviorresultsandnextarraysAgentin a database of comparable size to Geneva, Edinburgh, or Berlin to confirm no timeouts or OOM conditions occur