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

Fix GCP step logging #2533

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open

Conversation

adtygan
Copy link

@adtygan adtygan commented Mar 16, 2024

Describe changes

The issue arises because GCS artifacts are immutable Stack Overflow thread. To fix the issue, I suggest a fix based on @strickvl 's idea mentioned below

Alex Strick van Linschoten's Idea

Consider using the logging.StreamHandler facility to temporarily write logs to the remote file (GCS, S3, etc.). Here's an example:

import logging
import fsspec

f = fsspec.open("gs://<<my_gcs_bucket>>/test_log.log", "w")
with f as of:
    log_handler = logging.StreamHandler(of)
    logger = logging.getLogger()  # Root logger
    logger.addHandler(log_handler)
    for i in range(0, 5000):
        logger.warning(f"I'm log line #{i}")
    logger.removeHandler(log_handler)

This approach could fit nicely in the StepLogsStorageContext class.

My Solution

Set buffer size to sys.maxsize and copy the existing contents of the log file to the buffer. This prevents the triggering of the part of code that appends buffer contents to log file once buffer is full and flushes buffer. (reference)

Code to test the change:

Please use GCS stack

import gcsfs
from zenml.client import Client
from zenml.logging.step_logging import StepLogsStorage, StepLogsStorageContext

client = Client()
_ = client.active_stack

TEST_FILE="gs://<<my_gcs_bucket>>/test.txt"

with StepLogsStorageContext(logs_uri=TEST_FILE) as log_storage:
    for i in range(0,10):
        print(f"I'm log line #{i}")

fs = gcsfs.GCSFileSystem()

with fs.open(TEST_FILE, 'r') as f:
    all_of_it = f.read()

print('Final Contents:')
print(all_of_it)

Pre-requisites

Please ensure you have done the following:

  • I have read the CONTRIBUTING.md document.
  • If my change requires a change to docs, I have updated the documentation accordingly.
  • 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.
  • If my changes require changes to the dashboard, these changes are communicated/requested.

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)

Summary by CodeRabbit

  • Refactor
    • Enhanced the initialization process of StepLogs to support a maximum message limit and improved handling of log file reading errors.

Copy link
Contributor

coderabbitai bot commented Mar 16, 2024

Important

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

Walkthrough

The update introduces an enhancement to the logging mechanism within the StepLogs class by incorporating a max_messages parameter for better log message management and adding functionality to recover logs from a specified URI, improving robustness and usability in diverse environments, including cloud storage solutions.

Changes

File Path Change Summary
src/zenml/.../step_logging.py Added max_messages to StepLogsStorage initialization; implemented log recovery from logs_uri.

Related issues

🐇✨

In the realm of code, where logs do sprawl,
A rabbit hopped, aiming to enthrall.
With messages tamed, and errors so few,
Through cloud and through file, our insights grew.
🌟📜

"Fear not," it whispered, "for I've found a way,
To keep our logs safe, come what may."
🌈🚀

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.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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 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/coderabbit-overrides.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.

@adtygan adtygan mentioned this pull request Mar 16, 2024
9 tasks
@strickvl
Copy link
Contributor

@coderabbitai review

@strickvl strickvl added the good first issue Good for newcomers label Mar 16, 2024
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.

Review Status

Actionable comments generated: 1

Configuration used: .coderabbit.yaml

Commits Files that changed from the base of the PR and between 34d624f and 64a1e74.
Files selected for processing (1)
  • src/zenml/logging/step_logging.py (1 hunks)

src/zenml/logging/step_logging.py Show resolved Hide resolved
@strickvl
Copy link
Contributor

@adtygan there's a linting error that needs fixing

@adtygan
Copy link
Author

adtygan commented Mar 16, 2024

@strickvl , I ran the below commands before I made the PR

zenml integration install gcp 
pip install click~=8.0.3
mypy --install-types

After that I ran

bash scripts/format.sh
bash scripts/run-ci-checks.sh >> .local/run-ci-checks.log

For format.sh, I got the following output

Screenshot 2024-03-17 at 12 21 15 AM

For run-ci-checks.sh, I got the following output in the terminal

Screenshot 2024-03-17 at 12 23 46 AM

I am attaching the log file for run-ci-checks.sh. I see that the lint script ran without errors. I am not sure what I am doing wrong. Kindly request your assistance.

run-ci-checks.log

@strickvl
Copy link
Contributor

https://github.com/zenml-io/zenml/actions/runs/8307099335/job/22735937951?pr=2533#step:5:27 this is the specific thing you need to fix first of all.

@adtygan
Copy link
Author

adtygan commented Mar 16, 2024

In the commit I had made I do see the import at Line 25
SHA is: 34d179ca47be2927e1e0af77f5dd25d4971e186a
Screenshot 2024-03-17 at 12 44 43 AM

But after this, there was another commit made by you, and in this version I cannot see the import anymore
SHA is: 64a1e745e4b38efc5ff4ce9c6738e95199f4dd50

