-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ISSUE #1283]⚡️Optimize name server DefaultRequestProcessor #1284
Conversation
WalkthroughThe changes in this pull request involve modifications to the Changes
Assessment against linked issues
Possibly related issues
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
Documentation and Community
|
🔊@mxsm 🚀Thanks for your contribution 🎉. CodeRabbit(AI) will review your code first 🔥 |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1284 +/- ##
==========================================
- Coverage 18.98% 18.98% -0.01%
==========================================
Files 428 428
Lines 53853 53863 +10
==========================================
Hits 10224 10224
- Misses 43629 43639 +10 ☔ View full report in Codecov by Sentry. 🚨 Try these New Features:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (3)
rocketmq-namesrv/src/processor/default_request_processor.rs (1)
362-362
: Consider logging decode failuresWhile using
unwrap_or_default()
prevents panics, silently falling back to default values could mask underlying issues. Consider logging decode failures to aid in debugging.- let topic_route_data = TopicRouteData::decode(body).unwrap_or_default(); + let topic_route_data = match TopicRouteData::decode(body) { + Ok(data) => data, + Err(e) => { + warn!("Failed to decode TopicRouteData: {}", e); + TopicRouteData::default() + } + };rocketmq-namesrv/src/route/route_info_manager.rs (2)
Line range hint
765-802
: Consider optimizing the topic registration process.The implementation is correct, but there are opportunities for optimization:
- The write lock is held longer than necessary, spanning both the map operations and logging.
- The warning message construction happens inside the lock.
Consider this optimization:
pub(crate) fn register_topic(&self, topic: CheetahString, queue_data_vec: Vec<QueueData>) { if queue_data_vec.is_empty() { return; } - let lock = self.lock.write(); - if !self.topic_queue_table.contains_key(&topic) { - self.topic_queue_table - .mut_from_ref() - .insert(topic.clone(), HashMap::new()); - } - let queue_data_map = self - .topic_queue_table - .mut_from_ref() - .get_mut(&topic) - .unwrap(); - let vec_length = queue_data_vec.len(); + // Prepare data outside the lock + let mut invalid_brokers = Vec::new(); for queue_data in queue_data_vec { if !self .broker_addr_table .contains_key(queue_data.broker_name()) { - warn!( - "Register topic contains illegal broker, {}, {:?}", - topic, queue_data - ); - return; + invalid_brokers.push(queue_data.clone()); } - queue_data_map.insert(queue_data.broker_name().clone(), queue_data); } + + if !invalid_brokers.is_empty() { + warn!( + "Register topic contains illegal brokers, {}, {:?}", + topic, invalid_brokers + ); + return; + } + + // Minimize lock duration + let vec_length; + { + let lock = self.lock.write(); + if !self.topic_queue_table.contains_key(&topic) { + self.topic_queue_table + .mut_from_ref() + .insert(topic.clone(), HashMap::new()); + } + let queue_data_map = self + .topic_queue_table + .mut_from_ref() + .get_mut(&topic) + .unwrap(); + vec_length = queue_data_vec.len(); + for queue_data in queue_data_vec { + queue_data_map.insert(queue_data.broker_name().clone(), queue_data); + } + } - drop(lock); if queue_data_map.len() > vec_length { info!("Topic route already exist.{}, {:?}", topic, queue_data_map) } else { info!("Register topic route:{}, {:?}", topic, queue_data_map) } }
Line range hint
803-819
: Optimize topic collection efficiency.The current implementation iterates through all topics for each broker in the cluster. This can be inefficient for large topic sets.
Consider this more efficient approach:
pub(crate) fn get_topics_by_cluster(&self, cluster: &CheetahString) -> TopicList { - let mut topic_list = Vec::new(); + let mut topic_set = HashSet::new(); let lock = self.lock.read(); if let Some(broker_name_set) = self.cluster_addr_table.get(cluster) { + // Pre-calculate capacity to avoid reallocations + topic_set.reserve(self.topic_queue_table.len()); for broker_name in broker_name_set { for (topic, queue_data_map) in self.topic_queue_table.iter() { if let Some(_queue_data) = queue_data_map.get(broker_name) { - topic_list.push(topic.clone()); + topic_set.insert(topic.clone()); } } } } drop(lock); TopicList { - topic_list, + topic_list: topic_set.into_iter().collect(), broker_addr: None, } }This optimization:
- Uses a HashSet to automatically deduplicate topics
- Pre-allocates capacity to avoid reallocations
- Converts to Vec only at the end
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
📒 Files selected for processing (2)
rocketmq-namesrv/src/processor/default_request_processor.rs
(3 hunks)rocketmq-namesrv/src/route/route_info_manager.rs
(4 hunks)
🔇 Additional comments (2)
rocketmq-namesrv/src/processor/default_request_processor.rs (2)
374-374
: LGTM!
The error message improvement in expect()
provides better context for debugging.
399-402
: LGTM!
The change to use CheetahString
improves type safety and maintains consistency with the RouteInfoManager
interface.
Which Issue(s) This PR Fixes(Closes)
Fixes #1283
Brief Description
How Did You Test This Change?
Summary by CodeRabbit
New Features
Bug Fixes
Documentation