Skip to content

[ISSUE #3534]🚀Implement connection handling in HAConnection methods with error logging#3535

Merged
rocketmq-rust-bot merged 1 commit intomainfrom
feature-3534
Jun 26, 2025
Merged

[ISSUE #3534]🚀Implement connection handling in HAConnection methods with error logging#3535
rocketmq-rust-bot merged 1 commit intomainfrom
feature-3534

Conversation

@mxsm
Copy link
Owner

@mxsm mxsm commented Jun 26, 2025

Which Issue(s) This PR Fixes(Closes)

Fixes #3534

Brief Description

How Did You Test This Change?

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of high-availability connections by ensuring all connection methods handle missing connections gracefully and provide appropriate warnings or errors.

Copilot AI review requested due to automatic review settings June 26, 2025 03:32
@rocketmq-rust-robot rocketmq-rust-robot added the feature🚀 Suggest an idea for this project. label Jun 26, 2025
@rocketmq-rust-bot
Copy link
Collaborator

🔊@mxsm 🚀Thanks for your contribution🎉!

💡CodeRabbit(AI) will review your code first🔥!

Note

🚨The code review suggestions from CodeRabbit are to be used as a reference only, and the PR submitter can decide whether to make changes based on their own judgment. Ultimately, the project management personnel will conduct the final code review💥.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 26, 2025

Walkthrough

The implementation of the HAConnection trait for GeneralHAConnection in rocketmq-store/src/ha/general_ha_connection.rs is now complete. All previously unimplemented methods have been filled in, each delegating to either default_ha_connection or auto_switch_ha_connection as appropriate, with consistent error handling and logging for absent connections.

Changes

File(s) Change Summary
rocketmq-store/src/ha/general_ha_connection.rs Completed all HAConnection trait methods for GeneralHAConnection, replacing todo!() with logic delegating to internal connections, and added error handling and logging for missing connections.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant GeneralHAConnection
    participant DefaultHAConnection
    participant AutoSwitchHAConnection

    Client->>GeneralHAConnection: Call HAConnection method
    alt default_ha_connection is present
        GeneralHAConnection->>DefaultHAConnection: Delegate method call
        DefaultHAConnection-->>GeneralHAConnection: Return result
    else auto_switch_ha_connection is present
        GeneralHAConnection->>AutoSwitchHAConnection: Delegate method call
        AutoSwitchHAConnection-->>GeneralHAConnection: Return result
    else neither is present
        GeneralHAConnection->>GeneralHAConnection: Log warning / error
        GeneralHAConnection-->>Client: Return error or panic
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Implement connection handling in HAConnection methods with error logging (#3534)

Possibly related PRs

Poem

In tunnels of code, connections now flow,
No more todo!s, just logic to show.
Delegation is wise, with errors well caught,
Logging each warning, as every dev ought.
The rabbits all cheer, their work now complete—
HA connections robust, elegant, neat!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

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.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements connection handling for the GeneralHAConnection by replacing unimplemented todo!() stubs with logic that delegates to either the default or auto-switch HA connection, adds warning logs when no connection is available, and panics in getter methods if neither connection exists.

  • Delegates shutdown, close, and various getters to either default_ha_connection or auto_switch_ha_connection
  • Adds tracing::warn! logs on the (None, None) path
  • Introduces panic! in getter methods when no HA connection is available
Comments suppressed due to low confidence (2)

rocketmq-store/src/ha/general_ha_connection.rs:107

  • Avoid panicking in library code; consider returning a Result or Option to propagate the absence of an HA connection instead of aborting execution.
                panic!("No HA connection available");

rocketmq-store/src/ha/general_ha_connection.rs:78

  • Add unit tests covering all match branches (especially the None, None path) to verify that warnings are logged and the behavior is correct.
    async fn shutdown(&mut self) {


async fn shutdown(&mut self) {
todo!()
match (
Copy link

Copilot AI Jun 26, 2025

Choose a reason for hiding this comment

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

[nitpick] The same match logic is repeated across multiple methods; consider extracting a helper function or macro to reduce duplication and improve maintainability.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
rocketmq-store/src/ha/general_ha_connection.rs (1)

78-165: Consider refactoring to reduce code duplication and improve maintainability.

While the implementation correctly completes the trait requirements, the significant code duplication across getter methods could be reduced with a helper function or macro.

Consider extracting the common delegation pattern:

+    fn delegate_or_panic<T, F>(&self, operation_name: &str, operation: F) -> T
+    where
+        F: Fn(&DefaultHAConnection) -> T + Fn(&AutoSwitchHAConnection) -> T,
+    {
+        match (&self.default_ha_connection, &self.auto_switch_ha_connection) {
+            (Some(connection), _) => operation(connection),
+            (_, Some(connection)) => operation(connection),
+            (None, None) => {
+                tracing::warn!("No HA connection to {}", operation_name);
+                panic!("No HA connection available");
+            }
+        }
+    }

Also consider documenting that default_ha_connection takes priority over auto_switch_ha_connection when both are present.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 03e9b58 and c341a3d.

📒 Files selected for processing (1)
  • rocketmq-store/src/ha/general_ha_connection.rs (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
rocketmq-store/src/ha/general_ha_connection.rs (5)
rocketmq-store/src/ha/auto_switch/auto_switch_ha_connection.rs (7)
  • close (35-37)
  • get_socket (39-41)
  • get_current_state (43-45)
  • get_client_address (47-49)
  • get_transferred_byte_in_second (51-53)
  • get_transfer_from_where (55-57)
  • get_slave_ack_offset (59-61)
rocketmq-store/src/ha/ha_connection.rs (7)
  • close (38-38)
  • get_socket (44-44)
  • get_current_state (50-50)
  • get_client_address (56-56)
  • get_transferred_byte_in_second (62-62)
  • get_transfer_from_where (68-68)
  • get_slave_ack_offset (74-74)
rocketmq-store/src/ha/default_ha_connection.rs (7)
  • close (189-196)
  • get_socket (198-200)
  • get_current_state (202-204)
  • get_client_address (206-208)
  • get_transferred_byte_in_second (210-212)
  • get_transfer_from_where (214-220)
  • get_slave_ack_offset (222-224)
rocketmq-store/src/ha/auto_switch/auto_switch_ha_client.rs (2)
  • get_current_state (60-62)
  • get_transferred_byte_in_second (72-74)
rocketmq-store/src/ha/general_ha_client.rs (2)
  • get_current_state (109-111)
  • get_transferred_byte_in_second (121-123)
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: build (windows-latest, stable)
  • GitHub Check: build (ubuntu-latest, stable)
  • GitHub Check: build (macos-latest, stable)
  • GitHub Check: build (windows-latest, nightly)
  • GitHub Check: build (macos-latest, nightly)
  • GitHub Check: test
  • GitHub Check: build (ubuntu-latest, nightly)
  • GitHub Check: auto-approve
  • GitHub Check: build
🔇 Additional comments (2)
rocketmq-store/src/ha/general_ha_connection.rs (2)

78-89: Good implementation with proper async handling.

The delegation logic is correct and handles the case where no connection is available appropriately. However, consider the inconsistency with the start method which returns an error in similar circumstances.


91-99: Consistent implementation with appropriate warning logging.

The synchronous delegation pattern matches the method signature and handles the no-connection case gracefully.

Comment on lines 101 to 110
fn get_socket(&self) -> &TcpStream {
todo!()
match (&self.default_ha_connection, &self.auto_switch_ha_connection) {
(Some(connection), _) => connection.get_socket(),
(_, Some(connection)) => connection.get_socket(),
(None, None) => {
tracing::warn!("No HA connection to get socket from");
panic!("No HA connection available");
}
}
}
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

🧩 Verification correct

Inconsistent error handling and potential robustness concerns.

These getter methods use panic! when no connection is available, which is inconsistent with the graceful handling in shutdown/close methods. While panicking may be necessary given the trait return types, this approach reduces robustness in library code.

Additionally, there's significant code duplication across all these methods - they follow identical patterns.

Consider these alternatives:

  1. Return default/sentinel values where semantically appropriate
  2. Modify the trait to return Result types for better error handling
  3. Ensure underlying implementations are complete before relying on them

Let me verify if the underlying implementations are ready:

Also applies to: 112-121, 123-132, 134-143, 145-154, 156-165


🏁 Script executed:

#!/bin/bash
# Verify if underlying implementations are complete
echo "Checking DefaultHAConnection implementations:"
rg -A 5 "fn get_socket" rocketmq-store/src/ha/default_ha_connection.rs

echo -e "\nChecking AutoSwitchHAConnection implementations:"
rg -A 5 "fn get_socket\|fn get_client_address\|fn get_transferred_byte_in_second" rocketmq-store/src/ha/auto_switch/auto_switch_ha_connection.rs

Length of output: 599


Inconsistent error handling and potential robustness concerns.

These getter methods use panic! when no connection is available, which is inconsistent with the graceful handling in shutdown/close methods. While panicking may be necessary given the trait return types, this approach reduces robustness in library code.

Additionally, there's significant code duplication across all these methods - they follow identical patterns.

Consider these alternatives:

  1. Return default/sentinel values where semantically appropriate
  2. Modify the trait to return Result types for better error handling
  3. Ensure underlying implementations are complete before relying on them

Let me verify if the underlying implementations are ready:

#!/bin/bash
# Verify if underlying implementations are complete
echo "Checking DefaultHAConnection implementations:"
rg -A 5 "fn get_socket" rocketmq-store/src/ha/default_ha_connection.rs

echo -e "\nChecking AutoSwitchHAConnection implementations:"
rg -A 5 "fn get_socket\|fn get_client_address\|fn get_transferred_byte_in_second" rocketmq-store/src/ha/auto_switch/auto_switch_ha_connection.rs

Also applies to: 112-121, 123-132, 134-143, 145-154, 156-165

🤖 Prompt for AI Agents
In rocketmq-store/src/ha/general_ha_connection.rs around lines 101 to 110, the
get_socket method panics if no connection is available, which is inconsistent
with the graceful error handling in shutdown/close methods and reduces
robustness. Refactor these getter methods to reduce code duplication by
extracting the common pattern into a helper function that returns an Option or
Result, depending on trait constraints. If possible, modify the trait to return
Result types for better error handling; otherwise, consider returning default or
sentinel values where appropriate. Also, verify that the underlying
DefaultHAConnection and AutoSwitchHAConnection implementations are complete
before relying on them to avoid panics.

@codecov
Copy link

codecov bot commented Jun 26, 2025

Codecov Report

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

Project coverage is 26.17%. Comparing base (03e9b58) to head (c341a3d).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
rocketmq-store/src/ha/general_ha_connection.rs 0.00% 39 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3535      +/-   ##
==========================================
- Coverage   26.18%   26.17%   -0.02%     
==========================================
  Files         556      556              
  Lines       78642    78681      +39     
==========================================
  Hits        20593    20593              
- Misses      58049    58088      +39     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Collaborator

@rocketmq-rust-bot rocketmq-rust-bot left a comment

Choose a reason for hiding this comment

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

LGTM

@rocketmq-rust-bot rocketmq-rust-bot merged commit 93b0f65 into main Jun 26, 2025
22 of 24 checks passed
@rocketmq-rust-bot rocketmq-rust-bot added approved PR has approved and removed ready to review waiting-review waiting review this PR labels Jun 26, 2025
@mxsm mxsm deleted the feature-3534 branch June 26, 2025 03:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI review first Ai review pr first approved PR has approved auto merge feature🚀 Suggest an idea for this project.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature🚀] Implement connection handling in HAConnection methods with error logging

4 participants