-
Notifications
You must be signed in to change notification settings - Fork 116
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 #1230]🚀Support broker receive transaction message-4 #1231
Changes from all commits
de83e75
a06aafb
b6c1334
98ba18e
42b8137
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -16,6 +16,8 @@ | |||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
use std::collections::HashMap; | ||||||||||||||||||||||||||||||||||
use std::sync::atomic::AtomicI32; | ||||||||||||||||||||||||||||||||||
use std::sync::Arc; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
use cheetah_string::CheetahString; | ||||||||||||||||||||||||||||||||||
use rocketmq_common::TimeUtils::get_current_millis; | ||||||||||||||||||||||||||||||||||
|
@@ -31,13 +33,15 @@ | |||||||||||||||||||||||||||||||||
HashMap<CheetahString /* group name */, HashMap<Channel, ClientChannelInfo>>, | ||||||||||||||||||||||||||||||||||
>, | ||||||||||||||||||||||||||||||||||
client_channel_table: parking_lot::Mutex<HashMap<CheetahString, Channel /* client ip:port */>>, | ||||||||||||||||||||||||||||||||||
positive_atomic_counter: Arc<AtomicI32>, | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
impl ProducerManager { | ||||||||||||||||||||||||||||||||||
pub fn new() -> Self { | ||||||||||||||||||||||||||||||||||
Self { | ||||||||||||||||||||||||||||||||||
group_channel_table: parking_lot::Mutex::new(HashMap::new()), | ||||||||||||||||||||||||||||||||||
client_channel_table: parking_lot::Mutex::new(HashMap::new()), | ||||||||||||||||||||||||||||||||||
positive_atomic_counter: Arc::new(Default::default()), | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
@@ -116,4 +120,23 @@ | |||||||||||||||||||||||||||||||||
pub fn find_channel(&self, client_id: &str) -> Option<Channel> { | ||||||||||||||||||||||||||||||||||
self.client_channel_table.lock().get(client_id).cloned() | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
pub fn get_available_channel(&self, group: Option<&CheetahString>) -> Option<Channel> { | ||||||||||||||||||||||||||||||||||
let group = group?; | ||||||||||||||||||||||||||||||||||
let group_channel_table = self.group_channel_table.lock(); | ||||||||||||||||||||||||||||||||||
let channel_map = group_channel_table.get(group); | ||||||||||||||||||||||||||||||||||
if let Some(channel_map) = channel_map { | ||||||||||||||||||||||||||||||||||
if channel_map.is_empty() { | ||||||||||||||||||||||||||||||||||
return None; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
let channels = channel_map.keys().collect::<Vec<&Channel>>(); | ||||||||||||||||||||||||||||||||||
let index = self | ||||||||||||||||||||||||||||||||||
.positive_atomic_counter | ||||||||||||||||||||||||||||||||||
.fetch_add(1, std::sync::atomic::Ordering::AcqRel); | ||||||||||||||||||||||||||||||||||
let index = index.unsigned_abs() as usize % channels.len(); | ||||||||||||||||||||||||||||||||||
let channel = channels[index].clone(); | ||||||||||||||||||||||||||||||||||
return Some(channel); | ||||||||||||||||||||||||||||||||||
Comment on lines
+132
to
+138
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Optimize channel selection implementation The current implementation has several areas for improvement:
Consider this more efficient implementation: - let channels = channel_map.keys().collect::<Vec<&Channel>>();
- let index = self
- .positive_atomic_counter
- .fetch_add(1, std::sync::atomic::Ordering::AcqRel);
- let index = index.unsigned_abs() as usize % channels.len();
- let channel = channels[index].clone();
- return Some(channel);
+ let len = channel_map.len();
+ let index = self
+ .positive_atomic_counter
+ .fetch_add(1, std::sync::atomic::Ordering::Relaxed)
+ .unsigned_abs() as usize % len;
+ channel_map
+ .iter()
+ .nth(index)
+ .map(|(channel, _)| channel.clone()) 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
None | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} |
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.
🛠️ Refactor suggestion
Review initialization of Broker2Client and cloning approach
Two suggestions for improvement:
Broker2Client
is used directly without instantiation. Consider if this is intentional or if an instance should be created.cloned()
withclone()
on line 700 for consistency with Rust idioms.Apply this diff to improve the code: