Skip to content

[ISSUE #4465] BrokerRuntimeInner#message_store method to return an Option reference for improved safety - #4509

Merged
mxsm merged 1 commit into
mxsm:mainfrom
EricZZZ:fix-4465
Dec 8, 2025
Merged

mxsm merged 1 commit into
mxsm:mainfrom
EricZZZ:fix-4465

Conversation

@EricZZZ

@EricZZZ EricZZZ commented Dec 8, 2025 •

Copy link
Copy Markdown
Contributor

Which Issue(s) This PR Fixes(Closes)

Fixes #4465

Brief Description

How Did You Test This Change?

Summary by CodeRabbit

  • Refactor
    • Simplified internal message store access patterns throughout the broker by updating how the optional message store reference is retrieved and used. Removed redundant dereference operations across multiple service handlers and processors, improving code clarity without affecting external functionality.

✏️ Tip: You can customize this high-level summary in your review settings.

@rocketmq-rust-bot

Copy link
Copy Markdown
Collaborator

🔊@EricZZZ 🚀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

coderabbitai Bot commented Dec 8, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

The BrokerRuntimeInner::message_store() method signature was refactored to return Option<&ArcMut<MS>> instead of &Option<ArcMut<MS>>, moving the reference outside the Option. All 19 call sites across the broker were updated to remove intermediate .as_ref() calls and use direct .unwrap() access, aligning with the new return type.

Changes

Cohort / File(s) Summary
Core Method Signature
rocketmq-broker/src/broker_runtime.rs
Updated message_store() return type from &Option<ArcMut<MS>> to Option<&ArcMut<MS>> and adjusted implementation to use as_ref() internally, enabling callers to use idiomatic Option unwrapping.
Processor Message/Query Operations
rocketmq-broker/src/processor/pull_message_processor.rs, rocketmq-broker/src/processor/pop_message_processor.rs, rocketmq-broker/src/processor/query_message_processor.rs, rocketmq-broker/src/processor/reply_message_processor.rs
Removed .as_ref() before .unwrap() when accessing message_store for get_min_offset_in_queue, get_max_offset_in_queue, get_message, query_message, and select_one_message_by_offset calls.
Admin Request Handlers
rocketmq-broker/src/processor/admin_broker_processor/broker_config_request_handler.rs, rocketmq-broker/src/processor/admin_broker_processor/broker_epoch_cache_handler.rs, rocketmq-broker/src/processor/admin_broker_processor/consumer_request_handler.rs, rocketmq-broker/src/processor/admin_broker_processor/offset_request_handler.rs, rocketmq-broker/src/processor/admin_broker_processor/topic_request_handler.rs
Simplified message_store access by removing redundant .as_ref() calls across multiple offset and config retrieval methods.
Consumer and Message Handling
rocketmq-broker/src/processor/ack_message_processor.rs, rocketmq-broker/src/processor/consumer_manage_processor.rs, rocketmq-broker/src/processor/send_message_processor.rs
Removed .as_ref() from message_store access in ack paths, min_offset checks, and consumer message send operations.
Specialized Services
rocketmq-broker/src/failover/escape_bridge.rs, rocketmq-broker/src/long_polling/long_polling_service/pull_request_hold_service.rs, rocketmq-broker/src/processor/processor_service/pop_revive_service.rs, rocketmq-broker/src/processor/change_invisible_time_processor.rs
Removed .as_ref() from message_store calls in hold service checks, escape bridge retrieval, and revive service operations.
Scheduling and Transaction
rocketmq-broker/src/schedule/schedule_message_service.rs, rocketmq-broker/src/subscription/manager/subscription_group_manager.rs, rocketmq-broker/src/topic/manager/topic_config_manager.rs, rocketmq-broker/src/transaction/queue/transactional_message_bridge.rs
Simplified message_store accesses in scheduled message building, subscription state machine versioning, topic config management, and transactional message operations.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Key focus areas:
    • Verify the return type change propagates correctly through all 20 call sites and no sites were missed
    • Confirm .as_ref() removals are consistent and complete across all files
    • Check that the shift from borrowed to owned value semantics (via direct .unwrap()) doesn't introduce unexpected lifetime or borrowing issues in edge cases
    • Cross-verify that error handling behavior remains unchanged (panic on None still occurs at the same logical points)

Poem

🐰 A method once wrapped finds freedom in Optional cheer,
References dancing outside where they're crystal clear,
Twenty files align in harmonious refactor,
Simpler unwraps bring both safety and laughter!
✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 15.38% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title directly references the linked issue (#4465) and accurately describes the main change: modifying BrokerRuntimeInner#message_store to return an Option reference for improved safety.
Linked Issues check ✅ Passed The code changes implement the stated objective from issue #4465: the method signature was changed from returning &Option<ArcMut> to Option<&ArcMut>, and all callers were appropriately updated to work with the new return type.
Out of Scope Changes check ✅ Passed All changes are scoped to implementing the API change from issue #4465: signature modification in broker_runtime.rs and corresponding caller updates across multiple processor and service files to adapt to the new return type.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
rocketmq-broker/src/topic/manager/topic_config_manager.rs (1)

405-436: Bug: new topics in create_topic_in_send_message_back_method can panic when registering broker data

In create_topic_in_send_message_back_method, you currently do:

let mut config = ArcMut::new(TopicConfig::new(topic));
...
let current_ref = self.put_topic_config(config);
...
(self.data_version...).next_version_with(...);
self.persist();
(current_ref, true)
...
if create_new {
    self.register_broker_data(topic_config.clone().unwrap());
}

However, put_topic_config returns the previous value from the map, so for a newly created topic current_ref will be None. You then propagate (current_ref, true) into topic_config/create_new, and topic_config.clone().unwrap() will panic on exactly the case where you are inserting a fresh topic.

You likely intended to use the newly created config here, similar to how create_topic_in_send_message_method and create_topic_of_tran_check_max_time behave. A minimal fix:

-            let current_ref = self.put_topic_config(config);
+            self.put_topic_config(config.clone());
             self.data_version.mut_from_ref().next_version_with(
                 self.broker_runtime_inner
                     .message_store()
                     .unwrap()
                     .get_state_machine_version(),
             );
             self.persist();
-            (current_ref, true)
+            (Some(config), true)

This ensures topic_config is Some(new_config) when create_new is true, avoiding the panic and correctly registering the new topic.

🧹 Nitpick comments (1)
rocketmq-broker/src/topic/manager/topic_config_manager.rs (1)

361-365: Unify state_machine_version lookup to avoid repeated message_store().unwrap()

You now handle the None case explicitly in delete_topic_config, but other sites still do:

  • create_topic_in_send_message_method (lines 361–365)
  • create_topic_in_send_message_back_method (lines 421–427)
  • update_topic_config (lines 530–535)
  • create_topic_of_tran_check_max_time (lines 602–607)

If message_store can ever be legitimately None, these unwrap() calls would still panic; if it’s an invariant that it is always Some after broker startup, that invariant is currently implicit and duplicated. Consider centralizing this in a small helper (e.g., a method returning the current state_machine_version, either handling None like delete_topic_config or using an expect with a clear message). This would reduce repetition and make the lifecycle assumptions around message_store explicit.

Also applies to: 421-427, 530-535, 602-607

📜 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 d4baa7b and a8f2e6b.

📒 Files selected for processing (21)
  • rocketmq-broker/src/broker_runtime.rs (1 hunks)
  • rocketmq-broker/src/failover/escape_bridge.rs (1 hunks)
  • rocketmq-broker/src/long_polling/long_polling_service/pull_request_hold_service.rs (0 hunks)
  • rocketmq-broker/src/processor/ack_message_processor.rs (2 hunks)
  • rocketmq-broker/src/processor/admin_broker_processor/broker_config_request_handler.rs (1 hunks)
  • rocketmq-broker/src/processor/admin_broker_processor/broker_epoch_cache_handler.rs (1 hunks)
  • rocketmq-broker/src/processor/admin_broker_processor/consumer_request_handler.rs (0 hunks)
  • rocketmq-broker/src/processor/admin_broker_processor/offset_request_handler.rs (0 hunks)
  • rocketmq-broker/src/processor/admin_broker_processor/topic_request_handler.rs (0 hunks)
  • rocketmq-broker/src/processor/change_invisible_time_processor.rs (0 hunks)
  • rocketmq-broker/src/processor/consumer_manage_processor.rs (0 hunks)
  • rocketmq-broker/src/processor/pop_message_processor.rs (0 hunks)
  • rocketmq-broker/src/processor/processor_service/pop_revive_service.rs (0 hunks)
  • rocketmq-broker/src/processor/pull_message_processor.rs (0 hunks)
  • rocketmq-broker/src/processor/query_message_processor.rs (0 hunks)
  • rocketmq-broker/src/processor/reply_message_processor.rs (0 hunks)
  • rocketmq-broker/src/processor/send_message_processor.rs (0 hunks)
  • rocketmq-broker/src/schedule/schedule_message_service.rs (0 hunks)
  • rocketmq-broker/src/subscription/manager/subscription_group_manager.rs (0 hunks)
  • rocketmq-broker/src/topic/manager/topic_config_manager.rs (1 hunks)
  • rocketmq-broker/src/transaction/queue/transactional_message_bridge.rs (0 hunks)
💤 Files with no reviewable changes (15)
  • rocketmq-broker/src/processor/consumer_manage_processor.rs
  • rocketmq-broker/src/processor/admin_broker_processor/consumer_request_handler.rs
  • rocketmq-broker/src/transaction/queue/transactional_message_bridge.rs
  • rocketmq-broker/src/processor/reply_message_processor.rs
  • rocketmq-broker/src/processor/change_invisible_time_processor.rs
  • rocketmq-broker/src/processor/admin_broker_processor/topic_request_handler.rs
  • rocketmq-broker/src/processor/processor_service/pop_revive_service.rs
  • rocketmq-broker/src/processor/send_message_processor.rs
  • rocketmq-broker/src/schedule/schedule_message_service.rs
  • rocketmq-broker/src/long_polling/long_polling_service/pull_request_hold_service.rs
  • rocketmq-broker/src/processor/query_message_processor.rs
  • rocketmq-broker/src/processor/pop_message_processor.rs
  • rocketmq-broker/src/processor/pull_message_processor.rs
  • rocketmq-broker/src/subscription/manager/subscription_group_manager.rs
  • rocketmq-broker/src/processor/admin_broker_processor/offset_request_handler.rs
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-05-10T06:20:00.401Z
Learnt from: 578223592
Repo: mxsm/rocketmq-rust PR: 3240
File: rocketmq-namesrv/src/kvconfig/kvconfig_mananger.rs:69-71
Timestamp: 2025-05-10T06:20:00.401Z
Learning: In RocketMQ Rust, while dashmap is concurrent-safe, returning a direct reference to a DashMap can break encapsulation by allowing external code to modify the map without triggering persistence methods like `persist()`. Consider returning a read-only view, iterator methods, or a snapshot instead.

Applied to files:

  • rocketmq-broker/src/broker_runtime.rs
  • rocketmq-broker/src/processor/admin_broker_processor/broker_config_request_handler.rs
  • rocketmq-broker/src/failover/escape_bridge.rs
🧬 Code graph analysis (4)
rocketmq-broker/src/processor/ack_message_processor.rs (1)
rocketmq-broker/src/broker_runtime.rs (1)
  • message_store (1922-1924)
rocketmq-broker/src/processor/admin_broker_processor/broker_epoch_cache_handler.rs (2)
rocketmq-broker/src/broker_runtime.rs (1)
  • message_store (1922-1924)
rocketmq-broker/src/topic/manager/topic_config_manager.rs (1)
  • broker_runtime_inner (637-639)
rocketmq-broker/src/failover/escape_bridge.rs (1)
rocketmq-broker/src/broker_runtime.rs (1)
  • message_store (1922-1924)
rocketmq-broker/src/topic/manager/topic_config_manager.rs (1)
rocketmq-broker/src/broker_runtime.rs (1)
  • message_store (1922-1924)
⏰ 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). (2)
  • GitHub Check: Check (fmt + clippy)
  • GitHub Check: auto-approve
🔇 Additional comments (7)
rocketmq-broker/src/broker_runtime.rs (1)

1922-1924: LGTM - Improved API ergonomics with idiomatic Rust pattern.

The change from &Option<ArcMut<MS>> to Option<&ArcMut<MS>> is a good refactor. Returning Option<&T> instead of &Option<T> is more idiomatic in Rust because:

  1. Callers can directly use .unwrap(), .map(), .ok_or() etc. without needing an intermediate .as_ref() call
  2. It communicates intent better - the caller gets an optional reference rather than a reference to an optional value
rocketmq-broker/src/failover/escape_bridge.rs (1)

473-473: Consistent with the API change.

The call site correctly adapts to the new message_store() signature. Since message_store() now returns Option<&ArcMut<MS>>, the .unwrap() directly yields &ArcMut<MS>, eliminating the need for .clone().

rocketmq-broker/src/processor/admin_broker_processor/broker_epoch_cache_handler.rs (1)

71-71: LGTM - Clean adaptation to the new API.

The removal of .as_ref() is correct since message_store() now directly returns Option<&ArcMut<MS>>.

rocketmq-broker/src/processor/admin_broker_processor/broker_config_request_handler.rs (1)

266-266: LGTM - Consistent with the updated API.

The change correctly removes the intermediate .as_ref() call, aligning with the new message_store() return type.

rocketmq-broker/src/processor/ack_message_processor.rs (2)

235-235: LGTM - Correctly adapted to the new API.

The change aligns with the updated message_store() signature that now returns Option<&ArcMut<MS>> directly.


364-364: LGTM - Consistent pattern applied.

Same correct adaptation as above for the batch ack code path.

rocketmq-broker/src/topic/manager/topic_config_manager.rs (1)

479-484: Safer message_store() handling in delete_topic_config; please confirm 0 fallback semantics

Using if let Some(message_store) = self.broker_runtime_inner.message_store() here avoids a panic when message_store is None, which is a clear safety improvement for this path. The only open question is whether passing 0 into DataVersion::next_version_with is always semantically correct for “no message store available” (e.g., during early startup or specific test modes). If 0 is intended as a sentinel, consider adding a short comment, or otherwise double‑check that this won’t regress any versioning assumptions.

@codecov

codecov Bot commented Dec 8, 2025 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 30.37%. Comparing base (d4baa7b) to head (a8f2e6b).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
rocketmq-broker/src/broker_runtime.rs 0.00% 2 Missing ⚠️
...etmq-broker/src/processor/ack_message_processor.rs 0.00% 2 Missing ⚠️
rocketmq-broker/src/failover/escape_bridge.rs 0.00% 1 Missing ⚠️
..._broker_processor/broker_config_request_handler.rs 0.00% 1 Missing ⚠️
...min_broker_processor/broker_epoch_cache_handler.rs 0.00% 1 Missing ⚠️
...q-broker/src/topic/manager/topic_config_manager.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4509      +/-   ##
==========================================
+ Coverage   30.35%   30.37%   +0.01%     
==========================================
  Files         673      673              
  Lines       97386    97329      -57     
==========================================
  Hits        29561    29561              
+ Misses      67825    67768      -57     

☔ 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.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM - All CI checks passed ✅

@mxsm mxsm left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

LGTM

@mxsm
mxsm merged commit c860847 into mxsm:main Dec 8, 2025
12 of 22 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 Dec 8, 2025
@EricZZZ
EricZZZ deleted the fix-4465 branch December 8, 2025 08:02
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 enhancement⚡️ New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enhancement⚡️] BrokerRuntimeInner#message_store method to return an Option reference for improved safety

4 participants