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: duplicate read proto #1741

Merged
merged 24 commits into from
Apr 18, 2024

Conversation

Shylock-Hg
Copy link
Contributor

@Shylock-Hg Shylock-Hg commented Apr 16, 2024

Summary:
Briefly describe the changes made in this PR.

According to @tusharmath 's comments. use HashMap to avoid duplicate read.

Current output:

shylock@localhost:~/Workspace/tailcall> ./target/debug/tailcall start examples/grpc.graphql 
 INFO File read: examples/grpc.graphql ... ok
 INFO File read: examples/../src/grpc/tests/proto/news.proto ... ok
 INFO N + 1 detected: 0
 INFO 🚀 Tailcall launched at [127.0.0.1:8000] over HTTP/1.1
 INFO 🌍 Playground: http://127.0.0.1:8000

Issue Reference(s):
Fixes #... (Replace "..." with the issue number)
Close #1734
/claim #1734

Build & Testing:

  • I ran cargo test successfully.
  • I have run ./lint.sh --mode=fix to fix all linting issues raised by ./lint.sh --mode=check.

Checklist:

  • I have added relevant unit & integration tests.
  • I have updated the documentation accordingly.
  • I have performed a self-review of my code.
  • PR follows the naming convention of <type>(<optional scope>): <title>

Summary by CodeRabbit

  • New Features

    • Introduced a caching mechanism to improve file reading efficiency in the application.
    • Enhanced resource reading capabilities with options for both cached and direct file access.
  • Enhancements

    • Updated initialization processes for various readers to accommodate new caching features.
    • Added thread safety to file reading operations using mutex locks.
  • Refactor

    • Modified method signatures to support new concurrent and caching functionalities.

Copy link
Contributor

coderabbitai bot commented Apr 16, 2024

Walkthrough

Walkthrough

The recent updates in the codebase introduce a caching mechanism for file reading, aimed at enhancing efficiency and reducing redundant operations. The changes involve integrating async-lock for thread-safe operations and modifying several components to utilize a shared cache. The primary focus is on the ResourceReader and ProtoReader structures, ensuring they handle file reads with potential caching.

Changes

File(s) Change Summary
Cargo.toml Added async-lock dependency.
src/config/reader.rs
  • Added imports for async_lock::Mutex, std::collections::HashMap.
  • Updated ConfigReader to use a cached HashMap for file content.
  • Modified initialization of ResourceReader and ProtoReader to include caching support.
  • Updated methods in ConfigReader to support caching and thread safety.
    | src/generator/generator.rs |
  • Updated initialization of ProtoReader to support caching.
  • Adjusted Generator struct to incorporate caching in ProtoReader.
    | src/proto_reader.rs |
  • Refactored ProtoReader to accept ResourceReader for better flexibility.
  • Updated test functions to use ResourceReader.
    | src/resource_reader.rs |
  • Implemented caching with HashMap wrapped in Mutex.
  • Added caching support to ResourceReader for efficient file reading.
  • Introduced methods for cached and direct file reading, enhancing parallel file reads.

Assessment against linked issues

