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

Improve queries for pipelines, run templates, models and artifacts #3335

Merged
merged 4 commits into from
Feb 5, 2025

Conversation

schustmi
Copy link
Contributor

@schustmi schustmi commented Feb 3, 2025

Describe changes

  • Each time a pipeline was fetched from the DB, we also fetched all the runs for this pipeline in order to include the status/ID of the latest run in the model.
  • Each time a run template was fetched from the DB, we also fetched all the runs for this template in order to include the status/ID of the latest run in the model.
  • Each time an artifact was fetched from the DB, we also fetched all the versions for this artifact in order to include the ID/name of the latest version in the model.
  • Each time a model was fetched from the DB, we also fetched all the versions for this model in order to include the ID/name of the latest version in the model.

As the runs and artifact/model versions can get quite a lot, this is very inefficient given that we're only interested in a single element (=the latest one). This PR fixes that by only querying for the latest run/version.

Pre-requisites

Please ensure you have done the following:

  • I have read the CONTRIBUTING.md document.
  • I have added tests to cover my changes.
  • I have based my new branch on develop and the open PR is targeting develop. If your branch wasn't based on develop read Contribution guide on rebasing branch to develop.
  • IMPORTANT: I made sure that my changes are reflected properly in the following resources:
    • ZenML Docs
    • Dashboard: Needs to be communicated to the frontend team.
    • Templates: Might need adjustments (that are not reflected in the template tests) in case of non-breaking changes and deprecations.
    • Projects: Depending on the version dependencies, different projects might get affected.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Other (add details above)

@schustmi schustmi requested a review from stefannica February 3, 2025 10:58
Copy link
Contributor

coderabbitai bot commented Feb 3, 2025

Important

Review skipped

Auto 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.


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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

@github-actions github-actions bot added internal To filter out internal PRs and issues enhancement New feature or request labels Feb 3, 2025
@schustmi schustmi changed the title Limit queries for pipelines and artifacts Improve queries for pipelines, run templates, models and artifacts Feb 3, 2025
@schustmi schustmi requested a review from bcdurak February 4, 2025 12:29
Copy link
Contributor

@stefannica stefannica left a comment

Choose a reason for hiding this comment

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

The change looks good from a functional perspective, but it is breaks some design considerations. Please see my comment for some ways to improve this.

Returns:
The latest version for this artifact.
"""
if session := object_session(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

I know it is convenient to use a function like this to fetch the session out of thin air, but this goes against some design principles and breaks the current order of things. For one, nobody would suspect that you have a dependency here on a DB session if they follow the code bread crumbs, this is completely hidden from a module dependency perspective.

I highly recommend that you used one of two alternative approaches, to make this dependency more obvious and to keep the design aligned to the current status quo:

First, make latest_version a function instead of a property and have it take in the DB session as an argument (let's call it get_latest_version instead). Then you have one of three choices:

  1. modify to_model to also take in the DB session as a mandatory argument, and make sure to pass it in all calls made to to_model from the SQL Zen Store
  2. keep the to_model signature untouched, but call get_latest_version directly from SQL Zen Store in addition to to_model. Or, to make it clear that this is mandatory, you could even add latest_version as a mandatory argument to to_model and set it in SQL Zen Store to the result you get from calling the get_latest_version function.
  3. in addition to 2. above, move get_latest_version to SQL Zen Store, which is where it should belong to in the first place. The schema files shouldn't have any SQL statements in them. They're just there to define schemas after all. We have a method called _count_entity that follows almost the same logic.

Copy link
Contributor

Choose a reason for hiding this comment

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

Update: you explained that this is exactly how lazy loading works (hidden from the user and relies on the active DB session that created the schema object). In that case, this is fine. But please to check that:

  1. this is indeed tied to the object and represents the DB session that created it
  2. this is thread safe

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I verified that this is indeed always the session object that was used to create/read the schema, and also ran some stress tests on our server without issues.

@schustmi schustmi merged commit dff5ba8 into develop Feb 5, 2025
79 checks passed
@schustmi schustmi deleted the feature/improve-db-queries branch February 5, 2025 05:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request internal To filter out internal PRs and issues run-slow-ci
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants