From 50381fc8706353d6f580de843b3a57387b9cb85a Mon Sep 17 00:00:00 2001 From: Zike Yang Date: Tue, 28 Feb 2023 11:26:51 +0800 Subject: [PATCH 1/3] [improve] Refactor client version format ## Motivation Currently, the client_version only shows the version information. And clients in different languages use their own separate version numbers. This can lead to conflict. For example, the java client 2.10.2 uses the same value 2.10.2 as the C++ client 2.10.2. And C++ client 3.0.0 may conflict with the future java client 3.0.0. The information of client_version is incomplete. We could not determine what language(or specific library) the client uses. This raises inconvenience for debugging. Please see the discussion: https://lists.apache.org/thread/n59k537fhthjnzkfxtc2p4zk4l0cv3mp ## Modification * Change the client version value to Pulsar-C++-vXXX --- lib/Commands.cc | 4 ++-- tests/ClientTest.cc | 33 +++++++++++++++++++++++++++++++++ tests/HttpHelper.cc | 25 +++++++++++++++++++++---- tests/HttpHelper.h | 1 + 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/lib/Commands.cc b/lib/Commands.cc index bfd9a469..76fb6657 100644 --- a/lib/Commands.cc +++ b/lib/Commands.cc @@ -266,7 +266,7 @@ SharedBuffer Commands::newConnect(const AuthenticationPtr& authentication, const BaseCommand cmd; cmd.set_type(BaseCommand::CONNECT); CommandConnect* connect = cmd.mutable_connect(); - connect->set_client_version(PULSAR_VERSION_STR); + connect->set_client_version(std::string("Pulsar-C++-v") + PULSAR_VERSION_STR); connect->set_auth_method_name(authentication->getAuthMethodName()); connect->set_protocol_version(ProtocolVersion_MAX); @@ -294,7 +294,7 @@ SharedBuffer Commands::newAuthResponse(const AuthenticationPtr& authentication, BaseCommand cmd; cmd.set_type(BaseCommand::AUTH_RESPONSE); CommandAuthResponse* authResponse = cmd.mutable_authresponse(); - authResponse->set_client_version(PULSAR_VERSION_STR); + authResponse->set_client_version(std::string("Pulsar-C++-v") + PULSAR_VERSION_STR); AuthData* authData = authResponse->mutable_response(); authData->set_auth_method_name(authentication->getAuthMethodName()); diff --git a/tests/ClientTest.cc b/tests/ClientTest.cc index 07fe22fd..a987d2fc 100644 --- a/tests/ClientTest.cc +++ b/tests/ClientTest.cc @@ -18,6 +18,7 @@ */ #include #include +#include #include #include @@ -34,6 +35,7 @@ DECLARE_LOG_OBJECT() using namespace pulsar; static std::string lookupUrl = "pulsar://localhost:6650"; +static std::string adminUrl = "http://localhost:8080/"; TEST(ClientTest, testChecksumComputation) { std::string data = "test"; @@ -308,3 +310,34 @@ TEST(ClientTest, testCloseClient) { client.close(); } } + +TEST(ClientTest, testClientVersion) { + const std::string topic = "testClientVersion" + std::to_string(time(nullptr)); + const std::string expectedVersion = std::string("Pulsar-C++-v") + PULSAR_VERSION_STR; + + Client client(lookupUrl); + + std::string responseData; + + Producer producer; + Result result = client.createProducer(topic, producer); + ASSERT_EQ(ResultOk, result); + int res = + makeGetRequest(adminUrl + "admin/v2/persistent/public/default/" + topic + "/stats", responseData); + ASSERT_TRUE(res == 200) << "res: " << res; + + ASSERT_TRUE(responseData.find(expectedVersion) != std::string::npos); + producer.close(); + + responseData.clear(); + Consumer consumer; + result = client.subscribe(topic, "consumer-1", consumer); + ASSERT_EQ(ResultOk, result); + res = makeGetRequest(adminUrl + "admin/v2/persistent/public/default/" + topic + "/stats", responseData); + ASSERT_TRUE(res == 200) << "res: " << res; + + ASSERT_TRUE(responseData.find(expectedVersion) != std::string::npos); + consumer.close(); + + client.close(); +} diff --git a/tests/HttpHelper.cc b/tests/HttpHelper.cc index c4118e6e..69d59908 100644 --- a/tests/HttpHelper.cc +++ b/tests/HttpHelper.cc @@ -20,7 +20,13 @@ #include -static int makeRequest(const std::string& method, const std::string& url, const std::string& body) { +static size_t curlWriteCallback(void* contents, size_t size, size_t nmemb, void* responseDataPtr) { + ((std::string*)responseDataPtr)->append((char*)contents, size * nmemb); + return size * nmemb; +} + +static int makeRequest(const std::string& method, const std::string& url, const std::string& body, + const std::string& responseData) { CURL* curl = curl_easy_init(); struct curl_slist* list = NULL; @@ -33,6 +39,11 @@ static int makeRequest(const std::string& method, const std::string& url, const if (!body.empty()) { curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str()); } + + // Write callback + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteCallback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseData); + int res = curl_easy_perform(curl); curl_slist_free_all(list); /* free the list again */ @@ -46,10 +57,16 @@ static int makeRequest(const std::string& method, const std::string& url, const return (int)httpResult; } -int makePutRequest(const std::string& url, const std::string& body) { return makeRequest("PUT", url, body); } +int makePutRequest(const std::string& url, const std::string& body) { + return makeRequest("PUT", url, body, ""); +} int makePostRequest(const std::string& url, const std::string& body) { - return makeRequest("POST", url, body); + return makeRequest("POST", url, body, ""); } -int makeDeleteRequest(const std::string& url) { return makeRequest("DELETE", url, ""); } +int makeDeleteRequest(const std::string& url) { return makeRequest("DELETE", url, "", ""); } + +int makeGetRequest(const std::string& url, const std::string& responseData) { + return makeRequest("GET", url, "", responseData); +} diff --git a/tests/HttpHelper.h b/tests/HttpHelper.h index 68119a79..1a03ac51 100644 --- a/tests/HttpHelper.h +++ b/tests/HttpHelper.h @@ -24,5 +24,6 @@ int makePutRequest(const std::string& url, const std::string& body); int makePostRequest(const std::string& url, const std::string& body); int makeDeleteRequest(const std::string& url); +int makeGetRequest(const std::string& url, const std::string& responseData); #endif /* end of include guard: HTTP_HELPER */ From d03a37f7ea25892438df0a8f46091e49da00853d Mon Sep 17 00:00:00 2001 From: Zike Yang Date: Wed, 1 Mar 2023 11:24:17 +0800 Subject: [PATCH 2/3] Change format --- lib/Commands.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Commands.cc b/lib/Commands.cc index 76fb6657..46346d1d 100644 --- a/lib/Commands.cc +++ b/lib/Commands.cc @@ -266,7 +266,7 @@ SharedBuffer Commands::newConnect(const AuthenticationPtr& authentication, const BaseCommand cmd; cmd.set_type(BaseCommand::CONNECT); CommandConnect* connect = cmd.mutable_connect(); - connect->set_client_version(std::string("Pulsar-C++-v") + PULSAR_VERSION_STR); + connect->set_client_version(std::string("Pulsar-CPP-v") + PULSAR_VERSION_STR); connect->set_auth_method_name(authentication->getAuthMethodName()); connect->set_protocol_version(ProtocolVersion_MAX); From b29b6c4822fd2b988bc736e8550ecf2cec573622 Mon Sep 17 00:00:00 2001 From: Zike Yang Date: Wed, 1 Mar 2023 11:44:30 +0800 Subject: [PATCH 3/3] Fix test --- lib/Commands.cc | 2 +- tests/ClientTest.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Commands.cc b/lib/Commands.cc index 46346d1d..a04a386e 100644 --- a/lib/Commands.cc +++ b/lib/Commands.cc @@ -294,7 +294,7 @@ SharedBuffer Commands::newAuthResponse(const AuthenticationPtr& authentication, BaseCommand cmd; cmd.set_type(BaseCommand::AUTH_RESPONSE); CommandAuthResponse* authResponse = cmd.mutable_authresponse(); - authResponse->set_client_version(std::string("Pulsar-C++-v") + PULSAR_VERSION_STR); + authResponse->set_client_version(std::string("Pulsar-CPP-v") + PULSAR_VERSION_STR); AuthData* authData = authResponse->mutable_response(); authData->set_auth_method_name(authentication->getAuthMethodName()); diff --git a/tests/ClientTest.cc b/tests/ClientTest.cc index a987d2fc..d59633f3 100644 --- a/tests/ClientTest.cc +++ b/tests/ClientTest.cc @@ -313,7 +313,7 @@ TEST(ClientTest, testCloseClient) { TEST(ClientTest, testClientVersion) { const std::string topic = "testClientVersion" + std::to_string(time(nullptr)); - const std::string expectedVersion = std::string("Pulsar-C++-v") + PULSAR_VERSION_STR; + const std::string expectedVersion = std::string("Pulsar-CPP-v") + PULSAR_VERSION_STR; Client client(lookupUrl);