Screenshot 2024-03-17 at 12 47 42 AM

I am not sure why this is happening.

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@strickvl
Copy link
Contributor

It seems like maybe you're using an old version of the ruff formatter @adtygan? Can you pip install "zenml[server,dev]" again in your environment to make sure you're using the latest version, then rerun the script and push the changes? We def shouldn't be seeing changes to all these files in the PR.

@adtygan
Copy link
Author

adtygan commented Mar 24, 2024

I figured out that I was working on a fork of the repo so my develop branch was out of date. I pulled the updates from upstream and then pushed my changes. Please review.

@strickvl
Copy link
Contributor

There are quite a few updated files in here that shouldn't need to be updated. Not sure what happened on your side or with the fork, but since the changes are small, it might be worth resetting this branch somehow so that only the changes you made are reflected in the changed files?

@adtygan
Copy link
Author

adtygan commented Mar 25, 2024

I only made a change to the logging/step_logging.py file. But when I run the format.sh script it modifies a lot of other files. Can I run the script on only the login file and push a commit? I think that should fix the issue.

@strickvl
Copy link
Contributor

strickvl commented Mar 25, 2024 via email

@adtygan
Copy link
Author

adtygan commented Mar 25, 2024

I upgraded zenml but not ruff. I will do this also and then try once more like you mentioned.

@strickvl
Copy link
Contributor

Yeah the updates to the important formatting packages will only come when you update zenml[dev] packages, not just core zenml @adtygan

@adtygan
Copy link
Author

adtygan commented Mar 25, 2024

I had already run pip install "zenml[server,dev]" the last time. But this was using ruff 0.1.7. I ran pip install -U ruff after this like you suggested and it upgraded to version 0.3.4. When I ran format.sh now, it doesn't raise any errors. So I have now pushed my changes.

@adtygan
Copy link
Author

adtygan commented Apr 6, 2024

May I please know if there is any action required from my end to solve this issue?

@strickvl
Copy link
Contributor

strickvl commented Apr 6, 2024

@bcdurak is looking into it / at it

Copy link
Contributor

@bcdurak bcdurak left a comment

Choose a reason for hiding this comment

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

Hey @adtygan, sorry about the late response regarding this issue.

I have checked the original problem and the implementation in the PR again and there are a few points that I wanted to mention here:

  • Currently, the logic around how ZenML stores the log messages works as follows:
    • Whenever we start the execution of a step, we create the proper context manager (StepLogsStorageContext) based on the properties of this step and a unique identifier. This process is conducted only once per step execution.
    • During the execution, ZenML queues up the incoming log messages in a buffer. If the buffer reaches a certain size or a certain amount of time has passed, we empty it by saving the messages in the designated location using the active artifact store.

The problem arises due to the immutability of the designated file in GCS as you mentioned above. While this PR fixes the problem to some degree, unfortunately, this does not propose a full solution.

  • If I am not missing anything, in the current version of the implementation, it is redundant to read any existing log messages (and add them to the buffer) as it is impossible to for ZenML create a context manager which points to an already existing log file. As I have mentioned, this process is done once per step execution by using a unique identifier.

  • The current solution works because it removes the first threshold of the buffer by setting the max_messages to a high enough value. However, you would run into the same issue if the buffer is cleared not due to the size but due to the time limit. For instance, if you add a time.sleep(20) in between two log messages, you will run into the same issue.

  • Finally, the current changes impact the functionality of the entire implementation and deem the max_messages parameter useless unless it is explicitly defined. I would perhaps propose a solution where a patch is applied only if the current artifact store is a GCP artifact store.

Thank you once again for working on this issue. The logs storage is an important topic and we are actively looking for different ways to improve our implementation, so your contribution is much appreciated :)

@adtygan
Copy link
Author

adtygan commented May 2, 2024

Thank you for your review @bcdurak . I agree with the points you have mentioned above. If I am allowed to propose a GCP specific solution, I have 2 approaches in mind.

  1. Patch the time limit as well so that my current solution can work.
  2. GCP, if I'm not wrong, allows some other way to create mutable files. But as far as I know, it won't be a GCS object. I will look into using these tools to solve the problem.

Which of the 2 routes do you suggest?

Thanks

@strickvl
Copy link
Contributor

strickvl commented May 3, 2024

linked to #2366 (closed in favor of this one)

@bcdurak
Copy link
Contributor

bcdurak commented May 7, 2024

Hey @adtygan,

I think the second approach would be better here if possible. The first one is still viable but I would only apply it if the active artifact store is a GCS artifact store.

As I mentioned before, logging has been an integral part of ZenML and we will be working on improving the user experience when it comes to handling these log messages. @safoinme is currently taking a look at our implementation. He can also share any insights and recommendations he comes across.

@adtygan
Copy link
Author

adtygan commented May 8, 2024

Thank you for your inputs @bcdurak . I will see how I can implement option 2. @safoinme could you please share your insights?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants