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

refactor(flow): make from_substrait_* async& worker handle refactor #4210

Merged
merged 3 commits into from
Jun 27, 2024

Conversation

discord9
Copy link
Contributor

@discord9 discord9 commented Jun 25, 2024

I hereby agree to the terms of the GreptimeDB CLA.

Refer to a related PR or issue link (optional)

What's changed and what's your intention?

make from_substrait_* async&refactor flow worker handle impl

Please explain IN DETAIL what the changes are in this PR and why they are needed:

  • make from_substrait_* async
  • call block_on in deser to call async method
  • refactor flow worker handle to use oneshot to send back response to request
  • remove ser/de derive for flow plan&expr since they are not used for now

Checklist

  • I have written the necessary rustdoc comments.
  • I have added the necessary unit tests and integration tests.
  • This PR requires documentation updates.

Summary by CodeRabbit

  • New Features

    • Introduced asynchronous processing capabilities.
    • Added async-recursion dependency for enhanced async functionality.
  • Refactor

    • Converted several methods and functions to asynchronous, improving performance and scalability.
    • Simplified and streamlined various functionalities, removing unnecessary locks and synchronizations.
  • Style

    • Updated code to ensure consistent use of async/await patterns.
  • Chores

    • Removed Serialize and Deserialize traits from multiple structs and enums, indicating a change in serialization strategy.

@discord9 discord9 requested review from zhongzc, waynexia and a team as code owners June 25, 2024 09:26
@discord9 discord9 changed the title refactor: make from_substrait_* async& worker handle refactor refactor(flow): make from_substrait_* async& worker handle refactor Jun 25, 2024
Copy link
Contributor

coderabbitai bot commented Jun 25, 2024

Walkthrough

The recent update introduces substantial asynchronous capabilities into the project by changing multiple synchronous functions to asynchronous, enabled by the async-recursion dependency in Cargo.toml. It also simplifies the locking mechanisms in worker.rs and removes serialization traits from various structs, streamlining data handling and enhancing concurrency control.

Changes

File Change Summary
src/flow/Cargo.toml Added async-recursion = "1.0" dependency.
src/flow/src/adapter/worker.rs Refactored imports, simplified locks, refactored async mechanisms.
src/flow/src/expr/scalar.rs Made try_from_raw_fn asynchronous with awaited get_fn_impl().
src/flow/src/transform.rs Converted sql_to_flow_plan to asynchronous.
src/flow/src/transform/aggr.rs Changed various TypedExpr and AggregateExpr functions to asynchronous.
src/flow/src/transform/expr.rs Converted multiple functions to asynchronous.
src/flow/src/transform/literal.rs Added .await to TypedPlan::from_substrait_plan in tests.
src/flow/src/transform/plan.rs Changed several functions to asynchronous, including recursive functions.
src/flow/src/expr/func.rs Removed Serialize and Deserialize from UnmaterializableFunc enum.
src/flow/src/expr/linear.rs Removed serialization traits from MapFilterProject and SafeMfpPlan structs.
src/flow/src/expr/relation.rs Removed serialization traits from AggregateExpr. Added serde default for distinct.
src/flow/src/plan.rs Removed serialization traits from TypedPlan and Plan structs.
src/flow/src/plan/join.rs Removed serialization traits from multiple Join related structs.
src/flow/src/plan/reduce.rs Removed serialization traits from multiple Reduce related structures.
src/flow/src/utils.rs Removed serialization traits from KeyExpiryManager and Arrangement structs.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Worker
    participant InterThreadCallClient
    participant InterThreadCallServer
    participant RequestHandler

    Client->>+Worker: Send Request
    Worker->>+InterThreadCallClient: Forward Request
    InterThreadCallClient->>+RequestHandler: Handle Request asynchronously
    RequestHandler-->>-InterThreadCallClient: Send Response
    InterThreadCallClient-->>-Worker: Receive Response
    Worker-->>-Client: Send Response
Loading

Poem

In fields of code where bytes do play,
Async flows bring brighter day.
Locks removed, the worker sighs,
As tasks now dance beneath the skies.
No more serialize, we cheer and beam,
A simpler, swifter, coding dream. 🌟


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.
    • @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 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.

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 (2)
src/flow/src/transform/aggr.rs (2)

Line range hint 90-122: Review of from_substrait_agg_measures async implementation

The conversion of from_substrait_agg_measures to async is well executed. The handling of the optional filter using .await and .transpose() is a clean approach to conditionally process asynchronous operations. However, the comment TODO(discord9): impl order_by suggests that order by functionality is yet to be implemented, which might be critical depending on the use case.

Would you like assistance in implementing the order by functionality or should we track this as an issue?


Line range hint 150-165: Async conversion in from_substrait_agg_func

This function's conversion to async is well-handled, especially with the asynchronous fetching of arguments using TypedExpr::from_substrait_rex(e, input_schema, extensions).await. The error handling with not_impl_err! for unsupported argument types is a good defensive programming practice. However, the comment TODO(discord9): impl filter suggests that filter functionality is not yet implemented. This could be important for the function's completeness and correctness.

Would you like me to help implement the filtering functionality or should we create a tracking issue for this?

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1204477 and 4c1af2b.

Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
Files selected for processing (8)
  • src/flow/Cargo.toml (1 hunks)
  • src/flow/src/adapter/worker.rs (14 hunks)
  • src/flow/src/expr/scalar.rs (5 hunks)
  • src/flow/src/transform.rs (1 hunks)
  • src/flow/src/transform/aggr.rs (23 hunks)
  • src/flow/src/transform/expr.rs (22 hunks)
  • src/flow/src/transform/literal.rs (1 hunks)
  • src/flow/src/transform/plan.rs (7 hunks)
Files skipped from review due to trivial changes (1)
  • src/flow/Cargo.toml
Additional comments not posted (19)
src/flow/src/transform/literal.rs (1)

175-175: Refactor to asynchronous function call approved.

The change to make from_substrait_plan asynchronous aligns with the PR's goal to enhance asynchronous processing. This is a crucial step in improving the responsiveness and scalability of the system.

src/flow/src/transform.rs (1)

143-143: Asynchronous function implementation approved.

Converting sql_to_flow_plan to an asynchronous function is in line with modern Rust async programming practices, especially for IO-bound tasks. This change should help in managing more complex flows efficiently.

src/flow/src/transform/plan.rs (4)

35-35: Asynchronous conversion of function approved.

Converting from_substrait_plan to async improves the system's ability to handle concurrent operations efficiently, aligning with the PR's objectives.


68-68: Asynchronous function implementation approved.

Making from_substrait_project asynchronous is a significant improvement for handling complex projection operations that may involve IO or other asynchronous operations.


136-136: Asynchronous function implementation approved.

The async refactor of from_substrait_filter enhances the handling of filter operations, which can be particularly beneficial when these operations are dependent on external asynchronous calls or data fetching.


155-155: Asynchronous function implementation approved.

The conversion of from_substrait_read to an asynchronous function is crucial for efficient data retrieval operations, which are often IO-bound.

src/flow/src/adapter/worker.rs (1)

18-18: Comprehensive refactor of worker mechanisms approved.

The changes in worker.rs enhance the system's capability to handle asynchronous operations and inter-thread communications more robustly. The use of oneshot channels for response handling and the simplification of atomic operations are in line with modern Rust practices and contribute to better scalability and error handling.

Also applies to: 24-25, 42-42, 109-109, 125-125, 139-140, 156-156, 161-161, 174-176, 188-188, 257-285, 303-341, 394-394, 400-400, 405-405, 408-418, 424-424, 428-428, 432-432

src/flow/src/transform/expr.rs (5)

Line range hint 62-81: Refactor to asynchronous function signature and implementation.

The function from_scalar_fn_to_df_fn_impl has been correctly refactored to be asynchronous, which aligns with the PR's objectives to improve asynchronous handling. The usage of .await on the substrait::df_logical_plan::consumer::from_substrait_rex call is appropriate for handling asynchronous operations.


Line range hint 140-154: Refactor to asynchronous function signature and implementation.

The function from_substrait_to_datafusion_scalar_func has been refactored to be asynchronous. This change is part of the broader effort to make the codebase more asynchronous and responsive. The function now properly awaits the asynchronous function RawDfScalarFn::from_proto and DfScalarFunction::try_from_raw_fn, ensuring that all operations that could block are handled asynchronously.


Line range hint 165-299: Refactor to asynchronous function signature and implementation.

