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

Optimized database requests when removing resources #8192

Merged
merged 32 commits into from
Jul 31, 2024

Conversation

bsekachev
Copy link
Member

@bsekachev bsekachev commented Jul 18, 2024

Motivation and context

How has this been tested?

Checklist

  • I submit my changes into the develop branch
  • I have created a changelog fragment
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • I have linked related issues (see GitHub docs)
  • I have increased versions of npm packages if it is necessary
    (cvat-canvas,
    cvat-core,
    cvat-data and
    cvat-ui)

License

  • I submit my code changes under the same MIT License that covers the project.
    Feel free to contact the maintainers if that's a concern.

Summary by CodeRabbit

  • Bug Fixes

    • Optimized deletion process to ensure all related resources are deleted efficiently.
    • Improved model field relationships to prevent issues when deleting related objects.
  • Performance Improvements

    • Enhanced methods for cascading deletions, reducing potential orphaned records and improving database integrity.
  • General Improvements

    • Unified deletion methods for tasks and jobs to streamline resource management and reduce redundancy.
    • Ensured segments are appropriately deleted under specified conditions, maintaining data consistency.

Copy link
Contributor

coderabbitai bot commented Jul 18, 2024

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The updates primarily enhance the deletion process of related resources in the CVAT project, task, job, and segment models. New methods for cascading deletions and optimized annotation removals were added, ensuring efficient resource management. Additionally, changes in the database migration file modify foreign key relationships for better integrity and consistency.

Changes

Files/Modules Change Summary
cvat/apps/dataset_manager/task.py Consolidated the deletion process using delete_related_resources method.
cvat/apps/engine/migrations/...alter_labeledimageattributeval_image_and_more.py Altered various model fields to modify foreign key relationships and related names.
cvat/apps/engine/models.py Added methods for deleting related resources and annotations in Project, Task, Job, and Segment classes.
cvat/apps/engine/views.py Modified perform_destroy method to delete associated segment when applicable under specified conditions.

Poem

In code's deep dance, a change unfolds,
With tasks and jobs, their stories told.
Deletion now refined, a clever art,
From models' fields, we do depart.
Resources clear with just one call,
In CVAT, we stand tall.
A bunny's joy in every scroll.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b6228da and d7cedba.

Files selected for processing (4)
  • cvat/apps/dataset_manager/task.py (1 hunks)
  • cvat/apps/engine/migrations/0082_alter_labeledimageattributeval_image_and_more.py (1 hunks)
  • cvat/apps/engine/models.py (8 hunks)
  • cvat/apps/engine/views.py (1 hunks)
Additional comments not posted (18)
cvat/apps/engine/migrations/0082_alter_labeledimageattributeval_image_and_more.py (6)

14-23: Field alteration for labeledimageattributeval.image is correct.

The image field now uses DO_NOTHING on delete, which is appropriate for preventing cascading deletions.


24-33: Field alteration for labeledshape.parent is correct.

The parent field now uses DO_NOTHING on delete and allows null values, which is appropriate for maintaining the hierarchical structure without cascading deletions.


34-43: Field alteration for labeledshapeattributeval.shape is correct.

The shape field now uses DO_NOTHING on delete, which is appropriate for preventing cascading deletions.


44-53: Field alteration for labeledtrack.parent is correct.

The parent field now uses DO_NOTHING on delete and allows null values, which is appropriate for maintaining the hierarchical structure without cascading deletions.


54-63: Field alteration for labeledtrackattributeval.track is correct.

The track field now uses DO_NOTHING on delete, which is appropriate for preventing cascading deletions.


64-73: Field alteration for trackedshapeattributeval.shape is correct.

The shape field now uses DO_NOTHING on delete, which is appropriate for preventing cascading deletions.

cvat/apps/dataset_manager/task.py (1)

445-445: Optimized deletion process in _delete method is correct.

The _delete method now uses the delete_related_resources method of db_job, which consolidates the deletion process for related resources.

cvat/apps/engine/models.py (10)

347-347: Modification in Project.get_labels method is correct.

The method now returns all label sets, which is appropriate for the project's requirements.


