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

delegated operator fixes #4908

Merged
merged 1 commit into from
Oct 22, 2024
Merged

delegated operator fixes #4908

merged 1 commit into from
Oct 22, 2024

Conversation

swheaton
Copy link
Contributor

@swheaton swheaton commented Oct 10, 2024

What changes are proposed in this pull request?

Addresses some weirdness introduced in #4436.

  1. I believe the if on line 272 should be indented to be under the COMPLETED case. Otherwise the if statement flow is goofy and should be reworked anyways.
  2. Passing an array as the update param is unusual, though legal. I'm wondering what was intended with the addition of the $ifNull? As written, I don't think it does much because the output_schema is fixed.

How is this patch tested? If it is not, please explain why.

test still passes

Release Notes

Is this a user-facing change that should be mentioned in the release notes?

  • No. You can skip the rest of this section.
  • Yes. Give a description of this change to be included in the release
    notes for FiftyOne users.

What areas of FiftyOne does this PR affect?

  • App: FiftyOne application changes
  • Build: Build and test infrastructure changes
  • Core: Core fiftyone Python library changes
  • Documentation: FiftyOne documentation changes
  • Other

Summary by CodeRabbit

  • New Features

    • Enhanced logic for updating operation states, ensuring correct handling of null metadata.
  • Bug Fixes

    • Improved handling of outputs_schema to prevent unintended document creation.
  • Tests

    • Added a test case to verify system behavior when metadata is null, ensuring robust updates to outputs_schema.

@swheaton swheaton requested a review from imanjra October 10, 2024 03:11
Copy link
Contributor

coderabbitai bot commented Oct 10, 2024

Walkthrough

The changes in this pull request focus on the update_run_state method of the MongoDelegatedOperationRepo class in delegated_operation.py. A new boolean variable, needs_pipeline_update, is introduced to determine if a pipeline update is necessary. The handling of outputs_schema is simplified by directly assigning it to the update dictionary. Additionally, a new test method is added to delegated_tests.py to verify the behavior of the system when metadata is set to null. These modifications enhance clarity and robustness in handling metadata updates.

Changes

File Path Change Summary
fiftyone/factory/repos/delegated_operation.py Modified update_run_state method: added needs_pipeline_update, simplified outputs_schema handling.
tests/unittests/operators/delegated_tests.py Added test_output_schema_null_metadata to check behavior with null metadata; imported bson.

Possibly related PRs

🐰 In the burrow where changes bloom,
The update_run_state finds more room.
With outputs_schema now so clear,
Null metadata brings no fear.
Tests are added, coverage wide,
Hopping forward, with joy we glide! 🌼


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@@ -312,7 +312,7 @@ def update_run_state(

doc = self._collection.find_one_and_update(
filter={"_id": _id},
update=[update],
update=update,
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this should be an array (aggregation pipeline). Otherwise passing a dictionary will be interpreted as the new doc so you'd essentially be setting the doc to have a new field "$set" with value equal to the dictionary value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You may be thinking of find_one_and_replace(). This change works and tests pass.
It's more standard to pass a dict with the updates. It's possible, though uncommon, to use an agg pipeline, but I didn't see why that was necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alternatively if you don't specify $set and pass a replacement document instead, then it would behave as you say.

Copy link
Contributor

@kaixi-wang kaixi-wang Oct 10, 2024

Choose a reason for hiding this comment

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

https://www.mongodb.com/docs/manual/reference/method/db.collection.findOneAndUpdate/

I'm just going off of the mongo docs...

Since the change didn't require any corresponding test changes, are there tests that would catch this?

Copy link
Contributor

Choose a reason for hiding this comment

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

But yeah the alternative is to remove set

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have to go off of the pymongo docs here since that's what we use. https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection.find_one_and_update

I noticed that if you call find_one_and_update it actually uses the deprecated MongoDB function findAndModify.
https://github.com/mongodb/mongo-python-driver/blob/master/pymongo/synchronous/collection.py#L3561

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And yes the test test_updates_progress tests for this. After talking with Ibrahim I am also making an update and adding a test for a specific corner case that's broken.

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: 0

🧹 Outside diff range and nitpick comments (1)
tests/unittests/delegated_operators_tests.py (1)

399-427: LGTM! Well-structured test for handling null metadata.

The new test method test_output_schema_null_metadata is a valuable addition. It effectively covers the edge case of handling null metadata when completing an operation with an outputs_schema. The test structure is clear and the assertions are comprehensive.

Consider adding a brief comment explaining the purpose of setting metadata to null, as it might not be immediately clear to other developers why this step is necessary:

# Simulate a scenario where metadata is explicitly set to null
self.svc._repo._collection.find_one_and_update(
    {"_id": bson.ObjectId(doc.id)}, {"$set": {"metadata": None}}
)
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between c3c89a5 and 7b4e36d.

📒 Files selected for processing (2)
  • fiftyone/factory/repos/delegated_operation.py (3 hunks)
  • tests/unittests/delegated_operators_tests.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • fiftyone/factory/repos/delegated_operation.py
🧰 Additional context used
📓 Learnings (1)
📓 Common learnings
Learnt from: swheaton
PR: voxel51/fiftyone#4893
File: fiftyone/factory/repos/delegated_operation.py:328-328
Timestamp: 2024-10-09T02:11:07.857Z
Learning: In the `MongoDelegatedOperationRepo.update_run_state` method, when calling `find_one_and_update`, pass the `update` parameter directly as a dictionary, not wrapped in a list.

@@ -310,9 +313,15 @@ def update_run_state(
update["$set"]["status"] = progress
update["$set"]["status"]["updated_at"] = datetime.utcnow()

# Using pipeline update instead of a single update doc fixes a case
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the update can either be a pipeline or a single update, why do we need to only make it a pipeline under certain conditions and not just always like the current code? Is there a different bug that can happen by consistently using pipeline for all cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's nonstandard. I'd prefer to use the standard approach and deviate only when necessary.

@swheaton
Copy link
Contributor Author

@imanjra @kaixi-wang please re-review

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: 0

🧹 Outside diff range and nitpick comments (1)
tests/unittests/operators/delegated_tests.py (1)

488-516: LGTM! Consider adding a docstring.

The test implementation thoroughly verifies the handling of null metadata in operations. It's a good addition that covers an important edge case.

Add a docstring to improve documentation:

 def test_output_schema_null_metadata(
     self, mock_get_operator, mock_operator_exists
 ):
+    """
+    Test that outputs_schema is correctly set in metadata when the metadata
+    field is explicitly null (not just unset).
+    """
     mock_outputs = MockOutputs()
     doc = self.svc.queue_operation(
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 7b4e36d and a254324.

📒 Files selected for processing (2)
  • fiftyone/factory/repos/delegated_operation.py (3 hunks)
  • tests/unittests/operators/delegated_tests.py (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • fiftyone/factory/repos/delegated_operation.py
🧰 Additional context used
🔇 Additional comments (1)
tests/unittests/operators/delegated_tests.py (1)

14-14: LGTM!

The addition of the bson import is appropriate for handling ObjectId operations in the new test method.

Copy link
Contributor

@kaixi-wang kaixi-wang left a comment

Choose a reason for hiding this comment

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

lgtm

@swheaton swheaton merged commit adf51c9 into develop Oct 22, 2024
13 checks passed
@swheaton swheaton deleted the fix/do-update-metadata-fix branch October 22, 2024 21:15
lanzhenw pushed a commit that referenced this pull request Oct 24, 2024
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.

2 participants