Skip to content

Conversation

mxsm
Copy link
Owner

@mxsm mxsm commented Sep 20, 2025

Which Issue(s) This PR Fixes(Closes)

Fixes #4075

Brief Description

How Did You Test This Change?

Summary by CodeRabbit

  • New Features
    • Improved handling of master–slave synchronization state for smoother HA behavior.
  • Bug Fixes
    • Fixed issues with updating the master address on slaves, reducing stale or incorrect address usage.
    • More robust behavior when the master goes offline, including clearing outdated addresses and refreshing the HA master address.
  • Refactor
    • Internal synchronization APIs streamlined to use references for safer, more consistent updates, enhancing stability without changing user workflows.

@Copilot Copilot AI review requested due to automatic review settings September 20, 2025 07:03
Copy link
Contributor

coderabbitai bot commented Sep 20, 2025

Walkthrough

Introduces reference-based master address propagation in slave synchronization, adds accessor methods to BrokerRuntimeInner, modifies update_slave_master_addr to use references, and implements/updates on_master_offline to clear and propagate master address changes to HA via message_store. Adjusts SlaveSynchronize::set_master_addr to accept Option<&CheetahString> and clone internally.

Changes

Cohort / File(s) Summary
Broker runtime API and control flow
rocketmq-broker/src/broker_runtime.rs
Added slave_synchronize accessors: slave_synchronize_unchecked(&self), slave_synchronize_mut(&mut self), slave_synchronize_mut_unchecked(&mut self). Updated master address handling to pass references (&result.master_addr). Implemented/updated on_master_offline to check/clear master address and update HA master address through message_store. Adjusted update_slave_master_addr to use master_addr.as_ref().
Slave synchronize API change
rocketmq-broker/src/slave/slave_synchronize.rs
Changed set_master_addr signature to pub fn set_master_addr(&mut self, addr: Option<&CheetahString>) and now clones via addr.cloned(). Replaces previous generic Into<CheetahString> approach.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant BR as BrokerRuntimeInner
  participant SS as SlaveSynchronize
  participant MS as MessageStore (HA)

  rect rgba(220,240,255,0.4)
    note over BR: on_master_offline
    BR->>SS: access via slave_synchronize_mut(_)
    alt master_addr is non-empty
      BR->>SS: set_master_addr(None)
      BR->>MS: update HA master addr (None)
      MS-->>BR: ack
    else master_addr empty
      BR-->>BR: no-op
    end
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

A thump of paw, the master’s gone—oh my!
I nudge the wires, clear the old reply.
With whiskered care, I pass a borrowed string,
The HA hums, a gentle, tidy ping.
Burrow secure, the cluster hops in line—
Carrot commits, and everything’s fine. 🥕🐇

Pre-merge checks and finishing touches

❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 45.45% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
Linked Issues Check ❓ Inconclusive The linked issue #4075 requests implementing Broker.on_master_offline and the PR introduces on_master_offline-related logic in broker_runtime.rs plus related accessor and master-address propagation changes in slave_synchronize.rs, which appears to implement that feature; however, the linked issue contains no acceptance criteria and the PR lacks tests or a detailed description, so I cannot fully verify functional correctness or coverage of edge cases. Provide a short description of the expected on_master_offline behavior, add unit/integration tests covering normal and edge cases, and document any API changes so reviewers can confirm the implementation meets the issue's intent.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "[ISSUE #4075]🚀Implement Broker on_master_offline" directly names the primary change requested by the linked issue and aligns with the PR's modified files and intent (broker_runtime.rs and slave_synchronize.rs), so it accurately summarizes the main change; it is concise and focused, though it includes an emoji and the issue tag which are optional noise.
Out of Scope Changes Check ✅ Passed All changes are localized to broker_runtime.rs and rocketmq-broker/src/slave/slave_synchronize.rs and directly relate to master-offline handling and master-address propagation, so there are no apparent out-of-scope changes; note the set_master_addr signature was narrowed from Into to &CheetahString, which is an API surface change but still related to the objective.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature-4075

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 03737c2 and 5c3d083.

