Skip to content

Commit

Permalink
fix thrift version gt 0.13.0 (#2257)
Browse files Browse the repository at this point in the history
* fix thrift version gt 0.13.0

Signed-off-by: wangyitao <wangyitao999@outlook.com>

* address config_brpc.sh and fix issue

Signed-off-by: wangyitao <wangyitao999@outlook.com>

* make config_brpc.sh silent and make thrift_demo runable

---------

Signed-off-by: wangyitao <wangyitao999@outlook.com>
  • Loading branch information
ZjuYTW authored Sep 7, 2023
1 parent 0a940cd commit 5dc6562
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 7 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ endif()

if(WITH_THRIFT)
set(THRIFT_CPP_FLAG "-DENABLE_THRIFT_FRAMED_PROTOCOL")
set(THRIFT_LIB "thrift")
find_library(THRIFT_LIB NAMES thrift)
if (NOT THRIFT_LIB)
message(FATAL_ERROR "Fail to find Thrift")
endif()
endif()

set(WITH_RDMA_VAL "0")
Expand Down
10 changes: 10 additions & 0 deletions config_brpc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,16 @@ if [ $WITH_THRIFT != 0 ]; then
else
append_to_output "STATIC_LINKINGS+=-lthriftnb"
fi
# get thrift version
thrift_version=$(thrift --version | awk '{print $3}')
major=$(echo "$thrift_version" | awk -F '.' '{print $1}')
minor=$(echo "$thrift_version" | awk -F '.' '{print $2}')
if [ $((major)) -eq 0 -a $((minor)) -lt 11 ]; then
CPPFLAGS="${CPPFLAGS} -D_THRIFT_VERSION_LOWER_THAN_0_11_0_"
echo "less"
else
echo "greater"
fi
fi

if [ $WITH_RDMA != 0 ]; then
Expand Down
5 changes: 4 additions & 1 deletion example/thrift_extension_c++/native_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@
#include <butil/logging.h>

// _THRIFT_STDCXX_H_ is defined by thrift/stdcxx.h which was added since thrift 0.11.0
// but deprecated after 0.13.0
#ifndef THRIFT_STDCXX
#if defined(_THRIFT_STDCXX_H_)
# define THRIFT_STDCXX apache::thrift::stdcxx
#else
#elif defined(_THRIFT_VERSION_LOWER_THAN_0_11_0_)
# define THRIFT_STDCXX boost
# include <boost/make_shared.hpp>
#else
# define THRIFT_STDCXX std
#endif
#endif

Expand Down
21 changes: 17 additions & 4 deletions example/thrift_extension_c++/native_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,23 @@
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TTransportUtils.h>
#include <thrift/server/TNonblockingServer.h>
#include <thrift/concurrency/PosixThreadFactory.h>

// _THRIFT_STDCXX_H_ is defined by thrift/stdcxx.h which was added since thrift 0.11.0
// but deprecated after 0.13.0, PosixThreadFactory was also deprecated in 0.13.0
#include <thrift/TProcessor.h> // to include stdcxx.h if present
#ifndef THRIFT_STDCXX
#if defined(_THRIFT_STDCXX_H_)
# define THRIFT_STDCXX apache::thrift::stdcxx
#include <thrift/transport/TNonblockingServerSocket.h>
#else
#include <thrift/concurrency/PosixThreadFactory.h>
#elif defined(_THRIFT_VERSION_LOWER_THAN_0_11_0_)
# define THRIFT_STDCXX boost
# include <boost/make_shared.hpp>
#include <boost/make_shared.hpp>
#include <thrift/concurrency/PosixThreadFactory.h>
#else
# define THRIFT_STDCXX std
#include <thrift/concurrency/ThreadFactory.h>
#include <thrift/transport/TNonblockingServerSocket.h>
#endif
#endif

Expand All @@ -61,10 +67,17 @@ int main(int argc, char *argv[]) {
google::ParseCommandLineFlags(&argc, &argv, true);

THRIFT_STDCXX::shared_ptr<EchoServiceHandler> handler(new EchoServiceHandler());
#if THRIFT_STDCXX != std
// For thrift version less than 0.13.0
THRIFT_STDCXX::shared_ptr<apache::thrift::concurrency::PosixThreadFactory> thread_factory(
new apache::thrift::concurrency::PosixThreadFactory(
apache::thrift::concurrency::PosixThreadFactory::ROUND_ROBIN,
apache::thrift::concurrency::PosixThreadFactory::NORMAL, 1, false));
#else
// For thrift version greater equal than 0.13.0
THRIFT_STDCXX::shared_ptr<apache::thrift::concurrency::ThreadFactory> thread_factory(
new apache::thrift::concurrency::ThreadFactory(false));
#endif

THRIFT_STDCXX::shared_ptr<apache::thrift::server::TProcessor> processor(
new example::EchoServiceProcessor(handler));
Expand All @@ -79,7 +92,7 @@ int main(int argc, char *argv[]) {

thread_mgr->start();

#if defined(_THRIFT_STDCXX_H_)
#if defined(_THRIFT_STDCXX_H_) || !defined (_THRIFT_VERSION_LOWER_THAN_0_11_0_)
THRIFT_STDCXX::shared_ptr<apache::thrift::transport::TNonblockingServerSocket> server_transport =
THRIFT_STDCXX::make_shared<apache::thrift::transport::TNonblockingServerSocket>(FLAGS_port);

Expand Down
26 changes: 26 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,33 @@ add_library(brpc-static STATIC $<TARGET_OBJECTS:BUTIL_LIB>
$<TARGET_OBJECTS:SOURCES_LIB>
$<TARGET_OBJECTS:PROTO_LIB>)

function(check_thrift_version target_arg)
#use thrift command to get version
execute_process(
COMMAND thrift --version
OUTPUT_VARIABLE THRIFT_VERSION_OUTPUT
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)

string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" THRIFT_VERSION ${THRIFT_VERSION_OUTPUT})
string(REGEX REPLACE "\\." ";" THRIFT_VERSION_LIST ${THRIFT_VERSION})

list(GET THRIFT_VERSION_LIST 0 THRIFT_MAJOR_VERSION)
list(GET THRIFT_VERSION_LIST 1 THRIFT_MINOR_VERSION)

if (THRIFT_MAJOR_VERSION EQUAL 0 AND THRIFT_MINOR_VERSION LESS 11)
message(STATUS "Thrift version is less than 0.11.0")
target_compile_definitions($(target_arg) PRIVATE _THRIFT_VERSION_LOWER_THAN_0_11_0_)
else()
message(STATUS "Thrift version is equal to or greater than 0.11.0")
endif()
endfunction()


if(WITH_THRIFT)
target_link_libraries(brpc-static ${THRIFT_LIB})
check_thrift_version(brpc-static)
endif()

SET_TARGET_PROPERTIES(brpc-static PROPERTIES OUTPUT_NAME brpc CLEAN_DIRECT_OUTPUT 1)
Expand All @@ -60,6 +85,7 @@ if(BUILD_SHARED_LIBS)
endif()
if(WITH_THRIFT)
target_link_libraries(brpc-shared ${THRIFT_LIB})
check_thrift_version(brpc-shared)
endif()
SET_TARGET_PROPERTIES(brpc-shared PROPERTIES OUTPUT_NAME brpc CLEAN_DIRECT_OUTPUT 1)

Expand Down
5 changes: 4 additions & 1 deletion src/brpc/policy/thrift_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@
#include <thrift/TApplicationException.h>

// _THRIFT_STDCXX_H_ is defined by thrift/stdcxx.h which was added since thrift 0.11.0
// but deprecated after thrift 0.13.0
#include <thrift/TProcessor.h> // to include stdcxx.h if present
#ifndef THRIFT_STDCXX
#if defined(_THRIFT_STDCXX_H_)
# define THRIFT_STDCXX apache::thrift::stdcxx
#else
#elif defined(_THRIFT_VERSION_LOWER_THAN_0_11_0_)
# define THRIFT_STDCXX boost
# include <boost/make_shared.hpp>
#else
# define THRIFT_STDCXX std
#endif
#endif

Expand Down

0 comments on commit 5dc6562

Please sign in to comment.