diff --git a/example/AsyncProducer.cpp b/example/AsyncProducer.cpp old mode 100755 new mode 100644 index 3a5d0af65..0e9ff70d9 --- a/example/AsyncProducer.cpp +++ b/example/AsyncProducer.cpp @@ -36,105 +36,100 @@ SendCallback* g_callback = NULL; TpsReportService g_tps; class MySendCallback : public SendCallback { - virtual void onSuccess(SendResult& sendResult) { - g_msgCount--; - g_tps.Increment(); - if (g_msgCount.load() <= 0) { - std::unique_lock lck(g_mtx); - g_finished.notify_one(); + virtual void onSuccess(SendResult& sendResult) { + g_msgCount--; + g_tps.Increment(); + if (g_msgCount.load() <= 0) { + std::unique_lock lck(g_mtx); + g_finished.notify_one(); + } } - } - virtual void onException(MQException& e) { cout << "send Exception\n"; } + virtual void onException(MQException& e) { cout << "send Exception\n"; } }; class MyAutoDeleteSendCallback : public AutoDeleteSendCallBack { - public: - virtual ~MyAutoDeleteSendCallback() {} - virtual void onSuccess(SendResult& sendResult) { - g_msgCount--; - if (g_msgCount.load() <= 0) { - std::unique_lock lck(g_mtx); - g_finished.notify_one(); +public: + virtual ~MyAutoDeleteSendCallback() {} + virtual void onSuccess(SendResult& sendResult) { + g_msgCount--; + if (g_msgCount.load() <= 0) { + std::unique_lock lck(g_mtx); + g_finished.notify_one(); + } } - } - virtual void onException(MQException& e) { - std::cout << "send Exception" << e << "\n"; - } + virtual void onException(MQException& e) { std::cout << "send Exception" << e << "\n"; } }; -void AsyncProducerWorker(RocketmqSendAndConsumerArgs* info, - DefaultMQProducer* producer) { - while (!g_quit.load()) { - if (g_msgCount.load() <= 0) { - std::unique_lock lck(g_mtx); - g_finished.notify_one(); +void AsyncProducerWorker(RocketmqSendAndConsumerArgs* info, DefaultMQProducer* producer) { + while (!g_quit.load()) { + if (g_msgCount.load() <= 0) { + std::unique_lock lck(g_mtx); + g_finished.notify_one(); + } + MQMessage msg(info->topic, // topic + "*", // tag + info->body); // body + + if (info->IsAutoDeleteSendCallback) { + g_callback = new MyAutoDeleteSendCallback(); // auto delete + } + + try { + producer->send(msg, g_callback); + } + catch (MQException& e) { + std::cout << e << endl; // if catch excepiton , need re-send this msg by + // service + } } - MQMessage msg(info->topic, // topic - "*", // tag - info->body); // body +} - if (info->IsAutoDeleteSendCallback) { - g_callback = new MyAutoDeleteSendCallback(); // auto delete +int main(int argc, char* argv[]) { + RocketmqSendAndConsumerArgs info; + if (!ParseArgs(argc, argv, &info)) { + exit(-1); } - try { - producer->send(msg, g_callback); - } catch (MQException& e) { - std::cout << e << endl; // if catch excepiton , need re-send this msg by - // service + DefaultMQProducer producer("please_rename_unique_group_name"); + if (!info.IsAutoDeleteSendCallback) { + g_callback = new MySendCallback(); } - } -} -int main(int argc, char* argv[]) { - RocketmqSendAndConsumerArgs info; - if (!ParseArgs(argc, argv, &info)) { - exit(-1); - } - - DefaultMQProducer producer("please_rename_unique_group_name"); - if (!info.IsAutoDeleteSendCallback) { - g_callback = new MySendCallback(); - } - - PrintRocketmqSendAndConsumerArgs(info); - - if (!info.namesrv.empty()) producer.setNamesrvAddr(info.namesrv); - - producer.setGroupName(info.groupname); - producer.setInstanceName(info.groupname); - producer.setNamesrvDomain(info.namesrv_domain); - producer.start(); - g_tps.start(); - std::vector> work_pool; - auto start = std::chrono::system_clock::now(); - int msgcount = g_msgCount.load(); - for (int j = 0; j < info.thread_count; j++) { - std::shared_ptr th = - std::make_shared(AsyncProducerWorker, &info, &producer); - work_pool.push_back(th); - } - - { - std::unique_lock lck(g_mtx); - g_finished.wait(lck); - g_quit.store(true); - } - - auto end = std::chrono::system_clock::now(); - auto duration = - std::chrono::duration_cast(end - start); - - std::cout - << "per msg time: " << duration.count() / (double)msgcount << "ms \n" - << "========================finished==============================\n"; - - producer.shutdown(); - for (size_t th = 0; th != work_pool.size(); ++th) { - work_pool[th]->join(); - } - if (!info.IsAutoDeleteSendCallback) { - delete g_callback; - } - return 0; + PrintRocketmqSendAndConsumerArgs(info); + + if (!info.namesrv.empty()) producer.setNamesrvAddr(info.namesrv); + + producer.setGroupName(info.groupname); + producer.setInstanceName(info.groupname); + producer.setNamesrvDomain(info.namesrv_domain); + producer.start(); + g_tps.start(); + std::vector> work_pool; + auto start = std::chrono::system_clock::now(); + int msgcount = g_msgCount.load(); + for (int j = 0; j < info.thread_count; j++) { + std::shared_ptr th = std::make_shared(AsyncProducerWorker, &info, &producer); + work_pool.push_back(th); + } + + { + std::unique_lock lck(g_mtx); + g_finished.wait(lck); + g_quit.store(true); + } + + auto end = std::chrono::system_clock::now(); + auto duration = std::chrono::duration_cast(end - start); + + std::cout << "per msg time: " << duration.count() / (double)msgcount << "ms \n" + << "========================finished==============================\n"; + + producer.shutdown(); + for (size_t th = 0; th != work_pool.size(); ++th) { + work_pool[th]->join(); + } + if (!info.IsAutoDeleteSendCallback) { + delete g_callback; + } + return 0; } diff --git a/example/AsyncPushConsumer.cpp b/example/AsyncPushConsumer.cpp old mode 100755 new mode 100644 index 093d61de0..dc649b2a1 --- a/example/AsyncPushConsumer.cpp +++ b/example/AsyncPushConsumer.cpp @@ -33,79 +33,81 @@ TpsReportService g_tps; using namespace rocketmq; class MyMsgListener : public MessageListenerConcurrently { - public: - MyMsgListener() {} - virtual ~MyMsgListener() {} - - virtual ConsumeStatus consumeMessage(const std::vector &msgs) { - g_msgCount.store(g_msgCount.load() - msgs.size()); - for (size_t i = 0; i < msgs.size(); ++i) { - g_tps.Increment(); +public: + MyMsgListener() {} + virtual ~MyMsgListener() {} + + virtual ConsumeStatus consumeMessage(const std::vector &msgs) { + g_msgCount.store(g_msgCount.load() - msgs.size()); + for (size_t i = 0; i < msgs.size(); ++i) { + g_tps.Increment(); + } + + if (g_msgCount.load() <= 0) { + std::unique_lock lck(g_mtx); + g_finished.notify_one(); + } + return CONSUME_SUCCESS; } - - if (g_msgCount.load() <= 0) { - std::unique_lock lck(g_mtx); - g_finished.notify_one(); - } - return CONSUME_SUCCESS; - } }; int main(int argc, char *argv[]) { - RocketmqSendAndConsumerArgs info; - if (!ParseArgs(argc, argv, &info)) { - exit(-1); - } - PrintRocketmqSendAndConsumerArgs(info); - DefaultMQPushConsumer consumer("please_rename_unique_group_name"); - DefaultMQProducer producer("please_rename_unique_group_name"); - - producer.setNamesrvAddr(info.namesrv); - producer.setGroupName("msg-persist-group_producer_sandbox"); - producer.setNamesrvDomain(info.namesrv_domain); - producer.start(); - - consumer.setNamesrvAddr(info.namesrv); - consumer.setGroupName(info.groupname); - consumer.setNamesrvDomain(info.namesrv_domain); - consumer.setConsumeFromWhere(CONSUME_FROM_LAST_OFFSET); - - consumer.setInstanceName(info.groupname); - - consumer.subscribe(info.topic, "*"); - consumer.setConsumeThreadCount(15); - consumer.setTcpTransportTryLockTimeout(1000); - consumer.setTcpTransportConnectTimeout(400); - - MyMsgListener msglistener; - consumer.registerMessageListener(&msglistener); - - try { - consumer.start(); - } catch (MQClientException &e) { - cout << e << endl; - } - g_tps.start(); - - int msgcount = g_msgCount.load(); - for (int i = 0; i < msgcount; ++i) { - MQMessage msg(info.topic, // topic - "*", // tag - info.body); // body + RocketmqSendAndConsumerArgs info; + if (!ParseArgs(argc, argv, &info)) { + exit(-1); + } + PrintRocketmqSendAndConsumerArgs(info); + DefaultMQPushConsumer consumer("please_rename_unique_group_name"); + DefaultMQProducer producer("please_rename_unique_group_name"); + + producer.setNamesrvAddr(info.namesrv); + producer.setGroupName("msg-persist-group_producer_sandbox"); + producer.setNamesrvDomain(info.namesrv_domain); + producer.start(); + + consumer.setNamesrvAddr(info.namesrv); + consumer.setGroupName(info.groupname); + consumer.setNamesrvDomain(info.namesrv_domain); + consumer.setConsumeFromWhere(CONSUME_FROM_LAST_OFFSET); + + consumer.setInstanceName(info.groupname); + + consumer.subscribe(info.topic, "*"); + consumer.setConsumeThreadCount(15); + consumer.setTcpTransportTryLockTimeout(1000); + consumer.setTcpTransportConnectTimeout(400); + + MyMsgListener msglistener; + consumer.registerMessageListener(&msglistener); try { - producer.send(msg); - } catch (MQException &e) { - std::cout << e << endl; // if catch excepiton , need re-send this msg by - // service + consumer.start(); + } + catch (MQClientException &e) { + cout << e << endl; + } + g_tps.start(); + + int msgcount = g_msgCount.load(); + for (int i = 0; i < msgcount; ++i) { + MQMessage msg(info.topic, // topic + "*", // tag + info.body); // body + + try { + producer.send(msg); + } + catch (MQException &e) { + std::cout << e << endl; // if catch excepiton , need re-send this msg by + // service + } + } + + { + std::unique_lock lck(g_mtx); + g_finished.wait(lck); } - } - - { - std::unique_lock lck(g_mtx); - g_finished.wait(lck); - } - producer.shutdown(); - consumer.shutdown(); - return 0; + producer.shutdown(); + consumer.shutdown(); + return 0; } diff --git a/example/BatchProducer.cpp b/example/BatchProducer.cpp index 3788422f8..ba2469a8c 100644 --- a/example/BatchProducer.cpp +++ b/example/BatchProducer.cpp @@ -34,8 +34,7 @@ std::mutex g_mtx; std::condition_variable g_finished; TpsReportService g_tps; -void SyncProducerWorker(RocketmqSendAndConsumerArgs* info, - DefaultMQProducer* producer) { +void SyncProducerWorker(RocketmqSendAndConsumerArgs* info, DefaultMQProducer* producer) { while (!g_quit.load()) { if (g_msgCount.load() <= 0) { std::unique_lock lck(g_mtx); @@ -62,13 +61,13 @@ void SyncProducerWorker(RocketmqSendAndConsumerArgs* info, g_tps.Increment(); --g_msgCount; auto end = std::chrono::system_clock::now(); - auto duration = - std::chrono::duration_cast(end - start); + auto duration = std::chrono::duration_cast(end - start); if (duration.count() >= 500) { - std::cout << "send RT more than: " << duration.count() - << " ms with msgid: " << sendResult.getMsgId() << endl; + std::cout << "send RT more than: " << duration.count() << " ms with msgid: " << sendResult.getMsgId() + << endl; } - } catch (const MQException& e) { + } + catch (const MQException& e) { std::cout << "send failed: " << e.what() << std::endl; std::unique_lock lck(g_mtx); g_finished.notify_one(); @@ -101,8 +100,7 @@ int main(int argc, char* argv[]) { int threadCount = info.thread_count; for (int j = 0; j < threadCount; j++) { - std::shared_ptr th = - std::make_shared(SyncProducerWorker, &info, &producer); + std::shared_ptr th = std::make_shared(SyncProducerWorker, &info, &producer); work_pool.push_back(th); } @@ -113,12 +111,10 @@ int main(int argc, char* argv[]) { } auto end = std::chrono::system_clock::now(); - auto duration = - std::chrono::duration_cast(end - start); + auto duration = std::chrono::duration_cast(end - start); - std::cout - << "per msg time: " << duration.count() / (double)msgcount << "ms \n" - << "========================finished==============================\n"; + std::cout << "per msg time: " << duration.count() / (double)msgcount << "ms \n" + << "========================finished==============================\n"; for (size_t th = 0; th != work_pool.size(); ++th) { work_pool[th]->join(); diff --git a/example/CAsyncProducer.c b/example/CAsyncProducer.c index db4a957d9..8b2bb7e7f 100644 --- a/example/CAsyncProducer.c +++ b/example/CAsyncProducer.c @@ -35,11 +35,9 @@ void thread_sleep(unsigned milliseconds) { #endif } -void SendSuccessCallback(CSendResult result){ - printf("async send success, msgid:%s\n", result.msgId); -} +void SendSuccessCallback(CSendResult result) { printf("async send success, msgid:%s\n", result.msgId); } -void SendExceptionCallback(CMQException e){ +void SendExceptionCallback(CMQException e) { char msg[1024]; snprintf(msg, sizeof(msg), "error:%d, msg:%s, file:%s:%d", e.error, e.msg, e.file, e.line); printf("async send exception %s\n", msg); @@ -56,19 +54,19 @@ void StartSendMessage(CProducer *producer) { memset(body, 0, sizeof(body)); snprintf(body, sizeof(body), "new message body, index %d", i); SetMessageBody(msg, body); - ret_code = SendMessageAsync(producer, msg, SendSuccessCallback , SendExceptionCallback); + ret_code = SendMessageAsync(producer, msg, SendSuccessCallback, SendExceptionCallback); printf("async send message[%d] return code: %d\n", i, ret_code); thread_sleep(1000); } DestroyMessage(msg); } -void CreateProducerAndStartSendMessage(int i){ +void CreateProducerAndStartSendMessage(int i) { printf("Producer Initializing.....\n"); CProducer *producer = CreateProducer("Group_producer"); SetProducerNameServerAddress(producer, "127.0.0.1:9876"); - if(i == 1){ - SetProducerSendMsgTimeout(producer , 3); + if (i == 1) { + SetProducerSendMsgTimeout(producer, 3); } StartProducer(producer); printf("Producer start.....\n"); @@ -86,4 +84,3 @@ int main(int argc, char *argv[]) { CreateProducerAndStartSendMessage(1); return 0; } - diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/example/COrderlyAsyncProducer.c b/example/COrderlyAsyncProducer.c index f841878ed..8a182da6c 100644 --- a/example/COrderlyAsyncProducer.c +++ b/example/COrderlyAsyncProducer.c @@ -35,17 +35,15 @@ void thread_sleep(unsigned milliseconds) { #endif } -void SendSuccessCallback(CSendResult result){ - printf("async send success, msgid:%s\n", result.msgId); -} +void SendSuccessCallback(CSendResult result) { printf("async send success, msgid:%s\n", result.msgId); } -void SendExceptionCallback(CMQException e){ +void SendExceptionCallback(CMQException e) { char msg[1024]; snprintf(msg, sizeof(msg), "error:%d, msg:%s, file:%s:%d", e.error, e.msg, e.file, e.line); printf("async send exception %s\n", msg); } -int aQueueSelectorCallback(int size, CMessage *msg, void *arg){ +int aQueueSelectorCallback(int size, CMessage *msg, void *arg) { return 0; }; @@ -60,22 +58,20 @@ void StartSendMessage(CProducer *producer) { memset(body, 0, sizeof(body)); snprintf(body, sizeof(body), "new message body, index %d", i); SetMessageBody(msg, body); - ret_code = SendMessageOrderlyAsync(producer, msg, - aQueueSelectorCallback, - (void*)&i, - SendSuccessCallback , SendExceptionCallback); + ret_code = SendMessageOrderlyAsync(producer, msg, aQueueSelectorCallback, (void *)&i, SendSuccessCallback, + SendExceptionCallback); printf("async send message[%d] return code: %d\n", i, ret_code); thread_sleep(1000); } DestroyMessage(msg); } -void CreateProducerAndStartSendMessage(int i){ +void CreateProducerAndStartSendMessage(int i) { printf("Producer Initializing.....\n"); CProducer *producer = CreateProducer("FooBarGroup1"); SetProducerNameServerAddress(producer, "192.168.0.149:9876"); - if(i == 1){ - SetProducerSendMsgTimeout(producer , 3); + if (i == 1) { + SetProducerSendMsgTimeout(producer, 3); } StartProducer(producer); printf("Producer start.....\n"); @@ -93,4 +89,3 @@ int main(int argc, char *argv[]) { CreateProducerAndStartSendMessage(1); return 0; } - diff --git a/example/OrderProducer.cpp b/example/OrderProducer.cpp old mode 100755 new mode 100644 index ba4636314..be98b01f4 --- a/example/OrderProducer.cpp +++ b/example/OrderProducer.cpp @@ -33,82 +33,76 @@ std::mutex g_mtx; boost::atomic g_quit(false); class SelectMessageQueueByHash : public MessageQueueSelector { - public: - MQMessageQueue select(const std::vector &mqs, - const MQMessage &msg, void *arg) { - int orderId = *static_cast(arg); - int index = orderId % mqs.size(); - return mqs[index]; - } +public: + MQMessageQueue select(const std::vector &mqs, const MQMessage &msg, void *arg) { + int orderId = *static_cast(arg); + int index = orderId % mqs.size(); + return mqs[index]; + } }; SelectMessageQueueByHash g_mySelector; -void ProducerWorker(RocketmqSendAndConsumerArgs *info, - DefaultMQProducer *producer) { - while (!g_quit.load()) { - if (g_msgCount.load() <= 0) { - std::unique_lock lck(g_mtx); - g_finished.notify_one(); +void ProducerWorker(RocketmqSendAndConsumerArgs *info, DefaultMQProducer *producer) { + while (!g_quit.load()) { + if (g_msgCount.load() <= 0) { + std::unique_lock lck(g_mtx); + g_finished.notify_one(); + } + MQMessage msg(info->topic, // topic + "*", // tag + info->body); // body + + int orderId = 1; + SendResult sendResult = producer->send(msg, &g_mySelector, static_cast(&orderId), info->retrytimes, + info->SelectUnactiveBroker); + --g_msgCount; } - MQMessage msg(info->topic, // topic - "*", // tag - info->body); // body - - int orderId = 1; - SendResult sendResult = - producer->send(msg, &g_mySelector, static_cast(&orderId), - info->retrytimes, info->SelectUnactiveBroker); - --g_msgCount; - } } int main(int argc, char *argv[]) { - RocketmqSendAndConsumerArgs info; - if (!ParseArgs(argc, argv, &info)) { - exit(-1); - } + RocketmqSendAndConsumerArgs info; + if (!ParseArgs(argc, argv, &info)) { + exit(-1); + } - DefaultMQProducer producer("please_rename_unique_group_name"); - PrintRocketmqSendAndConsumerArgs(info); + DefaultMQProducer producer("please_rename_unique_group_name"); + PrintRocketmqSendAndConsumerArgs(info); - producer.setNamesrvAddr(info.namesrv); - producer.setNamesrvDomain(info.namesrv_domain); - producer.setGroupName(info.groupname); - producer.setInstanceName(info.groupname); + producer.setNamesrvAddr(info.namesrv); + producer.setNamesrvDomain(info.namesrv_domain); + producer.setGroupName(info.groupname); + producer.setInstanceName(info.groupname); - producer.start(); + producer.start(); - int msgcount = g_msgCount.load(); - std::vector> work_pool; + int msgcount = g_msgCount.load(); + std::vector> work_pool; - int threadCount = info.thread_count; - for (int j = 0; j < threadCount; j++) { - std::shared_ptr th = - std::make_shared(ProducerWorker, &info, &producer); - work_pool.push_back(th); - } + int threadCount = info.thread_count; + for (int j = 0; j < threadCount; j++) { + std::shared_ptr th = std::make_shared(ProducerWorker, &info, &producer); + work_pool.push_back(th); + } - auto start = std::chrono::system_clock::now(); - { - std::unique_lock lck(g_mtx); - g_finished.wait(lck); - g_quit.store(true); - } + auto start = std::chrono::system_clock::now(); + { + std::unique_lock lck(g_mtx); + g_finished.wait(lck); + g_quit.store(true); + } - auto end = std::chrono::system_clock::now(); - auto duration = - std::chrono::duration_cast(end - start); + auto end = std::chrono::system_clock::now(); + auto duration = std::chrono::duration_cast(end - start); - std::cout - << "per msg time: " << duration.count() / (double)msgcount << "ms \n" - << "========================finished==============================\n"; + std::cout << "per msg time: " << duration.count() / (double)msgcount << "ms \n" + << "========================finished==============================\n"; - for (size_t th = 0; th != work_pool.size(); ++th) { - work_pool[th]->join(); - } + for (size_t th = 0; th != work_pool.size(); ++th) { + work_pool[th]->join(); + } - producer.shutdown(); + producer.shutdown(); - return 0; + return 0; } diff --git a/example/OrderlyPushConsumer.cpp b/example/OrderlyPushConsumer.cpp old mode 100755 new mode 100644 index 81ad53321..c78c11a27 --- a/example/OrderlyPushConsumer.cpp +++ b/example/OrderlyPushConsumer.cpp @@ -36,76 +36,78 @@ boost::atomic g_quit(false); TpsReportService g_tps; class MyMsgListener : public MessageListenerOrderly { - public: - MyMsgListener() {} - virtual ~MyMsgListener() {} +public: + MyMsgListener() {} + virtual ~MyMsgListener() {} - virtual ConsumeStatus consumeMessage(const vector &msgs) { - if (g_consumedCount.load() >= g_msgCount) { - std::unique_lock lK(g_mtx); - g_quit.store(true); - g_finished.notify_one(); + virtual ConsumeStatus consumeMessage(const vector &msgs) { + if (g_consumedCount.load() >= g_msgCount) { + std::unique_lock lK(g_mtx); + g_quit.store(true); + g_finished.notify_one(); + } + for (size_t i = 0; i < msgs.size(); i++) { + ++g_consumedCount; + g_tps.Increment(); + } + return CONSUME_SUCCESS; } - for (size_t i = 0; i < msgs.size(); i++) { - ++g_consumedCount; - g_tps.Increment(); - } - return CONSUME_SUCCESS; - } }; int main(int argc, char *argv[]) { - RocketmqSendAndConsumerArgs info; - if (!ParseArgs(argc, argv, &info)) { - exit(-1); - } - PrintRocketmqSendAndConsumerArgs(info); - DefaultMQPushConsumer consumer("please_rename_unique_group_name"); - DefaultMQProducer producer("please_rename_unique_group_name"); + RocketmqSendAndConsumerArgs info; + if (!ParseArgs(argc, argv, &info)) { + exit(-1); + } + PrintRocketmqSendAndConsumerArgs(info); + DefaultMQPushConsumer consumer("please_rename_unique_group_name"); + DefaultMQProducer producer("please_rename_unique_group_name"); - producer.setNamesrvAddr(info.namesrv); - producer.setGroupName("msg-persist-group_producer_sandbox"); - producer.start(); + producer.setNamesrvAddr(info.namesrv); + producer.setGroupName("msg-persist-group_producer_sandbox"); + producer.start(); - consumer.setNamesrvAddr(info.namesrv); - consumer.setNamesrvDomain(info.namesrv_domain); - consumer.setGroupName(info.groupname); - consumer.setConsumeFromWhere(CONSUME_FROM_LAST_OFFSET); - consumer.subscribe(info.topic, "*"); - consumer.setConsumeThreadCount(info.thread_count); - consumer.setConsumeMessageBatchMaxSize(31); - if (info.syncpush) consumer.setAsyncPull(false); + consumer.setNamesrvAddr(info.namesrv); + consumer.setNamesrvDomain(info.namesrv_domain); + consumer.setGroupName(info.groupname); + consumer.setConsumeFromWhere(CONSUME_FROM_LAST_OFFSET); + consumer.subscribe(info.topic, "*"); + consumer.setConsumeThreadCount(info.thread_count); + consumer.setConsumeMessageBatchMaxSize(31); + if (info.syncpush) consumer.setAsyncPull(false); - MyMsgListener msglistener; - consumer.registerMessageListener(&msglistener); - g_tps.start(); + MyMsgListener msglistener; + consumer.registerMessageListener(&msglistener); + g_tps.start(); - try { - consumer.start(); - } catch (MQClientException &e) { - std::cout << e << std::endl; - } + try { + consumer.start(); + } + catch (MQClientException &e) { + std::cout << e << std::endl; + } - int msgcount = g_msgCount.load(); - for (int i = 0; i < msgcount; ++i) { - MQMessage msg(info.topic, // topic - "*", // tag - info.body); // body + int msgcount = g_msgCount.load(); + for (int i = 0; i < msgcount; ++i) { + MQMessage msg(info.topic, // topic + "*", // tag + info.body); // body - try { - producer.send(msg); - } catch (MQException &e) { - std::cout << e << endl; // if catch excepiton , need re-send this msg by - // service + try { + producer.send(msg); + } + catch (MQException &e) { + std::cout << e << endl; // if catch excepiton , need re-send this msg by + // service + } } - } - while (!g_quit.load()) { - std::unique_lock lk(g_mtx); - g_finished.wait(lk); - } + while (!g_quit.load()) { + std::unique_lock lk(g_mtx); + g_finished.wait(lk); + } - producer.shutdown(); - consumer.shutdown(); - return 0; + producer.shutdown(); + consumer.shutdown(); + return 0; } diff --git a/example/Producer.c b/example/Producer.c index cfffd77b4..32691b21c 100644 --- a/example/Producer.c +++ b/example/Producer.c @@ -65,4 +65,3 @@ int main(int argc, char *argv[]) { printf("Producer Shutdown!\n"); return 0; } - diff --git a/example/PullConsumeMessage.c b/example/PullConsumeMessage.c index 657370f58..d1b99f84b 100644 --- a/example/PullConsumeMessage.c +++ b/example/PullConsumeMessage.c @@ -49,7 +49,7 @@ int main(int argc, char *argv[]) { for (i = 1; i <= 5; i++) { printf("FetchSubscriptionMessageQueues : %d times\n", i); ret = FetchSubscriptionMessageQueues(consumer, "T_TestTopic", &mqs, &size); - if(ret != OK) { + if (ret != OK) { printf("Get MQ Queue Failed,ErrorCode:%d\n", ret); } printf("Get MQ Size:%d\n", size); @@ -69,7 +69,8 @@ int main(int argc, char *argv[]) { case E_FOUND: printf("Get Message Size:%d\n", pullResult.size); for (k = 0; k < pullResult.size; ++k) { - printf("Got Message ID:%s,Body:%s\n", GetMessageId(pullResult.msgFoundList[k]),GetMessageBody(pullResult.msgFoundList[k])); + printf("Got Message ID:%s,Body:%s\n", GetMessageId(pullResult.msgFoundList[k]), + GetMessageBody(pullResult.msgFoundList[k])); } break; case E_NO_MATCHED_MSG: diff --git a/example/PullConsumer.cpp b/example/PullConsumer.cpp old mode 100755 new mode 100644 index 857ed74dc..c32f9dd9f --- a/example/PullConsumer.cpp +++ b/example/PullConsumer.cpp @@ -30,96 +30,92 @@ using namespace rocketmq; std::map g_offseTable; -void putMessageQueueOffset(MQMessageQueue mq, uint64_t offset) { - g_offseTable[mq] = offset; -} +void putMessageQueueOffset(MQMessageQueue mq, uint64_t offset) { g_offseTable[mq] = offset; } uint64_t getMessageQueueOffset(MQMessageQueue mq) { - map::iterator it = g_offseTable.find(mq); - if (it != g_offseTable.end()) { - return it->second; - } - return 0; + map::iterator it = g_offseTable.find(mq); + if (it != g_offseTable.end()) { + return it->second; + } + return 0; } int main(int argc, char *argv[]) { - RocketmqSendAndConsumerArgs info; - if (!ParseArgs(argc, argv, &info)) { - exit(-1); - } - PrintRocketmqSendAndConsumerArgs(info); - DefaultMQPullConsumer consumer("please_rename_unique_group_name"); - consumer.setNamesrvAddr(info.namesrv); - consumer.setNamesrvDomain(info.namesrv_domain); - consumer.setGroupName(info.groupname); - consumer.setInstanceName(info.groupname); - consumer.registerMessageQueueListener(info.topic, NULL); - consumer.start(); - std::vector mqs; + RocketmqSendAndConsumerArgs info; + if (!ParseArgs(argc, argv, &info)) { + exit(-1); + } + PrintRocketmqSendAndConsumerArgs(info); + DefaultMQPullConsumer consumer("please_rename_unique_group_name"); + consumer.setNamesrvAddr(info.namesrv); + consumer.setNamesrvDomain(info.namesrv_domain); + consumer.setGroupName(info.groupname); + consumer.setInstanceName(info.groupname); + consumer.registerMessageQueueListener(info.topic, NULL); + consumer.start(); + std::vector mqs; - try { - consumer.fetchSubscribeMessageQueues(info.topic, mqs); - auto iter = mqs.begin(); - for (; iter != mqs.end(); ++iter) { - std::cout << "mq:" << (*iter).toString() << endl; + try { + consumer.fetchSubscribeMessageQueues(info.topic, mqs); + auto iter = mqs.begin(); + for (; iter != mqs.end(); ++iter) { + std::cout << "mq:" << (*iter).toString() << endl; + } + } + catch (MQException &e) { + std::cout << e << endl; } - } catch (MQException &e) { - std::cout << e << endl; - } - auto start = std::chrono::system_clock::now(); - auto iter = mqs.begin(); - for (; iter != mqs.end(); ++iter) { - MQMessageQueue mq = (*iter); - // if cluster model - // putMessageQueueOffset(mq, g_consumer.fetchConsumeOffset(mq,true)); - // if broadcast model - // putMessageQueueOffset(mq, your last consume offset); + auto start = std::chrono::system_clock::now(); + auto iter = mqs.begin(); + for (; iter != mqs.end(); ++iter) { + MQMessageQueue mq = (*iter); + // if cluster model + // putMessageQueueOffset(mq, g_consumer.fetchConsumeOffset(mq,true)); + // if broadcast model + // putMessageQueueOffset(mq, your last consume offset); - bool noNewMsg = false; - do { - try { - PullResult result = - consumer.pull(mq, "*", getMessageQueueOffset(mq), 32); - g_msgCount += result.msgFoundList.size(); - std::cout << result.msgFoundList.size() << std::endl; - // if pull request timeout or received NULL response, pullStatus will be - // setted to BROKER_TIMEOUT, - // And nextBeginOffset/minOffset/MaxOffset will be setted to 0 - if (result.pullStatus != BROKER_TIMEOUT) { - putMessageQueueOffset(mq, result.nextBeginOffset); - PrintPullResult(&result); - } else { - cout << "broker timeout occur" << endl; - } - switch (result.pullStatus) { - case FOUND: - case NO_MATCHED_MSG: - case OFFSET_ILLEGAL: - case BROKER_TIMEOUT: - break; - case NO_NEW_MSG: - noNewMsg = true; - break; - default: - break; - } - } catch (MQClientException &e) { - std::cout << e << std::endl; - } - } while (!noNewMsg); - } + bool noNewMsg = false; + do { + try { + PullResult result = consumer.pull(mq, "*", getMessageQueueOffset(mq), 32); + g_msgCount += result.msgFoundList.size(); + std::cout << result.msgFoundList.size() << std::endl; + // if pull request timeout or received NULL response, pullStatus will be + // setted to BROKER_TIMEOUT, + // And nextBeginOffset/minOffset/MaxOffset will be setted to 0 + if (result.pullStatus != BROKER_TIMEOUT) { + putMessageQueueOffset(mq, result.nextBeginOffset); + PrintPullResult(&result); + } else { + cout << "broker timeout occur" << endl; + } + switch (result.pullStatus) { + case FOUND: + case NO_MATCHED_MSG: + case OFFSET_ILLEGAL: + case BROKER_TIMEOUT: + break; + case NO_NEW_MSG: + noNewMsg = true; + break; + default: + break; + } + } + catch (MQClientException &e) { + std::cout << e << std::endl; + } + } while (!noNewMsg); + } - auto end = std::chrono::system_clock::now(); - auto duration = - std::chrono::duration_cast(end - start); + auto end = std::chrono::system_clock::now(); + auto duration = std::chrono::duration_cast(end - start); - std::cout << "msg count: " << g_msgCount.load() << "\n"; - std::cout - << "per msg time: " << duration.count() / (double)g_msgCount.load() - << "ms \n" - << "========================finished==============================\n"; + std::cout << "msg count: " << g_msgCount.load() << "\n"; + std::cout << "per msg time: " << duration.count() / (double)g_msgCount.load() << "ms \n" + << "========================finished==============================\n"; - consumer.shutdown(); - return 0; + consumer.shutdown(); + return 0; } diff --git a/example/PushConsumer.cpp b/example/PushConsumer.cpp old mode 100755 new mode 100644 index 119b7f267..8252509d2 --- a/example/PushConsumer.cpp +++ b/example/PushConsumer.cpp @@ -35,92 +35,92 @@ TpsReportService g_tps; using namespace rocketmq; class MyMsgListener : public MessageListenerConcurrently { - public: - MyMsgListener() {} - virtual ~MyMsgListener() {} - - virtual ConsumeStatus consumeMessage(const std::vector &msgs) { - g_msgCount.store(g_msgCount.load() - msgs.size()); - for (size_t i = 0; i < msgs.size(); ++i) { - g_tps.Increment(); - // cout << "msg body: "<< msgs[i].getBody() << endl; +public: + MyMsgListener() {} + virtual ~MyMsgListener() {} + + virtual ConsumeStatus consumeMessage(const std::vector &msgs) { + g_msgCount.store(g_msgCount.load() - msgs.size()); + for (size_t i = 0; i < msgs.size(); ++i) { + g_tps.Increment(); + // cout << "msg body: "<< msgs[i].getBody() << endl; + } + + if (g_msgCount.load() <= 0) { + std::unique_lock lck(g_mtx); + g_finished.notify_one(); + } + return CONSUME_SUCCESS; } - - if (g_msgCount.load() <= 0) { - std::unique_lock lck(g_mtx); - g_finished.notify_one(); - } - return CONSUME_SUCCESS; - } }; int main(int argc, char *argv[]) { - RocketmqSendAndConsumerArgs info; - if (!ParseArgs(argc, argv, &info)) { - exit(-1); - } - PrintRocketmqSendAndConsumerArgs(info); - DefaultMQPushConsumer consumer("please_rename_unique_group_name"); - DefaultMQProducer producer("please_rename_unique_group_name"); - producer.setSessionCredentials("AccessKey", - "SecretKey", "ALIYUN"); - producer.setTcpTransportTryLockTimeout(1000); - producer.setTcpTransportConnectTimeout(400); - producer.setNamesrvDomain(info.namesrv_domain); - producer.setNamesrvAddr(info.namesrv); - producer.setGroupName("msg-persist-group_producer_sandbox"); - producer.start(); - - consumer.setNamesrvAddr(info.namesrv); - consumer.setGroupName(info.groupname); - consumer.setSessionCredentials("AccessKey", - "SecretKey", "ALIYUN"); - consumer.setConsumeThreadCount(info.thread_count); - consumer.setNamesrvDomain(info.namesrv_domain); - consumer.setConsumeFromWhere(CONSUME_FROM_LAST_OFFSET); - - if (info.syncpush) consumer.setAsyncPull(false); // set sync pull - if (info.broadcasting) { - consumer.setMessageModel(rocketmq::BROADCASTING); - } - - consumer.setInstanceName(info.groupname); - - consumer.subscribe(info.topic, "*"); - consumer.setConsumeThreadCount(15); - consumer.setTcpTransportTryLockTimeout(1000); - consumer.setTcpTransportConnectTimeout(400); - - MyMsgListener msglistener; - consumer.registerMessageListener(&msglistener); - - try { - consumer.start(); - } catch (MQClientException &e) { - cout << e << endl; - } - g_tps.start(); - - int msgcount = g_msgCount.load(); - for (int i = 0; i < msgcount; ++i) { - MQMessage msg(info.topic, // topic - "*", // tag - info.body); // body - - // std::this_thread::sleep_for(std::chrono::seconds(100000)); + RocketmqSendAndConsumerArgs info; + if (!ParseArgs(argc, argv, &info)) { + exit(-1); + } + PrintRocketmqSendAndConsumerArgs(info); + DefaultMQPushConsumer consumer("please_rename_unique_group_name"); + DefaultMQProducer producer("please_rename_unique_group_name"); + producer.setSessionCredentials("AccessKey", "SecretKey", "ALIYUN"); + producer.setTcpTransportTryLockTimeout(1000); + producer.setTcpTransportConnectTimeout(400); + producer.setNamesrvDomain(info.namesrv_domain); + producer.setNamesrvAddr(info.namesrv); + producer.setGroupName("msg-persist-group_producer_sandbox"); + producer.start(); + + consumer.setNamesrvAddr(info.namesrv); + consumer.setGroupName(info.groupname); + consumer.setSessionCredentials("AccessKey", "SecretKey", "ALIYUN"); + consumer.setConsumeThreadCount(info.thread_count); + consumer.setNamesrvDomain(info.namesrv_domain); + consumer.setConsumeFromWhere(CONSUME_FROM_LAST_OFFSET); + + if (info.syncpush) consumer.setAsyncPull(false); // set sync pull + if (info.broadcasting) { + consumer.setMessageModel(rocketmq::BROADCASTING); + } + + consumer.setInstanceName(info.groupname); + + consumer.subscribe(info.topic, "*"); + consumer.setConsumeThreadCount(15); + consumer.setTcpTransportTryLockTimeout(1000); + consumer.setTcpTransportConnectTimeout(400); + + MyMsgListener msglistener; + consumer.registerMessageListener(&msglistener); + try { - producer.send(msg); - } catch (MQException &e) { - std::cout << e << endl; // if catch excepiton , need re-send this msg by - // service + consumer.start(); + } + catch (MQClientException &e) { + cout << e << endl; + } + g_tps.start(); + + int msgcount = g_msgCount.load(); + for (int i = 0; i < msgcount; ++i) { + MQMessage msg(info.topic, // topic + "*", // tag + info.body); // body + + // std::this_thread::sleep_for(std::chrono::seconds(100000)); + try { + producer.send(msg); + } + catch (MQException &e) { + std::cout << e << endl; // if catch excepiton , need re-send this msg by + // service + } + } + + { + std::unique_lock lck(g_mtx); + g_finished.wait(lck); } - } - - { - std::unique_lock lck(g_mtx); - g_finished.wait(lck); - } - producer.shutdown(); - consumer.shutdown(); - return 0; + producer.shutdown(); + consumer.shutdown(); + return 0; } diff --git a/example/PushConsumerOrderly.c b/example/PushConsumerOrderly.c index 63ca5275c..0624624b4 100644 --- a/example/PushConsumerOrderly.c +++ b/example/PushConsumerOrderly.c @@ -22,31 +22,26 @@ #include "CCommon.h" #include "CMessageExt.h" - -int doConsumeMessage(struct CPushConsumer * consumer, CMessageExt * msgExt) -{ +int doConsumeMessage(struct CPushConsumer* consumer, CMessageExt* msgExt) { printf("Hello,doConsumeMessage by Application!\n"); - printf("Msg Topic:%s\n",GetMessageTopic(msgExt)); - printf("Msg Tags:%s\n",GetMessageTags(msgExt)); - printf("Msg Keys:%s\n",GetMessageKeys(msgExt)); - printf("Msg Body:%s\n",GetMessageBody(msgExt)); + printf("Msg Topic:%s\n", GetMessageTopic(msgExt)); + printf("Msg Tags:%s\n", GetMessageTags(msgExt)); + printf("Msg Keys:%s\n", GetMessageKeys(msgExt)); + printf("Msg Body:%s\n", GetMessageBody(msgExt)); return E_CONSUME_SUCCESS; } - -int main(int argc,char * argv []) -{ +int main(int argc, char* argv[]) { int i = 0; printf("PushConsumer Initializing....\n"); CPushConsumer* consumer = CreatePushConsumer("Group_Consumer_Test"); - SetPushConsumerNameServerAddress(consumer,"127.0.0.1:9876"); - Subscribe(consumer,"test","*"); - RegisterMessageCallbackOrderly(consumer,doConsumeMessage); + SetPushConsumerNameServerAddress(consumer, "127.0.0.1:9876"); + Subscribe(consumer, "test", "*"); + RegisterMessageCallbackOrderly(consumer, doConsumeMessage); StartPushConsumer(consumer); printf("Push Consumer Start...\n"); - for( i=0; i<10; i++) - { - printf("Now Running : %d S\n",i*10); + for (i = 0; i < 10; i++) { + printf("Now Running : %d S\n", i * 10); sleep(10); } ShutdownPushConsumer(consumer); diff --git a/example/README.md b/example/README.md index 91965d012..378a4ead3 100644 --- a/example/README.md +++ b/example/README.md @@ -1,4 +1 @@ -1. AsyncProducer -2. OrderlyProducer -3. SyncProducer - +1. AsyncProducer 2. OrderlyProducer 3. SyncProducer diff --git a/example/SendDelayMsg.cpp b/example/SendDelayMsg.cpp old mode 100755 new mode 100644 index 248642780..ff545bb4f --- a/example/SendDelayMsg.cpp +++ b/example/SendDelayMsg.cpp @@ -31,36 +31,37 @@ using namespace rocketmq; int main(int argc, char* argv[]) { - RocketmqSendAndConsumerArgs info; - if (!ParseArgs(argc, argv, &info)) { - exit(-1); - } - PrintRocketmqSendAndConsumerArgs(info); - DefaultMQProducer producer("please_rename_unique_group_name"); - producer.setNamesrvAddr(info.namesrv); - producer.setNamesrvDomain(info.namesrv_domain); - producer.setGroupName(info.groupname); - producer.setInstanceName(info.groupname); + RocketmqSendAndConsumerArgs info; + if (!ParseArgs(argc, argv, &info)) { + exit(-1); + } + PrintRocketmqSendAndConsumerArgs(info); + DefaultMQProducer producer("please_rename_unique_group_name"); + producer.setNamesrvAddr(info.namesrv); + producer.setNamesrvDomain(info.namesrv_domain); + producer.setGroupName(info.groupname); + producer.setInstanceName(info.groupname); - producer.setSendMsgTimeout(500); - producer.setTcpTransportTryLockTimeout(1000); - producer.setTcpTransportConnectTimeout(400); + producer.setSendMsgTimeout(500); + producer.setTcpTransportTryLockTimeout(1000); + producer.setTcpTransportConnectTimeout(400); - producer.start(); + producer.start(); - MQMessage msg(info.topic, // topic - "*", // tag - info.body); // body + MQMessage msg(info.topic, // topic + "*", // tag + info.body); // body - // messageDelayLevel=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h - // 2h - msg.setDelayTimeLevel(5); // 1m - try { - SendResult sendResult = producer.send(msg, info.SelectUnactiveBroker); - } catch (const MQException& e) { - std::cout << "send failed: " << std::endl; - } + // messageDelayLevel=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h + // 2h + msg.setDelayTimeLevel(5); // 1m + try { + SendResult sendResult = producer.send(msg, info.SelectUnactiveBroker); + } + catch (const MQException& e) { + std::cout << "send failed: " << std::endl; + } - producer.shutdown(); - return 0; + producer.shutdown(); + return 0; } diff --git a/example/SyncProducer.cpp b/example/SyncProducer.cpp old mode 100755 new mode 100644 index f5682a697..91091a618 --- a/example/SyncProducer.cpp +++ b/example/SyncProducer.cpp @@ -35,82 +35,78 @@ std::mutex g_mtx; std::condition_variable g_finished; TpsReportService g_tps; -void SyncProducerWorker(RocketmqSendAndConsumerArgs* info, - DefaultMQProducer* producer) { - while (!g_quit.load()) { - if (g_msgCount.load() <= 0) { - std::unique_lock lck(g_mtx); - g_finished.notify_one(); +void SyncProducerWorker(RocketmqSendAndConsumerArgs* info, DefaultMQProducer* producer) { + while (!g_quit.load()) { + if (g_msgCount.load() <= 0) { + std::unique_lock lck(g_mtx); + g_finished.notify_one(); + } + MQMessage msg(info->topic, // topic + "*", // tag + info->body); // body + try { + auto start = std::chrono::system_clock::now(); + SendResult sendResult = producer->send(msg, info->SelectUnactiveBroker); + g_tps.Increment(); + --g_msgCount; + auto end = std::chrono::system_clock::now(); + auto duration = std::chrono::duration_cast(end - start); + if (duration.count() >= 500) { + std::cout << "send RT more than: " << duration.count() << " ms with msgid: " << sendResult.getMsgId() + << endl; + } + } + catch (const MQException& e) { + std::cout << "send failed: " << std::endl; + } } - MQMessage msg(info->topic, // topic - "*", // tag - info->body); // body - try { - auto start = std::chrono::system_clock::now(); - SendResult sendResult = producer->send(msg, info->SelectUnactiveBroker); - g_tps.Increment(); - --g_msgCount; - auto end = std::chrono::system_clock::now(); - auto duration = - std::chrono::duration_cast(end - start); - if (duration.count() >= 500) { - std::cout << "send RT more than: " << duration.count() - << " ms with msgid: " << sendResult.getMsgId() << endl; - } - } catch (const MQException& e) { - std::cout << "send failed: " << std::endl; - } - } } int main(int argc, char* argv[]) { - RocketmqSendAndConsumerArgs info; - if (!ParseArgs(argc, argv, &info)) { - exit(-1); - } - PrintRocketmqSendAndConsumerArgs(info); - DefaultMQProducer producer("please_rename_unique_group_name"); - producer.setNamesrvAddr(info.namesrv); - producer.setNamesrvDomain(info.namesrv_domain); - producer.setGroupName(info.groupname); - producer.setInstanceName(info.groupname); - producer.setSessionCredentials("mq acesskey", "mq secretkey", "ALIYUN"); - producer.setSendMsgTimeout(500); - producer.setTcpTransportTryLockTimeout(1000); - producer.setTcpTransportConnectTimeout(400); + RocketmqSendAndConsumerArgs info; + if (!ParseArgs(argc, argv, &info)) { + exit(-1); + } + PrintRocketmqSendAndConsumerArgs(info); + DefaultMQProducer producer("please_rename_unique_group_name"); + producer.setNamesrvAddr(info.namesrv); + producer.setNamesrvDomain(info.namesrv_domain); + producer.setGroupName(info.groupname); + producer.setInstanceName(info.groupname); + producer.setSessionCredentials("mq acesskey", "mq secretkey", "ALIYUN"); + producer.setSendMsgTimeout(500); + producer.setTcpTransportTryLockTimeout(1000); + producer.setTcpTransportConnectTimeout(400); - producer.start(); - std::vector> work_pool; - auto start = std::chrono::system_clock::now(); - int msgcount = g_msgCount.load(); - g_tps.start(); + producer.start(); + std::vector> work_pool; + auto start = std::chrono::system_clock::now(); + int msgcount = g_msgCount.load(); + g_tps.start(); - int threadCount = info.thread_count; - for (int j = 0; j < threadCount; j++) { - std::shared_ptr th = - std::make_shared(SyncProducerWorker, &info, &producer); - work_pool.push_back(th); - } + int threadCount = info.thread_count; + for (int j = 0; j < threadCount; j++) { + std::shared_ptr th = std::make_shared(SyncProducerWorker, &info, &producer); + work_pool.push_back(th); + } - { - std::unique_lock lck(g_mtx); - g_finished.wait(lck); - g_quit.store(true); - } + { + std::unique_lock lck(g_mtx); + g_finished.wait(lck); + g_quit.store(true); + } - auto end = std::chrono::system_clock::now(); - auto duration = - std::chrono::duration_cast(end - start); + auto end = std::chrono::system_clock::now(); + auto duration = std::chrono::duration_cast(end - start); - std::cout - << "per msg time: " << duration.count() / (double)msgcount << "ms \n" - << "========================finished==============================\n"; + std::cout << "per msg time: " << duration.count() / (double)msgcount << "ms \n" + << "========================finished==============================\n"; - for (size_t th = 0; th != work_pool.size(); ++th) { - work_pool[th]->join(); - } + for (size_t th = 0; th != work_pool.size(); ++th) { + work_pool[th]->join(); + } - producer.shutdown(); + producer.shutdown(); - return 0; + return 0; } diff --git a/example/common.h b/example/common.h old mode 100755 new mode 100644 index 21d2ba02f..c81e71141 --- a/example/common.h +++ b/example/common.h @@ -35,68 +35,67 @@ using namespace std; boost::atomic g_msgCount(1); class RocketmqSendAndConsumerArgs { - public: - RocketmqSendAndConsumerArgs() - : body("msgbody for test"), - thread_count(boost::thread::hardware_concurrency()), - broadcasting(false), - syncpush(false), - SelectUnactiveBroker(false), - IsAutoDeleteSendCallback(false), - retrytimes(5), - PrintMoreInfo(false) {} +public: + RocketmqSendAndConsumerArgs() + : body("msgbody for test"), + thread_count(boost::thread::hardware_concurrency()), + broadcasting(false), + syncpush(false), + SelectUnactiveBroker(false), + IsAutoDeleteSendCallback(false), + retrytimes(5), + PrintMoreInfo(false) {} - public: - std::string namesrv; - std::string namesrv_domain; - std::string groupname; - std::string topic; - std::string body; - int thread_count; - bool broadcasting; - bool syncpush; - bool SelectUnactiveBroker; // default select active broker - bool IsAutoDeleteSendCallback; - int retrytimes; // default retry 5 times; - bool PrintMoreInfo; +public: + std::string namesrv; + std::string namesrv_domain; + std::string groupname; + std::string topic; + std::string body; + int thread_count; + bool broadcasting; + bool syncpush; + bool SelectUnactiveBroker; // default select active broker + bool IsAutoDeleteSendCallback; + int retrytimes; // default retry 5 times; + bool PrintMoreInfo; }; class TpsReportService { - public: - TpsReportService() : tps_interval_(1), quit_flag_(false), tps_count_(0) {} - void start() { - if (tps_thread_ == NULL) { - std::cout << "tps_thread_ is null" << std::endl; - return; +public: + TpsReportService() : tps_interval_(1), quit_flag_(false), tps_count_(0) {} + void start() { + if (tps_thread_ == NULL) { + std::cout << "tps_thread_ is null" << std::endl; + return; + } + tps_thread_.reset(new boost::thread(boost::bind(&TpsReportService::TpsReport, this))); } - tps_thread_.reset( - new boost::thread(boost::bind(&TpsReportService::TpsReport, this))); - } - ~TpsReportService() { - quit_flag_.store(true); - if (tps_thread_ == NULL) { - std::cout << "tps_thread_ is null" << std::endl; - return; + ~TpsReportService() { + quit_flag_.store(true); + if (tps_thread_ == NULL) { + std::cout << "tps_thread_ is null" << std::endl; + return; + } + if (tps_thread_->joinable()) tps_thread_->join(); } - if (tps_thread_->joinable()) tps_thread_->join(); - } - void Increment() { ++tps_count_; } + void Increment() { ++tps_count_; } - void TpsReport() { - while (!quit_flag_.load()) { - boost::this_thread::sleep_for(tps_interval_); - std::cout << "tps: " << tps_count_.load() << std::endl; - tps_count_.store(0); + void TpsReport() { + while (!quit_flag_.load()) { + boost::this_thread::sleep_for(tps_interval_); + std::cout << "tps: " << tps_count_.load() << std::endl; + tps_count_.store(0); + } } - } - private: - boost::chrono::seconds tps_interval_; - boost::shared_ptr tps_thread_; - boost::atomic quit_flag_; - boost::atomic tps_count_; +private: + boost::chrono::seconds tps_interval_; + boost::shared_ptr tps_thread_; + boost::atomic quit_flag_; + boost::atomic tps_count_; }; /* @@ -109,122 +108,113 @@ static void PrintResult(rocketmq::SendResult* result) { */ void PrintPullResult(rocketmq::PullResult* result) { - std::cout << result->toString() << std::endl; - if (result->pullStatus == rocketmq::FOUND) { - std::cout << result->toString() << endl; - std::vector::iterator it = - result->msgFoundList.begin(); - for (; it != result->msgFoundList.end(); ++it) { - cout << "=======================================================" << endl - << (*it).toString() << endl; + std::cout << result->toString() << std::endl; + if (result->pullStatus == rocketmq::FOUND) { + std::cout << result->toString() << endl; + std::vector::iterator it = result->msgFoundList.begin(); + for (; it != result->msgFoundList.end(); ++it) { + cout << "=======================================================" << endl << (*it).toString() << endl; + } } - } } -static void PrintRocketmqSendAndConsumerArgs( - const RocketmqSendAndConsumerArgs& info) { - std::cout << "nameserver: " << info.namesrv << endl - << "topic: " << info.topic << endl - << "groupname: " << info.groupname << endl - << "produce content: " << info.body << endl - << "msg count: " << g_msgCount.load() << endl - << "thread count: " << info.thread_count << endl; +static void PrintRocketmqSendAndConsumerArgs(const RocketmqSendAndConsumerArgs& info) { + std::cout << "nameserver: " << info.namesrv << endl << "topic: " << info.topic << endl + << "groupname: " << info.groupname << endl << "produce content: " << info.body << endl + << "msg count: " << g_msgCount.load() << endl << "thread count: " << info.thread_count << endl; } static void help() { - std::cout - << "need option,like follow: \n" - << "-n nameserver addr, if not set -n and -i ,no nameSrv will be got \n" - "-i nameserver domain name, if not set -n and -i ,no nameSrv will be " - "got \n" - "-g groupname \n" - "-t msg topic \n" - "-m messagecout(default value: 1) \n" - "-c content(default value: only test ) \n" - "-b (BROADCASTING model, default value: CLUSTER) \n" - "-s sync push(default is async push)\n" - "-r setup retry times(default value: 5 times)\n" - "-u select active broker to send msg(default value: false)\n" - "-d use AutoDeleteSendcallback by cpp client(defalut value: false) \n" - "-T thread count of send msg or consume msg(defalut value: system cpu " - "core number) \n" - "-v print more details information \n"; + std::cout << "need option,like follow: \n" + << "-n nameserver addr, if not set -n and -i ,no nameSrv will be got \n" + "-i nameserver domain name, if not set -n and -i ,no nameSrv will be " + "got \n" + "-g groupname \n" + "-t msg topic \n" + "-m messagecout(default value: 1) \n" + "-c content(default value: only test ) \n" + "-b (BROADCASTING model, default value: CLUSTER) \n" + "-s sync push(default is async push)\n" + "-r setup retry times(default value: 5 times)\n" + "-u select active broker to send msg(default value: false)\n" + "-d use AutoDeleteSendcallback by cpp client(defalut value: false) \n" + "-T thread count of send msg or consume msg(defalut value: system cpu " + "core number) \n" + "-v print more details information \n"; } -static bool ParseArgs(int argc, char* argv[], - RocketmqSendAndConsumerArgs* info) { +static bool ParseArgs(int argc, char* argv[], RocketmqSendAndConsumerArgs* info) { #ifndef WIN32 - int ch; - while ((ch = getopt(argc, argv, "n:i:g:t:m:c:b:s:h:r:T:bu")) != -1) { - switch (ch) { - case 'n': - info->namesrv.insert(0, optarg); - break; - case 'i': - info->namesrv_domain.insert(0, optarg); - break; - case 'g': - info->groupname.insert(0, optarg); - break; - case 't': - info->topic.insert(0, optarg); - break; - case 'm': - g_msgCount.store(atoi(optarg)); - break; - case 'c': - info->body.insert(0, optarg); - break; - case 'b': - info->broadcasting = true; - break; - case 's': - info->syncpush = true; - break; - case 'r': - info->retrytimes = atoi(optarg); - break; - case 'u': - info->SelectUnactiveBroker = true; - break; - case 'T': - info->thread_count = atoi(optarg); - break; - case 'v': - info->PrintMoreInfo = true; - break; - case 'h': - help(); - return false; - default: - help(); - return false; + int ch; + while ((ch = getopt(argc, argv, "n:i:g:t:m:c:b:s:h:r:T:bu")) != -1) { + switch (ch) { + case 'n': + info->namesrv.insert(0, optarg); + break; + case 'i': + info->namesrv_domain.insert(0, optarg); + break; + case 'g': + info->groupname.insert(0, optarg); + break; + case 't': + info->topic.insert(0, optarg); + break; + case 'm': + g_msgCount.store(atoi(optarg)); + break; + case 'c': + info->body.insert(0, optarg); + break; + case 'b': + info->broadcasting = true; + break; + case 's': + info->syncpush = true; + break; + case 'r': + info->retrytimes = atoi(optarg); + break; + case 'u': + info->SelectUnactiveBroker = true; + break; + case 'T': + info->thread_count = atoi(optarg); + break; + case 'v': + info->PrintMoreInfo = true; + break; + case 'h': + help(); + return false; + default: + help(); + return false; + } } - } #else - rocketmq::Arg_helper arg_help(argc, argv); - info->namesrv = arg_help.get_option_value("-n"); - info->namesrv_domain = arg_help.get_option_value("-i"); - info->groupname = arg_help.get_option_value("-g"); - info->topic = arg_help.get_option_value("-t"); - info->broadcasting = atoi(arg_help.get_option_value("-b").c_str()); - string msgContent(arg_help.get_option_value("-c")); - if (!msgContent.empty()) info->body = msgContent; - info->syncpush = atoi(arg_help.get_option_value("-s").c_str()); - int retrytimes = atoi(arg_help.get_option_value("-r").c_str()); - if (retrytimes > 0) info->retrytimes = retrytimes; - info->SelectUnactiveBroker = atoi(arg_help.get_option_value("-u").c_str()); - int thread_count = atoi(arg_help.get_option_value("-T").c_str()); - if (thread_count > 0) info->thread_count = thread_count; - info->PrintMoreInfo = atoi(arg_help.get_option_value("-v").c_str()); - g_msgCount = atoi(arg_help.get_option_value("-m").c_str()); + rocketmq::Arg_helper arg_help(argc, argv); + info->namesrv = arg_help.get_option_value("-n"); + info->namesrv_domain = arg_help.get_option_value("-i"); + info->groupname = arg_help.get_option_value("-g"); + info->topic = arg_help.get_option_value("-t"); + info->broadcasting = atoi(arg_help.get_option_value("-b").c_str()); + string msgContent(arg_help.get_option_value("-c")); + if (!msgContent.empty()) info->body = msgContent; + info->syncpush = atoi(arg_help.get_option_value("-s").c_str()); + int retrytimes = atoi(arg_help.get_option_value("-r").c_str()); + if (retrytimes > 0) info->retrytimes = retrytimes; + info->SelectUnactiveBroker = atoi(arg_help.get_option_value("-u").c_str()); + int thread_count = atoi(arg_help.get_option_value("-T").c_str()); + if (thread_count > 0) info->thread_count = thread_count; + info->PrintMoreInfo = atoi(arg_help.get_option_value("-v").c_str()); + g_msgCount = atoi(arg_help.get_option_value("-m").c_str()); #endif - if (info->groupname.empty() || info->topic.empty() || - (info->namesrv_domain.empty() && info->namesrv.empty())) { - std::cout << "please use -g to setup groupname and -t setup topic \n"; - help(); - return false; - } - return true; + if (info->groupname.empty() || info->topic.empty() || (info->namesrv_domain.empty() && info->namesrv.empty())) { + std::cout << "please use -g to setup groupname and -t setup topic \n"; + help(); + return false; + } + return true; } #endif // ROCKETMQ_CLIENT4CPP_EXAMPLE_COMMON_H_ diff --git a/include/Arg_helper.h b/include/Arg_helper.h old mode 100755 new mode 100644 index f243fc162..e38ba9b22 --- a/include/Arg_helper.h +++ b/include/Arg_helper.h @@ -25,15 +25,15 @@ namespace rocketmq { // m_args; +private: + std::vector m_args; }; // namespace rocketmq { - class BatchMessage : public MQMessage { - public: - static std::string encode(std::vector &msgs); - static std::string encode(MQMessage &message); - }; +class BatchMessage : public MQMessage { +public: + static std::string encode(std::vector &msgs); + static std::string encode(MQMessage &message); +}; } #endif \ No newline at end of file diff --git a/include/CCommon.h b/include/CCommon.h index a1864b975..b363f91e1 100644 --- a/include/CCommon.h +++ b/include/CCommon.h @@ -36,12 +36,10 @@ typedef enum _CStatus_ { PRODUCER_SEND_SYNC_FAILED = 11, PRODUCER_SEND_ONEWAY_FAILED = 12, PRODUCER_SEND_ORDERLY_FAILED = 13, - PRODUCER_SEND_ASYNC_FAILED = 14, + PRODUCER_SEND_ASYNC_FAILED = 14, PRODUCER_SEND_ORDERLYASYNC_FAILED = 15, - PUSHCONSUMER_ERROR_CODE_START = 20, PUSHCONSUMER_START_FAILED = 20, - PULLCONSUMER_ERROR_CODE_START = 30, PULLCONSUMER_START_FAILED = 30, PULLCONSUMER_FETCH_MQ_FAILED = 31, @@ -76,7 +74,10 @@ typedef enum _CLogLevel_ { #define ROCKETMQCLIENT_API #endif -typedef enum _CMessageModel_ { BROADCASTING, CLUSTERING } CMessageModel; +typedef enum _CMessageModel_ { + BROADCASTING, + CLUSTERING +} CMessageModel; #ifdef __cplusplus }; diff --git a/include/CMQException.h b/include/CMQException.h index 69706e83c..3c2d626a1 100644 --- a/include/CMQException.h +++ b/include/CMQException.h @@ -22,10 +22,10 @@ extern "C" { #endif -#define MAX_EXEPTION_MSG_LENGTH 512 +#define MAX_EXEPTION_MSG_LENGTH 512 #define MAX_EXEPTION_FILE_LENGTH 256 #define MAX_EXEPTION_TYPE_LENGTH 128 -typedef struct _CMQException_{ +typedef struct _CMQException_ { int error; int line; char file[MAX_EXEPTION_FILE_LENGTH]; diff --git a/include/CMessage.h b/include/CMessage.h index cef7889e9..d7ec12d2e 100644 --- a/include/CMessage.h +++ b/include/CMessage.h @@ -23,11 +23,10 @@ extern "C" { #endif -//typedef struct _CMessage_ CMessage; +// typedef struct _CMessage_ CMessage; typedef struct CMessage CMessage; - -ROCKETMQCLIENT_API CMessage * CreateMessage(const char *topic); +ROCKETMQCLIENT_API CMessage *CreateMessage(const char *topic); ROCKETMQCLIENT_API int DestroyMessage(CMessage *msg); ROCKETMQCLIENT_API int SetMessageTopic(CMessage *msg, const char *topic); ROCKETMQCLIENT_API int SetMessageTags(CMessage *msg, const char *tags); @@ -40,4 +39,4 @@ ROCKETMQCLIENT_API int SetDelayTimeLevel(CMessage *msg, int level); #ifdef __cplusplus }; #endif -#endif //__C_MESSAGE_H__ +#endif //__C_MESSAGE_H__ diff --git a/include/CMessageExt.h b/include/CMessageExt.h index 26336aaae..d49b1b2f1 100644 --- a/include/CMessageExt.h +++ b/include/CMessageExt.h @@ -24,7 +24,7 @@ extern "C" { #endif -//typedef struct _CMessageExt_ _CMessageExt; +// typedef struct _CMessageExt_ _CMessageExt; typedef struct CMessageExt CMessageExt; ROCKETMQCLIENT_API const char *GetMessageTopic(CMessageExt *msgExt); @@ -46,4 +46,4 @@ ROCKETMQCLIENT_API long long GetMessagePreparedTransactionOffset(CMessageExt *ms #ifdef __cplusplus }; #endif -#endif //__C_MESSAGE_EXT_H__ +#endif //__C_MESSAGE_EXT_H__ diff --git a/include/CMessageQueue.h b/include/CMessageQueue.h index 273f536aa..6cd0a7059 100644 --- a/include/CMessageQueue.h +++ b/include/CMessageQueue.h @@ -25,12 +25,12 @@ extern "C" { #endif typedef struct _CMessageQueue_ { - char topic[MAX_TOPIC_LENGTH]; - char brokerName[MAX_BROKER_NAME_ID_LENGTH]; - int queueId; + char topic[MAX_TOPIC_LENGTH]; + char brokerName[MAX_BROKER_NAME_ID_LENGTH]; + int queueId; } CMessageQueue; #ifdef __cplusplus }; #endif -#endif //__C_MESSAGE_H__ +#endif //__C_MESSAGE_H__ diff --git a/include/CProducer.h b/include/CProducer.h index c542dc20d..28871dfde 100644 --- a/include/CProducer.h +++ b/include/CProducer.h @@ -26,11 +26,11 @@ extern "C" { #endif -//typedef struct _CProducer_ _CProducer; +// typedef struct _CProducer_ _CProducer; typedef struct CProducer CProducer; -typedef int(*QueueSelectorCallback)(int size, CMessage *msg, void *arg); -typedef void(*CSendSuccessCallback)(CSendResult result); -typedef void(*CSendExceptionCallback)(CMQException e); +typedef int (*QueueSelectorCallback)(int size, CMessage *msg, void *arg); +typedef void (*CSendSuccessCallback)(CSendResult result); +typedef void (*CSendExceptionCallback)(CMQException e); ROCKETMQCLIENT_API CProducer *CreateProducer(const char *groupId); ROCKETMQCLIENT_API int DestroyProducer(CProducer *producer); @@ -42,7 +42,7 @@ ROCKETMQCLIENT_API int SetProducerNameServerDomain(CProducer *producer, const ch ROCKETMQCLIENT_API int SetProducerGroupName(CProducer *producer, const char *groupName); ROCKETMQCLIENT_API int SetProducerInstanceName(CProducer *producer, const char *instanceName); ROCKETMQCLIENT_API int SetProducerSessionCredentials(CProducer *producer, const char *accessKey, const char *secretKey, - const char *onsChannel); + const char *onsChannel); ROCKETMQCLIENT_API int SetProducerLogPath(CProducer *producer, const char *logPath); ROCKETMQCLIENT_API int SetProducerLogFileNumAndSize(CProducer *producer, int fileNum, long fileSize); ROCKETMQCLIENT_API int SetProducerLogLevel(CProducer *producer, CLogLevel level); @@ -51,15 +51,19 @@ ROCKETMQCLIENT_API int SetProducerCompressLevel(CProducer *producer, int level); ROCKETMQCLIENT_API int SetProducerMaxMessageSize(CProducer *producer, int size); ROCKETMQCLIENT_API int SendMessageSync(CProducer *producer, CMessage *msg, CSendResult *result); -ROCKETMQCLIENT_API int SendMessageAsync(CProducer *producer, CMessage *msg, CSendSuccessCallback cSendSuccessCallback , CSendExceptionCallback cSendExceptionCallback); -ROCKETMQCLIENT_API int SendMessageOneway(CProducer *producer,CMessage *msg); -ROCKETMQCLIENT_API int SendMessageOnewayOrderly(CProducer *producer,CMessage *msg, QueueSelectorCallback selector, void* arg); -ROCKETMQCLIENT_API int SendMessageOrderly(CProducer *producer, CMessage *msg, QueueSelectorCallback callback, void *arg, int autoRetryTimes, CSendResult *result); +ROCKETMQCLIENT_API int SendMessageAsync(CProducer *producer, CMessage *msg, CSendSuccessCallback cSendSuccessCallback, + CSendExceptionCallback cSendExceptionCallback); +ROCKETMQCLIENT_API int SendMessageOneway(CProducer *producer, CMessage *msg); +ROCKETMQCLIENT_API int SendMessageOnewayOrderly(CProducer *producer, CMessage *msg, QueueSelectorCallback selector, + void *arg); +ROCKETMQCLIENT_API int SendMessageOrderly(CProducer *producer, CMessage *msg, QueueSelectorCallback callback, void *arg, + int autoRetryTimes, CSendResult *result); - -ROCKETMQCLIENT_API int SendMessageOrderlyAsync(CProducer *producer,CMessage *msg,QueueSelectorCallback callback,void *arg,CSendSuccessCallback cSendSuccessCallback,CSendExceptionCallback cSendExceptionCallback ); +ROCKETMQCLIENT_API int SendMessageOrderlyAsync(CProducer *producer, CMessage *msg, QueueSelectorCallback callback, + void *arg, CSendSuccessCallback cSendSuccessCallback, + CSendExceptionCallback cSendExceptionCallback); #ifdef __cplusplus }; #endif -#endif //__C_PRODUCER_H__ +#endif //__C_PRODUCER_H__ diff --git a/include/CPullConsumer.h b/include/CPullConsumer.h index a4c4f5aef..4052be490 100644 --- a/include/CPullConsumer.h +++ b/include/CPullConsumer.h @@ -27,10 +27,8 @@ extern "C" { #endif - typedef struct CPullConsumer CPullConsumer; - ROCKETMQCLIENT_API CPullConsumer *CreatePullConsumer(const char *groupId); ROCKETMQCLIENT_API int DestroyPullConsumer(CPullConsumer *consumer); ROCKETMQCLIENT_API int StartPullConsumer(CPullConsumer *consumer); @@ -39,20 +37,21 @@ ROCKETMQCLIENT_API int SetPullConsumerGroupID(CPullConsumer *consumer, const cha ROCKETMQCLIENT_API const char *GetPullConsumerGroupID(CPullConsumer *consumer); ROCKETMQCLIENT_API int SetPullConsumerNameServerAddress(CPullConsumer *consumer, const char *namesrv); ROCKETMQCLIENT_API int SetPullConsumerNameServerDomain(CPullConsumer *consumer, const char *domain); -ROCKETMQCLIENT_API int SetPullConsumerSessionCredentials(CPullConsumer *consumer, const char *accessKey, const char *secretKey, - const char *channel); +ROCKETMQCLIENT_API int SetPullConsumerSessionCredentials(CPullConsumer *consumer, const char *accessKey, + const char *secretKey, const char *channel); ROCKETMQCLIENT_API int SetPullConsumerLogPath(CPullConsumer *consumer, const char *logPath); ROCKETMQCLIENT_API int SetPullConsumerLogFileNumAndSize(CPullConsumer *consumer, int fileNum, long fileSize); ROCKETMQCLIENT_API int SetPullConsumerLogLevel(CPullConsumer *consumer, CLogLevel level); -ROCKETMQCLIENT_API int FetchSubscriptionMessageQueues(CPullConsumer *consumer, const char *topic, CMessageQueue **mqs, int *size); +ROCKETMQCLIENT_API int FetchSubscriptionMessageQueues(CPullConsumer *consumer, const char *topic, CMessageQueue **mqs, + int *size); ROCKETMQCLIENT_API int ReleaseSubscriptionMessageQueue(CMessageQueue *mqs); ROCKETMQCLIENT_API CPullResult -Pull(CPullConsumer *consumer, const CMessageQueue *mq, const char *subExpression, long long offset, int maxNums); + Pull(CPullConsumer *consumer, const CMessageQueue *mq, const char *subExpression, long long offset, int maxNums); ROCKETMQCLIENT_API int ReleasePullResult(CPullResult pullResult); #ifdef __cplusplus }; #endif -#endif //__C_PUSH_CONSUMER_H__ +#endif //__C_PUSH_CONSUMER_H__ diff --git a/include/CPullResult.h b/include/CPullResult.h index e22fd9ed3..f4e046415 100644 --- a/include/CPullResult.h +++ b/include/CPullResult.h @@ -29,7 +29,7 @@ typedef enum E_CPullStatus { E_NO_NEW_MSG, E_NO_MATCHED_MSG, E_OFFSET_ILLEGAL, - E_BROKER_TIMEOUT //indicate pull request timeout or received NULL response + E_BROKER_TIMEOUT // indicate pull request timeout or received NULL response } CPullStatus; typedef struct _CPullResult_ { @@ -45,4 +45,4 @@ typedef struct _CPullResult_ { #ifdef __cplusplus }; #endif -#endif //__C_PULL_RESULT_H__ +#endif //__C_PULL_RESULT_H__ diff --git a/include/CPushConsumer.h b/include/CPushConsumer.h index 49cbf9645..4fa3662a8 100644 --- a/include/CPushConsumer.h +++ b/include/CPushConsumer.h @@ -25,15 +25,15 @@ extern "C" { #endif -//typedef struct _CConsumer_ _CConsumer; +// typedef struct _CConsumer_ _CConsumer; typedef struct CPushConsumer CPushConsumer; -typedef enum E_CConsumeStatus{ +typedef enum E_CConsumeStatus { E_CONSUME_SUCCESS = 0, E_RECONSUME_LATER = 1 } CConsumeStatus; -typedef int(*MessageCallBack)(CPushConsumer *, CMessageExt *); +typedef int (*MessageCallBack)(CPushConsumer *, CMessageExt *); ROCKETMQCLIENT_API CPushConsumer *CreatePushConsumer(const char *groupId); ROCKETMQCLIENT_API int DestroyPushConsumer(CPushConsumer *consumer); @@ -51,13 +51,14 @@ ROCKETMQCLIENT_API int UnregisterMessageCallback(CPushConsumer *consumer); ROCKETMQCLIENT_API int SetPushConsumerThreadCount(CPushConsumer *consumer, int threadCount); ROCKETMQCLIENT_API int SetPushConsumerMessageBatchMaxSize(CPushConsumer *consumer, int batchSize); ROCKETMQCLIENT_API int SetPushConsumerInstanceName(CPushConsumer *consumer, const char *instanceName); -ROCKETMQCLIENT_API int SetPushConsumerSessionCredentials(CPushConsumer *consumer, const char *accessKey, const char *secretKey,const char *channel); +ROCKETMQCLIENT_API int SetPushConsumerSessionCredentials(CPushConsumer *consumer, const char *accessKey, + const char *secretKey, const char *channel); ROCKETMQCLIENT_API int SetPushConsumerLogPath(CPushConsumer *consumer, const char *logPath); ROCKETMQCLIENT_API int SetPushConsumerLogFileNumAndSize(CPushConsumer *consumer, int fileNum, long fileSize); ROCKETMQCLIENT_API int SetPushConsumerLogLevel(CPushConsumer *consumer, CLogLevel level); ROCKETMQCLIENT_API int SetPushConsumerMessageModel(CPushConsumer *consumer, CMessageModel messageModel); - + #ifdef __cplusplus }; #endif -#endif //__C_PUSH_CONSUMER_H__ +#endif //__C_PUSH_CONSUMER_H__ diff --git a/include/CSendResult.h b/include/CSendResult.h index 14100b4a7..ab668ee3b 100644 --- a/include/CSendResult.h +++ b/include/CSendResult.h @@ -23,7 +23,7 @@ extern "C" { #endif -typedef enum E_CSendStatus_{ +typedef enum E_CSendStatus_ { E_SEND_OK = 0, E_SEND_FLUSH_DISK_TIMEOUT = 1, E_SEND_FLUSH_SLAVE_TIMEOUT = 2, @@ -32,11 +32,11 @@ typedef enum E_CSendStatus_{ typedef struct _SendResult_ { CSendStatus sendStatus; - char msgId[MAX_MESSAGE_ID_LENGTH]; - long long offset; + char msgId[MAX_MESSAGE_ID_LENGTH]; + long long offset; } CSendResult; #ifdef __cplusplus }; #endif -#endif //__C_PRODUCER_H__ +#endif //__C_PRODUCER_H__ diff --git a/include/ConsumeType.h b/include/ConsumeType.h old mode 100755 new mode 100644 index f8046552a..0325db83a --- a/include/ConsumeType.h +++ b/include/ConsumeType.h @@ -14,47 +14,47 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + #ifndef __CONSUMETYPE_H__ #define __CONSUMETYPE_H__ namespace rocketmq { //& msgs); - virtual SendResult send(std::vector& msgs, const MQMessageQueue& mq); - virtual void send(MQMessage& msg, SendCallback* pSendCallback, - bool bSelectActiveBroker = false); - virtual void send(MQMessage& msg, const MQMessageQueue& mq, - SendCallback* pSendCallback); - virtual void send(MQMessage& msg, MessageQueueSelector* selector, void* arg, - SendCallback* pSendCallback); - virtual void sendOneway(MQMessage& msg, bool bSelectActiveBroker = false); - virtual void sendOneway(MQMessage& msg, const MQMessageQueue& mq); - virtual void sendOneway(MQMessage& msg, MessageQueueSelector* selector, - void* arg); - //& msgs); + virtual SendResult send(std::vector& msgs, const MQMessageQueue& mq); + virtual void send(MQMessage& msg, SendCallback* pSendCallback, bool bSelectActiveBroker = false); + virtual void send(MQMessage& msg, const MQMessageQueue& mq, SendCallback* pSendCallback); + virtual void send(MQMessage& msg, MessageQueueSelector* selector, void* arg, SendCallback* pSendCallback); + virtual void sendOneway(MQMessage& msg, bool bSelectActiveBroker = false); + virtual void sendOneway(MQMessage& msg, const MQMessageQueue& mq); + virtual void sendOneway(MQMessage& msg, MessageQueueSelector* selector, void* arg); + //& msgs); + int getRetryTimes4Async() const; + void setRetryTimes4Async(int times); - private: - int m_sendMsgTimeout; - int m_compressMsgBodyOverHowmuch; - int m_maxMessageSize; //& msgs); + +private: + int m_sendMsgTimeout; + int m_compressMsgBodyOverHowmuch; + int m_maxMessageSize; //& mqs); - virtual void doRebalance(); - virtual void persistConsumerOffset(); - virtual void persistConsumerOffsetByResetOffset(); - virtual void updateTopicSubscribeInfo(const std::string& topic, - std::vector& info); - virtual ConsumeType getConsumeType(); - virtual ConsumeFromWhere getConsumeFromWhere(); - virtual void getSubscriptions(std::vector&); - virtual void updateConsumeOffset(const MQMessageQueue& mq, int64 offset); - virtual void removeConsumeOffset(const MQMessageQueue& mq); - virtual void producePullMsgTask(PullRequest*); - virtual Rebalance* getRebalance() const; - //& mqs); + virtual void doRebalance(); + virtual void persistConsumerOffset(); + virtual void persistConsumerOffsetByResetOffset(); + virtual void updateTopicSubscribeInfo(const std::string& topic, std::vector& info); + virtual ConsumeType getConsumeType(); + virtual ConsumeFromWhere getConsumeFromWhere(); + virtual void getSubscriptions(std::vector&); + virtual void updateConsumeOffset(const MQMessageQueue& mq, int64 offset); + virtual void removeConsumeOffset(const MQMessageQueue& mq); + virtual void producePullMsgTask(PullRequest*); + virtual Rebalance* getRebalance() const; + // mqs); + virtual ConsumerRunningInfo* getConsumerRunningInfo() { return NULL; } + /** + * »ñÈ¡Ïû·Ñ½ø¶È£¬·µ»Ø-1±íʾ³ö´í + * + * @param mq + * @param fromStore + * @return + */ + int64 fetchConsumeOffset(const MQMessageQueue& mq, bool fromStore); + /** + * ¸ù¾Ýtopic»ñÈ¡MessageQueue£¬ÒÔ¾ùºâ·½Ê½ÔÚ×éÄÚ¶à¸ö³ÉÔ±Ö®¼ä·ÖÅä + * + * @param topic + * ÏûÏ¢Topic + * @return ·µ»Ø¶ÓÁм¯ºÏ + */ + void fetchMessageQueuesInBalance(const std::string& topic, std::vector mqs); - // temp persist consumer offset interface, only valid with - // RemoteBrokerOffsetStore, updateConsumeOffset should be called before. - void persistConsumerOffset4PullConsumer(const MQMessageQueue& mq); + // temp persist consumer offset interface, only valid with + // RemoteBrokerOffsetStore, updateConsumeOffset should be called before. + void persistConsumerOffset4PullConsumer(const MQMessageQueue& mq); - private: - void checkConfig(); - void copySubscription(); +private: + void checkConfig(); + void copySubscription(); - PullResult pullSyncImpl(const MQMessageQueue& mq, const std::string& subExpression, - int64 offset, int maxNums, bool block); + PullResult pullSyncImpl(const MQMessageQueue& mq, const std::string& subExpression, int64 offset, int maxNums, + bool block); - void pullAsyncImpl(const MQMessageQueue& mq, const std::string& subExpression, - int64 offset, int maxNums, bool block, - PullCallback* pPullCallback); + void pullAsyncImpl(const MQMessageQueue& mq, const std::string& subExpression, int64 offset, int maxNums, + bool block, PullCallback* pPullCallback); - void subscriptionAutomatically(const std::string& topic); + void subscriptionAutomatically(const std::string& topic); - private: - std::set m_registerTopics; +private: + std::set m_registerTopics; - MQueueListener* m_pMessageQueueListener; - OffsetStore* m_pOffsetStore; - Rebalance* m_pRebalance; - PullAPIWrapper* m_pPullAPIWrapper; + MQueueListener* m_pMessageQueueListener; + OffsetStore* m_pOffsetStore; + Rebalance* m_pRebalance; + PullAPIWrapper* m_pPullAPIWrapper; }; //& mqs); - virtual void doRebalance(); - virtual void persistConsumerOffset(); - virtual void persistConsumerOffsetByResetOffset(); - virtual void updateTopicSubscribeInfo(const std::string& topic, - std::vector& info); - virtual ConsumeType getConsumeType(); - virtual ConsumeFromWhere getConsumeFromWhere(); - void setConsumeFromWhere(ConsumeFromWhere consumeFromWhere); - virtual void getSubscriptions(std::vector&); - virtual void updateConsumeOffset(const MQMessageQueue& mq, int64 offset); - virtual void removeConsumeOffset(const MQMessageQueue& mq); - virtual PullResult pull(const MQMessageQueue& mq, const std::string& subExpression, - int64 offset, int maxNums) { - return PullResult(); - } - virtual void pull(const MQMessageQueue& mq, const std::string& subExpression, - int64 offset, int maxNums, - PullCallback* pPullCallback) {} - virtual ConsumerRunningInfo* getConsumerRunningInfo(); - // m_subTopics; - int m_consumeThreadCount; - OffsetStore* m_pOffsetStore; - Rebalance* m_pRebalance; - PullAPIWrapper* m_pPullAPIWrapper; - ConsumeMsgService* m_consumerService; - MQMessageListener* m_pMessageListener; - int m_consumeMessageBatchMaxSize; - int m_maxMsgCacheSize; - boost::asio::io_service m_async_ioService; - boost::scoped_ptr m_async_service_thread; - - typedef std::map PullMAP; - PullMAP m_PullCallback; - bool m_asyncPull; - int m_asyncPullTimeout; - int m_pullMsgThreadPoolNum; - - private: - TaskQueue* m_pullmsgQueue; - std::unique_ptr m_pullmsgThread; +public: + DefaultMQPushConsumer(const std::string& groupname); + void boost_asio_work(); + virtual ~DefaultMQPushConsumer(); + + //& mqs); + virtual void doRebalance(); + virtual void persistConsumerOffset(); + virtual void persistConsumerOffsetByResetOffset(); + virtual void updateTopicSubscribeInfo(const std::string& topic, std::vector& info); + virtual ConsumeType getConsumeType(); + virtual ConsumeFromWhere getConsumeFromWhere(); + void setConsumeFromWhere(ConsumeFromWhere consumeFromWhere); + virtual void getSubscriptions(std::vector&); + virtual void updateConsumeOffset(const MQMessageQueue& mq, int64 offset); + virtual void removeConsumeOffset(const MQMessageQueue& mq); + virtual PullResult pull(const MQMessageQueue& mq, const std::string& subExpression, int64 offset, int maxNums) { + return PullResult(); + } + virtual void pull(const MQMessageQueue& mq, const std::string& subExpression, int64 offset, int maxNums, + PullCallback* pPullCallback) {} + virtual ConsumerRunningInfo* getConsumerRunningInfo(); + // m_subTopics; + int m_consumeThreadCount; + OffsetStore* m_pOffsetStore; + Rebalance* m_pRebalance; + PullAPIWrapper* m_pPullAPIWrapper; + ConsumeMsgService* m_consumerService; + MQMessageListener* m_pMessageListener; + int m_consumeMessageBatchMaxSize; + int m_maxMsgCacheSize; + boost::asio::io_service m_async_ioService; + boost::scoped_ptr m_async_service_thread; + + typedef std::map PullMAP; + PullMAP m_PullCallback; + bool m_asyncPull; + int m_asyncPullTimeout; + int m_pullMsgThreadPoolNum; + +private: + TaskQueue* m_pullmsgQueue; + std::unique_ptr m_pullmsgThread; }; // getTopicMessageQueueInfo(const std::string& topic); - - // log configuration interface, default LOG_LEVEL is LOG_LEVEL_INFO, default - // log file num is 3, each log size is 100M - void setLogLevel(elogLevel inputLevel); - elogLevel getLogLevel(); - void setLogFileSizeAndNum(int fileNum, - long perFileSize); // perFileSize is MB unit - - /** set TcpTransport pull thread num, which dermine the num of threads to - distribute network data, - 1. its default value is CPU num, it must be setted before producer/consumer - start, minimum value is CPU num; - 2. this pullThread num must be tested on your environment to find the best - value for RT of sendMsg or delay time of consume msg before you change it; - 3. producer and consumer need different pullThread num, if set this num, - producer and consumer must set different instanceName. - 4. configuration suggestion: - 1>. minimum RT of sendMsg: - pullThreadNum = brokerNum*2 - **/ - void setTcpTransportPullThreadNum(int num); - const int getTcpTransportPullThreadNum() const; - - /** timeout of tcp connect, it is same meaning for both producer and consumer; - 1. default value is 3000ms - 2. input parameter could only be milliSecond, suggestion value is - 1000-3000ms; - **/ - void setTcpTransportConnectTimeout(uint64_t timeout); // ms - const uint64_t getTcpTransportConnectTimeout() const; - - /** timeout of tryLock tcpTransport before sendMsg/pullMsg, if timeout, - returns NULL - 1. paremeter unit is ms, default value is 3000ms, the minimun value is - 1000ms - suggestion value is 3000ms; - 2. if configured with value smaller than 1000ms, the tryLockTimeout value - will be setted to 1000ms - **/ - void setTcpTransportTryLockTimeout(uint64_t timeout); // ms - const uint64_t getTcpTransportTryLockTimeout() const; - - void setUnitName(std::string unitName); - const std::string& getUnitName(); - - void setSessionCredentials(const std::string& input_accessKey, - const std::string& input_secretKey, - const std::string& input_onsChannel); - const SessionCredentials& getSessionCredentials() const; - - protected: - virtual void start(); - virtual void shutdown(); - MQClientFactory* getFactory() const; - virtual bool isServiceStateOk(); - - protected: - std::string m_namesrvAddr; - std::string m_namesrvDomain; - std::string m_instanceName; - // getTopicMessageQueueInfo(const std::string& topic); + + // log configuration interface, default LOG_LEVEL is LOG_LEVEL_INFO, default + // log file num is 3, each log size is 100M + void setLogLevel(elogLevel inputLevel); + elogLevel getLogLevel(); + void setLogFileSizeAndNum(int fileNum, long perFileSize); // perFileSize is MB unit + + /** set TcpTransport pull thread num, which dermine the num of threads to + distribute network data, + 1. its default value is CPU num, it must be setted before producer/consumer + start, minimum value is CPU num; + 2. this pullThread num must be tested on your environment to find the best + value for RT of sendMsg or delay time of consume msg before you change it; + 3. producer and consumer need different pullThread num, if set this num, + producer and consumer must set different instanceName. + 4. configuration suggestion: + 1>. minimum RT of sendMsg: + pullThreadNum = brokerNum*2 + **/ + void setTcpTransportPullThreadNum(int num); + const int getTcpTransportPullThreadNum() const; + + /** timeout of tcp connect, it is same meaning for both producer and consumer; + 1. default value is 3000ms + 2. input parameter could only be milliSecond, suggestion value is + 1000-3000ms; + **/ + void setTcpTransportConnectTimeout(uint64_t timeout); // ms + const uint64_t getTcpTransportConnectTimeout() const; + + /** timeout of tryLock tcpTransport before sendMsg/pullMsg, if timeout, + returns NULL + 1. paremeter unit is ms, default value is 3000ms, the minimun value is + 1000ms + suggestion value is 3000ms; + 2. if configured with value smaller than 1000ms, the tryLockTimeout value + will be setted to 1000ms + **/ + void setTcpTransportTryLockTimeout(uint64_t timeout); // ms + const uint64_t getTcpTransportTryLockTimeout() const; + + void setUnitName(std::string unitName); + const std::string& getUnitName(); + + void setSessionCredentials(const std::string& input_accessKey, const std::string& input_secretKey, + const std::string& input_onsChannel); + const SessionCredentials& getSessionCredentials() const; + +protected: + virtual void start(); + virtual void shutdown(); + MQClientFactory* getFactory() const; + virtual bool isServiceStateOk(); + +protected: + std::string m_namesrvAddr; + std::string m_namesrvDomain; + std::string m_instanceName; + // line:" << line; m_msg = ss.str(); - } catch (...) { + } + catch (...) { } } @@ -45,7 +46,8 @@ class ROCKETMQCLIENT_API MQException : public std::exception { std::stringstream ss; ss << "msg: " << msg << ",error:" << error << ",in file <" << file << "> line:" << line; m_msg = ss.str(); - } catch (...) { + } + catch (...) { } } @@ -56,7 +58,7 @@ class ROCKETMQCLIENT_API MQException : public std::exception { int GetLine() { return m_line; } const char *GetFile() { return m_file.c_str(); } - protected: +protected: int m_error; int m_line; std::string m_msg; @@ -71,7 +73,7 @@ inline std::ostream &operator<<(std::ostream &os, const MQException &e) { #define DEFINE_MQCLIENTEXCEPTION(name) \ class ROCKETMQCLIENT_API name : public MQException { \ - public: \ + public: \ name(const std::string &msg, int error, const char *file, int line) throw() \ : MQException(msg, error, file, #name, line) {} \ virtual const char *GetType() const throw() { return m_type.c_str(); } \ diff --git a/include/MQConsumer.h b/include/MQConsumer.h old mode 100755 new mode 100644 index 89763a854..9e296dd70 --- a/include/MQConsumer.h +++ b/include/MQConsumer.h @@ -30,39 +30,32 @@ class Rebalance; class ConsumerRunningInfo; //& mqs) = 0; - virtual void doRebalance() = 0; - virtual void persistConsumerOffset() = 0; - virtual void persistConsumerOffsetByResetOffset() = 0; - virtual void updateTopicSubscribeInfo(const std::string& topic, - std::vector& info) = 0; - virtual void updateConsumeOffset(const MQMessageQueue& mq, - int64 offset) = 0; - virtual void removeConsumeOffset(const MQMessageQueue& mq) = 0; - virtual ConsumeType getConsumeType() = 0; - virtual ConsumeFromWhere getConsumeFromWhere() = 0; - virtual void getSubscriptions(std::vector&) = 0; - virtual void producePullMsgTask(PullRequest*) = 0; - virtual Rebalance* getRebalance() const = 0; - virtual PullResult pull(const MQMessageQueue& mq, const std::string& subExpression, - int64 offset, int maxNums) = 0; - virtual void pull(const MQMessageQueue& mq, const std::string& subExpression, - int64 offset, int maxNums, - PullCallback* pPullCallback) = 0; - virtual ConsumerRunningInfo* getConsumerRunningInfo() = 0; +public: + virtual ~MQConsumer() {} + virtual void sendMessageBack(MQMessageExt& msg, int delayLevel) = 0; + virtual void fetchSubscribeMessageQueues(const std::string& topic, std::vector& mqs) = 0; + virtual void doRebalance() = 0; + virtual void persistConsumerOffset() = 0; + virtual void persistConsumerOffsetByResetOffset() = 0; + virtual void updateTopicSubscribeInfo(const std::string& topic, std::vector& info) = 0; + virtual void updateConsumeOffset(const MQMessageQueue& mq, int64 offset) = 0; + virtual void removeConsumeOffset(const MQMessageQueue& mq) = 0; + virtual ConsumeType getConsumeType() = 0; + virtual ConsumeFromWhere getConsumeFromWhere() = 0; + virtual void getSubscriptions(std::vector&) = 0; + virtual void producePullMsgTask(PullRequest*) = 0; + virtual Rebalance* getRebalance() const = 0; + virtual PullResult pull(const MQMessageQueue& mq, const std::string& subExpression, int64 offset, int maxNums) = 0; + virtual void pull(const MQMessageQueue& mq, const std::string& subExpression, int64 offset, int maxNums, + PullCallback* pPullCallback) = 0; + virtual ConsumerRunningInfo* getConsumerRunningInfo() = 0; - public: - MessageModel getMessageModel() const { return m_messageModel; } - void setMessageModel(MessageModel messageModel) { - m_messageModel = messageModel; - } +public: + MessageModel getMessageModel() const { return m_messageModel; } + void setMessageModel(MessageModel messageModel) { m_messageModel = messageModel; } - protected: - MessageModel m_messageModel; +protected: + MessageModel m_messageModel; }; //& keys); - - int getDelayTimeLevel() const; - void setDelayTimeLevel(int level); - - bool isWaitStoreMsgOK() const; - void setWaitStoreMsgOK(bool waitStoreMsgOK); - - int getFlag() const; - void setFlag(int flag); - - int getSysFlag() const; - void setSysFlag(int sysFlag); - - const std::string &getBody() const; - - void setBody(const char* body, int len); - void setBody(const std::string& body); - - std::map getProperties() const; - void setProperties(std::map& properties); - - const std::string toString() const { - std::stringstream ss; - std::string tags = getTags(); - ss << "Message [topic=" << m_topic << ", flag=" << m_flag - << ", tag=" << tags << "]"; - return ss.str(); - } - - protected: - friend class MQDecoder; - void setPropertyInternal(const std::string& name, const std::string& value); - void setPropertiesInternal(std::map& properties); - - void Init(const std::string& topic, const std::string& tags, const std::string& keys, - const int flag, const std::string& body, bool waitStoreMsgOK); - - public: - static const std::string PROPERTY_KEYS; - static const std::string PROPERTY_TAGS; - static const std::string PROPERTY_WAIT_STORE_MSG_OK; - static const std::string PROPERTY_DELAY_TIME_LEVEL; - static const std::string PROPERTY_RETRY_TOPIC; - static const std::string PROPERTY_REAL_TOPIC; - static const std::string PROPERTY_REAL_QUEUE_ID; - static const std::string PROPERTY_TRANSACTION_PREPARED; - static const std::string PROPERTY_PRODUCER_GROUP; - static const std::string PROPERTY_MIN_OFFSET; - static const std::string PROPERTY_MAX_OFFSET; - - static const std::string PROPERTY_BUYER_ID; - static const std::string PROPERTY_ORIGIN_MESSAGE_ID; - static const std::string PROPERTY_TRANSFER_FLAG; - static const std::string PROPERTY_CORRECTION_FLAG; - static const std::string PROPERTY_MQ2_FLAG; - static const std::string PROPERTY_RECONSUME_TIME; - static const std::string PROPERTY_MSG_REGION; - static const std::string PROPERTY_TRACE_SWITCH; - static const std::string PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX; - static const std::string PROPERTY_MAX_RECONSUME_TIMES; - static const std::string PROPERTY_CONSUME_START_TIMESTAMP; - static const std::string PROPERTY_TRANSACTION_PREPARED_QUEUE_OFFSET; - static const std::string PROPERTY_TRANSACTION_CHECK_TIMES; - static const std::string PROPERTY_CHECK_IMMUNITY_TIME_IN_SECONDS; - - static const std::string KEY_SEPARATOR; - - protected: - int m_sysFlag; - - private: - std::string m_topic; - int m_flag; - std::string m_body; - std::map m_properties; +public: + MQMessage(); + MQMessage(const std::string& topic, const std::string& body); + MQMessage(const std::string& topic, const std::string& tags, const std::string& body); + MQMessage(const std::string& topic, const std::string& tags, const std::string& keys, const std::string& body); + MQMessage(const std::string& topic, const std::string& tags, const std::string& keys, const int flag, + const std::string& body, bool waitStoreMsgOK); + + virtual ~MQMessage(); + MQMessage(const MQMessage& other); + MQMessage& operator=(const MQMessage& other); + + void setProperty(const std::string& name, const std::string& value); + const std::string& getProperty(const std::string& name) const; + + const std::string& getTopic() const; + void setTopic(const std::string& topic); + void setTopic(const char* body, int len); + + const std::string& getTags() const; + void setTags(const std::string& tags); + + const std::string& getKeys() const; + void setKeys(const std::string& keys); + void setKeys(const std::vector& keys); + + int getDelayTimeLevel() const; + void setDelayTimeLevel(int level); + + bool isWaitStoreMsgOK() const; + void setWaitStoreMsgOK(bool waitStoreMsgOK); + + int getFlag() const; + void setFlag(int flag); + + int getSysFlag() const; + void setSysFlag(int sysFlag); + + const std::string& getBody() const; + + void setBody(const char* body, int len); + void setBody(const std::string& body); + + std::map getProperties() const; + void setProperties(std::map& properties); + + const std::string toString() const { + std::stringstream ss; + std::string tags = getTags(); + ss << "Message [topic=" << m_topic << ", flag=" << m_flag << ", tag=" << tags << "]"; + return ss.str(); + } + +protected: + friend class MQDecoder; + void setPropertyInternal(const std::string& name, const std::string& value); + void setPropertiesInternal(std::map& properties); + + void Init(const std::string& topic, const std::string& tags, const std::string& keys, const int flag, + const std::string& body, bool waitStoreMsgOK); + +public: + static const std::string PROPERTY_KEYS; + static const std::string PROPERTY_TAGS; + static const std::string PROPERTY_WAIT_STORE_MSG_OK; + static const std::string PROPERTY_DELAY_TIME_LEVEL; + static const std::string PROPERTY_RETRY_TOPIC; + static const std::string PROPERTY_REAL_TOPIC; + static const std::string PROPERTY_REAL_QUEUE_ID; + static const std::string PROPERTY_TRANSACTION_PREPARED; + static const std::string PROPERTY_PRODUCER_GROUP; + static const std::string PROPERTY_MIN_OFFSET; + static const std::string PROPERTY_MAX_OFFSET; + + static const std::string PROPERTY_BUYER_ID; + static const std::string PROPERTY_ORIGIN_MESSAGE_ID; + static const std::string PROPERTY_TRANSFER_FLAG; + static const std::string PROPERTY_CORRECTION_FLAG; + static const std::string PROPERTY_MQ2_FLAG; + static const std::string PROPERTY_RECONSUME_TIME; + static const std::string PROPERTY_MSG_REGION; + static const std::string PROPERTY_TRACE_SWITCH; + static const std::string PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX; + static const std::string PROPERTY_MAX_RECONSUME_TIMES; + static const std::string PROPERTY_CONSUME_START_TIMESTAMP; + static const std::string PROPERTY_TRANSACTION_PREPARED_QUEUE_OFFSET; + static const std::string PROPERTY_TRANSACTION_CHECK_TIMES; + static const std::string PROPERTY_CHECK_IMMUNITY_TIME_IN_SECONDS; + + static const std::string KEY_SEPARATOR; + +protected: + int m_sysFlag; + +private: + std::string m_topic; + int m_flag; + std::string m_body; + std::map m_properties; }; //& msgs) = 0; - virtual MessageListenerType getMessageListenerType() { - return messageListenerDefaultly; - } +public: + virtual ~MQMessageListener() {} + virtual ConsumeStatus consumeMessage(const std::vector& msgs) = 0; + virtual MessageListenerType getMessageListenerType() { return messageListenerDefaultly; } }; class ROCKETMQCLIENT_API MessageListenerOrderly : public MQMessageListener { - public: - virtual ~MessageListenerOrderly() {} - virtual ConsumeStatus consumeMessage(const std::vector& msgs) = 0; - virtual MessageListenerType getMessageListenerType() { - return messageListenerOrderly; - } +public: + virtual ~MessageListenerOrderly() {} + virtual ConsumeStatus consumeMessage(const std::vector& msgs) = 0; + virtual MessageListenerType getMessageListenerType() { return messageListenerOrderly; } }; -class ROCKETMQCLIENT_API MessageListenerConcurrently - : public MQMessageListener { - public: - virtual ~MessageListenerConcurrently() {} - virtual ConsumeStatus consumeMessage(const std::vector& msgs) = 0; - virtual MessageListenerType getMessageListenerType() { - return messageListenerConcurrently; - } +class ROCKETMQCLIENT_API MessageListenerConcurrently : public MQMessageListener { +public: + virtual ~MessageListenerConcurrently() {} + virtual ConsumeStatus consumeMessage(const std::vector& msgs) = 0; + virtual MessageListenerType getMessageListenerType() { return messageListenerConcurrently; } }; //& msgs) = 0; - virtual SendResult send(std::vector& msgs, const MQMessageQueue& mq) = 0; - virtual void sendOneway(MQMessage& msg, bool bSelectActiveBroker = false) = 0; - virtual void sendOneway(MQMessage& msg, const MQMessageQueue& mq) = 0; - virtual void sendOneway(MQMessage& msg, MessageQueueSelector* selector, - void* arg) = 0; +public: + MQProducer() {} + virtual ~MQProducer() {} + // if setted bActiveBroker, will search brokers with best service state + // firstly, then search brokers that had been sent failed by last time; + virtual SendResult send(MQMessage& msg, bool bSelectActiveBroker = false) = 0; + virtual SendResult send(MQMessage& msg, const MQMessageQueue& mq) = 0; + // strict order msg, if send failed on seleted MessageQueue, throw exception + // to up layer + virtual SendResult send(MQMessage& msg, MessageQueueSelector* selector, void* arg) = 0; + // non-strict order msg, if send failed on seleted MessageQueue, will auto + // retry others Broker queues with autoRetryTimes; + // if setted bActiveBroker, if send failed on seleted MessageQueue, , and then + // search brokers with best service state, lastly will search brokers that had + // been sent failed by last time; + virtual SendResult send(MQMessage& msg, MessageQueueSelector* selector, void* arg, int autoRetryTimes, + bool bActiveBroker = false) = 0; + virtual void send(MQMessage& msg, SendCallback* sendCallback, bool bSelectActiveBroker = false) = 0; + virtual void send(MQMessage& msg, const MQMessageQueue& mq, SendCallback* sendCallback) = 0; + virtual void send(MQMessage& msg, MessageQueueSelector* selector, void* arg, SendCallback* sendCallback) = 0; + virtual SendResult send(std::vector& msgs) = 0; + virtual SendResult send(std::vector& msgs, const MQMessageQueue& mq) = 0; + virtual void sendOneway(MQMessage& msg, bool bSelectActiveBroker = false) = 0; + virtual void sendOneway(MQMessage& msg, const MQMessageQueue& mq) = 0; + virtual void sendOneway(MQMessage& msg, MessageQueueSelector* selector, void* arg) = 0; }; //& mqs, - const MQMessage& msg, void* arg) = 0; +public: + virtual ~MessageQueueSelector() {} + virtual MQMessageQueue select(const std::vector& mqs, const MQMessage& msg, void* arg) = 0; }; //& mqAll, - std::vector& mqDivided) = 0; +public: + virtual ~MQueueListener() {} + virtual void messageQueueChanged(const std::string& topic, std::vector& mqAll, + std::vector& mqDivided) = 0; }; //& src); + PullResult(PullStatus pullStatus, int64 nextBeginOffset, int64 minOffset, int64 maxOffset, + const std::vector& src); - virtual ~PullResult(); + virtual ~PullResult(); - std::string toString() { - std::stringstream ss; - ss << "PullResult [ pullStatus=" << EnumStrings[pullStatus] - << ", nextBeginOffset=" << nextBeginOffset << ", minOffset=" << minOffset - << ", maxOffset=" << maxOffset - << ", msgFoundList=" << msgFoundList.size() << " ]"; - return ss.str(); - } + std::string toString() { + std::stringstream ss; + ss << "PullResult [ pullStatus=" << EnumStrings[pullStatus] << ", nextBeginOffset=" << nextBeginOffset + << ", minOffset=" << minOffset << ", maxOffset=" << maxOffset << ", msgFoundList=" << msgFoundList.size() + << " ]"; + return ss.str(); + } - public: - PullStatus pullStatus; - int64 nextBeginOffset; - int64 minOffset; - int64 maxOffset; - std::vector msgFoundList; +public: + PullStatus pullStatus; + int64 nextBeginOffset; + int64 minOffset; + int64 maxOffset; + std::vector msgFoundList; }; //& messageList) { - m_indexLastUpdateTimestamp = indexLastUpdateTimestamp; - m_messageList = messageList; - } +public: + QueryResult(uint64 indexLastUpdateTimestamp, const std::vector& messageList) { + m_indexLastUpdateTimestamp = indexLastUpdateTimestamp; + m_messageList = messageList; + } - uint64 getIndexLastUpdateTimestamp() { return m_indexLastUpdateTimestamp; } + uint64 getIndexLastUpdateTimestamp() { return m_indexLastUpdateTimestamp; } - std::vector& getMessageList() { return m_messageList; } + std::vector& getMessageList() { return m_messageList; } - private: - uint64 m_indexLastUpdateTimestamp; - std::vector m_messageList; +private: + uint64 m_indexLastUpdateTimestamp; + std::vector m_messageList; }; // +#include /* Get bool. */ -# include - +#include #ifdef __cplusplus -namespace rocketmqSignature{ +namespace rocketmqSignature { #endif - /* This uses that the expression (n+(k-1))/k means the smallest - integer >= n/k, i.e., the ceiling of n/k. */ -# define BASE64_LENGTH(inlen) ((((inlen) + 2) / 3) * 4) +/* This uses that the expression (n+(k-1))/k means the smallest + integer >= n/k, i.e., the ceiling of n/k. */ +#define BASE64_LENGTH(inlen) ((((inlen) + 2) / 3) * 4) extern bool isbase64(char ch); -extern void base64_encode(const char *in, size_t inlen, - char *out, size_t outlen); +extern void base64_encode(const char *in, size_t inlen, char *out, size_t outlen); extern size_t base64_encode_alloc(const char *in, size_t inlen, char **out); -extern bool base64_decode(const char *in, size_t inlen, - char *out, size_t *outlen); +extern bool base64_decode(const char *in, size_t inlen, char *out, size_t *outlen); -extern bool base64_decode_alloc(const char *in, size_t inlen, - char **out, size_t *outlen); +extern bool base64_decode_alloc(const char *in, size_t inlen, char **out, size_t *outlen); #ifdef __cplusplus } diff --git a/libs/signature/include/hmac.h b/libs/signature/include/hmac.h old mode 100755 new mode 100644 index 2b7515fef..f97ba2f8c --- a/libs/signature/include/hmac.h +++ b/libs/signature/include/hmac.h @@ -25,15 +25,15 @@ extern "C" { #include #ifndef SHA1_DIGEST_LEN -#define SHA1_DIGEST_LEN 20 +#define SHA1_DIGEST_LEN 20 #endif #ifndef SHA256_DIGEST_LEN -#define SHA256_DIGEST_LEN 32 +#define SHA256_DIGEST_LEN 32 #endif #ifndef SHA512_DIGEST_LEN -#define SHA512_DIGEST_LEN 64 +#define SHA512_DIGEST_LEN 64 #endif /* @@ -51,7 +51,7 @@ extern "C" { */ #ifdef __cplusplus -namespace rocketmqSignature{ +namespace rocketmqSignature { #endif @@ -68,4 +68,3 @@ extern int hmac_sha512(const void *key, size_t key_len, const void *data, size_t #endif #endif - diff --git a/libs/signature/include/param_list.h b/libs/signature/include/param_list.h old mode 100755 new mode 100644 index 86222043e..31c74df5b --- a/libs/signature/include/param_list.h +++ b/libs/signature/include/param_list.h @@ -23,26 +23,25 @@ extern "C" { #endif #ifdef __cplusplus -namespace rocketmqSignature{ +namespace rocketmqSignature { #endif - typedef struct _spas_param_node { - char *name; - char *value; - struct _spas_param_node *pnext; + char *name; + char *value; + struct _spas_param_node *pnext; } SPAS_PARAM_NODE; typedef struct _spas_param_list { - SPAS_PARAM_NODE *phead; - unsigned int length; /* count of nodes */ - unsigned int size; /* total size of string presentation */ + SPAS_PARAM_NODE *phead; + unsigned int length; /* count of nodes */ + unsigned int size; /* total size of string presentation */ } SPAS_PARAM_LIST; -extern SPAS_PARAM_LIST * create_param_list(void); +extern SPAS_PARAM_LIST *create_param_list(void); extern int add_param_to_list(SPAS_PARAM_LIST *list, const char *name, const char *value); extern void free_param_list(SPAS_PARAM_LIST *list); -extern char * param_list_to_str(const SPAS_PARAM_LIST *list); +extern char *param_list_to_str(const SPAS_PARAM_LIST *list); #ifdef __cplusplus } @@ -53,4 +52,3 @@ extern char * param_list_to_str(const SPAS_PARAM_LIST *list); #endif #endif - diff --git a/libs/signature/include/sha1.h b/libs/signature/include/sha1.h old mode 100755 new mode 100644 index 997610d45..9740ec27b --- a/libs/signature/include/sha1.h +++ b/libs/signature/include/sha1.h @@ -16,10 +16,10 @@ */ #ifndef SHA1_H -# define SHA1_H 1 +#define SHA1_H 1 -# include -# include +#include +#include #ifdef __cplusplus namespace rocketmqSignature { @@ -28,61 +28,54 @@ namespace rocketmqSignature { #define SHA1_DIGEST_SIZE 20 /* Structure to save state of computation between the single steps. */ -struct sha1_ctx -{ - uint32_t A; - uint32_t B; - uint32_t C; - uint32_t D; - uint32_t E; - - uint32_t total[2]; - uint32_t buflen; - uint32_t buffer[32]; +struct sha1_ctx { + uint32_t A; + uint32_t B; + uint32_t C; + uint32_t D; + uint32_t E; + + uint32_t total[2]; + uint32_t buflen; + uint32_t buffer[32]; }; - /* Initialize structure containing state of computation. */ -extern void sha1_init_ctx (struct sha1_ctx *ctx); +extern void sha1_init_ctx(struct sha1_ctx *ctx); /* Starting with the result of former calls of this function (or the initialization function update the context for the next LEN bytes starting at BUFFER. It is necessary that LEN is a multiple of 64!!! */ -extern void sha1_process_block (const void *buffer, size_t len, - struct sha1_ctx *ctx); +extern void sha1_process_block(const void *buffer, size_t len, struct sha1_ctx *ctx); /* Starting with the result of former calls of this function (or the initialization function update the context for the next LEN bytes starting at BUFFER. It is NOT required that LEN is a multiple of 64. */ -extern void sha1_process_bytes (const void *buffer, size_t len, - struct sha1_ctx *ctx); +extern void sha1_process_bytes(const void *buffer, size_t len, struct sha1_ctx *ctx); /* Process the remaining bytes in the buffer and put result from CTX in first 20 bytes following RESBUF. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */ -extern void *sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf); - +extern void *sha1_finish_ctx(struct sha1_ctx *ctx, void *resbuf); /* Put result from CTX in first 20 bytes following RESBUF. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */ -extern void *sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf); - +extern void *sha1_read_ctx(const struct sha1_ctx *ctx, void *resbuf); /* Compute SHA1 message digest for bytes read from STREAM. The resulting message digest number will be written into the 20 bytes beginning at RESBLOCK. */ -extern int sha1_stream (FILE *stream, void *resblock); - +extern int sha1_stream(FILE *stream, void *resblock); /* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */ -extern void *sha1_buffer (const char *buffer, size_t len, void *resblock); +extern void *sha1_buffer(const char *buffer, size_t len, void *resblock); #ifdef __cplusplus } diff --git a/libs/signature/include/sha256.h b/libs/signature/include/sha256.h old mode 100755 new mode 100644 index 2dfb880c1..548683421 --- a/libs/signature/include/sha256.h +++ b/libs/signature/include/sha256.h @@ -16,76 +16,75 @@ */ #ifndef SHA256_H -# define SHA256_H 1 +#define SHA256_H 1 -# include -# include +#include +#include #ifdef __cplusplus -namespace rocketmqSignature{ +namespace rocketmqSignature { #endif /* Structure to save state of computation between the single steps. */ -struct sha256_ctx -{ - uint32_t state[8]; +struct sha256_ctx { + uint32_t state[8]; - uint32_t total[2]; - size_t buflen; - uint32_t buffer[32]; + uint32_t total[2]; + size_t buflen; + uint32_t buffer[32]; }; -enum { SHA224_DIGEST_SIZE = 28 }; -enum { SHA256_DIGEST_SIZE = 32 }; +enum { + SHA224_DIGEST_SIZE = 28 +}; +enum { + SHA256_DIGEST_SIZE = 32 +}; /* Initialize structure containing state of computation. */ -extern void sha256_init_ctx (struct sha256_ctx *ctx); -extern void sha224_init_ctx (struct sha256_ctx *ctx); +extern void sha256_init_ctx(struct sha256_ctx *ctx); +extern void sha224_init_ctx(struct sha256_ctx *ctx); /* Starting with the result of former calls of this function (or the initialization function update the context for the next LEN bytes starting at BUFFER. It is necessary that LEN is a multiple of 64!!! */ -extern void sha256_process_block (const void *buffer, size_t len, - struct sha256_ctx *ctx); +extern void sha256_process_block(const void *buffer, size_t len, struct sha256_ctx *ctx); /* Starting with the result of former calls of this function (or the initialization function update the context for the next LEN bytes starting at BUFFER. It is NOT required that LEN is a multiple of 64. */ -extern void sha256_process_bytes (const void *buffer, size_t len, - struct sha256_ctx *ctx); +extern void sha256_process_bytes(const void *buffer, size_t len, struct sha256_ctx *ctx); /* Process the remaining bytes in the buffer and put result from CTX in first 32 (28) bytes following RESBUF. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */ -extern void *sha256_finish_ctx (struct sha256_ctx *ctx, void *resbuf); -extern void *sha224_finish_ctx (struct sha256_ctx *ctx, void *resbuf); - +extern void *sha256_finish_ctx(struct sha256_ctx *ctx, void *resbuf); +extern void *sha224_finish_ctx(struct sha256_ctx *ctx, void *resbuf); /* Put result from CTX in first 32 (28) bytes following RESBUF. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */ -extern void *sha256_read_ctx (const struct sha256_ctx *ctx, void *resbuf); -extern void *sha224_read_ctx (const struct sha256_ctx *ctx, void *resbuf); - +extern void *sha256_read_ctx(const struct sha256_ctx *ctx, void *resbuf); +extern void *sha224_read_ctx(const struct sha256_ctx *ctx, void *resbuf); /* Compute SHA256 (SHA224) message digest for bytes read from STREAM. The resulting message digest number will be written into the 32 (28) bytes beginning at RESBLOCK. */ -extern int sha256_stream (FILE *stream, void *resblock); -extern int sha224_stream (FILE *stream, void *resblock); +extern int sha256_stream(FILE *stream, void *resblock); +extern int sha224_stream(FILE *stream, void *resblock); /* Compute SHA256 (SHA224) message digest for LEN bytes beginning at BUFFER. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */ -extern void *sha256_buffer (const char *buffer, size_t len, void *resblock); -extern void *sha224_buffer (const char *buffer, size_t len, void *resblock); +extern void *sha256_buffer(const char *buffer, size_t len, void *resblock); +extern void *sha224_buffer(const char *buffer, size_t len, void *resblock); #ifdef __cplusplus } -#endif +#endif #endif diff --git a/libs/signature/include/sha512.h b/libs/signature/include/sha512.h old mode 100755 new mode 100644 index 43d6bdc60..7aeac709e --- a/libs/signature/include/sha512.h +++ b/libs/signature/include/sha512.h @@ -16,54 +16,54 @@ */ #ifndef SHA512_H -# define SHA512_H 1 +#define SHA512_H 1 -# include +#include -# include "u64.h" +#include "u64.h" #ifdef __cplusplus -namespace rocketmqSignature{ +namespace rocketmqSignature { #endif /* Structure to save state of computation between the single steps. */ -struct sha512_ctx -{ - u64 state[8]; +struct sha512_ctx { + u64 state[8]; - u64 total[2]; - size_t buflen; - u64 buffer[32]; + u64 total[2]; + size_t buflen; + u64 buffer[32]; }; -enum { SHA384_DIGEST_SIZE = 48 }; -enum { SHA512_DIGEST_SIZE = 64 }; +enum { + SHA384_DIGEST_SIZE = 48 +}; +enum { + SHA512_DIGEST_SIZE = 64 +}; /* Initialize structure containing state of computation. */ -extern void sha512_init_ctx (struct sha512_ctx *ctx); -extern void sha384_init_ctx (struct sha512_ctx *ctx); +extern void sha512_init_ctx(struct sha512_ctx *ctx); +extern void sha384_init_ctx(struct sha512_ctx *ctx); /* Starting with the result of former calls of this function (or the initialization function update the context for the next LEN bytes starting at BUFFER. It is necessary that LEN is a multiple of 128!!! */ -extern void sha512_process_block (const void *buffer, size_t len, - struct sha512_ctx *ctx); +extern void sha512_process_block(const void *buffer, size_t len, struct sha512_ctx *ctx); /* Starting with the result of former calls of this function (or the initialization function update the context for the next LEN bytes starting at BUFFER. It is NOT required that LEN is a multiple of 128. */ -extern void sha512_process_bytes (const void *buffer, size_t len, - struct sha512_ctx *ctx); +extern void sha512_process_bytes(const void *buffer, size_t len, struct sha512_ctx *ctx); /* Process the remaining bytes in the buffer and put result from CTX in first 64 (48) bytes following RESBUF. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */ -extern void *sha512_finish_ctx (struct sha512_ctx *ctx, void *resbuf); -extern void *sha384_finish_ctx (struct sha512_ctx *ctx, void *resbuf); - +extern void *sha512_finish_ctx(struct sha512_ctx *ctx, void *resbuf); +extern void *sha384_finish_ctx(struct sha512_ctx *ctx, void *resbuf); /* Put result from CTX in first 64 (48) bytes following RESBUF. The result is always in little endian byte order, so that a byte-wise output yields @@ -71,22 +71,21 @@ extern void *sha384_finish_ctx (struct sha512_ctx *ctx, void *resbuf); IMPORTANT: On some systems it is required that RESBUF is correctly aligned for a 32 bits value. */ -extern void *sha512_read_ctx (const struct sha512_ctx *ctx, void *resbuf); -extern void *sha384_read_ctx (const struct sha512_ctx *ctx, void *resbuf); - +extern void *sha512_read_ctx(const struct sha512_ctx *ctx, void *resbuf); +extern void *sha384_read_ctx(const struct sha512_ctx *ctx, void *resbuf); /* Compute SHA512 (SHA384) message digest for bytes read from STREAM. The resulting message digest number will be written into the 64 (48) bytes beginning at RESBLOCK. */ -extern int sha512_stream (FILE *stream, void *resblock); -extern int sha384_stream (FILE *stream, void *resblock); +extern int sha512_stream(FILE *stream, void *resblock); +extern int sha384_stream(FILE *stream, void *resblock); /* Compute SHA512 (SHA384) message digest for LEN bytes beginning at BUFFER. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */ -extern void *sha512_buffer (const char *buffer, size_t len, void *resblock); -extern void *sha384_buffer (const char *buffer, size_t len, void *resblock); +extern void *sha512_buffer(const char *buffer, size_t len, void *resblock); +extern void *sha384_buffer(const char *buffer, size_t len, void *resblock); #ifdef __cplusplus } diff --git a/libs/signature/include/spas_client.h b/libs/signature/include/spas_client.h old mode 100755 new mode 100644 index 9eb51439a..f3802222a --- a/libs/signature/include/spas_client.h +++ b/libs/signature/include/spas_client.h @@ -28,43 +28,43 @@ extern "C" { namespace rocketmqSignature { #endif -#define SPAS_MAX_KEY_LEN 128 /* max access_key/secret_key length */ -#define SPAS_MAX_PATH 256 /* max credential file path length */ -#define SPAS_ACCESS_KEY_TAG "accessKey" /* access_key tag in credential file \ - */ -#define SPAS_SECRET_KEY_TAG "secretKey" /* secret_key tag in credential file \ - */ -#define SPAS_CREDENTIAL_ENV \ - "SPAS_CREDENTIAL" /* credential file environment variable */ +#define SPAS_MAX_KEY_LEN 128 /* max access_key/secret_key length */ +#define SPAS_MAX_PATH 256 /* max credential file path length */ +#define SPAS_ACCESS_KEY_TAG \ + "accessKey" /* access_key tag in credential file \ + */ +#define SPAS_SECRET_KEY_TAG \ + "secretKey" /* secret_key tag in credential file \ + */ +#define SPAS_CREDENTIAL_ENV "SPAS_CREDENTIAL" /* credential file environment variable */ typedef enum { - SIGN_HMACSHA1 = 0, /* HmacSHA1 */ - SIGN_HMACSHA256 = 1, /* HmacSHA256 */ + SIGN_HMACSHA1 = 0, /* HmacSHA1 */ + SIGN_HMACSHA256 = 1, /* HmacSHA256 */ } SPAS_SIGN_ALGORITHM; typedef enum { - NO_UPDATE = 0, /* do not update credential */ - UPDATE_BY_ALARM = 1, /* update credential by SIGALRM */ + NO_UPDATE = 0, /* do not update credential */ + UPDATE_BY_ALARM = 1, /* update credential by SIGALRM */ #ifdef SPAS_MT - UPDATE_BY_THREAD = 2, /* update credential by standalone thread */ + UPDATE_BY_THREAD = 2, /* update credential by standalone thread */ #endif } CREDENTIAL_UPDATE_MODE; typedef enum { - SPAS_NO_ERROR = 0, /* success */ - ERROR_INVALID_PARAM = -1, /* invalid parameter */ - ERROR_NO_CREDENTIAL = -2, /* credential file not specified */ - ERROR_FILE_OPEN = -3, /* file open failed */ - ERROR_MEM_ALLOC = -4, /* memory allocation failed */ - ERROR_MISSING_KEY = -5, /* missing access_key/secret_key */ - ERROR_KEY_LENGTH = -6, /* key length exceed limit */ - ERROR_UPDATE_CREDENTIAL = -7 /* update credential file failed */ - + SPAS_NO_ERROR = 0, /* success */ + ERROR_INVALID_PARAM = -1, /* invalid parameter */ + ERROR_NO_CREDENTIAL = -2, /* credential file not specified */ + ERROR_FILE_OPEN = -3, /* file open failed */ + ERROR_MEM_ALLOC = -4, /* memory allocation failed */ + ERROR_MISSING_KEY = -5, /* missing access_key/secret_key */ + ERROR_KEY_LENGTH = -6, /* key length exceed limit */ + ERROR_UPDATE_CREDENTIAL = -7 /* update credential file failed */ } SPAS_ERROR_CODE; typedef struct _spas_credential { - char access_key[SPAS_MAX_KEY_LEN]; - char secret_key[SPAS_MAX_KEY_LEN]; + char access_key[SPAS_MAX_KEY_LEN]; + char secret_key[SPAS_MAX_KEY_LEN]; } SPAS_CREDENTIAL; extern int spas_load_credential(char *path, CREDENTIAL_UPDATE_MODE mode); @@ -85,11 +85,9 @@ extern char *spas_get_thread_secret_key(void); #endif extern char *spas_get_signature(const SPAS_PARAM_LIST *list, const char *key); -extern char *spas_get_signature2(const SPAS_PARAM_LIST *list, const char *key, - SPAS_SIGN_ALGORITHM algorithm); +extern char *spas_get_signature2(const SPAS_PARAM_LIST *list, const char *key, SPAS_SIGN_ALGORITHM algorithm); extern char *spas_sign(const char *data, size_t size, const char *key); -extern char *spas_sign2(const char *data, size_t size, const char *key, - SPAS_SIGN_ALGORITHM algorithm); +extern char *spas_sign2(const char *data, size_t size, const char *key, SPAS_SIGN_ALGORITHM algorithm); extern void spas_mem_free(char *pSignature); extern char *spas_get_version(void); diff --git a/libs/signature/include/u64.h b/libs/signature/include/u64.h index 612b04400..7ab89a681 100644 --- a/libs/signature/include/u64.h +++ b/libs/signature/include/u64.h @@ -19,139 +19,119 @@ #include /* Return X rotated left by N bits, where 0 < N < 64. */ -#define u64rol(x, n) u64or (u64shl (x, n), u64shr (x, 64 - n)) +#define u64rol(x, n) u64or(u64shl(x, n), u64shr(x, 64 - n)) #ifdef UINT64_MAX /* Native implementations are trivial. See below for comments on what these operations do. */ typedef uint64_t u64; -# define u64hilo(hi, lo) ((u64) (((u64) (hi) << 32) + (lo))) -# define u64init(hi, lo) u64hilo (hi, lo) -# define u64lo(x) ((u64) (x)) -# define u64lt(x, y) ((x) < (y)) -# define u64and(x, y) ((x) & (y)) -# define u64or(x, y) ((x) | (y)) -# define u64xor(x, y) ((x) ^ (y)) -# define u64plus(x, y) ((x) + (y)) -# define u64shl(x, n) ((x) << (n)) -# define u64shr(x, n) ((x) >> (n)) +#define u64hilo(hi, lo) ((u64)(((u64)(hi) << 32) + (lo))) +#define u64init(hi, lo) u64hilo(hi, lo) +#define u64lo(x) ((u64)(x)) +#define u64lt(x, y) ((x) < (y)) +#define u64and(x, y) ((x) & (y)) +#define u64or(x, y) ((x) | (y)) +#define u64xor(x, y) ((x) ^ (y)) +#define u64plus(x, y) ((x) + (y)) +#define u64shl(x, n) ((x) << (n)) +#define u64shr(x, n) ((x) >> (n)) #else /* u64 is a 64-bit unsigned integer value. u64init (HI, LO), is like u64hilo (HI, LO), but for use in initializer contexts. */ -# ifdef WORDS_BIGENDIAN -typedef struct { uint32_t hi, lo; } u64; -# define u64init(hi, lo) { hi, lo } -# else -typedef struct { uint32_t lo, hi; } u64; -# define u64init(hi, lo) { lo, hi } -# endif +#ifdef WORDS_BIGENDIAN +typedef struct { + uint32_t hi, lo; +} u64; +#define u64init(hi, lo) \ + { hi, lo } +#else +typedef struct { + uint32_t lo, hi; +} u64; +#define u64init(hi, lo) \ + { lo, hi } +#endif /* Given the high and low-order 32-bit quantities HI and LO, return a u64 value representing (HI << 32) + LO. */ -static inline u64 -u64hilo (uint32_t hi, uint32_t lo) -{ - u64 r; - r.hi = hi; - r.lo = lo; - return r; +static inline u64 u64hilo(uint32_t hi, uint32_t lo) { + u64 r; + r.hi = hi; + r.lo = lo; + return r; } /* Return a u64 value representing LO. */ -static inline u64 -u64lo (uint32_t lo) -{ - u64 r; - r.hi = 0; - r.lo = lo; - return r; +static inline u64 u64lo(uint32_t lo) { + u64 r; + r.hi = 0; + r.lo = lo; + return r; } /* Return X < Y. */ -static inline int -u64lt (u64 x, u64 y) -{ - return x.hi < y.hi || (x.hi == y.hi && x.lo < y.lo); -} +static inline int u64lt(u64 x, u64 y) { return x.hi < y.hi || (x.hi == y.hi && x.lo < y.lo); } /* Return X & Y. */ -static inline u64 -u64and (u64 x, u64 y) -{ - u64 r; - r.hi = x.hi & y.hi; - r.lo = x.lo & y.lo; - return r; +static inline u64 u64and(u64 x, u64 y) { + u64 r; + r.hi = x.hi & y.hi; + r.lo = x.lo & y.lo; + return r; } /* Return X | Y. */ -static inline u64 -u64or (u64 x, u64 y) -{ - u64 r; - r.hi = x.hi | y.hi; - r.lo = x.lo | y.lo; - return r; +static inline u64 u64or(u64 x, u64 y) { + u64 r; + r.hi = x.hi | y.hi; + r.lo = x.lo | y.lo; + return r; } /* Return X ^ Y. */ -static inline u64 -u64xor (u64 x, u64 y) -{ - u64 r; - r.hi = x.hi ^ y.hi; - r.lo = x.lo ^ y.lo; - return r; +static inline u64 u64xor(u64 x, u64 y) { + u64 r; + r.hi = x.hi ^ y.hi; + r.lo = x.lo ^ y.lo; + return r; } /* Return X + Y. */ -static inline u64 -u64plus (u64 x, u64 y) -{ - u64 r; - r.lo = x.lo + y.lo; - r.hi = x.hi + y.hi + (r.lo < x.lo); - return r; +static inline u64 u64plus(u64 x, u64 y) { + u64 r; + r.lo = x.lo + y.lo; + r.hi = x.hi + y.hi + (r.lo < x.lo); + return r; } /* Return X << N. */ -static inline u64 -u64shl (u64 x, int n) -{ - u64 r; - if (n < 32) - { - r.hi = (x.hi << n) | (x.lo >> (32 - n)); - r.lo = x.lo << n; +static inline u64 u64shl(u64 x, int n) { + u64 r; + if (n < 32) { + r.hi = (x.hi << n) | (x.lo >> (32 - n)); + r.lo = x.lo << n; + } else { + r.hi = x.lo << (n - 32); + r.lo = 0; } - else - { - r.hi = x.lo << (n - 32); - r.lo = 0; - } - return r; + return r; } /* Return X >> N. */ -static inline u64 -u64shr (u64 x, int n) -{ - u64 r; - if (n < 32) - { - r.hi = x.hi >> n; - r.lo = (x.hi << (32 - n)) | (x.lo >> n); - } - else - { - r.hi = 0; - r.lo = x.hi >> (n - 32); +static inline u64 u64shr(u64 x, int n) { + u64 r; + if (n < 32) { + r.hi = x.hi >> n; + r.lo = (x.hi << (32 - n)) | (x.lo >> n); + } else { + r.hi = 0; + r.lo = x.hi >> (n - 32); } - return r; + return r; } #endif diff --git a/src/MQClientAPIImpl.cpp b/src/MQClientAPIImpl.cpp index 8ec426db6..6787cfc7e 100644 --- a/src/MQClientAPIImpl.cpp +++ b/src/MQClientAPIImpl.cpp @@ -27,943 +27,820 @@ namespace rocketmq { //registerProcessor(CHECK_TRANSACTION_STATE, - clientRemotingProcessor); - m_pRemotingClient->registerProcessor(RESET_CONSUMER_CLIENT_OFFSET, - clientRemotingProcessor); - m_pRemotingClient->registerProcessor(GET_CONSUMER_STATUS_FROM_CLIENT, - clientRemotingProcessor); - m_pRemotingClient->registerProcessor(GET_CONSUMER_RUNNING_INFO, - clientRemotingProcessor); - m_pRemotingClient->registerProcessor(NOTIFY_CONSUMER_IDS_CHANGED, - clientRemotingProcessor); - m_pRemotingClient->registerProcessor(CONSUME_MESSAGE_DIRECTLY, - clientRemotingProcessor); - - m_topAddressing.reset(new TopAddressing(unitName)); + m_pRemotingClient.reset(new TcpRemotingClient(pullThreadNum, tcpConnectTimeout, tcpTransportTryLockTimeout)); + m_pRemotingClient->registerProcessor(CHECK_TRANSACTION_STATE, clientRemotingProcessor); + m_pRemotingClient->registerProcessor(RESET_CONSUMER_CLIENT_OFFSET, clientRemotingProcessor); + m_pRemotingClient->registerProcessor(GET_CONSUMER_STATUS_FROM_CLIENT, clientRemotingProcessor); + m_pRemotingClient->registerProcessor(GET_CONSUMER_RUNNING_INFO, clientRemotingProcessor); + m_pRemotingClient->registerProcessor(NOTIFY_CONSUMER_IDS_CHANGED, clientRemotingProcessor); + m_pRemotingClient->registerProcessor(CONSUME_MESSAGE_DIRECTLY, clientRemotingProcessor); + + m_topAddressing.reset(new TopAddressing(unitName)); } MQClientAPIImpl::~MQClientAPIImpl() { - m_pRemotingClient = NULL; - m_topAddressing = NULL; -} - -void MQClientAPIImpl::stopAllTcpTransportThread() { - m_pRemotingClient->stopAllTcpTransportThread(); -} - -bool MQClientAPIImpl::writeDataToFile(string filename, string data, - bool isSync) { - if (data.size() == 0) return false; - - FILE* pFd = fopen(filename.c_str(), "w+"); - if (NULL == pFd) { - LOG_ERROR("fopen failed, filename:%s", filename.c_str()); - return false; - } - - int byte_write = 0; - int byte_left = data.size(); - const char* pData = data.c_str(); - while (byte_left > 0) { - byte_write = fwrite(pData, sizeof(char), byte_left, pFd); - if (byte_write == byte_left) { - if (ferror(pFd)) { - LOG_ERROR("write data fail, data len:" SIZET_FMT ", file:%s, msg:%s", - data.size(), filename.c_str(), strerror(errno)); - fclose(pFd); + m_pRemotingClient = NULL; + m_topAddressing = NULL; +} + +void MQClientAPIImpl::stopAllTcpTransportThread() { m_pRemotingClient->stopAllTcpTransportThread(); } + +bool MQClientAPIImpl::writeDataToFile(string filename, string data, bool isSync) { + if (data.size() == 0) return false; + + FILE* pFd = fopen(filename.c_str(), "w+"); + if (NULL == pFd) { + LOG_ERROR("fopen failed, filename:%s", filename.c_str()); return false; - } } - byte_left -= byte_write; - pData += byte_write; - } - pData = NULL; - if (isSync) { - LOG_INFO("fsync with filename:%s", filename.c_str()); - fflush(pFd); - } - fclose(pFd); + int byte_write = 0; + int byte_left = data.size(); + const char* pData = data.c_str(); + while (byte_left > 0) { + byte_write = fwrite(pData, sizeof(char), byte_left, pFd); + if (byte_write == byte_left) { + if (ferror(pFd)) { + LOG_ERROR("write data fail, data len:" SIZET_FMT ", file:%s, msg:%s", data.size(), filename.c_str(), + strerror(errno)); + fclose(pFd); + return false; + } + } + byte_left -= byte_write; + pData += byte_write; + } + pData = NULL; + + if (isSync) { + LOG_INFO("fsync with filename:%s", filename.c_str()); + fflush(pFd); + } + fclose(pFd); - return true; + return true; } string MQClientAPIImpl::fetchNameServerAddr(const string& NSDomain) { - try { - string homeDir(UtilAll::getHomeDirectory()); - string storePath = homeDir + "/logs/rocketmq-cpp/snapshot"; - - boost::filesystem::path dir(storePath); - boost::system::error_code ec; - if (!boost::filesystem::exists(dir, ec)) { - if (!boost::filesystem::create_directory(dir, ec)) { - LOG_ERROR("create data dir:%s error", storePath.c_str()); - return ""; - } - } - string file(storePath); - string fileBak(storePath); - vector ret_; - int retSize = UtilAll::Split(ret_, m_mqClientId, "@"); - if (retSize == 2) { - file.append("/nameserver_addr-").append(ret_[retSize - 1]); - } else { - LOG_ERROR("split mqClientId:%s fail", m_mqClientId.c_str()); - file.append("/nameserver_addr-DEFAULT"); - } - boost::filesystem::path snapshot_file(file); - fileBak.append("/nameserver_addr.bak"); - const string addrs = m_topAddressing->fetchNSAddr(NSDomain); - if (addrs.empty()) { - if (m_nameSrvAddr.empty()) { - LOG_INFO("Load the name server snapshot local file:%s", file.c_str()); - if (boost::filesystem::exists(snapshot_file)) { - ifstream snapshot_file(file, ios::binary); - istreambuf_iterator beg(snapshot_file), end; - string filecontent(beg, end); - updateNameServerAddr(filecontent); - m_nameSrvAddr = filecontent; + try { + string homeDir(UtilAll::getHomeDirectory()); + string storePath = homeDir + "/logs/rocketmq-cpp/snapshot"; + + boost::filesystem::path dir(storePath); + boost::system::error_code ec; + if (!boost::filesystem::exists(dir, ec)) { + if (!boost::filesystem::create_directory(dir, ec)) { + LOG_ERROR("create data dir:%s error", storePath.c_str()); + return ""; + } + } + string file(storePath); + string fileBak(storePath); + vector ret_; + int retSize = UtilAll::Split(ret_, m_mqClientId, "@"); + if (retSize == 2) { + file.append("/nameserver_addr-").append(ret_[retSize - 1]); } else { - LOG_WARN("The name server snapshot local file not exists"); + LOG_ERROR("split mqClientId:%s fail", m_mqClientId.c_str()); + file.append("/nameserver_addr-DEFAULT"); } - } - } else { - if (m_firstFetchNameSrv == true) { - // it is the first time, so need to create the name server snapshot - // local file - m_firstFetchNameSrv = false; - } - if (addrs.compare(m_nameSrvAddr) != 0) { - LOG_INFO("name server address changed, old: %s, new: %s", - m_nameSrvAddr.c_str(), addrs.c_str()); - updateNameServerAddr(addrs); - m_nameSrvAddr = addrs; - } else { - if (!m_firstFetchNameSrv) return m_nameSrvAddr; - } - // update the snapshot local file if nameSrv changes or - // m_firstFetchNameSrv==true - if (writeDataToFile(fileBak, addrs, true)) { - if (!UtilAll::ReplaceFile(fileBak, file)) - LOG_ERROR("could not rename bak file:%s", strerror(errno)); - } - } - - if (!boost::filesystem::exists(snapshot_file)) { - // the name server snapshot local file maybe deleted by force, create it - if (writeDataToFile(fileBak, m_nameSrvAddr, true)) { - if (!UtilAll::ReplaceFile(fileBak, file)) - LOG_ERROR("could not rename bak file:%s", strerror(errno)); - } - } - } catch (...) { - } - return m_nameSrvAddr; + boost::filesystem::path snapshot_file(file); + fileBak.append("/nameserver_addr.bak"); + const string addrs = m_topAddressing->fetchNSAddr(NSDomain); + if (addrs.empty()) { + if (m_nameSrvAddr.empty()) { + LOG_INFO("Load the name server snapshot local file:%s", file.c_str()); + if (boost::filesystem::exists(snapshot_file)) { + ifstream snapshot_file(file, ios::binary); + istreambuf_iterator beg(snapshot_file), end; + string filecontent(beg, end); + updateNameServerAddr(filecontent); + m_nameSrvAddr = filecontent; + } else { + LOG_WARN("The name server snapshot local file not exists"); + } + } + } else { + if (m_firstFetchNameSrv == true) { + // it is the first time, so need to create the name server snapshot + // local file + m_firstFetchNameSrv = false; + } + if (addrs.compare(m_nameSrvAddr) != 0) { + LOG_INFO("name server address changed, old: %s, new: %s", m_nameSrvAddr.c_str(), addrs.c_str()); + updateNameServerAddr(addrs); + m_nameSrvAddr = addrs; + } else { + if (!m_firstFetchNameSrv) return m_nameSrvAddr; + } + // update the snapshot local file if nameSrv changes or + // m_firstFetchNameSrv==true + if (writeDataToFile(fileBak, addrs, true)) { + if (!UtilAll::ReplaceFile(fileBak, file)) LOG_ERROR("could not rename bak file:%s", strerror(errno)); + } + } + + if (!boost::filesystem::exists(snapshot_file)) { + // the name server snapshot local file maybe deleted by force, create it + if (writeDataToFile(fileBak, m_nameSrvAddr, true)) { + if (!UtilAll::ReplaceFile(fileBak, file)) LOG_ERROR("could not rename bak file:%s", strerror(errno)); + } + } + } + catch (...) { + } + return m_nameSrvAddr; } void MQClientAPIImpl::updateNameServerAddr(const string& addrs) { - if (m_pRemotingClient != NULL) - m_pRemotingClient->updateNameServerAddressList(addrs); + if (m_pRemotingClient != NULL) m_pRemotingClient->updateNameServerAddressList(addrs); } -void MQClientAPIImpl::callSignatureBeforeRequest( - const string& addr, RemotingCommand& request, - const SessionCredentials& session_credentials) { - ClientRPCHook rpcHook(session_credentials); - rpcHook.doBeforeRequest(addr, request); +void MQClientAPIImpl::callSignatureBeforeRequest(const string& addr, RemotingCommand& request, + const SessionCredentials& session_credentials) { + ClientRPCHook rpcHook(session_credentials); + rpcHook.doBeforeRequest(addr, request); } // Note: all request rules: throw exception if got broker error response, // exclude getTopicRouteInfoFromNameServer and unregisterClient -void MQClientAPIImpl::createTopic( - const string& addr, const string& defaultTopic, TopicConfig topicConfig, - const SessionCredentials& sessionCredentials) { - string topicWithProjectGroup = topicConfig.getTopicName(); - CreateTopicRequestHeader* requestHeader = new CreateTopicRequestHeader(); - requestHeader->topic = (topicWithProjectGroup); - requestHeader->defaultTopic = (defaultTopic); - requestHeader->readQueueNums = (topicConfig.getReadQueueNums()); - requestHeader->writeQueueNums = (topicConfig.getWriteQueueNums()); - requestHeader->perm = (topicConfig.getPerm()); - requestHeader->topicFilterType = (topicConfig.getTopicFilterType()); - - RemotingCommand request(UPDATE_AND_CREATE_TOPIC, requestHeader); - callSignatureBeforeRequest(addr, request, sessionCredentials); - request.Encode(); - - unique_ptr response( - m_pRemotingClient->invokeSync(addr, request)); - - if (response) { - switch (response->getCode()) { - case SUCCESS_VALUE: - return; - default: - break; - } - THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), - response->getCode()); - } - THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); -} - -SendResult MQClientAPIImpl::sendMessage( - const string& addr, const string& brokerName, const MQMessage& msg, - SendMessageRequestHeader* pRequestHeader, int timeoutMillis, int maxRetrySendTimes, - int communicationMode, SendCallback* pSendCallback, - const SessionCredentials& sessionCredentials) { - RemotingCommand request(SEND_MESSAGE, pRequestHeader); - string body = msg.getBody(); - request.SetBody(body.c_str(), body.length()); - request.setMsgBody(body); - callSignatureBeforeRequest(addr, request, sessionCredentials); - request.Encode(); - - switch (communicationMode) { - case ComMode_ONEWAY: - m_pRemotingClient->invokeOneway(addr, request); - break; - case ComMode_ASYNC: - sendMessageAsync(addr, brokerName, msg, request, pSendCallback, timeoutMillis, maxRetrySendTimes, 1); - break; - case ComMode_SYNC: - return sendMessageSync(addr, brokerName, msg, request, timeoutMillis); - default: - break; - } - return SendResult(); -} - -void MQClientAPIImpl::sendHearbeat( - const string& addr, HeartbeatData* pHeartbeatData, - const SessionCredentials& sessionCredentials) { - RemotingCommand request(HEART_BEAT, NULL); - - string body; - pHeartbeatData->Encode(body); - request.SetBody(body.data(), body.length()); - request.setMsgBody(body); - callSignatureBeforeRequest(addr, request, sessionCredentials); - request.Encode(); - - if (m_pRemotingClient->invokeHeartBeat(addr, request)) { - LOG_INFO("sendheartbeat to broker:%s success", addr.c_str()); - } -} - -void MQClientAPIImpl::unregisterClient( - const string& addr, const string& clientID, const string& producerGroup, - const string& consumerGroup, const SessionCredentials& sessionCredentials) { - LOG_INFO("unregisterClient to broker:%s", addr.c_str()); - RemotingCommand request(UNREGISTER_CLIENT, - new UnregisterClientRequestHeader( - clientID, producerGroup, consumerGroup)); - callSignatureBeforeRequest(addr, request, sessionCredentials); - request.Encode(); - - unique_ptr response( - m_pRemotingClient->invokeSync(addr, request)); - - if (response) { - switch (response->getCode()) { - case SUCCESS_VALUE: - LOG_INFO("unregisterClient to:%s success", addr.c_str()); - return; - default: - break; - } - LOG_WARN("unregisterClient fail:%s,%d", response->getRemark().c_str(), - response->getCode()); - } +void MQClientAPIImpl::createTopic(const string& addr, const string& defaultTopic, TopicConfig topicConfig, + const SessionCredentials& sessionCredentials) { + string topicWithProjectGroup = topicConfig.getTopicName(); + CreateTopicRequestHeader* requestHeader = new CreateTopicRequestHeader(); + requestHeader->topic = (topicWithProjectGroup); + requestHeader->defaultTopic = (defaultTopic); + requestHeader->readQueueNums = (topicConfig.getReadQueueNums()); + requestHeader->writeQueueNums = (topicConfig.getWriteQueueNums()); + requestHeader->perm = (topicConfig.getPerm()); + requestHeader->topicFilterType = (topicConfig.getTopicFilterType()); + + RemotingCommand request(UPDATE_AND_CREATE_TOPIC, requestHeader); + callSignatureBeforeRequest(addr, request, sessionCredentials); + request.Encode(); + + unique_ptr response(m_pRemotingClient->invokeSync(addr, request)); + + if (response) { + switch (response->getCode()) { + case SUCCESS_VALUE: + return; + default: + break; + } + THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), response->getCode()); + } + THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); +} + +SendResult MQClientAPIImpl::sendMessage(const string& addr, const string& brokerName, const MQMessage& msg, + SendMessageRequestHeader* pRequestHeader, int timeoutMillis, + int maxRetrySendTimes, int communicationMode, SendCallback* pSendCallback, + const SessionCredentials& sessionCredentials) { + RemotingCommand request(SEND_MESSAGE, pRequestHeader); + string body = msg.getBody(); + request.SetBody(body.c_str(), body.length()); + request.setMsgBody(body); + callSignatureBeforeRequest(addr, request, sessionCredentials); + request.Encode(); + + switch (communicationMode) { + case ComMode_ONEWAY: + m_pRemotingClient->invokeOneway(addr, request); + break; + case ComMode_ASYNC: + sendMessageAsync(addr, brokerName, msg, request, pSendCallback, timeoutMillis, maxRetrySendTimes, 1); + break; + case ComMode_SYNC: + return sendMessageSync(addr, brokerName, msg, request, timeoutMillis); + default: + break; + } + return SendResult(); } -// return NULL if got no response or error response -TopicRouteData* MQClientAPIImpl::getTopicRouteInfoFromNameServer( - const string& topic, int timeoutMillis, - const SessionCredentials& sessionCredentials) { - RemotingCommand request(GET_ROUTEINTO_BY_TOPIC, - new GetRouteInfoRequestHeader(topic)); - callSignatureBeforeRequest("", request, sessionCredentials); - request.Encode(); - - unique_ptr pResponse( - m_pRemotingClient->invokeSync("", request, timeoutMillis)); - - if (pResponse != NULL) { - if (((*(pResponse->GetBody())).getSize() == 0) || - ((*(pResponse->GetBody())).getData() != NULL)) { - switch (pResponse->getCode()) { - case SUCCESS_VALUE: { - const MemoryBlock* pbody = pResponse->GetBody(); - if (pbody->getSize()) { - TopicRouteData* topicRoute = TopicRouteData::Decode(pbody); - return topicRoute; - } +void MQClientAPIImpl::sendHearbeat(const string& addr, HeartbeatData* pHeartbeatData, + const SessionCredentials& sessionCredentials) { + RemotingCommand request(HEART_BEAT, NULL); + + string body; + pHeartbeatData->Encode(body); + request.SetBody(body.data(), body.length()); + request.setMsgBody(body); + callSignatureBeforeRequest(addr, request, sessionCredentials); + request.Encode(); + + if (m_pRemotingClient->invokeHeartBeat(addr, request)) { + LOG_INFO("sendheartbeat to broker:%s success", addr.c_str()); + } +} + +void MQClientAPIImpl::unregisterClient(const string& addr, const string& clientID, const string& producerGroup, + const string& consumerGroup, const SessionCredentials& sessionCredentials) { + LOG_INFO("unregisterClient to broker:%s", addr.c_str()); + RemotingCommand request(UNREGISTER_CLIENT, + new UnregisterClientRequestHeader(clientID, producerGroup, consumerGroup)); + callSignatureBeforeRequest(addr, request, sessionCredentials); + request.Encode(); + + unique_ptr response(m_pRemotingClient->invokeSync(addr, request)); + + if (response) { + switch (response->getCode()) { + case SUCCESS_VALUE: + LOG_INFO("unregisterClient to:%s success", addr.c_str()); + return; + default: + break; } - case TOPIC_NOT_EXIST: { - return NULL; + LOG_WARN("unregisterClient fail:%s,%d", response->getRemark().c_str(), response->getCode()); + } +} + +// return NULL if got no response or error response +TopicRouteData* MQClientAPIImpl::getTopicRouteInfoFromNameServer(const string& topic, int timeoutMillis, + const SessionCredentials& sessionCredentials) { + RemotingCommand request(GET_ROUTEINTO_BY_TOPIC, new GetRouteInfoRequestHeader(topic)); + callSignatureBeforeRequest("", request, sessionCredentials); + request.Encode(); + + unique_ptr pResponse(m_pRemotingClient->invokeSync("", request, timeoutMillis)); + + if (pResponse != NULL) { + if (((*(pResponse->GetBody())).getSize() == 0) || ((*(pResponse->GetBody())).getData() != NULL)) { + switch (pResponse->getCode()) { + case SUCCESS_VALUE: { + const MemoryBlock* pbody = pResponse->GetBody(); + if (pbody->getSize()) { + TopicRouteData* topicRoute = TopicRouteData::Decode(pbody); + return topicRoute; + } + } + case TOPIC_NOT_EXIST: { return NULL; } + default: + break; + } + LOG_WARN("%s,%d", pResponse->getRemark().c_str(), pResponse->getCode()); + return NULL; } - default: - break; - } - LOG_WARN("%s,%d", pResponse->getRemark().c_str(), pResponse->getCode()); - return NULL; - } - } - return NULL; -} - -TopicList* MQClientAPIImpl::getTopicListFromNameServer( - const SessionCredentials& sessionCredentials) { - RemotingCommand request(GET_ALL_TOPIC_LIST_FROM_NAMESERVER, NULL); - callSignatureBeforeRequest("", request, sessionCredentials); - request.Encode(); - - unique_ptr pResponse( - m_pRemotingClient->invokeSync("", request)); - if (pResponse != NULL) { - if (((*(pResponse->GetBody())).getSize() == 0) || - ((*(pResponse->GetBody())).getData() != NULL)) { - switch (pResponse->getCode()) { - case SUCCESS_VALUE: { - const MemoryBlock* pbody = pResponse->GetBody(); - if (pbody->getSize()) { - TopicList* topicList = TopicList::Decode(pbody); - return topicList; - } + } + return NULL; +} + +TopicList* MQClientAPIImpl::getTopicListFromNameServer(const SessionCredentials& sessionCredentials) { + RemotingCommand request(GET_ALL_TOPIC_LIST_FROM_NAMESERVER, NULL); + callSignatureBeforeRequest("", request, sessionCredentials); + request.Encode(); + + unique_ptr pResponse(m_pRemotingClient->invokeSync("", request)); + if (pResponse != NULL) { + if (((*(pResponse->GetBody())).getSize() == 0) || ((*(pResponse->GetBody())).getData() != NULL)) { + switch (pResponse->getCode()) { + case SUCCESS_VALUE: { + const MemoryBlock* pbody = pResponse->GetBody(); + if (pbody->getSize()) { + TopicList* topicList = TopicList::Decode(pbody); + return topicList; + } + } + default: + break; + } + + THROW_MQEXCEPTION(MQClientException, pResponse->getRemark(), pResponse->getCode()); } - default: - break; - } - - THROW_MQEXCEPTION(MQClientException, pResponse->getRemark(), - pResponse->getCode()); } - } - return NULL; + return NULL; } -int MQClientAPIImpl::wipeWritePermOfBroker(const string& namesrvAddr, - const string& brokerName, - int timeoutMillis) { - return 0; +int MQClientAPIImpl::wipeWritePermOfBroker(const string& namesrvAddr, const string& brokerName, int timeoutMillis) { + return 0; } -void MQClientAPIImpl::deleteTopicInBroker(const string& addr, - const string& topic, - int timeoutMillis) {} +void MQClientAPIImpl::deleteTopicInBroker(const string& addr, const string& topic, int timeoutMillis) {} -void MQClientAPIImpl::deleteTopicInNameServer(const string& addr, - const string& topic, - int timeoutMillis) {} +void MQClientAPIImpl::deleteTopicInNameServer(const string& addr, const string& topic, int timeoutMillis) {} -void MQClientAPIImpl::deleteSubscriptionGroup(const string& addr, - const string& groupName, - int timeoutMillis) {} +void MQClientAPIImpl::deleteSubscriptionGroup(const string& addr, const string& groupName, int timeoutMillis) {} -string MQClientAPIImpl::getKVConfigByValue(const string& projectNamespace, - const string& projectGroup, +string MQClientAPIImpl::getKVConfigByValue(const string& projectNamespace, const string& projectGroup, int timeoutMillis) { - return ""; + return ""; } -KVTable MQClientAPIImpl::getKVListByNamespace(const string& projectNamespace, - int timeoutMillis) { - return KVTable(); -} +KVTable MQClientAPIImpl::getKVListByNamespace(const string& projectNamespace, int timeoutMillis) { return KVTable(); } -void MQClientAPIImpl::deleteKVConfigByValue(const string& projectNamespace, - const string& projectGroup, +void MQClientAPIImpl::deleteKVConfigByValue(const string& projectNamespace, const string& projectGroup, int timeoutMillis) {} -SendResult MQClientAPIImpl::sendMessageSync(const string& addr, - const string& brokerName, - const MQMessage& msg, - RemotingCommand& request, - int timeoutMillis) { - // pResponse( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); - if (pResponse != NULL) { - try { - LOG_DEBUG("sendMessageSync success:%s to addr:%s,brokername:%s", - msg.toString().c_str(), addr.c_str(), brokerName.c_str()); - SendResult result = processSendResponse(brokerName, msg, pResponse.get()); - return result; - } catch (...) { - LOG_ERROR("send error"); - } - } - THROW_MQEXCEPTION(MQClientException, "response is null", -1); -} - -void MQClientAPIImpl::sendMessageAsync(const string& addr, - const string& brokerName, - const MQMessage& msg, - RemotingCommand& request, - SendCallback* pSendCallback, - int64 timeoutMilliseconds, - int maxRetryTimes, - int retrySendTimes) { - int64 begin_time = UtilAll::currentTimeMillis(); - //invokeAsync(addr, request, cbw, timeoutMilliseconds, maxRetryTimes, retrySendTimes) == - false) { - LOG_WARN("invokeAsync failed to addr:%s,topic:%s, timeout:%lld, maxRetryTimes:%d, retrySendTimes:%d", - addr.c_str(), msg.getTopic().data(), timeoutMilliseconds, maxRetryTimes, retrySendTimes); - //when getTcp return false, need consider retrySendTimes - int retry_time = retrySendTimes + 1; - int64 time_out = timeoutMilliseconds - (UtilAll::currentTimeMillis() - begin_time); - while (retry_time < maxRetryTimes && time_out > 0) { - begin_time = UtilAll::currentTimeMillis(); - if (m_pRemotingClient->invokeAsync(addr, request, cbw, time_out, maxRetryTimes, retry_time) == false) { - retry_time += 1; - time_out = time_out - (UtilAll::currentTimeMillis() - begin_time); - LOG_WARN("invokeAsync retry failed to addr:%s,topic:%s, timeout:%lld, maxRetryTimes:%d, retrySendTimes:%d", - addr.c_str(), msg.getTopic().data(), time_out, maxRetryTimes, retry_time); - continue; - } else { - return; //invokeAsync success - } - } - - LOG_ERROR("sendMessageAsync failed to addr:%s,topic:%s, timeout:%lld, maxRetryTimes:%d, retrySendTimes:%d", - addr.c_str(), msg.getTopic().data(), time_out, maxRetryTimes, retrySendTimes); - - if (cbw) { - cbw->onException(); - deleteAndZero(cbw); - } else { - THROW_MQEXCEPTION(MQClientException, "sendMessageAsync failed", -1); +SendResult MQClientAPIImpl::sendMessageSync(const string& addr, const string& brokerName, const MQMessage& msg, + RemotingCommand& request, int timeoutMillis) { + // pResponse(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + if (pResponse != NULL) { + try { + LOG_DEBUG("sendMessageSync success:%s to addr:%s,brokername:%s", msg.toString().c_str(), addr.c_str(), + brokerName.c_str()); + SendResult result = processSendResponse(brokerName, msg, pResponse.get()); + return result; + } + catch (...) { + LOG_ERROR("send error"); + } + } + THROW_MQEXCEPTION(MQClientException, "response is null", -1); +} + +void MQClientAPIImpl::sendMessageAsync(const string& addr, const string& brokerName, const MQMessage& msg, + RemotingCommand& request, SendCallback* pSendCallback, int64 timeoutMilliseconds, + int maxRetryTimes, int retrySendTimes) { + int64 begin_time = UtilAll::currentTimeMillis(); + //invokeAsync(addr, request, cbw, timeoutMilliseconds, maxRetryTimes, retrySendTimes) == + false) { + LOG_WARN("invokeAsync failed to addr:%s,topic:%s, timeout:%lld, maxRetryTimes:%d, retrySendTimes:%d", + addr.c_str(), msg.getTopic().data(), timeoutMilliseconds, maxRetryTimes, retrySendTimes); + // when getTcp return false, need consider retrySendTimes + int retry_time = retrySendTimes + 1; + int64 time_out = timeoutMilliseconds - (UtilAll::currentTimeMillis() - begin_time); + while (retry_time < maxRetryTimes && time_out > 0) { + begin_time = UtilAll::currentTimeMillis(); + if (m_pRemotingClient->invokeAsync(addr, request, cbw, time_out, maxRetryTimes, retry_time) == false) { + retry_time += 1; + time_out = time_out - (UtilAll::currentTimeMillis() - begin_time); + LOG_WARN( + "invokeAsync retry failed to addr:%s,topic:%s, timeout:%lld, maxRetryTimes:%d, retrySendTimes:%d", + addr.c_str(), msg.getTopic().data(), time_out, maxRetryTimes, retry_time); + continue; + } else { + return; // invokeAsync success + } + } + + LOG_ERROR("sendMessageAsync failed to addr:%s,topic:%s, timeout:%lld, maxRetryTimes:%d, retrySendTimes:%d", + addr.c_str(), msg.getTopic().data(), time_out, maxRetryTimes, retrySendTimes); + + if (cbw) { + cbw->onException(); + deleteAndZero(cbw); + } else { + THROW_MQEXCEPTION(MQClientException, "sendMessageAsync failed", -1); + } } - } } void MQClientAPIImpl::deleteOpaqueForDropPullRequest(const MQMessageQueue& mq, int opaque) { m_pRemotingClient->deleteOpaqueForDropPullRequest(mq, opaque); } -PullResult* MQClientAPIImpl::pullMessage( - const string& addr, PullMessageRequestHeader* pRequestHeader, - int timeoutMillis, int communicationMode, PullCallback* pullCallback, - void* pArg, const SessionCredentials& sessionCredentials) { - RemotingCommand request(PULL_MESSAGE, pRequestHeader); - callSignatureBeforeRequest(addr, request, sessionCredentials); - request.Encode(); - - switch (communicationMode) { - case ComMode_ONEWAY: - break; - case ComMode_ASYNC: - pullMessageAsync(addr, request, timeoutMillis, pullCallback, pArg); - break; - case ComMode_SYNC: - return pullMessageSync(addr, request, timeoutMillis); - default: - break; - } - - return NULL; -} - -void MQClientAPIImpl::pullMessageAsync(const string& addr, - RemotingCommand& request, - int timeoutMillis, +PullResult* MQClientAPIImpl::pullMessage(const string& addr, PullMessageRequestHeader* pRequestHeader, + int timeoutMillis, int communicationMode, PullCallback* pullCallback, + void* pArg, const SessionCredentials& sessionCredentials) { + RemotingCommand request(PULL_MESSAGE, pRequestHeader); + callSignatureBeforeRequest(addr, request, sessionCredentials); + request.Encode(); + + switch (communicationMode) { + case ComMode_ONEWAY: + break; + case ComMode_ASYNC: + pullMessageAsync(addr, request, timeoutMillis, pullCallback, pArg); + break; + case ComMode_SYNC: + return pullMessageSync(addr, request, timeoutMillis); + default: + break; + } + + return NULL; +} + +void MQClientAPIImpl::pullMessageAsync(const string& addr, RemotingCommand& request, int timeoutMillis, PullCallback* pullCallback, void* pArg) { - //(pArg); - if (pAsyncArg && pAsyncArg->pPullRequest) { - mq = pAsyncArg->mq; - pAsyncArg->pPullRequest->setLatestPullRequestOpaque(request.getOpaque()); - LOG_DEBUG("pullMessageAsync set opaque:%d, mq:%s", - pAsyncArg->pPullRequest->getLatestPullRequestOpaque(),mq.toString().c_str()); - } - - if (m_pRemotingClient->invokeAsync(addr, request, cbw, timeoutMillis) == - false) { - LOG_ERROR("pullMessageAsync failed of addr:%s, opaque:%d, mq:%s", addr.c_str(), request.getOpaque(), mq.toString().data()); + //(pArg); if (pAsyncArg && pAsyncArg->pPullRequest) { - pAsyncArg->pPullRequest->setLatestPullRequestOpaque(0); - } - deleteAndZero(cbw); - THROW_MQEXCEPTION(MQClientException, "pullMessageAsync failed", -1); - } -} - -PullResult* MQClientAPIImpl::pullMessageSync(const string& addr, - RemotingCommand& request, - int timeoutMillis) { - unique_ptr pResponse( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); - if (pResponse != NULL) { - if (((*(pResponse->GetBody())).getSize() == 0) || - ((*(pResponse->GetBody())).getData() != NULL)) { - try { - PullResult* pullResult = - processPullResponse(pResponse.get()); // pullMessage will handle - // exception from - // processPullResponse - return pullResult; - } catch (MQException& e) { - LOG_ERROR(e.what()); - return NULL; - } - } - } - return NULL; -} - -SendResult MQClientAPIImpl::processSendResponse(const string& brokerName, - const MQMessage& msg, + mq = pAsyncArg->mq; + pAsyncArg->pPullRequest->setLatestPullRequestOpaque(request.getOpaque()); + LOG_DEBUG("pullMessageAsync set opaque:%d, mq:%s", pAsyncArg->pPullRequest->getLatestPullRequestOpaque(), + mq.toString().c_str()); + } + + if (m_pRemotingClient->invokeAsync(addr, request, cbw, timeoutMillis) == false) { + LOG_ERROR("pullMessageAsync failed of addr:%s, opaque:%d, mq:%s", addr.c_str(), request.getOpaque(), + mq.toString().data()); + if (pAsyncArg && pAsyncArg->pPullRequest) { + pAsyncArg->pPullRequest->setLatestPullRequestOpaque(0); + } + deleteAndZero(cbw); + THROW_MQEXCEPTION(MQClientException, "pullMessageAsync failed", -1); + } +} + +PullResult* MQClientAPIImpl::pullMessageSync(const string& addr, RemotingCommand& request, int timeoutMillis) { + unique_ptr pResponse(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + if (pResponse != NULL) { + if (((*(pResponse->GetBody())).getSize() == 0) || ((*(pResponse->GetBody())).getData() != NULL)) { + try { + PullResult* pullResult = processPullResponse(pResponse.get()); // pullMessage will handle + // exception from + // processPullResponse + return pullResult; + } + catch (MQException& e) { + LOG_ERROR(e.what()); + return NULL; + } + } + } + return NULL; +} + +SendResult MQClientAPIImpl::processSendResponse(const string& brokerName, const MQMessage& msg, RemotingCommand* pResponse) { - SendStatus sendStatus = SEND_OK; - int res = 0; - switch (pResponse->getCode()) { - case FLUSH_DISK_TIMEOUT: - sendStatus = SEND_FLUSH_DISK_TIMEOUT; - break; - case FLUSH_SLAVE_TIMEOUT: - sendStatus = SEND_FLUSH_SLAVE_TIMEOUT; - break; - case SLAVE_NOT_AVAILABLE: - sendStatus = SEND_SLAVE_NOT_AVAILABLE; - break; - case SUCCESS_VALUE: - sendStatus = SEND_OK; - break; - default: - res = -1; - break; - } - if (res == 0) { - SendMessageResponseHeader* responseHeader = - (SendMessageResponseHeader*)pResponse->getCommandHeader(); - MQMessageQueue messageQueue(msg.getTopic(), brokerName, - responseHeader->queueId); - string unique_msgId = msg.getProperty(MQMessage::PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX); - return SendResult(sendStatus, unique_msgId, responseHeader->msgId, messageQueue, - responseHeader->queueOffset); - } - LOG_ERROR("processSendResponse error remark:%s, error code:%d", - (pResponse->getRemark()).c_str(), pResponse->getCode()); - THROW_MQEXCEPTION(MQClientException, pResponse->getRemark(), - pResponse->getCode()); + SendStatus sendStatus = SEND_OK; + int res = 0; + switch (pResponse->getCode()) { + case FLUSH_DISK_TIMEOUT: + sendStatus = SEND_FLUSH_DISK_TIMEOUT; + break; + case FLUSH_SLAVE_TIMEOUT: + sendStatus = SEND_FLUSH_SLAVE_TIMEOUT; + break; + case SLAVE_NOT_AVAILABLE: + sendStatus = SEND_SLAVE_NOT_AVAILABLE; + break; + case SUCCESS_VALUE: + sendStatus = SEND_OK; + break; + default: + res = -1; + break; + } + if (res == 0) { + SendMessageResponseHeader* responseHeader = (SendMessageResponseHeader*)pResponse->getCommandHeader(); + MQMessageQueue messageQueue(msg.getTopic(), brokerName, responseHeader->queueId); + string unique_msgId = msg.getProperty(MQMessage::PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX); + return SendResult(sendStatus, unique_msgId, responseHeader->msgId, messageQueue, responseHeader->queueOffset); + } + LOG_ERROR("processSendResponse error remark:%s, error code:%d", (pResponse->getRemark()).c_str(), + pResponse->getCode()); + THROW_MQEXCEPTION(MQClientException, pResponse->getRemark(), pResponse->getCode()); } PullResult* MQClientAPIImpl::processPullResponse(RemotingCommand* pResponse) { - PullStatus pullStatus = NO_NEW_MSG; - switch (pResponse->getCode()) { - case SUCCESS_VALUE: - pullStatus = FOUND; - break; - case PULL_NOT_FOUND: - pullStatus = NO_NEW_MSG; - break; - case PULL_RETRY_IMMEDIATELY: - pullStatus = NO_MATCHED_MSG; - break; - case PULL_OFFSET_MOVED: - pullStatus = OFFSET_ILLEGAL; - break; - default: - THROW_MQEXCEPTION(MQBrokerException, pResponse->getRemark(), - pResponse->getCode()); - break; - } - - PullMessageResponseHeader* responseHeader = - static_cast(pResponse->getCommandHeader()); - - if (!responseHeader) { - LOG_ERROR("processPullResponse:responseHeader is NULL"); - THROW_MQEXCEPTION(MQClientException, - "processPullResponse:responseHeader is NULL", -1); - } - //GetBody()); // response data judgement had been done outside - // of processPullResponse - if (bodyFromResponse.getSize() == 0) { - if (pullStatus != FOUND) { - return new PullResultExt(pullStatus, responseHeader->nextBeginOffset, - responseHeader->minOffset, - responseHeader->maxOffset, - (int)responseHeader->suggestWhichBrokerId); + PullStatus pullStatus = NO_NEW_MSG; + switch (pResponse->getCode()) { + case SUCCESS_VALUE: + pullStatus = FOUND; + break; + case PULL_NOT_FOUND: + pullStatus = NO_NEW_MSG; + break; + case PULL_RETRY_IMMEDIATELY: + pullStatus = NO_MATCHED_MSG; + break; + case PULL_OFFSET_MOVED: + pullStatus = OFFSET_ILLEGAL; + break; + default: + THROW_MQEXCEPTION(MQBrokerException, pResponse->getRemark(), pResponse->getCode()); + break; + } + + PullMessageResponseHeader* responseHeader = static_cast(pResponse->getCommandHeader()); + + if (!responseHeader) { + LOG_ERROR("processPullResponse:responseHeader is NULL"); + THROW_MQEXCEPTION(MQClientException, "processPullResponse:responseHeader is NULL", -1); + } + //GetBody()); // response data judgement had been done outside + // of processPullResponse + if (bodyFromResponse.getSize() == 0) { + if (pullStatus != FOUND) { + return new PullResultExt(pullStatus, responseHeader->nextBeginOffset, responseHeader->minOffset, + responseHeader->maxOffset, (int)responseHeader->suggestWhichBrokerId); + } else { + THROW_MQEXCEPTION(MQClientException, "memoryBody size is 0, but pullStatus equals found", -1); + } } else { - THROW_MQEXCEPTION(MQClientException, - "memoryBody size is 0, but pullStatus equals found", - -1); + return new PullResultExt(pullStatus, responseHeader->nextBeginOffset, responseHeader->minOffset, + responseHeader->maxOffset, (int)responseHeader->suggestWhichBrokerId, + bodyFromResponse); } - } else { - return new PullResultExt( - pullStatus, responseHeader->nextBeginOffset, responseHeader->minOffset, - responseHeader->maxOffset, (int)responseHeader->suggestWhichBrokerId, - bodyFromResponse); - } } //topic = topic; - pRequestHeader->queueId = queueId; - - RemotingCommand request(GET_MIN_OFFSET, pRequestHeader); - callSignatureBeforeRequest(addr, request, sessionCredentials); - request.Encode(); - - unique_ptr response( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); - - if (response) { - switch (response->getCode()) { - case SUCCESS_VALUE: { - GetMinOffsetResponseHeader* responseHeader = - (GetMinOffsetResponseHeader*)response->getCommandHeader(); - - int64 offset = responseHeader->offset; - return offset; - } - default: - break; - } - THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), - response->getCode()); - } - THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); -} - -int64 MQClientAPIImpl::getMaxOffset( - const string& addr, const string& topic, int queueId, int timeoutMillis, - const SessionCredentials& sessionCredentials) { - GetMaxOffsetRequestHeader* pRequestHeader = new GetMaxOffsetRequestHeader(); - pRequestHeader->topic = topic; - pRequestHeader->queueId = queueId; - - RemotingCommand request(GET_MAX_OFFSET, pRequestHeader); - callSignatureBeforeRequest(addr, request, sessionCredentials); - request.Encode(); - - unique_ptr response( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); - - if (response) { - switch (response->getCode()) { - case SUCCESS_VALUE: { - GetMaxOffsetResponseHeader* responseHeader = - (GetMaxOffsetResponseHeader*)response->getCommandHeader(); - - int64 offset = responseHeader->offset; - return offset; - } - default: - break; - } - THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), - response->getCode()); - } - THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); -} - -int64 MQClientAPIImpl::searchOffset( - const string& addr, const string& topic, int queueId, uint64_t timestamp, - int timeoutMillis, const SessionCredentials& sessionCredentials) { - SearchOffsetRequestHeader* pRequestHeader = new SearchOffsetRequestHeader(); - pRequestHeader->topic = topic; - pRequestHeader->queueId = queueId; - pRequestHeader->timestamp = timestamp; - - RemotingCommand request(SEARCH_OFFSET_BY_TIMESTAMP, pRequestHeader); - callSignatureBeforeRequest(addr, request, sessionCredentials); - request.Encode(); - - unique_ptr response( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); - - if (response) { - switch (response->getCode()) { - case SUCCESS_VALUE: { - SearchOffsetResponseHeader* responseHeader = - (SearchOffsetResponseHeader*)response->getCommandHeader(); - - int64 offset = responseHeader->offset; - return offset; - } - default: - break; - } - THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), - response->getCode()); - } - THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); -} - -MQMessageExt* MQClientAPIImpl::viewMessage( - const string& addr, int64 phyoffset, int timeoutMillis, - const SessionCredentials& sessionCredentials) { - ViewMessageRequestHeader* pRequestHeader = new ViewMessageRequestHeader(); - pRequestHeader->offset = phyoffset; - - RemotingCommand request(VIEW_MESSAGE_BY_ID, pRequestHeader); - callSignatureBeforeRequest(addr, request, sessionCredentials); - request.Encode(); - - unique_ptr response( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); - - if (response) { - switch (response->getCode()) { - case SUCCESS_VALUE: { - } - default: - break; - } - THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), - response->getCode()); - } - THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); -} - -int64 MQClientAPIImpl::getEarliestMsgStoretime( - const string& addr, const string& topic, int queueId, int timeoutMillis, - const SessionCredentials& sessionCredentials) { - GetEarliestMsgStoretimeRequestHeader* pRequestHeader = - new GetEarliestMsgStoretimeRequestHeader(); - pRequestHeader->topic = topic; - pRequestHeader->queueId = queueId; - - RemotingCommand request(GET_EARLIEST_MSG_STORETIME, pRequestHeader); - callSignatureBeforeRequest(addr, request, sessionCredentials); - request.Encode(); - - unique_ptr response( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); - - if (response) { - switch (response->getCode()) { - case SUCCESS_VALUE: { - GetEarliestMsgStoretimeResponseHeader* responseHeader = - (GetEarliestMsgStoretimeResponseHeader*) - response->getCommandHeader(); - - int64 timestamp = responseHeader->timestamp; - return timestamp; - } - default: - break; - } - THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), - response->getCode()); - } - THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); -} - -void MQClientAPIImpl::getConsumerIdListByGroup( - const string& addr, const string& consumerGroup, vector& cids, - int timeoutMillis, const SessionCredentials& sessionCredentials) { - GetConsumerListByGroupRequestHeader* pRequestHeader = - new GetConsumerListByGroupRequestHeader(); - pRequestHeader->consumerGroup = consumerGroup; - - RemotingCommand request(GET_CONSUMER_LIST_BY_GROUP, pRequestHeader); - callSignatureBeforeRequest(addr, request, sessionCredentials); - request.Encode(); - - unique_ptr pResponse( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); - - if (pResponse != NULL) { - if ((pResponse->GetBody()->getSize() == 0) || - (pResponse->GetBody()->getData() != NULL)) { - switch (pResponse->getCode()) { - case SUCCESS_VALUE: { - const MemoryBlock* pbody = pResponse->GetBody(); - if (pbody->getSize()) { - GetConsumerListByGroupResponseBody::Decode(pbody, cids); - return; - } +int64 MQClientAPIImpl::getMinOffset(const string& addr, const string& topic, int queueId, int timeoutMillis, + const SessionCredentials& sessionCredentials) { + GetMinOffsetRequestHeader* pRequestHeader = new GetMinOffsetRequestHeader(); + pRequestHeader->topic = topic; + pRequestHeader->queueId = queueId; + + RemotingCommand request(GET_MIN_OFFSET, pRequestHeader); + callSignatureBeforeRequest(addr, request, sessionCredentials); + request.Encode(); + + unique_ptr response(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + + if (response) { + switch (response->getCode()) { + case SUCCESS_VALUE: { + GetMinOffsetResponseHeader* responseHeader = (GetMinOffsetResponseHeader*)response->getCommandHeader(); + + int64 offset = responseHeader->offset; + return offset; + } + default: + break; } - default: - break; - } - THROW_MQEXCEPTION(MQBrokerException, pResponse->getRemark(), - pResponse->getCode()); - } - } - THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); -} - -int64 MQClientAPIImpl::queryConsumerOffset( - const string& addr, QueryConsumerOffsetRequestHeader* pRequestHeader, - int timeoutMillis, const SessionCredentials& sessionCredentials) { - RemotingCommand request(QUERY_CONSUMER_OFFSET, pRequestHeader); - callSignatureBeforeRequest(addr, request, sessionCredentials); - request.Encode(); - - unique_ptr response( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); - - if (response) { - switch (response->getCode()) { - case SUCCESS_VALUE: { - QueryConsumerOffsetResponseHeader* responseHeader = - (QueryConsumerOffsetResponseHeader*)response->getCommandHeader(); - int64 consumerOffset = responseHeader->offset; - return consumerOffset; - } - default: - break; - } - THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), - response->getCode()); - } - THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); - return -1; -} - -void MQClientAPIImpl::updateConsumerOffset( - const string& addr, UpdateConsumerOffsetRequestHeader* pRequestHeader, - int timeoutMillis, const SessionCredentials& sessionCredentials) { - RemotingCommand request(UPDATE_CONSUMER_OFFSET, pRequestHeader); - callSignatureBeforeRequest(addr, request, sessionCredentials); - request.Encode(); - - unique_ptr response( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); - - if (response) { - switch (response->getCode()) { - case SUCCESS_VALUE: { - return; - } - default: - break; - } - THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), - response->getCode()); - } - THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); -} - -void MQClientAPIImpl::updateConsumerOffsetOneway( - const string& addr, UpdateConsumerOffsetRequestHeader* pRequestHeader, - int timeoutMillis, const SessionCredentials& sessionCredentials) { - RemotingCommand request(UPDATE_CONSUMER_OFFSET, pRequestHeader); - callSignatureBeforeRequest(addr, request, sessionCredentials); - request.Encode(); - - m_pRemotingClient->invokeOneway(addr, request); -} - -void MQClientAPIImpl::consumerSendMessageBack( - MQMessageExt& msg, const string& consumerGroup, int delayLevel, - int timeoutMillis, const SessionCredentials& sessionCredentials) { - ConsumerSendMsgBackRequestHeader* pRequestHeader = - new ConsumerSendMsgBackRequestHeader(); - pRequestHeader->group = consumerGroup; - pRequestHeader->offset = msg.getCommitLogOffset(); - pRequestHeader->delayLevel = delayLevel; - - string addr = socketAddress2IPPort(msg.getStoreHost()); - RemotingCommand request(CONSUMER_SEND_MSG_BACK, pRequestHeader); - callSignatureBeforeRequest(addr, request, sessionCredentials); - request.Encode(); - - unique_ptr response( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); - - if (response) { - switch (response->getCode()) { - case SUCCESS_VALUE: { - return; - } - default: - break; - } - THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), - response->getCode()); - } - THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); -} - -void MQClientAPIImpl::lockBatchMQ( - const string& addr, LockBatchRequestBody* requestBody, - vector& mqs, int timeoutMillis, - const SessionCredentials& sessionCredentials) { - RemotingCommand request(LOCK_BATCH_MQ, NULL); - string body; - requestBody->Encode(body); - request.SetBody(body.data(), body.length()); - request.setMsgBody(body); - callSignatureBeforeRequest(addr, request, sessionCredentials); - request.Encode(); - - unique_ptr pResponse( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); - - if (pResponse != NULL) { - if (((*(pResponse->GetBody())).getSize() == 0) || - ((*(pResponse->GetBody())).getData() != NULL)) { - switch (pResponse->getCode()) { - case SUCCESS_VALUE: { - const MemoryBlock* pbody = pResponse->GetBody(); - if (pbody->getSize()) { - LockBatchResponseBody::Decode(pbody, mqs); - } - return; - } break; - default: - break; - } - THROW_MQEXCEPTION(MQBrokerException, pResponse->getRemark(), - pResponse->getCode()); - } - } - THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); -} - -void MQClientAPIImpl::unlockBatchMQ( - const string& addr, UnlockBatchRequestBody* requestBody, int timeoutMillis, - const SessionCredentials& sessionCredentials) { - RemotingCommand request(UNLOCK_BATCH_MQ, NULL); - string body; - requestBody->Encode(body); - request.SetBody(body.data(), body.length()); - request.setMsgBody(body); - callSignatureBeforeRequest(addr, request, sessionCredentials); - request.Encode(); - - unique_ptr pResponse( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); - - if (pResponse != NULL) { - switch (pResponse->getCode()) { - case SUCCESS_VALUE: { - return; - } break; - default: - break; - } - THROW_MQEXCEPTION(MQBrokerException, pResponse->getRemark(), - pResponse->getCode()); - } - THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); + THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), response->getCode()); + } + THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); +} + +int64 MQClientAPIImpl::getMaxOffset(const string& addr, const string& topic, int queueId, int timeoutMillis, + const SessionCredentials& sessionCredentials) { + GetMaxOffsetRequestHeader* pRequestHeader = new GetMaxOffsetRequestHeader(); + pRequestHeader->topic = topic; + pRequestHeader->queueId = queueId; + + RemotingCommand request(GET_MAX_OFFSET, pRequestHeader); + callSignatureBeforeRequest(addr, request, sessionCredentials); + request.Encode(); + + unique_ptr response(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + + if (response) { + switch (response->getCode()) { + case SUCCESS_VALUE: { + GetMaxOffsetResponseHeader* responseHeader = (GetMaxOffsetResponseHeader*)response->getCommandHeader(); + + int64 offset = responseHeader->offset; + return offset; + } + default: + break; + } + THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), response->getCode()); + } + THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); +} + +int64 MQClientAPIImpl::searchOffset(const string& addr, const string& topic, int queueId, uint64_t timestamp, + int timeoutMillis, const SessionCredentials& sessionCredentials) { + SearchOffsetRequestHeader* pRequestHeader = new SearchOffsetRequestHeader(); + pRequestHeader->topic = topic; + pRequestHeader->queueId = queueId; + pRequestHeader->timestamp = timestamp; + + RemotingCommand request(SEARCH_OFFSET_BY_TIMESTAMP, pRequestHeader); + callSignatureBeforeRequest(addr, request, sessionCredentials); + request.Encode(); + + unique_ptr response(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + + if (response) { + switch (response->getCode()) { + case SUCCESS_VALUE: { + SearchOffsetResponseHeader* responseHeader = (SearchOffsetResponseHeader*)response->getCommandHeader(); + + int64 offset = responseHeader->offset; + return offset; + } + default: + break; + } + THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), response->getCode()); + } + THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); +} + +MQMessageExt* MQClientAPIImpl::viewMessage(const string& addr, int64 phyoffset, int timeoutMillis, + const SessionCredentials& sessionCredentials) { + ViewMessageRequestHeader* pRequestHeader = new ViewMessageRequestHeader(); + pRequestHeader->offset = phyoffset; + + RemotingCommand request(VIEW_MESSAGE_BY_ID, pRequestHeader); + callSignatureBeforeRequest(addr, request, sessionCredentials); + request.Encode(); + + unique_ptr response(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + + if (response) { + switch (response->getCode()) { + case SUCCESS_VALUE: {} + default: + break; + } + THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), response->getCode()); + } + THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); +} + +int64 MQClientAPIImpl::getEarliestMsgStoretime(const string& addr, const string& topic, int queueId, int timeoutMillis, + const SessionCredentials& sessionCredentials) { + GetEarliestMsgStoretimeRequestHeader* pRequestHeader = new GetEarliestMsgStoretimeRequestHeader(); + pRequestHeader->topic = topic; + pRequestHeader->queueId = queueId; + + RemotingCommand request(GET_EARLIEST_MSG_STORETIME, pRequestHeader); + callSignatureBeforeRequest(addr, request, sessionCredentials); + request.Encode(); + + unique_ptr response(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + + if (response) { + switch (response->getCode()) { + case SUCCESS_VALUE: { + GetEarliestMsgStoretimeResponseHeader* responseHeader = + (GetEarliestMsgStoretimeResponseHeader*)response->getCommandHeader(); + + int64 timestamp = responseHeader->timestamp; + return timestamp; + } + default: + break; + } + THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), response->getCode()); + } + THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); +} + +void MQClientAPIImpl::getConsumerIdListByGroup(const string& addr, const string& consumerGroup, vector& cids, + int timeoutMillis, const SessionCredentials& sessionCredentials) { + GetConsumerListByGroupRequestHeader* pRequestHeader = new GetConsumerListByGroupRequestHeader(); + pRequestHeader->consumerGroup = consumerGroup; + + RemotingCommand request(GET_CONSUMER_LIST_BY_GROUP, pRequestHeader); + callSignatureBeforeRequest(addr, request, sessionCredentials); + request.Encode(); + + unique_ptr pResponse(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + + if (pResponse != NULL) { + if ((pResponse->GetBody()->getSize() == 0) || (pResponse->GetBody()->getData() != NULL)) { + switch (pResponse->getCode()) { + case SUCCESS_VALUE: { + const MemoryBlock* pbody = pResponse->GetBody(); + if (pbody->getSize()) { + GetConsumerListByGroupResponseBody::Decode(pbody, cids); + return; + } + } + default: + break; + } + THROW_MQEXCEPTION(MQBrokerException, pResponse->getRemark(), pResponse->getCode()); + } + } + THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); +} + +int64 MQClientAPIImpl::queryConsumerOffset(const string& addr, QueryConsumerOffsetRequestHeader* pRequestHeader, + int timeoutMillis, const SessionCredentials& sessionCredentials) { + RemotingCommand request(QUERY_CONSUMER_OFFSET, pRequestHeader); + callSignatureBeforeRequest(addr, request, sessionCredentials); + request.Encode(); + + unique_ptr response(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + + if (response) { + switch (response->getCode()) { + case SUCCESS_VALUE: { + QueryConsumerOffsetResponseHeader* responseHeader = + (QueryConsumerOffsetResponseHeader*)response->getCommandHeader(); + int64 consumerOffset = responseHeader->offset; + return consumerOffset; + } + default: + break; + } + THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), response->getCode()); + } + THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); + return -1; +} + +void MQClientAPIImpl::updateConsumerOffset(const string& addr, UpdateConsumerOffsetRequestHeader* pRequestHeader, + int timeoutMillis, const SessionCredentials& sessionCredentials) { + RemotingCommand request(UPDATE_CONSUMER_OFFSET, pRequestHeader); + callSignatureBeforeRequest(addr, request, sessionCredentials); + request.Encode(); + + unique_ptr response(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + + if (response) { + switch (response->getCode()) { + case SUCCESS_VALUE: { return; } + default: + break; + } + THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), response->getCode()); + } + THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); +} + +void MQClientAPIImpl::updateConsumerOffsetOneway(const string& addr, UpdateConsumerOffsetRequestHeader* pRequestHeader, + int timeoutMillis, const SessionCredentials& sessionCredentials) { + RemotingCommand request(UPDATE_CONSUMER_OFFSET, pRequestHeader); + callSignatureBeforeRequest(addr, request, sessionCredentials); + request.Encode(); + + m_pRemotingClient->invokeOneway(addr, request); +} + +void MQClientAPIImpl::consumerSendMessageBack(MQMessageExt& msg, const string& consumerGroup, int delayLevel, + int timeoutMillis, const SessionCredentials& sessionCredentials) { + ConsumerSendMsgBackRequestHeader* pRequestHeader = new ConsumerSendMsgBackRequestHeader(); + pRequestHeader->group = consumerGroup; + pRequestHeader->offset = msg.getCommitLogOffset(); + pRequestHeader->delayLevel = delayLevel; + + string addr = socketAddress2IPPort(msg.getStoreHost()); + RemotingCommand request(CONSUMER_SEND_MSG_BACK, pRequestHeader); + callSignatureBeforeRequest(addr, request, sessionCredentials); + request.Encode(); + + unique_ptr response(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + + if (response) { + switch (response->getCode()) { + case SUCCESS_VALUE: { return; } + default: + break; + } + THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), response->getCode()); + } + THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); +} + +void MQClientAPIImpl::lockBatchMQ(const string& addr, LockBatchRequestBody* requestBody, vector& mqs, + int timeoutMillis, const SessionCredentials& sessionCredentials) { + RemotingCommand request(LOCK_BATCH_MQ, NULL); + string body; + requestBody->Encode(body); + request.SetBody(body.data(), body.length()); + request.setMsgBody(body); + callSignatureBeforeRequest(addr, request, sessionCredentials); + request.Encode(); + + unique_ptr pResponse(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + + if (pResponse != NULL) { + if (((*(pResponse->GetBody())).getSize() == 0) || ((*(pResponse->GetBody())).getData() != NULL)) { + switch (pResponse->getCode()) { + case SUCCESS_VALUE: { + const MemoryBlock* pbody = pResponse->GetBody(); + if (pbody->getSize()) { + LockBatchResponseBody::Decode(pbody, mqs); + } + return; + } break; + default: + break; + } + THROW_MQEXCEPTION(MQBrokerException, pResponse->getRemark(), pResponse->getCode()); + } + } + THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); +} + +void MQClientAPIImpl::unlockBatchMQ(const string& addr, UnlockBatchRequestBody* requestBody, int timeoutMillis, + const SessionCredentials& sessionCredentials) { + RemotingCommand request(UNLOCK_BATCH_MQ, NULL); + string body; + requestBody->Encode(body); + request.SetBody(body.data(), body.length()); + request.setMsgBody(body); + callSignatureBeforeRequest(addr, request, sessionCredentials); + request.Encode(); + + unique_ptr pResponse(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + + if (pResponse != NULL) { + switch (pResponse->getCode()) { + case SUCCESS_VALUE: { + return; + } break; + default: + break; + } + THROW_MQEXCEPTION(MQBrokerException, pResponse->getRemark(), pResponse->getCode()); + } + THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); } //& cids, int timeoutMillis, - const SessionCredentials& sessionCredentials); + void unregisterClient(const string& addr, const string& clientID, const string& producerGroup, + const string& consumerGroup, const SessionCredentials& sessionCredentials); - int64 queryConsumerOffset(const string& addr, - QueryConsumerOffsetRequestHeader* pRequestHeader, - int timeoutMillis, - const SessionCredentials& sessionCredentials); + TopicRouteData* getTopicRouteInfoFromNameServer(const string& topic, int timeoutMillis, + const SessionCredentials& sessionCredentials); - void updateConsumerOffset(const string& addr, - UpdateConsumerOffsetRequestHeader* pRequestHeader, - int timeoutMillis, - const SessionCredentials& sessionCredentials); + TopicList* getTopicListFromNameServer(const SessionCredentials& sessionCredentials); - void updateConsumerOffsetOneway( - const string& addr, UpdateConsumerOffsetRequestHeader* pRequestHeader, - int timeoutMillis, const SessionCredentials& sessionCredentials); + int wipeWritePermOfBroker(const string& namesrvAddr, const string& brokerName, int timeoutMillis); - void consumerSendMessageBack(MQMessageExt& msg, const string& consumerGroup, - int delayLevel, int timeoutMillis, - const SessionCredentials& sessionCredentials); + void deleteTopicInBroker(const string& addr, const string& topic, int timeoutMillis); - void lockBatchMQ(const string& addr, LockBatchRequestBody* requestBody, - vector& mqs, int timeoutMillis, - const SessionCredentials& sessionCredentials); + void deleteTopicInNameServer(const string& addr, const string& topic, int timeoutMillis); - void unlockBatchMQ(const string& addr, UnlockBatchRequestBody* requestBody, - int timeoutMillis, - const SessionCredentials& sessionCredentials); + void deleteSubscriptionGroup(const string& addr, const string& groupName, int timeoutMillis); + + string getKVConfigByValue(const string& projectNamespace, const string& projectGroup, int timeoutMillis); + + KVTable getKVListByNamespace(const string& projectNamespace, int timeoutMillis); + + void deleteKVConfigByValue(const string& projectNamespace, const string& projectGroup, int timeoutMillis); + + SendResult processSendResponse(const string& brokerName, const MQMessage& msg, RemotingCommand* pResponse); + + PullResult* processPullResponse(RemotingCommand* pResponse); + + int64 getMinOffset(const string& addr, const string& topic, int queueId, int timeoutMillis, + const SessionCredentials& sessionCredentials); + + int64 getMaxOffset(const string& addr, const string& topic, int queueId, int timeoutMillis, + const SessionCredentials& sessionCredentials); + + int64 searchOffset(const string& addr, const string& topic, int queueId, uint64_t timestamp, int timeoutMillis, + const SessionCredentials& sessionCredentials); + + MQMessageExt* viewMessage(const string& addr, int64 phyoffset, int timeoutMillis, + const SessionCredentials& sessionCredentials); + + int64 getEarliestMsgStoretime(const string& addr, const string& topic, int queueId, int timeoutMillis, + const SessionCredentials& sessionCredentials); + + void getConsumerIdListByGroup(const string& addr, const string& consumerGroup, vector& cids, + int timeoutMillis, const SessionCredentials& sessionCredentials); + + int64 queryConsumerOffset(const string& addr, QueryConsumerOffsetRequestHeader* pRequestHeader, int timeoutMillis, + const SessionCredentials& sessionCredentials); + + void updateConsumerOffset(const string& addr, UpdateConsumerOffsetRequestHeader* pRequestHeader, int timeoutMillis, + const SessionCredentials& sessionCredentials); + + void updateConsumerOffsetOneway(const string& addr, UpdateConsumerOffsetRequestHeader* pRequestHeader, + int timeoutMillis, const SessionCredentials& sessionCredentials); + + void consumerSendMessageBack(MQMessageExt& msg, const string& consumerGroup, int delayLevel, int timeoutMillis, + const SessionCredentials& sessionCredentials); + + void lockBatchMQ(const string& addr, LockBatchRequestBody* requestBody, vector& mqs, + int timeoutMillis, const SessionCredentials& sessionCredentials); + + void unlockBatchMQ(const string& addr, UnlockBatchRequestBody* requestBody, int timeoutMillis, + const SessionCredentials& sessionCredentials); + + void sendMessageAsync(const string& addr, const string& brokerName, const MQMessage& msg, RemotingCommand& request, + SendCallback* pSendCallback, int64 timeoutMilliseconds, int maxRetryTimes = 1, + int retrySendTimes = 1); + void deleteOpaqueForDropPullRequest(const MQMessageQueue& mq, int opaque); + +private: + SendResult sendMessageSync(const string& addr, const string& brokerName, const MQMessage& msg, + RemotingCommand& request, int timeoutMillis); + /* + void sendMessageAsync(const string& addr, const string& brokerName, + const MQMessage& msg, RemotingCommand& request, + SendCallback* pSendCallback, int64 timeoutMilliseconds); + */ + PullResult* pullMessageSync(const string& addr, RemotingCommand& request, int timeoutMillis); + + void pullMessageAsync(const string& addr, RemotingCommand& request, int timeoutMillis, PullCallback* pullCallback, + void* pArg); - void sendMessageAsync(const string& addr, const string& brokerName, - const MQMessage& msg, RemotingCommand& request, - SendCallback* pSendCallback, int64 timeoutMilliseconds, - int maxRetryTimes=1, - int retrySendTimes=1); - void deleteOpaqueForDropPullRequest(const MQMessageQueue& mq, int opaque); - - private: - SendResult sendMessageSync(const string& addr, const string& brokerName, - const MQMessage& msg, RemotingCommand& request, - int timeoutMillis); - /* - void sendMessageAsync(const string& addr, const string& brokerName, - const MQMessage& msg, RemotingCommand& request, - SendCallback* pSendCallback, int64 timeoutMilliseconds); - */ - PullResult* pullMessageSync(const string& addr, RemotingCommand& request, - int timeoutMillis); - - void pullMessageAsync(const string& addr, RemotingCommand& request, - int timeoutMillis, PullCallback* pullCallback, - void* pArg); - - private: - unique_ptr m_pRemotingClient; - unique_ptr m_topAddressing; - string m_nameSrvAddr; - bool m_firstFetchNameSrv; - string m_mqClientId; +private: + unique_ptr m_pRemotingClient; + unique_ptr m_topAddressing; + string m_nameSrvAddr; + bool m_firstFetchNameSrv; + string m_mqClientId; }; } // pDefaultTopicInfo(new TopicPublishInfo()); - m_topicPublishInfoTable[DEFAULT_TOPIC] = pDefaultTopicInfo; - m_pClientRemotingProcessor.reset(new ClientRemotingProcessor(this)); - m_pClientAPIImpl.reset(new MQClientAPIImpl( - m_clientId, m_pClientRemotingProcessor.get(), pullThreadNum, - tcpConnectTimeout, tcpTransportTryLockTimeout, unitName)); - m_serviceState = CREATE_JUST; - LOG_DEBUG("MQClientFactory construct"); + m_clientId = clientID; + // default Topic register; + boost::shared_ptr pDefaultTopicInfo(new TopicPublishInfo()); + m_topicPublishInfoTable[DEFAULT_TOPIC] = pDefaultTopicInfo; + m_pClientRemotingProcessor.reset(new ClientRemotingProcessor(this)); + m_pClientAPIImpl.reset(new MQClientAPIImpl(m_clientId, m_pClientRemotingProcessor.get(), pullThreadNum, + tcpConnectTimeout, tcpTransportTryLockTimeout, unitName)); + m_serviceState = CREATE_JUST; + LOG_DEBUG("MQClientFactory construct"); } MQClientFactory::~MQClientFactory() { - LOG_INFO("MQClientFactory:%s destruct", m_clientId.c_str()); + LOG_INFO("MQClientFactory:%s destruct", m_clientId.c_str()); - for (TRDMAP::iterator itp = m_topicRouteTable.begin(); - itp != m_topicRouteTable.end(); ++itp) { - delete itp->second; - } + for (TRDMAP::iterator itp = m_topicRouteTable.begin(); itp != m_topicRouteTable.end(); ++itp) { + delete itp->second; + } - m_producerTable.clear(); - m_consumerTable.clear(); - m_topicRouteTable.clear(); - m_brokerAddrTable.clear(); - m_topicPublishInfoTable.clear(); + m_producerTable.clear(); + m_consumerTable.clear(); + m_topicRouteTable.clear(); + m_brokerAddrTable.clear(); + m_topicPublishInfoTable.clear(); - m_pClientAPIImpl = NULL; + m_pClientAPIImpl = NULL; } void MQClientFactory::start() { - switch (m_serviceState) { - case CREATE_JUST: - LOG_INFO("MQClientFactory:%s start", m_clientId.c_str()); - m_serviceState = START_FAILED; - // topicList; - //::iterator it = topicList.begin(); - for (; it != topicList.end(); ++it) { - updateTopicRouteInfoFromNameServer(*it, session_credentials); + switch (m_serviceState) { + case CREATE_JUST: + LOG_INFO("MQClientFactory:%s start", m_clientId.c_str()); + m_serviceState = START_FAILED; + //expires_from_now(t->expires_from_now() + boost::posix_time::seconds(30), - e); - t->async_wait( - boost::bind(&MQClientFactory::updateTopicRouteInfo, this, ec, t)); + set topicList; + //::iterator it = topicList.begin(); + for (; it != topicList.end(); ++it) { + updateTopicRouteInfoFromNameServer(*it, session_credentials); + } + } + + boost::system::error_code e; + t->expires_from_now(t->expires_from_now() + boost::posix_time::seconds(30), e); + t->async_wait(boost::bind(&MQClientFactory::updateTopicRouteInfo, this, ec, t)); } TopicRouteData* MQClientFactory::getTopicRouteData(const string& topic) { - boost::lock_guard lock(m_topicRouteTableMutex); - if (m_topicRouteTable.find(topic) != m_topicRouteTable.end()) { - return m_topicRouteTable[topic]; - } - return NULL; + boost::lock_guard lock(m_topicRouteTableMutex); + if (m_topicRouteTable.find(topic) != m_topicRouteTable.end()) { + return m_topicRouteTable[topic]; + } + return NULL; } -void MQClientFactory::addTopicRouteData(const string& topic, - TopicRouteData* pTopicRouteData) { - boost::lock_guard lock(m_topicRouteTableMutex); - if (m_topicRouteTable.find(topic) != m_topicRouteTable.end()) { - delete m_topicRouteTable[topic]; - m_topicRouteTable.erase(topic); - } - m_topicRouteTable[topic] = pTopicRouteData; +void MQClientFactory::addTopicRouteData(const string& topic, TopicRouteData* pTopicRouteData) { + boost::lock_guard lock(m_topicRouteTableMutex); + if (m_topicRouteTable.find(topic) != m_topicRouteTable.end()) { + delete m_topicRouteTable[topic]; + m_topicRouteTable.erase(topic); + } + m_topicRouteTable[topic] = pTopicRouteData; } boost::shared_ptr MQClientFactory::tryToFindTopicPublishInfo( const string& topic, const SessionCredentials& session_credentials) { - boost::lock_guard lock( - m_topicPublishInfoLock); // add topicPublishInfoLock to avoid con-current - // excuting updateTopicRouteInfoFromNameServer - // when producer send msg before topicRouteInfo - // was got; - if (!isTopicInfoValidInTable(topic)) { - updateTopicRouteInfoFromNameServer(topic, session_credentials); - } - // pTopicPublishInfo; - return pTopicPublishInfo; - } + boost::lock_guard lock(m_topicPublishInfoLock); // add topicPublishInfoLock to avoid con-current + // excuting updateTopicRouteInfoFromNameServer + // when producer send msg before topicRouteInfo + // was got; + if (!isTopicInfoValidInTable(topic)) { + updateTopicRouteInfoFromNameServer(topic, session_credentials); + } + // pTopicPublishInfo; + return pTopicPublishInfo; + } -bool MQClientFactory::updateTopicRouteInfoFromNameServer( - const string& topic, const SessionCredentials& session_credentials, - bool isDefault /* = false */) { - boost::lock_guard lock(m_factoryLock); - unique_ptr pTopicRouteData; - LOG_INFO("updateTopicRouteInfoFromNameServer start:%s", topic.c_str()); + return getTopicPublishInfoFromTable(topic); +} + +bool MQClientFactory::updateTopicRouteInfoFromNameServer(const string& topic, + const SessionCredentials& session_credentials, + bool isDefault /* = false */) { + boost::lock_guard lock(m_factoryLock); + unique_ptr pTopicRouteData; + LOG_INFO("updateTopicRouteInfoFromNameServer start:%s", topic.c_str()); + + if (isDefault) { + pTopicRouteData.reset( + m_pClientAPIImpl->getTopicRouteInfoFromNameServer(DEFAULT_TOPIC, 1000 * 5, session_credentials)); + if (pTopicRouteData != NULL) { + vector& queueDatas = pTopicRouteData->getQueueDatas(); + vector::iterator it = queueDatas.begin(); + for (; it != queueDatas.end(); ++it) { + // ¶Ãô·ÖÇø¸öÊýÊÇÒ»Ö£¬¹ÊÖ»×öÒ»´ÎÅöÃ; + int queueNums = std::min(4, it->readQueueNums); + it->readQueueNums = queueNums; + it->writeQueueNums = queueNums; + } + } + } else { + pTopicRouteData.reset(m_pClientAPIImpl->getTopicRouteInfoFromNameServer(topic, 1000 * 5, session_credentials)); + } - if (isDefault) { - pTopicRouteData.reset(m_pClientAPIImpl->getTopicRouteInfoFromNameServer( - DEFAULT_TOPIC, 1000 * 5, session_credentials)); if (pTopicRouteData != NULL) { - vector& queueDatas = pTopicRouteData->getQueueDatas(); - vector::iterator it = queueDatas.begin(); - for (; it != queueDatas.end(); ++it) { - // ¶Ãô·ÖÇø¸öÊýÊÇÒ»Ö£¬¹ÊÖ»×öÒ»´ÎÅöÃ; - int queueNums = std::min(4, it->readQueueNums); - it->readQueueNums = queueNums; - it->writeQueueNums = queueNums; - } - } - } else { - pTopicRouteData.reset(m_pClientAPIImpl->getTopicRouteInfoFromNameServer( - topic, 1000 * 5, session_credentials)); - } - - if (pTopicRouteData != NULL) { - LOG_INFO("updateTopicRouteInfoFromNameServer has data"); - TopicRouteData* pTemp = getTopicRouteData(topic); - bool changed = true; - if (pTemp != NULL) { - changed = !(*pTemp == *pTopicRouteData); + LOG_INFO("updateTopicRouteInfoFromNameServer has data"); + TopicRouteData* pTemp = getTopicRouteData(topic); + bool changed = true; + if (pTemp != NULL) { + changed = !(*pTemp == *pTopicRouteData); + } + + if (getConsumerTableSize() > 0) { + vector mqs; + topicRouteData2TopicSubscribeInfo(topic, pTopicRouteData.get(), mqs); + updateConsumerSubscribeTopicInfo(topic, mqs); + } + + if (changed) { + // brokerList = pTopicRouteData->getBrokerDatas(); + vector::iterator it = brokerList.begin(); + for (; it != brokerList.end(); ++it) { + LOG_INFO("updateTopicRouteInfoFromNameServer changed with broker name:%s", (*it).brokerName.c_str()); + addBrokerToAddrMap((*it).brokerName, (*it).brokerAddrs); + } + + // publishInfo( + topicRouteData2TopicPublishInfo(topic, pTopicRouteData.get())); + addTopicInfoToTable(topic, publishInfo); // erase first, then add + } + + // 0) { - vector mqs; - topicRouteData2TopicSubscribeInfo(topic, pTopicRouteData.get(), mqs); - updateConsumerSubscribeTopicInfo(topic, mqs); - } - - if (changed) { - // brokerList = pTopicRouteData->getBrokerDatas(); - vector::iterator it = brokerList.begin(); - for (; it != brokerList.end(); ++it) { - LOG_INFO( - "updateTopicRouteInfoFromNameServer changed with broker name:%s", - (*it).brokerName.c_str()); - addBrokerToAddrMap((*it).brokerName, (*it).brokerAddrs); - } - - // publishInfo( - topicRouteData2TopicPublishInfo(topic, pTopicRouteData.get())); - addTopicInfoToTable(topic, publishInfo); // erase first, then add - } - - // -MQClientFactory::topicRouteData2TopicPublishInfo(const string& topic, - TopicRouteData* pRoute) { - boost::shared_ptr info(new TopicPublishInfo()); - string OrderTopicConf = pRoute->getOrderTopicConf(); - // brokers; - UtilAll::Split(brokers, OrderTopicConf, ';'); - for (size_t i = 0; i < brokers.size(); i++) { - vector item; - UtilAll::Split(item, brokers[i], ':'); - int nums = atoi(item[1].c_str()); - for (int i = 0; i < nums; i++) { - MQMessageQueue mq(topic, item[0], i); - info->updateMessageQueueList(mq); - } - } - } - //& queueDatas = pRoute->getQueueDatas(); - vector::iterator it = queueDatas.begin(); - for (; it != queueDatas.end(); ++it) { - QueueData& qd = (*it); - if (PermName::isWriteable(qd.perm)) { - string addr = findBrokerAddressInPublish(qd.brokerName); - if (addr.empty()) { - continue; +boost::shared_ptr MQClientFactory::topicRouteData2TopicPublishInfo(const string& topic, + TopicRouteData* pRoute) { + boost::shared_ptr info(new TopicPublishInfo()); + string OrderTopicConf = pRoute->getOrderTopicConf(); + // brokers; + UtilAll::Split(brokers, OrderTopicConf, ';'); + for (size_t i = 0; i < brokers.size(); i++) { + vector item; + UtilAll::Split(item, brokers[i], ':'); + int nums = atoi(item[1].c_str()); + for (int i = 0; i < nums; i++) { + MQMessageQueue mq(topic, item[0], i); + info->updateMessageQueueList(mq); + } } - for (int i = 0; i < qd.writeQueueNums; i++) { - MQMessageQueue mq(topic, qd.brokerName, i); - info->updateMessageQueueList(mq); + } + //& queueDatas = pRoute->getQueueDatas(); + vector::iterator it = queueDatas.begin(); + for (; it != queueDatas.end(); ++it) { + QueueData& qd = (*it); + if (PermName::isWriteable(qd.perm)) { + string addr = findBrokerAddressInPublish(qd.brokerName); + if (addr.empty()) { + continue; + } + for (int i = 0; i < qd.writeQueueNums; i++) { + MQMessageQueue mq(topic, qd.brokerName, i); + info->updateMessageQueueList(mq); + } + } } - } } - } - return info; + return info; } -void MQClientFactory::topicRouteData2TopicSubscribeInfo( - const string& topic, TopicRouteData* pRoute, vector& mqs) { - mqs.clear(); - vector& queueDatas = pRoute->getQueueDatas(); - vector::iterator it = queueDatas.begin(); - for (; it != queueDatas.end(); ++it) { - QueueData& qd = (*it); - if (PermName::isReadable(qd.perm)) { - for (int i = 0; i < qd.readQueueNums; i++) { - MQMessageQueue mq(topic, qd.brokerName, i); - mqs.push_back(mq); - } +void MQClientFactory::topicRouteData2TopicSubscribeInfo(const string& topic, TopicRouteData* pRoute, + vector& mqs) { + mqs.clear(); + vector& queueDatas = pRoute->getQueueDatas(); + vector::iterator it = queueDatas.begin(); + for (; it != queueDatas.end(); ++it) { + QueueData& qd = (*it); + if (PermName::isReadable(qd.perm)) { + for (int i = 0; i < qd.readQueueNums; i++) { + MQMessageQueue mq(topic, qd.brokerName, i); + mqs.push_back(mq); + } + } } - } } void MQClientFactory::shutdown() { - if (getConsumerTableSize() != 0) return; - - if (getProducerTableSize() != 0) return; - - switch (m_serviceState) { - case RUNNING: { - //interrupt(); - m_consumer_async_service_thread->join(); - } - m_async_ioService.stop(); - m_async_service_thread->interrupt(); - m_async_service_thread->join(); - m_pClientAPIImpl->stopAllTcpTransportThread(); // Note: stop all - // TcpTransport Threads - // and release all - // responseFuture - // conditions - m_serviceState = SHUTDOWN_ALREADY; - LOG_INFO("MQClientFactory:%s shutdown", m_clientId.c_str()); - break; - } - case SHUTDOWN_ALREADY: - case CREATE_JUST: - break; - default: - break; - } - - //removeClientFactory(m_clientId); + if (getConsumerTableSize() != 0) return; + + if (getProducerTableSize() != 0) return; + + switch (m_serviceState) { + case RUNNING: { + //interrupt(); + m_consumer_async_service_thread->join(); + } + m_async_ioService.stop(); + m_async_service_thread->interrupt(); + m_async_service_thread->join(); + m_pClientAPIImpl->stopAllTcpTransportThread(); // Note: stop all + // TcpTransport Threads + // and release all + // responseFuture + // conditions + m_serviceState = SHUTDOWN_ALREADY; + LOG_INFO("MQClientFactory:%s shutdown", m_clientId.c_str()); + break; + } + case SHUTDOWN_ALREADY: + case CREATE_JUST: + break; + default: + break; + } + + //removeClientFactory(m_clientId); } bool MQClientFactory::registerProducer(MQProducer* pProducer) { - string groupName = pProducer->getGroupName(); - string namesrvaddr = pProducer->getNamesrvAddr(); - if (groupName.empty()) { - return false; - } + string groupName = pProducer->getGroupName(); + string namesrvaddr = pProducer->getNamesrvAddr(); + if (groupName.empty()) { + return false; + } - if (!addProducerToTable(groupName, pProducer)) { - return false; - } - - LOG_DEBUG("registerProducer success:%s", groupName.c_str()); - //getNamesrvDomain()); - if (!nameSrvDomain.empty()) m_nameSrvDomain = nameSrvDomain; - pProducer->setNamesrvAddr( - m_pClientAPIImpl->fetchNameServerAddr(m_nameSrvDomain)); - } else { - m_bFetchNSService = false; - m_pClientAPIImpl->updateNameServerAddr(namesrvaddr); - LOG_INFO("user specfied name server address: %s", namesrvaddr.c_str()); - } - return true; + if (!addProducerToTable(groupName, pProducer)) { + return false; + } + + LOG_DEBUG("registerProducer success:%s", groupName.c_str()); + //getNamesrvDomain()); + if (!nameSrvDomain.empty()) m_nameSrvDomain = nameSrvDomain; + pProducer->setNamesrvAddr(m_pClientAPIImpl->fetchNameServerAddr(m_nameSrvDomain)); + } else { + m_bFetchNSService = false; + m_pClientAPIImpl->updateNameServerAddr(namesrvaddr); + LOG_INFO("user specfied name server address: %s", namesrvaddr.c_str()); + } + return true; } void MQClientFactory::unregisterProducer(MQProducer* pProducer) { - string groupName = pProducer->getGroupName(); - unregisterClient(groupName, "", pProducer->getSessionCredentials()); + string groupName = pProducer->getGroupName(); + unregisterClient(groupName, "", pProducer->getSessionCredentials()); - eraseProducerFromTable(groupName); + eraseProducerFromTable(groupName); } bool MQClientFactory::registerConsumer(MQConsumer* pConsumer) { - string groupName = pConsumer->getGroupName(); - string namesrvaddr = pConsumer->getNamesrvAddr(); - if (groupName.empty()) { - return false; - } + string groupName = pConsumer->getGroupName(); + string namesrvaddr = pConsumer->getNamesrvAddr(); + if (groupName.empty()) { + return false; + } - if (!addConsumerToTable(groupName, pConsumer)) { - return false; - } - LOG_DEBUG("registerConsumer success:%s", groupName.c_str()); - //getNamesrvDomain()); - if (!nameSrvDomain.empty()) m_nameSrvDomain = nameSrvDomain; - pConsumer->setNamesrvAddr( - m_pClientAPIImpl->fetchNameServerAddr(m_nameSrvDomain)); - } else { - m_bFetchNSService = false; - m_pClientAPIImpl->updateNameServerAddr(namesrvaddr); - LOG_INFO("user specfied name server address: %s", namesrvaddr.c_str()); - } - - return true; + if (!addConsumerToTable(groupName, pConsumer)) { + return false; + } + LOG_DEBUG("registerConsumer success:%s", groupName.c_str()); + //getNamesrvDomain()); + if (!nameSrvDomain.empty()) m_nameSrvDomain = nameSrvDomain; + pConsumer->setNamesrvAddr(m_pClientAPIImpl->fetchNameServerAddr(m_nameSrvDomain)); + } else { + m_bFetchNSService = false; + m_pClientAPIImpl->updateNameServerAddr(namesrvaddr); + LOG_INFO("user specfied name server address: %s", namesrvaddr.c_str()); + } + + return true; } void MQClientFactory::unregisterConsumer(MQConsumer* pConsumer) { - string groupName = pConsumer->getGroupName(); - unregisterClient("", groupName, pConsumer->getSessionCredentials()); + string groupName = pConsumer->getGroupName(); + unregisterClient("", groupName, pConsumer->getSessionCredentials()); - eraseConsumerFromTable(groupName); + eraseConsumerFromTable(groupName); } MQProducer* MQClientFactory::selectProducer(const string& producerName) { - boost::lock_guard lock(m_producerTableMutex); - if (m_producerTable.find(producerName) != m_producerTable.end()) { - return m_producerTable[producerName]; - } - return NULL; + boost::lock_guard lock(m_producerTableMutex); + if (m_producerTable.find(producerName) != m_producerTable.end()) { + return m_producerTable[producerName]; + } + return NULL; } -bool MQClientFactory::getSessionCredentialFromProducerTable( - SessionCredentials& sessionCredentials) { - boost::lock_guard lock(m_producerTableMutex); - for (MQPMAP::iterator it = m_producerTable.begin(); - it != m_producerTable.end(); ++it) { - if (it->second) sessionCredentials = it->second->getSessionCredentials(); - } +bool MQClientFactory::getSessionCredentialFromProducerTable(SessionCredentials& sessionCredentials) { + boost::lock_guard lock(m_producerTableMutex); + for (MQPMAP::iterator it = m_producerTable.begin(); it != m_producerTable.end(); ++it) { + if (it->second) sessionCredentials = it->second->getSessionCredentials(); + } - if (sessionCredentials.isValid()) return true; + if (sessionCredentials.isValid()) return true; - return false; + return false; } -bool MQClientFactory::addProducerToTable(const string& producerName, - MQProducer* pMQProducer) { - boost::lock_guard lock(m_producerTableMutex); - if (m_producerTable.find(producerName) != m_producerTable.end()) return false; - m_producerTable[producerName] = pMQProducer; - return true; +bool MQClientFactory::addProducerToTable(const string& producerName, MQProducer* pMQProducer) { + boost::lock_guard lock(m_producerTableMutex); + if (m_producerTable.find(producerName) != m_producerTable.end()) return false; + m_producerTable[producerName] = pMQProducer; + return true; } void MQClientFactory::eraseProducerFromTable(const string& producerName) { - boost::lock_guard lock(m_producerTableMutex); - if (m_producerTable.find(producerName) != m_producerTable.end()) - m_producerTable.erase(producerName); + boost::lock_guard lock(m_producerTableMutex); + if (m_producerTable.find(producerName) != m_producerTable.end()) m_producerTable.erase(producerName); } int MQClientFactory::getProducerTableSize() { - boost::lock_guard lock(m_producerTableMutex); - return m_producerTable.size(); + boost::lock_guard lock(m_producerTableMutex); + return m_producerTable.size(); } -void MQClientFactory::insertProducerInfoToHeartBeatData( - HeartbeatData* pHeartbeatData) { - boost::lock_guard lock(m_producerTableMutex); - for (MQPMAP::iterator it = m_producerTable.begin(); - it != m_producerTable.end(); ++it) { - ProducerData producerData; - producerData.groupName = it->first; - pHeartbeatData->insertDataToProducerDataSet(producerData); - } +void MQClientFactory::insertProducerInfoToHeartBeatData(HeartbeatData* pHeartbeatData) { + boost::lock_guard lock(m_producerTableMutex); + for (MQPMAP::iterator it = m_producerTable.begin(); it != m_producerTable.end(); ++it) { + ProducerData producerData; + producerData.groupName = it->first; + pHeartbeatData->insertDataToProducerDataSet(producerData); + } } MQConsumer* MQClientFactory::selectConsumer(const string& group) { - boost::lock_guard lock(m_consumerTableMutex); - if (m_consumerTable.find(group) != m_consumerTable.end()) { - return m_consumerTable[group]; - } - return NULL; + boost::lock_guard lock(m_consumerTableMutex); + if (m_consumerTable.find(group) != m_consumerTable.end()) { + return m_consumerTable[group]; + } + return NULL; } -bool MQClientFactory::getSessionCredentialFromConsumerTable( - SessionCredentials& sessionCredentials) { - boost::lock_guard lock(m_consumerTableMutex); - for (MQCMAP::iterator it = m_consumerTable.begin(); - it != m_consumerTable.end(); ++it) { - if (it->second) sessionCredentials = it->second->getSessionCredentials(); - } +bool MQClientFactory::getSessionCredentialFromConsumerTable(SessionCredentials& sessionCredentials) { + boost::lock_guard lock(m_consumerTableMutex); + for (MQCMAP::iterator it = m_consumerTable.begin(); it != m_consumerTable.end(); ++it) { + if (it->second) sessionCredentials = it->second->getSessionCredentials(); + } - if (sessionCredentials.isValid()) return true; + if (sessionCredentials.isValid()) return true; - return false; + return false; } -bool MQClientFactory::getSessionCredentialFromConsumer( - const string& consumerGroup, SessionCredentials& sessionCredentials) { - boost::lock_guard lock(m_consumerTableMutex); - if (m_consumerTable.find(consumerGroup) != m_consumerTable.end()) { - sessionCredentials = - m_consumerTable[consumerGroup]->getSessionCredentials(); - } +bool MQClientFactory::getSessionCredentialFromConsumer(const string& consumerGroup, + SessionCredentials& sessionCredentials) { + boost::lock_guard lock(m_consumerTableMutex); + if (m_consumerTable.find(consumerGroup) != m_consumerTable.end()) { + sessionCredentials = m_consumerTable[consumerGroup]->getSessionCredentials(); + } - if (sessionCredentials.isValid()) return true; + if (sessionCredentials.isValid()) return true; - return false; + return false; } -bool MQClientFactory::addConsumerToTable(const string& consumerName, - MQConsumer* pMQConsumer) { - boost::lock_guard lock(m_consumerTableMutex); - if (m_consumerTable.find(consumerName) != m_consumerTable.end()) return false; - m_consumerTable[consumerName] = pMQConsumer; - return true; +bool MQClientFactory::addConsumerToTable(const string& consumerName, MQConsumer* pMQConsumer) { + boost::lock_guard lock(m_consumerTableMutex); + if (m_consumerTable.find(consumerName) != m_consumerTable.end()) return false; + m_consumerTable[consumerName] = pMQConsumer; + return true; } void MQClientFactory::eraseConsumerFromTable(const string& consumerName) { - boost::lock_guard lock(m_consumerTableMutex); - if (m_consumerTable.find(consumerName) != m_consumerTable.end()) - m_consumerTable.erase(consumerName); // do not need freee pConsumer, as it - // was allocated by user - else - LOG_WARN("could not find consumer:%s from table", consumerName.c_str()); + boost::lock_guard lock(m_consumerTableMutex); + if (m_consumerTable.find(consumerName) != m_consumerTable.end()) + m_consumerTable.erase(consumerName); // do not need freee pConsumer, as it + // was allocated by user + else + LOG_WARN("could not find consumer:%s from table", consumerName.c_str()); } int MQClientFactory::getConsumerTableSize() { - boost::lock_guard lock(m_consumerTableMutex); - return m_consumerTable.size(); + boost::lock_guard lock(m_consumerTableMutex); + return m_consumerTable.size(); } -void MQClientFactory::getTopicListFromConsumerSubscription( - set& topicList) { - boost::lock_guard lock(m_consumerTableMutex); - for (MQCMAP::iterator it = m_consumerTable.begin(); - it != m_consumerTable.end(); ++it) { - vector result; - it->second->getSubscriptions(result); +void MQClientFactory::getTopicListFromConsumerSubscription(set& topicList) { + boost::lock_guard lock(m_consumerTableMutex); + for (MQCMAP::iterator it = m_consumerTable.begin(); it != m_consumerTable.end(); ++it) { + vector result; + it->second->getSubscriptions(result); - vector::iterator iter = result.begin(); - for (; iter != result.end(); ++iter) { - topicList.insert((*iter).getTopic()); + vector::iterator iter = result.begin(); + for (; iter != result.end(); ++iter) { + topicList.insert((*iter).getTopic()); + } } - } } -void MQClientFactory::updateConsumerSubscribeTopicInfo( - const string& topic, vector mqs) { - boost::lock_guard lock(m_consumerTableMutex); - for (MQCMAP::iterator it = m_consumerTable.begin(); - it != m_consumerTable.end(); ++it) { - it->second->updateTopicSubscribeInfo(topic, mqs); - } +void MQClientFactory::updateConsumerSubscribeTopicInfo(const string& topic, vector mqs) { + boost::lock_guard lock(m_consumerTableMutex); + for (MQCMAP::iterator it = m_consumerTable.begin(); it != m_consumerTable.end(); ++it) { + it->second->updateTopicSubscribeInfo(topic, mqs); + } } -void MQClientFactory::insertConsumerInfoToHeartBeatData( - HeartbeatData* pHeartbeatData) { - boost::lock_guard lock(m_consumerTableMutex); - for (MQCMAP::iterator it = m_consumerTable.begin(); - it != m_consumerTable.end(); ++it) { - MQConsumer* pConsumer = it->second; - ConsumerData consumerData; - consumerData.groupName = pConsumer->getGroupName(); - consumerData.consumeType = pConsumer->getConsumeType(); - consumerData.messageModel = pConsumer->getMessageModel(); - consumerData.consumeFromWhere = pConsumer->getConsumeFromWhere(); - - // result; - pConsumer->getSubscriptions(result); - consumerData.subscriptionDataSet.swap(result); - - pHeartbeatData->insertDataToConsumerDataSet(consumerData); - } +void MQClientFactory::insertConsumerInfoToHeartBeatData(HeartbeatData* pHeartbeatData) { + boost::lock_guard lock(m_consumerTableMutex); + for (MQCMAP::iterator it = m_consumerTable.begin(); it != m_consumerTable.end(); ++it) { + MQConsumer* pConsumer = it->second; + ConsumerData consumerData; + consumerData.groupName = pConsumer->getGroupName(); + consumerData.consumeType = pConsumer->getConsumeType(); + consumerData.messageModel = pConsumer->getMessageModel(); + consumerData.consumeFromWhere = pConsumer->getConsumeFromWhere(); + + // result; + pConsumer->getSubscriptions(result); + consumerData.subscriptionDataSet.swap(result); + + pHeartbeatData->insertDataToConsumerDataSet(consumerData); + } } -void MQClientFactory::addTopicInfoToTable( - const string& topic, - boost::shared_ptr pTopicPublishInfo) { - boost::lock_guard lock(m_topicPublishInfoTableMutex); - if (m_topicPublishInfoTable.find(topic) != m_topicPublishInfoTable.end()) { - m_topicPublishInfoTable.erase(topic); - } - m_topicPublishInfoTable[topic] = pTopicPublishInfo; +void MQClientFactory::addTopicInfoToTable(const string& topic, boost::shared_ptr pTopicPublishInfo) { + boost::lock_guard lock(m_topicPublishInfoTableMutex); + if (m_topicPublishInfoTable.find(topic) != m_topicPublishInfoTable.end()) { + m_topicPublishInfoTable.erase(topic); + } + m_topicPublishInfoTable[topic] = pTopicPublishInfo; } void MQClientFactory::eraseTopicInfoFromTable(const string& topic) { - boost::lock_guard lock(m_topicPublishInfoTableMutex); - if (m_topicPublishInfoTable.find(topic) != m_topicPublishInfoTable.end()) { - m_topicPublishInfoTable.erase(topic); - } + boost::lock_guard lock(m_topicPublishInfoTableMutex); + if (m_topicPublishInfoTable.find(topic) != m_topicPublishInfoTable.end()) { + m_topicPublishInfoTable.erase(topic); + } } bool MQClientFactory::isTopicInfoValidInTable(const string& topic) { - boost::lock_guard lock(m_topicPublishInfoTableMutex); - if (m_topicPublishInfoTable.find(topic) != m_topicPublishInfoTable.end()) { - if (m_topicPublishInfoTable[topic]->ok()) return true; - } - return false; + boost::lock_guard lock(m_topicPublishInfoTableMutex); + if (m_topicPublishInfoTable.find(topic) != m_topicPublishInfoTable.end()) { + if (m_topicPublishInfoTable[topic]->ok()) return true; + } + return false; } -boost::shared_ptr -MQClientFactory::getTopicPublishInfoFromTable(const string& topic) { - boost::lock_guard lock(m_topicPublishInfoTableMutex); - if (m_topicPublishInfoTable.find(topic) != m_topicPublishInfoTable.end()) { - return m_topicPublishInfoTable[topic]; - } - boost::shared_ptr pTopicPublishInfo; - return pTopicPublishInfo; +boost::shared_ptr MQClientFactory::getTopicPublishInfoFromTable(const string& topic) { + boost::lock_guard lock(m_topicPublishInfoTableMutex); + if (m_topicPublishInfoTable.find(topic) != m_topicPublishInfoTable.end()) { + return m_topicPublishInfoTable[topic]; + } + boost::shared_ptr pTopicPublishInfo; + return pTopicPublishInfo; } void MQClientFactory::getTopicListFromTopicPublishInfo(set& topicList) { - boost::lock_guard lock(m_topicPublishInfoTableMutex); - for (TPMap::iterator itp = m_topicPublishInfoTable.begin(); - itp != m_topicPublishInfoTable.end(); ++itp) { - topicList.insert(itp->first); - } + boost::lock_guard lock(m_topicPublishInfoTableMutex); + for (TPMap::iterator itp = m_topicPublishInfoTable.begin(); itp != m_topicPublishInfoTable.end(); ++itp) { + topicList.insert(itp->first); + } } void MQClientFactory::clearBrokerAddrMap() { - boost::lock_guard lock(m_brokerAddrlock); - m_brokerAddrTable.clear(); + boost::lock_guard lock(m_brokerAddrlock); + m_brokerAddrTable.clear(); } -void MQClientFactory::addBrokerToAddrMap(const string& brokerName, - map& brokerAddrs) { - boost::lock_guard lock(m_brokerAddrlock); - if (m_brokerAddrTable.find(brokerName) != m_brokerAddrTable.end()) { - m_brokerAddrTable.erase(brokerName); - } - m_brokerAddrTable[brokerName] = brokerAddrs; +void MQClientFactory::addBrokerToAddrMap(const string& brokerName, map& brokerAddrs) { + boost::lock_guard lock(m_brokerAddrlock); + if (m_brokerAddrTable.find(brokerName) != m_brokerAddrTable.end()) { + m_brokerAddrTable.erase(brokerName); + } + m_brokerAddrTable[brokerName] = brokerAddrs; } MQClientFactory::BrokerAddrMAP MQClientFactory::getBrokerAddrMap() { - boost::lock_guard lock(m_brokerAddrlock); - return m_brokerAddrTable; + boost::lock_guard lock(m_brokerAddrlock); + return m_brokerAddrTable; } string MQClientFactory::findBrokerAddressInPublish(const string& brokerName) { - /*reslove the concurrent access m_brokerAddrTable by - findBrokerAddressInPublish(called by sendKernlImpl) And - sendHeartbeatToAllBroker, which leads hign RT of sendMsg - 1. change m_brokerAddrTable from hashMap to map; - 2. do not add m_factoryLock here, but copy m_brokerAddrTable, - this is used to avoid con-current access m_factoryLock by - findBrokerAddressInPublish(called by sendKernlImpl) And - updateTopicRouteInfoFromNameServer - - Note: after copying m_brokerAddrTable, updateTopicRouteInfoFromNameServer - modify m_brokerAddrTable imediatly, - after 1st send fail, producer will get topicPushlibshInfo again - before next try, so 2nd try will get correct broker to send ms; - */ - BrokerAddrMAP brokerTable(getBrokerAddrMap()); - string brokerAddr; - bool found = false; - - if (brokerTable.find(brokerName) != brokerTable.end()) { - map brokerMap(brokerTable[brokerName]); - map::iterator it1 = brokerMap.find(MASTER_ID); - if (it1 != brokerMap.end()) { - brokerAddr = it1->second; - found = true; - } - } - - brokerTable.clear(); - if (found) return brokerAddr; - - return ""; -} - -FindBrokerResult *MQClientFactory::findBrokerAddressInSubscribe(const string &brokerName, - int brokerId, + /*reslove the concurrent access m_brokerAddrTable by + findBrokerAddressInPublish(called by sendKernlImpl) And + sendHeartbeatToAllBroker, which leads hign RT of sendMsg + 1. change m_brokerAddrTable from hashMap to map; + 2. do not add m_factoryLock here, but copy m_brokerAddrTable, + this is used to avoid con-current access m_factoryLock by + findBrokerAddressInPublish(called by sendKernlImpl) And + updateTopicRouteInfoFromNameServer + + Note: after copying m_brokerAddrTable, updateTopicRouteInfoFromNameServer + modify m_brokerAddrTable imediatly, + after 1st send fail, producer will get topicPushlibshInfo again + before next try, so 2nd try will get correct broker to send ms; + */ + BrokerAddrMAP brokerTable(getBrokerAddrMap()); + string brokerAddr; + bool found = false; + + if (brokerTable.find(brokerName) != brokerTable.end()) { + map brokerMap(brokerTable[brokerName]); + map::iterator it1 = brokerMap.find(MASTER_ID); + if (it1 != brokerMap.end()) { + brokerAddr = it1->second; + found = true; + } + } + + brokerTable.clear(); + if (found) return brokerAddr; + + return ""; +} + +FindBrokerResult* MQClientFactory::findBrokerAddressInSubscribe(const string& brokerName, int brokerId, bool onlyThisBroker) { string brokerAddr; bool slave = false; @@ -665,502 +627,453 @@ FindBrokerResult *MQClientFactory::findBrokerAddressInSubscribe(const string &br return nullptr; } -FindBrokerResult* MQClientFactory::findBrokerAddressInAdmin( - const string& brokerName) { - BrokerAddrMAP brokerTable(getBrokerAddrMap()); - bool found = false; - bool slave = false; - string brokerAddr; +FindBrokerResult* MQClientFactory::findBrokerAddressInAdmin(const string& brokerName) { + BrokerAddrMAP brokerTable(getBrokerAddrMap()); + bool found = false; + bool slave = false; + string brokerAddr; - if (brokerTable.find(brokerName) != brokerTable.end()) { - map brokerMap(brokerTable[brokerName]); - map::iterator it1 = brokerMap.begin(); - if (it1 != brokerMap.end()) { - slave = (it1->first != MASTER_ID); - found = true; - brokerAddr = it1->second; + if (brokerTable.find(brokerName) != brokerTable.end()) { + map brokerMap(brokerTable[brokerName]); + map::iterator it1 = brokerMap.begin(); + if (it1 != brokerMap.end()) { + slave = (it1->first != MASTER_ID); + found = true; + brokerAddr = it1->second; + } } - } - brokerTable.clear(); - if (found) return new FindBrokerResult(brokerAddr, slave); + brokerTable.clear(); + if (found) return new FindBrokerResult(brokerAddr, slave); - return NULL; + return NULL; } -MQClientAPIImpl* MQClientFactory::getMQClientAPIImpl() const { - return m_pClientAPIImpl.get(); -} +MQClientAPIImpl* MQClientFactory::getMQClientAPIImpl() const { return m_pClientAPIImpl.get(); } void MQClientFactory::sendHeartbeatToAllBroker() { - BrokerAddrMAP brokerTable(getBrokerAddrMap()); - if (brokerTable.size() == 0) { - LOG_WARN("sendheartbeat brokeradd is empty"); - return; - } - - unique_ptr heartbeatData(prepareHeartbeatData()); - bool producerEmpty = heartbeatData->isProducerDataSetEmpty(); - bool consumerEmpty = heartbeatData->isConsumerDataSetEmpty(); - if (producerEmpty && consumerEmpty) { - LOG_WARN("sendheartbeat heartbeatData empty"); + BrokerAddrMAP brokerTable(getBrokerAddrMap()); + if (brokerTable.size() == 0) { + LOG_WARN("sendheartbeat brokeradd is empty"); + return; + } + + unique_ptr heartbeatData(prepareHeartbeatData()); + bool producerEmpty = heartbeatData->isProducerDataSetEmpty(); + bool consumerEmpty = heartbeatData->isConsumerDataSetEmpty(); + if (producerEmpty && consumerEmpty) { + LOG_WARN("sendheartbeat heartbeatData empty"); + brokerTable.clear(); + return; + } + + SessionCredentials session_credentials; + getSessionCredentialsFromOneOfProducerOrConsumer(session_credentials); + for (BrokerAddrMAP::iterator it = brokerTable.begin(); it != brokerTable.end(); ++it) { + map brokerMap(it->second); + map::iterator it1 = brokerMap.begin(); + for (; it1 != brokerMap.end(); ++it1) { + string& addr = it1->second; + if (consumerEmpty && it1->first != MASTER_ID) continue; + + try { + m_pClientAPIImpl->sendHearbeat(addr, heartbeatData.get(), session_credentials); + } + catch (MQException& e) { + LOG_ERROR(e.what()); + } + } + } brokerTable.clear(); - return; - } - - SessionCredentials session_credentials; - getSessionCredentialsFromOneOfProducerOrConsumer(session_credentials); - for (BrokerAddrMAP::iterator it = brokerTable.begin(); - it != brokerTable.end(); ++it) { - map brokerMap(it->second); - map::iterator it1 = brokerMap.begin(); - for (; it1 != brokerMap.end(); ++it1) { - string& addr = it1->second; - if (consumerEmpty && it1->first != MASTER_ID) continue; - - try { - m_pClientAPIImpl->sendHearbeat(addr, heartbeatData.get(), - session_credentials); - } catch (MQException& e) { - LOG_ERROR(e.what()); - } - } - } - brokerTable.clear(); -} - -void MQClientFactory::persistAllConsumerOffset(boost::system::error_code& ec, - boost::asio::deadline_timer* t) { - { - boost::lock_guard lock(m_consumerTableMutex); - if (m_consumerTable.size() > 0) { - for (MQCMAP::iterator it = m_consumerTable.begin(); - it != m_consumerTable.end(); ++it) { - LOG_DEBUG("Client factory start persistAllConsumerOffset"); - it->second->persistConsumerOffset(); - } +} + +void MQClientFactory::persistAllConsumerOffset(boost::system::error_code& ec, boost::asio::deadline_timer* t) { + { + boost::lock_guard lock(m_consumerTableMutex); + if (m_consumerTable.size() > 0) { + for (MQCMAP::iterator it = m_consumerTable.begin(); it != m_consumerTable.end(); ++it) { + LOG_DEBUG("Client factory start persistAllConsumerOffset"); + it->second->persistConsumerOffset(); + } + } } - } - boost::system::error_code e; - t->expires_from_now(t->expires_from_now() + boost::posix_time::seconds(5), e); - t->async_wait( - boost::bind(&MQClientFactory::persistAllConsumerOffset, this, ec, t)); + boost::system::error_code e; + t->expires_from_now(t->expires_from_now() + boost::posix_time::seconds(5), e); + t->async_wait(boost::bind(&MQClientFactory::persistAllConsumerOffset, this, ec, t)); } HeartbeatData* MQClientFactory::prepareHeartbeatData() { - HeartbeatData* pHeartbeatData = new HeartbeatData(); - // clientID - pHeartbeatData->setClientID(m_clientId); + HeartbeatData* pHeartbeatData = new HeartbeatData(); + // clientID + pHeartbeatData->setClientID(m_clientId); - // Consumer - insertConsumerInfoToHeartBeatData(pHeartbeatData); + // Consumer + insertConsumerInfoToHeartBeatData(pHeartbeatData); - // Producer - insertProducerInfoToHeartBeatData(pHeartbeatData); + // Producer + insertProducerInfoToHeartBeatData(pHeartbeatData); - return pHeartbeatData; + return pHeartbeatData; } -void MQClientFactory::timerCB_sendHeartbeatToAllBroker( - boost::system::error_code& ec, boost::asio::deadline_timer* t) { - sendHeartbeatToAllBroker(); +void MQClientFactory::timerCB_sendHeartbeatToAllBroker(boost::system::error_code& ec, boost::asio::deadline_timer* t) { + sendHeartbeatToAllBroker(); - boost::system::error_code e; - t->expires_from_now(t->expires_from_now() + boost::posix_time::seconds(30), - e); - t->async_wait(boost::bind(&MQClientFactory::timerCB_sendHeartbeatToAllBroker, - this, ec, t)); + boost::system::error_code e; + t->expires_from_now(t->expires_from_now() + boost::posix_time::seconds(30), e); + t->async_wait(boost::bind(&MQClientFactory::timerCB_sendHeartbeatToAllBroker, this, ec, t)); } -void MQClientFactory::fetchNameServerAddr(boost::system::error_code& ec, - boost::asio::deadline_timer* t) { - m_pClientAPIImpl->fetchNameServerAddr(m_nameSrvDomain); +void MQClientFactory::fetchNameServerAddr(boost::system::error_code& ec, boost::asio::deadline_timer* t) { + m_pClientAPIImpl->fetchNameServerAddr(m_nameSrvDomain); - boost::system::error_code e; - t->expires_from_now( - t->expires_from_now() + boost::posix_time::seconds(60 * 2), e); - t->async_wait( - boost::bind(&MQClientFactory::fetchNameServerAddr, this, ec, t)); + boost::system::error_code e; + t->expires_from_now(t->expires_from_now() + boost::posix_time::seconds(60 * 2), e); + t->async_wait(boost::bind(&MQClientFactory::fetchNameServerAddr, this, ec, t)); } void MQClientFactory::startScheduledTask(bool startFetchNSService) { - boost::asio::io_service::work work(m_async_ioService); // avoid async io - // service stops after - // first timer timeout - // callback - - boost::system::error_code ec1; - boost::asio::deadline_timer t1(m_async_ioService, - boost::posix_time::seconds(3)); - t1.async_wait( - boost::bind(&MQClientFactory::updateTopicRouteInfo, this, ec1, &t1)); - - boost::system::error_code ec2; - boost::asio::deadline_timer t2(m_async_ioService, - boost::posix_time::milliseconds(10)); - t2.async_wait(boost::bind(&MQClientFactory::timerCB_sendHeartbeatToAllBroker, - this, ec2, &t2)); - - if (startFetchNSService) { - boost::system::error_code ec5; - boost::asio::deadline_timer* t5 = new boost::asio::deadline_timer( - m_async_ioService, boost::posix_time::seconds(60 * 2)); - t5->async_wait( - boost::bind(&MQClientFactory::fetchNameServerAddr, this, ec5, t5)); - } - - LOG_INFO("start scheduled task:%s", m_clientId.c_str()); - boost::system::error_code ec; - m_async_ioService.run(ec); + boost::asio::io_service::work work(m_async_ioService); // avoid async io + // service stops after + // first timer timeout + // callback + + boost::system::error_code ec1; + boost::asio::deadline_timer t1(m_async_ioService, boost::posix_time::seconds(3)); + t1.async_wait(boost::bind(&MQClientFactory::updateTopicRouteInfo, this, ec1, &t1)); + + boost::system::error_code ec2; + boost::asio::deadline_timer t2(m_async_ioService, boost::posix_time::milliseconds(10)); + t2.async_wait(boost::bind(&MQClientFactory::timerCB_sendHeartbeatToAllBroker, this, ec2, &t2)); + + if (startFetchNSService) { + boost::system::error_code ec5; + boost::asio::deadline_timer* t5 = + new boost::asio::deadline_timer(m_async_ioService, boost::posix_time::seconds(60 * 2)); + t5->async_wait(boost::bind(&MQClientFactory::fetchNameServerAddr, this, ec5, t5)); + } + + LOG_INFO("start scheduled task:%s", m_clientId.c_str()); + boost::system::error_code ec; + m_async_ioService.run(ec); } void MQClientFactory::rebalanceImmediately() { - // m_consumer_async_service_thread will be only started once for all consumer - if (m_consumer_async_service_thread == NULL) { - doRebalance(); - m_consumer_async_service_thread.reset(new boost::thread( - boost::bind(&MQClientFactory::consumer_timerOperation, this))); - } + // m_consumer_async_service_thread will be only started once for all consumer + if (m_consumer_async_service_thread == NULL) { + doRebalance(); + m_consumer_async_service_thread.reset( + new boost::thread(boost::bind(&MQClientFactory::consumer_timerOperation, this))); + } } void MQClientFactory::consumer_timerOperation() { - LOG_INFO("clientFactory:%s start consumer_timerOperation", - m_clientId.c_str()); - boost::asio::io_service::work work( - m_consumer_async_ioService); // avoid async io - // service stops after - // first timer timeout - // callback - - boost::system::error_code ec1; - boost::asio::deadline_timer t(m_consumer_async_ioService, - boost::posix_time::seconds(10)); - t.async_wait( - boost::bind(&MQClientFactory::timerCB_doRebalance, this, ec1, &t)); - - boost::system::error_code ec2; - boost::asio::deadline_timer t2(m_consumer_async_ioService, - boost::posix_time::seconds(5)); - t2.async_wait( - boost::bind(&MQClientFactory::persistAllConsumerOffset, this, ec2, &t2)); - - boost::system::error_code ec; - m_consumer_async_ioService.run(ec); - LOG_INFO("clientFactory:%s stop consumer_timerOperation", m_clientId.c_str()); -} - -void MQClientFactory::timerCB_doRebalance(boost::system::error_code& ec, - boost::asio::deadline_timer* t) { - doRebalance(); - - boost::system::error_code e; - t->expires_from_now(t->expires_from_now() + boost::posix_time::seconds(10), - e); - t->async_wait( - boost::bind(&MQClientFactory::timerCB_doRebalance, this, ec, t)); + LOG_INFO("clientFactory:%s start consumer_timerOperation", m_clientId.c_str()); + boost::asio::io_service::work work(m_consumer_async_ioService); // avoid async io + // service stops after + // first timer timeout + // callback + + boost::system::error_code ec1; + boost::asio::deadline_timer t(m_consumer_async_ioService, boost::posix_time::seconds(10)); + t.async_wait(boost::bind(&MQClientFactory::timerCB_doRebalance, this, ec1, &t)); + + boost::system::error_code ec2; + boost::asio::deadline_timer t2(m_consumer_async_ioService, boost::posix_time::seconds(5)); + t2.async_wait(boost::bind(&MQClientFactory::persistAllConsumerOffset, this, ec2, &t2)); + + boost::system::error_code ec; + m_consumer_async_ioService.run(ec); + LOG_INFO("clientFactory:%s stop consumer_timerOperation", m_clientId.c_str()); +} + +void MQClientFactory::timerCB_doRebalance(boost::system::error_code& ec, boost::asio::deadline_timer* t) { + doRebalance(); + + boost::system::error_code e; + t->expires_from_now(t->expires_from_now() + boost::posix_time::seconds(10), e); + t->async_wait(boost::bind(&MQClientFactory::timerCB_doRebalance, this, ec, t)); } void MQClientFactory::doRebalance() { - LOG_INFO("Client factory:%s start dorebalance", m_clientId.c_str()); - if (getConsumerTableSize() > 0) { - boost::lock_guard lock(m_consumerTableMutex); - for (MQCMAP::iterator it = m_consumerTable.begin(); - it != m_consumerTable.end(); ++it) { - it->second->doRebalance(); + LOG_INFO("Client factory:%s start dorebalance", m_clientId.c_str()); + if (getConsumerTableSize() > 0) { + boost::lock_guard lock(m_consumerTableMutex); + for (MQCMAP::iterator it = m_consumerTable.begin(); it != m_consumerTable.end(); ++it) { + it->second->doRebalance(); + } } - } - LOG_INFO("Client factory:%s finish dorebalance", m_clientId.c_str()); + LOG_INFO("Client factory:%s finish dorebalance", m_clientId.c_str()); } void MQClientFactory::doRebalanceByConsumerGroup(const string& consumerGroup) { - boost::lock_guard lock(m_consumerTableMutex); - if (m_consumerTable.find(consumerGroup) != m_consumerTable.end()) { - LOG_INFO("Client factory:%s start dorebalance for consumer:%s", - m_clientId.c_str(), consumerGroup.c_str()); - MQConsumer* pMQConsumer = m_consumerTable[consumerGroup]; - pMQConsumer->doRebalance(); - } -} - -void MQClientFactory::unregisterClient( - const string& producerGroup, const string& consumerGroup, - const SessionCredentials& sessionCredentials) { - BrokerAddrMAP brokerTable(getBrokerAddrMap()); - for (BrokerAddrMAP::iterator it = brokerTable.begin(); - it != brokerTable.end(); ++it) { - map brokerMap(it->second); - map::iterator it1 = brokerMap.begin(); - for (; it1 != brokerMap.end(); ++it1) { - string& addr = it1->second; - m_pClientAPIImpl->unregisterClient(addr, m_clientId, producerGroup, - consumerGroup, sessionCredentials); - } - } + boost::lock_guard lock(m_consumerTableMutex); + if (m_consumerTable.find(consumerGroup) != m_consumerTable.end()) { + LOG_INFO("Client factory:%s start dorebalance for consumer:%s", m_clientId.c_str(), consumerGroup.c_str()); + MQConsumer* pMQConsumer = m_consumerTable[consumerGroup]; + pMQConsumer->doRebalance(); + } +} + +void MQClientFactory::unregisterClient(const string& producerGroup, const string& consumerGroup, + const SessionCredentials& sessionCredentials) { + BrokerAddrMAP brokerTable(getBrokerAddrMap()); + for (BrokerAddrMAP::iterator it = brokerTable.begin(); it != brokerTable.end(); ++it) { + map brokerMap(it->second); + map::iterator it1 = brokerMap.begin(); + for (; it1 != brokerMap.end(); ++it1) { + string& addr = it1->second; + m_pClientAPIImpl->unregisterClient(addr, m_clientId, producerGroup, consumerGroup, sessionCredentials); + } + } } //& mqs, - const SessionCredentials& sessionCredentials) { - TopicRouteData* pTopicRouteData = getTopicRouteData(topic); - if (pTopicRouteData == NULL) { - updateTopicRouteInfoFromNameServer(topic, sessionCredentials); - pTopicRouteData = getTopicRouteData(topic); - } - if (pTopicRouteData != NULL) { - topicRouteData2TopicSubscribeInfo(topic, pTopicRouteData, mqs); - if (mqs.empty()) { - THROW_MQEXCEPTION(MQClientException, "Can not find Message Queue", -1); - } - return; - } - THROW_MQEXCEPTION(MQClientException, "Can not find Message Queue", -1); +void MQClientFactory::fetchSubscribeMessageQueues(const string& topic, vector& mqs, + const SessionCredentials& sessionCredentials) { + TopicRouteData* pTopicRouteData = getTopicRouteData(topic); + if (pTopicRouteData == NULL) { + updateTopicRouteInfoFromNameServer(topic, sessionCredentials); + pTopicRouteData = getTopicRouteData(topic); + } + if (pTopicRouteData != NULL) { + topicRouteData2TopicSubscribeInfo(topic, pTopicRouteData, mqs); + if (mqs.empty()) { + THROW_MQEXCEPTION(MQClientException, "Can not find Message Queue", -1); + } + return; + } + THROW_MQEXCEPTION(MQClientException, "Can not find Message Queue", -1); } //getMinOffset(brokerAddr, mq.getTopic(), - mq.getQueueId(), 1000 * 3, - sessionCredentials); - } catch (MQException& e) { - LOG_ERROR(e.what()); +void MQClientFactory::createTopic(const string& key, const string& newTopic, int queueNum, + const SessionCredentials& sessionCredentials) {} + +int64 MQClientFactory::minOffset(const MQMessageQueue& mq, const SessionCredentials& sessionCredentials) { + string brokerAddr = findBrokerAddressInPublish(mq.getBrokerName()); + if (brokerAddr.empty()) { + updateTopicRouteInfoFromNameServer(mq.getTopic(), sessionCredentials); + brokerAddr = findBrokerAddressInPublish(mq.getBrokerName()); } - } - THROW_MQEXCEPTION(MQClientException, "The broker is not exist", -1); + + if (!brokerAddr.empty()) { + try { + return m_pClientAPIImpl->getMinOffset(brokerAddr, mq.getTopic(), mq.getQueueId(), 1000 * 3, + sessionCredentials); + } + catch (MQException& e) { + LOG_ERROR(e.what()); + } + } + THROW_MQEXCEPTION(MQClientException, "The broker is not exist", -1); } -int64 MQClientFactory::maxOffset(const MQMessageQueue& mq, - const SessionCredentials& sessionCredentials) { - string brokerAddr = findBrokerAddressInPublish(mq.getBrokerName()); - if (brokerAddr.empty()) { - updateTopicRouteInfoFromNameServer(mq.getTopic(), sessionCredentials); - brokerAddr = findBrokerAddressInPublish(mq.getBrokerName()); - } +int64 MQClientFactory::maxOffset(const MQMessageQueue& mq, const SessionCredentials& sessionCredentials) { + string brokerAddr = findBrokerAddressInPublish(mq.getBrokerName()); + if (brokerAddr.empty()) { + updateTopicRouteInfoFromNameServer(mq.getTopic(), sessionCredentials); + brokerAddr = findBrokerAddressInPublish(mq.getBrokerName()); + } - if (!brokerAddr.empty()) { - try { - return m_pClientAPIImpl->getMaxOffset(brokerAddr, mq.getTopic(), - mq.getQueueId(), 1000 * 3, - sessionCredentials); - } catch (MQException& e) { - THROW_MQEXCEPTION(MQClientException, "Invoke Broker exception", -1); - } - } - THROW_MQEXCEPTION(MQClientException, "The broker is not exist", -1); -} - -int64 MQClientFactory::searchOffset( - const MQMessageQueue& mq, int64 timestamp, - const SessionCredentials& sessionCredentials) { - string brokerAddr = findBrokerAddressInPublish(mq.getBrokerName()); - if (brokerAddr.empty()) { - updateTopicRouteInfoFromNameServer(mq.getTopic(), sessionCredentials); - brokerAddr = findBrokerAddressInPublish(mq.getBrokerName()); - } - - if (!brokerAddr.empty()) { + if (!brokerAddr.empty()) { + try { + return m_pClientAPIImpl->getMaxOffset(brokerAddr, mq.getTopic(), mq.getQueueId(), 1000 * 3, + sessionCredentials); + } + catch (MQException& e) { + THROW_MQEXCEPTION(MQClientException, "Invoke Broker exception", -1); + } + } + THROW_MQEXCEPTION(MQClientException, "The broker is not exist", -1); +} + +int64 MQClientFactory::searchOffset(const MQMessageQueue& mq, int64 timestamp, + const SessionCredentials& sessionCredentials) { + string brokerAddr = findBrokerAddressInPublish(mq.getBrokerName()); + if (brokerAddr.empty()) { + updateTopicRouteInfoFromNameServer(mq.getTopic(), sessionCredentials); + brokerAddr = findBrokerAddressInPublish(mq.getBrokerName()); + } + + if (!brokerAddr.empty()) { + try { + return m_pClientAPIImpl->searchOffset(brokerAddr, mq.getTopic(), mq.getQueueId(), timestamp, 1000 * 3, + sessionCredentials); + } + catch (MQException& e) { + THROW_MQEXCEPTION(MQClientException, "Invoke Broker exception", -1); + } + } + THROW_MQEXCEPTION(MQClientException, "The broker is not exist", -1); +} + +MQMessageExt* MQClientFactory::viewMessage(const string& msgId, const SessionCredentials& sessionCredentials) { try { - return m_pClientAPIImpl->searchOffset(brokerAddr, mq.getTopic(), - mq.getQueueId(), timestamp, - 1000 * 3, sessionCredentials); - } catch (MQException& e) { - THROW_MQEXCEPTION(MQClientException, "Invoke Broker exception", -1); + return NULL; + } + catch (MQException& e) { + THROW_MQEXCEPTION(MQClientException, "message id illegal", -1); } - } - THROW_MQEXCEPTION(MQClientException, "The broker is not exist", -1); } -MQMessageExt* MQClientFactory::viewMessage( - const string& msgId, const SessionCredentials& sessionCredentials) { - try { - return NULL; - } catch (MQException& e) { - THROW_MQEXCEPTION(MQClientException, "message id illegal", -1); - } +int64 MQClientFactory::earliestMsgStoreTime(const MQMessageQueue& mq, const SessionCredentials& sessionCredentials) { + string brokerAddr = findBrokerAddressInPublish(mq.getBrokerName()); + if (brokerAddr.empty()) { + updateTopicRouteInfoFromNameServer(mq.getTopic(), sessionCredentials); + brokerAddr = findBrokerAddressInPublish(mq.getBrokerName()); + } + + if (!brokerAddr.empty()) { + try { + return m_pClientAPIImpl->getEarliestMsgStoretime(brokerAddr, mq.getTopic(), mq.getQueueId(), 1000 * 3, + sessionCredentials); + } + catch (MQException& e) { + THROW_MQEXCEPTION(MQClientException, "Invoke Broker exception", -1); + } + } + THROW_MQEXCEPTION(MQClientException, "The broker is not exist", -1); } -int64 MQClientFactory::earliestMsgStoreTime( - const MQMessageQueue& mq, const SessionCredentials& sessionCredentials) { - string brokerAddr = findBrokerAddressInPublish(mq.getBrokerName()); - if (brokerAddr.empty()) { - updateTopicRouteInfoFromNameServer(mq.getTopic(), sessionCredentials); - brokerAddr = findBrokerAddressInPublish(mq.getBrokerName()); - } +QueryResult MQClientFactory::queryMessage(const string& topic, const string& key, int maxNum, int64 begin, int64 end, + const SessionCredentials& sessionCredentials) { + THROW_MQEXCEPTION(MQClientException, "queryMessage", -1); +} - if (!brokerAddr.empty()) { - try { - return m_pClientAPIImpl->getEarliestMsgStoretime( - brokerAddr, mq.getTopic(), mq.getQueueId(), 1000 * 3, - sessionCredentials); - } catch (MQException& e) { - THROW_MQEXCEPTION(MQClientException, "Invoke Broker exception", -1); - } - } - THROW_MQEXCEPTION(MQClientException, "The broker is not exist", -1); -} - -QueryResult MQClientFactory::queryMessage( - const string& topic, const string& key, int maxNum, int64 begin, int64 end, - const SessionCredentials& sessionCredentials) { - THROW_MQEXCEPTION(MQClientException, "queryMessage", -1); -} - -void MQClientFactory::findConsumerIds( - const string& topic, const string& group, vector& cids, - const SessionCredentials& sessionCredentials) { - string brokerAddr; - TopicRouteData* pTopicRouteData = getTopicRouteData(topic); - if (pTopicRouteData == NULL) { - updateTopicRouteInfoFromNameServer(topic, sessionCredentials); - pTopicRouteData = getTopicRouteData(topic); - } - if (pTopicRouteData != NULL) { - brokerAddr = pTopicRouteData->selectBrokerAddr(); - } - - if (!brokerAddr.empty()) { - try { - LOG_INFO("getConsumerIdList from broker:%s", brokerAddr.c_str()); - return m_pClientAPIImpl->getConsumerIdListByGroup( - brokerAddr, group, cids, 5000, sessionCredentials); - } catch (MQException& e) { - LOG_ERROR(e.what()); +void MQClientFactory::findConsumerIds(const string& topic, const string& group, vector& cids, + const SessionCredentials& sessionCredentials) { + string brokerAddr; + TopicRouteData* pTopicRouteData = getTopicRouteData(topic); + if (pTopicRouteData == NULL) { + updateTopicRouteInfoFromNameServer(topic, sessionCredentials); + pTopicRouteData = getTopicRouteData(topic); + } + if (pTopicRouteData != NULL) { + brokerAddr = pTopicRouteData->selectBrokerAddr(); + } + + if (!brokerAddr.empty()) { + try { + LOG_INFO("getConsumerIdList from broker:%s", brokerAddr.c_str()); + return m_pClientAPIImpl->getConsumerIdListByGroup(brokerAddr, group, cids, 5000, sessionCredentials); + } + catch (MQException& e) { + LOG_ERROR(e.what()); + } } - } } void MQClientFactory::removeDropedPullRequestOpaque(PullRequest* pullRequest) { - //delete the opaque record that's ignore the response of this pullrequest when drop pullrequest - if (!pullRequest) return; - MQMessageQueue mq = pullRequest->m_messageQueue; - int opaque = pullRequest->getLatestPullRequestOpaque(); - if (opaque > 0) { - LOG_INFO("####### need delete the pullrequest for opaque:%d, mq:%s", opaque, mq.toString().data()); - getMQClientAPIImpl()->deleteOpaqueForDropPullRequest(mq, opaque); - } -} - -void MQClientFactory::resetOffset( - const string& group, const string& topic, - const map& offsetTable) { - MQConsumer* pConsumer = selectConsumer(group); - if (pConsumer) { - map::const_iterator it = offsetTable.begin(); - - for (; it != offsetTable.end(); ++it) { - MQMessageQueue mq = it->first; - PullRequest* pullreq = pConsumer->getRebalance()->getPullRequest(mq); - if (pullreq) { - pullreq->setDroped(true); - LOG_INFO("resetOffset setDroped for opaque:%d, mq:%s", pullreq->getLatestPullRequestOpaque(), mq.toString().data()); - //delete the opaque record that's ignore the response of this pullrequest when drop pullrequest - //removeDropedPullRequestOpaque(pullreq); - pullreq->clearAllMsgs(); - pullreq->updateQueueMaxOffset(it->second); - } else { - LOG_ERROR("no corresponding pullRequest found for topic:%s", - topic.c_str()); - } - } - - for (it = offsetTable.begin(); it != offsetTable.end(); ++it) { - MQMessageQueue mq = it->first; - if (topic == mq.getTopic()) { - LOG_INFO("offset sets to:%lld", it->second); - pConsumer->updateConsumeOffset(mq, it->second); - } - } - pConsumer->persistConsumerOffsetByResetOffset(); - - boost::this_thread::sleep_for(boost::chrono::milliseconds(10)); - - for (it = offsetTable.begin(); it != offsetTable.end(); ++it) { - MQMessageQueue mq = it->first; - if (topic == mq.getTopic()) { - LOG_DEBUG("resetOffset sets to:%lld for mq:%s", it->second, - mq.toString().c_str()); - pConsumer->updateConsumeOffset(mq, it->second); - } - } - pConsumer->persistConsumerOffsetByResetOffset(); - - for (it = offsetTable.begin(); it != offsetTable.end(); ++it) { - MQMessageQueue mq = it->first; - if (topic == mq.getTopic()) { - pConsumer->removeConsumeOffset(mq); - } - } - - // do call pConsumer->doRebalance directly here, as it is conflict with - // timerCB_doRebalance; - doRebalanceByConsumerGroup(pConsumer->getGroupName()); - } else { - LOG_ERROR("no corresponding consumer found for group:%s", group.c_str()); - } -} - -ConsumerRunningInfo* MQClientFactory::consumerRunningInfo( - const string& consumerGroup) { - MQConsumer* pConsumer = selectConsumer(consumerGroup); - if (pConsumer) { - ConsumerRunningInfo* runningInfo = pConsumer->getConsumerRunningInfo(); - if (runningInfo) { - runningInfo->setProperty(ConsumerRunningInfo::PROP_NAMESERVER_ADDR, - pConsumer->getNamesrvAddr()); - if (pConsumer->getConsumeType() == CONSUME_PASSIVELY) { - runningInfo->setProperty(ConsumerRunningInfo::PROP_CONSUME_TYPE, - "CONSUME_PASSIVELY"); - } else { - runningInfo->setProperty(ConsumerRunningInfo::PROP_CONSUME_TYPE, - "CONSUME_ACTIVELY"); - } - runningInfo->setProperty(ConsumerRunningInfo::PROP_CLIENT_VERSION, - "V3_1_8"); // MQVersion::s_CurrentVersion )); - - return runningInfo; - } - } - - LOG_ERROR("no corresponding consumer found for group:%s", - consumerGroup.c_str()); - return NULL; -} - -void MQClientFactory::getSessionCredentialsFromOneOfProducerOrConsumer( - SessionCredentials& session_credentials) { - // Note: on the same MQClientFactory, all producers and consumers used the - // same - // sessionCredentials, - // So only need get sessionCredentials from the first one producer or consumer - // now. - // this function was only used by updateTopicRouteInfo() and - // sendHeartbeatToAllBrokers() now. - // if this strategy was changed in future, need get sessionCredentials for - // each - // producer and consumer. - getSessionCredentialFromProducerTable(session_credentials); - if (!session_credentials.isValid()) - getSessionCredentialFromConsumerTable(session_credentials); - - if (!session_credentials.isValid()) { - LOG_ERROR( - "updateTopicRouteInfo: didn't get the session_credentials from any " - "producers and consumers, please re-intialize it"); - } + // delete the opaque record that's ignore the response of this pullrequest when drop pullrequest + if (!pullRequest) return; + MQMessageQueue mq = pullRequest->m_messageQueue; + int opaque = pullRequest->getLatestPullRequestOpaque(); + if (opaque > 0) { + LOG_INFO("####### need delete the pullrequest for opaque:%d, mq:%s", opaque, mq.toString().data()); + getMQClientAPIImpl()->deleteOpaqueForDropPullRequest(mq, opaque); + } +} + +void MQClientFactory::resetOffset(const string& group, const string& topic, + const map& offsetTable) { + MQConsumer* pConsumer = selectConsumer(group); + if (pConsumer) { + map::const_iterator it = offsetTable.begin(); + + for (; it != offsetTable.end(); ++it) { + MQMessageQueue mq = it->first; + PullRequest* pullreq = pConsumer->getRebalance()->getPullRequest(mq); + if (pullreq) { + pullreq->setDroped(true); + LOG_INFO("resetOffset setDroped for opaque:%d, mq:%s", pullreq->getLatestPullRequestOpaque(), + mq.toString().data()); + // delete the opaque record that's ignore the response of this pullrequest when drop pullrequest + // removeDropedPullRequestOpaque(pullreq); + pullreq->clearAllMsgs(); + pullreq->updateQueueMaxOffset(it->second); + } else { + LOG_ERROR("no corresponding pullRequest found for topic:%s", topic.c_str()); + } + } + + for (it = offsetTable.begin(); it != offsetTable.end(); ++it) { + MQMessageQueue mq = it->first; + if (topic == mq.getTopic()) { + LOG_INFO("offset sets to:%lld", it->second); + pConsumer->updateConsumeOffset(mq, it->second); + } + } + pConsumer->persistConsumerOffsetByResetOffset(); + + boost::this_thread::sleep_for(boost::chrono::milliseconds(10)); + + for (it = offsetTable.begin(); it != offsetTable.end(); ++it) { + MQMessageQueue mq = it->first; + if (topic == mq.getTopic()) { + LOG_DEBUG("resetOffset sets to:%lld for mq:%s", it->second, mq.toString().c_str()); + pConsumer->updateConsumeOffset(mq, it->second); + } + } + pConsumer->persistConsumerOffsetByResetOffset(); + + for (it = offsetTable.begin(); it != offsetTable.end(); ++it) { + MQMessageQueue mq = it->first; + if (topic == mq.getTopic()) { + pConsumer->removeConsumeOffset(mq); + } + } + + // do call pConsumer->doRebalance directly here, as it is conflict with + // timerCB_doRebalance; + doRebalanceByConsumerGroup(pConsumer->getGroupName()); + } else { + LOG_ERROR("no corresponding consumer found for group:%s", group.c_str()); + } +} + +ConsumerRunningInfo* MQClientFactory::consumerRunningInfo(const string& consumerGroup) { + MQConsumer* pConsumer = selectConsumer(consumerGroup); + if (pConsumer) { + ConsumerRunningInfo* runningInfo = pConsumer->getConsumerRunningInfo(); + if (runningInfo) { + runningInfo->setProperty(ConsumerRunningInfo::PROP_NAMESERVER_ADDR, pConsumer->getNamesrvAddr()); + if (pConsumer->getConsumeType() == CONSUME_PASSIVELY) { + runningInfo->setProperty(ConsumerRunningInfo::PROP_CONSUME_TYPE, "CONSUME_PASSIVELY"); + } else { + runningInfo->setProperty(ConsumerRunningInfo::PROP_CONSUME_TYPE, "CONSUME_ACTIVELY"); + } + runningInfo->setProperty(ConsumerRunningInfo::PROP_CLIENT_VERSION, + "V3_1_8"); // MQVersion::s_CurrentVersion )); + + return runningInfo; + } + } + + LOG_ERROR("no corresponding consumer found for group:%s", consumerGroup.c_str()); + return NULL; +} + +void MQClientFactory::getSessionCredentialsFromOneOfProducerOrConsumer(SessionCredentials& session_credentials) { + // Note: on the same MQClientFactory, all producers and consumers used the + // same + // sessionCredentials, + // So only need get sessionCredentials from the first one producer or consumer + // now. + // this function was only used by updateTopicRouteInfo() and + // sendHeartbeatToAllBrokers() now. + // if this strategy was changed in future, need get sessionCredentials for + // each + // producer and consumer. + getSessionCredentialFromProducerTable(session_credentials); + if (!session_credentials.isValid()) getSessionCredentialFromConsumerTable(session_credentials); + + if (!session_credentials.isValid()) { + LOG_ERROR( + "updateTopicRouteInfo: didn't get the session_credentials from any " + "producers and consumers, please re-intialize it"); + } } // topicRouteData2TopicPublishInfo( - const string& topic, TopicRouteData* pRoute); - - void topicRouteData2TopicSubscribeInfo(const string& topic, - TopicRouteData* pRoute, - vector& mqs); - - FindBrokerResult* findBrokerAddressInSubscribe(const string& brokerName, - int brokerId, - bool onlyThisBroker); - - FindBrokerResult* findBrokerAddressInAdmin(const string& brokerName); - - string findBrokerAddressInPublish(const string& brokerName); - - boost::shared_ptr tryToFindTopicPublishInfo( - const string& topic, const SessionCredentials& session_credentials); - - void fetchSubscribeMessageQueues( - const string& topic, vector& mqs, - const SessionCredentials& session_credentials); - - bool updateTopicRouteInfoFromNameServer( - const string& topic, const SessionCredentials& session_credentials, - bool isDefault = false); - void rebalanceImmediately(); - void doRebalanceByConsumerGroup(const string& consumerGroup); - void sendHeartbeatToAllBroker(); - - void findConsumerIds(const string& topic, const string& group, - vector& cids, - const SessionCredentials& session_credentials); - void resetOffset(const string& group, const string& topic, - const map& offsetTable); - ConsumerRunningInfo* consumerRunningInfo(const string& consumerGroup); - bool getSessionCredentialFromConsumer(const string& consumerGroup, - SessionCredentials& sessionCredentials); - void addBrokerToAddrMap(const string& brokerName, - map& brokerAddrs); - map> getBrokerAddrMap(); - void clearBrokerAddrMap(); - void removeDropedPullRequestOpaque(PullRequest* pullRequest); - private: - void unregisterClient(const string& producerGroup, - const string& consumerGroup, - const SessionCredentials& session_credentials); - TopicRouteData* getTopicRouteData(const string& topic); - void addTopicRouteData(const string& topic, TopicRouteData* pTopicRouteData); - HeartbeatData* prepareHeartbeatData(); - - void startScheduledTask(bool startFetchNSService = true); - //& topicList); - void updateConsumerSubscribeTopicInfo(const string& topic, - vector mqs); - void insertConsumerInfoToHeartBeatData(HeartbeatData* pHeartbeatData); - - // producer related operation - bool getSessionCredentialFromProducerTable( - SessionCredentials& sessionCredentials); - bool addProducerToTable(const string& producerName, MQProducer* pMQProducer); - void eraseProducerFromTable(const string& producerName); - int getProducerTableSize(); - void insertProducerInfoToHeartBeatData(HeartbeatData* pHeartbeatData); - - // topicPublishInfo related operation - void addTopicInfoToTable( - const string& topic, - boost::shared_ptr pTopicPublishInfo); - void eraseTopicInfoFromTable(const string& topic); - bool isTopicInfoValidInTable(const string& topic); - boost::shared_ptr getTopicPublishInfoFromTable( - const string& topic); - void getTopicListFromTopicPublishInfo(set& topicList); - - void getSessionCredentialsFromOneOfProducerOrConsumer( - SessionCredentials& session_credentials); - - private: - string m_clientId; - string m_nameSrvDomain; // per clientId - ServiceState m_serviceState; - bool m_bFetchNSService; - - // MQProducer; - typedef map MQPMAP; - boost::mutex m_producerTableMutex; - MQPMAP m_producerTable; - - // MQConsumer; - typedef map MQCMAP; - boost::mutex m_consumerTableMutex; - MQCMAP m_consumerTable; - - // TopicRouteData - typedef map TRDMAP; - boost::mutex m_topicRouteTableMutex; - TRDMAP m_topicRouteTable; - - //TopicPublishInfo> ; - typedef map> TPMap; - boost::mutex m_topicPublishInfoTableMutex; - TPMap m_topicPublishInfoTable; - boost::mutex m_factoryLock; - boost::mutex m_topicPublishInfoLock; - - // m_pClientAPIImpl; - unique_ptr m_pClientRemotingProcessor; - - boost::asio::io_service m_async_ioService; - unique_ptr m_async_service_thread; - - boost::asio::io_service m_consumer_async_ioService; - unique_ptr m_consumer_async_service_thread; + + MQClientAPIImpl* getMQClientAPIImpl() const; + MQProducer* selectProducer(const string& group); + MQConsumer* selectConsumer(const string& group); + + boost::shared_ptr topicRouteData2TopicPublishInfo(const string& topic, TopicRouteData* pRoute); + + void topicRouteData2TopicSubscribeInfo(const string& topic, TopicRouteData* pRoute, vector& mqs); + + FindBrokerResult* findBrokerAddressInSubscribe(const string& brokerName, int brokerId, bool onlyThisBroker); + + FindBrokerResult* findBrokerAddressInAdmin(const string& brokerName); + + string findBrokerAddressInPublish(const string& brokerName); + + boost::shared_ptr tryToFindTopicPublishInfo(const string& topic, + const SessionCredentials& session_credentials); + + void fetchSubscribeMessageQueues(const string& topic, vector& mqs, + const SessionCredentials& session_credentials); + + bool updateTopicRouteInfoFromNameServer(const string& topic, const SessionCredentials& session_credentials, + bool isDefault = false); + void rebalanceImmediately(); + void doRebalanceByConsumerGroup(const string& consumerGroup); + void sendHeartbeatToAllBroker(); + + void findConsumerIds(const string& topic, const string& group, vector& cids, + const SessionCredentials& session_credentials); + void resetOffset(const string& group, const string& topic, const map& offsetTable); + ConsumerRunningInfo* consumerRunningInfo(const string& consumerGroup); + bool getSessionCredentialFromConsumer(const string& consumerGroup, SessionCredentials& sessionCredentials); + void addBrokerToAddrMap(const string& brokerName, map& brokerAddrs); + map> getBrokerAddrMap(); + void clearBrokerAddrMap(); + void removeDropedPullRequestOpaque(PullRequest* pullRequest); + +private: + void unregisterClient(const string& producerGroup, const string& consumerGroup, + const SessionCredentials& session_credentials); + TopicRouteData* getTopicRouteData(const string& topic); + void addTopicRouteData(const string& topic, TopicRouteData* pTopicRouteData); + HeartbeatData* prepareHeartbeatData(); + + void startScheduledTask(bool startFetchNSService = true); + //& topicList); + void updateConsumerSubscribeTopicInfo(const string& topic, vector mqs); + void insertConsumerInfoToHeartBeatData(HeartbeatData* pHeartbeatData); + + // producer related operation + bool getSessionCredentialFromProducerTable(SessionCredentials& sessionCredentials); + bool addProducerToTable(const string& producerName, MQProducer* pMQProducer); + void eraseProducerFromTable(const string& producerName); + int getProducerTableSize(); + void insertProducerInfoToHeartBeatData(HeartbeatData* pHeartbeatData); + + // topicPublishInfo related operation + void addTopicInfoToTable(const string& topic, boost::shared_ptr pTopicPublishInfo); + void eraseTopicInfoFromTable(const string& topic); + bool isTopicInfoValidInTable(const string& topic); + boost::shared_ptr getTopicPublishInfoFromTable(const string& topic); + void getTopicListFromTopicPublishInfo(set& topicList); + + void getSessionCredentialsFromOneOfProducerOrConsumer(SessionCredentials& session_credentials); + +private: + string m_clientId; + string m_nameSrvDomain; // per clientId + ServiceState m_serviceState; + bool m_bFetchNSService; + + // MQProducer; + typedef map MQPMAP; + boost::mutex m_producerTableMutex; + MQPMAP m_producerTable; + + // MQConsumer; + typedef map MQCMAP; + boost::mutex m_consumerTableMutex; + MQCMAP m_consumerTable; + + // TopicRouteData + typedef map TRDMAP; + boost::mutex m_topicRouteTableMutex; + TRDMAP m_topicRouteTable; + + //TopicPublishInfo> ; + typedef map> TPMap; + boost::mutex m_topicPublishInfoTableMutex; + TPMap m_topicPublishInfoTable; + boost::mutex m_factoryLock; + boost::mutex m_topicPublishInfoLock; + + // m_pClientAPIImpl; + unique_ptr m_pClientRemotingProcessor; + + boost::asio::io_service m_async_ioService; + unique_ptr m_async_service_thread; + + boost::asio::io_service m_consumer_async_ioService; + unique_ptr m_consumer_async_service_thread; }; } //second; - } else { - MQClientFactory* factory = - new MQClientFactory(clientId, pullThreadNum, tcpConnectTimeout, - tcpTransportTryLockTimeout, unitName); - m_factoryTable[clientId] = factory; - return factory; - } +MQClientFactory* MQClientManager::getMQClientFactory(const string& clientId, int pullThreadNum, + uint64_t tcpConnectTimeout, uint64_t tcpTransportTryLockTimeout, + string unitName) { + FTMAP::iterator it = m_factoryTable.find(clientId); + if (it != m_factoryTable.end()) { + return it->second; + } else { + MQClientFactory* factory = + new MQClientFactory(clientId, pullThreadNum, tcpConnectTimeout, tcpTransportTryLockTimeout, unitName); + m_factoryTable[clientId] = factory; + return factory; + } } void MQClientManager::removeClientFactory(const string& clientId) { - FTMAP::iterator it = m_factoryTable.find(clientId); - if (it != m_factoryTable.end()) { - deleteAndZero(it->second); - m_factoryTable.erase(it); - } + FTMAP::iterator it = m_factoryTable.find(clientId); + if (it != m_factoryTable.end()) { + deleteAndZero(it->second); + m_factoryTable.erase(it); + } } // FTMAP; - FTMAP m_factoryTable; +private: + typedef map FTMAP; + FTMAP m_factoryTable; }; // v; - UtilAll::Split(v, arg_str_, " "); - m_args.insert(m_args.end(), v.begin(), v.end()); + vector v; + UtilAll::Split(v, arg_str_, " "); + m_args.insert(m_args.end(), v.begin(), v.end()); } string Arg_helper::get_option(int idx_) const { - if ((size_t)idx_ >= m_args.size()) { - return ""; - } - return m_args[idx_]; + if ((size_t)idx_ >= m_args.size()) { + return ""; + } + return m_args[idx_]; } bool Arg_helper::is_enable_option(string opt_) const { - for (size_t i = 0; i < m_args.size(); ++i) { - if (opt_ == m_args[i]) { - return true; + for (size_t i = 0; i < m_args.size(); ++i) { + if (opt_ == m_args[i]) { + return true; + } } - } - return false; + return false; } string Arg_helper::get_option_value(string opt_) const { - string ret = ""; - for (size_t i = 0; i < m_args.size(); ++i) { - if (opt_ == m_args[i]) { - size_t value_idx = ++i; - if (value_idx >= m_args.size()) { - return ret; - } - ret = m_args[value_idx]; - return ret; + string ret = ""; + for (size_t i = 0; i < m_args.size(); ++i) { + if (opt_ == m_args[i]) { + size_t value_idx = ++i; + if (value_idx >= m_args.size()) { + return ret; + } + ret = m_args[value_idx]; + return ret; + } } - } - return ret; + return ret; } //(m_pAsyncCallBack); - if (pCallback) { - unique_ptr exception(new MQException( - "send msg failed due to wait response timeout or network error", -1, - __FILE__, __LINE__)); - pCallback->onException(*exception); - if (pCallback->getSendCallbackType() == autoDeleteSendCallback) { - deleteAndZero(pCallback); + if (m_pAsyncCallBack == NULL) return; + + SendCallback* pCallback = static_cast(m_pAsyncCallBack); + if (pCallback) { + unique_ptr exception( + new MQException("send msg failed due to wait response timeout or network error", -1, __FILE__, __LINE__)); + pCallback->onException(*exception); + if (pCallback->getSendCallbackType() == autoDeleteSendCallback) { + deleteAndZero(pCallback); + } } - } } -void SendCallbackWrap::operationComplete(ResponseFuture* pResponseFuture, - bool bProducePullRequest) { - unique_ptr pResponse(pResponseFuture->getCommand()); +void SendCallbackWrap::operationComplete(ResponseFuture* pResponseFuture, bool bProducePullRequest) { + unique_ptr pResponse(pResponseFuture->getCommand()); - if (m_pAsyncCallBack == NULL) { - return; - } - int opaque = pResponseFuture->getOpaque(); - SendCallback* pCallback = static_cast(m_pAsyncCallBack); + if (m_pAsyncCallBack == NULL) { + return; + } + int opaque = pResponseFuture->getOpaque(); + SendCallback* pCallback = static_cast(m_pAsyncCallBack); - if (!pResponse) { - string err = "unknow reseaon"; - if (!pResponseFuture->isSendRequestOK()) { - err = "send request failed"; + if (!pResponse) { + string err = "unknow reseaon"; + if (!pResponseFuture->isSendRequestOK()) { + err = "send request failed"; - } else if (pResponseFuture->isTimeOut()) { - // pResponseFuture->setAsyncResponseFlag(); - err = "wait response timeout"; - } - if (pCallback) { - MQException exception(err, -1, __FILE__, __LINE__); - pCallback->onException(exception); - } - LOG_ERROR("send failed of:%d", pResponseFuture->getOpaque()); - } else { - try { - SendResult ret = m_pClientAPI->processSendResponse(m_brokerName, m_msg, pResponse.get()); - if (pCallback) { - LOG_DEBUG("operationComplete: processSendResponse success, opaque:%d, maxRetryTime:%d, retrySendTimes:%d", opaque, pResponseFuture->getMaxRetrySendTimes(), pResponseFuture->getRetrySendTimes()); - pCallback->onSuccess(ret); + } else if (pResponseFuture->isTimeOut()) { + // pResponseFuture->setAsyncResponseFlag(); + err = "wait response timeout"; + } + if (pCallback) { + MQException exception(err, -1, __FILE__, __LINE__); + pCallback->onException(exception); + } + LOG_ERROR("send failed of:%d", pResponseFuture->getOpaque()); + } else { + try { + SendResult ret = m_pClientAPI->processSendResponse(m_brokerName, m_msg, pResponse.get()); + if (pCallback) { + LOG_DEBUG( + "operationComplete: processSendResponse success, opaque:%d, maxRetryTime:%d, retrySendTimes:%d", + opaque, pResponseFuture->getMaxRetrySendTimes(), pResponseFuture->getRetrySendTimes()); + pCallback->onSuccess(ret); + } } - } catch (MQException& e) { - LOG_ERROR("operationComplete: processSendResponse exception: %s", e.what()); - - //broker may return exception, need consider retry send - int maxRetryTimes = pResponseFuture->getMaxRetrySendTimes(); - int retryTimes = pResponseFuture->getRetrySendTimes(); - if (pResponseFuture->getASyncFlag() && retryTimes < maxRetryTimes && maxRetryTimes > 1) { - - int64 left_timeout_ms = pResponseFuture->leftTime(); - string brokerAddr = pResponseFuture->getBrokerAddr(); - const RemotingCommand& requestCommand = pResponseFuture->getRequestCommand(); - retryTimes += 1; - LOG_WARN("retry send, opaque:%d, sendTimes:%d, maxRetryTimes:%d, left_timeout:%lld, brokerAddr:%s, msg:%s", + catch (MQException& e) { + LOG_ERROR("operationComplete: processSendResponse exception: %s", e.what()); + + // broker may return exception, need consider retry send + int maxRetryTimes = pResponseFuture->getMaxRetrySendTimes(); + int retryTimes = pResponseFuture->getRetrySendTimes(); + if (pResponseFuture->getASyncFlag() && retryTimes < maxRetryTimes && maxRetryTimes > 1) { + + int64 left_timeout_ms = pResponseFuture->leftTime(); + string brokerAddr = pResponseFuture->getBrokerAddr(); + const RemotingCommand& requestCommand = pResponseFuture->getRequestCommand(); + retryTimes += 1; + LOG_WARN( + "retry send, opaque:%d, sendTimes:%d, maxRetryTimes:%d, left_timeout:%lld, brokerAddr:%s, msg:%s", opaque, retryTimes, maxRetryTimes, left_timeout_ms, brokerAddr.data(), m_msg.toString().data()); - bool exception_flag = false; - try { - m_pClientAPI->sendMessageAsync(pResponseFuture->getBrokerAddr(), m_brokerName, m_msg, (RemotingCommand&)requestCommand, pCallback, left_timeout_ms, maxRetryTimes, retryTimes); - } catch (MQClientException& e) { - LOG_ERROR("retry send exception:%s, opaque:%d, retryTimes:%d, msg:%s, not retry send again", e.what(), opaque, retryTimes, m_msg.toString().data()); - exception_flag = true; + bool exception_flag = false; + try { + m_pClientAPI->sendMessageAsync(pResponseFuture->getBrokerAddr(), m_brokerName, m_msg, + (RemotingCommand&)requestCommand, pCallback, left_timeout_ms, + maxRetryTimes, retryTimes); + } + catch (MQClientException& e) { + LOG_ERROR("retry send exception:%s, opaque:%d, retryTimes:%d, msg:%s, not retry send again", + e.what(), opaque, retryTimes, m_msg.toString().data()); + exception_flag = true; + } + + if (exception_flag == false) { + return; // send retry again, here need return + } } - if (exception_flag == false) { - return; //send retry again, here need return + if (pCallback) { + MQException exception("process send response error", -1, __FILE__, __LINE__); + pCallback->onException(exception); } } - - if (pCallback) { - MQException exception("process send response error", -1, __FILE__, - __LINE__); - pCallback->onException(exception); - } } - } - if (pCallback && pCallback->getSendCallbackType() == autoDeleteSendCallback) { - deleteAndZero(pCallback); - } + if (pCallback && pCallback->getSendCallbackType() == autoDeleteSendCallback) { + deleteAndZero(pCallback); + } } //(pArg); + m_pArg = *static_cast(pArg); } PullCallbackWarp::~PullCallbackWarp() {} void PullCallbackWarp::onException() { - if (m_pAsyncCallBack == NULL) return; - - PullCallback* pCallback = static_cast(m_pAsyncCallBack); - if (pCallback) { - MQException exception("wait response timeout", -1, __FILE__, __LINE__); - pCallback->onException(exception); - } else { - LOG_ERROR("PullCallback is NULL, AsyncPull could not continue"); - } + if (m_pAsyncCallBack == NULL) return; + + PullCallback* pCallback = static_cast(m_pAsyncCallBack); + if (pCallback) { + MQException exception("wait response timeout", -1, __FILE__, __LINE__); + pCallback->onException(exception); + } else { + LOG_ERROR("PullCallback is NULL, AsyncPull could not continue"); + } } -void PullCallbackWarp::operationComplete(ResponseFuture* pResponseFuture, - bool bProducePullRequest) { - unique_ptr pResponse(pResponseFuture->getCommand()); - if (m_pAsyncCallBack == NULL) { - LOG_ERROR("m_pAsyncCallBack is NULL, AsyncPull could not continue"); - return; - } - PullCallback* pCallback = static_cast(m_pAsyncCallBack); - if (!pResponse) { - string err = "unknow reseaon"; - if (!pResponseFuture->isSendRequestOK()) { - err = "send request failed"; - - } else if (pResponseFuture->isTimeOut()) { - // pResponseFuture->setAsyncResponseFlag(); - err = "wait response timeout"; +void PullCallbackWarp::operationComplete(ResponseFuture* pResponseFuture, bool bProducePullRequest) { + unique_ptr pResponse(pResponseFuture->getCommand()); + if (m_pAsyncCallBack == NULL) { + LOG_ERROR("m_pAsyncCallBack is NULL, AsyncPull could not continue"); + return; } - MQException exception(err, -1, __FILE__, __LINE__); - LOG_ERROR("Async pull exception of opaque:%d", - pResponseFuture->getOpaque()); - if (pCallback && bProducePullRequest) pCallback->onException(exception); - } else { - try { - if (m_pArg.pPullWrapper) { - unique_ptr pullResult( - m_pClientAPI->processPullResponse(pResponse.get())); - PullResult result = m_pArg.pPullWrapper->processPullResult( - m_pArg.mq, pullResult.get(), &m_pArg.subData); - if (pCallback) - pCallback->onSuccess(m_pArg.mq, result, bProducePullRequest); - } else { - LOG_ERROR("pPullWrapper had been destroyed with consumer"); - } - } catch (MQException& e) { - LOG_ERROR(e.what()); - MQException exception("pullResult error", -1, __FILE__, __LINE__); - if (pCallback && bProducePullRequest) pCallback->onException(exception); + PullCallback* pCallback = static_cast(m_pAsyncCallBack); + if (!pResponse) { + string err = "unknow reseaon"; + if (!pResponseFuture->isSendRequestOK()) { + err = "send request failed"; + + } else if (pResponseFuture->isTimeOut()) { + // pResponseFuture->setAsyncResponseFlag(); + err = "wait response timeout"; + } + MQException exception(err, -1, __FILE__, __LINE__); + LOG_ERROR("Async pull exception of opaque:%d", pResponseFuture->getOpaque()); + if (pCallback && bProducePullRequest) pCallback->onException(exception); + } else { + try { + if (m_pArg.pPullWrapper) { + unique_ptr pullResult(m_pClientAPI->processPullResponse(pResponse.get())); + PullResult result = + m_pArg.pPullWrapper->processPullResult(m_pArg.mq, pullResult.get(), &m_pArg.subData); + if (pCallback) pCallback->onSuccess(m_pArg.mq, result, bProducePullRequest); + } else { + LOG_ERROR("pPullWrapper had been destroyed with consumer"); + } + } + catch (MQException& e) { + LOG_ERROR(e.what()); + MQException exception("pullResult error", -1, __FILE__, __LINE__); + if (pCallback && bProducePullRequest) pCallback->onException(exception); + } } - } } //((n << 8) | (n >> 8)); -} +inline uint16 ByteOrder::swap(uint16 n) { return static_cast((n << 8) | (n >> 8)); } -inline uint32 ByteOrder::swap(uint32 n) { - return (n << 24) | (n >> 24) | ((n & 0xff00) << 8) | ((n & 0xff0000) >> 8); -} +inline uint32 ByteOrder::swap(uint32 n) { return (n << 24) | (n >> 24) | ((n & 0xff00) << 8) | ((n & 0xff0000) >> 8); } inline uint64 ByteOrder::swap(uint64 value) { - return (((uint64)swap((uint32)value)) << 32) | swap((uint32)(value >> 32)); + return (((uint64)swap((uint32)value)) << 32) | swap((uint32)(value >> 32)); } -#if __BYTE_ORDER__ == \ - __ORDER_LITTLE_ENDIAN__ //__BYTE_ORDER__ is defined by GCC +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ //__BYTE_ORDER__ is defined by GCC inline uint16 ByteOrder::swapIfBigEndian(const uint16 v) { return v; } inline uint32 ByteOrder::swapIfBigEndian(const uint32 v) { return v; } inline uint64 ByteOrder::swapIfBigEndian(const uint64 v) { return v; } inline uint16 ByteOrder::swapIfLittleEndian(const uint16 v) { return swap(v); } inline uint32 ByteOrder::swapIfLittleEndian(const uint32 v) { return swap(v); } inline uint64 ByteOrder::swapIfLittleEndian(const uint64 v) { return swap(v); } -inline uint32 ByteOrder::littleEndianInt(const void* const bytes) { - return *static_cast(bytes); -} -inline uint64 ByteOrder::littleEndianInt64(const void* const bytes) { - return *static_cast(bytes); -} -inline uint16 ByteOrder::littleEndianShort(const void* const bytes) { - return *static_cast(bytes); -} -inline uint32 ByteOrder::bigEndianInt(const void* const bytes) { - return swap(*static_cast(bytes)); -} -inline uint64 ByteOrder::bigEndianInt64(const void* const bytes) { - return swap(*static_cast(bytes)); -} -inline uint16 ByteOrder::bigEndianShort(const void* const bytes) { - return swap(*static_cast(bytes)); -} +inline uint32 ByteOrder::littleEndianInt(const void* const bytes) { return *static_cast(bytes); } +inline uint64 ByteOrder::littleEndianInt64(const void* const bytes) { return *static_cast(bytes); } +inline uint16 ByteOrder::littleEndianShort(const void* const bytes) { return *static_cast(bytes); } +inline uint32 ByteOrder::bigEndianInt(const void* const bytes) { return swap(*static_cast(bytes)); } +inline uint64 ByteOrder::bigEndianInt64(const void* const bytes) { return swap(*static_cast(bytes)); } +inline uint16 ByteOrder::bigEndianShort(const void* const bytes) { return swap(*static_cast(bytes)); } inline bool ByteOrder::isBigEndian() { return false; } #else inline uint16 ByteOrder::swapIfBigEndian(const uint16 v) { return swap(v); } @@ -147,48 +130,32 @@ inline uint64 ByteOrder::swapIfBigEndian(const uint64 v) { return swap(v); } inline uint16 ByteOrder::swapIfLittleEndian(const uint16 v) { return v; } inline uint32 ByteOrder::swapIfLittleEndian(const uint32 v) { return v; } inline uint64 ByteOrder::swapIfLittleEndian(const uint64 v) { return v; } -inline uint32 ByteOrder::littleEndianInt(const void* const bytes) { - return swap(*static_cast(bytes)); -} -inline uint64 ByteOrder::littleEndianInt64(const void* const bytes) { - return swap(*static_cast(bytes)); -} -inline uint16 ByteOrder::littleEndianShort(const void* const bytes) { - return swap(*static_cast(bytes)); -} -inline uint32 ByteOrder::bigEndianInt(const void* const bytes) { - return *static_cast(bytes); -} -inline uint64 ByteOrder::bigEndianInt64(const void* const bytes) { - return *static_cast(bytes); -} -inline uint16 ByteOrder::bigEndianShort(const void* const bytes) { - return *static_cast(bytes); -} +inline uint32 ByteOrder::littleEndianInt(const void* const bytes) { return swap(*static_cast(bytes)); } +inline uint64 ByteOrder::littleEndianInt64(const void* const bytes) { return swap(*static_cast(bytes)); } +inline uint16 ByteOrder::littleEndianShort(const void* const bytes) { return swap(*static_cast(bytes)); } +inline uint32 ByteOrder::bigEndianInt(const void* const bytes) { return *static_cast(bytes); } +inline uint64 ByteOrder::bigEndianInt64(const void* const bytes) { return *static_cast(bytes); } +inline uint16 ByteOrder::bigEndianShort(const void* const bytes) { return *static_cast(bytes); } inline bool ByteOrder::isBigEndian() { return true; } #endif inline int ByteOrder::littleEndian24Bit(const void* const bytes) { - return (((int)static_cast(bytes)[2]) << 16) | - (((int)static_cast(bytes)[1]) << 8) | - ((int)static_cast(bytes)[0]); + return (((int)static_cast(bytes)[2]) << 16) | (((int)static_cast(bytes)[1]) << 8) | + ((int)static_cast(bytes)[0]); } inline int ByteOrder::bigEndian24Bit(const void* const bytes) { - return (((int)static_cast(bytes)[0]) << 16) | - (((int)static_cast(bytes)[1]) << 8) | - ((int)static_cast(bytes)[2]); -} -inline void ByteOrder::littleEndian24BitToChars(const int value, - void* const destBytes) { - static_cast(destBytes)[0] = (uint8)value; - static_cast(destBytes)[1] = (uint8)(value >> 8); - static_cast(destBytes)[2] = (uint8)(value >> 16); -} -inline void ByteOrder::bigEndian24BitToChars(const int value, - void* const destBytes) { - static_cast(destBytes)[0] = (uint8)(value >> 16); - static_cast(destBytes)[1] = (uint8)(value >> 8); - static_cast(destBytes)[2] = (uint8)value; + return (((int)static_cast(bytes)[0]) << 16) | (((int)static_cast(bytes)[1]) << 8) | + ((int)static_cast(bytes)[2]); +} +inline void ByteOrder::littleEndian24BitToChars(const int value, void* const destBytes) { + static_cast(destBytes)[0] = (uint8)value; + static_cast(destBytes)[1] = (uint8)(value >> 8); + static_cast(destBytes)[2] = (uint8)(value >> 16); +} +inline void ByteOrder::bigEndian24BitToChars(const int value, void* const destBytes) { + static_cast(destBytes)[0] = (uint8)(value >> 16); + static_cast(destBytes)[1] = (uint8)(value >> 8); + static_cast(destBytes)[2] = (uint8)value; } } #endif // BYTEORDER_H_INCLUDED diff --git a/src/common/ClientRPCHook.cpp b/src/common/ClientRPCHook.cpp old mode 100755 new mode 100644 index 54c21dd98..a7b857df1 --- a/src/common/ClientRPCHook.cpp +++ b/src/common/ClientRPCHook.cpp @@ -31,54 +31,45 @@ const string SessionCredentials::Signature = "Signature"; const string SessionCredentials::SignatureMethod = "SignatureMethod"; const string SessionCredentials::ONSChannelKey = "OnsChannel"; -void ClientRPCHook::doBeforeRequest(const string& remoteAddr, - RemotingCommand& request) { - CommandHeader* header = request.getCommandHeader(); +void ClientRPCHook::doBeforeRequest(const string& remoteAddr, RemotingCommand& request) { + CommandHeader* header = request.getCommandHeader(); - map requestMap; - string totalMsg; + map requestMap; + string totalMsg; - requestMap.insert(pair(SessionCredentials::AccessKey, - sessionCredentials.getAccessKey())); - requestMap.insert(pair(SessionCredentials::ONSChannelKey, - sessionCredentials.getAuthChannel())); + requestMap.insert(pair(SessionCredentials::AccessKey, sessionCredentials.getAccessKey())); + requestMap.insert(pair(SessionCredentials::ONSChannelKey, sessionCredentials.getAuthChannel())); - LOG_DEBUG("before insert declared filed,MAP SIZE is:" SIZET_FMT "", - requestMap.size()); - if (header != NULL) { - header->SetDeclaredFieldOfCommandHeader(requestMap); - } - LOG_DEBUG("after insert declared filed, MAP SIZE is:" SIZET_FMT "", - requestMap.size()); + LOG_DEBUG("before insert declared filed,MAP SIZE is:" SIZET_FMT "", requestMap.size()); + if (header != NULL) { + header->SetDeclaredFieldOfCommandHeader(requestMap); + } + LOG_DEBUG("after insert declared filed, MAP SIZE is:" SIZET_FMT "", requestMap.size()); - map::iterator it = requestMap.begin(); - for (; it != requestMap.end(); ++it) { - totalMsg.append(it->second); - } - if (request.getMsgBody().length() > 0) { - LOG_DEBUG("msgBody is:%s, msgBody length is:" SIZET_FMT "", - request.getMsgBody().c_str(), request.getMsgBody().length()); + map::iterator it = requestMap.begin(); + for (; it != requestMap.end(); ++it) { + totalMsg.append(it->second); + } + if (request.getMsgBody().length() > 0) { + LOG_DEBUG("msgBody is:%s, msgBody length is:" SIZET_FMT "", request.getMsgBody().c_str(), + request.getMsgBody().length()); - totalMsg.append(request.getMsgBody()); - } - LOG_DEBUG("total msg info are:%s, size is:" SIZET_FMT "", totalMsg.c_str(), - totalMsg.size()); - char* pSignature = - rocketmqSignature::spas_sign(totalMsg.c_str(), totalMsg.size(), - sessionCredentials.getSecretKey().c_str()); - // char *pSignature = spas_sign(totalMsg.c_str(), - // sessionCredentials.getSecretKey().c_str()); + totalMsg.append(request.getMsgBody()); + } + LOG_DEBUG("total msg info are:%s, size is:" SIZET_FMT "", totalMsg.c_str(), totalMsg.size()); + char* pSignature = + rocketmqSignature::spas_sign(totalMsg.c_str(), totalMsg.size(), sessionCredentials.getSecretKey().c_str()); + // char *pSignature = spas_sign(totalMsg.c_str(), + // sessionCredentials.getSecretKey().c_str()); - if (pSignature != NULL) { - string signature(static_cast(pSignature)); - request.addExtField(SessionCredentials::Signature, signature); - request.addExtField(SessionCredentials::AccessKey, - sessionCredentials.getAccessKey()); - request.addExtField(SessionCredentials::ONSChannelKey, - sessionCredentials.getAuthChannel()); - rocketmqSignature::spas_mem_free(pSignature); - } else { - LOG_ERROR("signature for request failed"); - } + if (pSignature != NULL) { + string signature(static_cast(pSignature)); + request.addExtField(SessionCredentials::Signature, signature); + request.addExtField(SessionCredentials::AccessKey, sessionCredentials.getAccessKey()); + request.addExtField(SessionCredentials::ONSChannelKey, sessionCredentials.getAuthChannel()); + rocketmqSignature::spas_mem_free(pSignature); + } else { + LOG_ERROR("signature for request failed"); + } } } diff --git a/src/common/ClientRPCHook.h b/src/common/ClientRPCHook.h old mode 100755 new mode 100644 index 853917dfd..fec49400d --- a/src/common/ClientRPCHook.h +++ b/src/common/ClientRPCHook.h @@ -22,29 +22,24 @@ #include "SessionCredentials.h" namespace rocketmq { class RPCHook { - public: - RPCHook() {} - virtual ~RPCHook() {} - virtual void doBeforeRequest(const string& remoteAddr, - RemotingCommand& request) = 0; - virtual void doAfterResponse(RemotingCommand& request, - RemotingCommand& response) = 0; +public: + RPCHook() {} + virtual ~RPCHook() {} + virtual void doBeforeRequest(const string& remoteAddr, RemotingCommand& request) = 0; + virtual void doAfterResponse(RemotingCommand& request, RemotingCommand& response) = 0; }; class ClientRPCHook : public RPCHook { - private: - SessionCredentials sessionCredentials; +private: + SessionCredentials sessionCredentials; - public: - ClientRPCHook(const SessionCredentials& session_credentials) - : sessionCredentials(session_credentials) {} - virtual ~ClientRPCHook() {} +public: + ClientRPCHook(const SessionCredentials& session_credentials) : sessionCredentials(session_credentials) {} + virtual ~ClientRPCHook() {} - virtual void doBeforeRequest(const string& remoteAddr, - RemotingCommand& request); + virtual void doBeforeRequest(const string& remoteAddr, RemotingCommand& request); - virtual void doAfterResponse(RemotingCommand& request, - RemotingCommand& response) {} + virtual void doAfterResponse(RemotingCommand& request, RemotingCommand& response) {} }; } #endif diff --git a/src/common/CommunicationMode.h b/src/common/CommunicationMode.h old mode 100755 new mode 100644 index 9d9b283b0..cdce46ef2 --- a/src/common/CommunicationMode.h +++ b/src/common/CommunicationMode.h @@ -14,13 +14,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + #ifndef __COMMUNICATIONMODE_H__ #define __COMMUNICATIONMODE_H__ namespace rocketmq { //setSubString(SUB_ALL); - } else { - vector out; - UtilAll::Split(out, subString, "||"); + if (subString.empty() || !subString.compare(SUB_ALL)) { + subscriptionData->setSubString(SUB_ALL); + } else { + vector out; + UtilAll::Split(out, subString, "||"); - if (out.empty()) { - THROW_MQEXCEPTION(MQClientException, "FilterAPI subString split error", - -1); - } + if (out.empty()) { + THROW_MQEXCEPTION(MQClientException, "FilterAPI subString split error", -1); + } - for (size_t i = 0; i < out.size(); i++) { - string tag = out[i]; - if (!tag.empty()) { - UtilAll::Trim(tag); - if (!tag.empty()) { - subscriptionData->putTagsSet(tag); - subscriptionData->putCodeSet(tag); - } + for (size_t i = 0; i < out.size(); i++) { + string tag = out[i]; + if (!tag.empty()) { + UtilAll::Trim(tag); + if (!tag.empty()) { + subscriptionData->putTagsSet(tag); + subscriptionData->putCodeSet(tag); + } + } + } } - } - } - return subscriptionData; - } + return subscriptionData; + } }; // @@ -21,89 +21,88 @@ namespace rocketmq { int64 InputStream::getNumBytesRemaining() { - int64 len = getTotalLength(); + int64 len = getTotalLength(); - if (len >= 0) len -= getPosition(); + if (len >= 0) len -= getPosition(); - return len; + return len; } char InputStream::readByte() { - char temp = 0; - read(&temp, 1); - return temp; + char temp = 0; + read(&temp, 1); + return temp; } bool InputStream::readBool() { return readByte() != 0; } short InputStream::readShortBigEndian() { - char temp[2]; + char temp[2]; - if (read(temp, 2) == 2) { - short int v; - ReadBigEndian(temp, &v); - return v; - } + if (read(temp, 2) == 2) { + short int v; + ReadBigEndian(temp, &v); + return v; + } - return 0; + return 0; } int InputStream::readIntBigEndian() { - char temp[4]; - - if (read(temp, 4) == 4) { - int v; - ReadBigEndian(temp, &v); - return v; - } - return 0; + char temp[4]; + + if (read(temp, 4) == 4) { + int v; + ReadBigEndian(temp, &v); + return v; + } + return 0; } int64 InputStream::readInt64BigEndian() { - char asBytes[8]; - uint64 asInt64; - - if (read(asBytes, 8) == 8) { - ReadBigEndian(asBytes, &asInt64); - return asInt64; - } - return 0; + char asBytes[8]; + uint64 asInt64; + + if (read(asBytes, 8) == 8) { + ReadBigEndian(asBytes, &asInt64); + return asInt64; + } + return 0; } float InputStream::readFloatBigEndian() { - union { - int32 asInt; - float asFloat; - } n; - n.asInt = (int32)readIntBigEndian(); - return n.asFloat; + union { + int32 asInt; + float asFloat; + } n; + n.asInt = (int32)readIntBigEndian(); + return n.asFloat; } double InputStream::readDoubleBigEndian() { - union { - int64 asInt; - double asDouble; - } n; - n.asInt = readInt64BigEndian(); - return n.asDouble; + union { + int64 asInt; + double asDouble; + } n; + n.asInt = readInt64BigEndian(); + return n.asDouble; } size_t InputStream::readIntoMemoryBlock(MemoryBlock& block, size_t numBytes) { - MemoryOutputStream mo(block, true); - return (size_t)mo.writeFromInputStream(*this, numBytes); + MemoryOutputStream mo(block, true); + return (size_t)mo.writeFromInputStream(*this, numBytes); } //============================================================================== void InputStream::skipNextBytes(int64 numBytesToSkip) { - if (numBytesToSkip > 0) { - const int skipBufferSize = (int)std::min(numBytesToSkip, (int64)16384); - char* temp = static_cast(std::malloc(skipBufferSize * sizeof(char))); + if (numBytesToSkip > 0) { + const int skipBufferSize = (int)std::min(numBytesToSkip, (int64)16384); + char* temp = static_cast(std::malloc(skipBufferSize * sizeof(char))); - while (numBytesToSkip > 0 && !isExhausted()) - numBytesToSkip -= - read(temp, (int)std::min(numBytesToSkip, (int64)skipBufferSize)); + while (numBytesToSkip > 0 && !isExhausted()) + numBytesToSkip -= read(temp, (int)std::min(numBytesToSkip, (int64)skipBufferSize)); - std::free(temp); - } + std::free(temp); + } } } diff --git a/src/common/InputStream.h b/src/common/InputStream.h old mode 100755 new mode 100644 index d3916c481..750fef1bd --- a/src/common/InputStream.h +++ b/src/common/InputStream.h @@ -30,166 +30,165 @@ */ namespace rocketmq { class ROCKETMQCLIENT_API InputStream { - public: - /** Destructor. */ - virtual ~InputStream() {} - - //============================================================================== - /** Returns the total number of bytes available for reading in this stream. - - Note that this is the number of bytes available from the start of the - stream, not from the current position. - - If the size of the stream isn't actually known, this will return -1. - - @see getNumBytesRemaining - */ - virtual int64 getTotalLength() = 0; - - /** Returns the number of bytes available for reading, or a negative value if - the remaining length is not known. - @see getTotalLength - */ - int64 getNumBytesRemaining(); - - /** Returns true if the stream has no more data to read. */ - virtual bool isExhausted() = 0; - - //============================================================================== - /** Reads some data from the stream into a memory buffer. - - This is the only read method that subclasses actually need to implement, - as the - InputStream base class implements the other read methods in terms of this - one (although - it's often more efficient for subclasses to implement them directly). - - @param destBuffer the destination buffer for the data. This must not - be null. - @param maxBytesToRead the maximum number of bytes to read - make sure - the - memory block passed in is big enough to contain - this - many bytes. This value must not be negative. - - @returns the actual number of bytes that were read, which may be less - than - maxBytesToRead if the stream is exhausted before it gets that - far - */ - virtual int read(void* destBuffer, int maxBytesToRead) = 0; - - /** Reads a byte from the stream. - If the stream is exhausted, this will return zero. - @see OutputStream::writeByte - */ - virtual char readByte(); - - /** Reads a boolean from the stream. - The bool is encoded as a single byte - non-zero for true, 0 for false. - If the stream is exhausted, this will return false. - @see OutputStream::writeBool - */ - virtual bool readBool(); - - /** Reads two bytes from the stream as a little-endian 16-bit value. - If the next two bytes read are byte1 and byte2, this returns (byte2 | - (byte1 << 8)). - If the stream is exhausted partway through reading the bytes, this will - return zero. - @see OutputStream::writeShortBigEndian, readShort - */ - virtual short readShortBigEndian(); - - /** Reads four bytes from the stream as a big-endian 32-bit value. - - If the next four bytes are byte1 to byte4, this returns - (byte4 | (byte3 << 8) | (byte2 << 16) | (byte1 << 24)). - - If the stream is exhausted partway through reading the bytes, this will - return zero. - - @see OutputStream::writeIntBigEndian, readInt - */ - virtual int readIntBigEndian(); - - /** Reads eight bytes from the stream as a big-endian 64-bit value. - - If the next eight bytes are byte1 to byte8, this returns - (byte8 | (byte7 << 8) | (byte6 << 16) | (byte5 << 24) | (byte4 << 32) | - (byte3 << 40) | (byte2 << 48) | (byte1 << 56)). - - If the stream is exhausted partway through reading the bytes, this will - return zero. - - @see OutputStream::writeInt64BigEndian, readInt64 - */ - virtual int64 readInt64BigEndian(); - - /** Reads four bytes as a 32-bit floating point value. - The raw 32-bit encoding of the float is read from the stream as a - big-endian int. - If the stream is exhausted partway through reading the bytes, this will - return zero. - @see OutputStream::writeFloatBigEndian, readDoubleBigEndian - */ - virtual float readFloatBigEndian(); - - /** Reads eight bytes as a 64-bit floating point value. - The raw 64-bit encoding of the double is read from the stream as a - big-endian int64. - If the stream is exhausted partway through reading the bytes, this will - return zero. - @see OutputStream::writeDoubleBigEndian, readFloatBigEndian - */ - virtual double readDoubleBigEndian(); - - //==============================================================================whole - // stream and turn it into a string. - /** Reads from the stream and appends the data to a MemoryBlock. - - @param destBlock the block to append the data onto - @param maxNumBytesToRead if this is a positive value, it sets a limit - to the number - of bytes that will be read - if it's negative, - data - will be read until the stream is exhausted. - @returns the number of bytes that were added to the memory block - */ - virtual size_t readIntoMemoryBlock(MemoryBlock& destBlock, - size_t maxNumBytesToRead = -1); - - //============================================================================== - /** Returns the offset of the next byte that will be read from the stream. - @see setPosition - */ - virtual int64 getPosition() = 0; - - /** Tries to move the current read position of the stream. - - The position is an absolute number of bytes from the stream's start. - - Some streams might not be able to do this, in which case they should do - nothing and return false. Others might be able to manage it by resetting - themselves and skipping to the correct position, although this is - obviously a bit slow. - - @returns true if the stream manages to reposition itself correctly - @see getPosition - */ - virtual bool setPosition(int64 newPosition) = 0; - - /** Reads and discards a number of bytes from the stream. - - Some input streams might implement this efficiently, but the base - class will just keep reading data until the requisite number of bytes - have been done. - */ - virtual void skipNextBytes(int64 numBytesToSkip); - - protected: - //============================================================================== - InputStream() {} +public: + /** Destructor. */ + virtual ~InputStream() {} + + //============================================================================== + /** Returns the total number of bytes available for reading in this stream. + + Note that this is the number of bytes available from the start of the + stream, not from the current position. + + If the size of the stream isn't actually known, this will return -1. + + @see getNumBytesRemaining + */ + virtual int64 getTotalLength() = 0; + + /** Returns the number of bytes available for reading, or a negative value if + the remaining length is not known. + @see getTotalLength + */ + int64 getNumBytesRemaining(); + + /** Returns true if the stream has no more data to read. */ + virtual bool isExhausted() = 0; + + //============================================================================== + /** Reads some data from the stream into a memory buffer. + + This is the only read method that subclasses actually need to implement, + as the + InputStream base class implements the other read methods in terms of this + one (although + it's often more efficient for subclasses to implement them directly). + + @param destBuffer the destination buffer for the data. This must not + be null. + @param maxBytesToRead the maximum number of bytes to read - make sure + the + memory block passed in is big enough to contain + this + many bytes. This value must not be negative. + + @returns the actual number of bytes that were read, which may be less + than + maxBytesToRead if the stream is exhausted before it gets that + far + */ + virtual int read(void* destBuffer, int maxBytesToRead) = 0; + + /** Reads a byte from the stream. + If the stream is exhausted, this will return zero. + @see OutputStream::writeByte + */ + virtual char readByte(); + + /** Reads a boolean from the stream. + The bool is encoded as a single byte - non-zero for true, 0 for false. + If the stream is exhausted, this will return false. + @see OutputStream::writeBool + */ + virtual bool readBool(); + + /** Reads two bytes from the stream as a little-endian 16-bit value. + If the next two bytes read are byte1 and byte2, this returns (byte2 | + (byte1 << 8)). + If the stream is exhausted partway through reading the bytes, this will + return zero. + @see OutputStream::writeShortBigEndian, readShort + */ + virtual short readShortBigEndian(); + + /** Reads four bytes from the stream as a big-endian 32-bit value. + + If the next four bytes are byte1 to byte4, this returns + (byte4 | (byte3 << 8) | (byte2 << 16) | (byte1 << 24)). + + If the stream is exhausted partway through reading the bytes, this will + return zero. + + @see OutputStream::writeIntBigEndian, readInt + */ + virtual int readIntBigEndian(); + + /** Reads eight bytes from the stream as a big-endian 64-bit value. + + If the next eight bytes are byte1 to byte8, this returns + (byte8 | (byte7 << 8) | (byte6 << 16) | (byte5 << 24) | (byte4 << 32) | + (byte3 << 40) | (byte2 << 48) | (byte1 << 56)). + + If the stream is exhausted partway through reading the bytes, this will + return zero. + + @see OutputStream::writeInt64BigEndian, readInt64 + */ + virtual int64 readInt64BigEndian(); + + /** Reads four bytes as a 32-bit floating point value. + The raw 32-bit encoding of the float is read from the stream as a + big-endian int. + If the stream is exhausted partway through reading the bytes, this will + return zero. + @see OutputStream::writeFloatBigEndian, readDoubleBigEndian + */ + virtual float readFloatBigEndian(); + + /** Reads eight bytes as a 64-bit floating point value. + The raw 64-bit encoding of the double is read from the stream as a + big-endian int64. + If the stream is exhausted partway through reading the bytes, this will + return zero. + @see OutputStream::writeDoubleBigEndian, readFloatBigEndian + */ + virtual double readDoubleBigEndian(); + + //==============================================================================whole + // stream and turn it into a string. + /** Reads from the stream and appends the data to a MemoryBlock. + + @param destBlock the block to append the data onto + @param maxNumBytesToRead if this is a positive value, it sets a limit + to the number + of bytes that will be read - if it's negative, + data + will be read until the stream is exhausted. + @returns the number of bytes that were added to the memory block + */ + virtual size_t readIntoMemoryBlock(MemoryBlock& destBlock, size_t maxNumBytesToRead = -1); + + //============================================================================== + /** Returns the offset of the next byte that will be read from the stream. + @see setPosition + */ + virtual int64 getPosition() = 0; + + /** Tries to move the current read position of the stream. + + The position is an absolute number of bytes from the stream's start. + + Some streams might not be able to do this, in which case they should do + nothing and return false. Others might be able to manage it by resetting + themselves and skipping to the correct position, although this is + obviously a bit slow. + + @returns true if the stream manages to reposition itself correctly + @see getPosition + */ + virtual bool setPosition(int64 newPosition) = 0; + + /** Reads and discards a number of bytes from the stream. + + Some input streams might implement this efficiently, but the base + class will just keep reading data until the requisite number of bytes + have been done. + */ + virtual void skipNextBytes(int64 numBytesToSkip); + +protected: + //============================================================================== + InputStream() {} }; } #endif // INPUTSTREAM_H_INCLUDED diff --git a/src/common/MQClient.cpp b/src/common/MQClient.cpp index 152968994..e20271a90 100644 --- a/src/common/MQClient.cpp +++ b/src/common/MQClient.cpp @@ -27,118 +27,98 @@ namespace rocketmq { #define ROCKETMQCPP_VERSION "1.0.1" #define BUILD_DATE "03-14-2018" // display version: strings bin/librocketmq.so |grep VERSION -const char *rocketmq_build_time = - "VERSION: " ROCKETMQCPP_VERSION ", BUILD DATE: " BUILD_DATE " "; +const char *rocketmq_build_time = "VERSION: " ROCKETMQCPP_VERSION ", BUILD DATE: " BUILD_DATE " "; //createTopic(key, newTopic, queueNum, m_SessionCredentials); - } catch (MQException &e) { - LOG_ERROR(e.what()); - } +void MQClient::createTopic(const string &key, const string &newTopic, int queueNum) { + try { + getFactory()->createTopic(key, newTopic, queueNum, m_SessionCredentials); + } + catch (MQException &e) { + LOG_ERROR(e.what()); + } } int64 MQClient::earliestMsgStoreTime(const MQMessageQueue &mq) { - return getFactory()->earliestMsgStoreTime(mq, m_SessionCredentials); + return getFactory()->earliestMsgStoreTime(mq, m_SessionCredentials); } -QueryResult MQClient::queryMessage(const string &topic, const string &key, - int maxNum, int64 begin, int64 end) { - return getFactory()->queryMessage(topic, key, maxNum, begin, end, - m_SessionCredentials); +QueryResult MQClient::queryMessage(const string &topic, const string &key, int maxNum, int64 begin, int64 end) { + return getFactory()->queryMessage(topic, key, maxNum, begin, end, m_SessionCredentials); } -int64 MQClient::minOffset(const MQMessageQueue &mq) { - return getFactory()->minOffset(mq, m_SessionCredentials); -} +int64 MQClient::minOffset(const MQMessageQueue &mq) { return getFactory()->minOffset(mq, m_SessionCredentials); } -int64 MQClient::maxOffset(const MQMessageQueue &mq) { - return getFactory()->maxOffset(mq, m_SessionCredentials); -} +int64 MQClient::maxOffset(const MQMessageQueue &mq) { return getFactory()->maxOffset(mq, m_SessionCredentials); } int64 MQClient::searchOffset(const MQMessageQueue &mq, uint64_t timestamp) { - return getFactory()->searchOffset(mq, timestamp, m_SessionCredentials); + return getFactory()->searchOffset(mq, timestamp, m_SessionCredentials); } MQMessageExt *MQClient::viewMessage(const string &msgId) { - return getFactory()->viewMessage(msgId, m_SessionCredentials); + return getFactory()->viewMessage(msgId, m_SessionCredentials); } vector MQClient::getTopicMessageQueueInfo(const string &topic) { - boost::weak_ptr weak_topicPublishInfo( - getFactory()->tryToFindTopicPublishInfo(topic, m_SessionCredentials)); - boost::shared_ptr topicPublishInfo( - weak_topicPublishInfo.lock()); - if (topicPublishInfo) { - return topicPublishInfo->getMessageQueueList(); - } - THROW_MQEXCEPTION( - MQClientException, - "could not find MessageQueue Info of topic: [" + topic + "].", -1); + boost::weak_ptr weak_topicPublishInfo( + getFactory()->tryToFindTopicPublishInfo(topic, m_SessionCredentials)); + boost::shared_ptr topicPublishInfo(weak_topicPublishInfo.lock()); + if (topicPublishInfo) { + return topicPublishInfo->getMessageQueueList(); + } + THROW_MQEXCEPTION(MQClientException, "could not find MessageQueue Info of topic: [" + topic + "].", -1); } void MQClient::start() { - if (getFactory() == NULL) { - m_clientFactory = MQClientManager::getInstance()->getMQClientFactory( - getMQClientId(), m_pullThreadNum, m_tcpConnectTimeout, - m_tcpTransportTryLockTimeout, m_unitName); - } - LOG_INFO( - "MQClient " - "start,groupname:%s,clientID:%s,instanceName:%s,nameserveraddr:%s", - getGroupName().c_str(), getMQClientId().c_str(), - getInstanceName().c_str(), getNamesrvAddr().c_str()); + if (getFactory() == NULL) { + m_clientFactory = MQClientManager::getInstance()->getMQClientFactory( + getMQClientId(), m_pullThreadNum, m_tcpConnectTimeout, m_tcpTransportTryLockTimeout, m_unitName); + } + LOG_INFO( + "MQClient " + "start,groupname:%s,clientID:%s,instanceName:%s,nameserveraddr:%s", + getGroupName().c_str(), getMQClientId().c_str(), getInstanceName().c_str(), getNamesrvAddr().c_str()); } void MQClient::shutdown() { m_clientFactory = NULL; } @@ -147,57 +127,44 @@ MQClientFactory *MQClient::getFactory() const { return m_clientFactory; } bool MQClient::isServiceStateOk() { return m_serviceState == RUNNING; } -void MQClient::setLogLevel(elogLevel inputLevel) { - ALOG_ADAPTER->setLogLevel(inputLevel); -} +void MQClient::setLogLevel(elogLevel inputLevel) { ALOG_ADAPTER->setLogLevel(inputLevel); } elogLevel MQClient::getLogLevel() { return ALOG_ADAPTER->getLogLevel(); } void MQClient::setLogFileSizeAndNum(int fileNum, long perFileSize) { - ALOG_ADAPTER->setLogFileNumAndSize(fileNum, perFileSize); + ALOG_ADAPTER->setLogFileNumAndSize(fileNum, perFileSize); } void MQClient::setTcpTransportPullThreadNum(int num) { - if (num > m_pullThreadNum) { - m_pullThreadNum = num; - } + if (num > m_pullThreadNum) { + m_pullThreadNum = num; + } } -const int MQClient::getTcpTransportPullThreadNum() const { - return m_pullThreadNum; -} +const int MQClient::getTcpTransportPullThreadNum() const { return m_pullThreadNum; } -void MQClient::setTcpTransportConnectTimeout(uint64_t timeout) { - m_tcpConnectTimeout = timeout; -} -const uint64_t MQClient::getTcpTransportConnectTimeout() const { - return m_tcpConnectTimeout; -} +void MQClient::setTcpTransportConnectTimeout(uint64_t timeout) { m_tcpConnectTimeout = timeout; } +const uint64_t MQClient::getTcpTransportConnectTimeout() const { return m_tcpConnectTimeout; } void MQClient::setTcpTransportTryLockTimeout(uint64_t timeout) { - if (timeout < 1000) { - timeout = 1000; - } - m_tcpTransportTryLockTimeout = timeout / 1000; -} -const uint64_t MQClient::getTcpTransportTryLockTimeout() const { - return m_tcpTransportTryLockTimeout; + if (timeout < 1000) { + timeout = 1000; + } + m_tcpTransportTryLockTimeout = timeout / 1000; } +const uint64_t MQClient::getTcpTransportTryLockTimeout() const { return m_tcpTransportTryLockTimeout; } void MQClient::setUnitName(string unitName) { m_unitName = unitName; } const string &MQClient::getUnitName() { return m_unitName; } -void MQClient::setSessionCredentials(const string &input_accessKey, - const string &input_secretKey, +void MQClient::setSessionCredentials(const string &input_accessKey, const string &input_secretKey, const string &input_onsChannel) { - m_SessionCredentials.setAccessKey(input_accessKey); - m_SessionCredentials.setSecretKey(input_secretKey); - m_SessionCredentials.setAuthChannel(input_onsChannel); + m_SessionCredentials.setAccessKey(input_accessKey); + m_SessionCredentials.setSecretKey(input_secretKey); + m_SessionCredentials.setAuthChannel(input_onsChannel); } -const SessionCredentials &MQClient::getSessionCredentials() const { - return m_SessionCredentials; -} +const SessionCredentials &MQClient::getSessionCredentials() const { return m_SessionCredentials; } //(std::malloc(dataSize)); - memcpy(internalCopy, data, dataSize); - data = internalCopy; + std::free(internalCopy); + internalCopy = static_cast(std::malloc(dataSize)); + memcpy(internalCopy, data, dataSize); + data = internalCopy; } MemoryInputStream::~MemoryInputStream() { std::free(internalCopy); } @@ -48,23 +40,23 @@ MemoryInputStream::~MemoryInputStream() { std::free(internalCopy); } int64 MemoryInputStream::getTotalLength() { return (int64)dataSize; } int MemoryInputStream::read(void* const buffer, const int howMany) { - const int num = std::min(howMany, (int)(dataSize - position)); - if (num <= 0) return 0; + const int num = std::min(howMany, (int)(dataSize - position)); + if (num <= 0) return 0; - memcpy((char*)buffer, (char*)data + position, (size_t)num); - position += (unsigned int)num; - return num; + memcpy((char*)buffer, (char*)data + position, (size_t)num); + position += (unsigned int)num; + return num; } bool MemoryInputStream::isExhausted() { return position >= dataSize; } bool MemoryInputStream::setPosition(const int64 pos) { - if (pos < 0) - position = 0; - else - position = (int64)dataSize < pos ? (int64)dataSize : pos; + if (pos < 0) + position = 0; + else + position = (int64)dataSize < pos ? (int64)dataSize : pos; - return true; + return true; } int64 MemoryInputStream::getPosition() { return (int64)position; } diff --git a/src/common/MemoryInputStream.h b/src/common/MemoryInputStream.h old mode 100755 new mode 100644 index f76cadc22..28d9dec43 --- a/src/common/MemoryInputStream.h +++ b/src/common/MemoryInputStream.h @@ -1,19 +1,19 @@ -/* -* 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. -*/ +/* +* 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. +*/ #ifndef MEMORYINPUTSTREAM_H_INCLUDED #define MEMORYINPUTSTREAM_H_INCLUDED @@ -30,68 +30,67 @@ namespace rocketmq { own internal copy of the data when the MemoryInputStream is created. */ class ROCKETMQCLIENT_API MemoryInputStream : public InputStream { - public: - //============================================================================== - /** Creates a MemoryInputStream. +public: + //============================================================================== + /** Creates a MemoryInputStream. - @param sourceData the block of data to use as the stream's - source - @param sourceDataSize the number of bytes in the source data - block - @param keepInternalCopyOfData if false, the stream will just keep a - pointer to - the source data, so this data shouldn't be - changed - for the lifetime of the stream; if this - parameter is - true, the stream will make its own copy of - the - data and use that. - */ - MemoryInputStream(const void* sourceData, size_t sourceDataSize, - bool keepInternalCopyOfData); + @param sourceData the block of data to use as the stream's + source + @param sourceDataSize the number of bytes in the source data + block + @param keepInternalCopyOfData if false, the stream will just keep a + pointer to + the source data, so this data shouldn't be + changed + for the lifetime of the stream; if this + parameter is + true, the stream will make its own copy of + the + data and use that. + */ + MemoryInputStream(const void* sourceData, size_t sourceDataSize, bool keepInternalCopyOfData); - /** Creates a MemoryInputStream. + /** Creates a MemoryInputStream. - @param data a block of data to use as the stream's - source - @param keepInternalCopyOfData if false, the stream will just keep a - reference to - the source data, so this data shouldn't be - changed - for the lifetime of the stream; if this - parameter is - true, the stream will make its own copy of - the - data and use that. - */ - MemoryInputStream(const MemoryBlock& data, bool keepInternalCopyOfData); + @param data a block of data to use as the stream's + source + @param keepInternalCopyOfData if false, the stream will just keep a + reference to + the source data, so this data shouldn't be + changed + for the lifetime of the stream; if this + parameter is + true, the stream will make its own copy of + the + data and use that. + */ + MemoryInputStream(const MemoryBlock& data, bool keepInternalCopyOfData); - /** Destructor. */ - ~MemoryInputStream(); + /** Destructor. */ + ~MemoryInputStream(); - /** Returns a pointer to the source data block from which this stream is - * reading. */ - const void* getData() const { return data; } + /** Returns a pointer to the source data block from which this stream is + * reading. */ + const void* getData() const { return data; } - /** Returns the number of bytes of source data in the block from which this - * stream is reading. */ - size_t getDataSize() const { return dataSize; } + /** Returns the number of bytes of source data in the block from which this + * stream is reading. */ + size_t getDataSize() const { return dataSize; } - //============================================================================== - int64 getPosition(); - bool setPosition(int64 pos); - int64 getTotalLength(); - bool isExhausted(); - int read(void* destBuffer, int maxBytesToRead); + //============================================================================== + int64 getPosition(); + bool setPosition(int64 pos); + int64 getTotalLength(); + bool isExhausted(); + int read(void* destBuffer, int maxBytesToRead); - private: - //============================================================================== - const void* data; - size_t dataSize, position; - char* internalCopy; +private: + //============================================================================== + const void* data; + size_t dataSize, position; + char* internalCopy; - void createInternalCopy(); + void createInternalCopy(); }; } #endif diff --git a/src/common/MemoryOutputStream.cpp b/src/common/MemoryOutputStream.cpp old mode 100755 new mode 100644 index ce74d8e67..be1e4eefd --- a/src/common/MemoryOutputStream.cpp +++ b/src/common/MemoryOutputStream.cpp @@ -1,164 +1,139 @@ -/* -* 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. +/* +* 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 "MemoryOutputStream.h" namespace rocketmq { MemoryOutputStream::MemoryOutputStream(const size_t initialSize) - : blockToUse(&internalBlock), - externalData(NULL), - position(0), - size(0), - availableSize(0) { - internalBlock.setSize(initialSize, false); + : blockToUse(&internalBlock), externalData(NULL), position(0), size(0), availableSize(0) { + internalBlock.setSize(initialSize, false); } -MemoryOutputStream::MemoryOutputStream(MemoryBlock& memoryBlockToWriteTo, - const bool appendToExistingBlockContent) - : blockToUse(&memoryBlockToWriteTo), - externalData(NULL), - position(0), - size(0), - availableSize(0) { - if (appendToExistingBlockContent) - position = size = memoryBlockToWriteTo.getSize(); +MemoryOutputStream::MemoryOutputStream(MemoryBlock& memoryBlockToWriteTo, const bool appendToExistingBlockContent) + : blockToUse(&memoryBlockToWriteTo), externalData(NULL), position(0), size(0), availableSize(0) { + if (appendToExistingBlockContent) position = size = memoryBlockToWriteTo.getSize(); } MemoryOutputStream::MemoryOutputStream(void* destBuffer, size_t destBufferSize) - : blockToUse(NULL), - externalData(destBuffer), - position(0), - size(0), - availableSize(destBufferSize) {} + : blockToUse(NULL), externalData(destBuffer), position(0), size(0), availableSize(destBufferSize) {} MemoryOutputStream::~MemoryOutputStream() { trimExternalBlockSize(); } void MemoryOutputStream::flush() { trimExternalBlockSize(); } void MemoryOutputStream::trimExternalBlockSize() { - if (blockToUse != &internalBlock && blockToUse != NULL) - blockToUse->setSize(size, false); + if (blockToUse != &internalBlock && blockToUse != NULL) blockToUse->setSize(size, false); } void MemoryOutputStream::preallocate(const size_t bytesToPreallocate) { - if (blockToUse != NULL) blockToUse->ensureSize(bytesToPreallocate + 1); + if (blockToUse != NULL) blockToUse->ensureSize(bytesToPreallocate + 1); } void MemoryOutputStream::reset() { - position = 0; - size = 0; + position = 0; + size = 0; } char* MemoryOutputStream::prepareToWrite(size_t numBytes) { - size_t storageNeeded = position + numBytes; + size_t storageNeeded = position + numBytes; - char* data; + char* data; - if (blockToUse != NULL) { - if (storageNeeded >= (unsigned int)(blockToUse->getSize())) - blockToUse->ensureSize( - (storageNeeded + std::min(storageNeeded / 2, (size_t)(1024 * 1024)) + - 32) & - ~31u); + if (blockToUse != NULL) { + if (storageNeeded >= (unsigned int)(blockToUse->getSize())) + blockToUse->ensureSize((storageNeeded + std::min(storageNeeded / 2, (size_t)(1024 * 1024)) + 32) & ~31u); - data = static_cast(blockToUse->getData()); - } else { - if (storageNeeded > availableSize) return NULL; + data = static_cast(blockToUse->getData()); + } else { + if (storageNeeded > availableSize) return NULL; - data = static_cast(externalData); - } + data = static_cast(externalData); + } - char* const writePointer = data + position; - position += numBytes; - size = std::max(size, position); - return writePointer; + char* const writePointer = data + position; + position += numBytes; + size = std::max(size, position); + return writePointer; } bool MemoryOutputStream::write(const void* const buffer, size_t howMany) { - if (howMany == 0) return true; + if (howMany == 0) return true; - if (char* dest = prepareToWrite(howMany)) { - memcpy(dest, buffer, howMany); - return true; - } + if (char* dest = prepareToWrite(howMany)) { + memcpy(dest, buffer, howMany); + return true; + } - return false; + return false; } bool MemoryOutputStream::writeRepeatedByte(uint8 byte, size_t howMany) { - if (howMany == 0) return true; + if (howMany == 0) return true; - if (char* dest = prepareToWrite(howMany)) { - memset(dest, byte, howMany); - return true; - } + if (char* dest = prepareToWrite(howMany)) { + memset(dest, byte, howMany); + return true; + } - return false; + return false; } -MemoryBlock MemoryOutputStream::getMemoryBlock() const { - return MemoryBlock(getData(), getDataSize()); -} +MemoryBlock MemoryOutputStream::getMemoryBlock() const { return MemoryBlock(getData(), getDataSize()); } const void* MemoryOutputStream::getData() const { - if (blockToUse == NULL) return externalData; + if (blockToUse == NULL) return externalData; - if ((unsigned int)blockToUse->getSize() > size) - static_cast(blockToUse->getData())[size] = 0; + if ((unsigned int)blockToUse->getSize() > size) static_cast(blockToUse->getData())[size] = 0; - return blockToUse->getData(); + return blockToUse->getData(); } bool MemoryOutputStream::setPosition(int64 newPosition) { - if (newPosition <= (int64)size) { - // ok to seek backwards - if (newPosition < 0) - position = 0; - else - position = (int64)size < newPosition ? size : newPosition; - return true; - } - - // can't move beyond the end of the stream.. - return false; + if (newPosition <= (int64)size) { + // ok to seek backwards + if (newPosition < 0) + position = 0; + else + position = (int64)size < newPosition ? size : newPosition; + return true; + } + + // can't move beyond the end of the stream.. + return false; } -int64 MemoryOutputStream::writeFromInputStream(InputStream& source, - int64 maxNumBytesToWrite) { - // before writing from an input, see if we can preallocate to make it more - // efficient.. - int64 availableData = source.getTotalLength() - source.getPosition(); +int64 MemoryOutputStream::writeFromInputStream(InputStream& source, int64 maxNumBytesToWrite) { + // before writing from an input, see if we can preallocate to make it more + // efficient.. + int64 availableData = source.getTotalLength() - source.getPosition(); - if (availableData > 0) { - if (maxNumBytesToWrite > availableData || maxNumBytesToWrite < 0) - maxNumBytesToWrite = availableData; + if (availableData > 0) { + if (maxNumBytesToWrite > availableData || maxNumBytesToWrite < 0) maxNumBytesToWrite = availableData; - if (blockToUse != NULL) - preallocate(blockToUse->getSize() + (size_t)maxNumBytesToWrite); - } + if (blockToUse != NULL) preallocate(blockToUse->getSize() + (size_t)maxNumBytesToWrite); + } - return OutputStream::writeFromInputStream(source, maxNumBytesToWrite); + return OutputStream::writeFromInputStream(source, maxNumBytesToWrite); } -OutputStream& operator<<(OutputStream& stream, - const MemoryOutputStream& streamToRead) { - const size_t dataSize = streamToRead.getDataSize(); +OutputStream& operator<<(OutputStream& stream, const MemoryOutputStream& streamToRead) { + const size_t dataSize = streamToRead.getDataSize(); - if (dataSize > 0) stream.write(streamToRead.getData(), dataSize); + if (dataSize > 0) stream.write(streamToRead.getData(), dataSize); - return stream; + return stream; } } diff --git a/src/common/MemoryOutputStream.h b/src/common/MemoryOutputStream.h old mode 100755 new mode 100644 index 6d9098f78..0ef85512d --- a/src/common/MemoryOutputStream.h +++ b/src/common/MemoryOutputStream.h @@ -1,19 +1,19 @@ -/* -* 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. -*/ +/* +* 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. +*/ #ifndef MEMORYOUTPUTSTREAM_H_INCLUDED #define MEMORYOUTPUTSTREAM_H_INCLUDED @@ -29,104 +29,102 @@ namespace rocketmq { a contiguous block of memory. */ class ROCKETMQCLIENT_API MemoryOutputStream : public OutputStream { - public: - //============================================================================== - /** Creates an empty memory stream, ready to be written into. - @param initialSize the intial amount of capacity to allocate for writing - into - */ - MemoryOutputStream(size_t initialSize = 256); - - /** Creates a memory stream for writing into into a pre-existing MemoryBlock - object. - - Note that the destination block will always be larger than the amount of - data - that has been written to the stream, because the MemoryOutputStream keeps - some - spare capactity at its end. To trim the block's size down to fit the - actual - data, call flush(), or delete the MemoryOutputStream. - - @param memoryBlockToWriteTo the block into which new data will - be written. - @param appendToExistingBlockContent if this is true, the contents of - the block will be - kept, and new data will be - appended to it. If false, - the block will be cleared before - use - */ - MemoryOutputStream(MemoryBlock& memoryBlockToWriteTo, - bool appendToExistingBlockContent); - - /** Creates a MemoryOutputStream that will write into a user-supplied, - fixed-size - block of memory. - When using this mode, the stream will write directly into this memory area - until - it's full, at which point write operations will fail. - */ - MemoryOutputStream(void* destBuffer, size_t destBufferSize); - - /** Destructor. - This will free any data that was written to it. - */ - ~MemoryOutputStream(); - - //============================================================================== - /** Returns a pointer to the data that has been written to the stream. - @see getDataSize - */ - const void* getData() const; - - /** Returns the number of bytes of data that have been written to the stream. - @see getData - */ - size_t getDataSize() const { return size; } - - /** Resets the stream, clearing any data that has been written to it so far. - */ - void reset(); - - /** Increases the internal storage capacity to be able to contain at least the - specified - amount of data without needing to be resized. - */ - void preallocate(size_t bytesToPreallocate); - - /** Returns a copy of the stream's data as a memory block. */ - MemoryBlock getMemoryBlock() const; - - //============================================================================== - /** If the stream is writing to a user-supplied MemoryBlock, this will trim - any excess - capacity off the block, so that its length matches the amount of actual - data that - has been written so far. - */ - void flush(); - - bool write(const void*, size_t); - int64 getPosition() { return (int64)position; } - bool setPosition(int64); - int64 writeFromInputStream(InputStream&, int64 maxNumBytesToWrite); - bool writeRepeatedByte(uint8 byte, size_t numTimesToRepeat); - - private: - //============================================================================== - MemoryBlock* const blockToUse; - MemoryBlock internalBlock; - void* externalData; - size_t position, size, availableSize; - - void trimExternalBlockSize(); - char* prepareToWrite(size_t); +public: + //============================================================================== + /** Creates an empty memory stream, ready to be written into. + @param initialSize the intial amount of capacity to allocate for writing + into + */ + MemoryOutputStream(size_t initialSize = 256); + + /** Creates a memory stream for writing into into a pre-existing MemoryBlock + object. + + Note that the destination block will always be larger than the amount of + data + that has been written to the stream, because the MemoryOutputStream keeps + some + spare capactity at its end. To trim the block's size down to fit the + actual + data, call flush(), or delete the MemoryOutputStream. + + @param memoryBlockToWriteTo the block into which new data will + be written. + @param appendToExistingBlockContent if this is true, the contents of + the block will be + kept, and new data will be + appended to it. If false, + the block will be cleared before + use + */ + MemoryOutputStream(MemoryBlock& memoryBlockToWriteTo, bool appendToExistingBlockContent); + + /** Creates a MemoryOutputStream that will write into a user-supplied, + fixed-size + block of memory. + When using this mode, the stream will write directly into this memory area + until + it's full, at which point write operations will fail. + */ + MemoryOutputStream(void* destBuffer, size_t destBufferSize); + + /** Destructor. + This will free any data that was written to it. + */ + ~MemoryOutputStream(); + + //============================================================================== + /** Returns a pointer to the data that has been written to the stream. + @see getDataSize + */ + const void* getData() const; + + /** Returns the number of bytes of data that have been written to the stream. + @see getData + */ + size_t getDataSize() const { return size; } + + /** Resets the stream, clearing any data that has been written to it so far. + */ + void reset(); + + /** Increases the internal storage capacity to be able to contain at least the + specified + amount of data without needing to be resized. + */ + void preallocate(size_t bytesToPreallocate); + + /** Returns a copy of the stream's data as a memory block. */ + MemoryBlock getMemoryBlock() const; + + //============================================================================== + /** If the stream is writing to a user-supplied MemoryBlock, this will trim + any excess + capacity off the block, so that its length matches the amount of actual + data that + has been written so far. + */ + void flush(); + + bool write(const void*, size_t); + int64 getPosition() { return (int64)position; } + bool setPosition(int64); + int64 writeFromInputStream(InputStream&, int64 maxNumBytesToWrite); + bool writeRepeatedByte(uint8 byte, size_t numTimesToRepeat); + +private: + //============================================================================== + MemoryBlock* const blockToUse; + MemoryBlock internalBlock; + void* externalData; + size_t position, size, availableSize; + + void trimExternalBlockSize(); + char* prepareToWrite(size_t); }; /** Copies all the data that has been written to a MemoryOutputStream into * another stream. */ -OutputStream& operator<<(OutputStream& stream, - const MemoryOutputStream& streamToRead); +OutputStream& operator<<(OutputStream& stream, const MemoryOutputStream& streamToRead); } #endif // MEMORYOUTPUTSTREAM_H_INCLUDED diff --git a/src/common/MessageSysFlag.cpp b/src/common/MessageSysFlag.cpp old mode 100755 new mode 100644 index 50a28b476..dc20a6c8b --- a/src/common/MessageSysFlag.cpp +++ b/src/common/MessageSysFlag.cpp @@ -25,13 +25,9 @@ int MessageSysFlag::TransactionPreparedType = (0x1 << 2); int MessageSysFlag::TransactionCommitType = (0x2 << 2); int MessageSysFlag::TransactionRollbackType = (0x3 << 2); -int MessageSysFlag::getTransactionValue(int flag) { - return flag & TransactionRollbackType; -} +int MessageSysFlag::getTransactionValue(int flag) { return flag & TransactionRollbackType; } -int MessageSysFlag::resetTransactionValue(int flag, int type) { - return (flag & (~TransactionRollbackType)) | type; -} +int MessageSysFlag::resetTransactionValue(int flag, int type) { return (flag & (~TransactionRollbackType)) | type; } // @@ -25,77 +25,73 @@ OutputStream::OutputStream() {} OutputStream::~OutputStream() {} //============================================================================== -bool OutputStream::writeBool(const bool b) { - return writeByte(b ? (char)1 : (char)0); -} +bool OutputStream::writeBool(const bool b) { return writeByte(b ? (char)1 : (char)0); } bool OutputStream::writeByte(char byte) { return write(&byte, 1); } bool OutputStream::writeRepeatedByte(uint8 byte, size_t numTimesToRepeat) { - for (size_t i = 0; i < numTimesToRepeat; ++i) - if (!writeByte((char)byte)) return false; + for (size_t i = 0; i < numTimesToRepeat; ++i) + if (!writeByte((char)byte)) return false; - return true; + return true; } bool OutputStream::writeShortBigEndian(short value) { - unsigned short v; - char pShort[sizeof(v)]; - WriteBigEndian(pShort, (unsigned short)value); - return write(pShort, 2); + unsigned short v; + char pShort[sizeof(v)]; + WriteBigEndian(pShort, (unsigned short)value); + return write(pShort, 2); } bool OutputStream::writeIntBigEndian(int value) { - unsigned int v; - char pInt[sizeof(v)]; - WriteBigEndian(pInt, (unsigned int)value); - return write(pInt, 4); + unsigned int v; + char pInt[sizeof(v)]; + WriteBigEndian(pInt, (unsigned int)value); + return write(pInt, 4); } bool OutputStream::writeInt64BigEndian(int64 value) { - uint64 v; - char pUint64[sizeof(v)]; - WriteBigEndian(pUint64, (uint64)value); - return write(pUint64, 8); + uint64 v; + char pUint64[sizeof(v)]; + WriteBigEndian(pUint64, (uint64)value); + return write(pUint64, 8); } bool OutputStream::writeFloatBigEndian(float value) { - union { - int asInt; - float asFloat; - } n; - n.asFloat = value; - return writeIntBigEndian(n.asInt); + union { + int asInt; + float asFloat; + } n; + n.asFloat = value; + return writeIntBigEndian(n.asInt); } bool OutputStream::writeDoubleBigEndian(double value) { - union { - int64 asInt; - double asDouble; - } n; - n.asDouble = value; - return writeInt64BigEndian(n.asInt); + union { + int64 asInt; + double asDouble; + } n; + n.asDouble = value; + return writeInt64BigEndian(n.asInt); } -int64 OutputStream::writeFromInputStream(InputStream& source, - int64 numBytesToWrite) { - if (numBytesToWrite < 0) numBytesToWrite = std::numeric_limits::max(); +int64 OutputStream::writeFromInputStream(InputStream& source, int64 numBytesToWrite) { + if (numBytesToWrite < 0) numBytesToWrite = std::numeric_limits::max(); - int64 numWritten = 0; + int64 numWritten = 0; - while (numBytesToWrite > 0) { - char buffer[8192]; - const int num = source.read( - buffer, (int)std::min(numBytesToWrite, (int64)sizeof(buffer))); + while (numBytesToWrite > 0) { + char buffer[8192]; + const int num = source.read(buffer, (int)std::min(numBytesToWrite, (int64)sizeof(buffer))); - if (num <= 0) break; + if (num <= 0) break; - write(buffer, (size_t)num); + write(buffer, (size_t)num); - numBytesToWrite -= num; - numWritten += num; - } + numBytesToWrite -= num; + numWritten += num; + } - return numWritten; + return numWritten; } } diff --git a/src/common/OutputStream.h b/src/common/OutputStream.h old mode 100755 new mode 100644 index 30ca334fd..449afc124 --- a/src/common/OutputStream.h +++ b/src/common/OutputStream.h @@ -31,118 +31,117 @@ namespace rocketmq { @see InputStream, MemoryOutputStream, FileOutputStream */ class ROCKETMQCLIENT_API OutputStream { - protected: - //============================================================================== - OutputStream(); - - public: - /** Destructor. - - Some subclasses might want to do things like call flush() during their - destructors. - */ - virtual ~OutputStream(); - - //============================================================================== - /** If the stream is using a buffer, this will ensure it gets written - out to the destination. */ - virtual void flush() = 0; - - /** Tries to move the stream's output position. - - Not all streams will be able to seek to a new position - this will return - false if it fails to work. - - @see getPosition - */ - virtual bool setPosition(int64 newPosition) = 0; - - /** Returns the stream's current position. - - @see setPosition - */ - virtual int64 getPosition() = 0; - - //============================================================================== - /** Writes a block of data to the stream. - - When creating a subclass of OutputStream, this is the only write method - that needs to be overloaded - the base class has methods for writing other - types of data which use this to do the work. - - @param dataToWrite the target buffer to receive the data. This must - not be null. - @param numberOfBytes the number of bytes to write. - @returns false if the write operation fails for some reason - */ - virtual bool write(const void* dataToWrite, size_t numberOfBytes) = 0; - - //============================================================================== - /** Writes a single byte to the stream. - @returns false if the write operation fails for some reason - @see InputStream::readByte - */ - virtual bool writeByte(char byte); - - /** Writes a boolean to the stream as a single byte. - This is encoded as a binary byte (not as text) with a value of 1 or 0. - @returns false if the write operation fails for some reason - @see InputStream::readBool - */ - virtual bool writeBool(bool boolValue); - - /** Writes a 16-bit integer to the stream in a big-endian byte order. - This will write two bytes to the stream: (value >> 8), then (value & - 0xff). - @returns false if the write operation fails for some reason - @see InputStream::readShortBigEndian - */ - virtual bool writeShortBigEndian(short value); - - /** Writes a 32-bit integer to the stream in a big-endian byte order. - @returns false if the write operation fails for some reason - @see InputStream::readIntBigEndian - */ - virtual bool writeIntBigEndian(int value); - - /** Writes a 64-bit integer to the stream in a big-endian byte order. - @returns false if the write operation fails for some reason - @see InputStream::readInt64BigEndian - */ - virtual bool writeInt64BigEndian(int64 value); - - /** Writes a 32-bit floating point value to the stream in a binary format. - The binary 32-bit encoding of the float is written as a big-endian int. - @returns false if the write operation fails for some reason - @see InputStream::readFloatBigEndian - */ - virtual bool writeFloatBigEndian(float value); - - /** Writes a 64-bit floating point value to the stream in a binary format. - The eight raw bytes of the double value are written out as a big-endian - 64-bit int. - @see InputStream::readDoubleBigEndian - @returns false if the write operation fails for some reason - */ - virtual bool writeDoubleBigEndian(double value); - - /** Writes a byte to the output stream a given number of times. - @returns false if the write operation fails for some reason - */ - virtual bool writeRepeatedByte(uint8 byte, size_t numTimesToRepeat); - - /** Reads data from an input stream and writes it to this stream. - - @param source the stream to read from - @param maxNumBytesToWrite the number of bytes to read from the stream - (if this is - less than zero, it will keep reading until the - input - is exhausted) - @returns the number of bytes written - */ - virtual int64 writeFromInputStream(InputStream& source, - int64 maxNumBytesToWrite); +protected: + //============================================================================== + OutputStream(); + +public: + /** Destructor. + + Some subclasses might want to do things like call flush() during their + destructors. + */ + virtual ~OutputStream(); + + //============================================================================== + /** If the stream is using a buffer, this will ensure it gets written + out to the destination. */ + virtual void flush() = 0; + + /** Tries to move the stream's output position. + + Not all streams will be able to seek to a new position - this will return + false if it fails to work. + + @see getPosition + */ + virtual bool setPosition(int64 newPosition) = 0; + + /** Returns the stream's current position. + + @see setPosition + */ + virtual int64 getPosition() = 0; + + //============================================================================== + /** Writes a block of data to the stream. + + When creating a subclass of OutputStream, this is the only write method + that needs to be overloaded - the base class has methods for writing other + types of data which use this to do the work. + + @param dataToWrite the target buffer to receive the data. This must + not be null. + @param numberOfBytes the number of bytes to write. + @returns false if the write operation fails for some reason + */ + virtual bool write(const void* dataToWrite, size_t numberOfBytes) = 0; + + //============================================================================== + /** Writes a single byte to the stream. + @returns false if the write operation fails for some reason + @see InputStream::readByte + */ + virtual bool writeByte(char byte); + + /** Writes a boolean to the stream as a single byte. + This is encoded as a binary byte (not as text) with a value of 1 or 0. + @returns false if the write operation fails for some reason + @see InputStream::readBool + */ + virtual bool writeBool(bool boolValue); + + /** Writes a 16-bit integer to the stream in a big-endian byte order. + This will write two bytes to the stream: (value >> 8), then (value & + 0xff). + @returns false if the write operation fails for some reason + @see InputStream::readShortBigEndian + */ + virtual bool writeShortBigEndian(short value); + + /** Writes a 32-bit integer to the stream in a big-endian byte order. + @returns false if the write operation fails for some reason + @see InputStream::readIntBigEndian + */ + virtual bool writeIntBigEndian(int value); + + /** Writes a 64-bit integer to the stream in a big-endian byte order. + @returns false if the write operation fails for some reason + @see InputStream::readInt64BigEndian + */ + virtual bool writeInt64BigEndian(int64 value); + + /** Writes a 32-bit floating point value to the stream in a binary format. + The binary 32-bit encoding of the float is written as a big-endian int. + @returns false if the write operation fails for some reason + @see InputStream::readFloatBigEndian + */ + virtual bool writeFloatBigEndian(float value); + + /** Writes a 64-bit floating point value to the stream in a binary format. + The eight raw bytes of the double value are written out as a big-endian + 64-bit int. + @see InputStream::readDoubleBigEndian + @returns false if the write operation fails for some reason + */ + virtual bool writeDoubleBigEndian(double value); + + /** Writes a byte to the output stream a given number of times. + @returns false if the write operation fails for some reason + */ + virtual bool writeRepeatedByte(uint8 byte, size_t numTimesToRepeat); + + /** Reads data from an input stream and writes it to this stream. + + @param source the stream to read from + @param maxNumBytesToWrite the number of bytes to read from the stream + (if this is + less than zero, it will keep reading until the + input + is exhausted) + @returns the number of bytes written + */ + virtual int64 writeFromInputStream(InputStream& source, int64 maxNumBytesToWrite); }; } diff --git a/src/common/PermName.cpp b/src/common/PermName.cpp old mode 100755 new mode 100644 index 7f168a411..1291442f9 --- a/src/common/PermName.cpp +++ b/src/common/PermName.cpp @@ -26,29 +26,25 @@ int PermName::PERM_INHERIT = 0x1 << 0; bool PermName::isReadable(int perm) { return (perm & PERM_READ) == PERM_READ; } -bool PermName::isWriteable(int perm) { - return (perm & PERM_WRITE) == PERM_WRITE; -} +bool PermName::isWriteable(int perm) { return (perm & PERM_WRITE) == PERM_WRITE; } -bool PermName::isInherited(int perm) { - return (perm & PERM_INHERIT) == PERM_INHERIT; -} +bool PermName::isInherited(int perm) { return (perm & PERM_INHERIT) == PERM_INHERIT; } string PermName::perm2String(int perm) { - string pm("---"); - if (isReadable(perm)) { - pm.replace(0, 1, "R"); - } + string pm("---"); + if (isReadable(perm)) { + pm.replace(0, 1, "R"); + } - if (isWriteable(perm)) { - pm.replace(1, 2, "W"); - } + if (isWriteable(perm)) { + pm.replace(1, 2, "W"); + } - if (isInherited(perm)) { - pm.replace(2, 3, "X"); - } + if (isInherited(perm)) { + pm.replace(2, 3, "X"); + } - return pm; + return pm; } //groupName = groupName; - consumeEnable = true; - consumeFromMinEnable = true; - consumeBroadcastEnable = true; - retryQueueNums = 1; - retryMaxTimes = 5; - brokerId = MASTER_ID; - whichBrokerWhenConsumeSlowly = 1; - } +public: + SubscriptionGroupConfig(const string& groupName) { + this->groupName = groupName; + consumeEnable = true; + consumeFromMinEnable = true; + consumeBroadcastEnable = true; + retryQueueNums = 1; + retryMaxTimes = 5; + brokerId = MASTER_ID; + whichBrokerWhenConsumeSlowly = 1; + } - string groupName; - bool consumeEnable; - bool consumeFromMinEnable; - bool consumeBroadcastEnable; - int retryQueueNums; - int retryMaxTimes; - int brokerId; - int whichBrokerWhenConsumeSlowly; + string groupName; + bool consumeEnable; + bool consumeFromMinEnable; + bool consumeBroadcastEnable; + int retryQueueNums; + int retryMaxTimes; + int brokerId; + int whichBrokerWhenConsumeSlowly; }; // '9') && (*sValue != '.')) return -1; - sValue++; - } - return 0; + while (*sValue != '\0') { + if ((*sValue < '0' || *sValue > '9') && (*sValue != '.')) return -1; + sValue++; + } + return 0; } void TopAddressing::updateNameServerAddressList(const string& adds) { - boost::lock_guard lock(m_addrLock); - vector out; - UtilAll::Split(out, adds, ";"); - if (out.size() > 0) m_addrs.clear(); - for (size_t i = 0; i < out.size(); i++) { - string addr = out[i]; - UtilAll::Trim(addr); + boost::lock_guard lock(m_addrLock); + vector out; + UtilAll::Split(out, adds, ";"); + if (out.size() > 0) m_addrs.clear(); + for (size_t i = 0; i < out.size(); i++) { + string addr = out[i]; + UtilAll::Trim(addr); - list::iterator findit = find(m_addrs.begin(), m_addrs.end(), addr); - if (findit == m_addrs.end()) { - string hostName; - short portNumber; - if (UtilAll::SplitURL(addr, hostName, portNumber)) { - LOG_INFO("updateNameServerAddressList:%s", addr.c_str()); - m_addrs.push_back(addr); - } + list::iterator findit = find(m_addrs.begin(), m_addrs.end(), addr); + if (findit == m_addrs.end()) { + string hostName; + short portNumber; + if (UtilAll::SplitURL(addr, hostName, portNumber)) { + LOG_INFO("updateNameServerAddressList:%s", addr.c_str()); + m_addrs.push_back(addr); + } + } } - } } string TopAddressing::fetchNSAddr(const string& NSDomain) { - LOG_DEBUG("fetchNSAddr begin"); - string nsAddr = NSDomain.empty() ? WS_ADDR : NSDomain; - if (!m_unitName.empty()) { - nsAddr = nsAddr + "-" + m_unitName + "?nofix=1"; - LOG_INFO("NSAddr is:%s", nsAddr.c_str()); - } + LOG_DEBUG("fetchNSAddr begin"); + string nsAddr = NSDomain.empty() ? WS_ADDR : NSDomain; + if (!m_unitName.empty()) { + nsAddr = nsAddr + "-" + m_unitName + "?nofix=1"; + LOG_INFO("NSAddr is:%s", nsAddr.c_str()); + } - std::string tmp_nameservers; - std::string nameservers; - Url url_s(nsAddr); - LOG_INFO("fetchNSAddr protocol: %s, port: %s, host:%s, path:%s, ", - url_s.protocol_.c_str(), url_s.port_.c_str(), url_s.host_.c_str(), - url_s.path_.c_str()); + std::string tmp_nameservers; + std::string nameservers; + Url url_s(nsAddr); + LOG_INFO("fetchNSAddr protocol: %s, port: %s, host:%s, path:%s, ", url_s.protocol_.c_str(), url_s.port_.c_str(), + url_s.host_.c_str(), url_s.path_.c_str()); - bool ret = SyncfetchNsAddr(url_s, tmp_nameservers); - if (ret) { - nameservers = clearNewLine(tmp_nameservers); - if (nameservers.empty()) { - LOG_ERROR("fetchNSAddr with domain is empty"); + bool ret = SyncfetchNsAddr(url_s, tmp_nameservers); + if (ret) { + nameservers = clearNewLine(tmp_nameservers); + if (nameservers.empty()) { + LOG_ERROR("fetchNSAddr with domain is empty"); + } else { + updateNameServerAddressList(nameservers); + } } else { - updateNameServerAddressList(nameservers); + LOG_ERROR("fetchNSAddr with domain failed, connect failure or wrong response"); } - } else { - LOG_ERROR( - "fetchNSAddr with domain failed, connect failure or wrong response"); - } - return nameservers; + return nameservers; } string TopAddressing::clearNewLine(const string& str) { - string newString = str; - size_t index = newString.find("\r"); - if (index != string::npos) { - return newString.substr(0, index); - } + string newString = str; + size_t index = newString.find("\r"); + if (index != string::npos) { + return newString.substr(0, index); + } - index = newString.find("\n"); - if (index != string::npos) { - return newString.substr(0, index); - } + index = newString.find("\n"); + if (index != string::npos) { + return newString.substr(0, index); + } - return newString; + return newString; } } // m_addrs; - string m_unitName; +private: + boost::mutex m_addrLock; + list m_addrs; + string m_unitName; }; } #endif diff --git a/src/common/TopicConfig.cpp b/src/common/TopicConfig.cpp old mode 100755 new mode 100644 index e0e1b4d42..1dac56cb5 --- a/src/common/TopicConfig.cpp +++ b/src/common/TopicConfig.cpp @@ -39,8 +39,7 @@ TopicConfig::TopicConfig(const string& topicName) m_perm(PermName::PERM_READ | PermName::PERM_WRITE), m_topicFilterType(SINGLE_TAG) {} -TopicConfig::TopicConfig(const string& topicName, int readQueueNums, - int writeQueueNums, int perm) +TopicConfig::TopicConfig(const string& topicName, int readQueueNums, int writeQueueNums, int perm) : m_topicName(topicName), m_readQueueNums(readQueueNums), m_writeQueueNums(writeQueueNums), @@ -50,46 +49,39 @@ TopicConfig::TopicConfig(const string& topicName, int readQueueNums, TopicConfig::~TopicConfig() {} string TopicConfig::encode() { - stringstream ss; - ss << m_topicName << SEPARATOR << m_readQueueNums << SEPARATOR - << m_writeQueueNums << SEPARATOR << m_perm << SEPARATOR - << m_topicFilterType; + stringstream ss; + ss << m_topicName << SEPARATOR << m_readQueueNums << SEPARATOR << m_writeQueueNums << SEPARATOR << m_perm + << SEPARATOR << m_topicFilterType; - return ss.str(); + return ss.str(); } bool TopicConfig::decode(const string& in) { - stringstream ss(in); + stringstream ss(in); - ss >> m_topicName; - ss >> m_readQueueNums; - ss >> m_writeQueueNums; - ss >> m_perm; + ss >> m_topicName; + ss >> m_readQueueNums; + ss >> m_writeQueueNums; + ss >> m_perm; - int type; - ss >> type; - m_topicFilterType = (TopicFilterType)type; + int type; + ss >> type; + m_topicFilterType = (TopicFilterType)type; - return true; + return true; } const string& TopicConfig::getTopicName() { return m_topicName; } -void TopicConfig::setTopicName(const string& topicName) { - m_topicName = topicName; -} +void TopicConfig::setTopicName(const string& topicName) { m_topicName = topicName; } int TopicConfig::getReadQueueNums() { return m_readQueueNums; } -void TopicConfig::setReadQueueNums(int readQueueNums) { - m_readQueueNums = readQueueNums; -} +void TopicConfig::setReadQueueNums(int readQueueNums) { m_readQueueNums = readQueueNums; } int TopicConfig::getWriteQueueNums() { return m_writeQueueNums; } -void TopicConfig::setWriteQueueNums(int writeQueueNums) { - m_writeQueueNums = writeQueueNums; -} +void TopicConfig::setWriteQueueNums(int writeQueueNums) { m_writeQueueNums = writeQueueNums; } int TopicConfig::getPerm() { return m_perm; } @@ -97,8 +89,6 @@ void TopicConfig::setPerm(int perm) { m_perm = perm; } TopicFilterType TopicConfig::getTopicFilterType() { return m_topicFilterType; } -void TopicConfig::setTopicFilterType(TopicFilterType topicFilterType) { - m_topicFilterType = topicFilterType; -} +void TopicConfig::setTopicFilterType(TopicFilterType topicFilterType) { m_topicFilterType = topicFilterType; } //(str); -} +int64 UtilAll::str2ll(const char *str) { return boost::lexical_cast(str); } string UtilAll::bytes2string(const char *bytes, int len) { - if (bytes == NULL || len <= 0) { - return string(); - } + if (bytes == NULL || len <= 0) { + return string(); + } #ifdef WIN32 - string buffer; - for (int i = 0; i < len; i++) { - char tmp[3]; - sprintf(tmp, "%02X", (unsigned char)bytes[i]); - buffer.append(tmp); - } - - return buffer; + string buffer; + for (int i = 0; i < len; i++) { + char tmp[3]; + sprintf(tmp, "%02X", (unsigned char)bytes[i]); + buffer.append(tmp); + } + + return buffer; #else - static const char hex_str[] = "0123456789ABCDEF"; + static const char hex_str[] = "0123456789ABCDEF"; - char result[len * 2 + 1]; + char result[len * 2 + 1]; - result[len * 2] = 0; - for (int i = 0; i < len; i++) { - result[i * 2 + 0] = hex_str[(bytes[i] >> 4) & 0x0F]; - result[i * 2 + 1] = hex_str[(bytes[i]) & 0x0F]; - } + result[len * 2] = 0; + for (int i = 0; i < len; i++) { + result[i * 2 + 0] = hex_str[(bytes[i] >> 4) & 0x0F]; + result[i * 2 + 1] = hex_str[(bytes[i]) & 0x0F]; + } - string buffer(result); - return buffer; + string buffer(result); + return buffer; #endif } bool UtilAll::SplitURL(const string &serverURL, string &addr, short &nPort) { - size_t pos = serverURL.find(':'); - if (pos == string::npos) { - return false; - } + size_t pos = serverURL.find(':'); + if (pos == string::npos) { + return false; + } - addr = serverURL.substr(0, pos); - if (0 == addr.compare("localhost")) { - addr = "127.0.0.1"; - } + addr = serverURL.substr(0, pos); + if (0 == addr.compare("localhost")) { + addr = "127.0.0.1"; + } - pos++; - string port = serverURL.substr(pos, serverURL.length() - pos); - nPort = atoi(port.c_str()); - if (nPort == 0) { - return false; - } - return true; + pos++; + string port = serverURL.substr(pos, serverURL.length() - pos); + nPort = atoi(port.c_str()); + if (nPort == 0) { + return false; + } + return true; } int UtilAll::Split(vector &ret_, const string &strIn, const char sep) { - if (strIn.empty()) return 0; - - string tmp; - string::size_type pos_begin = strIn.find_first_not_of(sep); - string::size_type comma_pos = 0; - - while (pos_begin != string::npos) { - comma_pos = strIn.find(sep, pos_begin); - if (comma_pos != string::npos) { - tmp = strIn.substr(pos_begin, comma_pos - pos_begin); - pos_begin = comma_pos + 1; - } else { - tmp = strIn.substr(pos_begin); - pos_begin = comma_pos; - } - - if (!tmp.empty()) { - ret_.push_back(tmp); - tmp.clear(); + if (strIn.empty()) return 0; + + string tmp; + string::size_type pos_begin = strIn.find_first_not_of(sep); + string::size_type comma_pos = 0; + + while (pos_begin != string::npos) { + comma_pos = strIn.find(sep, pos_begin); + if (comma_pos != string::npos) { + tmp = strIn.substr(pos_begin, comma_pos - pos_begin); + pos_begin = comma_pos + 1; + } else { + tmp = strIn.substr(pos_begin); + pos_begin = comma_pos; + } + + if (!tmp.empty()) { + ret_.push_back(tmp); + tmp.clear(); + } } - } - return ret_.size(); + return ret_.size(); } -int UtilAll::Split(vector &ret_, const string &strIn, - const string &sep) { - if (strIn.empty()) return 0; - - string tmp; - string::size_type pos_begin = strIn.find_first_not_of(sep); - string::size_type comma_pos = 0; - - while (pos_begin != string::npos) { - comma_pos = strIn.find(sep, pos_begin); - if (comma_pos != string::npos) { - tmp = strIn.substr(pos_begin, comma_pos - pos_begin); - pos_begin = comma_pos + sep.length(); - } else { - tmp = strIn.substr(pos_begin); - pos_begin = comma_pos; - } - - if (!tmp.empty()) { - ret_.push_back(tmp); - tmp.clear(); +int UtilAll::Split(vector &ret_, const string &strIn, const string &sep) { + if (strIn.empty()) return 0; + + string tmp; + string::size_type pos_begin = strIn.find_first_not_of(sep); + string::size_type comma_pos = 0; + + while (pos_begin != string::npos) { + comma_pos = strIn.find(sep, pos_begin); + if (comma_pos != string::npos) { + tmp = strIn.substr(pos_begin, comma_pos - pos_begin); + pos_begin = comma_pos + sep.length(); + } else { + tmp = strIn.substr(pos_begin); + pos_begin = comma_pos; + } + + if (!tmp.empty()) { + ret_.push_back(tmp); + tmp.clear(); + } } - } - return ret_.size(); + return ret_.size(); } int32_t UtilAll::StringToInt32(const std::string &str, int32_t &out) { - out = 0; - if (str.empty()) { - return false; - } - - char *end = NULL; - errno = 0; - long l = strtol(str.c_str(), &end, 10); - /* Both checks are needed because INT_MAX == LONG_MAX is possible. */ - if (l > INT_MAX || (errno == ERANGE && l == LONG_MAX)) return false; - if (l < INT_MIN || (errno == ERANGE && l == LONG_MIN)) return false; - if (*end != '\0') return false; - out = l; - return true; + out = 0; + if (str.empty()) { + return false; + } + + char *end = NULL; + errno = 0; + long l = strtol(str.c_str(), &end, 10); + /* Both checks are needed because INT_MAX == LONG_MAX is possible. */ + if (l > INT_MAX || (errno == ERANGE && l == LONG_MAX)) return false; + if (l < INT_MIN || (errno == ERANGE && l == LONG_MIN)) return false; + if (*end != '\0') return false; + out = l; + return true; } int64_t UtilAll::StringToInt64(const std::string &str, int64_t &val) { - char *endptr = NULL; - errno = 0; /* To distinguish success/failure after call */ - val = strtoll(str.c_str(), &endptr, 10); + char *endptr = NULL; + errno = 0; /* To distinguish success/failure after call */ + val = strtoll(str.c_str(), &endptr, 10); - /* Check for various possible errors */ - if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || - (errno != 0 && val == 0)) { - return false; - } - /*no digit was found Or Further characters after number*/ - if (endptr == str.c_str()) { - return false; - } - /*no digit was found Or Further characters after number*/ - if (*endptr != '\0') { - return false; - } - /* If we got here, strtol() successfully parsed a number */ - return true; + /* Check for various possible errors */ + if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0)) { + return false; + } + /*no digit was found Or Further characters after number*/ + if (endptr == str.c_str()) { + return false; + } + /*no digit was found Or Further characters after number*/ + if (*endptr != '\0') { + return false; + } + /* If we got here, strtol() successfully parsed a number */ + return true; } string UtilAll::getLocalHostName() { - if (s_localHostName.empty()) { - // boost::system::error_code error; - // s_localHostName = boost::asio::ip::host_name(error); - - char name[1024]; - boost::system::error_code ec; - if (boost::asio::detail::socket_ops::gethostname(name, sizeof(name), ec) != - 0) { - return std::string(); + if (s_localHostName.empty()) { + // boost::system::error_code error; + // s_localHostName = boost::asio::ip::host_name(error); + + char name[1024]; + boost::system::error_code ec; + if (boost::asio::detail::socket_ops::gethostname(name, sizeof(name), ec) != 0) { + return std::string(); + } + s_localHostName.append(name, strlen(name)); } - s_localHostName.append(name, strlen(name)); - } - return s_localHostName; + return s_localHostName; } string UtilAll::getLocalAddress() { - if (s_localIpAddress.empty()) { - boost::asio::io_service io_service; - boost::asio::ip::tcp::resolver resolver(io_service); - boost::asio::ip::tcp::resolver::query query(getLocalHostName(), ""); - boost::system::error_code error; - boost::asio::ip::tcp::resolver::iterator iter = - resolver.resolve(query, error); - if (error) { - return ""; + if (s_localIpAddress.empty()) { + boost::asio::io_service io_service; + boost::asio::ip::tcp::resolver resolver(io_service); + boost::asio::ip::tcp::resolver::query query(getLocalHostName(), ""); + boost::system::error_code error; + boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query, error); + if (error) { + return ""; + } + boost::asio::ip::tcp::resolver::iterator end; // End marker. + boost::asio::ip::tcp::endpoint ep; + while (iter != end) { + ep = *iter++; + } + s_localIpAddress = ep.address().to_string(); } - boost::asio::ip::tcp::resolver::iterator end; // End marker. - boost::asio::ip::tcp::endpoint ep; - while (iter != end) { - ep = *iter++; - } - s_localIpAddress = ep.address().to_string(); - } - return s_localIpAddress; + return s_localIpAddress; } string UtilAll::getHomeDirectory() { #ifndef WIN32 - char *homeEnv = getenv("HOME"); - string homeDir; - if (homeEnv == NULL) { - homeDir.append(getpwuid(getuid())->pw_dir); - } else { - homeDir.append(homeEnv); - } + char *homeEnv = getenv("HOME"); + string homeDir; + if (homeEnv == NULL) { + homeDir.append(getpwuid(getuid())->pw_dir); + } else { + homeDir.append(homeEnv); + } #else - string homeDir(getenv("USERPROFILE")); + string homeDir(getenv("USERPROFILE")); #endif - return homeDir; + return homeDir; } string UtilAll::getProcessName() { #ifndef WIN32 - char buf[PATH_MAX + 1] = {0}; - int count = PATH_MAX + 1; - char procpath[PATH_MAX + 1] = {0}; - sprintf(procpath, "/proc/%d/exe", getpid()); - - if (access(procpath, F_OK) == -1) { - return ""; - } - - int retval = readlink(procpath, buf, count - 1); - if ((retval < 0 || retval >= count - 1)) { - return ""; - } - if (!strcmp(buf + retval - 10, " (deleted)")) - buf[retval - 10] = '\0'; // remove last " (deleted)" - else - buf[retval] = '\0'; - - char *process_name = strrchr(buf, '/'); - if (process_name) { - return std::string(process_name + 1); - } else { - return ""; - } + char buf[PATH_MAX + 1] = {0}; + int count = PATH_MAX + 1; + char procpath[PATH_MAX + 1] = {0}; + sprintf(procpath, "/proc/%d/exe", getpid()); + + if (access(procpath, F_OK) == -1) { + return ""; + } + + int retval = readlink(procpath, buf, count - 1); + if ((retval < 0 || retval >= count - 1)) { + return ""; + } + if (!strcmp(buf + retval - 10, " (deleted)")) + buf[retval - 10] = '\0'; // remove last " (deleted)" + else + buf[retval] = '\0'; + + char *process_name = strrchr(buf, '/'); + if (process_name) { + return std::string(process_name + 1); + } else { + return ""; + } #else - TCHAR szFileName[MAX_PATH + 1]; - GetModuleFileName(NULL, szFileName, MAX_PATH + 1); - return std::string(szFileName); + TCHAR szFileName[MAX_PATH + 1]; + GetModuleFileName(NULL, szFileName, MAX_PATH + 1); + return std::string(szFileName); #endif } uint64_t UtilAll::currentTimeMillis() { - auto since_epoch = - std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch()); - return static_cast(since_epoch.count()); + auto since_epoch = + std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()); + return static_cast(since_epoch.count()); } uint64_t UtilAll::currentTimeSeconds() { - auto since_epoch = - std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch()); - return static_cast(since_epoch.count()); + auto since_epoch = + std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()); + return static_cast(since_epoch.count()); } bool UtilAll::deflate(std::string &input, std::string &out, int level) { - boost::iostreams::zlib_params zlibParams(level, - boost::iostreams::zlib::deflated); - boost::iostreams::filtering_ostream compressingStream; - compressingStream.push(boost::iostreams::zlib_compressor(zlibParams)); - compressingStream.push(boost::iostreams::back_inserter(out)); - compressingStream << input; - boost::iostreams::close(compressingStream); - - return true; + boost::iostreams::zlib_params zlibParams(level, boost::iostreams::zlib::deflated); + boost::iostreams::filtering_ostream compressingStream; + compressingStream.push(boost::iostreams::zlib_compressor(zlibParams)); + compressingStream.push(boost::iostreams::back_inserter(out)); + compressingStream << input; + boost::iostreams::close(compressingStream); + + return true; } bool UtilAll::inflate(std::string &input, std::string &out) { - boost::iostreams::filtering_ostream decompressingStream; - decompressingStream.push(boost::iostreams::zlib_decompressor()); - decompressingStream.push(boost::iostreams::back_inserter(out)); - decompressingStream << input; - boost::iostreams::close(decompressingStream); + boost::iostreams::filtering_ostream decompressingStream; + decompressingStream.push(boost::iostreams::zlib_decompressor()); + decompressingStream.push(boost::iostreams::back_inserter(out)); + decompressingStream << input; + boost::iostreams::close(decompressingStream); - return true; + return true; } -bool UtilAll::ReplaceFile(const std::string &from_path, - const std::string &to_path) { +bool UtilAll::ReplaceFile(const std::string &from_path, const std::string &to_path) { #ifdef WIN32 - // Try a simple move first. It will only succeed when |to_path| doesn't - // already exist. - if (::MoveFile(from_path.c_str(), to_path.c_str())) return true; - // Try the full-blown replace if the move fails, as ReplaceFile will only - // succeed when |to_path| does exist. When writing to a network share, we may - // not be able to change the ACLs. Ignore ACL errors then - // (REPLACEFILE_IGNORE_MERGE_ERRORS). - if (::ReplaceFile(to_path.c_str(), from_path.c_str(), NULL, - REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL)) { - return true; - } - return false; + // Try a simple move first. It will only succeed when |to_path| doesn't + // already exist. + if (::MoveFile(from_path.c_str(), to_path.c_str())) return true; + // Try the full-blown replace if the move fails, as ReplaceFile will only + // succeed when |to_path| does exist. When writing to a network share, we may + // not be able to change the ACLs. Ignore ACL errors then + // (REPLACEFILE_IGNORE_MERGE_ERRORS). + if (::ReplaceFile(to_path.c_str(), from_path.c_str(), NULL, REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL)) { + return true; + } + return false; #else - if (rename(from_path.c_str(), to_path.c_str()) == 0) return true; - return false; + if (rename(from_path.c_str(), to_path.c_str()) == 0) return true; + return false; #endif } } diff --git a/src/common/UtilAll.h b/src/common/UtilAll.h index e342f5f29..9e92c9582 100644 --- a/src/common/UtilAll.h +++ b/src/common/UtilAll.h @@ -77,8 +77,8 @@ const string null = ""; template inline void deleteAndZero(Type &pointer) { - delete pointer; - pointer = NULL; + delete pointer; + pointer = NULL; } #define EMPTY_STR_PTR(ptr) (ptr == NULL || ptr[0] == '\0') #ifdef WIN32 @@ -89,56 +89,54 @@ inline void deleteAndZero(Type &pointer) { // - static string to_string(const T &n) { - std::ostringstream stm; - stm << n; - return stm.str(); - } - - static bool to_bool(std::string const &s) { return atoi(s.c_str()); } - - static bool SplitURL(const string &serverURL, string &addr, short &nPort); - static int Split(vector &ret_, const string &strIn, const char sep); - static int Split(vector &ret_, const string &strIn, - const string &sep); - - static int32_t StringToInt32(const std::string &str, int32_t &out); - static int64_t StringToInt64(const std::string &str, int64_t &val); - - static string getLocalHostName(); - static string getLocalAddress(); - static string getHomeDirectory(); - - static string getProcessName(); - - static uint64_t currentTimeMillis(); - static uint64_t currentTimeSeconds(); - - static bool deflate(std::string &input, std::string &out, int level); - static bool inflate(std::string &input, std::string &out); - // Renames file |from_path| to |to_path|. Both paths must be on the same - // volume, or the function will fail. Destination file will be created - // if it doesn't exist. Prefer this function over Move when dealing with - // temporary files. On Windows it preserves attributes of the target file. - // Returns true on success. - // Returns false on failure.. - static bool ReplaceFile(const std::string &from_path, - const std::string &to_path); - - private: - static std::string s_localHostName; - static std::string s_localIpAddress; +public: + static bool startsWith_retry(const string &topic); + static string getRetryTopic(const string &consumerGroup); + + static void Trim(string &str); + static bool isBlank(const string &str); + static uint64 hexstr2ull(const char *str); + static int64 str2ll(const char *str); + static string bytes2string(const char *bytes, int len); + + template + static string to_string(const T &n) { + std::ostringstream stm; + stm << n; + return stm.str(); + } + + static bool to_bool(std::string const &s) { return atoi(s.c_str()); } + + static bool SplitURL(const string &serverURL, string &addr, short &nPort); + static int Split(vector &ret_, const string &strIn, const char sep); + static int Split(vector &ret_, const string &strIn, const string &sep); + + static int32_t StringToInt32(const std::string &str, int32_t &out); + static int64_t StringToInt64(const std::string &str, int64_t &val); + + static string getLocalHostName(); + static string getLocalAddress(); + static string getHomeDirectory(); + + static string getProcessName(); + + static uint64_t currentTimeMillis(); + static uint64_t currentTimeSeconds(); + + static bool deflate(std::string &input, std::string &out, int level); + static bool inflate(std::string &input, std::string &out); + // Renames file |from_path| to |to_path|. Both paths must be on the same + // volume, or the function will fail. Destination file will be created + // if it doesn't exist. Prefer this function over Move when dealing with + // temporary files. On Windows it preserves attributes of the target file. + // Returns true on success. + // Returns false on failure.. + static bool ReplaceFile(const std::string &from_path, const std::string &to_path); + +private: + static std::string s_localHostName; + static std::string s_localIpAddress; }; // CHARACTER_MAX_LENGTH) { - THROW_MQEXCEPTION( - MQClientException, - "the specified topic is longer than topic max length 255.", -1); - } - - if (topic == DEFAULT_TOPIC) { - THROW_MQEXCEPTION( - MQClientException, - "the topic[" + topic + "] is conflict with default topic.", -1); - } - - if (!regularExpressionMatcher(topic, validPatternStr)) { - string str; - str = "the specified topic[" + topic + - "] contains illegal characters, allowing only" + validPatternStr; - - THROW_MQEXCEPTION(MQClientException, str.c_str(), -1); - } + if (UtilAll::isBlank(topic)) { + THROW_MQEXCEPTION(MQClientException, "the specified topic is blank", -1); + } + + if ((int)topic.length() > CHARACTER_MAX_LENGTH) { + THROW_MQEXCEPTION(MQClientException, "the specified topic is longer than topic max length 255.", -1); + } + + if (topic == DEFAULT_TOPIC) { + THROW_MQEXCEPTION(MQClientException, "the topic[" + topic + "] is conflict with default topic.", -1); + } + + if (!regularExpressionMatcher(topic, validPatternStr)) { + string str; + str = "the specified topic[" + topic + "] contains illegal characters, allowing only" + validPatternStr; + + THROW_MQEXCEPTION(MQClientException, str.c_str(), -1); + } } void Validators::checkGroup(const string& group) { - if (UtilAll::isBlank(group)) { - THROW_MQEXCEPTION(MQClientException, "the specified group is blank", -1); - } - - if (!regularExpressionMatcher(group, validPatternStr)) { - string str; - str = "the specified group[" + group + - "] contains illegal characters, allowing only" + validPatternStr; - - THROW_MQEXCEPTION(MQClientException, str.c_str(), -1); - } - if ((int)group.length() > CHARACTER_MAX_LENGTH) { - THROW_MQEXCEPTION( - MQClientException, - "the specified group is longer than group max length 255.", -1); - } + if (UtilAll::isBlank(group)) { + THROW_MQEXCEPTION(MQClientException, "the specified group is blank", -1); + } + + if (!regularExpressionMatcher(group, validPatternStr)) { + string str; + str = "the specified group[" + group + "] contains illegal characters, allowing only" + validPatternStr; + + THROW_MQEXCEPTION(MQClientException, str.c_str(), -1); + } + if ((int)group.length() > CHARACTER_MAX_LENGTH) { + THROW_MQEXCEPTION(MQClientException, "the specified group is longer than group max length 255.", -1); + } } void Validators::checkMessage(const MQMessage& msg, int maxMessageSize) { - checkTopic(msg.getTopic()); - - string body = msg.getBody(); - // maxMessageSize) { - char info[256]; - sprintf(info, "the message body size over max value, MAX: %d", - maxMessageSize); - THROW_MQEXCEPTION(MQClientException, info, -1); - } + checkTopic(msg.getTopic()); + + string body = msg.getBody(); + // maxMessageSize) { + char info[256]; + sprintf(info, "the message body size over max value, MAX: %d", maxMessageSize); + THROW_MQEXCEPTION(MQClientException, info, -1); + } } // end_) return false; - ptr_ += len; - return true; + if (ptr_ + len > end_) return false; + ptr_ += len; + return true; } bool BigEndianReader::ReadBytes(void* out, size_t len) { - if (ptr_ + len > end_) return false; - memcpy(out, ptr_, len); - ptr_ += len; - return true; + if (ptr_ + len > end_) return false; + memcpy(out, ptr_, len); + ptr_ += len; + return true; } template bool BigEndianReader::Read(T* value) { - if (ptr_ + sizeof(T) > end_) return false; - ReadBigEndian(ptr_, value); - ptr_ += sizeof(T); - return true; + if (ptr_ + sizeof(T) > end_) return false; + ReadBigEndian(ptr_, value); + ptr_ += sizeof(T); + return true; } bool BigEndianReader::ReadU8(uint8_t* value) { return Read(value); } @@ -56,28 +55,27 @@ bool BigEndianReader::ReadU32(uint32_t* value) { return Read(value); } bool BigEndianReader::ReadU64(uint64_t* value) { return Read(value); } -BigEndianWriter::BigEndianWriter(char* buf, size_t len) - : ptr_(buf), end_(ptr_ + len) {} +BigEndianWriter::BigEndianWriter(char* buf, size_t len) : ptr_(buf), end_(ptr_ + len) {} bool BigEndianWriter::Skip(size_t len) { - if (ptr_ + len > end_) return false; - ptr_ += len; - return true; + if (ptr_ + len > end_) return false; + ptr_ += len; + return true; } bool BigEndianWriter::WriteBytes(const void* buf, size_t len) { - if (ptr_ + len > end_) return false; - memcpy(ptr_, buf, len); - ptr_ += len; - return true; + if (ptr_ + len > end_) return false; + memcpy(ptr_, buf, len); + ptr_ += len; + return true; } template bool BigEndianWriter::Write(T value) { - if (ptr_ + sizeof(T) > end_) return false; - WriteBigEndian(ptr_, value); - ptr_ += sizeof(T); - return true; + if (ptr_ + sizeof(T) > end_) return false; + WriteBigEndian(ptr_, value); + ptr_ += sizeof(T); + return true; } bool BigEndianWriter::WriteU8(uint8_t value) { return Write(value); } diff --git a/src/common/big_endian.h b/src/common/big_endian.h index 142ac2d48..9367f0c47 100644 --- a/src/common/big_endian.h +++ b/src/common/big_endian.h @@ -34,83 +34,83 @@ namespace rocketmq { // This would cause SIGBUS on ARMv5 or earlier and ARMv6-M. template inline void ReadBigEndian(const char buf[], T* out) { - *out = buf[0]; - for (size_t i = 1; i < sizeof(T); ++i) { - *out <<= 8; - // Must cast to uint8_t to avoid clobbering by sign extension. - *out |= static_cast(buf[i]); - } + *out = buf[0]; + for (size_t i = 1; i < sizeof(T); ++i) { + *out <<= 8; + // Must cast to uint8_t to avoid clobbering by sign extension. + *out |= static_cast(buf[i]); + } } // Write an integer (signed or unsigned) |val| to |buf| in Big Endian order. // Note: this loop is unrolled with -O1 and above. template inline void WriteBigEndian(char buf[], T val) { - for (size_t i = 0; i < sizeof(T); ++i) { - buf[sizeof(T) - i - 1] = static_cast(val & 0xFF); - val >>= 8; - } + for (size_t i = 0; i < sizeof(T); ++i) { + buf[sizeof(T) - i - 1] = static_cast(val & 0xFF); + val >>= 8; + } } // Specializations to make clang happy about the (dead code) shifts above. template <> inline void ReadBigEndian(const char buf[], uint8_t* out) { - *out = buf[0]; + *out = buf[0]; } template <> inline void WriteBigEndian(char buf[], uint8_t val) { - buf[0] = static_cast(val); + buf[0] = static_cast(val); } // Allows reading integers in network order (big endian) while iterating over // an underlying buffer. All the reading functions advance the internal pointer. class BigEndianReader { - public: - BigEndianReader(const char* buf, size_t len); - - const char* ptr() const { return ptr_; } - int remaining() const { return end_ - ptr_; } - - bool Skip(size_t len); - bool ReadBytes(void* out, size_t len); - bool ReadU8(uint8_t* value); - bool ReadU16(uint16_t* value); - bool ReadU32(uint32_t* value); - bool ReadU64(uint64_t* value); - - private: - // Hidden to promote type safety. - template - bool Read(T* v); - - const char* ptr_; - const char* end_; +public: + BigEndianReader(const char* buf, size_t len); + + const char* ptr() const { return ptr_; } + int remaining() const { return end_ - ptr_; } + + bool Skip(size_t len); + bool ReadBytes(void* out, size_t len); + bool ReadU8(uint8_t* value); + bool ReadU16(uint16_t* value); + bool ReadU32(uint32_t* value); + bool ReadU64(uint64_t* value); + +private: + // Hidden to promote type safety. + template + bool Read(T* v); + + const char* ptr_; + const char* end_; }; // Allows writing integers in network order (big endian) while iterating over // an underlying buffer. All the writing functions advance the internal pointer. class BigEndianWriter { - public: - BigEndianWriter(char* buf, size_t len); - - char* ptr() const { return ptr_; } - int remaining() const { return end_ - ptr_; } - - bool Skip(size_t len); - bool WriteBytes(const void* buf, size_t len); - bool WriteU8(uint8_t value); - bool WriteU16(uint16_t value); - bool WriteU32(uint32_t value); - bool WriteU64(uint64_t value); - - private: - // Hidden to promote type safety. - template - bool Write(T v); - - char* ptr_; - char* end_; +public: + BigEndianWriter(char* buf, size_t len); + + char* ptr() const { return ptr_; } + int remaining() const { return end_ - ptr_; } + + bool Skip(size_t len); + bool WriteBytes(const void* buf, size_t len); + bool WriteU8(uint8_t value); + bool WriteU16(uint16_t value); + bool WriteU32(uint32_t value); + bool WriteU64(uint64_t value); + +private: + // Hidden to promote type safety. + template + bool Write(T v); + + char* ptr_; + char* end_; }; } // namespace base diff --git a/src/common/dataBlock.cpp b/src/common/dataBlock.cpp old mode 100755 new mode 100644 index 4195f2b55..ff41a0424 --- a/src/common/dataBlock.cpp +++ b/src/common/dataBlock.cpp @@ -21,185 +21,172 @@ namespace rocketmq { MemoryBlock::MemoryBlock() : size(0), data(NULL) {} -MemoryBlock::MemoryBlock(const int initialSize, const bool initialiseToZero) - : size(0), data(NULL) { - if (initialSize > 0) { - size = initialSize; - data = static_cast(initialiseToZero - ? std::calloc(initialSize, sizeof(char)) - : std::malloc(initialSize * sizeof(char))); - } +MemoryBlock::MemoryBlock(const int initialSize, const bool initialiseToZero) : size(0), data(NULL) { + if (initialSize > 0) { + size = initialSize; + data = static_cast(initialiseToZero ? std::calloc(initialSize, sizeof(char)) + : std::malloc(initialSize * sizeof(char))); + } } MemoryBlock::MemoryBlock(const void *const dataToInitialiseFrom, const size_t sizeInBytes) : size(sizeInBytes), data(NULL) { - if (size > 0) { - data = static_cast(std::malloc(size * sizeof(char))); + if (size > 0) { + data = static_cast(std::malloc(size * sizeof(char))); - if (dataToInitialiseFrom != NULL) memcpy(data, dataToInitialiseFrom, size); - } + if (dataToInitialiseFrom != NULL) memcpy(data, dataToInitialiseFrom, size); + } } MemoryBlock::MemoryBlock(const MemoryBlock &other) : size(other.size), data(NULL) { - if (size > 0) { - data = static_cast(std::malloc(size * sizeof(char))); - memcpy(data, other.data, size); - } + if (size > 0) { + data = static_cast(std::malloc(size * sizeof(char))); + memcpy(data, other.data, size); + } } MemoryBlock::MemoryBlock(MemoryBlock &&other) : size(other.size), data(other.data) { - other.size = 0; - other.data = NULL; + other.size = 0; + other.data = NULL; } MemoryBlock::~MemoryBlock() { std::free(data); } MemoryBlock &MemoryBlock::operator=(const MemoryBlock &other) { - if (this != &other) { - setSize(other.size, false); - memcpy(data, other.data, size); - } + if (this != &other) { + setSize(other.size, false); + memcpy(data, other.data, size); + } - return *this; + return *this; } MemoryBlock &MemoryBlock::operator=(MemoryBlock &&other) { - if (this != &other) { - std::free(data); + if (this != &other) { + std::free(data); - size = other.size; - data = other.data; + size = other.size; + data = other.data; - other.size = 0; - other.data = NULL; - } + other.size = 0; + other.data = NULL; + } - return *this; + return *this; } //============================================================================== -bool MemoryBlock::operator==(const MemoryBlock &other) const { - return matches(other.data, other.size); -} +bool MemoryBlock::operator==(const MemoryBlock &other) const { return matches(other.data, other.size); } -bool MemoryBlock::operator!=(const MemoryBlock &other) const { - return !operator==(other); -} +bool MemoryBlock::operator!=(const MemoryBlock &other) const { return !operator==(other); } bool MemoryBlock::matches(const void *dataToCompare, int dataSize) const { - return size == dataSize && memcmp(data, dataToCompare, size) == 0; + return size == dataSize && memcmp(data, dataToCompare, size) == 0; } //============================================================================== // this will resize the block to this size void MemoryBlock::setSize(const int newSize, const bool initialiseToZero) { - if (size != newSize) { - if (newSize <= 0) { - reset(); - } else { - if (data != NULL) { - data = static_cast( - data == NULL ? std::malloc(newSize * sizeof(char)) - : std::realloc(data, newSize * sizeof(char))); - - if (initialiseToZero && (newSize > size)) - memset(data + size, 0, newSize - size); - } else { - std::free(data); - data = static_cast(initialiseToZero - ? std::calloc(newSize, sizeof(char)) - : std::malloc(newSize * sizeof(char))); - } - - size = newSize; + if (size != newSize) { + if (newSize <= 0) { + reset(); + } else { + if (data != NULL) { + data = static_cast(data == NULL ? std::malloc(newSize * sizeof(char)) + : std::realloc(data, newSize * sizeof(char))); + + if (initialiseToZero && (newSize > size)) memset(data + size, 0, newSize - size); + } else { + std::free(data); + data = static_cast(initialiseToZero ? std::calloc(newSize, sizeof(char)) + : std::malloc(newSize * sizeof(char))); + } + + size = newSize; + } } - } } void MemoryBlock::reset() { - std::free(data); - data = NULL; - size = 0; + std::free(data); + data = NULL; + size = 0; } void MemoryBlock::ensureSize(const int minimumSize, const bool initialiseToZero) { - if (size < minimumSize) setSize(minimumSize, initialiseToZero); + if (size < minimumSize) setSize(minimumSize, initialiseToZero); } //============================================================================== -void MemoryBlock::fillWith(const int value) { - memset(data, (int) value, size); -} +void MemoryBlock::fillWith(const int value) { memset(data, (int)value, size); } void MemoryBlock::append(const void *const srcData, const int numBytes) { - if (numBytes > 0) { - const int oldSize = size; - setSize(size + numBytes); - memcpy(data + oldSize, srcData, numBytes); - } + if (numBytes > 0) { + const int oldSize = size; + setSize(size + numBytes); + memcpy(data + oldSize, srcData, numBytes); + } } void MemoryBlock::replaceWith(const void *const srcData, const int numBytes) { - if (numBytes > 0) { - setSize(numBytes); - memcpy(data, srcData, numBytes); - } + if (numBytes > 0) { + setSize(numBytes); + memcpy(data, srcData, numBytes); + } } void MemoryBlock::insert(const void *const srcData, const int numBytes, int insertPosition) { - if (numBytes > 0) { - insertPosition = std::min(insertPosition, size); - const int trailingDataSize = size - insertPosition; - setSize(size + numBytes, false); + if (numBytes > 0) { + insertPosition = std::min(insertPosition, size); + const int trailingDataSize = size - insertPosition; + setSize(size + numBytes, false); - if (trailingDataSize > 0) - memmove(data + insertPosition + numBytes, data + insertPosition, - trailingDataSize); + if (trailingDataSize > 0) memmove(data + insertPosition + numBytes, data + insertPosition, trailingDataSize); - memcpy(data + insertPosition, srcData, numBytes); - } + memcpy(data + insertPosition, srcData, numBytes); + } } void MemoryBlock::removeSection(const int startByte, const int numBytesToRemove) { - if (startByte + numBytesToRemove >= size) { - setSize(startByte); - } else if (numBytesToRemove > 0) { - memmove(data + startByte, data + startByte + numBytesToRemove, size - (startByte + numBytesToRemove)); + if (startByte + numBytesToRemove >= size) { + setSize(startByte); + } else if (numBytesToRemove > 0) { + memmove(data + startByte, data + startByte + numBytesToRemove, size - (startByte + numBytesToRemove)); - setSize(size - numBytesToRemove); - } + setSize(size - numBytesToRemove); + } } void MemoryBlock::copyFrom(const void *const src, int offset, int num) { - const char *d = static_cast(src); + const char *d = static_cast(src); - if (offset < 0) { - d -= offset; - num += (size_t) -offset; - offset = 0; - } + if (offset < 0) { + d -= offset; + num += (size_t) - offset; + offset = 0; + } - if ((size_t) offset + num > (unsigned int) size) num = size - (size_t) offset; + if ((size_t)offset + num > (unsigned int)size) num = size - (size_t)offset; - if (num > 0) memcpy(data + offset, d, num); + if (num > 0) memcpy(data + offset, d, num); } void MemoryBlock::copyTo(void *const dst, int offset, int num) const { - char *d = static_cast(dst); - - if (offset < 0) { - memset(d, 0, (size_t) -offset); - d -= offset; - num -= (size_t) -offset; - offset = 0; - } - - if ((size_t) offset + num > (unsigned int) size) { - const int newNum = (size_t) size - (size_t) offset; - memset(d + newNum, 0, num - newNum); - num = newNum; - } - - if (num > 0) memcpy(d, data + offset, num); + char *d = static_cast(dst); + + if (offset < 0) { + memset(d, 0, (size_t) - offset); + d -= offset; + num -= (size_t) - offset; + offset = 0; + } + + if ((size_t)offset + num > (unsigned int)size) { + const int newNum = (size_t)size - (size_t)offset; + memset(d + newNum, 0, num - newNum); + num = newNum; + } + + if (num > 0) memcpy(d, data + offset, num); } } diff --git a/src/common/dataBlock.h b/src/common/dataBlock.h old mode 100755 new mode 100644 index 88099b76a..9be054445 --- a/src/common/dataBlock.h +++ b/src/common/dataBlock.h @@ -28,181 +28,180 @@ namespace rocketmq { class ROCKETMQCLIENT_API MemoryBlock { - public: - //============================================================================== - /** Create an uninitialised block with 0 size. */ - MemoryBlock(); - - /** Creates a memory block with a given initial size. - - @param initialSize the size of block to create - @param initialiseToZero whether to clear the memory or just leave it - uninitialised - */ - MemoryBlock(const int initialSize, bool initialiseToZero = false); - - /** Creates a memory block using a copy of a block of data. - - @param dataToInitialiseFrom some data to copy into this block - @param sizeInBytes how much space to use - */ - MemoryBlock(const void *dataToInitialiseFrom, size_t sizeInBytes); - - /** Creates a copy of another memory block. */ - MemoryBlock(const MemoryBlock &); - - MemoryBlock(MemoryBlock &&); - - /** Destructor. */ - ~MemoryBlock(); - - /** Copies another memory block onto this one. - This block will be resized and copied to exactly match the other one. - */ - MemoryBlock &operator=(const MemoryBlock &); - - MemoryBlock &operator=(MemoryBlock &&); - - //============================================================================== - /** Compares two memory blocks. - @returns true only if the two blocks are the same size and have identical - contents. - */ - bool operator==(const MemoryBlock &other) const; - - /** Compares two memory blocks. - @returns true if the two blocks are different sizes or have different - contents. - */ - bool operator!=(const MemoryBlock &other) const; - - //============================================================================== - /** Returns a void pointer to the data. - - Note that the pointer returned will probably become invalid when the - block is resized. - */ - char *getData() const { return data; } - - /** Returns a byte from the memory block. - This returns a reference, so you can also use it to set a byte. - */ - template - char &operator[](const Type offset) const { - return data[offset]; - } - - /** Returns true if the data in this MemoryBlock matches the raw bytes - * passed-in. */ - bool matches(const void *data, int dataSize) const; - - //============================================================================== - /** Returns the block's current allocated size, in bytes. */ - int getSize() const { return size; } - - /** Resizes the memory block. - - Any data that is present in both the old and new sizes will be retained. - When enlarging the block, the new space that is allocated at the end can - either be - cleared, or left uninitialised. - - @param newSize the new desired size for the block - @param initialiseNewSpaceToZero if the block gets enlarged, this - determines - whether to clear the new section or - just leave it - uninitialised - @see ensureSize - */ - void setSize(const int newSize, bool initialiseNewSpaceToZero = false); - - /** Increases the block's size only if it's smaller than a given size. - - @param minimumSize if the block is already bigger than - this size, no action - will be taken; otherwise it will be - increased to this size - @param initialiseNewSpaceToZero if the block gets enlarged, this - determines - whether to clear the new section or - just leave it - uninitialised - @see setSize - */ - void ensureSize(const int minimumSize, bool initialiseNewSpaceToZero = false); - - /** Frees all the blocks data, setting its size to 0. */ - void reset(); - - //============================================================================== - /** Fills the entire memory block with a repeated byte value. - This is handy for clearing a block of memory to zero. - */ - void fillWith(int valueToUse); - - /** Adds another block of data to the end of this one. - The data pointer must not be null. This block's size will be increased - accordingly. - */ - void append(const void *data, int numBytes); - - /** Resizes this block to the given size and fills its contents from the - supplied buffer. - The data pointer must not be null. - */ - void replaceWith(const void *data, int numBytes); - - /** Inserts some data into the block. - The dataToInsert pointer must not be null. This block's size will be - increased accordingly. - If the insert position lies outside the valid range of the block, it will - be clipped to - within the range before being used. - */ - void insert(const void *dataToInsert, int numBytesToInsert, - int insertPosition); - - /** Chops out a section of the block. - - This will remove a section of the memory block and close the gap around - it, - shifting any subsequent data downwards and reducing the size of the block. - - If the range specified goes beyond the size of the block, it will be - clipped. - */ - void removeSection(int startByte, int numBytesToRemove); - - //============================================================================== - /** Copies data into this MemoryBlock from a memory address. - - @param srcData the memory location of the data to copy into - this block - @param destinationOffset the offset in this block at which the data - being copied should begin - @param numBytes how much to copy in (if this goes beyond the - size of the memory block, - it will be clipped so not to do anything - nasty) - */ - void copyFrom(const void *srcData, int destinationOffset, int numBytes); - - /** Copies data from this MemoryBlock to a memory address. - - @param destData the memory location to write to - @param sourceOffset the offset within this block from which the copied - data will be read - @param numBytes how much to copy (if this extends beyond the - limits of the memory block, - zeros will be used for that portion of the data) - */ - void copyTo(void *destData, int sourceOffset, int numBytes) const; - - private: - //============================================================================== - int size; - char *data; +public: + //============================================================================== + /** Create an uninitialised block with 0 size. */ + MemoryBlock(); + + /** Creates a memory block with a given initial size. + + @param initialSize the size of block to create + @param initialiseToZero whether to clear the memory or just leave it + uninitialised + */ + MemoryBlock(const int initialSize, bool initialiseToZero = false); + + /** Creates a memory block using a copy of a block of data. + + @param dataToInitialiseFrom some data to copy into this block + @param sizeInBytes how much space to use + */ + MemoryBlock(const void *dataToInitialiseFrom, size_t sizeInBytes); + + /** Creates a copy of another memory block. */ + MemoryBlock(const MemoryBlock &); + + MemoryBlock(MemoryBlock &&); + + /** Destructor. */ + ~MemoryBlock(); + + /** Copies another memory block onto this one. + This block will be resized and copied to exactly match the other one. + */ + MemoryBlock &operator=(const MemoryBlock &); + + MemoryBlock &operator=(MemoryBlock &&); + + //============================================================================== + /** Compares two memory blocks. + @returns true only if the two blocks are the same size and have identical + contents. + */ + bool operator==(const MemoryBlock &other) const; + + /** Compares two memory blocks. + @returns true if the two blocks are different sizes or have different + contents. + */ + bool operator!=(const MemoryBlock &other) const; + + //============================================================================== + /** Returns a void pointer to the data. + + Note that the pointer returned will probably become invalid when the + block is resized. + */ + char *getData() const { return data; } + + /** Returns a byte from the memory block. + This returns a reference, so you can also use it to set a byte. + */ + template + char &operator[](const Type offset) const { + return data[offset]; + } + + /** Returns true if the data in this MemoryBlock matches the raw bytes + * passed-in. */ + bool matches(const void *data, int dataSize) const; + + //============================================================================== + /** Returns the block's current allocated size, in bytes. */ + int getSize() const { return size; } + + /** Resizes the memory block. + + Any data that is present in both the old and new sizes will be retained. + When enlarging the block, the new space that is allocated at the end can + either be + cleared, or left uninitialised. + + @param newSize the new desired size for the block + @param initialiseNewSpaceToZero if the block gets enlarged, this + determines + whether to clear the new section or + just leave it + uninitialised + @see ensureSize + */ + void setSize(const int newSize, bool initialiseNewSpaceToZero = false); + + /** Increases the block's size only if it's smaller than a given size. + + @param minimumSize if the block is already bigger than + this size, no action + will be taken; otherwise it will be + increased to this size + @param initialiseNewSpaceToZero if the block gets enlarged, this + determines + whether to clear the new section or + just leave it + uninitialised + @see setSize + */ + void ensureSize(const int minimumSize, bool initialiseNewSpaceToZero = false); + + /** Frees all the blocks data, setting its size to 0. */ + void reset(); + + //============================================================================== + /** Fills the entire memory block with a repeated byte value. + This is handy for clearing a block of memory to zero. + */ + void fillWith(int valueToUse); + + /** Adds another block of data to the end of this one. + The data pointer must not be null. This block's size will be increased + accordingly. + */ + void append(const void *data, int numBytes); + + /** Resizes this block to the given size and fills its contents from the + supplied buffer. + The data pointer must not be null. + */ + void replaceWith(const void *data, int numBytes); + + /** Inserts some data into the block. + The dataToInsert pointer must not be null. This block's size will be + increased accordingly. + If the insert position lies outside the valid range of the block, it will + be clipped to + within the range before being used. + */ + void insert(const void *dataToInsert, int numBytesToInsert, int insertPosition); + + /** Chops out a section of the block. + + This will remove a section of the memory block and close the gap around + it, + shifting any subsequent data downwards and reducing the size of the block. + + If the range specified goes beyond the size of the block, it will be + clipped. + */ + void removeSection(int startByte, int numBytesToRemove); + + //============================================================================== + /** Copies data into this MemoryBlock from a memory address. + + @param srcData the memory location of the data to copy into + this block + @param destinationOffset the offset in this block at which the data + being copied should begin + @param numBytes how much to copy in (if this goes beyond the + size of the memory block, + it will be clipped so not to do anything + nasty) + */ + void copyFrom(const void *srcData, int destinationOffset, int numBytes); + + /** Copies data from this MemoryBlock to a memory address. + + @param destData the memory location to write to + @param sourceOffset the offset within this block from which the copied + data will be read + @param numBytes how much to copy (if this extends beyond the + limits of the memory block, + zeros will be used for that portion of the data) + */ + void copyTo(void *destData, int sourceOffset, int numBytes) const; + +private: + //============================================================================== + int size; + char *data; }; } diff --git a/src/common/sync_http_client.cpp b/src/common/sync_http_client.cpp old mode 100755 new mode 100644 index f07ac0d99..9c99b7e9f --- a/src/common/sync_http_client.cpp +++ b/src/common/sync_http_client.cpp @@ -31,126 +31,119 @@ using boost::asio::ip::tcp; using boost::asio::deadline_timer; namespace { -void check_deadline(deadline_timer* deadline, tcp::socket* socket, - const boost::system::error_code& ec) { - // Check whether the deadline has passed. We compare the deadline against - // the current time since a new asynchronous operation may have moved the - // deadline before this actor had a chance to run. - if (deadline->expires_at() <= deadline_timer::traits_type::now()) { - // The deadline has passed. The socket is closed so that any outstanding - // asynchronous operations are cancelled. This allows the blocked - // connect(), read_line() or write_line() functions to return. - boost::system::error_code ignored_ec; - socket->close(ignored_ec); - - // There is no longer an active deadline. The expiry is set to positive - // infinity so that the actor takes no action until a new deadline is set. - deadline->expires_at(boost::posix_time::pos_infin); - } - - // Put the actor back to sleep. - deadline->async_wait(boost::bind(&check_deadline, deadline, socket, - boost::asio::placeholders::error)); +void check_deadline(deadline_timer* deadline, tcp::socket* socket, const boost::system::error_code& ec) { + // Check whether the deadline has passed. We compare the deadline against + // the current time since a new asynchronous operation may have moved the + // deadline before this actor had a chance to run. + if (deadline->expires_at() <= deadline_timer::traits_type::now()) { + // The deadline has passed. The socket is closed so that any outstanding + // asynchronous operations are cancelled. This allows the blocked + // connect(), read_line() or write_line() functions to return. + boost::system::error_code ignored_ec; + socket->close(ignored_ec); + + // There is no longer an active deadline. The expiry is set to positive + // infinity so that the actor takes no action until a new deadline is set. + deadline->expires_at(boost::posix_time::pos_infin); + } + + // Put the actor back to sleep. + deadline->async_wait(boost::bind(&check_deadline, deadline, socket, boost::asio::placeholders::error)); } } // namespace namespace rocketmq { bool SyncfetchNsAddr(const Url& url_s, std::string& body) { - bool ret = true; - try { - boost::asio::io_service io_service; - // Get a list of endpoints corresponding to the server name. - tcp::resolver resolver(io_service); - tcp::resolver::query query(url_s.host_, url_s.port_); - tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); - boost::system::error_code ec = boost::asio::error::would_block; - deadline_timer deadline(io_service); - // TODO hardcode - boost::posix_time::seconds timeout(3); - deadline.expires_from_now(timeout); - // Try each endpoint until we successfully establish a connection. - tcp::socket socket(io_service); - boost::system::error_code deadline_ec; - check_deadline(&deadline, &socket, deadline_ec); - - boost::asio::async_connect(socket, endpoint_iterator, - boost::lambda::var(ec) = boost::lambda::_1); - - do { - io_service.run_one(); - } while (ec == boost::asio::error::would_block); - - if (ec || !socket.is_open()) { - LOG_ERROR("socket connect failure, connect timeout or connect failure"); - return false; - } - - // Form the request. We specify the "Connection: close" header so that the - // server will close the socket after transmitting the response. This will - // allow us to treat all data up until the EOF as the content. - boost::asio::streambuf request; - std::ostream request_stream(&request); - request_stream << "GET " << url_s.path_ << " HTTP/1.0\r\n"; - request_stream << "Host: " << url_s.host_ << "\r\n"; - request_stream << "Accept: */*\r\n"; - request_stream << "Connection: close\r\n\r\n"; - - // Send the request. - boost::asio::write(socket, request); - - // Read the response status line. The response streambuf will automatically - // grow to accommodate the entire line. The growth may be limited by passing - // a maximum size to the streambuf constructor. - boost::asio::streambuf response; - boost::asio::read_until(socket, response, "\r\n"); - - // Check that response is OK. - std::istream response_stream(&response); - std::string http_version; - response_stream >> http_version; - unsigned int status_code; - response_stream >> status_code; - std::string status_message; - std::getline(response_stream, status_message); - if (!response_stream || http_version.substr(0, 5) != "HTTP/") { - LOG_INFO("Invalid response %s\n", status_message.c_str()); - return false; + bool ret = true; + try { + boost::asio::io_service io_service; + // Get a list of endpoints corresponding to the server name. + tcp::resolver resolver(io_service); + tcp::resolver::query query(url_s.host_, url_s.port_); + tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); + boost::system::error_code ec = boost::asio::error::would_block; + deadline_timer deadline(io_service); + // TODO hardcode + boost::posix_time::seconds timeout(3); + deadline.expires_from_now(timeout); + // Try each endpoint until we successfully establish a connection. + tcp::socket socket(io_service); + boost::system::error_code deadline_ec; + check_deadline(&deadline, &socket, deadline_ec); + + boost::asio::async_connect(socket, endpoint_iterator, boost::lambda::var(ec) = boost::lambda::_1); + + do { + io_service.run_one(); + } while (ec == boost::asio::error::would_block); + + if (ec || !socket.is_open()) { + LOG_ERROR("socket connect failure, connect timeout or connect failure"); + return false; + } + + // Form the request. We specify the "Connection: close" header so that the + // server will close the socket after transmitting the response. This will + // allow us to treat all data up until the EOF as the content. + boost::asio::streambuf request; + std::ostream request_stream(&request); + request_stream << "GET " << url_s.path_ << " HTTP/1.0\r\n"; + request_stream << "Host: " << url_s.host_ << "\r\n"; + request_stream << "Accept: */*\r\n"; + request_stream << "Connection: close\r\n\r\n"; + + // Send the request. + boost::asio::write(socket, request); + + // Read the response status line. The response streambuf will automatically + // grow to accommodate the entire line. The growth may be limited by passing + // a maximum size to the streambuf constructor. + boost::asio::streambuf response; + boost::asio::read_until(socket, response, "\r\n"); + + // Check that response is OK. + std::istream response_stream(&response); + std::string http_version; + response_stream >> http_version; + unsigned int status_code; + response_stream >> status_code; + std::string status_message; + std::getline(response_stream, status_message); + if (!response_stream || http_version.substr(0, 5) != "HTTP/") { + LOG_INFO("Invalid response %s\n", status_message.c_str()); + return false; + } + + if (status_code != 200) { + LOG_INFO("Response returned with status code %d ", status_code); + return false; + } + + // Read the response headers, which are terminated by a blank line. + boost::asio::read_until(socket, response, "\r\n\r\n"); + + // Process the response headers. + std::string header; + while (std::getline(response_stream, header) && header != "\r") + ; + + // Write whatever content we already have to output. + if (response.size() > 0) { + boost::asio::streambuf::const_buffers_type cbt = response.data(); + body.clear(); + body.insert(body.begin(), boost::asio::buffers_begin(cbt), boost::asio::buffers_end(cbt)); + } + + // Read until EOF, writing data to output as we go. + boost::system::error_code error; + while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error)) std::cout << &response; + if (error != boost::asio::error::eof) throw boost::system::system_error(error); } - - if (status_code != 200) { - LOG_INFO("Response returned with status code %d ", status_code); - return false; + catch (std::exception& e) { + LOG_ERROR("Exception: %s", e.what()); + ret = false; } - // Read the response headers, which are terminated by a blank line. - boost::asio::read_until(socket, response, "\r\n\r\n"); - - // Process the response headers. - std::string header; - while (std::getline(response_stream, header) && header != "\r") - ; - - // Write whatever content we already have to output. - if (response.size() > 0) { - boost::asio::streambuf::const_buffers_type cbt = response.data(); - body.clear(); - body.insert(body.begin(), boost::asio::buffers_begin(cbt), - boost::asio::buffers_end(cbt)); - } - - // Read until EOF, writing data to output as we go. - boost::system::error_code error; - while (boost::asio::read(socket, response, - boost::asio::transfer_at_least(1), error)) - std::cout << &response; - if (error != boost::asio::error::eof) - throw boost::system::system_error(error); - - } catch (std::exception& e) { - LOG_ERROR("Exception: %s", e.what()); - ret = false; - } - - return ret; + return ret; } } // end of namespace ons diff --git a/src/common/sync_http_client.h b/src/common/sync_http_client.h old mode 100755 new mode 100644 diff --git a/src/common/url.cpp b/src/common/url.cpp old mode 100755 new mode 100644 index 348f27a33..334c6a806 --- a/src/common/url.cpp +++ b/src/common/url.cpp @@ -26,38 +26,36 @@ namespace rocketmq { Url::Url(const std::string& url_s) { parse(url_s); } void Url::parse(const std::string& url_s) { - const std::string prot_end("://"); - auto prot_i = - std::search(url_s.begin(), url_s.end(), prot_end.begin(), prot_end.end()); - protocol_.reserve(std::distance(url_s.begin(), prot_i)); - std::transform(url_s.begin(), prot_i, std::back_inserter(protocol_), - std::ptr_fun(tolower)); // protocol is icase - - if (prot_i == url_s.end()) return; - - std::advance(prot_i, prot_end.length()); - - auto path_i = find(prot_i, url_s.end(), ':'); - std::string::const_iterator path_end_i; - if (path_i == url_s.end()) { - // not include port, use default port - port_ = "80"; - path_i = std::find(prot_i, url_s.end(), '/'); - path_end_i = path_i; - } else { - auto port_i = find(path_i + 1, url_s.end(), '/'); - port_.insert(port_.begin(), path_i + 1, port_i); - path_end_i = path_i + port_.length() + 1; - } - - host_.reserve(distance(prot_i, path_i)); - std::transform(prot_i, path_i, std::back_inserter(host_), - std::ptr_fun(tolower)); // host is icase} - - auto query_i = find(path_end_i, url_s.end(), '?'); - path_.assign(path_end_i, query_i); - if (query_i != url_s.end()) ++query_i; - query_.assign(query_i, url_s.end()); + const std::string prot_end("://"); + auto prot_i = std::search(url_s.begin(), url_s.end(), prot_end.begin(), prot_end.end()); + protocol_.reserve(std::distance(url_s.begin(), prot_i)); + std::transform(url_s.begin(), prot_i, std::back_inserter(protocol_), + std::ptr_fun(tolower)); // protocol is icase + + if (prot_i == url_s.end()) return; + + std::advance(prot_i, prot_end.length()); + + auto path_i = find(prot_i, url_s.end(), ':'); + std::string::const_iterator path_end_i; + if (path_i == url_s.end()) { + // not include port, use default port + port_ = "80"; + path_i = std::find(prot_i, url_s.end(), '/'); + path_end_i = path_i; + } else { + auto port_i = find(path_i + 1, url_s.end(), '/'); + port_.insert(port_.begin(), path_i + 1, port_i); + path_end_i = path_i + port_.length() + 1; + } + + host_.reserve(distance(prot_i, path_i)); + std::transform(prot_i, path_i, std::back_inserter(host_), std::ptr_fun(tolower)); // host is icase} + + auto query_i = find(path_end_i, url_s.end(), '?'); + path_.assign(path_end_i, query_i); + if (query_i != url_s.end()) ++query_i; + query_.assign(query_i, url_s.end()); } } // namespace ons diff --git a/src/common/url.h b/src/common/url.h old mode 100755 new mode 100644 index 0fbcb85fe..6ff60b834 --- a/src/common/url.h +++ b/src/common/url.h @@ -21,18 +21,18 @@ namespace rocketmq { class Url { - public: - Url(const std::string& url_s); // omitted copy, ==, accessors, ... +public: + Url(const std::string& url_s); // omitted copy, ==, accessors, ... - private: - void parse(const std::string& url_s); +private: + void parse(const std::string& url_s); - public: - std::string protocol_; - std::string host_; - std::string port_; - std::string path_; - std::string query_; +public: + std::string protocol_; + std::string host_; + std::string port_; + std::string path_; + std::string query_; }; } #endif // ROCKETMQ_CLIENT4CPP_URL_HH_ diff --git a/src/consumer/AllocateMQStrategy.h b/src/consumer/AllocateMQStrategy.h old mode 100755 new mode 100644 index fbe501a18..d6cf3bcbf --- a/src/consumer/AllocateMQStrategy.h +++ b/src/consumer/AllocateMQStrategy.h @@ -25,73 +25,66 @@ namespace rocketmq { //& mqAll, - std::vector& cidAll, - std::vector& outReuslt) = 0; +public: + virtual ~AllocateMQStrategy() {} + virtual void allocate(const std::string& currentCID, std::vector& mqAll, + std::vector& cidAll, std::vector& outReuslt) = 0; }; //& mqAll, - std::vector& cidAll, - std::vector& outReuslt) { - outReuslt.clear(); - if (currentCID.empty()) { - THROW_MQEXCEPTION(MQClientException, "currentCID is empty", -1); - } +public: + virtual ~AllocateMQAveragely() {} + virtual void allocate(const std::string& currentCID, std::vector& mqAll, + std::vector& cidAll, std::vector& outReuslt) { + outReuslt.clear(); + if (currentCID.empty()) { + THROW_MQEXCEPTION(MQClientException, "currentCID is empty", -1); + } - if (mqAll.empty()) { - THROW_MQEXCEPTION(MQClientException, "mqAll is empty", -1); - } + if (mqAll.empty()) { + THROW_MQEXCEPTION(MQClientException, "mqAll is empty", -1); + } - if (cidAll.empty()) { - THROW_MQEXCEPTION(MQClientException, "cidAll is empty", -1); - } + if (cidAll.empty()) { + THROW_MQEXCEPTION(MQClientException, "cidAll is empty", -1); + } - int index = -1; - int cidAllSize = cidAll.size(); - for (int i = 0; i < cidAllSize; i++) { - if (cidAll[i] == currentCID) { - index = i; - break; - } - } + int index = -1; + int cidAllSize = cidAll.size(); + for (int i = 0; i < cidAllSize; i++) { + if (cidAll[i] == currentCID) { + index = i; + break; + } + } - if (index == -1) { - LOG_ERROR("could not find clientId from Broker"); - return; - } + if (index == -1) { + LOG_ERROR("could not find clientId from Broker"); + return; + } - int mqAllSize = mqAll.size(); - int mod = mqAllSize % cidAllSize; - int averageSize = mqAllSize <= cidAllSize - ? 1 - : (mod > 0 && index < mod ? mqAllSize / cidAllSize + 1 - : mqAllSize / cidAllSize); - int startIndex = (mod > 0 && index < mod) ? index * averageSize - : index * averageSize + mod; - int range = (std::min)(averageSize, mqAllSize - startIndex); - LOG_INFO( - "range is:%d, index is:%d, mqAllSize is:%d, averageSize is:%d, " - "startIndex is:%d", - range, index, mqAllSize, averageSize, startIndex); - //= 0) // example: range is:-1, index is:1, mqAllSize is:1, - // averageSize is:1, startIndex is:2 - { - for (int i = 0; i < range; i++) { - if ((startIndex + i) >= 0) { - outReuslt.push_back(mqAll.at((startIndex + i) % mqAllSize)); + int mqAllSize = mqAll.size(); + int mod = mqAllSize % cidAllSize; + int averageSize = mqAllSize <= cidAllSize ? 1 : (mod > 0 && index < mod ? mqAllSize / cidAllSize + 1 + : mqAllSize / cidAllSize); + int startIndex = (mod > 0 && index < mod) ? index * averageSize : index * averageSize + mod; + int range = (std::min)(averageSize, mqAllSize - startIndex); + LOG_INFO( + "range is:%d, index is:%d, mqAllSize is:%d, averageSize is:%d, " + "startIndex is:%d", + range, index, mqAllSize, averageSize, startIndex); + //= 0) // example: range is:-1, index is:1, mqAllSize is:1, + // averageSize is:1, startIndex is:2 + { + for (int i = 0; i < range; i++) { + if ((startIndex + i) >= 0) { + outReuslt.push_back(mqAll.at((startIndex + i) % mqAllSize)); + } + } } - } } - } }; //getMessageListenerType(); +MessageListenerType ConsumeMessageConcurrentlyService::getConsumeMsgSerivceListenerType() { + return m_pMessageListener->getMessageListenerType(); } -void ConsumeMessageConcurrentlyService::submitConsumeRequest( - PullRequest* request, vector& msgs) { - m_ioService.post(boost::bind( - &ConsumeMessageConcurrentlyService::ConsumeRequest, this, request, msgs)); +void ConsumeMessageConcurrentlyService::submitConsumeRequest(PullRequest* request, vector& msgs) { + m_ioService.post(boost::bind(&ConsumeMessageConcurrentlyService::ConsumeRequest, this, request, msgs)); } -void ConsumeMessageConcurrentlyService::ConsumeRequest( - PullRequest* request, vector& msgs) { - if (!request || request->isDroped()) { - LOG_WARN("the pull result is NULL or Had been dropped"); - request->clearAllMsgs(); // add clear operation to avoid bad state when - // dropped pullRequest returns normal - return; - } +void ConsumeMessageConcurrentlyService::ConsumeRequest(PullRequest* request, vector& msgs) { + if (!request || request->isDroped()) { + LOG_WARN("the pull result is NULL or Had been dropped"); + request->clearAllMsgs(); // add clear operation to avoid bad state when + // dropped pullRequest returns normal + return; + } - //m_messageQueue).toString().c_str()); - return; - } + //m_messageQueue).toString().c_str()); + return; + } - ConsumeStatus status = CONSUME_SUCCESS; - if (m_pMessageListener != NULL) { - resetRetryTopic(msgs); - request->setLastConsumeTimestamp(UtilAll::currentTimeMillis()); - status = m_pMessageListener->consumeMessage(msgs); - } + ConsumeStatus status = CONSUME_SUCCESS; + if (m_pMessageListener != NULL) { + resetRetryTopic(msgs); + request->setLastConsumeTimestamp(UtilAll::currentTimeMillis()); + status = m_pMessageListener->consumeMessage(msgs); + } - /*LOG_DEBUG("Consumed MSG size:%d of mq:%s", - msgs.size(), (request->m_messageQueue).toString().c_str());*/ - int ackIndex = -1; - switch (status) { - case CONSUME_SUCCESS: - ackIndex = msgs.size(); - break; - case RECONSUME_LATER: - ackIndex = -1; - break; - default: - break; - } + /*LOG_DEBUG("Consumed MSG size:%d of mq:%s", + msgs.size(), (request->m_messageQueue).toString().c_str());*/ + int ackIndex = -1; + switch (status) { + case CONSUME_SUCCESS: + ackIndex = msgs.size(); + break; + case RECONSUME_LATER: + ackIndex = -1; + break; + default: + break; + } - switch (m_pConsumer->getMessageModel()) { - case BROADCASTING: - // Note: broadcasting reconsume should do by application, as it has big - // affect to broker cluster - if (ackIndex != (int)msgs.size()) - LOG_WARN("BROADCASTING, the message consume failed, drop it:%s", - (request->m_messageQueue).toString().c_str()); - break; - case CLUSTERING: - // send back msg to broker; - for (size_t i = ackIndex + 1; i < msgs.size(); i++) { - LOG_WARN("consume fail, MQ is:%s, its msgId is:%s, index is:" SIZET_FMT - ", reconsume " - "times is:%d", - (request->m_messageQueue).toString().c_str(), - msgs[i].getMsgId().c_str(), i, msgs[i].getReconsumeTimes()); - m_pConsumer->sendMessageBack(msgs[i], 0); - } - break; - default: - break; - } + switch (m_pConsumer->getMessageModel()) { + case BROADCASTING: + // Note: broadcasting reconsume should do by application, as it has big + // affect to broker cluster + if (ackIndex != (int)msgs.size()) + LOG_WARN("BROADCASTING, the message consume failed, drop it:%s", + (request->m_messageQueue).toString().c_str()); + break; + case CLUSTERING: + // send back msg to broker; + for (size_t i = ackIndex + 1; i < msgs.size(); i++) { + LOG_WARN("consume fail, MQ is:%s, its msgId is:%s, index is:" SIZET_FMT + ", reconsume " + "times is:%d", + (request->m_messageQueue).toString().c_str(), msgs[i].getMsgId().c_str(), i, + msgs[i].getReconsumeTimes()); + m_pConsumer->sendMessageBack(msgs[i], 0); + } + break; + default: + break; + } - // update offset - int64 offset = request->removeMessage(msgs); - // LOG_DEBUG("update offset:%lld of mq: %s", - // offset,(request->m_messageQueue).toString().c_str()); - if (offset >= 0) { - m_pConsumer->updateConsumeOffset(request->m_messageQueue, offset); - } else { - LOG_WARN("Note: accumulation consume occurs on mq:%s", - (request->m_messageQueue).toString().c_str()); - } + // update offset + int64 offset = request->removeMessage(msgs); + // LOG_DEBUG("update offset:%lld of mq: %s", + // offset,(request->m_messageQueue).toString().c_str()); + if (offset >= 0) { + m_pConsumer->updateConsumeOffset(request->m_messageQueue, offset); + } else { + LOG_WARN("Note: accumulation consume occurs on mq:%s", (request->m_messageQueue).toString().c_str()); + } } -void ConsumeMessageConcurrentlyService::resetRetryTopic( - vector& msgs) { - string groupTopic = UtilAll::getRetryTopic(m_pConsumer->getGroupName()); - for (size_t i = 0; i < msgs.size(); i++) { - MQMessageExt& msg = msgs[i]; - string retryTopic = msg.getProperty(MQMessage::PROPERTY_RETRY_TOPIC); - if (!retryTopic.empty() && groupTopic.compare(msg.getTopic()) == 0) { - msg.setTopic(retryTopic); +void ConsumeMessageConcurrentlyService::resetRetryTopic(vector& msgs) { + string groupTopic = UtilAll::getRetryTopic(m_pConsumer->getGroupName()); + for (size_t i = 0; i < msgs.size(); i++) { + MQMessageExt& msg = msgs[i]; + string retryTopic = msg.getProperty(MQMessage::PROPERTY_RETRY_TOPIC); + if (!retryTopic.empty() && groupTopic.compare(msg.getTopic()) == 0) { + msg.setTopic(retryTopic); + } } - } } //getRebalance()->lockAll(); +void ConsumeMessageOrderlyService::lockMQPeriodically(boost::system::error_code& ec, boost::asio::deadline_timer* t) { + m_pConsumer->getRebalance()->lockAll(); - boost::system::error_code e; - t->expires_at(t->expires_at() + boost::posix_time::milliseconds( - PullRequest::RebalanceLockInterval), - e); - t->async_wait(boost::bind(&ConsumeMessageOrderlyService::lockMQPeriodically, - this, ec, t)); + boost::system::error_code e; + t->expires_at(t->expires_at() + boost::posix_time::milliseconds(PullRequest::RebalanceLockInterval), e); + t->async_wait(boost::bind(&ConsumeMessageOrderlyService::lockMQPeriodically, this, ec, t)); } -void ConsumeMessageOrderlyService::unlockAllMQ() { - m_pConsumer->getRebalance()->unlockAll(false); -} +void ConsumeMessageOrderlyService::unlockAllMQ() { m_pConsumer->getRebalance()->unlockAll(false); } -bool ConsumeMessageOrderlyService::lockOneMQ(const MQMessageQueue& mq) { - return m_pConsumer->getRebalance()->lock(mq); -} +bool ConsumeMessageOrderlyService::lockOneMQ(const MQMessageQueue& mq) { return m_pConsumer->getRebalance()->lock(mq); } void ConsumeMessageOrderlyService::stopThreadPool() { - m_shutdownInprogress = true; - m_ioService.stop(); - m_async_ioService.stop(); - m_async_service_thread->interrupt(); - m_async_service_thread->join(); - m_threadpool.join_all(); + m_shutdownInprogress = true; + m_ioService.stop(); + m_async_ioService.stop(); + m_async_service_thread->interrupt(); + m_async_service_thread->join(); + m_threadpool.join_all(); } -MessageListenerType -ConsumeMessageOrderlyService::getConsumeMsgSerivceListenerType() { - return m_pMessageListener->getMessageListenerType(); +MessageListenerType ConsumeMessageOrderlyService::getConsumeMsgSerivceListenerType() { + return m_pMessageListener->getMessageListenerType(); } -void ConsumeMessageOrderlyService::submitConsumeRequest( - PullRequest* request, vector& msgs) { - m_ioService.post(boost::bind(&ConsumeMessageOrderlyService::ConsumeRequest, - this, request)); +void ConsumeMessageOrderlyService::submitConsumeRequest(PullRequest* request, vector& msgs) { + m_ioService.post(boost::bind(&ConsumeMessageOrderlyService::ConsumeRequest, this, request)); } -void ConsumeMessageOrderlyService::static_submitConsumeRequestLater( - void* context, PullRequest* request, bool tryLockMQ, - boost::asio::deadline_timer* t) { - LOG_INFO("submit consumeRequest later for mq:%s", - request->m_messageQueue.toString().c_str()); - vector msgs; - ConsumeMessageOrderlyService* orderlyService = - (ConsumeMessageOrderlyService*)context; - orderlyService->submitConsumeRequest(request, msgs); - if (tryLockMQ) { - orderlyService->lockOneMQ(request->m_messageQueue); - } - if (t) deleteAndZero(t); +void ConsumeMessageOrderlyService::static_submitConsumeRequestLater(void* context, PullRequest* request, bool tryLockMQ, + boost::asio::deadline_timer* t) { + LOG_INFO("submit consumeRequest later for mq:%s", request->m_messageQueue.toString().c_str()); + vector msgs; + ConsumeMessageOrderlyService* orderlyService = (ConsumeMessageOrderlyService*)context; + orderlyService->submitConsumeRequest(request, msgs); + if (tryLockMQ) { + orderlyService->lockOneMQ(request->m_messageQueue); + } + if (t) deleteAndZero(t); } void ConsumeMessageOrderlyService::ConsumeRequest(PullRequest* request) { - bool bGetMutex = false; - boost::unique_lock lock( - request->getPullRequestCriticalSection(), boost::try_to_lock); - if (!lock.owns_lock()) { - if (!lock.timed_lock(boost::get_system_time() + - boost::posix_time::seconds(1))) { - LOG_ERROR("ConsumeRequest of:%s get timed_mutex timeout", - request->m_messageQueue.toString().c_str()); - return; + bool bGetMutex = false; + boost::unique_lock lock(request->getPullRequestCriticalSection(), boost::try_to_lock); + if (!lock.owns_lock()) { + if (!lock.timed_lock(boost::get_system_time() + boost::posix_time::seconds(1))) { + LOG_ERROR("ConsumeRequest of:%s get timed_mutex timeout", request->m_messageQueue.toString().c_str()); + return; + } else { + bGetMutex = true; + } } else { - bGetMutex = true; + bGetMutex = true; } - } else { - bGetMutex = true; - } - if (!bGetMutex) { - // LOG_INFO("pullrequest of mq:%s consume inprogress", - // request->m_messageQueue.toString().c_str()); - return; - } - if (!request || request->isDroped()) { - LOG_WARN("the pull result is NULL or Had been dropped"); - request->clearAllMsgs(); // add clear operation to avoid bad state when - // dropped pullRequest returns normal - return; - } - - if (m_pMessageListener) { - if ((request->isLocked() && !request->isLockExpired()) || - m_pConsumer->getMessageModel() == BROADCASTING) { - DefaultMQPushConsumer* pConsumer = (DefaultMQPushConsumer*)m_pConsumer; - uint64_t beginTime = UtilAll::currentTimeMillis(); - bool continueConsume = true; - while (continueConsume) { - if ((UtilAll::currentTimeMillis() - beginTime) > - m_MaxTimeConsumeContinuously) { - LOG_INFO( - "continuely consume message queue:%s more than 60s, consume it " - "later", - request->m_messageQueue.toString().c_str()); - tryLockLaterAndReconsume(request, false); - break; - } - vector msgs; - request->takeMessages(msgs, pConsumer->getConsumeMessageBatchMaxSize()); - if (!msgs.empty()) { - request->setLastConsumeTimestamp(UtilAll::currentTimeMillis()); - ConsumeStatus consumeStatus = - m_pMessageListener->consumeMessage(msgs); - if (consumeStatus == RECONSUME_LATER) { - request->makeMessageToCosumeAgain(msgs); - continueConsume = false; - tryLockLaterAndReconsume(request, false); - } else { - m_pConsumer->updateConsumeOffset(request->m_messageQueue, - request->commit()); - } + if (!bGetMutex) { + // LOG_INFO("pullrequest of mq:%s consume inprogress", + // request->m_messageQueue.toString().c_str()); + return; + } + if (!request || request->isDroped()) { + LOG_WARN("the pull result is NULL or Had been dropped"); + request->clearAllMsgs(); // add clear operation to avoid bad state when + // dropped pullRequest returns normal + return; + } + + if (m_pMessageListener) { + if ((request->isLocked() && !request->isLockExpired()) || m_pConsumer->getMessageModel() == BROADCASTING) { + DefaultMQPushConsumer* pConsumer = (DefaultMQPushConsumer*)m_pConsumer; + uint64_t beginTime = UtilAll::currentTimeMillis(); + bool continueConsume = true; + while (continueConsume) { + if ((UtilAll::currentTimeMillis() - beginTime) > m_MaxTimeConsumeContinuously) { + LOG_INFO( + "continuely consume message queue:%s more than 60s, consume it " + "later", + request->m_messageQueue.toString().c_str()); + tryLockLaterAndReconsume(request, false); + break; + } + vector msgs; + request->takeMessages(msgs, pConsumer->getConsumeMessageBatchMaxSize()); + if (!msgs.empty()) { + request->setLastConsumeTimestamp(UtilAll::currentTimeMillis()); + ConsumeStatus consumeStatus = m_pMessageListener->consumeMessage(msgs); + if (consumeStatus == RECONSUME_LATER) { + request->makeMessageToCosumeAgain(msgs); + continueConsume = false; + tryLockLaterAndReconsume(request, false); + } else { + m_pConsumer->updateConsumeOffset(request->m_messageQueue, request->commit()); + } + } else { + continueConsume = false; + } + msgs.clear(); + if (m_shutdownInprogress) { + LOG_INFO("shutdown inprogress, break the consuming"); + return; + } + } + LOG_DEBUG("consume once exit of mq:%s", request->m_messageQueue.toString().c_str()); } else { - continueConsume = false; - } - msgs.clear(); - if (m_shutdownInprogress) { - LOG_INFO("shutdown inprogress, break the consuming"); - return; + LOG_ERROR("message queue:%s was not locked", request->m_messageQueue.toString().c_str()); + tryLockLaterAndReconsume(request, true); } - } - LOG_DEBUG("consume once exit of mq:%s", - request->m_messageQueue.toString().c_str()); - } else { - LOG_ERROR("message queue:%s was not locked", - request->m_messageQueue.toString().c_str()); - tryLockLaterAndReconsume(request, true); } - } } -void ConsumeMessageOrderlyService::tryLockLaterAndReconsume( - PullRequest* request, bool tryLockMQ) { - int retryTimer = tryLockMQ ? 500 : 100; - boost::asio::deadline_timer* t = new boost::asio::deadline_timer( - m_async_ioService, boost::posix_time::milliseconds(retryTimer)); - t->async_wait(boost::bind( - &(ConsumeMessageOrderlyService::static_submitConsumeRequestLater), this, - request, tryLockMQ, t)); +void ConsumeMessageOrderlyService::tryLockLaterAndReconsume(PullRequest* request, bool tryLockMQ) { + int retryTimer = tryLockMQ ? 500 : 100; + boost::asio::deadline_timer* t = + new boost::asio::deadline_timer(m_async_ioService, boost::posix_time::milliseconds(retryTimer)); + t->async_wait( + boost::bind(&(ConsumeMessageOrderlyService::static_submitConsumeRequestLater), this, request, tryLockMQ, t)); } } diff --git a/src/consumer/ConsumeMsgService.h b/src/consumer/ConsumeMsgService.h old mode 100755 new mode 100644 index 5878cb462..7d4b8c140 --- a/src/consumer/ConsumeMsgService.h +++ b/src/consumer/ConsumeMsgService.h @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + #ifndef _CONSUMEMESSAGESERVICE_H_ #define _CONSUMEMESSAGESERVICE_H_ @@ -32,78 +32,68 @@ namespace rocketmq { class MQConsumer; //& msgs) {} - virtual MessageListenerType getConsumeMsgSerivceListenerType() { - return messageListenerDefaultly; - } +public: + ConsumeMsgService() {} + virtual ~ConsumeMsgService() {} + virtual void start() {} + virtual void shutdown() {} + virtual void stopThreadPool() {} + virtual void submitConsumeRequest(PullRequest* request, vector& msgs) {} + virtual MessageListenerType getConsumeMsgSerivceListenerType() { return messageListenerDefaultly; } }; class ConsumeMessageConcurrentlyService : public ConsumeMsgService { - public: - ConsumeMessageConcurrentlyService(MQConsumer*, int threadCount, - MQMessageListener* msgListener); - virtual ~ConsumeMessageConcurrentlyService(); - virtual void start(); - virtual void shutdown(); - virtual void submitConsumeRequest(PullRequest* request, - vector& msgs); - virtual MessageListenerType getConsumeMsgSerivceListenerType(); - virtual void stopThreadPool(); +public: + ConsumeMessageConcurrentlyService(MQConsumer*, int threadCount, MQMessageListener* msgListener); + virtual ~ConsumeMessageConcurrentlyService(); + virtual void start(); + virtual void shutdown(); + virtual void submitConsumeRequest(PullRequest* request, vector& msgs); + virtual MessageListenerType getConsumeMsgSerivceListenerType(); + virtual void stopThreadPool(); - void ConsumeRequest(PullRequest* request, vector& msgs); + void ConsumeRequest(PullRequest* request, vector& msgs); - private: - void resetRetryTopic(vector& msgs); +private: + void resetRetryTopic(vector& msgs); - private: - MQConsumer* m_pConsumer; - MQMessageListener* m_pMessageListener; - boost::asio::io_service m_ioService; - boost::thread_group m_threadpool; - boost::asio::io_service::work m_ioServiceWork; +private: + MQConsumer* m_pConsumer; + MQMessageListener* m_pMessageListener; + boost::asio::io_service m_ioService; + boost::thread_group m_threadpool; + boost::asio::io_service::work m_ioServiceWork; }; class ConsumeMessageOrderlyService : public ConsumeMsgService { - public: - ConsumeMessageOrderlyService(MQConsumer*, int threadCount, - MQMessageListener* msgListener); - virtual ~ConsumeMessageOrderlyService(); - virtual void start(); - virtual void shutdown(); - virtual void submitConsumeRequest(PullRequest* request, - vector& msgs); - virtual void stopThreadPool(); - virtual MessageListenerType getConsumeMsgSerivceListenerType(); +public: + ConsumeMessageOrderlyService(MQConsumer*, int threadCount, MQMessageListener* msgListener); + virtual ~ConsumeMessageOrderlyService(); + virtual void start(); + virtual void shutdown(); + virtual void submitConsumeRequest(PullRequest* request, vector& msgs); + virtual void stopThreadPool(); + virtual MessageListenerType getConsumeMsgSerivceListenerType(); - void boost_asio_work(); - void tryLockLaterAndReconsume(PullRequest* request, bool tryLockMQ); - static void static_submitConsumeRequestLater(void* context, - PullRequest* request, - bool tryLockMQ, - boost::asio::deadline_timer* t); - void ConsumeRequest(PullRequest* request); - void lockMQPeriodically(boost::system::error_code& ec, - boost::asio::deadline_timer* t); - void unlockAllMQ(); - bool lockOneMQ(const MQMessageQueue& mq); + void boost_asio_work(); + void tryLockLaterAndReconsume(PullRequest* request, bool tryLockMQ); + static void static_submitConsumeRequestLater(void* context, PullRequest* request, bool tryLockMQ, + boost::asio::deadline_timer* t); + void ConsumeRequest(PullRequest* request); + void lockMQPeriodically(boost::system::error_code& ec, boost::asio::deadline_timer* t); + void unlockAllMQ(); + bool lockOneMQ(const MQMessageQueue& mq); - private: - MQConsumer* m_pConsumer; - bool m_shutdownInprogress; - MQMessageListener* m_pMessageListener; - uint64_t m_MaxTimeConsumeContinuously; - boost::asio::io_service m_ioService; - boost::thread_group m_threadpool; - boost::asio::io_service::work m_ioServiceWork; - boost::asio::io_service m_async_ioService; - boost::scoped_ptr m_async_service_thread; +private: + MQConsumer* m_pConsumer; + bool m_shutdownInprogress; + MQMessageListener* m_pMessageListener; + uint64_t m_MaxTimeConsumeContinuously; + boost::asio::io_service m_ioService; + boost::thread_group m_threadpool; + boost::asio::io_service::work m_ioServiceWork; + boost::asio::io_service m_async_ioService; + boost::scoped_ptr m_async_service_thread; }; //registerConsumer(this); - if (!registerOK) { - m_serviceState = CREATE_JUST; - THROW_MQEXCEPTION( - MQClientException, - "The cousumer group[" + getGroupName() + - "] has been created before, specify another name please.", - -1); - } - - //load(); - } catch (MQClientException& e) { - bStartFailed = true; - errorMsg = std::string(e.what()); - } - - getFactory()->start(); - m_serviceState = RUNNING; - if (bStartFailed) { - shutdown(); - THROW_MQEXCEPTION(MQClientException, errorMsg, -1); - } - break; + switch (m_serviceState) { + case CREATE_JUST: { + m_serviceState = START_FAILED; + MQClient::start(); + LOG_INFO("DefaultMQPullConsumer:%s start", m_GroupName.c_str()); + + //registerConsumer(this); + if (!registerOK) { + m_serviceState = CREATE_JUST; + THROW_MQEXCEPTION(MQClientException, "The cousumer group[" + getGroupName() + + "] has been created before, specify another name please.", + -1); + } + + //load(); + } + catch (MQClientException& e) { + bStartFailed = true; + errorMsg = std::string(e.what()); + } + + getFactory()->start(); + m_serviceState = RUNNING; + if (bStartFailed) { + shutdown(); + THROW_MQEXCEPTION(MQClientException, errorMsg, -1); + } + break; + } + case RUNNING: + case START_FAILED: + case SHUTDOWN_ALREADY: + break; + default: + break; } - case RUNNING: - case START_FAILED: - case SHUTDOWN_ALREADY: - break; - default: - break; - } } void DefaultMQPullConsumer::shutdown() { - switch (m_serviceState) { - case RUNNING: { - LOG_INFO("DefaultMQPullConsumer:%s shutdown", m_GroupName.c_str()); - persistConsumerOffset(); - getFactory()->unregisterConsumer(this); - getFactory()->shutdown(); - m_serviceState = SHUTDOWN_ALREADY; - break; + switch (m_serviceState) { + case RUNNING: { + LOG_INFO("DefaultMQPullConsumer:%s shutdown", m_GroupName.c_str()); + persistConsumerOffset(); + getFactory()->unregisterConsumer(this); + getFactory()->shutdown(); + m_serviceState = SHUTDOWN_ALREADY; + break; + } + case SHUTDOWN_ALREADY: + case CREATE_JUST: + break; + default: + break; } - case SHUTDOWN_ALREADY: - case CREATE_JUST: - break; - default: - break; - } } -void DefaultMQPullConsumer::sendMessageBack(MQMessageExt& msg, int delayLevel) { - -} +void DefaultMQPullConsumer::sendMessageBack(MQMessageExt& msg, int delayLevel) {} -void DefaultMQPullConsumer::fetchSubscribeMessageQueues( - const string& topic, vector& mqs) { - mqs.clear(); - try { - getFactory()->fetchSubscribeMessageQueues(topic, mqs, - getSessionCredentials()); - } catch (MQException& e) { - LOG_ERROR(e.what()); - } +void DefaultMQPullConsumer::fetchSubscribeMessageQueues(const string& topic, vector& mqs) { + mqs.clear(); + try { + getFactory()->fetchSubscribeMessageQueues(topic, mqs, getSessionCredentials()); + } + catch (MQException& e) { + LOG_ERROR(e.what()); + } } -void DefaultMQPullConsumer::updateTopicSubscribeInfo( - const string& topic, vector& info) {} +void DefaultMQPullConsumer::updateTopicSubscribeInfo(const string& topic, vector& info) {} -void DefaultMQPullConsumer::registerMessageQueueListener( - const string& topic, MQueueListener* pListener) { - m_registerTopics.insert(topic); - if (pListener) { - m_pMessageQueueListener = pListener; - } +void DefaultMQPullConsumer::registerMessageQueueListener(const string& topic, MQueueListener* pListener) { + m_registerTopics.insert(topic); + if (pListener) { + m_pMessageQueueListener = pListener; + } } -PullResult DefaultMQPullConsumer::pull(const MQMessageQueue& mq, - const string& subExpression, - int64 offset, int maxNums) { - return pullSyncImpl(mq, subExpression, offset, maxNums, false); +PullResult DefaultMQPullConsumer::pull(const MQMessageQueue& mq, const string& subExpression, int64 offset, + int maxNums) { + return pullSyncImpl(mq, subExpression, offset, maxNums, false); } -void DefaultMQPullConsumer::pull(const MQMessageQueue& mq, - const string& subExpression, int64 offset, - int maxNums, PullCallback* pPullCallback) { - pullAsyncImpl(mq, subExpression, offset, maxNums, false, pPullCallback); +void DefaultMQPullConsumer::pull(const MQMessageQueue& mq, const string& subExpression, int64 offset, int maxNums, + PullCallback* pPullCallback) { + pullAsyncImpl(mq, subExpression, offset, maxNums, false, pPullCallback); } -PullResult DefaultMQPullConsumer::pullBlockIfNotFound( - const MQMessageQueue& mq, const string& subExpression, int64 offset, - int maxNums) { - return pullSyncImpl(mq, subExpression, offset, maxNums, true); +PullResult DefaultMQPullConsumer::pullBlockIfNotFound(const MQMessageQueue& mq, const string& subExpression, + int64 offset, int maxNums) { + return pullSyncImpl(mq, subExpression, offset, maxNums, true); } -void DefaultMQPullConsumer::pullBlockIfNotFound(const MQMessageQueue& mq, - const string& subExpression, - int64 offset, int maxNums, - PullCallback* pPullCallback) { - pullAsyncImpl(mq, subExpression, offset, maxNums, true, pPullCallback); +void DefaultMQPullConsumer::pullBlockIfNotFound(const MQMessageQueue& mq, const string& subExpression, int64 offset, + int maxNums, PullCallback* pPullCallback) { + pullAsyncImpl(mq, subExpression, offset, maxNums, true, pPullCallback); } -PullResult DefaultMQPullConsumer::pullSyncImpl(const MQMessageQueue& mq, - const string& subExpression, - int64 offset, int maxNums, - bool block) { - if (offset < 0) THROW_MQEXCEPTION(MQClientException, "offset < 0", -1); - - if (maxNums <= 0) THROW_MQEXCEPTION(MQClientException, "maxNums <= 0", -1); - - // pSData( - FilterAPI::buildSubscriptionData(mq.getTopic(), subExpression)); - - int timeoutMillis = block ? 1000 * 30 : 1000 * 10; - - try { - unique_ptr pullResult( - m_pPullAPIWrapper->pullKernelImpl(mq, // 1 - pSData->getSubString(), // 2 - 0L, // 3 - offset, // 4 - maxNums, // 5 - sysFlag, // 6 - 0, // 7 - 1000 * 20, // 8 - timeoutMillis, // 9 - ComMode_SYNC, // 10 - NULL, //processPullResult(mq, pullResult.get(), - pSData.get()); - } catch (MQException& e) { - LOG_ERROR(e.what()); - } - return PullResult(BROKER_TIMEOUT); +PullResult DefaultMQPullConsumer::pullSyncImpl(const MQMessageQueue& mq, const string& subExpression, int64 offset, + int maxNums, bool block) { + if (offset < 0) THROW_MQEXCEPTION(MQClientException, "offset < 0", -1); + + if (maxNums <= 0) THROW_MQEXCEPTION(MQClientException, "maxNums <= 0", -1); + + // pSData(FilterAPI::buildSubscriptionData(mq.getTopic(), subExpression)); + + int timeoutMillis = block ? 1000 * 30 : 1000 * 10; + + try { + unique_ptr pullResult(m_pPullAPIWrapper->pullKernelImpl(mq, // 1 + pSData->getSubString(), // 2 + 0L, // 3 + offset, // 4 + maxNums, // 5 + sysFlag, // 6 + 0, // 7 + 1000 * 20, // 8 + timeoutMillis, // 9 + ComMode_SYNC, // 10 + NULL, //processPullResult(mq, pullResult.get(), pSData.get()); + } + catch (MQException& e) { + LOG_ERROR(e.what()); + } + return PullResult(BROKER_TIMEOUT); } -void DefaultMQPullConsumer::pullAsyncImpl(const MQMessageQueue& mq, - const string& subExpression, - int64 offset, int maxNums, bool block, - PullCallback* pPullCallback) { - if (offset < 0) THROW_MQEXCEPTION(MQClientException, "offset < 0", -1); - - if (maxNums <= 0) THROW_MQEXCEPTION(MQClientException, "maxNums <= 0", -1); - - if (!pPullCallback) - THROW_MQEXCEPTION(MQClientException, "pPullCallback is null", -1); - - // pSData( - FilterAPI::buildSubscriptionData(mq.getTopic(), subExpression)); - - int timeoutMillis = block ? 1000 * 30 : 1000 * 10; - - // pullResult(m_pPullAPIWrapper->pullKernelImpl( - mq, // 1 - pSData->getSubString(), // 2 - 0L, // 3 - offset, // 4 - maxNums, // 5 - sysFlag, // 6 - 0, // 7 - 1000 * 20, // 8 - timeoutMillis, // 9 - ComMode_ASYNC, // 10 - pPullCallback, getSessionCredentials(), &arg)); - } catch (MQException& e) { - LOG_ERROR(e.what()); - } +void DefaultMQPullConsumer::pullAsyncImpl(const MQMessageQueue& mq, const string& subExpression, int64 offset, + int maxNums, bool block, PullCallback* pPullCallback) { + if (offset < 0) THROW_MQEXCEPTION(MQClientException, "offset < 0", -1); + + if (maxNums <= 0) THROW_MQEXCEPTION(MQClientException, "maxNums <= 0", -1); + + if (!pPullCallback) THROW_MQEXCEPTION(MQClientException, "pPullCallback is null", -1); + + // pSData(FilterAPI::buildSubscriptionData(mq.getTopic(), subExpression)); + + int timeoutMillis = block ? 1000 * 30 : 1000 * 10; + + // pullResult(m_pPullAPIWrapper->pullKernelImpl(mq, // 1 + pSData->getSubString(), // 2 + 0L, // 3 + offset, // 4 + maxNums, // 5 + sysFlag, // 6 + 0, // 7 + 1000 * 20, // 8 + timeoutMillis, // 9 + ComMode_ASYNC, // 10 + pPullCallback, getSessionCredentials(), + &arg)); + } + catch (MQException& e) { + LOG_ERROR(e.what()); + } } void DefaultMQPullConsumer::subscriptionAutomatically(const string& topic) { - SubscriptionData* pSdata = m_pRebalance->getSubscriptionData(topic); - if (pSdata == NULL) { - unique_ptr subscriptionData( - FilterAPI::buildSubscriptionData(topic, SUB_ALL)); - m_pRebalance->setSubscriptionData(topic, subscriptionData.release()); - } + SubscriptionData* pSdata = m_pRebalance->getSubscriptionData(topic); + if (pSdata == NULL) { + unique_ptr subscriptionData(FilterAPI::buildSubscriptionData(topic, SUB_ALL)); + m_pRebalance->setSubscriptionData(topic, subscriptionData.release()); + } } -void DefaultMQPullConsumer::updateConsumeOffset(const MQMessageQueue& mq, - int64 offset) { - m_pOffsetStore->updateOffset(mq, offset); +void DefaultMQPullConsumer::updateConsumeOffset(const MQMessageQueue& mq, int64 offset) { + m_pOffsetStore->updateOffset(mq, offset); } -void DefaultMQPullConsumer::removeConsumeOffset(const MQMessageQueue& mq) { - m_pOffsetStore->removeOffset(mq); -} +void DefaultMQPullConsumer::removeConsumeOffset(const MQMessageQueue& mq) { m_pOffsetStore->removeOffset(mq); } -int64 DefaultMQPullConsumer::fetchConsumeOffset(const MQMessageQueue& mq, - bool fromStore) { - return m_pOffsetStore->readOffset( - mq, fromStore ? READ_FROM_STORE : MEMORY_FIRST_THEN_STORE, - getSessionCredentials()); +int64 DefaultMQPullConsumer::fetchConsumeOffset(const MQMessageQueue& mq, bool fromStore) { + return m_pOffsetStore->readOffset(mq, fromStore ? READ_FROM_STORE : MEMORY_FIRST_THEN_STORE, + getSessionCredentials()); } void DefaultMQPullConsumer::persistConsumerOffset() { - /*As do not execute rebalance for pullConsumer now, requestTable is always - empty - map requestTable = - m_pRebalance->getPullRequestTable(); - map::iterator it = requestTable.begin(); - vector mqs; - for (; it != requestTable.end(); ++it) - { - if (it->second) - { - mqs.push_back(it->first); - } - } - m_pOffsetStore->persistAll(mqs);*/ + /*As do not execute rebalance for pullConsumer now, requestTable is always + empty + map requestTable = + m_pRebalance->getPullRequestTable(); + map::iterator it = requestTable.begin(); + vector mqs; + for (; it != requestTable.end(); ++it) + { + if (it->second) + { + mqs.push_back(it->first); + } + } + m_pOffsetStore->persistAll(mqs);*/ } void DefaultMQPullConsumer::persistConsumerOffsetByResetOffset() {} -void DefaultMQPullConsumer::persistConsumerOffset4PullConsumer( - const MQMessageQueue& mq) { - if (isServiceStateOk()) { - m_pOffsetStore->persist(mq, getSessionCredentials()); - } +void DefaultMQPullConsumer::persistConsumerOffset4PullConsumer(const MQMessageQueue& mq) { + if (isServiceStateOk()) { + m_pOffsetStore->persist(mq, getSessionCredentials()); + } } -void DefaultMQPullConsumer::fetchMessageQueuesInBalance( - const string& topic, vector mqs) {} +void DefaultMQPullConsumer::fetchMessageQueuesInBalance(const string& topic, vector mqs) {} void DefaultMQPullConsumer::checkConfig() { - string groupname = getGroupName(); - // check consumerGroup - Validators::checkGroup(groupname); - - // consumerGroup - if (!groupname.compare(DEFAULT_CONSUMER_GROUP)) { - THROW_MQEXCEPTION(MQClientException, - "consumerGroup can not equal DEFAULT_CONSUMER", -1); - } - - if (getMessageModel() != BROADCASTING && getMessageModel() != CLUSTERING) { - THROW_MQEXCEPTION(MQClientException, "messageModel is valid ", -1); - } + string groupname = getGroupName(); + // check consumerGroup + Validators::checkGroup(groupname); + + // consumerGroup + if (!groupname.compare(DEFAULT_CONSUMER_GROUP)) { + THROW_MQEXCEPTION(MQClientException, "consumerGroup can not equal DEFAULT_CONSUMER", -1); + } + + if (getMessageModel() != BROADCASTING && getMessageModel() != CLUSTERING) { + THROW_MQEXCEPTION(MQClientException, "messageModel is valid ", -1); + } } void DefaultMQPullConsumer::doRebalance() {} void DefaultMQPullConsumer::copySubscription() { - set::iterator it = m_registerTopics.begin(); - for (; it != m_registerTopics.end(); ++it) { - unique_ptr subscriptionData( - FilterAPI::buildSubscriptionData((*it), SUB_ALL)); - m_pRebalance->setSubscriptionData((*it), subscriptionData.release()); - } + set::iterator it = m_registerTopics.begin(); + for (; it != m_registerTopics.end(); ++it) { + unique_ptr subscriptionData(FilterAPI::buildSubscriptionData((*it), SUB_ALL)); + m_pRebalance->setSubscriptionData((*it), subscriptionData.release()); + } } ConsumeType DefaultMQPullConsumer::getConsumeType() { return CONSUME_ACTIVELY; } -ConsumeFromWhere DefaultMQPullConsumer::getConsumeFromWhere() { - return CONSUME_FROM_LAST_OFFSET; -} +ConsumeFromWhere DefaultMQPullConsumer::getConsumeFromWhere() { return CONSUME_FROM_LAST_OFFSET; } void DefaultMQPullConsumer::getSubscriptions(vector& result) { - set::iterator it = m_registerTopics.begin(); - for (; it != m_registerTopics.end(); ++it) { - SubscriptionData ms(*it, SUB_ALL); - result.push_back(ms); - } + set::iterator it = m_registerTopics.begin(); + for (; it != m_registerTopics.end(); ++it) { + SubscriptionData ms(*it, SUB_ALL); + result.push_back(ms); + } } void DefaultMQPullConsumer::producePullMsgTask(PullRequest*) {} diff --git a/src/consumer/DefaultMQPushConsumer.cpp b/src/consumer/DefaultMQPushConsumer.cpp index 11e16edc2..89a681846 100644 --- a/src/consumer/DefaultMQPushConsumer.cpp +++ b/src/consumer/DefaultMQPushConsumer.cpp @@ -35,932 +35,851 @@ namespace rocketmq { - class AsyncPullCallback : public PullCallback { - public: - AsyncPullCallback(DefaultMQPushConsumer *pushConsumer, PullRequest *request) - : m_callbackOwner(pushConsumer), - m_pullRequest(request), - m_bShutdown(false) {} - - virtual ~AsyncPullCallback() { - m_callbackOwner = NULL; - m_pullRequest = NULL; - } - - virtual void onSuccess(MQMessageQueue &mq, PullResult &result, - bool bProducePullRequest) { - if (m_bShutdown == true) { - LOG_INFO("pullrequest for:%s in shutdown, return", - (m_pullRequest->m_messageQueue).toString().c_str()); - m_pullRequest->removePullMsgEvent(); - return; - } +class AsyncPullCallback : public PullCallback { +public: + AsyncPullCallback(DefaultMQPushConsumer *pushConsumer, PullRequest *request) + : m_callbackOwner(pushConsumer), m_pullRequest(request), m_bShutdown(false) {} - switch (result.pullStatus) { - case FOUND: { - if (!m_pullRequest->isDroped()) // if request is setted to dropped, - // don't add msgFoundList to - // m_msgTreeMap and don't call - // producePullMsgTask - { // avoid issue: pullMsg is sent out, rebalance is doing concurrently - // and this request is dropped, and then received pulled msgs. - m_pullRequest->setNextOffset(result.nextBeginOffset); - m_pullRequest->putMessage(result.msgFoundList); - - m_callbackOwner->getConsumerMsgService()->submitConsumeRequest( - m_pullRequest, result.msgFoundList); - - if (bProducePullRequest) - m_callbackOwner->producePullMsgTask(m_pullRequest); - else - m_pullRequest->removePullMsgEvent(); - - LOG_DEBUG("FOUND:%s with size:" - SIZET_FMT - ", nextBeginOffset:%lld", - (m_pullRequest->m_messageQueue).toString().c_str(), - result.msgFoundList.size(), result.nextBeginOffset); - } else { - LOG_INFO("remove pullmsg event of mq:%s", - (m_pullRequest->m_messageQueue).toString().c_str()); - m_pullRequest->removePullMsgEvent(); - } - break; - } - case NO_NEW_MSG: { - m_pullRequest->setNextOffset(result.nextBeginOffset); + virtual ~AsyncPullCallback() { + m_callbackOwner = NULL; + m_pullRequest = NULL; + } - vector msgs; - m_pullRequest->getMessage(msgs); - if ((msgs.size() == 0) && (result.nextBeginOffset > 0)) { - /*if broker losted/cleared msgs of one msgQueue, but the brokerOffset - is kept, then consumer will enter following situation: - 1>. get pull offset with 0 when do rebalance, and set - m_offsetTable[mq] to 0; - 2>. NO_NEW_MSG or NO_MATCHED_MSG got when pullMessage, and nextBegin - offset increase by 800 - 3>. request->getMessage(msgs) always NULL - 4>. we need update consumerOffset to nextBeginOffset indicated by - broker - but if really no new msg could be pulled, also go to this CASE - - LOG_INFO("maybe misMatch between broker and client happens, update - consumerOffset to nextBeginOffset indicated by broker");*/ - m_callbackOwner->updateConsumeOffset(m_pullRequest->m_messageQueue, - result.nextBeginOffset); - } - if (bProducePullRequest) - m_callbackOwner->producePullMsgTask(m_pullRequest); - else - m_pullRequest->removePullMsgEvent(); + virtual void onSuccess(MQMessageQueue &mq, PullResult &result, bool bProducePullRequest) { + if (m_bShutdown == true) { + LOG_INFO("pullrequest for:%s in shutdown, return", (m_pullRequest->m_messageQueue).toString().c_str()); + m_pullRequest->removePullMsgEvent(); + return; + } - /*LOG_INFO("NO_NEW_MSG:%s,nextBeginOffset:%lld", - (m_pullRequest->m_messageQueue).toString().c_str(), - result.nextBeginOffset);*/ - break; - } - case NO_MATCHED_MSG: { + switch (result.pullStatus) { + case FOUND: { + if (!m_pullRequest->isDroped()) // if request is setted to dropped, + // don't add msgFoundList to + // m_msgTreeMap and don't call + // producePullMsgTask + { // avoid issue: pullMsg is sent out, rebalance is doing concurrently + // and this request is dropped, and then received pulled msgs. m_pullRequest->setNextOffset(result.nextBeginOffset); + m_pullRequest->putMessage(result.msgFoundList); + + m_callbackOwner->getConsumerMsgService()->submitConsumeRequest(m_pullRequest, result.msgFoundList); - vector msgs; - m_pullRequest->getMessage(msgs); - if ((msgs.size() == 0) && (result.nextBeginOffset > 0)) { - /*if broker losted/cleared msgs of one msgQueue, but the brokerOffset - is kept, then consumer will enter following situation: - 1>. get pull offset with 0 when do rebalance, and set - m_offsetTable[mq] to 0; - 2>. NO_NEW_MSG or NO_MATCHED_MSG got when pullMessage, and nextBegin - offset increase by 800 - 3>. request->getMessage(msgs) always NULL - 4>. we need update consumerOffset to nextBeginOffset indicated by - broker - but if really no new msg could be pulled, also go to this CASE - - LOG_INFO("maybe misMatch between broker and client happens, update - consumerOffset to nextBeginOffset indicated by broker");*/ - m_callbackOwner->updateConsumeOffset(m_pullRequest->m_messageQueue, - result.nextBeginOffset); - } - if (bProducePullRequest) - m_callbackOwner->producePullMsgTask(m_pullRequest); - else - m_pullRequest->removePullMsgEvent(); - /*LOG_INFO("NO_MATCHED_MSG:%s,nextBeginOffset:%lld", - (m_pullRequest->m_messageQueue).toString().c_str(), - result.nextBeginOffset);*/ - break; - } - case OFFSET_ILLEGAL: { - m_pullRequest->setNextOffset(result.nextBeginOffset); if (bProducePullRequest) m_callbackOwner->producePullMsgTask(m_pullRequest); else m_pullRequest->removePullMsgEvent(); - /*LOG_INFO("OFFSET_ILLEGAL:%s,nextBeginOffset:%lld", - (m_pullRequest->m_messageQueue).toString().c_str(), - result.nextBeginOffset);*/ - break; + LOG_DEBUG("FOUND:%s with size:" SIZET_FMT ", nextBeginOffset:%lld", + (m_pullRequest->m_messageQueue).toString().c_str(), result.msgFoundList.size(), + result.nextBeginOffset); + } else { + LOG_INFO("remove pullmsg event of mq:%s", (m_pullRequest->m_messageQueue).toString().c_str()); + m_pullRequest->removePullMsgEvent(); } - case BROKER_TIMEOUT: { // as BROKER_TIMEOUT is defined by client, broker - // will not returns this status, so this case - // could not be entered. - LOG_ERROR("impossible BROKER_TIMEOUT Occurs"); - m_pullRequest->setNextOffset(result.nextBeginOffset); - if (bProducePullRequest) - m_callbackOwner->producePullMsgTask(m_pullRequest); - else - m_pullRequest->removePullMsgEvent(); - break; + break; + } + case NO_NEW_MSG: { + m_pullRequest->setNextOffset(result.nextBeginOffset); + + vector msgs; + m_pullRequest->getMessage(msgs); + if ((msgs.size() == 0) && (result.nextBeginOffset > 0)) { + /*if broker losted/cleared msgs of one msgQueue, but the brokerOffset + is kept, then consumer will enter following situation: + 1>. get pull offset with 0 when do rebalance, and set + m_offsetTable[mq] to 0; + 2>. NO_NEW_MSG or NO_MATCHED_MSG got when pullMessage, and nextBegin + offset increase by 800 + 3>. request->getMessage(msgs) always NULL + 4>. we need update consumerOffset to nextBeginOffset indicated by + broker + but if really no new msg could be pulled, also go to this CASE + + LOG_INFO("maybe misMatch between broker and client happens, update + consumerOffset to nextBeginOffset indicated by broker");*/ + m_callbackOwner->updateConsumeOffset(m_pullRequest->m_messageQueue, result.nextBeginOffset); + } + if (bProducePullRequest) + m_callbackOwner->producePullMsgTask(m_pullRequest); + else + m_pullRequest->removePullMsgEvent(); + + /*LOG_INFO("NO_NEW_MSG:%s,nextBeginOffset:%lld", + (m_pullRequest->m_messageQueue).toString().c_str(), + result.nextBeginOffset);*/ + break; + } + case NO_MATCHED_MSG: { + m_pullRequest->setNextOffset(result.nextBeginOffset); + + vector msgs; + m_pullRequest->getMessage(msgs); + if ((msgs.size() == 0) && (result.nextBeginOffset > 0)) { + /*if broker losted/cleared msgs of one msgQueue, but the brokerOffset + is kept, then consumer will enter following situation: + 1>. get pull offset with 0 when do rebalance, and set + m_offsetTable[mq] to 0; + 2>. NO_NEW_MSG or NO_MATCHED_MSG got when pullMessage, and nextBegin + offset increase by 800 + 3>. request->getMessage(msgs) always NULL + 4>. we need update consumerOffset to nextBeginOffset indicated by + broker + but if really no new msg could be pulled, also go to this CASE + + LOG_INFO("maybe misMatch between broker and client happens, update + consumerOffset to nextBeginOffset indicated by broker");*/ + m_callbackOwner->updateConsumeOffset(m_pullRequest->m_messageQueue, result.nextBeginOffset); } + if (bProducePullRequest) + m_callbackOwner->producePullMsgTask(m_pullRequest); + else + m_pullRequest->removePullMsgEvent(); + /*LOG_INFO("NO_MATCHED_MSG:%s,nextBeginOffset:%lld", + (m_pullRequest->m_messageQueue).toString().c_str(), + result.nextBeginOffset);*/ + break; + } + case OFFSET_ILLEGAL: { + m_pullRequest->setNextOffset(result.nextBeginOffset); + if (bProducePullRequest) + m_callbackOwner->producePullMsgTask(m_pullRequest); + else + m_pullRequest->removePullMsgEvent(); + + /*LOG_INFO("OFFSET_ILLEGAL:%s,nextBeginOffset:%lld", + (m_pullRequest->m_messageQueue).toString().c_str(), + result.nextBeginOffset);*/ + break; + } + case BROKER_TIMEOUT: { // as BROKER_TIMEOUT is defined by client, broker + // will not returns this status, so this case + // could not be entered. + LOG_ERROR("impossible BROKER_TIMEOUT Occurs"); + m_pullRequest->setNextOffset(result.nextBeginOffset); + if (bProducePullRequest) + m_callbackOwner->producePullMsgTask(m_pullRequest); + else + m_pullRequest->removePullMsgEvent(); + break; } } + } - virtual void onException(MQException &e) { - if (m_bShutdown == true) { - LOG_INFO("pullrequest for:%s in shutdown, return", - (m_pullRequest->m_messageQueue).toString().c_str()); - m_pullRequest->removePullMsgEvent(); - return; - } - LOG_WARN("pullrequest for:%s occurs exception, reproduce it", - (m_pullRequest->m_messageQueue).toString().c_str()); - m_callbackOwner->producePullMsgTask(m_pullRequest); + virtual void onException(MQException &e) { + if (m_bShutdown == true) { + LOG_INFO("pullrequest for:%s in shutdown, return", (m_pullRequest->m_messageQueue).toString().c_str()); + m_pullRequest->removePullMsgEvent(); + return; } + LOG_WARN("pullrequest for:%s occurs exception, reproduce it", + (m_pullRequest->m_messageQueue).toString().c_str()); + m_callbackOwner->producePullMsgTask(m_pullRequest); + } - void setShutdownStatus() { m_bShutdown = true; } + void setShutdownStatus() { m_bShutdown = true; } - private: - DefaultMQPushConsumer *m_callbackOwner; - PullRequest *m_pullRequest; - bool m_bShutdown; - }; +private: + DefaultMQPushConsumer *m_callbackOwner; + PullRequest *m_pullRequest; + bool m_bShutdown; +}; //second); - } - m_PullCallback.clear(); - m_subTopics.clear(); - } - - void DefaultMQPushConsumer::sendMessageBack(MQMessageExt &msg, int delayLevel) { +static boost::mutex m_asyncCallbackLock; + +DefaultMQPushConsumer::DefaultMQPushConsumer(const string &groupname) + : m_consumeFromWhere(CONSUME_FROM_LAST_OFFSET), + m_pOffsetStore(NULL), + m_pRebalance(NULL), + m_pPullAPIWrapper(NULL), + m_consumerService(NULL), + m_pMessageListener(NULL), + m_consumeMessageBatchMaxSize(1), + m_maxMsgCacheSize(1000), + m_pullmsgQueue(NULL) { + //second); + } + m_PullCallback.clear(); + m_subTopics.clear(); +} + +void DefaultMQPushConsumer::sendMessageBack(MQMessageExt &msg, int delayLevel) { + try { + getFactory()->getMQClientAPIImpl()->consumerSendMessageBack(msg, getGroupName(), delayLevel, 3000, + getSessionCredentials()); + } + catch (MQException &e) { + LOG_ERROR(e.what()); + } +} + +void DefaultMQPushConsumer::fetchSubscribeMessageQueues(const string &topic, vector &mqs) { + mqs.clear(); + try { + getFactory()->fetchSubscribeMessageQueues(topic, mqs, getSessionCredentials()); + } + catch (MQException &e) { + LOG_ERROR(e.what()); + } +} + +void DefaultMQPushConsumer::doRebalance() { + if (isServiceStateOk()) { try { - getFactory()->getMQClientAPIImpl()->consumerSendMessageBack( - msg, getGroupName(), delayLevel, 3000, getSessionCredentials()); - } catch (MQException &e) { - LOG_ERROR(e.what()); + m_pRebalance->doRebalance(); } - } - - void DefaultMQPushConsumer::fetchSubscribeMessageQueues( - const string &topic, vector &mqs) { - mqs.clear(); - try { - getFactory()->fetchSubscribeMessageQueues(topic, mqs, - getSessionCredentials()); - } catch (MQException &e) { + catch (MQException &e) { LOG_ERROR(e.what()); } } +} - void DefaultMQPushConsumer::doRebalance() { - if (isServiceStateOk()) { - try { - m_pRebalance->doRebalance(); - } catch (MQException &e) { - LOG_ERROR(e.what()); - } - } +void DefaultMQPushConsumer::persistConsumerOffset() { + if (isServiceStateOk()) { + m_pRebalance->persistConsumerOffset(); } +} - void DefaultMQPushConsumer::persistConsumerOffset() { - if (isServiceStateOk()) { - m_pRebalance->persistConsumerOffset(); - } +void DefaultMQPushConsumer::persistConsumerOffsetByResetOffset() { + if (isServiceStateOk()) { + m_pRebalance->persistConsumerOffsetByResetOffset(); } +} - void DefaultMQPushConsumer::persistConsumerOffsetByResetOffset() { - if (isServiceStateOk()) { - m_pRebalance->persistConsumerOffsetByResetOffset(); - } - } - - void DefaultMQPushConsumer::start() { +void DefaultMQPushConsumer::start() { #ifndef WIN32 - /* Ignore the SIGPIPE */ - struct sigaction sa; - memset(&sa,0, sizeof(struct sigaction)); - sa.sa_handler = SIG_IGN; - sa.sa_flags = 0; - sigaction(SIGPIPE, &sa, 0); + /* Ignore the SIGPIPE */ + struct sigaction sa; + memset(&sa, 0, sizeof(struct sigaction)); + sa.sa_handler = SIG_IGN; + sa.sa_flags = 0; + sigaction(SIGPIPE, &sa, 0); #endif - switch (m_serviceState) { - case CREATE_JUST: { - m_serviceState = START_FAILED; - MQClient::start(); - LOG_INFO("DefaultMQPushConsumer:%s start", m_GroupName.c_str()); - - //getMessageListenerType() == - messageListenerOrderly) { - LOG_INFO("start orderly consume service:%s", getGroupName().c_str()); - m_consumerService = new ConsumeMessageOrderlyService( - this, m_consumeThreadCount, m_pMessageListener); - } else // for backward compatible, defaultly and concurrently listeners + switch (m_serviceState) { + case CREATE_JUST: { + m_serviceState = START_FAILED; + MQClient::start(); + LOG_INFO("DefaultMQPushConsumer:%s start", m_GroupName.c_str()); + + //getMessageListenerType() == messageListenerOrderly) { + LOG_INFO("start orderly consume service:%s", getGroupName().c_str()); + m_consumerService = + new ConsumeMessageOrderlyService(this, m_consumeThreadCount, m_pMessageListener); + } else // for backward compatible, defaultly and concurrently listeners // are allocating ConsumeMessageConcurrentlyService - { - LOG_INFO("start concurrently consume service:%s", - getGroupName().c_str()); - m_consumerService = new ConsumeMessageConcurrentlyService( - this, m_consumeThreadCount, m_pMessageListener); - } + { + LOG_INFO("start concurrently consume service:%s", getGroupName().c_str()); + m_consumerService = + new ConsumeMessageConcurrentlyService(this, m_consumeThreadCount, m_pMessageListener); } + } - m_pullmsgQueue = new TaskQueue(m_pullMsgThreadPoolNum); - m_pullmsgThread.reset(new boost::thread(boost::bind( - &DefaultMQPushConsumer::runPullMsgQueue, this, m_pullmsgQueue))); - - copySubscription(); - - //registerConsumer(this); - if (!registerOK) { - m_serviceState = CREATE_JUST; - THROW_MQEXCEPTION( - MQClientException, - "The cousumer group[" + getGroupName() + - "] has been created before, specify another name please.", - -1); - } + m_pullmsgQueue = new TaskQueue(m_pullMsgThreadPoolNum); + m_pullmsgThread.reset( + new boost::thread(boost::bind(&DefaultMQPushConsumer::runPullMsgQueue, this, m_pullmsgQueue))); - //load(); - } catch (MQClientException &e) { - bStartFailed = true; - errorMsg = std::string(e.what()); - } - m_consumerService->start(); - - getFactory()->start(); + copySubscription(); - updateTopicSubscribeInfoWhenSubscriptionChanged(); - getFactory()->sendHeartbeatToAllBroker(); + //registerConsumer(this); + if (!registerOK) { + m_serviceState = CREATE_JUST; + THROW_MQEXCEPTION(MQClientException, "The cousumer group[" + getGroupName() + + "] has been created before, specify another name please.", + -1); + } - m_serviceState = RUNNING; - if (bStartFailed) { - shutdown(); - THROW_MQEXCEPTION(MQClientException, errorMsg, -1); - } - break; + //load(); + } + catch (MQClientException &e) { + bStartFailed = true; + errorMsg = std::string(e.what()); + } + m_consumerService->start(); - getFactory()->rebalanceImmediately(); - } - - void DefaultMQPushConsumer::shutdown() { - switch (m_serviceState) { - case RUNNING: { - LOG_INFO("DefaultMQPushConsumer shutdown"); - m_async_ioService.stop(); - m_async_service_thread->interrupt(); - m_async_service_thread->join(); - m_pullmsgQueue->close(); - m_pullmsgThread->interrupt(); - m_pullmsgThread->join(); - m_consumerService->shutdown(); - persistConsumerOffset(); - shutdownAsyncPullCallBack(); // delete aync pullMsg resources - getFactory()->unregisterConsumer(this); - getFactory()->shutdown(); - m_serviceState = SHUTDOWN_ALREADY; - break; + getFactory()->start(); + + updateTopicSubscribeInfoWhenSubscriptionChanged(); + getFactory()->sendHeartbeatToAllBroker(); + + m_serviceState = RUNNING; + if (bStartFailed) { + shutdown(); + THROW_MQEXCEPTION(MQClientException, errorMsg, -1); } - case CREATE_JUST: - case SHUTDOWN_ALREADY: - break; - default: - break; + break; } + case RUNNING: + case START_FAILED: + case SHUTDOWN_ALREADY: + break; + default: + break; } - void DefaultMQPushConsumer::registerMessageListener( - MQMessageListener *pMessageListener) { - if (NULL != pMessageListener) { - m_pMessageListener = pMessageListener; - } - } + getFactory()->rebalanceImmediately(); +} - MessageListenerType DefaultMQPushConsumer::getMessageListenerType() { - if (NULL != m_pMessageListener) { - return m_pMessageListener->getMessageListenerType(); +void DefaultMQPushConsumer::shutdown() { + switch (m_serviceState) { + case RUNNING: { + LOG_INFO("DefaultMQPushConsumer shutdown"); + m_async_ioService.stop(); + m_async_service_thread->interrupt(); + m_async_service_thread->join(); + m_pullmsgQueue->close(); + m_pullmsgThread->interrupt(); + m_pullmsgThread->join(); + m_consumerService->shutdown(); + persistConsumerOffset(); + shutdownAsyncPullCallBack(); // delete aync pullMsg resources + getFactory()->unregisterConsumer(this); + getFactory()->shutdown(); + m_serviceState = SHUTDOWN_ALREADY; + break; } - return messageListenerDefaultly; + case CREATE_JUST: + case SHUTDOWN_ALREADY: + break; + default: + break; } +} - ConsumeMsgService *DefaultMQPushConsumer::getConsumerMsgService() const { - return m_consumerService; +void DefaultMQPushConsumer::registerMessageListener(MQMessageListener *pMessageListener) { + if (NULL != pMessageListener) { + m_pMessageListener = pMessageListener; } +} - OffsetStore *DefaultMQPushConsumer::getOffsetStore() const { - return m_pOffsetStore; +MessageListenerType DefaultMQPushConsumer::getMessageListenerType() { + if (NULL != m_pMessageListener) { + return m_pMessageListener->getMessageListenerType(); } + return messageListenerDefaultly; +} - Rebalance *DefaultMQPushConsumer::getRebalance() const { return m_pRebalance; } +ConsumeMsgService *DefaultMQPushConsumer::getConsumerMsgService() const { return m_consumerService; } - void DefaultMQPushConsumer::subscribe(const string &topic, - const string &subExpression) { - m_subTopics[topic] = subExpression; - } +OffsetStore *DefaultMQPushConsumer::getOffsetStore() const { return m_pOffsetStore; } - void DefaultMQPushConsumer::checkConfig() { - string groupname = getGroupName(); - // check consumerGroup - Validators::checkGroup(groupname); +Rebalance *DefaultMQPushConsumer::getRebalance() const { return m_pRebalance; } - // consumerGroup - if (!groupname.compare(DEFAULT_CONSUMER_GROUP)) { - THROW_MQEXCEPTION(MQClientException, - "consumerGroup can not equal DEFAULT_CONSUMER", -1); - } +void DefaultMQPushConsumer::subscribe(const string &topic, const string &subExpression) { + m_subTopics[topic] = subExpression; +} - if (getMessageModel() != BROADCASTING && getMessageModel() != CLUSTERING) { - THROW_MQEXCEPTION(MQClientException, "messageModel is valid ", -1); - } +void DefaultMQPushConsumer::checkConfig() { + string groupname = getGroupName(); + // check consumerGroup + Validators::checkGroup(groupname); - if (m_pMessageListener == NULL) { - THROW_MQEXCEPTION(MQClientException, "messageListener is null ", -1); - } + // consumerGroup + if (!groupname.compare(DEFAULT_CONSUMER_GROUP)) { + THROW_MQEXCEPTION(MQClientException, "consumerGroup can not equal DEFAULT_CONSUMER", -1); } - void DefaultMQPushConsumer::copySubscription() { - map::iterator it = m_subTopics.begin(); - for (; it != m_subTopics.end(); ++it) { - LOG_INFO("buildSubscriptionData,:%s,%s", it->first.c_str(), - it->second.c_str()); - unique_ptr pSData( - FilterAPI::buildSubscriptionData(it->first, it->second)); + if (getMessageModel() != BROADCASTING && getMessageModel() != CLUSTERING) { + THROW_MQEXCEPTION(MQClientException, "messageModel is valid ", -1); + } - m_pRebalance->setSubscriptionData(it->first, pSData.release()); - } + if (m_pMessageListener == NULL) { + THROW_MQEXCEPTION(MQClientException, "messageListener is null ", -1); + } +} - switch (getMessageModel()) { - case BROADCASTING: - break; - case CLUSTERING: { - string retryTopic = UtilAll::getRetryTopic(getGroupName()); +void DefaultMQPushConsumer::copySubscription() { + map::iterator it = m_subTopics.begin(); + for (; it != m_subTopics.end(); ++it) { + LOG_INFO("buildSubscriptionData,:%s,%s", it->first.c_str(), it->second.c_str()); + unique_ptr pSData(FilterAPI::buildSubscriptionData(it->first, it->second)); - // pSData( - FilterAPI::buildSubscriptionData(retryTopic, SUB_ALL)); + m_pRebalance->setSubscriptionData(it->first, pSData.release()); + } - m_pRebalance->setSubscriptionData(retryTopic, pSData.release()); - break; - } - default: - break; + switch (getMessageModel()) { + case BROADCASTING: + break; + case CLUSTERING: { + string retryTopic = UtilAll::getRetryTopic(getGroupName()); + + // pSData(FilterAPI::buildSubscriptionData(retryTopic, SUB_ALL)); + + m_pRebalance->setSubscriptionData(retryTopic, pSData.release()); + break; } + default: + break; } +} - void DefaultMQPushConsumer::updateTopicSubscribeInfo( - const string &topic, vector &info) { - m_pRebalance->setTopicSubscribeInfo(topic, info); - } +void DefaultMQPushConsumer::updateTopicSubscribeInfo(const string &topic, vector &info) { + m_pRebalance->setTopicSubscribeInfo(topic, info); +} - void DefaultMQPushConsumer::updateTopicSubscribeInfoWhenSubscriptionChanged() { - map &subTable = - m_pRebalance->getSubscriptionInner(); - map::iterator it = subTable.begin(); - for (; it != subTable.end(); ++it) { - bool btopic = getFactory()->updateTopicRouteInfoFromNameServer( - it->first, getSessionCredentials()); - if (btopic == false) { - LOG_WARN("The topic:[%s] not exist", it->first.c_str()); - } +void DefaultMQPushConsumer::updateTopicSubscribeInfoWhenSubscriptionChanged() { + map &subTable = m_pRebalance->getSubscriptionInner(); + map::iterator it = subTable.begin(); + for (; it != subTable.end(); ++it) { + bool btopic = getFactory()->updateTopicRouteInfoFromNameServer(it->first, getSessionCredentials()); + if (btopic == false) { + LOG_WARN("The topic:[%s] not exist", it->first.c_str()); } } +} - ConsumeType DefaultMQPushConsumer::getConsumeType() { - return CONSUME_PASSIVELY; - } +ConsumeType DefaultMQPushConsumer::getConsumeType() { return CONSUME_PASSIVELY; } - ConsumeFromWhere DefaultMQPushConsumer::getConsumeFromWhere() { - return m_consumeFromWhere; - } +ConsumeFromWhere DefaultMQPushConsumer::getConsumeFromWhere() { return m_consumeFromWhere; } - void DefaultMQPushConsumer::setConsumeFromWhere( - ConsumeFromWhere consumeFromWhere) { - m_consumeFromWhere = consumeFromWhere; - } +void DefaultMQPushConsumer::setConsumeFromWhere(ConsumeFromWhere consumeFromWhere) { + m_consumeFromWhere = consumeFromWhere; +} - void DefaultMQPushConsumer::getSubscriptions(vector &result) { - map &subTable = - m_pRebalance->getSubscriptionInner(); - map::iterator it = subTable.begin(); - for (; it != subTable.end(); ++it) { - result.push_back(*(it->second)); - } +void DefaultMQPushConsumer::getSubscriptions(vector &result) { + map &subTable = m_pRebalance->getSubscriptionInner(); + map::iterator it = subTable.begin(); + for (; it != subTable.end(); ++it) { + result.push_back(*(it->second)); } +} - void DefaultMQPushConsumer::updateConsumeOffset(const MQMessageQueue &mq, - int64 offset) { - if (offset >= 0) { - m_pOffsetStore->updateOffset(mq, offset); - } else { - LOG_ERROR("updateConsumeOffset of mq:%s error", mq.toString().c_str()); - } +void DefaultMQPushConsumer::updateConsumeOffset(const MQMessageQueue &mq, int64 offset) { + if (offset >= 0) { + m_pOffsetStore->updateOffset(mq, offset); + } else { + LOG_ERROR("updateConsumeOffset of mq:%s error", mq.toString().c_str()); } +} - void DefaultMQPushConsumer::removeConsumeOffset(const MQMessageQueue &mq) { - m_pOffsetStore->removeOffset(mq); - } +void DefaultMQPushConsumer::removeConsumeOffset(const MQMessageQueue &mq) { m_pOffsetStore->removeOffset(mq); } - void DefaultMQPushConsumer::triggerNextPullRequest( - boost::asio::deadline_timer *t, PullRequest *request) { - // LOG_INFO("trigger pullrequest for:%s", - // (request->m_messageQueue).toString().c_str()); - producePullMsgTask(request); - deleteAndZero(t); - } +void DefaultMQPushConsumer::triggerNextPullRequest(boost::asio::deadline_timer *t, PullRequest *request) { + // LOG_INFO("trigger pullrequest for:%s", + // (request->m_messageQueue).toString().c_str()); + producePullMsgTask(request); + deleteAndZero(t); +} - void DefaultMQPushConsumer::producePullMsgTask(PullRequest *request) { - if (m_pullmsgQueue->bTaskQueueStatusOK() && isServiceStateOk()) { - request->addPullMsgEvent(); - if (m_asyncPull) { - m_pullmsgQueue->produce(TaskBinder::gen( - &DefaultMQPushConsumer::pullMessageAsync, this, request)); - } else { - m_pullmsgQueue->produce( - TaskBinder::gen(&DefaultMQPushConsumer::pullMessage, this, request)); - } +void DefaultMQPushConsumer::producePullMsgTask(PullRequest *request) { + if (m_pullmsgQueue->bTaskQueueStatusOK() && isServiceStateOk()) { + request->addPullMsgEvent(); + if (m_asyncPull) { + m_pullmsgQueue->produce(TaskBinder::gen(&DefaultMQPushConsumer::pullMessageAsync, this, request)); } else { - LOG_WARN("produce pullmsg of mq:%s failed", - request->m_messageQueue.toString().c_str()); + m_pullmsgQueue->produce(TaskBinder::gen(&DefaultMQPushConsumer::pullMessage, this, request)); } + } else { + LOG_WARN("produce pullmsg of mq:%s failed", request->m_messageQueue.toString().c_str()); } +} - void DefaultMQPushConsumer::runPullMsgQueue(TaskQueue *pTaskQueue) { - pTaskQueue->run(); - } +void DefaultMQPushConsumer::runPullMsgQueue(TaskQueue *pTaskQueue) { pTaskQueue->run(); } - void DefaultMQPushConsumer::pullMessage(PullRequest *request) { - if (request == NULL) { - LOG_ERROR("Pull request is NULL, return"); - return; - } - if (request->isDroped()) { - LOG_WARN("Pull request is set drop with mq:%s, return", - (request->m_messageQueue).toString().c_str()); - request->removePullMsgEvent(); - return; - } +void DefaultMQPushConsumer::pullMessage(PullRequest *request) { + if (request == NULL) { + LOG_ERROR("Pull request is NULL, return"); + return; + } + if (request->isDroped()) { + LOG_WARN("Pull request is set drop with mq:%s, return", (request->m_messageQueue).toString().c_str()); + request->removePullMsgEvent(); + return; + } - MQMessageQueue &messageQueue = request->m_messageQueue; - if (m_consumerService->getConsumeMsgSerivceListenerType() == - messageListenerOrderly) { - if (!request->isLocked() || request->isLockExpired()) { - if (!m_pRebalance->lock(messageQueue)) { - producePullMsgTask(request); - return; - } + MQMessageQueue &messageQueue = request->m_messageQueue; + if (m_consumerService->getConsumeMsgSerivceListenerType() == messageListenerOrderly) { + if (!request->isLocked() || request->isLockExpired()) { + if (!m_pRebalance->lock(messageQueue)) { + producePullMsgTask(request); + return; } } + } - if (request->getCacheMsgCount() > m_maxMsgCacheSize) { - // LOG_INFO("retry pullrequest for:%s after 1s, as cachMsgSize:%d is larger - // than:%d", (request->m_messageQueue).toString().c_str(), - // request->getCacheMsgCount(), m_maxMsgCacheSize); - boost::asio::deadline_timer *t = new boost::asio::deadline_timer( - m_async_ioService, boost::posix_time::milliseconds(1 * 1000)); - t->async_wait(boost::bind(&DefaultMQPushConsumer::triggerNextPullRequest, - this, t, request)); - return; - } - - bool commitOffsetEnable = false; - int64 commitOffsetValue = 0; - if (CLUSTERING == getMessageModel()) { - commitOffsetValue = m_pOffsetStore->readOffset( - messageQueue, READ_FROM_MEMORY, getSessionCredentials()); - if (commitOffsetValue > 0) { - commitOffsetEnable = true; - } - } + if (request->getCacheMsgCount() > m_maxMsgCacheSize) { + // LOG_INFO("retry pullrequest for:%s after 1s, as cachMsgSize:%d is larger + // than:%d", (request->m_messageQueue).toString().c_str(), + // request->getCacheMsgCount(), m_maxMsgCacheSize); + boost::asio::deadline_timer *t = + new boost::asio::deadline_timer(m_async_ioService, boost::posix_time::milliseconds(1 * 1000)); + t->async_wait(boost::bind(&DefaultMQPushConsumer::triggerNextPullRequest, this, t, request)); + return; + } - string subExpression; - SubscriptionData *pSdata = - m_pRebalance->getSubscriptionData(messageQueue.getTopic()); - if (pSdata == NULL) { - producePullMsgTask(request); - return; + bool commitOffsetEnable = false; + int64 commitOffsetValue = 0; + if (CLUSTERING == getMessageModel()) { + commitOffsetValue = m_pOffsetStore->readOffset(messageQueue, READ_FROM_MEMORY, getSessionCredentials()); + if (commitOffsetValue > 0) { + commitOffsetEnable = true; } - subExpression = pSdata->getSubString(); - - int sysFlag = - PullSysFlag::buildSysFlag(commitOffsetEnable, // commitOffset - false, // suspend - !subExpression.empty(), // subscription - false); // class filter - - try { - request->setLastPullTimestamp(UtilAll::currentTimeMillis()); - unique_ptr result( - m_pPullAPIWrapper->pullKernelImpl(messageQueue, // 1 - subExpression, // 2 - pSdata->getSubVersion(), // 3 - request->getNextOffset(), // 4 - 32, // 5 - sysFlag, // 6 - commitOffsetValue, // 7 - 1000 * 15, // 8 - 1000 * 30, // 9 - ComMode_SYNC, // 10 - NULL, getSessionCredentials())); - - PullResult pullResult = m_pPullAPIWrapper->processPullResult( - messageQueue, result.get(), pSdata); - - switch (pullResult.pullStatus) { - case FOUND: { - if (!request->isDroped()) // if request is setted to dropped, don't add - // msgFoundList to m_msgTreeMap and don't - // call producePullMsgTask - { // avoid issue: pullMsg is sent out, rebalance is doing concurrently - // and this request is dropped, and then received pulled msgs. - request->setNextOffset(pullResult.nextBeginOffset); - request->putMessage(pullResult.msgFoundList); - - m_consumerService->submitConsumeRequest(request, - pullResult.msgFoundList); - producePullMsgTask(request); - - LOG_DEBUG("FOUND:%s with size:" - SIZET_FMT - ",nextBeginOffset:%lld", - messageQueue.toString().c_str(), - pullResult.msgFoundList.size(), pullResult.nextBeginOffset); - } else { - request->removePullMsgEvent(); - } - break; - } - case NO_NEW_MSG: { - request->setNextOffset(pullResult.nextBeginOffset); - vector msgs; - request->getMessage(msgs); - if ((msgs.size() == 0) && (pullResult.nextBeginOffset > 0)) { - /*if broker losted/cleared msgs of one msgQueue, but the brokerOffset - is kept, then consumer will enter following situation: - 1>. get pull offset with 0 when do rebalance, and set - m_offsetTable[mq] to 0; - 2>. NO_NEW_MSG or NO_MATCHED_MSG got when pullMessage, and nextBegin - offset increase by 800 - 3>. request->getMessage(msgs) always NULL - 4>. we need update consumerOffset to nextBeginOffset indicated by - broker - but if really no new msg could be pulled, also go to this CASE - */ - // LOG_DEBUG("maybe misMatch between broker and client happens, update - // consumerOffset to nextBeginOffset indicated by broker"); - updateConsumeOffset(messageQueue, pullResult.nextBeginOffset); - } - producePullMsgTask(request); - LOG_DEBUG("NO_NEW_MSG:%s,nextBeginOffset:%lld", - messageQueue.toString().c_str(), pullResult.nextBeginOffset); - break; - } - case NO_MATCHED_MSG: { - request->setNextOffset(pullResult.nextBeginOffset); - vector msgs; - request->getMessage(msgs); - if ((msgs.size() == 0) && (pullResult.nextBeginOffset > 0)) { - // LOG_DEBUG("maybe misMatch between broker and client happens, update - // consumerOffset to nextBeginOffset indicated by broker"); - updateConsumeOffset(messageQueue, pullResult.nextBeginOffset); - } - producePullMsgTask(request); + } - LOG_DEBUG("NO_MATCHED_MSG:%s,nextBeginOffset:%lld", - messageQueue.toString().c_str(), pullResult.nextBeginOffset); - break; - } - case OFFSET_ILLEGAL: { + string subExpression; + SubscriptionData *pSdata = m_pRebalance->getSubscriptionData(messageQueue.getTopic()); + if (pSdata == NULL) { + producePullMsgTask(request); + return; + } + subExpression = pSdata->getSubString(); + + int sysFlag = PullSysFlag::buildSysFlag(commitOffsetEnable, // commitOffset + false, // suspend + !subExpression.empty(), // subscription + false); // class filter + + try { + request->setLastPullTimestamp(UtilAll::currentTimeMillis()); + unique_ptr result(m_pPullAPIWrapper->pullKernelImpl(messageQueue, // 1 + subExpression, // 2 + pSdata->getSubVersion(), // 3 + request->getNextOffset(), // 4 + 32, // 5 + sysFlag, // 6 + commitOffsetValue, // 7 + 1000 * 15, // 8 + 1000 * 30, // 9 + ComMode_SYNC, // 10 + NULL, getSessionCredentials())); + + PullResult pullResult = m_pPullAPIWrapper->processPullResult(messageQueue, result.get(), pSdata); + + switch (pullResult.pullStatus) { + case FOUND: { + if (!request->isDroped()) // if request is setted to dropped, don't add + // msgFoundList to m_msgTreeMap and don't + // call producePullMsgTask + { // avoid issue: pullMsg is sent out, rebalance is doing concurrently + // and this request is dropped, and then received pulled msgs. request->setNextOffset(pullResult.nextBeginOffset); - producePullMsgTask(request); + request->putMessage(pullResult.msgFoundList); - LOG_DEBUG("OFFSET_ILLEGAL:%s,nextBeginOffset:%lld", - messageQueue.toString().c_str(), pullResult.nextBeginOffset); - break; - } - case BROKER_TIMEOUT: { // as BROKER_TIMEOUT is defined by client, broker - // will not returns this status, so this case - // could not be entered. - LOG_ERROR("impossible BROKER_TIMEOUT Occurs"); - request->setNextOffset(pullResult.nextBeginOffset); + m_consumerService->submitConsumeRequest(request, pullResult.msgFoundList); producePullMsgTask(request); - break; - } - } - } catch (MQException &e) { - LOG_ERROR(e.what()); - producePullMsgTask(request); - } - } - - AsyncPullCallback *DefaultMQPushConsumer::getAsyncPullCallBack( - PullRequest *request, MQMessageQueue msgQueue) { - boost::lock_guard lock(m_asyncCallbackLock); - if (m_asyncPull && request) { - PullMAP::iterator it = m_PullCallback.find(msgQueue); - if (it == m_PullCallback.end()) { - LOG_INFO("new pull callback for mq:%s", msgQueue.toString().c_str()); - m_PullCallback[msgQueue] = new AsyncPullCallback(this, request); - } - return m_PullCallback[msgQueue]; - } - return NULL; - } - - void DefaultMQPushConsumer::shutdownAsyncPullCallBack() { - boost::lock_guard lock(m_asyncCallbackLock); - if (m_asyncPull) { - PullMAP::iterator it = m_PullCallback.begin(); - for (; it != m_PullCallback.end(); ++it) { - if (it->second) { - it->second->setShutdownStatus(); + LOG_DEBUG("FOUND:%s with size:" SIZET_FMT ",nextBeginOffset:%lld", messageQueue.toString().c_str(), + pullResult.msgFoundList.size(), pullResult.nextBeginOffset); } else { - LOG_ERROR("could not find asyncPullCallback for:%s", - it->first.toString().c_str()); + request->removePullMsgEvent(); } + break; } - } - } - - void DefaultMQPushConsumer::pullMessageAsync(PullRequest *request) { - if (request == NULL) { - LOG_ERROR("Pull request is NULL, return"); - return; - } - if (request->isDroped()) { - LOG_WARN("Pull request is set drop with mq:%s, return", - (request->m_messageQueue).toString().c_str()); - request->removePullMsgEvent(); - return; - } - - MQMessageQueue &messageQueue = request->m_messageQueue; - if (m_consumerService->getConsumeMsgSerivceListenerType() == - messageListenerOrderly) { - if (!request->isLocked() || request->isLockExpired()) { - if (!m_pRebalance->lock(messageQueue)) { - producePullMsgTask(request); - return; + case NO_NEW_MSG: { + request->setNextOffset(pullResult.nextBeginOffset); + vector msgs; + request->getMessage(msgs); + if ((msgs.size() == 0) && (pullResult.nextBeginOffset > 0)) { + /*if broker losted/cleared msgs of one msgQueue, but the brokerOffset + is kept, then consumer will enter following situation: + 1>. get pull offset with 0 when do rebalance, and set + m_offsetTable[mq] to 0; + 2>. NO_NEW_MSG or NO_MATCHED_MSG got when pullMessage, and nextBegin + offset increase by 800 + 3>. request->getMessage(msgs) always NULL + 4>. we need update consumerOffset to nextBeginOffset indicated by + broker + but if really no new msg could be pulled, also go to this CASE + */ + // LOG_DEBUG("maybe misMatch between broker and client happens, update + // consumerOffset to nextBeginOffset indicated by broker"); + updateConsumeOffset(messageQueue, pullResult.nextBeginOffset); } + producePullMsgTask(request); + LOG_DEBUG("NO_NEW_MSG:%s,nextBeginOffset:%lld", messageQueue.toString().c_str(), + pullResult.nextBeginOffset); + break; } - } - - if (request->getCacheMsgCount() > m_maxMsgCacheSize) { - // LOG_INFO("retry pullrequest for:%s after 1s, as cachMsgSize:%d is larger - // than:%d", (request->m_messageQueue).toString().c_str(), - // request->getCacheMsgCount(), m_maxMsgCacheSize); - boost::asio::deadline_timer *t = new boost::asio::deadline_timer( - m_async_ioService, boost::posix_time::milliseconds(1 * 1000)); - t->async_wait(boost::bind(&DefaultMQPushConsumer::triggerNextPullRequest, - this, t, request)); - return; - } + case NO_MATCHED_MSG: { + request->setNextOffset(pullResult.nextBeginOffset); + vector msgs; + request->getMessage(msgs); + if ((msgs.size() == 0) && (pullResult.nextBeginOffset > 0)) { + // LOG_DEBUG("maybe misMatch between broker and client happens, update + // consumerOffset to nextBeginOffset indicated by broker"); + updateConsumeOffset(messageQueue, pullResult.nextBeginOffset); + } + producePullMsgTask(request); - bool commitOffsetEnable = false; - int64 commitOffsetValue = 0; - if (CLUSTERING == getMessageModel()) { - commitOffsetValue = m_pOffsetStore->readOffset( - messageQueue, READ_FROM_MEMORY, getSessionCredentials()); - if (commitOffsetValue > 0) { - commitOffsetEnable = true; + LOG_DEBUG("NO_MATCHED_MSG:%s,nextBeginOffset:%lld", messageQueue.toString().c_str(), + pullResult.nextBeginOffset); + break; } - } + case OFFSET_ILLEGAL: { + request->setNextOffset(pullResult.nextBeginOffset); + producePullMsgTask(request); - string subExpression; - SubscriptionData *pSdata = - (m_pRebalance->getSubscriptionData(messageQueue.getTopic())); - if (pSdata == NULL) { - producePullMsgTask(request); - return; - } - subExpression = pSdata->getSubString(); - - int sysFlag = - PullSysFlag::buildSysFlag(commitOffsetEnable, // commitOffset - true, // suspend - !subExpression.empty(), // subscription - false); // class filter - - AsyncArg arg; - arg.mq = messageQueue; - arg.subData = *pSdata; - arg.pPullWrapper = m_pPullAPIWrapper; - arg.pPullRequest = request; - try { - request->setLastPullTimestamp(UtilAll::currentTimeMillis()); - m_pPullAPIWrapper->pullKernelImpl( - messageQueue, // 1 - subExpression, // 2 - pSdata->getSubVersion(), // 3 - request->getNextOffset(), // 4 - 32, // 5 - sysFlag, // 6 - commitOffsetValue, // 7 - 1000 * 15, // 8 - m_asyncPullTimeout, // 9 - ComMode_ASYNC, // 10 - getAsyncPullCallBack(request, messageQueue), // 11 - getSessionCredentials(), // 12 - &arg); // 13 - } catch (MQException &e) { - LOG_ERROR(e.what()); - producePullMsgTask(request); + LOG_DEBUG("OFFSET_ILLEGAL:%s,nextBeginOffset:%lld", messageQueue.toString().c_str(), + pullResult.nextBeginOffset); + break; + } + case BROKER_TIMEOUT: { // as BROKER_TIMEOUT is defined by client, broker + // will not returns this status, so this case + // could not be entered. + LOG_ERROR("impossible BROKER_TIMEOUT Occurs"); + request->setNextOffset(pullResult.nextBeginOffset); + producePullMsgTask(request); + break; + } } } - - void DefaultMQPushConsumer::setAsyncPull(bool asyncFlag) { - if (asyncFlag) { - LOG_INFO("set pushConsumer:%s to async default pull mode", - getGroupName().c_str()); - } else { - LOG_INFO("set pushConsumer:%s to sync pull mode", getGroupName().c_str()); - } - m_asyncPull = asyncFlag; + catch (MQException &e) { + LOG_ERROR(e.what()); + producePullMsgTask(request); } +} - void DefaultMQPushConsumer::setConsumeThreadCount(int threadCount) { - if (threadCount > 0) { - m_consumeThreadCount = threadCount; - } else { - LOG_ERROR("setConsumeThreadCount with invalid value"); +AsyncPullCallback *DefaultMQPushConsumer::getAsyncPullCallBack(PullRequest *request, MQMessageQueue msgQueue) { + boost::lock_guard lock(m_asyncCallbackLock); + if (m_asyncPull && request) { + PullMAP::iterator it = m_PullCallback.find(msgQueue); + if (it == m_PullCallback.end()) { + LOG_INFO("new pull callback for mq:%s", msgQueue.toString().c_str()); + m_PullCallback[msgQueue] = new AsyncPullCallback(this, request); } + return m_PullCallback[msgQueue]; } - int DefaultMQPushConsumer::getConsumeThreadCount() const { - return m_consumeThreadCount; - } + return NULL; +} - void DefaultMQPushConsumer::setPullMsgThreadPoolCount(int threadCount) { - m_pullMsgThreadPoolNum = threadCount; +void DefaultMQPushConsumer::shutdownAsyncPullCallBack() { + boost::lock_guard lock(m_asyncCallbackLock); + if (m_asyncPull) { + PullMAP::iterator it = m_PullCallback.begin(); + for (; it != m_PullCallback.end(); ++it) { + if (it->second) { + it->second->setShutdownStatus(); + } else { + LOG_ERROR("could not find asyncPullCallback for:%s", it->first.toString().c_str()); + } + } } +} - int DefaultMQPushConsumer::getPullMsgThreadPoolCount() const { - return m_pullMsgThreadPoolNum; +void DefaultMQPushConsumer::pullMessageAsync(PullRequest *request) { + if (request == NULL) { + LOG_ERROR("Pull request is NULL, return"); + return; + } + if (request->isDroped()) { + LOG_WARN("Pull request is set drop with mq:%s, return", (request->m_messageQueue).toString().c_str()); + request->removePullMsgEvent(); + return; } - int DefaultMQPushConsumer::getConsumeMessageBatchMaxSize() const { - return m_consumeMessageBatchMaxSize; + MQMessageQueue &messageQueue = request->m_messageQueue; + if (m_consumerService->getConsumeMsgSerivceListenerType() == messageListenerOrderly) { + if (!request->isLocked() || request->isLockExpired()) { + if (!m_pRebalance->lock(messageQueue)) { + producePullMsgTask(request); + return; + } + } } - void DefaultMQPushConsumer::setConsumeMessageBatchMaxSize( - int consumeMessageBatchMaxSize) { - if (consumeMessageBatchMaxSize >= 1) - m_consumeMessageBatchMaxSize = consumeMessageBatchMaxSize; + if (request->getCacheMsgCount() > m_maxMsgCacheSize) { + // LOG_INFO("retry pullrequest for:%s after 1s, as cachMsgSize:%d is larger + // than:%d", (request->m_messageQueue).toString().c_str(), + // request->getCacheMsgCount(), m_maxMsgCacheSize); + boost::asio::deadline_timer *t = + new boost::asio::deadline_timer(m_async_ioService, boost::posix_time::milliseconds(1 * 1000)); + t->async_wait(boost::bind(&DefaultMQPushConsumer::triggerNextPullRequest, this, t, request)); + return; } - void DefaultMQPushConsumer::setMaxCacheMsgSizePerQueue(int maxCacheSize) { - if (maxCacheSize > 0 && maxCacheSize < 65535) { - LOG_INFO("set maxCacheSize to:%d for consumer:%s", maxCacheSize, - getGroupName().c_str()); - m_maxMsgCacheSize = maxCacheSize; + bool commitOffsetEnable = false; + int64 commitOffsetValue = 0; + if (CLUSTERING == getMessageModel()) { + commitOffsetValue = m_pOffsetStore->readOffset(messageQueue, READ_FROM_MEMORY, getSessionCredentials()); + if (commitOffsetValue > 0) { + commitOffsetEnable = true; } } - int DefaultMQPushConsumer::getMaxCacheMsgSizePerQueue() const { - return m_maxMsgCacheSize; - } - - ConsumerRunningInfo *DefaultMQPushConsumer::getConsumerRunningInfo() { - ConsumerRunningInfo *info = new ConsumerRunningInfo(); - if (info) { - if (m_consumerService->getConsumeMsgSerivceListenerType() == - messageListenerOrderly) - info->setProperty(ConsumerRunningInfo::PROP_CONSUME_ORDERLY, "true"); - else - info->setProperty(ConsumerRunningInfo::PROP_CONSUME_ORDERLY, "flase"); - info->setProperty(ConsumerRunningInfo::PROP_THREADPOOL_CORE_SIZE, - UtilAll::to_string(m_consumeThreadCount)); - info->setProperty(ConsumerRunningInfo::PROP_CONSUMER_START_TIMESTAMP, - UtilAll::to_string(m_startTime)); - - vector result; - getSubscriptions(result); - info->setSubscriptionSet(result); - - map requestTable = - m_pRebalance->getPullRequestTable(); - map::iterator it = requestTable.begin(); - - for (; it != requestTable.end(); ++it) { - if (!it->second->isDroped()) { - map queueTable; - MessageQueue queue((it->first).getTopic(), (it->first).getBrokerName(), - (it->first).getQueueId()); - ProcessQueueInfo processQueue; - processQueue.cachedMsgMinOffset = it->second->getCacheMinOffset(); - processQueue.cachedMsgMaxOffset = it->second->getCacheMaxOffset(); - processQueue.cachedMsgCount = it->second->getCacheMsgCount(); - processQueue.setCommitOffset(m_pOffsetStore->readOffset( - it->first, MEMORY_FIRST_THEN_STORE, getSessionCredentials())); - processQueue.setDroped(it->second->isDroped()); - processQueue.setLocked(it->second->isLocked()); - processQueue.lastLockTimestamp = it->second->getLastLockTimestamp(); - processQueue.lastPullTimestamp = it->second->getLastPullTimestamp(); - processQueue.lastConsumeTimestamp = - it->second->getLastConsumeTimestamp(); - info->setMqTable(queue, processQueue); - } + string subExpression; + SubscriptionData *pSdata = (m_pRebalance->getSubscriptionData(messageQueue.getTopic())); + if (pSdata == NULL) { + producePullMsgTask(request); + return; + } + subExpression = pSdata->getSubString(); + + int sysFlag = PullSysFlag::buildSysFlag(commitOffsetEnable, // commitOffset + true, // suspend + !subExpression.empty(), // subscription + false); // class filter + + AsyncArg arg; + arg.mq = messageQueue; + arg.subData = *pSdata; + arg.pPullWrapper = m_pPullAPIWrapper; + arg.pPullRequest = request; + try { + request->setLastPullTimestamp(UtilAll::currentTimeMillis()); + m_pPullAPIWrapper->pullKernelImpl(messageQueue, // 1 + subExpression, // 2 + pSdata->getSubVersion(), // 3 + request->getNextOffset(), // 4 + 32, // 5 + sysFlag, // 6 + commitOffsetValue, // 7 + 1000 * 15, // 8 + m_asyncPullTimeout, // 9 + ComMode_ASYNC, // 10 + getAsyncPullCallBack(request, messageQueue), // 11 + getSessionCredentials(), // 12 + &arg); // 13 + } + catch (MQException &e) { + LOG_ERROR(e.what()); + producePullMsgTask(request); + } +} + +void DefaultMQPushConsumer::setAsyncPull(bool asyncFlag) { + if (asyncFlag) { + LOG_INFO("set pushConsumer:%s to async default pull mode", getGroupName().c_str()); + } else { + LOG_INFO("set pushConsumer:%s to sync pull mode", getGroupName().c_str()); + } + m_asyncPull = asyncFlag; +} + +void DefaultMQPushConsumer::setConsumeThreadCount(int threadCount) { + if (threadCount > 0) { + m_consumeThreadCount = threadCount; + } else { + LOG_ERROR("setConsumeThreadCount with invalid value"); + } +} + +int DefaultMQPushConsumer::getConsumeThreadCount() const { return m_consumeThreadCount; } + +void DefaultMQPushConsumer::setPullMsgThreadPoolCount(int threadCount) { m_pullMsgThreadPoolNum = threadCount; } + +int DefaultMQPushConsumer::getPullMsgThreadPoolCount() const { return m_pullMsgThreadPoolNum; } + +int DefaultMQPushConsumer::getConsumeMessageBatchMaxSize() const { return m_consumeMessageBatchMaxSize; } + +void DefaultMQPushConsumer::setConsumeMessageBatchMaxSize(int consumeMessageBatchMaxSize) { + if (consumeMessageBatchMaxSize >= 1) m_consumeMessageBatchMaxSize = consumeMessageBatchMaxSize; +} + +void DefaultMQPushConsumer::setMaxCacheMsgSizePerQueue(int maxCacheSize) { + if (maxCacheSize > 0 && maxCacheSize < 65535) { + LOG_INFO("set maxCacheSize to:%d for consumer:%s", maxCacheSize, getGroupName().c_str()); + m_maxMsgCacheSize = maxCacheSize; + } +} + +int DefaultMQPushConsumer::getMaxCacheMsgSizePerQueue() const { return m_maxMsgCacheSize; } + +ConsumerRunningInfo *DefaultMQPushConsumer::getConsumerRunningInfo() { + ConsumerRunningInfo *info = new ConsumerRunningInfo(); + if (info) { + if (m_consumerService->getConsumeMsgSerivceListenerType() == messageListenerOrderly) + info->setProperty(ConsumerRunningInfo::PROP_CONSUME_ORDERLY, "true"); + else + info->setProperty(ConsumerRunningInfo::PROP_CONSUME_ORDERLY, "flase"); + info->setProperty(ConsumerRunningInfo::PROP_THREADPOOL_CORE_SIZE, UtilAll::to_string(m_consumeThreadCount)); + info->setProperty(ConsumerRunningInfo::PROP_CONSUMER_START_TIMESTAMP, UtilAll::to_string(m_startTime)); + + vector result; + getSubscriptions(result); + info->setSubscriptionSet(result); + + map requestTable = m_pRebalance->getPullRequestTable(); + map::iterator it = requestTable.begin(); + + for (; it != requestTable.end(); ++it) { + if (!it->second->isDroped()) { + map queueTable; + MessageQueue queue((it->first).getTopic(), (it->first).getBrokerName(), (it->first).getQueueId()); + ProcessQueueInfo processQueue; + processQueue.cachedMsgMinOffset = it->second->getCacheMinOffset(); + processQueue.cachedMsgMaxOffset = it->second->getCacheMaxOffset(); + processQueue.cachedMsgCount = it->second->getCacheMsgCount(); + processQueue.setCommitOffset( + m_pOffsetStore->readOffset(it->first, MEMORY_FIRST_THEN_STORE, getSessionCredentials())); + processQueue.setDroped(it->second->isDroped()); + processQueue.setLocked(it->second->isLocked()); + processQueue.lastLockTimestamp = it->second->getLastLockTimestamp(); + processQueue.lastPullTimestamp = it->second->getLastPullTimestamp(); + processQueue.lastConsumeTimestamp = it->second->getLastConsumeTimestamp(); + info->setMqTable(queue, processQueue); } - - return info; } - return NULL; + + return info; } + return NULL; +} //selectConsumer(groupName); - if (pConsumer) { - LOG_INFO("new LocalFileOffsetStore"); - string directoryName = - UtilAll::getLocalAddress() + "@" + pConsumer->getInstanceName(); - m_storePath = ".rocketmq_offsets/" + directoryName + "/" + groupName; - string homeDir(UtilAll::getHomeDirectory()); - m_storeFile = homeDir + "/" + m_storePath + "/offsets.Json"; - - string storePath(homeDir); - storePath.append("/").append(m_storePath); - boost::filesystem::path dir(storePath); - boost::system::error_code ec; - if (!boost::filesystem::exists(dir, ec)) { - if (!boost::filesystem::create_directories(dir, ec)) { - LOG_ERROR("create offset store dir:%s error", storePath.c_str()); - string errorMsg("create offset store dir fail: "); - errorMsg.append(storePath); - THROW_MQEXCEPTION(MQClientException, errorMsg, -1); - } + MQConsumer* pConsumer = pfactory->selectConsumer(groupName); + if (pConsumer) { + LOG_INFO("new LocalFileOffsetStore"); + string directoryName = UtilAll::getLocalAddress() + "@" + pConsumer->getInstanceName(); + m_storePath = ".rocketmq_offsets/" + directoryName + "/" + groupName; + string homeDir(UtilAll::getHomeDirectory()); + m_storeFile = homeDir + "/" + m_storePath + "/offsets.Json"; + + string storePath(homeDir); + storePath.append("/").append(m_storePath); + boost::filesystem::path dir(storePath); + boost::system::error_code ec; + if (!boost::filesystem::exists(dir, ec)) { + if (!boost::filesystem::create_directories(dir, ec)) { + LOG_ERROR("create offset store dir:%s error", storePath.c_str()); + string errorMsg("create offset store dir fail: "); + errorMsg.append(storePath); + THROW_MQEXCEPTION(MQClientException, errorMsg, -1); + } + } } - } } LocalFileOffsetStore::~LocalFileOffsetStore() {} void LocalFileOffsetStore::load() { - std::ifstream ifs(m_storeFile.c_str(), std::ios::in); - if (ifs.good()) { - if (ifs.is_open()) { - if (ifs.peek() != std::ifstream::traits_type::eof()) { - map m_offsetTable_tmp; - boost::system::error_code e; - try { - boost::archive::text_iarchive ia(ifs); - ia >> m_offsetTable_tmp; - } catch (...) { - LOG_ERROR( - "load offset store file failed, please check whether file: %s is " - "cleared by operator, if so, delete this offsets.Json file and " - "then restart consumer", - m_storeFile.c_str()); - ifs.close(); - string errorMsg("load offset store file: "); - errorMsg.append(m_storeFile) - .append( - " failed, please check whether offsets.Json is cleared by " - "operator, if so, delete this offsets.Json file and then " - "restart consumer"); - THROW_MQEXCEPTION(MQClientException, errorMsg, -1); - } - ifs.close(); - - for (map::iterator it = m_offsetTable_tmp.begin(); - it != m_offsetTable_tmp.end(); ++it) { - // LOG_INFO("it->first:%s, it->second:%lld", it->first.c_str(), - // it->second); - Json::Reader reader; - Json::Value object; - reader.parse(it->first.c_str(), object); - MQMessageQueue mq(object["topic"].asString(), - object["brokerName"].asString(), - object["queueId"].asInt()); - updateOffset(mq, it->second); + std::ifstream ifs(m_storeFile.c_str(), std::ios::in); + if (ifs.good()) { + if (ifs.is_open()) { + if (ifs.peek() != std::ifstream::traits_type::eof()) { + map m_offsetTable_tmp; + boost::system::error_code e; + try { + boost::archive::text_iarchive ia(ifs); + ia >> m_offsetTable_tmp; + } + catch (...) { + LOG_ERROR( + "load offset store file failed, please check whether file: %s is " + "cleared by operator, if so, delete this offsets.Json file and " + "then restart consumer", + m_storeFile.c_str()); + ifs.close(); + string errorMsg("load offset store file: "); + errorMsg.append(m_storeFile).append( + " failed, please check whether offsets.Json is cleared by " + "operator, if so, delete this offsets.Json file and then " + "restart consumer"); + THROW_MQEXCEPTION(MQClientException, errorMsg, -1); + } + ifs.close(); + + for (map::iterator it = m_offsetTable_tmp.begin(); it != m_offsetTable_tmp.end(); ++it) { + // LOG_INFO("it->first:%s, it->second:%lld", it->first.c_str(), + // it->second); + Json::Reader reader; + Json::Value object; + reader.parse(it->first.c_str(), object); + MQMessageQueue mq(object["topic"].asString(), object["brokerName"].asString(), + object["queueId"].asInt()); + updateOffset(mq, it->second); + } + m_offsetTable_tmp.clear(); + } else { + LOG_ERROR( + "open offset store file failed, please check whether file: %s is " + "cleared by operator, if so, delete this offsets.Json file and " + "then restart consumer", + m_storeFile.c_str()); + THROW_MQEXCEPTION(MQClientException, + "open offset store file failed, please check whether " + "offsets.Json is cleared by operator, if so, delete " + "this offsets.Json file and then restart consumer", + -1); + } + } else { + LOG_ERROR( + "open offset store file failed, please check whether file:%s is " + "deleted by operator and then restart consumer", + m_storeFile.c_str()); + THROW_MQEXCEPTION(MQClientException, + "open offset store file failed, please check " + "directory:%s is deleted by operator or offset.Json " + "file is cleared by operator, and then restart " + "consumer", + -1); } - m_offsetTable_tmp.clear(); - } else { - LOG_ERROR( - "open offset store file failed, please check whether file: %s is " - "cleared by operator, if so, delete this offsets.Json file and " - "then restart consumer", - m_storeFile.c_str()); - THROW_MQEXCEPTION(MQClientException, - "open offset store file failed, please check whether " - "offsets.Json is cleared by operator, if so, delete " - "this offsets.Json file and then restart consumer", - -1); - } } else { - LOG_ERROR( - "open offset store file failed, please check whether file:%s is " - "deleted by operator and then restart consumer", - m_storeFile.c_str()); - THROW_MQEXCEPTION(MQClientException, - "open offset store file failed, please check " - "directory:%s is deleted by operator or offset.Json " - "file is cleared by operator, and then restart " - "consumer", - -1); + LOG_WARN( + "offsets.Json file not exist, maybe this is the first time " + "consumation"); } - } else { - LOG_WARN( - "offsets.Json file not exist, maybe this is the first time " - "consumation"); - } } -void LocalFileOffsetStore::updateOffset(const MQMessageQueue& mq, - int64 offset) { - boost::lock_guard lock(m_lock); - m_offsetTable[mq] = offset; +void LocalFileOffsetStore::updateOffset(const MQMessageQueue& mq, int64 offset) { + boost::lock_guard lock(m_lock); + m_offsetTable[mq] = offset; } -int64 LocalFileOffsetStore::readOffset( - const MQMessageQueue& mq, ReadOffsetType type, - const SessionCredentials& session_credentials) { - switch (type) { - case MEMORY_FIRST_THEN_STORE: - case READ_FROM_MEMORY: { - boost::lock_guard lock(m_lock); - MQ2OFFSET::iterator it = m_offsetTable.find(mq); - if (it != m_offsetTable.end()) { - return it->second; - } else if (READ_FROM_MEMORY == type) { - return -1; - } - } - case READ_FROM_STORE: { - try { - load(); - } catch (MQException& e) { - LOG_ERROR("catch exception when load local file"); - return -1; - } - boost::lock_guard lock(m_lock); - MQ2OFFSET::iterator it = m_offsetTable.find(mq); - if (it != m_offsetTable.end()) { - return it->second; - } +int64 LocalFileOffsetStore::readOffset(const MQMessageQueue& mq, ReadOffsetType type, + const SessionCredentials& session_credentials) { + switch (type) { + case MEMORY_FIRST_THEN_STORE: + case READ_FROM_MEMORY: { + boost::lock_guard lock(m_lock); + MQ2OFFSET::iterator it = m_offsetTable.find(mq); + if (it != m_offsetTable.end()) { + return it->second; + } else if (READ_FROM_MEMORY == type) { + return -1; + } + } + case READ_FROM_STORE: { + try { + load(); + } + catch (MQException& e) { + LOG_ERROR("catch exception when load local file"); + return -1; + } + boost::lock_guard lock(m_lock); + MQ2OFFSET::iterator it = m_offsetTable.find(mq); + if (it != m_offsetTable.end()) { + return it->second; + } + } + default: + break; } - default: - break; - } - LOG_ERROR( - "can not readOffset from offsetStore.json, maybe first time consumation"); - return -1; + LOG_ERROR("can not readOffset from offsetStore.json, maybe first time consumation"); + return -1; } -void LocalFileOffsetStore::persist( - const MQMessageQueue& mq, const SessionCredentials& session_credentials) {} +void LocalFileOffsetStore::persist(const MQMessageQueue& mq, const SessionCredentials& session_credentials) {} void LocalFileOffsetStore::persistAll(const std::vector& mqs) { - boost::lock_guard lock(m_lock); - - map m_offsetTable_tmp; - vector::const_iterator it = mqs.begin(); - for (; it != mqs.end(); ++it) { - MessageQueue mq_tmp((*it).getTopic(), (*it).getBrokerName(), - (*it).getQueueId()); - string mqKey = mq_tmp.toJson().toStyledString(); - m_offsetTable_tmp[mqKey] = m_offsetTable[*it]; - } - - std::ofstream s; - string storefile_bak(m_storeFile); - storefile_bak.append(".bak"); - s.open(storefile_bak.c_str(), std::ios::out); - if (s.is_open()) { - try { - boost::archive::text_oarchive oa(s); - // Boost is nervous that archiving non-const class instances which might - // cause a problem with object tracking if different tracked objects use - // the same address. - oa << const_cast&>(m_offsetTable_tmp); - } catch (...) { - LOG_ERROR("persist offset store file:%s failed", m_storeFile.c_str()); - s.close(); - THROW_MQEXCEPTION(MQClientException, - "persistAll:open offset store file failed", -1); + boost::lock_guard lock(m_lock); + + map m_offsetTable_tmp; + vector::const_iterator it = mqs.begin(); + for (; it != mqs.end(); ++it) { + MessageQueue mq_tmp((*it).getTopic(), (*it).getBrokerName(), (*it).getQueueId()); + string mqKey = mq_tmp.toJson().toStyledString(); + m_offsetTable_tmp[mqKey] = m_offsetTable[*it]; + } + + std::ofstream s; + string storefile_bak(m_storeFile); + storefile_bak.append(".bak"); + s.open(storefile_bak.c_str(), std::ios::out); + if (s.is_open()) { + try { + boost::archive::text_oarchive oa(s); + // Boost is nervous that archiving non-const class instances which might + // cause a problem with object tracking if different tracked objects use + // the same address. + oa << const_cast&>(m_offsetTable_tmp); + } + catch (...) { + LOG_ERROR("persist offset store file:%s failed", m_storeFile.c_str()); + s.close(); + THROW_MQEXCEPTION(MQClientException, "persistAll:open offset store file failed", -1); + } + s.close(); + if (!UtilAll::ReplaceFile(storefile_bak, m_storeFile)) + LOG_ERROR("could not rename bak file:%s", strerror(errno)); + m_offsetTable_tmp.clear(); + } else { + LOG_ERROR("open offset store file:%s failed", m_storeFile.c_str()); + m_offsetTable_tmp.clear(); + THROW_MQEXCEPTION(MQClientException, "persistAll:open offset store file failed", -1); } - s.close(); - if (!UtilAll::ReplaceFile(storefile_bak, m_storeFile)) - LOG_ERROR("could not rename bak file:%s", strerror(errno)); - m_offsetTable_tmp.clear(); - } else { - LOG_ERROR("open offset store file:%s failed", m_storeFile.c_str()); - m_offsetTable_tmp.clear(); - THROW_MQEXCEPTION(MQClientException, - "persistAll:open offset store file failed", -1); - } } void LocalFileOffsetStore::removeOffset(const MQMessageQueue& mq) {} // lock(m_lock); - m_offsetTable[mq] = offset; +void RemoteBrokerOffsetStore::updateOffset(const MQMessageQueue& mq, int64 offset) { + boost::lock_guard lock(m_lock); + m_offsetTable[mq] = offset; } -int64 RemoteBrokerOffsetStore::readOffset( - const MQMessageQueue& mq, ReadOffsetType type, - const SessionCredentials& session_credentials) { - switch (type) { - case MEMORY_FIRST_THEN_STORE: - case READ_FROM_MEMORY: { - boost::lock_guard lock(m_lock); - - MQ2OFFSET::iterator it = m_offsetTable.find(mq); - if (it != m_offsetTable.end()) { - return it->second; - } else if (READ_FROM_MEMORY == type) { - return -1; - } - } - case READ_FROM_STORE: { - try { - int64 brokerOffset = - fetchConsumeOffsetFromBroker(mq, session_credentials); - // lock(m_lock); + + MQ2OFFSET::iterator it = m_offsetTable.find(mq); + if (it != m_offsetTable.end()) { + return it->second; + } else if (READ_FROM_MEMORY == type) { + return -1; + } + } + case READ_FROM_STORE: { + try { + int64 brokerOffset = fetchConsumeOffsetFromBroker(mq, session_credentials); + // lock(m_lock); - offsetTable = m_offsetTable; - } - - MQ2OFFSET::iterator it = offsetTable.find(mq); - if (it != offsetTable.end()) { - try { - updateConsumeOffsetToBroker(mq, it->second, session_credentials); - } catch (MQException& e) { - LOG_ERROR("updateConsumeOffsetToBroker error"); +void RemoteBrokerOffsetStore::persist(const MQMessageQueue& mq, const SessionCredentials& session_credentials) { + MQ2OFFSET offsetTable; + { + boost::lock_guard lock(m_lock); + offsetTable = m_offsetTable; + } + + MQ2OFFSET::iterator it = offsetTable.find(mq); + if (it != offsetTable.end()) { + try { + updateConsumeOffsetToBroker(mq, it->second, session_credentials); + } + catch (MQException& e) { + LOG_ERROR("updateConsumeOffsetToBroker error"); + } } - } } -void RemoteBrokerOffsetStore::persistAll( - const std::vector& mq) {} +void RemoteBrokerOffsetStore::persistAll(const std::vector& mq) {} void RemoteBrokerOffsetStore::removeOffset(const MQMessageQueue& mq) { - boost::lock_guard lock(m_lock); - if (m_offsetTable.find(mq) != m_offsetTable.end()) m_offsetTable.erase(mq); + boost::lock_guard lock(m_lock); + if (m_offsetTable.find(mq) != m_offsetTable.end()) m_offsetTable.erase(mq); } -void RemoteBrokerOffsetStore::updateConsumeOffsetToBroker( - const MQMessageQueue& mq, int64 offset, - const SessionCredentials& session_credentials) { - unique_ptr pFindBrokerResult( - m_pClientFactory->findBrokerAddressInAdmin(mq.getBrokerName())); - - if (pFindBrokerResult == NULL) { - m_pClientFactory->updateTopicRouteInfoFromNameServer(mq.getTopic(), - session_credentials); - pFindBrokerResult.reset( - m_pClientFactory->findBrokerAddressInAdmin(mq.getBrokerName())); - } - - if (pFindBrokerResult != NULL) { - UpdateConsumerOffsetRequestHeader* pRequestHeader = - new UpdateConsumerOffsetRequestHeader(); - pRequestHeader->topic = mq.getTopic(); - pRequestHeader->consumerGroup = m_groupName; - pRequestHeader->queueId = mq.getQueueId(); - pRequestHeader->commitOffset = offset; - - try { - LOG_INFO( - "oneway updateConsumeOffsetToBroker of mq:%s, its offset is:%lld", - mq.toString().c_str(), offset); - return m_pClientFactory->getMQClientAPIImpl()->updateConsumerOffsetOneway( - pFindBrokerResult->brokerAddr, pRequestHeader, 1000 * 5, - session_credentials); - } catch (MQException& e) { - LOG_ERROR(e.what()); +void RemoteBrokerOffsetStore::updateConsumeOffsetToBroker(const MQMessageQueue& mq, int64 offset, + const SessionCredentials& session_credentials) { + unique_ptr pFindBrokerResult(m_pClientFactory->findBrokerAddressInAdmin(mq.getBrokerName())); + + if (pFindBrokerResult == NULL) { + m_pClientFactory->updateTopicRouteInfoFromNameServer(mq.getTopic(), session_credentials); + pFindBrokerResult.reset(m_pClientFactory->findBrokerAddressInAdmin(mq.getBrokerName())); } - } - LOG_WARN("The broker not exist"); + + if (pFindBrokerResult != NULL) { + UpdateConsumerOffsetRequestHeader* pRequestHeader = new UpdateConsumerOffsetRequestHeader(); + pRequestHeader->topic = mq.getTopic(); + pRequestHeader->consumerGroup = m_groupName; + pRequestHeader->queueId = mq.getQueueId(); + pRequestHeader->commitOffset = offset; + + try { + LOG_INFO("oneway updateConsumeOffsetToBroker of mq:%s, its offset is:%lld", mq.toString().c_str(), offset); + return m_pClientFactory->getMQClientAPIImpl()->updateConsumerOffsetOneway( + pFindBrokerResult->brokerAddr, pRequestHeader, 1000 * 5, session_credentials); + } + catch (MQException& e) { + LOG_ERROR(e.what()); + } + } + LOG_WARN("The broker not exist"); } -int64 RemoteBrokerOffsetStore::fetchConsumeOffsetFromBroker( - const MQMessageQueue& mq, const SessionCredentials& session_credentials) { - unique_ptr pFindBrokerResult( - m_pClientFactory->findBrokerAddressInAdmin(mq.getBrokerName())); - - if (pFindBrokerResult == NULL) { - m_pClientFactory->updateTopicRouteInfoFromNameServer(mq.getTopic(), - session_credentials); - pFindBrokerResult.reset( - m_pClientFactory->findBrokerAddressInAdmin(mq.getBrokerName())); - } - - if (pFindBrokerResult != NULL) { - QueryConsumerOffsetRequestHeader* pRequestHeader = - new QueryConsumerOffsetRequestHeader(); - pRequestHeader->topic = mq.getTopic(); - pRequestHeader->consumerGroup = m_groupName; - pRequestHeader->queueId = mq.getQueueId(); - - return m_pClientFactory->getMQClientAPIImpl()->queryConsumerOffset( - pFindBrokerResult->brokerAddr, pRequestHeader, 1000 * 5, - session_credentials); - } else { - LOG_ERROR("The broker not exist when fetchConsumeOffsetFromBroker"); - THROW_MQEXCEPTION(MQClientException, "The broker not exist", -1); - } +int64 RemoteBrokerOffsetStore::fetchConsumeOffsetFromBroker(const MQMessageQueue& mq, + const SessionCredentials& session_credentials) { + unique_ptr pFindBrokerResult(m_pClientFactory->findBrokerAddressInAdmin(mq.getBrokerName())); + + if (pFindBrokerResult == NULL) { + m_pClientFactory->updateTopicRouteInfoFromNameServer(mq.getTopic(), session_credentials); + pFindBrokerResult.reset(m_pClientFactory->findBrokerAddressInAdmin(mq.getBrokerName())); + } + + if (pFindBrokerResult != NULL) { + QueryConsumerOffsetRequestHeader* pRequestHeader = new QueryConsumerOffsetRequestHeader(); + pRequestHeader->topic = mq.getTopic(); + pRequestHeader->consumerGroup = m_groupName; + pRequestHeader->queueId = mq.getQueueId(); + + return m_pClientFactory->getMQClientAPIImpl()->queryConsumerOffset( + pFindBrokerResult->brokerAddr, pRequestHeader, 1000 * 5, session_credentials); + } else { + LOG_ERROR("The broker not exist when fetchConsumeOffsetFromBroker"); + THROW_MQEXCEPTION(MQClientException, "The broker not exist", -1); + } } //& mq) = 0; - virtual void removeOffset(const MQMessageQueue& mq) = 0; + virtual void load() = 0; + virtual void updateOffset(const MQMessageQueue& mq, int64 offset) = 0; + virtual int64 readOffset(const MQMessageQueue& mq, ReadOffsetType type, + const SessionCredentials& session_credentials) = 0; + virtual void persist(const MQMessageQueue& mq, const SessionCredentials& session_credentials) = 0; + virtual void persistAll(const std::vector& mq) = 0; + virtual void removeOffset(const MQMessageQueue& mq) = 0; - protected: - std::string m_groupName; - typedef std::map MQ2OFFSET; - MQ2OFFSET m_offsetTable; - MQClientFactory* m_pClientFactory; - boost::mutex m_lock; +protected: + std::string m_groupName; + typedef std::map MQ2OFFSET; + MQ2OFFSET m_offsetTable; + MQClientFactory* m_pClientFactory; + boost::mutex m_lock; }; //& mq); - virtual void removeOffset(const MQMessageQueue& mq); + virtual void load(); + virtual void updateOffset(const MQMessageQueue& mq, int64 offset); + virtual int64 readOffset(const MQMessageQueue& mq, ReadOffsetType type, + const SessionCredentials& session_credentials); + virtual void persist(const MQMessageQueue& mq, const SessionCredentials& session_credentials); + virtual void persistAll(const std::vector& mq); + virtual void removeOffset(const MQMessageQueue& mq); - private: - std::string m_storePath; - std::string m_storeFile; +private: + std::string m_storePath; + std::string m_storeFile; }; //& mq); - virtual void removeOffset(const MQMessageQueue& mq); + virtual void load(); + virtual void updateOffset(const MQMessageQueue& mq, int64 offset); + virtual int64 readOffset(const MQMessageQueue& mq, ReadOffsetType type, + const SessionCredentials& session_credentials); + virtual void persist(const MQMessageQueue& mq, const SessionCredentials& session_credentials); + virtual void persistAll(const std::vector& mq); + virtual void removeOffset(const MQMessageQueue& mq); - private: - void updateConsumeOffsetToBroker( - const MQMessageQueue& mq, int64 offset, - const SessionCredentials& session_credentials); - int64 fetchConsumeOffsetFromBroker( - const MQMessageQueue& mq, const SessionCredentials& session_credentials); +private: + void updateConsumeOffsetToBroker(const MQMessageQueue& mq, int64 offset, + const SessionCredentials& session_credentials); + int64 fetchConsumeOffsetFromBroker(const MQMessageQueue& mq, const SessionCredentials& session_credentials); }; // lock(m_lock); - m_pullFromWhichNodeTable[mq] = brokerId; +void PullAPIWrapper::updatePullFromWhichNode(const MQMessageQueue& mq, int brokerId) { + boost::lock_guard lock(m_lock); + m_pullFromWhichNodeTable[mq] = brokerId; } int PullAPIWrapper::recalculatePullFromWhichNode(const MQMessageQueue& mq) { - boost::lock_guard lock(m_lock); - if (m_pullFromWhichNodeTable.find(mq) != m_pullFromWhichNodeTable.end()) { - return m_pullFromWhichNodeTable[mq]; - } - return MASTER_ID; + boost::lock_guard lock(m_lock); + if (m_pullFromWhichNodeTable.find(mq) != m_pullFromWhichNodeTable.end()) { + return m_pullFromWhichNodeTable[mq]; + } + return MASTER_ID; } -PullResult PullAPIWrapper::processPullResult( - const MQMessageQueue& mq, PullResult* pullResult, - SubscriptionData* subscriptionData) { - PullResultExt* pResultExt = static_cast(pullResult); - if (pResultExt == NULL) { - string errMsg("The pullResult NULL of"); - errMsg.append(mq.toString()); - THROW_MQEXCEPTION(MQClientException, errMsg, -1); - } +PullResult PullAPIWrapper::processPullResult(const MQMessageQueue& mq, PullResult* pullResult, + SubscriptionData* subscriptionData) { + PullResultExt* pResultExt = static_cast(pullResult); + if (pResultExt == NULL) { + string errMsg("The pullResult NULL of"); + errMsg.append(mq.toString()); + THROW_MQEXCEPTION(MQClientException, errMsg, -1); + } - //suggestWhichBrokerId); + //suggestWhichBrokerId); - vector msgFilterList; - if (pResultExt->pullStatus == FOUND) { - // msgAllList; - MQDecoder::decodes(&pResultExt->msgMemBlock, msgAllList); + vector msgFilterList; + if (pResultExt->pullStatus == FOUND) { + // msgAllList; + MQDecoder::decodes(&pResultExt->msgMemBlock, msgAllList); - //getTagsSet().empty()) { - msgFilterList.reserve(msgAllList.size()); - vector::iterator it = msgAllList.begin(); - for (; it != msgAllList.end(); ++it) { - string msgTag = (*it).getTags(); - if (subscriptionData->containTag(msgTag)) { - msgFilterList.push_back(*it); + //getTagsSet().empty()) { + msgFilterList.reserve(msgAllList.size()); + vector::iterator it = msgAllList.begin(); + for (; it != msgAllList.end(); ++it) { + string msgTag = (*it).getTags(); + if (subscriptionData->containTag(msgTag)) { + msgFilterList.push_back(*it); + } + } + } else { + msgFilterList.swap(msgAllList); } - } - } else - { - msgFilterList.swap(msgAllList); } - } - return PullResult(pResultExt->pullStatus, pResultExt->nextBeginOffset, - pResultExt->minOffset, pResultExt->maxOffset, - msgFilterList); + return PullResult(pResultExt->pullStatus, pResultExt->nextBeginOffset, pResultExt->minOffset, pResultExt->maxOffset, + msgFilterList); } -PullResult* PullAPIWrapper::pullKernelImpl( - const MQMessageQueue& mq, // 1 - string subExpression, // 2 - int64 subVersion, // 3 - int64 offset, // 4 - int maxNums, // 5 - int sysFlag, // 6 - int64 commitOffset, // 7 - int brokerSuspendMaxTimeMillis, // 8 - int timeoutMillis, // 9 - int communicationMode, // 10 - PullCallback* pullCallback, const SessionCredentials& session_credentials, - void* pArg /*= NULL*/) { - unique_ptr pFindBrokerResult( - m_MQClientFactory->findBrokerAddressInSubscribe( - mq.getBrokerName(), recalculatePullFromWhichNode(mq), false)); - //updateTopicRouteInfoFromNameServer(mq.getTopic(), - session_credentials); - pFindBrokerResult.reset(m_MQClientFactory->findBrokerAddressInSubscribe( - mq.getBrokerName(), recalculatePullFromWhichNode(mq), false)); - } +PullResult* PullAPIWrapper::pullKernelImpl(const MQMessageQueue& mq, // 1 + string subExpression, // 2 + int64 subVersion, // 3 + int64 offset, // 4 + int maxNums, // 5 + int sysFlag, // 6 + int64 commitOffset, // 7 + int brokerSuspendMaxTimeMillis, // 8 + int timeoutMillis, // 9 + int communicationMode, // 10 + PullCallback* pullCallback, const SessionCredentials& session_credentials, + void* pArg /*= NULL*/) { + unique_ptr pFindBrokerResult( + m_MQClientFactory->findBrokerAddressInSubscribe(mq.getBrokerName(), recalculatePullFromWhichNode(mq), false)); + //updateTopicRouteInfoFromNameServer(mq.getTopic(), session_credentials); + pFindBrokerResult.reset(m_MQClientFactory->findBrokerAddressInSubscribe( + mq.getBrokerName(), recalculatePullFromWhichNode(mq), false)); + } - if (pFindBrokerResult != NULL) { - int sysFlagInner = sysFlag; + if (pFindBrokerResult != NULL) { + int sysFlagInner = sysFlag; - if (pFindBrokerResult->slave) { - sysFlagInner = PullSysFlag::clearCommitOffsetFlag(sysFlagInner); - } + if (pFindBrokerResult->slave) { + sysFlagInner = PullSysFlag::clearCommitOffsetFlag(sysFlagInner); + } - PullMessageRequestHeader* pRequestHeader = new PullMessageRequestHeader(); - pRequestHeader->consumerGroup = m_consumerGroup; - pRequestHeader->topic = mq.getTopic(); - pRequestHeader->queueId = mq.getQueueId(); - pRequestHeader->queueOffset = offset; - pRequestHeader->maxMsgNums = maxNums; - pRequestHeader->sysFlag = sysFlagInner; - pRequestHeader->commitOffset = commitOffset; - pRequestHeader->suspendTimeoutMillis = brokerSuspendMaxTimeMillis; - pRequestHeader->subscription = subExpression; - pRequestHeader->subVersion = subVersion; + PullMessageRequestHeader* pRequestHeader = new PullMessageRequestHeader(); + pRequestHeader->consumerGroup = m_consumerGroup; + pRequestHeader->topic = mq.getTopic(); + pRequestHeader->queueId = mq.getQueueId(); + pRequestHeader->queueOffset = offset; + pRequestHeader->maxMsgNums = maxNums; + pRequestHeader->sysFlag = sysFlagInner; + pRequestHeader->commitOffset = commitOffset; + pRequestHeader->suspendTimeoutMillis = brokerSuspendMaxTimeMillis; + pRequestHeader->subscription = subExpression; + pRequestHeader->subVersion = subVersion; - return m_MQClientFactory->getMQClientAPIImpl()->pullMessage( - pFindBrokerResult->brokerAddr, pRequestHeader, timeoutMillis, - communicationMode, pullCallback, pArg, session_credentials); - } - THROW_MQEXCEPTION(MQClientException, "The broker not exist", -1); + return m_MQClientFactory->getMQClientAPIImpl()->pullMessage(pFindBrokerResult->brokerAddr, pRequestHeader, + timeoutMillis, communicationMode, pullCallback, + pArg, session_credentials); + } + THROW_MQEXCEPTION(MQClientException, "The broker not exist", -1); } } // m_pullFromWhichNodeTable; +private: + MQClientFactory* m_MQClientFactory; + string m_consumerGroup; + boost::mutex m_lock; + map m_pullFromWhichNodeTable; }; // lock(m_pullRequestLock); - if (this != &other) { - m_groupname = other.m_groupname; - m_nextOffset = other.m_nextOffset; - m_bDroped.store(other.m_bDroped.load()); - m_queueOffsetMax = other.m_queueOffsetMax; - m_messageQueue = other.m_messageQueue; - m_msgTreeMap = other.m_msgTreeMap; - m_msgTreeMapTemp = other.m_msgTreeMapTemp; - m_latestPullRequestOpaque = other.m_latestPullRequestOpaque; - } - return *this; + boost::lock_guard lock(m_pullRequestLock); + if (this != &other) { + m_groupname = other.m_groupname; + m_nextOffset = other.m_nextOffset; + m_bDroped.store(other.m_bDroped.load()); + m_queueOffsetMax = other.m_queueOffsetMax; + m_messageQueue = other.m_messageQueue; + m_msgTreeMap = other.m_msgTreeMap; + m_msgTreeMapTemp = other.m_msgTreeMapTemp; + m_latestPullRequestOpaque = other.m_latestPullRequestOpaque; + } + return *this; } void PullRequest::putMessage(vector& msgs) { - boost::lock_guard lock(m_pullRequestLock); - - vector::iterator it = msgs.begin(); - for (; it != msgs.end(); it++) { - m_msgTreeMap[it->getQueueOffset()] = *it; - m_queueOffsetMax = (std::max)(m_queueOffsetMax, it->getQueueOffset()); - } - LOG_DEBUG("PullRequest: putMessage m_queueOffsetMax:%lld ", m_queueOffsetMax); + boost::lock_guard lock(m_pullRequestLock); + + vector::iterator it = msgs.begin(); + for (; it != msgs.end(); it++) { + m_msgTreeMap[it->getQueueOffset()] = *it; + m_queueOffsetMax = (std::max)(m_queueOffsetMax, it->getQueueOffset()); + } + LOG_DEBUG("PullRequest: putMessage m_queueOffsetMax:%lld ", m_queueOffsetMax); } void PullRequest::getMessage(vector& msgs) { - boost::lock_guard lock(m_pullRequestLock); + boost::lock_guard lock(m_pullRequestLock); - map::iterator it = m_msgTreeMap.begin(); - for (; it != m_msgTreeMap.end(); it++) { - msgs.push_back(it->second); - } + map::iterator it = m_msgTreeMap.begin(); + for (; it != m_msgTreeMap.end(); it++) { + msgs.push_back(it->second); + } } int64 PullRequest::getCacheMinOffset() { - boost::lock_guard lock(m_pullRequestLock); - if (m_msgTreeMap.empty()) { - return 0; - } else { - map::iterator it = m_msgTreeMap.begin(); - MQMessageExt msg = it->second; - return msg.getQueueOffset(); - } + boost::lock_guard lock(m_pullRequestLock); + if (m_msgTreeMap.empty()) { + return 0; + } else { + map::iterator it = m_msgTreeMap.begin(); + MQMessageExt msg = it->second; + return msg.getQueueOffset(); + } } int64 PullRequest::getCacheMaxOffset() { return m_queueOffsetMax; } int PullRequest::getCacheMsgCount() { - boost::lock_guard lock(m_pullRequestLock); - return m_msgTreeMap.size(); + boost::lock_guard lock(m_pullRequestLock); + return m_msgTreeMap.size(); } -void PullRequest::getMessageByQueueOffset(vector& msgs, - int64 minQueueOffset, - int64 maxQueueOffset) { - boost::lock_guard lock(m_pullRequestLock); +void PullRequest::getMessageByQueueOffset(vector& msgs, int64 minQueueOffset, int64 maxQueueOffset) { + boost::lock_guard lock(m_pullRequestLock); - int64 it = minQueueOffset; - for (; it <= maxQueueOffset; it++) { - msgs.push_back(m_msgTreeMap[it]); - } + int64 it = minQueueOffset; + for (; it <= maxQueueOffset; it++) { + msgs.push_back(m_msgTreeMap[it]); + } } int64 PullRequest::removeMessage(vector& msgs) { - boost::lock_guard lock(m_pullRequestLock); - - int64 result = -1; - LOG_DEBUG("m_queueOffsetMax is:%lld", m_queueOffsetMax); - if (!m_msgTreeMap.empty()) { - result = m_queueOffsetMax + 1; - LOG_DEBUG( - " offset result is:%lld, m_queueOffsetMax is:%lld, msgs size:" SIZET_FMT - "", - result, m_queueOffsetMax, msgs.size()); - vector::iterator it = msgs.begin(); - for (; it != msgs.end(); it++) { - LOG_DEBUG("remove these msg from m_msgTreeMap, its offset:%lld", - it->getQueueOffset()); - m_msgTreeMap.erase(it->getQueueOffset()); - } + boost::lock_guard lock(m_pullRequestLock); + int64 result = -1; + LOG_DEBUG("m_queueOffsetMax is:%lld", m_queueOffsetMax); if (!m_msgTreeMap.empty()) { - map::iterator it = m_msgTreeMap.begin(); - result = it->first; - LOG_INFO("cache msg size:" SIZET_FMT - " of pullRequest:%s, return offset result is:%lld", - m_msgTreeMap.size(), m_messageQueue.toString().c_str(), result); + result = m_queueOffsetMax + 1; + LOG_DEBUG(" offset result is:%lld, m_queueOffsetMax is:%lld, msgs size:" SIZET_FMT "", result, m_queueOffsetMax, + msgs.size()); + vector::iterator it = msgs.begin(); + for (; it != msgs.end(); it++) { + LOG_DEBUG("remove these msg from m_msgTreeMap, its offset:%lld", it->getQueueOffset()); + m_msgTreeMap.erase(it->getQueueOffset()); + } + + if (!m_msgTreeMap.empty()) { + map::iterator it = m_msgTreeMap.begin(); + result = it->first; + LOG_INFO("cache msg size:" SIZET_FMT " of pullRequest:%s, return offset result is:%lld", + m_msgTreeMap.size(), m_messageQueue.toString().c_str(), result); + } } - } - return result; + return result; } void PullRequest::clearAllMsgs() { - boost::lock_guard lock(m_pullRequestLock); + boost::lock_guard lock(m_pullRequestLock); - if (isDroped()) { - LOG_DEBUG("clear m_msgTreeMap as PullRequest had been dropped."); - m_msgTreeMap.clear(); - m_msgTreeMapTemp.clear(); - } + if (isDroped()) { + LOG_DEBUG("clear m_msgTreeMap as PullRequest had been dropped."); + m_msgTreeMap.clear(); + m_msgTreeMapTemp.clear(); + } } void PullRequest::updateQueueMaxOffset(int64 queueOffset) { - // following 2 cases which may set queueOffset smaller than m_queueOffsetMax: - // 1. resetOffset cmd - // 2. during rebalance, if configured with CONSUMER_FROM_FIRST_OFFSET, when - // readOffset called by computePullFromWhere was failed, m_nextOffset will be - // setted to 0 - m_queueOffsetMax = queueOffset; + // following 2 cases which may set queueOffset smaller than m_queueOffsetMax: + // 1. resetOffset cmd + // 2. during rebalance, if configured with CONSUMER_FROM_FIRST_OFFSET, when + // readOffset called by computePullFromWhere was failed, m_nextOffset will be + // setted to 0 + m_queueOffsetMax = queueOffset; } void PullRequest::setDroped(bool droped) { - int temp = (droped == true ? 1 : 0); - m_bDroped.store(temp); - /* - m_queueOffsetMax = 0; - m_nextOffset = 0; - //the reason why not clear m_queueOffsetMax and m_nextOffset is due to - ConsumeMsgService and drop mq are concurrent running. - consider following situation: - 1>. ConsumeMsgService running - 2>. dorebalance, drop mq, reset m_nextOffset and m_queueOffsetMax - 3>. ConsumeMsgService calls removeMessages, if no other msgs in - m_msgTreeMap, m_queueOffsetMax(0)+1 will return; - 4>. updateOffset with 1, which is more smaller than correct offset. - */ + int temp = (droped == true ? 1 : 0); + m_bDroped.store(temp); + /* + m_queueOffsetMax = 0; + m_nextOffset = 0; + //the reason why not clear m_queueOffsetMax and m_nextOffset is due to + ConsumeMsgService and drop mq are concurrent running. + consider following situation: + 1>. ConsumeMsgService running + 2>. dorebalance, drop mq, reset m_nextOffset and m_queueOffsetMax + 3>. ConsumeMsgService calls removeMessages, if no other msgs in + m_msgTreeMap, m_queueOffsetMax(0)+1 will return; + 4>. updateOffset with 1, which is more smaller than correct offset. + */ } bool PullRequest::isDroped() const { return m_bDroped.load() == 1; } int64 PullRequest::getNextOffset() { - boost::lock_guard lock(m_pullRequestLock); - return m_nextOffset; + boost::lock_guard lock(m_pullRequestLock); + return m_nextOffset; } void PullRequest::setLocked(bool Locked) { - int temp = (Locked == true ? 1 : 0); - m_bLocked.store(temp); + int temp = (Locked == true ? 1 : 0); + m_bLocked.store(temp); } bool PullRequest::isLocked() const { return m_bLocked.load() == 1; } bool PullRequest::isLockExpired() const { - return (UtilAll::currentTimeMillis() - m_lastLockTimestamp) > - RebalanceLockMaxLiveTime; + return (UtilAll::currentTimeMillis() - m_lastLockTimestamp) > RebalanceLockMaxLiveTime; } -void PullRequest::setLastLockTimestamp(int64 time) { - m_lastLockTimestamp = time; -} +void PullRequest::setLastLockTimestamp(int64 time) { m_lastLockTimestamp = time; } int64 PullRequest::getLastLockTimestamp() const { return m_lastLockTimestamp; } -void PullRequest::setLastPullTimestamp(uint64 time) { - m_lastPullTimestamp = time; -} +void PullRequest::setLastPullTimestamp(uint64 time) { m_lastPullTimestamp = time; } uint64 PullRequest::getLastPullTimestamp() const { return m_lastPullTimestamp; } -void PullRequest::setLastConsumeTimestamp(uint64 time) { - m_lastConsumeTimestamp = time; -} +void PullRequest::setLastConsumeTimestamp(uint64 time) { m_lastConsumeTimestamp = time; } -uint64 PullRequest::getLastConsumeTimestamp() const { - return m_lastConsumeTimestamp; -} +uint64 PullRequest::getLastConsumeTimestamp() const { return m_lastConsumeTimestamp; } void PullRequest::setTryUnlockTimes(int time) { m_lastLockTimestamp = time; } int PullRequest::getTryUnlockTimes() const { return m_lastLockTimestamp; } void PullRequest::setNextOffset(int64 nextoffset) { - boost::lock_guard lock(m_pullRequestLock); - m_nextOffset = nextoffset; + boost::lock_guard lock(m_pullRequestLock); + m_nextOffset = nextoffset; } string PullRequest::getGroupName() const { return m_groupname; } -boost::timed_mutex& PullRequest::getPullRequestCriticalSection() { - return m_consumeLock; -} +boost::timed_mutex& PullRequest::getPullRequestCriticalSection() { return m_consumeLock; } void PullRequest::takeMessages(vector& msgs, int batchSize) { - boost::lock_guard lock(m_pullRequestLock); - for (int i = 0; i != batchSize; i++) { - map::iterator it = m_msgTreeMap.begin(); - if (it != m_msgTreeMap.end()) { - msgs.push_back(it->second); - m_msgTreeMapTemp[it->first] = it->second; - m_msgTreeMap.erase(it); + boost::lock_guard lock(m_pullRequestLock); + for (int i = 0; i != batchSize; i++) { + map::iterator it = m_msgTreeMap.begin(); + if (it != m_msgTreeMap.end()) { + msgs.push_back(it->second); + m_msgTreeMapTemp[it->first] = it->second; + m_msgTreeMap.erase(it); + } } - } } void PullRequest::makeMessageToCosumeAgain(vector& msgs) { - boost::lock_guard lock(m_pullRequestLock); - for (unsigned int it = 0; it != msgs.size(); ++it) { - m_msgTreeMap[msgs[it].getQueueOffset()] = msgs[it]; - m_msgTreeMapTemp.erase(msgs[it].getQueueOffset()); - } + boost::lock_guard lock(m_pullRequestLock); + for (unsigned int it = 0; it != msgs.size(); ++it) { + m_msgTreeMap[msgs[it].getQueueOffset()] = msgs[it]; + m_msgTreeMapTemp.erase(msgs[it].getQueueOffset()); + } } int64 PullRequest::commit() { - boost::lock_guard lock(m_pullRequestLock); - if (!m_msgTreeMapTemp.empty()) { - int64 offset = (--m_msgTreeMapTemp.end())->first; - m_msgTreeMapTemp.clear(); - return offset + 1; - } else { - return -1; - } + boost::lock_guard lock(m_pullRequestLock); + if (!m_msgTreeMapTemp.empty()) { + int64 offset = (--m_msgTreeMapTemp.end())->first; + m_msgTreeMapTemp.clear(); + return offset + 1; + } else { + return -1; + } } void PullRequest::removePullMsgEvent() { m_bPullMsgEventInprogress = false; } bool PullRequest::addPullMsgEvent() { - if (m_bPullMsgEventInprogress == false) { - m_bPullMsgEventInprogress = true; - LOG_INFO("pullRequest with mq :%s set pullMsgEvent", - m_messageQueue.toString().c_str()); - return true; - } - return false; + if (m_bPullMsgEventInprogress == false) { + m_bPullMsgEventInprogress = true; + LOG_INFO("pullRequest with mq :%s set pullMsgEvent", m_messageQueue.toString().c_str()); + return true; + } + return false; } int PullRequest::getLatestPullRequestOpaque() { diff --git a/src/consumer/PullRequest.h b/src/consumer/PullRequest.h index 57ab70b50..ea6c9c2f1 100644 --- a/src/consumer/PullRequest.h +++ b/src/consumer/PullRequest.h @@ -26,73 +26,72 @@ namespace rocketmq { //& msgs); - void getMessage(vector& msgs); - int64 getCacheMinOffset(); - int64 getCacheMaxOffset(); - int getCacheMsgCount(); - void getMessageByQueueOffset(vector& msgs, int64 minQueueOffset, - int64 maxQueueOffset); - int64 removeMessage(vector& msgs); - void clearAllMsgs(); + void putMessage(vector& msgs); + void getMessage(vector& msgs); + int64 getCacheMinOffset(); + int64 getCacheMaxOffset(); + int getCacheMsgCount(); + void getMessageByQueueOffset(vector& msgs, int64 minQueueOffset, int64 maxQueueOffset); + int64 removeMessage(vector& msgs); + void clearAllMsgs(); - PullRequest& operator=(const PullRequest& other); + PullRequest& operator=(const PullRequest& other); - void setDroped(bool droped); - bool isDroped() const; + void setDroped(bool droped); + bool isDroped() const; - int64 getNextOffset(); - void setNextOffset(int64 nextoffset); + int64 getNextOffset(); + void setNextOffset(int64 nextoffset); - string getGroupName() const; + string getGroupName() const; - void updateQueueMaxOffset(int64 queueOffset); + void updateQueueMaxOffset(int64 queueOffset); - void setLocked(bool Locked); - bool isLocked() const; - bool isLockExpired() const; - void setLastLockTimestamp(int64 time); - int64 getLastLockTimestamp() const; - void setLastPullTimestamp(uint64 time); - uint64 getLastPullTimestamp() const; - void setLastConsumeTimestamp(uint64 time); - uint64 getLastConsumeTimestamp() const; - void setTryUnlockTimes(int time); - int getTryUnlockTimes() const; - void takeMessages(vector& msgs, int batchSize); - int64 commit(); - void makeMessageToCosumeAgain(vector& msgs); - boost::timed_mutex& getPullRequestCriticalSection(); - void removePullMsgEvent(); - bool addPullMsgEvent(); - int getLatestPullRequestOpaque(); - void setLatestPullRequestOpaque(int opaque); + void setLocked(bool Locked); + bool isLocked() const; + bool isLockExpired() const; + void setLastLockTimestamp(int64 time); + int64 getLastLockTimestamp() const; + void setLastPullTimestamp(uint64 time); + uint64 getLastPullTimestamp() const; + void setLastConsumeTimestamp(uint64 time); + uint64 getLastConsumeTimestamp() const; + void setTryUnlockTimes(int time); + int getTryUnlockTimes() const; + void takeMessages(vector& msgs, int batchSize); + int64 commit(); + void makeMessageToCosumeAgain(vector& msgs); + boost::timed_mutex& getPullRequestCriticalSection(); + void removePullMsgEvent(); + bool addPullMsgEvent(); + int getLatestPullRequestOpaque(); + void setLatestPullRequestOpaque(int opaque); - public: - MQMessageQueue m_messageQueue; - static const uint64 RebalanceLockInterval; // ms - static const uint64 RebalanceLockMaxLiveTime; // ms +public: + MQMessageQueue m_messageQueue; + static const uint64 RebalanceLockInterval; // ms + static const uint64 RebalanceLockMaxLiveTime; // ms - private: - string m_groupname; - int64 m_nextOffset; - int64 m_queueOffsetMax; - boost::atomic m_bDroped; - boost::atomic m_bLocked; - map m_msgTreeMap; - map m_msgTreeMapTemp; - boost::mutex m_pullRequestLock; - uint64 m_lastLockTimestamp; // ms - //uint64 m_tryUnlockTimes; - uint64 m_lastPullTimestamp; - uint64 m_lastConsumeTimestamp; - int m_latestPullRequestOpaque; - boost::timed_mutex m_consumeLock; - boost::atomic m_bPullMsgEventInprogress; +private: + string m_groupname; + int64 m_nextOffset; + int64 m_queueOffsetMax; + boost::atomic m_bDroped; + boost::atomic m_bLocked; + map m_msgTreeMap; + map m_msgTreeMapTemp; + boost::mutex m_pullRequestLock; + uint64 m_lastLockTimestamp; // ms + // uint64 m_tryUnlockTimes; + uint64 m_lastPullTimestamp; + uint64 m_lastConsumeTimestamp; + int m_latestPullRequestOpaque; + boost::timed_mutex m_consumeLock; + boost::atomic m_bPullMsgEventInprogress; }; //& src) - : pullStatus(pullStatus), - nextBeginOffset(nextBeginOffset), - minOffset(minOffset), - maxOffset(maxOffset) { - msgFoundList.reserve(src.size()); - for (size_t i = 0; i < src.size(); i++) { - msgFoundList.push_back(src[i]); - } + : pullStatus(pullStatus), nextBeginOffset(nextBeginOffset), minOffset(minOffset), maxOffset(maxOffset) { + msgFoundList.reserve(src.size()); + for (size_t i = 0; i < src.size(); i++) { + msgFoundList.push_back(src[i]); + } } PullResult::~PullResult() { msgFoundList.clear(); } diff --git a/src/consumer/PullResultExt.h b/src/consumer/PullResultExt.h old mode 100755 new mode 100644 index 07a8c946f..45f5290d9 --- a/src/consumer/PullResultExt.h +++ b/src/consumer/PullResultExt.h @@ -24,29 +24,28 @@ namespace rocketmq { */ //(messageBinary)) {} + PullResultExt(PullStatus pullStatus, int64 nextBeginOffset, int64 minOffset, int64 maxOffset, + int suggestWhichBrokerId, MemoryBlock &&messageBinary) + : PullResult(pullStatus, nextBeginOffset, minOffset, maxOffset), + suggestWhichBrokerId(suggestWhichBrokerId), + msgMemBlock(std::forward(messageBinary)) {} - PullResultExt(PullStatus pullStatus, int64 nextBeginOffset, int64 minOffset, - int64 maxOffset, int suggestWhichBrokerId) - : PullResult(pullStatus, nextBeginOffset, minOffset, maxOffset), - suggestWhichBrokerId(suggestWhichBrokerId) {} + PullResultExt(PullStatus pullStatus, int64 nextBeginOffset, int64 minOffset, int64 maxOffset, + int suggestWhichBrokerId) + : PullResult(pullStatus, nextBeginOffset, minOffset, maxOffset), suggestWhichBrokerId(suggestWhichBrokerId) {} - virtual ~PullResultExt() {} + virtual ~PullResultExt() {} - public: - int suggestWhichBrokerId; - MemoryBlock msgMemBlock; +public: + int suggestWhichBrokerId; + MemoryBlock msgMemBlock; }; } //::iterator it = m_subscriptionData.begin(); - for (; it != m_subscriptionData.end(); ++it) deleteAndZero(it->second); - m_subscriptionData.clear(); - } - { - MQ2PULLREQ::iterator it = m_requestQueueTable.begin(); - for (; it != m_requestQueueTable.end(); ++it) { - delete it->second; - it->second = NULL; + { + map::iterator it = m_subscriptionData.begin(); + for (; it != m_subscriptionData.end(); ++it) deleteAndZero(it->second); + m_subscriptionData.clear(); + } + { + MQ2PULLREQ::iterator it = m_requestQueueTable.begin(); + for (; it != m_requestQueueTable.end(); ++it) { + delete it->second; + it->second = NULL; + } + m_requestQueueTable.clear(); } - m_requestQueueTable.clear(); - } - m_topicSubscribeInfoTable.clear(); - m_pConsumer = NULL; - m_pClientFactory = NULL; - deleteAndZero(m_pAllocateMQStrategy); + m_topicSubscribeInfoTable.clear(); + m_pConsumer = NULL; + m_pClientFactory = NULL; + deleteAndZero(m_pAllocateMQStrategy); } void Rebalance::doRebalance() { - LOG_DEBUG("start doRebalance"); - try { - map::iterator it = m_subscriptionData.begin(); - for (; it != m_subscriptionData.end(); ++it) { - string topic = (it->first); - LOG_INFO("current topic is:%s", topic.c_str()); - // mqs - vector mqAll; - if (!getTopicSubscribeInfo(topic, mqAll)) { - continue; - } - if (mqAll.empty()) { - if (!UtilAll::startsWith_retry(topic)) - THROW_MQEXCEPTION(MQClientException, "doRebalance the topic is empty", - -1); - } - - //getMessageModel()) { - case BROADCASTING: { - bool changed = updateRequestTableInRebalance(topic, mqAll); - if (changed) { - messageQueueChanged(topic, mqAll, mqAll); - } - break; - } - case CLUSTERING: { - vector cidAll; - m_pClientFactory->findConsumerIds( - topic, m_pConsumer->getGroupName(), cidAll, - m_pConsumer->getSessionCredentials()); - - if (cidAll.empty()) { - /*remove the droping pullRequest changes for recovery consume fastly - from network broken - //drop all pullRequest - MQ2PULLREQ::iterator it = m_requestQueueTable.begin(); - for (; it != m_requestQueueTable.end(); ++it) - { - if(!(it->second->isDroped())) - { - MQMessageQueue mqtemp = it->first; - it->second->setDroped(true); - removeUnnecessaryMessageQueue(mqtemp); - it->second->clearAllMsgs();//add clear operation to - avoid bad - state when dropped pullRequest returns normal - LOG_INFO("find consumer failed, drop undropped mq:%s", - mqtemp.toString().c_str()); + LOG_DEBUG("start doRebalance"); + try { + map::iterator it = m_subscriptionData.begin(); + for (; it != m_subscriptionData.end(); ++it) { + string topic = (it->first); + LOG_INFO("current topic is:%s", topic.c_str()); + // mqs + vector mqAll; + if (!getTopicSubscribeInfo(topic, mqAll)) { + continue; + } + if (mqAll.empty()) { + if (!UtilAll::startsWith_retry(topic)) + THROW_MQEXCEPTION(MQClientException, "doRebalance the topic is empty", -1); + } + + //getMessageModel()) { + case BROADCASTING: { + bool changed = updateRequestTableInRebalance(topic, mqAll); + if (changed) { + messageQueueChanged(topic, mqAll, mqAll); } - }*/ - - THROW_MQEXCEPTION(MQClientException, - "doRebalance the cidAll is empty", -1); - } - // log - for (int i = 0; i < (int)cidAll.size(); ++i) { - LOG_INFO("client id:%s of topic:%s", cidAll[i].c_str(), - topic.c_str()); - } - // allocateResult; - try { - m_pAllocateMQStrategy->allocate(m_pConsumer->getMQClientId(), mqAll, - cidAll, allocateResult); - } catch (MQException& e) { - THROW_MQEXCEPTION(MQClientException, "allocate error", -1); - } - - // log - for (int i = 0; i < (int)allocateResult.size(); ++i) { - LOG_INFO("allocate mq:%s", allocateResult[i].toString().c_str()); - } - - // cidAll; + m_pClientFactory->findConsumerIds(topic, m_pConsumer->getGroupName(), cidAll, + m_pConsumer->getSessionCredentials()); + + if (cidAll.empty()) { + /*remove the droping pullRequest changes for recovery consume fastly + from network broken + //drop all pullRequest + MQ2PULLREQ::iterator it = m_requestQueueTable.begin(); + for (; it != m_requestQueueTable.end(); ++it) + { + if(!(it->second->isDroped())) + { + MQMessageQueue mqtemp = it->first; + it->second->setDroped(true); + removeUnnecessaryMessageQueue(mqtemp); + it->second->clearAllMsgs();//add clear operation to + avoid bad + state when dropped pullRequest returns normal + LOG_INFO("find consumer failed, drop undropped mq:%s", + mqtemp.toString().c_str()); + } + }*/ + + THROW_MQEXCEPTION(MQClientException, "doRebalance the cidAll is empty", -1); + } + // log + for (int i = 0; i < (int)cidAll.size(); ++i) { + LOG_INFO("client id:%s of topic:%s", cidAll[i].c_str(), topic.c_str()); + } + // allocateResult; + try { + m_pAllocateMQStrategy->allocate(m_pConsumer->getMQClientId(), mqAll, cidAll, allocateResult); + } + catch (MQException& e) { + THROW_MQEXCEPTION(MQClientException, "allocate error", -1); + } + + // log + for (int i = 0; i < (int)allocateResult.size(); ++i) { + LOG_INFO("allocate mq:%s", allocateResult[i].toString().c_str()); + } + + //(m_pConsumer); - OffsetStore* pOffsetStore = pConsumer->getOffsetStore(); - vector mqs; - { - boost::lock_guard lock(m_requestTableMutex); - MQ2PULLREQ::iterator it = m_requestQueueTable.begin(); - for (; it != m_requestQueueTable.end(); ++it) { - if (it->second && (!it->second->isDroped())) { - mqs.push_back(it->first); - } + DefaultMQPushConsumer* pConsumer = static_cast(m_pConsumer); + OffsetStore* pOffsetStore = pConsumer->getOffsetStore(); + vector mqs; + { + boost::lock_guard lock(m_requestTableMutex); + MQ2PULLREQ::iterator it = m_requestQueueTable.begin(); + for (; it != m_requestQueueTable.end(); ++it) { + if (it->second && (!it->second->isDroped())) { + mqs.push_back(it->first); + } + } } - } - if (pConsumer->getMessageModel() == BROADCASTING) { - pOffsetStore->persistAll(mqs); - } else { - vector::iterator it2 = mqs.begin(); - for (; it2 != mqs.end(); ++it2) { - pOffsetStore->persist(*it2, m_pConsumer->getSessionCredentials()); + if (pConsumer->getMessageModel() == BROADCASTING) { + pOffsetStore->persistAll(mqs); + } else { + vector::iterator it2 = mqs.begin(); + for (; it2 != mqs.end(); ++it2) { + pOffsetStore->persist(*it2, m_pConsumer->getSessionCredentials()); + } } - } } void Rebalance::persistConsumerOffsetByResetOffset() { - DefaultMQPushConsumer* pConsumer = - static_cast(m_pConsumer); - OffsetStore* pOffsetStore = pConsumer->getOffsetStore(); - vector mqs; - { - boost::lock_guard lock(m_requestTableMutex); - MQ2PULLREQ::iterator it = m_requestQueueTable.begin(); - for (; it != m_requestQueueTable.end(); ++it) { - if (it->second) { // even if it was dropped, also need update offset when - // rcv resetOffset cmd - mqs.push_back(it->first); - } + DefaultMQPushConsumer* pConsumer = static_cast(m_pConsumer); + OffsetStore* pOffsetStore = pConsumer->getOffsetStore(); + vector mqs; + { + boost::lock_guard lock(m_requestTableMutex); + MQ2PULLREQ::iterator it = m_requestQueueTable.begin(); + for (; it != m_requestQueueTable.end(); ++it) { + if (it->second) { // even if it was dropped, also need update offset when + // rcv resetOffset cmd + mqs.push_back(it->first); + } + } + } + vector::iterator it2 = mqs.begin(); + for (; it2 != mqs.end(); ++it2) { + pOffsetStore->persist(*it2, m_pConsumer->getSessionCredentials()); } - } - vector::iterator it2 = mqs.begin(); - for (; it2 != mqs.end(); ++it2) { - pOffsetStore->persist(*it2, m_pConsumer->getSessionCredentials()); - } } SubscriptionData* Rebalance::getSubscriptionData(const string& topic) { - if (m_subscriptionData.find(topic) != m_subscriptionData.end()) { - return m_subscriptionData[topic]; - } - return NULL; + if (m_subscriptionData.find(topic) != m_subscriptionData.end()) { + return m_subscriptionData[topic]; + } + return NULL; } -map& Rebalance::getSubscriptionInner() { - return m_subscriptionData; -} +map& Rebalance::getSubscriptionInner() { return m_subscriptionData; } -void Rebalance::setSubscriptionData(const string& topic, - SubscriptionData* pdata) { - if (pdata != NULL && - m_subscriptionData.find(topic) == m_subscriptionData.end()) - m_subscriptionData[topic] = pdata; +void Rebalance::setSubscriptionData(const string& topic, SubscriptionData* pdata) { + if (pdata != NULL && m_subscriptionData.find(topic) == m_subscriptionData.end()) m_subscriptionData[topic] = pdata; } -void Rebalance::setTopicSubscribeInfo(const string& topic, - vector& mqs) { - if (m_subscriptionData.find(topic) != m_subscriptionData.end()) { - { - boost::lock_guard lock(m_topicSubscribeInfoTableMutex); - if (m_topicSubscribeInfoTable.find(topic) != - m_topicSubscribeInfoTable.end()) - m_topicSubscribeInfoTable.erase(topic); - m_topicSubscribeInfoTable[topic] = mqs; - } - // log - vector::iterator it = mqs.begin(); - for (; it != mqs.end(); ++it) { - LOG_DEBUG("topic [%s] has :%s", topic.c_str(), (*it).toString().c_str()); +void Rebalance::setTopicSubscribeInfo(const string& topic, vector& mqs) { + if (m_subscriptionData.find(topic) != m_subscriptionData.end()) { + { + boost::lock_guard lock(m_topicSubscribeInfoTableMutex); + if (m_topicSubscribeInfoTable.find(topic) != m_topicSubscribeInfoTable.end()) + m_topicSubscribeInfoTable.erase(topic); + m_topicSubscribeInfoTable[topic] = mqs; + } + // log + vector::iterator it = mqs.begin(); + for (; it != mqs.end(); ++it) { + LOG_DEBUG("topic [%s] has :%s", topic.c_str(), (*it).toString().c_str()); + } } - } } -bool Rebalance::getTopicSubscribeInfo(const string& topic, - vector& mqs) { - boost::lock_guard lock(m_topicSubscribeInfoTableMutex); - if (m_topicSubscribeInfoTable.find(topic) != - m_topicSubscribeInfoTable.end()) { - mqs = m_topicSubscribeInfoTable[topic]; - return true; - } - return false; +bool Rebalance::getTopicSubscribeInfo(const string& topic, vector& mqs) { + boost::lock_guard lock(m_topicSubscribeInfoTableMutex); + if (m_topicSubscribeInfoTable.find(topic) != m_topicSubscribeInfoTable.end()) { + mqs = m_topicSubscribeInfoTable[topic]; + return true; + } + return false; } void Rebalance::addPullRequest(MQMessageQueue mq, PullRequest* pPullRequest) { - boost::lock_guard lock(m_requestTableMutex); - m_requestQueueTable[mq] = pPullRequest; + boost::lock_guard lock(m_requestTableMutex); + m_requestQueueTable[mq] = pPullRequest; } PullRequest* Rebalance::getPullRequest(MQMessageQueue mq) { - boost::lock_guard lock(m_requestTableMutex); - if (m_requestQueueTable.find(mq) != m_requestQueueTable.end()) { - return m_requestQueueTable[mq]; - } - return NULL; + boost::lock_guard lock(m_requestTableMutex); + if (m_requestQueueTable.find(mq) != m_requestQueueTable.end()) { + return m_requestQueueTable[mq]; + } + return NULL; } map Rebalance::getPullRequestTable() { - boost::lock_guard lock(m_requestTableMutex); - return m_requestQueueTable; + boost::lock_guard lock(m_requestTableMutex); + return m_requestQueueTable; } void Rebalance::unlockAll(bool oneway) { - map*> brokerMqs; - MQ2PULLREQ requestQueueTable = getPullRequestTable(); - for (MQ2PULLREQ::iterator it = requestQueueTable.begin(); - it != requestQueueTable.end(); ++it) { - if (!(it->second->isDroped())) { - if (brokerMqs.find(it->first.getBrokerName()) == brokerMqs.end()) { - vector* mqs = new vector; - brokerMqs[it->first.getBrokerName()] = mqs; - } else { - brokerMqs[it->first.getBrokerName()]->push_back(it->first); - } + map*> brokerMqs; + MQ2PULLREQ requestQueueTable = getPullRequestTable(); + for (MQ2PULLREQ::iterator it = requestQueueTable.begin(); it != requestQueueTable.end(); ++it) { + if (!(it->second->isDroped())) { + if (brokerMqs.find(it->first.getBrokerName()) == brokerMqs.end()) { + vector* mqs = new vector; + brokerMqs[it->first.getBrokerName()] = mqs; + } else { + brokerMqs[it->first.getBrokerName()]->push_back(it->first); + } + } + } + LOG_INFO("unLockAll " SIZET_FMT " broker mqs", brokerMqs.size()); + for (map*>::iterator itb = brokerMqs.begin(); itb != brokerMqs.end(); ++itb) { + unique_ptr pFindBrokerResult( + m_pClientFactory->findBrokerAddressInSubscribe(itb->first, MASTER_ID, true)); + if (!pFindBrokerResult) { + LOG_ERROR("unlockAll findBrokerAddressInSubscribe ret null for broker:%s", itb->first.data()); + continue; + } + unique_ptr unlockBatchRequest(new UnlockBatchRequestBody()); + vector mqs(*(itb->second)); + unlockBatchRequest->setClientId(m_pConsumer->getMQClientId()); + unlockBatchRequest->setConsumerGroup(m_pConsumer->getGroupName()); + unlockBatchRequest->setMqSet(mqs); + + try { + m_pClientFactory->getMQClientAPIImpl()->unlockBatchMQ( + pFindBrokerResult->brokerAddr, unlockBatchRequest.get(), 1000, m_pConsumer->getSessionCredentials()); + for (unsigned int i = 0; i != mqs.size(); ++i) { + PullRequest* pullreq = getPullRequest(mqs[i]); + if (pullreq) { + LOG_INFO("unlockBatchMQ success of mq:%s", mqs[i].toString().c_str()); + pullreq->setLocked(true); + } else { + LOG_ERROR("unlockBatchMQ fails of mq:%s", mqs[i].toString().c_str()); + } + } + } + catch (MQException& e) { + LOG_ERROR("unlockBatchMQ fails"); + } + deleteAndZero(itb->second); } - } - LOG_INFO("unLockAll " SIZET_FMT " broker mqs", brokerMqs.size()); - for (map*>::iterator itb = brokerMqs.begin(); - itb != brokerMqs.end(); ++itb) { + brokerMqs.clear(); +} + +void Rebalance::unlock(MQMessageQueue mq) { unique_ptr pFindBrokerResult( - m_pClientFactory->findBrokerAddressInSubscribe(itb->first, MASTER_ID, - true)); + m_pClientFactory->findBrokerAddressInSubscribe(mq.getBrokerName(), MASTER_ID, true)); if (!pFindBrokerResult) { - LOG_ERROR("unlockAll findBrokerAddressInSubscribe ret null for broker:%s", itb->first.data()); - continue; + LOG_ERROR("unlock findBrokerAddressInSubscribe ret null for broker:%s", mq.getBrokerName().data()); + return; } - unique_ptr unlockBatchRequest( - new UnlockBatchRequestBody()); - vector mqs(*(itb->second)); + unique_ptr unlockBatchRequest(new UnlockBatchRequestBody()); + vector mqs; + mqs.push_back(mq); unlockBatchRequest->setClientId(m_pConsumer->getMQClientId()); unlockBatchRequest->setConsumerGroup(m_pConsumer->getGroupName()); unlockBatchRequest->setMqSet(mqs); try { - m_pClientFactory->getMQClientAPIImpl()->unlockBatchMQ( - pFindBrokerResult->brokerAddr, unlockBatchRequest.get(), 1000, - m_pConsumer->getSessionCredentials()); - for (unsigned int i = 0; i != mqs.size(); ++i) { - PullRequest* pullreq = getPullRequest(mqs[i]); - if (pullreq) { - LOG_INFO("unlockBatchMQ success of mq:%s", mqs[i].toString().c_str()); - pullreq->setLocked(true); - } else { - LOG_ERROR("unlockBatchMQ fails of mq:%s", mqs[i].toString().c_str()); + m_pClientFactory->getMQClientAPIImpl()->unlockBatchMQ(pFindBrokerResult->brokerAddr, unlockBatchRequest.get(), + 1000, m_pConsumer->getSessionCredentials()); + for (unsigned int i = 0; i != mqs.size(); ++i) { + PullRequest* pullreq = getPullRequest(mqs[i]); + if (pullreq) { + LOG_INFO("unlock success of mq:%s", mqs[i].toString().c_str()); + pullreq->setLocked(true); + } else { + LOG_ERROR("unlock fails of mq:%s", mqs[i].toString().c_str()); + } } - } - } catch (MQException& e) { - LOG_ERROR("unlockBatchMQ fails"); } - deleteAndZero(itb->second); - } - brokerMqs.clear(); -} - -void Rebalance::unlock(MQMessageQueue mq) { - unique_ptr pFindBrokerResult( - m_pClientFactory->findBrokerAddressInSubscribe(mq.getBrokerName(), - MASTER_ID, true)); - if (!pFindBrokerResult) { - LOG_ERROR("unlock findBrokerAddressInSubscribe ret null for broker:%s", mq.getBrokerName().data()); - return; - } - unique_ptr unlockBatchRequest( - new UnlockBatchRequestBody()); - vector mqs; - mqs.push_back(mq); - unlockBatchRequest->setClientId(m_pConsumer->getMQClientId()); - unlockBatchRequest->setConsumerGroup(m_pConsumer->getGroupName()); - unlockBatchRequest->setMqSet(mqs); - - try { - m_pClientFactory->getMQClientAPIImpl()->unlockBatchMQ( - pFindBrokerResult->brokerAddr, unlockBatchRequest.get(), 1000, - m_pConsumer->getSessionCredentials()); - for (unsigned int i = 0; i != mqs.size(); ++i) { - PullRequest* pullreq = getPullRequest(mqs[i]); - if (pullreq) { - LOG_INFO("unlock success of mq:%s", mqs[i].toString().c_str()); - pullreq->setLocked(true); - } else { - LOG_ERROR("unlock fails of mq:%s", mqs[i].toString().c_str()); - } + catch (MQException& e) { + LOG_ERROR("unlock fails of mq:%s", mq.toString().c_str()); } - } catch (MQException& e) { - LOG_ERROR("unlock fails of mq:%s", mq.toString().c_str()); - } } void Rebalance::lockAll() { - map*> brokerMqs; - MQ2PULLREQ requestQueueTable = getPullRequestTable(); - for (MQ2PULLREQ::iterator it = requestQueueTable.begin(); - it != requestQueueTable.end(); ++it) { - if (!(it->second->isDroped())) { - string brokerKey = it->first.getBrokerName() + it->first.getTopic(); - if (brokerMqs.find(brokerKey) == brokerMqs.end()) { - vector* mqs = new vector; - brokerMqs[brokerKey] = mqs; - brokerMqs[brokerKey]->push_back(it->first); - } else { - brokerMqs[brokerKey]->push_back(it->first); - } + map*> brokerMqs; + MQ2PULLREQ requestQueueTable = getPullRequestTable(); + for (MQ2PULLREQ::iterator it = requestQueueTable.begin(); it != requestQueueTable.end(); ++it) { + if (!(it->second->isDroped())) { + string brokerKey = it->first.getBrokerName() + it->first.getTopic(); + if (brokerMqs.find(brokerKey) == brokerMqs.end()) { + vector* mqs = new vector; + brokerMqs[brokerKey] = mqs; + brokerMqs[brokerKey]->push_back(it->first); + } else { + brokerMqs[brokerKey]->push_back(it->first); + } + } + } + LOG_INFO("LockAll " SIZET_FMT " broker mqs", brokerMqs.size()); + for (map*>::iterator itb = brokerMqs.begin(); itb != brokerMqs.end(); ++itb) { + string brokerName = (*(itb->second))[0].getBrokerName(); + unique_ptr pFindBrokerResult( + m_pClientFactory->findBrokerAddressInSubscribe(brokerName, MASTER_ID, true)); + if (!pFindBrokerResult) { + LOG_ERROR("lockAll findBrokerAddressInSubscribe ret null for broker:%s", brokerName.data()); + continue; + } + unique_ptr lockBatchRequest(new LockBatchRequestBody()); + lockBatchRequest->setClientId(m_pConsumer->getMQClientId()); + lockBatchRequest->setConsumerGroup(m_pConsumer->getGroupName()); + lockBatchRequest->setMqSet(*(itb->second)); + LOG_INFO("try to lock:" SIZET_FMT " mqs of broker:%s", itb->second->size(), itb->first.c_str()); + try { + vector messageQueues; + m_pClientFactory->getMQClientAPIImpl()->lockBatchMQ(pFindBrokerResult->brokerAddr, lockBatchRequest.get(), + messageQueues, 1000, + m_pConsumer->getSessionCredentials()); + for (unsigned int i = 0; i != messageQueues.size(); ++i) { + PullRequest* pullreq = getPullRequest(messageQueues[i]); + if (pullreq) { + LOG_INFO("lockBatchMQ success of mq:%s", messageQueues[i].toString().c_str()); + pullreq->setLocked(true); + pullreq->setLastLockTimestamp(UtilAll::currentTimeMillis()); + } else { + LOG_ERROR("lockBatchMQ fails of mq:%s", messageQueues[i].toString().c_str()); + } + } + messageQueues.clear(); + } + catch (MQException& e) { + LOG_ERROR("lockBatchMQ fails"); + } + deleteAndZero(itb->second); } - } - LOG_INFO("LockAll " SIZET_FMT " broker mqs", brokerMqs.size()); - for (map*>::iterator itb = brokerMqs.begin(); - itb != brokerMqs.end(); ++itb) { - string brokerName = (*(itb->second))[0].getBrokerName(); + brokerMqs.clear(); +} +bool Rebalance::lock(MQMessageQueue mq) { unique_ptr pFindBrokerResult( - m_pClientFactory->findBrokerAddressInSubscribe( - brokerName, MASTER_ID, true)); + m_pClientFactory->findBrokerAddressInSubscribe(mq.getBrokerName(), MASTER_ID, true)); if (!pFindBrokerResult) { - LOG_ERROR("lockAll findBrokerAddressInSubscribe ret null for broker:%s", brokerName.data()); - continue; + LOG_ERROR("lock findBrokerAddressInSubscribe ret null for broker:%s", mq.getBrokerName().data()); + return false; } - unique_ptr lockBatchRequest( - new LockBatchRequestBody()); + unique_ptr lockBatchRequest(new LockBatchRequestBody()); lockBatchRequest->setClientId(m_pConsumer->getMQClientId()); lockBatchRequest->setConsumerGroup(m_pConsumer->getGroupName()); - lockBatchRequest->setMqSet(*(itb->second)); - LOG_INFO("try to lock:" SIZET_FMT " mqs of broker:%s", itb->second->size(), - itb->first.c_str()); + vector in_mqSet; + in_mqSet.push_back(mq); + lockBatchRequest->setMqSet(in_mqSet); + bool lockResult = false; + try { - vector messageQueues; - m_pClientFactory->getMQClientAPIImpl()->lockBatchMQ( - pFindBrokerResult->brokerAddr, lockBatchRequest.get(), messageQueues, - 1000, m_pConsumer->getSessionCredentials()); - for (unsigned int i = 0; i != messageQueues.size(); ++i) { - PullRequest* pullreq = getPullRequest(messageQueues[i]); - if (pullreq) { - LOG_INFO("lockBatchMQ success of mq:%s", - messageQueues[i].toString().c_str()); - pullreq->setLocked(true); - pullreq->setLastLockTimestamp(UtilAll::currentTimeMillis()); - } else { - LOG_ERROR("lockBatchMQ fails of mq:%s", - messageQueues[i].toString().c_str()); + vector messageQueues; + LOG_DEBUG("try to lock mq:%s", mq.toString().c_str()); + m_pClientFactory->getMQClientAPIImpl()->lockBatchMQ(pFindBrokerResult->brokerAddr, lockBatchRequest.get(), + messageQueues, 1000, m_pConsumer->getSessionCredentials()); + if (messageQueues.size() == 0) { + LOG_ERROR("lock mq on broker:%s failed", pFindBrokerResult->brokerAddr.c_str()); + return false; } - } - messageQueues.clear(); - } catch (MQException& e) { - LOG_ERROR("lockBatchMQ fails"); - } - deleteAndZero(itb->second); - } - brokerMqs.clear(); -} -bool Rebalance::lock(MQMessageQueue mq) { - unique_ptr pFindBrokerResult( - m_pClientFactory->findBrokerAddressInSubscribe(mq.getBrokerName(), - MASTER_ID, true)); - if (!pFindBrokerResult) { - LOG_ERROR("lock findBrokerAddressInSubscribe ret null for broker:%s", mq.getBrokerName().data()); - return false; - } - unique_ptr lockBatchRequest(new LockBatchRequestBody()); - lockBatchRequest->setClientId(m_pConsumer->getMQClientId()); - lockBatchRequest->setConsumerGroup(m_pConsumer->getGroupName()); - vector in_mqSet; - in_mqSet.push_back(mq); - lockBatchRequest->setMqSet(in_mqSet); - bool lockResult = false; - - try { - vector messageQueues; - LOG_DEBUG("try to lock mq:%s", mq.toString().c_str()); - m_pClientFactory->getMQClientAPIImpl()->lockBatchMQ( - pFindBrokerResult->brokerAddr, lockBatchRequest.get(), messageQueues, - 1000, m_pConsumer->getSessionCredentials()); - if (messageQueues.size() == 0) { - LOG_ERROR("lock mq on broker:%s failed", - pFindBrokerResult->brokerAddr.c_str()); - return false; + for (unsigned int i = 0; i != messageQueues.size(); ++i) { + PullRequest* pullreq = getPullRequest(messageQueues[i]); + if (pullreq) { + LOG_INFO("lock success of mq:%s", messageQueues[i].toString().c_str()); + pullreq->setLocked(true); + pullreq->setLastLockTimestamp(UtilAll::currentTimeMillis()); + lockResult = true; + } else { + LOG_ERROR("lock fails of mq:%s", messageQueues[i].toString().c_str()); + } + } + messageQueues.clear(); + return lockResult; } - for (unsigned int i = 0; i != messageQueues.size(); ++i) { - PullRequest* pullreq = getPullRequest(messageQueues[i]); - if (pullreq) { - LOG_INFO("lock success of mq:%s", messageQueues[i].toString().c_str()); - pullreq->setLocked(true); - pullreq->setLastLockTimestamp(UtilAll::currentTimeMillis()); - lockResult = true; - } else { - LOG_ERROR("lock fails of mq:%s", messageQueues[i].toString().c_str()); - } + catch (MQException& e) { + LOG_ERROR("lock fails of mq:%s", mq.toString().c_str()); + return false; } - messageQueues.clear(); - return lockResult; - } catch (MQException& e) { - LOG_ERROR("lock fails of mq:%s", mq.toString().c_str()); - return false; - } } //& mqsSelf) { - return false; +bool RebalancePull::updateRequestTableInRebalance(const string& topic, vector& mqsSelf) { + return false; } -int64 RebalancePull::computePullFromWhere(const MQMessageQueue& mq) { - return 0; -} +int64 RebalancePull::computePullFromWhere(const MQMessageQueue& mq) { return 0; } -void RebalancePull::messageQueueChanged(const string& topic, - vector& mqAll, +void RebalancePull::messageQueueChanged(const string& topic, vector& mqAll, vector& mqDivided) {} void RebalancePull::removeUnnecessaryMessageQueue(const MQMessageQueue& mq) {} //& mqsSelf) { - LOG_DEBUG("updateRequestTableInRebalance Enter"); - if (mqsSelf.empty()) { - LOG_WARN("allocated queue is empty for topic:%s", topic.c_str()); - } - - bool changed = false; - - //first; - if (mqtemp.getTopic().compare(topic) == 0) { - if (mqsSelf.empty() || - (find(mqsSelf.begin(), mqsSelf.end(), mqtemp) == mqsSelf.end())) { - if (!(it->second->isDroped())) { - it->second->setDroped(true); - //delete the lastest pull request for this mq, which hasn't been response - //m_pClientFactory->removeDropedPullRequestOpaque(it->second); - removeUnnecessaryMessageQueue(mqtemp); - it->second->clearAllMsgs(); // add clear operation to avoid bad state - // when dropped pullRequest returns - // normal - LOG_INFO("drop mq:%s, delete opaque:%d", mqtemp.toString().c_str(), it->second->getLatestPullRequestOpaque()); - } - changed = true; - } +RebalancePush::RebalancePush(MQConsumer* consumer, MQClientFactory* pfactory) : Rebalance(consumer, pfactory) {} + +bool RebalancePush::updateRequestTableInRebalance(const string& topic, vector& mqsSelf) { + LOG_DEBUG("updateRequestTableInRebalance Enter"); + if (mqsSelf.empty()) { + LOG_WARN("allocated queue is empty for topic:%s", topic.c_str()); } - } - - // pullrequestAdd; - DefaultMQPushConsumer* pConsumer = - static_cast(m_pConsumer); - vector::iterator it2 = mqsSelf.begin(); - for (; it2 != mqsSelf.end(); ++it2) { - PullRequest* pPullRequest(getPullRequest(*it2)); - if (pPullRequest && pPullRequest->isDroped()) { - LOG_DEBUG( - "before resume the pull handle of this pullRequest, its mq is:%s, " - "its offset is:%lld", - (it2->toString()).c_str(), pPullRequest->getNextOffset()); - pConsumer->getOffsetStore()->removeOffset( - *it2); // remove dirty offset which maybe update to - // OffsetStore::m_offsetTable by consuming After last - // drop - int64 nextOffset = computePullFromWhere(*it2); - if (nextOffset >= 0) { - /* - Fix issue with following scenario: - 1. pullRequest was dropped - 2. the pullMsgEvent was not executed by taskQueue, so the PullMsgEvent - was not stop - 3. pullReuest was resumed by next doRebalance, then mulitple - pullMsgEvent were produced for pullRequest - */ - bool bPullMsgEvent = pPullRequest->addPullMsgEvent(); - while (!bPullMsgEvent) { - boost::this_thread::sleep_for(boost::chrono::milliseconds(50)); - LOG_INFO("pullRequest with mq :%s has unfinished pullMsgEvent", - (it2->toString()).c_str()); - bPullMsgEvent = pPullRequest->addPullMsgEvent(); + + bool changed = false; + + //first; + if (mqtemp.getTopic().compare(topic) == 0) { + if (mqsSelf.empty() || (find(mqsSelf.begin(), mqsSelf.end(), mqtemp) == mqsSelf.end())) { + if (!(it->second->isDroped())) { + it->second->setDroped(true); + // delete the lastest pull request for this mq, which hasn't been response + // m_pClientFactory->removeDropedPullRequestOpaque(it->second); + removeUnnecessaryMessageQueue(mqtemp); + it->second->clearAllMsgs(); // add clear operation to avoid bad state + // when dropped pullRequest returns + // normal + LOG_INFO("drop mq:%s, delete opaque:%d", mqtemp.toString().c_str(), + it->second->getLatestPullRequestOpaque()); + } + changed = true; + } } - pPullRequest->setDroped(false); - pPullRequest->clearAllMsgs(); // avoid consume accumulation and consume - // dumplication issues - pPullRequest->setNextOffset(nextOffset); - pPullRequest->updateQueueMaxOffset(nextOffset); - LOG_INFO( - "after resume the pull handle of this pullRequest, its mq is:%s, " - "its offset is:%lld", - (it2->toString()).c_str(), pPullRequest->getNextOffset()); - changed = true; - pConsumer->producePullMsgTask(pPullRequest); - } else { - LOG_ERROR( - "get fatel error QueryOffset of mq:%s, do not reconsume this queue", - (it2->toString()).c_str()); - } } - if (!pPullRequest) { - LOG_INFO("updateRequestTableInRebalance Doesn't find old mq"); - PullRequest* pullRequest = new PullRequest(m_pConsumer->getGroupName()); - pullRequest->m_messageQueue = *it2; - - int64 nextOffset = computePullFromWhere(*it2); - if (nextOffset >= 0) { - pullRequest->setNextOffset(nextOffset); - pullRequest->clearAllMsgs(); // avoid consume accumulation and consume - // dumplication issues - changed = true; - // pq; - addPullRequest(*it2, pullRequest); - pullrequestAdd.push_back(pullRequest); - LOG_INFO("add mq:%s, request initiall offset:%lld", - (*it2).toString().c_str(), nextOffset); - } + // pullrequestAdd; + DefaultMQPushConsumer* pConsumer = static_cast(m_pConsumer); + vector::iterator it2 = mqsSelf.begin(); + for (; it2 != mqsSelf.end(); ++it2) { + PullRequest* pPullRequest(getPullRequest(*it2)); + if (pPullRequest && pPullRequest->isDroped()) { + LOG_DEBUG( + "before resume the pull handle of this pullRequest, its mq is:%s, " + "its offset is:%lld", + (it2->toString()).c_str(), pPullRequest->getNextOffset()); + pConsumer->getOffsetStore()->removeOffset(*it2); // remove dirty offset which maybe update to + // OffsetStore::m_offsetTable by consuming After last + // drop + int64 nextOffset = computePullFromWhere(*it2); + if (nextOffset >= 0) { + /* + Fix issue with following scenario: + 1. pullRequest was dropped + 2. the pullMsgEvent was not executed by taskQueue, so the PullMsgEvent + was not stop + 3. pullReuest was resumed by next doRebalance, then mulitple + pullMsgEvent were produced for pullRequest + */ + bool bPullMsgEvent = pPullRequest->addPullMsgEvent(); + while (!bPullMsgEvent) { + boost::this_thread::sleep_for(boost::chrono::milliseconds(50)); + LOG_INFO("pullRequest with mq :%s has unfinished pullMsgEvent", (it2->toString()).c_str()); + bPullMsgEvent = pPullRequest->addPullMsgEvent(); + } + pPullRequest->setDroped(false); + pPullRequest->clearAllMsgs(); // avoid consume accumulation and consume + // dumplication issues + pPullRequest->setNextOffset(nextOffset); + pPullRequest->updateQueueMaxOffset(nextOffset); + LOG_INFO( + "after resume the pull handle of this pullRequest, its mq is:%s, " + "its offset is:%lld", + (it2->toString()).c_str(), pPullRequest->getNextOffset()); + changed = true; + pConsumer->producePullMsgTask(pPullRequest); + } else { + LOG_ERROR("get fatel error QueryOffset of mq:%s, do not reconsume this queue", + (it2->toString()).c_str()); + } + } + + if (!pPullRequest) { + LOG_INFO("updateRequestTableInRebalance Doesn't find old mq"); + PullRequest* pullRequest = new PullRequest(m_pConsumer->getGroupName()); + pullRequest->m_messageQueue = *it2; + + int64 nextOffset = computePullFromWhere(*it2); + if (nextOffset >= 0) { + pullRequest->setNextOffset(nextOffset); + pullRequest->clearAllMsgs(); // avoid consume accumulation and consume + // dumplication issues + changed = true; + // pq; + addPullRequest(*it2, pullRequest); + pullrequestAdd.push_back(pullRequest); + LOG_INFO("add mq:%s, request initiall offset:%lld", (*it2).toString().c_str(), nextOffset); + } + } } - } - vector::iterator it3 = pullrequestAdd.begin(); - for (; it3 != pullrequestAdd.end(); ++it3) { - LOG_DEBUG("start pull request"); - pConsumer->producePullMsgTask(*it3); - } + vector::iterator it3 = pullrequestAdd.begin(); + for (; it3 != pullrequestAdd.end(); ++it3) { + LOG_DEBUG("start pull request"); + pConsumer->producePullMsgTask(*it3); + } - LOG_DEBUG("updateRequestTableInRebalance exit"); - return changed; + LOG_DEBUG("updateRequestTableInRebalance exit"); + return changed; } int64 RebalancePush::computePullFromWhere(const MQMessageQueue& mq) { - int64 result = -1; - DefaultMQPushConsumer* pConsumer = - static_cast(m_pConsumer); - ConsumeFromWhere consumeFromWhere = pConsumer->getConsumeFromWhere(); - OffsetStore* pOffsetStore = pConsumer->getOffsetStore(); - switch (consumeFromWhere) { - case CONSUME_FROM_LAST_OFFSET: { - int64 lastOffset = pOffsetStore->readOffset( - mq, READ_FROM_STORE, m_pConsumer->getSessionCredentials()); - if (lastOffset >= 0) { - LOG_INFO("CONSUME_FROM_LAST_OFFSET, lastOffset of mq:%s is:%lld", - mq.toString().c_str(), lastOffset); - result = lastOffset; - } else if (-1 == lastOffset) { - LOG_WARN("CONSUME_FROM_LAST_OFFSET, lastOffset of mq:%s is -1", - mq.toString().c_str()); - if (UtilAll::startsWith_retry(mq.getTopic())) { - LOG_INFO("CONSUME_FROM_LAST_OFFSET, lastOffset of mq:%s is 0", - mq.toString().c_str()); - result = 0; - } else { - try { - result = pConsumer->maxOffset(mq); - LOG_INFO("CONSUME_FROM_LAST_OFFSET, maxOffset of mq:%s is:%lld", - mq.toString().c_str(), result); - } catch (MQException& e) { - LOG_ERROR( - "CONSUME_FROM_LAST_OFFSET error, lastOffset of mq:%s is -1", - mq.toString().c_str()); - result = -1; - } + int64 result = -1; + DefaultMQPushConsumer* pConsumer = static_cast(m_pConsumer); + ConsumeFromWhere consumeFromWhere = pConsumer->getConsumeFromWhere(); + OffsetStore* pOffsetStore = pConsumer->getOffsetStore(); + switch (consumeFromWhere) { + case CONSUME_FROM_LAST_OFFSET: { + int64 lastOffset = pOffsetStore->readOffset(mq, READ_FROM_STORE, m_pConsumer->getSessionCredentials()); + if (lastOffset >= 0) { + LOG_INFO("CONSUME_FROM_LAST_OFFSET, lastOffset of mq:%s is:%lld", mq.toString().c_str(), lastOffset); + result = lastOffset; + } else if (-1 == lastOffset) { + LOG_WARN("CONSUME_FROM_LAST_OFFSET, lastOffset of mq:%s is -1", mq.toString().c_str()); + if (UtilAll::startsWith_retry(mq.getTopic())) { + LOG_INFO("CONSUME_FROM_LAST_OFFSET, lastOffset of mq:%s is 0", mq.toString().c_str()); + result = 0; + } else { + try { + result = pConsumer->maxOffset(mq); + LOG_INFO("CONSUME_FROM_LAST_OFFSET, maxOffset of mq:%s is:%lld", mq.toString().c_str(), result); + } + catch (MQException& e) { + LOG_ERROR("CONSUME_FROM_LAST_OFFSET error, lastOffset of mq:%s is -1", mq.toString().c_str()); + result = -1; + } + } + } else { + LOG_ERROR("CONSUME_FROM_LAST_OFFSET error, lastOffset of mq:%s is -1", mq.toString().c_str()); + result = -1; + } + break; } - } else { - LOG_ERROR("CONSUME_FROM_LAST_OFFSET error, lastOffset of mq:%s is -1", - mq.toString().c_str()); - result = -1; - } - break; - } - case CONSUME_FROM_FIRST_OFFSET: { - int64 lastOffset = pOffsetStore->readOffset( - mq, READ_FROM_STORE, m_pConsumer->getSessionCredentials()); - if (lastOffset >= 0) { - LOG_INFO("CONSUME_FROM_FIRST_OFFSET, lastOffset of mq:%s is:%lld", - mq.toString().c_str(), lastOffset); - result = lastOffset; - } else if (-1 == lastOffset) { - LOG_INFO("CONSUME_FROM_FIRST_OFFSET, lastOffset of mq:%s, return 0", - mq.toString().c_str()); - result = 0; - } else { - LOG_ERROR("CONSUME_FROM_FIRST_OFFSET, lastOffset of mq:%s, return -1", - mq.toString().c_str()); - result = -1; - } - break; - } - case CONSUME_FROM_TIMESTAMP: { - int64 lastOffset = pOffsetStore->readOffset( - mq, READ_FROM_STORE, m_pConsumer->getSessionCredentials()); - if (lastOffset >= 0) { - LOG_INFO("CONSUME_FROM_TIMESTAMP, lastOffset of mq:%s is:%lld", - mq.toString().c_str(), lastOffset); - result = lastOffset; - } else if (-1 == lastOffset) { - if (UtilAll::startsWith_retry(mq.getTopic())) { - try { - result = pConsumer->maxOffset(mq); - LOG_INFO("CONSUME_FROM_TIMESTAMP, maxOffset of mq:%s is:%lld", - mq.toString().c_str(), result); - } catch (MQException& e) { - LOG_ERROR( - "CONSUME_FROM_TIMESTAMP error, lastOffset of mq:%s is -1", - mq.toString().c_str()); - result = -1; - } - } else { - try { - } catch (MQException& e) { - LOG_ERROR( - "CONSUME_FROM_TIMESTAMP error, lastOffset of mq:%s, return 0", - mq.toString().c_str()); - result = -1; - } + case CONSUME_FROM_FIRST_OFFSET: { + int64 lastOffset = pOffsetStore->readOffset(mq, READ_FROM_STORE, m_pConsumer->getSessionCredentials()); + if (lastOffset >= 0) { + LOG_INFO("CONSUME_FROM_FIRST_OFFSET, lastOffset of mq:%s is:%lld", mq.toString().c_str(), lastOffset); + result = lastOffset; + } else if (-1 == lastOffset) { + LOG_INFO("CONSUME_FROM_FIRST_OFFSET, lastOffset of mq:%s, return 0", mq.toString().c_str()); + result = 0; + } else { + LOG_ERROR("CONSUME_FROM_FIRST_OFFSET, lastOffset of mq:%s, return -1", mq.toString().c_str()); + result = -1; + } + break; } - } else { - LOG_ERROR( - "CONSUME_FROM_TIMESTAMP error, lastOffset of mq:%s, return -1", - mq.toString().c_str()); - result = -1; - } - break; + case CONSUME_FROM_TIMESTAMP: { + int64 lastOffset = pOffsetStore->readOffset(mq, READ_FROM_STORE, m_pConsumer->getSessionCredentials()); + if (lastOffset >= 0) { + LOG_INFO("CONSUME_FROM_TIMESTAMP, lastOffset of mq:%s is:%lld", mq.toString().c_str(), lastOffset); + result = lastOffset; + } else if (-1 == lastOffset) { + if (UtilAll::startsWith_retry(mq.getTopic())) { + try { + result = pConsumer->maxOffset(mq); + LOG_INFO("CONSUME_FROM_TIMESTAMP, maxOffset of mq:%s is:%lld", mq.toString().c_str(), result); + } + catch (MQException& e) { + LOG_ERROR("CONSUME_FROM_TIMESTAMP error, lastOffset of mq:%s is -1", mq.toString().c_str()); + result = -1; + } + } else { + try { + } + catch (MQException& e) { + LOG_ERROR("CONSUME_FROM_TIMESTAMP error, lastOffset of mq:%s, return 0", + mq.toString().c_str()); + result = -1; + } + } + } else { + LOG_ERROR("CONSUME_FROM_TIMESTAMP error, lastOffset of mq:%s, return -1", mq.toString().c_str()); + result = -1; + } + break; + } + default: + break; } - default: - break; - } - return result; + return result; } -void RebalancePush::messageQueueChanged(const string& topic, - vector& mqAll, +void RebalancePush::messageQueueChanged(const string& topic, vector& mqAll, vector& mqDivided) {} void RebalancePush::removeUnnecessaryMessageQueue(const MQMessageQueue& mq) { - DefaultMQPushConsumer* pConsumer = - static_cast(m_pConsumer); - OffsetStore* pOffsetStore = pConsumer->getOffsetStore(); - - pOffsetStore->persist(mq, m_pConsumer->getSessionCredentials()); - pOffsetStore->removeOffset(mq); - if (pConsumer->getMessageListenerType() == messageListenerOrderly) { - unlock(mq); - } + DefaultMQPushConsumer* pConsumer = static_cast(m_pConsumer); + OffsetStore* pOffsetStore = pConsumer->getOffsetStore(); + + pOffsetStore->persist(mq, m_pConsumer->getSessionCredentials()); + pOffsetStore->removeOffset(mq); + if (pConsumer->getMessageListenerType() == messageListenerOrderly) { + unlock(mq); + } } //& mqAll, - vector& mqDivided) = 0; - - virtual void removeUnnecessaryMessageQueue(const MQMessageQueue& mq) = 0; - - virtual int64 computePullFromWhere(const MQMessageQueue& mq) = 0; - - virtual bool updateRequestTableInRebalance( - const string& topic, vector& mqsSelf) = 0; - - public: - void doRebalance(); - void persistConsumerOffset(); - void persistConsumerOffsetByResetOffset(); - //& getSubscriptionInner(); - - //& mqs); - bool getTopicSubscribeInfo(const string& topic, vector& mqs); - - void addPullRequest(MQMessageQueue mq, PullRequest* pPullRequest); - PullRequest* getPullRequest(MQMessageQueue mq); - map getPullRequestTable(); - void lockAll(); - bool lock(MQMessageQueue mq); - void unlockAll(bool oneway = false); - void unlock(MQMessageQueue mq); - - protected: - map m_subscriptionData; - - boost::mutex m_topicSubscribeInfoTableMutex; - map> m_topicSubscribeInfoTable; - typedef map MQ2PULLREQ; - MQ2PULLREQ m_requestQueueTable; - boost::mutex m_requestTableMutex; - - AllocateMQStrategy* m_pAllocateMQStrategy; - MQConsumer* m_pConsumer; - MQClientFactory* m_pClientFactory; +public: + Rebalance(MQConsumer*, MQClientFactory*); + virtual ~Rebalance(); + + virtual void messageQueueChanged(const string& topic, vector& mqAll, + vector& mqDivided) = 0; + + virtual void removeUnnecessaryMessageQueue(const MQMessageQueue& mq) = 0; + + virtual int64 computePullFromWhere(const MQMessageQueue& mq) = 0; + + virtual bool updateRequestTableInRebalance(const string& topic, vector& mqsSelf) = 0; + +public: + void doRebalance(); + void persistConsumerOffset(); + void persistConsumerOffsetByResetOffset(); + //& getSubscriptionInner(); + + //& mqs); + bool getTopicSubscribeInfo(const string& topic, vector& mqs); + + void addPullRequest(MQMessageQueue mq, PullRequest* pPullRequest); + PullRequest* getPullRequest(MQMessageQueue mq); + map getPullRequestTable(); + void lockAll(); + bool lock(MQMessageQueue mq); + void unlockAll(bool oneway = false); + void unlock(MQMessageQueue mq); + +protected: + map m_subscriptionData; + + boost::mutex m_topicSubscribeInfoTableMutex; + map> m_topicSubscribeInfoTable; + typedef map MQ2PULLREQ; + MQ2PULLREQ m_requestQueueTable; + boost::mutex m_requestTableMutex; + + AllocateMQStrategy* m_pAllocateMQStrategy; + MQConsumer* m_pConsumer; + MQClientFactory* m_pClientFactory; }; //& mqAll, - vector& mqDivided); + virtual void messageQueueChanged(const string& topic, vector& mqAll, + vector& mqDivided); - virtual void removeUnnecessaryMessageQueue(const MQMessageQueue& mq); + virtual void removeUnnecessaryMessageQueue(const MQMessageQueue& mq); - virtual int64 computePullFromWhere(const MQMessageQueue& mq); + virtual int64 computePullFromWhere(const MQMessageQueue& mq); - virtual bool updateRequestTableInRebalance(const string& topic, - vector& mqsSelf); + virtual bool updateRequestTableInRebalance(const string& topic, vector& mqsSelf); }; //& mqAll, - vector& mqDivided); + virtual void messageQueueChanged(const string& topic, vector& mqAll, + vector& mqDivided); - virtual void removeUnnecessaryMessageQueue(const MQMessageQueue& mq); + virtual void removeUnnecessaryMessageQueue(const MQMessageQueue& mq); - virtual int64 computePullFromWhere(const MQMessageQueue& mq); + virtual int64 computePullFromWhere(const MQMessageQueue& mq); - virtual bool updateRequestTableInRebalance(const string& topic, - vector& mqsSelf); + virtual bool updateRequestTableInRebalance(const string& topic, vector& mqsSelf); }; //& SubscriptionData::getTagsSet() { return m_tagSet; } bool SubscriptionData::operator==(const SubscriptionData& other) const { - if (!m_subString.compare(other.m_subString)) { - return false; - } - if (m_subVersion != other.m_subVersion) { - return false; - } - if (m_tagSet.size() != other.m_tagSet.size()) { - return false; - } - if (!m_topic.compare(other.m_topic)) { - return false; - } - return true; + if (!m_subString.compare(other.m_subString)) { + return false; + } + if (m_subVersion != other.m_subVersion) { + return false; + } + if (m_tagSet.size() != other.m_tagSet.size()) { + return false; + } + if (!m_topic.compare(other.m_topic)) { + return false; + } + return true; } bool SubscriptionData::operator<(const SubscriptionData& other) const { - int ret = m_topic.compare(other.m_topic); - if (ret < 0) { - return true; - } else if (ret == 0) { - ret = m_subString.compare(other.m_subString); + int ret = m_topic.compare(other.m_topic); if (ret < 0) { - return true; + return true; + } else if (ret == 0) { + ret = m_subString.compare(other.m_subString); + if (ret < 0) { + return true; + } else { + return false; + } } else { - return false; + return false; } - } else { - return false; - } } void SubscriptionData::putCodeSet(const string& tag) { - int value = atoi(tag.c_str()); - m_codeSet.push_back(value); + int value = atoi(tag.c_str()); + m_codeSet.push_back(value); } Json::Value SubscriptionData::toJson() const { - Json::Value outJson; - outJson["subString"] = m_subString; - outJson["subVersion"] = UtilAll::to_string(m_subVersion); - outJson["topic"] = m_topic; - - { - vector::const_iterator it = m_tagSet.begin(); - for (; it != m_tagSet.end(); it++) { - outJson["tagsSet"].append(*it); + Json::Value outJson; + outJson["subString"] = m_subString; + outJson["subVersion"] = UtilAll::to_string(m_subVersion); + outJson["topic"] = m_topic; + + { + vector::const_iterator it = m_tagSet.begin(); + for (; it != m_tagSet.end(); it++) { + outJson["tagsSet"].append(*it); + } } - } - { - vector::const_iterator it = m_codeSet.begin(); - for (; it != m_codeSet.end(); it++) { - outJson["codeSet"].append(*it); + { + vector::const_iterator it = m_codeSet.begin(); + for (; it != m_codeSet.end(); it++) { + outJson["codeSet"].append(*it); + } } - } - return outJson; + return outJson; } //& getTagsSet(); - - void putCodeSet(const string& tag); - - bool operator==(const SubscriptionData& other) const; - bool operator<(const SubscriptionData& other) const; - - Json::Value toJson() const; - - private: - string m_topic; - string m_subString; - int64 m_subVersion; - vector m_tagSet; - vector m_codeSet; +public: + SubscriptionData(); + virtual ~SubscriptionData() { + m_tagSet.clear(); + m_codeSet.clear(); + } + SubscriptionData(const string& topic, const string& subString); + SubscriptionData(const SubscriptionData& other); + + const string& getTopic() const; + const string& getSubString() const; + void setSubString(const string& sub); + int64 getSubVersion() const; + + void putTagsSet(const string& tag); + bool containTag(const string& tag); + vector& getTagsSet(); + + void putCodeSet(const string& tag); + + bool operator==(const SubscriptionData& other) const; + bool operator<(const SubscriptionData& other) const; + + Json::Value toJson() const; + +private: + string m_topic; + string m_subString; + int64 m_subVersion; + vector m_tagSet; + vector m_codeSet; }; // #include "windows.h" -BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, - LPVOID lpReserved) { - switch (ul_reason_for_call) { - case DLL_PROCESS_ATTACH: - break; - case DLL_THREAD_ATTACH: - break; - case DLL_THREAD_DETACH: - break; - case DLL_PROCESS_DETACH: - break; - } - return TRUE; +BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { + switch (ul_reason_for_call) { + case DLL_PROCESS_ATTACH: + break; + case DLL_THREAD_ATTACH: + break; + case DLL_THREAD_DETACH: + break; + case DLL_PROCESS_DETACH: + break; + } + return TRUE; } diff --git a/src/extern/CMessage.cpp b/src/extern/CMessage.cpp index 4106c274c..915ed8459 100644 --- a/src/extern/CMessage.cpp +++ b/src/extern/CMessage.cpp @@ -31,7 +31,7 @@ CMessage *CreateMessage(const char *topic) { if (topic != NULL) { mqMessage->setTopic(topic); } - return (CMessage *) mqMessage; + return (CMessage *)mqMessage; } int DestroyMessage(CMessage *msg) { if (msg == NULL) { @@ -72,17 +72,17 @@ int SetByteMessageBody(CMessage *msg, const char *body, int len) { if (msg == NULL) { return NULL_POINTER; } - ((MQMessage *)msg)->setBody(body,len); + ((MQMessage *)msg)->setBody(body, len); return OK; } int SetMessageProperty(CMessage *msg, const char *key, const char *value) { if (msg == NULL) { return NULL_POINTER; } - ((MQMessage *)msg)->setProperty(key,value); + ((MQMessage *)msg)->setProperty(key, value); return OK; } -int SetDelayTimeLevel(CMessage *msg, int level){ +int SetDelayTimeLevel(CMessage *msg, int level) { if (msg == NULL) { return NULL_POINTER; } diff --git a/src/extern/CMessageExt.cpp b/src/extern/CMessageExt.cpp index 5d612d84b..8bcb86bf0 100644 --- a/src/extern/CMessageExt.cpp +++ b/src/extern/CMessageExt.cpp @@ -27,100 +27,100 @@ const char *GetMessageTopic(CMessageExt *msg) { if (msg == NULL) { return NULL; } - return ((MQMessageExt *) msg)->getTopic().c_str(); + return ((MQMessageExt *)msg)->getTopic().c_str(); } const char *GetMessageTags(CMessageExt *msg) { if (msg == NULL) { return NULL; } - return ((MQMessageExt *) msg)->getTags().c_str(); + return ((MQMessageExt *)msg)->getTags().c_str(); } const char *GetMessageKeys(CMessageExt *msg) { if (msg == NULL) { return NULL; } - return ((MQMessageExt *) msg)->getKeys().c_str(); + return ((MQMessageExt *)msg)->getKeys().c_str(); } const char *GetMessageBody(CMessageExt *msg) { if (msg == NULL) { return NULL; } - return ((MQMessageExt *) msg)->getBody().c_str(); + return ((MQMessageExt *)msg)->getBody().c_str(); } const char *GetMessageProperty(CMessageExt *msg, const char *key) { if (msg == NULL) { return NULL; } - return ((MQMessageExt *) msg)->getProperty(key).c_str(); + return ((MQMessageExt *)msg)->getProperty(key).c_str(); } const char *GetMessageId(CMessageExt *msg) { if (msg == NULL) { return NULL; } - return ((MQMessageExt *) msg)->getMsgId().c_str(); + return ((MQMessageExt *)msg)->getMsgId().c_str(); } -int GetMessageDelayTimeLevel(CMessageExt *msg){ +int GetMessageDelayTimeLevel(CMessageExt *msg) { if (msg == NULL) { return NULL_POINTER; } - return ((MQMessageExt *) msg)->getDelayTimeLevel(); + return ((MQMessageExt *)msg)->getDelayTimeLevel(); } -int GetMessageQueueId(CMessageExt *msg){ +int GetMessageQueueId(CMessageExt *msg) { if (msg == NULL) { return NULL_POINTER; } - return ((MQMessageExt *) msg)->getQueueId(); + return ((MQMessageExt *)msg)->getQueueId(); } -int GetMessageReconsumeTimes(CMessageExt *msg){ +int GetMessageReconsumeTimes(CMessageExt *msg) { if (msg == NULL) { return NULL_POINTER; } - return ((MQMessageExt *) msg)->getReconsumeTimes(); + return ((MQMessageExt *)msg)->getReconsumeTimes(); } -int GetMessageStoreSize(CMessageExt *msg){ +int GetMessageStoreSize(CMessageExt *msg) { if (msg == NULL) { return NULL_POINTER; } - return ((MQMessageExt *) msg)->getStoreSize(); + return ((MQMessageExt *)msg)->getStoreSize(); } long long GetMessageBornTimestamp(CMessageExt *msg) { if (msg == NULL) { return NULL_POINTER; } - return ((MQMessageExt *) msg)->getBornTimestamp(); + return ((MQMessageExt *)msg)->getBornTimestamp(); } -long long GetMessageStoreTimestamp(CMessageExt *msg){ +long long GetMessageStoreTimestamp(CMessageExt *msg) { if (msg == NULL) { return NULL_POINTER; } - return ((MQMessageExt *) msg)->getStoreTimestamp(); + return ((MQMessageExt *)msg)->getStoreTimestamp(); } -long long GetMessageQueueOffset(CMessageExt *msg){ +long long GetMessageQueueOffset(CMessageExt *msg) { if (msg == NULL) { return NULL_POINTER; } - return ((MQMessageExt *) msg)->getQueueOffset(); + return ((MQMessageExt *)msg)->getQueueOffset(); } -long long GetMessageCommitLogOffset(CMessageExt *msg){ +long long GetMessageCommitLogOffset(CMessageExt *msg) { if (msg == NULL) { return NULL_POINTER; } - return ((MQMessageExt *) msg)->getCommitLogOffset(); + return ((MQMessageExt *)msg)->getCommitLogOffset(); } -long long GetMessagePreparedTransactionOffset(CMessageExt *msg){ +long long GetMessagePreparedTransactionOffset(CMessageExt *msg) { if (msg == NULL) { return NULL_POINTER; } - return ((MQMessageExt *) msg)->getPreparedTransactionOffset(); + return ((MQMessageExt *)msg)->getPreparedTransactionOffset(); } #ifdef __cplusplus }; diff --git a/src/extern/CProducer.cpp b/src/extern/CProducer.cpp index 58542a68f..f7865e04e 100644 --- a/src/extern/CProducer.cpp +++ b/src/extern/CProducer.cpp @@ -33,14 +33,11 @@ using namespace std; class SelectMessageQueue : public MessageQueueSelector { public: - SelectMessageQueue(QueueSelectorCallback callback) { - m_pCallback = callback; - } + SelectMessageQueue(QueueSelectorCallback callback) { m_pCallback = callback; } - MQMessageQueue select(const std::vector &mqs, - const MQMessage &msg, void *arg) { - CMessage *message = (CMessage *) &msg; - //Get the index of sending MQMessageQueue through callback function. + MQMessageQueue select(const std::vector &mqs, const MQMessage &msg, void *arg) { + CMessage *message = (CMessage *)&msg; + // Get the index of sending MQMessageQueue through callback function. int index = m_pCallback(mqs.size(), message, arg); return mqs[index]; } @@ -49,29 +46,30 @@ class SelectMessageQueue : public MessageQueueSelector { QueueSelectorCallback m_pCallback; }; -class CSendCallback : public AutoDeleteSendCallBack{ +class CSendCallback : public AutoDeleteSendCallBack { public: - CSendCallback(CSendSuccessCallback cSendSuccessCallback,CSendExceptionCallback cSendExceptionCallback){ + CSendCallback(CSendSuccessCallback cSendSuccessCallback, CSendExceptionCallback cSendExceptionCallback) { m_cSendSuccessCallback = cSendSuccessCallback; m_cSendExceptionCallback = cSendExceptionCallback; } - virtual ~CSendCallback(){} - virtual void onSuccess(SendResult& sendResult) { + virtual ~CSendCallback() {} + virtual void onSuccess(SendResult &sendResult) { CSendResult result; - result.sendStatus = CSendStatus((int) sendResult.getSendStatus()); + result.sendStatus = CSendStatus((int)sendResult.getSendStatus()); result.offset = sendResult.getQueueOffset(); strncpy(result.msgId, sendResult.getMsgId().c_str(), MAX_MESSAGE_ID_LENGTH - 1); result.msgId[MAX_MESSAGE_ID_LENGTH - 1] = 0; m_cSendSuccessCallback(result); } - virtual void onException(MQException& e) { + virtual void onException(MQException &e) { CMQException exception; exception.error = e.GetError(); - exception.line = e.GetLine(); + exception.line = e.GetLine(); strncpy(exception.msg, e.what(), MAX_EXEPTION_MSG_LENGTH - 1); strncpy(exception.file, e.GetFile(), MAX_EXEPTION_FILE_LENGTH - 1); - m_cSendExceptionCallback( exception ); + m_cSendExceptionCallback(exception); } + private: CSendSuccessCallback m_cSendSuccessCallback; CSendExceptionCallback m_cSendExceptionCallback; @@ -82,13 +80,13 @@ CProducer *CreateProducer(const char *groupId) { return NULL; } DefaultMQProducer *defaultMQProducer = new DefaultMQProducer(groupId); - return (CProducer *) defaultMQProducer; + return (CProducer *)defaultMQProducer; } int DestroyProducer(CProducer *pProducer) { if (pProducer == NULL) { return NULL_POINTER; } - delete reinterpret_cast(pProducer); + delete reinterpret_cast(pProducer); return OK; } int StartProducer(CProducer *producer) { @@ -96,8 +94,9 @@ int StartProducer(CProducer *producer) { return NULL_POINTER; } try { - ((DefaultMQProducer *) producer)->start(); - } catch (exception &e) { + ((DefaultMQProducer *)producer)->start(); + } + catch (exception &e) { return PRODUCER_START_FAILED; } return OK; @@ -106,31 +105,31 @@ int ShutdownProducer(CProducer *producer) { if (producer == NULL) { return NULL_POINTER; } - ((DefaultMQProducer *) producer)->shutdown(); + ((DefaultMQProducer *)producer)->shutdown(); return OK; } int SetProducerNameServerAddress(CProducer *producer, const char *namesrv) { if (producer == NULL) { return NULL_POINTER; } - ((DefaultMQProducer *) producer)->setNamesrvAddr(namesrv); + ((DefaultMQProducer *)producer)->setNamesrvAddr(namesrv); return OK; } int SetProducerNameServerDomain(CProducer *producer, const char *domain) { if (producer == NULL) { return NULL_POINTER; } - ((DefaultMQProducer *) producer)->setNamesrvDomain(domain); + ((DefaultMQProducer *)producer)->setNamesrvDomain(domain); return OK; } int SendMessageSync(CProducer *producer, CMessage *msg, CSendResult *result) { - //CSendResult sendResult; + // CSendResult sendResult; if (producer == NULL || msg == NULL || result == NULL) { return NULL_POINTER; } try { - DefaultMQProducer *defaultMQProducer = (DefaultMQProducer *) producer; - MQMessage *message = (MQMessage *) msg; + DefaultMQProducer *defaultMQProducer = (DefaultMQProducer *)producer; + MQMessage *message = (MQMessage *)msg; SendResult sendResult = defaultMQProducer->send(*message); switch (sendResult.getSendStatus()) { case SEND_OK: @@ -152,27 +151,30 @@ int SendMessageSync(CProducer *producer, CMessage *msg, CSendResult *result) { result->offset = sendResult.getQueueOffset(); strncpy(result->msgId, sendResult.getMsgId().c_str(), MAX_MESSAGE_ID_LENGTH - 1); result->msgId[MAX_MESSAGE_ID_LENGTH - 1] = 0; - } catch (exception &e) { + } + catch (exception &e) { return PRODUCER_SEND_SYNC_FAILED; } return OK; } -int SendMessageAsync(CProducer *producer, CMessage *msg, CSendSuccessCallback cSendSuccessCallback, CSendExceptionCallback cSendExceptionCallback){ +int SendMessageAsync(CProducer *producer, CMessage *msg, CSendSuccessCallback cSendSuccessCallback, + CSendExceptionCallback cSendExceptionCallback) { if (producer == NULL || msg == NULL || cSendSuccessCallback == NULL || cSendExceptionCallback == NULL) { return NULL_POINTER; } - DefaultMQProducer *defaultMQProducer = (DefaultMQProducer *) producer; - MQMessage *message = (MQMessage *) msg; - CSendCallback* cSendCallback = new CSendCallback(cSendSuccessCallback , cSendExceptionCallback); + DefaultMQProducer *defaultMQProducer = (DefaultMQProducer *)producer; + MQMessage *message = (MQMessage *)msg; + CSendCallback *cSendCallback = new CSendCallback(cSendSuccessCallback, cSendExceptionCallback); try { - defaultMQProducer->send(*message ,cSendCallback); - } catch (exception &e) { - if(cSendCallback != NULL){ - if(typeid(e) == typeid( MQException )){ + defaultMQProducer->send(*message, cSendCallback); + } + catch (exception &e) { + if (cSendCallback != NULL) { + if (typeid(e) == typeid(MQException)) { MQException &mqe = (MQException &)e; - cSendCallback->onException( mqe ); + cSendCallback->onException(mqe); } delete cSendCallback; cSendCallback = NULL; @@ -186,79 +188,74 @@ int SendMessageOneway(CProducer *producer, CMessage *msg) { if (producer == NULL || msg == NULL) { return NULL_POINTER; } - DefaultMQProducer *defaultMQProducer = (DefaultMQProducer *) producer; - MQMessage *message = (MQMessage *) msg; + DefaultMQProducer *defaultMQProducer = (DefaultMQProducer *)producer; + MQMessage *message = (MQMessage *)msg; try { defaultMQProducer->sendOneway(*message); - } catch (exception &e) { + } + catch (exception &e) { return PRODUCER_SEND_ONEWAY_FAILED; } return OK; } -int SendMessageOnewayOrderly(CProducer *producer, CMessage *msg, QueueSelectorCallback selector, void* arg) { +int SendMessageOnewayOrderly(CProducer *producer, CMessage *msg, QueueSelectorCallback selector, void *arg) { if (producer == NULL || msg == NULL) { return NULL_POINTER; } - DefaultMQProducer *defaultMQProducer = (DefaultMQProducer *) producer; - MQMessage *message = (MQMessage *) msg; + DefaultMQProducer *defaultMQProducer = (DefaultMQProducer *)producer; + MQMessage *message = (MQMessage *)msg; try { SelectMessageQueue selectMessageQueue(selector); defaultMQProducer->sendOneway(*message, &selectMessageQueue, arg); - } catch (exception &e) { + } + catch (exception &e) { return PRODUCER_SEND_ONEWAY_FAILED; } return OK; } -int -SendMessageOrderlyAsync(CProducer *producer, - CMessage *msg, - QueueSelectorCallback callback, - void *arg, - CSendSuccessCallback cSendSuccessCallback, - CSendExceptionCallback cSendExceptionCallback - ) { - if (producer == NULL || msg == NULL || callback == NULL || cSendSuccessCallback == NULL || cSendExceptionCallback == NULL) { +int SendMessageOrderlyAsync(CProducer *producer, CMessage *msg, QueueSelectorCallback callback, void *arg, + CSendSuccessCallback cSendSuccessCallback, CSendExceptionCallback cSendExceptionCallback) { + if (producer == NULL || msg == NULL || callback == NULL || cSendSuccessCallback == NULL || + cSendExceptionCallback == NULL) { return NULL_POINTER; } - DefaultMQProducer *defaultMQProducer = (DefaultMQProducer *) producer; - MQMessage *message = (MQMessage *) msg; - CSendCallback* cSendCallback = new CSendCallback(cSendSuccessCallback , cSendExceptionCallback); + DefaultMQProducer *defaultMQProducer = (DefaultMQProducer *)producer; + MQMessage *message = (MQMessage *)msg; + CSendCallback *cSendCallback = new CSendCallback(cSendSuccessCallback, cSendExceptionCallback); try { - //Constructing SelectMessageQueue objects through function pointer callback + // Constructing SelectMessageQueue objects through function pointer callback SelectMessageQueue selectMessageQueue(callback); - defaultMQProducer->send(*message, - &selectMessageQueue, arg,cSendCallback); - } catch (exception &e) { - printf("%s\n",e.what()); - //std::count<send(*message, &selectMessageQueue, arg, cSendCallback); + } + catch (exception &e) { + printf("%s\n", e.what()); + // std::count<send(*message, &selectMessageQueue, arg, autoRetryTimes); - //Convert SendStatus to CSendStatus - result->sendStatus = CSendStatus((int) sendResult.getSendStatus()); + // Convert SendStatus to CSendStatus + result->sendStatus = CSendStatus((int)sendResult.getSendStatus()); result->offset = sendResult.getQueueOffset(); strncpy(result->msgId, sendResult.getMsgId().c_str(), MAX_MESSAGE_ID_LENGTH - 1); result->msgId[MAX_MESSAGE_ID_LENGTH - 1] = 0; - } catch (exception &e) { + } + catch (exception &e) { return PRODUCER_SEND_ORDERLY_FAILED; } return OK; @@ -268,14 +265,14 @@ int SetProducerGroupName(CProducer *producer, const char *groupName) { if (producer == NULL) { return NULL_POINTER; } - ((DefaultMQProducer *) producer)->setGroupName(groupName); + ((DefaultMQProducer *)producer)->setGroupName(groupName); return OK; } int SetProducerInstanceName(CProducer *producer, const char *instanceName) { if (producer == NULL) { return NULL_POINTER; } - ((DefaultMQProducer *) producer)->setInstanceName(instanceName); + ((DefaultMQProducer *)producer)->setInstanceName(instanceName); return OK; } int SetProducerSessionCredentials(CProducer *producer, const char *accessKey, const char *secretKey, @@ -283,14 +280,14 @@ int SetProducerSessionCredentials(CProducer *producer, const char *accessKey, co if (producer == NULL) { return NULL_POINTER; } - ((DefaultMQProducer *) producer)->setSessionCredentials(accessKey, secretKey, onsChannel); + ((DefaultMQProducer *)producer)->setSessionCredentials(accessKey, secretKey, onsChannel); return OK; } int SetProducerLogPath(CProducer *producer, const char *logPath) { if (producer == NULL) { return NULL_POINTER; } - //Todo, This api should be implemented by core api. + // Todo, This api should be implemented by core api. //((DefaultMQProducer *) producer)->setLogFileSizeAndNum(3, 102400000); return OK; } @@ -299,7 +296,7 @@ int SetProducerLogFileNumAndSize(CProducer *producer, int fileNum, long fileSize if (producer == NULL) { return NULL_POINTER; } - ((DefaultMQProducer *) producer)->setLogFileSizeAndNum(fileNum, fileSize); + ((DefaultMQProducer *)producer)->setLogFileSizeAndNum(fileNum, fileSize); return OK; } @@ -307,7 +304,7 @@ int SetProducerLogLevel(CProducer *producer, CLogLevel level) { if (producer == NULL) { return NULL_POINTER; } - ((DefaultMQProducer *) producer)->setLogLevel((elogLevel) level); + ((DefaultMQProducer *)producer)->setLogLevel((elogLevel)level); return OK; } @@ -315,7 +312,7 @@ int SetProducerSendMsgTimeout(CProducer *producer, int timeout) { if (producer == NULL) { return NULL_POINTER; } - ((DefaultMQProducer *) producer)->setSendMsgTimeout(timeout); + ((DefaultMQProducer *)producer)->setSendMsgTimeout(timeout); return OK; } @@ -323,7 +320,7 @@ int SetProducerCompressMsgBodyOverHowmuch(CProducer *producer, int howmuch) { if (producer == NULL) { return NULL_POINTER; } - ((DefaultMQProducer *) producer)->setCompressMsgBodyOverHowmuch(howmuch); + ((DefaultMQProducer *)producer)->setCompressMsgBodyOverHowmuch(howmuch); return OK; } @@ -331,7 +328,7 @@ int SetProducerCompressLevel(CProducer *producer, int level) { if (producer == NULL) { return NULL_POINTER; } - ((DefaultMQProducer *) producer)->setCompressLevel(level); + ((DefaultMQProducer *)producer)->setCompressLevel(level); return OK; } @@ -339,7 +336,7 @@ int SetProducerMaxMessageSize(CProducer *producer, int size) { if (producer == NULL) { return NULL_POINTER; } - ((DefaultMQProducer *) producer)->setMaxMessageSize(size); + ((DefaultMQProducer *)producer)->setMaxMessageSize(size); return OK; } #ifdef __cplusplus diff --git a/src/extern/CPullConsumer.cpp b/src/extern/CPullConsumer.cpp index cf7c01f5f..6503570d7 100644 --- a/src/extern/CPullConsumer.cpp +++ b/src/extern/CPullConsumer.cpp @@ -27,19 +27,18 @@ using namespace std; extern "C" { #endif - CPullConsumer *CreatePullConsumer(const char *groupId) { if (groupId == NULL) { return NULL; } DefaultMQPullConsumer *defaultMQPullConsumer = new DefaultMQPullConsumer(groupId); - return (CPullConsumer *) defaultMQPullConsumer; + return (CPullConsumer *)defaultMQPullConsumer; } int DestroyPullConsumer(CPullConsumer *consumer) { if (consumer == NULL) { return NULL_POINTER; } - delete reinterpret_cast(consumer); + delete reinterpret_cast(consumer); return OK; } int StartPullConsumer(CPullConsumer *consumer) { @@ -47,8 +46,9 @@ int StartPullConsumer(CPullConsumer *consumer) { return NULL_POINTER; } try { - ((DefaultMQPullConsumer *) consumer)->start(); - } catch (exception &e) { + ((DefaultMQPullConsumer *)consumer)->start(); + } + catch (exception &e) { return PULLCONSUMER_START_FAILED; } return OK; @@ -57,34 +57,34 @@ int ShutdownPullConsumer(CPullConsumer *consumer) { if (consumer == NULL) { return NULL_POINTER; } - ((DefaultMQPullConsumer *) consumer)->shutdown(); + ((DefaultMQPullConsumer *)consumer)->shutdown(); return OK; } int SetPullConsumerGroupID(CPullConsumer *consumer, const char *groupId) { if (consumer == NULL || groupId == NULL) { return NULL_POINTER; } - ((DefaultMQPullConsumer *) consumer)->setGroupName(groupId); + ((DefaultMQPullConsumer *)consumer)->setGroupName(groupId); return OK; } const char *GetPullConsumerGroupID(CPullConsumer *consumer) { if (consumer == NULL) { return NULL; } - return ((DefaultMQPullConsumer *) consumer)->getGroupName().c_str(); + return ((DefaultMQPullConsumer *)consumer)->getGroupName().c_str(); } int SetPullConsumerNameServerAddress(CPullConsumer *consumer, const char *namesrv) { if (consumer == NULL) { return NULL_POINTER; } - ((DefaultMQPullConsumer *) consumer)->setNamesrvAddr(namesrv); + ((DefaultMQPullConsumer *)consumer)->setNamesrvAddr(namesrv); return OK; } int SetPullConsumerNameServerDomain(CPullConsumer *consumer, const char *domain) { if (consumer == NULL) { return NULL_POINTER; } - ((DefaultMQPullConsumer *) consumer)->setNamesrvDomain(domain); + ((DefaultMQPullConsumer *)consumer)->setNamesrvDomain(domain); return OK; } int SetPullConsumerSessionCredentials(CPullConsumer *consumer, const char *accessKey, const char *secretKey, @@ -92,7 +92,7 @@ int SetPullConsumerSessionCredentials(CPullConsumer *consumer, const char *acces if (consumer == NULL) { return NULL_POINTER; } - ((DefaultMQPullConsumer *) consumer)->setSessionCredentials(accessKey, secretKey, channel); + ((DefaultMQPullConsumer *)consumer)->setSessionCredentials(accessKey, secretKey, channel); return OK; } @@ -100,7 +100,7 @@ int SetPullConsumerLogPath(CPullConsumer *consumer, const char *logPath) { if (consumer == NULL) { return NULL_POINTER; } - //Todo, This api should be implemented by core api. + // Todo, This api should be implemented by core api. //((DefaultMQPullConsumer *) consumer)->setInstanceName(instanceName); return OK; } @@ -109,7 +109,7 @@ int SetPullConsumerLogFileNumAndSize(CPullConsumer *consumer, int fileNum, long if (consumer == NULL) { return NULL_POINTER; } - ((DefaultMQPullConsumer *) consumer)->setLogFileSizeAndNum(fileNum, fileSize); + ((DefaultMQPullConsumer *)consumer)->setLogFileSizeAndNum(fileNum, fileSize); return OK; } @@ -117,7 +117,7 @@ int SetPullConsumerLogLevel(CPullConsumer *consumer, CLogLevel level) { if (consumer == NULL) { return NULL_POINTER; } - ((DefaultMQPullConsumer *) consumer)->setLogLevel((elogLevel) level); + ((DefaultMQPullConsumer *)consumer)->setLogLevel((elogLevel)level); return OK; } @@ -129,11 +129,11 @@ int FetchSubscriptionMessageQueues(CPullConsumer *consumer, const char *topic, C CMessageQueue *temMQ = NULL; std::vector fullMQ; try { - ((DefaultMQPullConsumer *) consumer)->fetchSubscribeMessageQueues(topic, fullMQ); + ((DefaultMQPullConsumer *)consumer)->fetchSubscribeMessageQueues(topic, fullMQ); *size = fullMQ.size(); - //Alloc memory to save the pointer to CPP MessageQueue, and the MessageQueues may be changed. - //Thus, this memory should be released by users using @ReleaseSubscribeMessageQueue every time. - temMQ = (CMessageQueue *) malloc(*size * sizeof(CMessageQueue)); + // Alloc memory to save the pointer to CPP MessageQueue, and the MessageQueues may be changed. + // Thus, this memory should be released by users using @ReleaseSubscribeMessageQueue every time. + temMQ = (CMessageQueue *)malloc(*size * sizeof(CMessageQueue)); if (temMQ == NULL) { *size = 0; *mqs = NULL; @@ -146,7 +146,8 @@ int FetchSubscriptionMessageQueues(CPullConsumer *consumer, const char *topic, C temMQ[index].queueId = iter->getQueueId(); } *mqs = temMQ; - } catch (MQException &e) { + } + catch (MQException &e) { *size = 0; *mqs = NULL; return PULLCONSUMER_FETCH_MQ_FAILED; @@ -157,23 +158,24 @@ int ReleaseSubscriptionMessageQueue(CMessageQueue *mqs) { if (mqs == NULL) { return NULL_POINTER; } - free((void *) mqs); + free((void *)mqs); mqs = NULL; return OK; } -CPullResult -Pull(CPullConsumer *consumer, const CMessageQueue *mq, const char *subExpression, long long offset, int maxNums) { +CPullResult Pull(CPullConsumer *consumer, const CMessageQueue *mq, const char *subExpression, long long offset, + int maxNums) { CPullResult pullResult; memset(&pullResult, 0, sizeof(CPullResult)); MQMessageQueue messageQueue(mq->topic, mq->brokerName, mq->queueId); PullResult cppPullResult; try { - cppPullResult = ((DefaultMQPullConsumer *) consumer)->pull(messageQueue, subExpression, offset, maxNums); - } catch (exception &e) { + cppPullResult = ((DefaultMQPullConsumer *)consumer)->pull(messageQueue, subExpression, offset, maxNums); + } + catch (exception &e) { cppPullResult.pullStatus = BROKER_TIMEOUT; } - if(cppPullResult.pullStatus != BROKER_TIMEOUT){ + if (cppPullResult.pullStatus != BROKER_TIMEOUT) { pullResult.maxOffset = cppPullResult.maxOffset; pullResult.minOffset = cppPullResult.minOffset; pullResult.nextBeginOffset = cppPullResult.nextBeginOffset; @@ -185,12 +187,12 @@ Pull(CPullConsumer *consumer, const CMessageQueue *mq, const char *subExpression pullResult.size = cppPullResult.msgFoundList.size(); PullResult *tmpPullResult = new PullResult(cppPullResult); pullResult.pData = tmpPullResult; - //Alloc memory to save the pointer to CPP MQMessageExt, which will be release by the CPP SDK core. - //Thus, this memory should be released by users using @ReleasePullResult - pullResult.msgFoundList = (CMessageExt **) malloc(pullResult.size * sizeof(CMessageExt *)); + // Alloc memory to save the pointer to CPP MQMessageExt, which will be release by the CPP SDK core. + // Thus, this memory should be released by users using @ReleasePullResult + pullResult.msgFoundList = (CMessageExt **)malloc(pullResult.size * sizeof(CMessageExt *)); for (size_t i = 0; i < cppPullResult.msgFoundList.size(); i++) { MQMessageExt *msg = const_cast(&tmpPullResult->msgFoundList[i]); - pullResult.msgFoundList[i] = (CMessageExt *) (msg); + pullResult.msgFoundList[i] = (CMessageExt *)(msg); } break; } @@ -213,7 +215,6 @@ Pull(CPullConsumer *consumer, const CMessageQueue *mq, const char *subExpression default: pullResult.pullStatus = E_NO_NEW_MSG; break; - } return pullResult; } @@ -223,12 +224,13 @@ int ReleasePullResult(CPullResult pullResult) { } if (pullResult.pData != NULL) { try { - delete ((PullResult *) pullResult.pData); - } catch (exception &e) { + delete ((PullResult *)pullResult.pData); + } + catch (exception &e) { return NULL_POINTER; } } - free((void *) pullResult.msgFoundList); + free((void *)pullResult.msgFoundList); pullResult.msgFoundList = NULL; return OK; } diff --git a/src/extern/CPushConsumer.cpp b/src/extern/CPushConsumer.cpp index 2c35c7491..6ed09e2de 100644 --- a/src/extern/CPushConsumer.cpp +++ b/src/extern/CPushConsumer.cpp @@ -36,15 +36,14 @@ class MessageListenerInner : public MessageListenerConcurrently { ~MessageListenerInner() {} ConsumeStatus consumeMessage(const std::vector &msgs) { - //to do user call back + // to do user call back if (m_pMsgReceiveCallback == NULL) { return RECONSUME_LATER; } for (size_t i = 0; i < msgs.size(); ++i) { MQMessageExt *msg = const_cast(&msgs[i]); - CMessageExt *message = (CMessageExt *) (msg); - if (m_pMsgReceiveCallback(m_pconsumer, message) != E_CONSUME_SUCCESS) - return RECONSUME_LATER; + CMessageExt *message = (CMessageExt *)(msg); + if (m_pMsgReceiveCallback(m_pconsumer, message) != E_CONSUME_SUCCESS) return RECONSUME_LATER; } return CONSUME_SUCCESS; } @@ -67,9 +66,8 @@ class MessageListenerOrderlyInner : public MessageListenerOrderly { } for (size_t i = 0; i < msgs.size(); ++i) { MQMessageExt *msg = const_cast(&msgs[i]); - CMessageExt *message = (CMessageExt *) (msg); - if (m_pMsgReceiveCallback(m_pconsumer, message) != E_CONSUME_SUCCESS) - return RECONSUME_LATER; + CMessageExt *message = (CMessageExt *)(msg); + if (m_pMsgReceiveCallback(m_pconsumer, message) != E_CONSUME_SUCCESS) return RECONSUME_LATER; } return CONSUME_SUCCESS; } @@ -85,20 +83,19 @@ map g_OrderListenerMap; extern "C" { #endif - CPushConsumer *CreatePushConsumer(const char *groupId) { if (groupId == NULL) { return NULL; } DefaultMQPushConsumer *defaultMQPushConsumer = new DefaultMQPushConsumer(groupId); defaultMQPushConsumer->setConsumeFromWhere(CONSUME_FROM_LAST_OFFSET); - return (CPushConsumer *) defaultMQPushConsumer; + return (CPushConsumer *)defaultMQPushConsumer; } int DestroyPushConsumer(CPushConsumer *consumer) { if (consumer == NULL) { return NULL_POINTER; } - delete reinterpret_cast(consumer); + delete reinterpret_cast(consumer); return OK; } int StartPushConsumer(CPushConsumer *consumer) { @@ -106,8 +103,9 @@ int StartPushConsumer(CPushConsumer *consumer) { return NULL_POINTER; } try { - ((DefaultMQPushConsumer *) consumer)->start(); - } catch (exception &e) { + ((DefaultMQPushConsumer *)consumer)->start(); + } + catch (exception &e) { return PUSHCONSUMER_START_FAILED; } return OK; @@ -116,41 +114,41 @@ int ShutdownPushConsumer(CPushConsumer *consumer) { if (consumer == NULL) { return NULL_POINTER; } - ((DefaultMQPushConsumer *) consumer)->shutdown(); + ((DefaultMQPushConsumer *)consumer)->shutdown(); return OK; } int SetPushConsumerGroupID(CPushConsumer *consumer, const char *groupId) { if (consumer == NULL || groupId == NULL) { return NULL_POINTER; } - ((DefaultMQPushConsumer *) consumer)->setGroupName(groupId); + ((DefaultMQPushConsumer *)consumer)->setGroupName(groupId); return OK; } const char *GetPushConsumerGroupID(CPushConsumer *consumer) { if (consumer == NULL) { return NULL; } - return ((DefaultMQPushConsumer *) consumer)->getGroupName().c_str(); + return ((DefaultMQPushConsumer *)consumer)->getGroupName().c_str(); } int SetPushConsumerNameServerAddress(CPushConsumer *consumer, const char *namesrv) { if (consumer == NULL) { return NULL_POINTER; } - ((DefaultMQPushConsumer *) consumer)->setNamesrvAddr(namesrv); + ((DefaultMQPushConsumer *)consumer)->setNamesrvAddr(namesrv); return OK; } int SetPushConsumerNameServerDomain(CPushConsumer *consumer, const char *domain) { if (consumer == NULL) { return NULL_POINTER; } - ((DefaultMQPushConsumer *) consumer)->setNamesrvDomain(domain); + ((DefaultMQPushConsumer *)consumer)->setNamesrvDomain(domain); return OK; } int Subscribe(CPushConsumer *consumer, const char *topic, const char *expression) { if (consumer == NULL) { return NULL_POINTER; } - ((DefaultMQPushConsumer *) consumer)->subscribe(topic, expression); + ((DefaultMQPushConsumer *)consumer)->subscribe(topic, expression); return OK; } @@ -159,7 +157,7 @@ int RegisterMessageCallback(CPushConsumer *consumer, MessageCallBack pCallback) return NULL_POINTER; } MessageListenerInner *listenerInner = new MessageListenerInner(consumer, pCallback); - ((DefaultMQPushConsumer *) consumer)->registerMessageListener(listenerInner); + ((DefaultMQPushConsumer *)consumer)->registerMessageListener(listenerInner); g_ListenerMap[consumer] = listenerInner; return OK; } @@ -169,12 +167,11 @@ int RegisterMessageCallbackOrderly(CPushConsumer *consumer, MessageCallBack pCal return NULL_POINTER; } MessageListenerOrderlyInner *messageListenerOrderlyInner = new MessageListenerOrderlyInner(consumer, pCallback); - ((DefaultMQPushConsumer *) consumer)->registerMessageListener(messageListenerOrderlyInner); + ((DefaultMQPushConsumer *)consumer)->registerMessageListener(messageListenerOrderlyInner); g_OrderListenerMap[consumer] = messageListenerOrderlyInner; return OK; } - int UnregisterMessageCallbackOrderly(CPushConsumer *consumer) { if (consumer == NULL) { return NULL_POINTER; @@ -212,21 +209,21 @@ int SetPushConsumerMessageModel(CPushConsumer *consumer, CMessageModel messageMo if (consumer == NULL) { return NULL_POINTER; } - ((DefaultMQPushConsumer *) consumer)->setMessageModel(MessageModel((int) messageModel)); + ((DefaultMQPushConsumer *)consumer)->setMessageModel(MessageModel((int)messageModel)); return OK; } int SetPushConsumerThreadCount(CPushConsumer *consumer, int threadCount) { if (consumer == NULL || threadCount == 0) { return NULL_POINTER; } - ((DefaultMQPushConsumer *) consumer)->setConsumeThreadCount(threadCount); + ((DefaultMQPushConsumer *)consumer)->setConsumeThreadCount(threadCount); return OK; } int SetPushConsumerMessageBatchMaxSize(CPushConsumer *consumer, int batchSize) { if (consumer == NULL || batchSize == 0) { return NULL_POINTER; } - ((DefaultMQPushConsumer *) consumer)->setConsumeMessageBatchMaxSize(batchSize); + ((DefaultMQPushConsumer *)consumer)->setConsumeMessageBatchMaxSize(batchSize); return OK; } @@ -234,7 +231,7 @@ int SetPushConsumerInstanceName(CPushConsumer *consumer, const char *instanceNam if (consumer == NULL) { return NULL_POINTER; } - ((DefaultMQPushConsumer *) consumer)->setInstanceName(instanceName); + ((DefaultMQPushConsumer *)consumer)->setInstanceName(instanceName); return OK; } @@ -243,7 +240,7 @@ int SetPushConsumerSessionCredentials(CPushConsumer *consumer, const char *acces if (consumer == NULL) { return NULL_POINTER; } - ((DefaultMQPushConsumer *) consumer)->setSessionCredentials(accessKey, secretKey, channel); + ((DefaultMQPushConsumer *)consumer)->setSessionCredentials(accessKey, secretKey, channel); return OK; } @@ -251,7 +248,7 @@ int SetPushConsumerLogPath(CPushConsumer *consumer, const char *logPath) { if (consumer == NULL) { return NULL_POINTER; } - //Todo, This api should be implemented by core api. + // Todo, This api should be implemented by core api. //((DefaultMQPushConsumer *) consumer)->setInstanceName(instanceName); return OK; } @@ -260,7 +257,7 @@ int SetPushConsumerLogFileNumAndSize(CPushConsumer *consumer, int fileNum, long if (consumer == NULL) { return NULL_POINTER; } - ((DefaultMQPushConsumer *) consumer)->setLogFileSizeAndNum(fileNum, fileSize); + ((DefaultMQPushConsumer *)consumer)->setLogFileSizeAndNum(fileNum, fileSize); return OK; } @@ -268,7 +265,7 @@ int SetPushConsumerLogLevel(CPushConsumer *consumer, CLogLevel level) { if (consumer == NULL) { return NULL_POINTER; } - ((DefaultMQPushConsumer *) consumer)->setLogLevel((elogLevel) level); + ((DefaultMQPushConsumer *)consumer)->setLogLevel((elogLevel)level); return OK; } diff --git a/src/extern/CSendResult.cpp b/src/extern/CSendResult.cpp index d4cae8541..8d4dc2bc8 100644 --- a/src/extern/CSendResult.cpp +++ b/src/extern/CSendResult.cpp @@ -21,7 +21,6 @@ extern "C" { #endif - #ifdef __cplusplus }; #endif diff --git a/src/log/Logging.cpp b/src/log/Logging.cpp old mode 100755 new mode 100644 index 0b94416af..8846a5f98 --- a/src/log/Logging.cpp +++ b/src/log/Logging.cpp @@ -26,89 +26,75 @@ boost::mutex logAdapter::m_imtx; logAdapter::~logAdapter() { logging::core::get()->remove_all_sinks(); } logAdapter* logAdapter::getLogInstance() { - if (alogInstance == NULL) { - boost::mutex::scoped_lock guard(m_imtx); if (alogInstance == NULL) { - alogInstance = new logAdapter(); + boost::mutex::scoped_lock guard(m_imtx); + if (alogInstance == NULL) { + alogInstance = new logAdapter(); + } } - } - return alogInstance; + return alogInstance; } logAdapter::logAdapter() : m_logLevel(eLOG_LEVEL_INFO) { - string homeDir(UtilAll::getHomeDirectory()); - homeDir.append("/logs/rocketmq-cpp/"); - m_logFile += homeDir; - std::string fileName = - UtilAll::to_string(getpid()) + "_" + "rocketmq-cpp.log.%N"; - m_logFile += fileName; + string homeDir(UtilAll::getHomeDirectory()); + homeDir.append("/logs/rocketmq-cpp/"); + m_logFile += homeDir; + std::string fileName = UtilAll::to_string(getpid()) + "_" + "rocketmq-cpp.log.%N"; + m_logFile += fileName; - // boost::log::expressions::attr< - // boost::log::attributes::current_thread_id::value_type>("ThreadID"); - boost::log::register_simple_formatter_factory< - boost::log::trivial::severity_level, char>("Severity"); - m_logSink = logging::add_file_log( - keywords::file_name = m_logFile, - keywords::rotation_size = 100 * 1024 * 1024, - keywords::time_based_rotation = - sinks::file::rotation_at_time_point(0, 0, 0), - keywords::format = "[%TimeStamp%](%Severity%):%Message%", - keywords::min_free_space = 300 * 1024 * 1024, keywords::target = homeDir, - keywords::max_size = 200 * 1024 * 1024, // max keep 3 log file defaultly - keywords::auto_flush = true); - logging::core::get()->set_filter(logging::trivial::severity >= - logging::trivial::info); + // boost::log::expressions::attr< + // boost::log::attributes::current_thread_id::value_type>("ThreadID"); + boost::log::register_simple_formatter_factory("Severity"); + m_logSink = logging::add_file_log(keywords::file_name = m_logFile, keywords::rotation_size = 100 * 1024 * 1024, + keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), + keywords::format = "[%TimeStamp%](%Severity%):%Message%", + keywords::min_free_space = 300 * 1024 * 1024, keywords::target = homeDir, + keywords::max_size = 200 * 1024 * 1024, // max keep 3 log file defaultly + keywords::auto_flush = true); + logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info); - logging::add_common_attributes(); + logging::add_common_attributes(); } void logAdapter::setLogLevel(elogLevel logLevel) { - m_logLevel = logLevel; - switch (logLevel) { - case eLOG_LEVEL_FATAL: - logging::core::get()->set_filter(logging::trivial::severity >= - logging::trivial::fatal); - break; - case eLOG_LEVEL_ERROR: - logging::core::get()->set_filter(logging::trivial::severity >= - logging::trivial::error); + m_logLevel = logLevel; + switch (logLevel) { + case eLOG_LEVEL_FATAL: + logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::fatal); + break; + case eLOG_LEVEL_ERROR: + logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::error); - break; - case eLOG_LEVEL_WARN: - logging::core::get()->set_filter(logging::trivial::severity >= - logging::trivial::warning); + break; + case eLOG_LEVEL_WARN: + logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::warning); - break; - case eLOG_LEVEL_INFO: - logging::core::get()->set_filter(logging::trivial::severity >= - logging::trivial::info); + break; + case eLOG_LEVEL_INFO: + logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info); - break; - case eLOG_LEVEL_DEBUG: - logging::core::get()->set_filter(logging::trivial::severity >= - logging::trivial::debug); + break; + case eLOG_LEVEL_DEBUG: + logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::debug); - break; - case eLOG_LEVEL_TRACE: - logging::core::get()->set_filter(logging::trivial::severity >= - logging::trivial::trace); + break; + case eLOG_LEVEL_TRACE: + logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::trace); - break; - default: - logging::core::get()->set_filter(logging::trivial::severity >= - logging::trivial::info); + break; + default: + logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info); - break; - } + break; + } } elogLevel logAdapter::getLogLevel() { return m_logLevel; } void logAdapter::setLogFileNumAndSize(int logNum, int sizeOfPerFile) { - string homeDir(UtilAll::getHomeDirectory()); - homeDir.append("/logs/rocketmq-cpp/"); - m_logSink->locked_backend()->set_file_collector(sinks::file::make_collector( - keywords::target = homeDir, - keywords::max_size = logNum * sizeOfPerFile * 1024 * 1024)); + string homeDir(UtilAll::getHomeDirectory()); + homeDir.append("/logs/rocketmq-cpp/"); + m_logSink->locked_backend()->set_file_collector(sinks::file::make_collector( + keywords::target = homeDir, keywords::max_size = logNum * sizeOfPerFile * 1024 * 1024)); } } diff --git a/src/log/Logging.h b/src/log/Logging.h old mode 100755 new mode 100644 index ee9f1859a..b5b0b5067 --- a/src/log/Logging.h +++ b/src/log/Logging.h @@ -41,26 +41,23 @@ using namespace boost::log::trivial; namespace rocketmq { class logAdapter { - public: - ~logAdapter(); - static logAdapter* getLogInstance(); - void setLogLevel(elogLevel logLevel); - elogLevel getLogLevel(); - void setLogFileNumAndSize(int logNum, int sizeOfPerFile); - src::severity_logger& - getSeverityLogger() { - return m_severityLogger; - } +public: + ~logAdapter(); + static logAdapter* getLogInstance(); + void setLogLevel(elogLevel logLevel); + elogLevel getLogLevel(); + void setLogFileNumAndSize(int logNum, int sizeOfPerFile); + src::severity_logger& getSeverityLogger() { return m_severityLogger; } - private: - logAdapter(); - elogLevel m_logLevel; - std::string m_logFile; - src::severity_logger m_severityLogger; - typedef sinks::synchronous_sink logSink_t; - boost::shared_ptr m_logSink; - static logAdapter* alogInstance; - static boost::mutex m_imtx; +private: + logAdapter(); + elogLevel m_logLevel; + std::string m_logFile; + src::severity_logger m_severityLogger; + typedef sinks::synchronous_sink logSink_t; + boost::shared_ptr m_logSink; + static logAdapter* alogInstance; + static boost::mutex m_imtx; }; #define ALOG_ADAPTER logAdapter::getLogInstance() @@ -68,27 +65,21 @@ class logAdapter { #define AGENT_LOGGER ALOG_ADAPTER->getSeverityLogger() class LogUtil { - public: - static void LogMessage(boost::log::trivial::severity_level level, int line, - const char* format, ...) { - va_list arg_ptr; - va_start(arg_ptr, format); - boost::scoped_array formattedString(new char[1024]); - vsnprintf(formattedString.get(), 1024, format, arg_ptr); - BOOST_LOG_SEV(AGENT_LOGGER, level) << formattedString.get(); - va_end(arg_ptr); - } +public: + static void LogMessage(boost::log::trivial::severity_level level, int line, const char* format, ...) { + va_list arg_ptr; + va_start(arg_ptr, format); + boost::scoped_array formattedString(new char[1024]); + vsnprintf(formattedString.get(), 1024, format, arg_ptr); + BOOST_LOG_SEV(AGENT_LOGGER, level) << formattedString.get(); + va_end(arg_ptr); + } }; -#define LOG_FATAL(...) \ - LogUtil::LogMessage(boost::log::trivial::fatal, __LINE__, __VA_ARGS__) -#define LOG_ERROR(...) \ - LogUtil::LogMessage(boost::log::trivial::error, __LINE__, __VA_ARGS__) -#define LOG_WARN(...) \ - LogUtil::LogMessage(boost::log::trivial::warning, __LINE__, __VA_ARGS__) -#define LOG_INFO(...) \ - LogUtil::LogMessage(boost::log::trivial::info, __LINE__, __VA_ARGS__) -#define LOG_DEBUG(...) \ - LogUtil::LogMessage(boost::log::trivial::debug, __LINE__, __VA_ARGS__) +#define LOG_FATAL(...) LogUtil::LogMessage(boost::log::trivial::fatal, __LINE__, __VA_ARGS__) +#define LOG_ERROR(...) LogUtil::LogMessage(boost::log::trivial::error, __LINE__, __VA_ARGS__) +#define LOG_WARN(...) LogUtil::LogMessage(boost::log::trivial::warning, __LINE__, __VA_ARGS__) +#define LOG_INFO(...) LogUtil::LogMessage(boost::log::trivial::info, __LINE__, __VA_ARGS__) +#define LOG_DEBUG(...) LogUtil::LogMessage(boost::log::trivial::debug, __LINE__, __VA_ARGS__) } #endif diff --git a/src/message/BatchMessage.cpp b/src/message/BatchMessage.cpp index c2b9ec23d..68d64c4ac 100644 --- a/src/message/BatchMessage.cpp +++ b/src/message/BatchMessage.cpp @@ -5,41 +5,41 @@ using namespace std; namespace rocketmq { - std::string BatchMessage::encode(std::vector &msgs) { - string encodedBody; - for (auto message : msgs) { - string unique_id = StringIdMaker::get_mutable_instance().get_unique_id(); - message.setProperty(MQMessage::PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX, unique_id); - encodedBody.append(encode(message)); - } - return encodedBody; +std::string BatchMessage::encode(std::vector &msgs) { + string encodedBody; + for (auto message : msgs) { + string unique_id = StringIdMaker::get_mutable_instance().get_unique_id(); + message.setProperty(MQMessage::PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX, unique_id); + encodedBody.append(encode(message)); } + return encodedBody; +} - std::string BatchMessage::encode(MQMessage &message) { - string encodeMsg; - const string &body = message.getBody(); - int bodyLen = body.length(); - string properties = MQDecoder::messageProperties2String(message.getProperties()); - short propertiesLength = (short) properties.length(); - int storeSize = 20 + bodyLen + 2 + propertiesLength; - //TOTALSIZE|MAGICCOD|BODYCRC|FLAG|BODYLen|Body|propertiesLength|properties - int magicCode = 0; - int bodyCrc = 0; - int flag = message.getFlag(); - int storeSize_net = htonl(storeSize); - int magicCode_net = htonl(magicCode); - int bodyCrc_net = htonl(bodyCrc); - int flag_net = htonl(flag); - int bodyLen_net = htonl(bodyLen); - int propertiesLength_net = htons(propertiesLength); - encodeMsg.append((char*)&storeSize_net, sizeof(int)); - encodeMsg.append((char*)&magicCode_net, sizeof(int)); - encodeMsg.append((char*)&bodyCrc_net, sizeof(int)); - encodeMsg.append((char*)&flag_net, sizeof(int)); - encodeMsg.append((char*)&bodyLen_net, sizeof(int)); - encodeMsg.append(body.c_str(), body.length()); - encodeMsg.append((char*)&propertiesLength_net, sizeof(short)); - encodeMsg.append(properties.c_str(), propertiesLength); - return encodeMsg; - } +std::string BatchMessage::encode(MQMessage &message) { + string encodeMsg; + const string &body = message.getBody(); + int bodyLen = body.length(); + string properties = MQDecoder::messageProperties2String(message.getProperties()); + short propertiesLength = (short)properties.length(); + int storeSize = 20 + bodyLen + 2 + propertiesLength; + // TOTALSIZE|MAGICCOD|BODYCRC|FLAG|BODYLen|Body|propertiesLength|properties + int magicCode = 0; + int bodyCrc = 0; + int flag = message.getFlag(); + int storeSize_net = htonl(storeSize); + int magicCode_net = htonl(magicCode); + int bodyCrc_net = htonl(bodyCrc); + int flag_net = htonl(flag); + int bodyLen_net = htonl(bodyLen); + int propertiesLength_net = htons(propertiesLength); + encodeMsg.append((char *)&storeSize_net, sizeof(int)); + encodeMsg.append((char *)&magicCode_net, sizeof(int)); + encodeMsg.append((char *)&bodyCrc_net, sizeof(int)); + encodeMsg.append((char *)&flag_net, sizeof(int)); + encodeMsg.append((char *)&bodyLen_net, sizeof(int)); + encodeMsg.append(body.c_str(), body.length()); + encodeMsg.append((char *)&propertiesLength_net, sizeof(short)); + encodeMsg.append(properties.c_str(), propertiesLength); + return encodeMsg; +} } diff --git a/src/message/MQDecoder.cpp b/src/message/MQDecoder.cpp old mode 100755 new mode 100644 index b64ff1704..e596ec3b3 --- a/src/message/MQDecoder.cpp +++ b/src/message/MQDecoder.cpp @@ -39,208 +39,206 @@ int MQDecoder::MessageStoreTimestampPostion = 56; //sin_addr.s_addr); - outputmen.writeRepeatedByte(0, 2); - outputmen.write(&(sa->sin_port), 2); - outputmen.writeInt64BigEndian(offset); + MemoryOutputStream outputmen(MSG_ID_LENGTH); + outputmen.writeIntBigEndian(sa->sin_addr.s_addr); + outputmen.writeRepeatedByte(0, 2); + outputmen.write(&(sa->sin_port), 2); + outputmen.writeInt64BigEndian(offset); - const char *bytes = static_cast(outputmen.getData()); - int len = outputmen.getDataSize(); + const char *bytes = static_cast(outputmen.getData()); + int len = outputmen.getDataSize(); - return UtilAll::bytes2string(bytes, len); + return UtilAll::bytes2string(bytes, len); } MQMessageId MQDecoder::decodeMessageId(const string &msgId) { - string ipStr = msgId.substr(0, 8); - string portStr = msgId.substr(8, 8); - string offsetStr = msgId.substr(16); + string ipStr = msgId.substr(0, 8); + string portStr = msgId.substr(8, 8); + string offsetStr = msgId.substr(16); - char *end; - int ipInt = strtoul(ipStr.c_str(), &end, 16); - int portInt = strtoul(portStr.c_str(), &end, 16); + char *end; + int ipInt = strtoul(ipStr.c_str(), &end, 16); + int portInt = strtoul(portStr.c_str(), &end, 16); - int64 offset = UtilAll::hexstr2ull(offsetStr.c_str()); + int64 offset = UtilAll::hexstr2ull(offsetStr.c_str()); - struct sockaddr_in sa; - sa.sin_family = AF_INET; - sa.sin_port = htons(portInt); - sa.sin_addr.s_addr = htonl(ipInt); + struct sockaddr_in sa; + sa.sin_family = AF_INET; + sa.sin_port = htons(portInt); + sa.sin_addr.s_addr = htonl(ipInt); - MQMessageId id(*((sockaddr*) &sa), offset); - return id; + MQMessageId id(*((sockaddr *)&sa), offset); + return id; } -MQMessageExt *MQDecoder::decode(MemoryInputStream &byteBuffer) { - return decode(byteBuffer, true); -} +MQMessageExt *MQDecoder::decode(MemoryInputStream &byteBuffer) { return decode(byteBuffer, true); } MQMessageExt *MQDecoder::decode(MemoryInputStream &byteBuffer, bool readBody) { - MQMessageExt *msgExt = new MQMessageExt(); - - // 1 TOTALSIZE - int storeSize = byteBuffer.readIntBigEndian(); - msgExt->setStoreSize(storeSize); - - // 2 MAGICCODE sizeof(int) - byteBuffer.skipNextBytes(sizeof(int)); - - // 3 BODYCRC - int bodyCRC = byteBuffer.readIntBigEndian(); - msgExt->setBodyCRC(bodyCRC); - - // 4 QUEUEID - int queueId = byteBuffer.readIntBigEndian(); - msgExt->setQueueId(queueId); - - // 5 FLAG - int flag = byteBuffer.readIntBigEndian(); - msgExt->setFlag(flag); - - // 6 QUEUEOFFSET - int64 queueOffset = byteBuffer.readInt64BigEndian(); - msgExt->setQueueOffset(queueOffset); - - // 7 PHYSICALOFFSET - int64 physicOffset = byteBuffer.readInt64BigEndian(); - msgExt->setCommitLogOffset(physicOffset); - - // 8 SYSFLAG - int sysFlag = byteBuffer.readIntBigEndian(); - msgExt->setSysFlag(sysFlag); - - // 9 BORNTIMESTAMP - int64 bornTimeStamp = byteBuffer.readInt64BigEndian(); - msgExt->setBornTimestamp(bornTimeStamp); - - // 10 BORNHOST - int bornHost = byteBuffer.readIntBigEndian(); - int port = byteBuffer.readIntBigEndian(); - sockaddr bornAddr = IPPort2socketAddress(bornHost, port); - msgExt->setBornHost(bornAddr); - - // 11 STORETIMESTAMP - int64 storeTimestamp = byteBuffer.readInt64BigEndian(); - msgExt->setStoreTimestamp(storeTimestamp); - - // // 12 STOREHOST - int storeHost = byteBuffer.readIntBigEndian(); - port = byteBuffer.readIntBigEndian(); - sockaddr storeAddr = IPPort2socketAddress(storeHost, port); - msgExt->setStoreHost(storeAddr); - - // 13 RECONSUMETIMES - int reconsumeTimes = byteBuffer.readIntBigEndian(); - msgExt->setReconsumeTimes(reconsumeTimes); - - // 14 Prepared Transaction Offset - int64 preparedTransactionOffset = byteBuffer.readInt64BigEndian(); - msgExt->setPreparedTransactionOffset(preparedTransactionOffset); - - // 15 BODY - int bodyLen = byteBuffer.readIntBigEndian(); - if (bodyLen > 0) { - if (readBody) { - MemoryBlock block; - byteBuffer.readIntoMemoryBlock(block, bodyLen); - - const char *const pBody = static_cast(block.getData()); - int len = block.getSize(); - string msgbody(pBody, len); - - // decompress body - if ((sysFlag & MessageSysFlag::CompressedFlag) == MessageSysFlag::CompressedFlag) { - string outbody; - if (UtilAll::inflate(msgbody, outbody)) { - msgExt->setBody(outbody); + MQMessageExt *msgExt = new MQMessageExt(); + + // 1 TOTALSIZE + int storeSize = byteBuffer.readIntBigEndian(); + msgExt->setStoreSize(storeSize); + + // 2 MAGICCODE sizeof(int) + byteBuffer.skipNextBytes(sizeof(int)); + + // 3 BODYCRC + int bodyCRC = byteBuffer.readIntBigEndian(); + msgExt->setBodyCRC(bodyCRC); + + // 4 QUEUEID + int queueId = byteBuffer.readIntBigEndian(); + msgExt->setQueueId(queueId); + + // 5 FLAG + int flag = byteBuffer.readIntBigEndian(); + msgExt->setFlag(flag); + + // 6 QUEUEOFFSET + int64 queueOffset = byteBuffer.readInt64BigEndian(); + msgExt->setQueueOffset(queueOffset); + + // 7 PHYSICALOFFSET + int64 physicOffset = byteBuffer.readInt64BigEndian(); + msgExt->setCommitLogOffset(physicOffset); + + // 8 SYSFLAG + int sysFlag = byteBuffer.readIntBigEndian(); + msgExt->setSysFlag(sysFlag); + + // 9 BORNTIMESTAMP + int64 bornTimeStamp = byteBuffer.readInt64BigEndian(); + msgExt->setBornTimestamp(bornTimeStamp); + + // 10 BORNHOST + int bornHost = byteBuffer.readIntBigEndian(); + int port = byteBuffer.readIntBigEndian(); + sockaddr bornAddr = IPPort2socketAddress(bornHost, port); + msgExt->setBornHost(bornAddr); + + // 11 STORETIMESTAMP + int64 storeTimestamp = byteBuffer.readInt64BigEndian(); + msgExt->setStoreTimestamp(storeTimestamp); + + // // 12 STOREHOST + int storeHost = byteBuffer.readIntBigEndian(); + port = byteBuffer.readIntBigEndian(); + sockaddr storeAddr = IPPort2socketAddress(storeHost, port); + msgExt->setStoreHost(storeAddr); + + // 13 RECONSUMETIMES + int reconsumeTimes = byteBuffer.readIntBigEndian(); + msgExt->setReconsumeTimes(reconsumeTimes); + + // 14 Prepared Transaction Offset + int64 preparedTransactionOffset = byteBuffer.readInt64BigEndian(); + msgExt->setPreparedTransactionOffset(preparedTransactionOffset); + + // 15 BODY + int bodyLen = byteBuffer.readIntBigEndian(); + if (bodyLen > 0) { + if (readBody) { + MemoryBlock block; + byteBuffer.readIntoMemoryBlock(block, bodyLen); + + const char *const pBody = static_cast(block.getData()); + int len = block.getSize(); + string msgbody(pBody, len); + + // decompress body + if ((sysFlag & MessageSysFlag::CompressedFlag) == MessageSysFlag::CompressedFlag) { + string outbody; + if (UtilAll::inflate(msgbody, outbody)) { + msgExt->setBody(outbody); + } + } else { + msgExt->setBody(msgbody); + } + } else { + byteBuffer.skipNextBytes(bodyLen); } - } else { - msgExt->setBody(msgbody); - } - } else { - byteBuffer.skipNextBytes(bodyLen); } - } - - // 16 TOPIC - int topicLen = (int) byteBuffer.readByte(); - MemoryBlock block; - byteBuffer.readIntoMemoryBlock(block, topicLen); - const char *const pTopic = static_cast(block.getData()); - topicLen = block.getSize(); - msgExt->setTopic(pTopic, topicLen); - - // 17 properties - short propertiesLen = byteBuffer.readShortBigEndian(); - if (propertiesLen > 0) { + + // 16 TOPIC + int topicLen = (int)byteBuffer.readByte(); MemoryBlock block; - byteBuffer.readIntoMemoryBlock(block, propertiesLen); - const char *const pProperty = static_cast(block.getData()); - int len = block.getSize(); - string propertiesString(pProperty, len); - - map propertiesMap; - string2messageProperties(propertiesString, propertiesMap); - msgExt->setPropertiesInternal(propertiesMap); - propertiesMap.clear(); - } - - // 18 msg ID - string offsetMsgId = createMessageId(msgExt->getStoreHost(), (int64) msgExt->getCommitLogOffset()); - msgExt->setOffsetMsgId(offsetMsgId); - - string msgId = msgExt->getProperty(MQMessage::PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX); - if (msgId.empty()) { - msgId = offsetMsgId; - } - msgExt->setMsgId(msgId); - - // LOG_INFO("get msgExt from remote server, its contents are:%s", msgExt->toString().c_str()); - return msgExt; + byteBuffer.readIntoMemoryBlock(block, topicLen); + const char *const pTopic = static_cast(block.getData()); + topicLen = block.getSize(); + msgExt->setTopic(pTopic, topicLen); + + // 17 properties + short propertiesLen = byteBuffer.readShortBigEndian(); + if (propertiesLen > 0) { + MemoryBlock block; + byteBuffer.readIntoMemoryBlock(block, propertiesLen); + const char *const pProperty = static_cast(block.getData()); + int len = block.getSize(); + string propertiesString(pProperty, len); + + map propertiesMap; + string2messageProperties(propertiesString, propertiesMap); + msgExt->setPropertiesInternal(propertiesMap); + propertiesMap.clear(); + } + + // 18 msg ID + string offsetMsgId = createMessageId(msgExt->getStoreHost(), (int64)msgExt->getCommitLogOffset()); + msgExt->setOffsetMsgId(offsetMsgId); + + string msgId = msgExt->getProperty(MQMessage::PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX); + if (msgId.empty()) { + msgId = offsetMsgId; + } + msgExt->setMsgId(msgId); + + // LOG_INFO("get msgExt from remote server, its contents are:%s", msgExt->toString().c_str()); + return msgExt; } void MQDecoder::decodes(const MemoryBlock *mem, vector &mqvec) { - mqvec.clear(); - decodes(mem, mqvec, true); + mqvec.clear(); + decodes(mem, mqvec, true); } void MQDecoder::decodes(const MemoryBlock *mem, vector &mqvec, bool readBody) { - MemoryInputStream rawInput(*mem, true); + MemoryInputStream rawInput(*mem, true); - while (rawInput.getNumBytesRemaining() > 0) { - unique_ptr msg(decode(rawInput, readBody)); - mqvec.push_back(*msg); - } + while (rawInput.getNumBytesRemaining() > 0) { + unique_ptr msg(decode(rawInput, readBody)); + mqvec.push_back(*msg); + } } string MQDecoder::messageProperties2String(const map &properties) { - string os; - - for (const auto &it : properties) { - // os << it->first << NAME_VALUE_SEPARATOR << it->second << PROPERTY_SEPARATOR; - os.append(it.first); - os += NAME_VALUE_SEPARATOR; - os.append(it.second); - os += PROPERTY_SEPARATOR; - } + string os; + + for (const auto &it : properties) { + // os << it->first << NAME_VALUE_SEPARATOR << it->second << PROPERTY_SEPARATOR; + os.append(it.first); + os += NAME_VALUE_SEPARATOR; + os.append(it.second); + os += PROPERTY_SEPARATOR; + } - return os; + return os; } void MQDecoder::string2messageProperties(const string &propertiesString, map &properties) { - vector out; - UtilAll::Split(out, propertiesString, PROPERTY_SEPARATOR); + vector out; + UtilAll::Split(out, propertiesString, PROPERTY_SEPARATOR); - for (size_t i = 0; i < out.size(); i++) { - vector outValue; - UtilAll::Split(outValue, out[i], NAME_VALUE_SEPARATOR); + for (size_t i = 0; i < out.size(); i++) { + vector outValue; + UtilAll::Split(outValue, out[i], NAME_VALUE_SEPARATOR); - if (outValue.size() == 2) { - properties[outValue[0]] = outValue[1]; + if (outValue.size() == 2) { + properties[outValue[0]] = outValue[1]; + } } - } } } //& mqvec); + static void decodes(const MemoryBlock* mem, vector& mqvec); - static void decodes(const MemoryBlock* mem, vector& mqvec, bool readBody); + static void decodes(const MemoryBlock* mem, vector& mqvec, bool readBody); - static string messageProperties2String(const map& properties); - static void string2messageProperties(const string& propertiesString, map& properties); + static string messageProperties2String(const map& properties); + static void string2messageProperties(const string& propertiesString, map& properties); - private: - static MQMessageExt* decode(MemoryInputStream& byteBuffer); - static MQMessageExt* decode(MemoryInputStream& byteBuffer, bool readBody); +private: + static MQMessageExt* decode(MemoryInputStream& byteBuffer); + static MQMessageExt* decode(MemoryInputStream& byteBuffer, bool readBody); - public: - static const char NAME_VALUE_SEPARATOR; - static const char PROPERTY_SEPARATOR; - static const int MSG_ID_LENGTH; - static int MessageMagicCodePostion; - static int MessageFlagPostion; - static int MessagePhysicOffsetPostion; - static int MessageStoreTimestampPostion; +public: + static const char NAME_VALUE_SEPARATOR; + static const char PROPERTY_SEPARATOR; + static const int MSG_ID_LENGTH; + static int MessageMagicCodePostion; + static int MessageFlagPostion; + static int MessagePhysicOffsetPostion; + static int MessageStoreTimestampPostion; }; } //::const_iterator it = m_properties.find(name); - if (it == m_properties.end()) { - return EMPTY_STRING; - } else { - return it->second; - } +void MQMessage::setPropertyInternal(const string& name, const string& value) { m_properties[name] = value; } + +const string& MQMessage::getProperty(const string& name) const { + map::const_iterator it = m_properties.find(name); + if (it == m_properties.end()) { + return EMPTY_STRING; + } else { + return it->second; + } } const string& MQMessage::getTopic() const { return m_topic; } @@ -124,70 +117,66 @@ const string& MQMessage::getTopic() const { return m_topic; } void MQMessage::setTopic(const string& topic) { m_topic = topic; } void MQMessage::setTopic(const char* body, int len) { - m_topic.clear(); - m_topic.append(body, len); + m_topic.clear(); + m_topic.append(body, len); } const string& MQMessage::getTags() const { return getProperty(PROPERTY_TAGS); } -void MQMessage::setTags(const string& tags) { - setPropertyInternal(PROPERTY_TAGS, tags); -} +void MQMessage::setTags(const string& tags) { setPropertyInternal(PROPERTY_TAGS, tags); } const string& MQMessage::getKeys() const { return getProperty(PROPERTY_KEYS); } -void MQMessage::setKeys(const string& keys) { - setPropertyInternal(PROPERTY_KEYS, keys); -} +void MQMessage::setKeys(const string& keys) { setPropertyInternal(PROPERTY_KEYS, keys); } void MQMessage::setKeys(const vector& keys) { - if (keys.empty()) { - return; - } - - vector::const_iterator it = keys.begin(); - string str; - str += *it; - it++; + if (keys.empty()) { + return; + } - for (; it != keys.end(); it++) { - str += KEY_SEPARATOR; + vector::const_iterator it = keys.begin(); + string str; str += *it; - } + it++; + + for (; it != keys.end(); it++) { + str += KEY_SEPARATOR; + str += *it; + } - setKeys(str); + setKeys(str); } int MQMessage::getDelayTimeLevel() const { - string tmp = getProperty(PROPERTY_DELAY_TIME_LEVEL); - if (!tmp.empty()) { - return atoi(tmp.c_str()); - } - return 0; + string tmp = getProperty(PROPERTY_DELAY_TIME_LEVEL); + if (!tmp.empty()) { + return atoi(tmp.c_str()); + } + return 0; } void MQMessage::setDelayTimeLevel(int level) { - char tmp[16]; - sprintf(tmp, "%d", level); + char tmp[16]; + sprintf(tmp, "%d", level); - setPropertyInternal(PROPERTY_DELAY_TIME_LEVEL, tmp); + setPropertyInternal(PROPERTY_DELAY_TIME_LEVEL, tmp); } bool MQMessage::isWaitStoreMsgOK() const { - string tmp = getProperty(PROPERTY_WAIT_STORE_MSG_OK); - if (tmp.empty()) { - return true; - } else { - return (tmp == "true") ? true : false; - } + string tmp = getProperty(PROPERTY_WAIT_STORE_MSG_OK); + if (tmp.empty()) { + return true; + } else { + return (tmp == "true") ? true : false; + } } void MQMessage::setWaitStoreMsgOK(bool waitStoreMsgOK) { - if (waitStoreMsgOK) { - setPropertyInternal(PROPERTY_WAIT_STORE_MSG_OK, "true"); - } else { - setPropertyInternal(PROPERTY_WAIT_STORE_MSG_OK, "false"); - } + if (waitStoreMsgOK) { + setPropertyInternal(PROPERTY_WAIT_STORE_MSG_OK, "true"); + } else { + setPropertyInternal(PROPERTY_WAIT_STORE_MSG_OK, "false"); + } } int MQMessage::getFlag() const { return m_flag; } @@ -201,51 +190,48 @@ void MQMessage::setSysFlag(int sysFlag) { m_sysFlag = sysFlag; } const string& MQMessage::getBody() const { return m_body; } void MQMessage::setBody(const char* body, int len) { - m_body.clear(); - m_body.append(body, len); + m_body.clear(); + m_body.append(body, len); } -void MQMessage::setBody(const string &body) { - m_body.clear(); - m_body.append(body); +void MQMessage::setBody(const string& body) { + m_body.clear(); + m_body.append(body); } map MQMessage::getProperties() const { return m_properties; } void MQMessage::setProperties(map& properties) { - m_properties = properties; - - map::const_iterator it = m_properties.find(PROPERTY_TRANSACTION_PREPARED); - if (it != m_properties.end()) { - string tranMsg = it->second; - if (!tranMsg.empty() && tranMsg == "true") { - m_sysFlag |= MessageSysFlag::TransactionPreparedType; - } else { - m_sysFlag &= ~MessageSysFlag::TransactionPreparedType; + m_properties = properties; + + map::const_iterator it = m_properties.find(PROPERTY_TRANSACTION_PREPARED); + if (it != m_properties.end()) { + string tranMsg = it->second; + if (!tranMsg.empty() && tranMsg == "true") { + m_sysFlag |= MessageSysFlag::TransactionPreparedType; + } else { + m_sysFlag &= ~MessageSysFlag::TransactionPreparedType; + } } - } } -void MQMessage::setPropertiesInternal(map& properties) { - m_properties = properties; -} +void MQMessage::setPropertiesInternal(map& properties) { m_properties = properties; } -void MQMessage::Init(const string& topic, const string& tags, - const string& keys, const int flag, const string& body, +void MQMessage::Init(const string& topic, const string& tags, const string& keys, const int flag, const string& body, bool waitStoreMsgOK) { - m_topic = topic; - m_flag = flag; - m_sysFlag = 0; - m_body = body; + m_topic = topic; + m_flag = flag; + m_sysFlag = 0; + m_body = body; - if (tags.length() > 0) { - setTags(tags); - } + if (tags.length() > 0) { + setTags(tags); + } - if (keys.length() > 0) { - setKeys(keys); - } + if (keys.length() > 0) { + setKeys(keys); + } - setWaitStoreMsgOK(waitStoreMsgOK); + setWaitStoreMsgOK(waitStoreMsgOK); } } //registerProducer(this); - if (!registerOK) { - m_serviceState = CREATE_JUST; - THROW_MQEXCEPTION( - MQClientException, - "The producer group[" + getGroupName() + - "] has been created before, specify another name please.", - -1); - } - - getFactory()->start(); - getFactory()->sendHeartbeatToAllBroker(); - m_serviceState = RUNNING; - break; - } - case RUNNING: - case START_FAILED: - case SHUTDOWN_ALREADY: - break; - default: - break; - } + switch (m_serviceState) { + case CREATE_JUST: { + m_serviceState = START_FAILED; + MQClient::start(); + LOG_INFO("DefaultMQProducer:%s start", m_GroupName.c_str()); + + bool registerOK = getFactory()->registerProducer(this); + if (!registerOK) { + m_serviceState = CREATE_JUST; + THROW_MQEXCEPTION(MQClientException, "The producer group[" + getGroupName() + + "] has been created before, specify another name please.", + -1); + } + + getFactory()->start(); + getFactory()->sendHeartbeatToAllBroker(); + m_serviceState = RUNNING; + break; + } + case RUNNING: + case START_FAILED: + case SHUTDOWN_ALREADY: + break; + default: + break; + } } void DefaultMQProducer::shutdown() { - switch (m_serviceState) { - case RUNNING: { - LOG_INFO("DefaultMQProducer shutdown"); - getFactory()->unregisterProducer(this); - getFactory()->shutdown(); - m_serviceState = SHUTDOWN_ALREADY; - break; - } - case SHUTDOWN_ALREADY: - case CREATE_JUST: - break; - default: - break; - } + switch (m_serviceState) { + case RUNNING: { + LOG_INFO("DefaultMQProducer shutdown"); + getFactory()->unregisterProducer(this); + getFactory()->shutdown(); + m_serviceState = SHUTDOWN_ALREADY; + break; + } + case SHUTDOWN_ALREADY: + case CREATE_JUST: + break; + default: + break; + } } SendResult DefaultMQProducer::send(MQMessage& msg, bool bSelectActiveBroker) { - Validators::checkMessage(msg, getMaxMessageSize()); - try { - return sendDefaultImpl(msg, ComMode_SYNC, NULL, bSelectActiveBroker); - } catch (MQException& e) { - LOG_ERROR(e.what()); - throw e; - } - return SendResult(); + Validators::checkMessage(msg, getMaxMessageSize()); + try { + return sendDefaultImpl(msg, ComMode_SYNC, NULL, bSelectActiveBroker); + } + catch (MQException& e) { + LOG_ERROR(e.what()); + throw e; + } + return SendResult(); } -void DefaultMQProducer::send(MQMessage& msg, SendCallback* pSendCallback, - bool bSelectActiveBroker) { - Validators::checkMessage(msg, getMaxMessageSize()); - try { - sendDefaultImpl(msg, ComMode_ASYNC, pSendCallback, bSelectActiveBroker); - } catch (MQException& e) { - LOG_ERROR(e.what()); - throw e; - } +void DefaultMQProducer::send(MQMessage& msg, SendCallback* pSendCallback, bool bSelectActiveBroker) { + Validators::checkMessage(msg, getMaxMessageSize()); + try { + sendDefaultImpl(msg, ComMode_ASYNC, pSendCallback, bSelectActiveBroker); + } + catch (MQException& e) { + LOG_ERROR(e.what()); + throw e; + } } SendResult DefaultMQProducer::send(std::vector& msgs) { - SendResult result; - try { - BatchMessage batchMessage = buildBatchMessage(msgs); - result = sendDefaultImpl(batchMessage, ComMode_SYNC, NULL); - } catch (MQException& e) { - LOG_ERROR(e.what()); - throw e; - } - return result; + SendResult result; + try { + BatchMessage batchMessage = buildBatchMessage(msgs); + result = sendDefaultImpl(batchMessage, ComMode_SYNC, NULL); + } + catch (MQException& e) { + LOG_ERROR(e.what()); + throw e; + } + return result; } SendResult DefaultMQProducer::send(std::vector& msgs, const MQMessageQueue& mq) { - SendResult result; - try { - BatchMessage batchMessage = buildBatchMessage(msgs); - result = sendKernelImpl(batchMessage, mq, ComMode_SYNC, NULL); - } catch (MQException& e) { - LOG_ERROR(e.what()); - throw e; - } - return result; + SendResult result; + try { + BatchMessage batchMessage = buildBatchMessage(msgs); + result = sendKernelImpl(batchMessage, mq, ComMode_SYNC, NULL); + } + catch (MQException& e) { + LOG_ERROR(e.what()); + throw e; + } + return result; } BatchMessage DefaultMQProducer::buildBatchMessage(std::vector& msgs) { - if (msgs.size() < 1) { - THROW_MQEXCEPTION(MQClientException, "msgs need one message at least", -1); - } - BatchMessage batchMessage; - bool firstFlag = true; - string topic; - bool waitStoreMsgOK = false; - for (auto& msg : msgs) { - Validators::checkMessage(msg, getMaxMessageSize()); - if (firstFlag) { - topic = msg.getTopic(); - waitStoreMsgOK = msg.isWaitStoreMsgOK(); - firstFlag = false; - - if (UtilAll::startsWith_retry(topic)) { - THROW_MQEXCEPTION(MQClientException, "Retry Group is not supported for batching", -1); - } - } else { - - if (msg.getDelayTimeLevel() > 0) { - THROW_MQEXCEPTION(MQClientException, "TimeDelayLevel in not supported for batching", -1); - } - if (msg.getTopic() != topic) { + if (msgs.size() < 1) { THROW_MQEXCEPTION(MQClientException, "msgs need one message at least", -1); - } - if (msg.isWaitStoreMsgOK() != waitStoreMsgOK) { - THROW_MQEXCEPTION(MQClientException, "msgs need one message at least", -2); - } - } - } - batchMessage.setBody(BatchMessage::encode(msgs)); - batchMessage.setTopic(topic); - batchMessage.setWaitStoreMsgOK(waitStoreMsgOK); - return batchMessage; + } + BatchMessage batchMessage; + bool firstFlag = true; + string topic; + bool waitStoreMsgOK = false; + for (auto& msg : msgs) { + Validators::checkMessage(msg, getMaxMessageSize()); + if (firstFlag) { + topic = msg.getTopic(); + waitStoreMsgOK = msg.isWaitStoreMsgOK(); + firstFlag = false; + + if (UtilAll::startsWith_retry(topic)) { + THROW_MQEXCEPTION(MQClientException, "Retry Group is not supported for batching", -1); + } + } else { + + if (msg.getDelayTimeLevel() > 0) { + THROW_MQEXCEPTION(MQClientException, "TimeDelayLevel in not supported for batching", -1); + } + if (msg.getTopic() != topic) { + THROW_MQEXCEPTION(MQClientException, "msgs need one message at least", -1); + } + if (msg.isWaitStoreMsgOK() != waitStoreMsgOK) { + THROW_MQEXCEPTION(MQClientException, "msgs need one message at least", -2); + } + } + } + batchMessage.setBody(BatchMessage::encode(msgs)); + batchMessage.setTopic(topic); + batchMessage.setWaitStoreMsgOK(waitStoreMsgOK); + return batchMessage; } SendResult DefaultMQProducer::send(MQMessage& msg, const MQMessageQueue& mq) { - Validators::checkMessage(msg, getMaxMessageSize()); - if (msg.getTopic() != mq.getTopic()) { - LOG_WARN("message's topic not equal mq's topic"); - } - try { - return sendKernelImpl(msg, mq, ComMode_SYNC, NULL); - } catch (MQException& e) { - LOG_ERROR(e.what()); - throw e; - } - return SendResult(); + Validators::checkMessage(msg, getMaxMessageSize()); + if (msg.getTopic() != mq.getTopic()) { + LOG_WARN("message's topic not equal mq's topic"); + } + try { + return sendKernelImpl(msg, mq, ComMode_SYNC, NULL); + } + catch (MQException& e) { + LOG_ERROR(e.what()); + throw e; + } + return SendResult(); } -void DefaultMQProducer::send(MQMessage& msg, const MQMessageQueue& mq, - SendCallback* pSendCallback) { - Validators::checkMessage(msg, getMaxMessageSize()); - if (msg.getTopic() != mq.getTopic()) { - LOG_WARN("message's topic not equal mq's topic"); - } - try { - sendKernelImpl(msg, mq, ComMode_ASYNC, pSendCallback); - } catch (MQException& e) { - LOG_ERROR(e.what()); - throw e; - } +void DefaultMQProducer::send(MQMessage& msg, const MQMessageQueue& mq, SendCallback* pSendCallback) { + Validators::checkMessage(msg, getMaxMessageSize()); + if (msg.getTopic() != mq.getTopic()) { + LOG_WARN("message's topic not equal mq's topic"); + } + try { + sendKernelImpl(msg, mq, ComMode_ASYNC, pSendCallback); + } + catch (MQException& e) { + LOG_ERROR(e.what()); + throw e; + } } void DefaultMQProducer::sendOneway(MQMessage& msg, bool bSelectActiveBroker) { - Validators::checkMessage(msg, getMaxMessageSize()); - try { - sendDefaultImpl(msg, ComMode_ONEWAY, NULL, bSelectActiveBroker); - } catch (MQException& e) { - LOG_ERROR(e.what()); - throw e; - } + Validators::checkMessage(msg, getMaxMessageSize()); + try { + sendDefaultImpl(msg, ComMode_ONEWAY, NULL, bSelectActiveBroker); + } + catch (MQException& e) { + LOG_ERROR(e.what()); + throw e; + } } void DefaultMQProducer::sendOneway(MQMessage& msg, const MQMessageQueue& mq) { - Validators::checkMessage(msg, getMaxMessageSize()); - if (msg.getTopic() != mq.getTopic()) { - LOG_WARN("message's topic not equal mq's topic"); - } - try { - sendKernelImpl(msg, mq, ComMode_ONEWAY, NULL); - } catch (MQException& e) { - LOG_ERROR(e.what()); - throw e; - } + Validators::checkMessage(msg, getMaxMessageSize()); + if (msg.getTopic() != mq.getTopic()) { + LOG_WARN("message's topic not equal mq's topic"); + } + try { + sendKernelImpl(msg, mq, ComMode_ONEWAY, NULL); + } + catch (MQException& e) { + LOG_ERROR(e.what()); + throw e; + } } -SendResult DefaultMQProducer::send(MQMessage& msg, - MessageQueueSelector* pSelector, void* arg) { - try { - return sendSelectImpl(msg, pSelector, arg, ComMode_SYNC, NULL); - } catch (MQException& e) { - LOG_ERROR(e.what()); - throw e; - } - return SendResult(); +SendResult DefaultMQProducer::send(MQMessage& msg, MessageQueueSelector* pSelector, void* arg) { + try { + return sendSelectImpl(msg, pSelector, arg, ComMode_SYNC, NULL); + } + catch (MQException& e) { + LOG_ERROR(e.what()); + throw e; + } + return SendResult(); } -SendResult DefaultMQProducer::send(MQMessage& msg, - MessageQueueSelector* pSelector, void* arg, - int autoRetryTimes, bool bActiveBroker) { - try { - return sendAutoRetrySelectImpl(msg, pSelector, arg, ComMode_SYNC, NULL, - autoRetryTimes, bActiveBroker); - } catch (MQException& e) { - LOG_ERROR(e.what()); - throw e; - } - return SendResult(); +SendResult DefaultMQProducer::send(MQMessage& msg, MessageQueueSelector* pSelector, void* arg, int autoRetryTimes, + bool bActiveBroker) { + try { + return sendAutoRetrySelectImpl(msg, pSelector, arg, ComMode_SYNC, NULL, autoRetryTimes, bActiveBroker); + } + catch (MQException& e) { + LOG_ERROR(e.what()); + throw e; + } + return SendResult(); } -void DefaultMQProducer::send(MQMessage& msg, MessageQueueSelector* pSelector, - void* arg, SendCallback* pSendCallback) { - try { - sendSelectImpl(msg, pSelector, arg, ComMode_ASYNC, pSendCallback); - } catch (MQException& e) { - LOG_ERROR(e.what()); - throw e; - } +void DefaultMQProducer::send(MQMessage& msg, MessageQueueSelector* pSelector, void* arg, SendCallback* pSendCallback) { + try { + sendSelectImpl(msg, pSelector, arg, ComMode_ASYNC, pSendCallback); + } + catch (MQException& e) { + LOG_ERROR(e.what()); + throw e; + } } -void DefaultMQProducer::sendOneway(MQMessage& msg, - MessageQueueSelector* pSelector, void* arg) { - try { - sendSelectImpl(msg, pSelector, arg, ComMode_ONEWAY, NULL); - } catch (MQException& e) { - LOG_ERROR(e.what()); - throw e; - } +void DefaultMQProducer::sendOneway(MQMessage& msg, MessageQueueSelector* pSelector, void* arg) { + try { + sendSelectImpl(msg, pSelector, arg, ComMode_ONEWAY, NULL); + } + catch (MQException& e) { + LOG_ERROR(e.what()); + throw e; + } } int DefaultMQProducer::getSendMsgTimeout() const { return m_sendMsgTimeout; } -void DefaultMQProducer::setSendMsgTimeout(int sendMsgTimeout) { - m_sendMsgTimeout = sendMsgTimeout; -} +void DefaultMQProducer::setSendMsgTimeout(int sendMsgTimeout) { m_sendMsgTimeout = sendMsgTimeout; } -int DefaultMQProducer::getCompressMsgBodyOverHowmuch() const { - return m_compressMsgBodyOverHowmuch; -} +int DefaultMQProducer::getCompressMsgBodyOverHowmuch() const { return m_compressMsgBodyOverHowmuch; } -void DefaultMQProducer::setCompressMsgBodyOverHowmuch( - int compressMsgBodyOverHowmuch) { - m_compressMsgBodyOverHowmuch = compressMsgBodyOverHowmuch; +void DefaultMQProducer::setCompressMsgBodyOverHowmuch(int compressMsgBodyOverHowmuch) { + m_compressMsgBodyOverHowmuch = compressMsgBodyOverHowmuch; } int DefaultMQProducer::getMaxMessageSize() const { return m_maxMessageSize; } -void DefaultMQProducer::setMaxMessageSize(int maxMessageSize) { - m_maxMessageSize = maxMessageSize; -} +void DefaultMQProducer::setMaxMessageSize(int maxMessageSize) { m_maxMessageSize = maxMessageSize; } int DefaultMQProducer::getCompressLevel() const { return m_compressLevel; } void DefaultMQProducer::setCompressLevel(int compressLevel) { - assert((compressLevel >= 0 && compressLevel <= 9) || compressLevel == -1); + assert((compressLevel >= 0 && compressLevel <= 9) || compressLevel == -1); - m_compressLevel = compressLevel; + m_compressLevel = compressLevel; } // weak_topicPublishInfo( - getFactory()->tryToFindTopicPublishInfo(msg.getTopic(), - getSessionCredentials())); - boost::shared_ptr topicPublishInfo( - weak_topicPublishInfo.lock()); - if (topicPublishInfo) { - if (times == 1) { - mq_index = topicPublishInfo->getWhichQueue(); - } else { - mq_index++; - } - - SendResult sendResult; - MQMessageQueue mq; - if (bActiveMQ) - mq = topicPublishInfo->selectOneActiveMessageQueue(lastmq, mq_index); - else - mq = topicPublishInfo->selectOneMessageQueue(lastmq, mq_index); - - lastmq = mq; - if (mq.getQueueId() == -1) { - // THROW_MQEXCEPTION(MQClientException, "the MQMessageQueue is - // invalide", -1); - continue; - } - - try { - LOG_DEBUG("send to mq:%s", mq.toString().data()); - sendResult = sendKernelImpl(msg, mq, communicationMode, pSendCallback); - switch (communicationMode) { - case ComMode_ASYNC: - return sendResult; - case ComMode_ONEWAY: - return sendResult; - case ComMode_SYNC: - if (sendResult.getSendStatus() != SEND_OK) { - if (bActiveMQ) { - topicPublishInfo->updateNonServiceMessageQueue( - mq, getSendMsgTimeout()); - } - continue; + MQMessageQueue lastmq; + int mq_index = 0; + for (int times = 1; times <= m_retryTimes; times++) { + boost::weak_ptr weak_topicPublishInfo( + getFactory()->tryToFindTopicPublishInfo(msg.getTopic(), getSessionCredentials())); + boost::shared_ptr topicPublishInfo(weak_topicPublishInfo.lock()); + if (topicPublishInfo) { + if (times == 1) { + mq_index = topicPublishInfo->getWhichQueue(); + } else { + mq_index++; } - return sendResult; - default: - break; - } - } catch (...) { - LOG_ERROR("send failed of times:%d,brokerName:%s", times, - mq.getBrokerName().c_str()); - if (bActiveMQ) { - topicPublishInfo->updateNonServiceMessageQueue(mq, - getSendMsgTimeout()); - } - continue; - } - } // end of for - LOG_WARN("Retry many times, still failed"); - } - string info = "No route info of this topic: " + msg.getTopic(); - THROW_MQEXCEPTION(MQClientException, info, -1); + + SendResult sendResult; + MQMessageQueue mq; + if (bActiveMQ) + mq = topicPublishInfo->selectOneActiveMessageQueue(lastmq, mq_index); + else + mq = topicPublishInfo->selectOneMessageQueue(lastmq, mq_index); + + lastmq = mq; + if (mq.getQueueId() == -1) { + // THROW_MQEXCEPTION(MQClientException, "the MQMessageQueue is + // invalide", -1); + continue; + } + + try { + LOG_DEBUG("send to mq:%s", mq.toString().data()); + sendResult = sendKernelImpl(msg, mq, communicationMode, pSendCallback); + switch (communicationMode) { + case ComMode_ASYNC: + return sendResult; + case ComMode_ONEWAY: + return sendResult; + case ComMode_SYNC: + if (sendResult.getSendStatus() != SEND_OK) { + if (bActiveMQ) { + topicPublishInfo->updateNonServiceMessageQueue(mq, getSendMsgTimeout()); + } + continue; + } + return sendResult; + default: + break; + } + } + catch (...) { + LOG_ERROR("send failed of times:%d,brokerName:%s", times, mq.getBrokerName().c_str()); + if (bActiveMQ) { + topicPublishInfo->updateNonServiceMessageQueue(mq, getSendMsgTimeout()); + } + continue; + } + } // end of for + LOG_WARN("Retry many times, still failed"); + } + string info = "No route info of this topic: " + msg.getTopic(); + THROW_MQEXCEPTION(MQClientException, info, -1); } -SendResult DefaultMQProducer::sendKernelImpl(MQMessage& msg, - const MQMessageQueue& mq, - int communicationMode, +SendResult DefaultMQProducer::sendKernelImpl(MQMessage& msg, const MQMessageQueue& mq, int communicationMode, SendCallback* sendCallback) { - string brokerAddr = - getFactory()->findBrokerAddressInPublish(mq.getBrokerName()); + string brokerAddr = getFactory()->findBrokerAddressInPublish(mq.getBrokerName()); - if (brokerAddr.empty()) { - getFactory()->tryToFindTopicPublishInfo(mq.getTopic(), - getSessionCredentials()); - brokerAddr = getFactory()->findBrokerAddressInPublish(mq.getBrokerName()); - } + if (brokerAddr.empty()) { + getFactory()->tryToFindTopicPublishInfo(mq.getTopic(), getSessionCredentials()); + brokerAddr = getFactory()->findBrokerAddressInPublish(mq.getBrokerName()); + } - if (!brokerAddr.empty()) { - try { - BatchMessage batchMessage; - bool isBatchMsg = (typeid(msg).name() == typeid(batchMessage).name()); - //msgId is produced by client, offsetMsgId produced by broker. (same with java sdk) - if (!isBatchMsg) { - string unique_id = StringIdMaker::get_mutable_instance().get_unique_id(); - msg.setProperty(MQMessage::PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX, unique_id); - } - - LOG_DEBUG("produce before:%s to %s", msg.toString().c_str(), mq.toString().c_str()); - - tryToCompressMessage(msg); - - SendMessageRequestHeader* requestHeader = new SendMessageRequestHeader(); - requestHeader->producerGroup = getGroupName(); - requestHeader->topic = (msg.getTopic()); - requestHeader->defaultTopic = DEFAULT_TOPIC; - requestHeader->defaultTopicQueueNums = 4; - requestHeader->queueId = (mq.getQueueId()); - requestHeader->sysFlag = (msg.getSysFlag()); - requestHeader->bornTimestamp = UtilAll::currentTimeMillis(); - requestHeader->flag = (msg.getFlag()); - requestHeader->batch = isBatchMsg; - requestHeader->properties = - (MQDecoder::messageProperties2String(msg.getProperties())); - - return getFactory()->getMQClientAPIImpl()->sendMessage( - brokerAddr, mq.getBrokerName(), msg, requestHeader, - getSendMsgTimeout(), getRetryTimes4Async(), communicationMode, sendCallback, - getSessionCredentials()); - } catch (MQException& e) { - throw e; - } - } - THROW_MQEXCEPTION(MQClientException, - "The broker[" + mq.getBrokerName() + "] not exist", -1); -} + if (!brokerAddr.empty()) { + try { + BatchMessage batchMessage; + bool isBatchMsg = (typeid(msg).name() == typeid(batchMessage).name()); + // msgId is produced by client, offsetMsgId produced by broker. (same with java sdk) + if (!isBatchMsg) { + string unique_id = StringIdMaker::get_mutable_instance().get_unique_id(); + msg.setProperty(MQMessage::PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX, unique_id); + } -SendResult DefaultMQProducer::sendSelectImpl(MQMessage& msg, - MessageQueueSelector* pSelector, - void* pArg, int communicationMode, - SendCallback* sendCallback) { - Validators::checkMessage(msg, getMaxMessageSize()); - - boost::weak_ptr weak_topicPublishInfo( - getFactory()->tryToFindTopicPublishInfo(msg.getTopic(), - getSessionCredentials())); - boost::shared_ptr topicPublishInfo( - weak_topicPublishInfo.lock()); - if (topicPublishInfo) //&& topicPublishInfo->ok()) - { - MQMessageQueue mq = - pSelector->select(topicPublishInfo->getMessageQueueList(), msg, pArg); - return sendKernelImpl(msg, mq, communicationMode, sendCallback); - } - THROW_MQEXCEPTION(MQClientException, "No route info for this topic", -1); + LOG_DEBUG("produce before:%s to %s", msg.toString().c_str(), mq.toString().c_str()); + + tryToCompressMessage(msg); + + SendMessageRequestHeader* requestHeader = new SendMessageRequestHeader(); + requestHeader->producerGroup = getGroupName(); + requestHeader->topic = (msg.getTopic()); + requestHeader->defaultTopic = DEFAULT_TOPIC; + requestHeader->defaultTopicQueueNums = 4; + requestHeader->queueId = (mq.getQueueId()); + requestHeader->sysFlag = (msg.getSysFlag()); + requestHeader->bornTimestamp = UtilAll::currentTimeMillis(); + requestHeader->flag = (msg.getFlag()); + requestHeader->batch = isBatchMsg; + requestHeader->properties = (MQDecoder::messageProperties2String(msg.getProperties())); + + return getFactory()->getMQClientAPIImpl()->sendMessage( + brokerAddr, mq.getBrokerName(), msg, requestHeader, getSendMsgTimeout(), getRetryTimes4Async(), + communicationMode, sendCallback, getSessionCredentials()); + } + catch (MQException& e) { + throw e; + } + } + THROW_MQEXCEPTION(MQClientException, "The broker[" + mq.getBrokerName() + "] not exist", -1); } -SendResult DefaultMQProducer::sendAutoRetrySelectImpl( - MQMessage& msg, MessageQueueSelector* pSelector, void* pArg, - int communicationMode, SendCallback* pSendCallback, int autoRetryTimes, - bool bActiveMQ) { - Validators::checkMessage(msg, getMaxMessageSize()); +SendResult DefaultMQProducer::sendSelectImpl(MQMessage& msg, MessageQueueSelector* pSelector, void* pArg, + int communicationMode, SendCallback* sendCallback) { + Validators::checkMessage(msg, getMaxMessageSize()); - MQMessageQueue lastmq; - MQMessageQueue mq; - int mq_index = 0; - for (int times = 1; times <= autoRetryTimes + 1; times++) { boost::weak_ptr weak_topicPublishInfo( - getFactory()->tryToFindTopicPublishInfo(msg.getTopic(), - getSessionCredentials())); - boost::shared_ptr topicPublishInfo( - weak_topicPublishInfo.lock()); - if (topicPublishInfo) { - SendResult sendResult; - if (times == 1) { // always send to selected MQ firstly, evenif bActiveMQ - // was setted to true - mq = pSelector->select(topicPublishInfo->getMessageQueueList(), msg, - pArg); - lastmq = mq; - } else { - LOG_INFO("sendAutoRetrySelectImpl with times:%d", times); - vector mqs(topicPublishInfo->getMessageQueueList()); - for (size_t i = 0; i < mqs.size(); i++) { - if (mqs[i] == lastmq) mq_index = i; - } - if (bActiveMQ) - mq = topicPublishInfo->selectOneActiveMessageQueue(lastmq, mq_index); - else - mq = topicPublishInfo->selectOneMessageQueue(lastmq, mq_index); - lastmq = mq; - if (mq.getQueueId() == -1) { - // THROW_MQEXCEPTION(MQClientException, "the MQMessageQueue is - // invalide", -1); - continue; - } - } - - try { - LOG_DEBUG("send to broker:%s", mq.toString().c_str()); - sendResult = sendKernelImpl(msg, mq, communicationMode, pSendCallback); - switch (communicationMode) { - case ComMode_ASYNC: - return sendResult; - case ComMode_ONEWAY: - return sendResult; - case ComMode_SYNC: - if (sendResult.getSendStatus() != SEND_OK) { - if (bActiveMQ) { - topicPublishInfo->updateNonServiceMessageQueue( - mq, getSendMsgTimeout()); - } - continue; + getFactory()->tryToFindTopicPublishInfo(msg.getTopic(), getSessionCredentials())); + boost::shared_ptr topicPublishInfo(weak_topicPublishInfo.lock()); + if (topicPublishInfo) //&& topicPublishInfo->ok()) + { + MQMessageQueue mq = pSelector->select(topicPublishInfo->getMessageQueueList(), msg, pArg); + return sendKernelImpl(msg, mq, communicationMode, sendCallback); + } + THROW_MQEXCEPTION(MQClientException, "No route info for this topic", -1); +} + +SendResult DefaultMQProducer::sendAutoRetrySelectImpl(MQMessage& msg, MessageQueueSelector* pSelector, void* pArg, + int communicationMode, SendCallback* pSendCallback, + int autoRetryTimes, bool bActiveMQ) { + Validators::checkMessage(msg, getMaxMessageSize()); + + MQMessageQueue lastmq; + MQMessageQueue mq; + int mq_index = 0; + for (int times = 1; times <= autoRetryTimes + 1; times++) { + boost::weak_ptr weak_topicPublishInfo( + getFactory()->tryToFindTopicPublishInfo(msg.getTopic(), getSessionCredentials())); + boost::shared_ptr topicPublishInfo(weak_topicPublishInfo.lock()); + if (topicPublishInfo) { + SendResult sendResult; + if (times == 1) { // always send to selected MQ firstly, evenif bActiveMQ + // was setted to true + mq = pSelector->select(topicPublishInfo->getMessageQueueList(), msg, pArg); + lastmq = mq; + } else { + LOG_INFO("sendAutoRetrySelectImpl with times:%d", times); + vector mqs(topicPublishInfo->getMessageQueueList()); + for (size_t i = 0; i < mqs.size(); i++) { + if (mqs[i] == lastmq) mq_index = i; + } + if (bActiveMQ) + mq = topicPublishInfo->selectOneActiveMessageQueue(lastmq, mq_index); + else + mq = topicPublishInfo->selectOneMessageQueue(lastmq, mq_index); + lastmq = mq; + if (mq.getQueueId() == -1) { + // THROW_MQEXCEPTION(MQClientException, "the MQMessageQueue is + // invalide", -1); + continue; + } } - return sendResult; - default: - break; - } - } catch (...) { - LOG_ERROR("send failed of times:%d,mq:%s", times, - mq.toString().c_str()); - if (bActiveMQ) { - topicPublishInfo->updateNonServiceMessageQueue(mq, - getSendMsgTimeout()); - } - continue; - } - } // end of for - LOG_WARN("Retry many times, still failed"); - } - THROW_MQEXCEPTION(MQClientException, "No route info of this topic, ", -1); + + try { + LOG_DEBUG("send to broker:%s", mq.toString().c_str()); + sendResult = sendKernelImpl(msg, mq, communicationMode, pSendCallback); + switch (communicationMode) { + case ComMode_ASYNC: + return sendResult; + case ComMode_ONEWAY: + return sendResult; + case ComMode_SYNC: + if (sendResult.getSendStatus() != SEND_OK) { + if (bActiveMQ) { + topicPublishInfo->updateNonServiceMessageQueue(mq, getSendMsgTimeout()); + } + continue; + } + return sendResult; + default: + break; + } + } + catch (...) { + LOG_ERROR("send failed of times:%d,mq:%s", times, mq.toString().c_str()); + if (bActiveMQ) { + topicPublishInfo->updateNonServiceMessageQueue(mq, getSendMsgTimeout()); + } + continue; + } + } // end of for + LOG_WARN("Retry many times, still failed"); + } + THROW_MQEXCEPTION(MQClientException, "No route info of this topic, ", -1); } bool DefaultMQProducer::tryToCompressMessage(MQMessage& msg) { - int sysFlag = msg.getSysFlag(); - if ((sysFlag & MessageSysFlag::CompressedFlag) == MessageSysFlag::CompressedFlag) { - return true; - } - - string body = msg.getBody(); - if ((int)body.length() >= getCompressMsgBodyOverHowmuch()) { - string outBody; - if (UtilAll::deflate(body, outBody, getCompressLevel())) { - msg.setBody(outBody); - msg.setSysFlag(sysFlag | MessageSysFlag::CompressedFlag); - return true; - } - } - - return false; + int sysFlag = msg.getSysFlag(); + if ((sysFlag & MessageSysFlag::CompressedFlag) == MessageSysFlag::CompressedFlag) { + return true; + } + + string body = msg.getBody(); + if ((int)body.length() >= getCompressMsgBodyOverHowmuch()) { + string outBody; + if (UtilAll::deflate(body, outBody, getCompressLevel())) { + msg.setBody(outBody); + msg.setSysFlag(sysFlag | MessageSysFlag::CompressedFlag); + return true; + } + } + + return false; } int DefaultMQProducer::getRetryTimes() const { return m_retryTimes; } void DefaultMQProducer::setRetryTimes(int times) { - if (times <= 0) { - LOG_WARN("set retry times illegal, use default value:5"); - return; - } - - if (times > 15) { - LOG_WARN("set retry times illegal, use max value:15"); - m_retryTimes = 15; - return; - } - LOG_WARN("set retry times to:%d", times); - m_retryTimes = times; -} + if (times <= 0) { + LOG_WARN("set retry times illegal, use default value:5"); + return; + } -int DefaultMQProducer::getRetryTimes4Async() const -{ - return m_retryTimes4Async; + if (times > 15) { + LOG_WARN("set retry times illegal, use max value:15"); + m_retryTimes = 15; + return; + } + LOG_WARN("set retry times to:%d", times); + m_retryTimes = times; } -void DefaultMQProducer::setRetryTimes4Async(int times) -{ - if (times <= 0) { - LOG_WARN("set retry times illegal, use default value:1"); - m_retryTimes4Async = 1; - return; - } - - if (times > 15) { - LOG_WARN("set retry times illegal, use max value:15"); - m_retryTimes4Async = 15; - return; - } - LOG_INFO("set retry times to:%d", times); - m_retryTimes4Async = times; + +int DefaultMQProducer::getRetryTimes4Async() const { return m_retryTimes4Async; } +void DefaultMQProducer::setRetryTimes4Async(int times) { + if (times <= 0) { + LOG_WARN("set retry times illegal, use default value:1"); + m_retryTimes4Async = 1; + return; + } + + if (times > 15) { + LOG_WARN("set retry times illegal, use max value:15"); + m_retryTimes4Async = 15; + return; + } + LOG_INFO("set retry times to:%d", times); + m_retryTimes4Async = times; } //tv_sec = clock; - tp->tv_usec = wtm.wMilliseconds * 1000; - return (0); +int gettimeofdayWin(struct timeval *tp, void *tzp) { + time_t clock; + struct tm tm; + SYSTEMTIME wtm; + GetLocalTime(&wtm); + tm.tm_year = wtm.wYear - 1900; + tm.tm_mon = wtm.wMonth - 1; + tm.tm_mday = wtm.wDay; + tm.tm_hour = wtm.wHour; + tm.tm_min = wtm.wMinute; + tm.tm_sec = wtm.wSecond; + tm.tm_isdst = -1; + clock = mktime(&tm); + tp->tv_sec = clock; + tp->tv_usec = wtm.wMilliseconds * 1000; + return (0); } #endif StringIdMaker::StringIdMaker() { - memset(_buff, 0, sizeof(_buff)); - memset(_0x_buff, 0, sizeof(_0x_buff)); - srand((uint32_t)time(NULL)); - init_prefix(); + memset(_buff, 0, sizeof(_buff)); + memset(_0x_buff, 0, sizeof(_0x_buff)); + srand((uint32_t)time(NULL)); + init_prefix(); } StringIdMaker::~StringIdMaker() {} void StringIdMaker::init_prefix() { - uint32_t pid = getpid(); - uint32_t ip = get_ip(); - uint32_t random_num = (rand() % 0xFFFF); + uint32_t pid = getpid(); + uint32_t ip = get_ip(); + uint32_t random_num = (rand() % 0xFFFF); - memcpy(_buff + 2, &pid, 4); - memcpy(_buff, &ip, 4); - memcpy(_buff + 6, &random_num, 4); + memcpy(_buff + 2, &pid, 4); + memcpy(_buff, &ip, 4); + memcpy(_buff + 6, &random_num, 4); - hexdump(_buff, _0x_buff, 10); + hexdump(_buff, _0x_buff, 10); - set_start_and_next_tm(); + set_start_and_next_tm(); } uint32_t StringIdMaker::get_ip() { - char name[1024]; - boost::system::error_code ec; - if (boost::asio::detail::socket_ops::gethostname(name, sizeof(name), ec) != 0) { - return 0; - } - - boost::asio::io_service io_service; - boost::asio::ip::tcp::resolver resolver(io_service); - boost::asio::ip::tcp::resolver::query query(name, ""); - boost::system::error_code error; - boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query, error); - if (error) { - return 0; - } - boost::asio::ip::tcp::resolver::iterator end; // End marker. - boost::asio::ip::tcp::endpoint ep; - while (iter != end) { - ep = *iter++; - } - std::string s_localIpAddress = ep.address().to_string(); - - int a[4]; - std::string IP = s_localIpAddress; - std::string strTemp; - size_t pos; - size_t i = 3; - - do { - pos = IP.find("."); - - if (pos != std::string::npos) { - strTemp = IP.substr(0, pos); - a[i] = atoi(strTemp.c_str()); - i--; - IP.erase(0, pos + 1); - } else { - strTemp = IP; - a[i] = atoi(strTemp.c_str()); - break; + char name[1024]; + boost::system::error_code ec; + if (boost::asio::detail::socket_ops::gethostname(name, sizeof(name), ec) != 0) { + return 0; } - } while (1); - - uint32_t nResult = (a[3] << 24) + (a[2] << 16) + (a[1] << 8) + a[0]; - return nResult; + boost::asio::io_service io_service; + boost::asio::ip::tcp::resolver resolver(io_service); + boost::asio::ip::tcp::resolver::query query(name, ""); + boost::system::error_code error; + boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query, error); + if (error) { + return 0; + } + boost::asio::ip::tcp::resolver::iterator end; // End marker. + boost::asio::ip::tcp::endpoint ep; + while (iter != end) { + ep = *iter++; + } + std::string s_localIpAddress = ep.address().to_string(); + + int a[4]; + std::string IP = s_localIpAddress; + std::string strTemp; + size_t pos; + size_t i = 3; + + do { + pos = IP.find("."); + + if (pos != std::string::npos) { + strTemp = IP.substr(0, pos); + a[i] = atoi(strTemp.c_str()); + i--; + IP.erase(0, pos + 1); + } else { + strTemp = IP; + a[i] = atoi(strTemp.c_str()); + break; + } + + } while (1); + + uint32_t nResult = (a[3] << 24) + (a[2] << 16) + (a[1] << 8) + a[0]; + return nResult; } uint64_t StringIdMaker::get_curr_ms() { - struct timeval time_now; - //windows and linux use the same function name, windows's defination as begining this file -#ifdef WIN32 - gettimeofdayWin(&time_now, NULL); // WIN32 + struct timeval time_now; +// windows and linux use the same function name, windows's defination as begining this file +#ifdef WIN32 + gettimeofdayWin(&time_now, NULL); // WIN32 #else - gettimeofday(&time_now, NULL); //LINUX + gettimeofday(&time_now, NULL); // LINUX #endif - uint64_t ms_time = time_now.tv_sec * 1000 + time_now.tv_usec / 1000; - return ms_time; + uint64_t ms_time = time_now.tv_sec * 1000 + time_now.tv_usec / 1000; + return ms_time; } void StringIdMaker::set_start_and_next_tm() { - time_t tmNow = time(NULL); - tm *ptmNow = localtime(&tmNow); - tm mon_begin; - mon_begin.tm_year = ptmNow->tm_year; - mon_begin.tm_mon = ptmNow->tm_mon; - mon_begin.tm_mday = 0; - mon_begin.tm_hour = 0; - mon_begin.tm_min = 0; - mon_begin.tm_sec = 0; - - tm mon_next_begin; - if (ptmNow->tm_mon == 12) { - mon_next_begin.tm_year = ptmNow->tm_year + 1; - mon_next_begin.tm_mon = 1; - } else { - mon_next_begin.tm_year = ptmNow->tm_year; - mon_next_begin.tm_mon = ptmNow->tm_mon + 1; - } - mon_next_begin.tm_mday = 0; - mon_next_begin.tm_hour = 0; - mon_next_begin.tm_min = 0; - mon_next_begin.tm_sec = 0; - - time_t mon_begin_tm = mktime(&mon_begin); - time_t mon_end_tm = mktime(&mon_next_begin); - - _start_tm = mon_begin_tm * 1000; - _next_start_tm = mon_end_tm * 1000; + time_t tmNow = time(NULL); + tm *ptmNow = localtime(&tmNow); + tm mon_begin; + mon_begin.tm_year = ptmNow->tm_year; + mon_begin.tm_mon = ptmNow->tm_mon; + mon_begin.tm_mday = 0; + mon_begin.tm_hour = 0; + mon_begin.tm_min = 0; + mon_begin.tm_sec = 0; + + tm mon_next_begin; + if (ptmNow->tm_mon == 12) { + mon_next_begin.tm_year = ptmNow->tm_year + 1; + mon_next_begin.tm_mon = 1; + } else { + mon_next_begin.tm_year = ptmNow->tm_year; + mon_next_begin.tm_mon = ptmNow->tm_mon + 1; + } + mon_next_begin.tm_mday = 0; + mon_next_begin.tm_hour = 0; + mon_next_begin.tm_min = 0; + mon_next_begin.tm_sec = 0; + + time_t mon_begin_tm = mktime(&mon_begin); + time_t mon_end_tm = mktime(&mon_next_begin); + + _start_tm = mon_begin_tm * 1000; + _next_start_tm = mon_end_tm * 1000; } int StringIdMaker::atomic_incr(int id) { - #ifdef WIN32 - InterlockedIncrement((LONG*)&id); - #else +#ifdef WIN32 + InterlockedIncrement((LONG *)&id); +#else __sync_add_and_fetch(&id, 1); - #endif - return id; +#endif + return id; } std::string StringIdMaker::get_unique_id() { - uint64_t now_time = get_curr_ms(); + uint64_t now_time = get_curr_ms(); - if (now_time > _next_start_tm) { - set_start_and_next_tm(); - } - uint32_t tm_period = now_time - _start_tm; - seqid = atomic_incr(seqid) & 0xFF; + if (now_time > _next_start_tm) { + set_start_and_next_tm(); + } + uint32_t tm_period = now_time - _start_tm; + seqid = atomic_incr(seqid) & 0xFF; - std::size_t prifix_len = 10; // 10 = prefix len - unsigned char *write_index = _buff + prifix_len; + std::size_t prifix_len = 10; // 10 = prefix len + unsigned char *write_index = _buff + prifix_len; - memcpy(write_index, &tm_period, 4); - write_index += 4; + memcpy(write_index, &tm_period, 4); + write_index += 4; - memcpy(write_index, &seqid, 2); + memcpy(write_index, &seqid, 2); - hexdump(_buff + prifix_len, (_0x_buff + (2 * prifix_len)), 6); - _0x_buff[32] = '\0'; - return std::string(_0x_buff); + hexdump(_buff + prifix_len, (_0x_buff + (2 * prifix_len)), 6); + _0x_buff[32] = '\0'; + return std::string(_0x_buff); } void StringIdMaker::hexdump(unsigned char *buffer, char *out_buff, unsigned long index) { - for (unsigned long i = 0; i < index; i++) { - sprintf(out_buff + 2 * i, "%02X ", buffer[i]); - } + for (unsigned long i = 0; i < index; i++) { + sprintf(out_buff + 2 * i, "%02X ", buffer[i]); + } } } diff --git a/src/producer/StringIdMaker.h b/src/producer/StringIdMaker.h index e95b744a4..d6f056670 100644 --- a/src/producer/StringIdMaker.h +++ b/src/producer/StringIdMaker.h @@ -49,26 +49,26 @@ namespace rocketmq { class StringIdMaker : public boost::serialization::singleton { - public: - StringIdMaker(); - ~StringIdMaker(); - std::string get_unique_id(); +public: + StringIdMaker(); + ~StringIdMaker(); + std::string get_unique_id(); - private: - uint32_t get_ip(); - void init_prefix(); - uint64_t get_curr_ms(); - int atomic_incr(int id); - void set_start_and_next_tm(); +private: + uint32_t get_ip(); + void init_prefix(); + uint64_t get_curr_ms(); + int atomic_incr(int id); + void set_start_and_next_tm(); - void hexdump(unsigned char *buffer, char *out_buff, unsigned long index); + void hexdump(unsigned char *buffer, char *out_buff, unsigned long index); - private: - uint64_t _start_tm; - uint64_t _next_start_tm; - unsigned char _buff[16]; - char _0x_buff[33]; - int16_t seqid; +private: + uint64_t _start_tm; + uint64_t _next_start_tm; + unsigned char _buff[16]; + char _0x_buff[33]; + int16_t seqid; }; } #endif diff --git a/src/producer/TopicPublishInfo.h b/src/producer/TopicPublishInfo.h index 4b4fbb483..704c26135 100644 --- a/src/producer/TopicPublishInfo.h +++ b/src/producer/TopicPublishInfo.h @@ -30,247 +30,225 @@ namespace rocketmq { //interrupt(); - m_async_service_thread->join(); + virtual ~TopicPublishInfo() { + m_async_ioService.stop(); + m_async_service_thread->interrupt(); + m_async_service_thread->join(); - m_nonSerivceQueues.clear(); - m_onSerivceQueues.clear(); - m_brokerTimerMap.clear(); - m_queues.clear(); - } + m_nonSerivceQueues.clear(); + m_onSerivceQueues.clear(); + m_brokerTimerMap.clear(); + m_queues.clear(); + } - bool ok() { - boost::lock_guard lock(m_queuelock); - return !m_queues.empty(); - } + bool ok() { + boost::lock_guard lock(m_queuelock); + return !m_queues.empty(); + } - void updateMessageQueueList(const MQMessageQueue& mq) { - boost::lock_guard lock(m_queuelock); - m_queues.push_back(mq); - string key = mq.getBrokerName() + UtilAll::to_string(mq.getQueueId()); - m_onSerivceQueues[key] = mq; - if (m_nonSerivceQueues.find(key) != m_nonSerivceQueues.end()) { - m_nonSerivceQueues.erase(key); // if topicPublishInfo changed, erase this - // mq from m_nonSerivceQueues to avoid 2 - // copies both in m_onSerivceQueues and - // m_nonSerivceQueues + void updateMessageQueueList(const MQMessageQueue& mq) { + boost::lock_guard lock(m_queuelock); + m_queues.push_back(mq); + string key = mq.getBrokerName() + UtilAll::to_string(mq.getQueueId()); + m_onSerivceQueues[key] = mq; + if (m_nonSerivceQueues.find(key) != m_nonSerivceQueues.end()) { + m_nonSerivceQueues.erase(key); // if topicPublishInfo changed, erase this + // mq from m_nonSerivceQueues to avoid 2 + // copies both in m_onSerivceQueues and + // m_nonSerivceQueues + } } - } - void op_resumeNonServiceMessageQueueList(boost::system::error_code& ec, - boost::asio::deadline_timer* t) { - resumeNonServiceMessageQueueList(); - boost::system::error_code e; - t->expires_from_now(t->expires_from_now() + boost::posix_time::seconds(60), - e); - t->async_wait(boost::bind( - &TopicPublishInfo::op_resumeNonServiceMessageQueueList, this, e, t)); - } + void op_resumeNonServiceMessageQueueList(boost::system::error_code& ec, boost::asio::deadline_timer* t) { + resumeNonServiceMessageQueueList(); + boost::system::error_code e; + t->expires_from_now(t->expires_from_now() + boost::posix_time::seconds(60), e); + t->async_wait(boost::bind(&TopicPublishInfo::op_resumeNonServiceMessageQueueList, this, e, t)); + } - void resumeNonServiceMessageQueueList() { - boost::lock_guard lock(m_queuelock); - for (map::iterator it = m_brokerTimerMap.begin(); - it != m_brokerTimerMap.end(); ++it) { - if (UtilAll::currentTimeMillis() - it->second >= 1000 * 60 * 5) { - string key = it->first.getBrokerName() + - UtilAll::to_string(it->first.getQueueId()); - if (m_nonSerivceQueues.find(key) != m_nonSerivceQueues.end()) { - m_nonSerivceQueues.erase(key); + void resumeNonServiceMessageQueueList() { + boost::lock_guard lock(m_queuelock); + for (map::iterator it = m_brokerTimerMap.begin(); it != m_brokerTimerMap.end(); ++it) { + if (UtilAll::currentTimeMillis() - it->second >= 1000 * 60 * 5) { + string key = it->first.getBrokerName() + UtilAll::to_string(it->first.getQueueId()); + if (m_nonSerivceQueues.find(key) != m_nonSerivceQueues.end()) { + m_nonSerivceQueues.erase(key); + } + m_onSerivceQueues[key] = it->first; + } } - m_onSerivceQueues[key] = it->first; - } } - } - void updateNonServiceMessageQueue(const MQMessageQueue& mq, - int timeoutMilliseconds) { - boost::lock_guard lock(m_queuelock); + void updateNonServiceMessageQueue(const MQMessageQueue& mq, int timeoutMilliseconds) { + boost::lock_guard lock(m_queuelock); - string key = mq.getBrokerName() + UtilAll::to_string(mq.getQueueId()); - if (m_nonSerivceQueues.find(key) != m_nonSerivceQueues.end()) { - return; + string key = mq.getBrokerName() + UtilAll::to_string(mq.getQueueId()); + if (m_nonSerivceQueues.find(key) != m_nonSerivceQueues.end()) { + return; + } + LOG_INFO("updateNonServiceMessageQueue of mq:%s", mq.toString().c_str()); + m_brokerTimerMap[mq] = UtilAll::currentTimeMillis(); + m_nonSerivceQueues[key] = mq; + if (m_onSerivceQueues.find(key) != m_onSerivceQueues.end()) { + m_onSerivceQueues.erase(key); + } } - LOG_INFO("updateNonServiceMessageQueue of mq:%s", mq.toString().c_str()); - m_brokerTimerMap[mq] = UtilAll::currentTimeMillis(); - m_nonSerivceQueues[key] = mq; - if (m_onSerivceQueues.find(key) != m_onSerivceQueues.end()) { - m_onSerivceQueues.erase(key); + + vector& getMessageQueueList() { + boost::lock_guard lock(m_queuelock); + return m_queues; } - } - vector& getMessageQueueList() { - boost::lock_guard lock(m_queuelock); - return m_queues; - } + int getWhichQueue() { return m_sendWhichQueue.load(boost::memory_order_acquire); } - int getWhichQueue() { - return m_sendWhichQueue.load(boost::memory_order_acquire); - } + MQMessageQueue selectOneMessageQueue(const MQMessageQueue& lastmq, int& mq_index) { + boost::lock_guard lock(m_queuelock); - MQMessageQueue selectOneMessageQueue(const MQMessageQueue& lastmq, - int& mq_index) { - boost::lock_guard lock(m_queuelock); + if (m_queues.size() > 0) { + LOG_DEBUG("selectOneMessageQueue Enter, queue size:" SIZET_FMT "", m_queues.size()); + unsigned int pos = 0; + if (mq_index >= 0) { + pos = mq_index % m_queues.size(); + } else { + LOG_ERROR("mq_index is negative"); + return MQMessageQueue(); + } + if (!lastmq.getBrokerName().empty()) { + for (size_t i = 0; i < m_queues.size(); i++) { + if (m_sendWhichQueue.load(boost::memory_order_acquire) == (numeric_limits::max)()) { + m_sendWhichQueue.store(0, boost::memory_order_release); + } - if (m_queues.size() > 0) { - LOG_DEBUG("selectOneMessageQueue Enter, queue size:" SIZET_FMT "", - m_queues.size()); - unsigned int pos = 0; - if (mq_index >= 0) { - pos = mq_index % m_queues.size(); - } else { - LOG_ERROR("mq_index is negative"); - return MQMessageQueue(); - } - if (!lastmq.getBrokerName().empty()) { - for (size_t i = 0; i < m_queues.size(); i++) { - if (m_sendWhichQueue.load(boost::memory_order_acquire) == - (numeric_limits::max)()) { - m_sendWhichQueue.store(0, boost::memory_order_release); - } + if (pos >= m_queues.size()) pos = pos % m_queues.size(); - if (pos >= m_queues.size()) pos = pos % m_queues.size(); + ++m_sendWhichQueue; + MQMessageQueue mq = m_queues.at(pos); + LOG_DEBUG("lastmq broker not empty, m_sendWhichQueue:%d, pos:%d", + m_sendWhichQueue.load(boost::memory_order_acquire), pos); + if (mq.getBrokerName().compare(lastmq.getBrokerName()) != 0) { + mq_index = pos; + return mq; + } + ++pos; + } + LOG_ERROR("could not find property mq"); + return MQMessageQueue(); + } else { + if (m_sendWhichQueue.load(boost::memory_order_acquire) == (numeric_limits::max)()) { + m_sendWhichQueue.store(0, boost::memory_order_release); + } - ++m_sendWhichQueue; - MQMessageQueue mq = m_queues.at(pos); - LOG_DEBUG("lastmq broker not empty, m_sendWhichQueue:%d, pos:%d", - m_sendWhichQueue.load(boost::memory_order_acquire), pos); - if (mq.getBrokerName().compare(lastmq.getBrokerName()) != 0) { - mq_index = pos; - return mq; - } - ++pos; - } - LOG_ERROR("could not find property mq"); - return MQMessageQueue(); - } else { - if (m_sendWhichQueue.load(boost::memory_order_acquire) == - (numeric_limits::max)()) { - m_sendWhichQueue.store(0, boost::memory_order_release); + ++m_sendWhichQueue; + LOG_DEBUG("lastmq broker empty, m_sendWhichQueue:%d, pos:%d", + m_sendWhichQueue.load(boost::memory_order_acquire), pos); + mq_index = pos; + return m_queues.at(pos); + } + } else { + LOG_ERROR("m_queues empty"); + return MQMessageQueue(); } - - ++m_sendWhichQueue; - LOG_DEBUG("lastmq broker empty, m_sendWhichQueue:%d, pos:%d", - m_sendWhichQueue.load(boost::memory_order_acquire), pos); - mq_index = pos; - return m_queues.at(pos); - } - } else { - LOG_ERROR("m_queues empty"); - return MQMessageQueue(); } - } - MQMessageQueue selectOneActiveMessageQueue(const MQMessageQueue& lastmq, - int& mq_index) { - boost::lock_guard lock(m_queuelock); + MQMessageQueue selectOneActiveMessageQueue(const MQMessageQueue& lastmq, int& mq_index) { + boost::lock_guard lock(m_queuelock); - if (m_queues.size() > 0) { - unsigned int pos = 0; - if (mq_index >= 0) { - pos = mq_index % m_queues.size(); - } else { - LOG_ERROR("mq_index is negative"); - return MQMessageQueue(); - } - if (!lastmq.getBrokerName().empty()) { - for (size_t i = 0; i < m_queues.size(); i++) { - if (m_sendWhichQueue.load(boost::memory_order_acquire) == - (numeric_limits::max)()) { - m_sendWhichQueue.store(0, boost::memory_order_release); - } + if (m_queues.size() > 0) { + unsigned int pos = 0; + if (mq_index >= 0) { + pos = mq_index % m_queues.size(); + } else { + LOG_ERROR("mq_index is negative"); + return MQMessageQueue(); + } + if (!lastmq.getBrokerName().empty()) { + for (size_t i = 0; i < m_queues.size(); i++) { + if (m_sendWhichQueue.load(boost::memory_order_acquire) == (numeric_limits::max)()) { + m_sendWhichQueue.store(0, boost::memory_order_release); + } - if (pos >= m_queues.size()) pos = pos % m_queues.size(); + if (pos >= m_queues.size()) pos = pos % m_queues.size(); - ++m_sendWhichQueue; - MQMessageQueue mq = m_queues.at(pos); - string key = mq.getBrokerName() + UtilAll::to_string(mq.getQueueId()); - if ((mq.getBrokerName().compare(lastmq.getBrokerName()) != 0) && - (m_onSerivceQueues.find(key) != m_onSerivceQueues.end())) { - mq_index = pos; - return mq; - } - ++pos; - } + ++m_sendWhichQueue; + MQMessageQueue mq = m_queues.at(pos); + string key = mq.getBrokerName() + UtilAll::to_string(mq.getQueueId()); + if ((mq.getBrokerName().compare(lastmq.getBrokerName()) != 0) && + (m_onSerivceQueues.find(key) != m_onSerivceQueues.end())) { + mq_index = pos; + return mq; + } + ++pos; + } - for (MQMAP::iterator it = m_nonSerivceQueues.begin(); - it != m_nonSerivceQueues.end(); - ++it) { // if no MQMessageQueue(except lastmq) in - // m_onSerivceQueues, search m_nonSerivceQueues - if (it->second.getBrokerName().compare(lastmq.getBrokerName()) != 0) - return it->second; - } - LOG_ERROR("can not find property mq"); - return MQMessageQueue(); - } else { - for (size_t i = 0; i < m_queues.size(); i++) { - if (m_sendWhichQueue.load(boost::memory_order_acquire) == - (numeric_limits::max)()) { - m_sendWhichQueue.store(0, boost::memory_order_release); - } - if (pos >= m_queues.size()) pos = pos % m_queues.size(); + for (MQMAP::iterator it = m_nonSerivceQueues.begin(); it != m_nonSerivceQueues.end(); + ++it) { // if no MQMessageQueue(except lastmq) in + // m_onSerivceQueues, search m_nonSerivceQueues + if (it->second.getBrokerName().compare(lastmq.getBrokerName()) != 0) return it->second; + } + LOG_ERROR("can not find property mq"); + return MQMessageQueue(); + } else { + for (size_t i = 0; i < m_queues.size(); i++) { + if (m_sendWhichQueue.load(boost::memory_order_acquire) == (numeric_limits::max)()) { + m_sendWhichQueue.store(0, boost::memory_order_release); + } + if (pos >= m_queues.size()) pos = pos % m_queues.size(); - ++m_sendWhichQueue; - LOG_DEBUG("lastmq broker empty, m_sendWhichQueue:%d, pos:%d", - m_sendWhichQueue.load(boost::memory_order_acquire), pos); - mq_index = pos; - MQMessageQueue mq = m_queues.at(pos); - string key = mq.getBrokerName() + UtilAll::to_string(mq.getQueueId()); - if (m_onSerivceQueues.find(key) != m_onSerivceQueues.end()) { - return mq; - } else { - ++pos; - } - } + ++m_sendWhichQueue; + LOG_DEBUG("lastmq broker empty, m_sendWhichQueue:%d, pos:%d", + m_sendWhichQueue.load(boost::memory_order_acquire), pos); + mq_index = pos; + MQMessageQueue mq = m_queues.at(pos); + string key = mq.getBrokerName() + UtilAll::to_string(mq.getQueueId()); + if (m_onSerivceQueues.find(key) != m_onSerivceQueues.end()) { + return mq; + } else { + ++pos; + } + } - for (MQMAP::iterator it = m_nonSerivceQueues.begin(); - it != m_nonSerivceQueues.end(); - ++it) { // if no MQMessageQueue(except lastmq) in - // m_onSerivceQueues, search m_nonSerivceQueues - if (it->second.getBrokerName().compare(lastmq.getBrokerName()) != 0) - return it->second; + for (MQMAP::iterator it = m_nonSerivceQueues.begin(); it != m_nonSerivceQueues.end(); + ++it) { // if no MQMessageQueue(except lastmq) in + // m_onSerivceQueues, search m_nonSerivceQueues + if (it->second.getBrokerName().compare(lastmq.getBrokerName()) != 0) return it->second; + } + LOG_ERROR("can not find property mq"); + return MQMessageQueue(); + } + } else { + LOG_ERROR("m_queues empty"); + return MQMessageQueue(); } - LOG_ERROR("can not find property mq"); - return MQMessageQueue(); - } - } else { - LOG_ERROR("m_queues empty"); - return MQMessageQueue(); } - } - private: - boost::mutex m_queuelock; - typedef vector QueuesVec; - QueuesVec m_queues; - typedef map MQMAP; - MQMAP m_onSerivceQueues; - MQMAP m_nonSerivceQueues; - boost::atomic m_sendWhichQueue; - map m_brokerTimerMap; - boost::asio::io_service m_async_ioService; - boost::scoped_ptr m_async_service_thread; +private: + boost::mutex m_queuelock; + typedef vector QueuesVec; + QueuesVec m_queues; + typedef map MQMAP; + MQMAP m_onSerivceQueues; + MQMAP m_nonSerivceQueues; + boost::atomic m_sendWhichQueue; + map m_brokerTimerMap; + boost::asio::io_service m_async_ioService; + boost::scoped_ptr m_async_service_thread; }; //& requestMap) { - requestMap.insert(pair("topic", topic)); +void GetRouteInfoRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("topic", topic)); } //& requestMap) { - requestMap.insert(pair("clientID", clientID)); - requestMap.insert(pair("producerGroup", producerGroup)); - requestMap.insert(pair("consumerGroup", consumerGroup)); +void UnregisterClientRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("clientID", clientID)); + requestMap.insert(pair("producerGroup", producerGroup)); + requestMap.insert(pair("consumerGroup", consumerGroup)); } //& requestMap) { - requestMap.insert(pair("topic", topic)); - requestMap.insert(pair("defaultTopic", defaultTopic)); - requestMap.insert( - pair("readQueueNums", UtilAll::to_string(readQueueNums))); - requestMap.insert(pair("writeQueueNums", - UtilAll::to_string(writeQueueNums))); - requestMap.insert(pair("perm", UtilAll::to_string(perm))); - requestMap.insert(pair("topicFilterType", topicFilterType)); + outData["topic"] = topic; + outData["defaultTopic"] = defaultTopic; + outData["readQueueNums"] = readQueueNums; + outData["writeQueueNums"] = writeQueueNums; + outData["perm"] = perm; + outData["topicFilterType"] = topicFilterType; +} +void CreateTopicRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("topic", topic)); + requestMap.insert(pair("defaultTopic", defaultTopic)); + requestMap.insert(pair("readQueueNums", UtilAll::to_string(readQueueNums))); + requestMap.insert(pair("writeQueueNums", UtilAll::to_string(writeQueueNums))); + requestMap.insert(pair("perm", UtilAll::to_string(perm))); + requestMap.insert(pair("topicFilterType", topicFilterType)); } //& requestMap) { - LOG_DEBUG( - "SendMessageRequestHeader producerGroup is:%s,topic is:%s, defaulttopic " - "is:%s, properties is:%s,UtilAll::to_string( defaultTopicQueueNums) " - "is:%s,UtilAll::to_string( queueId):%s, UtilAll::to_string( sysFlag) " - "is:%s, UtilAll::to_string( bornTimestamp) is:%s,UtilAll::to_string( " - "flag) is:%s", - producerGroup.c_str(), topic.c_str(), defaultTopic.c_str(), - properties.c_str(), UtilAll::to_string(defaultTopicQueueNums).c_str(), - UtilAll::to_string(queueId).c_str(), UtilAll::to_string(sysFlag).c_str(), - UtilAll::to_string(bornTimestamp).c_str(), - UtilAll::to_string(flag).c_str()); - - requestMap.insert(pair("producerGroup", producerGroup)); - requestMap.insert(pair("topic", topic)); - requestMap.insert(pair("defaultTopic", defaultTopic)); - requestMap.insert(pair( - "defaultTopicQueueNums", UtilAll::to_string(defaultTopicQueueNums))); - requestMap.insert( - pair("queueId", UtilAll::to_string(queueId))); - requestMap.insert( - pair("sysFlag", UtilAll::to_string(sysFlag))); - requestMap.insert( - pair("bornTimestamp", UtilAll::to_string(bornTimestamp))); - requestMap.insert(pair("flag", UtilAll::to_string(flag))); - requestMap.insert(pair("properties", properties)); +void SendMessageRequestHeader::setReconsumeTimes(int input_reconsumeTimes) { reconsumeTimes = input_reconsumeTimes; } + +void SendMessageRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + LOG_DEBUG( + "SendMessageRequestHeader producerGroup is:%s,topic is:%s, defaulttopic " + "is:%s, properties is:%s,UtilAll::to_string( defaultTopicQueueNums) " + "is:%s,UtilAll::to_string( queueId):%s, UtilAll::to_string( sysFlag) " + "is:%s, UtilAll::to_string( bornTimestamp) is:%s,UtilAll::to_string( " + "flag) is:%s", + producerGroup.c_str(), topic.c_str(), defaultTopic.c_str(), properties.c_str(), + UtilAll::to_string(defaultTopicQueueNums).c_str(), UtilAll::to_string(queueId).c_str(), + UtilAll::to_string(sysFlag).c_str(), UtilAll::to_string(bornTimestamp).c_str(), + UtilAll::to_string(flag).c_str()); + + requestMap.insert(pair("producerGroup", producerGroup)); + requestMap.insert(pair("topic", topic)); + requestMap.insert(pair("defaultTopic", defaultTopic)); + requestMap.insert(pair("defaultTopicQueueNums", UtilAll::to_string(defaultTopicQueueNums))); + requestMap.insert(pair("queueId", UtilAll::to_string(queueId))); + requestMap.insert(pair("sysFlag", UtilAll::to_string(sysFlag))); + requestMap.insert(pair("bornTimestamp", UtilAll::to_string(bornTimestamp))); + requestMap.insert(pair("flag", UtilAll::to_string(flag))); + requestMap.insert(pair("properties", properties)); #ifdef ONS - requestMap.insert(pair("reconsumeTimes", - UtilAll::to_string(reconsumeTimes))); - requestMap.insert( - pair("unitMode", UtilAll::to_string(unitMode))); + requestMap.insert(pair("reconsumeTimes", UtilAll::to_string(reconsumeTimes))); + requestMap.insert(pair("unitMode", UtilAll::to_string(unitMode))); #endif - requestMap.insert(pair("batch", UtilAll::to_string(batch))); + requestMap.insert(pair("batch", UtilAll::to_string(batch))); } //msgId = tempValue.asString(); - } + Json::Value& tempValue = ext["msgId"]; + if (tempValue.isString()) { + h->msgId = tempValue.asString(); + } - tempValue = ext["queueId"]; - if (tempValue.isString()) { - h->queueId = atoi(tempValue.asCString()); - } + tempValue = ext["queueId"]; + if (tempValue.isString()) { + h->queueId = atoi(tempValue.asCString()); + } - tempValue = ext["queueOffset"]; - if (tempValue.isString()) { - h->queueOffset = UtilAll::str2ll(tempValue.asCString()); - } - return h; + tempValue = ext["queueOffset"]; + if (tempValue.isString()) { + h->queueOffset = UtilAll::str2ll(tempValue.asCString()); + } + return h; } -void SendMessageResponseHeader::SetDeclaredFieldOfCommandHeader( - map& requestMap) { - requestMap.insert(pair("msgId", msgId)); - requestMap.insert( - pair("queueId", UtilAll::to_string(queueId))); - requestMap.insert( - pair("queueOffset", UtilAll::to_string(queueOffset))); +void SendMessageResponseHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("msgId", msgId)); + requestMap.insert(pair("queueId", UtilAll::to_string(queueId))); + requestMap.insert(pair("queueOffset", UtilAll::to_string(queueOffset))); } //& requestMap) { - requestMap.insert(pair("consumerGroup", consumerGroup)); - requestMap.insert(pair("topic", topic)); - requestMap.insert( - pair("queueId", UtilAll::to_string(queueId))); - requestMap.insert( - pair("queueOffset", UtilAll::to_string(queueOffset))); - requestMap.insert( - pair("maxMsgNums", UtilAll::to_string(maxMsgNums))); - requestMap.insert( - pair("sysFlag", UtilAll::to_string(sysFlag))); - requestMap.insert( - pair("commitOffset", UtilAll::to_string(commitOffset))); - requestMap.insert( - pair("subVersion", UtilAll::to_string(subVersion))); - requestMap.insert(pair( - "suspendTimeoutMillis", UtilAll::to_string(suspendTimeoutMillis))); - requestMap.insert(pair("subscription", subscription)); + outData["consumerGroup"] = consumerGroup; + outData["topic"] = topic; + outData["queueId"] = queueId; + outData["queueOffset"] = UtilAll::to_string(queueOffset); + ; + outData["maxMsgNums"] = maxMsgNums; + outData["sysFlag"] = sysFlag; + outData["commitOffset"] = UtilAll::to_string(commitOffset); + ; + outData["subVersion"] = UtilAll::to_string(subVersion); + ; + outData["suspendTimeoutMillis"] = UtilAll::to_string(suspendTimeoutMillis); + ; + outData["subscription"] = subscription; +} + +void PullMessageRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("consumerGroup", consumerGroup)); + requestMap.insert(pair("topic", topic)); + requestMap.insert(pair("queueId", UtilAll::to_string(queueId))); + requestMap.insert(pair("queueOffset", UtilAll::to_string(queueOffset))); + requestMap.insert(pair("maxMsgNums", UtilAll::to_string(maxMsgNums))); + requestMap.insert(pair("sysFlag", UtilAll::to_string(sysFlag))); + requestMap.insert(pair("commitOffset", UtilAll::to_string(commitOffset))); + requestMap.insert(pair("subVersion", UtilAll::to_string(subVersion))); + requestMap.insert(pair("suspendTimeoutMillis", UtilAll::to_string(suspendTimeoutMillis))); + requestMap.insert(pair("subscription", subscription)); } //suggestWhichBrokerId = UtilAll::str2ll(tempValue.asCString()); - } - - tempValue = ext["nextBeginOffset"]; - if (tempValue.isString()) { - h->nextBeginOffset = UtilAll::str2ll(tempValue.asCString()); - } - - tempValue = ext["minOffset"]; - if (tempValue.isString()) { - h->minOffset = UtilAll::str2ll(tempValue.asCString()); - } - - tempValue = ext["maxOffset"]; - if (tempValue.isString()) { - h->maxOffset = UtilAll::str2ll(tempValue.asCString()); - } - - return h; -} - -void PullMessageResponseHeader::SetDeclaredFieldOfCommandHeader( - map& requestMap) { - requestMap.insert(pair( - "suggestWhichBrokerId", UtilAll::to_string(suggestWhichBrokerId))); - requestMap.insert(pair("nextBeginOffset", - UtilAll::to_string(nextBeginOffset))); - requestMap.insert( - pair("minOffset", UtilAll::to_string(minOffset))); - requestMap.insert( - pair("maxOffset", UtilAll::to_string(maxOffset))); + PullMessageResponseHeader* h = new PullMessageResponseHeader(); + + Json::Value& tempValue = ext["suggestWhichBrokerId"]; + if (tempValue.isString()) { + h->suggestWhichBrokerId = UtilAll::str2ll(tempValue.asCString()); + } + + tempValue = ext["nextBeginOffset"]; + if (tempValue.isString()) { + h->nextBeginOffset = UtilAll::str2ll(tempValue.asCString()); + } + + tempValue = ext["minOffset"]; + if (tempValue.isString()) { + h->minOffset = UtilAll::str2ll(tempValue.asCString()); + } + + tempValue = ext["maxOffset"]; + if (tempValue.isString()) { + h->maxOffset = UtilAll::str2ll(tempValue.asCString()); + } + + return h; +} + +void PullMessageResponseHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("suggestWhichBrokerId", UtilAll::to_string(suggestWhichBrokerId))); + requestMap.insert(pair("nextBeginOffset", UtilAll::to_string(nextBeginOffset))); + requestMap.insert(pair("minOffset", UtilAll::to_string(minOffset))); + requestMap.insert(pair("maxOffset", UtilAll::to_string(maxOffset))); } //& requestMap) {} +void GetConsumerListByGroupResponseHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) {} //& requestMap) { - requestMap.insert(pair("topic", topic)); - requestMap.insert( - pair("queueId", UtilAll::to_string(queueId))); +void GetMinOffsetRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("topic", topic)); + requestMap.insert(pair("queueId", UtilAll::to_string(queueId))); } //offset = UtilAll::str2ll(tempValue.asCString()); - } - return h; + Json::Value& tempValue = ext["offset"]; + if (tempValue.isString()) { + h->offset = UtilAll::str2ll(tempValue.asCString()); + } + return h; } -void GetMinOffsetResponseHeader::SetDeclaredFieldOfCommandHeader( - map& requestMap) { - requestMap.insert(pair("offset", UtilAll::to_string(offset))); +void GetMinOffsetResponseHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("offset", UtilAll::to_string(offset))); } //& requestMap) { - requestMap.insert(pair("topic", topic)); - requestMap.insert( - pair("queueId", UtilAll::to_string(queueId))); +void GetMaxOffsetRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("topic", topic)); + requestMap.insert(pair("queueId", UtilAll::to_string(queueId))); } //offset = UtilAll::str2ll(tempValue.asCString()); - } - return h; + Json::Value& tempValue = ext["offset"]; + if (tempValue.isString()) { + h->offset = UtilAll::str2ll(tempValue.asCString()); + } + return h; } -void GetMaxOffsetResponseHeader::SetDeclaredFieldOfCommandHeader( - map& requestMap) { - requestMap.insert(pair("offset", UtilAll::to_string(offset))); +void GetMaxOffsetResponseHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("offset", UtilAll::to_string(offset))); } //& requestMap) { - requestMap.insert(pair("topic", topic)); - requestMap.insert( - pair("queueId", UtilAll::to_string(queueId))); - requestMap.insert( - pair("timestamp", UtilAll::to_string(timestamp))); +void SearchOffsetRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("topic", topic)); + requestMap.insert(pair("queueId", UtilAll::to_string(queueId))); + requestMap.insert(pair("timestamp", UtilAll::to_string(timestamp))); } //offset = UtilAll::str2ll(tempValue.asCString()); - } - return h; + Json::Value& tempValue = ext["offset"]; + if (tempValue.isString()) { + h->offset = UtilAll::str2ll(tempValue.asCString()); + } + return h; } -void SearchOffsetResponseHeader::SetDeclaredFieldOfCommandHeader( - map& requestMap) { - requestMap.insert(pair("offset", UtilAll::to_string(offset))); +void SearchOffsetResponseHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("offset", UtilAll::to_string(offset))); } //& requestMap) { - requestMap.insert(pair("offset", UtilAll::to_string(offset))); +void ViewMessageRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("offset", UtilAll::to_string(offset))); } //& requestMap) { - requestMap.insert(pair("topic", topic)); - requestMap.insert( - pair("queueId", UtilAll::to_string(queueId))); +void GetEarliestMsgStoretimeRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("topic", topic)); + requestMap.insert(pair("queueId", UtilAll::to_string(queueId))); } //timestamp = UtilAll::str2ll(tempValue.asCString()); - } - return h; + Json::Value& tempValue = ext["timestamp"]; + if (tempValue.isString()) { + h->timestamp = UtilAll::str2ll(tempValue.asCString()); + } + return h; } -void GetEarliestMsgStoretimeResponseHeader::SetDeclaredFieldOfCommandHeader( - map& requestMap) { - requestMap.insert( - pair("timestamp", UtilAll::to_string(timestamp))); +void GetEarliestMsgStoretimeResponseHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("timestamp", UtilAll::to_string(timestamp))); } //& requestMap) { - requestMap.insert(pair("consumerGroup", consumerGroup)); +void GetConsumerListByGroupRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("consumerGroup", consumerGroup)); } //& requestMap) { - requestMap.insert(pair("consumerGroup", consumerGroup)); - requestMap.insert(pair("topic", topic)); - requestMap.insert( - pair("queueId", UtilAll::to_string(queueId))); +void QueryConsumerOffsetRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("consumerGroup", consumerGroup)); + requestMap.insert(pair("topic", topic)); + requestMap.insert(pair("queueId", UtilAll::to_string(queueId))); } //offset = UtilAll::str2ll(tempValue.asCString()); - } - return h; +CommandHeader* QueryConsumerOffsetResponseHeader::Decode(Json::Value& ext) { + QueryConsumerOffsetResponseHeader* h = new QueryConsumerOffsetResponseHeader(); + Json::Value& tempValue = ext["offset"]; + if (tempValue.isString()) { + h->offset = UtilAll::str2ll(tempValue.asCString()); + } + return h; } -void QueryConsumerOffsetResponseHeader::SetDeclaredFieldOfCommandHeader( - map& requestMap) { - requestMap.insert(pair("offset", UtilAll::to_string(offset))); +void QueryConsumerOffsetResponseHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("offset", UtilAll::to_string(offset))); } //& requestMap) { - requestMap.insert(pair("consumerGroup", consumerGroup)); - requestMap.insert(pair("topic", topic)); - requestMap.insert( - pair("queueId", UtilAll::to_string(queueId))); - requestMap.insert( - pair("commitOffset", UtilAll::to_string(commitOffset))); +void UpdateConsumerOffsetRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("consumerGroup", consumerGroup)); + requestMap.insert(pair("topic", topic)); + requestMap.insert(pair("queueId", UtilAll::to_string(queueId))); + requestMap.insert(pair("commitOffset", UtilAll::to_string(commitOffset))); } //& requestMap) { - requestMap.insert(pair("group", group)); - requestMap.insert( - pair("delayLevel", UtilAll::to_string(delayLevel))); - requestMap.insert(pair("offset", UtilAll::to_string(offset))); +void ConsumerSendMsgBackRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("group", group)); + requestMap.insert(pair("delayLevel", UtilAll::to_string(delayLevel))); + requestMap.insert(pair("offset", UtilAll::to_string(offset))); } //& cids) { - cids.clear(); - //(mem->getData()); - - Json::Reader reader; - Json::Value root; - if (!reader.parse(pData, root)) { - LOG_ERROR("GetConsumerListByGroupResponse error"); - return; - } - - Json::Value ids = root["consumerIdList"]; - for (unsigned int i = 0; i < ids.size(); i++) { - if (ids[i].isString()) { - cids.push_back(ids[i].asString()); +void GetConsumerListByGroupResponseBody::Decode(const MemoryBlock* mem, vector& cids) { + cids.clear(); + //(mem->getData()); + + Json::Reader reader; + Json::Value root; + if (!reader.parse(pData, root)) { + LOG_ERROR("GetConsumerListByGroupResponse error"); + return; + } + + Json::Value ids = root["consumerIdList"]; + for (unsigned int i = 0; i < ids.size(); i++) { + if (ids[i].isString()) { + cids.push_back(ids[i].asString()); + } } - } } -void GetConsumerListByGroupResponseBody::SetDeclaredFieldOfCommandHeader( - map& requestMap) {} +void GetConsumerListByGroupResponseBody::SetDeclaredFieldOfCommandHeader(map& requestMap) {} void ResetOffsetRequestHeader::setTopic(const string& tmp) { topic = tmp; } void ResetOffsetRequestHeader::setGroup(const string& tmp) { group = tmp; } -void ResetOffsetRequestHeader::setTimeStamp(const int64& tmp) { - timestamp = tmp; -} +void ResetOffsetRequestHeader::setTimeStamp(const int64& tmp) { timestamp = tmp; } void ResetOffsetRequestHeader::setForceFlag(const bool& tmp) { isForce = tmp; } @@ -478,117 +408,93 @@ const int64 ResetOffsetRequestHeader::getTimeStamp() const { return timestamp; } const bool ResetOffsetRequestHeader::getForceFlag() const { return isForce; } CommandHeader* ResetOffsetRequestHeader::Decode(Json::Value& ext) { - ResetOffsetRequestHeader* h = new ResetOffsetRequestHeader(); - - Json::Value& tempValue = ext["topic"]; - if (tempValue.isString()) { - h->topic = tempValue.asString(); - } - - tempValue = ext["group"]; - if (tempValue.isString()) { - h->group = tempValue.asString(); - } - - tempValue = ext["timestamp"]; - if (tempValue.isString()) { - h->timestamp = UtilAll::str2ll(tempValue.asCString()); - } - - tempValue = ext["isForce"]; - if (tempValue.isString()) { - h->isForce = UtilAll::to_bool(tempValue.asCString()); - } - LOG_INFO("topic:%s, group:%s, timestamp:%lld, isForce:%d,isForce:%s", - h->topic.c_str(), h->group.c_str(), h->timestamp, h->isForce, - tempValue.asCString()); - return h; -} - -CommandHeader* GetConsumerRunningInfoRequestHeader::Decode( - Json::Value& ext) { - GetConsumerRunningInfoRequestHeader* h = - new GetConsumerRunningInfoRequestHeader(); - - Json::Value& tempValue = ext["consumerGroup"]; - if (tempValue.isString()) { - h->consumerGroup = tempValue.asString(); - } - - tempValue = ext["clientId"]; - if (tempValue.isString()) { - h->clientId = tempValue.asString(); - } - - tempValue = ext["jstackEnable"]; - if (tempValue.isString()) { - h->jstackEnable = UtilAll::to_bool(tempValue.asCString()); - } - LOG_INFO("consumerGroup:%s, clientId:%s, jstackEnable:%d", - h->consumerGroup.c_str(), h->clientId.c_str(), h->jstackEnable); - return h; -} + ResetOffsetRequestHeader* h = new ResetOffsetRequestHeader(); -void GetConsumerRunningInfoRequestHeader::Encode(Json::Value& outData) { - outData["consumerGroup"] = consumerGroup; - outData["clientId"] = clientId; - outData["jstackEnable"] = jstackEnable; -} + Json::Value& tempValue = ext["topic"]; + if (tempValue.isString()) { + h->topic = tempValue.asString(); + } -void GetConsumerRunningInfoRequestHeader::SetDeclaredFieldOfCommandHeader( - map& requestMap) { - requestMap.insert(pair("consumerGroup", consumerGroup)); - requestMap.insert(pair("clientId", clientId)); - requestMap.insert( - pair("jstackEnable", UtilAll::to_string(jstackEnable))); -} + tempValue = ext["group"]; + if (tempValue.isString()) { + h->group = tempValue.asString(); + } -const string GetConsumerRunningInfoRequestHeader::getConsumerGroup() const { - return consumerGroup; -} + tempValue = ext["timestamp"]; + if (tempValue.isString()) { + h->timestamp = UtilAll::str2ll(tempValue.asCString()); + } -void GetConsumerRunningInfoRequestHeader::setConsumerGroup( - const string& Group) { - consumerGroup = Group; + tempValue = ext["isForce"]; + if (tempValue.isString()) { + h->isForce = UtilAll::to_bool(tempValue.asCString()); + } + LOG_INFO("topic:%s, group:%s, timestamp:%lld, isForce:%d,isForce:%s", h->topic.c_str(), h->group.c_str(), + h->timestamp, h->isForce, tempValue.asCString()); + return h; } -const string GetConsumerRunningInfoRequestHeader::getClientId() const { - return clientId; -} +CommandHeader* GetConsumerRunningInfoRequestHeader::Decode(Json::Value& ext) { + GetConsumerRunningInfoRequestHeader* h = new GetConsumerRunningInfoRequestHeader(); + + Json::Value& tempValue = ext["consumerGroup"]; + if (tempValue.isString()) { + h->consumerGroup = tempValue.asString(); + } -void GetConsumerRunningInfoRequestHeader::setClientId( - const string& input_clientId) { - clientId = input_clientId; + tempValue = ext["clientId"]; + if (tempValue.isString()) { + h->clientId = tempValue.asString(); + } + + tempValue = ext["jstackEnable"]; + if (tempValue.isString()) { + h->jstackEnable = UtilAll::to_bool(tempValue.asCString()); + } + LOG_INFO("consumerGroup:%s, clientId:%s, jstackEnable:%d", h->consumerGroup.c_str(), h->clientId.c_str(), + h->jstackEnable); + return h; } -const bool GetConsumerRunningInfoRequestHeader::isJstackEnable() const { - return jstackEnable; +void GetConsumerRunningInfoRequestHeader::Encode(Json::Value& outData) { + outData["consumerGroup"] = consumerGroup; + outData["clientId"] = clientId; + outData["jstackEnable"] = jstackEnable; } -void GetConsumerRunningInfoRequestHeader::setJstackEnable( - const bool& input_jstackEnable) { - jstackEnable = input_jstackEnable; +void GetConsumerRunningInfoRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("consumerGroup", consumerGroup)); + requestMap.insert(pair("clientId", clientId)); + requestMap.insert(pair("jstackEnable", UtilAll::to_string(jstackEnable))); } -CommandHeader* NotifyConsumerIdsChangedRequestHeader::Decode( - Json::Value& ext) { - NotifyConsumerIdsChangedRequestHeader* h = - new NotifyConsumerIdsChangedRequestHeader(); +const string GetConsumerRunningInfoRequestHeader::getConsumerGroup() const { return consumerGroup; } - Json::Value& tempValue = ext["consumerGroup"]; - if (tempValue.isString()) { - h->consumerGroup = tempValue.asString(); - } +void GetConsumerRunningInfoRequestHeader::setConsumerGroup(const string& Group) { consumerGroup = Group; } - return h; -} +const string GetConsumerRunningInfoRequestHeader::getClientId() const { return clientId; } + +void GetConsumerRunningInfoRequestHeader::setClientId(const string& input_clientId) { clientId = input_clientId; } -void NotifyConsumerIdsChangedRequestHeader::setGroup(const string& tmp) { - consumerGroup = tmp; +const bool GetConsumerRunningInfoRequestHeader::isJstackEnable() const { return jstackEnable; } + +void GetConsumerRunningInfoRequestHeader::setJstackEnable(const bool& input_jstackEnable) { + jstackEnable = input_jstackEnable; } -const string NotifyConsumerIdsChangedRequestHeader::getGroup() const { - return consumerGroup; + +CommandHeader* NotifyConsumerIdsChangedRequestHeader::Decode(Json::Value& ext) { + NotifyConsumerIdsChangedRequestHeader* h = new NotifyConsumerIdsChangedRequestHeader(); + + Json::Value& tempValue = ext["consumerGroup"]; + if (tempValue.isString()) { + h->consumerGroup = tempValue.asString(); + } + + return h; } +void NotifyConsumerIdsChangedRequestHeader::setGroup(const string& tmp) { consumerGroup = tmp; } +const string NotifyConsumerIdsChangedRequestHeader::getGroup() const { return consumerGroup; } + //& requestMap) {} +public: + virtual ~CommandHeader() {} + virtual void Encode(Json::Value& outData) {} + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap) {} }; //& requestMap); - - private: - string topic; +public: + GetRouteInfoRequestHeader(const string& top) : topic(top) {} + virtual ~GetRouteInfoRequestHeader() {} + virtual void Encode(Json::Value& outData); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +private: + string topic; }; //& requestMap); - - private: - string clientID; - string producerGroup; - string consumerGroup; +public: + UnregisterClientRequestHeader(string cID, string proGroup, string conGroup) + : clientID(cID), producerGroup(proGroup), consumerGroup(conGroup) {} + virtual ~UnregisterClientRequestHeader() {} + virtual void Encode(Json::Value& outData); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +private: + string clientID; + string producerGroup; + string consumerGroup; }; //& requestMap); - - public: - string topic; - string defaultTopic; - int readQueueNums; - int writeQueueNums; - int perm; - string topicFilterType; +public: + CreateTopicRequestHeader() : readQueueNums(0), writeQueueNums(0), perm(0) {} + virtual ~CreateTopicRequestHeader() {} + virtual void Encode(Json::Value& outData); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +public: + string topic; + string defaultTopic; + int readQueueNums; + int writeQueueNums; + int perm; + string topicFilterType; }; //& requestMap); - int getReconsumeTimes(); - void setReconsumeTimes(int input_reconsumeTimes); - - public: - string producerGroup; - string topic; - string defaultTopic; - int defaultTopicQueueNums; - int queueId; - int sysFlag; - int64 bornTimestamp; - int flag; - string properties; - int reconsumeTimes; - bool unitMode; - bool batch; +public: + SendMessageRequestHeader() + : defaultTopicQueueNums(0), + queueId(0), + sysFlag(0), + bornTimestamp(0), + flag(0), + reconsumeTimes(0), + unitMode(false), + batch(false) {} + virtual ~SendMessageRequestHeader() {} + virtual void Encode(Json::Value& outData); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + int getReconsumeTimes(); + void setReconsumeTimes(int input_reconsumeTimes); + +public: + string producerGroup; + string topic; + string defaultTopic; + int defaultTopicQueueNums; + int queueId; + int sysFlag; + int64 bornTimestamp; + int flag; + string properties; + int reconsumeTimes; + bool unitMode; + bool batch; }; //& requestMap); - - public: - string msgId; - int queueId; - int64 queueOffset; +public: + SendMessageResponseHeader() : queueId(0), queueOffset(0) { msgId.clear(); } + virtual ~SendMessageResponseHeader() {} + static CommandHeader* Decode(Json::Value& ext); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +public: + string msgId; + int queueId; + int64 queueOffset; }; //& requestMap); - - public: - string consumerGroup; - string topic; - int queueId; - int maxMsgNums; - int sysFlag; - string subscription; - int64 queueOffset; - int64 commitOffset; - int64 suspendTimeoutMillis; - int64 subVersion; +public: + PullMessageRequestHeader() + : queueId(0), + maxMsgNums(0), + sysFlag(0), + queueOffset(0), + commitOffset(0), + suspendTimeoutMillis(0), + subVersion(0) {} + virtual ~PullMessageRequestHeader() {} + virtual void Encode(Json::Value& outData); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +public: + string consumerGroup; + string topic; + int queueId; + int maxMsgNums; + int sysFlag; + string subscription; + int64 queueOffset; + int64 commitOffset; + int64 suspendTimeoutMillis; + int64 subVersion; }; //& requestMap); - - public: - int64 suggestWhichBrokerId; - int64 nextBeginOffset; - int64 minOffset; - int64 maxOffset; +public: + PullMessageResponseHeader() : suggestWhichBrokerId(0), nextBeginOffset(0), minOffset(0), maxOffset(0) {} + virtual ~PullMessageResponseHeader() {} + static CommandHeader* Decode(Json::Value& ext); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +public: + int64 suggestWhichBrokerId; + int64 nextBeginOffset; + int64 minOffset; + int64 maxOffset; }; //& requestMap); +public: + GetConsumerListByGroupResponseHeader() {} + virtual ~GetConsumerListByGroupResponseHeader() {} + virtual void Encode(Json::Value& outData); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); }; //& requestMap); - - public: - string topic; - int queueId; +public: + GetMinOffsetRequestHeader() : queueId(0) {}; + virtual ~GetMinOffsetRequestHeader() {} + virtual void Encode(Json::Value& outData); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +public: + string topic; + int queueId; }; //& requestMap); - - public: - int64 offset; +public: + GetMinOffsetResponseHeader() : offset(0) {}; + virtual ~GetMinOffsetResponseHeader() {} + static CommandHeader* Decode(Json::Value& ext); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +public: + int64 offset; }; //& requestMap); - - public: - string topic; - int queueId; +public: + GetMaxOffsetRequestHeader() : queueId(0) {}; + virtual ~GetMaxOffsetRequestHeader() {} + virtual void Encode(Json::Value& outData); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +public: + string topic; + int queueId; }; //& requestMap); - - public: - int64 offset; +public: + GetMaxOffsetResponseHeader() : offset(0) {}; + virtual ~GetMaxOffsetResponseHeader() {} + static CommandHeader* Decode(Json::Value& ext); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +public: + int64 offset; }; //& requestMap); - - public: - string topic; - int queueId; - int64 timestamp; +public: + SearchOffsetRequestHeader() : queueId(0), timestamp(0) {}; + virtual ~SearchOffsetRequestHeader() {} + virtual void Encode(Json::Value& outData); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +public: + string topic; + int queueId; + int64 timestamp; }; //& requestMap); - - public: - int64 offset; +public: + SearchOffsetResponseHeader() : offset(0) {}; + virtual ~SearchOffsetResponseHeader() {} + static CommandHeader* Decode(Json::Value& ext); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +public: + int64 offset; }; //& requestMap); - - public: - int64 offset; +public: + ViewMessageRequestHeader() : offset(0) {}; + virtual ~ViewMessageRequestHeader() {} + virtual void Encode(Json::Value& outData); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +public: + int64 offset; }; //& requestMap); - - public: - string topic; - int queueId; +public: + GetEarliestMsgStoretimeRequestHeader() : queueId(0) {}; + virtual ~GetEarliestMsgStoretimeRequestHeader() {} + virtual void Encode(Json::Value& outData); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +public: + string topic; + int queueId; }; //& requestMap); - - public: - int64 timestamp; +public: + GetEarliestMsgStoretimeResponseHeader() : timestamp(0) {}; + virtual ~GetEarliestMsgStoretimeResponseHeader() {} + static CommandHeader* Decode(Json::Value& ext); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +public: + int64 timestamp; }; //& requestMap); - - public: - string consumerGroup; +public: + GetConsumerListByGroupRequestHeader() {}; + virtual ~GetConsumerListByGroupRequestHeader() {} + virtual void Encode(Json::Value& outData); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +public: + string consumerGroup; }; //& requestMap); - - public: - string consumerGroup; - string topic; - int queueId; +public: + QueryConsumerOffsetRequestHeader() : queueId(0) {}; + virtual ~QueryConsumerOffsetRequestHeader() {} + virtual void Encode(Json::Value& outData); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +public: + string consumerGroup; + string topic; + int queueId; }; //& requestMap); - - public: - int64 offset; +public: + QueryConsumerOffsetResponseHeader() : offset(0) {}; + virtual ~QueryConsumerOffsetResponseHeader() {} + static CommandHeader* Decode(Json::Value& ext); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +public: + int64 offset; }; //& requestMap); - - public: - string consumerGroup; - string topic; - int queueId; - int64 commitOffset; +public: + UpdateConsumerOffsetRequestHeader() : queueId(0), commitOffset(0) {}; + virtual ~UpdateConsumerOffsetRequestHeader() {} + virtual void Encode(Json::Value& outData); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +public: + string consumerGroup; + string topic; + int queueId; + int64 commitOffset; }; //& requestMap); - - public: - string group; - int delayLevel; - int64 offset; +public: + ConsumerSendMsgBackRequestHeader() : delayLevel(0), offset(0) {}; + virtual ~ConsumerSendMsgBackRequestHeader() {} + virtual void Encode(Json::Value& outData); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + +public: + string group; + int delayLevel; + int64 offset; }; //& requestMap); +public: + GetConsumerListByGroupResponseBody() {}; + virtual ~GetConsumerListByGroupResponseBody() {} + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); - public: - static void Decode(const MemoryBlock* mem, vector& cids); +public: + static void Decode(const MemoryBlock* mem, vector& cids); }; class ResetOffsetRequestHeader : public CommandHeader { - public: - ResetOffsetRequestHeader() {} - ~ResetOffsetRequestHeader() {} - static CommandHeader* Decode(Json::Value& ext); - void setTopic(const string& tmp); - void setGroup(const string& tmp); - void setTimeStamp(const int64& tmp); - void setForceFlag(const bool& tmp); - const string getTopic() const; - const string getGroup() const; - const int64 getTimeStamp() const; - const bool getForceFlag() const; - - private: - string topic; - string group; - int64 timestamp; - bool isForce; +public: + ResetOffsetRequestHeader() {} + ~ResetOffsetRequestHeader() {} + static CommandHeader* Decode(Json::Value& ext); + void setTopic(const string& tmp); + void setGroup(const string& tmp); + void setTimeStamp(const int64& tmp); + void setForceFlag(const bool& tmp); + const string getTopic() const; + const string getGroup() const; + const int64 getTimeStamp() const; + const bool getForceFlag() const; + +private: + string topic; + string group; + int64 timestamp; + bool isForce; }; class GetConsumerRunningInfoRequestHeader : public CommandHeader { - public: - GetConsumerRunningInfoRequestHeader() {} - virtual ~GetConsumerRunningInfoRequestHeader() {} - virtual void Encode(Json::Value& outData); - virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); - static CommandHeader* Decode(Json::Value& ext); - const string getConsumerGroup() const; - void setConsumerGroup(const string& consumerGroup); - const string getClientId() const; - void setClientId(const string& clientId); - const bool isJstackEnable() const; - void setJstackEnable(const bool& jstackEnable); - - private: - string consumerGroup; - string clientId; - bool jstackEnable; +public: + GetConsumerRunningInfoRequestHeader() {} + virtual ~GetConsumerRunningInfoRequestHeader() {} + virtual void Encode(Json::Value& outData); + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap); + static CommandHeader* Decode(Json::Value& ext); + const string getConsumerGroup() const; + void setConsumerGroup(const string& consumerGroup); + const string getClientId() const; + void setClientId(const string& clientId); + const bool isJstackEnable() const; + void setJstackEnable(const bool& jstackEnable); + +private: + string consumerGroup; + string clientId; + bool jstackEnable; }; class NotifyConsumerIdsChangedRequestHeader : public CommandHeader { - public: - NotifyConsumerIdsChangedRequestHeader() {} - virtual ~NotifyConsumerIdsChangedRequestHeader() {} - static CommandHeader* Decode(Json::Value& ext); - void setGroup(const string& tmp); - const string getGroup() const; - - private: - string consumerGroup; +public: + NotifyConsumerIdsChangedRequestHeader() {} + virtual ~NotifyConsumerIdsChangedRequestHeader() {} + static CommandHeader* Decode(Json::Value& ext); + void setGroup(const string& tmp); + const string getGroup() const; + +private: + string consumerGroup; }; // ConsumerRunningInfo::getProperties() const { - return properties; -} +const map ConsumerRunningInfo::getProperties() const { return properties; } -void ConsumerRunningInfo::setProperties( - const map& input_properties) { - properties = input_properties; -} +void ConsumerRunningInfo::setProperties(const map& input_properties) { properties = input_properties; } -void ConsumerRunningInfo::setProperty(const string& key, const string& value) { - properties[key] = value; -} +void ConsumerRunningInfo::setProperty(const string& key, const string& value) { properties[key] = value; } -const map ConsumerRunningInfo::getMqTable() - const { - return mqTable; -} +const map ConsumerRunningInfo::getMqTable() const { return mqTable; } -void ConsumerRunningInfo::setMqTable(MessageQueue queue, - ProcessQueueInfo queueInfo) { - mqTable[queue] = queueInfo; -} +void ConsumerRunningInfo::setMqTable(MessageQueue queue, ProcessQueueInfo queueInfo) { mqTable[queue] = queueInfo; } /*const map ConsumerRunningInfo::getStatusTable() const { @@ -62,64 +47,57 @@ input_statusTable) statusTable = input_statusTable; } */ -const vector ConsumerRunningInfo::getSubscriptionSet() const { - return subscriptionSet; -} +const vector ConsumerRunningInfo::getSubscriptionSet() const { return subscriptionSet; } -void ConsumerRunningInfo::setSubscriptionSet( - const vector& input_subscriptionSet) { - subscriptionSet = input_subscriptionSet; +void ConsumerRunningInfo::setSubscriptionSet(const vector& input_subscriptionSet) { + subscriptionSet = input_subscriptionSet; } const string ConsumerRunningInfo::getJstack() const { return jstack; } -void ConsumerRunningInfo::setJstack(const string& input_jstack) { - jstack = input_jstack; -} +void ConsumerRunningInfo::setJstack(const string& input_jstack) { jstack = input_jstack; } string ConsumerRunningInfo::encode() { - Json::Value outData; - - outData[PROP_NAMESERVER_ADDR] = properties[PROP_NAMESERVER_ADDR]; - outData[PROP_CONSUME_TYPE] = properties[PROP_CONSUME_TYPE]; - outData[PROP_CLIENT_VERSION] = properties[PROP_CLIENT_VERSION]; - outData[PROP_CONSUMER_START_TIMESTAMP] = - properties[PROP_CONSUMER_START_TIMESTAMP]; - outData[PROP_CONSUME_ORDERLY] = properties[PROP_CONSUME_ORDERLY]; - outData[PROP_THREADPOOL_CORE_SIZE] = properties[PROP_THREADPOOL_CORE_SIZE]; - - Json::Value root; - root["jstack"] = jstack; - root["properties"] = outData; - - { - vector::const_iterator it = subscriptionSet.begin(); - for (; it != subscriptionSet.end(); it++) { - root["subscriptionSet"].append(it->toJson()); + Json::Value outData; + + outData[PROP_NAMESERVER_ADDR] = properties[PROP_NAMESERVER_ADDR]; + outData[PROP_CONSUME_TYPE] = properties[PROP_CONSUME_TYPE]; + outData[PROP_CLIENT_VERSION] = properties[PROP_CLIENT_VERSION]; + outData[PROP_CONSUMER_START_TIMESTAMP] = properties[PROP_CONSUMER_START_TIMESTAMP]; + outData[PROP_CONSUME_ORDERLY] = properties[PROP_CONSUME_ORDERLY]; + outData[PROP_THREADPOOL_CORE_SIZE] = properties[PROP_THREADPOOL_CORE_SIZE]; + + Json::Value root; + root["jstack"] = jstack; + root["properties"] = outData; + + { + vector::const_iterator it = subscriptionSet.begin(); + for (; it != subscriptionSet.end(); it++) { + root["subscriptionSet"].append(it->toJson()); + } } - } - - Json::FastWriter fastwrite; - string finals = fastwrite.write(root); - Json::Value mq; - string key = "\"mqTable\":"; - key.append("{"); - for (map::iterator it = mqTable.begin(); - it != mqTable.end(); ++it) { - key.append((it->first).toJson().toStyledString()); + Json::FastWriter fastwrite; + string finals = fastwrite.write(root); + + Json::Value mq; + string key = "\"mqTable\":"; + key.append("{"); + for (map::iterator it = mqTable.begin(); it != mqTable.end(); ++it) { + key.append((it->first).toJson().toStyledString()); + key.erase(key.end() - 1); + key.append(":"); + key.append((it->second).toJson().toStyledString()); + key.append(","); + } key.erase(key.end() - 1); - key.append(":"); - key.append((it->second).toJson().toStyledString()); - key.append(","); - } - key.erase(key.end() - 1); - key.append("}"); + key.append("}"); - // insert mqTable to final string - key.append(","); - finals.insert(1, key); + // insert mqTable to final string + key.append(","); + finals.insert(1, key); - return finals; + return finals; } } diff --git a/src/protocol/ConsumerRunningInfo.h b/src/protocol/ConsumerRunningInfo.h old mode 100755 new mode 100644 index ec1e2b973..494025254 --- a/src/protocol/ConsumerRunningInfo.h +++ b/src/protocol/ConsumerRunningInfo.h @@ -24,43 +24,42 @@ namespace rocketmq { class ConsumerRunningInfo { - public: - ConsumerRunningInfo() {} - virtual ~ConsumerRunningInfo() { - properties.clear(); - mqTable.clear(); - subscriptionSet.clear(); - } +public: + ConsumerRunningInfo() {} + virtual ~ConsumerRunningInfo() { + properties.clear(); + mqTable.clear(); + subscriptionSet.clear(); + } - public: - static const string PROP_NAMESERVER_ADDR; - static const string PROP_THREADPOOL_CORE_SIZE; - static const string PROP_CONSUME_ORDERLY; - static const string PROP_CONSUME_TYPE; - static const string PROP_CLIENT_VERSION; - static const string PROP_CONSUMER_START_TIMESTAMP; +public: + static const string PROP_NAMESERVER_ADDR; + static const string PROP_THREADPOOL_CORE_SIZE; + static const string PROP_CONSUME_ORDERLY; + static const string PROP_CONSUME_TYPE; + static const string PROP_CLIENT_VERSION; + static const string PROP_CONSUMER_START_TIMESTAMP; - public: - const map getProperties() const; - void setProperties(const map& input_properties); - void setProperty(const string& key, const string& value); - const map getMqTable() const; - void setMqTable(MessageQueue queue, ProcessQueueInfo queueInfo); - // const map getStatusTable() const; - // void setStatusTable(const map& input_statusTable) ; - const vector getSubscriptionSet() const; - void setSubscriptionSet( - const vector& input_subscriptionSet); - const string getJstack() const; - void setJstack(const string& input_jstack); - string encode(); +public: + const map getProperties() const; + void setProperties(const map& input_properties); + void setProperty(const string& key, const string& value); + const map getMqTable() const; + void setMqTable(MessageQueue queue, ProcessQueueInfo queueInfo); + // const map getStatusTable() const; + // void setStatusTable(const map& input_statusTable) ; + const vector getSubscriptionSet() const; + void setSubscriptionSet(const vector& input_subscriptionSet); + const string getJstack() const; + void setJstack(const string& input_jstack); + string encode(); - private: - map properties; - vector subscriptionSet; - map mqTable; - // map statusTable; - string jstack; +private: + map properties; + vector subscriptionSet; + map mqTable; + // map statusTable; + string jstack; }; } #endif diff --git a/src/protocol/HeartbeatData.h b/src/protocol/HeartbeatData.h old mode 100755 new mode 100644 index 9b74280c1..bc3c8caa8 --- a/src/protocol/HeartbeatData.h +++ b/src/protocol/HeartbeatData.h @@ -27,116 +27,112 @@ namespace rocketmq { //::const_iterator it = subscriptionDataSet.begin(); - for (; it != subscriptionDataSet.end(); it++) { - outJson["subscriptionDataSet"].append((*it).toJson()); +public: + ConsumerData() {}; + virtual ~ConsumerData() { subscriptionDataSet.clear(); } + bool operator<(const ConsumerData& cd) const { return groupName < cd.groupName; } + + Json::Value toJson() const { + Json::Value outJson; + outJson["groupName"] = groupName; + outJson["consumeFromWhere"] = consumeFromWhere; + outJson["consumeType"] = consumeType; + outJson["messageModel"] = messageModel; + + vector::const_iterator it = subscriptionDataSet.begin(); + for (; it != subscriptionDataSet.end(); it++) { + outJson["subscriptionDataSet"].append((*it).toJson()); + } + + return outJson; } - return outJson; - } - - public: - string groupName; - ConsumeType consumeType; - MessageModel messageModel; - ConsumeFromWhere consumeFromWhere; - vector subscriptionDataSet; +public: + string groupName; + ConsumeType consumeType; + MessageModel messageModel; + ConsumeFromWhere consumeFromWhere; + vector subscriptionDataSet; }; // lock(m_consumerDataMutex); - vector::iterator itc = m_consumerDataSet.begin(); - for (; itc != m_consumerDataSet.end(); itc++) { - root["consumerDataSet"].append((*itc).toJson()); - } +public: + virtual ~HeartbeatData() { + m_producerDataSet.clear(); + m_consumerDataSet.clear(); + } + void Encode(string& outData) { + Json::Value root; + + // lock(m_consumerDataMutex); + vector::iterator itc = m_consumerDataSet.begin(); + for (; itc != m_consumerDataSet.end(); itc++) { + root["consumerDataSet"].append((*itc).toJson()); + } + } + + // lock(m_producerDataMutex); + vector::iterator itp = m_producerDataSet.begin(); + for (; itp != m_producerDataSet.end(); itp++) { + root["producerDataSet"].append((*itp).toJson()); + } + } + // lock(m_producerDataMutex); + return m_producerDataSet.empty(); + } + + void insertDataToProducerDataSet(ProducerData& producerData) { + boost::lock_guard lock(m_producerDataMutex); + m_producerDataSet.push_back(producerData); } - // lock(m_producerDataMutex); - vector::iterator itp = m_producerDataSet.begin(); - for (; itp != m_producerDataSet.end(); itp++) { - root["producerDataSet"].append((*itp).toJson()); - } + bool isConsumerDataSetEmpty() { + boost::lock_guard lock(m_consumerDataMutex); + return m_consumerDataSet.empty(); } - // lock(m_producerDataMutex); - return m_producerDataSet.empty(); - } - - void insertDataToProducerDataSet(ProducerData& producerData) { - boost::lock_guard lock(m_producerDataMutex); - m_producerDataSet.push_back(producerData); - } - - bool isConsumerDataSetEmpty() { - boost::lock_guard lock(m_consumerDataMutex); - return m_consumerDataSet.empty(); - } - - void insertDataToConsumerDataSet(ConsumerData& consumerData) { - boost::lock_guard lock(m_consumerDataMutex); - m_consumerDataSet.push_back(consumerData); - } - - private: - string m_clientID; - vector m_producerDataSet; - vector m_consumerDataSet; - boost::mutex m_producerDataMutex; - boost::mutex m_consumerDataMutex; + + void insertDataToConsumerDataSet(ConsumerData& consumerData) { + boost::lock_guard lock(m_consumerDataMutex); + m_consumerDataSet.push_back(consumerData); + } + +private: + string m_clientID; + vector m_producerDataSet; + vector m_consumerDataSet; + boost::mutex m_producerDataMutex; + boost::mutex m_consumerDataMutex; }; } // &table) { m_table = table; } - private: +private: map m_table; }; } // namespace rocketmq diff --git a/src/protocol/LockBatchBody.cpp b/src/protocol/LockBatchBody.cpp old mode 100755 new mode 100644 index c56c17f92..d93ac8d0d --- a/src/protocol/LockBatchBody.cpp +++ b/src/protocol/LockBatchBody.cpp @@ -20,105 +20,86 @@ namespace rocketmq { // LockBatchRequestBody::getMqSet() { return mqSet; } -void LockBatchRequestBody::setMqSet(vector in_mqSet) { - mqSet.swap(in_mqSet); -} +void LockBatchRequestBody::setMqSet(vector in_mqSet) { mqSet.swap(in_mqSet); } void LockBatchRequestBody::Encode(string& outData) { - Json::Value root; - root["consumerGroup"] = consumerGroup; - root["clientId"] = clientId; + Json::Value root; + root["consumerGroup"] = consumerGroup; + root["clientId"] = clientId; - vector::const_iterator it = mqSet.begin(); - for (; it != mqSet.end(); it++) { - root["mqSet"].append(toJson(*it)); - } + vector::const_iterator it = mqSet.begin(); + for (; it != mqSet.end(); it++) { + root["mqSet"].append(toJson(*it)); + } - Json::FastWriter fastwrite; - outData = fastwrite.write(root); + Json::FastWriter fastwrite; + outData = fastwrite.write(root); } Json::Value LockBatchRequestBody::toJson(const MQMessageQueue& mq) const { - Json::Value outJson; - outJson["topic"] = mq.getTopic(); - outJson["brokerName"] = mq.getBrokerName(); - outJson["queueId"] = mq.getQueueId(); - return outJson; + Json::Value outJson; + outJson["topic"] = mq.getTopic(); + outJson["brokerName"] = mq.getBrokerName(); + outJson["queueId"] = mq.getQueueId(); + return outJson; } -vector LockBatchResponseBody::getLockOKMQSet() { - return lockOKMQSet; -} -void LockBatchResponseBody::setLockOKMQSet( - vector in_lockOKMQSet) { - lockOKMQSet.swap(in_lockOKMQSet); -} +vector LockBatchResponseBody::getLockOKMQSet() { return lockOKMQSet; } +void LockBatchResponseBody::setLockOKMQSet(vector in_lockOKMQSet) { lockOKMQSet.swap(in_lockOKMQSet); } -void LockBatchResponseBody::Decode(const MemoryBlock* mem, - vector& messageQueues) { - messageQueues.clear(); - //(mem->getData()); +void LockBatchResponseBody::Decode(const MemoryBlock* mem, vector& messageQueues) { + messageQueues.clear(); + //(mem->getData()); - Json::Reader reader; - Json::Value root; - if (!reader.parse(pData, root)) { - LOG_WARN("decode LockBatchResponseBody error"); - return; - } + Json::Reader reader; + Json::Value root; + if (!reader.parse(pData, root)) { + LOG_WARN("decode LockBatchResponseBody error"); + return; + } - Json::Value mqs = root["lockOKMQSet"]; - LOG_DEBUG("LockBatchResponseBody mqs size:%d", mqs.size()); - for (unsigned int i = 0; i < mqs.size(); i++) { - MQMessageQueue mq; - Json::Value qd = mqs[i]; - mq.setTopic(qd["topic"].asString()); - mq.setBrokerName(qd["brokerName"].asString()); - mq.setQueueId(qd["queueId"].asInt()); - LOG_INFO("LockBatchResponseBody MQ:%s", mq.toString().c_str()); - messageQueues.push_back(mq); - } + Json::Value mqs = root["lockOKMQSet"]; + LOG_DEBUG("LockBatchResponseBody mqs size:%d", mqs.size()); + for (unsigned int i = 0; i < mqs.size(); i++) { + MQMessageQueue mq; + Json::Value qd = mqs[i]; + mq.setTopic(qd["topic"].asString()); + mq.setBrokerName(qd["brokerName"].asString()); + mq.setQueueId(qd["queueId"].asInt()); + LOG_INFO("LockBatchResponseBody MQ:%s", mq.toString().c_str()); + messageQueues.push_back(mq); + } } string UnlockBatchRequestBody::getConsumerGroup() { return consumerGroup; } -void UnlockBatchRequestBody::setConsumerGroup(string in_consumerGroup) { - consumerGroup = in_consumerGroup; -} +void UnlockBatchRequestBody::setConsumerGroup(string in_consumerGroup) { consumerGroup = in_consumerGroup; } string UnlockBatchRequestBody::getClientId() { return clientId; } -void UnlockBatchRequestBody::setClientId(string in_clientId) { - clientId = in_clientId; -} +void UnlockBatchRequestBody::setClientId(string in_clientId) { clientId = in_clientId; } vector UnlockBatchRequestBody::getMqSet() { return mqSet; } -void UnlockBatchRequestBody::setMqSet(vector in_mqSet) { - mqSet.swap(in_mqSet); -} +void UnlockBatchRequestBody::setMqSet(vector in_mqSet) { mqSet.swap(in_mqSet); } void UnlockBatchRequestBody::Encode(string& outData) { - Json::Value root; - root["consumerGroup"] = consumerGroup; - root["clientId"] = clientId; + Json::Value root; + root["consumerGroup"] = consumerGroup; + root["clientId"] = clientId; - vector::const_iterator it = mqSet.begin(); - for (; it != mqSet.end(); it++) { - root["mqSet"].append(toJson(*it)); - } + vector::const_iterator it = mqSet.begin(); + for (; it != mqSet.end(); it++) { + root["mqSet"].append(toJson(*it)); + } - Json::FastWriter fastwrite; - outData = fastwrite.write(root); + Json::FastWriter fastwrite; + outData = fastwrite.write(root); } -Json::Value UnlockBatchRequestBody::toJson( - const MQMessageQueue& mq) const { - Json::Value outJson; - outJson["topic"] = mq.getTopic(); - outJson["brokerName"] = mq.getBrokerName(); - outJson["queueId"] = mq.getQueueId(); - return outJson; +Json::Value UnlockBatchRequestBody::toJson(const MQMessageQueue& mq) const { + Json::Value outJson; + outJson["topic"] = mq.getTopic(); + outJson["brokerName"] = mq.getBrokerName(); + outJson["queueId"] = mq.getQueueId(); + return outJson; } } diff --git a/src/protocol/LockBatchBody.h b/src/protocol/LockBatchBody.h old mode 100755 new mode 100644 index c1d715566..36d8a9ce8 --- a/src/protocol/LockBatchBody.h +++ b/src/protocol/LockBatchBody.h @@ -29,51 +29,50 @@ namespace rocketmq { // getMqSet(); - void setMqSet(vector mqSet); - void Encode(string& outData); - Json::Value toJson(const MQMessageQueue& mq) const; +public: + virtual ~LockBatchRequestBody() { mqSet.clear(); } + string getConsumerGroup(); + void setConsumerGroup(string consumerGroup); + string getClientId(); + void setClientId(string clientId); + vector getMqSet(); + void setMqSet(vector mqSet); + void Encode(string& outData); + Json::Value toJson(const MQMessageQueue& mq) const; - private: - string consumerGroup; - string clientId; - vector mqSet; +private: + string consumerGroup; + string clientId; + vector mqSet; }; class LockBatchResponseBody { - public: - virtual ~LockBatchResponseBody() { lockOKMQSet.clear(); } - vector getLockOKMQSet(); - void setLockOKMQSet(vector lockOKMQSet); - static void Decode(const MemoryBlock* mem, - vector& messageQueues); +public: + virtual ~LockBatchResponseBody() { lockOKMQSet.clear(); } + vector getLockOKMQSet(); + void setLockOKMQSet(vector lockOKMQSet); + static void Decode(const MemoryBlock* mem, vector& messageQueues); - private: - vector lockOKMQSet; +private: + vector lockOKMQSet; }; class UnlockBatchRequestBody { - public: - virtual ~UnlockBatchRequestBody() { mqSet.clear(); } - string getConsumerGroup(); - void setConsumerGroup(string consumerGroup); - string getClientId(); - void setClientId(string clientId); - vector getMqSet(); - void setMqSet(vector mqSet); - void Encode(string& outData); - Json::Value toJson(const MQMessageQueue& mq) const; +public: + virtual ~UnlockBatchRequestBody() { mqSet.clear(); } + string getConsumerGroup(); + void setConsumerGroup(string consumerGroup); + string getClientId(); + void setClientId(string clientId); + vector getMqSet(); + void setMqSet(vector mqSet); + void Encode(string& outData); + Json::Value toJson(const MQMessageQueue& mq) const; - private: - string consumerGroup; - string clientId; - vector mqSet; +private: + string consumerGroup; + string clientId; + vector mqSet; }; } // RemotingCommand::s_seqNumber; //::max(); + // mask sign bit + m_opaque = s_seqNumber.fetch_add(1, boost::memory_order_relaxed) & numeric_limits::max(); } -RemotingCommand::RemotingCommand(int code, string language, int version, - int opaque, int flag, string remark, +RemotingCommand::RemotingCommand(int code, string language, int version, int opaque, int flag, string remark, CommandHeader* pExtHeader) : m_code(code), m_language(language), @@ -49,84 +47,76 @@ RemotingCommand::RemotingCommand(int code, string language, int version, m_remark(remark), m_pExtHeader(pExtHeader) {} -RemotingCommand::RemotingCommand(const RemotingCommand& command) { - Assign(command); -} +RemotingCommand::RemotingCommand(const RemotingCommand& command) { Assign(command); } RemotingCommand& RemotingCommand::operator=(const RemotingCommand& command) { - if (this != &command) { - Assign(command); - } - return *this; + if (this != &command) { + Assign(command); + } + return *this; } RemotingCommand::~RemotingCommand() { m_pExtHeader = NULL; } -void RemotingCommand::Assign(const RemotingCommand &command) { - m_code = command.m_code; - m_language = command.m_language; - m_version = command.m_version; - m_opaque = command.m_opaque; - m_flag = command.m_flag; - m_remark = command.m_remark; - m_msgBody = command.m_msgBody; - - for (auto &it : command.m_extFields) { - m_extFields[it.first] = it.second; - } - - m_head = command.m_head; - m_body = command.m_body; - m_parsedJson = command.m_parsedJson; - //m_pExtHeader = command.m_pExtHeader; //ignore this filed at this moment, if need please add it +void RemotingCommand::Assign(const RemotingCommand& command) { + m_code = command.m_code; + m_language = command.m_language; + m_version = command.m_version; + m_opaque = command.m_opaque; + m_flag = command.m_flag; + m_remark = command.m_remark; + m_msgBody = command.m_msgBody; + + for (auto& it : command.m_extFields) { + m_extFields[it.first] = it.second; + } + + m_head = command.m_head; + m_body = command.m_body; + m_parsedJson = command.m_parsedJson; + // m_pExtHeader = command.m_pExtHeader; //ignore this filed at this moment, if need please add it } void RemotingCommand::Encode() { - Json::Value root; - root["code"] = m_code; - root["language"] = "CPP"; - root["version"] = m_version; - root["opaque"] = m_opaque; - root["flag"] = m_flag; - root["remark"] = m_remark; - - if (m_pExtHeader) { - Json::Value extJson; - m_pExtHeader->Encode(extJson); - - extJson[SessionCredentials::Signature] = - m_extFields[SessionCredentials::Signature]; - extJson[SessionCredentials::AccessKey] = - m_extFields[SessionCredentials::AccessKey]; - extJson[SessionCredentials::ONSChannelKey] = - m_extFields[SessionCredentials::ONSChannelKey]; - - root["extFields"] = extJson; - } else { // for heartbeat - Json::Value extJson; - extJson[SessionCredentials::Signature] = - m_extFields[SessionCredentials::Signature]; - extJson[SessionCredentials::AccessKey] = - m_extFields[SessionCredentials::AccessKey]; - extJson[SessionCredentials::ONSChannelKey] = - m_extFields[SessionCredentials::ONSChannelKey]; - root["extFields"] = extJson; - } - - Json::FastWriter fastwrite; - string data = fastwrite.write(root); - - uint32 headLen = data.size(); - uint32 totalLen = 4 + headLen + m_body.getSize(); - - uint32 messageHeader[2]; - messageHeader[0] = htonl(totalLen); - messageHeader[1] = htonl(headLen); - - //Encode(extJson); + + extJson[SessionCredentials::Signature] = m_extFields[SessionCredentials::Signature]; + extJson[SessionCredentials::AccessKey] = m_extFields[SessionCredentials::AccessKey]; + extJson[SessionCredentials::ONSChannelKey] = m_extFields[SessionCredentials::ONSChannelKey]; + + root["extFields"] = extJson; + } else { // for heartbeat + Json::Value extJson; + extJson[SessionCredentials::Signature] = m_extFields[SessionCredentials::Signature]; + extJson[SessionCredentials::AccessKey] = m_extFields[SessionCredentials::AccessKey]; + extJson[SessionCredentials::ONSChannelKey] = m_extFields[SessionCredentials::ONSChannelKey]; + root["extFields"] = extJson; + } + + Json::FastWriter fastwrite; + string data = fastwrite.write(root); + + uint32 headLen = data.size(); + uint32 totalLen = 4 + headLen + m_body.getSize(); + + uint32 messageHeader[2]; + messageHeader[0] = htonl(totalLen); + messageHeader[1] = htonl(headLen); + + //(mem.getData()); - Json::Reader reader; - Json::Value object; - const char* begin = pData + 4; - const char* end = pData + 4 + headLen; - - if (!reader.parse(begin, end, object)) { - THROW_MQEXCEPTION(MQClientException, "conn't parse json", -1); - } - - int code = object["code"].asInt(); - - string language = object["language"].asString(); - int version = object["version"].asInt(); - int opaque = object["opaque"].asInt(); - int flag = object["flag"].asInt(); - Json::Value v = object["remark"]; - string remark = ""; - if (!v.isNull()) { - remark = object["remark"].asString(); - } - LOG_DEBUG( - "code:%d, remark:%s, version:%d, opaque:%d, flag:%d, remark:%s, " - "headLen:%d, bodyLen:%d ", - code, language.c_str(), version, opaque, flag, remark.c_str(), headLen, - bodyLen); - RemotingCommand* cmd = - new RemotingCommand(code, language, version, opaque, flag, remark, NULL); - cmd->setParsedJson(object); - if (bodyLen > 0) { - cmd->SetBody(pData + 4 + headLen, bodyLen); - } - return cmd; + //(mem.getData()); + Json::Reader reader; + Json::Value object; + const char* begin = pData + 4; + const char* end = pData + 4 + headLen; + + if (!reader.parse(begin, end, object)) { + THROW_MQEXCEPTION(MQClientException, "conn't parse json", -1); + } + + int code = object["code"].asInt(); + + string language = object["language"].asString(); + int version = object["version"].asInt(); + int opaque = object["opaque"].asInt(); + int flag = object["flag"].asInt(); + Json::Value v = object["remark"]; + string remark = ""; + if (!v.isNull()) { + remark = object["remark"].asString(); + } + LOG_DEBUG( + "code:%d, remark:%s, version:%d, opaque:%d, flag:%d, remark:%s, " + "headLen:%d, bodyLen:%d ", + code, language.c_str(), version, opaque, flag, remark.c_str(), headLen, bodyLen); + RemotingCommand* cmd = new RemotingCommand(code, language, version, opaque, flag, remark, NULL); + cmd->setParsedJson(object); + if (bodyLen > 0) { + cmd->SetBody(pData + 4 + headLen, bodyLen); + } + return cmd; } void RemotingCommand::markResponseType() { - int bits = 1 << RPC_TYPE; - m_flag |= bits; + int bits = 1 << RPC_TYPE; + m_flag |= bits; } bool RemotingCommand::isResponseType() { - int bits = 1 << RPC_TYPE; - return (m_flag & bits) == bits; + int bits = 1 << RPC_TYPE; + return (m_flag & bits) == bits; } void RemotingCommand::markOnewayRPC() { - int bits = 1 << RPC_ONEWAY; - m_flag |= bits; + int bits = 1 << RPC_ONEWAY; + m_flag |= bits; } bool RemotingCommand::isOnewayRPC() { - int bits = 1 << RPC_ONEWAY; - return (m_flag & bits) == bits; + int bits = 1 << RPC_ONEWAY; + return (m_flag & bits) == bits; } void RemotingCommand::setOpaque(const int opa) { m_opaque = opa; } void RemotingCommand::SetExtHeader(int code) { - try { - Json::Value ext = m_parsedJson["extFields"]; - if (!ext.isNull()) { - m_pExtHeader = NULL; - switch (code) { - case SEND_MESSAGE: - m_pExtHeader.reset(SendMessageResponseHeader::Decode(ext)); - break; - case PULL_MESSAGE: - m_pExtHeader.reset(PullMessageResponseHeader::Decode(ext)); - break; - case GET_MIN_OFFSET: - m_pExtHeader.reset(GetMinOffsetResponseHeader::Decode(ext)); - break; - case GET_MAX_OFFSET: - m_pExtHeader.reset(GetMaxOffsetResponseHeader::Decode(ext)); - break; - case SEARCH_OFFSET_BY_TIMESTAMP: - m_pExtHeader.reset(SearchOffsetResponseHeader::Decode(ext)); - break; - case GET_EARLIEST_MSG_STORETIME: - m_pExtHeader.reset( - GetEarliestMsgStoretimeResponseHeader::Decode(ext)); - break; - case QUERY_CONSUMER_OFFSET: - m_pExtHeader.reset(QueryConsumerOffsetResponseHeader::Decode(ext)); - break; - case RESET_CONSUMER_CLIENT_OFFSET: - m_pExtHeader.reset(ResetOffsetRequestHeader::Decode(ext)); - break; - case GET_CONSUMER_RUNNING_INFO: - m_pExtHeader.reset(GetConsumerRunningInfoRequestHeader::Decode(ext)); - break; - case NOTIFY_CONSUMER_IDS_CHANGED: - m_pExtHeader.reset( - NotifyConsumerIdsChangedRequestHeader::Decode(ext)); - default: - break; - } + try { + Json::Value ext = m_parsedJson["extFields"]; + if (!ext.isNull()) { + m_pExtHeader = NULL; + switch (code) { + case SEND_MESSAGE: + m_pExtHeader.reset(SendMessageResponseHeader::Decode(ext)); + break; + case PULL_MESSAGE: + m_pExtHeader.reset(PullMessageResponseHeader::Decode(ext)); + break; + case GET_MIN_OFFSET: + m_pExtHeader.reset(GetMinOffsetResponseHeader::Decode(ext)); + break; + case GET_MAX_OFFSET: + m_pExtHeader.reset(GetMaxOffsetResponseHeader::Decode(ext)); + break; + case SEARCH_OFFSET_BY_TIMESTAMP: + m_pExtHeader.reset(SearchOffsetResponseHeader::Decode(ext)); + break; + case GET_EARLIEST_MSG_STORETIME: + m_pExtHeader.reset(GetEarliestMsgStoretimeResponseHeader::Decode(ext)); + break; + case QUERY_CONSUMER_OFFSET: + m_pExtHeader.reset(QueryConsumerOffsetResponseHeader::Decode(ext)); + break; + case RESET_CONSUMER_CLIENT_OFFSET: + m_pExtHeader.reset(ResetOffsetRequestHeader::Decode(ext)); + break; + case GET_CONSUMER_RUNNING_INFO: + m_pExtHeader.reset(GetConsumerRunningInfoRequestHeader::Decode(ext)); + break; + case NOTIFY_CONSUMER_IDS_CHANGED: + m_pExtHeader.reset(NotifyConsumerIdsChangedRequestHeader::Decode(ext)); + default: + break; + } + } + } + catch (MQException& e) { + LOG_ERROR("set response head error"); } - } catch (MQException& e) { - LOG_ERROR("set response head error"); - } } void RemotingCommand::setCode(int code) { m_code = code; } @@ -261,9 +248,7 @@ string RemotingCommand::getRemark() const { return m_remark; } void RemotingCommand::setRemark(string mark) { m_remark = mark; } -CommandHeader* RemotingCommand::getCommandHeader() const { - return m_pExtHeader.get(); -} +CommandHeader* RemotingCommand::getCommandHeader() const { return m_pExtHeader.get(); } void RemotingCommand::setParsedJson(Json::Value json) { m_parsedJson = json; } @@ -275,18 +260,13 @@ void RemotingCommand::setMsgBody(const string& body) { m_msgBody = body; } string RemotingCommand::getMsgBody() const { return m_msgBody; } -void RemotingCommand::addExtField(const string& key, const string& value) { - m_extFields[key] = value; -} +void RemotingCommand::addExtField(const string& key, const string& value) { m_extFields[key] = value; } std::string RemotingCommand::ToString() const { - std::stringstream ss; - ss << "code:" << m_code - << ",opaque:" << m_opaque - << ",flag:" << m_flag - << ",body.size:" << m_body.getSize() - << ",header.size:" << m_head.getSize(); - return ss.str(); + std::stringstream ss; + ss << "code:" << m_code << ",opaque:" << m_opaque << ",flag:" << m_flag << ",body.size:" << m_body.getSize() + << ",header.size:" << m_head.getSize(); + return ss.str(); } } // m_extFields; +public: + RemotingCommand() : m_code(0) {}; + RemotingCommand(int code, CommandHeader* pCustomHeader = NULL); + RemotingCommand(int code, string language, int version, int opaque, int flag, string remark, + CommandHeader* pCustomHeader); + RemotingCommand(const RemotingCommand& command); + RemotingCommand& operator=(const RemotingCommand& command); + virtual ~RemotingCommand(); + const MemoryBlock* GetHead() const; + const MemoryBlock* GetBody() const; + void SetBody(const char* pData, int len); + void setOpaque(const int opa); + void SetExtHeader(int code); + void setCode(int code); + int getCode() const; + int getOpaque() const; + void setRemark(string mark); + string getRemark() const; + void markResponseType(); + bool isResponseType(); + void markOnewayRPC(); + bool isOnewayRPC(); + void setParsedJson(Json::Value json); + CommandHeader* getCommandHeader() const; + const int getFlag() const; + const int getVersion() const; + void addExtField(const string& key, const string& value); + string getMsgBody() const; + void setMsgBody(const string& body); - MemoryBlock m_head; - MemoryBlock m_body; - // m_pExtHeader; +public: + void Encode(); + static RemotingCommand* Decode(const MemoryBlock& mem); + std::string ToString() const; - static boost::atomic s_seqNumber; +private: + void Assign(const RemotingCommand& command); + +private: + int m_code; + string m_language; + int m_version; + int m_opaque; + int m_flag; + string m_remark; + string m_msgBody; + map m_extFields; + + MemoryBlock m_head; + MemoryBlock m_body; + // m_pExtHeader; + + static boost::atomic s_seqNumber; }; } // m_topicList; +private: + vector m_topicList; }; // m_queueDatas; std::vector m_brokerDatas; diff --git a/src/thread/disruptorLFQ.h b/src/thread/disruptorLFQ.h old mode 100755 new mode 100644 index 079bcc595..4c43e0fdf --- a/src/thread/disruptorLFQ.h +++ b/src/thread/disruptorLFQ.h @@ -30,92 +30,85 @@ namespace rocketmq { class Task; class taskEventFactory : public EventFactoryInterface { - public: - virtual Task* NewInstance(const int& size) const; +public: + virtual Task* NewInstance(const int& size) const; }; class taskBatchHandler : public EventHandlerInterface { - public: - taskBatchHandler(int pullMsgThreadPoolNum); - virtual ~taskBatchHandler() {} - - virtual void OnEvent(const int64_t& sequence, const bool& end_of_batch, - Task* event); - virtual void OnStart() {} - virtual void OnShutdown() {} - void runTaskEvent(Task event, int64_t sequence); - void stopIOService(); - - private: - boost::asio::io_service m_ioService; - boost::thread_group m_threadpool; - boost::asio::io_service::work m_ioServiceWork; +public: + taskBatchHandler(int pullMsgThreadPoolNum); + virtual ~taskBatchHandler() {} + + virtual void OnEvent(const int64_t& sequence, const bool& end_of_batch, Task* event); + virtual void OnStart() {} + virtual void OnShutdown() {} + void runTaskEvent(Task event, int64_t sequence); + void stopIOService(); + +private: + boost::asio::io_service m_ioService; + boost::thread_group m_threadpool; + boost::asio::io_service::work m_ioServiceWork; }; class taskEventTranslator : public EventTranslatorInterface { - public: - taskEventTranslator(Task* event); - virtual ~taskEventTranslator() {} - virtual Task* TranslateTo(const int64_t& sequence, Task* event); +public: + taskEventTranslator(Task* event); + virtual ~taskEventTranslator() {} + virtual Task* TranslateTo(const int64_t& sequence, Task* event); - private: - Task* m_taskEvent; +private: + Task* m_taskEvent; }; class taskExceptionHandler : public ExceptionHandlerInterface { - public: - virtual void Handle(const std::exception& exception, const int64_t& sequence, - Task* event) {} +public: + virtual void Handle(const std::exception& exception, const int64_t& sequence, Task* event) {} }; class disruptorLFQ { - public: - disruptorLFQ(int threadCount) { - m_task_factory.reset(new taskEventFactory()); - m_ring_buffer.reset(new RingBuffer( - m_task_factory.get(), - 1024, // default size is 1024, must be n power of 2 - kSingleThreadedStrategy, - kBlockingStrategy)); // load normal, lowest CPU occupy, but - // largest consume latency - - m_sequence_to_track.reset(new std::vector(0)); - m_sequenceBarrier.reset( - m_ring_buffer->NewBarrier(*(m_sequence_to_track.get()))); - - m_task_handler.reset(new taskBatchHandler(threadCount)); - m_task_exception_handler.reset(new taskExceptionHandler()); - m_processor.reset(new BatchEventProcessor( - m_ring_buffer.get(), - (SequenceBarrierInterface*)m_sequenceBarrier.get(), - m_task_handler.get(), m_task_exception_handler.get())); - - /* - Publisher will try to publish BUFFER_SIZE + 1 events. - The last event should wait for at least one consume before publishing, thus - preventing an overwrite. - After the single consume, the publisher should resume and publish the last - event. - */ - m_gating_sequences.push_back(m_processor.get()->GetSequence()); - m_ring_buffer->set_gating_sequences( - m_gating_sequences); // prevent overlap, publishEvent will be blocked - // on ring_buffer_->Next(); - - m_publisher.reset(new EventPublisher(m_ring_buffer.get())); - } - virtual ~disruptorLFQ() {} - - public: - boost::scoped_ptr m_task_factory; - boost::scoped_ptr m_task_handler; - boost::scoped_ptr m_task_exception_handler; - boost::scoped_ptr> m_sequence_to_track; - boost::scoped_ptr> m_ring_buffer; - boost::scoped_ptr m_sequenceBarrier; - boost::scoped_ptr> m_processor; - boost::scoped_ptr> m_publisher; - std::vector m_gating_sequences; +public: + disruptorLFQ(int threadCount) { + m_task_factory.reset(new taskEventFactory()); + m_ring_buffer.reset( + new RingBuffer(m_task_factory.get(), 1024, // default size is 1024, must be n power of 2 + kSingleThreadedStrategy, kBlockingStrategy)); // load normal, lowest CPU occupy, but + // largest consume latency + + m_sequence_to_track.reset(new std::vector(0)); + m_sequenceBarrier.reset(m_ring_buffer->NewBarrier(*(m_sequence_to_track.get()))); + + m_task_handler.reset(new taskBatchHandler(threadCount)); + m_task_exception_handler.reset(new taskExceptionHandler()); + m_processor.reset(new BatchEventProcessor(m_ring_buffer.get(), + (SequenceBarrierInterface*)m_sequenceBarrier.get(), + m_task_handler.get(), m_task_exception_handler.get())); + + /* + Publisher will try to publish BUFFER_SIZE + 1 events. + The last event should wait for at least one consume before publishing, thus + preventing an overwrite. + After the single consume, the publisher should resume and publish the last + event. + */ + m_gating_sequences.push_back(m_processor.get()->GetSequence()); + m_ring_buffer->set_gating_sequences(m_gating_sequences); // prevent overlap, publishEvent will be blocked + // on ring_buffer_->Next(); + + m_publisher.reset(new EventPublisher(m_ring_buffer.get())); + } + virtual ~disruptorLFQ() {} + +public: + boost::scoped_ptr m_task_factory; + boost::scoped_ptr m_task_handler; + boost::scoped_ptr m_task_exception_handler; + boost::scoped_ptr> m_sequence_to_track; + boost::scoped_ptr> m_ring_buffer; + boost::scoped_ptr m_sequenceBarrier; + boost::scoped_ptr> m_processor; + boost::scoped_ptr> m_publisher; + std::vector m_gating_sequences; }; } //m_task_handler->stopIOService(); - m_disruptorLFQ->m_processor->Halt(); + m_flag.store(false, boost::memory_order_release); + m_disruptorLFQ->m_task_handler->stopIOService(); + m_disruptorLFQ->m_processor->Halt(); } -bool TaskQueue::bTaskQueueStatusOK() { - return m_flag.load(boost::memory_order_acquire) == true; -} +bool TaskQueue::bTaskQueueStatusOK() { return m_flag.load(boost::memory_order_acquire) == true; } void TaskQueue::produce(const Task& task) { - boost::mutex::scoped_lock lock(m_publishLock); - taskEventTranslator pTranslator(const_cast(&task)); - m_disruptorLFQ->m_publisher->PublishEvent(&pTranslator); + boost::mutex::scoped_lock lock(m_publishLock); + taskEventTranslator pTranslator(const_cast(&task)); + m_disruptorLFQ->m_publisher->PublishEvent(&pTranslator); } int TaskQueue::run() { - while (true) { - m_disruptorLFQ->m_processor->Run(); - if (m_flag.load(boost::memory_order_acquire) == false) { - break; + while (true) { + m_disruptorLFQ->m_processor->Run(); + if (m_flag.load(boost::memory_order_acquire) == false) { + break; + } } - } - return 0; + return 0; } //fork()) {} - Task() { m_pTaskImpl = new Task_impl(&Task::dumy, 0); } - virtual ~Task() { delete m_pTaskImpl; } - Task& operator=(const Task& src_) { - delete m_pTaskImpl; - m_pTaskImpl = src_.m_pTaskImpl->fork(); - return *this; - } - void run() { - if (m_pTaskImpl) m_pTaskImpl->run(); - } - - private: - ITask_impl* m_pTaskImpl; + static void dumy(void*) {} + + Task(taskfunc f_, void* d_) { m_pTaskImpl = new Task_impl(f_, d_); } + Task(ITask_impl* task_imp_) : m_pTaskImpl(task_imp_) {} + Task(const Task& src_) : m_pTaskImpl(src_.m_pTaskImpl->fork()) {} + Task() { m_pTaskImpl = new Task_impl(&Task::dumy, 0); } + virtual ~Task() { delete m_pTaskImpl; } + Task& operator=(const Task& src_) { + delete m_pTaskImpl; + m_pTaskImpl = src_.m_pTaskImpl->fork(); + return *this; + } + void run() { + if (m_pTaskImpl) m_pTaskImpl->run(); + } + +private: + ITask_impl* m_pTaskImpl; }; // TaskList; - - public: - virtual ~ITaskQueue() {} - virtual void close() = 0; - virtual void produce(const Task& task) = 0; - // virtual void multi_produce(const TaskList& tasks) = 0; - // virtual int consume(Task& task) = 0; - // virtual int consume_all(TaskList& tasks) = 0; - virtual int run() = 0; - // virtual int batch_run() = 0; - virtual bool bTaskQueueStatusOK() = 0; +public: + typedef list TaskList; + +public: + virtual ~ITaskQueue() {} + virtual void close() = 0; + virtual void produce(const Task& task) = 0; + // virtual void multi_produce(const TaskList& tasks) = 0; + // virtual int consume(Task& task) = 0; + // virtual int consume_all(TaskList& tasks) = 0; + virtual int run() = 0; + // virtual int batch_run() = 0; + virtual bool bTaskQueueStatusOK() = 0; }; // - static Task gen(RET (*func)(void)) { - struct lambda { - static void taskfunc(void* p_) { (*(RET(*)(void))p_)(); }; - }; - return Task(lambda::taskfunc, (void*)func); - } - - template - static Task gen(FUNCT func, ARG1 arg1) { - struct lambda : public ITask_impl { - FUNCT dest_func; - ARG1 arg1; - lambda(FUNCT func, const ARG1& arg1) : dest_func(func), arg1(arg1) {} - virtual void run() { (*dest_func)(arg1); } - virtual ITask_impl* fork() { return new lambda(dest_func, arg1); } - }; - return Task(new lambda(func, arg1)); - } - - template - static Task gen(FUNCT func, ARG1 arg1, ARG2 arg2) { - struct lambda : public ITask_impl { - FUNCT dest_func; - ARG1 arg1; - ARG2 arg2; - lambda(FUNCT func, const ARG1& arg1, const ARG2& arg2) - : dest_func(func), arg1(arg1), arg2(arg2) {} - virtual void run() { (*dest_func)(arg1, arg2); } - virtual ITask_impl* fork() { return new lambda(dest_func, arg1, arg2); } - }; - return Task(new lambda(func, arg1, arg2)); - } - - template - static Task gen(FUNCT func, ARG1 arg1, ARG2 arg2, ARG3 arg3) { - struct lambda : public ITask_impl { - FUNCT dest_func; - ARG1 arg1; - ARG2 arg2; - ARG3 arg3; - lambda(FUNCT func, const ARG1& arg1, const ARG2& arg2, const ARG3& arg3) - : dest_func(func), arg1(arg1), arg2(arg2), arg3(arg3) {} - virtual void run() { (*dest_func)(arg1, arg2, arg3); } - virtual ITask_impl* fork() { - return new lambda(dest_func, arg1, arg2, arg3); - } - }; - return Task(new lambda(func, arg1, arg2, arg3)); - } - - template - static Task gen(FUNCT func, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4) { - struct lambda : public ITask_impl { - FUNCT dest_func; - ARG1 arg1; - ARG2 arg2; - ARG3 arg3; - ARG4 arg4; - lambda(FUNCT func, const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, - const ARG4& arg4) - : dest_func(func), arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4) {} - virtual void run() { (*dest_func)(arg1, arg2, arg3, arg4); } - virtual ITask_impl* fork() { - return new lambda(dest_func, arg1, arg2, arg3, arg4); - } - }; - return Task(new lambda(func, arg1, arg2, arg3, arg4)); - } - - template - static Task gen(FUNCT func, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4, - ARG5 arg5) { - struct lambda : public ITask_impl { - FUNCT dest_func; - ARG1 arg1; - ARG2 arg2; - ARG3 arg3; - ARG4 arg4; - ARG5 arg5; - lambda(FUNCT func, const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, - const ARG4& arg4, const ARG5& arg5) - : dest_func(func), - arg1(arg1), - arg2(arg2), - arg3(arg3), - arg4(arg4), - arg5(arg5) {} - virtual void run() { (*dest_func)(arg1, arg2, arg3, arg4, arg5); } - virtual ITask_impl* fork() { - return new lambda(dest_func, arg1, arg2, arg3, arg4, arg5); - } - }; - return Task(new lambda(func, arg1, arg2, arg3, arg4, arg5)); - } - - template - static Task gen(FUNCT func, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4, - ARG5 arg5, ARG6 arg6) { - struct lambda : public ITask_impl { - FUNCT dest_func; - ARG1 arg1; - ARG2 arg2; - ARG3 arg3; - ARG4 arg4; - ARG5 arg5; - ARG6 arg6; - lambda(FUNCT func, const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, - const ARG4& arg4, const ARG5& arg5, const ARG6& arg6) - : dest_func(func), - arg1(arg1), - arg2(arg2), - arg3(arg3), - arg4(arg4), - arg5(arg5), - arg6(arg6) {} - virtual void run() { (*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6); } - virtual ITask_impl* fork() { - return new lambda(dest_func, arg1, arg2, arg3, arg4, arg5, arg6); - } - }; - return Task(new lambda(func, arg1, arg2, arg3, arg4, arg5, arg6)); - } - - template - static Task gen(FUNCT func, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4, - ARG5 arg5, ARG6 arg6, ARG7 arg7) { - struct lambda : public ITask_impl { - FUNCT dest_func; - ARG1 arg1; - ARG2 arg2; - ARG3 arg3; - ARG4 arg4; - ARG5 arg5; - ARG6 arg6; - ARG7 arg7; - lambda(FUNCT func, const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, - const ARG4& arg4, const ARG5& arg5, const ARG6& arg6, - const ARG7& arg7) - : dest_func(func), - arg1(arg1), - arg2(arg2), - arg3(arg3), - arg4(arg4), - arg5(arg5), - arg6(arg6), - arg7(arg7) {} - virtual void run() { - (*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7); - } - virtual ITask_impl* fork() { - return new lambda(dest_func, arg1, arg2, arg3, arg4, arg5, arg6, arg7); - } - }; - return Task(new lambda(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7)); - } - - template - static Task gen(FUNCT func, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4, - ARG5 arg5, ARG6 arg6, ARG7 arg7, ARG8 arg8) { - struct lambda : public ITask_impl { - FUNCT dest_func; - ARG1 arg1; - ARG2 arg2; - ARG3 arg3; - ARG4 arg4; - ARG5 arg5; - ARG6 arg6; - ARG7 arg7; - ARG8 arg8; - lambda(FUNCT func, const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, - const ARG4& arg4, const ARG5& arg5, const ARG6& arg6, - const ARG7& arg7, const ARG8& arg8) - : dest_func(func), - arg1(arg1), - arg2(arg2), - arg3(arg3), - arg4(arg4), - arg5(arg5), - arg6(arg6), - arg7(arg7), - arg8(arg8) {} - virtual void run() { - (*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); - } - virtual ITask_impl* fork() { - return new lambda(dest_func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, - arg8); - } - }; - return Task( - new lambda(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)); - } - - template - static Task gen(FUNCT func, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4, - ARG5 arg5, ARG6 arg6, ARG7 arg7, ARG8 arg8, ARG9 arg9) { - struct lambda : public ITask_impl { - FUNCT dest_func; - ARG1 arg1; - ARG2 arg2; - ARG3 arg3; - ARG4 arg4; - ARG5 arg5; - ARG6 arg6; - ARG7 arg7; - ARG8 arg8; - ARG9 arg9; - lambda(FUNCT func, const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, - const ARG4& arg4, const ARG5& arg5, const ARG6& arg6, - const ARG7& arg7, const ARG8& arg8, const ARG9& arg9) - : dest_func(func), - arg1(arg1), - arg2(arg2), - arg3(arg3), - arg4(arg4), - arg5(arg5), - arg6(arg6), - arg7(arg7), - arg8(arg8), - arg9(arg9) {} - virtual void run() { - (*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); - } - virtual ITask_impl* fork() { - return new lambda(dest_func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, - arg8, arg9); - } - }; - return Task( - new lambda(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)); - } - - // - static Task gen(RET (T::*func)(void), T* obj) { - struct lambda : public ITask_impl { - RET (T::*dest_func)(void); - T* obj; - lambda(RET (T::*func)(void), T* obj) : dest_func(func), obj(obj) {} - virtual void run() { (obj->*dest_func)(); } - virtual ITask_impl* fork() { return new lambda(dest_func, obj); } - }; - return Task(new lambda(func, obj)); - } - - template - static Task gen(RET (T::*func)(FARG1), T* obj, ARG1 arg1) { - struct lambda : public ITask_impl { - RET (T::*dest_func)(FARG1); - T* obj; - ARG1 arg1; - lambda(RET (T::*pfunc)(FARG1), T* obj, const ARG1& arg1) - : dest_func(pfunc), obj(obj), arg1(arg1) {} - virtual void run() { (obj->*dest_func)(arg1); } - virtual ITask_impl* fork() { return new lambda(dest_func, obj, arg1); } - }; - return Task(new lambda(func, obj, arg1)); - } - - template - static Task gen(RET (T::*func)(FARG1, FARG2), T* obj, ARG1 arg1, ARG2 arg2) { - struct lambda : public ITask_impl { - RET (T::*dest_func)(FARG1, FARG2); - T* obj; - ARG1 arg1; - ARG2 arg2; - lambda(RET (T::*func)(FARG1, FARG2), T* obj, const ARG1& arg1, - const ARG2& arg2) - : dest_func(func), obj(obj), arg1(arg1), arg2(arg2) {} - virtual void run() { (obj->*dest_func)(arg1, arg2); } - virtual ITask_impl* fork() { - return new lambda(dest_func, obj, arg1, arg2); - } - }; - return Task(new lambda(func, obj, arg1, arg2)); - } - - template - static Task gen(RET (T::*func)(FARG1, FARG2, FARG3), T* obj, ARG1 arg1, - ARG2 arg2, ARG3 arg3) { - struct lambda : public ITask_impl { - RET (T::*dest_func)(FARG1, FARG2, FARG3); - T* obj; - ARG1 arg1; - ARG2 arg2; - ARG3 arg3; - lambda(RET (T::*func)(FARG1, FARG2, FARG3), T* obj, const ARG1& arg1, - const ARG2& arg2, const ARG3& arg3) - : dest_func(func), obj(obj), arg1(arg1), arg2(arg2), arg3(arg3) {} - virtual void run() { (obj->*dest_func)(arg1, arg2, arg3); } - virtual ITask_impl* fork() { - return new lambda(dest_func, obj, arg1, arg2, arg3); - } - }; - return Task(new lambda(func, obj, arg1, arg2, arg3)); - } - - template - static Task gen(RET (T::*func)(FARG1, FARG2, FARG3, FARG4), T* obj, ARG1 arg1, - ARG2 arg2, ARG3 arg3, ARG4 arg4) { - struct lambda : public ITask_impl { - RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4); - T* obj; - ARG1 arg1; - ARG2 arg2; - ARG3 arg3; - ARG4 arg4; - lambda(RET (T::*func)(FARG1, FARG2, FARG3, FARG4), T* obj, - const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, - const ARG4& arg4) - : dest_func(func), - obj(obj), - arg1(arg1), - arg2(arg2), - arg3(arg3), - arg4(arg4) {} - virtual void run() { (obj->*dest_func)(arg1, arg2, arg3, arg4); } - virtual ITask_impl* fork() { - return new lambda(dest_func, obj, arg1, arg2, arg3, arg4); - } - }; - return Task(new lambda(func, obj, arg1, arg2, arg3, arg4)); - } - - template - static Task gen(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5), T* obj, - ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4, ARG5 arg5) { - struct lambda : public ITask_impl { - RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4, FARG5); - T* obj; - ARG1 arg1; - ARG2 arg2; - ARG3 arg3; - ARG4 arg4; - ARG5 arg5; - lambda(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5), T* obj, - const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, - const ARG4& arg4, const ARG5& arg5) - : dest_func(func), - obj(obj), - arg1(arg1), - arg2(arg2), - arg3(arg3), - arg4(arg4), - arg5(arg5) {} - virtual void run() { (obj->*dest_func)(arg1, arg2, arg3, arg4, arg5); } - virtual ITask_impl* fork() { - return new lambda(dest_func, obj, arg1, arg2, arg3, arg4, arg5); - } - }; - return Task(new lambda(func, obj, arg1, arg2, arg3, arg4, arg5)); - } - - template - static Task gen(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6), - T* obj, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4, ARG5 arg5, - ARG6 arg6) { - struct lambda : public ITask_impl { - RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6); - T* obj; - ARG1 arg1; - ARG2 arg2; - ARG3 arg3; - ARG4 arg4; - ARG5 arg5; - ARG6 arg6; - lambda(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6), T* obj, - const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, - const ARG4& arg4, const ARG5& arg5, const ARG6& arg6) - : dest_func(func), - obj(obj), - arg1(arg1), - arg2(arg2), - arg3(arg3), - arg4(arg4), - arg5(arg5), - arg6(arg6) {} - virtual void run() { - (obj->*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6); - } - virtual ITask_impl* fork() { - return new lambda(dest_func, obj, arg1, arg2, arg3, arg4, arg5, arg6); - } - }; - return Task(new lambda(func, obj, arg1, arg2, arg3, arg4, arg5, arg6)); - } - - template - static Task gen(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, - FARG7), - T* obj, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4, ARG5 arg5, - ARG6 arg6, ARG7 arg7) { - struct lambda : public ITask_impl { - RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7); - T* obj; - ARG1 arg1; - ARG2 arg2; - ARG3 arg3; - ARG4 arg4; - ARG5 arg5; - ARG6 arg6; - ARG7 arg7; - lambda(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7), - T* obj, const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, - const ARG4& arg4, const ARG5& arg5, const ARG6& arg6, - const ARG7& arg7) - : dest_func(func), - obj(obj), - arg1(arg1), - arg2(arg2), - arg3(arg3), - arg4(arg4), - arg5(arg5), - arg6(arg6), - arg7(arg7) {} - virtual void run() { - (obj->*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7); - } - virtual ITask_impl* fork() { - return new lambda(dest_func, obj, arg1, arg2, arg3, arg4, arg5, arg6, - arg7); - } - }; - return Task( - new lambda(func, obj, arg1, arg2, arg3, arg4, arg5, arg6, arg7)); - } - - template - static Task gen(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, - FARG7, FARG8), - T* obj, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4, ARG5 arg5, - ARG6 arg6, ARG7 arg7, ARG8 arg8) { - struct lambda : public ITask_impl { - RET (T::*dest_func) - (FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8); - T* obj; - ARG1 arg1; - ARG2 arg2; - ARG3 arg3; - ARG4 arg4; - ARG5 arg5; - ARG6 arg6; - ARG7 arg7; - ARG8 arg8; - lambda(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, - FARG8), - T* obj, const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, - const ARG4& arg4, const ARG5& arg5, const ARG6& arg6, - const ARG7& arg7, const ARG8& arg8) - : dest_func(func), - obj(obj), - arg1(arg1), - arg2(arg2), - arg3(arg3), - arg4(arg4), - arg5(arg5), - arg6(arg6), - arg7(arg7), - arg8(arg8) {} - virtual void run() { - (obj->*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); - } - virtual ITask_impl* fork() { - return new lambda(dest_func, obj, arg1, arg2, arg3, arg4, arg5, arg6, - arg7, arg8); - } - }; - return Task( - new lambda(func, obj, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)); - } - - template - static Task gen(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, - FARG7, FARG8, FARG9), - T* obj, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4, ARG5 arg5, - ARG6 arg6, ARG7 arg7, ARG8 arg8, ARG9 arg9) { - struct lambda : public ITask_impl { - RET (T::*dest_func) - (FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8, FARG9); - T* obj; - ARG1 arg1; - ARG2 arg2; - ARG3 arg3; - ARG4 arg4; - ARG5 arg5; - ARG6 arg6; - ARG7 arg7; - ARG8 arg8; - ARG9 arg9; - lambda(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, - FARG8, FARG9), - T* obj, const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, - const ARG4& arg4, const ARG5& arg5, const ARG6& arg6, - const ARG7& arg7, const ARG8& arg8, const ARG9& arg9) - : dest_func(func), - obj(obj), - arg1(arg1), - arg2(arg2), - arg3(arg3), - arg4(arg4), - arg5(arg5), - arg6(arg6), - arg7(arg7), - arg8(arg8), - arg9(arg9) {} - virtual void run() { - (obj->*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); - } - virtual ITask_impl* fork() { - return new lambda(dest_func, obj, arg1, arg2, arg3, arg4, arg5, arg6, - arg7, arg8, arg9); - } - }; - return Task(new lambda(func, obj, arg1, arg2, arg3, arg4, arg5, arg6, arg7, - arg8, arg9)); - } + static Task gen(void (*func)(void*), void* p_) { return Task(func, p_); } + + template + static Task gen(RET (*func)(void)) { + struct lambda { + static void taskfunc(void* p_) { + (*(RET (*)(void))p_)(); + }; + }; + return Task(lambda::taskfunc, (void*)func); + } + + template + static Task gen(FUNCT func, ARG1 arg1) { + struct lambda : public ITask_impl { + FUNCT dest_func; + ARG1 arg1; + lambda(FUNCT func, const ARG1& arg1) : dest_func(func), arg1(arg1) {} + virtual void run() { (*dest_func)(arg1); } + virtual ITask_impl* fork() { return new lambda(dest_func, arg1); } + }; + return Task(new lambda(func, arg1)); + } + + template + static Task gen(FUNCT func, ARG1 arg1, ARG2 arg2) { + struct lambda : public ITask_impl { + FUNCT dest_func; + ARG1 arg1; + ARG2 arg2; + lambda(FUNCT func, const ARG1& arg1, const ARG2& arg2) : dest_func(func), arg1(arg1), arg2(arg2) {} + virtual void run() { (*dest_func)(arg1, arg2); } + virtual ITask_impl* fork() { return new lambda(dest_func, arg1, arg2); } + }; + return Task(new lambda(func, arg1, arg2)); + } + + template + static Task gen(FUNCT func, ARG1 arg1, ARG2 arg2, ARG3 arg3) { + struct lambda : public ITask_impl { + FUNCT dest_func; + ARG1 arg1; + ARG2 arg2; + ARG3 arg3; + lambda(FUNCT func, const ARG1& arg1, const ARG2& arg2, const ARG3& arg3) + : dest_func(func), arg1(arg1), arg2(arg2), arg3(arg3) {} + virtual void run() { (*dest_func)(arg1, arg2, arg3); } + virtual ITask_impl* fork() { return new lambda(dest_func, arg1, arg2, arg3); } + }; + return Task(new lambda(func, arg1, arg2, arg3)); + } + + template + static Task gen(FUNCT func, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4) { + struct lambda : public ITask_impl { + FUNCT dest_func; + ARG1 arg1; + ARG2 arg2; + ARG3 arg3; + ARG4 arg4; + lambda(FUNCT func, const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, const ARG4& arg4) + : dest_func(func), arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4) {} + virtual void run() { (*dest_func)(arg1, arg2, arg3, arg4); } + virtual ITask_impl* fork() { return new lambda(dest_func, arg1, arg2, arg3, arg4); } + }; + return Task(new lambda(func, arg1, arg2, arg3, arg4)); + } + + template + static Task gen(FUNCT func, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4, ARG5 arg5) { + struct lambda : public ITask_impl { + FUNCT dest_func; + ARG1 arg1; + ARG2 arg2; + ARG3 arg3; + ARG4 arg4; + ARG5 arg5; + lambda(FUNCT func, const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, const ARG4& arg4, const ARG5& arg5) + : dest_func(func), arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4), arg5(arg5) {} + virtual void run() { (*dest_func)(arg1, arg2, arg3, arg4, arg5); } + virtual ITask_impl* fork() { return new lambda(dest_func, arg1, arg2, arg3, arg4, arg5); } + }; + return Task(new lambda(func, arg1, arg2, arg3, arg4, arg5)); + } + + template + static Task gen(FUNCT func, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4, ARG5 arg5, ARG6 arg6) { + struct lambda : public ITask_impl { + FUNCT dest_func; + ARG1 arg1; + ARG2 arg2; + ARG3 arg3; + ARG4 arg4; + ARG5 arg5; + ARG6 arg6; + lambda(FUNCT func, const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, const ARG4& arg4, const ARG5& arg5, + const ARG6& arg6) + : dest_func(func), arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4), arg5(arg5), arg6(arg6) {} + virtual void run() { (*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6); } + virtual ITask_impl* fork() { return new lambda(dest_func, arg1, arg2, arg3, arg4, arg5, arg6); } + }; + return Task(new lambda(func, arg1, arg2, arg3, arg4, arg5, arg6)); + } + + template + static Task gen(FUNCT func, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4, ARG5 arg5, ARG6 arg6, ARG7 arg7) { + struct lambda : public ITask_impl { + FUNCT dest_func; + ARG1 arg1; + ARG2 arg2; + ARG3 arg3; + ARG4 arg4; + ARG5 arg5; + ARG6 arg6; + ARG7 arg7; + lambda(FUNCT func, const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, const ARG4& arg4, const ARG5& arg5, + const ARG6& arg6, const ARG7& arg7) + : dest_func(func), arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4), arg5(arg5), arg6(arg6), arg7(arg7) {} + virtual void run() { (*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7); } + virtual ITask_impl* fork() { return new lambda(dest_func, arg1, arg2, arg3, arg4, arg5, arg6, arg7); } + }; + return Task(new lambda(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7)); + } + + template + static Task gen(FUNCT func, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4, ARG5 arg5, ARG6 arg6, ARG7 arg7, + ARG8 arg8) { + struct lambda : public ITask_impl { + FUNCT dest_func; + ARG1 arg1; + ARG2 arg2; + ARG3 arg3; + ARG4 arg4; + ARG5 arg5; + ARG6 arg6; + ARG7 arg7; + ARG8 arg8; + lambda(FUNCT func, const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, const ARG4& arg4, const ARG5& arg5, + const ARG6& arg6, const ARG7& arg7, const ARG8& arg8) + : dest_func(func), + arg1(arg1), + arg2(arg2), + arg3(arg3), + arg4(arg4), + arg5(arg5), + arg6(arg6), + arg7(arg7), + arg8(arg8) {} + virtual void run() { (*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); } + virtual ITask_impl* fork() { return new lambda(dest_func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); } + }; + return Task(new lambda(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)); + } + + template + static Task gen(FUNCT func, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4, ARG5 arg5, ARG6 arg6, ARG7 arg7, ARG8 arg8, + ARG9 arg9) { + struct lambda : public ITask_impl { + FUNCT dest_func; + ARG1 arg1; + ARG2 arg2; + ARG3 arg3; + ARG4 arg4; + ARG5 arg5; + ARG6 arg6; + ARG7 arg7; + ARG8 arg8; + ARG9 arg9; + lambda(FUNCT func, const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, const ARG4& arg4, const ARG5& arg5, + const ARG6& arg6, const ARG7& arg7, const ARG8& arg8, const ARG9& arg9) + : dest_func(func), + arg1(arg1), + arg2(arg2), + arg3(arg3), + arg4(arg4), + arg5(arg5), + arg6(arg6), + arg7(arg7), + arg8(arg8), + arg9(arg9) {} + virtual void run() { (*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); } + virtual ITask_impl* fork() { + return new lambda(dest_func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); + } + }; + return Task(new lambda(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)); + } + + // + static Task gen(RET (T::*func)(void), T* obj) { + struct lambda : public ITask_impl { + RET (T::*dest_func)(void); + T* obj; + lambda(RET (T::*func)(void), T* obj) : dest_func(func), obj(obj) {} + virtual void run() { (obj->*dest_func)(); } + virtual ITask_impl* fork() { return new lambda(dest_func, obj); } + }; + return Task(new lambda(func, obj)); + } + + template + static Task gen(RET (T::*func)(FARG1), T* obj, ARG1 arg1) { + struct lambda : public ITask_impl { + RET (T::*dest_func)(FARG1); + T* obj; + ARG1 arg1; + lambda(RET (T::*pfunc)(FARG1), T* obj, const ARG1& arg1) : dest_func(pfunc), obj(obj), arg1(arg1) {} + virtual void run() { (obj->*dest_func)(arg1); } + virtual ITask_impl* fork() { return new lambda(dest_func, obj, arg1); } + }; + return Task(new lambda(func, obj, arg1)); + } + + template + static Task gen(RET (T::*func)(FARG1, FARG2), T* obj, ARG1 arg1, ARG2 arg2) { + struct lambda : public ITask_impl { + RET (T::*dest_func)(FARG1, FARG2); + T* obj; + ARG1 arg1; + ARG2 arg2; + lambda(RET (T::*func)(FARG1, FARG2), T* obj, const ARG1& arg1, const ARG2& arg2) + : dest_func(func), obj(obj), arg1(arg1), arg2(arg2) {} + virtual void run() { (obj->*dest_func)(arg1, arg2); } + virtual ITask_impl* fork() { return new lambda(dest_func, obj, arg1, arg2); } + }; + return Task(new lambda(func, obj, arg1, arg2)); + } + + template + static Task gen(RET (T::*func)(FARG1, FARG2, FARG3), T* obj, ARG1 arg1, ARG2 arg2, ARG3 arg3) { + struct lambda : public ITask_impl { + RET (T::*dest_func)(FARG1, FARG2, FARG3); + T* obj; + ARG1 arg1; + ARG2 arg2; + ARG3 arg3; + lambda(RET (T::*func)(FARG1, FARG2, FARG3), T* obj, const ARG1& arg1, const ARG2& arg2, const ARG3& arg3) + : dest_func(func), obj(obj), arg1(arg1), arg2(arg2), arg3(arg3) {} + virtual void run() { (obj->*dest_func)(arg1, arg2, arg3); } + virtual ITask_impl* fork() { return new lambda(dest_func, obj, arg1, arg2, arg3); } + }; + return Task(new lambda(func, obj, arg1, arg2, arg3)); + } + + template + static Task gen(RET (T::*func)(FARG1, FARG2, FARG3, FARG4), T* obj, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4) { + struct lambda : public ITask_impl { + RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4); + T* obj; + ARG1 arg1; + ARG2 arg2; + ARG3 arg3; + ARG4 arg4; + lambda(RET (T::*func)(FARG1, FARG2, FARG3, FARG4), T* obj, const ARG1& arg1, const ARG2& arg2, + const ARG3& arg3, const ARG4& arg4) + : dest_func(func), obj(obj), arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4) {} + virtual void run() { (obj->*dest_func)(arg1, arg2, arg3, arg4); } + virtual ITask_impl* fork() { return new lambda(dest_func, obj, arg1, arg2, arg3, arg4); } + }; + return Task(new lambda(func, obj, arg1, arg2, arg3, arg4)); + } + + template + static Task gen(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5), T* obj, ARG1 arg1, ARG2 arg2, ARG3 arg3, + ARG4 arg4, ARG5 arg5) { + struct lambda : public ITask_impl { + RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4, FARG5); + T* obj; + ARG1 arg1; + ARG2 arg2; + ARG3 arg3; + ARG4 arg4; + ARG5 arg5; + lambda(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5), T* obj, const ARG1& arg1, const ARG2& arg2, + const ARG3& arg3, const ARG4& arg4, const ARG5& arg5) + : dest_func(func), obj(obj), arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4), arg5(arg5) {} + virtual void run() { (obj->*dest_func)(arg1, arg2, arg3, arg4, arg5); } + virtual ITask_impl* fork() { return new lambda(dest_func, obj, arg1, arg2, arg3, arg4, arg5); } + }; + return Task(new lambda(func, obj, arg1, arg2, arg3, arg4, arg5)); + } + + template + static Task gen(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6), T* obj, ARG1 arg1, ARG2 arg2, ARG3 arg3, + ARG4 arg4, ARG5 arg5, ARG6 arg6) { + struct lambda : public ITask_impl { + RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6); + T* obj; + ARG1 arg1; + ARG2 arg2; + ARG3 arg3; + ARG4 arg4; + ARG5 arg5; + ARG6 arg6; + lambda(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6), T* obj, const ARG1& arg1, const ARG2& arg2, + const ARG3& arg3, const ARG4& arg4, const ARG5& arg5, const ARG6& arg6) + : dest_func(func), obj(obj), arg1(arg1), arg2(arg2), arg3(arg3), arg4(arg4), arg5(arg5), arg6(arg6) {} + virtual void run() { (obj->*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6); } + virtual ITask_impl* fork() { return new lambda(dest_func, obj, arg1, arg2, arg3, arg4, arg5, arg6); } + }; + return Task(new lambda(func, obj, arg1, arg2, arg3, arg4, arg5, arg6)); + } + + template + static Task gen(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7), T* obj, ARG1 arg1, ARG2 arg2, + ARG3 arg3, ARG4 arg4, ARG5 arg5, ARG6 arg6, ARG7 arg7) { + struct lambda : public ITask_impl { + RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7); + T* obj; + ARG1 arg1; + ARG2 arg2; + ARG3 arg3; + ARG4 arg4; + ARG5 arg5; + ARG6 arg6; + ARG7 arg7; + lambda(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7), T* obj, const ARG1& arg1, + const ARG2& arg2, const ARG3& arg3, const ARG4& arg4, const ARG5& arg5, const ARG6& arg6, + const ARG7& arg7) + : dest_func(func), + obj(obj), + arg1(arg1), + arg2(arg2), + arg3(arg3), + arg4(arg4), + arg5(arg5), + arg6(arg6), + arg7(arg7) {} + virtual void run() { (obj->*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7); } + virtual ITask_impl* fork() { return new lambda(dest_func, obj, arg1, arg2, arg3, arg4, arg5, arg6, arg7); } + }; + return Task(new lambda(func, obj, arg1, arg2, arg3, arg4, arg5, arg6, arg7)); + } + + template + static Task gen(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8), T* obj, ARG1 arg1, + ARG2 arg2, ARG3 arg3, ARG4 arg4, ARG5 arg5, ARG6 arg6, ARG7 arg7, ARG8 arg8) { + struct lambda : public ITask_impl { + RET(T::*dest_func) + (FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8); + T* obj; + ARG1 arg1; + ARG2 arg2; + ARG3 arg3; + ARG4 arg4; + ARG5 arg5; + ARG6 arg6; + ARG7 arg7; + ARG8 arg8; + lambda(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8), T* obj, const ARG1& arg1, + const ARG2& arg2, const ARG3& arg3, const ARG4& arg4, const ARG5& arg5, const ARG6& arg6, + const ARG7& arg7, const ARG8& arg8) + : dest_func(func), + obj(obj), + arg1(arg1), + arg2(arg2), + arg3(arg3), + arg4(arg4), + arg5(arg5), + arg6(arg6), + arg7(arg7), + arg8(arg8) {} + virtual void run() { (obj->*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); } + virtual ITask_impl* fork() { + return new lambda(dest_func, obj, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8); + } + }; + return Task(new lambda(func, obj, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)); + } + + template + static Task gen(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8, FARG9), T* obj, ARG1 arg1, + ARG2 arg2, ARG3 arg3, ARG4 arg4, ARG5 arg5, ARG6 arg6, ARG7 arg7, ARG8 arg8, ARG9 arg9) { + struct lambda : public ITask_impl { + RET(T::*dest_func) + (FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8, FARG9); + T* obj; + ARG1 arg1; + ARG2 arg2; + ARG3 arg3; + ARG4 arg4; + ARG5 arg5; + ARG6 arg6; + ARG7 arg7; + ARG8 arg8; + ARG9 arg9; + lambda(RET (T::*func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8, FARG9), T* obj, + const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, const ARG4& arg4, const ARG5& arg5, + const ARG6& arg6, const ARG7& arg7, const ARG8& arg8, const ARG9& arg9) + : dest_func(func), + obj(obj), + arg1(arg1), + arg2(arg2), + arg3(arg3), + arg4(arg4), + arg5(arg5), + arg6(arg6), + arg7(arg7), + arg8(arg8), + arg9(arg9) {} + virtual void run() { (obj->*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); } + virtual ITask_impl* fork() { + return new lambda(dest_func, obj, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9); + } + }; + return Task(new lambda(func, obj, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)); + } }; // m_flag; - disruptorLFQ* m_disruptorLFQ; - boost::mutex m_publishLock; +public: + TaskQueue(int threadCount); + virtual ~TaskQueue(); + virtual void close(); + virtual void produce(const Task& task); + virtual int run(); + virtual bool bTaskQueueStatusOK(); + +private: + boost::atomic m_flag; + disruptorLFQ* m_disruptorLFQ; + boost::mutex m_publishLock; }; //getCode()) { - case CHECK_TRANSACTION_STATE: - // return checkTransactionState( request); - break; - case NOTIFY_CONSUMER_IDS_CHANGED: - return notifyConsumerIdsChanged(request); - break; - case RESET_CONSUMER_CLIENT_OFFSET: // oneWayRPC - return resetOffset(request); - case GET_CONSUMER_STATUS_FROM_CLIENT: - // return getConsumeStatus( request); - break; - case GET_CONSUMER_RUNNING_INFO: - return getConsumerRunningInfo(addr, request); - break; - case CONSUME_MESSAGE_DIRECTLY: - // return consumeMessageDirectly( request); - break; - default: - break; - } - return NULL; +RemotingCommand* ClientRemotingProcessor::processRequest(const string& addr, RemotingCommand* request) { + LOG_DEBUG("request Command received:processRequest"); + switch (request->getCode()) { + case CHECK_TRANSACTION_STATE: + // return checkTransactionState( request); + break; + case NOTIFY_CONSUMER_IDS_CHANGED: + return notifyConsumerIdsChanged(request); + break; + case RESET_CONSUMER_CLIENT_OFFSET: // oneWayRPC + return resetOffset(request); + case GET_CONSUMER_STATUS_FROM_CLIENT: + // return getConsumeStatus( request); + break; + case GET_CONSUMER_RUNNING_INFO: + return getConsumerRunningInfo(addr, request); + break; + case CONSUME_MESSAGE_DIRECTLY: + // return consumeMessageDirectly( request); + break; + default: + break; + } + return NULL; } -RemotingCommand* ClientRemotingProcessor::resetOffset( - RemotingCommand* request) { - request->SetExtHeader(request->getCode()); - const MemoryBlock* pbody = request->GetBody(); - if (pbody->getSize()) { - ResetOffsetBody* offsetBody = ResetOffsetBody::Decode(pbody); - ResetOffsetRequestHeader* offsetHeader = - (ResetOffsetRequestHeader*)request->getCommandHeader(); - if (offsetBody) { - m_mqClientFactory->resetOffset(offsetHeader->getGroup(), - offsetHeader->getTopic(), - offsetBody->getOffsetTable()); - } else { - LOG_ERROR( - "resetOffset failed as received data could not be unserialized"); +RemotingCommand* ClientRemotingProcessor::resetOffset(RemotingCommand* request) { + request->SetExtHeader(request->getCode()); + const MemoryBlock* pbody = request->GetBody(); + if (pbody->getSize()) { + ResetOffsetBody* offsetBody = ResetOffsetBody::Decode(pbody); + ResetOffsetRequestHeader* offsetHeader = (ResetOffsetRequestHeader*)request->getCommandHeader(); + if (offsetBody) { + m_mqClientFactory->resetOffset(offsetHeader->getGroup(), offsetHeader->getTopic(), + offsetBody->getOffsetTable()); + } else { + LOG_ERROR("resetOffset failed as received data could not be unserialized"); + } } - } - return NULL; // as resetOffset is oneWayRPC, do not need return any response + return NULL; // as resetOffset is oneWayRPC, do not need return any response } -std::map ResetOffsetBody::getOffsetTable() { - return m_offsetTable; -} +std::map ResetOffsetBody::getOffsetTable() { return m_offsetTable; } -void ResetOffsetBody::setOffsetTable(MQMessageQueue mq, int64 offset) { - m_offsetTable[mq] = offset; -} +void ResetOffsetBody::setOffsetTable(MQMessageQueue mq, int64 offset) { m_offsetTable[mq] = offset; } ResetOffsetBody* ResetOffsetBody::Decode(const MemoryBlock* mem) { - const char* const pData = static_cast(mem->getData()); - Json::Reader reader; - Json::Value root; - const char* begin = pData; - const char* end = pData + mem->getSize(); + const char* const pData = static_cast(mem->getData()); + Json::Reader reader; + Json::Value root; + const char* begin = pData; + const char* end = pData + mem->getSize(); - if (!reader.parse(begin, end, root, true)) { - LOG_ERROR("ResetOffsetBody::Decode fail"); - return NULL; - } + if (!reader.parse(begin, end, root, true)) { + LOG_ERROR("ResetOffsetBody::Decode fail"); + return NULL; + } - ResetOffsetBody* rfb = new ResetOffsetBody(); - Json::Value qds = root["offsetTable"]; - for (unsigned int i = 0; i < qds.size(); i++) { - MQMessageQueue mq; - Json::Value qd = qds[i]; - mq.setBrokerName(qd["brokerName"].asString()); - mq.setQueueId(qd["queueId"].asInt()); - mq.setTopic(qd["topic"].asString()); - int64 offset = qd["offset"].asInt64(); - LOG_INFO("ResetOffsetBody brokerName:%s, queueID:%d, topic:%s, offset:%lld", - mq.getBrokerName().c_str(), mq.getQueueId(), mq.getTopic().c_str(), - offset); - rfb->setOffsetTable(mq, offset); - } - return rfb; + ResetOffsetBody* rfb = new ResetOffsetBody(); + Json::Value qds = root["offsetTable"]; + for (unsigned int i = 0; i < qds.size(); i++) { + MQMessageQueue mq; + Json::Value qd = qds[i]; + mq.setBrokerName(qd["brokerName"].asString()); + mq.setQueueId(qd["queueId"].asInt()); + mq.setTopic(qd["topic"].asString()); + int64 offset = qd["offset"].asInt64(); + LOG_INFO("ResetOffsetBody brokerName:%s, queueID:%d, topic:%s, offset:%lld", mq.getBrokerName().c_str(), + mq.getQueueId(), mq.getTopic().c_str(), offset); + rfb->setOffsetTable(mq, offset); + } + return rfb; } -RemotingCommand* ClientRemotingProcessor::getConsumerRunningInfo( - const string& addr, RemotingCommand* request) { - request->SetExtHeader(request->getCode()); - GetConsumerRunningInfoRequestHeader* requestHeader = - (GetConsumerRunningInfoRequestHeader*)request->getCommandHeader(); - LOG_INFO("getConsumerRunningInfo:%s", - requestHeader->getConsumerGroup().c_str()); +RemotingCommand* ClientRemotingProcessor::getConsumerRunningInfo(const string& addr, RemotingCommand* request) { + request->SetExtHeader(request->getCode()); + GetConsumerRunningInfoRequestHeader* requestHeader = + (GetConsumerRunningInfoRequestHeader*)request->getCommandHeader(); + LOG_INFO("getConsumerRunningInfo:%s", requestHeader->getConsumerGroup().c_str()); - RemotingCommand* pResponse = new RemotingCommand( - request->getCode(), "CPP", request->getVersion(), request->getOpaque(), - request->getFlag(), request->getRemark(), NULL); + RemotingCommand* pResponse = + new RemotingCommand(request->getCode(), "CPP", request->getVersion(), request->getOpaque(), request->getFlag(), + request->getRemark(), NULL); - unique_ptr runningInfo( - m_mqClientFactory->consumerRunningInfo( - requestHeader->getConsumerGroup())); - if (runningInfo) { - if (requestHeader->isJstackEnable()) { - /*string jstack = UtilAll::jstack(); - consumerRunningInfo->setJstack(jstack);*/ + unique_ptr runningInfo( + m_mqClientFactory->consumerRunningInfo(requestHeader->getConsumerGroup())); + if (runningInfo) { + if (requestHeader->isJstackEnable()) { + /*string jstack = UtilAll::jstack(); + consumerRunningInfo->setJstack(jstack);*/ + } + pResponse->setCode(SUCCESS_VALUE); + string body = runningInfo->encode(); + pResponse->SetBody(body.c_str(), body.length()); + pResponse->setMsgBody(body); + } else { + pResponse->setCode(SYSTEM_ERROR); + pResponse->setRemark("The Consumer Group not exist in this consumer"); } - pResponse->setCode(SUCCESS_VALUE); - string body = runningInfo->encode(); - pResponse->SetBody(body.c_str(), body.length()); - pResponse->setMsgBody(body); - } else { - pResponse->setCode(SYSTEM_ERROR); - pResponse->setRemark("The Consumer Group not exist in this consumer"); - } - SessionCredentials sessionCredentials; - m_mqClientFactory->getSessionCredentialFromConsumer( - requestHeader->getConsumerGroup(), sessionCredentials); - ClientRPCHook rpcHook(sessionCredentials); - rpcHook.doBeforeRequest(addr, *pResponse); - pResponse->Encode(); - return pResponse; + SessionCredentials sessionCredentials; + m_mqClientFactory->getSessionCredentialFromConsumer(requestHeader->getConsumerGroup(), sessionCredentials); + ClientRPCHook rpcHook(sessionCredentials); + rpcHook.doBeforeRequest(addr, *pResponse); + pResponse->Encode(); + return pResponse; } -RemotingCommand* ClientRemotingProcessor::notifyConsumerIdsChanged( - RemotingCommand* request) { - request->SetExtHeader(request->getCode()); - NotifyConsumerIdsChangedRequestHeader* requestHeader = - (NotifyConsumerIdsChangedRequestHeader*)request->getCommandHeader(); - LOG_INFO("notifyConsumerIdsChanged:%s", requestHeader->getGroup().c_str()); - m_mqClientFactory->doRebalanceByConsumerGroup(requestHeader->getGroup()); - return NULL; +RemotingCommand* ClientRemotingProcessor::notifyConsumerIdsChanged(RemotingCommand* request) { + request->SetExtHeader(request->getCode()); + NotifyConsumerIdsChangedRequestHeader* requestHeader = + (NotifyConsumerIdsChangedRequestHeader*)request->getCommandHeader(); + LOG_INFO("notifyConsumerIdsChanged:%s", requestHeader->getGroup().c_str()); + m_mqClientFactory->doRebalanceByConsumerGroup(requestHeader->getGroup()); + return NULL; } } diff --git a/src/transport/ClientRemotingProcessor.h b/src/transport/ClientRemotingProcessor.h old mode 100755 new mode 100644 index c4d4ce6a8..6b41c7769 --- a/src/transport/ClientRemotingProcessor.h +++ b/src/transport/ClientRemotingProcessor.h @@ -25,30 +25,29 @@ namespace rocketmq { class MQClientFactory; class ClientRemotingProcessor { - public: - ClientRemotingProcessor(MQClientFactory* mqClientFactory); - virtual ~ClientRemotingProcessor(); - - RemotingCommand* processRequest(const string& addr, RemotingCommand* request); - RemotingCommand* resetOffset(RemotingCommand* request); - RemotingCommand* getConsumerRunningInfo(const string& addr, - RemotingCommand* request); - RemotingCommand* notifyConsumerIdsChanged(RemotingCommand* request); - - private: - MQClientFactory* m_mqClientFactory; +public: + ClientRemotingProcessor(MQClientFactory* mqClientFactory); + virtual ~ClientRemotingProcessor(); + + RemotingCommand* processRequest(const string& addr, RemotingCommand* request); + RemotingCommand* resetOffset(RemotingCommand* request); + RemotingCommand* getConsumerRunningInfo(const string& addr, RemotingCommand* request); + RemotingCommand* notifyConsumerIdsChanged(RemotingCommand* request); + +private: + MQClientFactory* m_mqClientFactory; }; class ResetOffsetBody { - public: - ResetOffsetBody() {} - virtual ~ResetOffsetBody() { m_offsetTable.clear(); } - void setOffsetTable(MQMessageQueue mq, int64 offset); - std::map getOffsetTable(); - static ResetOffsetBody* Decode(const MemoryBlock* mem); - - private: - std::map m_offsetTable; +public: + ResetOffsetBody() {} + virtual ~ResetOffsetBody() { m_offsetTable.clear(); } + void setOffsetTable(MQMessageQueue mq, int64 offset); + std::map getOffsetTable(); + static ResetOffsetBody* Decode(const MemoryBlock* mem); + +private: + std::map m_offsetTable; }; } diff --git a/src/transport/ResponseFuture.cpp b/src/transport/ResponseFuture.cpp old mode 100755 new mode 100644 index a304e3fcf..eb8d98cf0 --- a/src/transport/ResponseFuture.cpp +++ b/src/transport/ResponseFuture.cpp @@ -20,197 +20,170 @@ namespace rocketmq { // lk(m_defaultEventLock); - if (!m_defaultEvent.timed_wait( - lk, boost::posix_time::milliseconds(timeoutMillis))) { - LOG_WARN("waitResponse of code:%d with opaque:%d timeout", m_requestCode, - m_opaque); - m_syncResponse.store(true); - } - return m_pResponseCommand; + boost::unique_lock lk(m_defaultEventLock); + if (!m_defaultEvent.timed_wait(lk, boost::posix_time::milliseconds(timeoutMillis))) { + LOG_WARN("waitResponse of code:%d with opaque:%d timeout", m_requestCode, m_opaque); + m_syncResponse.store(true); + } + return m_pResponseCommand; } void ResponseFuture::setResponse(RemotingCommand* pResponseCommand) { - // LOG_DEBUG("setResponse of opaque:%d",m_opaque); - m_pResponseCommand = pResponseCommand; - - if (!getASyncFlag()) { - if (m_syncResponse.load() == false) { - m_defaultEvent.notify_all(); - m_syncResponse.store(true); + // LOG_DEBUG("setResponse of opaque:%d",m_opaque); + m_pResponseCommand = pResponseCommand; + + if (!getASyncFlag()) { + if (m_syncResponse.load() == false) { + m_defaultEvent.notify_all(); + m_syncResponse.store(true); + } } - } } const bool ResponseFuture::getSyncResponseFlag() { - if (m_syncResponse.load() == true) { - return true; - } - return false; + if (m_syncResponse.load() == true) { + return true; + } + return false; } const bool ResponseFuture::getAsyncResponseFlag() { - if (m_asyncResponse.load() == true) { - // LOG_DEBUG("ASYNC flag is TRUE,opaque is:%d",getOpaque() ); - return true; - } + if (m_asyncResponse.load() == true) { + // LOG_DEBUG("ASYNC flag is TRUE,opaque is:%d",getOpaque() ); + return true; + } - return false; + return false; } void ResponseFuture::setAsyncResponseFlag() { m_asyncResponse.store(true); } const bool ResponseFuture::getASyncFlag() { - if (m_bAsync.load() == true) { - // LOG_DEBUG("ASYNC flag is TRUE,opaque is:%d",getOpaque() ); - return true; - } - return false; + if (m_bAsync.load() == true) { + // LOG_DEBUG("ASYNC flag is TRUE,opaque is:%d",getOpaque() ); + return true; + } + return false; } bool ResponseFuture::isSendRequestOK() { return m_sendRequestOK; } -void ResponseFuture::setSendRequestOK(bool sendRequestOK) { - m_sendRequestOK = sendRequestOK; -} +void ResponseFuture::setSendRequestOK(bool sendRequestOK) { m_sendRequestOK = sendRequestOK; } int ResponseFuture::getOpaque() const { return m_opaque; } int ResponseFuture::getRequestCode() const { return m_requestCode; } -void ResponseFuture::setAsyncCallBackStatus( - asyncCallBackStatus asyncCallbackStatus) { - boost::lock_guard lock(m_asyncCallbackLock); - if (m_asyncCallbackStatus == asyncCallBackStatus_init) { - m_asyncCallbackStatus = asyncCallbackStatus; - } +void ResponseFuture::setAsyncCallBackStatus(asyncCallBackStatus asyncCallbackStatus) { + boost::lock_guard lock(m_asyncCallbackLock); + if (m_asyncCallbackStatus == asyncCallBackStatus_init) { + m_asyncCallbackStatus = asyncCallbackStatus; + } } void ResponseFuture::executeInvokeCallback() { - if (m_pCallbackWrap == NULL) { - deleteAndZero(m_pResponseCommand); - return; - } else { - if (m_asyncCallbackStatus == asyncCallBackStatus_response) { - m_pCallbackWrap->operationComplete(this, true); + if (m_pCallbackWrap == NULL) { + deleteAndZero(m_pResponseCommand); + return; } else { - if (m_pResponseCommand) - deleteAndZero(m_pResponseCommand); // the responseCommand from - // RemotingCommand::Decode(mem) will - // only deleted by operationComplete - // automatically - LOG_WARN( - "timeout and response incoming concurrently of opaque:%d, and " - "executeInvokeCallbackException was called earlier", - m_opaque); + if (m_asyncCallbackStatus == asyncCallBackStatus_response) { + m_pCallbackWrap->operationComplete(this, true); + } else { + if (m_pResponseCommand) deleteAndZero(m_pResponseCommand); // the responseCommand from + // RemotingCommand::Decode(mem) will + // only deleted by operationComplete + // automatically + LOG_WARN( + "timeout and response incoming concurrently of opaque:%d, and " + "executeInvokeCallbackException was called earlier", + m_opaque); + } } - } } void ResponseFuture::executeInvokeCallbackException() { - if (m_pCallbackWrap == NULL) { - LOG_ERROR("m_pCallbackWrap is NULL, critical error"); - return; - } else { - if (m_asyncCallbackStatus == asyncCallBackStatus_timeout) { - - //here no need retrySendTimes process because of it have timeout - LOG_ERROR("send msg, callback timeout, opaque:%d, sendTimes:%d, maxRetryTimes:%d", getOpaque(), getRetrySendTimes(), getMaxRetrySendTimes()); - - m_pCallbackWrap->onException(); + if (m_pCallbackWrap == NULL) { + LOG_ERROR("m_pCallbackWrap is NULL, critical error"); + return; } else { - LOG_WARN( - "timeout and response incoming concurrently of opaque:%d, and " - "executeInvokeCallback was called earlier", - m_opaque); + if (m_asyncCallbackStatus == asyncCallBackStatus_timeout) { + + // here no need retrySendTimes process because of it have timeout + LOG_ERROR("send msg, callback timeout, opaque:%d, sendTimes:%d, maxRetryTimes:%d", getOpaque(), + getRetrySendTimes(), getMaxRetrySendTimes()); + + m_pCallbackWrap->onException(); + } else { + LOG_WARN( + "timeout and response incoming concurrently of opaque:%d, and " + "executeInvokeCallback was called earlier", + m_opaque); + } } - } } bool ResponseFuture::isTimeOut() const { - int64 diff = UtilAll::currentTimeMillis() - m_beginTimestamp; - // m_timeout; + int64 diff = UtilAll::currentTimeMillis() - m_beginTimestamp; + // m_timeout; } -int ResponseFuture::getMaxRetrySendTimes() const { - return m_maxRetrySendTimes; -} -int ResponseFuture::getRetrySendTimes() const { - return m_retrySendTimes; -} +int ResponseFuture::getMaxRetrySendTimes() const { return m_maxRetrySendTimes; } +int ResponseFuture::getRetrySendTimes() const { return m_retrySendTimes; } -void ResponseFuture::setMaxRetrySendTimes(int maxRetryTimes) { - m_maxRetrySendTimes = maxRetryTimes; -} -void ResponseFuture::setRetrySendTimes(int retryTimes) { - m_retrySendTimes = retryTimes; -} +void ResponseFuture::setMaxRetrySendTimes(int maxRetryTimes) { m_maxRetrySendTimes = maxRetryTimes; } +void ResponseFuture::setRetrySendTimes(int retryTimes) { m_retrySendTimes = retryTimes; } -void ResponseFuture::setBrokerAddr(const std::string& brokerAddr) { - m_brokerAddr = brokerAddr; -} -void ResponseFuture::setRequestCommand(const RemotingCommand& requestCommand) { - m_requestCommand = requestCommand; -} +void ResponseFuture::setBrokerAddr(const std::string& brokerAddr) { m_brokerAddr = brokerAddr; } +void ResponseFuture::setRequestCommand(const RemotingCommand& requestCommand) { m_requestCommand = requestCommand; } -const RemotingCommand& ResponseFuture::getRequestCommand() { - return m_requestCommand; -} -std::string ResponseFuture::getBrokerAddr() const { - return m_brokerAddr; -} +const RemotingCommand& ResponseFuture::getRequestCommand() { return m_requestCommand; } +std::string ResponseFuture::getBrokerAddr() const { return m_brokerAddr; } int64 ResponseFuture::leftTime() const { int64 diff = UtilAll::currentTimeMillis() - m_beginTimestamp; return m_timeout - diff; } -RemotingCommand* ResponseFuture::getCommand() const { - return m_pResponseCommand; -} +RemotingCommand* ResponseFuture::getCommand() const { return m_pResponseCommand; } -AsyncCallbackWrap* ResponseFuture::getAsyncCallbackWrap() { - return m_pCallbackWrap; -} +AsyncCallbackWrap* ResponseFuture::getAsyncCallbackWrap() { return m_pCallbackWrap; } // m_bAsync; - RemotingCommand* m_pResponseCommand; // m_asyncResponse; - boost::atomic m_syncResponse; + // m_bAsync; + RemotingCommand* m_pResponseCommand; // m_asyncResponse; + boost::atomic m_syncResponse; + + int m_maxRetrySendTimes; + int m_retrySendTimes; + std::string m_brokerAddr; + RemotingCommand m_requestCommand; + // TcpRemotingClient* m_tcpRemoteClient; }; //h_aliases; - if (*alias != 0) { - return *alias; - } else { - return inet_ntoa(in.sin_addr); - } + struct hostent* remoteHost = gethostbyaddr((char*)&(in.sin_addr), 4, AF_INET); + char** alias = remoteHost->h_aliases; + if (*alias != 0) { + return *alias; + } else { + return inet_ntoa(in.sin_addr); + } } uint64 swapll(uint64 v) { #ifdef ENDIANMODE_BIG - return v; + return v; #else - uint64 ret = ((v << 56) | ((v & 0xff00) << 40) | ((v & 0xff0000) << 24) | - ((v & 0xff000000) << 8) | ((v >> 8) & 0xff000000) | - ((v >> 24) & 0xff0000) | ((v >> 40) & 0xff00) | (v >> 56)); + uint64 ret = ((v << 56) | ((v & 0xff00) << 40) | ((v & 0xff0000) << 24) | ((v & 0xff000000) << 8) | + ((v >> 8) & 0xff000000) | ((v >> 24) & 0xff0000) | ((v >> 40) & 0xff00) | (v >> 56)); - return ret; + return ret; #endif } diff --git a/src/transport/SocketUtil.h b/src/transport/SocketUtil.h old mode 100755 new mode 100644 diff --git a/src/transport/TcpRemotingClient.cpp b/src/transport/TcpRemotingClient.cpp old mode 100755 new mode 100644 index 7b9dff990..2a217e632 --- a/src/transport/TcpRemotingClient.cpp +++ b/src/transport/TcpRemotingClient.cpp @@ -27,731 +27,659 @@ namespace rocketmq { //interrupt(); - m_async_service_thread->join(); - removeAllTimerCallback(); + LOG_DEBUG("TcpRemotingClient::stopAllTcpTransportThread Begin"); + m_async_ioService.stop(); + m_async_service_thread->interrupt(); + m_async_service_thread->join(); + removeAllTimerCallback(); - { - TcpMap::iterator it = m_tcpTable.begin(); - for (; it != m_tcpTable.end(); ++it) { - it->second->disconnect(it->first); + { + TcpMap::iterator it = m_tcpTable.begin(); + for (; it != m_tcpTable.end(); ++it) { + it->second->disconnect(it->first); + } + m_tcpTable.clear(); } - m_tcpTable.clear(); - } - m_ioService.stop(); - m_threadpool.join_all(); + m_ioService.stop(); + m_threadpool.join_all(); - { - boost::lock_guard lock(m_futureTableMutex); - for (ResMap::iterator it = m_futureTable.begin(); it != m_futureTable.end(); - ++it) { - if (it->second) it->second->releaseThreadCondition(); + { + boost::lock_guard lock(m_futureTableMutex); + for (ResMap::iterator it = m_futureTable.begin(); it != m_futureTable.end(); ++it) { + if (it->second) it->second->releaseThreadCondition(); + } } - } - LOG_DEBUG("TcpRemotingClient::stopAllTcpTransportThread End"); + LOG_DEBUG("TcpRemotingClient::stopAllTcpTransportThread End"); } void TcpRemotingClient::updateNameServerAddressList(const string& addrs) { - if (!addrs.empty()) { - boost::unique_lock lock(m_namesrvlock, - boost::try_to_lock); - if (!lock.owns_lock()) { - if (!lock.timed_lock(boost::get_system_time() + - boost::posix_time::seconds(10))) { - LOG_ERROR("updateNameServerAddressList get timed_mutex timeout"); - return; - } + if (!addrs.empty()) { + boost::unique_lock lock(m_namesrvlock, boost::try_to_lock); + if (!lock.owns_lock()) { + if (!lock.timed_lock(boost::get_system_time() + boost::posix_time::seconds(10))) { + LOG_ERROR("updateNameServerAddressList get timed_mutex timeout"); + return; + } + } + // clear first; + m_namesrvAddrList.clear(); + + vector out; + UtilAll::Split(out, addrs, ";"); + for (size_t i = 0; i < out.size(); i++) { + string addr = out[i]; + UtilAll::Trim(addr); + + string hostName; + short portNumber; + if (UtilAll::SplitURL(addr, hostName, portNumber)) { + LOG_INFO("update Namesrv:%s", addr.c_str()); + m_namesrvAddrList.push_back(addr); + } + } + out.clear(); } - // clear first; - m_namesrvAddrList.clear(); - - vector out; - UtilAll::Split(out, addrs, ";"); - for (size_t i = 0; i < out.size(); i++) { - string addr = out[i]; - UtilAll::Trim(addr); - - string hostName; - short portNumber; - if (UtilAll::SplitURL(addr, hostName, portNumber)) { - LOG_INFO("update Namesrv:%s", addr.c_str()); - m_namesrvAddrList.push_back(addr); - } - } - out.clear(); - } } -bool TcpRemotingClient::invokeHeartBeat(const string& addr, - RemotingCommand& request) { - boost::shared_ptr pTcp = GetTransport(addr, true); - if (pTcp != NULL) { - int code = request.getCode(); - int opaque = request.getOpaque(); - boost::shared_ptr responseFuture( - new ResponseFuture(code, opaque, this, 3000, false, NULL)); - addResponseFuture(opaque, responseFuture); - // LOG_INFO("invokeHeartbeat success, addr:%s, code:%d, opaque:%d, - // timeoutms:%d", addr.c_str(), code, opaque, 3000); - - if (SendCommand(pTcp, request)) { - responseFuture->setSendRequestOK(true); - unique_ptr pRsp(responseFuture->waitResponse(3000)); - if (pRsp == NULL) { - LOG_ERROR( - "wait response timeout of heartbeat, so closeTransport of addr:%s", - addr.c_str()); - CloseTransport(addr, pTcp); - return false; - } else if (pRsp->getCode() == SUCCESS_VALUE) { - return true; - } else { - LOG_WARN("get error response:%d of heartbeat to addr:%s", - pRsp->getCode(), addr.c_str()); - return false; - } - } else { - CloseTransport(addr, pTcp); +bool TcpRemotingClient::invokeHeartBeat(const string& addr, RemotingCommand& request) { + boost::shared_ptr pTcp = GetTransport(addr, true); + if (pTcp != NULL) { + int code = request.getCode(); + int opaque = request.getOpaque(); + boost::shared_ptr responseFuture(new ResponseFuture(code, opaque, this, 3000, false, NULL)); + addResponseFuture(opaque, responseFuture); + // LOG_INFO("invokeHeartbeat success, addr:%s, code:%d, opaque:%d, + // timeoutms:%d", addr.c_str(), code, opaque, 3000); + + if (SendCommand(pTcp, request)) { + responseFuture->setSendRequestOK(true); + unique_ptr pRsp(responseFuture->waitResponse(3000)); + if (pRsp == NULL) { + LOG_ERROR("wait response timeout of heartbeat, so closeTransport of addr:%s", addr.c_str()); + CloseTransport(addr, pTcp); + return false; + } else if (pRsp->getCode() == SUCCESS_VALUE) { + return true; + } else { + LOG_WARN("get error response:%d of heartbeat to addr:%s", pRsp->getCode(), addr.c_str()); + return false; + } + } else { + CloseTransport(addr, pTcp); + } } - } - return false; + return false; } -RemotingCommand* TcpRemotingClient::invokeSync(const string& addr, - RemotingCommand& request, +RemotingCommand* TcpRemotingClient::invokeSync(const string& addr, RemotingCommand& request, int timeoutMillis /* = 3000 */) { - boost::shared_ptr pTcp = GetTransport(addr, true); - if (pTcp != NULL) { - int code = request.getCode(); - int opaque = request.getOpaque(); - boost::shared_ptr responseFuture( - new ResponseFuture(code, opaque, this, timeoutMillis, false, NULL)); - addResponseFuture(opaque, responseFuture); - - if (SendCommand(pTcp, request)) { - // LOG_INFO("invokeSync success, addr:%s, code:%d, opaque:%d, - // timeoutms:%d", addr.c_str(), code, opaque, timeoutMillis); - responseFuture->setSendRequestOK(true); - RemotingCommand* pRsp = responseFuture->waitResponse(timeoutMillis); - if (pRsp == NULL) { - if (code != GET_CONSUMER_LIST_BY_GROUP) { - LOG_WARN( - "wait response timeout or get NULL response of code:%d, so " - "closeTransport of addr:%s", - code, addr.c_str()); - CloseTransport(addr, pTcp); + boost::shared_ptr pTcp = GetTransport(addr, true); + if (pTcp != NULL) { + int code = request.getCode(); + int opaque = request.getOpaque(); + boost::shared_ptr responseFuture( + new ResponseFuture(code, opaque, this, timeoutMillis, false, NULL)); + addResponseFuture(opaque, responseFuture); + + if (SendCommand(pTcp, request)) { + // LOG_INFO("invokeSync success, addr:%s, code:%d, opaque:%d, + // timeoutms:%d", addr.c_str(), code, opaque, timeoutMillis); + responseFuture->setSendRequestOK(true); + RemotingCommand* pRsp = responseFuture->waitResponse(timeoutMillis); + if (pRsp == NULL) { + if (code != GET_CONSUMER_LIST_BY_GROUP) { + LOG_WARN( + "wait response timeout or get NULL response of code:%d, so " + "closeTransport of addr:%s", + code, addr.c_str()); + CloseTransport(addr, pTcp); + } + // avoid responseFuture leak; + findAndDeleteResponseFuture(opaque); + return NULL; + } else { + return pRsp; + } + } else { + // avoid responseFuture leak; + findAndDeleteResponseFuture(opaque); + CloseTransport(addr, pTcp); } - // avoid responseFuture leak; - findAndDeleteResponseFuture(opaque); - return NULL; - } else { - return pRsp; - } - } else { - // avoid responseFuture leak; - findAndDeleteResponseFuture(opaque); - CloseTransport(addr, pTcp); } - } - return NULL; + return NULL; } -bool TcpRemotingClient::invokeAsync(const string& addr, - RemotingCommand& request, - AsyncCallbackWrap* cbw, - int64 timeoutMilliseconds, - int maxRetrySendTimes, - int retrySendTimes) { - boost::shared_ptr pTcp = GetTransport(addr, true); - if (pTcp != NULL) { - // responseFuture( - new ResponseFuture(code, opaque, this, timeoutMilliseconds, true, cbw)); - responseFuture->setMaxRetrySendTimes(maxRetrySendTimes); - responseFuture->setRetrySendTimes(retrySendTimes); - responseFuture->setBrokerAddr(addr); - responseFuture->setRequestCommand(request); - addAsyncResponseFuture(opaque, responseFuture); - if (cbw) { - boost::asio::deadline_timer* t = new boost::asio::deadline_timer( - m_async_ioService, - boost::posix_time::milliseconds(timeoutMilliseconds)); - addTimerCallback(t, opaque); - boost::system::error_code e; - t->async_wait( - boost::bind(&TcpRemotingClient::handleAsyncPullForResponseTimeout, - this, e, opaque)); - } - - if (SendCommand(pTcp, request)) // Even if send failed, asyncTimerThread - // will trigger next pull request or report - // send msg failed - { - LOG_DEBUG("invokeAsync success, addr:%s, code:%d, opaque:%d", - addr.c_str(), code, opaque); - responseFuture->setSendRequestOK(true); - } - return true; - } - LOG_ERROR("invokeAsync failed of addr:%s", addr.c_str()); - return false; +bool TcpRemotingClient::invokeAsync(const string& addr, RemotingCommand& request, AsyncCallbackWrap* cbw, + int64 timeoutMilliseconds, int maxRetrySendTimes, int retrySendTimes) { + boost::shared_ptr pTcp = GetTransport(addr, true); + if (pTcp != NULL) { + // responseFuture( + new ResponseFuture(code, opaque, this, timeoutMilliseconds, true, cbw)); + responseFuture->setMaxRetrySendTimes(maxRetrySendTimes); + responseFuture->setRetrySendTimes(retrySendTimes); + responseFuture->setBrokerAddr(addr); + responseFuture->setRequestCommand(request); + addAsyncResponseFuture(opaque, responseFuture); + if (cbw) { + boost::asio::deadline_timer* t = new boost::asio::deadline_timer( + m_async_ioService, boost::posix_time::milliseconds(timeoutMilliseconds)); + addTimerCallback(t, opaque); + boost::system::error_code e; + t->async_wait(boost::bind(&TcpRemotingClient::handleAsyncPullForResponseTimeout, this, e, opaque)); + } + + if (SendCommand(pTcp, request)) // Even if send failed, asyncTimerThread + // will trigger next pull request or report + // send msg failed + { + LOG_DEBUG("invokeAsync success, addr:%s, code:%d, opaque:%d", addr.c_str(), code, opaque); + responseFuture->setSendRequestOK(true); + } + return true; + } + LOG_ERROR("invokeAsync failed of addr:%s", addr.c_str()); + return false; } -void TcpRemotingClient::invokeOneway(const string& addr, - RemotingCommand& request) { - // pTcp = GetTransport(addr, true); - if (pTcp != NULL) { - request.markOnewayRPC(); - LOG_DEBUG("invokeOneway success, addr:%s, code:%d", addr.c_str(), - request.getCode()); - SendCommand(pTcp, request); - } +void TcpRemotingClient::invokeOneway(const string& addr, RemotingCommand& request) { + // pTcp = GetTransport(addr, true); + if (pTcp != NULL) { + request.markOnewayRPC(); + LOG_DEBUG("invokeOneway success, addr:%s, code:%d", addr.c_str(), request.getCode()); + SendCommand(pTcp, request); + } } -boost::shared_ptr TcpRemotingClient::GetTransport( - const string& addr, bool needRespons) { - if (addr.empty()) return CreateNameserverTransport(needRespons); +boost::shared_ptr TcpRemotingClient::GetTransport(const string& addr, bool needRespons) { + if (addr.empty()) return CreateNameserverTransport(needRespons); - return CreateTransport(addr, needRespons); + return CreateTransport(addr, needRespons); } -boost::shared_ptr TcpRemotingClient::CreateTransport( - const string& addr, bool needRespons) { - boost::shared_ptr tts; - { - // try get m_tcpLock util m_tcpTransportTryLockTimeout to avoid blocking - // long - // time, if could not get m_tcpLock, return NULL - bool bGetMutex = false; - boost::unique_lock lock(m_tcpLock, boost::try_to_lock); - if (!lock.owns_lock()) { - if (!lock.timed_lock( - boost::get_system_time() + - boost::posix_time::seconds(m_tcpTransportTryLockTimeout))) { - LOG_ERROR("GetTransport of:%s get timed_mutex timeout", addr.c_str()); - boost::shared_ptr pTcp; - return pTcp; - } else { - bGetMutex = true; - } - } else { - bGetMutex = true; - } - if (bGetMutex) { - if (m_tcpTable.find(addr) != m_tcpTable.end()) { - boost::weak_ptr weakPtcp(m_tcpTable[addr]); - boost::shared_ptr tcp = weakPtcp.lock(); - if (tcp) { - tcpConnectStatus connectStatus = tcp->getTcpConnectStatus(); - if (connectStatus == e_connectWaitResponse) { +boost::shared_ptr TcpRemotingClient::CreateTransport(const string& addr, bool needRespons) { + boost::shared_ptr tts; + { + // try get m_tcpLock util m_tcpTransportTryLockTimeout to avoid blocking + // long + // time, if could not get m_tcpLock, return NULL + bool bGetMutex = false; + boost::unique_lock lock(m_tcpLock, boost::try_to_lock); + if (!lock.owns_lock()) { + if (!lock.timed_lock(boost::get_system_time() + boost::posix_time::seconds(m_tcpTransportTryLockTimeout))) { + LOG_ERROR("GetTransport of:%s get timed_mutex timeout", addr.c_str()); + boost::shared_ptr pTcp; + return pTcp; + } else { + bGetMutex = true; + } + } else { + bGetMutex = true; + } + if (bGetMutex) { + if (m_tcpTable.find(addr) != m_tcpTable.end()) { + boost::weak_ptr weakPtcp(m_tcpTable[addr]); + boost::shared_ptr tcp = weakPtcp.lock(); + if (tcp) { + tcpConnectStatus connectStatus = tcp->getTcpConnectStatus(); + if (connectStatus == e_connectWaitResponse) { + boost::shared_ptr pTcp; + return pTcp; + } else if (connectStatus == e_connectFail) { + LOG_ERROR("tcpTransport with server disconnected, erase server:%s", addr.c_str()); + tcp->disconnect(addr); // avoid coredump when connection with broker was broken + m_tcpTable.erase(addr); + } else if (connectStatus == e_connectSuccess) { + return tcp; + } else { + LOG_ERROR( + "go to fault state, erase:%s from tcpMap, and reconnect " + "it", + addr.c_str()); + m_tcpTable.erase(addr); + } + } + } + + //connect(addr, m_tcpConnectTimeout); + if (connectStatus != e_connectWaitResponse) { + LOG_WARN("can not connect to :%s", addr.c_str()); + tts->disconnect(addr); + boost::shared_ptr pTcp; + return pTcp; + } else { + m_tcpTable[addr] = tts; // even if connecting failed finally, this + // server transport will be erased by next + // CreateTransport + } + } else { + LOG_WARN("get tcpTransport mutex failed :%s", addr.c_str()); boost::shared_ptr pTcp; return pTcp; - } else if (connectStatus == e_connectFail) { - LOG_ERROR("tcpTransport with server disconnected, erase server:%s", - addr.c_str()); - tcp->disconnect( - addr); // avoid coredump when connection with broker was broken - m_tcpTable.erase(addr); - } else if (connectStatus == e_connectSuccess) { - return tcp; - } else { - LOG_ERROR( - "go to fault state, erase:%s from tcpMap, and reconnect " - "it", - addr.c_str()); - m_tcpTable.erase(addr); - } } - } - - //connect(addr, m_tcpConnectTimeout); - if (connectStatus != e_connectWaitResponse) { - LOG_WARN("can not connect to :%s", addr.c_str()); + tcpConnectStatus connectStatus = tts->waitTcpConnectEvent(m_tcpConnectTimeout); + if (connectStatus != e_connectSuccess) { + LOG_WARN("can not connect to server:%s", addr.c_str()); tts->disconnect(addr); boost::shared_ptr pTcp; return pTcp; - } else { - m_tcpTable[addr] = tts; // even if connecting failed finally, this - // server transport will be erased by next - // CreateTransport - } } else { - LOG_WARN("get tcpTransport mutex failed :%s", addr.c_str()); - boost::shared_ptr pTcp; - return pTcp; - } - } - - tcpConnectStatus connectStatus = - tts->waitTcpConnectEvent(m_tcpConnectTimeout); - if (connectStatus != e_connectSuccess) { - LOG_WARN("can not connect to server:%s", addr.c_str()); - tts->disconnect(addr); - boost::shared_ptr pTcp; - return pTcp; - } else { - LOG_INFO("connect server with addr:%s success", addr.c_str()); - return tts; - } + LOG_INFO("connect server with addr:%s success", addr.c_str()); + return tts; + } } -boost::shared_ptr TcpRemotingClient::CreateNameserverTransport( - bool needRespons) { - // m_namesrvLock was added to avoid operation of nameServer was blocked by - // m_tcpLock, it was used by single Thread mostly, so no performance impact - // try get m_tcpLock util m_tcpTransportTryLockTimeout to avoid blocking long - // time, if could not get m_namesrvlock, return NULL - bool bGetMutex = false; - boost::unique_lock lock(m_namesrvlock, - boost::try_to_lock); - if (!lock.owns_lock()) { - if (!lock.timed_lock( - boost::get_system_time() + - boost::posix_time::seconds(m_tcpTransportTryLockTimeout))) { - LOG_ERROR("CreateNameserverTransport get timed_mutex timeout"); - boost::shared_ptr pTcp; - return pTcp; +boost::shared_ptr TcpRemotingClient::CreateNameserverTransport(bool needRespons) { + // m_namesrvLock was added to avoid operation of nameServer was blocked by + // m_tcpLock, it was used by single Thread mostly, so no performance impact + // try get m_tcpLock util m_tcpTransportTryLockTimeout to avoid blocking long + // time, if could not get m_namesrvlock, return NULL + bool bGetMutex = false; + boost::unique_lock lock(m_namesrvlock, boost::try_to_lock); + if (!lock.owns_lock()) { + if (!lock.timed_lock(boost::get_system_time() + boost::posix_time::seconds(m_tcpTransportTryLockTimeout))) { + LOG_ERROR("CreateNameserverTransport get timed_mutex timeout"); + boost::shared_ptr pTcp; + return pTcp; + } else { + bGetMutex = true; + } } else { - bGetMutex = true; + bGetMutex = true; } - } else { - bGetMutex = true; - } - if (bGetMutex) { - if (!m_namesrvAddrChoosed.empty()) { - boost::shared_ptr pTcp = - GetTransport(m_namesrvAddrChoosed, true); - if (pTcp) + if (bGetMutex) { + if (!m_namesrvAddrChoosed.empty()) { + boost::shared_ptr pTcp = GetTransport(m_namesrvAddrChoosed, true); + if (pTcp) + return pTcp; + else + m_namesrvAddrChoosed.clear(); + } + + vector::iterator itp = m_namesrvAddrList.begin(); + for (; itp != m_namesrvAddrList.end(); ++itp) { + unsigned int index = m_namesrvIndex % m_namesrvAddrList.size(); + if (m_namesrvIndex == numeric_limits::max()) m_namesrvIndex = 0; + m_namesrvIndex++; + LOG_INFO("namesrvIndex is:%d, index:%d, namesrvaddrlist size:" SIZET_FMT "", m_namesrvIndex, index, + m_namesrvAddrList.size()); + boost::shared_ptr pTcp = GetTransport(m_namesrvAddrList[index], true); + if (pTcp) { + m_namesrvAddrChoosed = m_namesrvAddrList[index]; + return pTcp; + } + } + boost::shared_ptr pTcp; return pTcp; - else - m_namesrvAddrChoosed.clear(); - } - - vector::iterator itp = m_namesrvAddrList.begin(); - for (; itp != m_namesrvAddrList.end(); ++itp) { - unsigned int index = m_namesrvIndex % m_namesrvAddrList.size(); - if (m_namesrvIndex == numeric_limits::max()) - m_namesrvIndex = 0; - m_namesrvIndex++; - LOG_INFO("namesrvIndex is:%d, index:%d, namesrvaddrlist size:" SIZET_FMT - "", - m_namesrvIndex, index, m_namesrvAddrList.size()); - boost::shared_ptr pTcp = - GetTransport(m_namesrvAddrList[index], true); - if (pTcp) { - m_namesrvAddrChoosed = m_namesrvAddrList[index]; + } else { + LOG_WARN("get nameServer tcpTransport mutex failed"); + boost::shared_ptr pTcp; return pTcp; - } - } - boost::shared_ptr pTcp; - return pTcp; - } else { - LOG_WARN("get nameServer tcpTransport mutex failed"); - boost::shared_ptr pTcp; - return pTcp; - } + } } -void TcpRemotingClient::CloseTransport(const string& addr, - boost::shared_ptr pTcp) { - if (addr.empty()) { - return CloseNameServerTransport(pTcp); - } - - bool bGetMutex = false; - boost::unique_lock lock(m_tcpLock, boost::try_to_lock); - if (!lock.owns_lock()) { - if (!lock.timed_lock( - boost::get_system_time() + - boost::posix_time::seconds(m_tcpTransportTryLockTimeout))) { - LOG_ERROR("CloseTransport of:%s get timed_mutex timeout", addr.c_str()); - return; +void TcpRemotingClient::CloseTransport(const string& addr, boost::shared_ptr pTcp) { + if (addr.empty()) { + return CloseNameServerTransport(pTcp); + } + + bool bGetMutex = false; + boost::unique_lock lock(m_tcpLock, boost::try_to_lock); + if (!lock.owns_lock()) { + if (!lock.timed_lock(boost::get_system_time() + boost::posix_time::seconds(m_tcpTransportTryLockTimeout))) { + LOG_ERROR("CloseTransport of:%s get timed_mutex timeout", addr.c_str()); + return; + } else { + bGetMutex = true; + } } else { - bGetMutex = true; - } - } else { - bGetMutex = true; - } - LOG_ERROR("CloseTransport of:%s", addr.c_str()); - if (bGetMutex) { - bool removeItemFromTable = true; - if (m_tcpTable.find(addr) != m_tcpTable.end()) { - if (m_tcpTable[addr]->getStartTime() != pTcp->getStartTime()) { - LOG_INFO( - "tcpTransport with addr:%s has been closed before, and has been " - "created again, nothing to do", - addr.c_str()); - removeItemFromTable = false; - } + bGetMutex = true; + } + LOG_ERROR("CloseTransport of:%s", addr.c_str()); + if (bGetMutex) { + bool removeItemFromTable = true; + if (m_tcpTable.find(addr) != m_tcpTable.end()) { + if (m_tcpTable[addr]->getStartTime() != pTcp->getStartTime()) { + LOG_INFO( + "tcpTransport with addr:%s has been closed before, and has been " + "created again, nothing to do", + addr.c_str()); + removeItemFromTable = false; + } + } else { + LOG_INFO("tcpTransport with addr:%s had been removed from tcpTable before", addr.c_str()); + removeItemFromTable = false; + } + + if (removeItemFromTable == true) { + LOG_WARN("closeTransport: disconnect broker:%s with state:%d", addr.c_str(), + m_tcpTable[addr]->getTcpConnectStatus()); + if (m_tcpTable[addr]->getTcpConnectStatus() == e_connectSuccess) + m_tcpTable[addr]->disconnect(addr); // avoid coredump when connection with server was broken + LOG_WARN("closeTransport: erase broker: %s", addr.c_str()); + m_tcpTable.erase(addr); + } } else { - LOG_INFO( - "tcpTransport with addr:%s had been removed from tcpTable before", - addr.c_str()); - removeItemFromTable = false; - } - - if (removeItemFromTable == true) { - LOG_WARN("closeTransport: disconnect broker:%s with state:%d", - addr.c_str(), m_tcpTable[addr]->getTcpConnectStatus()); - if (m_tcpTable[addr]->getTcpConnectStatus() == e_connectSuccess) - m_tcpTable[addr]->disconnect( - addr); // avoid coredump when connection with server was broken - LOG_WARN("closeTransport: erase broker: %s", addr.c_str()); - m_tcpTable.erase(addr); - } - } else { - LOG_WARN("CloseTransport::get tcpTransport mutex failed:%s", addr.c_str()); - return; - } - LOG_ERROR("CloseTransport of:%s end", addr.c_str()); + LOG_WARN("CloseTransport::get tcpTransport mutex failed:%s", addr.c_str()); + return; + } + LOG_ERROR("CloseTransport of:%s end", addr.c_str()); } -void TcpRemotingClient::CloseNameServerTransport( - boost::shared_ptr pTcp) { - bool bGetMutex = false; - boost::unique_lock lock(m_namesrvlock, - boost::try_to_lock); - if (!lock.owns_lock()) { - if (!lock.timed_lock( - boost::get_system_time() + - boost::posix_time::seconds(m_tcpTransportTryLockTimeout))) { - LOG_ERROR("CreateNameserverTransport get timed_mutex timeout"); - return; +void TcpRemotingClient::CloseNameServerTransport(boost::shared_ptr pTcp) { + bool bGetMutex = false; + boost::unique_lock lock(m_namesrvlock, boost::try_to_lock); + if (!lock.owns_lock()) { + if (!lock.timed_lock(boost::get_system_time() + boost::posix_time::seconds(m_tcpTransportTryLockTimeout))) { + LOG_ERROR("CreateNameserverTransport get timed_mutex timeout"); + return; + } else { + bGetMutex = true; + } } else { - bGetMutex = true; - } - } else { - bGetMutex = true; - } - if (bGetMutex) { - string addr = m_namesrvAddrChoosed; - bool removeItemFromTable = true; - if (m_tcpTable.find(addr) != m_tcpTable.end()) { - if (m_tcpTable[addr]->getStartTime() != pTcp->getStartTime()) { - LOG_INFO( - "tcpTransport with addr:%s has been closed before, and has been " - "created again, nothing to do", - addr.c_str()); - removeItemFromTable = false; - } + bGetMutex = true; + } + if (bGetMutex) { + string addr = m_namesrvAddrChoosed; + bool removeItemFromTable = true; + if (m_tcpTable.find(addr) != m_tcpTable.end()) { + if (m_tcpTable[addr]->getStartTime() != pTcp->getStartTime()) { + LOG_INFO( + "tcpTransport with addr:%s has been closed before, and has been " + "created again, nothing to do", + addr.c_str()); + removeItemFromTable = false; + } + } else { + LOG_INFO("tcpTransport with addr:%s had been removed from tcpTable before", addr.c_str()); + removeItemFromTable = false; + } + + if (removeItemFromTable == true) { + m_tcpTable[addr]->disconnect(addr); // avoid coredump when connection with server was broken + LOG_WARN("closeTransport: erase broker: %s", addr.c_str()); + m_tcpTable.erase(addr); + m_namesrvAddrChoosed.clear(); + } } else { - LOG_INFO( - "tcpTransport with addr:%s had been removed from tcpTable before", - addr.c_str()); - removeItemFromTable = false; - } - - if (removeItemFromTable == true) { - m_tcpTable[addr]->disconnect( - addr); // avoid coredump when connection with server was broken - LOG_WARN("closeTransport: erase broker: %s", addr.c_str()); - m_tcpTable.erase(addr); - m_namesrvAddrChoosed.clear(); - } - } else { - LOG_WARN("CloseNameServerTransport::get tcpTransport mutex failed:%s", - m_namesrvAddrChoosed.c_str()); - return; - } + LOG_WARN("CloseNameServerTransport::get tcpTransport mutex failed:%s", m_namesrvAddrChoosed.c_str()); + return; + } } -bool TcpRemotingClient::SendCommand(boost::shared_ptr pTts, - RemotingCommand& msg) { - const MemoryBlock* phead = msg.GetHead(); - const MemoryBlock* pbody = msg.GetBody(); - - unique_ptr result(new MemoryOutputStream(1024)); - if (phead->getData()) { - result->write(phead->getData(), phead->getSize()); - } - if (pbody->getData()) { - result->write(pbody->getData(), pbody->getSize()); - } - const char* pData = static_cast(result->getData()); - int len = result->getDataSize(); - return pTts->sendMessage(pData, len); +bool TcpRemotingClient::SendCommand(boost::shared_ptr pTts, RemotingCommand& msg) { + const MemoryBlock* phead = msg.GetHead(); + const MemoryBlock* pbody = msg.GetBody(); + + unique_ptr result(new MemoryOutputStream(1024)); + if (phead->getData()) { + result->write(phead->getData(), phead->getSize()); + } + if (pbody->getData()) { + result->write(pbody->getData(), pbody->getSize()); + } + const char* pData = static_cast(result->getData()); + int len = result->getDataSize(); + return pTts->sendMessage(pData, len); } -void TcpRemotingClient::static_messageReceived(void* context, - const MemoryBlock& mem, - const string& addr) { - TcpRemotingClient* pTcpRemotingClient = (TcpRemotingClient*)context; - if (pTcpRemotingClient) pTcpRemotingClient->messageReceived(mem, addr); +void TcpRemotingClient::static_messageReceived(void* context, const MemoryBlock& mem, const string& addr) { + TcpRemotingClient* pTcpRemotingClient = (TcpRemotingClient*)context; + if (pTcpRemotingClient) pTcpRemotingClient->messageReceived(mem, addr); } -void TcpRemotingClient::messageReceived(const MemoryBlock& mem, - const string& addr) { - m_ioService.post( - boost::bind(&TcpRemotingClient::ProcessData, this, mem, addr)); +void TcpRemotingClient::messageReceived(const MemoryBlock& mem, const string& addr) { + m_ioService.post(boost::bind(&TcpRemotingClient::ProcessData, this, mem, addr)); } -void TcpRemotingClient::ProcessData(const MemoryBlock& mem, - const string& addr) { - RemotingCommand* pRespondCmd = NULL; - try { - pRespondCmd = RemotingCommand::Decode(mem); - } catch (...) { - LOG_ERROR("processData_error"); - return; - } - - int opaque = pRespondCmd->getOpaque(); - - //isResponseType()) { - boost::shared_ptr pFuture( - findAndDeleteAsyncResponseFuture(opaque)); - if (!pFuture) { - pFuture = findAndDeleteResponseFuture(opaque); - if (pFuture) { - if (pFuture->getSyncResponseFlag()) { - LOG_WARN("waitResponse already timeout of opaque:%d", opaque); - deleteAndZero(pRespondCmd); - return; - } - LOG_DEBUG("find_response opaque:%d", opaque); - } else { - LOG_DEBUG("responseFuture was deleted by timeout of opaque:%d", opaque); - deleteAndZero(pRespondCmd); +void TcpRemotingClient::ProcessData(const MemoryBlock& mem, const string& addr) { + RemotingCommand* pRespondCmd = NULL; + try { + pRespondCmd = RemotingCommand::Decode(mem); + } + catch (...) { + LOG_ERROR("processData_error"); return; - } } - processResponseCommand(pRespondCmd, pFuture); - } else { - processRequestCommand(pRespondCmd, addr); - } -} -void TcpRemotingClient::processResponseCommand( - RemotingCommand* pCmd, boost::shared_ptr pfuture) { - int code = pfuture->getRequestCode(); - int opaque = pCmd->getOpaque(); - LOG_DEBUG("processResponseCommand, code:%d,opaque:%d, maxRetryTimes:%d, retrySendTimes:%d", code, opaque, pfuture->getMaxRetrySendTimes(), pfuture->getRetrySendTimes()); - pCmd->SetExtHeader(code); // set head , for response use - - pfuture->setResponse(pCmd); + int opaque = pRespondCmd->getOpaque(); + + //isResponseType()) { + boost::shared_ptr pFuture(findAndDeleteAsyncResponseFuture(opaque)); + if (!pFuture) { + pFuture = findAndDeleteResponseFuture(opaque); + if (pFuture) { + if (pFuture->getSyncResponseFlag()) { + LOG_WARN("waitResponse already timeout of opaque:%d", opaque); + deleteAndZero(pRespondCmd); + return; + } + LOG_DEBUG("find_response opaque:%d", opaque); + } else { + LOG_DEBUG("responseFuture was deleted by timeout of opaque:%d", opaque); + deleteAndZero(pRespondCmd); + return; + } + } + processResponseCommand(pRespondCmd, pFuture); + } else { + processRequestCommand(pRespondCmd, addr); + } +} - if (pfuture->getASyncFlag()) { - if (!pfuture->getAsyncResponseFlag()) { - pfuture->setAsyncResponseFlag(); - pfuture->setAsyncCallBackStatus(asyncCallBackStatus_response); - cancelTimerCallback(opaque); - pfuture->executeInvokeCallback(); +void TcpRemotingClient::processResponseCommand(RemotingCommand* pCmd, boost::shared_ptr pfuture) { + int code = pfuture->getRequestCode(); + int opaque = pCmd->getOpaque(); + LOG_DEBUG("processResponseCommand, code:%d,opaque:%d, maxRetryTimes:%d, retrySendTimes:%d", code, opaque, + pfuture->getMaxRetrySendTimes(), pfuture->getRetrySendTimes()); + pCmd->SetExtHeader(code); // set head , for response use + + pfuture->setResponse(pCmd); + + if (pfuture->getASyncFlag()) { + if (!pfuture->getAsyncResponseFlag()) { + pfuture->setAsyncResponseFlag(); + pfuture->setAsyncCallBackStatus(asyncCallBackStatus_response); + cancelTimerCallback(opaque); + pfuture->executeInvokeCallback(); + } } - } } -void TcpRemotingClient::processRequestCommand(RemotingCommand* pCmd, - const string& addr) { - unique_ptr pRequestCommand(pCmd); - int requestCode = pRequestCommand->getCode(); - if (m_requestTable.find(requestCode) == m_requestTable.end()) { - LOG_ERROR("can_not_find request:%d processor", requestCode); - } else { - unique_ptr pResponse( - m_requestTable[requestCode]->processRequest(addr, - pRequestCommand.get())); - if (!pRequestCommand->isOnewayRPC()) { - if (pResponse) { - pResponse->setOpaque(pRequestCommand->getOpaque()); - pResponse->markResponseType(); - pResponse->Encode(); - - invokeOneway(addr, *pResponse); - } - } - } +void TcpRemotingClient::processRequestCommand(RemotingCommand* pCmd, const string& addr) { + unique_ptr pRequestCommand(pCmd); + int requestCode = pRequestCommand->getCode(); + if (m_requestTable.find(requestCode) == m_requestTable.end()) { + LOG_ERROR("can_not_find request:%d processor", requestCode); + } else { + unique_ptr pResponse(m_requestTable[requestCode]->processRequest(addr, pRequestCommand.get())); + if (!pRequestCommand->isOnewayRPC()) { + if (pResponse) { + pResponse->setOpaque(pRequestCommand->getOpaque()); + pResponse->markResponseType(); + pResponse->Encode(); + + invokeOneway(addr, *pResponse); + } + } + } } -void TcpRemotingClient::addResponseFuture( - int opaque, boost::shared_ptr pfuture) { - boost::lock_guard lock(m_futureTableMutex); - m_futureTable[opaque] = pfuture; +void TcpRemotingClient::addResponseFuture(int opaque, boost::shared_ptr pfuture) { + boost::lock_guard lock(m_futureTableMutex); + m_futureTable[opaque] = pfuture; } // Note: after call this function, shared_ptr of m_syncFutureTable[opaque] will // be erased, so caller must ensure the life cycle of returned shared_ptr; -boost::shared_ptr -TcpRemotingClient::findAndDeleteResponseFuture(int opaque) { - boost::lock_guard lock(m_futureTableMutex); - boost::shared_ptr pResponseFuture; - if (m_futureTable.find(opaque) != m_futureTable.end()) { - pResponseFuture = m_futureTable[opaque]; - m_futureTable.erase(opaque); - } - return pResponseFuture; +boost::shared_ptr TcpRemotingClient::findAndDeleteResponseFuture(int opaque) { + boost::lock_guard lock(m_futureTableMutex); + boost::shared_ptr pResponseFuture; + if (m_futureTable.find(opaque) != m_futureTable.end()) { + pResponseFuture = m_futureTable[opaque]; + m_futureTable.erase(opaque); + } + return pResponseFuture; } -void TcpRemotingClient::handleAsyncPullForResponseTimeout( - const boost::system::error_code& e, int opaque) { - if (e == boost::asio::error::operation_aborted) { - LOG_INFO("handleAsyncPullForResponseTimeout aborted opaque:%d, e_code:%d, msg:%s", opaque, e.value(), e.message().data()); - return; - } - - LOG_DEBUG("handleAsyncPullForResponseTimeout opaque:%d, e_code:%d, msg:%s", opaque, e.value(), e.message().data()); - boost::shared_ptr pFuture( - findAndDeleteAsyncResponseFuture(opaque)); - if (pFuture && pFuture->getASyncFlag() && (pFuture->getAsyncCallbackWrap())) { - if ((pFuture->getAsyncResponseFlag() != - true)) // if no response received, then check timeout or not - { - LOG_ERROR("no response got for opaque:%d", opaque); - pFuture->setAsyncCallBackStatus(asyncCallBackStatus_timeout); - pFuture->executeInvokeCallbackException(); +void TcpRemotingClient::handleAsyncPullForResponseTimeout(const boost::system::error_code& e, int opaque) { + if (e == boost::asio::error::operation_aborted) { + LOG_INFO("handleAsyncPullForResponseTimeout aborted opaque:%d, e_code:%d, msg:%s", opaque, e.value(), + e.message().data()); + return; + } + + LOG_DEBUG("handleAsyncPullForResponseTimeout opaque:%d, e_code:%d, msg:%s", opaque, e.value(), e.message().data()); + boost::shared_ptr pFuture(findAndDeleteAsyncResponseFuture(opaque)); + if (pFuture && pFuture->getASyncFlag() && (pFuture->getAsyncCallbackWrap())) { + if ((pFuture->getAsyncResponseFlag() != true)) // if no response received, then check timeout or not + { + LOG_ERROR("no response got for opaque:%d", opaque); + pFuture->setAsyncCallBackStatus(asyncCallBackStatus_timeout); + pFuture->executeInvokeCallbackException(); + } } - } - eraseTimerCallback(opaque); + eraseTimerCallback(opaque); } -void TcpRemotingClient::addAsyncResponseFuture( - int opaque, boost::shared_ptr pfuture) { - boost::lock_guard lock(m_asyncFutureLock); - m_asyncFutureTable[opaque] = pfuture; +void TcpRemotingClient::addAsyncResponseFuture(int opaque, boost::shared_ptr pfuture) { + boost::lock_guard lock(m_asyncFutureLock); + m_asyncFutureTable[opaque] = pfuture; } // Note: after call this function, shared_ptr of m_asyncFutureTable[opaque] will // be erased, so caller must ensure the life cycle of returned shared_ptr; -boost::shared_ptr -TcpRemotingClient::findAndDeleteAsyncResponseFuture(int opaque) { - boost::lock_guard lock(m_asyncFutureLock); - boost::shared_ptr pResponseFuture; - if (m_asyncFutureTable.find(opaque) != m_asyncFutureTable.end()) { - pResponseFuture = m_asyncFutureTable[opaque]; - m_asyncFutureTable.erase(opaque); - } - - return pResponseFuture; +boost::shared_ptr TcpRemotingClient::findAndDeleteAsyncResponseFuture(int opaque) { + boost::lock_guard lock(m_asyncFutureLock); + boost::shared_ptr pResponseFuture; + if (m_asyncFutureTable.find(opaque) != m_asyncFutureTable.end()) { + pResponseFuture = m_asyncFutureTable[opaque]; + m_asyncFutureTable.erase(opaque); + } + + return pResponseFuture; } -void TcpRemotingClient::registerProcessor( - MQRequestCode requestCode, - ClientRemotingProcessor* clientRemotingProcessor) { - if (m_requestTable.find(requestCode) != m_requestTable.end()) - m_requestTable.erase(requestCode); - m_requestTable[requestCode] = clientRemotingProcessor; +void TcpRemotingClient::registerProcessor(MQRequestCode requestCode, ClientRemotingProcessor* clientRemotingProcessor) { + if (m_requestTable.find(requestCode) != m_requestTable.end()) m_requestTable.erase(requestCode); + m_requestTable[requestCode] = clientRemotingProcessor; } -void TcpRemotingClient::addTimerCallback(boost::asio::deadline_timer* t, - int opaque) { - boost::lock_guard lock(m_timerMapMutex); - if (m_async_timer_map.find(opaque) != m_async_timer_map.end()) { - LOG_DEBUG("addTimerCallback:erase timerCallback opaque:%lld", opaque); - boost::asio::deadline_timer* old_t = m_async_timer_map[opaque]; - old_t->cancel(); - delete old_t; - old_t = NULL; - m_async_timer_map.erase(opaque); - } - m_async_timer_map[opaque] = t; +void TcpRemotingClient::addTimerCallback(boost::asio::deadline_timer* t, int opaque) { + boost::lock_guard lock(m_timerMapMutex); + if (m_async_timer_map.find(opaque) != m_async_timer_map.end()) { + LOG_DEBUG("addTimerCallback:erase timerCallback opaque:%lld", opaque); + boost::asio::deadline_timer* old_t = m_async_timer_map[opaque]; + old_t->cancel(); + delete old_t; + old_t = NULL; + m_async_timer_map.erase(opaque); + } + m_async_timer_map[opaque] = t; } void TcpRemotingClient::eraseTimerCallback(int opaque) { - boost::lock_guard lock(m_timerMapMutex); - if (m_async_timer_map.find(opaque) != m_async_timer_map.end()) { - LOG_DEBUG("eraseTimerCallback: opaque:%lld", opaque); - boost::asio::deadline_timer* t = m_async_timer_map[opaque]; - delete t; - t = NULL; - m_async_timer_map.erase(opaque); - } + boost::lock_guard lock(m_timerMapMutex); + if (m_async_timer_map.find(opaque) != m_async_timer_map.end()) { + LOG_DEBUG("eraseTimerCallback: opaque:%lld", opaque); + boost::asio::deadline_timer* t = m_async_timer_map[opaque]; + delete t; + t = NULL; + m_async_timer_map.erase(opaque); + } } void TcpRemotingClient::cancelTimerCallback(int opaque) { - boost::lock_guard lock(m_timerMapMutex); - if (m_async_timer_map.find(opaque) != m_async_timer_map.end()) { - LOG_DEBUG("cancelTimerCallback: opaque:%lld", opaque); - boost::asio::deadline_timer* t = m_async_timer_map[opaque]; - t->cancel(); - delete t; - t = NULL; - m_async_timer_map.erase(opaque); - } + boost::lock_guard lock(m_timerMapMutex); + if (m_async_timer_map.find(opaque) != m_async_timer_map.end()) { + LOG_DEBUG("cancelTimerCallback: opaque:%lld", opaque); + boost::asio::deadline_timer* t = m_async_timer_map[opaque]; + t->cancel(); + delete t; + t = NULL; + m_async_timer_map.erase(opaque); + } } void TcpRemotingClient::removeAllTimerCallback() { - boost::lock_guard lock(m_timerMapMutex); - for (asyncTimerMap::iterator it = m_async_timer_map.begin(); - it != m_async_timer_map.end(); ++it) { - boost::asio::deadline_timer* t = it->second; - t->cancel(); - delete t; - t = NULL; - } - m_async_timer_map.clear(); + boost::lock_guard lock(m_timerMapMutex); + for (asyncTimerMap::iterator it = m_async_timer_map.begin(); it != m_async_timer_map.end(); ++it) { + boost::asio::deadline_timer* t = it->second; + t->cancel(); + delete t; + t = NULL; + } + m_async_timer_map.clear(); } void TcpRemotingClient::deleteOpaqueForDropPullRequest(const MQMessageQueue& mq, int opaque) { - //delete the map record of opaque<->ResponseFuture, so the answer for the pull request will discard when receive it later - boost::shared_ptr pFuture(findAndDeleteAsyncResponseFuture(opaque)); - if (!pFuture) { - pFuture = findAndDeleteResponseFuture(opaque); - if (pFuture) { - LOG_DEBUG("succ deleted the sync pullrequest for opaque:%d, mq:%s", opaque, mq.toString().data()); - } - } else { - LOG_DEBUG("succ deleted the async pullrequest for opaque:%d, mq:%s", opaque, mq.toString().data()); - } - //delete the timeout timer for opaque for pullrequest - cancelTimerCallback(opaque); + // delete the map record of opaque<->ResponseFuture, so the answer for the pull request will discard when receive it + // later + boost::shared_ptr pFuture(findAndDeleteAsyncResponseFuture(opaque)); + if (!pFuture) { + pFuture = findAndDeleteResponseFuture(opaque); + if (pFuture) { + LOG_DEBUG("succ deleted the sync pullrequest for opaque:%d, mq:%s", opaque, mq.toString().data()); + } + } else { + LOG_DEBUG("succ deleted the async pullrequest for opaque:%d, mq:%s", opaque, mq.toString().data()); + } + // delete the timeout timer for opaque for pullrequest + cancelTimerCallback(opaque); } // GetTransport(const string& addr, - bool needRespons); - boost::shared_ptr CreateTransport(const string& addr, - bool needRespons); - boost::shared_ptr CreateNameserverTransport(bool needRespons); - void CloseTransport(const string& addr, boost::shared_ptr pTcp); - void CloseNameServerTransport(boost::shared_ptr pTcp); - bool SendCommand(boost::shared_ptr pTts, RemotingCommand& msg); - void processRequestCommand(RemotingCommand* pCmd, const string& addr); - void processResponseCommand(RemotingCommand* pCmd, - boost::shared_ptr pfuture); - - void addResponseFuture(int opaque, boost::shared_ptr pfuture); - boost::shared_ptr findAndDeleteResponseFuture(int opaque); - - void addAsyncResponseFuture(int opaque, - boost::shared_ptr pfuture); - boost::shared_ptr findAndDeleteAsyncResponseFuture( - int opaque); - - void addTimerCallback(boost::asio::deadline_timer* t, int opaque); - void eraseTimerCallback(int opaque); - void cancelTimerCallback(int opaque); - void removeAllTimerCallback(); - - private: - typedef map> TcpMap; - typedef map> ResMap; - - typedef map RequestMap; - RequestMap m_requestTable; - - boost::mutex m_futureTableMutex; - ResMap m_futureTable; //future; - - ResMap m_asyncFutureTable; - boost::mutex m_asyncFutureLock; - - TcpMap m_tcpTable; //tcp; - boost::timed_mutex m_tcpLock; - - // ThreadPool m_threadpool; - int m_pullThreadNum; - uint64_t m_tcpConnectTimeout; // ms - uint64_t m_tcpTransportTryLockTimeout; // s - - // m_namesrvAddrList; - string m_namesrvAddrChoosed; - unsigned int m_namesrvIndex; - boost::asio::io_service m_ioService; - boost::thread_group m_threadpool; - boost::asio::io_service::work m_ioServiceWork; - - boost::asio::io_service m_async_ioService; - unique_ptr m_async_service_thread; - - typedef map asyncTimerMap; - boost::mutex m_timerMapMutex; - asyncTimerMap m_async_timer_map; +public: + TcpRemotingClient(int pullThreadNum, uint64_t tcpConnectTimeout, uint64_t tcpTransportTryLockTimeout); + virtual ~TcpRemotingClient(); + void stopAllTcpTransportThread(); + void updateNameServerAddressList(const string& addrs); + + // GetTransport(const string& addr, bool needRespons); + boost::shared_ptr CreateTransport(const string& addr, bool needRespons); + boost::shared_ptr CreateNameserverTransport(bool needRespons); + void CloseTransport(const string& addr, boost::shared_ptr pTcp); + void CloseNameServerTransport(boost::shared_ptr pTcp); + bool SendCommand(boost::shared_ptr pTts, RemotingCommand& msg); + void processRequestCommand(RemotingCommand* pCmd, const string& addr); + void processResponseCommand(RemotingCommand* pCmd, boost::shared_ptr pfuture); + + void addResponseFuture(int opaque, boost::shared_ptr pfuture); + boost::shared_ptr findAndDeleteResponseFuture(int opaque); + + void addAsyncResponseFuture(int opaque, boost::shared_ptr pfuture); + boost::shared_ptr findAndDeleteAsyncResponseFuture(int opaque); + + void addTimerCallback(boost::asio::deadline_timer* t, int opaque); + void eraseTimerCallback(int opaque); + void cancelTimerCallback(int opaque); + void removeAllTimerCallback(); + +private: + typedef map> TcpMap; + typedef map> ResMap; + + typedef map RequestMap; + RequestMap m_requestTable; + + boost::mutex m_futureTableMutex; + ResMap m_futureTable; //future; + + ResMap m_asyncFutureTable; + boost::mutex m_asyncFutureLock; + + TcpMap m_tcpTable; //tcp; + boost::timed_mutex m_tcpLock; + + // ThreadPool m_threadpool; + int m_pullThreadNum; + uint64_t m_tcpConnectTimeout; // ms + uint64_t m_tcpTransportTryLockTimeout; // s + + // m_namesrvAddrList; + string m_namesrvAddrChoosed; + unsigned int m_namesrvIndex; + boost::asio::io_service m_ioService; + boost::thread_group m_threadpool; + boost::asio::io_service::work m_ioServiceWork; + + boost::asio::io_service m_async_ioService; + unique_ptr m_async_service_thread; + + typedef map asyncTimerMap; + boost::mutex m_timerMapMutex; + asyncTimerMap m_async_timer_map; }; // lock(m_socketLock); - - struct sockaddr_in sin; - memset(&sin, 0, sizeof(sin)); - sin.sin_family = AF_INET; - sin.sin_addr.s_addr = getInetAddr(hostName); - - sin.sin_port = htons(portNumber); - - m_eventBase = event_base_new(); - m_bufferEvent = bufferevent_socket_new( - m_eventBase, -1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE); - bufferevent_setcb(m_bufferEvent, readNextMessageIntCallback, NULL, eventcb, - this); - bufferevent_enable(m_bufferEvent, EV_READ | EV_WRITE); - bufferevent_setwatermark(m_bufferEvent, EV_READ, 4, 0); - - setTcpConnectStatus(e_connectWaitResponse); - if (bufferevent_socket_connect(m_bufferEvent, (struct sockaddr *)&sin, - sizeof(sin)) < 0) { - LOG_INFO("connect to fd:%d failed", bufferevent_getfd(m_bufferEvent)); - setTcpConnectStatus(e_connectFail); - freeBufferEvent(); - return e_connectFail; - } else { - int fd = bufferevent_getfd(m_bufferEvent); - LOG_INFO("try to connect to fd:%d, addr:%s", fd, (hostName.c_str())); - - evthread_make_base_notifiable(m_eventBase); - - m_ReadDatathread = - new boost::thread(boost::bind(&TcpTransport::runThread, this)); - - while (!m_event_base_status) { - LOG_INFO("Wait till event base is looping"); - boost::system_time const timeout = - boost::get_system_time() + boost::posix_time::milliseconds(1000); - boost::unique_lock lock(m_event_base_mtx); - m_event_base_cv.timed_wait(lock, timeout); +tcpConnectStatus TcpTransport::connect(const string &strServerURL, int timeOutMillisecs /* = 3000 */) { + string hostName; + short portNumber; + if (!UtilAll::SplitURL(strServerURL, hostName, portNumber)) { + return e_connectFail; } - return e_connectWaitResponse; - } -} + boost::lock_guard lock(m_socketLock); -void TcpTransport::setTcpConnectStatus(tcpConnectStatus connectStatus) { - m_tcpConnectStatus = connectStatus; -} + struct sockaddr_in sin; + memset(&sin, 0, sizeof(sin)); + sin.sin_family = AF_INET; + sin.sin_addr.s_addr = getInetAddr(hostName); + + sin.sin_port = htons(portNumber); + + m_eventBase = event_base_new(); + m_bufferEvent = bufferevent_socket_new(m_eventBase, -1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE); + bufferevent_setcb(m_bufferEvent, readNextMessageIntCallback, NULL, eventcb, this); + bufferevent_enable(m_bufferEvent, EV_READ | EV_WRITE); + bufferevent_setwatermark(m_bufferEvent, EV_READ, 4, 0); + + setTcpConnectStatus(e_connectWaitResponse); + if (bufferevent_socket_connect(m_bufferEvent, (struct sockaddr *)&sin, sizeof(sin)) < 0) { + LOG_INFO("connect to fd:%d failed", bufferevent_getfd(m_bufferEvent)); + setTcpConnectStatus(e_connectFail); + freeBufferEvent(); + return e_connectFail; + } else { + int fd = bufferevent_getfd(m_bufferEvent); + LOG_INFO("try to connect to fd:%d, addr:%s", fd, (hostName.c_str())); -tcpConnectStatus TcpTransport::getTcpConnectStatus() { - return m_tcpConnectStatus; + evthread_make_base_notifiable(m_eventBase); + + m_ReadDatathread = new boost::thread(boost::bind(&TcpTransport::runThread, this)); + + while (!m_event_base_status) { + LOG_INFO("Wait till event base is looping"); + boost::system_time const timeout = boost::get_system_time() + boost::posix_time::milliseconds(1000); + boost::unique_lock lock(m_event_base_mtx); + m_event_base_cv.timed_wait(lock, timeout); + } + + return e_connectWaitResponse; + } } +void TcpTransport::setTcpConnectStatus(tcpConnectStatus connectStatus) { m_tcpConnectStatus = connectStatus; } + +tcpConnectStatus TcpTransport::getTcpConnectStatus() { return m_tcpConnectStatus; } + tcpConnectStatus TcpTransport::waitTcpConnectEvent(int timeoutMillisecs) { - boost::unique_lock lk(m_connectEventLock); - if (!m_connectEvent.timed_wait( - lk, boost::posix_time::milliseconds(timeoutMillisecs))) { - LOG_INFO("connect timeout"); - } - return getTcpConnectStatus(); + boost::unique_lock lk(m_connectEventLock); + if (!m_connectEvent.timed_wait(lk, boost::posix_time::milliseconds(timeoutMillisecs))) { + LOG_INFO("connect timeout"); + } + return getTcpConnectStatus(); } void TcpTransport::setTcpConnectEvent(tcpConnectStatus connectStatus) { - tcpConnectStatus baseStatus(getTcpConnectStatus()); - setTcpConnectStatus(connectStatus); - if (baseStatus == e_connectWaitResponse) { - LOG_INFO("received libevent callback event"); - m_connectEvent.notify_all(); - } + tcpConnectStatus baseStatus(getTcpConnectStatus()); + setTcpConnectStatus(connectStatus); + if (baseStatus == e_connectWaitResponse) { + LOG_INFO("received libevent callback event"); + m_connectEvent.notify_all(); + } } -u_long TcpTransport::getInetAddr(string &hostname) -{ - u_long addr = inet_addr(hostname.c_str()); - - if (INADDR_NONE == addr) { - constexpr size_t length = 128; - struct evutil_addrinfo hints; - struct evutil_addrinfo *answer = NULL; - /* Build the hints to tell getaddrinfo how to act. */ - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; /* v4 or v6 is fine. */ - //Look up the hostname. - int err = evutil_getaddrinfo(hostname.c_str(), NULL, &hints, &answer); - if (err != 0) { - string info = "Failed to resolve host name(" + hostname + "): " + evutil_gai_strerror(err); - THROW_MQEXCEPTION(MQClientException, info, -1); - } - - struct evutil_addrinfo *addressInfo; - for (addressInfo = answer; addressInfo; addressInfo = addressInfo->ai_next) { - char buf[length]; - const char *address = NULL; - if (addressInfo->ai_family == AF_INET) { - struct sockaddr_in *sin = (struct sockaddr_in*)addressInfo->ai_addr; - address = evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, length); - } - else if (addressInfo->ai_family == AF_INET6) { - struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)addressInfo->ai_addr; - address = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, length); - } - if (address) { - addr = inet_addr(address); - if (addr != INADDR_NONE) { - break; - } - } - } - } - - return addr; +u_long TcpTransport::getInetAddr(string &hostname) { + u_long addr = inet_addr(hostname.c_str()); + + if (INADDR_NONE == addr) { + constexpr size_t length = 128; + struct evutil_addrinfo hints; + struct evutil_addrinfo *answer = NULL; + /* Build the hints to tell getaddrinfo how to act. */ + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; /* v4 or v6 is fine. */ + // Look up the hostname. + int err = evutil_getaddrinfo(hostname.c_str(), NULL, &hints, &answer); + if (err != 0) { + string info = "Failed to resolve host name(" + hostname + "): " + evutil_gai_strerror(err); + THROW_MQEXCEPTION(MQClientException, info, -1); + } + + struct evutil_addrinfo *addressInfo; + for (addressInfo = answer; addressInfo; addressInfo = addressInfo->ai_next) { + char buf[length]; + const char *address = NULL; + if (addressInfo->ai_family == AF_INET) { + struct sockaddr_in *sin = (struct sockaddr_in *)addressInfo->ai_addr; + address = evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, length); + } else if (addressInfo->ai_family == AF_INET6) { + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addressInfo->ai_addr; + address = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, length); + } + if (address) { + addr = inet_addr(address); + if (addr != INADDR_NONE) { + break; + } + } + } + } + + return addr; } void TcpTransport::disconnect(const string &addr) { - boost::lock_guard lock(m_socketLock); - if (getTcpConnectStatus() != e_connectInit) { - clearBufferEventCallback(); - LOG_INFO("disconnect:%s start", addr.c_str()); - m_connectEvent.notify_all(); - setTcpConnectStatus(e_connectInit); - if (m_ReadDatathread) { - m_ReadDatathread->interrupt(); - exitBaseDispatch(); - while (m_ReadDatathread->timed_join(boost::posix_time::seconds(1)) == - false) { - LOG_WARN("join readDataThread fail, retry"); - m_ReadDatathread->interrupt(); - exitBaseDispatch(); - } - delete m_ReadDatathread; - m_ReadDatathread = NULL; + boost::lock_guard lock(m_socketLock); + if (getTcpConnectStatus() != e_connectInit) { + clearBufferEventCallback(); + LOG_INFO("disconnect:%s start", addr.c_str()); + m_connectEvent.notify_all(); + setTcpConnectStatus(e_connectInit); + if (m_ReadDatathread) { + m_ReadDatathread->interrupt(); + exitBaseDispatch(); + while (m_ReadDatathread->timed_join(boost::posix_time::seconds(1)) == false) { + LOG_WARN("join readDataThread fail, retry"); + m_ReadDatathread->interrupt(); + exitBaseDispatch(); + } + delete m_ReadDatathread; + m_ReadDatathread = NULL; + } + freeBufferEvent(); + LOG_INFO("disconnect:%s completely", addr.c_str()); } - freeBufferEvent(); - LOG_INFO("disconnect:%s completely", addr.c_str()); - } } void TcpTransport::clearBufferEventCallback() { - if (m_bufferEvent) { - // Bufferevents are internally reference-counted, so if the bufferevent has - // pending deferred callbacks when you free it, it won't be deleted until - // the callbacks are done. - // so just empty callback to avoid future callback by libevent - bufferevent_setcb(m_bufferEvent, NULL, NULL, NULL, NULL); - } + if (m_bufferEvent) { + // Bufferevents are internally reference-counted, so if the bufferevent has + // pending deferred callbacks when you free it, it won't be deleted until + // the callbacks are done. + // so just empty callback to avoid future callback by libevent + bufferevent_setcb(m_bufferEvent, NULL, NULL, NULL, NULL); + } } void TcpTransport::freeBufferEvent() { - if (m_bufferEvent) { - bufferevent_free(m_bufferEvent); - m_bufferEvent = NULL; - } - if (m_eventBase) { - event_base_free(m_eventBase); - m_eventBase = NULL; - } + if (m_bufferEvent) { + bufferevent_free(m_bufferEvent); + m_bufferEvent = NULL; + } + if (m_eventBase) { + event_base_free(m_eventBase); + m_eventBase = NULL; + } } void TcpTransport::exitBaseDispatch() { - if (m_eventBase) { - event_base_loopbreak(m_eventBase); - // event_base_loopexit(m_eventBase, NULL); //Note: memory leak will be - // occured when timer callback was not done; - } + if (m_eventBase) { + event_base_loopbreak(m_eventBase); + // event_base_loopexit(m_eventBase, NULL); //Note: memory leak will be + // occured when timer callback was not done; + } } void TcpTransport::runThread() { - if (m_eventBase != NULL) { - if (!m_event_base_status) { - boost::mutex::scoped_lock lock(m_event_base_mtx); - m_event_base_status.store(true); - m_event_base_cv.notify_all(); - LOG_INFO("Notify on event_base_dispatch"); + if (m_eventBase != NULL) { + if (!m_event_base_status) { + boost::mutex::scoped_lock lock(m_event_base_mtx); + m_event_base_status.store(true); + m_event_base_cv.notify_all(); + LOG_INFO("Notify on event_base_dispatch"); + } + event_base_dispatch(m_eventBase); + // event_base_loop(m_eventBase, EVLOOP_ONCE);//EVLOOP_NONBLOCK should not + // be used, as could not callback event immediatly } - event_base_dispatch(m_eventBase); - // event_base_loop(m_eventBase, EVLOOP_ONCE);//EVLOOP_NONBLOCK should not - // be used, as could not callback event immediatly - } - LOG_INFO("event_base_dispatch exit once"); - boost::this_thread::sleep(boost::posix_time::milliseconds(1)); - if (getTcpConnectStatus() != e_connectSuccess) return; + LOG_INFO("event_base_dispatch exit once"); + boost::this_thread::sleep(boost::posix_time::milliseconds(1)); + if (getTcpConnectStatus() != e_connectSuccess) return; } void TcpTransport::timeoutcb(evutil_socket_t fd, short what, void *arg) { - LOG_INFO("timeoutcb: received event:%d on fd:%d", what, fd); - TcpTransport *tcpTrans = (TcpTransport *)arg; - if (tcpTrans->getTcpConnectStatus() != e_connectSuccess) { - LOG_INFO("timeoutcb: after connect time, tcp was not established on fd:%d", - fd); - tcpTrans->setTcpConnectStatus(e_connectFail); - } else { - LOG_INFO("timeoutcb: after connect time, tcp was established on fd:%d", fd); - } + LOG_INFO("timeoutcb: received event:%d on fd:%d", what, fd); + TcpTransport *tcpTrans = (TcpTransport *)arg; + if (tcpTrans->getTcpConnectStatus() != e_connectSuccess) { + LOG_INFO("timeoutcb: after connect time, tcp was not established on fd:%d", fd); + tcpTrans->setTcpConnectStatus(e_connectFail); + } else { + LOG_INFO("timeoutcb: after connect time, tcp was established on fd:%d", fd); + } } void TcpTransport::eventcb(struct bufferevent *bev, short what, void *ctx) { - evutil_socket_t fd = bufferevent_getfd(bev); - TcpTransport *tcpTrans = (TcpTransport *)ctx; - LOG_INFO("eventcb: received event:%x on fd:%d", what, fd); - if (what & BEV_EVENT_CONNECTED) { - int val = 1; - setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); - LOG_INFO("eventcb:connect to fd:%d successfully", fd); - tcpTrans->setTcpConnectEvent(e_connectSuccess); - } else if (what & (BEV_EVENT_ERROR | BEV_EVENT_EOF | BEV_EVENT_READING | - BEV_EVENT_WRITING)) { - LOG_INFO("eventcb:rcv error event cb:%x on fd:%d", what, fd); - tcpTrans->setTcpConnectEvent(e_connectFail); - bufferevent_setcb(bev, NULL, NULL, NULL, NULL); - // bufferevent_disable(bev, EV_READ|EV_WRITE); - // bufferevent_free(bev); - } else { - LOG_ERROR("eventcb: received error event:%d on fd:%d", what, fd); - } -} - -void TcpTransport::readNextMessageIntCallback(struct bufferevent *bev, - void *ctx) { - /* This callback is invoked when there is data to read on bev. */ - - // protocol:
- // 1 2 3 4 - // rocketmq protocol contains 4 parts as following: - // 1, big endian 4 bytes int, its length is sum of 2,3 and 4 - // 2, big endian 4 bytes int, its length is 3 - // 3, use json to serialization data - // 4, application could self-defination binary data - - struct evbuffer *input = bufferevent_get_input(bev); - while (1) { - struct evbuffer_iovec v[4]; - int n = evbuffer_peek(input, 4, NULL, v, sizeof(v) / sizeof(v[0])); - - int idx = 0; - char hdr[4]; - char *p = hdr; - unsigned int needed = 4; - - for (idx = 0; idx < n; idx++) { - if (needed) { - unsigned int tmp = needed < v[idx].iov_len ? needed : v[idx].iov_len; - memcpy(p, v[idx].iov_base, tmp); - p += tmp; - needed -= tmp; - } else { - break; - } - } - - if (needed) { - LOG_DEBUG(" too little data received with sum = %d ", 4 - needed); - return; - } - uint32 totalLenOfOneMsg = - *(uint32 *)hdr; // first 4 bytes, which indicates 1st part of protocol - uint32 bytesInMessage = ntohl(totalLenOfOneMsg); - LOG_DEBUG("fd:%d, totalLen:" SIZET_FMT ", bytesInMessage:%d", - bufferevent_getfd(bev), v[0].iov_len, bytesInMessage); - - uint32 len = evbuffer_get_length(input); - if (len >= bytesInMessage + 4) { - LOG_DEBUG("had received all data with len:%d from fd:%d", len, - bufferevent_getfd(bev)); + evutil_socket_t fd = bufferevent_getfd(bev); + TcpTransport *tcpTrans = (TcpTransport *)ctx; + LOG_INFO("eventcb: received event:%x on fd:%d", what, fd); + if (what & BEV_EVENT_CONNECTED) { + int val = 1; + setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); + LOG_INFO("eventcb:connect to fd:%d successfully", fd); + tcpTrans->setTcpConnectEvent(e_connectSuccess); + } else if (what & (BEV_EVENT_ERROR | BEV_EVENT_EOF | BEV_EVENT_READING | BEV_EVENT_WRITING)) { + LOG_INFO("eventcb:rcv error event cb:%x on fd:%d", what, fd); + tcpTrans->setTcpConnectEvent(e_connectFail); + bufferevent_setcb(bev, NULL, NULL, NULL, NULL); + // bufferevent_disable(bev, EV_READ|EV_WRITE); + // bufferevent_free(bev); } else { - LOG_DEBUG( - "didn't received whole bytesInMessage:%d, from fd:%d, totalLen:%d", - bytesInMessage, bufferevent_getfd(bev), len); - return; // consider large data which was not received completely by now + LOG_ERROR("eventcb: received error event:%d on fd:%d", what, fd); } +} - if (bytesInMessage > 0) { - MemoryBlock messageData(bytesInMessage, true); - uint32 bytesRead = 0; - char *data = messageData.getData() + bytesRead; - bufferevent_read(bev, data, 4); - bytesRead = bufferevent_read(bev, data, bytesInMessage); - - TcpTransport *tcpTrans = (TcpTransport *)ctx; - tcpTrans->messageReceived(messageData); +void TcpTransport::readNextMessageIntCallback(struct bufferevent *bev, void *ctx) { + /* This callback is invoked when there is data to read on bev. */ + + // protocol:
+ // 1 2 3 4 + // rocketmq protocol contains 4 parts as following: + // 1, big endian 4 bytes int, its length is sum of 2,3 and 4 + // 2, big endian 4 bytes int, its length is 3 + // 3, use json to serialization data + // 4, application could self-defination binary data + + struct evbuffer *input = bufferevent_get_input(bev); + while (1) { + struct evbuffer_iovec v[4]; + int n = evbuffer_peek(input, 4, NULL, v, sizeof(v) / sizeof(v[0])); + + int idx = 0; + char hdr[4]; + char *p = hdr; + unsigned int needed = 4; + + for (idx = 0; idx < n; idx++) { + if (needed) { + unsigned int tmp = needed < v[idx].iov_len ? needed : v[idx].iov_len; + memcpy(p, v[idx].iov_base, tmp); + p += tmp; + needed -= tmp; + } else { + break; + } + } + + if (needed) { + LOG_DEBUG(" too little data received with sum = %d ", 4 - needed); + return; + } + uint32 totalLenOfOneMsg = *(uint32 *)hdr; // first 4 bytes, which indicates 1st part of protocol + uint32 bytesInMessage = ntohl(totalLenOfOneMsg); + LOG_DEBUG("fd:%d, totalLen:" SIZET_FMT ", bytesInMessage:%d", bufferevent_getfd(bev), v[0].iov_len, + bytesInMessage); + + uint32 len = evbuffer_get_length(input); + if (len >= bytesInMessage + 4) { + LOG_DEBUG("had received all data with len:%d from fd:%d", len, bufferevent_getfd(bev)); + } else { + LOG_DEBUG("didn't received whole bytesInMessage:%d, from fd:%d, totalLen:%d", bytesInMessage, + bufferevent_getfd(bev), len); + return; // consider large data which was not received completely by now + } + + if (bytesInMessage > 0) { + MemoryBlock messageData(bytesInMessage, true); + uint32 bytesRead = 0; + char *data = messageData.getData() + bytesRead; + bufferevent_read(bev, data, 4); + bytesRead = bufferevent_read(bev, data, bytesInMessage); + + TcpTransport *tcpTrans = (TcpTransport *)ctx; + tcpTrans->messageReceived(messageData); + } } - } } bool TcpTransport::sendMessage(const char *pData, int len) { - boost::lock_guard lock(m_socketLock); - if (getTcpConnectStatus() != e_connectSuccess) { + boost::lock_guard lock(m_socketLock); + if (getTcpConnectStatus() != e_connectSuccess) { + return false; + } + + int bytes_left = len; + int bytes_written = 0; + const char *ptr = pData; + + /*NOTE: + 1. do not need to consider large data which could not send by once, as + bufferevent could handle this case; + */ + if (m_bufferEvent) { + bytes_written = bufferevent_write(m_bufferEvent, ptr, bytes_left); + if (bytes_written == 0) + return true; + else + return false; + } return false; - } - - int bytes_left = len; - int bytes_written = 0; - const char *ptr = pData; - - /*NOTE: - 1. do not need to consider large data which could not send by once, as - bufferevent could handle this case; - */ - if (m_bufferEvent) { - bytes_written = bufferevent_write(m_bufferEvent, ptr, bytes_left); - if (bytes_written == 0) - return true; - else - return false; - } - return false; } void TcpTransport::messageReceived(const MemoryBlock &mem) { - if (m_readcallback) { - m_readcallback(m_tcpRemotingClient, mem, getPeerAddrAndPort()); - } + if (m_readcallback) { + m_readcallback(m_tcpRemotingClient, mem, getPeerAddrAndPort()); + } } const string TcpTransport::getPeerAddrAndPort() { - struct sockaddr_in broker; - socklen_t cLen = sizeof(broker); - - // getsockname(m_socket->getRawSocketHandle(), (struct sockaddr*) &s, &sLen); - // // ! use connectSock here. - getpeername(bufferevent_getfd(m_bufferEvent), (struct sockaddr *)&broker, - &cLen); // ! use connectSock here. - LOG_DEBUG("broker addr: %s, broker port: %d", inet_ntoa(broker.sin_addr), - ntohs(broker.sin_port)); - string brokerAddr(inet_ntoa(broker.sin_addr)); - brokerAddr.append(":"); - string brokerPort(UtilAll::to_string(ntohs(broker.sin_port))); - brokerAddr.append(brokerPort); - LOG_DEBUG("brokerAddr:%s", brokerAddr.c_str()); - return brokerAddr; + struct sockaddr_in broker; + socklen_t cLen = sizeof(broker); + + // getsockname(m_socket->getRawSocketHandle(), (struct sockaddr*) &s, &sLen); + // // ! use connectSock here. + getpeername(bufferevent_getfd(m_bufferEvent), (struct sockaddr *)&broker, &cLen); // ! use connectSock here. + LOG_DEBUG("broker addr: %s, broker port: %d", inet_ntoa(broker.sin_addr), ntohs(broker.sin_port)); + string brokerAddr(inet_ntoa(broker.sin_addr)); + brokerAddr.append(":"); + string brokerPort(UtilAll::to_string(ntohs(broker.sin_port))); + brokerAddr.append(brokerPort); + LOG_DEBUG("brokerAddr:%s", brokerAddr.c_str()); + return brokerAddr; } const uint64_t TcpTransport::getStartTime() const { return m_startTime; } diff --git a/src/transport/TcpTransport.h b/src/transport/TcpTransport.h old mode 100755 new mode 100644 index fa4da85b4..b8fd7cc44 --- a/src/transport/TcpTransport.h +++ b/src/transport/TcpTransport.h @@ -33,62 +33,59 @@ extern "C" { namespace rocketmq { // m_tcpConnectStatus; - boost::mutex m_connectEventLock; - boost::condition_variable_any m_connectEvent; +private: + uint64_t m_startTime; + boost::mutex m_socketLock; + struct event_base *m_eventBase; + struct bufferevent *m_bufferEvent; + boost::atomic m_tcpConnectStatus; + boost::mutex m_connectEventLock; + boost::condition_variable_any m_connectEvent; - boost::atomic m_event_base_status; - boost::mutex m_event_base_mtx; - boost::condition_variable_any m_event_base_cv; + boost::atomic m_event_base_status; + boost::mutex m_event_base_mtx; + boost::condition_variable_any m_event_base_cv; - //& properties = msg1.getProperties(); - //for (auto& pair : properties) { + // const map& properties = msg1.getProperties(); + // for (auto& pair : properties) { // std::cout << pair.first << " : " << pair.second << std::endl; //} EXPECT_EQ(msg1.getProperties().size(), 2); EXPECT_EQ(msg1.getBody().size(), 4); - //20 + bodyLen + 2 + propertiesLength; + // 20 + bodyLen + 2 + propertiesLength; string encodeMessage = BatchMessage::encode(msg1); EXPECT_EQ(encodeMessage.size(), 43); @@ -50,12 +50,12 @@ TEST(BatchMessageEncodeTest, encodeMQMessage) { TEST(BatchMessageEncodeTest, encodeMQMessages) { std::vector msgs; MQMessage msg1("topic", "*", "test1"); - //const map& properties = msg1.getProperties(); - //for (auto& pair : properties) { + // const map& properties = msg1.getProperties(); + // for (auto& pair : properties) { // std::cout << pair.first << " : " << pair.second << std::endl; //} msgs.push_back(msg1); - //20 + bodyLen + 2 + propertiesLength; + // 20 + bodyLen + 2 + propertiesLength; string encodeMessage = BatchMessage::encode(msgs); EXPECT_EQ(encodeMessage.size(), 86); MQMessage msg2("topic", "*", "test2"); @@ -63,7 +63,7 @@ TEST(BatchMessageEncodeTest, encodeMQMessages) { msgs.push_back(msg2); msgs.push_back(msg3); encodeMessage = BatchMessage::encode(msgs); - EXPECT_EQ(encodeMessage.size(), 258);//86*3 + EXPECT_EQ(encodeMessage.size(), 258); // 86*3 } int main(int argc, char* argv[]) { diff --git a/test/src/UrlTest.cpp b/test/src/UrlTest.cpp index b347e221f..eb057d639 100644 --- a/test/src/UrlTest.cpp +++ b/test/src/UrlTest.cpp @@ -44,7 +44,7 @@ using ::testing::InitGoogleTest; using testing::Return; class MockTopicConfig : public TopicConfig { - public: +public: MOCK_METHOD0(getReadQueueNums, int()); }; diff --git a/test/src/common/MemoryBlockTest.cpp b/test/src/common/MemoryBlockTest.cpp index a81dc8a6a..b18377fcb 100644 --- a/test/src/common/MemoryBlockTest.cpp +++ b/test/src/common/MemoryBlockTest.cpp @@ -46,7 +46,7 @@ TEST(memoryBlock, init) { EXPECT_EQ(frouMemoryBlock.getSize(), 12); EXPECT_TRUE(frouMemoryBlock.getData() != nullptr); - char *buf = (char *) malloc(sizeof(char) * 9); + char *buf = (char *)malloc(sizeof(char) * 9); strcpy(buf, "RocketMQ"); MemoryBlock fiveMemoryBlock(buf, -1); EXPECT_EQ(fiveMemoryBlock.getSize(), -1); @@ -80,7 +80,7 @@ TEST(memoryBlock, operators) { EXPECT_TRUE(operaterMemoryBlock == memoryBlock); - char *buf = (char *) malloc(sizeof(char) * 16); + char *buf = (char *)malloc(sizeof(char) * 16); memset(buf, 0, 16); strcpy(buf, "RocketMQ"); MemoryBlock twoMemoryBlock(buf, 12); @@ -119,7 +119,7 @@ TEST(memoryBlock, operators) { MemoryBlock replaceWithMemoryBlock; replaceWithMemoryBlock.append(buf, 8); - char *aliyunBuf = (char *) malloc(sizeof(char) * 8); + char *aliyunBuf = (char *)malloc(sizeof(char) * 8); memset(aliyunBuf, 0, 8); strcpy(aliyunBuf, "aliyun"); replaceWithMemoryBlock.replaceWith(aliyunBuf, 0); diff --git a/test/src/common/MemoryOutputStreamTest.cpp b/test/src/common/MemoryOutputStreamTest.cpp index 6cf10153f..61ef29100 100644 --- a/test/src/common/MemoryOutputStreamTest.cpp +++ b/test/src/common/MemoryOutputStreamTest.cpp @@ -53,7 +53,7 @@ TEST(memoryOutputStream, init) { EXPECT_EQ(frouMemoryOutput.getPosition(), memoryBlock.getSize()); EXPECT_EQ(frouMemoryOutput.getDataSize(), memoryBlock.getSize()); - char *buf = (char *) malloc(sizeof(char) * 9); + char *buf = (char *)malloc(sizeof(char) * 9); strcpy(buf, "RocketMQ"); MemoryOutputStream fiveMemoryOutputStream(buf, 8); EXPECT_EQ(fiveMemoryOutputStream.getData(), buf); @@ -65,7 +65,7 @@ TEST(memoryOutputStream, init) { } TEST(memoryOutputStream, flush) { - char *buf = (char *) malloc(sizeof(char) * 9); + char *buf = (char *)malloc(sizeof(char) * 9); strcpy(buf, "RocketMQ"); MemoryOutputStream memoryOutput; memoryOutput.write(buf, 9); @@ -82,7 +82,7 @@ TEST(memoryOutputStream, preallocate) { } TEST(memoryOutputStream, getMemoryBlock) { - char *buf = (char *) malloc(sizeof(char) * 9); + char *buf = (char *)malloc(sizeof(char) * 9); strcpy(buf, "RocketMQ"); MemoryOutputStream memoryOutput; memoryOutput.write(buf, 9); @@ -92,7 +92,7 @@ TEST(memoryOutputStream, getMemoryBlock) { } TEST(memoryOutputStream, prepareToWriteAndGetData) { - char *buf = (char *) malloc(sizeof(char) * 9); + char *buf = (char *)malloc(sizeof(char) * 9); strcpy(buf, "RocketMQ"); MemoryOutputStream memoryOutput(buf, 9); EXPECT_EQ(memoryOutput.getData(), buf); @@ -104,7 +104,7 @@ TEST(memoryOutputStream, prepareToWriteAndGetData) { EXPECT_EQ(string(data), "rocketMQ"); MemoryOutputStream blockMmoryOutput(8); - char *memoryData = (char *) blockMmoryOutput.getData(); + char *memoryData = (char *)blockMmoryOutput.getData(); EXPECT_EQ(memoryData[blockMmoryOutput.getDataSize()], 0); @@ -116,7 +116,7 @@ TEST(memoryOutputStream, prepareToWriteAndGetData) { } TEST(memoryOutputStream, position) { - char *buf = (char *) malloc(sizeof(char) * 9); + char *buf = (char *)malloc(sizeof(char) * 9); strcpy(buf, "RocketMQ"); MemoryOutputStream memoryOutput; @@ -171,12 +171,12 @@ TEST(memoryOutputStream, write) { } TEST(memoryInputStream, info) { - char *buf = (char *) malloc(sizeof(char) * 9); + char *buf = (char *)malloc(sizeof(char) * 9); strcpy(buf, "RocketMQ"); MemoryInputStream memoryInput(buf, 8, false); - char *memoryData = (char *) memoryInput.getData(); + char *memoryData = (char *)memoryInput.getData(); EXPECT_EQ(memoryData, buf); EXPECT_EQ(memoryInput.getTotalLength(), 8); @@ -184,12 +184,12 @@ TEST(memoryInputStream, info) { MemoryInputStream twoMemoryInput(buf, 8, true); EXPECT_NE(twoMemoryInput.getData(), buf); - memoryData = (char *) twoMemoryInput.getData(); + memoryData = (char *)twoMemoryInput.getData(); EXPECT_NE(&memoryData, &buf); MemoryBlock memoryBlock(buf, 8); MemoryInputStream threeMemoryInput(memoryBlock, false); - memoryData = (char *) threeMemoryInput.getData(); + memoryData = (char *)threeMemoryInput.getData(); EXPECT_EQ(memoryData, threeMemoryInput.getData()); EXPECT_EQ(threeMemoryInput.getTotalLength(), 8); @@ -199,7 +199,7 @@ TEST(memoryInputStream, info) { } TEST(memoryInputStream, position) { - char *buf = (char *) malloc(sizeof(char) * 9); + char *buf = (char *)malloc(sizeof(char) * 9); strcpy(buf, "RocketMQ"); MemoryInputStream memoryInput(buf, 8, false); diff --git a/test/src/common/big_endianTest.cpp b/test/src/common/big_endianTest.cpp index d4c6fe3b5..997451da4 100644 --- a/test/src/common/big_endianTest.cpp +++ b/test/src/common/big_endianTest.cpp @@ -29,42 +29,42 @@ using rocketmq::BigEndianReader; using rocketmq::BigEndianWriter; TEST(big_endian, bigEndianObject) { - char *buf = (char *) malloc(sizeof(char) * 32); + char *buf = (char *)malloc(sizeof(char) * 32); BigEndianWriter writer(buf, 32); BigEndianReader reader(buf, 32); - uint8_t *unit8 = (uint8_t *) malloc(sizeof(uint8_t)); - EXPECT_TRUE(writer.WriteU8((uint8_t) 12)); + uint8_t *unit8 = (uint8_t *)malloc(sizeof(uint8_t)); + EXPECT_TRUE(writer.WriteU8((uint8_t)12)); EXPECT_TRUE(reader.ReadU8(unit8)); EXPECT_EQ(*unit8, 12); free(unit8); - uint16_t *unit16 = (uint16_t *) malloc(sizeof(uint16_t)); - EXPECT_TRUE(writer.WriteU16((uint16_t) 1200)); + uint16_t *unit16 = (uint16_t *)malloc(sizeof(uint16_t)); + EXPECT_TRUE(writer.WriteU16((uint16_t)1200)); EXPECT_TRUE(reader.ReadU16(unit16)); EXPECT_EQ(*unit16, 1200); free(unit16); - uint32_t *unit32 = (uint32_t *) malloc(sizeof(uint32_t)); + uint32_t *unit32 = (uint32_t *)malloc(sizeof(uint32_t)); - EXPECT_TRUE(writer.WriteU32((uint32_t) 120000)); + EXPECT_TRUE(writer.WriteU32((uint32_t)120000)); EXPECT_TRUE(reader.ReadU32(unit32)); EXPECT_EQ(*unit32, 120000); free(unit32); - uint64_t *unit64 = (uint64_t *) malloc(sizeof(uint64_t)); + uint64_t *unit64 = (uint64_t *)malloc(sizeof(uint64_t)); - EXPECT_TRUE(writer.WriteU64((uint64_t) 120000)); + EXPECT_TRUE(writer.WriteU64((uint64_t)120000)); EXPECT_TRUE(reader.ReadU64(unit64)); EXPECT_EQ(*unit64, 120000); free(unit64); - char *newBuf = (char *) malloc(sizeof(char) * 8); - char *writeBuf = (char *) malloc(sizeof(char) * 8); + char *newBuf = (char *)malloc(sizeof(char) * 8); + char *writeBuf = (char *)malloc(sizeof(char) * 8); strcpy(writeBuf, "RocketMQ"); - EXPECT_TRUE(writer.WriteBytes(writeBuf, (size_t) 8)); - EXPECT_TRUE(reader.ReadBytes(newBuf, (size_t) 8)); + EXPECT_TRUE(writer.WriteBytes(writeBuf, (size_t)8)); + EXPECT_TRUE(reader.ReadBytes(newBuf, (size_t)8)); EXPECT_EQ(*writeBuf, *newBuf); free(newBuf); @@ -84,8 +84,8 @@ TEST(big_endian, bigEndian) { EXPECT_EQ(writeBuf, readBuf); */ - rocketmq::WriteBigEndian(writeBuf, (uint8_t) 12); - uint8_t *out = (uint8_t *) malloc(sizeof(uint8_t)); + rocketmq::WriteBigEndian(writeBuf, (uint8_t)12); + uint8_t *out = (uint8_t *)malloc(sizeof(uint8_t)); rocketmq::ReadBigEndian(writeBuf, out); EXPECT_EQ(*out, 12); free(out); diff --git a/test/src/extern/CMessageExtTest.cpp b/test/src/extern/CMessageExtTest.cpp index 3471c9e32..346260dab 100644 --- a/test/src/extern/CMessageExtTest.cpp +++ b/test/src/extern/CMessageExtTest.cpp @@ -30,7 +30,7 @@ using rocketmq::MQMessageExt; TEST(cmessageExt, info) { MQMessageExt *mqMessageExt = new MQMessageExt(); - CMessageExt *messageExt = (CMessageExt *) mqMessageExt; + CMessageExt *messageExt = (CMessageExt *)mqMessageExt; mqMessageExt->setTopic("testTopic"); EXPECT_EQ(GetMessageTopic(messageExt), mqMessageExt->getTopic()); diff --git a/test/src/extern/CMessageTest.cpp b/test/src/extern/CMessageTest.cpp index 22500ebaa..8d5f5d5da 100644 --- a/test/src/extern/CMessageTest.cpp +++ b/test/src/extern/CMessageTest.cpp @@ -29,7 +29,7 @@ using rocketmq::MQMessage; TEST(cmessages, info) { CMessage *message = CreateMessage(NULL); - MQMessage *mqMessage = (MQMessage *) message; + MQMessage *mqMessage = (MQMessage *)message; EXPECT_EQ(mqMessage->getTopic(), ""); SetMessageTopic(message, "testTopic"); @@ -56,7 +56,7 @@ TEST(cmessages, info) { EXPECT_EQ(DestroyMessage(message), OK); CMessage *twomessage = CreateMessage("testTwoTopic"); - MQMessage *twoMqMessage = (MQMessage *) twomessage; + MQMessage *twoMqMessage = (MQMessage *)twomessage; EXPECT_EQ(twoMqMessage->getTopic(), "testTwoTopic"); EXPECT_EQ(DestroyMessage(twomessage), OK); diff --git a/test/src/extern/CProducerTest.cpp b/test/src/extern/CProducerTest.cpp index 2d5c5bec7..2ab7f44ac 100644 --- a/test/src/extern/CProducerTest.cpp +++ b/test/src/extern/CProducerTest.cpp @@ -50,7 +50,7 @@ using rocketmq::SendStatus; using rocketmq::SessionCredentials; class MockDefaultMQProducer : public DefaultMQProducer { - public: +public: MockDefaultMQProducer(const string &groupname) : DefaultMQProducer(groupname) {} MOCK_METHOD0(start, void()); MOCK_METHOD0(shutdown, void()); @@ -62,15 +62,13 @@ class MockDefaultMQProducer : public DefaultMQProducer { MOCK_METHOD5(send, SendResult(MQMessage &, MessageQueueSelector *, void *, int, bool)); }; -void CSendSuccessCallbackFunc(CSendResult result) { -} -void cSendExceptionCallbackFunc(CMQException e) { -} +void CSendSuccessCallbackFunc(CSendResult result) {} +void cSendExceptionCallbackFunc(CMQException e) {} TEST(cProducer, SendMessageAsync) { MockDefaultMQProducer *mockProducer = new MockDefaultMQProducer("testGroup"); - CProducer *cProducer = (CProducer *) mockProducer; - CMessage *msg = (CMessage *) new MQMessage(); + CProducer *cProducer = (CProducer *)mockProducer; + CMessage *msg = (CMessage *)new MQMessage(); EXPECT_EQ(SendMessageAsync(NULL, NULL, NULL, NULL), NULL_POINTER); EXPECT_EQ(SendMessageAsync(cProducer, NULL, NULL, NULL), NULL_POINTER); @@ -82,14 +80,12 @@ TEST(cProducer, SendMessageAsync) { DestroyMessage(msg); } -int QueueSelectorCallbackFunc(int size, CMessage *msg, void *arg) { - return 0; -} +int QueueSelectorCallbackFunc(int size, CMessage *msg, void *arg) { return 0; } TEST(cProducer, sendMessageOrderly) { MockDefaultMQProducer *mockProducer = new MockDefaultMQProducer("testGroup"); - CProducer *cProducer = (CProducer *) mockProducer; - CMessage *msg = (CMessage *) new MQMessage(); + CProducer *cProducer = (CProducer *)mockProducer; + CMessage *msg = (CMessage *)new MQMessage(); MQMessageQueue messageQueue; EXPECT_EQ(SendMessageOrderly(NULL, NULL, NULL, msg, 1, NULL), NULL_POINTER); @@ -108,8 +104,8 @@ TEST(cProducer, sendMessageOrderly) { TEST(cProducer, sendOneway) { MockDefaultMQProducer *mockProducer = new MockDefaultMQProducer("testGroup"); - CProducer *cProducer = (CProducer *) mockProducer; - CMessage *msg = (CMessage *) new MQMessage(); + CProducer *cProducer = (CProducer *)mockProducer; + CMessage *msg = (CMessage *)new MQMessage(); EXPECT_EQ(SendMessageOneway(NULL, NULL), NULL_POINTER); EXPECT_EQ(SendMessageOneway(cProducer, NULL), NULL_POINTER); @@ -122,10 +118,10 @@ TEST(cProducer, sendOneway) { TEST(cProducer, sendMessageSync) { MockDefaultMQProducer *mockProducer = new MockDefaultMQProducer("testGroup"); - CProducer *cProducer = (CProducer *) mockProducer; + CProducer *cProducer = (CProducer *)mockProducer; MQMessage *mqMessage = new MQMessage(); - CMessage *msg = (CMessage *) mqMessage; + CMessage *msg = (CMessage *)mqMessage; CSendResult *result; MQMessageQueue messageQueue; EXPECT_EQ(SendMessageSync(NULL, NULL, NULL), NULL_POINTER); @@ -133,7 +129,7 @@ TEST(cProducer, sendMessageSync) { EXPECT_EQ(SendMessageSync(cProducer, msg, NULL), NULL_POINTER); - result = (CSendResult *) malloc(sizeof(CSendResult)); + result = (CSendResult *)malloc(sizeof(CSendResult)); EXPECT_CALL(*mockProducer, send(_, _)) .Times(5) @@ -141,7 +137,7 @@ TEST(cProducer, sendMessageSync) { .WillOnce(Return(SendResult(SendStatus::SEND_FLUSH_SLAVE_TIMEOUT, "2", "offset1", messageQueue, 14))) .WillOnce(Return(SendResult(SendStatus::SEND_SLAVE_NOT_AVAILABLE, "3", "offset1", messageQueue, 14))) .WillOnce(Return(SendResult(SendStatus::SEND_OK, "3", "offset1", messageQueue, 14))) - .WillOnce(Return(SendResult((SendStatus) -1, "4", "offset1", messageQueue, 14))); + .WillOnce(Return(SendResult((SendStatus) - 1, "4", "offset1", messageQueue, 14))); EXPECT_EQ(SendMessageSync(cProducer, msg, result), OK); EXPECT_EQ(result->sendStatus, E_SEND_FLUSH_DISK_TIMEOUT); @@ -164,7 +160,7 @@ TEST(cProducer, sendMessageSync) { TEST(cProducer, infoMock) { MockDefaultMQProducer *mockProducer = new MockDefaultMQProducer("testGroup"); - CProducer *cProducer = (CProducer *) mockProducer; + CProducer *cProducer = (CProducer *)mockProducer; EXPECT_CALL(*mockProducer, start()).Times(1); EXPECT_EQ(StartProducer(cProducer), OK); @@ -182,7 +178,7 @@ TEST(cProducer, infoMock) { TEST(cProducer, info) { CProducer *cProducer = CreateProducer("groupTest"); - DefaultMQProducer *defaultMQProducer = (DefaultMQProducer *) cProducer; + DefaultMQProducer *defaultMQProducer = (DefaultMQProducer *)cProducer; EXPECT_TRUE(cProducer != NULL); EXPECT_EQ(defaultMQProducer->getGroupName(), "groupTest"); diff --git a/test/src/extern/CPullConsumerTest.cpp b/test/src/extern/CPullConsumerTest.cpp index 6a1118d1b..9c9065305 100644 --- a/test/src/extern/CPullConsumerTest.cpp +++ b/test/src/extern/CPullConsumerTest.cpp @@ -51,7 +51,7 @@ using rocketmq::PullStatus; using rocketmq::SessionCredentials; class MockDefaultMQPullConsumer : public DefaultMQPullConsumer { - public: +public: MockDefaultMQPullConsumer(const string &groupname) : DefaultMQPullConsumer(groupname) {} MOCK_METHOD0(start, void()); MOCK_METHOD0(shutdown, void()); @@ -63,10 +63,10 @@ class MockDefaultMQPullConsumer : public DefaultMQPullConsumer { TEST(cpullConsumer, pull) { MockDefaultMQPullConsumer *mqPullConsumer = new MockDefaultMQPullConsumer("groudId"); - CPullConsumer *pullConsumer = (CPullConsumer *) mqPullConsumer; + CPullConsumer *pullConsumer = (CPullConsumer *)mqPullConsumer; CMessageQueue *cMessageQueue; - cMessageQueue = (CMessageQueue *) malloc(sizeof(CMessageQueue)); + cMessageQueue = (CMessageQueue *)malloc(sizeof(CMessageQueue)); strncpy(cMessageQueue->topic, "testTopic", 8); strncpy(cMessageQueue->brokerName, "testBroker", 9); cMessageQueue->queueId = 1; @@ -79,7 +79,7 @@ TEST(cpullConsumer, pull) { PullResult offsetIllegalPullResult(PullStatus::OFFSET_ILLEGAL, 1, 2, 3); - PullResult defaultPullResult((PullStatus) -1, 1, 2, 3); + PullResult defaultPullResult((PullStatus) - 1, 1, 2, 3); vector src; for (int i = 0; i < 5; i++) { @@ -123,7 +123,7 @@ TEST(cpullConsumer, pull) { TEST(cpullConsumer, infoMock) { MockDefaultMQPullConsumer *mqPullConsumer = new MockDefaultMQPullConsumer("groudId"); - CPullConsumer *pullConsumer = (CPullConsumer *) mqPullConsumer; + CPullConsumer *pullConsumer = (CPullConsumer *)mqPullConsumer; Expectation exp = EXPECT_CALL(*mqPullConsumer, start()).Times(1); EXPECT_EQ(StartPullConsumer(pullConsumer), OK); @@ -154,7 +154,7 @@ TEST(cpullConsumer, infoMock) { TEST(cpullConsumer, init) { CPullConsumer *pullConsumer = CreatePullConsumer("testGroupId"); - DefaultMQPullConsumer *defaultMQPullConsumer = (DefaultMQPullConsumer *) pullConsumer; + DefaultMQPullConsumer *defaultMQPullConsumer = (DefaultMQPullConsumer *)pullConsumer; EXPECT_FALSE(pullConsumer == NULL); EXPECT_EQ(SetPullConsumerGroupID(pullConsumer, "groupId"), OK); @@ -178,7 +178,7 @@ TEST(cpullConsumer, init) { TEST(cpullConsumer, null) { CPullConsumer *pullConsumer = CreatePullConsumer("testGroupId"); - DefaultMQPullConsumer *defaultMQPullConsumer = (DefaultMQPullConsumer *) pullConsumer; + DefaultMQPullConsumer *defaultMQPullConsumer = (DefaultMQPullConsumer *)pullConsumer; EXPECT_FALSE(pullConsumer == NULL); EXPECT_EQ(SetPullConsumerGroupID(pullConsumer, "groupId"), OK); diff --git a/test/src/extern/CPushConsumerTest.cpp b/test/src/extern/CPushConsumerTest.cpp index 30a645df8..8c445594e 100644 --- a/test/src/extern/CPushConsumerTest.cpp +++ b/test/src/extern/CPushConsumerTest.cpp @@ -42,7 +42,7 @@ using rocketmq::MessageModel; using rocketmq::SessionCredentials; class MockDefaultMQPushConsumer : public DefaultMQPushConsumer { - public: +public: MockDefaultMQPushConsumer(const string &groupname) : DefaultMQPushConsumer(groupname) {} MOCK_METHOD0(start, void()); @@ -53,7 +53,7 @@ class MockDefaultMQPushConsumer : public DefaultMQPushConsumer { TEST(cPushComsumer, infomock) { MockDefaultMQPushConsumer *pushComsumer = new MockDefaultMQPushConsumer("testGroup"); - CPushConsumer *consumer = (CPushConsumer *) pushComsumer; + CPushConsumer *consumer = (CPushConsumer *)pushComsumer; EXPECT_CALL(*pushComsumer, start()).Times(1); EXPECT_EQ(StartPushConsumer(consumer), OK); @@ -71,13 +71,11 @@ TEST(cPushComsumer, infomock) { Mock::AllowLeak(pushComsumer); } -int MessageCallBackFunc(CPushConsumer* consumer, CMessageExt* msg) { - return 0; -} +int MessageCallBackFunc(CPushConsumer *consumer, CMessageExt *msg) { return 0; } TEST(cPushComsumer, info) { CPushConsumer *cpushConsumer = CreatePushConsumer("testGroup"); - DefaultMQPushConsumer *mqPushConsumer = (DefaultMQPushConsumer *) cpushConsumer; + DefaultMQPushConsumer *mqPushConsumer = (DefaultMQPushConsumer *)cpushConsumer; EXPECT_TRUE(cpushConsumer != NULL); EXPECT_EQ(string(GetPushConsumerGroupID(cpushConsumer)), "testGroup"); diff --git a/test/src/message/MQDecoderTest.cpp b/test/src/message/MQDecoderTest.cpp index 4893200a4..09e7932d6 100644 --- a/test/src/message/MQDecoderTest.cpp +++ b/test/src/message/MQDecoderTest.cpp @@ -56,7 +56,7 @@ TEST(decoders, messageId) { int host; int port; string msgIdStr = - MQDecoder::createMessageId(rocketmq::IPPort2socketAddress(inet_addr("127.0.0.1"), 10091), (int64) 1024); + MQDecoder::createMessageId(rocketmq::IPPort2socketAddress(inet_addr("127.0.0.1"), 10091), (int64)1024); MQMessageId msgId = MQDecoder::decodeMessageId(msgIdStr); EXPECT_EQ(msgId.getOffset(), 1024); @@ -87,23 +87,23 @@ TEST(decoder, decoder) { memoryOut->writeIntBigEndian(4); mext.setFlag(4); // 6 QUEUEOFFSET 28 = 20+8 - memoryOut->writeInt64BigEndian((int64) 1024); + memoryOut->writeInt64BigEndian((int64)1024); mext.setQueueOffset(1024); // 7 PHYSICALOFFSET 36=28+8 - memoryOut->writeInt64BigEndian((int64) 2048); + memoryOut->writeInt64BigEndian((int64)2048); mext.setCommitLogOffset(2048); // 8 SYSFLAG 40=36+4 memoryOut->writeIntBigEndian(0); mext.setSysFlag(0); // 9 BORNTIMESTAMP 48 = 40+8 - memoryOut->writeInt64BigEndian((int64) 4096); + memoryOut->writeInt64BigEndian((int64)4096); mext.setBornTimestamp(4096); // 10 BORNHOST 56= 48+8 memoryOut->writeIntBigEndian(inet_addr("127.0.0.1")); memoryOut->writeIntBigEndian(10091); mext.setBornHost(rocketmq::IPPort2socketAddress(inet_addr("127.0.0.1"), 10091)); // 11 STORETIMESTAMP 64 =56+8 - memoryOut->writeInt64BigEndian((int64) 4096); + memoryOut->writeInt64BigEndian((int64)4096); mext.setStoreTimestamp(4096); // 12 STOREHOST 72 = 64+8 memoryOut->writeIntBigEndian(inet_addr("127.0.0.2")); @@ -113,7 +113,7 @@ TEST(decoder, decoder) { mext.setReconsumeTimes(111111); memoryOut->writeIntBigEndian(mext.getReconsumeTimes()); // 14 Prepared Transaction Offset 84 = 76+8 - memoryOut->writeInt64BigEndian((int64) 12); + memoryOut->writeInt64BigEndian((int64)12); mext.setPreparedTransactionOffset(12); // 15 BODY 88 = 84+4 10 string *body = new string("1234567890"); @@ -129,7 +129,7 @@ TEST(decoder, decoder) { // 17 PROPERTIES memoryOut->writeShortBigEndian(0); - mext.setMsgId(MQDecoder::createMessageId(mext.getStoreHost(), (int64) mext.getCommitLogOffset())); + mext.setMsgId(MQDecoder::createMessageId(mext.getStoreHost(), (int64)mext.getCommitLogOffset())); vector mqvec; MemoryBlock block = memoryOut->getMemoryBlock(); diff --git a/test/src/protocol/RemotingCommandTest.cpp b/test/src/protocol/RemotingCommandTest.cpp index 36e8f1be5..e529fae79 100644 --- a/test/src/protocol/RemotingCommandTest.cpp +++ b/test/src/protocol/RemotingCommandTest.cpp @@ -56,7 +56,7 @@ using rocketmq::SendMessageResponseHeader; TEST(remotingCommand, init) { RemotingCommand remotingCommand; EXPECT_EQ(remotingCommand.getCode(), 0); - + RemotingCommand twoRemotingCommand(13); EXPECT_EQ(twoRemotingCommand.getCode(), 13); EXPECT_EQ(twoRemotingCommand.getOpaque(), 0); @@ -87,8 +87,8 @@ TEST(remotingCommand, init) { EXPECT_EQ(sixRemotingCommand.getFlag(), 3); EXPECT_EQ(sixRemotingCommand.getMsgBody(), ""); EXPECT_TRUE(sixRemotingCommand.getCommandHeader() == nullptr); - - RemotingCommand* sevenRemotingCommand = &sixRemotingCommand; + + RemotingCommand *sevenRemotingCommand = &sixRemotingCommand; EXPECT_EQ(sevenRemotingCommand->getCode(), 13); EXPECT_EQ(sevenRemotingCommand->getOpaque(), 12); EXPECT_EQ(sevenRemotingCommand->getRemark(), "remark"); diff --git a/test/src/transport/ClientRemotingProcessorTest.cpp b/test/src/transport/ClientRemotingProcessorTest.cpp index 64fb9445a..95e226cd0 100644 --- a/test/src/transport/ClientRemotingProcessorTest.cpp +++ b/test/src/transport/ClientRemotingProcessorTest.cpp @@ -66,7 +66,7 @@ using rocketmq::SessionCredentials; using rocketmq::UtilAll; class MockClientRemotingProcessor : public ClientRemotingProcessor { - public: +public: MockClientRemotingProcessor(MQClientFactory *factrory) : ClientRemotingProcessor(factrory) {} MOCK_METHOD1(resetOffset, RemotingCommand *(RemotingCommand *request)); MOCK_METHOD1(getConsumerRunningInfo, RemotingCommand *(RemotingCommand *request)); @@ -74,12 +74,9 @@ class MockClientRemotingProcessor : public ClientRemotingProcessor { }; class MockMQClientFactory : public MQClientFactory { - public: - MockMQClientFactory(const string &clientID, - int pullThreadNum, - uint64_t tcpConnectTimeout, - uint64_t tcpTransportTryLockTimeout, - string unitName) +public: + MockMQClientFactory(const string &clientID, int pullThreadNum, uint64_t tcpConnectTimeout, + uint64_t tcpTransportTryLockTimeout, string unitName) : MQClientFactory(clientID, pullThreadNum, tcpConnectTimeout, tcpTransportTryLockTimeout, unitName) {} MOCK_METHOD3(resetOffset, @@ -159,8 +156,8 @@ TEST(clientRemotingProcessor, resetOffset) { request->SetBody(strData.c_str(), strData.size()); clientRemotingProcessor.resetOffset(request); - //here header no need delete, it will managered by RemotingCommand - //delete header; + // here header no need delete, it will managered by RemotingCommand + // delete header; delete request; } diff --git a/test/src/transport/ResponseFutureTest.cpp b/test/src/transport/ResponseFutureTest.cpp index 483fcedde..1eab23a33 100644 --- a/test/src/transport/ResponseFutureTest.cpp +++ b/test/src/transport/ResponseFutureTest.cpp @@ -44,7 +44,7 @@ using rocketmq::TcpRemotingClient; using rocketmq::UtilAll; class MockAsyncCallbackWrap : public SendCallbackWrap { - public: +public: MockAsyncCallbackWrap(AsyncCallback *pAsyncCallback, MQClientAPIImpl *pclientAPI) : SendCallbackWrap("", MQMessage(), pAsyncCallback, pclientAPI) {}