forked from apache/rocketmq-client-cpp
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMQClientInstance.cpp
More file actions
791 lines (670 loc) · 27.5 KB
/
Copy pathMQClientInstance.cpp
File metadata and controls
791 lines (670 loc) · 27.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "MQClientInstance.h"
#include <cstddef>
#include <memory>
#include <string>
#include <utility> // std::move
#include "ClientRemotingProcessor.h"
#include "MQAdminImpl.h"
#include "MQClientAPIImpl.h"
#include "MQClientManager.h"
#include "common/FindBrokerResult.hpp"
#include "common/MQVersion.h"
#include "common/PermName.h"
#include "common/UtilAll.h"
#include "consumer/PullMessageService.hpp"
#include "consumer/RebalancePushImpl.h"
#include "consumer/RebalanceService.h"
#include "logging/Logging.hpp"
#include "protocol/body/ConsumerRunningInfo.hpp"
#include "protocol/body/HeartbeatData.hpp"
#include "route/TopicPublishInfo.hpp"
#include "route/TopicRouteManager.hpp"
#include "route/TopicSubscribeInfo.hpp"
#include "transport/TcpRemotingClient.h"
#include "utility/MakeUnique.hpp"
#include "utility/MapAccessor.hpp"
#include "utility/SetAccessor.hpp"
namespace {
constexpr long kLockTimeoutMillis = 3000L;
}
namespace rocketmq {
MQClientInstance::MQClientInstance(const MQClientConfig& client_config, std::string client_id)
: MQClientInstance(client_config, std::move(client_id), nullptr) {}
MQClientInstance::MQClientInstance(const MQClientConfig& client_config, std::string client_id, RPCHookPtr rpc_hook)
: client_id_(std::move(client_id)),
rebalance_service_(MakeUnique<RebalanceService>(this)),
pull_message_service_(MakeUnique<PullMessageService>(this)),
scheduled_executor_service_("MQClient", false) {
// default Topic register
topic_route_manager_->PutTopicPublishInfo(AUTO_CREATE_TOPIC_KEY_TOPIC, std::make_shared<TopicPublishInfo>());
client_remoting_processor_ = MakeUnique<ClientRemotingProcessor>(this);
mq_client_api_impl_ =
MakeUnique<MQClientAPIImpl>(client_remoting_processor_.get(), std::move(rpc_hook), client_config);
std::string namesrv_addresses = client_config.namesrv_addr();
if (!namesrv_addresses.empty()) {
mq_client_api_impl_->UpdateNameServerAddressList(namesrv_addresses);
LOG_INFO_NEW("user specified name server address: {}", namesrv_addresses);
}
mq_admin_impl_ = MakeUnique<MQAdminImpl>(this);
service_state_ = ServiceState::kCreateJust;
LOG_DEBUG_NEW("MQClientInstance construct");
}
MQClientInstance::~MQClientInstance() {
LOG_INFO_NEW("MQClientInstance:{} destruct", client_id_);
}
std::string MQClientInstance::GetNamesrvAddress() const {
auto namesrv_addresses = mq_client_api_impl_->GetNameServerAddressList();
std::ostringstream oss;
for (const auto& address : namesrv_addresses) {
oss << address << ";";
}
return oss.str();
}
void MQClientInstance::Start() {
switch (service_state_) {
case ServiceState::kCreateJust:
LOG_INFO_NEW("the client instance [{}] is starting", client_id_);
service_state_ = ServiceState::kStartFailed;
mq_client_api_impl_->Start();
// start various schedule tasks
StartScheduledTask();
// start pull service
pull_message_service_->start();
// start rebalance service
rebalance_service_->start();
LOG_INFO_NEW("the client instance [{}] start OK", client_id_);
service_state_ = ServiceState::kRunning;
break;
case ServiceState::kRunning:
LOG_INFO_NEW("the client instance [{}] already running.", client_id_);
break;
case ServiceState::kShutdownAlready:
case ServiceState::kStartFailed:
LOG_INFO_NEW("the client instance [{}] start failed with fault state:{}", client_id_, ToString(service_state_));
break;
default:
break;
}
}
void MQClientInstance::Shutdown() {
if (MapAccessor::Size(consumer_table_, consumer_table_mutex_) > 0) {
return;
}
if (MapAccessor::Size(producer_table_, producer_table_mutex_) > 0) {
return;
}
switch (service_state_) {
case ServiceState::kCreateJust:
break;
case ServiceState::kRunning: {
service_state_ = ServiceState::kShutdownAlready;
pull_message_service_->shutdown();
scheduled_executor_service_.shutdown();
mq_client_api_impl_->Shutdown();
rebalance_service_->shutdown();
MQClientManager::getInstance()->removeMQClientInstance(client_id_);
LOG_INFO_NEW("the client instance [{}] shutdown OK", client_id_);
} break;
case ServiceState::kShutdownAlready:
default:
break;
}
}
void MQClientInstance::StartScheduledTask() {
LOG_INFO_NEW("start scheduled task:{}", client_id_);
scheduled_executor_service_.startup();
// updateTopicRouteInfoFromNameServer
scheduled_executor_service_.schedule([this]() { UpdateTopicRouteInfoPeriodically(); }, 10, time_unit::milliseconds);
// sendHeartbeatToAllBroker
scheduled_executor_service_.schedule([this]() { SendHeartbeatToAllBrokerPeriodically(); }, 1000,
time_unit::milliseconds);
// persistAllConsumerOffset
scheduled_executor_service_.schedule([this]() { PersistAllConsumerOffsetPeriodically(); }, 1000 * 10,
time_unit::milliseconds);
}
bool MQClientInstance::RegisterConsumer(const std::string& group, MQConsumerInner* consumer) {
if (group.empty()) {
return false;
}
if (!MapAccessor::Insert(consumer_table_, group, consumer, consumer_table_mutex_)) {
LOG_WARN_NEW("the consumer group[{}] exist already.", group);
return false;
}
LOG_DEBUG_NEW("registerConsumer success:{}", group);
return true;
}
bool MQClientInstance::RegisterProducer(const std::string& group, MQProducerInner* producer) {
if (group.empty()) {
return false;
}
if (!MapAccessor::Insert(producer_table_, group, producer, producer_table_mutex_)) {
LOG_WARN_NEW("the consumer group[{}] exist already.", group);
return false;
}
LOG_DEBUG_NEW("registerProducer success:{}", group);
return true;
}
void MQClientInstance::UnregisterConsumer(const std::string& group) {
MapAccessor::Erase(consumer_table_, group, consumer_table_mutex_);
UnregisterClientWithLock(null, group);
}
void MQClientInstance::UnregisterProducer(const std::string& group) {
MapAccessor::Erase(producer_table_, group, producer_table_mutex_);
UnregisterClientWithLock(group, null);
}
void MQClientInstance::UnregisterClientWithLock(const std::string& producer_group, const std::string& consumer_group) {
if (UtilAll::try_lock_for(lock_heartbeat_, kLockTimeoutMillis)) {
std::lock_guard<std::timed_mutex> lock(lock_heartbeat_, std::adopt_lock);
try {
UnregisterClient(producer_group, consumer_group);
} catch (const std::exception& e) {
LOG_ERROR_NEW("unregisterClient exception: {}", e.what());
}
} else {
LOG_WARN_NEW("lock heartBeat, but failed.");
}
}
void MQClientInstance::UnregisterClient(const std::string& producer_group, const std::string& consumer_group) {
auto broker_address_table = MapAccessor::Clone(broker_address_table_, broker_address_table_mutex_);
for (const auto& it : broker_address_table) {
const auto& broker_name = it.first;
const auto& address_table = it.second;
for (const auto& it2 : address_table) {
const auto& broker_id = it2.first;
const auto& address = it2.second;
try {
mq_client_api_impl_->UnregisterClient(address, client_id_, producer_group, consumer_group);
LOG_INFO_NEW("unregister client[Producer: {} Consumer: {}] from broker[{} {} {}] success", producer_group,
consumer_group, broker_name, broker_id, address);
} catch (const std::exception& e) {
LOG_ERROR_NEW("unregister client exception from broker: {}. EXCEPTION: {}", address, e.what());
}
}
}
}
MQConsumerInner* MQClientInstance::SelectConsumer(const std::string& consumer_group) {
return MapAccessor::GetOrDefault(consumer_table_, consumer_group, nullptr, consumer_table_mutex_);
}
MQProducerInner* MQClientInstance::SelectProducer(const std::string& producer_group) {
return MapAccessor::GetOrDefault(producer_table_, producer_group, nullptr, producer_table_mutex_);
}
void MQClientInstance::UpdateTopicRouteInfoPeriodically() {
UpdateTopicRouteInfoFromNameServer();
// next round
scheduled_executor_service_.schedule([this]() { UpdateTopicRouteInfoPeriodically(); }, 1000 * 30,
time_unit::milliseconds);
}
void MQClientInstance::UpdateTopicRouteInfoFromNameServer() {
std::set<std::string> topicList;
// Consumer
GetTopicListFromConsumerSubscription(topicList);
// Producer
SetAccessor::Merge(topicList, topic_route_manager_->TopicInPublish());
// update
if (!topicList.empty()) {
for (const auto& topic : topicList) {
UpdateTopicRouteInfoFromNameServer(topic, false);
}
}
}
void MQClientInstance::GetTopicListFromConsumerSubscription(std::set<std::string>& topic_list) {
std::lock_guard<std::mutex> lock(consumer_table_mutex_);
for (const auto& it : consumer_table_) {
std::vector<SubscriptionData> result = it.second->subscriptions();
for (const auto& sd : result) {
topic_list.insert(sd.topic);
}
}
}
namespace {
bool IsTopicRouteDataChanged(TopicRouteData* old_data, TopicRouteData* now_data) {
return old_data == nullptr || now_data == nullptr || !(*old_data == *now_data);
}
} // namespace
bool MQClientInstance::UpdateTopicRouteInfoFromNameServer(const std::string& topic, bool is_default) {
if (!UtilAll::try_lock_for(lock_namesrv_, kLockTimeoutMillis)) {
LOG_WARN_NEW("updateTopicRouteInfoFromNameServer tryLock timeout {}ms", kLockTimeoutMillis);
return false;
}
std::lock_guard<std::timed_mutex> lock(lock_namesrv_, std::adopt_lock);
LOG_DEBUG_NEW("updateTopicRouteInfoFromNameServer start:{}", topic);
try {
auto topic_route_data = [this, &topic, is_default]() -> TopicRouteDataPtr {
if (!is_default) {
return mq_client_api_impl_->GetTopicRouteInfoFromNameServer(topic, 1000 * 3);
}
auto topic_route_data =
mq_client_api_impl_->GetTopicRouteInfoFromNameServer(AUTO_CREATE_TOPIC_KEY_TOPIC, 1000 * 3);
if (topic_route_data != nullptr) {
auto& queue_datas = topic_route_data->queue_datas;
for (auto& queue_data : queue_datas) {
int queue_nums = std::min(4, queue_data.read_queue_nums);
queue_data.read_queue_nums = queue_nums;
queue_data.write_queue_nums = queue_nums;
}
} else {
LOG_DEBUG_NEW("getTopicRouteInfoFromNameServer is null for topic: {}", topic);
}
return topic_route_data;
}();
if (!topic_route_data) {
LOG_WARN_NEW("updateTopicRouteInfoFromNameServer, getTopicRouteInfoFromNameServer return null, Topic: {}", topic);
return false;
}
LOG_INFO_NEW("updateTopicRouteInfoFromNameServer has data");
auto old = topic_route_manager_->GetTopicRouteData(topic);
if (IsTopicRouteDataChanged(old.get(), topic_route_data.get())) {
LOG_INFO_NEW("updateTopicRouteInfoFromNameServer changed:{}", topic);
// update broker addr
const auto& broker_datas = topic_route_data->broker_datas;
for (const auto& broker_data : broker_datas) {
LOG_INFO_NEW("updateTopicRouteInfoFromNameServer changed with broker name:{}", broker_data.broker_name);
MapAccessor::InsertOrAssign(broker_address_table_, broker_data.broker_name, broker_data.broker_addrs,
broker_address_table_mutex_);
}
// update publish info
topic_route_manager_->PutTopicPublishInfo(topic, std::make_shared<TopicPublishInfo>(topic, topic_route_data));
// update subscribe info
auto subscribe_info = std::make_shared<TopicSubscribeInfo>(topic, topic_route_data);
const auto& message_queues = subscribe_info->message_queues();
{
std::lock_guard<std::mutex> lock(consumer_table_mutex_);
if (!consumer_table_.empty()) {
for (auto& it : consumer_table_) {
it.second->updateTopicSubscribeInfo(topic, message_queues);
}
}
}
topic_route_manager_->PutTopicRouteData(topic, std::move(topic_route_data));
}
LOG_DEBUG_NEW("updateTopicRouteInfoFromNameServer end:{}", topic);
return true;
} catch (const std::exception& e) {
if (!UtilAll::isRetryTopic(topic) && topic != AUTO_CREATE_TOPIC_KEY_TOPIC) {
LOG_WARN_NEW("updateTopicRouteInfoFromNameServer Exception, {}", e.what());
}
}
return false;
}
TopicRouteDataPtr MQClientInstance::GetTopicRouteData(const std::string& topic) {
return topic_route_manager_->GetTopicRouteData(topic);
}
std::string MQClientInstance::FindBrokerAddressInPublish(const std::string& broker_name) {
std::lock_guard<std::mutex> lock(broker_address_table_mutex_);
const auto& it = broker_address_table_.find(broker_name);
if (it != broker_address_table_.end()) {
const auto& broker_map = it->second;
const auto& it1 = broker_map.find(MASTER_ID);
if (it1 != broker_map.end()) {
return it1->second;
}
}
return std::string();
}
FindBrokerResult MQClientInstance::FindBrokerAddressInSubscribe(const std::string& broker_name,
int broker_id,
bool only_this_broker) {
std::lock_guard<std::mutex> lock(broker_address_table_mutex_);
const auto& it = broker_address_table_.find(broker_name);
if (it != broker_address_table_.end()) {
const auto& broker_map = it->second;
const auto& it1 = broker_map.find(broker_id);
if (it1 != broker_map.end()) {
return {it1->second, it1->first != MASTER_ID};
}
if (!only_this_broker) { // not only from master
const auto& it2 = broker_map.begin();
if (it2 != broker_map.end()) {
return {it2->second, it2->first != MASTER_ID};
}
}
}
return {};
}
FindBrokerResult MQClientInstance::FindBrokerAddressInAdmin(const std::string& broker_name) {
std::lock_guard<std::mutex> lock(broker_address_table_mutex_);
const auto& it = broker_address_table_.find(broker_name);
if (it != broker_address_table_.end()) {
const auto& broker_map = it->second;
const auto& it1 = broker_map.begin();
if (it1 != broker_map.end()) {
return {it1->second, it1->first != MASTER_ID};
}
}
return {};
}
std::string MQClientInstance::FindBrokerAddressInPublish(const MessageQueue& message_queue) {
auto broker_address = FindBrokerAddressInPublish(message_queue.broker_name());
if (broker_address.empty()) {
UpdateTopicRouteInfoFromNameServer(message_queue.topic());
broker_address = FindBrokerAddressInPublish(message_queue.broker_name());
}
return broker_address;
}
FindBrokerResult MQClientInstance::FindBrokerAddressInSubscribe(const MessageQueue& message_queue,
int broker_id,
bool only_this_broker) {
auto find_broker_result = FindBrokerAddressInSubscribe(message_queue.broker_name(), broker_id, only_this_broker);
if (!find_broker_result) {
UpdateTopicRouteInfoFromNameServer(message_queue.topic());
find_broker_result = FindBrokerAddressInSubscribe(message_queue.broker_name(), broker_id, only_this_broker);
}
return find_broker_result;
}
FindBrokerResult MQClientInstance::FindBrokerAddressInAdmin(const MessageQueue& message_queue) {
auto find_broker_result = FindBrokerAddressInAdmin(message_queue.broker_name());
if (!find_broker_result) {
UpdateTopicRouteInfoFromNameServer(message_queue.topic());
find_broker_result = FindBrokerAddressInAdmin(message_queue.broker_name());
}
return find_broker_result;
}
namespace {
/**
* @brief Selects a (preferably master) broker address from the registered list.
*
* @note If the master's address cannot be found, a slave broker address is selected in a random manner.
*
* @return Broker address.
*/
std::string SelectBrokerAddr(const TopicRouteData& topic_route_data) {
auto broker_data_size = topic_route_data.broker_datas.size();
if (broker_data_size > 0) {
auto broker_data_index = std::rand() % broker_data_size;
const auto& broker_data = topic_route_data.broker_datas[broker_data_index];
const auto& broker_addrs = broker_data.broker_addrs;
auto it = broker_addrs.find(MASTER_ID);
if (it == broker_addrs.end()) {
auto broker_addr_size = broker_addrs.size();
auto broker_addr_index = std::rand() % broker_addr_size;
for (it = broker_addrs.begin(); broker_addr_index > 0; --broker_addr_index) {
it++;
}
}
return it->second;
}
return std::string();
}
} // namespace
std::string MQClientInstance::FindBrokerAddrByTopic(const std::string& topic) {
auto topic_route_data = topic_route_manager_->GetTopicRouteData(topic);
if (topic_route_data != nullptr) {
return SelectBrokerAddr(*topic_route_data);
}
return std::string();
}
TopicPublishInfoPtr MQClientInstance::FindTopicPublishInfo(const std::string& topic) {
auto topic_publish_info = topic_route_manager_->GetTopicPublishInfo(topic);
if (!topic_publish_info) {
UpdateTopicRouteInfoFromNameServer(topic);
topic_publish_info = topic_route_manager_->GetTopicPublishInfo(topic);
}
if (topic_publish_info && topic_publish_info->ok()) {
return topic_publish_info;
}
LOG_INFO_NEW("updateTopicRouteInfoFromNameServer with default");
UpdateTopicRouteInfoFromNameServer(topic, true);
return topic_route_manager_->GetTopicPublishInfo(topic);
}
void MQClientInstance::SendHeartbeatToAllBrokerPeriodically() {
CleanOfflineBroker();
SendHeartbeatToAllBrokerWithLock();
// next round
scheduled_executor_service_.schedule([this]() { SendHeartbeatToAllBrokerPeriodically(); }, 1000 * 30,
time_unit::milliseconds);
}
void MQClientInstance::CleanOfflineBroker() {
if (!UtilAll::try_lock_for(lock_namesrv_, kLockTimeoutMillis)) {
LOG_WARN_NEW("lock namesrv, but failed.");
return;
}
std::lock_guard<std::timed_mutex> lock(lock_namesrv_, std::adopt_lock);
std::set<std::string> offline_brokers;
auto updated_table = MapAccessor::Clone(broker_address_table_, broker_address_table_mutex_);
for (auto it = updated_table.begin(); it != updated_table.end();) {
const auto& broker_name = it->first;
auto& clone_address_table = it->second;
for (auto it2 = clone_address_table.begin(); it2 != clone_address_table.end();) {
const auto& address = it2->second;
if (!topic_route_manager_->ContainsBrokerAddress(address)) {
offline_brokers.insert(address);
it2 = clone_address_table.erase(it2);
LOG_INFO_NEW("the broker addr[{} {}] is offline, remove it", broker_name, address);
} else {
it2++;
}
}
if (clone_address_table.empty()) {
it = updated_table.erase(it);
LOG_INFO_NEW("the broker[{}] name's host is offline, remove it", broker_name);
} else {
it++;
}
}
if (!offline_brokers.empty()) {
MapAccessor::Assign(broker_address_table_, std::move(updated_table), broker_address_table_mutex_);
std::lock_guard<std::mutex> lock(topic_broker_addr_table_mutex_);
for (auto it = topic_broker_addr_table_.begin(); it != topic_broker_addr_table_.end();) {
if (offline_brokers.find(it->second.first) != offline_brokers.end()) {
it = topic_broker_addr_table_.erase(it);
} else {
it++;
}
}
}
}
void MQClientInstance::SendHeartbeatToAllBrokerWithLock() {
if (lock_heartbeat_.try_lock()) {
std::lock_guard<std::timed_mutex> lock(lock_heartbeat_, std::adopt_lock);
SendHeartbeatToAllBroker();
} else {
LOG_WARN_NEW("lock heartBeat, but failed.");
}
}
void MQClientInstance::SendHeartbeatToAllBroker() {
auto heartbeat_data = PrepareHeartbeatData();
bool producer_empty = heartbeat_data.producer_data_set.empty();
bool consumer_empty = heartbeat_data.consumer_data_set.empty();
if (producer_empty && consumer_empty) {
LOG_WARN_NEW("sending heartbeat, but no consumer and no producer");
return;
}
auto broker_address_table = MapAccessor::Clone(broker_address_table_, broker_address_table_mutex_);
if (broker_address_table.empty()) {
LOG_WARN_NEW("sendheartbeat brokerAddrTable is empty");
return;
}
for (const auto& it : broker_address_table) {
// const auto& broker_name = it.first;
const auto& address_table = it.second;
for (const auto& it2 : address_table) {
const auto broker_id = it2.first;
const auto& address = it2.second;
if (consumer_empty && broker_id != MASTER_ID) {
continue;
}
try {
mq_client_api_impl_->SendHearbeat(address, heartbeat_data, 3000);
} catch (const MQException& e) {
LOG_ERROR_NEW("{}", e.what());
}
}
}
}
HeartbeatData MQClientInstance::PrepareHeartbeatData() {
HeartbeatData heartbeat_data;
// clientID
heartbeat_data.client_id = client_id_;
// Consumer
{
std::lock_guard<std::mutex> lock(consumer_table_mutex_);
for (const auto& it : consumer_table_) {
const auto* consumer = it.second;
// TODO: unitMode
heartbeat_data.consumer_data_set.emplace_back(consumer->groupName(), consumer->consumeType(),
consumer->messageModel(), consumer->consumeFromWhere(),
consumer->subscriptions());
}
}
// Producer
{
std::lock_guard<std::mutex> lock(producer_table_mutex_);
for (const auto& it : producer_table_) {
heartbeat_data.producer_data_set.emplace_back(it.first);
}
}
return heartbeat_data;
}
void MQClientInstance::PersistAllConsumerOffsetPeriodically() {
PersistAllConsumerOffset();
// next round
scheduled_executor_service_.schedule([this]() { PersistAllConsumerOffsetPeriodically(); }, 1000 * 5,
time_unit::milliseconds);
}
void MQClientInstance::PersistAllConsumerOffset() {
std::lock_guard<std::mutex> lock(consumer_table_mutex_);
for (const auto& it : consumer_table_) {
LOG_DEBUG_NEW("the client instance [{}] start persistAllConsumerOffset", client_id_);
it.second->persistConsumerOffset();
}
}
void MQClientInstance::RebalanceImmediately() {
rebalance_service_->wakeup();
}
void MQClientInstance::DoRebalance() {
LOG_INFO_NEW("the client instance:{} start doRebalance", client_id_);
std::lock_guard<std::mutex> lock(consumer_table_mutex_);
if (!consumer_table_.empty()) {
for (auto& it : consumer_table_) {
it.second->doRebalance();
}
}
LOG_INFO_NEW("the client instance [{}] finish doRebalance", client_id_);
}
void MQClientInstance::DoRebalanceByConsumerGroup(const std::string& consumer_group) {
std::lock_guard<std::mutex> lock(consumer_table_mutex_);
auto it = consumer_table_.find(consumer_group);
if (it != consumer_table_.end()) {
try {
LOG_INFO_NEW("the client instance [{}] start doRebalance for consumer [{}]", client_id_, consumer_group);
auto* consumer = it->second;
consumer->doRebalance();
} catch (const std::exception& e) {
LOG_ERROR_NEW("{}", e.what());
}
}
}
std::vector<std::string> MQClientInstance::FindConsumerIds(const std::string& topic, const std::string& group) {
std::string broker_addr = [this, &topic]() {
// find consumerIds from same broker every 40s
std::lock_guard<std::mutex> lock(topic_broker_addr_table_mutex_);
const auto& it = topic_broker_addr_table_.find(topic);
if (it != topic_broker_addr_table_.end()) {
if (UtilAll::currentTimeMillis() < it->second.second + 120 * 1000) {
return it->second.first;
}
}
return std::string();
}();
if (broker_addr.empty()) {
// select new one
broker_addr = FindBrokerAddrByTopic(topic);
if (broker_addr.empty()) {
UpdateTopicRouteInfoFromNameServer(topic);
broker_addr = FindBrokerAddrByTopic(topic);
}
if (!broker_addr.empty()) {
MapAccessor::InsertOrAssign(topic_broker_addr_table_, topic,
std::make_pair(broker_addr, UtilAll::currentTimeMillis()),
topic_broker_addr_table_mutex_);
}
}
if (!broker_addr.empty()) {
try {
LOG_INFO_NEW("getConsumerIdList from broker:{}", broker_addr);
return mq_client_api_impl_->GetConsumerIdListByGroup(broker_addr, group, 5000);
} catch (const MQException& e) {
LOG_ERROR_NEW("encounter exception when getConsumerIdList: {}", e.what());
MapAccessor::Erase(topic_broker_addr_table_, topic, topic_broker_addr_table_mutex_);
}
}
return {};
}
void MQClientInstance::ResetOffset(const std::string& group,
const std::string& topic,
const std::map<MessageQueue, int64_t>& offset_table) {
DefaultMQPushConsumerImpl* consumer = nullptr;
try {
consumer = dynamic_cast<DefaultMQPushConsumerImpl*>(SelectConsumer(group));
if (consumer == nullptr) {
LOG_INFO_NEW("[reset-offset] consumer dose not exist. group={}", group);
return;
}
consumer->Suspend();
auto process_queue_table = consumer->rebalance_impl()->getProcessQueueTable();
for (const auto& it : process_queue_table) {
const auto& mq = it.first;
if (topic == mq.topic() && offset_table.find(mq) != offset_table.end()) {
auto pq = it.second;
pq->set_dropped(true);
pq->ClearAllMessages();
}
}
std::this_thread::sleep_for(std::chrono::seconds(10));
for (const auto& it : process_queue_table) {
const auto& message_queue = it.first;
const auto& it2 = offset_table.find(message_queue);
if (it2 != offset_table.end()) {
auto offset = it2->second;
consumer->UpdateConsumeOffset(message_queue, offset);
consumer->rebalance_impl()->removeUnnecessaryMessageQueue(message_queue, it.second);
consumer->rebalance_impl()->removeProcessQueueDirectly(message_queue);
}
}
} catch (...) {
if (consumer != nullptr) {
consumer->Resume();
}
throw;
}
if (consumer != nullptr) {
consumer->Resume();
}
}
std::unique_ptr<ConsumerRunningInfo> MQClientInstance::ConsumerRunningInfo(const std::string& consumer_group) {
auto* consumer = SelectConsumer(consumer_group);
if (consumer != nullptr) {
auto running_info = consumer->consumerRunningInfo();
if (running_info != nullptr) {
std::string namesrv_address = GetNamesrvAddress();
running_info->properties.emplace(ConsumerRunningInfo::PROP_NAMESERVER_ADDR, namesrv_address);
if (consumer->consumeType() == CONSUME_PASSIVELY) {
running_info->properties.emplace(ConsumerRunningInfo::PROP_CONSUME_TYPE, "CONSUME_PASSIVELY");
} else {
running_info->properties.emplace(ConsumerRunningInfo::PROP_CONSUME_TYPE, "CONSUME_ACTIVELY");
}
running_info->properties.emplace(ConsumerRunningInfo::PROP_CLIENT_VERSION,
MQVersion::GetVersionDesc(MQVersion::CURRENT_VERSION));
return running_info;
}
}
LOG_ERROR_NEW("no corresponding consumer found for group:{}", consumer_group);
return nullptr;
}
} // namespace rocketmq