The function from_substrait_scalar_func has been converted to an asynchronous function. This is in line with the PR's objectives and the modernization of the codebase to use asynchronous programming patterns. The function effectively handles asynchronous operations, particularly with the .await calls on TypedExpr::from_substrait_rex within a loop, which is a good practice for operations that might block.


307-344: Refactor to asynchronous function signature and implementation.

The function from_substrait_ifthen_rex has been converted to asynchronous, which is consistent with the rest of the changes in the PR. The use of .await in potentially blocking calls within conditional logic is correctly implemented. This ensures that the function does not block the runtime while waiting for these operations to complete.


Line range hint 368-422: Refactor to asynchronous function signature and implementation.

The function from_substrait_rex has been marked with the async_recursion attribute and refactored to be asynchronous. This change supports recursive asynchronous calls within the function, which is crucial given the nature of the operations it performs. The handling of different RexType cases and their respective asynchronous operations is well implemented.

src/flow/src/expr/scalar.rs (3)

194-196: Approved the conversion to async for try_from_raw_fn.

The conversion to asynchronous function is correctly implemented and aligns with the PR's objectives to handle computational heavy tasks more efficiently.


284-286: Consider alternatives to block_on in deserialization.

While the use of block_on here is understood due to the computational heaviness of the task, it's generally recommended to avoid blocking in async contexts as it can lead to performance bottlenecks. Consider if there are alternative approaches that could be used here.
[REFACTOR_SUGGESTion]


Line range hint 317-326: Approved the conversion to async for get_fn_impl.

Converting get_fn_impl to async is appropriate given the I/O bound nature of decoding a function from bytes. This aligns with the PR's objectives to improve asynchronous handling.

src/flow/src/transform/aggr.rs (4)

Line range hint 61-72: Make from_substrait_agg_grouping asynchronous

The function from_substrait_agg_grouping has been correctly refactored to be asynchronous, which aligns with the PR's objective to enhance async handling. The use of .await on TypedExpr::from_substrait_rex is appropriate for handling asynchronous operations within the loop.


Line range hint 314-346: Async handling in from_substrait_agg_rel

The method from_substrait_agg_rel has been refactored to be asynchronous, which is in line with the PR's objectives. The async recursion is correctly applied, and the method handles complex asynchronous calls effectively, such as fetching group expressions and aggregate measures concurrently. The chaining and expansion of results are handled cleanly. However, ensure that the error handling in not_impl_err! for "Aggregate without an input is not supported" is sufficient and that such cases are appropriately managed upstream.


490-492: Ensure error handling in test for missing keys

This test case checks for error conditions when keys are missing, which is crucial for robustness. It's good to see async-await being used effectively in test scenarios to simulate real-world usage.


502-504: Basic functionality test for from_substrait_plan

The test verifies basic functionality and ensures that the async transformation works as expected. The result comparison using assert_eq! is a good practice for unit tests.

@github-actions github-actions bot added the docs-not-required This change does not impact docs. label Jun 25, 2024
Copy link

codecov bot commented Jun 25, 2024

Codecov Report

Attention: Patch coverage is 75.73964% with 41 lines in your changes missing coverage. Please review.

Project coverage is 84.58%. Comparing base (cdd4baf) to head (4c1796d).
Report is 13 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4210      +/-   ##
==========================================
- Coverage   85.05%   84.58%   -0.47%     
==========================================
  Files        1031     1038       +7     
  Lines      181276   183041    +1765     
==========================================
+ Hits       154176   154829     +653     
- Misses      27100    28212    +1112     

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

Outside diff range and nitpick comments (2)
src/flow/src/utils.rs (2)

43-43: Review the removal of serialization traits.

The removal of Serialize and Deserialize traits from KeyExpiryManager could affect serialization and deserialization processes across the system. Ensure that this change aligns with the overall architecture and that any serialization needs are handled appropriately elsewhere.


160-160: Consider the implications of struct changes on existing functionality.

The Arrangement struct now omits Serialize and Deserialize traits. This change should be carefully reviewed to ensure it does not break any existing functionality that relies on these traits, especially in distributed or persisted environments.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4c1af2b and 4c1796d.