366-373: Project.delete_related_resources method optimizes deletion process.

The method efficiently deletes labels and attributes, preventing redundant deletions.


376-378: Modification in Project.delete method is correct.

The method now calls delete_related_resources before deletion, ensuring efficient resource management.


444-444: Modification in Task.get_labels method is correct.

The method now returns all label sets, which is appropriate for the task's requirements.


483-493: Task.delete_related_annotations method optimizes deletion process.

The method efficiently deletes related annotations, preventing redundant deletions.


494-503: Task.delete_related_resources method optimizes deletion process.

The method efficiently deletes labels and attributes, preventing redundant deletions.


506-508: Modification in Task.delete method is correct.

The method now calls delete_related_resources before deletion, ensuring efficient resource management.


786-795: Job.delete_related_annotations method optimizes deletion process.

The method efficiently deletes related annotations, preventing redundant deletions.


796-798: Job.delete_related_resources method optimizes deletion process.

The method efficiently deletes labels and attributes, preventing redundant deletions.


801-803: Modification in Job.delete method is correct.

The method now calls delete_related_resources before deletion, ensuring efficient resource management.

cvat/apps/engine/views.py (1)

1749-1753: Ensure the transaction block covers all necessary operations.

The transaction block ensures atomicity, but make sure all related operations are included within the block to maintain data integrity.

Comment on lines 1750 to 1753
segment = instance.segment
super().perform_destroy(instance)
if segment:
segment.delete()
Copy link
Contributor

Choose a reason for hiding this comment

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

Optimize segment deletion logic.

The segment deletion logic can be optimized by checking the existence of the segment before entering the transaction block to avoid unnecessary transactions.

-        with transaction.atomic():
-            segment = instance.segment
-            super().perform_destroy(instance)
-            if segment:
-                segment.delete()
+        segment = instance.segment
+        if segment:
+            with transaction.atomic():
+                super().perform_destroy(instance)
+                segment.delete()
+        else:
+            super().perform_destroy(instance)
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
segment = instance.segment
super().perform_destroy(instance)
if segment:
segment.delete()
segment = instance.segment
if segment:
with transaction.atomic():
super().perform_destroy(instance)
segment.delete()
else:
super().perform_destroy(instance)

@bsekachev bsekachev changed the title [WIP] Optimized database requests when removing resources Optimized database requests when removing resources Jul 18, 2024
@codecov-commenter
Copy link

codecov-commenter commented Jul 18, 2024

Codecov Report

Attention: Patch coverage is 94.20290% with 4 lines in your changes missing coverage. Please review.

Please upload report for BASE (develop@2191ffa). Learn more about missing BASE report.

Additional details and impacted files
@@            Coverage Diff             @@
##             develop    #8192   +/-   ##
==========================================
  Coverage           ?   83.32%           
==========================================
  Files              ?      388           
  Lines              ?    41396           
  Branches           ?     3856           
==========================================
  Hits               ?    34493           
  Misses             ?     6903           
  Partials           ?        0           
Components Coverage Δ
cvat-ui 79.64% <ø> (?)
cvat-server 86.61% <94.20%> (?)

Copy link
Contributor

@Marishka17 Marishka17 left a comment

Choose a reason for hiding this comment

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

I have a few minor comments, but overall patch works for me.

cvat/apps/engine/utils.py Show resolved Hide resolved
cvat/apps/engine/views.py Outdated Show resolved Hide resolved
cvat/apps/engine/models.py Show resolved Hide resolved
cvat/apps/dataset_manager/task.py Outdated Show resolved Hide resolved
@bsekachev
Copy link
Member Author

@Marishka17 @zhiltsov-max Any more comments?

bsekachev and others added 4 commits July 26, 2024 14:45
Co-authored-by: Maxim Zhiltsov <zhiltsov.max35@gmail.com>
Co-authored-by: Maxim Zhiltsov <zhiltsov.max35@gmail.com>
Copy link

@bsekachev bsekachev merged commit e0da3a2 into develop Jul 31, 2024
33 checks passed
@bsekachev bsekachev deleted the bs/requests_optimization branch July 31, 2024 13:12
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.

4 participants