Objective Addressed Explanation
Ensure each file is logged only once in CLI output when read [#1734] The caching mechanism introduced could potentially prevent duplicate file reads, but a direct validation against the specific logging issue is needed. Further investigation into the logging behavior is recommended to confirm if duplicates are eliminated effectively.

Recent Review Details

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 3a2d9f5 and af2d812.
Files selected for processing (1)
  • src/resource_reader.rs (3 hunks)
Additional Context Used
Path-based Instructions (1)
src/resource_reader.rs (1)

Pattern **/*.rs: Programming Style Guidelines

  • When calling functions that do not need to modify values, pass references of those values.
  • When calling functions that need to modify values, pass ownership of the values, and ensure they are returned from the function.

IMPORTANT: This programming style may not be suitable for performance-sensitive components or hot code paths. In such cases, prioritize efficiency and optimization strategies to enhance performance.

Testing

  1. Write Tests: For every new feature or bugfix, ensure that you write appropriate tests.
    Structure your tests in the following way:

    use pretty_assertions::assert_eq;
    fn test_something_important() {
       let value = setup_something_using_a_function();
    
       let actual = perform_some_operation_on_the_value(value);
       let expected = ExpectedValue {foo: 1, bar: 2};
    
       assert_eq!(actual, expected);
    }
    • Setup the value using helper methods in tests.
    • Create an actual and an expected value.
    • Assert the two values in a new line.
    • Ensure there are only one assertions per test.
Learnings (1)
Common
User: Shylock-Hg"
URL: https://github.com/tailcallhq/tailcall/pull/1723

Timestamp: 2024-04-14T10:42:00.789Z
Learning: JSON schema tests for the `schemars::JsonSchema` trait in `Config` are considered outside the scope of PR #1723.
Additional comments not posted (1)
src/resource_reader.rs (1)

14-71: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [54-91]

The implementation of the Direct struct looks correct and efficient.


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

@github-actions github-actions bot added the type: fix Iterations on existing features or infrastructure. label Apr 16, 2024
@Shylock-Hg Shylock-Hg mentioned this pull request Apr 16, 2024
6 tasks
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: 3

src/resource_reader.rs Outdated Show resolved Hide resolved
src/generator/generator.rs Outdated Show resolved Hide resolved
src/config/reader.rs Outdated Show resolved Hide resolved
Copy link

codecov bot commented Apr 16, 2024

Codecov Report

Attention: Patch coverage is 95.58824% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 86.89%. Comparing base (568ca3d) to head (7ee194b).
Report is 10 commits behind head on main.

❗ Current head 7ee194b differs from pull request most recent head af2d812. Consider uploading reports for the commit af2d812 to get more accurate results

Files Patch % Lines
src/resource_reader.rs 93.87% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1741      +/-   ##
==========================================
- Coverage   86.93%   86.89%   -0.05%     
==========================================
  Files         155      155              
  Lines       15540    15574      +34     
==========================================
+ Hits        13510    13533      +23     
- Misses       2030     2041      +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

src/resource_reader.rs Outdated Show resolved Hide resolved
}

impl ResourceReader {
pub fn init(runtime: TargetRuntime) -> Self {
Self { runtime }
pub fn init(runtime: TargetRuntime, cache: Arc<Mutex<HashMap<String, String>>>) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pub fn init(runtime: TargetRuntime, cache: Arc<Mutex<HashMap<String, String>>>) -> Self {
pub fn init(runtime: TargetRuntime, cache: bool) -> Self {

If cache is true, we should initialize it inside of init

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 there is another issue, the first reading is called by ConfigReader::resource_reader, the second is called by ConfigReader::proto_reader, they are different object. So we need to get the HashMap from outside.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about using Option<HashMap>?

Copy link
Contributor

Choose a reason for hiding this comment

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

And there is another issue, the first reading is called by ConfigReader::resource_reader, the second is called by ConfigReader::proto_reader, they are different object. So we need to get the HashMap from outside.

The cache should not be shared. If the cache needs to be shared, we should do that by sharing the ResourceReader.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that's just an additional unwrapping that makes the code difficult to read. You can create two resource readers

struct ResourceReader<A>(A);

impl<A> ResourceReader<A> {
  fn direct(runtime: TargetRuntime) -> ResourceReader<Direct>
  fn cached(runtime: TargetRuntime) -> ResourceReader<Cache>
}

// Reads the files directly without cache
Direct {runtime: TargetRuntime }

// Reads it and maintains a cache
pub Cache {
  reader: Direct,
  cache: Arc<Mutex<HashMap<String, FileRead>>
}

Can we do something like above? We will know at compile time, if the reader is caching or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Static dispatch is contagious, so I use the option to control whether enable cache.

Copy link
Contributor

@tusharmath tusharmath Apr 17, 2024

Choose a reason for hiding this comment

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

It makes the code a lot more modular, type-safe and easy to test. The current implementation has a lot of checks before reading, increasing the potential of bugs.

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 tried it, but got compile error about not found self.resource_reader.read_file method of ProtoReader. I'm not familiar with this, maybe I need a trait bound to help compiler?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I switch to static dispatch and add a reader trait.

src/config/reader.rs Outdated Show resolved Hide resolved
@Shylock-Hg Shylock-Hg requested a review from tusharmath April 16, 2024 13:04
Self { proto_reader: ProtoReader::init(runtime) }
Self {
proto_reader: ProtoReader::init(Arc::new(ResourceReader::init(runtime, false))),
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we changing the API here? I don't think it's required.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ProtoReader need to share ResourceReader to avoid duplicate reading.

src/config/reader.rs Outdated Show resolved Hide resolved
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

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

src/proto_reader.rs Outdated Show resolved Hide resolved
src/proto_reader.rs Outdated Show resolved Hide resolved
src/proto_reader.rs Outdated Show resolved Hide resolved
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

src/proto_reader.rs Outdated Show resolved Hide resolved
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: 2

src/resource_reader.rs Outdated Show resolved Hide resolved
src/resource_reader.rs Outdated Show resolved Hide resolved
@Shylock-Hg Shylock-Hg requested a review from tusharmath April 17, 2024 08:53
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: 2

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

src/resource_reader.rs Outdated Show resolved Hide resolved
@Shylock-Hg
Copy link
Contributor Author

@tusharmath Please take a look.

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

src/resource_reader.rs Show resolved Hide resolved
src/resource_reader.rs Outdated Show resolved Hide resolved
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: 3

src/resource_reader.rs Outdated Show resolved Hide resolved
src/resource_reader.rs Outdated Show resolved Hide resolved
src/resource_reader.rs Outdated Show resolved Hide resolved
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: 4

src/resource_reader.rs Outdated Show resolved Hide resolved
@tusharmath tusharmath enabled auto-merge (squash) April 18, 2024 11:45
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
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: 3

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

src/resource_reader.rs Show resolved Hide resolved
src/resource_reader.rs Show resolved Hide resolved

impl Cached {
pub fn init(runtime: TargetRuntime) -> Self {
Self { direct: Direct::init(runtime), cache: Default::default() }
Copy link
Contributor

Choose a reason for hiding this comment

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

Initialize the cache explicitly to ensure it matches the expected type and is thread-safe.

- cache: Default::default(),
+ cache: Arc::new(Mutex::new(HashMap::new())),

Explicitly initializing the cache as Arc<Mutex<HashMap<String, String>>> ensures that the type is correct and that thread safety is maintained.


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.

Suggested change
Self { direct: Direct::init(runtime), cache: Default::default() }
Self { direct: Direct::init(runtime), cache: Arc::new(Mutex::new(HashMap::new())) }

@tusharmath tusharmath disabled auto-merge April 18, 2024 12:12
@tusharmath tusharmath merged commit 3170f76 into tailcallhq:main Apr 18, 2024
26 of 27 checks passed
@Shylock-Hg Shylock-Hg deleted the fix/duplicate-read-proto-2 branch April 18, 2024 14:19
ssddOnTop pushed a commit that referenced this pull request May 2, 2024
Co-authored-by: Tushar Mathur <[email protected]>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🙋 Bounty claim type: fix Iterations on existing features or infrastructure.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Duplicated CLI output related to read files
2 participants