voxel51 / fiftyone

The open-source tool for building high-quality datasets and computer vision models
https://fiftyone.ai
Apache License 2.0
7.86k stars 516 forks source link

More robust handling of multiple config docs #4541

Open brimoor opened 5 days ago

brimoor commented 5 days ago

Under heavy concurrent SDK usage, some users have reported seeing the following error when connecting to the database:

OSError: Cannot connect to database v0.14.4 with client v0.24.0 (compatibility >=0.19,<0.25). See https://docs.voxel51.com/user_guide/config.html#database-migrations for more information

For context, the config collection is supposed to have exactly one document. However, if it were empty for some reason, then this error would occur. How could it be empty? I suspect that, under heavy concurrent usage, this function: https://github.com/voxel51/fiftyone/blob/a5d2209903d3699be3d310a55ab1e518f858050a/fiftyone/core/odm/database.py#L101

could be subject to a race condition that accidentally deletes all config docs.

This PR makes two improvements to protect against such a race condition:

  1. When there are multiple config docs, keep the newest one rather than the one with the latest version. Also, for extra protection, only delete the duplicate docs if database_admin=True
    • This is more robust to race conditions because _id are universally unique while the duplicate config docs likely have the same version, so the max() logic used to select which doc to keep may not be deterministic, thus causing concurrent sessions to accidentally delete all the config docs
    • Note that sorting by _id is equivalent to sorting by insertion date
  2. When generating a new database config doc, directly insert the current foc.VERSION rather than 0.14.4
    • 0.14.4 was previously being inserted to correctly handle migrating <0.14.4 databases up to 0.14.4 (when the config collection was introduced). However, the 0.14.4 release is now over 2 years old, so let's assume that everyone has already migrated up. Why? Because...
    • ... the benefit of this change is that even if the config collection still somehow becomes empty, the new doc will assume the current client version and thus the compatibility error that we're trying to avoid will no longer occur

I don't know for certain that this race condition is actually the reason that users are seeing the compatibility error, but this PR doesn't have any side effects so we might as well give it a shot 🚀

Summary by CodeRabbit

coderabbitai[bot] commented 5 days ago

Walkthrough

The recent updates in the repository focus on improving database configuration management and enhancing test coverage. Key changes include refactoring the get_db_config function for better handling of multiple config documents, setting default values for configuration attributes, and renaming and refining internal functions. Additionally, minor changes were made to handle database migration logic and test functions to ensure consistency and reliability.

Changes

Files Change Summary
fiftyone/core/odm/database.py Refactored get_db_config for better handling of multiple config documents, set default values, and renamed functions
tests/unittests/utils_tests.py Added imports and modified a test function to insert documents with _id based on datetime object
fiftyone/migrations/runner.py Added assignment to ensure config is set during database migration

Poem

In the code where data flows, A rabbit hops, improvements it sows. 🐇 Configs now handled with care, Defaults set, clean and fair. Tests ensure paths are right, Through database fields, shining bright. ✨


[!TIP]

Early access features: enabled We are currently testing the following features in early access: - **OpenAI `gpt-4o` model for code reviews and chat**: OpenAI claims that this model is better at understanding and generating code than the previous models. We seek your feedback over the next few weeks before making it generally available. Note: - You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file. - Please join our [Discord Community](https://discord.com/invite/GsXnASn26c) to provide feedback and report issues. - OSS projects are always opted into early access features.

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 - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit .` - `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 Configration 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](https://docs.coderabbit.ai/guides/configure-coderabbit) 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](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.