📒 Files selected for processing (2)
  • rocketmq-broker/src/broker_runtime.rs (3 hunks)
  • rocketmq-broker/src/slave/slave_synchronize.rs (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
rocketmq-broker/src/broker_runtime.rs (1)
rocketmq-broker/src/slave/slave_synchronize.rs (1)
  • master_addr (46-48)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (9)
  • GitHub Check: build (windows-latest, nightly)
  • GitHub Check: build (macos-latest, stable)
  • GitHub Check: build (windows-latest, stable)
  • GitHub Check: build (ubuntu-latest, stable)
  • GitHub Check: build (ubuntu-latest, nightly)
  • GitHub Check: build (macos-latest, nightly)
  • GitHub Check: build
  • GitHub Check: test
  • GitHub Check: auto-approve
🔇 Additional comments (5)
rocketmq-broker/src/slave/slave_synchronize.rs (1)

50-51: LGTM!

The signature change from Option<impl Into<CheetahString>> to Option<&CheetahString> with internal cloning is a good improvement. This avoids unnecessary trait bound complexity and makes the API more explicit about taking ownership through cloning.

rocketmq-broker/src/broker_runtime.rs (4)

1458-1458: LGTM!

The change to pass a reference (&result.master_addr) instead of moving the value aligns perfectly with the updated set_master_addr signature in SlaveSynchronize.


2066-2079: LGTM!

These new accessor methods provide consistent and safe access to the slave_synchronize field:

  • slave_synchronize_unchecked() for guaranteed non-null immutable access
  • slave_synchronize_mut() for optional mutable access
  • slave_synchronize_mut_unchecked() for guaranteed non-null mutable access

The naming follows the existing pattern used throughout the codebase.


2084-2084: LGTM!

The change to use master_addr.as_ref() correctly aligns with the new reference-based API signature of set_master_addr.


2719-2730: LGTM!

The implementation correctly implements the on_master_offline functionality:

  1. Uses the new slave_synchronize_unchecked() accessor to get immutable access to check the master address
  2. Uses slave_synchronize_mut_unchecked() to clear the master address by calling set_master_addr(None)
  3. Updates the HA master address in the message store to an empty string

This properly handles master offline scenarios by clearing the cached master address and propagating the change to the HA subsystem.


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

Comment @coderabbitai help to get the list of available commands and usage tips.

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

Copy link
Contributor

@Copilot 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

Implements broker behavior for handling master offline events and adjusts how the slave tracks the master address.

  • Change SlaveSynchronize::set_master_addr to accept a borrowed address and update call sites
  • Add unchecked accessors for SlaveSynchronize in BrokerRuntimeInner and implement on_master_offline to clear master state and update HA address

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
rocketmq-broker/src/slave/slave_synchronize.rs Changes set_master_addr to take Option<&CheetahString> and clone internally
rocketmq-broker/src/broker_runtime.rs Updates call sites for set_master_addr, adds unchecked accessors, and implements on_master_offline logic

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +2720 to +2726
let slave_synchronize = self.slave_synchronize_unchecked();
if let Some(master_addr) = slave_synchronize.master_addr() {
if !master_addr.is_empty() {
//close channels
}
}
self.slave_synchronize_mut_unchecked().set_master_addr(None);
Copy link

Copilot AI Sep 20, 2025

Choose a reason for hiding this comment

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

Using unwrap_unchecked via slave_synchronize_unchecked()/slave_synchronize_mut_unchecked() here is UB if self.slave_synchronize is None. Replace with a safe path that gracefully no-ops when absent.

Suggested change
let slave_synchronize = self.slave_synchronize_unchecked();
if let Some(master_addr) = slave_synchronize.master_addr() {
if !master_addr.is_empty() {
//close channels
}
}
self.slave_synchronize_mut_unchecked().set_master_addr(None);
if let Some(slave_synchronize) = self.slave_synchronize() {
if let Some(master_addr) = slave_synchronize.master_addr() {
if !master_addr.is_empty() {
//close channels
}
}
}
if let Some(slave_synchronize_mut) = self.slave_synchronize_mut() {
slave_synchronize_mut.set_master_addr(None);
}

Copilot uses AI. Check for mistakes.

Comment on lines 2082 to 2085
pub fn update_slave_master_addr(&mut self, master_addr: Option<CheetahString>) {
if let Some(ref mut slave) = self.slave_synchronize {
slave.set_master_addr(master_addr);
slave.set_master_addr(master_addr.as_ref());
};
Copy link

Copilot AI Sep 20, 2025

Choose a reason for hiding this comment

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

This clones master_addr inside set_master_addr even though an owned Option is already available, causing an avoidable allocation. Either (a) change set_master_addr to take Option by value and pass master_addr directly, or (b) change update_slave_master_addr to accept Option<&CheetahString> to avoid constructing an owned value.

Copilot uses AI. Check for mistakes.

Comment on lines +50 to 53
pub fn set_master_addr(&mut self, addr: Option<&CheetahString>) {
let addr = addr.cloned();
if self.master_addr == addr {
return;
Copy link

Copilot AI Sep 20, 2025

Choose a reason for hiding this comment

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

[nitpick] Changing the signature from Option<impl Into> to Option<&CheetahString> reduces ergonomics and forces an internal clone, losing the zero-cost move path for owned values. Consider restoring the owned-taking API (e.g., pub fn set_master_addr(&mut self, addr: Option)) and, if needed, adding a separate convenience for borrowed inputs that clones explicitly.

Copilot uses AI. Check for mistakes.

Comment on lines +2066 to +2069
#[inline]
pub fn slave_synchronize_unchecked(&self) -> &SlaveSynchronize<MS> {
unsafe { self.slave_synchronize.as_ref().unwrap_unchecked() }
}
Copy link

Copilot AI Sep 20, 2025

Choose a reason for hiding this comment

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

These unchecked accessors rely on invariants about self.slave_synchronize being Some but provide no Safety docs specifying the required preconditions. Please add doc-comments with a Safety section detailing the invariant and when these may be called, or keep them private and prefer the checked variants at call sites.

Copilot uses AI. Check for mistakes.

Comment on lines +2076 to +2079
#[inline]
pub fn slave_synchronize_mut_unchecked(&mut self) -> &mut SlaveSynchronize<MS> {
unsafe { self.slave_synchronize.as_mut().unwrap_unchecked() }
}
Copy link

Copilot AI Sep 20, 2025

Choose a reason for hiding this comment

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

These unchecked accessors rely on invariants about self.slave_synchronize being Some but provide no Safety docs specifying the required preconditions. Please add doc-comments with a Safety section detailing the invariant and when these may be called, or keep them private and prefer the checked variants at call sites.

Copilot uses AI. Check for mistakes.

Copy link

codecov bot commented Sep 20, 2025

Codecov Report

❌ Patch coverage is 0% with 23 lines in your changes missing coverage. Please review.
✅ Project coverage is 26.48%. Comparing base (03737c2) to head (5c3d083).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
rocketmq-broker/src/broker_runtime.rs 0.00% 21 Missing ⚠️
rocketmq-broker/src/slave/slave_synchronize.rs 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4076      +/-   ##
==========================================
- Coverage   26.49%   26.48%   -0.01%     
==========================================
  Files         574      574              
  Lines       81182    81200      +18     
==========================================
  Hits        21507    21507              
- Misses      59675    59693      +18     

☔ 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 34be377 into main Sep 20, 2025
21 of 23 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 Sep 20, 2025
@mxsm mxsm deleted the feature-4075 branch September 21, 2025 05:21
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 Broker on_master_offline

3 participants