Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[ISSUE #1709]🚀Implement BrokerOuterAPI#send_message_to_specific_broker logic🔥 #1712

Merged
merged 2 commits into from
Dec 11, 2024

Conversation

mxsm
Copy link
Owner

@mxsm mxsm commented Dec 11, 2024

Which Issue(s) This PR Fixes(Closes)

Fixes #1709

Brief Description

How Did You Test This Change?

Summary by CodeRabbit

  • New Features

    • Enhanced message sending functionality in the Broker API with improved parameters and error handling.
    • Introduced new methods for building message requests and processing responses.
    • Added a method for creating message request headers with ownership transfer.
  • Bug Fixes

    • Improved error handling and logging for message routing scenarios.
  • Documentation

    • Updated method signatures to reflect new parameters and functionality.

Copy link
Contributor

coderabbitai bot commented Dec 11, 2024

Walkthrough

The changes in this pull request involve modifications to the message sending logic within the RocketMQ broker. Key updates include enhancements to the put_message_to_remote_broker method in the EscapeBridge struct and the send_message_to_specific_broker method in the BrokerOuterAPI struct. These updates introduce new parameters, improve error handling, and refine control flow to ensure messages are sent to the correct broker. Additionally, new utility functions for building message requests and handling responses have been added, along with a new method for creating message headers.

Changes

File Path Change Summary
rocketmq-broker/src/failover/escape_bridge.rs Updated put_message_to_remote_broker method to include broker_name_to_send parameter and improved error handling and control flow.
rocketmq-broker/src/out_api/broker_outer_api.rs Modified send_message_to_specific_broker method to include new parameters; added utility functions for message request handling and response processing.
rocketmq-remoting/src/protocol/header/message_operation_header/send_message_request_header_v2.rs Added create_send_message_request_header_v2_with_move method to facilitate header conversion.

Assessment against linked issues

Objective Addressed Explanation
Implement BrokerOuterAPI#send_message_to_specific_broker logic (#[1709])

Possibly related PRs

Suggested labels

feature, auto merge, ready to review, waiting-review, AI review first

Suggested reviewers

  • TeslaRustor
  • SpaceXCN

Poem

In the land of code where messages fly,
A bridge was built to send them high.
With names and headers, all in line,
Errors now caught, oh how they shine!
So hop along, dear coder friends,
For message magic never ends! 🐰✨


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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 generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@rocketmq-rust-bot
Copy link
Collaborator

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

@rocketmq-rust-robot rocketmq-rust-robot added Difficulty level/Moderate Moderate difficult ISSUE enhancement⚡️ New feature or request feature🚀 Suggest an idea for this project. labels Dec 11, 2024
Copy link

codecov bot commented Dec 11, 2024

Codecov Report

Attention: Patch coverage is 0% with 140 lines in your changes missing coverage. Please review.

Project coverage is 27.90%. Comparing base (49cba24) to head (b36c60d).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
rocketmq-broker/src/out_api/broker_outer_api.rs 0.00% 115 Missing ⚠️
...operation_header/send_message_request_header_v2.rs 0.00% 24 Missing ⚠️
rocketmq-broker/src/failover/escape_bridge.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1712      +/-   ##
==========================================
- Coverage   27.96%   27.90%   -0.06%     
==========================================
  Files         473      473              
  Lines       63702    63838     +136     
==========================================
  Hits        17816    17816              
- Misses      45886    46022     +136     

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

@rocketmq-rust-bot rocketmq-rust-bot merged commit 5eb115a into main Dec 11, 2024
23 of 26 checks passed
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

🧹 Outside diff range and nitpick comments (2)
rocketmq-broker/src/out_api/broker_outer_api.rs (1)

470-475: Update documentation for send_message_to_specific_broker method

The method signature of send_message_to_specific_broker has changed by adding broker_name and updating parameter names. Ensure that the documentation and comments reflect these changes for clarity.

rocketmq-broker/src/failover/escape_bridge.rs (1)

Line range hint 192-199: Handle None case for broker_name_to_send more explicitly

While broker_name_to_send is checked for is_some() earlier, using unwrap() can still lead to panics if the state changes unexpectedly. Consider using pattern matching to handle the None case explicitly.

Modify the code to safely handle broker_name_to_send:

- broker_name_to_send.as_ref().unwrap(),
+ match broker_name_to_send.as_ref() {
+     Some(broker_name) => broker_name,
+     None => {
+         warn!("Broker name to send is None");
+         return None;
+     }
+ },
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 5d3a0c0 and b36c60d.

📒 Files selected for processing (3)
  • rocketmq-broker/src/failover/escape_bridge.rs (1 hunks)
  • rocketmq-broker/src/out_api/broker_outer_api.rs (4 hunks)
  • rocketmq-remoting/src/protocol/header/message_operation_header/send_message_request_header_v2.rs (1 hunks)
🔇 Additional comments (3)
rocketmq-broker/src/out_api/broker_outer_api.rs (1)

476-479: Handle potential None value for uniq_msg_id

uniq_msg_id is obtained using MessageClientIDSetter::get_uniq_id(&msg), which may return None. Ensure that using unwrap_or_default() does not introduce ambiguity if the default unique ID is an empty string.

Run the following script to check for any usage of empty uniq_msg_id in the codebase:

rocketmq-remoting/src/protocol/header/message_operation_header/send_message_request_header_v2.rs (2)

361-363: Possible inconsistency in setting n field

In the create_send_message_request_header_v2_with_move method, the n field is set using v1.topic_request_header.as_ref().and_then(|v| v.get_broker_name().cloned()). Ensure that this correctly retrieves the broker name and handles cases where topic_request_header or broker_name might be None.

Confirm that broker_name is correctly extracted and that there are no cases where it could be unintentionally None.


342-365: Ensure all fields are correctly moved in create_send_message_request_header_v2_with_move

The method create_send_message_request_header_v2_with_move should transfer all fields from SendMessageRequestHeader to SendMessageRequestHeaderV2. Verify that no fields are missed or incorrectly assigned.

Run the following script to compare the fields of both structs and ensure they are all accounted for:

✅ Verification successful

All fields are correctly mapped in the conversion method

Based on the struct definitions and field mappings:

  • All fields from SendMessageRequestHeader are properly moved to their corresponding fields in SendMessageRequestHeaderV2
  • The mapping follows a documented pattern where each field in V2 (a through n) corresponds to a specific field from V1, as shown in the comments
  • The types match between source and destination fields
  • The optional fields are preserved with their correct optionality
  • The additional topic_request_header field is correctly handled by extracting the broker name into field n
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Compare fields of SendMessageRequestHeader and SendMessageRequestHeaderV2

# Extract fields from both structs
grep -E 'pub (.+?):' rocketmq-remoting/src/protocol/header/message_operation_header/send_message_request_header.rs | sed 's/pub //;s/:.*//' > header_fields.txt
grep -E 'pub (.+?):' rocketmq-remoting/src/protocol/header/message_operation_header/send_message_request_header_v2.rs | sed 's/pub //;s/:.*//' > header_v2_fields.txt

# Compare the fields
diff header_fields.txt header_v2_fields.txt

Length of output: 1079


Script:

#!/bin/bash
# Let's examine the struct definitions and field mappings more clearly
echo "=== SendMessageRequestHeader struct definition ==="
rg -A 15 "pub struct SendMessageRequestHeader" rocketmq-remoting/src/protocol/header/message_operation_header/send_message_request_header.rs

echo -e "\n=== SendMessageRequestHeaderV2 struct definition ==="
rg -A 15 "pub struct SendMessageRequestHeaderV2" rocketmq-remoting/src/protocol/header/message_operation_header/send_message_request_header_v2.rs

echo -e "\n=== Field mappings in create_send_message_request_header_v2_with_move ==="
rg -A 20 "create_send_message_request_header_v2_with_move" rocketmq-remoting/src/protocol/header/message_operation_header/send_message_request_header_v2.rs

Length of output: 2576


Script:

#!/bin/bash
# Let's check the complete SendMessageRequestHeader struct definition to ensure we see all fields
echo "=== Complete SendMessageRequestHeader struct definition ==="
rg -A 30 "pub struct SendMessageRequestHeader" rocketmq-remoting/src/protocol/header/message_operation_header/send_message_request_header.rs

# Also check if there are any comments in SendMessageRequestHeaderV2 that might explain field mappings
echo -e "\n=== SendMessageRequestHeaderV2 field documentation ==="
rg -B 1 "pub [a-z]:" rocketmq-remoting/src/protocol/header/message_operation_header/send_message_request_header_v2.rs

Length of output: 1855

Comment on lines +608 to +619
if let Some(trace_on) =
response
.get_ext_fields()
.unwrap()
.get(&CheetahString::from_static_str(
MessageConst::PROPERTY_MSG_REGION,
))
{
send_result.set_trace_on(trace_on == "true");
} else {
send_result.set_trace_on(false);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Incorrect property key used for setting trace_on

The property key used to retrieve the trace_on value should be MessageConst::PROPERTY_TRACE_ON instead of MessageConst::PROPERTY_MSG_REGION.

Apply the following diff to fix the property key:

-if let Some(trace_on) =
-    response
-        .get_ext_fields()
-        .unwrap()
-        .get(&CheetahString::from_static_str(
-            MessageConst::PROPERTY_MSG_REGION,
-        ))
+if let Some(ext_fields) = response.get_ext_fields() {
+    if let Some(trace_on) = ext_fields.get(&CheetahString::from_static_str(
+        MessageConst::PROPERTY_TRACE_ON,
+    )) {
+        send_result.set_trace_on(trace_on == "true");
+    } else {
+        send_result.set_trace_on(false);
+    }
+} else {
+    send_result.set_trace_on(false);
+}
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if let Some(trace_on) =
response
.get_ext_fields()
.unwrap()
.get(&CheetahString::from_static_str(
MessageConst::PROPERTY_MSG_REGION,
))
{
send_result.set_trace_on(trace_on == "true");
} else {
send_result.set_trace_on(false);
}
if let Some(ext_fields) = response.get_ext_fields() {
if let Some(trace_on) = ext_fields.get(&CheetahString::from_static_str(
MessageConst::PROPERTY_TRACE_ON,
)) {
send_result.set_trace_on(trace_on == "true");
} else {
send_result.set_trace_on(false);
}
} else {
send_result.set_trace_on(false);
}

Comment on lines +596 to +605
response
.get_ext_fields()
.unwrap()
.get(&CheetahString::from_static_str(
MessageConst::PROPERTY_MSG_REGION,
))
{
send_result.set_region_id(region_id.to_string());
} else {
send_result.set_region_id(mix_all::DEFAULT_TRACE_REGION_ID.to_string());
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Avoid using unwrap() on get_ext_fields() to prevent potential panics

Using unwrap() on response.get_ext_fields() can cause a panic if ext_fields is None. It's safer to handle the None case explicitly.

Consider modifying the code to handle the None case safely:

-if let Some(region_id) =
-    response
-        .get_ext_fields()
-        .unwrap()
-        .get(&CheetahString::from_static_str(
-            MessageConst::PROPERTY_MSG_REGION,
-        ))
+if let Some(ext_fields) = response.get_ext_fields() {
+    if let Some(region_id) = ext_fields.get(&CheetahString::from_static_str(
+        MessageConst::PROPERTY_MSG_REGION,
+    )) {
+        send_result.set_region_id(region_id.to_string());
+    } else {
+        send_result.set_region_id(mix_all::DEFAULT_TRACE_REGION_ID.to_string());
+    }
+} else {
+    send_result.set_region_id(mix_all::DEFAULT_TRACE_REGION_ID.to_string());
+}
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
response
.get_ext_fields()
.unwrap()
.get(&CheetahString::from_static_str(
MessageConst::PROPERTY_MSG_REGION,
))
{
send_result.set_region_id(region_id.to_string());
} else {
send_result.set_region_id(mix_all::DEFAULT_TRACE_REGION_ID.to_string());
if let Some(ext_fields) = response.get_ext_fields() {
if let Some(region_id) = ext_fields.get(&CheetahString::from_static_str(
MessageConst::PROPERTY_MSG_REGION,
)) {
send_result.set_region_id(region_id.to_string());
} else {
send_result.set_region_id(mix_all::DEFAULT_TRACE_REGION_ID.to_string());
}
} else {
send_result.set_region_id(mix_all::DEFAULT_TRACE_REGION_ID.to_string());
}

@rocketmq-rust-bot rocketmq-rust-bot added approved PR has approved and removed ready to review waiting-review waiting review this PR labels Dec 11, 2024
@mxsm mxsm deleted the feature-1709 branch December 13, 2024 03:09
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
AI review first Ai review pr first approved PR has approved auto merge Difficulty level/Moderate Moderate difficult ISSUE enhancement⚡️ New feature or request feature🚀 Suggest an idea for this project.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature🚀] Implement BrokerOuterAPI#send_message_to_specific_broker logic
4 participants