From 5a1c69027a029f60a01c5a2c0df74e9b3fb6fcbe Mon Sep 17 00:00:00 2001 From: donggang123 Date: Tue, 9 Apr 2019 18:54:54 +0800 Subject: [PATCH 1/2] Format project with clang-format Chromium --- .clang-format | 6 +- example/AsyncProducer.cpp | 21 +- example/AsyncPushConsumer.cpp | 8 +- example/BatchProducer.cpp | 152 +- example/CAsyncProducer.c | 87 +- example/COrderlyAsyncProducer.c | 95 +- example/OrderProducer.cpp | 24 +- example/OrderlyPushConsumer.cpp | 11 +- example/Producer.c | 59 +- example/PullConsumeMessage.c | 109 +- example/PullConsumer.cpp | 18 +- example/PushConsumeMessage.c | 52 +- example/PushConsumer.cpp | 17 +- example/PushConsumerOrderly.c | 53 +- example/SendDelayMsg.cpp | 0 example/SyncProducer.cpp | 20 +- example/common.h | 71 +- format.sh | 4 +- include/Arg_helper.h | 0 include/AsyncCallback.h | 18 +- include/BatchMessage.h | 10 +- include/CCommon.h | 50 +- include/CMQException.h | 14 +- include/CMessage.h | 23 +- include/CMessageExt.h | 34 +- include/CMessageQueue.h | 8 +- include/CProducer.h | 73 +- include/CPullConsumer.h | 43 +- include/CPullResult.h | 26 +- include/CPushConsumer.h | 62 +- include/CSendResult.h | 18 +- include/ConsumeType.h | 14 +- include/DefaultMQProducer.h | 51 +- include/DefaultMQPullConsumer.h | 50 +- include/DefaultMQPushConsumer.h | 21 +- include/MQClient.h | 8 +- include/MQClientException.h | 78 +- include/MQConsumer.h | 22 +- include/MQMessage.h | 37 +- include/MQMessageExt.h | 22 +- include/MQMessageListener.h | 25 +- include/MQMessageQueue.h | 3 +- include/MQProducer.h | 21 +- include/MQSelector.h | 3 +- include/MQueueListener.h | 0 include/PullResult.h | 19 +- include/QueryResult.h | 3 +- include/RocketMQClient.h | 0 include/SendMessageHook.h | 0 include/SendResult.h | 14 +- include/SessionCredentials.h | 13 +- libs/signature/include/base64.h | 26 +- libs/signature/include/hmac.h | 15 +- libs/signature/include/param_list.h | 24 +- libs/signature/include/sha1.h | 29 +- libs/signature/include/sha256.h | 41 +- libs/signature/include/sha512.h | 39 +- libs/signature/include/spas_client.h | 53 +- libs/signature/include/u64.h | 104 +- src/MQClientAPIImpl.cpp | 478 ++--- src/MQClientAPIImpl.h | 149 +- src/MQClientFactory.cpp | 464 ++--- src/MQClientFactory.h | 106 +- src/MQClientManager.cpp | 15 +- src/MQClientManager.h | 3 +- src/common/Arg_helper.cpp | 0 src/common/AsyncArg.h | 0 src/common/AsyncCallbackWrap.cpp | 114 +- src/common/AsyncCallbackWrap.h | 24 +- src/common/ByteOrder.h | 71 +- src/common/ClientRPCHook.cpp | 31 +- src/common/ClientRPCHook.h | 15 +- src/common/CommunicationMode.h | 2 +- src/common/FilterAPI.h | 6 +- src/common/InputStream.cpp | 40 +- src/common/InputStream.h | 3 +- src/common/MQClient.cpp | 97 +- src/common/MQVersion.cpp | 0 src/common/MQVersion.h | 0 src/common/MemoryInputStream.cpp | 68 +- src/common/MemoryInputStream.h | 35 +- src/common/MemoryOutputStream.cpp | 88 +- src/common/MemoryOutputStream.h | 38 +- src/common/MessageSysFlag.cpp | 0 src/common/MessageSysFlag.h | 0 src/common/NamesrvConfig.h | 34 +- src/common/OutputStream.cpp | 49 +- src/common/OutputStream.h | 3 +- src/common/PermName.cpp | 4 +- src/common/PermName.h | 0 src/common/PullSysFlag.cpp | 3 +- src/common/PullSysFlag.h | 3 +- src/common/ServiceState.h | 0 src/common/SubscriptionGroupConfig.h | 0 src/common/TopAddressing.cpp | 17 +- src/common/TopAddressing.h | 0 src/common/TopicConfig.cpp | 30 +- src/common/TopicConfig.h | 3 +- src/common/TopicFilterType.h | 0 src/common/UtilAll.cpp | 112 +- src/common/UtilAll.h | 38 +- src/common/Validators.cpp | 27 +- src/common/Validators.h | 6 +- src/common/VirtualEnvUtil.cpp | 6 +- src/common/VirtualEnvUtil.h | 6 +- src/common/big_endian.cpp | 56 +- src/common/dataBlock.cpp | 86 +- src/common/dataBlock.h | 35 +- src/common/sync_http_client.cpp | 15 +- src/common/sync_http_client.h | 0 src/common/url.cpp | 16 +- src/common/url.h | 0 src/consumer/AllocateMQStrategy.h | 9 +- .../ConsumeMessageConcurrentlyService.cpp | 44 +- src/consumer/ConsumeMessageOrderlyService.cpp | 92 +- src/consumer/ConsumeMsgService.h | 24 +- src/consumer/DefaultMQPullConsumer.cpp | 150 +- src/consumer/DefaultMQPushConsumer.cpp | 1748 ++++++++--------- src/consumer/FindBrokerResult.h | 3 +- src/consumer/OffsetStore.cpp | 107 +- src/consumer/OffsetStore.h | 33 +- src/consumer/PullAPIWrapper.cpp | 60 +- src/consumer/PullAPIWrapper.h | 3 +- src/consumer/PullRequest.cpp | 68 +- src/consumer/PullRequest.h | 7 +- src/consumer/PullResult.cpp | 32 +- src/consumer/PullResultExt.h | 26 +- src/consumer/Rebalance.cpp | 209 +- src/consumer/Rebalance.h | 9 +- src/consumer/SubscriptionData.cpp | 20 +- src/consumer/SubscriptionData.h | 0 src/dllmain.cpp | 3 +- src/extern/CMessage.cpp | 109 +- src/extern/CMessageExt.cpp | 150 +- src/extern/CProducer.cpp | 538 ++--- src/extern/CPullConsumer.cpp | 387 ++-- src/extern/CPushConsumer.cpp | 390 ++-- src/extern/CSendResult.cpp | 1 - src/log/Logging.cpp | 56 +- src/log/Logging.h | 23 +- src/message/BatchMessage.cpp | 72 +- src/message/MQDecoder.cpp | 37 +- src/message/MQDecoder.h | 0 src/message/MQMessage.cpp | 72 +- src/message/MQMessageExt.cpp | 81 +- src/message/MQMessageId.h | 3 +- src/message/MQMessageQueue.cpp | 27 +- src/producer/DefaultMQProducer.cpp | 141 +- src/producer/SendResult.cpp | 27 +- src/producer/StringIdMaker.cpp | 29 +- src/producer/StringIdMaker.h | 2 +- src/producer/TopicPublishInfo.h | 67 +- src/protocol/CommandHeader.cpp | 253 +-- src/protocol/CommandHeader.h | 13 +- src/protocol/ConsumerRunningInfo.cpp | 28 +- src/protocol/ConsumerRunningInfo.h | 3 +- src/protocol/HeartbeatData.h | 8 +- src/protocol/KVTable.h | 14 +- src/protocol/LockBatchBody.cpp | 33 +- src/protocol/LockBatchBody.h | 3 +- src/protocol/MQProtos.h | 53 +- src/protocol/MessageQueue.cpp | 27 +- src/protocol/MessageQueue.h | 3 +- src/protocol/ProcessQueueInfo.h | 25 +- src/protocol/RemotingCommand.cpp | 111 +- src/protocol/RemotingCommand.h | 16 +- src/protocol/RemotingSerializable.h | 0 src/protocol/TopicList.h | 0 src/protocol/TopicRouteData.h | 211 +- src/thread/disruptorLFQ.h | 32 +- src/thread/task_queue.cpp | 12 +- src/thread/task_queue.h | 542 ++--- src/transport/ClientRemotingProcessor.cpp | 45 +- src/transport/ClientRemotingProcessor.h | 3 +- src/transport/ResponseFuture.cpp | 63 +- src/transport/ResponseFuture.h | 24 +- src/transport/SocketUtil.cpp | 13 +- src/transport/SocketUtil.h | 0 src/transport/TcpRemotingClient.cpp | 221 +-- src/transport/TcpRemotingClient.h | 40 +- src/transport/TcpTransport.cpp | 169 +- src/transport/TcpTransport.h | 40 +- test/src/BatchMessageTest.cpp | 62 +- test/src/StringIdMakerTest.cpp | 10 +- test/src/UrlTest.cpp | 44 +- test/src/common/ClientRPCHookTest.cpp | 36 +- test/src/common/MemoryBlockTest.cpp | 272 +-- test/src/common/MemoryOutputStreamTest.cpp | 270 +-- test/src/common/NamesrvConfigTest.cpp | 24 +- test/src/common/PermNametTest.cpp | 30 +- test/src/common/PullSysFlagTest.cpp | 126 +- test/src/common/TopicConfigTest.cpp | 84 +- test/src/common/ValidatorsTest.cpp | 54 +- test/src/common/VirtualEnvUtilTest.cpp | 20 +- test/src/common/big_endianTest.cpp | 126 +- test/src/extern/CMessageExtTest.cpp | 106 +- test/src/extern/CMessageTest.cpp | 68 +- test/src/extern/CProducerTest.cpp | 274 ++- test/src/extern/CPullConsumerTest.cpp | 216 +- test/src/extern/CPushConsumerTest.cpp | 154 +- test/src/message/MQDecoderTest.cpp | 272 +-- test/src/message/MQMessageExtTest.cpp | 154 +- test/src/message/MQMessageIdTest.cpp | 44 +- test/src/message/MQMessageQueueTest.cpp | 86 +- test/src/message/MQMessageTest.cpp | 216 +- test/src/protocol/CommandHeaderTest.cpp | 207 +- test/src/protocol/ConsumerRunningInfoTest.cpp | 148 +- test/src/protocol/HeartbeatDataTest.cpp | 100 +- test/src/protocol/KVTableTest.cpp | 24 +- test/src/protocol/LockBatchBodyTest.cpp | 86 +- test/src/protocol/MessageQueueTest.cpp | 90 +- test/src/protocol/ProcessQueueInfoTest.cpp | 80 +- test/src/protocol/RemotingCommandTest.cpp | 364 ++-- test/src/protocol/TopicRouteDataTest.cpp | 90 +- .../transport/ClientRemotingProcessorTest.cpp | 260 +-- test/src/transport/ResponseFutureTest.cpp | 194 +- test/src/transport/SocketUtilTest.cpp | 28 +- 217 files changed, 7579 insertions(+), 8021 deletions(-) mode change 100755 => 100644 example/AsyncProducer.cpp mode change 100755 => 100644 example/AsyncPushConsumer.cpp mode change 100755 => 100644 example/OrderProducer.cpp mode change 100755 => 100644 example/OrderlyPushConsumer.cpp mode change 100755 => 100644 example/PullConsumer.cpp mode change 100755 => 100644 example/PushConsumer.cpp mode change 100755 => 100644 example/SendDelayMsg.cpp mode change 100755 => 100644 example/SyncProducer.cpp mode change 100755 => 100644 example/common.h mode change 100755 => 100644 include/Arg_helper.h mode change 100755 => 100644 include/AsyncCallback.h mode change 100755 => 100644 include/ConsumeType.h mode change 100755 => 100644 include/DefaultMQProducer.h mode change 100755 => 100644 include/DefaultMQPullConsumer.h mode change 100755 => 100644 include/DefaultMQPushConsumer.h mode change 100755 => 100644 include/MQClient.h mode change 100755 => 100644 include/MQClientException.h mode change 100755 => 100644 include/MQConsumer.h mode change 100755 => 100644 include/MQMessage.h mode change 100755 => 100644 include/MQMessageExt.h mode change 100755 => 100644 include/MQMessageListener.h mode change 100755 => 100644 include/MQMessageQueue.h mode change 100755 => 100644 include/MQProducer.h mode change 100755 => 100644 include/MQSelector.h mode change 100755 => 100644 include/MQueueListener.h mode change 100755 => 100644 include/PullResult.h mode change 100755 => 100644 include/QueryResult.h mode change 100755 => 100644 include/RocketMQClient.h mode change 100755 => 100644 include/SendMessageHook.h mode change 100755 => 100644 include/SendResult.h mode change 100755 => 100644 include/SessionCredentials.h mode change 100755 => 100644 libs/signature/include/base64.h mode change 100755 => 100644 libs/signature/include/hmac.h mode change 100755 => 100644 libs/signature/include/param_list.h mode change 100755 => 100644 libs/signature/include/sha1.h mode change 100755 => 100644 libs/signature/include/sha256.h mode change 100755 => 100644 libs/signature/include/sha512.h mode change 100755 => 100644 libs/signature/include/spas_client.h mode change 100755 => 100644 src/MQClientManager.cpp mode change 100755 => 100644 src/MQClientManager.h mode change 100755 => 100644 src/common/Arg_helper.cpp mode change 100755 => 100644 src/common/AsyncArg.h mode change 100755 => 100644 src/common/AsyncCallbackWrap.cpp mode change 100755 => 100644 src/common/AsyncCallbackWrap.h mode change 100755 => 100644 src/common/ByteOrder.h mode change 100755 => 100644 src/common/ClientRPCHook.cpp mode change 100755 => 100644 src/common/ClientRPCHook.h mode change 100755 => 100644 src/common/CommunicationMode.h mode change 100755 => 100644 src/common/FilterAPI.h mode change 100755 => 100644 src/common/InputStream.cpp mode change 100755 => 100644 src/common/InputStream.h mode change 100755 => 100644 src/common/MQVersion.cpp mode change 100755 => 100644 src/common/MQVersion.h mode change 100755 => 100644 src/common/MemoryInputStream.cpp mode change 100755 => 100644 src/common/MemoryInputStream.h mode change 100755 => 100644 src/common/MemoryOutputStream.cpp mode change 100755 => 100644 src/common/MemoryOutputStream.h mode change 100755 => 100644 src/common/MessageSysFlag.cpp mode change 100755 => 100644 src/common/MessageSysFlag.h mode change 100755 => 100644 src/common/NamesrvConfig.h mode change 100755 => 100644 src/common/OutputStream.cpp mode change 100755 => 100644 src/common/OutputStream.h mode change 100755 => 100644 src/common/PermName.cpp mode change 100755 => 100644 src/common/PermName.h mode change 100755 => 100644 src/common/PullSysFlag.cpp mode change 100755 => 100644 src/common/PullSysFlag.h mode change 100755 => 100644 src/common/ServiceState.h mode change 100755 => 100644 src/common/SubscriptionGroupConfig.h mode change 100755 => 100644 src/common/TopAddressing.h mode change 100755 => 100644 src/common/TopicConfig.cpp mode change 100755 => 100644 src/common/TopicConfig.h mode change 100755 => 100644 src/common/TopicFilterType.h mode change 100755 => 100644 src/common/Validators.cpp mode change 100755 => 100644 src/common/Validators.h mode change 100755 => 100644 src/common/VirtualEnvUtil.cpp mode change 100755 => 100644 src/common/VirtualEnvUtil.h mode change 100755 => 100644 src/common/dataBlock.cpp mode change 100755 => 100644 src/common/dataBlock.h mode change 100755 => 100644 src/common/sync_http_client.cpp mode change 100755 => 100644 src/common/sync_http_client.h mode change 100755 => 100644 src/common/url.cpp mode change 100755 => 100644 src/common/url.h mode change 100755 => 100644 src/consumer/AllocateMQStrategy.h mode change 100755 => 100644 src/consumer/ConsumeMessageConcurrentlyService.cpp mode change 100755 => 100644 src/consumer/ConsumeMessageOrderlyService.cpp mode change 100755 => 100644 src/consumer/ConsumeMsgService.h mode change 100755 => 100644 src/consumer/DefaultMQPullConsumer.cpp mode change 100755 => 100644 src/consumer/FindBrokerResult.h mode change 100755 => 100644 src/consumer/OffsetStore.h mode change 100755 => 100644 src/consumer/PullAPIWrapper.cpp mode change 100755 => 100644 src/consumer/PullAPIWrapper.h mode change 100755 => 100644 src/consumer/PullResult.cpp mode change 100755 => 100644 src/consumer/PullResultExt.h mode change 100755 => 100644 src/consumer/Rebalance.h mode change 100755 => 100644 src/consumer/SubscriptionData.cpp mode change 100755 => 100644 src/consumer/SubscriptionData.h mode change 100755 => 100644 src/dllmain.cpp mode change 100755 => 100644 src/log/Logging.cpp mode change 100755 => 100644 src/log/Logging.h mode change 100755 => 100644 src/message/MQDecoder.cpp mode change 100755 => 100644 src/message/MQDecoder.h mode change 100755 => 100644 src/message/MQMessage.cpp mode change 100755 => 100644 src/message/MQMessageExt.cpp mode change 100755 => 100644 src/message/MQMessageId.h mode change 100755 => 100644 src/message/MQMessageQueue.cpp mode change 100755 => 100644 src/producer/DefaultMQProducer.cpp mode change 100755 => 100644 src/producer/SendResult.cpp mode change 100755 => 100644 src/protocol/ConsumerRunningInfo.cpp mode change 100755 => 100644 src/protocol/ConsumerRunningInfo.h mode change 100755 => 100644 src/protocol/HeartbeatData.h mode change 100755 => 100644 src/protocol/KVTable.h mode change 100755 => 100644 src/protocol/LockBatchBody.cpp mode change 100755 => 100644 src/protocol/LockBatchBody.h mode change 100755 => 100644 src/protocol/MQProtos.h mode change 100755 => 100644 src/protocol/MessageQueue.cpp mode change 100755 => 100644 src/protocol/MessageQueue.h mode change 100755 => 100644 src/protocol/ProcessQueueInfo.h mode change 100755 => 100644 src/protocol/RemotingCommand.h mode change 100755 => 100644 src/protocol/RemotingSerializable.h mode change 100755 => 100644 src/protocol/TopicList.h mode change 100755 => 100644 src/protocol/TopicRouteData.h mode change 100755 => 100644 src/thread/disruptorLFQ.h mode change 100755 => 100644 src/thread/task_queue.cpp mode change 100755 => 100644 src/thread/task_queue.h mode change 100755 => 100644 src/transport/ClientRemotingProcessor.cpp mode change 100755 => 100644 src/transport/ClientRemotingProcessor.h mode change 100755 => 100644 src/transport/ResponseFuture.cpp mode change 100755 => 100644 src/transport/ResponseFuture.h mode change 100755 => 100644 src/transport/SocketUtil.cpp mode change 100755 => 100644 src/transport/SocketUtil.h mode change 100755 => 100644 src/transport/TcpRemotingClient.cpp mode change 100755 => 100644 src/transport/TcpRemotingClient.h mode change 100755 => 100644 src/transport/TcpTransport.h diff --git a/.clang-format b/.clang-format index 9d807c3f2..7904cdf16 100644 --- a/.clang-format +++ b/.clang-format @@ -1,7 +1,3 @@ BasedOnStyle: Chromium ColumnLimit: 120 -IndentWidth: 4 -DerivePointerAlignment: false -IndentCaseLabels: false -PointerAlignment: Right -SpaceAfterCStyleCast: true +TabWidth: 2 diff --git a/example/AsyncProducer.cpp b/example/AsyncProducer.cpp old mode 100755 new mode 100644 index 3a5d0af65..dae04597c --- a/example/AsyncProducer.cpp +++ b/example/AsyncProducer.cpp @@ -57,13 +57,10 @@ class MyAutoDeleteSendCallback : public AutoDeleteSendCallBack { 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) { +void AsyncProducerWorker(RocketmqSendAndConsumerArgs* info, DefaultMQProducer* producer) { while (!g_quit.load()) { if (g_msgCount.load() <= 0) { std::unique_lock lck(g_mtx); @@ -99,7 +96,8 @@ int main(int argc, char* argv[]) { PrintRocketmqSendAndConsumerArgs(info); - if (!info.namesrv.empty()) producer.setNamesrvAddr(info.namesrv); + if (!info.namesrv.empty()) + producer.setNamesrvAddr(info.namesrv); producer.setGroupName(info.groupname); producer.setInstanceName(info.groupname); @@ -110,8 +108,7 @@ int main(int argc, char* argv[]) { 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); + std::shared_ptr th = std::make_shared(AsyncProducerWorker, &info, &producer); work_pool.push_back(th); } @@ -122,12 +119,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"; producer.shutdown(); for (size_t th = 0; th != work_pool.size(); ++th) { diff --git a/example/AsyncPushConsumer.cpp b/example/AsyncPushConsumer.cpp old mode 100755 new mode 100644 index 093d61de0..9f0a955b1 --- a/example/AsyncPushConsumer.cpp +++ b/example/AsyncPushConsumer.cpp @@ -37,7 +37,7 @@ class MyMsgListener : public MessageListenerConcurrently { MyMsgListener() {} virtual ~MyMsgListener() {} - virtual ConsumeStatus consumeMessage(const std::vector &msgs) { + 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(); @@ -51,7 +51,7 @@ class MyMsgListener : public MessageListenerConcurrently { } }; -int main(int argc, char *argv[]) { +int main(int argc, char* argv[]) { RocketmqSendAndConsumerArgs info; if (!ParseArgs(argc, argv, &info)) { exit(-1); @@ -82,7 +82,7 @@ int main(int argc, char *argv[]) { try { consumer.start(); - } catch (MQClientException &e) { + } catch (MQClientException& e) { cout << e << endl; } g_tps.start(); @@ -95,7 +95,7 @@ int main(int argc, char *argv[]) { try { producer.send(msg); - } catch (MQException &e) { + } catch (MQException& e) { std::cout << e << endl; // if catch excepiton , need re-send this msg by // service } diff --git a/example/BatchProducer.cpp b/example/BatchProducer.cpp index 3788422f8..d889ea91f 100644 --- a/example/BatchProducer.cpp +++ b/example/BatchProducer.cpp @@ -34,97 +34,91 @@ 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(); - break; - } +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(); + break; + } - vector msgs; - MQMessage msg1(info->topic, "*", info->body); - msg1.setProperty("property1", "value1"); - MQMessage msg2(info->topic, "*", info->body); - msg2.setProperty("property1", "value1"); - msg2.setProperty("property2", "value2"); - MQMessage msg3(info->topic, "*", info->body); - msg3.setProperty("property1", "value1"); - msg3.setProperty("property2", "value2"); - msg3.setProperty("property3", "value3"); - msgs.push_back(msg1); - msgs.push_back(msg2); - msgs.push_back(msg3); - try { - auto start = std::chrono::system_clock::now(); - SendResult sendResult = producer->send(msgs); - 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: " << e.what() << std::endl; - std::unique_lock lck(g_mtx); - g_finished.notify_one(); - return; - } + vector msgs; + MQMessage msg1(info->topic, "*", info->body); + msg1.setProperty("property1", "value1"); + MQMessage msg2(info->topic, "*", info->body); + msg2.setProperty("property1", "value1"); + msg2.setProperty("property2", "value2"); + MQMessage msg3(info->topic, "*", info->body); + msg3.setProperty("property1", "value1"); + msg3.setProperty("property2", "value2"); + msg3.setProperty("property3", "value3"); + msgs.push_back(msg1); + msgs.push_back(msg2); + msgs.push_back(msg3); + try { + auto start = std::chrono::system_clock::now(); + SendResult sendResult = producer->send(msgs); + 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: " << e.what() << std::endl; + std::unique_lock lck(g_mtx); + g_finished.notify_one(); + return; } + } } 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" + 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/CAsyncProducer.c b/example/CAsyncProducer.c index db4a957d9..2d2af61d5 100644 --- a/example/CAsyncProducer.c +++ b/example/CAsyncProducer.c @@ -29,61 +29,60 @@ void thread_sleep(unsigned milliseconds) { #ifdef _WIN32 - Sleep(milliseconds); + Sleep(milliseconds); #else - usleep(milliseconds * 1000); // takes microseconds + usleep(milliseconds * 1000); // takes microseconds #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){ - 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); +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); } -void StartSendMessage(CProducer *producer) { - int i = 0; - int ret_code = 0; - char body[128]; - CMessage *msg = CreateMessage("T_TestTopic"); - SetMessageTags(msg, "Test_Tag"); - SetMessageKeys(msg, "Test_Keys"); - for (i = 0; i < 10; i++) { - 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); - printf("async send message[%d] return code: %d\n", i, ret_code); - thread_sleep(1000); - } - DestroyMessage(msg); +void StartSendMessage(CProducer* producer) { + int i = 0; + int ret_code = 0; + char body[128]; + CMessage* msg = CreateMessage("T_TestTopic"); + SetMessageTags(msg, "Test_Tag"); + SetMessageKeys(msg, "Test_Keys"); + for (i = 0; i < 10; i++) { + 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); + printf("async send message[%d] return code: %d\n", i, ret_code); + thread_sleep(1000); + } + DestroyMessage(msg); } -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); - } - StartProducer(producer); - printf("Producer start.....\n"); - StartSendMessage(producer); - ShutdownProducer(producer); - DestroyProducer(producer); - printf("Producer Shutdown!\n"); +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); + } + StartProducer(producer); + printf("Producer start.....\n"); + StartSendMessage(producer); + ShutdownProducer(producer); + DestroyProducer(producer); + printf("Producer Shutdown!\n"); } -int main(int argc, char *argv[]) { - printf("Send Async successCallback.....\n"); - CreateProducerAndStartSendMessage(0); +int main(int argc, char* argv[]) { + printf("Send Async successCallback.....\n"); + CreateProducerAndStartSendMessage(0); - printf("Send Async exceptionCallback.....\n"); - CreateProducerAndStartSendMessage(1); - return 0; + printf("Send Async exceptionCallback.....\n"); + CreateProducerAndStartSendMessage(1); + return 0; } - diff --git a/example/COrderlyAsyncProducer.c b/example/COrderlyAsyncProducer.c index f841878ed..48a822cec 100644 --- a/example/COrderlyAsyncProducer.c +++ b/example/COrderlyAsyncProducer.c @@ -29,68 +29,65 @@ void thread_sleep(unsigned milliseconds) { #ifdef _WIN32 - Sleep(milliseconds); + Sleep(milliseconds); #else - usleep(milliseconds * 1000); // takes microseconds + usleep(milliseconds * 1000); // takes microseconds #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){ - 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); +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){ - return 0; +int aQueueSelectorCallback(int size, CMessage* msg, void* arg) { + return 0; }; -void StartSendMessage(CProducer *producer) { - int i = 0; - int ret_code = 0; - char body[128]; - CMessage *msg = CreateMessage("topic_COrderlyAsyncProducer"); - SetMessageTags(msg, "Test_Tag"); - SetMessageKeys(msg, "Test_Keys"); - for (i = 0; i < 10; i++) { - 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); - printf("async send message[%d] return code: %d\n", i, ret_code); - thread_sleep(1000); - } - DestroyMessage(msg); +void StartSendMessage(CProducer* producer) { + int i = 0; + int ret_code = 0; + char body[128]; + CMessage* msg = CreateMessage("topic_COrderlyAsyncProducer"); + SetMessageTags(msg, "Test_Tag"); + SetMessageKeys(msg, "Test_Keys"); + for (i = 0; i < 10; i++) { + 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); + printf("async send message[%d] return code: %d\n", i, ret_code); + thread_sleep(1000); + } + DestroyMessage(msg); } -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); - } - StartProducer(producer); - printf("Producer start.....\n"); - StartSendMessage(producer); - ShutdownProducer(producer); - DestroyProducer(producer); - printf("Producer Shutdown!\n"); +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); + } + StartProducer(producer); + printf("Producer start.....\n"); + StartSendMessage(producer); + ShutdownProducer(producer); + DestroyProducer(producer); + printf("Producer Shutdown!\n"); } -int main(int argc, char *argv[]) { - printf("COrderlyAsyncProducer successCallback.....\n"); - CreateProducerAndStartSendMessage(0); +int main(int argc, char* argv[]) { + printf("COrderlyAsyncProducer successCallback.....\n"); + CreateProducerAndStartSendMessage(0); - printf("COrderlyAsyncProducer exceptionCallback.....\n"); - CreateProducerAndStartSendMessage(1); - return 0; + printf("COrderlyAsyncProducer exceptionCallback.....\n"); + CreateProducerAndStartSendMessage(1); + return 0; } - diff --git a/example/OrderProducer.cpp b/example/OrderProducer.cpp old mode 100755 new mode 100644 index ba4636314..613add4b8 --- a/example/OrderProducer.cpp +++ b/example/OrderProducer.cpp @@ -34,9 +34,8 @@ 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); + MQMessageQueue select(const std::vector& mqs, const MQMessage& msg, void* arg) { + int orderId = *static_cast(arg); int index = orderId % mqs.size(); return mqs[index]; } @@ -44,8 +43,7 @@ class SelectMessageQueueByHash : public MessageQueueSelector { SelectMessageQueueByHash g_mySelector; -void ProducerWorker(RocketmqSendAndConsumerArgs *info, - DefaultMQProducer *producer) { +void ProducerWorker(RocketmqSendAndConsumerArgs* info, DefaultMQProducer* producer) { while (!g_quit.load()) { if (g_msgCount.load() <= 0) { std::unique_lock lck(g_mtx); @@ -57,13 +55,12 @@ void ProducerWorker(RocketmqSendAndConsumerArgs *info, int orderId = 1; SendResult sendResult = - producer->send(msg, &g_mySelector, static_cast(&orderId), - info->retrytimes, info->SelectUnactiveBroker); + producer->send(msg, &g_mySelector, static_cast(&orderId), info->retrytimes, info->SelectUnactiveBroker); --g_msgCount; } } -int main(int argc, char *argv[]) { +int main(int argc, char* argv[]) { RocketmqSendAndConsumerArgs info; if (!ParseArgs(argc, argv, &info)) { exit(-1); @@ -84,8 +81,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(ProducerWorker, &info, &producer); + std::shared_ptr th = std::make_shared(ProducerWorker, &info, &producer); work_pool.push_back(th); } @@ -97,12 +93,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/OrderlyPushConsumer.cpp b/example/OrderlyPushConsumer.cpp old mode 100755 new mode 100644 index 81ad53321..c8a9eaa10 --- a/example/OrderlyPushConsumer.cpp +++ b/example/OrderlyPushConsumer.cpp @@ -40,7 +40,7 @@ class MyMsgListener : public MessageListenerOrderly { MyMsgListener() {} virtual ~MyMsgListener() {} - virtual ConsumeStatus consumeMessage(const vector &msgs) { + virtual ConsumeStatus consumeMessage(const vector& msgs) { if (g_consumedCount.load() >= g_msgCount) { std::unique_lock lK(g_mtx); g_quit.store(true); @@ -54,7 +54,7 @@ class MyMsgListener : public MessageListenerOrderly { } }; -int main(int argc, char *argv[]) { +int main(int argc, char* argv[]) { RocketmqSendAndConsumerArgs info; if (!ParseArgs(argc, argv, &info)) { exit(-1); @@ -74,7 +74,8 @@ int main(int argc, char *argv[]) { consumer.subscribe(info.topic, "*"); consumer.setConsumeThreadCount(info.thread_count); consumer.setConsumeMessageBatchMaxSize(31); - if (info.syncpush) consumer.setAsyncPull(false); + if (info.syncpush) + consumer.setAsyncPull(false); MyMsgListener msglistener; consumer.registerMessageListener(&msglistener); @@ -82,7 +83,7 @@ int main(int argc, char *argv[]) { try { consumer.start(); - } catch (MQClientException &e) { + } catch (MQClientException& e) { std::cout << e << std::endl; } @@ -94,7 +95,7 @@ int main(int argc, char *argv[]) { try { producer.send(msg); - } catch (MQException &e) { + } catch (MQException& e) { std::cout << e << endl; // if catch excepiton , need re-send this msg by // service } diff --git a/example/Producer.c b/example/Producer.c index cfffd77b4..d2ec42683 100644 --- a/example/Producer.c +++ b/example/Producer.c @@ -29,40 +29,39 @@ void thread_sleep(unsigned milliseconds) { #ifdef _WIN32 - Sleep(milliseconds); + Sleep(milliseconds); #else - usleep(milliseconds * 1000); // takes microseconds + usleep(milliseconds * 1000); // takes microseconds #endif } -void StartSendMessage(CProducer *producer) { - int i = 0; - char body[256]; - CMessage *msg = CreateMessage("T_TestTopic"); - SetMessageTags(msg, "Test_Tag"); - SetMessageKeys(msg, "Test_Keys"); - CSendResult result; - for (i = 0; i < 10; i++) { - memset(body, 0, sizeof(body)); - snprintf(body, sizeof(body), "new message body, index %d", i); - SetMessageBody(msg, body); - SendMessageSync(producer, msg, &result); - printf("send message[%d] result status:%d, msgId:%s\n", i, (int)result.sendStatus, result.msgId); - thread_sleep(1000); - } - DestroyMessage(msg); +void StartSendMessage(CProducer* producer) { + int i = 0; + char body[256]; + CMessage* msg = CreateMessage("T_TestTopic"); + SetMessageTags(msg, "Test_Tag"); + SetMessageKeys(msg, "Test_Keys"); + CSendResult result; + for (i = 0; i < 10; i++) { + memset(body, 0, sizeof(body)); + snprintf(body, sizeof(body), "new message body, index %d", i); + SetMessageBody(msg, body); + SendMessageSync(producer, msg, &result); + printf("send message[%d] result status:%d, msgId:%s\n", i, (int)result.sendStatus, result.msgId); + thread_sleep(1000); + } + DestroyMessage(msg); } -int main(int argc, char *argv[]) { - printf("Producer Initializing.....\n"); - CProducer *producer = CreateProducer("Group_producer"); - SetProducerNameServerAddress(producer, "127.0.0.1:9876"); - StartProducer(producer); - printf("Producer start.....\n"); - StartSendMessage(producer); - ShutdownProducer(producer); - DestroyProducer(producer); - printf("Producer Shutdown!\n"); - return 0; +int main(int argc, char* argv[]) { + printf("Producer Initializing.....\n"); + CProducer* producer = CreateProducer("Group_producer"); + SetProducerNameServerAddress(producer, "127.0.0.1:9876"); + StartProducer(producer); + printf("Producer start.....\n"); + StartSendMessage(producer); + ShutdownProducer(producer); + DestroyProducer(producer); + printf("Producer Shutdown!\n"); + return 0; } - diff --git a/example/PullConsumeMessage.c b/example/PullConsumeMessage.c index 657370f58..0868d08d0 100644 --- a/example/PullConsumeMessage.c +++ b/example/PullConsumeMessage.c @@ -30,65 +30,66 @@ void thread_sleep(unsigned milliseconds) { #ifdef _WIN32 - Sleep(milliseconds); + Sleep(milliseconds); #else - usleep(milliseconds * 1000); // takes microseconds + usleep(milliseconds * 1000); // takes microseconds #endif } -int main(int argc, char *argv[]) { - int i = 0, j = 0; - int ret = 0; - int size = 0; - CMessageQueue *mqs = NULL; - printf("PullConsumer Initializing....\n"); - CPullConsumer *consumer = CreatePullConsumer("Group_Consumer_Test"); - SetPullConsumerNameServerAddress(consumer, "172.17.0.2:9876"); - StartPullConsumer(consumer); - printf("Pull Consumer Start...\n"); - for (i = 1; i <= 5; i++) { - printf("FetchSubscriptionMessageQueues : %d times\n", i); - ret = FetchSubscriptionMessageQueues(consumer, "T_TestTopic", &mqs, &size); - if(ret != OK) { - printf("Get MQ Queue Failed,ErrorCode:%d\n", ret); +int main(int argc, char* argv[]) { + int i = 0, j = 0; + int ret = 0; + int size = 0; + CMessageQueue* mqs = NULL; + printf("PullConsumer Initializing....\n"); + CPullConsumer* consumer = CreatePullConsumer("Group_Consumer_Test"); + SetPullConsumerNameServerAddress(consumer, "172.17.0.2:9876"); + StartPullConsumer(consumer); + printf("Pull Consumer Start...\n"); + for (i = 1; i <= 5; i++) { + printf("FetchSubscriptionMessageQueues : %d times\n", i); + ret = FetchSubscriptionMessageQueues(consumer, "T_TestTopic", &mqs, &size); + if (ret != OK) { + printf("Get MQ Queue Failed,ErrorCode:%d\n", ret); + } + printf("Get MQ Size:%d\n", size); + for (j = 0; j < size; j++) { + int noNewMsg = 0; + long long tmpoffset = 0; + printf("Pull Message For Topic:%s,Queue:%s,QueueId:%d\n", mqs[j].topic, mqs[j].brokerName, mqs[j].queueId); + do { + int k = 0; + CPullResult pullResult = Pull(consumer, &mqs[j], "*", tmpoffset, 32); + if (pullResult.pullStatus != E_BROKER_TIMEOUT) { + tmpoffset = pullResult.nextBeginOffset; } - printf("Get MQ Size:%d\n", size); - for (j = 0; j < size; j++) { - int noNewMsg = 0; - long long tmpoffset = 0; - printf("Pull Message For Topic:%s,Queue:%s,QueueId:%d\n", mqs[j].topic, mqs[j].brokerName, mqs[j].queueId); - do { - int k = 0; - CPullResult pullResult = Pull(consumer, &mqs[j], "*", tmpoffset, 32); - if (pullResult.pullStatus != E_BROKER_TIMEOUT) { - tmpoffset = pullResult.nextBeginOffset; - } - printf("PullStatus:%d,MaxOffset:%lld,MinOffset:%lld,NextBegainOffset:%lld", pullResult.pullStatus, - pullResult.maxOffset, pullResult.minOffset, pullResult.nextBeginOffset); - switch (pullResult.pullStatus) { - 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])); - } - break; - case E_NO_MATCHED_MSG: - noNewMsg = 1; - break; - default: - noNewMsg = 0; - } - ReleasePullResult(pullResult); - thread_sleep(100); - } while (noNewMsg == 0); - thread_sleep(1000); + printf("PullStatus:%d,MaxOffset:%lld,MinOffset:%lld,NextBegainOffset:%lld", pullResult.pullStatus, + pullResult.maxOffset, pullResult.minOffset, pullResult.nextBeginOffset); + switch (pullResult.pullStatus) { + 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])); + } + break; + case E_NO_MATCHED_MSG: + noNewMsg = 1; + break; + default: + noNewMsg = 0; } - thread_sleep(2000); - ReleaseSubscriptionMessageQueue(mqs); + ReleasePullResult(pullResult); + thread_sleep(100); + } while (noNewMsg == 0); + thread_sleep(1000); } - thread_sleep(5000); - ShutdownPullConsumer(consumer); - DestroyPullConsumer(consumer); - printf("PullConsumer Shutdown!\n"); - return 0; + thread_sleep(2000); + ReleaseSubscriptionMessageQueue(mqs); + } + thread_sleep(5000); + ShutdownPullConsumer(consumer); + DestroyPullConsumer(consumer); + printf("PullConsumer Shutdown!\n"); + return 0; } diff --git a/example/PullConsumer.cpp b/example/PullConsumer.cpp old mode 100755 new mode 100644 index 857ed74dc..52e9094d6 --- a/example/PullConsumer.cpp +++ b/example/PullConsumer.cpp @@ -42,7 +42,7 @@ uint64_t getMessageQueueOffset(MQMessageQueue mq) { return 0; } -int main(int argc, char *argv[]) { +int main(int argc, char* argv[]) { RocketmqSendAndConsumerArgs info; if (!ParseArgs(argc, argv, &info)) { exit(-1); @@ -63,7 +63,7 @@ int main(int argc, char *argv[]) { for (; iter != mqs.end(); ++iter) { std::cout << "mq:" << (*iter).toString() << endl; } - } catch (MQException &e) { + } catch (MQException& e) { std::cout << e << endl; } @@ -79,8 +79,7 @@ int main(int argc, char *argv[]) { bool noNewMsg = false; do { try { - PullResult result = - consumer.pull(mq, "*", getMessageQueueOffset(mq), 32); + 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 @@ -104,21 +103,18 @@ int main(int argc, char *argv[]) { default: break; } - } catch (MQClientException &e) { + } 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 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 << "per msg time: " << duration.count() / (double)g_msgCount.load() << "ms \n" + << "========================finished==============================\n"; consumer.shutdown(); return 0; diff --git a/example/PushConsumeMessage.c b/example/PushConsumeMessage.c index 81bcbc1a3..2cb9c3f49 100644 --- a/example/PushConsumeMessage.c +++ b/example/PushConsumeMessage.c @@ -28,36 +28,36 @@ void thread_sleep(unsigned milliseconds) { #ifdef _WIN32 - Sleep(milliseconds); + Sleep(milliseconds); #else - usleep(milliseconds * 1000); // takes microseconds + usleep(milliseconds * 1000); // takes microseconds #endif } -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)); - return E_CONSUME_SUCCESS; +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)); + return E_CONSUME_SUCCESS; } -int main(int argc, char *argv[]) { - int i = 0; - printf("PushConsumer Initializing....\n"); - CPushConsumer *consumer = CreatePushConsumer("Group_Consumer_Test"); - SetPushConsumerNameServerAddress(consumer, "172.17.0.2:9876"); - Subscribe(consumer, "T_TestTopic", "*"); - RegisterMessageCallback(consumer, doConsumeMessage); - StartPushConsumer(consumer); - printf("Push Consumer Start...\n"); - for (i = 0; i < 10; i++) { - printf("Now Running : %d S\n", i * 10); - thread_sleep(10000); - } - ShutdownPushConsumer(consumer); - DestroyPushConsumer(consumer); - printf("PushConsumer Shutdown!\n"); - return 0; +int main(int argc, char* argv[]) { + int i = 0; + printf("PushConsumer Initializing....\n"); + CPushConsumer* consumer = CreatePushConsumer("Group_Consumer_Test"); + SetPushConsumerNameServerAddress(consumer, "172.17.0.2:9876"); + Subscribe(consumer, "T_TestTopic", "*"); + RegisterMessageCallback(consumer, doConsumeMessage); + StartPushConsumer(consumer); + printf("Push Consumer Start...\n"); + for (i = 0; i < 10; i++) { + printf("Now Running : %d S\n", i * 10); + thread_sleep(10000); + } + ShutdownPushConsumer(consumer); + DestroyPushConsumer(consumer); + printf("PushConsumer Shutdown!\n"); + return 0; } diff --git a/example/PushConsumer.cpp b/example/PushConsumer.cpp old mode 100755 new mode 100644 index 119b7f267..f0d4926cf --- a/example/PushConsumer.cpp +++ b/example/PushConsumer.cpp @@ -39,7 +39,7 @@ class MyMsgListener : public MessageListenerConcurrently { MyMsgListener() {} virtual ~MyMsgListener() {} - virtual ConsumeStatus consumeMessage(const std::vector &msgs) { + 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(); @@ -54,7 +54,7 @@ class MyMsgListener : public MessageListenerConcurrently { } }; -int main(int argc, char *argv[]) { +int main(int argc, char* argv[]) { RocketmqSendAndConsumerArgs info; if (!ParseArgs(argc, argv, &info)) { exit(-1); @@ -62,8 +62,7 @@ int main(int argc, char *argv[]) { PrintRocketmqSendAndConsumerArgs(info); DefaultMQPushConsumer consumer("please_rename_unique_group_name"); DefaultMQProducer producer("please_rename_unique_group_name"); - producer.setSessionCredentials("AccessKey", - "SecretKey", "ALIYUN"); + producer.setSessionCredentials("AccessKey", "SecretKey", "ALIYUN"); producer.setTcpTransportTryLockTimeout(1000); producer.setTcpTransportConnectTimeout(400); producer.setNamesrvDomain(info.namesrv_domain); @@ -73,13 +72,13 @@ int main(int argc, char *argv[]) { consumer.setNamesrvAddr(info.namesrv); consumer.setGroupName(info.groupname); - consumer.setSessionCredentials("AccessKey", - "SecretKey", "ALIYUN"); + 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.syncpush) + consumer.setAsyncPull(false); // set sync pull if (info.broadcasting) { consumer.setMessageModel(rocketmq::BROADCASTING); } @@ -96,7 +95,7 @@ int main(int argc, char *argv[]) { try { consumer.start(); - } catch (MQClientException &e) { + } catch (MQClientException& e) { cout << e << endl; } g_tps.start(); @@ -110,7 +109,7 @@ int main(int argc, char *argv[]) { // std::this_thread::sleep_for(std::chrono::seconds(100000)); try { producer.send(msg); - } catch (MQException &e) { + } catch (MQException& e) { std::cout << e << endl; // if catch excepiton , need re-send this msg by // service } diff --git a/example/PushConsumerOrderly.c b/example/PushConsumerOrderly.c index 63ca5275c..4b2c2bbbd 100644 --- a/example/PushConsumerOrderly.c +++ b/example/PushConsumerOrderly.c @@ -22,35 +22,30 @@ #include "CCommon.h" #include "CMessageExt.h" - -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)); - return E_CONSUME_SUCCESS; +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)); + return E_CONSUME_SUCCESS; } - -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); - StartPushConsumer(consumer); - printf("Push Consumer Start...\n"); - for( i=0; i<10; i++) - { - printf("Now Running : %d S\n",i*10); - sleep(10); - } - ShutdownPushConsumer(consumer); - DestroyPushConsumer(consumer); - printf("PushConsumer Shutdown!\n"); - return 0; +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); + StartPushConsumer(consumer); + printf("Push Consumer Start...\n"); + for (i = 0; i < 10; i++) { + printf("Now Running : %d S\n", i * 10); + sleep(10); + } + ShutdownPushConsumer(consumer); + DestroyPushConsumer(consumer); + printf("PushConsumer Shutdown!\n"); + return 0; } diff --git a/example/SendDelayMsg.cpp b/example/SendDelayMsg.cpp old mode 100755 new mode 100644 diff --git a/example/SyncProducer.cpp b/example/SyncProducer.cpp old mode 100755 new mode 100644 index f5682a697..0edb6dc8e --- a/example/SyncProducer.cpp +++ b/example/SyncProducer.cpp @@ -35,8 +35,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); @@ -51,11 +50,9 @@ 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) { std::cout << "send failed: " << std::endl; @@ -87,8 +84,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); } @@ -99,12 +95,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/common.h b/example/common.h old mode 100755 new mode 100644 index 21d2ba02f..5b674c37e --- a/example/common.h +++ b/example/common.h @@ -66,20 +66,20 @@ class TpsReportService { 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; + 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; + 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_; } @@ -112,17 +112,14 @@ 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(); + std::vector::iterator it = result->msgFoundList.begin(); for (; it != result->msgFoundList.end(); ++it) { - cout << "=======================================================" << endl - << (*it).toString() << endl; + cout << "=======================================================" << endl << (*it).toString() << endl; } } } -static void PrintRocketmqSendAndConsumerArgs( - const RocketmqSendAndConsumerArgs& info) { +static void PrintRocketmqSendAndConsumerArgs(const RocketmqSendAndConsumerArgs& info) { std::cout << "nameserver: " << info.namesrv << endl << "topic: " << info.topic << endl << "groupname: " << info.groupname << endl @@ -132,27 +129,25 @@ static void PrintRocketmqSendAndConsumerArgs( } 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) { @@ -209,18 +204,20 @@ static bool ParseArgs(int argc, char* argv[], 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; + 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; + 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; + 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())) { + 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; diff --git a/format.sh b/format.sh index 0e8e26974..f46c5f296 100755 --- a/format.sh +++ b/format.sh @@ -17,7 +17,7 @@ TMPFILE=".clang_format_file.tmp" -FORMAT="{BasedOnStyle: Google,IndentWidth: 2,ColumnLimit: 80}" +FORMAT="{BasedOnStyle: Chromium, ColumnLimit: 120, TabWidth: 2}" function Usage { @@ -62,7 +62,7 @@ done for file in $files do echo $file - clang-format -style="$FORMAT" $file > $TMPFILE + clang-format $file > $TMPFILE if [ -e $TMPFILE ];then filesize=`wc -c $TMPFILE |cut -d " " -f1` diff --git a/include/Arg_helper.h b/include/Arg_helper.h old mode 100755 new mode 100644 diff --git a/include/AsyncCallback.h b/include/AsyncCallback.h old mode 100755 new mode 100644 index 2e5caeb92..0b657ce36 --- a/include/AsyncCallback.h +++ b/include/AsyncCallback.h @@ -27,38 +27,30 @@ namespace rocketmq { // 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..a765184c5 100644 --- a/include/CCommon.h +++ b/include/CCommon.h @@ -26,36 +26,36 @@ extern "C" { #define MAX_TOPIC_LENGTH 512 #define MAX_BROKER_NAME_ID_LENGTH 256 typedef enum _CStatus_ { - // Success - OK = 0, - // Failed, null pointer value - NULL_POINTER = 1, - MALLOC_FAILED = 2, - PRODUCER_ERROR_CODE_START = 10, - PRODUCER_START_FAILED = 10, - PRODUCER_SEND_SYNC_FAILED = 11, - PRODUCER_SEND_ONEWAY_FAILED = 12, - PRODUCER_SEND_ORDERLY_FAILED = 13, - PRODUCER_SEND_ASYNC_FAILED = 14, - PRODUCER_SEND_ORDERLYASYNC_FAILED = 15, + // Success + OK = 0, + // Failed, null pointer value + NULL_POINTER = 1, + MALLOC_FAILED = 2, + PRODUCER_ERROR_CODE_START = 10, + PRODUCER_START_FAILED = 10, + PRODUCER_SEND_SYNC_FAILED = 11, + PRODUCER_SEND_ONEWAY_FAILED = 12, + PRODUCER_SEND_ORDERLY_FAILED = 13, + PRODUCER_SEND_ASYNC_FAILED = 14, + PRODUCER_SEND_ORDERLYASYNC_FAILED = 15, - PUSHCONSUMER_ERROR_CODE_START = 20, - PUSHCONSUMER_START_FAILED = 20, + PUSHCONSUMER_ERROR_CODE_START = 20, + PUSHCONSUMER_START_FAILED = 20, - PULLCONSUMER_ERROR_CODE_START = 30, - PULLCONSUMER_START_FAILED = 30, - PULLCONSUMER_FETCH_MQ_FAILED = 31, - PULLCONSUMER_FETCH_MESSAGE_FAILED = 32 + PULLCONSUMER_ERROR_CODE_START = 30, + PULLCONSUMER_START_FAILED = 30, + PULLCONSUMER_FETCH_MQ_FAILED = 31, + PULLCONSUMER_FETCH_MESSAGE_FAILED = 32 } CStatus; typedef enum _CLogLevel_ { - E_LOG_LEVEL_FATAL = 1, - E_LOG_LEVEL_ERROR = 2, - E_LOG_LEVEL_WARN = 3, - E_LOG_LEVEL_INFO = 4, - E_LOG_LEVEL_DEBUG = 5, - E_LOG_LEVEL_TRACE = 6, - E_LOG_LEVEL_LEVEL_NUM = 7 + E_LOG_LEVEL_FATAL = 1, + E_LOG_LEVEL_ERROR = 2, + E_LOG_LEVEL_WARN = 3, + E_LOG_LEVEL_INFO = 4, + E_LOG_LEVEL_DEBUG = 5, + E_LOG_LEVEL_TRACE = 6, + E_LOG_LEVEL_LEVEL_NUM = 7 } CLogLevel; #ifdef WIN32 diff --git a/include/CMQException.h b/include/CMQException.h index 69706e83c..51f956048 100644 --- a/include/CMQException.h +++ b/include/CMQException.h @@ -22,15 +22,15 @@ 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_{ - int error; - int line; - char file[MAX_EXEPTION_FILE_LENGTH]; - char msg[MAX_EXEPTION_MSG_LENGTH]; - char type[MAX_EXEPTION_TYPE_LENGTH]; +typedef struct _CMQException_ { + int error; + int line; + char file[MAX_EXEPTION_FILE_LENGTH]; + char msg[MAX_EXEPTION_MSG_LENGTH]; + char type[MAX_EXEPTION_TYPE_LENGTH]; } CMQException; diff --git a/include/CMessage.h b/include/CMessage.h index cef7889e9..f1b057b80 100644 --- a/include/CMessage.h +++ b/include/CMessage.h @@ -23,21 +23,20 @@ extern "C" { #endif -//typedef struct _CMessage_ CMessage; +// typedef struct _CMessage_ CMessage; typedef struct CMessage CMessage; - -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); -ROCKETMQCLIENT_API int SetMessageKeys(CMessage *msg, const char *keys); -ROCKETMQCLIENT_API int SetMessageBody(CMessage *msg, const char *body); -ROCKETMQCLIENT_API int SetByteMessageBody(CMessage *msg, const char *body, int len); -ROCKETMQCLIENT_API int SetMessageProperty(CMessage *msg, const char *key, const char *value); -ROCKETMQCLIENT_API int SetDelayTimeLevel(CMessage *msg, int level); +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); +ROCKETMQCLIENT_API int SetMessageKeys(CMessage* msg, const char* keys); +ROCKETMQCLIENT_API int SetMessageBody(CMessage* msg, const char* body); +ROCKETMQCLIENT_API int SetByteMessageBody(CMessage* msg, const char* body, int len); +ROCKETMQCLIENT_API int SetMessageProperty(CMessage* msg, const char* key, const char* value); +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..acd85e319 100644 --- a/include/CMessageExt.h +++ b/include/CMessageExt.h @@ -24,26 +24,26 @@ extern "C" { #endif -//typedef struct _CMessageExt_ _CMessageExt; +// typedef struct _CMessageExt_ _CMessageExt; typedef struct CMessageExt CMessageExt; -ROCKETMQCLIENT_API const char *GetMessageTopic(CMessageExt *msgExt); -ROCKETMQCLIENT_API const char *GetMessageTags(CMessageExt *msgExt); -ROCKETMQCLIENT_API const char *GetMessageKeys(CMessageExt *msgExt); -ROCKETMQCLIENT_API const char *GetMessageBody(CMessageExt *msgExt); -ROCKETMQCLIENT_API const char *GetMessageProperty(CMessageExt *msgExt, const char *key); -ROCKETMQCLIENT_API const char *GetMessageId(CMessageExt *msgExt); -ROCKETMQCLIENT_API int GetMessageDelayTimeLevel(CMessageExt *msgExt); -ROCKETMQCLIENT_API int GetMessageQueueId(CMessageExt *msgExt); -ROCKETMQCLIENT_API int GetMessageReconsumeTimes(CMessageExt *msgExt); -ROCKETMQCLIENT_API int GetMessageStoreSize(CMessageExt *msgExt); -ROCKETMQCLIENT_API long long GetMessageBornTimestamp(CMessageExt *msgExt); -ROCKETMQCLIENT_API long long GetMessageStoreTimestamp(CMessageExt *msgExt); -ROCKETMQCLIENT_API long long GetMessageQueueOffset(CMessageExt *msgExt); -ROCKETMQCLIENT_API long long GetMessageCommitLogOffset(CMessageExt *msgExt); -ROCKETMQCLIENT_API long long GetMessagePreparedTransactionOffset(CMessageExt *msgExt); +ROCKETMQCLIENT_API const char* GetMessageTopic(CMessageExt* msgExt); +ROCKETMQCLIENT_API const char* GetMessageTags(CMessageExt* msgExt); +ROCKETMQCLIENT_API const char* GetMessageKeys(CMessageExt* msgExt); +ROCKETMQCLIENT_API const char* GetMessageBody(CMessageExt* msgExt); +ROCKETMQCLIENT_API const char* GetMessageProperty(CMessageExt* msgExt, const char* key); +ROCKETMQCLIENT_API const char* GetMessageId(CMessageExt* msgExt); +ROCKETMQCLIENT_API int GetMessageDelayTimeLevel(CMessageExt* msgExt); +ROCKETMQCLIENT_API int GetMessageQueueId(CMessageExt* msgExt); +ROCKETMQCLIENT_API int GetMessageReconsumeTimes(CMessageExt* msgExt); +ROCKETMQCLIENT_API int GetMessageStoreSize(CMessageExt* msgExt); +ROCKETMQCLIENT_API long long GetMessageBornTimestamp(CMessageExt* msgExt); +ROCKETMQCLIENT_API long long GetMessageStoreTimestamp(CMessageExt* msgExt); +ROCKETMQCLIENT_API long long GetMessageQueueOffset(CMessageExt* msgExt); +ROCKETMQCLIENT_API long long GetMessageCommitLogOffset(CMessageExt* msgExt); +ROCKETMQCLIENT_API long long GetMessagePreparedTransactionOffset(CMessageExt* msgExt); #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..c514f9d5f 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..e345e60ea 100644 --- a/include/CProducer.h +++ b/include/CProducer.h @@ -26,40 +26,57 @@ 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); -ROCKETMQCLIENT_API int StartProducer(CProducer *producer); -ROCKETMQCLIENT_API int ShutdownProducer(CProducer *producer); +ROCKETMQCLIENT_API CProducer* CreateProducer(const char* groupId); +ROCKETMQCLIENT_API int DestroyProducer(CProducer* producer); +ROCKETMQCLIENT_API int StartProducer(CProducer* producer); +ROCKETMQCLIENT_API int ShutdownProducer(CProducer* producer); -ROCKETMQCLIENT_API int SetProducerNameServerAddress(CProducer *producer, const char *namesrv); -ROCKETMQCLIENT_API int SetProducerNameServerDomain(CProducer *producer, const char *domain); -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); -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); -ROCKETMQCLIENT_API int SetProducerSendMsgTimeout(CProducer *producer, int timeout); -ROCKETMQCLIENT_API int SetProducerCompressLevel(CProducer *producer, int level); -ROCKETMQCLIENT_API int SetProducerMaxMessageSize(CProducer *producer, int size); +ROCKETMQCLIENT_API int SetProducerNameServerAddress(CProducer* producer, const char* namesrv); +ROCKETMQCLIENT_API int SetProducerNameServerDomain(CProducer* producer, const char* domain); +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); +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); +ROCKETMQCLIENT_API int SetProducerSendMsgTimeout(CProducer* producer, int timeout); +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 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 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..01571088a 100644 --- a/include/CPullConsumer.h +++ b/include/CPullConsumer.h @@ -27,32 +27,35 @@ 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); -ROCKETMQCLIENT_API int ShutdownPullConsumer(CPullConsumer *consumer); -ROCKETMQCLIENT_API int SetPullConsumerGroupID(CPullConsumer *consumer, const char *groupId); -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 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 ReleaseSubscriptionMessageQueue(CMessageQueue *mqs); +ROCKETMQCLIENT_API CPullConsumer* CreatePullConsumer(const char* groupId); +ROCKETMQCLIENT_API int DestroyPullConsumer(CPullConsumer* consumer); +ROCKETMQCLIENT_API int StartPullConsumer(CPullConsumer* consumer); +ROCKETMQCLIENT_API int ShutdownPullConsumer(CPullConsumer* consumer); +ROCKETMQCLIENT_API int SetPullConsumerGroupID(CPullConsumer* consumer, const char* groupId); +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 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 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..2ab174f73 100644 --- a/include/CPullResult.h +++ b/include/CPullResult.h @@ -25,24 +25,24 @@ extern "C" { #endif typedef enum E_CPullStatus { - E_FOUND, - E_NO_NEW_MSG, - E_NO_MATCHED_MSG, - E_OFFSET_ILLEGAL, - E_BROKER_TIMEOUT //indicate pull request timeout or received NULL response + E_FOUND, + E_NO_NEW_MSG, + E_NO_MATCHED_MSG, + E_OFFSET_ILLEGAL, + E_BROKER_TIMEOUT // indicate pull request timeout or received NULL response } CPullStatus; typedef struct _CPullResult_ { - CPullStatus pullStatus; - long long nextBeginOffset; - long long minOffset; - long long maxOffset; - CMessageExt **msgFoundList; - int size; - void *pData; + CPullStatus pullStatus; + long long nextBeginOffset; + long long minOffset; + long long maxOffset; + CMessageExt** msgFoundList; + int size; + void* pData; } 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..10b55873b 100644 --- a/include/CPushConsumer.h +++ b/include/CPushConsumer.h @@ -25,39 +25,39 @@ extern "C" { #endif -//typedef struct _CConsumer_ _CConsumer; +// typedef struct _CConsumer_ _CConsumer; typedef struct CPushConsumer CPushConsumer; -typedef enum E_CConsumeStatus{ - E_CONSUME_SUCCESS = 0, - E_RECONSUME_LATER = 1 -} CConsumeStatus; - -typedef int(*MessageCallBack)(CPushConsumer *, CMessageExt *); - -ROCKETMQCLIENT_API CPushConsumer *CreatePushConsumer(const char *groupId); -ROCKETMQCLIENT_API int DestroyPushConsumer(CPushConsumer *consumer); -ROCKETMQCLIENT_API int StartPushConsumer(CPushConsumer *consumer); -ROCKETMQCLIENT_API int ShutdownPushConsumer(CPushConsumer *consumer); -ROCKETMQCLIENT_API int SetPushConsumerGroupID(CPushConsumer *consumer, const char *groupId); -ROCKETMQCLIENT_API const char *GetPushConsumerGroupID(CPushConsumer *consumer); -ROCKETMQCLIENT_API int SetPushConsumerNameServerAddress(CPushConsumer *consumer, const char *namesrv); -ROCKETMQCLIENT_API int SetPushConsumerNameServerDomain(CPushConsumer *consumer, const char *domain); -ROCKETMQCLIENT_API int Subscribe(CPushConsumer *consumer, const char *topic, const char *expression); -ROCKETMQCLIENT_API int RegisterMessageCallbackOrderly(CPushConsumer *consumer, MessageCallBack pCallback); -ROCKETMQCLIENT_API int RegisterMessageCallback(CPushConsumer *consumer, MessageCallBack pCallback); -ROCKETMQCLIENT_API int UnregisterMessageCallbackOrderly(CPushConsumer *consumer); -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 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); - +typedef enum E_CConsumeStatus { E_CONSUME_SUCCESS = 0, E_RECONSUME_LATER = 1 } CConsumeStatus; + +typedef int (*MessageCallBack)(CPushConsumer*, CMessageExt*); + +ROCKETMQCLIENT_API CPushConsumer* CreatePushConsumer(const char* groupId); +ROCKETMQCLIENT_API int DestroyPushConsumer(CPushConsumer* consumer); +ROCKETMQCLIENT_API int StartPushConsumer(CPushConsumer* consumer); +ROCKETMQCLIENT_API int ShutdownPushConsumer(CPushConsumer* consumer); +ROCKETMQCLIENT_API int SetPushConsumerGroupID(CPushConsumer* consumer, const char* groupId); +ROCKETMQCLIENT_API const char* GetPushConsumerGroupID(CPushConsumer* consumer); +ROCKETMQCLIENT_API int SetPushConsumerNameServerAddress(CPushConsumer* consumer, const char* namesrv); +ROCKETMQCLIENT_API int SetPushConsumerNameServerDomain(CPushConsumer* consumer, const char* domain); +ROCKETMQCLIENT_API int Subscribe(CPushConsumer* consumer, const char* topic, const char* expression); +ROCKETMQCLIENT_API int RegisterMessageCallbackOrderly(CPushConsumer* consumer, MessageCallBack pCallback); +ROCKETMQCLIENT_API int RegisterMessageCallback(CPushConsumer* consumer, MessageCallBack pCallback); +ROCKETMQCLIENT_API int UnregisterMessageCallbackOrderly(CPushConsumer* consumer); +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 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..fa640ca6a 100644 --- a/include/CSendResult.h +++ b/include/CSendResult.h @@ -23,20 +23,20 @@ extern "C" { #endif -typedef enum E_CSendStatus_{ - E_SEND_OK = 0, - E_SEND_FLUSH_DISK_TIMEOUT = 1, - E_SEND_FLUSH_SLAVE_TIMEOUT = 2, - E_SEND_SLAVE_NOT_AVAILABLE = 3 +typedef enum E_CSendStatus_ { + E_SEND_OK = 0, + E_SEND_FLUSH_DISK_TIMEOUT = 1, + E_SEND_FLUSH_SLAVE_TIMEOUT = 2, + E_SEND_SLAVE_NOT_AVAILABLE = 3 } CSendStatus; typedef struct _SendResult_ { - CSendStatus sendStatus; - char msgId[MAX_MESSAGE_ID_LENGTH]; - long long offset; + CSendStatus sendStatus; + 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..c404a3af9 --- a/include/ConsumeType.h +++ b/include/ConsumeType.h @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + #ifndef __CONSUMETYPE_H__ #define __CONSUMETYPE_H__ @@ -28,8 +28,8 @@ enum ConsumeType { //& 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 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); + virtual void sendOneway(MQMessage& msg, MessageQueueSelector* selector, void* arg); //& msgs); @@ -104,10 +107,10 @@ class ROCKETMQCLIENT_API DefaultMQProducer : public MQProducer { int m_sendMsgTimeout; int m_compressMsgBodyOverHowmuch; int m_maxMessageSize; //& mqs); + virtual void fetchSubscribeMessageQueues(const std::string& topic, std::vector& mqs); virtual void doRebalance(); virtual void persistConsumerOffset(); virtual void persistConsumerOffsetByResetOffset(); - virtual void updateTopicSubscribeInfo(const std::string& topic, - std::vector& info); + virtual void updateTopicSubscribeInfo(const std::string& topic, std::vector& info); virtual ConsumeType getConsumeType(); virtual ConsumeFromWhere getConsumeFromWhere(); virtual void getSubscriptions(std::vector&); @@ -60,8 +58,7 @@ class ROCKETMQCLIENT_API DefaultMQPullConsumer : public MQConsumer { virtual Rebalance* getRebalance() const; // mqs); + void fetchMessageQueuesInBalance(const std::string& topic, std::vector mqs); // temp persist consumer offset interface, only valid with // RemoteBrokerOffsetStore, updateConsumeOffset should be called before. @@ -133,11 +131,17 @@ class ROCKETMQCLIENT_API DefaultMQPullConsumer : public MQConsumer { 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, + void pullAsyncImpl(const MQMessageQueue& mq, + const std::string& subExpression, + int64 offset, + int maxNums, + bool block, PullCallback* pPullCallback); void subscriptionAutomatically(const std::string& topic); diff --git a/include/DefaultMQPushConsumer.h b/include/DefaultMQPushConsumer.h old mode 100755 new mode 100644 index 4051c772b..e1292bf8e --- a/include/DefaultMQPushConsumer.h +++ b/include/DefaultMQPushConsumer.h @@ -56,25 +56,24 @@ class ROCKETMQCLIENT_API DefaultMQPushConsumer : public MQConsumer { //& mqs); + virtual void fetchSubscribeMessageQueues(const std::string& topic, std::vector& mqs); virtual void doRebalance(); virtual void persistConsumerOffset(); virtual void persistConsumerOffsetByResetOffset(); - virtual void updateTopicSubscribeInfo(const std::string& topic, - std::vector& info); + 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) { + 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, + virtual void pull(const MQMessageQueue& mq, + const std::string& subExpression, + int64 offset, + int maxNums, PullCallback* pPullCallback) {} virtual ConsumerRunningInfo* getConsumerRunningInfo(); // getTopicMessageQueueInfo(const std::string& topic); @@ -131,8 +130,7 @@ class ROCKETMQCLIENT_API MQClient { // 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 + 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, diff --git a/include/MQClientException.h b/include/MQClientException.h old mode 100755 new mode 100644 index 85749f157..ad642cb7f --- a/include/MQClientException.h +++ b/include/MQClientException.h @@ -28,54 +28,54 @@ namespace rocketmq { // line:" << line; - m_msg = ss.str(); - } catch (...) { - } + public: + MQException(const std::string& msg, int error, const char* file, int line) throw() + : m_error(error), m_line(line), m_file(file) { + try { + std::stringstream ss; + ss << "msg: " << msg << ",error:" << error << ",in file <" << file << "> line:" << line; + m_msg = ss.str(); + } catch (...) { } + } - MQException(const std::string &msg, int error, const char *file, const char *type, int line) throw() - : m_error(error), m_line(line), m_file(file), m_type(type) { - try { - std::stringstream ss; - ss << "msg: " << msg << ",error:" << error << ",in file <" << file << "> line:" << line; - m_msg = ss.str(); - } catch (...) { - } + MQException(const std::string& msg, int error, const char* file, const char* type, int line) throw() + : m_error(error), m_line(line), m_file(file), m_type(type) { + try { + std::stringstream ss; + ss << "msg: " << msg << ",error:" << error << ",in file <" << file << "> line:" << line; + m_msg = ss.str(); + } catch (...) { } + } - virtual ~MQException() throw() {} - const char *what() const throw() { return m_msg.c_str(); } - int GetError() const throw() { return m_error; } - virtual const char *GetType() const throw() { return m_type.c_str(); } - int GetLine() { return m_line; } - const char *GetFile() { return m_file.c_str(); } + virtual ~MQException() throw() {} + const char* what() const throw() { return m_msg.c_str(); } + int GetError() const throw() { return m_error; } + virtual const char* GetType() const throw() { return m_type.c_str(); } + int GetLine() { return m_line; } + const char* GetFile() { return m_file.c_str(); } - protected: - int m_error; - int m_line; - std::string m_msg; - std::string m_file; - std::string m_type; + protected: + int m_error; + int m_line; + std::string m_msg; + std::string m_file; + std::string m_type; }; -inline std::ostream &operator<<(std::ostream &os, const MQException &e) { - os << "Type: " << e.GetType() << " , " << e.what(); - return os; +inline std::ostream& operator<<(std::ostream& os, const MQException& e) { + os << "Type: " << e.GetType() << " , " << e.what(); + return os; } -#define DEFINE_MQCLIENTEXCEPTION(name) \ - class ROCKETMQCLIENT_API name : public MQException { \ - 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(); } \ - }; +#define DEFINE_MQCLIENTEXCEPTION(name) \ + class ROCKETMQCLIENT_API name : public MQException { \ + 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(); } \ + }; DEFINE_MQCLIENTEXCEPTION(MQClientException) DEFINE_MQCLIENTEXCEPTION(MQBrokerException) diff --git a/include/MQConsumer.h b/include/MQConsumer.h old mode 100755 new mode 100644 index 89763a854..99505197e --- a/include/MQConsumer.h +++ b/include/MQConsumer.h @@ -33,33 +33,29 @@ class ROCKETMQCLIENT_API MQConsumer : public MQClient { public: virtual ~MQConsumer() {} virtual void sendMessageBack(MQMessageExt& msg, int delayLevel) = 0; - virtual void fetchSubscribeMessageQueues(const std::string& topic, - std::vector& mqs) = 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 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, + 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; - } + void setMessageModel(MessageModel messageModel) { m_messageModel = messageModel; } protected: MessageModel m_messageModel; diff --git a/include/MQMessage.h b/include/MQMessage.h old mode 100755 new mode 100644 index e6f229886..e4a6b5e53 --- a/include/MQMessage.h +++ b/include/MQMessage.h @@ -26,31 +26,33 @@ namespace rocketmq { //& keys); @@ -66,7 +68,7 @@ class ROCKETMQCLIENT_API MQMessage { int getSysFlag() const; void setSysFlag(int sysFlag); - const std::string &getBody() const; + const std::string& getBody() const; void setBody(const char* body, int len); void setBody(const std::string& body); @@ -77,8 +79,7 @@ class ROCKETMQCLIENT_API MQMessage { const std::string toString() const { std::stringstream ss; std::string tags = getTags(); - ss << "Message [topic=" << m_topic << ", flag=" << m_flag - << ", tag=" << tags << "]"; + ss << "Message [topic=" << m_topic << ", flag=" << m_flag << ", tag=" << tags << "]"; return ss.str(); } @@ -87,8 +88,12 @@ class ROCKETMQCLIENT_API MQMessage { 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); + 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; @@ -102,7 +107,7 @@ class ROCKETMQCLIENT_API MQMessage { 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; diff --git a/include/MQMessageExt.h b/include/MQMessageExt.h old mode 100755 new mode 100644 index fb48daeff..c5d60e294 --- a/include/MQMessageExt.h +++ b/include/MQMessageExt.h @@ -33,8 +33,12 @@ namespace rocketmq { class ROCKETMQCLIENT_API MQMessageExt : public MQMessage { public: MQMessageExt(); - MQMessageExt(int queueId, int64 bornTimestamp, sockaddr bornHost, - int64 storeTimestamp, sockaddr storeHost, std::string msgId); + MQMessageExt(int queueId, + int64 bornTimestamp, + sockaddr bornHost, + int64 storeTimestamp, + sockaddr storeHost, + std::string msgId); virtual ~MQMessageExt(); @@ -84,16 +88,12 @@ class ROCKETMQCLIENT_API MQMessageExt : public MQMessage { std::string toString() const { std::stringstream ss; - ss << "MessageExt [queueId=" << m_queueId << ", storeSize=" << m_storeSize - << ", queueOffset=" << m_queueOffset << ", sysFlag=" << m_sysFlag - << ", bornTimestamp=" << m_bornTimestamp - << ", bornHost=" << getBornHostString() - << ", storeTimestamp=" << m_storeTimestamp - << ", storeHost=" << getStoreHostString() << ", msgId=" << m_msgId + ss << "MessageExt [queueId=" << m_queueId << ", storeSize=" << m_storeSize << ", queueOffset=" << m_queueOffset + << ", sysFlag=" << m_sysFlag << ", bornTimestamp=" << m_bornTimestamp << ", bornHost=" << getBornHostString() + << ", storeTimestamp=" << m_storeTimestamp << ", storeHost=" << getStoreHostString() << ", msgId=" << m_msgId << ", commitLogOffset=" << m_commitLogOffset << ", bodyCRC=" << m_bodyCRC - << ", reconsumeTimes=" << m_reconsumeTimes - << ", preparedTransactionOffset=" << m_preparedTransactionOffset << ", " - << MQMessage::toString() << "]"; + << ", reconsumeTimes=" << m_reconsumeTimes << ", preparedTransactionOffset=" << m_preparedTransactionOffset + << ", " << MQMessage::toString() << "]"; return ss.str(); } diff --git a/include/MQMessageListener.h b/include/MQMessageListener.h old mode 100755 new mode 100644 index 63bd74d6a..84fdb48d3 --- a/include/MQMessageListener.h +++ b/include/MQMessageListener.h @@ -24,9 +24,9 @@ namespace rocketmq { //& msgs) = 0; - virtual MessageListenerType getMessageListenerType() { - return messageListenerDefaultly; - } + 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; - } + virtual MessageListenerType getMessageListenerType() { return messageListenerOrderly; } }; -class ROCKETMQCLIENT_API MessageListenerConcurrently - : public MQMessageListener { +class ROCKETMQCLIENT_API MessageListenerConcurrently : public MQMessageListener { public: virtual ~MessageListenerConcurrently() {} virtual ConsumeStatus consumeMessage(const std::vector& msgs) = 0; - virtual MessageListenerType getMessageListenerType() { - return messageListenerConcurrently; - } + 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; + virtual void sendOneway(MQMessage& msg, MessageQueueSelector* selector, void* arg) = 0; }; //& mqs, - const MQMessage& msg, void* arg) = 0; + virtual MQMessageQueue select(const std::vector& mqs, const MQMessage& msg, void* arg) = 0; }; //& src); virtual ~PullResult(); std::string toString() { std::stringstream ss; - ss << "PullResult [ pullStatus=" << EnumStrings[pullStatus] - << ", nextBeginOffset=" << nextBeginOffset << ", minOffset=" << minOffset - << ", maxOffset=" << maxOffset - << ", msgFoundList=" << msgFoundList.size() << " ]"; + ss << "PullResult [ pullStatus=" << EnumStrings[pullStatus] << ", nextBeginOffset=" << nextBeginOffset + << ", minOffset=" << minOffset << ", maxOffset=" << maxOffset << ", msgFoundList=" << msgFoundList.size() + << " ]"; return ss.str(); } diff --git a/include/QueryResult.h b/include/QueryResult.h old mode 100755 new mode 100644 index c9861a0a4..7b3ec089a --- a/include/QueryResult.h +++ b/include/QueryResult.h @@ -24,8 +24,7 @@ namespace rocketmq { //& messageList) { + QueryResult(uint64 indexLastUpdateTimestamp, const std::vector& messageList) { m_indexLastUpdateTimestamp = indexLastUpdateTimestamp; m_messageList = messageList; } diff --git a/include/RocketMQClient.h b/include/RocketMQClient.h old mode 100755 new mode 100644 diff --git a/include/SendMessageHook.h b/include/SendMessageHook.h old mode 100755 new mode 100644 diff --git a/include/SendResult.h b/include/SendResult.h old mode 100755 new mode 100644 index 239f07aa2..2f8883a83 --- a/include/SendResult.h +++ b/include/SendResult.h @@ -23,19 +23,17 @@ namespace rocketmq { // +#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 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..889ad7dbf --- 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,13 +51,13 @@ extern "C" { */ #ifdef __cplusplus -namespace rocketmqSignature{ +namespace rocketmqSignature { #endif -extern int hmac_sha1(const void *key, size_t key_len, const void *data, size_t data_len, void *ret_buf); -extern int hmac_sha256(const void *key, size_t key_len, const void *data, size_t data_len, void *ret_buf); -extern int hmac_sha512(const void *key, size_t key_len, const void *data, size_t data_len, void *ret_buf); +extern int hmac_sha1(const void* key, size_t key_len, const void* data, size_t data_len, void* ret_buf); +extern int hmac_sha256(const void* key, size_t key_len, const void* data, size_t data_len, void* ret_buf); +extern int hmac_sha512(const void* key, size_t key_len, const void* data, size_t data_len, void* ret_buf); #ifdef __cplusplus } @@ -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..afe87b05e --- 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 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 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); #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..789d4118c --- 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,8 +28,7 @@ namespace rocketmqSignature { #define SHA1_DIGEST_SIZE 20 /* Structure to save state of computation between the single steps. */ -struct sha1_ctx -{ +struct sha1_ctx { uint32_t A; uint32_t B; uint32_t C; @@ -41,48 +40,42 @@ struct sha1_ctx 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..1726b60f7 --- a/libs/signature/include/sha256.h +++ b/libs/signature/include/sha256.h @@ -16,18 +16,17 @@ */ #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 -{ +struct sha256_ctx { uint32_t state[8]; uint32_t total[2]; @@ -39,53 +38,49 @@ 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..38328a16c --- a/libs/signature/include/sha512.h +++ b/libs/signature/include/sha512.h @@ -16,19 +16,18 @@ */ #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 -{ +struct sha512_ctx { u64 state[8]; u64 total[2]; @@ -40,30 +39,27 @@ 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 +67,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..7bf6aae04 --- a/libs/signature/include/spas_client.h +++ b/libs/signature/include/spas_client.h @@ -28,14 +28,15 @@ 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 */ @@ -67,31 +68,29 @@ typedef struct _spas_credential { char secret_key[SPAS_MAX_KEY_LEN]; } SPAS_CREDENTIAL; -extern int spas_load_credential(char *path, CREDENTIAL_UPDATE_MODE mode); -extern int spas_set_access_key(char *key); -extern int spas_set_secret_key(char *key); -extern char *spas_get_access_key(void); -extern char *spas_get_secret_key(void); -extern SPAS_CREDENTIAL *spas_get_credential(void); +extern int spas_load_credential(char* path, CREDENTIAL_UPDATE_MODE mode); +extern int spas_set_access_key(char* key); +extern int spas_set_secret_key(char* key); +extern char* spas_get_access_key(void); +extern char* spas_get_secret_key(void); +extern SPAS_CREDENTIAL* spas_get_credential(void); #ifdef SPAS_MT -extern int spas_load_thread_credential(char *path); -extern int spas_set_thread_access_key(char *key); -extern int spas_set_thread_secret_key(char *key); -extern char *spas_get_thread_access_key(void); -extern char *spas_get_thread_secret_key(void); +extern int spas_load_thread_credential(char* path); +extern int spas_set_thread_access_key(char* key); +extern int spas_set_thread_secret_key(char* key); +extern char* spas_get_thread_access_key(void); +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_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 void spas_mem_free(char *pSignature); -extern char *spas_get_version(void); +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_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 void spas_mem_free(char* pSignature); +extern char* spas_get_version(void); #ifdef __cplusplus } diff --git a/libs/signature/include/u64.h b/libs/signature/include/u64.h index 612b04400..e7580e560 100644 --- a/libs/signature/include/u64.h +++ b/libs/signature/include/u64.h @@ -19,42 +19,42 @@ #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 +#ifdef WORDS_BIGENDIAN typedef struct { uint32_t hi, lo; } u64; -# define u64init(hi, lo) { hi, lo } -# else +#define u64init(hi, lo) \ + { hi, lo } +#else typedef struct { uint32_t lo, hi; } u64; -# define u64init(hi, lo) { lo, hi } -# endif +#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) -{ +static inline u64 u64hilo(uint32_t hi, uint32_t lo) { u64 r; r.hi = hi; r.lo = lo; @@ -62,9 +62,7 @@ u64hilo (uint32_t hi, uint32_t lo) } /* Return a u64 value representing LO. */ -static inline u64 -u64lo (uint32_t lo) -{ +static inline u64 u64lo(uint32_t lo) { u64 r; r.hi = 0; r.lo = lo; @@ -72,16 +70,12 @@ u64lo (uint32_t lo) } /* Return X < Y. */ -static inline int -u64lt (u64 x, u64 y) -{ +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) -{ +static inline u64 u64and(u64 x, u64 y) { u64 r; r.hi = x.hi & y.hi; r.lo = x.lo & y.lo; @@ -89,9 +83,7 @@ u64and (u64 x, u64 y) } /* Return X | Y. */ -static inline u64 -u64or (u64 x, u64 y) -{ +static inline u64 u64or(u64 x, u64 y) { u64 r; r.hi = x.hi | y.hi; r.lo = x.lo | y.lo; @@ -99,9 +91,7 @@ u64or (u64 x, u64 y) } /* Return X ^ Y. */ -static inline u64 -u64xor (u64 x, u64 y) -{ +static inline u64 u64xor(u64 x, u64 y) { u64 r; r.hi = x.hi ^ y.hi; r.lo = x.lo ^ y.lo; @@ -109,9 +99,7 @@ u64xor (u64 x, u64 y) } /* Return X + Y. */ -static inline u64 -u64plus (u64 x, u64 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); @@ -119,38 +107,28 @@ u64plus (u64 x, u64 y) } /* Return X << N. */ -static inline u64 -u64shl (u64 x, int 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; - } + 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; + } return r; } /* Return X >> N. */ -static inline u64 -u64shr (u64 x, int 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); - } + 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; } diff --git a/src/MQClientAPIImpl.cpp b/src/MQClientAPIImpl.cpp index 8ec426db6..8ec60449f 100644 --- a/src/MQClientAPIImpl.cpp +++ b/src/MQClientAPIImpl.cpp @@ -27,25 +27,20 @@ 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_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)); } @@ -59,9 +54,9 @@ void MQClientAPIImpl::stopAllTcpTransportThread() { m_pRemotingClient->stopAllTcpTransportThread(); } -bool MQClientAPIImpl::writeDataToFile(string filename, string data, - bool isSync) { - if (data.size() == 0) return false; +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) { @@ -76,8 +71,8 @@ bool MQClientAPIImpl::writeDataToFile(string filename, string data, 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)); + LOG_ERROR("write data fail, data len:" SIZET_FMT ", file:%s, msg:%s", data.size(), filename.c_str(), + strerror(errno)); fclose(pFd); return false; } @@ -142,12 +137,12 @@ string MQClientAPIImpl::fetchNameServerAddr(const string& NSDomain) { 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()); + 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; + if (!m_firstFetchNameSrv) + return m_nameSrvAddr; } // update the snapshot local file if nameSrv changes or // m_firstFetchNameSrv==true @@ -174,18 +169,19 @@ void MQClientAPIImpl::updateNameServerAddr(const string& addrs) { m_pRemotingClient->updateNameServerAddressList(addrs); } -void MQClientAPIImpl::callSignatureBeforeRequest( - const string& addr, RemotingCommand& request, - const SessionCredentials& session_credentials) { +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) { +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); @@ -199,8 +195,7 @@ void MQClientAPIImpl::createTopic( callSignatureBeforeRequest(addr, request, sessionCredentials); request.Encode(); - unique_ptr response( - m_pRemotingClient->invokeSync(addr, request)); + unique_ptr response(m_pRemotingClient->invokeSync(addr, request)); if (response) { switch (response->getCode()) { @@ -209,17 +204,20 @@ void MQClientAPIImpl::createTopic( default: break; } - THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), - response->getCode()); + 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) { +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()); @@ -242,9 +240,9 @@ SendResult MQClientAPIImpl::sendMessage( return SendResult(); } -void MQClientAPIImpl::sendHearbeat( - const string& addr, HeartbeatData* pHeartbeatData, - const SessionCredentials& sessionCredentials) { +void MQClientAPIImpl::sendHearbeat(const string& addr, + HeartbeatData* pHeartbeatData, + const SessionCredentials& sessionCredentials) { RemotingCommand request(HEART_BEAT, NULL); string body; @@ -259,18 +257,17 @@ void MQClientAPIImpl::sendHearbeat( } } -void MQClientAPIImpl::unregisterClient( - const string& addr, const string& clientID, const string& producerGroup, - const string& consumerGroup, const SessionCredentials& sessionCredentials) { +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)); + RemotingCommand request(UNREGISTER_CLIENT, new UnregisterClientRequestHeader(clientID, producerGroup, consumerGroup)); callSignatureBeforeRequest(addr, request, sessionCredentials); request.Encode(); - unique_ptr response( - m_pRemotingClient->invokeSync(addr, request)); + unique_ptr response(m_pRemotingClient->invokeSync(addr, request)); if (response) { switch (response->getCode()) { @@ -280,26 +277,22 @@ void MQClientAPIImpl::unregisterClient( default: break; } - LOG_WARN("unregisterClient fail:%s,%d", response->getRemark().c_str(), - response->getCode()); + 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)); +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)); + unique_ptr pResponse(m_pRemotingClient->invokeSync("", request, timeoutMillis)); if (pResponse != NULL) { - if (((*(pResponse->GetBody())).getSize() == 0) || - ((*(pResponse->GetBody())).getData() != NULL)) { + if (((*(pResponse->GetBody())).getSize() == 0) || ((*(pResponse->GetBody())).getData() != NULL)) { switch (pResponse->getCode()) { case SUCCESS_VALUE: { const MemoryBlock* pbody = pResponse->GetBody(); @@ -321,17 +314,14 @@ TopicRouteData* MQClientAPIImpl::getTopicRouteInfoFromNameServer( return NULL; } -TopicList* MQClientAPIImpl::getTopicListFromNameServer( - const SessionCredentials& sessionCredentials) { +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)); + unique_ptr pResponse(m_pRemotingClient->invokeSync("", request)); if (pResponse != NULL) { - if (((*(pResponse->GetBody())).getSize() == 0) || - ((*(pResponse->GetBody())).getData() != NULL)) { + if (((*(pResponse->GetBody())).getSize() == 0) || ((*(pResponse->GetBody())).getData() != NULL)) { switch (pResponse->getCode()) { case SUCCESS_VALUE: { const MemoryBlock* pbody = pResponse->GetBody(); @@ -344,30 +334,21 @@ TopicList* MQClientAPIImpl::getTopicListFromNameServer( break; } - THROW_MQEXCEPTION(MQClientException, pResponse->getRemark(), - pResponse->getCode()); + THROW_MQEXCEPTION(MQClientException, pResponse->getRemark(), pResponse->getCode()); } } return NULL; } -int MQClientAPIImpl::wipeWritePermOfBroker(const string& namesrvAddr, - const string& brokerName, - int timeoutMillis) { +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, @@ -375,8 +356,7 @@ string MQClientAPIImpl::getKVConfigByValue(const string& projectNamespace, return ""; } -KVTable MQClientAPIImpl::getKVListByNamespace(const string& projectNamespace, - int timeoutMillis) { +KVTable MQClientAPIImpl::getKVListByNamespace(const string& projectNamespace, int timeoutMillis) { return KVTable(); } @@ -390,12 +370,11 @@ SendResult MQClientAPIImpl::sendMessageSync(const string& addr, RemotingCommand& request, int timeoutMillis) { // pResponse( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + unique_ptr 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()); + 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 (...) { @@ -417,30 +396,30 @@ void MQClientAPIImpl::sendMessageAsync(const string& addr, //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_DEBUG("sendMessageAsync request:%s, timeout:%lld, maxRetryTimes:%d retrySendTimes:%d", request.ToString().data(), + timeoutMilliseconds, maxRetryTimes, retrySendTimes); + + if (m_pRemotingClient->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); + 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(); @@ -452,13 +431,16 @@ void MQClientAPIImpl::sendMessageAsync(const string& addr, } void MQClientAPIImpl::deleteOpaqueForDropPullRequest(const MQMessageQueue& mq, int opaque) { - m_pRemotingClient->deleteOpaqueForDropPullRequest(mq, 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) { +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(); @@ -481,7 +463,8 @@ PullResult* MQClientAPIImpl::pullMessage( void MQClientAPIImpl::pullMessageAsync(const string& addr, RemotingCommand& request, int timeoutMillis, - PullCallback* pullCallback, void* pArg) { + PullCallback* pullCallback, + void* pArg) { //pPullRequest) { mq = pAsyncArg->mq; pAsyncArg->pPullRequest->setLatestPullRequestOpaque(request.getOpaque()); - LOG_DEBUG("pullMessageAsync set opaque:%d, mq:%s", - pAsyncArg->pPullRequest->getLatestPullRequestOpaque(),mq.toString().c_str()); + 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 (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); + 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)); +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)) { + if (((*(pResponse->GetBody())).getSize() == 0) || ((*(pResponse->GetBody())).getData() != NULL)) { try { - PullResult* pullResult = - processPullResponse(pResponse.get()); // pullMessage will handle - // exception from - // processPullResponse + PullResult* pullResult = processPullResponse(pResponse.get()); // pullMessage will handle + // exception from + // processPullResponse return pullResult; } catch (MQException& e) { LOG_ERROR(e.what()); @@ -550,18 +528,14 @@ SendResult MQClientAPIImpl::processSendResponse(const string& brokerName, break; } if (res == 0) { - SendMessageResponseHeader* responseHeader = - (SendMessageResponseHeader*)pResponse->getCommandHeader(); - MQMessageQueue messageQueue(msg.getTopic(), brokerName, - responseHeader->queueId); + 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); + 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()); + 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) { @@ -580,46 +554,38 @@ PullResult* MQClientAPIImpl::processPullResponse(RemotingCommand* pResponse) { pullStatus = OFFSET_ILLEGAL; break; default: - THROW_MQEXCEPTION(MQBrokerException, pResponse->getRemark(), - pResponse->getCode()); + THROW_MQEXCEPTION(MQBrokerException, pResponse->getRemark(), pResponse->getCode()); break; } - PullMessageResponseHeader* responseHeader = - static_cast(pResponse->getCommandHeader()); + PullMessageResponseHeader* responseHeader = static_cast(pResponse->getCommandHeader()); if (!responseHeader) { LOG_ERROR("processPullResponse:responseHeader is NULL"); - THROW_MQEXCEPTION(MQClientException, - "processPullResponse:responseHeader is NULL", -1); + THROW_MQEXCEPTION(MQClientException, "processPullResponse:responseHeader is NULL", -1); } //GetBody()); // response data judgement had been done outside - // of processPullResponse + MemoryBlock bodyFromResponse = *(pResponse->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); + 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); + THROW_MQEXCEPTION(MQClientException, "memoryBody size is 0, but pullStatus equals found", -1); } } else { - return new PullResultExt( - pullStatus, responseHeader->nextBeginOffset, responseHeader->minOffset, - responseHeader->maxOffset, (int)responseHeader->suggestWhichBrokerId, - bodyFromResponse); + return new PullResultExt(pullStatus, responseHeader->nextBeginOffset, responseHeader->minOffset, + responseHeader->maxOffset, (int)responseHeader->suggestWhichBrokerId, bodyFromResponse); } } //topic = topic; pRequestHeader->queueId = queueId; @@ -628,14 +594,12 @@ int64 MQClientAPIImpl::getMinOffset( callSignatureBeforeRequest(addr, request, sessionCredentials); request.Encode(); - unique_ptr response( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + unique_ptr response(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); if (response) { switch (response->getCode()) { case SUCCESS_VALUE: { - GetMinOffsetResponseHeader* responseHeader = - (GetMinOffsetResponseHeader*)response->getCommandHeader(); + GetMinOffsetResponseHeader* responseHeader = (GetMinOffsetResponseHeader*)response->getCommandHeader(); int64 offset = responseHeader->offset; return offset; @@ -643,15 +607,16 @@ int64 MQClientAPIImpl::getMinOffset( default: break; } - THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), - response->getCode()); + 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) { +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; @@ -660,14 +625,12 @@ int64 MQClientAPIImpl::getMaxOffset( callSignatureBeforeRequest(addr, request, sessionCredentials); request.Encode(); - unique_ptr response( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + unique_ptr response(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); if (response) { switch (response->getCode()) { case SUCCESS_VALUE: { - GetMaxOffsetResponseHeader* responseHeader = - (GetMaxOffsetResponseHeader*)response->getCommandHeader(); + GetMaxOffsetResponseHeader* responseHeader = (GetMaxOffsetResponseHeader*)response->getCommandHeader(); int64 offset = responseHeader->offset; return offset; @@ -675,15 +638,17 @@ int64 MQClientAPIImpl::getMaxOffset( default: break; } - THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), - response->getCode()); + 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) { +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; @@ -693,14 +658,12 @@ int64 MQClientAPIImpl::searchOffset( callSignatureBeforeRequest(addr, request, sessionCredentials); request.Encode(); - unique_ptr response( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + unique_ptr response(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); if (response) { switch (response->getCode()) { case SUCCESS_VALUE: { - SearchOffsetResponseHeader* responseHeader = - (SearchOffsetResponseHeader*)response->getCommandHeader(); + SearchOffsetResponseHeader* responseHeader = (SearchOffsetResponseHeader*)response->getCommandHeader(); int64 offset = responseHeader->offset; return offset; @@ -708,15 +671,15 @@ int64 MQClientAPIImpl::searchOffset( default: break; } - THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), - response->getCode()); + 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) { +MQMessageExt* MQClientAPIImpl::viewMessage(const string& addr, + int64 phyoffset, + int timeoutMillis, + const SessionCredentials& sessionCredentials) { ViewMessageRequestHeader* pRequestHeader = new ViewMessageRequestHeader(); pRequestHeader->offset = phyoffset; @@ -724,8 +687,7 @@ MQMessageExt* MQClientAPIImpl::viewMessage( callSignatureBeforeRequest(addr, request, sessionCredentials); request.Encode(); - unique_ptr response( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + unique_ptr response(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); if (response) { switch (response->getCode()) { @@ -734,17 +696,17 @@ MQMessageExt* MQClientAPIImpl::viewMessage( default: break; } - THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), - response->getCode()); + 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(); +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; @@ -752,15 +714,13 @@ int64 MQClientAPIImpl::getEarliestMsgStoretime( callSignatureBeforeRequest(addr, request, sessionCredentials); request.Encode(); - unique_ptr response( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + unique_ptr response(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); if (response) { switch (response->getCode()) { case SUCCESS_VALUE: { GetEarliestMsgStoretimeResponseHeader* responseHeader = - (GetEarliestMsgStoretimeResponseHeader*) - response->getCommandHeader(); + (GetEarliestMsgStoretimeResponseHeader*)response->getCommandHeader(); int64 timestamp = responseHeader->timestamp; return timestamp; @@ -768,29 +728,27 @@ int64 MQClientAPIImpl::getEarliestMsgStoretime( default: break; } - THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), - response->getCode()); + 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(); +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)); + unique_ptr pResponse(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); if (pResponse != NULL) { - if ((pResponse->GetBody()->getSize() == 0) || - (pResponse->GetBody()->getData() != NULL)) { + if ((pResponse->GetBody()->getSize() == 0) || (pResponse->GetBody()->getData() != NULL)) { switch (pResponse->getCode()) { case SUCCESS_VALUE: { const MemoryBlock* pbody = pResponse->GetBody(); @@ -802,22 +760,21 @@ void MQClientAPIImpl::getConsumerIdListByGroup( default: break; } - THROW_MQEXCEPTION(MQBrokerException, pResponse->getRemark(), - pResponse->getCode()); + 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) { +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)); + unique_ptr response(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); if (response) { switch (response->getCode()) { @@ -830,22 +787,21 @@ int64 MQClientAPIImpl::queryConsumerOffset( default: break; } - THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), - response->getCode()); + 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) { +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)); + unique_ptr response(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); if (response) { switch (response->getCode()) { @@ -855,15 +811,15 @@ void MQClientAPIImpl::updateConsumerOffset( default: break; } - THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), - response->getCode()); + 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) { +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(); @@ -871,11 +827,12 @@ void MQClientAPIImpl::updateConsumerOffsetOneway( m_pRemotingClient->invokeOneway(addr, request); } -void MQClientAPIImpl::consumerSendMessageBack( - MQMessageExt& msg, const string& consumerGroup, int delayLevel, - int timeoutMillis, const SessionCredentials& sessionCredentials) { - ConsumerSendMsgBackRequestHeader* pRequestHeader = - new ConsumerSendMsgBackRequestHeader(); +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; @@ -885,8 +842,7 @@ void MQClientAPIImpl::consumerSendMessageBack( callSignatureBeforeRequest(addr, request, sessionCredentials); request.Encode(); - unique_ptr response( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + unique_ptr response(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); if (response) { switch (response->getCode()) { @@ -896,16 +852,16 @@ void MQClientAPIImpl::consumerSendMessageBack( default: break; } - THROW_MQEXCEPTION(MQBrokerException, response->getRemark(), - response->getCode()); + 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) { +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); @@ -914,12 +870,10 @@ void MQClientAPIImpl::lockBatchMQ( callSignatureBeforeRequest(addr, request, sessionCredentials); request.Encode(); - unique_ptr pResponse( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + unique_ptr pResponse(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); if (pResponse != NULL) { - if (((*(pResponse->GetBody())).getSize() == 0) || - ((*(pResponse->GetBody())).getData() != NULL)) { + if (((*(pResponse->GetBody())).getSize() == 0) || ((*(pResponse->GetBody())).getData() != NULL)) { switch (pResponse->getCode()) { case SUCCESS_VALUE: { const MemoryBlock* pbody = pResponse->GetBody(); @@ -931,16 +885,16 @@ void MQClientAPIImpl::lockBatchMQ( default: break; } - THROW_MQEXCEPTION(MQBrokerException, pResponse->getRemark(), - pResponse->getCode()); + 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) { +void MQClientAPIImpl::unlockBatchMQ(const string& addr, + UnlockBatchRequestBody* requestBody, + int timeoutMillis, + const SessionCredentials& sessionCredentials) { RemotingCommand request(UNLOCK_BATCH_MQ, NULL); string body; requestBody->Encode(body); @@ -949,8 +903,7 @@ void MQClientAPIImpl::unlockBatchMQ( callSignatureBeforeRequest(addr, request, sessionCredentials); request.Encode(); - unique_ptr pResponse( - m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); + unique_ptr pResponse(m_pRemotingClient->invokeSync(addr, request, timeoutMillis)); if (pResponse != NULL) { switch (pResponse->getCode()) { @@ -960,8 +913,7 @@ void MQClientAPIImpl::unlockBatchMQ( default: break; } - THROW_MQEXCEPTION(MQBrokerException, pResponse->getRemark(), - pResponse->getCode()); + THROW_MQEXCEPTION(MQBrokerException, pResponse->getRemark(), pResponse->getCode()); } THROW_MQEXCEPTION(MQBrokerException, "response is null", -1); } diff --git a/src/MQClientAPIImpl.h b/src/MQClientAPIImpl.h index f2ac27361..1a5e202c8 100644 --- a/src/MQClientAPIImpl.h +++ b/src/MQClientAPIImpl.h @@ -41,99 +41,110 @@ namespace rocketmq { //& cids, int timeoutMillis, + void getConsumerIdListByGroup(const string& addr, + const string& consumerGroup, + vector& cids, + int timeoutMillis, const SessionCredentials& sessionCredentials); int64 queryConsumerOffset(const string& addr, @@ -146,43 +157,55 @@ class MQClientAPIImpl { int timeoutMillis, const SessionCredentials& sessionCredentials); - void updateConsumerOffsetOneway( - 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, + 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, + void lockBatchMQ(const string& addr, + LockBatchRequestBody* requestBody, + vector& mqs, + int timeoutMillis, const SessionCredentials& sessionCredentials); - void unlockBatchMQ(const string& addr, UnlockBatchRequestBody* requestBody, + 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 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, + 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); + PullResult* pullMessageSync(const string& addr, RemotingCommand& request, int timeoutMillis); - void pullMessageAsync(const string& addr, RemotingCommand& request, - int timeoutMillis, PullCallback* pullCallback, + void pullMessageAsync(const string& addr, + RemotingCommand& request, + int timeoutMillis, + PullCallback* pullCallback, void* pArg); private: diff --git a/src/MQClientFactory.cpp b/src/MQClientFactory.cpp index 2dd7bdbe8..66cc2431e 100644 --- a/src/MQClientFactory.cpp +++ b/src/MQClientFactory.cpp @@ -28,7 +28,8 @@ namespace rocketmq { // 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_pClientAPIImpl.reset(new MQClientAPIImpl(m_clientId, m_pClientRemotingProcessor.get(), pullThreadNum, + tcpConnectTimeout, tcpTransportTryLockTimeout, unitName)); m_serviceState = CREATE_JUST; LOG_DEBUG("MQClientFactory construct"); } @@ -48,8 +48,7 @@ MQClientFactory::MQClientFactory(const string& clientID, int pullThreadNum, MQClientFactory::~MQClientFactory() { LOG_INFO("MQClientFactory:%s destruct", m_clientId.c_str()); - for (TRDMAP::iterator itp = m_topicRouteTable.begin(); - itp != m_topicRouteTable.end(); ++itp) { + for (TRDMAP::iterator itp = m_topicRouteTable.begin(); itp != m_topicRouteTable.end(); ++itp) { delete itp->second; } @@ -68,23 +67,21 @@ void MQClientFactory::start() { 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)); + 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) { @@ -121,8 +116,7 @@ TopicRouteData* MQClientFactory::getTopicRouteData(const string& topic) { return NULL; } -void MQClientFactory::addTopicRouteData(const string& topic, - TopicRouteData* 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]; @@ -132,12 +126,12 @@ void MQClientFactory::addTopicRouteData(const string& topic, } 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; + 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); } @@ -156,16 +150,16 @@ boost::shared_ptr MQClientFactory::tryToFindTopicPublishInfo( return getTopicPublishInfoFromTable(topic); } -bool MQClientFactory::updateTopicRouteInfoFromNameServer( - const string& topic, const SessionCredentials& session_credentials, - bool isDefault /* = false */) { +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)); + pTopicRouteData.reset( + m_pClientAPIImpl->getTopicRouteInfoFromNameServer(DEFAULT_TOPIC, 1000 * 5, session_credentials)); if (pTopicRouteData != NULL) { vector& queueDatas = pTopicRouteData->getQueueDatas(); vector::iterator it = queueDatas.begin(); @@ -177,8 +171,7 @@ bool MQClientFactory::updateTopicRouteInfoFromNameServer( } } } else { - pTopicRouteData.reset(m_pClientAPIImpl->getTopicRouteInfoFromNameServer( - topic, 1000 * 5, session_credentials)); + pTopicRouteData.reset(m_pClientAPIImpl->getTopicRouteInfoFromNameServer(topic, 1000 * 5, session_credentials)); } if (pTopicRouteData != NULL) { @@ -201,16 +194,13 @@ bool MQClientFactory::updateTopicRouteInfoFromNameServer( vector 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()); + LOG_INFO("updateTopicRouteInfoFromNameServer changed with broker name:%s", (*it).brokerName.c_str()); addBrokerToAddrMap((*it).brokerName, (*it).brokerAddrs); } // publishInfo( - topicRouteData2TopicPublishInfo(topic, pTopicRouteData.get())); + boost::shared_ptr publishInfo(topicRouteData2TopicPublishInfo(topic, pTopicRouteData.get())); addTopicInfoToTable(topic, publishInfo); // erase first, then add } @@ -224,9 +214,8 @@ bool MQClientFactory::updateTopicRouteInfoFromNameServer( return false; } -boost::shared_ptr -MQClientFactory::topicRouteData2TopicPublishInfo(const string& topic, - TopicRouteData* pRoute) { +boost::shared_ptr MQClientFactory::topicRouteData2TopicPublishInfo(const string& topic, + TopicRouteData* pRoute) { boost::shared_ptr info(new TopicPublishInfo()); string OrderTopicConf = pRoute->getOrderTopicConf(); //& mqs) { +void MQClientFactory::topicRouteData2TopicSubscribeInfo(const string& topic, + TopicRouteData* pRoute, + vector& mqs) { mqs.clear(); vector& queueDatas = pRoute->getQueueDatas(); vector::iterator it = queueDatas.begin(); @@ -282,9 +272,11 @@ void MQClientFactory::topicRouteData2TopicSubscribeInfo( } void MQClientFactory::shutdown() { - if (getConsumerTableSize() != 0) return; + if (getConsumerTableSize() != 0) + return; - if (getProducerTableSize() != 0) return; + if (getProducerTableSize() != 0) + return; switch (m_serviceState) { case RUNNING: { @@ -332,9 +324,9 @@ bool MQClientFactory::registerProducer(MQProducer* pProducer) { //getNamesrvDomain()); - if (!nameSrvDomain.empty()) m_nameSrvDomain = nameSrvDomain; - pProducer->setNamesrvAddr( - m_pClientAPIImpl->fetchNameServerAddr(m_nameSrvDomain)); + if (!nameSrvDomain.empty()) + m_nameSrvDomain = nameSrvDomain; + pProducer->setNamesrvAddr(m_pClientAPIImpl->fetchNameServerAddr(m_nameSrvDomain)); } else { m_bFetchNSService = false; m_pClientAPIImpl->updateNameServerAddr(namesrvaddr); @@ -364,9 +356,9 @@ bool MQClientFactory::registerConsumer(MQConsumer* pConsumer) { //getNamesrvDomain()); - if (!nameSrvDomain.empty()) m_nameSrvDomain = nameSrvDomain; - pConsumer->setNamesrvAddr( - m_pClientAPIImpl->fetchNameServerAddr(m_nameSrvDomain)); + if (!nameSrvDomain.empty()) + m_nameSrvDomain = nameSrvDomain; + pConsumer->setNamesrvAddr(m_pClientAPIImpl->fetchNameServerAddr(m_nameSrvDomain)); } else { m_bFetchNSService = false; m_pClientAPIImpl->updateNameServerAddr(namesrvaddr); @@ -391,23 +383,23 @@ MQProducer* MQClientFactory::selectProducer(const string& producerName) { return NULL; } -bool MQClientFactory::getSessionCredentialFromProducerTable( - SessionCredentials& sessionCredentials) { +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(); + 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; } -bool MQClientFactory::addProducerToTable(const string& producerName, - MQProducer* pMQProducer) { +bool MQClientFactory::addProducerToTable(const string& producerName, MQProducer* pMQProducer) { boost::lock_guard lock(m_producerTableMutex); - if (m_producerTable.find(producerName) != m_producerTable.end()) return false; + if (m_producerTable.find(producerName) != m_producerTable.end()) + return false; m_producerTable[producerName] = pMQProducer; return true; } @@ -423,11 +415,9 @@ int MQClientFactory::getProducerTableSize() { return m_producerTable.size(); } -void MQClientFactory::insertProducerInfoToHeartBeatData( - HeartbeatData* pHeartbeatData) { +void MQClientFactory::insertProducerInfoToHeartBeatData(HeartbeatData* pHeartbeatData) { boost::lock_guard lock(m_producerTableMutex); - for (MQPMAP::iterator it = m_producerTable.begin(); - it != m_producerTable.end(); ++it) { + for (MQPMAP::iterator it = m_producerTable.begin(); it != m_producerTable.end(); ++it) { ProducerData producerData; producerData.groupName = it->first; pHeartbeatData->insertDataToProducerDataSet(producerData); @@ -442,36 +432,36 @@ MQConsumer* MQClientFactory::selectConsumer(const string& group) { return NULL; } -bool MQClientFactory::getSessionCredentialFromConsumerTable( - SessionCredentials& sessionCredentials) { +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(); + 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; } -bool MQClientFactory::getSessionCredentialFromConsumer( - const string& consumerGroup, SessionCredentials& sessionCredentials) { +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(); + sessionCredentials = m_consumerTable[consumerGroup]->getSessionCredentials(); } - if (sessionCredentials.isValid()) return true; + if (sessionCredentials.isValid()) + return true; return false; } -bool MQClientFactory::addConsumerToTable(const string& consumerName, - MQConsumer* pMQConsumer) { +bool MQClientFactory::addConsumerToTable(const string& consumerName, MQConsumer* pMQConsumer) { boost::lock_guard lock(m_consumerTableMutex); - if (m_consumerTable.find(consumerName) != m_consumerTable.end()) return false; + if (m_consumerTable.find(consumerName) != m_consumerTable.end()) + return false; m_consumerTable[consumerName] = pMQConsumer; return true; } @@ -490,11 +480,9 @@ int MQClientFactory::getConsumerTableSize() { return m_consumerTable.size(); } -void MQClientFactory::getTopicListFromConsumerSubscription( - set& topicList) { +void MQClientFactory::getTopicListFromConsumerSubscription(set& topicList) { boost::lock_guard lock(m_consumerTableMutex); - for (MQCMAP::iterator it = m_consumerTable.begin(); - it != m_consumerTable.end(); ++it) { + for (MQCMAP::iterator it = m_consumerTable.begin(); it != m_consumerTable.end(); ++it) { vector result; it->second->getSubscriptions(result); @@ -505,20 +493,16 @@ void MQClientFactory::getTopicListFromConsumerSubscription( } } -void MQClientFactory::updateConsumerSubscribeTopicInfo( - const string& topic, vector 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) { + for (MQCMAP::iterator it = m_consumerTable.begin(); it != m_consumerTable.end(); ++it) { it->second->updateTopicSubscribeInfo(topic, mqs); } } -void MQClientFactory::insertConsumerInfoToHeartBeatData( - HeartbeatData* pHeartbeatData) { +void MQClientFactory::insertConsumerInfoToHeartBeatData(HeartbeatData* pHeartbeatData) { boost::lock_guard lock(m_consumerTableMutex); - for (MQCMAP::iterator it = m_consumerTable.begin(); - it != m_consumerTable.end(); ++it) { + for (MQCMAP::iterator it = m_consumerTable.begin(); it != m_consumerTable.end(); ++it) { MQConsumer* pConsumer = it->second; ConsumerData consumerData; consumerData.groupName = pConsumer->getGroupName(); @@ -535,9 +519,7 @@ void MQClientFactory::insertConsumerInfoToHeartBeatData( } } -void MQClientFactory::addTopicInfoToTable( - const string& topic, - boost::shared_ptr 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); @@ -555,13 +537,13 @@ void MQClientFactory::eraseTopicInfoFromTable(const string& 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; + if (m_topicPublishInfoTable[topic]->ok()) + return true; } return false; } -boost::shared_ptr -MQClientFactory::getTopicPublishInfoFromTable(const string& topic) { +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]; @@ -572,8 +554,7 @@ MQClientFactory::getTopicPublishInfoFromTable(const string& topic) { void MQClientFactory::getTopicListFromTopicPublishInfo(set& topicList) { boost::lock_guard lock(m_topicPublishInfoTableMutex); - for (TPMap::iterator itp = m_topicPublishInfoTable.begin(); - itp != m_topicPublishInfoTable.end(); ++itp) { + for (TPMap::iterator itp = m_topicPublishInfoTable.begin(); itp != m_topicPublishInfoTable.end(); ++itp) { topicList.insert(itp->first); } } @@ -583,8 +564,7 @@ void MQClientFactory::clearBrokerAddrMap() { m_brokerAddrTable.clear(); } -void MQClientFactory::addBrokerToAddrMap(const string& brokerName, - map& 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); @@ -626,47 +606,47 @@ string MQClientFactory::findBrokerAddressInPublish(const string& brokerName) { } brokerTable.clear(); - if (found) return brokerAddr; + if (found) + return brokerAddr; return ""; } -FindBrokerResult *MQClientFactory::findBrokerAddressInSubscribe(const string &brokerName, +FindBrokerResult* MQClientFactory::findBrokerAddressInSubscribe(const string& brokerName, int brokerId, bool onlyThisBroker) { - string brokerAddr; - bool slave = false; - bool found = false; - BrokerAddrMAP brokerTable(getBrokerAddrMap()); - - if (brokerTable.find(brokerName) != brokerTable.end()) { - map brokerMap(brokerTable[brokerName]); - if (!brokerMap.empty()) { - auto iter = brokerMap.find(brokerId); - if (iter != brokerMap.end()) { - brokerAddr = iter->second; - slave = (brokerId != MASTER_ID); - found = true; - } else if (!onlyThisBroker) { // not only from master - iter = brokerMap.begin(); - brokerAddr = iter->second; - slave = iter->first != MASTER_ID; - found = true; - } - } + string brokerAddr; + bool slave = false; + bool found = false; + BrokerAddrMAP brokerTable(getBrokerAddrMap()); + + if (brokerTable.find(brokerName) != brokerTable.end()) { + map brokerMap(brokerTable[brokerName]); + if (!brokerMap.empty()) { + auto iter = brokerMap.find(brokerId); + if (iter != brokerMap.end()) { + brokerAddr = iter->second; + slave = (brokerId != MASTER_ID); + found = true; + } else if (!onlyThisBroker) { // not only from master + iter = brokerMap.begin(); + brokerAddr = iter->second; + slave = iter->first != MASTER_ID; + found = true; + } } + } - brokerTable.clear(); + brokerTable.clear(); - if (found) { - return new FindBrokerResult(brokerAddr, slave); - } + if (found) { + return new FindBrokerResult(brokerAddr, slave); + } - return nullptr; + return nullptr; } -FindBrokerResult* MQClientFactory::findBrokerAddressInAdmin( - const string& brokerName) { +FindBrokerResult* MQClientFactory::findBrokerAddressInAdmin(const string& brokerName) { BrokerAddrMAP brokerTable(getBrokerAddrMap()); bool found = false; bool slave = false; @@ -683,7 +663,8 @@ FindBrokerResult* MQClientFactory::findBrokerAddressInAdmin( } brokerTable.clear(); - if (found) return new FindBrokerResult(brokerAddr, slave); + if (found) + return new FindBrokerResult(brokerAddr, slave); return NULL; } @@ -710,17 +691,16 @@ void MQClientFactory::sendHeartbeatToAllBroker() { SessionCredentials session_credentials; getSessionCredentialsFromOneOfProducerOrConsumer(session_credentials); - for (BrokerAddrMAP::iterator it = brokerTable.begin(); - it != brokerTable.end(); ++it) { + 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; + if (consumerEmpty && it1->first != MASTER_ID) + continue; try { - m_pClientAPIImpl->sendHearbeat(addr, heartbeatData.get(), - session_credentials); + m_pClientAPIImpl->sendHearbeat(addr, heartbeatData.get(), session_credentials); } catch (MQException& e) { LOG_ERROR(e.what()); } @@ -729,13 +709,11 @@ void MQClientFactory::sendHeartbeatToAllBroker() { brokerTable.clear(); } -void MQClientFactory::persistAllConsumerOffset(boost::system::error_code& ec, - boost::asio::deadline_timer* t) { +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) { + for (MQCMAP::iterator it = m_consumerTable.begin(); it != m_consumerTable.end(); ++it) { LOG_DEBUG("Client factory start persistAllConsumerOffset"); it->second->persistConsumerOffset(); } @@ -744,8 +722,7 @@ void MQClientFactory::persistAllConsumerOffset(boost::system::error_code& ec, 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)); + t->async_wait(boost::bind(&MQClientFactory::persistAllConsumerOffset, this, ec, t)); } HeartbeatData* MQClientFactory::prepareHeartbeatData() { @@ -762,26 +739,20 @@ HeartbeatData* MQClientFactory::prepareHeartbeatData() { return pHeartbeatData; } -void MQClientFactory::timerCB_sendHeartbeatToAllBroker( - boost::system::error_code& ec, boost::asio::deadline_timer* t) { +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)); + 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) { +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)); + 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) { @@ -791,23 +762,18 @@ void MQClientFactory::startScheduledTask(bool startFetchNSService) { // 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::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)); + 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)); + 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()); @@ -819,54 +785,44 @@ 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.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 + 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::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::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) { +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)); + 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) { + for (MQCMAP::iterator it = m_consumerTable.begin(); it != m_consumerTable.end(); ++it) { it->second->doRebalance(); } } @@ -876,33 +832,30 @@ void MQClientFactory::doRebalance() { 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()); + 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) { +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) { + 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); + m_pClientAPIImpl->unregisterClient(addr, m_clientId, producerGroup, consumerGroup, sessionCredentials); } } } //& mqs, - const SessionCredentials& sessionCredentials) { +void MQClientFactory::fetchSubscribeMessageQueues(const string& topic, + vector& mqs, + const SessionCredentials& sessionCredentials) { TopicRouteData* pTopicRouteData = getTopicRouteData(topic); if (pTopicRouteData == NULL) { updateTopicRouteInfoFromNameServer(topic, sessionCredentials); @@ -919,12 +872,12 @@ void MQClientFactory::fetchSubscribeMessageQueues( } //getMinOffset(brokerAddr, mq.getTopic(), - mq.getQueueId(), 1000 * 3, - sessionCredentials); + return m_pClientAPIImpl->getMinOffset(brokerAddr, mq.getTopic(), mq.getQueueId(), 1000 * 3, sessionCredentials); } catch (MQException& e) { LOG_ERROR(e.what()); } @@ -943,8 +894,7 @@ int64 MQClientFactory::minOffset(const MQMessageQueue& mq, THROW_MQEXCEPTION(MQClientException, "The broker is not exist", -1); } -int64 MQClientFactory::maxOffset(const MQMessageQueue& mq, - const SessionCredentials& sessionCredentials) { +int64 MQClientFactory::maxOffset(const MQMessageQueue& mq, const SessionCredentials& sessionCredentials) { string brokerAddr = findBrokerAddressInPublish(mq.getBrokerName()); if (brokerAddr.empty()) { updateTopicRouteInfoFromNameServer(mq.getTopic(), sessionCredentials); @@ -953,9 +903,7 @@ int64 MQClientFactory::maxOffset(const MQMessageQueue& mq, if (!brokerAddr.empty()) { try { - return m_pClientAPIImpl->getMaxOffset(brokerAddr, mq.getTopic(), - mq.getQueueId(), 1000 * 3, - sessionCredentials); + return m_pClientAPIImpl->getMaxOffset(brokerAddr, mq.getTopic(), mq.getQueueId(), 1000 * 3, sessionCredentials); } catch (MQException& e) { THROW_MQEXCEPTION(MQClientException, "Invoke Broker exception", -1); } @@ -963,9 +911,9 @@ int64 MQClientFactory::maxOffset(const MQMessageQueue& mq, THROW_MQEXCEPTION(MQClientException, "The broker is not exist", -1); } -int64 MQClientFactory::searchOffset( - const MQMessageQueue& mq, int64 timestamp, - const SessionCredentials& sessionCredentials) { +int64 MQClientFactory::searchOffset(const MQMessageQueue& mq, + int64 timestamp, + const SessionCredentials& sessionCredentials) { string brokerAddr = findBrokerAddressInPublish(mq.getBrokerName()); if (brokerAddr.empty()) { updateTopicRouteInfoFromNameServer(mq.getTopic(), sessionCredentials); @@ -974,9 +922,8 @@ int64 MQClientFactory::searchOffset( if (!brokerAddr.empty()) { try { - return m_pClientAPIImpl->searchOffset(brokerAddr, mq.getTopic(), - mq.getQueueId(), timestamp, - 1000 * 3, sessionCredentials); + return m_pClientAPIImpl->searchOffset(brokerAddr, mq.getTopic(), mq.getQueueId(), timestamp, 1000 * 3, + sessionCredentials); } catch (MQException& e) { THROW_MQEXCEPTION(MQClientException, "Invoke Broker exception", -1); } @@ -984,8 +931,7 @@ int64 MQClientFactory::searchOffset( THROW_MQEXCEPTION(MQClientException, "The broker is not exist", -1); } -MQMessageExt* MQClientFactory::viewMessage( - const string& msgId, const SessionCredentials& sessionCredentials) { +MQMessageExt* MQClientFactory::viewMessage(const string& msgId, const SessionCredentials& sessionCredentials) { try { return NULL; } catch (MQException& e) { @@ -993,8 +939,7 @@ MQMessageExt* MQClientFactory::viewMessage( } } -int64 MQClientFactory::earliestMsgStoreTime( - const MQMessageQueue& mq, const SessionCredentials& sessionCredentials) { +int64 MQClientFactory::earliestMsgStoreTime(const MQMessageQueue& mq, const SessionCredentials& sessionCredentials) { string brokerAddr = findBrokerAddressInPublish(mq.getBrokerName()); if (brokerAddr.empty()) { updateTopicRouteInfoFromNameServer(mq.getTopic(), sessionCredentials); @@ -1003,9 +948,8 @@ int64 MQClientFactory::earliestMsgStoreTime( if (!brokerAddr.empty()) { try { - return m_pClientAPIImpl->getEarliestMsgStoretime( - brokerAddr, mq.getTopic(), mq.getQueueId(), 1000 * 3, - sessionCredentials); + return m_pClientAPIImpl->getEarliestMsgStoretime(brokerAddr, mq.getTopic(), mq.getQueueId(), 1000 * 3, + sessionCredentials); } catch (MQException& e) { THROW_MQEXCEPTION(MQClientException, "Invoke Broker exception", -1); } @@ -1013,15 +957,19 @@ int64 MQClientFactory::earliestMsgStoreTime( 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) { +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) { +void MQClientFactory::findConsumerIds(const string& topic, + const string& group, + vector& cids, + const SessionCredentials& sessionCredentials) { string brokerAddr; TopicRouteData* pTopicRouteData = getTopicRouteData(topic); if (pTopicRouteData == NULL) { @@ -1035,8 +983,7 @@ void MQClientFactory::findConsumerIds( if (!brokerAddr.empty()) { try { LOG_INFO("getConsumerIdList from broker:%s", brokerAddr.c_str()); - return m_pClientAPIImpl->getConsumerIdListByGroup( - brokerAddr, group, cids, 5000, sessionCredentials); + return m_pClientAPIImpl->getConsumerIdListByGroup(brokerAddr, group, cids, 5000, sessionCredentials); } catch (MQException& e) { LOG_ERROR(e.what()); } @@ -1044,19 +991,20 @@ void MQClientFactory::findConsumerIds( } void MQClientFactory::removeDropedPullRequestOpaque(PullRequest* pullRequest) { - //delete the opaque record that's ignore the response of this pullrequest when drop pullrequest - if (!pullRequest) return; + // 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); + 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) { +void MQClientFactory::resetOffset(const string& group, + const string& topic, + const map& offsetTable) { MQConsumer* pConsumer = selectConsumer(group); if (pConsumer) { map::const_iterator it = offsetTable.begin(); @@ -1066,14 +1014,14 @@ void MQClientFactory::resetOffset( 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); + 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()); + LOG_ERROR("no corresponding pullRequest found for topic:%s", topic.c_str()); } } @@ -1091,8 +1039,7 @@ void MQClientFactory::resetOffset( 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()); + LOG_DEBUG("resetOffset sets to:%lld for mq:%s", it->second, mq.toString().c_str()); pConsumer->updateConsumeOffset(mq, it->second); } } @@ -1113,35 +1060,28 @@ void MQClientFactory::resetOffset( } } -ConsumerRunningInfo* MQClientFactory::consumerRunningInfo( - const string& consumerGroup) { +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()); + runningInfo->setProperty(ConsumerRunningInfo::PROP_NAMESERVER_ADDR, pConsumer->getNamesrvAddr()); if (pConsumer->getConsumeType() == CONSUME_PASSIVELY) { - runningInfo->setProperty(ConsumerRunningInfo::PROP_CONSUME_TYPE, - "CONSUME_PASSIVELY"); + runningInfo->setProperty(ConsumerRunningInfo::PROP_CONSUME_TYPE, "CONSUME_PASSIVELY"); } else { - runningInfo->setProperty(ConsumerRunningInfo::PROP_CONSUME_TYPE, - "CONSUME_ACTIVELY"); + runningInfo->setProperty(ConsumerRunningInfo::PROP_CONSUME_TYPE, "CONSUME_ACTIVELY"); } - runningInfo->setProperty(ConsumerRunningInfo::PROP_CLIENT_VERSION, - "V3_1_8"); // MQVersion::s_CurrentVersion )); + 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()); + LOG_ERROR("no corresponding consumer found for group:%s", consumerGroup.c_str()); return NULL; } -void MQClientFactory::getSessionCredentialsFromOneOfProducerOrConsumer( - SessionCredentials& session_credentials) { +void MQClientFactory::getSessionCredentialsFromOneOfProducerOrConsumer(SessionCredentials& session_credentials) { // Note: on the same MQClientFactory, all producers and consumers used the // same // sessionCredentials, diff --git a/src/MQClientFactory.h b/src/MQClientFactory.h index 7da2e2113..e5d6200fc 100644 --- a/src/MQClientFactory.h +++ b/src/MQClientFactory.h @@ -40,9 +40,11 @@ namespace rocketmq { class TopicPublishInfo; class MQClientFactory { public: - MQClientFactory(const string& clientID, int pullThreadNum, + MQClientFactory(const string& clientID, + int pullThreadNum, uint64_t tcpConnectTimeout, - uint64_t tcpTransportTryLockTimeout, string unitName); + uint64_t tcpTransportTryLockTimeout, + string unitName); virtual ~MQClientFactory(); void start(); @@ -52,68 +54,62 @@ class MQClientFactory { bool registerConsumer(MQConsumer* pConsumer); void unregisterConsumer(MQConsumer* pConsumer); - void createTopic(const string& key, const string& newTopic, int queueNum, + void createTopic(const string& key, + const string& newTopic, + int queueNum, const SessionCredentials& session_credentials); - int64 minOffset(const MQMessageQueue& mq, - const SessionCredentials& session_credentials); - int64 maxOffset(const MQMessageQueue& mq, - const SessionCredentials& session_credentials); - int64 searchOffset(const MQMessageQueue& mq, int64 timestamp, - const SessionCredentials& session_credentials); - int64 earliestMsgStoreTime(const MQMessageQueue& mq, - const SessionCredentials& session_credentials); - MQMessageExt* viewMessage(const string& msgId, - const SessionCredentials& session_credentials); - QueryResult queryMessage(const string& topic, const string& key, int maxNum, - int64 begin, int64 end, + int64 minOffset(const MQMessageQueue& mq, const SessionCredentials& session_credentials); + int64 maxOffset(const MQMessageQueue& mq, const SessionCredentials& session_credentials); + int64 searchOffset(const MQMessageQueue& mq, int64 timestamp, const SessionCredentials& session_credentials); + int64 earliestMsgStoreTime(const MQMessageQueue& mq, const SessionCredentials& session_credentials); + MQMessageExt* viewMessage(const string& msgId, const SessionCredentials& session_credentials); + QueryResult queryMessage(const string& topic, + const string& key, + int maxNum, + int64 begin, + int64 end, const SessionCredentials& session_credentials); MQClientAPIImpl* getMQClientAPIImpl() const; MQProducer* selectProducer(const string& group); MQConsumer* selectConsumer(const string& group); - boost::shared_ptr topicRouteData2TopicPublishInfo( - const string& topic, TopicRouteData* pRoute); + boost::shared_ptr topicRouteData2TopicPublishInfo(const string& topic, TopicRouteData* pRoute); - void topicRouteData2TopicSubscribeInfo(const string& topic, - TopicRouteData* pRoute, - vector& mqs); + void topicRouteData2TopicSubscribeInfo(const string& topic, TopicRouteData* pRoute, vector& mqs); - FindBrokerResult* findBrokerAddressInSubscribe(const string& brokerName, - int brokerId, - bool onlyThisBroker); + 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); + boost::shared_ptr tryToFindTopicPublishInfo(const string& topic, + const SessionCredentials& session_credentials); - void fetchSubscribeMessageQueues( - const string& topic, vector& mqs, - 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); + 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, + 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); + 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); + 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, @@ -124,50 +120,38 @@ class MQClientFactory { void startScheduledTask(bool startFetchNSService = true); //& topicList); - void updateConsumerSubscribeTopicInfo(const string& topic, - vector mqs); + void updateConsumerSubscribeTopicInfo(const string& topic, vector mqs); void insertConsumerInfoToHeartBeatData(HeartbeatData* pHeartbeatData); // producer related operation - bool getSessionCredentialFromProducerTable( - SessionCredentials& sessionCredentials); + 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 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); + boost::shared_ptr getTopicPublishInfoFromTable(const string& topic); void getTopicListFromTopicPublishInfo(set& topicList); - void getSessionCredentialsFromOneOfProducerOrConsumer( - SessionCredentials& session_credentials); + void getSessionCredentialsFromOneOfProducerOrConsumer(SessionCredentials& session_credentials); private: string m_clientId; diff --git a/src/MQClientManager.cpp b/src/MQClientManager.cpp old mode 100755 new mode 100644 index 79ef77e34..2a658c802 --- a/src/MQClientManager.cpp +++ b/src/MQClientManager.cpp @@ -21,23 +21,26 @@ namespace rocketmq { //second; } else { MQClientFactory* factory = - new MQClientFactory(clientId, pullThreadNum, tcpConnectTimeout, - tcpTransportTryLockTimeout, unitName); + new MQClientFactory(clientId, pullThreadNum, tcpConnectTimeout, tcpTransportTryLockTimeout, unitName); m_factoryTable[clientId] = factory; return factory; } diff --git a/src/MQClientManager.h b/src/MQClientManager.h old mode 100755 new mode 100644 index 7703c158f..fc253d776 --- a/src/MQClientManager.h +++ b/src/MQClientManager.h @@ -27,7 +27,8 @@ namespace rocketmq { class MQClientManager { public: virtual ~MQClientManager(); - MQClientFactory* getMQClientFactory(const string& clientId, int pullThreadNum, + MQClientFactory* getMQClientFactory(const string& clientId, + int pullThreadNum, uint64_t tcpConnectTimeout, uint64_t tcpTransportTryLockTimeout, string unitName); diff --git a/src/common/Arg_helper.cpp b/src/common/Arg_helper.cpp old mode 100755 new mode 100644 diff --git a/src/common/AsyncArg.h b/src/common/AsyncArg.h old mode 100755 new mode 100644 diff --git a/src/common/AsyncCallbackWrap.cpp b/src/common/AsyncCallbackWrap.cpp old mode 100755 new mode 100644 index b0ffa047d..52e037470 --- a/src/common/AsyncCallbackWrap.cpp +++ b/src/common/AsyncCallbackWrap.cpp @@ -27,8 +27,7 @@ namespace rocketmq { //(m_pAsyncCallBack); if (pCallback) { - unique_ptr exception(new MQException( - "send msg failed due to wait response timeout or network error", -1, - __FILE__, __LINE__)); + 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); @@ -60,8 +57,7 @@ void SendCallbackWrap::onException() { } } -void SendCallbackWrap::operationComplete(ResponseFuture* pResponseFuture, - bool bProducePullRequest) { +void SendCallbackWrap::operationComplete(ResponseFuture* pResponseFuture, bool bProducePullRequest) { unique_ptr pResponse(pResponseFuture->getCommand()); if (m_pAsyncCallBack == NULL) { @@ -86,44 +82,46 @@ void SendCallbackWrap::operationComplete(ResponseFuture* pResponseFuture, 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); - } + 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", - 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; - } - - if (exception_flag == false) { - return; //send retry again, here need return - } + 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; } - - if (pCallback) { - MQException exception("process send response error", -1, __FILE__, - __LINE__); - pCallback->onException(exception); + + 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 && pCallback->getSendCallbackType() == autoDeleteSendCallback) { @@ -132,8 +130,7 @@ void SendCallbackWrap::operationComplete(ResponseFuture* pResponseFuture, } //(pArg); } @@ -141,7 +138,8 @@ PullCallbackWarp::PullCallbackWarp(AsyncCallback* pAsyncCallback, PullCallbackWarp::~PullCallbackWarp() {} void PullCallbackWarp::onException() { - if (m_pAsyncCallBack == NULL) return; + if (m_pAsyncCallBack == NULL) + return; PullCallback* pCallback = static_cast(m_pAsyncCallBack); if (pCallback) { @@ -152,8 +150,7 @@ void PullCallbackWarp::onException() { } } -void PullCallbackWarp::operationComplete(ResponseFuture* pResponseFuture, - bool bProducePullRequest) { +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"); @@ -170,16 +167,14 @@ void PullCallbackWarp::operationComplete(ResponseFuture* pResponseFuture, 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); + 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); + 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 { @@ -188,7 +183,8 @@ void PullCallbackWarp::operationComplete(ResponseFuture* pResponseFuture, } catch (MQException& e) { LOG_ERROR(e.what()); MQException exception("pullResult error", -1, __FILE__, __LINE__); - if (pCallback && bProducePullRequest) pCallback->onException(exception); + if (pCallback && bProducePullRequest) + pCallback->onException(exception); } } } diff --git a/src/common/AsyncCallbackWrap.h b/src/common/AsyncCallbackWrap.h old mode 100755 new mode 100644 index 02ae07fc8..9b202a8f2 --- a/src/common/AsyncCallbackWrap.h +++ b/src/common/AsyncCallbackWrap.h @@ -30,18 +30,13 @@ class MQClientAPIImpl; class DefaultMQProducer; class SendMessageRequestHeader; //> 32)); } -#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); } +#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); } @@ -139,14 +150,28 @@ inline uint64 ByteOrder::bigEndianInt64(const void* const bytes) { inline uint16 ByteOrder::bigEndianShort(const void* const bytes) { return swap(*static_cast(bytes)); } -inline bool ByteOrder::isBigEndian() { return false; } +inline bool ByteOrder::isBigEndian() { + return false; +} #else -inline uint16 ByteOrder::swapIfBigEndian(const uint16 v) { return swap(v); } -inline uint32 ByteOrder::swapIfBigEndian(const uint32 v) { return swap(v); } -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 uint16 ByteOrder::swapIfBigEndian(const uint16 v) { + return swap(v); +} +inline uint32 ByteOrder::swapIfBigEndian(const uint32 v) { + return swap(v); +} +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)); } @@ -165,27 +190,25 @@ inline uint64 ByteOrder::bigEndianInt64(const void* const bytes) { inline uint16 ByteOrder::bigEndianShort(const void* const bytes) { return *static_cast(bytes); } -inline bool ByteOrder::isBigEndian() { return true; } +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) | + 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) | + 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) { +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) { +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; diff --git a/src/common/ClientRPCHook.cpp b/src/common/ClientRPCHook.cpp old mode 100755 new mode 100644 index 54c21dd98..cd216d10b --- a/src/common/ClientRPCHook.cpp +++ b/src/common/ClientRPCHook.cpp @@ -31,51 +31,42 @@ const string SessionCredentials::Signature = "Signature"; const string SessionCredentials::SignatureMethod = "SignatureMethod"; const string SessionCredentials::ONSChannelKey = "OnsChannel"; -void ClientRPCHook::doBeforeRequest(const string& remoteAddr, - RemotingCommand& request) { +void ClientRPCHook::doBeforeRequest(const string& remoteAddr, RemotingCommand& request) { CommandHeader* header = request.getCommandHeader(); 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()); + 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("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()); + 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()); + 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()); + 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()); + 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..72e2f7619 --- a/src/common/ClientRPCHook.h +++ b/src/common/ClientRPCHook.h @@ -25,10 +25,8 @@ class RPCHook { public: RPCHook() {} virtual ~RPCHook() {} - virtual void doBeforeRequest(const string& remoteAddr, - RemotingCommand& request) = 0; - virtual void doAfterResponse(RemotingCommand& request, - RemotingCommand& response) = 0; + virtual void doBeforeRequest(const string& remoteAddr, RemotingCommand& request) = 0; + virtual void doAfterResponse(RemotingCommand& request, RemotingCommand& response) = 0; }; class ClientRPCHook : public RPCHook { @@ -36,15 +34,12 @@ class ClientRPCHook : public RPCHook { SessionCredentials sessionCredentials; public: - ClientRPCHook(const SessionCredentials& session_credentials) - : sessionCredentials(session_credentials) {} + 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..6ba947ab6 --- a/src/common/CommunicationMode.h +++ b/src/common/CommunicationMode.h @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + #ifndef __COMMUNICATIONMODE_H__ #define __COMMUNICATIONMODE_H__ diff --git a/src/common/FilterAPI.h b/src/common/FilterAPI.h old mode 100755 new mode 100644 index c95f17e45..d632bc232 --- a/src/common/FilterAPI.h +++ b/src/common/FilterAPI.h @@ -26,8 +26,7 @@ namespace rocketmq { // @@ -23,7 +23,8 @@ namespace rocketmq { int64 InputStream::getNumBytesRemaining() { int64 len = getTotalLength(); - if (len >= 0) len -= getPosition(); + if (len >= 0) + len -= getPosition(); return len; } @@ -34,7 +35,9 @@ char InputStream::readByte() { return temp; } -bool InputStream::readBool() { return readByte() != 0; } +bool InputStream::readBool() { + return readByte() != 0; +} short InputStream::readShortBigEndian() { char temp[2]; @@ -100,8 +103,7 @@ void InputStream::skipNextBytes(int64 numBytesToSkip) { char* temp = static_cast(std::malloc(skipBufferSize * sizeof(char))); while (numBytesToSkip > 0 && !isExhausted()) - numBytesToSkip -= - read(temp, (int)std::min(numBytesToSkip, (int64)skipBufferSize)); + numBytesToSkip -= read(temp, (int)std::min(numBytesToSkip, (int64)skipBufferSize)); std::free(temp); } diff --git a/src/common/InputStream.h b/src/common/InputStream.h old mode 100755 new mode 100644 index d3916c481..8c6ddb51a --- a/src/common/InputStream.h +++ b/src/common/InputStream.h @@ -156,8 +156,7 @@ class ROCKETMQCLIENT_API InputStream { 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); + virtual size_t readIntoMemoryBlock(MemoryBlock& destBlock, size_t maxNumBytesToRead = -1); //============================================================================== /** Returns the offset of the next byte that will be read from the stream. diff --git a/src/common/MQClient.cpp b/src/common/MQClient.cpp index 152968994..6cc451c4f 100644 --- a/src/common/MQClient.cpp +++ b/src/common/MQClient.cpp @@ -27,13 +27,12 @@ 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) { + } catch (MQException& e) { LOG_ERROR(e.what()); } } -int64 MQClient::earliestMsgStoreTime(const MQMessageQueue &mq) { +int64 MQClient::earliestMsgStoreTime(const MQMessageQueue& mq) { 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) { +int64 MQClient::minOffset(const MQMessageQueue& mq) { return getFactory()->minOffset(mq, m_SessionCredentials); } -int64 MQClient::maxOffset(const MQMessageQueue &mq) { +int64 MQClient::maxOffset(const MQMessageQueue& mq) { return getFactory()->maxOffset(mq, m_SessionCredentials); } -int64 MQClient::searchOffset(const MQMessageQueue &mq, uint64_t timestamp) { +int64 MQClient::searchOffset(const MQMessageQueue& mq, uint64_t timestamp) { return getFactory()->searchOffset(mq, timestamp, m_SessionCredentials); } -MQMessageExt *MQClient::viewMessage(const string &msgId) { +MQMessageExt* MQClient::viewMessage(const string& msgId) { return getFactory()->viewMessage(msgId, m_SessionCredentials); } -vector MQClient::getTopicMessageQueueInfo(const string &topic) { +vector MQClient::getTopicMessageQueueInfo(const string& topic) { boost::weak_ptr weak_topicPublishInfo( getFactory()->tryToFindTopicPublishInfo(topic, m_SessionCredentials)); - boost::shared_ptr topicPublishInfo( - weak_topicPublishInfo.lock()); + boost::shared_ptr topicPublishInfo(weak_topicPublishInfo.lock()); if (topicPublishInfo) { return topicPublishInfo->getMessageQueueList(); } - THROW_MQEXCEPTION( - MQClientException, - "could not find MessageQueue Info of topic: [" + topic + "].", -1); + 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); + 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()); + getGroupName().c_str(), getMQClientId().c_str(), getInstanceName().c_str(), getNamesrvAddr().c_str()); } -void MQClient::shutdown() { m_clientFactory = NULL; } +void MQClient::shutdown() { + m_clientFactory = NULL; +} -MQClientFactory *MQClient::getFactory() const { return m_clientFactory; } +MQClientFactory* MQClient::getFactory() const { + return m_clientFactory; +} -bool MQClient::isServiceStateOk() { return m_serviceState == RUNNING; } +bool MQClient::isServiceStateOk() { + return m_serviceState == RUNNING; +} void MQClient::setLogLevel(elogLevel inputLevel) { ALOG_ADAPTER->setLogLevel(inputLevel); } -elogLevel MQClient::getLogLevel() { return ALOG_ADAPTER->getLogLevel(); } +elogLevel MQClient::getLogLevel() { + return ALOG_ADAPTER->getLogLevel(); +} void MQClient::setLogFileSizeAndNum(int fileNum, long perFileSize) { ALOG_ADAPTER->setLogFileNumAndSize(fileNum, perFileSize); @@ -184,18 +191,22 @@ 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::setUnitName(string unitName) { + m_unitName = unitName; +} +const string& MQClient::getUnitName() { + return m_unitName; +} -void MQClient::setSessionCredentials(const string &input_accessKey, - const string &input_secretKey, - const string &input_onsChannel) { +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); } -const SessionCredentials &MQClient::getSessionCredentials() const { +const SessionCredentials& MQClient::getSessionCredentials() const { return m_SessionCredentials; } diff --git a/src/common/MQVersion.cpp b/src/common/MQVersion.cpp old mode 100755 new mode 100644 diff --git a/src/common/MQVersion.h b/src/common/MQVersion.h old mode 100755 new mode 100644 diff --git a/src/common/MemoryInputStream.cpp b/src/common/MemoryInputStream.cpp old mode 100755 new mode 100644 index c3585bb41..3d6599246 --- a/src/common/MemoryInputStream.cpp +++ b/src/common/MemoryInputStream.cpp @@ -1,18 +1,18 @@ -/* -* 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 "MemoryInputStream.h" @@ -20,20 +20,15 @@ namespace rocketmq { MemoryInputStream::MemoryInputStream(const void* const sourceData, const size_t sourceDataSize, const bool keepInternalCopy) - : data(sourceData), - dataSize(sourceDataSize), - position(0), - internalCopy(NULL) { - if (keepInternalCopy) createInternalCopy(); + : data(sourceData), dataSize(sourceDataSize), position(0), internalCopy(NULL) { + if (keepInternalCopy) + createInternalCopy(); } -MemoryInputStream::MemoryInputStream(const MemoryBlock& sourceData, - const bool keepInternalCopy) - : data(sourceData.getData()), - dataSize(sourceData.getSize()), - position(0), - internalCopy(NULL) { - if (keepInternalCopy) createInternalCopy(); +MemoryInputStream::MemoryInputStream(const MemoryBlock& sourceData, const bool keepInternalCopy) + : data(sourceData.getData()), dataSize(sourceData.getSize()), position(0), internalCopy(NULL) { + if (keepInternalCopy) + createInternalCopy(); } void MemoryInputStream::createInternalCopy() { @@ -43,20 +38,27 @@ void MemoryInputStream::createInternalCopy() { data = internalCopy; } -MemoryInputStream::~MemoryInputStream() { std::free(internalCopy); } +MemoryInputStream::~MemoryInputStream() { + std::free(internalCopy); +} -int64 MemoryInputStream::getTotalLength() { return (int64)dataSize; } +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; + if (num <= 0) + return 0; memcpy((char*)buffer, (char*)data + position, (size_t)num); position += (unsigned int)num; return num; } -bool MemoryInputStream::isExhausted() { return position >= dataSize; } +bool MemoryInputStream::isExhausted() { + return position >= dataSize; +} bool MemoryInputStream::setPosition(const int64 pos) { if (pos < 0) @@ -67,5 +69,7 @@ bool MemoryInputStream::setPosition(const int64 pos) { return true; } -int64 MemoryInputStream::getPosition() { return (int64)position; } +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..53584b9a3 --- 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 @@ -48,8 +48,7 @@ class ROCKETMQCLIENT_API MemoryInputStream : public InputStream { the data and use that. */ - MemoryInputStream(const void* sourceData, size_t sourceDataSize, - bool keepInternalCopyOfData); + MemoryInputStream(const void* sourceData, size_t sourceDataSize, bool keepInternalCopyOfData); /** Creates a MemoryInputStream. diff --git a/src/common/MemoryOutputStream.cpp b/src/common/MemoryOutputStream.cpp old mode 100755 new mode 100644 index ce74d8e67..9578c8a35 --- a/src/common/MemoryOutputStream.cpp +++ b/src/common/MemoryOutputStream.cpp @@ -1,52 +1,43 @@ -/* -* 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) { + : 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) { +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(); } +MemoryOutputStream::~MemoryOutputStream() { + trimExternalBlockSize(); +} -void MemoryOutputStream::flush() { trimExternalBlockSize(); } +void MemoryOutputStream::flush() { + trimExternalBlockSize(); +} void MemoryOutputStream::trimExternalBlockSize() { if (blockToUse != &internalBlock && blockToUse != NULL) @@ -54,7 +45,8 @@ void MemoryOutputStream::trimExternalBlockSize() { } void MemoryOutputStream::preallocate(const size_t bytesToPreallocate) { - if (blockToUse != NULL) blockToUse->ensureSize(bytesToPreallocate + 1); + if (blockToUse != NULL) + blockToUse->ensureSize(bytesToPreallocate + 1); } void MemoryOutputStream::reset() { @@ -69,14 +61,12 @@ char* MemoryOutputStream::prepareToWrite(size_t numBytes) { if (blockToUse != NULL) { if (storageNeeded >= (unsigned int)(blockToUse->getSize())) - blockToUse->ensureSize( - (storageNeeded + std::min(storageNeeded / 2, (size_t)(1024 * 1024)) + - 32) & - ~31u); + blockToUse->ensureSize((storageNeeded + std::min(storageNeeded / 2, (size_t)(1024 * 1024)) + 32) & ~31u); data = static_cast(blockToUse->getData()); } else { - if (storageNeeded > availableSize) return NULL; + if (storageNeeded > availableSize) + return NULL; data = static_cast(externalData); } @@ -88,7 +78,8 @@ char* MemoryOutputStream::prepareToWrite(size_t numBytes) { } 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); @@ -99,7 +90,8 @@ bool MemoryOutputStream::write(const void* const buffer, size_t howMany) { } 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); @@ -114,7 +106,8 @@ MemoryBlock MemoryOutputStream::getMemoryBlock() const { } 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; @@ -136,8 +129,7 @@ bool MemoryOutputStream::setPosition(int64 newPosition) { return false; } -int64 MemoryOutputStream::writeFromInputStream(InputStream& source, - int64 maxNumBytesToWrite) { +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(); @@ -153,11 +145,11 @@ int64 MemoryOutputStream::writeFromInputStream(InputStream& source, return OutputStream::writeFromInputStream(source, maxNumBytesToWrite); } -OutputStream& operator<<(OutputStream& stream, - const MemoryOutputStream& streamToRead) { +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; } diff --git a/src/common/MemoryOutputStream.h b/src/common/MemoryOutputStream.h old mode 100755 new mode 100644 index 6d9098f78..4b39879b9 --- 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 @@ -57,8 +57,7 @@ class ROCKETMQCLIENT_API MemoryOutputStream : public OutputStream { the block will be cleared before use */ - MemoryOutputStream(MemoryBlock& memoryBlockToWriteTo, - bool appendToExistingBlockContent); + MemoryOutputStream(MemoryBlock& memoryBlockToWriteTo, bool appendToExistingBlockContent); /** Creates a MemoryOutputStream that will write into a user-supplied, fixed-size @@ -126,7 +125,6 @@ class ROCKETMQCLIENT_API MemoryOutputStream : public OutputStream { /** 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 diff --git a/src/common/MessageSysFlag.h b/src/common/MessageSysFlag.h old mode 100755 new mode 100644 diff --git a/src/common/NamesrvConfig.h b/src/common/NamesrvConfig.h old mode 100755 new mode 100644 index 4bf8eb16f..30bbb0e49 --- a/src/common/NamesrvConfig.h +++ b/src/common/NamesrvConfig.h @@ -24,29 +24,29 @@ namespace rocketmq { // @@ -29,11 +29,14 @@ bool OutputStream::writeBool(const bool b) { return writeByte(b ? (char)1 : (char)0); } -bool OutputStream::writeByte(char byte) { return write(&byte, 1); } +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; + if (!writeByte((char)byte)) + return false; return true; } @@ -77,18 +80,18 @@ bool OutputStream::writeDoubleBigEndian(double 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; while (numBytesToWrite > 0) { char buffer[8192]; - const int num = source.read( - buffer, (int)std::min(numBytesToWrite, (int64)sizeof(buffer))); + 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); diff --git a/src/common/OutputStream.h b/src/common/OutputStream.h old mode 100755 new mode 100644 index 30ca334fd..3792169d9 --- a/src/common/OutputStream.h +++ b/src/common/OutputStream.h @@ -141,8 +141,7 @@ class ROCKETMQCLIENT_API OutputStream { is exhausted) @returns the number of bytes written */ - virtual int64 writeFromInputStream(InputStream& source, - int64 maxNumBytesToWrite); + 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..bd36bfc05 --- a/src/common/PermName.cpp +++ b/src/common/PermName.cpp @@ -24,7 +24,9 @@ int PermName::PERM_READ = 0x1 << 2; int PermName::PERM_WRITE = 0x1 << 1; int PermName::PERM_INHERIT = 0x1 << 0; -bool PermName::isReadable(int perm) { return (perm & PERM_READ) == PERM_READ; } +bool PermName::isReadable(int perm) { + return (perm & PERM_READ) == PERM_READ; +} bool PermName::isWriteable(int perm) { return (perm & PERM_WRITE) == PERM_WRITE; diff --git a/src/common/PermName.h b/src/common/PermName.h old mode 100755 new mode 100644 diff --git a/src/common/PullSysFlag.cpp b/src/common/PullSysFlag.cpp old mode 100755 new mode 100644 index 6d684579a..a4af97aed --- a/src/common/PullSysFlag.cpp +++ b/src/common/PullSysFlag.cpp @@ -23,8 +23,7 @@ int PullSysFlag::FLAG_SUSPEND = 0x1 << 1; int PullSysFlag::FLAG_SUBSCRIPTION = 0x1 << 2; int PullSysFlag::FLAG_CLASS_FILTER = 0x1 << 3; -int PullSysFlag::buildSysFlag(bool commitOffset, bool suspend, - bool subscription, bool classFilter) { +int PullSysFlag::buildSysFlag(bool commitOffset, bool suspend, bool subscription, bool classFilter) { int flag = 0; if (commitOffset) { diff --git a/src/common/PullSysFlag.h b/src/common/PullSysFlag.h old mode 100755 new mode 100644 index c809772ec..37fcd89a4 --- a/src/common/PullSysFlag.h +++ b/src/common/PullSysFlag.h @@ -20,8 +20,7 @@ namespace rocketmq { // '9') && (*sValue != '.')) return -1; + if ((*sValue < '0' || *sValue > '9') && (*sValue != '.')) + return -1; sValue++; } return 0; @@ -39,7 +41,8 @@ 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(); + if (out.size() > 0) + m_addrs.clear(); for (size_t i = 0; i < out.size(); i++) { string addr = out[i]; UtilAll::Trim(addr); @@ -67,9 +70,8 @@ string TopAddressing::fetchNSAddr(const string& NSDomain) { 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()); + 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) { @@ -80,8 +82,7 @@ string TopAddressing::fetchNSAddr(const string& NSDomain) { updateNameServerAddressList(nameservers); } } else { - LOG_ERROR( - "fetchNSAddr with domain failed, connect failure or wrong response"); + LOG_ERROR("fetchNSAddr with domain failed, connect failure or wrong response"); } return nameservers; diff --git a/src/common/TopAddressing.h b/src/common/TopAddressing.h old mode 100755 new mode 100644 diff --git a/src/common/TopicConfig.cpp b/src/common/TopicConfig.cpp old mode 100755 new mode 100644 index e0e1b4d42..9f01199fc --- 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), @@ -51,8 +50,7 @@ TopicConfig::~TopicConfig() {} string TopicConfig::encode() { stringstream ss; - ss << m_topicName << SEPARATOR << m_readQueueNums << SEPARATOR - << m_writeQueueNums << SEPARATOR << m_perm << SEPARATOR + ss << m_topicName << SEPARATOR << m_readQueueNums << SEPARATOR << m_writeQueueNums << SEPARATOR << m_perm << SEPARATOR << m_topicFilterType; return ss.str(); @@ -73,29 +71,41 @@ bool TopicConfig::decode(const string& in) { return true; } -const string& TopicConfig::getTopicName() { return m_topicName; } +const string& TopicConfig::getTopicName() { + return m_topicName; +} void TopicConfig::setTopicName(const string& topicName) { m_topicName = topicName; } -int TopicConfig::getReadQueueNums() { return m_readQueueNums; } +int TopicConfig::getReadQueueNums() { + return m_readQueueNums; +} void TopicConfig::setReadQueueNums(int readQueueNums) { m_readQueueNums = readQueueNums; } -int TopicConfig::getWriteQueueNums() { return m_writeQueueNums; } +int TopicConfig::getWriteQueueNums() { + return m_writeQueueNums; +} void TopicConfig::setWriteQueueNums(int writeQueueNums) { m_writeQueueNums = writeQueueNums; } -int TopicConfig::getPerm() { return m_perm; } +int TopicConfig::getPerm() { + return m_perm; +} -void TopicConfig::setPerm(int perm) { m_perm = perm; } +void TopicConfig::setPerm(int perm) { + m_perm = perm; +} -TopicFilterType TopicConfig::getTopicFilterType() { return m_topicFilterType; } +TopicFilterType TopicConfig::getTopicFilterType() { + return m_topicFilterType; +} void TopicConfig::setTopicFilterType(TopicFilterType topicFilterType) { m_topicFilterType = topicFilterType; diff --git a/src/common/TopicConfig.h b/src/common/TopicConfig.h old mode 100755 new mode 100644 index 0e7c17a62..32178c97c --- a/src/common/TopicConfig.h +++ b/src/common/TopicConfig.h @@ -26,8 +26,7 @@ class TopicConfig { public: TopicConfig(); TopicConfig(const string& topicName); - TopicConfig(const string& topicName, int readQueueNums, int writeQueueNums, - int perm); + TopicConfig(const string& topicName, int readQueueNums, int writeQueueNums, int perm); ~TopicConfig(); string encode(); diff --git a/src/common/TopicFilterType.h b/src/common/TopicFilterType.h old mode 100755 new mode 100644 diff --git a/src/common/UtilAll.cpp b/src/common/UtilAll.cpp index 9f9126ba1..df3ec0b39 100644 --- a/src/common/UtilAll.cpp +++ b/src/common/UtilAll.cpp @@ -23,20 +23,20 @@ namespace rocketmq { std::string UtilAll::s_localHostName; std::string UtilAll::s_localIpAddress; -bool UtilAll::startsWith_retry(const string &topic) { +bool UtilAll::startsWith_retry(const string& topic) { return topic.find(RETRY_GROUP_TOPIC_PREFIX) == 0; } -string UtilAll::getRetryTopic(const string &consumerGroup) { +string UtilAll::getRetryTopic(const string& consumerGroup) { return RETRY_GROUP_TOPIC_PREFIX + consumerGroup; } -void UtilAll::Trim(string &str) { +void UtilAll::Trim(string& str) { str.erase(0, str.find_first_not_of(' ')); // prefixing spaces str.erase(str.find_last_not_of(' ') + 1); // surfixing spaces } -bool UtilAll::isBlank(const string &str) { +bool UtilAll::isBlank(const string& str) { if (str.empty()) { return true; } @@ -51,27 +51,19 @@ bool UtilAll::isBlank(const string &str) { } const int hex2int[256] = { - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, - -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 -}; - -uint64 UtilAll::hexstr2ull(const char *str) { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; + +uint64 UtilAll::hexstr2ull(const char* str) { uint64 num = 0; - unsigned char *ch = (unsigned char *) str; + unsigned char* ch = (unsigned char*)str; while (*ch != '\0') { num = (num << 4) + hex2int[*ch]; ch++; @@ -79,11 +71,11 @@ uint64 UtilAll::hexstr2ull(const char *str) { return num; } -int64 UtilAll::str2ll(const char *str) { +int64 UtilAll::str2ll(const char* str) { return boost::lexical_cast(str); } -string UtilAll::bytes2string(const char *bytes, int len) { +string UtilAll::bytes2string(const char* bytes, int len) { if (bytes == NULL || len <= 0) { return string(); } @@ -113,7 +105,7 @@ string UtilAll::bytes2string(const char *bytes, int len) { #endif } -bool UtilAll::SplitURL(const string &serverURL, string &addr, short &nPort) { +bool UtilAll::SplitURL(const string& serverURL, string& addr, short& nPort) { size_t pos = serverURL.find(':'); if (pos == string::npos) { return false; @@ -133,8 +125,9 @@ bool UtilAll::SplitURL(const string &serverURL, string &addr, short &nPort) { return true; } -int UtilAll::Split(vector &ret_, const string &strIn, const char sep) { - if (strIn.empty()) return 0; +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); @@ -157,9 +150,9 @@ int UtilAll::Split(vector &ret_, const string &strIn, const char sep) { } return ret_.size(); } -int UtilAll::Split(vector &ret_, const string &strIn, - const string &sep) { - if (strIn.empty()) return 0; +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); @@ -183,31 +176,33 @@ int UtilAll::Split(vector &ret_, const string &strIn, return ret_.size(); } -int32_t UtilAll::StringToInt32(const std::string &str, int32_t &out) { +int32_t UtilAll::StringToInt32(const std::string& str, int32_t& out) { out = 0; if (str.empty()) { return false; } - char *end = NULL; + 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; + 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; +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); /* Check for various possible errors */ - if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || - (errno != 0 && val == 0)) { + if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0)) { return false; } /*no digit was found Or Further characters after number*/ @@ -229,8 +224,7 @@ string UtilAll::getLocalHostName() { char name[1024]; boost::system::error_code ec; - if (boost::asio::detail::socket_ops::gethostname(name, sizeof(name), ec) != - 0) { + if (boost::asio::detail::socket_ops::gethostname(name, sizeof(name), ec) != 0) { return std::string(); } s_localHostName.append(name, strlen(name)); @@ -244,8 +238,7 @@ string UtilAll::getLocalAddress() { 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); + boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query, error); if (error) { return ""; } @@ -261,7 +254,7 @@ string UtilAll::getLocalAddress() { string UtilAll::getHomeDirectory() { #ifndef WIN32 - char *homeEnv = getenv("HOME"); + char* homeEnv = getenv("HOME"); string homeDir; if (homeEnv == NULL) { homeDir.append(getpwuid(getuid())->pw_dir); @@ -294,7 +287,7 @@ string UtilAll::getProcessName() { else buf[retval] = '\0'; - char *process_name = strrchr(buf, '/'); + char* process_name = strrchr(buf, '/'); if (process_name) { return std::string(process_name + 1); } else { @@ -309,21 +302,18 @@ string UtilAll::getProcessName() { uint64_t UtilAll::currentTimeMillis() { auto since_epoch = - std::chrono::duration_cast( - std::chrono::system_clock::now().time_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()); + 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); +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)); @@ -333,7 +323,7 @@ bool UtilAll::deflate(std::string &input, std::string &out, int level) { return true; } -bool UtilAll::inflate(std::string &input, std::string &out) { +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)); @@ -343,23 +333,23 @@ bool UtilAll::inflate(std::string &input, std::string &out) { 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; + 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)) { + 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; + 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..98fc906a2 100644 --- a/src/common/UtilAll.h +++ b/src/common/UtilAll.h @@ -76,7 +76,7 @@ const int CONNETERROR = -1; const string null = ""; template -inline void deleteAndZero(Type &pointer) { +inline void deleteAndZero(Type& pointer) { delete pointer; pointer = NULL; } @@ -90,31 +90,30 @@ inline void deleteAndZero(Type &pointer) { // - static string to_string(const T &n) { + 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 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 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 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(); @@ -125,16 +124,15 @@ class UtilAll { 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); + 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); + static bool ReplaceFile(const std::string& from_path, const std::string& to_path); private: static std::string s_localHostName; diff --git a/src/common/Validators.cpp b/src/common/Validators.cpp old mode 100755 new mode 100644 index 7484c983a..8743a20dc --- a/src/common/Validators.cpp +++ b/src/common/Validators.cpp @@ -22,8 +22,7 @@ namespace rocketmq { const string Validators::validPatternStr = "^[a-zA-Z0-9_-]+$"; const int Validators::CHARACTER_MAX_LENGTH = 255; // CHARACTER_MAX_LENGTH) { - THROW_MQEXCEPTION( - MQClientException, - "the specified topic is longer than topic max length 255.", -1); + 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); + 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; + str = "the specified topic[" + topic + "] contains illegal characters, allowing only" + validPatternStr; THROW_MQEXCEPTION(MQClientException, str.c_str(), -1); } @@ -82,15 +75,12 @@ void Validators::checkGroup(const string& group) { if (!regularExpressionMatcher(group, validPatternStr)) { string str; - str = "the specified group[" + group + - "] contains illegal characters, allowing only" + validPatternStr; + 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); + THROW_MQEXCEPTION(MQClientException, "the specified group is longer than group max length 255.", -1); } } @@ -105,8 +95,7 @@ void Validators::checkMessage(const MQMessage& msg, int maxMessageSize) { if ((int)body.length() > maxMessageSize) { char info[256]; - sprintf(info, "the message body size over max value, MAX: %d", - maxMessageSize); + sprintf(info, "the message body size over max value, MAX: %d", maxMessageSize); THROW_MQEXCEPTION(MQClientException, info, -1); } } diff --git a/src/common/Validators.h b/src/common/Validators.h old mode 100755 new mode 100644 index 419058819..cc03fb525 --- a/src/common/Validators.h +++ b/src/common/Validators.h @@ -25,10 +25,8 @@ namespace rocketmq { // end_) return false; + if (ptr_ + len > end_) + return false; ptr_ += len; return true; } bool BigEndianReader::ReadBytes(void* out, size_t len) { - if (ptr_ + len > end_) return false; + if (ptr_ + len > end_) + return false; memcpy(out, ptr_, len); ptr_ += len; return true; @@ -42,31 +43,41 @@ bool BigEndianReader::ReadBytes(void* out, size_t len) { template bool BigEndianReader::Read(T* value) { - if (ptr_ + sizeof(T) > end_) return false; + if (ptr_ + sizeof(T) > end_) + return false; ReadBigEndian(ptr_, value); ptr_ += sizeof(T); return true; } -bool BigEndianReader::ReadU8(uint8_t* value) { return Read(value); } +bool BigEndianReader::ReadU8(uint8_t* value) { + return Read(value); +} -bool BigEndianReader::ReadU16(uint16_t* value) { return Read(value); } +bool BigEndianReader::ReadU16(uint16_t* value) { + return Read(value); +} -bool BigEndianReader::ReadU32(uint32_t* value) { return Read(value); } +bool BigEndianReader::ReadU32(uint32_t* value) { + return Read(value); +} -bool BigEndianReader::ReadU64(uint64_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; + 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; + if (ptr_ + len > end_) + return false; memcpy(ptr_, buf, len); ptr_ += len; return true; @@ -74,18 +85,27 @@ bool BigEndianWriter::WriteBytes(const void* buf, size_t len) { template bool BigEndianWriter::Write(T value) { - if (ptr_ + sizeof(T) > end_) return false; + if (ptr_ + sizeof(T) > end_) + return false; WriteBigEndian(ptr_, value); ptr_ += sizeof(T); return true; } -bool BigEndianWriter::WriteU8(uint8_t value) { return Write(value); } +bool BigEndianWriter::WriteU8(uint8_t value) { + return Write(value); +} -bool BigEndianWriter::WriteU16(uint16_t value) { return Write(value); } +bool BigEndianWriter::WriteU16(uint16_t value) { + return Write(value); +} -bool BigEndianWriter::WriteU32(uint32_t value) { return Write(value); } +bool BigEndianWriter::WriteU32(uint32_t value) { + return Write(value); +} -bool BigEndianWriter::WriteU64(uint64_t value) { return Write(value); } +bool BigEndianWriter::WriteU64(uint64_t value) { + return Write(value); +} } // namespace base diff --git a/src/common/dataBlock.cpp b/src/common/dataBlock.cpp old mode 100755 new mode 100644 index 4195f2b55..daca4e4ea --- a/src/common/dataBlock.cpp +++ b/src/common/dataBlock.cpp @@ -21,40 +21,41 @@ namespace rocketmq { MemoryBlock::MemoryBlock() : size(0), data(NULL) {} -MemoryBlock::MemoryBlock(const int initialSize, const bool initialiseToZero) - : 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))); + data = static_cast(initialiseToZero ? std::calloc(initialSize, sizeof(char)) + : std::malloc(initialSize * sizeof(char))); } } -MemoryBlock::MemoryBlock(const void *const dataToInitialiseFrom, const size_t sizeInBytes) +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))); + 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) { +MemoryBlock::MemoryBlock(const MemoryBlock& other) : size(other.size), data(NULL) { if (size > 0) { - data = static_cast(std::malloc(size * sizeof(char))); + data = static_cast(std::malloc(size * sizeof(char))); memcpy(data, other.data, size); } } -MemoryBlock::MemoryBlock(MemoryBlock &&other) : size(other.size), data(other.data) { +MemoryBlock::MemoryBlock(MemoryBlock&& other) : size(other.size), data(other.data) { other.size = 0; other.data = NULL; } -MemoryBlock::~MemoryBlock() { std::free(data); } +MemoryBlock::~MemoryBlock() { + std::free(data); +} -MemoryBlock &MemoryBlock::operator=(const MemoryBlock &other) { +MemoryBlock& MemoryBlock::operator=(const MemoryBlock& other) { if (this != &other) { setSize(other.size, false); memcpy(data, other.data, size); @@ -63,7 +64,7 @@ MemoryBlock &MemoryBlock::operator=(const MemoryBlock &other) { return *this; } -MemoryBlock &MemoryBlock::operator=(MemoryBlock &&other) { +MemoryBlock& MemoryBlock::operator=(MemoryBlock&& other) { if (this != &other) { std::free(data); @@ -78,15 +79,15 @@ MemoryBlock &MemoryBlock::operator=(MemoryBlock &&other) { } //============================================================================== -bool MemoryBlock::operator==(const MemoryBlock &other) const { +bool MemoryBlock::operator==(const MemoryBlock& other) const { return matches(other.data, other.size); } -bool MemoryBlock::operator!=(const MemoryBlock &other) const { +bool MemoryBlock::operator!=(const MemoryBlock& other) const { return !operator==(other); } -bool MemoryBlock::matches(const void *dataToCompare, int dataSize) const { +bool MemoryBlock::matches(const void* dataToCompare, int dataSize) const { return size == dataSize && memcmp(data, dataToCompare, size) == 0; } @@ -98,17 +99,15 @@ void MemoryBlock::setSize(const int newSize, const bool initialiseToZero) { reset(); } else { if (data != NULL) { - data = static_cast( - data == NULL ? std::malloc(newSize * sizeof(char)) - : std::realloc(data, newSize * sizeof(char))); + 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))); + data = static_cast(initialiseToZero ? std::calloc(newSize, sizeof(char)) + : std::malloc(newSize * sizeof(char))); } size = newSize; @@ -123,15 +122,16 @@ void MemoryBlock::reset() { } 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); + memset(data, (int)value, size); } -void MemoryBlock::append(const void *const srcData, const int numBytes) { +void MemoryBlock::append(const void* const srcData, const int numBytes) { if (numBytes > 0) { const int oldSize = size; setSize(size + numBytes); @@ -139,22 +139,21 @@ void MemoryBlock::append(const void *const srcData, const int numBytes) { } } -void MemoryBlock::replaceWith(const void *const srcData, const int numBytes) { +void MemoryBlock::replaceWith(const void* const srcData, const int numBytes) { if (numBytes > 0) { setSize(numBytes); memcpy(data, srcData, numBytes); } } -void MemoryBlock::insert(const void *const srcData, const int numBytes, int insertPosition) { +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 (trailingDataSize > 0) - memmove(data + insertPosition + numBytes, data + insertPosition, - trailingDataSize); + memmove(data + insertPosition + numBytes, data + insertPosition, trailingDataSize); memcpy(data + insertPosition, srcData, numBytes); } @@ -170,36 +169,39 @@ void MemoryBlock::removeSection(const int startByte, const int numBytesToRemove) } } -void MemoryBlock::copyFrom(const void *const src, int offset, int num) { - const char *d = static_cast(src); +void MemoryBlock::copyFrom(const void* const src, int offset, int num) { + const char* d = static_cast(src); if (offset < 0) { d -= offset; - num += (size_t) -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); +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); + memset(d, 0, (size_t)-offset); d -= offset; - num -= (size_t) -offset; + num -= (size_t)-offset; offset = 0; } - if ((size_t) offset + num > (unsigned int) size) { - const int newNum = (size_t) size - (size_t) offset; + 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); + 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..4419af31d --- a/src/common/dataBlock.h +++ b/src/common/dataBlock.h @@ -46,12 +46,12 @@ class ROCKETMQCLIENT_API MemoryBlock { @param dataToInitialiseFrom some data to copy into this block @param sizeInBytes how much space to use */ - MemoryBlock(const void *dataToInitialiseFrom, size_t sizeInBytes); + MemoryBlock(const void* dataToInitialiseFrom, size_t sizeInBytes); /** Creates a copy of another memory block. */ - MemoryBlock(const MemoryBlock &); + MemoryBlock(const MemoryBlock&); - MemoryBlock(MemoryBlock &&); + MemoryBlock(MemoryBlock&&); /** Destructor. */ ~MemoryBlock(); @@ -59,22 +59,22 @@ class ROCKETMQCLIENT_API 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=(const MemoryBlock&); - MemoryBlock &operator=(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; + 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; + bool operator!=(const MemoryBlock& other) const; //============================================================================== /** Returns a void pointer to the data. @@ -82,19 +82,19 @@ class ROCKETMQCLIENT_API MemoryBlock { Note that the pointer returned will probably become invalid when the block is resized. */ - char *getData() const { return data; } + 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 { + 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; + bool matches(const void* data, int dataSize) const; //============================================================================== /** Returns the block's current allocated size, in bytes. */ @@ -145,13 +145,13 @@ class ROCKETMQCLIENT_API MemoryBlock { The data pointer must not be null. This block's size will be increased accordingly. */ - void append(const void *data, int numBytes); + 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); + 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 @@ -160,8 +160,7 @@ class ROCKETMQCLIENT_API MemoryBlock { be clipped to within the range before being used. */ - void insert(const void *dataToInsert, int numBytesToInsert, - int insertPosition); + void insert(const void* dataToInsert, int numBytesToInsert, int insertPosition); /** Chops out a section of the block. @@ -186,7 +185,7 @@ class ROCKETMQCLIENT_API MemoryBlock { it will be clipped so not to do anything nasty) */ - void copyFrom(const void *srcData, int destinationOffset, int numBytes); + void copyFrom(const void* srcData, int destinationOffset, int numBytes); /** Copies data from this MemoryBlock to a memory address. @@ -197,12 +196,12 @@ class ROCKETMQCLIENT_API MemoryBlock { limits of the memory block, zeros will be used for that portion of the data) */ - void copyTo(void *destData, int sourceOffset, int numBytes) const; + void copyTo(void* destData, int sourceOffset, int numBytes) const; private: //============================================================================== int size; - char *data; + 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..78f0292c6 --- a/src/common/sync_http_client.cpp +++ b/src/common/sync_http_client.cpp @@ -31,8 +31,7 @@ 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) { +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. @@ -49,8 +48,7 @@ void check_deadline(deadline_timer* deadline, tcp::socket* socket, } // Put the actor back to sleep. - deadline->async_wait(boost::bind(&check_deadline, deadline, socket, - boost::asio::placeholders::error)); + deadline->async_wait(boost::bind(&check_deadline, deadline, socket, boost::asio::placeholders::error)); } } // namespace @@ -73,8 +71,7 @@ bool SyncfetchNsAddr(const Url& url_s, std::string& body) { 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); + boost::asio::async_connect(socket, endpoint_iterator, boost::lambda::var(ec) = boost::lambda::_1); do { io_service.run_one(); @@ -134,14 +131,12 @@ bool SyncfetchNsAddr(const Url& url_s, std::string& body) { 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)); + 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)) + 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); 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..f8d915528 --- a/src/common/url.cpp +++ b/src/common/url.cpp @@ -23,17 +23,19 @@ namespace rocketmq { -Url::Url(const std::string& url_s) { parse(url_s); } +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()); + 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; + if (prot_i == url_s.end()) + return; std::advance(prot_i, prot_end.length()); @@ -51,12 +53,12 @@ void Url::parse(const std::string& url_s) { } host_.reserve(distance(prot_i, path_i)); - std::transform(prot_i, path_i, std::back_inserter(host_), - std::ptr_fun(tolower)); // host is icase} + 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; + if (query_i != url_s.end()) + ++query_i; query_.assign(query_i, url_s.end()); } diff --git a/src/common/url.h b/src/common/url.h old mode 100755 new mode 100644 diff --git a/src/consumer/AllocateMQStrategy.h b/src/consumer/AllocateMQStrategy.h old mode 100755 new mode 100644 index fbe501a18..4613040e9 --- a/src/consumer/AllocateMQStrategy.h +++ b/src/consumer/AllocateMQStrategy.h @@ -70,12 +70,9 @@ class AllocateMQAveragely : public AllocateMQStrategy { 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 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, " diff --git a/src/consumer/ConsumeMessageConcurrentlyService.cpp b/src/consumer/ConsumeMessageConcurrentlyService.cpp old mode 100755 new mode 100644 index 9d4e1a4c0..906935082 --- a/src/consumer/ConsumeMessageConcurrentlyService.cpp +++ b/src/consumer/ConsumeMessageConcurrentlyService.cpp @@ -24,18 +24,16 @@ namespace rocketmq { //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) { +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 @@ -78,8 +74,7 @@ void ConsumeMessageConcurrentlyService::ConsumeRequest( //m_messageQueue).toString().c_str()); + LOG_WARN("the msg of pull result is NULL,its mq:%s", (request->m_messageQueue).toString().c_str()); return; } @@ -109,8 +104,7 @@ void ConsumeMessageConcurrentlyService::ConsumeRequest( // 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()); + LOG_WARN("BROADCASTING, the message consume failed, drop it:%s", (request->m_messageQueue).toString().c_str()); break; case CLUSTERING: // send back msg to broker; @@ -118,8 +112,8 @@ void ConsumeMessageConcurrentlyService::ConsumeRequest( 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()); + (request->m_messageQueue).toString().c_str(), msgs[i].getMsgId().c_str(), i, + msgs[i].getReconsumeTimes()); m_pConsumer->sendMessageBack(msgs[i], 0); } break; @@ -134,13 +128,11 @@ void ConsumeMessageConcurrentlyService::ConsumeRequest( 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()); + LOG_WARN("Note: accumulation consume occurs on mq:%s", (request->m_messageQueue).toString().c_str()); } } -void ConsumeMessageConcurrentlyService::resetRetryTopic( - vector& msgs) { +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]; diff --git a/src/consumer/ConsumeMessageOrderlyService.cpp b/src/consumer/ConsumeMessageOrderlyService.cpp old mode 100755 new mode 100644 index e66c0cbf2..800bb4dd3 --- a/src/consumer/ConsumeMessageOrderlyService.cpp +++ b/src/consumer/ConsumeMessageOrderlyService.cpp @@ -26,8 +26,9 @@ namespace rocketmq { //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)); + 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() { @@ -107,41 +99,35 @@ void ConsumeMessageOrderlyService::stopThreadPool() { m_threadpool.join_all(); } -MessageListenerType -ConsumeMessageOrderlyService::getConsumeMsgSerivceListenerType() { +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()); +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; + ConsumeMessageOrderlyService* orderlyService = (ConsumeMessageOrderlyService*)context; orderlyService->submitConsumeRequest(request, msgs); if (tryLockMQ) { orderlyService->lockOneMQ(request->m_messageQueue); } - if (t) deleteAndZero(t); + if (t) + deleteAndZero(t); } void ConsumeMessageOrderlyService::ConsumeRequest(PullRequest* request) { bool bGetMutex = false; - boost::unique_lock lock( - request->getPullRequestCriticalSection(), boost::try_to_lock); + 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()); + 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; @@ -162,14 +148,12 @@ void ConsumeMessageOrderlyService::ConsumeRequest(PullRequest* request) { } if (m_pMessageListener) { - if ((request->isLocked() && !request->isLockExpired()) || - m_pConsumer->getMessageModel() == BROADCASTING) { + 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) { + if ((UtilAll::currentTimeMillis() - beginTime) > m_MaxTimeConsumeContinuously) { LOG_INFO( "continuely consume message queue:%s more than 60s, consume it " "later", @@ -181,15 +165,13 @@ void ConsumeMessageOrderlyService::ConsumeRequest(PullRequest* request) { request->takeMessages(msgs, pConsumer->getConsumeMessageBatchMaxSize()); if (!msgs.empty()) { request->setLastConsumeTimestamp(UtilAll::currentTimeMillis()); - ConsumeStatus consumeStatus = - m_pMessageListener->consumeMessage(msgs); + 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()); + m_pConsumer->updateConsumeOffset(request->m_messageQueue, request->commit()); } } else { continueConsume = false; @@ -200,22 +182,18 @@ void ConsumeMessageOrderlyService::ConsumeRequest(PullRequest* request) { return; } } - LOG_DEBUG("consume once exit of mq:%s", - request->m_messageQueue.toString().c_str()); + 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()); + LOG_ERROR("message queue:%s was not locked", request->m_messageQueue.toString().c_str()); tryLockLaterAndReconsume(request, true); } } } -void ConsumeMessageOrderlyService::tryLockLaterAndReconsume( - PullRequest* request, bool tryLockMQ) { +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)); + 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..2bb79794f --- 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_ @@ -38,22 +38,17 @@ class ConsumeMsgService { virtual void start() {} virtual void shutdown() {} virtual void stopThreadPool() {} - virtual void submitConsumeRequest(PullRequest* request, - vector& msgs) {} - virtual MessageListenerType getConsumeMsgSerivceListenerType() { - return messageListenerDefaultly; - } + virtual void submitConsumeRequest(PullRequest* request, vector& msgs) {} + virtual MessageListenerType getConsumeMsgSerivceListenerType() { return messageListenerDefaultly; } }; class ConsumeMessageConcurrentlyService : public ConsumeMsgService { public: - ConsumeMessageConcurrentlyService(MQConsumer*, int threadCount, - MQMessageListener* msgListener); + ConsumeMessageConcurrentlyService(MQConsumer*, int threadCount, MQMessageListener* msgListener); virtual ~ConsumeMessageConcurrentlyService(); virtual void start(); virtual void shutdown(); - virtual void submitConsumeRequest(PullRequest* request, - vector& msgs); + virtual void submitConsumeRequest(PullRequest* request, vector& msgs); virtual MessageListenerType getConsumeMsgSerivceListenerType(); virtual void stopThreadPool(); @@ -72,13 +67,11 @@ class ConsumeMessageConcurrentlyService : public ConsumeMsgService { class ConsumeMessageOrderlyService : public ConsumeMsgService { public: - ConsumeMessageOrderlyService(MQConsumer*, int threadCount, - MQMessageListener* msgListener); + ConsumeMessageOrderlyService(MQConsumer*, int threadCount, MQMessageListener* msgListener); virtual ~ConsumeMessageOrderlyService(); virtual void start(); virtual void shutdown(); - virtual void submitConsumeRequest(PullRequest* request, - vector& msgs); + virtual void submitConsumeRequest(PullRequest* request, vector& msgs); virtual void stopThreadPool(); virtual MessageListenerType getConsumeMsgSerivceListenerType(); @@ -89,8 +82,7 @@ class ConsumeMessageOrderlyService : public ConsumeMsgService { bool tryLockMQ, boost::asio::deadline_timer* t); void ConsumeRequest(PullRequest* request); - void lockMQPeriodically(boost::system::error_code& ec, - boost::asio::deadline_timer* t); + void lockMQPeriodically(boost::system::error_code& ec, boost::asio::deadline_timer* t); void unlockAllMQ(); bool lockOneMQ(const MQMessageQueue& mq); diff --git a/src/consumer/DefaultMQPullConsumer.cpp b/src/consumer/DefaultMQPullConsumer.cpp old mode 100755 new mode 100644 index bfc4e9d1a..0db381f51 --- a/src/consumer/DefaultMQPullConsumer.cpp +++ b/src/consumer/DefaultMQPullConsumer.cpp @@ -59,7 +59,7 @@ void DefaultMQPullConsumer::start() { #ifndef WIN32 /* Ignore the SIGPIPE */ struct sigaction sa; - memset(&sa,0, sizeof(struct sigaction)); + memset(&sa, 0, sizeof(struct sigaction)); sa.sa_handler = SIG_IGN; sa.sa_flags = 0; sigaction(SIGPIPE, &sa, 0); @@ -86,9 +86,7 @@ void DefaultMQPullConsumer::start() { m_serviceState = CREATE_JUST; THROW_MQEXCEPTION( MQClientException, - "The cousumer group[" + getGroupName() + - "] has been created before, specify another name please.", - -1); + "The cousumer group[" + getGroupName() + "] has been created before, specify another name please.", -1); } //& mqs) { +void DefaultMQPullConsumer::fetchSubscribeMessageQueues(const string& topic, vector& mqs) { mqs.clear(); try { - getFactory()->fetchSubscribeMessageQueues(topic, mqs, - getSessionCredentials()); + 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) { +void DefaultMQPullConsumer::registerMessageQueueListener(const string& topic, MQueueListener* pListener) { m_registerTopics.insert(topic); if (pListener) { m_pMessageQueueListener = pListener; @@ -172,36 +164,44 @@ void DefaultMQPullConsumer::registerMessageQueueListener( PullResult DefaultMQPullConsumer::pull(const MQMessageQueue& mq, const string& subExpression, - int64 offset, int maxNums) { + 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) { + 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) { +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, + 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, + int64 offset, + int maxNums, bool block) { - if (offset < 0) THROW_MQEXCEPTION(MQClientException, "offset < 0", -1); + if (offset < 0) + THROW_MQEXCEPTION(MQClientException, "offset < 0", -1); - if (maxNums <= 0) THROW_MQEXCEPTION(MQClientException, "maxNums <= 0", -1); + if (maxNums <= 0) + THROW_MQEXCEPTION(MQClientException, "maxNums <= 0", -1); // pSData( - FilterAPI::buildSubscriptionData(mq.getTopic(), subExpression)); + unique_ptr 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()); + 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()); } @@ -238,11 +235,15 @@ PullResult DefaultMQPullConsumer::pullSyncImpl(const MQMessageQueue& mq, void DefaultMQPullConsumer::pullAsyncImpl(const MQMessageQueue& mq, const string& subExpression, - int64 offset, int maxNums, bool block, + int64 offset, + int maxNums, + bool block, PullCallback* pPullCallback) { - if (offset < 0) THROW_MQEXCEPTION(MQClientException, "offset < 0", -1); + if (offset < 0) + THROW_MQEXCEPTION(MQClientException, "offset < 0", -1); - if (maxNums <= 0) THROW_MQEXCEPTION(MQClientException, "maxNums <= 0", -1); + if (maxNums <= 0) + THROW_MQEXCEPTION(MQClientException, "maxNums <= 0", -1); if (!pPullCallback) THROW_MQEXCEPTION(MQClientException, "pPullCallback is null", -1); @@ -253,8 +254,7 @@ void DefaultMQPullConsumer::pullAsyncImpl(const MQMessageQueue& mq, int sysFlag = PullSysFlag::buildSysFlag(false, block, true, false); // pSData( - FilterAPI::buildSubscriptionData(mq.getTopic(), subExpression)); + unique_ptr pSData(FilterAPI::buildSubscriptionData(mq.getTopic(), subExpression)); int timeoutMillis = block ? 1000 * 30 : 1000 * 10; @@ -266,18 +266,17 @@ void DefaultMQPullConsumer::pullAsyncImpl(const MQMessageQueue& mq, arg.pPullRequest = NULL; 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_ASYNC, // 10 - pPullCallback, getSessionCredentials(), &arg)); + 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_ASYNC, // 10 + pPullCallback, getSessionCredentials(), &arg)); } catch (MQException& e) { LOG_ERROR(e.what()); } @@ -286,14 +285,12 @@ void DefaultMQPullConsumer::pullAsyncImpl(const MQMessageQueue& mq, void DefaultMQPullConsumer::subscriptionAutomatically(const string& topic) { SubscriptionData* pSdata = m_pRebalance->getSubscriptionData(topic); if (pSdata == NULL) { - unique_ptr subscriptionData( - FilterAPI::buildSubscriptionData(topic, SUB_ALL)); + unique_ptr subscriptionData(FilterAPI::buildSubscriptionData(topic, SUB_ALL)); m_pRebalance->setSubscriptionData(topic, subscriptionData.release()); } } -void DefaultMQPullConsumer::updateConsumeOffset(const MQMessageQueue& mq, - int64 offset) { +void DefaultMQPullConsumer::updateConsumeOffset(const MQMessageQueue& mq, int64 offset) { m_pOffsetStore->updateOffset(mq, offset); } @@ -301,11 +298,8 @@ 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() { @@ -327,15 +321,13 @@ void DefaultMQPullConsumer::persistConsumerOffset() { void DefaultMQPullConsumer::persistConsumerOffsetByResetOffset() {} -void DefaultMQPullConsumer::persistConsumerOffset4PullConsumer( - const MQMessageQueue& mq) { +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(); @@ -344,8 +336,7 @@ void DefaultMQPullConsumer::checkConfig() { // consumerGroup if (!groupname.compare(DEFAULT_CONSUMER_GROUP)) { - THROW_MQEXCEPTION(MQClientException, - "consumerGroup can not equal DEFAULT_CONSUMER", -1); + THROW_MQEXCEPTION(MQClientException, "consumerGroup can not equal DEFAULT_CONSUMER", -1); } if (getMessageModel() != BROADCASTING && getMessageModel() != CLUSTERING) { @@ -358,13 +349,14 @@ 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)); + unique_ptr subscriptionData(FilterAPI::buildSubscriptionData((*it), SUB_ALL)); m_pRebalance->setSubscriptionData((*it), subscriptionData.release()); } } -ConsumeType DefaultMQPullConsumer::getConsumeType() { return CONSUME_ACTIVELY; } +ConsumeType DefaultMQPullConsumer::getConsumeType() { + return CONSUME_ACTIVELY; +} ConsumeFromWhere DefaultMQPullConsumer::getConsumeFromWhere() { return CONSUME_FROM_LAST_OFFSET; @@ -380,7 +372,9 @@ void DefaultMQPullConsumer::getSubscriptions(vector& result) { void DefaultMQPullConsumer::producePullMsgTask(PullRequest*) {} -Rebalance* DefaultMQPullConsumer::getRebalance() const { return NULL; } +Rebalance* DefaultMQPullConsumer::getRebalance() const { + return NULL; +} //m_messageQueue).toString().c_str()); - m_pullRequest->removePullMsgEvent(); - return; - } - - 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); - - 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()); +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; + } + + 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(); - void setShutdownStatus() { m_bShutdown = true; } - - private: - DefaultMQPushConsumer *m_callbackOwner; - PullRequest *m_pullRequest; - bool m_bShutdown; - }; + 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); + + 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); + } + + void setShutdownStatus() { m_bShutdown = true; } + + private: + DefaultMQPushConsumer* m_callbackOwner; + PullRequest* m_pullRequest; + bool m_bShutdown; +}; //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 { - m_pRebalance->doRebalance(); - } catch (MQException &e) { - LOG_ERROR(e.what()); - } - } - } - - void DefaultMQPushConsumer::persistConsumerOffset() { - if (isServiceStateOk()) { - m_pRebalance->persistConsumerOffset(); - } - } - - void DefaultMQPushConsumer::persistConsumerOffsetByResetOffset() { - if (isServiceStateOk()) { - m_pRebalance->persistConsumerOffsetByResetOffset(); - } - } - - void DefaultMQPushConsumer::start() { +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 { + m_pRebalance->doRebalance(); + } catch (MQException& e) { + LOG_ERROR(e.what()); + } + } +} + +void DefaultMQPushConsumer::persistConsumerOffset() { + if (isServiceStateOk()) { + m_pRebalance->persistConsumerOffset(); + } +} + +void DefaultMQPushConsumer::persistConsumerOffsetByResetOffset() { + if (isServiceStateOk()) { + m_pRebalance->persistConsumerOffsetByResetOffset(); + } +} + +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 - // are allocating ConsumeMessageConcurrentlyService - { - 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); - } - - //load(); - } catch (MQClientException &e) { - bStartFailed = true; - errorMsg = std::string(e.what()); - } - m_consumerService->start(); - - getFactory()->start(); - - updateTopicSubscribeInfoWhenSubscriptionChanged(); - getFactory()->sendHeartbeatToAllBroker(); - - m_serviceState = RUNNING; - if (bStartFailed) { - shutdown(); - THROW_MQEXCEPTION(MQClientException, errorMsg, -1); - } - break; - } - case RUNNING: - case START_FAILED: - case SHUTDOWN_ALREADY: - break; - default: - break; - } - - 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; - } - case CREATE_JUST: - case SHUTDOWN_ALREADY: - break; - default: - break; - } - } - - void DefaultMQPushConsumer::registerMessageListener( - MQMessageListener *pMessageListener) { - if (NULL != pMessageListener) { - m_pMessageListener = pMessageListener; - } - } - - MessageListenerType DefaultMQPushConsumer::getMessageListenerType() { - if (NULL != m_pMessageListener) { - return m_pMessageListener->getMessageListenerType(); - } - return messageListenerDefaultly; - } - - ConsumeMsgService *DefaultMQPushConsumer::getConsumerMsgService() const { - return m_consumerService; - } - - OffsetStore *DefaultMQPushConsumer::getOffsetStore() const { - return m_pOffsetStore; - } - - Rebalance *DefaultMQPushConsumer::getRebalance() const { return m_pRebalance; } - - void DefaultMQPushConsumer::subscribe(const string &topic, - const string &subExpression) { - m_subTopics[topic] = subExpression; - } - - void DefaultMQPushConsumer::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); - } - - if (m_pMessageListener == NULL) { - THROW_MQEXCEPTION(MQClientException, "messageListener is null ", -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)); - - m_pRebalance->setSubscriptionData(it->first, pSData.release()); - } - - 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::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; - } - - ConsumeFromWhere DefaultMQPushConsumer::getConsumeFromWhere() { - return m_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::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::triggerNextPullRequest( - boost::asio::deadline_timer *t, PullRequest *request) { - // LOG_INFO("trigger pullrequest for:%s", - // (request->m_messageQueue).toString().c_str()); + 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); + } + } + + 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); + } + + //load(); + } catch (MQClientException& e) { + bStartFailed = true; + errorMsg = std::string(e.what()); + } + m_consumerService->start(); + + getFactory()->start(); + + updateTopicSubscribeInfoWhenSubscriptionChanged(); + getFactory()->sendHeartbeatToAllBroker(); + + m_serviceState = RUNNING; + if (bStartFailed) { + shutdown(); + THROW_MQEXCEPTION(MQClientException, errorMsg, -1); + } + break; + } + case RUNNING: + case START_FAILED: + case SHUTDOWN_ALREADY: + break; + default: + break; + } + + 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; + } + case CREATE_JUST: + case SHUTDOWN_ALREADY: + break; + default: + break; + } +} + +void DefaultMQPushConsumer::registerMessageListener(MQMessageListener* pMessageListener) { + if (NULL != pMessageListener) { + m_pMessageListener = pMessageListener; + } +} + +MessageListenerType DefaultMQPushConsumer::getMessageListenerType() { + if (NULL != m_pMessageListener) { + return m_pMessageListener->getMessageListenerType(); + } + return messageListenerDefaultly; +} + +ConsumeMsgService* DefaultMQPushConsumer::getConsumerMsgService() const { + return m_consumerService; +} + +OffsetStore* DefaultMQPushConsumer::getOffsetStore() const { + return m_pOffsetStore; +} + +Rebalance* DefaultMQPushConsumer::getRebalance() const { + return m_pRebalance; +} + +void DefaultMQPushConsumer::subscribe(const string& topic, const string& subExpression) { + m_subTopics[topic] = subExpression; +} + +void DefaultMQPushConsumer::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); + } + + if (m_pMessageListener == NULL) { + THROW_MQEXCEPTION(MQClientException, "messageListener is null ", -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)); + + m_pRebalance->setSubscriptionData(it->first, pSData.release()); + } + + 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::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; +} + +ConsumeFromWhere DefaultMQPushConsumer::getConsumeFromWhere() { + return m_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::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::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)); + } + } else { + LOG_WARN("produce pullmsg of mq:%s failed", request->m_messageQueue.toString().c_str()); + } +} + +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; + } + + MQMessageQueue& messageQueue = request->m_messageQueue; + if (m_consumerService->getConsumeMsgSerivceListenerType() == messageListenerOrderly) { + if (!request->isLocked() || request->isLockExpired()) { + if (!m_pRebalance->lock(messageQueue)) { 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)); - } - } else { - LOG_WARN("produce pullmsg of mq:%s failed", - request->m_messageQueue.toString().c_str()); - } - } - - 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; - } - - 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; - } - } - - string subExpression; - SubscriptionData *pSdata = - m_pRebalance->getSubscriptionData(messageQueue.getTopic()); - if (pSdata == NULL) { - producePullMsgTask(request); - return; - } - subExpression = pSdata->getSubString(); - - int sysFlag = - PullSysFlag::buildSysFlag(commitOffsetEnable, // commitOffset + 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; + } + } + + 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); - 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: { - request->setNextOffset(pullResult.nextBeginOffset); - 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; - } - } - } 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(); - } else { - LOG_ERROR("could not find asyncPullCallback for:%s", - it->first.toString().c_str()); - } - } - } - } - - 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; - } - } - } - - 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; + 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); } - - bool commitOffsetEnable = false; - int64 commitOffsetValue = 0; - if (CLUSTERING == getMessageModel()) { - commitOffsetValue = m_pOffsetStore->readOffset( - messageQueue, READ_FROM_MEMORY, getSessionCredentials()); - if (commitOffsetValue > 0) { - commitOffsetEnable = true; - } + 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); - string subExpression; - SubscriptionData *pSdata = - (m_pRebalance->getSubscriptionData(messageQueue.getTopic())); - if (pSdata == NULL) { - producePullMsgTask(request); - return; - } - subExpression = pSdata->getSubString(); + LOG_DEBUG("NO_MATCHED_MSG:%s,nextBeginOffset:%lld", messageQueue.toString().c_str(), + pullResult.nextBeginOffset); + break; + } + case OFFSET_ILLEGAL: { + request->setNextOffset(pullResult.nextBeginOffset); + producePullMsgTask(request); - int sysFlag = - PullSysFlag::buildSysFlag(commitOffsetEnable, // commitOffset + 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; + } + } + } 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(); + } else { + LOG_ERROR("could not find asyncPullCallback for:%s", it->first.toString().c_str()); + } + } + } +} + +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; + } + } + } + + 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; + } + } + + 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; - } + 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; +} //selectConsumer(groupName); if (pConsumer) { LOG_INFO("new LocalFileOffsetStore"); - string directoryName = - UtilAll::getLocalAddress() + "@" + pConsumer->getInstanceName(); + string directoryName = UtilAll::getLocalAddress() + "@" + pConsumer->getInstanceName(); m_storePath = ".rocketmq_offsets/" + directoryName + "/" + groupName; string homeDir(UtilAll::getHomeDirectory()); m_storeFile = homeDir + "/" + m_storePath + "/offsets.Json"; @@ -96,16 +94,13 @@ void LocalFileOffsetStore::load() { } ifs.close(); - for (map::iterator it = m_offsetTable_tmp.begin(); - it != m_offsetTable_tmp.end(); ++it) { + 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()); + MQMessageQueue mq(object["topic"].asString(), object["brokerName"].asString(), object["queueId"].asInt()); updateOffset(mq, it->second); } m_offsetTable_tmp.clear(); @@ -140,15 +135,14 @@ void LocalFileOffsetStore::load() { } } -void LocalFileOffsetStore::updateOffset(const MQMessageQueue& mq, - int64 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) { +int64 LocalFileOffsetStore::readOffset(const MQMessageQueue& mq, + ReadOffsetType type, + const SessionCredentials& session_credentials) { switch (type) { case MEMORY_FIRST_THEN_STORE: case READ_FROM_MEMORY: { @@ -176,13 +170,11 @@ int64 LocalFileOffsetStore::readOffset( default: break; } - LOG_ERROR( - "can not readOffset from offsetStore.json, maybe first time consumation"); + 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); @@ -190,8 +182,7 @@ void LocalFileOffsetStore::persistAll(const std::vector& mqs) { map m_offsetTable_tmp; vector::const_iterator it = mqs.begin(); for (; it != mqs.end(); ++it) { - MessageQueue mq_tmp((*it).getTopic(), (*it).getBrokerName(), - (*it).getQueueId()); + MessageQueue mq_tmp((*it).getTopic(), (*it).getBrokerName(), (*it).getQueueId()); string mqKey = mq_tmp.toJson().toStyledString(); m_offsetTable_tmp[mqKey] = m_offsetTable[*it]; } @@ -210,8 +201,7 @@ void LocalFileOffsetStore::persistAll(const std::vector& mqs) { } 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); + THROW_MQEXCEPTION(MQClientException, "persistAll:open offset store file failed", -1); } s.close(); if (!UtilAll::ReplaceFile(storefile_bak, m_storeFile)) @@ -220,31 +210,28 @@ void LocalFileOffsetStore::persistAll(const std::vector& mqs) { } 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); + THROW_MQEXCEPTION(MQClientException, "persistAll:open offset store file failed", -1); } } void LocalFileOffsetStore::removeOffset(const MQMessageQueue& mq) {} // lock(m_lock); m_offsetTable[mq] = offset; } -int64 RemoteBrokerOffsetStore::readOffset( - const MQMessageQueue& mq, ReadOffsetType type, - const SessionCredentials& session_credentials) { +int64 RemoteBrokerOffsetStore::readOffset(const MQMessageQueue& mq, + ReadOffsetType type, + const SessionCredentials& session_credentials) { switch (type) { case MEMORY_FIRST_THEN_STORE: case READ_FROM_MEMORY: { @@ -259,8 +246,7 @@ int64 RemoteBrokerOffsetStore::readOffset( } case READ_FROM_STORE: { try { - int64 brokerOffset = - fetchConsumeOffsetFromBroker(mq, session_credentials); + int64 brokerOffset = fetchConsumeOffsetFromBroker(mq, session_credentials); // lock(m_lock); @@ -296,42 +281,35 @@ void RemoteBrokerOffsetStore::persist( } } -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); + 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())); +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())); + m_pClientFactory->updateTopicRouteInfoFromNameServer(mq.getTopic(), session_credentials); + pFindBrokerResult.reset(m_pClientFactory->findBrokerAddressInAdmin(mq.getBrokerName())); } if (pFindBrokerResult != NULL) { - UpdateConsumerOffsetRequestHeader* pRequestHeader = - new UpdateConsumerOffsetRequestHeader(); + 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); + 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); + pFindBrokerResult->brokerAddr, pRequestHeader, 1000 * 5, session_credentials); } catch (MQException& e) { LOG_ERROR(e.what()); } @@ -339,28 +317,23 @@ void RemoteBrokerOffsetStore::updateConsumeOffsetToBroker( LOG_WARN("The broker not exist"); } -int64 RemoteBrokerOffsetStore::fetchConsumeOffsetFromBroker( - const MQMessageQueue& mq, const SessionCredentials& session_credentials) { - unique_ptr pFindBrokerResult( - m_pClientFactory->findBrokerAddressInAdmin(mq.getBrokerName())); +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())); + m_pClientFactory->updateTopicRouteInfoFromNameServer(mq.getTopic(), session_credentials); + pFindBrokerResult.reset(m_pClientFactory->findBrokerAddressInAdmin(mq.getBrokerName())); } if (pFindBrokerResult != NULL) { - QueryConsumerOffsetRequestHeader* pRequestHeader = - new QueryConsumerOffsetRequestHeader(); + 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); + 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); diff --git a/src/consumer/OffsetStore.h b/src/consumer/OffsetStore.h old mode 100755 new mode 100644 index 7e8d8c324..d10f233a4 --- a/src/consumer/OffsetStore.h +++ b/src/consumer/OffsetStore.h @@ -28,11 +28,11 @@ namespace rocketmq { class MQClientFactory; //& mq) = 0; virtual void removeOffset(const MQMessageQueue& mq) = 0; @@ -67,10 +67,10 @@ class LocalFileOffsetStore : public OffsetStore { virtual void load(); virtual void updateOffset(const MQMessageQueue& mq, int64 offset); - virtual int64 readOffset(const MQMessageQueue& mq, ReadOffsetType type, + virtual int64 readOffset(const MQMessageQueue& mq, + ReadOffsetType type, const SessionCredentials& session_credentials); - virtual void persist(const MQMessageQueue& mq, - 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); @@ -87,19 +87,18 @@ class RemoteBrokerOffsetStore : public OffsetStore { virtual void load(); virtual void updateOffset(const MQMessageQueue& mq, int64 offset); - virtual int64 readOffset(const MQMessageQueue& mq, ReadOffsetType type, + virtual int64 readOffset(const MQMessageQueue& mq, + ReadOffsetType type, const SessionCredentials& session_credentials); - virtual void persist(const MQMessageQueue& mq, - 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); + 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; } @@ -46,9 +44,9 @@ int PullAPIWrapper::recalculatePullFromWhichNode(const MQMessageQueue& mq) { return MASTER_ID; } -PullResult PullAPIWrapper::processPullResult( - const MQMessageQueue& mq, PullResult* pullResult, - SubscriptionData* subscriptionData) { +PullResult PullAPIWrapper::processPullResult(const MQMessageQueue& mq, + PullResult* pullResult, + SubscriptionData* subscriptionData) { PullResultExt* pResultExt = static_cast(pullResult); if (pResultExt == NULL) { string errMsg("The pullResult NULL of"); @@ -75,39 +73,35 @@ PullResult PullAPIWrapper::processPullResult( msgFilterList.push_back(*it); } } - } else - { + } else { msgFilterList.swap(msgAllList); } } - return PullResult(pResultExt->pullStatus, pResultExt->nextBeginOffset, - pResultExt->minOffset, pResultExt->maxOffset, + 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*/) { +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)); + m_MQClientFactory->findBrokerAddressInSubscribe(mq.getBrokerName(), recalculatePullFromWhichNode(mq), false)); //updateTopicRouteInfoFromNameServer(mq.getTopic(), - session_credentials); - pFindBrokerResult.reset(m_MQClientFactory->findBrokerAddressInSubscribe( - mq.getBrokerName(), recalculatePullFromWhichNode(mq), false)); + m_MQClientFactory->updateTopicRouteInfoFromNameServer(mq.getTopic(), session_credentials); + pFindBrokerResult.reset( + m_MQClientFactory->findBrokerAddressInSubscribe(mq.getBrokerName(), recalculatePullFromWhichNode(mq), false)); } if (pFindBrokerResult != NULL) { @@ -129,9 +123,9 @@ PullResult* PullAPIWrapper::pullKernelImpl( pRequestHeader->subscription = subExpression; pRequestHeader->subVersion = subVersion; - return m_MQClientFactory->getMQClientAPIImpl()->pullMessage( - pFindBrokerResult->brokerAddr, pRequestHeader, timeoutMillis, - communicationMode, pullCallback, pArg, session_credentials); + return m_MQClientFactory->getMQClientAPIImpl()->pullMessage(pFindBrokerResult->brokerAddr, pRequestHeader, + timeoutMillis, communicationMode, pullCallback, pArg, + session_credentials); } THROW_MQEXCEPTION(MQClientException, "The broker not exist", -1); } diff --git a/src/consumer/PullAPIWrapper.h b/src/consumer/PullAPIWrapper.h old mode 100755 new mode 100644 index e3d0a1e31..57c6f0d15 --- a/src/consumer/PullAPIWrapper.h +++ b/src/consumer/PullAPIWrapper.h @@ -32,8 +32,7 @@ class PullAPIWrapper { PullAPIWrapper(MQClientFactory* mQClientFactory, const string& consumerGroup); ~PullAPIWrapper(); - PullResult processPullResult(const MQMessageQueue& mq, PullResult* pullResult, - SubscriptionData* subscriptionData); + PullResult processPullResult(const MQMessageQueue& mq, PullResult* pullResult, SubscriptionData* subscriptionData); PullResult* pullKernelImpl(const MQMessageQueue& mq, // 1 string subExpression, // 2 diff --git a/src/consumer/PullRequest.cpp b/src/consumer/PullRequest.cpp index b261d82d8..5fd6d5832 100644 --- a/src/consumer/PullRequest.cpp +++ b/src/consumer/PullRequest.cpp @@ -82,16 +82,16 @@ int64 PullRequest::getCacheMinOffset() { } } -int64 PullRequest::getCacheMaxOffset() { return m_queueOffsetMax; } +int64 PullRequest::getCacheMaxOffset() { + return m_queueOffsetMax; +} int PullRequest::getCacheMsgCount() { boost::lock_guard lock(m_pullRequestLock); return m_msgTreeMap.size(); } -void PullRequest::getMessageByQueueOffset(vector& msgs, - int64 minQueueOffset, - int64 maxQueueOffset) { +void PullRequest::getMessageByQueueOffset(vector& msgs, int64 minQueueOffset, int64 maxQueueOffset) { boost::lock_guard lock(m_pullRequestLock); int64 it = minQueueOffset; @@ -107,23 +107,19 @@ int64 PullRequest::removeMessage(vector& msgs) { 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()); + 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()); + 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); + LOG_INFO("cache msg size:" SIZET_FMT " of pullRequest:%s, return offset result is:%lld", m_msgTreeMap.size(), + m_messageQueue.toString().c_str(), result); } } @@ -166,7 +162,9 @@ void PullRequest::setDroped(bool droped) { */ } -bool PullRequest::isDroped() const { return m_bDroped.load() == 1; } +bool PullRequest::isDroped() const { + return m_bDroped.load() == 1; +} int64 PullRequest::getNextOffset() { boost::lock_guard lock(m_pullRequestLock); @@ -177,24 +175,29 @@ void PullRequest::setLocked(bool Locked) { int temp = (Locked == true ? 1 : 0); m_bLocked.store(temp); } -bool PullRequest::isLocked() const { return m_bLocked.load() == 1; } +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; } -int64 PullRequest::getLastLockTimestamp() const { return m_lastLockTimestamp; } +int64 PullRequest::getLastLockTimestamp() const { + return m_lastLockTimestamp; +} void PullRequest::setLastPullTimestamp(uint64 time) { m_lastPullTimestamp = time; } -uint64 PullRequest::getLastPullTimestamp() const { return m_lastPullTimestamp; } +uint64 PullRequest::getLastPullTimestamp() const { + return m_lastPullTimestamp; +} void PullRequest::setLastConsumeTimestamp(uint64 time) { m_lastConsumeTimestamp = time; @@ -204,16 +207,22 @@ uint64 PullRequest::getLastConsumeTimestamp() const { return m_lastConsumeTimestamp; } -void PullRequest::setTryUnlockTimes(int time) { m_lastLockTimestamp = time; } +void PullRequest::setTryUnlockTimes(int time) { + m_lastLockTimestamp = time; +} -int PullRequest::getTryUnlockTimes() const { return m_lastLockTimestamp; } +int PullRequest::getTryUnlockTimes() const { + return m_lastLockTimestamp; +} void PullRequest::setNextOffset(int64 nextoffset) { boost::lock_guard lock(m_pullRequestLock); m_nextOffset = nextoffset; } -string PullRequest::getGroupName() const { return m_groupname; } +string PullRequest::getGroupName() const { + return m_groupname; +} boost::timed_mutex& PullRequest::getPullRequestCriticalSection() { return m_consumeLock; @@ -250,26 +259,27 @@ int64 PullRequest::commit() { } } -void PullRequest::removePullMsgEvent() { m_bPullMsgEventInprogress = false; } +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()); + LOG_INFO("pullRequest with mq :%s set pullMsgEvent", m_messageQueue.toString().c_str()); return true; } return false; } int PullRequest::getLatestPullRequestOpaque() { - boost::lock_guard lock(m_pullRequestLock); - return m_latestPullRequestOpaque; + boost::lock_guard lock(m_pullRequestLock); + return m_latestPullRequestOpaque; } void PullRequest::setLatestPullRequestOpaque(int opaque) { - boost::lock_guard lock(m_pullRequestLock); - m_latestPullRequestOpaque = opaque; + boost::lock_guard lock(m_pullRequestLock); + m_latestPullRequestOpaque = opaque; } //& msgs, int64 minQueueOffset, - int64 maxQueueOffset); + void getMessageByQueueOffset(vector& msgs, int64 minQueueOffset, int64 maxQueueOffset); int64 removeMessage(vector& msgs); void clearAllMsgs(); @@ -87,10 +86,10 @@ class PullRequest { map m_msgTreeMapTemp; boost::mutex m_pullRequestLock; uint64 m_lastLockTimestamp; // ms - //uint64 m_tryUnlockTimes; + // uint64 m_tryUnlockTimes; uint64 m_lastPullTimestamp; uint64 m_lastConsumeTimestamp; - int m_latestPullRequestOpaque; + int m_latestPullRequestOpaque; boost::timed_mutex m_consumeLock; boost::atomic m_bPullMsgEventInprogress; }; diff --git a/src/consumer/PullResult.cpp b/src/consumer/PullResult.cpp old mode 100755 new mode 100644 index 8648abea5..5fc2f671c --- a/src/consumer/PullResult.cpp +++ b/src/consumer/PullResult.cpp @@ -19,36 +19,28 @@ namespace rocketmq { //& src) - : pullStatus(pullStatus), - nextBeginOffset(nextBeginOffset), - minOffset(minOffset), - maxOffset(maxOffset) { + : 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(); } +PullResult::~PullResult() { + msgFoundList.clear(); +} //(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() {} diff --git a/src/consumer/Rebalance.cpp b/src/consumer/Rebalance.cpp index b4e63603c..a2997a714 100644 --- a/src/consumer/Rebalance.cpp +++ b/src/consumer/Rebalance.cpp @@ -32,7 +32,8 @@ Rebalance::Rebalance(MQConsumer* consumer, MQClientFactory* pfactory) Rebalance::~Rebalance() { { map::iterator it = m_subscriptionData.begin(); - for (; it != m_subscriptionData.end(); ++it) deleteAndZero(it->second); + for (; it != m_subscriptionData.end(); ++it) + deleteAndZero(it->second); m_subscriptionData.clear(); } { @@ -63,8 +64,7 @@ void Rebalance::doRebalance() { } if (mqAll.empty()) { if (!UtilAll::startsWith_retry(topic)) - THROW_MQEXCEPTION(MQClientException, "doRebalance the topic is empty", - -1); + THROW_MQEXCEPTION(MQClientException, "doRebalance the topic is empty", -1); } // cidAll; - m_pClientFactory->findConsumerIds( - topic, m_pConsumer->getGroupName(), cidAll, - m_pConsumer->getSessionCredentials()); + m_pClientFactory->findConsumerIds(topic, m_pConsumer->getGroupName(), cidAll, + m_pConsumer->getSessionCredentials()); if (cidAll.empty()) { /*remove the droping pullRequest changes for recovery consume fastly @@ -102,13 +101,11 @@ void Rebalance::doRebalance() { } }*/ - THROW_MQEXCEPTION(MQClientException, - "doRebalance the cidAll is empty", -1); + 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()); + 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); + m_pAllocateMQStrategy->allocate(m_pConsumer->getMQClientId(), mqAll, cidAll, allocateResult); } catch (MQException& e) { THROW_MQEXCEPTION(MQClientException, "allocate error", -1); } @@ -145,8 +141,7 @@ void Rebalance::doRebalance() { } void Rebalance::persistConsumerOffset() { - DefaultMQPushConsumer* pConsumer = - static_cast(m_pConsumer); + DefaultMQPushConsumer* pConsumer = static_cast(m_pConsumer); OffsetStore* pOffsetStore = pConsumer->getOffsetStore(); vector mqs; { @@ -170,8 +165,7 @@ void Rebalance::persistConsumerOffset() { } void Rebalance::persistConsumerOffsetByResetOffset() { - DefaultMQPushConsumer* pConsumer = - static_cast(m_pConsumer); + DefaultMQPushConsumer* pConsumer = static_cast(m_pConsumer); OffsetStore* pOffsetStore = pConsumer->getOffsetStore(); vector mqs; { @@ -201,20 +195,16 @@ map& Rebalance::getSubscriptionInner() { return m_subscriptionData; } -void Rebalance::setSubscriptionData(const string& topic, - SubscriptionData* pdata) { - if (pdata != NULL && - m_subscriptionData.find(topic) == m_subscriptionData.end()) +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) { +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()) + if (m_topicSubscribeInfoTable.find(topic) != m_topicSubscribeInfoTable.end()) m_topicSubscribeInfoTable.erase(topic); m_topicSubscribeInfoTable[topic] = mqs; } @@ -226,11 +216,9 @@ void Rebalance::setTopicSubscribeInfo(const string& topic, } } -bool Rebalance::getTopicSubscribeInfo(const string& topic, - vector& mqs) { +bool Rebalance::getTopicSubscribeInfo(const string& topic, vector& mqs) { boost::lock_guard lock(m_topicSubscribeInfoTableMutex); - if (m_topicSubscribeInfoTable.find(topic) != - m_topicSubscribeInfoTable.end()) { + if (m_topicSubscribeInfoTable.find(topic) != m_topicSubscribeInfoTable.end()) { mqs = m_topicSubscribeInfoTable[topic]; return true; } @@ -258,8 +246,7 @@ map Rebalance::getPullRequestTable() { void Rebalance::unlockAll(bool oneway) { map*> brokerMqs; MQ2PULLREQ requestQueueTable = getPullRequestTable(); - for (MQ2PULLREQ::iterator it = requestQueueTable.begin(); - it != requestQueueTable.end(); ++it) { + 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; @@ -270,26 +257,22 @@ void Rebalance::unlockAll(bool oneway) { } } LOG_INFO("unLockAll " SIZET_FMT " broker mqs", brokerMqs.size()); - for (map*>::iterator itb = brokerMqs.begin(); - itb != brokerMqs.end(); ++itb) { + for (map*>::iterator itb = brokerMqs.begin(); itb != brokerMqs.end(); ++itb) { unique_ptr pFindBrokerResult( - m_pClientFactory->findBrokerAddressInSubscribe(itb->first, MASTER_ID, - true)); + 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()); + 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()); + 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) { @@ -309,14 +292,12 @@ void Rebalance::unlockAll(bool oneway) { void Rebalance::unlock(MQMessageQueue mq) { unique_ptr pFindBrokerResult( - m_pClientFactory->findBrokerAddressInSubscribe(mq.getBrokerName(), - MASTER_ID, true)); + 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()); + unique_ptr unlockBatchRequest(new UnlockBatchRequestBody()); vector mqs; mqs.push_back(mq); unlockBatchRequest->setClientId(m_pConsumer->getMQClientId()); @@ -324,9 +305,8 @@ void Rebalance::unlock(MQMessageQueue mq) { unlockBatchRequest->setMqSet(mqs); try { - m_pClientFactory->getMQClientAPIImpl()->unlockBatchMQ( - pFindBrokerResult->brokerAddr, unlockBatchRequest.get(), 1000, - m_pConsumer->getSessionCredentials()); + 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) { @@ -344,8 +324,7 @@ void Rebalance::unlock(MQMessageQueue mq) { void Rebalance::lockAll() { map*> brokerMqs; MQ2PULLREQ requestQueueTable = getPullRequestTable(); - for (MQ2PULLREQ::iterator it = requestQueueTable.begin(); - it != requestQueueTable.end(); ++it) { + 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()) { @@ -358,38 +337,31 @@ void Rebalance::lockAll() { } } LOG_INFO("LockAll " SIZET_FMT " broker mqs", brokerMqs.size()); - for (map*>::iterator itb = brokerMqs.begin(); - itb != brokerMqs.end(); ++itb) { + 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)); + 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()); + 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()); + 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()); + 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()); + 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()); + LOG_ERROR("lockBatchMQ fails of mq:%s", messageQueues[i].toString().c_str()); } } messageQueues.clear(); @@ -402,8 +374,7 @@ void Rebalance::lockAll() { } bool Rebalance::lock(MQMessageQueue mq) { unique_ptr pFindBrokerResult( - m_pClientFactory->findBrokerAddressInSubscribe(mq.getBrokerName(), - MASTER_ID, true)); + m_pClientFactory->findBrokerAddressInSubscribe(mq.getBrokerName(), MASTER_ID, true)); if (!pFindBrokerResult) { LOG_ERROR("lock findBrokerAddressInSubscribe ret null for broker:%s", mq.getBrokerName().data()); return false; @@ -419,12 +390,10 @@ bool Rebalance::lock(MQMessageQueue mq) { 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()); + 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()); + LOG_ERROR("lock mq on broker:%s failed", pFindBrokerResult->brokerAddr.c_str()); return false; } for (unsigned int i = 0; i != messageQueues.size(); ++i) { @@ -447,11 +416,9 @@ bool Rebalance::lock(MQMessageQueue mq) { } //& mqsSelf) { +bool RebalancePull::updateRequestTableInRebalance(const string& topic, vector& mqsSelf) { return false; } @@ -466,11 +433,9 @@ void RebalancePull::messageQueueChanged(const string& topic, void RebalancePull::removeUnnecessaryMessageQueue(const MQMessageQueue& mq) {} //& mqsSelf) { +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()); @@ -484,12 +449,11 @@ bool RebalancePush::updateRequestTableInRebalance( for (; it != requestQueueTable.end(); ++it) { MQMessageQueue mqtemp = it->first; if (mqtemp.getTopic().compare(topic) == 0) { - if (mqsSelf.empty() || - (find(mqsSelf.begin(), mqsSelf.end(), mqtemp) == mqsSelf.end())) { + 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); + // 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 @@ -503,8 +467,7 @@ bool RebalancePush::updateRequestTableInRebalance( // pullrequestAdd; - DefaultMQPushConsumer* pConsumer = - static_cast(m_pConsumer); + DefaultMQPushConsumer* pConsumer = static_cast(m_pConsumer); vector::iterator it2 = mqsSelf.begin(); for (; it2 != mqsSelf.end(); ++it2) { PullRequest* pPullRequest(getPullRequest(*it2)); @@ -513,10 +476,9 @@ bool RebalancePush::updateRequestTableInRebalance( "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 + 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) { /* @@ -530,8 +492,7 @@ bool RebalancePush::updateRequestTableInRebalance( 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()); + LOG_INFO("pullRequest with mq :%s has unfinished pullMsgEvent", (it2->toString()).c_str()); bPullMsgEvent = pPullRequest->addPullMsgEvent(); } pPullRequest->setDroped(false); @@ -546,9 +507,7 @@ bool RebalancePush::updateRequestTableInRebalance( changed = true; pConsumer->producePullMsgTask(pPullRequest); } else { - LOG_ERROR( - "get fatel error QueryOffset of mq:%s, do not reconsume this queue", - (it2->toString()).c_str()); + LOG_ERROR("get fatel error QueryOffset of mq:%s, do not reconsume this queue", (it2->toString()).c_str()); } } @@ -566,8 +525,7 @@ bool RebalancePush::updateRequestTableInRebalance( // pq; addPullRequest(*it2, pullRequest); pullrequestAdd.push_back(pullRequest); - LOG_INFO("add mq:%s, request initiall offset:%lld", - (*it2).toString().c_str(), nextOffset); + LOG_INFO("add mq:%s, request initiall offset:%lld", (*it2).toString().c_str(), nextOffset); } } } @@ -584,94 +542,72 @@ bool RebalancePush::updateRequestTableInRebalance( int64 RebalancePush::computePullFromWhere(const MQMessageQueue& mq) { int64 result = -1; - DefaultMQPushConsumer* pConsumer = - static_cast(m_pConsumer); + 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()); + 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); + 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()); + 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()); + 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); + 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()); + 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()); + 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()); + 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); + 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()); + 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()); + 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()); + 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); + 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); + 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()); + 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()); + 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()); + LOG_ERROR("CONSUME_FROM_TIMESTAMP error, lastOffset of mq:%s, return -1", mq.toString().c_str()); result = -1; } break; @@ -687,8 +623,7 @@ void RebalancePush::messageQueueChanged(const string& topic, vector& mqDivided) {} void RebalancePush::removeUnnecessaryMessageQueue(const MQMessageQueue& mq) { - DefaultMQPushConsumer* pConsumer = - static_cast(m_pConsumer); + DefaultMQPushConsumer* pConsumer = static_cast(m_pConsumer); OffsetStore* pOffsetStore = pConsumer->getOffsetStore(); pOffsetStore->persist(mq, m_pConsumer->getSessionCredentials()); diff --git a/src/consumer/Rebalance.h b/src/consumer/Rebalance.h old mode 100755 new mode 100644 index 42f86679a..8d8a9ae5f --- a/src/consumer/Rebalance.h +++ b/src/consumer/Rebalance.h @@ -42,8 +42,7 @@ class Rebalance { virtual int64 computePullFromWhere(const MQMessageQueue& mq) = 0; - virtual bool updateRequestTableInRebalance( - const string& topic, vector& mqsSelf) = 0; + virtual bool updateRequestTableInRebalance(const string& topic, vector& mqsSelf) = 0; public: void doRebalance(); @@ -95,8 +94,7 @@ class RebalancePull : public Rebalance { virtual int64 computePullFromWhere(const MQMessageQueue& mq); - virtual bool updateRequestTableInRebalance(const string& topic, - vector& mqsSelf); + virtual bool updateRequestTableInRebalance(const string& topic, vector& mqsSelf); }; //& mqsSelf); + virtual bool updateRequestTableInRebalance(const string& topic, vector& mqsSelf); }; //& SubscriptionData::getTagsSet() { return m_tagSet; } +vector& SubscriptionData::getTagsSet() { + return m_tagSet; +} bool SubscriptionData::operator==(const SubscriptionData& other) const { if (!m_subString.compare(other.m_subString)) { diff --git a/src/consumer/SubscriptionData.h b/src/consumer/SubscriptionData.h old mode 100755 new mode 100644 diff --git a/src/dllmain.cpp b/src/dllmain.cpp old mode 100755 new mode 100644 index 72f61fd46..a9be1e020 --- a/src/dllmain.cpp +++ b/src/dllmain.cpp @@ -18,8 +18,7 @@ #include #include "windows.h" -BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, - LPVOID lpReserved) { +BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: break; diff --git a/src/extern/CMessage.cpp b/src/extern/CMessage.cpp index 4106c274c..1008f00b2 100644 --- a/src/extern/CMessage.cpp +++ b/src/extern/CMessage.cpp @@ -25,69 +25,68 @@ extern "C" { using namespace rocketmq; -CMessage *CreateMessage(const char *topic) { - - MQMessage *mqMessage = new MQMessage(); - if (topic != NULL) { - mqMessage->setTopic(topic); - } - return (CMessage *) mqMessage; +CMessage* CreateMessage(const char* topic) { + MQMessage* mqMessage = new MQMessage(); + if (topic != NULL) { + mqMessage->setTopic(topic); + } + return (CMessage*)mqMessage; } -int DestroyMessage(CMessage *msg) { - if (msg == NULL) { - return NULL_POINTER; - } - delete (MQMessage *)msg; - return OK; +int DestroyMessage(CMessage* msg) { + if (msg == NULL) { + return NULL_POINTER; + } + delete (MQMessage*)msg; + return OK; } -int SetMessageTopic(CMessage *msg, const char *topic) { - if (msg == NULL) { - return NULL_POINTER; - } - ((MQMessage *)msg)->setTopic(topic); - return OK; +int SetMessageTopic(CMessage* msg, const char* topic) { + if (msg == NULL) { + return NULL_POINTER; + } + ((MQMessage*)msg)->setTopic(topic); + return OK; } -int SetMessageTags(CMessage *msg, const char *tags) { - if (msg == NULL) { - return NULL_POINTER; - } - ((MQMessage *)msg)->setTags(tags); - return OK; +int SetMessageTags(CMessage* msg, const char* tags) { + if (msg == NULL) { + return NULL_POINTER; + } + ((MQMessage*)msg)->setTags(tags); + return OK; } -int SetMessageKeys(CMessage *msg, const char *keys) { - if (msg == NULL) { - return NULL_POINTER; - } - ((MQMessage *)msg)->setKeys(keys); - return OK; +int SetMessageKeys(CMessage* msg, const char* keys) { + if (msg == NULL) { + return NULL_POINTER; + } + ((MQMessage*)msg)->setKeys(keys); + return OK; } -int SetMessageBody(CMessage *msg, const char *body) { - if (msg == NULL) { - return NULL_POINTER; - } - ((MQMessage *)msg)->setBody(body); - return OK; +int SetMessageBody(CMessage* msg, const char* body) { + if (msg == NULL) { + return NULL_POINTER; + } + ((MQMessage*)msg)->setBody(body); + return OK; } -int SetByteMessageBody(CMessage *msg, const char *body, int len) { - if (msg == NULL) { - return NULL_POINTER; - } - ((MQMessage *)msg)->setBody(body,len); - return OK; +int SetByteMessageBody(CMessage* msg, const char* body, int len) { + if (msg == NULL) { + return NULL_POINTER; + } + ((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); - return OK; +int SetMessageProperty(CMessage* msg, const char* key, const char* value) { + if (msg == NULL) { + return NULL_POINTER; + } + ((MQMessage*)msg)->setProperty(key, value); + return OK; } -int SetDelayTimeLevel(CMessage *msg, int level){ - if (msg == NULL) { - return NULL_POINTER; - } - ((MQMessage *)msg)->setDelayTimeLevel(level); - return OK; +int SetDelayTimeLevel(CMessage* msg, int level) { + if (msg == NULL) { + return NULL_POINTER; + } + ((MQMessage*)msg)->setDelayTimeLevel(level); + return OK; } #ifdef __cplusplus diff --git a/src/extern/CMessageExt.cpp b/src/extern/CMessageExt.cpp index 5d612d84b..03a5e2dcd 100644 --- a/src/extern/CMessageExt.cpp +++ b/src/extern/CMessageExt.cpp @@ -23,104 +23,104 @@ extern "C" { #endif using namespace rocketmq; -const char *GetMessageTopic(CMessageExt *msg) { - if (msg == NULL) { - return NULL; - } - return ((MQMessageExt *) msg)->getTopic().c_str(); +const char* GetMessageTopic(CMessageExt* msg) { + if (msg == NULL) { + return NULL; + } + return ((MQMessageExt*)msg)->getTopic().c_str(); } -const char *GetMessageTags(CMessageExt *msg) { - if (msg == NULL) { - return NULL; - } - return ((MQMessageExt *) msg)->getTags().c_str(); +const char* GetMessageTags(CMessageExt* msg) { + if (msg == NULL) { + return NULL; + } + return ((MQMessageExt*)msg)->getTags().c_str(); } -const char *GetMessageKeys(CMessageExt *msg) { - if (msg == NULL) { - return NULL; - } - return ((MQMessageExt *) msg)->getKeys().c_str(); +const char* GetMessageKeys(CMessageExt* msg) { + if (msg == NULL) { + return NULL; + } + return ((MQMessageExt*)msg)->getKeys().c_str(); } -const char *GetMessageBody(CMessageExt *msg) { - if (msg == NULL) { - return NULL; - } - return ((MQMessageExt *) msg)->getBody().c_str(); +const char* GetMessageBody(CMessageExt* msg) { + if (msg == NULL) { + return NULL; + } + 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(); +const char* GetMessageProperty(CMessageExt* msg, const char* key) { + if (msg == NULL) { + return NULL; + } + return ((MQMessageExt*)msg)->getProperty(key).c_str(); } -const char *GetMessageId(CMessageExt *msg) { - if (msg == NULL) { - return NULL; - } - return ((MQMessageExt *) msg)->getMsgId().c_str(); +const char* GetMessageId(CMessageExt* msg) { + if (msg == NULL) { + return NULL; + } + return ((MQMessageExt*)msg)->getMsgId().c_str(); } -int GetMessageDelayTimeLevel(CMessageExt *msg){ - if (msg == NULL) { - return NULL_POINTER; - } - return ((MQMessageExt *) msg)->getDelayTimeLevel(); +int GetMessageDelayTimeLevel(CMessageExt* msg) { + if (msg == NULL) { + return NULL_POINTER; + } + return ((MQMessageExt*)msg)->getDelayTimeLevel(); } -int GetMessageQueueId(CMessageExt *msg){ - if (msg == NULL) { - return NULL_POINTER; - } - return ((MQMessageExt *) msg)->getQueueId(); +int GetMessageQueueId(CMessageExt* msg) { + if (msg == NULL) { + return NULL_POINTER; + } + return ((MQMessageExt*)msg)->getQueueId(); } -int GetMessageReconsumeTimes(CMessageExt *msg){ - if (msg == NULL) { - return NULL_POINTER; - } - return ((MQMessageExt *) msg)->getReconsumeTimes(); +int GetMessageReconsumeTimes(CMessageExt* msg) { + if (msg == NULL) { + return NULL_POINTER; + } + return ((MQMessageExt*)msg)->getReconsumeTimes(); } -int GetMessageStoreSize(CMessageExt *msg){ - if (msg == NULL) { - return NULL_POINTER; - } - return ((MQMessageExt *) msg)->getStoreSize(); +int GetMessageStoreSize(CMessageExt* msg) { + if (msg == NULL) { + return NULL_POINTER; + } + return ((MQMessageExt*)msg)->getStoreSize(); } -long long GetMessageBornTimestamp(CMessageExt *msg) { - if (msg == NULL) { - return NULL_POINTER; - } - return ((MQMessageExt *) msg)->getBornTimestamp(); +long long GetMessageBornTimestamp(CMessageExt* msg) { + if (msg == NULL) { + return NULL_POINTER; + } + return ((MQMessageExt*)msg)->getBornTimestamp(); } -long long GetMessageStoreTimestamp(CMessageExt *msg){ - if (msg == NULL) { - return NULL_POINTER; - } - return ((MQMessageExt *) msg)->getStoreTimestamp(); +long long GetMessageStoreTimestamp(CMessageExt* msg) { + if (msg == NULL) { + return NULL_POINTER; + } + return ((MQMessageExt*)msg)->getStoreTimestamp(); } -long long GetMessageQueueOffset(CMessageExt *msg){ - if (msg == NULL) { - return NULL_POINTER; - } - return ((MQMessageExt *) msg)->getQueueOffset(); +long long GetMessageQueueOffset(CMessageExt* msg) { + if (msg == NULL) { + return NULL_POINTER; + } + return ((MQMessageExt*)msg)->getQueueOffset(); } -long long GetMessageCommitLogOffset(CMessageExt *msg){ - if (msg == NULL) { - return NULL_POINTER; - } - return ((MQMessageExt *) msg)->getCommitLogOffset(); +long long GetMessageCommitLogOffset(CMessageExt* msg) { + if (msg == NULL) { + return NULL_POINTER; + } + return ((MQMessageExt*)msg)->getCommitLogOffset(); } -long long GetMessagePreparedTransactionOffset(CMessageExt *msg){ - if (msg == NULL) { - return NULL_POINTER; - } - return ((MQMessageExt *) msg)->getPreparedTransactionOffset(); +long long GetMessagePreparedTransactionOffset(CMessageExt* msg) { + if (msg == NULL) { + return NULL_POINTER; + } + return ((MQMessageExt*)msg)->getPreparedTransactionOffset(); } #ifdef __cplusplus }; diff --git a/src/extern/CProducer.cpp b/src/extern/CProducer.cpp index 58542a68f..59ee184ea 100644 --- a/src/extern/CProducer.cpp +++ b/src/extern/CProducer.cpp @@ -32,315 +32,317 @@ using namespace rocketmq; using namespace std; class SelectMessageQueue : public MessageQueueSelector { -public: - SelectMessageQueue(QueueSelectorCallback callback) { - m_pCallback = callback; - } + public: + 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. - int index = m_pCallback(mqs.size(), message, arg); - return mqs[index]; - } + 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]; + } -private: - QueueSelectorCallback m_pCallback; + private: + QueueSelectorCallback m_pCallback; }; -class CSendCallback : public AutoDeleteSendCallBack{ -public: - CSendCallback(CSendSuccessCallback cSendSuccessCallback,CSendExceptionCallback cSendExceptionCallback){ - m_cSendSuccessCallback = cSendSuccessCallback; - m_cSendExceptionCallback = cSendExceptionCallback; - } - virtual ~CSendCallback(){} - virtual void onSuccess(SendResult& sendResult) { - CSendResult result; - 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) { - CMQException exception; - exception.error = e.GetError(); - 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 ); - } -private: - CSendSuccessCallback m_cSendSuccessCallback; - CSendExceptionCallback m_cSendExceptionCallback; +class CSendCallback : public AutoDeleteSendCallBack { + public: + CSendCallback(CSendSuccessCallback cSendSuccessCallback, CSendExceptionCallback cSendExceptionCallback) { + m_cSendSuccessCallback = cSendSuccessCallback; + m_cSendExceptionCallback = cSendExceptionCallback; + } + virtual ~CSendCallback() {} + virtual void onSuccess(SendResult& sendResult) { + CSendResult result; + 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) { + CMQException exception; + exception.error = e.GetError(); + 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); + } + + private: + CSendSuccessCallback m_cSendSuccessCallback; + CSendExceptionCallback m_cSendExceptionCallback; }; -CProducer *CreateProducer(const char *groupId) { - if (groupId == NULL) { - return NULL; - } - DefaultMQProducer *defaultMQProducer = new DefaultMQProducer(groupId); - return (CProducer *) defaultMQProducer; +CProducer* CreateProducer(const char* groupId) { + if (groupId == NULL) { + return NULL; + } + DefaultMQProducer* defaultMQProducer = new DefaultMQProducer(groupId); + return (CProducer*)defaultMQProducer; } -int DestroyProducer(CProducer *pProducer) { - if (pProducer == NULL) { - return NULL_POINTER; - } - delete reinterpret_cast(pProducer); - return OK; +int DestroyProducer(CProducer* pProducer) { + if (pProducer == NULL) { + return NULL_POINTER; + } + delete reinterpret_cast(pProducer); + return OK; } -int StartProducer(CProducer *producer) { - if (producer == NULL) { - return NULL_POINTER; - } - try { - ((DefaultMQProducer *) producer)->start(); - } catch (exception &e) { - return PRODUCER_START_FAILED; - } - return OK; +int StartProducer(CProducer* producer) { + if (producer == NULL) { + return NULL_POINTER; + } + try { + ((DefaultMQProducer*)producer)->start(); + } catch (exception& e) { + return PRODUCER_START_FAILED; + } + return OK; } -int ShutdownProducer(CProducer *producer) { - if (producer == NULL) { - return NULL_POINTER; - } - ((DefaultMQProducer *) producer)->shutdown(); - return OK; +int ShutdownProducer(CProducer* producer) { + if (producer == NULL) { + return NULL_POINTER; + } + ((DefaultMQProducer*)producer)->shutdown(); + return OK; } -int SetProducerNameServerAddress(CProducer *producer, const char *namesrv) { - if (producer == NULL) { - return NULL_POINTER; - } - ((DefaultMQProducer *) producer)->setNamesrvAddr(namesrv); - return OK; +int SetProducerNameServerAddress(CProducer* producer, const char* namesrv) { + if (producer == NULL) { + return NULL_POINTER; + } + ((DefaultMQProducer*)producer)->setNamesrvAddr(namesrv); + return OK; } -int SetProducerNameServerDomain(CProducer *producer, const char *domain) { - if (producer == NULL) { - return NULL_POINTER; - } - ((DefaultMQProducer *) producer)->setNamesrvDomain(domain); - return OK; +int SetProducerNameServerDomain(CProducer* producer, const char* domain) { + if (producer == NULL) { + return NULL_POINTER; + } + ((DefaultMQProducer*)producer)->setNamesrvDomain(domain); + return OK; } -int SendMessageSync(CProducer *producer, CMessage *msg, CSendResult *result) { - //CSendResult sendResult; - if (producer == NULL || msg == NULL || result == NULL) { - return NULL_POINTER; - } - try { - DefaultMQProducer *defaultMQProducer = (DefaultMQProducer *) producer; - MQMessage *message = (MQMessage *) msg; - SendResult sendResult = defaultMQProducer->send(*message); - switch (sendResult.getSendStatus()) { - case SEND_OK: - result->sendStatus = E_SEND_OK; - break; - case SEND_FLUSH_DISK_TIMEOUT: - result->sendStatus = E_SEND_FLUSH_DISK_TIMEOUT; - break; - case SEND_FLUSH_SLAVE_TIMEOUT: - result->sendStatus = E_SEND_FLUSH_SLAVE_TIMEOUT; - break; - case SEND_SLAVE_NOT_AVAILABLE: - result->sendStatus = E_SEND_SLAVE_NOT_AVAILABLE; - break; - default: - result->sendStatus = E_SEND_OK; - break; - } - 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) { - return PRODUCER_SEND_SYNC_FAILED; - } - return OK; +int SendMessageSync(CProducer* producer, CMessage* msg, CSendResult* result) { + // CSendResult sendResult; + if (producer == NULL || msg == NULL || result == NULL) { + return NULL_POINTER; + } + try { + DefaultMQProducer* defaultMQProducer = (DefaultMQProducer*)producer; + MQMessage* message = (MQMessage*)msg; + SendResult sendResult = defaultMQProducer->send(*message); + switch (sendResult.getSendStatus()) { + case SEND_OK: + result->sendStatus = E_SEND_OK; + break; + case SEND_FLUSH_DISK_TIMEOUT: + result->sendStatus = E_SEND_FLUSH_DISK_TIMEOUT; + break; + case SEND_FLUSH_SLAVE_TIMEOUT: + result->sendStatus = E_SEND_FLUSH_SLAVE_TIMEOUT; + break; + case SEND_SLAVE_NOT_AVAILABLE: + result->sendStatus = E_SEND_SLAVE_NOT_AVAILABLE; + break; + default: + result->sendStatus = E_SEND_OK; + break; + } + 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) { + return PRODUCER_SEND_SYNC_FAILED; + } + return OK; } -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); +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); - try { - defaultMQProducer->send(*message ,cSendCallback); - } catch (exception &e) { - if(cSendCallback != NULL){ - if(typeid(e) == typeid( MQException )){ - MQException &mqe = (MQException &)e; - cSendCallback->onException( mqe ); - } - delete cSendCallback; - cSendCallback = NULL; - } - return PRODUCER_SEND_ASYNC_FAILED; - } - return OK; + try { + defaultMQProducer->send(*message, cSendCallback); + } catch (exception& e) { + if (cSendCallback != NULL) { + if (typeid(e) == typeid(MQException)) { + MQException& mqe = (MQException&)e; + cSendCallback->onException(mqe); + } + delete cSendCallback; + cSendCallback = NULL; + } + return PRODUCER_SEND_ASYNC_FAILED; + } + return OK; } -int SendMessageOneway(CProducer *producer, CMessage *msg) { - if (producer == NULL || msg == NULL) { - return NULL_POINTER; - } - DefaultMQProducer *defaultMQProducer = (DefaultMQProducer *) producer; - MQMessage *message = (MQMessage *) msg; - try { - defaultMQProducer->sendOneway(*message); - } catch (exception &e) { - return PRODUCER_SEND_ONEWAY_FAILED; - } - return OK; +int SendMessageOneway(CProducer* producer, CMessage* msg) { + if (producer == NULL || msg == NULL) { + return NULL_POINTER; + } + DefaultMQProducer* defaultMQProducer = (DefaultMQProducer*)producer; + MQMessage* message = (MQMessage*)msg; + try { + defaultMQProducer->sendOneway(*message); + } catch (exception& e) { + return PRODUCER_SEND_ONEWAY_FAILED; + } + return OK; } -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; - try { - SelectMessageQueue selectMessageQueue(selector); - defaultMQProducer->sendOneway(*message, &selectMessageQueue, arg); - } catch (exception &e) { - return PRODUCER_SEND_ONEWAY_FAILED; - } - return OK; +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; + try { + SelectMessageQueue selectMessageQueue(selector); + defaultMQProducer->sendOneway(*message, &selectMessageQueue, arg); + } 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) { - return NULL_POINTER; - } - DefaultMQProducer *defaultMQProducer = (DefaultMQProducer *) producer; - MQMessage *message = (MQMessage *) msg; - CSendCallback* cSendCallback = new CSendCallback(cSendSuccessCallback , cSendExceptionCallback); +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); - try { - //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()); - 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) { - return PRODUCER_SEND_ORDERLY_FAILED; - } - return OK; +int SendMessageOrderly(CProducer* producer, + CMessage* msg, + QueueSelectorCallback callback, + void* arg, + int autoRetryTimes, + CSendResult* result) { + if (producer == NULL || msg == NULL || callback == NULL || arg == NULL || result == NULL) { + return NULL_POINTER; + } + DefaultMQProducer* defaultMQProducer = (DefaultMQProducer*)producer; + MQMessage* message = (MQMessage*)msg; + try { + // Constructing SelectMessageQueue objects through function pointer callback + SelectMessageQueue selectMessageQueue(callback); + SendResult sendResult = defaultMQProducer->send(*message, &selectMessageQueue, arg, autoRetryTimes); + // 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) { + return PRODUCER_SEND_ORDERLY_FAILED; + } + return OK; } -int SetProducerGroupName(CProducer *producer, const char *groupName) { - if (producer == NULL) { - return NULL_POINTER; - } - ((DefaultMQProducer *) producer)->setGroupName(groupName); - return OK; +int SetProducerGroupName(CProducer* producer, const char* groupName) { + if (producer == NULL) { + return NULL_POINTER; + } + ((DefaultMQProducer*)producer)->setGroupName(groupName); + return OK; } -int SetProducerInstanceName(CProducer *producer, const char *instanceName) { - if (producer == NULL) { - return NULL_POINTER; - } - ((DefaultMQProducer *) producer)->setInstanceName(instanceName); - return OK; +int SetProducerInstanceName(CProducer* producer, const char* instanceName) { + if (producer == NULL) { + return NULL_POINTER; + } + ((DefaultMQProducer*)producer)->setInstanceName(instanceName); + return OK; } -int SetProducerSessionCredentials(CProducer *producer, const char *accessKey, const char *secretKey, - const char *onsChannel) { - if (producer == NULL) { - return NULL_POINTER; - } - ((DefaultMQProducer *) producer)->setSessionCredentials(accessKey, secretKey, onsChannel); - return OK; +int SetProducerSessionCredentials(CProducer* producer, + const char* accessKey, + const char* secretKey, + const char* onsChannel) { + if (producer == NULL) { + return NULL_POINTER; + } + ((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. - //((DefaultMQProducer *) producer)->setLogFileSizeAndNum(3, 102400000); - return OK; +int SetProducerLogPath(CProducer* producer, const char* logPath) { + if (producer == NULL) { + return NULL_POINTER; + } + // Todo, This api should be implemented by core api. + //((DefaultMQProducer *) producer)->setLogFileSizeAndNum(3, 102400000); + return OK; } -int SetProducerLogFileNumAndSize(CProducer *producer, int fileNum, long fileSize) { - if (producer == NULL) { - return NULL_POINTER; - } - ((DefaultMQProducer *) producer)->setLogFileSizeAndNum(fileNum, fileSize); - return OK; +int SetProducerLogFileNumAndSize(CProducer* producer, int fileNum, long fileSize) { + if (producer == NULL) { + return NULL_POINTER; + } + ((DefaultMQProducer*)producer)->setLogFileSizeAndNum(fileNum, fileSize); + return OK; } -int SetProducerLogLevel(CProducer *producer, CLogLevel level) { - if (producer == NULL) { - return NULL_POINTER; - } - ((DefaultMQProducer *) producer)->setLogLevel((elogLevel) level); - return OK; +int SetProducerLogLevel(CProducer* producer, CLogLevel level) { + if (producer == NULL) { + return NULL_POINTER; + } + ((DefaultMQProducer*)producer)->setLogLevel((elogLevel)level); + return OK; } -int SetProducerSendMsgTimeout(CProducer *producer, int timeout) { - if (producer == NULL) { - return NULL_POINTER; - } - ((DefaultMQProducer *) producer)->setSendMsgTimeout(timeout); - return OK; +int SetProducerSendMsgTimeout(CProducer* producer, int timeout) { + if (producer == NULL) { + return NULL_POINTER; + } + ((DefaultMQProducer*)producer)->setSendMsgTimeout(timeout); + return OK; } -int SetProducerCompressMsgBodyOverHowmuch(CProducer *producer, int howmuch) { - if (producer == NULL) { - return NULL_POINTER; - } - ((DefaultMQProducer *) producer)->setCompressMsgBodyOverHowmuch(howmuch); - return OK; +int SetProducerCompressMsgBodyOverHowmuch(CProducer* producer, int howmuch) { + if (producer == NULL) { + return NULL_POINTER; + } + ((DefaultMQProducer*)producer)->setCompressMsgBodyOverHowmuch(howmuch); + return OK; } -int SetProducerCompressLevel(CProducer *producer, int level) { - if (producer == NULL) { - return NULL_POINTER; - } - ((DefaultMQProducer *) producer)->setCompressLevel(level); - return OK; +int SetProducerCompressLevel(CProducer* producer, int level) { + if (producer == NULL) { + return NULL_POINTER; + } + ((DefaultMQProducer*)producer)->setCompressLevel(level); + return OK; } -int SetProducerMaxMessageSize(CProducer *producer, int size) { - if (producer == NULL) { - return NULL_POINTER; - } - ((DefaultMQProducer *) producer)->setMaxMessageSize(size); - return OK; +int SetProducerMaxMessageSize(CProducer* producer, int size) { + if (producer == NULL) { + return NULL_POINTER; + } + ((DefaultMQProducer*)producer)->setMaxMessageSize(size); + return OK; } #ifdef __cplusplus }; diff --git a/src/extern/CPullConsumer.cpp b/src/extern/CPullConsumer.cpp index cf7c01f5f..a30937a4b 100644 --- a/src/extern/CPullConsumer.cpp +++ b/src/extern/CPullConsumer.cpp @@ -27,210 +27,213 @@ using namespace std; extern "C" { #endif - -CPullConsumer *CreatePullConsumer(const char *groupId) { - if (groupId == NULL) { - return NULL; - } - DefaultMQPullConsumer *defaultMQPullConsumer = new DefaultMQPullConsumer(groupId); - return (CPullConsumer *) defaultMQPullConsumer; -} -int DestroyPullConsumer(CPullConsumer *consumer) { - if (consumer == NULL) { - return NULL_POINTER; - } - delete reinterpret_cast(consumer); - return OK; -} -int StartPullConsumer(CPullConsumer *consumer) { - if (consumer == NULL) { - return NULL_POINTER; - } - try { - ((DefaultMQPullConsumer *) consumer)->start(); - } catch (exception &e) { - return PULLCONSUMER_START_FAILED; - } - return OK; -} -int ShutdownPullConsumer(CPullConsumer *consumer) { - if (consumer == NULL) { - return NULL_POINTER; - } - ((DefaultMQPullConsumer *) consumer)->shutdown(); - return OK; -} -int SetPullConsumerGroupID(CPullConsumer *consumer, const char *groupId) { - if (consumer == NULL || groupId == NULL) { - return NULL_POINTER; - } - ((DefaultMQPullConsumer *) consumer)->setGroupName(groupId); - return OK; -} -const char *GetPullConsumerGroupID(CPullConsumer *consumer) { - if (consumer == NULL) { - return NULL; - } - return ((DefaultMQPullConsumer *) consumer)->getGroupName().c_str(); -} -int SetPullConsumerNameServerAddress(CPullConsumer *consumer, const char *namesrv) { - if (consumer == NULL) { - return NULL_POINTER; - } - ((DefaultMQPullConsumer *) consumer)->setNamesrvAddr(namesrv); - return OK; -} -int SetPullConsumerNameServerDomain(CPullConsumer *consumer, const char *domain) { - if (consumer == NULL) { - return NULL_POINTER; - } - ((DefaultMQPullConsumer *) consumer)->setNamesrvDomain(domain); - return OK; -} -int SetPullConsumerSessionCredentials(CPullConsumer *consumer, const char *accessKey, const char *secretKey, - const char *channel) { - if (consumer == NULL) { - return NULL_POINTER; - } - ((DefaultMQPullConsumer *) consumer)->setSessionCredentials(accessKey, secretKey, channel); - return OK; +CPullConsumer* CreatePullConsumer(const char* groupId) { + if (groupId == NULL) { + return NULL; + } + DefaultMQPullConsumer* defaultMQPullConsumer = new DefaultMQPullConsumer(groupId); + return (CPullConsumer*)defaultMQPullConsumer; +} +int DestroyPullConsumer(CPullConsumer* consumer) { + if (consumer == NULL) { + return NULL_POINTER; + } + delete reinterpret_cast(consumer); + return OK; +} +int StartPullConsumer(CPullConsumer* consumer) { + if (consumer == NULL) { + return NULL_POINTER; + } + try { + ((DefaultMQPullConsumer*)consumer)->start(); + } catch (exception& e) { + return PULLCONSUMER_START_FAILED; + } + return OK; +} +int ShutdownPullConsumer(CPullConsumer* consumer) { + if (consumer == NULL) { + return NULL_POINTER; + } + ((DefaultMQPullConsumer*)consumer)->shutdown(); + return OK; +} +int SetPullConsumerGroupID(CPullConsumer* consumer, const char* groupId) { + if (consumer == NULL || groupId == NULL) { + return NULL_POINTER; + } + ((DefaultMQPullConsumer*)consumer)->setGroupName(groupId); + return OK; +} +const char* GetPullConsumerGroupID(CPullConsumer* consumer) { + if (consumer == NULL) { + return NULL; + } + return ((DefaultMQPullConsumer*)consumer)->getGroupName().c_str(); +} +int SetPullConsumerNameServerAddress(CPullConsumer* consumer, const char* namesrv) { + if (consumer == NULL) { + return NULL_POINTER; + } + ((DefaultMQPullConsumer*)consumer)->setNamesrvAddr(namesrv); + return OK; +} +int SetPullConsumerNameServerDomain(CPullConsumer* consumer, const char* domain) { + if (consumer == NULL) { + return NULL_POINTER; + } + ((DefaultMQPullConsumer*)consumer)->setNamesrvDomain(domain); + return OK; +} +int SetPullConsumerSessionCredentials(CPullConsumer* consumer, + const char* accessKey, + const char* secretKey, + const char* channel) { + if (consumer == NULL) { + return NULL_POINTER; + } + ((DefaultMQPullConsumer*)consumer)->setSessionCredentials(accessKey, secretKey, channel); + return OK; } -int SetPullConsumerLogPath(CPullConsumer *consumer, const char *logPath) { - if (consumer == NULL) { - return NULL_POINTER; - } - //Todo, This api should be implemented by core api. - //((DefaultMQPullConsumer *) consumer)->setInstanceName(instanceName); - return OK; +int SetPullConsumerLogPath(CPullConsumer* consumer, const char* logPath) { + if (consumer == NULL) { + return NULL_POINTER; + } + // Todo, This api should be implemented by core api. + //((DefaultMQPullConsumer *) consumer)->setInstanceName(instanceName); + return OK; } -int SetPullConsumerLogFileNumAndSize(CPullConsumer *consumer, int fileNum, long fileSize) { - if (consumer == NULL) { - return NULL_POINTER; - } - ((DefaultMQPullConsumer *) consumer)->setLogFileSizeAndNum(fileNum, fileSize); - return OK; +int SetPullConsumerLogFileNumAndSize(CPullConsumer* consumer, int fileNum, long fileSize) { + if (consumer == NULL) { + return NULL_POINTER; + } + ((DefaultMQPullConsumer*)consumer)->setLogFileSizeAndNum(fileNum, fileSize); + return OK; } -int SetPullConsumerLogLevel(CPullConsumer *consumer, CLogLevel level) { - if (consumer == NULL) { - return NULL_POINTER; - } - ((DefaultMQPullConsumer *) consumer)->setLogLevel((elogLevel) level); - return OK; -} - -int FetchSubscriptionMessageQueues(CPullConsumer *consumer, const char *topic, CMessageQueue **mqs, int *size) { - if (consumer == NULL) { - return NULL_POINTER; - } - unsigned int index = 0; - CMessageQueue *temMQ = NULL; - std::vector fullMQ; - try { - ((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)); - if (temMQ == NULL) { - *size = 0; - *mqs = NULL; - return MALLOC_FAILED; - } - auto iter = fullMQ.begin(); - for (index = 0; iter != fullMQ.end() && index <= fullMQ.size(); ++iter, index++) { - strncpy(temMQ[index].topic, iter->getTopic().c_str(), MAX_TOPIC_LENGTH - 1); - strncpy(temMQ[index].brokerName, iter->getBrokerName().c_str(), MAX_BROKER_NAME_ID_LENGTH - 1); - temMQ[index].queueId = iter->getQueueId(); - } - *mqs = temMQ; - } catch (MQException &e) { - *size = 0; - *mqs = NULL; - return PULLCONSUMER_FETCH_MQ_FAILED; - } - return OK; +int SetPullConsumerLogLevel(CPullConsumer* consumer, CLogLevel level) { + if (consumer == NULL) { + return NULL_POINTER; + } + ((DefaultMQPullConsumer*)consumer)->setLogLevel((elogLevel)level); + return OK; } -int ReleaseSubscriptionMessageQueue(CMessageQueue *mqs) { - if (mqs == NULL) { - return NULL_POINTER; - } - free((void *) mqs); - mqs = NULL; - return OK; -} -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.pullStatus = BROKER_TIMEOUT; - } - if(cppPullResult.pullStatus != BROKER_TIMEOUT){ - pullResult.maxOffset = cppPullResult.maxOffset; - pullResult.minOffset = cppPullResult.minOffset; - pullResult.nextBeginOffset = cppPullResult.nextBeginOffset; - } +int FetchSubscriptionMessageQueues(CPullConsumer* consumer, const char* topic, CMessageQueue** mqs, int* size) { + if (consumer == NULL) { + return NULL_POINTER; + } + unsigned int index = 0; + CMessageQueue* temMQ = NULL; + std::vector fullMQ; + try { + ((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)); + if (temMQ == NULL) { + *size = 0; + *mqs = NULL; + return MALLOC_FAILED; + } + auto iter = fullMQ.begin(); + for (index = 0; iter != fullMQ.end() && index <= fullMQ.size(); ++iter, index++) { + strncpy(temMQ[index].topic, iter->getTopic().c_str(), MAX_TOPIC_LENGTH - 1); + strncpy(temMQ[index].brokerName, iter->getBrokerName().c_str(), MAX_BROKER_NAME_ID_LENGTH - 1); + temMQ[index].queueId = iter->getQueueId(); + } + *mqs = temMQ; + } catch (MQException& e) { + *size = 0; + *mqs = NULL; + return PULLCONSUMER_FETCH_MQ_FAILED; + } + return OK; +} +int ReleaseSubscriptionMessageQueue(CMessageQueue* mqs) { + if (mqs == NULL) { + return NULL_POINTER; + } + free((void*)mqs); + mqs = NULL; + return OK; +} +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.pullStatus = BROKER_TIMEOUT; + } - switch (cppPullResult.pullStatus) { - case FOUND: { - pullResult.pullStatus = E_FOUND; - 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 *)); - for (size_t i = 0; i < cppPullResult.msgFoundList.size(); i++) { - MQMessageExt *msg = const_cast(&tmpPullResult->msgFoundList[i]); - pullResult.msgFoundList[i] = (CMessageExt *) (msg); - } - break; - } - case NO_NEW_MSG: { - pullResult.pullStatus = E_NO_NEW_MSG; - break; - } - case NO_MATCHED_MSG: { - pullResult.pullStatus = E_NO_MATCHED_MSG; - break; - } - case OFFSET_ILLEGAL: { - pullResult.pullStatus = E_OFFSET_ILLEGAL; - break; - } - case BROKER_TIMEOUT: { - pullResult.pullStatus = E_BROKER_TIMEOUT; - break; - } - default: - pullResult.pullStatus = E_NO_NEW_MSG; - break; + if (cppPullResult.pullStatus != BROKER_TIMEOUT) { + pullResult.maxOffset = cppPullResult.maxOffset; + pullResult.minOffset = cppPullResult.minOffset; + pullResult.nextBeginOffset = cppPullResult.nextBeginOffset; + } - } - return pullResult; + switch (cppPullResult.pullStatus) { + case FOUND: { + pullResult.pullStatus = E_FOUND; + 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*)); + for (size_t i = 0; i < cppPullResult.msgFoundList.size(); i++) { + MQMessageExt* msg = const_cast(&tmpPullResult->msgFoundList[i]); + pullResult.msgFoundList[i] = (CMessageExt*)(msg); + } + break; + } + case NO_NEW_MSG: { + pullResult.pullStatus = E_NO_NEW_MSG; + break; + } + case NO_MATCHED_MSG: { + pullResult.pullStatus = E_NO_MATCHED_MSG; + break; + } + case OFFSET_ILLEGAL: { + pullResult.pullStatus = E_OFFSET_ILLEGAL; + break; + } + case BROKER_TIMEOUT: { + pullResult.pullStatus = E_BROKER_TIMEOUT; + break; + } + default: + pullResult.pullStatus = E_NO_NEW_MSG; + break; + } + return pullResult; } int ReleasePullResult(CPullResult pullResult) { - if (pullResult.size == 0 || pullResult.msgFoundList == NULL || pullResult.pData == NULL) { - return NULL_POINTER; - } - if (pullResult.pData != NULL) { - try { - delete ((PullResult *) pullResult.pData); - } catch (exception &e) { - return NULL_POINTER; - } - } - free((void *) pullResult.msgFoundList); - pullResult.msgFoundList = NULL; - return OK; + if (pullResult.size == 0 || pullResult.msgFoundList == NULL || pullResult.pData == NULL) { + return NULL_POINTER; + } + if (pullResult.pData != NULL) { + try { + delete ((PullResult*)pullResult.pData); + } catch (exception& e) { + return NULL_POINTER; + } + } + free((void*)pullResult.msgFoundList); + pullResult.msgFoundList = NULL; + return OK; } #ifdef __cplusplus diff --git a/src/extern/CPushConsumer.cpp b/src/extern/CPushConsumer.cpp index 2c35c7491..9a6fe192e 100644 --- a/src/extern/CPushConsumer.cpp +++ b/src/extern/CPushConsumer.cpp @@ -25,251 +25,251 @@ using namespace rocketmq; using namespace std; class MessageListenerInner : public MessageListenerConcurrently { -public: - MessageListenerInner() {} + public: + MessageListenerInner() {} - MessageListenerInner(CPushConsumer *consumer, MessageCallBack pCallback) { - m_pconsumer = consumer; - m_pMsgReceiveCallback = pCallback; - } + MessageListenerInner(CPushConsumer* consumer, MessageCallBack pCallback) { + m_pconsumer = consumer; + m_pMsgReceiveCallback = pCallback; + } - ~MessageListenerInner() {} + ~MessageListenerInner() {} - ConsumeStatus consumeMessage(const std::vector &msgs) { - //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; - } - return CONSUME_SUCCESS; + ConsumeStatus consumeMessage(const std::vector& msgs) { + // 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; } + return CONSUME_SUCCESS; + } -private: - MessageCallBack m_pMsgReceiveCallback; - CPushConsumer *m_pconsumer; + private: + MessageCallBack m_pMsgReceiveCallback; + CPushConsumer* m_pconsumer; }; class MessageListenerOrderlyInner : public MessageListenerOrderly { -public: - MessageListenerOrderlyInner(CPushConsumer *consumer, MessageCallBack pCallback) { - m_pconsumer = consumer; - m_pMsgReceiveCallback = pCallback; - } + public: + MessageListenerOrderlyInner(CPushConsumer* consumer, MessageCallBack pCallback) { + m_pconsumer = consumer; + m_pMsgReceiveCallback = pCallback; + } - ConsumeStatus consumeMessage(const std::vector &msgs) { - 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; - } - return CONSUME_SUCCESS; + ConsumeStatus consumeMessage(const std::vector& msgs) { + 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; } + return CONSUME_SUCCESS; + } -private: - MessageCallBack m_pMsgReceiveCallback; - CPushConsumer *m_pconsumer; + private: + MessageCallBack m_pMsgReceiveCallback; + CPushConsumer* m_pconsumer; }; -map g_ListenerMap; -map g_OrderListenerMap; +map g_ListenerMap; +map g_OrderListenerMap; #ifdef __cplusplus 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; +CPushConsumer* CreatePushConsumer(const char* groupId) { + if (groupId == NULL) { + return NULL; + } + DefaultMQPushConsumer* defaultMQPushConsumer = new DefaultMQPushConsumer(groupId); + defaultMQPushConsumer->setConsumeFromWhere(CONSUME_FROM_LAST_OFFSET); + return (CPushConsumer*)defaultMQPushConsumer; } -int DestroyPushConsumer(CPushConsumer *consumer) { - if (consumer == NULL) { - return NULL_POINTER; - } - delete reinterpret_cast(consumer); - return OK; +int DestroyPushConsumer(CPushConsumer* consumer) { + if (consumer == NULL) { + return NULL_POINTER; + } + delete reinterpret_cast(consumer); + return OK; } -int StartPushConsumer(CPushConsumer *consumer) { - if (consumer == NULL) { - return NULL_POINTER; - } - try { - ((DefaultMQPushConsumer *) consumer)->start(); - } catch (exception &e) { - return PUSHCONSUMER_START_FAILED; - } - return OK; +int StartPushConsumer(CPushConsumer* consumer) { + if (consumer == NULL) { + return NULL_POINTER; + } + try { + ((DefaultMQPushConsumer*)consumer)->start(); + } catch (exception& e) { + return PUSHCONSUMER_START_FAILED; + } + return OK; } -int ShutdownPushConsumer(CPushConsumer *consumer) { - if (consumer == NULL) { - return NULL_POINTER; - } - ((DefaultMQPushConsumer *) consumer)->shutdown(); - return OK; +int ShutdownPushConsumer(CPushConsumer* consumer) { + if (consumer == NULL) { + return NULL_POINTER; + } + ((DefaultMQPushConsumer*)consumer)->shutdown(); + return OK; } -int SetPushConsumerGroupID(CPushConsumer *consumer, const char *groupId) { - if (consumer == NULL || groupId == NULL) { - return NULL_POINTER; - } - ((DefaultMQPushConsumer *) consumer)->setGroupName(groupId); - return OK; +int SetPushConsumerGroupID(CPushConsumer* consumer, const char* groupId) { + if (consumer == NULL || groupId == NULL) { + return NULL_POINTER; + } + ((DefaultMQPushConsumer*)consumer)->setGroupName(groupId); + return OK; } -const char *GetPushConsumerGroupID(CPushConsumer *consumer) { - if (consumer == NULL) { - return NULL; - } - return ((DefaultMQPushConsumer *) consumer)->getGroupName().c_str(); +const char* GetPushConsumerGroupID(CPushConsumer* consumer) { + if (consumer == NULL) { + return NULL; + } + return ((DefaultMQPushConsumer*)consumer)->getGroupName().c_str(); } -int SetPushConsumerNameServerAddress(CPushConsumer *consumer, const char *namesrv) { - if (consumer == NULL) { - return NULL_POINTER; - } - ((DefaultMQPushConsumer *) consumer)->setNamesrvAddr(namesrv); - return OK; +int SetPushConsumerNameServerAddress(CPushConsumer* consumer, const char* namesrv) { + if (consumer == NULL) { + return NULL_POINTER; + } + ((DefaultMQPushConsumer*)consumer)->setNamesrvAddr(namesrv); + return OK; } -int SetPushConsumerNameServerDomain(CPushConsumer *consumer, const char *domain) { - if (consumer == NULL) { - return NULL_POINTER; - } - ((DefaultMQPushConsumer *) consumer)->setNamesrvDomain(domain); - return OK; +int SetPushConsumerNameServerDomain(CPushConsumer* consumer, const char* domain) { + if (consumer == NULL) { + return NULL_POINTER; + } + ((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); - return OK; +int Subscribe(CPushConsumer* consumer, const char* topic, const char* expression) { + if (consumer == NULL) { + return NULL_POINTER; + } + ((DefaultMQPushConsumer*)consumer)->subscribe(topic, expression); + return OK; } -int RegisterMessageCallback(CPushConsumer *consumer, MessageCallBack pCallback) { - if (consumer == NULL || pCallback == NULL) { - return NULL_POINTER; - } - MessageListenerInner *listenerInner = new MessageListenerInner(consumer, pCallback); - ((DefaultMQPushConsumer *) consumer)->registerMessageListener(listenerInner); - g_ListenerMap[consumer] = listenerInner; - return OK; +int RegisterMessageCallback(CPushConsumer* consumer, MessageCallBack pCallback) { + if (consumer == NULL || pCallback == NULL) { + return NULL_POINTER; + } + MessageListenerInner* listenerInner = new MessageListenerInner(consumer, pCallback); + ((DefaultMQPushConsumer*)consumer)->registerMessageListener(listenerInner); + g_ListenerMap[consumer] = listenerInner; + return OK; } -int RegisterMessageCallbackOrderly(CPushConsumer *consumer, MessageCallBack pCallback) { - if (consumer == NULL || pCallback == NULL) { - return NULL_POINTER; - } - MessageListenerOrderlyInner *messageListenerOrderlyInner = new MessageListenerOrderlyInner(consumer, pCallback); - ((DefaultMQPushConsumer *) consumer)->registerMessageListener(messageListenerOrderlyInner); - g_OrderListenerMap[consumer] = messageListenerOrderlyInner; - return OK; +int RegisterMessageCallbackOrderly(CPushConsumer* consumer, MessageCallBack pCallback) { + if (consumer == NULL || pCallback == NULL) { + return NULL_POINTER; + } + MessageListenerOrderlyInner* messageListenerOrderlyInner = new MessageListenerOrderlyInner(consumer, pCallback); + ((DefaultMQPushConsumer*)consumer)->registerMessageListener(messageListenerOrderlyInner); + g_OrderListenerMap[consumer] = messageListenerOrderlyInner; + return OK; } - -int UnregisterMessageCallbackOrderly(CPushConsumer *consumer) { - if (consumer == NULL) { - return NULL_POINTER; +int UnregisterMessageCallbackOrderly(CPushConsumer* consumer) { + if (consumer == NULL) { + return NULL_POINTER; + } + map::iterator iter; + iter = g_OrderListenerMap.find(consumer); + if (iter != g_OrderListenerMap.end()) { + MessageListenerOrderlyInner* listenerInner = iter->second; + if (listenerInner != NULL) { + delete listenerInner; } - map::iterator iter; - iter = g_OrderListenerMap.find(consumer); - if (iter != g_OrderListenerMap.end()) { - MessageListenerOrderlyInner *listenerInner = iter->second; - if (listenerInner != NULL) { - delete listenerInner; - } - g_OrderListenerMap.erase(iter); - } - return OK; + g_OrderListenerMap.erase(iter); + } + return OK; } -int UnregisterMessageCallback(CPushConsumer *consumer) { - if (consumer == NULL) { - return NULL_POINTER; - } - map::iterator iter; - iter = g_ListenerMap.find(consumer); +int UnregisterMessageCallback(CPushConsumer* consumer) { + if (consumer == NULL) { + return NULL_POINTER; + } + map::iterator iter; + iter = g_ListenerMap.find(consumer); - if (iter != g_ListenerMap.end()) { - MessageListenerInner *listenerInner = iter->second; - if (listenerInner != NULL) { - delete listenerInner; - } - g_ListenerMap.erase(iter); + if (iter != g_ListenerMap.end()) { + MessageListenerInner* listenerInner = iter->second; + if (listenerInner != NULL) { + delete listenerInner; } - return OK; + g_ListenerMap.erase(iter); + } + return OK; } -int SetPushConsumerMessageModel(CPushConsumer *consumer, CMessageModel messageModel) { - if (consumer == NULL) { - return NULL_POINTER; - } - ((DefaultMQPushConsumer *) consumer)->setMessageModel(MessageModel((int) messageModel)); - return OK; +int SetPushConsumerMessageModel(CPushConsumer* consumer, CMessageModel messageModel) { + if (consumer == NULL) { + return NULL_POINTER; + } + ((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); - return OK; +int SetPushConsumerThreadCount(CPushConsumer* consumer, int threadCount) { + if (consumer == NULL || threadCount == 0) { + return NULL_POINTER; + } + ((DefaultMQPushConsumer*)consumer)->setConsumeThreadCount(threadCount); + return OK; } -int SetPushConsumerMessageBatchMaxSize(CPushConsumer *consumer, int batchSize) { - if (consumer == NULL || batchSize == 0) { - return NULL_POINTER; - } - ((DefaultMQPushConsumer *) consumer)->setConsumeMessageBatchMaxSize(batchSize); - return OK; +int SetPushConsumerMessageBatchMaxSize(CPushConsumer* consumer, int batchSize) { + if (consumer == NULL || batchSize == 0) { + return NULL_POINTER; + } + ((DefaultMQPushConsumer*)consumer)->setConsumeMessageBatchMaxSize(batchSize); + return OK; } -int SetPushConsumerInstanceName(CPushConsumer *consumer, const char *instanceName) { - if (consumer == NULL) { - return NULL_POINTER; - } - ((DefaultMQPushConsumer *) consumer)->setInstanceName(instanceName); - return OK; +int SetPushConsumerInstanceName(CPushConsumer* consumer, const char* instanceName) { + if (consumer == NULL) { + return NULL_POINTER; + } + ((DefaultMQPushConsumer*)consumer)->setInstanceName(instanceName); + return OK; } -int SetPushConsumerSessionCredentials(CPushConsumer *consumer, const char *accessKey, const char *secretKey, - const char *channel) { - if (consumer == NULL) { - return NULL_POINTER; - } - ((DefaultMQPushConsumer *) consumer)->setSessionCredentials(accessKey, secretKey, channel); - return OK; +int SetPushConsumerSessionCredentials(CPushConsumer* consumer, + const char* accessKey, + const char* secretKey, + const char* channel) { + if (consumer == NULL) { + return NULL_POINTER; + } + ((DefaultMQPushConsumer*)consumer)->setSessionCredentials(accessKey, secretKey, channel); + return OK; } -int SetPushConsumerLogPath(CPushConsumer *consumer, const char *logPath) { - if (consumer == NULL) { - return NULL_POINTER; - } - //Todo, This api should be implemented by core api. - //((DefaultMQPushConsumer *) consumer)->setInstanceName(instanceName); - return OK; +int SetPushConsumerLogPath(CPushConsumer* consumer, const char* logPath) { + if (consumer == NULL) { + return NULL_POINTER; + } + // Todo, This api should be implemented by core api. + //((DefaultMQPushConsumer *) consumer)->setInstanceName(instanceName); + return OK; } -int SetPushConsumerLogFileNumAndSize(CPushConsumer *consumer, int fileNum, long fileSize) { - if (consumer == NULL) { - return NULL_POINTER; - } - ((DefaultMQPushConsumer *) consumer)->setLogFileSizeAndNum(fileNum, fileSize); - return OK; +int SetPushConsumerLogFileNumAndSize(CPushConsumer* consumer, int fileNum, long fileSize) { + if (consumer == NULL) { + return NULL_POINTER; + } + ((DefaultMQPushConsumer*)consumer)->setLogFileSizeAndNum(fileNum, fileSize); + return OK; } -int SetPushConsumerLogLevel(CPushConsumer *consumer, CLogLevel level) { - if (consumer == NULL) { - return NULL_POINTER; - } - ((DefaultMQPushConsumer *) consumer)->setLogLevel((elogLevel) level); - return OK; +int SetPushConsumerLogLevel(CPushConsumer* consumer, CLogLevel level) { + if (consumer == NULL) { + return NULL_POINTER; + } + ((DefaultMQPushConsumer*)consumer)->setLogLevel((elogLevel)level); + return OK; } #ifdef __cplusplus 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..6e6a939ba --- a/src/log/Logging.cpp +++ b/src/log/Logging.cpp @@ -23,7 +23,9 @@ namespace rocketmq { logAdapter* logAdapter::alogInstance; boost::mutex logAdapter::m_imtx; -logAdapter::~logAdapter() { logging::core::get()->remove_all_sinks(); } +logAdapter::~logAdapter() { + logging::core::get()->remove_all_sinks(); +} logAdapter* logAdapter::getLogInstance() { if (alogInstance == NULL) { @@ -39,25 +41,19 @@ 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"; + 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::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(); } @@ -66,49 +62,43 @@ 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); + 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); + 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); + 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); + 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); + 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); + logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::trace); break; default: - logging::core::get()->set_filter(logging::trivial::severity >= - logging::trivial::info); + logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info); break; } } -elogLevel logAdapter::getLogLevel() { return m_logLevel; } +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)); + 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..57291484b --- a/src/log/Logging.h +++ b/src/log/Logging.h @@ -47,10 +47,7 @@ class logAdapter { void setLogLevel(elogLevel logLevel); elogLevel getLogLevel(); void setLogFileNumAndSize(int logNum, int sizeOfPerFile); - src::severity_logger& - getSeverityLogger() { - return m_severityLogger; - } + src::severity_logger& getSeverityLogger() { return m_severityLogger; } private: logAdapter(); @@ -69,8 +66,7 @@ class logAdapter { class LogUtil { public: - static void LogMessage(boost::log::trivial::severity_level level, int line, - const char* format, ...) { + 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]); @@ -80,15 +76,10 @@ class LogUtil { } }; -#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..ea8849ff9 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..9e60c4db7 --- a/src/message/MQDecoder.cpp +++ b/src/message/MQDecoder.cpp @@ -39,7 +39,7 @@ int MQDecoder::MessageStoreTimestampPostion = 56; //sin_addr.s_addr); @@ -47,19 +47,18 @@ string MQDecoder::createMessageId(sockaddr addr, int64 offset) { outputmen.write(&(sa->sin_port), 2); outputmen.writeInt64BigEndian(offset); - const char *bytes = static_cast(outputmen.getData()); + const char* bytes = static_cast(outputmen.getData()); int len = outputmen.getDataSize(); return UtilAll::bytes2string(bytes, len); } -MQMessageId MQDecoder::decodeMessageId(const string &msgId) { - +MQMessageId MQDecoder::decodeMessageId(const string& msgId) { string ipStr = msgId.substr(0, 8); string portStr = msgId.substr(8, 8); string offsetStr = msgId.substr(16); - char *end; + char* end; int ipInt = strtoul(ipStr.c_str(), &end, 16); int portInt = strtoul(portStr.c_str(), &end, 16); @@ -70,16 +69,16 @@ MQMessageId MQDecoder::decodeMessageId(const string &msgId) { sa.sin_port = htons(portInt); sa.sin_addr.s_addr = htonl(ipInt); - MQMessageId id(*((sockaddr*) &sa), offset); + MQMessageId id(*((sockaddr*)&sa), offset); return id; } -MQMessageExt *MQDecoder::decode(MemoryInputStream &byteBuffer) { +MQMessageExt* MQDecoder::decode(MemoryInputStream& byteBuffer) { return decode(byteBuffer, true); } -MQMessageExt *MQDecoder::decode(MemoryInputStream &byteBuffer, bool readBody) { - MQMessageExt *msgExt = new MQMessageExt(); +MQMessageExt* MQDecoder::decode(MemoryInputStream& byteBuffer, bool readBody) { + MQMessageExt* msgExt = new MQMessageExt(); // 1 TOTALSIZE int storeSize = byteBuffer.readIntBigEndian(); @@ -147,7 +146,7 @@ MQMessageExt *MQDecoder::decode(MemoryInputStream &byteBuffer, bool readBody) { MemoryBlock block; byteBuffer.readIntoMemoryBlock(block, bodyLen); - const char *const pBody = static_cast(block.getData()); + const char* const pBody = static_cast(block.getData()); int len = block.getSize(); string msgbody(pBody, len); @@ -166,10 +165,10 @@ MQMessageExt *MQDecoder::decode(MemoryInputStream &byteBuffer, bool readBody) { } // 16 TOPIC - int topicLen = (int) byteBuffer.readByte(); + int topicLen = (int)byteBuffer.readByte(); MemoryBlock block; byteBuffer.readIntoMemoryBlock(block, topicLen); - const char *const pTopic = static_cast(block.getData()); + const char* const pTopic = static_cast(block.getData()); topicLen = block.getSize(); msgExt->setTopic(pTopic, topicLen); @@ -178,7 +177,7 @@ MQMessageExt *MQDecoder::decode(MemoryInputStream &byteBuffer, bool readBody) { if (propertiesLen > 0) { MemoryBlock block; byteBuffer.readIntoMemoryBlock(block, propertiesLen); - const char *const pProperty = static_cast(block.getData()); + const char* const pProperty = static_cast(block.getData()); int len = block.getSize(); string propertiesString(pProperty, len); @@ -189,7 +188,7 @@ MQMessageExt *MQDecoder::decode(MemoryInputStream &byteBuffer, bool readBody) { } // 18 msg ID - string offsetMsgId = createMessageId(msgExt->getStoreHost(), (int64) msgExt->getCommitLogOffset()); + string offsetMsgId = createMessageId(msgExt->getStoreHost(), (int64)msgExt->getCommitLogOffset()); msgExt->setOffsetMsgId(offsetMsgId); string msgId = msgExt->getProperty(MQMessage::PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX); @@ -202,12 +201,12 @@ MQMessageExt *MQDecoder::decode(MemoryInputStream &byteBuffer, bool readBody) { return msgExt; } -void MQDecoder::decodes(const MemoryBlock *mem, vector &mqvec) { +void MQDecoder::decodes(const MemoryBlock* mem, vector& mqvec) { mqvec.clear(); decodes(mem, mqvec, true); } -void MQDecoder::decodes(const MemoryBlock *mem, vector &mqvec, bool readBody) { +void MQDecoder::decodes(const MemoryBlock* mem, vector& mqvec, bool readBody) { MemoryInputStream rawInput(*mem, true); while (rawInput.getNumBytesRemaining() > 0) { @@ -216,10 +215,10 @@ void MQDecoder::decodes(const MemoryBlock *mem, vector &mqvec, boo } } -string MQDecoder::messageProperties2String(const map &properties) { +string MQDecoder::messageProperties2String(const map& properties) { string os; - for (const auto &it : properties) { + for (const auto& it : properties) { // os << it->first << NAME_VALUE_SEPARATOR << it->second << PROPERTY_SEPARATOR; os.append(it.first); os += NAME_VALUE_SEPARATOR; @@ -230,7 +229,7 @@ string MQDecoder::messageProperties2String(const map &properties return os; } -void MQDecoder::string2messageProperties(const string &propertiesString, map &properties) { +void MQDecoder::string2messageProperties(const string& propertiesString, map& properties) { vector out; UtilAll::Split(out, propertiesString, PROPERTY_SEPARATOR); diff --git a/src/message/MQDecoder.h b/src/message/MQDecoder.h old mode 100755 new mode 100644 diff --git a/src/message/MQMessage.cpp b/src/message/MQMessage.cpp old mode 100755 new mode 100644 index e816879f0..83fe68da7 --- a/src/message/MQMessage.cpp +++ b/src/message/MQMessage.cpp @@ -52,29 +52,34 @@ const string MQMessage::KEY_SEPARATOR = " "; static const string EMPTY_STRING = ""; //::const_iterator it = m_properties.find(name); if (it == m_properties.end()) { return EMPTY_STRING; @@ -119,22 +124,30 @@ const string & MQMessage::getProperty(const string& name) const { } } -const string& MQMessage::getTopic() const { return m_topic; } +const string& MQMessage::getTopic() const { + return m_topic; +} -void MQMessage::setTopic(const string& topic) { m_topic = 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); } -const string& MQMessage::getTags() const { return getProperty(PROPERTY_TAGS); } +const string& MQMessage::getTags() const { + return getProperty(PROPERTY_TAGS); +} void MQMessage::setTags(const string& tags) { setPropertyInternal(PROPERTY_TAGS, tags); } -const string& MQMessage::getKeys() const { return getProperty(PROPERTY_KEYS); } +const string& MQMessage::getKeys() const { + return getProperty(PROPERTY_KEYS); +} void MQMessage::setKeys(const string& keys) { setPropertyInternal(PROPERTY_KEYS, keys); @@ -190,27 +203,39 @@ void MQMessage::setWaitStoreMsgOK(bool waitStoreMsgOK) { } } -int MQMessage::getFlag() const { return m_flag; } +int MQMessage::getFlag() const { + return m_flag; +} -void MQMessage::setFlag(int flag) { m_flag = flag; } +void MQMessage::setFlag(int flag) { + m_flag = flag; +} -int MQMessage::getSysFlag() const { return m_sysFlag; } +int MQMessage::getSysFlag() const { + return m_sysFlag; +} -void MQMessage::setSysFlag(int sysFlag) { m_sysFlag = sysFlag; } +void MQMessage::setSysFlag(int sysFlag) { + m_sysFlag = sysFlag; +} -const string& MQMessage::getBody() const { return m_body; } +const string& MQMessage::getBody() const { + return m_body; +} void MQMessage::setBody(const char* body, int len) { m_body.clear(); m_body.append(body, len); } -void MQMessage::setBody(const string &body) { +void MQMessage::setBody(const string& body) { m_body.clear(); m_body.append(body); } -map MQMessage::getProperties() const { return m_properties; } +map MQMessage::getProperties() const { + return m_properties; +} void MQMessage::setProperties(map& properties) { m_properties = properties; @@ -230,8 +255,11 @@ 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; diff --git a/src/message/MQMessageExt.cpp b/src/message/MQMessageExt.cpp old mode 100755 new mode 100644 index 43f4bf05f..fc022a2b2 --- a/src/message/MQMessageExt.cpp +++ b/src/message/MQMessageExt.cpp @@ -33,8 +33,11 @@ MQMessageExt::MQMessageExt() m_reconsumeTimes(3), m_msgId("") {} -MQMessageExt::MQMessageExt(int queueId, int64 bornTimestamp, sockaddr bornHost, - int64 storeTimestamp, sockaddr storeHost, +MQMessageExt::MQMessageExt(int queueId, + int64 bornTimestamp, + sockaddr bornHost, + int64 storeTimestamp, + sockaddr storeHost, string msgId) : m_queueOffset(0), m_commitLogOffset(0), @@ -51,17 +54,25 @@ MQMessageExt::MQMessageExt(int queueId, int64 bornTimestamp, sockaddr bornHost, MQMessageExt::~MQMessageExt() {} -int MQMessageExt::getQueueId() const { return m_queueId; } +int MQMessageExt::getQueueId() const { + return m_queueId; +} -void MQMessageExt::setQueueId(int queueId) { m_queueId = queueId; } +void MQMessageExt::setQueueId(int queueId) { + m_queueId = queueId; +} -int64 MQMessageExt::getBornTimestamp() const { return m_bornTimestamp; } +int64 MQMessageExt::getBornTimestamp() const { + return m_bornTimestamp; +} void MQMessageExt::setBornTimestamp(int64 bornTimestamp) { m_bornTimestamp = bornTimestamp; } -sockaddr MQMessageExt::getBornHost() const { return m_bornHost; } +sockaddr MQMessageExt::getBornHost() const { + return m_bornHost; +} string MQMessageExt::getBornHostString() const { return socketAddress2String(m_bornHost); @@ -75,13 +86,17 @@ void MQMessageExt::setBornHost(const sockaddr& bornHost) { m_bornHost = bornHost; } -int64 MQMessageExt::getStoreTimestamp() const { return m_storeTimestamp; } +int64 MQMessageExt::getStoreTimestamp() const { + return m_storeTimestamp; +} void MQMessageExt::setStoreTimestamp(int64 storeTimestamp) { m_storeTimestamp = storeTimestamp; } -sockaddr MQMessageExt::getStoreHost() const { return m_storeHost; } +sockaddr MQMessageExt::getStoreHost() const { + return m_storeHost; +} string MQMessageExt::getStoreHostString() const { return socketAddress2String(m_storeHost); @@ -91,43 +106,64 @@ void MQMessageExt::setStoreHost(const sockaddr& storeHost) { m_storeHost = storeHost; } -const string& MQMessageExt::getMsgId() const { return m_msgId; } +const string& MQMessageExt::getMsgId() const { + return m_msgId; +} -void MQMessageExt::setMsgId(const string& msgId) { m_msgId = msgId; } +void MQMessageExt::setMsgId(const string& msgId) { + m_msgId = msgId; +} -const string& MQMessageExt::getOffsetMsgId() const { return m_offsetMsgId; } +const string& MQMessageExt::getOffsetMsgId() const { + return m_offsetMsgId; +} -void MQMessageExt::setOffsetMsgId(const string& offsetMsgId) { m_offsetMsgId = offsetMsgId; } +void MQMessageExt::setOffsetMsgId(const string& offsetMsgId) { + m_offsetMsgId = offsetMsgId; +} -int MQMessageExt::getBodyCRC() const { return m_bodyCRC; } +int MQMessageExt::getBodyCRC() const { + return m_bodyCRC; +} -void MQMessageExt::setBodyCRC(int bodyCRC) { m_bodyCRC = bodyCRC; } +void MQMessageExt::setBodyCRC(int bodyCRC) { + m_bodyCRC = bodyCRC; +} -int64 MQMessageExt::getQueueOffset() const { return m_queueOffset; } +int64 MQMessageExt::getQueueOffset() const { + return m_queueOffset; +} void MQMessageExt::setQueueOffset(int64 queueOffset) { m_queueOffset = queueOffset; } -int64 MQMessageExt::getCommitLogOffset() const { return m_commitLogOffset; } +int64 MQMessageExt::getCommitLogOffset() const { + return m_commitLogOffset; +} void MQMessageExt::setCommitLogOffset(int64 physicOffset) { m_commitLogOffset = physicOffset; } -int MQMessageExt::getStoreSize() const { return m_storeSize; } +int MQMessageExt::getStoreSize() const { + return m_storeSize; +} -void MQMessageExt::setStoreSize(int storeSize) { m_storeSize = storeSize; } +void MQMessageExt::setStoreSize(int storeSize) { + m_storeSize = storeSize; +} int MQMessageExt::parseTopicFilterType(int sysFlag) { - if ((sysFlag & MessageSysFlag::MultiTagsFlag) == - MessageSysFlag::MultiTagsFlag) { + if ((sysFlag & MessageSysFlag::MultiTagsFlag) == MessageSysFlag::MultiTagsFlag) { return MULTI_TAG; } return SINGLE_TAG; } -int MQMessageExt::getReconsumeTimes() const { return m_reconsumeTimes; } +int MQMessageExt::getReconsumeTimes() const { + return m_reconsumeTimes; +} void MQMessageExt::setReconsumeTimes(int reconsumeTimes) { m_reconsumeTimes = reconsumeTimes; @@ -137,8 +173,7 @@ int64 MQMessageExt::getPreparedTransactionOffset() const { return m_preparedTransactionOffset; } -void MQMessageExt::setPreparedTransactionOffset( - int64 preparedTransactionOffset) { +void MQMessageExt::setPreparedTransactionOffset(int64 preparedTransactionOffset) { m_preparedTransactionOffset = preparedTransactionOffset; } diff --git a/src/message/MQMessageId.h b/src/message/MQMessageId.h old mode 100755 new mode 100644 index 366ac204b..38d11eeae --- a/src/message/MQMessageId.h +++ b/src/message/MQMessageId.h @@ -24,8 +24,7 @@ namespace rocketmq { //start(); @@ -118,8 +116,7 @@ SendResult DefaultMQProducer::send(MQMessage& msg, bool bSelectActiveBroker) { return SendResult(); } -void DefaultMQProducer::send(MQMessage& msg, SendCallback* pSendCallback, - bool bSelectActiveBroker) { +void DefaultMQProducer::send(MQMessage& msg, SendCallback* pSendCallback, bool bSelectActiveBroker) { Validators::checkMessage(msg, getMaxMessageSize()); try { sendDefaultImpl(msg, ComMode_ASYNC, pSendCallback, bSelectActiveBroker); @@ -154,7 +151,6 @@ SendResult DefaultMQProducer::send(std::vector& msgs, const MQMessage } BatchMessage DefaultMQProducer::buildBatchMessage(std::vector& msgs) { - if (msgs.size() < 1) { THROW_MQEXCEPTION(MQClientException, "msgs need one message at least", -1); } @@ -173,7 +169,6 @@ BatchMessage DefaultMQProducer::buildBatchMessage(std::vector& msgs) 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); } @@ -205,8 +200,7 @@ SendResult DefaultMQProducer::send(MQMessage& msg, const MQMessageQueue& mq) { return SendResult(); } -void DefaultMQProducer::send(MQMessage& msg, const MQMessageQueue& mq, - SendCallback* pSendCallback) { +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"); @@ -242,8 +236,7 @@ void DefaultMQProducer::sendOneway(MQMessage& msg, const MQMessageQueue& mq) { } } -SendResult DefaultMQProducer::send(MQMessage& msg, - MessageQueueSelector* pSelector, void* arg) { +SendResult DefaultMQProducer::send(MQMessage& msg, MessageQueueSelector* pSelector, void* arg) { try { return sendSelectImpl(msg, pSelector, arg, ComMode_SYNC, NULL); } catch (MQException& e) { @@ -254,11 +247,12 @@ SendResult DefaultMQProducer::send(MQMessage& msg, } SendResult DefaultMQProducer::send(MQMessage& msg, - MessageQueueSelector* pSelector, void* arg, - int autoRetryTimes, bool bActiveBroker) { + MessageQueueSelector* pSelector, + void* arg, + int autoRetryTimes, + bool bActiveBroker) { try { - return sendAutoRetrySelectImpl(msg, pSelector, arg, ComMode_SYNC, NULL, - autoRetryTimes, bActiveBroker); + return sendAutoRetrySelectImpl(msg, pSelector, arg, ComMode_SYNC, NULL, autoRetryTimes, bActiveBroker); } catch (MQException& e) { LOG_ERROR(e.what()); throw e; @@ -266,8 +260,7 @@ SendResult DefaultMQProducer::send(MQMessage& msg, return SendResult(); } -void DefaultMQProducer::send(MQMessage& msg, MessageQueueSelector* pSelector, - void* arg, SendCallback* pSendCallback) { +void DefaultMQProducer::send(MQMessage& msg, MessageQueueSelector* pSelector, void* arg, SendCallback* pSendCallback) { try { sendSelectImpl(msg, pSelector, arg, ComMode_ASYNC, pSendCallback); } catch (MQException& e) { @@ -276,8 +269,7 @@ void DefaultMQProducer::send(MQMessage& msg, MessageQueueSelector* pSelector, } } -void DefaultMQProducer::sendOneway(MQMessage& msg, - MessageQueueSelector* pSelector, void* arg) { +void DefaultMQProducer::sendOneway(MQMessage& msg, MessageQueueSelector* pSelector, void* arg) { try { sendSelectImpl(msg, pSelector, arg, ComMode_ONEWAY, NULL); } catch (MQException& e) { @@ -286,7 +278,9 @@ void DefaultMQProducer::sendOneway(MQMessage& msg, } } -int DefaultMQProducer::getSendMsgTimeout() const { return m_sendMsgTimeout; } +int DefaultMQProducer::getSendMsgTimeout() const { + return m_sendMsgTimeout; +} void DefaultMQProducer::setSendMsgTimeout(int sendMsgTimeout) { m_sendMsgTimeout = sendMsgTimeout; @@ -296,18 +290,21 @@ int DefaultMQProducer::getCompressMsgBodyOverHowmuch() const { return m_compressMsgBodyOverHowmuch; } -void DefaultMQProducer::setCompressMsgBodyOverHowmuch( - int compressMsgBodyOverHowmuch) { +void DefaultMQProducer::setCompressMsgBodyOverHowmuch(int compressMsgBodyOverHowmuch) { m_compressMsgBodyOverHowmuch = compressMsgBodyOverHowmuch; } -int DefaultMQProducer::getMaxMessageSize() const { return m_maxMessageSize; } +int DefaultMQProducer::getMaxMessageSize() const { + return m_maxMessageSize; +} void DefaultMQProducer::setMaxMessageSize(int maxMessageSize) { m_maxMessageSize = maxMessageSize; } -int DefaultMQProducer::getCompressLevel() const { return m_compressLevel; } +int DefaultMQProducer::getCompressLevel() const { + return m_compressLevel; +} void DefaultMQProducer::setCompressLevel(int compressLevel) { assert((compressLevel >= 0 && compressLevel <= 9) || compressLevel == -1); @@ -324,10 +321,8 @@ SendResult DefaultMQProducer::sendDefaultImpl(MQMessage& msg, 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()); + getFactory()->tryToFindTopicPublishInfo(msg.getTopic(), getSessionCredentials())); + boost::shared_ptr topicPublishInfo(weak_topicPublishInfo.lock()); if (topicPublishInfo) { if (times == 1) { mq_index = topicPublishInfo->getWhichQueue(); @@ -360,8 +355,7 @@ SendResult DefaultMQProducer::sendDefaultImpl(MQMessage& msg, case ComMode_SYNC: if (sendResult.getSendStatus() != SEND_OK) { if (bActiveMQ) { - topicPublishInfo->updateNonServiceMessageQueue( - mq, getSendMsgTimeout()); + topicPublishInfo->updateNonServiceMessageQueue(mq, getSendMsgTimeout()); } continue; } @@ -370,11 +364,9 @@ SendResult DefaultMQProducer::sendDefaultImpl(MQMessage& msg, break; } } catch (...) { - LOG_ERROR("send failed of times:%d,brokerName:%s", times, - mq.getBrokerName().c_str()); + LOG_ERROR("send failed of times:%d,brokerName:%s", times, mq.getBrokerName().c_str()); if (bActiveMQ) { - topicPublishInfo->updateNonServiceMessageQueue(mq, - getSendMsgTimeout()); + topicPublishInfo->updateNonServiceMessageQueue(mq, getSendMsgTimeout()); } continue; } @@ -389,12 +381,10 @@ 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()); + getFactory()->tryToFindTopicPublishInfo(mq.getTopic(), getSessionCredentials()); brokerAddr = getFactory()->findBrokerAddressInPublish(mq.getBrokerName()); } @@ -402,7 +392,7 @@ SendResult DefaultMQProducer::sendKernelImpl(MQMessage& msg, try { BatchMessage batchMessage; bool isBatchMsg = (typeid(msg).name() == typeid(batchMessage).name()); - //msgId is produced by client, offsetMsgId produced by broker. (same with java sdk) + // 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); @@ -422,45 +412,43 @@ SendResult DefaultMQProducer::sendKernelImpl(MQMessage& msg, requestHeader->bornTimestamp = UtilAll::currentTimeMillis(); requestHeader->flag = (msg.getFlag()); requestHeader->batch = isBatchMsg; - requestHeader->properties = - (MQDecoder::messageProperties2String(msg.getProperties())); + requestHeader->properties = (MQDecoder::messageProperties2String(msg.getProperties())); - return getFactory()->getMQClientAPIImpl()->sendMessage( - brokerAddr, mq.getBrokerName(), msg, requestHeader, - getSendMsgTimeout(), getRetryTimes4Async(), communicationMode, sendCallback, - getSessionCredentials()); + 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); + THROW_MQEXCEPTION(MQClientException, "The broker[" + mq.getBrokerName() + "] not exist", -1); } SendResult DefaultMQProducer::sendSelectImpl(MQMessage& msg, MessageQueueSelector* pSelector, - void* pArg, int communicationMode, + 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()); + getFactory()->tryToFindTopicPublishInfo(msg.getTopic(), getSessionCredentials())); + boost::shared_ptr topicPublishInfo(weak_topicPublishInfo.lock()); if (topicPublishInfo) //&& topicPublishInfo->ok()) { - MQMessageQueue mq = - pSelector->select(topicPublishInfo->getMessageQueueList(), msg, pArg); + 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) { +SendResult DefaultMQProducer::sendAutoRetrySelectImpl(MQMessage& msg, + MessageQueueSelector* pSelector, + void* pArg, + int communicationMode, + SendCallback* pSendCallback, + int autoRetryTimes, + bool bActiveMQ) { Validators::checkMessage(msg, getMaxMessageSize()); MQMessageQueue lastmq; @@ -468,22 +456,20 @@ SendResult DefaultMQProducer::sendAutoRetrySelectImpl( 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()); + 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); + 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 (mqs[i] == lastmq) + mq_index = i; } if (bActiveMQ) mq = topicPublishInfo->selectOneActiveMessageQueue(lastmq, mq_index); @@ -508,8 +494,7 @@ SendResult DefaultMQProducer::sendAutoRetrySelectImpl( case ComMode_SYNC: if (sendResult.getSendStatus() != SEND_OK) { if (bActiveMQ) { - topicPublishInfo->updateNonServiceMessageQueue( - mq, getSendMsgTimeout()); + topicPublishInfo->updateNonServiceMessageQueue(mq, getSendMsgTimeout()); } continue; } @@ -518,11 +503,9 @@ SendResult DefaultMQProducer::sendAutoRetrySelectImpl( break; } } catch (...) { - LOG_ERROR("send failed of times:%d,mq:%s", times, - mq.toString().c_str()); + LOG_ERROR("send failed of times:%d,mq:%s", times, mq.toString().c_str()); if (bActiveMQ) { - topicPublishInfo->updateNonServiceMessageQueue(mq, - getSendMsgTimeout()); + topicPublishInfo->updateNonServiceMessageQueue(mq, getSendMsgTimeout()); } continue; } @@ -550,7 +533,9 @@ bool DefaultMQProducer::tryToCompressMessage(MQMessage& msg) { return false; } -int DefaultMQProducer::getRetryTimes() const { return m_retryTimes; } +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"); @@ -566,12 +551,10 @@ void DefaultMQProducer::setRetryTimes(int times) { m_retryTimes = times; } -int DefaultMQProducer::getRetryTimes4Async() const -{ - return m_retryTimes4Async; +int DefaultMQProducer::getRetryTimes4Async() const { + return m_retryTimes4Async; } -void DefaultMQProducer::setRetryTimes4Async(int times) -{ +void DefaultMQProducer::setRetryTimes4Async(int times) { if (times <= 0) { LOG_WARN("set retry times illegal, use default value:1"); m_retryTimes4Async = 1; diff --git a/src/producer/SendResult.cpp b/src/producer/SendResult.cpp old mode 100755 new mode 100644 index 9eabf5deb..81ddf7661 --- a/src/producer/SendResult.cpp +++ b/src/producer/SendResult.cpp @@ -22,8 +22,11 @@ namespace rocketmq { //tm_year; mon_begin.tm_mon = ptmNow->tm_mon; @@ -154,11 +153,11 @@ void StringIdMaker::set_start_and_next_tm() { } int StringIdMaker::atomic_incr(int id) { - #ifdef WIN32 - InterlockedIncrement((LONG*)&id); - #else - __sync_add_and_fetch(&id, 1); - #endif +#ifdef WIN32 + InterlockedIncrement((LONG*)&id); +#else + __sync_add_and_fetch(&id, 1); +#endif return id; } std::string StringIdMaker::get_unique_id() { @@ -171,7 +170,7 @@ std::string StringIdMaker::get_unique_id() { seqid = atomic_incr(seqid) & 0xFF; std::size_t prifix_len = 10; // 10 = prefix len - unsigned char *write_index = _buff + prifix_len; + unsigned char* write_index = _buff + prifix_len; memcpy(write_index, &tm_period, 4); write_index += 4; @@ -183,7 +182,7 @@ std::string StringIdMaker::get_unique_id() { return std::string(_0x_buff); } -void StringIdMaker::hexdump(unsigned char *buffer, char *out_buff, unsigned long index) { +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]); } diff --git a/src/producer/StringIdMaker.h b/src/producer/StringIdMaker.h index e95b744a4..b693787c7 100644 --- a/src/producer/StringIdMaker.h +++ b/src/producer/StringIdMaker.h @@ -61,7 +61,7 @@ class StringIdMaker : public boost::serialization::singleton { 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; diff --git a/src/producer/TopicPublishInfo.h b/src/producer/TopicPublishInfo.h index 4b4fbb483..5f0380e68 100644 --- a/src/producer/TopicPublishInfo.h +++ b/src/producer/TopicPublishInfo.h @@ -32,8 +32,7 @@ namespace rocketmq { class TopicPublishInfo { public: TopicPublishInfo() : m_sendWhichQueue(0) { - m_async_service_thread.reset(new boost::thread( - boost::bind(&TopicPublishInfo::boost_asio_work, this))); + m_async_service_thread.reset(new boost::thread(boost::bind(&TopicPublishInfo::boost_asio_work, this))); } void boost_asio_work() { @@ -42,10 +41,8 @@ class TopicPublishInfo { // after first timer // timeout callback boost::system::error_code e; - boost::asio::deadline_timer t(m_async_ioService, - boost::posix_time::seconds(60)); - t.async_wait(boost::bind( - &TopicPublishInfo::op_resumeNonServiceMessageQueueList, this, e, &t)); + boost::asio::deadline_timer t(m_async_ioService, boost::posix_time::seconds(60)); + t.async_wait(boost::bind(&TopicPublishInfo::op_resumeNonServiceMessageQueueList, this, e, &t)); boost::system::error_code ec; m_async_ioService.run(ec); } @@ -79,23 +76,18 @@ class TopicPublishInfo { } } - void op_resumeNonServiceMessageQueueList(boost::system::error_code& ec, - boost::asio::deadline_timer* 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)); + 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) { + 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()); + string key = it->first.getBrokerName() + UtilAll::to_string(it->first.getQueueId()); if (m_nonSerivceQueues.find(key) != m_nonSerivceQueues.end()) { m_nonSerivceQueues.erase(key); } @@ -104,8 +96,7 @@ class TopicPublishInfo { } } - void updateNonServiceMessageQueue(const MQMessageQueue& mq, - int timeoutMilliseconds) { + void updateNonServiceMessageQueue(const MQMessageQueue& mq, int timeoutMilliseconds) { boost::lock_guard lock(m_queuelock); string key = mq.getBrokerName() + UtilAll::to_string(mq.getQueueId()); @@ -125,17 +116,13 @@ class TopicPublishInfo { 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) { + 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()); + 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(); @@ -145,12 +132,12 @@ class TopicPublishInfo { } 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)()) { + 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); @@ -165,8 +152,7 @@ class TopicPublishInfo { LOG_ERROR("could not find property mq"); return MQMessageQueue(); } else { - if (m_sendWhichQueue.load(boost::memory_order_acquire) == - (numeric_limits::max)()) { + if (m_sendWhichQueue.load(boost::memory_order_acquire) == (numeric_limits::max)()) { m_sendWhichQueue.store(0, boost::memory_order_release); } @@ -182,8 +168,7 @@ class TopicPublishInfo { } } - MQMessageQueue selectOneActiveMessageQueue(const MQMessageQueue& lastmq, - int& mq_index) { + MQMessageQueue selectOneActiveMessageQueue(const MQMessageQueue& lastmq, int& mq_index) { boost::lock_guard lock(m_queuelock); if (m_queues.size() > 0) { @@ -196,12 +181,12 @@ class TopicPublishInfo { } 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)()) { + 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); @@ -214,8 +199,7 @@ class TopicPublishInfo { ++pos; } - for (MQMAP::iterator it = m_nonSerivceQueues.begin(); - it != m_nonSerivceQueues.end(); + 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) @@ -225,11 +209,11 @@ class TopicPublishInfo { return MQMessageQueue(); } else { for (size_t i = 0; i < m_queues.size(); i++) { - if (m_sendWhichQueue.load(boost::memory_order_acquire) == - (numeric_limits::max)()) { + 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; LOG_DEBUG("lastmq broker empty, m_sendWhichQueue:%d, pos:%d", @@ -244,8 +228,7 @@ class TopicPublishInfo { } } - for (MQMAP::iterator it = m_nonSerivceQueues.begin(); - it != m_nonSerivceQueues.end(); + 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) diff --git a/src/protocol/CommandHeader.cpp b/src/protocol/CommandHeader.cpp index 6290ac7b6..025d5c6ef 100644 --- a/src/protocol/CommandHeader.cpp +++ b/src/protocol/CommandHeader.cpp @@ -27,8 +27,7 @@ void GetRouteInfoRequestHeader::Encode(Json::Value& outData) { outData["topic"] = topic; } -void GetRouteInfoRequestHeader::SetDeclaredFieldOfCommandHeader( - map& requestMap) { +void GetRouteInfoRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { requestMap.insert(pair("topic", topic)); } //& requestMap) { +void UnregisterClientRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { requestMap.insert(pair("clientID", clientID)); requestMap.insert(pair("producerGroup", producerGroup)); requestMap.insert(pair("consumerGroup", consumerGroup)); @@ -53,14 +51,11 @@ void CreateTopicRequestHeader::Encode(Json::Value& outData) { outData["perm"] = perm; outData["topicFilterType"] = topicFilterType; } -void CreateTopicRequestHeader::SetDeclaredFieldOfCommandHeader( - map& requestMap) { +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("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)); } @@ -83,44 +78,37 @@ void SendMessageRequestHeader::Encode(Json::Value& outData) { outData["batch"] = batch; } -int SendMessageRequestHeader::getReconsumeTimes() { return reconsumeTimes; } +int SendMessageRequestHeader::getReconsumeTimes() { + return reconsumeTimes; +} void SendMessageRequestHeader::setReconsumeTimes(int input_reconsumeTimes) { reconsumeTimes = input_reconsumeTimes; } -void SendMessageRequestHeader::SetDeclaredFieldOfCommandHeader( - map& requestMap) { +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()); + 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("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))); } @@ -146,13 +134,10 @@ CommandHeader* SendMessageResponseHeader::Decode(Json::Value& ext) { return h; } -void SendMessageResponseHeader::SetDeclaredFieldOfCommandHeader( - map& requestMap) { +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.insert(pair("queueId", UtilAll::to_string(queueId))); + requestMap.insert(pair("queueOffset", UtilAll::to_string(queueOffset))); } //& requestMap) { +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("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)); } //& 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))); +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) { +void GetMinOffsetRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { requestMap.insert(pair("topic", topic)); - requestMap.insert( - pair("queueId", UtilAll::to_string(queueId))); + requestMap.insert(pair("queueId", UtilAll::to_string(queueId))); } //& requestMap) { +void GetMinOffsetResponseHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { requestMap.insert(pair("offset", UtilAll::to_string(offset))); } //& requestMap) { +void GetMaxOffsetRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { requestMap.insert(pair("topic", topic)); - requestMap.insert( - pair("queueId", UtilAll::to_string(queueId))); + requestMap.insert(pair("queueId", UtilAll::to_string(queueId))); } //& requestMap) { +void GetMaxOffsetResponseHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { requestMap.insert(pair("offset", UtilAll::to_string(offset))); } //& requestMap) { +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))); + requestMap.insert(pair("queueId", UtilAll::to_string(queueId))); + requestMap.insert(pair("timestamp", UtilAll::to_string(timestamp))); } //& requestMap) { +void SearchOffsetResponseHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { requestMap.insert(pair("offset", UtilAll::to_string(offset))); } //& requestMap) { +void ViewMessageRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { requestMap.insert(pair("offset", UtilAll::to_string(offset))); } //& requestMap) { +void GetEarliestMsgStoretimeRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { requestMap.insert(pair("topic", topic)); - requestMap.insert( - pair("queueId", UtilAll::to_string(queueId))); + requestMap.insert(pair("queueId", UtilAll::to_string(queueId))); } //& requestMap) { - requestMap.insert( - pair("timestamp", UtilAll::to_string(timestamp))); +void GetEarliestMsgStoretimeResponseHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { + requestMap.insert(pair("timestamp", UtilAll::to_string(timestamp))); } //& requestMap) { +void GetConsumerListByGroupRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { requestMap.insert(pair("consumerGroup", consumerGroup)); } //& requestMap) { +void QueryConsumerOffsetRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { requestMap.insert(pair("consumerGroup", consumerGroup)); requestMap.insert(pair("topic", topic)); - requestMap.insert( - pair("queueId", UtilAll::to_string(queueId))); + requestMap.insert(pair("queueId", UtilAll::to_string(queueId))); } //offset = UtilAll::str2ll(tempValue.asCString()); @@ -395,8 +344,7 @@ CommandHeader* QueryConsumerOffsetResponseHeader::Decode( return h; } -void QueryConsumerOffsetResponseHeader::SetDeclaredFieldOfCommandHeader( - map& requestMap) { +void QueryConsumerOffsetResponseHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { requestMap.insert(pair("offset", UtilAll::to_string(offset))); } //& requestMap) { +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.insert(pair("queueId", UtilAll::to_string(queueId))); + requestMap.insert(pair("commitOffset", UtilAll::to_string(commitOffset))); } //& requestMap) { +void ConsumerSendMsgBackRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { requestMap.insert(pair("group", group)); - requestMap.insert( - pair("delayLevel", UtilAll::to_string(delayLevel))); + requestMap.insert(pair("delayLevel", UtilAll::to_string(delayLevel))); requestMap.insert(pair("offset", UtilAll::to_string(offset))); } //& cids) { +void GetConsumerListByGroupResponseBody::Decode(const MemoryBlock* mem, vector& cids) { cids.clear(); //(mem->getData()); @@ -456,26 +398,39 @@ void GetConsumerListByGroupResponseBody::Decode(const MemoryBlock* mem, } } -void GetConsumerListByGroupResponseBody::SetDeclaredFieldOfCommandHeader( - map& requestMap) {} +void GetConsumerListByGroupResponseBody::SetDeclaredFieldOfCommandHeader(map& requestMap) {} -void ResetOffsetRequestHeader::setTopic(const string& tmp) { topic = tmp; } +void ResetOffsetRequestHeader::setTopic(const string& tmp) { + topic = tmp; +} -void ResetOffsetRequestHeader::setGroup(const string& tmp) { group = tmp; } +void ResetOffsetRequestHeader::setGroup(const string& tmp) { + group = tmp; +} void ResetOffsetRequestHeader::setTimeStamp(const int64& tmp) { timestamp = tmp; } -void ResetOffsetRequestHeader::setForceFlag(const bool& tmp) { isForce = tmp; } +void ResetOffsetRequestHeader::setForceFlag(const bool& tmp) { + isForce = tmp; +} -const string ResetOffsetRequestHeader::getTopic() const { return topic; } +const string ResetOffsetRequestHeader::getTopic() const { + return topic; +} -const string ResetOffsetRequestHeader::getGroup() const { return group; } +const string ResetOffsetRequestHeader::getGroup() const { + return group; +} -const int64 ResetOffsetRequestHeader::getTimeStamp() const { return timestamp; } +const int64 ResetOffsetRequestHeader::getTimeStamp() const { + return timestamp; +} -const bool ResetOffsetRequestHeader::getForceFlag() const { return isForce; } +const bool ResetOffsetRequestHeader::getForceFlag() const { + return isForce; +} CommandHeader* ResetOffsetRequestHeader::Decode(Json::Value& ext) { ResetOffsetRequestHeader* h = new ResetOffsetRequestHeader(); @@ -499,16 +454,13 @@ CommandHeader* ResetOffsetRequestHeader::Decode(Json::Value& ext) { 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()); + 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(); +CommandHeader* GetConsumerRunningInfoRequestHeader::Decode(Json::Value& ext) { + GetConsumerRunningInfoRequestHeader* h = new GetConsumerRunningInfoRequestHeader(); Json::Value& tempValue = ext["consumerGroup"]; if (tempValue.isString()) { @@ -524,8 +476,8 @@ CommandHeader* GetConsumerRunningInfoRequestHeader::Decode( 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); + LOG_INFO("consumerGroup:%s, clientId:%s, jstackEnable:%d", h->consumerGroup.c_str(), h->clientId.c_str(), + h->jstackEnable); return h; } @@ -535,20 +487,17 @@ void GetConsumerRunningInfoRequestHeader::Encode(Json::Value& outData) { outData["jstackEnable"] = jstackEnable; } -void GetConsumerRunningInfoRequestHeader::SetDeclaredFieldOfCommandHeader( - map& requestMap) { +void GetConsumerRunningInfoRequestHeader::SetDeclaredFieldOfCommandHeader(map& requestMap) { requestMap.insert(pair("consumerGroup", consumerGroup)); requestMap.insert(pair("clientId", clientId)); - requestMap.insert( - pair("jstackEnable", UtilAll::to_string(jstackEnable))); + requestMap.insert(pair("jstackEnable", UtilAll::to_string(jstackEnable))); } const string GetConsumerRunningInfoRequestHeader::getConsumerGroup() const { return consumerGroup; } -void GetConsumerRunningInfoRequestHeader::setConsumerGroup( - const string& Group) { +void GetConsumerRunningInfoRequestHeader::setConsumerGroup(const string& Group) { consumerGroup = Group; } @@ -556,8 +505,7 @@ const string GetConsumerRunningInfoRequestHeader::getClientId() const { return clientId; } -void GetConsumerRunningInfoRequestHeader::setClientId( - const string& input_clientId) { +void GetConsumerRunningInfoRequestHeader::setClientId(const string& input_clientId) { clientId = input_clientId; } @@ -565,15 +513,12 @@ const bool GetConsumerRunningInfoRequestHeader::isJstackEnable() const { return jstackEnable; } -void GetConsumerRunningInfoRequestHeader::setJstackEnable( - const bool& input_jstackEnable) { +void GetConsumerRunningInfoRequestHeader::setJstackEnable(const bool& input_jstackEnable) { jstackEnable = input_jstackEnable; } -CommandHeader* NotifyConsumerIdsChangedRequestHeader::Decode( - Json::Value& ext) { - NotifyConsumerIdsChangedRequestHeader* h = - new NotifyConsumerIdsChangedRequestHeader(); +CommandHeader* NotifyConsumerIdsChangedRequestHeader::Decode(Json::Value& ext) { + NotifyConsumerIdsChangedRequestHeader* h = new NotifyConsumerIdsChangedRequestHeader(); Json::Value& tempValue = ext["consumerGroup"]; if (tempValue.isString()) { diff --git a/src/protocol/CommandHeader.h b/src/protocol/CommandHeader.h index b5ffe14ff..2ad3e47c8 100644 --- a/src/protocol/CommandHeader.h +++ b/src/protocol/CommandHeader.h @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + #ifndef __COMMANDCUSTOMHEADER_H__ #define __COMMANDCUSTOMHEADER_H__ @@ -32,8 +32,7 @@ class CommandHeader { public: virtual ~CommandHeader() {} virtual void Encode(Json::Value& outData) {} - virtual void SetDeclaredFieldOfCommandHeader( - map& requestMap) {} + virtual void SetDeclaredFieldOfCommandHeader(map& requestMap) {} }; //& requestMap); @@ -158,11 +157,7 @@ class PullMessageRequestHeader : public CommandHeader { //& requestMap); diff --git a/src/protocol/ConsumerRunningInfo.cpp b/src/protocol/ConsumerRunningInfo.cpp old mode 100755 new mode 100644 index ab7f389f3..999686bed --- a/src/protocol/ConsumerRunningInfo.cpp +++ b/src/protocol/ConsumerRunningInfo.cpp @@ -19,20 +19,17 @@ namespace rocketmq { const string ConsumerRunningInfo::PROP_NAMESERVER_ADDR = "PROP_NAMESERVER_ADDR"; -const string ConsumerRunningInfo::PROP_THREADPOOL_CORE_SIZE = - "PROP_THREADPOOL_CORE_SIZE"; +const string ConsumerRunningInfo::PROP_THREADPOOL_CORE_SIZE = "PROP_THREADPOOL_CORE_SIZE"; const string ConsumerRunningInfo::PROP_CONSUME_ORDERLY = "PROP_CONSUMEORDERLY"; const string ConsumerRunningInfo::PROP_CONSUME_TYPE = "PROP_CONSUME_TYPE"; const string ConsumerRunningInfo::PROP_CLIENT_VERSION = "PROP_CLIENT_VERSION"; -const string ConsumerRunningInfo::PROP_CONSUMER_START_TIMESTAMP = - "PROP_CONSUMER_START_TIMESTAMP"; +const string ConsumerRunningInfo::PROP_CONSUMER_START_TIMESTAMP = "PROP_CONSUMER_START_TIMESTAMP"; const map ConsumerRunningInfo::getProperties() const { return properties; } -void ConsumerRunningInfo::setProperties( - const map& input_properties) { +void ConsumerRunningInfo::setProperties(const map& input_properties) { properties = input_properties; } @@ -40,13 +37,11 @@ void ConsumerRunningInfo::setProperty(const string& key, const string& value) { properties[key] = value; } -const map ConsumerRunningInfo::getMqTable() - const { +const map ConsumerRunningInfo::getMqTable() const { return mqTable; } -void ConsumerRunningInfo::setMqTable(MessageQueue queue, - ProcessQueueInfo queueInfo) { +void ConsumerRunningInfo::setMqTable(MessageQueue queue, ProcessQueueInfo queueInfo) { mqTable[queue] = queueInfo; } @@ -66,12 +61,13 @@ const vector ConsumerRunningInfo::getSubscriptionSet() const { return subscriptionSet; } -void ConsumerRunningInfo::setSubscriptionSet( - const vector& input_subscriptionSet) { +void ConsumerRunningInfo::setSubscriptionSet(const vector& input_subscriptionSet) { subscriptionSet = input_subscriptionSet; } -const string ConsumerRunningInfo::getJstack() const { return jstack; } +const string ConsumerRunningInfo::getJstack() const { + return jstack; +} void ConsumerRunningInfo::setJstack(const string& input_jstack) { jstack = input_jstack; @@ -83,8 +79,7 @@ string ConsumerRunningInfo::encode() { 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_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]; @@ -105,8 +100,7 @@ string ConsumerRunningInfo::encode() { Json::Value mq; string key = "\"mqTable\":"; key.append("{"); - for (map::iterator it = mqTable.begin(); - it != mqTable.end(); ++it) { + for (map::iterator it = mqTable.begin(); it != mqTable.end(); ++it) { key.append((it->first).toJson().toStyledString()); key.erase(key.end() - 1); key.append(":"); diff --git a/src/protocol/ConsumerRunningInfo.h b/src/protocol/ConsumerRunningInfo.h old mode 100755 new mode 100644 index ec1e2b973..3c83834b6 --- a/src/protocol/ConsumerRunningInfo.h +++ b/src/protocol/ConsumerRunningInfo.h @@ -49,8 +49,7 @@ class ConsumerRunningInfo { // const map getStatusTable() const; // void setStatusTable(const map& input_statusTable) ; const vector getSubscriptionSet() const; - void setSubscriptionSet( - const vector& input_subscriptionSet); + void setSubscriptionSet(const vector& input_subscriptionSet); const string getJstack() const; void setJstack(const string& input_jstack); string encode(); diff --git a/src/protocol/HeartbeatData.h b/src/protocol/HeartbeatData.h old mode 100755 new mode 100644 index 9b74280c1..2bd4aa71a --- a/src/protocol/HeartbeatData.h +++ b/src/protocol/HeartbeatData.h @@ -29,9 +29,7 @@ namespace rocketmq { class ProducerData { public: ProducerData(){}; - bool operator<(const ProducerData& pd) const { - return groupName < pd.groupName; - } + bool operator<(const ProducerData& pd) const { return groupName < pd.groupName; } Json::Value toJson() const { Json::Value outJson; outJson["groupName"] = groupName; @@ -47,9 +45,7 @@ class ConsumerData { public: ConsumerData(){}; virtual ~ConsumerData() { subscriptionDataSet.clear(); } - bool operator<(const ConsumerData& cd) const { - return groupName < cd.groupName; - } + bool operator<(const ConsumerData& cd) const { return groupName < cd.groupName; } Json::Value toJson() const { Json::Value outJson; diff --git a/src/protocol/KVTable.h b/src/protocol/KVTable.h old mode 100755 new mode 100644 index 6cc557f9a..e9b6a53fc --- a/src/protocol/KVTable.h +++ b/src/protocol/KVTable.h @@ -27,17 +27,17 @@ using std::string; namespace rocketmq { // &getTable() { return m_table; } + const map& getTable() { return m_table; } - void setTable(const map &table) { m_table = table; } + void setTable(const map& table) { m_table = table; } - private: - map m_table; + 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..a63426dfc --- a/src/protocol/LockBatchBody.cpp +++ b/src/protocol/LockBatchBody.cpp @@ -19,15 +19,21 @@ #include "Logging.h" namespace rocketmq { // LockBatchRequestBody::getMqSet() { return mqSet; } +vector LockBatchRequestBody::getMqSet() { + return mqSet; +} void LockBatchRequestBody::setMqSet(vector in_mqSet) { mqSet.swap(in_mqSet); } @@ -56,13 +62,11 @@ Json::Value LockBatchRequestBody::toJson(const MQMessageQueue& mq) const { vector LockBatchResponseBody::getLockOKMQSet() { return lockOKMQSet; } -void LockBatchResponseBody::setLockOKMQSet( - vector in_lockOKMQSet) { +void LockBatchResponseBody::setLockOKMQSet(vector in_lockOKMQSet) { lockOKMQSet.swap(in_lockOKMQSet); } -void LockBatchResponseBody::Decode(const MemoryBlock* mem, - vector& messageQueues) { +void LockBatchResponseBody::Decode(const MemoryBlock* mem, vector& messageQueues) { messageQueues.clear(); //(mem->getData()); @@ -87,15 +91,21 @@ void LockBatchResponseBody::Decode(const MemoryBlock* mem, } } -string UnlockBatchRequestBody::getConsumerGroup() { return consumerGroup; } +string UnlockBatchRequestBody::getConsumerGroup() { + return consumerGroup; +} void UnlockBatchRequestBody::setConsumerGroup(string in_consumerGroup) { consumerGroup = in_consumerGroup; } -string UnlockBatchRequestBody::getClientId() { return clientId; } +string UnlockBatchRequestBody::getClientId() { + return clientId; +} void UnlockBatchRequestBody::setClientId(string in_clientId) { clientId = in_clientId; } -vector UnlockBatchRequestBody::getMqSet() { return mqSet; } +vector UnlockBatchRequestBody::getMqSet() { + return mqSet; +} void UnlockBatchRequestBody::setMqSet(vector in_mqSet) { mqSet.swap(in_mqSet); } @@ -113,8 +123,7 @@ void UnlockBatchRequestBody::Encode(string& outData) { outData = fastwrite.write(root); } -Json::Value UnlockBatchRequestBody::toJson( - const MQMessageQueue& mq) const { +Json::Value UnlockBatchRequestBody::toJson(const MQMessageQueue& mq) const { Json::Value outJson; outJson["topic"] = mq.getTopic(); outJson["brokerName"] = mq.getBrokerName(); diff --git a/src/protocol/LockBatchBody.h b/src/protocol/LockBatchBody.h old mode 100755 new mode 100644 index c1d715566..7f92e8936 --- a/src/protocol/LockBatchBody.h +++ b/src/protocol/LockBatchBody.h @@ -51,8 +51,7 @@ class LockBatchResponseBody { virtual ~LockBatchResponseBody() { lockOKMQSet.clear(); } vector getLockOKMQSet(); void setLockOKMQSet(vector lockOKMQSet); - static void Decode(const MemoryBlock* mem, - vector& messageQueues); + static void Decode(const MemoryBlock* mem, vector& messageQueues); private: vector lockOKMQSet; diff --git a/src/protocol/MQProtos.h b/src/protocol/MQProtos.h old mode 100755 new mode 100644 index 50c1841dd..19b377ac6 --- a/src/protocol/MQProtos.h +++ b/src/protocol/MQProtos.h @@ -36,9 +36,9 @@ enum MQRequestCode { UPDATE_AND_CREATE_TOPIC = 17, // get all topic config info from broker GET_ALL_TOPIC_CONFIG = 21, - //git all topic list from broker + // git all topic list from broker GET_TOPIC_CONFIG_LIST = 22, - //get topic name list from broker + // get topic name list from broker GET_TOPIC_NAME_LIST = 23, UPDATE_BROKER_CONFIG = 25, GET_BROKER_CONFIG = 26, @@ -49,23 +49,23 @@ enum MQRequestCode { GET_MIN_OFFSET = 31, GET_EARLIEST_MSG_STORETIME = 32, VIEW_MESSAGE_BY_ID = 33, - //send heartbeat to broker, and register itself + // send heartbeat to broker, and register itself HEART_BEAT = 34, - //unregister client to broker + // unregister client to broker UNREGISTER_CLIENT = 35, - //send back consume fail msg to broker + // send back consume fail msg to broker CONSUMER_SEND_MSG_BACK = 36, - //Commit Or Rollback transaction + // Commit Or Rollback transaction END_TRANSACTION = 37, // get consumer list by group from broker GET_CONSUMER_LIST_BY_GROUP = 38, CHECK_TRANSACTION_STATE = 39, - //broker send notify to consumer when consumer lists changes + // broker send notify to consumer when consumer lists changes NOTIFY_CONSUMER_IDS_CHANGED = 40, - //lock mq before orderly consume + // lock mq before orderly consume LOCK_BATCH_MQ = 41, - //unlock mq after orderly consume + // unlock mq after orderly consume UNLOCK_BATCH_MQ = 42, GET_ALL_CONSUMER_OFFSET = 43, GET_ALL_DELAY_OFFSET = 45, @@ -102,7 +102,6 @@ enum MQRequestCode { GET_KVLIST_BY_NAMESPACE = 219, - RESET_CONSUMER_CLIENT_OFFSET = 220, GET_CONSUMER_STATUS_FROM_CLIENT = 221, @@ -145,39 +144,39 @@ enum MQRequestCode { // RemotingCommand::s_seqNumber; //::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), @@ -50,7 +52,7 @@ RemotingCommand::RemotingCommand(int code, string language, int version, m_pExtHeader(pExtHeader) {} RemotingCommand::RemotingCommand(const RemotingCommand& command) { - Assign(command); + Assign(command); } RemotingCommand& RemotingCommand::operator=(const RemotingCommand& command) { @@ -60,9 +62,11 @@ RemotingCommand& RemotingCommand::operator=(const RemotingCommand& command) { return *this; } -RemotingCommand::~RemotingCommand() { m_pExtHeader = NULL; } +RemotingCommand::~RemotingCommand() { + m_pExtHeader = NULL; +} -void RemotingCommand::Assign(const RemotingCommand &command) { +void RemotingCommand::Assign(const RemotingCommand& command) { m_code = command.m_code; m_language = command.m_language; m_version = command.m_version; @@ -71,14 +75,14 @@ void RemotingCommand::Assign(const RemotingCommand &command) { m_remark = command.m_remark; m_msgBody = command.m_msgBody; - for (auto &it : command.m_extFields) { + 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 + // m_pExtHeader = command.m_pExtHeader; //ignore this filed at this moment, if need please add it } void RemotingCommand::Encode() { @@ -94,22 +98,16 @@ void RemotingCommand::Encode() { 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]; + 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]; + extJson[SessionCredentials::Signature] = m_extFields[SessionCredentials::Signature]; + extJson[SessionCredentials::AccessKey] = m_extFields[SessionCredentials::AccessKey]; + extJson[SessionCredentials::ONSChannelKey] = m_extFields[SessionCredentials::ONSChannelKey]; root["extFields"] = extJson; } @@ -129,9 +127,13 @@ void RemotingCommand::Encode() { m_head.copyFrom(data.c_str(), sizeof(messageHeader), headLen); } -const MemoryBlock* RemotingCommand::GetHead() const { return &m_head; } +const MemoryBlock* RemotingCommand::GetHead() const { + return &m_head; +} -const MemoryBlock* RemotingCommand::GetBody() const { return &m_body; } +const MemoryBlock* RemotingCommand::GetBody() const { + return &m_body; +} void RemotingCommand::SetBody(const char* pData, int len) { m_body.reset(); @@ -172,10 +174,8 @@ RemotingCommand* RemotingCommand::Decode(const MemoryBlock& mem) { 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); + 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); @@ -203,7 +203,9 @@ bool RemotingCommand::isOnewayRPC() { return (m_flag & bits) == bits; } -void RemotingCommand::setOpaque(const int opa) { m_opaque = opa; } +void RemotingCommand::setOpaque(const int opa) { + m_opaque = opa; +} void RemotingCommand::SetExtHeader(int code) { try { @@ -227,8 +229,7 @@ void RemotingCommand::SetExtHeader(int code) { m_pExtHeader.reset(SearchOffsetResponseHeader::Decode(ext)); break; case GET_EARLIEST_MSG_STORETIME: - m_pExtHeader.reset( - GetEarliestMsgStoretimeResponseHeader::Decode(ext)); + m_pExtHeader.reset(GetEarliestMsgStoretimeResponseHeader::Decode(ext)); break; case QUERY_CONSUMER_OFFSET: m_pExtHeader.reset(QueryConsumerOffsetResponseHeader::Decode(ext)); @@ -240,8 +241,7 @@ void RemotingCommand::SetExtHeader(int code) { m_pExtHeader.reset(GetConsumerRunningInfoRequestHeader::Decode(ext)); break; case NOTIFY_CONSUMER_IDS_CHANGED: - m_pExtHeader.reset( - NotifyConsumerIdsChangedRequestHeader::Decode(ext)); + m_pExtHeader.reset(NotifyConsumerIdsChangedRequestHeader::Decode(ext)); default: break; } @@ -251,29 +251,49 @@ void RemotingCommand::SetExtHeader(int code) { } } -void RemotingCommand::setCode(int code) { m_code = code; } +void RemotingCommand::setCode(int code) { + m_code = code; +} -int RemotingCommand::getCode() const { return m_code; } +int RemotingCommand::getCode() const { + return m_code; +} -int RemotingCommand::getOpaque() const { return m_opaque; } +int RemotingCommand::getOpaque() const { + return m_opaque; +} -string RemotingCommand::getRemark() const { return m_remark; } +string RemotingCommand::getRemark() const { + return m_remark; +} -void RemotingCommand::setRemark(string mark) { m_remark = mark; } +void RemotingCommand::setRemark(string mark) { + m_remark = mark; +} CommandHeader* RemotingCommand::getCommandHeader() const { return m_pExtHeader.get(); } -void RemotingCommand::setParsedJson(Json::Value json) { m_parsedJson = json; } +void RemotingCommand::setParsedJson(Json::Value json) { + m_parsedJson = json; +} -const int RemotingCommand::getFlag() const { return m_flag; } +const int RemotingCommand::getFlag() const { + return m_flag; +} -const int RemotingCommand::getVersion() const { return m_version; } +const int RemotingCommand::getVersion() const { + return m_version; +} -void RemotingCommand::setMsgBody(const string& body) { m_msgBody = body; } +void RemotingCommand::setMsgBody(const string& body) { + m_msgBody = body; +} -string RemotingCommand::getMsgBody() const { return m_msgBody; } +string RemotingCommand::getMsgBody() const { + return m_msgBody; +} void RemotingCommand::addExtField(const string& key, const string& value) { m_extFields[key] = value; @@ -281,10 +301,7 @@ void RemotingCommand::addExtField(const string& key, const string& value) { std::string RemotingCommand::ToString() const { std::stringstream ss; - ss << "code:" << m_code - << ",opaque:" << m_opaque - << ",flag:" << m_flag - << ",body.size:" << m_body.getSize() + ss << "code:" << m_code << ",opaque:" << m_opaque << ",flag:" << m_flag << ",body.size:" << m_body.getSize() << ",header.size:" << m_head.getSize(); return ss.str(); } diff --git a/src/protocol/RemotingCommand.h b/src/protocol/RemotingCommand.h old mode 100755 new mode 100644 index 254d964a1..bc137f7ed --- a/src/protocol/RemotingCommand.h +++ b/src/protocol/RemotingCommand.h @@ -31,10 +31,15 @@ const int RPC_ONEWAY = 1; // 0, RPC // 1, Oneway; // brokerAddrs; // brokerAddrs; //(mem->getData()); + string data(pData, mem->getSize()); + + Json::CharReaderBuilder charReaderBuilder; + charReaderBuilder.settings_["allowNumericKeys"] = true; + unique_ptr pCharReaderPtr(charReaderBuilder.newCharReader()); + + const char* begin = pData; + const char* end = pData + mem->getSize(); + Json::Value root; + string errs; + + if (!pCharReaderPtr->parse(begin, end, &root, &errs)) { + LOG_ERROR("parse json error:%s, value isArray:%d, isObject:%d", errs.c_str(), root.isArray(), root.isObject()); + return nullptr; } - static TopicRouteData *Decode(const MemoryBlock *mem) { - //(mem->getData()); - string data(pData, mem->getSize()); - - Json::CharReaderBuilder charReaderBuilder; - charReaderBuilder.settings_["allowNumericKeys"] = true; - unique_ptr pCharReaderPtr(charReaderBuilder.newCharReader()); - - const char *begin = pData; - const char *end = pData + mem->getSize(); - Json::Value root; - string errs; - - if (!pCharReaderPtr->parse(begin, end, &root, &errs)) { - LOG_ERROR("parse json error:%s, value isArray:%d, isObject:%d", errs.c_str(), root.isArray(), - root.isObject()); - return nullptr; - } - - auto *trd = new TopicRouteData(); - trd->setOrderTopicConf(root["orderTopicConf"].asString()); - - Json::Value qds = root["queueDatas"]; - for (auto qd : qds) { - QueueData d; - d.brokerName = qd["brokerName"].asString(); - d.readQueueNums = qd["readQueueNums"].asInt(); - d.writeQueueNums = qd["writeQueueNums"].asInt(); - d.perm = qd["perm"].asInt(); - trd->getQueueDatas().push_back(d); - } - sort(trd->getQueueDatas().begin(), trd->getQueueDatas().end()); - - Json::Value bds = root["brokerDatas"]; - for (auto bd : bds) { - BrokerData d; - d.brokerName = bd["brokerName"].asString(); - LOG_DEBUG("brokerName:%s", d.brokerName.c_str()); - Json::Value bas = bd["brokerAddrs"]; - Json::Value::Members mbs = bas.getMemberNames(); - for (const auto &key : mbs) { - int id = atoi(key.c_str()); - string addr = bas[key].asString(); - d.brokerAddrs[id] = addr; - LOG_DEBUG("brokerId:%d, brokerAddr:%s", id, addr.c_str()); - } - trd->getBrokerDatas().push_back(d); - } - sort(trd->getBrokerDatas().begin(), trd->getBrokerDatas().end()); - - return trd; + auto* trd = new TopicRouteData(); + trd->setOrderTopicConf(root["orderTopicConf"].asString()); + + Json::Value qds = root["queueDatas"]; + for (auto qd : qds) { + QueueData d; + d.brokerName = qd["brokerName"].asString(); + d.readQueueNums = qd["readQueueNums"].asInt(); + d.writeQueueNums = qd["writeQueueNums"].asInt(); + d.perm = qd["perm"].asInt(); + trd->getQueueDatas().push_back(d); } - - /** - * Selects a (preferably master) broker address from the registered list. - * If the master's address cannot be found, a slave broker address is selected in a random manner. - * - * @return Broker address. - */ - std::string selectBrokerAddr() { - int bdSize = m_brokerDatas.size(); - if (bdSize > 0) { - int bdIndex = std::rand() % bdSize; - auto bd = m_brokerDatas[bdIndex]; - auto iter = bd.brokerAddrs.find(MASTER_ID); - if (iter == bd.brokerAddrs.end()) { - int baSize = bd.brokerAddrs.size(); - int baIndex = std::rand() % baSize; - iter = bd.brokerAddrs.begin(); - for (; baIndex > 0; baIndex--) { - iter++; - } - } - return iter->second; + sort(trd->getQueueDatas().begin(), trd->getQueueDatas().end()); + + Json::Value bds = root["brokerDatas"]; + for (auto bd : bds) { + BrokerData d; + d.brokerName = bd["brokerName"].asString(); + LOG_DEBUG("brokerName:%s", d.brokerName.c_str()); + Json::Value bas = bd["brokerAddrs"]; + Json::Value::Members mbs = bas.getMemberNames(); + for (const auto& key : mbs) { + int id = atoi(key.c_str()); + string addr = bas[key].asString(); + d.brokerAddrs[id] = addr; + LOG_DEBUG("brokerId:%d, brokerAddr:%s", id, addr.c_str()); + } + trd->getBrokerDatas().push_back(d); + } + sort(trd->getBrokerDatas().begin(), trd->getBrokerDatas().end()); + + return trd; + } + + /** + * Selects a (preferably master) broker address from the registered list. + * If the master's address cannot be found, a slave broker address is selected in a random manner. + * + * @return Broker address. + */ + std::string selectBrokerAddr() { + int bdSize = m_brokerDatas.size(); + if (bdSize > 0) { + int bdIndex = std::rand() % bdSize; + auto bd = m_brokerDatas[bdIndex]; + auto iter = bd.brokerAddrs.find(MASTER_ID); + if (iter == bd.brokerAddrs.end()) { + int baSize = bd.brokerAddrs.size(); + int baIndex = std::rand() % baSize; + iter = bd.brokerAddrs.begin(); + for (; baIndex > 0; baIndex--) { + iter++; } - return ""; + } + return iter->second; } + return ""; + } - std::vector &getQueueDatas() { return m_queueDatas; } + std::vector& getQueueDatas() { return m_queueDatas; } - std::vector &getBrokerDatas() { return m_brokerDatas; } + std::vector& getBrokerDatas() { return m_brokerDatas; } - const std::string &getOrderTopicConf() const { return m_orderTopicConf; } + const std::string& getOrderTopicConf() const { return m_orderTopicConf; } - void setOrderTopicConf(const string &orderTopicConf) { m_orderTopicConf = orderTopicConf; } + void setOrderTopicConf(const string& orderTopicConf) { m_orderTopicConf = orderTopicConf; } - bool operator==(const TopicRouteData &other) const { - return m_brokerDatas == other.m_brokerDatas && m_orderTopicConf == other.m_orderTopicConf && - m_queueDatas == other.m_queueDatas; - } + bool operator==(const TopicRouteData& other) const { + return m_brokerDatas == other.m_brokerDatas && m_orderTopicConf == other.m_orderTopicConf && + m_queueDatas == other.m_queueDatas; + } - private: - std::string m_orderTopicConf; - std::vector m_queueDatas; - std::vector m_brokerDatas; + private: + std::string m_orderTopicConf; + std::vector m_queueDatas; + std::vector m_brokerDatas; }; } // namespace rocketmq diff --git a/src/thread/disruptorLFQ.h b/src/thread/disruptorLFQ.h old mode 100755 new mode 100644 index 079bcc595..30e5a1858 --- a/src/thread/disruptorLFQ.h +++ b/src/thread/disruptorLFQ.h @@ -39,8 +39,7 @@ class taskBatchHandler : public EventHandlerInterface { taskBatchHandler(int pullMsgThreadPoolNum); virtual ~taskBatchHandler() {} - virtual void OnEvent(const int64_t& sequence, const bool& end_of_batch, - Task* event); + 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); @@ -64,31 +63,27 @@ class taskEventTranslator : public EventTranslatorInterface { class taskExceptionHandler : public ExceptionHandlerInterface { public: - virtual void Handle(const std::exception& exception, const int64_t& sequence, - Task* event) {} + 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_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_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())); + 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. @@ -98,9 +93,8 @@ class disruptorLFQ { 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_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())); } diff --git a/src/thread/task_queue.cpp b/src/thread/task_queue.cpp old mode 100755 new mode 100644 index e7c062e85..3df2f74e5 --- a/src/thread/task_queue.cpp +++ b/src/thread/task_queue.cpp @@ -27,27 +27,23 @@ Task* taskEventFactory::NewInstance(const int& size) const { return new Task[size]; } -taskBatchHandler::taskBatchHandler(int pullMsgThreadPoolNum) - : m_ioServiceWork(m_ioService) { +taskBatchHandler::taskBatchHandler(int pullMsgThreadPoolNum) : m_ioServiceWork(m_ioService) { #if !defined(WIN32) && !defined(__APPLE__) string taskName = UtilAll::getProcessName(); prctl(PR_SET_NAME, "PullMsgTP", 0, 0, 0); #endif for (int i = 0; i != pullMsgThreadPoolNum; ++i) { - m_threadpool.create_thread( - boost::bind(&boost::asio::io_service::run, &m_ioService)); + m_threadpool.create_thread(boost::bind(&boost::asio::io_service::run, &m_ioService)); } #if !defined(WIN32) && !defined(__APPLE__) prctl(PR_SET_NAME, taskName.c_str(), 0, 0, 0); #endif } -void taskBatchHandler::OnEvent(const int64_t& sequence, - const bool& end_of_batch, Task* event) { +void taskBatchHandler::OnEvent(const int64_t& sequence, const bool& end_of_batch, Task* event) { // cp Task event out, avoid publish event override current Task event Task currentTask(*event); - m_ioService.post(boost::bind(&taskBatchHandler::runTaskEvent, this, - currentTask, sequence)); + m_ioService.post(boost::bind(&taskBatchHandler::runTaskEvent, this, currentTask, sequence)); } void taskBatchHandler::runTaskEvent(Task event, int64_t sequence) { diff --git a/src/thread/task_queue.h b/src/thread/task_queue.h old mode 100755 new mode 100644 index af79e0906..4c87864e2 --- a/src/thread/task_queue.h +++ b/src/thread/task_queue.h @@ -45,7 +45,8 @@ class Task_impl : public ITask_impl { m_arg = 0; } virtual void run() { - if (m_func != 0) m_func(m_arg); + if (m_func != 0) + m_func(m_arg); } virtual ITask_impl* fork() { return new Task_impl(m_func, m_arg); } @@ -72,7 +73,8 @@ struct Task { return *this; } void run() { - if (m_pTaskImpl) m_pTaskImpl->run(); + if (m_pTaskImpl) + m_pTaskImpl->run(); } private: @@ -131,8 +133,7 @@ struct TaskBinder { FUNCT dest_func; ARG1 arg1; ARG2 arg2; - lambda(FUNCT func, const ARG1& arg1, const ARG2& arg2) - : dest_func(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); } }; @@ -149,15 +150,12 @@ struct TaskBinder { 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); - } + virtual ITask_impl* fork() { return new lambda(dest_func, arg1, arg2, arg3); } }; return Task(new lambda(func, arg1, arg2, arg3)); } - template + template static Task gen(FUNCT func, ARG1 arg1, ARG2 arg2, ARG3 arg3, ARG4 arg4) { struct lambda : public ITask_impl { FUNCT dest_func; @@ -165,21 +163,16 @@ struct TaskBinder { ARG2 arg2; ARG3 arg3; ARG4 arg4; - lambda(FUNCT func, const ARG1& arg1, const ARG2& arg2, const ARG3& arg3, - const 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); - } + 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) { + 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; @@ -187,26 +180,16 @@ struct TaskBinder { 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) {} + 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); - } + 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) { + 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; @@ -215,27 +198,29 @@ struct TaskBinder { 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) {} + 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); - } + 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) { + 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; @@ -245,32 +230,31 @@ struct TaskBinder { 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, + 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); - } + : 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) { + 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; @@ -281,9 +265,15 @@ struct TaskBinder { 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) + 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), @@ -293,23 +283,24 @@ struct TaskBinder { 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); - } + 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)); + 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) { + 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; @@ -321,9 +312,16 @@ struct TaskBinder { 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) + 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), @@ -334,16 +332,10 @@ struct TaskBinder { 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); - } + 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)); + return Task(new lambda(func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)); } //*dest_func)(arg1); } virtual ITask_impl* fork() { return new lambda(dest_func, obj, arg1); } }; return Task(new lambda(func, obj, arg1)); } - template + 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) + 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); - } + 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) { + 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) + 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); - } + 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) { + 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; @@ -427,28 +422,38 @@ struct TaskBinder { 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, + 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) {} + : 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); - } + 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) { + 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; @@ -457,30 +462,41 @@ struct TaskBinder { 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) {} + 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); - } + 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 + 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, + 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); @@ -491,35 +507,46 @@ struct TaskBinder { 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); - } + 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) { + 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; @@ -531,8 +558,13 @@ struct TaskBinder { 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, + 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), @@ -543,29 +575,42 @@ struct TaskBinder { 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); - } + 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)); + 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) { + 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) + RET(T::*dest_func) (FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8); T* obj; ARG1 arg1; @@ -576,11 +621,16 @@ struct TaskBinder { 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) + 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), @@ -591,29 +641,45 @@ struct TaskBinder { 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); - } + 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)); + 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) { + 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) + RET(T::*dest_func) (FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8, FARG9); T* obj; ARG1 arg1; @@ -625,11 +691,17 @@ struct TaskBinder { 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) + 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), @@ -641,16 +713,12 @@ struct TaskBinder { arg7(arg7), arg8(arg8), arg9(arg9) {} - virtual void run() { - (obj->*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, 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 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)); + return Task(new lambda(func, obj, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)); } }; diff --git a/src/transport/ClientRemotingProcessor.cpp b/src/transport/ClientRemotingProcessor.cpp old mode 100755 new mode 100644 index 5e1a98b78..b0be046f7 --- a/src/transport/ClientRemotingProcessor.cpp +++ b/src/transport/ClientRemotingProcessor.cpp @@ -22,14 +22,12 @@ namespace rocketmq { -ClientRemotingProcessor::ClientRemotingProcessor( - MQClientFactory* mqClientFactory) +ClientRemotingProcessor::ClientRemotingProcessor(MQClientFactory* mqClientFactory) : m_mqClientFactory(mqClientFactory) {} ClientRemotingProcessor::~ClientRemotingProcessor() {} -RemotingCommand* ClientRemotingProcessor::processRequest( - const string& addr, RemotingCommand* request) { +RemotingCommand* ClientRemotingProcessor::processRequest(const string& addr, RemotingCommand* request) { LOG_DEBUG("request Command received:processRequest"); switch (request->getCode()) { case CHECK_TRANSACTION_STATE: @@ -55,21 +53,16 @@ RemotingCommand* ClientRemotingProcessor::processRequest( return NULL; } -RemotingCommand* ClientRemotingProcessor::resetOffset( - RemotingCommand* request) { +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(); + ResetOffsetRequestHeader* offsetHeader = (ResetOffsetRequestHeader*)request->getCommandHeader(); if (offsetBody) { - m_mqClientFactory->resetOffset(offsetHeader->getGroup(), - offsetHeader->getTopic(), - offsetBody->getOffsetTable()); + m_mqClientFactory->resetOffset(offsetHeader->getGroup(), offsetHeader->getTopic(), offsetBody->getOffsetTable()); } else { - LOG_ERROR( - "resetOffset failed as received data could not be unserialized"); + LOG_ERROR("resetOffset failed as received data could not be unserialized"); } } return NULL; // as resetOffset is oneWayRPC, do not need return any response @@ -104,29 +97,25 @@ ResetOffsetBody* ResetOffsetBody::Decode(const MemoryBlock* mem) { 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); + 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) { +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()); + 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())); + m_mqClientFactory->consumerRunningInfo(requestHeader->getConsumerGroup())); if (runningInfo) { if (requestHeader->isJstackEnable()) { /*string jstack = UtilAll::jstack(); @@ -142,16 +131,14 @@ RemotingCommand* ClientRemotingProcessor::getConsumerRunningInfo( } SessionCredentials sessionCredentials; - m_mqClientFactory->getSessionCredentialFromConsumer( - requestHeader->getConsumerGroup(), sessionCredentials); + m_mqClientFactory->getSessionCredentialFromConsumer(requestHeader->getConsumerGroup(), sessionCredentials); ClientRPCHook rpcHook(sessionCredentials); rpcHook.doBeforeRequest(addr, *pResponse); pResponse->Encode(); return pResponse; } -RemotingCommand* ClientRemotingProcessor::notifyConsumerIdsChanged( - RemotingCommand* request) { +RemotingCommand* ClientRemotingProcessor::notifyConsumerIdsChanged(RemotingCommand* request) { request->SetExtHeader(request->getCode()); NotifyConsumerIdsChangedRequestHeader* requestHeader = (NotifyConsumerIdsChangedRequestHeader*)request->getCommandHeader(); diff --git a/src/transport/ClientRemotingProcessor.h b/src/transport/ClientRemotingProcessor.h old mode 100755 new mode 100644 index c4d4ce6a8..58a041707 --- a/src/transport/ClientRemotingProcessor.h +++ b/src/transport/ClientRemotingProcessor.h @@ -31,8 +31,7 @@ class ClientRemotingProcessor { RemotingCommand* processRequest(const string& addr, RemotingCommand* request); RemotingCommand* resetOffset(RemotingCommand* request); - RemotingCommand* getConsumerRunningInfo(const string& addr, - RemotingCommand* request); + RemotingCommand* getConsumerRunningInfo(const string& addr, RemotingCommand* request); RemotingCommand* notifyConsumerIdsChanged(RemotingCommand* request); private: diff --git a/src/transport/ResponseFuture.cpp b/src/transport/ResponseFuture.cpp old mode 100755 new mode 100644 index a304e3fcf..cc10daa40 --- a/src/transport/ResponseFuture.cpp +++ b/src/transport/ResponseFuture.cpp @@ -20,8 +20,10 @@ 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); + 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; @@ -96,7 +98,9 @@ const bool ResponseFuture::getAsyncResponseFlag() { return false; } -void ResponseFuture::setAsyncResponseFlag() { m_asyncResponse.store(true); } +void ResponseFuture::setAsyncResponseFlag() { + m_asyncResponse.store(true); +} const bool ResponseFuture::getASyncFlag() { if (m_bAsync.load() == true) { @@ -106,18 +110,23 @@ const bool ResponseFuture::getASyncFlag() { return false; } -bool ResponseFuture::isSendRequestOK() { return m_sendRequestOK; } +bool ResponseFuture::isSendRequestOK() { + return m_sendRequestOK; +} void ResponseFuture::setSendRequestOK(bool sendRequestOK) { m_sendRequestOK = sendRequestOK; } -int ResponseFuture::getOpaque() const { return m_opaque; } +int ResponseFuture::getOpaque() const { + return m_opaque; +} -int ResponseFuture::getRequestCode() const { return m_requestCode; } +int ResponseFuture::getRequestCode() const { + return m_requestCode; +} -void ResponseFuture::setAsyncCallBackStatus( - asyncCallBackStatus asyncCallbackStatus) { +void ResponseFuture::setAsyncCallBackStatus(asyncCallBackStatus asyncCallbackStatus) { boost::lock_guard lock(m_asyncCallbackLock); if (m_asyncCallbackStatus == asyncCallBackStatus_init) { m_asyncCallbackStatus = asyncCallbackStatus; @@ -151,9 +160,9 @@ void ResponseFuture::executeInvokeCallbackException() { 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()); + // 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 { @@ -172,36 +181,36 @@ bool ResponseFuture::isTimeOut() const { } int ResponseFuture::getMaxRetrySendTimes() const { - return m_maxRetrySendTimes; -} + return m_maxRetrySendTimes; +} int ResponseFuture::getRetrySendTimes() const { - return m_retrySendTimes; + return m_retrySendTimes; } void ResponseFuture::setMaxRetrySendTimes(int maxRetryTimes) { - m_maxRetrySendTimes = maxRetryTimes; + m_maxRetrySendTimes = maxRetryTimes; } void ResponseFuture::setRetrySendTimes(int retryTimes) { - m_retrySendTimes = retryTimes; + m_retrySendTimes = retryTimes; } void ResponseFuture::setBrokerAddr(const std::string& brokerAddr) { - m_brokerAddr = brokerAddr; + m_brokerAddr = brokerAddr; } void ResponseFuture::setRequestCommand(const RemotingCommand& requestCommand) { - m_requestCommand = requestCommand; + m_requestCommand = requestCommand; } const RemotingCommand& ResponseFuture::getRequestCommand() { - return m_requestCommand; + return m_requestCommand; } std::string ResponseFuture::getBrokerAddr() const { - return m_brokerAddr; + return m_brokerAddr; } int64 ResponseFuture::leftTime() const { - int64 diff = UtilAll::currentTimeMillis() - m_beginTimestamp; - return m_timeout - diff; + int64 diff = UtilAll::currentTimeMillis() - m_beginTimestamp; + return m_timeout - diff; } RemotingCommand* ResponseFuture::getCommand() const { diff --git a/src/transport/ResponseFuture.h b/src/transport/ResponseFuture.h old mode 100755 new mode 100644 index 6564f537f..d6f6b7a72 --- a/src/transport/ResponseFuture.h +++ b/src/transport/ResponseFuture.h @@ -34,8 +34,11 @@ class TcpRemotingClient; // m_asyncResponse; boost::atomic m_syncResponse; - int m_maxRetrySendTimes; - int m_retrySendTimes; + int m_maxRetrySendTimes; + int m_retrySendTimes; std::string m_brokerAddr; RemotingCommand m_requestCommand; // TcpRemotingClient* m_tcpRemoteClient; diff --git a/src/transport/SocketUtil.cpp b/src/transport/SocketUtil.cpp old mode 100755 new mode 100644 index d428f242d..daa369c3f --- a/src/transport/SocketUtil.cpp +++ b/src/transport/SocketUtil.cpp @@ -72,15 +72,18 @@ uint64 swapll(uint64 v) { #ifdef ENDIANMODE_BIG 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; #endif } -uint64 h2nll(uint64 v) { return swapll(v); } +uint64 h2nll(uint64 v) { + return swapll(v); +} -uint64 n2hll(uint64 v) { return swapll(v); } +uint64 n2hll(uint64 v) { + return swapll(v); +} } // lock(m_futureTableMutex); - for (ResMap::iterator it = m_futureTable.begin(); it != m_futureTable.end(); - ++it) { - if (it->second) it->second->releaseThreadCondition(); + for (ResMap::iterator it = m_futureTable.begin(); it != m_futureTable.end(); ++it) { + if (it->second) + it->second->releaseThreadCondition(); } } LOG_DEBUG("TcpRemotingClient::stopAllTcpTransportThread End"); @@ -101,11 +97,9 @@ void TcpRemotingClient::stopAllTcpTransportThread() { void TcpRemotingClient::updateNameServerAddressList(const string& addrs) { if (!addrs.empty()) { - boost::unique_lock lock(m_namesrvlock, - boost::try_to_lock); + 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))) { + if (!lock.timed_lock(boost::get_system_time() + boost::posix_time::seconds(10))) { LOG_ERROR("updateNameServerAddressList get timed_mutex timeout"); return; } @@ -130,14 +124,12 @@ void TcpRemotingClient::updateNameServerAddressList(const string& addrs) { } } -bool TcpRemotingClient::invokeHeartBeat(const string& addr, - RemotingCommand& request) { +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)); + 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); @@ -146,16 +138,13 @@ bool TcpRemotingClient::invokeHeartBeat(const string& addr, 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()); + 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()); + LOG_WARN("get error response:%d of heartbeat to addr:%s", pRsp->getCode(), addr.c_str()); return false; } } else { @@ -220,25 +209,21 @@ bool TcpRemotingClient::invokeAsync(const string& addr, responseFuture->setMaxRetrySendTimes(maxRetrySendTimes); responseFuture->setRetrySendTimes(retrySendTimes); responseFuture->setBrokerAddr(addr); - responseFuture->setRequestCommand(request); + 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)); + 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)); + 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); + LOG_DEBUG("invokeAsync success, addr:%s, code:%d, opaque:%d", addr.c_str(), code, opaque); responseFuture->setSendRequestOK(true); } return true; @@ -247,27 +232,24 @@ bool TcpRemotingClient::invokeAsync(const string& addr, return false; } -void TcpRemotingClient::invokeOneway(const string& addr, - RemotingCommand& 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()); + 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); } -boost::shared_ptr TcpRemotingClient::CreateTransport( - const string& addr, bool 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 @@ -276,9 +258,7 @@ boost::shared_ptr TcpRemotingClient::CreateTransport( 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))) { + 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; @@ -298,10 +278,8 @@ boost::shared_ptr TcpRemotingClient::CreateTransport( 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 + 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; @@ -316,8 +294,7 @@ boost::shared_ptr TcpRemotingClient::CreateTransport( } //connect(addr, m_tcpConnectTimeout); @@ -338,8 +315,7 @@ boost::shared_ptr TcpRemotingClient::CreateTransport( } } - tcpConnectStatus connectStatus = - tts->waitTcpConnectEvent(m_tcpConnectTimeout); + tcpConnectStatus connectStatus = tts->waitTcpConnectEvent(m_tcpConnectTimeout); if (connectStatus != e_connectSuccess) { LOG_WARN("can not connect to server:%s", addr.c_str()); tts->disconnect(addr); @@ -351,19 +327,15 @@ boost::shared_ptr TcpRemotingClient::CreateTransport( } } -boost::shared_ptr TcpRemotingClient::CreateNameserverTransport( - bool needRespons) { +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); + 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))) { + 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; @@ -376,8 +348,7 @@ boost::shared_ptr TcpRemotingClient::CreateNameserverTransport( if (bGetMutex) { if (!m_namesrvAddrChoosed.empty()) { - boost::shared_ptr pTcp = - GetTransport(m_namesrvAddrChoosed, true); + boost::shared_ptr pTcp = GetTransport(m_namesrvAddrChoosed, true); if (pTcp) return pTcp; else @@ -390,11 +361,9 @@ boost::shared_ptr TcpRemotingClient::CreateNameserverTransport( 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); + 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; @@ -409,8 +378,7 @@ boost::shared_ptr TcpRemotingClient::CreateNameserverTransport( } } -void TcpRemotingClient::CloseTransport(const string& addr, - boost::shared_ptr pTcp) { +void TcpRemotingClient::CloseTransport(const string& addr, boost::shared_ptr pTcp) { if (addr.empty()) { return CloseNameServerTransport(pTcp); } @@ -418,9 +386,7 @@ void TcpRemotingClient::CloseTransport(const string& addr, 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))) { + 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 { @@ -441,18 +407,15 @@ void TcpRemotingClient::CloseTransport(const string& addr, removeItemFromTable = false; } } else { - LOG_INFO( - "tcpTransport with addr:%s had been removed from tcpTable before", - addr.c_str()); + 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()); + 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 + 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); } @@ -463,15 +426,11 @@ void TcpRemotingClient::CloseTransport(const string& addr, LOG_ERROR("CloseTransport of:%s end", addr.c_str()); } -void TcpRemotingClient::CloseNameServerTransport( - boost::shared_ptr pTcp) { +void TcpRemotingClient::CloseNameServerTransport(boost::shared_ptr pTcp) { bool bGetMutex = false; - boost::unique_lock lock(m_namesrvlock, - boost::try_to_lock); + 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))) { + if (!lock.timed_lock(boost::get_system_time() + boost::posix_time::seconds(m_tcpTransportTryLockTimeout))) { LOG_ERROR("CreateNameserverTransport get timed_mutex timeout"); return; } else { @@ -492,28 +451,23 @@ void TcpRemotingClient::CloseNameServerTransport( removeItemFromTable = false; } } else { - LOG_INFO( - "tcpTransport with addr:%s had been removed from tcpTable before", - addr.c_str()); + 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 + 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()); + LOG_WARN("CloseNameServerTransport::get tcpTransport mutex failed:%s", m_namesrvAddrChoosed.c_str()); return; } } -bool TcpRemotingClient::SendCommand(boost::shared_ptr pTts, - RemotingCommand& msg) { +bool TcpRemotingClient::SendCommand(boost::shared_ptr pTts, RemotingCommand& msg) { const MemoryBlock* phead = msg.GetHead(); const MemoryBlock* pbody = msg.GetBody(); @@ -529,21 +483,17 @@ bool TcpRemotingClient::SendCommand(boost::shared_ptr pTts, return pTts->sendMessage(pData, len); } -void TcpRemotingClient::static_messageReceived(void* context, - const MemoryBlock& mem, - const string& addr) { +void TcpRemotingClient::static_messageReceived(void* context, const MemoryBlock& mem, const string& addr) { TcpRemotingClient* pTcpRemotingClient = (TcpRemotingClient*)context; - if (pTcpRemotingClient) pTcpRemotingClient->messageReceived(mem, addr); + 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) { +void TcpRemotingClient::ProcessData(const MemoryBlock& mem, const string& addr) { RemotingCommand* pRespondCmd = NULL; try { pRespondCmd = RemotingCommand::Decode(mem); @@ -556,8 +506,7 @@ void TcpRemotingClient::ProcessData(const MemoryBlock& mem, //isResponseType()) { - boost::shared_ptr pFuture( - findAndDeleteAsyncResponseFuture(opaque)); + boost::shared_ptr pFuture(findAndDeleteAsyncResponseFuture(opaque)); if (!pFuture) { pFuture = findAndDeleteResponseFuture(opaque); if (pFuture) { @@ -579,11 +528,11 @@ void TcpRemotingClient::ProcessData(const MemoryBlock& mem, } } -void TcpRemotingClient::processResponseCommand( - RemotingCommand* pCmd, boost::shared_ptr pfuture) { +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()); + 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); @@ -593,21 +542,18 @@ void TcpRemotingClient::processResponseCommand( pfuture->setAsyncResponseFlag(); pfuture->setAsyncCallBackStatus(asyncCallBackStatus_response); cancelTimerCallback(opaque); - pfuture->executeInvokeCallback(); + pfuture->executeInvokeCallback(); } } } -void TcpRemotingClient::processRequestCommand(RemotingCommand* pCmd, - const string& addr) { +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())); + unique_ptr pResponse(m_requestTable[requestCode]->processRequest(addr, pRequestCommand.get())); if (!pRequestCommand->isOnewayRPC()) { if (pResponse) { pResponse->setOpaque(pRequestCommand->getOpaque()); @@ -620,16 +566,14 @@ void TcpRemotingClient::processRequestCommand(RemotingCommand* pCmd, } } -void TcpRemotingClient::addResponseFuture( - int opaque, boost::shared_ptr 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::shared_ptr TcpRemotingClient::findAndDeleteResponseFuture(int opaque) { boost::lock_guard lock(m_futureTableMutex); boost::shared_ptr pResponseFuture; if (m_futureTable.find(opaque) != m_futureTable.end()) { @@ -639,19 +583,17 @@ TcpRemotingClient::findAndDeleteResponseFuture(int opaque) { return pResponseFuture; } -void TcpRemotingClient::handleAsyncPullForResponseTimeout( - const boost::system::error_code& e, int opaque) { +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()); + 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)); + 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 + 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); @@ -662,16 +604,14 @@ void TcpRemotingClient::handleAsyncPullForResponseTimeout( eraseTimerCallback(opaque); } -void TcpRemotingClient::addAsyncResponseFuture( - int opaque, boost::shared_ptr 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::shared_ptr TcpRemotingClient::findAndDeleteAsyncResponseFuture(int opaque) { boost::lock_guard lock(m_asyncFutureLock); boost::shared_ptr pResponseFuture; if (m_asyncFutureTable.find(opaque) != m_asyncFutureTable.end()) { @@ -682,16 +622,13 @@ TcpRemotingClient::findAndDeleteAsyncResponseFuture(int opaque) { return pResponseFuture; } -void TcpRemotingClient::registerProcessor( - MQRequestCode requestCode, - ClientRemotingProcessor* 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) { +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); @@ -718,7 +655,7 @@ void TcpRemotingClient::eraseTimerCallback(int 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); + LOG_DEBUG("cancelTimerCallback: opaque:%lld", opaque); boost::asio::deadline_timer* t = m_async_timer_map[opaque]; t->cancel(); delete t; @@ -729,8 +666,7 @@ void TcpRemotingClient::cancelTimerCallback(int 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) { + 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; @@ -740,7 +676,8 @@ void TcpRemotingClient::removeAllTimerCallback() { } 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 + // 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); @@ -748,9 +685,9 @@ void TcpRemotingClient::deleteOpaqueForDropPullRequest(const MQMessageQueue& mq, 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()); + LOG_DEBUG("succ deleted the async pullrequest for opaque:%d, mq:%s", opaque, mq.toString().data()); } - //delete the timeout timer for opaque for pullrequest + // delete the timeout timer for opaque for pullrequest cancelTimerCallback(opaque); } diff --git a/src/transport/TcpRemotingClient.h b/src/transport/TcpRemotingClient.h old mode 100755 new mode 100644 index c338d5352..6085f7e96 --- a/src/transport/TcpRemotingClient.h +++ b/src/transport/TcpRemotingClient.h @@ -34,55 +34,49 @@ namespace rocketmq { class TcpRemotingClient { public: - TcpRemotingClient(int pullThreadNum, uint64_t tcpConnectTimeout, - uint64_t tcpTransportTryLockTimeout); + 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 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 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 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); diff --git a/src/transport/TcpTransport.cpp b/src/transport/TcpTransport.cpp index bbebc372c..7ecf3291b 100644 --- a/src/transport/TcpTransport.cpp +++ b/src/transport/TcpTransport.cpp @@ -27,8 +27,7 @@ namespace rocketmq { // lock(m_event_base_mtx); m_event_base_cv.timed_wait(lock, timeout); } @@ -114,8 +107,7 @@ tcpConnectStatus TcpTransport::getTcpConnectStatus() { tcpConnectStatus TcpTransport::waitTcpConnectEvent(int timeoutMillisecs) { boost::unique_lock lk(m_connectEventLock); - if (!m_connectEvent.timed_wait( - lk, boost::posix_time::milliseconds(timeoutMillisecs))) { + if (!m_connectEvent.timed_wait(lk, boost::posix_time::milliseconds(timeoutMillisecs))) { LOG_INFO("connect timeout"); } return getTcpConnectStatus(); @@ -130,49 +122,47 @@ void TcpTransport::setTcpConnectEvent(tcpConnectStatus connectStatus) { } } -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) { +void TcpTransport::disconnect(const string& addr) { boost::lock_guard lock(m_socketLock); if (getTcpConnectStatus() != e_connectInit) { clearBufferEventCallback(); @@ -182,8 +172,7 @@ void TcpTransport::disconnect(const string &addr) { if (m_ReadDatathread) { m_ReadDatathread->interrupt(); exitBaseDispatch(); - while (m_ReadDatathread->timed_join(boost::posix_time::seconds(1)) == - false) { + while (m_ReadDatathread->timed_join(boost::posix_time::seconds(1)) == false) { LOG_WARN("join readDataThread fail, retry"); m_ReadDatathread->interrupt(); exitBaseDispatch(); @@ -238,32 +227,31 @@ void TcpTransport::runThread() { } LOG_INFO("event_base_dispatch exit once"); boost::this_thread::sleep(boost::posix_time::milliseconds(1)); - if (getTcpConnectStatus() != e_connectSuccess) return; + if (getTcpConnectStatus() != e_connectSuccess) + return; } -void TcpTransport::timeoutcb(evutil_socket_t fd, short what, void *arg) { +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; + TcpTransport* tcpTrans = (TcpTransport*)arg; if (tcpTrans->getTcpConnectStatus() != e_connectSuccess) { - LOG_INFO("timeoutcb: after connect time, tcp was not established on fd:%d", - fd); + 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) { +void TcpTransport::eventcb(struct bufferevent* bev, short what, void* ctx) { evutil_socket_t fd = bufferevent_getfd(bev); - TcpTransport *tcpTrans = (TcpTransport *)ctx; + 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)); + 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)) { + } 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); @@ -274,8 +262,7 @@ void TcpTransport::eventcb(struct bufferevent *bev, short what, void *ctx) { } } -void TcpTransport::readNextMessageIntCallback(struct bufferevent *bev, - void *ctx) { +void TcpTransport::readNextMessageIntCallback(struct bufferevent* bev, void* ctx) { /* This callback is invoked when there is data to read on bev. */ // protocol:
@@ -286,14 +273,14 @@ void TcpTransport::readNextMessageIntCallback(struct bufferevent *bev, // 3, use json to serialization data // 4, application could self-defination binary data - struct evbuffer *input = bufferevent_get_input(bev); + 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; + char* p = hdr; unsigned int needed = 4; for (idx = 0; idx < n; idx++) { @@ -311,37 +298,33 @@ void TcpTransport::readNextMessageIntCallback(struct bufferevent *bev, 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 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); + 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)); + 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); + 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; + char* data = messageData.getData() + bytesRead; bufferevent_read(bev, data, 4); bytesRead = bufferevent_read(bev, data, bytesInMessage); - TcpTransport *tcpTrans = (TcpTransport *)ctx; + TcpTransport* tcpTrans = (TcpTransport*)ctx; tcpTrans->messageReceived(messageData); } } } -bool TcpTransport::sendMessage(const char *pData, int len) { +bool TcpTransport::sendMessage(const char* pData, int len) { boost::lock_guard lock(m_socketLock); if (getTcpConnectStatus() != e_connectSuccess) { return false; @@ -349,7 +332,7 @@ bool TcpTransport::sendMessage(const char *pData, int len) { int bytes_left = len; int bytes_written = 0; - const char *ptr = pData; + const char* ptr = pData; /*NOTE: 1. do not need to consider large data which could not send by once, as @@ -365,7 +348,7 @@ bool TcpTransport::sendMessage(const char *pData, int len) { return false; } -void TcpTransport::messageReceived(const MemoryBlock &mem) { +void TcpTransport::messageReceived(const MemoryBlock& mem) { if (m_readcallback) { m_readcallback(m_tcpRemotingClient, mem, getPeerAddrAndPort()); } @@ -377,10 +360,8 @@ const string TcpTransport::getPeerAddrAndPort() { // 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)); + 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))); @@ -389,6 +370,8 @@ const string TcpTransport::getPeerAddrAndPort() { return brokerAddr; } -const uint64_t TcpTransport::getStartTime() const { return m_startTime; } +const uint64_t TcpTransport::getStartTime() const { + return m_startTime; +} } // namespace rocketmq diff --git a/src/transport/TcpTransport.h b/src/transport/TcpTransport.h old mode 100755 new mode 100644 index fa4da85b4..cda03ca78 --- a/src/transport/TcpTransport.h +++ b/src/transport/TcpTransport.h @@ -32,63 +32,55 @@ extern "C" { namespace rocketmq { // 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::mutex m_event_base_mtx; boost::condition_variable_any m_event_base_cv; //& properties = msg1.getProperties(); - //for (auto& pair : properties) { - // std::cout << pair.first << " : " << pair.second << std::endl; - //} + MQMessage msg1("topic", "*", "test"); + // 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; - string encodeMessage = BatchMessage::encode(msg1); - EXPECT_EQ(encodeMessage.size(), 43); + EXPECT_EQ(msg1.getProperties().size(), 2); + EXPECT_EQ(msg1.getBody().size(), 4); + // 20 + bodyLen + 2 + propertiesLength; + string encodeMessage = BatchMessage::encode(msg1); + EXPECT_EQ(encodeMessage.size(), 43); - msg1.setProperty(MQMessage::PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX, "1"); - encodeMessage = BatchMessage::encode(msg1); - EXPECT_EQ(encodeMessage.size(), 54); + msg1.setProperty(MQMessage::PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX, "1"); + encodeMessage = BatchMessage::encode(msg1); + EXPECT_EQ(encodeMessage.size(), 54); } TEST(BatchMessageEncodeTest, encodeMQMessages) { - std::vector msgs; - MQMessage msg1("topic", "*", "test1"); - //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; - string encodeMessage = BatchMessage::encode(msgs); - EXPECT_EQ(encodeMessage.size(), 86); - MQMessage msg2("topic", "*", "test2"); - MQMessage msg3("topic", "*", "test3"); - msgs.push_back(msg2); - msgs.push_back(msg3); - encodeMessage = BatchMessage::encode(msgs); - EXPECT_EQ(encodeMessage.size(), 258);//86*3 + std::vector msgs; + MQMessage msg1("topic", "*", "test1"); + // 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; + string encodeMessage = BatchMessage::encode(msgs); + EXPECT_EQ(encodeMessage.size(), 86); + MQMessage msg2("topic", "*", "test2"); + MQMessage msg3("topic", "*", "test3"); + msgs.push_back(msg2); + msgs.push_back(msg3); + encodeMessage = BatchMessage::encode(msgs); + EXPECT_EQ(encodeMessage.size(), 258); // 86*3 } int main(int argc, char* argv[]) { - InitGoogleMock(&argc, argv); - return RUN_ALL_TESTS(); + InitGoogleMock(&argc, argv); + return RUN_ALL_TESTS(); } diff --git a/test/src/StringIdMakerTest.cpp b/test/src/StringIdMakerTest.cpp index 8889d28da..4fa25d3b3 100644 --- a/test/src/StringIdMakerTest.cpp +++ b/test/src/StringIdMakerTest.cpp @@ -27,12 +27,12 @@ using ::testing::InitGoogleMock; using testing::Return; TEST(StringIdMakerTest, get_unique_id) { - string unique_id = StringIdMaker::get_mutable_instance().get_unique_id(); - cout << "unique_id: " << unique_id << endl; - EXPECT_EQ(unique_id.size(), 32); + string unique_id = StringIdMaker::get_mutable_instance().get_unique_id(); + cout << "unique_id: " << unique_id << endl; + EXPECT_EQ(unique_id.size(), 32); } int main(int argc, char* argv[]) { - InitGoogleMock(&argc, argv); - return RUN_ALL_TESTS(); + InitGoogleMock(&argc, argv); + return RUN_ALL_TESTS(); } diff --git a/test/src/UrlTest.cpp b/test/src/UrlTest.cpp index b347e221f..636c2dee2 100644 --- a/test/src/UrlTest.cpp +++ b/test/src/UrlTest.cpp @@ -44,33 +44,33 @@ using ::testing::InitGoogleTest; using testing::Return; class MockTopicConfig : public TopicConfig { - public: - MOCK_METHOD0(getReadQueueNums, int()); + public: + MOCK_METHOD0(getReadQueueNums, int()); }; TEST(Url, Url) { - Url url_s("172.17.0.2:9876"); - EXPECT_EQ(url_s.protocol_, "172.17.0.2:9876"); + Url url_s("172.17.0.2:9876"); + EXPECT_EQ(url_s.protocol_, "172.17.0.2:9876"); - Url url_z("https://www.aliyun.com/RocketMQ?5.0"); - EXPECT_EQ(url_z.protocol_, "https"); - EXPECT_EQ(url_z.host_, "www.aliyun.com"); - EXPECT_EQ(url_z.port_, "80"); - EXPECT_EQ(url_z.path_, "/RocketMQ"); - EXPECT_EQ(url_z.query_, "5.0"); + Url url_z("https://www.aliyun.com/RocketMQ?5.0"); + EXPECT_EQ(url_z.protocol_, "https"); + EXPECT_EQ(url_z.host_, "www.aliyun.com"); + EXPECT_EQ(url_z.port_, "80"); + EXPECT_EQ(url_z.path_, "/RocketMQ"); + EXPECT_EQ(url_z.query_, "5.0"); - Url url_path("https://www.aliyun.com:9876/RocketMQ?5.0"); - EXPECT_EQ(url_path.port_, "9876"); - MockTopicConfig topicConfig; - EXPECT_CALL(topicConfig, getReadQueueNums()).WillRepeatedly(Return(-1)); - int nums = topicConfig.getReadQueueNums(); - cout << nums << endl; + Url url_path("https://www.aliyun.com:9876/RocketMQ?5.0"); + EXPECT_EQ(url_path.port_, "9876"); + MockTopicConfig topicConfig; + EXPECT_CALL(topicConfig, getReadQueueNums()).WillRepeatedly(Return(-1)); + int nums = topicConfig.getReadQueueNums(); + cout << nums << endl; } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(filter) = "Url.Url"; - int itestts = RUN_ALL_TESTS(); - ; - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(filter) = "Url.Url"; + int itestts = RUN_ALL_TESTS(); + ; + return itestts; } diff --git a/test/src/common/ClientRPCHookTest.cpp b/test/src/common/ClientRPCHookTest.cpp index eb6cd695c..6fd48b631 100644 --- a/test/src/common/ClientRPCHookTest.cpp +++ b/test/src/common/ClientRPCHookTest.cpp @@ -33,28 +33,28 @@ using rocketmq::SessionCredentials; using rocketmq::ClientRPCHook; TEST(clientRPCHook, doBeforeRequest) { - SessionCredentials sessionCredentials; - sessionCredentials.setAccessKey("accessKey"); - sessionCredentials.setSecretKey("secretKey"); - sessionCredentials.setAuthChannel("onsChannel"); + SessionCredentials sessionCredentials; + sessionCredentials.setAccessKey("accessKey"); + sessionCredentials.setSecretKey("secretKey"); + sessionCredentials.setAuthChannel("onsChannel"); - ClientRPCHook clientRPCHook(sessionCredentials); + ClientRPCHook clientRPCHook(sessionCredentials); - RemotingCommand remotingCommand; - clientRPCHook.doBeforeRequest("127.0.0.1:9876", remotingCommand); + RemotingCommand remotingCommand; + clientRPCHook.doBeforeRequest("127.0.0.1:9876", remotingCommand); - SendMessageRequestHeader *sendMessageRequestHeader = new SendMessageRequestHeader(); - RemotingCommand headeRremotingCommand(17, sendMessageRequestHeader); - clientRPCHook.doBeforeRequest("127.0.0.1:9876", headeRremotingCommand); + SendMessageRequestHeader* sendMessageRequestHeader = new SendMessageRequestHeader(); + RemotingCommand headeRremotingCommand(17, sendMessageRequestHeader); + clientRPCHook.doBeforeRequest("127.0.0.1:9876", headeRremotingCommand); - headeRremotingCommand.setMsgBody("1231231"); - clientRPCHook.doBeforeRequest("127.0.0.1:9876", headeRremotingCommand); + headeRremotingCommand.setMsgBody("1231231"); + clientRPCHook.doBeforeRequest("127.0.0.1:9876", headeRremotingCommand); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "clientRPCHook.doBeforeRequest"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "clientRPCHook.doBeforeRequest"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/common/MemoryBlockTest.cpp b/test/src/common/MemoryBlockTest.cpp index a81dc8a6a..bfd7b92f6 100644 --- a/test/src/common/MemoryBlockTest.cpp +++ b/test/src/common/MemoryBlockTest.cpp @@ -30,145 +30,145 @@ using testing::Return; using rocketmq::MemoryBlock; TEST(memoryBlock, init) { - MemoryBlock memoryBlock; - EXPECT_EQ(memoryBlock.getSize(), 0); - EXPECT_TRUE(memoryBlock.getData() == nullptr); - - MemoryBlock twoMemoryBlock(-1, true); - EXPECT_EQ(twoMemoryBlock.getSize(), 0); - EXPECT_TRUE(twoMemoryBlock.getData() == nullptr); - - MemoryBlock threeMemoryBlock(10, true); - EXPECT_EQ(threeMemoryBlock.getSize(), 10); - EXPECT_TRUE(threeMemoryBlock.getData() != nullptr); - - MemoryBlock frouMemoryBlock(12, false); - EXPECT_EQ(frouMemoryBlock.getSize(), 12); - EXPECT_TRUE(frouMemoryBlock.getData() != nullptr); - - char *buf = (char *) malloc(sizeof(char) * 9); - strcpy(buf, "RocketMQ"); - MemoryBlock fiveMemoryBlock(buf, -1); - EXPECT_EQ(fiveMemoryBlock.getSize(), -1); - EXPECT_TRUE(fiveMemoryBlock.getData() == nullptr); - - char *bufNull = NULL; - MemoryBlock sixMemoryBlock(bufNull, 16); - EXPECT_EQ(sixMemoryBlock.getSize(), 16); - EXPECT_TRUE(sixMemoryBlock.getData() != nullptr); - - MemoryBlock sevenMemoryBlock(buf, 20); - EXPECT_EQ(sevenMemoryBlock.getSize(), 20); - sevenMemoryBlock.getData(); - EXPECT_EQ(string(sevenMemoryBlock.getData()), string(buf)); - - MemoryBlock nineMemoryBlock(sevenMemoryBlock); - EXPECT_EQ(nineMemoryBlock.getSize(), sevenMemoryBlock.getSize()); - EXPECT_EQ(string(nineMemoryBlock.getData()), string(sevenMemoryBlock.getData())); - - MemoryBlock eightMemoryBlock(fiveMemoryBlock); - EXPECT_EQ(eightMemoryBlock.getSize(), fiveMemoryBlock.getSize()); - EXPECT_TRUE(eightMemoryBlock.getData() == nullptr); - - free(buf); + MemoryBlock memoryBlock; + EXPECT_EQ(memoryBlock.getSize(), 0); + EXPECT_TRUE(memoryBlock.getData() == nullptr); + + MemoryBlock twoMemoryBlock(-1, true); + EXPECT_EQ(twoMemoryBlock.getSize(), 0); + EXPECT_TRUE(twoMemoryBlock.getData() == nullptr); + + MemoryBlock threeMemoryBlock(10, true); + EXPECT_EQ(threeMemoryBlock.getSize(), 10); + EXPECT_TRUE(threeMemoryBlock.getData() != nullptr); + + MemoryBlock frouMemoryBlock(12, false); + EXPECT_EQ(frouMemoryBlock.getSize(), 12); + EXPECT_TRUE(frouMemoryBlock.getData() != nullptr); + + char* buf = (char*)malloc(sizeof(char) * 9); + strcpy(buf, "RocketMQ"); + MemoryBlock fiveMemoryBlock(buf, -1); + EXPECT_EQ(fiveMemoryBlock.getSize(), -1); + EXPECT_TRUE(fiveMemoryBlock.getData() == nullptr); + + char* bufNull = NULL; + MemoryBlock sixMemoryBlock(bufNull, 16); + EXPECT_EQ(sixMemoryBlock.getSize(), 16); + EXPECT_TRUE(sixMemoryBlock.getData() != nullptr); + + MemoryBlock sevenMemoryBlock(buf, 20); + EXPECT_EQ(sevenMemoryBlock.getSize(), 20); + sevenMemoryBlock.getData(); + EXPECT_EQ(string(sevenMemoryBlock.getData()), string(buf)); + + MemoryBlock nineMemoryBlock(sevenMemoryBlock); + EXPECT_EQ(nineMemoryBlock.getSize(), sevenMemoryBlock.getSize()); + EXPECT_EQ(string(nineMemoryBlock.getData()), string(sevenMemoryBlock.getData())); + + MemoryBlock eightMemoryBlock(fiveMemoryBlock); + EXPECT_EQ(eightMemoryBlock.getSize(), fiveMemoryBlock.getSize()); + EXPECT_TRUE(eightMemoryBlock.getData() == nullptr); + + free(buf); } TEST(memoryBlock, operators) { - MemoryBlock memoryBlock(12, false); - - MemoryBlock operaterMemoryBlock = memoryBlock; - - EXPECT_TRUE(operaterMemoryBlock == memoryBlock); - - char *buf = (char *) malloc(sizeof(char) * 16); - memset(buf, 0, 16); - strcpy(buf, "RocketMQ"); - MemoryBlock twoMemoryBlock(buf, 12); - EXPECT_FALSE(memoryBlock == twoMemoryBlock); - - MemoryBlock threeMemoryBlock(buf, 16); - EXPECT_FALSE(memoryBlock == threeMemoryBlock); - EXPECT_TRUE(twoMemoryBlock != threeMemoryBlock); - - threeMemoryBlock.fillWith(49); - EXPECT_EQ(string(threeMemoryBlock.getData(), 16), "1111111111111111"); - - threeMemoryBlock.reset(); - EXPECT_EQ(threeMemoryBlock.getSize(), 0); - EXPECT_TRUE(threeMemoryBlock.getData() == nullptr); - - threeMemoryBlock.setSize(16, 0); - EXPECT_EQ(threeMemoryBlock.getSize(), 16); - // EXPECT_EQ(threeMemoryBlock.getData() , buf); - - threeMemoryBlock.setSize(0, 0); - EXPECT_EQ(threeMemoryBlock.getSize(), 0); - EXPECT_TRUE(threeMemoryBlock.getData() == nullptr); - - MemoryBlock appendMemoryBlock; - EXPECT_EQ(appendMemoryBlock.getSize(), 0); - EXPECT_TRUE(appendMemoryBlock.getData() == nullptr); - - appendMemoryBlock.append(buf, -1); - EXPECT_EQ(appendMemoryBlock.getSize(), 0); - EXPECT_TRUE(appendMemoryBlock.getData() == nullptr); - - appendMemoryBlock.append(buf, 8); - EXPECT_EQ(appendMemoryBlock.getSize(), 8); - - MemoryBlock replaceWithMemoryBlock; - replaceWithMemoryBlock.append(buf, 8); - - char *aliyunBuf = (char *) malloc(sizeof(char) * 8); - memset(aliyunBuf, 0, 8); - strcpy(aliyunBuf, "aliyun"); - replaceWithMemoryBlock.replaceWith(aliyunBuf, 0); - EXPECT_EQ(replaceWithMemoryBlock.getSize(), 8); - EXPECT_EQ(string(replaceWithMemoryBlock.getData(), 8), "RocketMQ"); - - replaceWithMemoryBlock.replaceWith(aliyunBuf, 6); - EXPECT_EQ(replaceWithMemoryBlock.getSize(), 6); - EXPECT_EQ(string(replaceWithMemoryBlock.getData(), strlen(aliyunBuf)), "aliyun"); - - MemoryBlock insertMemoryBlock; - insertMemoryBlock.append(buf, 8); - insertMemoryBlock.insert(aliyunBuf, -1, -1); - EXPECT_EQ(string(insertMemoryBlock.getData(), 8), "RocketMQ"); - - /* MemoryBlock fourInsertMemoryBlock; - fourInsertMemoryBlock.append(buf , 8); - // 6+ (-1) - fourInsertMemoryBlock.insert(aliyunBuf , 8 , -1); - string fourStr( fourInsertMemoryBlock.getData()); - EXPECT_TRUE( fourStr == "liyun");*/ - - MemoryBlock twoInsertMemoryBlock; - twoInsertMemoryBlock.append(buf, 8); - twoInsertMemoryBlock.insert(aliyunBuf, strlen(aliyunBuf), 0); - EXPECT_EQ(string(twoInsertMemoryBlock.getData(), 8 + strlen(aliyunBuf)), "aliyunRocketMQ"); - - MemoryBlock threeInsertMemoryBlock; - threeInsertMemoryBlock.append(buf, 8); - threeInsertMemoryBlock.insert(aliyunBuf, 6, 100); - EXPECT_EQ(string(threeInsertMemoryBlock.getData(), 8 + strlen(aliyunBuf)), "RocketMQaliyun"); - - MemoryBlock removeSectionMemoryBlock(buf, 8); - removeSectionMemoryBlock.removeSection(8, -1); - EXPECT_EQ(string(removeSectionMemoryBlock.getData(), 8), "RocketMQ"); - - MemoryBlock twoRemoveSectionMemoryBlock(buf, 8); - twoRemoveSectionMemoryBlock.removeSection(1, 4); - string str(twoRemoveSectionMemoryBlock.getData(), 4); - EXPECT_TRUE(str == "RtMQ"); - - free(buf); - free(aliyunBuf); + MemoryBlock memoryBlock(12, false); + + MemoryBlock operaterMemoryBlock = memoryBlock; + + EXPECT_TRUE(operaterMemoryBlock == memoryBlock); + + char* buf = (char*)malloc(sizeof(char) * 16); + memset(buf, 0, 16); + strcpy(buf, "RocketMQ"); + MemoryBlock twoMemoryBlock(buf, 12); + EXPECT_FALSE(memoryBlock == twoMemoryBlock); + + MemoryBlock threeMemoryBlock(buf, 16); + EXPECT_FALSE(memoryBlock == threeMemoryBlock); + EXPECT_TRUE(twoMemoryBlock != threeMemoryBlock); + + threeMemoryBlock.fillWith(49); + EXPECT_EQ(string(threeMemoryBlock.getData(), 16), "1111111111111111"); + + threeMemoryBlock.reset(); + EXPECT_EQ(threeMemoryBlock.getSize(), 0); + EXPECT_TRUE(threeMemoryBlock.getData() == nullptr); + + threeMemoryBlock.setSize(16, 0); + EXPECT_EQ(threeMemoryBlock.getSize(), 16); + // EXPECT_EQ(threeMemoryBlock.getData() , buf); + + threeMemoryBlock.setSize(0, 0); + EXPECT_EQ(threeMemoryBlock.getSize(), 0); + EXPECT_TRUE(threeMemoryBlock.getData() == nullptr); + + MemoryBlock appendMemoryBlock; + EXPECT_EQ(appendMemoryBlock.getSize(), 0); + EXPECT_TRUE(appendMemoryBlock.getData() == nullptr); + + appendMemoryBlock.append(buf, -1); + EXPECT_EQ(appendMemoryBlock.getSize(), 0); + EXPECT_TRUE(appendMemoryBlock.getData() == nullptr); + + appendMemoryBlock.append(buf, 8); + EXPECT_EQ(appendMemoryBlock.getSize(), 8); + + MemoryBlock replaceWithMemoryBlock; + replaceWithMemoryBlock.append(buf, 8); + + char* aliyunBuf = (char*)malloc(sizeof(char) * 8); + memset(aliyunBuf, 0, 8); + strcpy(aliyunBuf, "aliyun"); + replaceWithMemoryBlock.replaceWith(aliyunBuf, 0); + EXPECT_EQ(replaceWithMemoryBlock.getSize(), 8); + EXPECT_EQ(string(replaceWithMemoryBlock.getData(), 8), "RocketMQ"); + + replaceWithMemoryBlock.replaceWith(aliyunBuf, 6); + EXPECT_EQ(replaceWithMemoryBlock.getSize(), 6); + EXPECT_EQ(string(replaceWithMemoryBlock.getData(), strlen(aliyunBuf)), "aliyun"); + + MemoryBlock insertMemoryBlock; + insertMemoryBlock.append(buf, 8); + insertMemoryBlock.insert(aliyunBuf, -1, -1); + EXPECT_EQ(string(insertMemoryBlock.getData(), 8), "RocketMQ"); + + /* MemoryBlock fourInsertMemoryBlock; + fourInsertMemoryBlock.append(buf , 8); + // 6+ (-1) + fourInsertMemoryBlock.insert(aliyunBuf , 8 , -1); + string fourStr( fourInsertMemoryBlock.getData()); + EXPECT_TRUE( fourStr == "liyun");*/ + + MemoryBlock twoInsertMemoryBlock; + twoInsertMemoryBlock.append(buf, 8); + twoInsertMemoryBlock.insert(aliyunBuf, strlen(aliyunBuf), 0); + EXPECT_EQ(string(twoInsertMemoryBlock.getData(), 8 + strlen(aliyunBuf)), "aliyunRocketMQ"); + + MemoryBlock threeInsertMemoryBlock; + threeInsertMemoryBlock.append(buf, 8); + threeInsertMemoryBlock.insert(aliyunBuf, 6, 100); + EXPECT_EQ(string(threeInsertMemoryBlock.getData(), 8 + strlen(aliyunBuf)), "RocketMQaliyun"); + + MemoryBlock removeSectionMemoryBlock(buf, 8); + removeSectionMemoryBlock.removeSection(8, -1); + EXPECT_EQ(string(removeSectionMemoryBlock.getData(), 8), "RocketMQ"); + + MemoryBlock twoRemoveSectionMemoryBlock(buf, 8); + twoRemoveSectionMemoryBlock.removeSection(1, 4); + string str(twoRemoveSectionMemoryBlock.getData(), 4); + EXPECT_TRUE(str == "RtMQ"); + + free(buf); + free(aliyunBuf); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "memoryBlock.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "memoryBlock.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/common/MemoryOutputStreamTest.cpp b/test/src/common/MemoryOutputStreamTest.cpp index 6cf10153f..05af181e0 100644 --- a/test/src/common/MemoryOutputStreamTest.cpp +++ b/test/src/common/MemoryOutputStreamTest.cpp @@ -34,191 +34,191 @@ using rocketmq::MemoryInputStream; using rocketmq::MemoryOutputStream; TEST(memoryOutputStream, init) { - MemoryOutputStream memoryOutput; - EXPECT_EQ(memoryOutput.getMemoryBlock().getSize(), 0); - EXPECT_TRUE(memoryOutput.getData() != nullptr); - - EXPECT_EQ(memoryOutput.getPosition(), 0); - EXPECT_EQ(memoryOutput.getDataSize(), 0); - - MemoryOutputStream twoMemoryOutput(512); - EXPECT_EQ(memoryOutput.getMemoryBlock().getSize(), 0); - - MemoryBlock memoryBlock(12, false); - MemoryOutputStream threeMemoryOutput(memoryBlock, false); - EXPECT_EQ(threeMemoryOutput.getPosition(), 0); - EXPECT_EQ(threeMemoryOutput.getDataSize(), 0); - - MemoryOutputStream frouMemoryOutput(memoryBlock, true); - EXPECT_EQ(frouMemoryOutput.getPosition(), memoryBlock.getSize()); - EXPECT_EQ(frouMemoryOutput.getDataSize(), memoryBlock.getSize()); - - char *buf = (char *) malloc(sizeof(char) * 9); - strcpy(buf, "RocketMQ"); - MemoryOutputStream fiveMemoryOutputStream(buf, 8); - EXPECT_EQ(fiveMemoryOutputStream.getData(), buf); - - fiveMemoryOutputStream.reset(); - EXPECT_EQ(memoryOutput.getPosition(), 0); - EXPECT_EQ(memoryOutput.getDataSize(), 0); - free(buf); + MemoryOutputStream memoryOutput; + EXPECT_EQ(memoryOutput.getMemoryBlock().getSize(), 0); + EXPECT_TRUE(memoryOutput.getData() != nullptr); + + EXPECT_EQ(memoryOutput.getPosition(), 0); + EXPECT_EQ(memoryOutput.getDataSize(), 0); + + MemoryOutputStream twoMemoryOutput(512); + EXPECT_EQ(memoryOutput.getMemoryBlock().getSize(), 0); + + MemoryBlock memoryBlock(12, false); + MemoryOutputStream threeMemoryOutput(memoryBlock, false); + EXPECT_EQ(threeMemoryOutput.getPosition(), 0); + EXPECT_EQ(threeMemoryOutput.getDataSize(), 0); + + MemoryOutputStream frouMemoryOutput(memoryBlock, true); + EXPECT_EQ(frouMemoryOutput.getPosition(), memoryBlock.getSize()); + EXPECT_EQ(frouMemoryOutput.getDataSize(), memoryBlock.getSize()); + + char* buf = (char*)malloc(sizeof(char) * 9); + strcpy(buf, "RocketMQ"); + MemoryOutputStream fiveMemoryOutputStream(buf, 8); + EXPECT_EQ(fiveMemoryOutputStream.getData(), buf); + + fiveMemoryOutputStream.reset(); + EXPECT_EQ(memoryOutput.getPosition(), 0); + EXPECT_EQ(memoryOutput.getDataSize(), 0); + free(buf); } TEST(memoryOutputStream, flush) { - char *buf = (char *) malloc(sizeof(char) * 9); - strcpy(buf, "RocketMQ"); - MemoryOutputStream memoryOutput; - memoryOutput.write(buf, 9); - memoryOutput.flush(); - EXPECT_FALSE(memoryOutput.getData() == buf); - free(buf); + char* buf = (char*)malloc(sizeof(char) * 9); + strcpy(buf, "RocketMQ"); + MemoryOutputStream memoryOutput; + memoryOutput.write(buf, 9); + memoryOutput.flush(); + EXPECT_FALSE(memoryOutput.getData() == buf); + free(buf); } TEST(memoryOutputStream, preallocate) { - MemoryOutputStream memoryOutput; - memoryOutput.preallocate(250); + MemoryOutputStream memoryOutput; + memoryOutput.preallocate(250); - memoryOutput.preallocate(256); + memoryOutput.preallocate(256); } TEST(memoryOutputStream, getMemoryBlock) { - char *buf = (char *) malloc(sizeof(char) * 9); - strcpy(buf, "RocketMQ"); - MemoryOutputStream memoryOutput; - memoryOutput.write(buf, 9); - MemoryBlock memoryBlock = memoryOutput.getMemoryBlock(); - EXPECT_EQ(memoryBlock.getSize(), memoryOutput.getDataSize()); - free(buf); + char* buf = (char*)malloc(sizeof(char) * 9); + strcpy(buf, "RocketMQ"); + MemoryOutputStream memoryOutput; + memoryOutput.write(buf, 9); + MemoryBlock memoryBlock = memoryOutput.getMemoryBlock(); + EXPECT_EQ(memoryBlock.getSize(), memoryOutput.getDataSize()); + free(buf); } TEST(memoryOutputStream, prepareToWriteAndGetData) { - char *buf = (char *) malloc(sizeof(char) * 9); - strcpy(buf, "RocketMQ"); - MemoryOutputStream memoryOutput(buf, 9); - EXPECT_EQ(memoryOutput.getData(), buf); - - // prepareToWrite - // EXPECT_TRUE(memoryOutput.writeIntBigEndian(123)); - EXPECT_TRUE(memoryOutput.writeByte('r')); - const char *data = static_cast(memoryOutput.getData()); - EXPECT_EQ(string(data), "rocketMQ"); - - MemoryOutputStream blockMmoryOutput(8); - char *memoryData = (char *) blockMmoryOutput.getData(); - - EXPECT_EQ(memoryData[blockMmoryOutput.getDataSize()], 0); - - blockMmoryOutput.write(buf, 8); - blockMmoryOutput.write(buf, 8); - data = static_cast(blockMmoryOutput.getData()); - EXPECT_EQ(string(data), "rocketMQrocketMQ"); - free(buf); + char* buf = (char*)malloc(sizeof(char) * 9); + strcpy(buf, "RocketMQ"); + MemoryOutputStream memoryOutput(buf, 9); + EXPECT_EQ(memoryOutput.getData(), buf); + + // prepareToWrite + // EXPECT_TRUE(memoryOutput.writeIntBigEndian(123)); + EXPECT_TRUE(memoryOutput.writeByte('r')); + const char* data = static_cast(memoryOutput.getData()); + EXPECT_EQ(string(data), "rocketMQ"); + + MemoryOutputStream blockMmoryOutput(8); + char* memoryData = (char*)blockMmoryOutput.getData(); + + EXPECT_EQ(memoryData[blockMmoryOutput.getDataSize()], 0); + + blockMmoryOutput.write(buf, 8); + blockMmoryOutput.write(buf, 8); + data = static_cast(blockMmoryOutput.getData()); + EXPECT_EQ(string(data), "rocketMQrocketMQ"); + free(buf); } TEST(memoryOutputStream, position) { - char *buf = (char *) malloc(sizeof(char) * 9); - strcpy(buf, "RocketMQ"); + char* buf = (char*)malloc(sizeof(char) * 9); + strcpy(buf, "RocketMQ"); - MemoryOutputStream memoryOutput; - EXPECT_EQ(memoryOutput.getPosition(), 0); + MemoryOutputStream memoryOutput; + EXPECT_EQ(memoryOutput.getPosition(), 0); - memoryOutput.write(buf, 8); - EXPECT_EQ(memoryOutput.getPosition(), 8); + memoryOutput.write(buf, 8); + EXPECT_EQ(memoryOutput.getPosition(), 8); - EXPECT_FALSE(memoryOutput.setPosition(9)); + EXPECT_FALSE(memoryOutput.setPosition(9)); - EXPECT_TRUE(memoryOutput.setPosition(-1)); - EXPECT_EQ(memoryOutput.getPosition(), 0); + EXPECT_TRUE(memoryOutput.setPosition(-1)); + EXPECT_EQ(memoryOutput.getPosition(), 0); - EXPECT_TRUE(memoryOutput.setPosition(8)); - EXPECT_EQ(memoryOutput.getPosition(), 8); + EXPECT_TRUE(memoryOutput.setPosition(8)); + EXPECT_EQ(memoryOutput.getPosition(), 8); - EXPECT_TRUE(memoryOutput.setPosition(7)); - EXPECT_EQ(memoryOutput.getPosition(), 7); - free(buf); + EXPECT_TRUE(memoryOutput.setPosition(7)); + EXPECT_EQ(memoryOutput.getPosition(), 7); + free(buf); } TEST(memoryOutputStream, write) { - MemoryOutputStream memoryOutput; - MemoryInputStream memoryInput(memoryOutput.getData(), 256, false); + MemoryOutputStream memoryOutput; + MemoryInputStream memoryInput(memoryOutput.getData(), 256, false); - EXPECT_TRUE(memoryOutput.writeBool(true)); - EXPECT_TRUE(memoryInput.readBool()); + EXPECT_TRUE(memoryOutput.writeBool(true)); + EXPECT_TRUE(memoryInput.readBool()); - EXPECT_TRUE(memoryOutput.writeBool(false)); - EXPECT_FALSE(memoryInput.readBool()); + EXPECT_TRUE(memoryOutput.writeBool(false)); + EXPECT_FALSE(memoryInput.readBool()); - EXPECT_TRUE(memoryOutput.writeByte('a')); - EXPECT_EQ(memoryInput.readByte(), 'a'); + EXPECT_TRUE(memoryOutput.writeByte('a')); + EXPECT_EQ(memoryInput.readByte(), 'a'); - EXPECT_TRUE(memoryOutput.writeShortBigEndian(128)); - EXPECT_EQ(memoryInput.readShortBigEndian(), 128); + EXPECT_TRUE(memoryOutput.writeShortBigEndian(128)); + EXPECT_EQ(memoryInput.readShortBigEndian(), 128); - EXPECT_TRUE(memoryOutput.writeIntBigEndian(123)); - EXPECT_EQ(memoryInput.readIntBigEndian(), 123); + EXPECT_TRUE(memoryOutput.writeIntBigEndian(123)); + EXPECT_EQ(memoryInput.readIntBigEndian(), 123); - EXPECT_TRUE(memoryOutput.writeInt64BigEndian(123123)); - EXPECT_EQ(memoryInput.readInt64BigEndian(), 123123); + EXPECT_TRUE(memoryOutput.writeInt64BigEndian(123123)); + EXPECT_EQ(memoryInput.readInt64BigEndian(), 123123); - EXPECT_TRUE(memoryOutput.writeDoubleBigEndian(12.71)); - EXPECT_EQ(memoryInput.readDoubleBigEndian(), 12.71); + EXPECT_TRUE(memoryOutput.writeDoubleBigEndian(12.71)); + EXPECT_EQ(memoryInput.readDoubleBigEndian(), 12.71); - EXPECT_TRUE(memoryOutput.writeFloatBigEndian(12.1)); - float f = 12.1; - EXPECT_EQ(memoryInput.readFloatBigEndian(), f); + EXPECT_TRUE(memoryOutput.writeFloatBigEndian(12.1)); + float f = 12.1; + EXPECT_EQ(memoryInput.readFloatBigEndian(), f); - // EXPECT_TRUE(memoryOutput.writeRepeatedByte(8 , 8)); + // EXPECT_TRUE(memoryOutput.writeRepeatedByte(8 , 8)); } TEST(memoryInputStream, info) { - char *buf = (char *) malloc(sizeof(char) * 9); - strcpy(buf, "RocketMQ"); + char* buf = (char*)malloc(sizeof(char) * 9); + strcpy(buf, "RocketMQ"); - MemoryInputStream memoryInput(buf, 8, false); + MemoryInputStream memoryInput(buf, 8, false); - char *memoryData = (char *) memoryInput.getData(); - EXPECT_EQ(memoryData, buf); + char* memoryData = (char*)memoryInput.getData(); + EXPECT_EQ(memoryData, buf); - EXPECT_EQ(memoryInput.getTotalLength(), 8); + EXPECT_EQ(memoryInput.getTotalLength(), 8); - MemoryInputStream twoMemoryInput(buf, 8, true); - EXPECT_NE(twoMemoryInput.getData(), buf); + MemoryInputStream twoMemoryInput(buf, 8, true); + EXPECT_NE(twoMemoryInput.getData(), buf); - memoryData = (char *) twoMemoryInput.getData(); - EXPECT_NE(&memoryData, &buf); + memoryData = (char*)twoMemoryInput.getData(); + EXPECT_NE(&memoryData, &buf); - MemoryBlock memoryBlock(buf, 8); - MemoryInputStream threeMemoryInput(memoryBlock, false); - memoryData = (char *) threeMemoryInput.getData(); - EXPECT_EQ(memoryData, threeMemoryInput.getData()); - EXPECT_EQ(threeMemoryInput.getTotalLength(), 8); + MemoryBlock memoryBlock(buf, 8); + MemoryInputStream threeMemoryInput(memoryBlock, false); + memoryData = (char*)threeMemoryInput.getData(); + EXPECT_EQ(memoryData, threeMemoryInput.getData()); + EXPECT_EQ(threeMemoryInput.getTotalLength(), 8); - MemoryInputStream frouMemoryInput(memoryBlock, true); - EXPECT_NE(frouMemoryInput.getData(), memoryBlock.getData()); - free(buf); + MemoryInputStream frouMemoryInput(memoryBlock, true); + EXPECT_NE(frouMemoryInput.getData(), memoryBlock.getData()); + free(buf); } TEST(memoryInputStream, position) { - char *buf = (char *) malloc(sizeof(char) * 9); - strcpy(buf, "RocketMQ"); + char* buf = (char*)malloc(sizeof(char) * 9); + strcpy(buf, "RocketMQ"); - MemoryInputStream memoryInput(buf, 8, false); - EXPECT_EQ(memoryInput.getPosition(), 0); - EXPECT_FALSE(memoryInput.isExhausted()); + MemoryInputStream memoryInput(buf, 8, false); + EXPECT_EQ(memoryInput.getPosition(), 0); + EXPECT_FALSE(memoryInput.isExhausted()); - memoryInput.setPosition(9); - EXPECT_EQ(memoryInput.getPosition(), 8); - EXPECT_TRUE(memoryInput.isExhausted()); + memoryInput.setPosition(9); + EXPECT_EQ(memoryInput.getPosition(), 8); + EXPECT_TRUE(memoryInput.isExhausted()); - memoryInput.setPosition(-1); - EXPECT_EQ(memoryInput.getPosition(), 0); - free(buf); + memoryInput.setPosition(-1); + EXPECT_EQ(memoryInput.getPosition(), 0); + free(buf); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "*.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "*.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/common/NamesrvConfigTest.cpp b/test/src/common/NamesrvConfigTest.cpp index 1ac2c13dd..e4e1206ae 100644 --- a/test/src/common/NamesrvConfigTest.cpp +++ b/test/src/common/NamesrvConfigTest.cpp @@ -31,20 +31,20 @@ using testing::Return; using rocketmq::NamesrvConfig; TEST(namesrvConfig, init) { - NamesrvConfig namesrvConfig; + NamesrvConfig namesrvConfig; - const string home = "/home/rocketmq"; - namesrvConfig.setRocketmqHome(home); - EXPECT_EQ(namesrvConfig.getRocketmqHome(), "/home/rocketmq"); + const string home = "/home/rocketmq"; + namesrvConfig.setRocketmqHome(home); + EXPECT_EQ(namesrvConfig.getRocketmqHome(), "/home/rocketmq"); - namesrvConfig.setKvConfigPath("/home/rocketmq"); - EXPECT_EQ(namesrvConfig.getKvConfigPath(), "/home/rocketmq"); + namesrvConfig.setKvConfigPath("/home/rocketmq"); + EXPECT_EQ(namesrvConfig.getKvConfigPath(), "/home/rocketmq"); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "namesrvConfig.init"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "namesrvConfig.init"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/common/PermNametTest.cpp b/test/src/common/PermNametTest.cpp index 1a98e31bc..3296b84d7 100644 --- a/test/src/common/PermNametTest.cpp +++ b/test/src/common/PermNametTest.cpp @@ -26,21 +26,21 @@ using testing::Return; using rocketmq::PermName; TEST(permName, perm2String) { - EXPECT_EQ(PermName::perm2String(0), "---"); - EXPECT_EQ(PermName::perm2String(1), "--X"); - EXPECT_EQ(PermName::perm2String(2), "-W"); - EXPECT_EQ(PermName::perm2String(3), "-WX"); - EXPECT_EQ(PermName::perm2String(4), "R--"); - EXPECT_EQ(PermName::perm2String(5), "R-X"); - EXPECT_EQ(PermName::perm2String(6), "RW"); - EXPECT_EQ(PermName::perm2String(7), "RWX"); - EXPECT_EQ(PermName::perm2String(8), "---"); + EXPECT_EQ(PermName::perm2String(0), "---"); + EXPECT_EQ(PermName::perm2String(1), "--X"); + EXPECT_EQ(PermName::perm2String(2), "-W"); + EXPECT_EQ(PermName::perm2String(3), "-WX"); + EXPECT_EQ(PermName::perm2String(4), "R--"); + EXPECT_EQ(PermName::perm2String(5), "R-X"); + EXPECT_EQ(PermName::perm2String(6), "RW"); + EXPECT_EQ(PermName::perm2String(7), "RWX"); + EXPECT_EQ(PermName::perm2String(8), "---"); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "permName.perm2String"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "permName.perm2String"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/common/PullSysFlagTest.cpp b/test/src/common/PullSysFlagTest.cpp index 4e5ebd504..7b7c9db95 100644 --- a/test/src/common/PullSysFlagTest.cpp +++ b/test/src/common/PullSysFlagTest.cpp @@ -26,70 +26,70 @@ using testing::Return; using rocketmq::PullSysFlag; TEST(pullSysFlag, flag) { - EXPECT_EQ(PullSysFlag::buildSysFlag(false, false, false, false), 0); - - EXPECT_EQ(PullSysFlag::buildSysFlag(true, false, false, false), 1); - EXPECT_EQ(PullSysFlag::buildSysFlag(true, true, false, false), 3); - EXPECT_EQ(PullSysFlag::buildSysFlag(true, true, true, false), 7); - EXPECT_EQ(PullSysFlag::buildSysFlag(true, true, true, true), 15); - - EXPECT_EQ(PullSysFlag::buildSysFlag(false, true, false, false), 2); - EXPECT_EQ(PullSysFlag::buildSysFlag(false, true, true, false), 6); - EXPECT_EQ(PullSysFlag::buildSysFlag(false, true, true, true), 14); - - EXPECT_EQ(PullSysFlag::buildSysFlag(false, false, true, false), 4); - EXPECT_EQ(PullSysFlag::buildSysFlag(false, false, true, true), 12); - - EXPECT_EQ(PullSysFlag::buildSysFlag(false, false, false, true), 8); - - int FLAG_COMMIT_OFFSET = 0x1 << 0; - int FLAG_SUSPEND = 0x1 << 1; - int FLAG_SUBSCRIPTION = 0x1 << 2; - int FLAG_CLASS_FILTER = 0x1 << 3; - - for (int i = 0; i < 16; i++) { - if ((i & FLAG_COMMIT_OFFSET) == FLAG_COMMIT_OFFSET) { - EXPECT_TRUE(PullSysFlag::hasCommitOffsetFlag(i)); - } else { - EXPECT_FALSE(PullSysFlag::hasCommitOffsetFlag(i)); - } - - if ((i & FLAG_SUSPEND) == FLAG_SUSPEND) { - EXPECT_TRUE(PullSysFlag::hasSuspendFlag(i)); - } else { - EXPECT_FALSE(PullSysFlag::hasSuspendFlag(i)); - } - - if ((i & FLAG_SUBSCRIPTION) == FLAG_SUBSCRIPTION) { - EXPECT_TRUE(PullSysFlag::hasSubscriptionFlag(i)); - } else { - EXPECT_FALSE(PullSysFlag::hasSubscriptionFlag(i)); - } - - if ((i & FLAG_CLASS_FILTER) == FLAG_CLASS_FILTER) { - EXPECT_TRUE(PullSysFlag::hasClassFilterFlag(i)); - } else { - EXPECT_FALSE(PullSysFlag::hasClassFilterFlag(i)); - } - - if ((i & FLAG_COMMIT_OFFSET) == FLAG_COMMIT_OFFSET) { - EXPECT_TRUE(PullSysFlag::hasCommitOffsetFlag(i)); - } else { - EXPECT_FALSE(PullSysFlag::hasCommitOffsetFlag(i)); - } - - if (i == 0 || i == 1) { - EXPECT_EQ(PullSysFlag::clearCommitOffsetFlag(i), 0); - } else { - EXPECT_TRUE(PullSysFlag::clearCommitOffsetFlag(i) > 0); - } + EXPECT_EQ(PullSysFlag::buildSysFlag(false, false, false, false), 0); + + EXPECT_EQ(PullSysFlag::buildSysFlag(true, false, false, false), 1); + EXPECT_EQ(PullSysFlag::buildSysFlag(true, true, false, false), 3); + EXPECT_EQ(PullSysFlag::buildSysFlag(true, true, true, false), 7); + EXPECT_EQ(PullSysFlag::buildSysFlag(true, true, true, true), 15); + + EXPECT_EQ(PullSysFlag::buildSysFlag(false, true, false, false), 2); + EXPECT_EQ(PullSysFlag::buildSysFlag(false, true, true, false), 6); + EXPECT_EQ(PullSysFlag::buildSysFlag(false, true, true, true), 14); + + EXPECT_EQ(PullSysFlag::buildSysFlag(false, false, true, false), 4); + EXPECT_EQ(PullSysFlag::buildSysFlag(false, false, true, true), 12); + + EXPECT_EQ(PullSysFlag::buildSysFlag(false, false, false, true), 8); + + int FLAG_COMMIT_OFFSET = 0x1 << 0; + int FLAG_SUSPEND = 0x1 << 1; + int FLAG_SUBSCRIPTION = 0x1 << 2; + int FLAG_CLASS_FILTER = 0x1 << 3; + + for (int i = 0; i < 16; i++) { + if ((i & FLAG_COMMIT_OFFSET) == FLAG_COMMIT_OFFSET) { + EXPECT_TRUE(PullSysFlag::hasCommitOffsetFlag(i)); + } else { + EXPECT_FALSE(PullSysFlag::hasCommitOffsetFlag(i)); + } + + if ((i & FLAG_SUSPEND) == FLAG_SUSPEND) { + EXPECT_TRUE(PullSysFlag::hasSuspendFlag(i)); + } else { + EXPECT_FALSE(PullSysFlag::hasSuspendFlag(i)); + } + + if ((i & FLAG_SUBSCRIPTION) == FLAG_SUBSCRIPTION) { + EXPECT_TRUE(PullSysFlag::hasSubscriptionFlag(i)); + } else { + EXPECT_FALSE(PullSysFlag::hasSubscriptionFlag(i)); + } + + if ((i & FLAG_CLASS_FILTER) == FLAG_CLASS_FILTER) { + EXPECT_TRUE(PullSysFlag::hasClassFilterFlag(i)); + } else { + EXPECT_FALSE(PullSysFlag::hasClassFilterFlag(i)); + } + + if ((i & FLAG_COMMIT_OFFSET) == FLAG_COMMIT_OFFSET) { + EXPECT_TRUE(PullSysFlag::hasCommitOffsetFlag(i)); + } else { + EXPECT_FALSE(PullSysFlag::hasCommitOffsetFlag(i)); + } + + if (i == 0 || i == 1) { + EXPECT_EQ(PullSysFlag::clearCommitOffsetFlag(i), 0); + } else { + EXPECT_TRUE(PullSysFlag::clearCommitOffsetFlag(i) > 0); } + } } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "pullSysFlag.flag"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "pullSysFlag.flag"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/common/TopicConfigTest.cpp b/test/src/common/TopicConfigTest.cpp index d435ba6db..8c163942c 100644 --- a/test/src/common/TopicConfigTest.cpp +++ b/test/src/common/TopicConfigTest.cpp @@ -34,60 +34,60 @@ using rocketmq::TopicConfig; using rocketmq::TopicFilterType; TEST(topicConfig, encodeAndDecode) { - TopicConfig topicConfig("testTopic", 4, 4, PermName::PERM_READ); - string str = topicConfig.encode(); + TopicConfig topicConfig("testTopic", 4, 4, PermName::PERM_READ); + string str = topicConfig.encode(); - TopicConfig topicDecodeConfig; - topicDecodeConfig.decode(str); - EXPECT_EQ(str, topicDecodeConfig.encode()); + TopicConfig topicDecodeConfig; + topicDecodeConfig.decode(str); + EXPECT_EQ(str, topicDecodeConfig.encode()); } TEST(topicConfig, info) { - TopicConfig topicConfig; + TopicConfig topicConfig; - topicConfig.setTopicName("testTopic"); - EXPECT_EQ(topicConfig.getTopicName(), "testTopic"); + topicConfig.setTopicName("testTopic"); + EXPECT_EQ(topicConfig.getTopicName(), "testTopic"); - topicConfig.setReadQueueNums(4); - EXPECT_EQ(topicConfig.getReadQueueNums(), 4); + topicConfig.setReadQueueNums(4); + EXPECT_EQ(topicConfig.getReadQueueNums(), 4); - topicConfig.setWriteQueueNums(4); - EXPECT_EQ(topicConfig.getWriteQueueNums(), 4); + topicConfig.setWriteQueueNums(4); + EXPECT_EQ(topicConfig.getWriteQueueNums(), 4); - topicConfig.setPerm(PermName::PERM_READ); - EXPECT_EQ(topicConfig.getPerm(), PermName::PERM_READ); + topicConfig.setPerm(PermName::PERM_READ); + EXPECT_EQ(topicConfig.getPerm(), PermName::PERM_READ); - topicConfig.setTopicFilterType(TopicFilterType::MULTI_TAG); - EXPECT_EQ(topicConfig.getTopicFilterType(), TopicFilterType::MULTI_TAG); + topicConfig.setTopicFilterType(TopicFilterType::MULTI_TAG); + EXPECT_EQ(topicConfig.getTopicFilterType(), TopicFilterType::MULTI_TAG); } TEST(topicConfig, init) { - TopicConfig topicConfig; - EXPECT_TRUE(topicConfig.getTopicName() == ""); - EXPECT_EQ(topicConfig.getReadQueueNums(), TopicConfig::DefaultReadQueueNums); - EXPECT_EQ(topicConfig.getWriteQueueNums(), TopicConfig::DefaultWriteQueueNums); - EXPECT_EQ(topicConfig.getPerm(), PermName::PERM_READ | PermName::PERM_WRITE); - EXPECT_EQ(topicConfig.getTopicFilterType(), TopicFilterType::SINGLE_TAG); - - TopicConfig twoTopicConfig("testTopic"); - EXPECT_EQ(twoTopicConfig.getTopicName(), "testTopic"); - EXPECT_EQ(twoTopicConfig.getReadQueueNums(), TopicConfig::DefaultReadQueueNums); - EXPECT_EQ(twoTopicConfig.getWriteQueueNums(), TopicConfig::DefaultWriteQueueNums); - EXPECT_EQ(twoTopicConfig.getPerm(), PermName::PERM_READ | PermName::PERM_WRITE); - EXPECT_EQ(twoTopicConfig.getTopicFilterType(), TopicFilterType::SINGLE_TAG); - - TopicConfig threeTopicConfig("testTopic", 4, 4, PermName::PERM_READ); - EXPECT_EQ(threeTopicConfig.getTopicName(), "testTopic"); - EXPECT_EQ(threeTopicConfig.getReadQueueNums(), 4); - EXPECT_EQ(threeTopicConfig.getWriteQueueNums(), 4); - EXPECT_EQ(threeTopicConfig.getPerm(), PermName::PERM_READ); - EXPECT_EQ(threeTopicConfig.getTopicFilterType(), TopicFilterType::SINGLE_TAG); + TopicConfig topicConfig; + EXPECT_TRUE(topicConfig.getTopicName() == ""); + EXPECT_EQ(topicConfig.getReadQueueNums(), TopicConfig::DefaultReadQueueNums); + EXPECT_EQ(topicConfig.getWriteQueueNums(), TopicConfig::DefaultWriteQueueNums); + EXPECT_EQ(topicConfig.getPerm(), PermName::PERM_READ | PermName::PERM_WRITE); + EXPECT_EQ(topicConfig.getTopicFilterType(), TopicFilterType::SINGLE_TAG); + + TopicConfig twoTopicConfig("testTopic"); + EXPECT_EQ(twoTopicConfig.getTopicName(), "testTopic"); + EXPECT_EQ(twoTopicConfig.getReadQueueNums(), TopicConfig::DefaultReadQueueNums); + EXPECT_EQ(twoTopicConfig.getWriteQueueNums(), TopicConfig::DefaultWriteQueueNums); + EXPECT_EQ(twoTopicConfig.getPerm(), PermName::PERM_READ | PermName::PERM_WRITE); + EXPECT_EQ(twoTopicConfig.getTopicFilterType(), TopicFilterType::SINGLE_TAG); + + TopicConfig threeTopicConfig("testTopic", 4, 4, PermName::PERM_READ); + EXPECT_EQ(threeTopicConfig.getTopicName(), "testTopic"); + EXPECT_EQ(threeTopicConfig.getReadQueueNums(), 4); + EXPECT_EQ(threeTopicConfig.getWriteQueueNums(), 4); + EXPECT_EQ(threeTopicConfig.getPerm(), PermName::PERM_READ); + EXPECT_EQ(threeTopicConfig.getTopicFilterType(), TopicFilterType::SINGLE_TAG); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "topicConfig.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "topicConfig.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/common/ValidatorsTest.cpp b/test/src/common/ValidatorsTest.cpp index 807ab385a..23594cdd3 100644 --- a/test/src/common/ValidatorsTest.cpp +++ b/test/src/common/ValidatorsTest.cpp @@ -34,49 +34,49 @@ using rocketmq::MQMessage; using rocketmq::Validators; TEST(validators, regularExpressionMatcher) { - EXPECT_FALSE(Validators::regularExpressionMatcher(string(), string())); + EXPECT_FALSE(Validators::regularExpressionMatcher(string(), string())); - EXPECT_TRUE(Validators::regularExpressionMatcher(string("123456"), string())); + EXPECT_TRUE(Validators::regularExpressionMatcher(string("123456"), string())); - EXPECT_TRUE(Validators::regularExpressionMatcher(string("123456"), string("123"))); + EXPECT_TRUE(Validators::regularExpressionMatcher(string("123456"), string("123"))); } TEST(validators, getGroupWithRegularExpression) { - EXPECT_EQ(Validators::getGroupWithRegularExpression(string(), string()), ""); + EXPECT_EQ(Validators::getGroupWithRegularExpression(string(), string()), ""); } TEST(validators, checkTopic) { - EXPECT_THROW(Validators::checkTopic(string()), MQClientException); - string exceptionTopic = "1234567890"; - for (int i = 0; i < 25; i++) { - exceptionTopic.append("1234567890"); - } - EXPECT_THROW(Validators::checkTopic(exceptionTopic), MQClientException); - - EXPECT_THROW(Validators::checkTopic("TBW102"), MQClientException); + EXPECT_THROW(Validators::checkTopic(string()), MQClientException); + string exceptionTopic = "1234567890"; + for (int i = 0; i < 25; i++) { + exceptionTopic.append("1234567890"); + } + EXPECT_THROW(Validators::checkTopic(exceptionTopic), MQClientException); + + EXPECT_THROW(Validators::checkTopic("TBW102"), MQClientException); } TEST(validators, checkGroup) { - EXPECT_THROW(Validators::checkGroup(string()), MQClientException); - string exceptionTopic = "1234567890"; - for (int i = 0; i < 25; i++) { - exceptionTopic.append("1234567890"); - } - EXPECT_THROW(Validators::checkGroup(exceptionTopic), MQClientException); + EXPECT_THROW(Validators::checkGroup(string()), MQClientException); + string exceptionTopic = "1234567890"; + for (int i = 0; i < 25; i++) { + exceptionTopic.append("1234567890"); + } + EXPECT_THROW(Validators::checkGroup(exceptionTopic), MQClientException); } TEST(validators, checkMessage) { - MQMessage message("testTopic", string()); + MQMessage message("testTopic", string()); - EXPECT_THROW(Validators::checkMessage(MQMessage("testTopic", string()), 1), MQClientException); + EXPECT_THROW(Validators::checkMessage(MQMessage("testTopic", string()), 1), MQClientException); - EXPECT_THROW(Validators::checkMessage(MQMessage("testTopic", string("123")), 2), MQClientException); + EXPECT_THROW(Validators::checkMessage(MQMessage("testTopic", string("123")), 2), MQClientException); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "validators.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "validators.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/common/VirtualEnvUtilTest.cpp b/test/src/common/VirtualEnvUtilTest.cpp index 305b0cd92..f02889cf7 100644 --- a/test/src/common/VirtualEnvUtilTest.cpp +++ b/test/src/common/VirtualEnvUtilTest.cpp @@ -32,20 +32,20 @@ using rocketmq::VirtualEnvUtil; VirtualEnvUtil virtualEnvUtil; TEST(virtualEnvUtil, buildWithProjectGroup) { - string origin = "origin"; - string projectGroup; - EXPECT_EQ(virtualEnvUtil.buildWithProjectGroup(origin, string()), origin); + string origin = "origin"; + string projectGroup; + EXPECT_EQ(virtualEnvUtil.buildWithProjectGroup(origin, string()), origin); - EXPECT_EQ(virtualEnvUtil.buildWithProjectGroup(origin, string("123")), origin); + EXPECT_EQ(virtualEnvUtil.buildWithProjectGroup(origin, string("123")), origin); } TEST(virtualEnvUtil, clearProjectGroup) {} -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "messageExt.init"; - int itestts = RUN_ALL_TESTS(); - return itestts; + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "messageExt.init"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/common/big_endianTest.cpp b/test/src/common/big_endianTest.cpp index d4c6fe3b5..5469dedb1 100644 --- a/test/src/common/big_endianTest.cpp +++ b/test/src/common/big_endianTest.cpp @@ -29,72 +29,72 @@ using rocketmq::BigEndianReader; using rocketmq::BigEndianWriter; TEST(big_endian, bigEndianObject) { - 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)); - 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)); - EXPECT_TRUE(reader.ReadU16(unit16)); - EXPECT_EQ(*unit16, 1200); - free(unit16); - - uint32_t *unit32 = (uint32_t *) malloc(sizeof(uint32_t)); - - 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)); - - 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); - strcpy(writeBuf, "RocketMQ"); - EXPECT_TRUE(writer.WriteBytes(writeBuf, (size_t) 8)); - EXPECT_TRUE(reader.ReadBytes(newBuf, (size_t) 8)); - EXPECT_EQ(*writeBuf, *newBuf); - - free(newBuf); - free(writeBuf); + 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)); + 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)); + EXPECT_TRUE(reader.ReadU16(unit16)); + EXPECT_EQ(*unit16, 1200); + free(unit16); + + uint32_t* unit32 = (uint32_t*)malloc(sizeof(uint32_t)); + + 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)); + + 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); + strcpy(writeBuf, "RocketMQ"); + EXPECT_TRUE(writer.WriteBytes(writeBuf, (size_t)8)); + EXPECT_TRUE(reader.ReadBytes(newBuf, (size_t)8)); + EXPECT_EQ(*writeBuf, *newBuf); + + free(newBuf); + free(writeBuf); } TEST(big_endian, bigEndian) { - char writeBuf[8]; - - /*TODO - char *newBuf = (char *) malloc(sizeof(char) * 8); - strcpy(newBuf, "RocketMQ"); - - char readBuf[8]; - rocketmq::WriteBigEndian(writeBuf, newBuf); - rocketmq::ReadBigEndian(writeBuf, readBuf); - EXPECT_EQ(writeBuf, readBuf); - */ - - 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); + char writeBuf[8]; + + /*TODO + char *newBuf = (char *) malloc(sizeof(char) * 8); + strcpy(newBuf, "RocketMQ"); + + char readBuf[8]; + rocketmq::WriteBigEndian(writeBuf, newBuf); + rocketmq::ReadBigEndian(writeBuf, readBuf); + EXPECT_EQ(writeBuf, readBuf); + */ + + 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); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "big_endian.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "big_endian.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/extern/CMessageExtTest.cpp b/test/src/extern/CMessageExtTest.cpp index 3471c9e32..8752d5ce4 100644 --- a/test/src/extern/CMessageExtTest.cpp +++ b/test/src/extern/CMessageExtTest.cpp @@ -29,78 +29,78 @@ using testing::Return; using rocketmq::MQMessageExt; TEST(cmessageExt, info) { - MQMessageExt *mqMessageExt = new MQMessageExt(); - CMessageExt *messageExt = (CMessageExt *) mqMessageExt; + MQMessageExt* mqMessageExt = new MQMessageExt(); + CMessageExt* messageExt = (CMessageExt*)mqMessageExt; - mqMessageExt->setTopic("testTopic"); - EXPECT_EQ(GetMessageTopic(messageExt), mqMessageExt->getTopic()); + mqMessageExt->setTopic("testTopic"); + EXPECT_EQ(GetMessageTopic(messageExt), mqMessageExt->getTopic()); - mqMessageExt->setTags("testTags"); - EXPECT_EQ(GetMessageTags(messageExt), mqMessageExt->getTags()); + mqMessageExt->setTags("testTags"); + EXPECT_EQ(GetMessageTags(messageExt), mqMessageExt->getTags()); - mqMessageExt->setKeys("testKeys"); - EXPECT_EQ(GetMessageKeys(messageExt), mqMessageExt->getKeys()); + mqMessageExt->setKeys("testKeys"); + EXPECT_EQ(GetMessageKeys(messageExt), mqMessageExt->getKeys()); - mqMessageExt->setBody("testBody"); - EXPECT_EQ(GetMessageBody(messageExt), mqMessageExt->getBody()); + mqMessageExt->setBody("testBody"); + EXPECT_EQ(GetMessageBody(messageExt), mqMessageExt->getBody()); - mqMessageExt->setProperty("testKey", "testValues"); - EXPECT_EQ(GetMessageProperty(messageExt, "testKey"), mqMessageExt->getProperty("testKey")); + mqMessageExt->setProperty("testKey", "testValues"); + EXPECT_EQ(GetMessageProperty(messageExt, "testKey"), mqMessageExt->getProperty("testKey")); - mqMessageExt->setMsgId("msgId123456"); - EXPECT_EQ(GetMessageId(messageExt), mqMessageExt->getMsgId()); + mqMessageExt->setMsgId("msgId123456"); + EXPECT_EQ(GetMessageId(messageExt), mqMessageExt->getMsgId()); - mqMessageExt->setDelayTimeLevel(1); - EXPECT_EQ(GetMessageDelayTimeLevel(messageExt), mqMessageExt->getDelayTimeLevel()); + mqMessageExt->setDelayTimeLevel(1); + EXPECT_EQ(GetMessageDelayTimeLevel(messageExt), mqMessageExt->getDelayTimeLevel()); - mqMessageExt->setQueueId(4); - EXPECT_EQ(GetMessageQueueId(messageExt), mqMessageExt->getQueueId()); + mqMessageExt->setQueueId(4); + EXPECT_EQ(GetMessageQueueId(messageExt), mqMessageExt->getQueueId()); - mqMessageExt->setReconsumeTimes(1234567); - EXPECT_EQ(GetMessageReconsumeTimes(messageExt), mqMessageExt->getReconsumeTimes()); + mqMessageExt->setReconsumeTimes(1234567); + EXPECT_EQ(GetMessageReconsumeTimes(messageExt), mqMessageExt->getReconsumeTimes()); - mqMessageExt->setStoreSize(127); - EXPECT_EQ(GetMessageStoreSize(messageExt), mqMessageExt->getStoreSize()); + mqMessageExt->setStoreSize(127); + EXPECT_EQ(GetMessageStoreSize(messageExt), mqMessageExt->getStoreSize()); - mqMessageExt->setBornTimestamp(9876543); - EXPECT_EQ(GetMessageBornTimestamp(messageExt), mqMessageExt->getBornTimestamp()); + mqMessageExt->setBornTimestamp(9876543); + EXPECT_EQ(GetMessageBornTimestamp(messageExt), mqMessageExt->getBornTimestamp()); - mqMessageExt->setStoreTimestamp(123123); - EXPECT_EQ(GetMessageStoreTimestamp(messageExt), mqMessageExt->getStoreTimestamp()); + mqMessageExt->setStoreTimestamp(123123); + EXPECT_EQ(GetMessageStoreTimestamp(messageExt), mqMessageExt->getStoreTimestamp()); - mqMessageExt->setQueueOffset(1024); - EXPECT_EQ(GetMessageQueueOffset(messageExt), mqMessageExt->getQueueOffset()); + mqMessageExt->setQueueOffset(1024); + EXPECT_EQ(GetMessageQueueOffset(messageExt), mqMessageExt->getQueueOffset()); - mqMessageExt->setCommitLogOffset(2048); - EXPECT_EQ(GetMessageCommitLogOffset(messageExt), mqMessageExt->getCommitLogOffset()); + mqMessageExt->setCommitLogOffset(2048); + EXPECT_EQ(GetMessageCommitLogOffset(messageExt), mqMessageExt->getCommitLogOffset()); - mqMessageExt->setPreparedTransactionOffset(4096); - EXPECT_EQ(GetMessagePreparedTransactionOffset(messageExt), mqMessageExt->getPreparedTransactionOffset()); + mqMessageExt->setPreparedTransactionOffset(4096); + EXPECT_EQ(GetMessagePreparedTransactionOffset(messageExt), mqMessageExt->getPreparedTransactionOffset()); - delete mqMessageExt; + delete mqMessageExt; } TEST(cmessageExt, null) { - EXPECT_TRUE(GetMessageTopic(NULL) == NULL); - EXPECT_TRUE(GetMessageTags(NULL) == NULL); - EXPECT_TRUE(GetMessageKeys(NULL) == NULL); - EXPECT_TRUE(GetMessageBody(NULL) == NULL); - EXPECT_TRUE(GetMessageProperty(NULL, NULL) == NULL); - EXPECT_TRUE(GetMessageId(NULL) == NULL); - EXPECT_EQ(GetMessageDelayTimeLevel(NULL), NULL_POINTER); - EXPECT_EQ(GetMessageQueueId(NULL), NULL_POINTER); - EXPECT_EQ(GetMessageReconsumeTimes(NULL), NULL_POINTER); - EXPECT_EQ(GetMessageStoreSize(NULL), NULL_POINTER); - EXPECT_EQ(GetMessageBornTimestamp(NULL), NULL_POINTER); - EXPECT_EQ(GetMessageStoreTimestamp(NULL), NULL_POINTER); - EXPECT_EQ(GetMessageQueueOffset(NULL), NULL_POINTER); - EXPECT_EQ(GetMessageCommitLogOffset(NULL), NULL_POINTER); - EXPECT_EQ(GetMessagePreparedTransactionOffset(NULL), NULL_POINTER); + EXPECT_TRUE(GetMessageTopic(NULL) == NULL); + EXPECT_TRUE(GetMessageTags(NULL) == NULL); + EXPECT_TRUE(GetMessageKeys(NULL) == NULL); + EXPECT_TRUE(GetMessageBody(NULL) == NULL); + EXPECT_TRUE(GetMessageProperty(NULL, NULL) == NULL); + EXPECT_TRUE(GetMessageId(NULL) == NULL); + EXPECT_EQ(GetMessageDelayTimeLevel(NULL), NULL_POINTER); + EXPECT_EQ(GetMessageQueueId(NULL), NULL_POINTER); + EXPECT_EQ(GetMessageReconsumeTimes(NULL), NULL_POINTER); + EXPECT_EQ(GetMessageStoreSize(NULL), NULL_POINTER); + EXPECT_EQ(GetMessageBornTimestamp(NULL), NULL_POINTER); + EXPECT_EQ(GetMessageStoreTimestamp(NULL), NULL_POINTER); + EXPECT_EQ(GetMessageQueueOffset(NULL), NULL_POINTER); + EXPECT_EQ(GetMessageCommitLogOffset(NULL), NULL_POINTER); + EXPECT_EQ(GetMessagePreparedTransactionOffset(NULL), NULL_POINTER); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(filter) = "cmessageExt.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(filter) = "cmessageExt.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/extern/CMessageTest.cpp b/test/src/extern/CMessageTest.cpp index 22500ebaa..f6da8fa84 100644 --- a/test/src/extern/CMessageTest.cpp +++ b/test/src/extern/CMessageTest.cpp @@ -28,54 +28,54 @@ using testing::Return; using rocketmq::MQMessage; TEST(cmessages, info) { - CMessage *message = CreateMessage(NULL); - MQMessage *mqMessage = (MQMessage *) message; - EXPECT_EQ(mqMessage->getTopic(), ""); + CMessage* message = CreateMessage(NULL); + MQMessage* mqMessage = (MQMessage*)message; + EXPECT_EQ(mqMessage->getTopic(), ""); - SetMessageTopic(message, "testTopic"); - EXPECT_EQ(mqMessage->getTopic(), "testTopic"); + SetMessageTopic(message, "testTopic"); + EXPECT_EQ(mqMessage->getTopic(), "testTopic"); - SetMessageTags(message, "testTags"); - EXPECT_EQ(mqMessage->getTags(), "testTags"); + SetMessageTags(message, "testTags"); + EXPECT_EQ(mqMessage->getTags(), "testTags"); - SetMessageKeys(message, "testKeys"); - EXPECT_EQ(mqMessage->getKeys(), "testKeys"); + SetMessageKeys(message, "testKeys"); + EXPECT_EQ(mqMessage->getKeys(), "testKeys"); - SetMessageBody(message, "testBody"); - EXPECT_EQ(mqMessage->getBody(), "testBody"); + SetMessageBody(message, "testBody"); + EXPECT_EQ(mqMessage->getBody(), "testBody"); - SetByteMessageBody(message, "testBody", 5); - EXPECT_EQ(mqMessage->getBody(), "testB"); + SetByteMessageBody(message, "testBody", 5); + EXPECT_EQ(mqMessage->getBody(), "testB"); - SetMessageProperty(message, "testKey", "testValue"); - EXPECT_EQ(mqMessage->getProperty("testKey"), "testValue"); + SetMessageProperty(message, "testKey", "testValue"); + EXPECT_EQ(mqMessage->getProperty("testKey"), "testValue"); - SetDelayTimeLevel(message, 1); - EXPECT_EQ(mqMessage->getDelayTimeLevel(), 1); + SetDelayTimeLevel(message, 1); + EXPECT_EQ(mqMessage->getDelayTimeLevel(), 1); - EXPECT_EQ(DestroyMessage(message), OK); + EXPECT_EQ(DestroyMessage(message), OK); - CMessage *twomessage = CreateMessage("testTwoTopic"); - MQMessage *twoMqMessage = (MQMessage *) twomessage; - EXPECT_EQ(twoMqMessage->getTopic(), "testTwoTopic"); + CMessage* twomessage = CreateMessage("testTwoTopic"); + MQMessage* twoMqMessage = (MQMessage*)twomessage; + EXPECT_EQ(twoMqMessage->getTopic(), "testTwoTopic"); - EXPECT_EQ(DestroyMessage(twomessage), OK); + EXPECT_EQ(DestroyMessage(twomessage), OK); } TEST(cmessages, null) { - EXPECT_EQ(SetMessageTopic(NULL, NULL), NULL_POINTER); - EXPECT_EQ(SetMessageTags(NULL, NULL), NULL_POINTER); - EXPECT_EQ(SetMessageKeys(NULL, NULL), NULL_POINTER); - EXPECT_EQ(SetMessageBody(NULL, NULL), NULL_POINTER); - EXPECT_EQ(SetByteMessageBody(NULL, NULL, 0), NULL_POINTER); - EXPECT_EQ(SetMessageProperty(NULL, NULL, NULL), NULL_POINTER); - EXPECT_EQ(SetDelayTimeLevel(NULL, 0), NULL_POINTER); + EXPECT_EQ(SetMessageTopic(NULL, NULL), NULL_POINTER); + EXPECT_EQ(SetMessageTags(NULL, NULL), NULL_POINTER); + EXPECT_EQ(SetMessageKeys(NULL, NULL), NULL_POINTER); + EXPECT_EQ(SetMessageBody(NULL, NULL), NULL_POINTER); + EXPECT_EQ(SetByteMessageBody(NULL, NULL, 0), NULL_POINTER); + EXPECT_EQ(SetMessageProperty(NULL, NULL, NULL), NULL_POINTER); + EXPECT_EQ(SetDelayTimeLevel(NULL, 0), NULL_POINTER); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(filter) = "cmessages.null"; - int itestts = RUN_ALL_TESTS(); - return itestts; + testing::GTEST_FLAG(filter) = "cmessages.null"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/extern/CProducerTest.cpp b/test/src/extern/CProducerTest.cpp index 2d5c5bec7..56b1cca1e 100644 --- a/test/src/extern/CProducerTest.cpp +++ b/test/src/extern/CProducerTest.cpp @@ -50,191 +50,189 @@ using rocketmq::SendStatus; using rocketmq::SessionCredentials; class MockDefaultMQProducer : public DefaultMQProducer { - public: - MockDefaultMQProducer(const string &groupname) : DefaultMQProducer(groupname) {} - MOCK_METHOD0(start, void()); - MOCK_METHOD0(shutdown, void()); - MOCK_METHOD2(setLogFileSizeAndNum, void(int, long)); - MOCK_METHOD1(SetProducerLogLevel, void(elogLevel)); - MOCK_METHOD2(send, SendResult(MQMessage &, bool)); - MOCK_METHOD3(send, void(MQMessage &, SendCallback *, bool)); - MOCK_METHOD2(sendOneway, void(MQMessage &, bool)); - MOCK_METHOD5(send, SendResult(MQMessage &, MessageQueueSelector *, void *, int, bool)); + public: + MockDefaultMQProducer(const string& groupname) : DefaultMQProducer(groupname) {} + MOCK_METHOD0(start, void()); + MOCK_METHOD0(shutdown, void()); + MOCK_METHOD2(setLogFileSizeAndNum, void(int, long)); + MOCK_METHOD1(SetProducerLogLevel, void(elogLevel)); + MOCK_METHOD2(send, SendResult(MQMessage&, bool)); + MOCK_METHOD3(send, void(MQMessage&, SendCallback*, bool)); + MOCK_METHOD2(sendOneway, void(MQMessage&, bool)); + 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(); - - EXPECT_EQ(SendMessageAsync(NULL, NULL, NULL, NULL), NULL_POINTER); - EXPECT_EQ(SendMessageAsync(cProducer, NULL, NULL, NULL), NULL_POINTER); - EXPECT_EQ(SendMessageAsync(cProducer, msg, CSendSuccessCallbackFunc, NULL), NULL_POINTER); - - EXPECT_CALL(*mockProducer, send(_, _)).Times(1); - EXPECT_EQ(SendMessageAsync(cProducer, msg, CSendSuccessCallbackFunc, cSendExceptionCallbackFunc), OK); - Mock::AllowLeak(mockProducer); - DestroyMessage(msg); + MockDefaultMQProducer* mockProducer = new MockDefaultMQProducer("testGroup"); + 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); + EXPECT_EQ(SendMessageAsync(cProducer, msg, CSendSuccessCallbackFunc, NULL), NULL_POINTER); + + EXPECT_CALL(*mockProducer, send(_, _)).Times(1); + EXPECT_EQ(SendMessageAsync(cProducer, msg, CSendSuccessCallbackFunc, cSendExceptionCallbackFunc), OK); + Mock::AllowLeak(mockProducer); + 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(); - MQMessageQueue messageQueue; - - EXPECT_EQ(SendMessageOrderly(NULL, NULL, NULL, msg, 1, NULL), NULL_POINTER); - EXPECT_EQ(SendMessageOrderly(cProducer, NULL, NULL, msg, 1, NULL), NULL_POINTER); - EXPECT_EQ(SendMessageOrderly(cProducer, msg, NULL, msg, 1, NULL), NULL_POINTER); - EXPECT_EQ(SendMessageOrderly(cProducer, msg, QueueSelectorCallbackFunc, NULL, 1, NULL), NULL_POINTER); - EXPECT_EQ(SendMessageOrderly(cProducer, msg, QueueSelectorCallbackFunc, msg, 1, NULL), NULL_POINTER); - - EXPECT_CALL(*mockProducer, send(_, _, _, _, _)) - .WillOnce(Return(SendResult(SendStatus::SEND_OK, "3", "offset1", messageQueue, 14))); - // EXPECT_EQ(SendMessageOrderly(cProducer, msg, callback, msg, 1, result), OK); - Mock::AllowLeak(mockProducer); - DestroyMessage(msg); - // free(result); + MockDefaultMQProducer* mockProducer = new MockDefaultMQProducer("testGroup"); + CProducer* cProducer = (CProducer*)mockProducer; + CMessage* msg = (CMessage*)new MQMessage(); + MQMessageQueue messageQueue; + + EXPECT_EQ(SendMessageOrderly(NULL, NULL, NULL, msg, 1, NULL), NULL_POINTER); + EXPECT_EQ(SendMessageOrderly(cProducer, NULL, NULL, msg, 1, NULL), NULL_POINTER); + EXPECT_EQ(SendMessageOrderly(cProducer, msg, NULL, msg, 1, NULL), NULL_POINTER); + EXPECT_EQ(SendMessageOrderly(cProducer, msg, QueueSelectorCallbackFunc, NULL, 1, NULL), NULL_POINTER); + EXPECT_EQ(SendMessageOrderly(cProducer, msg, QueueSelectorCallbackFunc, msg, 1, NULL), NULL_POINTER); + + EXPECT_CALL(*mockProducer, send(_, _, _, _, _)) + .WillOnce(Return(SendResult(SendStatus::SEND_OK, "3", "offset1", messageQueue, 14))); + // EXPECT_EQ(SendMessageOrderly(cProducer, msg, callback, msg, 1, result), OK); + Mock::AllowLeak(mockProducer); + DestroyMessage(msg); + // free(result); } TEST(cProducer, sendOneway) { - MockDefaultMQProducer *mockProducer = new MockDefaultMQProducer("testGroup"); - CProducer *cProducer = (CProducer *) mockProducer; - CMessage *msg = (CMessage *) new MQMessage(); + MockDefaultMQProducer* mockProducer = new MockDefaultMQProducer("testGroup"); + CProducer* cProducer = (CProducer*)mockProducer; + CMessage* msg = (CMessage*)new MQMessage(); - EXPECT_EQ(SendMessageOneway(NULL, NULL), NULL_POINTER); - EXPECT_EQ(SendMessageOneway(cProducer, NULL), NULL_POINTER); + EXPECT_EQ(SendMessageOneway(NULL, NULL), NULL_POINTER); + EXPECT_EQ(SendMessageOneway(cProducer, NULL), NULL_POINTER); - EXPECT_CALL(*mockProducer, sendOneway(_, _)).Times(1); - EXPECT_EQ(SendMessageOneway(cProducer, msg), OK); - Mock::AllowLeak(mockProducer); - DestroyMessage(msg); + EXPECT_CALL(*mockProducer, sendOneway(_, _)).Times(1); + EXPECT_EQ(SendMessageOneway(cProducer, msg), OK); + Mock::AllowLeak(mockProducer); + DestroyMessage(msg); } TEST(cProducer, sendMessageSync) { - MockDefaultMQProducer *mockProducer = new MockDefaultMQProducer("testGroup"); - CProducer *cProducer = (CProducer *) mockProducer; + MockDefaultMQProducer* mockProducer = new MockDefaultMQProducer("testGroup"); + CProducer* cProducer = (CProducer*)mockProducer; - MQMessage *mqMessage = new MQMessage(); - CMessage *msg = (CMessage *) mqMessage; - CSendResult *result; - MQMessageQueue messageQueue; - EXPECT_EQ(SendMessageSync(NULL, NULL, NULL), NULL_POINTER); - EXPECT_EQ(SendMessageSync(cProducer, NULL, NULL), NULL_POINTER); + MQMessage* mqMessage = new MQMessage(); + CMessage* msg = (CMessage*)mqMessage; + CSendResult* result; + MQMessageQueue messageQueue; + EXPECT_EQ(SendMessageSync(NULL, NULL, NULL), NULL_POINTER); + EXPECT_EQ(SendMessageSync(cProducer, NULL, NULL), NULL_POINTER); - EXPECT_EQ(SendMessageSync(cProducer, msg, NULL), NULL_POINTER); + EXPECT_EQ(SendMessageSync(cProducer, msg, NULL), NULL_POINTER); - result = (CSendResult *) malloc(sizeof(CSendResult)); + result = (CSendResult*)malloc(sizeof(CSendResult)); - EXPECT_CALL(*mockProducer, send(_, _)) - .Times(5) - .WillOnce(Return(SendResult(SendStatus::SEND_FLUSH_DISK_TIMEOUT, "1", "offset1", messageQueue, 14))) - .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))); + EXPECT_CALL(*mockProducer, send(_, _)) + .Times(5) + .WillOnce(Return(SendResult(SendStatus::SEND_FLUSH_DISK_TIMEOUT, "1", "offset1", messageQueue, 14))) + .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))); - EXPECT_EQ(SendMessageSync(cProducer, msg, result), OK); - EXPECT_EQ(result->sendStatus, E_SEND_FLUSH_DISK_TIMEOUT); + EXPECT_EQ(SendMessageSync(cProducer, msg, result), OK); + EXPECT_EQ(result->sendStatus, E_SEND_FLUSH_DISK_TIMEOUT); - EXPECT_EQ(SendMessageSync(cProducer, msg, result), OK); - EXPECT_EQ(result->sendStatus, E_SEND_FLUSH_SLAVE_TIMEOUT); + EXPECT_EQ(SendMessageSync(cProducer, msg, result), OK); + EXPECT_EQ(result->sendStatus, E_SEND_FLUSH_SLAVE_TIMEOUT); - EXPECT_EQ(SendMessageSync(cProducer, msg, result), OK); - EXPECT_EQ(result->sendStatus, E_SEND_SLAVE_NOT_AVAILABLE); + EXPECT_EQ(SendMessageSync(cProducer, msg, result), OK); + EXPECT_EQ(result->sendStatus, E_SEND_SLAVE_NOT_AVAILABLE); - EXPECT_EQ(SendMessageSync(cProducer, msg, result), OK); - EXPECT_EQ(result->sendStatus, E_SEND_OK); + EXPECT_EQ(SendMessageSync(cProducer, msg, result), OK); + EXPECT_EQ(result->sendStatus, E_SEND_OK); - EXPECT_EQ(SendMessageSync(cProducer, msg, result), OK); - EXPECT_EQ(result->sendStatus, E_SEND_OK); - Mock::AllowLeak(mockProducer); - DestroyMessage(msg); - free(result); + EXPECT_EQ(SendMessageSync(cProducer, msg, result), OK); + EXPECT_EQ(result->sendStatus, E_SEND_OK); + Mock::AllowLeak(mockProducer); + DestroyMessage(msg); + free(result); } TEST(cProducer, infoMock) { - MockDefaultMQProducer *mockProducer = new MockDefaultMQProducer("testGroup"); - CProducer *cProducer = (CProducer *) mockProducer; + MockDefaultMQProducer* mockProducer = new MockDefaultMQProducer("testGroup"); + CProducer* cProducer = (CProducer*)mockProducer; - EXPECT_CALL(*mockProducer, start()).Times(1); - EXPECT_EQ(StartProducer(cProducer), OK); + EXPECT_CALL(*mockProducer, start()).Times(1); + EXPECT_EQ(StartProducer(cProducer), OK); - EXPECT_CALL(*mockProducer, shutdown()).Times(1); - EXPECT_EQ(ShutdownProducer(cProducer), OK); + EXPECT_CALL(*mockProducer, shutdown()).Times(1); + EXPECT_EQ(ShutdownProducer(cProducer), OK); - EXPECT_CALL(*mockProducer, setLogFileSizeAndNum(_, _)).Times(1); - EXPECT_EQ(SetProducerLogFileNumAndSize(cProducer, 1, 1), OK); + EXPECT_CALL(*mockProducer, setLogFileSizeAndNum(_, _)).Times(1); + EXPECT_EQ(SetProducerLogFileNumAndSize(cProducer, 1, 1), OK); - EXPECT_CALL(*mockProducer, SetProducerLogLevel(_)).Times(1); - EXPECT_EQ(SetProducerLogLevel(cProducer, E_LOG_LEVEL_FATAL), OK); - Mock::AllowLeak(mockProducer); + EXPECT_CALL(*mockProducer, SetProducerLogLevel(_)).Times(1); + EXPECT_EQ(SetProducerLogLevel(cProducer, E_LOG_LEVEL_FATAL), OK); + Mock::AllowLeak(mockProducer); } TEST(cProducer, info) { - CProducer *cProducer = CreateProducer("groupTest"); - DefaultMQProducer *defaultMQProducer = (DefaultMQProducer *) cProducer; - EXPECT_TRUE(cProducer != NULL); - EXPECT_EQ(defaultMQProducer->getGroupName(), "groupTest"); + CProducer* cProducer = CreateProducer("groupTest"); + DefaultMQProducer* defaultMQProducer = (DefaultMQProducer*)cProducer; + EXPECT_TRUE(cProducer != NULL); + EXPECT_EQ(defaultMQProducer->getGroupName(), "groupTest"); - EXPECT_EQ(SetProducerNameServerAddress(cProducer, "127.0.0.1:9876"), OK); - EXPECT_EQ(defaultMQProducer->getNamesrvAddr(), "127.0.0.1:9876"); + EXPECT_EQ(SetProducerNameServerAddress(cProducer, "127.0.0.1:9876"), OK); + EXPECT_EQ(defaultMQProducer->getNamesrvAddr(), "127.0.0.1:9876"); - EXPECT_EQ(SetProducerNameServerDomain(cProducer, "domain"), OK); - EXPECT_EQ(defaultMQProducer->getNamesrvDomain(), "domain"); + EXPECT_EQ(SetProducerNameServerDomain(cProducer, "domain"), OK); + EXPECT_EQ(defaultMQProducer->getNamesrvDomain(), "domain"); - EXPECT_EQ(SetProducerGroupName(cProducer, "testGroup"), OK); - EXPECT_EQ(defaultMQProducer->getGroupName(), "testGroup"); + EXPECT_EQ(SetProducerGroupName(cProducer, "testGroup"), OK); + EXPECT_EQ(defaultMQProducer->getGroupName(), "testGroup"); - EXPECT_EQ(SetProducerInstanceName(cProducer, "instance"), OK); - EXPECT_EQ(defaultMQProducer->getInstanceName(), "instance"); + EXPECT_EQ(SetProducerInstanceName(cProducer, "instance"), OK); + EXPECT_EQ(defaultMQProducer->getInstanceName(), "instance"); - EXPECT_EQ(SetProducerSendMsgTimeout(cProducer, 1), OK); - EXPECT_EQ(defaultMQProducer->getSendMsgTimeout(), 1); + EXPECT_EQ(SetProducerSendMsgTimeout(cProducer, 1), OK); + EXPECT_EQ(defaultMQProducer->getSendMsgTimeout(), 1); - EXPECT_EQ(SetProducerMaxMessageSize(cProducer, 2), OK); - EXPECT_EQ(defaultMQProducer->getMaxMessageSize(), 2); + EXPECT_EQ(SetProducerMaxMessageSize(cProducer, 2), OK); + EXPECT_EQ(defaultMQProducer->getMaxMessageSize(), 2); - EXPECT_EQ(SetProducerCompressLevel(cProducer, 1), OK); - EXPECT_EQ(defaultMQProducer->getCompressLevel(), 1); + EXPECT_EQ(SetProducerCompressLevel(cProducer, 1), OK); + EXPECT_EQ(defaultMQProducer->getCompressLevel(), 1); - EXPECT_EQ(SetProducerSessionCredentials(NULL, NULL, NULL, NULL), NULL_POINTER); - EXPECT_EQ(SetProducerSessionCredentials(cProducer, "accessKey", "secretKey", "channel"), OK); - SessionCredentials sessionCredentials = defaultMQProducer->getSessionCredentials(); - EXPECT_EQ(sessionCredentials.getAccessKey(), "accessKey"); - Mock::AllowLeak(defaultMQProducer); + EXPECT_EQ(SetProducerSessionCredentials(NULL, NULL, NULL, NULL), NULL_POINTER); + EXPECT_EQ(SetProducerSessionCredentials(cProducer, "accessKey", "secretKey", "channel"), OK); + SessionCredentials sessionCredentials = defaultMQProducer->getSessionCredentials(); + EXPECT_EQ(sessionCredentials.getAccessKey(), "accessKey"); + Mock::AllowLeak(defaultMQProducer); } TEST(cProducer, null) { - EXPECT_TRUE(CreateProducer(NULL) == NULL); - EXPECT_EQ(StartProducer(NULL), NULL_POINTER); - EXPECT_EQ(ShutdownProducer(NULL), NULL_POINTER); - EXPECT_EQ(SetProducerNameServerAddress(NULL, NULL), NULL_POINTER); - EXPECT_EQ(SetProducerNameServerDomain(NULL, NULL), NULL_POINTER); - EXPECT_EQ(SetProducerGroupName(NULL, NULL), NULL_POINTER); - EXPECT_EQ(SetProducerInstanceName(NULL, NULL), NULL_POINTER); - EXPECT_EQ(SetProducerSessionCredentials(NULL, NULL, NULL, NULL), NULL_POINTER); - EXPECT_EQ(SetProducerLogLevel(NULL, E_LOG_LEVEL_FATAL), NULL_POINTER); - EXPECT_EQ(SetProducerSendMsgTimeout(NULL, 1), NULL_POINTER); - EXPECT_EQ(SetProducerCompressLevel(NULL, 1), NULL_POINTER); - EXPECT_EQ(SetProducerMaxMessageSize(NULL, 2), NULL_POINTER); - EXPECT_EQ(SetProducerLogLevel(NULL, E_LOG_LEVEL_FATAL), NULL_POINTER); - EXPECT_EQ(DestroyProducer(NULL), NULL_POINTER); + EXPECT_TRUE(CreateProducer(NULL) == NULL); + EXPECT_EQ(StartProducer(NULL), NULL_POINTER); + EXPECT_EQ(ShutdownProducer(NULL), NULL_POINTER); + EXPECT_EQ(SetProducerNameServerAddress(NULL, NULL), NULL_POINTER); + EXPECT_EQ(SetProducerNameServerDomain(NULL, NULL), NULL_POINTER); + EXPECT_EQ(SetProducerGroupName(NULL, NULL), NULL_POINTER); + EXPECT_EQ(SetProducerInstanceName(NULL, NULL), NULL_POINTER); + EXPECT_EQ(SetProducerSessionCredentials(NULL, NULL, NULL, NULL), NULL_POINTER); + EXPECT_EQ(SetProducerLogLevel(NULL, E_LOG_LEVEL_FATAL), NULL_POINTER); + EXPECT_EQ(SetProducerSendMsgTimeout(NULL, 1), NULL_POINTER); + EXPECT_EQ(SetProducerCompressLevel(NULL, 1), NULL_POINTER); + EXPECT_EQ(SetProducerMaxMessageSize(NULL, 2), NULL_POINTER); + EXPECT_EQ(SetProducerLogLevel(NULL, E_LOG_LEVEL_FATAL), NULL_POINTER); + EXPECT_EQ(DestroyProducer(NULL), NULL_POINTER); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "cProducer.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "cProducer.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/extern/CPullConsumerTest.cpp b/test/src/extern/CPullConsumerTest.cpp index 6a1118d1b..e326e749a 100644 --- a/test/src/extern/CPullConsumerTest.cpp +++ b/test/src/extern/CPullConsumerTest.cpp @@ -51,161 +51,161 @@ using rocketmq::PullStatus; using rocketmq::SessionCredentials; class MockDefaultMQPullConsumer : public DefaultMQPullConsumer { - public: - MockDefaultMQPullConsumer(const string &groupname) : DefaultMQPullConsumer(groupname) {} - MOCK_METHOD0(start, void()); - MOCK_METHOD0(shutdown, void()); - MOCK_METHOD2(setLogFileSizeAndNum, void(int, long)); - MOCK_METHOD1(setLogLevel, void(elogLevel)); - MOCK_METHOD2(fetchSubscribeMessageQueues, void(const string &, vector &)); - MOCK_METHOD4(pull, PullResult(const MQMessageQueue &, const string &, int64, int)); + public: + MockDefaultMQPullConsumer(const string& groupname) : DefaultMQPullConsumer(groupname) {} + MOCK_METHOD0(start, void()); + MOCK_METHOD0(shutdown, void()); + MOCK_METHOD2(setLogFileSizeAndNum, void(int, long)); + MOCK_METHOD1(setLogLevel, void(elogLevel)); + MOCK_METHOD2(fetchSubscribeMessageQueues, void(const string&, vector&)); + MOCK_METHOD4(pull, PullResult(const MQMessageQueue&, const string&, int64, int)); }; TEST(cpullConsumer, pull) { - MockDefaultMQPullConsumer *mqPullConsumer = new MockDefaultMQPullConsumer("groudId"); - CPullConsumer *pullConsumer = (CPullConsumer *) mqPullConsumer; + MockDefaultMQPullConsumer* mqPullConsumer = new MockDefaultMQPullConsumer("groudId"); + CPullConsumer* pullConsumer = (CPullConsumer*)mqPullConsumer; - CMessageQueue *cMessageQueue; - cMessageQueue = (CMessageQueue *) malloc(sizeof(CMessageQueue)); - strncpy(cMessageQueue->topic, "testTopic", 8); - strncpy(cMessageQueue->brokerName, "testBroker", 9); - cMessageQueue->queueId = 1; + CMessageQueue* cMessageQueue; + cMessageQueue = (CMessageQueue*)malloc(sizeof(CMessageQueue)); + strncpy(cMessageQueue->topic, "testTopic", 8); + strncpy(cMessageQueue->brokerName, "testBroker", 9); + cMessageQueue->queueId = 1; - PullResult timeOutPullResult(PullStatus::BROKER_TIMEOUT, 1, 2, 3); + PullResult timeOutPullResult(PullStatus::BROKER_TIMEOUT, 1, 2, 3); - PullResult noNewMsgPullResult(PullStatus::NO_NEW_MSG, 1, 2, 3); + PullResult noNewMsgPullResult(PullStatus::NO_NEW_MSG, 1, 2, 3); - PullResult noMatchedMsgPullResult(PullStatus::NO_MATCHED_MSG, 1, 2, 3); + PullResult noMatchedMsgPullResult(PullStatus::NO_MATCHED_MSG, 1, 2, 3); - PullResult offsetIllegalPullResult(PullStatus::OFFSET_ILLEGAL, 1, 2, 3); + 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++) { - MQMessageExt ext; - src.push_back(ext); - } + vector src; + for (int i = 0; i < 5; i++) { + MQMessageExt ext; + src.push_back(ext); + } - PullResult foundPullResult(PullStatus::FOUND, 1, 2, 3, src); + PullResult foundPullResult(PullStatus::FOUND, 1, 2, 3, src); - EXPECT_CALL(*mqPullConsumer, pull(_, _, _, _)) - .WillOnce(Return(timeOutPullResult)) - .WillOnce(Return(noNewMsgPullResult)) - .WillOnce(Return(noMatchedMsgPullResult)) - .WillOnce(Return(offsetIllegalPullResult)) - .WillOnce(Return(defaultPullResult)) - /*.WillOnce(Return(timeOutPullResult))*/.WillOnce(Return(foundPullResult)); + EXPECT_CALL(*mqPullConsumer, pull(_, _, _, _)) + .WillOnce(Return(timeOutPullResult)) + .WillOnce(Return(noNewMsgPullResult)) + .WillOnce(Return(noMatchedMsgPullResult)) + .WillOnce(Return(offsetIllegalPullResult)) + .WillOnce(Return(defaultPullResult)) + /*.WillOnce(Return(timeOutPullResult))*/.WillOnce(Return(foundPullResult)); - CPullResult timeOutcPullResult = Pull(pullConsumer, cMessageQueue, "123123", 0, 0); - EXPECT_EQ(timeOutcPullResult.pullStatus, E_BROKER_TIMEOUT); + CPullResult timeOutcPullResult = Pull(pullConsumer, cMessageQueue, "123123", 0, 0); + EXPECT_EQ(timeOutcPullResult.pullStatus, E_BROKER_TIMEOUT); - CPullResult noNewMsgcPullResult = Pull(pullConsumer, cMessageQueue, "123123", 0, 0); - EXPECT_EQ(noNewMsgcPullResult.pullStatus, E_NO_NEW_MSG); + CPullResult noNewMsgcPullResult = Pull(pullConsumer, cMessageQueue, "123123", 0, 0); + EXPECT_EQ(noNewMsgcPullResult.pullStatus, E_NO_NEW_MSG); - CPullResult noMatchedMsgcPullResult = Pull(pullConsumer, cMessageQueue, "123123", 0, 0); - EXPECT_EQ(noMatchedMsgcPullResult.pullStatus, E_NO_MATCHED_MSG); + CPullResult noMatchedMsgcPullResult = Pull(pullConsumer, cMessageQueue, "123123", 0, 0); + EXPECT_EQ(noMatchedMsgcPullResult.pullStatus, E_NO_MATCHED_MSG); - CPullResult offsetIllegalcPullResult = Pull(pullConsumer, cMessageQueue, "123123", 0, 0); - EXPECT_EQ(offsetIllegalcPullResult.pullStatus, E_OFFSET_ILLEGAL); + CPullResult offsetIllegalcPullResult = Pull(pullConsumer, cMessageQueue, "123123", 0, 0); + EXPECT_EQ(offsetIllegalcPullResult.pullStatus, E_OFFSET_ILLEGAL); - CPullResult defaultcPullResult = Pull(pullConsumer, cMessageQueue, "123123", 0, 0); - EXPECT_EQ(defaultcPullResult.pullStatus, E_NO_NEW_MSG); + CPullResult defaultcPullResult = Pull(pullConsumer, cMessageQueue, "123123", 0, 0); + EXPECT_EQ(defaultcPullResult.pullStatus, E_NO_NEW_MSG); - CPullResult exceptionPullResult = Pull(pullConsumer, cMessageQueue, NULL, 0, 0); - EXPECT_EQ(exceptionPullResult.pullStatus, E_BROKER_TIMEOUT); + CPullResult exceptionPullResult = Pull(pullConsumer, cMessageQueue, NULL, 0, 0); + EXPECT_EQ(exceptionPullResult.pullStatus, E_BROKER_TIMEOUT); - CPullResult foundcPullResult = Pull(pullConsumer, cMessageQueue, "123123", 0, 0); - EXPECT_EQ(foundcPullResult.pullStatus, E_FOUND); + CPullResult foundcPullResult = Pull(pullConsumer, cMessageQueue, "123123", 0, 0); + EXPECT_EQ(foundcPullResult.pullStatus, E_FOUND); - delete mqPullConsumer; + delete mqPullConsumer; } TEST(cpullConsumer, infoMock) { - MockDefaultMQPullConsumer *mqPullConsumer = new MockDefaultMQPullConsumer("groudId"); - CPullConsumer *pullConsumer = (CPullConsumer *) mqPullConsumer; + MockDefaultMQPullConsumer* mqPullConsumer = new MockDefaultMQPullConsumer("groudId"); + CPullConsumer* pullConsumer = (CPullConsumer*)mqPullConsumer; - Expectation exp = EXPECT_CALL(*mqPullConsumer, start()).Times(1); - EXPECT_EQ(StartPullConsumer(pullConsumer), OK); + Expectation exp = EXPECT_CALL(*mqPullConsumer, start()).Times(1); + EXPECT_EQ(StartPullConsumer(pullConsumer), OK); - EXPECT_CALL(*mqPullConsumer, shutdown()).Times(1); - EXPECT_EQ(ShutdownPullConsumer(pullConsumer), OK); + EXPECT_CALL(*mqPullConsumer, shutdown()).Times(1); + EXPECT_EQ(ShutdownPullConsumer(pullConsumer), OK); - // EXPECT_CALL(*mqPullConsumer,setLogFileSizeAndNum(_,_)).Times(1); - EXPECT_EQ(SetPullConsumerLogFileNumAndSize(pullConsumer, 1, 2), OK); + // EXPECT_CALL(*mqPullConsumer,setLogFileSizeAndNum(_,_)).Times(1); + EXPECT_EQ(SetPullConsumerLogFileNumAndSize(pullConsumer, 1, 2), OK); - // EXPECT_CALL(*mqPullConsumer,setLogLevel(_)).Times(1); - EXPECT_EQ(SetPullConsumerLogLevel(pullConsumer, E_LOG_LEVEL_INFO), OK); + // EXPECT_CALL(*mqPullConsumer,setLogLevel(_)).Times(1); + EXPECT_EQ(SetPullConsumerLogLevel(pullConsumer, E_LOG_LEVEL_INFO), OK); - std::vector fullMQ; - for (int i = 0; i < 5; i++) { - MQMessageQueue queue("testTopic", "testsBroker", i); - fullMQ.push_back(queue); - } + std::vector fullMQ; + for (int i = 0; i < 5; i++) { + MQMessageQueue queue("testTopic", "testsBroker", i); + fullMQ.push_back(queue); + } - EXPECT_CALL(*mqPullConsumer, fetchSubscribeMessageQueues(_, _)).Times(1).WillOnce(SetArgReferee<1>(fullMQ)); - CMessageQueue *mqs = NULL; - int size = 0; - FetchSubscriptionMessageQueues(pullConsumer, "testTopic", &mqs, &size); - EXPECT_EQ(size, 5); + EXPECT_CALL(*mqPullConsumer, fetchSubscribeMessageQueues(_, _)).Times(1).WillOnce(SetArgReferee<1>(fullMQ)); + CMessageQueue* mqs = NULL; + int size = 0; + FetchSubscriptionMessageQueues(pullConsumer, "testTopic", &mqs, &size); + EXPECT_EQ(size, 5); - delete mqPullConsumer; + delete mqPullConsumer; } TEST(cpullConsumer, init) { - CPullConsumer *pullConsumer = CreatePullConsumer("testGroupId"); - DefaultMQPullConsumer *defaultMQPullConsumer = (DefaultMQPullConsumer *) pullConsumer; - EXPECT_FALSE(pullConsumer == NULL); + CPullConsumer* pullConsumer = CreatePullConsumer("testGroupId"); + DefaultMQPullConsumer* defaultMQPullConsumer = (DefaultMQPullConsumer*)pullConsumer; + EXPECT_FALSE(pullConsumer == NULL); - EXPECT_EQ(SetPullConsumerGroupID(pullConsumer, "groupId"), OK); - EXPECT_EQ(GetPullConsumerGroupID(pullConsumer), defaultMQPullConsumer->getGroupName().c_str()); + EXPECT_EQ(SetPullConsumerGroupID(pullConsumer, "groupId"), OK); + EXPECT_EQ(GetPullConsumerGroupID(pullConsumer), defaultMQPullConsumer->getGroupName().c_str()); - EXPECT_EQ(SetPullConsumerNameServerAddress(pullConsumer, "127.0.0.1:10091"), OK); - EXPECT_EQ(defaultMQPullConsumer->getNamesrvAddr(), "127.0.0.1:10091"); + EXPECT_EQ(SetPullConsumerNameServerAddress(pullConsumer, "127.0.0.1:10091"), OK); + EXPECT_EQ(defaultMQPullConsumer->getNamesrvAddr(), "127.0.0.1:10091"); - EXPECT_EQ(SetPullConsumerNameServerDomain(pullConsumer, "domain"), OK); - EXPECT_EQ(defaultMQPullConsumer->getNamesrvDomain(), "domain"); + EXPECT_EQ(SetPullConsumerNameServerDomain(pullConsumer, "domain"), OK); + EXPECT_EQ(defaultMQPullConsumer->getNamesrvDomain(), "domain"); - EXPECT_EQ(SetPullConsumerSessionCredentials(pullConsumer, "accessKey", "secretKey", "channel"), OK); - SessionCredentials sessionCredentials = defaultMQPullConsumer->getSessionCredentials(); - EXPECT_EQ(sessionCredentials.getAccessKey(), "accessKey"); + EXPECT_EQ(SetPullConsumerSessionCredentials(pullConsumer, "accessKey", "secretKey", "channel"), OK); + SessionCredentials sessionCredentials = defaultMQPullConsumer->getSessionCredentials(); + EXPECT_EQ(sessionCredentials.getAccessKey(), "accessKey"); - EXPECT_EQ(SetPullConsumerLogPath(pullConsumer, NULL), OK); + EXPECT_EQ(SetPullConsumerLogPath(pullConsumer, NULL), OK); - // EXPECT_EQ(SetPullConsumerLogFileNumAndSize(pullConsumer,NULL,NULL),NULL_POINTER); - EXPECT_EQ(SetPullConsumerLogLevel(pullConsumer, E_LOG_LEVEL_DEBUG), OK); + // EXPECT_EQ(SetPullConsumerLogFileNumAndSize(pullConsumer,NULL,NULL),NULL_POINTER); + EXPECT_EQ(SetPullConsumerLogLevel(pullConsumer, E_LOG_LEVEL_DEBUG), OK); } TEST(cpullConsumer, null) { - CPullConsumer *pullConsumer = CreatePullConsumer("testGroupId"); - DefaultMQPullConsumer *defaultMQPullConsumer = (DefaultMQPullConsumer *) pullConsumer; - EXPECT_FALSE(pullConsumer == NULL); + CPullConsumer* pullConsumer = CreatePullConsumer("testGroupId"); + DefaultMQPullConsumer* defaultMQPullConsumer = (DefaultMQPullConsumer*)pullConsumer; + EXPECT_FALSE(pullConsumer == NULL); - EXPECT_EQ(SetPullConsumerGroupID(pullConsumer, "groupId"), OK); - EXPECT_EQ(GetPullConsumerGroupID(pullConsumer), defaultMQPullConsumer->getGroupName().c_str()); + EXPECT_EQ(SetPullConsumerGroupID(pullConsumer, "groupId"), OK); + EXPECT_EQ(GetPullConsumerGroupID(pullConsumer), defaultMQPullConsumer->getGroupName().c_str()); - EXPECT_EQ(SetPullConsumerNameServerAddress(pullConsumer, "127.0.0.1:10091"), OK); - EXPECT_EQ(defaultMQPullConsumer->getNamesrvAddr(), "127.0.0.1:10091"); + EXPECT_EQ(SetPullConsumerNameServerAddress(pullConsumer, "127.0.0.1:10091"), OK); + EXPECT_EQ(defaultMQPullConsumer->getNamesrvAddr(), "127.0.0.1:10091"); - EXPECT_EQ(SetPullConsumerNameServerDomain(pullConsumer, "domain"), OK); - EXPECT_EQ(defaultMQPullConsumer->getNamesrvDomain(), "domain"); + EXPECT_EQ(SetPullConsumerNameServerDomain(pullConsumer, "domain"), OK); + EXPECT_EQ(defaultMQPullConsumer->getNamesrvDomain(), "domain"); - EXPECT_EQ(SetPullConsumerSessionCredentials(pullConsumer, "accessKey", "secretKey", "channel"), OK); - SessionCredentials sessionCredentials = defaultMQPullConsumer->getSessionCredentials(); - EXPECT_EQ(sessionCredentials.getAccessKey(), "accessKey"); + EXPECT_EQ(SetPullConsumerSessionCredentials(pullConsumer, "accessKey", "secretKey", "channel"), OK); + SessionCredentials sessionCredentials = defaultMQPullConsumer->getSessionCredentials(); + EXPECT_EQ(sessionCredentials.getAccessKey(), "accessKey"); - EXPECT_EQ(SetPullConsumerLogPath(pullConsumer, NULL), OK); - // EXPECT_EQ(SetPullConsumerLogFileNumAndSize(pullConsumer,NULL,NULL),NULL_POINTER); - EXPECT_EQ(SetPullConsumerLogLevel(pullConsumer, E_LOG_LEVEL_DEBUG), OK); - EXPECT_EQ(DestroyPullConsumer(pullConsumer), OK); - EXPECT_EQ(StartPullConsumer(NULL), NULL_POINTER); - EXPECT_EQ(ShutdownPullConsumer(NULL), NULL_POINTER); + EXPECT_EQ(SetPullConsumerLogPath(pullConsumer, NULL), OK); + // EXPECT_EQ(SetPullConsumerLogFileNumAndSize(pullConsumer,NULL,NULL),NULL_POINTER); + EXPECT_EQ(SetPullConsumerLogLevel(pullConsumer, E_LOG_LEVEL_DEBUG), OK); + EXPECT_EQ(DestroyPullConsumer(pullConsumer), OK); + EXPECT_EQ(StartPullConsumer(NULL), NULL_POINTER); + EXPECT_EQ(ShutdownPullConsumer(NULL), NULL_POINTER); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "cpullConsumer.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "cpullConsumer.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/extern/CPushConsumerTest.cpp b/test/src/extern/CPushConsumerTest.cpp index 30a645df8..a2d2fc9fd 100644 --- a/test/src/extern/CPushConsumerTest.cpp +++ b/test/src/extern/CPushConsumerTest.cpp @@ -42,115 +42,115 @@ using rocketmq::MessageModel; using rocketmq::SessionCredentials; class MockDefaultMQPushConsumer : public DefaultMQPushConsumer { - public: - MockDefaultMQPushConsumer(const string &groupname) : DefaultMQPushConsumer(groupname) {} + public: + MockDefaultMQPushConsumer(const string& groupname) : DefaultMQPushConsumer(groupname) {} - MOCK_METHOD0(start, void()); - MOCK_METHOD0(shutdown, void()); - MOCK_METHOD2(setLogFileSizeAndNum, void(int, long)); - MOCK_METHOD1(setLogLevel, void(elogLevel)); + MOCK_METHOD0(start, void()); + MOCK_METHOD0(shutdown, void()); + MOCK_METHOD2(setLogFileSizeAndNum, void(int, long)); + MOCK_METHOD1(setLogLevel, void(elogLevel)); }; TEST(cPushComsumer, infomock) { - MockDefaultMQPushConsumer *pushComsumer = new MockDefaultMQPushConsumer("testGroup"); - CPushConsumer *consumer = (CPushConsumer *) pushComsumer; + MockDefaultMQPushConsumer* pushComsumer = new MockDefaultMQPushConsumer("testGroup"); + CPushConsumer* consumer = (CPushConsumer*)pushComsumer; - EXPECT_CALL(*pushComsumer, start()).Times(1); - EXPECT_EQ(StartPushConsumer(consumer), OK); + EXPECT_CALL(*pushComsumer, start()).Times(1); + EXPECT_EQ(StartPushConsumer(consumer), OK); - EXPECT_CALL(*pushComsumer, shutdown()).Times(1); - EXPECT_EQ(ShutdownPushConsumer(consumer), OK); + EXPECT_CALL(*pushComsumer, shutdown()).Times(1); + EXPECT_EQ(ShutdownPushConsumer(consumer), OK); - EXPECT_CALL(*pushComsumer, setLogFileSizeAndNum(1, 1)).Times(1); - pushComsumer->setLogFileSizeAndNum(1, 1); - EXPECT_EQ(SetPushConsumerLogFileNumAndSize(consumer, 1, 1), OK); + EXPECT_CALL(*pushComsumer, setLogFileSizeAndNum(1, 1)).Times(1); + pushComsumer->setLogFileSizeAndNum(1, 1); + EXPECT_EQ(SetPushConsumerLogFileNumAndSize(consumer, 1, 1), OK); - // EXPECT_CALL(*pushComsumer,setLogLevel(_)).Times(1); - EXPECT_EQ(SetPushConsumerLogLevel(consumer, E_LOG_LEVEL_FATAL), OK); + // EXPECT_CALL(*pushComsumer,setLogLevel(_)).Times(1); + EXPECT_EQ(SetPushConsumerLogLevel(consumer, E_LOG_LEVEL_FATAL), OK); - Mock::AllowLeak(pushComsumer); + Mock::AllowLeak(pushComsumer); } int MessageCallBackFunc(CPushConsumer* consumer, CMessageExt* msg) { - return 0; + return 0; } TEST(cPushComsumer, info) { - CPushConsumer *cpushConsumer = CreatePushConsumer("testGroup"); - DefaultMQPushConsumer *mqPushConsumer = (DefaultMQPushConsumer *) cpushConsumer; + CPushConsumer* cpushConsumer = CreatePushConsumer("testGroup"); + DefaultMQPushConsumer* mqPushConsumer = (DefaultMQPushConsumer*)cpushConsumer; - EXPECT_TRUE(cpushConsumer != NULL); - EXPECT_EQ(string(GetPushConsumerGroupID(cpushConsumer)), "testGroup"); + EXPECT_TRUE(cpushConsumer != NULL); + EXPECT_EQ(string(GetPushConsumerGroupID(cpushConsumer)), "testGroup"); - EXPECT_EQ(SetPushConsumerGroupID(cpushConsumer, "testGroupTwo"), OK); - EXPECT_EQ(string(GetPushConsumerGroupID(cpushConsumer)), "testGroupTwo"); + EXPECT_EQ(SetPushConsumerGroupID(cpushConsumer, "testGroupTwo"), OK); + EXPECT_EQ(string(GetPushConsumerGroupID(cpushConsumer)), "testGroupTwo"); - EXPECT_EQ(SetPushConsumerNameServerAddress(cpushConsumer, "127.0.0.1:9876"), OK); - EXPECT_EQ(mqPushConsumer->getNamesrvAddr(), "127.0.0.1:9876"); + EXPECT_EQ(SetPushConsumerNameServerAddress(cpushConsumer, "127.0.0.1:9876"), OK); + EXPECT_EQ(mqPushConsumer->getNamesrvAddr(), "127.0.0.1:9876"); - EXPECT_EQ(SetPushConsumerNameServerDomain(cpushConsumer, "domain"), OK); - EXPECT_EQ(mqPushConsumer->getNamesrvDomain(), "domain"); + EXPECT_EQ(SetPushConsumerNameServerDomain(cpushConsumer, "domain"), OK); + EXPECT_EQ(mqPushConsumer->getNamesrvDomain(), "domain"); - EXPECT_EQ(Subscribe(cpushConsumer, "testTopic", "testSub"), OK); + EXPECT_EQ(Subscribe(cpushConsumer, "testTopic", "testSub"), OK); - EXPECT_EQ(RegisterMessageCallbackOrderly(cpushConsumer, MessageCallBackFunc), OK); - EXPECT_EQ(mqPushConsumer->getMessageListenerType(), MessageListenerType::messageListenerOrderly); + EXPECT_EQ(RegisterMessageCallbackOrderly(cpushConsumer, MessageCallBackFunc), OK); + EXPECT_EQ(mqPushConsumer->getMessageListenerType(), MessageListenerType::messageListenerOrderly); - EXPECT_EQ(RegisterMessageCallback(cpushConsumer, MessageCallBackFunc), OK); - EXPECT_EQ(mqPushConsumer->getMessageListenerType(), MessageListenerType::messageListenerConcurrently); + EXPECT_EQ(RegisterMessageCallback(cpushConsumer, MessageCallBackFunc), OK); + EXPECT_EQ(mqPushConsumer->getMessageListenerType(), MessageListenerType::messageListenerConcurrently); - EXPECT_EQ(UnregisterMessageCallbackOrderly(cpushConsumer), OK); - EXPECT_EQ(UnregisterMessageCallback(cpushConsumer), OK); + EXPECT_EQ(UnregisterMessageCallbackOrderly(cpushConsumer), OK); + EXPECT_EQ(UnregisterMessageCallback(cpushConsumer), OK); - EXPECT_EQ(SetPushConsumerThreadCount(cpushConsumer, 10), OK); - EXPECT_EQ(mqPushConsumer->getConsumeThreadCount(), 10); + EXPECT_EQ(SetPushConsumerThreadCount(cpushConsumer, 10), OK); + EXPECT_EQ(mqPushConsumer->getConsumeThreadCount(), 10); - EXPECT_EQ(SetPushConsumerMessageBatchMaxSize(cpushConsumer, 1024), OK); - EXPECT_EQ(mqPushConsumer->getConsumeMessageBatchMaxSize(), 1024); + EXPECT_EQ(SetPushConsumerMessageBatchMaxSize(cpushConsumer, 1024), OK); + EXPECT_EQ(mqPushConsumer->getConsumeMessageBatchMaxSize(), 1024); - EXPECT_EQ(SetPushConsumerInstanceName(cpushConsumer, "instance"), OK); - EXPECT_EQ(mqPushConsumer->getInstanceName(), "instance"); + EXPECT_EQ(SetPushConsumerInstanceName(cpushConsumer, "instance"), OK); + EXPECT_EQ(mqPushConsumer->getInstanceName(), "instance"); - EXPECT_EQ(SetPushConsumerSessionCredentials(cpushConsumer, "accessKey", "secretKey", "channel"), OK); - SessionCredentials sessionCredentials = mqPushConsumer->getSessionCredentials(); - EXPECT_EQ(sessionCredentials.getAccessKey(), "accessKey"); + EXPECT_EQ(SetPushConsumerSessionCredentials(cpushConsumer, "accessKey", "secretKey", "channel"), OK); + SessionCredentials sessionCredentials = mqPushConsumer->getSessionCredentials(); + EXPECT_EQ(sessionCredentials.getAccessKey(), "accessKey"); - EXPECT_EQ(SetPushConsumerMessageModel(cpushConsumer, BROADCASTING), OK); - EXPECT_EQ(mqPushConsumer->getMessageModel(), MessageModel::BROADCASTING); + EXPECT_EQ(SetPushConsumerMessageModel(cpushConsumer, BROADCASTING), OK); + EXPECT_EQ(mqPushConsumer->getMessageModel(), MessageModel::BROADCASTING); - Mock::AllowLeak(mqPushConsumer); + Mock::AllowLeak(mqPushConsumer); } TEST(cPushComsumer, null) { - CPushConsumer *cpushConsumer = CreatePushConsumer("testGroup"); - - EXPECT_TRUE(CreatePushConsumer(NULL) == NULL); - EXPECT_EQ(DestroyPushConsumer(NULL), NULL_POINTER); - EXPECT_EQ(StartPushConsumer(NULL), NULL_POINTER); - EXPECT_EQ(ShutdownPushConsumer(NULL), NULL_POINTER); - EXPECT_EQ(SetPushConsumerGroupID(NULL, "testGroup"), NULL_POINTER); - EXPECT_TRUE(GetPushConsumerGroupID(NULL) == NULL); - EXPECT_EQ(SetPushConsumerNameServerAddress(NULL, NULL), NULL_POINTER); - EXPECT_EQ(SetPushConsumerNameServerDomain(NULL, NULL), NULL_POINTER); - EXPECT_EQ(Subscribe(NULL, NULL, NULL), NULL_POINTER); - EXPECT_EQ(RegisterMessageCallbackOrderly(NULL, NULL), NULL_POINTER); - EXPECT_EQ(RegisterMessageCallbackOrderly(cpushConsumer, NULL), NULL_POINTER); - EXPECT_EQ(RegisterMessageCallback(NULL, NULL), NULL_POINTER); - EXPECT_EQ(UnregisterMessageCallbackOrderly(NULL), NULL_POINTER); - EXPECT_EQ(UnregisterMessageCallback(NULL), NULL_POINTER); - EXPECT_EQ(SetPushConsumerThreadCount(NULL, 0), NULL_POINTER); - EXPECT_EQ(SetPushConsumerMessageBatchMaxSize(NULL, 0), NULL_POINTER); - EXPECT_EQ(SetPushConsumerInstanceName(NULL, NULL), NULL_POINTER); - EXPECT_EQ(SetPushConsumerSessionCredentials(NULL, NULL, NULL, NULL), NULL_POINTER); - EXPECT_EQ(SetPushConsumerLogPath(NULL, NULL), NULL_POINTER); - EXPECT_EQ(SetPushConsumerLogFileNumAndSize(NULL, 1, 1), NULL_POINTER); - EXPECT_EQ(SetPushConsumerLogLevel(NULL, E_LOG_LEVEL_LEVEL_NUM), NULL_POINTER); - EXPECT_EQ(SetPushConsumerMessageModel(NULL, BROADCASTING), NULL_POINTER); + CPushConsumer* cpushConsumer = CreatePushConsumer("testGroup"); + + EXPECT_TRUE(CreatePushConsumer(NULL) == NULL); + EXPECT_EQ(DestroyPushConsumer(NULL), NULL_POINTER); + EXPECT_EQ(StartPushConsumer(NULL), NULL_POINTER); + EXPECT_EQ(ShutdownPushConsumer(NULL), NULL_POINTER); + EXPECT_EQ(SetPushConsumerGroupID(NULL, "testGroup"), NULL_POINTER); + EXPECT_TRUE(GetPushConsumerGroupID(NULL) == NULL); + EXPECT_EQ(SetPushConsumerNameServerAddress(NULL, NULL), NULL_POINTER); + EXPECT_EQ(SetPushConsumerNameServerDomain(NULL, NULL), NULL_POINTER); + EXPECT_EQ(Subscribe(NULL, NULL, NULL), NULL_POINTER); + EXPECT_EQ(RegisterMessageCallbackOrderly(NULL, NULL), NULL_POINTER); + EXPECT_EQ(RegisterMessageCallbackOrderly(cpushConsumer, NULL), NULL_POINTER); + EXPECT_EQ(RegisterMessageCallback(NULL, NULL), NULL_POINTER); + EXPECT_EQ(UnregisterMessageCallbackOrderly(NULL), NULL_POINTER); + EXPECT_EQ(UnregisterMessageCallback(NULL), NULL_POINTER); + EXPECT_EQ(SetPushConsumerThreadCount(NULL, 0), NULL_POINTER); + EXPECT_EQ(SetPushConsumerMessageBatchMaxSize(NULL, 0), NULL_POINTER); + EXPECT_EQ(SetPushConsumerInstanceName(NULL, NULL), NULL_POINTER); + EXPECT_EQ(SetPushConsumerSessionCredentials(NULL, NULL, NULL, NULL), NULL_POINTER); + EXPECT_EQ(SetPushConsumerLogPath(NULL, NULL), NULL_POINTER); + EXPECT_EQ(SetPushConsumerLogFileNumAndSize(NULL, 1, 1), NULL_POINTER); + EXPECT_EQ(SetPushConsumerLogLevel(NULL, E_LOG_LEVEL_LEVEL_NUM), NULL_POINTER); + EXPECT_EQ(SetPushConsumerMessageModel(NULL, BROADCASTING), NULL_POINTER); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(filter) = "cPushComsumer.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(filter) = "cPushComsumer.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/message/MQDecoderTest.cpp b/test/src/message/MQDecoderTest.cpp index 4893200a4..748d89c54 100644 --- a/test/src/message/MQDecoderTest.cpp +++ b/test/src/message/MQDecoderTest.cpp @@ -53,152 +53,152 @@ using rocketmq::UtilAll; // TODO TEST(decoders, messageId) { - int host; - int port; - string msgIdStr = - MQDecoder::createMessageId(rocketmq::IPPort2socketAddress(inet_addr("127.0.0.1"), 10091), (int64) 1024); - MQMessageId msgId = MQDecoder::decodeMessageId(msgIdStr); + int host; + int port; + string msgIdStr = + MQDecoder::createMessageId(rocketmq::IPPort2socketAddress(inet_addr("127.0.0.1"), 10091), (int64)1024); + MQMessageId msgId = MQDecoder::decodeMessageId(msgIdStr); - EXPECT_EQ(msgId.getOffset(), 1024); + EXPECT_EQ(msgId.getOffset(), 1024); - rocketmq::socketAddress2IPPort(msgId.getAddress(), host, port); - EXPECT_EQ(host, inet_addr("127.0.0.1")); - EXPECT_EQ(port, 10091); + rocketmq::socketAddress2IPPort(msgId.getAddress(), host, port); + EXPECT_EQ(host, inet_addr("127.0.0.1")); + EXPECT_EQ(port, 10091); } TEST(decoder, decoder) { - MQMessageExt mext; - MemoryOutputStream *memoryOut = new MemoryOutputStream(1024); - - // 1 TOTALSIZE 4 - memoryOut->writeIntBigEndian(107); - mext.setStoreSize(107); - - // 2 MAGICCODE sizeof(int) 8=4+4 - memoryOut->writeIntBigEndian(14); - - // 3 BODYCRC 12=8+4 - memoryOut->writeIntBigEndian(24); - mext.setBodyCRC(24); - // 4 QUEUEID 16=12+4 - memoryOut->writeIntBigEndian(4); - mext.setQueueId(4); - // 5 FLAG 20=16+4 - memoryOut->writeIntBigEndian(4); - mext.setFlag(4); - // 6 QUEUEOFFSET 28 = 20+8 - memoryOut->writeInt64BigEndian((int64) 1024); - mext.setQueueOffset(1024); - // 7 PHYSICALOFFSET 36=28+8 - 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); - 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); - mext.setStoreTimestamp(4096); - // 12 STOREHOST 72 = 64+8 - memoryOut->writeIntBigEndian(inet_addr("127.0.0.2")); - memoryOut->writeIntBigEndian(10092); - mext.setStoreHost(rocketmq::IPPort2socketAddress(inet_addr("127.0.0.2"), 10092)); - // 13 RECONSUMETIMES 76 = 72+4 - mext.setReconsumeTimes(111111); - memoryOut->writeIntBigEndian(mext.getReconsumeTimes()); - // 14 Prepared Transaction Offset 84 = 76+8 - memoryOut->writeInt64BigEndian((int64) 12); - mext.setPreparedTransactionOffset(12); - // 15 BODY 88 = 84+4 10 - string *body = new string("1234567890"); - mext.setBody(body->c_str()); - memoryOut->writeIntBigEndian(10); - memoryOut->write(body->c_str(), body->size()); - - // 16 TOPIC - memoryOut->writeByte(10); - memoryOut->write(body->c_str(), body->size()); - mext.setTopic(body->c_str()); - - // 17 PROPERTIES - memoryOut->writeShortBigEndian(0); - - mext.setMsgId(MQDecoder::createMessageId(mext.getStoreHost(), (int64) mext.getCommitLogOffset())); - - vector mqvec; - MemoryBlock block = memoryOut->getMemoryBlock(); - MQDecoder::decodes(&block, mqvec); - EXPECT_EQ(mqvec.size(), 1); - std::cout << mext.toString() << "\n"; - std::cout << mqvec[0].toString() << "\n"; - EXPECT_EQ(mqvec[0].toString(), mext.toString()); - - mqvec.clear(); - MQDecoder::decodes(&block, mqvec, false); - EXPECT_FALSE(mqvec[0].getBody().size()); - - //=============================================================== - // 8 SYSFLAG 40=36+4 - mext.setSysFlag(0 | MessageSysFlag::CompressedFlag); - memoryOut->setPosition(36); - memoryOut->writeIntBigEndian(mext.getSysFlag()); - - // 15 Body 84 - string outBody; - string boody("123123123"); - UtilAll::deflate(boody, outBody, 5); - mext.setBody(outBody); - - memoryOut->setPosition(84); - memoryOut->writeIntBigEndian(outBody.size()); - memoryOut->write(outBody.c_str(), outBody.size()); - - // 16 TOPIC - memoryOut->writeByte(10); - memoryOut->write(body->c_str(), body->size()); - mext.setTopic(body->c_str()); - - // 17 PROPERTIES - map properties; - properties["RocketMQ"] = "cpp-client"; - properties[MQMessage::PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX] = "123456"; - mext.setProperties(properties); - mext.setMsgId("123456"); - - string proString = MQDecoder::messageProperties2String(properties); - - memoryOut->writeShortBigEndian(proString.size()); - memoryOut->write(proString.c_str(), proString.size()); - - mext.setStoreSize(memoryOut->getDataSize()); - memoryOut->setPosition(0); - memoryOut->writeIntBigEndian(mext.getStoreSize()); - - block = memoryOut->getMemoryBlock(); - MQDecoder::decodes(&block, mqvec); - EXPECT_EQ(mqvec[0].toString(), mext.toString()); + MQMessageExt mext; + MemoryOutputStream* memoryOut = new MemoryOutputStream(1024); + + // 1 TOTALSIZE 4 + memoryOut->writeIntBigEndian(107); + mext.setStoreSize(107); + + // 2 MAGICCODE sizeof(int) 8=4+4 + memoryOut->writeIntBigEndian(14); + + // 3 BODYCRC 12=8+4 + memoryOut->writeIntBigEndian(24); + mext.setBodyCRC(24); + // 4 QUEUEID 16=12+4 + memoryOut->writeIntBigEndian(4); + mext.setQueueId(4); + // 5 FLAG 20=16+4 + memoryOut->writeIntBigEndian(4); + mext.setFlag(4); + // 6 QUEUEOFFSET 28 = 20+8 + memoryOut->writeInt64BigEndian((int64)1024); + mext.setQueueOffset(1024); + // 7 PHYSICALOFFSET 36=28+8 + 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); + 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); + mext.setStoreTimestamp(4096); + // 12 STOREHOST 72 = 64+8 + memoryOut->writeIntBigEndian(inet_addr("127.0.0.2")); + memoryOut->writeIntBigEndian(10092); + mext.setStoreHost(rocketmq::IPPort2socketAddress(inet_addr("127.0.0.2"), 10092)); + // 13 RECONSUMETIMES 76 = 72+4 + mext.setReconsumeTimes(111111); + memoryOut->writeIntBigEndian(mext.getReconsumeTimes()); + // 14 Prepared Transaction Offset 84 = 76+8 + memoryOut->writeInt64BigEndian((int64)12); + mext.setPreparedTransactionOffset(12); + // 15 BODY 88 = 84+4 10 + string* body = new string("1234567890"); + mext.setBody(body->c_str()); + memoryOut->writeIntBigEndian(10); + memoryOut->write(body->c_str(), body->size()); + + // 16 TOPIC + memoryOut->writeByte(10); + memoryOut->write(body->c_str(), body->size()); + mext.setTopic(body->c_str()); + + // 17 PROPERTIES + memoryOut->writeShortBigEndian(0); + + mext.setMsgId(MQDecoder::createMessageId(mext.getStoreHost(), (int64)mext.getCommitLogOffset())); + + vector mqvec; + MemoryBlock block = memoryOut->getMemoryBlock(); + MQDecoder::decodes(&block, mqvec); + EXPECT_EQ(mqvec.size(), 1); + std::cout << mext.toString() << "\n"; + std::cout << mqvec[0].toString() << "\n"; + EXPECT_EQ(mqvec[0].toString(), mext.toString()); + + mqvec.clear(); + MQDecoder::decodes(&block, mqvec, false); + EXPECT_FALSE(mqvec[0].getBody().size()); + + //=============================================================== + // 8 SYSFLAG 40=36+4 + mext.setSysFlag(0 | MessageSysFlag::CompressedFlag); + memoryOut->setPosition(36); + memoryOut->writeIntBigEndian(mext.getSysFlag()); + + // 15 Body 84 + string outBody; + string boody("123123123"); + UtilAll::deflate(boody, outBody, 5); + mext.setBody(outBody); + + memoryOut->setPosition(84); + memoryOut->writeIntBigEndian(outBody.size()); + memoryOut->write(outBody.c_str(), outBody.size()); + + // 16 TOPIC + memoryOut->writeByte(10); + memoryOut->write(body->c_str(), body->size()); + mext.setTopic(body->c_str()); + + // 17 PROPERTIES + map properties; + properties["RocketMQ"] = "cpp-client"; + properties[MQMessage::PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX] = "123456"; + mext.setProperties(properties); + mext.setMsgId("123456"); + + string proString = MQDecoder::messageProperties2String(properties); + + memoryOut->writeShortBigEndian(proString.size()); + memoryOut->write(proString.c_str(), proString.size()); + + mext.setStoreSize(memoryOut->getDataSize()); + memoryOut->setPosition(0); + memoryOut->writeIntBigEndian(mext.getStoreSize()); + + block = memoryOut->getMemoryBlock(); + MQDecoder::decodes(&block, mqvec); + EXPECT_EQ(mqvec[0].toString(), mext.toString()); } TEST(decoder, messagePropertiesAndToString) { - map properties; - properties["RocketMQ"] = "cpp-client"; - string proString = MQDecoder::messageProperties2String(properties); + map properties; + properties["RocketMQ"] = "cpp-client"; + string proString = MQDecoder::messageProperties2String(properties); - map newProperties; - MQDecoder::string2messageProperties(proString, newProperties); - EXPECT_EQ(properties, newProperties); + map newProperties; + MQDecoder::string2messageProperties(proString, newProperties); + EXPECT_EQ(properties, newProperties); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(filter) = "decoder.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; + testing::GTEST_FLAG(filter) = "decoder.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/message/MQMessageExtTest.cpp b/test/src/message/MQMessageExtTest.cpp index 3772a03d7..df8303d55 100644 --- a/test/src/message/MQMessageExtTest.cpp +++ b/test/src/message/MQMessageExtTest.cpp @@ -32,107 +32,107 @@ using rocketmq::MQMessageExt; using rocketmq::TopicFilterType; TEST(messageExt, init) { - MQMessageExt messageExt; - EXPECT_EQ(messageExt.getQueueOffset(), 0); - EXPECT_EQ(messageExt.getCommitLogOffset(), 0); - EXPECT_EQ(messageExt.getBornTimestamp(), 0); - EXPECT_EQ(messageExt.getStoreTimestamp(), 0); - EXPECT_EQ(messageExt.getPreparedTransactionOffset(), 0); - EXPECT_EQ(messageExt.getQueueId(), 0); - EXPECT_EQ(messageExt.getStoreSize(), 0); - EXPECT_EQ(messageExt.getReconsumeTimes(), 3); - EXPECT_EQ(messageExt.getBodyCRC(), 0); - EXPECT_EQ(messageExt.getMsgId(), ""); - EXPECT_EQ(messageExt.getOffsetMsgId(), ""); + MQMessageExt messageExt; + EXPECT_EQ(messageExt.getQueueOffset(), 0); + EXPECT_EQ(messageExt.getCommitLogOffset(), 0); + EXPECT_EQ(messageExt.getBornTimestamp(), 0); + EXPECT_EQ(messageExt.getStoreTimestamp(), 0); + EXPECT_EQ(messageExt.getPreparedTransactionOffset(), 0); + EXPECT_EQ(messageExt.getQueueId(), 0); + EXPECT_EQ(messageExt.getStoreSize(), 0); + EXPECT_EQ(messageExt.getReconsumeTimes(), 3); + EXPECT_EQ(messageExt.getBodyCRC(), 0); + EXPECT_EQ(messageExt.getMsgId(), ""); + EXPECT_EQ(messageExt.getOffsetMsgId(), ""); - messageExt.setQueueOffset(1); - EXPECT_EQ(messageExt.getQueueOffset(), 1); + messageExt.setQueueOffset(1); + EXPECT_EQ(messageExt.getQueueOffset(), 1); - messageExt.setCommitLogOffset(1024); - EXPECT_EQ(messageExt.getCommitLogOffset(), 1024); + messageExt.setCommitLogOffset(1024); + EXPECT_EQ(messageExt.getCommitLogOffset(), 1024); - messageExt.setBornTimestamp(1024); - EXPECT_EQ(messageExt.getBornTimestamp(), 1024); + messageExt.setBornTimestamp(1024); + EXPECT_EQ(messageExt.getBornTimestamp(), 1024); - messageExt.setStoreTimestamp(2048); - EXPECT_EQ(messageExt.getStoreTimestamp(), 2048); + messageExt.setStoreTimestamp(2048); + EXPECT_EQ(messageExt.getStoreTimestamp(), 2048); - messageExt.setPreparedTransactionOffset(4096); - EXPECT_EQ(messageExt.getPreparedTransactionOffset(), 4096); + messageExt.setPreparedTransactionOffset(4096); + EXPECT_EQ(messageExt.getPreparedTransactionOffset(), 4096); - messageExt.setQueueId(2); - EXPECT_EQ(messageExt.getQueueId(), 2); + messageExt.setQueueId(2); + EXPECT_EQ(messageExt.getQueueId(), 2); - messageExt.setStoreSize(12); - EXPECT_EQ(messageExt.getStoreSize(), 12); + messageExt.setStoreSize(12); + EXPECT_EQ(messageExt.getStoreSize(), 12); - messageExt.setReconsumeTimes(48); - EXPECT_EQ(messageExt.getReconsumeTimes(), 48); + messageExt.setReconsumeTimes(48); + EXPECT_EQ(messageExt.getReconsumeTimes(), 48); - messageExt.setBodyCRC(32); - EXPECT_EQ(messageExt.getBodyCRC(), 32); + messageExt.setBodyCRC(32); + EXPECT_EQ(messageExt.getBodyCRC(), 32); - messageExt.setMsgId("MsgId"); - EXPECT_EQ(messageExt.getMsgId(), "MsgId"); + messageExt.setMsgId("MsgId"); + EXPECT_EQ(messageExt.getMsgId(), "MsgId"); - messageExt.setOffsetMsgId("offsetMsgId"); - EXPECT_EQ(messageExt.getOffsetMsgId(), "offsetMsgId"); + messageExt.setOffsetMsgId("offsetMsgId"); + EXPECT_EQ(messageExt.getOffsetMsgId(), "offsetMsgId"); - messageExt.setBornTimestamp(1111); - EXPECT_EQ(messageExt.getBornTimestamp(), 1111); + messageExt.setBornTimestamp(1111); + EXPECT_EQ(messageExt.getBornTimestamp(), 1111); - messageExt.setStoreTimestamp(2222); - EXPECT_EQ(messageExt.getStoreTimestamp(), 2222); + messageExt.setStoreTimestamp(2222); + EXPECT_EQ(messageExt.getStoreTimestamp(), 2222); - struct sockaddr_in sa; - sa.sin_family = AF_INET; - sa.sin_port = htons(10091); - sa.sin_addr.s_addr = inet_addr("127.0.0.1"); + struct sockaddr_in sa; + sa.sin_family = AF_INET; + sa.sin_port = htons(10091); + sa.sin_addr.s_addr = inet_addr("127.0.0.1"); - sockaddr bornHost; - memcpy(&bornHost, &sa, sizeof(sockaddr)); + sockaddr bornHost; + memcpy(&bornHost, &sa, sizeof(sockaddr)); - messageExt.setBornHost(bornHost); - EXPECT_EQ(messageExt.getBornHostNameString(), rocketmq::getHostName(bornHost)); - EXPECT_EQ(messageExt.getBornHostString(), rocketmq::socketAddress2String(bornHost)); + messageExt.setBornHost(bornHost); + EXPECT_EQ(messageExt.getBornHostNameString(), rocketmq::getHostName(bornHost)); + EXPECT_EQ(messageExt.getBornHostString(), rocketmq::socketAddress2String(bornHost)); - struct sockaddr_in storeSa; - storeSa.sin_family = AF_INET; - storeSa.sin_port = htons(10092); - storeSa.sin_addr.s_addr = inet_addr("127.0.0.2"); + struct sockaddr_in storeSa; + storeSa.sin_family = AF_INET; + storeSa.sin_port = htons(10092); + storeSa.sin_addr.s_addr = inet_addr("127.0.0.2"); - sockaddr storeHost; - memcpy(&storeHost, &storeSa, sizeof(sockaddr)); - messageExt.setStoreHost(storeHost); - EXPECT_EQ(messageExt.getStoreHostString(), rocketmq::socketAddress2String(storeHost)); + sockaddr storeHost; + memcpy(&storeHost, &storeSa, sizeof(sockaddr)); + messageExt.setStoreHost(storeHost); + EXPECT_EQ(messageExt.getStoreHostString(), rocketmq::socketAddress2String(storeHost)); - MQMessageExt twoMessageExt(2, 1024, bornHost, 2048, storeHost, "msgId"); - EXPECT_EQ(twoMessageExt.getQueueOffset(), 0); - EXPECT_EQ(twoMessageExt.getCommitLogOffset(), 0); - EXPECT_EQ(twoMessageExt.getBornTimestamp(), 1024); - EXPECT_EQ(twoMessageExt.getStoreTimestamp(), 2048); - EXPECT_EQ(twoMessageExt.getPreparedTransactionOffset(), 0); - EXPECT_EQ(twoMessageExt.getQueueId(), 2); - EXPECT_EQ(twoMessageExt.getStoreSize(), 0); - EXPECT_EQ(twoMessageExt.getReconsumeTimes(), 3); - EXPECT_EQ(twoMessageExt.getBodyCRC(), 0); - EXPECT_EQ(twoMessageExt.getMsgId(), "msgId"); - EXPECT_EQ(twoMessageExt.getOffsetMsgId(), ""); + MQMessageExt twoMessageExt(2, 1024, bornHost, 2048, storeHost, "msgId"); + EXPECT_EQ(twoMessageExt.getQueueOffset(), 0); + EXPECT_EQ(twoMessageExt.getCommitLogOffset(), 0); + EXPECT_EQ(twoMessageExt.getBornTimestamp(), 1024); + EXPECT_EQ(twoMessageExt.getStoreTimestamp(), 2048); + EXPECT_EQ(twoMessageExt.getPreparedTransactionOffset(), 0); + EXPECT_EQ(twoMessageExt.getQueueId(), 2); + EXPECT_EQ(twoMessageExt.getStoreSize(), 0); + EXPECT_EQ(twoMessageExt.getReconsumeTimes(), 3); + EXPECT_EQ(twoMessageExt.getBodyCRC(), 0); + EXPECT_EQ(twoMessageExt.getMsgId(), "msgId"); + EXPECT_EQ(twoMessageExt.getOffsetMsgId(), ""); - EXPECT_EQ(twoMessageExt.getBornHostNameString(), rocketmq::getHostName(bornHost)); - EXPECT_EQ(twoMessageExt.getBornHostString(), rocketmq::socketAddress2String(bornHost)); + EXPECT_EQ(twoMessageExt.getBornHostNameString(), rocketmq::getHostName(bornHost)); + EXPECT_EQ(twoMessageExt.getBornHostString(), rocketmq::socketAddress2String(bornHost)); - EXPECT_EQ(twoMessageExt.getStoreHostString(), rocketmq::socketAddress2String(storeHost)); + EXPECT_EQ(twoMessageExt.getStoreHostString(), rocketmq::socketAddress2String(storeHost)); - EXPECT_EQ(MQMessageExt::parseTopicFilterType(MessageSysFlag::MultiTagsFlag), TopicFilterType::MULTI_TAG); + EXPECT_EQ(MQMessageExt::parseTopicFilterType(MessageSysFlag::MultiTagsFlag), TopicFilterType::MULTI_TAG); - EXPECT_EQ(MQMessageExt::parseTopicFilterType(0), TopicFilterType::SINGLE_TAG); + EXPECT_EQ(MQMessageExt::parseTopicFilterType(0), TopicFilterType::SINGLE_TAG); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(filter) = "messageExt.init"; - int itestts = RUN_ALL_TESTS(); - return itestts; + testing::GTEST_FLAG(filter) = "messageExt.init"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/message/MQMessageIdTest.cpp b/test/src/message/MQMessageIdTest.cpp index 191438de9..d83de2f28 100644 --- a/test/src/message/MQMessageIdTest.cpp +++ b/test/src/message/MQMessageIdTest.cpp @@ -28,29 +28,29 @@ using testing::Return; using rocketmq::MQMessageId; TEST(messageId, id) { - int host; - int port; - sockaddr addr = rocketmq::IPPort2socketAddress(inet_addr("127.0.0.1"), 10091); - MQMessageId id(addr, 1024); - - rocketmq::socketAddress2IPPort(id.getAddress(), host, port); - EXPECT_EQ(host, inet_addr("127.0.0.1")); - EXPECT_EQ(port, 10091); - EXPECT_EQ(id.getOffset(), 1024); - - id.setAddress(rocketmq::IPPort2socketAddress(inet_addr("127.0.0.2"), 10092)); - id.setOffset(2048); - - rocketmq::socketAddress2IPPort(id.getAddress(), host, port); - EXPECT_EQ(host, inet_addr("127.0.0.2")); - EXPECT_EQ(port, 10092); - EXPECT_EQ(id.getOffset(), 2048); + int host; + int port; + sockaddr addr = rocketmq::IPPort2socketAddress(inet_addr("127.0.0.1"), 10091); + MQMessageId id(addr, 1024); + + rocketmq::socketAddress2IPPort(id.getAddress(), host, port); + EXPECT_EQ(host, inet_addr("127.0.0.1")); + EXPECT_EQ(port, 10091); + EXPECT_EQ(id.getOffset(), 1024); + + id.setAddress(rocketmq::IPPort2socketAddress(inet_addr("127.0.0.2"), 10092)); + id.setOffset(2048); + + rocketmq::socketAddress2IPPort(id.getAddress(), host, port); + EXPECT_EQ(host, inet_addr("127.0.0.2")); + EXPECT_EQ(port, 10092); + EXPECT_EQ(id.getOffset(), 2048); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(filter) = "messageId.id"; - int itestts = RUN_ALL_TESTS(); - return itestts; + testing::GTEST_FLAG(filter) = "messageId.id"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/message/MQMessageQueueTest.cpp b/test/src/message/MQMessageQueueTest.cpp index 45223497f..c1c542dd7 100644 --- a/test/src/message/MQMessageQueueTest.cpp +++ b/test/src/message/MQMessageQueueTest.cpp @@ -26,61 +26,61 @@ using testing::Return; using rocketmq::MQMessageQueue; TEST(messageQueue, init) { - MQMessageQueue messageQueue; - EXPECT_EQ(messageQueue.getBrokerName(), ""); - EXPECT_EQ(messageQueue.getTopic(), ""); - EXPECT_EQ(messageQueue.getQueueId(), -1); + MQMessageQueue messageQueue; + EXPECT_EQ(messageQueue.getBrokerName(), ""); + EXPECT_EQ(messageQueue.getTopic(), ""); + EXPECT_EQ(messageQueue.getQueueId(), -1); - MQMessageQueue twoMessageQueue("testTopic", "testBroker", 1); - EXPECT_EQ(twoMessageQueue.getBrokerName(), "testBroker"); - EXPECT_EQ(twoMessageQueue.getTopic(), "testTopic"); - EXPECT_EQ(twoMessageQueue.getQueueId(), 1); + MQMessageQueue twoMessageQueue("testTopic", "testBroker", 1); + EXPECT_EQ(twoMessageQueue.getBrokerName(), "testBroker"); + EXPECT_EQ(twoMessageQueue.getTopic(), "testTopic"); + EXPECT_EQ(twoMessageQueue.getQueueId(), 1); - MQMessageQueue threeMessageQueue("threeTestTopic", "threeTestBroker", 2); - MQMessageQueue frouMessageQueue(threeMessageQueue); - EXPECT_EQ(frouMessageQueue.getBrokerName(), "threeTestBroker"); - EXPECT_EQ(frouMessageQueue.getTopic(), "threeTestTopic"); - EXPECT_EQ(frouMessageQueue.getQueueId(), 2); + MQMessageQueue threeMessageQueue("threeTestTopic", "threeTestBroker", 2); + MQMessageQueue frouMessageQueue(threeMessageQueue); + EXPECT_EQ(frouMessageQueue.getBrokerName(), "threeTestBroker"); + EXPECT_EQ(frouMessageQueue.getTopic(), "threeTestTopic"); + EXPECT_EQ(frouMessageQueue.getQueueId(), 2); - frouMessageQueue = twoMessageQueue; - EXPECT_EQ(frouMessageQueue.getBrokerName(), "testBroker"); - EXPECT_EQ(frouMessageQueue.getTopic(), "testTopic"); - EXPECT_EQ(frouMessageQueue.getQueueId(), 1); + frouMessageQueue = twoMessageQueue; + EXPECT_EQ(frouMessageQueue.getBrokerName(), "testBroker"); + EXPECT_EQ(frouMessageQueue.getTopic(), "testTopic"); + EXPECT_EQ(frouMessageQueue.getQueueId(), 1); - frouMessageQueue.setBrokerName("frouTestBroker"); - frouMessageQueue.setTopic("frouTestTopic"); - frouMessageQueue.setQueueId(4); - EXPECT_EQ(frouMessageQueue.getBrokerName(), "frouTestBroker"); - EXPECT_EQ(frouMessageQueue.getTopic(), "frouTestTopic"); - EXPECT_EQ(frouMessageQueue.getQueueId(), 4); + frouMessageQueue.setBrokerName("frouTestBroker"); + frouMessageQueue.setTopic("frouTestTopic"); + frouMessageQueue.setQueueId(4); + EXPECT_EQ(frouMessageQueue.getBrokerName(), "frouTestBroker"); + EXPECT_EQ(frouMessageQueue.getTopic(), "frouTestTopic"); + EXPECT_EQ(frouMessageQueue.getQueueId(), 4); } TEST(messageQueue, operators) { - MQMessageQueue messageQueue; - EXPECT_EQ(messageQueue, messageQueue); - EXPECT_EQ(messageQueue.compareTo(messageQueue), 0); + MQMessageQueue messageQueue; + EXPECT_EQ(messageQueue, messageQueue); + EXPECT_EQ(messageQueue.compareTo(messageQueue), 0); - MQMessageQueue twoMessageQueue; - EXPECT_EQ(messageQueue, twoMessageQueue); - EXPECT_EQ(messageQueue.compareTo(twoMessageQueue), 0); + MQMessageQueue twoMessageQueue; + EXPECT_EQ(messageQueue, twoMessageQueue); + EXPECT_EQ(messageQueue.compareTo(twoMessageQueue), 0); - twoMessageQueue.setTopic("testTopic"); - EXPECT_FALSE(messageQueue == twoMessageQueue); - EXPECT_FALSE(messageQueue.compareTo(twoMessageQueue) == 0); + twoMessageQueue.setTopic("testTopic"); + EXPECT_FALSE(messageQueue == twoMessageQueue); + EXPECT_FALSE(messageQueue.compareTo(twoMessageQueue) == 0); - twoMessageQueue.setQueueId(1); - EXPECT_FALSE(messageQueue == twoMessageQueue); - EXPECT_FALSE(messageQueue.compareTo(twoMessageQueue) == 0); + twoMessageQueue.setQueueId(1); + EXPECT_FALSE(messageQueue == twoMessageQueue); + EXPECT_FALSE(messageQueue.compareTo(twoMessageQueue) == 0); - twoMessageQueue.setBrokerName("testBroker"); - EXPECT_FALSE(messageQueue == twoMessageQueue); - EXPECT_FALSE(messageQueue.compareTo(twoMessageQueue) == 0); + twoMessageQueue.setBrokerName("testBroker"); + EXPECT_FALSE(messageQueue == twoMessageQueue); + EXPECT_FALSE(messageQueue.compareTo(twoMessageQueue) == 0); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(filter) = "messageQueue.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; + testing::GTEST_FLAG(filter) = "messageQueue.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/message/MQMessageTest.cpp b/test/src/message/MQMessageTest.cpp index 2a121f520..6c0165ac4 100644 --- a/test/src/message/MQMessageTest.cpp +++ b/test/src/message/MQMessageTest.cpp @@ -32,121 +32,121 @@ using testing::Return; using rocketmq::MQMessage; TEST(message, Init) { - MQMessage messageOne; - EXPECT_EQ(messageOne.getTopic(), ""); - EXPECT_EQ(messageOne.getBody(), ""); - EXPECT_EQ(messageOne.getTags(), ""); - EXPECT_EQ(messageOne.getFlag(), 0); - - MQMessage messageTwo("test", "testBody"); - EXPECT_EQ(messageTwo.getTopic(), "test"); - EXPECT_EQ(messageTwo.getBody(), "testBody"); - EXPECT_EQ(messageTwo.getTags(), ""); - EXPECT_EQ(messageTwo.getFlag(), 0); - - MQMessage messageThree("test", "tagTest", "testBody"); - EXPECT_EQ(messageThree.getTopic(), "test"); - EXPECT_EQ(messageThree.getBody(), "testBody"); - EXPECT_EQ(messageThree.getTags(), "tagTest"); - EXPECT_EQ(messageThree.getFlag(), 0); - - MQMessage messageFour("test", "tagTest", "testKey", "testBody"); - EXPECT_EQ(messageFour.getTopic(), "test"); - EXPECT_EQ(messageFour.getBody(), "testBody"); - EXPECT_EQ(messageFour.getTags(), "tagTest"); - EXPECT_EQ(messageFour.getKeys(), "testKey"); - EXPECT_EQ(messageFour.getFlag(), 0); - - MQMessage messageFive("test", "tagTest", "testKey", 1, "testBody", 2); - EXPECT_EQ(messageFive.getTopic(), "test"); - EXPECT_EQ(messageFive.getBody(), "testBody"); - EXPECT_EQ(messageFive.getTags(), "tagTest"); - EXPECT_EQ(messageFive.getKeys(), "testKey"); - EXPECT_EQ(messageFive.getFlag(), 1); - - MQMessage messageSix(messageFive); - EXPECT_EQ(messageSix.getTopic(), "test"); - EXPECT_EQ(messageSix.getBody(), "testBody"); - EXPECT_EQ(messageSix.getTags(), "tagTest"); - EXPECT_EQ(messageSix.getKeys(), "testKey"); - EXPECT_EQ(messageSix.getFlag(), 1); + MQMessage messageOne; + EXPECT_EQ(messageOne.getTopic(), ""); + EXPECT_EQ(messageOne.getBody(), ""); + EXPECT_EQ(messageOne.getTags(), ""); + EXPECT_EQ(messageOne.getFlag(), 0); + + MQMessage messageTwo("test", "testBody"); + EXPECT_EQ(messageTwo.getTopic(), "test"); + EXPECT_EQ(messageTwo.getBody(), "testBody"); + EXPECT_EQ(messageTwo.getTags(), ""); + EXPECT_EQ(messageTwo.getFlag(), 0); + + MQMessage messageThree("test", "tagTest", "testBody"); + EXPECT_EQ(messageThree.getTopic(), "test"); + EXPECT_EQ(messageThree.getBody(), "testBody"); + EXPECT_EQ(messageThree.getTags(), "tagTest"); + EXPECT_EQ(messageThree.getFlag(), 0); + + MQMessage messageFour("test", "tagTest", "testKey", "testBody"); + EXPECT_EQ(messageFour.getTopic(), "test"); + EXPECT_EQ(messageFour.getBody(), "testBody"); + EXPECT_EQ(messageFour.getTags(), "tagTest"); + EXPECT_EQ(messageFour.getKeys(), "testKey"); + EXPECT_EQ(messageFour.getFlag(), 0); + + MQMessage messageFive("test", "tagTest", "testKey", 1, "testBody", 2); + EXPECT_EQ(messageFive.getTopic(), "test"); + EXPECT_EQ(messageFive.getBody(), "testBody"); + EXPECT_EQ(messageFive.getTags(), "tagTest"); + EXPECT_EQ(messageFive.getKeys(), "testKey"); + EXPECT_EQ(messageFive.getFlag(), 1); + + MQMessage messageSix(messageFive); + EXPECT_EQ(messageSix.getTopic(), "test"); + EXPECT_EQ(messageSix.getBody(), "testBody"); + EXPECT_EQ(messageSix.getTags(), "tagTest"); + EXPECT_EQ(messageSix.getKeys(), "testKey"); + EXPECT_EQ(messageSix.getFlag(), 1); } TEST(message, info) { - MQMessage message; - - EXPECT_EQ(message.getTopic(), ""); - message.setTopic("testTopic"); - EXPECT_EQ(message.getTopic(), "testTopic"); - char *topic = "testTopic"; - message.setTopic(topic, 5); - EXPECT_EQ(message.getTopic(), "testT"); - - EXPECT_EQ(message.getBody(), ""); - message.setBody("testBody"); - EXPECT_EQ(message.getBody(), "testBody"); - - char *body = "testBody"; - message.setBody(body, 5); - EXPECT_EQ(message.getBody(), "testB"); - - string tags(message.getTags()); - EXPECT_EQ(tags, ""); - EXPECT_EQ(message.getFlag(), 0); - message.setFlag(2); - EXPECT_EQ(message.getFlag(), 2); - - EXPECT_EQ(message.isWaitStoreMsgOK(), true); - message.setWaitStoreMsgOK(false); - EXPECT_EQ(message.isWaitStoreMsgOK(), false); - message.setWaitStoreMsgOK(true); - EXPECT_EQ(message.isWaitStoreMsgOK(), true); - - string keys(message.getTags()); - EXPECT_EQ(keys, ""); - message.setKeys("testKeys"); - EXPECT_EQ(message.getKeys(), "testKeys"); - - EXPECT_EQ(message.getDelayTimeLevel(), 0); - message.setDelayTimeLevel(1); - EXPECT_EQ(message.getDelayTimeLevel(), 1); - - message.setSysFlag(1); - EXPECT_EQ(message.getSysFlag(), 1); + MQMessage message; + + EXPECT_EQ(message.getTopic(), ""); + message.setTopic("testTopic"); + EXPECT_EQ(message.getTopic(), "testTopic"); + char* topic = "testTopic"; + message.setTopic(topic, 5); + EXPECT_EQ(message.getTopic(), "testT"); + + EXPECT_EQ(message.getBody(), ""); + message.setBody("testBody"); + EXPECT_EQ(message.getBody(), "testBody"); + + char* body = "testBody"; + message.setBody(body, 5); + EXPECT_EQ(message.getBody(), "testB"); + + string tags(message.getTags()); + EXPECT_EQ(tags, ""); + EXPECT_EQ(message.getFlag(), 0); + message.setFlag(2); + EXPECT_EQ(message.getFlag(), 2); + + EXPECT_EQ(message.isWaitStoreMsgOK(), true); + message.setWaitStoreMsgOK(false); + EXPECT_EQ(message.isWaitStoreMsgOK(), false); + message.setWaitStoreMsgOK(true); + EXPECT_EQ(message.isWaitStoreMsgOK(), true); + + string keys(message.getTags()); + EXPECT_EQ(keys, ""); + message.setKeys("testKeys"); + EXPECT_EQ(message.getKeys(), "testKeys"); + + EXPECT_EQ(message.getDelayTimeLevel(), 0); + message.setDelayTimeLevel(1); + EXPECT_EQ(message.getDelayTimeLevel(), 1); + + message.setSysFlag(1); + EXPECT_EQ(message.getSysFlag(), 1); } TEST(message, properties) { - MQMessage message; - EXPECT_EQ(message.getProperties().size(), 1); - EXPECT_STREQ(message.getProperty(MQMessage::PROPERTY_TRANSACTION_PREPARED).c_str(), ""); - - message.setProperty(MQMessage::PROPERTY_TRANSACTION_PREPARED, "true"); - EXPECT_EQ(message.getProperties().size(), 2); - EXPECT_EQ(message.getSysFlag(), 4); - EXPECT_EQ(message.getProperty(MQMessage::PROPERTY_TRANSACTION_PREPARED), "true"); - - message.setProperty(MQMessage::PROPERTY_TRANSACTION_PREPARED, "false"); - EXPECT_EQ(message.getProperties().size(), 2); - EXPECT_EQ(message.getSysFlag(), 0); - EXPECT_EQ(message.getProperty(MQMessage::PROPERTY_TRANSACTION_PREPARED), "false"); - - map newProperties; - - newProperties[MQMessage::PROPERTY_TRANSACTION_PREPARED] = "true"; - message.setProperties(newProperties); - EXPECT_EQ(message.getSysFlag(), 4); - EXPECT_EQ(message.getProperty(MQMessage::PROPERTY_TRANSACTION_PREPARED), "true"); - - newProperties[MQMessage::PROPERTY_TRANSACTION_PREPARED] = "false"; - message.setProperties(newProperties); - EXPECT_EQ(message.getSysFlag(), 0); - EXPECT_EQ(message.getProperty(MQMessage::PROPERTY_TRANSACTION_PREPARED), "false"); + MQMessage message; + EXPECT_EQ(message.getProperties().size(), 1); + EXPECT_STREQ(message.getProperty(MQMessage::PROPERTY_TRANSACTION_PREPARED).c_str(), ""); + + message.setProperty(MQMessage::PROPERTY_TRANSACTION_PREPARED, "true"); + EXPECT_EQ(message.getProperties().size(), 2); + EXPECT_EQ(message.getSysFlag(), 4); + EXPECT_EQ(message.getProperty(MQMessage::PROPERTY_TRANSACTION_PREPARED), "true"); + + message.setProperty(MQMessage::PROPERTY_TRANSACTION_PREPARED, "false"); + EXPECT_EQ(message.getProperties().size(), 2); + EXPECT_EQ(message.getSysFlag(), 0); + EXPECT_EQ(message.getProperty(MQMessage::PROPERTY_TRANSACTION_PREPARED), "false"); + + map newProperties; + + newProperties[MQMessage::PROPERTY_TRANSACTION_PREPARED] = "true"; + message.setProperties(newProperties); + EXPECT_EQ(message.getSysFlag(), 4); + EXPECT_EQ(message.getProperty(MQMessage::PROPERTY_TRANSACTION_PREPARED), "true"); + + newProperties[MQMessage::PROPERTY_TRANSACTION_PREPARED] = "false"; + message.setProperties(newProperties); + EXPECT_EQ(message.getSysFlag(), 0); + EXPECT_EQ(message.getProperty(MQMessage::PROPERTY_TRANSACTION_PREPARED), "false"); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(filter) = "message.info"; - int itestts = RUN_ALL_TESTS(); - return itestts; + testing::GTEST_FLAG(filter) = "message.info"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/protocol/CommandHeaderTest.cpp b/test/src/protocol/CommandHeaderTest.cpp index 7439c45e1..ee19e0e21 100644 --- a/test/src/protocol/CommandHeaderTest.cpp +++ b/test/src/protocol/CommandHeaderTest.cpp @@ -72,124 +72,123 @@ using rocketmq::ViewMessageRequestHeader; TEST(commandHeader, ConsumerSendMsgBackRequestHeader) {} TEST(commandHeader, GetConsumerListByGroupResponseBody) { - Value value; - value[0] = "body"; - value[1] = 1; + Value value; + value[0] = "body"; + value[1] = 1; - Value root; - root["consumerIdList"] = value; + Value root; + root["consumerIdList"] = value; - FastWriter writer; - string data = writer.write(root); + FastWriter writer; + string data = writer.write(root); - MemoryBlock *mem = new MemoryBlock(data.c_str(), data.size()); - vector cids; - GetConsumerListByGroupResponseBody::Decode(mem, cids); + MemoryBlock* mem = new MemoryBlock(data.c_str(), data.size()); + vector cids; + GetConsumerListByGroupResponseBody::Decode(mem, cids); - EXPECT_EQ(cids.size(), 1); + EXPECT_EQ(cids.size(), 1); - delete mem; + delete mem; } TEST(commandHeader, ResetOffsetRequestHeader) { - ResetOffsetRequestHeader header; - - header.setTopic("testTopic"); - EXPECT_EQ(header.getTopic(), "testTopic"); - - header.setGroup("testGroup"); - EXPECT_EQ(header.getGroup(), "testGroup"); - - header.setTimeStamp(123); - EXPECT_EQ(header.getTimeStamp(), 123); - - header.setForceFlag(true); - EXPECT_TRUE(header.getForceFlag()); - - Value value; - value["isForce"] = "false"; - shared_ptr headersh( - static_cast(ResetOffsetRequestHeader::Decode(value))); - EXPECT_EQ(headersh->getTopic(), ""); - EXPECT_EQ(headersh->getGroup(), ""); - // EXPECT_EQ(headersh->getTimeStamp(), 0); - EXPECT_FALSE(headersh->getForceFlag()); - value["topic"] = "testTopic"; - headersh.reset(static_cast(ResetOffsetRequestHeader::Decode(value))); - EXPECT_EQ(headersh->getTopic(), "testTopic"); - EXPECT_EQ(headersh->getGroup(), ""); - // EXPECT_EQ(headersh->getTimeStamp(), 0); - EXPECT_FALSE(headersh->getForceFlag()); - - value["topic"] = "testTopic"; - value["group"] = "testGroup"; - headersh.reset(static_cast(ResetOffsetRequestHeader::Decode(value))); - EXPECT_EQ(headersh->getTopic(), "testTopic"); - EXPECT_EQ(headersh->getGroup(), "testGroup"); - // EXPECT_EQ(headersh->getTimeStamp(), 0); - EXPECT_FALSE(headersh->getForceFlag()); - - value["topic"] = "testTopic"; - value["group"] = "testGroup"; - value["timestamp"] = "123"; - headersh.reset(static_cast(ResetOffsetRequestHeader::Decode(value))); - EXPECT_EQ(headersh->getTopic(), "testTopic"); - EXPECT_EQ(headersh->getGroup(), "testGroup"); - EXPECT_EQ(headersh->getTimeStamp(), 123); - EXPECT_FALSE(headersh->getForceFlag()); - - value["topic"] = "testTopic"; - value["group"] = "testGroup"; - value["timestamp"] = "123"; - value["isForce"] = "1"; - headersh.reset(static_cast(ResetOffsetRequestHeader::Decode(value))); - EXPECT_EQ(headersh->getTopic(), "testTopic"); - EXPECT_EQ(headersh->getGroup(), "testGroup"); - EXPECT_EQ(headersh->getTimeStamp(), 123); - EXPECT_TRUE(headersh->getForceFlag()); + ResetOffsetRequestHeader header; + + header.setTopic("testTopic"); + EXPECT_EQ(header.getTopic(), "testTopic"); + + header.setGroup("testGroup"); + EXPECT_EQ(header.getGroup(), "testGroup"); + + header.setTimeStamp(123); + EXPECT_EQ(header.getTimeStamp(), 123); + + header.setForceFlag(true); + EXPECT_TRUE(header.getForceFlag()); + + Value value; + value["isForce"] = "false"; + shared_ptr headersh( + static_cast(ResetOffsetRequestHeader::Decode(value))); + EXPECT_EQ(headersh->getTopic(), ""); + EXPECT_EQ(headersh->getGroup(), ""); + // EXPECT_EQ(headersh->getTimeStamp(), 0); + EXPECT_FALSE(headersh->getForceFlag()); + value["topic"] = "testTopic"; + headersh.reset(static_cast(ResetOffsetRequestHeader::Decode(value))); + EXPECT_EQ(headersh->getTopic(), "testTopic"); + EXPECT_EQ(headersh->getGroup(), ""); + // EXPECT_EQ(headersh->getTimeStamp(), 0); + EXPECT_FALSE(headersh->getForceFlag()); + + value["topic"] = "testTopic"; + value["group"] = "testGroup"; + headersh.reset(static_cast(ResetOffsetRequestHeader::Decode(value))); + EXPECT_EQ(headersh->getTopic(), "testTopic"); + EXPECT_EQ(headersh->getGroup(), "testGroup"); + // EXPECT_EQ(headersh->getTimeStamp(), 0); + EXPECT_FALSE(headersh->getForceFlag()); + + value["topic"] = "testTopic"; + value["group"] = "testGroup"; + value["timestamp"] = "123"; + headersh.reset(static_cast(ResetOffsetRequestHeader::Decode(value))); + EXPECT_EQ(headersh->getTopic(), "testTopic"); + EXPECT_EQ(headersh->getGroup(), "testGroup"); + EXPECT_EQ(headersh->getTimeStamp(), 123); + EXPECT_FALSE(headersh->getForceFlag()); + + value["topic"] = "testTopic"; + value["group"] = "testGroup"; + value["timestamp"] = "123"; + value["isForce"] = "1"; + headersh.reset(static_cast(ResetOffsetRequestHeader::Decode(value))); + EXPECT_EQ(headersh->getTopic(), "testTopic"); + EXPECT_EQ(headersh->getGroup(), "testGroup"); + EXPECT_EQ(headersh->getTimeStamp(), 123); + EXPECT_TRUE(headersh->getForceFlag()); } TEST(commandHeader, GetConsumerRunningInfoRequestHeader) { - GetConsumerRunningInfoRequestHeader header; - header.setClientId("testClientId"); - header.setConsumerGroup("testConsumer"); - header.setJstackEnable(true); - - map requestMap; - header.SetDeclaredFieldOfCommandHeader(requestMap); - EXPECT_EQ(requestMap["clientId"], "testClientId"); - EXPECT_EQ(requestMap["consumerGroup"], "testConsumer"); - EXPECT_EQ(requestMap["jstackEnable"], "1"); - - Value outData; - header.Encode(outData); - EXPECT_EQ(outData["clientId"], "testClientId"); - EXPECT_EQ(outData["consumerGroup"], "testConsumer"); - EXPECT_TRUE(outData["jstackEnable"].asBool()); - - shared_ptr decodeHeader( - static_cast(GetConsumerRunningInfoRequestHeader::Decode(outData))); - EXPECT_EQ(decodeHeader->getClientId(), "testClientId"); - EXPECT_EQ(decodeHeader->getConsumerGroup(), "testConsumer"); - EXPECT_TRUE(decodeHeader->isJstackEnable()); + GetConsumerRunningInfoRequestHeader header; + header.setClientId("testClientId"); + header.setConsumerGroup("testConsumer"); + header.setJstackEnable(true); + + map requestMap; + header.SetDeclaredFieldOfCommandHeader(requestMap); + EXPECT_EQ(requestMap["clientId"], "testClientId"); + EXPECT_EQ(requestMap["consumerGroup"], "testConsumer"); + EXPECT_EQ(requestMap["jstackEnable"], "1"); + + Value outData; + header.Encode(outData); + EXPECT_EQ(outData["clientId"], "testClientId"); + EXPECT_EQ(outData["consumerGroup"], "testConsumer"); + EXPECT_TRUE(outData["jstackEnable"].asBool()); + + shared_ptr decodeHeader( + static_cast(GetConsumerRunningInfoRequestHeader::Decode(outData))); + EXPECT_EQ(decodeHeader->getClientId(), "testClientId"); + EXPECT_EQ(decodeHeader->getConsumerGroup(), "testConsumer"); + EXPECT_TRUE(decodeHeader->isJstackEnable()); } TEST(commandHeader, NotifyConsumerIdsChangedRequestHeader) { - Json::Value ext; - shared_ptr header( - static_cast(NotifyConsumerIdsChangedRequestHeader::Decode(ext))); - EXPECT_EQ(header->getGroup(), ""); - - ext["consumerGroup"] = "testGroup"; - header.reset( - static_cast(NotifyConsumerIdsChangedRequestHeader::Decode(ext))); - EXPECT_EQ(header->getGroup(), "testGroup"); + Json::Value ext; + shared_ptr header( + static_cast(NotifyConsumerIdsChangedRequestHeader::Decode(ext))); + EXPECT_EQ(header->getGroup(), ""); + + ext["consumerGroup"] = "testGroup"; + header.reset(static_cast(NotifyConsumerIdsChangedRequestHeader::Decode(ext))); + EXPECT_EQ(header->getGroup(), "testGroup"); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "commandHeader.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "commandHeader.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/protocol/ConsumerRunningInfoTest.cpp b/test/src/protocol/ConsumerRunningInfoTest.cpp index ef46eb1b1..fbea4b289 100644 --- a/test/src/protocol/ConsumerRunningInfoTest.cpp +++ b/test/src/protocol/ConsumerRunningInfoTest.cpp @@ -45,80 +45,80 @@ using rocketmq::ProcessQueueInfo; using rocketmq::SubscriptionData; TEST(consumerRunningInfo, init) { - ConsumerRunningInfo consumerRunningInfo; - consumerRunningInfo.setJstack("jstack"); - EXPECT_EQ(consumerRunningInfo.getJstack(), "jstack"); - - EXPECT_TRUE(consumerRunningInfo.getProperties().empty()); - - consumerRunningInfo.setProperty("testKey", "testValue"); - map properties = consumerRunningInfo.getProperties(); - EXPECT_EQ(properties["testKey"], "testValue"); - - consumerRunningInfo.setProperties(map()); - EXPECT_TRUE(consumerRunningInfo.getProperties().empty()); - - EXPECT_TRUE(consumerRunningInfo.getSubscriptionSet().empty()); - - vector subscriptionSet; - subscriptionSet.push_back(SubscriptionData()); - - consumerRunningInfo.setSubscriptionSet(subscriptionSet); - EXPECT_EQ(consumerRunningInfo.getSubscriptionSet().size(), 1); - - EXPECT_TRUE(consumerRunningInfo.getMqTable().empty()); - - MessageQueue messageQueue("testTopic", "testBroker", 3); - ProcessQueueInfo processQueueInfo; - processQueueInfo.commitOffset = 1024; - consumerRunningInfo.setMqTable(messageQueue, processQueueInfo); - map mqTable = consumerRunningInfo.getMqTable(); - EXPECT_EQ(mqTable[messageQueue].commitOffset, processQueueInfo.commitOffset); - - // encode start - consumerRunningInfo.setProperty(ConsumerRunningInfo::PROP_NAMESERVER_ADDR, "127.0.0.1:9876"); - consumerRunningInfo.setProperty(ConsumerRunningInfo::PROP_THREADPOOL_CORE_SIZE, "core_size"); - consumerRunningInfo.setProperty(ConsumerRunningInfo::PROP_CONSUME_ORDERLY, "consume_orderly"); - consumerRunningInfo.setProperty(ConsumerRunningInfo::PROP_CONSUME_TYPE, "consume_type"); - consumerRunningInfo.setProperty(ConsumerRunningInfo::PROP_CLIENT_VERSION, "client_version"); - consumerRunningInfo.setProperty(ConsumerRunningInfo::PROP_CONSUMER_START_TIMESTAMP, "127"); - // TODO - /* string outstr = consumerRunningInfo.encode(); - std::cout<< outstr; - Value root; - Reader reader; - reader.parse(outstr.c_str(), root); - - EXPECT_EQ(root["jstack"].asString() , "jstack"); - - Json::Value outData = root["properties"]; - EXPECT_EQ(outData[ConsumerRunningInfo::PROP_NAMESERVER_ADDR].asString(),"127.0.0.1:9876"); - EXPECT_EQ( - outData[ConsumerRunningInfo::PROP_THREADPOOL_CORE_SIZE].asString(), - "core_size"); - EXPECT_EQ(outData[ConsumerRunningInfo::PROP_CONSUME_ORDERLY].asString(), - "consume_orderly"); - EXPECT_EQ(outData[ConsumerRunningInfo::PROP_CONSUME_TYPE].asString(), - "consume_type"); - EXPECT_EQ(outData[ConsumerRunningInfo::PROP_CLIENT_VERSION].asString(), - "client_version"); - EXPECT_EQ( - outData[ConsumerRunningInfo::PROP_CONSUMER_START_TIMESTAMP].asString(), - "127"); - - Json::Value subscriptionSetJson = root["subscriptionSet"]; - EXPECT_EQ(subscriptionSetJson[0], subscriptionSet[0].toJson()); - - Json::Value mqTableJson = root["mqTable"]; - EXPECT_EQ(mqTableJson[messageQueue.toJson().toStyledString()].asString(), - processQueueInfo.toJson().toStyledString()); - */ + ConsumerRunningInfo consumerRunningInfo; + consumerRunningInfo.setJstack("jstack"); + EXPECT_EQ(consumerRunningInfo.getJstack(), "jstack"); + + EXPECT_TRUE(consumerRunningInfo.getProperties().empty()); + + consumerRunningInfo.setProperty("testKey", "testValue"); + map properties = consumerRunningInfo.getProperties(); + EXPECT_EQ(properties["testKey"], "testValue"); + + consumerRunningInfo.setProperties(map()); + EXPECT_TRUE(consumerRunningInfo.getProperties().empty()); + + EXPECT_TRUE(consumerRunningInfo.getSubscriptionSet().empty()); + + vector subscriptionSet; + subscriptionSet.push_back(SubscriptionData()); + + consumerRunningInfo.setSubscriptionSet(subscriptionSet); + EXPECT_EQ(consumerRunningInfo.getSubscriptionSet().size(), 1); + + EXPECT_TRUE(consumerRunningInfo.getMqTable().empty()); + + MessageQueue messageQueue("testTopic", "testBroker", 3); + ProcessQueueInfo processQueueInfo; + processQueueInfo.commitOffset = 1024; + consumerRunningInfo.setMqTable(messageQueue, processQueueInfo); + map mqTable = consumerRunningInfo.getMqTable(); + EXPECT_EQ(mqTable[messageQueue].commitOffset, processQueueInfo.commitOffset); + + // encode start + consumerRunningInfo.setProperty(ConsumerRunningInfo::PROP_NAMESERVER_ADDR, "127.0.0.1:9876"); + consumerRunningInfo.setProperty(ConsumerRunningInfo::PROP_THREADPOOL_CORE_SIZE, "core_size"); + consumerRunningInfo.setProperty(ConsumerRunningInfo::PROP_CONSUME_ORDERLY, "consume_orderly"); + consumerRunningInfo.setProperty(ConsumerRunningInfo::PROP_CONSUME_TYPE, "consume_type"); + consumerRunningInfo.setProperty(ConsumerRunningInfo::PROP_CLIENT_VERSION, "client_version"); + consumerRunningInfo.setProperty(ConsumerRunningInfo::PROP_CONSUMER_START_TIMESTAMP, "127"); + // TODO + /* string outstr = consumerRunningInfo.encode(); + std::cout<< outstr; + Value root; + Reader reader; + reader.parse(outstr.c_str(), root); + + EXPECT_EQ(root["jstack"].asString() , "jstack"); + + Json::Value outData = root["properties"]; + EXPECT_EQ(outData[ConsumerRunningInfo::PROP_NAMESERVER_ADDR].asString(),"127.0.0.1:9876"); + EXPECT_EQ( + outData[ConsumerRunningInfo::PROP_THREADPOOL_CORE_SIZE].asString(), + "core_size"); + EXPECT_EQ(outData[ConsumerRunningInfo::PROP_CONSUME_ORDERLY].asString(), + "consume_orderly"); + EXPECT_EQ(outData[ConsumerRunningInfo::PROP_CONSUME_TYPE].asString(), + "consume_type"); + EXPECT_EQ(outData[ConsumerRunningInfo::PROP_CLIENT_VERSION].asString(), + "client_version"); + EXPECT_EQ( + outData[ConsumerRunningInfo::PROP_CONSUMER_START_TIMESTAMP].asString(), + "127"); + + Json::Value subscriptionSetJson = root["subscriptionSet"]; + EXPECT_EQ(subscriptionSetJson[0], subscriptionSet[0].toJson()); + + Json::Value mqTableJson = root["mqTable"]; + EXPECT_EQ(mqTableJson[messageQueue.toJson().toStyledString()].asString(), + processQueueInfo.toJson().toStyledString()); +*/ } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "consumerRunningInfo.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "consumerRunningInfo.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/protocol/HeartbeatDataTest.cpp b/test/src/protocol/HeartbeatDataTest.cpp index 21feae161..c153ed58a 100644 --- a/test/src/protocol/HeartbeatDataTest.cpp +++ b/test/src/protocol/HeartbeatDataTest.cpp @@ -39,77 +39,77 @@ using rocketmq::ProducerData; using rocketmq::SubscriptionData; TEST(heartbeatData, ProducerData) { - ProducerData producerData; - producerData.groupName = "testGroup"; + ProducerData producerData; + producerData.groupName = "testGroup"; - Json::Value outJson = producerData.toJson(); - EXPECT_EQ(outJson["groupName"], "testGroup"); + Json::Value outJson = producerData.toJson(); + EXPECT_EQ(outJson["groupName"], "testGroup"); } TEST(heartbeatData, ConsumerData) { - ConsumerData consumerData; - consumerData.groupName = "testGroup"; - consumerData.consumeType = ConsumeType::CONSUME_ACTIVELY; - consumerData.messageModel = MessageModel::BROADCASTING; - consumerData.consumeFromWhere = ConsumeFromWhere::CONSUME_FROM_TIMESTAMP; + ConsumerData consumerData; + consumerData.groupName = "testGroup"; + consumerData.consumeType = ConsumeType::CONSUME_ACTIVELY; + consumerData.messageModel = MessageModel::BROADCASTING; + consumerData.consumeFromWhere = ConsumeFromWhere::CONSUME_FROM_TIMESTAMP; - vector subs; - subs.push_back(SubscriptionData("testTopic", "sub")); + vector subs; + subs.push_back(SubscriptionData("testTopic", "sub")); - consumerData.subscriptionDataSet = subs; + consumerData.subscriptionDataSet = subs; - Json::Value outJson = consumerData.toJson(); + Json::Value outJson = consumerData.toJson(); - EXPECT_EQ(outJson["groupName"], "testGroup"); + EXPECT_EQ(outJson["groupName"], "testGroup"); - EXPECT_EQ(outJson["consumeType"].asInt(), ConsumeType::CONSUME_ACTIVELY); - EXPECT_EQ(outJson["messageModel"].asInt(), MessageModel::BROADCASTING); - EXPECT_EQ(outJson["consumeFromWhere"].asInt(), ConsumeFromWhere::CONSUME_FROM_TIMESTAMP); + EXPECT_EQ(outJson["consumeType"].asInt(), ConsumeType::CONSUME_ACTIVELY); + EXPECT_EQ(outJson["messageModel"].asInt(), MessageModel::BROADCASTING); + EXPECT_EQ(outJson["consumeFromWhere"].asInt(), ConsumeFromWhere::CONSUME_FROM_TIMESTAMP); - Json::Value subsValue = outJson["subscriptionDataSet"]; - EXPECT_EQ(subsValue[0]["topic"], "testTopic"); - EXPECT_EQ(subsValue[0]["subString"], "sub"); + Json::Value subsValue = outJson["subscriptionDataSet"]; + EXPECT_EQ(subsValue[0]["topic"], "testTopic"); + EXPECT_EQ(subsValue[0]["subString"], "sub"); } TEST(heartbeatData, HeartbeatData) { - HeartbeatData heartbeatData; - heartbeatData.setClientID("testClientId"); + HeartbeatData heartbeatData; + heartbeatData.setClientID("testClientId"); - ProducerData producerData; - producerData.groupName = "testGroup"; + ProducerData producerData; + producerData.groupName = "testGroup"; - EXPECT_TRUE(heartbeatData.isProducerDataSetEmpty()); - heartbeatData.insertDataToProducerDataSet(producerData); - EXPECT_FALSE(heartbeatData.isProducerDataSetEmpty()); + EXPECT_TRUE(heartbeatData.isProducerDataSetEmpty()); + heartbeatData.insertDataToProducerDataSet(producerData); + EXPECT_FALSE(heartbeatData.isProducerDataSetEmpty()); - ConsumerData consumerData; - consumerData.groupName = "testGroup"; - consumerData.consumeType = ConsumeType::CONSUME_ACTIVELY; - consumerData.messageModel = MessageModel::BROADCASTING; - consumerData.consumeFromWhere = ConsumeFromWhere::CONSUME_FROM_TIMESTAMP; + ConsumerData consumerData; + consumerData.groupName = "testGroup"; + consumerData.consumeType = ConsumeType::CONSUME_ACTIVELY; + consumerData.messageModel = MessageModel::BROADCASTING; + consumerData.consumeFromWhere = ConsumeFromWhere::CONSUME_FROM_TIMESTAMP; - vector subs; - subs.push_back(SubscriptionData("testTopic", "sub")); + vector subs; + subs.push_back(SubscriptionData("testTopic", "sub")); - consumerData.subscriptionDataSet = subs; - EXPECT_TRUE(heartbeatData.isConsumerDataSetEmpty()); - heartbeatData.insertDataToConsumerDataSet(consumerData); - EXPECT_FALSE(heartbeatData.isConsumerDataSetEmpty()); + consumerData.subscriptionDataSet = subs; + EXPECT_TRUE(heartbeatData.isConsumerDataSetEmpty()); + heartbeatData.insertDataToConsumerDataSet(consumerData); + EXPECT_FALSE(heartbeatData.isConsumerDataSetEmpty()); - string outData; - heartbeatData.Encode(outData); + string outData; + heartbeatData.Encode(outData); - Json::Value root; - Json::Reader reader; - reader.parse(outData, root); + Json::Value root; + Json::Reader reader; + reader.parse(outData, root); - EXPECT_EQ(root["clientID"], "testClientId"); + EXPECT_EQ(root["clientID"], "testClientId"); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "heartbeatData.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "heartbeatData.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/protocol/KVTableTest.cpp b/test/src/protocol/KVTableTest.cpp index 312d35c7f..b6fd50493 100644 --- a/test/src/protocol/KVTableTest.cpp +++ b/test/src/protocol/KVTableTest.cpp @@ -32,20 +32,20 @@ using testing::Return; using rocketmq::KVTable; TEST(KVTable, init) { - KVTable table; + KVTable table; - EXPECT_EQ(table.getTable().size(), 0); + EXPECT_EQ(table.getTable().size(), 0); - map maps; - maps["string"] = "string"; - table.setTable(maps); - EXPECT_EQ(table.getTable().size(), 1); + map maps; + maps["string"] = "string"; + table.setTable(maps); + EXPECT_EQ(table.getTable().size(), 1); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "KVTable.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "KVTable.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/protocol/LockBatchBodyTest.cpp b/test/src/protocol/LockBatchBodyTest.cpp index fec3b273a..9f8f3009f 100644 --- a/test/src/protocol/LockBatchBodyTest.cpp +++ b/test/src/protocol/LockBatchBodyTest.cpp @@ -37,67 +37,67 @@ using rocketmq::MQMessageQueue; using rocketmq::UnlockBatchRequestBody; TEST(lockBatchBody, LockBatchRequestBody) { - LockBatchRequestBody lockBatchRequestBody; + LockBatchRequestBody lockBatchRequestBody; - lockBatchRequestBody.setClientId("testClientId"); - EXPECT_EQ(lockBatchRequestBody.getClientId(), "testClientId"); + lockBatchRequestBody.setClientId("testClientId"); + EXPECT_EQ(lockBatchRequestBody.getClientId(), "testClientId"); - lockBatchRequestBody.setConsumerGroup("testGroup"); - EXPECT_EQ(lockBatchRequestBody.getConsumerGroup(), "testGroup"); + lockBatchRequestBody.setConsumerGroup("testGroup"); + EXPECT_EQ(lockBatchRequestBody.getConsumerGroup(), "testGroup"); - vector vectorMessageQueue; - vectorMessageQueue.push_back(MQMessageQueue()); - vectorMessageQueue.push_back(MQMessageQueue()); + vector vectorMessageQueue; + vectorMessageQueue.push_back(MQMessageQueue()); + vectorMessageQueue.push_back(MQMessageQueue()); - lockBatchRequestBody.setMqSet(vectorMessageQueue); - EXPECT_EQ(lockBatchRequestBody.getMqSet(), vectorMessageQueue); + lockBatchRequestBody.setMqSet(vectorMessageQueue); + EXPECT_EQ(lockBatchRequestBody.getMqSet(), vectorMessageQueue); - Json::Value outJson = lockBatchRequestBody.toJson(MQMessageQueue("topicTest", "testBroker", 10)); - EXPECT_EQ(outJson["topic"], "topicTest"); - EXPECT_EQ(outJson["brokerName"], "testBroker"); - EXPECT_EQ(outJson["queueId"], 10); + Json::Value outJson = lockBatchRequestBody.toJson(MQMessageQueue("topicTest", "testBroker", 10)); + EXPECT_EQ(outJson["topic"], "topicTest"); + EXPECT_EQ(outJson["brokerName"], "testBroker"); + EXPECT_EQ(outJson["queueId"], 10); - string outData; - lockBatchRequestBody.Encode(outData); + string outData; + lockBatchRequestBody.Encode(outData); - Json::Value root; - Json::Reader reader; - reader.parse(outData, root); - EXPECT_EQ(root["clientId"], "testClientId"); - EXPECT_EQ(root["consumerGroup"], "testGroup"); + Json::Value root; + Json::Reader reader; + reader.parse(outData, root); + EXPECT_EQ(root["clientId"], "testClientId"); + EXPECT_EQ(root["consumerGroup"], "testGroup"); } TEST(lockBatchBody, UnlockBatchRequestBody) {} TEST(lockBatchBody, LockBatchResponseBody) { - Json::Value root; - Json::Value mqs; + Json::Value root; + Json::Value mqs; - Json::Value mq; - mq["topic"] = "testTopic"; - mq["brokerName"] = "testBroker"; - mq["queueId"] = 1; - mqs[0] = mq; - root["lockOKMQSet"] = mqs; + Json::Value mq; + mq["topic"] = "testTopic"; + mq["brokerName"] = "testBroker"; + mq["queueId"] = 1; + mqs[0] = mq; + root["lockOKMQSet"] = mqs; - Json::FastWriter fastwrite; - string outData = fastwrite.write(root); + Json::FastWriter fastwrite; + string outData = fastwrite.write(root); - MemoryBlock *mem = new MemoryBlock(outData.c_str(), outData.size()); + MemoryBlock* mem = new MemoryBlock(outData.c_str(), outData.size()); - LockBatchResponseBody lockBatchResponseBody; + LockBatchResponseBody lockBatchResponseBody; - vector messageQueues; - LockBatchResponseBody::Decode(mem, messageQueues); + vector messageQueues; + LockBatchResponseBody::Decode(mem, messageQueues); - MQMessageQueue messageQueue("testTopic", "testBroker", 1); - EXPECT_EQ(messageQueue, messageQueues[0]); + MQMessageQueue messageQueue("testTopic", "testBroker", 1); + EXPECT_EQ(messageQueue, messageQueues[0]); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "lockBatchBody.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "lockBatchBody.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/protocol/MessageQueueTest.cpp b/test/src/protocol/MessageQueueTest.cpp index 68d137e4b..eb943d206 100644 --- a/test/src/protocol/MessageQueueTest.cpp +++ b/test/src/protocol/MessageQueueTest.cpp @@ -26,64 +26,64 @@ using testing::Return; using rocketmq::MessageQueue; TEST(messageExt, init) { - MessageQueue messageQueue; - EXPECT_EQ(messageQueue.getQueueId(), -1); - - MessageQueue twoMessageQueue("testTopic", "testBroker", 1); - EXPECT_EQ(twoMessageQueue.getQueueId(), 1); - EXPECT_EQ(twoMessageQueue.getTopic(), "testTopic"); - EXPECT_EQ(twoMessageQueue.getBrokerName(), "testBroker"); - - MessageQueue threeMessageQueue(twoMessageQueue); - EXPECT_EQ(threeMessageQueue.getQueueId(), 1); - EXPECT_EQ(threeMessageQueue.getTopic(), "testTopic"); - EXPECT_EQ(threeMessageQueue.getBrokerName(), "testBroker"); - - Json::Value outJson = twoMessageQueue.toJson(); - EXPECT_EQ(outJson["queueId"], 1); - EXPECT_EQ(outJson["topic"], "testTopic"); - EXPECT_EQ(outJson["brokerName"], "testBroker"); - - MessageQueue fiveMessageQueue = threeMessageQueue; - EXPECT_EQ(fiveMessageQueue.getQueueId(), 1); - EXPECT_EQ(fiveMessageQueue.getTopic(), "testTopic"); - EXPECT_EQ(fiveMessageQueue.getBrokerName(), "testBroker"); + MessageQueue messageQueue; + EXPECT_EQ(messageQueue.getQueueId(), -1); + + MessageQueue twoMessageQueue("testTopic", "testBroker", 1); + EXPECT_EQ(twoMessageQueue.getQueueId(), 1); + EXPECT_EQ(twoMessageQueue.getTopic(), "testTopic"); + EXPECT_EQ(twoMessageQueue.getBrokerName(), "testBroker"); + + MessageQueue threeMessageQueue(twoMessageQueue); + EXPECT_EQ(threeMessageQueue.getQueueId(), 1); + EXPECT_EQ(threeMessageQueue.getTopic(), "testTopic"); + EXPECT_EQ(threeMessageQueue.getBrokerName(), "testBroker"); + + Json::Value outJson = twoMessageQueue.toJson(); + EXPECT_EQ(outJson["queueId"], 1); + EXPECT_EQ(outJson["topic"], "testTopic"); + EXPECT_EQ(outJson["brokerName"], "testBroker"); + + MessageQueue fiveMessageQueue = threeMessageQueue; + EXPECT_EQ(fiveMessageQueue.getQueueId(), 1); + EXPECT_EQ(fiveMessageQueue.getTopic(), "testTopic"); + EXPECT_EQ(fiveMessageQueue.getBrokerName(), "testBroker"); } TEST(messageExt, info) { - MessageQueue messageQueue; + MessageQueue messageQueue; - messageQueue.setQueueId(11); - EXPECT_EQ(messageQueue.getQueueId(), 11); + messageQueue.setQueueId(11); + EXPECT_EQ(messageQueue.getQueueId(), 11); - messageQueue.setTopic("testTopic"); - EXPECT_EQ(messageQueue.getTopic(), "testTopic"); + messageQueue.setTopic("testTopic"); + EXPECT_EQ(messageQueue.getTopic(), "testTopic"); - messageQueue.setBrokerName("testBroker"); - EXPECT_EQ(messageQueue.getBrokerName(), "testBroker"); + messageQueue.setBrokerName("testBroker"); + EXPECT_EQ(messageQueue.getBrokerName(), "testBroker"); } TEST(messageExt, operators) { - MessageQueue messageQueue; - EXPECT_TRUE(messageQueue == messageQueue); + MessageQueue messageQueue; + EXPECT_TRUE(messageQueue == messageQueue); - MessageQueue twoMessageQueue; - EXPECT_TRUE(messageQueue == twoMessageQueue); + MessageQueue twoMessageQueue; + EXPECT_TRUE(messageQueue == twoMessageQueue); - twoMessageQueue.setTopic("testTopic"); - EXPECT_FALSE(messageQueue == twoMessageQueue); + twoMessageQueue.setTopic("testTopic"); + EXPECT_FALSE(messageQueue == twoMessageQueue); - messageQueue.setQueueId(11); - EXPECT_FALSE(messageQueue == twoMessageQueue); + messageQueue.setQueueId(11); + EXPECT_FALSE(messageQueue == twoMessageQueue); - messageQueue.setBrokerName("testBroker"); - EXPECT_FALSE(messageQueue == twoMessageQueue); + messageQueue.setBrokerName("testBroker"); + EXPECT_FALSE(messageQueue == twoMessageQueue); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "messageExt.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "messageExt.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/protocol/ProcessQueueInfoTest.cpp b/test/src/protocol/ProcessQueueInfoTest.cpp index 9f00d5101..bb5e7141e 100644 --- a/test/src/protocol/ProcessQueueInfoTest.cpp +++ b/test/src/protocol/ProcessQueueInfoTest.cpp @@ -28,51 +28,51 @@ using rocketmq::MQMessageExt; using rocketmq::ProcessQueueInfo; TEST(processQueueInfo, init) { - ProcessQueueInfo processQueueInfo; - EXPECT_EQ(processQueueInfo.commitOffset, 0); - EXPECT_EQ(processQueueInfo.cachedMsgMinOffset, 0); - EXPECT_EQ(processQueueInfo.cachedMsgMaxOffset, 0); - EXPECT_EQ(processQueueInfo.cachedMsgCount, 0); - EXPECT_EQ(processQueueInfo.transactionMsgMinOffset, 0); - EXPECT_EQ(processQueueInfo.transactionMsgMaxOffset, 0); - EXPECT_EQ(processQueueInfo.transactionMsgCount, 0); - EXPECT_EQ(processQueueInfo.locked, false); - EXPECT_EQ(processQueueInfo.tryUnlockTimes, 0); - EXPECT_EQ(processQueueInfo.lastLockTimestamp, 123); - EXPECT_EQ(processQueueInfo.droped, false); - EXPECT_EQ(processQueueInfo.lastPullTimestamp, 0); - EXPECT_EQ(processQueueInfo.lastConsumeTimestamp, 0); + ProcessQueueInfo processQueueInfo; + EXPECT_EQ(processQueueInfo.commitOffset, 0); + EXPECT_EQ(processQueueInfo.cachedMsgMinOffset, 0); + EXPECT_EQ(processQueueInfo.cachedMsgMaxOffset, 0); + EXPECT_EQ(processQueueInfo.cachedMsgCount, 0); + EXPECT_EQ(processQueueInfo.transactionMsgMinOffset, 0); + EXPECT_EQ(processQueueInfo.transactionMsgMaxOffset, 0); + EXPECT_EQ(processQueueInfo.transactionMsgCount, 0); + EXPECT_EQ(processQueueInfo.locked, false); + EXPECT_EQ(processQueueInfo.tryUnlockTimes, 0); + EXPECT_EQ(processQueueInfo.lastLockTimestamp, 123); + EXPECT_EQ(processQueueInfo.droped, false); + EXPECT_EQ(processQueueInfo.lastPullTimestamp, 0); + EXPECT_EQ(processQueueInfo.lastConsumeTimestamp, 0); - processQueueInfo.setLocked(true); - EXPECT_EQ(processQueueInfo.isLocked(), true); + processQueueInfo.setLocked(true); + EXPECT_EQ(processQueueInfo.isLocked(), true); - processQueueInfo.setDroped(true); - EXPECT_EQ(processQueueInfo.isDroped(), true); + processQueueInfo.setDroped(true); + EXPECT_EQ(processQueueInfo.isDroped(), true); - processQueueInfo.setCommitOffset(456); - EXPECT_EQ(processQueueInfo.getCommitOffset(), 456); + processQueueInfo.setCommitOffset(456); + EXPECT_EQ(processQueueInfo.getCommitOffset(), 456); - Json::Value outJson = processQueueInfo.toJson(); + Json::Value outJson = processQueueInfo.toJson(); - EXPECT_EQ(outJson["commitOffset"], "456"); - EXPECT_EQ(outJson["cachedMsgMinOffset"], "0"); - EXPECT_EQ(outJson["cachedMsgMaxOffset"], "0"); - EXPECT_EQ(outJson["cachedMsgCount"].asInt(), 0); - EXPECT_EQ(outJson["transactionMsgMinOffset"], "0"); - EXPECT_EQ(outJson["transactionMsgMaxOffset"], "0"); - EXPECT_EQ(outJson["transactionMsgCount"].asInt(), 0); - EXPECT_EQ(outJson["locked"].asBool(), true); - EXPECT_EQ(outJson["tryUnlockTimes"].asInt(), 0); - EXPECT_EQ(outJson["lastLockTimestamp"], "123"); - EXPECT_EQ(outJson["droped"].asBool(), true); - EXPECT_EQ(outJson["lastPullTimestamp"], "0"); - EXPECT_EQ(outJson["lastConsumeTimestamp"], "0"); + EXPECT_EQ(outJson["commitOffset"], "456"); + EXPECT_EQ(outJson["cachedMsgMinOffset"], "0"); + EXPECT_EQ(outJson["cachedMsgMaxOffset"], "0"); + EXPECT_EQ(outJson["cachedMsgCount"].asInt(), 0); + EXPECT_EQ(outJson["transactionMsgMinOffset"], "0"); + EXPECT_EQ(outJson["transactionMsgMaxOffset"], "0"); + EXPECT_EQ(outJson["transactionMsgCount"].asInt(), 0); + EXPECT_EQ(outJson["locked"].asBool(), true); + EXPECT_EQ(outJson["tryUnlockTimes"].asInt(), 0); + EXPECT_EQ(outJson["lastLockTimestamp"], "123"); + EXPECT_EQ(outJson["droped"].asBool(), true); + EXPECT_EQ(outJson["lastPullTimestamp"], "0"); + EXPECT_EQ(outJson["lastConsumeTimestamp"], "0"); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "processQueueInfo.init"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "processQueueInfo.init"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/protocol/RemotingCommandTest.cpp b/test/src/protocol/RemotingCommandTest.cpp index 36e8f1be5..ca28aa3f4 100644 --- a/test/src/protocol/RemotingCommandTest.cpp +++ b/test/src/protocol/RemotingCommandTest.cpp @@ -54,205 +54,205 @@ using rocketmq::SearchOffsetResponseHeader; 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); - EXPECT_EQ(twoRemotingCommand.getRemark(), ""); - EXPECT_EQ(twoRemotingCommand.getVersion(), MQVersion::s_CurrentVersion); - // EXPECT_EQ(twoRemotingCommand.getFlag() , 0); - EXPECT_EQ(twoRemotingCommand.getMsgBody(), ""); - EXPECT_TRUE(twoRemotingCommand.getCommandHeader() == nullptr); - - RemotingCommand threeRemotingCommand(13, new GetRouteInfoRequestHeader("topic")); - EXPECT_FALSE(threeRemotingCommand.getCommandHeader() == nullptr); - - RemotingCommand frouRemotingCommand(13, "CPP", MQVersion::s_CurrentVersion, 12, 3, "remark", - new GetRouteInfoRequestHeader("topic")); - EXPECT_EQ(frouRemotingCommand.getCode(), 13); - EXPECT_EQ(frouRemotingCommand.getOpaque(), 12); - EXPECT_EQ(frouRemotingCommand.getRemark(), "remark"); - EXPECT_EQ(frouRemotingCommand.getVersion(), MQVersion::s_CurrentVersion); - EXPECT_EQ(frouRemotingCommand.getFlag(), 3); - EXPECT_EQ(frouRemotingCommand.getMsgBody(), ""); - EXPECT_FALSE(frouRemotingCommand.getCommandHeader() == nullptr); - - RemotingCommand sixRemotingCommand(frouRemotingCommand); - EXPECT_EQ(sixRemotingCommand.getCode(), 13); - EXPECT_EQ(sixRemotingCommand.getOpaque(), 12); - EXPECT_EQ(sixRemotingCommand.getRemark(), "remark"); - EXPECT_EQ(sixRemotingCommand.getVersion(), MQVersion::s_CurrentVersion); - EXPECT_EQ(sixRemotingCommand.getFlag(), 3); - EXPECT_EQ(sixRemotingCommand.getMsgBody(), ""); - EXPECT_TRUE(sixRemotingCommand.getCommandHeader() == nullptr); - - RemotingCommand* sevenRemotingCommand = &sixRemotingCommand; - EXPECT_EQ(sevenRemotingCommand->getCode(), 13); - EXPECT_EQ(sevenRemotingCommand->getOpaque(), 12); - EXPECT_EQ(sevenRemotingCommand->getRemark(), "remark"); - EXPECT_EQ(sevenRemotingCommand->getVersion(), MQVersion::s_CurrentVersion); - EXPECT_EQ(sevenRemotingCommand->getFlag(), 3); - EXPECT_EQ(sevenRemotingCommand->getMsgBody(), ""); - EXPECT_TRUE(sevenRemotingCommand->getCommandHeader() == nullptr); + RemotingCommand remotingCommand; + EXPECT_EQ(remotingCommand.getCode(), 0); + + RemotingCommand twoRemotingCommand(13); + EXPECT_EQ(twoRemotingCommand.getCode(), 13); + EXPECT_EQ(twoRemotingCommand.getOpaque(), 0); + EXPECT_EQ(twoRemotingCommand.getRemark(), ""); + EXPECT_EQ(twoRemotingCommand.getVersion(), MQVersion::s_CurrentVersion); + // EXPECT_EQ(twoRemotingCommand.getFlag() , 0); + EXPECT_EQ(twoRemotingCommand.getMsgBody(), ""); + EXPECT_TRUE(twoRemotingCommand.getCommandHeader() == nullptr); + + RemotingCommand threeRemotingCommand(13, new GetRouteInfoRequestHeader("topic")); + EXPECT_FALSE(threeRemotingCommand.getCommandHeader() == nullptr); + + RemotingCommand frouRemotingCommand(13, "CPP", MQVersion::s_CurrentVersion, 12, 3, "remark", + new GetRouteInfoRequestHeader("topic")); + EXPECT_EQ(frouRemotingCommand.getCode(), 13); + EXPECT_EQ(frouRemotingCommand.getOpaque(), 12); + EXPECT_EQ(frouRemotingCommand.getRemark(), "remark"); + EXPECT_EQ(frouRemotingCommand.getVersion(), MQVersion::s_CurrentVersion); + EXPECT_EQ(frouRemotingCommand.getFlag(), 3); + EXPECT_EQ(frouRemotingCommand.getMsgBody(), ""); + EXPECT_FALSE(frouRemotingCommand.getCommandHeader() == nullptr); + + RemotingCommand sixRemotingCommand(frouRemotingCommand); + EXPECT_EQ(sixRemotingCommand.getCode(), 13); + EXPECT_EQ(sixRemotingCommand.getOpaque(), 12); + EXPECT_EQ(sixRemotingCommand.getRemark(), "remark"); + EXPECT_EQ(sixRemotingCommand.getVersion(), MQVersion::s_CurrentVersion); + EXPECT_EQ(sixRemotingCommand.getFlag(), 3); + EXPECT_EQ(sixRemotingCommand.getMsgBody(), ""); + EXPECT_TRUE(sixRemotingCommand.getCommandHeader() == nullptr); + + RemotingCommand* sevenRemotingCommand = &sixRemotingCommand; + EXPECT_EQ(sevenRemotingCommand->getCode(), 13); + EXPECT_EQ(sevenRemotingCommand->getOpaque(), 12); + EXPECT_EQ(sevenRemotingCommand->getRemark(), "remark"); + EXPECT_EQ(sevenRemotingCommand->getVersion(), MQVersion::s_CurrentVersion); + EXPECT_EQ(sevenRemotingCommand->getFlag(), 3); + EXPECT_EQ(sevenRemotingCommand->getMsgBody(), ""); + EXPECT_TRUE(sevenRemotingCommand->getCommandHeader() == nullptr); } TEST(remotingCommand, info) { - RemotingCommand remotingCommand; + RemotingCommand remotingCommand; - remotingCommand.setCode(13); - EXPECT_EQ(remotingCommand.getCode(), 13); + remotingCommand.setCode(13); + EXPECT_EQ(remotingCommand.getCode(), 13); - remotingCommand.setOpaque(12); - EXPECT_EQ(remotingCommand.getOpaque(), 12); + remotingCommand.setOpaque(12); + EXPECT_EQ(remotingCommand.getOpaque(), 12); - remotingCommand.setRemark("123"); - EXPECT_EQ(remotingCommand.getRemark(), "123"); + remotingCommand.setRemark("123"); + EXPECT_EQ(remotingCommand.getRemark(), "123"); - remotingCommand.setMsgBody("msgBody"); - EXPECT_EQ(remotingCommand.getMsgBody(), "msgBody"); + remotingCommand.setMsgBody("msgBody"); + EXPECT_EQ(remotingCommand.getMsgBody(), "msgBody"); - remotingCommand.addExtField("key", "value"); + remotingCommand.addExtField("key", "value"); } TEST(remotingCommand, flag) { - RemotingCommand remotingCommand(13, "CPP", MQVersion::s_CurrentVersion, 12, 0, "remark", - new GetRouteInfoRequestHeader("topic")); - ; - EXPECT_EQ(remotingCommand.getFlag(), 0); - - remotingCommand.markResponseType(); - int bits = 1 << 0; - int flag = 0; - flag |= bits; - EXPECT_EQ(remotingCommand.getFlag(), flag); - EXPECT_TRUE(remotingCommand.isResponseType()); - - bits = 1 << 1; - flag |= bits; - remotingCommand.markOnewayRPC(); - EXPECT_EQ(remotingCommand.getFlag(), flag); - EXPECT_TRUE(remotingCommand.isOnewayRPC()); + RemotingCommand remotingCommand(13, "CPP", MQVersion::s_CurrentVersion, 12, 0, "remark", + new GetRouteInfoRequestHeader("topic")); + ; + EXPECT_EQ(remotingCommand.getFlag(), 0); + + remotingCommand.markResponseType(); + int bits = 1 << 0; + int flag = 0; + flag |= bits; + EXPECT_EQ(remotingCommand.getFlag(), flag); + EXPECT_TRUE(remotingCommand.isResponseType()); + + bits = 1 << 1; + flag |= bits; + remotingCommand.markOnewayRPC(); + EXPECT_EQ(remotingCommand.getFlag(), flag); + EXPECT_TRUE(remotingCommand.isOnewayRPC()); } TEST(remotingCommand, encodeAndDecode) { - RemotingCommand remotingCommand(13, "CPP", MQVersion::s_CurrentVersion, 12, 3, "remark", NULL); - remotingCommand.SetBody("123123", 6); - remotingCommand.Encode(); - // no delete - const MemoryBlock *head = remotingCommand.GetHead(); - const MemoryBlock *body = remotingCommand.GetBody(); - - unique_ptr result(new MemoryOutputStream(1024)); - result->write(head->getData() + 4, head->getSize() - 4); - result->write(body->getData(), body->getSize()); - - shared_ptr decodeRemtingCommand(RemotingCommand::Decode(result->getMemoryBlock())); - EXPECT_EQ(remotingCommand.getCode(), decodeRemtingCommand->getCode()); - EXPECT_EQ(remotingCommand.getOpaque(), decodeRemtingCommand->getOpaque()); - EXPECT_EQ(remotingCommand.getRemark(), decodeRemtingCommand->getRemark()); - EXPECT_EQ(remotingCommand.getVersion(), decodeRemtingCommand->getVersion()); - EXPECT_EQ(remotingCommand.getFlag(), decodeRemtingCommand->getFlag()); - EXPECT_TRUE(decodeRemtingCommand->getCommandHeader() == NULL); - - // ~RemotingCommand delete - GetConsumerRunningInfoRequestHeader *requestHeader = new GetConsumerRunningInfoRequestHeader(); - requestHeader->setClientId("client"); - requestHeader->setConsumerGroup("consumerGroup"); - requestHeader->setJstackEnable(false); - - RemotingCommand encodeRemotingCommand(307, "CPP", MQVersion::s_CurrentVersion, 12, 3, "remark", requestHeader); - encodeRemotingCommand.SetBody("123123", 6); - encodeRemotingCommand.Encode(); - - // no delete - const MemoryBlock *phead = encodeRemotingCommand.GetHead(); - const MemoryBlock *pbody = encodeRemotingCommand.GetBody(); - - unique_ptr results(new MemoryOutputStream(1024)); - results->write(phead->getData() + 4, phead->getSize() - 4); - results->write(pbody->getData(), pbody->getSize()); - - shared_ptr decodeRemtingCommandTwo(RemotingCommand::Decode(results->getMemoryBlock())); - - decodeRemtingCommandTwo->SetExtHeader(encodeRemotingCommand.getCode()); - GetConsumerRunningInfoRequestHeader *header = - reinterpret_cast(decodeRemtingCommandTwo->getCommandHeader()); - EXPECT_EQ(requestHeader->getClientId(), header->getClientId()); - EXPECT_EQ(requestHeader->getConsumerGroup(), header->getConsumerGroup()); + RemotingCommand remotingCommand(13, "CPP", MQVersion::s_CurrentVersion, 12, 3, "remark", NULL); + remotingCommand.SetBody("123123", 6); + remotingCommand.Encode(); + // no delete + const MemoryBlock* head = remotingCommand.GetHead(); + const MemoryBlock* body = remotingCommand.GetBody(); + + unique_ptr result(new MemoryOutputStream(1024)); + result->write(head->getData() + 4, head->getSize() - 4); + result->write(body->getData(), body->getSize()); + + shared_ptr decodeRemtingCommand(RemotingCommand::Decode(result->getMemoryBlock())); + EXPECT_EQ(remotingCommand.getCode(), decodeRemtingCommand->getCode()); + EXPECT_EQ(remotingCommand.getOpaque(), decodeRemtingCommand->getOpaque()); + EXPECT_EQ(remotingCommand.getRemark(), decodeRemtingCommand->getRemark()); + EXPECT_EQ(remotingCommand.getVersion(), decodeRemtingCommand->getVersion()); + EXPECT_EQ(remotingCommand.getFlag(), decodeRemtingCommand->getFlag()); + EXPECT_TRUE(decodeRemtingCommand->getCommandHeader() == NULL); + + // ~RemotingCommand delete + GetConsumerRunningInfoRequestHeader* requestHeader = new GetConsumerRunningInfoRequestHeader(); + requestHeader->setClientId("client"); + requestHeader->setConsumerGroup("consumerGroup"); + requestHeader->setJstackEnable(false); + + RemotingCommand encodeRemotingCommand(307, "CPP", MQVersion::s_CurrentVersion, 12, 3, "remark", requestHeader); + encodeRemotingCommand.SetBody("123123", 6); + encodeRemotingCommand.Encode(); + + // no delete + const MemoryBlock* phead = encodeRemotingCommand.GetHead(); + const MemoryBlock* pbody = encodeRemotingCommand.GetBody(); + + unique_ptr results(new MemoryOutputStream(1024)); + results->write(phead->getData() + 4, phead->getSize() - 4); + results->write(pbody->getData(), pbody->getSize()); + + shared_ptr decodeRemtingCommandTwo(RemotingCommand::Decode(results->getMemoryBlock())); + + decodeRemtingCommandTwo->SetExtHeader(encodeRemotingCommand.getCode()); + GetConsumerRunningInfoRequestHeader* header = + reinterpret_cast(decodeRemtingCommandTwo->getCommandHeader()); + EXPECT_EQ(requestHeader->getClientId(), header->getClientId()); + EXPECT_EQ(requestHeader->getConsumerGroup(), header->getConsumerGroup()); } TEST(remotingCommand, SetExtHeader) { - shared_ptr remotingCommand(new RemotingCommand()); - - remotingCommand->SetExtHeader(-1); - EXPECT_TRUE(remotingCommand->getCommandHeader() == NULL); - - Json::Value object; - Json::Value extFields; - extFields["id"] = 1; - object["extFields"] = extFields; - remotingCommand->setParsedJson(object); - - remotingCommand->SetExtHeader(MQRequestCode::SEND_MESSAGE); - SendMessageResponseHeader *sendMessageResponseHeader = - reinterpret_cast(remotingCommand->getCommandHeader()); - EXPECT_EQ(sendMessageResponseHeader->msgId, ""); - - remotingCommand->SetExtHeader(MQRequestCode::PULL_MESSAGE); - PullMessageResponseHeader *pullMessageResponseHeader = - reinterpret_cast(remotingCommand->getCommandHeader()); - EXPECT_EQ(pullMessageResponseHeader->suggestWhichBrokerId, 0); - - remotingCommand->SetExtHeader(MQRequestCode::GET_MIN_OFFSET); - GetMinOffsetResponseHeader *getMinOffsetResponseHeader = - reinterpret_cast(remotingCommand->getCommandHeader()); - EXPECT_EQ(getMinOffsetResponseHeader->offset, 0); - - remotingCommand->SetExtHeader(MQRequestCode::GET_MAX_OFFSET); - GetMaxOffsetResponseHeader *getMaxOffsetResponseHeader = - reinterpret_cast(remotingCommand->getCommandHeader()); - EXPECT_EQ(getMaxOffsetResponseHeader->offset, 0); - - remotingCommand->SetExtHeader(MQRequestCode::SEARCH_OFFSET_BY_TIMESTAMP); - SearchOffsetResponseHeader *searchOffsetResponseHeader = - reinterpret_cast(remotingCommand->getCommandHeader()); - EXPECT_EQ(searchOffsetResponseHeader->offset, 0); - - remotingCommand->SetExtHeader(MQRequestCode::GET_EARLIEST_MSG_STORETIME); - GetEarliestMsgStoretimeResponseHeader *getEarliestMsgStoretimeResponseHeader = - reinterpret_cast(remotingCommand->getCommandHeader()); - EXPECT_EQ(getEarliestMsgStoretimeResponseHeader->timestamp, 0); - - remotingCommand->SetExtHeader(MQRequestCode::QUERY_CONSUMER_OFFSET); - QueryConsumerOffsetResponseHeader *queryConsumerOffsetResponseHeader = - reinterpret_cast(remotingCommand->getCommandHeader()); - EXPECT_EQ(queryConsumerOffsetResponseHeader->offset, 0); - - extFields["isForce"] = "true"; - object["extFields"] = extFields; - remotingCommand->setParsedJson(object); - remotingCommand->SetExtHeader(MQRequestCode::RESET_CONSUMER_CLIENT_OFFSET); - ResetOffsetRequestHeader *resetOffsetRequestHeader = - reinterpret_cast(remotingCommand->getCommandHeader()); - resetOffsetRequestHeader->setGroup("group"); - - remotingCommand->SetExtHeader(MQRequestCode::GET_CONSUMER_RUNNING_INFO); - GetConsumerRunningInfoRequestHeader *getConsumerRunningInfoRequestHeader = - reinterpret_cast(remotingCommand->getCommandHeader()); - getConsumerRunningInfoRequestHeader->setClientId("id"); - - remotingCommand->SetExtHeader(MQRequestCode::NOTIFY_CONSUMER_IDS_CHANGED); - NotifyConsumerIdsChangedRequestHeader *notifyConsumerIdsChangedRequestHeader = - reinterpret_cast(remotingCommand->getCommandHeader()); - notifyConsumerIdsChangedRequestHeader->setGroup("group"); + shared_ptr remotingCommand(new RemotingCommand()); + + remotingCommand->SetExtHeader(-1); + EXPECT_TRUE(remotingCommand->getCommandHeader() == NULL); + + Json::Value object; + Json::Value extFields; + extFields["id"] = 1; + object["extFields"] = extFields; + remotingCommand->setParsedJson(object); + + remotingCommand->SetExtHeader(MQRequestCode::SEND_MESSAGE); + SendMessageResponseHeader* sendMessageResponseHeader = + reinterpret_cast(remotingCommand->getCommandHeader()); + EXPECT_EQ(sendMessageResponseHeader->msgId, ""); + + remotingCommand->SetExtHeader(MQRequestCode::PULL_MESSAGE); + PullMessageResponseHeader* pullMessageResponseHeader = + reinterpret_cast(remotingCommand->getCommandHeader()); + EXPECT_EQ(pullMessageResponseHeader->suggestWhichBrokerId, 0); + + remotingCommand->SetExtHeader(MQRequestCode::GET_MIN_OFFSET); + GetMinOffsetResponseHeader* getMinOffsetResponseHeader = + reinterpret_cast(remotingCommand->getCommandHeader()); + EXPECT_EQ(getMinOffsetResponseHeader->offset, 0); + + remotingCommand->SetExtHeader(MQRequestCode::GET_MAX_OFFSET); + GetMaxOffsetResponseHeader* getMaxOffsetResponseHeader = + reinterpret_cast(remotingCommand->getCommandHeader()); + EXPECT_EQ(getMaxOffsetResponseHeader->offset, 0); + + remotingCommand->SetExtHeader(MQRequestCode::SEARCH_OFFSET_BY_TIMESTAMP); + SearchOffsetResponseHeader* searchOffsetResponseHeader = + reinterpret_cast(remotingCommand->getCommandHeader()); + EXPECT_EQ(searchOffsetResponseHeader->offset, 0); + + remotingCommand->SetExtHeader(MQRequestCode::GET_EARLIEST_MSG_STORETIME); + GetEarliestMsgStoretimeResponseHeader* getEarliestMsgStoretimeResponseHeader = + reinterpret_cast(remotingCommand->getCommandHeader()); + EXPECT_EQ(getEarliestMsgStoretimeResponseHeader->timestamp, 0); + + remotingCommand->SetExtHeader(MQRequestCode::QUERY_CONSUMER_OFFSET); + QueryConsumerOffsetResponseHeader* queryConsumerOffsetResponseHeader = + reinterpret_cast(remotingCommand->getCommandHeader()); + EXPECT_EQ(queryConsumerOffsetResponseHeader->offset, 0); + + extFields["isForce"] = "true"; + object["extFields"] = extFields; + remotingCommand->setParsedJson(object); + remotingCommand->SetExtHeader(MQRequestCode::RESET_CONSUMER_CLIENT_OFFSET); + ResetOffsetRequestHeader* resetOffsetRequestHeader = + reinterpret_cast(remotingCommand->getCommandHeader()); + resetOffsetRequestHeader->setGroup("group"); + + remotingCommand->SetExtHeader(MQRequestCode::GET_CONSUMER_RUNNING_INFO); + GetConsumerRunningInfoRequestHeader* getConsumerRunningInfoRequestHeader = + reinterpret_cast(remotingCommand->getCommandHeader()); + getConsumerRunningInfoRequestHeader->setClientId("id"); + + remotingCommand->SetExtHeader(MQRequestCode::NOTIFY_CONSUMER_IDS_CHANGED); + NotifyConsumerIdsChangedRequestHeader* notifyConsumerIdsChangedRequestHeader = + reinterpret_cast(remotingCommand->getCommandHeader()); + notifyConsumerIdsChangedRequestHeader->setGroup("group"); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "remotingCommand.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "remotingCommand.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/protocol/TopicRouteDataTest.cpp b/test/src/protocol/TopicRouteDataTest.cpp index 8ddba9fa7..cf88ac106 100644 --- a/test/src/protocol/TopicRouteDataTest.cpp +++ b/test/src/protocol/TopicRouteDataTest.cpp @@ -30,64 +30,64 @@ using rocketmq::QueueData; using rocketmq::TopicRouteData; TEST(topicRouteData, topicRouteData) { - Json::Value root; - root["orderTopicConf"] = "orderTopicConf"; + Json::Value root; + root["orderTopicConf"] = "orderTopicConf"; - Json::Value queueDatas; - Json::Value queueData; + Json::Value queueDatas; + Json::Value queueData; - queueData["brokerName"] = "brokerTest"; - queueData["readQueueNums"] = 8; - queueData["writeQueueNums"] = 8; - queueData["perm"] = 7; - queueDatas[0] = queueData; + queueData["brokerName"] = "brokerTest"; + queueData["readQueueNums"] = 8; + queueData["writeQueueNums"] = 8; + queueData["perm"] = 7; + queueDatas[0] = queueData; - root["queueDatas"] = queueDatas; + root["queueDatas"] = queueDatas; - Json::Value brokerDatas; - Json::Value brokerData; - brokerData["brokerName"] = "testBroker"; + Json::Value brokerDatas; + Json::Value brokerData; + brokerData["brokerName"] = "testBroker"; - Json::Value brokerAddrs; - brokerAddrs["0"] = "127.0.0.1:10091"; - brokerAddrs["1"] = "127.0.0.2:10092"; - brokerData["brokerAddrs"] = brokerAddrs; + Json::Value brokerAddrs; + brokerAddrs["0"] = "127.0.0.1:10091"; + brokerAddrs["1"] = "127.0.0.2:10092"; + brokerData["brokerAddrs"] = brokerAddrs; - brokerDatas[0] = brokerData; + brokerDatas[0] = brokerData; - root["brokerDatas"] = brokerDatas; - string out = root.toStyledString(); + root["brokerDatas"] = brokerDatas; + string out = root.toStyledString(); - MemoryBlock *block = new MemoryBlock(out.c_str(), out.size()); - TopicRouteData *topicRouteData = TopicRouteData::Decode(block); - EXPECT_EQ(root["orderTopicConf"], topicRouteData->getOrderTopicConf()); + MemoryBlock* block = new MemoryBlock(out.c_str(), out.size()); + TopicRouteData* topicRouteData = TopicRouteData::Decode(block); + EXPECT_EQ(root["orderTopicConf"], topicRouteData->getOrderTopicConf()); - BrokerData broker; - broker.brokerName = "testBroker"; - broker.brokerAddrs[0] = "127.0.0.1:10091"; - broker.brokerAddrs[1] = "127.0.0.2:10092"; + BrokerData broker; + broker.brokerName = "testBroker"; + broker.brokerAddrs[0] = "127.0.0.1:10091"; + broker.brokerAddrs[1] = "127.0.0.2:10092"; - vector brokerDataSt = topicRouteData->getBrokerDatas(); - EXPECT_EQ(broker, brokerDataSt[0]); + vector brokerDataSt = topicRouteData->getBrokerDatas(); + EXPECT_EQ(broker, brokerDataSt[0]); - QueueData queue; - queue.brokerName = "brokerTest"; - queue.readQueueNums = 8; - queue.writeQueueNums = 8; - queue.perm = 7; - vector queueDataSt = topicRouteData->getQueueDatas(); - EXPECT_EQ(queue, queueDataSt[0]); + QueueData queue; + queue.brokerName = "brokerTest"; + queue.readQueueNums = 8; + queue.writeQueueNums = 8; + queue.perm = 7; + vector queueDataSt = topicRouteData->getQueueDatas(); + EXPECT_EQ(queue, queueDataSt[0]); - EXPECT_EQ(topicRouteData->selectBrokerAddr(), "127.0.0.1:10091"); + EXPECT_EQ(topicRouteData->selectBrokerAddr(), "127.0.0.1:10091"); - delete block; - delete topicRouteData; + delete block; + delete topicRouteData; } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "topicRouteData.topicRouteData"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "topicRouteData.topicRouteData"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/transport/ClientRemotingProcessorTest.cpp b/test/src/transport/ClientRemotingProcessorTest.cpp index 64fb9445a..5c906ebde 100644 --- a/test/src/transport/ClientRemotingProcessorTest.cpp +++ b/test/src/transport/ClientRemotingProcessorTest.cpp @@ -66,172 +66,172 @@ using rocketmq::SessionCredentials; using rocketmq::UtilAll; class MockClientRemotingProcessor : public ClientRemotingProcessor { - public: - MockClientRemotingProcessor(MQClientFactory *factrory) : ClientRemotingProcessor(factrory) {} - MOCK_METHOD1(resetOffset, RemotingCommand *(RemotingCommand *request)); - MOCK_METHOD1(getConsumerRunningInfo, RemotingCommand *(RemotingCommand *request)); - MOCK_METHOD1(notifyConsumerIdsChanged, RemotingCommand *(RemotingCommand *request)); + public: + MockClientRemotingProcessor(MQClientFactory* factrory) : ClientRemotingProcessor(factrory) {} + MOCK_METHOD1(resetOffset, RemotingCommand*(RemotingCommand* request)); + MOCK_METHOD1(getConsumerRunningInfo, RemotingCommand*(RemotingCommand* request)); + MOCK_METHOD1(notifyConsumerIdsChanged, RemotingCommand*(RemotingCommand* request)); }; class MockMQClientFactory : public MQClientFactory { - public: - MockMQClientFactory(const string &clientID, - int pullThreadNum, - uint64_t tcpConnectTimeout, - uint64_t tcpTransportTryLockTimeout, - string unitName) - : MQClientFactory(clientID, pullThreadNum, tcpConnectTimeout, tcpTransportTryLockTimeout, unitName) {} - - MOCK_METHOD3(resetOffset, - void(const string &group, const string &topic, const map &offsetTable)); - MOCK_METHOD1(consumerRunningInfo, ConsumerRunningInfo *(const string &consumerGroup)); - MOCK_METHOD2(getSessionCredentialFromConsumer, - bool(const string &consumerGroup, SessionCredentials &sessionCredentials)); - MOCK_METHOD1(doRebalanceByConsumerGroup, void(const string &consumerGroup)); + public: + MockMQClientFactory(const string& clientID, + int pullThreadNum, + uint64_t tcpConnectTimeout, + uint64_t tcpTransportTryLockTimeout, + string unitName) + : MQClientFactory(clientID, pullThreadNum, tcpConnectTimeout, tcpTransportTryLockTimeout, unitName) {} + + MOCK_METHOD3(resetOffset, + void(const string& group, const string& topic, const map& offsetTable)); + MOCK_METHOD1(consumerRunningInfo, ConsumerRunningInfo*(const string& consumerGroup)); + MOCK_METHOD2(getSessionCredentialFromConsumer, + bool(const string& consumerGroup, SessionCredentials& sessionCredentials)); + MOCK_METHOD1(doRebalanceByConsumerGroup, void(const string& consumerGroup)); }; TEST(clientRemotingProcessor, processRequest) { - MockMQClientFactory *factory = new MockMQClientFactory("testClientId", 4, 3000, 4000, "a"); - ClientRemotingProcessor clientRemotingProcessor(factory); + MockMQClientFactory* factory = new MockMQClientFactory("testClientId", 4, 3000, 4000, "a"); + ClientRemotingProcessor clientRemotingProcessor(factory); - string addr = "127.0.0.1:9876"; - RemotingCommand *command = new RemotingCommand(); - RemotingCommand *pResponse = new RemotingCommand(13); + string addr = "127.0.0.1:9876"; + RemotingCommand* command = new RemotingCommand(); + RemotingCommand* pResponse = new RemotingCommand(13); - pResponse->setCode(MQRequestCode::RESET_CONSUMER_CLIENT_OFFSET); - command->setCode(MQRequestCode::RESET_CONSUMER_CLIENT_OFFSET); - EXPECT_TRUE(clientRemotingProcessor.processRequest(addr, command) == nullptr); - EXPECT_EQ(nullptr, clientRemotingProcessor.processRequest(addr, command)); + pResponse->setCode(MQRequestCode::RESET_CONSUMER_CLIENT_OFFSET); + command->setCode(MQRequestCode::RESET_CONSUMER_CLIENT_OFFSET); + EXPECT_TRUE(clientRemotingProcessor.processRequest(addr, command) == nullptr); + EXPECT_EQ(nullptr, clientRemotingProcessor.processRequest(addr, command)); - NotifyConsumerIdsChangedRequestHeader *header = new NotifyConsumerIdsChangedRequestHeader(); - header->setGroup("testGroup"); - RemotingCommand *twoCommand = new RemotingCommand(MQRequestCode::NOTIFY_CONSUMER_IDS_CHANGED, header); + NotifyConsumerIdsChangedRequestHeader* header = new NotifyConsumerIdsChangedRequestHeader(); + header->setGroup("testGroup"); + RemotingCommand* twoCommand = new RemotingCommand(MQRequestCode::NOTIFY_CONSUMER_IDS_CHANGED, header); - EXPECT_EQ(NULL, clientRemotingProcessor.processRequest(addr, twoCommand)); + EXPECT_EQ(NULL, clientRemotingProcessor.processRequest(addr, twoCommand)); - command->setCode(MQRequestCode::GET_CONSUMER_RUNNING_INFO); - // EXPECT_EQ(NULL , clientRemotingProcessor.processRequest(addr, command)); + command->setCode(MQRequestCode::GET_CONSUMER_RUNNING_INFO); + // EXPECT_EQ(NULL , clientRemotingProcessor.processRequest(addr, command)); - command->setCode(MQRequestCode::CHECK_TRANSACTION_STATE); - EXPECT_TRUE(clientRemotingProcessor.processRequest(addr, command) == nullptr); + command->setCode(MQRequestCode::CHECK_TRANSACTION_STATE); + EXPECT_TRUE(clientRemotingProcessor.processRequest(addr, command) == nullptr); - command->setCode(MQRequestCode::GET_CONSUMER_STATUS_FROM_CLIENT); - EXPECT_TRUE(clientRemotingProcessor.processRequest(addr, command) == nullptr); + command->setCode(MQRequestCode::GET_CONSUMER_STATUS_FROM_CLIENT); + EXPECT_TRUE(clientRemotingProcessor.processRequest(addr, command) == nullptr); - command->setCode(MQRequestCode::CONSUME_MESSAGE_DIRECTLY); - EXPECT_TRUE(clientRemotingProcessor.processRequest(addr, command) == nullptr); + command->setCode(MQRequestCode::CONSUME_MESSAGE_DIRECTLY); + EXPECT_TRUE(clientRemotingProcessor.processRequest(addr, command) == nullptr); - command->setCode(1); - EXPECT_TRUE(clientRemotingProcessor.processRequest(addr, command) == nullptr); + command->setCode(1); + EXPECT_TRUE(clientRemotingProcessor.processRequest(addr, command) == nullptr); - delete twoCommand; - delete command; - delete pResponse; + delete twoCommand; + delete command; + delete pResponse; } TEST(clientRemotingProcessor, resetOffset) { - MockMQClientFactory *factory = new MockMQClientFactory("testClientId", 4, 3000, 4000, "a"); - Mock::AllowLeak(factory); - ClientRemotingProcessor clientRemotingProcessor(factory); - Value root; - Value messageQueues; - Value messageQueue; - messageQueue["brokerName"] = "testBroker"; - messageQueue["queueId"] = 4; - messageQueue["topic"] = "testTopic"; - messageQueue["offset"] = 1024; - - messageQueues.append(messageQueue); - root["offsetTable"] = messageQueues; - - FastWriter wrtier; - string strData = wrtier.write(root); - - ResetOffsetRequestHeader *header = new ResetOffsetRequestHeader(); - RemotingCommand *request = new RemotingCommand(13, header); - - EXPECT_CALL(*factory, resetOffset(_, _, _)).Times(1); - clientRemotingProcessor.resetOffset(request); - - request->SetBody(strData.c_str(), strData.size() - 2); - clientRemotingProcessor.resetOffset(request); - - request->SetBody(strData.c_str(), strData.size()); - clientRemotingProcessor.resetOffset(request); - - //here header no need delete, it will managered by RemotingCommand - //delete header; - delete request; + MockMQClientFactory* factory = new MockMQClientFactory("testClientId", 4, 3000, 4000, "a"); + Mock::AllowLeak(factory); + ClientRemotingProcessor clientRemotingProcessor(factory); + Value root; + Value messageQueues; + Value messageQueue; + messageQueue["brokerName"] = "testBroker"; + messageQueue["queueId"] = 4; + messageQueue["topic"] = "testTopic"; + messageQueue["offset"] = 1024; + + messageQueues.append(messageQueue); + root["offsetTable"] = messageQueues; + + FastWriter wrtier; + string strData = wrtier.write(root); + + ResetOffsetRequestHeader* header = new ResetOffsetRequestHeader(); + RemotingCommand* request = new RemotingCommand(13, header); + + EXPECT_CALL(*factory, resetOffset(_, _, _)).Times(1); + clientRemotingProcessor.resetOffset(request); + + request->SetBody(strData.c_str(), strData.size() - 2); + clientRemotingProcessor.resetOffset(request); + + request->SetBody(strData.c_str(), strData.size()); + clientRemotingProcessor.resetOffset(request); + + // here header no need delete, it will managered by RemotingCommand + // delete header; + delete request; } TEST(clientRemotingProcessorS, getConsumerRunningInfo) { - MockMQClientFactory *factory = new MockMQClientFactory("testClientId", 4, 3000, 4000, "a"); - ConsumerRunningInfo *info = new ConsumerRunningInfo(); - EXPECT_CALL(*factory, consumerRunningInfo(_)).Times(2).WillOnce(Return(info)).WillOnce(Return(info)); - EXPECT_CALL(*factory, getSessionCredentialFromConsumer(_, _)) - .Times(2); //.WillRepeatedly(SetArgReferee<1>(sessionCredentials)); - ClientRemotingProcessor clientRemotingProcessor(factory); - - GetConsumerRunningInfoRequestHeader *header = new GetConsumerRunningInfoRequestHeader(); - header->setConsumerGroup("testGroup"); - - RemotingCommand *request = new RemotingCommand(14, header); - - RemotingCommand *command = clientRemotingProcessor.getConsumerRunningInfo("127.0.0.1:9876", request); - EXPECT_EQ(command->getCode(), MQResponseCode::SYSTEM_ERROR); - EXPECT_EQ(command->getRemark(), "The Consumer Group not exist in this consumer"); - delete command; - delete request; + MockMQClientFactory* factory = new MockMQClientFactory("testClientId", 4, 3000, 4000, "a"); + ConsumerRunningInfo* info = new ConsumerRunningInfo(); + EXPECT_CALL(*factory, consumerRunningInfo(_)).Times(2).WillOnce(Return(info)).WillOnce(Return(info)); + EXPECT_CALL(*factory, getSessionCredentialFromConsumer(_, _)) + .Times(2); //.WillRepeatedly(SetArgReferee<1>(sessionCredentials)); + ClientRemotingProcessor clientRemotingProcessor(factory); + + GetConsumerRunningInfoRequestHeader* header = new GetConsumerRunningInfoRequestHeader(); + header->setConsumerGroup("testGroup"); + + RemotingCommand* request = new RemotingCommand(14, header); + + RemotingCommand* command = clientRemotingProcessor.getConsumerRunningInfo("127.0.0.1:9876", request); + EXPECT_EQ(command->getCode(), MQResponseCode::SYSTEM_ERROR); + EXPECT_EQ(command->getRemark(), "The Consumer Group not exist in this consumer"); + delete command; + delete request; } TEST(clientRemotingProcessor, notifyConsumerIdsChanged) { - MockMQClientFactory *factory = new MockMQClientFactory("testClientId", 4, 3000, 4000, "a"); - Mock::AllowLeak(factory); - ClientRemotingProcessor clientRemotingProcessor(factory); - NotifyConsumerIdsChangedRequestHeader *header = new NotifyConsumerIdsChangedRequestHeader(); - header->setGroup("testGroup"); - RemotingCommand *request = new RemotingCommand(14, header); + MockMQClientFactory* factory = new MockMQClientFactory("testClientId", 4, 3000, 4000, "a"); + Mock::AllowLeak(factory); + ClientRemotingProcessor clientRemotingProcessor(factory); + NotifyConsumerIdsChangedRequestHeader* header = new NotifyConsumerIdsChangedRequestHeader(); + header->setGroup("testGroup"); + RemotingCommand* request = new RemotingCommand(14, header); - EXPECT_CALL(*factory, doRebalanceByConsumerGroup(_)).Times(1); - clientRemotingProcessor.notifyConsumerIdsChanged(request); + EXPECT_CALL(*factory, doRebalanceByConsumerGroup(_)).Times(1); + clientRemotingProcessor.notifyConsumerIdsChanged(request); - delete request; + delete request; } TEST(clientRemotingProcessor, resetOffsetBody) { - MockMQClientFactory *factory = new MockMQClientFactory("testClientId", 4, 3000, 4000, "a"); - ClientRemotingProcessor clientRemotingProcessor(factory); + MockMQClientFactory* factory = new MockMQClientFactory("testClientId", 4, 3000, 4000, "a"); + ClientRemotingProcessor clientRemotingProcessor(factory); - Value root; - Value messageQueues; - Value messageQueue; - messageQueue["brokerName"] = "testBroker"; - messageQueue["queueId"] = 4; - messageQueue["topic"] = "testTopic"; - messageQueue["offset"] = 1024; + Value root; + Value messageQueues; + Value messageQueue; + messageQueue["brokerName"] = "testBroker"; + messageQueue["queueId"] = 4; + messageQueue["topic"] = "testTopic"; + messageQueue["offset"] = 1024; - messageQueues.append(messageQueue); - root["offsetTable"] = messageQueues; + messageQueues.append(messageQueue); + root["offsetTable"] = messageQueues; - FastWriter wrtier; - string strData = wrtier.write(root); + FastWriter wrtier; + string strData = wrtier.write(root); - MemoryBlock *mem = new MemoryBlock(strData.c_str(), strData.size()); + MemoryBlock* mem = new MemoryBlock(strData.c_str(), strData.size()); - ResetOffsetBody *resetOffset = ResetOffsetBody::Decode(mem); + ResetOffsetBody* resetOffset = ResetOffsetBody::Decode(mem); - map map = resetOffset->getOffsetTable(); - MQMessageQueue mqmq("testTopic", "testBroker", 4); - EXPECT_EQ(map[mqmq], 1024); - Mock::AllowLeak(factory); - delete resetOffset; - delete mem; + map map = resetOffset->getOffsetTable(); + MQMessageQueue mqmq("testTopic", "testBroker", 4); + EXPECT_EQ(map[mqmq], 1024); + Mock::AllowLeak(factory); + delete resetOffset; + delete mem; } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "clientRemotingProcessor.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "clientRemotingProcessor.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/transport/ResponseFutureTest.cpp b/test/src/transport/ResponseFutureTest.cpp index 483fcedde..3c375c9f0 100644 --- a/test/src/transport/ResponseFutureTest.cpp +++ b/test/src/transport/ResponseFutureTest.cpp @@ -44,137 +44,137 @@ using rocketmq::TcpRemotingClient; using rocketmq::UtilAll; class MockAsyncCallbackWrap : public SendCallbackWrap { - public: - MockAsyncCallbackWrap(AsyncCallback *pAsyncCallback, MQClientAPIImpl *pclientAPI) - : SendCallbackWrap("", MQMessage(), pAsyncCallback, pclientAPI) {} + public: + MockAsyncCallbackWrap(AsyncCallback* pAsyncCallback, MQClientAPIImpl* pclientAPI) + : SendCallbackWrap("", MQMessage(), pAsyncCallback, pclientAPI) {} - MOCK_METHOD2(operationComplete, void(ResponseFuture *, bool)); - MOCK_METHOD0(onException, void()); - asyncCallBackType getCallbackType() { return asyncCallBackType::sendCallbackWrap; } + MOCK_METHOD2(operationComplete, void(ResponseFuture*, bool)); + MOCK_METHOD0(onException, void()); + asyncCallBackType getCallbackType() { return asyncCallBackType::sendCallbackWrap; } }; TEST(responseFuture, init) { - ResponseFuture responseFuture(13, 4, NULL, 1000); - EXPECT_EQ(responseFuture.getRequestCode(), 13); - EXPECT_EQ(responseFuture.getOpaque(), 4); - - EXPECT_EQ(responseFuture.getRequestCommand().getCode(), 0); - EXPECT_FALSE(responseFuture.isSendRequestOK()); - EXPECT_EQ(responseFuture.getMaxRetrySendTimes(), 1); - EXPECT_EQ(responseFuture.getRetrySendTimes(), 1); - EXPECT_EQ(responseFuture.getBrokerAddr(), ""); - - EXPECT_FALSE(responseFuture.getASyncFlag()); - EXPECT_TRUE(responseFuture.getAsyncResponseFlag()); - EXPECT_FALSE(responseFuture.getSyncResponseFlag()); - EXPECT_TRUE(responseFuture.getAsyncCallbackWrap() == nullptr); - - // ~ResponseFuture delete pcall - SendCallbackWrap *pcall = new SendCallbackWrap("", MQMessage(), nullptr, nullptr); - ResponseFuture twoResponseFuture(13, 4, nullptr, 1000, true, pcall); - EXPECT_TRUE(twoResponseFuture.getASyncFlag()); - EXPECT_FALSE(twoResponseFuture.getAsyncResponseFlag()); - EXPECT_TRUE(twoResponseFuture.getSyncResponseFlag()); - EXPECT_FALSE(twoResponseFuture.getAsyncCallbackWrap() == nullptr); + ResponseFuture responseFuture(13, 4, NULL, 1000); + EXPECT_EQ(responseFuture.getRequestCode(), 13); + EXPECT_EQ(responseFuture.getOpaque(), 4); + + EXPECT_EQ(responseFuture.getRequestCommand().getCode(), 0); + EXPECT_FALSE(responseFuture.isSendRequestOK()); + EXPECT_EQ(responseFuture.getMaxRetrySendTimes(), 1); + EXPECT_EQ(responseFuture.getRetrySendTimes(), 1); + EXPECT_EQ(responseFuture.getBrokerAddr(), ""); + + EXPECT_FALSE(responseFuture.getASyncFlag()); + EXPECT_TRUE(responseFuture.getAsyncResponseFlag()); + EXPECT_FALSE(responseFuture.getSyncResponseFlag()); + EXPECT_TRUE(responseFuture.getAsyncCallbackWrap() == nullptr); + + // ~ResponseFuture delete pcall + SendCallbackWrap* pcall = new SendCallbackWrap("", MQMessage(), nullptr, nullptr); + ResponseFuture twoResponseFuture(13, 4, nullptr, 1000, true, pcall); + EXPECT_TRUE(twoResponseFuture.getASyncFlag()); + EXPECT_FALSE(twoResponseFuture.getAsyncResponseFlag()); + EXPECT_TRUE(twoResponseFuture.getSyncResponseFlag()); + EXPECT_FALSE(twoResponseFuture.getAsyncCallbackWrap() == nullptr); } TEST(responseFuture, info) { - ResponseFuture responseFuture(13, 4, NULL, 1000); + ResponseFuture responseFuture(13, 4, NULL, 1000); - responseFuture.setAsyncResponseFlag(); - EXPECT_TRUE(responseFuture.getAsyncResponseFlag()); + responseFuture.setAsyncResponseFlag(); + EXPECT_TRUE(responseFuture.getAsyncResponseFlag()); - responseFuture.setBrokerAddr("127.0.0.1:9876"); - EXPECT_EQ(responseFuture.getBrokerAddr(), "127.0.0.1:9876"); + responseFuture.setBrokerAddr("127.0.0.1:9876"); + EXPECT_EQ(responseFuture.getBrokerAddr(), "127.0.0.1:9876"); - responseFuture.setMaxRetrySendTimes(3000); - EXPECT_EQ(responseFuture.getMaxRetrySendTimes(), 3000); + responseFuture.setMaxRetrySendTimes(3000); + EXPECT_EQ(responseFuture.getMaxRetrySendTimes(), 3000); - responseFuture.setRetrySendTimes(3000); - EXPECT_EQ(responseFuture.getRetrySendTimes(), 3000); + responseFuture.setRetrySendTimes(3000); + EXPECT_EQ(responseFuture.getRetrySendTimes(), 3000); - responseFuture.setSendRequestOK(true); - EXPECT_TRUE(responseFuture.isSendRequestOK()); + responseFuture.setSendRequestOK(true); + EXPECT_TRUE(responseFuture.isSendRequestOK()); } TEST(responseFuture, response) { - // m_bAsync = false m_syncResponse - ResponseFuture responseFuture(13, 4, NULL, 1000); + // m_bAsync = false m_syncResponse + ResponseFuture responseFuture(13, 4, NULL, 1000); - EXPECT_FALSE(responseFuture.getASyncFlag()); - EXPECT_FALSE(responseFuture.getSyncResponseFlag()); - EXPECT_TRUE(responseFuture.getAsyncResponseFlag()); + EXPECT_FALSE(responseFuture.getASyncFlag()); + EXPECT_FALSE(responseFuture.getSyncResponseFlag()); + EXPECT_TRUE(responseFuture.getAsyncResponseFlag()); - RemotingCommand *pResponseCommand = NULL; - responseFuture.setResponse(pResponseCommand); - EXPECT_EQ(responseFuture.getRequestCommand().getCode(), 0); + RemotingCommand* pResponseCommand = NULL; + responseFuture.setResponse(pResponseCommand); + EXPECT_EQ(responseFuture.getRequestCommand().getCode(), 0); - EXPECT_TRUE(responseFuture.getSyncResponseFlag()); + EXPECT_TRUE(responseFuture.getSyncResponseFlag()); - // m_bAsync = true m_syncResponse - ResponseFuture twoResponseFuture(13, 4, NULL, 1000, true); + // m_bAsync = true m_syncResponse + ResponseFuture twoResponseFuture(13, 4, NULL, 1000, true); - EXPECT_TRUE(twoResponseFuture.getASyncFlag()); - EXPECT_TRUE(twoResponseFuture.getSyncResponseFlag()); - EXPECT_FALSE(twoResponseFuture.getAsyncResponseFlag()); + EXPECT_TRUE(twoResponseFuture.getASyncFlag()); + EXPECT_TRUE(twoResponseFuture.getSyncResponseFlag()); + EXPECT_FALSE(twoResponseFuture.getAsyncResponseFlag()); - twoResponseFuture.setResponse(pResponseCommand); - EXPECT_TRUE(twoResponseFuture.getSyncResponseFlag()); + twoResponseFuture.setResponse(pResponseCommand); + EXPECT_TRUE(twoResponseFuture.getSyncResponseFlag()); - ResponseFuture threeSesponseFuture(13, 4, NULL, 1000); + ResponseFuture threeSesponseFuture(13, 4, NULL, 1000); - uint64_t millis = UtilAll::currentTimeMillis(); - RemotingCommand *remotingCommand = threeSesponseFuture.waitResponse(10); - uint64_t useTime = UtilAll::currentTimeMillis() - millis; - EXPECT_LT(useTime, 30); + uint64_t millis = UtilAll::currentTimeMillis(); + RemotingCommand* remotingCommand = threeSesponseFuture.waitResponse(10); + uint64_t useTime = UtilAll::currentTimeMillis() - millis; + EXPECT_LT(useTime, 30); - EXPECT_TRUE(responseFuture.getSyncResponseFlag()); - EXPECT_EQ(NULL, remotingCommand); + EXPECT_TRUE(responseFuture.getSyncResponseFlag()); + EXPECT_EQ(NULL, remotingCommand); } TEST(responseFuture, executeInvokeCallback) { - // executeInvokeCallback delete wrap - MockAsyncCallbackWrap *wrap = new MockAsyncCallbackWrap(nullptr, nullptr); - ResponseFuture responseFuture(13, 4, nullptr, 1000, false, wrap); - - RemotingCommand *pResponseCommand = new RemotingCommand(); - responseFuture.setResponse(pResponseCommand); - responseFuture.executeInvokeCallback(); - EXPECT_EQ(NULL, responseFuture.getCommand()); - - EXPECT_CALL(*wrap, operationComplete(_, _)).Times(1); - pResponseCommand = new RemotingCommand(); - responseFuture.setResponse(pResponseCommand); - responseFuture.setAsyncCallBackStatus(asyncCallBackStatus::asyncCallBackStatus_response); - responseFuture.executeInvokeCallback(); - EXPECT_EQ(pResponseCommand->getCode(), 0); - - ResponseFuture twoResponseFuture(13, 4, nullptr, 1000, false, NULL); - pResponseCommand = new RemotingCommand(); - twoResponseFuture.executeInvokeCallback(); - EXPECT_EQ(NULL, twoResponseFuture.getCommand()); + // executeInvokeCallback delete wrap + MockAsyncCallbackWrap* wrap = new MockAsyncCallbackWrap(nullptr, nullptr); + ResponseFuture responseFuture(13, 4, nullptr, 1000, false, wrap); + + RemotingCommand* pResponseCommand = new RemotingCommand(); + responseFuture.setResponse(pResponseCommand); + responseFuture.executeInvokeCallback(); + EXPECT_EQ(NULL, responseFuture.getCommand()); + + EXPECT_CALL(*wrap, operationComplete(_, _)).Times(1); + pResponseCommand = new RemotingCommand(); + responseFuture.setResponse(pResponseCommand); + responseFuture.setAsyncCallBackStatus(asyncCallBackStatus::asyncCallBackStatus_response); + responseFuture.executeInvokeCallback(); + EXPECT_EQ(pResponseCommand->getCode(), 0); + + ResponseFuture twoResponseFuture(13, 4, nullptr, 1000, false, NULL); + pResponseCommand = new RemotingCommand(); + twoResponseFuture.executeInvokeCallback(); + EXPECT_EQ(NULL, twoResponseFuture.getCommand()); } TEST(responseFuture, executeInvokeCallbackException) { - // executeInvokeCallbackException delete wrap - MockAsyncCallbackWrap *wrap = new MockAsyncCallbackWrap(nullptr, nullptr); + // executeInvokeCallbackException delete wrap + MockAsyncCallbackWrap* wrap = new MockAsyncCallbackWrap(nullptr, nullptr); - ResponseFuture responseFuture(13, 4, nullptr, 1000, false, wrap); + ResponseFuture responseFuture(13, 4, nullptr, 1000, false, wrap); - EXPECT_CALL(*wrap, onException()).Times(1); - responseFuture.executeInvokeCallbackException(); + EXPECT_CALL(*wrap, onException()).Times(1); + responseFuture.executeInvokeCallbackException(); - responseFuture.setAsyncCallBackStatus(asyncCallBackStatus::asyncCallBackStatus_timeout); - responseFuture.executeInvokeCallbackException(); + responseFuture.setAsyncCallBackStatus(asyncCallBackStatus::asyncCallBackStatus_timeout); + responseFuture.executeInvokeCallbackException(); - ResponseFuture twoRresponseFuture(13, 4, nullptr, 1000, false, NULL); - twoRresponseFuture.executeInvokeCallbackException(); + ResponseFuture twoRresponseFuture(13, 4, nullptr, 1000, false, NULL); + twoRresponseFuture.executeInvokeCallbackException(); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "responseFuture.*"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "responseFuture.*"; + int itestts = RUN_ALL_TESTS(); + return itestts; } diff --git a/test/src/transport/SocketUtilTest.cpp b/test/src/transport/SocketUtilTest.cpp index 875f2e723..09560b241 100644 --- a/test/src/transport/SocketUtilTest.cpp +++ b/test/src/transport/SocketUtilTest.cpp @@ -24,24 +24,24 @@ using ::testing::InitGoogleTest; using testing::Return; TEST(socketUtil, init) { - sockaddr addr = rocketmq::IPPort2socketAddress(inet_addr("127.0.0.1"), 10091); + sockaddr addr = rocketmq::IPPort2socketAddress(inet_addr("127.0.0.1"), 10091); - EXPECT_EQ(rocketmq::socketAddress2IPPort(addr), "1.0.0.127:10091"); + EXPECT_EQ(rocketmq::socketAddress2IPPort(addr), "1.0.0.127:10091"); - int host; - int port; + int host; + int port; - rocketmq::socketAddress2IPPort(addr, host, port); - EXPECT_EQ(host, inet_addr("127.0.0.1")); - EXPECT_EQ(port, 10091); + rocketmq::socketAddress2IPPort(addr, host, port); + EXPECT_EQ(host, inet_addr("127.0.0.1")); + EXPECT_EQ(port, 10091); - EXPECT_EQ(rocketmq::socketAddress2String(addr), "1.0.0.127"); + EXPECT_EQ(rocketmq::socketAddress2String(addr), "1.0.0.127"); } -int main(int argc, char *argv[]) { - InitGoogleMock(&argc, argv); - testing::GTEST_FLAG(throw_on_failure) = true; - testing::GTEST_FLAG(filter) = "socketUtil.init"; - int itestts = RUN_ALL_TESTS(); - return itestts; +int main(int argc, char* argv[]) { + InitGoogleMock(&argc, argv); + testing::GTEST_FLAG(throw_on_failure) = true; + testing::GTEST_FLAG(filter) = "socketUtil.init"; + int itestts = RUN_ALL_TESTS(); + return itestts; } From 0a639e507541558f405b98de4a34ae759cb5d07a Mon Sep 17 00:00:00 2001 From: donggang123 Date: Tue, 9 Apr 2019 19:46:26 +0800 Subject: [PATCH 2/2] Update the format of comment Update the format of comment --- src/producer/DefaultMQProducer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/producer/DefaultMQProducer.cpp b/src/producer/DefaultMQProducer.cpp index f7de8fb88..190c38d42 100644 --- a/src/producer/DefaultMQProducer.cpp +++ b/src/producer/DefaultMQProducer.cpp @@ -460,8 +460,8 @@ SendResult DefaultMQProducer::sendAutoRetrySelectImpl(MQMessage& msg, 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 + 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 {