Files selected for processing (8)
  • src/flow/src/expr/func.rs (1 hunks)
  • src/flow/src/expr/linear.rs (2 hunks)
  • src/flow/src/expr/relation.rs (2 hunks)
  • src/flow/src/expr/scalar.rs (7 hunks)
  • src/flow/src/plan.rs (2 hunks)
  • src/flow/src/plan/join.rs (3 hunks)
  • src/flow/src/plan/reduce.rs (4 hunks)
  • src/flow/src/utils.rs (2 hunks)
Files skipped from review due to trivial changes (2)
  • src/flow/src/expr/func.rs
  • src/flow/src/plan/reduce.rs
Additional comments not posted (2)
src/flow/src/plan/join.rs (1)

21-21: Serialization traits removed from join-related structs.

The removal of Serialize and Deserialize traits from JoinPlan, JoinFilter, LinearJoinPlan, and LinearStagePlan could impact serialization processes elsewhere in the codebase. Ensure that all serialization requirements are met with alternative implementations if necessary.

Also applies to: 27-27, 40-40, 63-63

src/flow/src/expr/scalar.rs (1)

46-46: Asynchronous function and serialization traits changes in scalar expressions.

The try_from_raw_fn function has been made asynchronous, which aligns with the overall goal of making the system more asynchronous. Additionally, the removal of Serialize and Deserialize traits from TypedExpr and ScalarExpr could impact serialization processes elsewhere in the codebase. Ensure that all serialization requirements are met with alternative implementations if necessary.

Also applies to: 132-132, 194-194, 267-267

Verification successful

Asynchronous function and serialization traits changes in scalar expressions.

The try_from_raw_fn function has been made asynchronous, which aligns with the overall goal of making the system more asynchronous. Additionally, the removal of Serialize and Deserialize traits from TypedExpr and ScalarExpr could impact serialization processes elsewhere in the codebase. However, after an extensive search, no direct or indirect serialization methods for TypedExpr and ScalarExpr were found in the codebase.

  • Ensure that all serialization requirements are met with alternative implementations if necessary.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify if `TypedExpr` and `ScalarExpr` are serialized elsewhere in the codebase.

# Test: Search for serialization of `TypedExpr` and `ScalarExpr`. Expect: No occurrences.
rg --type rust 'TypedExpr\|ScalarExpr' -A 3 | grep -i 'serialize'

Length of output: 65



Script:

#!/bin/bash
# Description: Verify if `TypedExpr` and `ScalarExpr` are serialized indirectly or through custom methods.

# Test: Search for custom serialization implementations for `TypedExpr`.
rg --type rust 'impl.*Serialize.*for.*TypedExpr'

# Test: Search for custom serialization implementations for `ScalarExpr`.
rg --type rust 'impl.*Serialize.*for.*ScalarExpr'

# Test: Search for any references to `TypedExpr` and `ScalarExpr` in context with serialization.
rg --type rust 'TypedExpr\|ScalarExpr' -A 10 -B 10 | grep -i 'serialize'

Length of output: 175

src/flow/src/expr/linear.rs Show resolved Hide resolved
src/flow/src/expr/linear.rs Show resolved Hide resolved
src/flow/src/expr/relation.rs Show resolved Hide resolved
src/flow/src/plan.rs Show resolved Hide resolved
@discord9
Copy link
Contributor Author

@coderabbitai resolve

Copy link
Contributor

coderabbitai bot commented Jun 26, 2024

Actions performed

Comments resolved.

Copy link
Member

@waynexia waynexia left a comment

Choose a reason for hiding this comment

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

DLJB

@discord9 discord9 added this pull request to the merge queue Jun 27, 2024
Merged via the queue into GreptimeTeam:main with commit b6585e3 Jun 27, 2024
53 checks passed
@discord9 discord9 deleted the flow_more_async branch June 27, 2024 17:36
zyy17 pushed a commit to zyy17/greptimedb that referenced this pull request Jun 28, 2024
…GreptimeTeam#4210)

* refactor: use oneshot to receive result

* refactor: make from_substrait_* async

* refacrot: remove serde for plan&expr
zyy17 pushed a commit to zyy17/greptimedb that referenced this pull request Jun 28, 2024
…GreptimeTeam#4210)

* refactor: use oneshot to receive result

* refactor: make from_substrait_* async

* refacrot: remove serde for plan&expr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs-not-required This change does not impact docs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants