Skip to content

[ISSUE #750]✨Add client meta data struct#758

Merged
mxsm merged 2 commits intomainfrom
feature-750
Jul 10, 2024
Merged

[ISSUE #750]✨Add client meta data struct#758
mxsm merged 2 commits intomainfrom
feature-750

Conversation

@mxsm
Copy link
Copy Markdown
Owner

@mxsm mxsm commented Jul 10, 2024

Which Issue(s) This PR Fixes(Closes)

Fixes #750

Brief Description

How Did You Test This Change?

Summary by CodeRabbit

  • New Features

    • Added several new modules to handle various RPC operations and client metadata management.
    • Introduced new structs and methods to manage topic route data, broker addresses, and cluster information.
    • Implemented methods for handling RPC requests and message queue invocations.
  • Enhancements

    • Improved MessageQueue struct with additional traits and methods for better manipulation and initialization.
    • Updated RpcRequest and RpcResponse structs with enhanced constructors and field types.
  • Dependency Updates

    • Added log dependency and bytes as a dev-dependency in rocketmq-remoting.
  • Bug Fixes

    • Adjusted visibility of fields in cluster_info.rs to improve access control.

These changes enhance the functionality, flexibility, and usability of the RocketMQ client, providing more robust management of topic routes and RPC operations.

@mxsm
Copy link
Copy Markdown
Owner Author

mxsm commented Jul 10, 2024

🚀Thanks for your contribution🎉. CodeRabbit(AI) will review your code first🔥

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Jul 10, 2024

Walkthrough

The overall changes implement new functionalities and improvements across the RocketMQ project. Key additions include creating a ClientMetadata struct for managing client data, modifying existing structs for enhanced functionality, adding new modules for protocol headers, and extending traits for various structs. These changes aim to enhance code usability, visibility, and maintainability.

Changes

File/Directory Change Summary
rocketmq-common/src/common/message/message_queue.rs Implemented additional traits for MessageQueue, modified the new method, introduced new methods, and adjusted formatting.
rocketmq-remoting/Cargo.toml Added dependencies: log version 0.4.22 and bytes version 1.6.0.
rocketmq-remoting/src/protocol/body/broker_body/cluster_info.rs Made broker_addr_table and cluster_addr_table fields public.
rocketmq-remoting/src/protocol/header/... Added new modules for response headers: get_earliest_msg_storetime_response_header, get_max_offset_response_header, get_min_offset_response_header, and search_offset_response_header.
rocketmq-remoting/src/protocol/route/route_data_view.rs Added traits for BrokerData, QueueData, and TopicRouteData. Added new methods to TopicRouteData.
rocketmq-remoting/src/protocol/static_topic/... Changed scope field in TopicQueueMappingInfo to Option<String>. Added get_mock_broker_name function.
rocketmq-remoting/src/rpc.rs Added new modules: client_metadata, rpc_client, rpc_client_hook, and rpc_client_impl.
rocketmq-remoting/src/rpc/client_metadata.rs Introduced ClientMetadata struct with methods for managing topic routes and broker addresses.
rocketmq-remoting/src/rpc/rpc_client.rs Introduced RpcClientLocal trait with methods for handling RPC requests and message queue invocations.
rocketmq-remoting/src/rpc/rpc_client_hook.rs Introduced RpcClientHook trait with methods for handling RPC requests and responses.
rocketmq-remoting/src/rpc/rpc_client_impl.rs Added methods to RpcClientImpl for handling various RPC operations.
rocketmq-remoting/src/rpc/rpc_client_utils.rs Updated create_command_for_rpc_request to conditionally set the body of RemotingCommand.
rocketmq-remoting/src/rpc/rpc_request.rs Updated RpcRequest struct to include the Send trait in the body field.
rocketmq-remoting/src/rpc/rpc_response.rs Added new constructor methods to RpcResponse struct.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant RpcClientImpl
    participant RocketmqDefaultClient

    Client->>RpcClientImpl: Request RPC operation
    RpcClientImpl->>RocketmqDefaultClient: Invoke remote method
    RocketmqDefaultClient-->>RpcClientImpl: Return response
    RpcClientImpl-->>Client: Return result
Loading

Assessment against linked issues

Objective (Issue #750) Addressed Explanation
Add client metadata struct

Poem

In the land of RocketMQ so bright,
New traits and structs come to light.
With ClientMetadata we steer,
Routes and brokers now so clear.
To every queue and topic's might,
These changes bring us pure delight! 🌟


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.

@TeslaRustor TeslaRustor added approved PR has approved and removed ready to review labels Jul 10, 2024
@mxsm mxsm merged commit 1c43718 into main Jul 10, 2024
@mxsm mxsm deleted the feature-750 branch July 10, 2024 09:04
Copy link
Copy Markdown
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: 8

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 33067a2 and b4d273f.

Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
Files selected for processing (19)
  • rocketmq-common/src/common/message/message_queue.rs (1 hunks)
  • rocketmq-remoting/Cargo.toml (1 hunks)
  • rocketmq-remoting/src/protocol/body/broker_body/cluster_info.rs (1 hunks)
  • rocketmq-remoting/src/protocol/header.rs (1 hunks)
  • rocketmq-remoting/src/protocol/header/get_earliest_msg_storetime_response_header.rs (1 hunks)
  • rocketmq-remoting/src/protocol/header/get_max_offset_response_header.rs (1 hunks)
  • rocketmq-remoting/src/protocol/header/get_min_offset_response_header.rs (1 hunks)
  • rocketmq-remoting/src/protocol/header/search_offset_response_header.rs (1 hunks)
  • rocketmq-remoting/src/protocol/route/route_data_view.rs (4 hunks)
  • rocketmq-remoting/src/protocol/static_topic/topic_queue_info.rs (3 hunks)
  • rocketmq-remoting/src/protocol/static_topic/topic_queue_mapping_utils.rs (2 hunks)
  • rocketmq-remoting/src/rpc.rs (1 hunks)
  • rocketmq-remoting/src/rpc/client_metadata.rs (1 hunks)
  • rocketmq-remoting/src/rpc/rpc_client.rs (1 hunks)
  • rocketmq-remoting/src/rpc/rpc_client_hook.rs (1 hunks)
  • rocketmq-remoting/src/rpc/rpc_client_impl.rs (1 hunks)
  • rocketmq-remoting/src/rpc/rpc_client_utils.rs (1 hunks)
  • rocketmq-remoting/src/rpc/rpc_request.rs (1 hunks)
  • rocketmq-remoting/src/rpc/rpc_response.rs (2 hunks)
Files skipped from review due to trivial changes (2)
  • rocketmq-remoting/Cargo.toml
  • rocketmq-remoting/src/rpc.rs
Additional comments not posted (33)
rocketmq-remoting/src/protocol/header/get_max_offset_response_header.rs (1)

1-24: LGTM!

The GetMaxOffsetResponseHeader struct is well-defined, and the necessary traits are derived correctly. The file complies with the Apache License.

rocketmq-remoting/src/protocol/header/get_min_offset_response_header.rs (1)

1-24: LGTM!

The GetMinOffsetResponseHeader struct is well-defined, and the necessary traits are derived correctly. The file complies with the Apache License.

rocketmq-remoting/src/protocol/header/search_offset_response_header.rs (1)

1-24: LGTM!

The SearchOffsetResponseHeader struct is well-defined, and the necessary traits are derived correctly. The file complies with the Apache License.

rocketmq-remoting/src/protocol/header/get_earliest_msg_storetime_response_header.rs (1)

1-24: LGTM!

The GetEarliestMsgStoretimeResponseHeader struct is well-defined, and the necessary traits are derived correctly. The file complies with the Apache License.

rocketmq-remoting/src/rpc/rpc_request.rs (1)

25-25: LGTM! Ensuring thread-safety with the Send trait.

The addition of the Send trait to the body field and its constructor is crucial for safe multi-threaded operations.

Also applies to: 29-31

rocketmq-remoting/src/rpc/rpc_client_hook.rs (1)

21-28: LGTM! Useful hooks for RPC operations.

The before_request and after_response methods provide useful hooks for custom logic before and after RPC calls, enhancing flexibility.

rocketmq-remoting/src/protocol/header.rs (1)

21-23: LGTM! Enhancing protocol capabilities with new response headers.

The addition of new response header modules enhances the protocol's capability to manage diverse responses effectively.

Also applies to: 30-30

rocketmq-remoting/src/rpc/rpc_client.rs (1)

23-37: LGTM! Enhancing RPC handling with new invocation methods.

The invoke and invoke_mq methods provide robust mechanisms for handling RPC calls, enhancing the client's capability to interact with remote services.

rocketmq-remoting/src/protocol/body/broker_body/cluster_info.rs (2)

29-29: Verify the necessity of making broker_addr_table public.

Making the field public increases accessibility but might expose internal state unnecessarily. Ensure this change is intentional and doesn't violate encapsulation principles.


32-32: Verify the necessity of making cluster_addr_table public.

Making the field public increases accessibility but might expose internal state unnecessarily. Ensure this change is intentional and doesn't violate encapsulation principles.

rocketmq-remoting/src/protocol/static_topic/topic_queue_info.rs (1)

27-27: LGTM! Changing scope to Option<String> enhances flexibility.

The change allows for cases where the scope might be unavailable. The default value ensures proper initialization.

rocketmq-remoting/src/rpc/rpc_client_utils.rs (1)

32-36: LGTM! Conditional body setting in create_command_for_rpc_request enhances robustness.

The change ensures that the body is only set if present, preventing potential errors when the body is None.

rocketmq-common/src/common/message/message_queue.rs (3)

24-25: LGTM! Adding traits to MessageQueue enhances flexibility.

The additional traits (Clone, Hash, Ord, PartialOrd, etc.) increase the flexibility and usability of the MessageQueue struct.


31-78: LGTM! New methods in MessageQueue provide a comprehensive API.

The methods (new, from_other, from_parts, get_topic, set_topic, get_broker_name, set_broker_name, get_queue_id, set_queue_id) offer a complete set of functionalities for creating and managing MessageQueue instances.


80-92: LGTM! fmt::Display and Default implementations enhance usability.

The fmt::Display and Default implementations make it easier to debug and use MessageQueue instances.

rocketmq-remoting/src/rpc/rpc_response.rs (3)

60-68: LGTM! The new_exception method is correctly implemented.

The method initializes an RpcResponse with an optional RpcException, setting the code based on the exception if it exists.


70-81: LGTM! The new method is correctly implemented.

The method initializes an RpcResponse with a code, header, and optional body.


83-90: LGTM! The new_option method is correctly implemented.

The method initializes an RpcResponse with a code and optional body.

rocketmq-remoting/src/protocol/static_topic/topic_queue_mapping_utils.rs (1)

83-95: LGTM! The get_mock_broker_name function is correctly implemented.

The function generates a mock broker name based on the provided scope, handling different cases appropriately.

rocketmq-remoting/src/protocol/route/route_data_view.rs (3)

Line range hint 25-107:
LGTM! The enhancements to the BrokerData struct are correctly implemented.

The additional traits and methods provide useful functionalities for managing broker data.


Line range hint 108-158:
LGTM! The enhancements to the QueueData struct are correctly implemented.

The additional traits and methods provide useful functionalities for managing queue data.


Line range hint 159-207:
LGTM! The enhancements to the TopicRouteData struct are correctly implemented.

The additional traits and methods provide useful functionalities for managing topic route data.

rocketmq-remoting/src/rpc/client_metadata.rs (7)

1-55: LGTM! The ClientMetadata struct and its initialization method are correctly implemented.

The struct definition and the new method correctly initialize the ClientMetadata with default values.


57-87: LGTM! The fresh_topic_route method is correctly implemented.

The method refreshes the topic route data efficiently, updating the broker address table and topic endpoints table.


89-98: LGTM! The get_broker_name_from_message_queue method is correctly implemented.

The method retrieves the broker name from a message queue efficiently.


100-114: LGTM! The refresh_cluster_info method is correctly implemented.

The method refreshes the cluster information efficiently, updating the broker address table.


116-126: LGTM! The find_master_broker_addr method is correctly implemented.

The method finds the master broker address efficiently.


128-212: LGTM! The topic_route_data2endpoints_for_static_topic method is correctly implemented.

The method converts topic route data to endpoints for static topics efficiently.


214-216: LGTM! The broker_addr_table method is correctly implemented.

The method retrieves the broker address table efficiently.

rocketmq-remoting/src/rpc/rpc_client_impl.rs (4)

48-54: LGTM!

The new method correctly initializes the RpcClientImpl struct.


56-61: LGTM!

The register_client_hook method correctly adds a client hook to the list.


63-65: LGTM!

The clear_client_hook method correctly clears the client hook list.


67-77: LGTM!

The get_broker_addr_by_name_or_exception method correctly retrieves the broker address and handles the error case.

Comment on lines +80 to +116
async fn handle_pull_message(
&self,
addr: String,
request: RpcRequest,
timeout_millis: u64,
) -> Result<RpcResponse, RpcException> {
let request_command = RpcClientUtils::create_command_for_rpc_request(request);
match self
.remoting_client
.invoke_async(addr.clone(), request_command, timeout_millis)
.await
{
Ok(response) => match ResponseCode::from(response.code()) {
ResponseCode::Success
| ResponseCode::PullNotFound
| ResponseCode::PullRetryImmediately
| ResponseCode::PullOffsetMoved => {
let response_header = response
.decode_command_custom_header::<PullMessageResponseHeader>()
.unwrap();
let body = response
.body()
.as_ref()
.map(|value| Box::new(value.clone()) as Box<dyn Any>);
let rpc_response = RpcResponse::new(response.code(), response_header, body);
Ok(rpc_response)
}
_ => Ok(RpcResponse::new_exception(Some(RpcException(
response.code(),
"unexpected remote response code".to_string(),
)))),
},
Err(_error) => Err(RpcException(
From::from(ResponseCode::SystemError),
format!("process failed. addr: {}. Request", addr),
)),
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Refactor error handling for better readability.

The nested match statements can be simplified for better readability.

async fn handle_pull_message(
    &self,
    addr: String,
    request: RpcRequest,
    timeout_millis: u64,
) -> Result<RpcResponse, RpcException> {
    let request_command = RpcClientUtils::create_command_for_rpc_request(request);
    let response = self
        .remoting_client
        .invoke_async(addr.clone(), request_command, timeout_millis)
        .await;

    match response {
        Ok(response) => {
            if matches!(
                ResponseCode::from(response.code()),
                ResponseCode::Success
                    | ResponseCode::PullNotFound
                    | ResponseCode::PullRetryImmediately
                    | ResponseCode::PullOffsetMoved
            ) {
                let response_header = response
                    .decode_command_custom_header::<PullMessageResponseHeader>()
                    .unwrap();
                let body = response
                    .body()
                    .as_ref()
                    .map(|value| Box::new(value.clone()) as Box<dyn Any>);
                let rpc_response = RpcResponse::new(response.code(), response_header, body);
                Ok(rpc_response)
            } else {
                Ok(RpcResponse::new_exception(Some(RpcException(
                    response.code(),
                    "unexpected remote response code".to_string(),
                ))))
            }
        }
        Err(_error) => Err(RpcException(
            From::from(ResponseCode::SystemError),
            format!("process failed. addr: {}. Request", addr),
        )),
    }
}

Comment on lines +119 to +152
async fn handle_get_min_offset(
&self,
addr: String,
request: RpcRequest,
timeout_millis: u64,
) -> Result<RpcResponse, RpcException> {
let request_command = RpcClientUtils::create_command_for_rpc_request(request);
match self
.remoting_client
.invoke_async(addr.clone(), request_command, timeout_millis)
.await
{
Ok(response) => match ResponseCode::from(response.code()) {
ResponseCode::Success => {
let response_header = response
.decode_command_custom_header::<GetMinOffsetResponseHeader>()
.unwrap();
let body = response
.body()
.as_ref()
.map(|value| Box::new(value.clone()) as Box<dyn Any>);
let rpc_response = RpcResponse::new(response.code(), response_header, body);
Ok(rpc_response)
}
_ => Ok(RpcResponse::new_exception(Some(RpcException(
response.code(),
"unknown remote error".to_string(),
)))),
},
Err(_error) => Err(RpcException(
From::from(ResponseCode::SystemError),
format!("process failed. addr: {}. Request", addr),
)),
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Refactor error handling for better readability.

The nested match statements can be simplified for better readability.

async fn handle_get_min_offset(
    &self,
    addr: String,
    request: RpcRequest,
    timeout_millis: u64,
) -> Result<RpcResponse, RpcException> {
    let request_command = RpcClientUtils::create_command_for_rpc_request(request);
    let response = self
        .remoting_client
        .invoke_async(addr.clone(), request_command, timeout_millis)
        .await;

    match response {
        Ok(response) => {
            if ResponseCode::from(response.code()) == ResponseCode::Success {
                let response_header = response
                    .decode_command_custom_header::<GetMinOffsetResponseHeader>()
                    .unwrap();
                let body = response
                    .body()
                    .as_ref()
                    .map(|value| Box::new(value.clone()) as Box<dyn Any>);
                let rpc_response = RpcResponse::new(response.code(), response_header, body);
                Ok(rpc_response)
            } else {
                Ok(RpcResponse::new_exception(Some(RpcException(
                    response.code(),
                    "unknown remote error".to_string(),
                ))))
            }
        }
        Err(_error) => Err(RpcException(
            From::from(ResponseCode::SystemError),
            format!("process failed. addr: {}. Request", addr),
        )),
    }
}

Comment on lines +154 to +187
async fn handle_get_max_offset(
&self,
addr: String,
request: RpcRequest,
timeout_millis: u64,
) -> Result<RpcResponse, RpcException> {
let request_command = RpcClientUtils::create_command_for_rpc_request(request);
match self
.remoting_client
.invoke_async(addr.clone(), request_command, timeout_millis)
.await
{
Ok(response) => match ResponseCode::from(response.code()) {
ResponseCode::Success => {
let response_header = response
.decode_command_custom_header::<GetMaxOffsetResponseHeader>()
.unwrap();
let body = response
.body()
.as_ref()
.map(|value| Box::new(value.clone()) as Box<dyn Any>);
let rpc_response = RpcResponse::new(response.code(), response_header, body);
Ok(rpc_response)
}
_ => Ok(RpcResponse::new_exception(Some(RpcException(
response.code(),
"unknown remote error".to_string(),
)))),
},
Err(_error) => Err(RpcException(
From::from(ResponseCode::SystemError),
format!("process failed. addr: {}. Request", addr),
)),
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Refactor error handling for better readability.

The nested match statements can be simplified for better readability.

async fn handle_get_max_offset(
    &self,
    addr: String,
    request: RpcRequest,
    timeout_millis: u64,
) -> Result<RpcResponse, RpcException> {
    let request_command = RpcClientUtils::create_command_for_rpc_request(request);
    let response = self
        .remoting_client
        .invoke_async(addr.clone(), request_command, timeout_millis)
        .await;

    match response {
        Ok(response) => {
            if ResponseCode::from(response.code()) == ResponseCode::Success {
                let response_header = response
                    .decode_command_custom_header::<GetMaxOffsetResponseHeader>()
                    .unwrap();
                let body = response
                    .body()
                    .as_ref()
                    .map(|value| Box::new(value.clone()) as Box<dyn Any>);
                let rpc_response = RpcResponse::new(response.code(), response_header, body);
                Ok(rpc_response)
            } else {
                Ok(RpcResponse::new_exception(Some(RpcException(
                    response.code(),
                    "unknown remote error".to_string(),
                ))))
            }
        }
        Err(_error) => Err(RpcException(
            From::from(ResponseCode::SystemError),
            format!("process failed. addr: {}. Request", addr),
        )),
    }
}

Comment on lines +189 to +222
async fn handle_search_offset(
&self,
addr: String,
request: RpcRequest,
timeout_millis: u64,
) -> Result<RpcResponse, RpcException> {
let request_command = RpcClientUtils::create_command_for_rpc_request(request);
match self
.remoting_client
.invoke_async(addr.clone(), request_command, timeout_millis)
.await
{
Ok(response) => match ResponseCode::from(response.code()) {
ResponseCode::Success => {
let response_header = response
.decode_command_custom_header::<SearchOffsetResponseHeader>()
.unwrap();
let body = response
.body()
.as_ref()
.map(|value| Box::new(value.clone()) as Box<dyn Any>);
let rpc_response = RpcResponse::new(response.code(), response_header, body);
Ok(rpc_response)
}
_ => Ok(RpcResponse::new_exception(Some(RpcException(
response.code(),
"unknown remote error".to_string(),
)))),
},
Err(_error) => Err(RpcException(
From::from(ResponseCode::SystemError),
format!("process failed. addr: {}. Request", addr),
)),
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Refactor error handling for better readability.

The nested match statements can be simplified for better readability.

async fn handle_search_offset(
    &self,
    addr: String,
    request: RpcRequest,
    timeout_millis: u64,
) -> Result<RpcResponse, RpcException> {
    let request_command = RpcClientUtils::create_command_for_rpc_request(request);
    let response = self
        .remoting_client
        .invoke_async(addr.clone(), request_command, timeout_millis)
        .await;

    match response {
        Ok(response) => {
            if ResponseCode::from(response.code()) == ResponseCode::Success {
                let response_header = response
                    .decode_command_custom_header::<SearchOffsetResponseHeader>()
                    .unwrap();
                let body = response
                    .body()
                    .as_ref()
                    .map(|value| Box::new(value.clone()) as Box<dyn Any>);
                let rpc_response = RpcResponse::new(response.code(), response_header, body);
                Ok(rpc_response)
            } else {
                Ok(RpcResponse::new_exception(Some(RpcException(
                    response.code(),
                    "unknown remote error".to_string(),
                ))))
            }
        }
        Err(_error) => Err(RpcException(
            From::from(ResponseCode::SystemError),
            format!("process failed. addr: {}. Request", addr),
        )),
    }
}

Comment on lines +224 to +257
async fn handle_get_earliest_msg_storetime(
&self,
addr: String,
request: RpcRequest,
timeout_millis: u64,
) -> Result<RpcResponse, RpcException> {
let request_command = RpcClientUtils::create_command_for_rpc_request(request);
match self
.remoting_client
.invoke_async(addr.clone(), request_command, timeout_millis)
.await
{
Ok(response) => match ResponseCode::from(response.code()) {
ResponseCode::Success => {
let response_header = response
.decode_command_custom_header::<GetEarliestMsgStoretimeResponseHeader>()
.unwrap();
let body = response
.body()
.as_ref()
.map(|value| Box::new(value.clone()) as Box<dyn Any>);
let rpc_response = RpcResponse::new(response.code(), response_header, body);
Ok(rpc_response)
}
_ => Ok(RpcResponse::new_exception(Some(RpcException(
response.code(),
"unknown remote error".to_string(),
)))),
},
Err(_error) => Err(RpcException(
From::from(ResponseCode::SystemError),
format!("process failed. addr: {}. Request", addr),
)),
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Refactor error handling for better readability.

The nested match statements can be simplified for better readability.

async fn handle_get_earliest_msg_storetime(
    &self,
    addr: String,
    request: RpcRequest,
    timeout_millis: u64,
) -> Result<RpcResponse, RpcException> {
    let request_command = RpcClientUtils::create_command_for_rpc_request(request);
    let response = self
        .remoting_client
        .invoke_async(addr.clone(), request_command, timeout_millis)
        .await;

    match response {
        Ok(response) => {
            if ResponseCode::from(response.code()) == ResponseCode::Success {
                let response_header = response
                    .decode_command_custom_header::<GetEarliestMsgStoretimeResponseHeader>()
                    .unwrap();
                let body = response
                    .body()
                    .as_ref()
                    .map(|value| Box::new(value.clone()) as Box<dyn Any>);
                let rpc_response = RpcResponse::new(response.code(), response_header, body);
                Ok(rpc_response)
            } else {
                Ok(RpcResponse::new_exception(Some(RpcException(
                    response.code(),
                    "unknown remote error".to_string(),
                ))))
            }
        }
        Err(_error) => Err(RpcException(
            From::from(ResponseCode::SystemError),
            format!("process failed. addr: {}. Request", addr),
        )),
    }
}

Comment on lines +259 to +296
async fn handle_query_consumer_offset(
&self,
addr: String,
request: RpcRequest,
timeout_millis: u64,
) -> Result<RpcResponse, RpcException> {
let request_command = RpcClientUtils::create_command_for_rpc_request(request);
match self
.remoting_client
.invoke_async(addr.clone(), request_command, timeout_millis)
.await
{
Ok(response) => match ResponseCode::from(response.code()) {
ResponseCode::Success => {
let response_header = response
.decode_command_custom_header::<QueryConsumerOffsetResponseHeader>()
.unwrap();
let body = response
.body()
.as_ref()
.map(|value| Box::new(value.clone()) as Box<dyn Any>);
let rpc_response = RpcResponse::new(response.code(), response_header, body);
Ok(rpc_response)
}
ResponseCode::QueryNotFound => {
let rpc_response = RpcResponse::new_option(response.code(), None);
Ok(rpc_response)
}
_ => Ok(RpcResponse::new_exception(Some(RpcException(
response.code(),
"unknown remote error".to_string(),
)))),
},
Err(_error) => Err(RpcException(
From::from(ResponseCode::SystemError),
format!("process failed. addr: {}. Request", addr),
)),
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Refactor error handling for better readability.

The nested match statements can be simplified for better readability.

async fn handle_query_consumer_offset(
    &self,
    addr: String,
    request: RpcRequest,
    timeout_millis: u64,
) -> Result<RpcResponse, RpcException> {
    let request_command = RpcClientUtils::create_command_for_rpc_request(request);
    let response = self
        .remoting_client
        .invoke_async(addr.clone(), request_command, timeout_millis)
        .await;

    match response {
        Ok(response) => {
            match ResponseCode::from(response.code()) {
                ResponseCode::Success => {
                    let response_header = response
                        .decode_command_custom_header::<QueryConsumerOffsetResponseHeader>()
                        .unwrap();
                    let body = response
                        .body()
                        .as_ref()
                        .map(|value| Box::new(value.clone()) as Box<dyn Any>);
                    let rpc_response = RpcResponse::new(response.code(), response_header, body);
                    Ok(rpc_response)
                }
                ResponseCode::QueryNotFound => {
                    let rpc_response = RpcResponse::new_option(response.code(), None);
                    Ok(rpc_response)
                }
                _ => Ok(RpcResponse::new_exception(Some(RpcException(
                    response.code(),
                    "unknown remote error".to_string(),
                )))),
            }
        }
        Err(_error) => Err(RpcException(
            From::from(ResponseCode::SystemError),
            format!("process failed. addr: {}. Request", addr),
        )),
    }
}

Comment on lines +298 to +331
async fn handle_update_consumer_offset(
&self,
addr: String,
request: RpcRequest,
timeout_millis: u64,
) -> Result<RpcResponse, RpcException> {
let request_command = RpcClientUtils::create_command_for_rpc_request(request);
match self
.remoting_client
.invoke_async(addr.clone(), request_command, timeout_millis)
.await
{
Ok(response) => match ResponseCode::from(response.code()) {
ResponseCode::Success => {
let response_header = response
.decode_command_custom_header::<UpdateConsumerOffsetResponseHeader>()
.unwrap();
let body = response
.body()
.as_ref()
.map(|value| Box::new(value.clone()) as Box<dyn Any>);
let rpc_response = RpcResponse::new(response.code(), response_header, body);
Ok(rpc_response)
}
_ => Ok(RpcResponse::new_exception(Some(RpcException(
response.code(),
"unknown remote error".to_string(),
)))),
},
Err(_error) => Err(RpcException(
From::from(ResponseCode::SystemError),
format!("process failed. addr: {}. Request", addr),
)),
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Refactor error handling for better readability.

The nested match statements can be simplified for better readability.

async fn handle_update_consumer_offset(
    &self,
    addr: String,
    request: RpcRequest,
    timeout_millis: u64,
) -> Result<RpcResponse, RpcException> {
    let request_command = RpcClientUtils::create_command_for_rpc_request(request);
    let response = self
        .remoting_client
        .invoke_async(addr.clone(), request_command, timeout_millis)
        .await;

    match response {
        Ok(response) => {
            if ResponseCode::from(response.code()) == ResponseCode::Success {
                let response_header = response
                    .decode_command_custom_header::<UpdateConsumerOffsetResponseHeader>()
                    .unwrap();
                let body = response
                    .body()
                    .as_ref()
                    .map(|value| Box::new(value.clone()) as Box<dyn Any>);
                let rpc_response = RpcResponse::new(response.code(), response_header, body);
                Ok(rpc_response)
            } else {
                Ok(RpcResponse::new_exception(Some(RpcException(
                    response.code(),
                    "unknown remote error".to_string(),
                ))))
            }
        }
        Err(_error) => Err(RpcException(
            From::from(ResponseCode::SystemError),
            format!("process failed. addr: {}. Request", addr),
        )),
    }
}

Comment on lines +333 to +363
async fn handle_common_body_request(
&self,
addr: String,
request: RpcRequest,
timeout_millis: u64,
) -> Result<RpcResponse, RpcException> {
let request_command = RpcClientUtils::create_command_for_rpc_request(request);
match self
.remoting_client
.invoke_async(addr.clone(), request_command, timeout_millis)
.await
{
Ok(response) => match ResponseCode::from(response.code()) {
ResponseCode::Success => {
let body = response
.body()
.as_ref()
.map(|value| Box::new(value.clone()) as Box<dyn Any>);
let rpc_response = RpcResponse::new_option(response.code(), body);
Ok(rpc_response)
}
_ => Ok(RpcResponse::new_exception(Some(RpcException(
response.code(),
"unknown remote error".to_string(),
)))),
},
Err(_error) => Err(RpcException(
From::from(ResponseCode::SystemError),
format!("process failed. addr: {}. Request", addr),
)),
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Refactor error handling for better readability.

The nested match statements can be simplified for better readability.

async fn handle_common_body_request(
    &self,
    addr: String,
    request: RpcRequest,
    timeout_millis: u64,
) -> Result<RpcResponse, RpcException> {
    let request_command = RpcClientUtils::create_command_for_rpc_request(request);
    let response = self
        .remoting_client
        .invoke_async(addr.clone(), request_command, timeout_millis)
        .await;

    match response {
        Ok(response) => {
            if ResponseCode::from(response.code()) == ResponseCode::Success {
                let body = response

<!-- This is an auto-generated comment by CodeRabbit -->

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

Labels

approved PR has approved auto merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature🚀] Add client meta data struct

2 participants