@@ -124,7 +124,7 @@ void MQClientInstance::start() {
124
124
}
125
125
126
126
void MQClientInstance::shutdown () {
127
- if (getConsumerTableSize () != 0 ) {
127
+ if (MapAccessor::Size (consumer_table_, consumer_table_mutex_) > 0 ) {
128
128
return ;
129
129
}
130
130
@@ -382,9 +382,12 @@ bool MQClientInstance::updateTopicRouteInfoFromNameServer(const std::string& top
382
382
topic_publish_info_table_mutex_);
383
383
384
384
// update subscribe info
385
- if (getConsumerTableSize () > 0 ) {
385
+ std::lock_guard<std::mutex> lock (consumer_table_mutex_);
386
+ if (!consumer_table_.empty ()) {
386
387
std::vector<MessageQueue> subscribeInfo = MakeTopicSubscribeInfo (topic, *topicRouteData);
387
- updateConsumerTopicSubscribeInfo (topic, subscribeInfo);
388
+ for (auto & it : consumer_table_) {
389
+ it.second ->updateTopicSubscribeInfo (topic, subscribeInfo);
390
+ }
388
391
}
389
392
390
393
MapAccessor::InsertOrAssign (topic_route_table_, topic, topicRouteData, topic_route_table_mutex_);
@@ -444,12 +447,12 @@ TopicRouteDataPtr MQClientInstance::GetTopicRouteData(const std::string& topic)
444
447
return MapAccessor::GetOrDefault (topic_route_table_, topic, nullptr , topic_route_table_mutex_);
445
448
}
446
449
447
- bool MQClientInstance::registerConsumer (const std::string& group, MQConsumerInner* consumer) {
450
+ bool MQClientInstance::RegisterConsumer (const std::string& group, MQConsumerInner* consumer) {
448
451
if (group.empty ()) {
449
452
return false ;
450
453
}
451
454
452
- if (!addConsumerToTable ( group, consumer)) {
455
+ if (!MapAccessor::Insert (consumer_table_, group, consumer, consumer_table_mutex_ )) {
453
456
LOG_WARN_NEW (" the consumer group[{}] exist already." , group);
454
457
return false ;
455
458
}
@@ -458,8 +461,8 @@ bool MQClientInstance::registerConsumer(const std::string& group, MQConsumerInne
458
461
return true ;
459
462
}
460
463
461
- void MQClientInstance::unregisterConsumer (const std::string& group) {
462
- eraseConsumerFromTable ( group);
464
+ void MQClientInstance::UnregisterConsumer (const std::string& group) {
465
+ MapAccessor::Erase (consumer_table_, group, consumer_table_mutex_ );
463
466
unregisterClientWithLock (null, group);
464
467
}
465
468
@@ -521,8 +524,8 @@ void MQClientInstance::rebalanceImmediately() {
521
524
522
525
void MQClientInstance::doRebalance () {
523
526
LOG_INFO_NEW (" the client instance:{} start doRebalance" , client_id_);
524
- if ( getConsumerTableSize () > 0 ) {
525
- std::lock_guard<std::mutex> lock (consumer_table_mutex_);
527
+ std::lock_guard<std::mutex> lock (consumer_table_mutex_);
528
+ if (!consumer_table_. empty ()) {
526
529
for (auto & it : consumer_table_) {
527
530
it.second ->doRebalance ();
528
531
}
@@ -548,37 +551,8 @@ MQProducerInner* MQClientInstance::SelectProducer(const std::string& producer_gr
548
551
return MapAccessor::GetOrDefault (producer_table_, producer_group, nullptr , producer_table_mutex_);
549
552
}
550
553
551
- MQConsumerInner* MQClientInstance::selectConsumer (const std::string& group) {
552
- std::lock_guard<std::mutex> lock (consumer_table_mutex_);
553
- const auto & it = consumer_table_.find (group);
554
- if (it != consumer_table_.end ()) {
555
- return it->second ;
556
- }
557
- return nullptr ;
558
- }
559
-
560
- bool MQClientInstance::addConsumerToTable (const std::string& consumerName, MQConsumerInner* consumer) {
561
- std::lock_guard<std::mutex> lock (consumer_table_mutex_);
562
- if (consumer_table_.find (consumerName) != consumer_table_.end ()) {
563
- return false ;
564
- }
565
- consumer_table_[consumerName] = consumer;
566
- return true ;
567
- }
568
-
569
- void MQClientInstance::eraseConsumerFromTable (const std::string& consumerName) {
570
- std::lock_guard<std::mutex> lock (consumer_table_mutex_);
571
- const auto & it = consumer_table_.find (consumerName);
572
- if (it != consumer_table_.end ()) {
573
- consumer_table_.erase (it); // do not need free consumer, as it was allocated by user
574
- } else {
575
- LOG_WARN_NEW (" could not find consumer:{} from table" , consumerName);
576
- }
577
- }
578
-
579
- int MQClientInstance::getConsumerTableSize () {
580
- std::lock_guard<std::mutex> lock (consumer_table_mutex_);
581
- return consumer_table_.size ();
554
+ MQConsumerInner* MQClientInstance::SelectConsumer (const std::string& consumer_group) {
555
+ return MapAccessor::GetOrDefault (consumer_table_, consumer_group, nullptr , consumer_table_mutex_);
582
556
}
583
557
584
558
void MQClientInstance::getTopicListFromConsumerSubscription (std::set<std::string>& topicList) {
@@ -591,14 +565,6 @@ void MQClientInstance::getTopicListFromConsumerSubscription(std::set<std::string
591
565
}
592
566
}
593
567
594
- void MQClientInstance::updateConsumerTopicSubscribeInfo (const std::string& topic,
595
- std::vector<MessageQueue> subscribeInfo) {
596
- std::lock_guard<std::mutex> lock (consumer_table_mutex_);
597
- for (auto & it : consumer_table_) {
598
- it.second ->updateTopicSubscribeInfo (topic, subscribeInfo);
599
- }
600
- }
601
-
602
568
TopicPublishInfoPtr MQClientInstance::tryToFindTopicPublishInfo (const std::string& topic) {
603
569
auto topicPublishInfo =
604
570
MapAccessor::GetOrDefault (topic_publish_info_table_, topic, nullptr , topic_publish_info_table_mutex_);
@@ -749,7 +715,7 @@ void MQClientInstance::resetOffset(const std::string& group,
749
715
const std::map<MessageQueue, int64_t >& offsetTable) {
750
716
DefaultMQPushConsumerImpl* consumer = nullptr ;
751
717
try {
752
- auto * impl = selectConsumer (group);
718
+ auto * impl = SelectConsumer (group);
753
719
if (impl != nullptr && std::type_index (typeid (*impl)) == std::type_index (typeid (DefaultMQPushConsumerImpl))) {
754
720
consumer = static_cast <DefaultMQPushConsumerImpl*>(impl);
755
721
} else {
@@ -792,7 +758,7 @@ void MQClientInstance::resetOffset(const std::string& group,
792
758
}
793
759
794
760
std::unique_ptr<ConsumerRunningInfo> MQClientInstance::consumerRunningInfo (const std::string& consumerGroup) {
795
- auto * consumer = selectConsumer (consumerGroup);
761
+ auto * consumer = SelectConsumer (consumerGroup);
796
762
if (consumer != nullptr ) {
797
763
std::unique_ptr<ConsumerRunningInfo> runningInfo (consumer->consumerRunningInfo ());
798
764
if (runningInfo != nullptr ) {
0 commit comments