You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by ww...@apache.org on 2023/09/07 09:00:14 UTC

[brpc] branch master updated: fix thrift version gt 0.13.0 (#2257)

This is an automated email from the ASF dual-hosted git repository.

wwbmmm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new 5dc65620 fix thrift version gt 0.13.0 (#2257)
5dc65620 is described below

commit 5dc6562049390ac2e25c044f5b975b01022b80f1
Author: Yitao Wang <48...@users.noreply.github.com>
AuthorDate: Thu Sep 7 17:00:10 2023 +0800

    fix thrift version gt 0.13.0 (#2257)
    
    * fix thrift version gt 0.13.0
    
    Signed-off-by: wangyitao <wa...@outlook.com>
    
    * address config_brpc.sh and fix issue
    
    Signed-off-by: wangyitao <wa...@outlook.com>
    
    * make config_brpc.sh silent and make thrift_demo runable
    
    ---------
    
    Signed-off-by: wangyitao <wa...@outlook.com>
---
 CMakeLists.txt                                 |  5 ++++-
 config_brpc.sh                                 | 10 ++++++++++
 example/thrift_extension_c++/native_client.cpp |  5 ++++-
 example/thrift_extension_c++/native_server.cpp | 21 +++++++++++++++++----
 src/CMakeLists.txt                             | 26 ++++++++++++++++++++++++++
 src/brpc/policy/thrift_protocol.cpp            |  5 ++++-
 6 files changed, 65 insertions(+), 7 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 85881a2e..66cc0157 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -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")
diff --git a/config_brpc.sh b/config_brpc.sh
index eabfda3f..a660171c 100755
--- a/config_brpc.sh
+++ b/config_brpc.sh
@@ -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
diff --git a/example/thrift_extension_c++/native_client.cpp b/example/thrift_extension_c++/native_client.cpp
index f157a12e..29d9e5c0 100644
--- a/example/thrift_extension_c++/native_client.cpp
+++ b/example/thrift_extension_c++/native_client.cpp
@@ -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
 
diff --git a/example/thrift_extension_c++/native_server.cpp b/example/thrift_extension_c++/native_server.cpp
index c501a493..1ba3d1d4 100755
--- a/example/thrift_extension_c++/native_server.cpp
+++ b/example/thrift_extension_c++/native_server.cpp
@@ -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
 
@@ -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));
@@ -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);
 
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index dc1d6fb9..1b4b2332 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -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)
@@ -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)
 
diff --git a/src/brpc/policy/thrift_protocol.cpp b/src/brpc/policy/thrift_protocol.cpp
index a746cb0a..d53ec5e9 100755
--- a/src/brpc/policy/thrift_protocol.cpp
+++ b/src/brpc/policy/thrift_protocol.cpp
@@ -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
 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org