From 1b3af699c94ff2b884c7dfcece7b428d280f239b Mon Sep 17 00:00:00 2001 From: Matteo Merli Date: Fri, 5 Feb 2021 17:04:23 -0800 Subject: [PATCH 1/4] [C++] Removed usages of boost::regex --- pulsar-client-cpp/CMakeLists.txt | 15 +++++++-- pulsar-client-cpp/lib/NamedEntity.cc | 18 +++++++++-- pulsar-client-cpp/lib/NamedEntity.h | 9 ++---- pulsar-client-cpp/lib/Url.cc | 18 ++++++++--- pulsar-client-cpp/lib/auth/AuthAthenz.cc | 10 ------ .../lib/auth/athenz/ZTSClient.cc | 31 ++++++++++++++----- pulsar-client-cpp/python/CMakeLists.txt | 9 ------ pulsar-client-cpp/tests/ZTSClientTest.cc | 2 +- 8 files changed, 68 insertions(+), 44 deletions(-) diff --git a/pulsar-client-cpp/CMakeLists.txt b/pulsar-client-cpp/CMakeLists.txt index 208069ee1f41d..7501c85899123 100644 --- a/pulsar-client-cpp/CMakeLists.txt +++ b/pulsar-client-cpp/CMakeLists.txt @@ -17,7 +17,7 @@ # under the License. # -cmake_minimum_required(VERSION 3.4) +cmake_minimum_required(VERSION 3.4) project (pulsar-cpp) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake_modules") @@ -155,7 +155,7 @@ endif (LINK_STATIC) find_package(Boost) -set(BOOST_COMPONENTS program_options regex) +set(BOOST_COMPONENTS program_options) if (NOT Boost_VERSION_MAJOR OR (Boost_VERSION_MAJOR EQUAL 1 AND Boost_VERSION_MINOR LESS 69) @@ -168,6 +168,15 @@ if (MSVC) set(BOOST_COMPONENTS ${BOOST_COMPONENTS} date_time) endif() +if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9) + # GCC 4.8.2 implementation of std::regex is buggy + set(BOOST_COMPONENTS ${BOOST_COMPONENTS} regex) + set(CMAKE_CXX_FLAGS " -DPULSAR_USE_BOOST_REGEX") + MESSAGE(STATUS "Using Boost::Regex") +else() + MESSAGE(STATUS "Using std::regex") +endif() + find_package(Boost REQUIRED COMPONENTS ${BOOST_COMPONENTS}) if (BUILD_PYTHON_WRAPPER) @@ -286,7 +295,7 @@ if (NOT MSVC) ) else() set(COMMON_LIBS - ${COMMON_LIBS} + ${COMMON_LIBS} wldap32.lib Normaliz.lib) endif() diff --git a/pulsar-client-cpp/lib/NamedEntity.cc b/pulsar-client-cpp/lib/NamedEntity.cc index cd68e5898eda9..3890667c0704c 100644 --- a/pulsar-client-cpp/lib/NamedEntity.cc +++ b/pulsar-client-cpp/lib/NamedEntity.cc @@ -18,8 +18,20 @@ */ #include "NamedEntity.h" -const boost::regex NamedEntity::pattern = boost::regex("^[-=:.\\w]*$"); - bool NamedEntity::checkName(const std::string& name) { - return boost::regex_match(name, pattern) ? true : false; + for (char c : name) { + switch (c) { + case '=': + case ':': + case ' ': + case '\t': + case '\r': + case '\n': + return false; + default: + break; + } + } + + return true; } diff --git a/pulsar-client-cpp/lib/NamedEntity.h b/pulsar-client-cpp/lib/NamedEntity.h index 44376026263b8..14b73d6069ca7 100644 --- a/pulsar-client-cpp/lib/NamedEntity.h +++ b/pulsar-client-cpp/lib/NamedEntity.h @@ -16,16 +16,11 @@ * specific language governing permissions and limitations * under the License. */ -#ifndef _PULSAR_NAMED_ENTITY_HEADER_ -#define _PULSAR_NAMED_ENTITY_HEADER_ +#pragma once -#include +#include class NamedEntity { - private: - static const boost::regex pattern; - public: static bool checkName(const std::string& name); }; -#endif diff --git a/pulsar-client-cpp/lib/Url.cc b/pulsar-client-cpp/lib/Url.cc index 0924652ee4424..6d9c4c4cc9b2f 100644 --- a/pulsar-client-cpp/lib/Url.cc +++ b/pulsar-client-cpp/lib/Url.cc @@ -18,9 +18,19 @@ */ #include "Url.h" -#include +#include + #include +#ifdef PULSAR_USE_BOOST_REGEX +# include +# define PULSAR_REGEX_NAMESPACE boost +#else +# include +# define PULSAR_REGEX_NAMESPACE std +#endif + + namespace pulsar { static const std::map initDefaultPortsMap() { @@ -39,14 +49,14 @@ static const std::map& defaultPortsMap() { bool Url::parse(const std::string& urlStr, Url& url) { std::vector values; - static const boost::regex expression( + static const PULSAR_REGEX_NAMESPACE::regex expression( // proto host port "^(\?:([^:/\?#]+)://)\?(\\w+[^/\?#:]*)(\?::(\\d+))\?" // path file parameters "(/\?(\?:[^\?#/]*/)*)\?([^\?#]*)\?(\\\?(.*))\?"); - boost::cmatch groups; - if (!boost::regex_match(urlStr.c_str(), groups, expression)) { + PULSAR_REGEX_NAMESPACE::cmatch groups; + if (!PULSAR_REGEX_NAMESPACE::regex_match(urlStr.c_str(), groups, expression)) { // Invalid url return false; } diff --git a/pulsar-client-cpp/lib/auth/AuthAthenz.cc b/pulsar-client-cpp/lib/auth/AuthAthenz.cc index 3141fb30d16c1..82d12761c56c7 100644 --- a/pulsar-client-cpp/lib/auth/AuthAthenz.cc +++ b/pulsar-client-cpp/lib/auth/AuthAthenz.cc @@ -18,16 +18,6 @@ */ #include -#include -#include - -#include -#include -#include -#include - -#include - #include #include namespace ptree = boost::property_tree; diff --git a/pulsar-client-cpp/lib/auth/athenz/ZTSClient.cc b/pulsar-client-cpp/lib/auth/athenz/ZTSClient.cc index 045f9188e9db7..34c286ac1f8e4 100644 --- a/pulsar-client-cpp/lib/auth/athenz/ZTSClient.cc +++ b/pulsar-client-cpp/lib/auth/athenz/ZTSClient.cc @@ -38,13 +38,18 @@ #include namespace ptree = boost::property_tree; -#include #include #include #include #include +#ifdef PULSAR_USE_BOOST_REGEX +# include +#else +# include +#endif + DECLARE_LOG_OBJECT() namespace pulsar { @@ -360,15 +365,27 @@ const std::string ZTSClient::getHeader() const { return roleHeader_; } PrivateKeyUri ZTSClient::parseUri(const char *uri) { PrivateKeyUri uriSt; // scheme mediatype[;base64] path file - static const boost::regex expression( - "^(\?:([^:/\?#]+):)(\?:([;/\\-\\w]*),)\?(/\?(\?:[^\?#/]*/)*)\?([^\?#]*)"); - boost::cmatch groups; - if (boost::regex_match(uri, groups, expression)) { + +#ifdef PULSAR_USE_BOOST_REGEX + static const boost::regex expression( + "^(\?:([^:/\?#]+):)(\?:([;/\\-\\w]*),)\?(/\?(\?:[^\?#/]*/)*)\?([^\?#]*)"); + boost::cmatch groups; + if (boost::regex_match(uri, groups, expression)) { + uriSt.scheme = groups.str(1); + uriSt.mediaTypeAndEncodingType = groups.str(2); + uriSt.data = groups.str(4); + } +#else // !PULSAR_USE_BOOST_REGEX + static const std::regex expression( + R"(^(?:([A-Za-z]+):)(?:([/\w\-]+;\w+),([=\w]+))?(?:\/\/)?(\/[^?#]+)?)"); + std::cmatch groups; + if (std::regex_match(uri, groups, expression)) { uriSt.scheme = groups.str(1); uriSt.mediaTypeAndEncodingType = groups.str(2); - uriSt.data = groups.str(4); - uriSt.path = groups.str(3) + groups.str(4); + uriSt.data = groups.str(3); + uriSt.path = groups.str(4); } +#endif //PULSAR_USE_BOOST_REGEX return uriSt; } } // namespace pulsar diff --git a/pulsar-client-cpp/python/CMakeLists.txt b/pulsar-client-cpp/python/CMakeLists.txt index 70b5bc130ae74..e343fd9d47fed 100644 --- a/pulsar-client-cpp/python/CMakeLists.txt +++ b/pulsar-client-cpp/python/CMakeLists.txt @@ -59,15 +59,6 @@ if (APPLE) ${Boost_PYTHON27-MT_LIBRARY_RELEASE} ${Boost_PYTHON37-MT_LIBRARY_RELEASE} ${Boost_PYTHON38-MT_LIBRARY_RELEASE}) - - if (LINK_STATIC) - # When linking statically on MacOS, include also libicu since it's now required by boost::regex - find_library(ICU_DATA REQUIRED NAMES libicudata.a PATHS /usr/local/opt/icu4c/lib) - find_library(ICU_I18N REQUIRED NAMES libicui18n.a PATHS /usr/local/opt/icu4c/lib) - find_library(ICU_UUC REQUIRED NAMES libicuuc.a PATHS /usr/local/opt/icu4c/lib) - - set(ICU_LIBS ${ICU_DATA} ${ICU_I18N} ${ICU_UUC}) - endif () endif() message(STATUS "Using Boost Python libs: ${PYTHON_WRAPPER_LIBS}") diff --git a/pulsar-client-cpp/tests/ZTSClientTest.cc b/pulsar-client-cpp/tests/ZTSClientTest.cc index 9eccf17eccd79..01c2e03381668 100644 --- a/pulsar-client-cpp/tests/ZTSClientTest.cc +++ b/pulsar-client-cpp/tests/ZTSClientTest.cc @@ -39,7 +39,7 @@ TEST(ZTSClientTest, testZTSClient) { { PrivateKeyUri uri = ZTSClientWrapper::parseUri("file:///path/to/private.key"); ASSERT_EQ("file", uri.scheme); - ASSERT_EQ("///path/to/private.key", uri.path); + ASSERT_EQ("/path/to/private.key", uri.path); } { From b1611001286ebd0cd995cb66054670b227f1336b Mon Sep 17 00:00:00 2001 From: Matteo Merli Date: Mon, 8 Feb 2021 13:47:53 -0800 Subject: [PATCH 2/4] Fixed formatting --- pulsar-client-cpp/lib/Url.cc | 9 ++++--- .../lib/auth/athenz/ZTSClient.cc | 24 +++++++++---------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/pulsar-client-cpp/lib/Url.cc b/pulsar-client-cpp/lib/Url.cc index 6d9c4c4cc9b2f..f31e1fcc186de 100644 --- a/pulsar-client-cpp/lib/Url.cc +++ b/pulsar-client-cpp/lib/Url.cc @@ -23,14 +23,13 @@ #include #ifdef PULSAR_USE_BOOST_REGEX -# include -# define PULSAR_REGEX_NAMESPACE boost +#include +#define PULSAR_REGEX_NAMESPACE boost #else -# include -# define PULSAR_REGEX_NAMESPACE std +#include +#define PULSAR_REGEX_NAMESPACE std #endif - namespace pulsar { static const std::map initDefaultPortsMap() { diff --git a/pulsar-client-cpp/lib/auth/athenz/ZTSClient.cc b/pulsar-client-cpp/lib/auth/athenz/ZTSClient.cc index 34c286ac1f8e4..68a0b8070cec0 100644 --- a/pulsar-client-cpp/lib/auth/athenz/ZTSClient.cc +++ b/pulsar-client-cpp/lib/auth/athenz/ZTSClient.cc @@ -45,9 +45,9 @@ namespace ptree = boost::property_tree; #include #ifdef PULSAR_USE_BOOST_REGEX -# include +#include #else -# include +#include #endif DECLARE_LOG_OBJECT() @@ -367,15 +367,15 @@ PrivateKeyUri ZTSClient::parseUri(const char *uri) { // scheme mediatype[;base64] path file #ifdef PULSAR_USE_BOOST_REGEX - static const boost::regex expression( - "^(\?:([^:/\?#]+):)(\?:([;/\\-\\w]*),)\?(/\?(\?:[^\?#/]*/)*)\?([^\?#]*)"); - boost::cmatch groups; - if (boost::regex_match(uri, groups, expression)) { - uriSt.scheme = groups.str(1); - uriSt.mediaTypeAndEncodingType = groups.str(2); - uriSt.data = groups.str(4); - } -#else // !PULSAR_USE_BOOST_REGEX + static const boost::regex expression( + "^(\?:([^:/\?#]+):)(\?:([;/\\-\\w]*),)\?(/\?(\?:[^\?#/]*/)*)\?([^\?#]*)"); + boost::cmatch groups; + if (boost::regex_match(uri, groups, expression)) { + uriSt.scheme = groups.str(1); + uriSt.mediaTypeAndEncodingType = groups.str(2); + uriSt.data = groups.str(4); + } +#else // !PULSAR_USE_BOOST_REGEX static const std::regex expression( R"(^(?:([A-Za-z]+):)(?:([/\w\-]+;\w+),([=\w]+))?(?:\/\/)?(\/[^?#]+)?)"); std::cmatch groups; @@ -385,7 +385,7 @@ PrivateKeyUri ZTSClient::parseUri(const char *uri) { uriSt.data = groups.str(3); uriSt.path = groups.str(4); } -#endif //PULSAR_USE_BOOST_REGEX +#endif // PULSAR_USE_BOOST_REGEX return uriSt; } } // namespace pulsar From f2267a73a5ccf5ec78d4d7f7d37f08dc38f9ba57 Mon Sep 17 00:00:00 2001 From: Matteo Merli Date: Mon, 8 Feb 2021 16:03:06 -0800 Subject: [PATCH 3/4] Fixed test --- pulsar-client-cpp/CMakeLists.txt | 12 ++++++++++-- pulsar-client-cpp/lib/NamedEntity.cc | 1 + pulsar-client-cpp/lib/PartitionedConsumerImpl.cc | 9 ++++++--- pulsar-client-cpp/tests/ConsumerConfigurationTest.cc | 3 +-- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/pulsar-client-cpp/CMakeLists.txt b/pulsar-client-cpp/CMakeLists.txt index 7501c85899123..913a0d29aa9fb 100644 --- a/pulsar-client-cpp/CMakeLists.txt +++ b/pulsar-client-cpp/CMakeLists.txt @@ -35,6 +35,9 @@ MESSAGE(STATUS "BUILD_TESTS: " ${BUILD_TESTS}) option(BUILD_PYTHON_WRAPPER "Build Pulsar Python wrapper" ON) MESSAGE(STATUS "BUILD_PYTHON_WRAPPER: " ${BUILD_PYTHON_WRAPPER}) +option(BUILD_PERF_TOOLS "Build Pulsar CLI perf producer/consumer" OFF) +MESSAGE(STATUS "BUILD_PERF_TOOLS: " ${BUILD_PERF_TOOLS}) + option(LINK_STATIC "Link against static libraries" OFF) MESSAGE(STATUS "LINK_STATIC: " ${LINK_STATIC}) @@ -155,7 +158,6 @@ endif (LINK_STATIC) find_package(Boost) -set(BOOST_COMPONENTS program_options) if (NOT Boost_VERSION_MAJOR OR (Boost_VERSION_MAJOR EQUAL 1 AND Boost_VERSION_MINOR LESS 69) @@ -177,6 +179,10 @@ else() MESSAGE(STATUS "Using std::regex") endif() +if(BUILD_PERF_TOOLS) + set(BOOST_COMPONENTS ${BOOST_COMPONENTS} program_options) +endif() + find_package(Boost REQUIRED COMPONENTS ${BOOST_COMPONENTS}) if (BUILD_PYTHON_WRAPPER) @@ -340,7 +346,9 @@ set(CLIENT_LIBS ) add_subdirectory(lib) -add_subdirectory(perf) +if(BUILD_PERF_TOOLS) + add_subdirectory(perf) +endif(BUILD_PERF_TOOLS) add_subdirectory(examples) if (BUILD_TESTS) diff --git a/pulsar-client-cpp/lib/NamedEntity.cc b/pulsar-client-cpp/lib/NamedEntity.cc index 3890667c0704c..ad7c385c6fe43 100644 --- a/pulsar-client-cpp/lib/NamedEntity.cc +++ b/pulsar-client-cpp/lib/NamedEntity.cc @@ -24,6 +24,7 @@ bool NamedEntity::checkName(const std::string& name) { case '=': case ':': case ' ': + case '!': case '\t': case '\r': case '\n': diff --git a/pulsar-client-cpp/lib/PartitionedConsumerImpl.cc b/pulsar-client-cpp/lib/PartitionedConsumerImpl.cc index 3a43a0bc6b7be..11b27445f230c 100644 --- a/pulsar-client-cpp/lib/PartitionedConsumerImpl.cc +++ b/pulsar-client-cpp/lib/PartitionedConsumerImpl.cc @@ -229,7 +229,8 @@ ConsumerConfiguration PartitionedConsumerImpl::getSinglePartitionConsumerConfig( config.setBrokerConsumerStatsCacheTimeInMs(conf_.getBrokerConsumerStatsCacheTimeInMs()); const auto shared_this = const_cast(this)->shared_from_this(); - config.setMessageListener(std::bind(&PartitionedConsumerImpl::messageReceived, shared_this, _1, _2)); + config.setMessageListener(std::bind(&PartitionedConsumerImpl::messageReceived, shared_this, + std::placeholders::_1, std::placeholders::_2)); // Apply total limit of receiver queue size across partitions // NOTE: if it's called by handleGetPartitions(), the queue size of new internal consumers may be smaller @@ -251,7 +252,8 @@ ConsumerImplPtr PartitionedConsumerImpl::newInternalConsumer(unsigned int partit const auto shared_this = const_cast(this)->shared_from_this(); consumer->getConsumerCreatedFuture().addListener(std::bind( - &PartitionedConsumerImpl::handleSinglePartitionConsumerCreated, shared_this, _1, _2, partition)); + &PartitionedConsumerImpl::handleSinglePartitionConsumerCreated, shared_this, std::placeholders::_1, + std::placeholders::_2, partition)); consumer->setPartitionIndex(partition); LOG_DEBUG("Creating Consumer for single Partition - " << topicPartitionName << "SubName - " @@ -573,7 +575,8 @@ void PartitionedConsumerImpl::runPartitionUpdateTask() { void PartitionedConsumerImpl::getPartitionMetadata() { using namespace std::placeholders; lookupServicePtr_->getPartitionMetadataAsync(topicName_) - .addListener(std::bind(&PartitionedConsumerImpl::handleGetPartitions, shared_from_this(), _1, _2)); + .addListener(std::bind(&PartitionedConsumerImpl::handleGetPartitions, shared_from_this(), + std::placeholders::_1, std::placeholders::_2)); } void PartitionedConsumerImpl::handleGetPartitions(Result result, diff --git a/pulsar-client-cpp/tests/ConsumerConfigurationTest.cc b/pulsar-client-cpp/tests/ConsumerConfigurationTest.cc index 42e84534f1c87..ced72e4410100 100644 --- a/pulsar-client-cpp/tests/ConsumerConfigurationTest.cc +++ b/pulsar-client-cpp/tests/ConsumerConfigurationTest.cc @@ -18,12 +18,11 @@ */ #include #include -#include -#include #include DECLARE_LOG_OBJECT() + #include "../lib/Future.h" #include "../lib/Utils.h" From 99493654b2e78a73922e89fe60bd1266a6cd62fc Mon Sep 17 00:00:00 2001 From: Matteo Merli Date: Mon, 8 Feb 2021 16:22:46 -0800 Subject: [PATCH 4/4] Fixed formatting --- pulsar-client-cpp/lib/PartitionedConsumerImpl.cc | 6 +++--- pulsar-client-cpp/tests/ConsumerConfigurationTest.cc | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/pulsar-client-cpp/lib/PartitionedConsumerImpl.cc b/pulsar-client-cpp/lib/PartitionedConsumerImpl.cc index 11b27445f230c..8881a0db9531c 100644 --- a/pulsar-client-cpp/lib/PartitionedConsumerImpl.cc +++ b/pulsar-client-cpp/lib/PartitionedConsumerImpl.cc @@ -251,9 +251,9 @@ ConsumerImplPtr PartitionedConsumerImpl::newInternalConsumer(unsigned int partit internalListenerExecutor_, true, Partitioned); const auto shared_this = const_cast(this)->shared_from_this(); - consumer->getConsumerCreatedFuture().addListener(std::bind( - &PartitionedConsumerImpl::handleSinglePartitionConsumerCreated, shared_this, std::placeholders::_1, - std::placeholders::_2, partition)); + consumer->getConsumerCreatedFuture().addListener( + std::bind(&PartitionedConsumerImpl::handleSinglePartitionConsumerCreated, shared_this, + std::placeholders::_1, std::placeholders::_2, partition)); consumer->setPartitionIndex(partition); LOG_DEBUG("Creating Consumer for single Partition - " << topicPartitionName << "SubName - " diff --git a/pulsar-client-cpp/tests/ConsumerConfigurationTest.cc b/pulsar-client-cpp/tests/ConsumerConfigurationTest.cc index ced72e4410100..379bcdca75c23 100644 --- a/pulsar-client-cpp/tests/ConsumerConfigurationTest.cc +++ b/pulsar-client-cpp/tests/ConsumerConfigurationTest.cc @@ -22,7 +22,6 @@ DECLARE_LOG_OBJECT() - #include "../lib/Future.h" #include "../lib/Utils.h"