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: RpcInvokeCallbackListener resolve response #336

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

JoeCqupt
Copy link
Contributor

@JoeCqupt JoeCqupt commented Jan 10, 2024

Why make this change?

The code for resolve response is duplicated。

replace duplicated code by using RpcResponseResolver.resolveResponseObject method

Summary by CodeRabbit

  • New Features

    • Implemented a new workflow for building and releasing the Java project using Maven.
    • Added new methods for writing, flushing, and cloning RemotingContext instances.
  • Bug Fixes

    • Fixed the isFine() method in Connection to properly check the closed status.
  • Improvements

    • Refactored CallbackTask class to enhance exception handling, error messages, and callback invocation logic.
    • Updated various dependency versions in pom.xml for improved stability and performance.
    • Renamed variables in BaseRemoting for better clarity.
    • Added thread safety in SerializerManager.
  • Tests

    • Added new test methods to improve coverage for connection handling, exception testing, and RPC command processing.

@JoeCqupt
Copy link
Contributor Author

JoeCqupt commented Jan 10, 2024

@chuailiwu PTAL

@OrezzerO @nobodyiam

Copy link

coderabbitai bot commented May 28, 2024

Walkthrough

The recent changes encompass a variety of improvements and additions across multiple files, focusing on enhancing callback handling, updating dependencies, refining connection status checks, and adding new test cases. Key modifications include refactoring the CallbackTask class for better error handling and callback logic, updating Maven dependencies in pom.xml, and introducing new methods and tests to ensure robust functionality and improved code coverage.

Changes

File(s) Summary of Changes
src/.../RpcInvokeCallbackListener.java Refactored CallbackTask class to improve exception handling, error messages, and callback logic.
.github/workflows/release.yml Added a new GitHub Actions workflow for building and releasing the project using Maven.
pom.xml Updated versions of several dependencies and plugins.
src/.../BaseRemoting.java Renamed defalutCommandFactory to commandFactory and updated related methods.
src/.../Connection.java Enhanced isFine() method to include a check for the closed status.
src/.../InvokeContext.java Added RecordContext field and related method.
src/.../RemotingContext.java Implemented Cloneable interface and added write, flush, and clone methods.
src/.../RpcCommandHandler.java Modified run method to clone ctx before processing.
src/.../RpcRequestProcessor.java Added try-finally block for recording start and stop, and handled exceptions during response serialization.
src/.../SerializerManager.java Introduced ReentrantLock for thread safety when retrieving serializers.
src/test/.../ConnectionTest.java Added testIsFine() method to enhance connection handling tests.
src/test/.../ExceptionTest.java Introduced testGetBizClassLoaderException1() to test exception handling in getBizClassLoader().
src/test/.../RpcCommandHandlerTest.java Added testHandleCommand() and MockUserProcessors class to test RPC command handling.
src/test/.../ClassCustomSerializerTest.java Added testResponseSerialThrowable() for testing Throwable serialization during response.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant User
    participant RpcCommandHandler
    participant RemotingContext
    participant ProcessMethod

    User->>RpcCommandHandler: Invoke run()
    RpcCommandHandler->>RemotingContext: Clone ctx
    RpcCommandHandler->>ProcessMethod: Call process(ctx.clone(), m)
    ProcessMethod-->>RpcCommandHandler: Return result
    RpcCommandHandler-->>User: Return response
Loading

Poem

In lines of code, where logic flows,
Callbacks dance, and error shows.
Dependencies updated, secure and tight,
Connections checked with all their might.
Tests abound, ensuring grace,
In our code, no flaws we trace.
🐇✨ Code refined, a joyous sight!

Warning

Review ran into problems

Problems (1)
  • Git: Failed to clone repository. Please contact CodeRabbit support.

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

@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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 829be55 and 206f2e4.
Files selected for processing (1)
  • src/main/java/com/alipay/remoting/rpc/RpcInvokeCallbackListener.java (1 hunks)
Additional comments not posted (1)
src/main/java/com/alipay/remoting/rpc/RpcInvokeCallbackListener.java (1)

136-147: Ensure thread safety when modifying the response object.

synchronized (response) {
    Object responseObj = RpcResponseResolver.resolveResponseObject(response, this.remoteAddress);
    response.setInvokeContext(future.getInvokeContext());
    callback.onResponse(responseObj);
}

This modification ensures that changes to the response object are thread-safe, which is crucial in a multi-threaded environment.

Copy link

@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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 206f2e4 and f6dd49e.
Files selected for processing (1)
  • src/main/java/com/alipay/remoting/rpc/RpcInvokeCallbackListener.java (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/main/java/com/alipay/remoting/rpc/RpcInvokeCallbackListener.java

Copy link

@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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between f6dd49e and a22ee85.
Files selected for processing (1)
  • src/main/java/com/alipay/remoting/rpc/RpcInvokeCallbackListener.java (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/main/java/com/alipay/remoting/rpc/RpcInvokeCallbackListener.java

Copy link

@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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between a22ee85 and 8187102.
Files selected for processing (1)
  • src/main/java/com/alipay/remoting/rpc/RpcInvokeCallbackListener.java (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/main/java/com/alipay/remoting/rpc/RpcInvokeCallbackListener.java

@JoeCqupt
Copy link
Contributor Author

JoeCqupt commented Jun 4, 2024

It's amazing, I feel like this CI will randomly fail

Copy link

@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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8187102 and c84504c.

Files selected for processing (13)
  • .github/workflows/release.yml (1 hunks)
  • pom.xml (5 hunks)
  • src/main/java/com/alipay/remoting/BaseRemoting.java (2 hunks)
  • src/main/java/com/alipay/remoting/Connection.java (1 hunks)
  • src/main/java/com/alipay/remoting/InvokeContext.java (3 hunks)
  • src/main/java/com/alipay/remoting/RemotingContext.java (3 hunks)
  • src/main/java/com/alipay/remoting/rpc/protocol/RpcCommandHandler.java (1 hunks)
  • src/main/java/com/alipay/remoting/rpc/protocol/RpcRequestProcessor.java (4 hunks)
  • src/main/java/com/alipay/remoting/serialization/SerializerManager.java (2 hunks)
  • src/test/java/com/alipay/remoting/inner/connection/ConnectionTest.java (2 hunks)
  • src/test/java/com/alipay/remoting/rpc/exception/ExceptionTest.java (1 hunks)
  • src/test/java/com/alipay/remoting/rpc/protocol/RpcCommandHandlerTest.java (1 hunks)
  • src/test/java/com/alipay/remoting/rpc/serializer/ClassCustomSerializerTest.java (3 hunks)
Files skipped from review due to trivial changes (2)
  • .github/workflows/release.yml
  • pom.xml
Additional comments not posted (20)
src/main/java/com/alipay/remoting/serialization/SerializerManager.java (2)

19-19: Import and declaration of ReentrantLock are appropriate for ensuring thread safety.

Also applies to: 37-37


40-53: Thread safety improvements in getSerializer method are correctly implemented using ReentrantLock.

src/test/java/com/alipay/remoting/rpc/protocol/RpcCommandHandlerTest.java (2)

55-71: The new test method testHandleCommand is well-structured and correctly tests command handling with asynchronous behavior.


73-143: The MockUserProcessors class is correctly implemented and serves its purpose in the test setup.

src/test/java/com/alipay/remoting/inner/connection/ConnectionTest.java (1)

128-133: The new test method testIsFine correctly tests the connection's fine-check logic after closing the connection.

src/main/java/com/alipay/remoting/InvokeContext.java (3)

76-85: Addition of RecordContext field and its initialization in the constructor are correctly implemented to enhance functionality.


137-137: Changes to the clear method, including resetting the RecordContext, are appropriate for ensuring consistency in the state of InvokeContext.


146-148: The getRecordContext method is correctly implemented to provide access to the RecordContext field.

src/main/java/com/alipay/remoting/RemotingContext.java (2)

121-131: The addition of write and flush methods in RemotingContext correctly abstracts lower-level channel operations, enhancing the usability of the class.


260-262: The clone method in RemotingContext is correctly implemented to provide a shallow copy of the context, suitable for its intended use.

src/main/java/com/alipay/remoting/rpc/protocol/RpcCommandHandler.java (1)

108-108: Cloning the ctx object in RpcCommandHandler before processing commands is a crucial change for ensuring that the original context remains unaltered, enhancing thread safety.

src/test/java/com/alipay/remoting/rpc/exception/ExceptionTest.java (1)

345-365: The new test method testGetBizClassLoaderException1 effectively tests the exception handling when getBizClassLoader throws a RuntimeException. This is a good addition to ensure robustness in error scenarios.

src/main/java/com/alipay/remoting/Connection.java (1)

160-160: The addition of the !closed.get() condition in the isFine method is a crucial improvement. It ensures that the connection's closed state is checked along with its active state, which is essential for accurate connection health checks.

src/main/java/com/alipay/remoting/BaseRemoting.java (1)

44-47: The change from a private to a protected commandFactory and its initialization in the constructor enhances the flexibility and extensibility of the BaseRemoting class. This allows subclasses to directly access and utilize the commandFactory, which is a good design decision for future enhancements and customizations.

src/main/java/com/alipay/remoting/rpc/protocol/RpcRequestProcessor.java (4)

23-25: The addition of imports for RecordContext, RecordScene, and RecorderManager indicates an enhancement in the monitoring or logging capabilities of the RpcRequestProcessor. This is a positive change as it likely supports more detailed or context-aware logging or metrics collection.


137-156: The introduction of recording logic using RecorderManager in the doProcess method is a significant enhancement. It allows for detailed tracking and analysis of request handling, which can be crucial for performance tuning and debugging. The use of try-finally ensures that recording stops even if an exception occurs, which is a good practice for maintaining accurate metrics.


193-198: The nested try-catch block inside the catch for serialization exceptions ensures that even if serialization of the exception response fails, the error is logged. This is a robust error handling strategy that prevents loss of error information, crucial for diagnosing issues in production environments.


415-419: The detailed error logging and response handling in the ProcessTask class's run method is well-implemented. It ensures that any exceptions during the processing of RPC requests are not only logged with detailed context but also communicated back to the caller, enhancing the reliability and debuggability of the system.

src/test/java/com/alipay/remoting/rpc/serializer/ClassCustomSerializerTest.java (2)

23-23: Added imports for AtomicBoolean, DefaultCustomSerializer, ResponseCommand, InvokeServerException, and RpcServerException.

These imports are necessary for the new test method testResponseSerialThrowable to function correctly.

Also applies to: 25-28


309-346: New test method testResponseSerialThrowable added to handle serialization exceptions for response objects.

This test method effectively checks the serialization process by intentionally throwing a StackOverflowError to simulate an error scenario. It's a good practice to include such robustness tests. However, ensure that the StackOverflowError is a deliberate choice for simulating serialization errors, as it typically indicates very different issues (such as infinite recursion).

Copy link

@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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8187102 and 9f4499e.

Files selected for processing (1)
  • src/main/java/com/alipay/remoting/rpc/RpcInvokeCallbackListener.java (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/main/java/com/alipay/remoting/rpc/RpcInvokeCallbackListener.java

@JoeCqupt
Copy link
Contributor Author

这个CI有问题,线下跑的单测没问题,CI跑的就报错

线下:
image

CI:
https://github.com/sofastack/sofa-bolt/actions/runs/9377858022/job/25820076402?pr=336#step:5:1472
image

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

Successfully merging this pull request may close these issues.

2 participants