apache / rocketmq-client-cpp

Apache RocketMQ cpp client
https://rocketmq.apache.org/
Apache License 2.0
366 stars 159 forks source link

No route info of this topic Error when sending msg sync in cpp, but everything normal when using the same library from python api. #346

Closed SandyKidYao closed 3 years ago

SandyKidYao commented 3 years ago

The msg producer works with python api but not working in cpp. The error msg is "No route info of this topic: ".

In fact the python api is rely on the cpp client, and it works fine while the cpp codes don't, I hope someone can give a little help with my problem ( I run the example and got the similar error).

The test code (c-style) in cpp like below

int main(int argc, const char *argv[]) {
    CProducer* producer = CreateProducer("Group_producer");
    SetProducerNameServerAddress(producer, "127.0.0.1:9876");
    StartProducer(producer);
    CMessage* msg = CreateMessage("T_TestTopic");
    std::string body = "hello";
    SetMessageBody(msg, body.c_str());
    CSendResult result;
    int status = SendMessageSync(producer, msg, &result);
    if (status == OK) {
        std::cout<<"send msg success " <<result.msgId << " "  << result.offset <<" " << (int)result.sendStatus<<std::endl;

    } else {
        std::cout<<"send msg failed"<<std::endl;
    }
    ShutdownProducer(producer);
    DestroyProducer(producer);
}

And the cpp-style test code

#include<rocketmq/DefaultMQProducer.h>

int main(int argc, const char *argv[]) {

    rocketmq::DefaultMQProducer producer("please_rename_unique_group_name");
    producer.setNamesrvAddr("127.0.0.1:9876");
    producer.start();
    rocketmq::MQMessage msg("channel_test", "{\"channel\": \"channel_001\"}");
    producer.send(msg);
    producer.shutdown();

}

The test code in python works fine, like this.

from rocketmq.client import Producer, Message

producer = Producer('PID-XXX')
producer.set_name_server_address(HOST)
producer.start()
msg = Message(TOPIC)
msg.set_body("hello")
ret = producer.send_sync(msg)
print(ret.status, ret.msg_id, ret.offset)
producer.shutdown()

The logs generated by cpp codes are a little different from the ones by python code. There are lots of warnings. I tried to figure out what happened but got noting valuable.

[2021-Feb-03 09:00:20.043586](warning):waitResponse of code:105 with opaque:8 timeout[waitResponse:61]
[2021-Feb-03 09:00:20.043956](warning):wait response timeout or get NULL response of code:105, so closeTransport of addr:[invokeSync:217]
[2021-Feb-03 09:00:20.044166](warning):closeTransport: disconnect:127.0.0.1:9876 with state:2[CloseTransport:429]
[2021-Feb-03 09:00:20.044593](warning):closeTransport: erase broker: 127.0.0.1:9876[CloseTransport:432]
[2021-Feb-03 09:00:20.044931](warning):Get topic[T_TestTopic] route failed [Null Response].[getTopicRouteInfoFromNameServer:338]
[2021-Feb-03 09:00:25.047442](warning):waitResponse of code:105 with opaque:9 timeout[waitResponse:61]
[2021-Feb-03 09:00:25.047591](warning):wait response timeout or get NULL response of code:105, so closeTransport of addr:[invokeSync:217]
[2021-Feb-03 09:00:25.047664](warning):closeTransport: disconnect:127.0.0.1:9876 with state:2[CloseTransport:429]
[2021-Feb-03 09:00:25.047908](warning):closeTransport: erase broker: 127.0.0.1:9876[CloseTransport:432]
[2021-Feb-03 09:00:25.048088](warning):Get topic[TBW102] route failed [Null Response].[getTopicRouteInfoFromNameServer:338]
[2021-Feb-03 09:00:25.048147](warning):tryToFindTopicPublishInfo null:T_TestTopic[tryToFindTopicPublishInfo:155]
[2021-Feb-03 09:00:25.048178](warning):Retry many times, still failed[sendDefaultImpl:431]
[2021-Feb-03 09:00:30.049733](warning):waitResponse of code:105 with opaque:10 timeout[waitResponse:61]
[2021-Feb-03 09:00:30.049868](warning):wait response timeout or get NULL response of code:105, so closeTransport of addr:[invokeSync:217]
[2021-Feb-03 09:00:30.049935](warning):closeTransport: disconnect:127.0.0.1:9876 with state:2[CloseTransport:429]
[2021-Feb-03 09:00:30.050166](warning):closeTransport: erase broker: 127.0.0.1:9876[CloseTransport:432]
[2021-Feb-03 09:00:30.050240](warning):Get topic[T_TestTopic] route failed [Null Response].[getTopicRouteInfoFromNameServer:338]
[2021-Feb-03 09:00:35.029496](warning):sendheartbeat brokeradd is empty[sendHeartbeatToAllBroker:772]
[2021-Feb-03 09:00:35.051976](warning):waitResponse of code:105 with opaque:11 timeout[waitResponse:61]
[2021-Feb-03 09:00:35.052085](warning):wait response timeout or get NULL response of code:105, so closeTransport of addr:[invokeSync:217]
[2021-Feb-03 09:00:35.052151](warning):closeTransport: disconnect:127.0.0.1:9876 with state:2[CloseTransport:429]
[2021-Feb-03 09:00:35.052344](warning):closeTransport: erase broker: 127.0.0.1:9876[CloseTransport:432]
[2021-Feb-03 09:00:35.052410](warning):Get topic[TBW102] route failed [Null Response].[getTopicRouteInfoFromNameServer:338]
[2021-Feb-03 09:00:35.052447](warning):tryToFindTopicPublishInfo null:T_TestTopic[tryToFindTopicPublishInfo:155]
[2021-Feb-03 09:00:35.052475](warning):Retry many times, still failed[sendDefaultImpl:431]
ifplusor commented 3 years ago

@SandyKidYao Can you show more logs? or test the re_dev branch.

SandyKidYao commented 3 years ago

Thx for reply. These are the logs I copied from the /root/logs/rocketmq-cpp folder.

13390_rocketmq-cpp.log 13128_rocketmq-cpp.log 13192_rocketmq-cpp.log

BTW, when I use the cpp test codes (both cpp-style and c-style), the log from rocket mq nameserver shows that they have connected to the nameserver while the log from the broker print nothing. I guess there may be sth wrong in the connection between my cpp program and the broker.

ifplusor commented 3 years ago

@SandyKidYao Can you compile cpp sdk from source?

SandyKidYao commented 3 years ago

I figured out what's wrong in my codes. If I link the librocketmq.so from CMake by TARGET_LINK_LIBRARIES(), the route error will happen. However, if I add the same lib (no matter installed via rpm or built from source code) through CMAKE_CXX_FLAGS, everything works fine.

THX for your time again.

ifplusor commented 3 years ago

It is crazy. In my opinion, TARGET_LINK_LIBRARIES is correct way, and it is better then use CMAKE_CXX_FLAGS.

SandyKidYao commented 3 years ago

It is crazy. In my opinion, TARGET_LINK_LIBRARIES is correct way, and it is better then use CMAKE_CXX_FLAGS.

While I am using other libs in my program too, it may be related to the link order between these libs.

ifplusor commented 3 years ago

@SandyKidYao The re_dev branch is recommend to use, you can test it.